xref: /openbmc/linux/drivers/nvme/host/fc.c (revision 23748076)
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 */
29ac7fe82bSJames Smart 
30e399441dSJames Smart struct nvme_fc_queue {
31e399441dSJames Smart 	struct nvme_fc_ctrl	*ctrl;
32e399441dSJames Smart 	struct device		*dev;
33e399441dSJames Smart 	struct blk_mq_hw_ctx	*hctx;
34e399441dSJames Smart 	void			*lldd_handle;
35e399441dSJames Smart 	size_t			cmnd_capsule_len;
36e399441dSJames Smart 	u32			qnum;
37e399441dSJames Smart 	u32			rqcnt;
38e399441dSJames Smart 	u32			seqno;
39e399441dSJames Smart 
40e399441dSJames Smart 	u64			connection_id;
41e399441dSJames Smart 	atomic_t		csn;
42e399441dSJames Smart 
43e399441dSJames Smart 	unsigned long		flags;
44e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
45e399441dSJames Smart 
468d64daf7SJames Smart enum nvme_fcop_flags {
478d64daf7SJames Smart 	FCOP_FLAGS_TERMIO	= (1 << 0),
48c3aedd22SJames Smart 	FCOP_FLAGS_AEN		= (1 << 1),
498d64daf7SJames Smart };
508d64daf7SJames Smart 
51e399441dSJames Smart struct nvmefc_ls_req_op {
52e399441dSJames Smart 	struct nvmefc_ls_req	ls_req;
53e399441dSJames Smart 
54c913a8b0SJames Smart 	struct nvme_fc_rport	*rport;
55e399441dSJames Smart 	struct nvme_fc_queue	*queue;
56e399441dSJames Smart 	struct request		*rq;
578d64daf7SJames Smart 	u32			flags;
58e399441dSJames Smart 
59e399441dSJames Smart 	int			ls_error;
60e399441dSJames Smart 	struct completion	ls_done;
61c913a8b0SJames Smart 	struct list_head	lsreq_list;	/* rport->ls_req_list */
62e399441dSJames Smart 	bool			req_queued;
63e399441dSJames Smart };
64e399441dSJames Smart 
6514fd1e98SJames Smart struct nvmefc_ls_rcv_op {
6614fd1e98SJames Smart 	struct nvme_fc_rport		*rport;
6714fd1e98SJames Smart 	struct nvmefc_ls_rsp		*lsrsp;
6814fd1e98SJames Smart 	union nvmefc_ls_requests	*rqstbuf;
6914fd1e98SJames Smart 	union nvmefc_ls_responses	*rspbuf;
7014fd1e98SJames Smart 	u16				rqstdatalen;
7114fd1e98SJames Smart 	bool				handled;
7214fd1e98SJames Smart 	dma_addr_t			rspdma;
7314fd1e98SJames Smart 	struct list_head		lsrcv_list;	/* rport->ls_rcv_list */
7414fd1e98SJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
7514fd1e98SJames Smart 
76e399441dSJames Smart enum nvme_fcpop_state {
77e399441dSJames Smart 	FCPOP_STATE_UNINIT	= 0,
78e399441dSJames Smart 	FCPOP_STATE_IDLE	= 1,
79e399441dSJames Smart 	FCPOP_STATE_ACTIVE	= 2,
80e399441dSJames Smart 	FCPOP_STATE_ABORTED	= 3,
8178a7ac26SJames Smart 	FCPOP_STATE_COMPLETE	= 4,
82e399441dSJames Smart };
83e399441dSJames Smart 
84e399441dSJames Smart struct nvme_fc_fcp_op {
85e399441dSJames Smart 	struct nvme_request	nreq;		/*
86e399441dSJames Smart 						 * nvme/host/core.c
87e399441dSJames Smart 						 * requires this to be
88e399441dSJames Smart 						 * the 1st element in the
89e399441dSJames Smart 						 * private structure
90e399441dSJames Smart 						 * associated with the
91e399441dSJames Smart 						 * request.
92e399441dSJames Smart 						 */
93e399441dSJames Smart 	struct nvmefc_fcp_req	fcp_req;
94e399441dSJames Smart 
95e399441dSJames Smart 	struct nvme_fc_ctrl	*ctrl;
96e399441dSJames Smart 	struct nvme_fc_queue	*queue;
97e399441dSJames Smart 	struct request		*rq;
98e399441dSJames Smart 
99e399441dSJames Smart 	atomic_t		state;
10078a7ac26SJames Smart 	u32			flags;
101e399441dSJames Smart 	u32			rqno;
102e399441dSJames Smart 	u32			nents;
103e399441dSJames Smart 
104e399441dSJames Smart 	struct nvme_fc_cmd_iu	cmd_iu;
105e399441dSJames Smart 	struct nvme_fc_ersp_iu	rsp_iu;
106e399441dSJames Smart };
107e399441dSJames Smart 
108d3d0bc78SBart Van Assche struct nvme_fcp_op_w_sgl {
109d3d0bc78SBart Van Assche 	struct nvme_fc_fcp_op	op;
110b1ae1a23SIsrael Rukshin 	struct scatterlist	sgl[NVME_INLINE_SG_CNT];
111f1e71d75SGustavo A. R. Silva 	uint8_t			priv[];
112d3d0bc78SBart Van Assche };
113d3d0bc78SBart Van Assche 
114e399441dSJames Smart struct nvme_fc_lport {
115e399441dSJames Smart 	struct nvme_fc_local_port	localport;
116e399441dSJames Smart 
117e399441dSJames Smart 	struct ida			endp_cnt;
118e399441dSJames Smart 	struct list_head		port_list;	/* nvme_fc_port_list */
119e399441dSJames Smart 	struct list_head		endp_list;
120e399441dSJames Smart 	struct device			*dev;	/* physical device for dma */
121e399441dSJames Smart 	struct nvme_fc_port_template	*ops;
122e399441dSJames Smart 	struct kref			ref;
123158bfb88SJames Smart 	atomic_t                        act_rport_cnt;
124e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
125e399441dSJames Smart 
126e399441dSJames Smart struct nvme_fc_rport {
127e399441dSJames Smart 	struct nvme_fc_remote_port	remoteport;
128e399441dSJames Smart 
129e399441dSJames Smart 	struct list_head		endp_list; /* for lport->endp_list */
130e399441dSJames Smart 	struct list_head		ctrl_list;
131c913a8b0SJames Smart 	struct list_head		ls_req_list;
13214fd1e98SJames Smart 	struct list_head		ls_rcv_list;
13397faec53SJames Smart 	struct list_head		disc_list;
134c913a8b0SJames Smart 	struct device			*dev;	/* physical device for dma */
135c913a8b0SJames Smart 	struct nvme_fc_lport		*lport;
136e399441dSJames Smart 	spinlock_t			lock;
137e399441dSJames Smart 	struct kref			ref;
138158bfb88SJames Smart 	atomic_t                        act_ctrl_cnt;
1392b632970SJames Smart 	unsigned long			dev_loss_end;
14014fd1e98SJames Smart 	struct work_struct		lsrcv_work;
141e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
142e399441dSJames Smart 
143eb4ee8f1SJames Smart /* fc_ctrl flags values - specified as bit positions */
144eb4ee8f1SJames Smart #define ASSOC_ACTIVE		0
145eb4ee8f1SJames Smart #define FCCTRL_TERMIO		1
146e399441dSJames Smart 
147e399441dSJames Smart struct nvme_fc_ctrl {
148e399441dSJames Smart 	spinlock_t		lock;
149e399441dSJames Smart 	struct nvme_fc_queue	*queues;
150e399441dSJames Smart 	struct device		*dev;
151e399441dSJames Smart 	struct nvme_fc_lport	*lport;
152e399441dSJames Smart 	struct nvme_fc_rport	*rport;
153e399441dSJames Smart 	u32			cnum;
154e399441dSJames Smart 
1554c984154SJames Smart 	bool			ioq_live;
1564cff280aSJames Smart 	atomic_t		err_work_active;
157e399441dSJames Smart 	u64			association_id;
15814fd1e98SJames Smart 	struct nvmefc_ls_rcv_op	*rcv_disconn;
159e399441dSJames Smart 
160e399441dSJames Smart 	struct list_head	ctrl_list;	/* rport->ctrl_list */
161e399441dSJames Smart 
162e399441dSJames Smart 	struct blk_mq_tag_set	admin_tag_set;
163e399441dSJames Smart 	struct blk_mq_tag_set	tag_set;
164e399441dSJames Smart 
16561bff8efSJames Smart 	struct delayed_work	connect_work;
1664cff280aSJames Smart 	struct work_struct	err_work;
16761bff8efSJames Smart 
168e399441dSJames Smart 	struct kref		ref;
169eb4ee8f1SJames Smart 	unsigned long		flags;
17061bff8efSJames Smart 	u32			iocnt;
17136715cf4SJames Smart 	wait_queue_head_t	ioabort_wait;
172e399441dSJames Smart 
17338dabe21SKeith Busch 	struct nvme_fc_fcp_op	aen_ops[NVME_NR_AEN_COMMANDS];
174e399441dSJames Smart 
175e399441dSJames Smart 	struct nvme_ctrl	ctrl;
176e399441dSJames Smart };
177e399441dSJames Smart 
178e399441dSJames Smart static inline struct nvme_fc_ctrl *
179e399441dSJames Smart to_fc_ctrl(struct nvme_ctrl *ctrl)
180e399441dSJames Smart {
181e399441dSJames Smart 	return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
182e399441dSJames Smart }
183e399441dSJames Smart 
184e399441dSJames Smart static inline struct nvme_fc_lport *
185e399441dSJames Smart localport_to_lport(struct nvme_fc_local_port *portptr)
186e399441dSJames Smart {
187e399441dSJames Smart 	return container_of(portptr, struct nvme_fc_lport, localport);
188e399441dSJames Smart }
189e399441dSJames Smart 
190e399441dSJames Smart static inline struct nvme_fc_rport *
191e399441dSJames Smart remoteport_to_rport(struct nvme_fc_remote_port *portptr)
192e399441dSJames Smart {
193e399441dSJames Smart 	return container_of(portptr, struct nvme_fc_rport, remoteport);
194e399441dSJames Smart }
195e399441dSJames Smart 
196e399441dSJames Smart static inline struct nvmefc_ls_req_op *
197e399441dSJames Smart ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
198e399441dSJames Smart {
199e399441dSJames Smart 	return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
200e399441dSJames Smart }
201e399441dSJames Smart 
202e399441dSJames Smart static inline struct nvme_fc_fcp_op *
203e399441dSJames Smart fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
204e399441dSJames Smart {
205e399441dSJames Smart 	return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
206e399441dSJames Smart }
207e399441dSJames Smart 
208e399441dSJames Smart 
209e399441dSJames Smart 
210e399441dSJames Smart /* *************************** Globals **************************** */
211e399441dSJames Smart 
212e399441dSJames Smart 
213e399441dSJames Smart static DEFINE_SPINLOCK(nvme_fc_lock);
214e399441dSJames Smart 
215e399441dSJames Smart static LIST_HEAD(nvme_fc_lport_list);
216e399441dSJames Smart static DEFINE_IDA(nvme_fc_local_port_cnt);
217e399441dSJames Smart static DEFINE_IDA(nvme_fc_ctrl_cnt);
218e399441dSJames Smart 
2198730c1ddSHannes Reinecke static struct workqueue_struct *nvme_fc_wq;
220e399441dSJames Smart 
2214c73cbdfSJames Smart static bool nvme_fc_waiting_to_unload;
2224c73cbdfSJames Smart static DECLARE_COMPLETION(nvme_fc_unload_proceed);
2234c73cbdfSJames Smart 
2245f568556SJames Smart /*
2255f568556SJames Smart  * These items are short-term. They will eventually be moved into
2265f568556SJames Smart  * a generic FC class. See comments in module init.
2275f568556SJames Smart  */
2285f568556SJames Smart static struct device *fc_udev_device;
2295f568556SJames Smart 
230ff029451SChristoph Hellwig static void nvme_fc_complete_rq(struct request *rq);
231e399441dSJames Smart 
232e399441dSJames Smart /* *********************** FC-NVME Port Management ************************ */
233e399441dSJames Smart 
234e399441dSJames Smart static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
235e399441dSJames Smart 			struct nvme_fc_queue *, unsigned int);
236e399441dSJames Smart 
23714fd1e98SJames Smart static void nvme_fc_handle_ls_rqst_work(struct work_struct *work);
23814fd1e98SJames Smart 
23914fd1e98SJames Smart 
2405533d424SJames Smart static void
2415533d424SJames Smart nvme_fc_free_lport(struct kref *ref)
2425533d424SJames Smart {
2435533d424SJames Smart 	struct nvme_fc_lport *lport =
2445533d424SJames Smart 		container_of(ref, struct nvme_fc_lport, ref);
2455533d424SJames Smart 	unsigned long flags;
2465533d424SJames Smart 
2475533d424SJames Smart 	WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
2485533d424SJames Smart 	WARN_ON(!list_empty(&lport->endp_list));
2495533d424SJames Smart 
2505533d424SJames Smart 	/* remove from transport list */
2515533d424SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
2525533d424SJames Smart 	list_del(&lport->port_list);
2534c73cbdfSJames Smart 	if (nvme_fc_waiting_to_unload && list_empty(&nvme_fc_lport_list))
2544c73cbdfSJames Smart 		complete(&nvme_fc_unload_proceed);
2555533d424SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
2565533d424SJames Smart 
2575533d424SJames Smart 	ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
2585533d424SJames Smart 	ida_destroy(&lport->endp_cnt);
2595533d424SJames Smart 
2605533d424SJames Smart 	put_device(lport->dev);
2615533d424SJames Smart 
2625533d424SJames Smart 	kfree(lport);
2635533d424SJames Smart }
2645533d424SJames Smart 
2655533d424SJames Smart static void
2665533d424SJames Smart nvme_fc_lport_put(struct nvme_fc_lport *lport)
2675533d424SJames Smart {
2685533d424SJames Smart 	kref_put(&lport->ref, nvme_fc_free_lport);
2695533d424SJames Smart }
2705533d424SJames Smart 
2715533d424SJames Smart static int
2725533d424SJames Smart nvme_fc_lport_get(struct nvme_fc_lport *lport)
2735533d424SJames Smart {
2745533d424SJames Smart 	return kref_get_unless_zero(&lport->ref);
2755533d424SJames Smart }
2765533d424SJames Smart 
2775533d424SJames Smart 
2785533d424SJames Smart static struct nvme_fc_lport *
279c5760f30SJames Smart nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo,
280c5760f30SJames Smart 			struct nvme_fc_port_template *ops,
281c5760f30SJames Smart 			struct device *dev)
2825533d424SJames Smart {
2835533d424SJames Smart 	struct nvme_fc_lport *lport;
2845533d424SJames Smart 	unsigned long flags;
2855533d424SJames Smart 
2865533d424SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
2875533d424SJames Smart 
2885533d424SJames Smart 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
2895533d424SJames Smart 		if (lport->localport.node_name != pinfo->node_name ||
2905533d424SJames Smart 		    lport->localport.port_name != pinfo->port_name)
2915533d424SJames Smart 			continue;
2925533d424SJames Smart 
293c5760f30SJames Smart 		if (lport->dev != dev) {
294c5760f30SJames Smart 			lport = ERR_PTR(-EXDEV);
295c5760f30SJames Smart 			goto out_done;
296c5760f30SJames Smart 		}
297c5760f30SJames Smart 
2985533d424SJames Smart 		if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
2995533d424SJames Smart 			lport = ERR_PTR(-EEXIST);
3005533d424SJames Smart 			goto out_done;
3015533d424SJames Smart 		}
3025533d424SJames Smart 
3035533d424SJames Smart 		if (!nvme_fc_lport_get(lport)) {
3045533d424SJames Smart 			/*
3055533d424SJames Smart 			 * fails if ref cnt already 0. If so,
3065533d424SJames Smart 			 * act as if lport already deleted
3075533d424SJames Smart 			 */
3085533d424SJames Smart 			lport = NULL;
3095533d424SJames Smart 			goto out_done;
3105533d424SJames Smart 		}
3115533d424SJames Smart 
3125533d424SJames Smart 		/* resume the lport */
3135533d424SJames Smart 
314c5760f30SJames Smart 		lport->ops = ops;
3155533d424SJames Smart 		lport->localport.port_role = pinfo->port_role;
3165533d424SJames Smart 		lport->localport.port_id = pinfo->port_id;
3175533d424SJames Smart 		lport->localport.port_state = FC_OBJSTATE_ONLINE;
3185533d424SJames Smart 
3195533d424SJames Smart 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
3205533d424SJames Smart 
3215533d424SJames Smart 		return lport;
3225533d424SJames Smart 	}
3235533d424SJames Smart 
3245533d424SJames Smart 	lport = NULL;
3255533d424SJames Smart 
3265533d424SJames Smart out_done:
3275533d424SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
3285533d424SJames Smart 
3295533d424SJames Smart 	return lport;
3305533d424SJames Smart }
331e399441dSJames Smart 
332e399441dSJames Smart /**
333e399441dSJames Smart  * nvme_fc_register_localport - transport entry point called by an
334e399441dSJames Smart  *                              LLDD to register the existence of a NVME
335e399441dSJames Smart  *                              host FC port.
336e399441dSJames Smart  * @pinfo:     pointer to information about the port to be registered
337e399441dSJames Smart  * @template:  LLDD entrypoints and operational parameters for the port
338e399441dSJames Smart  * @dev:       physical hardware device node port corresponds to. Will be
339e399441dSJames Smart  *             used for DMA mappings
34076c910c7SBart Van Assche  * @portptr:   pointer to a local port pointer. Upon success, the routine
341e399441dSJames Smart  *             will allocate a nvme_fc_local_port structure and place its
342e399441dSJames Smart  *             address in the local port pointer. Upon failure, local port
343e399441dSJames Smart  *             pointer will be set to 0.
344e399441dSJames Smart  *
345e399441dSJames Smart  * Returns:
346e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
347e399441dSJames Smart  * (ex: -ENXIO) upon failure.
348e399441dSJames Smart  */
349e399441dSJames Smart int
350e399441dSJames Smart nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
351e399441dSJames Smart 			struct nvme_fc_port_template *template,
352e399441dSJames Smart 			struct device *dev,
353e399441dSJames Smart 			struct nvme_fc_local_port **portptr)
354e399441dSJames Smart {
355e399441dSJames Smart 	struct nvme_fc_lport *newrec;
356e399441dSJames Smart 	unsigned long flags;
357e399441dSJames Smart 	int ret, idx;
358e399441dSJames Smart 
359e399441dSJames Smart 	if (!template->localport_delete || !template->remoteport_delete ||
360e399441dSJames Smart 	    !template->ls_req || !template->fcp_io ||
361e399441dSJames Smart 	    !template->ls_abort || !template->fcp_abort ||
362e399441dSJames Smart 	    !template->max_hw_queues || !template->max_sgl_segments ||
3638c5c6605SJames Smart 	    !template->max_dif_sgl_segments || !template->dma_boundary) {
364e399441dSJames Smart 		ret = -EINVAL;
365e399441dSJames Smart 		goto out_reghost_failed;
366e399441dSJames Smart 	}
367e399441dSJames Smart 
3685533d424SJames Smart 	/*
3695533d424SJames Smart 	 * look to see if there is already a localport that had been
3705533d424SJames Smart 	 * deregistered and in the process of waiting for all the
3715533d424SJames Smart 	 * references to fully be removed.  If the references haven't
3725533d424SJames Smart 	 * expired, we can simply re-enable the localport. Remoteports
3735533d424SJames Smart 	 * and controller reconnections should resume naturally.
3745533d424SJames Smart 	 */
375c5760f30SJames Smart 	newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
3765533d424SJames Smart 
3775533d424SJames Smart 	/* found an lport, but something about its state is bad */
3785533d424SJames Smart 	if (IS_ERR(newrec)) {
3795533d424SJames Smart 		ret = PTR_ERR(newrec);
3805533d424SJames Smart 		goto out_reghost_failed;
3815533d424SJames Smart 
3825533d424SJames Smart 	/* found existing lport, which was resumed */
3835533d424SJames Smart 	} else if (newrec) {
3845533d424SJames Smart 		*portptr = &newrec->localport;
3855533d424SJames Smart 		return 0;
3865533d424SJames Smart 	}
3875533d424SJames Smart 
3885533d424SJames Smart 	/* nothing found - allocate a new localport struct */
3895533d424SJames Smart 
390e399441dSJames Smart 	newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
391e399441dSJames Smart 			 GFP_KERNEL);
392e399441dSJames Smart 	if (!newrec) {
393e399441dSJames Smart 		ret = -ENOMEM;
394e399441dSJames Smart 		goto out_reghost_failed;
395e399441dSJames Smart 	}
396e399441dSJames Smart 
397e399441dSJames Smart 	idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
398e399441dSJames Smart 	if (idx < 0) {
399e399441dSJames Smart 		ret = -ENOSPC;
400e399441dSJames Smart 		goto out_fail_kfree;
401e399441dSJames Smart 	}
402e399441dSJames Smart 
403e399441dSJames Smart 	if (!get_device(dev) && dev) {
404e399441dSJames Smart 		ret = -ENODEV;
405e399441dSJames Smart 		goto out_ida_put;
406e399441dSJames Smart 	}
407e399441dSJames Smart 
408e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->port_list);
409e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->endp_list);
410e399441dSJames Smart 	kref_init(&newrec->ref);
411158bfb88SJames Smart 	atomic_set(&newrec->act_rport_cnt, 0);
412e399441dSJames Smart 	newrec->ops = template;
413e399441dSJames Smart 	newrec->dev = dev;
414e399441dSJames Smart 	ida_init(&newrec->endp_cnt);
415f56bf76fSJames Smart 	if (template->local_priv_sz)
416e399441dSJames Smart 		newrec->localport.private = &newrec[1];
417f56bf76fSJames Smart 	else
418f56bf76fSJames Smart 		newrec->localport.private = NULL;
419e399441dSJames Smart 	newrec->localport.node_name = pinfo->node_name;
420e399441dSJames Smart 	newrec->localport.port_name = pinfo->port_name;
421e399441dSJames Smart 	newrec->localport.port_role = pinfo->port_role;
422e399441dSJames Smart 	newrec->localport.port_id = pinfo->port_id;
423e399441dSJames Smart 	newrec->localport.port_state = FC_OBJSTATE_ONLINE;
424e399441dSJames Smart 	newrec->localport.port_num = idx;
425e399441dSJames Smart 
426e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
427e399441dSJames Smart 	list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
428e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
429e399441dSJames Smart 
430e399441dSJames Smart 	if (dev)
431e399441dSJames Smart 		dma_set_seg_boundary(dev, template->dma_boundary);
432e399441dSJames Smart 
433e399441dSJames Smart 	*portptr = &newrec->localport;
434e399441dSJames Smart 	return 0;
435e399441dSJames Smart 
436e399441dSJames Smart out_ida_put:
437e399441dSJames Smart 	ida_simple_remove(&nvme_fc_local_port_cnt, idx);
438e399441dSJames Smart out_fail_kfree:
439e399441dSJames Smart 	kfree(newrec);
440e399441dSJames Smart out_reghost_failed:
441e399441dSJames Smart 	*portptr = NULL;
442e399441dSJames Smart 
443e399441dSJames Smart 	return ret;
444e399441dSJames Smart }
445e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
446e399441dSJames Smart 
447e399441dSJames Smart /**
448e399441dSJames Smart  * nvme_fc_unregister_localport - transport entry point called by an
449e399441dSJames Smart  *                              LLDD to deregister/remove a previously
450e399441dSJames Smart  *                              registered a NVME host FC port.
45176c910c7SBart Van Assche  * @portptr: pointer to the (registered) local port that is to be deregistered.
452e399441dSJames Smart  *
453e399441dSJames Smart  * Returns:
454e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
455e399441dSJames Smart  * (ex: -ENXIO) upon failure.
456e399441dSJames Smart  */
457e399441dSJames Smart int
458e399441dSJames Smart nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
459e399441dSJames Smart {
460e399441dSJames Smart 	struct nvme_fc_lport *lport = localport_to_lport(portptr);
461e399441dSJames Smart 	unsigned long flags;
462e399441dSJames Smart 
463e399441dSJames Smart 	if (!portptr)
464e399441dSJames Smart 		return -EINVAL;
465e399441dSJames Smart 
466e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
467e399441dSJames Smart 
468e399441dSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
469e399441dSJames Smart 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
470e399441dSJames Smart 		return -EINVAL;
471e399441dSJames Smart 	}
472e399441dSJames Smart 	portptr->port_state = FC_OBJSTATE_DELETED;
473e399441dSJames Smart 
474e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
475e399441dSJames Smart 
476158bfb88SJames Smart 	if (atomic_read(&lport->act_rport_cnt) == 0)
477158bfb88SJames Smart 		lport->ops->localport_delete(&lport->localport);
478158bfb88SJames Smart 
479e399441dSJames Smart 	nvme_fc_lport_put(lport);
480e399441dSJames Smart 
481e399441dSJames Smart 	return 0;
482e399441dSJames Smart }
483e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
484e399441dSJames Smart 
485eaefd5abSJames Smart /*
486eaefd5abSJames Smart  * TRADDR strings, per FC-NVME are fixed format:
487eaefd5abSJames Smart  *   "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
488eaefd5abSJames Smart  * udev event will only differ by prefix of what field is
489eaefd5abSJames Smart  * being specified:
490eaefd5abSJames Smart  *    "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
491eaefd5abSJames Smart  *  19 + 43 + null_fudge = 64 characters
492eaefd5abSJames Smart  */
493eaefd5abSJames Smart #define FCNVME_TRADDR_LENGTH		64
494eaefd5abSJames Smart 
495eaefd5abSJames Smart static void
496eaefd5abSJames Smart nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
497eaefd5abSJames Smart 		struct nvme_fc_rport *rport)
498eaefd5abSJames Smart {
499eaefd5abSJames Smart 	char hostaddr[FCNVME_TRADDR_LENGTH];	/* NVMEFC_HOST_TRADDR=...*/
500eaefd5abSJames Smart 	char tgtaddr[FCNVME_TRADDR_LENGTH];	/* NVMEFC_TRADDR=...*/
501eaefd5abSJames Smart 	char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
502eaefd5abSJames Smart 
503eaefd5abSJames Smart 	if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
504eaefd5abSJames Smart 		return;
505eaefd5abSJames Smart 
506eaefd5abSJames Smart 	snprintf(hostaddr, sizeof(hostaddr),
507eaefd5abSJames Smart 		"NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
508eaefd5abSJames Smart 		lport->localport.node_name, lport->localport.port_name);
509eaefd5abSJames Smart 	snprintf(tgtaddr, sizeof(tgtaddr),
510eaefd5abSJames Smart 		"NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
511eaefd5abSJames Smart 		rport->remoteport.node_name, rport->remoteport.port_name);
512eaefd5abSJames Smart 	kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
513eaefd5abSJames Smart }
514eaefd5abSJames Smart 
515469d0ef0SJames Smart static void
516469d0ef0SJames Smart nvme_fc_free_rport(struct kref *ref)
517469d0ef0SJames Smart {
518469d0ef0SJames Smart 	struct nvme_fc_rport *rport =
519469d0ef0SJames Smart 		container_of(ref, struct nvme_fc_rport, ref);
520469d0ef0SJames Smart 	struct nvme_fc_lport *lport =
521469d0ef0SJames Smart 			localport_to_lport(rport->remoteport.localport);
522469d0ef0SJames Smart 	unsigned long flags;
523469d0ef0SJames Smart 
524469d0ef0SJames Smart 	WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
525469d0ef0SJames Smart 	WARN_ON(!list_empty(&rport->ctrl_list));
526469d0ef0SJames Smart 
527469d0ef0SJames Smart 	/* remove from lport list */
528469d0ef0SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
529469d0ef0SJames Smart 	list_del(&rport->endp_list);
530469d0ef0SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
531469d0ef0SJames Smart 
53297faec53SJames Smart 	WARN_ON(!list_empty(&rport->disc_list));
533469d0ef0SJames Smart 	ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
534469d0ef0SJames Smart 
535469d0ef0SJames Smart 	kfree(rport);
536469d0ef0SJames Smart 
537469d0ef0SJames Smart 	nvme_fc_lport_put(lport);
538469d0ef0SJames Smart }
539469d0ef0SJames Smart 
540469d0ef0SJames Smart static void
541469d0ef0SJames Smart nvme_fc_rport_put(struct nvme_fc_rport *rport)
542469d0ef0SJames Smart {
543469d0ef0SJames Smart 	kref_put(&rport->ref, nvme_fc_free_rport);
544469d0ef0SJames Smart }
545469d0ef0SJames Smart 
546469d0ef0SJames Smart static int
547469d0ef0SJames Smart nvme_fc_rport_get(struct nvme_fc_rport *rport)
548469d0ef0SJames Smart {
549469d0ef0SJames Smart 	return kref_get_unless_zero(&rport->ref);
550469d0ef0SJames Smart }
551469d0ef0SJames Smart 
5522b632970SJames Smart static void
5532b632970SJames Smart nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl)
5542b632970SJames Smart {
5552b632970SJames Smart 	switch (ctrl->ctrl.state) {
5562b632970SJames Smart 	case NVME_CTRL_NEW:
557ad6a0a52SMax Gurtovoy 	case NVME_CTRL_CONNECTING:
5582b632970SJames Smart 		/*
5592b632970SJames Smart 		 * As all reconnects were suppressed, schedule a
5602b632970SJames Smart 		 * connect.
5612b632970SJames Smart 		 */
5622b632970SJames Smart 		dev_info(ctrl->ctrl.device,
5632b632970SJames Smart 			"NVME-FC{%d}: connectivity re-established. "
5642b632970SJames Smart 			"Attempting reconnect\n", ctrl->cnum);
5652b632970SJames Smart 
5662b632970SJames Smart 		queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
5672b632970SJames Smart 		break;
5682b632970SJames Smart 
5692b632970SJames Smart 	case NVME_CTRL_RESETTING:
5702b632970SJames Smart 		/*
5712b632970SJames Smart 		 * Controller is already in the process of terminating the
5722b632970SJames Smart 		 * association. No need to do anything further. The reconnect
5732b632970SJames Smart 		 * step will naturally occur after the reset completes.
5742b632970SJames Smart 		 */
5752b632970SJames Smart 		break;
5762b632970SJames Smart 
5772b632970SJames Smart 	default:
5782b632970SJames Smart 		/* no action to take - let it delete */
5792b632970SJames Smart 		break;
5802b632970SJames Smart 	}
5812b632970SJames Smart }
5822b632970SJames Smart 
5832b632970SJames Smart static struct nvme_fc_rport *
5842b632970SJames Smart nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport,
5852b632970SJames Smart 				struct nvme_fc_port_info *pinfo)
5862b632970SJames Smart {
5872b632970SJames Smart 	struct nvme_fc_rport *rport;
5882b632970SJames Smart 	struct nvme_fc_ctrl *ctrl;
5892b632970SJames Smart 	unsigned long flags;
5902b632970SJames Smart 
5912b632970SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
5922b632970SJames Smart 
5932b632970SJames Smart 	list_for_each_entry(rport, &lport->endp_list, endp_list) {
5942b632970SJames Smart 		if (rport->remoteport.node_name != pinfo->node_name ||
5952b632970SJames Smart 		    rport->remoteport.port_name != pinfo->port_name)
5962b632970SJames Smart 			continue;
5972b632970SJames Smart 
5982b632970SJames Smart 		if (!nvme_fc_rport_get(rport)) {
5992b632970SJames Smart 			rport = ERR_PTR(-ENOLCK);
6002b632970SJames Smart 			goto out_done;
6012b632970SJames Smart 		}
6022b632970SJames Smart 
6032b632970SJames Smart 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
6042b632970SJames Smart 
6052b632970SJames Smart 		spin_lock_irqsave(&rport->lock, flags);
6062b632970SJames Smart 
6072b632970SJames Smart 		/* has it been unregistered */
6082b632970SJames Smart 		if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) {
6092b632970SJames Smart 			/* means lldd called us twice */
6102b632970SJames Smart 			spin_unlock_irqrestore(&rport->lock, flags);
6112b632970SJames Smart 			nvme_fc_rport_put(rport);
6122b632970SJames Smart 			return ERR_PTR(-ESTALE);
6132b632970SJames Smart 		}
6142b632970SJames Smart 
6150cdd5fcaSJames Smart 		rport->remoteport.port_role = pinfo->port_role;
6160cdd5fcaSJames Smart 		rport->remoteport.port_id = pinfo->port_id;
6172b632970SJames Smart 		rport->remoteport.port_state = FC_OBJSTATE_ONLINE;
6182b632970SJames Smart 		rport->dev_loss_end = 0;
6192b632970SJames Smart 
6202b632970SJames Smart 		/*
6212b632970SJames Smart 		 * kick off a reconnect attempt on all associations to the
6222b632970SJames Smart 		 * remote port. A successful reconnects will resume i/o.
6232b632970SJames Smart 		 */
6242b632970SJames Smart 		list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
6252b632970SJames Smart 			nvme_fc_resume_controller(ctrl);
6262b632970SJames Smart 
6272b632970SJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
6282b632970SJames Smart 
6292b632970SJames Smart 		return rport;
6302b632970SJames Smart 	}
6312b632970SJames Smart 
6322b632970SJames Smart 	rport = NULL;
6332b632970SJames Smart 
6342b632970SJames Smart out_done:
6352b632970SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
6362b632970SJames Smart 
6372b632970SJames Smart 	return rport;
6382b632970SJames Smart }
6392b632970SJames Smart 
6402b632970SJames Smart static inline void
6412b632970SJames Smart __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport,
6422b632970SJames Smart 			struct nvme_fc_port_info *pinfo)
6432b632970SJames Smart {
6442b632970SJames Smart 	if (pinfo->dev_loss_tmo)
6452b632970SJames Smart 		rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
6462b632970SJames Smart 	else
6472b632970SJames Smart 		rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
6482b632970SJames Smart }
6492b632970SJames Smart 
650e399441dSJames Smart /**
651e399441dSJames Smart  * nvme_fc_register_remoteport - transport entry point called by an
652e399441dSJames Smart  *                              LLDD to register the existence of a NVME
653e399441dSJames Smart  *                              subsystem FC port on its fabric.
654e399441dSJames Smart  * @localport: pointer to the (registered) local port that the remote
655e399441dSJames Smart  *             subsystem port is connected to.
656e399441dSJames Smart  * @pinfo:     pointer to information about the port to be registered
65776c910c7SBart Van Assche  * @portptr:   pointer to a remote port pointer. Upon success, the routine
658e399441dSJames Smart  *             will allocate a nvme_fc_remote_port structure and place its
659e399441dSJames Smart  *             address in the remote port pointer. Upon failure, remote port
660e399441dSJames Smart  *             pointer will be set to 0.
661e399441dSJames Smart  *
662e399441dSJames Smart  * Returns:
663e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
664e399441dSJames Smart  * (ex: -ENXIO) upon failure.
665e399441dSJames Smart  */
666e399441dSJames Smart int
667e399441dSJames Smart nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
668e399441dSJames Smart 				struct nvme_fc_port_info *pinfo,
669e399441dSJames Smart 				struct nvme_fc_remote_port **portptr)
670e399441dSJames Smart {
671e399441dSJames Smart 	struct nvme_fc_lport *lport = localport_to_lport(localport);
672e399441dSJames Smart 	struct nvme_fc_rport *newrec;
673e399441dSJames Smart 	unsigned long flags;
674e399441dSJames Smart 	int ret, idx;
675e399441dSJames Smart 
6762b632970SJames Smart 	if (!nvme_fc_lport_get(lport)) {
6772b632970SJames Smart 		ret = -ESHUTDOWN;
6782b632970SJames Smart 		goto out_reghost_failed;
6792b632970SJames Smart 	}
6802b632970SJames Smart 
6812b632970SJames Smart 	/*
6822b632970SJames Smart 	 * look to see if there is already a remoteport that is waiting
6832b632970SJames Smart 	 * for a reconnect (within dev_loss_tmo) with the same WWN's.
6842b632970SJames Smart 	 * If so, transition to it and reconnect.
6852b632970SJames Smart 	 */
6862b632970SJames Smart 	newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
6872b632970SJames Smart 
6882b632970SJames Smart 	/* found an rport, but something about its state is bad */
6892b632970SJames Smart 	if (IS_ERR(newrec)) {
6902b632970SJames Smart 		ret = PTR_ERR(newrec);
6912b632970SJames Smart 		goto out_lport_put;
6922b632970SJames Smart 
6932b632970SJames Smart 	/* found existing rport, which was resumed */
6942b632970SJames Smart 	} else if (newrec) {
6952b632970SJames Smart 		nvme_fc_lport_put(lport);
6962b632970SJames Smart 		__nvme_fc_set_dev_loss_tmo(newrec, pinfo);
6972b632970SJames Smart 		nvme_fc_signal_discovery_scan(lport, newrec);
6982b632970SJames Smart 		*portptr = &newrec->remoteport;
6992b632970SJames Smart 		return 0;
7002b632970SJames Smart 	}
7012b632970SJames Smart 
7022b632970SJames Smart 	/* nothing found - allocate a new remoteport struct */
7032b632970SJames Smart 
704e399441dSJames Smart 	newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
705e399441dSJames Smart 			 GFP_KERNEL);
706e399441dSJames Smart 	if (!newrec) {
707e399441dSJames Smart 		ret = -ENOMEM;
7082b632970SJames Smart 		goto out_lport_put;
709e399441dSJames Smart 	}
710e399441dSJames Smart 
711e399441dSJames Smart 	idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
712e399441dSJames Smart 	if (idx < 0) {
713e399441dSJames Smart 		ret = -ENOSPC;
7142b632970SJames Smart 		goto out_kfree_rport;
715e399441dSJames Smart 	}
716e399441dSJames Smart 
717e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->endp_list);
718e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->ctrl_list);
719c913a8b0SJames Smart 	INIT_LIST_HEAD(&newrec->ls_req_list);
72097faec53SJames Smart 	INIT_LIST_HEAD(&newrec->disc_list);
721e399441dSJames Smart 	kref_init(&newrec->ref);
722158bfb88SJames Smart 	atomic_set(&newrec->act_ctrl_cnt, 0);
723e399441dSJames Smart 	spin_lock_init(&newrec->lock);
724e399441dSJames Smart 	newrec->remoteport.localport = &lport->localport;
72514fd1e98SJames Smart 	INIT_LIST_HEAD(&newrec->ls_rcv_list);
726c913a8b0SJames Smart 	newrec->dev = lport->dev;
727c913a8b0SJames Smart 	newrec->lport = lport;
728f56bf76fSJames Smart 	if (lport->ops->remote_priv_sz)
729e399441dSJames Smart 		newrec->remoteport.private = &newrec[1];
730f56bf76fSJames Smart 	else
731f56bf76fSJames Smart 		newrec->remoteport.private = NULL;
732e399441dSJames Smart 	newrec->remoteport.port_role = pinfo->port_role;
733e399441dSJames Smart 	newrec->remoteport.node_name = pinfo->node_name;
734e399441dSJames Smart 	newrec->remoteport.port_name = pinfo->port_name;
735e399441dSJames Smart 	newrec->remoteport.port_id = pinfo->port_id;
736e399441dSJames Smart 	newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
737e399441dSJames Smart 	newrec->remoteport.port_num = idx;
7382b632970SJames Smart 	__nvme_fc_set_dev_loss_tmo(newrec, pinfo);
73914fd1e98SJames Smart 	INIT_WORK(&newrec->lsrcv_work, nvme_fc_handle_ls_rqst_work);
740e399441dSJames Smart 
741e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
742e399441dSJames Smart 	list_add_tail(&newrec->endp_list, &lport->endp_list);
743e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
744e399441dSJames Smart 
745eaefd5abSJames Smart 	nvme_fc_signal_discovery_scan(lport, newrec);
746eaefd5abSJames Smart 
747e399441dSJames Smart 	*portptr = &newrec->remoteport;
748e399441dSJames Smart 	return 0;
749e399441dSJames Smart 
750e399441dSJames Smart out_kfree_rport:
751e399441dSJames Smart 	kfree(newrec);
7522b632970SJames Smart out_lport_put:
7532b632970SJames Smart 	nvme_fc_lport_put(lport);
754e399441dSJames Smart out_reghost_failed:
755e399441dSJames Smart 	*portptr = NULL;
756e399441dSJames Smart 	return ret;
757e399441dSJames Smart }
758e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
759e399441dSJames Smart 
7608d64daf7SJames Smart static int
7618d64daf7SJames Smart nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
7628d64daf7SJames Smart {
7638d64daf7SJames Smart 	struct nvmefc_ls_req_op *lsop;
7648d64daf7SJames Smart 	unsigned long flags;
7658d64daf7SJames Smart 
7668d64daf7SJames Smart restart:
7678d64daf7SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
7688d64daf7SJames Smart 
7698d64daf7SJames Smart 	list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
7708d64daf7SJames Smart 		if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
7718d64daf7SJames Smart 			lsop->flags |= FCOP_FLAGS_TERMIO;
7728d64daf7SJames Smart 			spin_unlock_irqrestore(&rport->lock, flags);
7738d64daf7SJames Smart 			rport->lport->ops->ls_abort(&rport->lport->localport,
7748d64daf7SJames Smart 						&rport->remoteport,
7758d64daf7SJames Smart 						&lsop->ls_req);
7768d64daf7SJames Smart 			goto restart;
7778d64daf7SJames Smart 		}
7788d64daf7SJames Smart 	}
7798d64daf7SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
7808d64daf7SJames Smart 
7818d64daf7SJames Smart 	return 0;
7828d64daf7SJames Smart }
7838d64daf7SJames Smart 
7842b632970SJames Smart static void
7852b632970SJames Smart nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
7862b632970SJames Smart {
7872b632970SJames Smart 	dev_info(ctrl->ctrl.device,
7882b632970SJames Smart 		"NVME-FC{%d}: controller connectivity lost. Awaiting "
7892b632970SJames Smart 		"Reconnect", ctrl->cnum);
7902b632970SJames Smart 
7912b632970SJames Smart 	switch (ctrl->ctrl.state) {
7922b632970SJames Smart 	case NVME_CTRL_NEW:
7932b632970SJames Smart 	case NVME_CTRL_LIVE:
7942b632970SJames Smart 		/*
7952b632970SJames Smart 		 * Schedule a controller reset. The reset will terminate the
7962b632970SJames Smart 		 * association and schedule the reconnect timer.  Reconnects
7972b632970SJames Smart 		 * will be attempted until either the ctlr_loss_tmo
7982b632970SJames Smart 		 * (max_retries * connect_delay) expires or the remoteport's
7992b632970SJames Smart 		 * dev_loss_tmo expires.
8002b632970SJames Smart 		 */
8012b632970SJames Smart 		if (nvme_reset_ctrl(&ctrl->ctrl)) {
8022b632970SJames Smart 			dev_warn(ctrl->ctrl.device,
80377d0612dSMax Gurtovoy 				"NVME-FC{%d}: Couldn't schedule reset.\n",
8042b632970SJames Smart 				ctrl->cnum);
8052b632970SJames Smart 			nvme_delete_ctrl(&ctrl->ctrl);
8062b632970SJames Smart 		}
8072b632970SJames Smart 		break;
8082b632970SJames Smart 
809ad6a0a52SMax Gurtovoy 	case NVME_CTRL_CONNECTING:
8102b632970SJames Smart 		/*
8112b632970SJames Smart 		 * The association has already been terminated and the
8122b632970SJames Smart 		 * controller is attempting reconnects.  No need to do anything
8132b632970SJames Smart 		 * futher.  Reconnects will be attempted until either the
8142b632970SJames Smart 		 * ctlr_loss_tmo (max_retries * connect_delay) expires or the
8152b632970SJames Smart 		 * remoteport's dev_loss_tmo expires.
8162b632970SJames Smart 		 */
8172b632970SJames Smart 		break;
8182b632970SJames Smart 
8192b632970SJames Smart 	case NVME_CTRL_RESETTING:
8202b632970SJames Smart 		/*
8212b632970SJames Smart 		 * Controller is already in the process of terminating the
8222b632970SJames Smart 		 * association.  No need to do anything further. The reconnect
8232b632970SJames Smart 		 * step will kick in naturally after the association is
8242b632970SJames Smart 		 * terminated.
8252b632970SJames Smart 		 */
8262b632970SJames Smart 		break;
8272b632970SJames Smart 
8282b632970SJames Smart 	case NVME_CTRL_DELETING:
829ecca390eSSagi Grimberg 	case NVME_CTRL_DELETING_NOIO:
8302b632970SJames Smart 	default:
8312b632970SJames Smart 		/* no action to take - let it delete */
8322b632970SJames Smart 		break;
8332b632970SJames Smart 	}
8342b632970SJames Smart }
8352b632970SJames Smart 
836e399441dSJames Smart /**
837e399441dSJames Smart  * nvme_fc_unregister_remoteport - transport entry point called by an
838e399441dSJames Smart  *                              LLDD to deregister/remove a previously
839e399441dSJames Smart  *                              registered a NVME subsystem FC port.
84076c910c7SBart Van Assche  * @portptr: pointer to the (registered) remote port that is to be
841e399441dSJames Smart  *           deregistered.
842e399441dSJames Smart  *
843e399441dSJames Smart  * Returns:
844e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
845e399441dSJames Smart  * (ex: -ENXIO) upon failure.
846e399441dSJames Smart  */
847e399441dSJames Smart int
848e399441dSJames Smart nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
849e399441dSJames Smart {
850e399441dSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
851e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl;
852e399441dSJames Smart 	unsigned long flags;
853e399441dSJames Smart 
854e399441dSJames Smart 	if (!portptr)
855e399441dSJames Smart 		return -EINVAL;
856e399441dSJames Smart 
857e399441dSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
858e399441dSJames Smart 
859e399441dSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
860e399441dSJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
861e399441dSJames Smart 		return -EINVAL;
862e399441dSJames Smart 	}
863e399441dSJames Smart 	portptr->port_state = FC_OBJSTATE_DELETED;
864e399441dSJames Smart 
8652b632970SJames Smart 	rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
8662b632970SJames Smart 
8672b632970SJames Smart 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
8682b632970SJames Smart 		/* if dev_loss_tmo==0, dev loss is immediate */
8692b632970SJames Smart 		if (!portptr->dev_loss_tmo) {
8702b632970SJames Smart 			dev_warn(ctrl->ctrl.device,
87177d0612dSMax Gurtovoy 				"NVME-FC{%d}: controller connectivity lost.\n",
8722b632970SJames Smart 				ctrl->cnum);
873c5017e85SChristoph Hellwig 			nvme_delete_ctrl(&ctrl->ctrl);
8742b632970SJames Smart 		} else
8752b632970SJames Smart 			nvme_fc_ctrl_connectivity_loss(ctrl);
8762b632970SJames Smart 	}
877e399441dSJames Smart 
878e399441dSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
879e399441dSJames Smart 
8808d64daf7SJames Smart 	nvme_fc_abort_lsops(rport);
8818d64daf7SJames Smart 
882158bfb88SJames Smart 	if (atomic_read(&rport->act_ctrl_cnt) == 0)
883158bfb88SJames Smart 		rport->lport->ops->remoteport_delete(portptr);
884158bfb88SJames Smart 
8852b632970SJames Smart 	/*
8862b632970SJames Smart 	 * release the reference, which will allow, if all controllers
8872b632970SJames Smart 	 * go away, which should only occur after dev_loss_tmo occurs,
8882b632970SJames Smart 	 * for the rport to be torn down.
8892b632970SJames Smart 	 */
890e399441dSJames Smart 	nvme_fc_rport_put(rport);
8912b632970SJames Smart 
892e399441dSJames Smart 	return 0;
893e399441dSJames Smart }
894e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
895e399441dSJames Smart 
896eaefd5abSJames Smart /**
897eaefd5abSJames Smart  * nvme_fc_rescan_remoteport - transport entry point called by an
898eaefd5abSJames Smart  *                              LLDD to request a nvme device rescan.
899eaefd5abSJames Smart  * @remoteport: pointer to the (registered) remote port that is to be
900eaefd5abSJames Smart  *              rescanned.
901eaefd5abSJames Smart  *
902eaefd5abSJames Smart  * Returns: N/A
903eaefd5abSJames Smart  */
904eaefd5abSJames Smart void
905eaefd5abSJames Smart nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
906eaefd5abSJames Smart {
907eaefd5abSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
908eaefd5abSJames Smart 
909eaefd5abSJames Smart 	nvme_fc_signal_discovery_scan(rport->lport, rport);
910eaefd5abSJames Smart }
911eaefd5abSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
912eaefd5abSJames Smart 
913ac7fe82bSJames Smart int
914ac7fe82bSJames Smart nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
915ac7fe82bSJames Smart 			u32 dev_loss_tmo)
916ac7fe82bSJames Smart {
917ac7fe82bSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
918ac7fe82bSJames Smart 	unsigned long flags;
919ac7fe82bSJames Smart 
920ac7fe82bSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
921ac7fe82bSJames Smart 
922ac7fe82bSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
923ac7fe82bSJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
924ac7fe82bSJames Smart 		return -EINVAL;
925ac7fe82bSJames Smart 	}
926ac7fe82bSJames Smart 
927ac7fe82bSJames Smart 	/* a dev_loss_tmo of 0 (immediate) is allowed to be set */
928ac7fe82bSJames Smart 	rport->remoteport.dev_loss_tmo = dev_loss_tmo;
929ac7fe82bSJames Smart 
930ac7fe82bSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
931ac7fe82bSJames Smart 
932ac7fe82bSJames Smart 	return 0;
933ac7fe82bSJames Smart }
934ac7fe82bSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
935ac7fe82bSJames Smart 
936e399441dSJames Smart 
937e399441dSJames Smart /* *********************** FC-NVME DMA Handling **************************** */
938e399441dSJames Smart 
939e399441dSJames Smart /*
940e399441dSJames Smart  * The fcloop device passes in a NULL device pointer. Real LLD's will
941e399441dSJames Smart  * pass in a valid device pointer. If NULL is passed to the dma mapping
942e399441dSJames Smart  * routines, depending on the platform, it may or may not succeed, and
943e399441dSJames Smart  * may crash.
944e399441dSJames Smart  *
945e399441dSJames Smart  * As such:
946e399441dSJames Smart  * Wrapper all the dma routines and check the dev pointer.
947e399441dSJames Smart  *
948e399441dSJames Smart  * If simple mappings (return just a dma address, we'll noop them,
949e399441dSJames Smart  * returning a dma address of 0.
950e399441dSJames Smart  *
951e399441dSJames Smart  * On more complex mappings (dma_map_sg), a pseudo routine fills
952e399441dSJames Smart  * in the scatter list, setting all dma addresses to 0.
953e399441dSJames Smart  */
954e399441dSJames Smart 
955e399441dSJames Smart static inline dma_addr_t
956e399441dSJames Smart fc_dma_map_single(struct device *dev, void *ptr, size_t size,
957e399441dSJames Smart 		enum dma_data_direction dir)
958e399441dSJames Smart {
959e399441dSJames Smart 	return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
960e399441dSJames Smart }
961e399441dSJames Smart 
962e399441dSJames Smart static inline int
963e399441dSJames Smart fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
964e399441dSJames Smart {
965e399441dSJames Smart 	return dev ? dma_mapping_error(dev, dma_addr) : 0;
966e399441dSJames Smart }
967e399441dSJames Smart 
968e399441dSJames Smart static inline void
969e399441dSJames Smart fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
970e399441dSJames Smart 	enum dma_data_direction dir)
971e399441dSJames Smart {
972e399441dSJames Smart 	if (dev)
973e399441dSJames Smart 		dma_unmap_single(dev, addr, size, dir);
974e399441dSJames Smart }
975e399441dSJames Smart 
976e399441dSJames Smart static inline void
977e399441dSJames Smart fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
978e399441dSJames Smart 		enum dma_data_direction dir)
979e399441dSJames Smart {
980e399441dSJames Smart 	if (dev)
981e399441dSJames Smart 		dma_sync_single_for_cpu(dev, addr, size, dir);
982e399441dSJames Smart }
983e399441dSJames Smart 
984e399441dSJames Smart static inline void
985e399441dSJames Smart fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
986e399441dSJames Smart 		enum dma_data_direction dir)
987e399441dSJames Smart {
988e399441dSJames Smart 	if (dev)
989e399441dSJames Smart 		dma_sync_single_for_device(dev, addr, size, dir);
990e399441dSJames Smart }
991e399441dSJames Smart 
992e399441dSJames Smart /* pseudo dma_map_sg call */
993e399441dSJames Smart static int
994e399441dSJames Smart fc_map_sg(struct scatterlist *sg, int nents)
995e399441dSJames Smart {
996e399441dSJames Smart 	struct scatterlist *s;
997e399441dSJames Smart 	int i;
998e399441dSJames Smart 
999e399441dSJames Smart 	WARN_ON(nents == 0 || sg[0].length == 0);
1000e399441dSJames Smart 
1001e399441dSJames Smart 	for_each_sg(sg, s, nents, i) {
1002e399441dSJames Smart 		s->dma_address = 0L;
1003e399441dSJames Smart #ifdef CONFIG_NEED_SG_DMA_LENGTH
1004e399441dSJames Smart 		s->dma_length = s->length;
1005e399441dSJames Smart #endif
1006e399441dSJames Smart 	}
1007e399441dSJames Smart 	return nents;
1008e399441dSJames Smart }
1009e399441dSJames Smart 
1010e399441dSJames Smart static inline int
1011e399441dSJames Smart fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1012e399441dSJames Smart 		enum dma_data_direction dir)
1013e399441dSJames Smart {
1014e399441dSJames Smart 	return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
1015e399441dSJames Smart }
1016e399441dSJames Smart 
1017e399441dSJames Smart static inline void
1018e399441dSJames Smart fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1019e399441dSJames Smart 		enum dma_data_direction dir)
1020e399441dSJames Smart {
1021e399441dSJames Smart 	if (dev)
1022e399441dSJames Smart 		dma_unmap_sg(dev, sg, nents, dir);
1023e399441dSJames Smart }
1024e399441dSJames Smart 
1025e399441dSJames Smart /* *********************** FC-NVME LS Handling **************************** */
1026e399441dSJames Smart 
1027e399441dSJames Smart static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
1028e399441dSJames Smart static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
1029e399441dSJames Smart 
103014fd1e98SJames Smart static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
1031e399441dSJames Smart 
1032e399441dSJames Smart static void
1033c913a8b0SJames Smart __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
1034e399441dSJames Smart {
1035c913a8b0SJames Smart 	struct nvme_fc_rport *rport = lsop->rport;
1036e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1037e399441dSJames Smart 	unsigned long flags;
1038e399441dSJames Smart 
1039c913a8b0SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
1040e399441dSJames Smart 
1041e399441dSJames Smart 	if (!lsop->req_queued) {
1042c913a8b0SJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
1043e399441dSJames Smart 		return;
1044e399441dSJames Smart 	}
1045e399441dSJames Smart 
1046e399441dSJames Smart 	list_del(&lsop->lsreq_list);
1047e399441dSJames Smart 
1048e399441dSJames Smart 	lsop->req_queued = false;
1049e399441dSJames Smart 
1050c913a8b0SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
1051e399441dSJames Smart 
1052c913a8b0SJames Smart 	fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1053e399441dSJames Smart 				  (lsreq->rqstlen + lsreq->rsplen),
1054e399441dSJames Smart 				  DMA_BIDIRECTIONAL);
1055e399441dSJames Smart 
1056c913a8b0SJames Smart 	nvme_fc_rport_put(rport);
1057e399441dSJames Smart }
1058e399441dSJames Smart 
1059e399441dSJames Smart static int
1060c913a8b0SJames Smart __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
1061e399441dSJames Smart 		struct nvmefc_ls_req_op *lsop,
1062e399441dSJames Smart 		void (*done)(struct nvmefc_ls_req *req, int status))
1063e399441dSJames Smart {
1064e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1065e399441dSJames Smart 	unsigned long flags;
1066c913a8b0SJames Smart 	int ret = 0;
1067e399441dSJames Smart 
1068c913a8b0SJames Smart 	if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1069c913a8b0SJames Smart 		return -ECONNREFUSED;
1070c913a8b0SJames Smart 
1071c913a8b0SJames Smart 	if (!nvme_fc_rport_get(rport))
1072e399441dSJames Smart 		return -ESHUTDOWN;
1073e399441dSJames Smart 
1074e399441dSJames Smart 	lsreq->done = done;
1075c913a8b0SJames Smart 	lsop->rport = rport;
1076e399441dSJames Smart 	lsop->req_queued = false;
1077e399441dSJames Smart 	INIT_LIST_HEAD(&lsop->lsreq_list);
1078e399441dSJames Smart 	init_completion(&lsop->ls_done);
1079e399441dSJames Smart 
1080c913a8b0SJames Smart 	lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
1081e399441dSJames Smart 				  lsreq->rqstlen + lsreq->rsplen,
1082e399441dSJames Smart 				  DMA_BIDIRECTIONAL);
1083c913a8b0SJames Smart 	if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1084c913a8b0SJames Smart 		ret = -EFAULT;
1085c913a8b0SJames Smart 		goto out_putrport;
1086e399441dSJames Smart 	}
1087e399441dSJames Smart 	lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1088e399441dSJames Smart 
1089c913a8b0SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
1090e399441dSJames Smart 
1091c913a8b0SJames Smart 	list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
1092e399441dSJames Smart 
1093e399441dSJames Smart 	lsop->req_queued = true;
1094e399441dSJames Smart 
1095c913a8b0SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
1096e399441dSJames Smart 
1097c913a8b0SJames Smart 	ret = rport->lport->ops->ls_req(&rport->lport->localport,
1098c913a8b0SJames Smart 					&rport->remoteport, lsreq);
1099e399441dSJames Smart 	if (ret)
1100c913a8b0SJames Smart 		goto out_unlink;
1101c913a8b0SJames Smart 
1102c913a8b0SJames Smart 	return 0;
1103c913a8b0SJames Smart 
1104c913a8b0SJames Smart out_unlink:
1105e399441dSJames Smart 	lsop->ls_error = ret;
1106c913a8b0SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
1107c913a8b0SJames Smart 	lsop->req_queued = false;
1108c913a8b0SJames Smart 	list_del(&lsop->lsreq_list);
1109c913a8b0SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
1110c913a8b0SJames Smart 	fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1111c913a8b0SJames Smart 				  (lsreq->rqstlen + lsreq->rsplen),
1112c913a8b0SJames Smart 				  DMA_BIDIRECTIONAL);
1113c913a8b0SJames Smart out_putrport:
1114c913a8b0SJames Smart 	nvme_fc_rport_put(rport);
1115e399441dSJames Smart 
1116e399441dSJames Smart 	return ret;
1117e399441dSJames Smart }
1118e399441dSJames Smart 
1119e399441dSJames Smart static void
1120e399441dSJames Smart nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1121e399441dSJames Smart {
1122e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1123e399441dSJames Smart 
1124e399441dSJames Smart 	lsop->ls_error = status;
1125e399441dSJames Smart 	complete(&lsop->ls_done);
1126e399441dSJames Smart }
1127e399441dSJames Smart 
1128e399441dSJames Smart static int
1129c913a8b0SJames Smart nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
1130e399441dSJames Smart {
1131e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1132e399441dSJames Smart 	struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1133e399441dSJames Smart 	int ret;
1134e399441dSJames Smart 
1135c913a8b0SJames Smart 	ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
1136e399441dSJames Smart 
1137c913a8b0SJames Smart 	if (!ret) {
1138e399441dSJames Smart 		/*
1139e399441dSJames Smart 		 * No timeout/not interruptible as we need the struct
1140e399441dSJames Smart 		 * to exist until the lldd calls us back. Thus mandate
1141e399441dSJames Smart 		 * wait until driver calls back. lldd responsible for
1142e399441dSJames Smart 		 * the timeout action
1143e399441dSJames Smart 		 */
1144e399441dSJames Smart 		wait_for_completion(&lsop->ls_done);
1145e399441dSJames Smart 
1146c913a8b0SJames Smart 		__nvme_fc_finish_ls_req(lsop);
1147e399441dSJames Smart 
1148c913a8b0SJames Smart 		ret = lsop->ls_error;
1149e399441dSJames Smart 	}
1150e399441dSJames Smart 
1151c913a8b0SJames Smart 	if (ret)
1152c913a8b0SJames Smart 		return ret;
1153c913a8b0SJames Smart 
1154e399441dSJames Smart 	/* ACC or RJT payload ? */
1155e399441dSJames Smart 	if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1156e399441dSJames Smart 		return -ENXIO;
1157e399441dSJames Smart 
1158e399441dSJames Smart 	return 0;
1159e399441dSJames Smart }
1160e399441dSJames Smart 
1161c913a8b0SJames Smart static int
1162c913a8b0SJames Smart nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
1163e399441dSJames Smart 		struct nvmefc_ls_req_op *lsop,
1164e399441dSJames Smart 		void (*done)(struct nvmefc_ls_req *req, int status))
1165e399441dSJames Smart {
1166e399441dSJames Smart 	/* don't wait for completion */
1167e399441dSJames Smart 
1168c913a8b0SJames Smart 	return __nvme_fc_send_ls_req(rport, lsop, done);
1169e399441dSJames Smart }
1170e399441dSJames Smart 
1171e399441dSJames Smart static int
1172e399441dSJames Smart nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1173e399441dSJames Smart 	struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1174e399441dSJames Smart {
1175e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
1176e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
1177e399441dSJames Smart 	struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1178e399441dSJames Smart 	struct fcnvme_ls_cr_assoc_acc *assoc_acc;
117914fd1e98SJames Smart 	unsigned long flags;
1180e399441dSJames Smart 	int ret, fcret = 0;
1181e399441dSJames Smart 
1182e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
1183f56bf76fSJames Smart 			 sizeof(*assoc_rqst) + sizeof(*assoc_acc) +
1184f56bf76fSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1185e399441dSJames Smart 	if (!lsop) {
1186f56bf76fSJames Smart 		dev_info(ctrl->ctrl.device,
1187f56bf76fSJames Smart 			"NVME-FC{%d}: send Create Association failed: ENOMEM\n",
1188f56bf76fSJames Smart 			ctrl->cnum);
1189e399441dSJames Smart 		ret = -ENOMEM;
1190e399441dSJames Smart 		goto out_no_memory;
1191e399441dSJames Smart 	}
1192e399441dSJames Smart 
1193f56bf76fSJames Smart 	assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1];
1194e399441dSJames Smart 	assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1195f56bf76fSJames Smart 	lsreq = &lsop->ls_req;
1196f56bf76fSJames Smart 	if (ctrl->lport->ops->lsrqst_priv_sz)
1197f56bf76fSJames Smart 		lsreq->private = &assoc_acc[1];
1198f56bf76fSJames Smart 	else
1199f56bf76fSJames Smart 		lsreq->private = NULL;
1200e399441dSJames Smart 
1201e399441dSJames Smart 	assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1202e399441dSJames Smart 	assoc_rqst->desc_list_len =
1203e399441dSJames Smart 			cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1204e399441dSJames Smart 
1205e399441dSJames Smart 	assoc_rqst->assoc_cmd.desc_tag =
1206e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1207e399441dSJames Smart 	assoc_rqst->assoc_cmd.desc_len =
1208e399441dSJames Smart 			fcnvme_lsdesc_len(
1209e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1210e399441dSJames Smart 
1211e399441dSJames Smart 	assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1212d157e534SJames Smart 	assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
1213e399441dSJames Smart 	/* Linux supports only Dynamic controllers */
1214e399441dSJames Smart 	assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
12158e412263SChristoph Hellwig 	uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1216e399441dSJames Smart 	strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1217e399441dSJames Smart 		min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1218e399441dSJames Smart 	strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1219e399441dSJames Smart 		min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
1220e399441dSJames Smart 
1221e399441dSJames Smart 	lsop->queue = queue;
1222e399441dSJames Smart 	lsreq->rqstaddr = assoc_rqst;
1223e399441dSJames Smart 	lsreq->rqstlen = sizeof(*assoc_rqst);
1224e399441dSJames Smart 	lsreq->rspaddr = assoc_acc;
1225e399441dSJames Smart 	lsreq->rsplen = sizeof(*assoc_acc);
122653b2b2f5SJames Smart 	lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1227e399441dSJames Smart 
1228c913a8b0SJames Smart 	ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1229e399441dSJames Smart 	if (ret)
1230e399441dSJames Smart 		goto out_free_buffer;
1231e399441dSJames Smart 
1232e399441dSJames Smart 	/* process connect LS completion */
1233e399441dSJames Smart 
1234e399441dSJames Smart 	/* validate the ACC response */
1235e399441dSJames Smart 	if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1236e399441dSJames Smart 		fcret = VERR_LSACC;
1237f77fc87cSJames Smart 	else if (assoc_acc->hdr.desc_list_len !=
1238e399441dSJames Smart 			fcnvme_lsdesc_len(
1239e399441dSJames Smart 				sizeof(struct fcnvme_ls_cr_assoc_acc)))
1240e399441dSJames Smart 		fcret = VERR_CR_ASSOC_ACC_LEN;
1241f77fc87cSJames Smart 	else if (assoc_acc->hdr.rqst.desc_tag !=
1242f77fc87cSJames Smart 			cpu_to_be32(FCNVME_LSDESC_RQST))
1243e399441dSJames Smart 		fcret = VERR_LSDESC_RQST;
1244e399441dSJames Smart 	else if (assoc_acc->hdr.rqst.desc_len !=
1245e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1246e399441dSJames Smart 		fcret = VERR_LSDESC_RQST_LEN;
1247e399441dSJames Smart 	else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1248e399441dSJames Smart 		fcret = VERR_CR_ASSOC;
1249e399441dSJames Smart 	else if (assoc_acc->associd.desc_tag !=
1250e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1251e399441dSJames Smart 		fcret = VERR_ASSOC_ID;
1252e399441dSJames Smart 	else if (assoc_acc->associd.desc_len !=
1253e399441dSJames Smart 			fcnvme_lsdesc_len(
1254e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id)))
1255e399441dSJames Smart 		fcret = VERR_ASSOC_ID_LEN;
1256e399441dSJames Smart 	else if (assoc_acc->connectid.desc_tag !=
1257e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1258e399441dSJames Smart 		fcret = VERR_CONN_ID;
1259e399441dSJames Smart 	else if (assoc_acc->connectid.desc_len !=
1260e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1261e399441dSJames Smart 		fcret = VERR_CONN_ID_LEN;
1262e399441dSJames Smart 
1263e399441dSJames Smart 	if (fcret) {
1264e399441dSJames Smart 		ret = -EBADF;
1265e399441dSJames Smart 		dev_err(ctrl->dev,
12667db39484SJames Smart 			"q %d Create Association LS failed: %s\n",
1267e399441dSJames Smart 			queue->qnum, validation_errors[fcret]);
1268e399441dSJames Smart 	} else {
126914fd1e98SJames Smart 		spin_lock_irqsave(&ctrl->lock, flags);
1270e399441dSJames Smart 		ctrl->association_id =
1271e399441dSJames Smart 			be64_to_cpu(assoc_acc->associd.association_id);
1272e399441dSJames Smart 		queue->connection_id =
1273e399441dSJames Smart 			be64_to_cpu(assoc_acc->connectid.connection_id);
1274e399441dSJames Smart 		set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
127514fd1e98SJames Smart 		spin_unlock_irqrestore(&ctrl->lock, flags);
1276e399441dSJames Smart 	}
1277e399441dSJames Smart 
1278e399441dSJames Smart out_free_buffer:
1279e399441dSJames Smart 	kfree(lsop);
1280e399441dSJames Smart out_no_memory:
1281e399441dSJames Smart 	if (ret)
1282e399441dSJames Smart 		dev_err(ctrl->dev,
1283e399441dSJames Smart 			"queue %d connect admin queue failed (%d).\n",
1284e399441dSJames Smart 			queue->qnum, ret);
1285e399441dSJames Smart 	return ret;
1286e399441dSJames Smart }
1287e399441dSJames Smart 
1288e399441dSJames Smart static int
1289e399441dSJames Smart nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1290e399441dSJames Smart 			u16 qsize, u16 ersp_ratio)
1291e399441dSJames Smart {
1292e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
1293e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
1294e399441dSJames Smart 	struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1295e399441dSJames Smart 	struct fcnvme_ls_cr_conn_acc *conn_acc;
1296e399441dSJames Smart 	int ret, fcret = 0;
1297e399441dSJames Smart 
1298e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
1299f56bf76fSJames Smart 			 sizeof(*conn_rqst) + sizeof(*conn_acc) +
1300f56bf76fSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1301e399441dSJames Smart 	if (!lsop) {
1302f56bf76fSJames Smart 		dev_info(ctrl->ctrl.device,
1303f56bf76fSJames Smart 			"NVME-FC{%d}: send Create Connection failed: ENOMEM\n",
1304f56bf76fSJames Smart 			ctrl->cnum);
1305e399441dSJames Smart 		ret = -ENOMEM;
1306e399441dSJames Smart 		goto out_no_memory;
1307e399441dSJames Smart 	}
1308e399441dSJames Smart 
1309f56bf76fSJames Smart 	conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)&lsop[1];
1310e399441dSJames Smart 	conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1311f56bf76fSJames Smart 	lsreq = &lsop->ls_req;
1312f56bf76fSJames Smart 	if (ctrl->lport->ops->lsrqst_priv_sz)
1313f56bf76fSJames Smart 		lsreq->private = (void *)&conn_acc[1];
1314f56bf76fSJames Smart 	else
1315f56bf76fSJames Smart 		lsreq->private = NULL;
1316e399441dSJames Smart 
1317e399441dSJames Smart 	conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1318e399441dSJames Smart 	conn_rqst->desc_list_len = cpu_to_be32(
1319e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id) +
1320e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1321e399441dSJames Smart 
1322e399441dSJames Smart 	conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1323e399441dSJames Smart 	conn_rqst->associd.desc_len =
1324e399441dSJames Smart 			fcnvme_lsdesc_len(
1325e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id));
1326e399441dSJames Smart 	conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1327e399441dSJames Smart 	conn_rqst->connect_cmd.desc_tag =
1328e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1329e399441dSJames Smart 	conn_rqst->connect_cmd.desc_len =
1330e399441dSJames Smart 			fcnvme_lsdesc_len(
1331e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1332e399441dSJames Smart 	conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1333e399441dSJames Smart 	conn_rqst->connect_cmd.qid  = cpu_to_be16(queue->qnum);
1334d157e534SJames Smart 	conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
1335e399441dSJames Smart 
1336e399441dSJames Smart 	lsop->queue = queue;
1337e399441dSJames Smart 	lsreq->rqstaddr = conn_rqst;
1338e399441dSJames Smart 	lsreq->rqstlen = sizeof(*conn_rqst);
1339e399441dSJames Smart 	lsreq->rspaddr = conn_acc;
1340e399441dSJames Smart 	lsreq->rsplen = sizeof(*conn_acc);
134153b2b2f5SJames Smart 	lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1342e399441dSJames Smart 
1343c913a8b0SJames Smart 	ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1344e399441dSJames Smart 	if (ret)
1345e399441dSJames Smart 		goto out_free_buffer;
1346e399441dSJames Smart 
1347e399441dSJames Smart 	/* process connect LS completion */
1348e399441dSJames Smart 
1349e399441dSJames Smart 	/* validate the ACC response */
1350e399441dSJames Smart 	if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1351e399441dSJames Smart 		fcret = VERR_LSACC;
1352f77fc87cSJames Smart 	else if (conn_acc->hdr.desc_list_len !=
1353e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1354e399441dSJames Smart 		fcret = VERR_CR_CONN_ACC_LEN;
1355f77fc87cSJames Smart 	else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1356e399441dSJames Smart 		fcret = VERR_LSDESC_RQST;
1357e399441dSJames Smart 	else if (conn_acc->hdr.rqst.desc_len !=
1358e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1359e399441dSJames Smart 		fcret = VERR_LSDESC_RQST_LEN;
1360e399441dSJames Smart 	else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1361e399441dSJames Smart 		fcret = VERR_CR_CONN;
1362e399441dSJames Smart 	else if (conn_acc->connectid.desc_tag !=
1363e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1364e399441dSJames Smart 		fcret = VERR_CONN_ID;
1365e399441dSJames Smart 	else if (conn_acc->connectid.desc_len !=
1366e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1367e399441dSJames Smart 		fcret = VERR_CONN_ID_LEN;
1368e399441dSJames Smart 
1369e399441dSJames Smart 	if (fcret) {
1370e399441dSJames Smart 		ret = -EBADF;
1371e399441dSJames Smart 		dev_err(ctrl->dev,
13727db39484SJames Smart 			"q %d Create I/O Connection LS failed: %s\n",
1373e399441dSJames Smart 			queue->qnum, validation_errors[fcret]);
1374e399441dSJames Smart 	} else {
1375e399441dSJames Smart 		queue->connection_id =
1376e399441dSJames Smart 			be64_to_cpu(conn_acc->connectid.connection_id);
1377e399441dSJames Smart 		set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1378e399441dSJames Smart 	}
1379e399441dSJames Smart 
1380e399441dSJames Smart out_free_buffer:
1381e399441dSJames Smart 	kfree(lsop);
1382e399441dSJames Smart out_no_memory:
1383e399441dSJames Smart 	if (ret)
1384e399441dSJames Smart 		dev_err(ctrl->dev,
13857db39484SJames Smart 			"queue %d connect I/O queue failed (%d).\n",
1386e399441dSJames Smart 			queue->qnum, ret);
1387e399441dSJames Smart 	return ret;
1388e399441dSJames Smart }
1389e399441dSJames Smart 
1390e399441dSJames Smart static void
1391e399441dSJames Smart nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1392e399441dSJames Smart {
1393e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1394e399441dSJames Smart 
1395c913a8b0SJames Smart 	__nvme_fc_finish_ls_req(lsop);
1396e399441dSJames Smart 
1397d4e4230cSMilan P. Gandhi 	/* fc-nvme initiator doesn't care about success or failure of cmd */
1398e399441dSJames Smart 
1399e399441dSJames Smart 	kfree(lsop);
1400e399441dSJames Smart }
1401e399441dSJames Smart 
1402e399441dSJames Smart /*
1403e399441dSJames Smart  * This routine sends a FC-NVME LS to disconnect (aka terminate)
1404e399441dSJames Smart  * the FC-NVME Association.  Terminating the association also
1405e399441dSJames Smart  * terminates the FC-NVME connections (per queue, both admin and io
1406e399441dSJames Smart  * queues) that are part of the association. E.g. things are torn
1407e399441dSJames Smart  * down, and the related FC-NVME Association ID and Connection IDs
1408e399441dSJames Smart  * become invalid.
1409e399441dSJames Smart  *
1410e399441dSJames Smart  * The behavior of the fc-nvme initiator is such that it's
1411e399441dSJames Smart  * understanding of the association and connections will implicitly
1412e399441dSJames Smart  * be torn down. The action is implicit as it may be due to a loss of
1413e399441dSJames Smart  * connectivity with the fc-nvme target, so you may never get a
1414e399441dSJames Smart  * response even if you tried.  As such, the action of this routine
1415e399441dSJames Smart  * is to asynchronously send the LS, ignore any results of the LS, and
1416e399441dSJames Smart  * continue on with terminating the association. If the fc-nvme target
1417e399441dSJames Smart  * is present and receives the LS, it too can tear down.
1418e399441dSJames Smart  */
1419e399441dSJames Smart static void
1420e399441dSJames Smart nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1421e399441dSJames Smart {
142253b2b2f5SJames Smart 	struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
142353b2b2f5SJames Smart 	struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
1424e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
1425e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
1426c913a8b0SJames Smart 	int ret;
1427e399441dSJames Smart 
1428e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
1429f56bf76fSJames Smart 			sizeof(*discon_rqst) + sizeof(*discon_acc) +
1430f56bf76fSJames Smart 			ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1431f56bf76fSJames Smart 	if (!lsop) {
1432f56bf76fSJames Smart 		dev_info(ctrl->ctrl.device,
1433f56bf76fSJames Smart 			"NVME-FC{%d}: send Disconnect Association "
1434f56bf76fSJames Smart 			"failed: ENOMEM\n",
1435f56bf76fSJames Smart 			ctrl->cnum);
1436e399441dSJames Smart 		return;
1437f56bf76fSJames Smart 	}
1438e399441dSJames Smart 
1439f56bf76fSJames Smart 	discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)&lsop[1];
144053b2b2f5SJames Smart 	discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
1441f56bf76fSJames Smart 	lsreq = &lsop->ls_req;
1442f56bf76fSJames Smart 	if (ctrl->lport->ops->lsrqst_priv_sz)
1443f56bf76fSJames Smart 		lsreq->private = (void *)&discon_acc[1];
1444f56bf76fSJames Smart 	else
1445f56bf76fSJames Smart 		lsreq->private = NULL;
1446e399441dSJames Smart 
1447fd5a5f22SJames Smart 	nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc,
1448fd5a5f22SJames Smart 				ctrl->association_id);
1449e399441dSJames Smart 
1450c913a8b0SJames Smart 	ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1451c913a8b0SJames Smart 				nvme_fc_disconnect_assoc_done);
1452c913a8b0SJames Smart 	if (ret)
1453c913a8b0SJames Smart 		kfree(lsop);
1454e399441dSJames Smart }
1455e399441dSJames Smart 
145614fd1e98SJames Smart static void
145714fd1e98SJames Smart nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
145814fd1e98SJames Smart {
145914fd1e98SJames Smart 	struct nvmefc_ls_rcv_op *lsop = lsrsp->nvme_fc_private;
146014fd1e98SJames Smart 	struct nvme_fc_rport *rport = lsop->rport;
146114fd1e98SJames Smart 	struct nvme_fc_lport *lport = rport->lport;
146214fd1e98SJames Smart 	unsigned long flags;
146314fd1e98SJames Smart 
146414fd1e98SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
146514fd1e98SJames Smart 	list_del(&lsop->lsrcv_list);
146614fd1e98SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
146714fd1e98SJames Smart 
146814fd1e98SJames Smart 	fc_dma_sync_single_for_cpu(lport->dev, lsop->rspdma,
146914fd1e98SJames Smart 				sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
147014fd1e98SJames Smart 	fc_dma_unmap_single(lport->dev, lsop->rspdma,
147114fd1e98SJames Smart 			sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
147214fd1e98SJames Smart 
147314fd1e98SJames Smart 	kfree(lsop);
147414fd1e98SJames Smart 
147514fd1e98SJames Smart 	nvme_fc_rport_put(rport);
147614fd1e98SJames Smart }
147714fd1e98SJames Smart 
147814fd1e98SJames Smart static void
147914fd1e98SJames Smart nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op *lsop)
148014fd1e98SJames Smart {
148114fd1e98SJames Smart 	struct nvme_fc_rport *rport = lsop->rport;
148214fd1e98SJames Smart 	struct nvme_fc_lport *lport = rport->lport;
148314fd1e98SJames Smart 	struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
148414fd1e98SJames Smart 	int ret;
148514fd1e98SJames Smart 
148614fd1e98SJames Smart 	fc_dma_sync_single_for_device(lport->dev, lsop->rspdma,
148714fd1e98SJames Smart 				  sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
148814fd1e98SJames Smart 
148914fd1e98SJames Smart 	ret = lport->ops->xmt_ls_rsp(&lport->localport, &rport->remoteport,
149014fd1e98SJames Smart 				     lsop->lsrsp);
149114fd1e98SJames Smart 	if (ret) {
149214fd1e98SJames Smart 		dev_warn(lport->dev,
149314fd1e98SJames Smart 			"LLDD rejected LS RSP xmt: LS %d status %d\n",
149414fd1e98SJames Smart 			w0->ls_cmd, ret);
149514fd1e98SJames Smart 		nvme_fc_xmt_ls_rsp_done(lsop->lsrsp);
149614fd1e98SJames Smart 		return;
149714fd1e98SJames Smart 	}
149814fd1e98SJames Smart }
149914fd1e98SJames Smart 
150014fd1e98SJames Smart static struct nvme_fc_ctrl *
150114fd1e98SJames Smart nvme_fc_match_disconn_ls(struct nvme_fc_rport *rport,
150214fd1e98SJames Smart 		      struct nvmefc_ls_rcv_op *lsop)
150314fd1e98SJames Smart {
150414fd1e98SJames Smart 	struct fcnvme_ls_disconnect_assoc_rqst *rqst =
150514fd1e98SJames Smart 					&lsop->rqstbuf->rq_dis_assoc;
150614fd1e98SJames Smart 	struct nvme_fc_ctrl *ctrl, *ret = NULL;
150714fd1e98SJames Smart 	struct nvmefc_ls_rcv_op *oldls = NULL;
150814fd1e98SJames Smart 	u64 association_id = be64_to_cpu(rqst->associd.association_id);
150914fd1e98SJames Smart 	unsigned long flags;
151014fd1e98SJames Smart 
151114fd1e98SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
151214fd1e98SJames Smart 
151314fd1e98SJames Smart 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
151414fd1e98SJames Smart 		if (!nvme_fc_ctrl_get(ctrl))
151514fd1e98SJames Smart 			continue;
151614fd1e98SJames Smart 		spin_lock(&ctrl->lock);
151714fd1e98SJames Smart 		if (association_id == ctrl->association_id) {
151814fd1e98SJames Smart 			oldls = ctrl->rcv_disconn;
151914fd1e98SJames Smart 			ctrl->rcv_disconn = lsop;
152014fd1e98SJames Smart 			ret = ctrl;
152114fd1e98SJames Smart 		}
152214fd1e98SJames Smart 		spin_unlock(&ctrl->lock);
152314fd1e98SJames Smart 		if (ret)
152414fd1e98SJames Smart 			/* leave the ctrl get reference */
152514fd1e98SJames Smart 			break;
152614fd1e98SJames Smart 		nvme_fc_ctrl_put(ctrl);
152714fd1e98SJames Smart 	}
152814fd1e98SJames Smart 
152914fd1e98SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
153014fd1e98SJames Smart 
153114fd1e98SJames Smart 	/* transmit a response for anything that was pending */
153214fd1e98SJames Smart 	if (oldls) {
153314fd1e98SJames Smart 		dev_info(rport->lport->dev,
153414fd1e98SJames Smart 			"NVME-FC{%d}: Multiple Disconnect Association "
153514fd1e98SJames Smart 			"LS's received\n", ctrl->cnum);
153614fd1e98SJames Smart 		/* overwrite good response with bogus failure */
153714fd1e98SJames Smart 		oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf,
153814fd1e98SJames Smart 						sizeof(*oldls->rspbuf),
153914fd1e98SJames Smart 						rqst->w0.ls_cmd,
154014fd1e98SJames Smart 						FCNVME_RJT_RC_UNAB,
154114fd1e98SJames Smart 						FCNVME_RJT_EXP_NONE, 0);
154214fd1e98SJames Smart 		nvme_fc_xmt_ls_rsp(oldls);
154314fd1e98SJames Smart 	}
154414fd1e98SJames Smart 
154514fd1e98SJames Smart 	return ret;
154614fd1e98SJames Smart }
154714fd1e98SJames Smart 
154814fd1e98SJames Smart /*
154914fd1e98SJames Smart  * returns true to mean LS handled and ls_rsp can be sent
155014fd1e98SJames Smart  * returns false to defer ls_rsp xmt (will be done as part of
155114fd1e98SJames Smart  *     association termination)
155214fd1e98SJames Smart  */
155314fd1e98SJames Smart static bool
155414fd1e98SJames Smart nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op *lsop)
155514fd1e98SJames Smart {
155614fd1e98SJames Smart 	struct nvme_fc_rport *rport = lsop->rport;
155714fd1e98SJames Smart 	struct fcnvme_ls_disconnect_assoc_rqst *rqst =
155814fd1e98SJames Smart 					&lsop->rqstbuf->rq_dis_assoc;
155914fd1e98SJames Smart 	struct fcnvme_ls_disconnect_assoc_acc *acc =
156014fd1e98SJames Smart 					&lsop->rspbuf->rsp_dis_assoc;
156114fd1e98SJames Smart 	struct nvme_fc_ctrl *ctrl = NULL;
156214fd1e98SJames Smart 	int ret = 0;
156314fd1e98SJames Smart 
156414fd1e98SJames Smart 	memset(acc, 0, sizeof(*acc));
156514fd1e98SJames Smart 
156614fd1e98SJames Smart 	ret = nvmefc_vldt_lsreq_discon_assoc(lsop->rqstdatalen, rqst);
156714fd1e98SJames Smart 	if (!ret) {
156814fd1e98SJames Smart 		/* match an active association */
156914fd1e98SJames Smart 		ctrl = nvme_fc_match_disconn_ls(rport, lsop);
157014fd1e98SJames Smart 		if (!ctrl)
157114fd1e98SJames Smart 			ret = VERR_NO_ASSOC;
157214fd1e98SJames Smart 	}
157314fd1e98SJames Smart 
157414fd1e98SJames Smart 	if (ret) {
157514fd1e98SJames Smart 		dev_info(rport->lport->dev,
157614fd1e98SJames Smart 			"Disconnect LS failed: %s\n",
157714fd1e98SJames Smart 			validation_errors[ret]);
157814fd1e98SJames Smart 		lsop->lsrsp->rsplen = nvme_fc_format_rjt(acc,
157914fd1e98SJames Smart 					sizeof(*acc), rqst->w0.ls_cmd,
158014fd1e98SJames Smart 					(ret == VERR_NO_ASSOC) ?
158114fd1e98SJames Smart 						FCNVME_RJT_RC_INV_ASSOC :
158214fd1e98SJames Smart 						FCNVME_RJT_RC_LOGIC,
158314fd1e98SJames Smart 					FCNVME_RJT_EXP_NONE, 0);
158414fd1e98SJames Smart 		return true;
158514fd1e98SJames Smart 	}
158614fd1e98SJames Smart 
158714fd1e98SJames Smart 	/* format an ACCept response */
158814fd1e98SJames Smart 
158914fd1e98SJames Smart 	lsop->lsrsp->rsplen = sizeof(*acc);
159014fd1e98SJames Smart 
159114fd1e98SJames Smart 	nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
159214fd1e98SJames Smart 			fcnvme_lsdesc_len(
159314fd1e98SJames Smart 				sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
159414fd1e98SJames Smart 			FCNVME_LS_DISCONNECT_ASSOC);
159514fd1e98SJames Smart 
159614fd1e98SJames Smart 	/*
159714fd1e98SJames Smart 	 * the transmit of the response will occur after the exchanges
159814fd1e98SJames Smart 	 * for the association have been ABTS'd by
159914fd1e98SJames Smart 	 * nvme_fc_delete_association().
160014fd1e98SJames Smart 	 */
160114fd1e98SJames Smart 
160214fd1e98SJames Smart 	/* fail the association */
160314fd1e98SJames Smart 	nvme_fc_error_recovery(ctrl, "Disconnect Association LS received");
160414fd1e98SJames Smart 
160514fd1e98SJames Smart 	/* release the reference taken by nvme_fc_match_disconn_ls() */
160614fd1e98SJames Smart 	nvme_fc_ctrl_put(ctrl);
160714fd1e98SJames Smart 
160814fd1e98SJames Smart 	return false;
160914fd1e98SJames Smart }
161014fd1e98SJames Smart 
161114fd1e98SJames Smart /*
161214fd1e98SJames Smart  * Actual Processing routine for received FC-NVME LS Requests from the LLD
161314fd1e98SJames Smart  * returns true if a response should be sent afterward, false if rsp will
161414fd1e98SJames Smart  * be sent asynchronously.
161514fd1e98SJames Smart  */
161614fd1e98SJames Smart static bool
161714fd1e98SJames Smart nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op *lsop)
161814fd1e98SJames Smart {
161914fd1e98SJames Smart 	struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
162014fd1e98SJames Smart 	bool ret = true;
162114fd1e98SJames Smart 
162214fd1e98SJames Smart 	lsop->lsrsp->nvme_fc_private = lsop;
162314fd1e98SJames Smart 	lsop->lsrsp->rspbuf = lsop->rspbuf;
162414fd1e98SJames Smart 	lsop->lsrsp->rspdma = lsop->rspdma;
162514fd1e98SJames Smart 	lsop->lsrsp->done = nvme_fc_xmt_ls_rsp_done;
162614fd1e98SJames Smart 	/* Be preventative. handlers will later set to valid length */
162714fd1e98SJames Smart 	lsop->lsrsp->rsplen = 0;
162814fd1e98SJames Smart 
162914fd1e98SJames Smart 	/*
163014fd1e98SJames Smart 	 * handlers:
163114fd1e98SJames Smart 	 *   parse request input, execute the request, and format the
163214fd1e98SJames Smart 	 *   LS response
163314fd1e98SJames Smart 	 */
163414fd1e98SJames Smart 	switch (w0->ls_cmd) {
163514fd1e98SJames Smart 	case FCNVME_LS_DISCONNECT_ASSOC:
163614fd1e98SJames Smart 		ret = nvme_fc_ls_disconnect_assoc(lsop);
163714fd1e98SJames Smart 		break;
163814fd1e98SJames Smart 	case FCNVME_LS_DISCONNECT_CONN:
163914fd1e98SJames Smart 		lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
164014fd1e98SJames Smart 				sizeof(*lsop->rspbuf), w0->ls_cmd,
164114fd1e98SJames Smart 				FCNVME_RJT_RC_UNSUP, FCNVME_RJT_EXP_NONE, 0);
164214fd1e98SJames Smart 		break;
164314fd1e98SJames Smart 	case FCNVME_LS_CREATE_ASSOCIATION:
164414fd1e98SJames Smart 	case FCNVME_LS_CREATE_CONNECTION:
164514fd1e98SJames Smart 		lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
164614fd1e98SJames Smart 				sizeof(*lsop->rspbuf), w0->ls_cmd,
164714fd1e98SJames Smart 				FCNVME_RJT_RC_LOGIC, FCNVME_RJT_EXP_NONE, 0);
164814fd1e98SJames Smart 		break;
164914fd1e98SJames Smart 	default:
165014fd1e98SJames Smart 		lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
165114fd1e98SJames Smart 				sizeof(*lsop->rspbuf), w0->ls_cmd,
165214fd1e98SJames Smart 				FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
165314fd1e98SJames Smart 		break;
165414fd1e98SJames Smart 	}
165514fd1e98SJames Smart 
165614fd1e98SJames Smart 	return(ret);
165714fd1e98SJames Smart }
165814fd1e98SJames Smart 
165914fd1e98SJames Smart static void
166014fd1e98SJames Smart nvme_fc_handle_ls_rqst_work(struct work_struct *work)
166114fd1e98SJames Smart {
166214fd1e98SJames Smart 	struct nvme_fc_rport *rport =
166314fd1e98SJames Smart 		container_of(work, struct nvme_fc_rport, lsrcv_work);
166414fd1e98SJames Smart 	struct fcnvme_ls_rqst_w0 *w0;
166514fd1e98SJames Smart 	struct nvmefc_ls_rcv_op *lsop;
166614fd1e98SJames Smart 	unsigned long flags;
166714fd1e98SJames Smart 	bool sendrsp;
166814fd1e98SJames Smart 
166914fd1e98SJames Smart restart:
167014fd1e98SJames Smart 	sendrsp = true;
167114fd1e98SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
167214fd1e98SJames Smart 	list_for_each_entry(lsop, &rport->ls_rcv_list, lsrcv_list) {
167314fd1e98SJames Smart 		if (lsop->handled)
167414fd1e98SJames Smart 			continue;
167514fd1e98SJames Smart 
167614fd1e98SJames Smart 		lsop->handled = true;
167714fd1e98SJames Smart 		if (rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
167814fd1e98SJames Smart 			spin_unlock_irqrestore(&rport->lock, flags);
167914fd1e98SJames Smart 			sendrsp = nvme_fc_handle_ls_rqst(lsop);
168014fd1e98SJames Smart 		} else {
168114fd1e98SJames Smart 			spin_unlock_irqrestore(&rport->lock, flags);
168214fd1e98SJames Smart 			w0 = &lsop->rqstbuf->w0;
168314fd1e98SJames Smart 			lsop->lsrsp->rsplen = nvme_fc_format_rjt(
168414fd1e98SJames Smart 						lsop->rspbuf,
168514fd1e98SJames Smart 						sizeof(*lsop->rspbuf),
168614fd1e98SJames Smart 						w0->ls_cmd,
168714fd1e98SJames Smart 						FCNVME_RJT_RC_UNAB,
168814fd1e98SJames Smart 						FCNVME_RJT_EXP_NONE, 0);
168914fd1e98SJames Smart 		}
169014fd1e98SJames Smart 		if (sendrsp)
169114fd1e98SJames Smart 			nvme_fc_xmt_ls_rsp(lsop);
169214fd1e98SJames Smart 		goto restart;
169314fd1e98SJames Smart 	}
169414fd1e98SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
169514fd1e98SJames Smart }
169614fd1e98SJames Smart 
169772e6329fSJames Smart /**
169872e6329fSJames Smart  * nvme_fc_rcv_ls_req - transport entry point called by an LLDD
169972e6329fSJames Smart  *                       upon the reception of a NVME LS request.
170072e6329fSJames Smart  *
170172e6329fSJames Smart  * The nvme-fc layer will copy payload to an internal structure for
170272e6329fSJames Smart  * processing.  As such, upon completion of the routine, the LLDD may
170372e6329fSJames Smart  * immediately free/reuse the LS request buffer passed in the call.
170472e6329fSJames Smart  *
170572e6329fSJames Smart  * If this routine returns error, the LLDD should abort the exchange.
170672e6329fSJames Smart  *
170772e6329fSJames Smart  * @remoteport: pointer to the (registered) remote port that the LS
170872e6329fSJames Smart  *              was received from. The remoteport is associated with
170972e6329fSJames Smart  *              a specific localport.
171072e6329fSJames Smart  * @lsrsp:      pointer to a nvmefc_ls_rsp response structure to be
171172e6329fSJames Smart  *              used to reference the exchange corresponding to the LS
171272e6329fSJames Smart  *              when issuing an ls response.
171372e6329fSJames Smart  * @lsreqbuf:   pointer to the buffer containing the LS Request
171472e6329fSJames Smart  * @lsreqbuf_len: length, in bytes, of the received LS request
171572e6329fSJames Smart  */
171672e6329fSJames Smart int
171772e6329fSJames Smart nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
171872e6329fSJames Smart 			struct nvmefc_ls_rsp *lsrsp,
171972e6329fSJames Smart 			void *lsreqbuf, u32 lsreqbuf_len)
172072e6329fSJames Smart {
172172e6329fSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
172272e6329fSJames Smart 	struct nvme_fc_lport *lport = rport->lport;
172314fd1e98SJames Smart 	struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf;
172414fd1e98SJames Smart 	struct nvmefc_ls_rcv_op *lsop;
172514fd1e98SJames Smart 	unsigned long flags;
172614fd1e98SJames Smart 	int ret;
172714fd1e98SJames Smart 
172814fd1e98SJames Smart 	nvme_fc_rport_get(rport);
172972e6329fSJames Smart 
173072e6329fSJames Smart 	/* validate there's a routine to transmit a response */
173114fd1e98SJames Smart 	if (!lport->ops->xmt_ls_rsp) {
173214fd1e98SJames Smart 		dev_info(lport->dev,
173314fd1e98SJames Smart 			"RCV %s LS failed: no LLDD xmt_ls_rsp\n",
173414fd1e98SJames Smart 			(w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
173514fd1e98SJames Smart 				nvmefc_ls_names[w0->ls_cmd] : "");
173614fd1e98SJames Smart 		ret = -EINVAL;
173714fd1e98SJames Smart 		goto out_put;
173814fd1e98SJames Smart 	}
173914fd1e98SJames Smart 
174014fd1e98SJames Smart 	if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) {
174114fd1e98SJames Smart 		dev_info(lport->dev,
174214fd1e98SJames Smart 			"RCV %s LS failed: payload too large\n",
174314fd1e98SJames Smart 			(w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
174414fd1e98SJames Smart 				nvmefc_ls_names[w0->ls_cmd] : "");
174514fd1e98SJames Smart 		ret = -E2BIG;
174614fd1e98SJames Smart 		goto out_put;
174714fd1e98SJames Smart 	}
174814fd1e98SJames Smart 
174914fd1e98SJames Smart 	lsop = kzalloc(sizeof(*lsop) +
175014fd1e98SJames Smart 			sizeof(union nvmefc_ls_requests) +
175114fd1e98SJames Smart 			sizeof(union nvmefc_ls_responses),
175214fd1e98SJames Smart 			GFP_KERNEL);
175314fd1e98SJames Smart 	if (!lsop) {
175414fd1e98SJames Smart 		dev_info(lport->dev,
175514fd1e98SJames Smart 			"RCV %s LS failed: No memory\n",
175614fd1e98SJames Smart 			(w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
175714fd1e98SJames Smart 				nvmefc_ls_names[w0->ls_cmd] : "");
175814fd1e98SJames Smart 		ret = -ENOMEM;
175914fd1e98SJames Smart 		goto out_put;
176014fd1e98SJames Smart 	}
176114fd1e98SJames Smart 	lsop->rqstbuf = (union nvmefc_ls_requests *)&lsop[1];
176214fd1e98SJames Smart 	lsop->rspbuf = (union nvmefc_ls_responses *)&lsop->rqstbuf[1];
176314fd1e98SJames Smart 
176414fd1e98SJames Smart 	lsop->rspdma = fc_dma_map_single(lport->dev, lsop->rspbuf,
176514fd1e98SJames Smart 					sizeof(*lsop->rspbuf),
176614fd1e98SJames Smart 					DMA_TO_DEVICE);
176714fd1e98SJames Smart 	if (fc_dma_mapping_error(lport->dev, lsop->rspdma)) {
176814fd1e98SJames Smart 		dev_info(lport->dev,
176914fd1e98SJames Smart 			"RCV %s LS failed: DMA mapping failure\n",
177014fd1e98SJames Smart 			(w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
177114fd1e98SJames Smart 				nvmefc_ls_names[w0->ls_cmd] : "");
177214fd1e98SJames Smart 		ret = -EFAULT;
177314fd1e98SJames Smart 		goto out_free;
177414fd1e98SJames Smart 	}
177514fd1e98SJames Smart 
177614fd1e98SJames Smart 	lsop->rport = rport;
177714fd1e98SJames Smart 	lsop->lsrsp = lsrsp;
177814fd1e98SJames Smart 
177914fd1e98SJames Smart 	memcpy(lsop->rqstbuf, lsreqbuf, lsreqbuf_len);
178014fd1e98SJames Smart 	lsop->rqstdatalen = lsreqbuf_len;
178114fd1e98SJames Smart 
178214fd1e98SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
178314fd1e98SJames Smart 	if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) {
178414fd1e98SJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
178514fd1e98SJames Smart 		ret = -ENOTCONN;
178614fd1e98SJames Smart 		goto out_unmap;
178714fd1e98SJames Smart 	}
178814fd1e98SJames Smart 	list_add_tail(&lsop->lsrcv_list, &rport->ls_rcv_list);
178914fd1e98SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
179014fd1e98SJames Smart 
179114fd1e98SJames Smart 	schedule_work(&rport->lsrcv_work);
179272e6329fSJames Smart 
179372e6329fSJames Smart 	return 0;
179414fd1e98SJames Smart 
179514fd1e98SJames Smart out_unmap:
179614fd1e98SJames Smart 	fc_dma_unmap_single(lport->dev, lsop->rspdma,
179714fd1e98SJames Smart 			sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
179814fd1e98SJames Smart out_free:
179914fd1e98SJames Smart 	kfree(lsop);
180014fd1e98SJames Smart out_put:
180114fd1e98SJames Smart 	nvme_fc_rport_put(rport);
180214fd1e98SJames Smart 	return ret;
180372e6329fSJames Smart }
180472e6329fSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req);
180572e6329fSJames Smart 
1806e399441dSJames Smart 
1807e399441dSJames Smart /* *********************** NVME Ctrl Routines **************************** */
1808e399441dSJames Smart 
1809e399441dSJames Smart static void
1810e399441dSJames Smart __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1811e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
1812e399441dSJames Smart {
1813e399441dSJames Smart 	fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1814e399441dSJames Smart 				sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1815e399441dSJames Smart 	fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1816e399441dSJames Smart 				sizeof(op->cmd_iu), DMA_TO_DEVICE);
1817e399441dSJames Smart 
1818e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_UNINIT);
1819e399441dSJames Smart }
1820e399441dSJames Smart 
1821e399441dSJames Smart static void
1822d6296d39SChristoph Hellwig nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1823d6296d39SChristoph Hellwig 		unsigned int hctx_idx)
1824e399441dSJames Smart {
1825e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1826e399441dSJames Smart 
1827d6296d39SChristoph Hellwig 	return __nvme_fc_exit_request(set->driver_data, op);
1828e399441dSJames Smart }
1829e399441dSJames Smart 
183078a7ac26SJames Smart static int
183178a7ac26SJames Smart __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
183278a7ac26SJames Smart {
18333efd6e8eSJames Smart 	unsigned long flags;
18343efd6e8eSJames Smart 	int opstate;
183578a7ac26SJames Smart 
18363efd6e8eSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
18373efd6e8eSJames Smart 	opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
18383efd6e8eSJames Smart 	if (opstate != FCPOP_STATE_ACTIVE)
18393efd6e8eSJames Smart 		atomic_set(&op->state, opstate);
1840eb4ee8f1SJames Smart 	else if (test_bit(FCCTRL_TERMIO, &ctrl->flags))
18413efd6e8eSJames Smart 		ctrl->iocnt++;
18423efd6e8eSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
18433efd6e8eSJames Smart 
18443efd6e8eSJames Smart 	if (opstate != FCPOP_STATE_ACTIVE)
184578a7ac26SJames Smart 		return -ECANCELED;
184678a7ac26SJames Smart 
184778a7ac26SJames Smart 	ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
184878a7ac26SJames Smart 					&ctrl->rport->remoteport,
184978a7ac26SJames Smart 					op->queue->lldd_handle,
185078a7ac26SJames Smart 					&op->fcp_req);
185178a7ac26SJames Smart 
185278a7ac26SJames Smart 	return 0;
185378a7ac26SJames Smart }
185478a7ac26SJames Smart 
1855e399441dSJames Smart static void
185678a7ac26SJames Smart nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1857e399441dSJames Smart {
1858e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
18593efd6e8eSJames Smart 	int i;
1860e399441dSJames Smart 
18614cff280aSJames Smart 	/* ensure we've initialized the ops once */
18624cff280aSJames Smart 	if (!(aen_op->flags & FCOP_FLAGS_AEN))
18634cff280aSJames Smart 		return;
18644cff280aSJames Smart 
18653efd6e8eSJames Smart 	for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
18663efd6e8eSJames Smart 		__nvme_fc_abort_op(ctrl, aen_op);
186778a7ac26SJames Smart }
186878a7ac26SJames Smart 
1869c3aedd22SJames Smart static inline void
187078a7ac26SJames Smart __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
18713efd6e8eSJames Smart 		struct nvme_fc_fcp_op *op, int opstate)
187278a7ac26SJames Smart {
187378a7ac26SJames Smart 	unsigned long flags;
187478a7ac26SJames Smart 
1875c3aedd22SJames Smart 	if (opstate == FCPOP_STATE_ABORTED) {
187678a7ac26SJames Smart 		spin_lock_irqsave(&ctrl->lock, flags);
1877eb4ee8f1SJames Smart 		if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) {
187836715cf4SJames Smart 			if (!--ctrl->iocnt)
187936715cf4SJames Smart 				wake_up(&ctrl->ioabort_wait);
188036715cf4SJames Smart 		}
188178a7ac26SJames Smart 		spin_unlock_irqrestore(&ctrl->lock, flags);
1882c3aedd22SJames Smart 	}
188378a7ac26SJames Smart }
1884e399441dSJames Smart 
1885baee29acSChristoph Hellwig static void
1886e399441dSJames Smart nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1887e399441dSJames Smart {
1888e399441dSJames Smart 	struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1889e399441dSJames Smart 	struct request *rq = op->rq;
1890e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
1891e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
1892e399441dSJames Smart 	struct nvme_fc_queue *queue = op->queue;
1893e399441dSJames Smart 	struct nvme_completion *cqe = &op->rsp_iu.cqe;
1894458f280dSJames Smart 	struct nvme_command *sqe = &op->cmd_iu.sqe;
1895d663b69fSChristoph Hellwig 	__le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
189627fa9bc5SChristoph Hellwig 	union nvme_result result;
18970a02e39fSJames Smart 	bool terminate_assoc = true;
18983efd6e8eSJames Smart 	int opstate;
1899e399441dSJames Smart 
1900e399441dSJames Smart 	/*
1901e399441dSJames Smart 	 * WARNING:
1902e399441dSJames Smart 	 * The current linux implementation of a nvme controller
1903e399441dSJames Smart 	 * allocates a single tag set for all io queues and sizes
1904e399441dSJames Smart 	 * the io queues to fully hold all possible tags. Thus, the
1905e399441dSJames Smart 	 * implementation does not reference or care about the sqhd
1906e399441dSJames Smart 	 * value as it never needs to use the sqhd/sqtail pointers
1907e399441dSJames Smart 	 * for submission pacing.
1908e399441dSJames Smart 	 *
1909e399441dSJames Smart 	 * This affects the FC-NVME implementation in two ways:
1910e399441dSJames Smart 	 * 1) As the value doesn't matter, we don't need to waste
1911e399441dSJames Smart 	 *    cycles extracting it from ERSPs and stamping it in the
1912e399441dSJames Smart 	 *    cases where the transport fabricates CQEs on successful
1913e399441dSJames Smart 	 *    completions.
1914e399441dSJames Smart 	 * 2) The FC-NVME implementation requires that delivery of
1915e399441dSJames Smart 	 *    ERSP completions are to go back to the nvme layer in order
1916e399441dSJames Smart 	 *    relative to the rsn, such that the sqhd value will always
1917e399441dSJames Smart 	 *    be "in order" for the nvme layer. As the nvme layer in
1918e399441dSJames Smart 	 *    linux doesn't care about sqhd, there's no need to return
1919e399441dSJames Smart 	 *    them in order.
1920e399441dSJames Smart 	 *
1921e399441dSJames Smart 	 * Additionally:
1922e399441dSJames Smart 	 * As the core nvme layer in linux currently does not look at
1923e399441dSJames Smart 	 * every field in the cqe - in cases where the FC transport must
1924e399441dSJames Smart 	 * fabricate a CQE, the following fields will not be set as they
1925e399441dSJames Smart 	 * are not referenced:
1926e399441dSJames Smart 	 *      cqe.sqid,  cqe.sqhd,  cqe.command_id
1927f874d5d0SJames Smart 	 *
1928f874d5d0SJames Smart 	 * Failure or error of an individual i/o, in a transport
1929f874d5d0SJames Smart 	 * detected fashion unrelated to the nvme completion status,
1930f874d5d0SJames Smart 	 * potentially cause the initiator and target sides to get out
1931f874d5d0SJames Smart 	 * of sync on SQ head/tail (aka outstanding io count allowed).
1932f874d5d0SJames Smart 	 * Per FC-NVME spec, failure of an individual command requires
1933f874d5d0SJames Smart 	 * the connection to be terminated, which in turn requires the
1934f874d5d0SJames Smart 	 * association to be terminated.
1935e399441dSJames Smart 	 */
1936e399441dSJames Smart 
19373efd6e8eSJames Smart 	opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
19383efd6e8eSJames Smart 
1939e399441dSJames Smart 	fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1940e399441dSJames Smart 				sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1941e399441dSJames Smart 
19423efd6e8eSJames Smart 	if (opstate == FCPOP_STATE_ABORTED)
194374bd8cbeSJames Smart 		status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
194474bd8cbeSJames Smart 	else if (freq->status) {
194574bd8cbeSJames Smart 		status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
194674bd8cbeSJames Smart 		dev_info(ctrl->ctrl.device,
194774bd8cbeSJames Smart 			"NVME-FC{%d}: io failed due to lldd error %d\n",
194874bd8cbeSJames Smart 			ctrl->cnum, freq->status);
194974bd8cbeSJames Smart 	}
1950e399441dSJames Smart 
1951e399441dSJames Smart 	/*
1952e399441dSJames Smart 	 * For the linux implementation, if we have an unsuccesful
1953e399441dSJames Smart 	 * status, they blk-mq layer can typically be called with the
1954e399441dSJames Smart 	 * non-zero status and the content of the cqe isn't important.
1955e399441dSJames Smart 	 */
1956e399441dSJames Smart 	if (status)
1957e399441dSJames Smart 		goto done;
1958e399441dSJames Smart 
1959e399441dSJames Smart 	/*
1960e399441dSJames Smart 	 * command completed successfully relative to the wire
1961e399441dSJames Smart 	 * protocol. However, validate anything received and
1962e399441dSJames Smart 	 * extract the status and result from the cqe (create it
1963e399441dSJames Smart 	 * where necessary).
1964e399441dSJames Smart 	 */
1965e399441dSJames Smart 
1966e399441dSJames Smart 	switch (freq->rcv_rsplen) {
1967e399441dSJames Smart 
1968e399441dSJames Smart 	case 0:
1969e399441dSJames Smart 	case NVME_FC_SIZEOF_ZEROS_RSP:
1970e399441dSJames Smart 		/*
1971e399441dSJames Smart 		 * No response payload or 12 bytes of payload (which
1972e399441dSJames Smart 		 * should all be zeros) are considered successful and
1973e399441dSJames Smart 		 * no payload in the CQE by the transport.
1974e399441dSJames Smart 		 */
1975e399441dSJames Smart 		if (freq->transferred_length !=
1976e399441dSJames Smart 		    be32_to_cpu(op->cmd_iu.data_len)) {
197774bd8cbeSJames Smart 			status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
197874bd8cbeSJames Smart 			dev_info(ctrl->ctrl.device,
197974bd8cbeSJames Smart 				"NVME-FC{%d}: io failed due to bad transfer "
198074bd8cbeSJames Smart 				"length: %d vs expected %d\n",
198174bd8cbeSJames Smart 				ctrl->cnum, freq->transferred_length,
198274bd8cbeSJames Smart 				be32_to_cpu(op->cmd_iu.data_len));
1983e399441dSJames Smart 			goto done;
1984e399441dSJames Smart 		}
198527fa9bc5SChristoph Hellwig 		result.u64 = 0;
1986e399441dSJames Smart 		break;
1987e399441dSJames Smart 
1988e399441dSJames Smart 	case sizeof(struct nvme_fc_ersp_iu):
1989e399441dSJames Smart 		/*
1990e399441dSJames Smart 		 * The ERSP IU contains a full completion with CQE.
1991e399441dSJames Smart 		 * Validate ERSP IU and look at cqe.
1992e399441dSJames Smart 		 */
1993e399441dSJames Smart 		if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1994e399441dSJames Smart 					(freq->rcv_rsplen / 4) ||
1995e399441dSJames Smart 			     be32_to_cpu(op->rsp_iu.xfrd_len) !=
1996e399441dSJames Smart 					freq->transferred_length ||
199753b2b2f5SJames Smart 			     op->rsp_iu.ersp_result ||
1998458f280dSJames Smart 			     sqe->common.command_id != cqe->command_id)) {
199974bd8cbeSJames Smart 			status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
200074bd8cbeSJames Smart 			dev_info(ctrl->ctrl.device,
200174bd8cbeSJames Smart 				"NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
200274bd8cbeSJames Smart 				"iu len %d, xfr len %d vs %d, status code "
200374bd8cbeSJames Smart 				"%d, cmdid %d vs %d\n",
200474bd8cbeSJames Smart 				ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len),
200574bd8cbeSJames Smart 				be32_to_cpu(op->rsp_iu.xfrd_len),
200674bd8cbeSJames Smart 				freq->transferred_length,
200753b2b2f5SJames Smart 				op->rsp_iu.ersp_result,
200874bd8cbeSJames Smart 				sqe->common.command_id,
200974bd8cbeSJames Smart 				cqe->command_id);
2010e399441dSJames Smart 			goto done;
2011e399441dSJames Smart 		}
201227fa9bc5SChristoph Hellwig 		result = cqe->result;
2013d663b69fSChristoph Hellwig 		status = cqe->status;
2014e399441dSJames Smart 		break;
2015e399441dSJames Smart 
2016e399441dSJames Smart 	default:
201774bd8cbeSJames Smart 		status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
201874bd8cbeSJames Smart 		dev_info(ctrl->ctrl.device,
201974bd8cbeSJames Smart 			"NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
202074bd8cbeSJames Smart 			"len %d\n",
202174bd8cbeSJames Smart 			ctrl->cnum, freq->rcv_rsplen);
2022e399441dSJames Smart 		goto done;
2023e399441dSJames Smart 	}
2024e399441dSJames Smart 
2025f874d5d0SJames Smart 	terminate_assoc = false;
2026f874d5d0SJames Smart 
2027e399441dSJames Smart done:
202878a7ac26SJames Smart 	if (op->flags & FCOP_FLAGS_AEN) {
202927fa9bc5SChristoph Hellwig 		nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
20303efd6e8eSJames Smart 		__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
203178a7ac26SJames Smart 		atomic_set(&op->state, FCPOP_STATE_IDLE);
203278a7ac26SJames Smart 		op->flags = FCOP_FLAGS_AEN;	/* clear other flags */
2033e399441dSJames Smart 		nvme_fc_ctrl_put(ctrl);
2034f874d5d0SJames Smart 		goto check_error;
2035e399441dSJames Smart 	}
2036e399441dSJames Smart 
2037c3aedd22SJames Smart 	__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2038ff029451SChristoph Hellwig 	if (!nvme_end_request(rq, status, result))
2039ff029451SChristoph Hellwig 		nvme_fc_complete_rq(rq);
2040f874d5d0SJames Smart 
2041f874d5d0SJames Smart check_error:
2042f874d5d0SJames Smart 	if (terminate_assoc)
2043f874d5d0SJames Smart 		nvme_fc_error_recovery(ctrl, "transport detected io error");
2044e399441dSJames Smart }
2045e399441dSJames Smart 
2046e399441dSJames Smart static int
2047e399441dSJames Smart __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
2048e399441dSJames Smart 		struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
2049e399441dSJames Smart 		struct request *rq, u32 rqno)
2050e399441dSJames Smart {
2051d3d0bc78SBart Van Assche 	struct nvme_fcp_op_w_sgl *op_w_sgl =
2052d3d0bc78SBart Van Assche 		container_of(op, typeof(*op_w_sgl), op);
2053e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2054e399441dSJames Smart 	int ret = 0;
2055e399441dSJames Smart 
2056e399441dSJames Smart 	memset(op, 0, sizeof(*op));
2057e399441dSJames Smart 	op->fcp_req.cmdaddr = &op->cmd_iu;
2058e399441dSJames Smart 	op->fcp_req.cmdlen = sizeof(op->cmd_iu);
2059e399441dSJames Smart 	op->fcp_req.rspaddr = &op->rsp_iu;
2060e399441dSJames Smart 	op->fcp_req.rsplen = sizeof(op->rsp_iu);
2061e399441dSJames Smart 	op->fcp_req.done = nvme_fc_fcpio_done;
2062e399441dSJames Smart 	op->ctrl = ctrl;
2063e399441dSJames Smart 	op->queue = queue;
2064e399441dSJames Smart 	op->rq = rq;
2065e399441dSJames Smart 	op->rqno = rqno;
2066e399441dSJames Smart 
206753b2b2f5SJames Smart 	cmdiu->format_id = NVME_CMD_FORMAT_ID;
2068e399441dSJames Smart 	cmdiu->fc_id = NVME_CMD_FC_ID;
2069e399441dSJames Smart 	cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
207044fbf3bbSJames Smart 	if (queue->qnum)
207144fbf3bbSJames Smart 		cmdiu->rsv_cat = fccmnd_set_cat_css(0,
207244fbf3bbSJames Smart 					(NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT));
207344fbf3bbSJames Smart 	else
207444fbf3bbSJames Smart 		cmdiu->rsv_cat = fccmnd_set_cat_admin(0);
2075e399441dSJames Smart 
2076e399441dSJames Smart 	op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
2077e399441dSJames Smart 				&op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
2078e399441dSJames Smart 	if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
2079e399441dSJames Smart 		dev_err(ctrl->dev,
2080e399441dSJames Smart 			"FCP Op failed - cmdiu dma mapping failed.\n");
2081e399441dSJames Smart 		ret = EFAULT;
2082e399441dSJames Smart 		goto out_on_error;
2083e399441dSJames Smart 	}
2084e399441dSJames Smart 
2085e399441dSJames Smart 	op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
2086e399441dSJames Smart 				&op->rsp_iu, sizeof(op->rsp_iu),
2087e399441dSJames Smart 				DMA_FROM_DEVICE);
2088e399441dSJames Smart 	if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
2089e399441dSJames Smart 		dev_err(ctrl->dev,
2090e399441dSJames Smart 			"FCP Op failed - rspiu dma mapping failed.\n");
2091e399441dSJames Smart 		ret = EFAULT;
2092e399441dSJames Smart 	}
2093e399441dSJames Smart 
2094e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_IDLE);
2095e399441dSJames Smart out_on_error:
2096e399441dSJames Smart 	return ret;
2097e399441dSJames Smart }
2098e399441dSJames Smart 
2099e399441dSJames Smart static int
2100d6296d39SChristoph Hellwig nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
2101d6296d39SChristoph Hellwig 		unsigned int hctx_idx, unsigned int numa_node)
2102e399441dSJames Smart {
2103d6296d39SChristoph Hellwig 	struct nvme_fc_ctrl *ctrl = set->driver_data;
2104d3d0bc78SBart Van Assche 	struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq);
210576f983cbSChristoph Hellwig 	int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
210676f983cbSChristoph Hellwig 	struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
21070d2bdf9fSBart Van Assche 	int res;
2108e399441dSJames Smart 
21090d2bdf9fSBart Van Assche 	res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++);
21100d2bdf9fSBart Van Assche 	if (res)
21110d2bdf9fSBart Van Assche 		return res;
21123add1d93SArnd Bergmann 	op->op.fcp_req.first_sgl = op->sgl;
2113d19b8bc8SJames Smart 	op->op.fcp_req.private = &op->priv[0];
2114dfa74422SEwan D. Milne 	nvme_req(rq)->ctrl = &ctrl->ctrl;
21150d2bdf9fSBart Van Assche 	return res;
2116e399441dSJames Smart }
2117e399441dSJames Smart 
2118e399441dSJames Smart static int
2119e399441dSJames Smart nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
2120e399441dSJames Smart {
2121e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op;
2122e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu;
2123e399441dSJames Smart 	struct nvme_command *sqe;
2124f56bf76fSJames Smart 	void *private = NULL;
2125e399441dSJames Smart 	int i, ret;
2126e399441dSJames Smart 
2127e399441dSJames Smart 	aen_op = ctrl->aen_ops;
212838dabe21SKeith Busch 	for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2129f56bf76fSJames Smart 		if (ctrl->lport->ops->fcprqst_priv_sz) {
213061bff8efSJames Smart 			private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
213161bff8efSJames Smart 						GFP_KERNEL);
213261bff8efSJames Smart 			if (!private)
213361bff8efSJames Smart 				return -ENOMEM;
2134f56bf76fSJames Smart 		}
213561bff8efSJames Smart 
2136e399441dSJames Smart 		cmdiu = &aen_op->cmd_iu;
2137e399441dSJames Smart 		sqe = &cmdiu->sqe;
2138e399441dSJames Smart 		ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
2139e399441dSJames Smart 				aen_op, (struct request *)NULL,
214038dabe21SKeith Busch 				(NVME_AQ_BLK_MQ_DEPTH + i));
214161bff8efSJames Smart 		if (ret) {
214261bff8efSJames Smart 			kfree(private);
2143e399441dSJames Smart 			return ret;
214461bff8efSJames Smart 		}
2145e399441dSJames Smart 
214678a7ac26SJames Smart 		aen_op->flags = FCOP_FLAGS_AEN;
214761bff8efSJames Smart 		aen_op->fcp_req.private = private;
214878a7ac26SJames Smart 
2149e399441dSJames Smart 		memset(sqe, 0, sizeof(*sqe));
2150e399441dSJames Smart 		sqe->common.opcode = nvme_admin_async_event;
215178a7ac26SJames Smart 		/* Note: core layer may overwrite the sqe.command_id value */
215238dabe21SKeith Busch 		sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
2153e399441dSJames Smart 	}
2154e399441dSJames Smart 	return 0;
2155e399441dSJames Smart }
2156e399441dSJames Smart 
215761bff8efSJames Smart static void
215861bff8efSJames Smart nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
215961bff8efSJames Smart {
216061bff8efSJames Smart 	struct nvme_fc_fcp_op *aen_op;
216161bff8efSJames Smart 	int i;
216261bff8efSJames Smart 
216361bff8efSJames Smart 	aen_op = ctrl->aen_ops;
216438dabe21SKeith Busch 	for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
216561bff8efSJames Smart 		__nvme_fc_exit_request(ctrl, aen_op);
216661bff8efSJames Smart 
216761bff8efSJames Smart 		kfree(aen_op->fcp_req.private);
216861bff8efSJames Smart 		aen_op->fcp_req.private = NULL;
216961bff8efSJames Smart 	}
217061bff8efSJames Smart }
2171e399441dSJames Smart 
2172e399441dSJames Smart static inline void
2173e399441dSJames Smart __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
2174e399441dSJames Smart 		unsigned int qidx)
2175e399441dSJames Smart {
2176e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[qidx];
2177e399441dSJames Smart 
2178e399441dSJames Smart 	hctx->driver_data = queue;
2179e399441dSJames Smart 	queue->hctx = hctx;
2180e399441dSJames Smart }
2181e399441dSJames Smart 
2182e399441dSJames Smart static int
2183e399441dSJames Smart nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2184e399441dSJames Smart 		unsigned int hctx_idx)
2185e399441dSJames Smart {
2186e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = data;
2187e399441dSJames Smart 
2188e399441dSJames Smart 	__nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
2189e399441dSJames Smart 
2190e399441dSJames Smart 	return 0;
2191e399441dSJames Smart }
2192e399441dSJames Smart 
2193e399441dSJames Smart static int
2194e399441dSJames Smart nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2195e399441dSJames Smart 		unsigned int hctx_idx)
2196e399441dSJames Smart {
2197e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = data;
2198e399441dSJames Smart 
2199e399441dSJames Smart 	__nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
2200e399441dSJames Smart 
2201e399441dSJames Smart 	return 0;
2202e399441dSJames Smart }
2203e399441dSJames Smart 
2204e399441dSJames Smart static void
220508e15075SKeith Busch nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
2206e399441dSJames Smart {
2207e399441dSJames Smart 	struct nvme_fc_queue *queue;
2208e399441dSJames Smart 
2209e399441dSJames Smart 	queue = &ctrl->queues[idx];
2210e399441dSJames Smart 	memset(queue, 0, sizeof(*queue));
2211e399441dSJames Smart 	queue->ctrl = ctrl;
2212e399441dSJames Smart 	queue->qnum = idx;
221367f471b6SJames Smart 	atomic_set(&queue->csn, 0);
2214e399441dSJames Smart 	queue->dev = ctrl->dev;
2215e399441dSJames Smart 
2216e399441dSJames Smart 	if (idx > 0)
2217e399441dSJames Smart 		queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
2218e399441dSJames Smart 	else
2219e399441dSJames Smart 		queue->cmnd_capsule_len = sizeof(struct nvme_command);
2220e399441dSJames Smart 
2221e399441dSJames Smart 	/*
2222e399441dSJames Smart 	 * Considered whether we should allocate buffers for all SQEs
2223e399441dSJames Smart 	 * and CQEs and dma map them - mapping their respective entries
2224e399441dSJames Smart 	 * into the request structures (kernel vm addr and dma address)
2225e399441dSJames Smart 	 * thus the driver could use the buffers/mappings directly.
2226e399441dSJames Smart 	 * It only makes sense if the LLDD would use them for its
2227e399441dSJames Smart 	 * messaging api. It's very unlikely most adapter api's would use
2228e399441dSJames Smart 	 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
2229e399441dSJames Smart 	 * structures were used instead.
2230e399441dSJames Smart 	 */
2231e399441dSJames Smart }
2232e399441dSJames Smart 
2233e399441dSJames Smart /*
2234e399441dSJames Smart  * This routine terminates a queue at the transport level.
2235e399441dSJames Smart  * The transport has already ensured that all outstanding ios on
2236e399441dSJames Smart  * the queue have been terminated.
2237e399441dSJames Smart  * The transport will send a Disconnect LS request to terminate
2238e399441dSJames Smart  * the queue's connection. Termination of the admin queue will also
2239e399441dSJames Smart  * terminate the association at the target.
2240e399441dSJames Smart  */
2241e399441dSJames Smart static void
2242e399441dSJames Smart nvme_fc_free_queue(struct nvme_fc_queue *queue)
2243e399441dSJames Smart {
2244e399441dSJames Smart 	if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
2245e399441dSJames Smart 		return;
2246e399441dSJames Smart 
22479e0ed16aSSagi Grimberg 	clear_bit(NVME_FC_Q_LIVE, &queue->flags);
2248e399441dSJames Smart 	/*
2249e399441dSJames Smart 	 * Current implementation never disconnects a single queue.
2250e399441dSJames Smart 	 * It always terminates a whole association. So there is never
2251e399441dSJames Smart 	 * a disconnect(queue) LS sent to the target.
2252e399441dSJames Smart 	 */
2253e399441dSJames Smart 
2254e399441dSJames Smart 	queue->connection_id = 0;
225567f471b6SJames Smart 	atomic_set(&queue->csn, 0);
2256e399441dSJames Smart }
2257e399441dSJames Smart 
2258e399441dSJames Smart static void
2259e399441dSJames Smart __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
2260e399441dSJames Smart 	struct nvme_fc_queue *queue, unsigned int qidx)
2261e399441dSJames Smart {
2262e399441dSJames Smart 	if (ctrl->lport->ops->delete_queue)
2263e399441dSJames Smart 		ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
2264e399441dSJames Smart 				queue->lldd_handle);
2265e399441dSJames Smart 	queue->lldd_handle = NULL;
2266e399441dSJames Smart }
2267e399441dSJames Smart 
2268e399441dSJames Smart static void
2269e399441dSJames Smart nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
2270e399441dSJames Smart {
2271e399441dSJames Smart 	int i;
2272e399441dSJames Smart 
2273d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
2274e399441dSJames Smart 		nvme_fc_free_queue(&ctrl->queues[i]);
2275e399441dSJames Smart }
2276e399441dSJames Smart 
2277e399441dSJames Smart static int
2278e399441dSJames Smart __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
2279e399441dSJames Smart 	struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
2280e399441dSJames Smart {
2281e399441dSJames Smart 	int ret = 0;
2282e399441dSJames Smart 
2283e399441dSJames Smart 	queue->lldd_handle = NULL;
2284e399441dSJames Smart 	if (ctrl->lport->ops->create_queue)
2285e399441dSJames Smart 		ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
2286e399441dSJames Smart 				qidx, qsize, &queue->lldd_handle);
2287e399441dSJames Smart 
2288e399441dSJames Smart 	return ret;
2289e399441dSJames Smart }
2290e399441dSJames Smart 
2291e399441dSJames Smart static void
2292e399441dSJames Smart nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
2293e399441dSJames Smart {
2294d858e5f0SSagi Grimberg 	struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
2295e399441dSJames Smart 	int i;
2296e399441dSJames Smart 
2297d858e5f0SSagi Grimberg 	for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
2298e399441dSJames Smart 		__nvme_fc_delete_hw_queue(ctrl, queue, i);
2299e399441dSJames Smart }
2300e399441dSJames Smart 
2301e399441dSJames Smart static int
2302e399441dSJames Smart nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2303e399441dSJames Smart {
2304e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[1];
230517a1ec08SJohannes Thumshirn 	int i, ret;
2306e399441dSJames Smart 
2307d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
2308e399441dSJames Smart 		ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
230917a1ec08SJohannes Thumshirn 		if (ret)
231017a1ec08SJohannes Thumshirn 			goto delete_queues;
2311e399441dSJames Smart 	}
2312e399441dSJames Smart 
2313e399441dSJames Smart 	return 0;
231417a1ec08SJohannes Thumshirn 
231517a1ec08SJohannes Thumshirn delete_queues:
231617a1ec08SJohannes Thumshirn 	for (; i >= 0; i--)
231717a1ec08SJohannes Thumshirn 		__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
231817a1ec08SJohannes Thumshirn 	return ret;
2319e399441dSJames Smart }
2320e399441dSJames Smart 
2321e399441dSJames Smart static int
2322e399441dSJames Smart nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2323e399441dSJames Smart {
2324e399441dSJames Smart 	int i, ret = 0;
2325e399441dSJames Smart 
2326d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
2327e399441dSJames Smart 		ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
2328e399441dSJames Smart 					(qsize / 5));
2329e399441dSJames Smart 		if (ret)
2330e399441dSJames Smart 			break;
233126c68227SSagi Grimberg 		ret = nvmf_connect_io_queue(&ctrl->ctrl, i, false);
2332e399441dSJames Smart 		if (ret)
2333e399441dSJames Smart 			break;
23349e0ed16aSSagi Grimberg 
23359e0ed16aSSagi Grimberg 		set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
2336e399441dSJames Smart 	}
2337e399441dSJames Smart 
2338e399441dSJames Smart 	return ret;
2339e399441dSJames Smart }
2340e399441dSJames Smart 
2341e399441dSJames Smart static void
2342e399441dSJames Smart nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
2343e399441dSJames Smart {
2344e399441dSJames Smart 	int i;
2345e399441dSJames Smart 
2346d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
234708e15075SKeith Busch 		nvme_fc_init_queue(ctrl, i);
2348e399441dSJames Smart }
2349e399441dSJames Smart 
2350e399441dSJames Smart static void
2351e399441dSJames Smart nvme_fc_ctrl_free(struct kref *ref)
2352e399441dSJames Smart {
2353e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl =
2354e399441dSJames Smart 		container_of(ref, struct nvme_fc_ctrl, ref);
2355e399441dSJames Smart 	unsigned long flags;
2356e399441dSJames Smart 
235761bff8efSJames Smart 	if (ctrl->ctrl.tagset) {
235861bff8efSJames Smart 		blk_cleanup_queue(ctrl->ctrl.connect_q);
235961bff8efSJames Smart 		blk_mq_free_tag_set(&ctrl->tag_set);
236061bff8efSJames Smart 	}
236161bff8efSJames Smart 
2362e399441dSJames Smart 	/* remove from rport list */
2363e399441dSJames Smart 	spin_lock_irqsave(&ctrl->rport->lock, flags);
2364e399441dSJames Smart 	list_del(&ctrl->ctrl_list);
2365e399441dSJames Smart 	spin_unlock_irqrestore(&ctrl->rport->lock, flags);
236661bff8efSJames Smart 
2367f9c5af5fSSagi Grimberg 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
236861bff8efSJames Smart 	blk_cleanup_queue(ctrl->ctrl.admin_q);
2369e7832cb4SSagi Grimberg 	blk_cleanup_queue(ctrl->ctrl.fabrics_q);
237061bff8efSJames Smart 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
237161bff8efSJames Smart 
237261bff8efSJames Smart 	kfree(ctrl->queues);
2373e399441dSJames Smart 
2374e399441dSJames Smart 	put_device(ctrl->dev);
2375e399441dSJames Smart 	nvme_fc_rport_put(ctrl->rport);
2376e399441dSJames Smart 
2377e399441dSJames Smart 	ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2378de41447aSEwan D. Milne 	if (ctrl->ctrl.opts)
2379e399441dSJames Smart 		nvmf_free_options(ctrl->ctrl.opts);
2380e399441dSJames Smart 	kfree(ctrl);
2381e399441dSJames Smart }
2382e399441dSJames Smart 
2383e399441dSJames Smart static void
2384e399441dSJames Smart nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2385e399441dSJames Smart {
2386e399441dSJames Smart 	kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2387e399441dSJames Smart }
2388e399441dSJames Smart 
2389e399441dSJames Smart static int
2390e399441dSJames Smart nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2391e399441dSJames Smart {
2392e399441dSJames Smart 	return kref_get_unless_zero(&ctrl->ref);
2393e399441dSJames Smart }
2394e399441dSJames Smart 
2395e399441dSJames Smart /*
2396e399441dSJames Smart  * All accesses from nvme core layer done - can now free the
2397e399441dSJames Smart  * controller. Called after last nvme_put_ctrl() call
2398e399441dSJames Smart  */
2399e399441dSJames Smart static void
240061bff8efSJames Smart nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
2401e399441dSJames Smart {
2402e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2403e399441dSJames Smart 
2404e399441dSJames Smart 	WARN_ON(nctrl != &ctrl->ctrl);
2405e399441dSJames Smart 
2406e399441dSJames Smart 	nvme_fc_ctrl_put(ctrl);
2407e399441dSJames Smart }
2408e399441dSJames Smart 
240961bff8efSJames Smart static void
241061bff8efSJames Smart nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
241161bff8efSJames Smart {
24124cff280aSJames Smart 	int active;
24134cff280aSJames Smart 
24144cff280aSJames Smart 	/*
24154cff280aSJames Smart 	 * if an error (io timeout, etc) while (re)connecting,
24164cff280aSJames Smart 	 * it's an error on creating the new association.
24174cff280aSJames Smart 	 * Start the error recovery thread if it hasn't already
24184cff280aSJames Smart 	 * been started. It is expected there could be multiple
24194cff280aSJames Smart 	 * ios hitting this path before things are cleaned up.
24204cff280aSJames Smart 	 */
24214cff280aSJames Smart 	if (ctrl->ctrl.state == NVME_CTRL_CONNECTING) {
24224cff280aSJames Smart 		active = atomic_xchg(&ctrl->err_work_active, 1);
24238730c1ddSHannes Reinecke 		if (!active && !queue_work(nvme_fc_wq, &ctrl->err_work)) {
24244cff280aSJames Smart 			atomic_set(&ctrl->err_work_active, 0);
24254cff280aSJames Smart 			WARN_ON(1);
24264cff280aSJames Smart 		}
24274cff280aSJames Smart 		return;
24284cff280aSJames Smart 	}
24294cff280aSJames Smart 
24304cff280aSJames Smart 	/* Otherwise, only proceed if in LIVE state - e.g. on first error */
243169fa9646SJames Smart 	if (ctrl->ctrl.state != NVME_CTRL_LIVE)
243269fa9646SJames Smart 		return;
243369fa9646SJames Smart 
243461bff8efSJames Smart 	dev_warn(ctrl->ctrl.device,
243561bff8efSJames Smart 		"NVME-FC{%d}: transport association error detected: %s\n",
243661bff8efSJames Smart 		ctrl->cnum, errmsg);
2437589ff775SJames Smart 	dev_warn(ctrl->ctrl.device,
243861bff8efSJames Smart 		"NVME-FC{%d}: resetting controller\n", ctrl->cnum);
243961bff8efSJames Smart 
2440d86c4d8eSChristoph Hellwig 	nvme_reset_ctrl(&ctrl->ctrl);
244161bff8efSJames Smart }
244261bff8efSJames Smart 
2443baee29acSChristoph Hellwig static enum blk_eh_timer_return
2444e399441dSJames Smart nvme_fc_timeout(struct request *rq, bool reserved)
2445e399441dSJames Smart {
2446e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2447e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
2448e399441dSJames Smart 
2449e399441dSJames Smart 	/*
245061bff8efSJames Smart 	 * we can't individually ABTS an io without affecting the queue,
2451041018c6SJames Smart 	 * thus killing the queue, and thus the association.
245261bff8efSJames Smart 	 * So resolve by performing a controller reset, which will stop
245361bff8efSJames Smart 	 * the host/io stack, terminate the association on the link,
245461bff8efSJames Smart 	 * and recreate an association on the link.
2455e399441dSJames Smart 	 */
245661bff8efSJames Smart 	nvme_fc_error_recovery(ctrl, "io timeout error");
2457e399441dSJames Smart 
2458134aedc9SJames Smart 	/*
2459134aedc9SJames Smart 	 * the io abort has been initiated. Have the reset timer
2460134aedc9SJames Smart 	 * restarted and the abort completion will complete the io
2461134aedc9SJames Smart 	 * shortly. Avoids a synchronous wait while the abort finishes.
2462134aedc9SJames Smart 	 */
2463134aedc9SJames Smart 	return BLK_EH_RESET_TIMER;
2464e399441dSJames Smart }
2465e399441dSJames Smart 
2466e399441dSJames Smart static int
2467e399441dSJames Smart nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2468e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
2469e399441dSJames Smart {
2470e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
2471e399441dSJames Smart 	int ret;
2472e399441dSJames Smart 
2473e399441dSJames Smart 	freq->sg_cnt = 0;
2474e399441dSJames Smart 
24759f7d8ae2SJames Smart 	if (!blk_rq_nr_phys_segments(rq))
2476e399441dSJames Smart 		return 0;
2477e399441dSJames Smart 
2478e399441dSJames Smart 	freq->sg_table.sgl = freq->first_sgl;
247919e420bbSChristoph Hellwig 	ret = sg_alloc_table_chained(&freq->sg_table,
24804635873cSMing Lei 			blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
2481b1ae1a23SIsrael Rukshin 			NVME_INLINE_SG_CNT);
2482e399441dSJames Smart 	if (ret)
2483e399441dSJames Smart 		return -ENOMEM;
2484e399441dSJames Smart 
2485e399441dSJames Smart 	op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
248619e420bbSChristoph Hellwig 	WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2487e399441dSJames Smart 	freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2488f15872c5SIsrael Rukshin 				op->nents, rq_dma_dir(rq));
2489e399441dSJames Smart 	if (unlikely(freq->sg_cnt <= 0)) {
2490b1ae1a23SIsrael Rukshin 		sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2491e399441dSJames Smart 		freq->sg_cnt = 0;
2492e399441dSJames Smart 		return -EFAULT;
2493e399441dSJames Smart 	}
2494e399441dSJames Smart 
2495e399441dSJames Smart 	/*
2496e399441dSJames Smart 	 * TODO: blk_integrity_rq(rq)  for DIF
2497e399441dSJames Smart 	 */
2498e399441dSJames Smart 	return 0;
2499e399441dSJames Smart }
2500e399441dSJames Smart 
2501e399441dSJames Smart static void
2502e399441dSJames Smart nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2503e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
2504e399441dSJames Smart {
2505e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
2506e399441dSJames Smart 
2507e399441dSJames Smart 	if (!freq->sg_cnt)
2508e399441dSJames Smart 		return;
2509e399441dSJames Smart 
2510e399441dSJames Smart 	fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2511f15872c5SIsrael Rukshin 			rq_dma_dir(rq));
2512e399441dSJames Smart 
2513b1ae1a23SIsrael Rukshin 	sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2514e399441dSJames Smart 
2515e399441dSJames Smart 	freq->sg_cnt = 0;
2516e399441dSJames Smart }
2517e399441dSJames Smart 
2518e399441dSJames Smart /*
2519e399441dSJames Smart  * In FC, the queue is a logical thing. At transport connect, the target
2520e399441dSJames Smart  * creates its "queue" and returns a handle that is to be given to the
2521e399441dSJames Smart  * target whenever it posts something to the corresponding SQ.  When an
2522e399441dSJames Smart  * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2523e399441dSJames Smart  * command contained within the SQE, an io, and assigns a FC exchange
2524e399441dSJames Smart  * to it. The SQE and the associated SQ handle are sent in the initial
2525e399441dSJames Smart  * CMD IU sents on the exchange. All transfers relative to the io occur
2526e399441dSJames Smart  * as part of the exchange.  The CQE is the last thing for the io,
2527e399441dSJames Smart  * which is transferred (explicitly or implicitly) with the RSP IU
2528e399441dSJames Smart  * sent on the exchange. After the CQE is received, the FC exchange is
2529e399441dSJames Smart  * terminaed and the Exchange may be used on a different io.
2530e399441dSJames Smart  *
2531e399441dSJames Smart  * The transport to LLDD api has the transport making a request for a
2532e399441dSJames Smart  * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2533e399441dSJames Smart  * resource and transfers the command. The LLDD will then process all
2534e399441dSJames Smart  * steps to complete the io. Upon completion, the transport done routine
2535e399441dSJames Smart  * is called.
2536e399441dSJames Smart  *
2537e399441dSJames Smart  * So - while the operation is outstanding to the LLDD, there is a link
2538e399441dSJames Smart  * level FC exchange resource that is also outstanding. This must be
2539e399441dSJames Smart  * considered in all cleanup operations.
2540e399441dSJames Smart  */
2541fc17b653SChristoph Hellwig static blk_status_t
2542e399441dSJames Smart nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2543e399441dSJames Smart 	struct nvme_fc_fcp_op *op, u32 data_len,
2544e399441dSJames Smart 	enum nvmefc_fcp_datadir	io_dir)
2545e399441dSJames Smart {
2546e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2547e399441dSJames Smart 	struct nvme_command *sqe = &cmdiu->sqe;
2548b12740d3SJames Smart 	int ret, opstate;
2549e399441dSJames Smart 
255061bff8efSJames Smart 	/*
255161bff8efSJames Smart 	 * before attempting to send the io, check to see if we believe
255261bff8efSJames Smart 	 * the target device is present
255361bff8efSJames Smart 	 */
255461bff8efSJames Smart 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
255586ff7c2aSMing Lei 		return BLK_STS_RESOURCE;
255661bff8efSJames Smart 
2557e399441dSJames Smart 	if (!nvme_fc_ctrl_get(ctrl))
2558fc17b653SChristoph Hellwig 		return BLK_STS_IOERR;
2559e399441dSJames Smart 
2560e399441dSJames Smart 	/* format the FC-NVME CMD IU and fcp_req */
2561e399441dSJames Smart 	cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2562e399441dSJames Smart 	cmdiu->data_len = cpu_to_be32(data_len);
2563e399441dSJames Smart 	switch (io_dir) {
2564e399441dSJames Smart 	case NVMEFC_FCP_WRITE:
2565e399441dSJames Smart 		cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2566e399441dSJames Smart 		break;
2567e399441dSJames Smart 	case NVMEFC_FCP_READ:
2568e399441dSJames Smart 		cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2569e399441dSJames Smart 		break;
2570e399441dSJames Smart 	case NVMEFC_FCP_NODATA:
2571e399441dSJames Smart 		cmdiu->flags = 0;
2572e399441dSJames Smart 		break;
2573e399441dSJames Smart 	}
2574e399441dSJames Smart 	op->fcp_req.payload_length = data_len;
2575e399441dSJames Smart 	op->fcp_req.io_dir = io_dir;
2576e399441dSJames Smart 	op->fcp_req.transferred_length = 0;
2577e399441dSJames Smart 	op->fcp_req.rcv_rsplen = 0;
257862eeacb0SJames Smart 	op->fcp_req.status = NVME_SC_SUCCESS;
2579e399441dSJames Smart 	op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2580e399441dSJames Smart 
2581e399441dSJames Smart 	/*
2582e399441dSJames Smart 	 * validate per fabric rules, set fields mandated by fabric spec
2583e399441dSJames Smart 	 * as well as those by FC-NVME spec.
2584e399441dSJames Smart 	 */
2585e399441dSJames Smart 	WARN_ON_ONCE(sqe->common.metadata);
2586e399441dSJames Smart 	sqe->common.flags |= NVME_CMD_SGL_METABUF;
2587e399441dSJames Smart 
2588e399441dSJames Smart 	/*
2589d9d34c0bSJames Smart 	 * format SQE DPTR field per FC-NVME rules:
2590d9d34c0bSJames Smart 	 *    type=0x5     Transport SGL Data Block Descriptor
2591d9d34c0bSJames Smart 	 *    subtype=0xA  Transport-specific value
2592d9d34c0bSJames Smart 	 *    address=0
2593d9d34c0bSJames Smart 	 *    length=length of the data series
2594e399441dSJames Smart 	 */
2595d9d34c0bSJames Smart 	sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2596d9d34c0bSJames Smart 					NVME_SGL_FMT_TRANSPORT_A;
2597e399441dSJames Smart 	sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2598e399441dSJames Smart 	sqe->rw.dptr.sgl.addr = 0;
2599e399441dSJames Smart 
260078a7ac26SJames Smart 	if (!(op->flags & FCOP_FLAGS_AEN)) {
2601e399441dSJames Smart 		ret = nvme_fc_map_data(ctrl, op->rq, op);
2602e399441dSJames Smart 		if (ret < 0) {
2603e399441dSJames Smart 			nvme_cleanup_cmd(op->rq);
2604e399441dSJames Smart 			nvme_fc_ctrl_put(ctrl);
2605fc17b653SChristoph Hellwig 			if (ret == -ENOMEM || ret == -EAGAIN)
2606fc17b653SChristoph Hellwig 				return BLK_STS_RESOURCE;
2607fc17b653SChristoph Hellwig 			return BLK_STS_IOERR;
2608e399441dSJames Smart 		}
2609e399441dSJames Smart 	}
2610e399441dSJames Smart 
2611e399441dSJames Smart 	fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2612e399441dSJames Smart 				  sizeof(op->cmd_iu), DMA_TO_DEVICE);
2613e399441dSJames Smart 
2614e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2615e399441dSJames Smart 
261678a7ac26SJames Smart 	if (!(op->flags & FCOP_FLAGS_AEN))
2617e399441dSJames Smart 		blk_mq_start_request(op->rq);
2618e399441dSJames Smart 
261967f471b6SJames Smart 	cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2620e399441dSJames Smart 	ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2621e399441dSJames Smart 					&ctrl->rport->remoteport,
2622e399441dSJames Smart 					queue->lldd_handle, &op->fcp_req);
2623e399441dSJames Smart 
2624e399441dSJames Smart 	if (ret) {
262567f471b6SJames Smart 		/*
262667f471b6SJames Smart 		 * If the lld fails to send the command is there an issue with
262767f471b6SJames Smart 		 * the csn value?  If the command that fails is the Connect,
262867f471b6SJames Smart 		 * no - as the connection won't be live.  If it is a command
262967f471b6SJames Smart 		 * post-connect, it's possible a gap in csn may be created.
263067f471b6SJames Smart 		 * Does this matter?  As Linux initiators don't send fused
263167f471b6SJames Smart 		 * commands, no.  The gap would exist, but as there's nothing
263267f471b6SJames Smart 		 * that depends on csn order to be delivered on the target
263367f471b6SJames Smart 		 * side, it shouldn't hurt.  It would be difficult for a
263467f471b6SJames Smart 		 * target to even detect the csn gap as it has no idea when the
263567f471b6SJames Smart 		 * cmd with the csn was supposed to arrive.
263667f471b6SJames Smart 		 */
2637b12740d3SJames Smart 		opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2638b12740d3SJames Smart 		__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2639b12740d3SJames Smart 
2640c9c12e51SDaniel Wagner 		if (!(op->flags & FCOP_FLAGS_AEN)) {
2641e399441dSJames Smart 			nvme_fc_unmap_data(ctrl, op->rq, op);
264216686f3aSMax Gurtovoy 			nvme_cleanup_cmd(op->rq);
2643c9c12e51SDaniel Wagner 		}
2644c9c12e51SDaniel Wagner 
2645e399441dSJames Smart 		nvme_fc_ctrl_put(ctrl);
2646e399441dSJames Smart 
26478b25f351SJames Smart 		if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
26488b25f351SJames Smart 				ret != -EBUSY)
2649fc17b653SChristoph Hellwig 			return BLK_STS_IOERR;
2650e399441dSJames Smart 
265186ff7c2aSMing Lei 		return BLK_STS_RESOURCE;
2652e399441dSJames Smart 	}
2653e399441dSJames Smart 
2654fc17b653SChristoph Hellwig 	return BLK_STS_OK;
2655e399441dSJames Smart }
2656e399441dSJames Smart 
2657fc17b653SChristoph Hellwig static blk_status_t
2658e399441dSJames Smart nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2659e399441dSJames Smart 			const struct blk_mq_queue_data *bd)
2660e399441dSJames Smart {
2661e399441dSJames Smart 	struct nvme_ns *ns = hctx->queue->queuedata;
2662e399441dSJames Smart 	struct nvme_fc_queue *queue = hctx->driver_data;
2663e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = queue->ctrl;
2664e399441dSJames Smart 	struct request *rq = bd->rq;
2665e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2666e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2667e399441dSJames Smart 	struct nvme_command *sqe = &cmdiu->sqe;
2668e399441dSJames Smart 	enum nvmefc_fcp_datadir	io_dir;
26693bc32bb1SChristoph Hellwig 	bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2670e399441dSJames Smart 	u32 data_len;
2671fc17b653SChristoph Hellwig 	blk_status_t ret;
2672e399441dSJames Smart 
26733bc32bb1SChristoph Hellwig 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
26743bc32bb1SChristoph Hellwig 	    !nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
26756cdefc6eSJames Smart 		return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
26769e0ed16aSSagi Grimberg 
2677e399441dSJames Smart 	ret = nvme_setup_cmd(ns, rq, sqe);
2678e399441dSJames Smart 	if (ret)
2679e399441dSJames Smart 		return ret;
2680e399441dSJames Smart 
26819f7d8ae2SJames Smart 	/*
26829f7d8ae2SJames Smart 	 * nvme core doesn't quite treat the rq opaquely. Commands such
26839f7d8ae2SJames Smart 	 * as WRITE ZEROES will return a non-zero rq payload_bytes yet
26849f7d8ae2SJames Smart 	 * there is no actual payload to be transferred.
26859f7d8ae2SJames Smart 	 * To get it right, key data transmission on there being 1 or
26869f7d8ae2SJames Smart 	 * more physical segments in the sg list. If there is no
26879f7d8ae2SJames Smart 	 * physical segments, there is no payload.
26889f7d8ae2SJames Smart 	 */
26899f7d8ae2SJames Smart 	if (blk_rq_nr_phys_segments(rq)) {
2690b131c61dSChristoph Hellwig 		data_len = blk_rq_payload_bytes(rq);
2691e399441dSJames Smart 		io_dir = ((rq_data_dir(rq) == WRITE) ?
2692e399441dSJames Smart 					NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
26939f7d8ae2SJames Smart 	} else {
26949f7d8ae2SJames Smart 		data_len = 0;
2695e399441dSJames Smart 		io_dir = NVMEFC_FCP_NODATA;
26969f7d8ae2SJames Smart 	}
26979f7d8ae2SJames Smart 
2698e399441dSJames Smart 
2699e399441dSJames Smart 	return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2700e399441dSJames Smart }
2701e399441dSJames Smart 
2702e399441dSJames Smart static void
2703ad22c355SKeith Busch nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2704e399441dSJames Smart {
2705e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2706e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op;
2707fc17b653SChristoph Hellwig 	blk_status_t ret;
2708e399441dSJames Smart 
2709eb4ee8f1SJames Smart 	if (test_bit(FCCTRL_TERMIO, &ctrl->flags))
271061bff8efSJames Smart 		return;
271161bff8efSJames Smart 
2712ad22c355SKeith Busch 	aen_op = &ctrl->aen_ops[0];
2713e399441dSJames Smart 
2714e399441dSJames Smart 	ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2715e399441dSJames Smart 					NVMEFC_FCP_NODATA);
2716e399441dSJames Smart 	if (ret)
2717e399441dSJames Smart 		dev_err(ctrl->ctrl.device,
2718ad22c355SKeith Busch 			"failed async event work\n");
2719e399441dSJames Smart }
2720e399441dSJames Smart 
2721e399441dSJames Smart static void
2722c3aedd22SJames Smart nvme_fc_complete_rq(struct request *rq)
2723e399441dSJames Smart {
2724e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2725e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
2726e399441dSJames Smart 
272778a7ac26SJames Smart 	atomic_set(&op->state, FCPOP_STATE_IDLE);
2728e399441dSJames Smart 
2729e399441dSJames Smart 	nvme_fc_unmap_data(ctrl, rq, op);
273077f02a7aSChristoph Hellwig 	nvme_complete_rq(rq);
2731e399441dSJames Smart 	nvme_fc_ctrl_put(ctrl);
273278a7ac26SJames Smart }
273378a7ac26SJames Smart 
2734e399441dSJames Smart /*
2735e399441dSJames Smart  * This routine is used by the transport when it needs to find active
2736e399441dSJames Smart  * io on a queue that is to be terminated. The transport uses
2737e399441dSJames Smart  * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2738e399441dSJames Smart  * this routine to kill them on a 1 by 1 basis.
2739e399441dSJames Smart  *
2740e399441dSJames Smart  * As FC allocates FC exchange for each io, the transport must contact
2741e399441dSJames Smart  * the LLDD to terminate the exchange, thus releasing the FC exchange.
2742e399441dSJames Smart  * After terminating the exchange the LLDD will call the transport's
2743e399441dSJames Smart  * normal io done path for the request, but it will have an aborted
2744e399441dSJames Smart  * status. The done path will return the io request back to the block
2745e399441dSJames Smart  * layer with an error status.
2746e399441dSJames Smart  */
27477baa8572SJens Axboe static bool
2748e399441dSJames Smart nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2749e399441dSJames Smart {
2750e399441dSJames Smart 	struct nvme_ctrl *nctrl = data;
2751e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2752e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2753e399441dSJames Smart 
27543efd6e8eSJames Smart 	__nvme_fc_abort_op(ctrl, op);
27557baa8572SJens Axboe 	return true;
275678a7ac26SJames Smart }
2757e399441dSJames Smart 
2758e399441dSJames Smart 
275961bff8efSJames Smart static const struct blk_mq_ops nvme_fc_mq_ops = {
276061bff8efSJames Smart 	.queue_rq	= nvme_fc_queue_rq,
276161bff8efSJames Smart 	.complete	= nvme_fc_complete_rq,
276261bff8efSJames Smart 	.init_request	= nvme_fc_init_request,
276361bff8efSJames Smart 	.exit_request	= nvme_fc_exit_request,
276461bff8efSJames Smart 	.init_hctx	= nvme_fc_init_hctx,
276561bff8efSJames Smart 	.timeout	= nvme_fc_timeout,
2766e399441dSJames Smart };
2767e399441dSJames Smart 
2768e399441dSJames Smart static int
2769e399441dSJames Smart nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2770e399441dSJames Smart {
2771e399441dSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
27727314183dSSagi Grimberg 	unsigned int nr_io_queues;
2773e399441dSJames Smart 	int ret;
2774e399441dSJames Smart 
27757314183dSSagi Grimberg 	nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
27767314183dSSagi Grimberg 				ctrl->lport->ops->max_hw_queues);
27777314183dSSagi Grimberg 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2778e399441dSJames Smart 	if (ret) {
2779e399441dSJames Smart 		dev_info(ctrl->ctrl.device,
2780e399441dSJames Smart 			"set_queue_count failed: %d\n", ret);
2781e399441dSJames Smart 		return ret;
2782e399441dSJames Smart 	}
2783e399441dSJames Smart 
27847314183dSSagi Grimberg 	ctrl->ctrl.queue_count = nr_io_queues + 1;
27857314183dSSagi Grimberg 	if (!nr_io_queues)
2786e399441dSJames Smart 		return 0;
2787e399441dSJames Smart 
2788e399441dSJames Smart 	nvme_fc_init_io_queues(ctrl);
2789e399441dSJames Smart 
2790e399441dSJames Smart 	memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2791e399441dSJames Smart 	ctrl->tag_set.ops = &nvme_fc_mq_ops;
2792e399441dSJames Smart 	ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2793e399441dSJames Smart 	ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2794103e515eSHannes Reinecke 	ctrl->tag_set.numa_node = ctrl->ctrl.numa_node;
2795e399441dSJames Smart 	ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2796d3d0bc78SBart Van Assche 	ctrl->tag_set.cmd_size =
2797d3d0bc78SBart Van Assche 		struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv,
2798d3d0bc78SBart Van Assche 			    ctrl->lport->ops->fcprqst_priv_sz);
2799e399441dSJames Smart 	ctrl->tag_set.driver_data = ctrl;
2800d858e5f0SSagi Grimberg 	ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
2801e399441dSJames Smart 	ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2802e399441dSJames Smart 
2803e399441dSJames Smart 	ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2804e399441dSJames Smart 	if (ret)
2805e399441dSJames Smart 		return ret;
2806e399441dSJames Smart 
2807e399441dSJames Smart 	ctrl->ctrl.tagset = &ctrl->tag_set;
2808e399441dSJames Smart 
2809e399441dSJames Smart 	ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2810e399441dSJames Smart 	if (IS_ERR(ctrl->ctrl.connect_q)) {
2811e399441dSJames Smart 		ret = PTR_ERR(ctrl->ctrl.connect_q);
2812e399441dSJames Smart 		goto out_free_tag_set;
2813e399441dSJames Smart 	}
2814e399441dSJames Smart 
2815d157e534SJames Smart 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2816e399441dSJames Smart 	if (ret)
2817e399441dSJames Smart 		goto out_cleanup_blk_queue;
2818e399441dSJames Smart 
2819d157e534SJames Smart 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2820e399441dSJames Smart 	if (ret)
2821e399441dSJames Smart 		goto out_delete_hw_queues;
2822e399441dSJames Smart 
28234c984154SJames Smart 	ctrl->ioq_live = true;
28244c984154SJames Smart 
2825e399441dSJames Smart 	return 0;
2826e399441dSJames Smart 
2827e399441dSJames Smart out_delete_hw_queues:
2828e399441dSJames Smart 	nvme_fc_delete_hw_io_queues(ctrl);
2829e399441dSJames Smart out_cleanup_blk_queue:
2830e399441dSJames Smart 	blk_cleanup_queue(ctrl->ctrl.connect_q);
2831e399441dSJames Smart out_free_tag_set:
2832e399441dSJames Smart 	blk_mq_free_tag_set(&ctrl->tag_set);
2833e399441dSJames Smart 	nvme_fc_free_io_queues(ctrl);
2834e399441dSJames Smart 
2835e399441dSJames Smart 	/* force put free routine to ignore io queues */
2836e399441dSJames Smart 	ctrl->ctrl.tagset = NULL;
2837e399441dSJames Smart 
2838e399441dSJames Smart 	return ret;
2839e399441dSJames Smart }
2840e399441dSJames Smart 
284161bff8efSJames Smart static int
28423e493c00SJames Smart nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
284361bff8efSJames Smart {
284461bff8efSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2845834d3710SJames Smart 	u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
28467314183dSSagi Grimberg 	unsigned int nr_io_queues;
284761bff8efSJames Smart 	int ret;
284861bff8efSJames Smart 
28497314183dSSagi Grimberg 	nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
28507314183dSSagi Grimberg 				ctrl->lport->ops->max_hw_queues);
28517314183dSSagi Grimberg 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
285261bff8efSJames Smart 	if (ret) {
285361bff8efSJames Smart 		dev_info(ctrl->ctrl.device,
285461bff8efSJames Smart 			"set_queue_count failed: %d\n", ret);
285561bff8efSJames Smart 		return ret;
285661bff8efSJames Smart 	}
285761bff8efSJames Smart 
2858834d3710SJames Smart 	if (!nr_io_queues && prior_ioq_cnt) {
2859834d3710SJames Smart 		dev_info(ctrl->ctrl.device,
2860834d3710SJames Smart 			"Fail Reconnect: At least 1 io queue "
2861834d3710SJames Smart 			"required (was %d)\n", prior_ioq_cnt);
2862834d3710SJames Smart 		return -ENOSPC;
2863834d3710SJames Smart 	}
2864834d3710SJames Smart 
28657314183dSSagi Grimberg 	ctrl->ctrl.queue_count = nr_io_queues + 1;
286661bff8efSJames Smart 	/* check for io queues existing */
2867d858e5f0SSagi Grimberg 	if (ctrl->ctrl.queue_count == 1)
286861bff8efSJames Smart 		return 0;
286961bff8efSJames Smart 
2870d157e534SJames Smart 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
287161bff8efSJames Smart 	if (ret)
287261bff8efSJames Smart 		goto out_free_io_queues;
287361bff8efSJames Smart 
2874d157e534SJames Smart 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
287561bff8efSJames Smart 	if (ret)
287661bff8efSJames Smart 		goto out_delete_hw_queues;
287761bff8efSJames Smart 
2878834d3710SJames Smart 	if (prior_ioq_cnt != nr_io_queues)
2879834d3710SJames Smart 		dev_info(ctrl->ctrl.device,
2880834d3710SJames Smart 			"reconnect: revising io queue count from %d to %d\n",
2881834d3710SJames Smart 			prior_ioq_cnt, nr_io_queues);
2882cda5fd1aSSagi Grimberg 	blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2883cda5fd1aSSagi Grimberg 
288461bff8efSJames Smart 	return 0;
288561bff8efSJames Smart 
288661bff8efSJames Smart out_delete_hw_queues:
288761bff8efSJames Smart 	nvme_fc_delete_hw_io_queues(ctrl);
288861bff8efSJames Smart out_free_io_queues:
288961bff8efSJames Smart 	nvme_fc_free_io_queues(ctrl);
289061bff8efSJames Smart 	return ret;
289161bff8efSJames Smart }
289261bff8efSJames Smart 
2893158bfb88SJames Smart static void
2894158bfb88SJames Smart nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
2895158bfb88SJames Smart {
2896158bfb88SJames Smart 	struct nvme_fc_lport *lport = rport->lport;
2897158bfb88SJames Smart 
2898158bfb88SJames Smart 	atomic_inc(&lport->act_rport_cnt);
2899158bfb88SJames Smart }
2900158bfb88SJames Smart 
2901158bfb88SJames Smart static void
2902158bfb88SJames Smart nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
2903158bfb88SJames Smart {
2904158bfb88SJames Smart 	struct nvme_fc_lport *lport = rport->lport;
2905158bfb88SJames Smart 	u32 cnt;
2906158bfb88SJames Smart 
2907158bfb88SJames Smart 	cnt = atomic_dec_return(&lport->act_rport_cnt);
2908158bfb88SJames Smart 	if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
2909158bfb88SJames Smart 		lport->ops->localport_delete(&lport->localport);
2910158bfb88SJames Smart }
2911158bfb88SJames Smart 
2912158bfb88SJames Smart static int
2913158bfb88SJames Smart nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
2914158bfb88SJames Smart {
2915158bfb88SJames Smart 	struct nvme_fc_rport *rport = ctrl->rport;
2916158bfb88SJames Smart 	u32 cnt;
2917158bfb88SJames Smart 
2918eb4ee8f1SJames Smart 	if (test_and_set_bit(ASSOC_ACTIVE, &ctrl->flags))
2919158bfb88SJames Smart 		return 1;
2920158bfb88SJames Smart 
2921158bfb88SJames Smart 	cnt = atomic_inc_return(&rport->act_ctrl_cnt);
2922158bfb88SJames Smart 	if (cnt == 1)
2923158bfb88SJames Smart 		nvme_fc_rport_active_on_lport(rport);
2924158bfb88SJames Smart 
2925158bfb88SJames Smart 	return 0;
2926158bfb88SJames Smart }
2927158bfb88SJames Smart 
2928158bfb88SJames Smart static int
2929158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
2930158bfb88SJames Smart {
2931158bfb88SJames Smart 	struct nvme_fc_rport *rport = ctrl->rport;
2932158bfb88SJames Smart 	struct nvme_fc_lport *lport = rport->lport;
2933158bfb88SJames Smart 	u32 cnt;
2934158bfb88SJames Smart 
2935eb4ee8f1SJames Smart 	/* clearing of ctrl->flags ASSOC_ACTIVE bit is in association delete */
2936158bfb88SJames Smart 
2937158bfb88SJames Smart 	cnt = atomic_dec_return(&rport->act_ctrl_cnt);
2938158bfb88SJames Smart 	if (cnt == 0) {
2939158bfb88SJames Smart 		if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
2940158bfb88SJames Smart 			lport->ops->remoteport_delete(&rport->remoteport);
2941158bfb88SJames Smart 		nvme_fc_rport_inactive_on_lport(rport);
2942158bfb88SJames Smart 	}
2943158bfb88SJames Smart 
2944158bfb88SJames Smart 	return 0;
2945158bfb88SJames Smart }
2946158bfb88SJames Smart 
294761bff8efSJames Smart /*
294861bff8efSJames Smart  * This routine restarts the controller on the host side, and
294961bff8efSJames Smart  * on the link side, recreates the controller association.
295061bff8efSJames Smart  */
295161bff8efSJames Smart static int
295261bff8efSJames Smart nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
295361bff8efSJames Smart {
295461bff8efSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
295514fd1e98SJames Smart 	struct nvmefc_ls_rcv_op *disls = NULL;
295614fd1e98SJames Smart 	unsigned long flags;
295761bff8efSJames Smart 	int ret;
295861bff8efSJames Smart 	bool changed;
295961bff8efSJames Smart 
2960fdf9dfa8SSagi Grimberg 	++ctrl->ctrl.nr_reconnects;
296161bff8efSJames Smart 
296296e24801SJames Smart 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
296396e24801SJames Smart 		return -ENODEV;
296496e24801SJames Smart 
2965158bfb88SJames Smart 	if (nvme_fc_ctlr_active_on_rport(ctrl))
2966158bfb88SJames Smart 		return -ENOTUNIQ;
2967158bfb88SJames Smart 
29684bea364fSJames Smart 	dev_info(ctrl->ctrl.device,
29694bea364fSJames Smart 		"NVME-FC{%d}: create association : host wwpn 0x%016llx "
29704bea364fSJames Smart 		" rport wwpn 0x%016llx: NQN \"%s\"\n",
29714bea364fSJames Smart 		ctrl->cnum, ctrl->lport->localport.port_name,
29724bea364fSJames Smart 		ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
29734bea364fSJames Smart 
297461bff8efSJames Smart 	/*
297561bff8efSJames Smart 	 * Create the admin queue
297661bff8efSJames Smart 	 */
297761bff8efSJames Smart 
297861bff8efSJames Smart 	ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
2979d157e534SJames Smart 				NVME_AQ_DEPTH);
298061bff8efSJames Smart 	if (ret)
298161bff8efSJames Smart 		goto out_free_queue;
298261bff8efSJames Smart 
298361bff8efSJames Smart 	ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
2984d157e534SJames Smart 				NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
298561bff8efSJames Smart 	if (ret)
298661bff8efSJames Smart 		goto out_delete_hw_queue;
298761bff8efSJames Smart 
298861bff8efSJames Smart 	ret = nvmf_connect_admin_queue(&ctrl->ctrl);
298961bff8efSJames Smart 	if (ret)
299061bff8efSJames Smart 		goto out_disconnect_admin_queue;
299161bff8efSJames Smart 
29929e0ed16aSSagi Grimberg 	set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
29939e0ed16aSSagi Grimberg 
299461bff8efSJames Smart 	/*
299561bff8efSJames Smart 	 * Check controller capabilities
299661bff8efSJames Smart 	 *
299761bff8efSJames Smart 	 * todo:- add code to check if ctrl attributes changed from
299861bff8efSJames Smart 	 * prior connection values
299961bff8efSJames Smart 	 */
300061bff8efSJames Smart 
3001c0f2f45bSSagi Grimberg 	ret = nvme_enable_ctrl(&ctrl->ctrl);
300261bff8efSJames Smart 	if (ret)
300361bff8efSJames Smart 		goto out_disconnect_admin_queue;
300461bff8efSJames Smart 
300523748076SJames Smart 	ctrl->ctrl.max_segments = ctrl->lport->ops->max_sgl_segments;
300623748076SJames Smart 	ctrl->ctrl.max_hw_sectors = ctrl->ctrl.max_segments <<
300723748076SJames Smart 						(ilog2(SZ_4K) - 9);
300861bff8efSJames Smart 
3009e7832cb4SSagi Grimberg 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
3010e7832cb4SSagi Grimberg 
301161bff8efSJames Smart 	ret = nvme_init_identify(&ctrl->ctrl);
301261bff8efSJames Smart 	if (ret)
301361bff8efSJames Smart 		goto out_disconnect_admin_queue;
301461bff8efSJames Smart 
301561bff8efSJames Smart 	/* sanity checks */
301661bff8efSJames Smart 
301761bff8efSJames Smart 	/* FC-NVME does not have other data in the capsule */
301861bff8efSJames Smart 	if (ctrl->ctrl.icdoff) {
301961bff8efSJames Smart 		dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
302061bff8efSJames Smart 				ctrl->ctrl.icdoff);
302161bff8efSJames Smart 		goto out_disconnect_admin_queue;
302261bff8efSJames Smart 	}
302361bff8efSJames Smart 
302461bff8efSJames Smart 	/* FC-NVME supports normal SGL Data Block Descriptors */
302561bff8efSJames Smart 
302661bff8efSJames Smart 	if (opts->queue_size > ctrl->ctrl.maxcmd) {
302761bff8efSJames Smart 		/* warn if maxcmd is lower than queue_size */
302861bff8efSJames Smart 		dev_warn(ctrl->ctrl.device,
302961bff8efSJames Smart 			"queue_size %zu > ctrl maxcmd %u, reducing "
30307db39484SJames Smart 			"to maxcmd\n",
303161bff8efSJames Smart 			opts->queue_size, ctrl->ctrl.maxcmd);
303261bff8efSJames Smart 		opts->queue_size = ctrl->ctrl.maxcmd;
303361bff8efSJames Smart 	}
303461bff8efSJames Smart 
3035d157e534SJames Smart 	if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
3036d157e534SJames Smart 		/* warn if sqsize is lower than queue_size */
3037d157e534SJames Smart 		dev_warn(ctrl->ctrl.device,
30387db39484SJames Smart 			"queue_size %zu > ctrl sqsize %u, reducing "
30397db39484SJames Smart 			"to sqsize\n",
3040d157e534SJames Smart 			opts->queue_size, ctrl->ctrl.sqsize + 1);
3041d157e534SJames Smart 		opts->queue_size = ctrl->ctrl.sqsize + 1;
3042d157e534SJames Smart 	}
3043d157e534SJames Smart 
304461bff8efSJames Smart 	ret = nvme_fc_init_aen_ops(ctrl);
304561bff8efSJames Smart 	if (ret)
304661bff8efSJames Smart 		goto out_term_aen_ops;
304761bff8efSJames Smart 
304861bff8efSJames Smart 	/*
304961bff8efSJames Smart 	 * Create the io queues
305061bff8efSJames Smart 	 */
305161bff8efSJames Smart 
3052d858e5f0SSagi Grimberg 	if (ctrl->ctrl.queue_count > 1) {
30534c984154SJames Smart 		if (!ctrl->ioq_live)
305461bff8efSJames Smart 			ret = nvme_fc_create_io_queues(ctrl);
305561bff8efSJames Smart 		else
30563e493c00SJames Smart 			ret = nvme_fc_recreate_io_queues(ctrl);
305761bff8efSJames Smart 		if (ret)
305861bff8efSJames Smart 			goto out_term_aen_ops;
305961bff8efSJames Smart 	}
306061bff8efSJames Smart 
306161bff8efSJames Smart 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
306261bff8efSJames Smart 
3063fdf9dfa8SSagi Grimberg 	ctrl->ctrl.nr_reconnects = 0;
306461bff8efSJames Smart 
306544c6ec77SJames Smart 	if (changed)
3066d09f2b45SSagi Grimberg 		nvme_start_ctrl(&ctrl->ctrl);
306761bff8efSJames Smart 
306861bff8efSJames Smart 	return 0;	/* Success */
306961bff8efSJames Smart 
307061bff8efSJames Smart out_term_aen_ops:
307161bff8efSJames Smart 	nvme_fc_term_aen_ops(ctrl);
307261bff8efSJames Smart out_disconnect_admin_queue:
307361bff8efSJames Smart 	/* send a Disconnect(association) LS to fc-nvme target */
307461bff8efSJames Smart 	nvme_fc_xmt_disconnect_assoc(ctrl);
307514fd1e98SJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
3076bcde5f0fSJames Smart 	ctrl->association_id = 0;
307714fd1e98SJames Smart 	disls = ctrl->rcv_disconn;
307814fd1e98SJames Smart 	ctrl->rcv_disconn = NULL;
307914fd1e98SJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
308014fd1e98SJames Smart 	if (disls)
308114fd1e98SJames Smart 		nvme_fc_xmt_ls_rsp(disls);
308261bff8efSJames Smart out_delete_hw_queue:
308361bff8efSJames Smart 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
308461bff8efSJames Smart out_free_queue:
308561bff8efSJames Smart 	nvme_fc_free_queue(&ctrl->queues[0]);
3086eb4ee8f1SJames Smart 	clear_bit(ASSOC_ACTIVE, &ctrl->flags);
3087158bfb88SJames Smart 	nvme_fc_ctlr_inactive_on_rport(ctrl);
308861bff8efSJames Smart 
308961bff8efSJames Smart 	return ret;
309061bff8efSJames Smart }
309161bff8efSJames Smart 
309261bff8efSJames Smart /*
309361bff8efSJames Smart  * This routine stops operation of the controller on the host side.
309461bff8efSJames Smart  * On the host os stack side: Admin and IO queues are stopped,
309561bff8efSJames Smart  *   outstanding ios on them terminated via FC ABTS.
309661bff8efSJames Smart  * On the link side: the association is terminated.
309761bff8efSJames Smart  */
309861bff8efSJames Smart static void
309961bff8efSJames Smart nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
310061bff8efSJames Smart {
310114fd1e98SJames Smart 	struct nvmefc_ls_rcv_op *disls = NULL;
310261bff8efSJames Smart 	unsigned long flags;
310361bff8efSJames Smart 
3104eb4ee8f1SJames Smart 	if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
3105158bfb88SJames Smart 		return;
3106158bfb88SJames Smart 
310761bff8efSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
3108eb4ee8f1SJames Smart 	set_bit(FCCTRL_TERMIO, &ctrl->flags);
310961bff8efSJames Smart 	ctrl->iocnt = 0;
311061bff8efSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
311161bff8efSJames Smart 
311261bff8efSJames Smart 	/*
311361bff8efSJames Smart 	 * If io queues are present, stop them and terminate all outstanding
311461bff8efSJames Smart 	 * ios on them. As FC allocates FC exchange for each io, the
311561bff8efSJames Smart 	 * transport must contact the LLDD to terminate the exchange,
311661bff8efSJames Smart 	 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
311761bff8efSJames Smart 	 * to tell us what io's are busy and invoke a transport routine
311861bff8efSJames Smart 	 * to kill them with the LLDD.  After terminating the exchange
311961bff8efSJames Smart 	 * the LLDD will call the transport's normal io done path, but it
312061bff8efSJames Smart 	 * will have an aborted status. The done path will return the
312161bff8efSJames Smart 	 * io requests back to the block layer as part of normal completions
312261bff8efSJames Smart 	 * (but with error status).
312361bff8efSJames Smart 	 */
3124d858e5f0SSagi Grimberg 	if (ctrl->ctrl.queue_count > 1) {
312561bff8efSJames Smart 		nvme_stop_queues(&ctrl->ctrl);
312661bff8efSJames Smart 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
312761bff8efSJames Smart 				nvme_fc_terminate_exchange, &ctrl->ctrl);
3128622b8b68SMing Lei 		blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
312961bff8efSJames Smart 	}
313061bff8efSJames Smart 
313161bff8efSJames Smart 	/*
313261bff8efSJames Smart 	 * Other transports, which don't have link-level contexts bound
313361bff8efSJames Smart 	 * to sqe's, would try to gracefully shutdown the controller by
313461bff8efSJames Smart 	 * writing the registers for shutdown and polling (call
313561bff8efSJames Smart 	 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
313661bff8efSJames Smart 	 * just aborted and we will wait on those contexts, and given
313761bff8efSJames Smart 	 * there was no indication of how live the controlelr is on the
313861bff8efSJames Smart 	 * link, don't send more io to create more contexts for the
313961bff8efSJames Smart 	 * shutdown. Let the controller fail via keepalive failure if
314061bff8efSJames Smart 	 * its still present.
314161bff8efSJames Smart 	 */
314261bff8efSJames Smart 
314361bff8efSJames Smart 	/*
314461bff8efSJames Smart 	 * clean up the admin queue. Same thing as above.
314561bff8efSJames Smart 	 * use blk_mq_tagset_busy_itr() and the transport routine to
314661bff8efSJames Smart 	 * terminate the exchanges.
314761bff8efSJames Smart 	 */
3148f9c5af5fSSagi Grimberg 	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
314961bff8efSJames Smart 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
315061bff8efSJames Smart 				nvme_fc_terminate_exchange, &ctrl->ctrl);
3151622b8b68SMing Lei 	blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
315261bff8efSJames Smart 
315361bff8efSJames Smart 	/* kill the aens as they are a separate path */
315461bff8efSJames Smart 	nvme_fc_abort_aen_ops(ctrl);
315561bff8efSJames Smart 
315661bff8efSJames Smart 	/* wait for all io that had to be aborted */
31578a82dbf1SJames Smart 	spin_lock_irq(&ctrl->lock);
315836715cf4SJames Smart 	wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
3159eb4ee8f1SJames Smart 	clear_bit(FCCTRL_TERMIO, &ctrl->flags);
31608a82dbf1SJames Smart 	spin_unlock_irq(&ctrl->lock);
316161bff8efSJames Smart 
316261bff8efSJames Smart 	nvme_fc_term_aen_ops(ctrl);
316361bff8efSJames Smart 
316461bff8efSJames Smart 	/*
316561bff8efSJames Smart 	 * send a Disconnect(association) LS to fc-nvme target
316661bff8efSJames Smart 	 * Note: could have been sent at top of process, but
316761bff8efSJames Smart 	 * cleaner on link traffic if after the aborts complete.
316861bff8efSJames Smart 	 * Note: if association doesn't exist, association_id will be 0
316961bff8efSJames Smart 	 */
317061bff8efSJames Smart 	if (ctrl->association_id)
317161bff8efSJames Smart 		nvme_fc_xmt_disconnect_assoc(ctrl);
317261bff8efSJames Smart 
317314fd1e98SJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
3174bcde5f0fSJames Smart 	ctrl->association_id = 0;
317514fd1e98SJames Smart 	disls = ctrl->rcv_disconn;
317614fd1e98SJames Smart 	ctrl->rcv_disconn = NULL;
317714fd1e98SJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
317814fd1e98SJames Smart 	if (disls)
317914fd1e98SJames Smart 		/*
318014fd1e98SJames Smart 		 * if a Disconnect Request was waiting for a response, send
318114fd1e98SJames Smart 		 * now that all ABTS's have been issued (and are complete).
318214fd1e98SJames Smart 		 */
318314fd1e98SJames Smart 		nvme_fc_xmt_ls_rsp(disls);
3184bcde5f0fSJames Smart 
318561bff8efSJames Smart 	if (ctrl->ctrl.tagset) {
318661bff8efSJames Smart 		nvme_fc_delete_hw_io_queues(ctrl);
318761bff8efSJames Smart 		nvme_fc_free_io_queues(ctrl);
318861bff8efSJames Smart 	}
318961bff8efSJames Smart 
319061bff8efSJames Smart 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
319161bff8efSJames Smart 	nvme_fc_free_queue(&ctrl->queues[0]);
3192158bfb88SJames Smart 
3193d625d05eSJames Smart 	/* re-enable the admin_q so anything new can fast fail */
3194d625d05eSJames Smart 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
3195d625d05eSJames Smart 
319602d62a8bSJames Smart 	/* resume the io queues so that things will fast fail */
319702d62a8bSJames Smart 	nvme_start_queues(&ctrl->ctrl);
319802d62a8bSJames Smart 
3199158bfb88SJames Smart 	nvme_fc_ctlr_inactive_on_rport(ctrl);
320061bff8efSJames Smart }
320161bff8efSJames Smart 
320261bff8efSJames Smart static void
3203c5017e85SChristoph Hellwig nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
320461bff8efSJames Smart {
3205c5017e85SChristoph Hellwig 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
320661bff8efSJames Smart 
32074cff280aSJames Smart 	cancel_work_sync(&ctrl->err_work);
320861bff8efSJames Smart 	cancel_delayed_work_sync(&ctrl->connect_work);
320961bff8efSJames Smart 	/*
321061bff8efSJames Smart 	 * kill the association on the link side.  this will block
321161bff8efSJames Smart 	 * waiting for io to terminate
321261bff8efSJames Smart 	 */
321361bff8efSJames Smart 	nvme_fc_delete_association(ctrl);
321461bff8efSJames Smart }
321561bff8efSJames Smart 
321661bff8efSJames Smart static void
32175bbecdbcSJames Smart nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
32185bbecdbcSJames Smart {
32192b632970SJames Smart 	struct nvme_fc_rport *rport = ctrl->rport;
32202b632970SJames Smart 	struct nvme_fc_remote_port *portptr = &rport->remoteport;
32212b632970SJames Smart 	unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
32222b632970SJames Smart 	bool recon = true;
32235bbecdbcSJames Smart 
3224ad6a0a52SMax Gurtovoy 	if (ctrl->ctrl.state != NVME_CTRL_CONNECTING)
32255bbecdbcSJames Smart 		return;
32265bbecdbcSJames Smart 
32272b632970SJames Smart 	if (portptr->port_state == FC_OBJSTATE_ONLINE)
3228589ff775SJames Smart 		dev_info(ctrl->ctrl.device,
32295bbecdbcSJames Smart 			"NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
32305bbecdbcSJames Smart 			ctrl->cnum, status);
32312b632970SJames Smart 	else if (time_after_eq(jiffies, rport->dev_loss_end))
32322b632970SJames Smart 		recon = false;
32335bbecdbcSJames Smart 
32342b632970SJames Smart 	if (recon && nvmf_should_reconnect(&ctrl->ctrl)) {
32352b632970SJames Smart 		if (portptr->port_state == FC_OBJSTATE_ONLINE)
32365bbecdbcSJames Smart 			dev_info(ctrl->ctrl.device,
32372b632970SJames Smart 				"NVME-FC{%d}: Reconnect attempt in %ld "
32382b632970SJames Smart 				"seconds\n",
32392b632970SJames Smart 				ctrl->cnum, recon_delay / HZ);
32402b632970SJames Smart 		else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
32412b632970SJames Smart 			recon_delay = rport->dev_loss_end - jiffies;
32422b632970SJames Smart 
32432b632970SJames Smart 		queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
32445bbecdbcSJames Smart 	} else {
32452b632970SJames Smart 		if (portptr->port_state == FC_OBJSTATE_ONLINE)
3246589ff775SJames Smart 			dev_warn(ctrl->ctrl.device,
32475bbecdbcSJames Smart 				"NVME-FC{%d}: Max reconnect attempts (%d) "
324877d0612dSMax Gurtovoy 				"reached.\n",
3249fdf9dfa8SSagi Grimberg 				ctrl->cnum, ctrl->ctrl.nr_reconnects);
32502b632970SJames Smart 		else
32512b632970SJames Smart 			dev_warn(ctrl->ctrl.device,
32522b632970SJames Smart 				"NVME-FC{%d}: dev_loss_tmo (%d) expired "
325377d0612dSMax Gurtovoy 				"while waiting for remoteport connectivity.\n",
3254614fc1c0SMartin George 				ctrl->cnum, min_t(int, portptr->dev_loss_tmo,
3255614fc1c0SMartin George 					(ctrl->ctrl.opts->max_reconnects *
3256614fc1c0SMartin George 					 ctrl->ctrl.opts->reconnect_delay)));
3257c5017e85SChristoph Hellwig 		WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
32585bbecdbcSJames Smart 	}
32595bbecdbcSJames Smart }
32605bbecdbcSJames Smart 
32615bbecdbcSJames Smart static void
32624cff280aSJames Smart __nvme_fc_terminate_io(struct nvme_fc_ctrl *ctrl)
32634cff280aSJames Smart {
3264c869e494SJames Smart 	/*
3265c869e494SJames Smart 	 * if state is connecting - the error occurred as part of a
3266c869e494SJames Smart 	 * reconnect attempt. The create_association error paths will
3267c869e494SJames Smart 	 * clean up any outstanding io.
3268c869e494SJames Smart 	 *
3269c869e494SJames Smart 	 * if it's a different state - ensure all pending io is
3270c869e494SJames Smart 	 * terminated. Given this can delay while waiting for the
3271c869e494SJames Smart 	 * aborted io to return, we recheck adapter state below
3272c869e494SJames Smart 	 * before changing state.
3273c869e494SJames Smart 	 */
3274c869e494SJames Smart 	if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
32754cff280aSJames Smart 		nvme_stop_keep_alive(&ctrl->ctrl);
32764cff280aSJames Smart 
32774cff280aSJames Smart 		/* will block will waiting for io to terminate */
32784cff280aSJames Smart 		nvme_fc_delete_association(ctrl);
3279c869e494SJames Smart 	}
32804cff280aSJames Smart 
32814cff280aSJames Smart 	if (ctrl->ctrl.state != NVME_CTRL_CONNECTING &&
32824cff280aSJames Smart 	    !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
32834cff280aSJames Smart 		dev_err(ctrl->ctrl.device,
32844cff280aSJames Smart 			"NVME-FC{%d}: error_recovery: Couldn't change state "
32854cff280aSJames Smart 			"to CONNECTING\n", ctrl->cnum);
32864cff280aSJames Smart }
32874cff280aSJames Smart 
32884cff280aSJames Smart static void
328961bff8efSJames Smart nvme_fc_reset_ctrl_work(struct work_struct *work)
329061bff8efSJames Smart {
329161bff8efSJames Smart 	struct nvme_fc_ctrl *ctrl =
3292d86c4d8eSChristoph Hellwig 		container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
329361bff8efSJames Smart 	int ret;
329461bff8efSJames Smart 
32954cff280aSJames Smart 	__nvme_fc_terminate_io(ctrl);
32964cff280aSJames Smart 
3297d09f2b45SSagi Grimberg 	nvme_stop_ctrl(&ctrl->ctrl);
329844c6ec77SJames Smart 
32992b632970SJames Smart 	if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE)
330061bff8efSJames Smart 		ret = nvme_fc_create_association(ctrl);
33012b632970SJames Smart 	else
33022b632970SJames Smart 		ret = -ENOTCONN;
33032b632970SJames Smart 
33045bbecdbcSJames Smart 	if (ret)
33055bbecdbcSJames Smart 		nvme_fc_reconnect_or_delete(ctrl, ret);
33065bbecdbcSJames Smart 	else
330761bff8efSJames Smart 		dev_info(ctrl->ctrl.device,
330896e24801SJames Smart 			"NVME-FC{%d}: controller reset complete\n",
330996e24801SJames Smart 			ctrl->cnum);
331061bff8efSJames Smart }
331161bff8efSJames Smart 
33124cff280aSJames Smart static void
33134cff280aSJames Smart nvme_fc_connect_err_work(struct work_struct *work)
33144cff280aSJames Smart {
33154cff280aSJames Smart 	struct nvme_fc_ctrl *ctrl =
33164cff280aSJames Smart 			container_of(work, struct nvme_fc_ctrl, err_work);
33174cff280aSJames Smart 
33184cff280aSJames Smart 	__nvme_fc_terminate_io(ctrl);
33194cff280aSJames Smart 
33204cff280aSJames Smart 	atomic_set(&ctrl->err_work_active, 0);
33214cff280aSJames Smart 
33224cff280aSJames Smart 	/*
33234cff280aSJames Smart 	 * Rescheduling the connection after recovering
33244cff280aSJames Smart 	 * from the io error is left to the reconnect work
33254cff280aSJames Smart 	 * item, which is what should have stalled waiting on
33264cff280aSJames Smart 	 * the io that had the error that scheduled this work.
33274cff280aSJames Smart 	 */
33284cff280aSJames Smart }
33294cff280aSJames Smart 
333061bff8efSJames Smart static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
333161bff8efSJames Smart 	.name			= "fc",
333261bff8efSJames Smart 	.module			= THIS_MODULE,
3333d3d5b87dSChristoph Hellwig 	.flags			= NVME_F_FABRICS,
333461bff8efSJames Smart 	.reg_read32		= nvmf_reg_read32,
333561bff8efSJames Smart 	.reg_read64		= nvmf_reg_read64,
333661bff8efSJames Smart 	.reg_write32		= nvmf_reg_write32,
333761bff8efSJames Smart 	.free_ctrl		= nvme_fc_nvme_ctrl_freed,
333861bff8efSJames Smart 	.submit_async_event	= nvme_fc_submit_async_event,
3339c5017e85SChristoph Hellwig 	.delete_ctrl		= nvme_fc_delete_ctrl,
334061bff8efSJames Smart 	.get_address		= nvmf_get_address,
334161bff8efSJames Smart };
334261bff8efSJames Smart 
334361bff8efSJames Smart static void
334461bff8efSJames Smart nvme_fc_connect_ctrl_work(struct work_struct *work)
334561bff8efSJames Smart {
334661bff8efSJames Smart 	int ret;
334761bff8efSJames Smart 
334861bff8efSJames Smart 	struct nvme_fc_ctrl *ctrl =
334961bff8efSJames Smart 			container_of(to_delayed_work(work),
335061bff8efSJames Smart 				struct nvme_fc_ctrl, connect_work);
335161bff8efSJames Smart 
335261bff8efSJames Smart 	ret = nvme_fc_create_association(ctrl);
33535bbecdbcSJames Smart 	if (ret)
33545bbecdbcSJames Smart 		nvme_fc_reconnect_or_delete(ctrl, ret);
33555bbecdbcSJames Smart 	else
335661bff8efSJames Smart 		dev_info(ctrl->ctrl.device,
33574c984154SJames Smart 			"NVME-FC{%d}: controller connect complete\n",
335861bff8efSJames Smart 			ctrl->cnum);
335961bff8efSJames Smart }
336061bff8efSJames Smart 
336161bff8efSJames Smart 
336261bff8efSJames Smart static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
336361bff8efSJames Smart 	.queue_rq	= nvme_fc_queue_rq,
336461bff8efSJames Smart 	.complete	= nvme_fc_complete_rq,
336576f983cbSChristoph Hellwig 	.init_request	= nvme_fc_init_request,
336661bff8efSJames Smart 	.exit_request	= nvme_fc_exit_request,
336761bff8efSJames Smart 	.init_hctx	= nvme_fc_init_admin_hctx,
336861bff8efSJames Smart 	.timeout	= nvme_fc_timeout,
336961bff8efSJames Smart };
337061bff8efSJames Smart 
3371e399441dSJames Smart 
337256d5f4f1SJames Smart /*
337356d5f4f1SJames Smart  * Fails a controller request if it matches an existing controller
337456d5f4f1SJames Smart  * (association) with the same tuple:
337556d5f4f1SJames Smart  * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
337656d5f4f1SJames Smart  *
337756d5f4f1SJames Smart  * The ports don't need to be compared as they are intrinsically
337856d5f4f1SJames Smart  * already matched by the port pointers supplied.
337956d5f4f1SJames Smart  */
338056d5f4f1SJames Smart static bool
338156d5f4f1SJames Smart nvme_fc_existing_controller(struct nvme_fc_rport *rport,
338256d5f4f1SJames Smart 		struct nvmf_ctrl_options *opts)
338356d5f4f1SJames Smart {
338456d5f4f1SJames Smart 	struct nvme_fc_ctrl *ctrl;
338556d5f4f1SJames Smart 	unsigned long flags;
338656d5f4f1SJames Smart 	bool found = false;
338756d5f4f1SJames Smart 
338856d5f4f1SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
338956d5f4f1SJames Smart 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
339056d5f4f1SJames Smart 		found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
339156d5f4f1SJames Smart 		if (found)
339256d5f4f1SJames Smart 			break;
339356d5f4f1SJames Smart 	}
339456d5f4f1SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
339556d5f4f1SJames Smart 
339656d5f4f1SJames Smart 	return found;
339756d5f4f1SJames Smart }
339856d5f4f1SJames Smart 
3399e399441dSJames Smart static struct nvme_ctrl *
340061bff8efSJames Smart nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3401e399441dSJames Smart 	struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3402e399441dSJames Smart {
3403e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl;
3404e399441dSJames Smart 	unsigned long flags;
34054c984154SJames Smart 	int ret, idx;
3406e399441dSJames Smart 
340785e6a6adSJames Smart 	if (!(rport->remoteport.port_role &
340885e6a6adSJames Smart 	    (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
340985e6a6adSJames Smart 		ret = -EBADR;
341085e6a6adSJames Smart 		goto out_fail;
341185e6a6adSJames Smart 	}
341285e6a6adSJames Smart 
341356d5f4f1SJames Smart 	if (!opts->duplicate_connect &&
341456d5f4f1SJames Smart 	    nvme_fc_existing_controller(rport, opts)) {
341556d5f4f1SJames Smart 		ret = -EALREADY;
341656d5f4f1SJames Smart 		goto out_fail;
341756d5f4f1SJames Smart 	}
341856d5f4f1SJames Smart 
3419e399441dSJames Smart 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3420e399441dSJames Smart 	if (!ctrl) {
3421e399441dSJames Smart 		ret = -ENOMEM;
3422e399441dSJames Smart 		goto out_fail;
3423e399441dSJames Smart 	}
3424e399441dSJames Smart 
3425e399441dSJames Smart 	idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
3426e399441dSJames Smart 	if (idx < 0) {
3427e399441dSJames Smart 		ret = -ENOSPC;
34288c5c6605SJames Smart 		goto out_free_ctrl;
3429e399441dSJames Smart 	}
3430e399441dSJames Smart 
3431e399441dSJames Smart 	ctrl->ctrl.opts = opts;
34324c984154SJames Smart 	ctrl->ctrl.nr_reconnects = 0;
343306f3d71eSJames Smart 	if (lport->dev)
3434103e515eSHannes Reinecke 		ctrl->ctrl.numa_node = dev_to_node(lport->dev);
343506f3d71eSJames Smart 	else
343606f3d71eSJames Smart 		ctrl->ctrl.numa_node = NUMA_NO_NODE;
3437e399441dSJames Smart 	INIT_LIST_HEAD(&ctrl->ctrl_list);
3438e399441dSJames Smart 	ctrl->lport = lport;
3439e399441dSJames Smart 	ctrl->rport = rport;
3440e399441dSJames Smart 	ctrl->dev = lport->dev;
3441e399441dSJames Smart 	ctrl->cnum = idx;
34424c984154SJames Smart 	ctrl->ioq_live = false;
34434cff280aSJames Smart 	atomic_set(&ctrl->err_work_active, 0);
34448a82dbf1SJames Smart 	init_waitqueue_head(&ctrl->ioabort_wait);
3445e399441dSJames Smart 
3446e399441dSJames Smart 	get_device(ctrl->dev);
3447e399441dSJames Smart 	kref_init(&ctrl->ref);
3448e399441dSJames Smart 
3449d86c4d8eSChristoph Hellwig 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
345061bff8efSJames Smart 	INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
34514cff280aSJames Smart 	INIT_WORK(&ctrl->err_work, nvme_fc_connect_err_work);
3452e399441dSJames Smart 	spin_lock_init(&ctrl->lock);
3453e399441dSJames Smart 
3454e399441dSJames Smart 	/* io queue count */
3455d858e5f0SSagi Grimberg 	ctrl->ctrl.queue_count = min_t(unsigned int,
3456e399441dSJames Smart 				opts->nr_io_queues,
3457e399441dSJames Smart 				lport->ops->max_hw_queues);
3458d858e5f0SSagi Grimberg 	ctrl->ctrl.queue_count++;	/* +1 for admin queue */
3459e399441dSJames Smart 
3460e399441dSJames Smart 	ctrl->ctrl.sqsize = opts->queue_size - 1;
3461e399441dSJames Smart 	ctrl->ctrl.kato = opts->kato;
34624c984154SJames Smart 	ctrl->ctrl.cntlid = 0xffff;
3463e399441dSJames Smart 
3464e399441dSJames Smart 	ret = -ENOMEM;
3465d858e5f0SSagi Grimberg 	ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3466d858e5f0SSagi Grimberg 				sizeof(struct nvme_fc_queue), GFP_KERNEL);
3467e399441dSJames Smart 	if (!ctrl->queues)
346861bff8efSJames Smart 		goto out_free_ida;
3469e399441dSJames Smart 
34703e493c00SJames Smart 	nvme_fc_init_queue(ctrl, 0);
34713e493c00SJames Smart 
347261bff8efSJames Smart 	memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
347361bff8efSJames Smart 	ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
347438dabe21SKeith Busch 	ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
347561bff8efSJames Smart 	ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
3476103e515eSHannes Reinecke 	ctrl->admin_tag_set.numa_node = ctrl->ctrl.numa_node;
3477d3d0bc78SBart Van Assche 	ctrl->admin_tag_set.cmd_size =
3478d3d0bc78SBart Van Assche 		struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv,
3479d3d0bc78SBart Van Assche 			    ctrl->lport->ops->fcprqst_priv_sz);
348061bff8efSJames Smart 	ctrl->admin_tag_set.driver_data = ctrl;
348161bff8efSJames Smart 	ctrl->admin_tag_set.nr_hw_queues = 1;
348261bff8efSJames Smart 	ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
34835a22e2bfSIsrael Rukshin 	ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
348461bff8efSJames Smart 
348561bff8efSJames Smart 	ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
3486e399441dSJames Smart 	if (ret)
348761bff8efSJames Smart 		goto out_free_queues;
348834b6c231SSagi Grimberg 	ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
3489e399441dSJames Smart 
3490e7832cb4SSagi Grimberg 	ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
3491e7832cb4SSagi Grimberg 	if (IS_ERR(ctrl->ctrl.fabrics_q)) {
3492e7832cb4SSagi Grimberg 		ret = PTR_ERR(ctrl->ctrl.fabrics_q);
3493e7832cb4SSagi Grimberg 		goto out_free_admin_tag_set;
3494e7832cb4SSagi Grimberg 	}
3495e7832cb4SSagi Grimberg 
349661bff8efSJames Smart 	ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
349761bff8efSJames Smart 	if (IS_ERR(ctrl->ctrl.admin_q)) {
349861bff8efSJames Smart 		ret = PTR_ERR(ctrl->ctrl.admin_q);
3499e7832cb4SSagi Grimberg 		goto out_cleanup_fabrics_q;
3500e399441dSJames Smart 	}
3501e399441dSJames Smart 
350261bff8efSJames Smart 	/*
350361bff8efSJames Smart 	 * Would have been nice to init io queues tag set as well.
350461bff8efSJames Smart 	 * However, we require interaction from the controller
350561bff8efSJames Smart 	 * for max io queue count before we can do so.
350661bff8efSJames Smart 	 * Defer this to the connect path.
350761bff8efSJames Smart 	 */
3508e399441dSJames Smart 
350961bff8efSJames Smart 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3510e399441dSJames Smart 	if (ret)
351161bff8efSJames Smart 		goto out_cleanup_admin_q;
3512e399441dSJames Smart 
351361bff8efSJames Smart 	/* at this point, teardown path changes to ref counting on nvme ctrl */
3514e399441dSJames Smart 
3515e399441dSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
3516e399441dSJames Smart 	list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3517e399441dSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
3518e399441dSJames Smart 
35194c984154SJames Smart 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING) ||
35204c984154SJames Smart 	    !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
35214c984154SJames Smart 		dev_err(ctrl->ctrl.device,
35224c984154SJames Smart 			"NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
35234c984154SJames Smart 		goto fail_ctrl;
352417c4dc6eSJames Smart 	}
352517c4dc6eSJames Smart 
35264c984154SJames Smart 	if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
35274c984154SJames Smart 		dev_err(ctrl->ctrl.device,
35284c984154SJames Smart 			"NVME-FC{%d}: failed to schedule initial connect\n",
35294c984154SJames Smart 			ctrl->cnum);
35304c984154SJames Smart 		goto fail_ctrl;
35314c984154SJames Smart 	}
35324c984154SJames Smart 
35334c984154SJames Smart 	flush_delayed_work(&ctrl->connect_work);
35344c984154SJames Smart 
35354c984154SJames Smart 	dev_info(ctrl->ctrl.device,
35364c984154SJames Smart 		"NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
35374c984154SJames Smart 		ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
35384c984154SJames Smart 
35394c984154SJames Smart 	return &ctrl->ctrl;
35404c984154SJames Smart 
35414c984154SJames Smart fail_ctrl:
3542cf25809bSJames Smart 	nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3543cf25809bSJames Smart 	cancel_work_sync(&ctrl->ctrl.reset_work);
35444cff280aSJames Smart 	cancel_work_sync(&ctrl->err_work);
3545cf25809bSJames Smart 	cancel_delayed_work_sync(&ctrl->connect_work);
3546cf25809bSJames Smart 
3547de41447aSEwan D. Milne 	ctrl->ctrl.opts = NULL;
354817c4dc6eSJames Smart 
354961bff8efSJames Smart 	/* initiate nvme ctrl ref counting teardown */
355061bff8efSJames Smart 	nvme_uninit_ctrl(&ctrl->ctrl);
355161bff8efSJames Smart 
35520b5a7669SJames Smart 	/* Remove core ctrl ref. */
35530b5a7669SJames Smart 	nvme_put_ctrl(&ctrl->ctrl);
35540b5a7669SJames Smart 
355561bff8efSJames Smart 	/* as we're past the point where we transition to the ref
355661bff8efSJames Smart 	 * counting teardown path, if we return a bad pointer here,
355761bff8efSJames Smart 	 * the calling routine, thinking it's prior to the
355861bff8efSJames Smart 	 * transition, will do an rport put. Since the teardown
355961bff8efSJames Smart 	 * path also does a rport put, we do an extra get here to
356061bff8efSJames Smart 	 * so proper order/teardown happens.
356161bff8efSJames Smart 	 */
356261bff8efSJames Smart 	nvme_fc_rport_get(rport);
356361bff8efSJames Smart 
35644c984154SJames Smart 	return ERR_PTR(-EIO);
3565e399441dSJames Smart 
356661bff8efSJames Smart out_cleanup_admin_q:
356761bff8efSJames Smart 	blk_cleanup_queue(ctrl->ctrl.admin_q);
3568e7832cb4SSagi Grimberg out_cleanup_fabrics_q:
3569e7832cb4SSagi Grimberg 	blk_cleanup_queue(ctrl->ctrl.fabrics_q);
357061bff8efSJames Smart out_free_admin_tag_set:
357161bff8efSJames Smart 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
357261bff8efSJames Smart out_free_queues:
357361bff8efSJames Smart 	kfree(ctrl->queues);
3574e399441dSJames Smart out_free_ida:
357561bff8efSJames Smart 	put_device(ctrl->dev);
3576e399441dSJames Smart 	ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
3577e399441dSJames Smart out_free_ctrl:
3578e399441dSJames Smart 	kfree(ctrl);
3579e399441dSJames Smart out_fail:
3580e399441dSJames Smart 	/* exit via here doesn't follow ctlr ref points */
3581e399441dSJames Smart 	return ERR_PTR(ret);
3582e399441dSJames Smart }
3583e399441dSJames Smart 
3584e399441dSJames Smart 
3585e399441dSJames Smart struct nvmet_fc_traddr {
3586e399441dSJames Smart 	u64	nn;
3587e399441dSJames Smart 	u64	pn;
3588e399441dSJames Smart };
3589e399441dSJames Smart 
3590e399441dSJames Smart static int
35919c5358e1SJames Smart __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3592e399441dSJames Smart {
3593e399441dSJames Smart 	u64 token64;
3594e399441dSJames Smart 
35959c5358e1SJames Smart 	if (match_u64(sstr, &token64))
35969c5358e1SJames Smart 		return -EINVAL;
35979c5358e1SJames Smart 	*val = token64;
3598e399441dSJames Smart 
35999c5358e1SJames Smart 	return 0;
3600e399441dSJames Smart }
3601e399441dSJames Smart 
36029c5358e1SJames Smart /*
36039c5358e1SJames Smart  * This routine validates and extracts the WWN's from the TRADDR string.
36049c5358e1SJames Smart  * As kernel parsers need the 0x to determine number base, universally
36059c5358e1SJames Smart  * build string to parse with 0x prefix before parsing name strings.
36069c5358e1SJames Smart  */
36079c5358e1SJames Smart static int
36089c5358e1SJames Smart nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
36099c5358e1SJames Smart {
36109c5358e1SJames Smart 	char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
36119c5358e1SJames Smart 	substring_t wwn = { name, &name[sizeof(name)-1] };
36129c5358e1SJames Smart 	int nnoffset, pnoffset;
36139c5358e1SJames Smart 
3614d4e4230cSMilan P. Gandhi 	/* validate if string is one of the 2 allowed formats */
36159c5358e1SJames Smart 	if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
36169c5358e1SJames Smart 			!strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
36179c5358e1SJames Smart 			!strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
36189c5358e1SJames Smart 				"pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
36199c5358e1SJames Smart 		nnoffset = NVME_FC_TRADDR_OXNNLEN;
36209c5358e1SJames Smart 		pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
36219c5358e1SJames Smart 						NVME_FC_TRADDR_OXNNLEN;
36229c5358e1SJames Smart 	} else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
36239c5358e1SJames Smart 			!strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
36249c5358e1SJames Smart 			!strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
36259c5358e1SJames Smart 				"pn-", NVME_FC_TRADDR_NNLEN))) {
36269c5358e1SJames Smart 		nnoffset = NVME_FC_TRADDR_NNLEN;
36279c5358e1SJames Smart 		pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
36289c5358e1SJames Smart 	} else
36299c5358e1SJames Smart 		goto out_einval;
36309c5358e1SJames Smart 
36319c5358e1SJames Smart 	name[0] = '0';
36329c5358e1SJames Smart 	name[1] = 'x';
36339c5358e1SJames Smart 	name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
36349c5358e1SJames Smart 
36359c5358e1SJames Smart 	memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
36369c5358e1SJames Smart 	if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
36379c5358e1SJames Smart 		goto out_einval;
36389c5358e1SJames Smart 
36399c5358e1SJames Smart 	memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
36409c5358e1SJames Smart 	if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
36419c5358e1SJames Smart 		goto out_einval;
36429c5358e1SJames Smart 
36439c5358e1SJames Smart 	return 0;
36449c5358e1SJames Smart 
36459c5358e1SJames Smart out_einval:
36469c5358e1SJames Smart 	pr_warn("%s: bad traddr string\n", __func__);
36479c5358e1SJames Smart 	return -EINVAL;
3648e399441dSJames Smart }
3649e399441dSJames Smart 
3650e399441dSJames Smart static struct nvme_ctrl *
3651e399441dSJames Smart nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3652e399441dSJames Smart {
3653e399441dSJames Smart 	struct nvme_fc_lport *lport;
3654e399441dSJames Smart 	struct nvme_fc_rport *rport;
365561bff8efSJames Smart 	struct nvme_ctrl *ctrl;
3656e399441dSJames Smart 	struct nvmet_fc_traddr laddr = { 0L, 0L };
3657e399441dSJames Smart 	struct nvmet_fc_traddr raddr = { 0L, 0L };
3658e399441dSJames Smart 	unsigned long flags;
3659e399441dSJames Smart 	int ret;
3660e399441dSJames Smart 
36619c5358e1SJames Smart 	ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3662e399441dSJames Smart 	if (ret || !raddr.nn || !raddr.pn)
3663e399441dSJames Smart 		return ERR_PTR(-EINVAL);
3664e399441dSJames Smart 
36659c5358e1SJames Smart 	ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3666e399441dSJames Smart 	if (ret || !laddr.nn || !laddr.pn)
3667e399441dSJames Smart 		return ERR_PTR(-EINVAL);
3668e399441dSJames Smart 
3669e399441dSJames Smart 	/* find the host and remote ports to connect together */
3670e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
3671e399441dSJames Smart 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3672e399441dSJames Smart 		if (lport->localport.node_name != laddr.nn ||
3673e399441dSJames Smart 		    lport->localport.port_name != laddr.pn)
3674e399441dSJames Smart 			continue;
3675e399441dSJames Smart 
3676e399441dSJames Smart 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
3677e399441dSJames Smart 			if (rport->remoteport.node_name != raddr.nn ||
3678e399441dSJames Smart 			    rport->remoteport.port_name != raddr.pn)
3679e399441dSJames Smart 				continue;
3680e399441dSJames Smart 
3681e399441dSJames Smart 			/* if fail to get reference fall through. Will error */
3682e399441dSJames Smart 			if (!nvme_fc_rport_get(rport))
3683e399441dSJames Smart 				break;
3684e399441dSJames Smart 
3685e399441dSJames Smart 			spin_unlock_irqrestore(&nvme_fc_lock, flags);
3686e399441dSJames Smart 
368761bff8efSJames Smart 			ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
368861bff8efSJames Smart 			if (IS_ERR(ctrl))
368961bff8efSJames Smart 				nvme_fc_rport_put(rport);
369061bff8efSJames Smart 			return ctrl;
3691e399441dSJames Smart 		}
3692e399441dSJames Smart 	}
3693e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
3694e399441dSJames Smart 
36954fb135adSJohannes Thumshirn 	pr_warn("%s: %s - %s combination not found\n",
36964fb135adSJohannes Thumshirn 		__func__, opts->traddr, opts->host_traddr);
3697e399441dSJames Smart 	return ERR_PTR(-ENOENT);
3698e399441dSJames Smart }
3699e399441dSJames Smart 
3700e399441dSJames Smart 
3701e399441dSJames Smart static struct nvmf_transport_ops nvme_fc_transport = {
3702e399441dSJames Smart 	.name		= "fc",
37030de5cd36SRoy Shterman 	.module		= THIS_MODULE,
3704e399441dSJames Smart 	.required_opts	= NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
37055bbecdbcSJames Smart 	.allowed_opts	= NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3706e399441dSJames Smart 	.create_ctrl	= nvme_fc_create_ctrl,
3707e399441dSJames Smart };
3708e399441dSJames Smart 
370997faec53SJames Smart /* Arbitrary successive failures max. With lots of subsystems could be high */
371097faec53SJames Smart #define DISCOVERY_MAX_FAIL	20
371197faec53SJames Smart 
371297faec53SJames Smart static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
371397faec53SJames Smart 		struct device_attribute *attr, const char *buf, size_t count)
371497faec53SJames Smart {
371597faec53SJames Smart 	unsigned long flags;
371697faec53SJames Smart 	LIST_HEAD(local_disc_list);
371797faec53SJames Smart 	struct nvme_fc_lport *lport;
371897faec53SJames Smart 	struct nvme_fc_rport *rport;
371997faec53SJames Smart 	int failcnt = 0;
372097faec53SJames Smart 
372197faec53SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
372297faec53SJames Smart restart:
372397faec53SJames Smart 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
372497faec53SJames Smart 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
372597faec53SJames Smart 			if (!nvme_fc_lport_get(lport))
372697faec53SJames Smart 				continue;
372797faec53SJames Smart 			if (!nvme_fc_rport_get(rport)) {
372897faec53SJames Smart 				/*
372997faec53SJames Smart 				 * This is a temporary condition. Upon restart
373097faec53SJames Smart 				 * this rport will be gone from the list.
373197faec53SJames Smart 				 *
373297faec53SJames Smart 				 * Revert the lport put and retry.  Anything
373397faec53SJames Smart 				 * added to the list already will be skipped (as
373497faec53SJames Smart 				 * they are no longer list_empty).  Loops should
373597faec53SJames Smart 				 * resume at rports that were not yet seen.
373697faec53SJames Smart 				 */
373797faec53SJames Smart 				nvme_fc_lport_put(lport);
373897faec53SJames Smart 
373997faec53SJames Smart 				if (failcnt++ < DISCOVERY_MAX_FAIL)
374097faec53SJames Smart 					goto restart;
374197faec53SJames Smart 
374297faec53SJames Smart 				pr_err("nvme_discovery: too many reference "
374397faec53SJames Smart 				       "failures\n");
374497faec53SJames Smart 				goto process_local_list;
374597faec53SJames Smart 			}
374697faec53SJames Smart 			if (list_empty(&rport->disc_list))
374797faec53SJames Smart 				list_add_tail(&rport->disc_list,
374897faec53SJames Smart 					      &local_disc_list);
374997faec53SJames Smart 		}
375097faec53SJames Smart 	}
375197faec53SJames Smart 
375297faec53SJames Smart process_local_list:
375397faec53SJames Smart 	while (!list_empty(&local_disc_list)) {
375497faec53SJames Smart 		rport = list_first_entry(&local_disc_list,
375597faec53SJames Smart 					 struct nvme_fc_rport, disc_list);
375697faec53SJames Smart 		list_del_init(&rport->disc_list);
375797faec53SJames Smart 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
375897faec53SJames Smart 
375997faec53SJames Smart 		lport = rport->lport;
376097faec53SJames Smart 		/* signal discovery. Won't hurt if it repeats */
376197faec53SJames Smart 		nvme_fc_signal_discovery_scan(lport, rport);
376297faec53SJames Smart 		nvme_fc_rport_put(rport);
376397faec53SJames Smart 		nvme_fc_lport_put(lport);
376497faec53SJames Smart 
376597faec53SJames Smart 		spin_lock_irqsave(&nvme_fc_lock, flags);
376697faec53SJames Smart 	}
376797faec53SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
376897faec53SJames Smart 
376997faec53SJames Smart 	return count;
377097faec53SJames Smart }
377197faec53SJames Smart static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
377297faec53SJames Smart 
377397faec53SJames Smart static struct attribute *nvme_fc_attrs[] = {
377497faec53SJames Smart 	&dev_attr_nvme_discovery.attr,
377597faec53SJames Smart 	NULL
377697faec53SJames Smart };
377797faec53SJames Smart 
377897faec53SJames Smart static struct attribute_group nvme_fc_attr_group = {
377997faec53SJames Smart 	.attrs = nvme_fc_attrs,
378097faec53SJames Smart };
378197faec53SJames Smart 
378297faec53SJames Smart static const struct attribute_group *nvme_fc_attr_groups[] = {
378397faec53SJames Smart 	&nvme_fc_attr_group,
378497faec53SJames Smart 	NULL
378597faec53SJames Smart };
378697faec53SJames Smart 
378797faec53SJames Smart static struct class fc_class = {
378897faec53SJames Smart 	.name = "fc",
378997faec53SJames Smart 	.dev_groups = nvme_fc_attr_groups,
379097faec53SJames Smart 	.owner = THIS_MODULE,
379197faec53SJames Smart };
379297faec53SJames Smart 
3793e399441dSJames Smart static int __init nvme_fc_init_module(void)
3794e399441dSJames Smart {
37955f568556SJames Smart 	int ret;
37965f568556SJames Smart 
37978730c1ddSHannes Reinecke 	nvme_fc_wq = alloc_workqueue("nvme_fc_wq", WQ_MEM_RECLAIM, 0);
37988730c1ddSHannes Reinecke 	if (!nvme_fc_wq)
37998730c1ddSHannes Reinecke 		return -ENOMEM;
38008730c1ddSHannes Reinecke 
38015f568556SJames Smart 	/*
38025f568556SJames Smart 	 * NOTE:
38035f568556SJames Smart 	 * It is expected that in the future the kernel will combine
38045f568556SJames Smart 	 * the FC-isms that are currently under scsi and now being
38055f568556SJames Smart 	 * added to by NVME into a new standalone FC class. The SCSI
38065f568556SJames Smart 	 * and NVME protocols and their devices would be under this
38075f568556SJames Smart 	 * new FC class.
38085f568556SJames Smart 	 *
38095f568556SJames Smart 	 * As we need something to post FC-specific udev events to,
38105f568556SJames Smart 	 * specifically for nvme probe events, start by creating the
38115f568556SJames Smart 	 * new device class.  When the new standalone FC class is
38125f568556SJames Smart 	 * put in place, this code will move to a more generic
38135f568556SJames Smart 	 * location for the class.
38145f568556SJames Smart 	 */
381597faec53SJames Smart 	ret = class_register(&fc_class);
381697faec53SJames Smart 	if (ret) {
38175f568556SJames Smart 		pr_err("couldn't register class fc\n");
38188730c1ddSHannes Reinecke 		goto out_destroy_wq;
38195f568556SJames Smart 	}
38205f568556SJames Smart 
38215f568556SJames Smart 	/*
38225f568556SJames Smart 	 * Create a device for the FC-centric udev events
38235f568556SJames Smart 	 */
382497faec53SJames Smart 	fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
38255f568556SJames Smart 				"fc_udev_device");
38265f568556SJames Smart 	if (IS_ERR(fc_udev_device)) {
38275f568556SJames Smart 		pr_err("couldn't create fc_udev device!\n");
38285f568556SJames Smart 		ret = PTR_ERR(fc_udev_device);
38295f568556SJames Smart 		goto out_destroy_class;
38305f568556SJames Smart 	}
38315f568556SJames Smart 
38325f568556SJames Smart 	ret = nvmf_register_transport(&nvme_fc_transport);
38335f568556SJames Smart 	if (ret)
38345f568556SJames Smart 		goto out_destroy_device;
38355f568556SJames Smart 
38365f568556SJames Smart 	return 0;
38375f568556SJames Smart 
38385f568556SJames Smart out_destroy_device:
383997faec53SJames Smart 	device_destroy(&fc_class, MKDEV(0, 0));
38405f568556SJames Smart out_destroy_class:
384197faec53SJames Smart 	class_unregister(&fc_class);
38428730c1ddSHannes Reinecke out_destroy_wq:
38438730c1ddSHannes Reinecke 	destroy_workqueue(nvme_fc_wq);
38448730c1ddSHannes Reinecke 
38455f568556SJames Smart 	return ret;
3846e399441dSJames Smart }
3847e399441dSJames Smart 
38484c73cbdfSJames Smart static void
38494c73cbdfSJames Smart nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
38504c73cbdfSJames Smart {
38514c73cbdfSJames Smart 	struct nvme_fc_ctrl *ctrl;
38524c73cbdfSJames Smart 
38534c73cbdfSJames Smart 	spin_lock(&rport->lock);
38544c73cbdfSJames Smart 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
38554c73cbdfSJames Smart 		dev_warn(ctrl->ctrl.device,
38564c73cbdfSJames Smart 			"NVME-FC{%d}: transport unloading: deleting ctrl\n",
38574c73cbdfSJames Smart 			ctrl->cnum);
38584c73cbdfSJames Smart 		nvme_delete_ctrl(&ctrl->ctrl);
38594c73cbdfSJames Smart 	}
38604c73cbdfSJames Smart 	spin_unlock(&rport->lock);
38614c73cbdfSJames Smart }
38624c73cbdfSJames Smart 
38634c73cbdfSJames Smart static void
38644c73cbdfSJames Smart nvme_fc_cleanup_for_unload(void)
38654c73cbdfSJames Smart {
38664c73cbdfSJames Smart 	struct nvme_fc_lport *lport;
38674c73cbdfSJames Smart 	struct nvme_fc_rport *rport;
38684c73cbdfSJames Smart 
38694c73cbdfSJames Smart 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
38704c73cbdfSJames Smart 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
38714c73cbdfSJames Smart 			nvme_fc_delete_controllers(rport);
38724c73cbdfSJames Smart 		}
38734c73cbdfSJames Smart 	}
38744c73cbdfSJames Smart }
38754c73cbdfSJames Smart 
3876e399441dSJames Smart static void __exit nvme_fc_exit_module(void)
3877e399441dSJames Smart {
38784c73cbdfSJames Smart 	unsigned long flags;
38794c73cbdfSJames Smart 	bool need_cleanup = false;
38804c73cbdfSJames Smart 
38814c73cbdfSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
38824c73cbdfSJames Smart 	nvme_fc_waiting_to_unload = true;
38834c73cbdfSJames Smart 	if (!list_empty(&nvme_fc_lport_list)) {
38844c73cbdfSJames Smart 		need_cleanup = true;
38854c73cbdfSJames Smart 		nvme_fc_cleanup_for_unload();
38864c73cbdfSJames Smart 	}
38874c73cbdfSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
38884c73cbdfSJames Smart 	if (need_cleanup) {
38894c73cbdfSJames Smart 		pr_info("%s: waiting for ctlr deletes\n", __func__);
38904c73cbdfSJames Smart 		wait_for_completion(&nvme_fc_unload_proceed);
38914c73cbdfSJames Smart 		pr_info("%s: ctrl deletes complete\n", __func__);
38924c73cbdfSJames Smart 	}
3893e399441dSJames Smart 
3894e399441dSJames Smart 	nvmf_unregister_transport(&nvme_fc_transport);
3895e399441dSJames Smart 
3896e399441dSJames Smart 	ida_destroy(&nvme_fc_local_port_cnt);
3897e399441dSJames Smart 	ida_destroy(&nvme_fc_ctrl_cnt);
38985f568556SJames Smart 
389997faec53SJames Smart 	device_destroy(&fc_class, MKDEV(0, 0));
390097faec53SJames Smart 	class_unregister(&fc_class);
39018730c1ddSHannes Reinecke 	destroy_workqueue(nvme_fc_wq);
3902e399441dSJames Smart }
3903e399441dSJames Smart 
3904e399441dSJames Smart module_init(nvme_fc_init_module);
3905e399441dSJames Smart module_exit(nvme_fc_exit_module);
3906e399441dSJames Smart 
3907e399441dSJames Smart MODULE_LICENSE("GPL v2");
3908