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_done(struct nvmefc_ls_rsp * lsrsp)1413 nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
1414 {
1415 struct nvmefc_ls_rcv_op *lsop = lsrsp->nvme_fc_private;
1416 struct nvme_fc_rport *rport = lsop->rport;
1417 struct nvme_fc_lport *lport = rport->lport;
1418 unsigned long flags;
1419
1420 spin_lock_irqsave(&rport->lock, flags);
1421 list_del(&lsop->lsrcv_list);
1422 spin_unlock_irqrestore(&rport->lock, flags);
1423
1424 fc_dma_sync_single_for_cpu(lport->dev, lsop->rspdma,
1425 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1426 fc_dma_unmap_single(lport->dev, lsop->rspdma,
1427 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1428
1429 kfree(lsop->rspbuf);
1430 kfree(lsop->rqstbuf);
1431 kfree(lsop);
1432
1433 nvme_fc_rport_put(rport);
1434 }
1435
1436 static void
nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op * lsop)1437 nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op *lsop)
1438 {
1439 struct nvme_fc_rport *rport = lsop->rport;
1440 struct nvme_fc_lport *lport = rport->lport;
1441 struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
1442 int ret;
1443
1444 fc_dma_sync_single_for_device(lport->dev, lsop->rspdma,
1445 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1446
1447 ret = lport->ops->xmt_ls_rsp(&lport->localport, &rport->remoteport,
1448 lsop->lsrsp);
1449 if (ret) {
1450 dev_warn(lport->dev,
1451 "LLDD rejected LS RSP xmt: LS %d status %d\n",
1452 w0->ls_cmd, ret);
1453 nvme_fc_xmt_ls_rsp_done(lsop->lsrsp);
1454 return;
1455 }
1456 }
1457
1458 static struct nvme_fc_ctrl *
nvme_fc_match_disconn_ls(struct nvme_fc_rport * rport,struct nvmefc_ls_rcv_op * lsop)1459 nvme_fc_match_disconn_ls(struct nvme_fc_rport *rport,
1460 struct nvmefc_ls_rcv_op *lsop)
1461 {
1462 struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1463 &lsop->rqstbuf->rq_dis_assoc;
1464 struct nvme_fc_ctrl *ctrl, *ret = NULL;
1465 struct nvmefc_ls_rcv_op *oldls = NULL;
1466 u64 association_id = be64_to_cpu(rqst->associd.association_id);
1467 unsigned long flags;
1468
1469 spin_lock_irqsave(&rport->lock, flags);
1470
1471 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
1472 if (!nvme_fc_ctrl_get(ctrl))
1473 continue;
1474 spin_lock(&ctrl->lock);
1475 if (association_id == ctrl->association_id) {
1476 oldls = ctrl->rcv_disconn;
1477 ctrl->rcv_disconn = lsop;
1478 ret = ctrl;
1479 }
1480 spin_unlock(&ctrl->lock);
1481 if (ret)
1482 /* leave the ctrl get reference */
1483 break;
1484 nvme_fc_ctrl_put(ctrl);
1485 }
1486
1487 spin_unlock_irqrestore(&rport->lock, flags);
1488
1489 /* transmit a response for anything that was pending */
1490 if (oldls) {
1491 dev_info(rport->lport->dev,
1492 "NVME-FC{%d}: Multiple Disconnect Association "
1493 "LS's received\n", ctrl->cnum);
1494 /* overwrite good response with bogus failure */
1495 oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf,
1496 sizeof(*oldls->rspbuf),
1497 rqst->w0.ls_cmd,
1498 FCNVME_RJT_RC_UNAB,
1499 FCNVME_RJT_EXP_NONE, 0);
1500 nvme_fc_xmt_ls_rsp(oldls);
1501 }
1502
1503 return ret;
1504 }
1505
1506 /*
1507 * returns true to mean LS handled and ls_rsp can be sent
1508 * returns false to defer ls_rsp xmt (will be done as part of
1509 * association termination)
1510 */
1511 static bool
nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op * lsop)1512 nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op *lsop)
1513 {
1514 struct nvme_fc_rport *rport = lsop->rport;
1515 struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1516 &lsop->rqstbuf->rq_dis_assoc;
1517 struct fcnvme_ls_disconnect_assoc_acc *acc =
1518 &lsop->rspbuf->rsp_dis_assoc;
1519 struct nvme_fc_ctrl *ctrl = NULL;
1520 int ret = 0;
1521
1522 memset(acc, 0, sizeof(*acc));
1523
1524 ret = nvmefc_vldt_lsreq_discon_assoc(lsop->rqstdatalen, rqst);
1525 if (!ret) {
1526 /* match an active association */
1527 ctrl = nvme_fc_match_disconn_ls(rport, lsop);
1528 if (!ctrl)
1529 ret = VERR_NO_ASSOC;
1530 }
1531
1532 if (ret) {
1533 dev_info(rport->lport->dev,
1534 "Disconnect LS failed: %s\n",
1535 validation_errors[ret]);
1536 lsop->lsrsp->rsplen = nvme_fc_format_rjt(acc,
1537 sizeof(*acc), rqst->w0.ls_cmd,
1538 (ret == VERR_NO_ASSOC) ?
1539 FCNVME_RJT_RC_INV_ASSOC :
1540 FCNVME_RJT_RC_LOGIC,
1541 FCNVME_RJT_EXP_NONE, 0);
1542 return true;
1543 }
1544
1545 /* format an ACCept response */
1546
1547 lsop->lsrsp->rsplen = sizeof(*acc);
1548
1549 nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1550 fcnvme_lsdesc_len(
1551 sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
1552 FCNVME_LS_DISCONNECT_ASSOC);
1553
1554 /*
1555 * the transmit of the response will occur after the exchanges
1556 * for the association have been ABTS'd by
1557 * nvme_fc_delete_association().
1558 */
1559
1560 /* fail the association */
1561 nvme_fc_error_recovery(ctrl, "Disconnect Association LS received");
1562
1563 /* release the reference taken by nvme_fc_match_disconn_ls() */
1564 nvme_fc_ctrl_put(ctrl);
1565
1566 return false;
1567 }
1568
1569 /*
1570 * Actual Processing routine for received FC-NVME LS Requests from the LLD
1571 * returns true if a response should be sent afterward, false if rsp will
1572 * be sent asynchronously.
1573 */
1574 static bool
nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op * lsop)1575 nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op *lsop)
1576 {
1577 struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
1578 bool ret = true;
1579
1580 lsop->lsrsp->nvme_fc_private = lsop;
1581 lsop->lsrsp->rspbuf = lsop->rspbuf;
1582 lsop->lsrsp->rspdma = lsop->rspdma;
1583 lsop->lsrsp->done = nvme_fc_xmt_ls_rsp_done;
1584 /* Be preventative. handlers will later set to valid length */
1585 lsop->lsrsp->rsplen = 0;
1586
1587 /*
1588 * handlers:
1589 * parse request input, execute the request, and format the
1590 * LS response
1591 */
1592 switch (w0->ls_cmd) {
1593 case FCNVME_LS_DISCONNECT_ASSOC:
1594 ret = nvme_fc_ls_disconnect_assoc(lsop);
1595 break;
1596 case FCNVME_LS_DISCONNECT_CONN:
1597 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1598 sizeof(*lsop->rspbuf), w0->ls_cmd,
1599 FCNVME_RJT_RC_UNSUP, FCNVME_RJT_EXP_NONE, 0);
1600 break;
1601 case FCNVME_LS_CREATE_ASSOCIATION:
1602 case FCNVME_LS_CREATE_CONNECTION:
1603 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1604 sizeof(*lsop->rspbuf), w0->ls_cmd,
1605 FCNVME_RJT_RC_LOGIC, FCNVME_RJT_EXP_NONE, 0);
1606 break;
1607 default:
1608 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1609 sizeof(*lsop->rspbuf), w0->ls_cmd,
1610 FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
1611 break;
1612 }
1613
1614 return(ret);
1615 }
1616
1617 static void
nvme_fc_handle_ls_rqst_work(struct work_struct * work)1618 nvme_fc_handle_ls_rqst_work(struct work_struct *work)
1619 {
1620 struct nvme_fc_rport *rport =
1621 container_of(work, struct nvme_fc_rport, lsrcv_work);
1622 struct fcnvme_ls_rqst_w0 *w0;
1623 struct nvmefc_ls_rcv_op *lsop;
1624 unsigned long flags;
1625 bool sendrsp;
1626
1627 restart:
1628 sendrsp = true;
1629 spin_lock_irqsave(&rport->lock, flags);
1630 list_for_each_entry(lsop, &rport->ls_rcv_list, lsrcv_list) {
1631 if (lsop->handled)
1632 continue;
1633
1634 lsop->handled = true;
1635 if (rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
1636 spin_unlock_irqrestore(&rport->lock, flags);
1637 sendrsp = nvme_fc_handle_ls_rqst(lsop);
1638 } else {
1639 spin_unlock_irqrestore(&rport->lock, flags);
1640 w0 = &lsop->rqstbuf->w0;
1641 lsop->lsrsp->rsplen = nvme_fc_format_rjt(
1642 lsop->rspbuf,
1643 sizeof(*lsop->rspbuf),
1644 w0->ls_cmd,
1645 FCNVME_RJT_RC_UNAB,
1646 FCNVME_RJT_EXP_NONE, 0);
1647 }
1648 if (sendrsp)
1649 nvme_fc_xmt_ls_rsp(lsop);
1650 goto restart;
1651 }
1652 spin_unlock_irqrestore(&rport->lock, flags);
1653 }
1654
1655 static
nvme_fc_rcv_ls_req_err_msg(struct nvme_fc_lport * lport,struct fcnvme_ls_rqst_w0 * w0)1656 void nvme_fc_rcv_ls_req_err_msg(struct nvme_fc_lport *lport,
1657 struct fcnvme_ls_rqst_w0 *w0)
1658 {
1659 dev_info(lport->dev, "RCV %s LS failed: No memory\n",
1660 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1661 nvmefc_ls_names[w0->ls_cmd] : "");
1662 }
1663
1664 /**
1665 * nvme_fc_rcv_ls_req - transport entry point called by an LLDD
1666 * upon the reception of a NVME LS request.
1667 *
1668 * The nvme-fc layer will copy payload to an internal structure for
1669 * processing. As such, upon completion of the routine, the LLDD may
1670 * immediately free/reuse the LS request buffer passed in the call.
1671 *
1672 * If this routine returns error, the LLDD should abort the exchange.
1673 *
1674 * @portptr: pointer to the (registered) remote port that the LS
1675 * was received from. The remoteport is associated with
1676 * a specific localport.
1677 * @lsrsp: pointer to a nvmefc_ls_rsp response structure to be
1678 * used to reference the exchange corresponding to the LS
1679 * when issuing an ls response.
1680 * @lsreqbuf: pointer to the buffer containing the LS Request
1681 * @lsreqbuf_len: length, in bytes, of the received LS request
1682 */
1683 int
nvme_fc_rcv_ls_req(struct nvme_fc_remote_port * portptr,struct nvmefc_ls_rsp * lsrsp,void * lsreqbuf,u32 lsreqbuf_len)1684 nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
1685 struct nvmefc_ls_rsp *lsrsp,
1686 void *lsreqbuf, u32 lsreqbuf_len)
1687 {
1688 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
1689 struct nvme_fc_lport *lport = rport->lport;
1690 struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf;
1691 struct nvmefc_ls_rcv_op *lsop;
1692 unsigned long flags;
1693 int ret;
1694
1695 nvme_fc_rport_get(rport);
1696
1697 /* validate there's a routine to transmit a response */
1698 if (!lport->ops->xmt_ls_rsp) {
1699 dev_info(lport->dev,
1700 "RCV %s LS failed: no LLDD xmt_ls_rsp\n",
1701 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1702 nvmefc_ls_names[w0->ls_cmd] : "");
1703 ret = -EINVAL;
1704 goto out_put;
1705 }
1706
1707 if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) {
1708 dev_info(lport->dev,
1709 "RCV %s LS failed: payload too large\n",
1710 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1711 nvmefc_ls_names[w0->ls_cmd] : "");
1712 ret = -E2BIG;
1713 goto out_put;
1714 }
1715
1716 lsop = kzalloc(sizeof(*lsop), GFP_KERNEL);
1717 if (!lsop) {
1718 nvme_fc_rcv_ls_req_err_msg(lport, w0);
1719 ret = -ENOMEM;
1720 goto out_put;
1721 }
1722
1723 lsop->rqstbuf = kzalloc(sizeof(*lsop->rqstbuf), GFP_KERNEL);
1724 lsop->rspbuf = kzalloc(sizeof(*lsop->rspbuf), GFP_KERNEL);
1725 if (!lsop->rqstbuf || !lsop->rspbuf) {
1726 nvme_fc_rcv_ls_req_err_msg(lport, w0);
1727 ret = -ENOMEM;
1728 goto out_free;
1729 }
1730
1731 lsop->rspdma = fc_dma_map_single(lport->dev, lsop->rspbuf,
1732 sizeof(*lsop->rspbuf),
1733 DMA_TO_DEVICE);
1734 if (fc_dma_mapping_error(lport->dev, lsop->rspdma)) {
1735 dev_info(lport->dev,
1736 "RCV %s LS failed: DMA mapping failure\n",
1737 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1738 nvmefc_ls_names[w0->ls_cmd] : "");
1739 ret = -EFAULT;
1740 goto out_free;
1741 }
1742
1743 lsop->rport = rport;
1744 lsop->lsrsp = lsrsp;
1745
1746 memcpy(lsop->rqstbuf, lsreqbuf, lsreqbuf_len);
1747 lsop->rqstdatalen = lsreqbuf_len;
1748
1749 spin_lock_irqsave(&rport->lock, flags);
1750 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) {
1751 spin_unlock_irqrestore(&rport->lock, flags);
1752 ret = -ENOTCONN;
1753 goto out_unmap;
1754 }
1755 list_add_tail(&lsop->lsrcv_list, &rport->ls_rcv_list);
1756 spin_unlock_irqrestore(&rport->lock, flags);
1757
1758 schedule_work(&rport->lsrcv_work);
1759
1760 return 0;
1761
1762 out_unmap:
1763 fc_dma_unmap_single(lport->dev, lsop->rspdma,
1764 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1765 out_free:
1766 kfree(lsop->rspbuf);
1767 kfree(lsop->rqstbuf);
1768 kfree(lsop);
1769 out_put:
1770 nvme_fc_rport_put(rport);
1771 return ret;
1772 }
1773 EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req);
1774
1775
1776 /* *********************** NVME Ctrl Routines **************************** */
1777
1778 static void
__nvme_fc_exit_request(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op)1779 __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1780 struct nvme_fc_fcp_op *op)
1781 {
1782 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1783 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1784 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1785 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1786
1787 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1788 }
1789
1790 static void
nvme_fc_exit_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx)1791 nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1792 unsigned int hctx_idx)
1793 {
1794 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1795
1796 return __nvme_fc_exit_request(to_fc_ctrl(set->driver_data), op);
1797 }
1798
1799 static int
__nvme_fc_abort_op(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op)1800 __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1801 {
1802 unsigned long flags;
1803 int opstate;
1804
1805 spin_lock_irqsave(&ctrl->lock, flags);
1806 opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1807 if (opstate != FCPOP_STATE_ACTIVE)
1808 atomic_set(&op->state, opstate);
1809 else if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) {
1810 op->flags |= FCOP_FLAGS_TERMIO;
1811 ctrl->iocnt++;
1812 }
1813 spin_unlock_irqrestore(&ctrl->lock, flags);
1814
1815 if (opstate != FCPOP_STATE_ACTIVE)
1816 return -ECANCELED;
1817
1818 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1819 &ctrl->rport->remoteport,
1820 op->queue->lldd_handle,
1821 &op->fcp_req);
1822
1823 return 0;
1824 }
1825
1826 static void
nvme_fc_abort_aen_ops(struct nvme_fc_ctrl * ctrl)1827 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1828 {
1829 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
1830 int i;
1831
1832 /* ensure we've initialized the ops once */
1833 if (!(aen_op->flags & FCOP_FLAGS_AEN))
1834 return;
1835
1836 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
1837 __nvme_fc_abort_op(ctrl, aen_op);
1838 }
1839
1840 static inline void
__nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op,int opstate)1841 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1842 struct nvme_fc_fcp_op *op, int opstate)
1843 {
1844 unsigned long flags;
1845
1846 if (opstate == FCPOP_STATE_ABORTED) {
1847 spin_lock_irqsave(&ctrl->lock, flags);
1848 if (test_bit(FCCTRL_TERMIO, &ctrl->flags) &&
1849 op->flags & FCOP_FLAGS_TERMIO) {
1850 if (!--ctrl->iocnt)
1851 wake_up(&ctrl->ioabort_wait);
1852 }
1853 spin_unlock_irqrestore(&ctrl->lock, flags);
1854 }
1855 }
1856
1857 static void
nvme_fc_ctrl_ioerr_work(struct work_struct * work)1858 nvme_fc_ctrl_ioerr_work(struct work_struct *work)
1859 {
1860 struct nvme_fc_ctrl *ctrl =
1861 container_of(work, struct nvme_fc_ctrl, ioerr_work);
1862
1863 nvme_fc_error_recovery(ctrl, "transport detected io error");
1864 }
1865
1866 /*
1867 * nvme_fc_io_getuuid - Routine called to get the appid field
1868 * associated with request by the lldd
1869 * @req:IO request from nvme fc to driver
1870 * Returns: UUID if there is an appid associated with VM or
1871 * NULL if the user/libvirt has not set the appid to VM
1872 */
nvme_fc_io_getuuid(struct nvmefc_fcp_req * req)1873 char *nvme_fc_io_getuuid(struct nvmefc_fcp_req *req)
1874 {
1875 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1876 struct request *rq = op->rq;
1877
1878 if (!IS_ENABLED(CONFIG_BLK_CGROUP_FC_APPID) || !rq || !rq->bio)
1879 return NULL;
1880 return blkcg_get_fc_appid(rq->bio);
1881 }
1882 EXPORT_SYMBOL_GPL(nvme_fc_io_getuuid);
1883
1884 static void
nvme_fc_fcpio_done(struct nvmefc_fcp_req * req)1885 nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1886 {
1887 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1888 struct request *rq = op->rq;
1889 struct nvmefc_fcp_req *freq = &op->fcp_req;
1890 struct nvme_fc_ctrl *ctrl = op->ctrl;
1891 struct nvme_fc_queue *queue = op->queue;
1892 struct nvme_completion *cqe = &op->rsp_iu.cqe;
1893 struct nvme_command *sqe = &op->cmd_iu.sqe;
1894 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
1895 union nvme_result result;
1896 bool terminate_assoc = true;
1897 int opstate;
1898
1899 /*
1900 * WARNING:
1901 * The current linux implementation of a nvme controller
1902 * allocates a single tag set for all io queues and sizes
1903 * the io queues to fully hold all possible tags. Thus, the
1904 * implementation does not reference or care about the sqhd
1905 * value as it never needs to use the sqhd/sqtail pointers
1906 * for submission pacing.
1907 *
1908 * This affects the FC-NVME implementation in two ways:
1909 * 1) As the value doesn't matter, we don't need to waste
1910 * cycles extracting it from ERSPs and stamping it in the
1911 * cases where the transport fabricates CQEs on successful
1912 * completions.
1913 * 2) The FC-NVME implementation requires that delivery of
1914 * ERSP completions are to go back to the nvme layer in order
1915 * relative to the rsn, such that the sqhd value will always
1916 * be "in order" for the nvme layer. As the nvme layer in
1917 * linux doesn't care about sqhd, there's no need to return
1918 * them in order.
1919 *
1920 * Additionally:
1921 * As the core nvme layer in linux currently does not look at
1922 * every field in the cqe - in cases where the FC transport must
1923 * fabricate a CQE, the following fields will not be set as they
1924 * are not referenced:
1925 * cqe.sqid, cqe.sqhd, cqe.command_id
1926 *
1927 * Failure or error of an individual i/o, in a transport
1928 * detected fashion unrelated to the nvme completion status,
1929 * potentially cause the initiator and target sides to get out
1930 * of sync on SQ head/tail (aka outstanding io count allowed).
1931 * Per FC-NVME spec, failure of an individual command requires
1932 * the connection to be terminated, which in turn requires the
1933 * association to be terminated.
1934 */
1935
1936 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
1937
1938 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1939 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1940
1941 if (opstate == FCPOP_STATE_ABORTED)
1942 status = cpu_to_le16(NVME_SC_HOST_ABORTED_CMD << 1);
1943 else if (freq->status) {
1944 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1945 dev_info(ctrl->ctrl.device,
1946 "NVME-FC{%d}: io failed due to lldd error %d\n",
1947 ctrl->cnum, freq->status);
1948 }
1949
1950 /*
1951 * For the linux implementation, if we have an unsuccesful
1952 * status, they blk-mq layer can typically be called with the
1953 * non-zero status and the content of the cqe isn't important.
1954 */
1955 if (status)
1956 goto done;
1957
1958 /*
1959 * command completed successfully relative to the wire
1960 * protocol. However, validate anything received and
1961 * extract the status and result from the cqe (create it
1962 * where necessary).
1963 */
1964
1965 switch (freq->rcv_rsplen) {
1966
1967 case 0:
1968 case NVME_FC_SIZEOF_ZEROS_RSP:
1969 /*
1970 * No response payload or 12 bytes of payload (which
1971 * should all be zeros) are considered successful and
1972 * no payload in the CQE by the transport.
1973 */
1974 if (freq->transferred_length !=
1975 be32_to_cpu(op->cmd_iu.data_len)) {
1976 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1977 dev_info(ctrl->ctrl.device,
1978 "NVME-FC{%d}: io failed due to bad transfer "
1979 "length: %d vs expected %d\n",
1980 ctrl->cnum, freq->transferred_length,
1981 be32_to_cpu(op->cmd_iu.data_len));
1982 goto done;
1983 }
1984 result.u64 = 0;
1985 break;
1986
1987 case sizeof(struct nvme_fc_ersp_iu):
1988 /*
1989 * The ERSP IU contains a full completion with CQE.
1990 * Validate ERSP IU and look at cqe.
1991 */
1992 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1993 (freq->rcv_rsplen / 4) ||
1994 be32_to_cpu(op->rsp_iu.xfrd_len) !=
1995 freq->transferred_length ||
1996 op->rsp_iu.ersp_result ||
1997 sqe->common.command_id != cqe->command_id)) {
1998 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1999 dev_info(ctrl->ctrl.device,
2000 "NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
2001 "iu len %d, xfr len %d vs %d, status code "
2002 "%d, cmdid %d vs %d\n",
2003 ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len),
2004 be32_to_cpu(op->rsp_iu.xfrd_len),
2005 freq->transferred_length,
2006 op->rsp_iu.ersp_result,
2007 sqe->common.command_id,
2008 cqe->command_id);
2009 goto done;
2010 }
2011 result = cqe->result;
2012 status = cqe->status;
2013 break;
2014
2015 default:
2016 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2017 dev_info(ctrl->ctrl.device,
2018 "NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
2019 "len %d\n",
2020 ctrl->cnum, freq->rcv_rsplen);
2021 goto done;
2022 }
2023
2024 terminate_assoc = false;
2025
2026 done:
2027 if (op->flags & FCOP_FLAGS_AEN) {
2028 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
2029 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2030 atomic_set(&op->state, FCPOP_STATE_IDLE);
2031 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
2032 nvme_fc_ctrl_put(ctrl);
2033 goto check_error;
2034 }
2035
2036 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2037 if (!nvme_try_complete_req(rq, status, result))
2038 nvme_fc_complete_rq(rq);
2039
2040 check_error:
2041 if (terminate_assoc &&
2042 nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_RESETTING)
2043 queue_work(nvme_reset_wq, &ctrl->ioerr_work);
2044 }
2045
2046 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)2047 __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
2048 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
2049 struct request *rq, u32 rqno)
2050 {
2051 struct nvme_fcp_op_w_sgl *op_w_sgl =
2052 container_of(op, typeof(*op_w_sgl), op);
2053 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2054 int ret = 0;
2055
2056 memset(op, 0, sizeof(*op));
2057 op->fcp_req.cmdaddr = &op->cmd_iu;
2058 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
2059 op->fcp_req.rspaddr = &op->rsp_iu;
2060 op->fcp_req.rsplen = sizeof(op->rsp_iu);
2061 op->fcp_req.done = nvme_fc_fcpio_done;
2062 op->ctrl = ctrl;
2063 op->queue = queue;
2064 op->rq = rq;
2065 op->rqno = rqno;
2066
2067 cmdiu->format_id = NVME_CMD_FORMAT_ID;
2068 cmdiu->fc_id = NVME_CMD_FC_ID;
2069 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
2070 if (queue->qnum)
2071 cmdiu->rsv_cat = fccmnd_set_cat_css(0,
2072 (NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT));
2073 else
2074 cmdiu->rsv_cat = fccmnd_set_cat_admin(0);
2075
2076 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
2077 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
2078 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
2079 dev_err(ctrl->dev,
2080 "FCP Op failed - cmdiu dma mapping failed.\n");
2081 ret = -EFAULT;
2082 goto out_on_error;
2083 }
2084
2085 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
2086 &op->rsp_iu, sizeof(op->rsp_iu),
2087 DMA_FROM_DEVICE);
2088 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
2089 dev_err(ctrl->dev,
2090 "FCP Op failed - rspiu dma mapping failed.\n");
2091 ret = -EFAULT;
2092 }
2093
2094 atomic_set(&op->state, FCPOP_STATE_IDLE);
2095 out_on_error:
2096 return ret;
2097 }
2098
2099 static int
nvme_fc_init_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx,unsigned int numa_node)2100 nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
2101 unsigned int hctx_idx, unsigned int numa_node)
2102 {
2103 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
2104 struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq);
2105 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
2106 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
2107 int res;
2108
2109 res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++);
2110 if (res)
2111 return res;
2112 op->op.fcp_req.first_sgl = op->sgl;
2113 op->op.fcp_req.private = &op->priv[0];
2114 nvme_req(rq)->ctrl = &ctrl->ctrl;
2115 nvme_req(rq)->cmd = &op->op.cmd_iu.sqe;
2116 return res;
2117 }
2118
2119 static int
nvme_fc_init_aen_ops(struct nvme_fc_ctrl * ctrl)2120 nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
2121 {
2122 struct nvme_fc_fcp_op *aen_op;
2123 struct nvme_fc_cmd_iu *cmdiu;
2124 struct nvme_command *sqe;
2125 void *private = NULL;
2126 int i, ret;
2127
2128 aen_op = ctrl->aen_ops;
2129 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2130 if (ctrl->lport->ops->fcprqst_priv_sz) {
2131 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
2132 GFP_KERNEL);
2133 if (!private)
2134 return -ENOMEM;
2135 }
2136
2137 cmdiu = &aen_op->cmd_iu;
2138 sqe = &cmdiu->sqe;
2139 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
2140 aen_op, (struct request *)NULL,
2141 (NVME_AQ_BLK_MQ_DEPTH + i));
2142 if (ret) {
2143 kfree(private);
2144 return ret;
2145 }
2146
2147 aen_op->flags = FCOP_FLAGS_AEN;
2148 aen_op->fcp_req.private = private;
2149
2150 memset(sqe, 0, sizeof(*sqe));
2151 sqe->common.opcode = nvme_admin_async_event;
2152 /* Note: core layer may overwrite the sqe.command_id value */
2153 sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
2154 }
2155 return 0;
2156 }
2157
2158 static void
nvme_fc_term_aen_ops(struct nvme_fc_ctrl * ctrl)2159 nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
2160 {
2161 struct nvme_fc_fcp_op *aen_op;
2162 int i;
2163
2164 cancel_work_sync(&ctrl->ctrl.async_event_work);
2165 aen_op = ctrl->aen_ops;
2166 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2167 __nvme_fc_exit_request(ctrl, aen_op);
2168
2169 kfree(aen_op->fcp_req.private);
2170 aen_op->fcp_req.private = NULL;
2171 }
2172 }
2173
2174 static inline int
__nvme_fc_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int qidx)2175 __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int qidx)
2176 {
2177 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(data);
2178 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
2179
2180 hctx->driver_data = queue;
2181 queue->hctx = hctx;
2182 return 0;
2183 }
2184
2185 static int
nvme_fc_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)2186 nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int hctx_idx)
2187 {
2188 return __nvme_fc_init_hctx(hctx, data, hctx_idx + 1);
2189 }
2190
2191 static int
nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)2192 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2193 unsigned int hctx_idx)
2194 {
2195 return __nvme_fc_init_hctx(hctx, data, hctx_idx);
2196 }
2197
2198 static void
nvme_fc_init_queue(struct nvme_fc_ctrl * ctrl,int idx)2199 nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
2200 {
2201 struct nvme_fc_queue *queue;
2202
2203 queue = &ctrl->queues[idx];
2204 memset(queue, 0, sizeof(*queue));
2205 queue->ctrl = ctrl;
2206 queue->qnum = idx;
2207 atomic_set(&queue->csn, 0);
2208 queue->dev = ctrl->dev;
2209
2210 if (idx > 0)
2211 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
2212 else
2213 queue->cmnd_capsule_len = sizeof(struct nvme_command);
2214
2215 /*
2216 * Considered whether we should allocate buffers for all SQEs
2217 * and CQEs and dma map them - mapping their respective entries
2218 * into the request structures (kernel vm addr and dma address)
2219 * thus the driver could use the buffers/mappings directly.
2220 * It only makes sense if the LLDD would use them for its
2221 * messaging api. It's very unlikely most adapter api's would use
2222 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
2223 * structures were used instead.
2224 */
2225 }
2226
2227 /*
2228 * This routine terminates a queue at the transport level.
2229 * The transport has already ensured that all outstanding ios on
2230 * the queue have been terminated.
2231 * The transport will send a Disconnect LS request to terminate
2232 * the queue's connection. Termination of the admin queue will also
2233 * terminate the association at the target.
2234 */
2235 static void
nvme_fc_free_queue(struct nvme_fc_queue * queue)2236 nvme_fc_free_queue(struct nvme_fc_queue *queue)
2237 {
2238 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
2239 return;
2240
2241 clear_bit(NVME_FC_Q_LIVE, &queue->flags);
2242 /*
2243 * Current implementation never disconnects a single queue.
2244 * It always terminates a whole association. So there is never
2245 * a disconnect(queue) LS sent to the target.
2246 */
2247
2248 queue->connection_id = 0;
2249 atomic_set(&queue->csn, 0);
2250 }
2251
2252 static void
__nvme_fc_delete_hw_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,unsigned int qidx)2253 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
2254 struct nvme_fc_queue *queue, unsigned int qidx)
2255 {
2256 if (ctrl->lport->ops->delete_queue)
2257 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
2258 queue->lldd_handle);
2259 queue->lldd_handle = NULL;
2260 }
2261
2262 static void
nvme_fc_free_io_queues(struct nvme_fc_ctrl * ctrl)2263 nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
2264 {
2265 int i;
2266
2267 for (i = 1; i < ctrl->ctrl.queue_count; i++)
2268 nvme_fc_free_queue(&ctrl->queues[i]);
2269 }
2270
2271 static int
__nvme_fc_create_hw_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,unsigned int qidx,u16 qsize)2272 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
2273 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
2274 {
2275 int ret = 0;
2276
2277 queue->lldd_handle = NULL;
2278 if (ctrl->lport->ops->create_queue)
2279 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
2280 qidx, qsize, &queue->lldd_handle);
2281
2282 return ret;
2283 }
2284
2285 static void
nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl * ctrl)2286 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
2287 {
2288 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
2289 int i;
2290
2291 for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
2292 __nvme_fc_delete_hw_queue(ctrl, queue, i);
2293 }
2294
2295 static int
nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl * ctrl,u16 qsize)2296 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2297 {
2298 struct nvme_fc_queue *queue = &ctrl->queues[1];
2299 int i, ret;
2300
2301 for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
2302 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
2303 if (ret)
2304 goto delete_queues;
2305 }
2306
2307 return 0;
2308
2309 delete_queues:
2310 for (; i > 0; i--)
2311 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
2312 return ret;
2313 }
2314
2315 static int
nvme_fc_connect_io_queues(struct nvme_fc_ctrl * ctrl,u16 qsize)2316 nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2317 {
2318 int i, ret = 0;
2319
2320 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
2321 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
2322 (qsize / 5));
2323 if (ret)
2324 break;
2325 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
2326 if (ret)
2327 break;
2328
2329 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
2330 }
2331
2332 return ret;
2333 }
2334
2335 static void
nvme_fc_init_io_queues(struct nvme_fc_ctrl * ctrl)2336 nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
2337 {
2338 int i;
2339
2340 for (i = 1; i < ctrl->ctrl.queue_count; i++)
2341 nvme_fc_init_queue(ctrl, i);
2342 }
2343
2344 static void
nvme_fc_ctrl_free(struct kref * ref)2345 nvme_fc_ctrl_free(struct kref *ref)
2346 {
2347 struct nvme_fc_ctrl *ctrl =
2348 container_of(ref, struct nvme_fc_ctrl, ref);
2349 unsigned long flags;
2350
2351 if (ctrl->ctrl.tagset)
2352 nvme_remove_io_tag_set(&ctrl->ctrl);
2353
2354 /* remove from rport list */
2355 spin_lock_irqsave(&ctrl->rport->lock, flags);
2356 list_del(&ctrl->ctrl_list);
2357 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
2358
2359 nvme_unquiesce_admin_queue(&ctrl->ctrl);
2360 nvme_remove_admin_tag_set(&ctrl->ctrl);
2361
2362 kfree(ctrl->queues);
2363
2364 put_device(ctrl->dev);
2365 nvme_fc_rport_put(ctrl->rport);
2366
2367 ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
2368 if (ctrl->ctrl.opts)
2369 nvmf_free_options(ctrl->ctrl.opts);
2370 kfree(ctrl);
2371 }
2372
2373 static void
nvme_fc_ctrl_put(struct nvme_fc_ctrl * ctrl)2374 nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2375 {
2376 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2377 }
2378
2379 static int
nvme_fc_ctrl_get(struct nvme_fc_ctrl * ctrl)2380 nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2381 {
2382 return kref_get_unless_zero(&ctrl->ref);
2383 }
2384
2385 /*
2386 * All accesses from nvme core layer done - can now free the
2387 * controller. Called after last nvme_put_ctrl() call
2388 */
2389 static void
nvme_fc_free_ctrl(struct nvme_ctrl * nctrl)2390 nvme_fc_free_ctrl(struct nvme_ctrl *nctrl)
2391 {
2392 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2393
2394 WARN_ON(nctrl != &ctrl->ctrl);
2395
2396 nvme_fc_ctrl_put(ctrl);
2397 }
2398
2399 /*
2400 * This routine is used by the transport when it needs to find active
2401 * io on a queue that is to be terminated. The transport uses
2402 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2403 * this routine to kill them on a 1 by 1 basis.
2404 *
2405 * As FC allocates FC exchange for each io, the transport must contact
2406 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2407 * After terminating the exchange the LLDD will call the transport's
2408 * normal io done path for the request, but it will have an aborted
2409 * status. The done path will return the io request back to the block
2410 * layer with an error status.
2411 */
nvme_fc_terminate_exchange(struct request * req,void * data)2412 static bool nvme_fc_terminate_exchange(struct request *req, void *data)
2413 {
2414 struct nvme_ctrl *nctrl = data;
2415 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2416 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2417
2418 op->nreq.flags |= NVME_REQ_CANCELLED;
2419 __nvme_fc_abort_op(ctrl, op);
2420 return true;
2421 }
2422
2423 /*
2424 * This routine runs through all outstanding commands on the association
2425 * and aborts them. This routine is typically be called by the
2426 * delete_association routine. It is also called due to an error during
2427 * reconnect. In that scenario, it is most likely a command that initializes
2428 * the controller, including fabric Connect commands on io queues, that
2429 * may have timed out or failed thus the io must be killed for the connect
2430 * thread to see the error.
2431 */
2432 static void
__nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl * ctrl,bool start_queues)2433 __nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl *ctrl, bool start_queues)
2434 {
2435 int q;
2436
2437 /*
2438 * if aborting io, the queues are no longer good, mark them
2439 * all as not live.
2440 */
2441 if (ctrl->ctrl.queue_count > 1) {
2442 for (q = 1; q < ctrl->ctrl.queue_count; q++)
2443 clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[q].flags);
2444 }
2445 clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
2446
2447 /*
2448 * If io queues are present, stop them and terminate all outstanding
2449 * ios on them. As FC allocates FC exchange for each io, the
2450 * transport must contact the LLDD to terminate the exchange,
2451 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2452 * to tell us what io's are busy and invoke a transport routine
2453 * to kill them with the LLDD. After terminating the exchange
2454 * the LLDD will call the transport's normal io done path, but it
2455 * will have an aborted status. The done path will return the
2456 * io requests back to the block layer as part of normal completions
2457 * (but with error status).
2458 */
2459 if (ctrl->ctrl.queue_count > 1) {
2460 nvme_quiesce_io_queues(&ctrl->ctrl);
2461 nvme_sync_io_queues(&ctrl->ctrl);
2462 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2463 nvme_fc_terminate_exchange, &ctrl->ctrl);
2464 blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
2465 if (start_queues)
2466 nvme_unquiesce_io_queues(&ctrl->ctrl);
2467 }
2468
2469 /*
2470 * Other transports, which don't have link-level contexts bound
2471 * to sqe's, would try to gracefully shutdown the controller by
2472 * writing the registers for shutdown and polling (call
2473 * nvme_disable_ctrl()). Given a bunch of i/o was potentially
2474 * just aborted and we will wait on those contexts, and given
2475 * there was no indication of how live the controlelr is on the
2476 * link, don't send more io to create more contexts for the
2477 * shutdown. Let the controller fail via keepalive failure if
2478 * its still present.
2479 */
2480
2481 /*
2482 * clean up the admin queue. Same thing as above.
2483 */
2484 nvme_quiesce_admin_queue(&ctrl->ctrl);
2485 blk_sync_queue(ctrl->ctrl.admin_q);
2486 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2487 nvme_fc_terminate_exchange, &ctrl->ctrl);
2488 blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
2489 if (start_queues)
2490 nvme_unquiesce_admin_queue(&ctrl->ctrl);
2491 }
2492
2493 static void
nvme_fc_error_recovery(struct nvme_fc_ctrl * ctrl,char * errmsg)2494 nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
2495 {
2496 enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl);
2497
2498 /*
2499 * if an error (io timeout, etc) while (re)connecting, the remote
2500 * port requested terminating of the association (disconnect_ls)
2501 * or an error (timeout or abort) occurred on an io while creating
2502 * the controller. Abort any ios on the association and let the
2503 * create_association error path resolve things.
2504 */
2505 if (state == NVME_CTRL_CONNECTING) {
2506 __nvme_fc_abort_outstanding_ios(ctrl, true);
2507 dev_warn(ctrl->ctrl.device,
2508 "NVME-FC{%d}: transport error during (re)connect\n",
2509 ctrl->cnum);
2510 return;
2511 }
2512
2513 /* Otherwise, only proceed if in LIVE state - e.g. on first error */
2514 if (state != NVME_CTRL_LIVE)
2515 return;
2516
2517 dev_warn(ctrl->ctrl.device,
2518 "NVME-FC{%d}: transport association event: %s\n",
2519 ctrl->cnum, errmsg);
2520 dev_warn(ctrl->ctrl.device,
2521 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
2522
2523 nvme_reset_ctrl(&ctrl->ctrl);
2524 }
2525
nvme_fc_timeout(struct request * rq)2526 static enum blk_eh_timer_return nvme_fc_timeout(struct request *rq)
2527 {
2528 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2529 struct nvme_fc_ctrl *ctrl = op->ctrl;
2530 u16 qnum = op->queue->qnum;
2531 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2532 struct nvme_command *sqe = &cmdiu->sqe;
2533
2534 /*
2535 * Attempt to abort the offending command. Command completion
2536 * will detect the aborted io and will fail the connection.
2537 */
2538 dev_info(ctrl->ctrl.device,
2539 "NVME-FC{%d.%d}: io timeout: opcode %d fctype %d (%s) w10/11: "
2540 "x%08x/x%08x\n",
2541 ctrl->cnum, qnum, sqe->common.opcode, sqe->fabrics.fctype,
2542 nvme_fabrics_opcode_str(qnum, sqe),
2543 sqe->common.cdw10, sqe->common.cdw11);
2544 if (__nvme_fc_abort_op(ctrl, op))
2545 nvme_fc_error_recovery(ctrl, "io timeout abort failed");
2546
2547 /*
2548 * the io abort has been initiated. Have the reset timer
2549 * restarted and the abort completion will complete the io
2550 * shortly. Avoids a synchronous wait while the abort finishes.
2551 */
2552 return BLK_EH_RESET_TIMER;
2553 }
2554
2555 static int
nvme_fc_map_data(struct nvme_fc_ctrl * ctrl,struct request * rq,struct nvme_fc_fcp_op * op)2556 nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2557 struct nvme_fc_fcp_op *op)
2558 {
2559 struct nvmefc_fcp_req *freq = &op->fcp_req;
2560 int ret;
2561
2562 freq->sg_cnt = 0;
2563
2564 if (!blk_rq_nr_phys_segments(rq))
2565 return 0;
2566
2567 freq->sg_table.sgl = freq->first_sgl;
2568 ret = sg_alloc_table_chained(&freq->sg_table,
2569 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
2570 NVME_INLINE_SG_CNT);
2571 if (ret)
2572 return -ENOMEM;
2573
2574 op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
2575 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2576 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2577 op->nents, rq_dma_dir(rq));
2578 if (unlikely(freq->sg_cnt <= 0)) {
2579 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2580 freq->sg_cnt = 0;
2581 return -EFAULT;
2582 }
2583
2584 /*
2585 * TODO: blk_integrity_rq(rq) for DIF
2586 */
2587 return 0;
2588 }
2589
2590 static void
nvme_fc_unmap_data(struct nvme_fc_ctrl * ctrl,struct request * rq,struct nvme_fc_fcp_op * op)2591 nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2592 struct nvme_fc_fcp_op *op)
2593 {
2594 struct nvmefc_fcp_req *freq = &op->fcp_req;
2595
2596 if (!freq->sg_cnt)
2597 return;
2598
2599 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2600 rq_dma_dir(rq));
2601
2602 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2603
2604 freq->sg_cnt = 0;
2605 }
2606
2607 /*
2608 * In FC, the queue is a logical thing. At transport connect, the target
2609 * creates its "queue" and returns a handle that is to be given to the
2610 * target whenever it posts something to the corresponding SQ. When an
2611 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2612 * command contained within the SQE, an io, and assigns a FC exchange
2613 * to it. The SQE and the associated SQ handle are sent in the initial
2614 * CMD IU sents on the exchange. All transfers relative to the io occur
2615 * as part of the exchange. The CQE is the last thing for the io,
2616 * which is transferred (explicitly or implicitly) with the RSP IU
2617 * sent on the exchange. After the CQE is received, the FC exchange is
2618 * terminaed and the Exchange may be used on a different io.
2619 *
2620 * The transport to LLDD api has the transport making a request for a
2621 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2622 * resource and transfers the command. The LLDD will then process all
2623 * steps to complete the io. Upon completion, the transport done routine
2624 * is called.
2625 *
2626 * So - while the operation is outstanding to the LLDD, there is a link
2627 * level FC exchange resource that is also outstanding. This must be
2628 * considered in all cleanup operations.
2629 */
2630 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)2631 nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2632 struct nvme_fc_fcp_op *op, u32 data_len,
2633 enum nvmefc_fcp_datadir io_dir)
2634 {
2635 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2636 struct nvme_command *sqe = &cmdiu->sqe;
2637 int ret, opstate;
2638
2639 /*
2640 * before attempting to send the io, check to see if we believe
2641 * the target device is present
2642 */
2643 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2644 return BLK_STS_RESOURCE;
2645
2646 if (!nvme_fc_ctrl_get(ctrl))
2647 return BLK_STS_IOERR;
2648
2649 /* format the FC-NVME CMD IU and fcp_req */
2650 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2651 cmdiu->data_len = cpu_to_be32(data_len);
2652 switch (io_dir) {
2653 case NVMEFC_FCP_WRITE:
2654 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2655 break;
2656 case NVMEFC_FCP_READ:
2657 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2658 break;
2659 case NVMEFC_FCP_NODATA:
2660 cmdiu->flags = 0;
2661 break;
2662 }
2663 op->fcp_req.payload_length = data_len;
2664 op->fcp_req.io_dir = io_dir;
2665 op->fcp_req.transferred_length = 0;
2666 op->fcp_req.rcv_rsplen = 0;
2667 op->fcp_req.status = NVME_SC_SUCCESS;
2668 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2669
2670 /*
2671 * validate per fabric rules, set fields mandated by fabric spec
2672 * as well as those by FC-NVME spec.
2673 */
2674 WARN_ON_ONCE(sqe->common.metadata);
2675 sqe->common.flags |= NVME_CMD_SGL_METABUF;
2676
2677 /*
2678 * format SQE DPTR field per FC-NVME rules:
2679 * type=0x5 Transport SGL Data Block Descriptor
2680 * subtype=0xA Transport-specific value
2681 * address=0
2682 * length=length of the data series
2683 */
2684 sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2685 NVME_SGL_FMT_TRANSPORT_A;
2686 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2687 sqe->rw.dptr.sgl.addr = 0;
2688
2689 if (!(op->flags & FCOP_FLAGS_AEN)) {
2690 ret = nvme_fc_map_data(ctrl, op->rq, op);
2691 if (ret < 0) {
2692 nvme_cleanup_cmd(op->rq);
2693 nvme_fc_ctrl_put(ctrl);
2694 if (ret == -ENOMEM || ret == -EAGAIN)
2695 return BLK_STS_RESOURCE;
2696 return BLK_STS_IOERR;
2697 }
2698 }
2699
2700 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2701 sizeof(op->cmd_iu), DMA_TO_DEVICE);
2702
2703 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2704
2705 if (!(op->flags & FCOP_FLAGS_AEN))
2706 nvme_start_request(op->rq);
2707
2708 cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2709 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2710 &ctrl->rport->remoteport,
2711 queue->lldd_handle, &op->fcp_req);
2712
2713 if (ret) {
2714 /*
2715 * If the lld fails to send the command is there an issue with
2716 * the csn value? If the command that fails is the Connect,
2717 * no - as the connection won't be live. If it is a command
2718 * post-connect, it's possible a gap in csn may be created.
2719 * Does this matter? As Linux initiators don't send fused
2720 * commands, no. The gap would exist, but as there's nothing
2721 * that depends on csn order to be delivered on the target
2722 * side, it shouldn't hurt. It would be difficult for a
2723 * target to even detect the csn gap as it has no idea when the
2724 * cmd with the csn was supposed to arrive.
2725 */
2726 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2727 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2728
2729 if (!(op->flags & FCOP_FLAGS_AEN)) {
2730 nvme_fc_unmap_data(ctrl, op->rq, op);
2731 nvme_cleanup_cmd(op->rq);
2732 }
2733
2734 nvme_fc_ctrl_put(ctrl);
2735
2736 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2737 ret != -EBUSY)
2738 return BLK_STS_IOERR;
2739
2740 return BLK_STS_RESOURCE;
2741 }
2742
2743 return BLK_STS_OK;
2744 }
2745
2746 static blk_status_t
nvme_fc_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)2747 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2748 const struct blk_mq_queue_data *bd)
2749 {
2750 struct nvme_ns *ns = hctx->queue->queuedata;
2751 struct nvme_fc_queue *queue = hctx->driver_data;
2752 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2753 struct request *rq = bd->rq;
2754 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2755 enum nvmefc_fcp_datadir io_dir;
2756 bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2757 u32 data_len;
2758 blk_status_t ret;
2759
2760 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
2761 !nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2762 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
2763
2764 ret = nvme_setup_cmd(ns, rq);
2765 if (ret)
2766 return ret;
2767
2768 /*
2769 * nvme core doesn't quite treat the rq opaquely. Commands such
2770 * as WRITE ZEROES will return a non-zero rq payload_bytes yet
2771 * there is no actual payload to be transferred.
2772 * To get it right, key data transmission on there being 1 or
2773 * more physical segments in the sg list. If there is no
2774 * physical segments, there is no payload.
2775 */
2776 if (blk_rq_nr_phys_segments(rq)) {
2777 data_len = blk_rq_payload_bytes(rq);
2778 io_dir = ((rq_data_dir(rq) == WRITE) ?
2779 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2780 } else {
2781 data_len = 0;
2782 io_dir = NVMEFC_FCP_NODATA;
2783 }
2784
2785
2786 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2787 }
2788
2789 static void
nvme_fc_submit_async_event(struct nvme_ctrl * arg)2790 nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2791 {
2792 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2793 struct nvme_fc_fcp_op *aen_op;
2794 blk_status_t ret;
2795
2796 if (test_bit(FCCTRL_TERMIO, &ctrl->flags))
2797 return;
2798
2799 aen_op = &ctrl->aen_ops[0];
2800
2801 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2802 NVMEFC_FCP_NODATA);
2803 if (ret)
2804 dev_err(ctrl->ctrl.device,
2805 "failed async event work\n");
2806 }
2807
2808 static void
nvme_fc_complete_rq(struct request * rq)2809 nvme_fc_complete_rq(struct request *rq)
2810 {
2811 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2812 struct nvme_fc_ctrl *ctrl = op->ctrl;
2813
2814 atomic_set(&op->state, FCPOP_STATE_IDLE);
2815 op->flags &= ~FCOP_FLAGS_TERMIO;
2816
2817 nvme_fc_unmap_data(ctrl, rq, op);
2818 nvme_complete_rq(rq);
2819 nvme_fc_ctrl_put(ctrl);
2820 }
2821
nvme_fc_map_queues(struct blk_mq_tag_set * set)2822 static void nvme_fc_map_queues(struct blk_mq_tag_set *set)
2823 {
2824 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
2825 int i;
2826
2827 for (i = 0; i < set->nr_maps; i++) {
2828 struct blk_mq_queue_map *map = &set->map[i];
2829
2830 if (!map->nr_queues) {
2831 WARN_ON(i == HCTX_TYPE_DEFAULT);
2832 continue;
2833 }
2834
2835 /* Call LLDD map queue functionality if defined */
2836 if (ctrl->lport->ops->map_queues)
2837 ctrl->lport->ops->map_queues(&ctrl->lport->localport,
2838 map);
2839 else
2840 blk_mq_map_queues(map);
2841 }
2842 }
2843
2844 static const struct blk_mq_ops nvme_fc_mq_ops = {
2845 .queue_rq = nvme_fc_queue_rq,
2846 .complete = nvme_fc_complete_rq,
2847 .init_request = nvme_fc_init_request,
2848 .exit_request = nvme_fc_exit_request,
2849 .init_hctx = nvme_fc_init_hctx,
2850 .timeout = nvme_fc_timeout,
2851 .map_queues = nvme_fc_map_queues,
2852 };
2853
2854 static int
nvme_fc_create_io_queues(struct nvme_fc_ctrl * ctrl)2855 nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2856 {
2857 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2858 unsigned int nr_io_queues;
2859 int ret;
2860
2861 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2862 ctrl->lport->ops->max_hw_queues);
2863 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2864 if (ret) {
2865 dev_info(ctrl->ctrl.device,
2866 "set_queue_count failed: %d\n", ret);
2867 return ret;
2868 }
2869
2870 ctrl->ctrl.queue_count = nr_io_queues + 1;
2871 if (!nr_io_queues)
2872 return 0;
2873
2874 nvme_fc_init_io_queues(ctrl);
2875
2876 ret = nvme_alloc_io_tag_set(&ctrl->ctrl, &ctrl->tag_set,
2877 &nvme_fc_mq_ops, 1,
2878 struct_size_t(struct nvme_fcp_op_w_sgl, priv,
2879 ctrl->lport->ops->fcprqst_priv_sz));
2880 if (ret)
2881 return ret;
2882
2883 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2884 if (ret)
2885 goto out_cleanup_tagset;
2886
2887 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2888 if (ret)
2889 goto out_delete_hw_queues;
2890
2891 ctrl->ioq_live = true;
2892
2893 return 0;
2894
2895 out_delete_hw_queues:
2896 nvme_fc_delete_hw_io_queues(ctrl);
2897 out_cleanup_tagset:
2898 nvme_remove_io_tag_set(&ctrl->ctrl);
2899 nvme_fc_free_io_queues(ctrl);
2900
2901 /* force put free routine to ignore io queues */
2902 ctrl->ctrl.tagset = NULL;
2903
2904 return ret;
2905 }
2906
2907 static int
nvme_fc_recreate_io_queues(struct nvme_fc_ctrl * ctrl)2908 nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
2909 {
2910 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2911 u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
2912 unsigned int nr_io_queues;
2913 int ret;
2914
2915 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2916 ctrl->lport->ops->max_hw_queues);
2917 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2918 if (ret) {
2919 dev_info(ctrl->ctrl.device,
2920 "set_queue_count failed: %d\n", ret);
2921 return ret;
2922 }
2923
2924 if (!nr_io_queues && prior_ioq_cnt) {
2925 dev_info(ctrl->ctrl.device,
2926 "Fail Reconnect: At least 1 io queue "
2927 "required (was %d)\n", prior_ioq_cnt);
2928 return -ENOSPC;
2929 }
2930
2931 ctrl->ctrl.queue_count = nr_io_queues + 1;
2932 /* check for io queues existing */
2933 if (ctrl->ctrl.queue_count == 1)
2934 return 0;
2935
2936 if (prior_ioq_cnt != nr_io_queues) {
2937 dev_info(ctrl->ctrl.device,
2938 "reconnect: revising io queue count from %d to %d\n",
2939 prior_ioq_cnt, nr_io_queues);
2940 blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2941 }
2942
2943 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2944 if (ret)
2945 goto out_free_io_queues;
2946
2947 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2948 if (ret)
2949 goto out_delete_hw_queues;
2950
2951 return 0;
2952
2953 out_delete_hw_queues:
2954 nvme_fc_delete_hw_io_queues(ctrl);
2955 out_free_io_queues:
2956 nvme_fc_free_io_queues(ctrl);
2957 return ret;
2958 }
2959
2960 static void
nvme_fc_rport_active_on_lport(struct nvme_fc_rport * rport)2961 nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
2962 {
2963 struct nvme_fc_lport *lport = rport->lport;
2964
2965 atomic_inc(&lport->act_rport_cnt);
2966 }
2967
2968 static void
nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport * rport)2969 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
2970 {
2971 struct nvme_fc_lport *lport = rport->lport;
2972 u32 cnt;
2973
2974 cnt = atomic_dec_return(&lport->act_rport_cnt);
2975 if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
2976 lport->ops->localport_delete(&lport->localport);
2977 }
2978
2979 static int
nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl * ctrl)2980 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
2981 {
2982 struct nvme_fc_rport *rport = ctrl->rport;
2983 u32 cnt;
2984
2985 if (test_and_set_bit(ASSOC_ACTIVE, &ctrl->flags))
2986 return 1;
2987
2988 cnt = atomic_inc_return(&rport->act_ctrl_cnt);
2989 if (cnt == 1)
2990 nvme_fc_rport_active_on_lport(rport);
2991
2992 return 0;
2993 }
2994
2995 static int
nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl * ctrl)2996 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
2997 {
2998 struct nvme_fc_rport *rport = ctrl->rport;
2999 struct nvme_fc_lport *lport = rport->lport;
3000 u32 cnt;
3001
3002 /* clearing of ctrl->flags ASSOC_ACTIVE bit is in association delete */
3003
3004 cnt = atomic_dec_return(&rport->act_ctrl_cnt);
3005 if (cnt == 0) {
3006 if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
3007 lport->ops->remoteport_delete(&rport->remoteport);
3008 nvme_fc_rport_inactive_on_lport(rport);
3009 }
3010
3011 return 0;
3012 }
3013
3014 /*
3015 * This routine restarts the controller on the host side, and
3016 * on the link side, recreates the controller association.
3017 */
3018 static int
nvme_fc_create_association(struct nvme_fc_ctrl * ctrl)3019 nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
3020 {
3021 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
3022 struct nvmefc_ls_rcv_op *disls = NULL;
3023 unsigned long flags;
3024 int ret;
3025
3026 ++ctrl->ctrl.nr_reconnects;
3027
3028 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3029 return -ENODEV;
3030
3031 if (nvme_fc_ctlr_active_on_rport(ctrl))
3032 return -ENOTUNIQ;
3033
3034 dev_info(ctrl->ctrl.device,
3035 "NVME-FC{%d}: create association : host wwpn 0x%016llx "
3036 " rport wwpn 0x%016llx: NQN \"%s\"\n",
3037 ctrl->cnum, ctrl->lport->localport.port_name,
3038 ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
3039
3040 clear_bit(ASSOC_FAILED, &ctrl->flags);
3041
3042 /*
3043 * Create the admin queue
3044 */
3045
3046 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
3047 NVME_AQ_DEPTH);
3048 if (ret)
3049 goto out_free_queue;
3050
3051 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
3052 NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
3053 if (ret)
3054 goto out_delete_hw_queue;
3055
3056 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
3057 if (ret)
3058 goto out_disconnect_admin_queue;
3059
3060 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
3061
3062 /*
3063 * Check controller capabilities
3064 *
3065 * todo:- add code to check if ctrl attributes changed from
3066 * prior connection values
3067 */
3068
3069 ret = nvme_enable_ctrl(&ctrl->ctrl);
3070 if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
3071 ret = -EIO;
3072 if (ret)
3073 goto out_disconnect_admin_queue;
3074
3075 ctrl->ctrl.max_segments = ctrl->lport->ops->max_sgl_segments;
3076 ctrl->ctrl.max_hw_sectors = ctrl->ctrl.max_segments <<
3077 (ilog2(SZ_4K) - 9);
3078
3079 nvme_unquiesce_admin_queue(&ctrl->ctrl);
3080
3081 ret = nvme_init_ctrl_finish(&ctrl->ctrl, false);
3082 if (ret)
3083 goto out_disconnect_admin_queue;
3084 if (test_bit(ASSOC_FAILED, &ctrl->flags)) {
3085 ret = -EIO;
3086 goto out_stop_keep_alive;
3087 }
3088 /* sanity checks */
3089
3090 /* FC-NVME does not have other data in the capsule */
3091 if (ctrl->ctrl.icdoff) {
3092 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
3093 ctrl->ctrl.icdoff);
3094 ret = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
3095 goto out_stop_keep_alive;
3096 }
3097
3098 /* FC-NVME supports normal SGL Data Block Descriptors */
3099 if (!nvme_ctrl_sgl_supported(&ctrl->ctrl)) {
3100 dev_err(ctrl->ctrl.device,
3101 "Mandatory sgls are not supported!\n");
3102 ret = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
3103 goto out_stop_keep_alive;
3104 }
3105
3106 if (opts->queue_size > ctrl->ctrl.maxcmd) {
3107 /* warn if maxcmd is lower than queue_size */
3108 dev_warn(ctrl->ctrl.device,
3109 "queue_size %zu > ctrl maxcmd %u, reducing "
3110 "to maxcmd\n",
3111 opts->queue_size, ctrl->ctrl.maxcmd);
3112 opts->queue_size = ctrl->ctrl.maxcmd;
3113 ctrl->ctrl.sqsize = opts->queue_size - 1;
3114 }
3115
3116 ret = nvme_fc_init_aen_ops(ctrl);
3117 if (ret)
3118 goto out_term_aen_ops;
3119
3120 /*
3121 * Create the io queues
3122 */
3123
3124 if (ctrl->ctrl.queue_count > 1) {
3125 if (!ctrl->ioq_live)
3126 ret = nvme_fc_create_io_queues(ctrl);
3127 else
3128 ret = nvme_fc_recreate_io_queues(ctrl);
3129 }
3130 if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
3131 ret = -EIO;
3132 if (ret)
3133 goto out_term_aen_ops;
3134
3135 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE)) {
3136 ret = -EIO;
3137 goto out_term_aen_ops;
3138 }
3139
3140 ctrl->ctrl.nr_reconnects = 0;
3141 nvme_start_ctrl(&ctrl->ctrl);
3142
3143 return 0; /* Success */
3144
3145 out_term_aen_ops:
3146 nvme_fc_term_aen_ops(ctrl);
3147 out_stop_keep_alive:
3148 nvme_stop_keep_alive(&ctrl->ctrl);
3149 out_disconnect_admin_queue:
3150 dev_warn(ctrl->ctrl.device,
3151 "NVME-FC{%d}: create_assoc failed, assoc_id %llx ret %d\n",
3152 ctrl->cnum, ctrl->association_id, ret);
3153 /* send a Disconnect(association) LS to fc-nvme target */
3154 nvme_fc_xmt_disconnect_assoc(ctrl);
3155 spin_lock_irqsave(&ctrl->lock, flags);
3156 ctrl->association_id = 0;
3157 disls = ctrl->rcv_disconn;
3158 ctrl->rcv_disconn = NULL;
3159 spin_unlock_irqrestore(&ctrl->lock, flags);
3160 if (disls)
3161 nvme_fc_xmt_ls_rsp(disls);
3162 out_delete_hw_queue:
3163 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3164 out_free_queue:
3165 nvme_fc_free_queue(&ctrl->queues[0]);
3166 clear_bit(ASSOC_ACTIVE, &ctrl->flags);
3167 nvme_fc_ctlr_inactive_on_rport(ctrl);
3168
3169 return ret;
3170 }
3171
3172
3173 /*
3174 * This routine stops operation of the controller on the host side.
3175 * On the host os stack side: Admin and IO queues are stopped,
3176 * outstanding ios on them terminated via FC ABTS.
3177 * On the link side: the association is terminated.
3178 */
3179 static void
nvme_fc_delete_association(struct nvme_fc_ctrl * ctrl)3180 nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
3181 {
3182 struct nvmefc_ls_rcv_op *disls = NULL;
3183 unsigned long flags;
3184
3185 if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
3186 return;
3187
3188 spin_lock_irqsave(&ctrl->lock, flags);
3189 set_bit(FCCTRL_TERMIO, &ctrl->flags);
3190 ctrl->iocnt = 0;
3191 spin_unlock_irqrestore(&ctrl->lock, flags);
3192
3193 __nvme_fc_abort_outstanding_ios(ctrl, false);
3194
3195 /* kill the aens as they are a separate path */
3196 nvme_fc_abort_aen_ops(ctrl);
3197
3198 /* wait for all io that had to be aborted */
3199 spin_lock_irq(&ctrl->lock);
3200 wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
3201 clear_bit(FCCTRL_TERMIO, &ctrl->flags);
3202 spin_unlock_irq(&ctrl->lock);
3203
3204 nvme_fc_term_aen_ops(ctrl);
3205
3206 /*
3207 * send a Disconnect(association) LS to fc-nvme target
3208 * Note: could have been sent at top of process, but
3209 * cleaner on link traffic if after the aborts complete.
3210 * Note: if association doesn't exist, association_id will be 0
3211 */
3212 if (ctrl->association_id)
3213 nvme_fc_xmt_disconnect_assoc(ctrl);
3214
3215 spin_lock_irqsave(&ctrl->lock, flags);
3216 ctrl->association_id = 0;
3217 disls = ctrl->rcv_disconn;
3218 ctrl->rcv_disconn = NULL;
3219 spin_unlock_irqrestore(&ctrl->lock, flags);
3220 if (disls)
3221 /*
3222 * if a Disconnect Request was waiting for a response, send
3223 * now that all ABTS's have been issued (and are complete).
3224 */
3225 nvme_fc_xmt_ls_rsp(disls);
3226
3227 if (ctrl->ctrl.tagset) {
3228 nvme_fc_delete_hw_io_queues(ctrl);
3229 nvme_fc_free_io_queues(ctrl);
3230 }
3231
3232 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3233 nvme_fc_free_queue(&ctrl->queues[0]);
3234
3235 /* re-enable the admin_q so anything new can fast fail */
3236 nvme_unquiesce_admin_queue(&ctrl->ctrl);
3237
3238 /* resume the io queues so that things will fast fail */
3239 nvme_unquiesce_io_queues(&ctrl->ctrl);
3240
3241 nvme_fc_ctlr_inactive_on_rport(ctrl);
3242 }
3243
3244 static void
nvme_fc_delete_ctrl(struct nvme_ctrl * nctrl)3245 nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
3246 {
3247 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
3248
3249 cancel_work_sync(&ctrl->ioerr_work);
3250 cancel_delayed_work_sync(&ctrl->connect_work);
3251 /*
3252 * kill the association on the link side. this will block
3253 * waiting for io to terminate
3254 */
3255 nvme_fc_delete_association(ctrl);
3256 }
3257
3258 static void
nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl * ctrl,int status)3259 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
3260 {
3261 struct nvme_fc_rport *rport = ctrl->rport;
3262 struct nvme_fc_remote_port *portptr = &rport->remoteport;
3263 unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
3264 bool recon = true;
3265
3266 if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_CONNECTING)
3267 return;
3268
3269 if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3270 dev_info(ctrl->ctrl.device,
3271 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
3272 ctrl->cnum, status);
3273 } else if (time_after_eq(jiffies, rport->dev_loss_end))
3274 recon = false;
3275
3276 if (recon && nvmf_should_reconnect(&ctrl->ctrl, status)) {
3277 if (portptr->port_state == FC_OBJSTATE_ONLINE)
3278 dev_info(ctrl->ctrl.device,
3279 "NVME-FC{%d}: Reconnect attempt in %ld "
3280 "seconds\n",
3281 ctrl->cnum, recon_delay / HZ);
3282 else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
3283 recon_delay = rport->dev_loss_end - jiffies;
3284
3285 queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
3286 } else {
3287 if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3288 if (status > 0 && (status & NVME_STATUS_DNR))
3289 dev_warn(ctrl->ctrl.device,
3290 "NVME-FC{%d}: reconnect failure\n",
3291 ctrl->cnum);
3292 else
3293 dev_warn(ctrl->ctrl.device,
3294 "NVME-FC{%d}: Max reconnect attempts "
3295 "(%d) reached.\n",
3296 ctrl->cnum, ctrl->ctrl.nr_reconnects);
3297 } else
3298 dev_warn(ctrl->ctrl.device,
3299 "NVME-FC{%d}: dev_loss_tmo (%d) expired "
3300 "while waiting for remoteport connectivity.\n",
3301 ctrl->cnum, min_t(int, portptr->dev_loss_tmo,
3302 (ctrl->ctrl.opts->max_reconnects *
3303 ctrl->ctrl.opts->reconnect_delay)));
3304 WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
3305 }
3306 }
3307
3308 static void
nvme_fc_reset_ctrl_work(struct work_struct * work)3309 nvme_fc_reset_ctrl_work(struct work_struct *work)
3310 {
3311 struct nvme_fc_ctrl *ctrl =
3312 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
3313
3314 nvme_stop_ctrl(&ctrl->ctrl);
3315
3316 /* will block will waiting for io to terminate */
3317 nvme_fc_delete_association(ctrl);
3318
3319 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
3320 dev_err(ctrl->ctrl.device,
3321 "NVME-FC{%d}: error_recovery: Couldn't change state "
3322 "to CONNECTING\n", ctrl->cnum);
3323
3324 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
3325 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3326 dev_err(ctrl->ctrl.device,
3327 "NVME-FC{%d}: failed to schedule connect "
3328 "after reset\n", ctrl->cnum);
3329 } else {
3330 flush_delayed_work(&ctrl->connect_work);
3331 }
3332 } else {
3333 nvme_fc_reconnect_or_delete(ctrl, -ENOTCONN);
3334 }
3335 }
3336
3337
3338 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
3339 .name = "fc",
3340 .module = THIS_MODULE,
3341 .flags = NVME_F_FABRICS,
3342 .reg_read32 = nvmf_reg_read32,
3343 .reg_read64 = nvmf_reg_read64,
3344 .reg_write32 = nvmf_reg_write32,
3345 .subsystem_reset = nvmf_subsystem_reset,
3346 .free_ctrl = nvme_fc_free_ctrl,
3347 .submit_async_event = nvme_fc_submit_async_event,
3348 .delete_ctrl = nvme_fc_delete_ctrl,
3349 .get_address = nvmf_get_address,
3350 };
3351
3352 static void
nvme_fc_connect_ctrl_work(struct work_struct * work)3353 nvme_fc_connect_ctrl_work(struct work_struct *work)
3354 {
3355 int ret;
3356
3357 struct nvme_fc_ctrl *ctrl =
3358 container_of(to_delayed_work(work),
3359 struct nvme_fc_ctrl, connect_work);
3360
3361 ret = nvme_fc_create_association(ctrl);
3362 if (ret)
3363 nvme_fc_reconnect_or_delete(ctrl, ret);
3364 else
3365 dev_info(ctrl->ctrl.device,
3366 "NVME-FC{%d}: controller connect complete\n",
3367 ctrl->cnum);
3368 }
3369
3370
3371 static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
3372 .queue_rq = nvme_fc_queue_rq,
3373 .complete = nvme_fc_complete_rq,
3374 .init_request = nvme_fc_init_request,
3375 .exit_request = nvme_fc_exit_request,
3376 .init_hctx = nvme_fc_init_admin_hctx,
3377 .timeout = nvme_fc_timeout,
3378 };
3379
3380
3381 /*
3382 * Fails a controller request if it matches an existing controller
3383 * (association) with the same tuple:
3384 * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
3385 *
3386 * The ports don't need to be compared as they are intrinsically
3387 * already matched by the port pointers supplied.
3388 */
3389 static bool
nvme_fc_existing_controller(struct nvme_fc_rport * rport,struct nvmf_ctrl_options * opts)3390 nvme_fc_existing_controller(struct nvme_fc_rport *rport,
3391 struct nvmf_ctrl_options *opts)
3392 {
3393 struct nvme_fc_ctrl *ctrl;
3394 unsigned long flags;
3395 bool found = false;
3396
3397 spin_lock_irqsave(&rport->lock, flags);
3398 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3399 found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
3400 if (found)
3401 break;
3402 }
3403 spin_unlock_irqrestore(&rport->lock, flags);
3404
3405 return found;
3406 }
3407
3408 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)3409 nvme_fc_alloc_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3410 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3411 {
3412 struct nvme_fc_ctrl *ctrl;
3413 int ret, idx, ctrl_loss_tmo;
3414
3415 if (!(rport->remoteport.port_role &
3416 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
3417 ret = -EBADR;
3418 goto out_fail;
3419 }
3420
3421 if (!opts->duplicate_connect &&
3422 nvme_fc_existing_controller(rport, opts)) {
3423 ret = -EALREADY;
3424 goto out_fail;
3425 }
3426
3427 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3428 if (!ctrl) {
3429 ret = -ENOMEM;
3430 goto out_fail;
3431 }
3432
3433 idx = ida_alloc(&nvme_fc_ctrl_cnt, GFP_KERNEL);
3434 if (idx < 0) {
3435 ret = -ENOSPC;
3436 goto out_free_ctrl;
3437 }
3438
3439 /*
3440 * if ctrl_loss_tmo is being enforced and the default reconnect delay
3441 * is being used, change to a shorter reconnect delay for FC.
3442 */
3443 if (opts->max_reconnects != -1 &&
3444 opts->reconnect_delay == NVMF_DEF_RECONNECT_DELAY &&
3445 opts->reconnect_delay > NVME_FC_DEFAULT_RECONNECT_TMO) {
3446 ctrl_loss_tmo = opts->max_reconnects * opts->reconnect_delay;
3447 opts->reconnect_delay = NVME_FC_DEFAULT_RECONNECT_TMO;
3448 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
3449 opts->reconnect_delay);
3450 }
3451
3452 ctrl->ctrl.opts = opts;
3453 ctrl->ctrl.nr_reconnects = 0;
3454 INIT_LIST_HEAD(&ctrl->ctrl_list);
3455 ctrl->lport = lport;
3456 ctrl->rport = rport;
3457 ctrl->dev = lport->dev;
3458 ctrl->cnum = idx;
3459 ctrl->ioq_live = false;
3460 init_waitqueue_head(&ctrl->ioabort_wait);
3461
3462 get_device(ctrl->dev);
3463 kref_init(&ctrl->ref);
3464
3465 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
3466 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
3467 INIT_WORK(&ctrl->ioerr_work, nvme_fc_ctrl_ioerr_work);
3468 spin_lock_init(&ctrl->lock);
3469
3470 /* io queue count */
3471 ctrl->ctrl.queue_count = min_t(unsigned int,
3472 opts->nr_io_queues,
3473 lport->ops->max_hw_queues);
3474 ctrl->ctrl.queue_count++; /* +1 for admin queue */
3475
3476 ctrl->ctrl.sqsize = opts->queue_size - 1;
3477 ctrl->ctrl.kato = opts->kato;
3478 ctrl->ctrl.cntlid = 0xffff;
3479
3480 ret = -ENOMEM;
3481 ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3482 sizeof(struct nvme_fc_queue), GFP_KERNEL);
3483 if (!ctrl->queues)
3484 goto out_free_ida;
3485
3486 nvme_fc_init_queue(ctrl, 0);
3487
3488 /*
3489 * Would have been nice to init io queues tag set as well.
3490 * However, we require interaction from the controller
3491 * for max io queue count before we can do so.
3492 * Defer this to the connect path.
3493 */
3494
3495 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3496 if (ret)
3497 goto out_free_queues;
3498 if (lport->dev)
3499 ctrl->ctrl.numa_node = dev_to_node(lport->dev);
3500
3501 return ctrl;
3502
3503 out_free_queues:
3504 kfree(ctrl->queues);
3505 out_free_ida:
3506 put_device(ctrl->dev);
3507 ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
3508 out_free_ctrl:
3509 kfree(ctrl);
3510 out_fail:
3511 /* exit via here doesn't follow ctlr ref points */
3512 return ERR_PTR(ret);
3513 }
3514
3515 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)3516 nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3517 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3518 {
3519 struct nvme_fc_ctrl *ctrl;
3520 unsigned long flags;
3521 int ret;
3522
3523 ctrl = nvme_fc_alloc_ctrl(dev, opts, lport, rport);
3524 if (IS_ERR(ctrl))
3525 return ERR_CAST(ctrl);
3526
3527 ret = nvme_add_ctrl(&ctrl->ctrl);
3528 if (ret)
3529 goto out_put_ctrl;
3530
3531 ret = nvme_alloc_admin_tag_set(&ctrl->ctrl, &ctrl->admin_tag_set,
3532 &nvme_fc_admin_mq_ops,
3533 struct_size_t(struct nvme_fcp_op_w_sgl, priv,
3534 ctrl->lport->ops->fcprqst_priv_sz));
3535 if (ret)
3536 goto fail_ctrl;
3537
3538 spin_lock_irqsave(&rport->lock, flags);
3539 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3540 spin_unlock_irqrestore(&rport->lock, flags);
3541
3542 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
3543 dev_err(ctrl->ctrl.device,
3544 "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
3545 goto fail_ctrl;
3546 }
3547
3548 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3549 dev_err(ctrl->ctrl.device,
3550 "NVME-FC{%d}: failed to schedule initial connect\n",
3551 ctrl->cnum);
3552 goto fail_ctrl;
3553 }
3554
3555 flush_delayed_work(&ctrl->connect_work);
3556
3557 dev_info(ctrl->ctrl.device,
3558 "NVME-FC{%d}: new ctrl: NQN \"%s\", hostnqn: %s\n",
3559 ctrl->cnum, nvmf_ctrl_subsysnqn(&ctrl->ctrl), opts->host->nqn);
3560
3561 return &ctrl->ctrl;
3562
3563 fail_ctrl:
3564 nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3565 cancel_work_sync(&ctrl->ioerr_work);
3566 cancel_work_sync(&ctrl->ctrl.reset_work);
3567 cancel_delayed_work_sync(&ctrl->connect_work);
3568
3569 ctrl->ctrl.opts = NULL;
3570
3571 /* initiate nvme ctrl ref counting teardown */
3572 nvme_uninit_ctrl(&ctrl->ctrl);
3573
3574 out_put_ctrl:
3575 /* Remove core ctrl ref. */
3576 nvme_put_ctrl(&ctrl->ctrl);
3577
3578 /* as we're past the point where we transition to the ref
3579 * counting teardown path, if we return a bad pointer here,
3580 * the calling routine, thinking it's prior to the
3581 * transition, will do an rport put. Since the teardown
3582 * path also does a rport put, we do an extra get here to
3583 * so proper order/teardown happens.
3584 */
3585 nvme_fc_rport_get(rport);
3586
3587 return ERR_PTR(-EIO);
3588 }
3589
3590 struct nvmet_fc_traddr {
3591 u64 nn;
3592 u64 pn;
3593 };
3594
3595 static int
__nvme_fc_parse_u64(substring_t * sstr,u64 * val)3596 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3597 {
3598 u64 token64;
3599
3600 if (match_u64(sstr, &token64))
3601 return -EINVAL;
3602 *val = token64;
3603
3604 return 0;
3605 }
3606
3607 /*
3608 * This routine validates and extracts the WWN's from the TRADDR string.
3609 * As kernel parsers need the 0x to determine number base, universally
3610 * build string to parse with 0x prefix before parsing name strings.
3611 */
3612 static int
nvme_fc_parse_traddr(struct nvmet_fc_traddr * traddr,char * buf,size_t blen)3613 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
3614 {
3615 char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
3616 substring_t wwn = { name, &name[sizeof(name)-1] };
3617 int nnoffset, pnoffset;
3618
3619 /* validate if string is one of the 2 allowed formats */
3620 if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
3621 !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
3622 !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
3623 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
3624 nnoffset = NVME_FC_TRADDR_OXNNLEN;
3625 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
3626 NVME_FC_TRADDR_OXNNLEN;
3627 } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
3628 !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
3629 !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
3630 "pn-", NVME_FC_TRADDR_NNLEN))) {
3631 nnoffset = NVME_FC_TRADDR_NNLEN;
3632 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
3633 } else
3634 goto out_einval;
3635
3636 name[0] = '0';
3637 name[1] = 'x';
3638 name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
3639
3640 memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3641 if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
3642 goto out_einval;
3643
3644 memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3645 if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
3646 goto out_einval;
3647
3648 return 0;
3649
3650 out_einval:
3651 pr_warn("%s: bad traddr string\n", __func__);
3652 return -EINVAL;
3653 }
3654
3655 static struct nvme_ctrl *
nvme_fc_create_ctrl(struct device * dev,struct nvmf_ctrl_options * opts)3656 nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3657 {
3658 struct nvme_fc_lport *lport;
3659 struct nvme_fc_rport *rport;
3660 struct nvme_ctrl *ctrl;
3661 struct nvmet_fc_traddr laddr = { 0L, 0L };
3662 struct nvmet_fc_traddr raddr = { 0L, 0L };
3663 unsigned long flags;
3664 int ret;
3665
3666 ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3667 if (ret || !raddr.nn || !raddr.pn)
3668 return ERR_PTR(-EINVAL);
3669
3670 ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3671 if (ret || !laddr.nn || !laddr.pn)
3672 return ERR_PTR(-EINVAL);
3673
3674 /* find the host and remote ports to connect together */
3675 spin_lock_irqsave(&nvme_fc_lock, flags);
3676 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3677 if (lport->localport.node_name != laddr.nn ||
3678 lport->localport.port_name != laddr.pn ||
3679 lport->localport.port_state != FC_OBJSTATE_ONLINE)
3680 continue;
3681
3682 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3683 if (rport->remoteport.node_name != raddr.nn ||
3684 rport->remoteport.port_name != raddr.pn ||
3685 rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3686 continue;
3687
3688 /* if fail to get reference fall through. Will error */
3689 if (!nvme_fc_rport_get(rport))
3690 break;
3691
3692 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3693
3694 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3695 if (IS_ERR(ctrl))
3696 nvme_fc_rport_put(rport);
3697 return ctrl;
3698 }
3699 }
3700 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3701
3702 pr_warn("%s: %s - %s combination not found\n",
3703 __func__, opts->traddr, opts->host_traddr);
3704 return ERR_PTR(-ENOENT);
3705 }
3706
3707
3708 static struct nvmf_transport_ops nvme_fc_transport = {
3709 .name = "fc",
3710 .module = THIS_MODULE,
3711 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
3712 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3713 .create_ctrl = nvme_fc_create_ctrl,
3714 };
3715
3716 /* Arbitrary successive failures max. With lots of subsystems could be high */
3717 #define DISCOVERY_MAX_FAIL 20
3718
nvme_fc_nvme_discovery_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3719 static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
3720 struct device_attribute *attr, const char *buf, size_t count)
3721 {
3722 unsigned long flags;
3723 LIST_HEAD(local_disc_list);
3724 struct nvme_fc_lport *lport;
3725 struct nvme_fc_rport *rport;
3726 int failcnt = 0;
3727
3728 spin_lock_irqsave(&nvme_fc_lock, flags);
3729 restart:
3730 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3731 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3732 if (!nvme_fc_lport_get(lport))
3733 continue;
3734 if (!nvme_fc_rport_get(rport)) {
3735 /*
3736 * This is a temporary condition. Upon restart
3737 * this rport will be gone from the list.
3738 *
3739 * Revert the lport put and retry. Anything
3740 * added to the list already will be skipped (as
3741 * they are no longer list_empty). Loops should
3742 * resume at rports that were not yet seen.
3743 */
3744 nvme_fc_lport_put(lport);
3745
3746 if (failcnt++ < DISCOVERY_MAX_FAIL)
3747 goto restart;
3748
3749 pr_err("nvme_discovery: too many reference "
3750 "failures\n");
3751 goto process_local_list;
3752 }
3753 if (list_empty(&rport->disc_list))
3754 list_add_tail(&rport->disc_list,
3755 &local_disc_list);
3756 }
3757 }
3758
3759 process_local_list:
3760 while (!list_empty(&local_disc_list)) {
3761 rport = list_first_entry(&local_disc_list,
3762 struct nvme_fc_rport, disc_list);
3763 list_del_init(&rport->disc_list);
3764 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3765
3766 lport = rport->lport;
3767 /* signal discovery. Won't hurt if it repeats */
3768 nvme_fc_signal_discovery_scan(lport, rport);
3769 nvme_fc_rport_put(rport);
3770 nvme_fc_lport_put(lport);
3771
3772 spin_lock_irqsave(&nvme_fc_lock, flags);
3773 }
3774 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3775
3776 return count;
3777 }
3778
3779 static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
3780
3781 #ifdef CONFIG_BLK_CGROUP_FC_APPID
3782 /* Parse the cgroup id from a buf and return the length of cgrpid */
fc_parse_cgrpid(const char * buf,u64 * id)3783 static int fc_parse_cgrpid(const char *buf, u64 *id)
3784 {
3785 char cgrp_id[16+1];
3786 int cgrpid_len, j;
3787
3788 memset(cgrp_id, 0x0, sizeof(cgrp_id));
3789 for (cgrpid_len = 0, j = 0; cgrpid_len < 17; cgrpid_len++) {
3790 if (buf[cgrpid_len] != ':')
3791 cgrp_id[cgrpid_len] = buf[cgrpid_len];
3792 else {
3793 j = 1;
3794 break;
3795 }
3796 }
3797 if (!j)
3798 return -EINVAL;
3799 if (kstrtou64(cgrp_id, 16, id) < 0)
3800 return -EINVAL;
3801 return cgrpid_len;
3802 }
3803
3804 /*
3805 * Parse and update the appid in the blkcg associated with the cgroupid.
3806 */
fc_appid_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3807 static ssize_t fc_appid_store(struct device *dev,
3808 struct device_attribute *attr, const char *buf, size_t count)
3809 {
3810 size_t orig_count = count;
3811 u64 cgrp_id;
3812 int appid_len = 0;
3813 int cgrpid_len = 0;
3814 char app_id[FC_APPID_LEN];
3815 int ret = 0;
3816
3817 if (buf[count-1] == '\n')
3818 count--;
3819
3820 if ((count > (16+1+FC_APPID_LEN)) || (!strchr(buf, ':')))
3821 return -EINVAL;
3822
3823 cgrpid_len = fc_parse_cgrpid(buf, &cgrp_id);
3824 if (cgrpid_len < 0)
3825 return -EINVAL;
3826 appid_len = count - cgrpid_len - 1;
3827 if (appid_len > FC_APPID_LEN)
3828 return -EINVAL;
3829
3830 memset(app_id, 0x0, sizeof(app_id));
3831 memcpy(app_id, &buf[cgrpid_len+1], appid_len);
3832 ret = blkcg_set_fc_appid(app_id, cgrp_id, sizeof(app_id));
3833 if (ret < 0)
3834 return ret;
3835 return orig_count;
3836 }
3837 static DEVICE_ATTR(appid_store, 0200, NULL, fc_appid_store);
3838 #endif /* CONFIG_BLK_CGROUP_FC_APPID */
3839
3840 static struct attribute *nvme_fc_attrs[] = {
3841 &dev_attr_nvme_discovery.attr,
3842 #ifdef CONFIG_BLK_CGROUP_FC_APPID
3843 &dev_attr_appid_store.attr,
3844 #endif
3845 NULL
3846 };
3847
3848 static const struct attribute_group nvme_fc_attr_group = {
3849 .attrs = nvme_fc_attrs,
3850 };
3851
3852 static const struct attribute_group *nvme_fc_attr_groups[] = {
3853 &nvme_fc_attr_group,
3854 NULL
3855 };
3856
3857 static struct class fc_class = {
3858 .name = "fc",
3859 .dev_groups = nvme_fc_attr_groups,
3860 };
3861
nvme_fc_init_module(void)3862 static int __init nvme_fc_init_module(void)
3863 {
3864 int ret;
3865
3866 /*
3867 * NOTE:
3868 * It is expected that in the future the kernel will combine
3869 * the FC-isms that are currently under scsi and now being
3870 * added to by NVME into a new standalone FC class. The SCSI
3871 * and NVME protocols and their devices would be under this
3872 * new FC class.
3873 *
3874 * As we need something to post FC-specific udev events to,
3875 * specifically for nvme probe events, start by creating the
3876 * new device class. When the new standalone FC class is
3877 * put in place, this code will move to a more generic
3878 * location for the class.
3879 */
3880 ret = class_register(&fc_class);
3881 if (ret) {
3882 pr_err("couldn't register class fc\n");
3883 return ret;
3884 }
3885
3886 /*
3887 * Create a device for the FC-centric udev events
3888 */
3889 fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
3890 "fc_udev_device");
3891 if (IS_ERR(fc_udev_device)) {
3892 pr_err("couldn't create fc_udev device!\n");
3893 ret = PTR_ERR(fc_udev_device);
3894 goto out_destroy_class;
3895 }
3896
3897 ret = nvmf_register_transport(&nvme_fc_transport);
3898 if (ret)
3899 goto out_destroy_device;
3900
3901 return 0;
3902
3903 out_destroy_device:
3904 device_destroy(&fc_class, MKDEV(0, 0));
3905 out_destroy_class:
3906 class_unregister(&fc_class);
3907
3908 return ret;
3909 }
3910
3911 static void
nvme_fc_delete_controllers(struct nvme_fc_rport * rport)3912 nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
3913 {
3914 struct nvme_fc_ctrl *ctrl;
3915
3916 spin_lock(&rport->lock);
3917 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3918 dev_warn(ctrl->ctrl.device,
3919 "NVME-FC{%d}: transport unloading: deleting ctrl\n",
3920 ctrl->cnum);
3921 nvme_delete_ctrl(&ctrl->ctrl);
3922 }
3923 spin_unlock(&rport->lock);
3924 }
3925
nvme_fc_exit_module(void)3926 static void __exit nvme_fc_exit_module(void)
3927 {
3928 struct nvme_fc_lport *lport;
3929 struct nvme_fc_rport *rport;
3930 unsigned long flags;
3931
3932 spin_lock_irqsave(&nvme_fc_lock, flags);
3933 list_for_each_entry(lport, &nvme_fc_lport_list, port_list)
3934 list_for_each_entry(rport, &lport->endp_list, endp_list)
3935 nvme_fc_delete_controllers(rport);
3936 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3937 flush_workqueue(nvme_delete_wq);
3938
3939 nvmf_unregister_transport(&nvme_fc_transport);
3940
3941 device_destroy(&fc_class, MKDEV(0, 0));
3942 class_unregister(&fc_class);
3943 }
3944
3945 module_init(nvme_fc_init_module);
3946 module_exit(nvme_fc_exit_module);
3947
3948 MODULE_DESCRIPTION("NVMe host FC transport driver");
3949 MODULE_LICENSE("GPL v2");
3950