2 * Copyright (c) 2016 Avago Technologies. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful.
9 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
10 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
11 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO
12 * THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
13 * See the GNU General Public License for more details, a copy of which
14 * can be found in the file COPYING included with this package
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/parser.h>
20 #include <uapi/scsi/fc/fc_fs.h>
21 #include <uapi/scsi/fc/fc_els.h>
22 #include <linux/delay.h>
26 #include <linux/nvme-fc-driver.h>
27 #include <linux/nvme-fc.h>
30 /* *************************** Data Structures/Defines ****************** */
33 enum nvme_fc_queue_flags
{
34 NVME_FC_Q_CONNECTED
= 0,
38 #define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */
40 struct nvme_fc_queue
{
41 struct nvme_fc_ctrl
*ctrl
;
43 struct blk_mq_hw_ctx
*hctx
;
45 size_t cmnd_capsule_len
;
54 } __aligned(sizeof(u64
)); /* alignment for other things alloc'd with */
56 enum nvme_fcop_flags
{
57 FCOP_FLAGS_TERMIO
= (1 << 0),
58 FCOP_FLAGS_AEN
= (1 << 1),
61 struct nvmefc_ls_req_op
{
62 struct nvmefc_ls_req ls_req
;
64 struct nvme_fc_rport
*rport
;
65 struct nvme_fc_queue
*queue
;
70 struct completion ls_done
;
71 struct list_head lsreq_list
; /* rport->ls_req_list */
75 enum nvme_fcpop_state
{
76 FCPOP_STATE_UNINIT
= 0,
78 FCPOP_STATE_ACTIVE
= 2,
79 FCPOP_STATE_ABORTED
= 3,
80 FCPOP_STATE_COMPLETE
= 4,
83 struct nvme_fc_fcp_op
{
84 struct nvme_request nreq
; /*
87 * the 1st element in the
92 struct nvmefc_fcp_req fcp_req
;
94 struct nvme_fc_ctrl
*ctrl
;
95 struct nvme_fc_queue
*queue
;
103 struct nvme_fc_cmd_iu cmd_iu
;
104 struct nvme_fc_ersp_iu rsp_iu
;
107 struct nvme_fc_lport
{
108 struct nvme_fc_local_port localport
;
111 struct list_head port_list
; /* nvme_fc_port_list */
112 struct list_head endp_list
;
113 struct device
*dev
; /* physical device for dma */
114 struct nvme_fc_port_template
*ops
;
116 atomic_t act_rport_cnt
;
117 } __aligned(sizeof(u64
)); /* alignment for other things alloc'd with */
119 struct nvme_fc_rport
{
120 struct nvme_fc_remote_port remoteport
;
122 struct list_head endp_list
; /* for lport->endp_list */
123 struct list_head ctrl_list
;
124 struct list_head ls_req_list
;
125 struct device
*dev
; /* physical device for dma */
126 struct nvme_fc_lport
*lport
;
129 atomic_t act_ctrl_cnt
;
130 unsigned long dev_loss_end
;
131 } __aligned(sizeof(u64
)); /* alignment for other things alloc'd with */
133 enum nvme_fcctrl_flags
{
134 FCCTRL_TERMIO
= (1 << 0),
137 struct nvme_fc_ctrl
{
139 struct nvme_fc_queue
*queues
;
141 struct nvme_fc_lport
*lport
;
142 struct nvme_fc_rport
*rport
;
148 struct list_head ctrl_list
; /* rport->ctrl_list */
150 struct blk_mq_tag_set admin_tag_set
;
151 struct blk_mq_tag_set tag_set
;
153 struct delayed_work connect_work
;
158 wait_queue_head_t ioabort_wait
;
160 struct nvme_fc_fcp_op aen_ops
[NVME_NR_AEN_COMMANDS
];
162 struct nvme_ctrl ctrl
;
165 static inline struct nvme_fc_ctrl
*
166 to_fc_ctrl(struct nvme_ctrl
*ctrl
)
168 return container_of(ctrl
, struct nvme_fc_ctrl
, ctrl
);
171 static inline struct nvme_fc_lport
*
172 localport_to_lport(struct nvme_fc_local_port
*portptr
)
174 return container_of(portptr
, struct nvme_fc_lport
, localport
);
177 static inline struct nvme_fc_rport
*
178 remoteport_to_rport(struct nvme_fc_remote_port
*portptr
)
180 return container_of(portptr
, struct nvme_fc_rport
, remoteport
);
183 static inline struct nvmefc_ls_req_op
*
184 ls_req_to_lsop(struct nvmefc_ls_req
*lsreq
)
186 return container_of(lsreq
, struct nvmefc_ls_req_op
, ls_req
);
189 static inline struct nvme_fc_fcp_op
*
190 fcp_req_to_fcp_op(struct nvmefc_fcp_req
*fcpreq
)
192 return container_of(fcpreq
, struct nvme_fc_fcp_op
, fcp_req
);
197 /* *************************** Globals **************************** */
200 static DEFINE_SPINLOCK(nvme_fc_lock
);
202 static LIST_HEAD(nvme_fc_lport_list
);
203 static DEFINE_IDA(nvme_fc_local_port_cnt
);
204 static DEFINE_IDA(nvme_fc_ctrl_cnt
);
209 * These items are short-term. They will eventually be moved into
210 * a generic FC class. See comments in module init.
212 static struct class *fc_class
;
213 static struct device
*fc_udev_device
;
216 /* *********************** FC-NVME Port Management ************************ */
218 static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl
*,
219 struct nvme_fc_queue
*, unsigned int);
222 nvme_fc_free_lport(struct kref
*ref
)
224 struct nvme_fc_lport
*lport
=
225 container_of(ref
, struct nvme_fc_lport
, ref
);
228 WARN_ON(lport
->localport
.port_state
!= FC_OBJSTATE_DELETED
);
229 WARN_ON(!list_empty(&lport
->endp_list
));
231 /* remove from transport list */
232 spin_lock_irqsave(&nvme_fc_lock
, flags
);
233 list_del(&lport
->port_list
);
234 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
236 ida_simple_remove(&nvme_fc_local_port_cnt
, lport
->localport
.port_num
);
237 ida_destroy(&lport
->endp_cnt
);
239 put_device(lport
->dev
);
245 nvme_fc_lport_put(struct nvme_fc_lport
*lport
)
247 kref_put(&lport
->ref
, nvme_fc_free_lport
);
251 nvme_fc_lport_get(struct nvme_fc_lport
*lport
)
253 return kref_get_unless_zero(&lport
->ref
);
257 static struct nvme_fc_lport
*
258 nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info
*pinfo
,
259 struct nvme_fc_port_template
*ops
,
262 struct nvme_fc_lport
*lport
;
265 spin_lock_irqsave(&nvme_fc_lock
, flags
);
267 list_for_each_entry(lport
, &nvme_fc_lport_list
, port_list
) {
268 if (lport
->localport
.node_name
!= pinfo
->node_name
||
269 lport
->localport
.port_name
!= pinfo
->port_name
)
272 if (lport
->dev
!= dev
) {
273 lport
= ERR_PTR(-EXDEV
);
277 if (lport
->localport
.port_state
!= FC_OBJSTATE_DELETED
) {
278 lport
= ERR_PTR(-EEXIST
);
282 if (!nvme_fc_lport_get(lport
)) {
284 * fails if ref cnt already 0. If so,
285 * act as if lport already deleted
291 /* resume the lport */
294 lport
->localport
.port_role
= pinfo
->port_role
;
295 lport
->localport
.port_id
= pinfo
->port_id
;
296 lport
->localport
.port_state
= FC_OBJSTATE_ONLINE
;
298 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
306 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
312 * nvme_fc_register_localport - transport entry point called by an
313 * LLDD to register the existence of a NVME
315 * @pinfo: pointer to information about the port to be registered
316 * @template: LLDD entrypoints and operational parameters for the port
317 * @dev: physical hardware device node port corresponds to. Will be
318 * used for DMA mappings
319 * @lport_p: pointer to a local port pointer. Upon success, the routine
320 * will allocate a nvme_fc_local_port structure and place its
321 * address in the local port pointer. Upon failure, local port
322 * pointer will be set to 0.
325 * a completion status. Must be 0 upon success; a negative errno
326 * (ex: -ENXIO) upon failure.
329 nvme_fc_register_localport(struct nvme_fc_port_info
*pinfo
,
330 struct nvme_fc_port_template
*template,
332 struct nvme_fc_local_port
**portptr
)
334 struct nvme_fc_lport
*newrec
;
338 if (!template->localport_delete
|| !template->remoteport_delete
||
339 !template->ls_req
|| !template->fcp_io
||
340 !template->ls_abort
|| !template->fcp_abort
||
341 !template->max_hw_queues
|| !template->max_sgl_segments
||
342 !template->max_dif_sgl_segments
|| !template->dma_boundary
) {
344 goto out_reghost_failed
;
348 * look to see if there is already a localport that had been
349 * deregistered and in the process of waiting for all the
350 * references to fully be removed. If the references haven't
351 * expired, we can simply re-enable the localport. Remoteports
352 * and controller reconnections should resume naturally.
354 newrec
= nvme_fc_attach_to_unreg_lport(pinfo
, template, dev
);
356 /* found an lport, but something about its state is bad */
357 if (IS_ERR(newrec
)) {
358 ret
= PTR_ERR(newrec
);
359 goto out_reghost_failed
;
361 /* found existing lport, which was resumed */
363 *portptr
= &newrec
->localport
;
367 /* nothing found - allocate a new localport struct */
369 newrec
= kmalloc((sizeof(*newrec
) + template->local_priv_sz
),
373 goto out_reghost_failed
;
376 idx
= ida_simple_get(&nvme_fc_local_port_cnt
, 0, 0, GFP_KERNEL
);
382 if (!get_device(dev
) && dev
) {
387 INIT_LIST_HEAD(&newrec
->port_list
);
388 INIT_LIST_HEAD(&newrec
->endp_list
);
389 kref_init(&newrec
->ref
);
390 atomic_set(&newrec
->act_rport_cnt
, 0);
391 newrec
->ops
= template;
393 ida_init(&newrec
->endp_cnt
);
394 newrec
->localport
.private = &newrec
[1];
395 newrec
->localport
.node_name
= pinfo
->node_name
;
396 newrec
->localport
.port_name
= pinfo
->port_name
;
397 newrec
->localport
.port_role
= pinfo
->port_role
;
398 newrec
->localport
.port_id
= pinfo
->port_id
;
399 newrec
->localport
.port_state
= FC_OBJSTATE_ONLINE
;
400 newrec
->localport
.port_num
= idx
;
402 spin_lock_irqsave(&nvme_fc_lock
, flags
);
403 list_add_tail(&newrec
->port_list
, &nvme_fc_lport_list
);
404 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
407 dma_set_seg_boundary(dev
, template->dma_boundary
);
409 *portptr
= &newrec
->localport
;
413 ida_simple_remove(&nvme_fc_local_port_cnt
, idx
);
421 EXPORT_SYMBOL_GPL(nvme_fc_register_localport
);
424 * nvme_fc_unregister_localport - transport entry point called by an
425 * LLDD to deregister/remove a previously
426 * registered a NVME host FC port.
427 * @localport: pointer to the (registered) local port that is to be
431 * a completion status. Must be 0 upon success; a negative errno
432 * (ex: -ENXIO) upon failure.
435 nvme_fc_unregister_localport(struct nvme_fc_local_port
*portptr
)
437 struct nvme_fc_lport
*lport
= localport_to_lport(portptr
);
443 spin_lock_irqsave(&nvme_fc_lock
, flags
);
445 if (portptr
->port_state
!= FC_OBJSTATE_ONLINE
) {
446 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
449 portptr
->port_state
= FC_OBJSTATE_DELETED
;
451 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
453 if (atomic_read(&lport
->act_rport_cnt
) == 0)
454 lport
->ops
->localport_delete(&lport
->localport
);
456 nvme_fc_lport_put(lport
);
460 EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport
);
463 * TRADDR strings, per FC-NVME are fixed format:
464 * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
465 * udev event will only differ by prefix of what field is
467 * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
468 * 19 + 43 + null_fudge = 64 characters
470 #define FCNVME_TRADDR_LENGTH 64
473 nvme_fc_signal_discovery_scan(struct nvme_fc_lport
*lport
,
474 struct nvme_fc_rport
*rport
)
476 char hostaddr
[FCNVME_TRADDR_LENGTH
]; /* NVMEFC_HOST_TRADDR=...*/
477 char tgtaddr
[FCNVME_TRADDR_LENGTH
]; /* NVMEFC_TRADDR=...*/
478 char *envp
[4] = { "FC_EVENT=nvmediscovery", hostaddr
, tgtaddr
, NULL
};
480 if (!(rport
->remoteport
.port_role
& FC_PORT_ROLE_NVME_DISCOVERY
))
483 snprintf(hostaddr
, sizeof(hostaddr
),
484 "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
485 lport
->localport
.node_name
, lport
->localport
.port_name
);
486 snprintf(tgtaddr
, sizeof(tgtaddr
),
487 "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
488 rport
->remoteport
.node_name
, rport
->remoteport
.port_name
);
489 kobject_uevent_env(&fc_udev_device
->kobj
, KOBJ_CHANGE
, envp
);
493 nvme_fc_free_rport(struct kref
*ref
)
495 struct nvme_fc_rport
*rport
=
496 container_of(ref
, struct nvme_fc_rport
, ref
);
497 struct nvme_fc_lport
*lport
=
498 localport_to_lport(rport
->remoteport
.localport
);
501 WARN_ON(rport
->remoteport
.port_state
!= FC_OBJSTATE_DELETED
);
502 WARN_ON(!list_empty(&rport
->ctrl_list
));
504 /* remove from lport list */
505 spin_lock_irqsave(&nvme_fc_lock
, flags
);
506 list_del(&rport
->endp_list
);
507 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
509 ida_simple_remove(&lport
->endp_cnt
, rport
->remoteport
.port_num
);
513 nvme_fc_lport_put(lport
);
517 nvme_fc_rport_put(struct nvme_fc_rport
*rport
)
519 kref_put(&rport
->ref
, nvme_fc_free_rport
);
523 nvme_fc_rport_get(struct nvme_fc_rport
*rport
)
525 return kref_get_unless_zero(&rport
->ref
);
529 nvme_fc_resume_controller(struct nvme_fc_ctrl
*ctrl
)
531 switch (ctrl
->ctrl
.state
) {
533 case NVME_CTRL_CONNECTING
:
535 * As all reconnects were suppressed, schedule a
538 dev_info(ctrl
->ctrl
.device
,
539 "NVME-FC{%d}: connectivity re-established. "
540 "Attempting reconnect\n", ctrl
->cnum
);
542 queue_delayed_work(nvme_wq
, &ctrl
->connect_work
, 0);
545 case NVME_CTRL_RESETTING
:
547 * Controller is already in the process of terminating the
548 * association. No need to do anything further. The reconnect
549 * step will naturally occur after the reset completes.
554 /* no action to take - let it delete */
559 static struct nvme_fc_rport
*
560 nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport
*lport
,
561 struct nvme_fc_port_info
*pinfo
)
563 struct nvme_fc_rport
*rport
;
564 struct nvme_fc_ctrl
*ctrl
;
567 spin_lock_irqsave(&nvme_fc_lock
, flags
);
569 list_for_each_entry(rport
, &lport
->endp_list
, endp_list
) {
570 if (rport
->remoteport
.node_name
!= pinfo
->node_name
||
571 rport
->remoteport
.port_name
!= pinfo
->port_name
)
574 if (!nvme_fc_rport_get(rport
)) {
575 rport
= ERR_PTR(-ENOLCK
);
579 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
581 spin_lock_irqsave(&rport
->lock
, flags
);
583 /* has it been unregistered */
584 if (rport
->remoteport
.port_state
!= FC_OBJSTATE_DELETED
) {
585 /* means lldd called us twice */
586 spin_unlock_irqrestore(&rport
->lock
, flags
);
587 nvme_fc_rport_put(rport
);
588 return ERR_PTR(-ESTALE
);
591 rport
->remoteport
.port_role
= pinfo
->port_role
;
592 rport
->remoteport
.port_id
= pinfo
->port_id
;
593 rport
->remoteport
.port_state
= FC_OBJSTATE_ONLINE
;
594 rport
->dev_loss_end
= 0;
597 * kick off a reconnect attempt on all associations to the
598 * remote port. A successful reconnects will resume i/o.
600 list_for_each_entry(ctrl
, &rport
->ctrl_list
, ctrl_list
)
601 nvme_fc_resume_controller(ctrl
);
603 spin_unlock_irqrestore(&rport
->lock
, flags
);
611 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
617 __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport
*rport
,
618 struct nvme_fc_port_info
*pinfo
)
620 if (pinfo
->dev_loss_tmo
)
621 rport
->remoteport
.dev_loss_tmo
= pinfo
->dev_loss_tmo
;
623 rport
->remoteport
.dev_loss_tmo
= NVME_FC_DEFAULT_DEV_LOSS_TMO
;
627 * nvme_fc_register_remoteport - transport entry point called by an
628 * LLDD to register the existence of a NVME
629 * subsystem FC port on its fabric.
630 * @localport: pointer to the (registered) local port that the remote
631 * subsystem port is connected to.
632 * @pinfo: pointer to information about the port to be registered
633 * @rport_p: pointer to a remote port pointer. Upon success, the routine
634 * will allocate a nvme_fc_remote_port structure and place its
635 * address in the remote port pointer. Upon failure, remote port
636 * pointer will be set to 0.
639 * a completion status. Must be 0 upon success; a negative errno
640 * (ex: -ENXIO) upon failure.
643 nvme_fc_register_remoteport(struct nvme_fc_local_port
*localport
,
644 struct nvme_fc_port_info
*pinfo
,
645 struct nvme_fc_remote_port
**portptr
)
647 struct nvme_fc_lport
*lport
= localport_to_lport(localport
);
648 struct nvme_fc_rport
*newrec
;
652 if (!nvme_fc_lport_get(lport
)) {
654 goto out_reghost_failed
;
658 * look to see if there is already a remoteport that is waiting
659 * for a reconnect (within dev_loss_tmo) with the same WWN's.
660 * If so, transition to it and reconnect.
662 newrec
= nvme_fc_attach_to_suspended_rport(lport
, pinfo
);
664 /* found an rport, but something about its state is bad */
665 if (IS_ERR(newrec
)) {
666 ret
= PTR_ERR(newrec
);
669 /* found existing rport, which was resumed */
671 nvme_fc_lport_put(lport
);
672 __nvme_fc_set_dev_loss_tmo(newrec
, pinfo
);
673 nvme_fc_signal_discovery_scan(lport
, newrec
);
674 *portptr
= &newrec
->remoteport
;
678 /* nothing found - allocate a new remoteport struct */
680 newrec
= kmalloc((sizeof(*newrec
) + lport
->ops
->remote_priv_sz
),
687 idx
= ida_simple_get(&lport
->endp_cnt
, 0, 0, GFP_KERNEL
);
690 goto out_kfree_rport
;
693 INIT_LIST_HEAD(&newrec
->endp_list
);
694 INIT_LIST_HEAD(&newrec
->ctrl_list
);
695 INIT_LIST_HEAD(&newrec
->ls_req_list
);
696 kref_init(&newrec
->ref
);
697 atomic_set(&newrec
->act_ctrl_cnt
, 0);
698 spin_lock_init(&newrec
->lock
);
699 newrec
->remoteport
.localport
= &lport
->localport
;
700 newrec
->dev
= lport
->dev
;
701 newrec
->lport
= lport
;
702 newrec
->remoteport
.private = &newrec
[1];
703 newrec
->remoteport
.port_role
= pinfo
->port_role
;
704 newrec
->remoteport
.node_name
= pinfo
->node_name
;
705 newrec
->remoteport
.port_name
= pinfo
->port_name
;
706 newrec
->remoteport
.port_id
= pinfo
->port_id
;
707 newrec
->remoteport
.port_state
= FC_OBJSTATE_ONLINE
;
708 newrec
->remoteport
.port_num
= idx
;
709 __nvme_fc_set_dev_loss_tmo(newrec
, pinfo
);
711 spin_lock_irqsave(&nvme_fc_lock
, flags
);
712 list_add_tail(&newrec
->endp_list
, &lport
->endp_list
);
713 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
715 nvme_fc_signal_discovery_scan(lport
, newrec
);
717 *portptr
= &newrec
->remoteport
;
723 nvme_fc_lport_put(lport
);
728 EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport
);
731 nvme_fc_abort_lsops(struct nvme_fc_rport
*rport
)
733 struct nvmefc_ls_req_op
*lsop
;
737 spin_lock_irqsave(&rport
->lock
, flags
);
739 list_for_each_entry(lsop
, &rport
->ls_req_list
, lsreq_list
) {
740 if (!(lsop
->flags
& FCOP_FLAGS_TERMIO
)) {
741 lsop
->flags
|= FCOP_FLAGS_TERMIO
;
742 spin_unlock_irqrestore(&rport
->lock
, flags
);
743 rport
->lport
->ops
->ls_abort(&rport
->lport
->localport
,
749 spin_unlock_irqrestore(&rport
->lock
, flags
);
755 nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl
*ctrl
)
757 dev_info(ctrl
->ctrl
.device
,
758 "NVME-FC{%d}: controller connectivity lost. Awaiting "
759 "Reconnect", ctrl
->cnum
);
761 switch (ctrl
->ctrl
.state
) {
765 * Schedule a controller reset. The reset will terminate the
766 * association and schedule the reconnect timer. Reconnects
767 * will be attempted until either the ctlr_loss_tmo
768 * (max_retries * connect_delay) expires or the remoteport's
769 * dev_loss_tmo expires.
771 if (nvme_reset_ctrl(&ctrl
->ctrl
)) {
772 dev_warn(ctrl
->ctrl
.device
,
773 "NVME-FC{%d}: Couldn't schedule reset.\n",
775 nvme_delete_ctrl(&ctrl
->ctrl
);
779 case NVME_CTRL_CONNECTING
:
781 * The association has already been terminated and the
782 * controller is attempting reconnects. No need to do anything
783 * futher. Reconnects will be attempted until either the
784 * ctlr_loss_tmo (max_retries * connect_delay) expires or the
785 * remoteport's dev_loss_tmo expires.
789 case NVME_CTRL_RESETTING
:
791 * Controller is already in the process of terminating the
792 * association. No need to do anything further. The reconnect
793 * step will kick in naturally after the association is
798 case NVME_CTRL_DELETING
:
800 /* no action to take - let it delete */
806 * nvme_fc_unregister_remoteport - transport entry point called by an
807 * LLDD to deregister/remove a previously
808 * registered a NVME subsystem FC port.
809 * @remoteport: pointer to the (registered) remote port that is to be
813 * a completion status. Must be 0 upon success; a negative errno
814 * (ex: -ENXIO) upon failure.
817 nvme_fc_unregister_remoteport(struct nvme_fc_remote_port
*portptr
)
819 struct nvme_fc_rport
*rport
= remoteport_to_rport(portptr
);
820 struct nvme_fc_ctrl
*ctrl
;
826 spin_lock_irqsave(&rport
->lock
, flags
);
828 if (portptr
->port_state
!= FC_OBJSTATE_ONLINE
) {
829 spin_unlock_irqrestore(&rport
->lock
, flags
);
832 portptr
->port_state
= FC_OBJSTATE_DELETED
;
834 rport
->dev_loss_end
= jiffies
+ (portptr
->dev_loss_tmo
* HZ
);
836 list_for_each_entry(ctrl
, &rport
->ctrl_list
, ctrl_list
) {
837 /* if dev_loss_tmo==0, dev loss is immediate */
838 if (!portptr
->dev_loss_tmo
) {
839 dev_warn(ctrl
->ctrl
.device
,
840 "NVME-FC{%d}: controller connectivity lost.\n",
842 nvme_delete_ctrl(&ctrl
->ctrl
);
844 nvme_fc_ctrl_connectivity_loss(ctrl
);
847 spin_unlock_irqrestore(&rport
->lock
, flags
);
849 nvme_fc_abort_lsops(rport
);
851 if (atomic_read(&rport
->act_ctrl_cnt
) == 0)
852 rport
->lport
->ops
->remoteport_delete(portptr
);
855 * release the reference, which will allow, if all controllers
856 * go away, which should only occur after dev_loss_tmo occurs,
857 * for the rport to be torn down.
859 nvme_fc_rport_put(rport
);
863 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport
);
866 * nvme_fc_rescan_remoteport - transport entry point called by an
867 * LLDD to request a nvme device rescan.
868 * @remoteport: pointer to the (registered) remote port that is to be
874 nvme_fc_rescan_remoteport(struct nvme_fc_remote_port
*remoteport
)
876 struct nvme_fc_rport
*rport
= remoteport_to_rport(remoteport
);
878 nvme_fc_signal_discovery_scan(rport
->lport
, rport
);
880 EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport
);
883 nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port
*portptr
,
886 struct nvme_fc_rport
*rport
= remoteport_to_rport(portptr
);
889 spin_lock_irqsave(&rport
->lock
, flags
);
891 if (portptr
->port_state
!= FC_OBJSTATE_ONLINE
) {
892 spin_unlock_irqrestore(&rport
->lock
, flags
);
896 /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
897 rport
->remoteport
.dev_loss_tmo
= dev_loss_tmo
;
899 spin_unlock_irqrestore(&rport
->lock
, flags
);
903 EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss
);
906 /* *********************** FC-NVME DMA Handling **************************** */
909 * The fcloop device passes in a NULL device pointer. Real LLD's will
910 * pass in a valid device pointer. If NULL is passed to the dma mapping
911 * routines, depending on the platform, it may or may not succeed, and
915 * Wrapper all the dma routines and check the dev pointer.
917 * If simple mappings (return just a dma address, we'll noop them,
918 * returning a dma address of 0.
920 * On more complex mappings (dma_map_sg), a pseudo routine fills
921 * in the scatter list, setting all dma addresses to 0.
924 static inline dma_addr_t
925 fc_dma_map_single(struct device
*dev
, void *ptr
, size_t size
,
926 enum dma_data_direction dir
)
928 return dev
? dma_map_single(dev
, ptr
, size
, dir
) : (dma_addr_t
)0L;
932 fc_dma_mapping_error(struct device
*dev
, dma_addr_t dma_addr
)
934 return dev
? dma_mapping_error(dev
, dma_addr
) : 0;
938 fc_dma_unmap_single(struct device
*dev
, dma_addr_t addr
, size_t size
,
939 enum dma_data_direction dir
)
942 dma_unmap_single(dev
, addr
, size
, dir
);
946 fc_dma_sync_single_for_cpu(struct device
*dev
, dma_addr_t addr
, size_t size
,
947 enum dma_data_direction dir
)
950 dma_sync_single_for_cpu(dev
, addr
, size
, dir
);
954 fc_dma_sync_single_for_device(struct device
*dev
, dma_addr_t addr
, size_t size
,
955 enum dma_data_direction dir
)
958 dma_sync_single_for_device(dev
, addr
, size
, dir
);
961 /* pseudo dma_map_sg call */
963 fc_map_sg(struct scatterlist
*sg
, int nents
)
965 struct scatterlist
*s
;
968 WARN_ON(nents
== 0 || sg
[0].length
== 0);
970 for_each_sg(sg
, s
, nents
, i
) {
972 #ifdef CONFIG_NEED_SG_DMA_LENGTH
973 s
->dma_length
= s
->length
;
980 fc_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
, int nents
,
981 enum dma_data_direction dir
)
983 return dev
? dma_map_sg(dev
, sg
, nents
, dir
) : fc_map_sg(sg
, nents
);
987 fc_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sg
, int nents
,
988 enum dma_data_direction dir
)
991 dma_unmap_sg(dev
, sg
, nents
, dir
);
994 /* *********************** FC-NVME LS Handling **************************** */
996 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl
*);
997 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl
*);
1001 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op
*lsop
)
1003 struct nvme_fc_rport
*rport
= lsop
->rport
;
1004 struct nvmefc_ls_req
*lsreq
= &lsop
->ls_req
;
1005 unsigned long flags
;
1007 spin_lock_irqsave(&rport
->lock
, flags
);
1009 if (!lsop
->req_queued
) {
1010 spin_unlock_irqrestore(&rport
->lock
, flags
);
1014 list_del(&lsop
->lsreq_list
);
1016 lsop
->req_queued
= false;
1018 spin_unlock_irqrestore(&rport
->lock
, flags
);
1020 fc_dma_unmap_single(rport
->dev
, lsreq
->rqstdma
,
1021 (lsreq
->rqstlen
+ lsreq
->rsplen
),
1024 nvme_fc_rport_put(rport
);
1028 __nvme_fc_send_ls_req(struct nvme_fc_rport
*rport
,
1029 struct nvmefc_ls_req_op
*lsop
,
1030 void (*done
)(struct nvmefc_ls_req
*req
, int status
))
1032 struct nvmefc_ls_req
*lsreq
= &lsop
->ls_req
;
1033 unsigned long flags
;
1036 if (rport
->remoteport
.port_state
!= FC_OBJSTATE_ONLINE
)
1037 return -ECONNREFUSED
;
1039 if (!nvme_fc_rport_get(rport
))
1043 lsop
->rport
= rport
;
1044 lsop
->req_queued
= false;
1045 INIT_LIST_HEAD(&lsop
->lsreq_list
);
1046 init_completion(&lsop
->ls_done
);
1048 lsreq
->rqstdma
= fc_dma_map_single(rport
->dev
, lsreq
->rqstaddr
,
1049 lsreq
->rqstlen
+ lsreq
->rsplen
,
1051 if (fc_dma_mapping_error(rport
->dev
, lsreq
->rqstdma
)) {
1055 lsreq
->rspdma
= lsreq
->rqstdma
+ lsreq
->rqstlen
;
1057 spin_lock_irqsave(&rport
->lock
, flags
);
1059 list_add_tail(&lsop
->lsreq_list
, &rport
->ls_req_list
);
1061 lsop
->req_queued
= true;
1063 spin_unlock_irqrestore(&rport
->lock
, flags
);
1065 ret
= rport
->lport
->ops
->ls_req(&rport
->lport
->localport
,
1066 &rport
->remoteport
, lsreq
);
1073 lsop
->ls_error
= ret
;
1074 spin_lock_irqsave(&rport
->lock
, flags
);
1075 lsop
->req_queued
= false;
1076 list_del(&lsop
->lsreq_list
);
1077 spin_unlock_irqrestore(&rport
->lock
, flags
);
1078 fc_dma_unmap_single(rport
->dev
, lsreq
->rqstdma
,
1079 (lsreq
->rqstlen
+ lsreq
->rsplen
),
1082 nvme_fc_rport_put(rport
);
1088 nvme_fc_send_ls_req_done(struct nvmefc_ls_req
*lsreq
, int status
)
1090 struct nvmefc_ls_req_op
*lsop
= ls_req_to_lsop(lsreq
);
1092 lsop
->ls_error
= status
;
1093 complete(&lsop
->ls_done
);
1097 nvme_fc_send_ls_req(struct nvme_fc_rport
*rport
, struct nvmefc_ls_req_op
*lsop
)
1099 struct nvmefc_ls_req
*lsreq
= &lsop
->ls_req
;
1100 struct fcnvme_ls_rjt
*rjt
= lsreq
->rspaddr
;
1103 ret
= __nvme_fc_send_ls_req(rport
, lsop
, nvme_fc_send_ls_req_done
);
1107 * No timeout/not interruptible as we need the struct
1108 * to exist until the lldd calls us back. Thus mandate
1109 * wait until driver calls back. lldd responsible for
1110 * the timeout action
1112 wait_for_completion(&lsop
->ls_done
);
1114 __nvme_fc_finish_ls_req(lsop
);
1116 ret
= lsop
->ls_error
;
1122 /* ACC or RJT payload ? */
1123 if (rjt
->w0
.ls_cmd
== FCNVME_LS_RJT
)
1130 nvme_fc_send_ls_req_async(struct nvme_fc_rport
*rport
,
1131 struct nvmefc_ls_req_op
*lsop
,
1132 void (*done
)(struct nvmefc_ls_req
*req
, int status
))
1134 /* don't wait for completion */
1136 return __nvme_fc_send_ls_req(rport
, lsop
, done
);
1139 /* Validation Error indexes into the string table below */
1143 VERR_LSDESC_RQST
= 2,
1144 VERR_LSDESC_RQST_LEN
= 3,
1146 VERR_ASSOC_ID_LEN
= 5,
1148 VERR_CONN_ID_LEN
= 7,
1150 VERR_CR_ASSOC_ACC_LEN
= 9,
1152 VERR_CR_CONN_ACC_LEN
= 11,
1154 VERR_DISCONN_ACC_LEN
= 13,
1157 static char *validation_errors
[] = {
1161 "Bad LSDESC_RQST Length",
1162 "Not Association ID",
1163 "Bad Association ID Length",
1164 "Not Connection ID",
1165 "Bad Connection ID Length",
1166 "Not CR_ASSOC Rqst",
1167 "Bad CR_ASSOC ACC Length",
1169 "Bad CR_CONN ACC Length",
1170 "Not Disconnect Rqst",
1171 "Bad Disconnect ACC Length",
1175 nvme_fc_connect_admin_queue(struct nvme_fc_ctrl
*ctrl
,
1176 struct nvme_fc_queue
*queue
, u16 qsize
, u16 ersp_ratio
)
1178 struct nvmefc_ls_req_op
*lsop
;
1179 struct nvmefc_ls_req
*lsreq
;
1180 struct fcnvme_ls_cr_assoc_rqst
*assoc_rqst
;
1181 struct fcnvme_ls_cr_assoc_acc
*assoc_acc
;
1184 lsop
= kzalloc((sizeof(*lsop
) +
1185 ctrl
->lport
->ops
->lsrqst_priv_sz
+
1186 sizeof(*assoc_rqst
) + sizeof(*assoc_acc
)), GFP_KERNEL
);
1191 lsreq
= &lsop
->ls_req
;
1193 lsreq
->private = (void *)&lsop
[1];
1194 assoc_rqst
= (struct fcnvme_ls_cr_assoc_rqst
*)
1195 (lsreq
->private + ctrl
->lport
->ops
->lsrqst_priv_sz
);
1196 assoc_acc
= (struct fcnvme_ls_cr_assoc_acc
*)&assoc_rqst
[1];
1198 assoc_rqst
->w0
.ls_cmd
= FCNVME_LS_CREATE_ASSOCIATION
;
1199 assoc_rqst
->desc_list_len
=
1200 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd
));
1202 assoc_rqst
->assoc_cmd
.desc_tag
=
1203 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD
);
1204 assoc_rqst
->assoc_cmd
.desc_len
=
1206 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd
));
1208 assoc_rqst
->assoc_cmd
.ersp_ratio
= cpu_to_be16(ersp_ratio
);
1209 assoc_rqst
->assoc_cmd
.sqsize
= cpu_to_be16(qsize
- 1);
1210 /* Linux supports only Dynamic controllers */
1211 assoc_rqst
->assoc_cmd
.cntlid
= cpu_to_be16(0xffff);
1212 uuid_copy(&assoc_rqst
->assoc_cmd
.hostid
, &ctrl
->ctrl
.opts
->host
->id
);
1213 strncpy(assoc_rqst
->assoc_cmd
.hostnqn
, ctrl
->ctrl
.opts
->host
->nqn
,
1214 min(FCNVME_ASSOC_HOSTNQN_LEN
, NVMF_NQN_SIZE
));
1215 strncpy(assoc_rqst
->assoc_cmd
.subnqn
, ctrl
->ctrl
.opts
->subsysnqn
,
1216 min(FCNVME_ASSOC_SUBNQN_LEN
, NVMF_NQN_SIZE
));
1218 lsop
->queue
= queue
;
1219 lsreq
->rqstaddr
= assoc_rqst
;
1220 lsreq
->rqstlen
= sizeof(*assoc_rqst
);
1221 lsreq
->rspaddr
= assoc_acc
;
1222 lsreq
->rsplen
= sizeof(*assoc_acc
);
1223 lsreq
->timeout
= NVME_FC_CONNECT_TIMEOUT_SEC
;
1225 ret
= nvme_fc_send_ls_req(ctrl
->rport
, lsop
);
1227 goto out_free_buffer
;
1229 /* process connect LS completion */
1231 /* validate the ACC response */
1232 if (assoc_acc
->hdr
.w0
.ls_cmd
!= FCNVME_LS_ACC
)
1234 else if (assoc_acc
->hdr
.desc_list_len
!=
1236 sizeof(struct fcnvme_ls_cr_assoc_acc
)))
1237 fcret
= VERR_CR_ASSOC_ACC_LEN
;
1238 else if (assoc_acc
->hdr
.rqst
.desc_tag
!=
1239 cpu_to_be32(FCNVME_LSDESC_RQST
))
1240 fcret
= VERR_LSDESC_RQST
;
1241 else if (assoc_acc
->hdr
.rqst
.desc_len
!=
1242 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst
)))
1243 fcret
= VERR_LSDESC_RQST_LEN
;
1244 else if (assoc_acc
->hdr
.rqst
.w0
.ls_cmd
!= FCNVME_LS_CREATE_ASSOCIATION
)
1245 fcret
= VERR_CR_ASSOC
;
1246 else if (assoc_acc
->associd
.desc_tag
!=
1247 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID
))
1248 fcret
= VERR_ASSOC_ID
;
1249 else if (assoc_acc
->associd
.desc_len
!=
1251 sizeof(struct fcnvme_lsdesc_assoc_id
)))
1252 fcret
= VERR_ASSOC_ID_LEN
;
1253 else if (assoc_acc
->connectid
.desc_tag
!=
1254 cpu_to_be32(FCNVME_LSDESC_CONN_ID
))
1255 fcret
= VERR_CONN_ID
;
1256 else if (assoc_acc
->connectid
.desc_len
!=
1257 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id
)))
1258 fcret
= VERR_CONN_ID_LEN
;
1263 "q %d connect failed: %s\n",
1264 queue
->qnum
, validation_errors
[fcret
]);
1266 ctrl
->association_id
=
1267 be64_to_cpu(assoc_acc
->associd
.association_id
);
1268 queue
->connection_id
=
1269 be64_to_cpu(assoc_acc
->connectid
.connection_id
);
1270 set_bit(NVME_FC_Q_CONNECTED
, &queue
->flags
);
1278 "queue %d connect admin queue failed (%d).\n",
1284 nvme_fc_connect_queue(struct nvme_fc_ctrl
*ctrl
, struct nvme_fc_queue
*queue
,
1285 u16 qsize
, u16 ersp_ratio
)
1287 struct nvmefc_ls_req_op
*lsop
;
1288 struct nvmefc_ls_req
*lsreq
;
1289 struct fcnvme_ls_cr_conn_rqst
*conn_rqst
;
1290 struct fcnvme_ls_cr_conn_acc
*conn_acc
;
1293 lsop
= kzalloc((sizeof(*lsop
) +
1294 ctrl
->lport
->ops
->lsrqst_priv_sz
+
1295 sizeof(*conn_rqst
) + sizeof(*conn_acc
)), GFP_KERNEL
);
1300 lsreq
= &lsop
->ls_req
;
1302 lsreq
->private = (void *)&lsop
[1];
1303 conn_rqst
= (struct fcnvme_ls_cr_conn_rqst
*)
1304 (lsreq
->private + ctrl
->lport
->ops
->lsrqst_priv_sz
);
1305 conn_acc
= (struct fcnvme_ls_cr_conn_acc
*)&conn_rqst
[1];
1307 conn_rqst
->w0
.ls_cmd
= FCNVME_LS_CREATE_CONNECTION
;
1308 conn_rqst
->desc_list_len
= cpu_to_be32(
1309 sizeof(struct fcnvme_lsdesc_assoc_id
) +
1310 sizeof(struct fcnvme_lsdesc_cr_conn_cmd
));
1312 conn_rqst
->associd
.desc_tag
= cpu_to_be32(FCNVME_LSDESC_ASSOC_ID
);
1313 conn_rqst
->associd
.desc_len
=
1315 sizeof(struct fcnvme_lsdesc_assoc_id
));
1316 conn_rqst
->associd
.association_id
= cpu_to_be64(ctrl
->association_id
);
1317 conn_rqst
->connect_cmd
.desc_tag
=
1318 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD
);
1319 conn_rqst
->connect_cmd
.desc_len
=
1321 sizeof(struct fcnvme_lsdesc_cr_conn_cmd
));
1322 conn_rqst
->connect_cmd
.ersp_ratio
= cpu_to_be16(ersp_ratio
);
1323 conn_rqst
->connect_cmd
.qid
= cpu_to_be16(queue
->qnum
);
1324 conn_rqst
->connect_cmd
.sqsize
= cpu_to_be16(qsize
- 1);
1326 lsop
->queue
= queue
;
1327 lsreq
->rqstaddr
= conn_rqst
;
1328 lsreq
->rqstlen
= sizeof(*conn_rqst
);
1329 lsreq
->rspaddr
= conn_acc
;
1330 lsreq
->rsplen
= sizeof(*conn_acc
);
1331 lsreq
->timeout
= NVME_FC_CONNECT_TIMEOUT_SEC
;
1333 ret
= nvme_fc_send_ls_req(ctrl
->rport
, lsop
);
1335 goto out_free_buffer
;
1337 /* process connect LS completion */
1339 /* validate the ACC response */
1340 if (conn_acc
->hdr
.w0
.ls_cmd
!= FCNVME_LS_ACC
)
1342 else if (conn_acc
->hdr
.desc_list_len
!=
1343 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc
)))
1344 fcret
= VERR_CR_CONN_ACC_LEN
;
1345 else if (conn_acc
->hdr
.rqst
.desc_tag
!= cpu_to_be32(FCNVME_LSDESC_RQST
))
1346 fcret
= VERR_LSDESC_RQST
;
1347 else if (conn_acc
->hdr
.rqst
.desc_len
!=
1348 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst
)))
1349 fcret
= VERR_LSDESC_RQST_LEN
;
1350 else if (conn_acc
->hdr
.rqst
.w0
.ls_cmd
!= FCNVME_LS_CREATE_CONNECTION
)
1351 fcret
= VERR_CR_CONN
;
1352 else if (conn_acc
->connectid
.desc_tag
!=
1353 cpu_to_be32(FCNVME_LSDESC_CONN_ID
))
1354 fcret
= VERR_CONN_ID
;
1355 else if (conn_acc
->connectid
.desc_len
!=
1356 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id
)))
1357 fcret
= VERR_CONN_ID_LEN
;
1362 "q %d connect failed: %s\n",
1363 queue
->qnum
, validation_errors
[fcret
]);
1365 queue
->connection_id
=
1366 be64_to_cpu(conn_acc
->connectid
.connection_id
);
1367 set_bit(NVME_FC_Q_CONNECTED
, &queue
->flags
);
1375 "queue %d connect command failed (%d).\n",
1381 nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req
*lsreq
, int status
)
1383 struct nvmefc_ls_req_op
*lsop
= ls_req_to_lsop(lsreq
);
1385 __nvme_fc_finish_ls_req(lsop
);
1387 /* fc-nvme iniator doesn't care about success or failure of cmd */
1393 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1394 * the FC-NVME Association. Terminating the association also
1395 * terminates the FC-NVME connections (per queue, both admin and io
1396 * queues) that are part of the association. E.g. things are torn
1397 * down, and the related FC-NVME Association ID and Connection IDs
1400 * The behavior of the fc-nvme initiator is such that it's
1401 * understanding of the association and connections will implicitly
1402 * be torn down. The action is implicit as it may be due to a loss of
1403 * connectivity with the fc-nvme target, so you may never get a
1404 * response even if you tried. As such, the action of this routine
1405 * is to asynchronously send the LS, ignore any results of the LS, and
1406 * continue on with terminating the association. If the fc-nvme target
1407 * is present and receives the LS, it too can tear down.
1410 nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl
*ctrl
)
1412 struct fcnvme_ls_disconnect_rqst
*discon_rqst
;
1413 struct fcnvme_ls_disconnect_acc
*discon_acc
;
1414 struct nvmefc_ls_req_op
*lsop
;
1415 struct nvmefc_ls_req
*lsreq
;
1418 lsop
= kzalloc((sizeof(*lsop
) +
1419 ctrl
->lport
->ops
->lsrqst_priv_sz
+
1420 sizeof(*discon_rqst
) + sizeof(*discon_acc
)),
1423 /* couldn't sent it... too bad */
1426 lsreq
= &lsop
->ls_req
;
1428 lsreq
->private = (void *)&lsop
[1];
1429 discon_rqst
= (struct fcnvme_ls_disconnect_rqst
*)
1430 (lsreq
->private + ctrl
->lport
->ops
->lsrqst_priv_sz
);
1431 discon_acc
= (struct fcnvme_ls_disconnect_acc
*)&discon_rqst
[1];
1433 discon_rqst
->w0
.ls_cmd
= FCNVME_LS_DISCONNECT
;
1434 discon_rqst
->desc_list_len
= cpu_to_be32(
1435 sizeof(struct fcnvme_lsdesc_assoc_id
) +
1436 sizeof(struct fcnvme_lsdesc_disconn_cmd
));
1438 discon_rqst
->associd
.desc_tag
= cpu_to_be32(FCNVME_LSDESC_ASSOC_ID
);
1439 discon_rqst
->associd
.desc_len
=
1441 sizeof(struct fcnvme_lsdesc_assoc_id
));
1443 discon_rqst
->associd
.association_id
= cpu_to_be64(ctrl
->association_id
);
1445 discon_rqst
->discon_cmd
.desc_tag
= cpu_to_be32(
1446 FCNVME_LSDESC_DISCONN_CMD
);
1447 discon_rqst
->discon_cmd
.desc_len
=
1449 sizeof(struct fcnvme_lsdesc_disconn_cmd
));
1450 discon_rqst
->discon_cmd
.scope
= FCNVME_DISCONN_ASSOCIATION
;
1451 discon_rqst
->discon_cmd
.id
= cpu_to_be64(ctrl
->association_id
);
1453 lsreq
->rqstaddr
= discon_rqst
;
1454 lsreq
->rqstlen
= sizeof(*discon_rqst
);
1455 lsreq
->rspaddr
= discon_acc
;
1456 lsreq
->rsplen
= sizeof(*discon_acc
);
1457 lsreq
->timeout
= NVME_FC_CONNECT_TIMEOUT_SEC
;
1459 ret
= nvme_fc_send_ls_req_async(ctrl
->rport
, lsop
,
1460 nvme_fc_disconnect_assoc_done
);
1464 /* only meaningful part to terminating the association */
1465 ctrl
->association_id
= 0;
1469 /* *********************** NVME Ctrl Routines **************************** */
1471 static void nvme_fc_error_recovery(struct nvme_fc_ctrl
*ctrl
, char *errmsg
);
1474 nvme_fc_reinit_request(void *data
, struct request
*rq
)
1476 struct nvme_fc_fcp_op
*op
= blk_mq_rq_to_pdu(rq
);
1477 struct nvme_fc_cmd_iu
*cmdiu
= &op
->cmd_iu
;
1479 memset(cmdiu
, 0, sizeof(*cmdiu
));
1480 cmdiu
->scsi_id
= NVME_CMD_SCSI_ID
;
1481 cmdiu
->fc_id
= NVME_CMD_FC_ID
;
1482 cmdiu
->iu_len
= cpu_to_be16(sizeof(*cmdiu
) / sizeof(u32
));
1483 memset(&op
->rsp_iu
, 0, sizeof(op
->rsp_iu
));
1489 __nvme_fc_exit_request(struct nvme_fc_ctrl
*ctrl
,
1490 struct nvme_fc_fcp_op
*op
)
1492 fc_dma_unmap_single(ctrl
->lport
->dev
, op
->fcp_req
.rspdma
,
1493 sizeof(op
->rsp_iu
), DMA_FROM_DEVICE
);
1494 fc_dma_unmap_single(ctrl
->lport
->dev
, op
->fcp_req
.cmddma
,
1495 sizeof(op
->cmd_iu
), DMA_TO_DEVICE
);
1497 atomic_set(&op
->state
, FCPOP_STATE_UNINIT
);
1501 nvme_fc_exit_request(struct blk_mq_tag_set
*set
, struct request
*rq
,
1502 unsigned int hctx_idx
)
1504 struct nvme_fc_fcp_op
*op
= blk_mq_rq_to_pdu(rq
);
1506 return __nvme_fc_exit_request(set
->driver_data
, op
);
1510 __nvme_fc_abort_op(struct nvme_fc_ctrl
*ctrl
, struct nvme_fc_fcp_op
*op
)
1512 unsigned long flags
;
1515 spin_lock_irqsave(&ctrl
->lock
, flags
);
1516 opstate
= atomic_xchg(&op
->state
, FCPOP_STATE_ABORTED
);
1517 if (opstate
!= FCPOP_STATE_ACTIVE
)
1518 atomic_set(&op
->state
, opstate
);
1519 else if (ctrl
->flags
& FCCTRL_TERMIO
)
1521 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
1523 if (opstate
!= FCPOP_STATE_ACTIVE
)
1526 ctrl
->lport
->ops
->fcp_abort(&ctrl
->lport
->localport
,
1527 &ctrl
->rport
->remoteport
,
1528 op
->queue
->lldd_handle
,
1535 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl
*ctrl
)
1537 struct nvme_fc_fcp_op
*aen_op
= ctrl
->aen_ops
;
1540 for (i
= 0; i
< NVME_NR_AEN_COMMANDS
; i
++, aen_op
++)
1541 __nvme_fc_abort_op(ctrl
, aen_op
);
1545 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl
*ctrl
,
1546 struct nvme_fc_fcp_op
*op
, int opstate
)
1548 unsigned long flags
;
1550 if (opstate
== FCPOP_STATE_ABORTED
) {
1551 spin_lock_irqsave(&ctrl
->lock
, flags
);
1552 if (ctrl
->flags
& FCCTRL_TERMIO
) {
1554 wake_up(&ctrl
->ioabort_wait
);
1556 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
1561 nvme_fc_fcpio_done(struct nvmefc_fcp_req
*req
)
1563 struct nvme_fc_fcp_op
*op
= fcp_req_to_fcp_op(req
);
1564 struct request
*rq
= op
->rq
;
1565 struct nvmefc_fcp_req
*freq
= &op
->fcp_req
;
1566 struct nvme_fc_ctrl
*ctrl
= op
->ctrl
;
1567 struct nvme_fc_queue
*queue
= op
->queue
;
1568 struct nvme_completion
*cqe
= &op
->rsp_iu
.cqe
;
1569 struct nvme_command
*sqe
= &op
->cmd_iu
.sqe
;
1570 __le16 status
= cpu_to_le16(NVME_SC_SUCCESS
<< 1);
1571 union nvme_result result
;
1572 bool terminate_assoc
= true;
1577 * The current linux implementation of a nvme controller
1578 * allocates a single tag set for all io queues and sizes
1579 * the io queues to fully hold all possible tags. Thus, the
1580 * implementation does not reference or care about the sqhd
1581 * value as it never needs to use the sqhd/sqtail pointers
1582 * for submission pacing.
1584 * This affects the FC-NVME implementation in two ways:
1585 * 1) As the value doesn't matter, we don't need to waste
1586 * cycles extracting it from ERSPs and stamping it in the
1587 * cases where the transport fabricates CQEs on successful
1589 * 2) The FC-NVME implementation requires that delivery of
1590 * ERSP completions are to go back to the nvme layer in order
1591 * relative to the rsn, such that the sqhd value will always
1592 * be "in order" for the nvme layer. As the nvme layer in
1593 * linux doesn't care about sqhd, there's no need to return
1597 * As the core nvme layer in linux currently does not look at
1598 * every field in the cqe - in cases where the FC transport must
1599 * fabricate a CQE, the following fields will not be set as they
1600 * are not referenced:
1601 * cqe.sqid, cqe.sqhd, cqe.command_id
1603 * Failure or error of an individual i/o, in a transport
1604 * detected fashion unrelated to the nvme completion status,
1605 * potentially cause the initiator and target sides to get out
1606 * of sync on SQ head/tail (aka outstanding io count allowed).
1607 * Per FC-NVME spec, failure of an individual command requires
1608 * the connection to be terminated, which in turn requires the
1609 * association to be terminated.
1612 opstate
= atomic_xchg(&op
->state
, FCPOP_STATE_COMPLETE
);
1614 fc_dma_sync_single_for_cpu(ctrl
->lport
->dev
, op
->fcp_req
.rspdma
,
1615 sizeof(op
->rsp_iu
), DMA_FROM_DEVICE
);
1617 if (opstate
== FCPOP_STATE_ABORTED
)
1618 status
= cpu_to_le16(NVME_SC_ABORT_REQ
<< 1);
1619 else if (freq
->status
)
1620 status
= cpu_to_le16(NVME_SC_INTERNAL
<< 1);
1623 * For the linux implementation, if we have an unsuccesful
1624 * status, they blk-mq layer can typically be called with the
1625 * non-zero status and the content of the cqe isn't important.
1631 * command completed successfully relative to the wire
1632 * protocol. However, validate anything received and
1633 * extract the status and result from the cqe (create it
1637 switch (freq
->rcv_rsplen
) {
1640 case NVME_FC_SIZEOF_ZEROS_RSP
:
1642 * No response payload or 12 bytes of payload (which
1643 * should all be zeros) are considered successful and
1644 * no payload in the CQE by the transport.
1646 if (freq
->transferred_length
!=
1647 be32_to_cpu(op
->cmd_iu
.data_len
)) {
1648 status
= cpu_to_le16(NVME_SC_INTERNAL
<< 1);
1654 case sizeof(struct nvme_fc_ersp_iu
):
1656 * The ERSP IU contains a full completion with CQE.
1657 * Validate ERSP IU and look at cqe.
1659 if (unlikely(be16_to_cpu(op
->rsp_iu
.iu_len
) !=
1660 (freq
->rcv_rsplen
/ 4) ||
1661 be32_to_cpu(op
->rsp_iu
.xfrd_len
) !=
1662 freq
->transferred_length
||
1663 op
->rsp_iu
.status_code
||
1664 sqe
->common
.command_id
!= cqe
->command_id
)) {
1665 status
= cpu_to_le16(NVME_SC_INTERNAL
<< 1);
1668 result
= cqe
->result
;
1669 status
= cqe
->status
;
1673 status
= cpu_to_le16(NVME_SC_INTERNAL
<< 1);
1677 terminate_assoc
= false;
1680 if (op
->flags
& FCOP_FLAGS_AEN
) {
1681 nvme_complete_async_event(&queue
->ctrl
->ctrl
, status
, &result
);
1682 __nvme_fc_fcpop_chk_teardowns(ctrl
, op
, opstate
);
1683 atomic_set(&op
->state
, FCPOP_STATE_IDLE
);
1684 op
->flags
= FCOP_FLAGS_AEN
; /* clear other flags */
1685 nvme_fc_ctrl_put(ctrl
);
1690 * Force failures of commands if we're killing the controller
1691 * or have an error on a command used to create an new association
1694 (blk_queue_dying(rq
->q
) ||
1695 ctrl
->ctrl
.state
== NVME_CTRL_NEW
||
1696 ctrl
->ctrl
.state
== NVME_CTRL_CONNECTING
))
1697 status
|= cpu_to_le16(NVME_SC_DNR
<< 1);
1699 __nvme_fc_fcpop_chk_teardowns(ctrl
, op
, opstate
);
1700 nvme_end_request(rq
, status
, result
);
1703 if (terminate_assoc
)
1704 nvme_fc_error_recovery(ctrl
, "transport detected io error");
1708 __nvme_fc_init_request(struct nvme_fc_ctrl
*ctrl
,
1709 struct nvme_fc_queue
*queue
, struct nvme_fc_fcp_op
*op
,
1710 struct request
*rq
, u32 rqno
)
1712 struct nvme_fc_cmd_iu
*cmdiu
= &op
->cmd_iu
;
1715 memset(op
, 0, sizeof(*op
));
1716 op
->fcp_req
.cmdaddr
= &op
->cmd_iu
;
1717 op
->fcp_req
.cmdlen
= sizeof(op
->cmd_iu
);
1718 op
->fcp_req
.rspaddr
= &op
->rsp_iu
;
1719 op
->fcp_req
.rsplen
= sizeof(op
->rsp_iu
);
1720 op
->fcp_req
.done
= nvme_fc_fcpio_done
;
1721 op
->fcp_req
.first_sgl
= (struct scatterlist
*)&op
[1];
1722 op
->fcp_req
.private = &op
->fcp_req
.first_sgl
[SG_CHUNK_SIZE
];
1728 cmdiu
->scsi_id
= NVME_CMD_SCSI_ID
;
1729 cmdiu
->fc_id
= NVME_CMD_FC_ID
;
1730 cmdiu
->iu_len
= cpu_to_be16(sizeof(*cmdiu
) / sizeof(u32
));
1732 op
->fcp_req
.cmddma
= fc_dma_map_single(ctrl
->lport
->dev
,
1733 &op
->cmd_iu
, sizeof(op
->cmd_iu
), DMA_TO_DEVICE
);
1734 if (fc_dma_mapping_error(ctrl
->lport
->dev
, op
->fcp_req
.cmddma
)) {
1736 "FCP Op failed - cmdiu dma mapping failed.\n");
1741 op
->fcp_req
.rspdma
= fc_dma_map_single(ctrl
->lport
->dev
,
1742 &op
->rsp_iu
, sizeof(op
->rsp_iu
),
1744 if (fc_dma_mapping_error(ctrl
->lport
->dev
, op
->fcp_req
.rspdma
)) {
1746 "FCP Op failed - rspiu dma mapping failed.\n");
1750 atomic_set(&op
->state
, FCPOP_STATE_IDLE
);
1756 nvme_fc_init_request(struct blk_mq_tag_set
*set
, struct request
*rq
,
1757 unsigned int hctx_idx
, unsigned int numa_node
)
1759 struct nvme_fc_ctrl
*ctrl
= set
->driver_data
;
1760 struct nvme_fc_fcp_op
*op
= blk_mq_rq_to_pdu(rq
);
1761 int queue_idx
= (set
== &ctrl
->tag_set
) ? hctx_idx
+ 1 : 0;
1762 struct nvme_fc_queue
*queue
= &ctrl
->queues
[queue_idx
];
1764 return __nvme_fc_init_request(ctrl
, queue
, op
, rq
, queue
->rqcnt
++);
1768 nvme_fc_init_aen_ops(struct nvme_fc_ctrl
*ctrl
)
1770 struct nvme_fc_fcp_op
*aen_op
;
1771 struct nvme_fc_cmd_iu
*cmdiu
;
1772 struct nvme_command
*sqe
;
1776 aen_op
= ctrl
->aen_ops
;
1777 for (i
= 0; i
< NVME_NR_AEN_COMMANDS
; i
++, aen_op
++) {
1778 private = kzalloc(ctrl
->lport
->ops
->fcprqst_priv_sz
,
1783 cmdiu
= &aen_op
->cmd_iu
;
1785 ret
= __nvme_fc_init_request(ctrl
, &ctrl
->queues
[0],
1786 aen_op
, (struct request
*)NULL
,
1787 (NVME_AQ_BLK_MQ_DEPTH
+ i
));
1793 aen_op
->flags
= FCOP_FLAGS_AEN
;
1794 aen_op
->fcp_req
.first_sgl
= NULL
; /* no sg list */
1795 aen_op
->fcp_req
.private = private;
1797 memset(sqe
, 0, sizeof(*sqe
));
1798 sqe
->common
.opcode
= nvme_admin_async_event
;
1799 /* Note: core layer may overwrite the sqe.command_id value */
1800 sqe
->common
.command_id
= NVME_AQ_BLK_MQ_DEPTH
+ i
;
1806 nvme_fc_term_aen_ops(struct nvme_fc_ctrl
*ctrl
)
1808 struct nvme_fc_fcp_op
*aen_op
;
1811 aen_op
= ctrl
->aen_ops
;
1812 for (i
= 0; i
< NVME_NR_AEN_COMMANDS
; i
++, aen_op
++) {
1813 if (!aen_op
->fcp_req
.private)
1816 __nvme_fc_exit_request(ctrl
, aen_op
);
1818 kfree(aen_op
->fcp_req
.private);
1819 aen_op
->fcp_req
.private = NULL
;
1824 __nvme_fc_init_hctx(struct blk_mq_hw_ctx
*hctx
, struct nvme_fc_ctrl
*ctrl
,
1827 struct nvme_fc_queue
*queue
= &ctrl
->queues
[qidx
];
1829 hctx
->driver_data
= queue
;
1834 nvme_fc_init_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
1835 unsigned int hctx_idx
)
1837 struct nvme_fc_ctrl
*ctrl
= data
;
1839 __nvme_fc_init_hctx(hctx
, ctrl
, hctx_idx
+ 1);
1845 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
1846 unsigned int hctx_idx
)
1848 struct nvme_fc_ctrl
*ctrl
= data
;
1850 __nvme_fc_init_hctx(hctx
, ctrl
, hctx_idx
);
1856 nvme_fc_init_queue(struct nvme_fc_ctrl
*ctrl
, int idx
)
1858 struct nvme_fc_queue
*queue
;
1860 queue
= &ctrl
->queues
[idx
];
1861 memset(queue
, 0, sizeof(*queue
));
1864 atomic_set(&queue
->csn
, 1);
1865 queue
->dev
= ctrl
->dev
;
1868 queue
->cmnd_capsule_len
= ctrl
->ctrl
.ioccsz
* 16;
1870 queue
->cmnd_capsule_len
= sizeof(struct nvme_command
);
1873 * Considered whether we should allocate buffers for all SQEs
1874 * and CQEs and dma map them - mapping their respective entries
1875 * into the request structures (kernel vm addr and dma address)
1876 * thus the driver could use the buffers/mappings directly.
1877 * It only makes sense if the LLDD would use them for its
1878 * messaging api. It's very unlikely most adapter api's would use
1879 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1880 * structures were used instead.
1885 * This routine terminates a queue at the transport level.
1886 * The transport has already ensured that all outstanding ios on
1887 * the queue have been terminated.
1888 * The transport will send a Disconnect LS request to terminate
1889 * the queue's connection. Termination of the admin queue will also
1890 * terminate the association at the target.
1893 nvme_fc_free_queue(struct nvme_fc_queue
*queue
)
1895 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED
, &queue
->flags
))
1898 clear_bit(NVME_FC_Q_LIVE
, &queue
->flags
);
1900 * Current implementation never disconnects a single queue.
1901 * It always terminates a whole association. So there is never
1902 * a disconnect(queue) LS sent to the target.
1905 queue
->connection_id
= 0;
1909 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl
*ctrl
,
1910 struct nvme_fc_queue
*queue
, unsigned int qidx
)
1912 if (ctrl
->lport
->ops
->delete_queue
)
1913 ctrl
->lport
->ops
->delete_queue(&ctrl
->lport
->localport
, qidx
,
1914 queue
->lldd_handle
);
1915 queue
->lldd_handle
= NULL
;
1919 nvme_fc_free_io_queues(struct nvme_fc_ctrl
*ctrl
)
1923 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++)
1924 nvme_fc_free_queue(&ctrl
->queues
[i
]);
1928 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl
*ctrl
,
1929 struct nvme_fc_queue
*queue
, unsigned int qidx
, u16 qsize
)
1933 queue
->lldd_handle
= NULL
;
1934 if (ctrl
->lport
->ops
->create_queue
)
1935 ret
= ctrl
->lport
->ops
->create_queue(&ctrl
->lport
->localport
,
1936 qidx
, qsize
, &queue
->lldd_handle
);
1942 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl
*ctrl
)
1944 struct nvme_fc_queue
*queue
= &ctrl
->queues
[ctrl
->ctrl
.queue_count
- 1];
1947 for (i
= ctrl
->ctrl
.queue_count
- 1; i
>= 1; i
--, queue
--)
1948 __nvme_fc_delete_hw_queue(ctrl
, queue
, i
);
1952 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl
*ctrl
, u16 qsize
)
1954 struct nvme_fc_queue
*queue
= &ctrl
->queues
[1];
1957 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++, queue
++) {
1958 ret
= __nvme_fc_create_hw_queue(ctrl
, queue
, i
, qsize
);
1967 __nvme_fc_delete_hw_queue(ctrl
, &ctrl
->queues
[i
], i
);
1972 nvme_fc_connect_io_queues(struct nvme_fc_ctrl
*ctrl
, u16 qsize
)
1976 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++) {
1977 ret
= nvme_fc_connect_queue(ctrl
, &ctrl
->queues
[i
], qsize
,
1981 ret
= nvmf_connect_io_queue(&ctrl
->ctrl
, i
);
1985 set_bit(NVME_FC_Q_LIVE
, &ctrl
->queues
[i
].flags
);
1992 nvme_fc_init_io_queues(struct nvme_fc_ctrl
*ctrl
)
1996 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++)
1997 nvme_fc_init_queue(ctrl
, i
);
2001 nvme_fc_ctrl_free(struct kref
*ref
)
2003 struct nvme_fc_ctrl
*ctrl
=
2004 container_of(ref
, struct nvme_fc_ctrl
, ref
);
2005 unsigned long flags
;
2007 if (ctrl
->ctrl
.tagset
) {
2008 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
2009 blk_mq_free_tag_set(&ctrl
->tag_set
);
2012 /* remove from rport list */
2013 spin_lock_irqsave(&ctrl
->rport
->lock
, flags
);
2014 list_del(&ctrl
->ctrl_list
);
2015 spin_unlock_irqrestore(&ctrl
->rport
->lock
, flags
);
2017 blk_mq_unquiesce_queue(ctrl
->ctrl
.admin_q
);
2018 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
2019 blk_mq_free_tag_set(&ctrl
->admin_tag_set
);
2021 kfree(ctrl
->queues
);
2023 put_device(ctrl
->dev
);
2024 nvme_fc_rport_put(ctrl
->rport
);
2026 ida_simple_remove(&nvme_fc_ctrl_cnt
, ctrl
->cnum
);
2027 if (ctrl
->ctrl
.opts
)
2028 nvmf_free_options(ctrl
->ctrl
.opts
);
2033 nvme_fc_ctrl_put(struct nvme_fc_ctrl
*ctrl
)
2035 kref_put(&ctrl
->ref
, nvme_fc_ctrl_free
);
2039 nvme_fc_ctrl_get(struct nvme_fc_ctrl
*ctrl
)
2041 return kref_get_unless_zero(&ctrl
->ref
);
2045 * All accesses from nvme core layer done - can now free the
2046 * controller. Called after last nvme_put_ctrl() call
2049 nvme_fc_nvme_ctrl_freed(struct nvme_ctrl
*nctrl
)
2051 struct nvme_fc_ctrl
*ctrl
= to_fc_ctrl(nctrl
);
2053 WARN_ON(nctrl
!= &ctrl
->ctrl
);
2055 nvme_fc_ctrl_put(ctrl
);
2059 nvme_fc_error_recovery(struct nvme_fc_ctrl
*ctrl
, char *errmsg
)
2061 /* only proceed if in LIVE state - e.g. on first error */
2062 if (ctrl
->ctrl
.state
!= NVME_CTRL_LIVE
)
2065 dev_warn(ctrl
->ctrl
.device
,
2066 "NVME-FC{%d}: transport association error detected: %s\n",
2067 ctrl
->cnum
, errmsg
);
2068 dev_warn(ctrl
->ctrl
.device
,
2069 "NVME-FC{%d}: resetting controller\n", ctrl
->cnum
);
2071 nvme_reset_ctrl(&ctrl
->ctrl
);
2074 static enum blk_eh_timer_return
2075 nvme_fc_timeout(struct request
*rq
, bool reserved
)
2077 struct nvme_fc_fcp_op
*op
= blk_mq_rq_to_pdu(rq
);
2078 struct nvme_fc_ctrl
*ctrl
= op
->ctrl
;
2081 * we can't individually ABTS an io without affecting the queue,
2082 * thus killing the queue, and thus the association.
2083 * So resolve by performing a controller reset, which will stop
2084 * the host/io stack, terminate the association on the link,
2085 * and recreate an association on the link.
2087 nvme_fc_error_recovery(ctrl
, "io timeout error");
2090 * the io abort has been initiated. Have the reset timer
2091 * restarted and the abort completion will complete the io
2092 * shortly. Avoids a synchronous wait while the abort finishes.
2094 return BLK_EH_RESET_TIMER
;
2098 nvme_fc_map_data(struct nvme_fc_ctrl
*ctrl
, struct request
*rq
,
2099 struct nvme_fc_fcp_op
*op
)
2101 struct nvmefc_fcp_req
*freq
= &op
->fcp_req
;
2102 enum dma_data_direction dir
;
2107 if (!blk_rq_payload_bytes(rq
))
2110 freq
->sg_table
.sgl
= freq
->first_sgl
;
2111 ret
= sg_alloc_table_chained(&freq
->sg_table
,
2112 blk_rq_nr_phys_segments(rq
), freq
->sg_table
.sgl
);
2116 op
->nents
= blk_rq_map_sg(rq
->q
, rq
, freq
->sg_table
.sgl
);
2117 WARN_ON(op
->nents
> blk_rq_nr_phys_segments(rq
));
2118 dir
= (rq_data_dir(rq
) == WRITE
) ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
2119 freq
->sg_cnt
= fc_dma_map_sg(ctrl
->lport
->dev
, freq
->sg_table
.sgl
,
2121 if (unlikely(freq
->sg_cnt
<= 0)) {
2122 sg_free_table_chained(&freq
->sg_table
, true);
2128 * TODO: blk_integrity_rq(rq) for DIF
2134 nvme_fc_unmap_data(struct nvme_fc_ctrl
*ctrl
, struct request
*rq
,
2135 struct nvme_fc_fcp_op
*op
)
2137 struct nvmefc_fcp_req
*freq
= &op
->fcp_req
;
2142 fc_dma_unmap_sg(ctrl
->lport
->dev
, freq
->sg_table
.sgl
, op
->nents
,
2143 ((rq_data_dir(rq
) == WRITE
) ?
2144 DMA_TO_DEVICE
: DMA_FROM_DEVICE
));
2146 nvme_cleanup_cmd(rq
);
2148 sg_free_table_chained(&freq
->sg_table
, true);
2154 * In FC, the queue is a logical thing. At transport connect, the target
2155 * creates its "queue" and returns a handle that is to be given to the
2156 * target whenever it posts something to the corresponding SQ. When an
2157 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2158 * command contained within the SQE, an io, and assigns a FC exchange
2159 * to it. The SQE and the associated SQ handle are sent in the initial
2160 * CMD IU sents on the exchange. All transfers relative to the io occur
2161 * as part of the exchange. The CQE is the last thing for the io,
2162 * which is transferred (explicitly or implicitly) with the RSP IU
2163 * sent on the exchange. After the CQE is received, the FC exchange is
2164 * terminaed and the Exchange may be used on a different io.
2166 * The transport to LLDD api has the transport making a request for a
2167 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2168 * resource and transfers the command. The LLDD will then process all
2169 * steps to complete the io. Upon completion, the transport done routine
2172 * So - while the operation is outstanding to the LLDD, there is a link
2173 * level FC exchange resource that is also outstanding. This must be
2174 * considered in all cleanup operations.
2177 nvme_fc_start_fcp_op(struct nvme_fc_ctrl
*ctrl
, struct nvme_fc_queue
*queue
,
2178 struct nvme_fc_fcp_op
*op
, u32 data_len
,
2179 enum nvmefc_fcp_datadir io_dir
)
2181 struct nvme_fc_cmd_iu
*cmdiu
= &op
->cmd_iu
;
2182 struct nvme_command
*sqe
= &cmdiu
->sqe
;
2187 * before attempting to send the io, check to see if we believe
2188 * the target device is present
2190 if (ctrl
->rport
->remoteport
.port_state
!= FC_OBJSTATE_ONLINE
)
2191 return BLK_STS_RESOURCE
;
2193 if (!nvme_fc_ctrl_get(ctrl
))
2194 return BLK_STS_IOERR
;
2196 /* format the FC-NVME CMD IU and fcp_req */
2197 cmdiu
->connection_id
= cpu_to_be64(queue
->connection_id
);
2198 csn
= atomic_inc_return(&queue
->csn
);
2199 cmdiu
->csn
= cpu_to_be32(csn
);
2200 cmdiu
->data_len
= cpu_to_be32(data_len
);
2202 case NVMEFC_FCP_WRITE
:
2203 cmdiu
->flags
= FCNVME_CMD_FLAGS_WRITE
;
2205 case NVMEFC_FCP_READ
:
2206 cmdiu
->flags
= FCNVME_CMD_FLAGS_READ
;
2208 case NVMEFC_FCP_NODATA
:
2212 op
->fcp_req
.payload_length
= data_len
;
2213 op
->fcp_req
.io_dir
= io_dir
;
2214 op
->fcp_req
.transferred_length
= 0;
2215 op
->fcp_req
.rcv_rsplen
= 0;
2216 op
->fcp_req
.status
= NVME_SC_SUCCESS
;
2217 op
->fcp_req
.sqid
= cpu_to_le16(queue
->qnum
);
2220 * validate per fabric rules, set fields mandated by fabric spec
2221 * as well as those by FC-NVME spec.
2223 WARN_ON_ONCE(sqe
->common
.metadata
);
2224 sqe
->common
.flags
|= NVME_CMD_SGL_METABUF
;
2227 * format SQE DPTR field per FC-NVME rules:
2228 * type=0x5 Transport SGL Data Block Descriptor
2229 * subtype=0xA Transport-specific value
2231 * length=length of the data series
2233 sqe
->rw
.dptr
.sgl
.type
= (NVME_TRANSPORT_SGL_DATA_DESC
<< 4) |
2234 NVME_SGL_FMT_TRANSPORT_A
;
2235 sqe
->rw
.dptr
.sgl
.length
= cpu_to_le32(data_len
);
2236 sqe
->rw
.dptr
.sgl
.addr
= 0;
2238 if (!(op
->flags
& FCOP_FLAGS_AEN
)) {
2239 ret
= nvme_fc_map_data(ctrl
, op
->rq
, op
);
2241 nvme_cleanup_cmd(op
->rq
);
2242 nvme_fc_ctrl_put(ctrl
);
2243 if (ret
== -ENOMEM
|| ret
== -EAGAIN
)
2244 return BLK_STS_RESOURCE
;
2245 return BLK_STS_IOERR
;
2249 fc_dma_sync_single_for_device(ctrl
->lport
->dev
, op
->fcp_req
.cmddma
,
2250 sizeof(op
->cmd_iu
), DMA_TO_DEVICE
);
2252 atomic_set(&op
->state
, FCPOP_STATE_ACTIVE
);
2254 if (!(op
->flags
& FCOP_FLAGS_AEN
))
2255 blk_mq_start_request(op
->rq
);
2257 ret
= ctrl
->lport
->ops
->fcp_io(&ctrl
->lport
->localport
,
2258 &ctrl
->rport
->remoteport
,
2259 queue
->lldd_handle
, &op
->fcp_req
);
2262 opstate
= atomic_xchg(&op
->state
, FCPOP_STATE_COMPLETE
);
2263 __nvme_fc_fcpop_chk_teardowns(ctrl
, op
, opstate
);
2265 if (!(op
->flags
& FCOP_FLAGS_AEN
))
2266 nvme_fc_unmap_data(ctrl
, op
->rq
, op
);
2268 nvme_fc_ctrl_put(ctrl
);
2270 if (ctrl
->rport
->remoteport
.port_state
== FC_OBJSTATE_ONLINE
&&
2272 return BLK_STS_IOERR
;
2274 return BLK_STS_RESOURCE
;
2280 static inline blk_status_t
nvme_fc_is_ready(struct nvme_fc_queue
*queue
,
2283 if (unlikely(!test_bit(NVME_FC_Q_LIVE
, &queue
->flags
)))
2284 return nvmf_check_init_req(&queue
->ctrl
->ctrl
, rq
);
2289 nvme_fc_queue_rq(struct blk_mq_hw_ctx
*hctx
,
2290 const struct blk_mq_queue_data
*bd
)
2292 struct nvme_ns
*ns
= hctx
->queue
->queuedata
;
2293 struct nvme_fc_queue
*queue
= hctx
->driver_data
;
2294 struct nvme_fc_ctrl
*ctrl
= queue
->ctrl
;
2295 struct request
*rq
= bd
->rq
;
2296 struct nvme_fc_fcp_op
*op
= blk_mq_rq_to_pdu(rq
);
2297 struct nvme_fc_cmd_iu
*cmdiu
= &op
->cmd_iu
;
2298 struct nvme_command
*sqe
= &cmdiu
->sqe
;
2299 enum nvmefc_fcp_datadir io_dir
;
2303 ret
= nvme_fc_is_ready(queue
, rq
);
2307 ret
= nvme_setup_cmd(ns
, rq
, sqe
);
2311 data_len
= blk_rq_payload_bytes(rq
);
2313 io_dir
= ((rq_data_dir(rq
) == WRITE
) ?
2314 NVMEFC_FCP_WRITE
: NVMEFC_FCP_READ
);
2316 io_dir
= NVMEFC_FCP_NODATA
;
2318 return nvme_fc_start_fcp_op(ctrl
, queue
, op
, data_len
, io_dir
);
2321 static struct blk_mq_tags
*
2322 nvme_fc_tagset(struct nvme_fc_queue
*queue
)
2324 if (queue
->qnum
== 0)
2325 return queue
->ctrl
->admin_tag_set
.tags
[queue
->qnum
];
2327 return queue
->ctrl
->tag_set
.tags
[queue
->qnum
- 1];
2331 nvme_fc_poll(struct blk_mq_hw_ctx
*hctx
, unsigned int tag
)
2334 struct nvme_fc_queue
*queue
= hctx
->driver_data
;
2335 struct nvme_fc_ctrl
*ctrl
= queue
->ctrl
;
2336 struct request
*req
;
2337 struct nvme_fc_fcp_op
*op
;
2339 req
= blk_mq_tag_to_rq(nvme_fc_tagset(queue
), tag
);
2343 op
= blk_mq_rq_to_pdu(req
);
2345 if ((atomic_read(&op
->state
) == FCPOP_STATE_ACTIVE
) &&
2346 (ctrl
->lport
->ops
->poll_queue
))
2347 ctrl
->lport
->ops
->poll_queue(&ctrl
->lport
->localport
,
2348 queue
->lldd_handle
);
2350 return ((atomic_read(&op
->state
) != FCPOP_STATE_ACTIVE
));
2354 nvme_fc_submit_async_event(struct nvme_ctrl
*arg
)
2356 struct nvme_fc_ctrl
*ctrl
= to_fc_ctrl(arg
);
2357 struct nvme_fc_fcp_op
*aen_op
;
2358 unsigned long flags
;
2359 bool terminating
= false;
2362 spin_lock_irqsave(&ctrl
->lock
, flags
);
2363 if (ctrl
->flags
& FCCTRL_TERMIO
)
2365 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
2370 aen_op
= &ctrl
->aen_ops
[0];
2372 ret
= nvme_fc_start_fcp_op(ctrl
, aen_op
->queue
, aen_op
, 0,
2375 dev_err(ctrl
->ctrl
.device
,
2376 "failed async event work\n");
2380 nvme_fc_complete_rq(struct request
*rq
)
2382 struct nvme_fc_fcp_op
*op
= blk_mq_rq_to_pdu(rq
);
2383 struct nvme_fc_ctrl
*ctrl
= op
->ctrl
;
2385 atomic_set(&op
->state
, FCPOP_STATE_IDLE
);
2387 nvme_fc_unmap_data(ctrl
, rq
, op
);
2388 nvme_complete_rq(rq
);
2389 nvme_fc_ctrl_put(ctrl
);
2393 * This routine is used by the transport when it needs to find active
2394 * io on a queue that is to be terminated. The transport uses
2395 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2396 * this routine to kill them on a 1 by 1 basis.
2398 * As FC allocates FC exchange for each io, the transport must contact
2399 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2400 * After terminating the exchange the LLDD will call the transport's
2401 * normal io done path for the request, but it will have an aborted
2402 * status. The done path will return the io request back to the block
2403 * layer with an error status.
2406 nvme_fc_terminate_exchange(struct request
*req
, void *data
, bool reserved
)
2408 struct nvme_ctrl
*nctrl
= data
;
2409 struct nvme_fc_ctrl
*ctrl
= to_fc_ctrl(nctrl
);
2410 struct nvme_fc_fcp_op
*op
= blk_mq_rq_to_pdu(req
);
2412 if (!blk_mq_request_started(req
))
2415 __nvme_fc_abort_op(ctrl
, op
);
2419 static const struct blk_mq_ops nvme_fc_mq_ops
= {
2420 .queue_rq
= nvme_fc_queue_rq
,
2421 .complete
= nvme_fc_complete_rq
,
2422 .init_request
= nvme_fc_init_request
,
2423 .exit_request
= nvme_fc_exit_request
,
2424 .init_hctx
= nvme_fc_init_hctx
,
2425 .poll
= nvme_fc_poll
,
2426 .timeout
= nvme_fc_timeout
,
2430 nvme_fc_create_io_queues(struct nvme_fc_ctrl
*ctrl
)
2432 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
2433 unsigned int nr_io_queues
;
2436 nr_io_queues
= min(min(opts
->nr_io_queues
, num_online_cpus()),
2437 ctrl
->lport
->ops
->max_hw_queues
);
2438 ret
= nvme_set_queue_count(&ctrl
->ctrl
, &nr_io_queues
);
2440 dev_info(ctrl
->ctrl
.device
,
2441 "set_queue_count failed: %d\n", ret
);
2445 ctrl
->ctrl
.queue_count
= nr_io_queues
+ 1;
2449 nvme_fc_init_io_queues(ctrl
);
2451 memset(&ctrl
->tag_set
, 0, sizeof(ctrl
->tag_set
));
2452 ctrl
->tag_set
.ops
= &nvme_fc_mq_ops
;
2453 ctrl
->tag_set
.queue_depth
= ctrl
->ctrl
.opts
->queue_size
;
2454 ctrl
->tag_set
.reserved_tags
= 1; /* fabric connect */
2455 ctrl
->tag_set
.numa_node
= NUMA_NO_NODE
;
2456 ctrl
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
;
2457 ctrl
->tag_set
.cmd_size
= sizeof(struct nvme_fc_fcp_op
) +
2459 sizeof(struct scatterlist
)) +
2460 ctrl
->lport
->ops
->fcprqst_priv_sz
;
2461 ctrl
->tag_set
.driver_data
= ctrl
;
2462 ctrl
->tag_set
.nr_hw_queues
= ctrl
->ctrl
.queue_count
- 1;
2463 ctrl
->tag_set
.timeout
= NVME_IO_TIMEOUT
;
2465 ret
= blk_mq_alloc_tag_set(&ctrl
->tag_set
);
2469 ctrl
->ctrl
.tagset
= &ctrl
->tag_set
;
2471 ctrl
->ctrl
.connect_q
= blk_mq_init_queue(&ctrl
->tag_set
);
2472 if (IS_ERR(ctrl
->ctrl
.connect_q
)) {
2473 ret
= PTR_ERR(ctrl
->ctrl
.connect_q
);
2474 goto out_free_tag_set
;
2477 ret
= nvme_fc_create_hw_io_queues(ctrl
, ctrl
->ctrl
.sqsize
+ 1);
2479 goto out_cleanup_blk_queue
;
2481 ret
= nvme_fc_connect_io_queues(ctrl
, ctrl
->ctrl
.sqsize
+ 1);
2483 goto out_delete_hw_queues
;
2487 out_delete_hw_queues
:
2488 nvme_fc_delete_hw_io_queues(ctrl
);
2489 out_cleanup_blk_queue
:
2490 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
2492 blk_mq_free_tag_set(&ctrl
->tag_set
);
2493 nvme_fc_free_io_queues(ctrl
);
2495 /* force put free routine to ignore io queues */
2496 ctrl
->ctrl
.tagset
= NULL
;
2502 nvme_fc_reinit_io_queues(struct nvme_fc_ctrl
*ctrl
)
2504 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
2505 unsigned int nr_io_queues
;
2508 nr_io_queues
= min(min(opts
->nr_io_queues
, num_online_cpus()),
2509 ctrl
->lport
->ops
->max_hw_queues
);
2510 ret
= nvme_set_queue_count(&ctrl
->ctrl
, &nr_io_queues
);
2512 dev_info(ctrl
->ctrl
.device
,
2513 "set_queue_count failed: %d\n", ret
);
2517 ctrl
->ctrl
.queue_count
= nr_io_queues
+ 1;
2518 /* check for io queues existing */
2519 if (ctrl
->ctrl
.queue_count
== 1)
2522 nvme_fc_init_io_queues(ctrl
);
2524 ret
= nvme_reinit_tagset(&ctrl
->ctrl
, ctrl
->ctrl
.tagset
);
2526 goto out_free_io_queues
;
2528 ret
= nvme_fc_create_hw_io_queues(ctrl
, ctrl
->ctrl
.sqsize
+ 1);
2530 goto out_free_io_queues
;
2532 ret
= nvme_fc_connect_io_queues(ctrl
, ctrl
->ctrl
.sqsize
+ 1);
2534 goto out_delete_hw_queues
;
2536 blk_mq_update_nr_hw_queues(&ctrl
->tag_set
, nr_io_queues
);
2540 out_delete_hw_queues
:
2541 nvme_fc_delete_hw_io_queues(ctrl
);
2543 nvme_fc_free_io_queues(ctrl
);
2548 nvme_fc_rport_active_on_lport(struct nvme_fc_rport
*rport
)
2550 struct nvme_fc_lport
*lport
= rport
->lport
;
2552 atomic_inc(&lport
->act_rport_cnt
);
2556 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport
*rport
)
2558 struct nvme_fc_lport
*lport
= rport
->lport
;
2561 cnt
= atomic_dec_return(&lport
->act_rport_cnt
);
2562 if (cnt
== 0 && lport
->localport
.port_state
== FC_OBJSTATE_DELETED
)
2563 lport
->ops
->localport_delete(&lport
->localport
);
2567 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl
*ctrl
)
2569 struct nvme_fc_rport
*rport
= ctrl
->rport
;
2572 if (ctrl
->assoc_active
)
2575 ctrl
->assoc_active
= true;
2576 cnt
= atomic_inc_return(&rport
->act_ctrl_cnt
);
2578 nvme_fc_rport_active_on_lport(rport
);
2584 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl
*ctrl
)
2586 struct nvme_fc_rport
*rport
= ctrl
->rport
;
2587 struct nvme_fc_lport
*lport
= rport
->lport
;
2590 /* ctrl->assoc_active=false will be set independently */
2592 cnt
= atomic_dec_return(&rport
->act_ctrl_cnt
);
2594 if (rport
->remoteport
.port_state
== FC_OBJSTATE_DELETED
)
2595 lport
->ops
->remoteport_delete(&rport
->remoteport
);
2596 nvme_fc_rport_inactive_on_lport(rport
);
2603 * This routine restarts the controller on the host side, and
2604 * on the link side, recreates the controller association.
2607 nvme_fc_create_association(struct nvme_fc_ctrl
*ctrl
)
2609 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
2613 ++ctrl
->ctrl
.nr_reconnects
;
2615 if (ctrl
->rport
->remoteport
.port_state
!= FC_OBJSTATE_ONLINE
)
2618 if (nvme_fc_ctlr_active_on_rport(ctrl
))
2622 * Create the admin queue
2625 nvme_fc_init_queue(ctrl
, 0);
2627 ret
= __nvme_fc_create_hw_queue(ctrl
, &ctrl
->queues
[0], 0,
2630 goto out_free_queue
;
2632 ret
= nvme_fc_connect_admin_queue(ctrl
, &ctrl
->queues
[0],
2633 NVME_AQ_DEPTH
, (NVME_AQ_DEPTH
/ 4));
2635 goto out_delete_hw_queue
;
2637 if (ctrl
->ctrl
.state
!= NVME_CTRL_NEW
)
2638 blk_mq_unquiesce_queue(ctrl
->ctrl
.admin_q
);
2640 ret
= nvmf_connect_admin_queue(&ctrl
->ctrl
);
2642 goto out_disconnect_admin_queue
;
2644 set_bit(NVME_FC_Q_LIVE
, &ctrl
->queues
[0].flags
);
2647 * Check controller capabilities
2649 * todo:- add code to check if ctrl attributes changed from
2650 * prior connection values
2653 ret
= nvmf_reg_read64(&ctrl
->ctrl
, NVME_REG_CAP
, &ctrl
->ctrl
.cap
);
2655 dev_err(ctrl
->ctrl
.device
,
2656 "prop_get NVME_REG_CAP failed\n");
2657 goto out_disconnect_admin_queue
;
2661 min_t(int, NVME_CAP_MQES(ctrl
->ctrl
.cap
), ctrl
->ctrl
.sqsize
);
2663 ret
= nvme_enable_ctrl(&ctrl
->ctrl
, ctrl
->ctrl
.cap
);
2665 goto out_disconnect_admin_queue
;
2667 ctrl
->ctrl
.max_hw_sectors
=
2668 (ctrl
->lport
->ops
->max_sgl_segments
- 1) << (PAGE_SHIFT
- 9);
2670 ret
= nvme_init_identify(&ctrl
->ctrl
);
2672 goto out_disconnect_admin_queue
;
2676 /* FC-NVME does not have other data in the capsule */
2677 if (ctrl
->ctrl
.icdoff
) {
2678 dev_err(ctrl
->ctrl
.device
, "icdoff %d is not supported!\n",
2680 goto out_disconnect_admin_queue
;
2683 /* FC-NVME supports normal SGL Data Block Descriptors */
2685 if (opts
->queue_size
> ctrl
->ctrl
.maxcmd
) {
2686 /* warn if maxcmd is lower than queue_size */
2687 dev_warn(ctrl
->ctrl
.device
,
2688 "queue_size %zu > ctrl maxcmd %u, reducing "
2690 opts
->queue_size
, ctrl
->ctrl
.maxcmd
);
2691 opts
->queue_size
= ctrl
->ctrl
.maxcmd
;
2694 if (opts
->queue_size
> ctrl
->ctrl
.sqsize
+ 1) {
2695 /* warn if sqsize is lower than queue_size */
2696 dev_warn(ctrl
->ctrl
.device
,
2697 "queue_size %zu > ctrl sqsize %u, clamping down\n",
2698 opts
->queue_size
, ctrl
->ctrl
.sqsize
+ 1);
2699 opts
->queue_size
= ctrl
->ctrl
.sqsize
+ 1;
2702 ret
= nvme_fc_init_aen_ops(ctrl
);
2704 goto out_term_aen_ops
;
2707 * Create the io queues
2710 if (ctrl
->ctrl
.queue_count
> 1) {
2711 if (ctrl
->ctrl
.state
== NVME_CTRL_NEW
)
2712 ret
= nvme_fc_create_io_queues(ctrl
);
2714 ret
= nvme_fc_reinit_io_queues(ctrl
);
2716 goto out_term_aen_ops
;
2719 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
2721 ctrl
->ctrl
.nr_reconnects
= 0;
2724 nvme_start_ctrl(&ctrl
->ctrl
);
2726 return 0; /* Success */
2729 nvme_fc_term_aen_ops(ctrl
);
2730 out_disconnect_admin_queue
:
2731 /* send a Disconnect(association) LS to fc-nvme target */
2732 nvme_fc_xmt_disconnect_assoc(ctrl
);
2733 out_delete_hw_queue
:
2734 __nvme_fc_delete_hw_queue(ctrl
, &ctrl
->queues
[0], 0);
2736 nvme_fc_free_queue(&ctrl
->queues
[0]);
2737 ctrl
->assoc_active
= false;
2738 nvme_fc_ctlr_inactive_on_rport(ctrl
);
2744 * This routine stops operation of the controller on the host side.
2745 * On the host os stack side: Admin and IO queues are stopped,
2746 * outstanding ios on them terminated via FC ABTS.
2747 * On the link side: the association is terminated.
2750 nvme_fc_delete_association(struct nvme_fc_ctrl
*ctrl
)
2752 unsigned long flags
;
2754 if (!ctrl
->assoc_active
)
2756 ctrl
->assoc_active
= false;
2758 spin_lock_irqsave(&ctrl
->lock
, flags
);
2759 ctrl
->flags
|= FCCTRL_TERMIO
;
2761 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
2764 * If io queues are present, stop them and terminate all outstanding
2765 * ios on them. As FC allocates FC exchange for each io, the
2766 * transport must contact the LLDD to terminate the exchange,
2767 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2768 * to tell us what io's are busy and invoke a transport routine
2769 * to kill them with the LLDD. After terminating the exchange
2770 * the LLDD will call the transport's normal io done path, but it
2771 * will have an aborted status. The done path will return the
2772 * io requests back to the block layer as part of normal completions
2773 * (but with error status).
2775 if (ctrl
->ctrl
.queue_count
> 1) {
2776 nvme_stop_queues(&ctrl
->ctrl
);
2777 blk_mq_tagset_busy_iter(&ctrl
->tag_set
,
2778 nvme_fc_terminate_exchange
, &ctrl
->ctrl
);
2782 * Other transports, which don't have link-level contexts bound
2783 * to sqe's, would try to gracefully shutdown the controller by
2784 * writing the registers for shutdown and polling (call
2785 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
2786 * just aborted and we will wait on those contexts, and given
2787 * there was no indication of how live the controlelr is on the
2788 * link, don't send more io to create more contexts for the
2789 * shutdown. Let the controller fail via keepalive failure if
2790 * its still present.
2794 * clean up the admin queue. Same thing as above.
2795 * use blk_mq_tagset_busy_itr() and the transport routine to
2796 * terminate the exchanges.
2798 if (ctrl
->ctrl
.state
!= NVME_CTRL_NEW
)
2799 blk_mq_quiesce_queue(ctrl
->ctrl
.admin_q
);
2800 blk_mq_tagset_busy_iter(&ctrl
->admin_tag_set
,
2801 nvme_fc_terminate_exchange
, &ctrl
->ctrl
);
2803 /* kill the aens as they are a separate path */
2804 nvme_fc_abort_aen_ops(ctrl
);
2806 /* wait for all io that had to be aborted */
2807 spin_lock_irq(&ctrl
->lock
);
2808 wait_event_lock_irq(ctrl
->ioabort_wait
, ctrl
->iocnt
== 0, ctrl
->lock
);
2809 ctrl
->flags
&= ~FCCTRL_TERMIO
;
2810 spin_unlock_irq(&ctrl
->lock
);
2812 nvme_fc_term_aen_ops(ctrl
);
2815 * send a Disconnect(association) LS to fc-nvme target
2816 * Note: could have been sent at top of process, but
2817 * cleaner on link traffic if after the aborts complete.
2818 * Note: if association doesn't exist, association_id will be 0
2820 if (ctrl
->association_id
)
2821 nvme_fc_xmt_disconnect_assoc(ctrl
);
2823 if (ctrl
->ctrl
.tagset
) {
2824 nvme_fc_delete_hw_io_queues(ctrl
);
2825 nvme_fc_free_io_queues(ctrl
);
2828 __nvme_fc_delete_hw_queue(ctrl
, &ctrl
->queues
[0], 0);
2829 nvme_fc_free_queue(&ctrl
->queues
[0]);
2831 /* re-enable the admin_q so anything new can fast fail */
2832 blk_mq_unquiesce_queue(ctrl
->ctrl
.admin_q
);
2834 nvme_fc_ctlr_inactive_on_rport(ctrl
);
2838 nvme_fc_delete_ctrl(struct nvme_ctrl
*nctrl
)
2840 struct nvme_fc_ctrl
*ctrl
= to_fc_ctrl(nctrl
);
2842 cancel_delayed_work_sync(&ctrl
->connect_work
);
2844 * kill the association on the link side. this will block
2845 * waiting for io to terminate
2847 nvme_fc_delete_association(ctrl
);
2849 /* resume the io queues so that things will fast fail */
2850 nvme_start_queues(nctrl
);
2854 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl
*ctrl
, int status
)
2856 struct nvme_fc_rport
*rport
= ctrl
->rport
;
2857 struct nvme_fc_remote_port
*portptr
= &rport
->remoteport
;
2858 unsigned long recon_delay
= ctrl
->ctrl
.opts
->reconnect_delay
* HZ
;
2861 if (ctrl
->ctrl
.state
!= NVME_CTRL_CONNECTING
)
2864 if (portptr
->port_state
== FC_OBJSTATE_ONLINE
)
2865 dev_info(ctrl
->ctrl
.device
,
2866 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
2867 ctrl
->cnum
, status
);
2868 else if (time_after_eq(jiffies
, rport
->dev_loss_end
))
2871 if (recon
&& nvmf_should_reconnect(&ctrl
->ctrl
)) {
2872 if (portptr
->port_state
== FC_OBJSTATE_ONLINE
)
2873 dev_info(ctrl
->ctrl
.device
,
2874 "NVME-FC{%d}: Reconnect attempt in %ld "
2876 ctrl
->cnum
, recon_delay
/ HZ
);
2877 else if (time_after(jiffies
+ recon_delay
, rport
->dev_loss_end
))
2878 recon_delay
= rport
->dev_loss_end
- jiffies
;
2880 queue_delayed_work(nvme_wq
, &ctrl
->connect_work
, recon_delay
);
2882 if (portptr
->port_state
== FC_OBJSTATE_ONLINE
)
2883 dev_warn(ctrl
->ctrl
.device
,
2884 "NVME-FC{%d}: Max reconnect attempts (%d) "
2886 ctrl
->cnum
, ctrl
->ctrl
.nr_reconnects
);
2888 dev_warn(ctrl
->ctrl
.device
,
2889 "NVME-FC{%d}: dev_loss_tmo (%d) expired "
2890 "while waiting for remoteport connectivity.\n",
2891 ctrl
->cnum
, portptr
->dev_loss_tmo
);
2892 WARN_ON(nvme_delete_ctrl(&ctrl
->ctrl
));
2897 nvme_fc_reset_ctrl_work(struct work_struct
*work
)
2899 struct nvme_fc_ctrl
*ctrl
=
2900 container_of(work
, struct nvme_fc_ctrl
, ctrl
.reset_work
);
2903 nvme_stop_ctrl(&ctrl
->ctrl
);
2905 /* will block will waiting for io to terminate */
2906 nvme_fc_delete_association(ctrl
);
2908 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_CONNECTING
)) {
2909 dev_err(ctrl
->ctrl
.device
,
2910 "NVME-FC{%d}: error_recovery: Couldn't change state "
2911 "to CONNECTING\n", ctrl
->cnum
);
2915 if (ctrl
->rport
->remoteport
.port_state
== FC_OBJSTATE_ONLINE
)
2916 ret
= nvme_fc_create_association(ctrl
);
2921 nvme_fc_reconnect_or_delete(ctrl
, ret
);
2923 dev_info(ctrl
->ctrl
.device
,
2924 "NVME-FC{%d}: controller reset complete\n",
2928 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops
= {
2930 .module
= THIS_MODULE
,
2931 .flags
= NVME_F_FABRICS
,
2932 .reg_read32
= nvmf_reg_read32
,
2933 .reg_read64
= nvmf_reg_read64
,
2934 .reg_write32
= nvmf_reg_write32
,
2935 .free_ctrl
= nvme_fc_nvme_ctrl_freed
,
2936 .submit_async_event
= nvme_fc_submit_async_event
,
2937 .delete_ctrl
= nvme_fc_delete_ctrl
,
2938 .get_address
= nvmf_get_address
,
2939 .reinit_request
= nvme_fc_reinit_request
,
2943 nvme_fc_connect_ctrl_work(struct work_struct
*work
)
2947 struct nvme_fc_ctrl
*ctrl
=
2948 container_of(to_delayed_work(work
),
2949 struct nvme_fc_ctrl
, connect_work
);
2951 ret
= nvme_fc_create_association(ctrl
);
2953 nvme_fc_reconnect_or_delete(ctrl
, ret
);
2955 dev_info(ctrl
->ctrl
.device
,
2956 "NVME-FC{%d}: controller reconnect complete\n",
2961 static const struct blk_mq_ops nvme_fc_admin_mq_ops
= {
2962 .queue_rq
= nvme_fc_queue_rq
,
2963 .complete
= nvme_fc_complete_rq
,
2964 .init_request
= nvme_fc_init_request
,
2965 .exit_request
= nvme_fc_exit_request
,
2966 .init_hctx
= nvme_fc_init_admin_hctx
,
2967 .timeout
= nvme_fc_timeout
,
2972 * Fails a controller request if it matches an existing controller
2973 * (association) with the same tuple:
2974 * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
2976 * The ports don't need to be compared as they are intrinsically
2977 * already matched by the port pointers supplied.
2980 nvme_fc_existing_controller(struct nvme_fc_rport
*rport
,
2981 struct nvmf_ctrl_options
*opts
)
2983 struct nvme_fc_ctrl
*ctrl
;
2984 unsigned long flags
;
2987 spin_lock_irqsave(&rport
->lock
, flags
);
2988 list_for_each_entry(ctrl
, &rport
->ctrl_list
, ctrl_list
) {
2989 found
= nvmf_ctlr_matches_baseopts(&ctrl
->ctrl
, opts
);
2993 spin_unlock_irqrestore(&rport
->lock
, flags
);
2998 static struct nvme_ctrl
*
2999 nvme_fc_init_ctrl(struct device
*dev
, struct nvmf_ctrl_options
*opts
,
3000 struct nvme_fc_lport
*lport
, struct nvme_fc_rport
*rport
)
3002 struct nvme_fc_ctrl
*ctrl
;
3003 unsigned long flags
;
3004 int ret
, idx
, retry
;
3006 if (!(rport
->remoteport
.port_role
&
3007 (FC_PORT_ROLE_NVME_DISCOVERY
| FC_PORT_ROLE_NVME_TARGET
))) {
3012 if (!opts
->duplicate_connect
&&
3013 nvme_fc_existing_controller(rport
, opts
)) {
3018 ctrl
= kzalloc(sizeof(*ctrl
), GFP_KERNEL
);
3024 idx
= ida_simple_get(&nvme_fc_ctrl_cnt
, 0, 0, GFP_KERNEL
);
3030 ctrl
->ctrl
.opts
= opts
;
3031 INIT_LIST_HEAD(&ctrl
->ctrl_list
);
3032 ctrl
->lport
= lport
;
3033 ctrl
->rport
= rport
;
3034 ctrl
->dev
= lport
->dev
;
3036 ctrl
->assoc_active
= false;
3037 init_waitqueue_head(&ctrl
->ioabort_wait
);
3039 get_device(ctrl
->dev
);
3040 kref_init(&ctrl
->ref
);
3042 INIT_WORK(&ctrl
->ctrl
.reset_work
, nvme_fc_reset_ctrl_work
);
3043 INIT_DELAYED_WORK(&ctrl
->connect_work
, nvme_fc_connect_ctrl_work
);
3044 spin_lock_init(&ctrl
->lock
);
3046 /* io queue count */
3047 ctrl
->ctrl
.queue_count
= min_t(unsigned int,
3049 lport
->ops
->max_hw_queues
);
3050 ctrl
->ctrl
.queue_count
++; /* +1 for admin queue */
3052 ctrl
->ctrl
.sqsize
= opts
->queue_size
- 1;
3053 ctrl
->ctrl
.kato
= opts
->kato
;
3056 ctrl
->queues
= kcalloc(ctrl
->ctrl
.queue_count
,
3057 sizeof(struct nvme_fc_queue
), GFP_KERNEL
);
3061 memset(&ctrl
->admin_tag_set
, 0, sizeof(ctrl
->admin_tag_set
));
3062 ctrl
->admin_tag_set
.ops
= &nvme_fc_admin_mq_ops
;
3063 ctrl
->admin_tag_set
.queue_depth
= NVME_AQ_MQ_TAG_DEPTH
;
3064 ctrl
->admin_tag_set
.reserved_tags
= 2; /* fabric connect + Keep-Alive */
3065 ctrl
->admin_tag_set
.numa_node
= NUMA_NO_NODE
;
3066 ctrl
->admin_tag_set
.cmd_size
= sizeof(struct nvme_fc_fcp_op
) +
3068 sizeof(struct scatterlist
)) +
3069 ctrl
->lport
->ops
->fcprqst_priv_sz
;
3070 ctrl
->admin_tag_set
.driver_data
= ctrl
;
3071 ctrl
->admin_tag_set
.nr_hw_queues
= 1;
3072 ctrl
->admin_tag_set
.timeout
= ADMIN_TIMEOUT
;
3073 ctrl
->admin_tag_set
.flags
= BLK_MQ_F_NO_SCHED
;
3075 ret
= blk_mq_alloc_tag_set(&ctrl
->admin_tag_set
);
3077 goto out_free_queues
;
3078 ctrl
->ctrl
.admin_tagset
= &ctrl
->admin_tag_set
;
3080 ctrl
->ctrl
.admin_q
= blk_mq_init_queue(&ctrl
->admin_tag_set
);
3081 if (IS_ERR(ctrl
->ctrl
.admin_q
)) {
3082 ret
= PTR_ERR(ctrl
->ctrl
.admin_q
);
3083 goto out_free_admin_tag_set
;
3087 * Would have been nice to init io queues tag set as well.
3088 * However, we require interaction from the controller
3089 * for max io queue count before we can do so.
3090 * Defer this to the connect path.
3093 ret
= nvme_init_ctrl(&ctrl
->ctrl
, dev
, &nvme_fc_ctrl_ops
, 0);
3095 goto out_cleanup_admin_q
;
3097 /* at this point, teardown path changes to ref counting on nvme ctrl */
3099 spin_lock_irqsave(&rport
->lock
, flags
);
3100 list_add_tail(&ctrl
->ctrl_list
, &rport
->ctrl_list
);
3101 spin_unlock_irqrestore(&rport
->lock
, flags
);
3104 * It's possible that transactions used to create the association
3105 * may fail. Examples: CreateAssociation LS or CreateIOConnection
3106 * LS gets dropped/corrupted/fails; or a frame gets dropped or a
3107 * command times out for one of the actions to init the controller
3108 * (Connect, Get/Set_Property, Set_Features, etc). Many of these
3109 * transport errors (frame drop, LS failure) inherently must kill
3110 * the association. The transport is coded so that any command used
3111 * to create the association (prior to a LIVE state transition
3112 * while NEW or CONNECTING) will fail if it completes in error or
3115 * As such: as the connect request was mostly likely due to a
3116 * udev event that discovered the remote port, meaning there is
3117 * not an admin or script there to restart if the connect
3118 * request fails, retry the initial connection creation up to
3119 * three times before giving up and declaring failure.
3121 for (retry
= 0; retry
< 3; retry
++) {
3122 ret
= nvme_fc_create_association(ctrl
);
3128 nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_DELETING
);
3129 cancel_work_sync(&ctrl
->ctrl
.reset_work
);
3130 cancel_delayed_work_sync(&ctrl
->connect_work
);
3132 /* couldn't schedule retry - fail out */
3133 dev_err(ctrl
->ctrl
.device
,
3134 "NVME-FC{%d}: Connect retry failed\n", ctrl
->cnum
);
3136 ctrl
->ctrl
.opts
= NULL
;
3138 /* initiate nvme ctrl ref counting teardown */
3139 nvme_uninit_ctrl(&ctrl
->ctrl
);
3141 /* Remove core ctrl ref. */
3142 nvme_put_ctrl(&ctrl
->ctrl
);
3144 /* as we're past the point where we transition to the ref
3145 * counting teardown path, if we return a bad pointer here,
3146 * the calling routine, thinking it's prior to the
3147 * transition, will do an rport put. Since the teardown
3148 * path also does a rport put, we do an extra get here to
3149 * so proper order/teardown happens.
3151 nvme_fc_rport_get(rport
);
3155 return ERR_PTR(ret
);
3158 nvme_get_ctrl(&ctrl
->ctrl
);
3160 dev_info(ctrl
->ctrl
.device
,
3161 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
3162 ctrl
->cnum
, ctrl
->ctrl
.opts
->subsysnqn
);
3166 out_cleanup_admin_q
:
3167 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
3168 out_free_admin_tag_set
:
3169 blk_mq_free_tag_set(&ctrl
->admin_tag_set
);
3171 kfree(ctrl
->queues
);
3173 put_device(ctrl
->dev
);
3174 ida_simple_remove(&nvme_fc_ctrl_cnt
, ctrl
->cnum
);
3178 /* exit via here doesn't follow ctlr ref points */
3179 return ERR_PTR(ret
);
3183 struct nvmet_fc_traddr
{
3189 __nvme_fc_parse_u64(substring_t
*sstr
, u64
*val
)
3193 if (match_u64(sstr
, &token64
))
3201 * This routine validates and extracts the WWN's from the TRADDR string.
3202 * As kernel parsers need the 0x to determine number base, universally
3203 * build string to parse with 0x prefix before parsing name strings.
3206 nvme_fc_parse_traddr(struct nvmet_fc_traddr
*traddr
, char *buf
, size_t blen
)
3208 char name
[2 + NVME_FC_TRADDR_HEXNAMELEN
+ 1];
3209 substring_t wwn
= { name
, &name
[sizeof(name
)-1] };
3210 int nnoffset
, pnoffset
;
3212 /* validate it string one of the 2 allowed formats */
3213 if (strnlen(buf
, blen
) == NVME_FC_TRADDR_MAXLENGTH
&&
3214 !strncmp(buf
, "nn-0x", NVME_FC_TRADDR_OXNNLEN
) &&
3215 !strncmp(&buf
[NVME_FC_TRADDR_MAX_PN_OFFSET
],
3216 "pn-0x", NVME_FC_TRADDR_OXNNLEN
)) {
3217 nnoffset
= NVME_FC_TRADDR_OXNNLEN
;
3218 pnoffset
= NVME_FC_TRADDR_MAX_PN_OFFSET
+
3219 NVME_FC_TRADDR_OXNNLEN
;
3220 } else if ((strnlen(buf
, blen
) == NVME_FC_TRADDR_MINLENGTH
&&
3221 !strncmp(buf
, "nn-", NVME_FC_TRADDR_NNLEN
) &&
3222 !strncmp(&buf
[NVME_FC_TRADDR_MIN_PN_OFFSET
],
3223 "pn-", NVME_FC_TRADDR_NNLEN
))) {
3224 nnoffset
= NVME_FC_TRADDR_NNLEN
;
3225 pnoffset
= NVME_FC_TRADDR_MIN_PN_OFFSET
+ NVME_FC_TRADDR_NNLEN
;
3231 name
[2 + NVME_FC_TRADDR_HEXNAMELEN
] = 0;
3233 memcpy(&name
[2], &buf
[nnoffset
], NVME_FC_TRADDR_HEXNAMELEN
);
3234 if (__nvme_fc_parse_u64(&wwn
, &traddr
->nn
))
3237 memcpy(&name
[2], &buf
[pnoffset
], NVME_FC_TRADDR_HEXNAMELEN
);
3238 if (__nvme_fc_parse_u64(&wwn
, &traddr
->pn
))
3244 pr_warn("%s: bad traddr string\n", __func__
);
3248 static struct nvme_ctrl
*
3249 nvme_fc_create_ctrl(struct device
*dev
, struct nvmf_ctrl_options
*opts
)
3251 struct nvme_fc_lport
*lport
;
3252 struct nvme_fc_rport
*rport
;
3253 struct nvme_ctrl
*ctrl
;
3254 struct nvmet_fc_traddr laddr
= { 0L, 0L };
3255 struct nvmet_fc_traddr raddr
= { 0L, 0L };
3256 unsigned long flags
;
3259 ret
= nvme_fc_parse_traddr(&raddr
, opts
->traddr
, NVMF_TRADDR_SIZE
);
3260 if (ret
|| !raddr
.nn
|| !raddr
.pn
)
3261 return ERR_PTR(-EINVAL
);
3263 ret
= nvme_fc_parse_traddr(&laddr
, opts
->host_traddr
, NVMF_TRADDR_SIZE
);
3264 if (ret
|| !laddr
.nn
|| !laddr
.pn
)
3265 return ERR_PTR(-EINVAL
);
3267 /* find the host and remote ports to connect together */
3268 spin_lock_irqsave(&nvme_fc_lock
, flags
);
3269 list_for_each_entry(lport
, &nvme_fc_lport_list
, port_list
) {
3270 if (lport
->localport
.node_name
!= laddr
.nn
||
3271 lport
->localport
.port_name
!= laddr
.pn
)
3274 list_for_each_entry(rport
, &lport
->endp_list
, endp_list
) {
3275 if (rport
->remoteport
.node_name
!= raddr
.nn
||
3276 rport
->remoteport
.port_name
!= raddr
.pn
)
3279 /* if fail to get reference fall through. Will error */
3280 if (!nvme_fc_rport_get(rport
))
3283 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
3285 ctrl
= nvme_fc_init_ctrl(dev
, opts
, lport
, rport
);
3287 nvme_fc_rport_put(rport
);
3291 spin_unlock_irqrestore(&nvme_fc_lock
, flags
);
3293 return ERR_PTR(-ENOENT
);
3297 static struct nvmf_transport_ops nvme_fc_transport
= {
3299 .module
= THIS_MODULE
,
3300 .required_opts
= NVMF_OPT_TRADDR
| NVMF_OPT_HOST_TRADDR
,
3301 .allowed_opts
= NVMF_OPT_RECONNECT_DELAY
| NVMF_OPT_CTRL_LOSS_TMO
,
3302 .create_ctrl
= nvme_fc_create_ctrl
,
3305 static int __init
nvme_fc_init_module(void)
3311 * It is expected that in the future the kernel will combine
3312 * the FC-isms that are currently under scsi and now being
3313 * added to by NVME into a new standalone FC class. The SCSI
3314 * and NVME protocols and their devices would be under this
3317 * As we need something to post FC-specific udev events to,
3318 * specifically for nvme probe events, start by creating the
3319 * new device class. When the new standalone FC class is
3320 * put in place, this code will move to a more generic
3321 * location for the class.
3323 fc_class
= class_create(THIS_MODULE
, "fc");
3324 if (IS_ERR(fc_class
)) {
3325 pr_err("couldn't register class fc\n");
3326 return PTR_ERR(fc_class
);
3330 * Create a device for the FC-centric udev events
3332 fc_udev_device
= device_create(fc_class
, NULL
, MKDEV(0, 0), NULL
,
3334 if (IS_ERR(fc_udev_device
)) {
3335 pr_err("couldn't create fc_udev device!\n");
3336 ret
= PTR_ERR(fc_udev_device
);
3337 goto out_destroy_class
;
3340 ret
= nvmf_register_transport(&nvme_fc_transport
);
3342 goto out_destroy_device
;
3347 device_destroy(fc_class
, MKDEV(0, 0));
3349 class_destroy(fc_class
);
3353 static void __exit
nvme_fc_exit_module(void)
3355 /* sanity check - all lports should be removed */
3356 if (!list_empty(&nvme_fc_lport_list
))
3357 pr_warn("%s: localport list not empty\n", __func__
);
3359 nvmf_unregister_transport(&nvme_fc_transport
);
3361 ida_destroy(&nvme_fc_local_port_cnt
);
3362 ida_destroy(&nvme_fc_ctrl_cnt
);
3364 device_destroy(fc_class
, MKDEV(0, 0));
3365 class_destroy(fc_class
);
3368 module_init(nvme_fc_init_module
);
3369 module_exit(nvme_fc_exit_module
);
3371 MODULE_LICENSE("GPL v2");