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>
15 #include <linux/nvme-fc-driver.h>
16 #include <linux/nvme-fc.h>
18 #include <scsi/scsi_transport_fc.h>
20 /* *************************** Data Structures/Defines ****************** */
23 enum nvme_fc_queue_flags {
24 NVME_FC_Q_CONNECTED = 0,
28 #define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */
30 struct nvme_fc_queue {
31 struct nvme_fc_ctrl *ctrl;
33 struct blk_mq_hw_ctx *hctx;
35 size_t cmnd_capsule_len;
44 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
46 enum nvme_fcop_flags {
47 FCOP_FLAGS_TERMIO = (1 << 0),
48 FCOP_FLAGS_AEN = (1 << 1),
51 struct nvmefc_ls_req_op {
52 struct nvmefc_ls_req ls_req;
54 struct nvme_fc_rport *rport;
55 struct nvme_fc_queue *queue;
60 struct completion ls_done;
61 struct list_head lsreq_list; /* rport->ls_req_list */
65 enum nvme_fcpop_state {
66 FCPOP_STATE_UNINIT = 0,
68 FCPOP_STATE_ACTIVE = 2,
69 FCPOP_STATE_ABORTED = 3,
70 FCPOP_STATE_COMPLETE = 4,
73 struct nvme_fc_fcp_op {
74 struct nvme_request nreq; /*
77 * the 1st element in the
82 struct nvmefc_fcp_req fcp_req;
84 struct nvme_fc_ctrl *ctrl;
85 struct nvme_fc_queue *queue;
93 struct nvme_fc_cmd_iu cmd_iu;
94 struct nvme_fc_ersp_iu rsp_iu;
97 struct nvme_fcp_op_w_sgl {
98 struct nvme_fc_fcp_op op;
99 struct scatterlist sgl[NVME_INLINE_SG_CNT];
103 struct nvme_fc_lport {
104 struct nvme_fc_local_port localport;
107 struct list_head port_list; /* nvme_fc_port_list */
108 struct list_head endp_list;
109 struct device *dev; /* physical device for dma */
110 struct nvme_fc_port_template *ops;
112 atomic_t act_rport_cnt;
113 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
115 struct nvme_fc_rport {
116 struct nvme_fc_remote_port remoteport;
118 struct list_head endp_list; /* for lport->endp_list */
119 struct list_head ctrl_list;
120 struct list_head ls_req_list;
121 struct list_head disc_list;
122 struct device *dev; /* physical device for dma */
123 struct nvme_fc_lport *lport;
126 atomic_t act_ctrl_cnt;
127 unsigned long dev_loss_end;
128 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
130 enum nvme_fcctrl_flags {
131 FCCTRL_TERMIO = (1 << 0),
134 struct nvme_fc_ctrl {
136 struct nvme_fc_queue *queues;
138 struct nvme_fc_lport *lport;
139 struct nvme_fc_rport *rport;
144 atomic_t err_work_active;
147 struct list_head ctrl_list; /* rport->ctrl_list */
149 struct blk_mq_tag_set admin_tag_set;
150 struct blk_mq_tag_set tag_set;
152 struct delayed_work connect_work;
153 struct work_struct err_work;
158 wait_queue_head_t ioabort_wait;
160 struct nvme_fc_fcp_op aen_ops[NVME_NR_AEN_COMMANDS];
162 struct nvme_ctrl ctrl;
165 static inline struct nvme_fc_ctrl *
166 to_fc_ctrl(struct nvme_ctrl *ctrl)
168 return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
171 static inline struct nvme_fc_lport *
172 localport_to_lport(struct nvme_fc_local_port *portptr)
174 return container_of(portptr, struct nvme_fc_lport, localport);
177 static inline struct nvme_fc_rport *
178 remoteport_to_rport(struct nvme_fc_remote_port *portptr)
180 return container_of(portptr, struct nvme_fc_rport, remoteport);
183 static inline struct nvmefc_ls_req_op *
184 ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
186 return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
189 static inline struct nvme_fc_fcp_op *
190 fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
192 return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
197 /* *************************** Globals **************************** */
200 static DEFINE_SPINLOCK(nvme_fc_lock);
202 static LIST_HEAD(nvme_fc_lport_list);
203 static DEFINE_IDA(nvme_fc_local_port_cnt);
204 static DEFINE_IDA(nvme_fc_ctrl_cnt);
206 static struct workqueue_struct *nvme_fc_wq;
208 static bool nvme_fc_waiting_to_unload;
209 static DECLARE_COMPLETION(nvme_fc_unload_proceed);
212 * These items are short-term. They will eventually be moved into
213 * a generic FC class. See comments in module init.
215 static struct device *fc_udev_device;
218 /* *********************** FC-NVME Port Management ************************ */
220 static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
221 struct nvme_fc_queue *, unsigned int);
224 nvme_fc_free_lport(struct kref *ref)
226 struct nvme_fc_lport *lport =
227 container_of(ref, struct nvme_fc_lport, ref);
230 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
231 WARN_ON(!list_empty(&lport->endp_list));
233 /* remove from transport list */
234 spin_lock_irqsave(&nvme_fc_lock, flags);
235 list_del(&lport->port_list);
236 if (nvme_fc_waiting_to_unload && list_empty(&nvme_fc_lport_list))
237 complete(&nvme_fc_unload_proceed);
238 spin_unlock_irqrestore(&nvme_fc_lock, flags);
240 ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
241 ida_destroy(&lport->endp_cnt);
243 put_device(lport->dev);
249 nvme_fc_lport_put(struct nvme_fc_lport *lport)
251 kref_put(&lport->ref, nvme_fc_free_lport);
255 nvme_fc_lport_get(struct nvme_fc_lport *lport)
257 return kref_get_unless_zero(&lport->ref);
261 static struct nvme_fc_lport *
262 nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo,
263 struct nvme_fc_port_template *ops,
266 struct nvme_fc_lport *lport;
269 spin_lock_irqsave(&nvme_fc_lock, flags);
271 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
272 if (lport->localport.node_name != pinfo->node_name ||
273 lport->localport.port_name != pinfo->port_name)
276 if (lport->dev != dev) {
277 lport = ERR_PTR(-EXDEV);
281 if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
282 lport = ERR_PTR(-EEXIST);
286 if (!nvme_fc_lport_get(lport)) {
288 * fails if ref cnt already 0. If so,
289 * act as if lport already deleted
295 /* resume the lport */
298 lport->localport.port_role = pinfo->port_role;
299 lport->localport.port_id = pinfo->port_id;
300 lport->localport.port_state = FC_OBJSTATE_ONLINE;
302 spin_unlock_irqrestore(&nvme_fc_lock, flags);
310 spin_unlock_irqrestore(&nvme_fc_lock, flags);
316 * nvme_fc_register_localport - transport entry point called by an
317 * LLDD to register the existence of a NVME
319 * @pinfo: pointer to information about the port to be registered
320 * @template: LLDD entrypoints and operational parameters for the port
321 * @dev: physical hardware device node port corresponds to. Will be
322 * used for DMA mappings
323 * @portptr: pointer to a local port pointer. Upon success, the routine
324 * will allocate a nvme_fc_local_port structure and place its
325 * address in the local port pointer. Upon failure, local port
326 * pointer will be set to 0.
329 * a completion status. Must be 0 upon success; a negative errno
330 * (ex: -ENXIO) upon failure.
333 nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
334 struct nvme_fc_port_template *template,
336 struct nvme_fc_local_port **portptr)
338 struct nvme_fc_lport *newrec;
342 if (!template->localport_delete || !template->remoteport_delete ||
343 !template->ls_req || !template->fcp_io ||
344 !template->ls_abort || !template->fcp_abort ||
345 !template->max_hw_queues || !template->max_sgl_segments ||
346 !template->max_dif_sgl_segments || !template->dma_boundary) {
348 goto out_reghost_failed;
352 * look to see if there is already a localport that had been
353 * deregistered and in the process of waiting for all the
354 * references to fully be removed. If the references haven't
355 * expired, we can simply re-enable the localport. Remoteports
356 * and controller reconnections should resume naturally.
358 newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
360 /* found an lport, but something about its state is bad */
361 if (IS_ERR(newrec)) {
362 ret = PTR_ERR(newrec);
363 goto out_reghost_failed;
365 /* found existing lport, which was resumed */
367 *portptr = &newrec->localport;
371 /* nothing found - allocate a new localport struct */
373 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
377 goto out_reghost_failed;
380 idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
386 if (!get_device(dev) && dev) {
391 INIT_LIST_HEAD(&newrec->port_list);
392 INIT_LIST_HEAD(&newrec->endp_list);
393 kref_init(&newrec->ref);
394 atomic_set(&newrec->act_rport_cnt, 0);
395 newrec->ops = template;
397 ida_init(&newrec->endp_cnt);
398 newrec->localport.private = &newrec[1];
399 newrec->localport.node_name = pinfo->node_name;
400 newrec->localport.port_name = pinfo->port_name;
401 newrec->localport.port_role = pinfo->port_role;
402 newrec->localport.port_id = pinfo->port_id;
403 newrec->localport.port_state = FC_OBJSTATE_ONLINE;
404 newrec->localport.port_num = idx;
406 spin_lock_irqsave(&nvme_fc_lock, flags);
407 list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
408 spin_unlock_irqrestore(&nvme_fc_lock, flags);
411 dma_set_seg_boundary(dev, template->dma_boundary);
413 *portptr = &newrec->localport;
417 ida_simple_remove(&nvme_fc_local_port_cnt, idx);
425 EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
428 * nvme_fc_unregister_localport - transport entry point called by an
429 * LLDD to deregister/remove a previously
430 * registered a NVME host FC port.
431 * @portptr: pointer to the (registered) local port that is to be deregistered.
434 * a completion status. Must be 0 upon success; a negative errno
435 * (ex: -ENXIO) upon failure.
438 nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
440 struct nvme_fc_lport *lport = localport_to_lport(portptr);
446 spin_lock_irqsave(&nvme_fc_lock, flags);
448 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
449 spin_unlock_irqrestore(&nvme_fc_lock, flags);
452 portptr->port_state = FC_OBJSTATE_DELETED;
454 spin_unlock_irqrestore(&nvme_fc_lock, flags);
456 if (atomic_read(&lport->act_rport_cnt) == 0)
457 lport->ops->localport_delete(&lport->localport);
459 nvme_fc_lport_put(lport);
463 EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
466 * TRADDR strings, per FC-NVME are fixed format:
467 * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
468 * udev event will only differ by prefix of what field is
470 * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
471 * 19 + 43 + null_fudge = 64 characters
473 #define FCNVME_TRADDR_LENGTH 64
476 nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
477 struct nvme_fc_rport *rport)
479 char hostaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_HOST_TRADDR=...*/
480 char tgtaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_TRADDR=...*/
481 char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
483 if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
486 snprintf(hostaddr, sizeof(hostaddr),
487 "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
488 lport->localport.node_name, lport->localport.port_name);
489 snprintf(tgtaddr, sizeof(tgtaddr),
490 "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
491 rport->remoteport.node_name, rport->remoteport.port_name);
492 kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
496 nvme_fc_free_rport(struct kref *ref)
498 struct nvme_fc_rport *rport =
499 container_of(ref, struct nvme_fc_rport, ref);
500 struct nvme_fc_lport *lport =
501 localport_to_lport(rport->remoteport.localport);
504 WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
505 WARN_ON(!list_empty(&rport->ctrl_list));
507 /* remove from lport list */
508 spin_lock_irqsave(&nvme_fc_lock, flags);
509 list_del(&rport->endp_list);
510 spin_unlock_irqrestore(&nvme_fc_lock, flags);
512 WARN_ON(!list_empty(&rport->disc_list));
513 ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
517 nvme_fc_lport_put(lport);
521 nvme_fc_rport_put(struct nvme_fc_rport *rport)
523 kref_put(&rport->ref, nvme_fc_free_rport);
527 nvme_fc_rport_get(struct nvme_fc_rport *rport)
529 return kref_get_unless_zero(&rport->ref);
533 nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl)
535 switch (ctrl->ctrl.state) {
537 case NVME_CTRL_CONNECTING:
539 * As all reconnects were suppressed, schedule a
542 dev_info(ctrl->ctrl.device,
543 "NVME-FC{%d}: connectivity re-established. "
544 "Attempting reconnect\n", ctrl->cnum);
546 queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
549 case NVME_CTRL_RESETTING:
551 * Controller is already in the process of terminating the
552 * association. No need to do anything further. The reconnect
553 * step will naturally occur after the reset completes.
558 /* no action to take - let it delete */
563 static struct nvme_fc_rport *
564 nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport,
565 struct nvme_fc_port_info *pinfo)
567 struct nvme_fc_rport *rport;
568 struct nvme_fc_ctrl *ctrl;
571 spin_lock_irqsave(&nvme_fc_lock, flags);
573 list_for_each_entry(rport, &lport->endp_list, endp_list) {
574 if (rport->remoteport.node_name != pinfo->node_name ||
575 rport->remoteport.port_name != pinfo->port_name)
578 if (!nvme_fc_rport_get(rport)) {
579 rport = ERR_PTR(-ENOLCK);
583 spin_unlock_irqrestore(&nvme_fc_lock, flags);
585 spin_lock_irqsave(&rport->lock, flags);
587 /* has it been unregistered */
588 if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) {
589 /* means lldd called us twice */
590 spin_unlock_irqrestore(&rport->lock, flags);
591 nvme_fc_rport_put(rport);
592 return ERR_PTR(-ESTALE);
595 rport->remoteport.port_role = pinfo->port_role;
596 rport->remoteport.port_id = pinfo->port_id;
597 rport->remoteport.port_state = FC_OBJSTATE_ONLINE;
598 rport->dev_loss_end = 0;
601 * kick off a reconnect attempt on all associations to the
602 * remote port. A successful reconnects will resume i/o.
604 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
605 nvme_fc_resume_controller(ctrl);
607 spin_unlock_irqrestore(&rport->lock, flags);
615 spin_unlock_irqrestore(&nvme_fc_lock, flags);
621 __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport,
622 struct nvme_fc_port_info *pinfo)
624 if (pinfo->dev_loss_tmo)
625 rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
627 rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
631 * nvme_fc_register_remoteport - transport entry point called by an
632 * LLDD to register the existence of a NVME
633 * subsystem FC port on its fabric.
634 * @localport: pointer to the (registered) local port that the remote
635 * subsystem port is connected to.
636 * @pinfo: pointer to information about the port to be registered
637 * @portptr: pointer to a remote port pointer. Upon success, the routine
638 * will allocate a nvme_fc_remote_port structure and place its
639 * address in the remote port pointer. Upon failure, remote port
640 * pointer will be set to 0.
643 * a completion status. Must be 0 upon success; a negative errno
644 * (ex: -ENXIO) upon failure.
647 nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
648 struct nvme_fc_port_info *pinfo,
649 struct nvme_fc_remote_port **portptr)
651 struct nvme_fc_lport *lport = localport_to_lport(localport);
652 struct nvme_fc_rport *newrec;
656 if (!nvme_fc_lport_get(lport)) {
658 goto out_reghost_failed;
662 * look to see if there is already a remoteport that is waiting
663 * for a reconnect (within dev_loss_tmo) with the same WWN's.
664 * If so, transition to it and reconnect.
666 newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
668 /* found an rport, but something about its state is bad */
669 if (IS_ERR(newrec)) {
670 ret = PTR_ERR(newrec);
673 /* found existing rport, which was resumed */
675 nvme_fc_lport_put(lport);
676 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
677 nvme_fc_signal_discovery_scan(lport, newrec);
678 *portptr = &newrec->remoteport;
682 /* nothing found - allocate a new remoteport struct */
684 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
691 idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
694 goto out_kfree_rport;
697 INIT_LIST_HEAD(&newrec->endp_list);
698 INIT_LIST_HEAD(&newrec->ctrl_list);
699 INIT_LIST_HEAD(&newrec->ls_req_list);
700 INIT_LIST_HEAD(&newrec->disc_list);
701 kref_init(&newrec->ref);
702 atomic_set(&newrec->act_ctrl_cnt, 0);
703 spin_lock_init(&newrec->lock);
704 newrec->remoteport.localport = &lport->localport;
705 newrec->dev = lport->dev;
706 newrec->lport = lport;
707 newrec->remoteport.private = &newrec[1];
708 newrec->remoteport.port_role = pinfo->port_role;
709 newrec->remoteport.node_name = pinfo->node_name;
710 newrec->remoteport.port_name = pinfo->port_name;
711 newrec->remoteport.port_id = pinfo->port_id;
712 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
713 newrec->remoteport.port_num = idx;
714 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
716 spin_lock_irqsave(&nvme_fc_lock, flags);
717 list_add_tail(&newrec->endp_list, &lport->endp_list);
718 spin_unlock_irqrestore(&nvme_fc_lock, flags);
720 nvme_fc_signal_discovery_scan(lport, newrec);
722 *portptr = &newrec->remoteport;
728 nvme_fc_lport_put(lport);
733 EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
736 nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
738 struct nvmefc_ls_req_op *lsop;
742 spin_lock_irqsave(&rport->lock, flags);
744 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
745 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
746 lsop->flags |= FCOP_FLAGS_TERMIO;
747 spin_unlock_irqrestore(&rport->lock, flags);
748 rport->lport->ops->ls_abort(&rport->lport->localport,
754 spin_unlock_irqrestore(&rport->lock, flags);
760 nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
762 dev_info(ctrl->ctrl.device,
763 "NVME-FC{%d}: controller connectivity lost. Awaiting "
764 "Reconnect", ctrl->cnum);
766 switch (ctrl->ctrl.state) {
770 * Schedule a controller reset. The reset will terminate the
771 * association and schedule the reconnect timer. Reconnects
772 * will be attempted until either the ctlr_loss_tmo
773 * (max_retries * connect_delay) expires or the remoteport's
774 * dev_loss_tmo expires.
776 if (nvme_reset_ctrl(&ctrl->ctrl)) {
777 dev_warn(ctrl->ctrl.device,
778 "NVME-FC{%d}: Couldn't schedule reset.\n",
780 nvme_delete_ctrl(&ctrl->ctrl);
784 case NVME_CTRL_CONNECTING:
786 * The association has already been terminated and the
787 * controller is attempting reconnects. No need to do anything
788 * futher. Reconnects will be attempted until either the
789 * ctlr_loss_tmo (max_retries * connect_delay) expires or the
790 * remoteport's dev_loss_tmo expires.
794 case NVME_CTRL_RESETTING:
796 * Controller is already in the process of terminating the
797 * association. No need to do anything further. The reconnect
798 * step will kick in naturally after the association is
803 case NVME_CTRL_DELETING:
805 /* no action to take - let it delete */
811 * nvme_fc_unregister_remoteport - transport entry point called by an
812 * LLDD to deregister/remove a previously
813 * registered a NVME subsystem FC port.
814 * @portptr: pointer to the (registered) remote port that is to be
818 * a completion status. Must be 0 upon success; a negative errno
819 * (ex: -ENXIO) upon failure.
822 nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
824 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
825 struct nvme_fc_ctrl *ctrl;
831 spin_lock_irqsave(&rport->lock, flags);
833 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
834 spin_unlock_irqrestore(&rport->lock, flags);
837 portptr->port_state = FC_OBJSTATE_DELETED;
839 rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
841 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
842 /* if dev_loss_tmo==0, dev loss is immediate */
843 if (!portptr->dev_loss_tmo) {
844 dev_warn(ctrl->ctrl.device,
845 "NVME-FC{%d}: controller connectivity lost.\n",
847 nvme_delete_ctrl(&ctrl->ctrl);
849 nvme_fc_ctrl_connectivity_loss(ctrl);
852 spin_unlock_irqrestore(&rport->lock, flags);
854 nvme_fc_abort_lsops(rport);
856 if (atomic_read(&rport->act_ctrl_cnt) == 0)
857 rport->lport->ops->remoteport_delete(portptr);
860 * release the reference, which will allow, if all controllers
861 * go away, which should only occur after dev_loss_tmo occurs,
862 * for the rport to be torn down.
864 nvme_fc_rport_put(rport);
868 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
871 * nvme_fc_rescan_remoteport - transport entry point called by an
872 * LLDD to request a nvme device rescan.
873 * @remoteport: pointer to the (registered) remote port that is to be
879 nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
881 struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
883 nvme_fc_signal_discovery_scan(rport->lport, rport);
885 EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
888 nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
891 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
894 spin_lock_irqsave(&rport->lock, flags);
896 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
897 spin_unlock_irqrestore(&rport->lock, flags);
901 /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
902 rport->remoteport.dev_loss_tmo = dev_loss_tmo;
904 spin_unlock_irqrestore(&rport->lock, flags);
908 EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
911 /* *********************** FC-NVME DMA Handling **************************** */
914 * The fcloop device passes in a NULL device pointer. Real LLD's will
915 * pass in a valid device pointer. If NULL is passed to the dma mapping
916 * routines, depending on the platform, it may or may not succeed, and
920 * Wrapper all the dma routines and check the dev pointer.
922 * If simple mappings (return just a dma address, we'll noop them,
923 * returning a dma address of 0.
925 * On more complex mappings (dma_map_sg), a pseudo routine fills
926 * in the scatter list, setting all dma addresses to 0.
929 static inline dma_addr_t
930 fc_dma_map_single(struct device *dev, void *ptr, size_t size,
931 enum dma_data_direction dir)
933 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
937 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
939 return dev ? dma_mapping_error(dev, dma_addr) : 0;
943 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
944 enum dma_data_direction dir)
947 dma_unmap_single(dev, addr, size, dir);
951 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
952 enum dma_data_direction dir)
955 dma_sync_single_for_cpu(dev, addr, size, dir);
959 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
960 enum dma_data_direction dir)
963 dma_sync_single_for_device(dev, addr, size, dir);
966 /* pseudo dma_map_sg call */
968 fc_map_sg(struct scatterlist *sg, int nents)
970 struct scatterlist *s;
973 WARN_ON(nents == 0 || sg[0].length == 0);
975 for_each_sg(sg, s, nents, i) {
977 #ifdef CONFIG_NEED_SG_DMA_LENGTH
978 s->dma_length = s->length;
985 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
986 enum dma_data_direction dir)
988 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
992 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
993 enum dma_data_direction dir)
996 dma_unmap_sg(dev, sg, nents, dir);
999 /* *********************** FC-NVME LS Handling **************************** */
1001 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
1002 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
1006 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
1008 struct nvme_fc_rport *rport = lsop->rport;
1009 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1010 unsigned long flags;
1012 spin_lock_irqsave(&rport->lock, flags);
1014 if (!lsop->req_queued) {
1015 spin_unlock_irqrestore(&rport->lock, flags);
1019 list_del(&lsop->lsreq_list);
1021 lsop->req_queued = false;
1023 spin_unlock_irqrestore(&rport->lock, flags);
1025 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1026 (lsreq->rqstlen + lsreq->rsplen),
1029 nvme_fc_rport_put(rport);
1033 __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
1034 struct nvmefc_ls_req_op *lsop,
1035 void (*done)(struct nvmefc_ls_req *req, int status))
1037 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1038 unsigned long flags;
1041 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1042 return -ECONNREFUSED;
1044 if (!nvme_fc_rport_get(rport))
1048 lsop->rport = rport;
1049 lsop->req_queued = false;
1050 INIT_LIST_HEAD(&lsop->lsreq_list);
1051 init_completion(&lsop->ls_done);
1053 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
1054 lsreq->rqstlen + lsreq->rsplen,
1056 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1060 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1062 spin_lock_irqsave(&rport->lock, flags);
1064 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
1066 lsop->req_queued = true;
1068 spin_unlock_irqrestore(&rport->lock, flags);
1070 ret = rport->lport->ops->ls_req(&rport->lport->localport,
1071 &rport->remoteport, lsreq);
1078 lsop->ls_error = ret;
1079 spin_lock_irqsave(&rport->lock, flags);
1080 lsop->req_queued = false;
1081 list_del(&lsop->lsreq_list);
1082 spin_unlock_irqrestore(&rport->lock, flags);
1083 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1084 (lsreq->rqstlen + lsreq->rsplen),
1087 nvme_fc_rport_put(rport);
1093 nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1095 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1097 lsop->ls_error = status;
1098 complete(&lsop->ls_done);
1102 nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
1104 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1105 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1108 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
1112 * No timeout/not interruptible as we need the struct
1113 * to exist until the lldd calls us back. Thus mandate
1114 * wait until driver calls back. lldd responsible for
1115 * the timeout action
1117 wait_for_completion(&lsop->ls_done);
1119 __nvme_fc_finish_ls_req(lsop);
1121 ret = lsop->ls_error;
1127 /* ACC or RJT payload ? */
1128 if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1135 nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
1136 struct nvmefc_ls_req_op *lsop,
1137 void (*done)(struct nvmefc_ls_req *req, int status))
1139 /* don't wait for completion */
1141 return __nvme_fc_send_ls_req(rport, lsop, done);
1145 nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1146 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1148 struct nvmefc_ls_req_op *lsop;
1149 struct nvmefc_ls_req *lsreq;
1150 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1151 struct fcnvme_ls_cr_assoc_acc *assoc_acc;
1154 lsop = kzalloc((sizeof(*lsop) +
1155 ctrl->lport->ops->lsrqst_priv_sz +
1156 sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
1161 lsreq = &lsop->ls_req;
1163 lsreq->private = (void *)&lsop[1];
1164 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
1165 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1166 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1168 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1169 assoc_rqst->desc_list_len =
1170 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1172 assoc_rqst->assoc_cmd.desc_tag =
1173 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1174 assoc_rqst->assoc_cmd.desc_len =
1176 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1178 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1179 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
1180 /* Linux supports only Dynamic controllers */
1181 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
1182 uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1183 strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1184 min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1185 strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1186 min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
1188 lsop->queue = queue;
1189 lsreq->rqstaddr = assoc_rqst;
1190 lsreq->rqstlen = sizeof(*assoc_rqst);
1191 lsreq->rspaddr = assoc_acc;
1192 lsreq->rsplen = sizeof(*assoc_acc);
1193 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1195 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1197 goto out_free_buffer;
1199 /* process connect LS completion */
1201 /* validate the ACC response */
1202 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1204 else if (assoc_acc->hdr.desc_list_len !=
1206 sizeof(struct fcnvme_ls_cr_assoc_acc)))
1207 fcret = VERR_CR_ASSOC_ACC_LEN;
1208 else if (assoc_acc->hdr.rqst.desc_tag !=
1209 cpu_to_be32(FCNVME_LSDESC_RQST))
1210 fcret = VERR_LSDESC_RQST;
1211 else if (assoc_acc->hdr.rqst.desc_len !=
1212 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1213 fcret = VERR_LSDESC_RQST_LEN;
1214 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1215 fcret = VERR_CR_ASSOC;
1216 else if (assoc_acc->associd.desc_tag !=
1217 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1218 fcret = VERR_ASSOC_ID;
1219 else if (assoc_acc->associd.desc_len !=
1221 sizeof(struct fcnvme_lsdesc_assoc_id)))
1222 fcret = VERR_ASSOC_ID_LEN;
1223 else if (assoc_acc->connectid.desc_tag !=
1224 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1225 fcret = VERR_CONN_ID;
1226 else if (assoc_acc->connectid.desc_len !=
1227 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1228 fcret = VERR_CONN_ID_LEN;
1233 "q %d Create Association LS failed: %s\n",
1234 queue->qnum, validation_errors[fcret]);
1236 ctrl->association_id =
1237 be64_to_cpu(assoc_acc->associd.association_id);
1238 queue->connection_id =
1239 be64_to_cpu(assoc_acc->connectid.connection_id);
1240 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1248 "queue %d connect admin queue failed (%d).\n",
1254 nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1255 u16 qsize, u16 ersp_ratio)
1257 struct nvmefc_ls_req_op *lsop;
1258 struct nvmefc_ls_req *lsreq;
1259 struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1260 struct fcnvme_ls_cr_conn_acc *conn_acc;
1263 lsop = kzalloc((sizeof(*lsop) +
1264 ctrl->lport->ops->lsrqst_priv_sz +
1265 sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
1270 lsreq = &lsop->ls_req;
1272 lsreq->private = (void *)&lsop[1];
1273 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
1274 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1275 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1277 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1278 conn_rqst->desc_list_len = cpu_to_be32(
1279 sizeof(struct fcnvme_lsdesc_assoc_id) +
1280 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1282 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1283 conn_rqst->associd.desc_len =
1285 sizeof(struct fcnvme_lsdesc_assoc_id));
1286 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1287 conn_rqst->connect_cmd.desc_tag =
1288 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1289 conn_rqst->connect_cmd.desc_len =
1291 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1292 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1293 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
1294 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
1296 lsop->queue = queue;
1297 lsreq->rqstaddr = conn_rqst;
1298 lsreq->rqstlen = sizeof(*conn_rqst);
1299 lsreq->rspaddr = conn_acc;
1300 lsreq->rsplen = sizeof(*conn_acc);
1301 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1303 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1305 goto out_free_buffer;
1307 /* process connect LS completion */
1309 /* validate the ACC response */
1310 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1312 else if (conn_acc->hdr.desc_list_len !=
1313 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1314 fcret = VERR_CR_CONN_ACC_LEN;
1315 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1316 fcret = VERR_LSDESC_RQST;
1317 else if (conn_acc->hdr.rqst.desc_len !=
1318 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1319 fcret = VERR_LSDESC_RQST_LEN;
1320 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1321 fcret = VERR_CR_CONN;
1322 else if (conn_acc->connectid.desc_tag !=
1323 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1324 fcret = VERR_CONN_ID;
1325 else if (conn_acc->connectid.desc_len !=
1326 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1327 fcret = VERR_CONN_ID_LEN;
1332 "q %d Create I/O Connection LS failed: %s\n",
1333 queue->qnum, validation_errors[fcret]);
1335 queue->connection_id =
1336 be64_to_cpu(conn_acc->connectid.connection_id);
1337 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1345 "queue %d connect I/O queue failed (%d).\n",
1351 nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1353 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1355 __nvme_fc_finish_ls_req(lsop);
1357 /* fc-nvme initiator doesn't care about success or failure of cmd */
1363 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1364 * the FC-NVME Association. Terminating the association also
1365 * terminates the FC-NVME connections (per queue, both admin and io
1366 * queues) that are part of the association. E.g. things are torn
1367 * down, and the related FC-NVME Association ID and Connection IDs
1370 * The behavior of the fc-nvme initiator is such that it's
1371 * understanding of the association and connections will implicitly
1372 * be torn down. The action is implicit as it may be due to a loss of
1373 * connectivity with the fc-nvme target, so you may never get a
1374 * response even if you tried. As such, the action of this routine
1375 * is to asynchronously send the LS, ignore any results of the LS, and
1376 * continue on with terminating the association. If the fc-nvme target
1377 * is present and receives the LS, it too can tear down.
1380 nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1382 struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
1383 struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
1384 struct nvmefc_ls_req_op *lsop;
1385 struct nvmefc_ls_req *lsreq;
1388 lsop = kzalloc((sizeof(*lsop) +
1389 ctrl->lport->ops->lsrqst_priv_sz +
1390 sizeof(*discon_rqst) + sizeof(*discon_acc)),
1393 /* couldn't sent it... too bad */
1396 lsreq = &lsop->ls_req;
1398 lsreq->private = (void *)&lsop[1];
1399 discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)
1400 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1401 discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
1403 discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT_ASSOC;
1404 discon_rqst->desc_list_len = cpu_to_be32(
1405 sizeof(struct fcnvme_lsdesc_assoc_id) +
1406 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1408 discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1409 discon_rqst->associd.desc_len =
1411 sizeof(struct fcnvme_lsdesc_assoc_id));
1413 discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1415 discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
1416 FCNVME_LSDESC_DISCONN_CMD);
1417 discon_rqst->discon_cmd.desc_len =
1419 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1421 lsreq->rqstaddr = discon_rqst;
1422 lsreq->rqstlen = sizeof(*discon_rqst);
1423 lsreq->rspaddr = discon_acc;
1424 lsreq->rsplen = sizeof(*discon_acc);
1425 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1427 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1428 nvme_fc_disconnect_assoc_done);
1434 * nvme_fc_rcv_ls_req - transport entry point called by an LLDD
1435 * upon the reception of a NVME LS request.
1437 * The nvme-fc layer will copy payload to an internal structure for
1438 * processing. As such, upon completion of the routine, the LLDD may
1439 * immediately free/reuse the LS request buffer passed in the call.
1441 * If this routine returns error, the LLDD should abort the exchange.
1443 * @remoteport: pointer to the (registered) remote port that the LS
1444 * was received from. The remoteport is associated with
1445 * a specific localport.
1446 * @lsrsp: pointer to a nvmefc_ls_rsp response structure to be
1447 * used to reference the exchange corresponding to the LS
1448 * when issuing an ls response.
1449 * @lsreqbuf: pointer to the buffer containing the LS Request
1450 * @lsreqbuf_len: length, in bytes, of the received LS request
1453 nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
1454 struct nvmefc_ls_rsp *lsrsp,
1455 void *lsreqbuf, u32 lsreqbuf_len)
1457 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
1458 struct nvme_fc_lport *lport = rport->lport;
1460 /* validate there's a routine to transmit a response */
1461 if (!lport->ops->xmt_ls_rsp)
1466 EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req);
1469 /* *********************** NVME Ctrl Routines **************************** */
1471 static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
1474 __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1475 struct nvme_fc_fcp_op *op)
1477 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1478 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1479 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1480 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1482 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1486 nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1487 unsigned int hctx_idx)
1489 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1491 return __nvme_fc_exit_request(set->driver_data, op);
1495 __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1497 unsigned long flags;
1500 spin_lock_irqsave(&ctrl->lock, flags);
1501 opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1502 if (opstate != FCPOP_STATE_ACTIVE)
1503 atomic_set(&op->state, opstate);
1504 else if (ctrl->flags & FCCTRL_TERMIO)
1506 spin_unlock_irqrestore(&ctrl->lock, flags);
1508 if (opstate != FCPOP_STATE_ACTIVE)
1511 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1512 &ctrl->rport->remoteport,
1513 op->queue->lldd_handle,
1520 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1522 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
1525 /* ensure we've initialized the ops once */
1526 if (!(aen_op->flags & FCOP_FLAGS_AEN))
1529 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
1530 __nvme_fc_abort_op(ctrl, aen_op);
1534 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1535 struct nvme_fc_fcp_op *op, int opstate)
1537 unsigned long flags;
1539 if (opstate == FCPOP_STATE_ABORTED) {
1540 spin_lock_irqsave(&ctrl->lock, flags);
1541 if (ctrl->flags & FCCTRL_TERMIO) {
1543 wake_up(&ctrl->ioabort_wait);
1545 spin_unlock_irqrestore(&ctrl->lock, flags);
1550 nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1552 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1553 struct request *rq = op->rq;
1554 struct nvmefc_fcp_req *freq = &op->fcp_req;
1555 struct nvme_fc_ctrl *ctrl = op->ctrl;
1556 struct nvme_fc_queue *queue = op->queue;
1557 struct nvme_completion *cqe = &op->rsp_iu.cqe;
1558 struct nvme_command *sqe = &op->cmd_iu.sqe;
1559 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
1560 union nvme_result result;
1561 bool terminate_assoc = true;
1566 * The current linux implementation of a nvme controller
1567 * allocates a single tag set for all io queues and sizes
1568 * the io queues to fully hold all possible tags. Thus, the
1569 * implementation does not reference or care about the sqhd
1570 * value as it never needs to use the sqhd/sqtail pointers
1571 * for submission pacing.
1573 * This affects the FC-NVME implementation in two ways:
1574 * 1) As the value doesn't matter, we don't need to waste
1575 * cycles extracting it from ERSPs and stamping it in the
1576 * cases where the transport fabricates CQEs on successful
1578 * 2) The FC-NVME implementation requires that delivery of
1579 * ERSP completions are to go back to the nvme layer in order
1580 * relative to the rsn, such that the sqhd value will always
1581 * be "in order" for the nvme layer. As the nvme layer in
1582 * linux doesn't care about sqhd, there's no need to return
1586 * As the core nvme layer in linux currently does not look at
1587 * every field in the cqe - in cases where the FC transport must
1588 * fabricate a CQE, the following fields will not be set as they
1589 * are not referenced:
1590 * cqe.sqid, cqe.sqhd, cqe.command_id
1592 * Failure or error of an individual i/o, in a transport
1593 * detected fashion unrelated to the nvme completion status,
1594 * potentially cause the initiator and target sides to get out
1595 * of sync on SQ head/tail (aka outstanding io count allowed).
1596 * Per FC-NVME spec, failure of an individual command requires
1597 * the connection to be terminated, which in turn requires the
1598 * association to be terminated.
1601 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
1603 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1604 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1606 if (opstate == FCPOP_STATE_ABORTED)
1607 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1608 else if (freq->status) {
1609 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1610 dev_info(ctrl->ctrl.device,
1611 "NVME-FC{%d}: io failed due to lldd error %d\n",
1612 ctrl->cnum, freq->status);
1616 * For the linux implementation, if we have an unsuccesful
1617 * status, they blk-mq layer can typically be called with the
1618 * non-zero status and the content of the cqe isn't important.
1624 * command completed successfully relative to the wire
1625 * protocol. However, validate anything received and
1626 * extract the status and result from the cqe (create it
1630 switch (freq->rcv_rsplen) {
1633 case NVME_FC_SIZEOF_ZEROS_RSP:
1635 * No response payload or 12 bytes of payload (which
1636 * should all be zeros) are considered successful and
1637 * no payload in the CQE by the transport.
1639 if (freq->transferred_length !=
1640 be32_to_cpu(op->cmd_iu.data_len)) {
1641 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1642 dev_info(ctrl->ctrl.device,
1643 "NVME-FC{%d}: io failed due to bad transfer "
1644 "length: %d vs expected %d\n",
1645 ctrl->cnum, freq->transferred_length,
1646 be32_to_cpu(op->cmd_iu.data_len));
1652 case sizeof(struct nvme_fc_ersp_iu):
1654 * The ERSP IU contains a full completion with CQE.
1655 * Validate ERSP IU and look at cqe.
1657 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1658 (freq->rcv_rsplen / 4) ||
1659 be32_to_cpu(op->rsp_iu.xfrd_len) !=
1660 freq->transferred_length ||
1661 op->rsp_iu.ersp_result ||
1662 sqe->common.command_id != cqe->command_id)) {
1663 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1664 dev_info(ctrl->ctrl.device,
1665 "NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
1666 "iu len %d, xfr len %d vs %d, status code "
1667 "%d, cmdid %d vs %d\n",
1668 ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len),
1669 be32_to_cpu(op->rsp_iu.xfrd_len),
1670 freq->transferred_length,
1671 op->rsp_iu.ersp_result,
1672 sqe->common.command_id,
1676 result = cqe->result;
1677 status = cqe->status;
1681 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1682 dev_info(ctrl->ctrl.device,
1683 "NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
1685 ctrl->cnum, freq->rcv_rsplen);
1689 terminate_assoc = false;
1692 if (op->flags & FCOP_FLAGS_AEN) {
1693 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
1694 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
1695 atomic_set(&op->state, FCPOP_STATE_IDLE);
1696 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
1697 nvme_fc_ctrl_put(ctrl);
1701 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
1702 nvme_end_request(rq, status, result);
1705 if (terminate_assoc)
1706 nvme_fc_error_recovery(ctrl, "transport detected io error");
1710 __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
1711 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
1712 struct request *rq, u32 rqno)
1714 struct nvme_fcp_op_w_sgl *op_w_sgl =
1715 container_of(op, typeof(*op_w_sgl), op);
1716 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1719 memset(op, 0, sizeof(*op));
1720 op->fcp_req.cmdaddr = &op->cmd_iu;
1721 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
1722 op->fcp_req.rspaddr = &op->rsp_iu;
1723 op->fcp_req.rsplen = sizeof(op->rsp_iu);
1724 op->fcp_req.done = nvme_fc_fcpio_done;
1730 cmdiu->format_id = NVME_CMD_FORMAT_ID;
1731 cmdiu->fc_id = NVME_CMD_FC_ID;
1732 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1734 cmdiu->rsv_cat = fccmnd_set_cat_css(0,
1735 (NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT));
1737 cmdiu->rsv_cat = fccmnd_set_cat_admin(0);
1739 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
1740 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
1741 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
1743 "FCP Op failed - cmdiu dma mapping failed.\n");
1748 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
1749 &op->rsp_iu, sizeof(op->rsp_iu),
1751 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
1753 "FCP Op failed - rspiu dma mapping failed.\n");
1757 atomic_set(&op->state, FCPOP_STATE_IDLE);
1763 nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
1764 unsigned int hctx_idx, unsigned int numa_node)
1766 struct nvme_fc_ctrl *ctrl = set->driver_data;
1767 struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq);
1768 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
1769 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
1772 res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++);
1775 op->op.fcp_req.first_sgl = &op->sgl[0];
1776 op->op.fcp_req.private = &op->priv[0];
1777 nvme_req(rq)->ctrl = &ctrl->ctrl;
1782 nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
1784 struct nvme_fc_fcp_op *aen_op;
1785 struct nvme_fc_cmd_iu *cmdiu;
1786 struct nvme_command *sqe;
1790 aen_op = ctrl->aen_ops;
1791 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
1792 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
1797 cmdiu = &aen_op->cmd_iu;
1799 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
1800 aen_op, (struct request *)NULL,
1801 (NVME_AQ_BLK_MQ_DEPTH + i));
1807 aen_op->flags = FCOP_FLAGS_AEN;
1808 aen_op->fcp_req.private = private;
1810 memset(sqe, 0, sizeof(*sqe));
1811 sqe->common.opcode = nvme_admin_async_event;
1812 /* Note: core layer may overwrite the sqe.command_id value */
1813 sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
1819 nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
1821 struct nvme_fc_fcp_op *aen_op;
1824 aen_op = ctrl->aen_ops;
1825 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
1826 if (!aen_op->fcp_req.private)
1829 __nvme_fc_exit_request(ctrl, aen_op);
1831 kfree(aen_op->fcp_req.private);
1832 aen_op->fcp_req.private = NULL;
1837 __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
1840 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
1842 hctx->driver_data = queue;
1847 nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1848 unsigned int hctx_idx)
1850 struct nvme_fc_ctrl *ctrl = data;
1852 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
1858 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1859 unsigned int hctx_idx)
1861 struct nvme_fc_ctrl *ctrl = data;
1863 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
1869 nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
1871 struct nvme_fc_queue *queue;
1873 queue = &ctrl->queues[idx];
1874 memset(queue, 0, sizeof(*queue));
1877 atomic_set(&queue->csn, 0);
1878 queue->dev = ctrl->dev;
1881 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1883 queue->cmnd_capsule_len = sizeof(struct nvme_command);
1886 * Considered whether we should allocate buffers for all SQEs
1887 * and CQEs and dma map them - mapping their respective entries
1888 * into the request structures (kernel vm addr and dma address)
1889 * thus the driver could use the buffers/mappings directly.
1890 * It only makes sense if the LLDD would use them for its
1891 * messaging api. It's very unlikely most adapter api's would use
1892 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1893 * structures were used instead.
1898 * This routine terminates a queue at the transport level.
1899 * The transport has already ensured that all outstanding ios on
1900 * the queue have been terminated.
1901 * The transport will send a Disconnect LS request to terminate
1902 * the queue's connection. Termination of the admin queue will also
1903 * terminate the association at the target.
1906 nvme_fc_free_queue(struct nvme_fc_queue *queue)
1908 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
1911 clear_bit(NVME_FC_Q_LIVE, &queue->flags);
1913 * Current implementation never disconnects a single queue.
1914 * It always terminates a whole association. So there is never
1915 * a disconnect(queue) LS sent to the target.
1918 queue->connection_id = 0;
1919 atomic_set(&queue->csn, 0);
1923 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
1924 struct nvme_fc_queue *queue, unsigned int qidx)
1926 if (ctrl->lport->ops->delete_queue)
1927 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
1928 queue->lldd_handle);
1929 queue->lldd_handle = NULL;
1933 nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
1937 for (i = 1; i < ctrl->ctrl.queue_count; i++)
1938 nvme_fc_free_queue(&ctrl->queues[i]);
1942 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
1943 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
1947 queue->lldd_handle = NULL;
1948 if (ctrl->lport->ops->create_queue)
1949 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
1950 qidx, qsize, &queue->lldd_handle);
1956 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
1958 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
1961 for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
1962 __nvme_fc_delete_hw_queue(ctrl, queue, i);
1966 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1968 struct nvme_fc_queue *queue = &ctrl->queues[1];
1971 for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
1972 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
1981 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
1986 nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1990 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
1991 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
1995 ret = nvmf_connect_io_queue(&ctrl->ctrl, i, false);
1999 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
2006 nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
2010 for (i = 1; i < ctrl->ctrl.queue_count; i++)
2011 nvme_fc_init_queue(ctrl, i);
2015 nvme_fc_ctrl_free(struct kref *ref)
2017 struct nvme_fc_ctrl *ctrl =
2018 container_of(ref, struct nvme_fc_ctrl, ref);
2019 unsigned long flags;
2021 if (ctrl->ctrl.tagset) {
2022 blk_cleanup_queue(ctrl->ctrl.connect_q);
2023 blk_mq_free_tag_set(&ctrl->tag_set);
2026 /* remove from rport list */
2027 spin_lock_irqsave(&ctrl->rport->lock, flags);
2028 list_del(&ctrl->ctrl_list);
2029 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
2031 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
2032 blk_cleanup_queue(ctrl->ctrl.admin_q);
2033 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
2034 blk_mq_free_tag_set(&ctrl->admin_tag_set);
2036 kfree(ctrl->queues);
2038 put_device(ctrl->dev);
2039 nvme_fc_rport_put(ctrl->rport);
2041 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2042 if (ctrl->ctrl.opts)
2043 nvmf_free_options(ctrl->ctrl.opts);
2048 nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2050 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2054 nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2056 return kref_get_unless_zero(&ctrl->ref);
2060 * All accesses from nvme core layer done - can now free the
2061 * controller. Called after last nvme_put_ctrl() call
2064 nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
2066 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2068 WARN_ON(nctrl != &ctrl->ctrl);
2070 nvme_fc_ctrl_put(ctrl);
2074 nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
2079 * if an error (io timeout, etc) while (re)connecting,
2080 * it's an error on creating the new association.
2081 * Start the error recovery thread if it hasn't already
2082 * been started. It is expected there could be multiple
2083 * ios hitting this path before things are cleaned up.
2085 if (ctrl->ctrl.state == NVME_CTRL_CONNECTING) {
2086 active = atomic_xchg(&ctrl->err_work_active, 1);
2087 if (!active && !queue_work(nvme_fc_wq, &ctrl->err_work)) {
2088 atomic_set(&ctrl->err_work_active, 0);
2094 /* Otherwise, only proceed if in LIVE state - e.g. on first error */
2095 if (ctrl->ctrl.state != NVME_CTRL_LIVE)
2098 dev_warn(ctrl->ctrl.device,
2099 "NVME-FC{%d}: transport association error detected: %s\n",
2100 ctrl->cnum, errmsg);
2101 dev_warn(ctrl->ctrl.device,
2102 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
2104 nvme_reset_ctrl(&ctrl->ctrl);
2107 static enum blk_eh_timer_return
2108 nvme_fc_timeout(struct request *rq, bool reserved)
2110 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2111 struct nvme_fc_ctrl *ctrl = op->ctrl;
2114 * we can't individually ABTS an io without affecting the queue,
2115 * thus killing the queue, and thus the association.
2116 * So resolve by performing a controller reset, which will stop
2117 * the host/io stack, terminate the association on the link,
2118 * and recreate an association on the link.
2120 nvme_fc_error_recovery(ctrl, "io timeout error");
2123 * the io abort has been initiated. Have the reset timer
2124 * restarted and the abort completion will complete the io
2125 * shortly. Avoids a synchronous wait while the abort finishes.
2127 return BLK_EH_RESET_TIMER;
2131 nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2132 struct nvme_fc_fcp_op *op)
2134 struct nvmefc_fcp_req *freq = &op->fcp_req;
2139 if (!blk_rq_nr_phys_segments(rq))
2142 freq->sg_table.sgl = freq->first_sgl;
2143 ret = sg_alloc_table_chained(&freq->sg_table,
2144 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
2145 NVME_INLINE_SG_CNT);
2149 op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
2150 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2151 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2152 op->nents, rq_dma_dir(rq));
2153 if (unlikely(freq->sg_cnt <= 0)) {
2154 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2160 * TODO: blk_integrity_rq(rq) for DIF
2166 nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2167 struct nvme_fc_fcp_op *op)
2169 struct nvmefc_fcp_req *freq = &op->fcp_req;
2174 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2177 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2183 * In FC, the queue is a logical thing. At transport connect, the target
2184 * creates its "queue" and returns a handle that is to be given to the
2185 * target whenever it posts something to the corresponding SQ. When an
2186 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2187 * command contained within the SQE, an io, and assigns a FC exchange
2188 * to it. The SQE and the associated SQ handle are sent in the initial
2189 * CMD IU sents on the exchange. All transfers relative to the io occur
2190 * as part of the exchange. The CQE is the last thing for the io,
2191 * which is transferred (explicitly or implicitly) with the RSP IU
2192 * sent on the exchange. After the CQE is received, the FC exchange is
2193 * terminaed and the Exchange may be used on a different io.
2195 * The transport to LLDD api has the transport making a request for a
2196 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2197 * resource and transfers the command. The LLDD will then process all
2198 * steps to complete the io. Upon completion, the transport done routine
2201 * So - while the operation is outstanding to the LLDD, there is a link
2202 * level FC exchange resource that is also outstanding. This must be
2203 * considered in all cleanup operations.
2206 nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2207 struct nvme_fc_fcp_op *op, u32 data_len,
2208 enum nvmefc_fcp_datadir io_dir)
2210 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2211 struct nvme_command *sqe = &cmdiu->sqe;
2215 * before attempting to send the io, check to see if we believe
2216 * the target device is present
2218 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2219 return BLK_STS_RESOURCE;
2221 if (!nvme_fc_ctrl_get(ctrl))
2222 return BLK_STS_IOERR;
2224 /* format the FC-NVME CMD IU and fcp_req */
2225 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2226 cmdiu->data_len = cpu_to_be32(data_len);
2228 case NVMEFC_FCP_WRITE:
2229 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2231 case NVMEFC_FCP_READ:
2232 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2234 case NVMEFC_FCP_NODATA:
2238 op->fcp_req.payload_length = data_len;
2239 op->fcp_req.io_dir = io_dir;
2240 op->fcp_req.transferred_length = 0;
2241 op->fcp_req.rcv_rsplen = 0;
2242 op->fcp_req.status = NVME_SC_SUCCESS;
2243 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2246 * validate per fabric rules, set fields mandated by fabric spec
2247 * as well as those by FC-NVME spec.
2249 WARN_ON_ONCE(sqe->common.metadata);
2250 sqe->common.flags |= NVME_CMD_SGL_METABUF;
2253 * format SQE DPTR field per FC-NVME rules:
2254 * type=0x5 Transport SGL Data Block Descriptor
2255 * subtype=0xA Transport-specific value
2257 * length=length of the data series
2259 sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2260 NVME_SGL_FMT_TRANSPORT_A;
2261 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2262 sqe->rw.dptr.sgl.addr = 0;
2264 if (!(op->flags & FCOP_FLAGS_AEN)) {
2265 ret = nvme_fc_map_data(ctrl, op->rq, op);
2267 nvme_cleanup_cmd(op->rq);
2268 nvme_fc_ctrl_put(ctrl);
2269 if (ret == -ENOMEM || ret == -EAGAIN)
2270 return BLK_STS_RESOURCE;
2271 return BLK_STS_IOERR;
2275 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2276 sizeof(op->cmd_iu), DMA_TO_DEVICE);
2278 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2280 if (!(op->flags & FCOP_FLAGS_AEN))
2281 blk_mq_start_request(op->rq);
2283 cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2284 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2285 &ctrl->rport->remoteport,
2286 queue->lldd_handle, &op->fcp_req);
2290 * If the lld fails to send the command is there an issue with
2291 * the csn value? If the command that fails is the Connect,
2292 * no - as the connection won't be live. If it is a command
2293 * post-connect, it's possible a gap in csn may be created.
2294 * Does this matter? As Linux initiators don't send fused
2295 * commands, no. The gap would exist, but as there's nothing
2296 * that depends on csn order to be delivered on the target
2297 * side, it shouldn't hurt. It would be difficult for a
2298 * target to even detect the csn gap as it has no idea when the
2299 * cmd with the csn was supposed to arrive.
2301 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2302 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2304 if (!(op->flags & FCOP_FLAGS_AEN))
2305 nvme_fc_unmap_data(ctrl, op->rq, op);
2307 nvme_cleanup_cmd(op->rq);
2308 nvme_fc_ctrl_put(ctrl);
2310 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2312 return BLK_STS_IOERR;
2314 return BLK_STS_RESOURCE;
2321 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2322 const struct blk_mq_queue_data *bd)
2324 struct nvme_ns *ns = hctx->queue->queuedata;
2325 struct nvme_fc_queue *queue = hctx->driver_data;
2326 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2327 struct request *rq = bd->rq;
2328 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2329 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2330 struct nvme_command *sqe = &cmdiu->sqe;
2331 enum nvmefc_fcp_datadir io_dir;
2332 bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2336 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
2337 !nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2338 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
2340 ret = nvme_setup_cmd(ns, rq, sqe);
2345 * nvme core doesn't quite treat the rq opaquely. Commands such
2346 * as WRITE ZEROES will return a non-zero rq payload_bytes yet
2347 * there is no actual payload to be transferred.
2348 * To get it right, key data transmission on there being 1 or
2349 * more physical segments in the sg list. If there is no
2350 * physical segments, there is no payload.
2352 if (blk_rq_nr_phys_segments(rq)) {
2353 data_len = blk_rq_payload_bytes(rq);
2354 io_dir = ((rq_data_dir(rq) == WRITE) ?
2355 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2358 io_dir = NVMEFC_FCP_NODATA;
2362 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2366 nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2368 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2369 struct nvme_fc_fcp_op *aen_op;
2370 unsigned long flags;
2371 bool terminating = false;
2374 spin_lock_irqsave(&ctrl->lock, flags);
2375 if (ctrl->flags & FCCTRL_TERMIO)
2377 spin_unlock_irqrestore(&ctrl->lock, flags);
2382 aen_op = &ctrl->aen_ops[0];
2384 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2387 dev_err(ctrl->ctrl.device,
2388 "failed async event work\n");
2392 nvme_fc_complete_rq(struct request *rq)
2394 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2395 struct nvme_fc_ctrl *ctrl = op->ctrl;
2397 atomic_set(&op->state, FCPOP_STATE_IDLE);
2399 nvme_fc_unmap_data(ctrl, rq, op);
2400 nvme_complete_rq(rq);
2401 nvme_fc_ctrl_put(ctrl);
2405 * This routine is used by the transport when it needs to find active
2406 * io on a queue that is to be terminated. The transport uses
2407 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2408 * this routine to kill them on a 1 by 1 basis.
2410 * As FC allocates FC exchange for each io, the transport must contact
2411 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2412 * After terminating the exchange the LLDD will call the transport's
2413 * normal io done path for the request, but it will have an aborted
2414 * status. The done path will return the io request back to the block
2415 * layer with an error status.
2418 nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2420 struct nvme_ctrl *nctrl = data;
2421 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2422 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2424 __nvme_fc_abort_op(ctrl, op);
2429 static const struct blk_mq_ops nvme_fc_mq_ops = {
2430 .queue_rq = nvme_fc_queue_rq,
2431 .complete = nvme_fc_complete_rq,
2432 .init_request = nvme_fc_init_request,
2433 .exit_request = nvme_fc_exit_request,
2434 .init_hctx = nvme_fc_init_hctx,
2435 .timeout = nvme_fc_timeout,
2439 nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2441 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2442 unsigned int nr_io_queues;
2445 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2446 ctrl->lport->ops->max_hw_queues);
2447 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2449 dev_info(ctrl->ctrl.device,
2450 "set_queue_count failed: %d\n", ret);
2454 ctrl->ctrl.queue_count = nr_io_queues + 1;
2458 nvme_fc_init_io_queues(ctrl);
2460 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2461 ctrl->tag_set.ops = &nvme_fc_mq_ops;
2462 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2463 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2464 ctrl->tag_set.numa_node = ctrl->ctrl.numa_node;
2465 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2466 ctrl->tag_set.cmd_size =
2467 struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv,
2468 ctrl->lport->ops->fcprqst_priv_sz);
2469 ctrl->tag_set.driver_data = ctrl;
2470 ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
2471 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2473 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2477 ctrl->ctrl.tagset = &ctrl->tag_set;
2479 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2480 if (IS_ERR(ctrl->ctrl.connect_q)) {
2481 ret = PTR_ERR(ctrl->ctrl.connect_q);
2482 goto out_free_tag_set;
2485 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2487 goto out_cleanup_blk_queue;
2489 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2491 goto out_delete_hw_queues;
2493 ctrl->ioq_live = true;
2497 out_delete_hw_queues:
2498 nvme_fc_delete_hw_io_queues(ctrl);
2499 out_cleanup_blk_queue:
2500 blk_cleanup_queue(ctrl->ctrl.connect_q);
2502 blk_mq_free_tag_set(&ctrl->tag_set);
2503 nvme_fc_free_io_queues(ctrl);
2505 /* force put free routine to ignore io queues */
2506 ctrl->ctrl.tagset = NULL;
2512 nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
2514 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2515 u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
2516 unsigned int nr_io_queues;
2519 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2520 ctrl->lport->ops->max_hw_queues);
2521 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2523 dev_info(ctrl->ctrl.device,
2524 "set_queue_count failed: %d\n", ret);
2528 if (!nr_io_queues && prior_ioq_cnt) {
2529 dev_info(ctrl->ctrl.device,
2530 "Fail Reconnect: At least 1 io queue "
2531 "required (was %d)\n", prior_ioq_cnt);
2535 ctrl->ctrl.queue_count = nr_io_queues + 1;
2536 /* check for io queues existing */
2537 if (ctrl->ctrl.queue_count == 1)
2540 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2542 goto out_free_io_queues;
2544 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2546 goto out_delete_hw_queues;
2548 if (prior_ioq_cnt != nr_io_queues)
2549 dev_info(ctrl->ctrl.device,
2550 "reconnect: revising io queue count from %d to %d\n",
2551 prior_ioq_cnt, nr_io_queues);
2552 blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2556 out_delete_hw_queues:
2557 nvme_fc_delete_hw_io_queues(ctrl);
2559 nvme_fc_free_io_queues(ctrl);
2564 nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
2566 struct nvme_fc_lport *lport = rport->lport;
2568 atomic_inc(&lport->act_rport_cnt);
2572 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
2574 struct nvme_fc_lport *lport = rport->lport;
2577 cnt = atomic_dec_return(&lport->act_rport_cnt);
2578 if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
2579 lport->ops->localport_delete(&lport->localport);
2583 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
2585 struct nvme_fc_rport *rport = ctrl->rport;
2588 if (ctrl->assoc_active)
2591 ctrl->assoc_active = true;
2592 cnt = atomic_inc_return(&rport->act_ctrl_cnt);
2594 nvme_fc_rport_active_on_lport(rport);
2600 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
2602 struct nvme_fc_rport *rport = ctrl->rport;
2603 struct nvme_fc_lport *lport = rport->lport;
2606 /* ctrl->assoc_active=false will be set independently */
2608 cnt = atomic_dec_return(&rport->act_ctrl_cnt);
2610 if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
2611 lport->ops->remoteport_delete(&rport->remoteport);
2612 nvme_fc_rport_inactive_on_lport(rport);
2619 * This routine restarts the controller on the host side, and
2620 * on the link side, recreates the controller association.
2623 nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
2625 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2629 ++ctrl->ctrl.nr_reconnects;
2631 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2634 if (nvme_fc_ctlr_active_on_rport(ctrl))
2637 dev_info(ctrl->ctrl.device,
2638 "NVME-FC{%d}: create association : host wwpn 0x%016llx "
2639 " rport wwpn 0x%016llx: NQN \"%s\"\n",
2640 ctrl->cnum, ctrl->lport->localport.port_name,
2641 ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
2644 * Create the admin queue
2647 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
2650 goto out_free_queue;
2652 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
2653 NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
2655 goto out_delete_hw_queue;
2657 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
2659 goto out_disconnect_admin_queue;
2661 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
2664 * Check controller capabilities
2666 * todo:- add code to check if ctrl attributes changed from
2667 * prior connection values
2670 ret = nvme_enable_ctrl(&ctrl->ctrl);
2672 goto out_disconnect_admin_queue;
2674 ctrl->ctrl.max_hw_sectors =
2675 (ctrl->lport->ops->max_sgl_segments - 1) << (PAGE_SHIFT - 9);
2677 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
2679 ret = nvme_init_identify(&ctrl->ctrl);
2681 goto out_disconnect_admin_queue;
2685 /* FC-NVME does not have other data in the capsule */
2686 if (ctrl->ctrl.icdoff) {
2687 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
2689 goto out_disconnect_admin_queue;
2692 /* FC-NVME supports normal SGL Data Block Descriptors */
2694 if (opts->queue_size > ctrl->ctrl.maxcmd) {
2695 /* warn if maxcmd is lower than queue_size */
2696 dev_warn(ctrl->ctrl.device,
2697 "queue_size %zu > ctrl maxcmd %u, reducing "
2699 opts->queue_size, ctrl->ctrl.maxcmd);
2700 opts->queue_size = ctrl->ctrl.maxcmd;
2703 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
2704 /* warn if sqsize is lower than queue_size */
2705 dev_warn(ctrl->ctrl.device,
2706 "queue_size %zu > ctrl sqsize %u, reducing "
2708 opts->queue_size, ctrl->ctrl.sqsize + 1);
2709 opts->queue_size = ctrl->ctrl.sqsize + 1;
2712 ret = nvme_fc_init_aen_ops(ctrl);
2714 goto out_term_aen_ops;
2717 * Create the io queues
2720 if (ctrl->ctrl.queue_count > 1) {
2721 if (!ctrl->ioq_live)
2722 ret = nvme_fc_create_io_queues(ctrl);
2724 ret = nvme_fc_recreate_io_queues(ctrl);
2726 goto out_term_aen_ops;
2729 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
2731 ctrl->ctrl.nr_reconnects = 0;
2734 nvme_start_ctrl(&ctrl->ctrl);
2736 return 0; /* Success */
2739 nvme_fc_term_aen_ops(ctrl);
2740 out_disconnect_admin_queue:
2741 /* send a Disconnect(association) LS to fc-nvme target */
2742 nvme_fc_xmt_disconnect_assoc(ctrl);
2743 ctrl->association_id = 0;
2744 out_delete_hw_queue:
2745 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2747 nvme_fc_free_queue(&ctrl->queues[0]);
2748 ctrl->assoc_active = false;
2749 nvme_fc_ctlr_inactive_on_rport(ctrl);
2755 * This routine stops operation of the controller on the host side.
2756 * On the host os stack side: Admin and IO queues are stopped,
2757 * outstanding ios on them terminated via FC ABTS.
2758 * On the link side: the association is terminated.
2761 nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
2763 unsigned long flags;
2765 if (!ctrl->assoc_active)
2767 ctrl->assoc_active = false;
2769 spin_lock_irqsave(&ctrl->lock, flags);
2770 ctrl->flags |= FCCTRL_TERMIO;
2772 spin_unlock_irqrestore(&ctrl->lock, flags);
2775 * If io queues are present, stop them and terminate all outstanding
2776 * ios on them. As FC allocates FC exchange for each io, the
2777 * transport must contact the LLDD to terminate the exchange,
2778 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2779 * to tell us what io's are busy and invoke a transport routine
2780 * to kill them with the LLDD. After terminating the exchange
2781 * the LLDD will call the transport's normal io done path, but it
2782 * will have an aborted status. The done path will return the
2783 * io requests back to the block layer as part of normal completions
2784 * (but with error status).
2786 if (ctrl->ctrl.queue_count > 1) {
2787 nvme_stop_queues(&ctrl->ctrl);
2788 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2789 nvme_fc_terminate_exchange, &ctrl->ctrl);
2790 blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
2794 * Other transports, which don't have link-level contexts bound
2795 * to sqe's, would try to gracefully shutdown the controller by
2796 * writing the registers for shutdown and polling (call
2797 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
2798 * just aborted and we will wait on those contexts, and given
2799 * there was no indication of how live the controlelr is on the
2800 * link, don't send more io to create more contexts for the
2801 * shutdown. Let the controller fail via keepalive failure if
2802 * its still present.
2806 * clean up the admin queue. Same thing as above.
2807 * use blk_mq_tagset_busy_itr() and the transport routine to
2808 * terminate the exchanges.
2810 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
2811 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2812 nvme_fc_terminate_exchange, &ctrl->ctrl);
2813 blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
2815 /* kill the aens as they are a separate path */
2816 nvme_fc_abort_aen_ops(ctrl);
2818 /* wait for all io that had to be aborted */
2819 spin_lock_irq(&ctrl->lock);
2820 wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
2821 ctrl->flags &= ~FCCTRL_TERMIO;
2822 spin_unlock_irq(&ctrl->lock);
2824 nvme_fc_term_aen_ops(ctrl);
2827 * send a Disconnect(association) LS to fc-nvme target
2828 * Note: could have been sent at top of process, but
2829 * cleaner on link traffic if after the aborts complete.
2830 * Note: if association doesn't exist, association_id will be 0
2832 if (ctrl->association_id)
2833 nvme_fc_xmt_disconnect_assoc(ctrl);
2835 ctrl->association_id = 0;
2837 if (ctrl->ctrl.tagset) {
2838 nvme_fc_delete_hw_io_queues(ctrl);
2839 nvme_fc_free_io_queues(ctrl);
2842 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2843 nvme_fc_free_queue(&ctrl->queues[0]);
2845 /* re-enable the admin_q so anything new can fast fail */
2846 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
2848 /* resume the io queues so that things will fast fail */
2849 nvme_start_queues(&ctrl->ctrl);
2851 nvme_fc_ctlr_inactive_on_rport(ctrl);
2855 nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
2857 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2859 cancel_work_sync(&ctrl->err_work);
2860 cancel_delayed_work_sync(&ctrl->connect_work);
2862 * kill the association on the link side. this will block
2863 * waiting for io to terminate
2865 nvme_fc_delete_association(ctrl);
2869 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
2871 struct nvme_fc_rport *rport = ctrl->rport;
2872 struct nvme_fc_remote_port *portptr = &rport->remoteport;
2873 unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
2876 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING)
2879 if (portptr->port_state == FC_OBJSTATE_ONLINE)
2880 dev_info(ctrl->ctrl.device,
2881 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
2882 ctrl->cnum, status);
2883 else if (time_after_eq(jiffies, rport->dev_loss_end))
2886 if (recon && nvmf_should_reconnect(&ctrl->ctrl)) {
2887 if (portptr->port_state == FC_OBJSTATE_ONLINE)
2888 dev_info(ctrl->ctrl.device,
2889 "NVME-FC{%d}: Reconnect attempt in %ld "
2891 ctrl->cnum, recon_delay / HZ);
2892 else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
2893 recon_delay = rport->dev_loss_end - jiffies;
2895 queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
2897 if (portptr->port_state == FC_OBJSTATE_ONLINE)
2898 dev_warn(ctrl->ctrl.device,
2899 "NVME-FC{%d}: Max reconnect attempts (%d) "
2901 ctrl->cnum, ctrl->ctrl.nr_reconnects);
2903 dev_warn(ctrl->ctrl.device,
2904 "NVME-FC{%d}: dev_loss_tmo (%d) expired "
2905 "while waiting for remoteport connectivity.\n",
2906 ctrl->cnum, portptr->dev_loss_tmo);
2907 WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
2912 __nvme_fc_terminate_io(struct nvme_fc_ctrl *ctrl)
2915 * if state is connecting - the error occurred as part of a
2916 * reconnect attempt. The create_association error paths will
2917 * clean up any outstanding io.
2919 * if it's a different state - ensure all pending io is
2920 * terminated. Given this can delay while waiting for the
2921 * aborted io to return, we recheck adapter state below
2922 * before changing state.
2924 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
2925 nvme_stop_keep_alive(&ctrl->ctrl);
2927 /* will block will waiting for io to terminate */
2928 nvme_fc_delete_association(ctrl);
2931 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING &&
2932 !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
2933 dev_err(ctrl->ctrl.device,
2934 "NVME-FC{%d}: error_recovery: Couldn't change state "
2935 "to CONNECTING\n", ctrl->cnum);
2939 nvme_fc_reset_ctrl_work(struct work_struct *work)
2941 struct nvme_fc_ctrl *ctrl =
2942 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
2945 __nvme_fc_terminate_io(ctrl);
2947 nvme_stop_ctrl(&ctrl->ctrl);
2949 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE)
2950 ret = nvme_fc_create_association(ctrl);
2955 nvme_fc_reconnect_or_delete(ctrl, ret);
2957 dev_info(ctrl->ctrl.device,
2958 "NVME-FC{%d}: controller reset complete\n",
2963 nvme_fc_connect_err_work(struct work_struct *work)
2965 struct nvme_fc_ctrl *ctrl =
2966 container_of(work, struct nvme_fc_ctrl, err_work);
2968 __nvme_fc_terminate_io(ctrl);
2970 atomic_set(&ctrl->err_work_active, 0);
2973 * Rescheduling the connection after recovering
2974 * from the io error is left to the reconnect work
2975 * item, which is what should have stalled waiting on
2976 * the io that had the error that scheduled this work.
2980 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
2982 .module = THIS_MODULE,
2983 .flags = NVME_F_FABRICS,
2984 .reg_read32 = nvmf_reg_read32,
2985 .reg_read64 = nvmf_reg_read64,
2986 .reg_write32 = nvmf_reg_write32,
2987 .free_ctrl = nvme_fc_nvme_ctrl_freed,
2988 .submit_async_event = nvme_fc_submit_async_event,
2989 .delete_ctrl = nvme_fc_delete_ctrl,
2990 .get_address = nvmf_get_address,
2994 nvme_fc_connect_ctrl_work(struct work_struct *work)
2998 struct nvme_fc_ctrl *ctrl =
2999 container_of(to_delayed_work(work),
3000 struct nvme_fc_ctrl, connect_work);
3002 ret = nvme_fc_create_association(ctrl);
3004 nvme_fc_reconnect_or_delete(ctrl, ret);
3006 dev_info(ctrl->ctrl.device,
3007 "NVME-FC{%d}: controller connect complete\n",
3012 static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
3013 .queue_rq = nvme_fc_queue_rq,
3014 .complete = nvme_fc_complete_rq,
3015 .init_request = nvme_fc_init_request,
3016 .exit_request = nvme_fc_exit_request,
3017 .init_hctx = nvme_fc_init_admin_hctx,
3018 .timeout = nvme_fc_timeout,
3023 * Fails a controller request if it matches an existing controller
3024 * (association) with the same tuple:
3025 * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
3027 * The ports don't need to be compared as they are intrinsically
3028 * already matched by the port pointers supplied.
3031 nvme_fc_existing_controller(struct nvme_fc_rport *rport,
3032 struct nvmf_ctrl_options *opts)
3034 struct nvme_fc_ctrl *ctrl;
3035 unsigned long flags;
3038 spin_lock_irqsave(&rport->lock, flags);
3039 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3040 found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
3044 spin_unlock_irqrestore(&rport->lock, flags);
3049 static struct nvme_ctrl *
3050 nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3051 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3053 struct nvme_fc_ctrl *ctrl;
3054 unsigned long flags;
3057 if (!(rport->remoteport.port_role &
3058 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
3063 if (!opts->duplicate_connect &&
3064 nvme_fc_existing_controller(rport, opts)) {
3069 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3075 idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
3081 ctrl->ctrl.opts = opts;
3082 ctrl->ctrl.nr_reconnects = 0;
3084 ctrl->ctrl.numa_node = dev_to_node(lport->dev);
3086 ctrl->ctrl.numa_node = NUMA_NO_NODE;
3087 INIT_LIST_HEAD(&ctrl->ctrl_list);
3088 ctrl->lport = lport;
3089 ctrl->rport = rport;
3090 ctrl->dev = lport->dev;
3092 ctrl->ioq_live = false;
3093 ctrl->assoc_active = false;
3094 atomic_set(&ctrl->err_work_active, 0);
3095 init_waitqueue_head(&ctrl->ioabort_wait);
3097 get_device(ctrl->dev);
3098 kref_init(&ctrl->ref);
3100 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
3101 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
3102 INIT_WORK(&ctrl->err_work, nvme_fc_connect_err_work);
3103 spin_lock_init(&ctrl->lock);
3105 /* io queue count */
3106 ctrl->ctrl.queue_count = min_t(unsigned int,
3108 lport->ops->max_hw_queues);
3109 ctrl->ctrl.queue_count++; /* +1 for admin queue */
3111 ctrl->ctrl.sqsize = opts->queue_size - 1;
3112 ctrl->ctrl.kato = opts->kato;
3113 ctrl->ctrl.cntlid = 0xffff;
3116 ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3117 sizeof(struct nvme_fc_queue), GFP_KERNEL);
3121 nvme_fc_init_queue(ctrl, 0);
3123 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
3124 ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
3125 ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
3126 ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
3127 ctrl->admin_tag_set.numa_node = ctrl->ctrl.numa_node;
3128 ctrl->admin_tag_set.cmd_size =
3129 struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv,
3130 ctrl->lport->ops->fcprqst_priv_sz);
3131 ctrl->admin_tag_set.driver_data = ctrl;
3132 ctrl->admin_tag_set.nr_hw_queues = 1;
3133 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
3134 ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
3136 ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
3138 goto out_free_queues;
3139 ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
3141 ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
3142 if (IS_ERR(ctrl->ctrl.fabrics_q)) {
3143 ret = PTR_ERR(ctrl->ctrl.fabrics_q);
3144 goto out_free_admin_tag_set;
3147 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
3148 if (IS_ERR(ctrl->ctrl.admin_q)) {
3149 ret = PTR_ERR(ctrl->ctrl.admin_q);
3150 goto out_cleanup_fabrics_q;
3154 * Would have been nice to init io queues tag set as well.
3155 * However, we require interaction from the controller
3156 * for max io queue count before we can do so.
3157 * Defer this to the connect path.
3160 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3162 goto out_cleanup_admin_q;
3164 /* at this point, teardown path changes to ref counting on nvme ctrl */
3166 spin_lock_irqsave(&rport->lock, flags);
3167 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3168 spin_unlock_irqrestore(&rport->lock, flags);
3170 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING) ||
3171 !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
3172 dev_err(ctrl->ctrl.device,
3173 "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
3177 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3178 dev_err(ctrl->ctrl.device,
3179 "NVME-FC{%d}: failed to schedule initial connect\n",
3184 flush_delayed_work(&ctrl->connect_work);
3186 dev_info(ctrl->ctrl.device,
3187 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
3188 ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
3193 nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3194 cancel_work_sync(&ctrl->ctrl.reset_work);
3195 cancel_work_sync(&ctrl->err_work);
3196 cancel_delayed_work_sync(&ctrl->connect_work);
3198 ctrl->ctrl.opts = NULL;
3200 /* initiate nvme ctrl ref counting teardown */
3201 nvme_uninit_ctrl(&ctrl->ctrl);
3203 /* Remove core ctrl ref. */
3204 nvme_put_ctrl(&ctrl->ctrl);
3206 /* as we're past the point where we transition to the ref
3207 * counting teardown path, if we return a bad pointer here,
3208 * the calling routine, thinking it's prior to the
3209 * transition, will do an rport put. Since the teardown
3210 * path also does a rport put, we do an extra get here to
3211 * so proper order/teardown happens.
3213 nvme_fc_rport_get(rport);
3215 return ERR_PTR(-EIO);
3217 out_cleanup_admin_q:
3218 blk_cleanup_queue(ctrl->ctrl.admin_q);
3219 out_cleanup_fabrics_q:
3220 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
3221 out_free_admin_tag_set:
3222 blk_mq_free_tag_set(&ctrl->admin_tag_set);
3224 kfree(ctrl->queues);
3226 put_device(ctrl->dev);
3227 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
3231 /* exit via here doesn't follow ctlr ref points */
3232 return ERR_PTR(ret);
3236 struct nvmet_fc_traddr {
3242 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3246 if (match_u64(sstr, &token64))
3254 * This routine validates and extracts the WWN's from the TRADDR string.
3255 * As kernel parsers need the 0x to determine number base, universally
3256 * build string to parse with 0x prefix before parsing name strings.
3259 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
3261 char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
3262 substring_t wwn = { name, &name[sizeof(name)-1] };
3263 int nnoffset, pnoffset;
3265 /* validate if string is one of the 2 allowed formats */
3266 if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
3267 !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
3268 !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
3269 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
3270 nnoffset = NVME_FC_TRADDR_OXNNLEN;
3271 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
3272 NVME_FC_TRADDR_OXNNLEN;
3273 } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
3274 !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
3275 !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
3276 "pn-", NVME_FC_TRADDR_NNLEN))) {
3277 nnoffset = NVME_FC_TRADDR_NNLEN;
3278 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
3284 name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
3286 memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3287 if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
3290 memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3291 if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
3297 pr_warn("%s: bad traddr string\n", __func__);
3301 static struct nvme_ctrl *
3302 nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3304 struct nvme_fc_lport *lport;
3305 struct nvme_fc_rport *rport;
3306 struct nvme_ctrl *ctrl;
3307 struct nvmet_fc_traddr laddr = { 0L, 0L };
3308 struct nvmet_fc_traddr raddr = { 0L, 0L };
3309 unsigned long flags;
3312 ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3313 if (ret || !raddr.nn || !raddr.pn)
3314 return ERR_PTR(-EINVAL);
3316 ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3317 if (ret || !laddr.nn || !laddr.pn)
3318 return ERR_PTR(-EINVAL);
3320 /* find the host and remote ports to connect together */
3321 spin_lock_irqsave(&nvme_fc_lock, flags);
3322 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3323 if (lport->localport.node_name != laddr.nn ||
3324 lport->localport.port_name != laddr.pn)
3327 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3328 if (rport->remoteport.node_name != raddr.nn ||
3329 rport->remoteport.port_name != raddr.pn)
3332 /* if fail to get reference fall through. Will error */
3333 if (!nvme_fc_rport_get(rport))
3336 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3338 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3340 nvme_fc_rport_put(rport);
3344 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3346 pr_warn("%s: %s - %s combination not found\n",
3347 __func__, opts->traddr, opts->host_traddr);
3348 return ERR_PTR(-ENOENT);
3352 static struct nvmf_transport_ops nvme_fc_transport = {
3354 .module = THIS_MODULE,
3355 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
3356 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3357 .create_ctrl = nvme_fc_create_ctrl,
3360 /* Arbitrary successive failures max. With lots of subsystems could be high */
3361 #define DISCOVERY_MAX_FAIL 20
3363 static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
3364 struct device_attribute *attr, const char *buf, size_t count)
3366 unsigned long flags;
3367 LIST_HEAD(local_disc_list);
3368 struct nvme_fc_lport *lport;
3369 struct nvme_fc_rport *rport;
3372 spin_lock_irqsave(&nvme_fc_lock, flags);
3374 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3375 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3376 if (!nvme_fc_lport_get(lport))
3378 if (!nvme_fc_rport_get(rport)) {
3380 * This is a temporary condition. Upon restart
3381 * this rport will be gone from the list.
3383 * Revert the lport put and retry. Anything
3384 * added to the list already will be skipped (as
3385 * they are no longer list_empty). Loops should
3386 * resume at rports that were not yet seen.
3388 nvme_fc_lport_put(lport);
3390 if (failcnt++ < DISCOVERY_MAX_FAIL)
3393 pr_err("nvme_discovery: too many reference "
3395 goto process_local_list;
3397 if (list_empty(&rport->disc_list))
3398 list_add_tail(&rport->disc_list,
3404 while (!list_empty(&local_disc_list)) {
3405 rport = list_first_entry(&local_disc_list,
3406 struct nvme_fc_rport, disc_list);
3407 list_del_init(&rport->disc_list);
3408 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3410 lport = rport->lport;
3411 /* signal discovery. Won't hurt if it repeats */
3412 nvme_fc_signal_discovery_scan(lport, rport);
3413 nvme_fc_rport_put(rport);
3414 nvme_fc_lport_put(lport);
3416 spin_lock_irqsave(&nvme_fc_lock, flags);
3418 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3422 static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
3424 static struct attribute *nvme_fc_attrs[] = {
3425 &dev_attr_nvme_discovery.attr,
3429 static struct attribute_group nvme_fc_attr_group = {
3430 .attrs = nvme_fc_attrs,
3433 static const struct attribute_group *nvme_fc_attr_groups[] = {
3434 &nvme_fc_attr_group,
3438 static struct class fc_class = {
3440 .dev_groups = nvme_fc_attr_groups,
3441 .owner = THIS_MODULE,
3444 static int __init nvme_fc_init_module(void)
3448 nvme_fc_wq = alloc_workqueue("nvme_fc_wq", WQ_MEM_RECLAIM, 0);
3454 * It is expected that in the future the kernel will combine
3455 * the FC-isms that are currently under scsi and now being
3456 * added to by NVME into a new standalone FC class. The SCSI
3457 * and NVME protocols and their devices would be under this
3460 * As we need something to post FC-specific udev events to,
3461 * specifically for nvme probe events, start by creating the
3462 * new device class. When the new standalone FC class is
3463 * put in place, this code will move to a more generic
3464 * location for the class.
3466 ret = class_register(&fc_class);
3468 pr_err("couldn't register class fc\n");
3469 goto out_destroy_wq;
3473 * Create a device for the FC-centric udev events
3475 fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
3477 if (IS_ERR(fc_udev_device)) {
3478 pr_err("couldn't create fc_udev device!\n");
3479 ret = PTR_ERR(fc_udev_device);
3480 goto out_destroy_class;
3483 ret = nvmf_register_transport(&nvme_fc_transport);
3485 goto out_destroy_device;
3490 device_destroy(&fc_class, MKDEV(0, 0));
3492 class_unregister(&fc_class);
3494 destroy_workqueue(nvme_fc_wq);
3500 nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
3502 struct nvme_fc_ctrl *ctrl;
3504 spin_lock(&rport->lock);
3505 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3506 dev_warn(ctrl->ctrl.device,
3507 "NVME-FC{%d}: transport unloading: deleting ctrl\n",
3509 nvme_delete_ctrl(&ctrl->ctrl);
3511 spin_unlock(&rport->lock);
3515 nvme_fc_cleanup_for_unload(void)
3517 struct nvme_fc_lport *lport;
3518 struct nvme_fc_rport *rport;
3520 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3521 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3522 nvme_fc_delete_controllers(rport);
3527 static void __exit nvme_fc_exit_module(void)
3529 unsigned long flags;
3530 bool need_cleanup = false;
3532 spin_lock_irqsave(&nvme_fc_lock, flags);
3533 nvme_fc_waiting_to_unload = true;
3534 if (!list_empty(&nvme_fc_lport_list)) {
3535 need_cleanup = true;
3536 nvme_fc_cleanup_for_unload();
3538 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3540 pr_info("%s: waiting for ctlr deletes\n", __func__);
3541 wait_for_completion(&nvme_fc_unload_proceed);
3542 pr_info("%s: ctrl deletes complete\n", __func__);
3545 nvmf_unregister_transport(&nvme_fc_transport);
3547 ida_destroy(&nvme_fc_local_port_cnt);
3548 ida_destroy(&nvme_fc_ctrl_cnt);
3550 device_destroy(&fc_class, MKDEV(0, 0));
3551 class_unregister(&fc_class);
3552 destroy_workqueue(nvme_fc_wq);
3555 module_init(nvme_fc_init_module);
3556 module_exit(nvme_fc_exit_module);
3558 MODULE_LICENSE("GPL v2");