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>
17 #include <scsi/scsi_transport_fc.h>
19 /* *************************** Data Structures/Defines ****************** */
22 enum nvme_fc_queue_flags
{
23 NVME_FC_Q_CONNECTED
= 0,
27 #define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */
29 struct nvme_fc_queue
{
30 struct nvme_fc_ctrl
*ctrl
;
32 struct blk_mq_hw_ctx
*hctx
;
34 size_t cmnd_capsule_len
;
43 } __aligned(sizeof(u64
)); /* alignment for other things alloc'd with */
45 enum nvme_fcop_flags
{
46 FCOP_FLAGS_TERMIO
= (1 << 0),
47 FCOP_FLAGS_AEN
= (1 << 1),
50 struct nvmefc_ls_req_op
{
51 struct nvmefc_ls_req ls_req
;
53 struct nvme_fc_rport
*rport
;
54 struct nvme_fc_queue
*queue
;
59 struct completion ls_done
;
60 struct list_head lsreq_list
; /* rport->ls_req_list */
64 enum nvme_fcpop_state
{
65 FCPOP_STATE_UNINIT
= 0,
67 FCPOP_STATE_ACTIVE
= 2,
68 FCPOP_STATE_ABORTED
= 3,
69 FCPOP_STATE_COMPLETE
= 4,
72 struct nvme_fc_fcp_op
{
73 struct nvme_request nreq
; /*
76 * the 1st element in the
81 struct nvmefc_fcp_req fcp_req
;
83 struct nvme_fc_ctrl
*ctrl
;
84 struct nvme_fc_queue
*queue
;
92 struct nvme_fc_cmd_iu cmd_iu
;
93 struct nvme_fc_ersp_iu rsp_iu
;
96 struct nvme_fcp_op_w_sgl
{
97 struct nvme_fc_fcp_op op
;
98 struct scatterlist sgl
[SG_CHUNK_SIZE
];
102 struct nvme_fc_lport
{
103 struct nvme_fc_local_port localport
;
106 struct list_head port_list
; /* nvme_fc_port_list */
107 struct list_head endp_list
;
108 struct device
*dev
; /* physical device for dma */
109 struct nvme_fc_port_template
*ops
;
111 atomic_t act_rport_cnt
;
112 } __aligned(sizeof(u64
)); /* alignment for other things alloc'd with */
114 struct nvme_fc_rport
{
115 struct nvme_fc_remote_port remoteport
;
117 struct list_head endp_list
; /* for lport->endp_list */
118 struct list_head ctrl_list
;
119 struct list_head ls_req_list
;
120 struct list_head disc_list
;
121 struct device
*dev
; /* physical device for dma */
122 struct nvme_fc_lport
*lport
;
125 atomic_t act_ctrl_cnt
;
126 unsigned long dev_loss_end
;
127 } __aligned(sizeof(u64
)); /* alignment for other things alloc'd with */
129 enum nvme_fcctrl_flags
{
130 FCCTRL_TERMIO
= (1 << 0),
133 struct nvme_fc_ctrl
{
135 struct nvme_fc_queue
*queues
;
137 struct nvme_fc_lport
*lport
;
138 struct nvme_fc_rport
*rport
;
143 atomic_t err_work_active
;
146 struct list_head ctrl_list
; /* rport->ctrl_list */
148 struct blk_mq_tag_set admin_tag_set
;
149 struct blk_mq_tag_set tag_set
;
151 struct delayed_work connect_work
;
152 struct work_struct err_work
;
157 wait_queue_head_t ioabort_wait
;
159 struct nvme_fc_fcp_op aen_ops
[NVME_NR_AEN_COMMANDS
];
161 struct nvme_ctrl ctrl
;
164 static inline struct nvme_fc_ctrl
*
165 to_fc_ctrl(struct nvme_ctrl
*ctrl
)
167 return container_of(ctrl
, struct nvme_fc_ctrl
, ctrl
);
170 static inline struct nvme_fc_lport
*
171 localport_to_lport(struct nvme_fc_local_port
*portptr
)
173 return container_of(portptr
, struct nvme_fc_lport
, localport
);
176 static inline struct nvme_fc_rport
*
177 remoteport_to_rport(struct nvme_fc_remote_port
*portptr
)
179 return container_of(portptr
, struct nvme_fc_rport
, remoteport
);
182 static inline struct nvmefc_ls_req_op
*
183 ls_req_to_lsop(struct nvmefc_ls_req
*lsreq
)
185 return container_of(lsreq
, struct nvmefc_ls_req_op
, ls_req
);
188 static inline struct nvme_fc_fcp_op
*
189 fcp_req_to_fcp_op(struct nvmefc_fcp_req
*fcpreq
)
191 return container_of(fcpreq
, struct nvme_fc_fcp_op
, fcp_req
);
196 /* *************************** Globals **************************** */
199 static DEFINE_SPINLOCK(nvme_fc_lock
);
201 static LIST_HEAD(nvme_fc_lport_list
);
202 static DEFINE_IDA(nvme_fc_local_port_cnt
);
203 static DEFINE_IDA(nvme_fc_ctrl_cnt
);
205 static struct workqueue_struct
*nvme_fc_wq
;
207 static bool nvme_fc_waiting_to_unload
;
208 static DECLARE_COMPLETION(nvme_fc_unload_proceed
);
211 * These items are short-term. They will eventually be moved into
212 * a generic FC class. See comments in module init.
214 static struct device
*fc_udev_device
;
217 /* *********************** FC-NVME Port Management ************************ */
219 static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl
*,
220 struct nvme_fc_queue
*, unsigned int);
223 nvme_fc_free_lport(struct kref
*ref
)
225 struct nvme_fc_lport
*lport
=
226 container_of(ref
, struct nvme_fc_lport
, ref
);
229 WARN_ON(lport
->localport
.port_state
!= FC_OBJSTATE_DELETED
);
230 WARN_ON(!list_empty(&lport
->endp_list
));
232 /* remove from transport list */
233 spin_lock_irqsave(&nvme_fc_lock
, flags
);
234 list_del(&lport
->port_list
);
235 if (nvme_fc_waiting_to_unload
&& list_empty(&nvme_fc_lport_list
))
236 complete(&nvme_fc_unload_proceed
);
237 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
239 ida_simple_remove(&nvme_fc_local_port_cnt
, lport
->localport
.port_num
);
240 ida_destroy(&lport
->endp_cnt
);
242 put_device(lport
->dev
);
248 nvme_fc_lport_put(struct nvme_fc_lport
*lport
)
250 kref_put(&lport
->ref
, nvme_fc_free_lport
);
254 nvme_fc_lport_get(struct nvme_fc_lport
*lport
)
256 return kref_get_unless_zero(&lport
->ref
);
260 static struct nvme_fc_lport
*
261 nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info
*pinfo
,
262 struct nvme_fc_port_template
*ops
,
265 struct nvme_fc_lport
*lport
;
268 spin_lock_irqsave(&nvme_fc_lock
, flags
);
270 list_for_each_entry(lport
, &nvme_fc_lport_list
, port_list
) {
271 if (lport
->localport
.node_name
!= pinfo
->node_name
||
272 lport
->localport
.port_name
!= pinfo
->port_name
)
275 if (lport
->dev
!= dev
) {
276 lport
= ERR_PTR(-EXDEV
);
280 if (lport
->localport
.port_state
!= FC_OBJSTATE_DELETED
) {
281 lport
= ERR_PTR(-EEXIST
);
285 if (!nvme_fc_lport_get(lport
)) {
287 * fails if ref cnt already 0. If so,
288 * act as if lport already deleted
294 /* resume the lport */
297 lport
->localport
.port_role
= pinfo
->port_role
;
298 lport
->localport
.port_id
= pinfo
->port_id
;
299 lport
->localport
.port_state
= FC_OBJSTATE_ONLINE
;
301 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
309 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
315 * nvme_fc_register_localport - transport entry point called by an
316 * LLDD to register the existence of a NVME
318 * @pinfo: pointer to information about the port to be registered
319 * @template: LLDD entrypoints and operational parameters for the port
320 * @dev: physical hardware device node port corresponds to. Will be
321 * used for DMA mappings
322 * @portptr: pointer to a local port pointer. Upon success, the routine
323 * will allocate a nvme_fc_local_port structure and place its
324 * address in the local port pointer. Upon failure, local port
325 * pointer will be set to 0.
328 * a completion status. Must be 0 upon success; a negative errno
329 * (ex: -ENXIO) upon failure.
332 nvme_fc_register_localport(struct nvme_fc_port_info
*pinfo
,
333 struct nvme_fc_port_template
*template,
335 struct nvme_fc_local_port
**portptr
)
337 struct nvme_fc_lport
*newrec
;
341 if (!template->localport_delete
|| !template->remoteport_delete
||
342 !template->ls_req
|| !template->fcp_io
||
343 !template->ls_abort
|| !template->fcp_abort
||
344 !template->max_hw_queues
|| !template->max_sgl_segments
||
345 !template->max_dif_sgl_segments
|| !template->dma_boundary
) {
347 goto out_reghost_failed
;
351 * look to see if there is already a localport that had been
352 * deregistered and in the process of waiting for all the
353 * references to fully be removed. If the references haven't
354 * expired, we can simply re-enable the localport. Remoteports
355 * and controller reconnections should resume naturally.
357 newrec
= nvme_fc_attach_to_unreg_lport(pinfo
, template, dev
);
359 /* found an lport, but something about its state is bad */
360 if (IS_ERR(newrec
)) {
361 ret
= PTR_ERR(newrec
);
362 goto out_reghost_failed
;
364 /* found existing lport, which was resumed */
366 *portptr
= &newrec
->localport
;
370 /* nothing found - allocate a new localport struct */
372 newrec
= kmalloc((sizeof(*newrec
) + template->local_priv_sz
),
376 goto out_reghost_failed
;
379 idx
= ida_simple_get(&nvme_fc_local_port_cnt
, 0, 0, GFP_KERNEL
);
385 if (!get_device(dev
) && dev
) {
390 INIT_LIST_HEAD(&newrec
->port_list
);
391 INIT_LIST_HEAD(&newrec
->endp_list
);
392 kref_init(&newrec
->ref
);
393 atomic_set(&newrec
->act_rport_cnt
, 0);
394 newrec
->ops
= template;
396 ida_init(&newrec
->endp_cnt
);
397 newrec
->localport
.private = &newrec
[1];
398 newrec
->localport
.node_name
= pinfo
->node_name
;
399 newrec
->localport
.port_name
= pinfo
->port_name
;
400 newrec
->localport
.port_role
= pinfo
->port_role
;
401 newrec
->localport
.port_id
= pinfo
->port_id
;
402 newrec
->localport
.port_state
= FC_OBJSTATE_ONLINE
;
403 newrec
->localport
.port_num
= idx
;
405 spin_lock_irqsave(&nvme_fc_lock
, flags
);
406 list_add_tail(&newrec
->port_list
, &nvme_fc_lport_list
);
407 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
410 dma_set_seg_boundary(dev
, template->dma_boundary
);
412 *portptr
= &newrec
->localport
;
416 ida_simple_remove(&nvme_fc_local_port_cnt
, idx
);
424 EXPORT_SYMBOL_GPL(nvme_fc_register_localport
);
427 * nvme_fc_unregister_localport - transport entry point called by an
428 * LLDD to deregister/remove a previously
429 * registered a NVME host FC port.
430 * @portptr: pointer to the (registered) local port that is to be deregistered.
433 * a completion status. Must be 0 upon success; a negative errno
434 * (ex: -ENXIO) upon failure.
437 nvme_fc_unregister_localport(struct nvme_fc_local_port
*portptr
)
439 struct nvme_fc_lport
*lport
= localport_to_lport(portptr
);
445 spin_lock_irqsave(&nvme_fc_lock
, flags
);
447 if (portptr
->port_state
!= FC_OBJSTATE_ONLINE
) {
448 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
451 portptr
->port_state
= FC_OBJSTATE_DELETED
;
453 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
455 if (atomic_read(&lport
->act_rport_cnt
) == 0)
456 lport
->ops
->localport_delete(&lport
->localport
);
458 nvme_fc_lport_put(lport
);
462 EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport
);
465 * TRADDR strings, per FC-NVME are fixed format:
466 * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
467 * udev event will only differ by prefix of what field is
469 * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
470 * 19 + 43 + null_fudge = 64 characters
472 #define FCNVME_TRADDR_LENGTH 64
475 nvme_fc_signal_discovery_scan(struct nvme_fc_lport
*lport
,
476 struct nvme_fc_rport
*rport
)
478 char hostaddr
[FCNVME_TRADDR_LENGTH
]; /* NVMEFC_HOST_TRADDR=...*/
479 char tgtaddr
[FCNVME_TRADDR_LENGTH
]; /* NVMEFC_TRADDR=...*/
480 char *envp
[4] = { "FC_EVENT=nvmediscovery", hostaddr
, tgtaddr
, NULL
};
482 if (!(rport
->remoteport
.port_role
& FC_PORT_ROLE_NVME_DISCOVERY
))
485 snprintf(hostaddr
, sizeof(hostaddr
),
486 "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
487 lport
->localport
.node_name
, lport
->localport
.port_name
);
488 snprintf(tgtaddr
, sizeof(tgtaddr
),
489 "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
490 rport
->remoteport
.node_name
, rport
->remoteport
.port_name
);
491 kobject_uevent_env(&fc_udev_device
->kobj
, KOBJ_CHANGE
, envp
);
495 nvme_fc_free_rport(struct kref
*ref
)
497 struct nvme_fc_rport
*rport
=
498 container_of(ref
, struct nvme_fc_rport
, ref
);
499 struct nvme_fc_lport
*lport
=
500 localport_to_lport(rport
->remoteport
.localport
);
503 WARN_ON(rport
->remoteport
.port_state
!= FC_OBJSTATE_DELETED
);
504 WARN_ON(!list_empty(&rport
->ctrl_list
));
506 /* remove from lport list */
507 spin_lock_irqsave(&nvme_fc_lock
, flags
);
508 list_del(&rport
->endp_list
);
509 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
511 WARN_ON(!list_empty(&rport
->disc_list
));
512 ida_simple_remove(&lport
->endp_cnt
, rport
->remoteport
.port_num
);
516 nvme_fc_lport_put(lport
);
520 nvme_fc_rport_put(struct nvme_fc_rport
*rport
)
522 kref_put(&rport
->ref
, nvme_fc_free_rport
);
526 nvme_fc_rport_get(struct nvme_fc_rport
*rport
)
528 return kref_get_unless_zero(&rport
->ref
);
532 nvme_fc_resume_controller(struct nvme_fc_ctrl
*ctrl
)
534 switch (ctrl
->ctrl
.state
) {
536 case NVME_CTRL_CONNECTING
:
538 * As all reconnects were suppressed, schedule a
541 dev_info(ctrl
->ctrl
.device
,
542 "NVME-FC{%d}: connectivity re-established. "
543 "Attempting reconnect\n", ctrl
->cnum
);
545 queue_delayed_work(nvme_wq
, &ctrl
->connect_work
, 0);
548 case NVME_CTRL_RESETTING
:
550 * Controller is already in the process of terminating the
551 * association. No need to do anything further. The reconnect
552 * step will naturally occur after the reset completes.
557 /* no action to take - let it delete */
562 static struct nvme_fc_rport
*
563 nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport
*lport
,
564 struct nvme_fc_port_info
*pinfo
)
566 struct nvme_fc_rport
*rport
;
567 struct nvme_fc_ctrl
*ctrl
;
570 spin_lock_irqsave(&nvme_fc_lock
, flags
);
572 list_for_each_entry(rport
, &lport
->endp_list
, endp_list
) {
573 if (rport
->remoteport
.node_name
!= pinfo
->node_name
||
574 rport
->remoteport
.port_name
!= pinfo
->port_name
)
577 if (!nvme_fc_rport_get(rport
)) {
578 rport
= ERR_PTR(-ENOLCK
);
582 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
584 spin_lock_irqsave(&rport
->lock
, flags
);
586 /* has it been unregistered */
587 if (rport
->remoteport
.port_state
!= FC_OBJSTATE_DELETED
) {
588 /* means lldd called us twice */
589 spin_unlock_irqrestore(&rport
->lock
, flags
);
590 nvme_fc_rport_put(rport
);
591 return ERR_PTR(-ESTALE
);
594 rport
->remoteport
.port_role
= pinfo
->port_role
;
595 rport
->remoteport
.port_id
= pinfo
->port_id
;
596 rport
->remoteport
.port_state
= FC_OBJSTATE_ONLINE
;
597 rport
->dev_loss_end
= 0;
600 * kick off a reconnect attempt on all associations to the
601 * remote port. A successful reconnects will resume i/o.
603 list_for_each_entry(ctrl
, &rport
->ctrl_list
, ctrl_list
)
604 nvme_fc_resume_controller(ctrl
);
606 spin_unlock_irqrestore(&rport
->lock
, flags
);
614 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
620 __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport
*rport
,
621 struct nvme_fc_port_info
*pinfo
)
623 if (pinfo
->dev_loss_tmo
)
624 rport
->remoteport
.dev_loss_tmo
= pinfo
->dev_loss_tmo
;
626 rport
->remoteport
.dev_loss_tmo
= NVME_FC_DEFAULT_DEV_LOSS_TMO
;
630 * nvme_fc_register_remoteport - transport entry point called by an
631 * LLDD to register the existence of a NVME
632 * subsystem FC port on its fabric.
633 * @localport: pointer to the (registered) local port that the remote
634 * subsystem port is connected to.
635 * @pinfo: pointer to information about the port to be registered
636 * @portptr: pointer to a remote port pointer. Upon success, the routine
637 * will allocate a nvme_fc_remote_port structure and place its
638 * address in the remote port pointer. Upon failure, remote port
639 * pointer will be set to 0.
642 * a completion status. Must be 0 upon success; a negative errno
643 * (ex: -ENXIO) upon failure.
646 nvme_fc_register_remoteport(struct nvme_fc_local_port
*localport
,
647 struct nvme_fc_port_info
*pinfo
,
648 struct nvme_fc_remote_port
**portptr
)
650 struct nvme_fc_lport
*lport
= localport_to_lport(localport
);
651 struct nvme_fc_rport
*newrec
;
655 if (!nvme_fc_lport_get(lport
)) {
657 goto out_reghost_failed
;
661 * look to see if there is already a remoteport that is waiting
662 * for a reconnect (within dev_loss_tmo) with the same WWN's.
663 * If so, transition to it and reconnect.
665 newrec
= nvme_fc_attach_to_suspended_rport(lport
, pinfo
);
667 /* found an rport, but something about its state is bad */
668 if (IS_ERR(newrec
)) {
669 ret
= PTR_ERR(newrec
);
672 /* found existing rport, which was resumed */
674 nvme_fc_lport_put(lport
);
675 __nvme_fc_set_dev_loss_tmo(newrec
, pinfo
);
676 nvme_fc_signal_discovery_scan(lport
, newrec
);
677 *portptr
= &newrec
->remoteport
;
681 /* nothing found - allocate a new remoteport struct */
683 newrec
= kmalloc((sizeof(*newrec
) + lport
->ops
->remote_priv_sz
),
690 idx
= ida_simple_get(&lport
->endp_cnt
, 0, 0, GFP_KERNEL
);
693 goto out_kfree_rport
;
696 INIT_LIST_HEAD(&newrec
->endp_list
);
697 INIT_LIST_HEAD(&newrec
->ctrl_list
);
698 INIT_LIST_HEAD(&newrec
->ls_req_list
);
699 INIT_LIST_HEAD(&newrec
->disc_list
);
700 kref_init(&newrec
->ref
);
701 atomic_set(&newrec
->act_ctrl_cnt
, 0);
702 spin_lock_init(&newrec
->lock
);
703 newrec
->remoteport
.localport
= &lport
->localport
;
704 newrec
->dev
= lport
->dev
;
705 newrec
->lport
= lport
;
706 newrec
->remoteport
.private = &newrec
[1];
707 newrec
->remoteport
.port_role
= pinfo
->port_role
;
708 newrec
->remoteport
.node_name
= pinfo
->node_name
;
709 newrec
->remoteport
.port_name
= pinfo
->port_name
;
710 newrec
->remoteport
.port_id
= pinfo
->port_id
;
711 newrec
->remoteport
.port_state
= FC_OBJSTATE_ONLINE
;
712 newrec
->remoteport
.port_num
= idx
;
713 __nvme_fc_set_dev_loss_tmo(newrec
, pinfo
);
715 spin_lock_irqsave(&nvme_fc_lock
, flags
);
716 list_add_tail(&newrec
->endp_list
, &lport
->endp_list
);
717 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
719 nvme_fc_signal_discovery_scan(lport
, newrec
);
721 *portptr
= &newrec
->remoteport
;
727 nvme_fc_lport_put(lport
);
732 EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport
);
735 nvme_fc_abort_lsops(struct nvme_fc_rport
*rport
)
737 struct nvmefc_ls_req_op
*lsop
;
741 spin_lock_irqsave(&rport
->lock
, flags
);
743 list_for_each_entry(lsop
, &rport
->ls_req_list
, lsreq_list
) {
744 if (!(lsop
->flags
& FCOP_FLAGS_TERMIO
)) {
745 lsop
->flags
|= FCOP_FLAGS_TERMIO
;
746 spin_unlock_irqrestore(&rport
->lock
, flags
);
747 rport
->lport
->ops
->ls_abort(&rport
->lport
->localport
,
753 spin_unlock_irqrestore(&rport
->lock
, flags
);
759 nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl
*ctrl
)
761 dev_info(ctrl
->ctrl
.device
,
762 "NVME-FC{%d}: controller connectivity lost. Awaiting "
763 "Reconnect", ctrl
->cnum
);
765 switch (ctrl
->ctrl
.state
) {
769 * Schedule a controller reset. The reset will terminate the
770 * association and schedule the reconnect timer. Reconnects
771 * will be attempted until either the ctlr_loss_tmo
772 * (max_retries * connect_delay) expires or the remoteport's
773 * dev_loss_tmo expires.
775 if (nvme_reset_ctrl(&ctrl
->ctrl
)) {
776 dev_warn(ctrl
->ctrl
.device
,
777 "NVME-FC{%d}: Couldn't schedule reset.\n",
779 nvme_delete_ctrl(&ctrl
->ctrl
);
783 case NVME_CTRL_CONNECTING
:
785 * The association has already been terminated and the
786 * controller is attempting reconnects. No need to do anything
787 * futher. Reconnects will be attempted until either the
788 * ctlr_loss_tmo (max_retries * connect_delay) expires or the
789 * remoteport's dev_loss_tmo expires.
793 case NVME_CTRL_RESETTING
:
795 * Controller is already in the process of terminating the
796 * association. No need to do anything further. The reconnect
797 * step will kick in naturally after the association is
802 case NVME_CTRL_DELETING
:
804 /* no action to take - let it delete */
810 * nvme_fc_unregister_remoteport - transport entry point called by an
811 * LLDD to deregister/remove a previously
812 * registered a NVME subsystem FC port.
813 * @portptr: pointer to the (registered) remote port that is to be
817 * a completion status. Must be 0 upon success; a negative errno
818 * (ex: -ENXIO) upon failure.
821 nvme_fc_unregister_remoteport(struct nvme_fc_remote_port
*portptr
)
823 struct nvme_fc_rport
*rport
= remoteport_to_rport(portptr
);
824 struct nvme_fc_ctrl
*ctrl
;
830 spin_lock_irqsave(&rport
->lock
, flags
);
832 if (portptr
->port_state
!= FC_OBJSTATE_ONLINE
) {
833 spin_unlock_irqrestore(&rport
->lock
, flags
);
836 portptr
->port_state
= FC_OBJSTATE_DELETED
;
838 rport
->dev_loss_end
= jiffies
+ (portptr
->dev_loss_tmo
* HZ
);
840 list_for_each_entry(ctrl
, &rport
->ctrl_list
, ctrl_list
) {
841 /* if dev_loss_tmo==0, dev loss is immediate */
842 if (!portptr
->dev_loss_tmo
) {
843 dev_warn(ctrl
->ctrl
.device
,
844 "NVME-FC{%d}: controller connectivity lost.\n",
846 nvme_delete_ctrl(&ctrl
->ctrl
);
848 nvme_fc_ctrl_connectivity_loss(ctrl
);
851 spin_unlock_irqrestore(&rport
->lock
, flags
);
853 nvme_fc_abort_lsops(rport
);
855 if (atomic_read(&rport
->act_ctrl_cnt
) == 0)
856 rport
->lport
->ops
->remoteport_delete(portptr
);
859 * release the reference, which will allow, if all controllers
860 * go away, which should only occur after dev_loss_tmo occurs,
861 * for the rport to be torn down.
863 nvme_fc_rport_put(rport
);
867 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport
);
870 * nvme_fc_rescan_remoteport - transport entry point called by an
871 * LLDD to request a nvme device rescan.
872 * @remoteport: pointer to the (registered) remote port that is to be
878 nvme_fc_rescan_remoteport(struct nvme_fc_remote_port
*remoteport
)
880 struct nvme_fc_rport
*rport
= remoteport_to_rport(remoteport
);
882 nvme_fc_signal_discovery_scan(rport
->lport
, rport
);
884 EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport
);
887 nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port
*portptr
,
890 struct nvme_fc_rport
*rport
= remoteport_to_rport(portptr
);
893 spin_lock_irqsave(&rport
->lock
, flags
);
895 if (portptr
->port_state
!= FC_OBJSTATE_ONLINE
) {
896 spin_unlock_irqrestore(&rport
->lock
, flags
);
900 /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
901 rport
->remoteport
.dev_loss_tmo
= dev_loss_tmo
;
903 spin_unlock_irqrestore(&rport
->lock
, flags
);
907 EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss
);
910 /* *********************** FC-NVME DMA Handling **************************** */
913 * The fcloop device passes in a NULL device pointer. Real LLD's will
914 * pass in a valid device pointer. If NULL is passed to the dma mapping
915 * routines, depending on the platform, it may or may not succeed, and
919 * Wrapper all the dma routines and check the dev pointer.
921 * If simple mappings (return just a dma address, we'll noop them,
922 * returning a dma address of 0.
924 * On more complex mappings (dma_map_sg), a pseudo routine fills
925 * in the scatter list, setting all dma addresses to 0.
928 static inline dma_addr_t
929 fc_dma_map_single(struct device
*dev
, void *ptr
, size_t size
,
930 enum dma_data_direction dir
)
932 return dev
? dma_map_single(dev
, ptr
, size
, dir
) : (dma_addr_t
)0L;
936 fc_dma_mapping_error(struct device
*dev
, dma_addr_t dma_addr
)
938 return dev
? dma_mapping_error(dev
, dma_addr
) : 0;
942 fc_dma_unmap_single(struct device
*dev
, dma_addr_t addr
, size_t size
,
943 enum dma_data_direction dir
)
946 dma_unmap_single(dev
, addr
, size
, dir
);
950 fc_dma_sync_single_for_cpu(struct device
*dev
, dma_addr_t addr
, size_t size
,
951 enum dma_data_direction dir
)
954 dma_sync_single_for_cpu(dev
, addr
, size
, dir
);
958 fc_dma_sync_single_for_device(struct device
*dev
, dma_addr_t addr
, size_t size
,
959 enum dma_data_direction dir
)
962 dma_sync_single_for_device(dev
, addr
, size
, dir
);
965 /* pseudo dma_map_sg call */
967 fc_map_sg(struct scatterlist
*sg
, int nents
)
969 struct scatterlist
*s
;
972 WARN_ON(nents
== 0 || sg
[0].length
== 0);
974 for_each_sg(sg
, s
, nents
, i
) {
976 #ifdef CONFIG_NEED_SG_DMA_LENGTH
977 s
->dma_length
= s
->length
;
984 fc_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
, int nents
,
985 enum dma_data_direction dir
)
987 return dev
? dma_map_sg(dev
, sg
, nents
, dir
) : fc_map_sg(sg
, nents
);
991 fc_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sg
, int nents
,
992 enum dma_data_direction dir
)
995 dma_unmap_sg(dev
, sg
, nents
, dir
);
998 /* *********************** FC-NVME LS Handling **************************** */
1000 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl
*);
1001 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl
*);
1005 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op
*lsop
)
1007 struct nvme_fc_rport
*rport
= lsop
->rport
;
1008 struct nvmefc_ls_req
*lsreq
= &lsop
->ls_req
;
1009 unsigned long flags
;
1011 spin_lock_irqsave(&rport
->lock
, flags
);
1013 if (!lsop
->req_queued
) {
1014 spin_unlock_irqrestore(&rport
->lock
, flags
);
1018 list_del(&lsop
->lsreq_list
);
1020 lsop
->req_queued
= false;
1022 spin_unlock_irqrestore(&rport
->lock
, flags
);
1024 fc_dma_unmap_single(rport
->dev
, lsreq
->rqstdma
,
1025 (lsreq
->rqstlen
+ lsreq
->rsplen
),
1028 nvme_fc_rport_put(rport
);
1032 __nvme_fc_send_ls_req(struct nvme_fc_rport
*rport
,
1033 struct nvmefc_ls_req_op
*lsop
,
1034 void (*done
)(struct nvmefc_ls_req
*req
, int status
))
1036 struct nvmefc_ls_req
*lsreq
= &lsop
->ls_req
;
1037 unsigned long flags
;
1040 if (rport
->remoteport
.port_state
!= FC_OBJSTATE_ONLINE
)
1041 return -ECONNREFUSED
;
1043 if (!nvme_fc_rport_get(rport
))
1047 lsop
->rport
= rport
;
1048 lsop
->req_queued
= false;
1049 INIT_LIST_HEAD(&lsop
->lsreq_list
);
1050 init_completion(&lsop
->ls_done
);
1052 lsreq
->rqstdma
= fc_dma_map_single(rport
->dev
, lsreq
->rqstaddr
,
1053 lsreq
->rqstlen
+ lsreq
->rsplen
,
1055 if (fc_dma_mapping_error(rport
->dev
, lsreq
->rqstdma
)) {
1059 lsreq
->rspdma
= lsreq
->rqstdma
+ lsreq
->rqstlen
;
1061 spin_lock_irqsave(&rport
->lock
, flags
);
1063 list_add_tail(&lsop
->lsreq_list
, &rport
->ls_req_list
);
1065 lsop
->req_queued
= true;
1067 spin_unlock_irqrestore(&rport
->lock
, flags
);
1069 ret
= rport
->lport
->ops
->ls_req(&rport
->lport
->localport
,
1070 &rport
->remoteport
, lsreq
);
1077 lsop
->ls_error
= ret
;
1078 spin_lock_irqsave(&rport
->lock
, flags
);
1079 lsop
->req_queued
= false;
1080 list_del(&lsop
->lsreq_list
);
1081 spin_unlock_irqrestore(&rport
->lock
, flags
);
1082 fc_dma_unmap_single(rport
->dev
, lsreq
->rqstdma
,
1083 (lsreq
->rqstlen
+ lsreq
->rsplen
),
1086 nvme_fc_rport_put(rport
);
1092 nvme_fc_send_ls_req_done(struct nvmefc_ls_req
*lsreq
, int status
)
1094 struct nvmefc_ls_req_op
*lsop
= ls_req_to_lsop(lsreq
);
1096 lsop
->ls_error
= status
;
1097 complete(&lsop
->ls_done
);
1101 nvme_fc_send_ls_req(struct nvme_fc_rport
*rport
, struct nvmefc_ls_req_op
*lsop
)
1103 struct nvmefc_ls_req
*lsreq
= &lsop
->ls_req
;
1104 struct fcnvme_ls_rjt
*rjt
= lsreq
->rspaddr
;
1107 ret
= __nvme_fc_send_ls_req(rport
, lsop
, nvme_fc_send_ls_req_done
);
1111 * No timeout/not interruptible as we need the struct
1112 * to exist until the lldd calls us back. Thus mandate
1113 * wait until driver calls back. lldd responsible for
1114 * the timeout action
1116 wait_for_completion(&lsop
->ls_done
);
1118 __nvme_fc_finish_ls_req(lsop
);
1120 ret
= lsop
->ls_error
;
1126 /* ACC or RJT payload ? */
1127 if (rjt
->w0
.ls_cmd
== FCNVME_LS_RJT
)
1134 nvme_fc_send_ls_req_async(struct nvme_fc_rport
*rport
,
1135 struct nvmefc_ls_req_op
*lsop
,
1136 void (*done
)(struct nvmefc_ls_req
*req
, int status
))
1138 /* don't wait for completion */
1140 return __nvme_fc_send_ls_req(rport
, lsop
, done
);
1143 /* Validation Error indexes into the string table below */
1147 VERR_LSDESC_RQST
= 2,
1148 VERR_LSDESC_RQST_LEN
= 3,
1150 VERR_ASSOC_ID_LEN
= 5,
1152 VERR_CONN_ID_LEN
= 7,
1154 VERR_CR_ASSOC_ACC_LEN
= 9,
1156 VERR_CR_CONN_ACC_LEN
= 11,
1158 VERR_DISCONN_ACC_LEN
= 13,
1161 static char *validation_errors
[] = {
1165 "Bad LSDESC_RQST Length",
1166 "Not Association ID",
1167 "Bad Association ID Length",
1168 "Not Connection ID",
1169 "Bad Connection ID Length",
1170 "Not CR_ASSOC Rqst",
1171 "Bad CR_ASSOC ACC Length",
1173 "Bad CR_CONN ACC Length",
1174 "Not Disconnect Rqst",
1175 "Bad Disconnect ACC Length",
1179 nvme_fc_connect_admin_queue(struct nvme_fc_ctrl
*ctrl
,
1180 struct nvme_fc_queue
*queue
, u16 qsize
, u16 ersp_ratio
)
1182 struct nvmefc_ls_req_op
*lsop
;
1183 struct nvmefc_ls_req
*lsreq
;
1184 struct fcnvme_ls_cr_assoc_rqst
*assoc_rqst
;
1185 struct fcnvme_ls_cr_assoc_acc
*assoc_acc
;
1188 lsop
= kzalloc((sizeof(*lsop
) +
1189 ctrl
->lport
->ops
->lsrqst_priv_sz
+
1190 sizeof(*assoc_rqst
) + sizeof(*assoc_acc
)), GFP_KERNEL
);
1195 lsreq
= &lsop
->ls_req
;
1197 lsreq
->private = (void *)&lsop
[1];
1198 assoc_rqst
= (struct fcnvme_ls_cr_assoc_rqst
*)
1199 (lsreq
->private + ctrl
->lport
->ops
->lsrqst_priv_sz
);
1200 assoc_acc
= (struct fcnvme_ls_cr_assoc_acc
*)&assoc_rqst
[1];
1202 assoc_rqst
->w0
.ls_cmd
= FCNVME_LS_CREATE_ASSOCIATION
;
1203 assoc_rqst
->desc_list_len
=
1204 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd
));
1206 assoc_rqst
->assoc_cmd
.desc_tag
=
1207 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD
);
1208 assoc_rqst
->assoc_cmd
.desc_len
=
1210 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd
));
1212 assoc_rqst
->assoc_cmd
.ersp_ratio
= cpu_to_be16(ersp_ratio
);
1213 assoc_rqst
->assoc_cmd
.sqsize
= cpu_to_be16(qsize
- 1);
1214 /* Linux supports only Dynamic controllers */
1215 assoc_rqst
->assoc_cmd
.cntlid
= cpu_to_be16(0xffff);
1216 uuid_copy(&assoc_rqst
->assoc_cmd
.hostid
, &ctrl
->ctrl
.opts
->host
->id
);
1217 strncpy(assoc_rqst
->assoc_cmd
.hostnqn
, ctrl
->ctrl
.opts
->host
->nqn
,
1218 min(FCNVME_ASSOC_HOSTNQN_LEN
, NVMF_NQN_SIZE
));
1219 strncpy(assoc_rqst
->assoc_cmd
.subnqn
, ctrl
->ctrl
.opts
->subsysnqn
,
1220 min(FCNVME_ASSOC_SUBNQN_LEN
, NVMF_NQN_SIZE
));
1222 lsop
->queue
= queue
;
1223 lsreq
->rqstaddr
= assoc_rqst
;
1224 lsreq
->rqstlen
= sizeof(*assoc_rqst
);
1225 lsreq
->rspaddr
= assoc_acc
;
1226 lsreq
->rsplen
= sizeof(*assoc_acc
);
1227 lsreq
->timeout
= NVME_FC_LS_TIMEOUT_SEC
;
1229 ret
= nvme_fc_send_ls_req(ctrl
->rport
, lsop
);
1231 goto out_free_buffer
;
1233 /* process connect LS completion */
1235 /* validate the ACC response */
1236 if (assoc_acc
->hdr
.w0
.ls_cmd
!= FCNVME_LS_ACC
)
1238 else if (assoc_acc
->hdr
.desc_list_len
!=
1240 sizeof(struct fcnvme_ls_cr_assoc_acc
)))
1241 fcret
= VERR_CR_ASSOC_ACC_LEN
;
1242 else if (assoc_acc
->hdr
.rqst
.desc_tag
!=
1243 cpu_to_be32(FCNVME_LSDESC_RQST
))
1244 fcret
= VERR_LSDESC_RQST
;
1245 else if (assoc_acc
->hdr
.rqst
.desc_len
!=
1246 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst
)))
1247 fcret
= VERR_LSDESC_RQST_LEN
;
1248 else if (assoc_acc
->hdr
.rqst
.w0
.ls_cmd
!= FCNVME_LS_CREATE_ASSOCIATION
)
1249 fcret
= VERR_CR_ASSOC
;
1250 else if (assoc_acc
->associd
.desc_tag
!=
1251 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID
))
1252 fcret
= VERR_ASSOC_ID
;
1253 else if (assoc_acc
->associd
.desc_len
!=
1255 sizeof(struct fcnvme_lsdesc_assoc_id
)))
1256 fcret
= VERR_ASSOC_ID_LEN
;
1257 else if (assoc_acc
->connectid
.desc_tag
!=
1258 cpu_to_be32(FCNVME_LSDESC_CONN_ID
))
1259 fcret
= VERR_CONN_ID
;
1260 else if (assoc_acc
->connectid
.desc_len
!=
1261 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id
)))
1262 fcret
= VERR_CONN_ID_LEN
;
1267 "q %d Create Association LS failed: %s\n",
1268 queue
->qnum
, validation_errors
[fcret
]);
1270 ctrl
->association_id
=
1271 be64_to_cpu(assoc_acc
->associd
.association_id
);
1272 queue
->connection_id
=
1273 be64_to_cpu(assoc_acc
->connectid
.connection_id
);
1274 set_bit(NVME_FC_Q_CONNECTED
, &queue
->flags
);
1282 "queue %d connect admin queue failed (%d).\n",
1288 nvme_fc_connect_queue(struct nvme_fc_ctrl
*ctrl
, struct nvme_fc_queue
*queue
,
1289 u16 qsize
, u16 ersp_ratio
)
1291 struct nvmefc_ls_req_op
*lsop
;
1292 struct nvmefc_ls_req
*lsreq
;
1293 struct fcnvme_ls_cr_conn_rqst
*conn_rqst
;
1294 struct fcnvme_ls_cr_conn_acc
*conn_acc
;
1297 lsop
= kzalloc((sizeof(*lsop
) +
1298 ctrl
->lport
->ops
->lsrqst_priv_sz
+
1299 sizeof(*conn_rqst
) + sizeof(*conn_acc
)), GFP_KERNEL
);
1304 lsreq
= &lsop
->ls_req
;
1306 lsreq
->private = (void *)&lsop
[1];
1307 conn_rqst
= (struct fcnvme_ls_cr_conn_rqst
*)
1308 (lsreq
->private + ctrl
->lport
->ops
->lsrqst_priv_sz
);
1309 conn_acc
= (struct fcnvme_ls_cr_conn_acc
*)&conn_rqst
[1];
1311 conn_rqst
->w0
.ls_cmd
= FCNVME_LS_CREATE_CONNECTION
;
1312 conn_rqst
->desc_list_len
= cpu_to_be32(
1313 sizeof(struct fcnvme_lsdesc_assoc_id
) +
1314 sizeof(struct fcnvme_lsdesc_cr_conn_cmd
));
1316 conn_rqst
->associd
.desc_tag
= cpu_to_be32(FCNVME_LSDESC_ASSOC_ID
);
1317 conn_rqst
->associd
.desc_len
=
1319 sizeof(struct fcnvme_lsdesc_assoc_id
));
1320 conn_rqst
->associd
.association_id
= cpu_to_be64(ctrl
->association_id
);
1321 conn_rqst
->connect_cmd
.desc_tag
=
1322 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD
);
1323 conn_rqst
->connect_cmd
.desc_len
=
1325 sizeof(struct fcnvme_lsdesc_cr_conn_cmd
));
1326 conn_rqst
->connect_cmd
.ersp_ratio
= cpu_to_be16(ersp_ratio
);
1327 conn_rqst
->connect_cmd
.qid
= cpu_to_be16(queue
->qnum
);
1328 conn_rqst
->connect_cmd
.sqsize
= cpu_to_be16(qsize
- 1);
1330 lsop
->queue
= queue
;
1331 lsreq
->rqstaddr
= conn_rqst
;
1332 lsreq
->rqstlen
= sizeof(*conn_rqst
);
1333 lsreq
->rspaddr
= conn_acc
;
1334 lsreq
->rsplen
= sizeof(*conn_acc
);
1335 lsreq
->timeout
= NVME_FC_LS_TIMEOUT_SEC
;
1337 ret
= nvme_fc_send_ls_req(ctrl
->rport
, lsop
);
1339 goto out_free_buffer
;
1341 /* process connect LS completion */
1343 /* validate the ACC response */
1344 if (conn_acc
->hdr
.w0
.ls_cmd
!= FCNVME_LS_ACC
)
1346 else if (conn_acc
->hdr
.desc_list_len
!=
1347 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc
)))
1348 fcret
= VERR_CR_CONN_ACC_LEN
;
1349 else if (conn_acc
->hdr
.rqst
.desc_tag
!= cpu_to_be32(FCNVME_LSDESC_RQST
))
1350 fcret
= VERR_LSDESC_RQST
;
1351 else if (conn_acc
->hdr
.rqst
.desc_len
!=
1352 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst
)))
1353 fcret
= VERR_LSDESC_RQST_LEN
;
1354 else if (conn_acc
->hdr
.rqst
.w0
.ls_cmd
!= FCNVME_LS_CREATE_CONNECTION
)
1355 fcret
= VERR_CR_CONN
;
1356 else if (conn_acc
->connectid
.desc_tag
!=
1357 cpu_to_be32(FCNVME_LSDESC_CONN_ID
))
1358 fcret
= VERR_CONN_ID
;
1359 else if (conn_acc
->connectid
.desc_len
!=
1360 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id
)))
1361 fcret
= VERR_CONN_ID_LEN
;
1366 "q %d Create I/O Connection LS failed: %s\n",
1367 queue
->qnum
, validation_errors
[fcret
]);
1369 queue
->connection_id
=
1370 be64_to_cpu(conn_acc
->connectid
.connection_id
);
1371 set_bit(NVME_FC_Q_CONNECTED
, &queue
->flags
);
1379 "queue %d connect I/O queue failed (%d).\n",
1385 nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req
*lsreq
, int status
)
1387 struct nvmefc_ls_req_op
*lsop
= ls_req_to_lsop(lsreq
);
1389 __nvme_fc_finish_ls_req(lsop
);
1391 /* fc-nvme initiator doesn't care about success or failure of cmd */
1397 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1398 * the FC-NVME Association. Terminating the association also
1399 * terminates the FC-NVME connections (per queue, both admin and io
1400 * queues) that are part of the association. E.g. things are torn
1401 * down, and the related FC-NVME Association ID and Connection IDs
1404 * The behavior of the fc-nvme initiator is such that it's
1405 * understanding of the association and connections will implicitly
1406 * be torn down. The action is implicit as it may be due to a loss of
1407 * connectivity with the fc-nvme target, so you may never get a
1408 * response even if you tried. As such, the action of this routine
1409 * is to asynchronously send the LS, ignore any results of the LS, and
1410 * continue on with terminating the association. If the fc-nvme target
1411 * is present and receives the LS, it too can tear down.
1414 nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl
*ctrl
)
1416 struct fcnvme_ls_disconnect_assoc_rqst
*discon_rqst
;
1417 struct fcnvme_ls_disconnect_assoc_acc
*discon_acc
;
1418 struct nvmefc_ls_req_op
*lsop
;
1419 struct nvmefc_ls_req
*lsreq
;
1422 lsop
= kzalloc((sizeof(*lsop
) +
1423 ctrl
->lport
->ops
->lsrqst_priv_sz
+
1424 sizeof(*discon_rqst
) + sizeof(*discon_acc
)),
1427 /* couldn't sent it... too bad */
1430 lsreq
= &lsop
->ls_req
;
1432 lsreq
->private = (void *)&lsop
[1];
1433 discon_rqst
= (struct fcnvme_ls_disconnect_assoc_rqst
*)
1434 (lsreq
->private + ctrl
->lport
->ops
->lsrqst_priv_sz
);
1435 discon_acc
= (struct fcnvme_ls_disconnect_assoc_acc
*)&discon_rqst
[1];
1437 discon_rqst
->w0
.ls_cmd
= FCNVME_LS_DISCONNECT_ASSOC
;
1438 discon_rqst
->desc_list_len
= cpu_to_be32(
1439 sizeof(struct fcnvme_lsdesc_assoc_id
) +
1440 sizeof(struct fcnvme_lsdesc_disconn_cmd
));
1442 discon_rqst
->associd
.desc_tag
= cpu_to_be32(FCNVME_LSDESC_ASSOC_ID
);
1443 discon_rqst
->associd
.desc_len
=
1445 sizeof(struct fcnvme_lsdesc_assoc_id
));
1447 discon_rqst
->associd
.association_id
= cpu_to_be64(ctrl
->association_id
);
1449 discon_rqst
->discon_cmd
.desc_tag
= cpu_to_be32(
1450 FCNVME_LSDESC_DISCONN_CMD
);
1451 discon_rqst
->discon_cmd
.desc_len
=
1453 sizeof(struct fcnvme_lsdesc_disconn_cmd
));
1455 lsreq
->rqstaddr
= discon_rqst
;
1456 lsreq
->rqstlen
= sizeof(*discon_rqst
);
1457 lsreq
->rspaddr
= discon_acc
;
1458 lsreq
->rsplen
= sizeof(*discon_acc
);
1459 lsreq
->timeout
= NVME_FC_LS_TIMEOUT_SEC
;
1461 ret
= nvme_fc_send_ls_req_async(ctrl
->rport
, lsop
,
1462 nvme_fc_disconnect_assoc_done
);
1468 /* *********************** NVME Ctrl Routines **************************** */
1470 static void nvme_fc_error_recovery(struct nvme_fc_ctrl
*ctrl
, char *errmsg
);
1473 __nvme_fc_exit_request(struct nvme_fc_ctrl
*ctrl
,
1474 struct nvme_fc_fcp_op
*op
)
1476 fc_dma_unmap_single(ctrl
->lport
->dev
, op
->fcp_req
.rspdma
,
1477 sizeof(op
->rsp_iu
), DMA_FROM_DEVICE
);
1478 fc_dma_unmap_single(ctrl
->lport
->dev
, op
->fcp_req
.cmddma
,
1479 sizeof(op
->cmd_iu
), DMA_TO_DEVICE
);
1481 atomic_set(&op
->state
, FCPOP_STATE_UNINIT
);
1485 nvme_fc_exit_request(struct blk_mq_tag_set
*set
, struct request
*rq
,
1486 unsigned int hctx_idx
)
1488 struct nvme_fc_fcp_op
*op
= blk_mq_rq_to_pdu(rq
);
1490 return __nvme_fc_exit_request(set
->driver_data
, op
);
1494 __nvme_fc_abort_op(struct nvme_fc_ctrl
*ctrl
, struct nvme_fc_fcp_op
*op
)
1496 unsigned long flags
;
1499 spin_lock_irqsave(&ctrl
->lock
, flags
);
1500 opstate
= atomic_xchg(&op
->state
, FCPOP_STATE_ABORTED
);
1501 if (opstate
!= FCPOP_STATE_ACTIVE
)
1502 atomic_set(&op
->state
, opstate
);
1503 else if (ctrl
->flags
& FCCTRL_TERMIO
)
1505 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
1507 if (opstate
!= FCPOP_STATE_ACTIVE
)
1510 ctrl
->lport
->ops
->fcp_abort(&ctrl
->lport
->localport
,
1511 &ctrl
->rport
->remoteport
,
1512 op
->queue
->lldd_handle
,
1519 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl
*ctrl
)
1521 struct nvme_fc_fcp_op
*aen_op
= ctrl
->aen_ops
;
1524 /* ensure we've initialized the ops once */
1525 if (!(aen_op
->flags
& FCOP_FLAGS_AEN
))
1528 for (i
= 0; i
< NVME_NR_AEN_COMMANDS
; i
++, aen_op
++)
1529 __nvme_fc_abort_op(ctrl
, aen_op
);
1533 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl
*ctrl
,
1534 struct nvme_fc_fcp_op
*op
, int opstate
)
1536 unsigned long flags
;
1538 if (opstate
== FCPOP_STATE_ABORTED
) {
1539 spin_lock_irqsave(&ctrl
->lock
, flags
);
1540 if (ctrl
->flags
& FCCTRL_TERMIO
) {
1542 wake_up(&ctrl
->ioabort_wait
);
1544 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
1549 nvme_fc_fcpio_done(struct nvmefc_fcp_req
*req
)
1551 struct nvme_fc_fcp_op
*op
= fcp_req_to_fcp_op(req
);
1552 struct request
*rq
= op
->rq
;
1553 struct nvmefc_fcp_req
*freq
= &op
->fcp_req
;
1554 struct nvme_fc_ctrl
*ctrl
= op
->ctrl
;
1555 struct nvme_fc_queue
*queue
= op
->queue
;
1556 struct nvme_completion
*cqe
= &op
->rsp_iu
.cqe
;
1557 struct nvme_command
*sqe
= &op
->cmd_iu
.sqe
;
1558 __le16 status
= cpu_to_le16(NVME_SC_SUCCESS
<< 1);
1559 union nvme_result result
;
1560 bool terminate_assoc
= true;
1565 * The current linux implementation of a nvme controller
1566 * allocates a single tag set for all io queues and sizes
1567 * the io queues to fully hold all possible tags. Thus, the
1568 * implementation does not reference or care about the sqhd
1569 * value as it never needs to use the sqhd/sqtail pointers
1570 * for submission pacing.
1572 * This affects the FC-NVME implementation in two ways:
1573 * 1) As the value doesn't matter, we don't need to waste
1574 * cycles extracting it from ERSPs and stamping it in the
1575 * cases where the transport fabricates CQEs on successful
1577 * 2) The FC-NVME implementation requires that delivery of
1578 * ERSP completions are to go back to the nvme layer in order
1579 * relative to the rsn, such that the sqhd value will always
1580 * be "in order" for the nvme layer. As the nvme layer in
1581 * linux doesn't care about sqhd, there's no need to return
1585 * As the core nvme layer in linux currently does not look at
1586 * every field in the cqe - in cases where the FC transport must
1587 * fabricate a CQE, the following fields will not be set as they
1588 * are not referenced:
1589 * cqe.sqid, cqe.sqhd, cqe.command_id
1591 * Failure or error of an individual i/o, in a transport
1592 * detected fashion unrelated to the nvme completion status,
1593 * potentially cause the initiator and target sides to get out
1594 * of sync on SQ head/tail (aka outstanding io count allowed).
1595 * Per FC-NVME spec, failure of an individual command requires
1596 * the connection to be terminated, which in turn requires the
1597 * association to be terminated.
1600 opstate
= atomic_xchg(&op
->state
, FCPOP_STATE_COMPLETE
);
1602 fc_dma_sync_single_for_cpu(ctrl
->lport
->dev
, op
->fcp_req
.rspdma
,
1603 sizeof(op
->rsp_iu
), DMA_FROM_DEVICE
);
1605 if (opstate
== FCPOP_STATE_ABORTED
)
1606 status
= cpu_to_le16(NVME_SC_HOST_PATH_ERROR
<< 1);
1607 else if (freq
->status
) {
1608 status
= cpu_to_le16(NVME_SC_HOST_PATH_ERROR
<< 1);
1609 dev_info(ctrl
->ctrl
.device
,
1610 "NVME-FC{%d}: io failed due to lldd error %d\n",
1611 ctrl
->cnum
, freq
->status
);
1615 * For the linux implementation, if we have an unsuccesful
1616 * status, they blk-mq layer can typically be called with the
1617 * non-zero status and the content of the cqe isn't important.
1623 * command completed successfully relative to the wire
1624 * protocol. However, validate anything received and
1625 * extract the status and result from the cqe (create it
1629 switch (freq
->rcv_rsplen
) {
1632 case NVME_FC_SIZEOF_ZEROS_RSP
:
1634 * No response payload or 12 bytes of payload (which
1635 * should all be zeros) are considered successful and
1636 * no payload in the CQE by the transport.
1638 if (freq
->transferred_length
!=
1639 be32_to_cpu(op
->cmd_iu
.data_len
)) {
1640 status
= cpu_to_le16(NVME_SC_HOST_PATH_ERROR
<< 1);
1641 dev_info(ctrl
->ctrl
.device
,
1642 "NVME-FC{%d}: io failed due to bad transfer "
1643 "length: %d vs expected %d\n",
1644 ctrl
->cnum
, freq
->transferred_length
,
1645 be32_to_cpu(op
->cmd_iu
.data_len
));
1651 case sizeof(struct nvme_fc_ersp_iu
):
1653 * The ERSP IU contains a full completion with CQE.
1654 * Validate ERSP IU and look at cqe.
1656 if (unlikely(be16_to_cpu(op
->rsp_iu
.iu_len
) !=
1657 (freq
->rcv_rsplen
/ 4) ||
1658 be32_to_cpu(op
->rsp_iu
.xfrd_len
) !=
1659 freq
->transferred_length
||
1660 op
->rsp_iu
.ersp_result
||
1661 sqe
->common
.command_id
!= cqe
->command_id
)) {
1662 status
= cpu_to_le16(NVME_SC_HOST_PATH_ERROR
<< 1);
1663 dev_info(ctrl
->ctrl
.device
,
1664 "NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
1665 "iu len %d, xfr len %d vs %d, status code "
1666 "%d, cmdid %d vs %d\n",
1667 ctrl
->cnum
, be16_to_cpu(op
->rsp_iu
.iu_len
),
1668 be32_to_cpu(op
->rsp_iu
.xfrd_len
),
1669 freq
->transferred_length
,
1670 op
->rsp_iu
.ersp_result
,
1671 sqe
->common
.command_id
,
1675 result
= cqe
->result
;
1676 status
= cqe
->status
;
1680 status
= cpu_to_le16(NVME_SC_HOST_PATH_ERROR
<< 1);
1681 dev_info(ctrl
->ctrl
.device
,
1682 "NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
1684 ctrl
->cnum
, freq
->rcv_rsplen
);
1688 terminate_assoc
= false;
1691 if (op
->flags
& FCOP_FLAGS_AEN
) {
1692 nvme_complete_async_event(&queue
->ctrl
->ctrl
, status
, &result
);
1693 __nvme_fc_fcpop_chk_teardowns(ctrl
, op
, opstate
);
1694 atomic_set(&op
->state
, FCPOP_STATE_IDLE
);
1695 op
->flags
= FCOP_FLAGS_AEN
; /* clear other flags */
1696 nvme_fc_ctrl_put(ctrl
);
1700 __nvme_fc_fcpop_chk_teardowns(ctrl
, op
, opstate
);
1701 nvme_end_request(rq
, status
, result
);
1704 if (terminate_assoc
)
1705 nvme_fc_error_recovery(ctrl
, "transport detected io error");
1709 __nvme_fc_init_request(struct nvme_fc_ctrl
*ctrl
,
1710 struct nvme_fc_queue
*queue
, struct nvme_fc_fcp_op
*op
,
1711 struct request
*rq
, u32 rqno
)
1713 struct nvme_fcp_op_w_sgl
*op_w_sgl
=
1714 container_of(op
, typeof(*op_w_sgl
), op
);
1715 struct nvme_fc_cmd_iu
*cmdiu
= &op
->cmd_iu
;
1718 memset(op
, 0, sizeof(*op
));
1719 op
->fcp_req
.cmdaddr
= &op
->cmd_iu
;
1720 op
->fcp_req
.cmdlen
= sizeof(op
->cmd_iu
);
1721 op
->fcp_req
.rspaddr
= &op
->rsp_iu
;
1722 op
->fcp_req
.rsplen
= sizeof(op
->rsp_iu
);
1723 op
->fcp_req
.done
= nvme_fc_fcpio_done
;
1729 cmdiu
->format_id
= NVME_CMD_FORMAT_ID
;
1730 cmdiu
->fc_id
= NVME_CMD_FC_ID
;
1731 cmdiu
->iu_len
= cpu_to_be16(sizeof(*cmdiu
) / sizeof(u32
));
1733 cmdiu
->rsv_cat
= fccmnd_set_cat_css(0,
1734 (NVME_CC_CSS_NVM
>> NVME_CC_CSS_SHIFT
));
1736 cmdiu
->rsv_cat
= fccmnd_set_cat_admin(0);
1738 op
->fcp_req
.cmddma
= fc_dma_map_single(ctrl
->lport
->dev
,
1739 &op
->cmd_iu
, sizeof(op
->cmd_iu
), DMA_TO_DEVICE
);
1740 if (fc_dma_mapping_error(ctrl
->lport
->dev
, op
->fcp_req
.cmddma
)) {
1742 "FCP Op failed - cmdiu dma mapping failed.\n");
1747 op
->fcp_req
.rspdma
= fc_dma_map_single(ctrl
->lport
->dev
,
1748 &op
->rsp_iu
, sizeof(op
->rsp_iu
),
1750 if (fc_dma_mapping_error(ctrl
->lport
->dev
, op
->fcp_req
.rspdma
)) {
1752 "FCP Op failed - rspiu dma mapping failed.\n");
1756 atomic_set(&op
->state
, FCPOP_STATE_IDLE
);
1762 nvme_fc_init_request(struct blk_mq_tag_set
*set
, struct request
*rq
,
1763 unsigned int hctx_idx
, unsigned int numa_node
)
1765 struct nvme_fc_ctrl
*ctrl
= set
->driver_data
;
1766 struct nvme_fcp_op_w_sgl
*op
= blk_mq_rq_to_pdu(rq
);
1767 int queue_idx
= (set
== &ctrl
->tag_set
) ? hctx_idx
+ 1 : 0;
1768 struct nvme_fc_queue
*queue
= &ctrl
->queues
[queue_idx
];
1771 res
= __nvme_fc_init_request(ctrl
, queue
, &op
->op
, rq
, queue
->rqcnt
++);
1774 op
->op
.fcp_req
.first_sgl
= &op
->sgl
[0];
1775 op
->op
.fcp_req
.private = &op
->priv
[0];
1776 nvme_req(rq
)->ctrl
= &ctrl
->ctrl
;
1781 nvme_fc_init_aen_ops(struct nvme_fc_ctrl
*ctrl
)
1783 struct nvme_fc_fcp_op
*aen_op
;
1784 struct nvme_fc_cmd_iu
*cmdiu
;
1785 struct nvme_command
*sqe
;
1789 aen_op
= ctrl
->aen_ops
;
1790 for (i
= 0; i
< NVME_NR_AEN_COMMANDS
; i
++, aen_op
++) {
1791 private = kzalloc(ctrl
->lport
->ops
->fcprqst_priv_sz
,
1796 cmdiu
= &aen_op
->cmd_iu
;
1798 ret
= __nvme_fc_init_request(ctrl
, &ctrl
->queues
[0],
1799 aen_op
, (struct request
*)NULL
,
1800 (NVME_AQ_BLK_MQ_DEPTH
+ i
));
1806 aen_op
->flags
= FCOP_FLAGS_AEN
;
1807 aen_op
->fcp_req
.private = private;
1809 memset(sqe
, 0, sizeof(*sqe
));
1810 sqe
->common
.opcode
= nvme_admin_async_event
;
1811 /* Note: core layer may overwrite the sqe.command_id value */
1812 sqe
->common
.command_id
= NVME_AQ_BLK_MQ_DEPTH
+ i
;
1818 nvme_fc_term_aen_ops(struct nvme_fc_ctrl
*ctrl
)
1820 struct nvme_fc_fcp_op
*aen_op
;
1823 aen_op
= ctrl
->aen_ops
;
1824 for (i
= 0; i
< NVME_NR_AEN_COMMANDS
; i
++, aen_op
++) {
1825 if (!aen_op
->fcp_req
.private)
1828 __nvme_fc_exit_request(ctrl
, aen_op
);
1830 kfree(aen_op
->fcp_req
.private);
1831 aen_op
->fcp_req
.private = NULL
;
1836 __nvme_fc_init_hctx(struct blk_mq_hw_ctx
*hctx
, struct nvme_fc_ctrl
*ctrl
,
1839 struct nvme_fc_queue
*queue
= &ctrl
->queues
[qidx
];
1841 hctx
->driver_data
= queue
;
1846 nvme_fc_init_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
1847 unsigned int hctx_idx
)
1849 struct nvme_fc_ctrl
*ctrl
= data
;
1851 __nvme_fc_init_hctx(hctx
, ctrl
, hctx_idx
+ 1);
1857 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
1858 unsigned int hctx_idx
)
1860 struct nvme_fc_ctrl
*ctrl
= data
;
1862 __nvme_fc_init_hctx(hctx
, ctrl
, hctx_idx
);
1868 nvme_fc_init_queue(struct nvme_fc_ctrl
*ctrl
, int idx
)
1870 struct nvme_fc_queue
*queue
;
1872 queue
= &ctrl
->queues
[idx
];
1873 memset(queue
, 0, sizeof(*queue
));
1876 atomic_set(&queue
->csn
, 0);
1877 queue
->dev
= ctrl
->dev
;
1880 queue
->cmnd_capsule_len
= ctrl
->ctrl
.ioccsz
* 16;
1882 queue
->cmnd_capsule_len
= sizeof(struct nvme_command
);
1885 * Considered whether we should allocate buffers for all SQEs
1886 * and CQEs and dma map them - mapping their respective entries
1887 * into the request structures (kernel vm addr and dma address)
1888 * thus the driver could use the buffers/mappings directly.
1889 * It only makes sense if the LLDD would use them for its
1890 * messaging api. It's very unlikely most adapter api's would use
1891 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1892 * structures were used instead.
1897 * This routine terminates a queue at the transport level.
1898 * The transport has already ensured that all outstanding ios on
1899 * the queue have been terminated.
1900 * The transport will send a Disconnect LS request to terminate
1901 * the queue's connection. Termination of the admin queue will also
1902 * terminate the association at the target.
1905 nvme_fc_free_queue(struct nvme_fc_queue
*queue
)
1907 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED
, &queue
->flags
))
1910 clear_bit(NVME_FC_Q_LIVE
, &queue
->flags
);
1912 * Current implementation never disconnects a single queue.
1913 * It always terminates a whole association. So there is never
1914 * a disconnect(queue) LS sent to the target.
1917 queue
->connection_id
= 0;
1918 atomic_set(&queue
->csn
, 0);
1922 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl
*ctrl
,
1923 struct nvme_fc_queue
*queue
, unsigned int qidx
)
1925 if (ctrl
->lport
->ops
->delete_queue
)
1926 ctrl
->lport
->ops
->delete_queue(&ctrl
->lport
->localport
, qidx
,
1927 queue
->lldd_handle
);
1928 queue
->lldd_handle
= NULL
;
1932 nvme_fc_free_io_queues(struct nvme_fc_ctrl
*ctrl
)
1936 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++)
1937 nvme_fc_free_queue(&ctrl
->queues
[i
]);
1941 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl
*ctrl
,
1942 struct nvme_fc_queue
*queue
, unsigned int qidx
, u16 qsize
)
1946 queue
->lldd_handle
= NULL
;
1947 if (ctrl
->lport
->ops
->create_queue
)
1948 ret
= ctrl
->lport
->ops
->create_queue(&ctrl
->lport
->localport
,
1949 qidx
, qsize
, &queue
->lldd_handle
);
1955 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl
*ctrl
)
1957 struct nvme_fc_queue
*queue
= &ctrl
->queues
[ctrl
->ctrl
.queue_count
- 1];
1960 for (i
= ctrl
->ctrl
.queue_count
- 1; i
>= 1; i
--, queue
--)
1961 __nvme_fc_delete_hw_queue(ctrl
, queue
, i
);
1965 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl
*ctrl
, u16 qsize
)
1967 struct nvme_fc_queue
*queue
= &ctrl
->queues
[1];
1970 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++, queue
++) {
1971 ret
= __nvme_fc_create_hw_queue(ctrl
, queue
, i
, qsize
);
1980 __nvme_fc_delete_hw_queue(ctrl
, &ctrl
->queues
[i
], i
);
1985 nvme_fc_connect_io_queues(struct nvme_fc_ctrl
*ctrl
, u16 qsize
)
1989 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++) {
1990 ret
= nvme_fc_connect_queue(ctrl
, &ctrl
->queues
[i
], qsize
,
1994 ret
= nvmf_connect_io_queue(&ctrl
->ctrl
, i
, false);
1998 set_bit(NVME_FC_Q_LIVE
, &ctrl
->queues
[i
].flags
);
2005 nvme_fc_init_io_queues(struct nvme_fc_ctrl
*ctrl
)
2009 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++)
2010 nvme_fc_init_queue(ctrl
, i
);
2014 nvme_fc_ctrl_free(struct kref
*ref
)
2016 struct nvme_fc_ctrl
*ctrl
=
2017 container_of(ref
, struct nvme_fc_ctrl
, ref
);
2018 unsigned long flags
;
2020 if (ctrl
->ctrl
.tagset
) {
2021 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
2022 blk_mq_free_tag_set(&ctrl
->tag_set
);
2025 /* remove from rport list */
2026 spin_lock_irqsave(&ctrl
->rport
->lock
, flags
);
2027 list_del(&ctrl
->ctrl_list
);
2028 spin_unlock_irqrestore(&ctrl
->rport
->lock
, flags
);
2030 blk_mq_unquiesce_queue(ctrl
->ctrl
.admin_q
);
2031 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
2032 blk_cleanup_queue(ctrl
->ctrl
.fabrics_q
);
2033 blk_mq_free_tag_set(&ctrl
->admin_tag_set
);
2035 kfree(ctrl
->queues
);
2037 put_device(ctrl
->dev
);
2038 nvme_fc_rport_put(ctrl
->rport
);
2040 ida_simple_remove(&nvme_fc_ctrl_cnt
, ctrl
->cnum
);
2041 if (ctrl
->ctrl
.opts
)
2042 nvmf_free_options(ctrl
->ctrl
.opts
);
2047 nvme_fc_ctrl_put(struct nvme_fc_ctrl
*ctrl
)
2049 kref_put(&ctrl
->ref
, nvme_fc_ctrl_free
);
2053 nvme_fc_ctrl_get(struct nvme_fc_ctrl
*ctrl
)
2055 return kref_get_unless_zero(&ctrl
->ref
);
2059 * All accesses from nvme core layer done - can now free the
2060 * controller. Called after last nvme_put_ctrl() call
2063 nvme_fc_nvme_ctrl_freed(struct nvme_ctrl
*nctrl
)
2065 struct nvme_fc_ctrl
*ctrl
= to_fc_ctrl(nctrl
);
2067 WARN_ON(nctrl
!= &ctrl
->ctrl
);
2069 nvme_fc_ctrl_put(ctrl
);
2073 nvme_fc_error_recovery(struct nvme_fc_ctrl
*ctrl
, char *errmsg
)
2078 * if an error (io timeout, etc) while (re)connecting,
2079 * it's an error on creating the new association.
2080 * Start the error recovery thread if it hasn't already
2081 * been started. It is expected there could be multiple
2082 * ios hitting this path before things are cleaned up.
2084 if (ctrl
->ctrl
.state
== NVME_CTRL_CONNECTING
) {
2085 active
= atomic_xchg(&ctrl
->err_work_active
, 1);
2086 if (!active
&& !queue_work(nvme_fc_wq
, &ctrl
->err_work
)) {
2087 atomic_set(&ctrl
->err_work_active
, 0);
2093 /* Otherwise, only proceed if in LIVE state - e.g. on first error */
2094 if (ctrl
->ctrl
.state
!= NVME_CTRL_LIVE
)
2097 dev_warn(ctrl
->ctrl
.device
,
2098 "NVME-FC{%d}: transport association error detected: %s\n",
2099 ctrl
->cnum
, errmsg
);
2100 dev_warn(ctrl
->ctrl
.device
,
2101 "NVME-FC{%d}: resetting controller\n", ctrl
->cnum
);
2103 nvme_reset_ctrl(&ctrl
->ctrl
);
2106 static enum blk_eh_timer_return
2107 nvme_fc_timeout(struct request
*rq
, bool reserved
)
2109 struct nvme_fc_fcp_op
*op
= blk_mq_rq_to_pdu(rq
);
2110 struct nvme_fc_ctrl
*ctrl
= op
->ctrl
;
2113 * we can't individually ABTS an io without affecting the queue,
2114 * thus killing the queue, and thus the association.
2115 * So resolve by performing a controller reset, which will stop
2116 * the host/io stack, terminate the association on the link,
2117 * and recreate an association on the link.
2119 nvme_fc_error_recovery(ctrl
, "io timeout error");
2122 * the io abort has been initiated. Have the reset timer
2123 * restarted and the abort completion will complete the io
2124 * shortly. Avoids a synchronous wait while the abort finishes.
2126 return BLK_EH_RESET_TIMER
;
2130 nvme_fc_map_data(struct nvme_fc_ctrl
*ctrl
, struct request
*rq
,
2131 struct nvme_fc_fcp_op
*op
)
2133 struct nvmefc_fcp_req
*freq
= &op
->fcp_req
;
2138 if (!blk_rq_nr_phys_segments(rq
))
2141 freq
->sg_table
.sgl
= freq
->first_sgl
;
2142 ret
= sg_alloc_table_chained(&freq
->sg_table
,
2143 blk_rq_nr_phys_segments(rq
), freq
->sg_table
.sgl
,
2148 op
->nents
= blk_rq_map_sg(rq
->q
, rq
, freq
->sg_table
.sgl
);
2149 WARN_ON(op
->nents
> blk_rq_nr_phys_segments(rq
));
2150 freq
->sg_cnt
= fc_dma_map_sg(ctrl
->lport
->dev
, freq
->sg_table
.sgl
,
2151 op
->nents
, rq_dma_dir(rq
));
2152 if (unlikely(freq
->sg_cnt
<= 0)) {
2153 sg_free_table_chained(&freq
->sg_table
, SG_CHUNK_SIZE
);
2159 * TODO: blk_integrity_rq(rq) for DIF
2165 nvme_fc_unmap_data(struct nvme_fc_ctrl
*ctrl
, struct request
*rq
,
2166 struct nvme_fc_fcp_op
*op
)
2168 struct nvmefc_fcp_req
*freq
= &op
->fcp_req
;
2173 fc_dma_unmap_sg(ctrl
->lport
->dev
, freq
->sg_table
.sgl
, op
->nents
,
2176 sg_free_table_chained(&freq
->sg_table
, SG_CHUNK_SIZE
);
2182 * In FC, the queue is a logical thing. At transport connect, the target
2183 * creates its "queue" and returns a handle that is to be given to the
2184 * target whenever it posts something to the corresponding SQ. When an
2185 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2186 * command contained within the SQE, an io, and assigns a FC exchange
2187 * to it. The SQE and the associated SQ handle are sent in the initial
2188 * CMD IU sents on the exchange. All transfers relative to the io occur
2189 * as part of the exchange. The CQE is the last thing for the io,
2190 * which is transferred (explicitly or implicitly) with the RSP IU
2191 * sent on the exchange. After the CQE is received, the FC exchange is
2192 * terminaed and the Exchange may be used on a different io.
2194 * The transport to LLDD api has the transport making a request for a
2195 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2196 * resource and transfers the command. The LLDD will then process all
2197 * steps to complete the io. Upon completion, the transport done routine
2200 * So - while the operation is outstanding to the LLDD, there is a link
2201 * level FC exchange resource that is also outstanding. This must be
2202 * considered in all cleanup operations.
2205 nvme_fc_start_fcp_op(struct nvme_fc_ctrl
*ctrl
, struct nvme_fc_queue
*queue
,
2206 struct nvme_fc_fcp_op
*op
, u32 data_len
,
2207 enum nvmefc_fcp_datadir io_dir
)
2209 struct nvme_fc_cmd_iu
*cmdiu
= &op
->cmd_iu
;
2210 struct nvme_command
*sqe
= &cmdiu
->sqe
;
2214 * before attempting to send the io, check to see if we believe
2215 * the target device is present
2217 if (ctrl
->rport
->remoteport
.port_state
!= FC_OBJSTATE_ONLINE
)
2218 return BLK_STS_RESOURCE
;
2220 if (!nvme_fc_ctrl_get(ctrl
))
2221 return BLK_STS_IOERR
;
2223 /* format the FC-NVME CMD IU and fcp_req */
2224 cmdiu
->connection_id
= cpu_to_be64(queue
->connection_id
);
2225 cmdiu
->data_len
= cpu_to_be32(data_len
);
2227 case NVMEFC_FCP_WRITE
:
2228 cmdiu
->flags
= FCNVME_CMD_FLAGS_WRITE
;
2230 case NVMEFC_FCP_READ
:
2231 cmdiu
->flags
= FCNVME_CMD_FLAGS_READ
;
2233 case NVMEFC_FCP_NODATA
:
2237 op
->fcp_req
.payload_length
= data_len
;
2238 op
->fcp_req
.io_dir
= io_dir
;
2239 op
->fcp_req
.transferred_length
= 0;
2240 op
->fcp_req
.rcv_rsplen
= 0;
2241 op
->fcp_req
.status
= NVME_SC_SUCCESS
;
2242 op
->fcp_req
.sqid
= cpu_to_le16(queue
->qnum
);
2245 * validate per fabric rules, set fields mandated by fabric spec
2246 * as well as those by FC-NVME spec.
2248 WARN_ON_ONCE(sqe
->common
.metadata
);
2249 sqe
->common
.flags
|= NVME_CMD_SGL_METABUF
;
2252 * format SQE DPTR field per FC-NVME rules:
2253 * type=0x5 Transport SGL Data Block Descriptor
2254 * subtype=0xA Transport-specific value
2256 * length=length of the data series
2258 sqe
->rw
.dptr
.sgl
.type
= (NVME_TRANSPORT_SGL_DATA_DESC
<< 4) |
2259 NVME_SGL_FMT_TRANSPORT_A
;
2260 sqe
->rw
.dptr
.sgl
.length
= cpu_to_le32(data_len
);
2261 sqe
->rw
.dptr
.sgl
.addr
= 0;
2263 if (!(op
->flags
& FCOP_FLAGS_AEN
)) {
2264 ret
= nvme_fc_map_data(ctrl
, op
->rq
, op
);
2266 nvme_cleanup_cmd(op
->rq
);
2267 nvme_fc_ctrl_put(ctrl
);
2268 if (ret
== -ENOMEM
|| ret
== -EAGAIN
)
2269 return BLK_STS_RESOURCE
;
2270 return BLK_STS_IOERR
;
2274 fc_dma_sync_single_for_device(ctrl
->lport
->dev
, op
->fcp_req
.cmddma
,
2275 sizeof(op
->cmd_iu
), DMA_TO_DEVICE
);
2277 atomic_set(&op
->state
, FCPOP_STATE_ACTIVE
);
2279 if (!(op
->flags
& FCOP_FLAGS_AEN
))
2280 blk_mq_start_request(op
->rq
);
2282 cmdiu
->csn
= cpu_to_be32(atomic_inc_return(&queue
->csn
));
2283 ret
= ctrl
->lport
->ops
->fcp_io(&ctrl
->lport
->localport
,
2284 &ctrl
->rport
->remoteport
,
2285 queue
->lldd_handle
, &op
->fcp_req
);
2289 * If the lld fails to send the command is there an issue with
2290 * the csn value? If the command that fails is the Connect,
2291 * no - as the connection won't be live. If it is a command
2292 * post-connect, it's possible a gap in csn may be created.
2293 * Does this matter? As Linux initiators don't send fused
2294 * commands, no. The gap would exist, but as there's nothing
2295 * that depends on csn order to be delivered on the target
2296 * side, it shouldn't hurt. It would be difficult for a
2297 * target to even detect the csn gap as it has no idea when the
2298 * cmd with the csn was supposed to arrive.
2300 opstate
= atomic_xchg(&op
->state
, FCPOP_STATE_COMPLETE
);
2301 __nvme_fc_fcpop_chk_teardowns(ctrl
, op
, opstate
);
2303 if (!(op
->flags
& FCOP_FLAGS_AEN
))
2304 nvme_fc_unmap_data(ctrl
, op
->rq
, op
);
2306 nvme_cleanup_cmd(op
->rq
);
2307 nvme_fc_ctrl_put(ctrl
);
2309 if (ctrl
->rport
->remoteport
.port_state
== FC_OBJSTATE_ONLINE
&&
2311 return BLK_STS_IOERR
;
2313 return BLK_STS_RESOURCE
;
2320 nvme_fc_queue_rq(struct blk_mq_hw_ctx
*hctx
,
2321 const struct blk_mq_queue_data
*bd
)
2323 struct nvme_ns
*ns
= hctx
->queue
->queuedata
;
2324 struct nvme_fc_queue
*queue
= hctx
->driver_data
;
2325 struct nvme_fc_ctrl
*ctrl
= queue
->ctrl
;
2326 struct request
*rq
= bd
->rq
;
2327 struct nvme_fc_fcp_op
*op
= blk_mq_rq_to_pdu(rq
);
2328 struct nvme_fc_cmd_iu
*cmdiu
= &op
->cmd_iu
;
2329 struct nvme_command
*sqe
= &cmdiu
->sqe
;
2330 enum nvmefc_fcp_datadir io_dir
;
2331 bool queue_ready
= test_bit(NVME_FC_Q_LIVE
, &queue
->flags
);
2335 if (ctrl
->rport
->remoteport
.port_state
!= FC_OBJSTATE_ONLINE
||
2336 !nvmf_check_ready(&queue
->ctrl
->ctrl
, rq
, queue_ready
))
2337 return nvmf_fail_nonready_command(&queue
->ctrl
->ctrl
, rq
);
2339 ret
= nvme_setup_cmd(ns
, rq
, sqe
);
2344 * nvme core doesn't quite treat the rq opaquely. Commands such
2345 * as WRITE ZEROES will return a non-zero rq payload_bytes yet
2346 * there is no actual payload to be transferred.
2347 * To get it right, key data transmission on there being 1 or
2348 * more physical segments in the sg list. If there is no
2349 * physical segments, there is no payload.
2351 if (blk_rq_nr_phys_segments(rq
)) {
2352 data_len
= blk_rq_payload_bytes(rq
);
2353 io_dir
= ((rq_data_dir(rq
) == WRITE
) ?
2354 NVMEFC_FCP_WRITE
: NVMEFC_FCP_READ
);
2357 io_dir
= NVMEFC_FCP_NODATA
;
2361 return nvme_fc_start_fcp_op(ctrl
, queue
, op
, data_len
, io_dir
);
2365 nvme_fc_submit_async_event(struct nvme_ctrl
*arg
)
2367 struct nvme_fc_ctrl
*ctrl
= to_fc_ctrl(arg
);
2368 struct nvme_fc_fcp_op
*aen_op
;
2369 unsigned long flags
;
2370 bool terminating
= false;
2373 spin_lock_irqsave(&ctrl
->lock
, flags
);
2374 if (ctrl
->flags
& FCCTRL_TERMIO
)
2376 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
2381 aen_op
= &ctrl
->aen_ops
[0];
2383 ret
= nvme_fc_start_fcp_op(ctrl
, aen_op
->queue
, aen_op
, 0,
2386 dev_err(ctrl
->ctrl
.device
,
2387 "failed async event work\n");
2391 nvme_fc_complete_rq(struct request
*rq
)
2393 struct nvme_fc_fcp_op
*op
= blk_mq_rq_to_pdu(rq
);
2394 struct nvme_fc_ctrl
*ctrl
= op
->ctrl
;
2396 atomic_set(&op
->state
, FCPOP_STATE_IDLE
);
2398 nvme_fc_unmap_data(ctrl
, rq
, op
);
2399 nvme_complete_rq(rq
);
2400 nvme_fc_ctrl_put(ctrl
);
2404 * This routine is used by the transport when it needs to find active
2405 * io on a queue that is to be terminated. The transport uses
2406 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2407 * this routine to kill them on a 1 by 1 basis.
2409 * As FC allocates FC exchange for each io, the transport must contact
2410 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2411 * After terminating the exchange the LLDD will call the transport's
2412 * normal io done path for the request, but it will have an aborted
2413 * status. The done path will return the io request back to the block
2414 * layer with an error status.
2417 nvme_fc_terminate_exchange(struct request
*req
, void *data
, bool reserved
)
2419 struct nvme_ctrl
*nctrl
= data
;
2420 struct nvme_fc_ctrl
*ctrl
= to_fc_ctrl(nctrl
);
2421 struct nvme_fc_fcp_op
*op
= blk_mq_rq_to_pdu(req
);
2423 __nvme_fc_abort_op(ctrl
, op
);
2428 static const struct blk_mq_ops nvme_fc_mq_ops
= {
2429 .queue_rq
= nvme_fc_queue_rq
,
2430 .complete
= nvme_fc_complete_rq
,
2431 .init_request
= nvme_fc_init_request
,
2432 .exit_request
= nvme_fc_exit_request
,
2433 .init_hctx
= nvme_fc_init_hctx
,
2434 .timeout
= nvme_fc_timeout
,
2438 nvme_fc_create_io_queues(struct nvme_fc_ctrl
*ctrl
)
2440 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
2441 unsigned int nr_io_queues
;
2444 nr_io_queues
= min(min(opts
->nr_io_queues
, num_online_cpus()),
2445 ctrl
->lport
->ops
->max_hw_queues
);
2446 ret
= nvme_set_queue_count(&ctrl
->ctrl
, &nr_io_queues
);
2448 dev_info(ctrl
->ctrl
.device
,
2449 "set_queue_count failed: %d\n", ret
);
2453 ctrl
->ctrl
.queue_count
= nr_io_queues
+ 1;
2457 nvme_fc_init_io_queues(ctrl
);
2459 memset(&ctrl
->tag_set
, 0, sizeof(ctrl
->tag_set
));
2460 ctrl
->tag_set
.ops
= &nvme_fc_mq_ops
;
2461 ctrl
->tag_set
.queue_depth
= ctrl
->ctrl
.opts
->queue_size
;
2462 ctrl
->tag_set
.reserved_tags
= 1; /* fabric connect */
2463 ctrl
->tag_set
.numa_node
= ctrl
->ctrl
.numa_node
;
2464 ctrl
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
;
2465 ctrl
->tag_set
.cmd_size
=
2466 struct_size((struct nvme_fcp_op_w_sgl
*)NULL
, priv
,
2467 ctrl
->lport
->ops
->fcprqst_priv_sz
);
2468 ctrl
->tag_set
.driver_data
= ctrl
;
2469 ctrl
->tag_set
.nr_hw_queues
= ctrl
->ctrl
.queue_count
- 1;
2470 ctrl
->tag_set
.timeout
= NVME_IO_TIMEOUT
;
2472 ret
= blk_mq_alloc_tag_set(&ctrl
->tag_set
);
2476 ctrl
->ctrl
.tagset
= &ctrl
->tag_set
;
2478 ctrl
->ctrl
.connect_q
= blk_mq_init_queue(&ctrl
->tag_set
);
2479 if (IS_ERR(ctrl
->ctrl
.connect_q
)) {
2480 ret
= PTR_ERR(ctrl
->ctrl
.connect_q
);
2481 goto out_free_tag_set
;
2484 ret
= nvme_fc_create_hw_io_queues(ctrl
, ctrl
->ctrl
.sqsize
+ 1);
2486 goto out_cleanup_blk_queue
;
2488 ret
= nvme_fc_connect_io_queues(ctrl
, ctrl
->ctrl
.sqsize
+ 1);
2490 goto out_delete_hw_queues
;
2492 ctrl
->ioq_live
= true;
2496 out_delete_hw_queues
:
2497 nvme_fc_delete_hw_io_queues(ctrl
);
2498 out_cleanup_blk_queue
:
2499 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
2501 blk_mq_free_tag_set(&ctrl
->tag_set
);
2502 nvme_fc_free_io_queues(ctrl
);
2504 /* force put free routine to ignore io queues */
2505 ctrl
->ctrl
.tagset
= NULL
;
2511 nvme_fc_recreate_io_queues(struct nvme_fc_ctrl
*ctrl
)
2513 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
2514 u32 prior_ioq_cnt
= ctrl
->ctrl
.queue_count
- 1;
2515 unsigned int nr_io_queues
;
2518 nr_io_queues
= min(min(opts
->nr_io_queues
, num_online_cpus()),
2519 ctrl
->lport
->ops
->max_hw_queues
);
2520 ret
= nvme_set_queue_count(&ctrl
->ctrl
, &nr_io_queues
);
2522 dev_info(ctrl
->ctrl
.device
,
2523 "set_queue_count failed: %d\n", ret
);
2527 if (!nr_io_queues
&& prior_ioq_cnt
) {
2528 dev_info(ctrl
->ctrl
.device
,
2529 "Fail Reconnect: At least 1 io queue "
2530 "required (was %d)\n", prior_ioq_cnt
);
2534 ctrl
->ctrl
.queue_count
= nr_io_queues
+ 1;
2535 /* check for io queues existing */
2536 if (ctrl
->ctrl
.queue_count
== 1)
2539 ret
= nvme_fc_create_hw_io_queues(ctrl
, ctrl
->ctrl
.sqsize
+ 1);
2541 goto out_free_io_queues
;
2543 ret
= nvme_fc_connect_io_queues(ctrl
, ctrl
->ctrl
.sqsize
+ 1);
2545 goto out_delete_hw_queues
;
2547 if (prior_ioq_cnt
!= nr_io_queues
)
2548 dev_info(ctrl
->ctrl
.device
,
2549 "reconnect: revising io queue count from %d to %d\n",
2550 prior_ioq_cnt
, nr_io_queues
);
2551 blk_mq_update_nr_hw_queues(&ctrl
->tag_set
, nr_io_queues
);
2555 out_delete_hw_queues
:
2556 nvme_fc_delete_hw_io_queues(ctrl
);
2558 nvme_fc_free_io_queues(ctrl
);
2563 nvme_fc_rport_active_on_lport(struct nvme_fc_rport
*rport
)
2565 struct nvme_fc_lport
*lport
= rport
->lport
;
2567 atomic_inc(&lport
->act_rport_cnt
);
2571 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport
*rport
)
2573 struct nvme_fc_lport
*lport
= rport
->lport
;
2576 cnt
= atomic_dec_return(&lport
->act_rport_cnt
);
2577 if (cnt
== 0 && lport
->localport
.port_state
== FC_OBJSTATE_DELETED
)
2578 lport
->ops
->localport_delete(&lport
->localport
);
2582 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl
*ctrl
)
2584 struct nvme_fc_rport
*rport
= ctrl
->rport
;
2587 if (ctrl
->assoc_active
)
2590 ctrl
->assoc_active
= true;
2591 cnt
= atomic_inc_return(&rport
->act_ctrl_cnt
);
2593 nvme_fc_rport_active_on_lport(rport
);
2599 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl
*ctrl
)
2601 struct nvme_fc_rport
*rport
= ctrl
->rport
;
2602 struct nvme_fc_lport
*lport
= rport
->lport
;
2605 /* ctrl->assoc_active=false will be set independently */
2607 cnt
= atomic_dec_return(&rport
->act_ctrl_cnt
);
2609 if (rport
->remoteport
.port_state
== FC_OBJSTATE_DELETED
)
2610 lport
->ops
->remoteport_delete(&rport
->remoteport
);
2611 nvme_fc_rport_inactive_on_lport(rport
);
2618 * This routine restarts the controller on the host side, and
2619 * on the link side, recreates the controller association.
2622 nvme_fc_create_association(struct nvme_fc_ctrl
*ctrl
)
2624 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
2628 ++ctrl
->ctrl
.nr_reconnects
;
2630 if (ctrl
->rport
->remoteport
.port_state
!= FC_OBJSTATE_ONLINE
)
2633 if (nvme_fc_ctlr_active_on_rport(ctrl
))
2636 dev_info(ctrl
->ctrl
.device
,
2637 "NVME-FC{%d}: create association : host wwpn 0x%016llx "
2638 " rport wwpn 0x%016llx: NQN \"%s\"\n",
2639 ctrl
->cnum
, ctrl
->lport
->localport
.port_name
,
2640 ctrl
->rport
->remoteport
.port_name
, ctrl
->ctrl
.opts
->subsysnqn
);
2643 * Create the admin queue
2646 ret
= __nvme_fc_create_hw_queue(ctrl
, &ctrl
->queues
[0], 0,
2649 goto out_free_queue
;
2651 ret
= nvme_fc_connect_admin_queue(ctrl
, &ctrl
->queues
[0],
2652 NVME_AQ_DEPTH
, (NVME_AQ_DEPTH
/ 4));
2654 goto out_delete_hw_queue
;
2656 ret
= nvmf_connect_admin_queue(&ctrl
->ctrl
);
2658 goto out_disconnect_admin_queue
;
2660 set_bit(NVME_FC_Q_LIVE
, &ctrl
->queues
[0].flags
);
2663 * Check controller capabilities
2665 * todo:- add code to check if ctrl attributes changed from
2666 * prior connection values
2669 ret
= nvme_enable_ctrl(&ctrl
->ctrl
);
2671 goto out_disconnect_admin_queue
;
2673 ctrl
->ctrl
.max_hw_sectors
=
2674 (ctrl
->lport
->ops
->max_sgl_segments
- 1) << (PAGE_SHIFT
- 9);
2676 blk_mq_unquiesce_queue(ctrl
->ctrl
.admin_q
);
2678 ret
= nvme_init_identify(&ctrl
->ctrl
);
2680 goto out_disconnect_admin_queue
;
2684 /* FC-NVME does not have other data in the capsule */
2685 if (ctrl
->ctrl
.icdoff
) {
2686 dev_err(ctrl
->ctrl
.device
, "icdoff %d is not supported!\n",
2688 goto out_disconnect_admin_queue
;
2691 /* FC-NVME supports normal SGL Data Block Descriptors */
2693 if (opts
->queue_size
> ctrl
->ctrl
.maxcmd
) {
2694 /* warn if maxcmd is lower than queue_size */
2695 dev_warn(ctrl
->ctrl
.device
,
2696 "queue_size %zu > ctrl maxcmd %u, reducing "
2698 opts
->queue_size
, ctrl
->ctrl
.maxcmd
);
2699 opts
->queue_size
= ctrl
->ctrl
.maxcmd
;
2702 if (opts
->queue_size
> ctrl
->ctrl
.sqsize
+ 1) {
2703 /* warn if sqsize is lower than queue_size */
2704 dev_warn(ctrl
->ctrl
.device
,
2705 "queue_size %zu > ctrl sqsize %u, reducing "
2707 opts
->queue_size
, ctrl
->ctrl
.sqsize
+ 1);
2708 opts
->queue_size
= ctrl
->ctrl
.sqsize
+ 1;
2711 ret
= nvme_fc_init_aen_ops(ctrl
);
2713 goto out_term_aen_ops
;
2716 * Create the io queues
2719 if (ctrl
->ctrl
.queue_count
> 1) {
2720 if (!ctrl
->ioq_live
)
2721 ret
= nvme_fc_create_io_queues(ctrl
);
2723 ret
= nvme_fc_recreate_io_queues(ctrl
);
2725 goto out_term_aen_ops
;
2728 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
2730 ctrl
->ctrl
.nr_reconnects
= 0;
2733 nvme_start_ctrl(&ctrl
->ctrl
);
2735 return 0; /* Success */
2738 nvme_fc_term_aen_ops(ctrl
);
2739 out_disconnect_admin_queue
:
2740 /* send a Disconnect(association) LS to fc-nvme target */
2741 nvme_fc_xmt_disconnect_assoc(ctrl
);
2742 ctrl
->association_id
= 0;
2743 out_delete_hw_queue
:
2744 __nvme_fc_delete_hw_queue(ctrl
, &ctrl
->queues
[0], 0);
2746 nvme_fc_free_queue(&ctrl
->queues
[0]);
2747 ctrl
->assoc_active
= false;
2748 nvme_fc_ctlr_inactive_on_rport(ctrl
);
2754 * This routine stops operation of the controller on the host side.
2755 * On the host os stack side: Admin and IO queues are stopped,
2756 * outstanding ios on them terminated via FC ABTS.
2757 * On the link side: the association is terminated.
2760 nvme_fc_delete_association(struct nvme_fc_ctrl
*ctrl
)
2762 unsigned long flags
;
2764 if (!ctrl
->assoc_active
)
2766 ctrl
->assoc_active
= false;
2768 spin_lock_irqsave(&ctrl
->lock
, flags
);
2769 ctrl
->flags
|= FCCTRL_TERMIO
;
2771 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
2774 * If io queues are present, stop them and terminate all outstanding
2775 * ios on them. As FC allocates FC exchange for each io, the
2776 * transport must contact the LLDD to terminate the exchange,
2777 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2778 * to tell us what io's are busy and invoke a transport routine
2779 * to kill them with the LLDD. After terminating the exchange
2780 * the LLDD will call the transport's normal io done path, but it
2781 * will have an aborted status. The done path will return the
2782 * io requests back to the block layer as part of normal completions
2783 * (but with error status).
2785 if (ctrl
->ctrl
.queue_count
> 1) {
2786 nvme_stop_queues(&ctrl
->ctrl
);
2787 blk_mq_tagset_busy_iter(&ctrl
->tag_set
,
2788 nvme_fc_terminate_exchange
, &ctrl
->ctrl
);
2789 blk_mq_tagset_wait_completed_request(&ctrl
->tag_set
);
2793 * Other transports, which don't have link-level contexts bound
2794 * to sqe's, would try to gracefully shutdown the controller by
2795 * writing the registers for shutdown and polling (call
2796 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
2797 * just aborted and we will wait on those contexts, and given
2798 * there was no indication of how live the controlelr is on the
2799 * link, don't send more io to create more contexts for the
2800 * shutdown. Let the controller fail via keepalive failure if
2801 * its still present.
2805 * clean up the admin queue. Same thing as above.
2806 * use blk_mq_tagset_busy_itr() and the transport routine to
2807 * terminate the exchanges.
2809 blk_mq_quiesce_queue(ctrl
->ctrl
.admin_q
);
2810 blk_mq_tagset_busy_iter(&ctrl
->admin_tag_set
,
2811 nvme_fc_terminate_exchange
, &ctrl
->ctrl
);
2812 blk_mq_tagset_wait_completed_request(&ctrl
->admin_tag_set
);
2814 /* kill the aens as they are a separate path */
2815 nvme_fc_abort_aen_ops(ctrl
);
2817 /* wait for all io that had to be aborted */
2818 spin_lock_irq(&ctrl
->lock
);
2819 wait_event_lock_irq(ctrl
->ioabort_wait
, ctrl
->iocnt
== 0, ctrl
->lock
);
2820 ctrl
->flags
&= ~FCCTRL_TERMIO
;
2821 spin_unlock_irq(&ctrl
->lock
);
2823 nvme_fc_term_aen_ops(ctrl
);
2826 * send a Disconnect(association) LS to fc-nvme target
2827 * Note: could have been sent at top of process, but
2828 * cleaner on link traffic if after the aborts complete.
2829 * Note: if association doesn't exist, association_id will be 0
2831 if (ctrl
->association_id
)
2832 nvme_fc_xmt_disconnect_assoc(ctrl
);
2834 ctrl
->association_id
= 0;
2836 if (ctrl
->ctrl
.tagset
) {
2837 nvme_fc_delete_hw_io_queues(ctrl
);
2838 nvme_fc_free_io_queues(ctrl
);
2841 __nvme_fc_delete_hw_queue(ctrl
, &ctrl
->queues
[0], 0);
2842 nvme_fc_free_queue(&ctrl
->queues
[0]);
2844 /* re-enable the admin_q so anything new can fast fail */
2845 blk_mq_unquiesce_queue(ctrl
->ctrl
.admin_q
);
2847 /* resume the io queues so that things will fast fail */
2848 nvme_start_queues(&ctrl
->ctrl
);
2850 nvme_fc_ctlr_inactive_on_rport(ctrl
);
2854 nvme_fc_delete_ctrl(struct nvme_ctrl
*nctrl
)
2856 struct nvme_fc_ctrl
*ctrl
= to_fc_ctrl(nctrl
);
2858 cancel_work_sync(&ctrl
->err_work
);
2859 cancel_delayed_work_sync(&ctrl
->connect_work
);
2861 * kill the association on the link side. this will block
2862 * waiting for io to terminate
2864 nvme_fc_delete_association(ctrl
);
2868 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl
*ctrl
, int status
)
2870 struct nvme_fc_rport
*rport
= ctrl
->rport
;
2871 struct nvme_fc_remote_port
*portptr
= &rport
->remoteport
;
2872 unsigned long recon_delay
= ctrl
->ctrl
.opts
->reconnect_delay
* HZ
;
2875 if (ctrl
->ctrl
.state
!= NVME_CTRL_CONNECTING
)
2878 if (portptr
->port_state
== FC_OBJSTATE_ONLINE
)
2879 dev_info(ctrl
->ctrl
.device
,
2880 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
2881 ctrl
->cnum
, status
);
2882 else if (time_after_eq(jiffies
, rport
->dev_loss_end
))
2885 if (recon
&& nvmf_should_reconnect(&ctrl
->ctrl
)) {
2886 if (portptr
->port_state
== FC_OBJSTATE_ONLINE
)
2887 dev_info(ctrl
->ctrl
.device
,
2888 "NVME-FC{%d}: Reconnect attempt in %ld "
2890 ctrl
->cnum
, recon_delay
/ HZ
);
2891 else if (time_after(jiffies
+ recon_delay
, rport
->dev_loss_end
))
2892 recon_delay
= rport
->dev_loss_end
- jiffies
;
2894 queue_delayed_work(nvme_wq
, &ctrl
->connect_work
, recon_delay
);
2896 if (portptr
->port_state
== FC_OBJSTATE_ONLINE
)
2897 dev_warn(ctrl
->ctrl
.device
,
2898 "NVME-FC{%d}: Max reconnect attempts (%d) "
2900 ctrl
->cnum
, ctrl
->ctrl
.nr_reconnects
);
2902 dev_warn(ctrl
->ctrl
.device
,
2903 "NVME-FC{%d}: dev_loss_tmo (%d) expired "
2904 "while waiting for remoteport connectivity.\n",
2905 ctrl
->cnum
, portptr
->dev_loss_tmo
);
2906 WARN_ON(nvme_delete_ctrl(&ctrl
->ctrl
));
2911 __nvme_fc_terminate_io(struct nvme_fc_ctrl
*ctrl
)
2913 nvme_stop_keep_alive(&ctrl
->ctrl
);
2915 /* will block will waiting for io to terminate */
2916 nvme_fc_delete_association(ctrl
);
2918 if (ctrl
->ctrl
.state
!= NVME_CTRL_CONNECTING
&&
2919 !nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_CONNECTING
))
2920 dev_err(ctrl
->ctrl
.device
,
2921 "NVME-FC{%d}: error_recovery: Couldn't change state "
2922 "to CONNECTING\n", ctrl
->cnum
);
2926 nvme_fc_reset_ctrl_work(struct work_struct
*work
)
2928 struct nvme_fc_ctrl
*ctrl
=
2929 container_of(work
, struct nvme_fc_ctrl
, ctrl
.reset_work
);
2932 __nvme_fc_terminate_io(ctrl
);
2934 nvme_stop_ctrl(&ctrl
->ctrl
);
2936 if (ctrl
->rport
->remoteport
.port_state
== FC_OBJSTATE_ONLINE
)
2937 ret
= nvme_fc_create_association(ctrl
);
2942 nvme_fc_reconnect_or_delete(ctrl
, ret
);
2944 dev_info(ctrl
->ctrl
.device
,
2945 "NVME-FC{%d}: controller reset complete\n",
2950 nvme_fc_connect_err_work(struct work_struct
*work
)
2952 struct nvme_fc_ctrl
*ctrl
=
2953 container_of(work
, struct nvme_fc_ctrl
, err_work
);
2955 __nvme_fc_terminate_io(ctrl
);
2957 atomic_set(&ctrl
->err_work_active
, 0);
2960 * Rescheduling the connection after recovering
2961 * from the io error is left to the reconnect work
2962 * item, which is what should have stalled waiting on
2963 * the io that had the error that scheduled this work.
2967 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops
= {
2969 .module
= THIS_MODULE
,
2970 .flags
= NVME_F_FABRICS
,
2971 .reg_read32
= nvmf_reg_read32
,
2972 .reg_read64
= nvmf_reg_read64
,
2973 .reg_write32
= nvmf_reg_write32
,
2974 .free_ctrl
= nvme_fc_nvme_ctrl_freed
,
2975 .submit_async_event
= nvme_fc_submit_async_event
,
2976 .delete_ctrl
= nvme_fc_delete_ctrl
,
2977 .get_address
= nvmf_get_address
,
2981 nvme_fc_connect_ctrl_work(struct work_struct
*work
)
2985 struct nvme_fc_ctrl
*ctrl
=
2986 container_of(to_delayed_work(work
),
2987 struct nvme_fc_ctrl
, connect_work
);
2989 ret
= nvme_fc_create_association(ctrl
);
2991 nvme_fc_reconnect_or_delete(ctrl
, ret
);
2993 dev_info(ctrl
->ctrl
.device
,
2994 "NVME-FC{%d}: controller connect complete\n",
2999 static const struct blk_mq_ops nvme_fc_admin_mq_ops
= {
3000 .queue_rq
= nvme_fc_queue_rq
,
3001 .complete
= nvme_fc_complete_rq
,
3002 .init_request
= nvme_fc_init_request
,
3003 .exit_request
= nvme_fc_exit_request
,
3004 .init_hctx
= nvme_fc_init_admin_hctx
,
3005 .timeout
= nvme_fc_timeout
,
3010 * Fails a controller request if it matches an existing controller
3011 * (association) with the same tuple:
3012 * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
3014 * The ports don't need to be compared as they are intrinsically
3015 * already matched by the port pointers supplied.
3018 nvme_fc_existing_controller(struct nvme_fc_rport
*rport
,
3019 struct nvmf_ctrl_options
*opts
)
3021 struct nvme_fc_ctrl
*ctrl
;
3022 unsigned long flags
;
3025 spin_lock_irqsave(&rport
->lock
, flags
);
3026 list_for_each_entry(ctrl
, &rport
->ctrl_list
, ctrl_list
) {
3027 found
= nvmf_ctlr_matches_baseopts(&ctrl
->ctrl
, opts
);
3031 spin_unlock_irqrestore(&rport
->lock
, flags
);
3036 static struct nvme_ctrl
*
3037 nvme_fc_init_ctrl(struct device
*dev
, struct nvmf_ctrl_options
*opts
,
3038 struct nvme_fc_lport
*lport
, struct nvme_fc_rport
*rport
)
3040 struct nvme_fc_ctrl
*ctrl
;
3041 unsigned long flags
;
3044 if (!(rport
->remoteport
.port_role
&
3045 (FC_PORT_ROLE_NVME_DISCOVERY
| FC_PORT_ROLE_NVME_TARGET
))) {
3050 if (!opts
->duplicate_connect
&&
3051 nvme_fc_existing_controller(rport
, opts
)) {
3056 ctrl
= kzalloc(sizeof(*ctrl
), GFP_KERNEL
);
3062 idx
= ida_simple_get(&nvme_fc_ctrl_cnt
, 0, 0, GFP_KERNEL
);
3068 ctrl
->ctrl
.opts
= opts
;
3069 ctrl
->ctrl
.nr_reconnects
= 0;
3071 ctrl
->ctrl
.numa_node
= dev_to_node(lport
->dev
);
3073 ctrl
->ctrl
.numa_node
= NUMA_NO_NODE
;
3074 INIT_LIST_HEAD(&ctrl
->ctrl_list
);
3075 ctrl
->lport
= lport
;
3076 ctrl
->rport
= rport
;
3077 ctrl
->dev
= lport
->dev
;
3079 ctrl
->ioq_live
= false;
3080 ctrl
->assoc_active
= false;
3081 atomic_set(&ctrl
->err_work_active
, 0);
3082 init_waitqueue_head(&ctrl
->ioabort_wait
);
3084 get_device(ctrl
->dev
);
3085 kref_init(&ctrl
->ref
);
3087 INIT_WORK(&ctrl
->ctrl
.reset_work
, nvme_fc_reset_ctrl_work
);
3088 INIT_DELAYED_WORK(&ctrl
->connect_work
, nvme_fc_connect_ctrl_work
);
3089 INIT_WORK(&ctrl
->err_work
, nvme_fc_connect_err_work
);
3090 spin_lock_init(&ctrl
->lock
);
3092 /* io queue count */
3093 ctrl
->ctrl
.queue_count
= min_t(unsigned int,
3095 lport
->ops
->max_hw_queues
);
3096 ctrl
->ctrl
.queue_count
++; /* +1 for admin queue */
3098 ctrl
->ctrl
.sqsize
= opts
->queue_size
- 1;
3099 ctrl
->ctrl
.kato
= opts
->kato
;
3100 ctrl
->ctrl
.cntlid
= 0xffff;
3103 ctrl
->queues
= kcalloc(ctrl
->ctrl
.queue_count
,
3104 sizeof(struct nvme_fc_queue
), GFP_KERNEL
);
3108 nvme_fc_init_queue(ctrl
, 0);
3110 memset(&ctrl
->admin_tag_set
, 0, sizeof(ctrl
->admin_tag_set
));
3111 ctrl
->admin_tag_set
.ops
= &nvme_fc_admin_mq_ops
;
3112 ctrl
->admin_tag_set
.queue_depth
= NVME_AQ_MQ_TAG_DEPTH
;
3113 ctrl
->admin_tag_set
.reserved_tags
= 2; /* fabric connect + Keep-Alive */
3114 ctrl
->admin_tag_set
.numa_node
= ctrl
->ctrl
.numa_node
;
3115 ctrl
->admin_tag_set
.cmd_size
=
3116 struct_size((struct nvme_fcp_op_w_sgl
*)NULL
, priv
,
3117 ctrl
->lport
->ops
->fcprqst_priv_sz
);
3118 ctrl
->admin_tag_set
.driver_data
= ctrl
;
3119 ctrl
->admin_tag_set
.nr_hw_queues
= 1;
3120 ctrl
->admin_tag_set
.timeout
= ADMIN_TIMEOUT
;
3121 ctrl
->admin_tag_set
.flags
= BLK_MQ_F_NO_SCHED
;
3123 ret
= blk_mq_alloc_tag_set(&ctrl
->admin_tag_set
);
3125 goto out_free_queues
;
3126 ctrl
->ctrl
.admin_tagset
= &ctrl
->admin_tag_set
;
3128 ctrl
->ctrl
.fabrics_q
= blk_mq_init_queue(&ctrl
->admin_tag_set
);
3129 if (IS_ERR(ctrl
->ctrl
.fabrics_q
)) {
3130 ret
= PTR_ERR(ctrl
->ctrl
.fabrics_q
);
3131 goto out_free_admin_tag_set
;
3134 ctrl
->ctrl
.admin_q
= blk_mq_init_queue(&ctrl
->admin_tag_set
);
3135 if (IS_ERR(ctrl
->ctrl
.admin_q
)) {
3136 ret
= PTR_ERR(ctrl
->ctrl
.admin_q
);
3137 goto out_cleanup_fabrics_q
;
3141 * Would have been nice to init io queues tag set as well.
3142 * However, we require interaction from the controller
3143 * for max io queue count before we can do so.
3144 * Defer this to the connect path.
3147 ret
= nvme_init_ctrl(&ctrl
->ctrl
, dev
, &nvme_fc_ctrl_ops
, 0);
3149 goto out_cleanup_admin_q
;
3151 /* at this point, teardown path changes to ref counting on nvme ctrl */
3153 spin_lock_irqsave(&rport
->lock
, flags
);
3154 list_add_tail(&ctrl
->ctrl_list
, &rport
->ctrl_list
);
3155 spin_unlock_irqrestore(&rport
->lock
, flags
);
3157 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_RESETTING
) ||
3158 !nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_CONNECTING
)) {
3159 dev_err(ctrl
->ctrl
.device
,
3160 "NVME-FC{%d}: failed to init ctrl state\n", ctrl
->cnum
);
3164 nvme_get_ctrl(&ctrl
->ctrl
);
3166 if (!queue_delayed_work(nvme_wq
, &ctrl
->connect_work
, 0)) {
3167 nvme_put_ctrl(&ctrl
->ctrl
);
3168 dev_err(ctrl
->ctrl
.device
,
3169 "NVME-FC{%d}: failed to schedule initial connect\n",
3174 flush_delayed_work(&ctrl
->connect_work
);
3176 dev_info(ctrl
->ctrl
.device
,
3177 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
3178 ctrl
->cnum
, ctrl
->ctrl
.opts
->subsysnqn
);
3183 nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_DELETING
);
3184 cancel_work_sync(&ctrl
->ctrl
.reset_work
);
3185 cancel_work_sync(&ctrl
->err_work
);
3186 cancel_delayed_work_sync(&ctrl
->connect_work
);
3188 ctrl
->ctrl
.opts
= NULL
;
3190 /* initiate nvme ctrl ref counting teardown */
3191 nvme_uninit_ctrl(&ctrl
->ctrl
);
3193 /* Remove core ctrl ref. */
3194 nvme_put_ctrl(&ctrl
->ctrl
);
3196 /* as we're past the point where we transition to the ref
3197 * counting teardown path, if we return a bad pointer here,
3198 * the calling routine, thinking it's prior to the
3199 * transition, will do an rport put. Since the teardown
3200 * path also does a rport put, we do an extra get here to
3201 * so proper order/teardown happens.
3203 nvme_fc_rport_get(rport
);
3205 return ERR_PTR(-EIO
);
3207 out_cleanup_admin_q
:
3208 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
3209 out_cleanup_fabrics_q
:
3210 blk_cleanup_queue(ctrl
->ctrl
.fabrics_q
);
3211 out_free_admin_tag_set
:
3212 blk_mq_free_tag_set(&ctrl
->admin_tag_set
);
3214 kfree(ctrl
->queues
);
3216 put_device(ctrl
->dev
);
3217 ida_simple_remove(&nvme_fc_ctrl_cnt
, ctrl
->cnum
);
3221 /* exit via here doesn't follow ctlr ref points */
3222 return ERR_PTR(ret
);
3226 struct nvmet_fc_traddr
{
3232 __nvme_fc_parse_u64(substring_t
*sstr
, u64
*val
)
3236 if (match_u64(sstr
, &token64
))
3244 * This routine validates and extracts the WWN's from the TRADDR string.
3245 * As kernel parsers need the 0x to determine number base, universally
3246 * build string to parse with 0x prefix before parsing name strings.
3249 nvme_fc_parse_traddr(struct nvmet_fc_traddr
*traddr
, char *buf
, size_t blen
)
3251 char name
[2 + NVME_FC_TRADDR_HEXNAMELEN
+ 1];
3252 substring_t wwn
= { name
, &name
[sizeof(name
)-1] };
3253 int nnoffset
, pnoffset
;
3255 /* validate if string is one of the 2 allowed formats */
3256 if (strnlen(buf
, blen
) == NVME_FC_TRADDR_MAXLENGTH
&&
3257 !strncmp(buf
, "nn-0x", NVME_FC_TRADDR_OXNNLEN
) &&
3258 !strncmp(&buf
[NVME_FC_TRADDR_MAX_PN_OFFSET
],
3259 "pn-0x", NVME_FC_TRADDR_OXNNLEN
)) {
3260 nnoffset
= NVME_FC_TRADDR_OXNNLEN
;
3261 pnoffset
= NVME_FC_TRADDR_MAX_PN_OFFSET
+
3262 NVME_FC_TRADDR_OXNNLEN
;
3263 } else if ((strnlen(buf
, blen
) == NVME_FC_TRADDR_MINLENGTH
&&
3264 !strncmp(buf
, "nn-", NVME_FC_TRADDR_NNLEN
) &&
3265 !strncmp(&buf
[NVME_FC_TRADDR_MIN_PN_OFFSET
],
3266 "pn-", NVME_FC_TRADDR_NNLEN
))) {
3267 nnoffset
= NVME_FC_TRADDR_NNLEN
;
3268 pnoffset
= NVME_FC_TRADDR_MIN_PN_OFFSET
+ NVME_FC_TRADDR_NNLEN
;
3274 name
[2 + NVME_FC_TRADDR_HEXNAMELEN
] = 0;
3276 memcpy(&name
[2], &buf
[nnoffset
], NVME_FC_TRADDR_HEXNAMELEN
);
3277 if (__nvme_fc_parse_u64(&wwn
, &traddr
->nn
))
3280 memcpy(&name
[2], &buf
[pnoffset
], NVME_FC_TRADDR_HEXNAMELEN
);
3281 if (__nvme_fc_parse_u64(&wwn
, &traddr
->pn
))
3287 pr_warn("%s: bad traddr string\n", __func__
);
3291 static struct nvme_ctrl
*
3292 nvme_fc_create_ctrl(struct device
*dev
, struct nvmf_ctrl_options
*opts
)
3294 struct nvme_fc_lport
*lport
;
3295 struct nvme_fc_rport
*rport
;
3296 struct nvme_ctrl
*ctrl
;
3297 struct nvmet_fc_traddr laddr
= { 0L, 0L };
3298 struct nvmet_fc_traddr raddr
= { 0L, 0L };
3299 unsigned long flags
;
3302 ret
= nvme_fc_parse_traddr(&raddr
, opts
->traddr
, NVMF_TRADDR_SIZE
);
3303 if (ret
|| !raddr
.nn
|| !raddr
.pn
)
3304 return ERR_PTR(-EINVAL
);
3306 ret
= nvme_fc_parse_traddr(&laddr
, opts
->host_traddr
, NVMF_TRADDR_SIZE
);
3307 if (ret
|| !laddr
.nn
|| !laddr
.pn
)
3308 return ERR_PTR(-EINVAL
);
3310 /* find the host and remote ports to connect together */
3311 spin_lock_irqsave(&nvme_fc_lock
, flags
);
3312 list_for_each_entry(lport
, &nvme_fc_lport_list
, port_list
) {
3313 if (lport
->localport
.node_name
!= laddr
.nn
||
3314 lport
->localport
.port_name
!= laddr
.pn
)
3317 list_for_each_entry(rport
, &lport
->endp_list
, endp_list
) {
3318 if (rport
->remoteport
.node_name
!= raddr
.nn
||
3319 rport
->remoteport
.port_name
!= raddr
.pn
)
3322 /* if fail to get reference fall through. Will error */
3323 if (!nvme_fc_rport_get(rport
))
3326 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
3328 ctrl
= nvme_fc_init_ctrl(dev
, opts
, lport
, rport
);
3330 nvme_fc_rport_put(rport
);
3334 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
3336 pr_warn("%s: %s - %s combination not found\n",
3337 __func__
, opts
->traddr
, opts
->host_traddr
);
3338 return ERR_PTR(-ENOENT
);
3342 static struct nvmf_transport_ops nvme_fc_transport
= {
3344 .module
= THIS_MODULE
,
3345 .required_opts
= NVMF_OPT_TRADDR
| NVMF_OPT_HOST_TRADDR
,
3346 .allowed_opts
= NVMF_OPT_RECONNECT_DELAY
| NVMF_OPT_CTRL_LOSS_TMO
,
3347 .create_ctrl
= nvme_fc_create_ctrl
,
3350 /* Arbitrary successive failures max. With lots of subsystems could be high */
3351 #define DISCOVERY_MAX_FAIL 20
3353 static ssize_t
nvme_fc_nvme_discovery_store(struct device
*dev
,
3354 struct device_attribute
*attr
, const char *buf
, size_t count
)
3356 unsigned long flags
;
3357 LIST_HEAD(local_disc_list
);
3358 struct nvme_fc_lport
*lport
;
3359 struct nvme_fc_rport
*rport
;
3362 spin_lock_irqsave(&nvme_fc_lock
, flags
);
3364 list_for_each_entry(lport
, &nvme_fc_lport_list
, port_list
) {
3365 list_for_each_entry(rport
, &lport
->endp_list
, endp_list
) {
3366 if (!nvme_fc_lport_get(lport
))
3368 if (!nvme_fc_rport_get(rport
)) {
3370 * This is a temporary condition. Upon restart
3371 * this rport will be gone from the list.
3373 * Revert the lport put and retry. Anything
3374 * added to the list already will be skipped (as
3375 * they are no longer list_empty). Loops should
3376 * resume at rports that were not yet seen.
3378 nvme_fc_lport_put(lport
);
3380 if (failcnt
++ < DISCOVERY_MAX_FAIL
)
3383 pr_err("nvme_discovery: too many reference "
3385 goto process_local_list
;
3387 if (list_empty(&rport
->disc_list
))
3388 list_add_tail(&rport
->disc_list
,
3394 while (!list_empty(&local_disc_list
)) {
3395 rport
= list_first_entry(&local_disc_list
,
3396 struct nvme_fc_rport
, disc_list
);
3397 list_del_init(&rport
->disc_list
);
3398 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
3400 lport
= rport
->lport
;
3401 /* signal discovery. Won't hurt if it repeats */
3402 nvme_fc_signal_discovery_scan(lport
, rport
);
3403 nvme_fc_rport_put(rport
);
3404 nvme_fc_lport_put(lport
);
3406 spin_lock_irqsave(&nvme_fc_lock
, flags
);
3408 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
3412 static DEVICE_ATTR(nvme_discovery
, 0200, NULL
, nvme_fc_nvme_discovery_store
);
3414 static struct attribute
*nvme_fc_attrs
[] = {
3415 &dev_attr_nvme_discovery
.attr
,
3419 static struct attribute_group nvme_fc_attr_group
= {
3420 .attrs
= nvme_fc_attrs
,
3423 static const struct attribute_group
*nvme_fc_attr_groups
[] = {
3424 &nvme_fc_attr_group
,
3428 static struct class fc_class
= {
3430 .dev_groups
= nvme_fc_attr_groups
,
3431 .owner
= THIS_MODULE
,
3434 static int __init
nvme_fc_init_module(void)
3438 nvme_fc_wq
= alloc_workqueue("nvme_fc_wq", WQ_MEM_RECLAIM
, 0);
3444 * It is expected that in the future the kernel will combine
3445 * the FC-isms that are currently under scsi and now being
3446 * added to by NVME into a new standalone FC class. The SCSI
3447 * and NVME protocols and their devices would be under this
3450 * As we need something to post FC-specific udev events to,
3451 * specifically for nvme probe events, start by creating the
3452 * new device class. When the new standalone FC class is
3453 * put in place, this code will move to a more generic
3454 * location for the class.
3456 ret
= class_register(&fc_class
);
3458 pr_err("couldn't register class fc\n");
3459 goto out_destroy_wq
;
3463 * Create a device for the FC-centric udev events
3465 fc_udev_device
= device_create(&fc_class
, NULL
, MKDEV(0, 0), NULL
,
3467 if (IS_ERR(fc_udev_device
)) {
3468 pr_err("couldn't create fc_udev device!\n");
3469 ret
= PTR_ERR(fc_udev_device
);
3470 goto out_destroy_class
;
3473 ret
= nvmf_register_transport(&nvme_fc_transport
);
3475 goto out_destroy_device
;
3480 device_destroy(&fc_class
, MKDEV(0, 0));
3482 class_unregister(&fc_class
);
3484 destroy_workqueue(nvme_fc_wq
);
3490 nvme_fc_delete_controllers(struct nvme_fc_rport
*rport
)
3492 struct nvme_fc_ctrl
*ctrl
;
3494 spin_lock(&rport
->lock
);
3495 list_for_each_entry(ctrl
, &rport
->ctrl_list
, ctrl_list
) {
3496 dev_warn(ctrl
->ctrl
.device
,
3497 "NVME-FC{%d}: transport unloading: deleting ctrl\n",
3499 nvme_delete_ctrl(&ctrl
->ctrl
);
3501 spin_unlock(&rport
->lock
);
3505 nvme_fc_cleanup_for_unload(void)
3507 struct nvme_fc_lport
*lport
;
3508 struct nvme_fc_rport
*rport
;
3510 list_for_each_entry(lport
, &nvme_fc_lport_list
, port_list
) {
3511 list_for_each_entry(rport
, &lport
->endp_list
, endp_list
) {
3512 nvme_fc_delete_controllers(rport
);
3517 static void __exit
nvme_fc_exit_module(void)
3519 unsigned long flags
;
3520 bool need_cleanup
= false;
3522 spin_lock_irqsave(&nvme_fc_lock
, flags
);
3523 nvme_fc_waiting_to_unload
= true;
3524 if (!list_empty(&nvme_fc_lport_list
)) {
3525 need_cleanup
= true;
3526 nvme_fc_cleanup_for_unload();
3528 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
3530 pr_info("%s: waiting for ctlr deletes\n", __func__
);
3531 wait_for_completion(&nvme_fc_unload_proceed
);
3532 pr_info("%s: ctrl deletes complete\n", __func__
);
3535 nvmf_unregister_transport(&nvme_fc_transport
);
3537 ida_destroy(&nvme_fc_local_port_cnt
);
3538 ida_destroy(&nvme_fc_ctrl_cnt
);
3540 device_destroy(&fc_class
, MKDEV(0, 0));
3541 class_unregister(&fc_class
);
3542 destroy_workqueue(nvme_fc_wq
);
3545 module_init(nvme_fc_init_module
);
3546 module_exit(nvme_fc_exit_module
);
3548 MODULE_LICENSE("GPL v2");