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
789fa5bfdd9SDaniel Wagner set_bit(ASSOC_FAILED, &ctrl->flags);
790*a09e085bSDaniel Wagner nvme_reset_ctrl(&ctrl->ctrl);
7912b632970SJames Smart }
7922b632970SJames Smart
793e399441dSJames Smart /**
794e399441dSJames Smart * nvme_fc_unregister_remoteport - transport entry point called by an
795e399441dSJames Smart * LLDD to deregister/remove a previously
796e399441dSJames Smart * registered a NVME subsystem FC port.
79776c910c7SBart Van Assche * @portptr: pointer to the (registered) remote port that is to be
798e399441dSJames Smart * deregistered.
799e399441dSJames Smart *
800e399441dSJames Smart * Returns:
801e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno
802e399441dSJames Smart * (ex: -ENXIO) upon failure.
803e399441dSJames Smart */
804e399441dSJames Smart int
805e399441dSJames Smart nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
806e399441dSJames Smart {
807e399441dSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
808e399441dSJames Smart struct nvme_fc_ctrl *ctrl;
809e399441dSJames Smart unsigned long flags;
810e399441dSJames Smart
811e399441dSJames Smart if (!portptr)
812e399441dSJames Smart return -EINVAL;
813e399441dSJames Smart
814e399441dSJames Smart spin_lock_irqsave(&rport->lock, flags);
815e399441dSJames Smart
816e399441dSJames Smart if (portptr->port_state != FC_OBJSTATE_ONLINE) {
817e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags);
818e399441dSJames Smart return -EINVAL;
819e399441dSJames Smart }
820e399441dSJames Smart portptr->port_state = FC_OBJSTATE_DELETED;
821e399441dSJames Smart
8222b632970SJames Smart rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
8232b632970SJames Smart
8242b632970SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
8252b632970SJames Smart /* if dev_loss_tmo==0, dev loss is immediate */
8262b632970SJames Smart if (!portptr->dev_loss_tmo) {
8272b632970SJames Smart dev_warn(ctrl->ctrl.device,
82877d0612dSMax Gurtovoy "NVME-FC{%d}: controller connectivity lost.\n",
8292b632970SJames Smart ctrl->cnum);
830c5017e85SChristoph Hellwig nvme_delete_ctrl(&ctrl->ctrl);
8312b632970SJames Smart } else
8322b632970SJames Smart nvme_fc_ctrl_connectivity_loss(ctrl);
8332b632970SJames Smart }
834e399441dSJames Smart
835e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags);
836e399441dSJames Smart
8378d64daf7SJames Smart nvme_fc_abort_lsops(rport);
8388d64daf7SJames Smart
839158bfb88SJames Smart if (atomic_read(&rport->act_ctrl_cnt) == 0)
840158bfb88SJames Smart rport->lport->ops->remoteport_delete(portptr);
841158bfb88SJames Smart
8422b632970SJames Smart /*
8432b632970SJames Smart * release the reference, which will allow, if all controllers
8442b632970SJames Smart * go away, which should only occur after dev_loss_tmo occurs,
8452b632970SJames Smart * for the rport to be torn down.
nvme_fc_unregister_remoteport(struct nvme_fc_remote_port * portptr)8462b632970SJames Smart */
847e399441dSJames Smart nvme_fc_rport_put(rport);
8482b632970SJames Smart
849e399441dSJames Smart return 0;
850e399441dSJames Smart }
851e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
852e399441dSJames Smart
853eaefd5abSJames Smart /**
854eaefd5abSJames Smart * nvme_fc_rescan_remoteport - transport entry point called by an
855eaefd5abSJames Smart * LLDD to request a nvme device rescan.
856eaefd5abSJames Smart * @remoteport: pointer to the (registered) remote port that is to be
857eaefd5abSJames Smart * rescanned.
858eaefd5abSJames Smart *
859eaefd5abSJames Smart * Returns: N/A
860eaefd5abSJames Smart */
861eaefd5abSJames Smart void
862eaefd5abSJames Smart nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
863eaefd5abSJames Smart {
864eaefd5abSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
865eaefd5abSJames Smart
866eaefd5abSJames Smart nvme_fc_signal_discovery_scan(rport->lport, rport);
867eaefd5abSJames Smart }
868eaefd5abSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
869eaefd5abSJames Smart
870ac7fe82bSJames Smart int
871ac7fe82bSJames Smart nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
872ac7fe82bSJames Smart u32 dev_loss_tmo)
873ac7fe82bSJames Smart {
874ac7fe82bSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
875ac7fe82bSJames Smart unsigned long flags;
876ac7fe82bSJames Smart
877ac7fe82bSJames Smart spin_lock_irqsave(&rport->lock, flags);
878ac7fe82bSJames Smart
879ac7fe82bSJames Smart if (portptr->port_state != FC_OBJSTATE_ONLINE) {
880ac7fe82bSJames Smart spin_unlock_irqrestore(&rport->lock, flags);
881ac7fe82bSJames Smart return -EINVAL;
882ac7fe82bSJames Smart }
883ac7fe82bSJames Smart
884ac7fe82bSJames Smart /* a dev_loss_tmo of 0 (immediate) is allowed to be set */
885ac7fe82bSJames Smart rport->remoteport.dev_loss_tmo = dev_loss_tmo;
886ac7fe82bSJames Smart
887ac7fe82bSJames Smart spin_unlock_irqrestore(&rport->lock, flags);
888ac7fe82bSJames Smart
889ac7fe82bSJames Smart return 0;
890ac7fe82bSJames Smart }
891ac7fe82bSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
892ac7fe82bSJames Smart
893e399441dSJames Smart
894e399441dSJames Smart /* *********************** FC-NVME DMA Handling **************************** */
895e399441dSJames Smart
896e399441dSJames Smart /*
897e399441dSJames Smart * The fcloop device passes in a NULL device pointer. Real LLD's will
898e399441dSJames Smart * pass in a valid device pointer. If NULL is passed to the dma mapping
899e399441dSJames Smart * routines, depending on the platform, it may or may not succeed, and
900e399441dSJames Smart * may crash.
901e399441dSJames Smart *
902e399441dSJames Smart * As such:
nvme_fc_rescan_remoteport(struct nvme_fc_remote_port * remoteport)903e399441dSJames Smart * Wrapper all the dma routines and check the dev pointer.
904e399441dSJames Smart *
905e399441dSJames Smart * If simple mappings (return just a dma address, we'll noop them,
906e399441dSJames Smart * returning a dma address of 0.
907e399441dSJames Smart *
908e399441dSJames Smart * On more complex mappings (dma_map_sg), a pseudo routine fills
909e399441dSJames Smart * in the scatter list, setting all dma addresses to 0.
910e399441dSJames Smart */
911e399441dSJames Smart
912e399441dSJames Smart static inline dma_addr_t
913e399441dSJames Smart fc_dma_map_single(struct device *dev, void *ptr, size_t size,
914e399441dSJames Smart enum dma_data_direction dir)
915e399441dSJames Smart {
916e399441dSJames Smart return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
917e399441dSJames Smart }
918e399441dSJames Smart
919e399441dSJames Smart static inline int
920e399441dSJames Smart fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
921e399441dSJames Smart {
922e399441dSJames Smart return dev ? dma_mapping_error(dev, dma_addr) : 0;
923e399441dSJames Smart }
924e399441dSJames Smart
925e399441dSJames Smart static inline void
926e399441dSJames Smart fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
927e399441dSJames Smart enum dma_data_direction dir)
928e399441dSJames Smart {
929e399441dSJames Smart if (dev)
930e399441dSJames Smart dma_unmap_single(dev, addr, size, dir);
931e399441dSJames Smart }
932e399441dSJames Smart
933e399441dSJames Smart static inline void
934e399441dSJames Smart fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
935e399441dSJames Smart enum dma_data_direction dir)
936e399441dSJames Smart {
937e399441dSJames Smart if (dev)
938e399441dSJames Smart dma_sync_single_for_cpu(dev, addr, size, dir);
939e399441dSJames Smart }
940e399441dSJames Smart
941e399441dSJames Smart static inline void
942e399441dSJames Smart fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
943e399441dSJames Smart enum dma_data_direction dir)
944e399441dSJames Smart {
945e399441dSJames Smart if (dev)
946e399441dSJames Smart dma_sync_single_for_device(dev, addr, size, dir);
947e399441dSJames Smart }
948e399441dSJames Smart
949e399441dSJames Smart /* pseudo dma_map_sg call */
950e399441dSJames Smart static int
951e399441dSJames Smart fc_map_sg(struct scatterlist *sg, int nents)
952e399441dSJames Smart {
953e399441dSJames Smart struct scatterlist *s;
fc_dma_map_single(struct device * dev,void * ptr,size_t size,enum dma_data_direction dir)954e399441dSJames Smart int i;
955e399441dSJames Smart
956e399441dSJames Smart WARN_ON(nents == 0 || sg[0].length == 0);
957e399441dSJames Smart
958e399441dSJames Smart for_each_sg(sg, s, nents, i) {
959e399441dSJames Smart s->dma_address = 0L;
960e399441dSJames Smart #ifdef CONFIG_NEED_SG_DMA_LENGTH
fc_dma_mapping_error(struct device * dev,dma_addr_t dma_addr)961e399441dSJames Smart s->dma_length = s->length;
962e399441dSJames Smart #endif
963e399441dSJames Smart }
964e399441dSJames Smart return nents;
965e399441dSJames Smart }
966e399441dSJames Smart
fc_dma_unmap_single(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)967e399441dSJames Smart static inline int
968e399441dSJames Smart fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
969e399441dSJames Smart enum dma_data_direction dir)
970e399441dSJames Smart {
971e399441dSJames Smart return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
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_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
976e399441dSJames Smart enum dma_data_direction dir)
977e399441dSJames Smart {
978e399441dSJames Smart if (dev)
979e399441dSJames Smart dma_unmap_sg(dev, sg, nents, dir);
980e399441dSJames Smart }
981e399441dSJames Smart
982e399441dSJames Smart /* *********************** FC-NVME LS Handling **************************** */
fc_dma_sync_single_for_device(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)983e399441dSJames Smart
984e399441dSJames Smart static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
985e399441dSJames Smart static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
986e399441dSJames Smart
98714fd1e98SJames Smart static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
988e399441dSJames Smart
989e399441dSJames Smart static void
990c913a8b0SJames Smart __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
991e399441dSJames Smart {
fc_map_sg(struct scatterlist * sg,int nents)992c913a8b0SJames Smart struct nvme_fc_rport *rport = lsop->rport;
993e399441dSJames Smart struct nvmefc_ls_req *lsreq = &lsop->ls_req;
994e399441dSJames Smart unsigned long flags;
995e399441dSJames Smart
996c913a8b0SJames Smart spin_lock_irqsave(&rport->lock, flags);
997e399441dSJames Smart
998e399441dSJames Smart if (!lsop->req_queued) {
999c913a8b0SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
1000e399441dSJames Smart return;
1001e399441dSJames Smart }
1002e399441dSJames Smart
1003e399441dSJames Smart list_del(&lsop->lsreq_list);
1004e399441dSJames Smart
1005e399441dSJames Smart lsop->req_queued = false;
1006e399441dSJames Smart
1007c913a8b0SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
1008e399441dSJames Smart
fc_dma_map_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)1009c913a8b0SJames Smart fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1010e399441dSJames Smart (lsreq->rqstlen + lsreq->rsplen),
1011e399441dSJames Smart DMA_BIDIRECTIONAL);
1012e399441dSJames Smart
1013c913a8b0SJames Smart nvme_fc_rport_put(rport);
1014e399441dSJames Smart }
1015e399441dSJames Smart
fc_dma_unmap_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)1016e399441dSJames Smart static int
1017c913a8b0SJames Smart __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
1018e399441dSJames Smart struct nvmefc_ls_req_op *lsop,
1019e399441dSJames Smart void (*done)(struct nvmefc_ls_req *req, int status))
1020e399441dSJames Smart {
1021e399441dSJames Smart struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1022e399441dSJames Smart unsigned long flags;
1023c913a8b0SJames Smart int ret = 0;
1024e399441dSJames Smart
1025c913a8b0SJames Smart if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1026c913a8b0SJames Smart return -ECONNREFUSED;
1027c913a8b0SJames Smart
1028c913a8b0SJames Smart if (!nvme_fc_rport_get(rport))
1029e399441dSJames Smart return -ESHUTDOWN;
1030e399441dSJames Smart
1031e399441dSJames Smart lsreq->done = done;
1032c913a8b0SJames Smart lsop->rport = rport;
1033e399441dSJames Smart lsop->req_queued = false;
1034e399441dSJames Smart INIT_LIST_HEAD(&lsop->lsreq_list);
1035e399441dSJames Smart init_completion(&lsop->ls_done);
1036e399441dSJames Smart
1037c913a8b0SJames Smart lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
1038e399441dSJames Smart lsreq->rqstlen + lsreq->rsplen,
1039e399441dSJames Smart DMA_BIDIRECTIONAL);
1040c913a8b0SJames Smart if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1041c913a8b0SJames Smart ret = -EFAULT;
1042c913a8b0SJames Smart goto out_putrport;
1043e399441dSJames Smart }
1044e399441dSJames Smart lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1045e399441dSJames Smart
1046c913a8b0SJames Smart spin_lock_irqsave(&rport->lock, flags);
1047e399441dSJames Smart
1048c913a8b0SJames Smart list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
1049e399441dSJames Smart
1050e399441dSJames Smart lsop->req_queued = true;
1051e399441dSJames Smart
1052c913a8b0SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
1053e399441dSJames Smart
1054c913a8b0SJames Smart ret = rport->lport->ops->ls_req(&rport->lport->localport,
1055c913a8b0SJames Smart &rport->remoteport, lsreq);
1056e399441dSJames Smart if (ret)
1057c913a8b0SJames Smart goto out_unlink;
1058c913a8b0SJames Smart
1059c913a8b0SJames Smart return 0;
1060c913a8b0SJames Smart
1061c913a8b0SJames Smart out_unlink:
1062e399441dSJames Smart lsop->ls_error = ret;
1063c913a8b0SJames Smart spin_lock_irqsave(&rport->lock, flags);
1064c913a8b0SJames Smart lsop->req_queued = false;
1065c913a8b0SJames Smart list_del(&lsop->lsreq_list);
1066c913a8b0SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
1067c913a8b0SJames Smart fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1068c913a8b0SJames Smart (lsreq->rqstlen + lsreq->rsplen),
1069c913a8b0SJames Smart DMA_BIDIRECTIONAL);
1070c913a8b0SJames Smart out_putrport:
1071c913a8b0SJames Smart nvme_fc_rport_put(rport);
1072e399441dSJames Smart
1073e399441dSJames Smart return ret;
1074e399441dSJames Smart }
1075e399441dSJames Smart
1076e399441dSJames Smart static void
1077e399441dSJames Smart nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1078e399441dSJames Smart {
1079e399441dSJames Smart struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1080e399441dSJames Smart
1081e399441dSJames Smart lsop->ls_error = status;
1082e399441dSJames Smart complete(&lsop->ls_done);
1083e399441dSJames Smart }
1084e399441dSJames Smart
1085e399441dSJames Smart static int
1086c913a8b0SJames Smart nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
1087e399441dSJames Smart {
1088e399441dSJames Smart struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1089e399441dSJames Smart struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1090e399441dSJames Smart int ret;
1091e399441dSJames Smart
1092c913a8b0SJames Smart ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
1093e399441dSJames Smart
1094c913a8b0SJames Smart if (!ret) {
1095e399441dSJames Smart /*
1096e399441dSJames Smart * No timeout/not interruptible as we need the struct
1097e399441dSJames Smart * to exist until the lldd calls us back. Thus mandate
1098e399441dSJames Smart * wait until driver calls back. lldd responsible for
1099e399441dSJames Smart * the timeout action
1100e399441dSJames Smart */
1101e399441dSJames Smart wait_for_completion(&lsop->ls_done);
1102e399441dSJames Smart
1103c913a8b0SJames Smart __nvme_fc_finish_ls_req(lsop);
1104e399441dSJames Smart
1105c913a8b0SJames Smart ret = lsop->ls_error;
1106e399441dSJames Smart }
1107e399441dSJames Smart
1108c913a8b0SJames Smart if (ret)
1109c913a8b0SJames Smart return ret;
1110c913a8b0SJames Smart
1111e399441dSJames Smart /* ACC or RJT payload ? */
1112e399441dSJames Smart if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1113e399441dSJames Smart return -ENXIO;
1114e399441dSJames Smart
1115e399441dSJames Smart return 0;
1116e399441dSJames Smart }
1117e399441dSJames Smart
nvme_fc_send_ls_req_done(struct nvmefc_ls_req * lsreq,int status)1118c913a8b0SJames Smart static int
1119c913a8b0SJames Smart nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
1120e399441dSJames Smart struct nvmefc_ls_req_op *lsop,
1121e399441dSJames Smart void (*done)(struct nvmefc_ls_req *req, int status))
1122e399441dSJames Smart {
1123e399441dSJames Smart /* don't wait for completion */
1124e399441dSJames Smart
1125c913a8b0SJames Smart return __nvme_fc_send_ls_req(rport, lsop, done);
1126e399441dSJames Smart }
nvme_fc_send_ls_req(struct nvme_fc_rport * rport,struct nvmefc_ls_req_op * lsop)1127e399441dSJames Smart
1128e399441dSJames Smart static int
1129e399441dSJames Smart nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1130e399441dSJames Smart struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1131e399441dSJames Smart {
1132e399441dSJames Smart struct nvmefc_ls_req_op *lsop;
1133e399441dSJames Smart struct nvmefc_ls_req *lsreq;
1134e399441dSJames Smart struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1135e399441dSJames Smart struct fcnvme_ls_cr_assoc_acc *assoc_acc;
113614fd1e98SJames Smart unsigned long flags;
1137e399441dSJames Smart int ret, fcret = 0;
1138e399441dSJames Smart
1139e399441dSJames Smart lsop = kzalloc((sizeof(*lsop) +
1140f56bf76fSJames Smart sizeof(*assoc_rqst) + sizeof(*assoc_acc) +
1141f56bf76fSJames Smart ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1142e399441dSJames Smart if (!lsop) {
1143f56bf76fSJames Smart dev_info(ctrl->ctrl.device,
1144f56bf76fSJames Smart "NVME-FC{%d}: send Create Association failed: ENOMEM\n",
1145f56bf76fSJames Smart ctrl->cnum);
1146e399441dSJames Smart ret = -ENOMEM;
1147e399441dSJames Smart goto out_no_memory;
1148e399441dSJames Smart }
1149e399441dSJames Smart
1150f56bf76fSJames Smart assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1];
1151e399441dSJames Smart assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1152f56bf76fSJames Smart lsreq = &lsop->ls_req;
1153f56bf76fSJames Smart if (ctrl->lport->ops->lsrqst_priv_sz)
1154f56bf76fSJames Smart lsreq->private = &assoc_acc[1];
1155f56bf76fSJames Smart else
1156f56bf76fSJames Smart lsreq->private = NULL;
1157e399441dSJames Smart
1158e399441dSJames Smart assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1159e399441dSJames Smart assoc_rqst->desc_list_len =
1160e399441dSJames Smart cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1161e399441dSJames Smart
1162e399441dSJames Smart assoc_rqst->assoc_cmd.desc_tag =
1163e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1164e399441dSJames Smart assoc_rqst->assoc_cmd.desc_len =
1165e399441dSJames Smart fcnvme_lsdesc_len(
1166e399441dSJames Smart sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1167e399441dSJames Smart
1168e399441dSJames Smart assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1169d157e534SJames Smart assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
1170e399441dSJames Smart /* Linux supports only Dynamic controllers */
1171e399441dSJames Smart assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
11728e412263SChristoph Hellwig uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1173e399441dSJames Smart strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1174e399441dSJames Smart min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1175e399441dSJames Smart strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1176e399441dSJames Smart min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
1177e399441dSJames Smart
1178e399441dSJames Smart lsop->queue = queue;
1179e399441dSJames Smart lsreq->rqstaddr = assoc_rqst;
1180e399441dSJames Smart lsreq->rqstlen = sizeof(*assoc_rqst);
1181e399441dSJames Smart lsreq->rspaddr = assoc_acc;
1182e399441dSJames Smart lsreq->rsplen = sizeof(*assoc_acc);
118353b2b2f5SJames Smart lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1184e399441dSJames Smart
1185c913a8b0SJames Smart ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1186e399441dSJames Smart if (ret)
1187e399441dSJames Smart goto out_free_buffer;
1188e399441dSJames Smart
1189e399441dSJames Smart /* process connect LS completion */
1190e399441dSJames Smart
1191e399441dSJames Smart /* validate the ACC response */
1192e399441dSJames Smart if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1193e399441dSJames Smart fcret = VERR_LSACC;
1194f77fc87cSJames Smart else if (assoc_acc->hdr.desc_list_len !=
1195e399441dSJames Smart fcnvme_lsdesc_len(
1196e399441dSJames Smart sizeof(struct fcnvme_ls_cr_assoc_acc)))
1197e399441dSJames Smart fcret = VERR_CR_ASSOC_ACC_LEN;
1198f77fc87cSJames Smart else if (assoc_acc->hdr.rqst.desc_tag !=
1199f77fc87cSJames Smart cpu_to_be32(FCNVME_LSDESC_RQST))
1200e399441dSJames Smart fcret = VERR_LSDESC_RQST;
1201e399441dSJames Smart else if (assoc_acc->hdr.rqst.desc_len !=
1202e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1203e399441dSJames Smart fcret = VERR_LSDESC_RQST_LEN;
1204e399441dSJames Smart else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1205e399441dSJames Smart fcret = VERR_CR_ASSOC;
1206e399441dSJames Smart else if (assoc_acc->associd.desc_tag !=
1207e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1208e399441dSJames Smart fcret = VERR_ASSOC_ID;
1209e399441dSJames Smart else if (assoc_acc->associd.desc_len !=
1210e399441dSJames Smart fcnvme_lsdesc_len(
1211e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id)))
1212e399441dSJames Smart fcret = VERR_ASSOC_ID_LEN;
1213e399441dSJames Smart else if (assoc_acc->connectid.desc_tag !=
1214e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1215e399441dSJames Smart fcret = VERR_CONN_ID;
1216e399441dSJames Smart else if (assoc_acc->connectid.desc_len !=
1217e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1218e399441dSJames Smart fcret = VERR_CONN_ID_LEN;
1219e399441dSJames Smart
1220e399441dSJames Smart if (fcret) {
1221e399441dSJames Smart ret = -EBADF;
1222e399441dSJames Smart dev_err(ctrl->dev,
12237db39484SJames Smart "q %d Create Association LS failed: %s\n",
1224e399441dSJames Smart queue->qnum, validation_errors[fcret]);
1225e399441dSJames Smart } else {
122614fd1e98SJames Smart spin_lock_irqsave(&ctrl->lock, flags);
1227e399441dSJames Smart ctrl->association_id =
1228e399441dSJames Smart be64_to_cpu(assoc_acc->associd.association_id);
1229e399441dSJames Smart queue->connection_id =
1230e399441dSJames Smart be64_to_cpu(assoc_acc->connectid.connection_id);
1231e399441dSJames Smart set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
123214fd1e98SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags);
1233e399441dSJames Smart }
1234e399441dSJames Smart
1235e399441dSJames Smart out_free_buffer:
1236e399441dSJames Smart kfree(lsop);
1237e399441dSJames Smart out_no_memory:
1238e399441dSJames Smart if (ret)
1239e399441dSJames Smart dev_err(ctrl->dev,
1240e399441dSJames Smart "queue %d connect admin queue failed (%d).\n",
1241e399441dSJames Smart queue->qnum, ret);
1242e399441dSJames Smart return ret;
1243e399441dSJames Smart }
1244e399441dSJames Smart
1245e399441dSJames Smart static int
1246e399441dSJames Smart nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1247e399441dSJames Smart u16 qsize, u16 ersp_ratio)
1248e399441dSJames Smart {
1249e399441dSJames Smart struct nvmefc_ls_req_op *lsop;
1250e399441dSJames Smart struct nvmefc_ls_req *lsreq;
1251e399441dSJames Smart struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1252e399441dSJames Smart struct fcnvme_ls_cr_conn_acc *conn_acc;
1253e399441dSJames Smart int ret, fcret = 0;
1254e399441dSJames Smart
1255e399441dSJames Smart lsop = kzalloc((sizeof(*lsop) +
1256f56bf76fSJames Smart sizeof(*conn_rqst) + sizeof(*conn_acc) +
1257f56bf76fSJames Smart ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1258e399441dSJames Smart if (!lsop) {
1259f56bf76fSJames Smart dev_info(ctrl->ctrl.device,
1260f56bf76fSJames Smart "NVME-FC{%d}: send Create Connection failed: ENOMEM\n",
1261f56bf76fSJames Smart ctrl->cnum);
1262e399441dSJames Smart ret = -ENOMEM;
1263e399441dSJames Smart goto out_no_memory;
1264e399441dSJames Smart }
1265e399441dSJames Smart
1266f56bf76fSJames Smart conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)&lsop[1];
1267e399441dSJames Smart conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1268f56bf76fSJames Smart lsreq = &lsop->ls_req;
1269f56bf76fSJames Smart if (ctrl->lport->ops->lsrqst_priv_sz)
1270f56bf76fSJames Smart lsreq->private = (void *)&conn_acc[1];
1271f56bf76fSJames Smart else
1272f56bf76fSJames Smart lsreq->private = NULL;
1273e399441dSJames Smart
1274e399441dSJames Smart conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1275e399441dSJames Smart conn_rqst->desc_list_len = cpu_to_be32(
1276e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id) +
1277e399441dSJames Smart sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1278e399441dSJames Smart
1279e399441dSJames Smart conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1280e399441dSJames Smart conn_rqst->associd.desc_len =
1281e399441dSJames Smart fcnvme_lsdesc_len(
1282e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id));
1283e399441dSJames Smart conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1284e399441dSJames Smart conn_rqst->connect_cmd.desc_tag =
1285e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1286e399441dSJames Smart conn_rqst->connect_cmd.desc_len =
nvme_fc_connect_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,u16 qsize,u16 ersp_ratio)1287e399441dSJames Smart fcnvme_lsdesc_len(
1288e399441dSJames Smart sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1289e399441dSJames Smart conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1290e399441dSJames Smart conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
1291d157e534SJames Smart conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
1292e399441dSJames Smart
1293e399441dSJames Smart lsop->queue = queue;
1294e399441dSJames Smart lsreq->rqstaddr = conn_rqst;
1295e399441dSJames Smart lsreq->rqstlen = sizeof(*conn_rqst);
1296e399441dSJames Smart lsreq->rspaddr = conn_acc;
1297e399441dSJames Smart lsreq->rsplen = sizeof(*conn_acc);
129853b2b2f5SJames Smart lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1299e399441dSJames Smart
1300c913a8b0SJames Smart ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1301e399441dSJames Smart if (ret)
1302e399441dSJames Smart goto out_free_buffer;
1303e399441dSJames Smart
1304e399441dSJames Smart /* process connect LS completion */
1305e399441dSJames Smart
1306e399441dSJames Smart /* validate the ACC response */
1307e399441dSJames Smart if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1308e399441dSJames Smart fcret = VERR_LSACC;
1309f77fc87cSJames Smart else if (conn_acc->hdr.desc_list_len !=
1310e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1311e399441dSJames Smart fcret = VERR_CR_CONN_ACC_LEN;
1312f77fc87cSJames Smart else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1313e399441dSJames Smart fcret = VERR_LSDESC_RQST;
1314e399441dSJames Smart else if (conn_acc->hdr.rqst.desc_len !=
1315e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1316e399441dSJames Smart fcret = VERR_LSDESC_RQST_LEN;
1317e399441dSJames Smart else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1318e399441dSJames Smart fcret = VERR_CR_CONN;
1319e399441dSJames Smart else if (conn_acc->connectid.desc_tag !=
1320e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1321e399441dSJames Smart fcret = VERR_CONN_ID;
1322e399441dSJames Smart else if (conn_acc->connectid.desc_len !=
1323e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1324e399441dSJames Smart fcret = VERR_CONN_ID_LEN;
1325e399441dSJames Smart
1326e399441dSJames Smart if (fcret) {
1327e399441dSJames Smart ret = -EBADF;
1328e399441dSJames Smart dev_err(ctrl->dev,
13297db39484SJames Smart "q %d Create I/O Connection LS failed: %s\n",
1330e399441dSJames Smart queue->qnum, validation_errors[fcret]);
1331e399441dSJames Smart } else {
1332e399441dSJames Smart queue->connection_id =
1333e399441dSJames Smart be64_to_cpu(conn_acc->connectid.connection_id);
1334e399441dSJames Smart set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1335e399441dSJames Smart }
1336e399441dSJames Smart
1337e399441dSJames Smart out_free_buffer:
1338e399441dSJames Smart kfree(lsop);
1339e399441dSJames Smart out_no_memory:
1340e399441dSJames Smart if (ret)
1341e399441dSJames Smart dev_err(ctrl->dev,
13427db39484SJames Smart "queue %d connect I/O queue failed (%d).\n",
1343e399441dSJames Smart queue->qnum, ret);
1344e399441dSJames Smart return ret;
1345e399441dSJames Smart }
1346e399441dSJames Smart
1347e399441dSJames Smart static void
1348e399441dSJames Smart nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1349e399441dSJames Smart {
1350e399441dSJames Smart struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1351e399441dSJames Smart
1352c913a8b0SJames Smart __nvme_fc_finish_ls_req(lsop);
1353e399441dSJames Smart
1354d4e4230cSMilan P. Gandhi /* fc-nvme initiator doesn't care about success or failure of cmd */
1355e399441dSJames Smart
1356e399441dSJames Smart kfree(lsop);
1357e399441dSJames Smart }
1358e399441dSJames Smart
1359e399441dSJames Smart /*
1360e399441dSJames Smart * This routine sends a FC-NVME LS to disconnect (aka terminate)
1361e399441dSJames Smart * the FC-NVME Association. Terminating the association also
1362e399441dSJames Smart * terminates the FC-NVME connections (per queue, both admin and io
1363e399441dSJames Smart * queues) that are part of the association. E.g. things are torn
1364e399441dSJames Smart * down, and the related FC-NVME Association ID and Connection IDs
1365e399441dSJames Smart * become invalid.
1366e399441dSJames Smart *
1367e399441dSJames Smart * The behavior of the fc-nvme initiator is such that it's
1368e399441dSJames Smart * understanding of the association and connections will implicitly
1369e399441dSJames Smart * be torn down. The action is implicit as it may be due to a loss of
1370e399441dSJames Smart * connectivity with the fc-nvme target, so you may never get a
1371e399441dSJames Smart * response even if you tried. As such, the action of this routine
1372e399441dSJames Smart * is to asynchronously send the LS, ignore any results of the LS, and
1373e399441dSJames Smart * continue on with terminating the association. If the fc-nvme target
1374e399441dSJames Smart * is present and receives the LS, it too can tear down.
1375e399441dSJames Smart */
1376e399441dSJames Smart static void
1377e399441dSJames Smart nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1378e399441dSJames Smart {
137953b2b2f5SJames Smart struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
138053b2b2f5SJames Smart struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
1381e399441dSJames Smart struct nvmefc_ls_req_op *lsop;
1382e399441dSJames Smart struct nvmefc_ls_req *lsreq;
1383c913a8b0SJames Smart int ret;
1384e399441dSJames Smart
1385e399441dSJames Smart lsop = kzalloc((sizeof(*lsop) +
1386f56bf76fSJames Smart sizeof(*discon_rqst) + sizeof(*discon_acc) +
1387f56bf76fSJames Smart ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1388f56bf76fSJames Smart if (!lsop) {
nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req * lsreq,int status)1389f56bf76fSJames Smart dev_info(ctrl->ctrl.device,
1390f56bf76fSJames Smart "NVME-FC{%d}: send Disconnect Association "
1391f56bf76fSJames Smart "failed: ENOMEM\n",
1392f56bf76fSJames Smart ctrl->cnum);
1393e399441dSJames Smart return;
1394f56bf76fSJames Smart }
1395e399441dSJames Smart
1396f56bf76fSJames Smart discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)&lsop[1];
139753b2b2f5SJames Smart discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
1398f56bf76fSJames Smart lsreq = &lsop->ls_req;
1399f56bf76fSJames Smart if (ctrl->lport->ops->lsrqst_priv_sz)
1400f56bf76fSJames Smart lsreq->private = (void *)&discon_acc[1];
1401f56bf76fSJames Smart else
1402f56bf76fSJames Smart lsreq->private = NULL;
1403e399441dSJames Smart
1404fd5a5f22SJames Smart nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc,
1405fd5a5f22SJames Smart ctrl->association_id);
1406e399441dSJames Smart
1407c913a8b0SJames Smart ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1408c913a8b0SJames Smart nvme_fc_disconnect_assoc_done);
1409c913a8b0SJames Smart if (ret)
1410c913a8b0SJames Smart kfree(lsop);
1411e399441dSJames Smart }
1412e399441dSJames Smart
141314fd1e98SJames Smart static void
141414fd1e98SJames Smart nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
141514fd1e98SJames Smart {
141614fd1e98SJames Smart struct nvmefc_ls_rcv_op *lsop = lsrsp->nvme_fc_private;
141714fd1e98SJames Smart struct nvme_fc_rport *rport = lsop->rport;
nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl * ctrl)141814fd1e98SJames Smart struct nvme_fc_lport *lport = rport->lport;
141914fd1e98SJames Smart unsigned long flags;
142014fd1e98SJames Smart
142114fd1e98SJames Smart spin_lock_irqsave(&rport->lock, flags);
142214fd1e98SJames Smart list_del(&lsop->lsrcv_list);
142314fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
142414fd1e98SJames Smart
142514fd1e98SJames Smart fc_dma_sync_single_for_cpu(lport->dev, lsop->rspdma,
142614fd1e98SJames Smart sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
142714fd1e98SJames Smart fc_dma_unmap_single(lport->dev, lsop->rspdma,
142814fd1e98SJames Smart sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
142914fd1e98SJames Smart
1430cf3d0084SChristophe JAILLET kfree(lsop->rspbuf);
1431cf3d0084SChristophe JAILLET kfree(lsop->rqstbuf);
143214fd1e98SJames Smart kfree(lsop);
143314fd1e98SJames Smart
143414fd1e98SJames Smart nvme_fc_rport_put(rport);
143514fd1e98SJames Smart }
143614fd1e98SJames Smart
143714fd1e98SJames Smart static void
143814fd1e98SJames Smart nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op *lsop)
143914fd1e98SJames Smart {
144014fd1e98SJames Smart struct nvme_fc_rport *rport = lsop->rport;
144114fd1e98SJames Smart struct nvme_fc_lport *lport = rport->lport;
144214fd1e98SJames Smart struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
144314fd1e98SJames Smart int ret;
144414fd1e98SJames Smart
144514fd1e98SJames Smart fc_dma_sync_single_for_device(lport->dev, lsop->rspdma,
144614fd1e98SJames Smart sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
144714fd1e98SJames Smart
144814fd1e98SJames Smart ret = lport->ops->xmt_ls_rsp(&lport->localport, &rport->remoteport,
144914fd1e98SJames Smart lsop->lsrsp);
145014fd1e98SJames Smart if (ret) {
145114fd1e98SJames Smart dev_warn(lport->dev,
145214fd1e98SJames Smart "LLDD rejected LS RSP xmt: LS %d status %d\n",
145314fd1e98SJames Smart w0->ls_cmd, ret);
145414fd1e98SJames Smart nvme_fc_xmt_ls_rsp_done(lsop->lsrsp);
nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp * lsrsp)145514fd1e98SJames Smart return;
145614fd1e98SJames Smart }
145714fd1e98SJames Smart }
145814fd1e98SJames Smart
145914fd1e98SJames Smart static struct nvme_fc_ctrl *
146014fd1e98SJames Smart nvme_fc_match_disconn_ls(struct nvme_fc_rport *rport,
146114fd1e98SJames Smart struct nvmefc_ls_rcv_op *lsop)
146214fd1e98SJames Smart {
146314fd1e98SJames Smart struct fcnvme_ls_disconnect_assoc_rqst *rqst =
146414fd1e98SJames Smart &lsop->rqstbuf->rq_dis_assoc;
146514fd1e98SJames Smart struct nvme_fc_ctrl *ctrl, *ret = NULL;
146614fd1e98SJames Smart struct nvmefc_ls_rcv_op *oldls = NULL;
146714fd1e98SJames Smart u64 association_id = be64_to_cpu(rqst->associd.association_id);
146814fd1e98SJames Smart unsigned long flags;
146914fd1e98SJames Smart
147014fd1e98SJames Smart spin_lock_irqsave(&rport->lock, flags);
147114fd1e98SJames Smart
147214fd1e98SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
147314fd1e98SJames Smart if (!nvme_fc_ctrl_get(ctrl))
147414fd1e98SJames Smart continue;
147514fd1e98SJames Smart spin_lock(&ctrl->lock);
147614fd1e98SJames Smart if (association_id == ctrl->association_id) {
147714fd1e98SJames Smart oldls = ctrl->rcv_disconn;
147814fd1e98SJames Smart ctrl->rcv_disconn = lsop;
nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op * lsop)147914fd1e98SJames Smart ret = ctrl;
148014fd1e98SJames Smart }
148114fd1e98SJames Smart spin_unlock(&ctrl->lock);
148214fd1e98SJames Smart if (ret)
148314fd1e98SJames Smart /* leave the ctrl get reference */
148414fd1e98SJames Smart break;
148514fd1e98SJames Smart nvme_fc_ctrl_put(ctrl);
148614fd1e98SJames Smart }
148714fd1e98SJames Smart
148814fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
148914fd1e98SJames Smart
149014fd1e98SJames Smart /* transmit a response for anything that was pending */
149114fd1e98SJames Smart if (oldls) {
149214fd1e98SJames Smart dev_info(rport->lport->dev,
149314fd1e98SJames Smart "NVME-FC{%d}: Multiple Disconnect Association "
149414fd1e98SJames Smart "LS's received\n", ctrl->cnum);
149514fd1e98SJames Smart /* overwrite good response with bogus failure */
149614fd1e98SJames Smart oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf,
149714fd1e98SJames Smart sizeof(*oldls->rspbuf),
149814fd1e98SJames Smart rqst->w0.ls_cmd,
149914fd1e98SJames Smart FCNVME_RJT_RC_UNAB,
150014fd1e98SJames Smart FCNVME_RJT_EXP_NONE, 0);
nvme_fc_match_disconn_ls(struct nvme_fc_rport * rport,struct nvmefc_ls_rcv_op * lsop)150114fd1e98SJames Smart nvme_fc_xmt_ls_rsp(oldls);
150214fd1e98SJames Smart }
150314fd1e98SJames Smart
150414fd1e98SJames Smart return ret;
150514fd1e98SJames Smart }
150614fd1e98SJames Smart
150714fd1e98SJames Smart /*
150814fd1e98SJames Smart * returns true to mean LS handled and ls_rsp can be sent
150914fd1e98SJames Smart * returns false to defer ls_rsp xmt (will be done as part of
151014fd1e98SJames Smart * association termination)
151114fd1e98SJames Smart */
151214fd1e98SJames Smart static bool
151314fd1e98SJames Smart nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op *lsop)
151414fd1e98SJames Smart {
151514fd1e98SJames Smart struct nvme_fc_rport *rport = lsop->rport;
151614fd1e98SJames Smart struct fcnvme_ls_disconnect_assoc_rqst *rqst =
151714fd1e98SJames Smart &lsop->rqstbuf->rq_dis_assoc;
151814fd1e98SJames Smart struct fcnvme_ls_disconnect_assoc_acc *acc =
151914fd1e98SJames Smart &lsop->rspbuf->rsp_dis_assoc;
152014fd1e98SJames Smart struct nvme_fc_ctrl *ctrl = NULL;
152114fd1e98SJames Smart int ret = 0;
152214fd1e98SJames Smart
152314fd1e98SJames Smart memset(acc, 0, sizeof(*acc));
152414fd1e98SJames Smart
152514fd1e98SJames Smart ret = nvmefc_vldt_lsreq_discon_assoc(lsop->rqstdatalen, rqst);
152614fd1e98SJames Smart if (!ret) {
152714fd1e98SJames Smart /* match an active association */
152814fd1e98SJames Smart ctrl = nvme_fc_match_disconn_ls(rport, lsop);
152914fd1e98SJames Smart if (!ctrl)
153014fd1e98SJames Smart ret = VERR_NO_ASSOC;
153114fd1e98SJames Smart }
153214fd1e98SJames Smart
153314fd1e98SJames Smart if (ret) {
153414fd1e98SJames Smart dev_info(rport->lport->dev,
153514fd1e98SJames Smart "Disconnect LS failed: %s\n",
153614fd1e98SJames Smart validation_errors[ret]);
153714fd1e98SJames Smart lsop->lsrsp->rsplen = nvme_fc_format_rjt(acc,
153814fd1e98SJames Smart sizeof(*acc), rqst->w0.ls_cmd,
153914fd1e98SJames Smart (ret == VERR_NO_ASSOC) ?
154014fd1e98SJames Smart FCNVME_RJT_RC_INV_ASSOC :
154114fd1e98SJames Smart FCNVME_RJT_RC_LOGIC,
154214fd1e98SJames Smart FCNVME_RJT_EXP_NONE, 0);
154314fd1e98SJames Smart return true;
154414fd1e98SJames Smart }
154514fd1e98SJames Smart
154614fd1e98SJames Smart /* format an ACCept response */
154714fd1e98SJames Smart
154814fd1e98SJames Smart lsop->lsrsp->rsplen = sizeof(*acc);
154914fd1e98SJames Smart
155014fd1e98SJames Smart nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
155114fd1e98SJames Smart fcnvme_lsdesc_len(
155214fd1e98SJames Smart sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
155314fd1e98SJames Smart FCNVME_LS_DISCONNECT_ASSOC);
nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op * lsop)155414fd1e98SJames Smart
155514fd1e98SJames Smart /*
155614fd1e98SJames Smart * the transmit of the response will occur after the exchanges
155714fd1e98SJames Smart * for the association have been ABTS'd by
155814fd1e98SJames Smart * nvme_fc_delete_association().
155914fd1e98SJames Smart */
156014fd1e98SJames Smart
156114fd1e98SJames Smart /* fail the association */
156214fd1e98SJames Smart nvme_fc_error_recovery(ctrl, "Disconnect Association LS received");
156314fd1e98SJames Smart
156414fd1e98SJames Smart /* release the reference taken by nvme_fc_match_disconn_ls() */
156514fd1e98SJames Smart nvme_fc_ctrl_put(ctrl);
156614fd1e98SJames Smart
156714fd1e98SJames Smart return false;
156814fd1e98SJames Smart }
156914fd1e98SJames Smart
157014fd1e98SJames Smart /*
157114fd1e98SJames Smart * Actual Processing routine for received FC-NVME LS Requests from the LLD
157214fd1e98SJames Smart * returns true if a response should be sent afterward, false if rsp will
157314fd1e98SJames Smart * be sent asynchronously.
157414fd1e98SJames Smart */
157514fd1e98SJames Smart static bool
157614fd1e98SJames Smart nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op *lsop)
157714fd1e98SJames Smart {
157814fd1e98SJames Smart struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
157914fd1e98SJames Smart bool ret = true;
158014fd1e98SJames Smart
158114fd1e98SJames Smart lsop->lsrsp->nvme_fc_private = lsop;
158214fd1e98SJames Smart lsop->lsrsp->rspbuf = lsop->rspbuf;
158314fd1e98SJames Smart lsop->lsrsp->rspdma = lsop->rspdma;
158414fd1e98SJames Smart lsop->lsrsp->done = nvme_fc_xmt_ls_rsp_done;
158514fd1e98SJames Smart /* Be preventative. handlers will later set to valid length */
158614fd1e98SJames Smart lsop->lsrsp->rsplen = 0;
158714fd1e98SJames Smart
158814fd1e98SJames Smart /*
158914fd1e98SJames Smart * handlers:
159014fd1e98SJames Smart * parse request input, execute the request, and format the
159114fd1e98SJames Smart * LS response
159214fd1e98SJames Smart */
159314fd1e98SJames Smart switch (w0->ls_cmd) {
159414fd1e98SJames Smart case FCNVME_LS_DISCONNECT_ASSOC:
159514fd1e98SJames Smart ret = nvme_fc_ls_disconnect_assoc(lsop);
159614fd1e98SJames Smart break;
159714fd1e98SJames Smart case FCNVME_LS_DISCONNECT_CONN:
159814fd1e98SJames Smart lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
159914fd1e98SJames Smart sizeof(*lsop->rspbuf), w0->ls_cmd,
160014fd1e98SJames Smart FCNVME_RJT_RC_UNSUP, FCNVME_RJT_EXP_NONE, 0);
160114fd1e98SJames Smart break;
160214fd1e98SJames Smart case FCNVME_LS_CREATE_ASSOCIATION:
160314fd1e98SJames Smart case FCNVME_LS_CREATE_CONNECTION:
160414fd1e98SJames Smart lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
160514fd1e98SJames Smart sizeof(*lsop->rspbuf), w0->ls_cmd,
160614fd1e98SJames Smart FCNVME_RJT_RC_LOGIC, FCNVME_RJT_EXP_NONE, 0);
160714fd1e98SJames Smart break;
160814fd1e98SJames Smart default:
160914fd1e98SJames Smart lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
161014fd1e98SJames Smart sizeof(*lsop->rspbuf), w0->ls_cmd,
161114fd1e98SJames Smart FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
161214fd1e98SJames Smart break;
161314fd1e98SJames Smart }
161414fd1e98SJames Smart
161514fd1e98SJames Smart return(ret);
161614fd1e98SJames Smart }
nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op * lsop)161714fd1e98SJames Smart
161814fd1e98SJames Smart static void
161914fd1e98SJames Smart nvme_fc_handle_ls_rqst_work(struct work_struct *work)
162014fd1e98SJames Smart {
162114fd1e98SJames Smart struct nvme_fc_rport *rport =
162214fd1e98SJames Smart container_of(work, struct nvme_fc_rport, lsrcv_work);
162314fd1e98SJames Smart struct fcnvme_ls_rqst_w0 *w0;
162414fd1e98SJames Smart struct nvmefc_ls_rcv_op *lsop;
162514fd1e98SJames Smart unsigned long flags;
162614fd1e98SJames Smart bool sendrsp;
162714fd1e98SJames Smart
162814fd1e98SJames Smart restart:
162914fd1e98SJames Smart sendrsp = true;
163014fd1e98SJames Smart spin_lock_irqsave(&rport->lock, flags);
163114fd1e98SJames Smart list_for_each_entry(lsop, &rport->ls_rcv_list, lsrcv_list) {
163214fd1e98SJames Smart if (lsop->handled)
163314fd1e98SJames Smart continue;
163414fd1e98SJames Smart
163514fd1e98SJames Smart lsop->handled = true;
163614fd1e98SJames Smart if (rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
163714fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
163814fd1e98SJames Smart sendrsp = nvme_fc_handle_ls_rqst(lsop);
163914fd1e98SJames Smart } else {
164014fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
164114fd1e98SJames Smart w0 = &lsop->rqstbuf->w0;
164214fd1e98SJames Smart lsop->lsrsp->rsplen = nvme_fc_format_rjt(
164314fd1e98SJames Smart lsop->rspbuf,
164414fd1e98SJames Smart sizeof(*lsop->rspbuf),
164514fd1e98SJames Smart w0->ls_cmd,
164614fd1e98SJames Smart FCNVME_RJT_RC_UNAB,
164714fd1e98SJames Smart FCNVME_RJT_EXP_NONE, 0);
164814fd1e98SJames Smart }
164914fd1e98SJames Smart if (sendrsp)
165014fd1e98SJames Smart nvme_fc_xmt_ls_rsp(lsop);
165114fd1e98SJames Smart goto restart;
165214fd1e98SJames Smart }
165314fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
165414fd1e98SJames Smart }
165514fd1e98SJames Smart
1656b2969585SChaitanya Kulkarni static
1657b2969585SChaitanya Kulkarni void nvme_fc_rcv_ls_req_err_msg(struct nvme_fc_lport *lport,
1658b2969585SChaitanya Kulkarni struct fcnvme_ls_rqst_w0 *w0)
1659b2969585SChaitanya Kulkarni {
nvme_fc_handle_ls_rqst_work(struct work_struct * work)1660b2969585SChaitanya Kulkarni dev_info(lport->dev, "RCV %s LS failed: No memory\n",
1661b2969585SChaitanya Kulkarni (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1662b2969585SChaitanya Kulkarni nvmefc_ls_names[w0->ls_cmd] : "");
1663b2969585SChaitanya Kulkarni }
1664b2969585SChaitanya Kulkarni
166572e6329fSJames Smart /**
166672e6329fSJames Smart * nvme_fc_rcv_ls_req - transport entry point called by an LLDD
166772e6329fSJames Smart * upon the reception of a NVME LS request.
166872e6329fSJames Smart *
166972e6329fSJames Smart * The nvme-fc layer will copy payload to an internal structure for
167072e6329fSJames Smart * processing. As such, upon completion of the routine, the LLDD may
167172e6329fSJames Smart * immediately free/reuse the LS request buffer passed in the call.
167272e6329fSJames Smart *
167372e6329fSJames Smart * If this routine returns error, the LLDD should abort the exchange.
167472e6329fSJames Smart *
16752afc4866SChaitanya Kulkarni * @portptr: pointer to the (registered) remote port that the LS
167672e6329fSJames Smart * was received from. The remoteport is associated with
167772e6329fSJames Smart * a specific localport.
167872e6329fSJames Smart * @lsrsp: pointer to a nvmefc_ls_rsp response structure to be
167972e6329fSJames Smart * used to reference the exchange corresponding to the LS
168072e6329fSJames Smart * when issuing an ls response.
168172e6329fSJames Smart * @lsreqbuf: pointer to the buffer containing the LS Request
168272e6329fSJames Smart * @lsreqbuf_len: length, in bytes, of the received LS request
168372e6329fSJames Smart */
168472e6329fSJames Smart int
168572e6329fSJames Smart nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
168672e6329fSJames Smart struct nvmefc_ls_rsp *lsrsp,
168772e6329fSJames Smart void *lsreqbuf, u32 lsreqbuf_len)
168872e6329fSJames Smart {
168972e6329fSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
169072e6329fSJames Smart struct nvme_fc_lport *lport = rport->lport;
169114fd1e98SJames Smart struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf;
169214fd1e98SJames Smart struct nvmefc_ls_rcv_op *lsop;
169314fd1e98SJames Smart unsigned long flags;
169414fd1e98SJames Smart int ret;
169514fd1e98SJames Smart
169614fd1e98SJames Smart nvme_fc_rport_get(rport);
169772e6329fSJames Smart
nvme_fc_rcv_ls_req_err_msg(struct nvme_fc_lport * lport,struct fcnvme_ls_rqst_w0 * w0)169872e6329fSJames Smart /* validate there's a routine to transmit a response */
169914fd1e98SJames Smart if (!lport->ops->xmt_ls_rsp) {
170014fd1e98SJames Smart dev_info(lport->dev,
170114fd1e98SJames Smart "RCV %s LS failed: no LLDD xmt_ls_rsp\n",
170214fd1e98SJames Smart (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
170314fd1e98SJames Smart nvmefc_ls_names[w0->ls_cmd] : "");
170414fd1e98SJames Smart ret = -EINVAL;
170514fd1e98SJames Smart goto out_put;
170614fd1e98SJames Smart }
170714fd1e98SJames Smart
170814fd1e98SJames Smart if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) {
170914fd1e98SJames Smart dev_info(lport->dev,
171014fd1e98SJames Smart "RCV %s LS failed: payload too large\n",
171114fd1e98SJames Smart (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
171214fd1e98SJames Smart nvmefc_ls_names[w0->ls_cmd] : "");
171314fd1e98SJames Smart ret = -E2BIG;
171414fd1e98SJames Smart goto out_put;
171514fd1e98SJames Smart }
171614fd1e98SJames Smart
1717cf3d0084SChristophe JAILLET lsop = kzalloc(sizeof(*lsop), GFP_KERNEL);
17186c90294dSChaitanya Kulkarni if (!lsop) {
1719b2969585SChaitanya Kulkarni nvme_fc_rcv_ls_req_err_msg(lport, w0);
17206c90294dSChaitanya Kulkarni ret = -ENOMEM;
17216c90294dSChaitanya Kulkarni goto out_put;
17226c90294dSChaitanya Kulkarni }
17236c90294dSChaitanya Kulkarni
1724cf3d0084SChristophe JAILLET lsop->rqstbuf = kzalloc(sizeof(*lsop->rqstbuf), GFP_KERNEL);
1725cf3d0084SChristophe JAILLET lsop->rspbuf = kzalloc(sizeof(*lsop->rspbuf), GFP_KERNEL);
nvme_fc_rcv_ls_req(struct nvme_fc_remote_port * portptr,struct nvmefc_ls_rsp * lsrsp,void * lsreqbuf,u32 lsreqbuf_len)17266c90294dSChaitanya Kulkarni if (!lsop->rqstbuf || !lsop->rspbuf) {
1727b2969585SChaitanya Kulkarni nvme_fc_rcv_ls_req_err_msg(lport, w0);
172814fd1e98SJames Smart ret = -ENOMEM;
1729cf3d0084SChristophe JAILLET goto out_free;
173014fd1e98SJames Smart }
173114fd1e98SJames Smart
173214fd1e98SJames Smart lsop->rspdma = fc_dma_map_single(lport->dev, lsop->rspbuf,
173314fd1e98SJames Smart sizeof(*lsop->rspbuf),
173414fd1e98SJames Smart DMA_TO_DEVICE);
173514fd1e98SJames Smart if (fc_dma_mapping_error(lport->dev, lsop->rspdma)) {
173614fd1e98SJames Smart dev_info(lport->dev,
173714fd1e98SJames Smart "RCV %s LS failed: DMA mapping failure\n",
173814fd1e98SJames Smart (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
173914fd1e98SJames Smart nvmefc_ls_names[w0->ls_cmd] : "");
174014fd1e98SJames Smart ret = -EFAULT;
174114fd1e98SJames Smart goto out_free;
174214fd1e98SJames Smart }
174314fd1e98SJames Smart
174414fd1e98SJames Smart lsop->rport = rport;
174514fd1e98SJames Smart lsop->lsrsp = lsrsp;
174614fd1e98SJames Smart
174714fd1e98SJames Smart memcpy(lsop->rqstbuf, lsreqbuf, lsreqbuf_len);
174814fd1e98SJames Smart lsop->rqstdatalen = lsreqbuf_len;
174914fd1e98SJames Smart
175014fd1e98SJames Smart spin_lock_irqsave(&rport->lock, flags);
175114fd1e98SJames Smart if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) {
175214fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
175314fd1e98SJames Smart ret = -ENOTCONN;
175414fd1e98SJames Smart goto out_unmap;
175514fd1e98SJames Smart }
175614fd1e98SJames Smart list_add_tail(&lsop->lsrcv_list, &rport->ls_rcv_list);
175714fd1e98SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
175814fd1e98SJames Smart
175914fd1e98SJames Smart schedule_work(&rport->lsrcv_work);
176072e6329fSJames Smart
176172e6329fSJames Smart return 0;
176214fd1e98SJames Smart
176314fd1e98SJames Smart out_unmap:
176414fd1e98SJames Smart fc_dma_unmap_single(lport->dev, lsop->rspdma,
176514fd1e98SJames Smart sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
176614fd1e98SJames Smart out_free:
1767cf3d0084SChristophe JAILLET kfree(lsop->rspbuf);
1768cf3d0084SChristophe JAILLET kfree(lsop->rqstbuf);
176914fd1e98SJames Smart kfree(lsop);
177014fd1e98SJames Smart out_put:
177114fd1e98SJames Smart nvme_fc_rport_put(rport);
177214fd1e98SJames Smart return ret;
177372e6329fSJames Smart }
177472e6329fSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req);
177572e6329fSJames Smart
1776e399441dSJames Smart
1777e399441dSJames Smart /* *********************** NVME Ctrl Routines **************************** */
1778e399441dSJames Smart
1779e399441dSJames Smart static void
1780e399441dSJames Smart __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1781e399441dSJames Smart struct nvme_fc_fcp_op *op)
1782e399441dSJames Smart {
1783e399441dSJames Smart fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1784e399441dSJames Smart sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1785e399441dSJames Smart fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1786e399441dSJames Smart sizeof(op->cmd_iu), DMA_TO_DEVICE);
1787e399441dSJames Smart
1788e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_UNINIT);
1789e399441dSJames Smart }
1790e399441dSJames Smart
1791e399441dSJames Smart static void
1792d6296d39SChristoph Hellwig nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1793d6296d39SChristoph Hellwig unsigned int hctx_idx)
1794e399441dSJames Smart {
1795e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1796e399441dSJames Smart
17971864ea46SChristoph Hellwig return __nvme_fc_exit_request(to_fc_ctrl(set->driver_data), op);
1798e399441dSJames Smart }
1799e399441dSJames Smart
180078a7ac26SJames Smart static int
180178a7ac26SJames Smart __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
180278a7ac26SJames Smart {
18033efd6e8eSJames Smart unsigned long flags;
18043efd6e8eSJames Smart int opstate;
180578a7ac26SJames Smart
18063efd6e8eSJames Smart spin_lock_irqsave(&ctrl->lock, flags);
18073efd6e8eSJames Smart opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
18083efd6e8eSJames Smart if (opstate != FCPOP_STATE_ACTIVE)
18093efd6e8eSJames Smart atomic_set(&op->state, opstate);
181052793d62SJames Smart else if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) {
181152793d62SJames Smart op->flags |= FCOP_FLAGS_TERMIO;
18123efd6e8eSJames Smart ctrl->iocnt++;
181352793d62SJames Smart }
18143efd6e8eSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags);
18153efd6e8eSJames Smart
18163efd6e8eSJames Smart if (opstate != FCPOP_STATE_ACTIVE)
181778a7ac26SJames Smart return -ECANCELED;
181878a7ac26SJames Smart
181978a7ac26SJames Smart ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
182078a7ac26SJames Smart &ctrl->rport->remoteport,
__nvme_fc_exit_request(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op)182178a7ac26SJames Smart op->queue->lldd_handle,
182278a7ac26SJames Smart &op->fcp_req);
182378a7ac26SJames Smart
182478a7ac26SJames Smart return 0;
182578a7ac26SJames Smart }
182678a7ac26SJames Smart
1827e399441dSJames Smart static void
182878a7ac26SJames Smart nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1829e399441dSJames Smart {
1830e399441dSJames Smart struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
18313efd6e8eSJames Smart int i;
1832e399441dSJames Smart
nvme_fc_exit_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx)18334cff280aSJames Smart /* ensure we've initialized the ops once */
18344cff280aSJames Smart if (!(aen_op->flags & FCOP_FLAGS_AEN))
18354cff280aSJames Smart return;
18364cff280aSJames Smart
18373efd6e8eSJames Smart for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
18383efd6e8eSJames Smart __nvme_fc_abort_op(ctrl, aen_op);
183978a7ac26SJames Smart }
184078a7ac26SJames Smart
1841c3aedd22SJames Smart static inline void
__nvme_fc_abort_op(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op)184278a7ac26SJames Smart __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
18433efd6e8eSJames Smart struct nvme_fc_fcp_op *op, int opstate)
184478a7ac26SJames Smart {
184578a7ac26SJames Smart unsigned long flags;
184678a7ac26SJames Smart
1847c3aedd22SJames Smart if (opstate == FCPOP_STATE_ABORTED) {
184878a7ac26SJames Smart spin_lock_irqsave(&ctrl->lock, flags);
184952793d62SJames Smart if (test_bit(FCCTRL_TERMIO, &ctrl->flags) &&
185052793d62SJames Smart op->flags & FCOP_FLAGS_TERMIO) {
185136715cf4SJames Smart if (!--ctrl->iocnt)
185236715cf4SJames Smart wake_up(&ctrl->ioabort_wait);
185336715cf4SJames Smart }
185478a7ac26SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags);
1855c3aedd22SJames Smart }
185678a7ac26SJames Smart }
1857e399441dSJames Smart
1858baee29acSChristoph Hellwig static void
185919fce047SJames Smart nvme_fc_ctrl_ioerr_work(struct work_struct *work)
186019fce047SJames Smart {
186119fce047SJames Smart struct nvme_fc_ctrl *ctrl =
186219fce047SJames Smart container_of(work, struct nvme_fc_ctrl, ioerr_work);
186319fce047SJames Smart
186419fce047SJames Smart nvme_fc_error_recovery(ctrl, "transport detected io error");
186519fce047SJames Smart }
186619fce047SJames Smart
1867827fc630SMuneendra Kumar /*
1868827fc630SMuneendra Kumar * nvme_fc_io_getuuid - Routine called to get the appid field
nvme_fc_abort_aen_ops(struct nvme_fc_ctrl * ctrl)1869827fc630SMuneendra Kumar * associated with request by the lldd
1870827fc630SMuneendra Kumar * @req:IO request from nvme fc to driver
1871827fc630SMuneendra Kumar * Returns: UUID if there is an appid associated with VM or
1872827fc630SMuneendra Kumar * NULL if the user/libvirt has not set the appid to VM
1873827fc630SMuneendra Kumar */
1874827fc630SMuneendra Kumar char *nvme_fc_io_getuuid(struct nvmefc_fcp_req *req)
1875827fc630SMuneendra Kumar {
1876827fc630SMuneendra Kumar struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1877827fc630SMuneendra Kumar struct request *rq = op->rq;
1878827fc630SMuneendra Kumar
18798ae5b3a6SNigel Kirkland if (!IS_ENABLED(CONFIG_BLK_CGROUP_FC_APPID) || !rq || !rq->bio)
1880827fc630SMuneendra Kumar return NULL;
1881827fc630SMuneendra Kumar return blkcg_get_fc_appid(rq->bio);
1882827fc630SMuneendra Kumar }
__nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl * ctrl,struct nvme_fc_fcp_op * op,int opstate)1883827fc630SMuneendra Kumar EXPORT_SYMBOL_GPL(nvme_fc_io_getuuid);
1884827fc630SMuneendra Kumar
188519fce047SJames Smart static void
1886e399441dSJames Smart nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1887e399441dSJames Smart {
1888e399441dSJames Smart struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1889e399441dSJames Smart struct request *rq = op->rq;
1890e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req;
1891e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl;
1892e399441dSJames Smart struct nvme_fc_queue *queue = op->queue;
1893e399441dSJames Smart struct nvme_completion *cqe = &op->rsp_iu.cqe;
1894458f280dSJames Smart struct nvme_command *sqe = &op->cmd_iu.sqe;
1895d663b69fSChristoph Hellwig __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
189627fa9bc5SChristoph Hellwig union nvme_result result;
18970a02e39fSJames Smart bool terminate_assoc = true;
18983efd6e8eSJames Smart int opstate;
1899e399441dSJames Smart
nvme_fc_ctrl_ioerr_work(struct work_struct * work)1900e399441dSJames Smart /*
1901e399441dSJames Smart * WARNING:
1902e399441dSJames Smart * The current linux implementation of a nvme controller
1903e399441dSJames Smart * allocates a single tag set for all io queues and sizes
1904e399441dSJames Smart * the io queues to fully hold all possible tags. Thus, the
1905e399441dSJames Smart * implementation does not reference or care about the sqhd
1906e399441dSJames Smart * value as it never needs to use the sqhd/sqtail pointers
1907e399441dSJames Smart * for submission pacing.
1908e399441dSJames Smart *
1909e399441dSJames Smart * This affects the FC-NVME implementation in two ways:
1910e399441dSJames Smart * 1) As the value doesn't matter, we don't need to waste
1911e399441dSJames Smart * cycles extracting it from ERSPs and stamping it in the
1912e399441dSJames Smart * cases where the transport fabricates CQEs on successful
1913e399441dSJames Smart * completions.
1914e399441dSJames Smart * 2) The FC-NVME implementation requires that delivery of
1915e399441dSJames Smart * ERSP completions are to go back to the nvme layer in order
1916e399441dSJames Smart * relative to the rsn, such that the sqhd value will always
1917e399441dSJames Smart * be "in order" for the nvme layer. As the nvme layer in
1918e399441dSJames Smart * linux doesn't care about sqhd, there's no need to return
1919e399441dSJames Smart * them in order.
1920e399441dSJames Smart *
1921e399441dSJames Smart * Additionally:
1922e399441dSJames Smart * As the core nvme layer in linux currently does not look at
1923e399441dSJames Smart * every field in the cqe - in cases where the FC transport must
1924e399441dSJames Smart * fabricate a CQE, the following fields will not be set as they
1925e399441dSJames Smart * are not referenced:
1926e399441dSJames Smart * cqe.sqid, cqe.sqhd, cqe.command_id
1927f874d5d0SJames Smart *
1928f874d5d0SJames Smart * Failure or error of an individual i/o, in a transport
1929f874d5d0SJames Smart * detected fashion unrelated to the nvme completion status,
1930f874d5d0SJames Smart * potentially cause the initiator and target sides to get out
1931f874d5d0SJames Smart * of sync on SQ head/tail (aka outstanding io count allowed).
1932f874d5d0SJames Smart * Per FC-NVME spec, failure of an individual command requires
1933f874d5d0SJames Smart * the connection to be terminated, which in turn requires the
1934f874d5d0SJames Smart * association to be terminated.
1935e399441dSJames Smart */
1936e399441dSJames Smart
19373efd6e8eSJames Smart opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
19383efd6e8eSJames Smart
1939e399441dSJames Smart fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1940e399441dSJames Smart sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1941e399441dSJames Smart
19423efd6e8eSJames Smart if (opstate == FCPOP_STATE_ABORTED)
1943ae3afe63SHannes Reinecke status = cpu_to_le16(NVME_SC_HOST_ABORTED_CMD << 1);
194474bd8cbeSJames Smart else if (freq->status) {
194574bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
194674bd8cbeSJames Smart dev_info(ctrl->ctrl.device,
194774bd8cbeSJames Smart "NVME-FC{%d}: io failed due to lldd error %d\n",
194874bd8cbeSJames Smart ctrl->cnum, freq->status);
194974bd8cbeSJames Smart }
1950e399441dSJames Smart
1951e399441dSJames Smart /*
1952e399441dSJames Smart * For the linux implementation, if we have an unsuccesful
1953e399441dSJames Smart * status, they blk-mq layer can typically be called with the
1954e399441dSJames Smart * non-zero status and the content of the cqe isn't important.
1955e399441dSJames Smart */
1956e399441dSJames Smart if (status)
1957e399441dSJames Smart goto done;
1958e399441dSJames Smart
1959e399441dSJames Smart /*
1960e399441dSJames Smart * command completed successfully relative to the wire
1961e399441dSJames Smart * protocol. However, validate anything received and
1962e399441dSJames Smart * extract the status and result from the cqe (create it
1963e399441dSJames Smart * where necessary).
1964e399441dSJames Smart */
1965e399441dSJames Smart
1966e399441dSJames Smart switch (freq->rcv_rsplen) {
1967e399441dSJames Smart
1968e399441dSJames Smart case 0:
1969e399441dSJames Smart case NVME_FC_SIZEOF_ZEROS_RSP:
1970e399441dSJames Smart /*
1971e399441dSJames Smart * No response payload or 12 bytes of payload (which
1972e399441dSJames Smart * should all be zeros) are considered successful and
1973e399441dSJames Smart * no payload in the CQE by the transport.
1974e399441dSJames Smart */
1975e399441dSJames Smart if (freq->transferred_length !=
1976e399441dSJames Smart be32_to_cpu(op->cmd_iu.data_len)) {
197774bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
197874bd8cbeSJames Smart dev_info(ctrl->ctrl.device,
197974bd8cbeSJames Smart "NVME-FC{%d}: io failed due to bad transfer "
198074bd8cbeSJames Smart "length: %d vs expected %d\n",
198174bd8cbeSJames Smart ctrl->cnum, freq->transferred_length,
198274bd8cbeSJames Smart be32_to_cpu(op->cmd_iu.data_len));
1983e399441dSJames Smart goto done;
1984e399441dSJames Smart }
198527fa9bc5SChristoph Hellwig result.u64 = 0;
1986e399441dSJames Smart break;
1987e399441dSJames Smart
1988e399441dSJames Smart case sizeof(struct nvme_fc_ersp_iu):
1989e399441dSJames Smart /*
1990e399441dSJames Smart * The ERSP IU contains a full completion with CQE.
1991e399441dSJames Smart * Validate ERSP IU and look at cqe.
1992e399441dSJames Smart */
1993e399441dSJames Smart if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1994e399441dSJames Smart (freq->rcv_rsplen / 4) ||
1995e399441dSJames Smart be32_to_cpu(op->rsp_iu.xfrd_len) !=
1996e399441dSJames Smart freq->transferred_length ||
199753b2b2f5SJames Smart op->rsp_iu.ersp_result ||
1998458f280dSJames Smart sqe->common.command_id != cqe->command_id)) {
199974bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
200074bd8cbeSJames Smart dev_info(ctrl->ctrl.device,
200174bd8cbeSJames Smart "NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
200274bd8cbeSJames Smart "iu len %d, xfr len %d vs %d, status code "
200374bd8cbeSJames Smart "%d, cmdid %d vs %d\n",
200474bd8cbeSJames Smart ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len),
200574bd8cbeSJames Smart be32_to_cpu(op->rsp_iu.xfrd_len),
200674bd8cbeSJames Smart freq->transferred_length,
200753b2b2f5SJames Smart op->rsp_iu.ersp_result,
200874bd8cbeSJames Smart sqe->common.command_id,
200974bd8cbeSJames Smart cqe->command_id);
2010e399441dSJames Smart goto done;
2011e399441dSJames Smart }
201227fa9bc5SChristoph Hellwig result = cqe->result;
2013d663b69fSChristoph Hellwig status = cqe->status;
2014e399441dSJames Smart break;
2015e399441dSJames Smart
2016e399441dSJames Smart default:
201774bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
201874bd8cbeSJames Smart dev_info(ctrl->ctrl.device,
201974bd8cbeSJames Smart "NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
202074bd8cbeSJames Smart "len %d\n",
202174bd8cbeSJames Smart ctrl->cnum, freq->rcv_rsplen);
2022e399441dSJames Smart goto done;
2023e399441dSJames Smart }
2024e399441dSJames Smart
2025f874d5d0SJames Smart terminate_assoc = false;
2026f874d5d0SJames Smart
2027e399441dSJames Smart done:
202878a7ac26SJames Smart if (op->flags & FCOP_FLAGS_AEN) {
202927fa9bc5SChristoph Hellwig nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
20303efd6e8eSJames Smart __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
203178a7ac26SJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE);
203278a7ac26SJames Smart op->flags = FCOP_FLAGS_AEN; /* clear other flags */
2033e399441dSJames Smart nvme_fc_ctrl_put(ctrl);
2034f874d5d0SJames Smart goto check_error;
2035e399441dSJames Smart }
2036e399441dSJames Smart
2037c3aedd22SJames Smart __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
20382eb81a33SChristoph Hellwig if (!nvme_try_complete_req(rq, status, result))
2039ff029451SChristoph Hellwig nvme_fc_complete_rq(rq);
2040f874d5d0SJames Smart
2041f874d5d0SJames Smart check_error:
204240a29e84SDaniel Wagner if (terminate_assoc &&
204340a29e84SDaniel Wagner nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_RESETTING)
204419fce047SJames Smart queue_work(nvme_reset_wq, &ctrl->ioerr_work);
2045e399441dSJames Smart }
2046e399441dSJames Smart
2047e399441dSJames Smart static int
2048e399441dSJames Smart __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
2049e399441dSJames Smart struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
2050e399441dSJames Smart struct request *rq, u32 rqno)
2051e399441dSJames Smart {
2052d3d0bc78SBart Van Assche struct nvme_fcp_op_w_sgl *op_w_sgl =
2053d3d0bc78SBart Van Assche container_of(op, typeof(*op_w_sgl), op);
2054e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2055e399441dSJames Smart int ret = 0;
2056e399441dSJames Smart
2057e399441dSJames Smart memset(op, 0, sizeof(*op));
2058e399441dSJames Smart op->fcp_req.cmdaddr = &op->cmd_iu;
2059e399441dSJames Smart op->fcp_req.cmdlen = sizeof(op->cmd_iu);
2060e399441dSJames Smart op->fcp_req.rspaddr = &op->rsp_iu;
2061e399441dSJames Smart op->fcp_req.rsplen = sizeof(op->rsp_iu);
2062e399441dSJames Smart op->fcp_req.done = nvme_fc_fcpio_done;
2063e399441dSJames Smart op->ctrl = ctrl;
2064e399441dSJames Smart op->queue = queue;
2065e399441dSJames Smart op->rq = rq;
2066e399441dSJames Smart op->rqno = rqno;
2067e399441dSJames Smart
206853b2b2f5SJames Smart cmdiu->format_id = NVME_CMD_FORMAT_ID;
2069e399441dSJames Smart cmdiu->fc_id = NVME_CMD_FC_ID;
2070e399441dSJames Smart cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
207144fbf3bbSJames Smart if (queue->qnum)
207244fbf3bbSJames Smart cmdiu->rsv_cat = fccmnd_set_cat_css(0,
207344fbf3bbSJames Smart (NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT));
207444fbf3bbSJames Smart else
207544fbf3bbSJames Smart cmdiu->rsv_cat = fccmnd_set_cat_admin(0);
2076e399441dSJames Smart
2077e399441dSJames Smart op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
2078e399441dSJames Smart &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
2079e399441dSJames Smart if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
2080e399441dSJames Smart dev_err(ctrl->dev,
2081e399441dSJames Smart "FCP Op failed - cmdiu dma mapping failed.\n");
2082f34448cdSTianjia Zhang ret = -EFAULT;
2083e399441dSJames Smart goto out_on_error;
2084e399441dSJames Smart }
2085e399441dSJames Smart
2086e399441dSJames Smart op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
2087e399441dSJames Smart &op->rsp_iu, sizeof(op->rsp_iu),
2088e399441dSJames Smart DMA_FROM_DEVICE);
__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)2089e399441dSJames Smart if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
2090e399441dSJames Smart dev_err(ctrl->dev,
2091e399441dSJames Smart "FCP Op failed - rspiu dma mapping failed.\n");
2092f34448cdSTianjia Zhang ret = -EFAULT;
2093e399441dSJames Smart }
2094e399441dSJames Smart
2095e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE);
2096e399441dSJames Smart out_on_error:
2097e399441dSJames Smart return ret;
2098e399441dSJames Smart }
2099e399441dSJames Smart
2100e399441dSJames Smart static int
2101d6296d39SChristoph Hellwig nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
2102d6296d39SChristoph Hellwig unsigned int hctx_idx, unsigned int numa_node)
2103e399441dSJames Smart {
21041864ea46SChristoph Hellwig struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
2105d3d0bc78SBart Van Assche struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq);
210676f983cbSChristoph Hellwig int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
210776f983cbSChristoph Hellwig struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
21080d2bdf9fSBart Van Assche int res;
2109e399441dSJames Smart
21100d2bdf9fSBart Van Assche res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++);
21110d2bdf9fSBart Van Assche if (res)
21120d2bdf9fSBart Van Assche return res;
21133add1d93SArnd Bergmann op->op.fcp_req.first_sgl = op->sgl;
2114d19b8bc8SJames Smart op->op.fcp_req.private = &op->priv[0];
2115dfa74422SEwan D. Milne nvme_req(rq)->ctrl = &ctrl->ctrl;
2116f4b9e6c9SKeith Busch nvme_req(rq)->cmd = &op->op.cmd_iu.sqe;
21170d2bdf9fSBart Van Assche return res;
2118e399441dSJames Smart }
2119e399441dSJames Smart
2120e399441dSJames Smart static int
2121e399441dSJames Smart nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
2122e399441dSJames Smart {
2123e399441dSJames Smart struct nvme_fc_fcp_op *aen_op;
2124e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu;
2125e399441dSJames Smart struct nvme_command *sqe;
2126f56bf76fSJames Smart void *private = NULL;
2127e399441dSJames Smart int i, ret;
2128e399441dSJames Smart
2129e399441dSJames Smart aen_op = ctrl->aen_ops;
213038dabe21SKeith Busch for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2131f56bf76fSJames Smart if (ctrl->lport->ops->fcprqst_priv_sz) {
213261bff8efSJames Smart private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
213361bff8efSJames Smart GFP_KERNEL);
213461bff8efSJames Smart if (!private)
213561bff8efSJames Smart return -ENOMEM;
2136f56bf76fSJames Smart }
213761bff8efSJames Smart
2138e399441dSJames Smart cmdiu = &aen_op->cmd_iu;
2139e399441dSJames Smart sqe = &cmdiu->sqe;
2140e399441dSJames Smart ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
2141e399441dSJames Smart aen_op, (struct request *)NULL,
nvme_fc_init_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx,unsigned int numa_node)214238dabe21SKeith Busch (NVME_AQ_BLK_MQ_DEPTH + i));
214361bff8efSJames Smart if (ret) {
214461bff8efSJames Smart kfree(private);
2145e399441dSJames Smart return ret;
214661bff8efSJames Smart }
2147e399441dSJames Smart
214878a7ac26SJames Smart aen_op->flags = FCOP_FLAGS_AEN;
214961bff8efSJames Smart aen_op->fcp_req.private = private;
215078a7ac26SJames Smart
2151e399441dSJames Smart memset(sqe, 0, sizeof(*sqe));
2152e399441dSJames Smart sqe->common.opcode = nvme_admin_async_event;
215378a7ac26SJames Smart /* Note: core layer may overwrite the sqe.command_id value */
215438dabe21SKeith Busch sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
2155e399441dSJames Smart }
2156e399441dSJames Smart return 0;
2157e399441dSJames Smart }
2158e399441dSJames Smart
215961bff8efSJames Smart static void
216061bff8efSJames Smart nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
216161bff8efSJames Smart {
nvme_fc_init_aen_ops(struct nvme_fc_ctrl * ctrl)216261bff8efSJames Smart struct nvme_fc_fcp_op *aen_op;
216361bff8efSJames Smart int i;
216461bff8efSJames Smart
2165e126e821SDavid Milburn cancel_work_sync(&ctrl->ctrl.async_event_work);
216661bff8efSJames Smart aen_op = ctrl->aen_ops;
216738dabe21SKeith Busch for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
216861bff8efSJames Smart __nvme_fc_exit_request(ctrl, aen_op);
216961bff8efSJames Smart
217061bff8efSJames Smart kfree(aen_op->fcp_req.private);
217161bff8efSJames Smart aen_op->fcp_req.private = NULL;
217261bff8efSJames Smart }
217361bff8efSJames Smart }
2174e399441dSJames Smart
21751864ea46SChristoph Hellwig static inline int
21761864ea46SChristoph Hellwig __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int qidx)
2177e399441dSJames Smart {
21781864ea46SChristoph Hellwig struct nvme_fc_ctrl *ctrl = to_fc_ctrl(data);
2179e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[qidx];
2180e399441dSJames Smart
2181e399441dSJames Smart hctx->driver_data = queue;
2182e399441dSJames Smart queue->hctx = hctx;
21831864ea46SChristoph Hellwig return 0;
2184e399441dSJames Smart }
2185e399441dSJames Smart
2186e399441dSJames Smart static int
21871864ea46SChristoph Hellwig nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int hctx_idx)
2188e399441dSJames Smart {
21891864ea46SChristoph Hellwig return __nvme_fc_init_hctx(hctx, data, hctx_idx + 1);
2190e399441dSJames Smart }
2191e399441dSJames Smart
2192e399441dSJames Smart static int
2193e399441dSJames Smart nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2194e399441dSJames Smart unsigned int hctx_idx)
2195e399441dSJames Smart {
21961864ea46SChristoph Hellwig return __nvme_fc_init_hctx(hctx, data, hctx_idx);
2197e399441dSJames Smart }
2198e399441dSJames Smart
2199e399441dSJames Smart static void
220008e15075SKeith Busch nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
nvme_fc_term_aen_ops(struct nvme_fc_ctrl * ctrl)2201e399441dSJames Smart {
2202e399441dSJames Smart struct nvme_fc_queue *queue;
2203e399441dSJames Smart
2204e399441dSJames Smart queue = &ctrl->queues[idx];
2205e399441dSJames Smart memset(queue, 0, sizeof(*queue));
2206e399441dSJames Smart queue->ctrl = ctrl;
2207e399441dSJames Smart queue->qnum = idx;
220867f471b6SJames Smart atomic_set(&queue->csn, 0);
2209e399441dSJames Smart queue->dev = ctrl->dev;
2210e399441dSJames Smart
2211e399441dSJames Smart if (idx > 0)
2212e399441dSJames Smart queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
2213e399441dSJames Smart else
2214e399441dSJames Smart queue->cmnd_capsule_len = sizeof(struct nvme_command);
2215e399441dSJames Smart
2216e399441dSJames Smart /*
2217e399441dSJames Smart * Considered whether we should allocate buffers for all SQEs
2218e399441dSJames Smart * and CQEs and dma map them - mapping their respective entries
2219e399441dSJames Smart * into the request structures (kernel vm addr and dma address)
2220e399441dSJames Smart * thus the driver could use the buffers/mappings directly.
2221e399441dSJames Smart * It only makes sense if the LLDD would use them for its
2222e399441dSJames Smart * messaging api. It's very unlikely most adapter api's would use
2223e399441dSJames Smart * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
2224e399441dSJames Smart * structures were used instead.
2225e399441dSJames Smart */
2226e399441dSJames Smart }
2227e399441dSJames Smart
nvme_fc_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)2228e399441dSJames Smart /*
2229e399441dSJames Smart * This routine terminates a queue at the transport level.
2230e399441dSJames Smart * The transport has already ensured that all outstanding ios on
2231e399441dSJames Smart * the queue have been terminated.
2232e399441dSJames Smart * The transport will send a Disconnect LS request to terminate
2233e399441dSJames Smart * the queue's connection. Termination of the admin queue will also
2234e399441dSJames Smart * terminate the association at the target.
2235e399441dSJames Smart */
2236e399441dSJames Smart static void
2237e399441dSJames Smart nvme_fc_free_queue(struct nvme_fc_queue *queue)
2238e399441dSJames Smart {
2239e399441dSJames Smart if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
2240e399441dSJames Smart return;
2241e399441dSJames Smart
22429e0ed16aSSagi Grimberg clear_bit(NVME_FC_Q_LIVE, &queue->flags);
2243e399441dSJames Smart /*
2244e399441dSJames Smart * Current implementation never disconnects a single queue.
2245e399441dSJames Smart * It always terminates a whole association. So there is never
2246e399441dSJames Smart * a disconnect(queue) LS sent to the target.
2247e399441dSJames Smart */
2248e399441dSJames Smart
2249e399441dSJames Smart queue->connection_id = 0;
225067f471b6SJames Smart atomic_set(&queue->csn, 0);
2251e399441dSJames Smart }
2252e399441dSJames Smart
2253e399441dSJames Smart static void
2254e399441dSJames Smart __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
2255e399441dSJames Smart struct nvme_fc_queue *queue, unsigned int qidx)
2256e399441dSJames Smart {
2257e399441dSJames Smart if (ctrl->lport->ops->delete_queue)
2258e399441dSJames Smart ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
2259e399441dSJames Smart queue->lldd_handle);
2260e399441dSJames Smart queue->lldd_handle = NULL;
2261e399441dSJames Smart }
2262e399441dSJames Smart
2263e399441dSJames Smart static void
2264e399441dSJames Smart nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
2265e399441dSJames Smart {
2266e399441dSJames Smart int i;
2267e399441dSJames Smart
2268d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++)
2269e399441dSJames Smart nvme_fc_free_queue(&ctrl->queues[i]);
2270e399441dSJames Smart }
2271e399441dSJames Smart
2272e399441dSJames Smart static int
2273e399441dSJames Smart __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
2274e399441dSJames Smart struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
2275e399441dSJames Smart {
2276e399441dSJames Smart int ret = 0;
2277e399441dSJames Smart
nvme_fc_free_queue(struct nvme_fc_queue * queue)2278e399441dSJames Smart queue->lldd_handle = NULL;
2279e399441dSJames Smart if (ctrl->lport->ops->create_queue)
2280e399441dSJames Smart ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
2281e399441dSJames Smart qidx, qsize, &queue->lldd_handle);
2282e399441dSJames Smart
2283e399441dSJames Smart return ret;
2284e399441dSJames Smart }
2285e399441dSJames Smart
2286e399441dSJames Smart static void
2287e399441dSJames Smart nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
2288e399441dSJames Smart {
2289d858e5f0SSagi Grimberg struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
2290e399441dSJames Smart int i;
2291e399441dSJames Smart
2292d858e5f0SSagi Grimberg for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
2293e399441dSJames Smart __nvme_fc_delete_hw_queue(ctrl, queue, i);
2294e399441dSJames Smart }
__nvme_fc_delete_hw_queue(struct nvme_fc_ctrl * ctrl,struct nvme_fc_queue * queue,unsigned int qidx)2295e399441dSJames Smart
2296e399441dSJames Smart static int
2297e399441dSJames Smart nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2298e399441dSJames Smart {
2299e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[1];
230017a1ec08SJohannes Thumshirn int i, ret;
2301e399441dSJames Smart
2302d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
2303e399441dSJames Smart ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
230417a1ec08SJohannes Thumshirn if (ret)
230517a1ec08SJohannes Thumshirn goto delete_queues;
2306e399441dSJames Smart }
2307e399441dSJames Smart
2308e399441dSJames Smart return 0;
230917a1ec08SJohannes Thumshirn
231017a1ec08SJohannes Thumshirn delete_queues:
2311514a6dc9SJames Smart for (; i > 0; i--)
231217a1ec08SJohannes Thumshirn __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
231317a1ec08SJohannes Thumshirn return ret;
2314e399441dSJames Smart }
2315e399441dSJames Smart
2316e399441dSJames Smart static int
2317e399441dSJames Smart nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2318e399441dSJames Smart {
2319e399441dSJames Smart int i, ret = 0;
2320e399441dSJames Smart
2321d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++) {
2322e399441dSJames Smart ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
2323e399441dSJames Smart (qsize / 5));
2324e399441dSJames Smart if (ret)
2325e399441dSJames Smart break;
2326be42a33bSKeith Busch ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
2327e399441dSJames Smart if (ret)
nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl * ctrl)2328e399441dSJames Smart break;
23299e0ed16aSSagi Grimberg
23309e0ed16aSSagi Grimberg set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
2331e399441dSJames Smart }
2332e399441dSJames Smart
2333e399441dSJames Smart return ret;
2334e399441dSJames Smart }
2335e399441dSJames Smart
2336e399441dSJames Smart static void
2337e399441dSJames Smart nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl * ctrl,u16 qsize)2338e399441dSJames Smart {
2339e399441dSJames Smart int i;
2340e399441dSJames Smart
2341d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++)
234208e15075SKeith Busch nvme_fc_init_queue(ctrl, i);
2343e399441dSJames Smart }
2344e399441dSJames Smart
2345e399441dSJames Smart static void
2346e399441dSJames Smart nvme_fc_ctrl_free(struct kref *ref)
2347e399441dSJames Smart {
2348e399441dSJames Smart struct nvme_fc_ctrl *ctrl =
2349e399441dSJames Smart container_of(ref, struct nvme_fc_ctrl, ref);
2350e399441dSJames Smart unsigned long flags;
2351e399441dSJames Smart
23526dfba1c0SChristoph Hellwig if (ctrl->ctrl.tagset)
23536dfba1c0SChristoph Hellwig nvme_remove_io_tag_set(&ctrl->ctrl);
235461bff8efSJames Smart
2355e399441dSJames Smart /* remove from rport list */
2356e399441dSJames Smart spin_lock_irqsave(&ctrl->rport->lock, flags);
2357e399441dSJames Smart list_del(&ctrl->ctrl_list);
nvme_fc_connect_io_queues(struct nvme_fc_ctrl * ctrl,u16 qsize)2358e399441dSJames Smart spin_unlock_irqrestore(&ctrl->rport->lock, flags);
235961bff8efSJames Smart
23609f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(&ctrl->ctrl);
23616dfba1c0SChristoph Hellwig nvme_remove_admin_tag_set(&ctrl->ctrl);
236261bff8efSJames Smart
236361bff8efSJames Smart kfree(ctrl->queues);
2364e399441dSJames Smart
2365e399441dSJames Smart put_device(ctrl->dev);
2366e399441dSJames Smart nvme_fc_rport_put(ctrl->rport);
2367e399441dSJames Smart
23683dd83f40SSagi Grimberg ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
2369de41447aSEwan D. Milne if (ctrl->ctrl.opts)
2370e399441dSJames Smart nvmf_free_options(ctrl->ctrl.opts);
2371e399441dSJames Smart kfree(ctrl);
2372e399441dSJames Smart }
2373e399441dSJames Smart
2374e399441dSJames Smart static void
2375e399441dSJames Smart nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2376e399441dSJames Smart {
2377e399441dSJames Smart kref_put(&ctrl->ref, nvme_fc_ctrl_free);
nvme_fc_init_io_queues(struct nvme_fc_ctrl * ctrl)2378e399441dSJames Smart }
2379e399441dSJames Smart
2380e399441dSJames Smart static int
2381e399441dSJames Smart nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2382e399441dSJames Smart {
2383e399441dSJames Smart return kref_get_unless_zero(&ctrl->ref);
2384e399441dSJames Smart }
2385e399441dSJames Smart
2386e399441dSJames Smart /*
nvme_fc_ctrl_free(struct kref * ref)2387e399441dSJames Smart * All accesses from nvme core layer done - can now free the
2388e399441dSJames Smart * controller. Called after last nvme_put_ctrl() call
2389e399441dSJames Smart */
2390e399441dSJames Smart static void
239161bff8efSJames Smart nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
2392e399441dSJames Smart {
2393e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2394e399441dSJames Smart
2395e399441dSJames Smart WARN_ON(nctrl != &ctrl->ctrl);
2396e399441dSJames Smart
2397e399441dSJames Smart nvme_fc_ctrl_put(ctrl);
2398e399441dSJames Smart }
2399e399441dSJames Smart
240095ced8a2SJames Smart /*
240195ced8a2SJames Smart * This routine is used by the transport when it needs to find active
240295ced8a2SJames Smart * io on a queue that is to be terminated. The transport uses
240395ced8a2SJames Smart * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
240495ced8a2SJames Smart * this routine to kill them on a 1 by 1 basis.
240595ced8a2SJames Smart *
240695ced8a2SJames Smart * As FC allocates FC exchange for each io, the transport must contact
240795ced8a2SJames Smart * the LLDD to terminate the exchange, thus releasing the FC exchange.
240895ced8a2SJames Smart * After terminating the exchange the LLDD will call the transport's
240995ced8a2SJames Smart * normal io done path for the request, but it will have an aborted
241095ced8a2SJames Smart * status. The done path will return the io request back to the block
241195ced8a2SJames Smart * layer with an error status.
241295ced8a2SJames Smart */
24132dd6532eSJohn Garry static bool nvme_fc_terminate_exchange(struct request *req, void *data)
241495ced8a2SJames Smart {
241595ced8a2SJames Smart struct nvme_ctrl *nctrl = data;
nvme_fc_ctrl_put(struct nvme_fc_ctrl * ctrl)241695ced8a2SJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
241795ced8a2SJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
241895ced8a2SJames Smart
24193c7aafbcSHannes Reinecke op->nreq.flags |= NVME_REQ_CANCELLED;
242095ced8a2SJames Smart __nvme_fc_abort_op(ctrl, op);
242195ced8a2SJames Smart return true;
nvme_fc_ctrl_get(struct nvme_fc_ctrl * ctrl)242295ced8a2SJames Smart }
242395ced8a2SJames Smart
242495ced8a2SJames Smart /*
242595ced8a2SJames Smart * This routine runs through all outstanding commands on the association
242695ced8a2SJames Smart * and aborts them. This routine is typically be called by the
242795ced8a2SJames Smart * delete_association routine. It is also called due to an error during
242895ced8a2SJames Smart * reconnect. In that scenario, it is most likely a command that initializes
242995ced8a2SJames Smart * the controller, including fabric Connect commands on io queues, that
243095ced8a2SJames Smart * may have timed out or failed thus the io must be killed for the connect
243195ced8a2SJames Smart * thread to see the error.
243295ced8a2SJames Smart */
243395ced8a2SJames Smart static void
243495ced8a2SJames Smart __nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl *ctrl, bool start_queues)
243595ced8a2SJames Smart {
2436a7d13914SJames Smart int q;
2437a7d13914SJames Smart
2438a7d13914SJames Smart /*
2439a7d13914SJames Smart * if aborting io, the queues are no longer good, mark them
2440a7d13914SJames Smart * all as not live.
2441a7d13914SJames Smart */
2442a7d13914SJames Smart if (ctrl->ctrl.queue_count > 1) {
2443a7d13914SJames Smart for (q = 1; q < ctrl->ctrl.queue_count; q++)
2444a7d13914SJames Smart clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[q].flags);
2445a7d13914SJames Smart }
2446a7d13914SJames Smart clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
2447a7d13914SJames Smart
244895ced8a2SJames Smart /*
244995ced8a2SJames Smart * If io queues are present, stop them and terminate all outstanding
245095ced8a2SJames Smart * ios on them. As FC allocates FC exchange for each io, the
245195ced8a2SJames Smart * transport must contact the LLDD to terminate the exchange,
245295ced8a2SJames Smart * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
245395ced8a2SJames Smart * to tell us what io's are busy and invoke a transport routine
245495ced8a2SJames Smart * to kill them with the LLDD. After terminating the exchange
245595ced8a2SJames Smart * the LLDD will call the transport's normal io done path, but it
245695ced8a2SJames Smart * will have an aborted status. The done path will return the
245795ced8a2SJames Smart * io requests back to the block layer as part of normal completions
245895ced8a2SJames Smart * (but with error status).
245995ced8a2SJames Smart */
246095ced8a2SJames Smart if (ctrl->ctrl.queue_count > 1) {
24619f27bd70SChristoph Hellwig nvme_quiesce_io_queues(&ctrl->ctrl);
2462e5445daeSJames Smart nvme_sync_io_queues(&ctrl->ctrl);
246395ced8a2SJames Smart blk_mq_tagset_busy_iter(&ctrl->tag_set,
246495ced8a2SJames Smart nvme_fc_terminate_exchange, &ctrl->ctrl);
246595ced8a2SJames Smart blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
246695ced8a2SJames Smart if (start_queues)
24679f27bd70SChristoph Hellwig nvme_unquiesce_io_queues(&ctrl->ctrl);
246895ced8a2SJames Smart }
246995ced8a2SJames Smart
247095ced8a2SJames Smart /*
247195ced8a2SJames Smart * Other transports, which don't have link-level contexts bound
247295ced8a2SJames Smart * to sqe's, would try to gracefully shutdown the controller by
247395ced8a2SJames Smart * writing the registers for shutdown and polling (call
2474285b6e9bSChristoph Hellwig * nvme_disable_ctrl()). Given a bunch of i/o was potentially
247595ced8a2SJames Smart * just aborted and we will wait on those contexts, and given
247695ced8a2SJames Smart * there was no indication of how live the controlelr is on the
247795ced8a2SJames Smart * link, don't send more io to create more contexts for the
247895ced8a2SJames Smart * shutdown. Let the controller fail via keepalive failure if
247995ced8a2SJames Smart * its still present.
248095ced8a2SJames Smart */
248195ced8a2SJames Smart
248295ced8a2SJames Smart /*
248395ced8a2SJames Smart * clean up the admin queue. Same thing as above.
248495ced8a2SJames Smart */
24859f27bd70SChristoph Hellwig nvme_quiesce_admin_queue(&ctrl->ctrl);
2486e5445daeSJames Smart blk_sync_queue(ctrl->ctrl.admin_q);
248795ced8a2SJames Smart blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
248895ced8a2SJames Smart nvme_fc_terminate_exchange, &ctrl->ctrl);
248995ced8a2SJames Smart blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
24906fb271f1SMing Lei if (start_queues)
24919f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(&ctrl->ctrl);
249295ced8a2SJames Smart }
24939c2bb257SJames Smart
249461bff8efSJames Smart static void
249561bff8efSJames Smart nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
249661bff8efSJames Smart {
249740a29e84SDaniel Wagner enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl);
249840a29e84SDaniel Wagner
24994cff280aSJames Smart /*
250095ced8a2SJames Smart * if an error (io timeout, etc) while (re)connecting, the remote
250195ced8a2SJames Smart * port requested terminating of the association (disconnect_ls)
250295ced8a2SJames Smart * or an error (timeout or abort) occurred on an io while creating
250395ced8a2SJames Smart * the controller. Abort any ios on the association and let the
250495ced8a2SJames Smart * create_association error path resolve things.
25054cff280aSJames Smart */
250640a29e84SDaniel Wagner if (state == NVME_CTRL_CONNECTING) {
2507ee6fdc50SMichael Liang __nvme_fc_abort_outstanding_ios(ctrl, true);
250860e445bdSMichael Liang dev_warn(ctrl->ctrl.device,
250960e445bdSMichael Liang "NVME-FC{%d}: transport error during (re)connect\n",
251060e445bdSMichael Liang ctrl->cnum);
25114cff280aSJames Smart return;
25124cff280aSJames Smart }
25134cff280aSJames Smart
25144cff280aSJames Smart /* Otherwise, only proceed if in LIVE state - e.g. on first error */
251540a29e84SDaniel Wagner if (state != NVME_CTRL_LIVE)
251669fa9646SJames Smart return;
251769fa9646SJames Smart
251861bff8efSJames Smart dev_warn(ctrl->ctrl.device,
2519514a6dc9SJames Smart "NVME-FC{%d}: transport association event: %s\n",
252061bff8efSJames Smart ctrl->cnum, errmsg);
2521589ff775SJames Smart dev_warn(ctrl->ctrl.device,
252261bff8efSJames Smart "NVME-FC{%d}: resetting controller\n", ctrl->cnum);
252361bff8efSJames Smart
2524d86c4d8eSChristoph Hellwig nvme_reset_ctrl(&ctrl->ctrl);
252561bff8efSJames Smart }
252661bff8efSJames Smart
25279bdb4833SJohn Garry static enum blk_eh_timer_return nvme_fc_timeout(struct request *rq)
2528e399441dSJames Smart {
2529e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2530e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl;
253152793d62SJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
253252793d62SJames Smart struct nvme_command *sqe = &cmdiu->sqe;
2533e399441dSJames Smart
2534e399441dSJames Smart /*
253552793d62SJames Smart * Attempt to abort the offending command. Command completion
nvme_fc_error_recovery(struct nvme_fc_ctrl * ctrl,char * errmsg)253652793d62SJames Smart * will detect the aborted io and will fail the connection.
2537e399441dSJames Smart */
253852793d62SJames Smart dev_info(ctrl->ctrl.device,
253952793d62SJames Smart "NVME-FC{%d.%d}: io timeout: opcode %d fctype %d w10/11: "
254052793d62SJames Smart "x%08x/x%08x\n",
254152793d62SJames Smart ctrl->cnum, op->queue->qnum, sqe->common.opcode,
254252793d62SJames Smart sqe->connect.fctype, sqe->common.cdw10, sqe->common.cdw11);
254352793d62SJames Smart if (__nvme_fc_abort_op(ctrl, op))
254452793d62SJames Smart nvme_fc_error_recovery(ctrl, "io timeout abort failed");
2545e399441dSJames Smart
2546134aedc9SJames Smart /*
2547134aedc9SJames Smart * the io abort has been initiated. Have the reset timer
2548134aedc9SJames Smart * restarted and the abort completion will complete the io
2549134aedc9SJames Smart * shortly. Avoids a synchronous wait while the abort finishes.
2550134aedc9SJames Smart */
2551134aedc9SJames Smart return BLK_EH_RESET_TIMER;
2552e399441dSJames Smart }
2553e399441dSJames Smart
2554e399441dSJames Smart static int
2555e399441dSJames Smart nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2556e399441dSJames Smart struct nvme_fc_fcp_op *op)
2557e399441dSJames Smart {
2558e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req;
2559e399441dSJames Smart int ret;
2560e399441dSJames Smart
2561e399441dSJames Smart freq->sg_cnt = 0;
2562e399441dSJames Smart
25639f7d8ae2SJames Smart if (!blk_rq_nr_phys_segments(rq))
2564e399441dSJames Smart return 0;
2565e399441dSJames Smart
2566e399441dSJames Smart freq->sg_table.sgl = freq->first_sgl;
256719e420bbSChristoph Hellwig ret = sg_alloc_table_chained(&freq->sg_table,
25684635873cSMing Lei blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
nvme_fc_timeout(struct request * rq)2569b1ae1a23SIsrael Rukshin NVME_INLINE_SG_CNT);
2570e399441dSJames Smart if (ret)
2571e399441dSJames Smart return -ENOMEM;
2572e399441dSJames Smart
2573e399441dSJames Smart op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
257419e420bbSChristoph Hellwig WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2575e399441dSJames Smart freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2576f15872c5SIsrael Rukshin op->nents, rq_dma_dir(rq));
2577e399441dSJames Smart if (unlikely(freq->sg_cnt <= 0)) {
2578b1ae1a23SIsrael Rukshin sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2579e399441dSJames Smart freq->sg_cnt = 0;
2580e399441dSJames Smart return -EFAULT;
2581e399441dSJames Smart }
2582e399441dSJames Smart
2583e399441dSJames Smart /*
2584e399441dSJames Smart * TODO: blk_integrity_rq(rq) for DIF
2585e399441dSJames Smart */
2586e399441dSJames Smart return 0;
2587e399441dSJames Smart }
2588e399441dSJames Smart
2589e399441dSJames Smart static void
2590e399441dSJames Smart nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2591e399441dSJames Smart struct nvme_fc_fcp_op *op)
2592e399441dSJames Smart {
2593e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req;
2594e399441dSJames Smart
2595e399441dSJames Smart if (!freq->sg_cnt)
2596e399441dSJames Smart return;
nvme_fc_map_data(struct nvme_fc_ctrl * ctrl,struct request * rq,struct nvme_fc_fcp_op * op)2597e399441dSJames Smart
2598e399441dSJames Smart fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2599f15872c5SIsrael Rukshin rq_dma_dir(rq));
2600e399441dSJames Smart
2601b1ae1a23SIsrael Rukshin sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2602e399441dSJames Smart
2603e399441dSJames Smart freq->sg_cnt = 0;
2604e399441dSJames Smart }
2605e399441dSJames Smart
2606e399441dSJames Smart /*
2607e399441dSJames Smart * In FC, the queue is a logical thing. At transport connect, the target
2608e399441dSJames Smart * creates its "queue" and returns a handle that is to be given to the
2609e399441dSJames Smart * target whenever it posts something to the corresponding SQ. When an
2610e399441dSJames Smart * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2611e399441dSJames Smart * command contained within the SQE, an io, and assigns a FC exchange
2612e399441dSJames Smart * to it. The SQE and the associated SQ handle are sent in the initial
2613e399441dSJames Smart * CMD IU sents on the exchange. All transfers relative to the io occur
2614e399441dSJames Smart * as part of the exchange. The CQE is the last thing for the io,
2615e399441dSJames Smart * which is transferred (explicitly or implicitly) with the RSP IU
2616e399441dSJames Smart * sent on the exchange. After the CQE is received, the FC exchange is
2617e399441dSJames Smart * terminaed and the Exchange may be used on a different io.
2618e399441dSJames Smart *
2619e399441dSJames Smart * The transport to LLDD api has the transport making a request for a
2620e399441dSJames Smart * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2621e399441dSJames Smart * resource and transfers the command. The LLDD will then process all
2622e399441dSJames Smart * steps to complete the io. Upon completion, the transport done routine
2623e399441dSJames Smart * is called.
2624e399441dSJames Smart *
2625e399441dSJames Smart * So - while the operation is outstanding to the LLDD, there is a link
2626e399441dSJames Smart * level FC exchange resource that is also outstanding. This must be
2627e399441dSJames Smart * considered in all cleanup operations.
2628e399441dSJames Smart */
2629fc17b653SChristoph Hellwig static blk_status_t
2630e399441dSJames Smart nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2631e399441dSJames Smart struct nvme_fc_fcp_op *op, u32 data_len,
nvme_fc_unmap_data(struct nvme_fc_ctrl * ctrl,struct request * rq,struct nvme_fc_fcp_op * op)2632e399441dSJames Smart enum nvmefc_fcp_datadir io_dir)
2633e399441dSJames Smart {
2634e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2635e399441dSJames Smart struct nvme_command *sqe = &cmdiu->sqe;
2636b12740d3SJames Smart int ret, opstate;
2637e399441dSJames Smart
263861bff8efSJames Smart /*
263961bff8efSJames Smart * before attempting to send the io, check to see if we believe
264061bff8efSJames Smart * the target device is present
264161bff8efSJames Smart */
264261bff8efSJames Smart if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
264386ff7c2aSMing Lei return BLK_STS_RESOURCE;
264461bff8efSJames Smart
2645e399441dSJames Smart if (!nvme_fc_ctrl_get(ctrl))
2646fc17b653SChristoph Hellwig return BLK_STS_IOERR;
2647e399441dSJames Smart
2648e399441dSJames Smart /* format the FC-NVME CMD IU and fcp_req */
2649e399441dSJames Smart cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2650e399441dSJames Smart cmdiu->data_len = cpu_to_be32(data_len);
2651e399441dSJames Smart switch (io_dir) {
2652e399441dSJames Smart case NVMEFC_FCP_WRITE:
2653e399441dSJames Smart cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2654e399441dSJames Smart break;
2655e399441dSJames Smart case NVMEFC_FCP_READ:
2656e399441dSJames Smart cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2657e399441dSJames Smart break;
2658e399441dSJames Smart case NVMEFC_FCP_NODATA:
2659e399441dSJames Smart cmdiu->flags = 0;
2660e399441dSJames Smart break;
2661e399441dSJames Smart }
2662e399441dSJames Smart op->fcp_req.payload_length = data_len;
2663e399441dSJames Smart op->fcp_req.io_dir = io_dir;
2664e399441dSJames Smart op->fcp_req.transferred_length = 0;
2665e399441dSJames Smart op->fcp_req.rcv_rsplen = 0;
266662eeacb0SJames Smart op->fcp_req.status = NVME_SC_SUCCESS;
2667e399441dSJames Smart op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2668e399441dSJames Smart
2669e399441dSJames Smart /*
2670e399441dSJames Smart * validate per fabric rules, set fields mandated by fabric spec
2671e399441dSJames Smart * as well as those by FC-NVME spec.
2672e399441dSJames Smart */
2673e399441dSJames Smart WARN_ON_ONCE(sqe->common.metadata);
2674e399441dSJames Smart sqe->common.flags |= NVME_CMD_SGL_METABUF;
2675e399441dSJames Smart
2676e399441dSJames Smart /*
2677d9d34c0bSJames Smart * format SQE DPTR field per FC-NVME rules:
2678d9d34c0bSJames Smart * type=0x5 Transport SGL Data Block Descriptor
2679d9d34c0bSJames Smart * subtype=0xA Transport-specific value
2680d9d34c0bSJames Smart * address=0
2681d9d34c0bSJames Smart * length=length of the data series
2682e399441dSJames Smart */
2683d9d34c0bSJames Smart sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2684d9d34c0bSJames Smart NVME_SGL_FMT_TRANSPORT_A;
2685e399441dSJames Smart sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2686e399441dSJames Smart sqe->rw.dptr.sgl.addr = 0;
2687e399441dSJames Smart
268878a7ac26SJames Smart if (!(op->flags & FCOP_FLAGS_AEN)) {
2689e399441dSJames Smart ret = nvme_fc_map_data(ctrl, op->rq, op);
2690e399441dSJames Smart if (ret < 0) {
2691e399441dSJames Smart nvme_cleanup_cmd(op->rq);
2692e399441dSJames Smart nvme_fc_ctrl_put(ctrl);
2693fc17b653SChristoph Hellwig if (ret == -ENOMEM || ret == -EAGAIN)
2694fc17b653SChristoph Hellwig return BLK_STS_RESOURCE;
2695fc17b653SChristoph Hellwig return BLK_STS_IOERR;
2696e399441dSJames Smart }
2697e399441dSJames Smart }
2698e399441dSJames Smart
2699e399441dSJames Smart fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2700e399441dSJames Smart sizeof(op->cmd_iu), DMA_TO_DEVICE);
2701e399441dSJames Smart
2702e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2703e399441dSJames Smart
270478a7ac26SJames Smart if (!(op->flags & FCOP_FLAGS_AEN))
27056887fc64SSagi Grimberg nvme_start_request(op->rq);
2706e399441dSJames Smart
270767f471b6SJames Smart cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2708e399441dSJames Smart ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2709e399441dSJames Smart &ctrl->rport->remoteport,
2710e399441dSJames Smart queue->lldd_handle, &op->fcp_req);
2711e399441dSJames Smart
2712e399441dSJames Smart if (ret) {
271367f471b6SJames Smart /*
271467f471b6SJames Smart * If the lld fails to send the command is there an issue with
271567f471b6SJames Smart * the csn value? If the command that fails is the Connect,
271667f471b6SJames Smart * no - as the connection won't be live. If it is a command
271767f471b6SJames Smart * post-connect, it's possible a gap in csn may be created.
271867f471b6SJames Smart * Does this matter? As Linux initiators don't send fused
271967f471b6SJames Smart * commands, no. The gap would exist, but as there's nothing
272067f471b6SJames Smart * that depends on csn order to be delivered on the target
272167f471b6SJames Smart * side, it shouldn't hurt. It would be difficult for a
272267f471b6SJames Smart * target to even detect the csn gap as it has no idea when the
272367f471b6SJames Smart * cmd with the csn was supposed to arrive.
272467f471b6SJames Smart */
2725b12740d3SJames Smart opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2726b12740d3SJames Smart __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2727b12740d3SJames Smart
2728c9c12e51SDaniel Wagner if (!(op->flags & FCOP_FLAGS_AEN)) {
2729e399441dSJames Smart nvme_fc_unmap_data(ctrl, op->rq, op);
273016686f3aSMax Gurtovoy nvme_cleanup_cmd(op->rq);
2731c9c12e51SDaniel Wagner }
2732c9c12e51SDaniel Wagner
2733e399441dSJames Smart nvme_fc_ctrl_put(ctrl);
2734e399441dSJames Smart
27358b25f351SJames Smart if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
27368b25f351SJames Smart ret != -EBUSY)
2737fc17b653SChristoph Hellwig return BLK_STS_IOERR;
2738e399441dSJames Smart
273986ff7c2aSMing Lei return BLK_STS_RESOURCE;
2740e399441dSJames Smart }
2741e399441dSJames Smart
2742fc17b653SChristoph Hellwig return BLK_STS_OK;
2743e399441dSJames Smart }
2744e399441dSJames Smart
2745fc17b653SChristoph Hellwig static blk_status_t
2746e399441dSJames Smart nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2747e399441dSJames Smart const struct blk_mq_queue_data *bd)
2748e399441dSJames Smart {
2749e399441dSJames Smart struct nvme_ns *ns = hctx->queue->queuedata;
2750e399441dSJames Smart struct nvme_fc_queue *queue = hctx->driver_data;
2751e399441dSJames Smart struct nvme_fc_ctrl *ctrl = queue->ctrl;
2752e399441dSJames Smart struct request *rq = bd->rq;
2753e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2754e399441dSJames Smart enum nvmefc_fcp_datadir io_dir;
27553bc32bb1SChristoph Hellwig bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2756e399441dSJames Smart u32 data_len;
2757fc17b653SChristoph Hellwig blk_status_t ret;
2758e399441dSJames Smart
27593bc32bb1SChristoph Hellwig if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
2760a9715744STao Chiu !nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2761a9715744STao Chiu return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
27629e0ed16aSSagi Grimberg
2763f4b9e6c9SKeith Busch ret = nvme_setup_cmd(ns, rq);
2764e399441dSJames Smart if (ret)
2765e399441dSJames Smart return ret;
2766e399441dSJames Smart
27679f7d8ae2SJames Smart /*
27689f7d8ae2SJames Smart * nvme core doesn't quite treat the rq opaquely. Commands such
27699f7d8ae2SJames Smart * as WRITE ZEROES will return a non-zero rq payload_bytes yet
27709f7d8ae2SJames Smart * there is no actual payload to be transferred.
27719f7d8ae2SJames Smart * To get it right, key data transmission on there being 1 or
27729f7d8ae2SJames Smart * more physical segments in the sg list. If there is no
27739f7d8ae2SJames Smart * physical segments, there is no payload.
27749f7d8ae2SJames Smart */
27759f7d8ae2SJames Smart if (blk_rq_nr_phys_segments(rq)) {
2776b131c61dSChristoph Hellwig data_len = blk_rq_payload_bytes(rq);
2777e399441dSJames Smart io_dir = ((rq_data_dir(rq) == WRITE) ?
2778e399441dSJames Smart NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
27799f7d8ae2SJames Smart } else {
27809f7d8ae2SJames Smart data_len = 0;
2781e399441dSJames Smart io_dir = NVMEFC_FCP_NODATA;
27829f7d8ae2SJames Smart }
27839f7d8ae2SJames Smart
2784e399441dSJames Smart
2785e399441dSJames Smart return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2786e399441dSJames Smart }
2787e399441dSJames Smart
nvme_fc_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)2788e399441dSJames Smart static void
2789ad22c355SKeith Busch nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2790e399441dSJames Smart {
2791e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2792e399441dSJames Smart struct nvme_fc_fcp_op *aen_op;
2793fc17b653SChristoph Hellwig blk_status_t ret;
2794e399441dSJames Smart
2795eb4ee8f1SJames Smart if (test_bit(FCCTRL_TERMIO, &ctrl->flags))
279661bff8efSJames Smart return;
279761bff8efSJames Smart
2798ad22c355SKeith Busch aen_op = &ctrl->aen_ops[0];
2799e399441dSJames Smart
2800e399441dSJames Smart ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2801e399441dSJames Smart NVMEFC_FCP_NODATA);
2802e399441dSJames Smart if (ret)
2803e399441dSJames Smart dev_err(ctrl->ctrl.device,
2804ad22c355SKeith Busch "failed async event work\n");
2805e399441dSJames Smart }
2806e399441dSJames Smart
2807e399441dSJames Smart static void
2808c3aedd22SJames Smart nvme_fc_complete_rq(struct request *rq)
2809e399441dSJames Smart {
2810e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2811e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl;
2812e399441dSJames Smart
281378a7ac26SJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE);
281452793d62SJames Smart op->flags &= ~FCOP_FLAGS_TERMIO;
2815e399441dSJames Smart
2816e399441dSJames Smart nvme_fc_unmap_data(ctrl, rq, op);
281777f02a7aSChristoph Hellwig nvme_complete_rq(rq);
2818e399441dSJames Smart nvme_fc_ctrl_put(ctrl);
281978a7ac26SJames Smart }
282078a7ac26SJames Smart
2821a4e1d0b7SBart Van Assche static void nvme_fc_map_queues(struct blk_mq_tag_set *set)
282201d83816SSaurav Kashyap {
28231864ea46SChristoph Hellwig struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
282401d83816SSaurav Kashyap int i;
282501d83816SSaurav Kashyap
282601d83816SSaurav Kashyap for (i = 0; i < set->nr_maps; i++) {
282701d83816SSaurav Kashyap struct blk_mq_queue_map *map = &set->map[i];
282801d83816SSaurav Kashyap
282901d83816SSaurav Kashyap if (!map->nr_queues) {
283001d83816SSaurav Kashyap WARN_ON(i == HCTX_TYPE_DEFAULT);
nvme_fc_submit_async_event(struct nvme_ctrl * arg)283101d83816SSaurav Kashyap continue;
283201d83816SSaurav Kashyap }
283301d83816SSaurav Kashyap
283401d83816SSaurav Kashyap /* Call LLDD map queue functionality if defined */
283501d83816SSaurav Kashyap if (ctrl->lport->ops->map_queues)
283601d83816SSaurav Kashyap ctrl->lport->ops->map_queues(&ctrl->lport->localport,
283701d83816SSaurav Kashyap map);
283801d83816SSaurav Kashyap else
283901d83816SSaurav Kashyap blk_mq_map_queues(map);
284001d83816SSaurav Kashyap }
284101d83816SSaurav Kashyap }
2842e399441dSJames Smart
284361bff8efSJames Smart static const struct blk_mq_ops nvme_fc_mq_ops = {
284461bff8efSJames Smart .queue_rq = nvme_fc_queue_rq,
284561bff8efSJames Smart .complete = nvme_fc_complete_rq,
284661bff8efSJames Smart .init_request = nvme_fc_init_request,
284761bff8efSJames Smart .exit_request = nvme_fc_exit_request,
284861bff8efSJames Smart .init_hctx = nvme_fc_init_hctx,
284961bff8efSJames Smart .timeout = nvme_fc_timeout,
nvme_fc_complete_rq(struct request * rq)285001d83816SSaurav Kashyap .map_queues = nvme_fc_map_queues,
2851e399441dSJames Smart };
2852e399441dSJames Smart
2853e399441dSJames Smart static int
2854e399441dSJames Smart nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2855e399441dSJames Smart {
2856e399441dSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
28577314183dSSagi Grimberg unsigned int nr_io_queues;
2858e399441dSJames Smart int ret;
2859e399441dSJames Smart
28607314183dSSagi Grimberg nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
28617314183dSSagi Grimberg ctrl->lport->ops->max_hw_queues);
28627314183dSSagi Grimberg ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
nvme_fc_map_queues(struct blk_mq_tag_set * set)2863e399441dSJames Smart if (ret) {
2864e399441dSJames Smart dev_info(ctrl->ctrl.device,
2865e399441dSJames Smart "set_queue_count failed: %d\n", ret);
2866e399441dSJames Smart return ret;
2867e399441dSJames Smart }
2868e399441dSJames Smart
28697314183dSSagi Grimberg ctrl->ctrl.queue_count = nr_io_queues + 1;
28707314183dSSagi Grimberg if (!nr_io_queues)
2871e399441dSJames Smart return 0;
2872e399441dSJames Smart
2873e399441dSJames Smart nvme_fc_init_io_queues(ctrl);
2874e399441dSJames Smart
28756dfba1c0SChristoph Hellwig ret = nvme_alloc_io_tag_set(&ctrl->ctrl, &ctrl->tag_set,
2876db45e1a5SChristoph Hellwig &nvme_fc_mq_ops, 1,
2877d67790ddSKees Cook struct_size_t(struct nvme_fcp_op_w_sgl, priv,
28786dfba1c0SChristoph Hellwig ctrl->lport->ops->fcprqst_priv_sz));
2879e399441dSJames Smart if (ret)
2880e399441dSJames Smart return ret;
2881e399441dSJames Smart
2882d157e534SJames Smart ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2883e399441dSJames Smart if (ret)
28846dfba1c0SChristoph Hellwig goto out_cleanup_tagset;
2885e399441dSJames Smart
2886d157e534SJames Smart ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2887e399441dSJames Smart if (ret)
2888e399441dSJames Smart goto out_delete_hw_queues;
2889e399441dSJames Smart
28904c984154SJames Smart ctrl->ioq_live = true;
28914c984154SJames Smart
2892e399441dSJames Smart return 0;
2893e399441dSJames Smart
2894e399441dSJames Smart out_delete_hw_queues:
2895e399441dSJames Smart nvme_fc_delete_hw_io_queues(ctrl);
nvme_fc_create_io_queues(struct nvme_fc_ctrl * ctrl)28966dfba1c0SChristoph Hellwig out_cleanup_tagset:
28976dfba1c0SChristoph Hellwig nvme_remove_io_tag_set(&ctrl->ctrl);
2898e399441dSJames Smart nvme_fc_free_io_queues(ctrl);
2899e399441dSJames Smart
2900e399441dSJames Smart /* force put free routine to ignore io queues */
2901e399441dSJames Smart ctrl->ctrl.tagset = NULL;
2902e399441dSJames Smart
2903e399441dSJames Smart return ret;
2904e399441dSJames Smart }
2905e399441dSJames Smart
290661bff8efSJames Smart static int
29073e493c00SJames Smart nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
290861bff8efSJames Smart {
290961bff8efSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2910834d3710SJames Smart u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
29117314183dSSagi Grimberg unsigned int nr_io_queues;
291261bff8efSJames Smart int ret;
291361bff8efSJames Smart
29147314183dSSagi Grimberg nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
29157314183dSSagi Grimberg ctrl->lport->ops->max_hw_queues);
29167314183dSSagi Grimberg ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
291761bff8efSJames Smart if (ret) {
291861bff8efSJames Smart dev_info(ctrl->ctrl.device,
291961bff8efSJames Smart "set_queue_count failed: %d\n", ret);
292061bff8efSJames Smart return ret;
292161bff8efSJames Smart }
292261bff8efSJames Smart
2923834d3710SJames Smart if (!nr_io_queues && prior_ioq_cnt) {
2924834d3710SJames Smart dev_info(ctrl->ctrl.device,
2925834d3710SJames Smart "Fail Reconnect: At least 1 io queue "
2926834d3710SJames Smart "required (was %d)\n", prior_ioq_cnt);
2927834d3710SJames Smart return -ENOSPC;
2928834d3710SJames Smart }
2929834d3710SJames Smart
29307314183dSSagi Grimberg ctrl->ctrl.queue_count = nr_io_queues + 1;
293161bff8efSJames Smart /* check for io queues existing */
2932d858e5f0SSagi Grimberg if (ctrl->ctrl.queue_count == 1)
293361bff8efSJames Smart return 0;
293461bff8efSJames Smart
293588e837edSJames Smart if (prior_ioq_cnt != nr_io_queues) {
2936834d3710SJames Smart dev_info(ctrl->ctrl.device,
2937834d3710SJames Smart "reconnect: revising io queue count from %d to %d\n",
2938834d3710SJames Smart prior_ioq_cnt, nr_io_queues);
2939cda5fd1aSSagi Grimberg blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
294088e837edSJames Smart }
2941cda5fd1aSSagi Grimberg
2942555f66d0SDaniel Wagner ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2943555f66d0SDaniel Wagner if (ret)
2944555f66d0SDaniel Wagner goto out_free_io_queues;
2945555f66d0SDaniel Wagner
2946555f66d0SDaniel Wagner ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2947555f66d0SDaniel Wagner if (ret)
2948555f66d0SDaniel Wagner goto out_delete_hw_queues;
nvme_fc_recreate_io_queues(struct nvme_fc_ctrl * ctrl)2949555f66d0SDaniel Wagner
295061bff8efSJames Smart return 0;
295161bff8efSJames Smart
295261bff8efSJames Smart out_delete_hw_queues:
295361bff8efSJames Smart nvme_fc_delete_hw_io_queues(ctrl);
295461bff8efSJames Smart out_free_io_queues:
295561bff8efSJames Smart nvme_fc_free_io_queues(ctrl);
295661bff8efSJames Smart return ret;
295761bff8efSJames Smart }
295861bff8efSJames Smart
2959158bfb88SJames Smart static void
2960158bfb88SJames Smart nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
2961158bfb88SJames Smart {
2962158bfb88SJames Smart struct nvme_fc_lport *lport = rport->lport;
2963158bfb88SJames Smart
2964158bfb88SJames Smart atomic_inc(&lport->act_rport_cnt);
2965158bfb88SJames Smart }
2966158bfb88SJames Smart
2967158bfb88SJames Smart static void
2968158bfb88SJames Smart nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
2969158bfb88SJames Smart {
2970158bfb88SJames Smart struct nvme_fc_lport *lport = rport->lport;
2971158bfb88SJames Smart u32 cnt;
2972158bfb88SJames Smart
2973158bfb88SJames Smart cnt = atomic_dec_return(&lport->act_rport_cnt);
2974158bfb88SJames Smart if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
2975158bfb88SJames Smart lport->ops->localport_delete(&lport->localport);
2976158bfb88SJames Smart }
2977158bfb88SJames Smart
2978158bfb88SJames Smart static int
2979158bfb88SJames Smart nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
2980158bfb88SJames Smart {
2981158bfb88SJames Smart struct nvme_fc_rport *rport = ctrl->rport;
2982158bfb88SJames Smart u32 cnt;
2983158bfb88SJames Smart
2984eb4ee8f1SJames Smart if (test_and_set_bit(ASSOC_ACTIVE, &ctrl->flags))
2985158bfb88SJames Smart return 1;
2986158bfb88SJames Smart
2987158bfb88SJames Smart cnt = atomic_inc_return(&rport->act_ctrl_cnt);
2988158bfb88SJames Smart if (cnt == 1)
2989158bfb88SJames Smart nvme_fc_rport_active_on_lport(rport);
2990158bfb88SJames Smart
2991158bfb88SJames Smart return 0;
2992158bfb88SJames Smart }
2993158bfb88SJames Smart
2994158bfb88SJames Smart static int
2995158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
2996158bfb88SJames Smart {
2997158bfb88SJames Smart struct nvme_fc_rport *rport = ctrl->rport;
2998158bfb88SJames Smart struct nvme_fc_lport *lport = rport->lport;
2999158bfb88SJames Smart u32 cnt;
3000158bfb88SJames Smart
3001eb4ee8f1SJames Smart /* clearing of ctrl->flags ASSOC_ACTIVE bit is in association delete */
nvme_fc_rport_active_on_lport(struct nvme_fc_rport * rport)3002158bfb88SJames Smart
3003158bfb88SJames Smart cnt = atomic_dec_return(&rport->act_ctrl_cnt);
3004158bfb88SJames Smart if (cnt == 0) {
3005158bfb88SJames Smart if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
3006158bfb88SJames Smart lport->ops->remoteport_delete(&rport->remoteport);
3007158bfb88SJames Smart nvme_fc_rport_inactive_on_lport(rport);
3008158bfb88SJames Smart }
3009158bfb88SJames Smart
nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport * rport)3010158bfb88SJames Smart return 0;
3011158bfb88SJames Smart }
3012158bfb88SJames Smart
301361bff8efSJames Smart /*
301461bff8efSJames Smart * This routine restarts the controller on the host side, and
301561bff8efSJames Smart * on the link side, recreates the controller association.
301661bff8efSJames Smart */
301761bff8efSJames Smart static int
301861bff8efSJames Smart nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
301961bff8efSJames Smart {
302061bff8efSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl * ctrl)302114fd1e98SJames Smart struct nvmefc_ls_rcv_op *disls = NULL;
302214fd1e98SJames Smart unsigned long flags;
302361bff8efSJames Smart int ret;
302461bff8efSJames Smart
3025fdf9dfa8SSagi Grimberg ++ctrl->ctrl.nr_reconnects;
302661bff8efSJames Smart
302796e24801SJames Smart if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
302896e24801SJames Smart return -ENODEV;
302996e24801SJames Smart
3030158bfb88SJames Smart if (nvme_fc_ctlr_active_on_rport(ctrl))
3031158bfb88SJames Smart return -ENOTUNIQ;
3032158bfb88SJames Smart
30334bea364fSJames Smart dev_info(ctrl->ctrl.device,
30344bea364fSJames Smart "NVME-FC{%d}: create association : host wwpn 0x%016llx "
30354bea364fSJames Smart " rport wwpn 0x%016llx: NQN \"%s\"\n",
30364bea364fSJames Smart ctrl->cnum, ctrl->lport->localport.port_name,
nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl * ctrl)30374bea364fSJames Smart ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
30384bea364fSJames Smart
3039caf1cbe3SJames Smart clear_bit(ASSOC_FAILED, &ctrl->flags);
3040caf1cbe3SJames Smart
304161bff8efSJames Smart /*
304261bff8efSJames Smart * Create the admin queue
304361bff8efSJames Smart */
304461bff8efSJames Smart
304561bff8efSJames Smart ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
3046d157e534SJames Smart NVME_AQ_DEPTH);
304761bff8efSJames Smart if (ret)
304861bff8efSJames Smart goto out_free_queue;
304961bff8efSJames Smart
305061bff8efSJames Smart ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
3051d157e534SJames Smart NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
305261bff8efSJames Smart if (ret)
305361bff8efSJames Smart goto out_delete_hw_queue;
305461bff8efSJames Smart
305561bff8efSJames Smart ret = nvmf_connect_admin_queue(&ctrl->ctrl);
305661bff8efSJames Smart if (ret)
305761bff8efSJames Smart goto out_disconnect_admin_queue;
305861bff8efSJames Smart
30599e0ed16aSSagi Grimberg set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
nvme_fc_create_association(struct nvme_fc_ctrl * ctrl)30609e0ed16aSSagi Grimberg
306161bff8efSJames Smart /*
306261bff8efSJames Smart * Check controller capabilities
306361bff8efSJames Smart *
306461bff8efSJames Smart * todo:- add code to check if ctrl attributes changed from
306561bff8efSJames Smart * prior connection values
306661bff8efSJames Smart */
306761bff8efSJames Smart
3068c0f2f45bSSagi Grimberg ret = nvme_enable_ctrl(&ctrl->ctrl);
306960e445bdSMichael Liang if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
307060e445bdSMichael Liang ret = -EIO;
307160e445bdSMichael Liang if (ret)
307261bff8efSJames Smart goto out_disconnect_admin_queue;
307361bff8efSJames Smart
307423748076SJames Smart ctrl->ctrl.max_segments = ctrl->lport->ops->max_sgl_segments;
307523748076SJames Smart ctrl->ctrl.max_hw_sectors = ctrl->ctrl.max_segments <<
307623748076SJames Smart (ilog2(SZ_4K) - 9);
307761bff8efSJames Smart
30789f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(&ctrl->ctrl);
3079e7832cb4SSagi Grimberg
308094cc781fSChristoph Hellwig ret = nvme_init_ctrl_finish(&ctrl->ctrl, false);
308160e445bdSMichael Liang if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
308260e445bdSMichael Liang ret = -EIO;
308360e445bdSMichael Liang if (ret)
308461bff8efSJames Smart goto out_disconnect_admin_queue;
308561bff8efSJames Smart
308661bff8efSJames Smart /* sanity checks */
308761bff8efSJames Smart
308861bff8efSJames Smart /* FC-NVME does not have other data in the capsule */
308961bff8efSJames Smart if (ctrl->ctrl.icdoff) {
309061bff8efSJames Smart dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
309161bff8efSJames Smart ctrl->ctrl.icdoff);
3092f25f8ef7SHannes Reinecke ret = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
309361bff8efSJames Smart goto out_disconnect_admin_queue;
309461bff8efSJames Smart }
309561bff8efSJames Smart
309661bff8efSJames Smart /* FC-NVME supports normal SGL Data Block Descriptors */
3097b61678bcSChaitanya Kulkarni if (!nvme_ctrl_sgl_supported(&ctrl->ctrl)) {
30988df1bff5SMax Gurtovoy dev_err(ctrl->ctrl.device,
30998df1bff5SMax Gurtovoy "Mandatory sgls are not supported!\n");
3100f25f8ef7SHannes Reinecke ret = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
31018df1bff5SMax Gurtovoy goto out_disconnect_admin_queue;
31028df1bff5SMax Gurtovoy }
310361bff8efSJames Smart
310461bff8efSJames Smart if (opts->queue_size > ctrl->ctrl.maxcmd) {
310561bff8efSJames Smart /* warn if maxcmd is lower than queue_size */
310661bff8efSJames Smart dev_warn(ctrl->ctrl.device,
310761bff8efSJames Smart "queue_size %zu > ctrl maxcmd %u, reducing "
31087db39484SJames Smart "to maxcmd\n",
310961bff8efSJames Smart opts->queue_size, ctrl->ctrl.maxcmd);
311061bff8efSJames Smart opts->queue_size = ctrl->ctrl.maxcmd;
311118ecd975SChristoph Hellwig ctrl->ctrl.sqsize = opts->queue_size - 1;
3112d157e534SJames Smart }
3113d157e534SJames Smart
311461bff8efSJames Smart ret = nvme_fc_init_aen_ops(ctrl);
311561bff8efSJames Smart if (ret)
311661bff8efSJames Smart goto out_term_aen_ops;
311761bff8efSJames Smart
311861bff8efSJames Smart /*
311961bff8efSJames Smart * Create the io queues
312061bff8efSJames Smart */
312161bff8efSJames Smart
3122d858e5f0SSagi Grimberg if (ctrl->ctrl.queue_count > 1) {
31234c984154SJames Smart if (!ctrl->ioq_live)
312461bff8efSJames Smart ret = nvme_fc_create_io_queues(ctrl);
312561bff8efSJames Smart else
31263e493c00SJames Smart ret = nvme_fc_recreate_io_queues(ctrl);
312761bff8efSJames Smart }
3128*a09e085bSDaniel Wagner if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
3129fa5bfdd9SDaniel Wagner ret = -EIO;
3130fa5bfdd9SDaniel Wagner if (ret)
3131fa5bfdd9SDaniel Wagner goto out_term_aen_ops;
313261bff8efSJames Smart
3133*a09e085bSDaniel Wagner if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE)) {
3134*a09e085bSDaniel Wagner ret = -EIO;
3135*a09e085bSDaniel Wagner goto out_term_aen_ops;
3136*a09e085bSDaniel Wagner }
3137*a09e085bSDaniel Wagner
3138fdf9dfa8SSagi Grimberg ctrl->ctrl.nr_reconnects = 0;
3139d09f2b45SSagi Grimberg nvme_start_ctrl(&ctrl->ctrl);
314061bff8efSJames Smart
314161bff8efSJames Smart return 0; /* Success */
314261bff8efSJames Smart
314361bff8efSJames Smart out_term_aen_ops:
314461bff8efSJames Smart nvme_fc_term_aen_ops(ctrl);
314561bff8efSJames Smart out_disconnect_admin_queue:
314660e445bdSMichael Liang dev_warn(ctrl->ctrl.device,
314760e445bdSMichael Liang "NVME-FC{%d}: create_assoc failed, assoc_id %llx ret %d\n",
314860e445bdSMichael Liang ctrl->cnum, ctrl->association_id, ret);
314961bff8efSJames Smart /* send a Disconnect(association) LS to fc-nvme target */
315061bff8efSJames Smart nvme_fc_xmt_disconnect_assoc(ctrl);
315114fd1e98SJames Smart spin_lock_irqsave(&ctrl->lock, flags);
3152bcde5f0fSJames Smart ctrl->association_id = 0;
315314fd1e98SJames Smart disls = ctrl->rcv_disconn;
315414fd1e98SJames Smart ctrl->rcv_disconn = NULL;
315514fd1e98SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags);
315614fd1e98SJames Smart if (disls)
315714fd1e98SJames Smart nvme_fc_xmt_ls_rsp(disls);
315861bff8efSJames Smart out_delete_hw_queue:
315961bff8efSJames Smart __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
316061bff8efSJames Smart out_free_queue:
316161bff8efSJames Smart nvme_fc_free_queue(&ctrl->queues[0]);
3162eb4ee8f1SJames Smart clear_bit(ASSOC_ACTIVE, &ctrl->flags);
3163158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(ctrl);
316461bff8efSJames Smart
316561bff8efSJames Smart return ret;
316661bff8efSJames Smart }
316761bff8efSJames Smart
316852793d62SJames Smart
316952793d62SJames Smart /*
317061bff8efSJames Smart * This routine stops operation of the controller on the host side.
317161bff8efSJames Smart * On the host os stack side: Admin and IO queues are stopped,
317261bff8efSJames Smart * outstanding ios on them terminated via FC ABTS.
317361bff8efSJames Smart * On the link side: the association is terminated.
317461bff8efSJames Smart */
317561bff8efSJames Smart static void
317661bff8efSJames Smart nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
317761bff8efSJames Smart {
317814fd1e98SJames Smart struct nvmefc_ls_rcv_op *disls = NULL;
317961bff8efSJames Smart unsigned long flags;
318061bff8efSJames Smart
3181eb4ee8f1SJames Smart if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
3182158bfb88SJames Smart return;
3183158bfb88SJames Smart
318461bff8efSJames Smart spin_lock_irqsave(&ctrl->lock, flags);
3185eb4ee8f1SJames Smart set_bit(FCCTRL_TERMIO, &ctrl->flags);
318661bff8efSJames Smart ctrl->iocnt = 0;
318761bff8efSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags);
318861bff8efSJames Smart
318952793d62SJames Smart __nvme_fc_abort_outstanding_ios(ctrl, false);
319061bff8efSJames Smart
319161bff8efSJames Smart /* kill the aens as they are a separate path */
319261bff8efSJames Smart nvme_fc_abort_aen_ops(ctrl);
319361bff8efSJames Smart
319461bff8efSJames Smart /* wait for all io that had to be aborted */
31958a82dbf1SJames Smart spin_lock_irq(&ctrl->lock);
319636715cf4SJames Smart wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
3197eb4ee8f1SJames Smart clear_bit(FCCTRL_TERMIO, &ctrl->flags);
31988a82dbf1SJames Smart spin_unlock_irq(&ctrl->lock);
319961bff8efSJames Smart
320061bff8efSJames Smart nvme_fc_term_aen_ops(ctrl);
320161bff8efSJames Smart
320261bff8efSJames Smart /*
320361bff8efSJames Smart * send a Disconnect(association) LS to fc-nvme target
320461bff8efSJames Smart * Note: could have been sent at top of process, but
320561bff8efSJames Smart * cleaner on link traffic if after the aborts complete.
320661bff8efSJames Smart * Note: if association doesn't exist, association_id will be 0
320761bff8efSJames Smart */
320861bff8efSJames Smart if (ctrl->association_id)
320961bff8efSJames Smart nvme_fc_xmt_disconnect_assoc(ctrl);
321061bff8efSJames Smart
321114fd1e98SJames Smart spin_lock_irqsave(&ctrl->lock, flags);
3212bcde5f0fSJames Smart ctrl->association_id = 0;
321314fd1e98SJames Smart disls = ctrl->rcv_disconn;
321414fd1e98SJames Smart ctrl->rcv_disconn = NULL;
321514fd1e98SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags);
321614fd1e98SJames Smart if (disls)
321714fd1e98SJames Smart /*
nvme_fc_delete_association(struct nvme_fc_ctrl * ctrl)321814fd1e98SJames Smart * if a Disconnect Request was waiting for a response, send
321914fd1e98SJames Smart * now that all ABTS's have been issued (and are complete).
322014fd1e98SJames Smart */
322114fd1e98SJames Smart nvme_fc_xmt_ls_rsp(disls);
3222bcde5f0fSJames Smart
322361bff8efSJames Smart if (ctrl->ctrl.tagset) {
322461bff8efSJames Smart nvme_fc_delete_hw_io_queues(ctrl);
322561bff8efSJames Smart nvme_fc_free_io_queues(ctrl);
322661bff8efSJames Smart }
322761bff8efSJames Smart
322861bff8efSJames Smart __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
322961bff8efSJames Smart nvme_fc_free_queue(&ctrl->queues[0]);
3230158bfb88SJames Smart
3231d625d05eSJames Smart /* re-enable the admin_q so anything new can fast fail */
32329f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(&ctrl->ctrl);
3233d625d05eSJames Smart
323402d62a8bSJames Smart /* resume the io queues so that things will fast fail */
32359f27bd70SChristoph Hellwig nvme_unquiesce_io_queues(&ctrl->ctrl);
323602d62a8bSJames Smart
3237158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(ctrl);
323861bff8efSJames Smart }
323961bff8efSJames Smart
324061bff8efSJames Smart static void
3241c5017e85SChristoph Hellwig nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
324261bff8efSJames Smart {
3243c5017e85SChristoph Hellwig struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
324461bff8efSJames Smart
324519fce047SJames Smart cancel_work_sync(&ctrl->ioerr_work);
324661bff8efSJames Smart cancel_delayed_work_sync(&ctrl->connect_work);
324761bff8efSJames Smart /*
324861bff8efSJames Smart * kill the association on the link side. this will block
324961bff8efSJames Smart * waiting for io to terminate
325061bff8efSJames Smart */
325161bff8efSJames Smart nvme_fc_delete_association(ctrl);
325261bff8efSJames Smart }
325361bff8efSJames Smart
325461bff8efSJames Smart static void
32555bbecdbcSJames Smart nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
32565bbecdbcSJames Smart {
32572b632970SJames Smart struct nvme_fc_rport *rport = ctrl->rport;
32582b632970SJames Smart struct nvme_fc_remote_port *portptr = &rport->remoteport;
32592b632970SJames Smart unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
32602b632970SJames Smart bool recon = true;
32615bbecdbcSJames Smart
32628884a56dSKeith Busch if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_CONNECTING)
32635bbecdbcSJames Smart return;
32645bbecdbcSJames Smart
3265f25f8ef7SHannes Reinecke if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3266589ff775SJames Smart dev_info(ctrl->ctrl.device,
32675bbecdbcSJames Smart "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
32685bbecdbcSJames Smart ctrl->cnum, status);
3269f25f8ef7SHannes Reinecke if (status > 0 && (status & NVME_SC_DNR))
3270f25f8ef7SHannes Reinecke recon = false;
3271f25f8ef7SHannes Reinecke } else if (time_after_eq(jiffies, rport->dev_loss_end))
32722b632970SJames Smart recon = false;
32735bbecdbcSJames Smart
32742b632970SJames Smart if (recon && nvmf_should_reconnect(&ctrl->ctrl)) {
32752b632970SJames Smart if (portptr->port_state == FC_OBJSTATE_ONLINE)
32765bbecdbcSJames Smart dev_info(ctrl->ctrl.device,
32772b632970SJames Smart "NVME-FC{%d}: Reconnect attempt in %ld "
32782b632970SJames Smart "seconds\n",
32792b632970SJames Smart ctrl->cnum, recon_delay / HZ);
32802b632970SJames Smart else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
32812b632970SJames Smart recon_delay = rport->dev_loss_end - jiffies;
32822b632970SJames Smart
nvme_fc_delete_ctrl(struct nvme_ctrl * nctrl)32832b632970SJames Smart queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
32845bbecdbcSJames Smart } else {
3285f25f8ef7SHannes Reinecke if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3286f25f8ef7SHannes Reinecke if (status > 0 && (status & NVME_SC_DNR))
3287589ff775SJames Smart dev_warn(ctrl->ctrl.device,
3288f25f8ef7SHannes Reinecke "NVME-FC{%d}: reconnect failure\n",
3289f25f8ef7SHannes Reinecke ctrl->cnum);
32902b632970SJames Smart else
32912b632970SJames Smart dev_warn(ctrl->ctrl.device,
3292f25f8ef7SHannes Reinecke "NVME-FC{%d}: Max reconnect attempts "
3293f25f8ef7SHannes Reinecke "(%d) reached.\n",
3294f25f8ef7SHannes Reinecke ctrl->cnum, ctrl->ctrl.nr_reconnects);
3295f25f8ef7SHannes Reinecke } else
3296f25f8ef7SHannes Reinecke dev_warn(ctrl->ctrl.device,
nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl * ctrl,int status)32972b632970SJames Smart "NVME-FC{%d}: dev_loss_tmo (%d) expired "
329877d0612dSMax Gurtovoy "while waiting for remoteport connectivity.\n",
3299614fc1c0SMartin George ctrl->cnum, min_t(int, portptr->dev_loss_tmo,
3300614fc1c0SMartin George (ctrl->ctrl.opts->max_reconnects *
3301614fc1c0SMartin George ctrl->ctrl.opts->reconnect_delay)));
3302c5017e85SChristoph Hellwig WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
33035bbecdbcSJames Smart }
33045bbecdbcSJames Smart }
33055bbecdbcSJames Smart
33065bbecdbcSJames Smart static void
330761bff8efSJames Smart nvme_fc_reset_ctrl_work(struct work_struct *work)
330861bff8efSJames Smart {
330961bff8efSJames Smart struct nvme_fc_ctrl *ctrl =
3310d86c4d8eSChristoph Hellwig container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
33114cff280aSJames Smart
3312d09f2b45SSagi Grimberg nvme_stop_ctrl(&ctrl->ctrl);
331344c6ec77SJames Smart
3314ac9b820eSJames Smart /* will block will waiting for io to terminate */
3315ac9b820eSJames Smart nvme_fc_delete_association(ctrl);
33162b632970SJames Smart
3317ac9b820eSJames Smart if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
3318ac9b820eSJames Smart dev_err(ctrl->ctrl.device,
3319ac9b820eSJames Smart "NVME-FC{%d}: error_recovery: Couldn't change state "
3320ac9b820eSJames Smart "to CONNECTING\n", ctrl->cnum);
3321ac9b820eSJames Smart
3322ac9b820eSJames Smart if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
3323ac9b820eSJames Smart if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3324ac9b820eSJames Smart dev_err(ctrl->ctrl.device,
3325ac9b820eSJames Smart "NVME-FC{%d}: failed to schedule connect "
3326ac9b820eSJames Smart "after reset\n", ctrl->cnum);
3327ac9b820eSJames Smart } else {
3328ac9b820eSJames Smart flush_delayed_work(&ctrl->connect_work);
3329ac9b820eSJames Smart }
3330ac9b820eSJames Smart } else {
3331ac9b820eSJames Smart nvme_fc_reconnect_or_delete(ctrl, -ENOTCONN);
3332ac9b820eSJames Smart }
333361bff8efSJames Smart }
333461bff8efSJames Smart
33354cff280aSJames Smart
333661bff8efSJames Smart static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
333761bff8efSJames Smart .name = "fc",
333861bff8efSJames Smart .module = THIS_MODULE,
3339d3d5b87dSChristoph Hellwig .flags = NVME_F_FABRICS,
334061bff8efSJames Smart .reg_read32 = nvmf_reg_read32,
334161bff8efSJames Smart .reg_read64 = nvmf_reg_read64,
334261bff8efSJames Smart .reg_write32 = nvmf_reg_write32,
334361bff8efSJames Smart .free_ctrl = nvme_fc_nvme_ctrl_freed,
334461bff8efSJames Smart .submit_async_event = nvme_fc_submit_async_event,
3345c5017e85SChristoph Hellwig .delete_ctrl = nvme_fc_delete_ctrl,
334661bff8efSJames Smart .get_address = nvmf_get_address,
334761bff8efSJames Smart };
334861bff8efSJames Smart
nvme_fc_reset_ctrl_work(struct work_struct * work)334961bff8efSJames Smart static void
335061bff8efSJames Smart nvme_fc_connect_ctrl_work(struct work_struct *work)
335161bff8efSJames Smart {
335261bff8efSJames Smart int ret;
335361bff8efSJames Smart
335461bff8efSJames Smart struct nvme_fc_ctrl *ctrl =
335561bff8efSJames Smart container_of(to_delayed_work(work),
335661bff8efSJames Smart struct nvme_fc_ctrl, connect_work);
335761bff8efSJames Smart
335861bff8efSJames Smart ret = nvme_fc_create_association(ctrl);
33595bbecdbcSJames Smart if (ret)
33605bbecdbcSJames Smart nvme_fc_reconnect_or_delete(ctrl, ret);
33615bbecdbcSJames Smart else
336261bff8efSJames Smart dev_info(ctrl->ctrl.device,
33634c984154SJames Smart "NVME-FC{%d}: controller connect complete\n",
336461bff8efSJames Smart ctrl->cnum);
336561bff8efSJames Smart }
336661bff8efSJames Smart
336761bff8efSJames Smart
336861bff8efSJames Smart static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
336961bff8efSJames Smart .queue_rq = nvme_fc_queue_rq,
337061bff8efSJames Smart .complete = nvme_fc_complete_rq,
337176f983cbSChristoph Hellwig .init_request = nvme_fc_init_request,
337261bff8efSJames Smart .exit_request = nvme_fc_exit_request,
337361bff8efSJames Smart .init_hctx = nvme_fc_init_admin_hctx,
337461bff8efSJames Smart .timeout = nvme_fc_timeout,
337561bff8efSJames Smart };
337661bff8efSJames Smart
3377e399441dSJames Smart
337856d5f4f1SJames Smart /*
337956d5f4f1SJames Smart * Fails a controller request if it matches an existing controller
338056d5f4f1SJames Smart * (association) with the same tuple:
338156d5f4f1SJames Smart * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
338256d5f4f1SJames Smart *
338356d5f4f1SJames Smart * The ports don't need to be compared as they are intrinsically
338456d5f4f1SJames Smart * already matched by the port pointers supplied.
338556d5f4f1SJames Smart */
338656d5f4f1SJames Smart static bool
338756d5f4f1SJames Smart nvme_fc_existing_controller(struct nvme_fc_rport *rport,
338856d5f4f1SJames Smart struct nvmf_ctrl_options *opts)
338956d5f4f1SJames Smart {
339056d5f4f1SJames Smart struct nvme_fc_ctrl *ctrl;
339156d5f4f1SJames Smart unsigned long flags;
nvme_fc_connect_ctrl_work(struct work_struct * work)339256d5f4f1SJames Smart bool found = false;
339356d5f4f1SJames Smart
339456d5f4f1SJames Smart spin_lock_irqsave(&rport->lock, flags);
339556d5f4f1SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
339656d5f4f1SJames Smart found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
339756d5f4f1SJames Smart if (found)
339856d5f4f1SJames Smart break;
339956d5f4f1SJames Smart }
340056d5f4f1SJames Smart spin_unlock_irqrestore(&rport->lock, flags);
340156d5f4f1SJames Smart
340256d5f4f1SJames Smart return found;
340356d5f4f1SJames Smart }
340456d5f4f1SJames Smart
3405e399441dSJames Smart static struct nvme_ctrl *
340661bff8efSJames Smart nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3407e399441dSJames Smart struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3408e399441dSJames Smart {
3409e399441dSJames Smart struct nvme_fc_ctrl *ctrl;
3410e399441dSJames Smart unsigned long flags;
3411f673714aSJames Smart int ret, idx, ctrl_loss_tmo;
3412e399441dSJames Smart
341385e6a6adSJames Smart if (!(rport->remoteport.port_role &
341485e6a6adSJames Smart (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
341585e6a6adSJames Smart ret = -EBADR;
341685e6a6adSJames Smart goto out_fail;
341785e6a6adSJames Smart }
341885e6a6adSJames Smart
341956d5f4f1SJames Smart if (!opts->duplicate_connect &&
342056d5f4f1SJames Smart nvme_fc_existing_controller(rport, opts)) {
342156d5f4f1SJames Smart ret = -EALREADY;
342256d5f4f1SJames Smart goto out_fail;
342356d5f4f1SJames Smart }
342456d5f4f1SJames Smart
3425e399441dSJames Smart ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3426e399441dSJames Smart if (!ctrl) {
3427e399441dSJames Smart ret = -ENOMEM;
3428e399441dSJames Smart goto out_fail;
nvme_fc_existing_controller(struct nvme_fc_rport * rport,struct nvmf_ctrl_options * opts)3429e399441dSJames Smart }
3430e399441dSJames Smart
34313dd83f40SSagi Grimberg idx = ida_alloc(&nvme_fc_ctrl_cnt, GFP_KERNEL);
3432e399441dSJames Smart if (idx < 0) {
3433e399441dSJames Smart ret = -ENOSPC;
34348c5c6605SJames Smart goto out_free_ctrl;
3435e399441dSJames Smart }
3436e399441dSJames Smart
3437f673714aSJames Smart /*
3438f673714aSJames Smart * if ctrl_loss_tmo is being enforced and the default reconnect delay
3439f673714aSJames Smart * is being used, change to a shorter reconnect delay for FC.
3440f673714aSJames Smart */
3441f673714aSJames Smart if (opts->max_reconnects != -1 &&
3442f673714aSJames Smart opts->reconnect_delay == NVMF_DEF_RECONNECT_DELAY &&
3443f673714aSJames Smart opts->reconnect_delay > NVME_FC_DEFAULT_RECONNECT_TMO) {
3444f673714aSJames Smart ctrl_loss_tmo = opts->max_reconnects * opts->reconnect_delay;
3445f673714aSJames Smart opts->reconnect_delay = NVME_FC_DEFAULT_RECONNECT_TMO;
3446f673714aSJames Smart opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
3447f673714aSJames Smart opts->reconnect_delay);
nvme_fc_init_ctrl(struct device * dev,struct nvmf_ctrl_options * opts,struct nvme_fc_lport * lport,struct nvme_fc_rport * rport)3448f673714aSJames Smart }
3449f673714aSJames Smart
3450e399441dSJames Smart ctrl->ctrl.opts = opts;
34514c984154SJames Smart ctrl->ctrl.nr_reconnects = 0;
345206f3d71eSJames Smart if (lport->dev)
3453103e515eSHannes Reinecke ctrl->ctrl.numa_node = dev_to_node(lport->dev);
345406f3d71eSJames Smart else
345506f3d71eSJames Smart ctrl->ctrl.numa_node = NUMA_NO_NODE;
3456e399441dSJames Smart INIT_LIST_HEAD(&ctrl->ctrl_list);
3457e399441dSJames Smart ctrl->lport = lport;
3458e399441dSJames Smart ctrl->rport = rport;
3459e399441dSJames Smart ctrl->dev = lport->dev;
3460e399441dSJames Smart ctrl->cnum = idx;
34614c984154SJames Smart ctrl->ioq_live = false;
34628a82dbf1SJames Smart init_waitqueue_head(&ctrl->ioabort_wait);
3463e399441dSJames Smart
3464e399441dSJames Smart get_device(ctrl->dev);
3465e399441dSJames Smart kref_init(&ctrl->ref);
3466e399441dSJames Smart
3467d86c4d8eSChristoph Hellwig INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
346861bff8efSJames Smart INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
346919fce047SJames Smart INIT_WORK(&ctrl->ioerr_work, nvme_fc_ctrl_ioerr_work);
3470e399441dSJames Smart spin_lock_init(&ctrl->lock);
3471e399441dSJames Smart
3472e399441dSJames Smart /* io queue count */
3473d858e5f0SSagi Grimberg ctrl->ctrl.queue_count = min_t(unsigned int,
3474e399441dSJames Smart opts->nr_io_queues,
3475e399441dSJames Smart lport->ops->max_hw_queues);
3476d858e5f0SSagi Grimberg ctrl->ctrl.queue_count++; /* +1 for admin queue */
3477e399441dSJames Smart
3478e399441dSJames Smart ctrl->ctrl.sqsize = opts->queue_size - 1;
3479e399441dSJames Smart ctrl->ctrl.kato = opts->kato;
34804c984154SJames Smart ctrl->ctrl.cntlid = 0xffff;
3481e399441dSJames Smart
3482e399441dSJames Smart ret = -ENOMEM;
3483d858e5f0SSagi Grimberg ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3484d858e5f0SSagi Grimberg sizeof(struct nvme_fc_queue), GFP_KERNEL);
3485e399441dSJames Smart if (!ctrl->queues)
348661bff8efSJames Smart goto out_free_ida;
3487e399441dSJames Smart
34883e493c00SJames Smart nvme_fc_init_queue(ctrl, 0);
34893e493c00SJames Smart
349061bff8efSJames Smart /*
349161bff8efSJames Smart * Would have been nice to init io queues tag set as well.
349261bff8efSJames Smart * However, we require interaction from the controller
349361bff8efSJames Smart * for max io queue count before we can do so.
349461bff8efSJames Smart * Defer this to the connect path.
349561bff8efSJames Smart */
3496e399441dSJames Smart
349761bff8efSJames Smart ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3498e399441dSJames Smart if (ret)
349998e35280SRoss Lagerwall goto out_free_queues;
3500e399441dSJames Smart
350161bff8efSJames Smart /* at this point, teardown path changes to ref counting on nvme ctrl */
3502e399441dSJames Smart
350398e35280SRoss Lagerwall ret = nvme_alloc_admin_tag_set(&ctrl->ctrl, &ctrl->admin_tag_set,
350498e35280SRoss Lagerwall &nvme_fc_admin_mq_ops,
3505d67790ddSKees Cook struct_size_t(struct nvme_fcp_op_w_sgl, priv,
350698e35280SRoss Lagerwall ctrl->lport->ops->fcprqst_priv_sz));
350798e35280SRoss Lagerwall if (ret)
350898e35280SRoss Lagerwall goto fail_ctrl;
350998e35280SRoss Lagerwall
3510e399441dSJames Smart spin_lock_irqsave(&rport->lock, flags);
3511e399441dSJames Smart list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3512e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags);
3513e399441dSJames Smart
3514aef373bbSDaniel Wagner if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
35154c984154SJames Smart dev_err(ctrl->ctrl.device,
35164c984154SJames Smart "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
35174c984154SJames Smart goto fail_ctrl;
351817c4dc6eSJames Smart }
351917c4dc6eSJames Smart
35204c984154SJames Smart if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
35214c984154SJames Smart dev_err(ctrl->ctrl.device,
35224c984154SJames Smart "NVME-FC{%d}: failed to schedule initial connect\n",
35234c984154SJames Smart ctrl->cnum);
35244c984154SJames Smart goto fail_ctrl;
35254c984154SJames Smart }
35264c984154SJames Smart
35274c984154SJames Smart flush_delayed_work(&ctrl->connect_work);
35284c984154SJames Smart
35294c984154SJames Smart dev_info(ctrl->ctrl.device,
35304c984154SJames Smart "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
3531e5ea42faSHannes Reinecke ctrl->cnum, nvmf_ctrl_subsysnqn(&ctrl->ctrl));
35324c984154SJames Smart
35334c984154SJames Smart return &ctrl->ctrl;
35344c984154SJames Smart
35354c984154SJames Smart fail_ctrl:
3536cf25809bSJames Smart nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
353719fce047SJames Smart cancel_work_sync(&ctrl->ioerr_work);
3538cf25809bSJames Smart cancel_work_sync(&ctrl->ctrl.reset_work);
3539cf25809bSJames Smart cancel_delayed_work_sync(&ctrl->connect_work);
3540cf25809bSJames Smart
3541de41447aSEwan D. Milne ctrl->ctrl.opts = NULL;
354217c4dc6eSJames Smart
354361bff8efSJames Smart /* initiate nvme ctrl ref counting teardown */
354461bff8efSJames Smart nvme_uninit_ctrl(&ctrl->ctrl);
354561bff8efSJames Smart
35460b5a7669SJames Smart /* Remove core ctrl ref. */
35470b5a7669SJames Smart nvme_put_ctrl(&ctrl->ctrl);
35480b5a7669SJames Smart
354961bff8efSJames Smart /* as we're past the point where we transition to the ref
355061bff8efSJames Smart * counting teardown path, if we return a bad pointer here,
355161bff8efSJames Smart * the calling routine, thinking it's prior to the
355261bff8efSJames Smart * transition, will do an rport put. Since the teardown
355361bff8efSJames Smart * path also does a rport put, we do an extra get here to
355461bff8efSJames Smart * so proper order/teardown happens.
355561bff8efSJames Smart */
355661bff8efSJames Smart nvme_fc_rport_get(rport);
355761bff8efSJames Smart
35584c984154SJames Smart return ERR_PTR(-EIO);
3559e399441dSJames Smart
356061bff8efSJames Smart out_free_queues:
356161bff8efSJames Smart kfree(ctrl->queues);
3562e399441dSJames Smart out_free_ida:
356361bff8efSJames Smart put_device(ctrl->dev);
35643dd83f40SSagi Grimberg ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
3565e399441dSJames Smart out_free_ctrl:
3566e399441dSJames Smart kfree(ctrl);
3567e399441dSJames Smart out_fail:
3568e399441dSJames Smart /* exit via here doesn't follow ctlr ref points */
3569e399441dSJames Smart return ERR_PTR(ret);
3570e399441dSJames Smart }
3571e399441dSJames Smart
3572e399441dSJames Smart
3573e399441dSJames Smart struct nvmet_fc_traddr {
3574e399441dSJames Smart u64 nn;
3575e399441dSJames Smart u64 pn;
3576e399441dSJames Smart };
3577e399441dSJames Smart
3578e399441dSJames Smart static int
35799c5358e1SJames Smart __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3580e399441dSJames Smart {
3581e399441dSJames Smart u64 token64;
3582e399441dSJames Smart
35839c5358e1SJames Smart if (match_u64(sstr, &token64))
35849c5358e1SJames Smart return -EINVAL;
35859c5358e1SJames Smart *val = token64;
3586e399441dSJames Smart
35879c5358e1SJames Smart return 0;
3588e399441dSJames Smart }
3589e399441dSJames Smart
35909c5358e1SJames Smart /*
35919c5358e1SJames Smart * This routine validates and extracts the WWN's from the TRADDR string.
35929c5358e1SJames Smart * As kernel parsers need the 0x to determine number base, universally
35939c5358e1SJames Smart * build string to parse with 0x prefix before parsing name strings.
35949c5358e1SJames Smart */
35959c5358e1SJames Smart static int
35969c5358e1SJames Smart nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
35979c5358e1SJames Smart {
35989c5358e1SJames Smart char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
35999c5358e1SJames Smart substring_t wwn = { name, &name[sizeof(name)-1] };
36009c5358e1SJames Smart int nnoffset, pnoffset;
36019c5358e1SJames Smart
3602d4e4230cSMilan P. Gandhi /* validate if string is one of the 2 allowed formats */
36039c5358e1SJames Smart if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
36049c5358e1SJames Smart !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
36059c5358e1SJames Smart !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
36069c5358e1SJames Smart "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
36079c5358e1SJames Smart nnoffset = NVME_FC_TRADDR_OXNNLEN;
36089c5358e1SJames Smart pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
36099c5358e1SJames Smart NVME_FC_TRADDR_OXNNLEN;
36109c5358e1SJames Smart } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
36119c5358e1SJames Smart !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
36129c5358e1SJames Smart !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
36139c5358e1SJames Smart "pn-", NVME_FC_TRADDR_NNLEN))) {
36149c5358e1SJames Smart nnoffset = NVME_FC_TRADDR_NNLEN;
36159c5358e1SJames Smart pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
36169c5358e1SJames Smart } else
36179c5358e1SJames Smart goto out_einval;
36189c5358e1SJames Smart
36199c5358e1SJames Smart name[0] = '0';
36209c5358e1SJames Smart name[1] = 'x';
36219c5358e1SJames Smart name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
__nvme_fc_parse_u64(substring_t * sstr,u64 * val)36229c5358e1SJames Smart
36239c5358e1SJames Smart memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
36249c5358e1SJames Smart if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
36259c5358e1SJames Smart goto out_einval;
36269c5358e1SJames Smart
36279c5358e1SJames Smart memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
36289c5358e1SJames Smart if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
36299c5358e1SJames Smart goto out_einval;
36309c5358e1SJames Smart
36319c5358e1SJames Smart return 0;
36329c5358e1SJames Smart
36339c5358e1SJames Smart out_einval:
36349c5358e1SJames Smart pr_warn("%s: bad traddr string\n", __func__);
36359c5358e1SJames Smart return -EINVAL;
3636e399441dSJames Smart }
3637e399441dSJames Smart
3638e399441dSJames Smart static struct nvme_ctrl *
nvme_fc_parse_traddr(struct nvmet_fc_traddr * traddr,char * buf,size_t blen)3639e399441dSJames Smart nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3640e399441dSJames Smart {
3641e399441dSJames Smart struct nvme_fc_lport *lport;
3642e399441dSJames Smart struct nvme_fc_rport *rport;
364361bff8efSJames Smart struct nvme_ctrl *ctrl;
3644e399441dSJames Smart struct nvmet_fc_traddr laddr = { 0L, 0L };
3645e399441dSJames Smart struct nvmet_fc_traddr raddr = { 0L, 0L };
3646e399441dSJames Smart unsigned long flags;
3647e399441dSJames Smart int ret;
3648e399441dSJames Smart
36499c5358e1SJames Smart ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3650e399441dSJames Smart if (ret || !raddr.nn || !raddr.pn)
3651e399441dSJames Smart return ERR_PTR(-EINVAL);
3652e399441dSJames Smart
36539c5358e1SJames Smart ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3654e399441dSJames Smart if (ret || !laddr.nn || !laddr.pn)
3655e399441dSJames Smart return ERR_PTR(-EINVAL);
3656e399441dSJames Smart
3657e399441dSJames Smart /* find the host and remote ports to connect together */
3658e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
3659e399441dSJames Smart list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3660e399441dSJames Smart if (lport->localport.node_name != laddr.nn ||
36619e0e8dacSJames Smart lport->localport.port_name != laddr.pn ||
36629e0e8dacSJames Smart lport->localport.port_state != FC_OBJSTATE_ONLINE)
3663e399441dSJames Smart continue;
3664e399441dSJames Smart
3665e399441dSJames Smart list_for_each_entry(rport, &lport->endp_list, endp_list) {
3666e399441dSJames Smart if (rport->remoteport.node_name != raddr.nn ||
36679e0e8dacSJames Smart rport->remoteport.port_name != raddr.pn ||
36689e0e8dacSJames Smart rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3669e399441dSJames Smart continue;
3670e399441dSJames Smart
3671e399441dSJames Smart /* if fail to get reference fall through. Will error */
3672e399441dSJames Smart if (!nvme_fc_rport_get(rport))
3673e399441dSJames Smart break;
3674e399441dSJames Smart
3675e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
3676e399441dSJames Smart
367761bff8efSJames Smart ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
367861bff8efSJames Smart if (IS_ERR(ctrl))
367961bff8efSJames Smart nvme_fc_rport_put(rport);
368061bff8efSJames Smart return ctrl;
3681e399441dSJames Smart }
3682e399441dSJames Smart }
3683e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
3684e399441dSJames Smart
36854fb135adSJohannes Thumshirn pr_warn("%s: %s - %s combination not found\n",
36864fb135adSJohannes Thumshirn __func__, opts->traddr, opts->host_traddr);
3687e399441dSJames Smart return ERR_PTR(-ENOENT);
3688e399441dSJames Smart }
3689e399441dSJames Smart
3690e399441dSJames Smart
3691e399441dSJames Smart static struct nvmf_transport_ops nvme_fc_transport = {
3692e399441dSJames Smart .name = "fc",
36930de5cd36SRoy Shterman .module = THIS_MODULE,
3694e399441dSJames Smart .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
36955bbecdbcSJames Smart .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3696e399441dSJames Smart .create_ctrl = nvme_fc_create_ctrl,
3697e399441dSJames Smart };
3698e399441dSJames Smart
369997faec53SJames Smart /* Arbitrary successive failures max. With lots of subsystems could be high */
370097faec53SJames Smart #define DISCOVERY_MAX_FAIL 20
370197faec53SJames Smart
370297faec53SJames Smart static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
370397faec53SJames Smart struct device_attribute *attr, const char *buf, size_t count)
370497faec53SJames Smart {
370597faec53SJames Smart unsigned long flags;
370697faec53SJames Smart LIST_HEAD(local_disc_list);
370797faec53SJames Smart struct nvme_fc_lport *lport;
370897faec53SJames Smart struct nvme_fc_rport *rport;
370997faec53SJames Smart int failcnt = 0;
371097faec53SJames Smart
371197faec53SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
371297faec53SJames Smart restart:
371397faec53SJames Smart list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
371497faec53SJames Smart list_for_each_entry(rport, &lport->endp_list, endp_list) {
371597faec53SJames Smart if (!nvme_fc_lport_get(lport))
371697faec53SJames Smart continue;
371797faec53SJames Smart if (!nvme_fc_rport_get(rport)) {
371897faec53SJames Smart /*
371997faec53SJames Smart * This is a temporary condition. Upon restart
372097faec53SJames Smart * this rport will be gone from the list.
372197faec53SJames Smart *
372297faec53SJames Smart * Revert the lport put and retry. Anything
372397faec53SJames Smart * added to the list already will be skipped (as
372497faec53SJames Smart * they are no longer list_empty). Loops should
372597faec53SJames Smart * resume at rports that were not yet seen.
372697faec53SJames Smart */
372797faec53SJames Smart nvme_fc_lport_put(lport);
372897faec53SJames Smart
372997faec53SJames Smart if (failcnt++ < DISCOVERY_MAX_FAIL)
373097faec53SJames Smart goto restart;
373197faec53SJames Smart
373297faec53SJames Smart pr_err("nvme_discovery: too many reference "
373397faec53SJames Smart "failures\n");
373497faec53SJames Smart goto process_local_list;
373597faec53SJames Smart }
373697faec53SJames Smart if (list_empty(&rport->disc_list))
373797faec53SJames Smart list_add_tail(&rport->disc_list,
373897faec53SJames Smart &local_disc_list);
373997faec53SJames Smart }
374097faec53SJames Smart }
374197faec53SJames Smart
374297faec53SJames Smart process_local_list:
374397faec53SJames Smart while (!list_empty(&local_disc_list)) {
374497faec53SJames Smart rport = list_first_entry(&local_disc_list,
nvme_fc_nvme_discovery_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)374597faec53SJames Smart struct nvme_fc_rport, disc_list);
374697faec53SJames Smart list_del_init(&rport->disc_list);
374797faec53SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
374897faec53SJames Smart
374997faec53SJames Smart lport = rport->lport;
375097faec53SJames Smart /* signal discovery. Won't hurt if it repeats */
375197faec53SJames Smart nvme_fc_signal_discovery_scan(lport, rport);
375297faec53SJames Smart nvme_fc_rport_put(rport);
375397faec53SJames Smart nvme_fc_lport_put(lport);
375497faec53SJames Smart
375597faec53SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
375697faec53SJames Smart }
375797faec53SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
375897faec53SJames Smart
375997faec53SJames Smart return count;
376097faec53SJames Smart }
37613dbbca75SMuneendra Kumar
376255d7baa3SChristoph Hellwig static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
376355d7baa3SChristoph Hellwig
376455d7baa3SChristoph Hellwig #ifdef CONFIG_BLK_CGROUP_FC_APPID
37653dbbca75SMuneendra Kumar /* Parse the cgroup id from a buf and return the length of cgrpid */
37663dbbca75SMuneendra Kumar static int fc_parse_cgrpid(const char *buf, u64 *id)
37673dbbca75SMuneendra Kumar {
37683dbbca75SMuneendra Kumar char cgrp_id[16+1];
37693dbbca75SMuneendra Kumar int cgrpid_len, j;
37703dbbca75SMuneendra Kumar
37713dbbca75SMuneendra Kumar memset(cgrp_id, 0x0, sizeof(cgrp_id));
37723dbbca75SMuneendra Kumar for (cgrpid_len = 0, j = 0; cgrpid_len < 17; cgrpid_len++) {
37733dbbca75SMuneendra Kumar if (buf[cgrpid_len] != ':')
37743dbbca75SMuneendra Kumar cgrp_id[cgrpid_len] = buf[cgrpid_len];
37753dbbca75SMuneendra Kumar else {
37763dbbca75SMuneendra Kumar j = 1;
37773dbbca75SMuneendra Kumar break;
37783dbbca75SMuneendra Kumar }
37793dbbca75SMuneendra Kumar }
37803dbbca75SMuneendra Kumar if (!j)
37813dbbca75SMuneendra Kumar return -EINVAL;
37823dbbca75SMuneendra Kumar if (kstrtou64(cgrp_id, 16, id) < 0)
37833dbbca75SMuneendra Kumar return -EINVAL;
37843dbbca75SMuneendra Kumar return cgrpid_len;
37853dbbca75SMuneendra Kumar }
37863dbbca75SMuneendra Kumar
37873dbbca75SMuneendra Kumar /*
3788c814153cSChristoph Hellwig * Parse and update the appid in the blkcg associated with the cgroupid.
37893dbbca75SMuneendra Kumar */
3790c814153cSChristoph Hellwig static ssize_t fc_appid_store(struct device *dev,
3791c814153cSChristoph Hellwig struct device_attribute *attr, const char *buf, size_t count)
37923dbbca75SMuneendra Kumar {
37939317d001SChristoph Hellwig size_t orig_count = count;
37943dbbca75SMuneendra Kumar u64 cgrp_id;
37953dbbca75SMuneendra Kumar int appid_len = 0;
37963dbbca75SMuneendra Kumar int cgrpid_len = 0;
37973dbbca75SMuneendra Kumar char app_id[FC_APPID_LEN];
37983dbbca75SMuneendra Kumar int ret = 0;
37993dbbca75SMuneendra Kumar
38003dbbca75SMuneendra Kumar if (buf[count-1] == '\n')
38013dbbca75SMuneendra Kumar count--;
38023dbbca75SMuneendra Kumar
38033dbbca75SMuneendra Kumar if ((count > (16+1+FC_APPID_LEN)) || (!strchr(buf, ':')))
38043dbbca75SMuneendra Kumar return -EINVAL;
38053dbbca75SMuneendra Kumar
38063dbbca75SMuneendra Kumar cgrpid_len = fc_parse_cgrpid(buf, &cgrp_id);
38073dbbca75SMuneendra Kumar if (cgrpid_len < 0)
38083dbbca75SMuneendra Kumar return -EINVAL;
fc_parse_cgrpid(const char * buf,u64 * id)38093dbbca75SMuneendra Kumar appid_len = count - cgrpid_len - 1;
38103dbbca75SMuneendra Kumar if (appid_len > FC_APPID_LEN)
38113dbbca75SMuneendra Kumar return -EINVAL;
38123dbbca75SMuneendra Kumar
38133dbbca75SMuneendra Kumar memset(app_id, 0x0, sizeof(app_id));
38143dbbca75SMuneendra Kumar memcpy(app_id, &buf[cgrpid_len+1], appid_len);
38153dbbca75SMuneendra Kumar ret = blkcg_set_fc_appid(app_id, cgrp_id, sizeof(app_id));
38163dbbca75SMuneendra Kumar if (ret < 0)
38173dbbca75SMuneendra Kumar return ret;
38189317d001SChristoph Hellwig return orig_count;
38193dbbca75SMuneendra Kumar }
38203dbbca75SMuneendra Kumar static DEVICE_ATTR(appid_store, 0200, NULL, fc_appid_store);
382155d7baa3SChristoph Hellwig #endif /* CONFIG_BLK_CGROUP_FC_APPID */
382297faec53SJames Smart
382397faec53SJames Smart static struct attribute *nvme_fc_attrs[] = {
382497faec53SJames Smart &dev_attr_nvme_discovery.attr,
382555d7baa3SChristoph Hellwig #ifdef CONFIG_BLK_CGROUP_FC_APPID
38263dbbca75SMuneendra Kumar &dev_attr_appid_store.attr,
382755d7baa3SChristoph Hellwig #endif
382897faec53SJames Smart NULL
382997faec53SJames Smart };
383097faec53SJames Smart
383160b152a5SRikard Falkeborn static const struct attribute_group nvme_fc_attr_group = {
383297faec53SJames Smart .attrs = nvme_fc_attrs,
fc_appid_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)383397faec53SJames Smart };
383497faec53SJames Smart
383597faec53SJames Smart static const struct attribute_group *nvme_fc_attr_groups[] = {
383697faec53SJames Smart &nvme_fc_attr_group,
383797faec53SJames Smart NULL
383897faec53SJames Smart };
383997faec53SJames Smart
384097faec53SJames Smart static struct class fc_class = {
384197faec53SJames Smart .name = "fc",
384297faec53SJames Smart .dev_groups = nvme_fc_attr_groups,
384397faec53SJames Smart };
384497faec53SJames Smart
3845e399441dSJames Smart static int __init nvme_fc_init_module(void)
3846e399441dSJames Smart {
38475f568556SJames Smart int ret;
38485f568556SJames Smart
38495f568556SJames Smart /*
38505f568556SJames Smart * NOTE:
38515f568556SJames Smart * It is expected that in the future the kernel will combine
38525f568556SJames Smart * the FC-isms that are currently under scsi and now being
38535f568556SJames Smart * added to by NVME into a new standalone FC class. The SCSI
38545f568556SJames Smart * and NVME protocols and their devices would be under this
38555f568556SJames Smart * new FC class.
38565f568556SJames Smart *
38575f568556SJames Smart * As we need something to post FC-specific udev events to,
38585f568556SJames Smart * specifically for nvme probe events, start by creating the
38595f568556SJames Smart * new device class. When the new standalone FC class is
38605f568556SJames Smart * put in place, this code will move to a more generic
38615f568556SJames Smart * location for the class.
38625f568556SJames Smart */
386397faec53SJames Smart ret = class_register(&fc_class);
386497faec53SJames Smart if (ret) {
38655f568556SJames Smart pr_err("couldn't register class fc\n");
3866baa6b7ebSDaniel Wagner return ret;
38675f568556SJames Smart }
38685f568556SJames Smart
38695f568556SJames Smart /*
38705f568556SJames Smart * Create a device for the FC-centric udev events
38715f568556SJames Smart */
387297faec53SJames Smart fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
38735f568556SJames Smart "fc_udev_device");
38745f568556SJames Smart if (IS_ERR(fc_udev_device)) {
38755f568556SJames Smart pr_err("couldn't create fc_udev device!\n");
38765f568556SJames Smart ret = PTR_ERR(fc_udev_device);
38775f568556SJames Smart goto out_destroy_class;
38785f568556SJames Smart }
38795f568556SJames Smart
38805f568556SJames Smart ret = nvmf_register_transport(&nvme_fc_transport);
38815f568556SJames Smart if (ret)
38825f568556SJames Smart goto out_destroy_device;
38835f568556SJames Smart
38845f568556SJames Smart return 0;
38855f568556SJames Smart
38865f568556SJames Smart out_destroy_device:
388797faec53SJames Smart device_destroy(&fc_class, MKDEV(0, 0));
nvme_fc_init_module(void)38885f568556SJames Smart out_destroy_class:
388997faec53SJames Smart class_unregister(&fc_class);
38908730c1ddSHannes Reinecke
38915f568556SJames Smart return ret;
3892e399441dSJames Smart }
3893e399441dSJames Smart
38944c73cbdfSJames Smart static void
38954c73cbdfSJames Smart nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
38964c73cbdfSJames Smart {
38974c73cbdfSJames Smart struct nvme_fc_ctrl *ctrl;
38984c73cbdfSJames Smart
38994c73cbdfSJames Smart spin_lock(&rport->lock);
39004c73cbdfSJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
39014c73cbdfSJames Smart dev_warn(ctrl->ctrl.device,
39024c73cbdfSJames Smart "NVME-FC{%d}: transport unloading: deleting ctrl\n",
39034c73cbdfSJames Smart ctrl->cnum);
39044c73cbdfSJames Smart nvme_delete_ctrl(&ctrl->ctrl);
39054c73cbdfSJames Smart }
39064c73cbdfSJames Smart spin_unlock(&rport->lock);
39074c73cbdfSJames Smart }
39084c73cbdfSJames Smart
3909baa6b7ebSDaniel Wagner static void __exit nvme_fc_exit_module(void)
39104c73cbdfSJames Smart {
39114c73cbdfSJames Smart struct nvme_fc_lport *lport;
39124c73cbdfSJames Smart struct nvme_fc_rport *rport;
39134c73cbdfSJames Smart unsigned long flags;
39144c73cbdfSJames Smart
39154c73cbdfSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags);
3916baa6b7ebSDaniel Wagner list_for_each_entry(lport, &nvme_fc_lport_list, port_list)
3917baa6b7ebSDaniel Wagner list_for_each_entry(rport, &lport->endp_list, endp_list)
3918baa6b7ebSDaniel Wagner nvme_fc_delete_controllers(rport);
39194c73cbdfSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags);
3920baa6b7ebSDaniel Wagner flush_workqueue(nvme_delete_wq);
3921e399441dSJames Smart
3922e399441dSJames Smart nvmf_unregister_transport(&nvme_fc_transport);
3923e399441dSJames Smart
392497faec53SJames Smart device_destroy(&fc_class, MKDEV(0, 0));
392597faec53SJames Smart class_unregister(&fc_class);
3926e399441dSJames Smart }
3927e399441dSJames Smart
3928e399441dSJames Smart module_init(nvme_fc_init_module);
3929e399441dSJames Smart module_exit(nvme_fc_exit_module);
3930e399441dSJames Smart
3931e399441dSJames Smart MODULE_LICENSE("GPL v2");
3932