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 */
29 #define NVME_FC_DEFAULT_RECONNECT_TMO 2 /* delay between reconnects
30 * when connected and a
34 struct nvme_fc_queue {
35 struct nvme_fc_ctrl *ctrl;
37 struct blk_mq_hw_ctx *hctx;
39 size_t cmnd_capsule_len;
48 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
50 enum nvme_fcop_flags {
51 FCOP_FLAGS_TERMIO = (1 << 0),
52 FCOP_FLAGS_AEN = (1 << 1),
55 struct nvmefc_ls_req_op {
56 struct nvmefc_ls_req ls_req;
58 struct nvme_fc_rport *rport;
59 struct nvme_fc_queue *queue;
64 struct completion ls_done;
65 struct list_head lsreq_list; /* rport->ls_req_list */
69 struct nvmefc_ls_rcv_op {
70 struct nvme_fc_rport *rport;
71 struct nvmefc_ls_rsp *lsrsp;
72 union nvmefc_ls_requests *rqstbuf;
73 union nvmefc_ls_responses *rspbuf;
77 struct list_head lsrcv_list; /* rport->ls_rcv_list */
78 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
80 enum nvme_fcpop_state {
81 FCPOP_STATE_UNINIT = 0,
83 FCPOP_STATE_ACTIVE = 2,
84 FCPOP_STATE_ABORTED = 3,
85 FCPOP_STATE_COMPLETE = 4,
88 struct nvme_fc_fcp_op {
89 struct nvme_request nreq; /*
92 * the 1st element in the
97 struct nvmefc_fcp_req fcp_req;
99 struct nvme_fc_ctrl *ctrl;
100 struct nvme_fc_queue *queue;
108 struct nvme_fc_cmd_iu cmd_iu;
109 struct nvme_fc_ersp_iu rsp_iu;
112 struct nvme_fcp_op_w_sgl {
113 struct nvme_fc_fcp_op op;
114 struct scatterlist sgl[NVME_INLINE_SG_CNT];
118 struct nvme_fc_lport {
119 struct nvme_fc_local_port localport;
122 struct list_head port_list; /* nvme_fc_port_list */
123 struct list_head endp_list;
124 struct device *dev; /* physical device for dma */
125 struct nvme_fc_port_template *ops;
127 atomic_t act_rport_cnt;
128 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
130 struct nvme_fc_rport {
131 struct nvme_fc_remote_port remoteport;
133 struct list_head endp_list; /* for lport->endp_list */
134 struct list_head ctrl_list;
135 struct list_head ls_req_list;
136 struct list_head ls_rcv_list;
137 struct list_head disc_list;
138 struct device *dev; /* physical device for dma */
139 struct nvme_fc_lport *lport;
142 atomic_t act_ctrl_cnt;
143 unsigned long dev_loss_end;
144 struct work_struct lsrcv_work;
145 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
147 /* fc_ctrl flags values - specified as bit positions */
148 #define ASSOC_ACTIVE 0
149 #define ASSOC_FAILED 1
150 #define FCCTRL_TERMIO 2
152 struct nvme_fc_ctrl {
154 struct nvme_fc_queue *queues;
156 struct nvme_fc_lport *lport;
157 struct nvme_fc_rport *rport;
162 struct nvmefc_ls_rcv_op *rcv_disconn;
164 struct list_head ctrl_list; /* rport->ctrl_list */
166 struct blk_mq_tag_set admin_tag_set;
167 struct blk_mq_tag_set tag_set;
169 struct delayed_work connect_work;
174 wait_queue_head_t ioabort_wait;
176 struct nvme_fc_fcp_op aen_ops[NVME_NR_AEN_COMMANDS];
178 struct nvme_ctrl ctrl;
181 static inline struct nvme_fc_ctrl *
182 to_fc_ctrl(struct nvme_ctrl *ctrl)
184 return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
187 static inline struct nvme_fc_lport *
188 localport_to_lport(struct nvme_fc_local_port *portptr)
190 return container_of(portptr, struct nvme_fc_lport, localport);
193 static inline struct nvme_fc_rport *
194 remoteport_to_rport(struct nvme_fc_remote_port *portptr)
196 return container_of(portptr, struct nvme_fc_rport, remoteport);
199 static inline struct nvmefc_ls_req_op *
200 ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
202 return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
205 static inline struct nvme_fc_fcp_op *
206 fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
208 return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
213 /* *************************** Globals **************************** */
216 static DEFINE_SPINLOCK(nvme_fc_lock);
218 static LIST_HEAD(nvme_fc_lport_list);
219 static DEFINE_IDA(nvme_fc_local_port_cnt);
220 static DEFINE_IDA(nvme_fc_ctrl_cnt);
222 static struct workqueue_struct *nvme_fc_wq;
224 static bool nvme_fc_waiting_to_unload;
225 static DECLARE_COMPLETION(nvme_fc_unload_proceed);
228 * These items are short-term. They will eventually be moved into
229 * a generic FC class. See comments in module init.
231 static struct device *fc_udev_device;
233 static void nvme_fc_complete_rq(struct request *rq);
235 /* *********************** FC-NVME Port Management ************************ */
237 static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
238 struct nvme_fc_queue *, unsigned int);
240 static void nvme_fc_handle_ls_rqst_work(struct work_struct *work);
244 nvme_fc_free_lport(struct kref *ref)
246 struct nvme_fc_lport *lport =
247 container_of(ref, struct nvme_fc_lport, ref);
250 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
251 WARN_ON(!list_empty(&lport->endp_list));
253 /* remove from transport list */
254 spin_lock_irqsave(&nvme_fc_lock, flags);
255 list_del(&lport->port_list);
256 if (nvme_fc_waiting_to_unload && list_empty(&nvme_fc_lport_list))
257 complete(&nvme_fc_unload_proceed);
258 spin_unlock_irqrestore(&nvme_fc_lock, flags);
260 ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
261 ida_destroy(&lport->endp_cnt);
263 put_device(lport->dev);
269 nvme_fc_lport_put(struct nvme_fc_lport *lport)
271 kref_put(&lport->ref, nvme_fc_free_lport);
275 nvme_fc_lport_get(struct nvme_fc_lport *lport)
277 return kref_get_unless_zero(&lport->ref);
281 static struct nvme_fc_lport *
282 nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo,
283 struct nvme_fc_port_template *ops,
286 struct nvme_fc_lport *lport;
289 spin_lock_irqsave(&nvme_fc_lock, flags);
291 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
292 if (lport->localport.node_name != pinfo->node_name ||
293 lport->localport.port_name != pinfo->port_name)
296 if (lport->dev != dev) {
297 lport = ERR_PTR(-EXDEV);
301 if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
302 lport = ERR_PTR(-EEXIST);
306 if (!nvme_fc_lport_get(lport)) {
308 * fails if ref cnt already 0. If so,
309 * act as if lport already deleted
315 /* resume the lport */
318 lport->localport.port_role = pinfo->port_role;
319 lport->localport.port_id = pinfo->port_id;
320 lport->localport.port_state = FC_OBJSTATE_ONLINE;
322 spin_unlock_irqrestore(&nvme_fc_lock, flags);
330 spin_unlock_irqrestore(&nvme_fc_lock, flags);
336 * nvme_fc_register_localport - transport entry point called by an
337 * LLDD to register the existence of a NVME
339 * @pinfo: pointer to information about the port to be registered
340 * @template: LLDD entrypoints and operational parameters for the port
341 * @dev: physical hardware device node port corresponds to. Will be
342 * used for DMA mappings
343 * @portptr: pointer to a local port pointer. Upon success, the routine
344 * will allocate a nvme_fc_local_port structure and place its
345 * address in the local port pointer. Upon failure, local port
346 * pointer will be set to 0.
349 * a completion status. Must be 0 upon success; a negative errno
350 * (ex: -ENXIO) upon failure.
353 nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
354 struct nvme_fc_port_template *template,
356 struct nvme_fc_local_port **portptr)
358 struct nvme_fc_lport *newrec;
362 if (!template->localport_delete || !template->remoteport_delete ||
363 !template->ls_req || !template->fcp_io ||
364 !template->ls_abort || !template->fcp_abort ||
365 !template->max_hw_queues || !template->max_sgl_segments ||
366 !template->max_dif_sgl_segments || !template->dma_boundary) {
368 goto out_reghost_failed;
372 * look to see if there is already a localport that had been
373 * deregistered and in the process of waiting for all the
374 * references to fully be removed. If the references haven't
375 * expired, we can simply re-enable the localport. Remoteports
376 * and controller reconnections should resume naturally.
378 newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
380 /* found an lport, but something about its state is bad */
381 if (IS_ERR(newrec)) {
382 ret = PTR_ERR(newrec);
383 goto out_reghost_failed;
385 /* found existing lport, which was resumed */
387 *portptr = &newrec->localport;
391 /* nothing found - allocate a new localport struct */
393 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
397 goto out_reghost_failed;
400 idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
406 if (!get_device(dev) && dev) {
411 INIT_LIST_HEAD(&newrec->port_list);
412 INIT_LIST_HEAD(&newrec->endp_list);
413 kref_init(&newrec->ref);
414 atomic_set(&newrec->act_rport_cnt, 0);
415 newrec->ops = template;
417 ida_init(&newrec->endp_cnt);
418 if (template->local_priv_sz)
419 newrec->localport.private = &newrec[1];
421 newrec->localport.private = NULL;
422 newrec->localport.node_name = pinfo->node_name;
423 newrec->localport.port_name = pinfo->port_name;
424 newrec->localport.port_role = pinfo->port_role;
425 newrec->localport.port_id = pinfo->port_id;
426 newrec->localport.port_state = FC_OBJSTATE_ONLINE;
427 newrec->localport.port_num = idx;
429 spin_lock_irqsave(&nvme_fc_lock, flags);
430 list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
431 spin_unlock_irqrestore(&nvme_fc_lock, flags);
434 dma_set_seg_boundary(dev, template->dma_boundary);
436 *portptr = &newrec->localport;
440 ida_simple_remove(&nvme_fc_local_port_cnt, idx);
448 EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
451 * nvme_fc_unregister_localport - transport entry point called by an
452 * LLDD to deregister/remove a previously
453 * registered a NVME host FC port.
454 * @portptr: pointer to the (registered) local port that is to be deregistered.
457 * a completion status. Must be 0 upon success; a negative errno
458 * (ex: -ENXIO) upon failure.
461 nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
463 struct nvme_fc_lport *lport = localport_to_lport(portptr);
469 spin_lock_irqsave(&nvme_fc_lock, flags);
471 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
472 spin_unlock_irqrestore(&nvme_fc_lock, flags);
475 portptr->port_state = FC_OBJSTATE_DELETED;
477 spin_unlock_irqrestore(&nvme_fc_lock, flags);
479 if (atomic_read(&lport->act_rport_cnt) == 0)
480 lport->ops->localport_delete(&lport->localport);
482 nvme_fc_lport_put(lport);
486 EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
489 * TRADDR strings, per FC-NVME are fixed format:
490 * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
491 * udev event will only differ by prefix of what field is
493 * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
494 * 19 + 43 + null_fudge = 64 characters
496 #define FCNVME_TRADDR_LENGTH 64
499 nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
500 struct nvme_fc_rport *rport)
502 char hostaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_HOST_TRADDR=...*/
503 char tgtaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_TRADDR=...*/
504 char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
506 if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
509 snprintf(hostaddr, sizeof(hostaddr),
510 "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
511 lport->localport.node_name, lport->localport.port_name);
512 snprintf(tgtaddr, sizeof(tgtaddr),
513 "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
514 rport->remoteport.node_name, rport->remoteport.port_name);
515 kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
519 nvme_fc_free_rport(struct kref *ref)
521 struct nvme_fc_rport *rport =
522 container_of(ref, struct nvme_fc_rport, ref);
523 struct nvme_fc_lport *lport =
524 localport_to_lport(rport->remoteport.localport);
527 WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
528 WARN_ON(!list_empty(&rport->ctrl_list));
530 /* remove from lport list */
531 spin_lock_irqsave(&nvme_fc_lock, flags);
532 list_del(&rport->endp_list);
533 spin_unlock_irqrestore(&nvme_fc_lock, flags);
535 WARN_ON(!list_empty(&rport->disc_list));
536 ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
540 nvme_fc_lport_put(lport);
544 nvme_fc_rport_put(struct nvme_fc_rport *rport)
546 kref_put(&rport->ref, nvme_fc_free_rport);
550 nvme_fc_rport_get(struct nvme_fc_rport *rport)
552 return kref_get_unless_zero(&rport->ref);
556 nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl)
558 switch (ctrl->ctrl.state) {
560 case NVME_CTRL_CONNECTING:
562 * As all reconnects were suppressed, schedule a
565 dev_info(ctrl->ctrl.device,
566 "NVME-FC{%d}: connectivity re-established. "
567 "Attempting reconnect\n", ctrl->cnum);
569 queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
572 case NVME_CTRL_RESETTING:
574 * Controller is already in the process of terminating the
575 * association. No need to do anything further. The reconnect
576 * step will naturally occur after the reset completes.
581 /* no action to take - let it delete */
586 static struct nvme_fc_rport *
587 nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport,
588 struct nvme_fc_port_info *pinfo)
590 struct nvme_fc_rport *rport;
591 struct nvme_fc_ctrl *ctrl;
594 spin_lock_irqsave(&nvme_fc_lock, flags);
596 list_for_each_entry(rport, &lport->endp_list, endp_list) {
597 if (rport->remoteport.node_name != pinfo->node_name ||
598 rport->remoteport.port_name != pinfo->port_name)
601 if (!nvme_fc_rport_get(rport)) {
602 rport = ERR_PTR(-ENOLCK);
606 spin_unlock_irqrestore(&nvme_fc_lock, flags);
608 spin_lock_irqsave(&rport->lock, flags);
610 /* has it been unregistered */
611 if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) {
612 /* means lldd called us twice */
613 spin_unlock_irqrestore(&rport->lock, flags);
614 nvme_fc_rport_put(rport);
615 return ERR_PTR(-ESTALE);
618 rport->remoteport.port_role = pinfo->port_role;
619 rport->remoteport.port_id = pinfo->port_id;
620 rport->remoteport.port_state = FC_OBJSTATE_ONLINE;
621 rport->dev_loss_end = 0;
624 * kick off a reconnect attempt on all associations to the
625 * remote port. A successful reconnects will resume i/o.
627 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
628 nvme_fc_resume_controller(ctrl);
630 spin_unlock_irqrestore(&rport->lock, flags);
638 spin_unlock_irqrestore(&nvme_fc_lock, flags);
644 __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport,
645 struct nvme_fc_port_info *pinfo)
647 if (pinfo->dev_loss_tmo)
648 rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
650 rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
654 * nvme_fc_register_remoteport - transport entry point called by an
655 * LLDD to register the existence of a NVME
656 * subsystem FC port on its fabric.
657 * @localport: pointer to the (registered) local port that the remote
658 * subsystem port is connected to.
659 * @pinfo: pointer to information about the port to be registered
660 * @portptr: pointer to a remote port pointer. Upon success, the routine
661 * will allocate a nvme_fc_remote_port structure and place its
662 * address in the remote port pointer. Upon failure, remote port
663 * pointer will be set to 0.
666 * a completion status. Must be 0 upon success; a negative errno
667 * (ex: -ENXIO) upon failure.
670 nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
671 struct nvme_fc_port_info *pinfo,
672 struct nvme_fc_remote_port **portptr)
674 struct nvme_fc_lport *lport = localport_to_lport(localport);
675 struct nvme_fc_rport *newrec;
679 if (!nvme_fc_lport_get(lport)) {
681 goto out_reghost_failed;
685 * look to see if there is already a remoteport that is waiting
686 * for a reconnect (within dev_loss_tmo) with the same WWN's.
687 * If so, transition to it and reconnect.
689 newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
691 /* found an rport, but something about its state is bad */
692 if (IS_ERR(newrec)) {
693 ret = PTR_ERR(newrec);
696 /* found existing rport, which was resumed */
698 nvme_fc_lport_put(lport);
699 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
700 nvme_fc_signal_discovery_scan(lport, newrec);
701 *portptr = &newrec->remoteport;
705 /* nothing found - allocate a new remoteport struct */
707 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
714 idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
717 goto out_kfree_rport;
720 INIT_LIST_HEAD(&newrec->endp_list);
721 INIT_LIST_HEAD(&newrec->ctrl_list);
722 INIT_LIST_HEAD(&newrec->ls_req_list);
723 INIT_LIST_HEAD(&newrec->disc_list);
724 kref_init(&newrec->ref);
725 atomic_set(&newrec->act_ctrl_cnt, 0);
726 spin_lock_init(&newrec->lock);
727 newrec->remoteport.localport = &lport->localport;
728 INIT_LIST_HEAD(&newrec->ls_rcv_list);
729 newrec->dev = lport->dev;
730 newrec->lport = lport;
731 if (lport->ops->remote_priv_sz)
732 newrec->remoteport.private = &newrec[1];
734 newrec->remoteport.private = NULL;
735 newrec->remoteport.port_role = pinfo->port_role;
736 newrec->remoteport.node_name = pinfo->node_name;
737 newrec->remoteport.port_name = pinfo->port_name;
738 newrec->remoteport.port_id = pinfo->port_id;
739 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
740 newrec->remoteport.port_num = idx;
741 __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
742 INIT_WORK(&newrec->lsrcv_work, nvme_fc_handle_ls_rqst_work);
744 spin_lock_irqsave(&nvme_fc_lock, flags);
745 list_add_tail(&newrec->endp_list, &lport->endp_list);
746 spin_unlock_irqrestore(&nvme_fc_lock, flags);
748 nvme_fc_signal_discovery_scan(lport, newrec);
750 *portptr = &newrec->remoteport;
756 nvme_fc_lport_put(lport);
761 EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
764 nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
766 struct nvmefc_ls_req_op *lsop;
770 spin_lock_irqsave(&rport->lock, flags);
772 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
773 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
774 lsop->flags |= FCOP_FLAGS_TERMIO;
775 spin_unlock_irqrestore(&rport->lock, flags);
776 rport->lport->ops->ls_abort(&rport->lport->localport,
782 spin_unlock_irqrestore(&rport->lock, flags);
788 nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
790 dev_info(ctrl->ctrl.device,
791 "NVME-FC{%d}: controller connectivity lost. Awaiting "
792 "Reconnect", ctrl->cnum);
794 switch (ctrl->ctrl.state) {
798 * Schedule a controller reset. The reset will terminate the
799 * association and schedule the reconnect timer. Reconnects
800 * will be attempted until either the ctlr_loss_tmo
801 * (max_retries * connect_delay) expires or the remoteport's
802 * dev_loss_tmo expires.
804 if (nvme_reset_ctrl(&ctrl->ctrl)) {
805 dev_warn(ctrl->ctrl.device,
806 "NVME-FC{%d}: Couldn't schedule reset.\n",
808 nvme_delete_ctrl(&ctrl->ctrl);
812 case NVME_CTRL_CONNECTING:
814 * The association has already been terminated and the
815 * controller is attempting reconnects. No need to do anything
816 * futher. Reconnects will be attempted until either the
817 * ctlr_loss_tmo (max_retries * connect_delay) expires or the
818 * remoteport's dev_loss_tmo expires.
822 case NVME_CTRL_RESETTING:
824 * Controller is already in the process of terminating the
825 * association. No need to do anything further. The reconnect
826 * step will kick in naturally after the association is
831 case NVME_CTRL_DELETING:
832 case NVME_CTRL_DELETING_NOIO:
834 /* no action to take - let it delete */
840 * nvme_fc_unregister_remoteport - transport entry point called by an
841 * LLDD to deregister/remove a previously
842 * registered a NVME subsystem FC port.
843 * @portptr: pointer to the (registered) remote port that is to be
847 * a completion status. Must be 0 upon success; a negative errno
848 * (ex: -ENXIO) upon failure.
851 nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
853 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
854 struct nvme_fc_ctrl *ctrl;
860 spin_lock_irqsave(&rport->lock, flags);
862 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
863 spin_unlock_irqrestore(&rport->lock, flags);
866 portptr->port_state = FC_OBJSTATE_DELETED;
868 rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
870 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
871 /* if dev_loss_tmo==0, dev loss is immediate */
872 if (!portptr->dev_loss_tmo) {
873 dev_warn(ctrl->ctrl.device,
874 "NVME-FC{%d}: controller connectivity lost.\n",
876 nvme_delete_ctrl(&ctrl->ctrl);
878 nvme_fc_ctrl_connectivity_loss(ctrl);
881 spin_unlock_irqrestore(&rport->lock, flags);
883 nvme_fc_abort_lsops(rport);
885 if (atomic_read(&rport->act_ctrl_cnt) == 0)
886 rport->lport->ops->remoteport_delete(portptr);
889 * release the reference, which will allow, if all controllers
890 * go away, which should only occur after dev_loss_tmo occurs,
891 * for the rport to be torn down.
893 nvme_fc_rport_put(rport);
897 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
900 * nvme_fc_rescan_remoteport - transport entry point called by an
901 * LLDD to request a nvme device rescan.
902 * @remoteport: pointer to the (registered) remote port that is to be
908 nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
910 struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
912 nvme_fc_signal_discovery_scan(rport->lport, rport);
914 EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
917 nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
920 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
923 spin_lock_irqsave(&rport->lock, flags);
925 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
926 spin_unlock_irqrestore(&rport->lock, flags);
930 /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
931 rport->remoteport.dev_loss_tmo = dev_loss_tmo;
933 spin_unlock_irqrestore(&rport->lock, flags);
937 EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
940 /* *********************** FC-NVME DMA Handling **************************** */
943 * The fcloop device passes in a NULL device pointer. Real LLD's will
944 * pass in a valid device pointer. If NULL is passed to the dma mapping
945 * routines, depending on the platform, it may or may not succeed, and
949 * Wrapper all the dma routines and check the dev pointer.
951 * If simple mappings (return just a dma address, we'll noop them,
952 * returning a dma address of 0.
954 * On more complex mappings (dma_map_sg), a pseudo routine fills
955 * in the scatter list, setting all dma addresses to 0.
958 static inline dma_addr_t
959 fc_dma_map_single(struct device *dev, void *ptr, size_t size,
960 enum dma_data_direction dir)
962 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
966 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
968 return dev ? dma_mapping_error(dev, dma_addr) : 0;
972 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
973 enum dma_data_direction dir)
976 dma_unmap_single(dev, addr, size, dir);
980 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
981 enum dma_data_direction dir)
984 dma_sync_single_for_cpu(dev, addr, size, dir);
988 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
989 enum dma_data_direction dir)
992 dma_sync_single_for_device(dev, addr, size, dir);
995 /* pseudo dma_map_sg call */
997 fc_map_sg(struct scatterlist *sg, int nents)
999 struct scatterlist *s;
1002 WARN_ON(nents == 0 || sg[0].length == 0);
1004 for_each_sg(sg, s, nents, i) {
1005 s->dma_address = 0L;
1006 #ifdef CONFIG_NEED_SG_DMA_LENGTH
1007 s->dma_length = s->length;
1014 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1015 enum dma_data_direction dir)
1017 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
1021 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1022 enum dma_data_direction dir)
1025 dma_unmap_sg(dev, sg, nents, dir);
1028 /* *********************** FC-NVME LS Handling **************************** */
1030 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
1031 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
1033 static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
1036 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
1038 struct nvme_fc_rport *rport = lsop->rport;
1039 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1040 unsigned long flags;
1042 spin_lock_irqsave(&rport->lock, flags);
1044 if (!lsop->req_queued) {
1045 spin_unlock_irqrestore(&rport->lock, flags);
1049 list_del(&lsop->lsreq_list);
1051 lsop->req_queued = false;
1053 spin_unlock_irqrestore(&rport->lock, flags);
1055 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1056 (lsreq->rqstlen + lsreq->rsplen),
1059 nvme_fc_rport_put(rport);
1063 __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
1064 struct nvmefc_ls_req_op *lsop,
1065 void (*done)(struct nvmefc_ls_req *req, int status))
1067 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1068 unsigned long flags;
1071 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1072 return -ECONNREFUSED;
1074 if (!nvme_fc_rport_get(rport))
1078 lsop->rport = rport;
1079 lsop->req_queued = false;
1080 INIT_LIST_HEAD(&lsop->lsreq_list);
1081 init_completion(&lsop->ls_done);
1083 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
1084 lsreq->rqstlen + lsreq->rsplen,
1086 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1090 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1092 spin_lock_irqsave(&rport->lock, flags);
1094 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
1096 lsop->req_queued = true;
1098 spin_unlock_irqrestore(&rport->lock, flags);
1100 ret = rport->lport->ops->ls_req(&rport->lport->localport,
1101 &rport->remoteport, lsreq);
1108 lsop->ls_error = ret;
1109 spin_lock_irqsave(&rport->lock, flags);
1110 lsop->req_queued = false;
1111 list_del(&lsop->lsreq_list);
1112 spin_unlock_irqrestore(&rport->lock, flags);
1113 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1114 (lsreq->rqstlen + lsreq->rsplen),
1117 nvme_fc_rport_put(rport);
1123 nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1125 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1127 lsop->ls_error = status;
1128 complete(&lsop->ls_done);
1132 nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
1134 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1135 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1138 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
1142 * No timeout/not interruptible as we need the struct
1143 * to exist until the lldd calls us back. Thus mandate
1144 * wait until driver calls back. lldd responsible for
1145 * the timeout action
1147 wait_for_completion(&lsop->ls_done);
1149 __nvme_fc_finish_ls_req(lsop);
1151 ret = lsop->ls_error;
1157 /* ACC or RJT payload ? */
1158 if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1165 nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
1166 struct nvmefc_ls_req_op *lsop,
1167 void (*done)(struct nvmefc_ls_req *req, int status))
1169 /* don't wait for completion */
1171 return __nvme_fc_send_ls_req(rport, lsop, done);
1175 nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1176 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1178 struct nvmefc_ls_req_op *lsop;
1179 struct nvmefc_ls_req *lsreq;
1180 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1181 struct fcnvme_ls_cr_assoc_acc *assoc_acc;
1182 unsigned long flags;
1185 lsop = kzalloc((sizeof(*lsop) +
1186 sizeof(*assoc_rqst) + sizeof(*assoc_acc) +
1187 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1189 dev_info(ctrl->ctrl.device,
1190 "NVME-FC{%d}: send Create Association failed: ENOMEM\n",
1196 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1];
1197 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1198 lsreq = &lsop->ls_req;
1199 if (ctrl->lport->ops->lsrqst_priv_sz)
1200 lsreq->private = &assoc_acc[1];
1202 lsreq->private = NULL;
1204 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1205 assoc_rqst->desc_list_len =
1206 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1208 assoc_rqst->assoc_cmd.desc_tag =
1209 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1210 assoc_rqst->assoc_cmd.desc_len =
1212 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1214 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1215 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
1216 /* Linux supports only Dynamic controllers */
1217 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
1218 uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1219 strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1220 min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1221 strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1222 min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
1224 lsop->queue = queue;
1225 lsreq->rqstaddr = assoc_rqst;
1226 lsreq->rqstlen = sizeof(*assoc_rqst);
1227 lsreq->rspaddr = assoc_acc;
1228 lsreq->rsplen = sizeof(*assoc_acc);
1229 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1231 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1233 goto out_free_buffer;
1235 /* process connect LS completion */
1237 /* validate the ACC response */
1238 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1240 else if (assoc_acc->hdr.desc_list_len !=
1242 sizeof(struct fcnvme_ls_cr_assoc_acc)))
1243 fcret = VERR_CR_ASSOC_ACC_LEN;
1244 else if (assoc_acc->hdr.rqst.desc_tag !=
1245 cpu_to_be32(FCNVME_LSDESC_RQST))
1246 fcret = VERR_LSDESC_RQST;
1247 else if (assoc_acc->hdr.rqst.desc_len !=
1248 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1249 fcret = VERR_LSDESC_RQST_LEN;
1250 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1251 fcret = VERR_CR_ASSOC;
1252 else if (assoc_acc->associd.desc_tag !=
1253 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1254 fcret = VERR_ASSOC_ID;
1255 else if (assoc_acc->associd.desc_len !=
1257 sizeof(struct fcnvme_lsdesc_assoc_id)))
1258 fcret = VERR_ASSOC_ID_LEN;
1259 else if (assoc_acc->connectid.desc_tag !=
1260 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1261 fcret = VERR_CONN_ID;
1262 else if (assoc_acc->connectid.desc_len !=
1263 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1264 fcret = VERR_CONN_ID_LEN;
1269 "q %d Create Association LS failed: %s\n",
1270 queue->qnum, validation_errors[fcret]);
1272 spin_lock_irqsave(&ctrl->lock, flags);
1273 ctrl->association_id =
1274 be64_to_cpu(assoc_acc->associd.association_id);
1275 queue->connection_id =
1276 be64_to_cpu(assoc_acc->connectid.connection_id);
1277 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1278 spin_unlock_irqrestore(&ctrl->lock, flags);
1286 "queue %d connect admin queue failed (%d).\n",
1292 nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1293 u16 qsize, u16 ersp_ratio)
1295 struct nvmefc_ls_req_op *lsop;
1296 struct nvmefc_ls_req *lsreq;
1297 struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1298 struct fcnvme_ls_cr_conn_acc *conn_acc;
1301 lsop = kzalloc((sizeof(*lsop) +
1302 sizeof(*conn_rqst) + sizeof(*conn_acc) +
1303 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1305 dev_info(ctrl->ctrl.device,
1306 "NVME-FC{%d}: send Create Connection failed: ENOMEM\n",
1312 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)&lsop[1];
1313 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1314 lsreq = &lsop->ls_req;
1315 if (ctrl->lport->ops->lsrqst_priv_sz)
1316 lsreq->private = (void *)&conn_acc[1];
1318 lsreq->private = NULL;
1320 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1321 conn_rqst->desc_list_len = cpu_to_be32(
1322 sizeof(struct fcnvme_lsdesc_assoc_id) +
1323 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1325 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1326 conn_rqst->associd.desc_len =
1328 sizeof(struct fcnvme_lsdesc_assoc_id));
1329 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1330 conn_rqst->connect_cmd.desc_tag =
1331 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1332 conn_rqst->connect_cmd.desc_len =
1334 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1335 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1336 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
1337 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
1339 lsop->queue = queue;
1340 lsreq->rqstaddr = conn_rqst;
1341 lsreq->rqstlen = sizeof(*conn_rqst);
1342 lsreq->rspaddr = conn_acc;
1343 lsreq->rsplen = sizeof(*conn_acc);
1344 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1346 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1348 goto out_free_buffer;
1350 /* process connect LS completion */
1352 /* validate the ACC response */
1353 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1355 else if (conn_acc->hdr.desc_list_len !=
1356 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1357 fcret = VERR_CR_CONN_ACC_LEN;
1358 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1359 fcret = VERR_LSDESC_RQST;
1360 else if (conn_acc->hdr.rqst.desc_len !=
1361 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1362 fcret = VERR_LSDESC_RQST_LEN;
1363 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1364 fcret = VERR_CR_CONN;
1365 else if (conn_acc->connectid.desc_tag !=
1366 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1367 fcret = VERR_CONN_ID;
1368 else if (conn_acc->connectid.desc_len !=
1369 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1370 fcret = VERR_CONN_ID_LEN;
1375 "q %d Create I/O Connection LS failed: %s\n",
1376 queue->qnum, validation_errors[fcret]);
1378 queue->connection_id =
1379 be64_to_cpu(conn_acc->connectid.connection_id);
1380 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1388 "queue %d connect I/O queue failed (%d).\n",
1394 nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1396 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1398 __nvme_fc_finish_ls_req(lsop);
1400 /* fc-nvme initiator doesn't care about success or failure of cmd */
1406 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1407 * the FC-NVME Association. Terminating the association also
1408 * terminates the FC-NVME connections (per queue, both admin and io
1409 * queues) that are part of the association. E.g. things are torn
1410 * down, and the related FC-NVME Association ID and Connection IDs
1413 * The behavior of the fc-nvme initiator is such that it's
1414 * understanding of the association and connections will implicitly
1415 * be torn down. The action is implicit as it may be due to a loss of
1416 * connectivity with the fc-nvme target, so you may never get a
1417 * response even if you tried. As such, the action of this routine
1418 * is to asynchronously send the LS, ignore any results of the LS, and
1419 * continue on with terminating the association. If the fc-nvme target
1420 * is present and receives the LS, it too can tear down.
1423 nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1425 struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
1426 struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
1427 struct nvmefc_ls_req_op *lsop;
1428 struct nvmefc_ls_req *lsreq;
1431 lsop = kzalloc((sizeof(*lsop) +
1432 sizeof(*discon_rqst) + sizeof(*discon_acc) +
1433 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1435 dev_info(ctrl->ctrl.device,
1436 "NVME-FC{%d}: send Disconnect Association "
1442 discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)&lsop[1];
1443 discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
1444 lsreq = &lsop->ls_req;
1445 if (ctrl->lport->ops->lsrqst_priv_sz)
1446 lsreq->private = (void *)&discon_acc[1];
1448 lsreq->private = NULL;
1450 nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc,
1451 ctrl->association_id);
1453 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1454 nvme_fc_disconnect_assoc_done);
1460 nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
1462 struct nvmefc_ls_rcv_op *lsop = lsrsp->nvme_fc_private;
1463 struct nvme_fc_rport *rport = lsop->rport;
1464 struct nvme_fc_lport *lport = rport->lport;
1465 unsigned long flags;
1467 spin_lock_irqsave(&rport->lock, flags);
1468 list_del(&lsop->lsrcv_list);
1469 spin_unlock_irqrestore(&rport->lock, flags);
1471 fc_dma_sync_single_for_cpu(lport->dev, lsop->rspdma,
1472 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1473 fc_dma_unmap_single(lport->dev, lsop->rspdma,
1474 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1478 nvme_fc_rport_put(rport);
1482 nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op *lsop)
1484 struct nvme_fc_rport *rport = lsop->rport;
1485 struct nvme_fc_lport *lport = rport->lport;
1486 struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
1489 fc_dma_sync_single_for_device(lport->dev, lsop->rspdma,
1490 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1492 ret = lport->ops->xmt_ls_rsp(&lport->localport, &rport->remoteport,
1495 dev_warn(lport->dev,
1496 "LLDD rejected LS RSP xmt: LS %d status %d\n",
1498 nvme_fc_xmt_ls_rsp_done(lsop->lsrsp);
1503 static struct nvme_fc_ctrl *
1504 nvme_fc_match_disconn_ls(struct nvme_fc_rport *rport,
1505 struct nvmefc_ls_rcv_op *lsop)
1507 struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1508 &lsop->rqstbuf->rq_dis_assoc;
1509 struct nvme_fc_ctrl *ctrl, *ret = NULL;
1510 struct nvmefc_ls_rcv_op *oldls = NULL;
1511 u64 association_id = be64_to_cpu(rqst->associd.association_id);
1512 unsigned long flags;
1514 spin_lock_irqsave(&rport->lock, flags);
1516 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
1517 if (!nvme_fc_ctrl_get(ctrl))
1519 spin_lock(&ctrl->lock);
1520 if (association_id == ctrl->association_id) {
1521 oldls = ctrl->rcv_disconn;
1522 ctrl->rcv_disconn = lsop;
1525 spin_unlock(&ctrl->lock);
1527 /* leave the ctrl get reference */
1529 nvme_fc_ctrl_put(ctrl);
1532 spin_unlock_irqrestore(&rport->lock, flags);
1534 /* transmit a response for anything that was pending */
1536 dev_info(rport->lport->dev,
1537 "NVME-FC{%d}: Multiple Disconnect Association "
1538 "LS's received\n", ctrl->cnum);
1539 /* overwrite good response with bogus failure */
1540 oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf,
1541 sizeof(*oldls->rspbuf),
1544 FCNVME_RJT_EXP_NONE, 0);
1545 nvme_fc_xmt_ls_rsp(oldls);
1552 * returns true to mean LS handled and ls_rsp can be sent
1553 * returns false to defer ls_rsp xmt (will be done as part of
1554 * association termination)
1557 nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op *lsop)
1559 struct nvme_fc_rport *rport = lsop->rport;
1560 struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1561 &lsop->rqstbuf->rq_dis_assoc;
1562 struct fcnvme_ls_disconnect_assoc_acc *acc =
1563 &lsop->rspbuf->rsp_dis_assoc;
1564 struct nvme_fc_ctrl *ctrl = NULL;
1567 memset(acc, 0, sizeof(*acc));
1569 ret = nvmefc_vldt_lsreq_discon_assoc(lsop->rqstdatalen, rqst);
1571 /* match an active association */
1572 ctrl = nvme_fc_match_disconn_ls(rport, lsop);
1574 ret = VERR_NO_ASSOC;
1578 dev_info(rport->lport->dev,
1579 "Disconnect LS failed: %s\n",
1580 validation_errors[ret]);
1581 lsop->lsrsp->rsplen = nvme_fc_format_rjt(acc,
1582 sizeof(*acc), rqst->w0.ls_cmd,
1583 (ret == VERR_NO_ASSOC) ?
1584 FCNVME_RJT_RC_INV_ASSOC :
1585 FCNVME_RJT_RC_LOGIC,
1586 FCNVME_RJT_EXP_NONE, 0);
1590 /* format an ACCept response */
1592 lsop->lsrsp->rsplen = sizeof(*acc);
1594 nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1596 sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
1597 FCNVME_LS_DISCONNECT_ASSOC);
1600 * the transmit of the response will occur after the exchanges
1601 * for the association have been ABTS'd by
1602 * nvme_fc_delete_association().
1605 /* fail the association */
1606 nvme_fc_error_recovery(ctrl, "Disconnect Association LS received");
1608 /* release the reference taken by nvme_fc_match_disconn_ls() */
1609 nvme_fc_ctrl_put(ctrl);
1615 * Actual Processing routine for received FC-NVME LS Requests from the LLD
1616 * returns true if a response should be sent afterward, false if rsp will
1617 * be sent asynchronously.
1620 nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op *lsop)
1622 struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
1625 lsop->lsrsp->nvme_fc_private = lsop;
1626 lsop->lsrsp->rspbuf = lsop->rspbuf;
1627 lsop->lsrsp->rspdma = lsop->rspdma;
1628 lsop->lsrsp->done = nvme_fc_xmt_ls_rsp_done;
1629 /* Be preventative. handlers will later set to valid length */
1630 lsop->lsrsp->rsplen = 0;
1634 * parse request input, execute the request, and format the
1637 switch (w0->ls_cmd) {
1638 case FCNVME_LS_DISCONNECT_ASSOC:
1639 ret = nvme_fc_ls_disconnect_assoc(lsop);
1641 case FCNVME_LS_DISCONNECT_CONN:
1642 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1643 sizeof(*lsop->rspbuf), w0->ls_cmd,
1644 FCNVME_RJT_RC_UNSUP, FCNVME_RJT_EXP_NONE, 0);
1646 case FCNVME_LS_CREATE_ASSOCIATION:
1647 case FCNVME_LS_CREATE_CONNECTION:
1648 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1649 sizeof(*lsop->rspbuf), w0->ls_cmd,
1650 FCNVME_RJT_RC_LOGIC, FCNVME_RJT_EXP_NONE, 0);
1653 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1654 sizeof(*lsop->rspbuf), w0->ls_cmd,
1655 FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
1663 nvme_fc_handle_ls_rqst_work(struct work_struct *work)
1665 struct nvme_fc_rport *rport =
1666 container_of(work, struct nvme_fc_rport, lsrcv_work);
1667 struct fcnvme_ls_rqst_w0 *w0;
1668 struct nvmefc_ls_rcv_op *lsop;
1669 unsigned long flags;
1674 spin_lock_irqsave(&rport->lock, flags);
1675 list_for_each_entry(lsop, &rport->ls_rcv_list, lsrcv_list) {
1679 lsop->handled = true;
1680 if (rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
1681 spin_unlock_irqrestore(&rport->lock, flags);
1682 sendrsp = nvme_fc_handle_ls_rqst(lsop);
1684 spin_unlock_irqrestore(&rport->lock, flags);
1685 w0 = &lsop->rqstbuf->w0;
1686 lsop->lsrsp->rsplen = nvme_fc_format_rjt(
1688 sizeof(*lsop->rspbuf),
1691 FCNVME_RJT_EXP_NONE, 0);
1694 nvme_fc_xmt_ls_rsp(lsop);
1697 spin_unlock_irqrestore(&rport->lock, flags);
1701 * nvme_fc_rcv_ls_req - transport entry point called by an LLDD
1702 * upon the reception of a NVME LS request.
1704 * The nvme-fc layer will copy payload to an internal structure for
1705 * processing. As such, upon completion of the routine, the LLDD may
1706 * immediately free/reuse the LS request buffer passed in the call.
1708 * If this routine returns error, the LLDD should abort the exchange.
1710 * @remoteport: pointer to the (registered) remote port that the LS
1711 * was received from. The remoteport is associated with
1712 * a specific localport.
1713 * @lsrsp: pointer to a nvmefc_ls_rsp response structure to be
1714 * used to reference the exchange corresponding to the LS
1715 * when issuing an ls response.
1716 * @lsreqbuf: pointer to the buffer containing the LS Request
1717 * @lsreqbuf_len: length, in bytes, of the received LS request
1720 nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
1721 struct nvmefc_ls_rsp *lsrsp,
1722 void *lsreqbuf, u32 lsreqbuf_len)
1724 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
1725 struct nvme_fc_lport *lport = rport->lport;
1726 struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf;
1727 struct nvmefc_ls_rcv_op *lsop;
1728 unsigned long flags;
1731 nvme_fc_rport_get(rport);
1733 /* validate there's a routine to transmit a response */
1734 if (!lport->ops->xmt_ls_rsp) {
1735 dev_info(lport->dev,
1736 "RCV %s LS failed: no LLDD xmt_ls_rsp\n",
1737 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1738 nvmefc_ls_names[w0->ls_cmd] : "");
1743 if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) {
1744 dev_info(lport->dev,
1745 "RCV %s LS failed: payload too large\n",
1746 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1747 nvmefc_ls_names[w0->ls_cmd] : "");
1752 lsop = kzalloc(sizeof(*lsop) +
1753 sizeof(union nvmefc_ls_requests) +
1754 sizeof(union nvmefc_ls_responses),
1757 dev_info(lport->dev,
1758 "RCV %s LS failed: No memory\n",
1759 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1760 nvmefc_ls_names[w0->ls_cmd] : "");
1764 lsop->rqstbuf = (union nvmefc_ls_requests *)&lsop[1];
1765 lsop->rspbuf = (union nvmefc_ls_responses *)&lsop->rqstbuf[1];
1767 lsop->rspdma = fc_dma_map_single(lport->dev, lsop->rspbuf,
1768 sizeof(*lsop->rspbuf),
1770 if (fc_dma_mapping_error(lport->dev, lsop->rspdma)) {
1771 dev_info(lport->dev,
1772 "RCV %s LS failed: DMA mapping failure\n",
1773 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1774 nvmefc_ls_names[w0->ls_cmd] : "");
1779 lsop->rport = rport;
1780 lsop->lsrsp = lsrsp;
1782 memcpy(lsop->rqstbuf, lsreqbuf, lsreqbuf_len);
1783 lsop->rqstdatalen = lsreqbuf_len;
1785 spin_lock_irqsave(&rport->lock, flags);
1786 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) {
1787 spin_unlock_irqrestore(&rport->lock, flags);
1791 list_add_tail(&lsop->lsrcv_list, &rport->ls_rcv_list);
1792 spin_unlock_irqrestore(&rport->lock, flags);
1794 schedule_work(&rport->lsrcv_work);
1799 fc_dma_unmap_single(lport->dev, lsop->rspdma,
1800 sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1804 nvme_fc_rport_put(rport);
1807 EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req);
1810 /* *********************** NVME Ctrl Routines **************************** */
1813 __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1814 struct nvme_fc_fcp_op *op)
1816 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1817 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1818 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1819 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1821 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1825 nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1826 unsigned int hctx_idx)
1828 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1830 return __nvme_fc_exit_request(set->driver_data, op);
1834 __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1836 unsigned long flags;
1839 spin_lock_irqsave(&ctrl->lock, flags);
1840 opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1841 if (opstate != FCPOP_STATE_ACTIVE)
1842 atomic_set(&op->state, opstate);
1843 else if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) {
1844 op->flags |= FCOP_FLAGS_TERMIO;
1847 spin_unlock_irqrestore(&ctrl->lock, flags);
1849 if (opstate != FCPOP_STATE_ACTIVE)
1852 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1853 &ctrl->rport->remoteport,
1854 op->queue->lldd_handle,
1861 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1863 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
1866 /* ensure we've initialized the ops once */
1867 if (!(aen_op->flags & FCOP_FLAGS_AEN))
1870 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
1871 __nvme_fc_abort_op(ctrl, aen_op);
1875 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1876 struct nvme_fc_fcp_op *op, int opstate)
1878 unsigned long flags;
1880 if (opstate == FCPOP_STATE_ABORTED) {
1881 spin_lock_irqsave(&ctrl->lock, flags);
1882 if (test_bit(FCCTRL_TERMIO, &ctrl->flags) &&
1883 op->flags & FCOP_FLAGS_TERMIO) {
1885 wake_up(&ctrl->ioabort_wait);
1887 spin_unlock_irqrestore(&ctrl->lock, flags);
1892 nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1894 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1895 struct request *rq = op->rq;
1896 struct nvmefc_fcp_req *freq = &op->fcp_req;
1897 struct nvme_fc_ctrl *ctrl = op->ctrl;
1898 struct nvme_fc_queue *queue = op->queue;
1899 struct nvme_completion *cqe = &op->rsp_iu.cqe;
1900 struct nvme_command *sqe = &op->cmd_iu.sqe;
1901 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
1902 union nvme_result result;
1903 bool terminate_assoc = true;
1908 * The current linux implementation of a nvme controller
1909 * allocates a single tag set for all io queues and sizes
1910 * the io queues to fully hold all possible tags. Thus, the
1911 * implementation does not reference or care about the sqhd
1912 * value as it never needs to use the sqhd/sqtail pointers
1913 * for submission pacing.
1915 * This affects the FC-NVME implementation in two ways:
1916 * 1) As the value doesn't matter, we don't need to waste
1917 * cycles extracting it from ERSPs and stamping it in the
1918 * cases where the transport fabricates CQEs on successful
1920 * 2) The FC-NVME implementation requires that delivery of
1921 * ERSP completions are to go back to the nvme layer in order
1922 * relative to the rsn, such that the sqhd value will always
1923 * be "in order" for the nvme layer. As the nvme layer in
1924 * linux doesn't care about sqhd, there's no need to return
1928 * As the core nvme layer in linux currently does not look at
1929 * every field in the cqe - in cases where the FC transport must
1930 * fabricate a CQE, the following fields will not be set as they
1931 * are not referenced:
1932 * cqe.sqid, cqe.sqhd, cqe.command_id
1934 * Failure or error of an individual i/o, in a transport
1935 * detected fashion unrelated to the nvme completion status,
1936 * potentially cause the initiator and target sides to get out
1937 * of sync on SQ head/tail (aka outstanding io count allowed).
1938 * Per FC-NVME spec, failure of an individual command requires
1939 * the connection to be terminated, which in turn requires the
1940 * association to be terminated.
1943 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
1945 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1946 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1948 if (opstate == FCPOP_STATE_ABORTED)
1949 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1950 else if (freq->status) {
1951 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1952 dev_info(ctrl->ctrl.device,
1953 "NVME-FC{%d}: io failed due to lldd error %d\n",
1954 ctrl->cnum, freq->status);
1958 * For the linux implementation, if we have an unsuccesful
1959 * status, they blk-mq layer can typically be called with the
1960 * non-zero status and the content of the cqe isn't important.
1966 * command completed successfully relative to the wire
1967 * protocol. However, validate anything received and
1968 * extract the status and result from the cqe (create it
1972 switch (freq->rcv_rsplen) {
1975 case NVME_FC_SIZEOF_ZEROS_RSP:
1977 * No response payload or 12 bytes of payload (which
1978 * should all be zeros) are considered successful and
1979 * no payload in the CQE by the transport.
1981 if (freq->transferred_length !=
1982 be32_to_cpu(op->cmd_iu.data_len)) {
1983 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1984 dev_info(ctrl->ctrl.device,
1985 "NVME-FC{%d}: io failed due to bad transfer "
1986 "length: %d vs expected %d\n",
1987 ctrl->cnum, freq->transferred_length,
1988 be32_to_cpu(op->cmd_iu.data_len));
1994 case sizeof(struct nvme_fc_ersp_iu):
1996 * The ERSP IU contains a full completion with CQE.
1997 * Validate ERSP IU and look at cqe.
1999 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
2000 (freq->rcv_rsplen / 4) ||
2001 be32_to_cpu(op->rsp_iu.xfrd_len) !=
2002 freq->transferred_length ||
2003 op->rsp_iu.ersp_result ||
2004 sqe->common.command_id != cqe->command_id)) {
2005 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2006 dev_info(ctrl->ctrl.device,
2007 "NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
2008 "iu len %d, xfr len %d vs %d, status code "
2009 "%d, cmdid %d vs %d\n",
2010 ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len),
2011 be32_to_cpu(op->rsp_iu.xfrd_len),
2012 freq->transferred_length,
2013 op->rsp_iu.ersp_result,
2014 sqe->common.command_id,
2018 result = cqe->result;
2019 status = cqe->status;
2023 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2024 dev_info(ctrl->ctrl.device,
2025 "NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
2027 ctrl->cnum, freq->rcv_rsplen);
2031 terminate_assoc = false;
2034 if (op->flags & FCOP_FLAGS_AEN) {
2035 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
2036 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2037 atomic_set(&op->state, FCPOP_STATE_IDLE);
2038 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
2039 nvme_fc_ctrl_put(ctrl);
2043 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2044 if (!nvme_try_complete_req(rq, status, result))
2045 nvme_fc_complete_rq(rq);
2048 if (terminate_assoc)
2049 nvme_fc_error_recovery(ctrl, "transport detected io error");
2053 __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
2054 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
2055 struct request *rq, u32 rqno)
2057 struct nvme_fcp_op_w_sgl *op_w_sgl =
2058 container_of(op, typeof(*op_w_sgl), op);
2059 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2062 memset(op, 0, sizeof(*op));
2063 op->fcp_req.cmdaddr = &op->cmd_iu;
2064 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
2065 op->fcp_req.rspaddr = &op->rsp_iu;
2066 op->fcp_req.rsplen = sizeof(op->rsp_iu);
2067 op->fcp_req.done = nvme_fc_fcpio_done;
2073 cmdiu->format_id = NVME_CMD_FORMAT_ID;
2074 cmdiu->fc_id = NVME_CMD_FC_ID;
2075 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
2077 cmdiu->rsv_cat = fccmnd_set_cat_css(0,
2078 (NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT));
2080 cmdiu->rsv_cat = fccmnd_set_cat_admin(0);
2082 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
2083 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
2084 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
2086 "FCP Op failed - cmdiu dma mapping failed.\n");
2091 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
2092 &op->rsp_iu, sizeof(op->rsp_iu),
2094 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
2096 "FCP Op failed - rspiu dma mapping failed.\n");
2100 atomic_set(&op->state, FCPOP_STATE_IDLE);
2106 nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
2107 unsigned int hctx_idx, unsigned int numa_node)
2109 struct nvme_fc_ctrl *ctrl = set->driver_data;
2110 struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq);
2111 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
2112 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
2115 res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++);
2118 op->op.fcp_req.first_sgl = op->sgl;
2119 op->op.fcp_req.private = &op->priv[0];
2120 nvme_req(rq)->ctrl = &ctrl->ctrl;
2125 nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
2127 struct nvme_fc_fcp_op *aen_op;
2128 struct nvme_fc_cmd_iu *cmdiu;
2129 struct nvme_command *sqe;
2130 void *private = NULL;
2133 aen_op = ctrl->aen_ops;
2134 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2135 if (ctrl->lport->ops->fcprqst_priv_sz) {
2136 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
2142 cmdiu = &aen_op->cmd_iu;
2144 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
2145 aen_op, (struct request *)NULL,
2146 (NVME_AQ_BLK_MQ_DEPTH + i));
2152 aen_op->flags = FCOP_FLAGS_AEN;
2153 aen_op->fcp_req.private = private;
2155 memset(sqe, 0, sizeof(*sqe));
2156 sqe->common.opcode = nvme_admin_async_event;
2157 /* Note: core layer may overwrite the sqe.command_id value */
2158 sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
2164 nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
2166 struct nvme_fc_fcp_op *aen_op;
2169 cancel_work_sync(&ctrl->ctrl.async_event_work);
2170 aen_op = ctrl->aen_ops;
2171 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2172 __nvme_fc_exit_request(ctrl, aen_op);
2174 kfree(aen_op->fcp_req.private);
2175 aen_op->fcp_req.private = NULL;
2180 __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
2183 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
2185 hctx->driver_data = queue;
2190 nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2191 unsigned int hctx_idx)
2193 struct nvme_fc_ctrl *ctrl = data;
2195 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
2201 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2202 unsigned int hctx_idx)
2204 struct nvme_fc_ctrl *ctrl = data;
2206 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
2212 nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
2214 struct nvme_fc_queue *queue;
2216 queue = &ctrl->queues[idx];
2217 memset(queue, 0, sizeof(*queue));
2220 atomic_set(&queue->csn, 0);
2221 queue->dev = ctrl->dev;
2224 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
2226 queue->cmnd_capsule_len = sizeof(struct nvme_command);
2229 * Considered whether we should allocate buffers for all SQEs
2230 * and CQEs and dma map them - mapping their respective entries
2231 * into the request structures (kernel vm addr and dma address)
2232 * thus the driver could use the buffers/mappings directly.
2233 * It only makes sense if the LLDD would use them for its
2234 * messaging api. It's very unlikely most adapter api's would use
2235 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
2236 * structures were used instead.
2241 * This routine terminates a queue at the transport level.
2242 * The transport has already ensured that all outstanding ios on
2243 * the queue have been terminated.
2244 * The transport will send a Disconnect LS request to terminate
2245 * the queue's connection. Termination of the admin queue will also
2246 * terminate the association at the target.
2249 nvme_fc_free_queue(struct nvme_fc_queue *queue)
2251 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
2254 clear_bit(NVME_FC_Q_LIVE, &queue->flags);
2256 * Current implementation never disconnects a single queue.
2257 * It always terminates a whole association. So there is never
2258 * a disconnect(queue) LS sent to the target.
2261 queue->connection_id = 0;
2262 atomic_set(&queue->csn, 0);
2266 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
2267 struct nvme_fc_queue *queue, unsigned int qidx)
2269 if (ctrl->lport->ops->delete_queue)
2270 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
2271 queue->lldd_handle);
2272 queue->lldd_handle = NULL;
2276 nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
2280 for (i = 1; i < ctrl->ctrl.queue_count; i++)
2281 nvme_fc_free_queue(&ctrl->queues[i]);
2285 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
2286 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
2290 queue->lldd_handle = NULL;
2291 if (ctrl->lport->ops->create_queue)
2292 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
2293 qidx, qsize, &queue->lldd_handle);
2299 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
2301 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
2304 for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
2305 __nvme_fc_delete_hw_queue(ctrl, queue, i);
2309 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2311 struct nvme_fc_queue *queue = &ctrl->queues[1];
2314 for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
2315 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
2324 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
2329 nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2333 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
2334 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
2338 ret = nvmf_connect_io_queue(&ctrl->ctrl, i, false);
2342 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
2349 nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
2353 for (i = 1; i < ctrl->ctrl.queue_count; i++)
2354 nvme_fc_init_queue(ctrl, i);
2358 nvme_fc_ctrl_free(struct kref *ref)
2360 struct nvme_fc_ctrl *ctrl =
2361 container_of(ref, struct nvme_fc_ctrl, ref);
2362 unsigned long flags;
2364 if (ctrl->ctrl.tagset) {
2365 blk_cleanup_queue(ctrl->ctrl.connect_q);
2366 blk_mq_free_tag_set(&ctrl->tag_set);
2369 /* remove from rport list */
2370 spin_lock_irqsave(&ctrl->rport->lock, flags);
2371 list_del(&ctrl->ctrl_list);
2372 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
2374 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
2375 blk_cleanup_queue(ctrl->ctrl.admin_q);
2376 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
2377 blk_mq_free_tag_set(&ctrl->admin_tag_set);
2379 kfree(ctrl->queues);
2381 put_device(ctrl->dev);
2382 nvme_fc_rport_put(ctrl->rport);
2384 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2385 if (ctrl->ctrl.opts)
2386 nvmf_free_options(ctrl->ctrl.opts);
2391 nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2393 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2397 nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2399 return kref_get_unless_zero(&ctrl->ref);
2403 * All accesses from nvme core layer done - can now free the
2404 * controller. Called after last nvme_put_ctrl() call
2407 nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
2409 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2411 WARN_ON(nctrl != &ctrl->ctrl);
2413 nvme_fc_ctrl_put(ctrl);
2417 * This routine is used by the transport when it needs to find active
2418 * io on a queue that is to be terminated. The transport uses
2419 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2420 * this routine to kill them on a 1 by 1 basis.
2422 * As FC allocates FC exchange for each io, the transport must contact
2423 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2424 * After terminating the exchange the LLDD will call the transport's
2425 * normal io done path for the request, but it will have an aborted
2426 * status. The done path will return the io request back to the block
2427 * layer with an error status.
2430 nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2432 struct nvme_ctrl *nctrl = data;
2433 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2434 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2436 __nvme_fc_abort_op(ctrl, op);
2441 * This routine runs through all outstanding commands on the association
2442 * and aborts them. This routine is typically be called by the
2443 * delete_association routine. It is also called due to an error during
2444 * reconnect. In that scenario, it is most likely a command that initializes
2445 * the controller, including fabric Connect commands on io queues, that
2446 * may have timed out or failed thus the io must be killed for the connect
2447 * thread to see the error.
2450 __nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl *ctrl, bool start_queues)
2453 * If io queues are present, stop them and terminate all outstanding
2454 * ios on them. As FC allocates FC exchange for each io, the
2455 * transport must contact the LLDD to terminate the exchange,
2456 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2457 * to tell us what io's are busy and invoke a transport routine
2458 * to kill them with the LLDD. After terminating the exchange
2459 * the LLDD will call the transport's normal io done path, but it
2460 * will have an aborted status. The done path will return the
2461 * io requests back to the block layer as part of normal completions
2462 * (but with error status).
2464 if (ctrl->ctrl.queue_count > 1) {
2465 nvme_stop_queues(&ctrl->ctrl);
2466 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2467 nvme_fc_terminate_exchange, &ctrl->ctrl);
2468 blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
2470 nvme_start_queues(&ctrl->ctrl);
2474 * Other transports, which don't have link-level contexts bound
2475 * to sqe's, would try to gracefully shutdown the controller by
2476 * writing the registers for shutdown and polling (call
2477 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
2478 * just aborted and we will wait on those contexts, and given
2479 * there was no indication of how live the controlelr is on the
2480 * link, don't send more io to create more contexts for the
2481 * shutdown. Let the controller fail via keepalive failure if
2482 * its still present.
2486 * clean up the admin queue. Same thing as above.
2488 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
2489 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2490 nvme_fc_terminate_exchange, &ctrl->ctrl);
2491 blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
2495 nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
2498 * if an error (io timeout, etc) while (re)connecting, the remote
2499 * port requested terminating of the association (disconnect_ls)
2500 * or an error (timeout or abort) occurred on an io while creating
2501 * the controller. Abort any ios on the association and let the
2502 * create_association error path resolve things.
2504 if (ctrl->ctrl.state == NVME_CTRL_CONNECTING) {
2505 __nvme_fc_abort_outstanding_ios(ctrl, true);
2506 set_bit(ASSOC_FAILED, &ctrl->flags);
2510 /* Otherwise, only proceed if in LIVE state - e.g. on first error */
2511 if (ctrl->ctrl.state != NVME_CTRL_LIVE)
2514 dev_warn(ctrl->ctrl.device,
2515 "NVME-FC{%d}: transport association event: %s\n",
2516 ctrl->cnum, errmsg);
2517 dev_warn(ctrl->ctrl.device,
2518 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
2520 nvme_reset_ctrl(&ctrl->ctrl);
2523 static enum blk_eh_timer_return
2524 nvme_fc_timeout(struct request *rq, bool reserved)
2526 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2527 struct nvme_fc_ctrl *ctrl = op->ctrl;
2528 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2529 struct nvme_command *sqe = &cmdiu->sqe;
2532 * Attempt to abort the offending command. Command completion
2533 * will detect the aborted io and will fail the connection.
2535 dev_info(ctrl->ctrl.device,
2536 "NVME-FC{%d.%d}: io timeout: opcode %d fctype %d w10/11: "
2538 ctrl->cnum, op->queue->qnum, sqe->common.opcode,
2539 sqe->connect.fctype, sqe->common.cdw10, sqe->common.cdw11);
2540 if (__nvme_fc_abort_op(ctrl, op))
2541 nvme_fc_error_recovery(ctrl, "io timeout abort failed");
2544 * the io abort has been initiated. Have the reset timer
2545 * restarted and the abort completion will complete the io
2546 * shortly. Avoids a synchronous wait while the abort finishes.
2548 return BLK_EH_RESET_TIMER;
2552 nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2553 struct nvme_fc_fcp_op *op)
2555 struct nvmefc_fcp_req *freq = &op->fcp_req;
2560 if (!blk_rq_nr_phys_segments(rq))
2563 freq->sg_table.sgl = freq->first_sgl;
2564 ret = sg_alloc_table_chained(&freq->sg_table,
2565 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
2566 NVME_INLINE_SG_CNT);
2570 op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
2571 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2572 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2573 op->nents, rq_dma_dir(rq));
2574 if (unlikely(freq->sg_cnt <= 0)) {
2575 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2581 * TODO: blk_integrity_rq(rq) for DIF
2587 nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2588 struct nvme_fc_fcp_op *op)
2590 struct nvmefc_fcp_req *freq = &op->fcp_req;
2595 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2598 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2604 * In FC, the queue is a logical thing. At transport connect, the target
2605 * creates its "queue" and returns a handle that is to be given to the
2606 * target whenever it posts something to the corresponding SQ. When an
2607 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2608 * command contained within the SQE, an io, and assigns a FC exchange
2609 * to it. The SQE and the associated SQ handle are sent in the initial
2610 * CMD IU sents on the exchange. All transfers relative to the io occur
2611 * as part of the exchange. The CQE is the last thing for the io,
2612 * which is transferred (explicitly or implicitly) with the RSP IU
2613 * sent on the exchange. After the CQE is received, the FC exchange is
2614 * terminaed and the Exchange may be used on a different io.
2616 * The transport to LLDD api has the transport making a request for a
2617 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2618 * resource and transfers the command. The LLDD will then process all
2619 * steps to complete the io. Upon completion, the transport done routine
2622 * So - while the operation is outstanding to the LLDD, there is a link
2623 * level FC exchange resource that is also outstanding. This must be
2624 * considered in all cleanup operations.
2627 nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2628 struct nvme_fc_fcp_op *op, u32 data_len,
2629 enum nvmefc_fcp_datadir io_dir)
2631 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2632 struct nvme_command *sqe = &cmdiu->sqe;
2636 * before attempting to send the io, check to see if we believe
2637 * the target device is present
2639 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2640 return BLK_STS_RESOURCE;
2642 if (!nvme_fc_ctrl_get(ctrl))
2643 return BLK_STS_IOERR;
2645 /* format the FC-NVME CMD IU and fcp_req */
2646 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2647 cmdiu->data_len = cpu_to_be32(data_len);
2649 case NVMEFC_FCP_WRITE:
2650 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2652 case NVMEFC_FCP_READ:
2653 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2655 case NVMEFC_FCP_NODATA:
2659 op->fcp_req.payload_length = data_len;
2660 op->fcp_req.io_dir = io_dir;
2661 op->fcp_req.transferred_length = 0;
2662 op->fcp_req.rcv_rsplen = 0;
2663 op->fcp_req.status = NVME_SC_SUCCESS;
2664 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2667 * validate per fabric rules, set fields mandated by fabric spec
2668 * as well as those by FC-NVME spec.
2670 WARN_ON_ONCE(sqe->common.metadata);
2671 sqe->common.flags |= NVME_CMD_SGL_METABUF;
2674 * format SQE DPTR field per FC-NVME rules:
2675 * type=0x5 Transport SGL Data Block Descriptor
2676 * subtype=0xA Transport-specific value
2678 * length=length of the data series
2680 sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2681 NVME_SGL_FMT_TRANSPORT_A;
2682 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2683 sqe->rw.dptr.sgl.addr = 0;
2685 if (!(op->flags & FCOP_FLAGS_AEN)) {
2686 ret = nvme_fc_map_data(ctrl, op->rq, op);
2688 nvme_cleanup_cmd(op->rq);
2689 nvme_fc_ctrl_put(ctrl);
2690 if (ret == -ENOMEM || ret == -EAGAIN)
2691 return BLK_STS_RESOURCE;
2692 return BLK_STS_IOERR;
2696 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2697 sizeof(op->cmd_iu), DMA_TO_DEVICE);
2699 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2701 if (!(op->flags & FCOP_FLAGS_AEN))
2702 blk_mq_start_request(op->rq);
2704 cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2705 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2706 &ctrl->rport->remoteport,
2707 queue->lldd_handle, &op->fcp_req);
2711 * If the lld fails to send the command is there an issue with
2712 * the csn value? If the command that fails is the Connect,
2713 * no - as the connection won't be live. If it is a command
2714 * post-connect, it's possible a gap in csn may be created.
2715 * Does this matter? As Linux initiators don't send fused
2716 * commands, no. The gap would exist, but as there's nothing
2717 * that depends on csn order to be delivered on the target
2718 * side, it shouldn't hurt. It would be difficult for a
2719 * target to even detect the csn gap as it has no idea when the
2720 * cmd with the csn was supposed to arrive.
2722 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2723 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2725 if (!(op->flags & FCOP_FLAGS_AEN)) {
2726 nvme_fc_unmap_data(ctrl, op->rq, op);
2727 nvme_cleanup_cmd(op->rq);
2730 nvme_fc_ctrl_put(ctrl);
2732 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2734 return BLK_STS_IOERR;
2736 return BLK_STS_RESOURCE;
2743 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2744 const struct blk_mq_queue_data *bd)
2746 struct nvme_ns *ns = hctx->queue->queuedata;
2747 struct nvme_fc_queue *queue = hctx->driver_data;
2748 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2749 struct request *rq = bd->rq;
2750 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2751 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2752 struct nvme_command *sqe = &cmdiu->sqe;
2753 enum nvmefc_fcp_datadir io_dir;
2754 bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2758 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
2759 !nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2760 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
2762 ret = nvme_setup_cmd(ns, rq, sqe);
2767 * nvme core doesn't quite treat the rq opaquely. Commands such
2768 * as WRITE ZEROES will return a non-zero rq payload_bytes yet
2769 * there is no actual payload to be transferred.
2770 * To get it right, key data transmission on there being 1 or
2771 * more physical segments in the sg list. If there is no
2772 * physical segments, there is no payload.
2774 if (blk_rq_nr_phys_segments(rq)) {
2775 data_len = blk_rq_payload_bytes(rq);
2776 io_dir = ((rq_data_dir(rq) == WRITE) ?
2777 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2780 io_dir = NVMEFC_FCP_NODATA;
2784 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2788 nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2790 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2791 struct nvme_fc_fcp_op *aen_op;
2794 if (test_bit(FCCTRL_TERMIO, &ctrl->flags))
2797 aen_op = &ctrl->aen_ops[0];
2799 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2802 dev_err(ctrl->ctrl.device,
2803 "failed async event work\n");
2807 nvme_fc_complete_rq(struct request *rq)
2809 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2810 struct nvme_fc_ctrl *ctrl = op->ctrl;
2812 atomic_set(&op->state, FCPOP_STATE_IDLE);
2813 op->flags &= ~FCOP_FLAGS_TERMIO;
2815 nvme_fc_unmap_data(ctrl, rq, op);
2816 nvme_complete_rq(rq);
2817 nvme_fc_ctrl_put(ctrl);
2821 static const struct blk_mq_ops nvme_fc_mq_ops = {
2822 .queue_rq = nvme_fc_queue_rq,
2823 .complete = nvme_fc_complete_rq,
2824 .init_request = nvme_fc_init_request,
2825 .exit_request = nvme_fc_exit_request,
2826 .init_hctx = nvme_fc_init_hctx,
2827 .timeout = nvme_fc_timeout,
2831 nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2833 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2834 unsigned int nr_io_queues;
2837 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2838 ctrl->lport->ops->max_hw_queues);
2839 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2841 dev_info(ctrl->ctrl.device,
2842 "set_queue_count failed: %d\n", ret);
2846 ctrl->ctrl.queue_count = nr_io_queues + 1;
2850 nvme_fc_init_io_queues(ctrl);
2852 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2853 ctrl->tag_set.ops = &nvme_fc_mq_ops;
2854 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2855 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2856 ctrl->tag_set.numa_node = ctrl->ctrl.numa_node;
2857 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2858 ctrl->tag_set.cmd_size =
2859 struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv,
2860 ctrl->lport->ops->fcprqst_priv_sz);
2861 ctrl->tag_set.driver_data = ctrl;
2862 ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
2863 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2865 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2869 ctrl->ctrl.tagset = &ctrl->tag_set;
2871 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2872 if (IS_ERR(ctrl->ctrl.connect_q)) {
2873 ret = PTR_ERR(ctrl->ctrl.connect_q);
2874 goto out_free_tag_set;
2877 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2879 goto out_cleanup_blk_queue;
2881 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2883 goto out_delete_hw_queues;
2885 ctrl->ioq_live = true;
2889 out_delete_hw_queues:
2890 nvme_fc_delete_hw_io_queues(ctrl);
2891 out_cleanup_blk_queue:
2892 blk_cleanup_queue(ctrl->ctrl.connect_q);
2894 blk_mq_free_tag_set(&ctrl->tag_set);
2895 nvme_fc_free_io_queues(ctrl);
2897 /* force put free routine to ignore io queues */
2898 ctrl->ctrl.tagset = NULL;
2904 nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
2906 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2907 u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
2908 unsigned int nr_io_queues;
2911 nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2912 ctrl->lport->ops->max_hw_queues);
2913 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2915 dev_info(ctrl->ctrl.device,
2916 "set_queue_count failed: %d\n", ret);
2920 if (!nr_io_queues && prior_ioq_cnt) {
2921 dev_info(ctrl->ctrl.device,
2922 "Fail Reconnect: At least 1 io queue "
2923 "required (was %d)\n", prior_ioq_cnt);
2927 ctrl->ctrl.queue_count = nr_io_queues + 1;
2928 /* check for io queues existing */
2929 if (ctrl->ctrl.queue_count == 1)
2932 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2934 goto out_free_io_queues;
2936 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2938 goto out_delete_hw_queues;
2940 if (prior_ioq_cnt != nr_io_queues) {
2941 dev_info(ctrl->ctrl.device,
2942 "reconnect: revising io queue count from %d to %d\n",
2943 prior_ioq_cnt, nr_io_queues);
2944 nvme_wait_freeze(&ctrl->ctrl);
2945 blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2946 nvme_unfreeze(&ctrl->ctrl);
2951 out_delete_hw_queues:
2952 nvme_fc_delete_hw_io_queues(ctrl);
2954 nvme_fc_free_io_queues(ctrl);
2959 nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
2961 struct nvme_fc_lport *lport = rport->lport;
2963 atomic_inc(&lport->act_rport_cnt);
2967 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
2969 struct nvme_fc_lport *lport = rport->lport;
2972 cnt = atomic_dec_return(&lport->act_rport_cnt);
2973 if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
2974 lport->ops->localport_delete(&lport->localport);
2978 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
2980 struct nvme_fc_rport *rport = ctrl->rport;
2983 if (test_and_set_bit(ASSOC_ACTIVE, &ctrl->flags))
2986 cnt = atomic_inc_return(&rport->act_ctrl_cnt);
2988 nvme_fc_rport_active_on_lport(rport);
2994 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
2996 struct nvme_fc_rport *rport = ctrl->rport;
2997 struct nvme_fc_lport *lport = rport->lport;
3000 /* clearing of ctrl->flags ASSOC_ACTIVE bit is in association delete */
3002 cnt = atomic_dec_return(&rport->act_ctrl_cnt);
3004 if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
3005 lport->ops->remoteport_delete(&rport->remoteport);
3006 nvme_fc_rport_inactive_on_lport(rport);
3013 * This routine restarts the controller on the host side, and
3014 * on the link side, recreates the controller association.
3017 nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
3019 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
3020 struct nvmefc_ls_rcv_op *disls = NULL;
3021 unsigned long flags;
3025 ++ctrl->ctrl.nr_reconnects;
3027 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3030 if (nvme_fc_ctlr_active_on_rport(ctrl))
3033 dev_info(ctrl->ctrl.device,
3034 "NVME-FC{%d}: create association : host wwpn 0x%016llx "
3035 " rport wwpn 0x%016llx: NQN \"%s\"\n",
3036 ctrl->cnum, ctrl->lport->localport.port_name,
3037 ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
3039 clear_bit(ASSOC_FAILED, &ctrl->flags);
3042 * Create the admin queue
3045 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
3048 goto out_free_queue;
3050 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
3051 NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
3053 goto out_delete_hw_queue;
3055 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
3057 goto out_disconnect_admin_queue;
3059 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
3062 * Check controller capabilities
3064 * todo:- add code to check if ctrl attributes changed from
3065 * prior connection values
3068 ret = nvme_enable_ctrl(&ctrl->ctrl);
3069 if (ret || test_bit(ASSOC_FAILED, &ctrl->flags))
3070 goto out_disconnect_admin_queue;
3072 ctrl->ctrl.max_segments = ctrl->lport->ops->max_sgl_segments;
3073 ctrl->ctrl.max_hw_sectors = ctrl->ctrl.max_segments <<
3076 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
3078 ret = nvme_init_identify(&ctrl->ctrl);
3079 if (ret || test_bit(ASSOC_FAILED, &ctrl->flags))
3080 goto out_disconnect_admin_queue;
3084 /* FC-NVME does not have other data in the capsule */
3085 if (ctrl->ctrl.icdoff) {
3086 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
3088 goto out_disconnect_admin_queue;
3091 /* FC-NVME supports normal SGL Data Block Descriptors */
3093 if (opts->queue_size > ctrl->ctrl.maxcmd) {
3094 /* warn if maxcmd is lower than queue_size */
3095 dev_warn(ctrl->ctrl.device,
3096 "queue_size %zu > ctrl maxcmd %u, reducing "
3098 opts->queue_size, ctrl->ctrl.maxcmd);
3099 opts->queue_size = ctrl->ctrl.maxcmd;
3102 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
3103 /* warn if sqsize is lower than queue_size */
3104 dev_warn(ctrl->ctrl.device,
3105 "queue_size %zu > ctrl sqsize %u, reducing "
3107 opts->queue_size, ctrl->ctrl.sqsize + 1);
3108 opts->queue_size = ctrl->ctrl.sqsize + 1;
3111 ret = nvme_fc_init_aen_ops(ctrl);
3113 goto out_term_aen_ops;
3116 * Create the io queues
3119 if (ctrl->ctrl.queue_count > 1) {
3120 if (!ctrl->ioq_live)
3121 ret = nvme_fc_create_io_queues(ctrl);
3123 ret = nvme_fc_recreate_io_queues(ctrl);
3125 if (ret || test_bit(ASSOC_FAILED, &ctrl->flags))
3126 goto out_term_aen_ops;
3128 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
3130 ctrl->ctrl.nr_reconnects = 0;
3133 nvme_start_ctrl(&ctrl->ctrl);
3135 return 0; /* Success */
3138 nvme_fc_term_aen_ops(ctrl);
3139 out_disconnect_admin_queue:
3140 /* send a Disconnect(association) LS to fc-nvme target */
3141 nvme_fc_xmt_disconnect_assoc(ctrl);
3142 spin_lock_irqsave(&ctrl->lock, flags);
3143 ctrl->association_id = 0;
3144 disls = ctrl->rcv_disconn;
3145 ctrl->rcv_disconn = NULL;
3146 spin_unlock_irqrestore(&ctrl->lock, flags);
3148 nvme_fc_xmt_ls_rsp(disls);
3149 out_delete_hw_queue:
3150 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3152 nvme_fc_free_queue(&ctrl->queues[0]);
3153 clear_bit(ASSOC_ACTIVE, &ctrl->flags);
3154 nvme_fc_ctlr_inactive_on_rport(ctrl);
3161 * This routine stops operation of the controller on the host side.
3162 * On the host os stack side: Admin and IO queues are stopped,
3163 * outstanding ios on them terminated via FC ABTS.
3164 * On the link side: the association is terminated.
3167 nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
3169 struct nvmefc_ls_rcv_op *disls = NULL;
3170 unsigned long flags;
3172 if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
3175 spin_lock_irqsave(&ctrl->lock, flags);
3176 set_bit(FCCTRL_TERMIO, &ctrl->flags);
3178 spin_unlock_irqrestore(&ctrl->lock, flags);
3180 __nvme_fc_abort_outstanding_ios(ctrl, false);
3182 /* kill the aens as they are a separate path */
3183 nvme_fc_abort_aen_ops(ctrl);
3185 /* wait for all io that had to be aborted */
3186 spin_lock_irq(&ctrl->lock);
3187 wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
3188 clear_bit(FCCTRL_TERMIO, &ctrl->flags);
3189 spin_unlock_irq(&ctrl->lock);
3191 nvme_fc_term_aen_ops(ctrl);
3194 * send a Disconnect(association) LS to fc-nvme target
3195 * Note: could have been sent at top of process, but
3196 * cleaner on link traffic if after the aborts complete.
3197 * Note: if association doesn't exist, association_id will be 0
3199 if (ctrl->association_id)
3200 nvme_fc_xmt_disconnect_assoc(ctrl);
3202 spin_lock_irqsave(&ctrl->lock, flags);
3203 ctrl->association_id = 0;
3204 disls = ctrl->rcv_disconn;
3205 ctrl->rcv_disconn = NULL;
3206 spin_unlock_irqrestore(&ctrl->lock, flags);
3209 * if a Disconnect Request was waiting for a response, send
3210 * now that all ABTS's have been issued (and are complete).
3212 nvme_fc_xmt_ls_rsp(disls);
3214 if (ctrl->ctrl.tagset) {
3215 nvme_fc_delete_hw_io_queues(ctrl);
3216 nvme_fc_free_io_queues(ctrl);
3219 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3220 nvme_fc_free_queue(&ctrl->queues[0]);
3222 /* re-enable the admin_q so anything new can fast fail */
3223 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
3225 /* resume the io queues so that things will fast fail */
3226 nvme_start_queues(&ctrl->ctrl);
3228 nvme_fc_ctlr_inactive_on_rport(ctrl);
3232 nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
3234 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
3236 cancel_delayed_work_sync(&ctrl->connect_work);
3238 * kill the association on the link side. this will block
3239 * waiting for io to terminate
3241 nvme_fc_delete_association(ctrl);
3245 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
3247 struct nvme_fc_rport *rport = ctrl->rport;
3248 struct nvme_fc_remote_port *portptr = &rport->remoteport;
3249 unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
3252 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING)
3255 if (portptr->port_state == FC_OBJSTATE_ONLINE)
3256 dev_info(ctrl->ctrl.device,
3257 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
3258 ctrl->cnum, status);
3259 else if (time_after_eq(jiffies, rport->dev_loss_end))
3262 if (recon && nvmf_should_reconnect(&ctrl->ctrl)) {
3263 if (portptr->port_state == FC_OBJSTATE_ONLINE)
3264 dev_info(ctrl->ctrl.device,
3265 "NVME-FC{%d}: Reconnect attempt in %ld "
3267 ctrl->cnum, recon_delay / HZ);
3268 else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
3269 recon_delay = rport->dev_loss_end - jiffies;
3271 queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
3273 if (portptr->port_state == FC_OBJSTATE_ONLINE)
3274 dev_warn(ctrl->ctrl.device,
3275 "NVME-FC{%d}: Max reconnect attempts (%d) "
3277 ctrl->cnum, ctrl->ctrl.nr_reconnects);
3279 dev_warn(ctrl->ctrl.device,
3280 "NVME-FC{%d}: dev_loss_tmo (%d) expired "
3281 "while waiting for remoteport connectivity.\n",
3282 ctrl->cnum, min_t(int, portptr->dev_loss_tmo,
3283 (ctrl->ctrl.opts->max_reconnects *
3284 ctrl->ctrl.opts->reconnect_delay)));
3285 WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
3290 nvme_fc_reset_ctrl_work(struct work_struct *work)
3292 struct nvme_fc_ctrl *ctrl =
3293 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
3295 nvme_stop_ctrl(&ctrl->ctrl);
3297 /* will block will waiting for io to terminate */
3298 nvme_fc_delete_association(ctrl);
3300 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
3301 dev_err(ctrl->ctrl.device,
3302 "NVME-FC{%d}: error_recovery: Couldn't change state "
3303 "to CONNECTING\n", ctrl->cnum);
3305 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
3306 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3307 dev_err(ctrl->ctrl.device,
3308 "NVME-FC{%d}: failed to schedule connect "
3309 "after reset\n", ctrl->cnum);
3311 flush_delayed_work(&ctrl->connect_work);
3314 nvme_fc_reconnect_or_delete(ctrl, -ENOTCONN);
3319 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
3321 .module = THIS_MODULE,
3322 .flags = NVME_F_FABRICS,
3323 .reg_read32 = nvmf_reg_read32,
3324 .reg_read64 = nvmf_reg_read64,
3325 .reg_write32 = nvmf_reg_write32,
3326 .free_ctrl = nvme_fc_nvme_ctrl_freed,
3327 .submit_async_event = nvme_fc_submit_async_event,
3328 .delete_ctrl = nvme_fc_delete_ctrl,
3329 .get_address = nvmf_get_address,
3333 nvme_fc_connect_ctrl_work(struct work_struct *work)
3337 struct nvme_fc_ctrl *ctrl =
3338 container_of(to_delayed_work(work),
3339 struct nvme_fc_ctrl, connect_work);
3341 ret = nvme_fc_create_association(ctrl);
3343 nvme_fc_reconnect_or_delete(ctrl, ret);
3345 dev_info(ctrl->ctrl.device,
3346 "NVME-FC{%d}: controller connect complete\n",
3351 static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
3352 .queue_rq = nvme_fc_queue_rq,
3353 .complete = nvme_fc_complete_rq,
3354 .init_request = nvme_fc_init_request,
3355 .exit_request = nvme_fc_exit_request,
3356 .init_hctx = nvme_fc_init_admin_hctx,
3357 .timeout = nvme_fc_timeout,
3362 * Fails a controller request if it matches an existing controller
3363 * (association) with the same tuple:
3364 * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
3366 * The ports don't need to be compared as they are intrinsically
3367 * already matched by the port pointers supplied.
3370 nvme_fc_existing_controller(struct nvme_fc_rport *rport,
3371 struct nvmf_ctrl_options *opts)
3373 struct nvme_fc_ctrl *ctrl;
3374 unsigned long flags;
3377 spin_lock_irqsave(&rport->lock, flags);
3378 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3379 found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
3383 spin_unlock_irqrestore(&rport->lock, flags);
3388 static struct nvme_ctrl *
3389 nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3390 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3392 struct nvme_fc_ctrl *ctrl;
3393 unsigned long flags;
3394 int ret, idx, ctrl_loss_tmo;
3396 if (!(rport->remoteport.port_role &
3397 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
3402 if (!opts->duplicate_connect &&
3403 nvme_fc_existing_controller(rport, opts)) {
3408 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3414 idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
3421 * if ctrl_loss_tmo is being enforced and the default reconnect delay
3422 * is being used, change to a shorter reconnect delay for FC.
3424 if (opts->max_reconnects != -1 &&
3425 opts->reconnect_delay == NVMF_DEF_RECONNECT_DELAY &&
3426 opts->reconnect_delay > NVME_FC_DEFAULT_RECONNECT_TMO) {
3427 ctrl_loss_tmo = opts->max_reconnects * opts->reconnect_delay;
3428 opts->reconnect_delay = NVME_FC_DEFAULT_RECONNECT_TMO;
3429 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
3430 opts->reconnect_delay);
3433 ctrl->ctrl.opts = opts;
3434 ctrl->ctrl.nr_reconnects = 0;
3436 ctrl->ctrl.numa_node = dev_to_node(lport->dev);
3438 ctrl->ctrl.numa_node = NUMA_NO_NODE;
3439 INIT_LIST_HEAD(&ctrl->ctrl_list);
3440 ctrl->lport = lport;
3441 ctrl->rport = rport;
3442 ctrl->dev = lport->dev;
3444 ctrl->ioq_live = false;
3445 init_waitqueue_head(&ctrl->ioabort_wait);
3447 get_device(ctrl->dev);
3448 kref_init(&ctrl->ref);
3450 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
3451 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
3452 spin_lock_init(&ctrl->lock);
3454 /* io queue count */
3455 ctrl->ctrl.queue_count = min_t(unsigned int,
3457 lport->ops->max_hw_queues);
3458 ctrl->ctrl.queue_count++; /* +1 for admin queue */
3460 ctrl->ctrl.sqsize = opts->queue_size - 1;
3461 ctrl->ctrl.kato = opts->kato;
3462 ctrl->ctrl.cntlid = 0xffff;
3465 ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3466 sizeof(struct nvme_fc_queue), GFP_KERNEL);
3470 nvme_fc_init_queue(ctrl, 0);
3472 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
3473 ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
3474 ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
3475 ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
3476 ctrl->admin_tag_set.numa_node = ctrl->ctrl.numa_node;
3477 ctrl->admin_tag_set.cmd_size =
3478 struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv,
3479 ctrl->lport->ops->fcprqst_priv_sz);
3480 ctrl->admin_tag_set.driver_data = ctrl;
3481 ctrl->admin_tag_set.nr_hw_queues = 1;
3482 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
3483 ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
3485 ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
3487 goto out_free_queues;
3488 ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
3490 ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
3491 if (IS_ERR(ctrl->ctrl.fabrics_q)) {
3492 ret = PTR_ERR(ctrl->ctrl.fabrics_q);
3493 goto out_free_admin_tag_set;
3496 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
3497 if (IS_ERR(ctrl->ctrl.admin_q)) {
3498 ret = PTR_ERR(ctrl->ctrl.admin_q);
3499 goto out_cleanup_fabrics_q;
3503 * Would have been nice to init io queues tag set as well.
3504 * However, we require interaction from the controller
3505 * for max io queue count before we can do so.
3506 * Defer this to the connect path.
3509 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3511 goto out_cleanup_admin_q;
3513 /* at this point, teardown path changes to ref counting on nvme ctrl */
3515 spin_lock_irqsave(&rport->lock, flags);
3516 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3517 spin_unlock_irqrestore(&rport->lock, flags);
3519 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING) ||
3520 !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
3521 dev_err(ctrl->ctrl.device,
3522 "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
3526 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3527 dev_err(ctrl->ctrl.device,
3528 "NVME-FC{%d}: failed to schedule initial connect\n",
3533 flush_delayed_work(&ctrl->connect_work);
3535 dev_info(ctrl->ctrl.device,
3536 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
3537 ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
3542 nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3543 cancel_work_sync(&ctrl->ctrl.reset_work);
3544 cancel_delayed_work_sync(&ctrl->connect_work);
3546 ctrl->ctrl.opts = NULL;
3548 /* initiate nvme ctrl ref counting teardown */
3549 nvme_uninit_ctrl(&ctrl->ctrl);
3551 /* Remove core ctrl ref. */
3552 nvme_put_ctrl(&ctrl->ctrl);
3554 /* as we're past the point where we transition to the ref
3555 * counting teardown path, if we return a bad pointer here,
3556 * the calling routine, thinking it's prior to the
3557 * transition, will do an rport put. Since the teardown
3558 * path also does a rport put, we do an extra get here to
3559 * so proper order/teardown happens.
3561 nvme_fc_rport_get(rport);
3563 return ERR_PTR(-EIO);
3565 out_cleanup_admin_q:
3566 blk_cleanup_queue(ctrl->ctrl.admin_q);
3567 out_cleanup_fabrics_q:
3568 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
3569 out_free_admin_tag_set:
3570 blk_mq_free_tag_set(&ctrl->admin_tag_set);
3572 kfree(ctrl->queues);
3574 put_device(ctrl->dev);
3575 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
3579 /* exit via here doesn't follow ctlr ref points */
3580 return ERR_PTR(ret);
3584 struct nvmet_fc_traddr {
3590 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3594 if (match_u64(sstr, &token64))
3602 * This routine validates and extracts the WWN's from the TRADDR string.
3603 * As kernel parsers need the 0x to determine number base, universally
3604 * build string to parse with 0x prefix before parsing name strings.
3607 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
3609 char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
3610 substring_t wwn = { name, &name[sizeof(name)-1] };
3611 int nnoffset, pnoffset;
3613 /* validate if string is one of the 2 allowed formats */
3614 if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
3615 !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
3616 !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
3617 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
3618 nnoffset = NVME_FC_TRADDR_OXNNLEN;
3619 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
3620 NVME_FC_TRADDR_OXNNLEN;
3621 } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
3622 !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
3623 !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
3624 "pn-", NVME_FC_TRADDR_NNLEN))) {
3625 nnoffset = NVME_FC_TRADDR_NNLEN;
3626 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
3632 name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
3634 memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3635 if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
3638 memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3639 if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
3645 pr_warn("%s: bad traddr string\n", __func__);
3649 static struct nvme_ctrl *
3650 nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3652 struct nvme_fc_lport *lport;
3653 struct nvme_fc_rport *rport;
3654 struct nvme_ctrl *ctrl;
3655 struct nvmet_fc_traddr laddr = { 0L, 0L };
3656 struct nvmet_fc_traddr raddr = { 0L, 0L };
3657 unsigned long flags;
3660 ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3661 if (ret || !raddr.nn || !raddr.pn)
3662 return ERR_PTR(-EINVAL);
3664 ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3665 if (ret || !laddr.nn || !laddr.pn)
3666 return ERR_PTR(-EINVAL);
3668 /* find the host and remote ports to connect together */
3669 spin_lock_irqsave(&nvme_fc_lock, flags);
3670 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3671 if (lport->localport.node_name != laddr.nn ||
3672 lport->localport.port_name != laddr.pn ||
3673 lport->localport.port_state != FC_OBJSTATE_ONLINE)
3676 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3677 if (rport->remoteport.node_name != raddr.nn ||
3678 rport->remoteport.port_name != raddr.pn ||
3679 rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3682 /* if fail to get reference fall through. Will error */
3683 if (!nvme_fc_rport_get(rport))
3686 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3688 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3690 nvme_fc_rport_put(rport);
3694 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3696 pr_warn("%s: %s - %s combination not found\n",
3697 __func__, opts->traddr, opts->host_traddr);
3698 return ERR_PTR(-ENOENT);
3702 static struct nvmf_transport_ops nvme_fc_transport = {
3704 .module = THIS_MODULE,
3705 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
3706 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3707 .create_ctrl = nvme_fc_create_ctrl,
3710 /* Arbitrary successive failures max. With lots of subsystems could be high */
3711 #define DISCOVERY_MAX_FAIL 20
3713 static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
3714 struct device_attribute *attr, const char *buf, size_t count)
3716 unsigned long flags;
3717 LIST_HEAD(local_disc_list);
3718 struct nvme_fc_lport *lport;
3719 struct nvme_fc_rport *rport;
3722 spin_lock_irqsave(&nvme_fc_lock, flags);
3724 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3725 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3726 if (!nvme_fc_lport_get(lport))
3728 if (!nvme_fc_rport_get(rport)) {
3730 * This is a temporary condition. Upon restart
3731 * this rport will be gone from the list.
3733 * Revert the lport put and retry. Anything
3734 * added to the list already will be skipped (as
3735 * they are no longer list_empty). Loops should
3736 * resume at rports that were not yet seen.
3738 nvme_fc_lport_put(lport);
3740 if (failcnt++ < DISCOVERY_MAX_FAIL)
3743 pr_err("nvme_discovery: too many reference "
3745 goto process_local_list;
3747 if (list_empty(&rport->disc_list))
3748 list_add_tail(&rport->disc_list,
3754 while (!list_empty(&local_disc_list)) {
3755 rport = list_first_entry(&local_disc_list,
3756 struct nvme_fc_rport, disc_list);
3757 list_del_init(&rport->disc_list);
3758 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3760 lport = rport->lport;
3761 /* signal discovery. Won't hurt if it repeats */
3762 nvme_fc_signal_discovery_scan(lport, rport);
3763 nvme_fc_rport_put(rport);
3764 nvme_fc_lport_put(lport);
3766 spin_lock_irqsave(&nvme_fc_lock, flags);
3768 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3772 static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
3774 static struct attribute *nvme_fc_attrs[] = {
3775 &dev_attr_nvme_discovery.attr,
3779 static struct attribute_group nvme_fc_attr_group = {
3780 .attrs = nvme_fc_attrs,
3783 static const struct attribute_group *nvme_fc_attr_groups[] = {
3784 &nvme_fc_attr_group,
3788 static struct class fc_class = {
3790 .dev_groups = nvme_fc_attr_groups,
3791 .owner = THIS_MODULE,
3794 static int __init nvme_fc_init_module(void)
3798 nvme_fc_wq = alloc_workqueue("nvme_fc_wq", WQ_MEM_RECLAIM, 0);
3804 * It is expected that in the future the kernel will combine
3805 * the FC-isms that are currently under scsi and now being
3806 * added to by NVME into a new standalone FC class. The SCSI
3807 * and NVME protocols and their devices would be under this
3810 * As we need something to post FC-specific udev events to,
3811 * specifically for nvme probe events, start by creating the
3812 * new device class. When the new standalone FC class is
3813 * put in place, this code will move to a more generic
3814 * location for the class.
3816 ret = class_register(&fc_class);
3818 pr_err("couldn't register class fc\n");
3819 goto out_destroy_wq;
3823 * Create a device for the FC-centric udev events
3825 fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
3827 if (IS_ERR(fc_udev_device)) {
3828 pr_err("couldn't create fc_udev device!\n");
3829 ret = PTR_ERR(fc_udev_device);
3830 goto out_destroy_class;
3833 ret = nvmf_register_transport(&nvme_fc_transport);
3835 goto out_destroy_device;
3840 device_destroy(&fc_class, MKDEV(0, 0));
3842 class_unregister(&fc_class);
3844 destroy_workqueue(nvme_fc_wq);
3850 nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
3852 struct nvme_fc_ctrl *ctrl;
3854 spin_lock(&rport->lock);
3855 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3856 dev_warn(ctrl->ctrl.device,
3857 "NVME-FC{%d}: transport unloading: deleting ctrl\n",
3859 nvme_delete_ctrl(&ctrl->ctrl);
3861 spin_unlock(&rport->lock);
3865 nvme_fc_cleanup_for_unload(void)
3867 struct nvme_fc_lport *lport;
3868 struct nvme_fc_rport *rport;
3870 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3871 list_for_each_entry(rport, &lport->endp_list, endp_list) {
3872 nvme_fc_delete_controllers(rport);
3877 static void __exit nvme_fc_exit_module(void)
3879 unsigned long flags;
3880 bool need_cleanup = false;
3882 spin_lock_irqsave(&nvme_fc_lock, flags);
3883 nvme_fc_waiting_to_unload = true;
3884 if (!list_empty(&nvme_fc_lport_list)) {
3885 need_cleanup = true;
3886 nvme_fc_cleanup_for_unload();
3888 spin_unlock_irqrestore(&nvme_fc_lock, flags);
3890 pr_info("%s: waiting for ctlr deletes\n", __func__);
3891 wait_for_completion(&nvme_fc_unload_proceed);
3892 pr_info("%s: ctrl deletes complete\n", __func__);
3895 nvmf_unregister_transport(&nvme_fc_transport);
3897 ida_destroy(&nvme_fc_local_port_cnt);
3898 ida_destroy(&nvme_fc_ctrl_cnt);
3900 device_destroy(&fc_class, MKDEV(0, 0));
3901 class_unregister(&fc_class);
3902 destroy_workqueue(nvme_fc_wq);
3905 module_init(nvme_fc_init_module);
3906 module_exit(nvme_fc_exit_module);
3908 MODULE_LICENSE("GPL v2");