1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016 Avago Technologies. All rights reserved.
4 */
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/module.h>
7 #include <linux/parser.h>
8 #include <uapi/scsi/fc/fc_fs.h>
9 #include <uapi/scsi/fc/fc_els.h>
10 #include <linux/delay.h>
11 #include <linux/overflow.h>
12 #include <linux/blk-cgroup.h>
13 #include "nvme.h"
14 #include "fabrics.h"
15 #include <linux/nvme-fc-driver.h>
16 #include <linux/nvme-fc.h>
17 #include "fc.h"
18 #include <scsi/scsi_transport_fc.h>
19
20 /* *************************** Data Structures/Defines ****************** */
21
22
23 enum nvme_fc_queue_flags {
24 NVME_FC_Q_CONNECTED = 0,
25 NVME_FC_Q_LIVE,
26 };
27
28 #define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */
29 #define NVME_FC_DEFAULT_RECONNECT_TMO 2 /* delay between reconnects
30 * when connected and a
31 * connection failure.
32 */
33
34 struct nvme_fc_queue {
35 struct nvme_fc_ctrl *ctrl;
36 struct device *dev;
37 struct blk_mq_hw_ctx *hctx;
38 void *lldd_handle;
39 size_t cmnd_capsule_len;
40 u32 qnum;
41 u32 rqcnt;
42 u32 seqno;
43
44 u64 connection_id;
45 atomic_t csn;
46
47 unsigned long flags;
48 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
49
50 enum nvme_fcop_flags {
51 FCOP_FLAGS_TERMIO = (1 << 0),
52 FCOP_FLAGS_AEN = (1 << 1),
53 };
54
55 struct nvmefc_ls_req_op {
56 struct nvmefc_ls_req ls_req;
57
58 struct nvme_fc_rport *rport;
59 struct nvme_fc_queue *queue;
60 struct request *rq;
61 u32 flags;
62
63 int ls_error;
64 struct completion ls_done;
65 struct list_head lsreq_list; /* rport->ls_req_list */
66 bool req_queued;
67 };
68
69 struct nvmefc_ls_rcv_op {
70 struct nvme_fc_rport *rport;
71 struct nvmefc_ls_rsp *lsrsp;
72 union nvmefc_ls_requests *rqstbuf;
73 union nvmefc_ls_responses *rspbuf;
74 u16 rqstdatalen;
75 bool handled;
76 dma_addr_t rspdma;
77 struct list_head lsrcv_list; /* rport->ls_rcv_list */
78 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
79
80 enum nvme_fcpop_state {
81 FCPOP_STATE_UNINIT = 0,
82 FCPOP_STATE_IDLE = 1,
83 FCPOP_STATE_ACTIVE = 2,
84 FCPOP_STATE_ABORTED = 3,
85 FCPOP_STATE_COMPLETE = 4,
86 };
87
88 struct nvme_fc_fcp_op {
89 struct nvme_request nreq; /*
90 * nvme/host/core.c
91 * requires this to be
92 * the 1st element in the
93 * private structure
94 * associated with the
95 * request.
96 */
97 struct nvmefc_fcp_req fcp_req;
98
99 struct nvme_fc_ctrl *ctrl;
100 struct nvme_fc_queue *queue;
101 struct request *rq;
102
103 atomic_t state;
104 u32 flags;
105 u32 rqno;
106 u32 nents;
107
108 struct nvme_fc_cmd_iu cmd_iu;
109 struct nvme_fc_ersp_iu rsp_iu;
110 };
111
112 struct nvme_fcp_op_w_sgl {
113 struct nvme_fc_fcp_op op;
114 struct scatterlist sgl[NVME_INLINE_SG_CNT];
115 uint8_t priv[];
116 };
117
118 struct nvme_fc_lport {
119 struct nvme_fc_local_port localport;
120
121 struct ida endp_cnt;
122 struct list_head port_list; /* nvme_fc_port_list */
123 struct list_head endp_list;
124 struct device *dev; /* physical device for dma */
125 struct nvme_fc_port_template *ops;
126 struct kref ref;
127 atomic_t act_rport_cnt;
128 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
129
130 struct nvme_fc_rport {
131 struct nvme_fc_remote_port remoteport;
132
133 struct list_head endp_list; /* for lport->endp_list */
134 struct list_head ctrl_list;
135 struct list_head ls_req_list;
136 struct list_head ls_rcv_list;
137 struct list_head disc_list;
138 struct device *dev; /* physical device for dma */
139 struct nvme_fc_lport *lport;
140 spinlock_t lock;
141 struct kref ref;
142 atomic_t act_ctrl_cnt;
143 unsigned long dev_loss_end;
144 struct work_struct lsrcv_work;
145 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
146
147 /* fc_ctrl flags values - specified as bit positions */
148 #define ASSOC_ACTIVE 0
149 #define ASSOC_FAILED 1
150 #define FCCTRL_TERMIO 2
151
152 struct nvme_fc_ctrl {
153 spinlock_t lock;
154 struct nvme_fc_queue *queues;
155 struct device *dev;
156 struct nvme_fc_lport *lport;
157 struct nvme_fc_rport *rport;
158 u32 cnum;
159
160 bool ioq_live;
161 u64 association_id;
162 struct nvmefc_ls_rcv_op *rcv_disconn;
163
164 struct list_head ctrl_list; /* rport->ctrl_list */
165
166 struct blk_mq_tag_set admin_tag_set;
167 struct blk_mq_tag_set tag_set;
168
169 struct work_struct ioerr_work;
170 struct delayed_work connect_work;
171
172 struct kref ref;
173 unsigned long flags;
174 u32 iocnt;
175 wait_queue_head_t ioabort_wait;
176
177 struct nvme_fc_fcp_op aen_ops[NVME_NR_AEN_COMMANDS];
178
179 struct nvme_ctrl ctrl;
180 };
181
182 static inline struct nvme_fc_ctrl *
to_fc_ctrl(struct nvme_ctrl * ctrl)183 to_fc_ctrl(struct nvme_ctrl *ctrl)
184 {
185 return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
186 }
187
188 static inline struct nvme_fc_lport *
localport_to_lport(struct nvme_fc_local_port * portptr)189 localport_to_lport(struct nvme_fc_local_port *portptr)
190 {
191 return container_of(portptr, struct nvme_fc_lport, localport);
192 }
193
194 static inline struct nvme_fc_rport *
remoteport_to_rport(struct nvme_fc_remote_port * portptr)195 remoteport_to_rport(struct nvme_fc_remote_port *portptr)
196 {
197 return container_of(portptr, struct nvme_fc_rport, remoteport);
198 }
199
200 static inline struct nvmefc_ls_req_op *
ls_req_to_lsop(struct nvmefc_ls_req * lsreq)201 ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
202 {
203 return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
204 }
205
206 static inline struct nvme_fc_fcp_op *
fcp_req_to_fcp_op(struct nvmefc_fcp_req * fcpreq)207 fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
208 {
209 return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
210 }
211
212
213
214 /* *************************** Globals **************************** */
215
216
217 static DEFINE_SPINLOCK(nvme_fc_lock);
218
219 static LIST_HEAD(nvme_fc_lport_list);
220 static DEFINE_IDA(nvme_fc_local_port_cnt);
221 static DEFINE_IDA(nvme_fc_ctrl_cnt);
222
223 /*
224 * These items are short-term. They will eventually be moved into
225 * a generic FC class. See comments in module init.
226 */
227 static struct device *fc_udev_device;
228
229 static void nvme_fc_complete_rq(struct request *rq);
230
231 /* *********************** FC-NVME Port Management ************************ */
232
233 static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
234 struct nvme_fc_queue *, unsigned int);
235
236 static void nvme_fc_handle_ls_rqst_work(struct work_struct *work);
237
238
239 static void
nvme_fc_free_lport(struct kref * ref)240 nvme_fc_free_lport(struct kref *ref)
241 {
242 struct nvme_fc_lport *lport =
243 container_of(ref, struct nvme_fc_lport, ref);
244 unsigned long flags;
245
246 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
247 WARN_ON(!list_empty(&lport->endp_list));
248
249 /* remove from transport list */
250 spin_lock_irqsave(&nvme_fc_lock, flags);
251 list_del(&lport->port_list);
252 spin_unlock_irqrestore(&nvme_fc_lock, flags);
253
254 ida_free(&nvme_fc_local_port_cnt, lport->localport.port_num);
255 ida_destroy(&lport->endp_cnt);
256
257 put_device(lport->dev);
258
259 kfree(lport);
260 }
261
262 static void
nvme_fc_lport_put(struct nvme_fc_lport * lport)263 nvme_fc_lport_put(struct nvme_fc_lport *lport)
264 {
265 kref_put(&lport->ref, nvme_fc_free_lport);
266 }
267
268 static int
nvme_fc_lport_get(struct nvme_fc_lport * lport)269 nvme_fc_lport_get(struct nvme_fc_lport *lport)
270 {
271 return kref_get_unless_zero(&lport->ref);
272 }
273
274
275 static struct nvme_fc_lport *
nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info * pinfo,struct nvme_fc_port_template * ops,struct device * dev)276 nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo,
277 struct nvme_fc_port_template *ops,
278 struct device *dev)
279 {
280 struct nvme_fc_lport *lport;
281 unsigned long flags;
282
283 spin_lock_irqsave(&nvme_fc_lock, flags);
284
285 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
286 if (lport->localport.node_name != pinfo->node_name ||
287 lport->localport.port_name != pinfo->port_name)
288 continue;
289
290 if (lport->dev != dev) {
291 lport = ERR_PTR(-EXDEV);
292 goto out_done;
293 }
294
295 if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
296 lport = ERR_PTR(-EEXIST);
297 goto out_done;
298 }
299
300 if (!nvme_fc_lport_get(lport)) {
301 /*
302 * fails if ref cnt already 0. If so,
303 * act as if lport already deleted
304 */
305 lport = NULL;
306 goto out_done;
307 }
308
309 /* resume the lport */
310
311 lport->ops = ops;
312 lport->localport.port_role = pinfo->port_role;
313 lport->localport.port_id = pinfo->port_id;
314 lport->localport.port_state = FC_OBJSTATE_ONLINE;
315
316 spin_unlock_irqrestore(&nvme_fc_lock, flags);
317
318 return lport;
319 }
320
321 lport = NULL;
322
323 out_done:
324 spin_unlock_irqrestore(&nvme_fc_lock, flags);
325
326 return lport;
327 }
328
329 /**
330 * nvme_fc_register_localport - transport entry point called by an
331 * LLDD to register the existence of a NVME
332 * host FC port.
333 * @pinfo: pointer to information about the port to be registered
334 * @template: LLDD entrypoints and operational parameters for the port
335 * @dev: physical hardware device node port corresponds to. Will be
336 * used for DMA mappings
337 * @portptr: pointer to a local port pointer. Upon success, the routine
338 * will allocate a nvme_fc_local_port structure and place its
339 * address in the local port pointer. Upon failure, local port
340 * pointer will be set to 0.
341 *
342 * Returns:
343 * a completion status. Must be 0 upon success; a negative errno
344 * (ex: -ENXIO) upon failure.
345 */
346 int
nvme_fc_register_localport(struct nvme_fc_port_info * pinfo,struct nvme_fc_port_template * template,struct device * dev,struct nvme_fc_local_port ** portptr)347 nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
348 struct nvme_fc_port_template *template,
349 struct device *dev,
350 struct nvme_fc_local_port **portptr)
351 {
352 struct nvme_fc_lport *newrec;
353 unsigned long flags;
354 int ret, idx;
355
356 if (!template->localport_delete || !template->remoteport_delete ||
357 !template->ls_req || !template->fcp_io ||
358 !template->ls_abort || !template->fcp_abort ||
359 !template->max_hw_queues || !template->max_sgl_segments ||
360 !template->max_dif_sgl_segments || !template->dma_boundary) {
361 ret = -EINVAL;
362 goto out_reghost_failed;
363 }
364
365 /*
366 * look to see if there is already a localport that had been
367 * deregistered and in the process of waiting for all the
368 * references to fully be removed. If the references haven't
369 * expired, we can simply re-enable the localport. Remoteports
370 * and controller reconnections should resume naturally.
371 */
372 newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
373
374 /* found an lport, but something about its state is bad */
375 if (IS_ERR(newrec)) {
376 ret = PTR_ERR(newrec);
377 goto out_reghost_failed;
378
379 /* found existing lport, which was resumed */
380 } else if (newrec) {
381 *portptr = &newrec->localport;
382 return 0;
383 }
384
385 /* nothing found - allocate a new localport struct */
386
387 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
388 GFP_KERNEL);
389 if (!newrec) {
390 ret = -ENOMEM;
391 goto out_reghost_failed;
392 }
393
394 idx = ida_alloc(&nvme_fc_local_port_cnt, GFP_KERNEL);
395 if (idx < 0) {
396 ret = -ENOSPC;
397 goto out_fail_kfree;
398 }
399
400 if (!get_device(dev) && dev) {
401 ret = -ENODEV;
402 goto out_ida_put;
403 }
404
405 INIT_LIST_HEAD(&newrec->port_list);
406 INIT_LIST_HEAD(&newrec->endp_list);
407 kref_init(&newrec->ref);
408 atomic_set(&newrec->act_rport_cnt, 0);
409 newrec->ops = template;
410 newrec->dev = dev;
411 ida_init(&newrec->endp_cnt);
412 if (template->local_priv_sz)
413 newrec->localport.private = &newrec[1];
414 else
415 newrec->localport.private = NULL;
416 newrec->localport.node_name = pinfo->node_name;
417 newrec->localport.port_name = pinfo->port_name;
418 newrec->localport.port_role = pinfo->port_role;
419 newrec->localport.port_id = pinfo->port_id;
420 newrec->localport.port_state = FC_OBJSTATE_ONLINE;
421 newrec->localport.port_num = idx;
422
423 spin_lock_irqsave(&nvme_fc_lock, flags);
424 list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
425 spin_unlock_irqrestore(&nvme_fc_lock, flags);
426
427 if (dev)
428 dma_set_seg_boundary(dev, template->dma_boundary);
429
430 *portptr = &newrec->localport;
431 return 0;
432
433 out_ida_put:
434 ida_free(&nvme_fc_local_port_cnt, idx);
435 out_fail_kfree:
436 kfree(newrec);
437 out_reghost_failed:
438 *portptr = NULL;
439
440 return ret;
441 }
442 EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
443
444 /**
445 * nvme_fc_unregister_localport - transport entry point called by an
446 * LLDD to deregister/remove a previously
447 * registered a NVME host FC port.
448 * @portptr: pointer to the (registered) local port that is to be deregistered.
449 *
450 * Returns:
451 * a completion status. Must be 0 upon success; a negative errno
452 * (ex: -ENXIO) upon failure.
453 */
454 int
nvme_fc_unregister_localport(struct nvme_fc_local_port * portptr)455 nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
456 {
457 struct nvme_fc_lport *lport = localport_to_lport(portptr);
458 unsigned long flags;
459
460 if (!portptr)
461 return -EINVAL;
462
463 spin_lock_irqsave(&nvme_fc_lock, flags);
464
465 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
466 spin_unlock_irqrestore(&nvme_fc_lock, flags);
467 return -EINVAL;
468 }
469 portptr->port_state = FC_OBJSTATE_DELETED;
470
471 spin_unlock_irqrestore(&nvme_fc_lock, flags);
472
473 if (atomic_read(&lport->act_rport_cnt) == 0)
474 lport->ops->localport_delete(&lport->localport);
475
476 nvme_fc_lport_put(lport);
477
478 return 0;
479 }
480 EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
481
482 /*
483 * TRADDR strings, per FC-NVME are fixed format:
484 * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
485 * udev event will only differ by prefix of what field is
486 * being specified:
487 * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
488 * 19 + 43 + null_fudge = 64 characters
489 */
490 #define FCNVME_TRADDR_LENGTH 64
491
492 static void
nvme_fc_signal_discovery_scan(struct nvme_fc_lport * lport,struct nvme_fc_rport * rport)493 nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
494 struct nvme_fc_rport *rport)
495 {
496 char hostaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_HOST_TRADDR=...*/
497 char tgtaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_TRADDR=...*/
498 char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
499
500 if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
501 return;
502
503 snprintf(hostaddr, sizeof(hostaddr),
504 "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
505 lport->localport.node_name, lport->localport.port_name);
506 snprintf(tgtaddr, sizeof(tgtaddr),
507 "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
508 rport->remoteport.node_name, rport->remoteport.port_name);
509 kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
510 }
511
512 static void
nvme_fc_free_rport(struct kref * ref)513 nvme_fc_free_rport(struct kref *ref)
514 {
515 struct nvme_fc_rport *rport =
516 container_of(ref, struct nvme_fc_rport, ref);
517 struct nvme_fc_lport *lport =
518 localport_to_lport(rport->remoteport.localport);
519 unsigned long flags;
520
521 WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
522 WARN_ON(!list_empty(&rport->ctrl_list));
523
524 /* remove from lport list */
525 spin_lock_irqsave(&nvme_fc_lock, flags);
526 list_del(&rport->endp_list);
527 spin_unlock_irqrestore(&nvme_fc_lock, flags);
528
529 WARN_ON(!list_empty(&rport->disc_list));
530 ida_free(&lport->endp_cnt, rport->remoteport.port_num);
531
532 kfree(rport);
533
534 nvme_fc_lport_put(lport);
535 }
536
537 static void
nvme_fc_rport_put(struct nvme_fc_rport * rport)538 nvme_fc_rport_put(struct nvme_fc_rport *rport)
539 {
540 kref_put(&rport->ref, nvme_fc_free_rport);
541 }
542
543 static int
nvme_fc_rport_get(struct nvme_fc_rport * rport)544 nvme_fc_rport_get(struct nvme_fc_rport *rport)
545 {
546 return kref_get_unless_zero(&rport->ref);
547 }
548
549 static void
nvme_fc_resume_controller(struct nvme_fc_ctrl * ctrl)550 nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl)
551 {
552 switch (nvme_ctrl_state(&ctrl->ctrl)) {
553 case NVME_CTRL_NEW:
554 case NVME_CTRL_CONNECTING:
555 /*
556 * As all reconnects were suppressed, schedule a
557 * connect.
558 */
559 dev_info(ctrl->ctrl.device,
560 "NVME-FC{%d}: connectivity re-established. "
561 "Attempting reconnect\n", ctrl->cnum);
562
563 queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
564 break;
565
566 case NVME_CTRL_RESETTING:
567 /*
568 * Controller is already in the process of terminating the
569 * association. No need to do anything further. The reconnect
570 * step will naturally occur after the reset completes.
571 */
572 break;
573
574 default:
575 /* no action to take - let it delete */
576 break;
577 }
578 }
579
580 static struct nvme_fc_rport *
nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport * lport,struct nvme_fc_port_info * pinfo)581 nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport,
582 struct nvme_fc_port_info *pinfo)
583 {
584 struct nvme_fc_rport *rport;
585 struct nvme_fc_ctrl *ctrl;
586 unsigned long flags;
587
588 spin_lock_irqsave(&nvme_fc_lock, flags);
589
590 list_for_each_entry(rport, &lport->endp_list, endp_list) {
591 if (rport->remoteport.node_name != pinfo->node_name ||
592 rport->remoteport.port_name != pinfo->port_name)
593 continue;
594
595 if (!nvme_fc_rport_get(rport)) {
596 rport = ERR_PTR(-ENOLCK);
597 goto out_done;
598 }
599
600 spin_unlock_irqrestore(&nvme_fc_lock, flags);
601
602 spin_lock_irqsave(&rport->lock, flags);
603
604 /* has it been unregistered */
605 if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) {
606 /* means lldd called us twice */
607 spin_unlock_irqrestore(&rport->lock, flags);
608 nvme_fc_rport_put(rport);
609 return ERR_PTR(-ESTALE);
610 }
611
612 rport->remoteport.port_role = pinfo->port_role;
613 rport->remoteport.port_id = pinfo->port_id;
614 rport->remoteport.port_state = FC_OBJSTATE_ONLINE;
615 rport->dev_loss_end = 0;
616
617 /*
618 * kick off a reconnect attempt on all associations to the
619 * remote port. A successful reconnects will resume i/o.
620 */
621 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
622 nvme_fc_resume_controller(ctrl);
623
624 spin_unlock_irqrestore(&rport->lock, flags);
625
626 return rport;
627 }
628
629 rport = NULL;
630
631 out_done:
632 spin_unlock_irqrestore(&nvme_fc_lock, flags);
633
634 return rport;
635 }
636
637 static inline void
__nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport * rport,struct nvme_fc_port_info * pinfo)638 __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport,
639 struct nvme_fc_port_info *pinfo)
640 {
641 if (pinfo->dev_loss_tmo)
642 rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
643 else
644 rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
645 }
646
647 /**
648 * nvme_fc_register_remoteport - transport entry point called by an
649 * LLDD to register the existence of a NVME
650 * subsystem FC port on its fabric.
651 * @localport: pointer to the (registered) local port that the remote
652 * subsystem port is connected to.
653 * @pinfo: pointer to information about the port to be registered
654 * @portptr: pointer to a remote port pointer. Upon success, the routine
655 * will allocate a nvme_fc_remote_port structure and place its
656 * address in the remote port pointer. Upon failure, remote port
657 * pointer will be set to 0.
658 *
659 * Returns:
660 * a completion status. Must be 0 upon success; a negative errno
661 * (ex: -ENXIO) upon failure.
662 */
663 int
nvme_fc_register_remoteport(struct nvme_fc_local_port * localport,struct nvme_fc_port_info * pinfo,struct nvme_fc_remote_port ** portptr)664 nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
665 struct nvme_fc_port_info *pinfo,
666 struct nvme_fc_remote_port **portptr)
667 {
668 struct nvme_fc_lport *lport = localport_to_lport(localport);
669 struct nvme_fc_rport *newrec;
670 unsigned long flags;
671 int ret, idx;
672
673 if (!nvme_fc_lport_get(lport)) {
674 ret = -ESHUTDOWN;
675 goto out_reghost_failed;
676 }
677
678 /*
679 * look to see if there is already a remoteport that is waiting
680 * for a reconnect (within dev_loss_tmo) with the same WWN's.
681 * If so, transition to it and reconnect.
682 */
683 newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
684
685 /* found an rport, but something about its state is bad */
686 if (IS_ERR(newrec)) {
687 ret = PTR_ERR(newrec);
688 goto out_lport_put;
689
690 /* found existing rport, which was resumed */
691 } else if (newrec) {
692 nvme_fc_lport_put(lport);
693 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
694 nvme_fc_signal_discovery_scan(lport, newrec);
695 *portptr = &newrec->remoteport;
696 return 0;
697 }
698
699 /* nothing found - allocate a new remoteport struct */
700
701 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
702 GFP_KERNEL);
703 if (!newrec) {
704 ret = -ENOMEM;
705 goto out_lport_put;
706 }
707
708 idx = ida_alloc(&lport->endp_cnt, GFP_KERNEL);
709 if (idx < 0) {
710 ret = -ENOSPC;
711 goto out_kfree_rport;
712 }
713
714 INIT_LIST_HEAD(&newrec->endp_list);
715 INIT_LIST_HEAD(&newrec->ctrl_list);
716 INIT_LIST_HEAD(&newrec->ls_req_list);
717 INIT_LIST_HEAD(&newrec->disc_list);
718 kref_init(&newrec->ref);
719 atomic_set(&newrec->act_ctrl_cnt, 0);
720 spin_lock_init(&newrec->lock);
721 newrec->remoteport.localport = &lport->localport;
722 INIT_LIST_HEAD(&newrec->ls_rcv_list);
723 newrec->dev = lport->dev;
724 newrec->lport = lport;
725 if (lport->ops->remote_priv_sz)
726 newrec->remoteport.private = &newrec[1];
727 else
728 newrec->remoteport.private = NULL;
729 newrec->remoteport.port_role = pinfo->port_role;
730 newrec->remoteport.node_name = pinfo->node_name;
731 newrec->remoteport.port_name = pinfo->port_name;
732 newrec->remoteport.port_id = pinfo->port_id;
733 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
734 newrec->remoteport.port_num = idx;
735 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
736 INIT_WORK(&newrec->lsrcv_work, nvme_fc_handle_ls_rqst_work);
737
738 spin_lock_irqsave(&nvme_fc_lock, flags);
739 list_add_tail(&newrec->endp_list, &lport->endp_list);
740 spin_unlock_irqrestore(&nvme_fc_lock, flags);
741
742 nvme_fc_signal_discovery_scan(lport, newrec);
743
744 *portptr = &newrec->remoteport;
745 return 0;
746
747 out_kfree_rport:
748 kfree(newrec);
749 out_lport_put:
750 nvme_fc_lport_put(lport);
751 out_reghost_failed:
752 *portptr = NULL;
753 return ret;
754 }
755 EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
756
757 static int
nvme_fc_abort_lsops(struct nvme_fc_rport * rport)758 nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
759 {
760 struct nvmefc_ls_req_op *lsop;
761 unsigned long flags;
762
763 restart:
764 spin_lock_irqsave(&rport->lock, flags);
765
766 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
767 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
768 lsop->flags |= FCOP_FLAGS_TERMIO;
769 spin_unlock_irqrestore(&rport->lock, flags);
770 rport->lport->ops->ls_abort(&rport->lport->localport,
771 &rport->remoteport,
772 &lsop->ls_req);
773 goto restart;
774 }
775 }
776 spin_unlock_irqrestore(&rport->lock, flags);
777
778 return 0;
779 }
780
781 static void
nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl * ctrl)782 nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
783 {
784 dev_info(ctrl->ctrl.device,
785 "NVME-FC{%d}: controller connectivity lost. Awaiting "
786 "Reconnect", ctrl->cnum);
787
788 set_bit(ASSOC_FAILED, &ctrl->flags);
789 nvme_reset_ctrl(&ctrl->ctrl);
790 }
791
792 /**
793 * nvme_fc_unregister_remoteport - transport entry point called by an
794 * LLDD to deregister/remove a previously
795 * registered a NVME subsystem FC port.
796 * @portptr: pointer to the (registered) remote port that is to be
797 * deregistered.
798 *
799 * Returns:
800 * a completion status. Must be 0 upon success; a negative errno
801 * (ex: -ENXIO) upon failure.
802 */
803 int
nvme_fc_unregister_remoteport(struct nvme_fc_remote_port * portptr)804 nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
805 {
806 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
807 struct nvme_fc_ctrl *ctrl;
808 unsigned long flags;
809
810 if (!portptr)
811 return -EINVAL;
812
813 spin_lock_irqsave(&rport->lock, flags);
814
815 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
816 spin_unlock_irqrestore(&rport->lock, flags);
817 return -EINVAL;
818 }
819 portptr->port_state = FC_OBJSTATE_DELETED;
820
821 rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
822
823 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
824 /* if dev_loss_tmo==0, dev loss is immediate */
825 if (!portptr->dev_loss_tmo) {
826 dev_warn(ctrl->ctrl.device,
827 "NVME-FC{%d}: controller connectivity lost.\n",
828 ctrl->cnum);
829 nvme_delete_ctrl(&ctrl->ctrl);
830 } else
831 nvme_fc_ctrl_connectivity_loss(ctrl);
832 }
833
834 spin_unlock_irqrestore(&rport->lock, flags);
835
836 nvme_fc_abort_lsops(rport);
837
838 if (atomic_read(&rport->act_ctrl_cnt) == 0)
839 rport->lport->ops->remoteport_delete(portptr);
840
841 /*
842 * release the reference, which will allow, if all controllers
843 * go away, which should only occur after dev_loss_tmo occurs,
844 * for the rport to be torn down.
845 */
846 nvme_fc_rport_put(rport);
847
848 return 0;
849 }
850 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
851
852 /**
853 * nvme_fc_rescan_remoteport - transport entry point called by an
854 * LLDD to request a nvme device rescan.
855 * @remoteport: pointer to the (registered) remote port that is to be
856 * rescanned.
857 *
858 * Returns: N/A
859 */
860 void
nvme_fc_rescan_remoteport(struct nvme_fc_remote_port * remoteport)861 nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
862 {
863 struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
864
865 nvme_fc_signal_discovery_scan(rport->lport, rport);
866 }
867 EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
868
869 int
nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port * portptr,u32 dev_loss_tmo)870 nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
871 u32 dev_loss_tmo)
872 {
873 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
874 unsigned long flags;
875
876 spin_lock_irqsave(&rport->lock, flags);
877
878 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
879 spin_unlock_irqrestore(&rport->lock, flags);
880 return -EINVAL;
881 }
882
883 /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
884 rport->remoteport.dev_loss_tmo = dev_loss_tmo;
885
886 spin_unlock_irqrestore(&rport->lock, flags);
887
888 return 0;
889 }
890 EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
891
892
893 /* *********************** FC-NVME DMA Handling **************************** */
894
895 /*
896 * The fcloop device passes in a NULL device pointer. Real LLD's will
897 * pass in a valid device pointer. If NULL is passed to the dma mapping
898 * routines, depending on the platform, it may or may not succeed, and
899 * may crash.
900 *
901 * As such:
902 * Wrapper all the dma routines and check the dev pointer.
903 *
904 * If simple mappings (return just a dma address, we'll noop them,
905 * returning a dma address of 0.
906 *
907 * On more complex mappings (dma_map_sg), a pseudo routine fills
908 * in the scatter list, setting all dma addresses to 0.
909 */
910
911 static inline dma_addr_t
fc_dma_map_single(struct device * dev,void * ptr,size_t size,enum dma_data_direction dir)912 fc_dma_map_single(struct device *dev, void *ptr, size_t size,
913 enum dma_data_direction dir)
914 {
915 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
916 }
917
918 static inline int
fc_dma_mapping_error(struct device * dev,dma_addr_t dma_addr)919 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
920 {
921 return dev ? dma_mapping_error(dev, dma_addr) : 0;
922 }
923
924 static inline void
fc_dma_unmap_single(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)925 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
926 enum dma_data_direction dir)
927 {
928 if (dev)
929 dma_unmap_single(dev, addr, size, dir);
930 }
931
932 static inline void
fc_dma_sync_single_for_cpu(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)933 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
934 enum dma_data_direction dir)
935 {
936 if (dev)
937 dma_sync_single_for_cpu(dev, addr, size, dir);
938 }
939
940 static inline void
fc_dma_sync_single_for_device(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)941 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
942 enum dma_data_direction dir)
943 {
944 if (dev)
945 dma_sync_single_for_device(dev, addr, size, dir);
946 }
947
948 /* pseudo dma_map_sg call */
949 static int
fc_map_sg(struct scatterlist * sg,int nents)950 fc_map_sg(struct scatterlist *sg, int nents)
951 {
952 struct scatterlist *s;
953 int i;
954
955 WARN_ON(nents == 0 || sg[0].length == 0);
956
957 for_each_sg(sg, s, nents, i) {
958 s->dma_address = 0L;
959 #ifdef CONFIG_NEED_SG_DMA_LENGTH
960 s->dma_length = s->length;
961 #endif
962 }
963 return nents;
964 }
965
966 static inline int
fc_dma_map_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)967 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
968 enum dma_data_direction dir)
969 {
970 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
971 }
972
973 static inline void
fc_dma_unmap_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)974 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
975 enum dma_data_direction dir)
976 {
977 if (dev)
978 dma_unmap_sg(dev, sg, nents, dir);
979 }
980
981 /* *********************** FC-NVME LS Handling **************************** */
982
983 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
984 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
985
986 static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
987
988 static void
__nvme_fc_finish_ls_req(struct nvmefc_ls_req_op * lsop)989 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
990 {
991 struct nvme_fc_rport *rport = lsop->rport;
992 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
993 unsigned long flags;
994
995 spin_lock_irqsave(&rport->lock, flags);
996
997 if (!lsop->req_queued) {
998 spin_unlock_irqrestore(&rport->lock, flags);
999 return;
1000 }
1001
1002 list_del(&lsop->lsreq_list);
1003
1004 lsop->req_queued = false;
1005
1006 spin_unlock_irqrestore(&rport->lock, flags);
1007
1008 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1009 (lsreq->rqstlen + lsreq->rsplen),
1010 DMA_BIDIRECTIONAL);
1011
1012 nvme_fc_rport_put(rport);
1013 }
1014
1015 static int
__nvme_fc_send_ls_req(struct nvme_fc_rport * rport,struct nvmefc_ls_req_op * lsop,void (* done)(struct nvmefc_ls_req * req,int status))1016 __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
1017 struct nvmefc_ls_req_op *lsop,
1018 void (*done)(struct nvmefc_ls_req *req, int status))
1019 {
1020 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1021 unsigned long flags;
1022 int ret = 0;
1023
1024 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1025 return -ECONNREFUSED;
1026
1027 if (!nvme_fc_rport_get(rport))
1028 return -ESHUTDOWN;
1029
1030 lsreq->done = done;
1031 lsop->rport = rport;
1032 lsop->req_queued = false;
1033 INIT_LIST_HEAD(&lsop->lsreq_list);
1034 init_completion(&lsop->ls_done);
1035
1036 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
1037 lsreq->rqstlen + lsreq->rsplen,
1038 DMA_BIDIRECTIONAL);
1039 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1040 ret = -EFAULT;
1041 goto out_putrport;
1042 }
1043 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1044
1045 spin_lock_irqsave(&rport->lock, flags);
1046
1047 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
1048
1049 lsop->req_queued = true;
1050
1051 spin_unlock_irqrestore(&rport->lock, flags);
1052
1053 ret = rport->lport->ops->ls_req(&rport->lport->localport,
1054 &rport->remoteport, lsreq);
1055 if (ret)
1056 goto out_unlink;
1057
1058 return 0;
1059
1060 out_unlink:
1061 lsop->ls_error = ret;
1062 spin_lock_irqsave(&rport->lock, flags);
1063 lsop->req_queued = false;
1064 list_del(&lsop->lsreq_list);
1065 spin_unlock_irqrestore(&rport->lock, flags);
1066 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1067 (lsreq->rqstlen + lsreq->rsplen),
1068 DMA_BIDIRECTIONAL);
1069 out_putrport:
1070 nvme_fc_rport_put(rport);
1071
1072 return ret;
1073 }
1074
1075 static void
nvme_fc_send_ls_req_done(struct nvmefc_ls_req * lsreq,int status)1076 nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1077 {
1078 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1079
1080 lsop->ls_error = status;
1081 complete(&lsop->ls_done);
1082 }
1083
1084 static int
nvme_fc_send_ls_req(struct nvme_fc_rport * rport,struct nvmefc_ls_req_op * lsop)1085 nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
1086 {
1087 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1088 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1089 int ret;
1090
1091 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
1092
1093 if (!ret) {
1094 /*
1095 * No timeout/not interruptible as we need the struct
1096 * to exist until the lldd calls us back. Thus mandate
1097 * wait until driver calls back. lldd responsible for
1098 * the timeout action
1099 */
1100 wait_for_completion(&lsop->ls_done);
1101
1102 __nvme_fc_finish_ls_req(lsop);
1103
1104 ret = lsop->ls_error;
1105 }
1106
1107 if (ret)
1108 return ret;
1109
1110 /* ACC or RJT payload ? */
1111 if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1112 return -ENXIO;
1113
1114 return 0;
1115 }
1116
1117 static int
nvme_fc_send_ls_req_async(struct nvme_fc_rport * rport,struct nvmefc_ls_req_op * lsop,void (* done)(struct nvmefc_ls_req * req,int status))1118 nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
1119 struct nvmefc_ls_req_op *lsop,
1120 void (*done)(struct nvmefc_ls_req *req, int status))
1121 {
1122 /* don't wait for completion */
1123
1124 return __nvme_fc_send_ls_req(rport, lsop, done);
1125 }
1126
1127 static int
nvme_fc_connect_admin_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,u16 qsize,u16 ersp_ratio)1128 nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1129 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1130 {
1131 struct nvmefc_ls_req_op *lsop;
1132 struct nvmefc_ls_req *lsreq;
1133 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1134 struct fcnvme_ls_cr_assoc_acc *assoc_acc;
1135 unsigned long flags;
1136 int ret, fcret = 0;
1137
1138 lsop = kzalloc((sizeof(*lsop) +
1139 sizeof(*assoc_rqst) + sizeof(*assoc_acc) +
1140 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1141 if (!lsop) {
1142 dev_info(ctrl->ctrl.device,
1143 "NVME-FC{%d}: send Create Association failed: ENOMEM\n",
1144 ctrl->cnum);
1145 ret = -ENOMEM;
1146 goto out_no_memory;
1147 }
1148
1149 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1];
1150 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1151 lsreq = &lsop->ls_req;
1152 if (ctrl->lport->ops->lsrqst_priv_sz)
1153 lsreq->private = &assoc_acc[1];
1154 else
1155 lsreq->private = NULL;
1156
1157 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1158 assoc_rqst->desc_list_len =
1159 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1160
1161 assoc_rqst->assoc_cmd.desc_tag =
1162 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1163 assoc_rqst->assoc_cmd.desc_len =
1164 fcnvme_lsdesc_len(
1165 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1166
1167 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1168 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
1169 /* Linux supports only Dynamic controllers */
1170 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
1171 uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1172 strscpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1173 sizeof(assoc_rqst->assoc_cmd.hostnqn));
1174 strscpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1175 sizeof(assoc_rqst->assoc_cmd.subnqn));
1176
1177 lsop->queue = queue;
1178 lsreq->rqstaddr = assoc_rqst;
1179 lsreq->rqstlen = sizeof(*assoc_rqst);
1180 lsreq->rspaddr = assoc_acc;
1181 lsreq->rsplen = sizeof(*assoc_acc);
1182 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1183
1184 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1185 if (ret)
1186 goto out_free_buffer;
1187
1188 /* process connect LS completion */
1189
1190 /* validate the ACC response */
1191 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1192 fcret = VERR_LSACC;
1193 else if (assoc_acc->hdr.desc_list_len !=
1194 fcnvme_lsdesc_len(
1195 sizeof(struct fcnvme_ls_cr_assoc_acc)))
1196 fcret = VERR_CR_ASSOC_ACC_LEN;
1197 else if (assoc_acc->hdr.rqst.desc_tag !=
1198 cpu_to_be32(FCNVME_LSDESC_RQST))
1199 fcret = VERR_LSDESC_RQST;
1200 else if (assoc_acc->hdr.rqst.desc_len !=
1201 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1202 fcret = VERR_LSDESC_RQST_LEN;
1203 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1204 fcret = VERR_CR_ASSOC;
1205 else if (assoc_acc->associd.desc_tag !=
1206 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1207 fcret = VERR_ASSOC_ID;
1208 else if (assoc_acc->associd.desc_len !=
1209 fcnvme_lsdesc_len(
1210 sizeof(struct fcnvme_lsdesc_assoc_id)))
1211 fcret = VERR_ASSOC_ID_LEN;
1212 else if (assoc_acc->connectid.desc_tag !=
1213 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1214 fcret = VERR_CONN_ID;
1215 else if (assoc_acc->connectid.desc_len !=
1216 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1217 fcret = VERR_CONN_ID_LEN;
1218
1219 if (fcret) {
1220 ret = -EBADF;
1221 dev_err(ctrl->dev,
1222 "q %d Create Association LS failed: %s\n",
1223 queue->qnum, validation_errors[fcret]);
1224 } else {
1225 spin_lock_irqsave(&ctrl->lock, flags);
1226 ctrl->association_id =
1227 be64_to_cpu(assoc_acc->associd.association_id);
1228 queue->connection_id =
1229 be64_to_cpu(assoc_acc->connectid.connection_id);
1230 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1231 spin_unlock_irqrestore(&ctrl->lock, flags);
1232 }
1233
1234 out_free_buffer:
1235 kfree(lsop);
1236 out_no_memory:
1237 if (ret)
1238 dev_err(ctrl->dev,
1239 "queue %d connect admin queue failed (%d).\n",
1240 queue->qnum, ret);
1241 return ret;
1242 }
1243
1244 static int
nvme_fc_connect_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,u16 qsize,u16 ersp_ratio)1245 nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1246 u16 qsize, u16 ersp_ratio)
1247 {
1248 struct nvmefc_ls_req_op *lsop;
1249 struct nvmefc_ls_req *lsreq;
1250 struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1251 struct fcnvme_ls_cr_conn_acc *conn_acc;
1252 int ret, fcret = 0;
1253
1254 lsop = kzalloc((sizeof(*lsop) +
1255 sizeof(*conn_rqst) + sizeof(*conn_acc) +
1256 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1257 if (!lsop) {
1258 dev_info(ctrl->ctrl.device,
1259 "NVME-FC{%d}: send Create Connection failed: ENOMEM\n",
1260 ctrl->cnum);
1261 ret = -ENOMEM;
1262 goto out_no_memory;
1263 }
1264
1265 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)&lsop[1];
1266 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1267 lsreq = &lsop->ls_req;
1268 if (ctrl->lport->ops->lsrqst_priv_sz)
1269 lsreq->private = (void *)&conn_acc[1];
1270 else
1271 lsreq->private = NULL;
1272
1273 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1274 conn_rqst->desc_list_len = cpu_to_be32(
1275 sizeof(struct fcnvme_lsdesc_assoc_id) +
1276 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1277
1278 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1279 conn_rqst->associd.desc_len =
1280 fcnvme_lsdesc_len(
1281 sizeof(struct fcnvme_lsdesc_assoc_id));
1282 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1283 conn_rqst->connect_cmd.desc_tag =
1284 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1285 conn_rqst->connect_cmd.desc_len =
1286 fcnvme_lsdesc_len(
1287 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1288 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1289 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
1290 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
1291
1292 lsop->queue = queue;
1293 lsreq->rqstaddr = conn_rqst;
1294 lsreq->rqstlen = sizeof(*conn_rqst);
1295 lsreq->rspaddr = conn_acc;
1296 lsreq->rsplen = sizeof(*conn_acc);
1297 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1298
1299 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1300 if (ret)
1301 goto out_free_buffer;
1302
1303 /* process connect LS completion */
1304
1305 /* validate the ACC response */
1306 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1307 fcret = VERR_LSACC;
1308 else if (conn_acc->hdr.desc_list_len !=
1309 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1310 fcret = VERR_CR_CONN_ACC_LEN;
1311 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1312 fcret = VERR_LSDESC_RQST;
1313 else if (conn_acc->hdr.rqst.desc_len !=
1314 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1315 fcret = VERR_LSDESC_RQST_LEN;
1316 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1317 fcret = VERR_CR_CONN;
1318 else if (conn_acc->connectid.desc_tag !=
1319 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1320 fcret = VERR_CONN_ID;
1321 else if (conn_acc->connectid.desc_len !=
1322 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1323 fcret = VERR_CONN_ID_LEN;
1324
1325 if (fcret) {
1326 ret = -EBADF;
1327 dev_err(ctrl->dev,
1328 "q %d Create I/O Connection LS failed: %s\n",
1329 queue->qnum, validation_errors[fcret]);
1330 } else {
1331 queue->connection_id =
1332 be64_to_cpu(conn_acc->connectid.connection_id);
1333 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1334 }
1335
1336 out_free_buffer:
1337 kfree(lsop);
1338 out_no_memory:
1339 if (ret)
1340 dev_err(ctrl->dev,
1341 "queue %d connect I/O queue failed (%d).\n",
1342 queue->qnum, ret);
1343 return ret;
1344 }
1345
1346 static void
nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req * lsreq,int status)1347 nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1348 {
1349 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1350
1351 __nvme_fc_finish_ls_req(lsop);
1352
1353 /* fc-nvme initiator doesn't care about success or failure of cmd */
1354
1355 kfree(lsop);
1356 }
1357
1358 /*
1359 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1360 * the FC-NVME Association. Terminating the association also
1361 * terminates the FC-NVME connections (per queue, both admin and io
1362 * queues) that are part of the association. E.g. things are torn
1363 * down, and the related FC-NVME Association ID and Connection IDs
1364 * become invalid.
1365 *
1366 * The behavior of the fc-nvme initiator is such that it's
1367 * understanding of the association and connections will implicitly
1368 * be torn down. The action is implicit as it may be due to a loss of
1369 * connectivity with the fc-nvme target, so you may never get a
1370 * response even if you tried. As such, the action of this routine
1371 * is to asynchronously send the LS, ignore any results of the LS, and
1372 * continue on with terminating the association. If the fc-nvme target
1373 * is present and receives the LS, it too can tear down.
1374 */
1375 static void
nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl * ctrl)1376 nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1377 {
1378 struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
1379 struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
1380 struct nvmefc_ls_req_op *lsop;
1381 struct nvmefc_ls_req *lsreq;
1382 int ret;
1383
1384 lsop = kzalloc((sizeof(*lsop) +
1385 sizeof(*discon_rqst) + sizeof(*discon_acc) +
1386 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1387 if (!lsop) {
1388 dev_info(ctrl->ctrl.device,
1389 "NVME-FC{%d}: send Disconnect Association "
1390 "failed: ENOMEM\n",
1391 ctrl->cnum);
1392 return;
1393 }
1394
1395 discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)&lsop[1];
1396 discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
1397 lsreq = &lsop->ls_req;
1398 if (ctrl->lport->ops->lsrqst_priv_sz)
1399 lsreq->private = (void *)&discon_acc[1];
1400 else
1401 lsreq->private = NULL;
1402
1403 nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc,
1404 ctrl->association_id);
1405
1406 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1407 nvme_fc_disconnect_assoc_done);
1408 if (ret)
1409 kfree(lsop);
1410 }
1411
1412 static void
nvme_fc_xmt_ls_rsp_free(struct nvmefc_ls_rcv_op * lsop)1413 nvme_fc_xmt_ls_rsp_free(struct nvmefc_ls_rcv_op *lsop)
1414 {
1415 struct nvme_fc_rport *rport = lsop->rport;
1416 struct nvme_fc_lport *lport = rport->lport;
1417 unsigned long flags;
1418
1419 spin_lock_irqsave(&rport->lock, flags);
1420 list_del(&lsop->lsrcv_list);
1421 spin_unlock_irqrestore(&rport->lock, flags);
1422
1423 fc_dma_sync_single_for_cpu(lport->dev, lsop->rspdma,
1424 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1425 fc_dma_unmap_single(lport->dev, lsop->rspdma,
1426 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1427
1428 kfree(lsop->rspbuf);
1429 kfree(lsop->rqstbuf);
1430 kfree(lsop);
1431
1432 nvme_fc_rport_put(rport);
1433 }
1434
1435 static void
nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp * lsrsp)1436 nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
1437 {
1438 struct nvmefc_ls_rcv_op *lsop = lsrsp->nvme_fc_private;
1439
1440 nvme_fc_xmt_ls_rsp_free(lsop);
1441 }
1442
1443 static void
nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op * lsop)1444 nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op *lsop)
1445 {
1446 struct nvme_fc_rport *rport = lsop->rport;
1447 struct nvme_fc_lport *lport = rport->lport;
1448 struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
1449 int ret;
1450
1451 fc_dma_sync_single_for_device(lport->dev, lsop->rspdma,
1452 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1453
1454 ret = lport->ops->xmt_ls_rsp(&lport->localport, &rport->remoteport,
1455 lsop->lsrsp);
1456 if (ret) {
1457 dev_warn(lport->dev,
1458 "LLDD rejected LS RSP xmt: LS %d status %d\n",
1459 w0->ls_cmd, ret);
1460 nvme_fc_xmt_ls_rsp_free(lsop);
1461 return;
1462 }
1463 }
1464
1465 static struct nvme_fc_ctrl *
nvme_fc_match_disconn_ls(struct nvme_fc_rport * rport,struct nvmefc_ls_rcv_op * lsop)1466 nvme_fc_match_disconn_ls(struct nvme_fc_rport *rport,
1467 struct nvmefc_ls_rcv_op *lsop)
1468 {
1469 struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1470 &lsop->rqstbuf->rq_dis_assoc;
1471 struct nvme_fc_ctrl *ctrl, *ret = NULL;
1472 struct nvmefc_ls_rcv_op *oldls = NULL;
1473 u64 association_id = be64_to_cpu(rqst->associd.association_id);
1474 unsigned long flags;
1475
1476 spin_lock_irqsave(&rport->lock, flags);
1477
1478 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
1479 if (!nvme_fc_ctrl_get(ctrl))
1480 continue;
1481 spin_lock(&ctrl->lock);
1482 if (association_id == ctrl->association_id) {
1483 oldls = ctrl->rcv_disconn;
1484 ctrl->rcv_disconn = lsop;
1485 ret = ctrl;
1486 }
1487 spin_unlock(&ctrl->lock);
1488 if (ret)
1489 /* leave the ctrl get reference */
1490 break;
1491 nvme_fc_ctrl_put(ctrl);
1492 }
1493
1494 spin_unlock_irqrestore(&rport->lock, flags);
1495
1496 /* transmit a response for anything that was pending */
1497 if (oldls) {
1498 dev_info(rport->lport->dev,
1499 "NVME-FC{%d}: Multiple Disconnect Association "
1500 "LS's received\n", ctrl->cnum);
1501 /* overwrite good response with bogus failure */
1502 oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf,
1503 sizeof(*oldls->rspbuf),
1504 rqst->w0.ls_cmd,
1505 FCNVME_RJT_RC_UNAB,
1506 FCNVME_RJT_EXP_NONE, 0);
1507 nvme_fc_xmt_ls_rsp(oldls);
1508 }
1509
1510 return ret;
1511 }
1512
1513 /*
1514 * returns true to mean LS handled and ls_rsp can be sent
1515 * returns false to defer ls_rsp xmt (will be done as part of
1516 * association termination)
1517 */
1518 static bool
nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op * lsop)1519 nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op *lsop)
1520 {
1521 struct nvme_fc_rport *rport = lsop->rport;
1522 struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1523 &lsop->rqstbuf->rq_dis_assoc;
1524 struct fcnvme_ls_disconnect_assoc_acc *acc =
1525 &lsop->rspbuf->rsp_dis_assoc;
1526 struct nvme_fc_ctrl *ctrl = NULL;
1527 int ret = 0;
1528
1529 memset(acc, 0, sizeof(*acc));
1530
1531 ret = nvmefc_vldt_lsreq_discon_assoc(lsop->rqstdatalen, rqst);
1532 if (!ret) {
1533 /* match an active association */
1534 ctrl = nvme_fc_match_disconn_ls(rport, lsop);
1535 if (!ctrl)
1536 ret = VERR_NO_ASSOC;
1537 }
1538
1539 if (ret) {
1540 dev_info(rport->lport->dev,
1541 "Disconnect LS failed: %s\n",
1542 validation_errors[ret]);
1543 lsop->lsrsp->rsplen = nvme_fc_format_rjt(acc,
1544 sizeof(*acc), rqst->w0.ls_cmd,
1545 (ret == VERR_NO_ASSOC) ?
1546 FCNVME_RJT_RC_INV_ASSOC :
1547 FCNVME_RJT_RC_LOGIC,
1548 FCNVME_RJT_EXP_NONE, 0);
1549 return true;
1550 }
1551
1552 /* format an ACCept response */
1553
1554 lsop->lsrsp->rsplen = sizeof(*acc);
1555
1556 nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1557 fcnvme_lsdesc_len(
1558 sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
1559 FCNVME_LS_DISCONNECT_ASSOC);
1560
1561 /*
1562 * the transmit of the response will occur after the exchanges
1563 * for the association have been ABTS'd by
1564 * nvme_fc_delete_association().
1565 */
1566
1567 /* fail the association */
1568 nvme_fc_error_recovery(ctrl, "Disconnect Association LS received");
1569
1570 /* release the reference taken by nvme_fc_match_disconn_ls() */
1571 nvme_fc_ctrl_put(ctrl);
1572
1573 return false;
1574 }
1575
1576 /*
1577 * Actual Processing routine for received FC-NVME LS Requests from the LLD
1578 * returns true if a response should be sent afterward, false if rsp will
1579 * be sent asynchronously.
1580 */
1581 static bool
nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op * lsop)1582 nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op *lsop)
1583 {
1584 struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
1585 bool ret = true;
1586
1587 lsop->lsrsp->nvme_fc_private = lsop;
1588 lsop->lsrsp->rspbuf = lsop->rspbuf;
1589 lsop->lsrsp->rspdma = lsop->rspdma;
1590 lsop->lsrsp->done = nvme_fc_xmt_ls_rsp_done;
1591 /* Be preventative. handlers will later set to valid length */
1592 lsop->lsrsp->rsplen = 0;
1593
1594 /*
1595 * handlers:
1596 * parse request input, execute the request, and format the
1597 * LS response
1598 */
1599 switch (w0->ls_cmd) {
1600 case FCNVME_LS_DISCONNECT_ASSOC:
1601 ret = nvme_fc_ls_disconnect_assoc(lsop);
1602 break;
1603 case FCNVME_LS_DISCONNECT_CONN:
1604 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1605 sizeof(*lsop->rspbuf), w0->ls_cmd,
1606 FCNVME_RJT_RC_UNSUP, FCNVME_RJT_EXP_NONE, 0);
1607 break;
1608 case FCNVME_LS_CREATE_ASSOCIATION:
1609 case FCNVME_LS_CREATE_CONNECTION:
1610 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1611 sizeof(*lsop->rspbuf), w0->ls_cmd,
1612 FCNVME_RJT_RC_LOGIC, FCNVME_RJT_EXP_NONE, 0);
1613 break;
1614 default:
1615 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1616 sizeof(*lsop->rspbuf), w0->ls_cmd,
1617 FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
1618 break;
1619 }
1620
1621 return(ret);
1622 }
1623
1624 static void
nvme_fc_handle_ls_rqst_work(struct work_struct * work)1625 nvme_fc_handle_ls_rqst_work(struct work_struct *work)
1626 {
1627 struct nvme_fc_rport *rport =
1628 container_of(work, struct nvme_fc_rport, lsrcv_work);
1629 struct fcnvme_ls_rqst_w0 *w0;
1630 struct nvmefc_ls_rcv_op *lsop;
1631 unsigned long flags;
1632 bool sendrsp;
1633
1634 restart:
1635 sendrsp = true;
1636 spin_lock_irqsave(&rport->lock, flags);
1637 list_for_each_entry(lsop, &rport->ls_rcv_list, lsrcv_list) {
1638 if (lsop->handled)
1639 continue;
1640
1641 lsop->handled = true;
1642 if (rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
1643 spin_unlock_irqrestore(&rport->lock, flags);
1644 sendrsp = nvme_fc_handle_ls_rqst(lsop);
1645 } else {
1646 spin_unlock_irqrestore(&rport->lock, flags);
1647 w0 = &lsop->rqstbuf->w0;
1648 lsop->lsrsp->rsplen = nvme_fc_format_rjt(
1649 lsop->rspbuf,
1650 sizeof(*lsop->rspbuf),
1651 w0->ls_cmd,
1652 FCNVME_RJT_RC_UNAB,
1653 FCNVME_RJT_EXP_NONE, 0);
1654 }
1655 if (sendrsp)
1656 nvme_fc_xmt_ls_rsp(lsop);
1657 goto restart;
1658 }
1659 spin_unlock_irqrestore(&rport->lock, flags);
1660 }
1661
1662 static
nvme_fc_rcv_ls_req_err_msg(struct nvme_fc_lport * lport,struct fcnvme_ls_rqst_w0 * w0)1663 void nvme_fc_rcv_ls_req_err_msg(struct nvme_fc_lport *lport,
1664 struct fcnvme_ls_rqst_w0 *w0)
1665 {
1666 dev_info(lport->dev, "RCV %s LS failed: No memory\n",
1667 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1668 nvmefc_ls_names[w0->ls_cmd] : "");
1669 }
1670
1671 /**
1672 * nvme_fc_rcv_ls_req - transport entry point called by an LLDD
1673 * upon the reception of a NVME LS request.
1674 *
1675 * The nvme-fc layer will copy payload to an internal structure for
1676 * processing. As such, upon completion of the routine, the LLDD may
1677 * immediately free/reuse the LS request buffer passed in the call.
1678 *
1679 * If this routine returns error, the LLDD should abort the exchange.
1680 *
1681 * @portptr: pointer to the (registered) remote port that the LS
1682 * was received from. The remoteport is associated with
1683 * a specific localport.
1684 * @lsrsp: pointer to a nvmefc_ls_rsp response structure to be
1685 * used to reference the exchange corresponding to the LS
1686 * when issuing an ls response.
1687 * @lsreqbuf: pointer to the buffer containing the LS Request
1688 * @lsreqbuf_len: length, in bytes, of the received LS request
1689 */
1690 int
nvme_fc_rcv_ls_req(struct nvme_fc_remote_port * portptr,struct nvmefc_ls_rsp * lsrsp,void * lsreqbuf,u32 lsreqbuf_len)1691 nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
1692 struct nvmefc_ls_rsp *lsrsp,
1693 void *lsreqbuf, u32 lsreqbuf_len)
1694 {
1695 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
1696 struct nvme_fc_lport *lport = rport->lport;
1697 struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf;
1698 struct nvmefc_ls_rcv_op *lsop;
1699 unsigned long flags;
1700 int ret;
1701
1702 nvme_fc_rport_get(rport);
1703
1704 /* validate there's a routine to transmit a response */
1705 if (!lport->ops->xmt_ls_rsp) {
1706 dev_info(lport->dev,
1707 "RCV %s LS failed: no LLDD xmt_ls_rsp\n",
1708 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1709 nvmefc_ls_names[w0->ls_cmd] : "");
1710 ret = -EINVAL;
1711 goto out_put;
1712 }
1713
1714 if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) {
1715 dev_info(lport->dev,
1716 "RCV %s LS failed: payload too large\n",
1717 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1718 nvmefc_ls_names[w0->ls_cmd] : "");
1719 ret = -E2BIG;
1720 goto out_put;
1721 }
1722
1723 lsop = kzalloc(sizeof(*lsop), GFP_KERNEL);
1724 if (!lsop) {
1725 nvme_fc_rcv_ls_req_err_msg(lport, w0);
1726 ret = -ENOMEM;
1727 goto out_put;
1728 }
1729
1730 lsop->rqstbuf = kzalloc(sizeof(*lsop->rqstbuf), GFP_KERNEL);
1731 lsop->rspbuf = kzalloc(sizeof(*lsop->rspbuf), GFP_KERNEL);
1732 if (!lsop->rqstbuf || !lsop->rspbuf) {
1733 nvme_fc_rcv_ls_req_err_msg(lport, w0);
1734 ret = -ENOMEM;
1735 goto out_free;
1736 }
1737
1738 lsop->rspdma = fc_dma_map_single(lport->dev, lsop->rspbuf,
1739 sizeof(*lsop->rspbuf),
1740 DMA_TO_DEVICE);
1741 if (fc_dma_mapping_error(lport->dev, lsop->rspdma)) {
1742 dev_info(lport->dev,
1743 "RCV %s LS failed: DMA mapping failure\n",
1744 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1745 nvmefc_ls_names[w0->ls_cmd] : "");
1746 ret = -EFAULT;
1747 goto out_free;
1748 }
1749
1750 lsop->rport = rport;
1751 lsop->lsrsp = lsrsp;
1752
1753 memcpy(lsop->rqstbuf, lsreqbuf, lsreqbuf_len);
1754 lsop->rqstdatalen = lsreqbuf_len;
1755
1756 spin_lock_irqsave(&rport->lock, flags);
1757 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) {
1758 spin_unlock_irqrestore(&rport->lock, flags);
1759 ret = -ENOTCONN;
1760 goto out_unmap;
1761 }
1762 list_add_tail(&lsop->lsrcv_list, &rport->ls_rcv_list);
1763 spin_unlock_irqrestore(&rport->lock, flags);
1764
1765 schedule_work(&rport->lsrcv_work);
1766
1767 return 0;
1768
1769 out_unmap:
1770 fc_dma_unmap_single(lport->dev, lsop->rspdma,
1771 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1772 out_free:
1773 kfree(lsop->rspbuf);
1774 kfree(lsop->rqstbuf);
1775 kfree(lsop);
1776 out_put:
1777 nvme_fc_rport_put(rport);
1778 return ret;
1779 }
1780 EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req);
1781
1782
1783 /* *********************** NVME Ctrl Routines **************************** */
1784
1785 static void
__nvme_fc_exit_request(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op)1786 __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1787 struct nvme_fc_fcp_op *op)
1788 {
1789 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1790 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1791 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1792 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1793
1794 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1795 }
1796
1797 static void
nvme_fc_exit_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx)1798 nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1799 unsigned int hctx_idx)
1800 {
1801 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1802
1803 return __nvme_fc_exit_request(to_fc_ctrl(set->driver_data), op);
1804 }
1805
1806 static int
__nvme_fc_abort_op(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op)1807 __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1808 {
1809 unsigned long flags;
1810 int opstate;
1811
1812 spin_lock_irqsave(&ctrl->lock, flags);
1813 opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1814 if (opstate != FCPOP_STATE_ACTIVE)
1815 atomic_set(&op->state, opstate);
1816 else if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) {
1817 op->flags |= FCOP_FLAGS_TERMIO;
1818 ctrl->iocnt++;
1819 }
1820 spin_unlock_irqrestore(&ctrl->lock, flags);
1821
1822 if (opstate != FCPOP_STATE_ACTIVE)
1823 return -ECANCELED;
1824
1825 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1826 &ctrl->rport->remoteport,
1827 op->queue->lldd_handle,
1828 &op->fcp_req);
1829
1830 return 0;
1831 }
1832
1833 static void
nvme_fc_abort_aen_ops(struct nvme_fc_ctrl * ctrl)1834 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1835 {
1836 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
1837 int i;
1838
1839 /* ensure we've initialized the ops once */
1840 if (!(aen_op->flags & FCOP_FLAGS_AEN))
1841 return;
1842
1843 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
1844 __nvme_fc_abort_op(ctrl, aen_op);
1845 }
1846
1847 static inline void
__nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op,int opstate)1848 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1849 struct nvme_fc_fcp_op *op, int opstate)
1850 {
1851 unsigned long flags;
1852
1853 if (opstate == FCPOP_STATE_ABORTED) {
1854 spin_lock_irqsave(&ctrl->lock, flags);
1855 if (test_bit(FCCTRL_TERMIO, &ctrl->flags) &&
1856 op->flags & FCOP_FLAGS_TERMIO) {
1857 if (!--ctrl->iocnt)
1858 wake_up(&ctrl->ioabort_wait);
1859 }
1860 spin_unlock_irqrestore(&ctrl->lock, flags);
1861 }
1862 }
1863
1864 static void
nvme_fc_ctrl_ioerr_work(struct work_struct * work)1865 nvme_fc_ctrl_ioerr_work(struct work_struct *work)
1866 {
1867 struct nvme_fc_ctrl *ctrl =
1868 container_of(work, struct nvme_fc_ctrl, ioerr_work);
1869
1870 nvme_fc_error_recovery(ctrl, "transport detected io error");
1871 }
1872
1873 /*
1874 * nvme_fc_io_getuuid - Routine called to get the appid field
1875 * associated with request by the lldd
1876 * @req:IO request from nvme fc to driver
1877 * Returns: UUID if there is an appid associated with VM or
1878 * NULL if the user/libvirt has not set the appid to VM
1879 */
nvme_fc_io_getuuid(struct nvmefc_fcp_req * req)1880 char *nvme_fc_io_getuuid(struct nvmefc_fcp_req *req)
1881 {
1882 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1883 struct request *rq = op->rq;
1884
1885 if (!IS_ENABLED(CONFIG_BLK_CGROUP_FC_APPID) || !rq || !rq->bio)
1886 return NULL;
1887 return blkcg_get_fc_appid(rq->bio);
1888 }
1889 EXPORT_SYMBOL_GPL(nvme_fc_io_getuuid);
1890
1891 static void
nvme_fc_fcpio_done(struct nvmefc_fcp_req * req)1892 nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1893 {
1894 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1895 struct request *rq = op->rq;
1896 struct nvmefc_fcp_req *freq = &op->fcp_req;
1897 struct nvme_fc_ctrl *ctrl = op->ctrl;
1898 struct nvme_fc_queue *queue = op->queue;
1899 struct nvme_completion *cqe = &op->rsp_iu.cqe;
1900 struct nvme_command *sqe = &op->cmd_iu.sqe;
1901 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
1902 union nvme_result result;
1903 bool terminate_assoc = true;
1904 int opstate;
1905
1906 /*
1907 * WARNING:
1908 * The current linux implementation of a nvme controller
1909 * allocates a single tag set for all io queues and sizes
1910 * the io queues to fully hold all possible tags. Thus, the
1911 * implementation does not reference or care about the sqhd
1912 * value as it never needs to use the sqhd/sqtail pointers
1913 * for submission pacing.
1914 *
1915 * This affects the FC-NVME implementation in two ways:
1916 * 1) As the value doesn't matter, we don't need to waste
1917 * cycles extracting it from ERSPs and stamping it in the
1918 * cases where the transport fabricates CQEs on successful
1919 * completions.
1920 * 2) The FC-NVME implementation requires that delivery of
1921 * ERSP completions are to go back to the nvme layer in order
1922 * relative to the rsn, such that the sqhd value will always
1923 * be "in order" for the nvme layer. As the nvme layer in
1924 * linux doesn't care about sqhd, there's no need to return
1925 * them in order.
1926 *
1927 * Additionally:
1928 * As the core nvme layer in linux currently does not look at
1929 * every field in the cqe - in cases where the FC transport must
1930 * fabricate a CQE, the following fields will not be set as they
1931 * are not referenced:
1932 * cqe.sqid, cqe.sqhd, cqe.command_id
1933 *
1934 * Failure or error of an individual i/o, in a transport
1935 * detected fashion unrelated to the nvme completion status,
1936 * potentially cause the initiator and target sides to get out
1937 * of sync on SQ head/tail (aka outstanding io count allowed).
1938 * Per FC-NVME spec, failure of an individual command requires
1939 * the connection to be terminated, which in turn requires the
1940 * association to be terminated.
1941 */
1942
1943 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
1944
1945 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1946 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1947
1948 if (opstate == FCPOP_STATE_ABORTED)
1949 status = cpu_to_le16(NVME_SC_HOST_ABORTED_CMD << 1);
1950 else if (freq->status) {
1951 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1952 dev_info(ctrl->ctrl.device,
1953 "NVME-FC{%d}: io failed due to lldd error %d\n",
1954 ctrl->cnum, freq->status);
1955 }
1956
1957 /*
1958 * For the linux implementation, if we have an unsucceesful
1959 * status, they blk-mq layer can typically be called with the
1960 * non-zero status and the content of the cqe isn't important.
1961 */
1962 if (status)
1963 goto done;
1964
1965 /*
1966 * command completed successfully relative to the wire
1967 * protocol. However, validate anything received and
1968 * extract the status and result from the cqe (create it
1969 * where necessary).
1970 */
1971
1972 switch (freq->rcv_rsplen) {
1973
1974 case 0:
1975 case NVME_FC_SIZEOF_ZEROS_RSP:
1976 /*
1977 * No response payload or 12 bytes of payload (which
1978 * should all be zeros) are considered successful and
1979 * no payload in the CQE by the transport.
1980 */
1981 if (freq->transferred_length !=
1982 be32_to_cpu(op->cmd_iu.data_len)) {
1983 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1984 dev_info(ctrl->ctrl.device,
1985 "NVME-FC{%d}: io failed due to bad transfer "
1986 "length: %d vs expected %d\n",
1987 ctrl->cnum, freq->transferred_length,
1988 be32_to_cpu(op->cmd_iu.data_len));
1989 goto done;
1990 }
1991 result.u64 = 0;
1992 break;
1993
1994 case sizeof(struct nvme_fc_ersp_iu):
1995 /*
1996 * The ERSP IU contains a full completion with CQE.
1997 * Validate ERSP IU and look at cqe.
1998 */
1999 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
2000 (freq->rcv_rsplen / 4) ||
2001 be32_to_cpu(op->rsp_iu.xfrd_len) !=
2002 freq->transferred_length ||
2003 op->rsp_iu.ersp_result ||
2004 sqe->common.command_id != cqe->command_id)) {
2005 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2006 dev_info(ctrl->ctrl.device,
2007 "NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
2008 "iu len %d, xfr len %d vs %d, status code "
2009 "%d, cmdid %d vs %d\n",
2010 ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len),
2011 be32_to_cpu(op->rsp_iu.xfrd_len),
2012 freq->transferred_length,
2013 op->rsp_iu.ersp_result,
2014 sqe->common.command_id,
2015 cqe->command_id);
2016 goto done;
2017 }
2018 result = cqe->result;
2019 status = cqe->status;
2020 break;
2021
2022 default:
2023 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2024 dev_info(ctrl->ctrl.device,
2025 "NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
2026 "len %d\n",
2027 ctrl->cnum, freq->rcv_rsplen);
2028 goto done;
2029 }
2030
2031 terminate_assoc = false;
2032
2033 done:
2034 if (op->flags & FCOP_FLAGS_AEN) {
2035 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
2036 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2037 atomic_set(&op->state, FCPOP_STATE_IDLE);
2038 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
2039 nvme_fc_ctrl_put(ctrl);
2040 goto check_error;
2041 }
2042
2043 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2044 if (!nvme_try_complete_req(rq, status, result))
2045 nvme_fc_complete_rq(rq);
2046
2047 check_error:
2048 if (terminate_assoc &&
2049 nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_RESETTING)
2050 queue_work(nvme_reset_wq, &ctrl->ioerr_work);
2051 }
2052
2053 static int
__nvme_fc_init_request(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,struct nvme_fc_fcp_op * op,struct request * rq,u32 rqno)2054 __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
2055 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
2056 struct request *rq, u32 rqno)
2057 {
2058 struct nvme_fcp_op_w_sgl *op_w_sgl =
2059 container_of(op, typeof(*op_w_sgl), op);
2060 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2061 int ret = 0;
2062
2063 memset(op, 0, sizeof(*op));
2064 op->fcp_req.cmdaddr = &op->cmd_iu;
2065 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
2066 op->fcp_req.rspaddr = &op->rsp_iu;
2067 op->fcp_req.rsplen = sizeof(op->rsp_iu);
2068 op->fcp_req.done = nvme_fc_fcpio_done;
2069 op->ctrl = ctrl;
2070 op->queue = queue;
2071 op->rq = rq;
2072 op->rqno = rqno;
2073
2074 cmdiu->format_id = NVME_CMD_FORMAT_ID;
2075 cmdiu->fc_id = NVME_CMD_FC_ID;
2076 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
2077 if (queue->qnum)
2078 cmdiu->rsv_cat = fccmnd_set_cat_css(0,
2079 (NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT));
2080 else
2081 cmdiu->rsv_cat = fccmnd_set_cat_admin(0);
2082
2083 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
2084 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
2085 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
2086 dev_err(ctrl->dev,
2087 "FCP Op failed - cmdiu dma mapping failed.\n");
2088 ret = -EFAULT;
2089 goto out_on_error;
2090 }
2091
2092 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
2093 &op->rsp_iu, sizeof(op->rsp_iu),
2094 DMA_FROM_DEVICE);
2095 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
2096 dev_err(ctrl->dev,
2097 "FCP Op failed - rspiu dma mapping failed.\n");
2098 ret = -EFAULT;
2099 }
2100
2101 atomic_set(&op->state, FCPOP_STATE_IDLE);
2102 out_on_error:
2103 return ret;
2104 }
2105
2106 static int
nvme_fc_init_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx,unsigned int numa_node)2107 nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
2108 unsigned int hctx_idx, unsigned int numa_node)
2109 {
2110 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
2111 struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq);
2112 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
2113 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
2114 int res;
2115
2116 res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++);
2117 if (res)
2118 return res;
2119 op->op.fcp_req.first_sgl = op->sgl;
2120 op->op.fcp_req.private = &op->priv[0];
2121 nvme_req(rq)->ctrl = &ctrl->ctrl;
2122 nvme_req(rq)->cmd = &op->op.cmd_iu.sqe;
2123 return res;
2124 }
2125
2126 static int
nvme_fc_init_aen_ops(struct nvme_fc_ctrl * ctrl)2127 nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
2128 {
2129 struct nvme_fc_fcp_op *aen_op;
2130 struct nvme_fc_cmd_iu *cmdiu;
2131 struct nvme_command *sqe;
2132 void *private = NULL;
2133 int i, ret;
2134
2135 aen_op = ctrl->aen_ops;
2136 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2137 if (ctrl->lport->ops->fcprqst_priv_sz) {
2138 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
2139 GFP_KERNEL);
2140 if (!private)
2141 return -ENOMEM;
2142 }
2143
2144 cmdiu = &aen_op->cmd_iu;
2145 sqe = &cmdiu->sqe;
2146 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
2147 aen_op, (struct request *)NULL,
2148 (NVME_AQ_BLK_MQ_DEPTH + i));
2149 if (ret) {
2150 kfree(private);
2151 return ret;
2152 }
2153
2154 aen_op->flags = FCOP_FLAGS_AEN;
2155 aen_op->fcp_req.private = private;
2156
2157 memset(sqe, 0, sizeof(*sqe));
2158 sqe->common.opcode = nvme_admin_async_event;
2159 /* Note: core layer may overwrite the sqe.command_id value */
2160 sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
2161 }
2162 return 0;
2163 }
2164
2165 static void
nvme_fc_term_aen_ops(struct nvme_fc_ctrl * ctrl)2166 nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
2167 {
2168 struct nvme_fc_fcp_op *aen_op;
2169 int i;
2170
2171 cancel_work_sync(&ctrl->ctrl.async_event_work);
2172 aen_op = ctrl->aen_ops;
2173 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2174 __nvme_fc_exit_request(ctrl, aen_op);
2175
2176 kfree(aen_op->fcp_req.private);
2177 aen_op->fcp_req.private = NULL;
2178 }
2179 }
2180
2181 static inline int
__nvme_fc_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int qidx)2182 __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int qidx)
2183 {
2184 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(data);
2185 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
2186
2187 hctx->driver_data = queue;
2188 queue->hctx = hctx;
2189 return 0;
2190 }
2191
2192 static int
nvme_fc_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)2193 nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int hctx_idx)
2194 {
2195 return __nvme_fc_init_hctx(hctx, data, hctx_idx + 1);
2196 }
2197
2198 static int
nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)2199 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2200 unsigned int hctx_idx)
2201 {
2202 return __nvme_fc_init_hctx(hctx, data, hctx_idx);
2203 }
2204
2205 static void
nvme_fc_init_queue(struct nvme_fc_ctrl * ctrl,int idx)2206 nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
2207 {
2208 struct nvme_fc_queue *queue;
2209
2210 queue = &ctrl->queues[idx];
2211 memset(queue, 0, sizeof(*queue));
2212 queue->ctrl = ctrl;
2213 queue->qnum = idx;
2214 atomic_set(&queue->csn, 0);
2215 queue->dev = ctrl->dev;
2216
2217 if (idx > 0)
2218 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
2219 else
2220 queue->cmnd_capsule_len = sizeof(struct nvme_command);
2221
2222 /*
2223 * Considered whether we should allocate buffers for all SQEs
2224 * and CQEs and dma map them - mapping their respective entries
2225 * into the request structures (kernel vm addr and dma address)
2226 * thus the driver could use the buffers/mappings directly.
2227 * It only makes sense if the LLDD would use them for its
2228 * messaging api. It's very unlikely most adapter api's would use
2229 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
2230 * structures were used instead.
2231 */
2232 }
2233
2234 /*
2235 * This routine terminates a queue at the transport level.
2236 * The transport has already ensured that all outstanding ios on
2237 * the queue have been terminated.
2238 * The transport will send a Disconnect LS request to terminate
2239 * the queue's connection. Termination of the admin queue will also
2240 * terminate the association at the target.
2241 */
2242 static void
nvme_fc_free_queue(struct nvme_fc_queue * queue)2243 nvme_fc_free_queue(struct nvme_fc_queue *queue)
2244 {
2245 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
2246 return;
2247
2248 clear_bit(NVME_FC_Q_LIVE, &queue->flags);
2249 /*
2250 * Current implementation never disconnects a single queue.
2251 * It always terminates a whole association. So there is never
2252 * a disconnect(queue) LS sent to the target.
2253 */
2254
2255 queue->connection_id = 0;
2256 atomic_set(&queue->csn, 0);
2257 }
2258
2259 static void
__nvme_fc_delete_hw_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,unsigned int qidx)2260 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
2261 struct nvme_fc_queue *queue, unsigned int qidx)
2262 {
2263 if (ctrl->lport->ops->delete_queue)
2264 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
2265 queue->lldd_handle);
2266 queue->lldd_handle = NULL;
2267 }
2268
2269 static void
nvme_fc_free_io_queues(struct nvme_fc_ctrl * ctrl)2270 nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
2271 {
2272 int i;
2273
2274 for (i = 1; i < ctrl->ctrl.queue_count; i++)
2275 nvme_fc_free_queue(&ctrl->queues[i]);
2276 }
2277
2278 static int
__nvme_fc_create_hw_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,unsigned int qidx,u16 qsize)2279 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
2280 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
2281 {
2282 int ret = 0;
2283
2284 queue->lldd_handle = NULL;
2285 if (ctrl->lport->ops->create_queue)
2286 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
2287 qidx, qsize, &queue->lldd_handle);
2288
2289 return ret;
2290 }
2291
2292 static void
nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl * ctrl)2293 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
2294 {
2295 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
2296 int i;
2297
2298 for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
2299 __nvme_fc_delete_hw_queue(ctrl, queue, i);
2300 }
2301
2302 static int
nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl * ctrl,u16 qsize)2303 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2304 {
2305 struct nvme_fc_queue *queue = &ctrl->queues[1];
2306 int i, ret;
2307
2308 for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
2309 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
2310 if (ret)
2311 goto delete_queues;
2312 }
2313
2314 return 0;
2315
2316 delete_queues:
2317 for (; i > 0; i--)
2318 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
2319 return ret;
2320 }
2321
2322 static int
nvme_fc_connect_io_queues(struct nvme_fc_ctrl * ctrl,u16 qsize)2323 nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2324 {
2325 int i, ret = 0;
2326
2327 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
2328 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
2329 (qsize / 5));
2330 if (ret)
2331 break;
2332 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
2333 if (ret)
2334 break;
2335
2336 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
2337 }
2338
2339 return ret;
2340 }
2341
2342 static void
nvme_fc_init_io_queues(struct nvme_fc_ctrl * ctrl)2343 nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
2344 {
2345 int i;
2346
2347 for (i = 1; i < ctrl->ctrl.queue_count; i++)
2348 nvme_fc_init_queue(ctrl, i);
2349 }
2350
2351 static void
nvme_fc_ctrl_free(struct kref * ref)2352 nvme_fc_ctrl_free(struct kref *ref)
2353 {
2354 struct nvme_fc_ctrl *ctrl =
2355 container_of(ref, struct nvme_fc_ctrl, ref);
2356 unsigned long flags;
2357
2358 if (ctrl->ctrl.tagset)
2359 nvme_remove_io_tag_set(&ctrl->ctrl);
2360
2361 /* remove from rport list */
2362 spin_lock_irqsave(&ctrl->rport->lock, flags);
2363 list_del(&ctrl->ctrl_list);
2364 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
2365
2366 nvme_unquiesce_admin_queue(&ctrl->ctrl);
2367 nvme_remove_admin_tag_set(&ctrl->ctrl);
2368
2369 kfree(ctrl->queues);
2370
2371 put_device(ctrl->dev);
2372 nvme_fc_rport_put(ctrl->rport);
2373
2374 ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
2375 if (ctrl->ctrl.opts)
2376 nvmf_free_options(ctrl->ctrl.opts);
2377 kfree(ctrl);
2378 }
2379
2380 static void
nvme_fc_ctrl_put(struct nvme_fc_ctrl * ctrl)2381 nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2382 {
2383 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2384 }
2385
2386 static int
nvme_fc_ctrl_get(struct nvme_fc_ctrl * ctrl)2387 nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2388 {
2389 return kref_get_unless_zero(&ctrl->ref);
2390 }
2391
2392 /*
2393 * All accesses from nvme core layer done - can now free the
2394 * controller. Called after last nvme_put_ctrl() call
2395 */
2396 static void
nvme_fc_free_ctrl(struct nvme_ctrl * nctrl)2397 nvme_fc_free_ctrl(struct nvme_ctrl *nctrl)
2398 {
2399 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2400
2401 WARN_ON(nctrl != &ctrl->ctrl);
2402
2403 nvme_fc_ctrl_put(ctrl);
2404 }
2405
2406 /*
2407 * This routine is used by the transport when it needs to find active
2408 * io on a queue that is to be terminated. The transport uses
2409 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2410 * this routine to kill them on a 1 by 1 basis.
2411 *
2412 * As FC allocates FC exchange for each io, the transport must contact
2413 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2414 * After terminating the exchange the LLDD will call the transport's
2415 * normal io done path for the request, but it will have an aborted
2416 * status. The done path will return the io request back to the block
2417 * layer with an error status.
2418 */
nvme_fc_terminate_exchange(struct request * req,void * data)2419 static bool nvme_fc_terminate_exchange(struct request *req, void *data)
2420 {
2421 struct nvme_ctrl *nctrl = data;
2422 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2423 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2424
2425 op->nreq.flags |= NVME_REQ_CANCELLED;
2426 __nvme_fc_abort_op(ctrl, op);
2427 return true;
2428 }
2429
2430 /*
2431 * This routine runs through all outstanding commands on the association
2432 * and aborts them. This routine is typically be called by the
2433 * delete_association routine. It is also called due to an error during
2434 * reconnect. In that scenario, it is most likely a command that initializes
2435 * the controller, including fabric Connect commands on io queues, that
2436 * may have timed out or failed thus the io must be killed for the connect
2437 * thread to see the error.
2438 */
2439 static void
__nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl * ctrl,bool start_queues)2440 __nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl *ctrl, bool start_queues)
2441 {
2442 int q;
2443
2444 /*
2445 * if aborting io, the queues are no longer good, mark them
2446 * all as not live.
2447 */
2448 if (ctrl->ctrl.queue_count > 1) {
2449 for (q = 1; q < ctrl->ctrl.queue_count; q++)
2450 clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[q].flags);
2451 }
2452 clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
2453
2454 /*
2455 * If io queues are present, stop them and terminate all outstanding
2456 * ios on them. As FC allocates FC exchange for each io, the
2457 * transport must contact the LLDD to terminate the exchange,
2458 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2459 * to tell us what io's are busy and invoke a transport routine
2460 * to kill them with the LLDD. After terminating the exchange
2461 * the LLDD will call the transport's normal io done path, but it
2462 * will have an aborted status. The done path will return the
2463 * io requests back to the block layer as part of normal completions
2464 * (but with error status).
2465 */
2466 if (ctrl->ctrl.queue_count > 1) {
2467 nvme_quiesce_io_queues(&ctrl->ctrl);
2468 nvme_sync_io_queues(&ctrl->ctrl);
2469 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2470 nvme_fc_terminate_exchange, &ctrl->ctrl);
2471 blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
2472 if (start_queues)
2473 nvme_unquiesce_io_queues(&ctrl->ctrl);
2474 }
2475
2476 /*
2477 * Other transports, which don't have link-level contexts bound
2478 * to sqe's, would try to gracefully shutdown the controller by
2479 * writing the registers for shutdown and polling (call
2480 * nvme_disable_ctrl()). Given a bunch of i/o was potentially
2481 * just aborted and we will wait on those contexts, and given
2482 * there was no indication of how live the controller is on the
2483 * link, don't send more io to create more contexts for the
2484 * shutdown. Let the controller fail via keepalive failure if
2485 * its still present.
2486 */
2487
2488 /*
2489 * clean up the admin queue. Same thing as above.
2490 */
2491 nvme_quiesce_admin_queue(&ctrl->ctrl);
2492 blk_sync_queue(ctrl->ctrl.admin_q);
2493 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2494 nvme_fc_terminate_exchange, &ctrl->ctrl);
2495 blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
2496 if (start_queues)
2497 nvme_unquiesce_admin_queue(&ctrl->ctrl);
2498 }
2499
2500 static void
nvme_fc_error_recovery(struct nvme_fc_ctrl * ctrl,char * errmsg)2501 nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
2502 {
2503 enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl);
2504
2505 /*
2506 * if an error (io timeout, etc) while (re)connecting, the remote
2507 * port requested terminating of the association (disconnect_ls)
2508 * or an error (timeout or abort) occurred on an io while creating
2509 * the controller. Abort any ios on the association and let the
2510 * create_association error path resolve things.
2511 */
2512 if (state == NVME_CTRL_CONNECTING) {
2513 __nvme_fc_abort_outstanding_ios(ctrl, true);
2514 dev_warn(ctrl->ctrl.device,
2515 "NVME-FC{%d}: transport error during (re)connect\n",
2516 ctrl->cnum);
2517 return;
2518 }
2519
2520 /* Otherwise, only proceed if in LIVE state - e.g. on first error */
2521 if (state != NVME_CTRL_LIVE)
2522 return;
2523
2524 dev_warn(ctrl->ctrl.device,
2525 "NVME-FC{%d}: transport association event: %s\n",
2526 ctrl->cnum, errmsg);
2527 dev_warn(ctrl->ctrl.device,
2528 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
2529
2530 nvme_reset_ctrl(&ctrl->ctrl);
2531 }
2532
nvme_fc_timeout(struct request * rq)2533 static enum blk_eh_timer_return nvme_fc_timeout(struct request *rq)
2534 {
2535 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2536 struct nvme_fc_ctrl *ctrl = op->ctrl;
2537 u16 qnum = op->queue->qnum;
2538 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2539 struct nvme_command *sqe = &cmdiu->sqe;
2540
2541 /*
2542 * Attempt to abort the offending command. Command completion
2543 * will detect the aborted io and will fail the connection.
2544 */
2545 dev_info(ctrl->ctrl.device,
2546 "NVME-FC{%d.%d}: io timeout: opcode %d fctype %d (%s) w10/11: "
2547 "x%08x/x%08x\n",
2548 ctrl->cnum, qnum, sqe->common.opcode, sqe->fabrics.fctype,
2549 nvme_fabrics_opcode_str(qnum, sqe),
2550 sqe->common.cdw10, sqe->common.cdw11);
2551 if (__nvme_fc_abort_op(ctrl, op))
2552 nvme_fc_error_recovery(ctrl, "io timeout abort failed");
2553
2554 /*
2555 * the io abort has been initiated. Have the reset timer
2556 * restarted and the abort completion will complete the io
2557 * shortly. Avoids a synchronous wait while the abort finishes.
2558 */
2559 return BLK_EH_RESET_TIMER;
2560 }
2561
2562 static int
nvme_fc_map_data(struct nvme_fc_ctrl * ctrl,struct request * rq,struct nvme_fc_fcp_op * op)2563 nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2564 struct nvme_fc_fcp_op *op)
2565 {
2566 struct nvmefc_fcp_req *freq = &op->fcp_req;
2567 int ret;
2568
2569 freq->sg_cnt = 0;
2570
2571 if (!blk_rq_nr_phys_segments(rq))
2572 return 0;
2573
2574 freq->sg_table.sgl = freq->first_sgl;
2575 ret = sg_alloc_table_chained(&freq->sg_table,
2576 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
2577 NVME_INLINE_SG_CNT);
2578 if (ret)
2579 return -ENOMEM;
2580
2581 op->nents = blk_rq_map_sg(rq, freq->sg_table.sgl);
2582 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2583 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2584 op->nents, rq_dma_dir(rq));
2585 if (unlikely(freq->sg_cnt <= 0)) {
2586 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2587 freq->sg_cnt = 0;
2588 return -EFAULT;
2589 }
2590
2591 /*
2592 * TODO: blk_integrity_rq(rq) for DIF
2593 */
2594 return 0;
2595 }
2596
2597 static void
nvme_fc_unmap_data(struct nvme_fc_ctrl * ctrl,struct request * rq,struct nvme_fc_fcp_op * op)2598 nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2599 struct nvme_fc_fcp_op *op)
2600 {
2601 struct nvmefc_fcp_req *freq = &op->fcp_req;
2602
2603 if (!freq->sg_cnt)
2604 return;
2605
2606 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2607 rq_dma_dir(rq));
2608
2609 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2610
2611 freq->sg_cnt = 0;
2612 }
2613
2614 /*
2615 * In FC, the queue is a logical thing. At transport connect, the target
2616 * creates its "queue" and returns a handle that is to be given to the
2617 * target whenever it posts something to the corresponding SQ. When an
2618 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2619 * command contained within the SQE, an io, and assigns a FC exchange
2620 * to it. The SQE and the associated SQ handle are sent in the initial
2621 * CMD IU sents on the exchange. All transfers relative to the io occur
2622 * as part of the exchange. The CQE is the last thing for the io,
2623 * which is transferred (explicitly or implicitly) with the RSP IU
2624 * sent on the exchange. After the CQE is received, the FC exchange is
2625 * terminaed and the Exchange may be used on a different io.
2626 *
2627 * The transport to LLDD api has the transport making a request for a
2628 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2629 * resource and transfers the command. The LLDD will then process all
2630 * steps to complete the io. Upon completion, the transport done routine
2631 * is called.
2632 *
2633 * So - while the operation is outstanding to the LLDD, there is a link
2634 * level FC exchange resource that is also outstanding. This must be
2635 * considered in all cleanup operations.
2636 */
2637 static blk_status_t
nvme_fc_start_fcp_op(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,struct nvme_fc_fcp_op * op,u32 data_len,enum nvmefc_fcp_datadir io_dir)2638 nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2639 struct nvme_fc_fcp_op *op, u32 data_len,
2640 enum nvmefc_fcp_datadir io_dir)
2641 {
2642 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2643 struct nvme_command *sqe = &cmdiu->sqe;
2644 int ret, opstate;
2645
2646 /*
2647 * before attempting to send the io, check to see if we believe
2648 * the target device is present
2649 */
2650 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2651 return BLK_STS_RESOURCE;
2652
2653 if (!nvme_fc_ctrl_get(ctrl))
2654 return BLK_STS_IOERR;
2655
2656 /* format the FC-NVME CMD IU and fcp_req */
2657 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2658 cmdiu->data_len = cpu_to_be32(data_len);
2659 switch (io_dir) {
2660 case NVMEFC_FCP_WRITE:
2661 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2662 break;
2663 case NVMEFC_FCP_READ:
2664 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2665 break;
2666 case NVMEFC_FCP_NODATA:
2667 cmdiu->flags = 0;
2668 break;
2669 }
2670 op->fcp_req.payload_length = data_len;
2671 op->fcp_req.io_dir = io_dir;
2672 op->fcp_req.transferred_length = 0;
2673 op->fcp_req.rcv_rsplen = 0;
2674 op->fcp_req.status = NVME_SC_SUCCESS;
2675 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2676
2677 /*
2678 * validate per fabric rules, set fields mandated by fabric spec
2679 * as well as those by FC-NVME spec.
2680 */
2681 WARN_ON_ONCE(sqe->common.metadata);
2682 sqe->common.flags |= NVME_CMD_SGL_METABUF;
2683
2684 /*
2685 * format SQE DPTR field per FC-NVME rules:
2686 * type=0x5 Transport SGL Data Block Descriptor
2687 * subtype=0xA Transport-specific value
2688 * address=0
2689 * length=length of the data series
2690 */
2691 sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2692 NVME_SGL_FMT_TRANSPORT_A;
2693 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2694 sqe->rw.dptr.sgl.addr = 0;
2695
2696 if (!(op->flags & FCOP_FLAGS_AEN)) {
2697 ret = nvme_fc_map_data(ctrl, op->rq, op);
2698 if (ret < 0) {
2699 nvme_cleanup_cmd(op->rq);
2700 nvme_fc_ctrl_put(ctrl);
2701 if (ret == -ENOMEM || ret == -EAGAIN)
2702 return BLK_STS_RESOURCE;
2703 return BLK_STS_IOERR;
2704 }
2705 }
2706
2707 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2708 sizeof(op->cmd_iu), DMA_TO_DEVICE);
2709
2710 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2711
2712 if (!(op->flags & FCOP_FLAGS_AEN))
2713 nvme_start_request(op->rq);
2714
2715 cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2716 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2717 &ctrl->rport->remoteport,
2718 queue->lldd_handle, &op->fcp_req);
2719
2720 if (ret) {
2721 /*
2722 * If the lld fails to send the command is there an issue with
2723 * the csn value? If the command that fails is the Connect,
2724 * no - as the connection won't be live. If it is a command
2725 * post-connect, it's possible a gap in csn may be created.
2726 * Does this matter? As Linux initiators don't send fused
2727 * commands, no. The gap would exist, but as there's nothing
2728 * that depends on csn order to be delivered on the target
2729 * side, it shouldn't hurt. It would be difficult for a
2730 * target to even detect the csn gap as it has no idea when the
2731 * cmd with the csn was supposed to arrive.
2732 */
2733 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2734 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2735
2736 if (!(op->flags & FCOP_FLAGS_AEN)) {
2737 nvme_fc_unmap_data(ctrl, op->rq, op);
2738 nvme_cleanup_cmd(op->rq);
2739 }
2740
2741 nvme_fc_ctrl_put(ctrl);
2742
2743 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2744 ret != -EBUSY)
2745 return BLK_STS_IOERR;
2746
2747 return BLK_STS_RESOURCE;
2748 }
2749
2750 return BLK_STS_OK;
2751 }
2752
2753 static blk_status_t
nvme_fc_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)2754 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2755 const struct blk_mq_queue_data *bd)
2756 {
2757 struct nvme_ns *ns = hctx->queue->queuedata;
2758 struct nvme_fc_queue *queue = hctx->driver_data;
2759 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2760 struct request *rq = bd->rq;
2761 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2762 enum nvmefc_fcp_datadir io_dir;
2763 bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2764 u32 data_len;
2765 blk_status_t ret;
2766
2767 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
2768 !nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2769 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
2770
2771 ret = nvme_setup_cmd(ns, rq);
2772 if (ret)
2773 return ret;
2774
2775 /*
2776 * nvme core doesn't quite treat the rq opaquely. Commands such
2777 * as WRITE ZEROES will return a non-zero rq payload_bytes yet
2778 * there is no actual payload to be transferred.
2779 * To get it right, key data transmission on there being 1 or
2780 * more physical segments in the sg list. If there is no
2781 * physical segments, there is no payload.
2782 */
2783 if (blk_rq_nr_phys_segments(rq)) {
2784 data_len = blk_rq_payload_bytes(rq);
2785 io_dir = ((rq_data_dir(rq) == WRITE) ?
2786 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2787 } else {
2788 data_len = 0;
2789 io_dir = NVMEFC_FCP_NODATA;
2790 }
2791
2792
2793 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2794 }
2795
2796 static void
nvme_fc_submit_async_event(struct nvme_ctrl * arg)2797 nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2798 {
2799 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2800 struct nvme_fc_fcp_op *aen_op;
2801 blk_status_t ret;
2802
2803 if (test_bit(FCCTRL_TERMIO, &ctrl->flags))
2804 return;
2805
2806 aen_op = &ctrl->aen_ops[0];
2807
2808 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2809 NVMEFC_FCP_NODATA);
2810 if (ret)
2811 dev_err(ctrl->ctrl.device,
2812 "failed async event work\n");
2813 }
2814
2815 static void
nvme_fc_complete_rq(struct request * rq)2816 nvme_fc_complete_rq(struct request *rq)
2817 {
2818 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2819 struct nvme_fc_ctrl *ctrl = op->ctrl;
2820
2821 atomic_set(&op->state, FCPOP_STATE_IDLE);
2822 op->flags &= ~FCOP_FLAGS_TERMIO;
2823
2824 nvme_fc_unmap_data(ctrl, rq, op);
2825 nvme_complete_rq(rq);
2826 nvme_fc_ctrl_put(ctrl);
2827 }
2828
nvme_fc_map_queues(struct blk_mq_tag_set * set)2829 static void nvme_fc_map_queues(struct blk_mq_tag_set *set)
2830 {
2831 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
2832 int i;
2833
2834 for (i = 0; i < set->nr_maps; i++) {
2835 struct blk_mq_queue_map *map = &set->map[i];
2836
2837 if (!map->nr_queues) {
2838 WARN_ON(i == HCTX_TYPE_DEFAULT);
2839 continue;
2840 }
2841
2842 /* Call LLDD map queue functionality if defined */
2843 if (ctrl->lport->ops->map_queues)
2844 ctrl->lport->ops->map_queues(&ctrl->lport->localport,
2845 map);
2846 else
2847 blk_mq_map_queues(map);
2848 }
2849 }
2850
2851 static const struct blk_mq_ops nvme_fc_mq_ops = {
2852 .queue_rq = nvme_fc_queue_rq,
2853 .complete = nvme_fc_complete_rq,
2854 .init_request = nvme_fc_init_request,
2855 .exit_request = nvme_fc_exit_request,
2856 .init_hctx = nvme_fc_init_hctx,
2857 .timeout = nvme_fc_timeout,
2858 .map_queues = nvme_fc_map_queues,
2859 };
2860
2861 static int
nvme_fc_create_io_queues(struct nvme_fc_ctrl * ctrl)2862 nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2863 {
2864 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2865 unsigned int nr_io_queues;
2866 int ret;
2867
2868 nr_io_queues = min3(opts->nr_io_queues, num_online_cpus(),
2869 ctrl->lport->ops->max_hw_queues);
2870 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2871 if (ret) {
2872 dev_info(ctrl->ctrl.device,
2873 "set_queue_count failed: %d\n", ret);
2874 return ret;
2875 }
2876
2877 ctrl->ctrl.queue_count = nr_io_queues + 1;
2878 if (!nr_io_queues)
2879 return 0;
2880
2881 nvme_fc_init_io_queues(ctrl);
2882
2883 ret = nvme_alloc_io_tag_set(&ctrl->ctrl, &ctrl->tag_set,
2884 &nvme_fc_mq_ops, 1,
2885 struct_size_t(struct nvme_fcp_op_w_sgl, priv,
2886 ctrl->lport->ops->fcprqst_priv_sz));
2887 if (ret)
2888 return ret;
2889
2890 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2891 if (ret)
2892 goto out_cleanup_tagset;
2893
2894 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2895 if (ret)
2896 goto out_delete_hw_queues;
2897
2898 ctrl->ioq_live = true;
2899
2900 return 0;
2901
2902 out_delete_hw_queues:
2903 nvme_fc_delete_hw_io_queues(ctrl);
2904 out_cleanup_tagset:
2905 nvme_remove_io_tag_set(&ctrl->ctrl);
2906 nvme_fc_free_io_queues(ctrl);
2907
2908 /* force put free routine to ignore io queues */
2909 ctrl->ctrl.tagset = NULL;
2910
2911 return ret;
2912 }
2913
2914 static int
nvme_fc_recreate_io_queues(struct nvme_fc_ctrl * ctrl)2915 nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
2916 {
2917 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2918 u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
2919 unsigned int nr_io_queues;
2920 int ret;
2921
2922 nr_io_queues = min3(opts->nr_io_queues, num_online_cpus(),
2923 ctrl->lport->ops->max_hw_queues);
2924 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2925 if (ret) {
2926 dev_info(ctrl->ctrl.device,
2927 "set_queue_count failed: %d\n", ret);
2928 return ret;
2929 }
2930
2931 if (!nr_io_queues && prior_ioq_cnt) {
2932 dev_info(ctrl->ctrl.device,
2933 "Fail Reconnect: At least 1 io queue "
2934 "required (was %d)\n", prior_ioq_cnt);
2935 return -ENOSPC;
2936 }
2937
2938 ctrl->ctrl.queue_count = nr_io_queues + 1;
2939 /* check for io queues existing */
2940 if (ctrl->ctrl.queue_count == 1)
2941 return 0;
2942
2943 if (prior_ioq_cnt != nr_io_queues) {
2944 dev_info(ctrl->ctrl.device,
2945 "reconnect: revising io queue count from %d to %d\n",
2946 prior_ioq_cnt, nr_io_queues);
2947 blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2948 }
2949
2950 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2951 if (ret)
2952 goto out_free_io_queues;
2953
2954 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2955 if (ret)
2956 goto out_delete_hw_queues;
2957
2958 return 0;
2959
2960 out_delete_hw_queues:
2961 nvme_fc_delete_hw_io_queues(ctrl);
2962 out_free_io_queues:
2963 nvme_fc_free_io_queues(ctrl);
2964 return ret;
2965 }
2966
2967 static void
nvme_fc_rport_active_on_lport(struct nvme_fc_rport * rport)2968 nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
2969 {
2970 struct nvme_fc_lport *lport = rport->lport;
2971
2972 atomic_inc(&lport->act_rport_cnt);
2973 }
2974
2975 static void
nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport * rport)2976 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
2977 {
2978 struct nvme_fc_lport *lport = rport->lport;
2979 u32 cnt;
2980
2981 cnt = atomic_dec_return(&lport->act_rport_cnt);
2982 if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
2983 lport->ops->localport_delete(&lport->localport);
2984 }
2985
2986 static int
nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl * ctrl)2987 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
2988 {
2989 struct nvme_fc_rport *rport = ctrl->rport;
2990 u32 cnt;
2991
2992 if (test_and_set_bit(ASSOC_ACTIVE, &ctrl->flags))
2993 return 1;
2994
2995 cnt = atomic_inc_return(&rport->act_ctrl_cnt);
2996 if (cnt == 1)
2997 nvme_fc_rport_active_on_lport(rport);
2998
2999 return 0;
3000 }
3001
3002 static int
nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl * ctrl)3003 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
3004 {
3005 struct nvme_fc_rport *rport = ctrl->rport;
3006 struct nvme_fc_lport *lport = rport->lport;
3007 u32 cnt;
3008
3009 /* clearing of ctrl->flags ASSOC_ACTIVE bit is in association delete */
3010
3011 cnt = atomic_dec_return(&rport->act_ctrl_cnt);
3012 if (cnt == 0) {
3013 if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
3014 lport->ops->remoteport_delete(&rport->remoteport);
3015 nvme_fc_rport_inactive_on_lport(rport);
3016 }
3017
3018 return 0;
3019 }
3020
3021 /*
3022 * This routine restarts the controller on the host side, and
3023 * on the link side, recreates the controller association.
3024 */
3025 static int
nvme_fc_create_association(struct nvme_fc_ctrl * ctrl)3026 nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
3027 {
3028 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
3029 struct nvmefc_ls_rcv_op *disls = NULL;
3030 unsigned long flags;
3031 int ret;
3032
3033 ++ctrl->ctrl.nr_reconnects;
3034
3035 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3036 return -ENODEV;
3037
3038 if (nvme_fc_ctlr_active_on_rport(ctrl))
3039 return -ENOTUNIQ;
3040
3041 dev_info(ctrl->ctrl.device,
3042 "NVME-FC{%d}: create association : host wwpn 0x%016llx "
3043 " rport wwpn 0x%016llx: NQN \"%s\"\n",
3044 ctrl->cnum, ctrl->lport->localport.port_name,
3045 ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
3046
3047 clear_bit(ASSOC_FAILED, &ctrl->flags);
3048
3049 /*
3050 * Create the admin queue
3051 */
3052
3053 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
3054 NVME_AQ_DEPTH);
3055 if (ret)
3056 goto out_free_queue;
3057
3058 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
3059 NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
3060 if (ret)
3061 goto out_delete_hw_queue;
3062
3063 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
3064 if (ret)
3065 goto out_disconnect_admin_queue;
3066
3067 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
3068
3069 /*
3070 * Check controller capabilities
3071 *
3072 * todo:- add code to check if ctrl attributes changed from
3073 * prior connection values
3074 */
3075
3076 ret = nvme_enable_ctrl(&ctrl->ctrl);
3077 if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
3078 ret = -EIO;
3079 if (ret)
3080 goto out_disconnect_admin_queue;
3081
3082 ctrl->ctrl.max_segments = ctrl->lport->ops->max_sgl_segments;
3083 ctrl->ctrl.max_hw_sectors = ctrl->ctrl.max_segments <<
3084 (ilog2(SZ_4K) - 9);
3085
3086 nvme_unquiesce_admin_queue(&ctrl->ctrl);
3087
3088 ret = nvme_init_ctrl_finish(&ctrl->ctrl, false);
3089 if (ret)
3090 goto out_disconnect_admin_queue;
3091 if (test_bit(ASSOC_FAILED, &ctrl->flags)) {
3092 ret = -EIO;
3093 goto out_stop_keep_alive;
3094 }
3095 /* sanity checks */
3096
3097 /* FC-NVME does not have other data in the capsule */
3098 if (ctrl->ctrl.icdoff) {
3099 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
3100 ctrl->ctrl.icdoff);
3101 ret = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
3102 goto out_stop_keep_alive;
3103 }
3104
3105 /* FC-NVME supports normal SGL Data Block Descriptors */
3106 if (!nvme_ctrl_sgl_supported(&ctrl->ctrl)) {
3107 dev_err(ctrl->ctrl.device,
3108 "Mandatory sgls are not supported!\n");
3109 ret = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
3110 goto out_stop_keep_alive;
3111 }
3112
3113 if (opts->queue_size > ctrl->ctrl.maxcmd) {
3114 /* warn if maxcmd is lower than queue_size */
3115 dev_warn(ctrl->ctrl.device,
3116 "queue_size %zu > ctrl maxcmd %u, reducing "
3117 "to maxcmd\n",
3118 opts->queue_size, ctrl->ctrl.maxcmd);
3119 opts->queue_size = ctrl->ctrl.maxcmd;
3120 ctrl->ctrl.sqsize = opts->queue_size - 1;
3121 }
3122
3123 ret = nvme_fc_init_aen_ops(ctrl);
3124 if (ret)
3125 goto out_term_aen_ops;
3126
3127 /*
3128 * Create the io queues
3129 */
3130
3131 if (ctrl->ctrl.queue_count > 1) {
3132 if (!ctrl->ioq_live)
3133 ret = nvme_fc_create_io_queues(ctrl);
3134 else
3135 ret = nvme_fc_recreate_io_queues(ctrl);
3136 }
3137 if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
3138 ret = -EIO;
3139 if (ret)
3140 goto out_term_aen_ops;
3141
3142 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE)) {
3143 ret = -EIO;
3144 goto out_term_aen_ops;
3145 }
3146
3147 ctrl->ctrl.nr_reconnects = 0;
3148 nvme_start_ctrl(&ctrl->ctrl);
3149
3150 return 0; /* Success */
3151
3152 out_term_aen_ops:
3153 nvme_fc_term_aen_ops(ctrl);
3154 out_stop_keep_alive:
3155 nvme_stop_keep_alive(&ctrl->ctrl);
3156 out_disconnect_admin_queue:
3157 dev_warn(ctrl->ctrl.device,
3158 "NVME-FC{%d}: create_assoc failed, assoc_id %llx ret %d\n",
3159 ctrl->cnum, ctrl->association_id, ret);
3160 /* send a Disconnect(association) LS to fc-nvme target */
3161 nvme_fc_xmt_disconnect_assoc(ctrl);
3162 spin_lock_irqsave(&ctrl->lock, flags);
3163 ctrl->association_id = 0;
3164 disls = ctrl->rcv_disconn;
3165 ctrl->rcv_disconn = NULL;
3166 spin_unlock_irqrestore(&ctrl->lock, flags);
3167 if (disls)
3168 nvme_fc_xmt_ls_rsp(disls);
3169 out_delete_hw_queue:
3170 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3171 out_free_queue:
3172 nvme_fc_free_queue(&ctrl->queues[0]);
3173 clear_bit(ASSOC_ACTIVE, &ctrl->flags);
3174 nvme_fc_ctlr_inactive_on_rport(ctrl);
3175
3176 return ret;
3177 }
3178
3179
3180 /*
3181 * This routine stops operation of the controller on the host side.
3182 * On the host os stack side: Admin and IO queues are stopped,
3183 * outstanding ios on them terminated via FC ABTS.
3184 * On the link side: the association is terminated.
3185 */
3186 static void
nvme_fc_delete_association(struct nvme_fc_ctrl * ctrl)3187 nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
3188 {
3189 struct nvmefc_ls_rcv_op *disls = NULL;
3190 unsigned long flags;
3191
3192 if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
3193 return;
3194
3195 spin_lock_irqsave(&ctrl->lock, flags);
3196 set_bit(FCCTRL_TERMIO, &ctrl->flags);
3197 ctrl->iocnt = 0;
3198 spin_unlock_irqrestore(&ctrl->lock, flags);
3199
3200 __nvme_fc_abort_outstanding_ios(ctrl, false);
3201
3202 /* kill the aens as they are a separate path */
3203 nvme_fc_abort_aen_ops(ctrl);
3204
3205 /* wait for all io that had to be aborted */
3206 spin_lock_irq(&ctrl->lock);
3207 wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
3208 clear_bit(FCCTRL_TERMIO, &ctrl->flags);
3209 spin_unlock_irq(&ctrl->lock);
3210
3211 nvme_fc_term_aen_ops(ctrl);
3212
3213 /*
3214 * send a Disconnect(association) LS to fc-nvme target
3215 * Note: could have been sent at top of process, but
3216 * cleaner on link traffic if after the aborts complete.
3217 * Note: if association doesn't exist, association_id will be 0
3218 */
3219 if (ctrl->association_id)
3220 nvme_fc_xmt_disconnect_assoc(ctrl);
3221
3222 spin_lock_irqsave(&ctrl->lock, flags);
3223 ctrl->association_id = 0;
3224 disls = ctrl->rcv_disconn;
3225 ctrl->rcv_disconn = NULL;
3226 spin_unlock_irqrestore(&ctrl->lock, flags);
3227 if (disls)
3228 /*
3229 * if a Disconnect Request was waiting for a response, send
3230 * now that all ABTS's have been issued (and are complete).
3231 */
3232 nvme_fc_xmt_ls_rsp(disls);
3233
3234 if (ctrl->ctrl.tagset) {
3235 nvme_fc_delete_hw_io_queues(ctrl);
3236 nvme_fc_free_io_queues(ctrl);
3237 }
3238
3239 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3240 nvme_fc_free_queue(&ctrl->queues[0]);
3241
3242 /* re-enable the admin_q so anything new can fast fail */
3243 nvme_unquiesce_admin_queue(&ctrl->ctrl);
3244
3245 /* resume the io queues so that things will fast fail */
3246 nvme_unquiesce_io_queues(&ctrl->ctrl);
3247
3248 nvme_fc_ctlr_inactive_on_rport(ctrl);
3249 }
3250
3251 static void
nvme_fc_delete_ctrl(struct nvme_ctrl * nctrl)3252 nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
3253 {
3254 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
3255
3256 cancel_work_sync(&ctrl->ioerr_work);
3257 cancel_delayed_work_sync(&ctrl->connect_work);
3258 /*
3259 * kill the association on the link side. this will block
3260 * waiting for io to terminate
3261 */
3262 nvme_fc_delete_association(ctrl);
3263 }
3264
3265 static void
nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl * ctrl,int status)3266 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
3267 {
3268 struct nvme_fc_rport *rport = ctrl->rport;
3269 struct nvme_fc_remote_port *portptr = &rport->remoteport;
3270 unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
3271 bool recon = true;
3272
3273 if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_CONNECTING)
3274 return;
3275
3276 if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3277 dev_info(ctrl->ctrl.device,
3278 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
3279 ctrl->cnum, status);
3280 } else if (time_after_eq(jiffies, rport->dev_loss_end))
3281 recon = false;
3282
3283 if (recon && nvmf_should_reconnect(&ctrl->ctrl, status)) {
3284 if (portptr->port_state == FC_OBJSTATE_ONLINE)
3285 dev_info(ctrl->ctrl.device,
3286 "NVME-FC{%d}: Reconnect attempt in %ld "
3287 "seconds\n",
3288 ctrl->cnum, recon_delay / HZ);
3289 else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
3290 recon_delay = rport->dev_loss_end - jiffies;
3291
3292 queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
3293 } else {
3294 if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3295 if (status > 0 && (status & NVME_STATUS_DNR))
3296 dev_warn(ctrl->ctrl.device,
3297 "NVME-FC{%d}: reconnect failure\n",
3298 ctrl->cnum);
3299 else
3300 dev_warn(ctrl->ctrl.device,
3301 "NVME-FC{%d}: Max reconnect attempts "
3302 "(%d) reached.\n",
3303 ctrl->cnum, ctrl->ctrl.nr_reconnects);
3304 } else
3305 dev_warn(ctrl->ctrl.device,
3306 "NVME-FC{%d}: dev_loss_tmo (%d) expired "
3307 "while waiting for remoteport connectivity.\n",
3308 ctrl->cnum, min_t(int, portptr->dev_loss_tmo,
3309 (ctrl->ctrl.opts->max_reconnects *
3310 ctrl->ctrl.opts->reconnect_delay)));
3311 WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
3312 }
3313 }
3314
3315 static void
nvme_fc_reset_ctrl_work(struct work_struct * work)3316 nvme_fc_reset_ctrl_work(struct work_struct *work)
3317 {
3318 struct nvme_fc_ctrl *ctrl =
3319 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
3320
3321 nvme_stop_ctrl(&ctrl->ctrl);
3322
3323 /* will block will waiting for io to terminate */
3324 nvme_fc_delete_association(ctrl);
3325
3326 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
3327 dev_err(ctrl->ctrl.device,
3328 "NVME-FC{%d}: error_recovery: Couldn't change state "
3329 "to CONNECTING\n", ctrl->cnum);
3330
3331 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
3332 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3333 dev_err(ctrl->ctrl.device,
3334 "NVME-FC{%d}: failed to schedule connect "
3335 "after reset\n", ctrl->cnum);
3336 } else {
3337 flush_delayed_work(&ctrl->connect_work);
3338 }
3339 } else {
3340 nvme_fc_reconnect_or_delete(ctrl, -ENOTCONN);
3341 }
3342 }
3343
3344
3345 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
3346 .name = "fc",
3347 .module = THIS_MODULE,
3348 .flags = NVME_F_FABRICS,
3349 .reg_read32 = nvmf_reg_read32,
3350 .reg_read64 = nvmf_reg_read64,
3351 .reg_write32 = nvmf_reg_write32,
3352 .subsystem_reset = nvmf_subsystem_reset,
3353 .free_ctrl = nvme_fc_free_ctrl,
3354 .submit_async_event = nvme_fc_submit_async_event,
3355 .delete_ctrl = nvme_fc_delete_ctrl,
3356 .get_address = nvmf_get_address,
3357 };
3358
3359 static void
nvme_fc_connect_ctrl_work(struct work_struct * work)3360 nvme_fc_connect_ctrl_work(struct work_struct *work)
3361 {
3362 int ret;
3363
3364 struct nvme_fc_ctrl *ctrl =
3365 container_of(to_delayed_work(work),
3366 struct nvme_fc_ctrl, connect_work);
3367
3368 ret = nvme_fc_create_association(ctrl);
3369 if (ret)
3370 nvme_fc_reconnect_or_delete(ctrl, ret);
3371 else
3372 dev_info(ctrl->ctrl.device,
3373 "NVME-FC{%d}: controller connect complete\n",
3374 ctrl->cnum);
3375 }
3376
3377
3378 static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
3379 .queue_rq = nvme_fc_queue_rq,
3380 .complete = nvme_fc_complete_rq,
3381 .init_request = nvme_fc_init_request,
3382 .exit_request = nvme_fc_exit_request,
3383 .init_hctx = nvme_fc_init_admin_hctx,
3384 .timeout = nvme_fc_timeout,
3385 };
3386
3387
3388 /*
3389 * Fails a controller request if it matches an existing controller
3390 * (association) with the same tuple:
3391 * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
3392 *
3393 * The ports don't need to be compared as they are intrinsically
3394 * already matched by the port pointers supplied.
3395 */
3396 static bool
nvme_fc_existing_controller(struct nvme_fc_rport * rport,struct nvmf_ctrl_options * opts)3397 nvme_fc_existing_controller(struct nvme_fc_rport *rport,
3398 struct nvmf_ctrl_options *opts)
3399 {
3400 struct nvme_fc_ctrl *ctrl;
3401 unsigned long flags;
3402 bool found = false;
3403
3404 spin_lock_irqsave(&rport->lock, flags);
3405 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3406 found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
3407 if (found)
3408 break;
3409 }
3410 spin_unlock_irqrestore(&rport->lock, flags);
3411
3412 return found;
3413 }
3414
3415 static struct nvme_fc_ctrl *
nvme_fc_alloc_ctrl(struct device * dev,struct nvmf_ctrl_options * opts,struct nvme_fc_lport * lport,struct nvme_fc_rport * rport)3416 nvme_fc_alloc_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3417 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3418 {
3419 struct nvme_fc_ctrl *ctrl;
3420 int ret, idx, ctrl_loss_tmo;
3421
3422 if (!(rport->remoteport.port_role &
3423 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
3424 ret = -EBADR;
3425 goto out_fail;
3426 }
3427
3428 if (!opts->duplicate_connect &&
3429 nvme_fc_existing_controller(rport, opts)) {
3430 ret = -EALREADY;
3431 goto out_fail;
3432 }
3433
3434 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3435 if (!ctrl) {
3436 ret = -ENOMEM;
3437 goto out_fail;
3438 }
3439
3440 idx = ida_alloc(&nvme_fc_ctrl_cnt, GFP_KERNEL);
3441 if (idx < 0) {
3442 ret = -ENOSPC;
3443 goto out_free_ctrl;
3444 }
3445
3446 /*
3447 * if ctrl_loss_tmo is being enforced and the default reconnect delay
3448 * is being used, change to a shorter reconnect delay for FC.
3449 */
3450 if (opts->max_reconnects != -1 &&
3451 opts->reconnect_delay == NVMF_DEF_RECONNECT_DELAY &&
3452 opts->reconnect_delay > NVME_FC_DEFAULT_RECONNECT_TMO) {
3453 ctrl_loss_tmo = opts->max_reconnects * opts->reconnect_delay;
3454 opts->reconnect_delay = NVME_FC_DEFAULT_RECONNECT_TMO;
3455 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
3456 opts->reconnect_delay);
3457 }
3458
3459 ctrl->ctrl.opts = opts;
3460 ctrl->ctrl.nr_reconnects = 0;
3461 INIT_LIST_HEAD(&ctrl->ctrl_list);
3462 ctrl->lport = lport;
3463 ctrl->rport = rport;
3464 ctrl->dev = lport->dev;
3465 ctrl->cnum = idx;
3466 ctrl->ioq_live = false;
3467 init_waitqueue_head(&ctrl->ioabort_wait);
3468
3469 get_device(ctrl->dev);
3470 kref_init(&ctrl->ref);
3471
3472 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
3473 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
3474 INIT_WORK(&ctrl->ioerr_work, nvme_fc_ctrl_ioerr_work);
3475 spin_lock_init(&ctrl->lock);
3476
3477 /* io queue count */
3478 ctrl->ctrl.queue_count = min_t(unsigned int,
3479 opts->nr_io_queues,
3480 lport->ops->max_hw_queues);
3481 ctrl->ctrl.queue_count++; /* +1 for admin queue */
3482
3483 ctrl->ctrl.sqsize = opts->queue_size - 1;
3484 ctrl->ctrl.kato = opts->kato;
3485 ctrl->ctrl.cntlid = 0xffff;
3486
3487 ret = -ENOMEM;
3488 ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3489 sizeof(struct nvme_fc_queue), GFP_KERNEL);
3490 if (!ctrl->queues)
3491 goto out_free_ida;
3492
3493 nvme_fc_init_queue(ctrl, 0);
3494
3495 /*
3496 * Would have been nice to init io queues tag set as well.
3497 * However, we require interaction from the controller
3498 * for max io queue count before we can do so.
3499 * Defer this to the connect path.
3500 */
3501
3502 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3503 if (ret)
3504 goto out_free_queues;
3505 if (lport->dev)
3506 ctrl->ctrl.numa_node = dev_to_node(lport->dev);
3507
3508 return ctrl;
3509
3510 out_free_queues:
3511 kfree(ctrl->queues);
3512 out_free_ida:
3513 put_device(ctrl->dev);
3514 ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
3515 out_free_ctrl:
3516 kfree(ctrl);
3517 out_fail:
3518 /* exit via here doesn't follow ctlr ref points */
3519 return ERR_PTR(ret);
3520 }
3521
3522 static struct nvme_ctrl *
nvme_fc_init_ctrl(struct device * dev,struct nvmf_ctrl_options * opts,struct nvme_fc_lport * lport,struct nvme_fc_rport * rport)3523 nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3524 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3525 {
3526 struct nvme_fc_ctrl *ctrl;
3527 unsigned long flags;
3528 int ret;
3529
3530 ctrl = nvme_fc_alloc_ctrl(dev, opts, lport, rport);
3531 if (IS_ERR(ctrl))
3532 return ERR_CAST(ctrl);
3533
3534 ret = nvme_add_ctrl(&ctrl->ctrl);
3535 if (ret)
3536 goto out_put_ctrl;
3537
3538 ret = nvme_alloc_admin_tag_set(&ctrl->ctrl, &ctrl->admin_tag_set,
3539 &nvme_fc_admin_mq_ops,
3540 struct_size_t(struct nvme_fcp_op_w_sgl, priv,
3541 ctrl->lport->ops->fcprqst_priv_sz));
3542 if (ret)
3543 goto fail_ctrl;
3544
3545 spin_lock_irqsave(&rport->lock, flags);
3546 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3547 spin_unlock_irqrestore(&rport->lock, flags);
3548
3549 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
3550 dev_err(ctrl->ctrl.device,
3551 "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
3552 goto fail_ctrl;
3553 }
3554
3555 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3556 dev_err(ctrl->ctrl.device,
3557 "NVME-FC{%d}: failed to schedule initial connect\n",
3558 ctrl->cnum);
3559 goto fail_ctrl;
3560 }
3561
3562 flush_delayed_work(&ctrl->connect_work);
3563
3564 dev_info(ctrl->ctrl.device,
3565 "NVME-FC{%d}: new ctrl: NQN \"%s\", hostnqn: %s\n",
3566 ctrl->cnum, nvmf_ctrl_subsysnqn(&ctrl->ctrl), opts->host->nqn);
3567
3568 return &ctrl->ctrl;
3569
3570 fail_ctrl:
3571 nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3572 cancel_work_sync(&ctrl->ioerr_work);
3573 cancel_work_sync(&ctrl->ctrl.reset_work);
3574 cancel_delayed_work_sync(&ctrl->connect_work);
3575
3576 ctrl->ctrl.opts = NULL;
3577
3578 /* initiate nvme ctrl ref counting teardown */
3579 nvme_uninit_ctrl(&ctrl->ctrl);
3580
3581 out_put_ctrl:
3582 /* Remove core ctrl ref. */
3583 nvme_put_ctrl(&ctrl->ctrl);
3584
3585 /* as we're past the point where we transition to the ref
3586 * counting teardown path, if we return a bad pointer here,
3587 * the calling routine, thinking it's prior to the
3588 * transition, will do an rport put. Since the teardown
3589 * path also does a rport put, we do an extra get here to
3590 * so proper order/teardown happens.
3591 */
3592 nvme_fc_rport_get(rport);
3593
3594 return ERR_PTR(-EIO);
3595 }
3596
3597 struct nvmet_fc_traddr {
3598 u64 nn;
3599 u64 pn;
3600 };
3601
3602 static int
__nvme_fc_parse_u64(substring_t * sstr,u64 * val)3603 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3604 {
3605 u64 token64;
3606
3607 if (match_u64(sstr, &token64))
3608 return -EINVAL;
3609 *val = token64;
3610
3611 return 0;
3612 }
3613
3614 /*
3615 * This routine validates and extracts the WWN's from the TRADDR string.
3616 * As kernel parsers need the 0x to determine number base, universally
3617 * build string to parse with 0x prefix before parsing name strings.
3618 */
3619 static int
nvme_fc_parse_traddr(struct nvmet_fc_traddr * traddr,char * buf,size_t blen)3620 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
3621 {
3622 char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
3623 substring_t wwn = { name, &name[sizeof(name)-1] };
3624 int nnoffset, pnoffset;
3625
3626 /* validate if string is one of the 2 allowed formats */
3627 if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
3628 !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
3629 !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
3630 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
3631 nnoffset = NVME_FC_TRADDR_OXNNLEN;
3632 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
3633 NVME_FC_TRADDR_OXNNLEN;
3634 } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
3635 !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
3636 !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
3637 "pn-", NVME_FC_TRADDR_NNLEN))) {
3638 nnoffset = NVME_FC_TRADDR_NNLEN;
3639 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
3640 } else
3641 goto out_einval;
3642
3643 name[0] = '0';
3644 name[1] = 'x';
3645 name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
3646
3647 memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3648 if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
3649 goto out_einval;
3650
3651 memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3652 if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
3653 goto out_einval;
3654
3655 return 0;
3656
3657 out_einval:
3658 pr_warn("%s: bad traddr string\n", __func__);
3659 return -EINVAL;
3660 }
3661
3662 static struct nvme_ctrl *
nvme_fc_create_ctrl(struct device * dev,struct nvmf_ctrl_options * opts)3663 nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3664 {
3665 struct nvme_fc_lport *lport;
3666 struct nvme_fc_rport *rport;
3667 struct nvme_ctrl *ctrl;
3668 struct nvmet_fc_traddr laddr = { 0L, 0L };
3669 struct nvmet_fc_traddr raddr = { 0L, 0L };
3670 unsigned long flags;
3671 int ret;
3672
3673 ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3674 if (ret || !raddr.nn || !raddr.pn)
3675 return ERR_PTR(-EINVAL);
3676
3677 ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3678 if (ret || !laddr.nn || !laddr.pn)
3679 return ERR_PTR(-EINVAL);
3680
3681 /* find the host and remote ports to connect together */
3682 spin_lock_irqsave(&nvme_fc_lock, flags);
3683 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3684 if (lport->localport.node_name != laddr.nn ||
3685 lport->localport.port_name != laddr.pn ||
3686 lport->localport.port_state != FC_OBJSTATE_ONLINE)
3687 continue;
3688
3689 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3690 if (rport->remoteport.node_name != raddr.nn ||
3691 rport->remoteport.port_name != raddr.pn ||
3692 rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3693 continue;
3694
3695 /* if fail to get reference fall through. Will error */
3696 if (!nvme_fc_rport_get(rport))
3697 break;
3698
3699 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3700
3701 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3702 if (IS_ERR(ctrl))
3703 nvme_fc_rport_put(rport);
3704 return ctrl;
3705 }
3706 }
3707 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3708
3709 pr_warn("%s: %s - %s combination not found\n",
3710 __func__, opts->traddr, opts->host_traddr);
3711 return ERR_PTR(-ENOENT);
3712 }
3713
3714
3715 static struct nvmf_transport_ops nvme_fc_transport = {
3716 .name = "fc",
3717 .module = THIS_MODULE,
3718 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
3719 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3720 .create_ctrl = nvme_fc_create_ctrl,
3721 };
3722
3723 /* Arbitrary successive failures max. With lots of subsystems could be high */
3724 #define DISCOVERY_MAX_FAIL 20
3725
nvme_fc_nvme_discovery_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3726 static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
3727 struct device_attribute *attr, const char *buf, size_t count)
3728 {
3729 unsigned long flags;
3730 LIST_HEAD(local_disc_list);
3731 struct nvme_fc_lport *lport;
3732 struct nvme_fc_rport *rport;
3733 int failcnt = 0;
3734
3735 spin_lock_irqsave(&nvme_fc_lock, flags);
3736 restart:
3737 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3738 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3739 if (!nvme_fc_lport_get(lport))
3740 continue;
3741 if (!nvme_fc_rport_get(rport)) {
3742 /*
3743 * This is a temporary condition. Upon restart
3744 * this rport will be gone from the list.
3745 *
3746 * Revert the lport put and retry. Anything
3747 * added to the list already will be skipped (as
3748 * they are no longer list_empty). Loops should
3749 * resume at rports that were not yet seen.
3750 */
3751 nvme_fc_lport_put(lport);
3752
3753 if (failcnt++ < DISCOVERY_MAX_FAIL)
3754 goto restart;
3755
3756 pr_err("nvme_discovery: too many reference "
3757 "failures\n");
3758 goto process_local_list;
3759 }
3760 if (list_empty(&rport->disc_list))
3761 list_add_tail(&rport->disc_list,
3762 &local_disc_list);
3763 }
3764 }
3765
3766 process_local_list:
3767 while (!list_empty(&local_disc_list)) {
3768 rport = list_first_entry(&local_disc_list,
3769 struct nvme_fc_rport, disc_list);
3770 list_del_init(&rport->disc_list);
3771 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3772
3773 lport = rport->lport;
3774 /* signal discovery. Won't hurt if it repeats */
3775 nvme_fc_signal_discovery_scan(lport, rport);
3776 nvme_fc_rport_put(rport);
3777 nvme_fc_lport_put(lport);
3778
3779 spin_lock_irqsave(&nvme_fc_lock, flags);
3780 }
3781 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3782
3783 return count;
3784 }
3785
3786 static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
3787
3788 #ifdef CONFIG_BLK_CGROUP_FC_APPID
3789 /* Parse the cgroup id from a buf and return the length of cgrpid */
fc_parse_cgrpid(const char * buf,u64 * id)3790 static int fc_parse_cgrpid(const char *buf, u64 *id)
3791 {
3792 char cgrp_id[16+1];
3793 int cgrpid_len, j;
3794
3795 memset(cgrp_id, 0x0, sizeof(cgrp_id));
3796 for (cgrpid_len = 0, j = 0; cgrpid_len < 17; cgrpid_len++) {
3797 if (buf[cgrpid_len] != ':')
3798 cgrp_id[cgrpid_len] = buf[cgrpid_len];
3799 else {
3800 j = 1;
3801 break;
3802 }
3803 }
3804 if (!j)
3805 return -EINVAL;
3806 if (kstrtou64(cgrp_id, 16, id) < 0)
3807 return -EINVAL;
3808 return cgrpid_len;
3809 }
3810
3811 /*
3812 * Parse and update the appid in the blkcg associated with the cgroupid.
3813 */
fc_appid_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3814 static ssize_t fc_appid_store(struct device *dev,
3815 struct device_attribute *attr, const char *buf, size_t count)
3816 {
3817 size_t orig_count = count;
3818 u64 cgrp_id;
3819 int appid_len = 0;
3820 int cgrpid_len = 0;
3821 char app_id[FC_APPID_LEN];
3822 int ret = 0;
3823
3824 if (buf[count-1] == '\n')
3825 count--;
3826
3827 if ((count > (16+1+FC_APPID_LEN)) || (!strchr(buf, ':')))
3828 return -EINVAL;
3829
3830 cgrpid_len = fc_parse_cgrpid(buf, &cgrp_id);
3831 if (cgrpid_len < 0)
3832 return -EINVAL;
3833 appid_len = count - cgrpid_len - 1;
3834 if (appid_len > FC_APPID_LEN)
3835 return -EINVAL;
3836
3837 memset(app_id, 0x0, sizeof(app_id));
3838 memcpy(app_id, &buf[cgrpid_len+1], appid_len);
3839 ret = blkcg_set_fc_appid(app_id, cgrp_id, sizeof(app_id));
3840 if (ret < 0)
3841 return ret;
3842 return orig_count;
3843 }
3844 static DEVICE_ATTR(appid_store, 0200, NULL, fc_appid_store);
3845 #endif /* CONFIG_BLK_CGROUP_FC_APPID */
3846
3847 static struct attribute *nvme_fc_attrs[] = {
3848 &dev_attr_nvme_discovery.attr,
3849 #ifdef CONFIG_BLK_CGROUP_FC_APPID
3850 &dev_attr_appid_store.attr,
3851 #endif
3852 NULL
3853 };
3854
3855 static const struct attribute_group nvme_fc_attr_group = {
3856 .attrs = nvme_fc_attrs,
3857 };
3858
3859 static const struct attribute_group *nvme_fc_attr_groups[] = {
3860 &nvme_fc_attr_group,
3861 NULL
3862 };
3863
3864 static struct class fc_class = {
3865 .name = "fc",
3866 .dev_groups = nvme_fc_attr_groups,
3867 };
3868
nvme_fc_init_module(void)3869 static int __init nvme_fc_init_module(void)
3870 {
3871 int ret;
3872
3873 /*
3874 * NOTE:
3875 * It is expected that in the future the kernel will combine
3876 * the FC-isms that are currently under scsi and now being
3877 * added to by NVME into a new standalone FC class. The SCSI
3878 * and NVME protocols and their devices would be under this
3879 * new FC class.
3880 *
3881 * As we need something to post FC-specific udev events to,
3882 * specifically for nvme probe events, start by creating the
3883 * new device class. When the new standalone FC class is
3884 * put in place, this code will move to a more generic
3885 * location for the class.
3886 */
3887 ret = class_register(&fc_class);
3888 if (ret) {
3889 pr_err("couldn't register class fc\n");
3890 return ret;
3891 }
3892
3893 /*
3894 * Create a device for the FC-centric udev events
3895 */
3896 fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
3897 "fc_udev_device");
3898 if (IS_ERR(fc_udev_device)) {
3899 pr_err("couldn't create fc_udev device!\n");
3900 ret = PTR_ERR(fc_udev_device);
3901 goto out_destroy_class;
3902 }
3903
3904 ret = nvmf_register_transport(&nvme_fc_transport);
3905 if (ret)
3906 goto out_destroy_device;
3907
3908 return 0;
3909
3910 out_destroy_device:
3911 device_destroy(&fc_class, MKDEV(0, 0));
3912 out_destroy_class:
3913 class_unregister(&fc_class);
3914
3915 return ret;
3916 }
3917
3918 static void
nvme_fc_delete_controllers(struct nvme_fc_rport * rport)3919 nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
3920 {
3921 struct nvme_fc_ctrl *ctrl;
3922
3923 spin_lock(&rport->lock);
3924 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3925 dev_warn(ctrl->ctrl.device,
3926 "NVME-FC{%d}: transport unloading: deleting ctrl\n",
3927 ctrl->cnum);
3928 nvme_delete_ctrl(&ctrl->ctrl);
3929 }
3930 spin_unlock(&rport->lock);
3931 }
3932
nvme_fc_exit_module(void)3933 static void __exit nvme_fc_exit_module(void)
3934 {
3935 struct nvme_fc_lport *lport;
3936 struct nvme_fc_rport *rport;
3937 unsigned long flags;
3938
3939 spin_lock_irqsave(&nvme_fc_lock, flags);
3940 list_for_each_entry(lport, &nvme_fc_lport_list, port_list)
3941 list_for_each_entry(rport, &lport->endp_list, endp_list)
3942 nvme_fc_delete_controllers(rport);
3943 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3944 flush_workqueue(nvme_delete_wq);
3945
3946 nvmf_unregister_transport(&nvme_fc_transport);
3947
3948 device_destroy(&fc_class, MKDEV(0, 0));
3949 class_unregister(&fc_class);
3950 }
3951
3952 module_init(nvme_fc_init_module);
3953 module_exit(nvme_fc_exit_module);
3954
3955 MODULE_DESCRIPTION("NVMe host FC transport driver");
3956 MODULE_LICENSE("GPL v2");
3957