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 ****************** */
34 * We handle AEN commands ourselves and don't even let the
35 * block layer know about them.
37 #define NVME_FC_NR_AEN_COMMANDS 1
38 #define NVME_FC_AQ_BLKMQ_DEPTH \
39 (NVMF_AQ_DEPTH - NVME_FC_NR_AEN_COMMANDS)
40 #define AEN_CMDID_BASE (NVME_FC_AQ_BLKMQ_DEPTH + 1)
42 enum nvme_fc_queue_flags {
43 NVME_FC_Q_CONNECTED = (1 << 0),
46 #define NVMEFC_QUEUE_DELAY 3 /* ms units */
48 #define NVME_FC_MAX_CONNECT_ATTEMPTS 1
50 struct nvme_fc_queue {
51 struct nvme_fc_ctrl *ctrl;
53 struct blk_mq_hw_ctx *hctx;
56 size_t cmnd_capsule_len;
65 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
67 enum nvme_fcop_flags {
68 FCOP_FLAGS_TERMIO = (1 << 0),
69 FCOP_FLAGS_RELEASED = (1 << 1),
70 FCOP_FLAGS_COMPLETE = (1 << 2),
71 FCOP_FLAGS_AEN = (1 << 3),
74 struct nvmefc_ls_req_op {
75 struct nvmefc_ls_req ls_req;
77 struct nvme_fc_rport *rport;
78 struct nvme_fc_queue *queue;
83 struct completion ls_done;
84 struct list_head lsreq_list; /* rport->ls_req_list */
88 enum nvme_fcpop_state {
89 FCPOP_STATE_UNINIT = 0,
91 FCPOP_STATE_ACTIVE = 2,
92 FCPOP_STATE_ABORTED = 3,
93 FCPOP_STATE_COMPLETE = 4,
96 struct nvme_fc_fcp_op {
97 struct nvme_request nreq; /*
100 * the 1st element in the
102 * associated with the
105 struct nvmefc_fcp_req fcp_req;
107 struct nvme_fc_ctrl *ctrl;
108 struct nvme_fc_queue *queue;
116 struct nvme_fc_cmd_iu cmd_iu;
117 struct nvme_fc_ersp_iu rsp_iu;
120 struct nvme_fc_lport {
121 struct nvme_fc_local_port localport;
124 struct list_head port_list; /* nvme_fc_port_list */
125 struct list_head endp_list;
126 struct device *dev; /* physical device for dma */
127 struct nvme_fc_port_template *ops;
129 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
131 struct nvme_fc_rport {
132 struct nvme_fc_remote_port remoteport;
134 struct list_head endp_list; /* for lport->endp_list */
135 struct list_head ctrl_list;
136 struct list_head ls_req_list;
137 struct device *dev; /* physical device for dma */
138 struct nvme_fc_lport *lport;
141 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
143 enum nvme_fcctrl_flags {
144 FCCTRL_TERMIO = (1 << 0),
147 struct nvme_fc_ctrl {
149 struct nvme_fc_queue *queues;
151 struct nvme_fc_lport *lport;
152 struct nvme_fc_rport *rport;
160 struct list_head ctrl_list; /* rport->ctrl_list */
162 struct blk_mq_tag_set admin_tag_set;
163 struct blk_mq_tag_set tag_set;
165 struct work_struct delete_work;
166 struct work_struct reset_work;
167 struct delayed_work connect_work;
169 int connect_attempts;
175 struct nvme_fc_fcp_op aen_ops[NVME_FC_NR_AEN_COMMANDS];
177 struct nvme_ctrl ctrl;
180 static inline struct nvme_fc_ctrl *
181 to_fc_ctrl(struct nvme_ctrl *ctrl)
183 return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
186 static inline struct nvme_fc_lport *
187 localport_to_lport(struct nvme_fc_local_port *portptr)
189 return container_of(portptr, struct nvme_fc_lport, localport);
192 static inline struct nvme_fc_rport *
193 remoteport_to_rport(struct nvme_fc_remote_port *portptr)
195 return container_of(portptr, struct nvme_fc_rport, remoteport);
198 static inline struct nvmefc_ls_req_op *
199 ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
201 return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
204 static inline struct nvme_fc_fcp_op *
205 fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
207 return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
212 /* *************************** Globals **************************** */
215 static DEFINE_SPINLOCK(nvme_fc_lock);
217 static LIST_HEAD(nvme_fc_lport_list);
218 static DEFINE_IDA(nvme_fc_local_port_cnt);
219 static DEFINE_IDA(nvme_fc_ctrl_cnt);
221 static struct workqueue_struct *nvme_fc_wq;
225 /* *********************** FC-NVME Port Management ************************ */
227 static int __nvme_fc_del_ctrl(struct nvme_fc_ctrl *);
228 static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
229 struct nvme_fc_queue *, unsigned int);
233 * nvme_fc_register_localport - transport entry point called by an
234 * LLDD to register the existence of a NVME
236 * @pinfo: pointer to information about the port to be registered
237 * @template: LLDD entrypoints and operational parameters for the port
238 * @dev: physical hardware device node port corresponds to. Will be
239 * used for DMA mappings
240 * @lport_p: pointer to a local port pointer. Upon success, the routine
241 * will allocate a nvme_fc_local_port structure and place its
242 * address in the local port pointer. Upon failure, local port
243 * pointer will be set to 0.
246 * a completion status. Must be 0 upon success; a negative errno
247 * (ex: -ENXIO) upon failure.
250 nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
251 struct nvme_fc_port_template *template,
253 struct nvme_fc_local_port **portptr)
255 struct nvme_fc_lport *newrec;
259 if (!template->localport_delete || !template->remoteport_delete ||
260 !template->ls_req || !template->fcp_io ||
261 !template->ls_abort || !template->fcp_abort ||
262 !template->max_hw_queues || !template->max_sgl_segments ||
263 !template->max_dif_sgl_segments || !template->dma_boundary) {
265 goto out_reghost_failed;
268 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
272 goto out_reghost_failed;
275 idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
281 if (!get_device(dev) && dev) {
286 INIT_LIST_HEAD(&newrec->port_list);
287 INIT_LIST_HEAD(&newrec->endp_list);
288 kref_init(&newrec->ref);
289 newrec->ops = template;
291 ida_init(&newrec->endp_cnt);
292 newrec->localport.private = &newrec[1];
293 newrec->localport.node_name = pinfo->node_name;
294 newrec->localport.port_name = pinfo->port_name;
295 newrec->localport.port_role = pinfo->port_role;
296 newrec->localport.port_id = pinfo->port_id;
297 newrec->localport.port_state = FC_OBJSTATE_ONLINE;
298 newrec->localport.port_num = idx;
300 spin_lock_irqsave(&nvme_fc_lock, flags);
301 list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
302 spin_unlock_irqrestore(&nvme_fc_lock, flags);
305 dma_set_seg_boundary(dev, template->dma_boundary);
307 *portptr = &newrec->localport;
311 ida_simple_remove(&nvme_fc_local_port_cnt, idx);
319 EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
322 nvme_fc_free_lport(struct kref *ref)
324 struct nvme_fc_lport *lport =
325 container_of(ref, struct nvme_fc_lport, ref);
328 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
329 WARN_ON(!list_empty(&lport->endp_list));
331 /* remove from transport list */
332 spin_lock_irqsave(&nvme_fc_lock, flags);
333 list_del(&lport->port_list);
334 spin_unlock_irqrestore(&nvme_fc_lock, flags);
336 /* let the LLDD know we've finished tearing it down */
337 lport->ops->localport_delete(&lport->localport);
339 ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
340 ida_destroy(&lport->endp_cnt);
342 put_device(lport->dev);
348 nvme_fc_lport_put(struct nvme_fc_lport *lport)
350 kref_put(&lport->ref, nvme_fc_free_lport);
354 nvme_fc_lport_get(struct nvme_fc_lport *lport)
356 return kref_get_unless_zero(&lport->ref);
360 * nvme_fc_unregister_localport - transport entry point called by an
361 * LLDD to deregister/remove a previously
362 * registered a NVME host FC port.
363 * @localport: pointer to the (registered) local port that is to be
367 * a completion status. Must be 0 upon success; a negative errno
368 * (ex: -ENXIO) upon failure.
371 nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
373 struct nvme_fc_lport *lport = localport_to_lport(portptr);
379 spin_lock_irqsave(&nvme_fc_lock, flags);
381 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
382 spin_unlock_irqrestore(&nvme_fc_lock, flags);
385 portptr->port_state = FC_OBJSTATE_DELETED;
387 spin_unlock_irqrestore(&nvme_fc_lock, flags);
389 nvme_fc_lport_put(lport);
393 EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
396 * nvme_fc_register_remoteport - transport entry point called by an
397 * LLDD to register the existence of a NVME
398 * subsystem FC port on its fabric.
399 * @localport: pointer to the (registered) local port that the remote
400 * subsystem port is connected to.
401 * @pinfo: pointer to information about the port to be registered
402 * @rport_p: pointer to a remote port pointer. Upon success, the routine
403 * will allocate a nvme_fc_remote_port structure and place its
404 * address in the remote port pointer. Upon failure, remote port
405 * pointer will be set to 0.
408 * a completion status. Must be 0 upon success; a negative errno
409 * (ex: -ENXIO) upon failure.
412 nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
413 struct nvme_fc_port_info *pinfo,
414 struct nvme_fc_remote_port **portptr)
416 struct nvme_fc_lport *lport = localport_to_lport(localport);
417 struct nvme_fc_rport *newrec;
421 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
425 goto out_reghost_failed;
428 if (!nvme_fc_lport_get(lport)) {
430 goto out_kfree_rport;
433 idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
439 INIT_LIST_HEAD(&newrec->endp_list);
440 INIT_LIST_HEAD(&newrec->ctrl_list);
441 INIT_LIST_HEAD(&newrec->ls_req_list);
442 kref_init(&newrec->ref);
443 spin_lock_init(&newrec->lock);
444 newrec->remoteport.localport = &lport->localport;
445 newrec->dev = lport->dev;
446 newrec->lport = lport;
447 newrec->remoteport.private = &newrec[1];
448 newrec->remoteport.port_role = pinfo->port_role;
449 newrec->remoteport.node_name = pinfo->node_name;
450 newrec->remoteport.port_name = pinfo->port_name;
451 newrec->remoteport.port_id = pinfo->port_id;
452 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
453 newrec->remoteport.port_num = idx;
455 spin_lock_irqsave(&nvme_fc_lock, flags);
456 list_add_tail(&newrec->endp_list, &lport->endp_list);
457 spin_unlock_irqrestore(&nvme_fc_lock, flags);
459 *portptr = &newrec->remoteport;
463 nvme_fc_lport_put(lport);
470 EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
473 nvme_fc_free_rport(struct kref *ref)
475 struct nvme_fc_rport *rport =
476 container_of(ref, struct nvme_fc_rport, ref);
477 struct nvme_fc_lport *lport =
478 localport_to_lport(rport->remoteport.localport);
481 WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
482 WARN_ON(!list_empty(&rport->ctrl_list));
484 /* remove from lport list */
485 spin_lock_irqsave(&nvme_fc_lock, flags);
486 list_del(&rport->endp_list);
487 spin_unlock_irqrestore(&nvme_fc_lock, flags);
489 /* let the LLDD know we've finished tearing it down */
490 lport->ops->remoteport_delete(&rport->remoteport);
492 ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
496 nvme_fc_lport_put(lport);
500 nvme_fc_rport_put(struct nvme_fc_rport *rport)
502 kref_put(&rport->ref, nvme_fc_free_rport);
506 nvme_fc_rport_get(struct nvme_fc_rport *rport)
508 return kref_get_unless_zero(&rport->ref);
512 nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
514 struct nvmefc_ls_req_op *lsop;
518 spin_lock_irqsave(&rport->lock, flags);
520 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
521 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
522 lsop->flags |= FCOP_FLAGS_TERMIO;
523 spin_unlock_irqrestore(&rport->lock, flags);
524 rport->lport->ops->ls_abort(&rport->lport->localport,
530 spin_unlock_irqrestore(&rport->lock, flags);
536 * nvme_fc_unregister_remoteport - transport entry point called by an
537 * LLDD to deregister/remove a previously
538 * registered a NVME subsystem FC port.
539 * @remoteport: pointer to the (registered) remote port that is to be
543 * a completion status. Must be 0 upon success; a negative errno
544 * (ex: -ENXIO) upon failure.
547 nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
549 struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
550 struct nvme_fc_ctrl *ctrl;
556 spin_lock_irqsave(&rport->lock, flags);
558 if (portptr->port_state != FC_OBJSTATE_ONLINE) {
559 spin_unlock_irqrestore(&rport->lock, flags);
562 portptr->port_state = FC_OBJSTATE_DELETED;
564 /* tear down all associations to the remote port */
565 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
566 __nvme_fc_del_ctrl(ctrl);
568 spin_unlock_irqrestore(&rport->lock, flags);
570 nvme_fc_abort_lsops(rport);
572 nvme_fc_rport_put(rport);
575 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
578 /* *********************** FC-NVME DMA Handling **************************** */
581 * The fcloop device passes in a NULL device pointer. Real LLD's will
582 * pass in a valid device pointer. If NULL is passed to the dma mapping
583 * routines, depending on the platform, it may or may not succeed, and
587 * Wrapper all the dma routines and check the dev pointer.
589 * If simple mappings (return just a dma address, we'll noop them,
590 * returning a dma address of 0.
592 * On more complex mappings (dma_map_sg), a pseudo routine fills
593 * in the scatter list, setting all dma addresses to 0.
596 static inline dma_addr_t
597 fc_dma_map_single(struct device *dev, void *ptr, size_t size,
598 enum dma_data_direction dir)
600 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
604 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
606 return dev ? dma_mapping_error(dev, dma_addr) : 0;
610 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
611 enum dma_data_direction dir)
614 dma_unmap_single(dev, addr, size, dir);
618 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
619 enum dma_data_direction dir)
622 dma_sync_single_for_cpu(dev, addr, size, dir);
626 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
627 enum dma_data_direction dir)
630 dma_sync_single_for_device(dev, addr, size, dir);
633 /* pseudo dma_map_sg call */
635 fc_map_sg(struct scatterlist *sg, int nents)
637 struct scatterlist *s;
640 WARN_ON(nents == 0 || sg[0].length == 0);
642 for_each_sg(sg, s, nents, i) {
644 #ifdef CONFIG_NEED_SG_DMA_LENGTH
645 s->dma_length = s->length;
652 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
653 enum dma_data_direction dir)
655 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
659 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
660 enum dma_data_direction dir)
663 dma_unmap_sg(dev, sg, nents, dir);
667 /* *********************** FC-NVME LS Handling **************************** */
669 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
670 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
674 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
676 struct nvme_fc_rport *rport = lsop->rport;
677 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
680 spin_lock_irqsave(&rport->lock, flags);
682 if (!lsop->req_queued) {
683 spin_unlock_irqrestore(&rport->lock, flags);
687 list_del(&lsop->lsreq_list);
689 lsop->req_queued = false;
691 spin_unlock_irqrestore(&rport->lock, flags);
693 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
694 (lsreq->rqstlen + lsreq->rsplen),
697 nvme_fc_rport_put(rport);
701 __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
702 struct nvmefc_ls_req_op *lsop,
703 void (*done)(struct nvmefc_ls_req *req, int status))
705 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
709 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
710 return -ECONNREFUSED;
712 if (!nvme_fc_rport_get(rport))
717 lsop->req_queued = false;
718 INIT_LIST_HEAD(&lsop->lsreq_list);
719 init_completion(&lsop->ls_done);
721 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
722 lsreq->rqstlen + lsreq->rsplen,
724 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
728 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
730 spin_lock_irqsave(&rport->lock, flags);
732 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
734 lsop->req_queued = true;
736 spin_unlock_irqrestore(&rport->lock, flags);
738 ret = rport->lport->ops->ls_req(&rport->lport->localport,
739 &rport->remoteport, lsreq);
746 lsop->ls_error = ret;
747 spin_lock_irqsave(&rport->lock, flags);
748 lsop->req_queued = false;
749 list_del(&lsop->lsreq_list);
750 spin_unlock_irqrestore(&rport->lock, flags);
751 fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
752 (lsreq->rqstlen + lsreq->rsplen),
755 nvme_fc_rport_put(rport);
761 nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
763 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
765 lsop->ls_error = status;
766 complete(&lsop->ls_done);
770 nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
772 struct nvmefc_ls_req *lsreq = &lsop->ls_req;
773 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
776 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
780 * No timeout/not interruptible as we need the struct
781 * to exist until the lldd calls us back. Thus mandate
782 * wait until driver calls back. lldd responsible for
785 wait_for_completion(&lsop->ls_done);
787 __nvme_fc_finish_ls_req(lsop);
789 ret = lsop->ls_error;
795 /* ACC or RJT payload ? */
796 if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
803 nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
804 struct nvmefc_ls_req_op *lsop,
805 void (*done)(struct nvmefc_ls_req *req, int status))
807 /* don't wait for completion */
809 return __nvme_fc_send_ls_req(rport, lsop, done);
812 /* Validation Error indexes into the string table below */
816 VERR_LSDESC_RQST = 2,
817 VERR_LSDESC_RQST_LEN = 3,
819 VERR_ASSOC_ID_LEN = 5,
821 VERR_CONN_ID_LEN = 7,
823 VERR_CR_ASSOC_ACC_LEN = 9,
825 VERR_CR_CONN_ACC_LEN = 11,
827 VERR_DISCONN_ACC_LEN = 13,
830 static char *validation_errors[] = {
834 "Bad LSDESC_RQST Length",
835 "Not Association ID",
836 "Bad Association ID Length",
838 "Bad Connection ID Length",
840 "Bad CR_ASSOC ACC Length",
842 "Bad CR_CONN ACC Length",
843 "Not Disconnect Rqst",
844 "Bad Disconnect ACC Length",
848 nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
849 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
851 struct nvmefc_ls_req_op *lsop;
852 struct nvmefc_ls_req *lsreq;
853 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
854 struct fcnvme_ls_cr_assoc_acc *assoc_acc;
857 lsop = kzalloc((sizeof(*lsop) +
858 ctrl->lport->ops->lsrqst_priv_sz +
859 sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
864 lsreq = &lsop->ls_req;
866 lsreq->private = (void *)&lsop[1];
867 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
868 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
869 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
871 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
872 assoc_rqst->desc_list_len =
873 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
875 assoc_rqst->assoc_cmd.desc_tag =
876 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
877 assoc_rqst->assoc_cmd.desc_len =
879 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
881 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
882 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize);
883 /* Linux supports only Dynamic controllers */
884 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
885 memcpy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id,
886 min_t(size_t, FCNVME_ASSOC_HOSTID_LEN, sizeof(uuid_be)));
887 strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
888 min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
889 strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
890 min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
893 lsreq->rqstaddr = assoc_rqst;
894 lsreq->rqstlen = sizeof(*assoc_rqst);
895 lsreq->rspaddr = assoc_acc;
896 lsreq->rsplen = sizeof(*assoc_acc);
897 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
899 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
901 goto out_free_buffer;
903 /* process connect LS completion */
905 /* validate the ACC response */
906 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
908 else if (assoc_acc->hdr.desc_list_len !=
910 sizeof(struct fcnvme_ls_cr_assoc_acc)))
911 fcret = VERR_CR_ASSOC_ACC_LEN;
912 else if (assoc_acc->hdr.rqst.desc_tag !=
913 cpu_to_be32(FCNVME_LSDESC_RQST))
914 fcret = VERR_LSDESC_RQST;
915 else if (assoc_acc->hdr.rqst.desc_len !=
916 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
917 fcret = VERR_LSDESC_RQST_LEN;
918 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
919 fcret = VERR_CR_ASSOC;
920 else if (assoc_acc->associd.desc_tag !=
921 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
922 fcret = VERR_ASSOC_ID;
923 else if (assoc_acc->associd.desc_len !=
925 sizeof(struct fcnvme_lsdesc_assoc_id)))
926 fcret = VERR_ASSOC_ID_LEN;
927 else if (assoc_acc->connectid.desc_tag !=
928 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
929 fcret = VERR_CONN_ID;
930 else if (assoc_acc->connectid.desc_len !=
931 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
932 fcret = VERR_CONN_ID_LEN;
937 "q %d connect failed: %s\n",
938 queue->qnum, validation_errors[fcret]);
940 ctrl->association_id =
941 be64_to_cpu(assoc_acc->associd.association_id);
942 queue->connection_id =
943 be64_to_cpu(assoc_acc->connectid.connection_id);
944 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
952 "queue %d connect admin queue failed (%d).\n",
958 nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
959 u16 qsize, u16 ersp_ratio)
961 struct nvmefc_ls_req_op *lsop;
962 struct nvmefc_ls_req *lsreq;
963 struct fcnvme_ls_cr_conn_rqst *conn_rqst;
964 struct fcnvme_ls_cr_conn_acc *conn_acc;
967 lsop = kzalloc((sizeof(*lsop) +
968 ctrl->lport->ops->lsrqst_priv_sz +
969 sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
974 lsreq = &lsop->ls_req;
976 lsreq->private = (void *)&lsop[1];
977 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
978 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
979 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
981 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
982 conn_rqst->desc_list_len = cpu_to_be32(
983 sizeof(struct fcnvme_lsdesc_assoc_id) +
984 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
986 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
987 conn_rqst->associd.desc_len =
989 sizeof(struct fcnvme_lsdesc_assoc_id));
990 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
991 conn_rqst->connect_cmd.desc_tag =
992 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
993 conn_rqst->connect_cmd.desc_len =
995 sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
996 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
997 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
998 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize);
1000 lsop->queue = queue;
1001 lsreq->rqstaddr = conn_rqst;
1002 lsreq->rqstlen = sizeof(*conn_rqst);
1003 lsreq->rspaddr = conn_acc;
1004 lsreq->rsplen = sizeof(*conn_acc);
1005 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1007 ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1009 goto out_free_buffer;
1011 /* process connect LS completion */
1013 /* validate the ACC response */
1014 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1016 else if (conn_acc->hdr.desc_list_len !=
1017 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1018 fcret = VERR_CR_CONN_ACC_LEN;
1019 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1020 fcret = VERR_LSDESC_RQST;
1021 else if (conn_acc->hdr.rqst.desc_len !=
1022 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1023 fcret = VERR_LSDESC_RQST_LEN;
1024 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1025 fcret = VERR_CR_CONN;
1026 else if (conn_acc->connectid.desc_tag !=
1027 cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1028 fcret = VERR_CONN_ID;
1029 else if (conn_acc->connectid.desc_len !=
1030 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1031 fcret = VERR_CONN_ID_LEN;
1036 "q %d connect failed: %s\n",
1037 queue->qnum, validation_errors[fcret]);
1039 queue->connection_id =
1040 be64_to_cpu(conn_acc->connectid.connection_id);
1041 set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1049 "queue %d connect command failed (%d).\n",
1055 nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1057 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1059 __nvme_fc_finish_ls_req(lsop);
1061 /* fc-nvme iniator doesn't care about success or failure of cmd */
1067 * This routine sends a FC-NVME LS to disconnect (aka terminate)
1068 * the FC-NVME Association. Terminating the association also
1069 * terminates the FC-NVME connections (per queue, both admin and io
1070 * queues) that are part of the association. E.g. things are torn
1071 * down, and the related FC-NVME Association ID and Connection IDs
1074 * The behavior of the fc-nvme initiator is such that it's
1075 * understanding of the association and connections will implicitly
1076 * be torn down. The action is implicit as it may be due to a loss of
1077 * connectivity with the fc-nvme target, so you may never get a
1078 * response even if you tried. As such, the action of this routine
1079 * is to asynchronously send the LS, ignore any results of the LS, and
1080 * continue on with terminating the association. If the fc-nvme target
1081 * is present and receives the LS, it too can tear down.
1084 nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1086 struct fcnvme_ls_disconnect_rqst *discon_rqst;
1087 struct fcnvme_ls_disconnect_acc *discon_acc;
1088 struct nvmefc_ls_req_op *lsop;
1089 struct nvmefc_ls_req *lsreq;
1092 lsop = kzalloc((sizeof(*lsop) +
1093 ctrl->lport->ops->lsrqst_priv_sz +
1094 sizeof(*discon_rqst) + sizeof(*discon_acc)),
1097 /* couldn't sent it... too bad */
1100 lsreq = &lsop->ls_req;
1102 lsreq->private = (void *)&lsop[1];
1103 discon_rqst = (struct fcnvme_ls_disconnect_rqst *)
1104 (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1105 discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1];
1107 discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT;
1108 discon_rqst->desc_list_len = cpu_to_be32(
1109 sizeof(struct fcnvme_lsdesc_assoc_id) +
1110 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1112 discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1113 discon_rqst->associd.desc_len =
1115 sizeof(struct fcnvme_lsdesc_assoc_id));
1117 discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1119 discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
1120 FCNVME_LSDESC_DISCONN_CMD);
1121 discon_rqst->discon_cmd.desc_len =
1123 sizeof(struct fcnvme_lsdesc_disconn_cmd));
1124 discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION;
1125 discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id);
1127 lsreq->rqstaddr = discon_rqst;
1128 lsreq->rqstlen = sizeof(*discon_rqst);
1129 lsreq->rspaddr = discon_acc;
1130 lsreq->rsplen = sizeof(*discon_acc);
1131 lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1133 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1134 nvme_fc_disconnect_assoc_done);
1138 /* only meaningful part to terminating the association */
1139 ctrl->association_id = 0;
1143 /* *********************** NVME Ctrl Routines **************************** */
1145 static void __nvme_fc_final_op_cleanup(struct request *rq);
1148 nvme_fc_reinit_request(void *data, struct request *rq)
1150 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1151 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1153 memset(cmdiu, 0, sizeof(*cmdiu));
1154 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1155 cmdiu->fc_id = NVME_CMD_FC_ID;
1156 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1157 memset(&op->rsp_iu, 0, sizeof(op->rsp_iu));
1163 __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1164 struct nvme_fc_fcp_op *op)
1166 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1167 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1168 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1169 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1171 atomic_set(&op->state, FCPOP_STATE_UNINIT);
1175 nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1176 unsigned int hctx_idx)
1178 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1180 return __nvme_fc_exit_request(set->driver_data, op);
1184 __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1188 state = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1189 if (state != FCPOP_STATE_ACTIVE) {
1190 atomic_set(&op->state, state);
1194 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1195 &ctrl->rport->remoteport,
1196 op->queue->lldd_handle,
1203 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1205 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
1206 unsigned long flags;
1209 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
1210 if (atomic_read(&aen_op->state) != FCPOP_STATE_ACTIVE)
1213 spin_lock_irqsave(&ctrl->lock, flags);
1214 if (ctrl->flags & FCCTRL_TERMIO) {
1216 aen_op->flags |= FCOP_FLAGS_TERMIO;
1218 spin_unlock_irqrestore(&ctrl->lock, flags);
1220 ret = __nvme_fc_abort_op(ctrl, aen_op);
1223 * if __nvme_fc_abort_op failed the io wasn't
1224 * active. Thus this call path is running in
1225 * parallel to the io complete. Treat as non-error.
1228 /* back out the flags/counters */
1229 spin_lock_irqsave(&ctrl->lock, flags);
1230 if (ctrl->flags & FCCTRL_TERMIO)
1232 aen_op->flags &= ~FCOP_FLAGS_TERMIO;
1233 spin_unlock_irqrestore(&ctrl->lock, flags);
1240 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1241 struct nvme_fc_fcp_op *op)
1243 unsigned long flags;
1244 bool complete_rq = false;
1246 spin_lock_irqsave(&ctrl->lock, flags);
1247 if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) {
1248 if (ctrl->flags & FCCTRL_TERMIO)
1251 if (op->flags & FCOP_FLAGS_RELEASED)
1254 op->flags |= FCOP_FLAGS_COMPLETE;
1255 spin_unlock_irqrestore(&ctrl->lock, flags);
1261 nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1263 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1264 struct request *rq = op->rq;
1265 struct nvmefc_fcp_req *freq = &op->fcp_req;
1266 struct nvme_fc_ctrl *ctrl = op->ctrl;
1267 struct nvme_fc_queue *queue = op->queue;
1268 struct nvme_completion *cqe = &op->rsp_iu.cqe;
1269 struct nvme_command *sqe = &op->cmd_iu.sqe;
1270 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
1271 union nvme_result result;
1276 * The current linux implementation of a nvme controller
1277 * allocates a single tag set for all io queues and sizes
1278 * the io queues to fully hold all possible tags. Thus, the
1279 * implementation does not reference or care about the sqhd
1280 * value as it never needs to use the sqhd/sqtail pointers
1281 * for submission pacing.
1283 * This affects the FC-NVME implementation in two ways:
1284 * 1) As the value doesn't matter, we don't need to waste
1285 * cycles extracting it from ERSPs and stamping it in the
1286 * cases where the transport fabricates CQEs on successful
1288 * 2) The FC-NVME implementation requires that delivery of
1289 * ERSP completions are to go back to the nvme layer in order
1290 * relative to the rsn, such that the sqhd value will always
1291 * be "in order" for the nvme layer. As the nvme layer in
1292 * linux doesn't care about sqhd, there's no need to return
1296 * As the core nvme layer in linux currently does not look at
1297 * every field in the cqe - in cases where the FC transport must
1298 * fabricate a CQE, the following fields will not be set as they
1299 * are not referenced:
1300 * cqe.sqid, cqe.sqhd, cqe.command_id
1303 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1304 sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1306 if (atomic_read(&op->state) == FCPOP_STATE_ABORTED)
1307 status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1308 else if (freq->status)
1309 status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
1312 * For the linux implementation, if we have an unsuccesful
1313 * status, they blk-mq layer can typically be called with the
1314 * non-zero status and the content of the cqe isn't important.
1320 * command completed successfully relative to the wire
1321 * protocol. However, validate anything received and
1322 * extract the status and result from the cqe (create it
1326 switch (freq->rcv_rsplen) {
1329 case NVME_FC_SIZEOF_ZEROS_RSP:
1331 * No response payload or 12 bytes of payload (which
1332 * should all be zeros) are considered successful and
1333 * no payload in the CQE by the transport.
1335 if (freq->transferred_length !=
1336 be32_to_cpu(op->cmd_iu.data_len)) {
1337 status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
1343 case sizeof(struct nvme_fc_ersp_iu):
1345 * The ERSP IU contains a full completion with CQE.
1346 * Validate ERSP IU and look at cqe.
1348 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1349 (freq->rcv_rsplen / 4) ||
1350 be32_to_cpu(op->rsp_iu.xfrd_len) !=
1351 freq->transferred_length ||
1352 op->rsp_iu.status_code ||
1353 sqe->common.command_id != cqe->command_id)) {
1354 status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
1357 result = cqe->result;
1358 status = cqe->status;
1362 status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
1367 if (op->flags & FCOP_FLAGS_AEN) {
1368 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
1369 complete_rq = __nvme_fc_fcpop_chk_teardowns(ctrl, op);
1370 atomic_set(&op->state, FCPOP_STATE_IDLE);
1371 op->flags = FCOP_FLAGS_AEN; /* clear other flags */
1372 nvme_fc_ctrl_put(ctrl);
1376 complete_rq = __nvme_fc_fcpop_chk_teardowns(ctrl, op);
1378 if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) {
1379 status = cpu_to_le16(NVME_SC_ABORT_REQ);
1380 if (blk_queue_dying(rq->q))
1381 status |= cpu_to_le16(NVME_SC_DNR);
1383 nvme_end_request(rq, status, result);
1385 __nvme_fc_final_op_cleanup(rq);
1389 __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
1390 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
1391 struct request *rq, u32 rqno)
1393 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1396 memset(op, 0, sizeof(*op));
1397 op->fcp_req.cmdaddr = &op->cmd_iu;
1398 op->fcp_req.cmdlen = sizeof(op->cmd_iu);
1399 op->fcp_req.rspaddr = &op->rsp_iu;
1400 op->fcp_req.rsplen = sizeof(op->rsp_iu);
1401 op->fcp_req.done = nvme_fc_fcpio_done;
1402 op->fcp_req.first_sgl = (struct scatterlist *)&op[1];
1403 op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE];
1409 cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1410 cmdiu->fc_id = NVME_CMD_FC_ID;
1411 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1413 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
1414 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
1415 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
1417 "FCP Op failed - cmdiu dma mapping failed.\n");
1422 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
1423 &op->rsp_iu, sizeof(op->rsp_iu),
1425 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
1427 "FCP Op failed - rspiu dma mapping failed.\n");
1431 atomic_set(&op->state, FCPOP_STATE_IDLE);
1437 nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
1438 unsigned int hctx_idx, unsigned int numa_node)
1440 struct nvme_fc_ctrl *ctrl = set->driver_data;
1441 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1442 struct nvme_fc_queue *queue = &ctrl->queues[hctx_idx+1];
1444 return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
1448 nvme_fc_init_admin_request(struct blk_mq_tag_set *set, struct request *rq,
1449 unsigned int hctx_idx, unsigned int numa_node)
1451 struct nvme_fc_ctrl *ctrl = set->driver_data;
1452 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1453 struct nvme_fc_queue *queue = &ctrl->queues[0];
1455 return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
1459 nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
1461 struct nvme_fc_fcp_op *aen_op;
1462 struct nvme_fc_cmd_iu *cmdiu;
1463 struct nvme_command *sqe;
1467 aen_op = ctrl->aen_ops;
1468 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
1469 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
1474 cmdiu = &aen_op->cmd_iu;
1476 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
1477 aen_op, (struct request *)NULL,
1478 (AEN_CMDID_BASE + i));
1484 aen_op->flags = FCOP_FLAGS_AEN;
1485 aen_op->fcp_req.first_sgl = NULL; /* no sg list */
1486 aen_op->fcp_req.private = private;
1488 memset(sqe, 0, sizeof(*sqe));
1489 sqe->common.opcode = nvme_admin_async_event;
1490 /* Note: core layer may overwrite the sqe.command_id value */
1491 sqe->common.command_id = AEN_CMDID_BASE + i;
1497 nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
1499 struct nvme_fc_fcp_op *aen_op;
1502 aen_op = ctrl->aen_ops;
1503 for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
1504 if (!aen_op->fcp_req.private)
1507 __nvme_fc_exit_request(ctrl, aen_op);
1509 kfree(aen_op->fcp_req.private);
1510 aen_op->fcp_req.private = NULL;
1515 __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
1518 struct nvme_fc_queue *queue = &ctrl->queues[qidx];
1520 hctx->driver_data = queue;
1525 nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1526 unsigned int hctx_idx)
1528 struct nvme_fc_ctrl *ctrl = data;
1530 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
1536 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1537 unsigned int hctx_idx)
1539 struct nvme_fc_ctrl *ctrl = data;
1541 __nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
1547 nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx, size_t queue_size)
1549 struct nvme_fc_queue *queue;
1551 queue = &ctrl->queues[idx];
1552 memset(queue, 0, sizeof(*queue));
1555 atomic_set(&queue->csn, 1);
1556 queue->dev = ctrl->dev;
1559 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1561 queue->cmnd_capsule_len = sizeof(struct nvme_command);
1563 queue->queue_size = queue_size;
1566 * Considered whether we should allocate buffers for all SQEs
1567 * and CQEs and dma map them - mapping their respective entries
1568 * into the request structures (kernel vm addr and dma address)
1569 * thus the driver could use the buffers/mappings directly.
1570 * It only makes sense if the LLDD would use them for its
1571 * messaging api. It's very unlikely most adapter api's would use
1572 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1573 * structures were used instead.
1578 * This routine terminates a queue at the transport level.
1579 * The transport has already ensured that all outstanding ios on
1580 * the queue have been terminated.
1581 * The transport will send a Disconnect LS request to terminate
1582 * the queue's connection. Termination of the admin queue will also
1583 * terminate the association at the target.
1586 nvme_fc_free_queue(struct nvme_fc_queue *queue)
1588 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
1592 * Current implementation never disconnects a single queue.
1593 * It always terminates a whole association. So there is never
1594 * a disconnect(queue) LS sent to the target.
1597 queue->connection_id = 0;
1598 clear_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1602 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
1603 struct nvme_fc_queue *queue, unsigned int qidx)
1605 if (ctrl->lport->ops->delete_queue)
1606 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
1607 queue->lldd_handle);
1608 queue->lldd_handle = NULL;
1612 nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
1616 for (i = 1; i < ctrl->queue_count; i++)
1617 nvme_fc_free_queue(&ctrl->queues[i]);
1621 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
1622 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
1626 queue->lldd_handle = NULL;
1627 if (ctrl->lport->ops->create_queue)
1628 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
1629 qidx, qsize, &queue->lldd_handle);
1635 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
1637 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->queue_count - 1];
1640 for (i = ctrl->queue_count - 1; i >= 1; i--, queue--)
1641 __nvme_fc_delete_hw_queue(ctrl, queue, i);
1645 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1647 struct nvme_fc_queue *queue = &ctrl->queues[1];
1650 for (i = 1; i < ctrl->queue_count; i++, queue++) {
1651 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
1660 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
1665 nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1669 for (i = 1; i < ctrl->queue_count; i++) {
1670 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
1674 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
1683 nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
1687 for (i = 1; i < ctrl->queue_count; i++)
1688 nvme_fc_init_queue(ctrl, i, ctrl->ctrl.sqsize);
1692 nvme_fc_ctrl_free(struct kref *ref)
1694 struct nvme_fc_ctrl *ctrl =
1695 container_of(ref, struct nvme_fc_ctrl, ref);
1696 unsigned long flags;
1698 if (ctrl->ctrl.tagset) {
1699 blk_cleanup_queue(ctrl->ctrl.connect_q);
1700 blk_mq_free_tag_set(&ctrl->tag_set);
1703 /* remove from rport list */
1704 spin_lock_irqsave(&ctrl->rport->lock, flags);
1705 list_del(&ctrl->ctrl_list);
1706 spin_unlock_irqrestore(&ctrl->rport->lock, flags);
1708 blk_cleanup_queue(ctrl->ctrl.admin_q);
1709 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1711 kfree(ctrl->queues);
1713 put_device(ctrl->dev);
1714 nvme_fc_rport_put(ctrl->rport);
1716 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
1717 if (ctrl->ctrl.opts)
1718 nvmf_free_options(ctrl->ctrl.opts);
1723 nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
1725 kref_put(&ctrl->ref, nvme_fc_ctrl_free);
1729 nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
1731 return kref_get_unless_zero(&ctrl->ref);
1735 * All accesses from nvme core layer done - can now free the
1736 * controller. Called after last nvme_put_ctrl() call
1739 nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
1741 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
1743 WARN_ON(nctrl != &ctrl->ctrl);
1745 nvme_fc_ctrl_put(ctrl);
1749 nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
1751 dev_warn(ctrl->ctrl.device,
1752 "NVME-FC{%d}: transport association error detected: %s\n",
1753 ctrl->cnum, errmsg);
1754 dev_info(ctrl->ctrl.device,
1755 "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
1757 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) {
1758 dev_err(ctrl->ctrl.device,
1759 "NVME-FC{%d}: error_recovery: Couldn't change state "
1760 "to RECONNECTING\n", ctrl->cnum);
1764 if (!queue_work(nvme_fc_wq, &ctrl->reset_work))
1765 dev_err(ctrl->ctrl.device,
1766 "NVME-FC{%d}: error_recovery: Failed to schedule "
1767 "reset work\n", ctrl->cnum);
1770 static enum blk_eh_timer_return
1771 nvme_fc_timeout(struct request *rq, bool reserved)
1773 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1774 struct nvme_fc_ctrl *ctrl = op->ctrl;
1778 return BLK_EH_RESET_TIMER;
1780 ret = __nvme_fc_abort_op(ctrl, op);
1782 /* io wasn't active to abort consider it done */
1783 return BLK_EH_HANDLED;
1786 * we can't individually ABTS an io without affecting the queue,
1787 * thus killing the queue, adn thus the association.
1788 * So resolve by performing a controller reset, which will stop
1789 * the host/io stack, terminate the association on the link,
1790 * and recreate an association on the link.
1792 nvme_fc_error_recovery(ctrl, "io timeout error");
1794 return BLK_EH_HANDLED;
1798 nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1799 struct nvme_fc_fcp_op *op)
1801 struct nvmefc_fcp_req *freq = &op->fcp_req;
1802 enum dma_data_direction dir;
1807 if (!blk_rq_payload_bytes(rq))
1810 freq->sg_table.sgl = freq->first_sgl;
1811 ret = sg_alloc_table_chained(&freq->sg_table,
1812 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl);
1816 op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
1817 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
1818 dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1819 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
1821 if (unlikely(freq->sg_cnt <= 0)) {
1822 sg_free_table_chained(&freq->sg_table, true);
1828 * TODO: blk_integrity_rq(rq) for DIF
1834 nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1835 struct nvme_fc_fcp_op *op)
1837 struct nvmefc_fcp_req *freq = &op->fcp_req;
1842 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
1843 ((rq_data_dir(rq) == WRITE) ?
1844 DMA_TO_DEVICE : DMA_FROM_DEVICE));
1846 nvme_cleanup_cmd(rq);
1848 sg_free_table_chained(&freq->sg_table, true);
1854 * In FC, the queue is a logical thing. At transport connect, the target
1855 * creates its "queue" and returns a handle that is to be given to the
1856 * target whenever it posts something to the corresponding SQ. When an
1857 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
1858 * command contained within the SQE, an io, and assigns a FC exchange
1859 * to it. The SQE and the associated SQ handle are sent in the initial
1860 * CMD IU sents on the exchange. All transfers relative to the io occur
1861 * as part of the exchange. The CQE is the last thing for the io,
1862 * which is transferred (explicitly or implicitly) with the RSP IU
1863 * sent on the exchange. After the CQE is received, the FC exchange is
1864 * terminaed and the Exchange may be used on a different io.
1866 * The transport to LLDD api has the transport making a request for a
1867 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
1868 * resource and transfers the command. The LLDD will then process all
1869 * steps to complete the io. Upon completion, the transport done routine
1872 * So - while the operation is outstanding to the LLDD, there is a link
1873 * level FC exchange resource that is also outstanding. This must be
1874 * considered in all cleanup operations.
1877 nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1878 struct nvme_fc_fcp_op *op, u32 data_len,
1879 enum nvmefc_fcp_datadir io_dir)
1881 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1882 struct nvme_command *sqe = &cmdiu->sqe;
1887 * before attempting to send the io, check to see if we believe
1888 * the target device is present
1890 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1891 return BLK_MQ_RQ_QUEUE_ERROR;
1893 if (!nvme_fc_ctrl_get(ctrl))
1894 return BLK_MQ_RQ_QUEUE_ERROR;
1896 /* format the FC-NVME CMD IU and fcp_req */
1897 cmdiu->connection_id = cpu_to_be64(queue->connection_id);
1898 csn = atomic_inc_return(&queue->csn);
1899 cmdiu->csn = cpu_to_be32(csn);
1900 cmdiu->data_len = cpu_to_be32(data_len);
1902 case NVMEFC_FCP_WRITE:
1903 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
1905 case NVMEFC_FCP_READ:
1906 cmdiu->flags = FCNVME_CMD_FLAGS_READ;
1908 case NVMEFC_FCP_NODATA:
1912 op->fcp_req.payload_length = data_len;
1913 op->fcp_req.io_dir = io_dir;
1914 op->fcp_req.transferred_length = 0;
1915 op->fcp_req.rcv_rsplen = 0;
1916 op->fcp_req.status = NVME_SC_SUCCESS;
1917 op->fcp_req.sqid = cpu_to_le16(queue->qnum);
1920 * validate per fabric rules, set fields mandated by fabric spec
1921 * as well as those by FC-NVME spec.
1923 WARN_ON_ONCE(sqe->common.metadata);
1924 WARN_ON_ONCE(sqe->common.dptr.prp1);
1925 WARN_ON_ONCE(sqe->common.dptr.prp2);
1926 sqe->common.flags |= NVME_CMD_SGL_METABUF;
1929 * format SQE DPTR field per FC-NVME rules
1930 * type=data block descr; subtype=offset;
1931 * offset is currently 0.
1933 sqe->rw.dptr.sgl.type = NVME_SGL_FMT_OFFSET;
1934 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
1935 sqe->rw.dptr.sgl.addr = 0;
1937 if (!(op->flags & FCOP_FLAGS_AEN)) {
1938 ret = nvme_fc_map_data(ctrl, op->rq, op);
1940 nvme_cleanup_cmd(op->rq);
1941 nvme_fc_ctrl_put(ctrl);
1942 return (ret == -ENOMEM || ret == -EAGAIN) ?
1943 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
1947 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
1948 sizeof(op->cmd_iu), DMA_TO_DEVICE);
1950 atomic_set(&op->state, FCPOP_STATE_ACTIVE);
1952 if (!(op->flags & FCOP_FLAGS_AEN))
1953 blk_mq_start_request(op->rq);
1955 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
1956 &ctrl->rport->remoteport,
1957 queue->lldd_handle, &op->fcp_req);
1960 if (op->rq) { /* normal request */
1961 nvme_fc_unmap_data(ctrl, op->rq, op);
1962 nvme_cleanup_cmd(op->rq);
1964 /* else - aen. no cleanup needed */
1966 nvme_fc_ctrl_put(ctrl);
1969 return BLK_MQ_RQ_QUEUE_ERROR;
1972 blk_mq_stop_hw_queues(op->rq->q);
1973 blk_mq_delay_queue(queue->hctx, NVMEFC_QUEUE_DELAY);
1975 return BLK_MQ_RQ_QUEUE_BUSY;
1978 return BLK_MQ_RQ_QUEUE_OK;
1982 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
1983 const struct blk_mq_queue_data *bd)
1985 struct nvme_ns *ns = hctx->queue->queuedata;
1986 struct nvme_fc_queue *queue = hctx->driver_data;
1987 struct nvme_fc_ctrl *ctrl = queue->ctrl;
1988 struct request *rq = bd->rq;
1989 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1990 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1991 struct nvme_command *sqe = &cmdiu->sqe;
1992 enum nvmefc_fcp_datadir io_dir;
1996 ret = nvme_setup_cmd(ns, rq, sqe);
2000 data_len = blk_rq_payload_bytes(rq);
2002 io_dir = ((rq_data_dir(rq) == WRITE) ?
2003 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2005 io_dir = NVMEFC_FCP_NODATA;
2007 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2010 static struct blk_mq_tags *
2011 nvme_fc_tagset(struct nvme_fc_queue *queue)
2013 if (queue->qnum == 0)
2014 return queue->ctrl->admin_tag_set.tags[queue->qnum];
2016 return queue->ctrl->tag_set.tags[queue->qnum - 1];
2020 nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
2023 struct nvme_fc_queue *queue = hctx->driver_data;
2024 struct nvme_fc_ctrl *ctrl = queue->ctrl;
2025 struct request *req;
2026 struct nvme_fc_fcp_op *op;
2028 req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag);
2032 op = blk_mq_rq_to_pdu(req);
2034 if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) &&
2035 (ctrl->lport->ops->poll_queue))
2036 ctrl->lport->ops->poll_queue(&ctrl->lport->localport,
2037 queue->lldd_handle);
2039 return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE));
2043 nvme_fc_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
2045 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2046 struct nvme_fc_fcp_op *aen_op;
2047 unsigned long flags;
2048 bool terminating = false;
2051 if (aer_idx > NVME_FC_NR_AEN_COMMANDS)
2054 spin_lock_irqsave(&ctrl->lock, flags);
2055 if (ctrl->flags & FCCTRL_TERMIO)
2057 spin_unlock_irqrestore(&ctrl->lock, flags);
2062 aen_op = &ctrl->aen_ops[aer_idx];
2064 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2067 dev_err(ctrl->ctrl.device,
2068 "failed async event work [%d]\n", aer_idx);
2072 __nvme_fc_final_op_cleanup(struct request *rq)
2074 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2075 struct nvme_fc_ctrl *ctrl = op->ctrl;
2077 atomic_set(&op->state, FCPOP_STATE_IDLE);
2078 op->flags &= ~(FCOP_FLAGS_TERMIO | FCOP_FLAGS_RELEASED |
2079 FCOP_FLAGS_COMPLETE);
2081 nvme_cleanup_cmd(rq);
2082 nvme_fc_unmap_data(ctrl, rq, op);
2083 nvme_complete_rq(rq);
2084 nvme_fc_ctrl_put(ctrl);
2089 nvme_fc_complete_rq(struct request *rq)
2091 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2092 struct nvme_fc_ctrl *ctrl = op->ctrl;
2093 unsigned long flags;
2094 bool completed = false;
2097 * the core layer, on controller resets after calling
2098 * nvme_shutdown_ctrl(), calls complete_rq without our
2099 * calling blk_mq_complete_request(), thus there may still
2100 * be live i/o outstanding with the LLDD. Means transport has
2101 * to track complete calls vs fcpio_done calls to know what
2102 * path to take on completes and dones.
2104 spin_lock_irqsave(&ctrl->lock, flags);
2105 if (op->flags & FCOP_FLAGS_COMPLETE)
2108 op->flags |= FCOP_FLAGS_RELEASED;
2109 spin_unlock_irqrestore(&ctrl->lock, flags);
2112 __nvme_fc_final_op_cleanup(rq);
2116 * This routine is used by the transport when it needs to find active
2117 * io on a queue that is to be terminated. The transport uses
2118 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2119 * this routine to kill them on a 1 by 1 basis.
2121 * As FC allocates FC exchange for each io, the transport must contact
2122 * the LLDD to terminate the exchange, thus releasing the FC exchange.
2123 * After terminating the exchange the LLDD will call the transport's
2124 * normal io done path for the request, but it will have an aborted
2125 * status. The done path will return the io request back to the block
2126 * layer with an error status.
2129 nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2131 struct nvme_ctrl *nctrl = data;
2132 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2133 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2134 unsigned long flags;
2137 if (!blk_mq_request_started(req))
2140 spin_lock_irqsave(&ctrl->lock, flags);
2141 if (ctrl->flags & FCCTRL_TERMIO) {
2143 op->flags |= FCOP_FLAGS_TERMIO;
2145 spin_unlock_irqrestore(&ctrl->lock, flags);
2147 status = __nvme_fc_abort_op(ctrl, op);
2150 * if __nvme_fc_abort_op failed the io wasn't
2151 * active. Thus this call path is running in
2152 * parallel to the io complete. Treat as non-error.
2155 /* back out the flags/counters */
2156 spin_lock_irqsave(&ctrl->lock, flags);
2157 if (ctrl->flags & FCCTRL_TERMIO)
2159 op->flags &= ~FCOP_FLAGS_TERMIO;
2160 spin_unlock_irqrestore(&ctrl->lock, flags);
2166 static const struct blk_mq_ops nvme_fc_mq_ops = {
2167 .queue_rq = nvme_fc_queue_rq,
2168 .complete = nvme_fc_complete_rq,
2169 .init_request = nvme_fc_init_request,
2170 .exit_request = nvme_fc_exit_request,
2171 .reinit_request = nvme_fc_reinit_request,
2172 .init_hctx = nvme_fc_init_hctx,
2173 .poll = nvme_fc_poll,
2174 .timeout = nvme_fc_timeout,
2178 nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2180 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2183 ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
2185 dev_info(ctrl->ctrl.device,
2186 "set_queue_count failed: %d\n", ret);
2190 ctrl->queue_count = opts->nr_io_queues + 1;
2191 if (!opts->nr_io_queues)
2194 dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n",
2195 opts->nr_io_queues);
2197 nvme_fc_init_io_queues(ctrl);
2199 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2200 ctrl->tag_set.ops = &nvme_fc_mq_ops;
2201 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2202 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2203 ctrl->tag_set.numa_node = NUMA_NO_NODE;
2204 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2205 ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2207 sizeof(struct scatterlist)) +
2208 ctrl->lport->ops->fcprqst_priv_sz;
2209 ctrl->tag_set.driver_data = ctrl;
2210 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
2211 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2213 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2217 ctrl->ctrl.tagset = &ctrl->tag_set;
2219 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2220 if (IS_ERR(ctrl->ctrl.connect_q)) {
2221 ret = PTR_ERR(ctrl->ctrl.connect_q);
2222 goto out_free_tag_set;
2225 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2227 goto out_cleanup_blk_queue;
2229 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2231 goto out_delete_hw_queues;
2235 out_delete_hw_queues:
2236 nvme_fc_delete_hw_io_queues(ctrl);
2237 out_cleanup_blk_queue:
2238 nvme_stop_keep_alive(&ctrl->ctrl);
2239 blk_cleanup_queue(ctrl->ctrl.connect_q);
2241 blk_mq_free_tag_set(&ctrl->tag_set);
2242 nvme_fc_free_io_queues(ctrl);
2244 /* force put free routine to ignore io queues */
2245 ctrl->ctrl.tagset = NULL;
2251 nvme_fc_reinit_io_queues(struct nvme_fc_ctrl *ctrl)
2253 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2256 ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
2258 dev_info(ctrl->ctrl.device,
2259 "set_queue_count failed: %d\n", ret);
2263 /* check for io queues existing */
2264 if (ctrl->queue_count == 1)
2267 dev_info(ctrl->ctrl.device, "Recreating %d I/O queues.\n",
2268 opts->nr_io_queues);
2270 nvme_fc_init_io_queues(ctrl);
2272 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
2274 goto out_free_io_queues;
2276 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2278 goto out_free_io_queues;
2280 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2282 goto out_delete_hw_queues;
2286 out_delete_hw_queues:
2287 nvme_fc_delete_hw_io_queues(ctrl);
2289 nvme_fc_free_io_queues(ctrl);
2294 * This routine restarts the controller on the host side, and
2295 * on the link side, recreates the controller association.
2298 nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
2300 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2305 ctrl->connect_attempts++;
2308 * Create the admin queue
2311 nvme_fc_init_queue(ctrl, 0, NVME_FC_AQ_BLKMQ_DEPTH);
2313 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
2314 NVME_FC_AQ_BLKMQ_DEPTH);
2316 goto out_free_queue;
2318 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
2319 NVME_FC_AQ_BLKMQ_DEPTH,
2320 (NVME_FC_AQ_BLKMQ_DEPTH / 4));
2322 goto out_delete_hw_queue;
2324 if (ctrl->ctrl.state != NVME_CTRL_NEW)
2325 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
2327 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
2329 goto out_disconnect_admin_queue;
2332 * Check controller capabilities
2334 * todo:- add code to check if ctrl attributes changed from
2335 * prior connection values
2338 ret = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
2340 dev_err(ctrl->ctrl.device,
2341 "prop_get NVME_REG_CAP failed\n");
2342 goto out_disconnect_admin_queue;
2346 min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
2348 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
2350 goto out_disconnect_admin_queue;
2352 segs = min_t(u32, NVME_FC_MAX_SEGMENTS,
2353 ctrl->lport->ops->max_sgl_segments);
2354 ctrl->ctrl.max_hw_sectors = (segs - 1) << (PAGE_SHIFT - 9);
2356 ret = nvme_init_identify(&ctrl->ctrl);
2358 goto out_disconnect_admin_queue;
2362 /* FC-NVME does not have other data in the capsule */
2363 if (ctrl->ctrl.icdoff) {
2364 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
2366 goto out_disconnect_admin_queue;
2369 nvme_start_keep_alive(&ctrl->ctrl);
2371 /* FC-NVME supports normal SGL Data Block Descriptors */
2373 if (opts->queue_size > ctrl->ctrl.maxcmd) {
2374 /* warn if maxcmd is lower than queue_size */
2375 dev_warn(ctrl->ctrl.device,
2376 "queue_size %zu > ctrl maxcmd %u, reducing "
2378 opts->queue_size, ctrl->ctrl.maxcmd);
2379 opts->queue_size = ctrl->ctrl.maxcmd;
2382 ret = nvme_fc_init_aen_ops(ctrl);
2384 goto out_term_aen_ops;
2387 * Create the io queues
2390 if (ctrl->queue_count > 1) {
2391 if (ctrl->ctrl.state == NVME_CTRL_NEW)
2392 ret = nvme_fc_create_io_queues(ctrl);
2394 ret = nvme_fc_reinit_io_queues(ctrl);
2396 goto out_term_aen_ops;
2399 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
2400 WARN_ON_ONCE(!changed);
2402 ctrl->connect_attempts = 0;
2404 kref_get(&ctrl->ctrl.kref);
2406 if (ctrl->queue_count > 1) {
2407 nvme_start_queues(&ctrl->ctrl);
2408 nvme_queue_scan(&ctrl->ctrl);
2409 nvme_queue_async_events(&ctrl->ctrl);
2412 return 0; /* Success */
2415 nvme_fc_term_aen_ops(ctrl);
2416 nvme_stop_keep_alive(&ctrl->ctrl);
2417 out_disconnect_admin_queue:
2418 /* send a Disconnect(association) LS to fc-nvme target */
2419 nvme_fc_xmt_disconnect_assoc(ctrl);
2420 out_delete_hw_queue:
2421 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2423 nvme_fc_free_queue(&ctrl->queues[0]);
2429 * This routine stops operation of the controller on the host side.
2430 * On the host os stack side: Admin and IO queues are stopped,
2431 * outstanding ios on them terminated via FC ABTS.
2432 * On the link side: the association is terminated.
2435 nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
2437 unsigned long flags;
2439 nvme_stop_keep_alive(&ctrl->ctrl);
2441 spin_lock_irqsave(&ctrl->lock, flags);
2442 ctrl->flags |= FCCTRL_TERMIO;
2444 spin_unlock_irqrestore(&ctrl->lock, flags);
2447 * If io queues are present, stop them and terminate all outstanding
2448 * ios on them. As FC allocates FC exchange for each io, the
2449 * transport must contact the LLDD to terminate the exchange,
2450 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2451 * to tell us what io's are busy and invoke a transport routine
2452 * to kill them with the LLDD. After terminating the exchange
2453 * the LLDD will call the transport's normal io done path, but it
2454 * will have an aborted status. The done path will return the
2455 * io requests back to the block layer as part of normal completions
2456 * (but with error status).
2458 if (ctrl->queue_count > 1) {
2459 nvme_stop_queues(&ctrl->ctrl);
2460 blk_mq_tagset_busy_iter(&ctrl->tag_set,
2461 nvme_fc_terminate_exchange, &ctrl->ctrl);
2465 * Other transports, which don't have link-level contexts bound
2466 * to sqe's, would try to gracefully shutdown the controller by
2467 * writing the registers for shutdown and polling (call
2468 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
2469 * just aborted and we will wait on those contexts, and given
2470 * there was no indication of how live the controlelr is on the
2471 * link, don't send more io to create more contexts for the
2472 * shutdown. Let the controller fail via keepalive failure if
2473 * its still present.
2477 * clean up the admin queue. Same thing as above.
2478 * use blk_mq_tagset_busy_itr() and the transport routine to
2479 * terminate the exchanges.
2481 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
2482 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2483 nvme_fc_terminate_exchange, &ctrl->ctrl);
2485 /* kill the aens as they are a separate path */
2486 nvme_fc_abort_aen_ops(ctrl);
2488 /* wait for all io that had to be aborted */
2489 spin_lock_irqsave(&ctrl->lock, flags);
2490 while (ctrl->iocnt) {
2491 spin_unlock_irqrestore(&ctrl->lock, flags);
2493 spin_lock_irqsave(&ctrl->lock, flags);
2495 ctrl->flags &= ~FCCTRL_TERMIO;
2496 spin_unlock_irqrestore(&ctrl->lock, flags);
2498 nvme_fc_term_aen_ops(ctrl);
2501 * send a Disconnect(association) LS to fc-nvme target
2502 * Note: could have been sent at top of process, but
2503 * cleaner on link traffic if after the aborts complete.
2504 * Note: if association doesn't exist, association_id will be 0
2506 if (ctrl->association_id)
2507 nvme_fc_xmt_disconnect_assoc(ctrl);
2509 if (ctrl->ctrl.tagset) {
2510 nvme_fc_delete_hw_io_queues(ctrl);
2511 nvme_fc_free_io_queues(ctrl);
2514 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2515 nvme_fc_free_queue(&ctrl->queues[0]);
2519 nvme_fc_delete_ctrl_work(struct work_struct *work)
2521 struct nvme_fc_ctrl *ctrl =
2522 container_of(work, struct nvme_fc_ctrl, delete_work);
2524 cancel_work_sync(&ctrl->reset_work);
2525 cancel_delayed_work_sync(&ctrl->connect_work);
2528 * kill the association on the link side. this will block
2529 * waiting for io to terminate
2531 nvme_fc_delete_association(ctrl);
2534 * tear down the controller
2535 * This will result in the last reference on the nvme ctrl to
2536 * expire, calling the transport nvme_fc_nvme_ctrl_freed() callback.
2537 * From there, the transport will tear down it's logical queues and
2540 nvme_uninit_ctrl(&ctrl->ctrl);
2542 nvme_put_ctrl(&ctrl->ctrl);
2546 __nvme_fc_del_ctrl(struct nvme_fc_ctrl *ctrl)
2548 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
2551 if (!queue_work(nvme_fc_wq, &ctrl->delete_work))
2558 * Request from nvme core layer to delete the controller
2561 nvme_fc_del_nvme_ctrl(struct nvme_ctrl *nctrl)
2563 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2566 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
2569 ret = __nvme_fc_del_ctrl(ctrl);
2572 flush_workqueue(nvme_fc_wq);
2574 nvme_put_ctrl(&ctrl->ctrl);
2580 nvme_fc_reset_ctrl_work(struct work_struct *work)
2582 struct nvme_fc_ctrl *ctrl =
2583 container_of(work, struct nvme_fc_ctrl, reset_work);
2586 /* will block will waiting for io to terminate */
2587 nvme_fc_delete_association(ctrl);
2589 ret = nvme_fc_create_association(ctrl);
2591 dev_warn(ctrl->ctrl.device,
2592 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
2594 if (ctrl->connect_attempts >= NVME_FC_MAX_CONNECT_ATTEMPTS) {
2595 dev_warn(ctrl->ctrl.device,
2596 "NVME-FC{%d}: Max reconnect attempts (%d) "
2597 "reached. Removing controller\n",
2598 ctrl->cnum, ctrl->connect_attempts);
2600 if (!nvme_change_ctrl_state(&ctrl->ctrl,
2601 NVME_CTRL_DELETING)) {
2602 dev_err(ctrl->ctrl.device,
2603 "NVME-FC{%d}: failed to change state "
2604 "to DELETING\n", ctrl->cnum);
2608 WARN_ON(!queue_work(nvme_fc_wq, &ctrl->delete_work));
2612 dev_warn(ctrl->ctrl.device,
2613 "NVME-FC{%d}: Reconnect attempt in %d seconds.\n",
2614 ctrl->cnum, ctrl->reconnect_delay);
2615 queue_delayed_work(nvme_fc_wq, &ctrl->connect_work,
2616 ctrl->reconnect_delay * HZ);
2618 dev_info(ctrl->ctrl.device,
2619 "NVME-FC{%d}: controller reset complete\n", ctrl->cnum);
2623 * called by the nvme core layer, for sysfs interface that requests
2624 * a reset of the nvme controller
2627 nvme_fc_reset_nvme_ctrl(struct nvme_ctrl *nctrl)
2629 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2631 dev_warn(ctrl->ctrl.device,
2632 "NVME-FC{%d}: admin requested controller reset\n", ctrl->cnum);
2634 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
2637 if (!queue_work(nvme_fc_wq, &ctrl->reset_work))
2640 flush_work(&ctrl->reset_work);
2645 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
2647 .module = THIS_MODULE,
2649 .reg_read32 = nvmf_reg_read32,
2650 .reg_read64 = nvmf_reg_read64,
2651 .reg_write32 = nvmf_reg_write32,
2652 .reset_ctrl = nvme_fc_reset_nvme_ctrl,
2653 .free_ctrl = nvme_fc_nvme_ctrl_freed,
2654 .submit_async_event = nvme_fc_submit_async_event,
2655 .delete_ctrl = nvme_fc_del_nvme_ctrl,
2656 .get_subsysnqn = nvmf_get_subsysnqn,
2657 .get_address = nvmf_get_address,
2661 nvme_fc_connect_ctrl_work(struct work_struct *work)
2665 struct nvme_fc_ctrl *ctrl =
2666 container_of(to_delayed_work(work),
2667 struct nvme_fc_ctrl, connect_work);
2669 ret = nvme_fc_create_association(ctrl);
2671 dev_warn(ctrl->ctrl.device,
2672 "NVME-FC{%d}: Reconnect attempt failed (%d)\n",
2674 if (ctrl->connect_attempts >= NVME_FC_MAX_CONNECT_ATTEMPTS) {
2675 dev_warn(ctrl->ctrl.device,
2676 "NVME-FC{%d}: Max reconnect attempts (%d) "
2677 "reached. Removing controller\n",
2678 ctrl->cnum, ctrl->connect_attempts);
2680 if (!nvme_change_ctrl_state(&ctrl->ctrl,
2681 NVME_CTRL_DELETING)) {
2682 dev_err(ctrl->ctrl.device,
2683 "NVME-FC{%d}: failed to change state "
2684 "to DELETING\n", ctrl->cnum);
2688 WARN_ON(!queue_work(nvme_fc_wq, &ctrl->delete_work));
2692 dev_warn(ctrl->ctrl.device,
2693 "NVME-FC{%d}: Reconnect attempt in %d seconds.\n",
2694 ctrl->cnum, ctrl->reconnect_delay);
2695 queue_delayed_work(nvme_fc_wq, &ctrl->connect_work,
2696 ctrl->reconnect_delay * HZ);
2698 dev_info(ctrl->ctrl.device,
2699 "NVME-FC{%d}: controller reconnect complete\n",
2704 static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
2705 .queue_rq = nvme_fc_queue_rq,
2706 .complete = nvme_fc_complete_rq,
2707 .init_request = nvme_fc_init_admin_request,
2708 .exit_request = nvme_fc_exit_request,
2709 .reinit_request = nvme_fc_reinit_request,
2710 .init_hctx = nvme_fc_init_admin_hctx,
2711 .timeout = nvme_fc_timeout,
2715 static struct nvme_ctrl *
2716 nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
2717 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
2719 struct nvme_fc_ctrl *ctrl;
2720 unsigned long flags;
2723 if (!(rport->remoteport.port_role &
2724 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
2729 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2735 idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
2741 ctrl->ctrl.opts = opts;
2742 INIT_LIST_HEAD(&ctrl->ctrl_list);
2743 ctrl->lport = lport;
2744 ctrl->rport = rport;
2745 ctrl->dev = lport->dev;
2748 get_device(ctrl->dev);
2749 kref_init(&ctrl->ref);
2751 INIT_WORK(&ctrl->delete_work, nvme_fc_delete_ctrl_work);
2752 INIT_WORK(&ctrl->reset_work, nvme_fc_reset_ctrl_work);
2753 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
2754 ctrl->reconnect_delay = opts->reconnect_delay;
2755 spin_lock_init(&ctrl->lock);
2757 /* io queue count */
2758 ctrl->queue_count = min_t(unsigned int,
2760 lport->ops->max_hw_queues);
2761 opts->nr_io_queues = ctrl->queue_count; /* so opts has valid value */
2762 ctrl->queue_count++; /* +1 for admin queue */
2764 ctrl->ctrl.sqsize = opts->queue_size - 1;
2765 ctrl->ctrl.kato = opts->kato;
2768 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(struct nvme_fc_queue),
2773 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
2774 ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
2775 ctrl->admin_tag_set.queue_depth = NVME_FC_AQ_BLKMQ_DEPTH;
2776 ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
2777 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
2778 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2780 sizeof(struct scatterlist)) +
2781 ctrl->lport->ops->fcprqst_priv_sz;
2782 ctrl->admin_tag_set.driver_data = ctrl;
2783 ctrl->admin_tag_set.nr_hw_queues = 1;
2784 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
2786 ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
2788 goto out_free_queues;
2790 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
2791 if (IS_ERR(ctrl->ctrl.admin_q)) {
2792 ret = PTR_ERR(ctrl->ctrl.admin_q);
2793 goto out_free_admin_tag_set;
2797 * Would have been nice to init io queues tag set as well.
2798 * However, we require interaction from the controller
2799 * for max io queue count before we can do so.
2800 * Defer this to the connect path.
2803 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
2805 goto out_cleanup_admin_q;
2807 /* at this point, teardown path changes to ref counting on nvme ctrl */
2809 spin_lock_irqsave(&rport->lock, flags);
2810 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
2811 spin_unlock_irqrestore(&rport->lock, flags);
2813 ret = nvme_fc_create_association(ctrl);
2815 ctrl->ctrl.opts = NULL;
2816 /* initiate nvme ctrl ref counting teardown */
2817 nvme_uninit_ctrl(&ctrl->ctrl);
2818 nvme_put_ctrl(&ctrl->ctrl);
2820 /* as we're past the point where we transition to the ref
2821 * counting teardown path, if we return a bad pointer here,
2822 * the calling routine, thinking it's prior to the
2823 * transition, will do an rport put. Since the teardown
2824 * path also does a rport put, we do an extra get here to
2825 * so proper order/teardown happens.
2827 nvme_fc_rport_get(rport);
2831 return ERR_PTR(ret);
2834 dev_info(ctrl->ctrl.device,
2835 "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
2836 ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
2840 out_cleanup_admin_q:
2841 blk_cleanup_queue(ctrl->ctrl.admin_q);
2842 out_free_admin_tag_set:
2843 blk_mq_free_tag_set(&ctrl->admin_tag_set);
2845 kfree(ctrl->queues);
2847 put_device(ctrl->dev);
2848 ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2852 /* exit via here doesn't follow ctlr ref points */
2853 return ERR_PTR(ret);
2858 FCT_TRADDR_WWNN = 1 << 0,
2859 FCT_TRADDR_WWPN = 1 << 1,
2862 struct nvmet_fc_traddr {
2867 static const match_table_t traddr_opt_tokens = {
2868 { FCT_TRADDR_WWNN, "nn-%s" },
2869 { FCT_TRADDR_WWPN, "pn-%s" },
2870 { FCT_TRADDR_ERR, NULL }
2874 nvme_fc_parse_address(struct nvmet_fc_traddr *traddr, char *buf)
2876 substring_t args[MAX_OPT_ARGS];
2877 char *options, *o, *p;
2881 options = o = kstrdup(buf, GFP_KERNEL);
2885 while ((p = strsep(&o, ":\n")) != NULL) {
2889 token = match_token(p, traddr_opt_tokens, args);
2891 case FCT_TRADDR_WWNN:
2892 if (match_u64(args, &token64)) {
2896 traddr->nn = token64;
2898 case FCT_TRADDR_WWPN:
2899 if (match_u64(args, &token64)) {
2903 traddr->pn = token64;
2906 pr_warn("unknown traddr token or missing value '%s'\n",
2918 static struct nvme_ctrl *
2919 nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
2921 struct nvme_fc_lport *lport;
2922 struct nvme_fc_rport *rport;
2923 struct nvme_ctrl *ctrl;
2924 struct nvmet_fc_traddr laddr = { 0L, 0L };
2925 struct nvmet_fc_traddr raddr = { 0L, 0L };
2926 unsigned long flags;
2929 ret = nvme_fc_parse_address(&raddr, opts->traddr);
2930 if (ret || !raddr.nn || !raddr.pn)
2931 return ERR_PTR(-EINVAL);
2933 ret = nvme_fc_parse_address(&laddr, opts->host_traddr);
2934 if (ret || !laddr.nn || !laddr.pn)
2935 return ERR_PTR(-EINVAL);
2937 /* find the host and remote ports to connect together */
2938 spin_lock_irqsave(&nvme_fc_lock, flags);
2939 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
2940 if (lport->localport.node_name != laddr.nn ||
2941 lport->localport.port_name != laddr.pn)
2944 list_for_each_entry(rport, &lport->endp_list, endp_list) {
2945 if (rport->remoteport.node_name != raddr.nn ||
2946 rport->remoteport.port_name != raddr.pn)
2949 /* if fail to get reference fall through. Will error */
2950 if (!nvme_fc_rport_get(rport))
2953 spin_unlock_irqrestore(&nvme_fc_lock, flags);
2955 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
2957 nvme_fc_rport_put(rport);
2961 spin_unlock_irqrestore(&nvme_fc_lock, flags);
2963 return ERR_PTR(-ENOENT);
2967 static struct nvmf_transport_ops nvme_fc_transport = {
2969 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
2970 .allowed_opts = NVMF_OPT_RECONNECT_DELAY,
2971 .create_ctrl = nvme_fc_create_ctrl,
2974 static int __init nvme_fc_init_module(void)
2978 nvme_fc_wq = create_workqueue("nvme_fc_wq");
2982 ret = nvmf_register_transport(&nvme_fc_transport);
2988 destroy_workqueue(nvme_fc_wq);
2992 static void __exit nvme_fc_exit_module(void)
2994 /* sanity check - all lports should be removed */
2995 if (!list_empty(&nvme_fc_lport_list))
2996 pr_warn("%s: localport list not empty\n", __func__);
2998 nvmf_unregister_transport(&nvme_fc_transport);
3000 destroy_workqueue(nvme_fc_wq);
3002 ida_destroy(&nvme_fc_local_port_cnt);
3003 ida_destroy(&nvme_fc_ctrl_cnt);
3006 module_init(nvme_fc_init_module);
3007 module_exit(nvme_fc_exit_module);
3009 MODULE_LICENSE("GPL v2");