1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 Avago Technologies. All rights reserved.
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>
15 #include <linux/nvme-fc-driver.h>
16 #include <linux/nvme-fc.h>
18 #include <scsi/scsi_transport_fc.h>
19 #include <linux/blk-mq-pci.h>
21 /* *************************** Data Structures/Defines ****************** */
24 enum nvme_fc_queue_flags {
25 NVME_FC_Q_CONNECTED = 0,
29 #define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */
30 #define NVME_FC_DEFAULT_RECONNECT_TMO 2 /* delay between reconnects
31 * when connected and a
35 struct nvme_fc_queue {
36 struct nvme_fc_ctrl *ctrl;
38 struct blk_mq_hw_ctx *hctx;
40 size_t cmnd_capsule_len;
49 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
51 enum nvme_fcop_flags {
52 FCOP_FLAGS_TERMIO = (1 << 0),
53 FCOP_FLAGS_AEN = (1 << 1),
56 struct nvmefc_ls_req_op {
57 struct nvmefc_ls_req ls_req;
59 struct nvme_fc_rport *rport;
60 struct nvme_fc_queue *queue;
65 struct completion ls_done;
66 struct list_head lsreq_list; /* rport->ls_req_list */
70 struct nvmefc_ls_rcv_op {
71 struct nvme_fc_rport *rport;
72 struct nvmefc_ls_rsp *lsrsp;
73 union nvmefc_ls_requests *rqstbuf;
74 union nvmefc_ls_responses *rspbuf;
78 struct list_head lsrcv_list; /* rport->ls_rcv_list */
79 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
81 enum nvme_fcpop_state {
82 FCPOP_STATE_UNINIT = 0,
84 FCPOP_STATE_ACTIVE = 2,
85 FCPOP_STATE_ABORTED = 3,
86 FCPOP_STATE_COMPLETE = 4,
89 struct nvme_fc_fcp_op {
90 struct nvme_request nreq; /*
93 * the 1st element in the
98 struct nvmefc_fcp_req fcp_req;
100 struct nvme_fc_ctrl *ctrl;
101 struct nvme_fc_queue *queue;
109 struct nvme_fc_cmd_iu cmd_iu;
110 struct nvme_fc_ersp_iu rsp_iu;
113 struct nvme_fcp_op_w_sgl {
114 struct nvme_fc_fcp_op op;
115 struct scatterlist sgl[NVME_INLINE_SG_CNT];
119 struct nvme_fc_lport {
120 struct nvme_fc_local_port localport;
123 struct list_head port_list; /* nvme_fc_port_list */
124 struct list_head endp_list;
125 struct device *dev; /* physical device for dma */
126 struct nvme_fc_port_template *ops;
128 atomic_t act_rport_cnt;
129 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
131 struct nvme_fc_rport {
132 struct nvme_fc_remote_port remoteport;
134 struct list_head endp_list; /* for lport->endp_list */
135 struct list_head ctrl_list;
136 struct list_head ls_req_list;
137 struct list_head ls_rcv_list;
138 struct list_head disc_list;
139 struct device *dev; /* physical device for dma */
140 struct nvme_fc_lport *lport;
143 atomic_t act_ctrl_cnt;
144 unsigned long dev_loss_end;
145 struct work_struct lsrcv_work;
146 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
148 /* fc_ctrl flags values - specified as bit positions */
149 #define ASSOC_ACTIVE 0
150 #define ASSOC_FAILED 1
151 #define FCCTRL_TERMIO 2
153 struct nvme_fc_ctrl {
155 struct nvme_fc_queue *queues;
157 struct nvme_fc_lport *lport;
158 struct nvme_fc_rport *rport;
163 struct nvmefc_ls_rcv_op *rcv_disconn;
165 struct list_head ctrl_list; /* rport->ctrl_list */
167 struct blk_mq_tag_set admin_tag_set;
168 struct blk_mq_tag_set tag_set;
170 struct work_struct ioerr_work;
171 struct delayed_work connect_work;
176 wait_queue_head_t ioabort_wait;
178 struct nvme_fc_fcp_op aen_ops[NVME_NR_AEN_COMMANDS];
180 struct nvme_ctrl ctrl;
183 static inline struct nvme_fc_ctrl *
184 to_fc_ctrl(struct nvme_ctrl *ctrl)
186 return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
189 static inline struct nvme_fc_lport *
190 localport_to_lport(struct nvme_fc_local_port *portptr)
192 return container_of(portptr, struct nvme_fc_lport, localport);
195 static inline struct nvme_fc_rport *
196 remoteport_to_rport(struct nvme_fc_remote_port *portptr)
198 return container_of(portptr, struct nvme_fc_rport, remoteport);
201 static inline struct nvmefc_ls_req_op *
202 ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
204 return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
207 static inline struct nvme_fc_fcp_op *
208 fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
210 return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
215 /* *************************** Globals **************************** */
218 static DEFINE_SPINLOCK(nvme_fc_lock);
220 static LIST_HEAD(nvme_fc_lport_list);
221 static DEFINE_IDA(nvme_fc_local_port_cnt);
222 static DEFINE_IDA(nvme_fc_ctrl_cnt);
224 static struct workqueue_struct *nvme_fc_wq;
226 static bool nvme_fc_waiting_to_unload;
227 static DECLARE_COMPLETION(nvme_fc_unload_proceed);
230 * These items are short-term. They will eventually be moved into
231 * a generic FC class. See comments in module init.
233 static struct device *fc_udev_device;
235 static void nvme_fc_complete_rq(struct request *rq);
237 /* *********************** FC-NVME Port Management ************************ */
239 static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
240 struct nvme_fc_queue *, unsigned int);
242 static void nvme_fc_handle_ls_rqst_work(struct work_struct *work);
246 nvme_fc_free_lport(struct kref *ref)
248 struct nvme_fc_lport *lport =
249 container_of(ref, struct nvme_fc_lport, ref);
252 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
253 WARN_ON(!list_empty(&lport->endp_list));
255 /* remove from transport list */
256 spin_lock_irqsave(&nvme_fc_lock, flags);
257 list_del(&lport->port_list);
258 if (nvme_fc_waiting_to_unload && list_empty(&nvme_fc_lport_list))
259 complete(&nvme_fc_unload_proceed);
260 spin_unlock_irqrestore(&nvme_fc_lock, flags);
262 ida_free(&nvme_fc_local_port_cnt, lport->localport.port_num);
263 ida_destroy(&lport->endp_cnt);
265 put_device(lport->dev);
271 nvme_fc_lport_put(struct nvme_fc_lport *lport)
273 kref_put(&lport->ref, nvme_fc_free_lport);
277 nvme_fc_lport_get(struct nvme_fc_lport *lport)
279 return kref_get_unless_zero(&lport->ref);
283 static struct nvme_fc_lport *
284 nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo,
285 struct nvme_fc_port_template *ops,
288 struct nvme_fc_lport *lport;
291 spin_lock_irqsave(&nvme_fc_lock, flags);
293 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
294 if (lport->localport.node_name != pinfo->node_name ||
295 lport->localport.port_name != pinfo->port_name)
298 if (lport->dev != dev) {
299 lport = ERR_PTR(-EXDEV);
303 if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
304 lport = ERR_PTR(-EEXIST);
308 if (!nvme_fc_lport_get(lport)) {
310 * fails if ref cnt already 0. If so,
311 * act as if lport already deleted
317 /* resume the lport */
320 lport->localport.port_role = pinfo->port_role;
321 lport->localport.port_id = pinfo->port_id;
322 lport->localport.port_state = FC_OBJSTATE_ONLINE;
324 spin_unlock_irqrestore(&nvme_fc_lock, flags);
332 spin_unlock_irqrestore(&nvme_fc_lock, flags);
338 * nvme_fc_register_localport - transport entry point called by an
339 * LLDD to register the existence of a NVME
341 * @pinfo: pointer to information about the port to be registered
342 * @template: LLDD entrypoints and operational parameters for the port
343 * @dev: physical hardware device node port corresponds to. Will be
344 * used for DMA mappings
345 * @portptr: pointer to a local port pointer. Upon success, the routine
346 * will allocate a nvme_fc_local_port structure and place its
347 * address in the local port pointer. Upon failure, local port
348 * pointer will be set to 0.
351 * a completion status. Must be 0 upon success; a negative errno
352 * (ex: -ENXIO) upon failure.
355 nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
356 struct nvme_fc_port_template *template,
358 struct nvme_fc_local_port **portptr)
360 struct nvme_fc_lport *newrec;
364 if (!template->localport_delete || !template->remoteport_delete ||
365 !template->ls_req || !template->fcp_io ||
366 !template->ls_abort || !template->fcp_abort ||
367 !template->max_hw_queues || !template->max_sgl_segments ||
368 !template->max_dif_sgl_segments || !template->dma_boundary) {
370 goto out_reghost_failed;
374 * look to see if there is already a localport that had been
375 * deregistered and in the process of waiting for all the
376 * references to fully be removed. If the references haven't
377 * expired, we can simply re-enable the localport. Remoteports
378 * and controller reconnections should resume naturally.
380 newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
382 /* found an lport, but something about its state is bad */
383 if (IS_ERR(newrec)) {
384 ret = PTR_ERR(newrec);
385 goto out_reghost_failed;
387 /* found existing lport, which was resumed */
389 *portptr = &newrec->localport;
393 /* nothing found - allocate a new localport struct */
395 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
399 goto out_reghost_failed;
402 idx = ida_alloc(&nvme_fc_local_port_cnt, GFP_KERNEL);
408 if (!get_device(dev) && dev) {
413 INIT_LIST_HEAD(&newrec->port_list);
414 INIT_LIST_HEAD(&newrec->endp_list);
415 kref_init(&newrec->ref);
416 atomic_set(&newrec->act_rport_cnt, 0);
417 newrec->ops = template;
419 ida_init(&newrec->endp_cnt);
420 if (template->local_priv_sz)
421 newrec->localport.private = &newrec[1];
423 newrec->localport.private = NULL;
424 newrec->localport.node_name = pinfo->node_name;
425 newrec->localport.port_name = pinfo->port_name;
426 newrec->localport.port_role = pinfo->port_role;
427 newrec->localport.port_id = pinfo->port_id;
428 newrec->localport.port_state = FC_OBJSTATE_ONLINE;
429 newrec->localport.port_num = idx;
431 spin_lock_irqsave(&nvme_fc_lock, flags);
432 list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
433 spin_unlock_irqrestore(&nvme_fc_lock, flags);
436 dma_set_seg_boundary(dev, template->dma_boundary);
438 *portptr = &newrec->localport;
442 ida_free(&nvme_fc_local_port_cnt, idx);
450 EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
453 * nvme_fc_unregister_localport - transport entry point called by an
454 * LLDD to deregister/remove a previously
455 * registered a NVME host FC port.
456 * @portptr: pointer to the (registered) local port that is to be deregistered.
459 * a completion status. Must be 0 upon success; a negative errno
460 * (ex: -ENXIO) upon failure.
463 nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
465 struct nvme_fc_lport *lport = localport_to_lport(portptr);
471 spin_lock_irqsave(&nvme_fc_lock, flags);
473 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
474 spin_unlock_irqrestore(&nvme_fc_lock, flags);
477 portptr->port_state = FC_OBJSTATE_DELETED;
479 spin_unlock_irqrestore(&nvme_fc_lock, flags);
481 if (atomic_read(&lport->act_rport_cnt) == 0)
482 lport->ops->localport_delete(&lport->localport);
484 nvme_fc_lport_put(lport);
488 EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
491 * TRADDR strings, per FC-NVME are fixed format:
492 * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
493 * udev event will only differ by prefix of what field is
495 * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
496 * 19 + 43 + null_fudge = 64 characters
498 #define FCNVME_TRADDR_LENGTH 64
501 nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
502 struct nvme_fc_rport *rport)
504 char hostaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_HOST_TRADDR=...*/
505 char tgtaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_TRADDR=...*/
506 char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
508 if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
511 snprintf(hostaddr, sizeof(hostaddr),
512 "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
513 lport->localport.node_name, lport->localport.port_name);
514 snprintf(tgtaddr, sizeof(tgtaddr),
515 "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
516 rport->remoteport.node_name, rport->remoteport.port_name);
517 kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
521 nvme_fc_free_rport(struct kref *ref)
523 struct nvme_fc_rport *rport =
524 container_of(ref, struct nvme_fc_rport, ref);
525 struct nvme_fc_lport *lport =
526 localport_to_lport(rport->remoteport.localport);
529 WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
530 WARN_ON(!list_empty(&rport->ctrl_list));
532 /* remove from lport list */
533 spin_lock_irqsave(&nvme_fc_lock, flags);
534 list_del(&rport->endp_list);
535 spin_unlock_irqrestore(&nvme_fc_lock, flags);
537 WARN_ON(!list_empty(&rport->disc_list));
538 ida_free(&lport->endp_cnt, rport->remoteport.port_num);
542 nvme_fc_lport_put(lport);
546 nvme_fc_rport_put(struct nvme_fc_rport *rport)
548 kref_put(&rport->ref, nvme_fc_free_rport);
552 nvme_fc_rport_get(struct nvme_fc_rport *rport)
554 return kref_get_unless_zero(&rport->ref);
558 nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl)
560 switch (ctrl->ctrl.state) {
562 case NVME_CTRL_CONNECTING:
564 * As all reconnects were suppressed, schedule a
567 dev_info(ctrl->ctrl.device,
568 "NVME-FC{%d}: connectivity re-established. "
569 "Attempting reconnect\n", ctrl->cnum);
571 queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
574 case NVME_CTRL_RESETTING:
576 * Controller is already in the process of terminating the
577 * association. No need to do anything further. The reconnect
578 * step will naturally occur after the reset completes.
583 /* no action to take - let it delete */
588 static struct nvme_fc_rport *
589 nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport,
590 struct nvme_fc_port_info *pinfo)
592 struct nvme_fc_rport *rport;
593 struct nvme_fc_ctrl *ctrl;
596 spin_lock_irqsave(&nvme_fc_lock, flags);
598 list_for_each_entry(rport, &lport->endp_list, endp_list) {
599 if (rport->remoteport.node_name != pinfo->node_name ||
600 rport->remoteport.port_name != pinfo->port_name)
603 if (!nvme_fc_rport_get(rport)) {
604 rport = ERR_PTR(-ENOLCK);
608 spin_unlock_irqrestore(&nvme_fc_lock, flags);
610 spin_lock_irqsave(&rport->lock, flags);
612 /* has it been unregistered */
613 if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) {
614 /* means lldd called us twice */
615 spin_unlock_irqrestore(&rport->lock, flags);
616 nvme_fc_rport_put(rport);
617 return ERR_PTR(-ESTALE);
620 rport->remoteport.port_role = pinfo->port_role;
621 rport->remoteport.port_id = pinfo->port_id;
622 rport->remoteport.port_state = FC_OBJSTATE_ONLINE;
623 rport->dev_loss_end = 0;
626 * kick off a reconnect attempt on all associations to the
627 * remote port. A successful reconnects will resume i/o.
629 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
630 nvme_fc_resume_controller(ctrl);
632 spin_unlock_irqrestore(&rport->lock, flags);
640 spin_unlock_irqrestore(&nvme_fc_lock, flags);
646 __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport,
647 struct nvme_fc_port_info *pinfo)
649 if (pinfo->dev_loss_tmo)
650 rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
652 rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
656 * nvme_fc_register_remoteport - transport entry point called by an
657 * LLDD to register the existence of a NVME
658 * subsystem FC port on its fabric.
659 * @localport: pointer to the (registered) local port that the remote
660 * subsystem port is connected to.
661 * @pinfo: pointer to information about the port to be registered
662 * @portptr: pointer to a remote port pointer. Upon success, the routine
663 * will allocate a nvme_fc_remote_port structure and place its
664 * address in the remote port pointer. Upon failure, remote port
665 * pointer will be set to 0.
668 * a completion status. Must be 0 upon success; a negative errno
669 * (ex: -ENXIO) upon failure.
672 nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
673 struct nvme_fc_port_info *pinfo,
674 struct nvme_fc_remote_port **portptr)
676 struct nvme_fc_lport *lport = localport_to_lport(localport);
677 struct nvme_fc_rport *newrec;
681 if (!nvme_fc_lport_get(lport)) {
683 goto out_reghost_failed;
687 * look to see if there is already a remoteport that is waiting
688 * for a reconnect (within dev_loss_tmo) with the same WWN's.
689 * If so, transition to it and reconnect.
691 newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
693 /* found an rport, but something about its state is bad */
694 if (IS_ERR(newrec)) {
695 ret = PTR_ERR(newrec);
698 /* found existing rport, which was resumed */
700 nvme_fc_lport_put(lport);
701 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
702 nvme_fc_signal_discovery_scan(lport, newrec);
703 *portptr = &newrec->remoteport;
707 /* nothing found - allocate a new remoteport struct */
709 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
716 idx = ida_alloc(&lport->endp_cnt, GFP_KERNEL);
719 goto out_kfree_rport;
722 INIT_LIST_HEAD(&newrec->endp_list);
723 INIT_LIST_HEAD(&newrec->ctrl_list);
724 INIT_LIST_HEAD(&newrec->ls_req_list);
725 INIT_LIST_HEAD(&newrec->disc_list);
726 kref_init(&newrec->ref);
727 atomic_set(&newrec->act_ctrl_cnt, 0);
728 spin_lock_init(&newrec->lock);
729 newrec->remoteport.localport = &lport->localport;
730 INIT_LIST_HEAD(&newrec->ls_rcv_list);
731 newrec->dev = lport->dev;
732 newrec->lport = lport;
733 if (lport->ops->remote_priv_sz)
734 newrec->remoteport.private = &newrec[1];
736 newrec->remoteport.private = NULL;
737 newrec->remoteport.port_role = pinfo->port_role;
738 newrec->remoteport.node_name = pinfo->node_name;
739 newrec->remoteport.port_name = pinfo->port_name;
740 newrec->remoteport.port_id = pinfo->port_id;
741 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
742 newrec->remoteport.port_num = idx;
743 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
744 INIT_WORK(&newrec->lsrcv_work, nvme_fc_handle_ls_rqst_work);
746 spin_lock_irqsave(&nvme_fc_lock, flags);
747 list_add_tail(&newrec->endp_list, &lport->endp_list);
748 spin_unlock_irqrestore(&nvme_fc_lock, flags);
750 nvme_fc_signal_discovery_scan(lport, newrec);
752 *portptr = &newrec->remoteport;
758 nvme_fc_lport_put(lport);
763 EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
766 nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
768 struct nvmefc_ls_req_op *lsop;
772 spin_lock_irqsave(&rport->lock, flags);
774 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
775 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
776 lsop->flags |= FCOP_FLAGS_TERMIO;
777 spin_unlock_irqrestore(&rport->lock, flags);
778 rport->lport->ops->ls_abort(&rport->lport->localport,
784 spin_unlock_irqrestore(&rport->lock, flags);
790 nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
792 dev_info(ctrl->ctrl.device,
793 "NVME-FC{%d}: controller connectivity lost. Awaiting "
794 "Reconnect", ctrl->cnum);
796 switch (ctrl->ctrl.state) {
800 * Schedule a controller reset. The reset will terminate the
801 * association and schedule the reconnect timer. Reconnects
802 * will be attempted until either the ctlr_loss_tmo
803 * (max_retries * connect_delay) expires or the remoteport's
804 * dev_loss_tmo expires.
806 if (nvme_reset_ctrl(&ctrl->ctrl)) {
807 dev_warn(ctrl->ctrl.device,
808 "NVME-FC{%d}: Couldn't schedule reset.\n",
810 nvme_delete_ctrl(&ctrl->ctrl);
814 case NVME_CTRL_CONNECTING:
816 * The association has already been terminated and the
817 * controller is attempting reconnects. No need to do anything
818 * futher. Reconnects will be attempted until either the
819 * ctlr_loss_tmo (max_retries * connect_delay) expires or the
820 * remoteport's dev_loss_tmo expires.
824 case NVME_CTRL_RESETTING:
826 * Controller is already in the process of terminating the
827 * association. No need to do anything further. The reconnect
828 * step will kick in naturally after the association is
833 case NVME_CTRL_DELETING:
834 case NVME_CTRL_DELETING_NOIO:
836 /* no action to take - let it delete */
842 * nvme_fc_unregister_remoteport - transport entry point called by an
843 * LLDD to deregister/remove a previously
844 * registered a NVME subsystem FC port.
845 * @portptr: pointer to the (registered) remote port that is to be
849 * a completion status. Must be 0 upon success; a negative errno
850 * (ex: -ENXIO) upon failure.
853 nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
855 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
856 struct nvme_fc_ctrl *ctrl;
862 spin_lock_irqsave(&rport->lock, flags);
864 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
865 spin_unlock_irqrestore(&rport->lock, flags);
868 portptr->port_state = FC_OBJSTATE_DELETED;
870 rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
872 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
873 /* if dev_loss_tmo==0, dev loss is immediate */
874 if (!portptr->dev_loss_tmo) {
875 dev_warn(ctrl->ctrl.device,
876 "NVME-FC{%d}: controller connectivity lost.\n",
878 nvme_delete_ctrl(&ctrl->ctrl);
880 nvme_fc_ctrl_connectivity_loss(ctrl);
883 spin_unlock_irqrestore(&rport->lock, flags);
885 nvme_fc_abort_lsops(rport);
887 if (atomic_read(&rport->act_ctrl_cnt) == 0)
888 rport->lport->ops->remoteport_delete(portptr);
891 * release the reference, which will allow, if all controllers
892 * go away, which should only occur after dev_loss_tmo occurs,
893 * for the rport to be torn down.
895 nvme_fc_rport_put(rport);
899 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
902 * nvme_fc_rescan_remoteport - transport entry point called by an
903 * LLDD to request a nvme device rescan.
904 * @remoteport: pointer to the (registered) remote port that is to be
910 nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
912 struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
914 nvme_fc_signal_discovery_scan(rport->lport, rport);
916 EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
919 nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
922 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
925 spin_lock_irqsave(&rport->lock, flags);
927 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
928 spin_unlock_irqrestore(&rport->lock, flags);
932 /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
933 rport->remoteport.dev_loss_tmo = dev_loss_tmo;
935 spin_unlock_irqrestore(&rport->lock, flags);
939 EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
942 /* *********************** FC-NVME DMA Handling **************************** */
945 * The fcloop device passes in a NULL device pointer. Real LLD's will
946 * pass in a valid device pointer. If NULL is passed to the dma mapping
947 * routines, depending on the platform, it may or may not succeed, and
951 * Wrapper all the dma routines and check the dev pointer.
953 * If simple mappings (return just a dma address, we'll noop them,
954 * returning a dma address of 0.
956 * On more complex mappings (dma_map_sg), a pseudo routine fills
957 * in the scatter list, setting all dma addresses to 0.
960 static inline dma_addr_t
961 fc_dma_map_single(struct device *dev, void *ptr, size_t size,
962 enum dma_data_direction dir)
964 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
968 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
970 return dev ? dma_mapping_error(dev, dma_addr) : 0;
974 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
975 enum dma_data_direction dir)
978 dma_unmap_single(dev, addr, size, dir);
982 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
983 enum dma_data_direction dir)
986 dma_sync_single_for_cpu(dev, addr, size, dir);
990 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
991 enum dma_data_direction dir)
994 dma_sync_single_for_device(dev, addr, size, dir);
997 /* pseudo dma_map_sg call */
999 fc_map_sg(struct scatterlist *sg, int nents)
1001 struct scatterlist *s;
1004 WARN_ON(nents == 0 || sg[0].length == 0);
1006 for_each_sg(sg, s, nents, i) {
1007 s->dma_address = 0L;
1008 #ifdef CONFIG_NEED_SG_DMA_LENGTH
1009 s->dma_length = s->length;
1016 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1017 enum dma_data_direction dir)
1019 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
1023 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1024 enum dma_data_direction dir)
1027 dma_unmap_sg(dev, sg, nents, dir);
1030 /* *********************** FC-NVME LS Handling **************************** */
1032 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
1033 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
1035 static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
1038 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
1040 struct nvme_fc_rport *rport = lsop->rport;
1041 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1042 unsigned long flags;
1044 spin_lock_irqsave(&rport->lock, flags);
1046 if (!lsop->req_queued) {
1047 spin_unlock_irqrestore(&rport->lock, flags);
1051 list_del(&lsop->lsreq_list);
1053 lsop->req_queued = false;
1055 spin_unlock_irqrestore(&rport->lock, flags);
1057 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1058 (lsreq->rqstlen + lsreq->rsplen),
1061 nvme_fc_rport_put(rport);
1065 __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
1066 struct nvmefc_ls_req_op *lsop,
1067 void (*done)(struct nvmefc_ls_req *req, int status))
1069 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1070 unsigned long flags;
1073 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1074 return -ECONNREFUSED;
1076 if (!nvme_fc_rport_get(rport))
1080 lsop->rport = rport;
1081 lsop->req_queued = false;
1082 INIT_LIST_HEAD(&lsop->lsreq_list);
1083 init_completion(&lsop->ls_done);
1085 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
1086 lsreq->rqstlen + lsreq->rsplen,
1088 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1092 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1094 spin_lock_irqsave(&rport->lock, flags);
1096 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
1098 lsop->req_queued = true;
1100 spin_unlock_irqrestore(&rport->lock, flags);
1102 ret = rport->lport->ops->ls_req(&rport->lport->localport,
1103 &rport->remoteport, lsreq);
1110 lsop->ls_error = ret;
1111 spin_lock_irqsave(&rport->lock, flags);
1112 lsop->req_queued = false;
1113 list_del(&lsop->lsreq_list);
1114 spin_unlock_irqrestore(&rport->lock, flags);
1115 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1116 (lsreq->rqstlen + lsreq->rsplen),
1119 nvme_fc_rport_put(rport);
1125 nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1127 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1129 lsop->ls_error = status;
1130 complete(&lsop->ls_done);
1134 nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
1136 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1137 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1140 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
1144 * No timeout/not interruptible as we need the struct
1145 * to exist until the lldd calls us back. Thus mandate
1146 * wait until driver calls back. lldd responsible for
1147 * the timeout action
1149 wait_for_completion(&lsop->ls_done);
1151 __nvme_fc_finish_ls_req(lsop);
1153 ret = lsop->ls_error;
1159 /* ACC or RJT payload ? */
1160 if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1167 nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
1168 struct nvmefc_ls_req_op *lsop,
1169 void (*done)(struct nvmefc_ls_req *req, int status))
1171 /* don't wait for completion */
1173 return __nvme_fc_send_ls_req(rport, lsop, done);
1177 nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1178 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1180 struct nvmefc_ls_req_op *lsop;
1181 struct nvmefc_ls_req *lsreq;
1182 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1183 struct fcnvme_ls_cr_assoc_acc *assoc_acc;
1184 unsigned long flags;
1187 lsop = kzalloc((sizeof(*lsop) +
1188 sizeof(*assoc_rqst) + sizeof(*assoc_acc) +
1189 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1191 dev_info(ctrl->ctrl.device,
1192 "NVME-FC{%d}: send Create Association failed: ENOMEM\n",
1198 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1];
1199 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1200 lsreq = &lsop->ls_req;
1201 if (ctrl->lport->ops->lsrqst_priv_sz)
1202 lsreq->private = &assoc_acc[1];
1204 lsreq->private = NULL;
1206 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1207 assoc_rqst->desc_list_len =
1208 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1210 assoc_rqst->assoc_cmd.desc_tag =
1211 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1212 assoc_rqst->assoc_cmd.desc_len =
1214 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1216 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1217 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
1218 /* Linux supports only Dynamic controllers */
1219 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
1220 uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1221 strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1222 min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1223 strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1224 min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
1226 lsop->queue = queue;
1227 lsreq->rqstaddr = assoc_rqst;
1228 lsreq->rqstlen = sizeof(*assoc_rqst);
1229 lsreq->rspaddr = assoc_acc;
1230 lsreq->rsplen = sizeof(*assoc_acc);
1231 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1233 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1235 goto out_free_buffer;
1237 /* process connect LS completion */
1239 /* validate the ACC response */
1240 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1242 else if (assoc_acc->hdr.desc_list_len !=
1244 sizeof(struct fcnvme_ls_cr_assoc_acc)))
1245 fcret = VERR_CR_ASSOC_ACC_LEN;
1246 else if (assoc_acc->hdr.rqst.desc_tag !=
1247 cpu_to_be32(FCNVME_LSDESC_RQST))
1248 fcret = VERR_LSDESC_RQST;
1249 else if (assoc_acc->hdr.rqst.desc_len !=
1250 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1251 fcret = VERR_LSDESC_RQST_LEN;
1252 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1253 fcret = VERR_CR_ASSOC;
1254 else if (assoc_acc->associd.desc_tag !=
1255 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1256 fcret = VERR_ASSOC_ID;
1257 else if (assoc_acc->associd.desc_len !=
1259 sizeof(struct fcnvme_lsdesc_assoc_id)))
1260 fcret = VERR_ASSOC_ID_LEN;
1261 else if (assoc_acc->connectid.desc_tag !=
1262 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1263 fcret = VERR_CONN_ID;
1264 else if (assoc_acc->connectid.desc_len !=
1265 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1266 fcret = VERR_CONN_ID_LEN;
1271 "q %d Create Association LS failed: %s\n",
1272 queue->qnum, validation_errors[fcret]);
1274 spin_lock_irqsave(&ctrl->lock, flags);
1275 ctrl->association_id =
1276 be64_to_cpu(assoc_acc->associd.association_id);
1277 queue->connection_id =
1278 be64_to_cpu(assoc_acc->connectid.connection_id);
1279 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1280 spin_unlock_irqrestore(&ctrl->lock, flags);
1288 "queue %d connect admin queue failed (%d).\n",
1294 nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1295 u16 qsize, u16 ersp_ratio)
1297 struct nvmefc_ls_req_op *lsop;
1298 struct nvmefc_ls_req *lsreq;
1299 struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1300 struct fcnvme_ls_cr_conn_acc *conn_acc;
1303 lsop = kzalloc((sizeof(*lsop) +
1304 sizeof(*conn_rqst) + sizeof(*conn_acc) +
1305 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1307 dev_info(ctrl->ctrl.device,
1308 "NVME-FC{%d}: send Create Connection failed: ENOMEM\n",
1314 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)&lsop[1];
1315 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1316 lsreq = &lsop->ls_req;
1317 if (ctrl->lport->ops->lsrqst_priv_sz)
1318 lsreq->private = (void *)&conn_acc[1];
1320 lsreq->private = NULL;
1322 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1323 conn_rqst->desc_list_len = cpu_to_be32(
1324 sizeof(struct fcnvme_lsdesc_assoc_id) +
1325 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1327 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1328 conn_rqst->associd.desc_len =
1330 sizeof(struct fcnvme_lsdesc_assoc_id));
1331 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1332 conn_rqst->connect_cmd.desc_tag =
1333 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1334 conn_rqst->connect_cmd.desc_len =
1336 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1337 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1338 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
1339 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
1341 lsop->queue = queue;
1342 lsreq->rqstaddr = conn_rqst;
1343 lsreq->rqstlen = sizeof(*conn_rqst);
1344 lsreq->rspaddr = conn_acc;
1345 lsreq->rsplen = sizeof(*conn_acc);
1346 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1348 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1350 goto out_free_buffer;
1352 /* process connect LS completion */
1354 /* validate the ACC response */
1355 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1357 else if (conn_acc->hdr.desc_list_len !=
1358 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1359 fcret = VERR_CR_CONN_ACC_LEN;
1360 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1361 fcret = VERR_LSDESC_RQST;
1362 else if (conn_acc->hdr.rqst.desc_len !=
1363 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1364 fcret = VERR_LSDESC_RQST_LEN;
1365 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1366 fcret = VERR_CR_CONN;
1367 else if (conn_acc->connectid.desc_tag !=
1368 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1369 fcret = VERR_CONN_ID;
1370 else if (conn_acc->connectid.desc_len !=
1371 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1372 fcret = VERR_CONN_ID_LEN;
1377 "q %d Create I/O Connection LS failed: %s\n",
1378 queue->qnum, validation_errors[fcret]);
1380 queue->connection_id =
1381 be64_to_cpu(conn_acc->connectid.connection_id);
1382 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1390 "queue %d connect I/O queue failed (%d).\n",
1396 nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1398 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1400 __nvme_fc_finish_ls_req(lsop);
1402 /* fc-nvme initiator doesn't care about success or failure of cmd */
1408 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1409 * the FC-NVME Association. Terminating the association also
1410 * terminates the FC-NVME connections (per queue, both admin and io
1411 * queues) that are part of the association. E.g. things are torn
1412 * down, and the related FC-NVME Association ID and Connection IDs
1415 * The behavior of the fc-nvme initiator is such that it's
1416 * understanding of the association and connections will implicitly
1417 * be torn down. The action is implicit as it may be due to a loss of
1418 * connectivity with the fc-nvme target, so you may never get a
1419 * response even if you tried. As such, the action of this routine
1420 * is to asynchronously send the LS, ignore any results of the LS, and
1421 * continue on with terminating the association. If the fc-nvme target
1422 * is present and receives the LS, it too can tear down.
1425 nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1427 struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
1428 struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
1429 struct nvmefc_ls_req_op *lsop;
1430 struct nvmefc_ls_req *lsreq;
1433 lsop = kzalloc((sizeof(*lsop) +
1434 sizeof(*discon_rqst) + sizeof(*discon_acc) +
1435 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1437 dev_info(ctrl->ctrl.device,
1438 "NVME-FC{%d}: send Disconnect Association "
1444 discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)&lsop[1];
1445 discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
1446 lsreq = &lsop->ls_req;
1447 if (ctrl->lport->ops->lsrqst_priv_sz)
1448 lsreq->private = (void *)&discon_acc[1];
1450 lsreq->private = NULL;
1452 nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc,
1453 ctrl->association_id);
1455 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1456 nvme_fc_disconnect_assoc_done);
1462 nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
1464 struct nvmefc_ls_rcv_op *lsop = lsrsp->nvme_fc_private;
1465 struct nvme_fc_rport *rport = lsop->rport;
1466 struct nvme_fc_lport *lport = rport->lport;
1467 unsigned long flags;
1469 spin_lock_irqsave(&rport->lock, flags);
1470 list_del(&lsop->lsrcv_list);
1471 spin_unlock_irqrestore(&rport->lock, flags);
1473 fc_dma_sync_single_for_cpu(lport->dev, lsop->rspdma,
1474 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1475 fc_dma_unmap_single(lport->dev, lsop->rspdma,
1476 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1480 nvme_fc_rport_put(rport);
1484 nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op *lsop)
1486 struct nvme_fc_rport *rport = lsop->rport;
1487 struct nvme_fc_lport *lport = rport->lport;
1488 struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
1491 fc_dma_sync_single_for_device(lport->dev, lsop->rspdma,
1492 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1494 ret = lport->ops->xmt_ls_rsp(&lport->localport, &rport->remoteport,
1497 dev_warn(lport->dev,
1498 "LLDD rejected LS RSP xmt: LS %d status %d\n",
1500 nvme_fc_xmt_ls_rsp_done(lsop->lsrsp);
1505 static struct nvme_fc_ctrl *
1506 nvme_fc_match_disconn_ls(struct nvme_fc_rport *rport,
1507 struct nvmefc_ls_rcv_op *lsop)
1509 struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1510 &lsop->rqstbuf->rq_dis_assoc;
1511 struct nvme_fc_ctrl *ctrl, *ret = NULL;
1512 struct nvmefc_ls_rcv_op *oldls = NULL;
1513 u64 association_id = be64_to_cpu(rqst->associd.association_id);
1514 unsigned long flags;
1516 spin_lock_irqsave(&rport->lock, flags);
1518 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
1519 if (!nvme_fc_ctrl_get(ctrl))
1521 spin_lock(&ctrl->lock);
1522 if (association_id == ctrl->association_id) {
1523 oldls = ctrl->rcv_disconn;
1524 ctrl->rcv_disconn = lsop;
1527 spin_unlock(&ctrl->lock);
1529 /* leave the ctrl get reference */
1531 nvme_fc_ctrl_put(ctrl);
1534 spin_unlock_irqrestore(&rport->lock, flags);
1536 /* transmit a response for anything that was pending */
1538 dev_info(rport->lport->dev,
1539 "NVME-FC{%d}: Multiple Disconnect Association "
1540 "LS's received\n", ctrl->cnum);
1541 /* overwrite good response with bogus failure */
1542 oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf,
1543 sizeof(*oldls->rspbuf),
1546 FCNVME_RJT_EXP_NONE, 0);
1547 nvme_fc_xmt_ls_rsp(oldls);
1554 * returns true to mean LS handled and ls_rsp can be sent
1555 * returns false to defer ls_rsp xmt (will be done as part of
1556 * association termination)
1559 nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op *lsop)
1561 struct nvme_fc_rport *rport = lsop->rport;
1562 struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1563 &lsop->rqstbuf->rq_dis_assoc;
1564 struct fcnvme_ls_disconnect_assoc_acc *acc =
1565 &lsop->rspbuf->rsp_dis_assoc;
1566 struct nvme_fc_ctrl *ctrl = NULL;
1569 memset(acc, 0, sizeof(*acc));
1571 ret = nvmefc_vldt_lsreq_discon_assoc(lsop->rqstdatalen, rqst);
1573 /* match an active association */
1574 ctrl = nvme_fc_match_disconn_ls(rport, lsop);
1576 ret = VERR_NO_ASSOC;
1580 dev_info(rport->lport->dev,
1581 "Disconnect LS failed: %s\n",
1582 validation_errors[ret]);
1583 lsop->lsrsp->rsplen = nvme_fc_format_rjt(acc,
1584 sizeof(*acc), rqst->w0.ls_cmd,
1585 (ret == VERR_NO_ASSOC) ?
1586 FCNVME_RJT_RC_INV_ASSOC :
1587 FCNVME_RJT_RC_LOGIC,
1588 FCNVME_RJT_EXP_NONE, 0);
1592 /* format an ACCept response */
1594 lsop->lsrsp->rsplen = sizeof(*acc);
1596 nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1598 sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
1599 FCNVME_LS_DISCONNECT_ASSOC);
1602 * the transmit of the response will occur after the exchanges
1603 * for the association have been ABTS'd by
1604 * nvme_fc_delete_association().
1607 /* fail the association */
1608 nvme_fc_error_recovery(ctrl, "Disconnect Association LS received");
1610 /* release the reference taken by nvme_fc_match_disconn_ls() */
1611 nvme_fc_ctrl_put(ctrl);
1617 * Actual Processing routine for received FC-NVME LS Requests from the LLD
1618 * returns true if a response should be sent afterward, false if rsp will
1619 * be sent asynchronously.
1622 nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op *lsop)
1624 struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
1627 lsop->lsrsp->nvme_fc_private = lsop;
1628 lsop->lsrsp->rspbuf = lsop->rspbuf;
1629 lsop->lsrsp->rspdma = lsop->rspdma;
1630 lsop->lsrsp->done = nvme_fc_xmt_ls_rsp_done;
1631 /* Be preventative. handlers will later set to valid length */
1632 lsop->lsrsp->rsplen = 0;
1636 * parse request input, execute the request, and format the
1639 switch (w0->ls_cmd) {
1640 case FCNVME_LS_DISCONNECT_ASSOC:
1641 ret = nvme_fc_ls_disconnect_assoc(lsop);
1643 case FCNVME_LS_DISCONNECT_CONN:
1644 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1645 sizeof(*lsop->rspbuf), w0->ls_cmd,
1646 FCNVME_RJT_RC_UNSUP, FCNVME_RJT_EXP_NONE, 0);
1648 case FCNVME_LS_CREATE_ASSOCIATION:
1649 case FCNVME_LS_CREATE_CONNECTION:
1650 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1651 sizeof(*lsop->rspbuf), w0->ls_cmd,
1652 FCNVME_RJT_RC_LOGIC, FCNVME_RJT_EXP_NONE, 0);
1655 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1656 sizeof(*lsop->rspbuf), w0->ls_cmd,
1657 FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
1665 nvme_fc_handle_ls_rqst_work(struct work_struct *work)
1667 struct nvme_fc_rport *rport =
1668 container_of(work, struct nvme_fc_rport, lsrcv_work);
1669 struct fcnvme_ls_rqst_w0 *w0;
1670 struct nvmefc_ls_rcv_op *lsop;
1671 unsigned long flags;
1676 spin_lock_irqsave(&rport->lock, flags);
1677 list_for_each_entry(lsop, &rport->ls_rcv_list, lsrcv_list) {
1681 lsop->handled = true;
1682 if (rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
1683 spin_unlock_irqrestore(&rport->lock, flags);
1684 sendrsp = nvme_fc_handle_ls_rqst(lsop);
1686 spin_unlock_irqrestore(&rport->lock, flags);
1687 w0 = &lsop->rqstbuf->w0;
1688 lsop->lsrsp->rsplen = nvme_fc_format_rjt(
1690 sizeof(*lsop->rspbuf),
1693 FCNVME_RJT_EXP_NONE, 0);
1696 nvme_fc_xmt_ls_rsp(lsop);
1699 spin_unlock_irqrestore(&rport->lock, flags);
1703 * nvme_fc_rcv_ls_req - transport entry point called by an LLDD
1704 * upon the reception of a NVME LS request.
1706 * The nvme-fc layer will copy payload to an internal structure for
1707 * processing. As such, upon completion of the routine, the LLDD may
1708 * immediately free/reuse the LS request buffer passed in the call.
1710 * If this routine returns error, the LLDD should abort the exchange.
1712 * @portptr: pointer to the (registered) remote port that the LS
1713 * was received from. The remoteport is associated with
1714 * a specific localport.
1715 * @lsrsp: pointer to a nvmefc_ls_rsp response structure to be
1716 * used to reference the exchange corresponding to the LS
1717 * when issuing an ls response.
1718 * @lsreqbuf: pointer to the buffer containing the LS Request
1719 * @lsreqbuf_len: length, in bytes, of the received LS request
1722 nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
1723 struct nvmefc_ls_rsp *lsrsp,
1724 void *lsreqbuf, u32 lsreqbuf_len)
1726 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
1727 struct nvme_fc_lport *lport = rport->lport;
1728 struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf;
1729 struct nvmefc_ls_rcv_op *lsop;
1730 unsigned long flags;
1733 nvme_fc_rport_get(rport);
1735 /* validate there's a routine to transmit a response */
1736 if (!lport->ops->xmt_ls_rsp) {
1737 dev_info(lport->dev,
1738 "RCV %s LS failed: no LLDD xmt_ls_rsp\n",
1739 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1740 nvmefc_ls_names[w0->ls_cmd] : "");
1745 if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) {
1746 dev_info(lport->dev,
1747 "RCV %s LS failed: payload too large\n",
1748 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1749 nvmefc_ls_names[w0->ls_cmd] : "");
1754 lsop = kzalloc(sizeof(*lsop) +
1755 sizeof(union nvmefc_ls_requests) +
1756 sizeof(union nvmefc_ls_responses),
1759 dev_info(lport->dev,
1760 "RCV %s LS failed: No memory\n",
1761 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1762 nvmefc_ls_names[w0->ls_cmd] : "");
1766 lsop->rqstbuf = (union nvmefc_ls_requests *)&lsop[1];
1767 lsop->rspbuf = (union nvmefc_ls_responses *)&lsop->rqstbuf[1];
1769 lsop->rspdma = fc_dma_map_single(lport->dev, lsop->rspbuf,
1770 sizeof(*lsop->rspbuf),
1772 if (fc_dma_mapping_error(lport->dev, lsop->rspdma)) {
1773 dev_info(lport->dev,
1774 "RCV %s LS failed: DMA mapping failure\n",
1775 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1776 nvmefc_ls_names[w0->ls_cmd] : "");
1781 lsop->rport = rport;
1782 lsop->lsrsp = lsrsp;
1784 memcpy(lsop->rqstbuf, lsreqbuf, lsreqbuf_len);
1785 lsop->rqstdatalen = lsreqbuf_len;
1787 spin_lock_irqsave(&rport->lock, flags);
1788 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) {
1789 spin_unlock_irqrestore(&rport->lock, flags);
1793 list_add_tail(&lsop->lsrcv_list, &rport->ls_rcv_list);
1794 spin_unlock_irqrestore(&rport->lock, flags);
1796 schedule_work(&rport->lsrcv_work);
1801 fc_dma_unmap_single(lport->dev, lsop->rspdma,
1802 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1806 nvme_fc_rport_put(rport);
1809 EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req);
1812 /* *********************** NVME Ctrl Routines **************************** */
1815 __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1816 struct nvme_fc_fcp_op *op)
1818 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1819 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1820 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1821 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1823 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1827 nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1828 unsigned int hctx_idx)
1830 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1832 return __nvme_fc_exit_request(set->driver_data, op);
1836 __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1838 unsigned long flags;
1841 spin_lock_irqsave(&ctrl->lock, flags);
1842 opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1843 if (opstate != FCPOP_STATE_ACTIVE)
1844 atomic_set(&op->state, opstate);
1845 else if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) {
1846 op->flags |= FCOP_FLAGS_TERMIO;
1849 spin_unlock_irqrestore(&ctrl->lock, flags);
1851 if (opstate != FCPOP_STATE_ACTIVE)
1854 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1855 &ctrl->rport->remoteport,
1856 op->queue->lldd_handle,
1863 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1865 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
1868 /* ensure we've initialized the ops once */
1869 if (!(aen_op->flags & FCOP_FLAGS_AEN))
1872 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
1873 __nvme_fc_abort_op(ctrl, aen_op);
1877 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1878 struct nvme_fc_fcp_op *op, int opstate)
1880 unsigned long flags;
1882 if (opstate == FCPOP_STATE_ABORTED) {
1883 spin_lock_irqsave(&ctrl->lock, flags);
1884 if (test_bit(FCCTRL_TERMIO, &ctrl->flags) &&
1885 op->flags & FCOP_FLAGS_TERMIO) {
1887 wake_up(&ctrl->ioabort_wait);
1889 spin_unlock_irqrestore(&ctrl->lock, flags);
1894 nvme_fc_ctrl_ioerr_work(struct work_struct *work)
1896 struct nvme_fc_ctrl *ctrl =
1897 container_of(work, struct nvme_fc_ctrl, ioerr_work);
1899 nvme_fc_error_recovery(ctrl, "transport detected io error");
1903 * nvme_fc_io_getuuid - Routine called to get the appid field
1904 * associated with request by the lldd
1905 * @req:IO request from nvme fc to driver
1906 * Returns: UUID if there is an appid associated with VM or
1907 * NULL if the user/libvirt has not set the appid to VM
1909 char *nvme_fc_io_getuuid(struct nvmefc_fcp_req *req)
1911 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1912 struct request *rq = op->rq;
1914 if (!IS_ENABLED(CONFIG_BLK_CGROUP_FC_APPID) || !rq->bio)
1916 return blkcg_get_fc_appid(rq->bio);
1918 EXPORT_SYMBOL_GPL(nvme_fc_io_getuuid);
1921 nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1923 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1924 struct request *rq = op->rq;
1925 struct nvmefc_fcp_req *freq = &op->fcp_req;
1926 struct nvme_fc_ctrl *ctrl = op->ctrl;
1927 struct nvme_fc_queue *queue = op->queue;
1928 struct nvme_completion *cqe = &op->rsp_iu.cqe;
1929 struct nvme_command *sqe = &op->cmd_iu.sqe;
1930 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
1931 union nvme_result result;
1932 bool terminate_assoc = true;
1937 * The current linux implementation of a nvme controller
1938 * allocates a single tag set for all io queues and sizes
1939 * the io queues to fully hold all possible tags. Thus, the
1940 * implementation does not reference or care about the sqhd
1941 * value as it never needs to use the sqhd/sqtail pointers
1942 * for submission pacing.
1944 * This affects the FC-NVME implementation in two ways:
1945 * 1) As the value doesn't matter, we don't need to waste
1946 * cycles extracting it from ERSPs and stamping it in the
1947 * cases where the transport fabricates CQEs on successful
1949 * 2) The FC-NVME implementation requires that delivery of
1950 * ERSP completions are to go back to the nvme layer in order
1951 * relative to the rsn, such that the sqhd value will always
1952 * be "in order" for the nvme layer. As the nvme layer in
1953 * linux doesn't care about sqhd, there's no need to return
1957 * As the core nvme layer in linux currently does not look at
1958 * every field in the cqe - in cases where the FC transport must
1959 * fabricate a CQE, the following fields will not be set as they
1960 * are not referenced:
1961 * cqe.sqid, cqe.sqhd, cqe.command_id
1963 * Failure or error of an individual i/o, in a transport
1964 * detected fashion unrelated to the nvme completion status,
1965 * potentially cause the initiator and target sides to get out
1966 * of sync on SQ head/tail (aka outstanding io count allowed).
1967 * Per FC-NVME spec, failure of an individual command requires
1968 * the connection to be terminated, which in turn requires the
1969 * association to be terminated.
1972 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
1974 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1975 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1977 if (opstate == FCPOP_STATE_ABORTED)
1978 status = cpu_to_le16(NVME_SC_HOST_ABORTED_CMD << 1);
1979 else if (freq->status) {
1980 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1981 dev_info(ctrl->ctrl.device,
1982 "NVME-FC{%d}: io failed due to lldd error %d\n",
1983 ctrl->cnum, freq->status);
1987 * For the linux implementation, if we have an unsuccesful
1988 * status, they blk-mq layer can typically be called with the
1989 * non-zero status and the content of the cqe isn't important.
1995 * command completed successfully relative to the wire
1996 * protocol. However, validate anything received and
1997 * extract the status and result from the cqe (create it
2001 switch (freq->rcv_rsplen) {
2004 case NVME_FC_SIZEOF_ZEROS_RSP:
2006 * No response payload or 12 bytes of payload (which
2007 * should all be zeros) are considered successful and
2008 * no payload in the CQE by the transport.
2010 if (freq->transferred_length !=
2011 be32_to_cpu(op->cmd_iu.data_len)) {
2012 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2013 dev_info(ctrl->ctrl.device,
2014 "NVME-FC{%d}: io failed due to bad transfer "
2015 "length: %d vs expected %d\n",
2016 ctrl->cnum, freq->transferred_length,
2017 be32_to_cpu(op->cmd_iu.data_len));
2023 case sizeof(struct nvme_fc_ersp_iu):
2025 * The ERSP IU contains a full completion with CQE.
2026 * Validate ERSP IU and look at cqe.
2028 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
2029 (freq->rcv_rsplen / 4) ||
2030 be32_to_cpu(op->rsp_iu.xfrd_len) !=
2031 freq->transferred_length ||
2032 op->rsp_iu.ersp_result ||
2033 sqe->common.command_id != cqe->command_id)) {
2034 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2035 dev_info(ctrl->ctrl.device,
2036 "NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
2037 "iu len %d, xfr len %d vs %d, status code "
2038 "%d, cmdid %d vs %d\n",
2039 ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len),
2040 be32_to_cpu(op->rsp_iu.xfrd_len),
2041 freq->transferred_length,
2042 op->rsp_iu.ersp_result,
2043 sqe->common.command_id,
2047 result = cqe->result;
2048 status = cqe->status;
2052 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2053 dev_info(ctrl->ctrl.device,
2054 "NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
2056 ctrl->cnum, freq->rcv_rsplen);
2060 terminate_assoc = false;
2063 if (op->flags & FCOP_FLAGS_AEN) {
2064 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
2065 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2066 atomic_set(&op->state, FCPOP_STATE_IDLE);
2067 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
2068 nvme_fc_ctrl_put(ctrl);
2072 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2073 if (!nvme_try_complete_req(rq, status, result))
2074 nvme_fc_complete_rq(rq);
2077 if (terminate_assoc && ctrl->ctrl.state != NVME_CTRL_RESETTING)
2078 queue_work(nvme_reset_wq, &ctrl->ioerr_work);
2082 __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
2083 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
2084 struct request *rq, u32 rqno)
2086 struct nvme_fcp_op_w_sgl *op_w_sgl =
2087 container_of(op, typeof(*op_w_sgl), op);
2088 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2091 memset(op, 0, sizeof(*op));
2092 op->fcp_req.cmdaddr = &op->cmd_iu;
2093 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
2094 op->fcp_req.rspaddr = &op->rsp_iu;
2095 op->fcp_req.rsplen = sizeof(op->rsp_iu);
2096 op->fcp_req.done = nvme_fc_fcpio_done;
2102 cmdiu->format_id = NVME_CMD_FORMAT_ID;
2103 cmdiu->fc_id = NVME_CMD_FC_ID;
2104 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
2106 cmdiu->rsv_cat = fccmnd_set_cat_css(0,
2107 (NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT));
2109 cmdiu->rsv_cat = fccmnd_set_cat_admin(0);
2111 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
2112 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
2113 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
2115 "FCP Op failed - cmdiu dma mapping failed.\n");
2120 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
2121 &op->rsp_iu, sizeof(op->rsp_iu),
2123 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
2125 "FCP Op failed - rspiu dma mapping failed.\n");
2129 atomic_set(&op->state, FCPOP_STATE_IDLE);
2135 nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
2136 unsigned int hctx_idx, unsigned int numa_node)
2138 struct nvme_fc_ctrl *ctrl = set->driver_data;
2139 struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq);
2140 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
2141 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
2144 res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++);
2147 op->op.fcp_req.first_sgl = op->sgl;
2148 op->op.fcp_req.private = &op->priv[0];
2149 nvme_req(rq)->ctrl = &ctrl->ctrl;
2150 nvme_req(rq)->cmd = &op->op.cmd_iu.sqe;
2155 nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
2157 struct nvme_fc_fcp_op *aen_op;
2158 struct nvme_fc_cmd_iu *cmdiu;
2159 struct nvme_command *sqe;
2160 void *private = NULL;
2163 aen_op = ctrl->aen_ops;
2164 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2165 if (ctrl->lport->ops->fcprqst_priv_sz) {
2166 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
2172 cmdiu = &aen_op->cmd_iu;
2174 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
2175 aen_op, (struct request *)NULL,
2176 (NVME_AQ_BLK_MQ_DEPTH + i));
2182 aen_op->flags = FCOP_FLAGS_AEN;
2183 aen_op->fcp_req.private = private;
2185 memset(sqe, 0, sizeof(*sqe));
2186 sqe->common.opcode = nvme_admin_async_event;
2187 /* Note: core layer may overwrite the sqe.command_id value */
2188 sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
2194 nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
2196 struct nvme_fc_fcp_op *aen_op;
2199 cancel_work_sync(&ctrl->ctrl.async_event_work);
2200 aen_op = ctrl->aen_ops;
2201 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2202 __nvme_fc_exit_request(ctrl, aen_op);
2204 kfree(aen_op->fcp_req.private);
2205 aen_op->fcp_req.private = NULL;
2210 __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
2213 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
2215 hctx->driver_data = queue;
2220 nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2221 unsigned int hctx_idx)
2223 struct nvme_fc_ctrl *ctrl = data;
2225 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
2231 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2232 unsigned int hctx_idx)
2234 struct nvme_fc_ctrl *ctrl = data;
2236 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
2242 nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
2244 struct nvme_fc_queue *queue;
2246 queue = &ctrl->queues[idx];
2247 memset(queue, 0, sizeof(*queue));
2250 atomic_set(&queue->csn, 0);
2251 queue->dev = ctrl->dev;
2254 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
2256 queue->cmnd_capsule_len = sizeof(struct nvme_command);
2259 * Considered whether we should allocate buffers for all SQEs
2260 * and CQEs and dma map them - mapping their respective entries
2261 * into the request structures (kernel vm addr and dma address)
2262 * thus the driver could use the buffers/mappings directly.
2263 * It only makes sense if the LLDD would use them for its
2264 * messaging api. It's very unlikely most adapter api's would use
2265 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
2266 * structures were used instead.
2271 * This routine terminates a queue at the transport level.
2272 * The transport has already ensured that all outstanding ios on
2273 * the queue have been terminated.
2274 * The transport will send a Disconnect LS request to terminate
2275 * the queue's connection. Termination of the admin queue will also
2276 * terminate the association at the target.
2279 nvme_fc_free_queue(struct nvme_fc_queue *queue)
2281 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
2284 clear_bit(NVME_FC_Q_LIVE, &queue->flags);
2286 * Current implementation never disconnects a single queue.
2287 * It always terminates a whole association. So there is never
2288 * a disconnect(queue) LS sent to the target.
2291 queue->connection_id = 0;
2292 atomic_set(&queue->csn, 0);
2296 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
2297 struct nvme_fc_queue *queue, unsigned int qidx)
2299 if (ctrl->lport->ops->delete_queue)
2300 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
2301 queue->lldd_handle);
2302 queue->lldd_handle = NULL;
2306 nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
2310 for (i = 1; i < ctrl->ctrl.queue_count; i++)
2311 nvme_fc_free_queue(&ctrl->queues[i]);
2315 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
2316 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
2320 queue->lldd_handle = NULL;
2321 if (ctrl->lport->ops->create_queue)
2322 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
2323 qidx, qsize, &queue->lldd_handle);
2329 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
2331 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
2334 for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
2335 __nvme_fc_delete_hw_queue(ctrl, queue, i);
2339 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2341 struct nvme_fc_queue *queue = &ctrl->queues[1];
2344 for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
2345 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
2354 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
2359 nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2363 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
2364 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
2368 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
2372 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
2379 nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
2383 for (i = 1; i < ctrl->ctrl.queue_count; i++)
2384 nvme_fc_init_queue(ctrl, i);
2388 nvme_fc_ctrl_free(struct kref *ref)
2390 struct nvme_fc_ctrl *ctrl =
2391 container_of(ref, struct nvme_fc_ctrl, ref);
2392 unsigned long flags;
2394 if (ctrl->ctrl.tagset) {
2395 blk_mq_destroy_queue(ctrl->ctrl.connect_q);
2396 blk_mq_free_tag_set(&ctrl->tag_set);
2399 /* remove from rport list */
2400 spin_lock_irqsave(&ctrl->rport->lock, flags);
2401 list_del(&ctrl->ctrl_list);
2402 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
2404 nvme_start_admin_queue(&ctrl->ctrl);
2405 blk_mq_destroy_queue(ctrl->ctrl.admin_q);
2406 blk_mq_destroy_queue(ctrl->ctrl.fabrics_q);
2407 blk_mq_free_tag_set(&ctrl->admin_tag_set);
2409 kfree(ctrl->queues);
2411 put_device(ctrl->dev);
2412 nvme_fc_rport_put(ctrl->rport);
2414 ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
2415 if (ctrl->ctrl.opts)
2416 nvmf_free_options(ctrl->ctrl.opts);
2421 nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2423 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2427 nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2429 return kref_get_unless_zero(&ctrl->ref);
2433 * All accesses from nvme core layer done - can now free the
2434 * controller. Called after last nvme_put_ctrl() call
2437 nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
2439 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2441 WARN_ON(nctrl != &ctrl->ctrl);
2443 nvme_fc_ctrl_put(ctrl);
2447 * This routine is used by the transport when it needs to find active
2448 * io on a queue that is to be terminated. The transport uses
2449 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2450 * this routine to kill them on a 1 by 1 basis.
2452 * As FC allocates FC exchange for each io, the transport must contact
2453 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2454 * After terminating the exchange the LLDD will call the transport's
2455 * normal io done path for the request, but it will have an aborted
2456 * status. The done path will return the io request back to the block
2457 * layer with an error status.
2459 static bool nvme_fc_terminate_exchange(struct request *req, void *data)
2461 struct nvme_ctrl *nctrl = data;
2462 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2463 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2465 op->nreq.flags |= NVME_REQ_CANCELLED;
2466 __nvme_fc_abort_op(ctrl, op);
2471 * This routine runs through all outstanding commands on the association
2472 * and aborts them. This routine is typically be called by the
2473 * delete_association routine. It is also called due to an error during
2474 * reconnect. In that scenario, it is most likely a command that initializes
2475 * the controller, including fabric Connect commands on io queues, that
2476 * may have timed out or failed thus the io must be killed for the connect
2477 * thread to see the error.
2480 __nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl *ctrl, bool start_queues)
2485 * if aborting io, the queues are no longer good, mark them
2488 if (ctrl->ctrl.queue_count > 1) {
2489 for (q = 1; q < ctrl->ctrl.queue_count; q++)
2490 clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[q].flags);
2492 clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
2495 * If io queues are present, stop them and terminate all outstanding
2496 * ios on them. As FC allocates FC exchange for each io, the
2497 * transport must contact the LLDD to terminate the exchange,
2498 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2499 * to tell us what io's are busy and invoke a transport routine
2500 * to kill them with the LLDD. After terminating the exchange
2501 * the LLDD will call the transport's normal io done path, but it
2502 * will have an aborted status. The done path will return the
2503 * io requests back to the block layer as part of normal completions
2504 * (but with error status).
2506 if (ctrl->ctrl.queue_count > 1) {
2507 nvme_stop_queues(&ctrl->ctrl);
2508 nvme_sync_io_queues(&ctrl->ctrl);
2509 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2510 nvme_fc_terminate_exchange, &ctrl->ctrl);
2511 blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
2513 nvme_start_queues(&ctrl->ctrl);
2517 * Other transports, which don't have link-level contexts bound
2518 * to sqe's, would try to gracefully shutdown the controller by
2519 * writing the registers for shutdown and polling (call
2520 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
2521 * just aborted and we will wait on those contexts, and given
2522 * there was no indication of how live the controlelr is on the
2523 * link, don't send more io to create more contexts for the
2524 * shutdown. Let the controller fail via keepalive failure if
2525 * its still present.
2529 * clean up the admin queue. Same thing as above.
2531 nvme_stop_admin_queue(&ctrl->ctrl);
2532 blk_sync_queue(ctrl->ctrl.admin_q);
2533 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2534 nvme_fc_terminate_exchange, &ctrl->ctrl);
2535 blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
2539 nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
2542 * if an error (io timeout, etc) while (re)connecting, the remote
2543 * port requested terminating of the association (disconnect_ls)
2544 * or an error (timeout or abort) occurred on an io while creating
2545 * the controller. Abort any ios on the association and let the
2546 * create_association error path resolve things.
2548 if (ctrl->ctrl.state == NVME_CTRL_CONNECTING) {
2549 __nvme_fc_abort_outstanding_ios(ctrl, true);
2550 set_bit(ASSOC_FAILED, &ctrl->flags);
2554 /* Otherwise, only proceed if in LIVE state - e.g. on first error */
2555 if (ctrl->ctrl.state != NVME_CTRL_LIVE)
2558 dev_warn(ctrl->ctrl.device,
2559 "NVME-FC{%d}: transport association event: %s\n",
2560 ctrl->cnum, errmsg);
2561 dev_warn(ctrl->ctrl.device,
2562 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
2564 nvme_reset_ctrl(&ctrl->ctrl);
2567 static enum blk_eh_timer_return nvme_fc_timeout(struct request *rq)
2569 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2570 struct nvme_fc_ctrl *ctrl = op->ctrl;
2571 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2572 struct nvme_command *sqe = &cmdiu->sqe;
2575 * Attempt to abort the offending command. Command completion
2576 * will detect the aborted io and will fail the connection.
2578 dev_info(ctrl->ctrl.device,
2579 "NVME-FC{%d.%d}: io timeout: opcode %d fctype %d w10/11: "
2581 ctrl->cnum, op->queue->qnum, sqe->common.opcode,
2582 sqe->connect.fctype, sqe->common.cdw10, sqe->common.cdw11);
2583 if (__nvme_fc_abort_op(ctrl, op))
2584 nvme_fc_error_recovery(ctrl, "io timeout abort failed");
2587 * the io abort has been initiated. Have the reset timer
2588 * restarted and the abort completion will complete the io
2589 * shortly. Avoids a synchronous wait while the abort finishes.
2591 return BLK_EH_RESET_TIMER;
2595 nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2596 struct nvme_fc_fcp_op *op)
2598 struct nvmefc_fcp_req *freq = &op->fcp_req;
2603 if (!blk_rq_nr_phys_segments(rq))
2606 freq->sg_table.sgl = freq->first_sgl;
2607 ret = sg_alloc_table_chained(&freq->sg_table,
2608 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
2609 NVME_INLINE_SG_CNT);
2613 op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
2614 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2615 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2616 op->nents, rq_dma_dir(rq));
2617 if (unlikely(freq->sg_cnt <= 0)) {
2618 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2624 * TODO: blk_integrity_rq(rq) for DIF
2630 nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2631 struct nvme_fc_fcp_op *op)
2633 struct nvmefc_fcp_req *freq = &op->fcp_req;
2638 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2641 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2647 * In FC, the queue is a logical thing. At transport connect, the target
2648 * creates its "queue" and returns a handle that is to be given to the
2649 * target whenever it posts something to the corresponding SQ. When an
2650 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2651 * command contained within the SQE, an io, and assigns a FC exchange
2652 * to it. The SQE and the associated SQ handle are sent in the initial
2653 * CMD IU sents on the exchange. All transfers relative to the io occur
2654 * as part of the exchange. The CQE is the last thing for the io,
2655 * which is transferred (explicitly or implicitly) with the RSP IU
2656 * sent on the exchange. After the CQE is received, the FC exchange is
2657 * terminaed and the Exchange may be used on a different io.
2659 * The transport to LLDD api has the transport making a request for a
2660 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2661 * resource and transfers the command. The LLDD will then process all
2662 * steps to complete the io. Upon completion, the transport done routine
2665 * So - while the operation is outstanding to the LLDD, there is a link
2666 * level FC exchange resource that is also outstanding. This must be
2667 * considered in all cleanup operations.
2670 nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2671 struct nvme_fc_fcp_op *op, u32 data_len,
2672 enum nvmefc_fcp_datadir io_dir)
2674 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2675 struct nvme_command *sqe = &cmdiu->sqe;
2679 * before attempting to send the io, check to see if we believe
2680 * the target device is present
2682 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2683 return BLK_STS_RESOURCE;
2685 if (!nvme_fc_ctrl_get(ctrl))
2686 return BLK_STS_IOERR;
2688 /* format the FC-NVME CMD IU and fcp_req */
2689 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2690 cmdiu->data_len = cpu_to_be32(data_len);
2692 case NVMEFC_FCP_WRITE:
2693 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2695 case NVMEFC_FCP_READ:
2696 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2698 case NVMEFC_FCP_NODATA:
2702 op->fcp_req.payload_length = data_len;
2703 op->fcp_req.io_dir = io_dir;
2704 op->fcp_req.transferred_length = 0;
2705 op->fcp_req.rcv_rsplen = 0;
2706 op->fcp_req.status = NVME_SC_SUCCESS;
2707 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2710 * validate per fabric rules, set fields mandated by fabric spec
2711 * as well as those by FC-NVME spec.
2713 WARN_ON_ONCE(sqe->common.metadata);
2714 sqe->common.flags |= NVME_CMD_SGL_METABUF;
2717 * format SQE DPTR field per FC-NVME rules:
2718 * type=0x5 Transport SGL Data Block Descriptor
2719 * subtype=0xA Transport-specific value
2721 * length=length of the data series
2723 sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2724 NVME_SGL_FMT_TRANSPORT_A;
2725 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2726 sqe->rw.dptr.sgl.addr = 0;
2728 if (!(op->flags & FCOP_FLAGS_AEN)) {
2729 ret = nvme_fc_map_data(ctrl, op->rq, op);
2731 nvme_cleanup_cmd(op->rq);
2732 nvme_fc_ctrl_put(ctrl);
2733 if (ret == -ENOMEM || ret == -EAGAIN)
2734 return BLK_STS_RESOURCE;
2735 return BLK_STS_IOERR;
2739 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2740 sizeof(op->cmd_iu), DMA_TO_DEVICE);
2742 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2744 if (!(op->flags & FCOP_FLAGS_AEN))
2745 blk_mq_start_request(op->rq);
2747 cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2748 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2749 &ctrl->rport->remoteport,
2750 queue->lldd_handle, &op->fcp_req);
2754 * If the lld fails to send the command is there an issue with
2755 * the csn value? If the command that fails is the Connect,
2756 * no - as the connection won't be live. If it is a command
2757 * post-connect, it's possible a gap in csn may be created.
2758 * Does this matter? As Linux initiators don't send fused
2759 * commands, no. The gap would exist, but as there's nothing
2760 * that depends on csn order to be delivered on the target
2761 * side, it shouldn't hurt. It would be difficult for a
2762 * target to even detect the csn gap as it has no idea when the
2763 * cmd with the csn was supposed to arrive.
2765 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2766 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2768 if (!(op->flags & FCOP_FLAGS_AEN)) {
2769 nvme_fc_unmap_data(ctrl, op->rq, op);
2770 nvme_cleanup_cmd(op->rq);
2773 nvme_fc_ctrl_put(ctrl);
2775 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2777 return BLK_STS_IOERR;
2779 return BLK_STS_RESOURCE;
2786 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2787 const struct blk_mq_queue_data *bd)
2789 struct nvme_ns *ns = hctx->queue->queuedata;
2790 struct nvme_fc_queue *queue = hctx->driver_data;
2791 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2792 struct request *rq = bd->rq;
2793 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2794 enum nvmefc_fcp_datadir io_dir;
2795 bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2799 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
2800 !nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2801 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
2803 ret = nvme_setup_cmd(ns, rq);
2808 * nvme core doesn't quite treat the rq opaquely. Commands such
2809 * as WRITE ZEROES will return a non-zero rq payload_bytes yet
2810 * there is no actual payload to be transferred.
2811 * To get it right, key data transmission on there being 1 or
2812 * more physical segments in the sg list. If there is no
2813 * physical segments, there is no payload.
2815 if (blk_rq_nr_phys_segments(rq)) {
2816 data_len = blk_rq_payload_bytes(rq);
2817 io_dir = ((rq_data_dir(rq) == WRITE) ?
2818 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2821 io_dir = NVMEFC_FCP_NODATA;
2825 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2829 nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2831 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2832 struct nvme_fc_fcp_op *aen_op;
2835 if (test_bit(FCCTRL_TERMIO, &ctrl->flags))
2838 aen_op = &ctrl->aen_ops[0];
2840 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2843 dev_err(ctrl->ctrl.device,
2844 "failed async event work\n");
2848 nvme_fc_complete_rq(struct request *rq)
2850 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2851 struct nvme_fc_ctrl *ctrl = op->ctrl;
2853 atomic_set(&op->state, FCPOP_STATE_IDLE);
2854 op->flags &= ~FCOP_FLAGS_TERMIO;
2856 nvme_fc_unmap_data(ctrl, rq, op);
2857 nvme_complete_rq(rq);
2858 nvme_fc_ctrl_put(ctrl);
2861 static int nvme_fc_map_queues(struct blk_mq_tag_set *set)
2863 struct nvme_fc_ctrl *ctrl = set->driver_data;
2866 for (i = 0; i < set->nr_maps; i++) {
2867 struct blk_mq_queue_map *map = &set->map[i];
2869 if (!map->nr_queues) {
2870 WARN_ON(i == HCTX_TYPE_DEFAULT);
2874 /* Call LLDD map queue functionality if defined */
2875 if (ctrl->lport->ops->map_queues)
2876 ctrl->lport->ops->map_queues(&ctrl->lport->localport,
2879 blk_mq_map_queues(map);
2884 static const struct blk_mq_ops nvme_fc_mq_ops = {
2885 .queue_rq = nvme_fc_queue_rq,
2886 .complete = nvme_fc_complete_rq,
2887 .init_request = nvme_fc_init_request,
2888 .exit_request = nvme_fc_exit_request,
2889 .init_hctx = nvme_fc_init_hctx,
2890 .timeout = nvme_fc_timeout,
2891 .map_queues = nvme_fc_map_queues,
2895 nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2897 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2898 unsigned int nr_io_queues;
2901 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2902 ctrl->lport->ops->max_hw_queues);
2903 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2905 dev_info(ctrl->ctrl.device,
2906 "set_queue_count failed: %d\n", ret);
2910 ctrl->ctrl.queue_count = nr_io_queues + 1;
2914 nvme_fc_init_io_queues(ctrl);
2916 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2917 ctrl->tag_set.ops = &nvme_fc_mq_ops;
2918 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2919 ctrl->tag_set.reserved_tags = NVMF_RESERVED_TAGS;
2920 ctrl->tag_set.numa_node = ctrl->ctrl.numa_node;
2921 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2922 ctrl->tag_set.cmd_size =
2923 struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv,
2924 ctrl->lport->ops->fcprqst_priv_sz);
2925 ctrl->tag_set.driver_data = ctrl;
2926 ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
2927 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2929 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2933 ctrl->ctrl.tagset = &ctrl->tag_set;
2935 ret = nvme_ctrl_init_connect_q(&(ctrl->ctrl));
2937 goto out_free_tag_set;
2939 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2941 goto out_cleanup_blk_queue;
2943 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2945 goto out_delete_hw_queues;
2947 ctrl->ioq_live = true;
2951 out_delete_hw_queues:
2952 nvme_fc_delete_hw_io_queues(ctrl);
2953 out_cleanup_blk_queue:
2954 blk_mq_destroy_queue(ctrl->ctrl.connect_q);
2956 blk_mq_free_tag_set(&ctrl->tag_set);
2957 nvme_fc_free_io_queues(ctrl);
2959 /* force put free routine to ignore io queues */
2960 ctrl->ctrl.tagset = NULL;
2966 nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
2968 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2969 u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
2970 unsigned int nr_io_queues;
2973 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2974 ctrl->lport->ops->max_hw_queues);
2975 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2977 dev_info(ctrl->ctrl.device,
2978 "set_queue_count failed: %d\n", ret);
2982 if (!nr_io_queues && prior_ioq_cnt) {
2983 dev_info(ctrl->ctrl.device,
2984 "Fail Reconnect: At least 1 io queue "
2985 "required (was %d)\n", prior_ioq_cnt);
2989 ctrl->ctrl.queue_count = nr_io_queues + 1;
2990 /* check for io queues existing */
2991 if (ctrl->ctrl.queue_count == 1)
2994 if (prior_ioq_cnt != nr_io_queues) {
2995 dev_info(ctrl->ctrl.device,
2996 "reconnect: revising io queue count from %d to %d\n",
2997 prior_ioq_cnt, nr_io_queues);
2998 blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
3001 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
3003 goto out_free_io_queues;
3005 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
3007 goto out_delete_hw_queues;
3011 out_delete_hw_queues:
3012 nvme_fc_delete_hw_io_queues(ctrl);
3014 nvme_fc_free_io_queues(ctrl);
3019 nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
3021 struct nvme_fc_lport *lport = rport->lport;
3023 atomic_inc(&lport->act_rport_cnt);
3027 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
3029 struct nvme_fc_lport *lport = rport->lport;
3032 cnt = atomic_dec_return(&lport->act_rport_cnt);
3033 if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
3034 lport->ops->localport_delete(&lport->localport);
3038 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
3040 struct nvme_fc_rport *rport = ctrl->rport;
3043 if (test_and_set_bit(ASSOC_ACTIVE, &ctrl->flags))
3046 cnt = atomic_inc_return(&rport->act_ctrl_cnt);
3048 nvme_fc_rport_active_on_lport(rport);
3054 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
3056 struct nvme_fc_rport *rport = ctrl->rport;
3057 struct nvme_fc_lport *lport = rport->lport;
3060 /* clearing of ctrl->flags ASSOC_ACTIVE bit is in association delete */
3062 cnt = atomic_dec_return(&rport->act_ctrl_cnt);
3064 if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
3065 lport->ops->remoteport_delete(&rport->remoteport);
3066 nvme_fc_rport_inactive_on_lport(rport);
3073 * This routine restarts the controller on the host side, and
3074 * on the link side, recreates the controller association.
3077 nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
3079 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
3080 struct nvmefc_ls_rcv_op *disls = NULL;
3081 unsigned long flags;
3085 ++ctrl->ctrl.nr_reconnects;
3087 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3090 if (nvme_fc_ctlr_active_on_rport(ctrl))
3093 dev_info(ctrl->ctrl.device,
3094 "NVME-FC{%d}: create association : host wwpn 0x%016llx "
3095 " rport wwpn 0x%016llx: NQN \"%s\"\n",
3096 ctrl->cnum, ctrl->lport->localport.port_name,
3097 ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
3099 clear_bit(ASSOC_FAILED, &ctrl->flags);
3102 * Create the admin queue
3105 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
3108 goto out_free_queue;
3110 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
3111 NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
3113 goto out_delete_hw_queue;
3115 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
3117 goto out_disconnect_admin_queue;
3119 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
3122 * Check controller capabilities
3124 * todo:- add code to check if ctrl attributes changed from
3125 * prior connection values
3128 ret = nvme_enable_ctrl(&ctrl->ctrl);
3129 if (ret || test_bit(ASSOC_FAILED, &ctrl->flags))
3130 goto out_disconnect_admin_queue;
3132 ctrl->ctrl.max_segments = ctrl->lport->ops->max_sgl_segments;
3133 ctrl->ctrl.max_hw_sectors = ctrl->ctrl.max_segments <<
3136 nvme_start_admin_queue(&ctrl->ctrl);
3138 ret = nvme_init_ctrl_finish(&ctrl->ctrl);
3139 if (ret || test_bit(ASSOC_FAILED, &ctrl->flags))
3140 goto out_disconnect_admin_queue;
3144 /* FC-NVME does not have other data in the capsule */
3145 if (ctrl->ctrl.icdoff) {
3146 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
3148 ret = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
3149 goto out_disconnect_admin_queue;
3152 /* FC-NVME supports normal SGL Data Block Descriptors */
3153 if (!nvme_ctrl_sgl_supported(&ctrl->ctrl)) {
3154 dev_err(ctrl->ctrl.device,
3155 "Mandatory sgls are not supported!\n");
3156 ret = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
3157 goto out_disconnect_admin_queue;
3160 if (opts->queue_size > ctrl->ctrl.maxcmd) {
3161 /* warn if maxcmd is lower than queue_size */
3162 dev_warn(ctrl->ctrl.device,
3163 "queue_size %zu > ctrl maxcmd %u, reducing "
3165 opts->queue_size, ctrl->ctrl.maxcmd);
3166 opts->queue_size = ctrl->ctrl.maxcmd;
3169 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
3170 /* warn if sqsize is lower than queue_size */
3171 dev_warn(ctrl->ctrl.device,
3172 "queue_size %zu > ctrl sqsize %u, reducing "
3174 opts->queue_size, ctrl->ctrl.sqsize + 1);
3175 opts->queue_size = ctrl->ctrl.sqsize + 1;
3178 ret = nvme_fc_init_aen_ops(ctrl);
3180 goto out_term_aen_ops;
3183 * Create the io queues
3186 if (ctrl->ctrl.queue_count > 1) {
3187 if (!ctrl->ioq_live)
3188 ret = nvme_fc_create_io_queues(ctrl);
3190 ret = nvme_fc_recreate_io_queues(ctrl);
3192 if (ret || test_bit(ASSOC_FAILED, &ctrl->flags))
3193 goto out_term_aen_ops;
3195 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
3197 ctrl->ctrl.nr_reconnects = 0;
3200 nvme_start_ctrl(&ctrl->ctrl);
3202 return 0; /* Success */
3205 nvme_fc_term_aen_ops(ctrl);
3206 out_disconnect_admin_queue:
3207 /* send a Disconnect(association) LS to fc-nvme target */
3208 nvme_fc_xmt_disconnect_assoc(ctrl);
3209 spin_lock_irqsave(&ctrl->lock, flags);
3210 ctrl->association_id = 0;
3211 disls = ctrl->rcv_disconn;
3212 ctrl->rcv_disconn = NULL;
3213 spin_unlock_irqrestore(&ctrl->lock, flags);
3215 nvme_fc_xmt_ls_rsp(disls);
3216 out_delete_hw_queue:
3217 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3219 nvme_fc_free_queue(&ctrl->queues[0]);
3220 clear_bit(ASSOC_ACTIVE, &ctrl->flags);
3221 nvme_fc_ctlr_inactive_on_rport(ctrl);
3228 * This routine stops operation of the controller on the host side.
3229 * On the host os stack side: Admin and IO queues are stopped,
3230 * outstanding ios on them terminated via FC ABTS.
3231 * On the link side: the association is terminated.
3234 nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
3236 struct nvmefc_ls_rcv_op *disls = NULL;
3237 unsigned long flags;
3239 if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
3242 spin_lock_irqsave(&ctrl->lock, flags);
3243 set_bit(FCCTRL_TERMIO, &ctrl->flags);
3245 spin_unlock_irqrestore(&ctrl->lock, flags);
3247 __nvme_fc_abort_outstanding_ios(ctrl, false);
3249 /* kill the aens as they are a separate path */
3250 nvme_fc_abort_aen_ops(ctrl);
3252 /* wait for all io that had to be aborted */
3253 spin_lock_irq(&ctrl->lock);
3254 wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
3255 clear_bit(FCCTRL_TERMIO, &ctrl->flags);
3256 spin_unlock_irq(&ctrl->lock);
3258 nvme_fc_term_aen_ops(ctrl);
3261 * send a Disconnect(association) LS to fc-nvme target
3262 * Note: could have been sent at top of process, but
3263 * cleaner on link traffic if after the aborts complete.
3264 * Note: if association doesn't exist, association_id will be 0
3266 if (ctrl->association_id)
3267 nvme_fc_xmt_disconnect_assoc(ctrl);
3269 spin_lock_irqsave(&ctrl->lock, flags);
3270 ctrl->association_id = 0;
3271 disls = ctrl->rcv_disconn;
3272 ctrl->rcv_disconn = NULL;
3273 spin_unlock_irqrestore(&ctrl->lock, flags);
3276 * if a Disconnect Request was waiting for a response, send
3277 * now that all ABTS's have been issued (and are complete).
3279 nvme_fc_xmt_ls_rsp(disls);
3281 if (ctrl->ctrl.tagset) {
3282 nvme_fc_delete_hw_io_queues(ctrl);
3283 nvme_fc_free_io_queues(ctrl);
3286 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3287 nvme_fc_free_queue(&ctrl->queues[0]);
3289 /* re-enable the admin_q so anything new can fast fail */
3290 nvme_start_admin_queue(&ctrl->ctrl);
3292 /* resume the io queues so that things will fast fail */
3293 nvme_start_queues(&ctrl->ctrl);
3295 nvme_fc_ctlr_inactive_on_rport(ctrl);
3299 nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
3301 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
3303 cancel_work_sync(&ctrl->ioerr_work);
3304 cancel_delayed_work_sync(&ctrl->connect_work);
3306 * kill the association on the link side. this will block
3307 * waiting for io to terminate
3309 nvme_fc_delete_association(ctrl);
3313 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
3315 struct nvme_fc_rport *rport = ctrl->rport;
3316 struct nvme_fc_remote_port *portptr = &rport->remoteport;
3317 unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
3320 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING)
3323 if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3324 dev_info(ctrl->ctrl.device,
3325 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
3326 ctrl->cnum, status);
3327 if (status > 0 && (status & NVME_SC_DNR))
3329 } else if (time_after_eq(jiffies, rport->dev_loss_end))
3332 if (recon && nvmf_should_reconnect(&ctrl->ctrl)) {
3333 if (portptr->port_state == FC_OBJSTATE_ONLINE)
3334 dev_info(ctrl->ctrl.device,
3335 "NVME-FC{%d}: Reconnect attempt in %ld "
3337 ctrl->cnum, recon_delay / HZ);
3338 else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
3339 recon_delay = rport->dev_loss_end - jiffies;
3341 queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
3343 if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3344 if (status > 0 && (status & NVME_SC_DNR))
3345 dev_warn(ctrl->ctrl.device,
3346 "NVME-FC{%d}: reconnect failure\n",
3349 dev_warn(ctrl->ctrl.device,
3350 "NVME-FC{%d}: Max reconnect attempts "
3352 ctrl->cnum, ctrl->ctrl.nr_reconnects);
3354 dev_warn(ctrl->ctrl.device,
3355 "NVME-FC{%d}: dev_loss_tmo (%d) expired "
3356 "while waiting for remoteport connectivity.\n",
3357 ctrl->cnum, min_t(int, portptr->dev_loss_tmo,
3358 (ctrl->ctrl.opts->max_reconnects *
3359 ctrl->ctrl.opts->reconnect_delay)));
3360 WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
3365 nvme_fc_reset_ctrl_work(struct work_struct *work)
3367 struct nvme_fc_ctrl *ctrl =
3368 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
3370 nvme_stop_ctrl(&ctrl->ctrl);
3372 /* will block will waiting for io to terminate */
3373 nvme_fc_delete_association(ctrl);
3375 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
3376 dev_err(ctrl->ctrl.device,
3377 "NVME-FC{%d}: error_recovery: Couldn't change state "
3378 "to CONNECTING\n", ctrl->cnum);
3380 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
3381 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3382 dev_err(ctrl->ctrl.device,
3383 "NVME-FC{%d}: failed to schedule connect "
3384 "after reset\n", ctrl->cnum);
3386 flush_delayed_work(&ctrl->connect_work);
3389 nvme_fc_reconnect_or_delete(ctrl, -ENOTCONN);
3394 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
3396 .module = THIS_MODULE,
3397 .flags = NVME_F_FABRICS,
3398 .reg_read32 = nvmf_reg_read32,
3399 .reg_read64 = nvmf_reg_read64,
3400 .reg_write32 = nvmf_reg_write32,
3401 .free_ctrl = nvme_fc_nvme_ctrl_freed,
3402 .submit_async_event = nvme_fc_submit_async_event,
3403 .delete_ctrl = nvme_fc_delete_ctrl,
3404 .get_address = nvmf_get_address,
3408 nvme_fc_connect_ctrl_work(struct work_struct *work)
3412 struct nvme_fc_ctrl *ctrl =
3413 container_of(to_delayed_work(work),
3414 struct nvme_fc_ctrl, connect_work);
3416 ret = nvme_fc_create_association(ctrl);
3418 nvme_fc_reconnect_or_delete(ctrl, ret);
3420 dev_info(ctrl->ctrl.device,
3421 "NVME-FC{%d}: controller connect complete\n",
3426 static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
3427 .queue_rq = nvme_fc_queue_rq,
3428 .complete = nvme_fc_complete_rq,
3429 .init_request = nvme_fc_init_request,
3430 .exit_request = nvme_fc_exit_request,
3431 .init_hctx = nvme_fc_init_admin_hctx,
3432 .timeout = nvme_fc_timeout,
3437 * Fails a controller request if it matches an existing controller
3438 * (association) with the same tuple:
3439 * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
3441 * The ports don't need to be compared as they are intrinsically
3442 * already matched by the port pointers supplied.
3445 nvme_fc_existing_controller(struct nvme_fc_rport *rport,
3446 struct nvmf_ctrl_options *opts)
3448 struct nvme_fc_ctrl *ctrl;
3449 unsigned long flags;
3452 spin_lock_irqsave(&rport->lock, flags);
3453 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3454 found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
3458 spin_unlock_irqrestore(&rport->lock, flags);
3463 static struct nvme_ctrl *
3464 nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3465 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3467 struct nvme_fc_ctrl *ctrl;
3468 unsigned long flags;
3469 int ret, idx, ctrl_loss_tmo;
3471 if (!(rport->remoteport.port_role &
3472 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
3477 if (!opts->duplicate_connect &&
3478 nvme_fc_existing_controller(rport, opts)) {
3483 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3489 idx = ida_alloc(&nvme_fc_ctrl_cnt, GFP_KERNEL);
3496 * if ctrl_loss_tmo is being enforced and the default reconnect delay
3497 * is being used, change to a shorter reconnect delay for FC.
3499 if (opts->max_reconnects != -1 &&
3500 opts->reconnect_delay == NVMF_DEF_RECONNECT_DELAY &&
3501 opts->reconnect_delay > NVME_FC_DEFAULT_RECONNECT_TMO) {
3502 ctrl_loss_tmo = opts->max_reconnects * opts->reconnect_delay;
3503 opts->reconnect_delay = NVME_FC_DEFAULT_RECONNECT_TMO;
3504 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
3505 opts->reconnect_delay);
3508 ctrl->ctrl.opts = opts;
3509 ctrl->ctrl.nr_reconnects = 0;
3511 ctrl->ctrl.numa_node = dev_to_node(lport->dev);
3513 ctrl->ctrl.numa_node = NUMA_NO_NODE;
3514 INIT_LIST_HEAD(&ctrl->ctrl_list);
3515 ctrl->lport = lport;
3516 ctrl->rport = rport;
3517 ctrl->dev = lport->dev;
3519 ctrl->ioq_live = false;
3520 init_waitqueue_head(&ctrl->ioabort_wait);
3522 get_device(ctrl->dev);
3523 kref_init(&ctrl->ref);
3525 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
3526 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
3527 INIT_WORK(&ctrl->ioerr_work, nvme_fc_ctrl_ioerr_work);
3528 spin_lock_init(&ctrl->lock);
3530 /* io queue count */
3531 ctrl->ctrl.queue_count = min_t(unsigned int,
3533 lport->ops->max_hw_queues);
3534 ctrl->ctrl.queue_count++; /* +1 for admin queue */
3536 ctrl->ctrl.sqsize = opts->queue_size - 1;
3537 ctrl->ctrl.kato = opts->kato;
3538 ctrl->ctrl.cntlid = 0xffff;
3541 ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3542 sizeof(struct nvme_fc_queue), GFP_KERNEL);
3546 nvme_fc_init_queue(ctrl, 0);
3548 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
3549 ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
3550 ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
3551 ctrl->admin_tag_set.reserved_tags = NVMF_RESERVED_TAGS;
3552 ctrl->admin_tag_set.numa_node = ctrl->ctrl.numa_node;
3553 ctrl->admin_tag_set.cmd_size =
3554 struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv,
3555 ctrl->lport->ops->fcprqst_priv_sz);
3556 ctrl->admin_tag_set.driver_data = ctrl;
3557 ctrl->admin_tag_set.nr_hw_queues = 1;
3558 ctrl->admin_tag_set.timeout = NVME_ADMIN_TIMEOUT;
3559 ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
3561 ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
3563 goto out_free_queues;
3564 ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
3566 ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
3567 if (IS_ERR(ctrl->ctrl.fabrics_q)) {
3568 ret = PTR_ERR(ctrl->ctrl.fabrics_q);
3569 goto out_free_admin_tag_set;
3572 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
3573 if (IS_ERR(ctrl->ctrl.admin_q)) {
3574 ret = PTR_ERR(ctrl->ctrl.admin_q);
3575 goto out_cleanup_fabrics_q;
3579 * Would have been nice to init io queues tag set as well.
3580 * However, we require interaction from the controller
3581 * for max io queue count before we can do so.
3582 * Defer this to the connect path.
3585 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3587 goto out_cleanup_admin_q;
3589 /* at this point, teardown path changes to ref counting on nvme ctrl */
3591 spin_lock_irqsave(&rport->lock, flags);
3592 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3593 spin_unlock_irqrestore(&rport->lock, flags);
3595 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING) ||
3596 !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
3597 dev_err(ctrl->ctrl.device,
3598 "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
3602 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3603 dev_err(ctrl->ctrl.device,
3604 "NVME-FC{%d}: failed to schedule initial connect\n",
3609 flush_delayed_work(&ctrl->connect_work);
3611 dev_info(ctrl->ctrl.device,
3612 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
3613 ctrl->cnum, nvmf_ctrl_subsysnqn(&ctrl->ctrl));
3618 nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3619 cancel_work_sync(&ctrl->ioerr_work);
3620 cancel_work_sync(&ctrl->ctrl.reset_work);
3621 cancel_delayed_work_sync(&ctrl->connect_work);
3623 ctrl->ctrl.opts = NULL;
3625 /* initiate nvme ctrl ref counting teardown */
3626 nvme_uninit_ctrl(&ctrl->ctrl);
3628 /* Remove core ctrl ref. */
3629 nvme_put_ctrl(&ctrl->ctrl);
3631 /* as we're past the point where we transition to the ref
3632 * counting teardown path, if we return a bad pointer here,
3633 * the calling routine, thinking it's prior to the
3634 * transition, will do an rport put. Since the teardown
3635 * path also does a rport put, we do an extra get here to
3636 * so proper order/teardown happens.
3638 nvme_fc_rport_get(rport);
3640 return ERR_PTR(-EIO);
3642 out_cleanup_admin_q:
3643 blk_mq_destroy_queue(ctrl->ctrl.admin_q);
3644 out_cleanup_fabrics_q:
3645 blk_mq_destroy_queue(ctrl->ctrl.fabrics_q);
3646 out_free_admin_tag_set:
3647 blk_mq_free_tag_set(&ctrl->admin_tag_set);
3649 kfree(ctrl->queues);
3651 put_device(ctrl->dev);
3652 ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
3656 /* exit via here doesn't follow ctlr ref points */
3657 return ERR_PTR(ret);
3661 struct nvmet_fc_traddr {
3667 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3671 if (match_u64(sstr, &token64))
3679 * This routine validates and extracts the WWN's from the TRADDR string.
3680 * As kernel parsers need the 0x to determine number base, universally
3681 * build string to parse with 0x prefix before parsing name strings.
3684 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
3686 char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
3687 substring_t wwn = { name, &name[sizeof(name)-1] };
3688 int nnoffset, pnoffset;
3690 /* validate if string is one of the 2 allowed formats */
3691 if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
3692 !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
3693 !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
3694 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
3695 nnoffset = NVME_FC_TRADDR_OXNNLEN;
3696 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
3697 NVME_FC_TRADDR_OXNNLEN;
3698 } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
3699 !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
3700 !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
3701 "pn-", NVME_FC_TRADDR_NNLEN))) {
3702 nnoffset = NVME_FC_TRADDR_NNLEN;
3703 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
3709 name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
3711 memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3712 if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
3715 memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3716 if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
3722 pr_warn("%s: bad traddr string\n", __func__);
3726 static struct nvme_ctrl *
3727 nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3729 struct nvme_fc_lport *lport;
3730 struct nvme_fc_rport *rport;
3731 struct nvme_ctrl *ctrl;
3732 struct nvmet_fc_traddr laddr = { 0L, 0L };
3733 struct nvmet_fc_traddr raddr = { 0L, 0L };
3734 unsigned long flags;
3737 ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3738 if (ret || !raddr.nn || !raddr.pn)
3739 return ERR_PTR(-EINVAL);
3741 ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3742 if (ret || !laddr.nn || !laddr.pn)
3743 return ERR_PTR(-EINVAL);
3745 /* find the host and remote ports to connect together */
3746 spin_lock_irqsave(&nvme_fc_lock, flags);
3747 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3748 if (lport->localport.node_name != laddr.nn ||
3749 lport->localport.port_name != laddr.pn ||
3750 lport->localport.port_state != FC_OBJSTATE_ONLINE)
3753 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3754 if (rport->remoteport.node_name != raddr.nn ||
3755 rport->remoteport.port_name != raddr.pn ||
3756 rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3759 /* if fail to get reference fall through. Will error */
3760 if (!nvme_fc_rport_get(rport))
3763 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3765 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3767 nvme_fc_rport_put(rport);
3771 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3773 pr_warn("%s: %s - %s combination not found\n",
3774 __func__, opts->traddr, opts->host_traddr);
3775 return ERR_PTR(-ENOENT);
3779 static struct nvmf_transport_ops nvme_fc_transport = {
3781 .module = THIS_MODULE,
3782 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
3783 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3784 .create_ctrl = nvme_fc_create_ctrl,
3787 /* Arbitrary successive failures max. With lots of subsystems could be high */
3788 #define DISCOVERY_MAX_FAIL 20
3790 static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
3791 struct device_attribute *attr, const char *buf, size_t count)
3793 unsigned long flags;
3794 LIST_HEAD(local_disc_list);
3795 struct nvme_fc_lport *lport;
3796 struct nvme_fc_rport *rport;
3799 spin_lock_irqsave(&nvme_fc_lock, flags);
3801 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3802 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3803 if (!nvme_fc_lport_get(lport))
3805 if (!nvme_fc_rport_get(rport)) {
3807 * This is a temporary condition. Upon restart
3808 * this rport will be gone from the list.
3810 * Revert the lport put and retry. Anything
3811 * added to the list already will be skipped (as
3812 * they are no longer list_empty). Loops should
3813 * resume at rports that were not yet seen.
3815 nvme_fc_lport_put(lport);
3817 if (failcnt++ < DISCOVERY_MAX_FAIL)
3820 pr_err("nvme_discovery: too many reference "
3822 goto process_local_list;
3824 if (list_empty(&rport->disc_list))
3825 list_add_tail(&rport->disc_list,
3831 while (!list_empty(&local_disc_list)) {
3832 rport = list_first_entry(&local_disc_list,
3833 struct nvme_fc_rport, disc_list);
3834 list_del_init(&rport->disc_list);
3835 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3837 lport = rport->lport;
3838 /* signal discovery. Won't hurt if it repeats */
3839 nvme_fc_signal_discovery_scan(lport, rport);
3840 nvme_fc_rport_put(rport);
3841 nvme_fc_lport_put(lport);
3843 spin_lock_irqsave(&nvme_fc_lock, flags);
3845 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3850 static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
3852 #ifdef CONFIG_BLK_CGROUP_FC_APPID
3853 /* Parse the cgroup id from a buf and return the length of cgrpid */
3854 static int fc_parse_cgrpid(const char *buf, u64 *id)
3859 memset(cgrp_id, 0x0, sizeof(cgrp_id));
3860 for (cgrpid_len = 0, j = 0; cgrpid_len < 17; cgrpid_len++) {
3861 if (buf[cgrpid_len] != ':')
3862 cgrp_id[cgrpid_len] = buf[cgrpid_len];
3870 if (kstrtou64(cgrp_id, 16, id) < 0)
3876 * Parse and update the appid in the blkcg associated with the cgroupid.
3878 static ssize_t fc_appid_store(struct device *dev,
3879 struct device_attribute *attr, const char *buf, size_t count)
3884 char app_id[FC_APPID_LEN];
3887 if (buf[count-1] == '\n')
3890 if ((count > (16+1+FC_APPID_LEN)) || (!strchr(buf, ':')))
3893 cgrpid_len = fc_parse_cgrpid(buf, &cgrp_id);
3896 appid_len = count - cgrpid_len - 1;
3897 if (appid_len > FC_APPID_LEN)
3900 memset(app_id, 0x0, sizeof(app_id));
3901 memcpy(app_id, &buf[cgrpid_len+1], appid_len);
3902 ret = blkcg_set_fc_appid(app_id, cgrp_id, sizeof(app_id));
3907 static DEVICE_ATTR(appid_store, 0200, NULL, fc_appid_store);
3908 #endif /* CONFIG_BLK_CGROUP_FC_APPID */
3910 static struct attribute *nvme_fc_attrs[] = {
3911 &dev_attr_nvme_discovery.attr,
3912 #ifdef CONFIG_BLK_CGROUP_FC_APPID
3913 &dev_attr_appid_store.attr,
3918 static const struct attribute_group nvme_fc_attr_group = {
3919 .attrs = nvme_fc_attrs,
3922 static const struct attribute_group *nvme_fc_attr_groups[] = {
3923 &nvme_fc_attr_group,
3927 static struct class fc_class = {
3929 .dev_groups = nvme_fc_attr_groups,
3930 .owner = THIS_MODULE,
3933 static int __init nvme_fc_init_module(void)
3937 nvme_fc_wq = alloc_workqueue("nvme_fc_wq", WQ_MEM_RECLAIM, 0);
3943 * It is expected that in the future the kernel will combine
3944 * the FC-isms that are currently under scsi and now being
3945 * added to by NVME into a new standalone FC class. The SCSI
3946 * and NVME protocols and their devices would be under this
3949 * As we need something to post FC-specific udev events to,
3950 * specifically for nvme probe events, start by creating the
3951 * new device class. When the new standalone FC class is
3952 * put in place, this code will move to a more generic
3953 * location for the class.
3955 ret = class_register(&fc_class);
3957 pr_err("couldn't register class fc\n");
3958 goto out_destroy_wq;
3962 * Create a device for the FC-centric udev events
3964 fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
3966 if (IS_ERR(fc_udev_device)) {
3967 pr_err("couldn't create fc_udev device!\n");
3968 ret = PTR_ERR(fc_udev_device);
3969 goto out_destroy_class;
3972 ret = nvmf_register_transport(&nvme_fc_transport);
3974 goto out_destroy_device;
3979 device_destroy(&fc_class, MKDEV(0, 0));
3981 class_unregister(&fc_class);
3983 destroy_workqueue(nvme_fc_wq);
3989 nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
3991 struct nvme_fc_ctrl *ctrl;
3993 spin_lock(&rport->lock);
3994 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3995 dev_warn(ctrl->ctrl.device,
3996 "NVME-FC{%d}: transport unloading: deleting ctrl\n",
3998 nvme_delete_ctrl(&ctrl->ctrl);
4000 spin_unlock(&rport->lock);
4004 nvme_fc_cleanup_for_unload(void)
4006 struct nvme_fc_lport *lport;
4007 struct nvme_fc_rport *rport;
4009 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
4010 list_for_each_entry(rport, &lport->endp_list, endp_list) {
4011 nvme_fc_delete_controllers(rport);
4016 static void __exit nvme_fc_exit_module(void)
4018 unsigned long flags;
4019 bool need_cleanup = false;
4021 spin_lock_irqsave(&nvme_fc_lock, flags);
4022 nvme_fc_waiting_to_unload = true;
4023 if (!list_empty(&nvme_fc_lport_list)) {
4024 need_cleanup = true;
4025 nvme_fc_cleanup_for_unload();
4027 spin_unlock_irqrestore(&nvme_fc_lock, flags);
4029 pr_info("%s: waiting for ctlr deletes\n", __func__);
4030 wait_for_completion(&nvme_fc_unload_proceed);
4031 pr_info("%s: ctrl deletes complete\n", __func__);
4034 nvmf_unregister_transport(&nvme_fc_transport);
4036 ida_destroy(&nvme_fc_local_port_cnt);
4037 ida_destroy(&nvme_fc_ctrl_cnt);
4039 device_destroy(&fc_class, MKDEV(0, 0));
4040 class_unregister(&fc_class);
4041 destroy_workqueue(nvme_fc_wq);
4044 module_init(nvme_fc_init_module);
4045 module_exit(nvme_fc_exit_module);
4047 MODULE_LICENSE("GPL v2");