xref: /openbmc/linux/drivers/nvme/host/fc.c (revision f673714a)
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>
12e399441dSJames Smart 
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>
19e399441dSJames Smart 
20e399441dSJames Smart /* *************************** Data Structures/Defines ****************** */
21e399441dSJames Smart 
22e399441dSJames Smart 
23e399441dSJames Smart enum nvme_fc_queue_flags {
2426c0a26dSJens Axboe 	NVME_FC_Q_CONNECTED = 0,
2526c0a26dSJens Axboe 	NVME_FC_Q_LIVE,
26e399441dSJames Smart };
27e399441dSJames Smart 
28ac7fe82bSJames Smart #define NVME_FC_DEFAULT_DEV_LOSS_TMO	60	/* seconds */
29f673714aSJames Smart #define NVME_FC_DEFAULT_RECONNECT_TMO	2	/* delay between reconnects
30f673714aSJames Smart 						 * when connected and a
31f673714aSJames Smart 						 * connection failure.
32f673714aSJames Smart 						 */
33ac7fe82bSJames Smart 
34e399441dSJames Smart struct nvme_fc_queue {
35e399441dSJames Smart 	struct nvme_fc_ctrl	*ctrl;
36e399441dSJames Smart 	struct device		*dev;
37e399441dSJames Smart 	struct blk_mq_hw_ctx	*hctx;
38e399441dSJames Smart 	void			*lldd_handle;
39e399441dSJames Smart 	size_t			cmnd_capsule_len;
40e399441dSJames Smart 	u32			qnum;
41e399441dSJames Smart 	u32			rqcnt;
42e399441dSJames Smart 	u32			seqno;
43e399441dSJames Smart 
44e399441dSJames Smart 	u64			connection_id;
45e399441dSJames Smart 	atomic_t		csn;
46e399441dSJames Smart 
47e399441dSJames Smart 	unsigned long		flags;
48e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
49e399441dSJames Smart 
508d64daf7SJames Smart enum nvme_fcop_flags {
518d64daf7SJames Smart 	FCOP_FLAGS_TERMIO	= (1 << 0),
52c3aedd22SJames Smart 	FCOP_FLAGS_AEN		= (1 << 1),
538d64daf7SJames Smart };
548d64daf7SJames Smart 
55e399441dSJames Smart struct nvmefc_ls_req_op {
56e399441dSJames Smart 	struct nvmefc_ls_req	ls_req;
57e399441dSJames Smart 
58c913a8b0SJames Smart 	struct nvme_fc_rport	*rport;
59e399441dSJames Smart 	struct nvme_fc_queue	*queue;
60e399441dSJames Smart 	struct request		*rq;
618d64daf7SJames Smart 	u32			flags;
62e399441dSJames Smart 
63e399441dSJames Smart 	int			ls_error;
64e399441dSJames Smart 	struct completion	ls_done;
65c913a8b0SJames Smart 	struct list_head	lsreq_list;	/* rport->ls_req_list */
66e399441dSJames Smart 	bool			req_queued;
67e399441dSJames Smart };
68e399441dSJames Smart 
6914fd1e98SJames Smart struct nvmefc_ls_rcv_op {
7014fd1e98SJames Smart 	struct nvme_fc_rport		*rport;
7114fd1e98SJames Smart 	struct nvmefc_ls_rsp		*lsrsp;
7214fd1e98SJames Smart 	union nvmefc_ls_requests	*rqstbuf;
7314fd1e98SJames Smart 	union nvmefc_ls_responses	*rspbuf;
7414fd1e98SJames Smart 	u16				rqstdatalen;
7514fd1e98SJames Smart 	bool				handled;
7614fd1e98SJames Smart 	dma_addr_t			rspdma;
7714fd1e98SJames Smart 	struct list_head		lsrcv_list;	/* rport->ls_rcv_list */
7814fd1e98SJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
7914fd1e98SJames Smart 
80e399441dSJames Smart enum nvme_fcpop_state {
81e399441dSJames Smart 	FCPOP_STATE_UNINIT	= 0,
82e399441dSJames Smart 	FCPOP_STATE_IDLE	= 1,
83e399441dSJames Smart 	FCPOP_STATE_ACTIVE	= 2,
84e399441dSJames Smart 	FCPOP_STATE_ABORTED	= 3,
8578a7ac26SJames Smart 	FCPOP_STATE_COMPLETE	= 4,
86e399441dSJames Smart };
87e399441dSJames Smart 
88e399441dSJames Smart struct nvme_fc_fcp_op {
89e399441dSJames Smart 	struct nvme_request	nreq;		/*
90e399441dSJames Smart 						 * nvme/host/core.c
91e399441dSJames Smart 						 * requires this to be
92e399441dSJames Smart 						 * the 1st element in the
93e399441dSJames Smart 						 * private structure
94e399441dSJames Smart 						 * associated with the
95e399441dSJames Smart 						 * request.
96e399441dSJames Smart 						 */
97e399441dSJames Smart 	struct nvmefc_fcp_req	fcp_req;
98e399441dSJames Smart 
99e399441dSJames Smart 	struct nvme_fc_ctrl	*ctrl;
100e399441dSJames Smart 	struct nvme_fc_queue	*queue;
101e399441dSJames Smart 	struct request		*rq;
102e399441dSJames Smart 
103e399441dSJames Smart 	atomic_t		state;
10478a7ac26SJames Smart 	u32			flags;
105e399441dSJames Smart 	u32			rqno;
106e399441dSJames Smart 	u32			nents;
107e399441dSJames Smart 
108e399441dSJames Smart 	struct nvme_fc_cmd_iu	cmd_iu;
109e399441dSJames Smart 	struct nvme_fc_ersp_iu	rsp_iu;
110e399441dSJames Smart };
111e399441dSJames Smart 
112d3d0bc78SBart Van Assche struct nvme_fcp_op_w_sgl {
113d3d0bc78SBart Van Assche 	struct nvme_fc_fcp_op	op;
114b1ae1a23SIsrael Rukshin 	struct scatterlist	sgl[NVME_INLINE_SG_CNT];
115f1e71d75SGustavo A. R. Silva 	uint8_t			priv[];
116d3d0bc78SBart Van Assche };
117d3d0bc78SBart Van Assche 
118e399441dSJames Smart struct nvme_fc_lport {
119e399441dSJames Smart 	struct nvme_fc_local_port	localport;
120e399441dSJames Smart 
121e399441dSJames Smart 	struct ida			endp_cnt;
122e399441dSJames Smart 	struct list_head		port_list;	/* nvme_fc_port_list */
123e399441dSJames Smart 	struct list_head		endp_list;
124e399441dSJames Smart 	struct device			*dev;	/* physical device for dma */
125e399441dSJames Smart 	struct nvme_fc_port_template	*ops;
126e399441dSJames Smart 	struct kref			ref;
127158bfb88SJames Smart 	atomic_t                        act_rport_cnt;
128e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
129e399441dSJames Smart 
130e399441dSJames Smart struct nvme_fc_rport {
131e399441dSJames Smart 	struct nvme_fc_remote_port	remoteport;
132e399441dSJames Smart 
133e399441dSJames Smart 	struct list_head		endp_list; /* for lport->endp_list */
134e399441dSJames Smart 	struct list_head		ctrl_list;
135c913a8b0SJames Smart 	struct list_head		ls_req_list;
13614fd1e98SJames Smart 	struct list_head		ls_rcv_list;
13797faec53SJames Smart 	struct list_head		disc_list;
138c913a8b0SJames Smart 	struct device			*dev;	/* physical device for dma */
139c913a8b0SJames Smart 	struct nvme_fc_lport		*lport;
140e399441dSJames Smart 	spinlock_t			lock;
141e399441dSJames Smart 	struct kref			ref;
142158bfb88SJames Smart 	atomic_t                        act_ctrl_cnt;
1432b632970SJames Smart 	unsigned long			dev_loss_end;
14414fd1e98SJames Smart 	struct work_struct		lsrcv_work;
145e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
146e399441dSJames Smart 
147eb4ee8f1SJames Smart /* fc_ctrl flags values - specified as bit positions */
148eb4ee8f1SJames Smart #define ASSOC_ACTIVE		0
149eb4ee8f1SJames Smart #define FCCTRL_TERMIO		1
150e399441dSJames Smart 
151e399441dSJames Smart struct nvme_fc_ctrl {
152e399441dSJames Smart 	spinlock_t		lock;
153e399441dSJames Smart 	struct nvme_fc_queue	*queues;
154e399441dSJames Smart 	struct device		*dev;
155e399441dSJames Smart 	struct nvme_fc_lport	*lport;
156e399441dSJames Smart 	struct nvme_fc_rport	*rport;
157e399441dSJames Smart 	u32			cnum;
158e399441dSJames Smart 
1594c984154SJames Smart 	bool			ioq_live;
1604cff280aSJames Smart 	atomic_t		err_work_active;
161e399441dSJames Smart 	u64			association_id;
16214fd1e98SJames Smart 	struct nvmefc_ls_rcv_op	*rcv_disconn;
163e399441dSJames Smart 
164e399441dSJames Smart 	struct list_head	ctrl_list;	/* rport->ctrl_list */
165e399441dSJames Smart 
166e399441dSJames Smart 	struct blk_mq_tag_set	admin_tag_set;
167e399441dSJames Smart 	struct blk_mq_tag_set	tag_set;
168e399441dSJames Smart 
16961bff8efSJames Smart 	struct delayed_work	connect_work;
1704cff280aSJames Smart 	struct work_struct	err_work;
17161bff8efSJames Smart 
172e399441dSJames Smart 	struct kref		ref;
173eb4ee8f1SJames Smart 	unsigned long		flags;
17461bff8efSJames Smart 	u32			iocnt;
17536715cf4SJames Smart 	wait_queue_head_t	ioabort_wait;
176e399441dSJames Smart 
17738dabe21SKeith Busch 	struct nvme_fc_fcp_op	aen_ops[NVME_NR_AEN_COMMANDS];
178e399441dSJames Smart 
179e399441dSJames Smart 	struct nvme_ctrl	ctrl;
180e399441dSJames Smart };
181e399441dSJames Smart 
182e399441dSJames Smart static inline struct nvme_fc_ctrl *
183e399441dSJames Smart to_fc_ctrl(struct nvme_ctrl *ctrl)
184e399441dSJames Smart {
185e399441dSJames Smart 	return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
186e399441dSJames Smart }
187e399441dSJames Smart 
188e399441dSJames Smart static inline struct nvme_fc_lport *
189e399441dSJames Smart localport_to_lport(struct nvme_fc_local_port *portptr)
190e399441dSJames Smart {
191e399441dSJames Smart 	return container_of(portptr, struct nvme_fc_lport, localport);
192e399441dSJames Smart }
193e399441dSJames Smart 
194e399441dSJames Smart static inline struct nvme_fc_rport *
195e399441dSJames Smart remoteport_to_rport(struct nvme_fc_remote_port *portptr)
196e399441dSJames Smart {
197e399441dSJames Smart 	return container_of(portptr, struct nvme_fc_rport, remoteport);
198e399441dSJames Smart }
199e399441dSJames Smart 
200e399441dSJames Smart static inline struct nvmefc_ls_req_op *
201e399441dSJames Smart ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
202e399441dSJames Smart {
203e399441dSJames Smart 	return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
204e399441dSJames Smart }
205e399441dSJames Smart 
206e399441dSJames Smart static inline struct nvme_fc_fcp_op *
207e399441dSJames Smart fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
208e399441dSJames Smart {
209e399441dSJames Smart 	return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
210e399441dSJames Smart }
211e399441dSJames Smart 
212e399441dSJames Smart 
213e399441dSJames Smart 
214e399441dSJames Smart /* *************************** Globals **************************** */
215e399441dSJames Smart 
216e399441dSJames Smart 
217e399441dSJames Smart static DEFINE_SPINLOCK(nvme_fc_lock);
218e399441dSJames Smart 
219e399441dSJames Smart static LIST_HEAD(nvme_fc_lport_list);
220e399441dSJames Smart static DEFINE_IDA(nvme_fc_local_port_cnt);
221e399441dSJames Smart static DEFINE_IDA(nvme_fc_ctrl_cnt);
222e399441dSJames Smart 
2238730c1ddSHannes Reinecke static struct workqueue_struct *nvme_fc_wq;
224e399441dSJames Smart 
2254c73cbdfSJames Smart static bool nvme_fc_waiting_to_unload;
2264c73cbdfSJames Smart static DECLARE_COMPLETION(nvme_fc_unload_proceed);
2274c73cbdfSJames Smart 
2285f568556SJames Smart /*
2295f568556SJames Smart  * These items are short-term. They will eventually be moved into
2305f568556SJames Smart  * a generic FC class. See comments in module init.
2315f568556SJames Smart  */
2325f568556SJames Smart static struct device *fc_udev_device;
2335f568556SJames Smart 
234ff029451SChristoph Hellwig static void nvme_fc_complete_rq(struct request *rq);
235e399441dSJames Smart 
236e399441dSJames Smart /* *********************** FC-NVME Port Management ************************ */
237e399441dSJames Smart 
238e399441dSJames Smart static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
239e399441dSJames Smart 			struct nvme_fc_queue *, unsigned int);
240e399441dSJames Smart 
24114fd1e98SJames Smart static void nvme_fc_handle_ls_rqst_work(struct work_struct *work);
24214fd1e98SJames Smart 
24314fd1e98SJames Smart 
2445533d424SJames Smart static void
2455533d424SJames Smart nvme_fc_free_lport(struct kref *ref)
2465533d424SJames Smart {
2475533d424SJames Smart 	struct nvme_fc_lport *lport =
2485533d424SJames Smart 		container_of(ref, struct nvme_fc_lport, ref);
2495533d424SJames Smart 	unsigned long flags;
2505533d424SJames Smart 
2515533d424SJames Smart 	WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
2525533d424SJames Smart 	WARN_ON(!list_empty(&lport->endp_list));
2535533d424SJames Smart 
2545533d424SJames Smart 	/* remove from transport list */
2555533d424SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
2565533d424SJames Smart 	list_del(&lport->port_list);
2574c73cbdfSJames Smart 	if (nvme_fc_waiting_to_unload && list_empty(&nvme_fc_lport_list))
2584c73cbdfSJames Smart 		complete(&nvme_fc_unload_proceed);
2595533d424SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
2605533d424SJames Smart 
2615533d424SJames Smart 	ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
2625533d424SJames Smart 	ida_destroy(&lport->endp_cnt);
2635533d424SJames Smart 
2645533d424SJames Smart 	put_device(lport->dev);
2655533d424SJames Smart 
2665533d424SJames Smart 	kfree(lport);
2675533d424SJames Smart }
2685533d424SJames Smart 
2695533d424SJames Smart static void
2705533d424SJames Smart nvme_fc_lport_put(struct nvme_fc_lport *lport)
2715533d424SJames Smart {
2725533d424SJames Smart 	kref_put(&lport->ref, nvme_fc_free_lport);
2735533d424SJames Smart }
2745533d424SJames Smart 
2755533d424SJames Smart static int
2765533d424SJames Smart nvme_fc_lport_get(struct nvme_fc_lport *lport)
2775533d424SJames Smart {
2785533d424SJames Smart 	return kref_get_unless_zero(&lport->ref);
2795533d424SJames Smart }
2805533d424SJames Smart 
2815533d424SJames Smart 
2825533d424SJames Smart static struct nvme_fc_lport *
283c5760f30SJames Smart nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo,
284c5760f30SJames Smart 			struct nvme_fc_port_template *ops,
285c5760f30SJames Smart 			struct device *dev)
2865533d424SJames Smart {
2875533d424SJames Smart 	struct nvme_fc_lport *lport;
2885533d424SJames Smart 	unsigned long flags;
2895533d424SJames Smart 
2905533d424SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
2915533d424SJames Smart 
2925533d424SJames Smart 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
2935533d424SJames Smart 		if (lport->localport.node_name != pinfo->node_name ||
2945533d424SJames Smart 		    lport->localport.port_name != pinfo->port_name)
2955533d424SJames Smart 			continue;
2965533d424SJames Smart 
297c5760f30SJames Smart 		if (lport->dev != dev) {
298c5760f30SJames Smart 			lport = ERR_PTR(-EXDEV);
299c5760f30SJames Smart 			goto out_done;
300c5760f30SJames Smart 		}
301c5760f30SJames Smart 
3025533d424SJames Smart 		if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
3035533d424SJames Smart 			lport = ERR_PTR(-EEXIST);
3045533d424SJames Smart 			goto out_done;
3055533d424SJames Smart 		}
3065533d424SJames Smart 
3075533d424SJames Smart 		if (!nvme_fc_lport_get(lport)) {
3085533d424SJames Smart 			/*
3095533d424SJames Smart 			 * fails if ref cnt already 0. If so,
3105533d424SJames Smart 			 * act as if lport already deleted
3115533d424SJames Smart 			 */
3125533d424SJames Smart 			lport = NULL;
3135533d424SJames Smart 			goto out_done;
3145533d424SJames Smart 		}
3155533d424SJames Smart 
3165533d424SJames Smart 		/* resume the lport */
3175533d424SJames Smart 
318c5760f30SJames Smart 		lport->ops = ops;
3195533d424SJames Smart 		lport->localport.port_role = pinfo->port_role;
3205533d424SJames Smart 		lport->localport.port_id = pinfo->port_id;
3215533d424SJames Smart 		lport->localport.port_state = FC_OBJSTATE_ONLINE;
3225533d424SJames Smart 
3235533d424SJames Smart 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
3245533d424SJames Smart 
3255533d424SJames Smart 		return lport;
3265533d424SJames Smart 	}
3275533d424SJames Smart 
3285533d424SJames Smart 	lport = NULL;
3295533d424SJames Smart 
3305533d424SJames Smart out_done:
3315533d424SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
3325533d424SJames Smart 
3335533d424SJames Smart 	return lport;
3345533d424SJames Smart }
335e399441dSJames Smart 
336e399441dSJames Smart /**
337e399441dSJames Smart  * nvme_fc_register_localport - transport entry point called by an
338e399441dSJames Smart  *                              LLDD to register the existence of a NVME
339e399441dSJames Smart  *                              host FC port.
340e399441dSJames Smart  * @pinfo:     pointer to information about the port to be registered
341e399441dSJames Smart  * @template:  LLDD entrypoints and operational parameters for the port
342e399441dSJames Smart  * @dev:       physical hardware device node port corresponds to. Will be
343e399441dSJames Smart  *             used for DMA mappings
34476c910c7SBart Van Assche  * @portptr:   pointer to a local port pointer. Upon success, the routine
345e399441dSJames Smart  *             will allocate a nvme_fc_local_port structure and place its
346e399441dSJames Smart  *             address in the local port pointer. Upon failure, local port
347e399441dSJames Smart  *             pointer will be set to 0.
348e399441dSJames Smart  *
349e399441dSJames Smart  * Returns:
350e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
351e399441dSJames Smart  * (ex: -ENXIO) upon failure.
352e399441dSJames Smart  */
353e399441dSJames Smart int
354e399441dSJames Smart nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
355e399441dSJames Smart 			struct nvme_fc_port_template *template,
356e399441dSJames Smart 			struct device *dev,
357e399441dSJames Smart 			struct nvme_fc_local_port **portptr)
358e399441dSJames Smart {
359e399441dSJames Smart 	struct nvme_fc_lport *newrec;
360e399441dSJames Smart 	unsigned long flags;
361e399441dSJames Smart 	int ret, idx;
362e399441dSJames Smart 
363e399441dSJames Smart 	if (!template->localport_delete || !template->remoteport_delete ||
364e399441dSJames Smart 	    !template->ls_req || !template->fcp_io ||
365e399441dSJames Smart 	    !template->ls_abort || !template->fcp_abort ||
366e399441dSJames Smart 	    !template->max_hw_queues || !template->max_sgl_segments ||
3678c5c6605SJames Smart 	    !template->max_dif_sgl_segments || !template->dma_boundary) {
368e399441dSJames Smart 		ret = -EINVAL;
369e399441dSJames Smart 		goto out_reghost_failed;
370e399441dSJames Smart 	}
371e399441dSJames Smart 
3725533d424SJames Smart 	/*
3735533d424SJames Smart 	 * look to see if there is already a localport that had been
3745533d424SJames Smart 	 * deregistered and in the process of waiting for all the
3755533d424SJames Smart 	 * references to fully be removed.  If the references haven't
3765533d424SJames Smart 	 * expired, we can simply re-enable the localport. Remoteports
3775533d424SJames Smart 	 * and controller reconnections should resume naturally.
3785533d424SJames Smart 	 */
379c5760f30SJames Smart 	newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
3805533d424SJames Smart 
3815533d424SJames Smart 	/* found an lport, but something about its state is bad */
3825533d424SJames Smart 	if (IS_ERR(newrec)) {
3835533d424SJames Smart 		ret = PTR_ERR(newrec);
3845533d424SJames Smart 		goto out_reghost_failed;
3855533d424SJames Smart 
3865533d424SJames Smart 	/* found existing lport, which was resumed */
3875533d424SJames Smart 	} else if (newrec) {
3885533d424SJames Smart 		*portptr = &newrec->localport;
3895533d424SJames Smart 		return 0;
3905533d424SJames Smart 	}
3915533d424SJames Smart 
3925533d424SJames Smart 	/* nothing found - allocate a new localport struct */
3935533d424SJames Smart 
394e399441dSJames Smart 	newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
395e399441dSJames Smart 			 GFP_KERNEL);
396e399441dSJames Smart 	if (!newrec) {
397e399441dSJames Smart 		ret = -ENOMEM;
398e399441dSJames Smart 		goto out_reghost_failed;
399e399441dSJames Smart 	}
400e399441dSJames Smart 
401e399441dSJames Smart 	idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
402e399441dSJames Smart 	if (idx < 0) {
403e399441dSJames Smart 		ret = -ENOSPC;
404e399441dSJames Smart 		goto out_fail_kfree;
405e399441dSJames Smart 	}
406e399441dSJames Smart 
407e399441dSJames Smart 	if (!get_device(dev) && dev) {
408e399441dSJames Smart 		ret = -ENODEV;
409e399441dSJames Smart 		goto out_ida_put;
410e399441dSJames Smart 	}
411e399441dSJames Smart 
412e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->port_list);
413e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->endp_list);
414e399441dSJames Smart 	kref_init(&newrec->ref);
415158bfb88SJames Smart 	atomic_set(&newrec->act_rport_cnt, 0);
416e399441dSJames Smart 	newrec->ops = template;
417e399441dSJames Smart 	newrec->dev = dev;
418e399441dSJames Smart 	ida_init(&newrec->endp_cnt);
419f56bf76fSJames Smart 	if (template->local_priv_sz)
420e399441dSJames Smart 		newrec->localport.private = &newrec[1];
421f56bf76fSJames Smart 	else
422f56bf76fSJames Smart 		newrec->localport.private = NULL;
423e399441dSJames Smart 	newrec->localport.node_name = pinfo->node_name;
424e399441dSJames Smart 	newrec->localport.port_name = pinfo->port_name;
425e399441dSJames Smart 	newrec->localport.port_role = pinfo->port_role;
426e399441dSJames Smart 	newrec->localport.port_id = pinfo->port_id;
427e399441dSJames Smart 	newrec->localport.port_state = FC_OBJSTATE_ONLINE;
428e399441dSJames Smart 	newrec->localport.port_num = idx;
429e399441dSJames Smart 
430e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
431e399441dSJames Smart 	list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
432e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
433e399441dSJames Smart 
434e399441dSJames Smart 	if (dev)
435e399441dSJames Smart 		dma_set_seg_boundary(dev, template->dma_boundary);
436e399441dSJames Smart 
437e399441dSJames Smart 	*portptr = &newrec->localport;
438e399441dSJames Smart 	return 0;
439e399441dSJames Smart 
440e399441dSJames Smart out_ida_put:
441e399441dSJames Smart 	ida_simple_remove(&nvme_fc_local_port_cnt, idx);
442e399441dSJames Smart out_fail_kfree:
443e399441dSJames Smart 	kfree(newrec);
444e399441dSJames Smart out_reghost_failed:
445e399441dSJames Smart 	*portptr = NULL;
446e399441dSJames Smart 
447e399441dSJames Smart 	return ret;
448e399441dSJames Smart }
449e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
450e399441dSJames Smart 
451e399441dSJames Smart /**
452e399441dSJames Smart  * nvme_fc_unregister_localport - transport entry point called by an
453e399441dSJames Smart  *                              LLDD to deregister/remove a previously
454e399441dSJames Smart  *                              registered a NVME host FC port.
45576c910c7SBart Van Assche  * @portptr: pointer to the (registered) local port that is to be deregistered.
456e399441dSJames Smart  *
457e399441dSJames Smart  * Returns:
458e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
459e399441dSJames Smart  * (ex: -ENXIO) upon failure.
460e399441dSJames Smart  */
461e399441dSJames Smart int
462e399441dSJames Smart nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
463e399441dSJames Smart {
464e399441dSJames Smart 	struct nvme_fc_lport *lport = localport_to_lport(portptr);
465e399441dSJames Smart 	unsigned long flags;
466e399441dSJames Smart 
467e399441dSJames Smart 	if (!portptr)
468e399441dSJames Smart 		return -EINVAL;
469e399441dSJames Smart 
470e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
471e399441dSJames Smart 
472e399441dSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
473e399441dSJames Smart 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
474e399441dSJames Smart 		return -EINVAL;
475e399441dSJames Smart 	}
476e399441dSJames Smart 	portptr->port_state = FC_OBJSTATE_DELETED;
477e399441dSJames Smart 
478e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
479e399441dSJames Smart 
480158bfb88SJames Smart 	if (atomic_read(&lport->act_rport_cnt) == 0)
481158bfb88SJames Smart 		lport->ops->localport_delete(&lport->localport);
482158bfb88SJames Smart 
483e399441dSJames Smart 	nvme_fc_lport_put(lport);
484e399441dSJames Smart 
485e399441dSJames Smart 	return 0;
486e399441dSJames Smart }
487e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
488e399441dSJames Smart 
489eaefd5abSJames Smart /*
490eaefd5abSJames Smart  * TRADDR strings, per FC-NVME are fixed format:
491eaefd5abSJames Smart  *   "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
492eaefd5abSJames Smart  * udev event will only differ by prefix of what field is
493eaefd5abSJames Smart  * being specified:
494eaefd5abSJames Smart  *    "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
495eaefd5abSJames Smart  *  19 + 43 + null_fudge = 64 characters
496eaefd5abSJames Smart  */
497eaefd5abSJames Smart #define FCNVME_TRADDR_LENGTH		64
498eaefd5abSJames Smart 
499eaefd5abSJames Smart static void
500eaefd5abSJames Smart nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
501eaefd5abSJames Smart 		struct nvme_fc_rport *rport)
502eaefd5abSJames Smart {
503eaefd5abSJames Smart 	char hostaddr[FCNVME_TRADDR_LENGTH];	/* NVMEFC_HOST_TRADDR=...*/
504eaefd5abSJames Smart 	char tgtaddr[FCNVME_TRADDR_LENGTH];	/* NVMEFC_TRADDR=...*/
505eaefd5abSJames Smart 	char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
506eaefd5abSJames Smart 
507eaefd5abSJames Smart 	if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
508eaefd5abSJames Smart 		return;
509eaefd5abSJames Smart 
510eaefd5abSJames Smart 	snprintf(hostaddr, sizeof(hostaddr),
511eaefd5abSJames Smart 		"NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
512eaefd5abSJames Smart 		lport->localport.node_name, lport->localport.port_name);
513eaefd5abSJames Smart 	snprintf(tgtaddr, sizeof(tgtaddr),
514eaefd5abSJames Smart 		"NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
515eaefd5abSJames Smart 		rport->remoteport.node_name, rport->remoteport.port_name);
516eaefd5abSJames Smart 	kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
517eaefd5abSJames Smart }
518eaefd5abSJames Smart 
519469d0ef0SJames Smart static void
520469d0ef0SJames Smart nvme_fc_free_rport(struct kref *ref)
521469d0ef0SJames Smart {
522469d0ef0SJames Smart 	struct nvme_fc_rport *rport =
523469d0ef0SJames Smart 		container_of(ref, struct nvme_fc_rport, ref);
524469d0ef0SJames Smart 	struct nvme_fc_lport *lport =
525469d0ef0SJames Smart 			localport_to_lport(rport->remoteport.localport);
526469d0ef0SJames Smart 	unsigned long flags;
527469d0ef0SJames Smart 
528469d0ef0SJames Smart 	WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
529469d0ef0SJames Smart 	WARN_ON(!list_empty(&rport->ctrl_list));
530469d0ef0SJames Smart 
531469d0ef0SJames Smart 	/* remove from lport list */
532469d0ef0SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
533469d0ef0SJames Smart 	list_del(&rport->endp_list);
534469d0ef0SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
535469d0ef0SJames Smart 
53697faec53SJames Smart 	WARN_ON(!list_empty(&rport->disc_list));
537469d0ef0SJames Smart 	ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
538469d0ef0SJames Smart 
539469d0ef0SJames Smart 	kfree(rport);
540469d0ef0SJames Smart 
541469d0ef0SJames Smart 	nvme_fc_lport_put(lport);
542469d0ef0SJames Smart }
543469d0ef0SJames Smart 
544469d0ef0SJames Smart static void
545469d0ef0SJames Smart nvme_fc_rport_put(struct nvme_fc_rport *rport)
546469d0ef0SJames Smart {
547469d0ef0SJames Smart 	kref_put(&rport->ref, nvme_fc_free_rport);
548469d0ef0SJames Smart }
549469d0ef0SJames Smart 
550469d0ef0SJames Smart static int
551469d0ef0SJames Smart nvme_fc_rport_get(struct nvme_fc_rport *rport)
552469d0ef0SJames Smart {
553469d0ef0SJames Smart 	return kref_get_unless_zero(&rport->ref);
554469d0ef0SJames Smart }
555469d0ef0SJames Smart 
5562b632970SJames Smart static void
5572b632970SJames Smart nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl)
5582b632970SJames Smart {
5592b632970SJames Smart 	switch (ctrl->ctrl.state) {
5602b632970SJames Smart 	case NVME_CTRL_NEW:
561ad6a0a52SMax Gurtovoy 	case NVME_CTRL_CONNECTING:
5622b632970SJames Smart 		/*
5632b632970SJames Smart 		 * As all reconnects were suppressed, schedule a
5642b632970SJames Smart 		 * connect.
5652b632970SJames Smart 		 */
5662b632970SJames Smart 		dev_info(ctrl->ctrl.device,
5672b632970SJames Smart 			"NVME-FC{%d}: connectivity re-established. "
5682b632970SJames Smart 			"Attempting reconnect\n", ctrl->cnum);
5692b632970SJames Smart 
5702b632970SJames Smart 		queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
5712b632970SJames Smart 		break;
5722b632970SJames Smart 
5732b632970SJames Smart 	case NVME_CTRL_RESETTING:
5742b632970SJames Smart 		/*
5752b632970SJames Smart 		 * Controller is already in the process of terminating the
5762b632970SJames Smart 		 * association. No need to do anything further. The reconnect
5772b632970SJames Smart 		 * step will naturally occur after the reset completes.
5782b632970SJames Smart 		 */
5792b632970SJames Smart 		break;
5802b632970SJames Smart 
5812b632970SJames Smart 	default:
5822b632970SJames Smart 		/* no action to take - let it delete */
5832b632970SJames Smart 		break;
5842b632970SJames Smart 	}
5852b632970SJames Smart }
5862b632970SJames Smart 
5872b632970SJames Smart static struct nvme_fc_rport *
5882b632970SJames Smart nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport,
5892b632970SJames Smart 				struct nvme_fc_port_info *pinfo)
5902b632970SJames Smart {
5912b632970SJames Smart 	struct nvme_fc_rport *rport;
5922b632970SJames Smart 	struct nvme_fc_ctrl *ctrl;
5932b632970SJames Smart 	unsigned long flags;
5942b632970SJames Smart 
5952b632970SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
5962b632970SJames Smart 
5972b632970SJames Smart 	list_for_each_entry(rport, &lport->endp_list, endp_list) {
5982b632970SJames Smart 		if (rport->remoteport.node_name != pinfo->node_name ||
5992b632970SJames Smart 		    rport->remoteport.port_name != pinfo->port_name)
6002b632970SJames Smart 			continue;
6012b632970SJames Smart 
6022b632970SJames Smart 		if (!nvme_fc_rport_get(rport)) {
6032b632970SJames Smart 			rport = ERR_PTR(-ENOLCK);
6042b632970SJames Smart 			goto out_done;
6052b632970SJames Smart 		}
6062b632970SJames Smart 
6072b632970SJames Smart 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
6082b632970SJames Smart 
6092b632970SJames Smart 		spin_lock_irqsave(&rport->lock, flags);
6102b632970SJames Smart 
6112b632970SJames Smart 		/* has it been unregistered */
6122b632970SJames Smart 		if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) {
6132b632970SJames Smart 			/* means lldd called us twice */
6142b632970SJames Smart 			spin_unlock_irqrestore(&rport->lock, flags);
6152b632970SJames Smart 			nvme_fc_rport_put(rport);
6162b632970SJames Smart 			return ERR_PTR(-ESTALE);
6172b632970SJames Smart 		}
6182b632970SJames Smart 
6190cdd5fcaSJames Smart 		rport->remoteport.port_role = pinfo->port_role;
6200cdd5fcaSJames Smart 		rport->remoteport.port_id = pinfo->port_id;
6212b632970SJames Smart 		rport->remoteport.port_state = FC_OBJSTATE_ONLINE;
6222b632970SJames Smart 		rport->dev_loss_end = 0;
6232b632970SJames Smart 
6242b632970SJames Smart 		/*
6252b632970SJames Smart 		 * kick off a reconnect attempt on all associations to the
6262b632970SJames Smart 		 * remote port. A successful reconnects will resume i/o.
6272b632970SJames Smart 		 */
6282b632970SJames Smart 		list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
6292b632970SJames Smart 			nvme_fc_resume_controller(ctrl);
6302b632970SJames Smart 
6312b632970SJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
6322b632970SJames Smart 
6332b632970SJames Smart 		return rport;
6342b632970SJames Smart 	}
6352b632970SJames Smart 
6362b632970SJames Smart 	rport = NULL;
6372b632970SJames Smart 
6382b632970SJames Smart out_done:
6392b632970SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
6402b632970SJames Smart 
6412b632970SJames Smart 	return rport;
6422b632970SJames Smart }
6432b632970SJames Smart 
6442b632970SJames Smart static inline void
6452b632970SJames Smart __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport,
6462b632970SJames Smart 			struct nvme_fc_port_info *pinfo)
6472b632970SJames Smart {
6482b632970SJames Smart 	if (pinfo->dev_loss_tmo)
6492b632970SJames Smart 		rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
6502b632970SJames Smart 	else
6512b632970SJames Smart 		rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
6522b632970SJames Smart }
6532b632970SJames Smart 
654e399441dSJames Smart /**
655e399441dSJames Smart  * nvme_fc_register_remoteport - transport entry point called by an
656e399441dSJames Smart  *                              LLDD to register the existence of a NVME
657e399441dSJames Smart  *                              subsystem FC port on its fabric.
658e399441dSJames Smart  * @localport: pointer to the (registered) local port that the remote
659e399441dSJames Smart  *             subsystem port is connected to.
660e399441dSJames Smart  * @pinfo:     pointer to information about the port to be registered
66176c910c7SBart Van Assche  * @portptr:   pointer to a remote port pointer. Upon success, the routine
662e399441dSJames Smart  *             will allocate a nvme_fc_remote_port structure and place its
663e399441dSJames Smart  *             address in the remote port pointer. Upon failure, remote port
664e399441dSJames Smart  *             pointer will be set to 0.
665e399441dSJames Smart  *
666e399441dSJames Smart  * Returns:
667e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
668e399441dSJames Smart  * (ex: -ENXIO) upon failure.
669e399441dSJames Smart  */
670e399441dSJames Smart int
671e399441dSJames Smart nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
672e399441dSJames Smart 				struct nvme_fc_port_info *pinfo,
673e399441dSJames Smart 				struct nvme_fc_remote_port **portptr)
674e399441dSJames Smart {
675e399441dSJames Smart 	struct nvme_fc_lport *lport = localport_to_lport(localport);
676e399441dSJames Smart 	struct nvme_fc_rport *newrec;
677e399441dSJames Smart 	unsigned long flags;
678e399441dSJames Smart 	int ret, idx;
679e399441dSJames Smart 
6802b632970SJames Smart 	if (!nvme_fc_lport_get(lport)) {
6812b632970SJames Smart 		ret = -ESHUTDOWN;
6822b632970SJames Smart 		goto out_reghost_failed;
6832b632970SJames Smart 	}
6842b632970SJames Smart 
6852b632970SJames Smart 	/*
6862b632970SJames Smart 	 * look to see if there is already a remoteport that is waiting
6872b632970SJames Smart 	 * for a reconnect (within dev_loss_tmo) with the same WWN's.
6882b632970SJames Smart 	 * If so, transition to it and reconnect.
6892b632970SJames Smart 	 */
6902b632970SJames Smart 	newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
6912b632970SJames Smart 
6922b632970SJames Smart 	/* found an rport, but something about its state is bad */
6932b632970SJames Smart 	if (IS_ERR(newrec)) {
6942b632970SJames Smart 		ret = PTR_ERR(newrec);
6952b632970SJames Smart 		goto out_lport_put;
6962b632970SJames Smart 
6972b632970SJames Smart 	/* found existing rport, which was resumed */
6982b632970SJames Smart 	} else if (newrec) {
6992b632970SJames Smart 		nvme_fc_lport_put(lport);
7002b632970SJames Smart 		__nvme_fc_set_dev_loss_tmo(newrec, pinfo);
7012b632970SJames Smart 		nvme_fc_signal_discovery_scan(lport, newrec);
7022b632970SJames Smart 		*portptr = &newrec->remoteport;
7032b632970SJames Smart 		return 0;
7042b632970SJames Smart 	}
7052b632970SJames Smart 
7062b632970SJames Smart 	/* nothing found - allocate a new remoteport struct */
7072b632970SJames Smart 
708e399441dSJames Smart 	newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
709e399441dSJames Smart 			 GFP_KERNEL);
710e399441dSJames Smart 	if (!newrec) {
711e399441dSJames Smart 		ret = -ENOMEM;
7122b632970SJames Smart 		goto out_lport_put;
713e399441dSJames Smart 	}
714e399441dSJames Smart 
715e399441dSJames Smart 	idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
716e399441dSJames Smart 	if (idx < 0) {
717e399441dSJames Smart 		ret = -ENOSPC;
7182b632970SJames Smart 		goto out_kfree_rport;
719e399441dSJames Smart 	}
720e399441dSJames Smart 
721e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->endp_list);
722e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->ctrl_list);
723c913a8b0SJames Smart 	INIT_LIST_HEAD(&newrec->ls_req_list);
72497faec53SJames Smart 	INIT_LIST_HEAD(&newrec->disc_list);
725e399441dSJames Smart 	kref_init(&newrec->ref);
726158bfb88SJames Smart 	atomic_set(&newrec->act_ctrl_cnt, 0);
727e399441dSJames Smart 	spin_lock_init(&newrec->lock);
728e399441dSJames Smart 	newrec->remoteport.localport = &lport->localport;
72914fd1e98SJames Smart 	INIT_LIST_HEAD(&newrec->ls_rcv_list);
730c913a8b0SJames Smart 	newrec->dev = lport->dev;
731c913a8b0SJames Smart 	newrec->lport = lport;
732f56bf76fSJames Smart 	if (lport->ops->remote_priv_sz)
733e399441dSJames Smart 		newrec->remoteport.private = &newrec[1];
734f56bf76fSJames Smart 	else
735f56bf76fSJames Smart 		newrec->remoteport.private = NULL;
736e399441dSJames Smart 	newrec->remoteport.port_role = pinfo->port_role;
737e399441dSJames Smart 	newrec->remoteport.node_name = pinfo->node_name;
738e399441dSJames Smart 	newrec->remoteport.port_name = pinfo->port_name;
739e399441dSJames Smart 	newrec->remoteport.port_id = pinfo->port_id;
740e399441dSJames Smart 	newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
741e399441dSJames Smart 	newrec->remoteport.port_num = idx;
7422b632970SJames Smart 	__nvme_fc_set_dev_loss_tmo(newrec, pinfo);
74314fd1e98SJames Smart 	INIT_WORK(&newrec->lsrcv_work, nvme_fc_handle_ls_rqst_work);
744e399441dSJames Smart 
745e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
746e399441dSJames Smart 	list_add_tail(&newrec->endp_list, &lport->endp_list);
747e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
748e399441dSJames Smart 
749eaefd5abSJames Smart 	nvme_fc_signal_discovery_scan(lport, newrec);
750eaefd5abSJames Smart 
751e399441dSJames Smart 	*portptr = &newrec->remoteport;
752e399441dSJames Smart 	return 0;
753e399441dSJames Smart 
754e399441dSJames Smart out_kfree_rport:
755e399441dSJames Smart 	kfree(newrec);
7562b632970SJames Smart out_lport_put:
7572b632970SJames Smart 	nvme_fc_lport_put(lport);
758e399441dSJames Smart out_reghost_failed:
759e399441dSJames Smart 	*portptr = NULL;
760e399441dSJames Smart 	return ret;
761e399441dSJames Smart }
762e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
763e399441dSJames Smart 
7648d64daf7SJames Smart static int
7658d64daf7SJames Smart nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
7668d64daf7SJames Smart {
7678d64daf7SJames Smart 	struct nvmefc_ls_req_op *lsop;
7688d64daf7SJames Smart 	unsigned long flags;
7698d64daf7SJames Smart 
7708d64daf7SJames Smart restart:
7718d64daf7SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
7728d64daf7SJames Smart 
7738d64daf7SJames Smart 	list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
7748d64daf7SJames Smart 		if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
7758d64daf7SJames Smart 			lsop->flags |= FCOP_FLAGS_TERMIO;
7768d64daf7SJames Smart 			spin_unlock_irqrestore(&rport->lock, flags);
7778d64daf7SJames Smart 			rport->lport->ops->ls_abort(&rport->lport->localport,
7788d64daf7SJames Smart 						&rport->remoteport,
7798d64daf7SJames Smart 						&lsop->ls_req);
7808d64daf7SJames Smart 			goto restart;
7818d64daf7SJames Smart 		}
7828d64daf7SJames Smart 	}
7838d64daf7SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
7848d64daf7SJames Smart 
7858d64daf7SJames Smart 	return 0;
7868d64daf7SJames Smart }
7878d64daf7SJames Smart 
7882b632970SJames Smart static void
7892b632970SJames Smart nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
7902b632970SJames Smart {
7912b632970SJames Smart 	dev_info(ctrl->ctrl.device,
7922b632970SJames Smart 		"NVME-FC{%d}: controller connectivity lost. Awaiting "
7932b632970SJames Smart 		"Reconnect", ctrl->cnum);
7942b632970SJames Smart 
7952b632970SJames Smart 	switch (ctrl->ctrl.state) {
7962b632970SJames Smart 	case NVME_CTRL_NEW:
7972b632970SJames Smart 	case NVME_CTRL_LIVE:
7982b632970SJames Smart 		/*
7992b632970SJames Smart 		 * Schedule a controller reset. The reset will terminate the
8002b632970SJames Smart 		 * association and schedule the reconnect timer.  Reconnects
8012b632970SJames Smart 		 * will be attempted until either the ctlr_loss_tmo
8022b632970SJames Smart 		 * (max_retries * connect_delay) expires or the remoteport's
8032b632970SJames Smart 		 * dev_loss_tmo expires.
8042b632970SJames Smart 		 */
8052b632970SJames Smart 		if (nvme_reset_ctrl(&ctrl->ctrl)) {
8062b632970SJames Smart 			dev_warn(ctrl->ctrl.device,
80777d0612dSMax Gurtovoy 				"NVME-FC{%d}: Couldn't schedule reset.\n",
8082b632970SJames Smart 				ctrl->cnum);
8092b632970SJames Smart 			nvme_delete_ctrl(&ctrl->ctrl);
8102b632970SJames Smart 		}
8112b632970SJames Smart 		break;
8122b632970SJames Smart 
813ad6a0a52SMax Gurtovoy 	case NVME_CTRL_CONNECTING:
8142b632970SJames Smart 		/*
8152b632970SJames Smart 		 * The association has already been terminated and the
8162b632970SJames Smart 		 * controller is attempting reconnects.  No need to do anything
8172b632970SJames Smart 		 * futher.  Reconnects will be attempted until either the
8182b632970SJames Smart 		 * ctlr_loss_tmo (max_retries * connect_delay) expires or the
8192b632970SJames Smart 		 * remoteport's dev_loss_tmo expires.
8202b632970SJames Smart 		 */
8212b632970SJames Smart 		break;
8222b632970SJames Smart 
8232b632970SJames Smart 	case NVME_CTRL_RESETTING:
8242b632970SJames Smart 		/*
8252b632970SJames Smart 		 * Controller is already in the process of terminating the
8262b632970SJames Smart 		 * association.  No need to do anything further. The reconnect
8272b632970SJames Smart 		 * step will kick in naturally after the association is
8282b632970SJames Smart 		 * terminated.
8292b632970SJames Smart 		 */
8302b632970SJames Smart 		break;
8312b632970SJames Smart 
8322b632970SJames Smart 	case NVME_CTRL_DELETING:
833ecca390eSSagi Grimberg 	case NVME_CTRL_DELETING_NOIO:
8342b632970SJames Smart 	default:
8352b632970SJames Smart 		/* no action to take - let it delete */
8362b632970SJames Smart 		break;
8372b632970SJames Smart 	}
8382b632970SJames Smart }
8392b632970SJames Smart 
840e399441dSJames Smart /**
841e399441dSJames Smart  * nvme_fc_unregister_remoteport - transport entry point called by an
842e399441dSJames Smart  *                              LLDD to deregister/remove a previously
843e399441dSJames Smart  *                              registered a NVME subsystem FC port.
84476c910c7SBart Van Assche  * @portptr: pointer to the (registered) remote port that is to be
845e399441dSJames Smart  *           deregistered.
846e399441dSJames Smart  *
847e399441dSJames Smart  * Returns:
848e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
849e399441dSJames Smart  * (ex: -ENXIO) upon failure.
850e399441dSJames Smart  */
851e399441dSJames Smart int
852e399441dSJames Smart nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
853e399441dSJames Smart {
854e399441dSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
855e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl;
856e399441dSJames Smart 	unsigned long flags;
857e399441dSJames Smart 
858e399441dSJames Smart 	if (!portptr)
859e399441dSJames Smart 		return -EINVAL;
860e399441dSJames Smart 
861e399441dSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
862e399441dSJames Smart 
863e399441dSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
864e399441dSJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
865e399441dSJames Smart 		return -EINVAL;
866e399441dSJames Smart 	}
867e399441dSJames Smart 	portptr->port_state = FC_OBJSTATE_DELETED;
868e399441dSJames Smart 
8692b632970SJames Smart 	rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
8702b632970SJames Smart 
8712b632970SJames Smart 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
8722b632970SJames Smart 		/* if dev_loss_tmo==0, dev loss is immediate */
8732b632970SJames Smart 		if (!portptr->dev_loss_tmo) {
8742b632970SJames Smart 			dev_warn(ctrl->ctrl.device,
87577d0612dSMax Gurtovoy 				"NVME-FC{%d}: controller connectivity lost.\n",
8762b632970SJames Smart 				ctrl->cnum);
877c5017e85SChristoph Hellwig 			nvme_delete_ctrl(&ctrl->ctrl);
8782b632970SJames Smart 		} else
8792b632970SJames Smart 			nvme_fc_ctrl_connectivity_loss(ctrl);
8802b632970SJames Smart 	}
881e399441dSJames Smart 
882e399441dSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
883e399441dSJames Smart 
8848d64daf7SJames Smart 	nvme_fc_abort_lsops(rport);
8858d64daf7SJames Smart 
886158bfb88SJames Smart 	if (atomic_read(&rport->act_ctrl_cnt) == 0)
887158bfb88SJames Smart 		rport->lport->ops->remoteport_delete(portptr);
888158bfb88SJames Smart 
8892b632970SJames Smart 	/*
8902b632970SJames Smart 	 * release the reference, which will allow, if all controllers
8912b632970SJames Smart 	 * go away, which should only occur after dev_loss_tmo occurs,
8922b632970SJames Smart 	 * for the rport to be torn down.
8932b632970SJames Smart 	 */
894e399441dSJames Smart 	nvme_fc_rport_put(rport);
8952b632970SJames Smart 
896e399441dSJames Smart 	return 0;
897e399441dSJames Smart }
898e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
899e399441dSJames Smart 
900eaefd5abSJames Smart /**
901eaefd5abSJames Smart  * nvme_fc_rescan_remoteport - transport entry point called by an
902eaefd5abSJames Smart  *                              LLDD to request a nvme device rescan.
903eaefd5abSJames Smart  * @remoteport: pointer to the (registered) remote port that is to be
904eaefd5abSJames Smart  *              rescanned.
905eaefd5abSJames Smart  *
906eaefd5abSJames Smart  * Returns: N/A
907eaefd5abSJames Smart  */
908eaefd5abSJames Smart void
909eaefd5abSJames Smart nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
910eaefd5abSJames Smart {
911eaefd5abSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
912eaefd5abSJames Smart 
913eaefd5abSJames Smart 	nvme_fc_signal_discovery_scan(rport->lport, rport);
914eaefd5abSJames Smart }
915eaefd5abSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
916eaefd5abSJames Smart 
917ac7fe82bSJames Smart int
918ac7fe82bSJames Smart nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
919ac7fe82bSJames Smart 			u32 dev_loss_tmo)
920ac7fe82bSJames Smart {
921ac7fe82bSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
922ac7fe82bSJames Smart 	unsigned long flags;
923ac7fe82bSJames Smart 
924ac7fe82bSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
925ac7fe82bSJames Smart 
926ac7fe82bSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
927ac7fe82bSJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
928ac7fe82bSJames Smart 		return -EINVAL;
929ac7fe82bSJames Smart 	}
930ac7fe82bSJames Smart 
931ac7fe82bSJames Smart 	/* a dev_loss_tmo of 0 (immediate) is allowed to be set */
932ac7fe82bSJames Smart 	rport->remoteport.dev_loss_tmo = dev_loss_tmo;
933ac7fe82bSJames Smart 
934ac7fe82bSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
935ac7fe82bSJames Smart 
936ac7fe82bSJames Smart 	return 0;
937ac7fe82bSJames Smart }
938ac7fe82bSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
939ac7fe82bSJames Smart 
940e399441dSJames Smart 
941e399441dSJames Smart /* *********************** FC-NVME DMA Handling **************************** */
942e399441dSJames Smart 
943e399441dSJames Smart /*
944e399441dSJames Smart  * The fcloop device passes in a NULL device pointer. Real LLD's will
945e399441dSJames Smart  * pass in a valid device pointer. If NULL is passed to the dma mapping
946e399441dSJames Smart  * routines, depending on the platform, it may or may not succeed, and
947e399441dSJames Smart  * may crash.
948e399441dSJames Smart  *
949e399441dSJames Smart  * As such:
950e399441dSJames Smart  * Wrapper all the dma routines and check the dev pointer.
951e399441dSJames Smart  *
952e399441dSJames Smart  * If simple mappings (return just a dma address, we'll noop them,
953e399441dSJames Smart  * returning a dma address of 0.
954e399441dSJames Smart  *
955e399441dSJames Smart  * On more complex mappings (dma_map_sg), a pseudo routine fills
956e399441dSJames Smart  * in the scatter list, setting all dma addresses to 0.
957e399441dSJames Smart  */
958e399441dSJames Smart 
959e399441dSJames Smart static inline dma_addr_t
960e399441dSJames Smart fc_dma_map_single(struct device *dev, void *ptr, size_t size,
961e399441dSJames Smart 		enum dma_data_direction dir)
962e399441dSJames Smart {
963e399441dSJames Smart 	return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
964e399441dSJames Smart }
965e399441dSJames Smart 
966e399441dSJames Smart static inline int
967e399441dSJames Smart fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
968e399441dSJames Smart {
969e399441dSJames Smart 	return dev ? dma_mapping_error(dev, dma_addr) : 0;
970e399441dSJames Smart }
971e399441dSJames Smart 
972e399441dSJames Smart static inline void
973e399441dSJames Smart fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
974e399441dSJames Smart 	enum dma_data_direction dir)
975e399441dSJames Smart {
976e399441dSJames Smart 	if (dev)
977e399441dSJames Smart 		dma_unmap_single(dev, addr, size, dir);
978e399441dSJames Smart }
979e399441dSJames Smart 
980e399441dSJames Smart static inline void
981e399441dSJames Smart fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
982e399441dSJames Smart 		enum dma_data_direction dir)
983e399441dSJames Smart {
984e399441dSJames Smart 	if (dev)
985e399441dSJames Smart 		dma_sync_single_for_cpu(dev, addr, size, dir);
986e399441dSJames Smart }
987e399441dSJames Smart 
988e399441dSJames Smart static inline void
989e399441dSJames Smart fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
990e399441dSJames Smart 		enum dma_data_direction dir)
991e399441dSJames Smart {
992e399441dSJames Smart 	if (dev)
993e399441dSJames Smart 		dma_sync_single_for_device(dev, addr, size, dir);
994e399441dSJames Smart }
995e399441dSJames Smart 
996e399441dSJames Smart /* pseudo dma_map_sg call */
997e399441dSJames Smart static int
998e399441dSJames Smart fc_map_sg(struct scatterlist *sg, int nents)
999e399441dSJames Smart {
1000e399441dSJames Smart 	struct scatterlist *s;
1001e399441dSJames Smart 	int i;
1002e399441dSJames Smart 
1003e399441dSJames Smart 	WARN_ON(nents == 0 || sg[0].length == 0);
1004e399441dSJames Smart 
1005e399441dSJames Smart 	for_each_sg(sg, s, nents, i) {
1006e399441dSJames Smart 		s->dma_address = 0L;
1007e399441dSJames Smart #ifdef CONFIG_NEED_SG_DMA_LENGTH
1008e399441dSJames Smart 		s->dma_length = s->length;
1009e399441dSJames Smart #endif
1010e399441dSJames Smart 	}
1011e399441dSJames Smart 	return nents;
1012e399441dSJames Smart }
1013e399441dSJames Smart 
1014e399441dSJames Smart static inline int
1015e399441dSJames Smart fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1016e399441dSJames Smart 		enum dma_data_direction dir)
1017e399441dSJames Smart {
1018e399441dSJames Smart 	return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
1019e399441dSJames Smart }
1020e399441dSJames Smart 
1021e399441dSJames Smart static inline void
1022e399441dSJames Smart fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1023e399441dSJames Smart 		enum dma_data_direction dir)
1024e399441dSJames Smart {
1025e399441dSJames Smart 	if (dev)
1026e399441dSJames Smart 		dma_unmap_sg(dev, sg, nents, dir);
1027e399441dSJames Smart }
1028e399441dSJames Smart 
1029e399441dSJames Smart /* *********************** FC-NVME LS Handling **************************** */
1030e399441dSJames Smart 
1031e399441dSJames Smart static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
1032e399441dSJames Smart static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
1033e399441dSJames Smart 
103414fd1e98SJames Smart static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
1035e399441dSJames Smart 
1036e399441dSJames Smart static void
1037c913a8b0SJames Smart __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
1038e399441dSJames Smart {
1039c913a8b0SJames Smart 	struct nvme_fc_rport *rport = lsop->rport;
1040e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1041e399441dSJames Smart 	unsigned long flags;
1042e399441dSJames Smart 
1043c913a8b0SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
1044e399441dSJames Smart 
1045e399441dSJames Smart 	if (!lsop->req_queued) {
1046c913a8b0SJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
1047e399441dSJames Smart 		return;
1048e399441dSJames Smart 	}
1049e399441dSJames Smart 
1050e399441dSJames Smart 	list_del(&lsop->lsreq_list);
1051e399441dSJames Smart 
1052e399441dSJames Smart 	lsop->req_queued = false;
1053e399441dSJames Smart 
1054c913a8b0SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
1055e399441dSJames Smart 
1056c913a8b0SJames Smart 	fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1057e399441dSJames Smart 				  (lsreq->rqstlen + lsreq->rsplen),
1058e399441dSJames Smart 				  DMA_BIDIRECTIONAL);
1059e399441dSJames Smart 
1060c913a8b0SJames Smart 	nvme_fc_rport_put(rport);
1061e399441dSJames Smart }
1062e399441dSJames Smart 
1063e399441dSJames Smart static int
1064c913a8b0SJames Smart __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
1065e399441dSJames Smart 		struct nvmefc_ls_req_op *lsop,
1066e399441dSJames Smart 		void (*done)(struct nvmefc_ls_req *req, int status))
1067e399441dSJames Smart {
1068e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1069e399441dSJames Smart 	unsigned long flags;
1070c913a8b0SJames Smart 	int ret = 0;
1071e399441dSJames Smart 
1072c913a8b0SJames Smart 	if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1073c913a8b0SJames Smart 		return -ECONNREFUSED;
1074c913a8b0SJames Smart 
1075c913a8b0SJames Smart 	if (!nvme_fc_rport_get(rport))
1076e399441dSJames Smart 		return -ESHUTDOWN;
1077e399441dSJames Smart 
1078e399441dSJames Smart 	lsreq->done = done;
1079c913a8b0SJames Smart 	lsop->rport = rport;
1080e399441dSJames Smart 	lsop->req_queued = false;
1081e399441dSJames Smart 	INIT_LIST_HEAD(&lsop->lsreq_list);
1082e399441dSJames Smart 	init_completion(&lsop->ls_done);
1083e399441dSJames Smart 
1084c913a8b0SJames Smart 	lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
1085e399441dSJames Smart 				  lsreq->rqstlen + lsreq->rsplen,
1086e399441dSJames Smart 				  DMA_BIDIRECTIONAL);
1087c913a8b0SJames Smart 	if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1088c913a8b0SJames Smart 		ret = -EFAULT;
1089c913a8b0SJames Smart 		goto out_putrport;
1090e399441dSJames Smart 	}
1091e399441dSJames Smart 	lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1092e399441dSJames Smart 
1093c913a8b0SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
1094e399441dSJames Smart 
1095c913a8b0SJames Smart 	list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
1096e399441dSJames Smart 
1097e399441dSJames Smart 	lsop->req_queued = true;
1098e399441dSJames Smart 
1099c913a8b0SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
1100e399441dSJames Smart 
1101c913a8b0SJames Smart 	ret = rport->lport->ops->ls_req(&rport->lport->localport,
1102c913a8b0SJames Smart 					&rport->remoteport, lsreq);
1103e399441dSJames Smart 	if (ret)
1104c913a8b0SJames Smart 		goto out_unlink;
1105c913a8b0SJames Smart 
1106c913a8b0SJames Smart 	return 0;
1107c913a8b0SJames Smart 
1108c913a8b0SJames Smart out_unlink:
1109e399441dSJames Smart 	lsop->ls_error = ret;
1110c913a8b0SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
1111c913a8b0SJames Smart 	lsop->req_queued = false;
1112c913a8b0SJames Smart 	list_del(&lsop->lsreq_list);
1113c913a8b0SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
1114c913a8b0SJames Smart 	fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1115c913a8b0SJames Smart 				  (lsreq->rqstlen + lsreq->rsplen),
1116c913a8b0SJames Smart 				  DMA_BIDIRECTIONAL);
1117c913a8b0SJames Smart out_putrport:
1118c913a8b0SJames Smart 	nvme_fc_rport_put(rport);
1119e399441dSJames Smart 
1120e399441dSJames Smart 	return ret;
1121e399441dSJames Smart }
1122e399441dSJames Smart 
1123e399441dSJames Smart static void
1124e399441dSJames Smart nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1125e399441dSJames Smart {
1126e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1127e399441dSJames Smart 
1128e399441dSJames Smart 	lsop->ls_error = status;
1129e399441dSJames Smart 	complete(&lsop->ls_done);
1130e399441dSJames Smart }
1131e399441dSJames Smart 
1132e399441dSJames Smart static int
1133c913a8b0SJames Smart nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
1134e399441dSJames Smart {
1135e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1136e399441dSJames Smart 	struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1137e399441dSJames Smart 	int ret;
1138e399441dSJames Smart 
1139c913a8b0SJames Smart 	ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
1140e399441dSJames Smart 
1141c913a8b0SJames Smart 	if (!ret) {
1142e399441dSJames Smart 		/*
1143e399441dSJames Smart 		 * No timeout/not interruptible as we need the struct
1144e399441dSJames Smart 		 * to exist until the lldd calls us back. Thus mandate
1145e399441dSJames Smart 		 * wait until driver calls back. lldd responsible for
1146e399441dSJames Smart 		 * the timeout action
1147e399441dSJames Smart 		 */
1148e399441dSJames Smart 		wait_for_completion(&lsop->ls_done);
1149e399441dSJames Smart 
1150c913a8b0SJames Smart 		__nvme_fc_finish_ls_req(lsop);
1151e399441dSJames Smart 
1152c913a8b0SJames Smart 		ret = lsop->ls_error;
1153e399441dSJames Smart 	}
1154e399441dSJames Smart 
1155c913a8b0SJames Smart 	if (ret)
1156c913a8b0SJames Smart 		return ret;
1157c913a8b0SJames Smart 
1158e399441dSJames Smart 	/* ACC or RJT payload ? */
1159e399441dSJames Smart 	if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1160e399441dSJames Smart 		return -ENXIO;
1161e399441dSJames Smart 
1162e399441dSJames Smart 	return 0;
1163e399441dSJames Smart }
1164e399441dSJames Smart 
1165c913a8b0SJames Smart static int
1166c913a8b0SJames Smart nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
1167e399441dSJames Smart 		struct nvmefc_ls_req_op *lsop,
1168e399441dSJames Smart 		void (*done)(struct nvmefc_ls_req *req, int status))
1169e399441dSJames Smart {
1170e399441dSJames Smart 	/* don't wait for completion */
1171e399441dSJames Smart 
1172c913a8b0SJames Smart 	return __nvme_fc_send_ls_req(rport, lsop, done);
1173e399441dSJames Smart }
1174e399441dSJames Smart 
1175e399441dSJames Smart static int
1176e399441dSJames Smart nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1177e399441dSJames Smart 	struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1178e399441dSJames Smart {
1179e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
1180e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
1181e399441dSJames Smart 	struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1182e399441dSJames Smart 	struct fcnvme_ls_cr_assoc_acc *assoc_acc;
118314fd1e98SJames Smart 	unsigned long flags;
1184e399441dSJames Smart 	int ret, fcret = 0;
1185e399441dSJames Smart 
1186e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
1187f56bf76fSJames Smart 			 sizeof(*assoc_rqst) + sizeof(*assoc_acc) +
1188f56bf76fSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1189e399441dSJames Smart 	if (!lsop) {
1190f56bf76fSJames Smart 		dev_info(ctrl->ctrl.device,
1191f56bf76fSJames Smart 			"NVME-FC{%d}: send Create Association failed: ENOMEM\n",
1192f56bf76fSJames Smart 			ctrl->cnum);
1193e399441dSJames Smart 		ret = -ENOMEM;
1194e399441dSJames Smart 		goto out_no_memory;
1195e399441dSJames Smart 	}
1196e399441dSJames Smart 
1197f56bf76fSJames Smart 	assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1];
1198e399441dSJames Smart 	assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1199f56bf76fSJames Smart 	lsreq = &lsop->ls_req;
1200f56bf76fSJames Smart 	if (ctrl->lport->ops->lsrqst_priv_sz)
1201f56bf76fSJames Smart 		lsreq->private = &assoc_acc[1];
1202f56bf76fSJames Smart 	else
1203f56bf76fSJames Smart 		lsreq->private = NULL;
1204e399441dSJames Smart 
1205e399441dSJames Smart 	assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1206e399441dSJames Smart 	assoc_rqst->desc_list_len =
1207e399441dSJames Smart 			cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1208e399441dSJames Smart 
1209e399441dSJames Smart 	assoc_rqst->assoc_cmd.desc_tag =
1210e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1211e399441dSJames Smart 	assoc_rqst->assoc_cmd.desc_len =
1212e399441dSJames Smart 			fcnvme_lsdesc_len(
1213e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1214e399441dSJames Smart 
1215e399441dSJames Smart 	assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1216d157e534SJames Smart 	assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
1217e399441dSJames Smart 	/* Linux supports only Dynamic controllers */
1218e399441dSJames Smart 	assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
12198e412263SChristoph Hellwig 	uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1220e399441dSJames Smart 	strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1221e399441dSJames Smart 		min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1222e399441dSJames Smart 	strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1223e399441dSJames Smart 		min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
1224e399441dSJames Smart 
1225e399441dSJames Smart 	lsop->queue = queue;
1226e399441dSJames Smart 	lsreq->rqstaddr = assoc_rqst;
1227e399441dSJames Smart 	lsreq->rqstlen = sizeof(*assoc_rqst);
1228e399441dSJames Smart 	lsreq->rspaddr = assoc_acc;
1229e399441dSJames Smart 	lsreq->rsplen = sizeof(*assoc_acc);
123053b2b2f5SJames Smart 	lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1231e399441dSJames Smart 
1232c913a8b0SJames Smart 	ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1233e399441dSJames Smart 	if (ret)
1234e399441dSJames Smart 		goto out_free_buffer;
1235e399441dSJames Smart 
1236e399441dSJames Smart 	/* process connect LS completion */
1237e399441dSJames Smart 
1238e399441dSJames Smart 	/* validate the ACC response */
1239e399441dSJames Smart 	if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1240e399441dSJames Smart 		fcret = VERR_LSACC;
1241f77fc87cSJames Smart 	else if (assoc_acc->hdr.desc_list_len !=
1242e399441dSJames Smart 			fcnvme_lsdesc_len(
1243e399441dSJames Smart 				sizeof(struct fcnvme_ls_cr_assoc_acc)))
1244e399441dSJames Smart 		fcret = VERR_CR_ASSOC_ACC_LEN;
1245f77fc87cSJames Smart 	else if (assoc_acc->hdr.rqst.desc_tag !=
1246f77fc87cSJames Smart 			cpu_to_be32(FCNVME_LSDESC_RQST))
1247e399441dSJames Smart 		fcret = VERR_LSDESC_RQST;
1248e399441dSJames Smart 	else if (assoc_acc->hdr.rqst.desc_len !=
1249e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1250e399441dSJames Smart 		fcret = VERR_LSDESC_RQST_LEN;
1251e399441dSJames Smart 	else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1252e399441dSJames Smart 		fcret = VERR_CR_ASSOC;
1253e399441dSJames Smart 	else if (assoc_acc->associd.desc_tag !=
1254e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1255e399441dSJames Smart 		fcret = VERR_ASSOC_ID;
1256e399441dSJames Smart 	else if (assoc_acc->associd.desc_len !=
1257e399441dSJames Smart 			fcnvme_lsdesc_len(
1258e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id)))
1259e399441dSJames Smart 		fcret = VERR_ASSOC_ID_LEN;
1260e399441dSJames Smart 	else if (assoc_acc->connectid.desc_tag !=
1261e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1262e399441dSJames Smart 		fcret = VERR_CONN_ID;
1263e399441dSJames Smart 	else if (assoc_acc->connectid.desc_len !=
1264e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1265e399441dSJames Smart 		fcret = VERR_CONN_ID_LEN;
1266e399441dSJames Smart 
1267e399441dSJames Smart 	if (fcret) {
1268e399441dSJames Smart 		ret = -EBADF;
1269e399441dSJames Smart 		dev_err(ctrl->dev,
12707db39484SJames Smart 			"q %d Create Association LS failed: %s\n",
1271e399441dSJames Smart 			queue->qnum, validation_errors[fcret]);
1272e399441dSJames Smart 	} else {
127314fd1e98SJames Smart 		spin_lock_irqsave(&ctrl->lock, flags);
1274e399441dSJames Smart 		ctrl->association_id =
1275e399441dSJames Smart 			be64_to_cpu(assoc_acc->associd.association_id);
1276e399441dSJames Smart 		queue->connection_id =
1277e399441dSJames Smart 			be64_to_cpu(assoc_acc->connectid.connection_id);
1278e399441dSJames Smart 		set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
127914fd1e98SJames Smart 		spin_unlock_irqrestore(&ctrl->lock, flags);
1280e399441dSJames Smart 	}
1281e399441dSJames Smart 
1282e399441dSJames Smart out_free_buffer:
1283e399441dSJames Smart 	kfree(lsop);
1284e399441dSJames Smart out_no_memory:
1285e399441dSJames Smart 	if (ret)
1286e399441dSJames Smart 		dev_err(ctrl->dev,
1287e399441dSJames Smart 			"queue %d connect admin queue failed (%d).\n",
1288e399441dSJames Smart 			queue->qnum, ret);
1289e399441dSJames Smart 	return ret;
1290e399441dSJames Smart }
1291e399441dSJames Smart 
1292e399441dSJames Smart static int
1293e399441dSJames Smart nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1294e399441dSJames Smart 			u16 qsize, u16 ersp_ratio)
1295e399441dSJames Smart {
1296e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
1297e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
1298e399441dSJames Smart 	struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1299e399441dSJames Smart 	struct fcnvme_ls_cr_conn_acc *conn_acc;
1300e399441dSJames Smart 	int ret, fcret = 0;
1301e399441dSJames Smart 
1302e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
1303f56bf76fSJames Smart 			 sizeof(*conn_rqst) + sizeof(*conn_acc) +
1304f56bf76fSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1305e399441dSJames Smart 	if (!lsop) {
1306f56bf76fSJames Smart 		dev_info(ctrl->ctrl.device,
1307f56bf76fSJames Smart 			"NVME-FC{%d}: send Create Connection failed: ENOMEM\n",
1308f56bf76fSJames Smart 			ctrl->cnum);
1309e399441dSJames Smart 		ret = -ENOMEM;
1310e399441dSJames Smart 		goto out_no_memory;
1311e399441dSJames Smart 	}
1312e399441dSJames Smart 
1313f56bf76fSJames Smart 	conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)&lsop[1];
1314e399441dSJames Smart 	conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1315f56bf76fSJames Smart 	lsreq = &lsop->ls_req;
1316f56bf76fSJames Smart 	if (ctrl->lport->ops->lsrqst_priv_sz)
1317f56bf76fSJames Smart 		lsreq->private = (void *)&conn_acc[1];
1318f56bf76fSJames Smart 	else
1319f56bf76fSJames Smart 		lsreq->private = NULL;
1320e399441dSJames Smart 
1321e399441dSJames Smart 	conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1322e399441dSJames Smart 	conn_rqst->desc_list_len = cpu_to_be32(
1323e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id) +
1324e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1325e399441dSJames Smart 
1326e399441dSJames Smart 	conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1327e399441dSJames Smart 	conn_rqst->associd.desc_len =
1328e399441dSJames Smart 			fcnvme_lsdesc_len(
1329e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id));
1330e399441dSJames Smart 	conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1331e399441dSJames Smart 	conn_rqst->connect_cmd.desc_tag =
1332e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1333e399441dSJames Smart 	conn_rqst->connect_cmd.desc_len =
1334e399441dSJames Smart 			fcnvme_lsdesc_len(
1335e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1336e399441dSJames Smart 	conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1337e399441dSJames Smart 	conn_rqst->connect_cmd.qid  = cpu_to_be16(queue->qnum);
1338d157e534SJames Smart 	conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
1339e399441dSJames Smart 
1340e399441dSJames Smart 	lsop->queue = queue;
1341e399441dSJames Smart 	lsreq->rqstaddr = conn_rqst;
1342e399441dSJames Smart 	lsreq->rqstlen = sizeof(*conn_rqst);
1343e399441dSJames Smart 	lsreq->rspaddr = conn_acc;
1344e399441dSJames Smart 	lsreq->rsplen = sizeof(*conn_acc);
134553b2b2f5SJames Smart 	lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1346e399441dSJames Smart 
1347c913a8b0SJames Smart 	ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1348e399441dSJames Smart 	if (ret)
1349e399441dSJames Smart 		goto out_free_buffer;
1350e399441dSJames Smart 
1351e399441dSJames Smart 	/* process connect LS completion */
1352e399441dSJames Smart 
1353e399441dSJames Smart 	/* validate the ACC response */
1354e399441dSJames Smart 	if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1355e399441dSJames Smart 		fcret = VERR_LSACC;
1356f77fc87cSJames Smart 	else if (conn_acc->hdr.desc_list_len !=
1357e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1358e399441dSJames Smart 		fcret = VERR_CR_CONN_ACC_LEN;
1359f77fc87cSJames Smart 	else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1360e399441dSJames Smart 		fcret = VERR_LSDESC_RQST;
1361e399441dSJames Smart 	else if (conn_acc->hdr.rqst.desc_len !=
1362e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1363e399441dSJames Smart 		fcret = VERR_LSDESC_RQST_LEN;
1364e399441dSJames Smart 	else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1365e399441dSJames Smart 		fcret = VERR_CR_CONN;
1366e399441dSJames Smart 	else if (conn_acc->connectid.desc_tag !=
1367e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1368e399441dSJames Smart 		fcret = VERR_CONN_ID;
1369e399441dSJames Smart 	else if (conn_acc->connectid.desc_len !=
1370e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1371e399441dSJames Smart 		fcret = VERR_CONN_ID_LEN;
1372e399441dSJames Smart 
1373e399441dSJames Smart 	if (fcret) {
1374e399441dSJames Smart 		ret = -EBADF;
1375e399441dSJames Smart 		dev_err(ctrl->dev,
13767db39484SJames Smart 			"q %d Create I/O Connection LS failed: %s\n",
1377e399441dSJames Smart 			queue->qnum, validation_errors[fcret]);
1378e399441dSJames Smart 	} else {
1379e399441dSJames Smart 		queue->connection_id =
1380e399441dSJames Smart 			be64_to_cpu(conn_acc->connectid.connection_id);
1381e399441dSJames Smart 		set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1382e399441dSJames Smart 	}
1383e399441dSJames Smart 
1384e399441dSJames Smart out_free_buffer:
1385e399441dSJames Smart 	kfree(lsop);
1386e399441dSJames Smart out_no_memory:
1387e399441dSJames Smart 	if (ret)
1388e399441dSJames Smart 		dev_err(ctrl->dev,
13897db39484SJames Smart 			"queue %d connect I/O queue failed (%d).\n",
1390e399441dSJames Smart 			queue->qnum, ret);
1391e399441dSJames Smart 	return ret;
1392e399441dSJames Smart }
1393e399441dSJames Smart 
1394e399441dSJames Smart static void
1395e399441dSJames Smart nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1396e399441dSJames Smart {
1397e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1398e399441dSJames Smart 
1399c913a8b0SJames Smart 	__nvme_fc_finish_ls_req(lsop);
1400e399441dSJames Smart 
1401d4e4230cSMilan P. Gandhi 	/* fc-nvme initiator doesn't care about success or failure of cmd */
1402e399441dSJames Smart 
1403e399441dSJames Smart 	kfree(lsop);
1404e399441dSJames Smart }
1405e399441dSJames Smart 
1406e399441dSJames Smart /*
1407e399441dSJames Smart  * This routine sends a FC-NVME LS to disconnect (aka terminate)
1408e399441dSJames Smart  * the FC-NVME Association.  Terminating the association also
1409e399441dSJames Smart  * terminates the FC-NVME connections (per queue, both admin and io
1410e399441dSJames Smart  * queues) that are part of the association. E.g. things are torn
1411e399441dSJames Smart  * down, and the related FC-NVME Association ID and Connection IDs
1412e399441dSJames Smart  * become invalid.
1413e399441dSJames Smart  *
1414e399441dSJames Smart  * The behavior of the fc-nvme initiator is such that it's
1415e399441dSJames Smart  * understanding of the association and connections will implicitly
1416e399441dSJames Smart  * be torn down. The action is implicit as it may be due to a loss of
1417e399441dSJames Smart  * connectivity with the fc-nvme target, so you may never get a
1418e399441dSJames Smart  * response even if you tried.  As such, the action of this routine
1419e399441dSJames Smart  * is to asynchronously send the LS, ignore any results of the LS, and
1420e399441dSJames Smart  * continue on with terminating the association. If the fc-nvme target
1421e399441dSJames Smart  * is present and receives the LS, it too can tear down.
1422e399441dSJames Smart  */
1423e399441dSJames Smart static void
1424e399441dSJames Smart nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1425e399441dSJames Smart {
142653b2b2f5SJames Smart 	struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
142753b2b2f5SJames Smart 	struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
1428e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
1429e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
1430c913a8b0SJames Smart 	int ret;
1431e399441dSJames Smart 
1432e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
1433f56bf76fSJames Smart 			sizeof(*discon_rqst) + sizeof(*discon_acc) +
1434f56bf76fSJames Smart 			ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1435f56bf76fSJames Smart 	if (!lsop) {
1436f56bf76fSJames Smart 		dev_info(ctrl->ctrl.device,
1437f56bf76fSJames Smart 			"NVME-FC{%d}: send Disconnect Association "
1438f56bf76fSJames Smart 			"failed: ENOMEM\n",
1439f56bf76fSJames Smart 			ctrl->cnum);
1440e399441dSJames Smart 		return;
1441f56bf76fSJames Smart 	}
1442e399441dSJames Smart 
1443f56bf76fSJames Smart 	discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)&lsop[1];
144453b2b2f5SJames Smart 	discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
1445f56bf76fSJames Smart 	lsreq = &lsop->ls_req;
1446f56bf76fSJames Smart 	if (ctrl->lport->ops->lsrqst_priv_sz)
1447f56bf76fSJames Smart 		lsreq->private = (void *)&discon_acc[1];
1448f56bf76fSJames Smart 	else
1449f56bf76fSJames Smart 		lsreq->private = NULL;
1450e399441dSJames Smart 
1451fd5a5f22SJames Smart 	nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc,
1452fd5a5f22SJames Smart 				ctrl->association_id);
1453e399441dSJames Smart 
1454c913a8b0SJames Smart 	ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1455c913a8b0SJames Smart 				nvme_fc_disconnect_assoc_done);
1456c913a8b0SJames Smart 	if (ret)
1457c913a8b0SJames Smart 		kfree(lsop);
1458e399441dSJames Smart }
1459e399441dSJames Smart 
146014fd1e98SJames Smart static void
146114fd1e98SJames Smart nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
146214fd1e98SJames Smart {
146314fd1e98SJames Smart 	struct nvmefc_ls_rcv_op *lsop = lsrsp->nvme_fc_private;
146414fd1e98SJames Smart 	struct nvme_fc_rport *rport = lsop->rport;
146514fd1e98SJames Smart 	struct nvme_fc_lport *lport = rport->lport;
146614fd1e98SJames Smart 	unsigned long flags;
146714fd1e98SJames Smart 
146814fd1e98SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
146914fd1e98SJames Smart 	list_del(&lsop->lsrcv_list);
147014fd1e98SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
147114fd1e98SJames Smart 
147214fd1e98SJames Smart 	fc_dma_sync_single_for_cpu(lport->dev, lsop->rspdma,
147314fd1e98SJames Smart 				sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
147414fd1e98SJames Smart 	fc_dma_unmap_single(lport->dev, lsop->rspdma,
147514fd1e98SJames Smart 			sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
147614fd1e98SJames Smart 
147714fd1e98SJames Smart 	kfree(lsop);
147814fd1e98SJames Smart 
147914fd1e98SJames Smart 	nvme_fc_rport_put(rport);
148014fd1e98SJames Smart }
148114fd1e98SJames Smart 
148214fd1e98SJames Smart static void
148314fd1e98SJames Smart nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op *lsop)
148414fd1e98SJames Smart {
148514fd1e98SJames Smart 	struct nvme_fc_rport *rport = lsop->rport;
148614fd1e98SJames Smart 	struct nvme_fc_lport *lport = rport->lport;
148714fd1e98SJames Smart 	struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
148814fd1e98SJames Smart 	int ret;
148914fd1e98SJames Smart 
149014fd1e98SJames Smart 	fc_dma_sync_single_for_device(lport->dev, lsop->rspdma,
149114fd1e98SJames Smart 				  sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
149214fd1e98SJames Smart 
149314fd1e98SJames Smart 	ret = lport->ops->xmt_ls_rsp(&lport->localport, &rport->remoteport,
149414fd1e98SJames Smart 				     lsop->lsrsp);
149514fd1e98SJames Smart 	if (ret) {
149614fd1e98SJames Smart 		dev_warn(lport->dev,
149714fd1e98SJames Smart 			"LLDD rejected LS RSP xmt: LS %d status %d\n",
149814fd1e98SJames Smart 			w0->ls_cmd, ret);
149914fd1e98SJames Smart 		nvme_fc_xmt_ls_rsp_done(lsop->lsrsp);
150014fd1e98SJames Smart 		return;
150114fd1e98SJames Smart 	}
150214fd1e98SJames Smart }
150314fd1e98SJames Smart 
150414fd1e98SJames Smart static struct nvme_fc_ctrl *
150514fd1e98SJames Smart nvme_fc_match_disconn_ls(struct nvme_fc_rport *rport,
150614fd1e98SJames Smart 		      struct nvmefc_ls_rcv_op *lsop)
150714fd1e98SJames Smart {
150814fd1e98SJames Smart 	struct fcnvme_ls_disconnect_assoc_rqst *rqst =
150914fd1e98SJames Smart 					&lsop->rqstbuf->rq_dis_assoc;
151014fd1e98SJames Smart 	struct nvme_fc_ctrl *ctrl, *ret = NULL;
151114fd1e98SJames Smart 	struct nvmefc_ls_rcv_op *oldls = NULL;
151214fd1e98SJames Smart 	u64 association_id = be64_to_cpu(rqst->associd.association_id);
151314fd1e98SJames Smart 	unsigned long flags;
151414fd1e98SJames Smart 
151514fd1e98SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
151614fd1e98SJames Smart 
151714fd1e98SJames Smart 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
151814fd1e98SJames Smart 		if (!nvme_fc_ctrl_get(ctrl))
151914fd1e98SJames Smart 			continue;
152014fd1e98SJames Smart 		spin_lock(&ctrl->lock);
152114fd1e98SJames Smart 		if (association_id == ctrl->association_id) {
152214fd1e98SJames Smart 			oldls = ctrl->rcv_disconn;
152314fd1e98SJames Smart 			ctrl->rcv_disconn = lsop;
152414fd1e98SJames Smart 			ret = ctrl;
152514fd1e98SJames Smart 		}
152614fd1e98SJames Smart 		spin_unlock(&ctrl->lock);
152714fd1e98SJames Smart 		if (ret)
152814fd1e98SJames Smart 			/* leave the ctrl get reference */
152914fd1e98SJames Smart 			break;
153014fd1e98SJames Smart 		nvme_fc_ctrl_put(ctrl);
153114fd1e98SJames Smart 	}
153214fd1e98SJames Smart 
153314fd1e98SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
153414fd1e98SJames Smart 
153514fd1e98SJames Smart 	/* transmit a response for anything that was pending */
153614fd1e98SJames Smart 	if (oldls) {
153714fd1e98SJames Smart 		dev_info(rport->lport->dev,
153814fd1e98SJames Smart 			"NVME-FC{%d}: Multiple Disconnect Association "
153914fd1e98SJames Smart 			"LS's received\n", ctrl->cnum);
154014fd1e98SJames Smart 		/* overwrite good response with bogus failure */
154114fd1e98SJames Smart 		oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf,
154214fd1e98SJames Smart 						sizeof(*oldls->rspbuf),
154314fd1e98SJames Smart 						rqst->w0.ls_cmd,
154414fd1e98SJames Smart 						FCNVME_RJT_RC_UNAB,
154514fd1e98SJames Smart 						FCNVME_RJT_EXP_NONE, 0);
154614fd1e98SJames Smart 		nvme_fc_xmt_ls_rsp(oldls);
154714fd1e98SJames Smart 	}
154814fd1e98SJames Smart 
154914fd1e98SJames Smart 	return ret;
155014fd1e98SJames Smart }
155114fd1e98SJames Smart 
155214fd1e98SJames Smart /*
155314fd1e98SJames Smart  * returns true to mean LS handled and ls_rsp can be sent
155414fd1e98SJames Smart  * returns false to defer ls_rsp xmt (will be done as part of
155514fd1e98SJames Smart  *     association termination)
155614fd1e98SJames Smart  */
155714fd1e98SJames Smart static bool
155814fd1e98SJames Smart nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op *lsop)
155914fd1e98SJames Smart {
156014fd1e98SJames Smart 	struct nvme_fc_rport *rport = lsop->rport;
156114fd1e98SJames Smart 	struct fcnvme_ls_disconnect_assoc_rqst *rqst =
156214fd1e98SJames Smart 					&lsop->rqstbuf->rq_dis_assoc;
156314fd1e98SJames Smart 	struct fcnvme_ls_disconnect_assoc_acc *acc =
156414fd1e98SJames Smart 					&lsop->rspbuf->rsp_dis_assoc;
156514fd1e98SJames Smart 	struct nvme_fc_ctrl *ctrl = NULL;
156614fd1e98SJames Smart 	int ret = 0;
156714fd1e98SJames Smart 
156814fd1e98SJames Smart 	memset(acc, 0, sizeof(*acc));
156914fd1e98SJames Smart 
157014fd1e98SJames Smart 	ret = nvmefc_vldt_lsreq_discon_assoc(lsop->rqstdatalen, rqst);
157114fd1e98SJames Smart 	if (!ret) {
157214fd1e98SJames Smart 		/* match an active association */
157314fd1e98SJames Smart 		ctrl = nvme_fc_match_disconn_ls(rport, lsop);
157414fd1e98SJames Smart 		if (!ctrl)
157514fd1e98SJames Smart 			ret = VERR_NO_ASSOC;
157614fd1e98SJames Smart 	}
157714fd1e98SJames Smart 
157814fd1e98SJames Smart 	if (ret) {
157914fd1e98SJames Smart 		dev_info(rport->lport->dev,
158014fd1e98SJames Smart 			"Disconnect LS failed: %s\n",
158114fd1e98SJames Smart 			validation_errors[ret]);
158214fd1e98SJames Smart 		lsop->lsrsp->rsplen = nvme_fc_format_rjt(acc,
158314fd1e98SJames Smart 					sizeof(*acc), rqst->w0.ls_cmd,
158414fd1e98SJames Smart 					(ret == VERR_NO_ASSOC) ?
158514fd1e98SJames Smart 						FCNVME_RJT_RC_INV_ASSOC :
158614fd1e98SJames Smart 						FCNVME_RJT_RC_LOGIC,
158714fd1e98SJames Smart 					FCNVME_RJT_EXP_NONE, 0);
158814fd1e98SJames Smart 		return true;
158914fd1e98SJames Smart 	}
159014fd1e98SJames Smart 
159114fd1e98SJames Smart 	/* format an ACCept response */
159214fd1e98SJames Smart 
159314fd1e98SJames Smart 	lsop->lsrsp->rsplen = sizeof(*acc);
159414fd1e98SJames Smart 
159514fd1e98SJames Smart 	nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
159614fd1e98SJames Smart 			fcnvme_lsdesc_len(
159714fd1e98SJames Smart 				sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
159814fd1e98SJames Smart 			FCNVME_LS_DISCONNECT_ASSOC);
159914fd1e98SJames Smart 
160014fd1e98SJames Smart 	/*
160114fd1e98SJames Smart 	 * the transmit of the response will occur after the exchanges
160214fd1e98SJames Smart 	 * for the association have been ABTS'd by
160314fd1e98SJames Smart 	 * nvme_fc_delete_association().
160414fd1e98SJames Smart 	 */
160514fd1e98SJames Smart 
160614fd1e98SJames Smart 	/* fail the association */
160714fd1e98SJames Smart 	nvme_fc_error_recovery(ctrl, "Disconnect Association LS received");
160814fd1e98SJames Smart 
160914fd1e98SJames Smart 	/* release the reference taken by nvme_fc_match_disconn_ls() */
161014fd1e98SJames Smart 	nvme_fc_ctrl_put(ctrl);
161114fd1e98SJames Smart 
161214fd1e98SJames Smart 	return false;
161314fd1e98SJames Smart }
161414fd1e98SJames Smart 
161514fd1e98SJames Smart /*
161614fd1e98SJames Smart  * Actual Processing routine for received FC-NVME LS Requests from the LLD
161714fd1e98SJames Smart  * returns true if a response should be sent afterward, false if rsp will
161814fd1e98SJames Smart  * be sent asynchronously.
161914fd1e98SJames Smart  */
162014fd1e98SJames Smart static bool
162114fd1e98SJames Smart nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op *lsop)
162214fd1e98SJames Smart {
162314fd1e98SJames Smart 	struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
162414fd1e98SJames Smart 	bool ret = true;
162514fd1e98SJames Smart 
162614fd1e98SJames Smart 	lsop->lsrsp->nvme_fc_private = lsop;
162714fd1e98SJames Smart 	lsop->lsrsp->rspbuf = lsop->rspbuf;
162814fd1e98SJames Smart 	lsop->lsrsp->rspdma = lsop->rspdma;
162914fd1e98SJames Smart 	lsop->lsrsp->done = nvme_fc_xmt_ls_rsp_done;
163014fd1e98SJames Smart 	/* Be preventative. handlers will later set to valid length */
163114fd1e98SJames Smart 	lsop->lsrsp->rsplen = 0;
163214fd1e98SJames Smart 
163314fd1e98SJames Smart 	/*
163414fd1e98SJames Smart 	 * handlers:
163514fd1e98SJames Smart 	 *   parse request input, execute the request, and format the
163614fd1e98SJames Smart 	 *   LS response
163714fd1e98SJames Smart 	 */
163814fd1e98SJames Smart 	switch (w0->ls_cmd) {
163914fd1e98SJames Smart 	case FCNVME_LS_DISCONNECT_ASSOC:
164014fd1e98SJames Smart 		ret = nvme_fc_ls_disconnect_assoc(lsop);
164114fd1e98SJames Smart 		break;
164214fd1e98SJames Smart 	case FCNVME_LS_DISCONNECT_CONN:
164314fd1e98SJames Smart 		lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
164414fd1e98SJames Smart 				sizeof(*lsop->rspbuf), w0->ls_cmd,
164514fd1e98SJames Smart 				FCNVME_RJT_RC_UNSUP, FCNVME_RJT_EXP_NONE, 0);
164614fd1e98SJames Smart 		break;
164714fd1e98SJames Smart 	case FCNVME_LS_CREATE_ASSOCIATION:
164814fd1e98SJames Smart 	case FCNVME_LS_CREATE_CONNECTION:
164914fd1e98SJames Smart 		lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
165014fd1e98SJames Smart 				sizeof(*lsop->rspbuf), w0->ls_cmd,
165114fd1e98SJames Smart 				FCNVME_RJT_RC_LOGIC, FCNVME_RJT_EXP_NONE, 0);
165214fd1e98SJames Smart 		break;
165314fd1e98SJames Smart 	default:
165414fd1e98SJames Smart 		lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
165514fd1e98SJames Smart 				sizeof(*lsop->rspbuf), w0->ls_cmd,
165614fd1e98SJames Smart 				FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
165714fd1e98SJames Smart 		break;
165814fd1e98SJames Smart 	}
165914fd1e98SJames Smart 
166014fd1e98SJames Smart 	return(ret);
166114fd1e98SJames Smart }
166214fd1e98SJames Smart 
166314fd1e98SJames Smart static void
166414fd1e98SJames Smart nvme_fc_handle_ls_rqst_work(struct work_struct *work)
166514fd1e98SJames Smart {
166614fd1e98SJames Smart 	struct nvme_fc_rport *rport =
166714fd1e98SJames Smart 		container_of(work, struct nvme_fc_rport, lsrcv_work);
166814fd1e98SJames Smart 	struct fcnvme_ls_rqst_w0 *w0;
166914fd1e98SJames Smart 	struct nvmefc_ls_rcv_op *lsop;
167014fd1e98SJames Smart 	unsigned long flags;
167114fd1e98SJames Smart 	bool sendrsp;
167214fd1e98SJames Smart 
167314fd1e98SJames Smart restart:
167414fd1e98SJames Smart 	sendrsp = true;
167514fd1e98SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
167614fd1e98SJames Smart 	list_for_each_entry(lsop, &rport->ls_rcv_list, lsrcv_list) {
167714fd1e98SJames Smart 		if (lsop->handled)
167814fd1e98SJames Smart 			continue;
167914fd1e98SJames Smart 
168014fd1e98SJames Smart 		lsop->handled = true;
168114fd1e98SJames Smart 		if (rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
168214fd1e98SJames Smart 			spin_unlock_irqrestore(&rport->lock, flags);
168314fd1e98SJames Smart 			sendrsp = nvme_fc_handle_ls_rqst(lsop);
168414fd1e98SJames Smart 		} else {
168514fd1e98SJames Smart 			spin_unlock_irqrestore(&rport->lock, flags);
168614fd1e98SJames Smart 			w0 = &lsop->rqstbuf->w0;
168714fd1e98SJames Smart 			lsop->lsrsp->rsplen = nvme_fc_format_rjt(
168814fd1e98SJames Smart 						lsop->rspbuf,
168914fd1e98SJames Smart 						sizeof(*lsop->rspbuf),
169014fd1e98SJames Smart 						w0->ls_cmd,
169114fd1e98SJames Smart 						FCNVME_RJT_RC_UNAB,
169214fd1e98SJames Smart 						FCNVME_RJT_EXP_NONE, 0);
169314fd1e98SJames Smart 		}
169414fd1e98SJames Smart 		if (sendrsp)
169514fd1e98SJames Smart 			nvme_fc_xmt_ls_rsp(lsop);
169614fd1e98SJames Smart 		goto restart;
169714fd1e98SJames Smart 	}
169814fd1e98SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
169914fd1e98SJames Smart }
170014fd1e98SJames Smart 
170172e6329fSJames Smart /**
170272e6329fSJames Smart  * nvme_fc_rcv_ls_req - transport entry point called by an LLDD
170372e6329fSJames Smart  *                       upon the reception of a NVME LS request.
170472e6329fSJames Smart  *
170572e6329fSJames Smart  * The nvme-fc layer will copy payload to an internal structure for
170672e6329fSJames Smart  * processing.  As such, upon completion of the routine, the LLDD may
170772e6329fSJames Smart  * immediately free/reuse the LS request buffer passed in the call.
170872e6329fSJames Smart  *
170972e6329fSJames Smart  * If this routine returns error, the LLDD should abort the exchange.
171072e6329fSJames Smart  *
171172e6329fSJames Smart  * @remoteport: pointer to the (registered) remote port that the LS
171272e6329fSJames Smart  *              was received from. The remoteport is associated with
171372e6329fSJames Smart  *              a specific localport.
171472e6329fSJames Smart  * @lsrsp:      pointer to a nvmefc_ls_rsp response structure to be
171572e6329fSJames Smart  *              used to reference the exchange corresponding to the LS
171672e6329fSJames Smart  *              when issuing an ls response.
171772e6329fSJames Smart  * @lsreqbuf:   pointer to the buffer containing the LS Request
171872e6329fSJames Smart  * @lsreqbuf_len: length, in bytes, of the received LS request
171972e6329fSJames Smart  */
172072e6329fSJames Smart int
172172e6329fSJames Smart nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
172272e6329fSJames Smart 			struct nvmefc_ls_rsp *lsrsp,
172372e6329fSJames Smart 			void *lsreqbuf, u32 lsreqbuf_len)
172472e6329fSJames Smart {
172572e6329fSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
172672e6329fSJames Smart 	struct nvme_fc_lport *lport = rport->lport;
172714fd1e98SJames Smart 	struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf;
172814fd1e98SJames Smart 	struct nvmefc_ls_rcv_op *lsop;
172914fd1e98SJames Smart 	unsigned long flags;
173014fd1e98SJames Smart 	int ret;
173114fd1e98SJames Smart 
173214fd1e98SJames Smart 	nvme_fc_rport_get(rport);
173372e6329fSJames Smart 
173472e6329fSJames Smart 	/* validate there's a routine to transmit a response */
173514fd1e98SJames Smart 	if (!lport->ops->xmt_ls_rsp) {
173614fd1e98SJames Smart 		dev_info(lport->dev,
173714fd1e98SJames Smart 			"RCV %s LS failed: no LLDD xmt_ls_rsp\n",
173814fd1e98SJames Smart 			(w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
173914fd1e98SJames Smart 				nvmefc_ls_names[w0->ls_cmd] : "");
174014fd1e98SJames Smart 		ret = -EINVAL;
174114fd1e98SJames Smart 		goto out_put;
174214fd1e98SJames Smart 	}
174314fd1e98SJames Smart 
174414fd1e98SJames Smart 	if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) {
174514fd1e98SJames Smart 		dev_info(lport->dev,
174614fd1e98SJames Smart 			"RCV %s LS failed: payload too large\n",
174714fd1e98SJames Smart 			(w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
174814fd1e98SJames Smart 				nvmefc_ls_names[w0->ls_cmd] : "");
174914fd1e98SJames Smart 		ret = -E2BIG;
175014fd1e98SJames Smart 		goto out_put;
175114fd1e98SJames Smart 	}
175214fd1e98SJames Smart 
175314fd1e98SJames Smart 	lsop = kzalloc(sizeof(*lsop) +
175414fd1e98SJames Smart 			sizeof(union nvmefc_ls_requests) +
175514fd1e98SJames Smart 			sizeof(union nvmefc_ls_responses),
175614fd1e98SJames Smart 			GFP_KERNEL);
175714fd1e98SJames Smart 	if (!lsop) {
175814fd1e98SJames Smart 		dev_info(lport->dev,
175914fd1e98SJames Smart 			"RCV %s LS failed: No memory\n",
176014fd1e98SJames Smart 			(w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
176114fd1e98SJames Smart 				nvmefc_ls_names[w0->ls_cmd] : "");
176214fd1e98SJames Smart 		ret = -ENOMEM;
176314fd1e98SJames Smart 		goto out_put;
176414fd1e98SJames Smart 	}
176514fd1e98SJames Smart 	lsop->rqstbuf = (union nvmefc_ls_requests *)&lsop[1];
176614fd1e98SJames Smart 	lsop->rspbuf = (union nvmefc_ls_responses *)&lsop->rqstbuf[1];
176714fd1e98SJames Smart 
176814fd1e98SJames Smart 	lsop->rspdma = fc_dma_map_single(lport->dev, lsop->rspbuf,
176914fd1e98SJames Smart 					sizeof(*lsop->rspbuf),
177014fd1e98SJames Smart 					DMA_TO_DEVICE);
177114fd1e98SJames Smart 	if (fc_dma_mapping_error(lport->dev, lsop->rspdma)) {
177214fd1e98SJames Smart 		dev_info(lport->dev,
177314fd1e98SJames Smart 			"RCV %s LS failed: DMA mapping failure\n",
177414fd1e98SJames Smart 			(w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
177514fd1e98SJames Smart 				nvmefc_ls_names[w0->ls_cmd] : "");
177614fd1e98SJames Smart 		ret = -EFAULT;
177714fd1e98SJames Smart 		goto out_free;
177814fd1e98SJames Smart 	}
177914fd1e98SJames Smart 
178014fd1e98SJames Smart 	lsop->rport = rport;
178114fd1e98SJames Smart 	lsop->lsrsp = lsrsp;
178214fd1e98SJames Smart 
178314fd1e98SJames Smart 	memcpy(lsop->rqstbuf, lsreqbuf, lsreqbuf_len);
178414fd1e98SJames Smart 	lsop->rqstdatalen = lsreqbuf_len;
178514fd1e98SJames Smart 
178614fd1e98SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
178714fd1e98SJames Smart 	if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) {
178814fd1e98SJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
178914fd1e98SJames Smart 		ret = -ENOTCONN;
179014fd1e98SJames Smart 		goto out_unmap;
179114fd1e98SJames Smart 	}
179214fd1e98SJames Smart 	list_add_tail(&lsop->lsrcv_list, &rport->ls_rcv_list);
179314fd1e98SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
179414fd1e98SJames Smart 
179514fd1e98SJames Smart 	schedule_work(&rport->lsrcv_work);
179672e6329fSJames Smart 
179772e6329fSJames Smart 	return 0;
179814fd1e98SJames Smart 
179914fd1e98SJames Smart out_unmap:
180014fd1e98SJames Smart 	fc_dma_unmap_single(lport->dev, lsop->rspdma,
180114fd1e98SJames Smart 			sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
180214fd1e98SJames Smart out_free:
180314fd1e98SJames Smart 	kfree(lsop);
180414fd1e98SJames Smart out_put:
180514fd1e98SJames Smart 	nvme_fc_rport_put(rport);
180614fd1e98SJames Smart 	return ret;
180772e6329fSJames Smart }
180872e6329fSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req);
180972e6329fSJames Smart 
1810e399441dSJames Smart 
1811e399441dSJames Smart /* *********************** NVME Ctrl Routines **************************** */
1812e399441dSJames Smart 
1813e399441dSJames Smart static void
1814e399441dSJames Smart __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1815e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
1816e399441dSJames Smart {
1817e399441dSJames Smart 	fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1818e399441dSJames Smart 				sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1819e399441dSJames Smart 	fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1820e399441dSJames Smart 				sizeof(op->cmd_iu), DMA_TO_DEVICE);
1821e399441dSJames Smart 
1822e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_UNINIT);
1823e399441dSJames Smart }
1824e399441dSJames Smart 
1825e399441dSJames Smart static void
1826d6296d39SChristoph Hellwig nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1827d6296d39SChristoph Hellwig 		unsigned int hctx_idx)
1828e399441dSJames Smart {
1829e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1830e399441dSJames Smart 
1831d6296d39SChristoph Hellwig 	return __nvme_fc_exit_request(set->driver_data, op);
1832e399441dSJames Smart }
1833e399441dSJames Smart 
183478a7ac26SJames Smart static int
183578a7ac26SJames Smart __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
183678a7ac26SJames Smart {
18373efd6e8eSJames Smart 	unsigned long flags;
18383efd6e8eSJames Smart 	int opstate;
183978a7ac26SJames Smart 
18403efd6e8eSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
18413efd6e8eSJames Smart 	opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
18423efd6e8eSJames Smart 	if (opstate != FCPOP_STATE_ACTIVE)
18433efd6e8eSJames Smart 		atomic_set(&op->state, opstate);
184452793d62SJames Smart 	else if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) {
184552793d62SJames Smart 		op->flags |= FCOP_FLAGS_TERMIO;
18463efd6e8eSJames Smart 		ctrl->iocnt++;
184752793d62SJames Smart 	}
18483efd6e8eSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
18493efd6e8eSJames Smart 
18503efd6e8eSJames Smart 	if (opstate != FCPOP_STATE_ACTIVE)
185178a7ac26SJames Smart 		return -ECANCELED;
185278a7ac26SJames Smart 
185378a7ac26SJames Smart 	ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
185478a7ac26SJames Smart 					&ctrl->rport->remoteport,
185578a7ac26SJames Smart 					op->queue->lldd_handle,
185678a7ac26SJames Smart 					&op->fcp_req);
185778a7ac26SJames Smart 
185878a7ac26SJames Smart 	return 0;
185978a7ac26SJames Smart }
186078a7ac26SJames Smart 
1861e399441dSJames Smart static void
186278a7ac26SJames Smart nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1863e399441dSJames Smart {
1864e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
18653efd6e8eSJames Smart 	int i;
1866e399441dSJames Smart 
18674cff280aSJames Smart 	/* ensure we've initialized the ops once */
18684cff280aSJames Smart 	if (!(aen_op->flags & FCOP_FLAGS_AEN))
18694cff280aSJames Smart 		return;
18704cff280aSJames Smart 
18713efd6e8eSJames Smart 	for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
18723efd6e8eSJames Smart 		__nvme_fc_abort_op(ctrl, aen_op);
187378a7ac26SJames Smart }
187478a7ac26SJames Smart 
1875c3aedd22SJames Smart static inline void
187678a7ac26SJames Smart __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
18773efd6e8eSJames Smart 		struct nvme_fc_fcp_op *op, int opstate)
187878a7ac26SJames Smart {
187978a7ac26SJames Smart 	unsigned long flags;
188078a7ac26SJames Smart 
1881c3aedd22SJames Smart 	if (opstate == FCPOP_STATE_ABORTED) {
188278a7ac26SJames Smart 		spin_lock_irqsave(&ctrl->lock, flags);
188352793d62SJames Smart 		if (test_bit(FCCTRL_TERMIO, &ctrl->flags) &&
188452793d62SJames Smart 		    op->flags & FCOP_FLAGS_TERMIO) {
188536715cf4SJames Smart 			if (!--ctrl->iocnt)
188636715cf4SJames Smart 				wake_up(&ctrl->ioabort_wait);
188736715cf4SJames Smart 		}
188878a7ac26SJames Smart 		spin_unlock_irqrestore(&ctrl->lock, flags);
1889c3aedd22SJames Smart 	}
189078a7ac26SJames Smart }
1891e399441dSJames Smart 
1892baee29acSChristoph Hellwig static void
1893e399441dSJames Smart nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1894e399441dSJames Smart {
1895e399441dSJames Smart 	struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1896e399441dSJames Smart 	struct request *rq = op->rq;
1897e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
1898e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
1899e399441dSJames Smart 	struct nvme_fc_queue *queue = op->queue;
1900e399441dSJames Smart 	struct nvme_completion *cqe = &op->rsp_iu.cqe;
1901458f280dSJames Smart 	struct nvme_command *sqe = &op->cmd_iu.sqe;
1902d663b69fSChristoph Hellwig 	__le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
190327fa9bc5SChristoph Hellwig 	union nvme_result result;
19040a02e39fSJames Smart 	bool terminate_assoc = true;
19053efd6e8eSJames Smart 	int opstate;
1906e399441dSJames Smart 
1907e399441dSJames Smart 	/*
1908e399441dSJames Smart 	 * WARNING:
1909e399441dSJames Smart 	 * The current linux implementation of a nvme controller
1910e399441dSJames Smart 	 * allocates a single tag set for all io queues and sizes
1911e399441dSJames Smart 	 * the io queues to fully hold all possible tags. Thus, the
1912e399441dSJames Smart 	 * implementation does not reference or care about the sqhd
1913e399441dSJames Smart 	 * value as it never needs to use the sqhd/sqtail pointers
1914e399441dSJames Smart 	 * for submission pacing.
1915e399441dSJames Smart 	 *
1916e399441dSJames Smart 	 * This affects the FC-NVME implementation in two ways:
1917e399441dSJames Smart 	 * 1) As the value doesn't matter, we don't need to waste
1918e399441dSJames Smart 	 *    cycles extracting it from ERSPs and stamping it in the
1919e399441dSJames Smart 	 *    cases where the transport fabricates CQEs on successful
1920e399441dSJames Smart 	 *    completions.
1921e399441dSJames Smart 	 * 2) The FC-NVME implementation requires that delivery of
1922e399441dSJames Smart 	 *    ERSP completions are to go back to the nvme layer in order
1923e399441dSJames Smart 	 *    relative to the rsn, such that the sqhd value will always
1924e399441dSJames Smart 	 *    be "in order" for the nvme layer. As the nvme layer in
1925e399441dSJames Smart 	 *    linux doesn't care about sqhd, there's no need to return
1926e399441dSJames Smart 	 *    them in order.
1927e399441dSJames Smart 	 *
1928e399441dSJames Smart 	 * Additionally:
1929e399441dSJames Smart 	 * As the core nvme layer in linux currently does not look at
1930e399441dSJames Smart 	 * every field in the cqe - in cases where the FC transport must
1931e399441dSJames Smart 	 * fabricate a CQE, the following fields will not be set as they
1932e399441dSJames Smart 	 * are not referenced:
1933e399441dSJames Smart 	 *      cqe.sqid,  cqe.sqhd,  cqe.command_id
1934f874d5d0SJames Smart 	 *
1935f874d5d0SJames Smart 	 * Failure or error of an individual i/o, in a transport
1936f874d5d0SJames Smart 	 * detected fashion unrelated to the nvme completion status,
1937f874d5d0SJames Smart 	 * potentially cause the initiator and target sides to get out
1938f874d5d0SJames Smart 	 * of sync on SQ head/tail (aka outstanding io count allowed).
1939f874d5d0SJames Smart 	 * Per FC-NVME spec, failure of an individual command requires
1940f874d5d0SJames Smart 	 * the connection to be terminated, which in turn requires the
1941f874d5d0SJames Smart 	 * association to be terminated.
1942e399441dSJames Smart 	 */
1943e399441dSJames Smart 
19443efd6e8eSJames Smart 	opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
19453efd6e8eSJames Smart 
1946e399441dSJames Smart 	fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1947e399441dSJames Smart 				sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1948e399441dSJames Smart 
19493efd6e8eSJames Smart 	if (opstate == FCPOP_STATE_ABORTED)
195074bd8cbeSJames Smart 		status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
195174bd8cbeSJames Smart 	else if (freq->status) {
195274bd8cbeSJames Smart 		status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
195374bd8cbeSJames Smart 		dev_info(ctrl->ctrl.device,
195474bd8cbeSJames Smart 			"NVME-FC{%d}: io failed due to lldd error %d\n",
195574bd8cbeSJames Smart 			ctrl->cnum, freq->status);
195674bd8cbeSJames Smart 	}
1957e399441dSJames Smart 
1958e399441dSJames Smart 	/*
1959e399441dSJames Smart 	 * For the linux implementation, if we have an unsuccesful
1960e399441dSJames Smart 	 * status, they blk-mq layer can typically be called with the
1961e399441dSJames Smart 	 * non-zero status and the content of the cqe isn't important.
1962e399441dSJames Smart 	 */
1963e399441dSJames Smart 	if (status)
1964e399441dSJames Smart 		goto done;
1965e399441dSJames Smart 
1966e399441dSJames Smart 	/*
1967e399441dSJames Smart 	 * command completed successfully relative to the wire
1968e399441dSJames Smart 	 * protocol. However, validate anything received and
1969e399441dSJames Smart 	 * extract the status and result from the cqe (create it
1970e399441dSJames Smart 	 * where necessary).
1971e399441dSJames Smart 	 */
1972e399441dSJames Smart 
1973e399441dSJames Smart 	switch (freq->rcv_rsplen) {
1974e399441dSJames Smart 
1975e399441dSJames Smart 	case 0:
1976e399441dSJames Smart 	case NVME_FC_SIZEOF_ZEROS_RSP:
1977e399441dSJames Smart 		/*
1978e399441dSJames Smart 		 * No response payload or 12 bytes of payload (which
1979e399441dSJames Smart 		 * should all be zeros) are considered successful and
1980e399441dSJames Smart 		 * no payload in the CQE by the transport.
1981e399441dSJames Smart 		 */
1982e399441dSJames Smart 		if (freq->transferred_length !=
1983e399441dSJames Smart 		    be32_to_cpu(op->cmd_iu.data_len)) {
198474bd8cbeSJames Smart 			status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
198574bd8cbeSJames Smart 			dev_info(ctrl->ctrl.device,
198674bd8cbeSJames Smart 				"NVME-FC{%d}: io failed due to bad transfer "
198774bd8cbeSJames Smart 				"length: %d vs expected %d\n",
198874bd8cbeSJames Smart 				ctrl->cnum, freq->transferred_length,
198974bd8cbeSJames Smart 				be32_to_cpu(op->cmd_iu.data_len));
1990e399441dSJames Smart 			goto done;
1991e399441dSJames Smart 		}
199227fa9bc5SChristoph Hellwig 		result.u64 = 0;
1993e399441dSJames Smart 		break;
1994e399441dSJames Smart 
1995e399441dSJames Smart 	case sizeof(struct nvme_fc_ersp_iu):
1996e399441dSJames Smart 		/*
1997e399441dSJames Smart 		 * The ERSP IU contains a full completion with CQE.
1998e399441dSJames Smart 		 * Validate ERSP IU and look at cqe.
1999e399441dSJames Smart 		 */
2000e399441dSJames Smart 		if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
2001e399441dSJames Smart 					(freq->rcv_rsplen / 4) ||
2002e399441dSJames Smart 			     be32_to_cpu(op->rsp_iu.xfrd_len) !=
2003e399441dSJames Smart 					freq->transferred_length ||
200453b2b2f5SJames Smart 			     op->rsp_iu.ersp_result ||
2005458f280dSJames Smart 			     sqe->common.command_id != cqe->command_id)) {
200674bd8cbeSJames Smart 			status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
200774bd8cbeSJames Smart 			dev_info(ctrl->ctrl.device,
200874bd8cbeSJames Smart 				"NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
200974bd8cbeSJames Smart 				"iu len %d, xfr len %d vs %d, status code "
201074bd8cbeSJames Smart 				"%d, cmdid %d vs %d\n",
201174bd8cbeSJames Smart 				ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len),
201274bd8cbeSJames Smart 				be32_to_cpu(op->rsp_iu.xfrd_len),
201374bd8cbeSJames Smart 				freq->transferred_length,
201453b2b2f5SJames Smart 				op->rsp_iu.ersp_result,
201574bd8cbeSJames Smart 				sqe->common.command_id,
201674bd8cbeSJames Smart 				cqe->command_id);
2017e399441dSJames Smart 			goto done;
2018e399441dSJames Smart 		}
201927fa9bc5SChristoph Hellwig 		result = cqe->result;
2020d663b69fSChristoph Hellwig 		status = cqe->status;
2021e399441dSJames Smart 		break;
2022e399441dSJames Smart 
2023e399441dSJames Smart 	default:
202474bd8cbeSJames Smart 		status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
202574bd8cbeSJames Smart 		dev_info(ctrl->ctrl.device,
202674bd8cbeSJames Smart 			"NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
202774bd8cbeSJames Smart 			"len %d\n",
202874bd8cbeSJames Smart 			ctrl->cnum, freq->rcv_rsplen);
2029e399441dSJames Smart 		goto done;
2030e399441dSJames Smart 	}
2031e399441dSJames Smart 
2032f874d5d0SJames Smart 	terminate_assoc = false;
2033f874d5d0SJames Smart 
2034e399441dSJames Smart done:
203578a7ac26SJames Smart 	if (op->flags & FCOP_FLAGS_AEN) {
203627fa9bc5SChristoph Hellwig 		nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
20373efd6e8eSJames Smart 		__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
203878a7ac26SJames Smart 		atomic_set(&op->state, FCPOP_STATE_IDLE);
203978a7ac26SJames Smart 		op->flags = FCOP_FLAGS_AEN;	/* clear other flags */
2040e399441dSJames Smart 		nvme_fc_ctrl_put(ctrl);
2041f874d5d0SJames Smart 		goto check_error;
2042e399441dSJames Smart 	}
2043e399441dSJames Smart 
2044c3aedd22SJames Smart 	__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
20452eb81a33SChristoph Hellwig 	if (!nvme_try_complete_req(rq, status, result))
2046ff029451SChristoph Hellwig 		nvme_fc_complete_rq(rq);
2047f874d5d0SJames Smart 
2048f874d5d0SJames Smart check_error:
2049f874d5d0SJames Smart 	if (terminate_assoc)
2050f874d5d0SJames Smart 		nvme_fc_error_recovery(ctrl, "transport detected io error");
2051e399441dSJames Smart }
2052e399441dSJames Smart 
2053e399441dSJames Smart static int
2054e399441dSJames Smart __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
2055e399441dSJames Smart 		struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
2056e399441dSJames Smart 		struct request *rq, u32 rqno)
2057e399441dSJames Smart {
2058d3d0bc78SBart Van Assche 	struct nvme_fcp_op_w_sgl *op_w_sgl =
2059d3d0bc78SBart Van Assche 		container_of(op, typeof(*op_w_sgl), op);
2060e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2061e399441dSJames Smart 	int ret = 0;
2062e399441dSJames Smart 
2063e399441dSJames Smart 	memset(op, 0, sizeof(*op));
2064e399441dSJames Smart 	op->fcp_req.cmdaddr = &op->cmd_iu;
2065e399441dSJames Smart 	op->fcp_req.cmdlen = sizeof(op->cmd_iu);
2066e399441dSJames Smart 	op->fcp_req.rspaddr = &op->rsp_iu;
2067e399441dSJames Smart 	op->fcp_req.rsplen = sizeof(op->rsp_iu);
2068e399441dSJames Smart 	op->fcp_req.done = nvme_fc_fcpio_done;
2069e399441dSJames Smart 	op->ctrl = ctrl;
2070e399441dSJames Smart 	op->queue = queue;
2071e399441dSJames Smart 	op->rq = rq;
2072e399441dSJames Smart 	op->rqno = rqno;
2073e399441dSJames Smart 
207453b2b2f5SJames Smart 	cmdiu->format_id = NVME_CMD_FORMAT_ID;
2075e399441dSJames Smart 	cmdiu->fc_id = NVME_CMD_FC_ID;
2076e399441dSJames Smart 	cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
207744fbf3bbSJames Smart 	if (queue->qnum)
207844fbf3bbSJames Smart 		cmdiu->rsv_cat = fccmnd_set_cat_css(0,
207944fbf3bbSJames Smart 					(NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT));
208044fbf3bbSJames Smart 	else
208144fbf3bbSJames Smart 		cmdiu->rsv_cat = fccmnd_set_cat_admin(0);
2082e399441dSJames Smart 
2083e399441dSJames Smart 	op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
2084e399441dSJames Smart 				&op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
2085e399441dSJames Smart 	if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
2086e399441dSJames Smart 		dev_err(ctrl->dev,
2087e399441dSJames Smart 			"FCP Op failed - cmdiu dma mapping failed.\n");
2088f34448cdSTianjia Zhang 		ret = -EFAULT;
2089e399441dSJames Smart 		goto out_on_error;
2090e399441dSJames Smart 	}
2091e399441dSJames Smart 
2092e399441dSJames Smart 	op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
2093e399441dSJames Smart 				&op->rsp_iu, sizeof(op->rsp_iu),
2094e399441dSJames Smart 				DMA_FROM_DEVICE);
2095e399441dSJames Smart 	if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
2096e399441dSJames Smart 		dev_err(ctrl->dev,
2097e399441dSJames Smart 			"FCP Op failed - rspiu dma mapping failed.\n");
2098f34448cdSTianjia Zhang 		ret = -EFAULT;
2099e399441dSJames Smart 	}
2100e399441dSJames Smart 
2101e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_IDLE);
2102e399441dSJames Smart out_on_error:
2103e399441dSJames Smart 	return ret;
2104e399441dSJames Smart }
2105e399441dSJames Smart 
2106e399441dSJames Smart static int
2107d6296d39SChristoph Hellwig nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
2108d6296d39SChristoph Hellwig 		unsigned int hctx_idx, unsigned int numa_node)
2109e399441dSJames Smart {
2110d6296d39SChristoph Hellwig 	struct nvme_fc_ctrl *ctrl = set->driver_data;
2111d3d0bc78SBart Van Assche 	struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq);
211276f983cbSChristoph Hellwig 	int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
211376f983cbSChristoph Hellwig 	struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
21140d2bdf9fSBart Van Assche 	int res;
2115e399441dSJames Smart 
21160d2bdf9fSBart Van Assche 	res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++);
21170d2bdf9fSBart Van Assche 	if (res)
21180d2bdf9fSBart Van Assche 		return res;
21193add1d93SArnd Bergmann 	op->op.fcp_req.first_sgl = op->sgl;
2120d19b8bc8SJames Smart 	op->op.fcp_req.private = &op->priv[0];
2121dfa74422SEwan D. Milne 	nvme_req(rq)->ctrl = &ctrl->ctrl;
21220d2bdf9fSBart Van Assche 	return res;
2123e399441dSJames Smart }
2124e399441dSJames Smart 
2125e399441dSJames Smart static int
2126e399441dSJames Smart nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
2127e399441dSJames Smart {
2128e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op;
2129e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu;
2130e399441dSJames Smart 	struct nvme_command *sqe;
2131f56bf76fSJames Smart 	void *private = NULL;
2132e399441dSJames Smart 	int i, ret;
2133e399441dSJames Smart 
2134e399441dSJames Smart 	aen_op = ctrl->aen_ops;
213538dabe21SKeith Busch 	for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2136f56bf76fSJames Smart 		if (ctrl->lport->ops->fcprqst_priv_sz) {
213761bff8efSJames Smart 			private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
213861bff8efSJames Smart 						GFP_KERNEL);
213961bff8efSJames Smart 			if (!private)
214061bff8efSJames Smart 				return -ENOMEM;
2141f56bf76fSJames Smart 		}
214261bff8efSJames Smart 
2143e399441dSJames Smart 		cmdiu = &aen_op->cmd_iu;
2144e399441dSJames Smart 		sqe = &cmdiu->sqe;
2145e399441dSJames Smart 		ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
2146e399441dSJames Smart 				aen_op, (struct request *)NULL,
214738dabe21SKeith Busch 				(NVME_AQ_BLK_MQ_DEPTH + i));
214861bff8efSJames Smart 		if (ret) {
214961bff8efSJames Smart 			kfree(private);
2150e399441dSJames Smart 			return ret;
215161bff8efSJames Smart 		}
2152e399441dSJames Smart 
215378a7ac26SJames Smart 		aen_op->flags = FCOP_FLAGS_AEN;
215461bff8efSJames Smart 		aen_op->fcp_req.private = private;
215578a7ac26SJames Smart 
2156e399441dSJames Smart 		memset(sqe, 0, sizeof(*sqe));
2157e399441dSJames Smart 		sqe->common.opcode = nvme_admin_async_event;
215878a7ac26SJames Smart 		/* Note: core layer may overwrite the sqe.command_id value */
215938dabe21SKeith Busch 		sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
2160e399441dSJames Smart 	}
2161e399441dSJames Smart 	return 0;
2162e399441dSJames Smart }
2163e399441dSJames Smart 
216461bff8efSJames Smart static void
216561bff8efSJames Smart nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
216661bff8efSJames Smart {
216761bff8efSJames Smart 	struct nvme_fc_fcp_op *aen_op;
216861bff8efSJames Smart 	int i;
216961bff8efSJames Smart 
2170e126e821SDavid Milburn 	cancel_work_sync(&ctrl->ctrl.async_event_work);
217161bff8efSJames Smart 	aen_op = ctrl->aen_ops;
217238dabe21SKeith Busch 	for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
217361bff8efSJames Smart 		__nvme_fc_exit_request(ctrl, aen_op);
217461bff8efSJames Smart 
217561bff8efSJames Smart 		kfree(aen_op->fcp_req.private);
217661bff8efSJames Smart 		aen_op->fcp_req.private = NULL;
217761bff8efSJames Smart 	}
217861bff8efSJames Smart }
2179e399441dSJames Smart 
2180e399441dSJames Smart static inline void
2181e399441dSJames Smart __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
2182e399441dSJames Smart 		unsigned int qidx)
2183e399441dSJames Smart {
2184e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[qidx];
2185e399441dSJames Smart 
2186e399441dSJames Smart 	hctx->driver_data = queue;
2187e399441dSJames Smart 	queue->hctx = hctx;
2188e399441dSJames Smart }
2189e399441dSJames Smart 
2190e399441dSJames Smart static int
2191e399441dSJames Smart nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2192e399441dSJames Smart 		unsigned int hctx_idx)
2193e399441dSJames Smart {
2194e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = data;
2195e399441dSJames Smart 
2196e399441dSJames Smart 	__nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
2197e399441dSJames Smart 
2198e399441dSJames Smart 	return 0;
2199e399441dSJames Smart }
2200e399441dSJames Smart 
2201e399441dSJames Smart static int
2202e399441dSJames Smart nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2203e399441dSJames Smart 		unsigned int hctx_idx)
2204e399441dSJames Smart {
2205e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = data;
2206e399441dSJames Smart 
2207e399441dSJames Smart 	__nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
2208e399441dSJames Smart 
2209e399441dSJames Smart 	return 0;
2210e399441dSJames Smart }
2211e399441dSJames Smart 
2212e399441dSJames Smart static void
221308e15075SKeith Busch nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
2214e399441dSJames Smart {
2215e399441dSJames Smart 	struct nvme_fc_queue *queue;
2216e399441dSJames Smart 
2217e399441dSJames Smart 	queue = &ctrl->queues[idx];
2218e399441dSJames Smart 	memset(queue, 0, sizeof(*queue));
2219e399441dSJames Smart 	queue->ctrl = ctrl;
2220e399441dSJames Smart 	queue->qnum = idx;
222167f471b6SJames Smart 	atomic_set(&queue->csn, 0);
2222e399441dSJames Smart 	queue->dev = ctrl->dev;
2223e399441dSJames Smart 
2224e399441dSJames Smart 	if (idx > 0)
2225e399441dSJames Smart 		queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
2226e399441dSJames Smart 	else
2227e399441dSJames Smart 		queue->cmnd_capsule_len = sizeof(struct nvme_command);
2228e399441dSJames Smart 
2229e399441dSJames Smart 	/*
2230e399441dSJames Smart 	 * Considered whether we should allocate buffers for all SQEs
2231e399441dSJames Smart 	 * and CQEs and dma map them - mapping their respective entries
2232e399441dSJames Smart 	 * into the request structures (kernel vm addr and dma address)
2233e399441dSJames Smart 	 * thus the driver could use the buffers/mappings directly.
2234e399441dSJames Smart 	 * It only makes sense if the LLDD would use them for its
2235e399441dSJames Smart 	 * messaging api. It's very unlikely most adapter api's would use
2236e399441dSJames Smart 	 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
2237e399441dSJames Smart 	 * structures were used instead.
2238e399441dSJames Smart 	 */
2239e399441dSJames Smart }
2240e399441dSJames Smart 
2241e399441dSJames Smart /*
2242e399441dSJames Smart  * This routine terminates a queue at the transport level.
2243e399441dSJames Smart  * The transport has already ensured that all outstanding ios on
2244e399441dSJames Smart  * the queue have been terminated.
2245e399441dSJames Smart  * The transport will send a Disconnect LS request to terminate
2246e399441dSJames Smart  * the queue's connection. Termination of the admin queue will also
2247e399441dSJames Smart  * terminate the association at the target.
2248e399441dSJames Smart  */
2249e399441dSJames Smart static void
2250e399441dSJames Smart nvme_fc_free_queue(struct nvme_fc_queue *queue)
2251e399441dSJames Smart {
2252e399441dSJames Smart 	if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
2253e399441dSJames Smart 		return;
2254e399441dSJames Smart 
22559e0ed16aSSagi Grimberg 	clear_bit(NVME_FC_Q_LIVE, &queue->flags);
2256e399441dSJames Smart 	/*
2257e399441dSJames Smart 	 * Current implementation never disconnects a single queue.
2258e399441dSJames Smart 	 * It always terminates a whole association. So there is never
2259e399441dSJames Smart 	 * a disconnect(queue) LS sent to the target.
2260e399441dSJames Smart 	 */
2261e399441dSJames Smart 
2262e399441dSJames Smart 	queue->connection_id = 0;
226367f471b6SJames Smart 	atomic_set(&queue->csn, 0);
2264e399441dSJames Smart }
2265e399441dSJames Smart 
2266e399441dSJames Smart static void
2267e399441dSJames Smart __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
2268e399441dSJames Smart 	struct nvme_fc_queue *queue, unsigned int qidx)
2269e399441dSJames Smart {
2270e399441dSJames Smart 	if (ctrl->lport->ops->delete_queue)
2271e399441dSJames Smart 		ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
2272e399441dSJames Smart 				queue->lldd_handle);
2273e399441dSJames Smart 	queue->lldd_handle = NULL;
2274e399441dSJames Smart }
2275e399441dSJames Smart 
2276e399441dSJames Smart static void
2277e399441dSJames Smart nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
2278e399441dSJames Smart {
2279e399441dSJames Smart 	int i;
2280e399441dSJames Smart 
2281d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
2282e399441dSJames Smart 		nvme_fc_free_queue(&ctrl->queues[i]);
2283e399441dSJames Smart }
2284e399441dSJames Smart 
2285e399441dSJames Smart static int
2286e399441dSJames Smart __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
2287e399441dSJames Smart 	struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
2288e399441dSJames Smart {
2289e399441dSJames Smart 	int ret = 0;
2290e399441dSJames Smart 
2291e399441dSJames Smart 	queue->lldd_handle = NULL;
2292e399441dSJames Smart 	if (ctrl->lport->ops->create_queue)
2293e399441dSJames Smart 		ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
2294e399441dSJames Smart 				qidx, qsize, &queue->lldd_handle);
2295e399441dSJames Smart 
2296e399441dSJames Smart 	return ret;
2297e399441dSJames Smart }
2298e399441dSJames Smart 
2299e399441dSJames Smart static void
2300e399441dSJames Smart nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
2301e399441dSJames Smart {
2302d858e5f0SSagi Grimberg 	struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
2303e399441dSJames Smart 	int i;
2304e399441dSJames Smart 
2305d858e5f0SSagi Grimberg 	for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
2306e399441dSJames Smart 		__nvme_fc_delete_hw_queue(ctrl, queue, i);
2307e399441dSJames Smart }
2308e399441dSJames Smart 
2309e399441dSJames Smart static int
2310e399441dSJames Smart nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2311e399441dSJames Smart {
2312e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[1];
231317a1ec08SJohannes Thumshirn 	int i, ret;
2314e399441dSJames Smart 
2315d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
2316e399441dSJames Smart 		ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
231717a1ec08SJohannes Thumshirn 		if (ret)
231817a1ec08SJohannes Thumshirn 			goto delete_queues;
2319e399441dSJames Smart 	}
2320e399441dSJames Smart 
2321e399441dSJames Smart 	return 0;
232217a1ec08SJohannes Thumshirn 
232317a1ec08SJohannes Thumshirn delete_queues:
2324514a6dc9SJames Smart 	for (; i > 0; i--)
232517a1ec08SJohannes Thumshirn 		__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
232617a1ec08SJohannes Thumshirn 	return ret;
2327e399441dSJames Smart }
2328e399441dSJames Smart 
2329e399441dSJames Smart static int
2330e399441dSJames Smart nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2331e399441dSJames Smart {
2332e399441dSJames Smart 	int i, ret = 0;
2333e399441dSJames Smart 
2334d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
2335e399441dSJames Smart 		ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
2336e399441dSJames Smart 					(qsize / 5));
2337e399441dSJames Smart 		if (ret)
2338e399441dSJames Smart 			break;
233926c68227SSagi Grimberg 		ret = nvmf_connect_io_queue(&ctrl->ctrl, i, false);
2340e399441dSJames Smart 		if (ret)
2341e399441dSJames Smart 			break;
23429e0ed16aSSagi Grimberg 
23439e0ed16aSSagi Grimberg 		set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
2344e399441dSJames Smart 	}
2345e399441dSJames Smart 
2346e399441dSJames Smart 	return ret;
2347e399441dSJames Smart }
2348e399441dSJames Smart 
2349e399441dSJames Smart static void
2350e399441dSJames Smart nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
2351e399441dSJames Smart {
2352e399441dSJames Smart 	int i;
2353e399441dSJames Smart 
2354d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
235508e15075SKeith Busch 		nvme_fc_init_queue(ctrl, i);
2356e399441dSJames Smart }
2357e399441dSJames Smart 
2358e399441dSJames Smart static void
2359e399441dSJames Smart nvme_fc_ctrl_free(struct kref *ref)
2360e399441dSJames Smart {
2361e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl =
2362e399441dSJames Smart 		container_of(ref, struct nvme_fc_ctrl, ref);
2363e399441dSJames Smart 	unsigned long flags;
2364e399441dSJames Smart 
236561bff8efSJames Smart 	if (ctrl->ctrl.tagset) {
236661bff8efSJames Smart 		blk_cleanup_queue(ctrl->ctrl.connect_q);
236761bff8efSJames Smart 		blk_mq_free_tag_set(&ctrl->tag_set);
236861bff8efSJames Smart 	}
236961bff8efSJames Smart 
2370e399441dSJames Smart 	/* remove from rport list */
2371e399441dSJames Smart 	spin_lock_irqsave(&ctrl->rport->lock, flags);
2372e399441dSJames Smart 	list_del(&ctrl->ctrl_list);
2373e399441dSJames Smart 	spin_unlock_irqrestore(&ctrl->rport->lock, flags);
237461bff8efSJames Smart 
2375f9c5af5fSSagi Grimberg 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
237661bff8efSJames Smart 	blk_cleanup_queue(ctrl->ctrl.admin_q);
2377e7832cb4SSagi Grimberg 	blk_cleanup_queue(ctrl->ctrl.fabrics_q);
237861bff8efSJames Smart 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
237961bff8efSJames Smart 
238061bff8efSJames Smart 	kfree(ctrl->queues);
2381e399441dSJames Smart 
2382e399441dSJames Smart 	put_device(ctrl->dev);
2383e399441dSJames Smart 	nvme_fc_rport_put(ctrl->rport);
2384e399441dSJames Smart 
2385e399441dSJames Smart 	ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2386de41447aSEwan D. Milne 	if (ctrl->ctrl.opts)
2387e399441dSJames Smart 		nvmf_free_options(ctrl->ctrl.opts);
2388e399441dSJames Smart 	kfree(ctrl);
2389e399441dSJames Smart }
2390e399441dSJames Smart 
2391e399441dSJames Smart static void
2392e399441dSJames Smart nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2393e399441dSJames Smart {
2394e399441dSJames Smart 	kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2395e399441dSJames Smart }
2396e399441dSJames Smart 
2397e399441dSJames Smart static int
2398e399441dSJames Smart nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2399e399441dSJames Smart {
2400e399441dSJames Smart 	return kref_get_unless_zero(&ctrl->ref);
2401e399441dSJames Smart }
2402e399441dSJames Smart 
2403e399441dSJames Smart /*
2404e399441dSJames Smart  * All accesses from nvme core layer done - can now free the
2405e399441dSJames Smart  * controller. Called after last nvme_put_ctrl() call
2406e399441dSJames Smart  */
2407e399441dSJames Smart static void
240861bff8efSJames Smart nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
2409e399441dSJames Smart {
2410e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2411e399441dSJames Smart 
2412e399441dSJames Smart 	WARN_ON(nctrl != &ctrl->ctrl);
2413e399441dSJames Smart 
2414e399441dSJames Smart 	nvme_fc_ctrl_put(ctrl);
2415e399441dSJames Smart }
2416e399441dSJames Smart 
241761bff8efSJames Smart static void
241861bff8efSJames Smart nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
241961bff8efSJames Smart {
24204cff280aSJames Smart 	int active;
24214cff280aSJames Smart 
24224cff280aSJames Smart 	/*
24234cff280aSJames Smart 	 * if an error (io timeout, etc) while (re)connecting,
24244cff280aSJames Smart 	 * it's an error on creating the new association.
24254cff280aSJames Smart 	 * Start the error recovery thread if it hasn't already
24264cff280aSJames Smart 	 * been started. It is expected there could be multiple
24274cff280aSJames Smart 	 * ios hitting this path before things are cleaned up.
24284cff280aSJames Smart 	 */
24294cff280aSJames Smart 	if (ctrl->ctrl.state == NVME_CTRL_CONNECTING) {
24304cff280aSJames Smart 		active = atomic_xchg(&ctrl->err_work_active, 1);
24318730c1ddSHannes Reinecke 		if (!active && !queue_work(nvme_fc_wq, &ctrl->err_work)) {
24324cff280aSJames Smart 			atomic_set(&ctrl->err_work_active, 0);
24334cff280aSJames Smart 			WARN_ON(1);
24344cff280aSJames Smart 		}
24354cff280aSJames Smart 		return;
24364cff280aSJames Smart 	}
24374cff280aSJames Smart 
24384cff280aSJames Smart 	/* Otherwise, only proceed if in LIVE state - e.g. on first error */
243969fa9646SJames Smart 	if (ctrl->ctrl.state != NVME_CTRL_LIVE)
244069fa9646SJames Smart 		return;
244169fa9646SJames Smart 
244261bff8efSJames Smart 	dev_warn(ctrl->ctrl.device,
2443514a6dc9SJames Smart 		"NVME-FC{%d}: transport association event: %s\n",
244461bff8efSJames Smart 		ctrl->cnum, errmsg);
2445589ff775SJames Smart 	dev_warn(ctrl->ctrl.device,
244661bff8efSJames Smart 		"NVME-FC{%d}: resetting controller\n", ctrl->cnum);
244761bff8efSJames Smart 
2448d86c4d8eSChristoph Hellwig 	nvme_reset_ctrl(&ctrl->ctrl);
244961bff8efSJames Smart }
245061bff8efSJames Smart 
2451baee29acSChristoph Hellwig static enum blk_eh_timer_return
2452e399441dSJames Smart nvme_fc_timeout(struct request *rq, bool reserved)
2453e399441dSJames Smart {
2454e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2455e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
245652793d62SJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
245752793d62SJames Smart 	struct nvme_command *sqe = &cmdiu->sqe;
2458e399441dSJames Smart 
2459e399441dSJames Smart 	/*
246052793d62SJames Smart 	 * Attempt to abort the offending command. Command completion
246152793d62SJames Smart 	 * will detect the aborted io and will fail the connection.
2462e399441dSJames Smart 	 */
246352793d62SJames Smart 	dev_info(ctrl->ctrl.device,
246452793d62SJames Smart 		"NVME-FC{%d.%d}: io timeout: opcode %d fctype %d w10/11: "
246552793d62SJames Smart 		"x%08x/x%08x\n",
246652793d62SJames Smart 		ctrl->cnum, op->queue->qnum, sqe->common.opcode,
246752793d62SJames Smart 		sqe->connect.fctype, sqe->common.cdw10, sqe->common.cdw11);
246852793d62SJames Smart 	if (__nvme_fc_abort_op(ctrl, op))
246952793d62SJames Smart 		nvme_fc_error_recovery(ctrl, "io timeout abort failed");
2470e399441dSJames Smart 
2471134aedc9SJames Smart 	/*
2472134aedc9SJames Smart 	 * the io abort has been initiated. Have the reset timer
2473134aedc9SJames Smart 	 * restarted and the abort completion will complete the io
2474134aedc9SJames Smart 	 * shortly. Avoids a synchronous wait while the abort finishes.
2475134aedc9SJames Smart 	 */
2476134aedc9SJames Smart 	return BLK_EH_RESET_TIMER;
2477e399441dSJames Smart }
2478e399441dSJames Smart 
2479e399441dSJames Smart static int
2480e399441dSJames Smart nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2481e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
2482e399441dSJames Smart {
2483e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
2484e399441dSJames Smart 	int ret;
2485e399441dSJames Smart 
2486e399441dSJames Smart 	freq->sg_cnt = 0;
2487e399441dSJames Smart 
24889f7d8ae2SJames Smart 	if (!blk_rq_nr_phys_segments(rq))
2489e399441dSJames Smart 		return 0;
2490e399441dSJames Smart 
2491e399441dSJames Smart 	freq->sg_table.sgl = freq->first_sgl;
249219e420bbSChristoph Hellwig 	ret = sg_alloc_table_chained(&freq->sg_table,
24934635873cSMing Lei 			blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
2494b1ae1a23SIsrael Rukshin 			NVME_INLINE_SG_CNT);
2495e399441dSJames Smart 	if (ret)
2496e399441dSJames Smart 		return -ENOMEM;
2497e399441dSJames Smart 
2498e399441dSJames Smart 	op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
249919e420bbSChristoph Hellwig 	WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2500e399441dSJames Smart 	freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2501f15872c5SIsrael Rukshin 				op->nents, rq_dma_dir(rq));
2502e399441dSJames Smart 	if (unlikely(freq->sg_cnt <= 0)) {
2503b1ae1a23SIsrael Rukshin 		sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2504e399441dSJames Smart 		freq->sg_cnt = 0;
2505e399441dSJames Smart 		return -EFAULT;
2506e399441dSJames Smart 	}
2507e399441dSJames Smart 
2508e399441dSJames Smart 	/*
2509e399441dSJames Smart 	 * TODO: blk_integrity_rq(rq)  for DIF
2510e399441dSJames Smart 	 */
2511e399441dSJames Smart 	return 0;
2512e399441dSJames Smart }
2513e399441dSJames Smart 
2514e399441dSJames Smart static void
2515e399441dSJames Smart nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2516e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
2517e399441dSJames Smart {
2518e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
2519e399441dSJames Smart 
2520e399441dSJames Smart 	if (!freq->sg_cnt)
2521e399441dSJames Smart 		return;
2522e399441dSJames Smart 
2523e399441dSJames Smart 	fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2524f15872c5SIsrael Rukshin 			rq_dma_dir(rq));
2525e399441dSJames Smart 
2526b1ae1a23SIsrael Rukshin 	sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2527e399441dSJames Smart 
2528e399441dSJames Smart 	freq->sg_cnt = 0;
2529e399441dSJames Smart }
2530e399441dSJames Smart 
2531e399441dSJames Smart /*
2532e399441dSJames Smart  * In FC, the queue is a logical thing. At transport connect, the target
2533e399441dSJames Smart  * creates its "queue" and returns a handle that is to be given to the
2534e399441dSJames Smart  * target whenever it posts something to the corresponding SQ.  When an
2535e399441dSJames Smart  * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2536e399441dSJames Smart  * command contained within the SQE, an io, and assigns a FC exchange
2537e399441dSJames Smart  * to it. The SQE and the associated SQ handle are sent in the initial
2538e399441dSJames Smart  * CMD IU sents on the exchange. All transfers relative to the io occur
2539e399441dSJames Smart  * as part of the exchange.  The CQE is the last thing for the io,
2540e399441dSJames Smart  * which is transferred (explicitly or implicitly) with the RSP IU
2541e399441dSJames Smart  * sent on the exchange. After the CQE is received, the FC exchange is
2542e399441dSJames Smart  * terminaed and the Exchange may be used on a different io.
2543e399441dSJames Smart  *
2544e399441dSJames Smart  * The transport to LLDD api has the transport making a request for a
2545e399441dSJames Smart  * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2546e399441dSJames Smart  * resource and transfers the command. The LLDD will then process all
2547e399441dSJames Smart  * steps to complete the io. Upon completion, the transport done routine
2548e399441dSJames Smart  * is called.
2549e399441dSJames Smart  *
2550e399441dSJames Smart  * So - while the operation is outstanding to the LLDD, there is a link
2551e399441dSJames Smart  * level FC exchange resource that is also outstanding. This must be
2552e399441dSJames Smart  * considered in all cleanup operations.
2553e399441dSJames Smart  */
2554fc17b653SChristoph Hellwig static blk_status_t
2555e399441dSJames Smart nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2556e399441dSJames Smart 	struct nvme_fc_fcp_op *op, u32 data_len,
2557e399441dSJames Smart 	enum nvmefc_fcp_datadir	io_dir)
2558e399441dSJames Smart {
2559e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2560e399441dSJames Smart 	struct nvme_command *sqe = &cmdiu->sqe;
2561b12740d3SJames Smart 	int ret, opstate;
2562e399441dSJames Smart 
256361bff8efSJames Smart 	/*
256461bff8efSJames Smart 	 * before attempting to send the io, check to see if we believe
256561bff8efSJames Smart 	 * the target device is present
256661bff8efSJames Smart 	 */
256761bff8efSJames Smart 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
256886ff7c2aSMing Lei 		return BLK_STS_RESOURCE;
256961bff8efSJames Smart 
2570e399441dSJames Smart 	if (!nvme_fc_ctrl_get(ctrl))
2571fc17b653SChristoph Hellwig 		return BLK_STS_IOERR;
2572e399441dSJames Smart 
2573e399441dSJames Smart 	/* format the FC-NVME CMD IU and fcp_req */
2574e399441dSJames Smart 	cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2575e399441dSJames Smart 	cmdiu->data_len = cpu_to_be32(data_len);
2576e399441dSJames Smart 	switch (io_dir) {
2577e399441dSJames Smart 	case NVMEFC_FCP_WRITE:
2578e399441dSJames Smart 		cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2579e399441dSJames Smart 		break;
2580e399441dSJames Smart 	case NVMEFC_FCP_READ:
2581e399441dSJames Smart 		cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2582e399441dSJames Smart 		break;
2583e399441dSJames Smart 	case NVMEFC_FCP_NODATA:
2584e399441dSJames Smart 		cmdiu->flags = 0;
2585e399441dSJames Smart 		break;
2586e399441dSJames Smart 	}
2587e399441dSJames Smart 	op->fcp_req.payload_length = data_len;
2588e399441dSJames Smart 	op->fcp_req.io_dir = io_dir;
2589e399441dSJames Smart 	op->fcp_req.transferred_length = 0;
2590e399441dSJames Smart 	op->fcp_req.rcv_rsplen = 0;
259162eeacb0SJames Smart 	op->fcp_req.status = NVME_SC_SUCCESS;
2592e399441dSJames Smart 	op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2593e399441dSJames Smart 
2594e399441dSJames Smart 	/*
2595e399441dSJames Smart 	 * validate per fabric rules, set fields mandated by fabric spec
2596e399441dSJames Smart 	 * as well as those by FC-NVME spec.
2597e399441dSJames Smart 	 */
2598e399441dSJames Smart 	WARN_ON_ONCE(sqe->common.metadata);
2599e399441dSJames Smart 	sqe->common.flags |= NVME_CMD_SGL_METABUF;
2600e399441dSJames Smart 
2601e399441dSJames Smart 	/*
2602d9d34c0bSJames Smart 	 * format SQE DPTR field per FC-NVME rules:
2603d9d34c0bSJames Smart 	 *    type=0x5     Transport SGL Data Block Descriptor
2604d9d34c0bSJames Smart 	 *    subtype=0xA  Transport-specific value
2605d9d34c0bSJames Smart 	 *    address=0
2606d9d34c0bSJames Smart 	 *    length=length of the data series
2607e399441dSJames Smart 	 */
2608d9d34c0bSJames Smart 	sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2609d9d34c0bSJames Smart 					NVME_SGL_FMT_TRANSPORT_A;
2610e399441dSJames Smart 	sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2611e399441dSJames Smart 	sqe->rw.dptr.sgl.addr = 0;
2612e399441dSJames Smart 
261378a7ac26SJames Smart 	if (!(op->flags & FCOP_FLAGS_AEN)) {
2614e399441dSJames Smart 		ret = nvme_fc_map_data(ctrl, op->rq, op);
2615e399441dSJames Smart 		if (ret < 0) {
2616e399441dSJames Smart 			nvme_cleanup_cmd(op->rq);
2617e399441dSJames Smart 			nvme_fc_ctrl_put(ctrl);
2618fc17b653SChristoph Hellwig 			if (ret == -ENOMEM || ret == -EAGAIN)
2619fc17b653SChristoph Hellwig 				return BLK_STS_RESOURCE;
2620fc17b653SChristoph Hellwig 			return BLK_STS_IOERR;
2621e399441dSJames Smart 		}
2622e399441dSJames Smart 	}
2623e399441dSJames Smart 
2624e399441dSJames Smart 	fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2625e399441dSJames Smart 				  sizeof(op->cmd_iu), DMA_TO_DEVICE);
2626e399441dSJames Smart 
2627e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2628e399441dSJames Smart 
262978a7ac26SJames Smart 	if (!(op->flags & FCOP_FLAGS_AEN))
2630e399441dSJames Smart 		blk_mq_start_request(op->rq);
2631e399441dSJames Smart 
263267f471b6SJames Smart 	cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2633e399441dSJames Smart 	ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2634e399441dSJames Smart 					&ctrl->rport->remoteport,
2635e399441dSJames Smart 					queue->lldd_handle, &op->fcp_req);
2636e399441dSJames Smart 
2637e399441dSJames Smart 	if (ret) {
263867f471b6SJames Smart 		/*
263967f471b6SJames Smart 		 * If the lld fails to send the command is there an issue with
264067f471b6SJames Smart 		 * the csn value?  If the command that fails is the Connect,
264167f471b6SJames Smart 		 * no - as the connection won't be live.  If it is a command
264267f471b6SJames Smart 		 * post-connect, it's possible a gap in csn may be created.
264367f471b6SJames Smart 		 * Does this matter?  As Linux initiators don't send fused
264467f471b6SJames Smart 		 * commands, no.  The gap would exist, but as there's nothing
264567f471b6SJames Smart 		 * that depends on csn order to be delivered on the target
264667f471b6SJames Smart 		 * side, it shouldn't hurt.  It would be difficult for a
264767f471b6SJames Smart 		 * target to even detect the csn gap as it has no idea when the
264867f471b6SJames Smart 		 * cmd with the csn was supposed to arrive.
264967f471b6SJames Smart 		 */
2650b12740d3SJames Smart 		opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2651b12740d3SJames Smart 		__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2652b12740d3SJames Smart 
2653c9c12e51SDaniel Wagner 		if (!(op->flags & FCOP_FLAGS_AEN)) {
2654e399441dSJames Smart 			nvme_fc_unmap_data(ctrl, op->rq, op);
265516686f3aSMax Gurtovoy 			nvme_cleanup_cmd(op->rq);
2656c9c12e51SDaniel Wagner 		}
2657c9c12e51SDaniel Wagner 
2658e399441dSJames Smart 		nvme_fc_ctrl_put(ctrl);
2659e399441dSJames Smart 
26608b25f351SJames Smart 		if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
26618b25f351SJames Smart 				ret != -EBUSY)
2662fc17b653SChristoph Hellwig 			return BLK_STS_IOERR;
2663e399441dSJames Smart 
266486ff7c2aSMing Lei 		return BLK_STS_RESOURCE;
2665e399441dSJames Smart 	}
2666e399441dSJames Smart 
2667fc17b653SChristoph Hellwig 	return BLK_STS_OK;
2668e399441dSJames Smart }
2669e399441dSJames Smart 
2670fc17b653SChristoph Hellwig static blk_status_t
2671e399441dSJames Smart nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2672e399441dSJames Smart 			const struct blk_mq_queue_data *bd)
2673e399441dSJames Smart {
2674e399441dSJames Smart 	struct nvme_ns *ns = hctx->queue->queuedata;
2675e399441dSJames Smart 	struct nvme_fc_queue *queue = hctx->driver_data;
2676e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = queue->ctrl;
2677e399441dSJames Smart 	struct request *rq = bd->rq;
2678e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2679e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2680e399441dSJames Smart 	struct nvme_command *sqe = &cmdiu->sqe;
2681e399441dSJames Smart 	enum nvmefc_fcp_datadir	io_dir;
26823bc32bb1SChristoph Hellwig 	bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2683e399441dSJames Smart 	u32 data_len;
2684fc17b653SChristoph Hellwig 	blk_status_t ret;
2685e399441dSJames Smart 
26863bc32bb1SChristoph Hellwig 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
26873bc32bb1SChristoph Hellwig 	    !nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
26886cdefc6eSJames Smart 		return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
26899e0ed16aSSagi Grimberg 
2690e399441dSJames Smart 	ret = nvme_setup_cmd(ns, rq, sqe);
2691e399441dSJames Smart 	if (ret)
2692e399441dSJames Smart 		return ret;
2693e399441dSJames Smart 
26949f7d8ae2SJames Smart 	/*
26959f7d8ae2SJames Smart 	 * nvme core doesn't quite treat the rq opaquely. Commands such
26969f7d8ae2SJames Smart 	 * as WRITE ZEROES will return a non-zero rq payload_bytes yet
26979f7d8ae2SJames Smart 	 * there is no actual payload to be transferred.
26989f7d8ae2SJames Smart 	 * To get it right, key data transmission on there being 1 or
26999f7d8ae2SJames Smart 	 * more physical segments in the sg list. If there is no
27009f7d8ae2SJames Smart 	 * physical segments, there is no payload.
27019f7d8ae2SJames Smart 	 */
27029f7d8ae2SJames Smart 	if (blk_rq_nr_phys_segments(rq)) {
2703b131c61dSChristoph Hellwig 		data_len = blk_rq_payload_bytes(rq);
2704e399441dSJames Smart 		io_dir = ((rq_data_dir(rq) == WRITE) ?
2705e399441dSJames Smart 					NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
27069f7d8ae2SJames Smart 	} else {
27079f7d8ae2SJames Smart 		data_len = 0;
2708e399441dSJames Smart 		io_dir = NVMEFC_FCP_NODATA;
27099f7d8ae2SJames Smart 	}
27109f7d8ae2SJames Smart 
2711e399441dSJames Smart 
2712e399441dSJames Smart 	return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2713e399441dSJames Smart }
2714e399441dSJames Smart 
2715e399441dSJames Smart static void
2716ad22c355SKeith Busch nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2717e399441dSJames Smart {
2718e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2719e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op;
2720fc17b653SChristoph Hellwig 	blk_status_t ret;
2721e399441dSJames Smart 
2722eb4ee8f1SJames Smart 	if (test_bit(FCCTRL_TERMIO, &ctrl->flags))
272361bff8efSJames Smart 		return;
272461bff8efSJames Smart 
2725ad22c355SKeith Busch 	aen_op = &ctrl->aen_ops[0];
2726e399441dSJames Smart 
2727e399441dSJames Smart 	ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2728e399441dSJames Smart 					NVMEFC_FCP_NODATA);
2729e399441dSJames Smart 	if (ret)
2730e399441dSJames Smart 		dev_err(ctrl->ctrl.device,
2731ad22c355SKeith Busch 			"failed async event work\n");
2732e399441dSJames Smart }
2733e399441dSJames Smart 
2734e399441dSJames Smart static void
2735c3aedd22SJames Smart nvme_fc_complete_rq(struct request *rq)
2736e399441dSJames Smart {
2737e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2738e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
2739e399441dSJames Smart 
274078a7ac26SJames Smart 	atomic_set(&op->state, FCPOP_STATE_IDLE);
274152793d62SJames Smart 	op->flags &= ~FCOP_FLAGS_TERMIO;
2742e399441dSJames Smart 
2743e399441dSJames Smart 	nvme_fc_unmap_data(ctrl, rq, op);
274477f02a7aSChristoph Hellwig 	nvme_complete_rq(rq);
2745e399441dSJames Smart 	nvme_fc_ctrl_put(ctrl);
274678a7ac26SJames Smart }
274778a7ac26SJames Smart 
2748e399441dSJames Smart /*
2749e399441dSJames Smart  * This routine is used by the transport when it needs to find active
2750e399441dSJames Smart  * io on a queue that is to be terminated. The transport uses
2751e399441dSJames Smart  * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2752e399441dSJames Smart  * this routine to kill them on a 1 by 1 basis.
2753e399441dSJames Smart  *
2754e399441dSJames Smart  * As FC allocates FC exchange for each io, the transport must contact
2755e399441dSJames Smart  * the LLDD to terminate the exchange, thus releasing the FC exchange.
2756e399441dSJames Smart  * After terminating the exchange the LLDD will call the transport's
2757e399441dSJames Smart  * normal io done path for the request, but it will have an aborted
2758e399441dSJames Smart  * status. The done path will return the io request back to the block
2759e399441dSJames Smart  * layer with an error status.
2760e399441dSJames Smart  */
27617baa8572SJens Axboe static bool
2762e399441dSJames Smart nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2763e399441dSJames Smart {
2764e399441dSJames Smart 	struct nvme_ctrl *nctrl = data;
2765e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2766e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2767e399441dSJames Smart 
27683efd6e8eSJames Smart 	__nvme_fc_abort_op(ctrl, op);
27697baa8572SJens Axboe 	return true;
277078a7ac26SJames Smart }
2771e399441dSJames Smart 
2772e399441dSJames Smart 
277361bff8efSJames Smart static const struct blk_mq_ops nvme_fc_mq_ops = {
277461bff8efSJames Smart 	.queue_rq	= nvme_fc_queue_rq,
277561bff8efSJames Smart 	.complete	= nvme_fc_complete_rq,
277661bff8efSJames Smart 	.init_request	= nvme_fc_init_request,
277761bff8efSJames Smart 	.exit_request	= nvme_fc_exit_request,
277861bff8efSJames Smart 	.init_hctx	= nvme_fc_init_hctx,
277961bff8efSJames Smart 	.timeout	= nvme_fc_timeout,
2780e399441dSJames Smart };
2781e399441dSJames Smart 
2782e399441dSJames Smart static int
2783e399441dSJames Smart nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2784e399441dSJames Smart {
2785e399441dSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
27867314183dSSagi Grimberg 	unsigned int nr_io_queues;
2787e399441dSJames Smart 	int ret;
2788e399441dSJames Smart 
27897314183dSSagi Grimberg 	nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
27907314183dSSagi Grimberg 				ctrl->lport->ops->max_hw_queues);
27917314183dSSagi Grimberg 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2792e399441dSJames Smart 	if (ret) {
2793e399441dSJames Smart 		dev_info(ctrl->ctrl.device,
2794e399441dSJames Smart 			"set_queue_count failed: %d\n", ret);
2795e399441dSJames Smart 		return ret;
2796e399441dSJames Smart 	}
2797e399441dSJames Smart 
27987314183dSSagi Grimberg 	ctrl->ctrl.queue_count = nr_io_queues + 1;
27997314183dSSagi Grimberg 	if (!nr_io_queues)
2800e399441dSJames Smart 		return 0;
2801e399441dSJames Smart 
2802e399441dSJames Smart 	nvme_fc_init_io_queues(ctrl);
2803e399441dSJames Smart 
2804e399441dSJames Smart 	memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2805e399441dSJames Smart 	ctrl->tag_set.ops = &nvme_fc_mq_ops;
2806e399441dSJames Smart 	ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2807e399441dSJames Smart 	ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2808103e515eSHannes Reinecke 	ctrl->tag_set.numa_node = ctrl->ctrl.numa_node;
2809e399441dSJames Smart 	ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2810d3d0bc78SBart Van Assche 	ctrl->tag_set.cmd_size =
2811d3d0bc78SBart Van Assche 		struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv,
2812d3d0bc78SBart Van Assche 			    ctrl->lport->ops->fcprqst_priv_sz);
2813e399441dSJames Smart 	ctrl->tag_set.driver_data = ctrl;
2814d858e5f0SSagi Grimberg 	ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
2815e399441dSJames Smart 	ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2816e399441dSJames Smart 
2817e399441dSJames Smart 	ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2818e399441dSJames Smart 	if (ret)
2819e399441dSJames Smart 		return ret;
2820e399441dSJames Smart 
2821e399441dSJames Smart 	ctrl->ctrl.tagset = &ctrl->tag_set;
2822e399441dSJames Smart 
2823e399441dSJames Smart 	ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2824e399441dSJames Smart 	if (IS_ERR(ctrl->ctrl.connect_q)) {
2825e399441dSJames Smart 		ret = PTR_ERR(ctrl->ctrl.connect_q);
2826e399441dSJames Smart 		goto out_free_tag_set;
2827e399441dSJames Smart 	}
2828e399441dSJames Smart 
2829d157e534SJames Smart 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2830e399441dSJames Smart 	if (ret)
2831e399441dSJames Smart 		goto out_cleanup_blk_queue;
2832e399441dSJames Smart 
2833d157e534SJames Smart 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2834e399441dSJames Smart 	if (ret)
2835e399441dSJames Smart 		goto out_delete_hw_queues;
2836e399441dSJames Smart 
28374c984154SJames Smart 	ctrl->ioq_live = true;
28384c984154SJames Smart 
2839e399441dSJames Smart 	return 0;
2840e399441dSJames Smart 
2841e399441dSJames Smart out_delete_hw_queues:
2842e399441dSJames Smart 	nvme_fc_delete_hw_io_queues(ctrl);
2843e399441dSJames Smart out_cleanup_blk_queue:
2844e399441dSJames Smart 	blk_cleanup_queue(ctrl->ctrl.connect_q);
2845e399441dSJames Smart out_free_tag_set:
2846e399441dSJames Smart 	blk_mq_free_tag_set(&ctrl->tag_set);
2847e399441dSJames Smart 	nvme_fc_free_io_queues(ctrl);
2848e399441dSJames Smart 
2849e399441dSJames Smart 	/* force put free routine to ignore io queues */
2850e399441dSJames Smart 	ctrl->ctrl.tagset = NULL;
2851e399441dSJames Smart 
2852e399441dSJames Smart 	return ret;
2853e399441dSJames Smart }
2854e399441dSJames Smart 
285561bff8efSJames Smart static int
28563e493c00SJames Smart nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
285761bff8efSJames Smart {
285861bff8efSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2859834d3710SJames Smart 	u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
28607314183dSSagi Grimberg 	unsigned int nr_io_queues;
286161bff8efSJames Smart 	int ret;
286261bff8efSJames Smart 
28637314183dSSagi Grimberg 	nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
28647314183dSSagi Grimberg 				ctrl->lport->ops->max_hw_queues);
28657314183dSSagi Grimberg 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
286661bff8efSJames Smart 	if (ret) {
286761bff8efSJames Smart 		dev_info(ctrl->ctrl.device,
286861bff8efSJames Smart 			"set_queue_count failed: %d\n", ret);
286961bff8efSJames Smart 		return ret;
287061bff8efSJames Smart 	}
287161bff8efSJames Smart 
2872834d3710SJames Smart 	if (!nr_io_queues && prior_ioq_cnt) {
2873834d3710SJames Smart 		dev_info(ctrl->ctrl.device,
2874834d3710SJames Smart 			"Fail Reconnect: At least 1 io queue "
2875834d3710SJames Smart 			"required (was %d)\n", prior_ioq_cnt);
2876834d3710SJames Smart 		return -ENOSPC;
2877834d3710SJames Smart 	}
2878834d3710SJames Smart 
28797314183dSSagi Grimberg 	ctrl->ctrl.queue_count = nr_io_queues + 1;
288061bff8efSJames Smart 	/* check for io queues existing */
2881d858e5f0SSagi Grimberg 	if (ctrl->ctrl.queue_count == 1)
288261bff8efSJames Smart 		return 0;
288361bff8efSJames Smart 
2884d157e534SJames Smart 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
288561bff8efSJames Smart 	if (ret)
288661bff8efSJames Smart 		goto out_free_io_queues;
288761bff8efSJames Smart 
2888d157e534SJames Smart 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
288961bff8efSJames Smart 	if (ret)
289061bff8efSJames Smart 		goto out_delete_hw_queues;
289161bff8efSJames Smart 
289288e837edSJames Smart 	if (prior_ioq_cnt != nr_io_queues) {
2893834d3710SJames Smart 		dev_info(ctrl->ctrl.device,
2894834d3710SJames Smart 			"reconnect: revising io queue count from %d to %d\n",
2895834d3710SJames Smart 			prior_ioq_cnt, nr_io_queues);
289688e837edSJames Smart 		nvme_wait_freeze(&ctrl->ctrl);
2897cda5fd1aSSagi Grimberg 		blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
289888e837edSJames Smart 		nvme_unfreeze(&ctrl->ctrl);
289988e837edSJames Smart 	}
2900cda5fd1aSSagi Grimberg 
290161bff8efSJames Smart 	return 0;
290261bff8efSJames Smart 
290361bff8efSJames Smart out_delete_hw_queues:
290461bff8efSJames Smart 	nvme_fc_delete_hw_io_queues(ctrl);
290561bff8efSJames Smart out_free_io_queues:
290661bff8efSJames Smart 	nvme_fc_free_io_queues(ctrl);
290761bff8efSJames Smart 	return ret;
290861bff8efSJames Smart }
290961bff8efSJames Smart 
2910158bfb88SJames Smart static void
2911158bfb88SJames Smart nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
2912158bfb88SJames Smart {
2913158bfb88SJames Smart 	struct nvme_fc_lport *lport = rport->lport;
2914158bfb88SJames Smart 
2915158bfb88SJames Smart 	atomic_inc(&lport->act_rport_cnt);
2916158bfb88SJames Smart }
2917158bfb88SJames Smart 
2918158bfb88SJames Smart static void
2919158bfb88SJames Smart nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
2920158bfb88SJames Smart {
2921158bfb88SJames Smart 	struct nvme_fc_lport *lport = rport->lport;
2922158bfb88SJames Smart 	u32 cnt;
2923158bfb88SJames Smart 
2924158bfb88SJames Smart 	cnt = atomic_dec_return(&lport->act_rport_cnt);
2925158bfb88SJames Smart 	if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
2926158bfb88SJames Smart 		lport->ops->localport_delete(&lport->localport);
2927158bfb88SJames Smart }
2928158bfb88SJames Smart 
2929158bfb88SJames Smart static int
2930158bfb88SJames Smart nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
2931158bfb88SJames Smart {
2932158bfb88SJames Smart 	struct nvme_fc_rport *rport = ctrl->rport;
2933158bfb88SJames Smart 	u32 cnt;
2934158bfb88SJames Smart 
2935eb4ee8f1SJames Smart 	if (test_and_set_bit(ASSOC_ACTIVE, &ctrl->flags))
2936158bfb88SJames Smart 		return 1;
2937158bfb88SJames Smart 
2938158bfb88SJames Smart 	cnt = atomic_inc_return(&rport->act_ctrl_cnt);
2939158bfb88SJames Smart 	if (cnt == 1)
2940158bfb88SJames Smart 		nvme_fc_rport_active_on_lport(rport);
2941158bfb88SJames Smart 
2942158bfb88SJames Smart 	return 0;
2943158bfb88SJames Smart }
2944158bfb88SJames Smart 
2945158bfb88SJames Smart static int
2946158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
2947158bfb88SJames Smart {
2948158bfb88SJames Smart 	struct nvme_fc_rport *rport = ctrl->rport;
2949158bfb88SJames Smart 	struct nvme_fc_lport *lport = rport->lport;
2950158bfb88SJames Smart 	u32 cnt;
2951158bfb88SJames Smart 
2952eb4ee8f1SJames Smart 	/* clearing of ctrl->flags ASSOC_ACTIVE bit is in association delete */
2953158bfb88SJames Smart 
2954158bfb88SJames Smart 	cnt = atomic_dec_return(&rport->act_ctrl_cnt);
2955158bfb88SJames Smart 	if (cnt == 0) {
2956158bfb88SJames Smart 		if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
2957158bfb88SJames Smart 			lport->ops->remoteport_delete(&rport->remoteport);
2958158bfb88SJames Smart 		nvme_fc_rport_inactive_on_lport(rport);
2959158bfb88SJames Smart 	}
2960158bfb88SJames Smart 
2961158bfb88SJames Smart 	return 0;
2962158bfb88SJames Smart }
2963158bfb88SJames Smart 
296461bff8efSJames Smart /*
296561bff8efSJames Smart  * This routine restarts the controller on the host side, and
296661bff8efSJames Smart  * on the link side, recreates the controller association.
296761bff8efSJames Smart  */
296861bff8efSJames Smart static int
296961bff8efSJames Smart nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
297061bff8efSJames Smart {
297161bff8efSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
297214fd1e98SJames Smart 	struct nvmefc_ls_rcv_op *disls = NULL;
297314fd1e98SJames Smart 	unsigned long flags;
297461bff8efSJames Smart 	int ret;
297561bff8efSJames Smart 	bool changed;
297661bff8efSJames Smart 
2977fdf9dfa8SSagi Grimberg 	++ctrl->ctrl.nr_reconnects;
297861bff8efSJames Smart 
297996e24801SJames Smart 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
298096e24801SJames Smart 		return -ENODEV;
298196e24801SJames Smart 
2982158bfb88SJames Smart 	if (nvme_fc_ctlr_active_on_rport(ctrl))
2983158bfb88SJames Smart 		return -ENOTUNIQ;
2984158bfb88SJames Smart 
29854bea364fSJames Smart 	dev_info(ctrl->ctrl.device,
29864bea364fSJames Smart 		"NVME-FC{%d}: create association : host wwpn 0x%016llx "
29874bea364fSJames Smart 		" rport wwpn 0x%016llx: NQN \"%s\"\n",
29884bea364fSJames Smart 		ctrl->cnum, ctrl->lport->localport.port_name,
29894bea364fSJames Smart 		ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
29904bea364fSJames Smart 
299161bff8efSJames Smart 	/*
299261bff8efSJames Smart 	 * Create the admin queue
299361bff8efSJames Smart 	 */
299461bff8efSJames Smart 
299561bff8efSJames Smart 	ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
2996d157e534SJames Smart 				NVME_AQ_DEPTH);
299761bff8efSJames Smart 	if (ret)
299861bff8efSJames Smart 		goto out_free_queue;
299961bff8efSJames Smart 
300061bff8efSJames Smart 	ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
3001d157e534SJames Smart 				NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
300261bff8efSJames Smart 	if (ret)
300361bff8efSJames Smart 		goto out_delete_hw_queue;
300461bff8efSJames Smart 
300561bff8efSJames Smart 	ret = nvmf_connect_admin_queue(&ctrl->ctrl);
300661bff8efSJames Smart 	if (ret)
300761bff8efSJames Smart 		goto out_disconnect_admin_queue;
300861bff8efSJames Smart 
30099e0ed16aSSagi Grimberg 	set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
30109e0ed16aSSagi Grimberg 
301161bff8efSJames Smart 	/*
301261bff8efSJames Smart 	 * Check controller capabilities
301361bff8efSJames Smart 	 *
301461bff8efSJames Smart 	 * todo:- add code to check if ctrl attributes changed from
301561bff8efSJames Smart 	 * prior connection values
301661bff8efSJames Smart 	 */
301761bff8efSJames Smart 
3018c0f2f45bSSagi Grimberg 	ret = nvme_enable_ctrl(&ctrl->ctrl);
301961bff8efSJames Smart 	if (ret)
302061bff8efSJames Smart 		goto out_disconnect_admin_queue;
302161bff8efSJames Smart 
302223748076SJames Smart 	ctrl->ctrl.max_segments = ctrl->lport->ops->max_sgl_segments;
302323748076SJames Smart 	ctrl->ctrl.max_hw_sectors = ctrl->ctrl.max_segments <<
302423748076SJames Smart 						(ilog2(SZ_4K) - 9);
302561bff8efSJames Smart 
3026e7832cb4SSagi Grimberg 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
3027e7832cb4SSagi Grimberg 
302861bff8efSJames Smart 	ret = nvme_init_identify(&ctrl->ctrl);
302961bff8efSJames Smart 	if (ret)
303061bff8efSJames Smart 		goto out_disconnect_admin_queue;
303161bff8efSJames Smart 
303261bff8efSJames Smart 	/* sanity checks */
303361bff8efSJames Smart 
303461bff8efSJames Smart 	/* FC-NVME does not have other data in the capsule */
303561bff8efSJames Smart 	if (ctrl->ctrl.icdoff) {
303661bff8efSJames Smart 		dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
303761bff8efSJames Smart 				ctrl->ctrl.icdoff);
303861bff8efSJames Smart 		goto out_disconnect_admin_queue;
303961bff8efSJames Smart 	}
304061bff8efSJames Smart 
304161bff8efSJames Smart 	/* FC-NVME supports normal SGL Data Block Descriptors */
304261bff8efSJames Smart 
304361bff8efSJames Smart 	if (opts->queue_size > ctrl->ctrl.maxcmd) {
304461bff8efSJames Smart 		/* warn if maxcmd is lower than queue_size */
304561bff8efSJames Smart 		dev_warn(ctrl->ctrl.device,
304661bff8efSJames Smart 			"queue_size %zu > ctrl maxcmd %u, reducing "
30477db39484SJames Smart 			"to maxcmd\n",
304861bff8efSJames Smart 			opts->queue_size, ctrl->ctrl.maxcmd);
304961bff8efSJames Smart 		opts->queue_size = ctrl->ctrl.maxcmd;
305061bff8efSJames Smart 	}
305161bff8efSJames Smart 
3052d157e534SJames Smart 	if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
3053d157e534SJames Smart 		/* warn if sqsize is lower than queue_size */
3054d157e534SJames Smart 		dev_warn(ctrl->ctrl.device,
30557db39484SJames Smart 			"queue_size %zu > ctrl sqsize %u, reducing "
30567db39484SJames Smart 			"to sqsize\n",
3057d157e534SJames Smart 			opts->queue_size, ctrl->ctrl.sqsize + 1);
3058d157e534SJames Smart 		opts->queue_size = ctrl->ctrl.sqsize + 1;
3059d157e534SJames Smart 	}
3060d157e534SJames Smart 
306161bff8efSJames Smart 	ret = nvme_fc_init_aen_ops(ctrl);
306261bff8efSJames Smart 	if (ret)
306361bff8efSJames Smart 		goto out_term_aen_ops;
306461bff8efSJames Smart 
306561bff8efSJames Smart 	/*
306661bff8efSJames Smart 	 * Create the io queues
306761bff8efSJames Smart 	 */
306861bff8efSJames Smart 
3069d858e5f0SSagi Grimberg 	if (ctrl->ctrl.queue_count > 1) {
30704c984154SJames Smart 		if (!ctrl->ioq_live)
307161bff8efSJames Smart 			ret = nvme_fc_create_io_queues(ctrl);
307261bff8efSJames Smart 		else
30733e493c00SJames Smart 			ret = nvme_fc_recreate_io_queues(ctrl);
307461bff8efSJames Smart 		if (ret)
307561bff8efSJames Smart 			goto out_term_aen_ops;
307661bff8efSJames Smart 	}
307761bff8efSJames Smart 
307861bff8efSJames Smart 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
307961bff8efSJames Smart 
3080fdf9dfa8SSagi Grimberg 	ctrl->ctrl.nr_reconnects = 0;
308161bff8efSJames Smart 
308244c6ec77SJames Smart 	if (changed)
3083d09f2b45SSagi Grimberg 		nvme_start_ctrl(&ctrl->ctrl);
308461bff8efSJames Smart 
308561bff8efSJames Smart 	return 0;	/* Success */
308661bff8efSJames Smart 
308761bff8efSJames Smart out_term_aen_ops:
308861bff8efSJames Smart 	nvme_fc_term_aen_ops(ctrl);
308961bff8efSJames Smart out_disconnect_admin_queue:
309061bff8efSJames Smart 	/* send a Disconnect(association) LS to fc-nvme target */
309161bff8efSJames Smart 	nvme_fc_xmt_disconnect_assoc(ctrl);
309214fd1e98SJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
3093bcde5f0fSJames Smart 	ctrl->association_id = 0;
309414fd1e98SJames Smart 	disls = ctrl->rcv_disconn;
309514fd1e98SJames Smart 	ctrl->rcv_disconn = NULL;
309614fd1e98SJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
309714fd1e98SJames Smart 	if (disls)
309814fd1e98SJames Smart 		nvme_fc_xmt_ls_rsp(disls);
309961bff8efSJames Smart out_delete_hw_queue:
310061bff8efSJames Smart 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
310161bff8efSJames Smart out_free_queue:
310261bff8efSJames Smart 	nvme_fc_free_queue(&ctrl->queues[0]);
3103eb4ee8f1SJames Smart 	clear_bit(ASSOC_ACTIVE, &ctrl->flags);
3104158bfb88SJames Smart 	nvme_fc_ctlr_inactive_on_rport(ctrl);
310561bff8efSJames Smart 
310661bff8efSJames Smart 	return ret;
310761bff8efSJames Smart }
310861bff8efSJames Smart 
310952793d62SJames Smart 
311052793d62SJames Smart /*
311152793d62SJames Smart  * This routine runs through all outstanding commands on the association
311252793d62SJames Smart  * and aborts them.  This routine is typically be called by the
311352793d62SJames Smart  * delete_association routine. It is also called due to an error during
311452793d62SJames Smart  * reconnect. In that scenario, it is most likely a command that initializes
311552793d62SJames Smart  * the controller, including fabric Connect commands on io queues, that
311652793d62SJames Smart  * may have timed out or failed thus the io must be killed for the connect
311752793d62SJames Smart  * thread to see the error.
311852793d62SJames Smart  */
311952793d62SJames Smart static void
312052793d62SJames Smart __nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl *ctrl, bool start_queues)
312152793d62SJames Smart {
312252793d62SJames Smart 	/*
312352793d62SJames Smart 	 * If io queues are present, stop them and terminate all outstanding
312452793d62SJames Smart 	 * ios on them. As FC allocates FC exchange for each io, the
312552793d62SJames Smart 	 * transport must contact the LLDD to terminate the exchange,
312652793d62SJames Smart 	 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
312752793d62SJames Smart 	 * to tell us what io's are busy and invoke a transport routine
312852793d62SJames Smart 	 * to kill them with the LLDD.  After terminating the exchange
312952793d62SJames Smart 	 * the LLDD will call the transport's normal io done path, but it
313052793d62SJames Smart 	 * will have an aborted status. The done path will return the
313152793d62SJames Smart 	 * io requests back to the block layer as part of normal completions
313252793d62SJames Smart 	 * (but with error status).
313352793d62SJames Smart 	 */
313452793d62SJames Smart 	if (ctrl->ctrl.queue_count > 1) {
313552793d62SJames Smart 		nvme_stop_queues(&ctrl->ctrl);
313652793d62SJames Smart 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
313752793d62SJames Smart 				nvme_fc_terminate_exchange, &ctrl->ctrl);
313852793d62SJames Smart 		blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
313952793d62SJames Smart 		if (start_queues)
314052793d62SJames Smart 			nvme_start_queues(&ctrl->ctrl);
314152793d62SJames Smart 	}
314252793d62SJames Smart 
314352793d62SJames Smart 	/*
314452793d62SJames Smart 	 * Other transports, which don't have link-level contexts bound
314552793d62SJames Smart 	 * to sqe's, would try to gracefully shutdown the controller by
314652793d62SJames Smart 	 * writing the registers for shutdown and polling (call
314752793d62SJames Smart 	 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
314852793d62SJames Smart 	 * just aborted and we will wait on those contexts, and given
314952793d62SJames Smart 	 * there was no indication of how live the controlelr is on the
315052793d62SJames Smart 	 * link, don't send more io to create more contexts for the
315152793d62SJames Smart 	 * shutdown. Let the controller fail via keepalive failure if
315252793d62SJames Smart 	 * its still present.
315352793d62SJames Smart 	 */
315452793d62SJames Smart 
315552793d62SJames Smart 	/*
315652793d62SJames Smart 	 * clean up the admin queue. Same thing as above.
315752793d62SJames Smart 	 */
315852793d62SJames Smart 	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
315952793d62SJames Smart 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
316052793d62SJames Smart 				nvme_fc_terminate_exchange, &ctrl->ctrl);
316152793d62SJames Smart 	blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
316252793d62SJames Smart }
316352793d62SJames Smart 
316461bff8efSJames Smart /*
316561bff8efSJames Smart  * This routine stops operation of the controller on the host side.
316661bff8efSJames Smart  * On the host os stack side: Admin and IO queues are stopped,
316761bff8efSJames Smart  *   outstanding ios on them terminated via FC ABTS.
316861bff8efSJames Smart  * On the link side: the association is terminated.
316961bff8efSJames Smart  */
317061bff8efSJames Smart static void
317161bff8efSJames Smart nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
317261bff8efSJames Smart {
317314fd1e98SJames Smart 	struct nvmefc_ls_rcv_op *disls = NULL;
317461bff8efSJames Smart 	unsigned long flags;
317561bff8efSJames Smart 
3176eb4ee8f1SJames Smart 	if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
3177158bfb88SJames Smart 		return;
3178158bfb88SJames Smart 
317961bff8efSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
3180eb4ee8f1SJames Smart 	set_bit(FCCTRL_TERMIO, &ctrl->flags);
318161bff8efSJames Smart 	ctrl->iocnt = 0;
318261bff8efSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
318361bff8efSJames Smart 
318452793d62SJames Smart 	__nvme_fc_abort_outstanding_ios(ctrl, false);
318561bff8efSJames Smart 
318661bff8efSJames Smart 	/* kill the aens as they are a separate path */
318761bff8efSJames Smart 	nvme_fc_abort_aen_ops(ctrl);
318861bff8efSJames Smart 
318961bff8efSJames Smart 	/* wait for all io that had to be aborted */
31908a82dbf1SJames Smart 	spin_lock_irq(&ctrl->lock);
319136715cf4SJames Smart 	wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
3192eb4ee8f1SJames Smart 	clear_bit(FCCTRL_TERMIO, &ctrl->flags);
31938a82dbf1SJames Smart 	spin_unlock_irq(&ctrl->lock);
319461bff8efSJames Smart 
319561bff8efSJames Smart 	nvme_fc_term_aen_ops(ctrl);
319661bff8efSJames Smart 
319761bff8efSJames Smart 	/*
319861bff8efSJames Smart 	 * send a Disconnect(association) LS to fc-nvme target
319961bff8efSJames Smart 	 * Note: could have been sent at top of process, but
320061bff8efSJames Smart 	 * cleaner on link traffic if after the aborts complete.
320161bff8efSJames Smart 	 * Note: if association doesn't exist, association_id will be 0
320261bff8efSJames Smart 	 */
320361bff8efSJames Smart 	if (ctrl->association_id)
320461bff8efSJames Smart 		nvme_fc_xmt_disconnect_assoc(ctrl);
320561bff8efSJames Smart 
320614fd1e98SJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
3207bcde5f0fSJames Smart 	ctrl->association_id = 0;
320814fd1e98SJames Smart 	disls = ctrl->rcv_disconn;
320914fd1e98SJames Smart 	ctrl->rcv_disconn = NULL;
321014fd1e98SJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
321114fd1e98SJames Smart 	if (disls)
321214fd1e98SJames Smart 		/*
321314fd1e98SJames Smart 		 * if a Disconnect Request was waiting for a response, send
321414fd1e98SJames Smart 		 * now that all ABTS's have been issued (and are complete).
321514fd1e98SJames Smart 		 */
321614fd1e98SJames Smart 		nvme_fc_xmt_ls_rsp(disls);
3217bcde5f0fSJames Smart 
321861bff8efSJames Smart 	if (ctrl->ctrl.tagset) {
321961bff8efSJames Smart 		nvme_fc_delete_hw_io_queues(ctrl);
322061bff8efSJames Smart 		nvme_fc_free_io_queues(ctrl);
322161bff8efSJames Smart 	}
322261bff8efSJames Smart 
322361bff8efSJames Smart 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
322461bff8efSJames Smart 	nvme_fc_free_queue(&ctrl->queues[0]);
3225158bfb88SJames Smart 
3226d625d05eSJames Smart 	/* re-enable the admin_q so anything new can fast fail */
3227d625d05eSJames Smart 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
3228d625d05eSJames Smart 
322902d62a8bSJames Smart 	/* resume the io queues so that things will fast fail */
323002d62a8bSJames Smart 	nvme_start_queues(&ctrl->ctrl);
323102d62a8bSJames Smart 
3232158bfb88SJames Smart 	nvme_fc_ctlr_inactive_on_rport(ctrl);
323361bff8efSJames Smart }
323461bff8efSJames Smart 
323561bff8efSJames Smart static void
3236c5017e85SChristoph Hellwig nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
323761bff8efSJames Smart {
3238c5017e85SChristoph Hellwig 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
323961bff8efSJames Smart 
32404cff280aSJames Smart 	cancel_work_sync(&ctrl->err_work);
324161bff8efSJames Smart 	cancel_delayed_work_sync(&ctrl->connect_work);
324261bff8efSJames Smart 	/*
324361bff8efSJames Smart 	 * kill the association on the link side.  this will block
324461bff8efSJames Smart 	 * waiting for io to terminate
324561bff8efSJames Smart 	 */
324661bff8efSJames Smart 	nvme_fc_delete_association(ctrl);
324761bff8efSJames Smart }
324861bff8efSJames Smart 
324961bff8efSJames Smart static void
32505bbecdbcSJames Smart nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
32515bbecdbcSJames Smart {
32522b632970SJames Smart 	struct nvme_fc_rport *rport = ctrl->rport;
32532b632970SJames Smart 	struct nvme_fc_remote_port *portptr = &rport->remoteport;
32542b632970SJames Smart 	unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
32552b632970SJames Smart 	bool recon = true;
32565bbecdbcSJames Smart 
3257ad6a0a52SMax Gurtovoy 	if (ctrl->ctrl.state != NVME_CTRL_CONNECTING)
32585bbecdbcSJames Smart 		return;
32595bbecdbcSJames Smart 
32602b632970SJames Smart 	if (portptr->port_state == FC_OBJSTATE_ONLINE)
3261589ff775SJames Smart 		dev_info(ctrl->ctrl.device,
32625bbecdbcSJames Smart 			"NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
32635bbecdbcSJames Smart 			ctrl->cnum, status);
32642b632970SJames Smart 	else if (time_after_eq(jiffies, rport->dev_loss_end))
32652b632970SJames Smart 		recon = false;
32665bbecdbcSJames Smart 
32672b632970SJames Smart 	if (recon && nvmf_should_reconnect(&ctrl->ctrl)) {
32682b632970SJames Smart 		if (portptr->port_state == FC_OBJSTATE_ONLINE)
32695bbecdbcSJames Smart 			dev_info(ctrl->ctrl.device,
32702b632970SJames Smart 				"NVME-FC{%d}: Reconnect attempt in %ld "
32712b632970SJames Smart 				"seconds\n",
32722b632970SJames Smart 				ctrl->cnum, recon_delay / HZ);
32732b632970SJames Smart 		else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
32742b632970SJames Smart 			recon_delay = rport->dev_loss_end - jiffies;
32752b632970SJames Smart 
32762b632970SJames Smart 		queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
32775bbecdbcSJames Smart 	} else {
32782b632970SJames Smart 		if (portptr->port_state == FC_OBJSTATE_ONLINE)
3279589ff775SJames Smart 			dev_warn(ctrl->ctrl.device,
32805bbecdbcSJames Smart 				"NVME-FC{%d}: Max reconnect attempts (%d) "
328177d0612dSMax Gurtovoy 				"reached.\n",
3282fdf9dfa8SSagi Grimberg 				ctrl->cnum, ctrl->ctrl.nr_reconnects);
32832b632970SJames Smart 		else
32842b632970SJames Smart 			dev_warn(ctrl->ctrl.device,
32852b632970SJames Smart 				"NVME-FC{%d}: dev_loss_tmo (%d) expired "
328677d0612dSMax Gurtovoy 				"while waiting for remoteport connectivity.\n",
3287614fc1c0SMartin George 				ctrl->cnum, min_t(int, portptr->dev_loss_tmo,
3288614fc1c0SMartin George 					(ctrl->ctrl.opts->max_reconnects *
3289614fc1c0SMartin George 					 ctrl->ctrl.opts->reconnect_delay)));
3290c5017e85SChristoph Hellwig 		WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
32915bbecdbcSJames Smart 	}
32925bbecdbcSJames Smart }
32935bbecdbcSJames Smart 
32945bbecdbcSJames Smart static void
32954cff280aSJames Smart __nvme_fc_terminate_io(struct nvme_fc_ctrl *ctrl)
32964cff280aSJames Smart {
3297c869e494SJames Smart 	/*
329852793d62SJames Smart 	 * if state is CONNECTING - the error occurred as part of a
329952793d62SJames Smart 	 * reconnect attempt. Abort any ios on the association and
330052793d62SJames Smart 	 * let the create_association error paths resolve things.
3301c869e494SJames Smart 	 */
330252793d62SJames Smart 	if (ctrl->ctrl.state == NVME_CTRL_CONNECTING) {
330352793d62SJames Smart 		__nvme_fc_abort_outstanding_ios(ctrl, true);
330452793d62SJames Smart 		return;
330552793d62SJames Smart 	}
330652793d62SJames Smart 
330752793d62SJames Smart 	/*
330852793d62SJames Smart 	 * For any other state, kill the association. As this routine
330952793d62SJames Smart 	 * is a common io abort routine for resetting and such, after
331052793d62SJames Smart 	 * the association is terminated, ensure that the state is set
331152793d62SJames Smart 	 * to CONNECTING.
331252793d62SJames Smart 	 */
331352793d62SJames Smart 
33144cff280aSJames Smart 	nvme_stop_keep_alive(&ctrl->ctrl);
33154cff280aSJames Smart 
33164cff280aSJames Smart 	/* will block will waiting for io to terminate */
33174cff280aSJames Smart 	nvme_fc_delete_association(ctrl);
33184cff280aSJames Smart 
33194cff280aSJames Smart 	if (ctrl->ctrl.state != NVME_CTRL_CONNECTING &&
33204cff280aSJames Smart 	    !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
33214cff280aSJames Smart 		dev_err(ctrl->ctrl.device,
33224cff280aSJames Smart 			"NVME-FC{%d}: error_recovery: Couldn't change state "
33234cff280aSJames Smart 			"to CONNECTING\n", ctrl->cnum);
33244cff280aSJames Smart }
33254cff280aSJames Smart 
33264cff280aSJames Smart static void
332761bff8efSJames Smart nvme_fc_reset_ctrl_work(struct work_struct *work)
332861bff8efSJames Smart {
332961bff8efSJames Smart 	struct nvme_fc_ctrl *ctrl =
3330d86c4d8eSChristoph Hellwig 		container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
333161bff8efSJames Smart 	int ret;
333261bff8efSJames Smart 
33334cff280aSJames Smart 	__nvme_fc_terminate_io(ctrl);
33344cff280aSJames Smart 
3335d09f2b45SSagi Grimberg 	nvme_stop_ctrl(&ctrl->ctrl);
333644c6ec77SJames Smart 
33372b632970SJames Smart 	if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE)
333861bff8efSJames Smart 		ret = nvme_fc_create_association(ctrl);
33392b632970SJames Smart 	else
33402b632970SJames Smart 		ret = -ENOTCONN;
33412b632970SJames Smart 
33425bbecdbcSJames Smart 	if (ret)
33435bbecdbcSJames Smart 		nvme_fc_reconnect_or_delete(ctrl, ret);
33445bbecdbcSJames Smart 	else
334561bff8efSJames Smart 		dev_info(ctrl->ctrl.device,
334696e24801SJames Smart 			"NVME-FC{%d}: controller reset complete\n",
334796e24801SJames Smart 			ctrl->cnum);
334861bff8efSJames Smart }
334961bff8efSJames Smart 
33504cff280aSJames Smart static void
33514cff280aSJames Smart nvme_fc_connect_err_work(struct work_struct *work)
33524cff280aSJames Smart {
33534cff280aSJames Smart 	struct nvme_fc_ctrl *ctrl =
33544cff280aSJames Smart 			container_of(work, struct nvme_fc_ctrl, err_work);
33554cff280aSJames Smart 
33564cff280aSJames Smart 	__nvme_fc_terminate_io(ctrl);
33574cff280aSJames Smart 
33584cff280aSJames Smart 	atomic_set(&ctrl->err_work_active, 0);
33594cff280aSJames Smart 
33604cff280aSJames Smart 	/*
33614cff280aSJames Smart 	 * Rescheduling the connection after recovering
33624cff280aSJames Smart 	 * from the io error is left to the reconnect work
33634cff280aSJames Smart 	 * item, which is what should have stalled waiting on
33644cff280aSJames Smart 	 * the io that had the error that scheduled this work.
33654cff280aSJames Smart 	 */
33664cff280aSJames Smart }
33674cff280aSJames Smart 
336861bff8efSJames Smart static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
336961bff8efSJames Smart 	.name			= "fc",
337061bff8efSJames Smart 	.module			= THIS_MODULE,
3371d3d5b87dSChristoph Hellwig 	.flags			= NVME_F_FABRICS,
337261bff8efSJames Smart 	.reg_read32		= nvmf_reg_read32,
337361bff8efSJames Smart 	.reg_read64		= nvmf_reg_read64,
337461bff8efSJames Smart 	.reg_write32		= nvmf_reg_write32,
337561bff8efSJames Smart 	.free_ctrl		= nvme_fc_nvme_ctrl_freed,
337661bff8efSJames Smart 	.submit_async_event	= nvme_fc_submit_async_event,
3377c5017e85SChristoph Hellwig 	.delete_ctrl		= nvme_fc_delete_ctrl,
337861bff8efSJames Smart 	.get_address		= nvmf_get_address,
337961bff8efSJames Smart };
338061bff8efSJames Smart 
338161bff8efSJames Smart static void
338261bff8efSJames Smart nvme_fc_connect_ctrl_work(struct work_struct *work)
338361bff8efSJames Smart {
338461bff8efSJames Smart 	int ret;
338561bff8efSJames Smart 
338661bff8efSJames Smart 	struct nvme_fc_ctrl *ctrl =
338761bff8efSJames Smart 			container_of(to_delayed_work(work),
338861bff8efSJames Smart 				struct nvme_fc_ctrl, connect_work);
338961bff8efSJames Smart 
339061bff8efSJames Smart 	ret = nvme_fc_create_association(ctrl);
33915bbecdbcSJames Smart 	if (ret)
33925bbecdbcSJames Smart 		nvme_fc_reconnect_or_delete(ctrl, ret);
33935bbecdbcSJames Smart 	else
339461bff8efSJames Smart 		dev_info(ctrl->ctrl.device,
33954c984154SJames Smart 			"NVME-FC{%d}: controller connect complete\n",
339661bff8efSJames Smart 			ctrl->cnum);
339761bff8efSJames Smart }
339861bff8efSJames Smart 
339961bff8efSJames Smart 
340061bff8efSJames Smart static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
340161bff8efSJames Smart 	.queue_rq	= nvme_fc_queue_rq,
340261bff8efSJames Smart 	.complete	= nvme_fc_complete_rq,
340376f983cbSChristoph Hellwig 	.init_request	= nvme_fc_init_request,
340461bff8efSJames Smart 	.exit_request	= nvme_fc_exit_request,
340561bff8efSJames Smart 	.init_hctx	= nvme_fc_init_admin_hctx,
340661bff8efSJames Smart 	.timeout	= nvme_fc_timeout,
340761bff8efSJames Smart };
340861bff8efSJames Smart 
3409e399441dSJames Smart 
341056d5f4f1SJames Smart /*
341156d5f4f1SJames Smart  * Fails a controller request if it matches an existing controller
341256d5f4f1SJames Smart  * (association) with the same tuple:
341356d5f4f1SJames Smart  * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
341456d5f4f1SJames Smart  *
341556d5f4f1SJames Smart  * The ports don't need to be compared as they are intrinsically
341656d5f4f1SJames Smart  * already matched by the port pointers supplied.
341756d5f4f1SJames Smart  */
341856d5f4f1SJames Smart static bool
341956d5f4f1SJames Smart nvme_fc_existing_controller(struct nvme_fc_rport *rport,
342056d5f4f1SJames Smart 		struct nvmf_ctrl_options *opts)
342156d5f4f1SJames Smart {
342256d5f4f1SJames Smart 	struct nvme_fc_ctrl *ctrl;
342356d5f4f1SJames Smart 	unsigned long flags;
342456d5f4f1SJames Smart 	bool found = false;
342556d5f4f1SJames Smart 
342656d5f4f1SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
342756d5f4f1SJames Smart 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
342856d5f4f1SJames Smart 		found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
342956d5f4f1SJames Smart 		if (found)
343056d5f4f1SJames Smart 			break;
343156d5f4f1SJames Smart 	}
343256d5f4f1SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
343356d5f4f1SJames Smart 
343456d5f4f1SJames Smart 	return found;
343556d5f4f1SJames Smart }
343656d5f4f1SJames Smart 
3437e399441dSJames Smart static struct nvme_ctrl *
343861bff8efSJames Smart nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3439e399441dSJames Smart 	struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3440e399441dSJames Smart {
3441e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl;
3442e399441dSJames Smart 	unsigned long flags;
3443f673714aSJames Smart 	int ret, idx, ctrl_loss_tmo;
3444e399441dSJames Smart 
344585e6a6adSJames Smart 	if (!(rport->remoteport.port_role &
344685e6a6adSJames Smart 	    (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
344785e6a6adSJames Smart 		ret = -EBADR;
344885e6a6adSJames Smart 		goto out_fail;
344985e6a6adSJames Smart 	}
345085e6a6adSJames Smart 
345156d5f4f1SJames Smart 	if (!opts->duplicate_connect &&
345256d5f4f1SJames Smart 	    nvme_fc_existing_controller(rport, opts)) {
345356d5f4f1SJames Smart 		ret = -EALREADY;
345456d5f4f1SJames Smart 		goto out_fail;
345556d5f4f1SJames Smart 	}
345656d5f4f1SJames Smart 
3457e399441dSJames Smart 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3458e399441dSJames Smart 	if (!ctrl) {
3459e399441dSJames Smart 		ret = -ENOMEM;
3460e399441dSJames Smart 		goto out_fail;
3461e399441dSJames Smart 	}
3462e399441dSJames Smart 
3463e399441dSJames Smart 	idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
3464e399441dSJames Smart 	if (idx < 0) {
3465e399441dSJames Smart 		ret = -ENOSPC;
34668c5c6605SJames Smart 		goto out_free_ctrl;
3467e399441dSJames Smart 	}
3468e399441dSJames Smart 
3469f673714aSJames Smart 	/*
3470f673714aSJames Smart 	 * if ctrl_loss_tmo is being enforced and the default reconnect delay
3471f673714aSJames Smart 	 * is being used, change to a shorter reconnect delay for FC.
3472f673714aSJames Smart 	 */
3473f673714aSJames Smart 	if (opts->max_reconnects != -1 &&
3474f673714aSJames Smart 	    opts->reconnect_delay == NVMF_DEF_RECONNECT_DELAY &&
3475f673714aSJames Smart 	    opts->reconnect_delay > NVME_FC_DEFAULT_RECONNECT_TMO) {
3476f673714aSJames Smart 		ctrl_loss_tmo = opts->max_reconnects * opts->reconnect_delay;
3477f673714aSJames Smart 		opts->reconnect_delay = NVME_FC_DEFAULT_RECONNECT_TMO;
3478f673714aSJames Smart 		opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
3479f673714aSJames Smart 						opts->reconnect_delay);
3480f673714aSJames Smart 	}
3481f673714aSJames Smart 
3482e399441dSJames Smart 	ctrl->ctrl.opts = opts;
34834c984154SJames Smart 	ctrl->ctrl.nr_reconnects = 0;
348406f3d71eSJames Smart 	if (lport->dev)
3485103e515eSHannes Reinecke 		ctrl->ctrl.numa_node = dev_to_node(lport->dev);
348606f3d71eSJames Smart 	else
348706f3d71eSJames Smart 		ctrl->ctrl.numa_node = NUMA_NO_NODE;
3488e399441dSJames Smart 	INIT_LIST_HEAD(&ctrl->ctrl_list);
3489e399441dSJames Smart 	ctrl->lport = lport;
3490e399441dSJames Smart 	ctrl->rport = rport;
3491e399441dSJames Smart 	ctrl->dev = lport->dev;
3492e399441dSJames Smart 	ctrl->cnum = idx;
34934c984154SJames Smart 	ctrl->ioq_live = false;
34944cff280aSJames Smart 	atomic_set(&ctrl->err_work_active, 0);
34958a82dbf1SJames Smart 	init_waitqueue_head(&ctrl->ioabort_wait);
3496e399441dSJames Smart 
3497e399441dSJames Smart 	get_device(ctrl->dev);
3498e399441dSJames Smart 	kref_init(&ctrl->ref);
3499e399441dSJames Smart 
3500d86c4d8eSChristoph Hellwig 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
350161bff8efSJames Smart 	INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
35024cff280aSJames Smart 	INIT_WORK(&ctrl->err_work, nvme_fc_connect_err_work);
3503e399441dSJames Smart 	spin_lock_init(&ctrl->lock);
3504e399441dSJames Smart 
3505e399441dSJames Smart 	/* io queue count */
3506d858e5f0SSagi Grimberg 	ctrl->ctrl.queue_count = min_t(unsigned int,
3507e399441dSJames Smart 				opts->nr_io_queues,
3508e399441dSJames Smart 				lport->ops->max_hw_queues);
3509d858e5f0SSagi Grimberg 	ctrl->ctrl.queue_count++;	/* +1 for admin queue */
3510e399441dSJames Smart 
3511e399441dSJames Smart 	ctrl->ctrl.sqsize = opts->queue_size - 1;
3512e399441dSJames Smart 	ctrl->ctrl.kato = opts->kato;
35134c984154SJames Smart 	ctrl->ctrl.cntlid = 0xffff;
3514e399441dSJames Smart 
3515e399441dSJames Smart 	ret = -ENOMEM;
3516d858e5f0SSagi Grimberg 	ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3517d858e5f0SSagi Grimberg 				sizeof(struct nvme_fc_queue), GFP_KERNEL);
3518e399441dSJames Smart 	if (!ctrl->queues)
351961bff8efSJames Smart 		goto out_free_ida;
3520e399441dSJames Smart 
35213e493c00SJames Smart 	nvme_fc_init_queue(ctrl, 0);
35223e493c00SJames Smart 
352361bff8efSJames Smart 	memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
352461bff8efSJames Smart 	ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
352538dabe21SKeith Busch 	ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
352661bff8efSJames Smart 	ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
3527103e515eSHannes Reinecke 	ctrl->admin_tag_set.numa_node = ctrl->ctrl.numa_node;
3528d3d0bc78SBart Van Assche 	ctrl->admin_tag_set.cmd_size =
3529d3d0bc78SBart Van Assche 		struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv,
3530d3d0bc78SBart Van Assche 			    ctrl->lport->ops->fcprqst_priv_sz);
353161bff8efSJames Smart 	ctrl->admin_tag_set.driver_data = ctrl;
353261bff8efSJames Smart 	ctrl->admin_tag_set.nr_hw_queues = 1;
353361bff8efSJames Smart 	ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
35345a22e2bfSIsrael Rukshin 	ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
353561bff8efSJames Smart 
353661bff8efSJames Smart 	ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
3537e399441dSJames Smart 	if (ret)
353861bff8efSJames Smart 		goto out_free_queues;
353934b6c231SSagi Grimberg 	ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
3540e399441dSJames Smart 
3541e7832cb4SSagi Grimberg 	ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
3542e7832cb4SSagi Grimberg 	if (IS_ERR(ctrl->ctrl.fabrics_q)) {
3543e7832cb4SSagi Grimberg 		ret = PTR_ERR(ctrl->ctrl.fabrics_q);
3544e7832cb4SSagi Grimberg 		goto out_free_admin_tag_set;
3545e7832cb4SSagi Grimberg 	}
3546e7832cb4SSagi Grimberg 
354761bff8efSJames Smart 	ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
354861bff8efSJames Smart 	if (IS_ERR(ctrl->ctrl.admin_q)) {
354961bff8efSJames Smart 		ret = PTR_ERR(ctrl->ctrl.admin_q);
3550e7832cb4SSagi Grimberg 		goto out_cleanup_fabrics_q;
3551e399441dSJames Smart 	}
3552e399441dSJames Smart 
355361bff8efSJames Smart 	/*
355461bff8efSJames Smart 	 * Would have been nice to init io queues tag set as well.
355561bff8efSJames Smart 	 * However, we require interaction from the controller
355661bff8efSJames Smart 	 * for max io queue count before we can do so.
355761bff8efSJames Smart 	 * Defer this to the connect path.
355861bff8efSJames Smart 	 */
3559e399441dSJames Smart 
356061bff8efSJames Smart 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3561e399441dSJames Smart 	if (ret)
356261bff8efSJames Smart 		goto out_cleanup_admin_q;
3563e399441dSJames Smart 
356461bff8efSJames Smart 	/* at this point, teardown path changes to ref counting on nvme ctrl */
3565e399441dSJames Smart 
3566e399441dSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
3567e399441dSJames Smart 	list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3568e399441dSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
3569e399441dSJames Smart 
35704c984154SJames Smart 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING) ||
35714c984154SJames Smart 	    !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
35724c984154SJames Smart 		dev_err(ctrl->ctrl.device,
35734c984154SJames Smart 			"NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
35744c984154SJames Smart 		goto fail_ctrl;
357517c4dc6eSJames Smart 	}
357617c4dc6eSJames Smart 
35774c984154SJames Smart 	if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
35784c984154SJames Smart 		dev_err(ctrl->ctrl.device,
35794c984154SJames Smart 			"NVME-FC{%d}: failed to schedule initial connect\n",
35804c984154SJames Smart 			ctrl->cnum);
35814c984154SJames Smart 		goto fail_ctrl;
35824c984154SJames Smart 	}
35834c984154SJames Smart 
35844c984154SJames Smart 	flush_delayed_work(&ctrl->connect_work);
35854c984154SJames Smart 
35864c984154SJames Smart 	dev_info(ctrl->ctrl.device,
35874c984154SJames Smart 		"NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
35884c984154SJames Smart 		ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
35894c984154SJames Smart 
35904c984154SJames Smart 	return &ctrl->ctrl;
35914c984154SJames Smart 
35924c984154SJames Smart fail_ctrl:
3593cf25809bSJames Smart 	nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3594cf25809bSJames Smart 	cancel_work_sync(&ctrl->ctrl.reset_work);
35954cff280aSJames Smart 	cancel_work_sync(&ctrl->err_work);
3596cf25809bSJames Smart 	cancel_delayed_work_sync(&ctrl->connect_work);
3597cf25809bSJames Smart 
3598de41447aSEwan D. Milne 	ctrl->ctrl.opts = NULL;
359917c4dc6eSJames Smart 
360061bff8efSJames Smart 	/* initiate nvme ctrl ref counting teardown */
360161bff8efSJames Smart 	nvme_uninit_ctrl(&ctrl->ctrl);
360261bff8efSJames Smart 
36030b5a7669SJames Smart 	/* Remove core ctrl ref. */
36040b5a7669SJames Smart 	nvme_put_ctrl(&ctrl->ctrl);
36050b5a7669SJames Smart 
360661bff8efSJames Smart 	/* as we're past the point where we transition to the ref
360761bff8efSJames Smart 	 * counting teardown path, if we return a bad pointer here,
360861bff8efSJames Smart 	 * the calling routine, thinking it's prior to the
360961bff8efSJames Smart 	 * transition, will do an rport put. Since the teardown
361061bff8efSJames Smart 	 * path also does a rport put, we do an extra get here to
361161bff8efSJames Smart 	 * so proper order/teardown happens.
361261bff8efSJames Smart 	 */
361361bff8efSJames Smart 	nvme_fc_rport_get(rport);
361461bff8efSJames Smart 
36154c984154SJames Smart 	return ERR_PTR(-EIO);
3616e399441dSJames Smart 
361761bff8efSJames Smart out_cleanup_admin_q:
361861bff8efSJames Smart 	blk_cleanup_queue(ctrl->ctrl.admin_q);
3619e7832cb4SSagi Grimberg out_cleanup_fabrics_q:
3620e7832cb4SSagi Grimberg 	blk_cleanup_queue(ctrl->ctrl.fabrics_q);
362161bff8efSJames Smart out_free_admin_tag_set:
362261bff8efSJames Smart 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
362361bff8efSJames Smart out_free_queues:
362461bff8efSJames Smart 	kfree(ctrl->queues);
3625e399441dSJames Smart out_free_ida:
362661bff8efSJames Smart 	put_device(ctrl->dev);
3627e399441dSJames Smart 	ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
3628e399441dSJames Smart out_free_ctrl:
3629e399441dSJames Smart 	kfree(ctrl);
3630e399441dSJames Smart out_fail:
3631e399441dSJames Smart 	/* exit via here doesn't follow ctlr ref points */
3632e399441dSJames Smart 	return ERR_PTR(ret);
3633e399441dSJames Smart }
3634e399441dSJames Smart 
3635e399441dSJames Smart 
3636e399441dSJames Smart struct nvmet_fc_traddr {
3637e399441dSJames Smart 	u64	nn;
3638e399441dSJames Smart 	u64	pn;
3639e399441dSJames Smart };
3640e399441dSJames Smart 
3641e399441dSJames Smart static int
36429c5358e1SJames Smart __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3643e399441dSJames Smart {
3644e399441dSJames Smart 	u64 token64;
3645e399441dSJames Smart 
36469c5358e1SJames Smart 	if (match_u64(sstr, &token64))
36479c5358e1SJames Smart 		return -EINVAL;
36489c5358e1SJames Smart 	*val = token64;
3649e399441dSJames Smart 
36509c5358e1SJames Smart 	return 0;
3651e399441dSJames Smart }
3652e399441dSJames Smart 
36539c5358e1SJames Smart /*
36549c5358e1SJames Smart  * This routine validates and extracts the WWN's from the TRADDR string.
36559c5358e1SJames Smart  * As kernel parsers need the 0x to determine number base, universally
36569c5358e1SJames Smart  * build string to parse with 0x prefix before parsing name strings.
36579c5358e1SJames Smart  */
36589c5358e1SJames Smart static int
36599c5358e1SJames Smart nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
36609c5358e1SJames Smart {
36619c5358e1SJames Smart 	char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
36629c5358e1SJames Smart 	substring_t wwn = { name, &name[sizeof(name)-1] };
36639c5358e1SJames Smart 	int nnoffset, pnoffset;
36649c5358e1SJames Smart 
3665d4e4230cSMilan P. Gandhi 	/* validate if string is one of the 2 allowed formats */
36669c5358e1SJames Smart 	if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
36679c5358e1SJames Smart 			!strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
36689c5358e1SJames Smart 			!strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
36699c5358e1SJames Smart 				"pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
36709c5358e1SJames Smart 		nnoffset = NVME_FC_TRADDR_OXNNLEN;
36719c5358e1SJames Smart 		pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
36729c5358e1SJames Smart 						NVME_FC_TRADDR_OXNNLEN;
36739c5358e1SJames Smart 	} else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
36749c5358e1SJames Smart 			!strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
36759c5358e1SJames Smart 			!strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
36769c5358e1SJames Smart 				"pn-", NVME_FC_TRADDR_NNLEN))) {
36779c5358e1SJames Smart 		nnoffset = NVME_FC_TRADDR_NNLEN;
36789c5358e1SJames Smart 		pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
36799c5358e1SJames Smart 	} else
36809c5358e1SJames Smart 		goto out_einval;
36819c5358e1SJames Smart 
36829c5358e1SJames Smart 	name[0] = '0';
36839c5358e1SJames Smart 	name[1] = 'x';
36849c5358e1SJames Smart 	name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
36859c5358e1SJames Smart 
36869c5358e1SJames Smart 	memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
36879c5358e1SJames Smart 	if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
36889c5358e1SJames Smart 		goto out_einval;
36899c5358e1SJames Smart 
36909c5358e1SJames Smart 	memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
36919c5358e1SJames Smart 	if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
36929c5358e1SJames Smart 		goto out_einval;
36939c5358e1SJames Smart 
36949c5358e1SJames Smart 	return 0;
36959c5358e1SJames Smart 
36969c5358e1SJames Smart out_einval:
36979c5358e1SJames Smart 	pr_warn("%s: bad traddr string\n", __func__);
36989c5358e1SJames Smart 	return -EINVAL;
3699e399441dSJames Smart }
3700e399441dSJames Smart 
3701e399441dSJames Smart static struct nvme_ctrl *
3702e399441dSJames Smart nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3703e399441dSJames Smart {
3704e399441dSJames Smart 	struct nvme_fc_lport *lport;
3705e399441dSJames Smart 	struct nvme_fc_rport *rport;
370661bff8efSJames Smart 	struct nvme_ctrl *ctrl;
3707e399441dSJames Smart 	struct nvmet_fc_traddr laddr = { 0L, 0L };
3708e399441dSJames Smart 	struct nvmet_fc_traddr raddr = { 0L, 0L };
3709e399441dSJames Smart 	unsigned long flags;
3710e399441dSJames Smart 	int ret;
3711e399441dSJames Smart 
37129c5358e1SJames Smart 	ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3713e399441dSJames Smart 	if (ret || !raddr.nn || !raddr.pn)
3714e399441dSJames Smart 		return ERR_PTR(-EINVAL);
3715e399441dSJames Smart 
37169c5358e1SJames Smart 	ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3717e399441dSJames Smart 	if (ret || !laddr.nn || !laddr.pn)
3718e399441dSJames Smart 		return ERR_PTR(-EINVAL);
3719e399441dSJames Smart 
3720e399441dSJames Smart 	/* find the host and remote ports to connect together */
3721e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
3722e399441dSJames Smart 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3723e399441dSJames Smart 		if (lport->localport.node_name != laddr.nn ||
37249e0e8dacSJames Smart 		    lport->localport.port_name != laddr.pn ||
37259e0e8dacSJames Smart 		    lport->localport.port_state != FC_OBJSTATE_ONLINE)
3726e399441dSJames Smart 			continue;
3727e399441dSJames Smart 
3728e399441dSJames Smart 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
3729e399441dSJames Smart 			if (rport->remoteport.node_name != raddr.nn ||
37309e0e8dacSJames Smart 			    rport->remoteport.port_name != raddr.pn ||
37319e0e8dacSJames Smart 			    rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3732e399441dSJames Smart 				continue;
3733e399441dSJames Smart 
3734e399441dSJames Smart 			/* if fail to get reference fall through. Will error */
3735e399441dSJames Smart 			if (!nvme_fc_rport_get(rport))
3736e399441dSJames Smart 				break;
3737e399441dSJames Smart 
3738e399441dSJames Smart 			spin_unlock_irqrestore(&nvme_fc_lock, flags);
3739e399441dSJames Smart 
374061bff8efSJames Smart 			ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
374161bff8efSJames Smart 			if (IS_ERR(ctrl))
374261bff8efSJames Smart 				nvme_fc_rport_put(rport);
374361bff8efSJames Smart 			return ctrl;
3744e399441dSJames Smart 		}
3745e399441dSJames Smart 	}
3746e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
3747e399441dSJames Smart 
37484fb135adSJohannes Thumshirn 	pr_warn("%s: %s - %s combination not found\n",
37494fb135adSJohannes Thumshirn 		__func__, opts->traddr, opts->host_traddr);
3750e399441dSJames Smart 	return ERR_PTR(-ENOENT);
3751e399441dSJames Smart }
3752e399441dSJames Smart 
3753e399441dSJames Smart 
3754e399441dSJames Smart static struct nvmf_transport_ops nvme_fc_transport = {
3755e399441dSJames Smart 	.name		= "fc",
37560de5cd36SRoy Shterman 	.module		= THIS_MODULE,
3757e399441dSJames Smart 	.required_opts	= NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
37585bbecdbcSJames Smart 	.allowed_opts	= NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3759e399441dSJames Smart 	.create_ctrl	= nvme_fc_create_ctrl,
3760e399441dSJames Smart };
3761e399441dSJames Smart 
376297faec53SJames Smart /* Arbitrary successive failures max. With lots of subsystems could be high */
376397faec53SJames Smart #define DISCOVERY_MAX_FAIL	20
376497faec53SJames Smart 
376597faec53SJames Smart static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
376697faec53SJames Smart 		struct device_attribute *attr, const char *buf, size_t count)
376797faec53SJames Smart {
376897faec53SJames Smart 	unsigned long flags;
376997faec53SJames Smart 	LIST_HEAD(local_disc_list);
377097faec53SJames Smart 	struct nvme_fc_lport *lport;
377197faec53SJames Smart 	struct nvme_fc_rport *rport;
377297faec53SJames Smart 	int failcnt = 0;
377397faec53SJames Smart 
377497faec53SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
377597faec53SJames Smart restart:
377697faec53SJames Smart 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
377797faec53SJames Smart 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
377897faec53SJames Smart 			if (!nvme_fc_lport_get(lport))
377997faec53SJames Smart 				continue;
378097faec53SJames Smart 			if (!nvme_fc_rport_get(rport)) {
378197faec53SJames Smart 				/*
378297faec53SJames Smart 				 * This is a temporary condition. Upon restart
378397faec53SJames Smart 				 * this rport will be gone from the list.
378497faec53SJames Smart 				 *
378597faec53SJames Smart 				 * Revert the lport put and retry.  Anything
378697faec53SJames Smart 				 * added to the list already will be skipped (as
378797faec53SJames Smart 				 * they are no longer list_empty).  Loops should
378897faec53SJames Smart 				 * resume at rports that were not yet seen.
378997faec53SJames Smart 				 */
379097faec53SJames Smart 				nvme_fc_lport_put(lport);
379197faec53SJames Smart 
379297faec53SJames Smart 				if (failcnt++ < DISCOVERY_MAX_FAIL)
379397faec53SJames Smart 					goto restart;
379497faec53SJames Smart 
379597faec53SJames Smart 				pr_err("nvme_discovery: too many reference "
379697faec53SJames Smart 				       "failures\n");
379797faec53SJames Smart 				goto process_local_list;
379897faec53SJames Smart 			}
379997faec53SJames Smart 			if (list_empty(&rport->disc_list))
380097faec53SJames Smart 				list_add_tail(&rport->disc_list,
380197faec53SJames Smart 					      &local_disc_list);
380297faec53SJames Smart 		}
380397faec53SJames Smart 	}
380497faec53SJames Smart 
380597faec53SJames Smart process_local_list:
380697faec53SJames Smart 	while (!list_empty(&local_disc_list)) {
380797faec53SJames Smart 		rport = list_first_entry(&local_disc_list,
380897faec53SJames Smart 					 struct nvme_fc_rport, disc_list);
380997faec53SJames Smart 		list_del_init(&rport->disc_list);
381097faec53SJames Smart 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
381197faec53SJames Smart 
381297faec53SJames Smart 		lport = rport->lport;
381397faec53SJames Smart 		/* signal discovery. Won't hurt if it repeats */
381497faec53SJames Smart 		nvme_fc_signal_discovery_scan(lport, rport);
381597faec53SJames Smart 		nvme_fc_rport_put(rport);
381697faec53SJames Smart 		nvme_fc_lport_put(lport);
381797faec53SJames Smart 
381897faec53SJames Smart 		spin_lock_irqsave(&nvme_fc_lock, flags);
381997faec53SJames Smart 	}
382097faec53SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
382197faec53SJames Smart 
382297faec53SJames Smart 	return count;
382397faec53SJames Smart }
382497faec53SJames Smart static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
382597faec53SJames Smart 
382697faec53SJames Smart static struct attribute *nvme_fc_attrs[] = {
382797faec53SJames Smart 	&dev_attr_nvme_discovery.attr,
382897faec53SJames Smart 	NULL
382997faec53SJames Smart };
383097faec53SJames Smart 
383197faec53SJames Smart static struct attribute_group nvme_fc_attr_group = {
383297faec53SJames Smart 	.attrs = nvme_fc_attrs,
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 	.owner = THIS_MODULE,
384497faec53SJames Smart };
384597faec53SJames Smart 
3846e399441dSJames Smart static int __init nvme_fc_init_module(void)
3847e399441dSJames Smart {
38485f568556SJames Smart 	int ret;
38495f568556SJames Smart 
38508730c1ddSHannes Reinecke 	nvme_fc_wq = alloc_workqueue("nvme_fc_wq", WQ_MEM_RECLAIM, 0);
38518730c1ddSHannes Reinecke 	if (!nvme_fc_wq)
38528730c1ddSHannes Reinecke 		return -ENOMEM;
38538730c1ddSHannes Reinecke 
38545f568556SJames Smart 	/*
38555f568556SJames Smart 	 * NOTE:
38565f568556SJames Smart 	 * It is expected that in the future the kernel will combine
38575f568556SJames Smart 	 * the FC-isms that are currently under scsi and now being
38585f568556SJames Smart 	 * added to by NVME into a new standalone FC class. The SCSI
38595f568556SJames Smart 	 * and NVME protocols and their devices would be under this
38605f568556SJames Smart 	 * new FC class.
38615f568556SJames Smart 	 *
38625f568556SJames Smart 	 * As we need something to post FC-specific udev events to,
38635f568556SJames Smart 	 * specifically for nvme probe events, start by creating the
38645f568556SJames Smart 	 * new device class.  When the new standalone FC class is
38655f568556SJames Smart 	 * put in place, this code will move to a more generic
38665f568556SJames Smart 	 * location for the class.
38675f568556SJames Smart 	 */
386897faec53SJames Smart 	ret = class_register(&fc_class);
386997faec53SJames Smart 	if (ret) {
38705f568556SJames Smart 		pr_err("couldn't register class fc\n");
38718730c1ddSHannes Reinecke 		goto out_destroy_wq;
38725f568556SJames Smart 	}
38735f568556SJames Smart 
38745f568556SJames Smart 	/*
38755f568556SJames Smart 	 * Create a device for the FC-centric udev events
38765f568556SJames Smart 	 */
387797faec53SJames Smart 	fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
38785f568556SJames Smart 				"fc_udev_device");
38795f568556SJames Smart 	if (IS_ERR(fc_udev_device)) {
38805f568556SJames Smart 		pr_err("couldn't create fc_udev device!\n");
38815f568556SJames Smart 		ret = PTR_ERR(fc_udev_device);
38825f568556SJames Smart 		goto out_destroy_class;
38835f568556SJames Smart 	}
38845f568556SJames Smart 
38855f568556SJames Smart 	ret = nvmf_register_transport(&nvme_fc_transport);
38865f568556SJames Smart 	if (ret)
38875f568556SJames Smart 		goto out_destroy_device;
38885f568556SJames Smart 
38895f568556SJames Smart 	return 0;
38905f568556SJames Smart 
38915f568556SJames Smart out_destroy_device:
389297faec53SJames Smart 	device_destroy(&fc_class, MKDEV(0, 0));
38935f568556SJames Smart out_destroy_class:
389497faec53SJames Smart 	class_unregister(&fc_class);
38958730c1ddSHannes Reinecke out_destroy_wq:
38968730c1ddSHannes Reinecke 	destroy_workqueue(nvme_fc_wq);
38978730c1ddSHannes Reinecke 
38985f568556SJames Smart 	return ret;
3899e399441dSJames Smart }
3900e399441dSJames Smart 
39014c73cbdfSJames Smart static void
39024c73cbdfSJames Smart nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
39034c73cbdfSJames Smart {
39044c73cbdfSJames Smart 	struct nvme_fc_ctrl *ctrl;
39054c73cbdfSJames Smart 
39064c73cbdfSJames Smart 	spin_lock(&rport->lock);
39074c73cbdfSJames Smart 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
39084c73cbdfSJames Smart 		dev_warn(ctrl->ctrl.device,
39094c73cbdfSJames Smart 			"NVME-FC{%d}: transport unloading: deleting ctrl\n",
39104c73cbdfSJames Smart 			ctrl->cnum);
39114c73cbdfSJames Smart 		nvme_delete_ctrl(&ctrl->ctrl);
39124c73cbdfSJames Smart 	}
39134c73cbdfSJames Smart 	spin_unlock(&rport->lock);
39144c73cbdfSJames Smart }
39154c73cbdfSJames Smart 
39164c73cbdfSJames Smart static void
39174c73cbdfSJames Smart nvme_fc_cleanup_for_unload(void)
39184c73cbdfSJames Smart {
39194c73cbdfSJames Smart 	struct nvme_fc_lport *lport;
39204c73cbdfSJames Smart 	struct nvme_fc_rport *rport;
39214c73cbdfSJames Smart 
39224c73cbdfSJames Smart 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
39234c73cbdfSJames Smart 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
39244c73cbdfSJames Smart 			nvme_fc_delete_controllers(rport);
39254c73cbdfSJames Smart 		}
39264c73cbdfSJames Smart 	}
39274c73cbdfSJames Smart }
39284c73cbdfSJames Smart 
3929e399441dSJames Smart static void __exit nvme_fc_exit_module(void)
3930e399441dSJames Smart {
39314c73cbdfSJames Smart 	unsigned long flags;
39324c73cbdfSJames Smart 	bool need_cleanup = false;
39334c73cbdfSJames Smart 
39344c73cbdfSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
39354c73cbdfSJames Smart 	nvme_fc_waiting_to_unload = true;
39364c73cbdfSJames Smart 	if (!list_empty(&nvme_fc_lport_list)) {
39374c73cbdfSJames Smart 		need_cleanup = true;
39384c73cbdfSJames Smart 		nvme_fc_cleanup_for_unload();
39394c73cbdfSJames Smart 	}
39404c73cbdfSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
39414c73cbdfSJames Smart 	if (need_cleanup) {
39424c73cbdfSJames Smart 		pr_info("%s: waiting for ctlr deletes\n", __func__);
39434c73cbdfSJames Smart 		wait_for_completion(&nvme_fc_unload_proceed);
39444c73cbdfSJames Smart 		pr_info("%s: ctrl deletes complete\n", __func__);
39454c73cbdfSJames Smart 	}
3946e399441dSJames Smart 
3947e399441dSJames Smart 	nvmf_unregister_transport(&nvme_fc_transport);
3948e399441dSJames Smart 
3949e399441dSJames Smart 	ida_destroy(&nvme_fc_local_port_cnt);
3950e399441dSJames Smart 	ida_destroy(&nvme_fc_ctrl_cnt);
39515f568556SJames Smart 
395297faec53SJames Smart 	device_destroy(&fc_class, MKDEV(0, 0));
395397faec53SJames Smart 	class_unregister(&fc_class);
39548730c1ddSHannes Reinecke 	destroy_workqueue(nvme_fc_wq);
3955e399441dSJames Smart }
3956e399441dSJames Smart 
3957e399441dSJames Smart module_init(nvme_fc_init_module);
3958e399441dSJames Smart module_exit(nvme_fc_exit_module);
3959e399441dSJames Smart 
3960e399441dSJames Smart MODULE_LICENSE("GPL v2");
3961