2 * Copyright (c) 2016 Avago Technologies. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful.
9 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
10 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
11 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO
12 * THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
13 * See the GNU General Public License for more details, a copy of which
14 * can be found in the file COPYING included with this package
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/parser.h>
20 #include <uapi/scsi/fc/fc_fs.h>
21 #include <uapi/scsi/fc/fc_els.h>
22 #include <linux/delay.h>
26 #include <linux/nvme-fc-driver.h>
27 #include <linux/nvme-fc.h>
30 /* *************************** Data Structures/Defines ****************** */
33 enum nvme_fc_queue_flags {
34 NVME_FC_Q_CONNECTED = 0,
38 #define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */
40 struct nvme_fc_queue {
41 struct nvme_fc_ctrl *ctrl;
43 struct blk_mq_hw_ctx *hctx;
45 size_t cmnd_capsule_len;
54 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
56 enum nvme_fcop_flags {
57 FCOP_FLAGS_TERMIO = (1 << 0),
58 FCOP_FLAGS_AEN = (1 << 1),
61 struct nvmefc_ls_req_op {
62 struct nvmefc_ls_req ls_req;
64 struct nvme_fc_rport *rport;
65 struct nvme_fc_queue *queue;
70 struct completion ls_done;
71 struct list_head lsreq_list; /* rport->ls_req_list */
75 enum nvme_fcpop_state {
76 FCPOP_STATE_UNINIT = 0,
78 FCPOP_STATE_ACTIVE = 2,
79 FCPOP_STATE_ABORTED = 3,
80 FCPOP_STATE_COMPLETE = 4,
83 struct nvme_fc_fcp_op {
84 struct nvme_request nreq; /*
87 * the 1st element in the
92 struct nvmefc_fcp_req fcp_req;
94 struct nvme_fc_ctrl *ctrl;
95 struct nvme_fc_queue *queue;
103 struct nvme_fc_cmd_iu cmd_iu;
104 struct nvme_fc_ersp_iu rsp_iu;
107 struct nvme_fc_lport {
108 struct nvme_fc_local_port localport;
111 struct list_head port_list; /* nvme_fc_port_list */
112 struct list_head endp_list;
113 struct device *dev; /* physical device for dma */
114 struct nvme_fc_port_template *ops;
116 atomic_t act_rport_cnt;
117 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
119 struct nvme_fc_rport {
120 struct nvme_fc_remote_port remoteport;
122 struct list_head endp_list; /* for lport->endp_list */
123 struct list_head ctrl_list;
124 struct list_head ls_req_list;
125 struct device *dev; /* physical device for dma */
126 struct nvme_fc_lport *lport;
129 atomic_t act_ctrl_cnt;
130 unsigned long dev_loss_end;
131 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
133 enum nvme_fcctrl_flags {
134 FCCTRL_TERMIO = (1 << 0),
137 struct nvme_fc_ctrl {
139 struct nvme_fc_queue *queues;
141 struct nvme_fc_lport *lport;
142 struct nvme_fc_rport *rport;
147 atomic_t err_work_active;
150 struct list_head ctrl_list; /* rport->ctrl_list */
152 struct blk_mq_tag_set admin_tag_set;
153 struct blk_mq_tag_set tag_set;
155 struct delayed_work connect_work;
156 struct work_struct err_work;
161 wait_queue_head_t ioabort_wait;
163 struct nvme_fc_fcp_op aen_ops[NVME_NR_AEN_COMMANDS];
165 struct nvme_ctrl ctrl;
168 static inline struct nvme_fc_ctrl *
169 to_fc_ctrl(struct nvme_ctrl *ctrl)
171 return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
174 static inline struct nvme_fc_lport *
175 localport_to_lport(struct nvme_fc_local_port *portptr)
177 return container_of(portptr, struct nvme_fc_lport, localport);
180 static inline struct nvme_fc_rport *
181 remoteport_to_rport(struct nvme_fc_remote_port *portptr)
183 return container_of(portptr, struct nvme_fc_rport, remoteport);
186 static inline struct nvmefc_ls_req_op *
187 ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
189 return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
192 static inline struct nvme_fc_fcp_op *
193 fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
195 return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
200 /* *************************** Globals **************************** */
203 static DEFINE_SPINLOCK(nvme_fc_lock);
205 static LIST_HEAD(nvme_fc_lport_list);
206 static DEFINE_IDA(nvme_fc_local_port_cnt);
207 static DEFINE_IDA(nvme_fc_ctrl_cnt);
209 static struct workqueue_struct *nvme_fc_wq;
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 class *fc_class;
216 static struct device *fc_udev_device;
219 /* *********************** FC-NVME Port Management ************************ */
221 static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
222 struct nvme_fc_queue *, unsigned int);
225 nvme_fc_free_lport(struct kref *ref)
227 struct nvme_fc_lport *lport =
228 container_of(ref, struct nvme_fc_lport, ref);
231 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
232 WARN_ON(!list_empty(&lport->endp_list));
234 /* remove from transport list */
235 spin_lock_irqsave(&nvme_fc_lock, flags);
236 list_del(&lport->port_list);
237 spin_unlock_irqrestore(&nvme_fc_lock, flags);
239 ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
240 ida_destroy(&lport->endp_cnt);
242 put_device(lport->dev);
248 nvme_fc_lport_put(struct nvme_fc_lport *lport)
250 kref_put(&lport->ref, nvme_fc_free_lport);
254 nvme_fc_lport_get(struct nvme_fc_lport *lport)
256 return kref_get_unless_zero(&lport->ref);
260 static struct nvme_fc_lport *
261 nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo,
262 struct nvme_fc_port_template *ops,
265 struct nvme_fc_lport *lport;
268 spin_lock_irqsave(&nvme_fc_lock, flags);
270 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
271 if (lport->localport.node_name != pinfo->node_name ||
272 lport->localport.port_name != pinfo->port_name)
275 if (lport->dev != dev) {
276 lport = ERR_PTR(-EXDEV);
280 if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
281 lport = ERR_PTR(-EEXIST);
285 if (!nvme_fc_lport_get(lport)) {
287 * fails if ref cnt already 0. If so,
288 * act as if lport already deleted
294 /* resume the lport */
297 lport->localport.port_role = pinfo->port_role;
298 lport->localport.port_id = pinfo->port_id;
299 lport->localport.port_state = FC_OBJSTATE_ONLINE;
301 spin_unlock_irqrestore(&nvme_fc_lock, flags);
309 spin_unlock_irqrestore(&nvme_fc_lock, flags);
315 * nvme_fc_register_localport - transport entry point called by an
316 * LLDD to register the existence of a NVME
318 * @pinfo: pointer to information about the port to be registered
319 * @template: LLDD entrypoints and operational parameters for the port
320 * @dev: physical hardware device node port corresponds to. Will be
321 * used for DMA mappings
322 * @lport_p: pointer to a local port pointer. Upon success, the routine
323 * will allocate a nvme_fc_local_port structure and place its
324 * address in the local port pointer. Upon failure, local port
325 * pointer will be set to 0.
328 * a completion status. Must be 0 upon success; a negative errno
329 * (ex: -ENXIO) upon failure.
332 nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
333 struct nvme_fc_port_template *template,
335 struct nvme_fc_local_port **portptr)
337 struct nvme_fc_lport *newrec;
341 if (!template->localport_delete || !template->remoteport_delete ||
342 !template->ls_req || !template->fcp_io ||
343 !template->ls_abort || !template->fcp_abort ||
344 !template->max_hw_queues || !template->max_sgl_segments ||
345 !template->max_dif_sgl_segments || !template->dma_boundary) {
347 goto out_reghost_failed;
351 * look to see if there is already a localport that had been
352 * deregistered and in the process of waiting for all the
353 * references to fully be removed. If the references haven't
354 * expired, we can simply re-enable the localport. Remoteports
355 * and controller reconnections should resume naturally.
357 newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
359 /* found an lport, but something about its state is bad */
360 if (IS_ERR(newrec)) {
361 ret = PTR_ERR(newrec);
362 goto out_reghost_failed;
364 /* found existing lport, which was resumed */
366 *portptr = &newrec->localport;
370 /* nothing found - allocate a new localport struct */
372 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
376 goto out_reghost_failed;
379 idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
385 if (!get_device(dev) && dev) {
390 INIT_LIST_HEAD(&newrec->port_list);
391 INIT_LIST_HEAD(&newrec->endp_list);
392 kref_init(&newrec->ref);
393 atomic_set(&newrec->act_rport_cnt, 0);
394 newrec->ops = template;
396 ida_init(&newrec->endp_cnt);
397 newrec->localport.private = &newrec[1];
398 newrec->localport.node_name = pinfo->node_name;
399 newrec->localport.port_name = pinfo->port_name;
400 newrec->localport.port_role = pinfo->port_role;
401 newrec->localport.port_id = pinfo->port_id;
402 newrec->localport.port_state = FC_OBJSTATE_ONLINE;
403 newrec->localport.port_num = idx;
405 spin_lock_irqsave(&nvme_fc_lock, flags);
406 list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
407 spin_unlock_irqrestore(&nvme_fc_lock, flags);
410 dma_set_seg_boundary(dev, template->dma_boundary);
412 *portptr = &newrec->localport;
416 ida_simple_remove(&nvme_fc_local_port_cnt, idx);
424 EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
427 * nvme_fc_unregister_localport - transport entry point called by an
428 * LLDD to deregister/remove a previously
429 * registered a NVME host FC port.
430 * @localport: pointer to the (registered) local port that is to be
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 ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
516 nvme_fc_lport_put(lport);
520 nvme_fc_rport_put(struct nvme_fc_rport *rport)
522 kref_put(&rport->ref, nvme_fc_free_rport);
526 nvme_fc_rport_get(struct nvme_fc_rport *rport)
528 return kref_get_unless_zero(&rport->ref);
532 nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl)
534 switch (ctrl->ctrl.state) {
536 case NVME_CTRL_CONNECTING:
538 * As all reconnects were suppressed, schedule a
541 dev_info(ctrl->ctrl.device,
542 "NVME-FC{%d}: connectivity re-established. "
543 "Attempting reconnect\n", ctrl->cnum);
545 queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
548 case NVME_CTRL_RESETTING:
550 * Controller is already in the process of terminating the
551 * association. No need to do anything further. The reconnect
552 * step will naturally occur after the reset completes.
557 /* no action to take - let it delete */
562 static struct nvme_fc_rport *
563 nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport,
564 struct nvme_fc_port_info *pinfo)
566 struct nvme_fc_rport *rport;
567 struct nvme_fc_ctrl *ctrl;
570 spin_lock_irqsave(&nvme_fc_lock, flags);
572 list_for_each_entry(rport, &lport->endp_list, endp_list) {
573 if (rport->remoteport.node_name != pinfo->node_name ||
574 rport->remoteport.port_name != pinfo->port_name)
577 if (!nvme_fc_rport_get(rport)) {
578 rport = ERR_PTR(-ENOLCK);
582 spin_unlock_irqrestore(&nvme_fc_lock, flags);
584 spin_lock_irqsave(&rport->lock, flags);
586 /* has it been unregistered */
587 if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) {
588 /* means lldd called us twice */
589 spin_unlock_irqrestore(&rport->lock, flags);
590 nvme_fc_rport_put(rport);
591 return ERR_PTR(-ESTALE);
594 rport->remoteport.port_role = pinfo->port_role;
595 rport->remoteport.port_id = pinfo->port_id;
596 rport->remoteport.port_state = FC_OBJSTATE_ONLINE;
597 rport->dev_loss_end = 0;
600 * kick off a reconnect attempt on all associations to the
601 * remote port. A successful reconnects will resume i/o.
603 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
604 nvme_fc_resume_controller(ctrl);
606 spin_unlock_irqrestore(&rport->lock, flags);
614 spin_unlock_irqrestore(&nvme_fc_lock, flags);
620 __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport,
621 struct nvme_fc_port_info *pinfo)
623 if (pinfo->dev_loss_tmo)
624 rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
626 rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
630 * nvme_fc_register_remoteport - transport entry point called by an
631 * LLDD to register the existence of a NVME
632 * subsystem FC port on its fabric.
633 * @localport: pointer to the (registered) local port that the remote
634 * subsystem port is connected to.
635 * @pinfo: pointer to information about the port to be registered
636 * @rport_p: pointer to a remote port pointer. Upon success, the routine
637 * will allocate a nvme_fc_remote_port structure and place its
638 * address in the remote port pointer. Upon failure, remote port
639 * pointer will be set to 0.
642 * a completion status. Must be 0 upon success; a negative errno
643 * (ex: -ENXIO) upon failure.
646 nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
647 struct nvme_fc_port_info *pinfo,
648 struct nvme_fc_remote_port **portptr)
650 struct nvme_fc_lport *lport = localport_to_lport(localport);
651 struct nvme_fc_rport *newrec;
655 if (!nvme_fc_lport_get(lport)) {
657 goto out_reghost_failed;
661 * look to see if there is already a remoteport that is waiting
662 * for a reconnect (within dev_loss_tmo) with the same WWN's.
663 * If so, transition to it and reconnect.
665 newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
667 /* found an rport, but something about its state is bad */
668 if (IS_ERR(newrec)) {
669 ret = PTR_ERR(newrec);
672 /* found existing rport, which was resumed */
674 nvme_fc_lport_put(lport);
675 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
676 nvme_fc_signal_discovery_scan(lport, newrec);
677 *portptr = &newrec->remoteport;
681 /* nothing found - allocate a new remoteport struct */
683 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
690 idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
693 goto out_kfree_rport;
696 INIT_LIST_HEAD(&newrec->endp_list);
697 INIT_LIST_HEAD(&newrec->ctrl_list);
698 INIT_LIST_HEAD(&newrec->ls_req_list);
699 kref_init(&newrec->ref);
700 atomic_set(&newrec->act_ctrl_cnt, 0);
701 spin_lock_init(&newrec->lock);
702 newrec->remoteport.localport = &lport->localport;
703 newrec->dev = lport->dev;
704 newrec->lport = lport;
705 newrec->remoteport.private = &newrec[1];
706 newrec->remoteport.port_role = pinfo->port_role;
707 newrec->remoteport.node_name = pinfo->node_name;
708 newrec->remoteport.port_name = pinfo->port_name;
709 newrec->remoteport.port_id = pinfo->port_id;
710 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
711 newrec->remoteport.port_num = idx;
712 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
714 spin_lock_irqsave(&nvme_fc_lock, flags);
715 list_add_tail(&newrec->endp_list, &lport->endp_list);
716 spin_unlock_irqrestore(&nvme_fc_lock, flags);
718 nvme_fc_signal_discovery_scan(lport, newrec);
720 *portptr = &newrec->remoteport;
726 nvme_fc_lport_put(lport);
731 EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
734 nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
736 struct nvmefc_ls_req_op *lsop;
740 spin_lock_irqsave(&rport->lock, flags);
742 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
743 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
744 lsop->flags |= FCOP_FLAGS_TERMIO;
745 spin_unlock_irqrestore(&rport->lock, flags);
746 rport->lport->ops->ls_abort(&rport->lport->localport,
752 spin_unlock_irqrestore(&rport->lock, flags);
758 nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
760 dev_info(ctrl->ctrl.device,
761 "NVME-FC{%d}: controller connectivity lost. Awaiting "
762 "Reconnect", ctrl->cnum);
764 switch (ctrl->ctrl.state) {
768 * Schedule a controller reset. The reset will terminate the
769 * association and schedule the reconnect timer. Reconnects
770 * will be attempted until either the ctlr_loss_tmo
771 * (max_retries * connect_delay) expires or the remoteport's
772 * dev_loss_tmo expires.
774 if (nvme_reset_ctrl(&ctrl->ctrl)) {
775 dev_warn(ctrl->ctrl.device,
776 "NVME-FC{%d}: Couldn't schedule reset.\n",
778 nvme_delete_ctrl(&ctrl->ctrl);
782 case NVME_CTRL_CONNECTING:
784 * The association has already been terminated and the
785 * controller is attempting reconnects. No need to do anything
786 * futher. Reconnects will be attempted until either the
787 * ctlr_loss_tmo (max_retries * connect_delay) expires or the
788 * remoteport's dev_loss_tmo expires.
792 case NVME_CTRL_RESETTING:
794 * Controller is already in the process of terminating the
795 * association. No need to do anything further. The reconnect
796 * step will kick in naturally after the association is
801 case NVME_CTRL_DELETING:
803 /* no action to take - let it delete */
809 * nvme_fc_unregister_remoteport - transport entry point called by an
810 * LLDD to deregister/remove a previously
811 * registered a NVME subsystem FC port.
812 * @remoteport: pointer to the (registered) remote port that is to be
816 * a completion status. Must be 0 upon success; a negative errno
817 * (ex: -ENXIO) upon failure.
820 nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
822 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
823 struct nvme_fc_ctrl *ctrl;
829 spin_lock_irqsave(&rport->lock, flags);
831 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
832 spin_unlock_irqrestore(&rport->lock, flags);
835 portptr->port_state = FC_OBJSTATE_DELETED;
837 rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
839 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
840 /* if dev_loss_tmo==0, dev loss is immediate */
841 if (!portptr->dev_loss_tmo) {
842 dev_warn(ctrl->ctrl.device,
843 "NVME-FC{%d}: controller connectivity lost.\n",
845 nvme_delete_ctrl(&ctrl->ctrl);
847 nvme_fc_ctrl_connectivity_loss(ctrl);
850 spin_unlock_irqrestore(&rport->lock, flags);
852 nvme_fc_abort_lsops(rport);
854 if (atomic_read(&rport->act_ctrl_cnt) == 0)
855 rport->lport->ops->remoteport_delete(portptr);
858 * release the reference, which will allow, if all controllers
859 * go away, which should only occur after dev_loss_tmo occurs,
860 * for the rport to be torn down.
862 nvme_fc_rport_put(rport);
866 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
869 * nvme_fc_rescan_remoteport - transport entry point called by an
870 * LLDD to request a nvme device rescan.
871 * @remoteport: pointer to the (registered) remote port that is to be
877 nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
879 struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
881 nvme_fc_signal_discovery_scan(rport->lport, rport);
883 EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
886 nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
889 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
892 spin_lock_irqsave(&rport->lock, flags);
894 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
895 spin_unlock_irqrestore(&rport->lock, flags);
899 /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
900 rport->remoteport.dev_loss_tmo = dev_loss_tmo;
902 spin_unlock_irqrestore(&rport->lock, flags);
906 EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
909 /* *********************** FC-NVME DMA Handling **************************** */
912 * The fcloop device passes in a NULL device pointer. Real LLD's will
913 * pass in a valid device pointer. If NULL is passed to the dma mapping
914 * routines, depending on the platform, it may or may not succeed, and
918 * Wrapper all the dma routines and check the dev pointer.
920 * If simple mappings (return just a dma address, we'll noop them,
921 * returning a dma address of 0.
923 * On more complex mappings (dma_map_sg), a pseudo routine fills
924 * in the scatter list, setting all dma addresses to 0.
927 static inline dma_addr_t
928 fc_dma_map_single(struct device *dev, void *ptr, size_t size,
929 enum dma_data_direction dir)
931 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
935 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
937 return dev ? dma_mapping_error(dev, dma_addr) : 0;
941 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
942 enum dma_data_direction dir)
945 dma_unmap_single(dev, addr, size, dir);
949 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
950 enum dma_data_direction dir)
953 dma_sync_single_for_cpu(dev, addr, size, dir);
957 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
958 enum dma_data_direction dir)
961 dma_sync_single_for_device(dev, addr, size, dir);
964 /* pseudo dma_map_sg call */
966 fc_map_sg(struct scatterlist *sg, int nents)
968 struct scatterlist *s;
971 WARN_ON(nents == 0 || sg[0].length == 0);
973 for_each_sg(sg, s, nents, i) {
975 #ifdef CONFIG_NEED_SG_DMA_LENGTH
976 s->dma_length = s->length;
983 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
984 enum dma_data_direction dir)
986 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
990 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
991 enum dma_data_direction dir)
994 dma_unmap_sg(dev, sg, nents, dir);
997 /* *********************** FC-NVME LS Handling **************************** */
999 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
1000 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
1004 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
1006 struct nvme_fc_rport *rport = lsop->rport;
1007 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1008 unsigned long flags;
1010 spin_lock_irqsave(&rport->lock, flags);
1012 if (!lsop->req_queued) {
1013 spin_unlock_irqrestore(&rport->lock, flags);
1017 list_del(&lsop->lsreq_list);
1019 lsop->req_queued = false;
1021 spin_unlock_irqrestore(&rport->lock, flags);
1023 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1024 (lsreq->rqstlen + lsreq->rsplen),
1027 nvme_fc_rport_put(rport);
1031 __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
1032 struct nvmefc_ls_req_op *lsop,
1033 void (*done)(struct nvmefc_ls_req *req, int status))
1035 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1036 unsigned long flags;
1039 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1040 return -ECONNREFUSED;
1042 if (!nvme_fc_rport_get(rport))
1046 lsop->rport = rport;
1047 lsop->req_queued = false;
1048 INIT_LIST_HEAD(&lsop->lsreq_list);
1049 init_completion(&lsop->ls_done);
1051 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
1052 lsreq->rqstlen + lsreq->rsplen,
1054 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1058 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1060 spin_lock_irqsave(&rport->lock, flags);
1062 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
1064 lsop->req_queued = true;
1066 spin_unlock_irqrestore(&rport->lock, flags);
1068 ret = rport->lport->ops->ls_req(&rport->lport->localport,
1069 &rport->remoteport, lsreq);
1076 lsop->ls_error = ret;
1077 spin_lock_irqsave(&rport->lock, flags);
1078 lsop->req_queued = false;
1079 list_del(&lsop->lsreq_list);
1080 spin_unlock_irqrestore(&rport->lock, flags);
1081 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1082 (lsreq->rqstlen + lsreq->rsplen),
1085 nvme_fc_rport_put(rport);
1091 nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1093 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1095 lsop->ls_error = status;
1096 complete(&lsop->ls_done);
1100 nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
1102 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1103 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1106 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
1110 * No timeout/not interruptible as we need the struct
1111 * to exist until the lldd calls us back. Thus mandate
1112 * wait until driver calls back. lldd responsible for
1113 * the timeout action
1115 wait_for_completion(&lsop->ls_done);
1117 __nvme_fc_finish_ls_req(lsop);
1119 ret = lsop->ls_error;
1125 /* ACC or RJT payload ? */
1126 if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1133 nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
1134 struct nvmefc_ls_req_op *lsop,
1135 void (*done)(struct nvmefc_ls_req *req, int status))
1137 /* don't wait for completion */
1139 return __nvme_fc_send_ls_req(rport, lsop, done);
1142 /* Validation Error indexes into the string table below */
1146 VERR_LSDESC_RQST = 2,
1147 VERR_LSDESC_RQST_LEN = 3,
1149 VERR_ASSOC_ID_LEN = 5,
1151 VERR_CONN_ID_LEN = 7,
1153 VERR_CR_ASSOC_ACC_LEN = 9,
1155 VERR_CR_CONN_ACC_LEN = 11,
1157 VERR_DISCONN_ACC_LEN = 13,
1160 static char *validation_errors[] = {
1164 "Bad LSDESC_RQST Length",
1165 "Not Association ID",
1166 "Bad Association ID Length",
1167 "Not Connection ID",
1168 "Bad Connection ID Length",
1169 "Not CR_ASSOC Rqst",
1170 "Bad CR_ASSOC ACC Length",
1172 "Bad CR_CONN ACC Length",
1173 "Not Disconnect Rqst",
1174 "Bad Disconnect ACC Length",
1178 nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1179 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1181 struct nvmefc_ls_req_op *lsop;
1182 struct nvmefc_ls_req *lsreq;
1183 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1184 struct fcnvme_ls_cr_assoc_acc *assoc_acc;
1187 lsop = kzalloc((sizeof(*lsop) +
1188 ctrl->lport->ops->lsrqst_priv_sz +
1189 sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
1194 lsreq = &lsop->ls_req;
1196 lsreq->private = (void *)&lsop[1];
1197 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
1198 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1199 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1201 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1202 assoc_rqst->desc_list_len =
1203 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1205 assoc_rqst->assoc_cmd.desc_tag =
1206 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1207 assoc_rqst->assoc_cmd.desc_len =
1209 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1211 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1212 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
1213 /* Linux supports only Dynamic controllers */
1214 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
1215 uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1216 strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1217 min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1218 strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1219 min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
1221 lsop->queue = queue;
1222 lsreq->rqstaddr = assoc_rqst;
1223 lsreq->rqstlen = sizeof(*assoc_rqst);
1224 lsreq->rspaddr = assoc_acc;
1225 lsreq->rsplen = sizeof(*assoc_acc);
1226 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1228 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1230 goto out_free_buffer;
1232 /* process connect LS completion */
1234 /* validate the ACC response */
1235 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1237 else if (assoc_acc->hdr.desc_list_len !=
1239 sizeof(struct fcnvme_ls_cr_assoc_acc)))
1240 fcret = VERR_CR_ASSOC_ACC_LEN;
1241 else if (assoc_acc->hdr.rqst.desc_tag !=
1242 cpu_to_be32(FCNVME_LSDESC_RQST))
1243 fcret = VERR_LSDESC_RQST;
1244 else if (assoc_acc->hdr.rqst.desc_len !=
1245 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1246 fcret = VERR_LSDESC_RQST_LEN;
1247 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1248 fcret = VERR_CR_ASSOC;
1249 else if (assoc_acc->associd.desc_tag !=
1250 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1251 fcret = VERR_ASSOC_ID;
1252 else if (assoc_acc->associd.desc_len !=
1254 sizeof(struct fcnvme_lsdesc_assoc_id)))
1255 fcret = VERR_ASSOC_ID_LEN;
1256 else if (assoc_acc->connectid.desc_tag !=
1257 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1258 fcret = VERR_CONN_ID;
1259 else if (assoc_acc->connectid.desc_len !=
1260 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1261 fcret = VERR_CONN_ID_LEN;
1266 "q %d connect failed: %s\n",
1267 queue->qnum, validation_errors[fcret]);
1269 ctrl->association_id =
1270 be64_to_cpu(assoc_acc->associd.association_id);
1271 queue->connection_id =
1272 be64_to_cpu(assoc_acc->connectid.connection_id);
1273 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1281 "queue %d connect admin queue failed (%d).\n",
1287 nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1288 u16 qsize, u16 ersp_ratio)
1290 struct nvmefc_ls_req_op *lsop;
1291 struct nvmefc_ls_req *lsreq;
1292 struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1293 struct fcnvme_ls_cr_conn_acc *conn_acc;
1296 lsop = kzalloc((sizeof(*lsop) +
1297 ctrl->lport->ops->lsrqst_priv_sz +
1298 sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
1303 lsreq = &lsop->ls_req;
1305 lsreq->private = (void *)&lsop[1];
1306 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
1307 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1308 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1310 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1311 conn_rqst->desc_list_len = cpu_to_be32(
1312 sizeof(struct fcnvme_lsdesc_assoc_id) +
1313 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1315 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1316 conn_rqst->associd.desc_len =
1318 sizeof(struct fcnvme_lsdesc_assoc_id));
1319 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1320 conn_rqst->connect_cmd.desc_tag =
1321 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1322 conn_rqst->connect_cmd.desc_len =
1324 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1325 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1326 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
1327 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
1329 lsop->queue = queue;
1330 lsreq->rqstaddr = conn_rqst;
1331 lsreq->rqstlen = sizeof(*conn_rqst);
1332 lsreq->rspaddr = conn_acc;
1333 lsreq->rsplen = sizeof(*conn_acc);
1334 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1336 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1338 goto out_free_buffer;
1340 /* process connect LS completion */
1342 /* validate the ACC response */
1343 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1345 else if (conn_acc->hdr.desc_list_len !=
1346 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1347 fcret = VERR_CR_CONN_ACC_LEN;
1348 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1349 fcret = VERR_LSDESC_RQST;
1350 else if (conn_acc->hdr.rqst.desc_len !=
1351 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1352 fcret = VERR_LSDESC_RQST_LEN;
1353 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1354 fcret = VERR_CR_CONN;
1355 else if (conn_acc->connectid.desc_tag !=
1356 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1357 fcret = VERR_CONN_ID;
1358 else if (conn_acc->connectid.desc_len !=
1359 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1360 fcret = VERR_CONN_ID_LEN;
1365 "q %d connect failed: %s\n",
1366 queue->qnum, validation_errors[fcret]);
1368 queue->connection_id =
1369 be64_to_cpu(conn_acc->connectid.connection_id);
1370 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1378 "queue %d connect command failed (%d).\n",
1384 nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1386 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1388 __nvme_fc_finish_ls_req(lsop);
1390 /* fc-nvme iniator doesn't care about success or failure of cmd */
1396 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1397 * the FC-NVME Association. Terminating the association also
1398 * terminates the FC-NVME connections (per queue, both admin and io
1399 * queues) that are part of the association. E.g. things are torn
1400 * down, and the related FC-NVME Association ID and Connection IDs
1403 * The behavior of the fc-nvme initiator is such that it's
1404 * understanding of the association and connections will implicitly
1405 * be torn down. The action is implicit as it may be due to a loss of
1406 * connectivity with the fc-nvme target, so you may never get a
1407 * response even if you tried. As such, the action of this routine
1408 * is to asynchronously send the LS, ignore any results of the LS, and
1409 * continue on with terminating the association. If the fc-nvme target
1410 * is present and receives the LS, it too can tear down.
1413 nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1415 struct fcnvme_ls_disconnect_rqst *discon_rqst;
1416 struct fcnvme_ls_disconnect_acc *discon_acc;
1417 struct nvmefc_ls_req_op *lsop;
1418 struct nvmefc_ls_req *lsreq;
1421 lsop = kzalloc((sizeof(*lsop) +
1422 ctrl->lport->ops->lsrqst_priv_sz +
1423 sizeof(*discon_rqst) + sizeof(*discon_acc)),
1426 /* couldn't sent it... too bad */
1429 lsreq = &lsop->ls_req;
1431 lsreq->private = (void *)&lsop[1];
1432 discon_rqst = (struct fcnvme_ls_disconnect_rqst *)
1433 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1434 discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1];
1436 discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT;
1437 discon_rqst->desc_list_len = cpu_to_be32(
1438 sizeof(struct fcnvme_lsdesc_assoc_id) +
1439 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1441 discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1442 discon_rqst->associd.desc_len =
1444 sizeof(struct fcnvme_lsdesc_assoc_id));
1446 discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1448 discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
1449 FCNVME_LSDESC_DISCONN_CMD);
1450 discon_rqst->discon_cmd.desc_len =
1452 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1453 discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION;
1454 discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id);
1456 lsreq->rqstaddr = discon_rqst;
1457 lsreq->rqstlen = sizeof(*discon_rqst);
1458 lsreq->rspaddr = discon_acc;
1459 lsreq->rsplen = sizeof(*discon_acc);
1460 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1462 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1463 nvme_fc_disconnect_assoc_done);
1467 /* only meaningful part to terminating the association */
1468 ctrl->association_id = 0;
1472 /* *********************** NVME Ctrl Routines **************************** */
1474 static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
1477 __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1478 struct nvme_fc_fcp_op *op)
1480 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1481 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1482 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1483 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1485 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1489 nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1490 unsigned int hctx_idx)
1492 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1494 return __nvme_fc_exit_request(set->driver_data, op);
1498 __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1500 unsigned long flags;
1503 spin_lock_irqsave(&ctrl->lock, flags);
1504 opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1505 if (opstate != FCPOP_STATE_ACTIVE)
1506 atomic_set(&op->state, opstate);
1507 else if (ctrl->flags & FCCTRL_TERMIO)
1509 spin_unlock_irqrestore(&ctrl->lock, flags);
1511 if (opstate != FCPOP_STATE_ACTIVE)
1514 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1515 &ctrl->rport->remoteport,
1516 op->queue->lldd_handle,
1523 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1525 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
1528 /* ensure we've initialized the ops once */
1529 if (!(aen_op->flags & FCOP_FLAGS_AEN))
1532 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
1533 __nvme_fc_abort_op(ctrl, aen_op);
1537 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1538 struct nvme_fc_fcp_op *op, int opstate)
1540 unsigned long flags;
1542 if (opstate == FCPOP_STATE_ABORTED) {
1543 spin_lock_irqsave(&ctrl->lock, flags);
1544 if (ctrl->flags & FCCTRL_TERMIO) {
1546 wake_up(&ctrl->ioabort_wait);
1548 spin_unlock_irqrestore(&ctrl->lock, flags);
1553 nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1555 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1556 struct request *rq = op->rq;
1557 struct nvmefc_fcp_req *freq = &op->fcp_req;
1558 struct nvme_fc_ctrl *ctrl = op->ctrl;
1559 struct nvme_fc_queue *queue = op->queue;
1560 struct nvme_completion *cqe = &op->rsp_iu.cqe;
1561 struct nvme_command *sqe = &op->cmd_iu.sqe;
1562 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
1563 union nvme_result result;
1564 bool terminate_assoc = true;
1569 * The current linux implementation of a nvme controller
1570 * allocates a single tag set for all io queues and sizes
1571 * the io queues to fully hold all possible tags. Thus, the
1572 * implementation does not reference or care about the sqhd
1573 * value as it never needs to use the sqhd/sqtail pointers
1574 * for submission pacing.
1576 * This affects the FC-NVME implementation in two ways:
1577 * 1) As the value doesn't matter, we don't need to waste
1578 * cycles extracting it from ERSPs and stamping it in the
1579 * cases where the transport fabricates CQEs on successful
1581 * 2) The FC-NVME implementation requires that delivery of
1582 * ERSP completions are to go back to the nvme layer in order
1583 * relative to the rsn, such that the sqhd value will always
1584 * be "in order" for the nvme layer. As the nvme layer in
1585 * linux doesn't care about sqhd, there's no need to return
1589 * As the core nvme layer in linux currently does not look at
1590 * every field in the cqe - in cases where the FC transport must
1591 * fabricate a CQE, the following fields will not be set as they
1592 * are not referenced:
1593 * cqe.sqid, cqe.sqhd, cqe.command_id
1595 * Failure or error of an individual i/o, in a transport
1596 * detected fashion unrelated to the nvme completion status,
1597 * potentially cause the initiator and target sides to get out
1598 * of sync on SQ head/tail (aka outstanding io count allowed).
1599 * Per FC-NVME spec, failure of an individual command requires
1600 * the connection to be terminated, which in turn requires the
1601 * association to be terminated.
1604 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
1606 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1607 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1609 if (opstate == FCPOP_STATE_ABORTED)
1610 status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1611 else if (freq->status)
1612 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
1615 * For the linux implementation, if we have an unsuccesful
1616 * status, they blk-mq layer can typically be called with the
1617 * non-zero status and the content of the cqe isn't important.
1623 * command completed successfully relative to the wire
1624 * protocol. However, validate anything received and
1625 * extract the status and result from the cqe (create it
1629 switch (freq->rcv_rsplen) {
1632 case NVME_FC_SIZEOF_ZEROS_RSP:
1634 * No response payload or 12 bytes of payload (which
1635 * should all be zeros) are considered successful and
1636 * no payload in the CQE by the transport.
1638 if (freq->transferred_length !=
1639 be32_to_cpu(op->cmd_iu.data_len)) {
1640 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
1646 case sizeof(struct nvme_fc_ersp_iu):
1648 * The ERSP IU contains a full completion with CQE.
1649 * Validate ERSP IU and look at cqe.
1651 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1652 (freq->rcv_rsplen / 4) ||
1653 be32_to_cpu(op->rsp_iu.xfrd_len) !=
1654 freq->transferred_length ||
1655 op->rsp_iu.status_code ||
1656 sqe->common.command_id != cqe->command_id)) {
1657 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
1660 result = cqe->result;
1661 status = cqe->status;
1665 status = cpu_to_le16(NVME_SC_INTERNAL << 1);
1669 terminate_assoc = false;
1672 if (op->flags & FCOP_FLAGS_AEN) {
1673 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
1674 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
1675 atomic_set(&op->state, FCPOP_STATE_IDLE);
1676 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
1677 nvme_fc_ctrl_put(ctrl);
1681 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
1682 nvme_end_request(rq, status, result);
1685 if (terminate_assoc)
1686 nvme_fc_error_recovery(ctrl, "transport detected io error");
1690 __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
1691 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
1692 struct request *rq, u32 rqno)
1694 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1697 memset(op, 0, sizeof(*op));
1698 op->fcp_req.cmdaddr = &op->cmd_iu;
1699 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
1700 op->fcp_req.rspaddr = &op->rsp_iu;
1701 op->fcp_req.rsplen = sizeof(op->rsp_iu);
1702 op->fcp_req.done = nvme_fc_fcpio_done;
1703 op->fcp_req.first_sgl = (struct scatterlist *)&op[1];
1704 op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE];
1710 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1711 cmdiu->fc_id = NVME_CMD_FC_ID;
1712 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1714 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
1715 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
1716 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
1718 "FCP Op failed - cmdiu dma mapping failed.\n");
1723 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
1724 &op->rsp_iu, sizeof(op->rsp_iu),
1726 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
1728 "FCP Op failed - rspiu dma mapping failed.\n");
1732 atomic_set(&op->state, FCPOP_STATE_IDLE);
1738 nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
1739 unsigned int hctx_idx, unsigned int numa_node)
1741 struct nvme_fc_ctrl *ctrl = set->driver_data;
1742 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1743 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
1744 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
1746 nvme_req(rq)->ctrl = &ctrl->ctrl;
1747 return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
1751 nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
1753 struct nvme_fc_fcp_op *aen_op;
1754 struct nvme_fc_cmd_iu *cmdiu;
1755 struct nvme_command *sqe;
1759 aen_op = ctrl->aen_ops;
1760 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
1761 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
1766 cmdiu = &aen_op->cmd_iu;
1768 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
1769 aen_op, (struct request *)NULL,
1770 (NVME_AQ_BLK_MQ_DEPTH + i));
1776 aen_op->flags = FCOP_FLAGS_AEN;
1777 aen_op->fcp_req.first_sgl = NULL; /* no sg list */
1778 aen_op->fcp_req.private = private;
1780 memset(sqe, 0, sizeof(*sqe));
1781 sqe->common.opcode = nvme_admin_async_event;
1782 /* Note: core layer may overwrite the sqe.command_id value */
1783 sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
1789 nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
1791 struct nvme_fc_fcp_op *aen_op;
1794 cancel_work_sync(&ctrl->ctrl.async_event_work);
1795 aen_op = ctrl->aen_ops;
1796 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
1797 if (!aen_op->fcp_req.private)
1800 __nvme_fc_exit_request(ctrl, aen_op);
1802 kfree(aen_op->fcp_req.private);
1803 aen_op->fcp_req.private = NULL;
1808 __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
1811 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
1813 hctx->driver_data = queue;
1818 nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1819 unsigned int hctx_idx)
1821 struct nvme_fc_ctrl *ctrl = data;
1823 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
1829 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1830 unsigned int hctx_idx)
1832 struct nvme_fc_ctrl *ctrl = data;
1834 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
1840 nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
1842 struct nvme_fc_queue *queue;
1844 queue = &ctrl->queues[idx];
1845 memset(queue, 0, sizeof(*queue));
1848 atomic_set(&queue->csn, 0);
1849 queue->dev = ctrl->dev;
1852 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1854 queue->cmnd_capsule_len = sizeof(struct nvme_command);
1857 * Considered whether we should allocate buffers for all SQEs
1858 * and CQEs and dma map them - mapping their respective entries
1859 * into the request structures (kernel vm addr and dma address)
1860 * thus the driver could use the buffers/mappings directly.
1861 * It only makes sense if the LLDD would use them for its
1862 * messaging api. It's very unlikely most adapter api's would use
1863 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1864 * structures were used instead.
1869 * This routine terminates a queue at the transport level.
1870 * The transport has already ensured that all outstanding ios on
1871 * the queue have been terminated.
1872 * The transport will send a Disconnect LS request to terminate
1873 * the queue's connection. Termination of the admin queue will also
1874 * terminate the association at the target.
1877 nvme_fc_free_queue(struct nvme_fc_queue *queue)
1879 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
1882 clear_bit(NVME_FC_Q_LIVE, &queue->flags);
1884 * Current implementation never disconnects a single queue.
1885 * It always terminates a whole association. So there is never
1886 * a disconnect(queue) LS sent to the target.
1889 queue->connection_id = 0;
1890 atomic_set(&queue->csn, 0);
1894 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
1895 struct nvme_fc_queue *queue, unsigned int qidx)
1897 if (ctrl->lport->ops->delete_queue)
1898 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
1899 queue->lldd_handle);
1900 queue->lldd_handle = NULL;
1904 nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
1908 for (i = 1; i < ctrl->ctrl.queue_count; i++)
1909 nvme_fc_free_queue(&ctrl->queues[i]);
1913 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
1914 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
1918 queue->lldd_handle = NULL;
1919 if (ctrl->lport->ops->create_queue)
1920 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
1921 qidx, qsize, &queue->lldd_handle);
1927 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
1929 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
1932 for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
1933 __nvme_fc_delete_hw_queue(ctrl, queue, i);
1937 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1939 struct nvme_fc_queue *queue = &ctrl->queues[1];
1942 for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
1943 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
1952 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
1957 nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1961 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
1962 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
1966 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
1970 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
1977 nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
1981 for (i = 1; i < ctrl->ctrl.queue_count; i++)
1982 nvme_fc_init_queue(ctrl, i);
1986 nvme_fc_ctrl_free(struct kref *ref)
1988 struct nvme_fc_ctrl *ctrl =
1989 container_of(ref, struct nvme_fc_ctrl, ref);
1990 unsigned long flags;
1992 if (ctrl->ctrl.tagset) {
1993 blk_cleanup_queue(ctrl->ctrl.connect_q);
1994 blk_mq_free_tag_set(&ctrl->tag_set);
1997 /* remove from rport list */
1998 spin_lock_irqsave(&ctrl->rport->lock, flags);
1999 list_del(&ctrl->ctrl_list);
2000 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
2002 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
2003 blk_cleanup_queue(ctrl->ctrl.admin_q);
2004 blk_mq_free_tag_set(&ctrl->admin_tag_set);
2006 kfree(ctrl->queues);
2008 put_device(ctrl->dev);
2009 nvme_fc_rport_put(ctrl->rport);
2011 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2012 if (ctrl->ctrl.opts)
2013 nvmf_free_options(ctrl->ctrl.opts);
2018 nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2020 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2024 nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2026 return kref_get_unless_zero(&ctrl->ref);
2030 * All accesses from nvme core layer done - can now free the
2031 * controller. Called after last nvme_put_ctrl() call
2034 nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
2036 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2038 WARN_ON(nctrl != &ctrl->ctrl);
2040 nvme_fc_ctrl_put(ctrl);
2044 nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
2049 * if an error (io timeout, etc) while (re)connecting,
2050 * it's an error on creating the new association.
2051 * Start the error recovery thread if it hasn't already
2052 * been started. It is expected there could be multiple
2053 * ios hitting this path before things are cleaned up.
2055 if (ctrl->ctrl.state == NVME_CTRL_CONNECTING) {
2056 active = atomic_xchg(&ctrl->err_work_active, 1);
2057 if (!active && !queue_work(nvme_fc_wq, &ctrl->err_work)) {
2058 atomic_set(&ctrl->err_work_active, 0);
2064 /* Otherwise, only proceed if in LIVE state - e.g. on first error */
2065 if (ctrl->ctrl.state != NVME_CTRL_LIVE)
2068 dev_warn(ctrl->ctrl.device,
2069 "NVME-FC{%d}: transport association error detected: %s\n",
2070 ctrl->cnum, errmsg);
2071 dev_warn(ctrl->ctrl.device,
2072 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
2074 nvme_reset_ctrl(&ctrl->ctrl);
2077 static enum blk_eh_timer_return
2078 nvme_fc_timeout(struct request *rq, bool reserved)
2080 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2081 struct nvme_fc_ctrl *ctrl = op->ctrl;
2084 * we can't individually ABTS an io without affecting the queue,
2085 * thus killing the queue, and thus the association.
2086 * So resolve by performing a controller reset, which will stop
2087 * the host/io stack, terminate the association on the link,
2088 * and recreate an association on the link.
2090 nvme_fc_error_recovery(ctrl, "io timeout error");
2093 * the io abort has been initiated. Have the reset timer
2094 * restarted and the abort completion will complete the io
2095 * shortly. Avoids a synchronous wait while the abort finishes.
2097 return BLK_EH_RESET_TIMER;
2101 nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2102 struct nvme_fc_fcp_op *op)
2104 struct nvmefc_fcp_req *freq = &op->fcp_req;
2105 enum dma_data_direction dir;
2110 if (!blk_rq_payload_bytes(rq))
2113 freq->sg_table.sgl = freq->first_sgl;
2114 ret = sg_alloc_table_chained(&freq->sg_table,
2115 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl);
2119 op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
2120 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2121 dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
2122 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2124 if (unlikely(freq->sg_cnt <= 0)) {
2125 sg_free_table_chained(&freq->sg_table, true);
2131 * TODO: blk_integrity_rq(rq) for DIF
2137 nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2138 struct nvme_fc_fcp_op *op)
2140 struct nvmefc_fcp_req *freq = &op->fcp_req;
2145 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2146 ((rq_data_dir(rq) == WRITE) ?
2147 DMA_TO_DEVICE : DMA_FROM_DEVICE));
2149 nvme_cleanup_cmd(rq);
2151 sg_free_table_chained(&freq->sg_table, true);
2157 * In FC, the queue is a logical thing. At transport connect, the target
2158 * creates its "queue" and returns a handle that is to be given to the
2159 * target whenever it posts something to the corresponding SQ. When an
2160 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2161 * command contained within the SQE, an io, and assigns a FC exchange
2162 * to it. The SQE and the associated SQ handle are sent in the initial
2163 * CMD IU sents on the exchange. All transfers relative to the io occur
2164 * as part of the exchange. The CQE is the last thing for the io,
2165 * which is transferred (explicitly or implicitly) with the RSP IU
2166 * sent on the exchange. After the CQE is received, the FC exchange is
2167 * terminaed and the Exchange may be used on a different io.
2169 * The transport to LLDD api has the transport making a request for a
2170 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2171 * resource and transfers the command. The LLDD will then process all
2172 * steps to complete the io. Upon completion, the transport done routine
2175 * So - while the operation is outstanding to the LLDD, there is a link
2176 * level FC exchange resource that is also outstanding. This must be
2177 * considered in all cleanup operations.
2180 nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2181 struct nvme_fc_fcp_op *op, u32 data_len,
2182 enum nvmefc_fcp_datadir io_dir)
2184 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2185 struct nvme_command *sqe = &cmdiu->sqe;
2189 * before attempting to send the io, check to see if we believe
2190 * the target device is present
2192 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2193 return BLK_STS_RESOURCE;
2195 if (!nvme_fc_ctrl_get(ctrl))
2196 return BLK_STS_IOERR;
2198 /* format the FC-NVME CMD IU and fcp_req */
2199 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2200 cmdiu->data_len = cpu_to_be32(data_len);
2202 case NVMEFC_FCP_WRITE:
2203 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2205 case NVMEFC_FCP_READ:
2206 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2208 case NVMEFC_FCP_NODATA:
2212 op->fcp_req.payload_length = data_len;
2213 op->fcp_req.io_dir = io_dir;
2214 op->fcp_req.transferred_length = 0;
2215 op->fcp_req.rcv_rsplen = 0;
2216 op->fcp_req.status = NVME_SC_SUCCESS;
2217 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2220 * validate per fabric rules, set fields mandated by fabric spec
2221 * as well as those by FC-NVME spec.
2223 WARN_ON_ONCE(sqe->common.metadata);
2224 sqe->common.flags |= NVME_CMD_SGL_METABUF;
2227 * format SQE DPTR field per FC-NVME rules:
2228 * type=0x5 Transport SGL Data Block Descriptor
2229 * subtype=0xA Transport-specific value
2231 * length=length of the data series
2233 sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2234 NVME_SGL_FMT_TRANSPORT_A;
2235 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2236 sqe->rw.dptr.sgl.addr = 0;
2238 if (!(op->flags & FCOP_FLAGS_AEN)) {
2239 ret = nvme_fc_map_data(ctrl, op->rq, op);
2241 nvme_cleanup_cmd(op->rq);
2242 nvme_fc_ctrl_put(ctrl);
2243 if (ret == -ENOMEM || ret == -EAGAIN)
2244 return BLK_STS_RESOURCE;
2245 return BLK_STS_IOERR;
2249 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2250 sizeof(op->cmd_iu), DMA_TO_DEVICE);
2252 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2254 if (!(op->flags & FCOP_FLAGS_AEN))
2255 blk_mq_start_request(op->rq);
2257 cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2258 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2259 &ctrl->rport->remoteport,
2260 queue->lldd_handle, &op->fcp_req);
2264 * If the lld fails to send the command is there an issue with
2265 * the csn value? If the command that fails is the Connect,
2266 * no - as the connection won't be live. If it is a command
2267 * post-connect, it's possible a gap in csn may be created.
2268 * Does this matter? As Linux initiators don't send fused
2269 * commands, no. The gap would exist, but as there's nothing
2270 * that depends on csn order to be delivered on the target
2271 * side, it shouldn't hurt. It would be difficult for a
2272 * target to even detect the csn gap as it has no idea when the
2273 * cmd with the csn was supposed to arrive.
2275 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2276 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2278 if (!(op->flags & FCOP_FLAGS_AEN))
2279 nvme_fc_unmap_data(ctrl, op->rq, op);
2281 nvme_fc_ctrl_put(ctrl);
2283 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2285 return BLK_STS_IOERR;
2287 return BLK_STS_RESOURCE;
2294 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2295 const struct blk_mq_queue_data *bd)
2297 struct nvme_ns *ns = hctx->queue->queuedata;
2298 struct nvme_fc_queue *queue = hctx->driver_data;
2299 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2300 struct request *rq = bd->rq;
2301 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2302 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2303 struct nvme_command *sqe = &cmdiu->sqe;
2304 enum nvmefc_fcp_datadir io_dir;
2305 bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2309 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
2310 !nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2311 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
2313 ret = nvme_setup_cmd(ns, rq, sqe);
2317 data_len = blk_rq_payload_bytes(rq);
2319 io_dir = ((rq_data_dir(rq) == WRITE) ?
2320 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2322 io_dir = NVMEFC_FCP_NODATA;
2324 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2327 static struct blk_mq_tags *
2328 nvme_fc_tagset(struct nvme_fc_queue *queue)
2330 if (queue->qnum == 0)
2331 return queue->ctrl->admin_tag_set.tags[queue->qnum];
2333 return queue->ctrl->tag_set.tags[queue->qnum - 1];
2337 nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
2340 struct nvme_fc_queue *queue = hctx->driver_data;
2341 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2342 struct request *req;
2343 struct nvme_fc_fcp_op *op;
2345 req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag);
2349 op = blk_mq_rq_to_pdu(req);
2351 if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) &&
2352 (ctrl->lport->ops->poll_queue))
2353 ctrl->lport->ops->poll_queue(&ctrl->lport->localport,
2354 queue->lldd_handle);
2356 return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE));
2360 nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2362 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2363 struct nvme_fc_fcp_op *aen_op;
2364 unsigned long flags;
2365 bool terminating = false;
2368 spin_lock_irqsave(&ctrl->lock, flags);
2369 if (ctrl->flags & FCCTRL_TERMIO)
2371 spin_unlock_irqrestore(&ctrl->lock, flags);
2376 aen_op = &ctrl->aen_ops[0];
2378 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2381 dev_err(ctrl->ctrl.device,
2382 "failed async event work\n");
2386 nvme_fc_complete_rq(struct request *rq)
2388 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2389 struct nvme_fc_ctrl *ctrl = op->ctrl;
2391 atomic_set(&op->state, FCPOP_STATE_IDLE);
2393 nvme_fc_unmap_data(ctrl, rq, op);
2394 nvme_complete_rq(rq);
2395 nvme_fc_ctrl_put(ctrl);
2399 * This routine is used by the transport when it needs to find active
2400 * io on a queue that is to be terminated. The transport uses
2401 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2402 * this routine to kill them on a 1 by 1 basis.
2404 * As FC allocates FC exchange for each io, the transport must contact
2405 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2406 * After terminating the exchange the LLDD will call the transport's
2407 * normal io done path for the request, but it will have an aborted
2408 * status. The done path will return the io request back to the block
2409 * layer with an error status.
2412 nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2414 struct nvme_ctrl *nctrl = data;
2415 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2416 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2418 __nvme_fc_abort_op(ctrl, op);
2422 static const struct blk_mq_ops nvme_fc_mq_ops = {
2423 .queue_rq = nvme_fc_queue_rq,
2424 .complete = nvme_fc_complete_rq,
2425 .init_request = nvme_fc_init_request,
2426 .exit_request = nvme_fc_exit_request,
2427 .init_hctx = nvme_fc_init_hctx,
2428 .poll = nvme_fc_poll,
2429 .timeout = nvme_fc_timeout,
2433 nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2435 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2436 unsigned int nr_io_queues;
2439 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2440 ctrl->lport->ops->max_hw_queues);
2441 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2443 dev_info(ctrl->ctrl.device,
2444 "set_queue_count failed: %d\n", ret);
2448 ctrl->ctrl.queue_count = nr_io_queues + 1;
2452 nvme_fc_init_io_queues(ctrl);
2454 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2455 ctrl->tag_set.ops = &nvme_fc_mq_ops;
2456 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2457 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2458 ctrl->tag_set.numa_node = NUMA_NO_NODE;
2459 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2460 ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2462 sizeof(struct scatterlist)) +
2463 ctrl->lport->ops->fcprqst_priv_sz;
2464 ctrl->tag_set.driver_data = ctrl;
2465 ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
2466 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2468 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2472 ctrl->ctrl.tagset = &ctrl->tag_set;
2474 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2475 if (IS_ERR(ctrl->ctrl.connect_q)) {
2476 ret = PTR_ERR(ctrl->ctrl.connect_q);
2477 goto out_free_tag_set;
2480 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2482 goto out_cleanup_blk_queue;
2484 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2486 goto out_delete_hw_queues;
2488 ctrl->ioq_live = true;
2492 out_delete_hw_queues:
2493 nvme_fc_delete_hw_io_queues(ctrl);
2494 out_cleanup_blk_queue:
2495 blk_cleanup_queue(ctrl->ctrl.connect_q);
2497 blk_mq_free_tag_set(&ctrl->tag_set);
2498 nvme_fc_free_io_queues(ctrl);
2500 /* force put free routine to ignore io queues */
2501 ctrl->ctrl.tagset = NULL;
2507 nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
2509 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2510 unsigned int nr_io_queues;
2513 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2514 ctrl->lport->ops->max_hw_queues);
2515 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2517 dev_info(ctrl->ctrl.device,
2518 "set_queue_count failed: %d\n", ret);
2522 ctrl->ctrl.queue_count = nr_io_queues + 1;
2523 /* check for io queues existing */
2524 if (ctrl->ctrl.queue_count == 1)
2527 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2529 goto out_free_io_queues;
2531 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2533 goto out_delete_hw_queues;
2535 blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2539 out_delete_hw_queues:
2540 nvme_fc_delete_hw_io_queues(ctrl);
2542 nvme_fc_free_io_queues(ctrl);
2547 nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
2549 struct nvme_fc_lport *lport = rport->lport;
2551 atomic_inc(&lport->act_rport_cnt);
2555 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
2557 struct nvme_fc_lport *lport = rport->lport;
2560 cnt = atomic_dec_return(&lport->act_rport_cnt);
2561 if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
2562 lport->ops->localport_delete(&lport->localport);
2566 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
2568 struct nvme_fc_rport *rport = ctrl->rport;
2571 if (ctrl->assoc_active)
2574 ctrl->assoc_active = true;
2575 cnt = atomic_inc_return(&rport->act_ctrl_cnt);
2577 nvme_fc_rport_active_on_lport(rport);
2583 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
2585 struct nvme_fc_rport *rport = ctrl->rport;
2586 struct nvme_fc_lport *lport = rport->lport;
2589 /* ctrl->assoc_active=false will be set independently */
2591 cnt = atomic_dec_return(&rport->act_ctrl_cnt);
2593 if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
2594 lport->ops->remoteport_delete(&rport->remoteport);
2595 nvme_fc_rport_inactive_on_lport(rport);
2602 * This routine restarts the controller on the host side, and
2603 * on the link side, recreates the controller association.
2606 nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
2608 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2612 ++ctrl->ctrl.nr_reconnects;
2614 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2617 if (nvme_fc_ctlr_active_on_rport(ctrl))
2621 * Create the admin queue
2624 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
2627 goto out_free_queue;
2629 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
2630 NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
2632 goto out_delete_hw_queue;
2634 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
2636 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
2638 goto out_disconnect_admin_queue;
2640 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
2643 * Check controller capabilities
2645 * todo:- add code to check if ctrl attributes changed from
2646 * prior connection values
2649 ret = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap);
2651 dev_err(ctrl->ctrl.device,
2652 "prop_get NVME_REG_CAP failed\n");
2653 goto out_disconnect_admin_queue;
2657 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
2659 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
2661 goto out_disconnect_admin_queue;
2663 ctrl->ctrl.max_hw_sectors =
2664 (ctrl->lport->ops->max_sgl_segments - 1) << (PAGE_SHIFT - 9);
2666 ret = nvme_init_identify(&ctrl->ctrl);
2668 goto out_disconnect_admin_queue;
2672 /* FC-NVME does not have other data in the capsule */
2673 if (ctrl->ctrl.icdoff) {
2674 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
2676 goto out_disconnect_admin_queue;
2679 /* FC-NVME supports normal SGL Data Block Descriptors */
2681 if (opts->queue_size > ctrl->ctrl.maxcmd) {
2682 /* warn if maxcmd is lower than queue_size */
2683 dev_warn(ctrl->ctrl.device,
2684 "queue_size %zu > ctrl maxcmd %u, reducing "
2686 opts->queue_size, ctrl->ctrl.maxcmd);
2687 opts->queue_size = ctrl->ctrl.maxcmd;
2690 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
2691 /* warn if sqsize is lower than queue_size */
2692 dev_warn(ctrl->ctrl.device,
2693 "queue_size %zu > ctrl sqsize %u, clamping down\n",
2694 opts->queue_size, ctrl->ctrl.sqsize + 1);
2695 opts->queue_size = ctrl->ctrl.sqsize + 1;
2698 ret = nvme_fc_init_aen_ops(ctrl);
2700 goto out_term_aen_ops;
2703 * Create the io queues
2706 if (ctrl->ctrl.queue_count > 1) {
2707 if (!ctrl->ioq_live)
2708 ret = nvme_fc_create_io_queues(ctrl);
2710 ret = nvme_fc_recreate_io_queues(ctrl);
2712 goto out_term_aen_ops;
2715 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
2717 ctrl->ctrl.nr_reconnects = 0;
2720 nvme_start_ctrl(&ctrl->ctrl);
2722 return 0; /* Success */
2725 nvme_fc_term_aen_ops(ctrl);
2726 out_disconnect_admin_queue:
2727 /* send a Disconnect(association) LS to fc-nvme target */
2728 nvme_fc_xmt_disconnect_assoc(ctrl);
2729 out_delete_hw_queue:
2730 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2732 nvme_fc_free_queue(&ctrl->queues[0]);
2733 ctrl->assoc_active = false;
2734 nvme_fc_ctlr_inactive_on_rport(ctrl);
2740 * This routine stops operation of the controller on the host side.
2741 * On the host os stack side: Admin and IO queues are stopped,
2742 * outstanding ios on them terminated via FC ABTS.
2743 * On the link side: the association is terminated.
2746 nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
2748 unsigned long flags;
2750 if (!ctrl->assoc_active)
2752 ctrl->assoc_active = false;
2754 spin_lock_irqsave(&ctrl->lock, flags);
2755 ctrl->flags |= FCCTRL_TERMIO;
2757 spin_unlock_irqrestore(&ctrl->lock, flags);
2760 * If io queues are present, stop them and terminate all outstanding
2761 * ios on them. As FC allocates FC exchange for each io, the
2762 * transport must contact the LLDD to terminate the exchange,
2763 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2764 * to tell us what io's are busy and invoke a transport routine
2765 * to kill them with the LLDD. After terminating the exchange
2766 * the LLDD will call the transport's normal io done path, but it
2767 * will have an aborted status. The done path will return the
2768 * io requests back to the block layer as part of normal completions
2769 * (but with error status).
2771 if (ctrl->ctrl.queue_count > 1) {
2772 nvme_stop_queues(&ctrl->ctrl);
2773 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2774 nvme_fc_terminate_exchange, &ctrl->ctrl);
2778 * Other transports, which don't have link-level contexts bound
2779 * to sqe's, would try to gracefully shutdown the controller by
2780 * writing the registers for shutdown and polling (call
2781 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
2782 * just aborted and we will wait on those contexts, and given
2783 * there was no indication of how live the controlelr is on the
2784 * link, don't send more io to create more contexts for the
2785 * shutdown. Let the controller fail via keepalive failure if
2786 * its still present.
2790 * clean up the admin queue. Same thing as above.
2791 * use blk_mq_tagset_busy_itr() and the transport routine to
2792 * terminate the exchanges.
2794 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
2795 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2796 nvme_fc_terminate_exchange, &ctrl->ctrl);
2798 /* kill the aens as they are a separate path */
2799 nvme_fc_abort_aen_ops(ctrl);
2801 /* wait for all io that had to be aborted */
2802 spin_lock_irq(&ctrl->lock);
2803 wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
2804 ctrl->flags &= ~FCCTRL_TERMIO;
2805 spin_unlock_irq(&ctrl->lock);
2807 nvme_fc_term_aen_ops(ctrl);
2810 * send a Disconnect(association) LS to fc-nvme target
2811 * Note: could have been sent at top of process, but
2812 * cleaner on link traffic if after the aborts complete.
2813 * Note: if association doesn't exist, association_id will be 0
2815 if (ctrl->association_id)
2816 nvme_fc_xmt_disconnect_assoc(ctrl);
2818 if (ctrl->ctrl.tagset) {
2819 nvme_fc_delete_hw_io_queues(ctrl);
2820 nvme_fc_free_io_queues(ctrl);
2823 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2824 nvme_fc_free_queue(&ctrl->queues[0]);
2826 /* re-enable the admin_q so anything new can fast fail */
2827 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
2829 /* resume the io queues so that things will fast fail */
2830 nvme_start_queues(&ctrl->ctrl);
2832 nvme_fc_ctlr_inactive_on_rport(ctrl);
2836 nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
2838 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2840 cancel_work_sync(&ctrl->err_work);
2841 cancel_delayed_work_sync(&ctrl->connect_work);
2843 * kill the association on the link side. this will block
2844 * waiting for io to terminate
2846 nvme_fc_delete_association(ctrl);
2850 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
2852 struct nvme_fc_rport *rport = ctrl->rport;
2853 struct nvme_fc_remote_port *portptr = &rport->remoteport;
2854 unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
2857 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING)
2860 if (portptr->port_state == FC_OBJSTATE_ONLINE)
2861 dev_info(ctrl->ctrl.device,
2862 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
2863 ctrl->cnum, status);
2864 else if (time_after_eq(jiffies, rport->dev_loss_end))
2867 if (recon && nvmf_should_reconnect(&ctrl->ctrl)) {
2868 if (portptr->port_state == FC_OBJSTATE_ONLINE)
2869 dev_info(ctrl->ctrl.device,
2870 "NVME-FC{%d}: Reconnect attempt in %ld "
2872 ctrl->cnum, recon_delay / HZ);
2873 else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
2874 recon_delay = rport->dev_loss_end - jiffies;
2876 queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
2878 if (portptr->port_state == FC_OBJSTATE_ONLINE)
2879 dev_warn(ctrl->ctrl.device,
2880 "NVME-FC{%d}: Max reconnect attempts (%d) "
2882 ctrl->cnum, ctrl->ctrl.nr_reconnects);
2884 dev_warn(ctrl->ctrl.device,
2885 "NVME-FC{%d}: dev_loss_tmo (%d) expired "
2886 "while waiting for remoteport connectivity.\n",
2887 ctrl->cnum, portptr->dev_loss_tmo);
2888 WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
2893 __nvme_fc_terminate_io(struct nvme_fc_ctrl *ctrl)
2896 * if state is connecting - the error occurred as part of a
2897 * reconnect attempt. The create_association error paths will
2898 * clean up any outstanding io.
2900 * if it's a different state - ensure all pending io is
2901 * terminated. Given this can delay while waiting for the
2902 * aborted io to return, we recheck adapter state below
2903 * before changing state.
2905 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
2906 nvme_stop_keep_alive(&ctrl->ctrl);
2908 /* will block will waiting for io to terminate */
2909 nvme_fc_delete_association(ctrl);
2912 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING &&
2913 !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
2914 dev_err(ctrl->ctrl.device,
2915 "NVME-FC{%d}: error_recovery: Couldn't change state "
2916 "to CONNECTING\n", ctrl->cnum);
2920 nvme_fc_reset_ctrl_work(struct work_struct *work)
2922 struct nvme_fc_ctrl *ctrl =
2923 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
2926 __nvme_fc_terminate_io(ctrl);
2928 nvme_stop_ctrl(&ctrl->ctrl);
2930 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE)
2931 ret = nvme_fc_create_association(ctrl);
2936 nvme_fc_reconnect_or_delete(ctrl, ret);
2938 dev_info(ctrl->ctrl.device,
2939 "NVME-FC{%d}: controller reset complete\n",
2944 nvme_fc_connect_err_work(struct work_struct *work)
2946 struct nvme_fc_ctrl *ctrl =
2947 container_of(work, struct nvme_fc_ctrl, err_work);
2949 __nvme_fc_terminate_io(ctrl);
2951 atomic_set(&ctrl->err_work_active, 0);
2954 * Rescheduling the connection after recovering
2955 * from the io error is left to the reconnect work
2956 * item, which is what should have stalled waiting on
2957 * the io that had the error that scheduled this work.
2961 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
2963 .module = THIS_MODULE,
2964 .flags = NVME_F_FABRICS,
2965 .reg_read32 = nvmf_reg_read32,
2966 .reg_read64 = nvmf_reg_read64,
2967 .reg_write32 = nvmf_reg_write32,
2968 .free_ctrl = nvme_fc_nvme_ctrl_freed,
2969 .submit_async_event = nvme_fc_submit_async_event,
2970 .delete_ctrl = nvme_fc_delete_ctrl,
2971 .get_address = nvmf_get_address,
2975 nvme_fc_connect_ctrl_work(struct work_struct *work)
2979 struct nvme_fc_ctrl *ctrl =
2980 container_of(to_delayed_work(work),
2981 struct nvme_fc_ctrl, connect_work);
2983 ret = nvme_fc_create_association(ctrl);
2985 nvme_fc_reconnect_or_delete(ctrl, ret);
2987 dev_info(ctrl->ctrl.device,
2988 "NVME-FC{%d}: controller connect complete\n",
2993 static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
2994 .queue_rq = nvme_fc_queue_rq,
2995 .complete = nvme_fc_complete_rq,
2996 .init_request = nvme_fc_init_request,
2997 .exit_request = nvme_fc_exit_request,
2998 .init_hctx = nvme_fc_init_admin_hctx,
2999 .timeout = nvme_fc_timeout,
3004 * Fails a controller request if it matches an existing controller
3005 * (association) with the same tuple:
3006 * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
3008 * The ports don't need to be compared as they are intrinsically
3009 * already matched by the port pointers supplied.
3012 nvme_fc_existing_controller(struct nvme_fc_rport *rport,
3013 struct nvmf_ctrl_options *opts)
3015 struct nvme_fc_ctrl *ctrl;
3016 unsigned long flags;
3019 spin_lock_irqsave(&rport->lock, flags);
3020 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3021 found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
3025 spin_unlock_irqrestore(&rport->lock, flags);
3030 static struct nvme_ctrl *
3031 nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3032 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3034 struct nvme_fc_ctrl *ctrl;
3035 unsigned long flags;
3038 if (!(rport->remoteport.port_role &
3039 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
3044 if (!opts->duplicate_connect &&
3045 nvme_fc_existing_controller(rport, opts)) {
3050 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3056 idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
3062 ctrl->ctrl.opts = opts;
3063 ctrl->ctrl.nr_reconnects = 0;
3064 INIT_LIST_HEAD(&ctrl->ctrl_list);
3065 ctrl->lport = lport;
3066 ctrl->rport = rport;
3067 ctrl->dev = lport->dev;
3069 ctrl->ioq_live = false;
3070 ctrl->assoc_active = false;
3071 atomic_set(&ctrl->err_work_active, 0);
3072 init_waitqueue_head(&ctrl->ioabort_wait);
3074 get_device(ctrl->dev);
3075 kref_init(&ctrl->ref);
3077 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
3078 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
3079 INIT_WORK(&ctrl->err_work, nvme_fc_connect_err_work);
3080 spin_lock_init(&ctrl->lock);
3082 /* io queue count */
3083 ctrl->ctrl.queue_count = min_t(unsigned int,
3085 lport->ops->max_hw_queues);
3086 ctrl->ctrl.queue_count++; /* +1 for admin queue */
3088 ctrl->ctrl.sqsize = opts->queue_size - 1;
3089 ctrl->ctrl.kato = opts->kato;
3090 ctrl->ctrl.cntlid = 0xffff;
3093 ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3094 sizeof(struct nvme_fc_queue), GFP_KERNEL);
3098 nvme_fc_init_queue(ctrl, 0);
3100 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
3101 ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
3102 ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
3103 ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
3104 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
3105 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
3107 sizeof(struct scatterlist)) +
3108 ctrl->lport->ops->fcprqst_priv_sz;
3109 ctrl->admin_tag_set.driver_data = ctrl;
3110 ctrl->admin_tag_set.nr_hw_queues = 1;
3111 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
3112 ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
3114 ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
3116 goto out_free_queues;
3117 ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
3119 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
3120 if (IS_ERR(ctrl->ctrl.admin_q)) {
3121 ret = PTR_ERR(ctrl->ctrl.admin_q);
3122 goto out_free_admin_tag_set;
3126 * Would have been nice to init io queues tag set as well.
3127 * However, we require interaction from the controller
3128 * for max io queue count before we can do so.
3129 * Defer this to the connect path.
3132 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3134 goto out_cleanup_admin_q;
3136 /* at this point, teardown path changes to ref counting on nvme ctrl */
3138 spin_lock_irqsave(&rport->lock, flags);
3139 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3140 spin_unlock_irqrestore(&rport->lock, flags);
3142 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING) ||
3143 !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
3144 dev_err(ctrl->ctrl.device,
3145 "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
3149 nvme_get_ctrl(&ctrl->ctrl);
3151 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3152 nvme_put_ctrl(&ctrl->ctrl);
3153 dev_err(ctrl->ctrl.device,
3154 "NVME-FC{%d}: failed to schedule initial connect\n",
3159 flush_delayed_work(&ctrl->connect_work);
3161 dev_info(ctrl->ctrl.device,
3162 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
3163 ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
3168 nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3169 cancel_work_sync(&ctrl->ctrl.reset_work);
3170 cancel_work_sync(&ctrl->err_work);
3171 cancel_delayed_work_sync(&ctrl->connect_work);
3173 ctrl->ctrl.opts = NULL;
3175 /* initiate nvme ctrl ref counting teardown */
3176 nvme_uninit_ctrl(&ctrl->ctrl);
3178 /* Remove core ctrl ref. */
3179 nvme_put_ctrl(&ctrl->ctrl);
3181 /* as we're past the point where we transition to the ref
3182 * counting teardown path, if we return a bad pointer here,
3183 * the calling routine, thinking it's prior to the
3184 * transition, will do an rport put. Since the teardown
3185 * path also does a rport put, we do an extra get here to
3186 * so proper order/teardown happens.
3188 nvme_fc_rport_get(rport);
3190 return ERR_PTR(-EIO);
3192 out_cleanup_admin_q:
3193 blk_cleanup_queue(ctrl->ctrl.admin_q);
3194 out_free_admin_tag_set:
3195 blk_mq_free_tag_set(&ctrl->admin_tag_set);
3197 kfree(ctrl->queues);
3199 put_device(ctrl->dev);
3200 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
3204 /* exit via here doesn't follow ctlr ref points */
3205 return ERR_PTR(ret);
3209 struct nvmet_fc_traddr {
3215 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3219 if (match_u64(sstr, &token64))
3227 * This routine validates and extracts the WWN's from the TRADDR string.
3228 * As kernel parsers need the 0x to determine number base, universally
3229 * build string to parse with 0x prefix before parsing name strings.
3232 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
3234 char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
3235 substring_t wwn = { name, &name[sizeof(name)-1] };
3236 int nnoffset, pnoffset;
3238 /* validate it string one of the 2 allowed formats */
3239 if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
3240 !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
3241 !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
3242 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
3243 nnoffset = NVME_FC_TRADDR_OXNNLEN;
3244 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
3245 NVME_FC_TRADDR_OXNNLEN;
3246 } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
3247 !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
3248 !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
3249 "pn-", NVME_FC_TRADDR_NNLEN))) {
3250 nnoffset = NVME_FC_TRADDR_NNLEN;
3251 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
3257 name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
3259 memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3260 if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
3263 memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3264 if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
3270 pr_warn("%s: bad traddr string\n", __func__);
3274 static struct nvme_ctrl *
3275 nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3277 struct nvme_fc_lport *lport;
3278 struct nvme_fc_rport *rport;
3279 struct nvme_ctrl *ctrl;
3280 struct nvmet_fc_traddr laddr = { 0L, 0L };
3281 struct nvmet_fc_traddr raddr = { 0L, 0L };
3282 unsigned long flags;
3285 ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3286 if (ret || !raddr.nn || !raddr.pn)
3287 return ERR_PTR(-EINVAL);
3289 ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3290 if (ret || !laddr.nn || !laddr.pn)
3291 return ERR_PTR(-EINVAL);
3293 /* find the host and remote ports to connect together */
3294 spin_lock_irqsave(&nvme_fc_lock, flags);
3295 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3296 if (lport->localport.node_name != laddr.nn ||
3297 lport->localport.port_name != laddr.pn ||
3298 lport->localport.port_state != FC_OBJSTATE_ONLINE)
3301 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3302 if (rport->remoteport.node_name != raddr.nn ||
3303 rport->remoteport.port_name != raddr.pn ||
3304 rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3307 /* if fail to get reference fall through. Will error */
3308 if (!nvme_fc_rport_get(rport))
3311 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3313 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3315 nvme_fc_rport_put(rport);
3319 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3321 pr_warn("%s: %s - %s combination not found\n",
3322 __func__, opts->traddr, opts->host_traddr);
3323 return ERR_PTR(-ENOENT);
3327 static struct nvmf_transport_ops nvme_fc_transport = {
3329 .module = THIS_MODULE,
3330 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
3331 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3332 .create_ctrl = nvme_fc_create_ctrl,
3335 static int __init nvme_fc_init_module(void)
3339 nvme_fc_wq = alloc_workqueue("nvme_fc_wq", WQ_MEM_RECLAIM, 0);
3345 * It is expected that in the future the kernel will combine
3346 * the FC-isms that are currently under scsi and now being
3347 * added to by NVME into a new standalone FC class. The SCSI
3348 * and NVME protocols and their devices would be under this
3351 * As we need something to post FC-specific udev events to,
3352 * specifically for nvme probe events, start by creating the
3353 * new device class. When the new standalone FC class is
3354 * put in place, this code will move to a more generic
3355 * location for the class.
3357 fc_class = class_create(THIS_MODULE, "fc");
3358 if (IS_ERR(fc_class)) {
3359 pr_err("couldn't register class fc\n");
3360 ret = PTR_ERR(fc_class);
3361 goto out_destroy_wq;
3365 * Create a device for the FC-centric udev events
3367 fc_udev_device = device_create(fc_class, NULL, MKDEV(0, 0), NULL,
3369 if (IS_ERR(fc_udev_device)) {
3370 pr_err("couldn't create fc_udev device!\n");
3371 ret = PTR_ERR(fc_udev_device);
3372 goto out_destroy_class;
3375 ret = nvmf_register_transport(&nvme_fc_transport);
3377 goto out_destroy_device;
3382 device_destroy(fc_class, MKDEV(0, 0));
3384 class_destroy(fc_class);
3386 destroy_workqueue(nvme_fc_wq);
3391 static void __exit nvme_fc_exit_module(void)
3393 /* sanity check - all lports should be removed */
3394 if (!list_empty(&nvme_fc_lport_list))
3395 pr_warn("%s: localport list not empty\n", __func__);
3397 nvmf_unregister_transport(&nvme_fc_transport);
3399 ida_destroy(&nvme_fc_local_port_cnt);
3400 ida_destroy(&nvme_fc_ctrl_cnt);
3402 device_destroy(fc_class, MKDEV(0, 0));
3403 class_destroy(fc_class);
3404 destroy_workqueue(nvme_fc_wq);
3407 module_init(nvme_fc_init_module);
3408 module_exit(nvme_fc_exit_module);
3410 MODULE_LICENSE("GPL v2");