18638b246SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
2e399441dSJames Smart /*
3e399441dSJames Smart * Copyright (c) 2016 Avago Technologies. All rights reserved.
4e399441dSJames Smart */
5e399441dSJames Smart #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6e399441dSJames Smart #include <linux/module.h>
7e399441dSJames Smart #include <linux/parser.h>
8e399441dSJames Smart #include <uapi/scsi/fc/fc_fs.h>
9e399441dSJames Smart #include <uapi/scsi/fc/fc_els.h>
1061bff8efSJames Smart #include <linux/delay.h>
11d3d0bc78SBart Van Assche #include <linux/overflow.h>
123dbbca75SMuneendra Kumar #include <linux/blk-cgroup.h>
13e399441dSJames Smart #include "nvme.h"
14e399441dSJames Smart #include "fabrics.h"
15e399441dSJames Smart #include <linux/nvme-fc-driver.h>
16e399441dSJames Smart #include <linux/nvme-fc.h>
17ca19bcd0SJames Smart #include "fc.h"
18a6a6d058SHannes Reinecke #include <scsi/scsi_transport_fc.h>
1901d83816SSaurav Kashyap #include <linux/blk-mq-pci.h>
20e399441dSJames Smart
21e399441dSJames Smart /* *************************** Data Structures/Defines ****************** */
22e399441dSJames Smart
23e399441dSJames Smart
24e399441dSJames Smart enum nvme_fc_queue_flags {
2526c0a26dSJens Axboe NVME_FC_Q_CONNECTED = 0,
2626c0a26dSJens Axboe NVME_FC_Q_LIVE,
27e399441dSJames Smart };
28e399441dSJames Smart
29ac7fe82bSJames Smart #define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */
30f673714aSJames Smart #define NVME_FC_DEFAULT_RECONNECT_TMO 2 /* delay between reconnects
31f673714aSJames Smart * when connected and a
32f673714aSJames Smart * connection failure.
33f673714aSJames Smart */
34ac7fe82bSJames Smart
35e399441dSJames Smart struct nvme_fc_queue {
36e399441dSJames Smart struct nvme_fc_ctrl *ctrl;
37e399441dSJames Smart struct device *dev;
38e399441dSJames Smart struct blk_mq_hw_ctx *hctx;
39e399441dSJames Smart void *lldd_handle;
40e399441dSJames Smart size_t cmnd_capsule_len;
41e399441dSJames Smart u32 qnum;
42e399441dSJames Smart u32 rqcnt;
43e399441dSJames Smart u32 seqno;
44e399441dSJames Smart
45e399441dSJames Smart u64 connection_id;
46e399441dSJames Smart atomic_t csn;
47e399441dSJames Smart
48e399441dSJames Smart unsigned long flags;
49e399441dSJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
50e399441dSJames Smart
518d64daf7SJames Smart enum nvme_fcop_flags {
528d64daf7SJames Smart FCOP_FLAGS_TERMIO = (1 << 0),
53c3aedd22SJames Smart FCOP_FLAGS_AEN = (1 << 1),
548d64daf7SJames Smart };
558d64daf7SJames Smart
56e399441dSJames Smart struct nvmefc_ls_req_op {
57e399441dSJames Smart struct nvmefc_ls_req ls_req;
58e399441dSJames Smart
59c913a8b0SJames Smart struct nvme_fc_rport *rport;
60e399441dSJames Smart struct nvme_fc_queue *queue;
61e399441dSJames Smart struct request *rq;
628d64daf7SJames Smart u32 flags;
63e399441dSJames Smart
64e399441dSJames Smart int ls_error;
65e399441dSJames Smart struct completion ls_done;
66c913a8b0SJames Smart struct list_head lsreq_list; /* rport->ls_req_list */
67e399441dSJames Smart bool req_queued;
68e399441dSJames Smart };
69e399441dSJames Smart
7014fd1e98SJames Smart struct nvmefc_ls_rcv_op {
7114fd1e98SJames Smart struct nvme_fc_rport *rport;
7214fd1e98SJames Smart struct nvmefc_ls_rsp *lsrsp;
7314fd1e98SJames Smart union nvmefc_ls_requests *rqstbuf;
7414fd1e98SJames Smart union nvmefc_ls_responses *rspbuf;
7514fd1e98SJames Smart u16 rqstdatalen;
7614fd1e98SJames Smart bool handled;
7714fd1e98SJames Smart dma_addr_t rspdma;
7814fd1e98SJames Smart struct list_head lsrcv_list; /* rport->ls_rcv_list */
7914fd1e98SJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
8014fd1e98SJames Smart
81e399441dSJames Smart enum nvme_fcpop_state {
82e399441dSJames Smart FCPOP_STATE_UNINIT = 0,
83e399441dSJames Smart FCPOP_STATE_IDLE = 1,
84e399441dSJames Smart FCPOP_STATE_ACTIVE = 2,
85e399441dSJames Smart FCPOP_STATE_ABORTED = 3,
8678a7ac26SJames Smart FCPOP_STATE_COMPLETE = 4,
87e399441dSJames Smart };
88e399441dSJames Smart
89e399441dSJames Smart struct nvme_fc_fcp_op {
90e399441dSJames Smart struct nvme_request nreq; /*
91e399441dSJames Smart * nvme/host/core.c
92e399441dSJames Smart * requires this to be
93e399441dSJames Smart * the 1st element in the
94e399441dSJames Smart * private structure
95e399441dSJames Smart * associated with the
96e399441dSJames Smart * request.
97e399441dSJames Smart */
98e399441dSJames Smart struct nvmefc_fcp_req fcp_req;
99e399441dSJames Smart
100e399441dSJames Smart struct nvme_fc_ctrl *ctrl;
101e399441dSJames Smart struct nvme_fc_queue *queue;
102e399441dSJames Smart struct request *rq;
103e399441dSJames Smart
104e399441dSJames Smart atomic_t state;
10578a7ac26SJames Smart u32 flags;
106e399441dSJames Smart u32 rqno;
107e399441dSJames Smart u32 nents;
108e399441dSJames Smart
109e399441dSJames Smart struct nvme_fc_cmd_iu cmd_iu;
110e399441dSJames Smart struct nvme_fc_ersp_iu rsp_iu;
111e399441dSJames Smart };
112e399441dSJames Smart
113d3d0bc78SBart Van Assche struct nvme_fcp_op_w_sgl {
114d3d0bc78SBart Van Assche struct nvme_fc_fcp_op op;
115b1ae1a23SIsrael Rukshin struct scatterlist sgl[NVME_INLINE_SG_CNT];
116f1e71d75SGustavo A. R. Silva uint8_t priv[];
117d3d0bc78SBart Van Assche };
118d3d0bc78SBart Van Assche
119e399441dSJames Smart struct nvme_fc_lport {
120e399441dSJames Smart struct nvme_fc_local_port localport;
121e399441dSJames Smart
122e399441dSJames Smart struct ida endp_cnt;
123e399441dSJames Smart struct list_head port_list; /* nvme_fc_port_list */
124e399441dSJames Smart struct list_head endp_list;
125e399441dSJames Smart struct device *dev; /* physical device for dma */
126e399441dSJames Smart struct nvme_fc_port_template *ops;
127e399441dSJames Smart struct kref ref;
128158bfb88SJames Smart atomic_t act_rport_cnt;
129e399441dSJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
130e399441dSJames Smart
131e399441dSJames Smart struct nvme_fc_rport {
132e399441dSJames Smart struct nvme_fc_remote_port remoteport;
133e399441dSJames Smart
134e399441dSJames Smart struct list_head endp_list; /* for lport->endp_list */
135e399441dSJames Smart struct list_head ctrl_list;
136c913a8b0SJames Smart struct list_head ls_req_list;
13714fd1e98SJames Smart struct list_head ls_rcv_list;
13897faec53SJames Smart struct list_head disc_list;
139c913a8b0SJames Smart struct device *dev; /* physical device for dma */
140c913a8b0SJames Smart struct nvme_fc_lport *lport;
141e399441dSJames Smart spinlock_t lock;
142e399441dSJames Smart struct kref ref;
143158bfb88SJames Smart atomic_t act_ctrl_cnt;
1442b632970SJames Smart unsigned long dev_loss_end;
14514fd1e98SJames Smart struct work_struct lsrcv_work;
146e399441dSJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
147e399441dSJames Smart
148eb4ee8f1SJames Smart /* fc_ctrl flags values - specified as bit positions */
149eb4ee8f1SJames Smart #define ASSOC_ACTIVE 0
150caf1cbe3SJames Smart #define ASSOC_FAILED 1
151caf1cbe3SJames Smart #define FCCTRL_TERMIO 2
152e399441dSJames Smart
153e399441dSJames Smart struct nvme_fc_ctrl {
154e399441dSJames Smart spinlock_t lock;
155e399441dSJames Smart struct nvme_fc_queue *queues;
156e399441dSJames Smart struct device *dev;
157e399441dSJames Smart struct nvme_fc_lport *lport;
158e399441dSJames Smart struct nvme_fc_rport *rport;
159e399441dSJames Smart u32 cnum;
160e399441dSJames Smart
1614c984154SJames Smart bool ioq_live;
162e399441dSJames Smart u64 association_id;
16314fd1e98SJames Smart struct nvmefc_ls_rcv_op *rcv_disconn;
164e399441dSJames Smart
165e399441dSJames Smart struct list_head ctrl_list; /* rport->ctrl_list */
166e399441dSJames Smart
167e399441dSJames Smart struct blk_mq_tag_set admin_tag_set;
168e399441dSJames Smart struct blk_mq_tag_set tag_set;
169e399441dSJames Smart
17019fce047SJames Smart struct work_struct ioerr_work;
17161bff8efSJames Smart struct delayed_work connect_work;
17261bff8efSJames Smart
173e399441dSJames Smart struct kref ref;
174eb4ee8f1SJames Smart unsigned long flags;
17561bff8efSJames Smart u32 iocnt;
17636715cf4SJames Smart wait_queue_head_t ioabort_wait;
177e399441dSJames Smart
17838dabe21SKeith Busch struct nvme_fc_fcp_op aen_ops[NVME_NR_AEN_COMMANDS];
179e399441dSJames Smart
180e399441dSJames Smart struct nvme_ctrl ctrl;
181e399441dSJames Smart };
182e399441dSJames Smart
183e399441dSJames Smart static inline struct nvme_fc_ctrl *
to_fc_ctrl(struct nvme_ctrl * ctrl)184e399441dSJames Smart to_fc_ctrl(struct nvme_ctrl *ctrl)
185e399441dSJames Smart {
186e399441dSJames Smart return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
187e399441dSJames Smart }
188e399441dSJames Smart
189e399441dSJames Smart static inline struct nvme_fc_lport *
localport_to_lport(struct nvme_fc_local_port * portptr)190e399441dSJames Smart localport_to_lport(struct nvme_fc_local_port *portptr)
191e399441dSJames Smart {
192e399441dSJames Smart return container_of(portptr, struct nvme_fc_lport, localport);
193e399441dSJames Smart }
194e399441dSJames Smart
195e399441dSJames Smart static inline struct nvme_fc_rport *
remoteport_to_rport(struct nvme_fc_remote_port * portptr)196e399441dSJames Smart remoteport_to_rport(struct nvme_fc_remote_port *portptr)
197e399441dSJames Smart {
198e399441dSJames Smart return container_of(portptr, struct nvme_fc_rport, remoteport);
199e399441dSJames Smart }
200e399441dSJames Smart
201e399441dSJames Smart static inline struct nvmefc_ls_req_op *
ls_req_to_lsop(struct nvmefc_ls_req * lsreq)202e399441dSJames Smart ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
203e399441dSJames Smart {
204e399441dSJames Smart return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
205e399441dSJames Smart }
206e399441dSJames Smart
207e399441dSJames Smart static inline struct nvme_fc_fcp_op *
fcp_req_to_fcp_op(struct nvmefc_fcp_req * fcpreq)208e399441dSJames Smart fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
209e399441dSJames Smart {
210e399441dSJames Smart return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
211e399441dSJames Smart }
212e399441dSJames Smart
213e399441dSJames Smart
214e399441dSJames Smart
215e399441dSJames Smart /* *************************** Globals **************************** */
216e399441dSJames Smart
217e399441dSJames Smart
218e399441dSJames Smart static DEFINE_SPINLOCK(nvme_fc_lock);
219e399441dSJames Smart
220e399441dSJames Smart static LIST_HEAD(nvme_fc_lport_list);
221e399441dSJames Smart static DEFINE_IDA(nvme_fc_local_port_cnt);
222e399441dSJames Smart static DEFINE_IDA(nvme_fc_ctrl_cnt);
223e399441dSJames Smart
2245f568556SJames Smart /*
2255f568556SJames Smart * These items are short-term. They will eventually be moved into
2265f568556SJames Smart * a generic FC class. See comments in module init.
2275f568556SJames Smart */
2285f568556SJames Smart static struct device *fc_udev_device;
2295f568556SJames Smart
230ff029451SChristoph Hellwig static void nvme_fc_complete_rq(struct request *rq);
231e399441dSJames Smart
232e399441dSJames Smart /* *********************** FC-NVME Port Management ************************ */
233e399441dSJames Smart
234e399441dSJames Smart static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
235e399441dSJames Smart struct nvme_fc_queue *, unsigned int);
236e399441dSJames Smart
23714fd1e98SJames Smart static void nvme_fc_handle_ls_rqst_work(struct work_struct *work);
23814fd1e98SJames Smart
23914fd1e98SJames Smart
2405533d424SJames Smart static void
nvme_fc_free_lport(struct kref * ref)2415533d424SJames Smart nvme_fc_free_lport(struct kref *ref)
2425533d424SJames Smart {
2435533d424SJames Smart struct nvme_fc_lport *lport =
2445533d424SJames Smart container_of(ref, struct nvme_fc_lport, ref);
2455533d424SJames Smart unsigned long flags;
2465533d424SJames Smart
2475533d424SJames Smart WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
2485533d424SJames Smart WARN_ON(!list_empty(&lport->endp_list));
2495533d424SJames Smart
2505533d424SJames Smart /* remove from transport list */
2515533d424SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
2525533d424SJames Smart list_del(&lport->port_list);
2535533d424SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
2545533d424SJames Smart
2553dd83f40SSagi Grimberg ida_free(&nvme_fc_local_port_cnt, lport->localport.port_num);
2565533d424SJames Smart ida_destroy(&lport->endp_cnt);
2575533d424SJames Smart
2585533d424SJames Smart put_device(lport->dev);
2595533d424SJames Smart
2605533d424SJames Smart kfree(lport);
2615533d424SJames Smart }
2625533d424SJames Smart
2635533d424SJames Smart static void
nvme_fc_lport_put(struct nvme_fc_lport * lport)2645533d424SJames Smart nvme_fc_lport_put(struct nvme_fc_lport *lport)
2655533d424SJames Smart {
2665533d424SJames Smart kref_put(&lport->ref, nvme_fc_free_lport);
2675533d424SJames Smart }
2685533d424SJames Smart
2695533d424SJames Smart static int
nvme_fc_lport_get(struct nvme_fc_lport * lport)2705533d424SJames Smart nvme_fc_lport_get(struct nvme_fc_lport *lport)
2715533d424SJames Smart {
2725533d424SJames Smart return kref_get_unless_zero(&lport->ref);
2735533d424SJames Smart }
2745533d424SJames Smart
2755533d424SJames Smart
2765533d424SJames Smart static struct nvme_fc_lport *
nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info * pinfo,struct nvme_fc_port_template * ops,struct device * dev)277c5760f30SJames Smart nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo,
278c5760f30SJames Smart struct nvme_fc_port_template *ops,
279c5760f30SJames Smart struct device *dev)
2805533d424SJames Smart {
2815533d424SJames Smart struct nvme_fc_lport *lport;
2825533d424SJames Smart unsigned long flags;
2835533d424SJames Smart
2845533d424SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
2855533d424SJames Smart
2865533d424SJames Smart list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
2875533d424SJames Smart if (lport->localport.node_name != pinfo->node_name ||
2885533d424SJames Smart lport->localport.port_name != pinfo->port_name)
2895533d424SJames Smart continue;
2905533d424SJames Smart
291c5760f30SJames Smart if (lport->dev != dev) {
292c5760f30SJames Smart lport = ERR_PTR(-EXDEV);
293c5760f30SJames Smart goto out_done;
294c5760f30SJames Smart }
295c5760f30SJames Smart
2965533d424SJames Smart if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
2975533d424SJames Smart lport = ERR_PTR(-EEXIST);
2985533d424SJames Smart goto out_done;
2995533d424SJames Smart }
3005533d424SJames Smart
3015533d424SJames Smart if (!nvme_fc_lport_get(lport)) {
3025533d424SJames Smart /*
3035533d424SJames Smart * fails if ref cnt already 0. If so,
3045533d424SJames Smart * act as if lport already deleted
3055533d424SJames Smart */
3065533d424SJames Smart lport = NULL;
3075533d424SJames Smart goto out_done;
3085533d424SJames Smart }
3095533d424SJames Smart
3105533d424SJames Smart /* resume the lport */
3115533d424SJames Smart
312c5760f30SJames Smart lport->ops = ops;
3135533d424SJames Smart lport->localport.port_role = pinfo->port_role;
3145533d424SJames Smart lport->localport.port_id = pinfo->port_id;
3155533d424SJames Smart lport->localport.port_state = FC_OBJSTATE_ONLINE;
3165533d424SJames Smart
3175533d424SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
3185533d424SJames Smart
3195533d424SJames Smart return lport;
3205533d424SJames Smart }
3215533d424SJames Smart
3225533d424SJames Smart lport = NULL;
3235533d424SJames Smart
3245533d424SJames Smart out_done:
3255533d424SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
3265533d424SJames Smart
3275533d424SJames Smart return lport;
3285533d424SJames Smart }
329e399441dSJames Smart
330e399441dSJames Smart /**
331e399441dSJames Smart * nvme_fc_register_localport - transport entry point called by an
332e399441dSJames Smart * LLDD to register the existence of a NVME
333e399441dSJames Smart * host FC port.
334e399441dSJames Smart * @pinfo: pointer to information about the port to be registered
335e399441dSJames Smart * @template: LLDD entrypoints and operational parameters for the port
336e399441dSJames Smart * @dev: physical hardware device node port corresponds to. Will be
337e399441dSJames Smart * used for DMA mappings
33876c910c7SBart Van Assche * @portptr: pointer to a local port pointer. Upon success, the routine
339e399441dSJames Smart * will allocate a nvme_fc_local_port structure and place its
340e399441dSJames Smart * address in the local port pointer. Upon failure, local port
341e399441dSJames Smart * pointer will be set to 0.
342e399441dSJames Smart *
343e399441dSJames Smart * Returns:
344e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno
345e399441dSJames Smart * (ex: -ENXIO) upon failure.
346e399441dSJames Smart */
347e399441dSJames Smart int
nvme_fc_register_localport(struct nvme_fc_port_info * pinfo,struct nvme_fc_port_template * template,struct device * dev,struct nvme_fc_local_port ** portptr)348e399441dSJames Smart nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
349e399441dSJames Smart struct nvme_fc_port_template *template,
350e399441dSJames Smart struct device *dev,
351e399441dSJames Smart struct nvme_fc_local_port **portptr)
352e399441dSJames Smart {
353e399441dSJames Smart struct nvme_fc_lport *newrec;
354e399441dSJames Smart unsigned long flags;
355e399441dSJames Smart int ret, idx;
356e399441dSJames Smart
357e399441dSJames Smart if (!template->localport_delete || !template->remoteport_delete ||
358e399441dSJames Smart !template->ls_req || !template->fcp_io ||
359e399441dSJames Smart !template->ls_abort || !template->fcp_abort ||
360e399441dSJames Smart !template->max_hw_queues || !template->max_sgl_segments ||
3618c5c6605SJames Smart !template->max_dif_sgl_segments || !template->dma_boundary) {
362e399441dSJames Smart ret = -EINVAL;
363e399441dSJames Smart goto out_reghost_failed;
364e399441dSJames Smart }
365e399441dSJames Smart
3665533d424SJames Smart /*
3675533d424SJames Smart * look to see if there is already a localport that had been
3685533d424SJames Smart * deregistered and in the process of waiting for all the
3695533d424SJames Smart * references to fully be removed. If the references haven't
3705533d424SJames Smart * expired, we can simply re-enable the localport. Remoteports
3715533d424SJames Smart * and controller reconnections should resume naturally.
3725533d424SJames Smart */
373c5760f30SJames Smart newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
3745533d424SJames Smart
3755533d424SJames Smart /* found an lport, but something about its state is bad */
3765533d424SJames Smart if (IS_ERR(newrec)) {
3775533d424SJames Smart ret = PTR_ERR(newrec);
3785533d424SJames Smart goto out_reghost_failed;
3795533d424SJames Smart
3805533d424SJames Smart /* found existing lport, which was resumed */
3815533d424SJames Smart } else if (newrec) {
3825533d424SJames Smart *portptr = &newrec->localport;
3835533d424SJames Smart return 0;
3845533d424SJames Smart }
3855533d424SJames Smart
3865533d424SJames Smart /* nothing found - allocate a new localport struct */
3875533d424SJames Smart
388e399441dSJames Smart newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
389e399441dSJames Smart GFP_KERNEL);
390e399441dSJames Smart if (!newrec) {
391e399441dSJames Smart ret = -ENOMEM;
392e399441dSJames Smart goto out_reghost_failed;
393e399441dSJames Smart }
394e399441dSJames Smart
3953dd83f40SSagi Grimberg idx = ida_alloc(&nvme_fc_local_port_cnt, GFP_KERNEL);
396e399441dSJames Smart if (idx < 0) {
397e399441dSJames Smart ret = -ENOSPC;
398e399441dSJames Smart goto out_fail_kfree;
399e399441dSJames Smart }
400e399441dSJames Smart
401e399441dSJames Smart if (!get_device(dev) && dev) {
402e399441dSJames Smart ret = -ENODEV;
403e399441dSJames Smart goto out_ida_put;
404e399441dSJames Smart }
405e399441dSJames Smart
406e399441dSJames Smart INIT_LIST_HEAD(&newrec->port_list);
407e399441dSJames Smart INIT_LIST_HEAD(&newrec->endp_list);
408e399441dSJames Smart kref_init(&newrec->ref);
409158bfb88SJames Smart atomic_set(&newrec->act_rport_cnt, 0);
410e399441dSJames Smart newrec->ops = template;
411e399441dSJames Smart newrec->dev = dev;
412e399441dSJames Smart ida_init(&newrec->endp_cnt);
413f56bf76fSJames Smart if (template->local_priv_sz)
414e399441dSJames Smart newrec->localport.private = &newrec[1];
415f56bf76fSJames Smart else
416f56bf76fSJames Smart newrec->localport.private = NULL;
417e399441dSJames Smart newrec->localport.node_name = pinfo->node_name;
418e399441dSJames Smart newrec->localport.port_name = pinfo->port_name;
419e399441dSJames Smart newrec->localport.port_role = pinfo->port_role;
420e399441dSJames Smart newrec->localport.port_id = pinfo->port_id;
421e399441dSJames Smart newrec->localport.port_state = FC_OBJSTATE_ONLINE;
422e399441dSJames Smart newrec->localport.port_num = idx;
423e399441dSJames Smart
424e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
425e399441dSJames Smart list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
426e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
427e399441dSJames Smart
428e399441dSJames Smart if (dev)
429e399441dSJames Smart dma_set_seg_boundary(dev, template->dma_boundary);
430e399441dSJames Smart
431e399441dSJames Smart *portptr = &newrec->localport;
432e399441dSJames Smart return 0;
433e399441dSJames Smart
434e399441dSJames Smart out_ida_put:
4353dd83f40SSagi Grimberg ida_free(&nvme_fc_local_port_cnt, idx);
436e399441dSJames Smart out_fail_kfree:
437e399441dSJames Smart kfree(newrec);
438e399441dSJames Smart out_reghost_failed:
439e399441dSJames Smart *portptr = NULL;
440e399441dSJames Smart
441e399441dSJames Smart return ret;
442e399441dSJames Smart }
443e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
444e399441dSJames Smart
445e399441dSJames Smart /**
446e399441dSJames Smart * nvme_fc_unregister_localport - transport entry point called by an
447e399441dSJames Smart * LLDD to deregister/remove a previously
448e399441dSJames Smart * registered a NVME host FC port.
44976c910c7SBart Van Assche * @portptr: pointer to the (registered) local port that is to be deregistered.
450e399441dSJames Smart *
451e399441dSJames Smart * Returns:
452e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno
453e399441dSJames Smart * (ex: -ENXIO) upon failure.
454e399441dSJames Smart */
455e399441dSJames Smart int
nvme_fc_unregister_localport(struct nvme_fc_local_port * portptr)456e399441dSJames Smart nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
457e399441dSJames Smart {
458e399441dSJames Smart struct nvme_fc_lport *lport = localport_to_lport(portptr);
459e399441dSJames Smart unsigned long flags;
460e399441dSJames Smart
461e399441dSJames Smart if (!portptr)
462e399441dSJames Smart return -EINVAL;
463e399441dSJames Smart
464e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
465e399441dSJames Smart
466e399441dSJames Smart if (portptr->port_state != FC_OBJSTATE_ONLINE) {
467e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
468e399441dSJames Smart return -EINVAL;
469e399441dSJames Smart }
470e399441dSJames Smart portptr->port_state = FC_OBJSTATE_DELETED;
471e399441dSJames Smart
472e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
473e399441dSJames Smart
474158bfb88SJames Smart if (atomic_read(&lport->act_rport_cnt) == 0)
475158bfb88SJames Smart lport->ops->localport_delete(&lport->localport);
476158bfb88SJames Smart
477e399441dSJames Smart nvme_fc_lport_put(lport);
478e399441dSJames Smart
479e399441dSJames Smart return 0;
480e399441dSJames Smart }
481e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
482e399441dSJames Smart
483eaefd5abSJames Smart /*
484eaefd5abSJames Smart * TRADDR strings, per FC-NVME are fixed format:
485eaefd5abSJames Smart * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
486eaefd5abSJames Smart * udev event will only differ by prefix of what field is
487eaefd5abSJames Smart * being specified:
488eaefd5abSJames Smart * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
489eaefd5abSJames Smart * 19 + 43 + null_fudge = 64 characters
490eaefd5abSJames Smart */
491eaefd5abSJames Smart #define FCNVME_TRADDR_LENGTH 64
492eaefd5abSJames Smart
493eaefd5abSJames Smart static void
nvme_fc_signal_discovery_scan(struct nvme_fc_lport * lport,struct nvme_fc_rport * rport)494eaefd5abSJames Smart nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
495eaefd5abSJames Smart struct nvme_fc_rport *rport)
496eaefd5abSJames Smart {
497eaefd5abSJames Smart char hostaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_HOST_TRADDR=...*/
498eaefd5abSJames Smart char tgtaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_TRADDR=...*/
499eaefd5abSJames Smart char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
500eaefd5abSJames Smart
501eaefd5abSJames Smart if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
502eaefd5abSJames Smart return;
503eaefd5abSJames Smart
504eaefd5abSJames Smart snprintf(hostaddr, sizeof(hostaddr),
505eaefd5abSJames Smart "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
506eaefd5abSJames Smart lport->localport.node_name, lport->localport.port_name);
507eaefd5abSJames Smart snprintf(tgtaddr, sizeof(tgtaddr),
508eaefd5abSJames Smart "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
509eaefd5abSJames Smart rport->remoteport.node_name, rport->remoteport.port_name);
510eaefd5abSJames Smart kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
511eaefd5abSJames Smart }
512eaefd5abSJames Smart
513469d0ef0SJames Smart static void
nvme_fc_free_rport(struct kref * ref)514469d0ef0SJames Smart nvme_fc_free_rport(struct kref *ref)
515469d0ef0SJames Smart {
516469d0ef0SJames Smart struct nvme_fc_rport *rport =
517469d0ef0SJames Smart container_of(ref, struct nvme_fc_rport, ref);
518469d0ef0SJames Smart struct nvme_fc_lport *lport =
519469d0ef0SJames Smart localport_to_lport(rport->remoteport.localport);
520469d0ef0SJames Smart unsigned long flags;
521469d0ef0SJames Smart
522469d0ef0SJames Smart WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
523469d0ef0SJames Smart WARN_ON(!list_empty(&rport->ctrl_list));
524469d0ef0SJames Smart
525469d0ef0SJames Smart /* remove from lport list */
526469d0ef0SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
527469d0ef0SJames Smart list_del(&rport->endp_list);
528469d0ef0SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
529469d0ef0SJames Smart
53097faec53SJames Smart WARN_ON(!list_empty(&rport->disc_list));
5313dd83f40SSagi Grimberg ida_free(&lport->endp_cnt, rport->remoteport.port_num);
532469d0ef0SJames Smart
533469d0ef0SJames Smart kfree(rport);
534469d0ef0SJames Smart
535469d0ef0SJames Smart nvme_fc_lport_put(lport);
536469d0ef0SJames Smart }
537469d0ef0SJames Smart
538469d0ef0SJames Smart static void
nvme_fc_rport_put(struct nvme_fc_rport * rport)539469d0ef0SJames Smart nvme_fc_rport_put(struct nvme_fc_rport *rport)
540469d0ef0SJames Smart {
541469d0ef0SJames Smart kref_put(&rport->ref, nvme_fc_free_rport);
542469d0ef0SJames Smart }
543469d0ef0SJames Smart
544469d0ef0SJames Smart static int
nvme_fc_rport_get(struct nvme_fc_rport * rport)545469d0ef0SJames Smart nvme_fc_rport_get(struct nvme_fc_rport *rport)
546469d0ef0SJames Smart {
547469d0ef0SJames Smart return kref_get_unless_zero(&rport->ref);
548469d0ef0SJames Smart }
549469d0ef0SJames Smart
5502b632970SJames Smart static void
nvme_fc_resume_controller(struct nvme_fc_ctrl * ctrl)5512b632970SJames Smart nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl)
5522b632970SJames Smart {
5538884a56dSKeith Busch switch (nvme_ctrl_state(&ctrl->ctrl)) {
5542b632970SJames Smart case NVME_CTRL_NEW:
555ad6a0a52SMax Gurtovoy case NVME_CTRL_CONNECTING:
5562b632970SJames Smart /*
5572b632970SJames Smart * As all reconnects were suppressed, schedule a
5582b632970SJames Smart * connect.
5592b632970SJames Smart */
5602b632970SJames Smart dev_info(ctrl->ctrl.device,
5612b632970SJames Smart "NVME-FC{%d}: connectivity re-established. "
5622b632970SJames Smart "Attempting reconnect\n", ctrl->cnum);
5632b632970SJames Smart
5642b632970SJames Smart queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
5652b632970SJames Smart break;
5662b632970SJames Smart
5672b632970SJames Smart case NVME_CTRL_RESETTING:
5682b632970SJames Smart /*
5692b632970SJames Smart * Controller is already in the process of terminating the
5702b632970SJames Smart * association. No need to do anything further. The reconnect
5712b632970SJames Smart * step will naturally occur after the reset completes.
5722b632970SJames Smart */
5732b632970SJames Smart break;
5742b632970SJames Smart
5752b632970SJames Smart default:
5762b632970SJames Smart /* no action to take - let it delete */
5772b632970SJames Smart break;
5782b632970SJames Smart }
5792b632970SJames Smart }
5802b632970SJames Smart
5812b632970SJames Smart static struct nvme_fc_rport *
nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport * lport,struct nvme_fc_port_info * pinfo)5822b632970SJames Smart nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport,
5832b632970SJames Smart struct nvme_fc_port_info *pinfo)
5842b632970SJames Smart {
5852b632970SJames Smart struct nvme_fc_rport *rport;
5862b632970SJames Smart struct nvme_fc_ctrl *ctrl;
5872b632970SJames Smart unsigned long flags;
5882b632970SJames Smart
5892b632970SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
5902b632970SJames Smart
5912b632970SJames Smart list_for_each_entry(rport, &lport->endp_list, endp_list) {
5922b632970SJames Smart if (rport->remoteport.node_name != pinfo->node_name ||
5932b632970SJames Smart rport->remoteport.port_name != pinfo->port_name)
5942b632970SJames Smart continue;
5952b632970SJames Smart
5962b632970SJames Smart if (!nvme_fc_rport_get(rport)) {
5972b632970SJames Smart rport = ERR_PTR(-ENOLCK);
5982b632970SJames Smart goto out_done;
5992b632970SJames Smart }
6002b632970SJames Smart
6012b632970SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
6022b632970SJames Smart
6032b632970SJames Smart spin_lock_irqsave(&rport->lock, flags);
6042b632970SJames Smart
6052b632970SJames Smart /* has it been unregistered */
6062b632970SJames Smart if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) {
6072b632970SJames Smart /* means lldd called us twice */
6082b632970SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
6092b632970SJames Smart nvme_fc_rport_put(rport);
6102b632970SJames Smart return ERR_PTR(-ESTALE);
6112b632970SJames Smart }
6122b632970SJames Smart
6130cdd5fcaSJames Smart rport->remoteport.port_role = pinfo->port_role;
6140cdd5fcaSJames Smart rport->remoteport.port_id = pinfo->port_id;
6152b632970SJames Smart rport->remoteport.port_state = FC_OBJSTATE_ONLINE;
6162b632970SJames Smart rport->dev_loss_end = 0;
6172b632970SJames Smart
6182b632970SJames Smart /*
6192b632970SJames Smart * kick off a reconnect attempt on all associations to the
6202b632970SJames Smart * remote port. A successful reconnects will resume i/o.
6212b632970SJames Smart */
6222b632970SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
6232b632970SJames Smart nvme_fc_resume_controller(ctrl);
6242b632970SJames Smart
6252b632970SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
6262b632970SJames Smart
6272b632970SJames Smart return rport;
6282b632970SJames Smart }
6292b632970SJames Smart
6302b632970SJames Smart rport = NULL;
6312b632970SJames Smart
6322b632970SJames Smart out_done:
6332b632970SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
6342b632970SJames Smart
6352b632970SJames Smart return rport;
6362b632970SJames Smart }
6372b632970SJames Smart
6382b632970SJames Smart static inline void
__nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport * rport,struct nvme_fc_port_info * pinfo)6392b632970SJames Smart __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport,
6402b632970SJames Smart struct nvme_fc_port_info *pinfo)
6412b632970SJames Smart {
6422b632970SJames Smart if (pinfo->dev_loss_tmo)
6432b632970SJames Smart rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
6442b632970SJames Smart else
6452b632970SJames Smart rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
6462b632970SJames Smart }
6472b632970SJames Smart
648e399441dSJames Smart /**
649e399441dSJames Smart * nvme_fc_register_remoteport - transport entry point called by an
650e399441dSJames Smart * LLDD to register the existence of a NVME
651e399441dSJames Smart * subsystem FC port on its fabric.
652e399441dSJames Smart * @localport: pointer to the (registered) local port that the remote
653e399441dSJames Smart * subsystem port is connected to.
654e399441dSJames Smart * @pinfo: pointer to information about the port to be registered
65576c910c7SBart Van Assche * @portptr: pointer to a remote port pointer. Upon success, the routine
656e399441dSJames Smart * will allocate a nvme_fc_remote_port structure and place its
657e399441dSJames Smart * address in the remote port pointer. Upon failure, remote port
658e399441dSJames Smart * pointer will be set to 0.
659e399441dSJames Smart *
660e399441dSJames Smart * Returns:
661e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno
662e399441dSJames Smart * (ex: -ENXIO) upon failure.
663e399441dSJames Smart */
664e399441dSJames Smart int
nvme_fc_register_remoteport(struct nvme_fc_local_port * localport,struct nvme_fc_port_info * pinfo,struct nvme_fc_remote_port ** portptr)665e399441dSJames Smart nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
666e399441dSJames Smart struct nvme_fc_port_info *pinfo,
667e399441dSJames Smart struct nvme_fc_remote_port **portptr)
668e399441dSJames Smart {
669e399441dSJames Smart struct nvme_fc_lport *lport = localport_to_lport(localport);
670e399441dSJames Smart struct nvme_fc_rport *newrec;
671e399441dSJames Smart unsigned long flags;
672e399441dSJames Smart int ret, idx;
673e399441dSJames Smart
6742b632970SJames Smart if (!nvme_fc_lport_get(lport)) {
6752b632970SJames Smart ret = -ESHUTDOWN;
6762b632970SJames Smart goto out_reghost_failed;
6772b632970SJames Smart }
6782b632970SJames Smart
6792b632970SJames Smart /*
6802b632970SJames Smart * look to see if there is already a remoteport that is waiting
6812b632970SJames Smart * for a reconnect (within dev_loss_tmo) with the same WWN's.
6822b632970SJames Smart * If so, transition to it and reconnect.
6832b632970SJames Smart */
6842b632970SJames Smart newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
6852b632970SJames Smart
6862b632970SJames Smart /* found an rport, but something about its state is bad */
6872b632970SJames Smart if (IS_ERR(newrec)) {
6882b632970SJames Smart ret = PTR_ERR(newrec);
6892b632970SJames Smart goto out_lport_put;
6902b632970SJames Smart
6912b632970SJames Smart /* found existing rport, which was resumed */
6922b632970SJames Smart } else if (newrec) {
6932b632970SJames Smart nvme_fc_lport_put(lport);
6942b632970SJames Smart __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
6952b632970SJames Smart nvme_fc_signal_discovery_scan(lport, newrec);
6962b632970SJames Smart *portptr = &newrec->remoteport;
6972b632970SJames Smart return 0;
6982b632970SJames Smart }
6992b632970SJames Smart
7002b632970SJames Smart /* nothing found - allocate a new remoteport struct */
7012b632970SJames Smart
702e399441dSJames Smart newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
703e399441dSJames Smart GFP_KERNEL);
704e399441dSJames Smart if (!newrec) {
705e399441dSJames Smart ret = -ENOMEM;
7062b632970SJames Smart goto out_lport_put;
707e399441dSJames Smart }
708e399441dSJames Smart
7093dd83f40SSagi Grimberg idx = ida_alloc(&lport->endp_cnt, GFP_KERNEL);
710e399441dSJames Smart if (idx < 0) {
711e399441dSJames Smart ret = -ENOSPC;
7122b632970SJames Smart goto out_kfree_rport;
713e399441dSJames Smart }
714e399441dSJames Smart
715e399441dSJames Smart INIT_LIST_HEAD(&newrec->endp_list);
716e399441dSJames Smart INIT_LIST_HEAD(&newrec->ctrl_list);
717c913a8b0SJames Smart INIT_LIST_HEAD(&newrec->ls_req_list);
71897faec53SJames Smart INIT_LIST_HEAD(&newrec->disc_list);
719e399441dSJames Smart kref_init(&newrec->ref);
720158bfb88SJames Smart atomic_set(&newrec->act_ctrl_cnt, 0);
721e399441dSJames Smart spin_lock_init(&newrec->lock);
722e399441dSJames Smart newrec->remoteport.localport = &lport->localport;
72314fd1e98SJames Smart INIT_LIST_HEAD(&newrec->ls_rcv_list);
724c913a8b0SJames Smart newrec->dev = lport->dev;
725c913a8b0SJames Smart newrec->lport = lport;
726f56bf76fSJames Smart if (lport->ops->remote_priv_sz)
727e399441dSJames Smart newrec->remoteport.private = &newrec[1];
728f56bf76fSJames Smart else
729f56bf76fSJames Smart newrec->remoteport.private = NULL;
730e399441dSJames Smart newrec->remoteport.port_role = pinfo->port_role;
731e399441dSJames Smart newrec->remoteport.node_name = pinfo->node_name;
732e399441dSJames Smart newrec->remoteport.port_name = pinfo->port_name;
733e399441dSJames Smart newrec->remoteport.port_id = pinfo->port_id;
734e399441dSJames Smart newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
735e399441dSJames Smart newrec->remoteport.port_num = idx;
7362b632970SJames Smart __nvme_fc_set_dev_loss_tmo(newrec, pinfo);
73714fd1e98SJames Smart INIT_WORK(&newrec->lsrcv_work, nvme_fc_handle_ls_rqst_work);
738e399441dSJames Smart
739e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
740e399441dSJames Smart list_add_tail(&newrec->endp_list, &lport->endp_list);
741e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
742e399441dSJames Smart
743eaefd5abSJames Smart nvme_fc_signal_discovery_scan(lport, newrec);
744eaefd5abSJames Smart
745e399441dSJames Smart *portptr = &newrec->remoteport;
746e399441dSJames Smart return 0;
747e399441dSJames Smart
748e399441dSJames Smart out_kfree_rport:
749e399441dSJames Smart kfree(newrec);
7502b632970SJames Smart out_lport_put:
7512b632970SJames Smart nvme_fc_lport_put(lport);
752e399441dSJames Smart out_reghost_failed:
753e399441dSJames Smart *portptr = NULL;
754e399441dSJames Smart return ret;
755e399441dSJames Smart }
756e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
757e399441dSJames Smart
7588d64daf7SJames Smart static int
nvme_fc_abort_lsops(struct nvme_fc_rport * rport)7598d64daf7SJames Smart nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
7608d64daf7SJames Smart {
7618d64daf7SJames Smart struct nvmefc_ls_req_op *lsop;
7628d64daf7SJames Smart unsigned long flags;
7638d64daf7SJames Smart
7648d64daf7SJames Smart restart:
7658d64daf7SJames Smart spin_lock_irqsave(&rport->lock, flags);
7668d64daf7SJames Smart
7678d64daf7SJames Smart list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
7688d64daf7SJames Smart if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
7698d64daf7SJames Smart lsop->flags |= FCOP_FLAGS_TERMIO;
7708d64daf7SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
7718d64daf7SJames Smart rport->lport->ops->ls_abort(&rport->lport->localport,
7728d64daf7SJames Smart &rport->remoteport,
7738d64daf7SJames Smart &lsop->ls_req);
7748d64daf7SJames Smart goto restart;
7758d64daf7SJames Smart }
7768d64daf7SJames Smart }
7778d64daf7SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
7788d64daf7SJames Smart
7798d64daf7SJames Smart return 0;
7808d64daf7SJames Smart }
7818d64daf7SJames Smart
7822b632970SJames Smart static void
nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl * ctrl)7832b632970SJames Smart nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
7842b632970SJames Smart {
7852b632970SJames Smart dev_info(ctrl->ctrl.device,
7862b632970SJames Smart "NVME-FC{%d}: controller connectivity lost. Awaiting "
7872b632970SJames Smart "Reconnect", ctrl->cnum);
7882b632970SJames Smart
7898884a56dSKeith Busch switch (nvme_ctrl_state(&ctrl->ctrl)) {
7902b632970SJames Smart case NVME_CTRL_NEW:
7912b632970SJames Smart case NVME_CTRL_LIVE:
7922b632970SJames Smart /*
7932b632970SJames Smart * Schedule a controller reset. The reset will terminate the
7942b632970SJames Smart * association and schedule the reconnect timer. Reconnects
7952b632970SJames Smart * will be attempted until either the ctlr_loss_tmo
7962b632970SJames Smart * (max_retries * connect_delay) expires or the remoteport's
7972b632970SJames Smart * dev_loss_tmo expires.
7982b632970SJames Smart */
7992b632970SJames Smart if (nvme_reset_ctrl(&ctrl->ctrl)) {
8002b632970SJames Smart dev_warn(ctrl->ctrl.device,
80177d0612dSMax Gurtovoy "NVME-FC{%d}: Couldn't schedule reset.\n",
8022b632970SJames Smart ctrl->cnum);
8032b632970SJames Smart nvme_delete_ctrl(&ctrl->ctrl);
8042b632970SJames Smart }
8052b632970SJames Smart break;
8062b632970SJames Smart
807ad6a0a52SMax Gurtovoy case NVME_CTRL_CONNECTING:
8082b632970SJames Smart /*
8092b632970SJames Smart * The association has already been terminated and the
8102b632970SJames Smart * controller is attempting reconnects. No need to do anything
8112b632970SJames Smart * futher. Reconnects will be attempted until either the
8122b632970SJames Smart * ctlr_loss_tmo (max_retries * connect_delay) expires or the
8132b632970SJames Smart * remoteport's dev_loss_tmo expires.
8142b632970SJames Smart */
8152b632970SJames Smart break;
8162b632970SJames Smart
8172b632970SJames Smart case NVME_CTRL_RESETTING:
8182b632970SJames Smart /*
8192b632970SJames Smart * Controller is already in the process of terminating the
8202b632970SJames Smart * association. No need to do anything further. The reconnect
8212b632970SJames Smart * step will kick in naturally after the association is
8222b632970SJames Smart * terminated.
8232b632970SJames Smart */
8242b632970SJames Smart break;
8252b632970SJames Smart
8262b632970SJames Smart case NVME_CTRL_DELETING:
827ecca390eSSagi Grimberg case NVME_CTRL_DELETING_NOIO:
8282b632970SJames Smart default:
8292b632970SJames Smart /* no action to take - let it delete */
8302b632970SJames Smart break;
8312b632970SJames Smart }
8322b632970SJames Smart }
8332b632970SJames Smart
834e399441dSJames Smart /**
835e399441dSJames Smart * nvme_fc_unregister_remoteport - transport entry point called by an
836e399441dSJames Smart * LLDD to deregister/remove a previously
837e399441dSJames Smart * registered a NVME subsystem FC port.
83876c910c7SBart Van Assche * @portptr: pointer to the (registered) remote port that is to be
839e399441dSJames Smart * deregistered.
840e399441dSJames Smart *
841e399441dSJames Smart * Returns:
842e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno
843e399441dSJames Smart * (ex: -ENXIO) upon failure.
844e399441dSJames Smart */
845e399441dSJames Smart int
nvme_fc_unregister_remoteport(struct nvme_fc_remote_port * portptr)846e399441dSJames Smart nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
847e399441dSJames Smart {
848e399441dSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
849e399441dSJames Smart struct nvme_fc_ctrl *ctrl;
850e399441dSJames Smart unsigned long flags;
851e399441dSJames Smart
852e399441dSJames Smart if (!portptr)
853e399441dSJames Smart return -EINVAL;
854e399441dSJames Smart
855e399441dSJames Smart spin_lock_irqsave(&rport->lock, flags);
856e399441dSJames Smart
857e399441dSJames Smart if (portptr->port_state != FC_OBJSTATE_ONLINE) {
858e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags);
859e399441dSJames Smart return -EINVAL;
860e399441dSJames Smart }
861e399441dSJames Smart portptr->port_state = FC_OBJSTATE_DELETED;
862e399441dSJames Smart
8632b632970SJames Smart rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
8642b632970SJames Smart
8652b632970SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
8662b632970SJames Smart /* if dev_loss_tmo==0, dev loss is immediate */
8672b632970SJames Smart if (!portptr->dev_loss_tmo) {
8682b632970SJames Smart dev_warn(ctrl->ctrl.device,
86977d0612dSMax Gurtovoy "NVME-FC{%d}: controller connectivity lost.\n",
8702b632970SJames Smart ctrl->cnum);
871c5017e85SChristoph Hellwig nvme_delete_ctrl(&ctrl->ctrl);
8722b632970SJames Smart } else
8732b632970SJames Smart nvme_fc_ctrl_connectivity_loss(ctrl);
8742b632970SJames Smart }
875e399441dSJames Smart
876e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags);
877e399441dSJames Smart
8788d64daf7SJames Smart nvme_fc_abort_lsops(rport);
8798d64daf7SJames Smart
880158bfb88SJames Smart if (atomic_read(&rport->act_ctrl_cnt) == 0)
881158bfb88SJames Smart rport->lport->ops->remoteport_delete(portptr);
882158bfb88SJames Smart
8832b632970SJames Smart /*
8842b632970SJames Smart * release the reference, which will allow, if all controllers
8852b632970SJames Smart * go away, which should only occur after dev_loss_tmo occurs,
8862b632970SJames Smart * for the rport to be torn down.
8872b632970SJames Smart */
888e399441dSJames Smart nvme_fc_rport_put(rport);
8892b632970SJames Smart
890e399441dSJames Smart return 0;
891e399441dSJames Smart }
892e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
893e399441dSJames Smart
894eaefd5abSJames Smart /**
895eaefd5abSJames Smart * nvme_fc_rescan_remoteport - transport entry point called by an
896eaefd5abSJames Smart * LLDD to request a nvme device rescan.
897eaefd5abSJames Smart * @remoteport: pointer to the (registered) remote port that is to be
898eaefd5abSJames Smart * rescanned.
899eaefd5abSJames Smart *
900eaefd5abSJames Smart * Returns: N/A
901eaefd5abSJames Smart */
902eaefd5abSJames Smart void
nvme_fc_rescan_remoteport(struct nvme_fc_remote_port * remoteport)903eaefd5abSJames Smart nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
904eaefd5abSJames Smart {
905eaefd5abSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
906eaefd5abSJames Smart
907eaefd5abSJames Smart nvme_fc_signal_discovery_scan(rport->lport, rport);
908eaefd5abSJames Smart }
909eaefd5abSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
910eaefd5abSJames Smart
911ac7fe82bSJames Smart int
nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port * portptr,u32 dev_loss_tmo)912ac7fe82bSJames Smart nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
913ac7fe82bSJames Smart u32 dev_loss_tmo)
914ac7fe82bSJames Smart {
915ac7fe82bSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
916ac7fe82bSJames Smart unsigned long flags;
917ac7fe82bSJames Smart
918ac7fe82bSJames Smart spin_lock_irqsave(&rport->lock, flags);
919ac7fe82bSJames Smart
920ac7fe82bSJames Smart if (portptr->port_state != FC_OBJSTATE_ONLINE) {
921ac7fe82bSJames Smart spin_unlock_irqrestore(&rport->lock, flags);
922ac7fe82bSJames Smart return -EINVAL;
923ac7fe82bSJames Smart }
924ac7fe82bSJames Smart
925ac7fe82bSJames Smart /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
926ac7fe82bSJames Smart rport->remoteport.dev_loss_tmo = dev_loss_tmo;
927ac7fe82bSJames Smart
928ac7fe82bSJames Smart spin_unlock_irqrestore(&rport->lock, flags);
929ac7fe82bSJames Smart
930ac7fe82bSJames Smart return 0;
931ac7fe82bSJames Smart }
932ac7fe82bSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
933ac7fe82bSJames Smart
934e399441dSJames Smart
935e399441dSJames Smart /* *********************** FC-NVME DMA Handling **************************** */
936e399441dSJames Smart
937e399441dSJames Smart /*
938e399441dSJames Smart * The fcloop device passes in a NULL device pointer. Real LLD's will
939e399441dSJames Smart * pass in a valid device pointer. If NULL is passed to the dma mapping
940e399441dSJames Smart * routines, depending on the platform, it may or may not succeed, and
941e399441dSJames Smart * may crash.
942e399441dSJames Smart *
943e399441dSJames Smart * As such:
944e399441dSJames Smart * Wrapper all the dma routines and check the dev pointer.
945e399441dSJames Smart *
946e399441dSJames Smart * If simple mappings (return just a dma address, we'll noop them,
947e399441dSJames Smart * returning a dma address of 0.
948e399441dSJames Smart *
949e399441dSJames Smart * On more complex mappings (dma_map_sg), a pseudo routine fills
950e399441dSJames Smart * in the scatter list, setting all dma addresses to 0.
951e399441dSJames Smart */
952e399441dSJames Smart
953e399441dSJames Smart static inline dma_addr_t
fc_dma_map_single(struct device * dev,void * ptr,size_t size,enum dma_data_direction dir)954e399441dSJames Smart fc_dma_map_single(struct device *dev, void *ptr, size_t size,
955e399441dSJames Smart enum dma_data_direction dir)
956e399441dSJames Smart {
957e399441dSJames Smart return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
958e399441dSJames Smart }
959e399441dSJames Smart
960e399441dSJames Smart static inline int
fc_dma_mapping_error(struct device * dev,dma_addr_t dma_addr)961e399441dSJames Smart fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
962e399441dSJames Smart {
963e399441dSJames Smart return dev ? dma_mapping_error(dev, dma_addr) : 0;
964e399441dSJames Smart }
965e399441dSJames Smart
966e399441dSJames Smart static inline void
fc_dma_unmap_single(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)967e399441dSJames Smart fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
968e399441dSJames Smart enum dma_data_direction dir)
969e399441dSJames Smart {
970e399441dSJames Smart if (dev)
971e399441dSJames Smart dma_unmap_single(dev, addr, size, dir);
972e399441dSJames Smart }
973e399441dSJames Smart
974e399441dSJames Smart static inline void
fc_dma_sync_single_for_cpu(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)975e399441dSJames Smart fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
976e399441dSJames Smart enum dma_data_direction dir)
977e399441dSJames Smart {
978e399441dSJames Smart if (dev)
979e399441dSJames Smart dma_sync_single_for_cpu(dev, addr, size, dir);
980e399441dSJames Smart }
981e399441dSJames Smart
982e399441dSJames Smart static inline void
fc_dma_sync_single_for_device(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)983e399441dSJames Smart fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
984e399441dSJames Smart enum dma_data_direction dir)
985e399441dSJames Smart {
986e399441dSJames Smart if (dev)
987e399441dSJames Smart dma_sync_single_for_device(dev, addr, size, dir);
988e399441dSJames Smart }
989e399441dSJames Smart
990e399441dSJames Smart /* pseudo dma_map_sg call */
991e399441dSJames Smart static int
fc_map_sg(struct scatterlist * sg,int nents)992e399441dSJames Smart fc_map_sg(struct scatterlist *sg, int nents)
993e399441dSJames Smart {
994e399441dSJames Smart struct scatterlist *s;
995e399441dSJames Smart int i;
996e399441dSJames Smart
997e399441dSJames Smart WARN_ON(nents == 0 || sg[0].length == 0);
998e399441dSJames Smart
999e399441dSJames Smart for_each_sg(sg, s, nents, i) {
1000e399441dSJames Smart s->dma_address = 0L;
1001e399441dSJames Smart #ifdef CONFIG_NEED_SG_DMA_LENGTH
1002e399441dSJames Smart s->dma_length = s->length;
1003e399441dSJames Smart #endif
1004e399441dSJames Smart }
1005e399441dSJames Smart return nents;
1006e399441dSJames Smart }
1007e399441dSJames Smart
1008e399441dSJames Smart static inline int
fc_dma_map_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)1009e399441dSJames Smart fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1010e399441dSJames Smart enum dma_data_direction dir)
1011e399441dSJames Smart {
1012e399441dSJames Smart return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
1013e399441dSJames Smart }
1014e399441dSJames Smart
1015e399441dSJames Smart static inline void
fc_dma_unmap_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)1016e399441dSJames Smart fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1017e399441dSJames Smart enum dma_data_direction dir)
1018e399441dSJames Smart {
1019e399441dSJames Smart if (dev)
1020e399441dSJames Smart dma_unmap_sg(dev, sg, nents, dir);
1021e399441dSJames Smart }
1022e399441dSJames Smart
1023e399441dSJames Smart /* *********************** FC-NVME LS Handling **************************** */
1024e399441dSJames Smart
1025e399441dSJames Smart static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
1026e399441dSJames Smart static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
1027e399441dSJames Smart
102814fd1e98SJames Smart static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
1029e399441dSJames Smart
1030e399441dSJames Smart static void
__nvme_fc_finish_ls_req(struct nvmefc_ls_req_op * lsop)1031c913a8b0SJames Smart __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
1032e399441dSJames Smart {
1033c913a8b0SJames Smart struct nvme_fc_rport *rport = lsop->rport;
1034e399441dSJames Smart struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1035e399441dSJames Smart unsigned long flags;
1036e399441dSJames Smart
1037c913a8b0SJames Smart spin_lock_irqsave(&rport->lock, flags);
1038e399441dSJames Smart
1039e399441dSJames Smart if (!lsop->req_queued) {
1040c913a8b0SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
1041e399441dSJames Smart return;
1042e399441dSJames Smart }
1043e399441dSJames Smart
1044e399441dSJames Smart list_del(&lsop->lsreq_list);
1045e399441dSJames Smart
1046e399441dSJames Smart lsop->req_queued = false;
1047e399441dSJames Smart
1048c913a8b0SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
1049e399441dSJames Smart
1050c913a8b0SJames Smart fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1051e399441dSJames Smart (lsreq->rqstlen + lsreq->rsplen),
1052e399441dSJames Smart DMA_BIDIRECTIONAL);
1053e399441dSJames Smart
1054c913a8b0SJames Smart nvme_fc_rport_put(rport);
1055e399441dSJames Smart }
1056e399441dSJames Smart
1057e399441dSJames Smart static int
__nvme_fc_send_ls_req(struct nvme_fc_rport * rport,struct nvmefc_ls_req_op * lsop,void (* done)(struct nvmefc_ls_req * req,int status))1058c913a8b0SJames Smart __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
1059e399441dSJames Smart struct nvmefc_ls_req_op *lsop,
1060e399441dSJames Smart void (*done)(struct nvmefc_ls_req *req, int status))
1061e399441dSJames Smart {
1062e399441dSJames Smart struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1063e399441dSJames Smart unsigned long flags;
1064c913a8b0SJames Smart int ret = 0;
1065e399441dSJames Smart
1066c913a8b0SJames Smart if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1067c913a8b0SJames Smart return -ECONNREFUSED;
1068c913a8b0SJames Smart
1069c913a8b0SJames Smart if (!nvme_fc_rport_get(rport))
1070e399441dSJames Smart return -ESHUTDOWN;
1071e399441dSJames Smart
1072e399441dSJames Smart lsreq->done = done;
1073c913a8b0SJames Smart lsop->rport = rport;
1074e399441dSJames Smart lsop->req_queued = false;
1075e399441dSJames Smart INIT_LIST_HEAD(&lsop->lsreq_list);
1076e399441dSJames Smart init_completion(&lsop->ls_done);
1077e399441dSJames Smart
1078c913a8b0SJames Smart lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
1079e399441dSJames Smart lsreq->rqstlen + lsreq->rsplen,
1080e399441dSJames Smart DMA_BIDIRECTIONAL);
1081c913a8b0SJames Smart if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1082c913a8b0SJames Smart ret = -EFAULT;
1083c913a8b0SJames Smart goto out_putrport;
1084e399441dSJames Smart }
1085e399441dSJames Smart lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1086e399441dSJames Smart
1087c913a8b0SJames Smart spin_lock_irqsave(&rport->lock, flags);
1088e399441dSJames Smart
1089c913a8b0SJames Smart list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
1090e399441dSJames Smart
1091e399441dSJames Smart lsop->req_queued = true;
1092e399441dSJames Smart
1093c913a8b0SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
1094e399441dSJames Smart
1095c913a8b0SJames Smart ret = rport->lport->ops->ls_req(&rport->lport->localport,
1096c913a8b0SJames Smart &rport->remoteport, lsreq);
1097e399441dSJames Smart if (ret)
1098c913a8b0SJames Smart goto out_unlink;
1099c913a8b0SJames Smart
1100c913a8b0SJames Smart return 0;
1101c913a8b0SJames Smart
1102c913a8b0SJames Smart out_unlink:
1103e399441dSJames Smart lsop->ls_error = ret;
1104c913a8b0SJames Smart spin_lock_irqsave(&rport->lock, flags);
1105c913a8b0SJames Smart lsop->req_queued = false;
1106c913a8b0SJames Smart list_del(&lsop->lsreq_list);
1107c913a8b0SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
1108c913a8b0SJames Smart fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1109c913a8b0SJames Smart (lsreq->rqstlen + lsreq->rsplen),
1110c913a8b0SJames Smart DMA_BIDIRECTIONAL);
1111c913a8b0SJames Smart out_putrport:
1112c913a8b0SJames Smart nvme_fc_rport_put(rport);
1113e399441dSJames Smart
1114e399441dSJames Smart return ret;
1115e399441dSJames Smart }
1116e399441dSJames Smart
1117e399441dSJames Smart static void
nvme_fc_send_ls_req_done(struct nvmefc_ls_req * lsreq,int status)1118e399441dSJames Smart nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1119e399441dSJames Smart {
1120e399441dSJames Smart struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1121e399441dSJames Smart
1122e399441dSJames Smart lsop->ls_error = status;
1123e399441dSJames Smart complete(&lsop->ls_done);
1124e399441dSJames Smart }
1125e399441dSJames Smart
1126e399441dSJames Smart static int
nvme_fc_send_ls_req(struct nvme_fc_rport * rport,struct nvmefc_ls_req_op * lsop)1127c913a8b0SJames Smart nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
1128e399441dSJames Smart {
1129e399441dSJames Smart struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1130e399441dSJames Smart struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1131e399441dSJames Smart int ret;
1132e399441dSJames Smart
1133c913a8b0SJames Smart ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
1134e399441dSJames Smart
1135c913a8b0SJames Smart if (!ret) {
1136e399441dSJames Smart /*
1137e399441dSJames Smart * No timeout/not interruptible as we need the struct
1138e399441dSJames Smart * to exist until the lldd calls us back. Thus mandate
1139e399441dSJames Smart * wait until driver calls back. lldd responsible for
1140e399441dSJames Smart * the timeout action
1141e399441dSJames Smart */
1142e399441dSJames Smart wait_for_completion(&lsop->ls_done);
1143e399441dSJames Smart
1144c913a8b0SJames Smart __nvme_fc_finish_ls_req(lsop);
1145e399441dSJames Smart
1146c913a8b0SJames Smart ret = lsop->ls_error;
1147e399441dSJames Smart }
1148e399441dSJames Smart
1149c913a8b0SJames Smart if (ret)
1150c913a8b0SJames Smart return ret;
1151c913a8b0SJames Smart
1152e399441dSJames Smart /* ACC or RJT payload ? */
1153e399441dSJames Smart if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1154e399441dSJames Smart return -ENXIO;
1155e399441dSJames Smart
1156e399441dSJames Smart return 0;
1157e399441dSJames Smart }
1158e399441dSJames Smart
1159c913a8b0SJames Smart static int
nvme_fc_send_ls_req_async(struct nvme_fc_rport * rport,struct nvmefc_ls_req_op * lsop,void (* done)(struct nvmefc_ls_req * req,int status))1160c913a8b0SJames Smart nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
1161e399441dSJames Smart struct nvmefc_ls_req_op *lsop,
1162e399441dSJames Smart void (*done)(struct nvmefc_ls_req *req, int status))
1163e399441dSJames Smart {
1164e399441dSJames Smart /* don't wait for completion */
1165e399441dSJames Smart
1166c913a8b0SJames Smart return __nvme_fc_send_ls_req(rport, lsop, done);
1167e399441dSJames Smart }
1168e399441dSJames Smart
1169e399441dSJames Smart static int
nvme_fc_connect_admin_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,u16 qsize,u16 ersp_ratio)1170e399441dSJames Smart nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1171e399441dSJames Smart struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1172e399441dSJames Smart {
1173e399441dSJames Smart struct nvmefc_ls_req_op *lsop;
1174e399441dSJames Smart struct nvmefc_ls_req *lsreq;
1175e399441dSJames Smart struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1176e399441dSJames Smart struct fcnvme_ls_cr_assoc_acc *assoc_acc;
117714fd1e98SJames Smart unsigned long flags;
1178e399441dSJames Smart int ret, fcret = 0;
1179e399441dSJames Smart
1180e399441dSJames Smart lsop = kzalloc((sizeof(*lsop) +
1181f56bf76fSJames Smart sizeof(*assoc_rqst) + sizeof(*assoc_acc) +
1182f56bf76fSJames Smart ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1183e399441dSJames Smart if (!lsop) {
1184f56bf76fSJames Smart dev_info(ctrl->ctrl.device,
1185f56bf76fSJames Smart "NVME-FC{%d}: send Create Association failed: ENOMEM\n",
1186f56bf76fSJames Smart ctrl->cnum);
1187e399441dSJames Smart ret = -ENOMEM;
1188e399441dSJames Smart goto out_no_memory;
1189e399441dSJames Smart }
1190e399441dSJames Smart
1191f56bf76fSJames Smart assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1];
1192e399441dSJames Smart assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1193f56bf76fSJames Smart lsreq = &lsop->ls_req;
1194f56bf76fSJames Smart if (ctrl->lport->ops->lsrqst_priv_sz)
1195f56bf76fSJames Smart lsreq->private = &assoc_acc[1];
1196f56bf76fSJames Smart else
1197f56bf76fSJames Smart lsreq->private = NULL;
1198e399441dSJames Smart
1199e399441dSJames Smart assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1200e399441dSJames Smart assoc_rqst->desc_list_len =
1201e399441dSJames Smart cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1202e399441dSJames Smart
1203e399441dSJames Smart assoc_rqst->assoc_cmd.desc_tag =
1204e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1205e399441dSJames Smart assoc_rqst->assoc_cmd.desc_len =
1206e399441dSJames Smart fcnvme_lsdesc_len(
1207e399441dSJames Smart sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1208e399441dSJames Smart
1209e399441dSJames Smart assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1210d157e534SJames Smart assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
1211e399441dSJames Smart /* Linux supports only Dynamic controllers */
1212e399441dSJames Smart assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
12138e412263SChristoph Hellwig uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1214e399441dSJames Smart strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1215e399441dSJames Smart min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1216e399441dSJames Smart strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1217e399441dSJames Smart min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
1218e399441dSJames Smart
1219e399441dSJames Smart lsop->queue = queue;
1220e399441dSJames Smart lsreq->rqstaddr = assoc_rqst;
1221e399441dSJames Smart lsreq->rqstlen = sizeof(*assoc_rqst);
1222e399441dSJames Smart lsreq->rspaddr = assoc_acc;
1223e399441dSJames Smart lsreq->rsplen = sizeof(*assoc_acc);
122453b2b2f5SJames Smart lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1225e399441dSJames Smart
1226c913a8b0SJames Smart ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1227e399441dSJames Smart if (ret)
1228e399441dSJames Smart goto out_free_buffer;
1229e399441dSJames Smart
1230e399441dSJames Smart /* process connect LS completion */
1231e399441dSJames Smart
1232e399441dSJames Smart /* validate the ACC response */
1233e399441dSJames Smart if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1234e399441dSJames Smart fcret = VERR_LSACC;
1235f77fc87cSJames Smart else if (assoc_acc->hdr.desc_list_len !=
1236e399441dSJames Smart fcnvme_lsdesc_len(
1237e399441dSJames Smart sizeof(struct fcnvme_ls_cr_assoc_acc)))
1238e399441dSJames Smart fcret = VERR_CR_ASSOC_ACC_LEN;
1239f77fc87cSJames Smart else if (assoc_acc->hdr.rqst.desc_tag !=
1240f77fc87cSJames Smart cpu_to_be32(FCNVME_LSDESC_RQST))
1241e399441dSJames Smart fcret = VERR_LSDESC_RQST;
1242e399441dSJames Smart else if (assoc_acc->hdr.rqst.desc_len !=
1243e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1244e399441dSJames Smart fcret = VERR_LSDESC_RQST_LEN;
1245e399441dSJames Smart else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1246e399441dSJames Smart fcret = VERR_CR_ASSOC;
1247e399441dSJames Smart else if (assoc_acc->associd.desc_tag !=
1248e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1249e399441dSJames Smart fcret = VERR_ASSOC_ID;
1250e399441dSJames Smart else if (assoc_acc->associd.desc_len !=
1251e399441dSJames Smart fcnvme_lsdesc_len(
1252e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id)))
1253e399441dSJames Smart fcret = VERR_ASSOC_ID_LEN;
1254e399441dSJames Smart else if (assoc_acc->connectid.desc_tag !=
1255e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1256e399441dSJames Smart fcret = VERR_CONN_ID;
1257e399441dSJames Smart else if (assoc_acc->connectid.desc_len !=
1258e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1259e399441dSJames Smart fcret = VERR_CONN_ID_LEN;
1260e399441dSJames Smart
1261e399441dSJames Smart if (fcret) {
1262e399441dSJames Smart ret = -EBADF;
1263e399441dSJames Smart dev_err(ctrl->dev,
12647db39484SJames Smart "q %d Create Association LS failed: %s\n",
1265e399441dSJames Smart queue->qnum, validation_errors[fcret]);
1266e399441dSJames Smart } else {
126714fd1e98SJames Smart spin_lock_irqsave(&ctrl->lock, flags);
1268e399441dSJames Smart ctrl->association_id =
1269e399441dSJames Smart be64_to_cpu(assoc_acc->associd.association_id);
1270e399441dSJames Smart queue->connection_id =
1271e399441dSJames Smart be64_to_cpu(assoc_acc->connectid.connection_id);
1272e399441dSJames Smart set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
127314fd1e98SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags);
1274e399441dSJames Smart }
1275e399441dSJames Smart
1276e399441dSJames Smart out_free_buffer:
1277e399441dSJames Smart kfree(lsop);
1278e399441dSJames Smart out_no_memory:
1279e399441dSJames Smart if (ret)
1280e399441dSJames Smart dev_err(ctrl->dev,
1281e399441dSJames Smart "queue %d connect admin queue failed (%d).\n",
1282e399441dSJames Smart queue->qnum, ret);
1283e399441dSJames Smart return ret;
1284e399441dSJames Smart }
1285e399441dSJames Smart
1286e399441dSJames Smart static int
nvme_fc_connect_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,u16 qsize,u16 ersp_ratio)1287e399441dSJames Smart nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1288e399441dSJames Smart u16 qsize, u16 ersp_ratio)
1289e399441dSJames Smart {
1290e399441dSJames Smart struct nvmefc_ls_req_op *lsop;
1291e399441dSJames Smart struct nvmefc_ls_req *lsreq;
1292e399441dSJames Smart struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1293e399441dSJames Smart struct fcnvme_ls_cr_conn_acc *conn_acc;
1294e399441dSJames Smart int ret, fcret = 0;
1295e399441dSJames Smart
1296e399441dSJames Smart lsop = kzalloc((sizeof(*lsop) +
1297f56bf76fSJames Smart sizeof(*conn_rqst) + sizeof(*conn_acc) +
1298f56bf76fSJames Smart ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1299e399441dSJames Smart if (!lsop) {
1300f56bf76fSJames Smart dev_info(ctrl->ctrl.device,
1301f56bf76fSJames Smart "NVME-FC{%d}: send Create Connection failed: ENOMEM\n",
1302f56bf76fSJames Smart ctrl->cnum);
1303e399441dSJames Smart ret = -ENOMEM;
1304e399441dSJames Smart goto out_no_memory;
1305e399441dSJames Smart }
1306e399441dSJames Smart
1307f56bf76fSJames Smart conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)&lsop[1];
1308e399441dSJames Smart conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1309f56bf76fSJames Smart lsreq = &lsop->ls_req;
1310f56bf76fSJames Smart if (ctrl->lport->ops->lsrqst_priv_sz)
1311f56bf76fSJames Smart lsreq->private = (void *)&conn_acc[1];
1312f56bf76fSJames Smart else
1313f56bf76fSJames Smart lsreq->private = NULL;
1314e399441dSJames Smart
1315e399441dSJames Smart conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1316e399441dSJames Smart conn_rqst->desc_list_len = cpu_to_be32(
1317e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id) +
1318e399441dSJames Smart sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1319e399441dSJames Smart
1320e399441dSJames Smart conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1321e399441dSJames Smart conn_rqst->associd.desc_len =
1322e399441dSJames Smart fcnvme_lsdesc_len(
1323e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id));
1324e399441dSJames Smart conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1325e399441dSJames Smart conn_rqst->connect_cmd.desc_tag =
1326e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1327e399441dSJames Smart conn_rqst->connect_cmd.desc_len =
1328e399441dSJames Smart fcnvme_lsdesc_len(
1329e399441dSJames Smart sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1330e399441dSJames Smart conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1331e399441dSJames Smart conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
1332d157e534SJames Smart conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
1333e399441dSJames Smart
1334e399441dSJames Smart lsop->queue = queue;
1335e399441dSJames Smart lsreq->rqstaddr = conn_rqst;
1336e399441dSJames Smart lsreq->rqstlen = sizeof(*conn_rqst);
1337e399441dSJames Smart lsreq->rspaddr = conn_acc;
1338e399441dSJames Smart lsreq->rsplen = sizeof(*conn_acc);
133953b2b2f5SJames Smart lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1340e399441dSJames Smart
1341c913a8b0SJames Smart ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1342e399441dSJames Smart if (ret)
1343e399441dSJames Smart goto out_free_buffer;
1344e399441dSJames Smart
1345e399441dSJames Smart /* process connect LS completion */
1346e399441dSJames Smart
1347e399441dSJames Smart /* validate the ACC response */
1348e399441dSJames Smart if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1349e399441dSJames Smart fcret = VERR_LSACC;
1350f77fc87cSJames Smart else if (conn_acc->hdr.desc_list_len !=
1351e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1352e399441dSJames Smart fcret = VERR_CR_CONN_ACC_LEN;
1353f77fc87cSJames Smart else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1354e399441dSJames Smart fcret = VERR_LSDESC_RQST;
1355e399441dSJames Smart else if (conn_acc->hdr.rqst.desc_len !=
1356e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1357e399441dSJames Smart fcret = VERR_LSDESC_RQST_LEN;
1358e399441dSJames Smart else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1359e399441dSJames Smart fcret = VERR_CR_CONN;
1360e399441dSJames Smart else if (conn_acc->connectid.desc_tag !=
1361e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1362e399441dSJames Smart fcret = VERR_CONN_ID;
1363e399441dSJames Smart else if (conn_acc->connectid.desc_len !=
1364e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1365e399441dSJames Smart fcret = VERR_CONN_ID_LEN;
1366e399441dSJames Smart
1367e399441dSJames Smart if (fcret) {
1368e399441dSJames Smart ret = -EBADF;
1369e399441dSJames Smart dev_err(ctrl->dev,
13707db39484SJames Smart "q %d Create I/O Connection LS failed: %s\n",
1371e399441dSJames Smart queue->qnum, validation_errors[fcret]);
1372e399441dSJames Smart } else {
1373e399441dSJames Smart queue->connection_id =
1374e399441dSJames Smart be64_to_cpu(conn_acc->connectid.connection_id);
1375e399441dSJames Smart set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1376e399441dSJames Smart }
1377e399441dSJames Smart
1378e399441dSJames Smart out_free_buffer:
1379e399441dSJames Smart kfree(lsop);
1380e399441dSJames Smart out_no_memory:
1381e399441dSJames Smart if (ret)
1382e399441dSJames Smart dev_err(ctrl->dev,
13837db39484SJames Smart "queue %d connect I/O queue failed (%d).\n",
1384e399441dSJames Smart queue->qnum, ret);
1385e399441dSJames Smart return ret;
1386e399441dSJames Smart }
1387e399441dSJames Smart
1388e399441dSJames Smart static void
nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req * lsreq,int status)1389e399441dSJames Smart nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1390e399441dSJames Smart {
1391e399441dSJames Smart struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1392e399441dSJames Smart
1393c913a8b0SJames Smart __nvme_fc_finish_ls_req(lsop);
1394e399441dSJames Smart
1395d4e4230cSMilan P. Gandhi /* fc-nvme initiator doesn't care about success or failure of cmd */
1396e399441dSJames Smart
1397e399441dSJames Smart kfree(lsop);
1398e399441dSJames Smart }
1399e399441dSJames Smart
1400e399441dSJames Smart /*
1401e399441dSJames Smart * This routine sends a FC-NVME LS to disconnect (aka terminate)
1402e399441dSJames Smart * the FC-NVME Association. Terminating the association also
1403e399441dSJames Smart * terminates the FC-NVME connections (per queue, both admin and io
1404e399441dSJames Smart * queues) that are part of the association. E.g. things are torn
1405e399441dSJames Smart * down, and the related FC-NVME Association ID and Connection IDs
1406e399441dSJames Smart * become invalid.
1407e399441dSJames Smart *
1408e399441dSJames Smart * The behavior of the fc-nvme initiator is such that it's
1409e399441dSJames Smart * understanding of the association and connections will implicitly
1410e399441dSJames Smart * be torn down. The action is implicit as it may be due to a loss of
1411e399441dSJames Smart * connectivity with the fc-nvme target, so you may never get a
1412e399441dSJames Smart * response even if you tried. As such, the action of this routine
1413e399441dSJames Smart * is to asynchronously send the LS, ignore any results of the LS, and
1414e399441dSJames Smart * continue on with terminating the association. If the fc-nvme target
1415e399441dSJames Smart * is present and receives the LS, it too can tear down.
1416e399441dSJames Smart */
1417e399441dSJames Smart static void
nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl * ctrl)1418e399441dSJames Smart nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1419e399441dSJames Smart {
142053b2b2f5SJames Smart struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
142153b2b2f5SJames Smart struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
1422e399441dSJames Smart struct nvmefc_ls_req_op *lsop;
1423e399441dSJames Smart struct nvmefc_ls_req *lsreq;
1424c913a8b0SJames Smart int ret;
1425e399441dSJames Smart
1426e399441dSJames Smart lsop = kzalloc((sizeof(*lsop) +
1427f56bf76fSJames Smart sizeof(*discon_rqst) + sizeof(*discon_acc) +
1428f56bf76fSJames Smart ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1429f56bf76fSJames Smart if (!lsop) {
1430f56bf76fSJames Smart dev_info(ctrl->ctrl.device,
1431f56bf76fSJames Smart "NVME-FC{%d}: send Disconnect Association "
1432f56bf76fSJames Smart "failed: ENOMEM\n",
1433f56bf76fSJames Smart ctrl->cnum);
1434e399441dSJames Smart return;
1435f56bf76fSJames Smart }
1436e399441dSJames Smart
1437f56bf76fSJames Smart discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)&lsop[1];
143853b2b2f5SJames Smart discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
1439f56bf76fSJames Smart lsreq = &lsop->ls_req;
1440f56bf76fSJames Smart if (ctrl->lport->ops->lsrqst_priv_sz)
1441f56bf76fSJames Smart lsreq->private = (void *)&discon_acc[1];
1442f56bf76fSJames Smart else
1443f56bf76fSJames Smart lsreq->private = NULL;
1444e399441dSJames Smart
1445fd5a5f22SJames Smart nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc,
1446fd5a5f22SJames Smart ctrl->association_id);
1447e399441dSJames Smart
1448c913a8b0SJames Smart ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1449c913a8b0SJames Smart nvme_fc_disconnect_assoc_done);
1450c913a8b0SJames Smart if (ret)
1451c913a8b0SJames Smart kfree(lsop);
1452e399441dSJames Smart }
1453e399441dSJames Smart
145414fd1e98SJames Smart static void
nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp * lsrsp)145514fd1e98SJames Smart nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
145614fd1e98SJames Smart {
145714fd1e98SJames Smart struct nvmefc_ls_rcv_op *lsop = lsrsp->nvme_fc_private;
145814fd1e98SJames Smart struct nvme_fc_rport *rport = lsop->rport;
145914fd1e98SJames Smart struct nvme_fc_lport *lport = rport->lport;
146014fd1e98SJames Smart unsigned long flags;
146114fd1e98SJames Smart
146214fd1e98SJames Smart spin_lock_irqsave(&rport->lock, flags);
146314fd1e98SJames Smart list_del(&lsop->lsrcv_list);
146414fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
146514fd1e98SJames Smart
146614fd1e98SJames Smart fc_dma_sync_single_for_cpu(lport->dev, lsop->rspdma,
146714fd1e98SJames Smart sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
146814fd1e98SJames Smart fc_dma_unmap_single(lport->dev, lsop->rspdma,
146914fd1e98SJames Smart sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
147014fd1e98SJames Smart
1471cf3d0084SChristophe JAILLET kfree(lsop->rspbuf);
1472cf3d0084SChristophe JAILLET kfree(lsop->rqstbuf);
147314fd1e98SJames Smart kfree(lsop);
147414fd1e98SJames Smart
147514fd1e98SJames Smart nvme_fc_rport_put(rport);
147614fd1e98SJames Smart }
147714fd1e98SJames Smart
147814fd1e98SJames Smart static void
nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op * lsop)147914fd1e98SJames Smart nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op *lsop)
148014fd1e98SJames Smart {
148114fd1e98SJames Smart struct nvme_fc_rport *rport = lsop->rport;
148214fd1e98SJames Smart struct nvme_fc_lport *lport = rport->lport;
148314fd1e98SJames Smart struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
148414fd1e98SJames Smart int ret;
148514fd1e98SJames Smart
148614fd1e98SJames Smart fc_dma_sync_single_for_device(lport->dev, lsop->rspdma,
148714fd1e98SJames Smart sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
148814fd1e98SJames Smart
148914fd1e98SJames Smart ret = lport->ops->xmt_ls_rsp(&lport->localport, &rport->remoteport,
149014fd1e98SJames Smart lsop->lsrsp);
149114fd1e98SJames Smart if (ret) {
149214fd1e98SJames Smart dev_warn(lport->dev,
149314fd1e98SJames Smart "LLDD rejected LS RSP xmt: LS %d status %d\n",
149414fd1e98SJames Smart w0->ls_cmd, ret);
149514fd1e98SJames Smart nvme_fc_xmt_ls_rsp_done(lsop->lsrsp);
149614fd1e98SJames Smart return;
149714fd1e98SJames Smart }
149814fd1e98SJames Smart }
149914fd1e98SJames Smart
150014fd1e98SJames Smart static struct nvme_fc_ctrl *
nvme_fc_match_disconn_ls(struct nvme_fc_rport * rport,struct nvmefc_ls_rcv_op * lsop)150114fd1e98SJames Smart nvme_fc_match_disconn_ls(struct nvme_fc_rport *rport,
150214fd1e98SJames Smart struct nvmefc_ls_rcv_op *lsop)
150314fd1e98SJames Smart {
150414fd1e98SJames Smart struct fcnvme_ls_disconnect_assoc_rqst *rqst =
150514fd1e98SJames Smart &lsop->rqstbuf->rq_dis_assoc;
150614fd1e98SJames Smart struct nvme_fc_ctrl *ctrl, *ret = NULL;
150714fd1e98SJames Smart struct nvmefc_ls_rcv_op *oldls = NULL;
150814fd1e98SJames Smart u64 association_id = be64_to_cpu(rqst->associd.association_id);
150914fd1e98SJames Smart unsigned long flags;
151014fd1e98SJames Smart
151114fd1e98SJames Smart spin_lock_irqsave(&rport->lock, flags);
151214fd1e98SJames Smart
151314fd1e98SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
151414fd1e98SJames Smart if (!nvme_fc_ctrl_get(ctrl))
151514fd1e98SJames Smart continue;
151614fd1e98SJames Smart spin_lock(&ctrl->lock);
151714fd1e98SJames Smart if (association_id == ctrl->association_id) {
151814fd1e98SJames Smart oldls = ctrl->rcv_disconn;
151914fd1e98SJames Smart ctrl->rcv_disconn = lsop;
152014fd1e98SJames Smart ret = ctrl;
152114fd1e98SJames Smart }
152214fd1e98SJames Smart spin_unlock(&ctrl->lock);
152314fd1e98SJames Smart if (ret)
152414fd1e98SJames Smart /* leave the ctrl get reference */
152514fd1e98SJames Smart break;
152614fd1e98SJames Smart nvme_fc_ctrl_put(ctrl);
152714fd1e98SJames Smart }
152814fd1e98SJames Smart
152914fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
153014fd1e98SJames Smart
153114fd1e98SJames Smart /* transmit a response for anything that was pending */
153214fd1e98SJames Smart if (oldls) {
153314fd1e98SJames Smart dev_info(rport->lport->dev,
153414fd1e98SJames Smart "NVME-FC{%d}: Multiple Disconnect Association "
153514fd1e98SJames Smart "LS's received\n", ctrl->cnum);
153614fd1e98SJames Smart /* overwrite good response with bogus failure */
153714fd1e98SJames Smart oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf,
153814fd1e98SJames Smart sizeof(*oldls->rspbuf),
153914fd1e98SJames Smart rqst->w0.ls_cmd,
154014fd1e98SJames Smart FCNVME_RJT_RC_UNAB,
154114fd1e98SJames Smart FCNVME_RJT_EXP_NONE, 0);
154214fd1e98SJames Smart nvme_fc_xmt_ls_rsp(oldls);
154314fd1e98SJames Smart }
154414fd1e98SJames Smart
154514fd1e98SJames Smart return ret;
154614fd1e98SJames Smart }
154714fd1e98SJames Smart
154814fd1e98SJames Smart /*
154914fd1e98SJames Smart * returns true to mean LS handled and ls_rsp can be sent
155014fd1e98SJames Smart * returns false to defer ls_rsp xmt (will be done as part of
155114fd1e98SJames Smart * association termination)
155214fd1e98SJames Smart */
155314fd1e98SJames Smart static bool
nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op * lsop)155414fd1e98SJames Smart nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op *lsop)
155514fd1e98SJames Smart {
155614fd1e98SJames Smart struct nvme_fc_rport *rport = lsop->rport;
155714fd1e98SJames Smart struct fcnvme_ls_disconnect_assoc_rqst *rqst =
155814fd1e98SJames Smart &lsop->rqstbuf->rq_dis_assoc;
155914fd1e98SJames Smart struct fcnvme_ls_disconnect_assoc_acc *acc =
156014fd1e98SJames Smart &lsop->rspbuf->rsp_dis_assoc;
156114fd1e98SJames Smart struct nvme_fc_ctrl *ctrl = NULL;
156214fd1e98SJames Smart int ret = 0;
156314fd1e98SJames Smart
156414fd1e98SJames Smart memset(acc, 0, sizeof(*acc));
156514fd1e98SJames Smart
156614fd1e98SJames Smart ret = nvmefc_vldt_lsreq_discon_assoc(lsop->rqstdatalen, rqst);
156714fd1e98SJames Smart if (!ret) {
156814fd1e98SJames Smart /* match an active association */
156914fd1e98SJames Smart ctrl = nvme_fc_match_disconn_ls(rport, lsop);
157014fd1e98SJames Smart if (!ctrl)
157114fd1e98SJames Smart ret = VERR_NO_ASSOC;
157214fd1e98SJames Smart }
157314fd1e98SJames Smart
157414fd1e98SJames Smart if (ret) {
157514fd1e98SJames Smart dev_info(rport->lport->dev,
157614fd1e98SJames Smart "Disconnect LS failed: %s\n",
157714fd1e98SJames Smart validation_errors[ret]);
157814fd1e98SJames Smart lsop->lsrsp->rsplen = nvme_fc_format_rjt(acc,
157914fd1e98SJames Smart sizeof(*acc), rqst->w0.ls_cmd,
158014fd1e98SJames Smart (ret == VERR_NO_ASSOC) ?
158114fd1e98SJames Smart FCNVME_RJT_RC_INV_ASSOC :
158214fd1e98SJames Smart FCNVME_RJT_RC_LOGIC,
158314fd1e98SJames Smart FCNVME_RJT_EXP_NONE, 0);
158414fd1e98SJames Smart return true;
158514fd1e98SJames Smart }
158614fd1e98SJames Smart
158714fd1e98SJames Smart /* format an ACCept response */
158814fd1e98SJames Smart
158914fd1e98SJames Smart lsop->lsrsp->rsplen = sizeof(*acc);
159014fd1e98SJames Smart
159114fd1e98SJames Smart nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
159214fd1e98SJames Smart fcnvme_lsdesc_len(
159314fd1e98SJames Smart sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
159414fd1e98SJames Smart FCNVME_LS_DISCONNECT_ASSOC);
159514fd1e98SJames Smart
159614fd1e98SJames Smart /*
159714fd1e98SJames Smart * the transmit of the response will occur after the exchanges
159814fd1e98SJames Smart * for the association have been ABTS'd by
159914fd1e98SJames Smart * nvme_fc_delete_association().
160014fd1e98SJames Smart */
160114fd1e98SJames Smart
160214fd1e98SJames Smart /* fail the association */
160314fd1e98SJames Smart nvme_fc_error_recovery(ctrl, "Disconnect Association LS received");
160414fd1e98SJames Smart
160514fd1e98SJames Smart /* release the reference taken by nvme_fc_match_disconn_ls() */
160614fd1e98SJames Smart nvme_fc_ctrl_put(ctrl);
160714fd1e98SJames Smart
160814fd1e98SJames Smart return false;
160914fd1e98SJames Smart }
161014fd1e98SJames Smart
161114fd1e98SJames Smart /*
161214fd1e98SJames Smart * Actual Processing routine for received FC-NVME LS Requests from the LLD
161314fd1e98SJames Smart * returns true if a response should be sent afterward, false if rsp will
161414fd1e98SJames Smart * be sent asynchronously.
161514fd1e98SJames Smart */
161614fd1e98SJames Smart static bool
nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op * lsop)161714fd1e98SJames Smart nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op *lsop)
161814fd1e98SJames Smart {
161914fd1e98SJames Smart struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
162014fd1e98SJames Smart bool ret = true;
162114fd1e98SJames Smart
162214fd1e98SJames Smart lsop->lsrsp->nvme_fc_private = lsop;
162314fd1e98SJames Smart lsop->lsrsp->rspbuf = lsop->rspbuf;
162414fd1e98SJames Smart lsop->lsrsp->rspdma = lsop->rspdma;
162514fd1e98SJames Smart lsop->lsrsp->done = nvme_fc_xmt_ls_rsp_done;
162614fd1e98SJames Smart /* Be preventative. handlers will later set to valid length */
162714fd1e98SJames Smart lsop->lsrsp->rsplen = 0;
162814fd1e98SJames Smart
162914fd1e98SJames Smart /*
163014fd1e98SJames Smart * handlers:
163114fd1e98SJames Smart * parse request input, execute the request, and format the
163214fd1e98SJames Smart * LS response
163314fd1e98SJames Smart */
163414fd1e98SJames Smart switch (w0->ls_cmd) {
163514fd1e98SJames Smart case FCNVME_LS_DISCONNECT_ASSOC:
163614fd1e98SJames Smart ret = nvme_fc_ls_disconnect_assoc(lsop);
163714fd1e98SJames Smart break;
163814fd1e98SJames Smart case FCNVME_LS_DISCONNECT_CONN:
163914fd1e98SJames Smart lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
164014fd1e98SJames Smart sizeof(*lsop->rspbuf), w0->ls_cmd,
164114fd1e98SJames Smart FCNVME_RJT_RC_UNSUP, FCNVME_RJT_EXP_NONE, 0);
164214fd1e98SJames Smart break;
164314fd1e98SJames Smart case FCNVME_LS_CREATE_ASSOCIATION:
164414fd1e98SJames Smart case FCNVME_LS_CREATE_CONNECTION:
164514fd1e98SJames Smart lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
164614fd1e98SJames Smart sizeof(*lsop->rspbuf), w0->ls_cmd,
164714fd1e98SJames Smart FCNVME_RJT_RC_LOGIC, FCNVME_RJT_EXP_NONE, 0);
164814fd1e98SJames Smart break;
164914fd1e98SJames Smart default:
165014fd1e98SJames Smart lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
165114fd1e98SJames Smart sizeof(*lsop->rspbuf), w0->ls_cmd,
165214fd1e98SJames Smart FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
165314fd1e98SJames Smart break;
165414fd1e98SJames Smart }
165514fd1e98SJames Smart
165614fd1e98SJames Smart return(ret);
165714fd1e98SJames Smart }
165814fd1e98SJames Smart
165914fd1e98SJames Smart static void
nvme_fc_handle_ls_rqst_work(struct work_struct * work)166014fd1e98SJames Smart nvme_fc_handle_ls_rqst_work(struct work_struct *work)
166114fd1e98SJames Smart {
166214fd1e98SJames Smart struct nvme_fc_rport *rport =
166314fd1e98SJames Smart container_of(work, struct nvme_fc_rport, lsrcv_work);
166414fd1e98SJames Smart struct fcnvme_ls_rqst_w0 *w0;
166514fd1e98SJames Smart struct nvmefc_ls_rcv_op *lsop;
166614fd1e98SJames Smart unsigned long flags;
166714fd1e98SJames Smart bool sendrsp;
166814fd1e98SJames Smart
166914fd1e98SJames Smart restart:
167014fd1e98SJames Smart sendrsp = true;
167114fd1e98SJames Smart spin_lock_irqsave(&rport->lock, flags);
167214fd1e98SJames Smart list_for_each_entry(lsop, &rport->ls_rcv_list, lsrcv_list) {
167314fd1e98SJames Smart if (lsop->handled)
167414fd1e98SJames Smart continue;
167514fd1e98SJames Smart
167614fd1e98SJames Smart lsop->handled = true;
167714fd1e98SJames Smart if (rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
167814fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
167914fd1e98SJames Smart sendrsp = nvme_fc_handle_ls_rqst(lsop);
168014fd1e98SJames Smart } else {
168114fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
168214fd1e98SJames Smart w0 = &lsop->rqstbuf->w0;
168314fd1e98SJames Smart lsop->lsrsp->rsplen = nvme_fc_format_rjt(
168414fd1e98SJames Smart lsop->rspbuf,
168514fd1e98SJames Smart sizeof(*lsop->rspbuf),
168614fd1e98SJames Smart w0->ls_cmd,
168714fd1e98SJames Smart FCNVME_RJT_RC_UNAB,
168814fd1e98SJames Smart FCNVME_RJT_EXP_NONE, 0);
168914fd1e98SJames Smart }
169014fd1e98SJames Smart if (sendrsp)
169114fd1e98SJames Smart nvme_fc_xmt_ls_rsp(lsop);
169214fd1e98SJames Smart goto restart;
169314fd1e98SJames Smart }
169414fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
169514fd1e98SJames Smart }
169614fd1e98SJames Smart
1697b2969585SChaitanya Kulkarni static
nvme_fc_rcv_ls_req_err_msg(struct nvme_fc_lport * lport,struct fcnvme_ls_rqst_w0 * w0)1698b2969585SChaitanya Kulkarni void nvme_fc_rcv_ls_req_err_msg(struct nvme_fc_lport *lport,
1699b2969585SChaitanya Kulkarni struct fcnvme_ls_rqst_w0 *w0)
1700b2969585SChaitanya Kulkarni {
1701b2969585SChaitanya Kulkarni dev_info(lport->dev, "RCV %s LS failed: No memory\n",
1702b2969585SChaitanya Kulkarni (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1703b2969585SChaitanya Kulkarni nvmefc_ls_names[w0->ls_cmd] : "");
1704b2969585SChaitanya Kulkarni }
1705b2969585SChaitanya Kulkarni
170672e6329fSJames Smart /**
170772e6329fSJames Smart * nvme_fc_rcv_ls_req - transport entry point called by an LLDD
170872e6329fSJames Smart * upon the reception of a NVME LS request.
170972e6329fSJames Smart *
171072e6329fSJames Smart * The nvme-fc layer will copy payload to an internal structure for
171172e6329fSJames Smart * processing. As such, upon completion of the routine, the LLDD may
171272e6329fSJames Smart * immediately free/reuse the LS request buffer passed in the call.
171372e6329fSJames Smart *
171472e6329fSJames Smart * If this routine returns error, the LLDD should abort the exchange.
171572e6329fSJames Smart *
17162afc4866SChaitanya Kulkarni * @portptr: pointer to the (registered) remote port that the LS
171772e6329fSJames Smart * was received from. The remoteport is associated with
171872e6329fSJames Smart * a specific localport.
171972e6329fSJames Smart * @lsrsp: pointer to a nvmefc_ls_rsp response structure to be
172072e6329fSJames Smart * used to reference the exchange corresponding to the LS
172172e6329fSJames Smart * when issuing an ls response.
172272e6329fSJames Smart * @lsreqbuf: pointer to the buffer containing the LS Request
172372e6329fSJames Smart * @lsreqbuf_len: length, in bytes, of the received LS request
172472e6329fSJames Smart */
172572e6329fSJames Smart int
nvme_fc_rcv_ls_req(struct nvme_fc_remote_port * portptr,struct nvmefc_ls_rsp * lsrsp,void * lsreqbuf,u32 lsreqbuf_len)172672e6329fSJames Smart nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
172772e6329fSJames Smart struct nvmefc_ls_rsp *lsrsp,
172872e6329fSJames Smart void *lsreqbuf, u32 lsreqbuf_len)
172972e6329fSJames Smart {
173072e6329fSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
173172e6329fSJames Smart struct nvme_fc_lport *lport = rport->lport;
173214fd1e98SJames Smart struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf;
173314fd1e98SJames Smart struct nvmefc_ls_rcv_op *lsop;
173414fd1e98SJames Smart unsigned long flags;
173514fd1e98SJames Smart int ret;
173614fd1e98SJames Smart
173714fd1e98SJames Smart nvme_fc_rport_get(rport);
173872e6329fSJames Smart
173972e6329fSJames Smart /* validate there's a routine to transmit a response */
174014fd1e98SJames Smart if (!lport->ops->xmt_ls_rsp) {
174114fd1e98SJames Smart dev_info(lport->dev,
174214fd1e98SJames Smart "RCV %s LS failed: no LLDD xmt_ls_rsp\n",
174314fd1e98SJames Smart (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
174414fd1e98SJames Smart nvmefc_ls_names[w0->ls_cmd] : "");
174514fd1e98SJames Smart ret = -EINVAL;
174614fd1e98SJames Smart goto out_put;
174714fd1e98SJames Smart }
174814fd1e98SJames Smart
174914fd1e98SJames Smart if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) {
175014fd1e98SJames Smart dev_info(lport->dev,
175114fd1e98SJames Smart "RCV %s LS failed: payload too large\n",
175214fd1e98SJames Smart (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
175314fd1e98SJames Smart nvmefc_ls_names[w0->ls_cmd] : "");
175414fd1e98SJames Smart ret = -E2BIG;
175514fd1e98SJames Smart goto out_put;
175614fd1e98SJames Smart }
175714fd1e98SJames Smart
1758cf3d0084SChristophe JAILLET lsop = kzalloc(sizeof(*lsop), GFP_KERNEL);
17596c90294dSChaitanya Kulkarni if (!lsop) {
1760b2969585SChaitanya Kulkarni nvme_fc_rcv_ls_req_err_msg(lport, w0);
17616c90294dSChaitanya Kulkarni ret = -ENOMEM;
17626c90294dSChaitanya Kulkarni goto out_put;
17636c90294dSChaitanya Kulkarni }
17646c90294dSChaitanya Kulkarni
1765cf3d0084SChristophe JAILLET lsop->rqstbuf = kzalloc(sizeof(*lsop->rqstbuf), GFP_KERNEL);
1766cf3d0084SChristophe JAILLET lsop->rspbuf = kzalloc(sizeof(*lsop->rspbuf), GFP_KERNEL);
17676c90294dSChaitanya Kulkarni if (!lsop->rqstbuf || !lsop->rspbuf) {
1768b2969585SChaitanya Kulkarni nvme_fc_rcv_ls_req_err_msg(lport, w0);
176914fd1e98SJames Smart ret = -ENOMEM;
1770cf3d0084SChristophe JAILLET goto out_free;
177114fd1e98SJames Smart }
177214fd1e98SJames Smart
177314fd1e98SJames Smart lsop->rspdma = fc_dma_map_single(lport->dev, lsop->rspbuf,
177414fd1e98SJames Smart sizeof(*lsop->rspbuf),
177514fd1e98SJames Smart DMA_TO_DEVICE);
177614fd1e98SJames Smart if (fc_dma_mapping_error(lport->dev, lsop->rspdma)) {
177714fd1e98SJames Smart dev_info(lport->dev,
177814fd1e98SJames Smart "RCV %s LS failed: DMA mapping failure\n",
177914fd1e98SJames Smart (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
178014fd1e98SJames Smart nvmefc_ls_names[w0->ls_cmd] : "");
178114fd1e98SJames Smart ret = -EFAULT;
178214fd1e98SJames Smart goto out_free;
178314fd1e98SJames Smart }
178414fd1e98SJames Smart
178514fd1e98SJames Smart lsop->rport = rport;
178614fd1e98SJames Smart lsop->lsrsp = lsrsp;
178714fd1e98SJames Smart
178814fd1e98SJames Smart memcpy(lsop->rqstbuf, lsreqbuf, lsreqbuf_len);
178914fd1e98SJames Smart lsop->rqstdatalen = lsreqbuf_len;
179014fd1e98SJames Smart
179114fd1e98SJames Smart spin_lock_irqsave(&rport->lock, flags);
179214fd1e98SJames Smart if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) {
179314fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
179414fd1e98SJames Smart ret = -ENOTCONN;
179514fd1e98SJames Smart goto out_unmap;
179614fd1e98SJames Smart }
179714fd1e98SJames Smart list_add_tail(&lsop->lsrcv_list, &rport->ls_rcv_list);
179814fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
179914fd1e98SJames Smart
180014fd1e98SJames Smart schedule_work(&rport->lsrcv_work);
180172e6329fSJames Smart
180272e6329fSJames Smart return 0;
180314fd1e98SJames Smart
180414fd1e98SJames Smart out_unmap:
180514fd1e98SJames Smart fc_dma_unmap_single(lport->dev, lsop->rspdma,
180614fd1e98SJames Smart sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
180714fd1e98SJames Smart out_free:
1808cf3d0084SChristophe JAILLET kfree(lsop->rspbuf);
1809cf3d0084SChristophe JAILLET kfree(lsop->rqstbuf);
181014fd1e98SJames Smart kfree(lsop);
181114fd1e98SJames Smart out_put:
181214fd1e98SJames Smart nvme_fc_rport_put(rport);
181314fd1e98SJames Smart return ret;
181472e6329fSJames Smart }
181572e6329fSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req);
181672e6329fSJames Smart
1817e399441dSJames Smart
1818e399441dSJames Smart /* *********************** NVME Ctrl Routines **************************** */
1819e399441dSJames Smart
1820e399441dSJames Smart static void
__nvme_fc_exit_request(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op)1821e399441dSJames Smart __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1822e399441dSJames Smart struct nvme_fc_fcp_op *op)
1823e399441dSJames Smart {
1824e399441dSJames Smart fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1825e399441dSJames Smart sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1826e399441dSJames Smart fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1827e399441dSJames Smart sizeof(op->cmd_iu), DMA_TO_DEVICE);
1828e399441dSJames Smart
1829e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_UNINIT);
1830e399441dSJames Smart }
1831e399441dSJames Smart
1832e399441dSJames Smart static void
nvme_fc_exit_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx)1833d6296d39SChristoph Hellwig nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1834d6296d39SChristoph Hellwig unsigned int hctx_idx)
1835e399441dSJames Smart {
1836e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1837e399441dSJames Smart
18381864ea46SChristoph Hellwig return __nvme_fc_exit_request(to_fc_ctrl(set->driver_data), op);
1839e399441dSJames Smart }
1840e399441dSJames Smart
184178a7ac26SJames Smart static int
__nvme_fc_abort_op(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op)184278a7ac26SJames Smart __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
184378a7ac26SJames Smart {
18443efd6e8eSJames Smart unsigned long flags;
18453efd6e8eSJames Smart int opstate;
184678a7ac26SJames Smart
18473efd6e8eSJames Smart spin_lock_irqsave(&ctrl->lock, flags);
18483efd6e8eSJames Smart opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
18493efd6e8eSJames Smart if (opstate != FCPOP_STATE_ACTIVE)
18503efd6e8eSJames Smart atomic_set(&op->state, opstate);
185152793d62SJames Smart else if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) {
185252793d62SJames Smart op->flags |= FCOP_FLAGS_TERMIO;
18533efd6e8eSJames Smart ctrl->iocnt++;
185452793d62SJames Smart }
18553efd6e8eSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags);
18563efd6e8eSJames Smart
18573efd6e8eSJames Smart if (opstate != FCPOP_STATE_ACTIVE)
185878a7ac26SJames Smart return -ECANCELED;
185978a7ac26SJames Smart
186078a7ac26SJames Smart ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
186178a7ac26SJames Smart &ctrl->rport->remoteport,
186278a7ac26SJames Smart op->queue->lldd_handle,
186378a7ac26SJames Smart &op->fcp_req);
186478a7ac26SJames Smart
186578a7ac26SJames Smart return 0;
186678a7ac26SJames Smart }
186778a7ac26SJames Smart
1868e399441dSJames Smart static void
nvme_fc_abort_aen_ops(struct nvme_fc_ctrl * ctrl)186978a7ac26SJames Smart nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1870e399441dSJames Smart {
1871e399441dSJames Smart struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
18723efd6e8eSJames Smart int i;
1873e399441dSJames Smart
18744cff280aSJames Smart /* ensure we've initialized the ops once */
18754cff280aSJames Smart if (!(aen_op->flags & FCOP_FLAGS_AEN))
18764cff280aSJames Smart return;
18774cff280aSJames Smart
18783efd6e8eSJames Smart for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
18793efd6e8eSJames Smart __nvme_fc_abort_op(ctrl, aen_op);
188078a7ac26SJames Smart }
188178a7ac26SJames Smart
1882c3aedd22SJames Smart static inline void
__nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op,int opstate)188378a7ac26SJames Smart __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
18843efd6e8eSJames Smart struct nvme_fc_fcp_op *op, int opstate)
188578a7ac26SJames Smart {
188678a7ac26SJames Smart unsigned long flags;
188778a7ac26SJames Smart
1888c3aedd22SJames Smart if (opstate == FCPOP_STATE_ABORTED) {
188978a7ac26SJames Smart spin_lock_irqsave(&ctrl->lock, flags);
189052793d62SJames Smart if (test_bit(FCCTRL_TERMIO, &ctrl->flags) &&
189152793d62SJames Smart op->flags & FCOP_FLAGS_TERMIO) {
189236715cf4SJames Smart if (!--ctrl->iocnt)
189336715cf4SJames Smart wake_up(&ctrl->ioabort_wait);
189436715cf4SJames Smart }
189578a7ac26SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags);
1896c3aedd22SJames Smart }
189778a7ac26SJames Smart }
1898e399441dSJames Smart
1899baee29acSChristoph Hellwig static void
nvme_fc_ctrl_ioerr_work(struct work_struct * work)190019fce047SJames Smart nvme_fc_ctrl_ioerr_work(struct work_struct *work)
190119fce047SJames Smart {
190219fce047SJames Smart struct nvme_fc_ctrl *ctrl =
190319fce047SJames Smart container_of(work, struct nvme_fc_ctrl, ioerr_work);
190419fce047SJames Smart
190519fce047SJames Smart nvme_fc_error_recovery(ctrl, "transport detected io error");
190619fce047SJames Smart }
190719fce047SJames Smart
1908827fc630SMuneendra Kumar /*
1909827fc630SMuneendra Kumar * nvme_fc_io_getuuid - Routine called to get the appid field
1910827fc630SMuneendra Kumar * associated with request by the lldd
1911827fc630SMuneendra Kumar * @req:IO request from nvme fc to driver
1912827fc630SMuneendra Kumar * Returns: UUID if there is an appid associated with VM or
1913827fc630SMuneendra Kumar * NULL if the user/libvirt has not set the appid to VM
1914827fc630SMuneendra Kumar */
nvme_fc_io_getuuid(struct nvmefc_fcp_req * req)1915827fc630SMuneendra Kumar char *nvme_fc_io_getuuid(struct nvmefc_fcp_req *req)
1916827fc630SMuneendra Kumar {
1917827fc630SMuneendra Kumar struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1918827fc630SMuneendra Kumar struct request *rq = op->rq;
1919827fc630SMuneendra Kumar
19208ae5b3a6SNigel Kirkland if (!IS_ENABLED(CONFIG_BLK_CGROUP_FC_APPID) || !rq || !rq->bio)
1921827fc630SMuneendra Kumar return NULL;
1922827fc630SMuneendra Kumar return blkcg_get_fc_appid(rq->bio);
1923827fc630SMuneendra Kumar }
1924827fc630SMuneendra Kumar EXPORT_SYMBOL_GPL(nvme_fc_io_getuuid);
1925827fc630SMuneendra Kumar
192619fce047SJames Smart static void
nvme_fc_fcpio_done(struct nvmefc_fcp_req * req)1927e399441dSJames Smart nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1928e399441dSJames Smart {
1929e399441dSJames Smart struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1930e399441dSJames Smart struct request *rq = op->rq;
1931e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req;
1932e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl;
1933e399441dSJames Smart struct nvme_fc_queue *queue = op->queue;
1934e399441dSJames Smart struct nvme_completion *cqe = &op->rsp_iu.cqe;
1935458f280dSJames Smart struct nvme_command *sqe = &op->cmd_iu.sqe;
1936d663b69fSChristoph Hellwig __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
193727fa9bc5SChristoph Hellwig union nvme_result result;
19380a02e39fSJames Smart bool terminate_assoc = true;
19393efd6e8eSJames Smart int opstate;
1940e399441dSJames Smart
1941e399441dSJames Smart /*
1942e399441dSJames Smart * WARNING:
1943e399441dSJames Smart * The current linux implementation of a nvme controller
1944e399441dSJames Smart * allocates a single tag set for all io queues and sizes
1945e399441dSJames Smart * the io queues to fully hold all possible tags. Thus, the
1946e399441dSJames Smart * implementation does not reference or care about the sqhd
1947e399441dSJames Smart * value as it never needs to use the sqhd/sqtail pointers
1948e399441dSJames Smart * for submission pacing.
1949e399441dSJames Smart *
1950e399441dSJames Smart * This affects the FC-NVME implementation in two ways:
1951e399441dSJames Smart * 1) As the value doesn't matter, we don't need to waste
1952e399441dSJames Smart * cycles extracting it from ERSPs and stamping it in the
1953e399441dSJames Smart * cases where the transport fabricates CQEs on successful
1954e399441dSJames Smart * completions.
1955e399441dSJames Smart * 2) The FC-NVME implementation requires that delivery of
1956e399441dSJames Smart * ERSP completions are to go back to the nvme layer in order
1957e399441dSJames Smart * relative to the rsn, such that the sqhd value will always
1958e399441dSJames Smart * be "in order" for the nvme layer. As the nvme layer in
1959e399441dSJames Smart * linux doesn't care about sqhd, there's no need to return
1960e399441dSJames Smart * them in order.
1961e399441dSJames Smart *
1962e399441dSJames Smart * Additionally:
1963e399441dSJames Smart * As the core nvme layer in linux currently does not look at
1964e399441dSJames Smart * every field in the cqe - in cases where the FC transport must
1965e399441dSJames Smart * fabricate a CQE, the following fields will not be set as they
1966e399441dSJames Smart * are not referenced:
1967e399441dSJames Smart * cqe.sqid, cqe.sqhd, cqe.command_id
1968f874d5d0SJames Smart *
1969f874d5d0SJames Smart * Failure or error of an individual i/o, in a transport
1970f874d5d0SJames Smart * detected fashion unrelated to the nvme completion status,
1971f874d5d0SJames Smart * potentially cause the initiator and target sides to get out
1972f874d5d0SJames Smart * of sync on SQ head/tail (aka outstanding io count allowed).
1973f874d5d0SJames Smart * Per FC-NVME spec, failure of an individual command requires
1974f874d5d0SJames Smart * the connection to be terminated, which in turn requires the
1975f874d5d0SJames Smart * association to be terminated.
1976e399441dSJames Smart */
1977e399441dSJames Smart
19783efd6e8eSJames Smart opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
19793efd6e8eSJames Smart
1980e399441dSJames Smart fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1981e399441dSJames Smart sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1982e399441dSJames Smart
19833efd6e8eSJames Smart if (opstate == FCPOP_STATE_ABORTED)
1984ae3afe63SHannes Reinecke status = cpu_to_le16(NVME_SC_HOST_ABORTED_CMD << 1);
198574bd8cbeSJames Smart else if (freq->status) {
198674bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
198774bd8cbeSJames Smart dev_info(ctrl->ctrl.device,
198874bd8cbeSJames Smart "NVME-FC{%d}: io failed due to lldd error %d\n",
198974bd8cbeSJames Smart ctrl->cnum, freq->status);
199074bd8cbeSJames Smart }
1991e399441dSJames Smart
1992e399441dSJames Smart /*
1993e399441dSJames Smart * For the linux implementation, if we have an unsuccesful
1994e399441dSJames Smart * status, they blk-mq layer can typically be called with the
1995e399441dSJames Smart * non-zero status and the content of the cqe isn't important.
1996e399441dSJames Smart */
1997e399441dSJames Smart if (status)
1998e399441dSJames Smart goto done;
1999e399441dSJames Smart
2000e399441dSJames Smart /*
2001e399441dSJames Smart * command completed successfully relative to the wire
2002e399441dSJames Smart * protocol. However, validate anything received and
2003e399441dSJames Smart * extract the status and result from the cqe (create it
2004e399441dSJames Smart * where necessary).
2005e399441dSJames Smart */
2006e399441dSJames Smart
2007e399441dSJames Smart switch (freq->rcv_rsplen) {
2008e399441dSJames Smart
2009e399441dSJames Smart case 0:
2010e399441dSJames Smart case NVME_FC_SIZEOF_ZEROS_RSP:
2011e399441dSJames Smart /*
2012e399441dSJames Smart * No response payload or 12 bytes of payload (which
2013e399441dSJames Smart * should all be zeros) are considered successful and
2014e399441dSJames Smart * no payload in the CQE by the transport.
2015e399441dSJames Smart */
2016e399441dSJames Smart if (freq->transferred_length !=
2017e399441dSJames Smart be32_to_cpu(op->cmd_iu.data_len)) {
201874bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
201974bd8cbeSJames Smart dev_info(ctrl->ctrl.device,
202074bd8cbeSJames Smart "NVME-FC{%d}: io failed due to bad transfer "
202174bd8cbeSJames Smart "length: %d vs expected %d\n",
202274bd8cbeSJames Smart ctrl->cnum, freq->transferred_length,
202374bd8cbeSJames Smart be32_to_cpu(op->cmd_iu.data_len));
2024e399441dSJames Smart goto done;
2025e399441dSJames Smart }
202627fa9bc5SChristoph Hellwig result.u64 = 0;
2027e399441dSJames Smart break;
2028e399441dSJames Smart
2029e399441dSJames Smart case sizeof(struct nvme_fc_ersp_iu):
2030e399441dSJames Smart /*
2031e399441dSJames Smart * The ERSP IU contains a full completion with CQE.
2032e399441dSJames Smart * Validate ERSP IU and look at cqe.
2033e399441dSJames Smart */
2034e399441dSJames Smart if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
2035e399441dSJames Smart (freq->rcv_rsplen / 4) ||
2036e399441dSJames Smart be32_to_cpu(op->rsp_iu.xfrd_len) !=
2037e399441dSJames Smart freq->transferred_length ||
203853b2b2f5SJames Smart op->rsp_iu.ersp_result ||
2039458f280dSJames Smart sqe->common.command_id != cqe->command_id)) {
204074bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
204174bd8cbeSJames Smart dev_info(ctrl->ctrl.device,
204274bd8cbeSJames Smart "NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
204374bd8cbeSJames Smart "iu len %d, xfr len %d vs %d, status code "
204474bd8cbeSJames Smart "%d, cmdid %d vs %d\n",
204574bd8cbeSJames Smart ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len),
204674bd8cbeSJames Smart be32_to_cpu(op->rsp_iu.xfrd_len),
204774bd8cbeSJames Smart freq->transferred_length,
204853b2b2f5SJames Smart op->rsp_iu.ersp_result,
204974bd8cbeSJames Smart sqe->common.command_id,
205074bd8cbeSJames Smart cqe->command_id);
2051e399441dSJames Smart goto done;
2052e399441dSJames Smart }
205327fa9bc5SChristoph Hellwig result = cqe->result;
2054d663b69fSChristoph Hellwig status = cqe->status;
2055e399441dSJames Smart break;
2056e399441dSJames Smart
2057e399441dSJames Smart default:
205874bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
205974bd8cbeSJames Smart dev_info(ctrl->ctrl.device,
206074bd8cbeSJames Smart "NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
206174bd8cbeSJames Smart "len %d\n",
206274bd8cbeSJames Smart ctrl->cnum, freq->rcv_rsplen);
2063e399441dSJames Smart goto done;
2064e399441dSJames Smart }
2065e399441dSJames Smart
2066f874d5d0SJames Smart terminate_assoc = false;
2067f874d5d0SJames Smart
2068e399441dSJames Smart done:
206978a7ac26SJames Smart if (op->flags & FCOP_FLAGS_AEN) {
207027fa9bc5SChristoph Hellwig nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
20713efd6e8eSJames Smart __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
207278a7ac26SJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE);
207378a7ac26SJames Smart op->flags = FCOP_FLAGS_AEN; /* clear other flags */
2074e399441dSJames Smart nvme_fc_ctrl_put(ctrl);
2075f874d5d0SJames Smart goto check_error;
2076e399441dSJames Smart }
2077e399441dSJames Smart
2078c3aedd22SJames Smart __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
20792eb81a33SChristoph Hellwig if (!nvme_try_complete_req(rq, status, result))
2080ff029451SChristoph Hellwig nvme_fc_complete_rq(rq);
2081f874d5d0SJames Smart
2082f874d5d0SJames Smart check_error:
2083f20ef34dSJames Smart if (terminate_assoc && ctrl->ctrl.state != NVME_CTRL_RESETTING)
208419fce047SJames Smart queue_work(nvme_reset_wq, &ctrl->ioerr_work);
2085e399441dSJames Smart }
2086e399441dSJames Smart
2087e399441dSJames Smart static int
__nvme_fc_init_request(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,struct nvme_fc_fcp_op * op,struct request * rq,u32 rqno)2088e399441dSJames Smart __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
2089e399441dSJames Smart struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
2090e399441dSJames Smart struct request *rq, u32 rqno)
2091e399441dSJames Smart {
2092d3d0bc78SBart Van Assche struct nvme_fcp_op_w_sgl *op_w_sgl =
2093d3d0bc78SBart Van Assche container_of(op, typeof(*op_w_sgl), op);
2094e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2095e399441dSJames Smart int ret = 0;
2096e399441dSJames Smart
2097e399441dSJames Smart memset(op, 0, sizeof(*op));
2098e399441dSJames Smart op->fcp_req.cmdaddr = &op->cmd_iu;
2099e399441dSJames Smart op->fcp_req.cmdlen = sizeof(op->cmd_iu);
2100e399441dSJames Smart op->fcp_req.rspaddr = &op->rsp_iu;
2101e399441dSJames Smart op->fcp_req.rsplen = sizeof(op->rsp_iu);
2102e399441dSJames Smart op->fcp_req.done = nvme_fc_fcpio_done;
2103e399441dSJames Smart op->ctrl = ctrl;
2104e399441dSJames Smart op->queue = queue;
2105e399441dSJames Smart op->rq = rq;
2106e399441dSJames Smart op->rqno = rqno;
2107e399441dSJames Smart
210853b2b2f5SJames Smart cmdiu->format_id = NVME_CMD_FORMAT_ID;
2109e399441dSJames Smart cmdiu->fc_id = NVME_CMD_FC_ID;
2110e399441dSJames Smart cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
211144fbf3bbSJames Smart if (queue->qnum)
211244fbf3bbSJames Smart cmdiu->rsv_cat = fccmnd_set_cat_css(0,
211344fbf3bbSJames Smart (NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT));
211444fbf3bbSJames Smart else
211544fbf3bbSJames Smart cmdiu->rsv_cat = fccmnd_set_cat_admin(0);
2116e399441dSJames Smart
2117e399441dSJames Smart op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
2118e399441dSJames Smart &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
2119e399441dSJames Smart if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
2120e399441dSJames Smart dev_err(ctrl->dev,
2121e399441dSJames Smart "FCP Op failed - cmdiu dma mapping failed.\n");
2122f34448cdSTianjia Zhang ret = -EFAULT;
2123e399441dSJames Smart goto out_on_error;
2124e399441dSJames Smart }
2125e399441dSJames Smart
2126e399441dSJames Smart op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
2127e399441dSJames Smart &op->rsp_iu, sizeof(op->rsp_iu),
2128e399441dSJames Smart DMA_FROM_DEVICE);
2129e399441dSJames Smart if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
2130e399441dSJames Smart dev_err(ctrl->dev,
2131e399441dSJames Smart "FCP Op failed - rspiu dma mapping failed.\n");
2132f34448cdSTianjia Zhang ret = -EFAULT;
2133e399441dSJames Smart }
2134e399441dSJames Smart
2135e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE);
2136e399441dSJames Smart out_on_error:
2137e399441dSJames Smart return ret;
2138e399441dSJames Smart }
2139e399441dSJames Smart
2140e399441dSJames Smart static int
nvme_fc_init_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx,unsigned int numa_node)2141d6296d39SChristoph Hellwig nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
2142d6296d39SChristoph Hellwig unsigned int hctx_idx, unsigned int numa_node)
2143e399441dSJames Smart {
21441864ea46SChristoph Hellwig struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
2145d3d0bc78SBart Van Assche struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq);
214676f983cbSChristoph Hellwig int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
214776f983cbSChristoph Hellwig struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
21480d2bdf9fSBart Van Assche int res;
2149e399441dSJames Smart
21500d2bdf9fSBart Van Assche res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++);
21510d2bdf9fSBart Van Assche if (res)
21520d2bdf9fSBart Van Assche return res;
21533add1d93SArnd Bergmann op->op.fcp_req.first_sgl = op->sgl;
2154d19b8bc8SJames Smart op->op.fcp_req.private = &op->priv[0];
2155dfa74422SEwan D. Milne nvme_req(rq)->ctrl = &ctrl->ctrl;
2156f4b9e6c9SKeith Busch nvme_req(rq)->cmd = &op->op.cmd_iu.sqe;
21570d2bdf9fSBart Van Assche return res;
2158e399441dSJames Smart }
2159e399441dSJames Smart
2160e399441dSJames Smart static int
nvme_fc_init_aen_ops(struct nvme_fc_ctrl * ctrl)2161e399441dSJames Smart nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
2162e399441dSJames Smart {
2163e399441dSJames Smart struct nvme_fc_fcp_op *aen_op;
2164e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu;
2165e399441dSJames Smart struct nvme_command *sqe;
2166f56bf76fSJames Smart void *private = NULL;
2167e399441dSJames Smart int i, ret;
2168e399441dSJames Smart
2169e399441dSJames Smart aen_op = ctrl->aen_ops;
217038dabe21SKeith Busch for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2171f56bf76fSJames Smart if (ctrl->lport->ops->fcprqst_priv_sz) {
217261bff8efSJames Smart private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
217361bff8efSJames Smart GFP_KERNEL);
217461bff8efSJames Smart if (!private)
217561bff8efSJames Smart return -ENOMEM;
2176f56bf76fSJames Smart }
217761bff8efSJames Smart
2178e399441dSJames Smart cmdiu = &aen_op->cmd_iu;
2179e399441dSJames Smart sqe = &cmdiu->sqe;
2180e399441dSJames Smart ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
2181e399441dSJames Smart aen_op, (struct request *)NULL,
218238dabe21SKeith Busch (NVME_AQ_BLK_MQ_DEPTH + i));
218361bff8efSJames Smart if (ret) {
218461bff8efSJames Smart kfree(private);
2185e399441dSJames Smart return ret;
218661bff8efSJames Smart }
2187e399441dSJames Smart
218878a7ac26SJames Smart aen_op->flags = FCOP_FLAGS_AEN;
218961bff8efSJames Smart aen_op->fcp_req.private = private;
219078a7ac26SJames Smart
2191e399441dSJames Smart memset(sqe, 0, sizeof(*sqe));
2192e399441dSJames Smart sqe->common.opcode = nvme_admin_async_event;
219378a7ac26SJames Smart /* Note: core layer may overwrite the sqe.command_id value */
219438dabe21SKeith Busch sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
2195e399441dSJames Smart }
2196e399441dSJames Smart return 0;
2197e399441dSJames Smart }
2198e399441dSJames Smart
219961bff8efSJames Smart static void
nvme_fc_term_aen_ops(struct nvme_fc_ctrl * ctrl)220061bff8efSJames Smart nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
220161bff8efSJames Smart {
220261bff8efSJames Smart struct nvme_fc_fcp_op *aen_op;
220361bff8efSJames Smart int i;
220461bff8efSJames Smart
2205e126e821SDavid Milburn cancel_work_sync(&ctrl->ctrl.async_event_work);
220661bff8efSJames Smart aen_op = ctrl->aen_ops;
220738dabe21SKeith Busch for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
220861bff8efSJames Smart __nvme_fc_exit_request(ctrl, aen_op);
220961bff8efSJames Smart
221061bff8efSJames Smart kfree(aen_op->fcp_req.private);
221161bff8efSJames Smart aen_op->fcp_req.private = NULL;
221261bff8efSJames Smart }
221361bff8efSJames Smart }
2214e399441dSJames Smart
22151864ea46SChristoph Hellwig static inline int
__nvme_fc_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int qidx)22161864ea46SChristoph Hellwig __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int qidx)
2217e399441dSJames Smart {
22181864ea46SChristoph Hellwig struct nvme_fc_ctrl *ctrl = to_fc_ctrl(data);
2219e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[qidx];
2220e399441dSJames Smart
2221e399441dSJames Smart hctx->driver_data = queue;
2222e399441dSJames Smart queue->hctx = hctx;
22231864ea46SChristoph Hellwig return 0;
2224e399441dSJames Smart }
2225e399441dSJames Smart
2226e399441dSJames Smart static int
nvme_fc_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)22271864ea46SChristoph Hellwig nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int hctx_idx)
2228e399441dSJames Smart {
22291864ea46SChristoph Hellwig return __nvme_fc_init_hctx(hctx, data, hctx_idx + 1);
2230e399441dSJames Smart }
2231e399441dSJames Smart
2232e399441dSJames Smart static int
nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)2233e399441dSJames Smart nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2234e399441dSJames Smart unsigned int hctx_idx)
2235e399441dSJames Smart {
22361864ea46SChristoph Hellwig return __nvme_fc_init_hctx(hctx, data, hctx_idx);
2237e399441dSJames Smart }
2238e399441dSJames Smart
2239e399441dSJames Smart static void
nvme_fc_init_queue(struct nvme_fc_ctrl * ctrl,int idx)224008e15075SKeith Busch nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
2241e399441dSJames Smart {
2242e399441dSJames Smart struct nvme_fc_queue *queue;
2243e399441dSJames Smart
2244e399441dSJames Smart queue = &ctrl->queues[idx];
2245e399441dSJames Smart memset(queue, 0, sizeof(*queue));
2246e399441dSJames Smart queue->ctrl = ctrl;
2247e399441dSJames Smart queue->qnum = idx;
224867f471b6SJames Smart atomic_set(&queue->csn, 0);
2249e399441dSJames Smart queue->dev = ctrl->dev;
2250e399441dSJames Smart
2251e399441dSJames Smart if (idx > 0)
2252e399441dSJames Smart queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
2253e399441dSJames Smart else
2254e399441dSJames Smart queue->cmnd_capsule_len = sizeof(struct nvme_command);
2255e399441dSJames Smart
2256e399441dSJames Smart /*
2257e399441dSJames Smart * Considered whether we should allocate buffers for all SQEs
2258e399441dSJames Smart * and CQEs and dma map them - mapping their respective entries
2259e399441dSJames Smart * into the request structures (kernel vm addr and dma address)
2260e399441dSJames Smart * thus the driver could use the buffers/mappings directly.
2261e399441dSJames Smart * It only makes sense if the LLDD would use them for its
2262e399441dSJames Smart * messaging api. It's very unlikely most adapter api's would use
2263e399441dSJames Smart * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
2264e399441dSJames Smart * structures were used instead.
2265e399441dSJames Smart */
2266e399441dSJames Smart }
2267e399441dSJames Smart
2268e399441dSJames Smart /*
2269e399441dSJames Smart * This routine terminates a queue at the transport level.
2270e399441dSJames Smart * The transport has already ensured that all outstanding ios on
2271e399441dSJames Smart * the queue have been terminated.
2272e399441dSJames Smart * The transport will send a Disconnect LS request to terminate
2273e399441dSJames Smart * the queue's connection. Termination of the admin queue will also
2274e399441dSJames Smart * terminate the association at the target.
2275e399441dSJames Smart */
2276e399441dSJames Smart static void
nvme_fc_free_queue(struct nvme_fc_queue * queue)2277e399441dSJames Smart nvme_fc_free_queue(struct nvme_fc_queue *queue)
2278e399441dSJames Smart {
2279e399441dSJames Smart if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
2280e399441dSJames Smart return;
2281e399441dSJames Smart
22829e0ed16aSSagi Grimberg clear_bit(NVME_FC_Q_LIVE, &queue->flags);
2283e399441dSJames Smart /*
2284e399441dSJames Smart * Current implementation never disconnects a single queue.
2285e399441dSJames Smart * It always terminates a whole association. So there is never
2286e399441dSJames Smart * a disconnect(queue) LS sent to the target.
2287e399441dSJames Smart */
2288e399441dSJames Smart
2289e399441dSJames Smart queue->connection_id = 0;
229067f471b6SJames Smart atomic_set(&queue->csn, 0);
2291e399441dSJames Smart }
2292e399441dSJames Smart
2293e399441dSJames Smart static void
__nvme_fc_delete_hw_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,unsigned int qidx)2294e399441dSJames Smart __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
2295e399441dSJames Smart struct nvme_fc_queue *queue, unsigned int qidx)
2296e399441dSJames Smart {
2297e399441dSJames Smart if (ctrl->lport->ops->delete_queue)
2298e399441dSJames Smart ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
2299e399441dSJames Smart queue->lldd_handle);
2300e399441dSJames Smart queue->lldd_handle = NULL;
2301e399441dSJames Smart }
2302e399441dSJames Smart
2303e399441dSJames Smart static void
nvme_fc_free_io_queues(struct nvme_fc_ctrl * ctrl)2304e399441dSJames Smart nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
2305e399441dSJames Smart {
2306e399441dSJames Smart int i;
2307e399441dSJames Smart
2308d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++)
2309e399441dSJames Smart nvme_fc_free_queue(&ctrl->queues[i]);
2310e399441dSJames Smart }
2311e399441dSJames Smart
2312e399441dSJames Smart static int
__nvme_fc_create_hw_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,unsigned int qidx,u16 qsize)2313e399441dSJames Smart __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
2314e399441dSJames Smart struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
2315e399441dSJames Smart {
2316e399441dSJames Smart int ret = 0;
2317e399441dSJames Smart
2318e399441dSJames Smart queue->lldd_handle = NULL;
2319e399441dSJames Smart if (ctrl->lport->ops->create_queue)
2320e399441dSJames Smart ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
2321e399441dSJames Smart qidx, qsize, &queue->lldd_handle);
2322e399441dSJames Smart
2323e399441dSJames Smart return ret;
2324e399441dSJames Smart }
2325e399441dSJames Smart
2326e399441dSJames Smart static void
nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl * ctrl)2327e399441dSJames Smart nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
2328e399441dSJames Smart {
2329d858e5f0SSagi Grimberg struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
2330e399441dSJames Smart int i;
2331e399441dSJames Smart
2332d858e5f0SSagi Grimberg for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
2333e399441dSJames Smart __nvme_fc_delete_hw_queue(ctrl, queue, i);
2334e399441dSJames Smart }
2335e399441dSJames Smart
2336e399441dSJames Smart static int
nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl * ctrl,u16 qsize)2337e399441dSJames Smart nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2338e399441dSJames Smart {
2339e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[1];
234017a1ec08SJohannes Thumshirn int i, ret;
2341e399441dSJames Smart
2342d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
2343e399441dSJames Smart ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
234417a1ec08SJohannes Thumshirn if (ret)
234517a1ec08SJohannes Thumshirn goto delete_queues;
2346e399441dSJames Smart }
2347e399441dSJames Smart
2348e399441dSJames Smart return 0;
234917a1ec08SJohannes Thumshirn
235017a1ec08SJohannes Thumshirn delete_queues:
2351514a6dc9SJames Smart for (; i > 0; i--)
235217a1ec08SJohannes Thumshirn __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
235317a1ec08SJohannes Thumshirn return ret;
2354e399441dSJames Smart }
2355e399441dSJames Smart
2356e399441dSJames Smart static int
nvme_fc_connect_io_queues(struct nvme_fc_ctrl * ctrl,u16 qsize)2357e399441dSJames Smart nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2358e399441dSJames Smart {
2359e399441dSJames Smart int i, ret = 0;
2360e399441dSJames Smart
2361d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++) {
2362e399441dSJames Smart ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
2363e399441dSJames Smart (qsize / 5));
2364e399441dSJames Smart if (ret)
2365e399441dSJames Smart break;
2366be42a33bSKeith Busch ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
2367e399441dSJames Smart if (ret)
2368e399441dSJames Smart break;
23699e0ed16aSSagi Grimberg
23709e0ed16aSSagi Grimberg set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
2371e399441dSJames Smart }
2372e399441dSJames Smart
2373e399441dSJames Smart return ret;
2374e399441dSJames Smart }
2375e399441dSJames Smart
2376e399441dSJames Smart static void
nvme_fc_init_io_queues(struct nvme_fc_ctrl * ctrl)2377e399441dSJames Smart nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
2378e399441dSJames Smart {
2379e399441dSJames Smart int i;
2380e399441dSJames Smart
2381d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++)
238208e15075SKeith Busch nvme_fc_init_queue(ctrl, i);
2383e399441dSJames Smart }
2384e399441dSJames Smart
2385e399441dSJames Smart static void
nvme_fc_ctrl_free(struct kref * ref)2386e399441dSJames Smart nvme_fc_ctrl_free(struct kref *ref)
2387e399441dSJames Smart {
2388e399441dSJames Smart struct nvme_fc_ctrl *ctrl =
2389e399441dSJames Smart container_of(ref, struct nvme_fc_ctrl, ref);
2390e399441dSJames Smart unsigned long flags;
2391e399441dSJames Smart
23926dfba1c0SChristoph Hellwig if (ctrl->ctrl.tagset)
23936dfba1c0SChristoph Hellwig nvme_remove_io_tag_set(&ctrl->ctrl);
239461bff8efSJames Smart
2395e399441dSJames Smart /* remove from rport list */
2396e399441dSJames Smart spin_lock_irqsave(&ctrl->rport->lock, flags);
2397e399441dSJames Smart list_del(&ctrl->ctrl_list);
2398e399441dSJames Smart spin_unlock_irqrestore(&ctrl->rport->lock, flags);
239961bff8efSJames Smart
24009f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(&ctrl->ctrl);
24016dfba1c0SChristoph Hellwig nvme_remove_admin_tag_set(&ctrl->ctrl);
240261bff8efSJames Smart
240361bff8efSJames Smart kfree(ctrl->queues);
2404e399441dSJames Smart
2405e399441dSJames Smart put_device(ctrl->dev);
2406e399441dSJames Smart nvme_fc_rport_put(ctrl->rport);
2407e399441dSJames Smart
24083dd83f40SSagi Grimberg ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
2409de41447aSEwan D. Milne if (ctrl->ctrl.opts)
2410e399441dSJames Smart nvmf_free_options(ctrl->ctrl.opts);
2411e399441dSJames Smart kfree(ctrl);
2412e399441dSJames Smart }
2413e399441dSJames Smart
2414e399441dSJames Smart static void
nvme_fc_ctrl_put(struct nvme_fc_ctrl * ctrl)2415e399441dSJames Smart nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2416e399441dSJames Smart {
2417e399441dSJames Smart kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2418e399441dSJames Smart }
2419e399441dSJames Smart
2420e399441dSJames Smart static int
nvme_fc_ctrl_get(struct nvme_fc_ctrl * ctrl)2421e399441dSJames Smart nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2422e399441dSJames Smart {
2423e399441dSJames Smart return kref_get_unless_zero(&ctrl->ref);
2424e399441dSJames Smart }
2425e399441dSJames Smart
2426e399441dSJames Smart /*
2427e399441dSJames Smart * All accesses from nvme core layer done - can now free the
2428e399441dSJames Smart * controller. Called after last nvme_put_ctrl() call
2429e399441dSJames Smart */
2430e399441dSJames Smart static void
nvme_fc_nvme_ctrl_freed(struct nvme_ctrl * nctrl)243161bff8efSJames Smart nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
2432e399441dSJames Smart {
2433e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2434e399441dSJames Smart
2435e399441dSJames Smart WARN_ON(nctrl != &ctrl->ctrl);
2436e399441dSJames Smart
2437e399441dSJames Smart nvme_fc_ctrl_put(ctrl);
2438e399441dSJames Smart }
2439e399441dSJames Smart
244095ced8a2SJames Smart /*
244195ced8a2SJames Smart * This routine is used by the transport when it needs to find active
244295ced8a2SJames Smart * io on a queue that is to be terminated. The transport uses
244395ced8a2SJames Smart * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
244495ced8a2SJames Smart * this routine to kill them on a 1 by 1 basis.
244595ced8a2SJames Smart *
244695ced8a2SJames Smart * As FC allocates FC exchange for each io, the transport must contact
244795ced8a2SJames Smart * the LLDD to terminate the exchange, thus releasing the FC exchange.
244895ced8a2SJames Smart * After terminating the exchange the LLDD will call the transport's
244995ced8a2SJames Smart * normal io done path for the request, but it will have an aborted
245095ced8a2SJames Smart * status. The done path will return the io request back to the block
245195ced8a2SJames Smart * layer with an error status.
245295ced8a2SJames Smart */
nvme_fc_terminate_exchange(struct request * req,void * data)24532dd6532eSJohn Garry static bool nvme_fc_terminate_exchange(struct request *req, void *data)
245495ced8a2SJames Smart {
245595ced8a2SJames Smart struct nvme_ctrl *nctrl = data;
245695ced8a2SJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
245795ced8a2SJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
245895ced8a2SJames Smart
24593c7aafbcSHannes Reinecke op->nreq.flags |= NVME_REQ_CANCELLED;
246095ced8a2SJames Smart __nvme_fc_abort_op(ctrl, op);
246195ced8a2SJames Smart return true;
246295ced8a2SJames Smart }
246395ced8a2SJames Smart
246495ced8a2SJames Smart /*
246595ced8a2SJames Smart * This routine runs through all outstanding commands on the association
246695ced8a2SJames Smart * and aborts them. This routine is typically be called by the
246795ced8a2SJames Smart * delete_association routine. It is also called due to an error during
246895ced8a2SJames Smart * reconnect. In that scenario, it is most likely a command that initializes
246995ced8a2SJames Smart * the controller, including fabric Connect commands on io queues, that
247095ced8a2SJames Smart * may have timed out or failed thus the io must be killed for the connect
247195ced8a2SJames Smart * thread to see the error.
247295ced8a2SJames Smart */
247395ced8a2SJames Smart static void
__nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl * ctrl,bool start_queues)247495ced8a2SJames Smart __nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl *ctrl, bool start_queues)
247595ced8a2SJames Smart {
2476a7d13914SJames Smart int q;
2477a7d13914SJames Smart
2478a7d13914SJames Smart /*
2479a7d13914SJames Smart * if aborting io, the queues are no longer good, mark them
2480a7d13914SJames Smart * all as not live.
2481a7d13914SJames Smart */
2482a7d13914SJames Smart if (ctrl->ctrl.queue_count > 1) {
2483a7d13914SJames Smart for (q = 1; q < ctrl->ctrl.queue_count; q++)
2484a7d13914SJames Smart clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[q].flags);
2485a7d13914SJames Smart }
2486a7d13914SJames Smart clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
2487a7d13914SJames Smart
248895ced8a2SJames Smart /*
248995ced8a2SJames Smart * If io queues are present, stop them and terminate all outstanding
249095ced8a2SJames Smart * ios on them. As FC allocates FC exchange for each io, the
249195ced8a2SJames Smart * transport must contact the LLDD to terminate the exchange,
249295ced8a2SJames Smart * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
249395ced8a2SJames Smart * to tell us what io's are busy and invoke a transport routine
249495ced8a2SJames Smart * to kill them with the LLDD. After terminating the exchange
249595ced8a2SJames Smart * the LLDD will call the transport's normal io done path, but it
249695ced8a2SJames Smart * will have an aborted status. The done path will return the
249795ced8a2SJames Smart * io requests back to the block layer as part of normal completions
249895ced8a2SJames Smart * (but with error status).
249995ced8a2SJames Smart */
250095ced8a2SJames Smart if (ctrl->ctrl.queue_count > 1) {
25019f27bd70SChristoph Hellwig nvme_quiesce_io_queues(&ctrl->ctrl);
2502e5445daeSJames Smart nvme_sync_io_queues(&ctrl->ctrl);
250395ced8a2SJames Smart blk_mq_tagset_busy_iter(&ctrl->tag_set,
250495ced8a2SJames Smart nvme_fc_terminate_exchange, &ctrl->ctrl);
250595ced8a2SJames Smart blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
250695ced8a2SJames Smart if (start_queues)
25079f27bd70SChristoph Hellwig nvme_unquiesce_io_queues(&ctrl->ctrl);
250895ced8a2SJames Smart }
250995ced8a2SJames Smart
251095ced8a2SJames Smart /*
251195ced8a2SJames Smart * Other transports, which don't have link-level contexts bound
251295ced8a2SJames Smart * to sqe's, would try to gracefully shutdown the controller by
251395ced8a2SJames Smart * writing the registers for shutdown and polling (call
2514285b6e9bSChristoph Hellwig * nvme_disable_ctrl()). Given a bunch of i/o was potentially
251595ced8a2SJames Smart * just aborted and we will wait on those contexts, and given
251695ced8a2SJames Smart * there was no indication of how live the controlelr is on the
251795ced8a2SJames Smart * link, don't send more io to create more contexts for the
251895ced8a2SJames Smart * shutdown. Let the controller fail via keepalive failure if
251995ced8a2SJames Smart * its still present.
252095ced8a2SJames Smart */
252195ced8a2SJames Smart
252295ced8a2SJames Smart /*
252395ced8a2SJames Smart * clean up the admin queue. Same thing as above.
252495ced8a2SJames Smart */
25259f27bd70SChristoph Hellwig nvme_quiesce_admin_queue(&ctrl->ctrl);
2526e5445daeSJames Smart blk_sync_queue(ctrl->ctrl.admin_q);
252795ced8a2SJames Smart blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
252895ced8a2SJames Smart nvme_fc_terminate_exchange, &ctrl->ctrl);
252995ced8a2SJames Smart blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
25306fb271f1SMing Lei if (start_queues)
25319f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(&ctrl->ctrl);
253295ced8a2SJames Smart }
25339c2bb257SJames Smart
253461bff8efSJames Smart static void
nvme_fc_error_recovery(struct nvme_fc_ctrl * ctrl,char * errmsg)253561bff8efSJames Smart nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
253661bff8efSJames Smart {
25374cff280aSJames Smart /*
253895ced8a2SJames Smart * if an error (io timeout, etc) while (re)connecting, the remote
253995ced8a2SJames Smart * port requested terminating of the association (disconnect_ls)
254095ced8a2SJames Smart * or an error (timeout or abort) occurred on an io while creating
254195ced8a2SJames Smart * the controller. Abort any ios on the association and let the
254295ced8a2SJames Smart * create_association error path resolve things.
25434cff280aSJames Smart */
2544c62b9a2dSKeith Busch if (ctrl->ctrl.state == NVME_CTRL_CONNECTING) {
2545ee6fdc50SMichael Liang __nvme_fc_abort_outstanding_ios(ctrl, true);
2546c62b9a2dSKeith Busch set_bit(ASSOC_FAILED, &ctrl->flags);
254760e445bdSMichael Liang dev_warn(ctrl->ctrl.device,
254860e445bdSMichael Liang "NVME-FC{%d}: transport error during (re)connect\n",
254960e445bdSMichael Liang ctrl->cnum);
25504cff280aSJames Smart return;
25514cff280aSJames Smart }
25524cff280aSJames Smart
25534cff280aSJames Smart /* Otherwise, only proceed if in LIVE state - e.g. on first error */
2554c62b9a2dSKeith Busch if (ctrl->ctrl.state != NVME_CTRL_LIVE)
255569fa9646SJames Smart return;
255669fa9646SJames Smart
255761bff8efSJames Smart dev_warn(ctrl->ctrl.device,
2558514a6dc9SJames Smart "NVME-FC{%d}: transport association event: %s\n",
255961bff8efSJames Smart ctrl->cnum, errmsg);
2560589ff775SJames Smart dev_warn(ctrl->ctrl.device,
256161bff8efSJames Smart "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
256261bff8efSJames Smart
2563d86c4d8eSChristoph Hellwig nvme_reset_ctrl(&ctrl->ctrl);
256461bff8efSJames Smart }
256561bff8efSJames Smart
nvme_fc_timeout(struct request * rq)25669bdb4833SJohn Garry static enum blk_eh_timer_return nvme_fc_timeout(struct request *rq)
2567e399441dSJames Smart {
2568e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2569e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl;
257052793d62SJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
257152793d62SJames Smart struct nvme_command *sqe = &cmdiu->sqe;
2572e399441dSJames Smart
2573e399441dSJames Smart /*
257452793d62SJames Smart * Attempt to abort the offending command. Command completion
257552793d62SJames Smart * will detect the aborted io and will fail the connection.
2576e399441dSJames Smart */
257752793d62SJames Smart dev_info(ctrl->ctrl.device,
257852793d62SJames Smart "NVME-FC{%d.%d}: io timeout: opcode %d fctype %d w10/11: "
257952793d62SJames Smart "x%08x/x%08x\n",
258052793d62SJames Smart ctrl->cnum, op->queue->qnum, sqe->common.opcode,
258152793d62SJames Smart sqe->connect.fctype, sqe->common.cdw10, sqe->common.cdw11);
258252793d62SJames Smart if (__nvme_fc_abort_op(ctrl, op))
258352793d62SJames Smart nvme_fc_error_recovery(ctrl, "io timeout abort failed");
2584e399441dSJames Smart
2585134aedc9SJames Smart /*
2586134aedc9SJames Smart * the io abort has been initiated. Have the reset timer
2587134aedc9SJames Smart * restarted and the abort completion will complete the io
2588134aedc9SJames Smart * shortly. Avoids a synchronous wait while the abort finishes.
2589134aedc9SJames Smart */
2590134aedc9SJames Smart return BLK_EH_RESET_TIMER;
2591e399441dSJames Smart }
2592e399441dSJames Smart
2593e399441dSJames Smart static int
nvme_fc_map_data(struct nvme_fc_ctrl * ctrl,struct request * rq,struct nvme_fc_fcp_op * op)2594e399441dSJames Smart nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2595e399441dSJames Smart struct nvme_fc_fcp_op *op)
2596e399441dSJames Smart {
2597e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req;
2598e399441dSJames Smart int ret;
2599e399441dSJames Smart
2600e399441dSJames Smart freq->sg_cnt = 0;
2601e399441dSJames Smart
26029f7d8ae2SJames Smart if (!blk_rq_nr_phys_segments(rq))
2603e399441dSJames Smart return 0;
2604e399441dSJames Smart
2605e399441dSJames Smart freq->sg_table.sgl = freq->first_sgl;
260619e420bbSChristoph Hellwig ret = sg_alloc_table_chained(&freq->sg_table,
26074635873cSMing Lei blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
2608b1ae1a23SIsrael Rukshin NVME_INLINE_SG_CNT);
2609e399441dSJames Smart if (ret)
2610e399441dSJames Smart return -ENOMEM;
2611e399441dSJames Smart
2612e399441dSJames Smart op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
261319e420bbSChristoph Hellwig WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2614e399441dSJames Smart freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2615f15872c5SIsrael Rukshin op->nents, rq_dma_dir(rq));
2616e399441dSJames Smart if (unlikely(freq->sg_cnt <= 0)) {
2617b1ae1a23SIsrael Rukshin sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2618e399441dSJames Smart freq->sg_cnt = 0;
2619e399441dSJames Smart return -EFAULT;
2620e399441dSJames Smart }
2621e399441dSJames Smart
2622e399441dSJames Smart /*
2623e399441dSJames Smart * TODO: blk_integrity_rq(rq) for DIF
2624e399441dSJames Smart */
2625e399441dSJames Smart return 0;
2626e399441dSJames Smart }
2627e399441dSJames Smart
2628e399441dSJames Smart static void
nvme_fc_unmap_data(struct nvme_fc_ctrl * ctrl,struct request * rq,struct nvme_fc_fcp_op * op)2629e399441dSJames Smart nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2630e399441dSJames Smart struct nvme_fc_fcp_op *op)
2631e399441dSJames Smart {
2632e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req;
2633e399441dSJames Smart
2634e399441dSJames Smart if (!freq->sg_cnt)
2635e399441dSJames Smart return;
2636e399441dSJames Smart
2637e399441dSJames Smart fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2638f15872c5SIsrael Rukshin rq_dma_dir(rq));
2639e399441dSJames Smart
2640b1ae1a23SIsrael Rukshin sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2641e399441dSJames Smart
2642e399441dSJames Smart freq->sg_cnt = 0;
2643e399441dSJames Smart }
2644e399441dSJames Smart
2645e399441dSJames Smart /*
2646e399441dSJames Smart * In FC, the queue is a logical thing. At transport connect, the target
2647e399441dSJames Smart * creates its "queue" and returns a handle that is to be given to the
2648e399441dSJames Smart * target whenever it posts something to the corresponding SQ. When an
2649e399441dSJames Smart * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2650e399441dSJames Smart * command contained within the SQE, an io, and assigns a FC exchange
2651e399441dSJames Smart * to it. The SQE and the associated SQ handle are sent in the initial
2652e399441dSJames Smart * CMD IU sents on the exchange. All transfers relative to the io occur
2653e399441dSJames Smart * as part of the exchange. The CQE is the last thing for the io,
2654e399441dSJames Smart * which is transferred (explicitly or implicitly) with the RSP IU
2655e399441dSJames Smart * sent on the exchange. After the CQE is received, the FC exchange is
2656e399441dSJames Smart * terminaed and the Exchange may be used on a different io.
2657e399441dSJames Smart *
2658e399441dSJames Smart * The transport to LLDD api has the transport making a request for a
2659e399441dSJames Smart * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2660e399441dSJames Smart * resource and transfers the command. The LLDD will then process all
2661e399441dSJames Smart * steps to complete the io. Upon completion, the transport done routine
2662e399441dSJames Smart * is called.
2663e399441dSJames Smart *
2664e399441dSJames Smart * So - while the operation is outstanding to the LLDD, there is a link
2665e399441dSJames Smart * level FC exchange resource that is also outstanding. This must be
2666e399441dSJames Smart * considered in all cleanup operations.
2667e399441dSJames Smart */
2668fc17b653SChristoph Hellwig static blk_status_t
nvme_fc_start_fcp_op(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,struct nvme_fc_fcp_op * op,u32 data_len,enum nvmefc_fcp_datadir io_dir)2669e399441dSJames Smart nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2670e399441dSJames Smart struct nvme_fc_fcp_op *op, u32 data_len,
2671e399441dSJames Smart enum nvmefc_fcp_datadir io_dir)
2672e399441dSJames Smart {
2673e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2674e399441dSJames Smart struct nvme_command *sqe = &cmdiu->sqe;
2675b12740d3SJames Smart int ret, opstate;
2676e399441dSJames Smart
267761bff8efSJames Smart /*
267861bff8efSJames Smart * before attempting to send the io, check to see if we believe
267961bff8efSJames Smart * the target device is present
268061bff8efSJames Smart */
268161bff8efSJames Smart if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
268286ff7c2aSMing Lei return BLK_STS_RESOURCE;
268361bff8efSJames Smart
2684e399441dSJames Smart if (!nvme_fc_ctrl_get(ctrl))
2685fc17b653SChristoph Hellwig return BLK_STS_IOERR;
2686e399441dSJames Smart
2687e399441dSJames Smart /* format the FC-NVME CMD IU and fcp_req */
2688e399441dSJames Smart cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2689e399441dSJames Smart cmdiu->data_len = cpu_to_be32(data_len);
2690e399441dSJames Smart switch (io_dir) {
2691e399441dSJames Smart case NVMEFC_FCP_WRITE:
2692e399441dSJames Smart cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2693e399441dSJames Smart break;
2694e399441dSJames Smart case NVMEFC_FCP_READ:
2695e399441dSJames Smart cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2696e399441dSJames Smart break;
2697e399441dSJames Smart case NVMEFC_FCP_NODATA:
2698e399441dSJames Smart cmdiu->flags = 0;
2699e399441dSJames Smart break;
2700e399441dSJames Smart }
2701e399441dSJames Smart op->fcp_req.payload_length = data_len;
2702e399441dSJames Smart op->fcp_req.io_dir = io_dir;
2703e399441dSJames Smart op->fcp_req.transferred_length = 0;
2704e399441dSJames Smart op->fcp_req.rcv_rsplen = 0;
270562eeacb0SJames Smart op->fcp_req.status = NVME_SC_SUCCESS;
2706e399441dSJames Smart op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2707e399441dSJames Smart
2708e399441dSJames Smart /*
2709e399441dSJames Smart * validate per fabric rules, set fields mandated by fabric spec
2710e399441dSJames Smart * as well as those by FC-NVME spec.
2711e399441dSJames Smart */
2712e399441dSJames Smart WARN_ON_ONCE(sqe->common.metadata);
2713e399441dSJames Smart sqe->common.flags |= NVME_CMD_SGL_METABUF;
2714e399441dSJames Smart
2715e399441dSJames Smart /*
2716d9d34c0bSJames Smart * format SQE DPTR field per FC-NVME rules:
2717d9d34c0bSJames Smart * type=0x5 Transport SGL Data Block Descriptor
2718d9d34c0bSJames Smart * subtype=0xA Transport-specific value
2719d9d34c0bSJames Smart * address=0
2720d9d34c0bSJames Smart * length=length of the data series
2721e399441dSJames Smart */
2722d9d34c0bSJames Smart sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2723d9d34c0bSJames Smart NVME_SGL_FMT_TRANSPORT_A;
2724e399441dSJames Smart sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2725e399441dSJames Smart sqe->rw.dptr.sgl.addr = 0;
2726e399441dSJames Smart
272778a7ac26SJames Smart if (!(op->flags & FCOP_FLAGS_AEN)) {
2728e399441dSJames Smart ret = nvme_fc_map_data(ctrl, op->rq, op);
2729e399441dSJames Smart if (ret < 0) {
2730e399441dSJames Smart nvme_cleanup_cmd(op->rq);
2731e399441dSJames Smart nvme_fc_ctrl_put(ctrl);
2732fc17b653SChristoph Hellwig if (ret == -ENOMEM || ret == -EAGAIN)
2733fc17b653SChristoph Hellwig return BLK_STS_RESOURCE;
2734fc17b653SChristoph Hellwig return BLK_STS_IOERR;
2735e399441dSJames Smart }
2736e399441dSJames Smart }
2737e399441dSJames Smart
2738e399441dSJames Smart fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2739e399441dSJames Smart sizeof(op->cmd_iu), DMA_TO_DEVICE);
2740e399441dSJames Smart
2741e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2742e399441dSJames Smart
274378a7ac26SJames Smart if (!(op->flags & FCOP_FLAGS_AEN))
27446887fc64SSagi Grimberg nvme_start_request(op->rq);
2745e399441dSJames Smart
274667f471b6SJames Smart cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2747e399441dSJames Smart ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2748e399441dSJames Smart &ctrl->rport->remoteport,
2749e399441dSJames Smart queue->lldd_handle, &op->fcp_req);
2750e399441dSJames Smart
2751e399441dSJames Smart if (ret) {
275267f471b6SJames Smart /*
275367f471b6SJames Smart * If the lld fails to send the command is there an issue with
275467f471b6SJames Smart * the csn value? If the command that fails is the Connect,
275567f471b6SJames Smart * no - as the connection won't be live. If it is a command
275667f471b6SJames Smart * post-connect, it's possible a gap in csn may be created.
275767f471b6SJames Smart * Does this matter? As Linux initiators don't send fused
275867f471b6SJames Smart * commands, no. The gap would exist, but as there's nothing
275967f471b6SJames Smart * that depends on csn order to be delivered on the target
276067f471b6SJames Smart * side, it shouldn't hurt. It would be difficult for a
276167f471b6SJames Smart * target to even detect the csn gap as it has no idea when the
276267f471b6SJames Smart * cmd with the csn was supposed to arrive.
276367f471b6SJames Smart */
2764b12740d3SJames Smart opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2765b12740d3SJames Smart __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2766b12740d3SJames Smart
2767c9c12e51SDaniel Wagner if (!(op->flags & FCOP_FLAGS_AEN)) {
2768e399441dSJames Smart nvme_fc_unmap_data(ctrl, op->rq, op);
276916686f3aSMax Gurtovoy nvme_cleanup_cmd(op->rq);
2770c9c12e51SDaniel Wagner }
2771c9c12e51SDaniel Wagner
2772e399441dSJames Smart nvme_fc_ctrl_put(ctrl);
2773e399441dSJames Smart
27748b25f351SJames Smart if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
27758b25f351SJames Smart ret != -EBUSY)
2776fc17b653SChristoph Hellwig return BLK_STS_IOERR;
2777e399441dSJames Smart
277886ff7c2aSMing Lei return BLK_STS_RESOURCE;
2779e399441dSJames Smart }
2780e399441dSJames Smart
2781fc17b653SChristoph Hellwig return BLK_STS_OK;
2782e399441dSJames Smart }
2783e399441dSJames Smart
2784fc17b653SChristoph Hellwig static blk_status_t
nvme_fc_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)2785e399441dSJames Smart nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2786e399441dSJames Smart const struct blk_mq_queue_data *bd)
2787e399441dSJames Smart {
2788e399441dSJames Smart struct nvme_ns *ns = hctx->queue->queuedata;
2789e399441dSJames Smart struct nvme_fc_queue *queue = hctx->driver_data;
2790e399441dSJames Smart struct nvme_fc_ctrl *ctrl = queue->ctrl;
2791e399441dSJames Smart struct request *rq = bd->rq;
2792e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2793e399441dSJames Smart enum nvmefc_fcp_datadir io_dir;
27943bc32bb1SChristoph Hellwig bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2795e399441dSJames Smart u32 data_len;
2796fc17b653SChristoph Hellwig blk_status_t ret;
2797e399441dSJames Smart
27983bc32bb1SChristoph Hellwig if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
2799a9715744STao Chiu !nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2800a9715744STao Chiu return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
28019e0ed16aSSagi Grimberg
2802f4b9e6c9SKeith Busch ret = nvme_setup_cmd(ns, rq);
2803e399441dSJames Smart if (ret)
2804e399441dSJames Smart return ret;
2805e399441dSJames Smart
28069f7d8ae2SJames Smart /*
28079f7d8ae2SJames Smart * nvme core doesn't quite treat the rq opaquely. Commands such
28089f7d8ae2SJames Smart * as WRITE ZEROES will return a non-zero rq payload_bytes yet
28099f7d8ae2SJames Smart * there is no actual payload to be transferred.
28109f7d8ae2SJames Smart * To get it right, key data transmission on there being 1 or
28119f7d8ae2SJames Smart * more physical segments in the sg list. If there is no
28129f7d8ae2SJames Smart * physical segments, there is no payload.
28139f7d8ae2SJames Smart */
28149f7d8ae2SJames Smart if (blk_rq_nr_phys_segments(rq)) {
2815b131c61dSChristoph Hellwig data_len = blk_rq_payload_bytes(rq);
2816e399441dSJames Smart io_dir = ((rq_data_dir(rq) == WRITE) ?
2817e399441dSJames Smart NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
28189f7d8ae2SJames Smart } else {
28199f7d8ae2SJames Smart data_len = 0;
2820e399441dSJames Smart io_dir = NVMEFC_FCP_NODATA;
28219f7d8ae2SJames Smart }
28229f7d8ae2SJames Smart
2823e399441dSJames Smart
2824e399441dSJames Smart return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2825e399441dSJames Smart }
2826e399441dSJames Smart
2827e399441dSJames Smart static void
nvme_fc_submit_async_event(struct nvme_ctrl * arg)2828ad22c355SKeith Busch nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2829e399441dSJames Smart {
2830e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2831e399441dSJames Smart struct nvme_fc_fcp_op *aen_op;
2832fc17b653SChristoph Hellwig blk_status_t ret;
2833e399441dSJames Smart
2834eb4ee8f1SJames Smart if (test_bit(FCCTRL_TERMIO, &ctrl->flags))
283561bff8efSJames Smart return;
283661bff8efSJames Smart
2837ad22c355SKeith Busch aen_op = &ctrl->aen_ops[0];
2838e399441dSJames Smart
2839e399441dSJames Smart ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2840e399441dSJames Smart NVMEFC_FCP_NODATA);
2841e399441dSJames Smart if (ret)
2842e399441dSJames Smart dev_err(ctrl->ctrl.device,
2843ad22c355SKeith Busch "failed async event work\n");
2844e399441dSJames Smart }
2845e399441dSJames Smart
2846e399441dSJames Smart static void
nvme_fc_complete_rq(struct request * rq)2847c3aedd22SJames Smart nvme_fc_complete_rq(struct request *rq)
2848e399441dSJames Smart {
2849e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2850e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl;
2851e399441dSJames Smart
285278a7ac26SJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE);
285352793d62SJames Smart op->flags &= ~FCOP_FLAGS_TERMIO;
2854e399441dSJames Smart
2855e399441dSJames Smart nvme_fc_unmap_data(ctrl, rq, op);
285677f02a7aSChristoph Hellwig nvme_complete_rq(rq);
2857e399441dSJames Smart nvme_fc_ctrl_put(ctrl);
285878a7ac26SJames Smart }
285978a7ac26SJames Smart
nvme_fc_map_queues(struct blk_mq_tag_set * set)2860a4e1d0b7SBart Van Assche static void nvme_fc_map_queues(struct blk_mq_tag_set *set)
286101d83816SSaurav Kashyap {
28621864ea46SChristoph Hellwig struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
286301d83816SSaurav Kashyap int i;
286401d83816SSaurav Kashyap
286501d83816SSaurav Kashyap for (i = 0; i < set->nr_maps; i++) {
286601d83816SSaurav Kashyap struct blk_mq_queue_map *map = &set->map[i];
286701d83816SSaurav Kashyap
286801d83816SSaurav Kashyap if (!map->nr_queues) {
286901d83816SSaurav Kashyap WARN_ON(i == HCTX_TYPE_DEFAULT);
287001d83816SSaurav Kashyap continue;
287101d83816SSaurav Kashyap }
287201d83816SSaurav Kashyap
287301d83816SSaurav Kashyap /* Call LLDD map queue functionality if defined */
287401d83816SSaurav Kashyap if (ctrl->lport->ops->map_queues)
287501d83816SSaurav Kashyap ctrl->lport->ops->map_queues(&ctrl->lport->localport,
287601d83816SSaurav Kashyap map);
287701d83816SSaurav Kashyap else
287801d83816SSaurav Kashyap blk_mq_map_queues(map);
287901d83816SSaurav Kashyap }
288001d83816SSaurav Kashyap }
2881e399441dSJames Smart
288261bff8efSJames Smart static const struct blk_mq_ops nvme_fc_mq_ops = {
288361bff8efSJames Smart .queue_rq = nvme_fc_queue_rq,
288461bff8efSJames Smart .complete = nvme_fc_complete_rq,
288561bff8efSJames Smart .init_request = nvme_fc_init_request,
288661bff8efSJames Smart .exit_request = nvme_fc_exit_request,
288761bff8efSJames Smart .init_hctx = nvme_fc_init_hctx,
288861bff8efSJames Smart .timeout = nvme_fc_timeout,
288901d83816SSaurav Kashyap .map_queues = nvme_fc_map_queues,
2890e399441dSJames Smart };
2891e399441dSJames Smart
2892e399441dSJames Smart static int
nvme_fc_create_io_queues(struct nvme_fc_ctrl * ctrl)2893e399441dSJames Smart nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2894e399441dSJames Smart {
2895e399441dSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
28967314183dSSagi Grimberg unsigned int nr_io_queues;
2897e399441dSJames Smart int ret;
2898e399441dSJames Smart
28997314183dSSagi Grimberg nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
29007314183dSSagi Grimberg ctrl->lport->ops->max_hw_queues);
29017314183dSSagi Grimberg ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2902e399441dSJames Smart if (ret) {
2903e399441dSJames Smart dev_info(ctrl->ctrl.device,
2904e399441dSJames Smart "set_queue_count failed: %d\n", ret);
2905e399441dSJames Smart return ret;
2906e399441dSJames Smart }
2907e399441dSJames Smart
29087314183dSSagi Grimberg ctrl->ctrl.queue_count = nr_io_queues + 1;
29097314183dSSagi Grimberg if (!nr_io_queues)
2910e399441dSJames Smart return 0;
2911e399441dSJames Smart
2912e399441dSJames Smart nvme_fc_init_io_queues(ctrl);
2913e399441dSJames Smart
29146dfba1c0SChristoph Hellwig ret = nvme_alloc_io_tag_set(&ctrl->ctrl, &ctrl->tag_set,
2915db45e1a5SChristoph Hellwig &nvme_fc_mq_ops, 1,
2916d67790ddSKees Cook struct_size_t(struct nvme_fcp_op_w_sgl, priv,
29176dfba1c0SChristoph Hellwig ctrl->lport->ops->fcprqst_priv_sz));
2918e399441dSJames Smart if (ret)
2919e399441dSJames Smart return ret;
2920e399441dSJames Smart
2921d157e534SJames Smart ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2922e399441dSJames Smart if (ret)
29236dfba1c0SChristoph Hellwig goto out_cleanup_tagset;
2924e399441dSJames Smart
2925d157e534SJames Smart ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2926e399441dSJames Smart if (ret)
2927e399441dSJames Smart goto out_delete_hw_queues;
2928e399441dSJames Smart
29294c984154SJames Smart ctrl->ioq_live = true;
29304c984154SJames Smart
2931e399441dSJames Smart return 0;
2932e399441dSJames Smart
2933e399441dSJames Smart out_delete_hw_queues:
2934e399441dSJames Smart nvme_fc_delete_hw_io_queues(ctrl);
29356dfba1c0SChristoph Hellwig out_cleanup_tagset:
29366dfba1c0SChristoph Hellwig nvme_remove_io_tag_set(&ctrl->ctrl);
2937e399441dSJames Smart nvme_fc_free_io_queues(ctrl);
2938e399441dSJames Smart
2939e399441dSJames Smart /* force put free routine to ignore io queues */
2940e399441dSJames Smart ctrl->ctrl.tagset = NULL;
2941e399441dSJames Smart
2942e399441dSJames Smart return ret;
2943e399441dSJames Smart }
2944e399441dSJames Smart
294561bff8efSJames Smart static int
nvme_fc_recreate_io_queues(struct nvme_fc_ctrl * ctrl)29463e493c00SJames Smart nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
294761bff8efSJames Smart {
294861bff8efSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2949834d3710SJames Smart u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
29507314183dSSagi Grimberg unsigned int nr_io_queues;
295161bff8efSJames Smart int ret;
295261bff8efSJames Smart
29537314183dSSagi Grimberg nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
29547314183dSSagi Grimberg ctrl->lport->ops->max_hw_queues);
29557314183dSSagi Grimberg ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
295661bff8efSJames Smart if (ret) {
295761bff8efSJames Smart dev_info(ctrl->ctrl.device,
295861bff8efSJames Smart "set_queue_count failed: %d\n", ret);
295961bff8efSJames Smart return ret;
296061bff8efSJames Smart }
296161bff8efSJames Smart
2962834d3710SJames Smart if (!nr_io_queues && prior_ioq_cnt) {
2963834d3710SJames Smart dev_info(ctrl->ctrl.device,
2964834d3710SJames Smart "Fail Reconnect: At least 1 io queue "
2965834d3710SJames Smart "required (was %d)\n", prior_ioq_cnt);
2966834d3710SJames Smart return -ENOSPC;
2967834d3710SJames Smart }
2968834d3710SJames Smart
29697314183dSSagi Grimberg ctrl->ctrl.queue_count = nr_io_queues + 1;
297061bff8efSJames Smart /* check for io queues existing */
2971d858e5f0SSagi Grimberg if (ctrl->ctrl.queue_count == 1)
297261bff8efSJames Smart return 0;
297361bff8efSJames Smart
297488e837edSJames Smart if (prior_ioq_cnt != nr_io_queues) {
2975834d3710SJames Smart dev_info(ctrl->ctrl.device,
2976834d3710SJames Smart "reconnect: revising io queue count from %d to %d\n",
2977834d3710SJames Smart prior_ioq_cnt, nr_io_queues);
2978cda5fd1aSSagi Grimberg blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
297988e837edSJames Smart }
2980cda5fd1aSSagi Grimberg
2981555f66d0SDaniel Wagner ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2982555f66d0SDaniel Wagner if (ret)
2983555f66d0SDaniel Wagner goto out_free_io_queues;
2984555f66d0SDaniel Wagner
2985555f66d0SDaniel Wagner ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2986555f66d0SDaniel Wagner if (ret)
2987555f66d0SDaniel Wagner goto out_delete_hw_queues;
2988555f66d0SDaniel Wagner
298961bff8efSJames Smart return 0;
299061bff8efSJames Smart
299161bff8efSJames Smart out_delete_hw_queues:
299261bff8efSJames Smart nvme_fc_delete_hw_io_queues(ctrl);
299361bff8efSJames Smart out_free_io_queues:
299461bff8efSJames Smart nvme_fc_free_io_queues(ctrl);
299561bff8efSJames Smart return ret;
299661bff8efSJames Smart }
299761bff8efSJames Smart
2998158bfb88SJames Smart static void
nvme_fc_rport_active_on_lport(struct nvme_fc_rport * rport)2999158bfb88SJames Smart nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
3000158bfb88SJames Smart {
3001158bfb88SJames Smart struct nvme_fc_lport *lport = rport->lport;
3002158bfb88SJames Smart
3003158bfb88SJames Smart atomic_inc(&lport->act_rport_cnt);
3004158bfb88SJames Smart }
3005158bfb88SJames Smart
3006158bfb88SJames Smart static void
nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport * rport)3007158bfb88SJames Smart nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
3008158bfb88SJames Smart {
3009158bfb88SJames Smart struct nvme_fc_lport *lport = rport->lport;
3010158bfb88SJames Smart u32 cnt;
3011158bfb88SJames Smart
3012158bfb88SJames Smart cnt = atomic_dec_return(&lport->act_rport_cnt);
3013158bfb88SJames Smart if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
3014158bfb88SJames Smart lport->ops->localport_delete(&lport->localport);
3015158bfb88SJames Smart }
3016158bfb88SJames Smart
3017158bfb88SJames Smart static int
nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl * ctrl)3018158bfb88SJames Smart nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
3019158bfb88SJames Smart {
3020158bfb88SJames Smart struct nvme_fc_rport *rport = ctrl->rport;
3021158bfb88SJames Smart u32 cnt;
3022158bfb88SJames Smart
3023eb4ee8f1SJames Smart if (test_and_set_bit(ASSOC_ACTIVE, &ctrl->flags))
3024158bfb88SJames Smart return 1;
3025158bfb88SJames Smart
3026158bfb88SJames Smart cnt = atomic_inc_return(&rport->act_ctrl_cnt);
3027158bfb88SJames Smart if (cnt == 1)
3028158bfb88SJames Smart nvme_fc_rport_active_on_lport(rport);
3029158bfb88SJames Smart
3030158bfb88SJames Smart return 0;
3031158bfb88SJames Smart }
3032158bfb88SJames Smart
3033158bfb88SJames Smart static int
nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl * ctrl)3034158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
3035158bfb88SJames Smart {
3036158bfb88SJames Smart struct nvme_fc_rport *rport = ctrl->rport;
3037158bfb88SJames Smart struct nvme_fc_lport *lport = rport->lport;
3038158bfb88SJames Smart u32 cnt;
3039158bfb88SJames Smart
3040eb4ee8f1SJames Smart /* clearing of ctrl->flags ASSOC_ACTIVE bit is in association delete */
3041158bfb88SJames Smart
3042158bfb88SJames Smart cnt = atomic_dec_return(&rport->act_ctrl_cnt);
3043158bfb88SJames Smart if (cnt == 0) {
3044158bfb88SJames Smart if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
3045158bfb88SJames Smart lport->ops->remoteport_delete(&rport->remoteport);
3046158bfb88SJames Smart nvme_fc_rport_inactive_on_lport(rport);
3047158bfb88SJames Smart }
3048158bfb88SJames Smart
3049158bfb88SJames Smart return 0;
3050158bfb88SJames Smart }
3051158bfb88SJames Smart
305261bff8efSJames Smart /*
305361bff8efSJames Smart * This routine restarts the controller on the host side, and
305461bff8efSJames Smart * on the link side, recreates the controller association.
305561bff8efSJames Smart */
305661bff8efSJames Smart static int
nvme_fc_create_association(struct nvme_fc_ctrl * ctrl)305761bff8efSJames Smart nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
305861bff8efSJames Smart {
305961bff8efSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
306014fd1e98SJames Smart struct nvmefc_ls_rcv_op *disls = NULL;
306114fd1e98SJames Smart unsigned long flags;
306261bff8efSJames Smart int ret;
306361bff8efSJames Smart bool changed;
306461bff8efSJames Smart
3065fdf9dfa8SSagi Grimberg ++ctrl->ctrl.nr_reconnects;
306661bff8efSJames Smart
306796e24801SJames Smart if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
306896e24801SJames Smart return -ENODEV;
306996e24801SJames Smart
3070158bfb88SJames Smart if (nvme_fc_ctlr_active_on_rport(ctrl))
3071158bfb88SJames Smart return -ENOTUNIQ;
3072158bfb88SJames Smart
30734bea364fSJames Smart dev_info(ctrl->ctrl.device,
30744bea364fSJames Smart "NVME-FC{%d}: create association : host wwpn 0x%016llx "
30754bea364fSJames Smart " rport wwpn 0x%016llx: NQN \"%s\"\n",
30764bea364fSJames Smart ctrl->cnum, ctrl->lport->localport.port_name,
30774bea364fSJames Smart ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
30784bea364fSJames Smart
3079caf1cbe3SJames Smart clear_bit(ASSOC_FAILED, &ctrl->flags);
3080caf1cbe3SJames Smart
308161bff8efSJames Smart /*
308261bff8efSJames Smart * Create the admin queue
308361bff8efSJames Smart */
308461bff8efSJames Smart
308561bff8efSJames Smart ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
3086d157e534SJames Smart NVME_AQ_DEPTH);
308761bff8efSJames Smart if (ret)
308861bff8efSJames Smart goto out_free_queue;
308961bff8efSJames Smart
309061bff8efSJames Smart ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
3091d157e534SJames Smart NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
309261bff8efSJames Smart if (ret)
309361bff8efSJames Smart goto out_delete_hw_queue;
309461bff8efSJames Smart
309561bff8efSJames Smart ret = nvmf_connect_admin_queue(&ctrl->ctrl);
309661bff8efSJames Smart if (ret)
309761bff8efSJames Smart goto out_disconnect_admin_queue;
309861bff8efSJames Smart
30999e0ed16aSSagi Grimberg set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
31009e0ed16aSSagi Grimberg
310161bff8efSJames Smart /*
310261bff8efSJames Smart * Check controller capabilities
310361bff8efSJames Smart *
310461bff8efSJames Smart * todo:- add code to check if ctrl attributes changed from
310561bff8efSJames Smart * prior connection values
310661bff8efSJames Smart */
310761bff8efSJames Smart
3108c0f2f45bSSagi Grimberg ret = nvme_enable_ctrl(&ctrl->ctrl);
310960e445bdSMichael Liang if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
311060e445bdSMichael Liang ret = -EIO;
311160e445bdSMichael Liang if (ret)
311261bff8efSJames Smart goto out_disconnect_admin_queue;
311361bff8efSJames Smart
311423748076SJames Smart ctrl->ctrl.max_segments = ctrl->lport->ops->max_sgl_segments;
311523748076SJames Smart ctrl->ctrl.max_hw_sectors = ctrl->ctrl.max_segments <<
311623748076SJames Smart (ilog2(SZ_4K) - 9);
311761bff8efSJames Smart
31189f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(&ctrl->ctrl);
3119e7832cb4SSagi Grimberg
312094cc781fSChristoph Hellwig ret = nvme_init_ctrl_finish(&ctrl->ctrl, false);
312160e445bdSMichael Liang if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
312260e445bdSMichael Liang ret = -EIO;
312360e445bdSMichael Liang if (ret)
312461bff8efSJames Smart goto out_disconnect_admin_queue;
312561bff8efSJames Smart
312661bff8efSJames Smart /* sanity checks */
312761bff8efSJames Smart
312861bff8efSJames Smart /* FC-NVME does not have other data in the capsule */
312961bff8efSJames Smart if (ctrl->ctrl.icdoff) {
313061bff8efSJames Smart dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
313161bff8efSJames Smart ctrl->ctrl.icdoff);
3132f25f8ef7SHannes Reinecke ret = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
313361bff8efSJames Smart goto out_disconnect_admin_queue;
313461bff8efSJames Smart }
313561bff8efSJames Smart
313661bff8efSJames Smart /* FC-NVME supports normal SGL Data Block Descriptors */
3137b61678bcSChaitanya Kulkarni if (!nvme_ctrl_sgl_supported(&ctrl->ctrl)) {
31388df1bff5SMax Gurtovoy dev_err(ctrl->ctrl.device,
31398df1bff5SMax Gurtovoy "Mandatory sgls are not supported!\n");
3140f25f8ef7SHannes Reinecke ret = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
31418df1bff5SMax Gurtovoy goto out_disconnect_admin_queue;
31428df1bff5SMax Gurtovoy }
314361bff8efSJames Smart
314461bff8efSJames Smart if (opts->queue_size > ctrl->ctrl.maxcmd) {
314561bff8efSJames Smart /* warn if maxcmd is lower than queue_size */
314661bff8efSJames Smart dev_warn(ctrl->ctrl.device,
314761bff8efSJames Smart "queue_size %zu > ctrl maxcmd %u, reducing "
31487db39484SJames Smart "to maxcmd\n",
314961bff8efSJames Smart opts->queue_size, ctrl->ctrl.maxcmd);
315061bff8efSJames Smart opts->queue_size = ctrl->ctrl.maxcmd;
315118ecd975SChristoph Hellwig ctrl->ctrl.sqsize = opts->queue_size - 1;
3152d157e534SJames Smart }
3153d157e534SJames Smart
315461bff8efSJames Smart ret = nvme_fc_init_aen_ops(ctrl);
315561bff8efSJames Smart if (ret)
315661bff8efSJames Smart goto out_term_aen_ops;
315761bff8efSJames Smart
315861bff8efSJames Smart /*
315961bff8efSJames Smart * Create the io queues
316061bff8efSJames Smart */
316161bff8efSJames Smart
3162d858e5f0SSagi Grimberg if (ctrl->ctrl.queue_count > 1) {
31634c984154SJames Smart if (!ctrl->ioq_live)
316461bff8efSJames Smart ret = nvme_fc_create_io_queues(ctrl);
316561bff8efSJames Smart else
31663e493c00SJames Smart ret = nvme_fc_recreate_io_queues(ctrl);
316761bff8efSJames Smart }
316860e445bdSMichael Liang if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
316960e445bdSMichael Liang ret = -EIO;
3170c62b9a2dSKeith Busch if (ret)
3171caf1cbe3SJames Smart goto out_term_aen_ops;
3172c62b9a2dSKeith Busch
317361bff8efSJames Smart changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
317461bff8efSJames Smart
3175fdf9dfa8SSagi Grimberg ctrl->ctrl.nr_reconnects = 0;
317661bff8efSJames Smart
317744c6ec77SJames Smart if (changed)
3178d09f2b45SSagi Grimberg nvme_start_ctrl(&ctrl->ctrl);
317961bff8efSJames Smart
318061bff8efSJames Smart return 0; /* Success */
318161bff8efSJames Smart
318261bff8efSJames Smart out_term_aen_ops:
318361bff8efSJames Smart nvme_fc_term_aen_ops(ctrl);
318461bff8efSJames Smart out_disconnect_admin_queue:
318560e445bdSMichael Liang dev_warn(ctrl->ctrl.device,
318660e445bdSMichael Liang "NVME-FC{%d}: create_assoc failed, assoc_id %llx ret %d\n",
318760e445bdSMichael Liang ctrl->cnum, ctrl->association_id, ret);
318861bff8efSJames Smart /* send a Disconnect(association) LS to fc-nvme target */
318961bff8efSJames Smart nvme_fc_xmt_disconnect_assoc(ctrl);
319014fd1e98SJames Smart spin_lock_irqsave(&ctrl->lock, flags);
3191bcde5f0fSJames Smart ctrl->association_id = 0;
319214fd1e98SJames Smart disls = ctrl->rcv_disconn;
319314fd1e98SJames Smart ctrl->rcv_disconn = NULL;
319414fd1e98SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags);
319514fd1e98SJames Smart if (disls)
319614fd1e98SJames Smart nvme_fc_xmt_ls_rsp(disls);
319761bff8efSJames Smart out_delete_hw_queue:
319861bff8efSJames Smart __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
319961bff8efSJames Smart out_free_queue:
320061bff8efSJames Smart nvme_fc_free_queue(&ctrl->queues[0]);
3201eb4ee8f1SJames Smart clear_bit(ASSOC_ACTIVE, &ctrl->flags);
3202158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(ctrl);
320361bff8efSJames Smart
320461bff8efSJames Smart return ret;
320561bff8efSJames Smart }
320661bff8efSJames Smart
320752793d62SJames Smart
320852793d62SJames Smart /*
320961bff8efSJames Smart * This routine stops operation of the controller on the host side.
321061bff8efSJames Smart * On the host os stack side: Admin and IO queues are stopped,
321161bff8efSJames Smart * outstanding ios on them terminated via FC ABTS.
321261bff8efSJames Smart * On the link side: the association is terminated.
321361bff8efSJames Smart */
321461bff8efSJames Smart static void
nvme_fc_delete_association(struct nvme_fc_ctrl * ctrl)321561bff8efSJames Smart nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
321661bff8efSJames Smart {
321714fd1e98SJames Smart struct nvmefc_ls_rcv_op *disls = NULL;
321861bff8efSJames Smart unsigned long flags;
321961bff8efSJames Smart
3220eb4ee8f1SJames Smart if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
3221158bfb88SJames Smart return;
3222158bfb88SJames Smart
322361bff8efSJames Smart spin_lock_irqsave(&ctrl->lock, flags);
3224eb4ee8f1SJames Smart set_bit(FCCTRL_TERMIO, &ctrl->flags);
322561bff8efSJames Smart ctrl->iocnt = 0;
322661bff8efSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags);
322761bff8efSJames Smart
322852793d62SJames Smart __nvme_fc_abort_outstanding_ios(ctrl, false);
322961bff8efSJames Smart
323061bff8efSJames Smart /* kill the aens as they are a separate path */
323161bff8efSJames Smart nvme_fc_abort_aen_ops(ctrl);
323261bff8efSJames Smart
323361bff8efSJames Smart /* wait for all io that had to be aborted */
32348a82dbf1SJames Smart spin_lock_irq(&ctrl->lock);
323536715cf4SJames Smart wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
3236eb4ee8f1SJames Smart clear_bit(FCCTRL_TERMIO, &ctrl->flags);
32378a82dbf1SJames Smart spin_unlock_irq(&ctrl->lock);
323861bff8efSJames Smart
323961bff8efSJames Smart nvme_fc_term_aen_ops(ctrl);
324061bff8efSJames Smart
324161bff8efSJames Smart /*
324261bff8efSJames Smart * send a Disconnect(association) LS to fc-nvme target
324361bff8efSJames Smart * Note: could have been sent at top of process, but
324461bff8efSJames Smart * cleaner on link traffic if after the aborts complete.
324561bff8efSJames Smart * Note: if association doesn't exist, association_id will be 0
324661bff8efSJames Smart */
324761bff8efSJames Smart if (ctrl->association_id)
324861bff8efSJames Smart nvme_fc_xmt_disconnect_assoc(ctrl);
324961bff8efSJames Smart
325014fd1e98SJames Smart spin_lock_irqsave(&ctrl->lock, flags);
3251bcde5f0fSJames Smart ctrl->association_id = 0;
325214fd1e98SJames Smart disls = ctrl->rcv_disconn;
325314fd1e98SJames Smart ctrl->rcv_disconn = NULL;
325414fd1e98SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags);
325514fd1e98SJames Smart if (disls)
325614fd1e98SJames Smart /*
325714fd1e98SJames Smart * if a Disconnect Request was waiting for a response, send
325814fd1e98SJames Smart * now that all ABTS's have been issued (and are complete).
325914fd1e98SJames Smart */
326014fd1e98SJames Smart nvme_fc_xmt_ls_rsp(disls);
3261bcde5f0fSJames Smart
326261bff8efSJames Smart if (ctrl->ctrl.tagset) {
326361bff8efSJames Smart nvme_fc_delete_hw_io_queues(ctrl);
326461bff8efSJames Smart nvme_fc_free_io_queues(ctrl);
326561bff8efSJames Smart }
326661bff8efSJames Smart
326761bff8efSJames Smart __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
326861bff8efSJames Smart nvme_fc_free_queue(&ctrl->queues[0]);
3269158bfb88SJames Smart
3270d625d05eSJames Smart /* re-enable the admin_q so anything new can fast fail */
32719f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(&ctrl->ctrl);
3272d625d05eSJames Smart
327302d62a8bSJames Smart /* resume the io queues so that things will fast fail */
32749f27bd70SChristoph Hellwig nvme_unquiesce_io_queues(&ctrl->ctrl);
327502d62a8bSJames Smart
3276158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(ctrl);
327761bff8efSJames Smart }
327861bff8efSJames Smart
327961bff8efSJames Smart static void
nvme_fc_delete_ctrl(struct nvme_ctrl * nctrl)3280c5017e85SChristoph Hellwig nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
328161bff8efSJames Smart {
3282c5017e85SChristoph Hellwig struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
328361bff8efSJames Smart
328419fce047SJames Smart cancel_work_sync(&ctrl->ioerr_work);
328561bff8efSJames Smart cancel_delayed_work_sync(&ctrl->connect_work);
328661bff8efSJames Smart /*
328761bff8efSJames Smart * kill the association on the link side. this will block
328861bff8efSJames Smart * waiting for io to terminate
328961bff8efSJames Smart */
329061bff8efSJames Smart nvme_fc_delete_association(ctrl);
329161bff8efSJames Smart }
329261bff8efSJames Smart
329361bff8efSJames Smart static void
nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl * ctrl,int status)32945bbecdbcSJames Smart nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
32955bbecdbcSJames Smart {
32962b632970SJames Smart struct nvme_fc_rport *rport = ctrl->rport;
32972b632970SJames Smart struct nvme_fc_remote_port *portptr = &rport->remoteport;
32982b632970SJames Smart unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
32992b632970SJames Smart bool recon = true;
33005bbecdbcSJames Smart
33018884a56dSKeith Busch if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_CONNECTING)
33025bbecdbcSJames Smart return;
33035bbecdbcSJames Smart
3304f25f8ef7SHannes Reinecke if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3305589ff775SJames Smart dev_info(ctrl->ctrl.device,
33065bbecdbcSJames Smart "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
33075bbecdbcSJames Smart ctrl->cnum, status);
3308f25f8ef7SHannes Reinecke if (status > 0 && (status & NVME_SC_DNR))
3309f25f8ef7SHannes Reinecke recon = false;
3310f25f8ef7SHannes Reinecke } else if (time_after_eq(jiffies, rport->dev_loss_end))
33112b632970SJames Smart recon = false;
33125bbecdbcSJames Smart
33132b632970SJames Smart if (recon && nvmf_should_reconnect(&ctrl->ctrl)) {
33142b632970SJames Smart if (portptr->port_state == FC_OBJSTATE_ONLINE)
33155bbecdbcSJames Smart dev_info(ctrl->ctrl.device,
33162b632970SJames Smart "NVME-FC{%d}: Reconnect attempt in %ld "
33172b632970SJames Smart "seconds\n",
33182b632970SJames Smart ctrl->cnum, recon_delay / HZ);
33192b632970SJames Smart else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
33202b632970SJames Smart recon_delay = rport->dev_loss_end - jiffies;
33212b632970SJames Smart
33222b632970SJames Smart queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
33235bbecdbcSJames Smart } else {
3324f25f8ef7SHannes Reinecke if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3325f25f8ef7SHannes Reinecke if (status > 0 && (status & NVME_SC_DNR))
3326589ff775SJames Smart dev_warn(ctrl->ctrl.device,
3327f25f8ef7SHannes Reinecke "NVME-FC{%d}: reconnect failure\n",
3328f25f8ef7SHannes Reinecke ctrl->cnum);
33292b632970SJames Smart else
33302b632970SJames Smart dev_warn(ctrl->ctrl.device,
3331f25f8ef7SHannes Reinecke "NVME-FC{%d}: Max reconnect attempts "
3332f25f8ef7SHannes Reinecke "(%d) reached.\n",
3333f25f8ef7SHannes Reinecke ctrl->cnum, ctrl->ctrl.nr_reconnects);
3334f25f8ef7SHannes Reinecke } else
3335f25f8ef7SHannes Reinecke dev_warn(ctrl->ctrl.device,
33362b632970SJames Smart "NVME-FC{%d}: dev_loss_tmo (%d) expired "
333777d0612dSMax Gurtovoy "while waiting for remoteport connectivity.\n",
3338614fc1c0SMartin George ctrl->cnum, min_t(int, portptr->dev_loss_tmo,
3339614fc1c0SMartin George (ctrl->ctrl.opts->max_reconnects *
3340614fc1c0SMartin George ctrl->ctrl.opts->reconnect_delay)));
3341c5017e85SChristoph Hellwig WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
33425bbecdbcSJames Smart }
33435bbecdbcSJames Smart }
33445bbecdbcSJames Smart
33455bbecdbcSJames Smart static void
nvme_fc_reset_ctrl_work(struct work_struct * work)334661bff8efSJames Smart nvme_fc_reset_ctrl_work(struct work_struct *work)
334761bff8efSJames Smart {
334861bff8efSJames Smart struct nvme_fc_ctrl *ctrl =
3349d86c4d8eSChristoph Hellwig container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
33504cff280aSJames Smart
3351d09f2b45SSagi Grimberg nvme_stop_ctrl(&ctrl->ctrl);
335244c6ec77SJames Smart
3353ac9b820eSJames Smart /* will block will waiting for io to terminate */
3354ac9b820eSJames Smart nvme_fc_delete_association(ctrl);
33552b632970SJames Smart
3356ac9b820eSJames Smart if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
3357ac9b820eSJames Smart dev_err(ctrl->ctrl.device,
3358ac9b820eSJames Smart "NVME-FC{%d}: error_recovery: Couldn't change state "
3359ac9b820eSJames Smart "to CONNECTING\n", ctrl->cnum);
3360ac9b820eSJames Smart
3361ac9b820eSJames Smart if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
3362ac9b820eSJames Smart if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3363ac9b820eSJames Smart dev_err(ctrl->ctrl.device,
3364ac9b820eSJames Smart "NVME-FC{%d}: failed to schedule connect "
3365ac9b820eSJames Smart "after reset\n", ctrl->cnum);
3366ac9b820eSJames Smart } else {
3367ac9b820eSJames Smart flush_delayed_work(&ctrl->connect_work);
3368ac9b820eSJames Smart }
3369ac9b820eSJames Smart } else {
3370ac9b820eSJames Smart nvme_fc_reconnect_or_delete(ctrl, -ENOTCONN);
3371ac9b820eSJames Smart }
337261bff8efSJames Smart }
337361bff8efSJames Smart
33744cff280aSJames Smart
337561bff8efSJames Smart static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
337661bff8efSJames Smart .name = "fc",
337761bff8efSJames Smart .module = THIS_MODULE,
3378d3d5b87dSChristoph Hellwig .flags = NVME_F_FABRICS,
337961bff8efSJames Smart .reg_read32 = nvmf_reg_read32,
338061bff8efSJames Smart .reg_read64 = nvmf_reg_read64,
338161bff8efSJames Smart .reg_write32 = nvmf_reg_write32,
338261bff8efSJames Smart .free_ctrl = nvme_fc_nvme_ctrl_freed,
338361bff8efSJames Smart .submit_async_event = nvme_fc_submit_async_event,
3384c5017e85SChristoph Hellwig .delete_ctrl = nvme_fc_delete_ctrl,
338561bff8efSJames Smart .get_address = nvmf_get_address,
338661bff8efSJames Smart };
338761bff8efSJames Smart
338861bff8efSJames Smart static void
nvme_fc_connect_ctrl_work(struct work_struct * work)338961bff8efSJames Smart nvme_fc_connect_ctrl_work(struct work_struct *work)
339061bff8efSJames Smart {
339161bff8efSJames Smart int ret;
339261bff8efSJames Smart
339361bff8efSJames Smart struct nvme_fc_ctrl *ctrl =
339461bff8efSJames Smart container_of(to_delayed_work(work),
339561bff8efSJames Smart struct nvme_fc_ctrl, connect_work);
339661bff8efSJames Smart
339761bff8efSJames Smart ret = nvme_fc_create_association(ctrl);
33985bbecdbcSJames Smart if (ret)
33995bbecdbcSJames Smart nvme_fc_reconnect_or_delete(ctrl, ret);
34005bbecdbcSJames Smart else
340161bff8efSJames Smart dev_info(ctrl->ctrl.device,
34024c984154SJames Smart "NVME-FC{%d}: controller connect complete\n",
340361bff8efSJames Smart ctrl->cnum);
340461bff8efSJames Smart }
340561bff8efSJames Smart
340661bff8efSJames Smart
340761bff8efSJames Smart static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
340861bff8efSJames Smart .queue_rq = nvme_fc_queue_rq,
340961bff8efSJames Smart .complete = nvme_fc_complete_rq,
341076f983cbSChristoph Hellwig .init_request = nvme_fc_init_request,
341161bff8efSJames Smart .exit_request = nvme_fc_exit_request,
341261bff8efSJames Smart .init_hctx = nvme_fc_init_admin_hctx,
341361bff8efSJames Smart .timeout = nvme_fc_timeout,
341461bff8efSJames Smart };
341561bff8efSJames Smart
3416e399441dSJames Smart
341756d5f4f1SJames Smart /*
341856d5f4f1SJames Smart * Fails a controller request if it matches an existing controller
341956d5f4f1SJames Smart * (association) with the same tuple:
342056d5f4f1SJames Smart * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
342156d5f4f1SJames Smart *
342256d5f4f1SJames Smart * The ports don't need to be compared as they are intrinsically
342356d5f4f1SJames Smart * already matched by the port pointers supplied.
342456d5f4f1SJames Smart */
342556d5f4f1SJames Smart static bool
nvme_fc_existing_controller(struct nvme_fc_rport * rport,struct nvmf_ctrl_options * opts)342656d5f4f1SJames Smart nvme_fc_existing_controller(struct nvme_fc_rport *rport,
342756d5f4f1SJames Smart struct nvmf_ctrl_options *opts)
342856d5f4f1SJames Smart {
342956d5f4f1SJames Smart struct nvme_fc_ctrl *ctrl;
343056d5f4f1SJames Smart unsigned long flags;
343156d5f4f1SJames Smart bool found = false;
343256d5f4f1SJames Smart
343356d5f4f1SJames Smart spin_lock_irqsave(&rport->lock, flags);
343456d5f4f1SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
343556d5f4f1SJames Smart found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
343656d5f4f1SJames Smart if (found)
343756d5f4f1SJames Smart break;
343856d5f4f1SJames Smart }
343956d5f4f1SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
344056d5f4f1SJames Smart
344156d5f4f1SJames Smart return found;
344256d5f4f1SJames Smart }
344356d5f4f1SJames Smart
3444e399441dSJames Smart static struct nvme_ctrl *
nvme_fc_init_ctrl(struct device * dev,struct nvmf_ctrl_options * opts,struct nvme_fc_lport * lport,struct nvme_fc_rport * rport)344561bff8efSJames Smart nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3446e399441dSJames Smart struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3447e399441dSJames Smart {
3448e399441dSJames Smart struct nvme_fc_ctrl *ctrl;
3449e399441dSJames Smart unsigned long flags;
3450f673714aSJames Smart int ret, idx, ctrl_loss_tmo;
3451e399441dSJames Smart
345285e6a6adSJames Smart if (!(rport->remoteport.port_role &
345385e6a6adSJames Smart (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
345485e6a6adSJames Smart ret = -EBADR;
345585e6a6adSJames Smart goto out_fail;
345685e6a6adSJames Smart }
345785e6a6adSJames Smart
345856d5f4f1SJames Smart if (!opts->duplicate_connect &&
345956d5f4f1SJames Smart nvme_fc_existing_controller(rport, opts)) {
346056d5f4f1SJames Smart ret = -EALREADY;
346156d5f4f1SJames Smart goto out_fail;
346256d5f4f1SJames Smart }
346356d5f4f1SJames Smart
3464e399441dSJames Smart ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3465e399441dSJames Smart if (!ctrl) {
3466e399441dSJames Smart ret = -ENOMEM;
3467e399441dSJames Smart goto out_fail;
3468e399441dSJames Smart }
3469e399441dSJames Smart
34703dd83f40SSagi Grimberg idx = ida_alloc(&nvme_fc_ctrl_cnt, GFP_KERNEL);
3471e399441dSJames Smart if (idx < 0) {
3472e399441dSJames Smart ret = -ENOSPC;
34738c5c6605SJames Smart goto out_free_ctrl;
3474e399441dSJames Smart }
3475e399441dSJames Smart
3476f673714aSJames Smart /*
3477f673714aSJames Smart * if ctrl_loss_tmo is being enforced and the default reconnect delay
3478f673714aSJames Smart * is being used, change to a shorter reconnect delay for FC.
3479f673714aSJames Smart */
3480f673714aSJames Smart if (opts->max_reconnects != -1 &&
3481f673714aSJames Smart opts->reconnect_delay == NVMF_DEF_RECONNECT_DELAY &&
3482f673714aSJames Smart opts->reconnect_delay > NVME_FC_DEFAULT_RECONNECT_TMO) {
3483f673714aSJames Smart ctrl_loss_tmo = opts->max_reconnects * opts->reconnect_delay;
3484f673714aSJames Smart opts->reconnect_delay = NVME_FC_DEFAULT_RECONNECT_TMO;
3485f673714aSJames Smart opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
3486f673714aSJames Smart opts->reconnect_delay);
3487f673714aSJames Smart }
3488f673714aSJames Smart
3489e399441dSJames Smart ctrl->ctrl.opts = opts;
34904c984154SJames Smart ctrl->ctrl.nr_reconnects = 0;
349106f3d71eSJames Smart if (lport->dev)
3492103e515eSHannes Reinecke ctrl->ctrl.numa_node = dev_to_node(lport->dev);
349306f3d71eSJames Smart else
349406f3d71eSJames Smart ctrl->ctrl.numa_node = NUMA_NO_NODE;
3495e399441dSJames Smart INIT_LIST_HEAD(&ctrl->ctrl_list);
3496e399441dSJames Smart ctrl->lport = lport;
3497e399441dSJames Smart ctrl->rport = rport;
3498e399441dSJames Smart ctrl->dev = lport->dev;
3499e399441dSJames Smart ctrl->cnum = idx;
35004c984154SJames Smart ctrl->ioq_live = false;
35018a82dbf1SJames Smart init_waitqueue_head(&ctrl->ioabort_wait);
3502e399441dSJames Smart
3503e399441dSJames Smart get_device(ctrl->dev);
3504e399441dSJames Smart kref_init(&ctrl->ref);
3505e399441dSJames Smart
3506d86c4d8eSChristoph Hellwig INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
350761bff8efSJames Smart INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
350819fce047SJames Smart INIT_WORK(&ctrl->ioerr_work, nvme_fc_ctrl_ioerr_work);
3509e399441dSJames Smart spin_lock_init(&ctrl->lock);
3510e399441dSJames Smart
3511e399441dSJames Smart /* io queue count */
3512d858e5f0SSagi Grimberg ctrl->ctrl.queue_count = min_t(unsigned int,
3513e399441dSJames Smart opts->nr_io_queues,
3514e399441dSJames Smart lport->ops->max_hw_queues);
3515d858e5f0SSagi Grimberg ctrl->ctrl.queue_count++; /* +1 for admin queue */
3516e399441dSJames Smart
3517e399441dSJames Smart ctrl->ctrl.sqsize = opts->queue_size - 1;
3518e399441dSJames Smart ctrl->ctrl.kato = opts->kato;
35194c984154SJames Smart ctrl->ctrl.cntlid = 0xffff;
3520e399441dSJames Smart
3521e399441dSJames Smart ret = -ENOMEM;
3522d858e5f0SSagi Grimberg ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3523d858e5f0SSagi Grimberg sizeof(struct nvme_fc_queue), GFP_KERNEL);
3524e399441dSJames Smart if (!ctrl->queues)
352561bff8efSJames Smart goto out_free_ida;
3526e399441dSJames Smart
35273e493c00SJames Smart nvme_fc_init_queue(ctrl, 0);
35283e493c00SJames Smart
352961bff8efSJames Smart /*
353061bff8efSJames Smart * Would have been nice to init io queues tag set as well.
353161bff8efSJames Smart * However, we require interaction from the controller
353261bff8efSJames Smart * for max io queue count before we can do so.
353361bff8efSJames Smart * Defer this to the connect path.
353461bff8efSJames Smart */
3535e399441dSJames Smart
353661bff8efSJames Smart ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3537e399441dSJames Smart if (ret)
353898e35280SRoss Lagerwall goto out_free_queues;
3539e399441dSJames Smart
354061bff8efSJames Smart /* at this point, teardown path changes to ref counting on nvme ctrl */
3541e399441dSJames Smart
354298e35280SRoss Lagerwall ret = nvme_alloc_admin_tag_set(&ctrl->ctrl, &ctrl->admin_tag_set,
354398e35280SRoss Lagerwall &nvme_fc_admin_mq_ops,
3544d67790ddSKees Cook struct_size_t(struct nvme_fcp_op_w_sgl, priv,
354598e35280SRoss Lagerwall ctrl->lport->ops->fcprqst_priv_sz));
354698e35280SRoss Lagerwall if (ret)
354798e35280SRoss Lagerwall goto fail_ctrl;
354898e35280SRoss Lagerwall
3549e399441dSJames Smart spin_lock_irqsave(&rport->lock, flags);
3550e399441dSJames Smart list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3551e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags);
3552e399441dSJames Smart
35534c984154SJames Smart if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING) ||
35544c984154SJames Smart !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
35554c984154SJames Smart dev_err(ctrl->ctrl.device,
35564c984154SJames Smart "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
35574c984154SJames Smart goto fail_ctrl;
355817c4dc6eSJames Smart }
355917c4dc6eSJames Smart
35604c984154SJames Smart if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
35614c984154SJames Smart dev_err(ctrl->ctrl.device,
35624c984154SJames Smart "NVME-FC{%d}: failed to schedule initial connect\n",
35634c984154SJames Smart ctrl->cnum);
35644c984154SJames Smart goto fail_ctrl;
35654c984154SJames Smart }
35664c984154SJames Smart
35674c984154SJames Smart flush_delayed_work(&ctrl->connect_work);
35684c984154SJames Smart
35694c984154SJames Smart dev_info(ctrl->ctrl.device,
35704c984154SJames Smart "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
3571e5ea42faSHannes Reinecke ctrl->cnum, nvmf_ctrl_subsysnqn(&ctrl->ctrl));
35724c984154SJames Smart
35734c984154SJames Smart return &ctrl->ctrl;
35744c984154SJames Smart
35754c984154SJames Smart fail_ctrl:
3576cf25809bSJames Smart nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
357719fce047SJames Smart cancel_work_sync(&ctrl->ioerr_work);
3578cf25809bSJames Smart cancel_work_sync(&ctrl->ctrl.reset_work);
3579cf25809bSJames Smart cancel_delayed_work_sync(&ctrl->connect_work);
3580cf25809bSJames Smart
3581de41447aSEwan D. Milne ctrl->ctrl.opts = NULL;
358217c4dc6eSJames Smart
358361bff8efSJames Smart /* initiate nvme ctrl ref counting teardown */
358461bff8efSJames Smart nvme_uninit_ctrl(&ctrl->ctrl);
358561bff8efSJames Smart
35860b5a7669SJames Smart /* Remove core ctrl ref. */
35870b5a7669SJames Smart nvme_put_ctrl(&ctrl->ctrl);
35880b5a7669SJames Smart
358961bff8efSJames Smart /* as we're past the point where we transition to the ref
359061bff8efSJames Smart * counting teardown path, if we return a bad pointer here,
359161bff8efSJames Smart * the calling routine, thinking it's prior to the
359261bff8efSJames Smart * transition, will do an rport put. Since the teardown
359361bff8efSJames Smart * path also does a rport put, we do an extra get here to
359461bff8efSJames Smart * so proper order/teardown happens.
359561bff8efSJames Smart */
359661bff8efSJames Smart nvme_fc_rport_get(rport);
359761bff8efSJames Smart
35984c984154SJames Smart return ERR_PTR(-EIO);
3599e399441dSJames Smart
360061bff8efSJames Smart out_free_queues:
360161bff8efSJames Smart kfree(ctrl->queues);
3602e399441dSJames Smart out_free_ida:
360361bff8efSJames Smart put_device(ctrl->dev);
36043dd83f40SSagi Grimberg ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
3605e399441dSJames Smart out_free_ctrl:
3606e399441dSJames Smart kfree(ctrl);
3607e399441dSJames Smart out_fail:
3608e399441dSJames Smart /* exit via here doesn't follow ctlr ref points */
3609e399441dSJames Smart return ERR_PTR(ret);
3610e399441dSJames Smart }
3611e399441dSJames Smart
3612e399441dSJames Smart
3613e399441dSJames Smart struct nvmet_fc_traddr {
3614e399441dSJames Smart u64 nn;
3615e399441dSJames Smart u64 pn;
3616e399441dSJames Smart };
3617e399441dSJames Smart
3618e399441dSJames Smart static int
__nvme_fc_parse_u64(substring_t * sstr,u64 * val)36199c5358e1SJames Smart __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3620e399441dSJames Smart {
3621e399441dSJames Smart u64 token64;
3622e399441dSJames Smart
36239c5358e1SJames Smart if (match_u64(sstr, &token64))
36249c5358e1SJames Smart return -EINVAL;
36259c5358e1SJames Smart *val = token64;
3626e399441dSJames Smart
36279c5358e1SJames Smart return 0;
3628e399441dSJames Smart }
3629e399441dSJames Smart
36309c5358e1SJames Smart /*
36319c5358e1SJames Smart * This routine validates and extracts the WWN's from the TRADDR string.
36329c5358e1SJames Smart * As kernel parsers need the 0x to determine number base, universally
36339c5358e1SJames Smart * build string to parse with 0x prefix before parsing name strings.
36349c5358e1SJames Smart */
36359c5358e1SJames Smart static int
nvme_fc_parse_traddr(struct nvmet_fc_traddr * traddr,char * buf,size_t blen)36369c5358e1SJames Smart nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
36379c5358e1SJames Smart {
36389c5358e1SJames Smart char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
36399c5358e1SJames Smart substring_t wwn = { name, &name[sizeof(name)-1] };
36409c5358e1SJames Smart int nnoffset, pnoffset;
36419c5358e1SJames Smart
3642d4e4230cSMilan P. Gandhi /* validate if string is one of the 2 allowed formats */
36439c5358e1SJames Smart if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
36449c5358e1SJames Smart !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
36459c5358e1SJames Smart !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
36469c5358e1SJames Smart "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
36479c5358e1SJames Smart nnoffset = NVME_FC_TRADDR_OXNNLEN;
36489c5358e1SJames Smart pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
36499c5358e1SJames Smart NVME_FC_TRADDR_OXNNLEN;
36509c5358e1SJames Smart } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
36519c5358e1SJames Smart !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
36529c5358e1SJames Smart !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
36539c5358e1SJames Smart "pn-", NVME_FC_TRADDR_NNLEN))) {
36549c5358e1SJames Smart nnoffset = NVME_FC_TRADDR_NNLEN;
36559c5358e1SJames Smart pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
36569c5358e1SJames Smart } else
36579c5358e1SJames Smart goto out_einval;
36589c5358e1SJames Smart
36599c5358e1SJames Smart name[0] = '0';
36609c5358e1SJames Smart name[1] = 'x';
36619c5358e1SJames Smart name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
36629c5358e1SJames Smart
36639c5358e1SJames Smart memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
36649c5358e1SJames Smart if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
36659c5358e1SJames Smart goto out_einval;
36669c5358e1SJames Smart
36679c5358e1SJames Smart memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
36689c5358e1SJames Smart if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
36699c5358e1SJames Smart goto out_einval;
36709c5358e1SJames Smart
36719c5358e1SJames Smart return 0;
36729c5358e1SJames Smart
36739c5358e1SJames Smart out_einval:
36749c5358e1SJames Smart pr_warn("%s: bad traddr string\n", __func__);
36759c5358e1SJames Smart return -EINVAL;
3676e399441dSJames Smart }
3677e399441dSJames Smart
3678e399441dSJames Smart static struct nvme_ctrl *
nvme_fc_create_ctrl(struct device * dev,struct nvmf_ctrl_options * opts)3679e399441dSJames Smart nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3680e399441dSJames Smart {
3681e399441dSJames Smart struct nvme_fc_lport *lport;
3682e399441dSJames Smart struct nvme_fc_rport *rport;
368361bff8efSJames Smart struct nvme_ctrl *ctrl;
3684e399441dSJames Smart struct nvmet_fc_traddr laddr = { 0L, 0L };
3685e399441dSJames Smart struct nvmet_fc_traddr raddr = { 0L, 0L };
3686e399441dSJames Smart unsigned long flags;
3687e399441dSJames Smart int ret;
3688e399441dSJames Smart
36899c5358e1SJames Smart ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3690e399441dSJames Smart if (ret || !raddr.nn || !raddr.pn)
3691e399441dSJames Smart return ERR_PTR(-EINVAL);
3692e399441dSJames Smart
36939c5358e1SJames Smart ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3694e399441dSJames Smart if (ret || !laddr.nn || !laddr.pn)
3695e399441dSJames Smart return ERR_PTR(-EINVAL);
3696e399441dSJames Smart
3697e399441dSJames Smart /* find the host and remote ports to connect together */
3698e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
3699e399441dSJames Smart list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3700e399441dSJames Smart if (lport->localport.node_name != laddr.nn ||
37019e0e8dacSJames Smart lport->localport.port_name != laddr.pn ||
37029e0e8dacSJames Smart lport->localport.port_state != FC_OBJSTATE_ONLINE)
3703e399441dSJames Smart continue;
3704e399441dSJames Smart
3705e399441dSJames Smart list_for_each_entry(rport, &lport->endp_list, endp_list) {
3706e399441dSJames Smart if (rport->remoteport.node_name != raddr.nn ||
37079e0e8dacSJames Smart rport->remoteport.port_name != raddr.pn ||
37089e0e8dacSJames Smart rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3709e399441dSJames Smart continue;
3710e399441dSJames Smart
3711e399441dSJames Smart /* if fail to get reference fall through. Will error */
3712e399441dSJames Smart if (!nvme_fc_rport_get(rport))
3713e399441dSJames Smart break;
3714e399441dSJames Smart
3715e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
3716e399441dSJames Smart
371761bff8efSJames Smart ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
371861bff8efSJames Smart if (IS_ERR(ctrl))
371961bff8efSJames Smart nvme_fc_rport_put(rport);
372061bff8efSJames Smart return ctrl;
3721e399441dSJames Smart }
3722e399441dSJames Smart }
3723e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
3724e399441dSJames Smart
37254fb135adSJohannes Thumshirn pr_warn("%s: %s - %s combination not found\n",
37264fb135adSJohannes Thumshirn __func__, opts->traddr, opts->host_traddr);
3727e399441dSJames Smart return ERR_PTR(-ENOENT);
3728e399441dSJames Smart }
3729e399441dSJames Smart
3730e399441dSJames Smart
3731e399441dSJames Smart static struct nvmf_transport_ops nvme_fc_transport = {
3732e399441dSJames Smart .name = "fc",
37330de5cd36SRoy Shterman .module = THIS_MODULE,
3734e399441dSJames Smart .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
37355bbecdbcSJames Smart .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3736e399441dSJames Smart .create_ctrl = nvme_fc_create_ctrl,
3737e399441dSJames Smart };
3738e399441dSJames Smart
373997faec53SJames Smart /* Arbitrary successive failures max. With lots of subsystems could be high */
374097faec53SJames Smart #define DISCOVERY_MAX_FAIL 20
374197faec53SJames Smart
nvme_fc_nvme_discovery_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)374297faec53SJames Smart static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
374397faec53SJames Smart struct device_attribute *attr, const char *buf, size_t count)
374497faec53SJames Smart {
374597faec53SJames Smart unsigned long flags;
374697faec53SJames Smart LIST_HEAD(local_disc_list);
374797faec53SJames Smart struct nvme_fc_lport *lport;
374897faec53SJames Smart struct nvme_fc_rport *rport;
374997faec53SJames Smart int failcnt = 0;
375097faec53SJames Smart
375197faec53SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
375297faec53SJames Smart restart:
375397faec53SJames Smart list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
375497faec53SJames Smart list_for_each_entry(rport, &lport->endp_list, endp_list) {
375597faec53SJames Smart if (!nvme_fc_lport_get(lport))
375697faec53SJames Smart continue;
375797faec53SJames Smart if (!nvme_fc_rport_get(rport)) {
375897faec53SJames Smart /*
375997faec53SJames Smart * This is a temporary condition. Upon restart
376097faec53SJames Smart * this rport will be gone from the list.
376197faec53SJames Smart *
376297faec53SJames Smart * Revert the lport put and retry. Anything
376397faec53SJames Smart * added to the list already will be skipped (as
376497faec53SJames Smart * they are no longer list_empty). Loops should
376597faec53SJames Smart * resume at rports that were not yet seen.
376697faec53SJames Smart */
376797faec53SJames Smart nvme_fc_lport_put(lport);
376897faec53SJames Smart
376997faec53SJames Smart if (failcnt++ < DISCOVERY_MAX_FAIL)
377097faec53SJames Smart goto restart;
377197faec53SJames Smart
377297faec53SJames Smart pr_err("nvme_discovery: too many reference "
377397faec53SJames Smart "failures\n");
377497faec53SJames Smart goto process_local_list;
377597faec53SJames Smart }
377697faec53SJames Smart if (list_empty(&rport->disc_list))
377797faec53SJames Smart list_add_tail(&rport->disc_list,
377897faec53SJames Smart &local_disc_list);
377997faec53SJames Smart }
378097faec53SJames Smart }
378197faec53SJames Smart
378297faec53SJames Smart process_local_list:
378397faec53SJames Smart while (!list_empty(&local_disc_list)) {
378497faec53SJames Smart rport = list_first_entry(&local_disc_list,
378597faec53SJames Smart struct nvme_fc_rport, disc_list);
378697faec53SJames Smart list_del_init(&rport->disc_list);
378797faec53SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
378897faec53SJames Smart
378997faec53SJames Smart lport = rport->lport;
379097faec53SJames Smart /* signal discovery. Won't hurt if it repeats */
379197faec53SJames Smart nvme_fc_signal_discovery_scan(lport, rport);
379297faec53SJames Smart nvme_fc_rport_put(rport);
379397faec53SJames Smart nvme_fc_lport_put(lport);
379497faec53SJames Smart
379597faec53SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
379697faec53SJames Smart }
379797faec53SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
379897faec53SJames Smart
379997faec53SJames Smart return count;
380097faec53SJames Smart }
38013dbbca75SMuneendra Kumar
380255d7baa3SChristoph Hellwig static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
380355d7baa3SChristoph Hellwig
380455d7baa3SChristoph Hellwig #ifdef CONFIG_BLK_CGROUP_FC_APPID
38053dbbca75SMuneendra Kumar /* Parse the cgroup id from a buf and return the length of cgrpid */
fc_parse_cgrpid(const char * buf,u64 * id)38063dbbca75SMuneendra Kumar static int fc_parse_cgrpid(const char *buf, u64 *id)
38073dbbca75SMuneendra Kumar {
38083dbbca75SMuneendra Kumar char cgrp_id[16+1];
38093dbbca75SMuneendra Kumar int cgrpid_len, j;
38103dbbca75SMuneendra Kumar
38113dbbca75SMuneendra Kumar memset(cgrp_id, 0x0, sizeof(cgrp_id));
38123dbbca75SMuneendra Kumar for (cgrpid_len = 0, j = 0; cgrpid_len < 17; cgrpid_len++) {
38133dbbca75SMuneendra Kumar if (buf[cgrpid_len] != ':')
38143dbbca75SMuneendra Kumar cgrp_id[cgrpid_len] = buf[cgrpid_len];
38153dbbca75SMuneendra Kumar else {
38163dbbca75SMuneendra Kumar j = 1;
38173dbbca75SMuneendra Kumar break;
38183dbbca75SMuneendra Kumar }
38193dbbca75SMuneendra Kumar }
38203dbbca75SMuneendra Kumar if (!j)
38213dbbca75SMuneendra Kumar return -EINVAL;
38223dbbca75SMuneendra Kumar if (kstrtou64(cgrp_id, 16, id) < 0)
38233dbbca75SMuneendra Kumar return -EINVAL;
38243dbbca75SMuneendra Kumar return cgrpid_len;
38253dbbca75SMuneendra Kumar }
38263dbbca75SMuneendra Kumar
38273dbbca75SMuneendra Kumar /*
3828c814153cSChristoph Hellwig * Parse and update the appid in the blkcg associated with the cgroupid.
38293dbbca75SMuneendra Kumar */
fc_appid_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3830c814153cSChristoph Hellwig static ssize_t fc_appid_store(struct device *dev,
3831c814153cSChristoph Hellwig struct device_attribute *attr, const char *buf, size_t count)
38323dbbca75SMuneendra Kumar {
38339317d001SChristoph Hellwig size_t orig_count = count;
38343dbbca75SMuneendra Kumar u64 cgrp_id;
38353dbbca75SMuneendra Kumar int appid_len = 0;
38363dbbca75SMuneendra Kumar int cgrpid_len = 0;
38373dbbca75SMuneendra Kumar char app_id[FC_APPID_LEN];
38383dbbca75SMuneendra Kumar int ret = 0;
38393dbbca75SMuneendra Kumar
38403dbbca75SMuneendra Kumar if (buf[count-1] == '\n')
38413dbbca75SMuneendra Kumar count--;
38423dbbca75SMuneendra Kumar
38433dbbca75SMuneendra Kumar if ((count > (16+1+FC_APPID_LEN)) || (!strchr(buf, ':')))
38443dbbca75SMuneendra Kumar return -EINVAL;
38453dbbca75SMuneendra Kumar
38463dbbca75SMuneendra Kumar cgrpid_len = fc_parse_cgrpid(buf, &cgrp_id);
38473dbbca75SMuneendra Kumar if (cgrpid_len < 0)
38483dbbca75SMuneendra Kumar return -EINVAL;
38493dbbca75SMuneendra Kumar appid_len = count - cgrpid_len - 1;
38503dbbca75SMuneendra Kumar if (appid_len > FC_APPID_LEN)
38513dbbca75SMuneendra Kumar return -EINVAL;
38523dbbca75SMuneendra Kumar
38533dbbca75SMuneendra Kumar memset(app_id, 0x0, sizeof(app_id));
38543dbbca75SMuneendra Kumar memcpy(app_id, &buf[cgrpid_len+1], appid_len);
38553dbbca75SMuneendra Kumar ret = blkcg_set_fc_appid(app_id, cgrp_id, sizeof(app_id));
38563dbbca75SMuneendra Kumar if (ret < 0)
38573dbbca75SMuneendra Kumar return ret;
38589317d001SChristoph Hellwig return orig_count;
38593dbbca75SMuneendra Kumar }
38603dbbca75SMuneendra Kumar static DEVICE_ATTR(appid_store, 0200, NULL, fc_appid_store);
386155d7baa3SChristoph Hellwig #endif /* CONFIG_BLK_CGROUP_FC_APPID */
386297faec53SJames Smart
386397faec53SJames Smart static struct attribute *nvme_fc_attrs[] = {
386497faec53SJames Smart &dev_attr_nvme_discovery.attr,
386555d7baa3SChristoph Hellwig #ifdef CONFIG_BLK_CGROUP_FC_APPID
38663dbbca75SMuneendra Kumar &dev_attr_appid_store.attr,
386755d7baa3SChristoph Hellwig #endif
386897faec53SJames Smart NULL
386997faec53SJames Smart };
387097faec53SJames Smart
387160b152a5SRikard Falkeborn static const struct attribute_group nvme_fc_attr_group = {
387297faec53SJames Smart .attrs = nvme_fc_attrs,
387397faec53SJames Smart };
387497faec53SJames Smart
387597faec53SJames Smart static const struct attribute_group *nvme_fc_attr_groups[] = {
387697faec53SJames Smart &nvme_fc_attr_group,
387797faec53SJames Smart NULL
387897faec53SJames Smart };
387997faec53SJames Smart
388097faec53SJames Smart static struct class fc_class = {
388197faec53SJames Smart .name = "fc",
388297faec53SJames Smart .dev_groups = nvme_fc_attr_groups,
388397faec53SJames Smart };
388497faec53SJames Smart
nvme_fc_init_module(void)3885e399441dSJames Smart static int __init nvme_fc_init_module(void)
3886e399441dSJames Smart {
38875f568556SJames Smart int ret;
38885f568556SJames Smart
38895f568556SJames Smart /*
38905f568556SJames Smart * NOTE:
38915f568556SJames Smart * It is expected that in the future the kernel will combine
38925f568556SJames Smart * the FC-isms that are currently under scsi and now being
38935f568556SJames Smart * added to by NVME into a new standalone FC class. The SCSI
38945f568556SJames Smart * and NVME protocols and their devices would be under this
38955f568556SJames Smart * new FC class.
38965f568556SJames Smart *
38975f568556SJames Smart * As we need something to post FC-specific udev events to,
38985f568556SJames Smart * specifically for nvme probe events, start by creating the
38995f568556SJames Smart * new device class. When the new standalone FC class is
39005f568556SJames Smart * put in place, this code will move to a more generic
39015f568556SJames Smart * location for the class.
39025f568556SJames Smart */
390397faec53SJames Smart ret = class_register(&fc_class);
390497faec53SJames Smart if (ret) {
39055f568556SJames Smart pr_err("couldn't register class fc\n");
3906*baa6b7ebSDaniel Wagner return ret;
39075f568556SJames Smart }
39085f568556SJames Smart
39095f568556SJames Smart /*
39105f568556SJames Smart * Create a device for the FC-centric udev events
39115f568556SJames Smart */
391297faec53SJames Smart fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
39135f568556SJames Smart "fc_udev_device");
39145f568556SJames Smart if (IS_ERR(fc_udev_device)) {
39155f568556SJames Smart pr_err("couldn't create fc_udev device!\n");
39165f568556SJames Smart ret = PTR_ERR(fc_udev_device);
39175f568556SJames Smart goto out_destroy_class;
39185f568556SJames Smart }
39195f568556SJames Smart
39205f568556SJames Smart ret = nvmf_register_transport(&nvme_fc_transport);
39215f568556SJames Smart if (ret)
39225f568556SJames Smart goto out_destroy_device;
39235f568556SJames Smart
39245f568556SJames Smart return 0;
39255f568556SJames Smart
39265f568556SJames Smart out_destroy_device:
392797faec53SJames Smart device_destroy(&fc_class, MKDEV(0, 0));
39285f568556SJames Smart out_destroy_class:
392997faec53SJames Smart class_unregister(&fc_class);
39308730c1ddSHannes Reinecke
39315f568556SJames Smart return ret;
3932e399441dSJames Smart }
3933e399441dSJames Smart
39344c73cbdfSJames Smart static void
nvme_fc_delete_controllers(struct nvme_fc_rport * rport)39354c73cbdfSJames Smart nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
39364c73cbdfSJames Smart {
39374c73cbdfSJames Smart struct nvme_fc_ctrl *ctrl;
39384c73cbdfSJames Smart
39394c73cbdfSJames Smart spin_lock(&rport->lock);
39404c73cbdfSJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
39414c73cbdfSJames Smart dev_warn(ctrl->ctrl.device,
39424c73cbdfSJames Smart "NVME-FC{%d}: transport unloading: deleting ctrl\n",
39434c73cbdfSJames Smart ctrl->cnum);
39444c73cbdfSJames Smart nvme_delete_ctrl(&ctrl->ctrl);
39454c73cbdfSJames Smart }
39464c73cbdfSJames Smart spin_unlock(&rport->lock);
39474c73cbdfSJames Smart }
39484c73cbdfSJames Smart
nvme_fc_exit_module(void)3949*baa6b7ebSDaniel Wagner static void __exit nvme_fc_exit_module(void)
39504c73cbdfSJames Smart {
39514c73cbdfSJames Smart struct nvme_fc_lport *lport;
39524c73cbdfSJames Smart struct nvme_fc_rport *rport;
39534c73cbdfSJames Smart unsigned long flags;
39544c73cbdfSJames Smart
39554c73cbdfSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
3956*baa6b7ebSDaniel Wagner list_for_each_entry(lport, &nvme_fc_lport_list, port_list)
3957*baa6b7ebSDaniel Wagner list_for_each_entry(rport, &lport->endp_list, endp_list)
3958*baa6b7ebSDaniel Wagner nvme_fc_delete_controllers(rport);
39594c73cbdfSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
3960*baa6b7ebSDaniel Wagner flush_workqueue(nvme_delete_wq);
3961e399441dSJames Smart
3962e399441dSJames Smart nvmf_unregister_transport(&nvme_fc_transport);
3963e399441dSJames Smart
396497faec53SJames Smart device_destroy(&fc_class, MKDEV(0, 0));
396597faec53SJames Smart class_unregister(&fc_class);
3966e399441dSJames Smart }
3967e399441dSJames Smart
3968e399441dSJames Smart module_init(nvme_fc_init_module);
3969e399441dSJames Smart module_exit(nvme_fc_exit_module);
3970e399441dSJames Smart
3971e399441dSJames Smart MODULE_LICENSE("GPL v2");
3972