xref: /openbmc/linux/drivers/nvme/host/fc.c (revision ac7fe82b)
1e399441dSJames Smart /*
2e399441dSJames Smart  * Copyright (c) 2016 Avago Technologies.  All rights reserved.
3e399441dSJames Smart  *
4e399441dSJames Smart  * This program is free software; you can redistribute it and/or modify
5e399441dSJames Smart  * it under the terms of version 2 of the GNU General Public License as
6e399441dSJames Smart  * published by the Free Software Foundation.
7e399441dSJames Smart  *
8e399441dSJames Smart  * This program is distributed in the hope that it will be useful.
9e399441dSJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
10e399441dSJames Smart  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
11e399441dSJames Smart  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO
12e399441dSJames Smart  * THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
13e399441dSJames Smart  * See the GNU General Public License for more details, a copy of which
14e399441dSJames Smart  * can be found in the file COPYING included with this package
15e399441dSJames Smart  *
16e399441dSJames Smart  */
17e399441dSJames Smart #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18e399441dSJames Smart #include <linux/module.h>
19e399441dSJames Smart #include <linux/parser.h>
20e399441dSJames Smart #include <uapi/scsi/fc/fc_fs.h>
21e399441dSJames Smart #include <uapi/scsi/fc/fc_els.h>
2261bff8efSJames Smart #include <linux/delay.h>
23e399441dSJames Smart 
24e399441dSJames Smart #include "nvme.h"
25e399441dSJames Smart #include "fabrics.h"
26e399441dSJames Smart #include <linux/nvme-fc-driver.h>
27e399441dSJames Smart #include <linux/nvme-fc.h>
28e399441dSJames Smart 
29e399441dSJames Smart 
30e399441dSJames Smart /* *************************** Data Structures/Defines ****************** */
31e399441dSJames Smart 
32e399441dSJames Smart 
33e399441dSJames Smart /*
34e399441dSJames Smart  * We handle AEN commands ourselves and don't even let the
35e399441dSJames Smart  * block layer know about them.
36e399441dSJames Smart  */
37e399441dSJames Smart #define NVME_FC_NR_AEN_COMMANDS	1
38e399441dSJames Smart #define NVME_FC_AQ_BLKMQ_DEPTH	\
397aa1f427SSagi Grimberg 	(NVME_AQ_DEPTH - NVME_FC_NR_AEN_COMMANDS)
40e399441dSJames Smart #define AEN_CMDID_BASE		(NVME_FC_AQ_BLKMQ_DEPTH + 1)
41e399441dSJames Smart 
42e399441dSJames Smart enum nvme_fc_queue_flags {
43e399441dSJames Smart 	NVME_FC_Q_CONNECTED = (1 << 0),
44e399441dSJames Smart };
45e399441dSJames Smart 
46e399441dSJames Smart #define NVMEFC_QUEUE_DELAY	3		/* ms units */
47e399441dSJames Smart 
48ac7fe82bSJames Smart #define NVME_FC_DEFAULT_DEV_LOSS_TMO	60	/* seconds */
49ac7fe82bSJames Smart 
50e399441dSJames Smart struct nvme_fc_queue {
51e399441dSJames Smart 	struct nvme_fc_ctrl	*ctrl;
52e399441dSJames Smart 	struct device		*dev;
53e399441dSJames Smart 	struct blk_mq_hw_ctx	*hctx;
54e399441dSJames Smart 	void			*lldd_handle;
55e399441dSJames Smart 	int			queue_size;
56e399441dSJames Smart 	size_t			cmnd_capsule_len;
57e399441dSJames Smart 	u32			qnum;
58e399441dSJames Smart 	u32			rqcnt;
59e399441dSJames Smart 	u32			seqno;
60e399441dSJames Smart 
61e399441dSJames Smart 	u64			connection_id;
62e399441dSJames Smart 	atomic_t		csn;
63e399441dSJames Smart 
64e399441dSJames Smart 	unsigned long		flags;
65e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
66e399441dSJames Smart 
678d64daf7SJames Smart enum nvme_fcop_flags {
688d64daf7SJames Smart 	FCOP_FLAGS_TERMIO	= (1 << 0),
698d64daf7SJames Smart 	FCOP_FLAGS_RELEASED	= (1 << 1),
708d64daf7SJames Smart 	FCOP_FLAGS_COMPLETE	= (1 << 2),
7178a7ac26SJames Smart 	FCOP_FLAGS_AEN		= (1 << 3),
728d64daf7SJames Smart };
738d64daf7SJames Smart 
74e399441dSJames Smart struct nvmefc_ls_req_op {
75e399441dSJames Smart 	struct nvmefc_ls_req	ls_req;
76e399441dSJames Smart 
77c913a8b0SJames Smart 	struct nvme_fc_rport	*rport;
78e399441dSJames Smart 	struct nvme_fc_queue	*queue;
79e399441dSJames Smart 	struct request		*rq;
808d64daf7SJames Smart 	u32			flags;
81e399441dSJames Smart 
82e399441dSJames Smart 	int			ls_error;
83e399441dSJames Smart 	struct completion	ls_done;
84c913a8b0SJames Smart 	struct list_head	lsreq_list;	/* rport->ls_req_list */
85e399441dSJames Smart 	bool			req_queued;
86e399441dSJames Smart };
87e399441dSJames Smart 
88e399441dSJames Smart enum nvme_fcpop_state {
89e399441dSJames Smart 	FCPOP_STATE_UNINIT	= 0,
90e399441dSJames Smart 	FCPOP_STATE_IDLE	= 1,
91e399441dSJames Smart 	FCPOP_STATE_ACTIVE	= 2,
92e399441dSJames Smart 	FCPOP_STATE_ABORTED	= 3,
9378a7ac26SJames Smart 	FCPOP_STATE_COMPLETE	= 4,
94e399441dSJames Smart };
95e399441dSJames Smart 
96e399441dSJames Smart struct nvme_fc_fcp_op {
97e399441dSJames Smart 	struct nvme_request	nreq;		/*
98e399441dSJames Smart 						 * nvme/host/core.c
99e399441dSJames Smart 						 * requires this to be
100e399441dSJames Smart 						 * the 1st element in the
101e399441dSJames Smart 						 * private structure
102e399441dSJames Smart 						 * associated with the
103e399441dSJames Smart 						 * request.
104e399441dSJames Smart 						 */
105e399441dSJames Smart 	struct nvmefc_fcp_req	fcp_req;
106e399441dSJames Smart 
107e399441dSJames Smart 	struct nvme_fc_ctrl	*ctrl;
108e399441dSJames Smart 	struct nvme_fc_queue	*queue;
109e399441dSJames Smart 	struct request		*rq;
110e399441dSJames Smart 
111e399441dSJames Smart 	atomic_t		state;
11278a7ac26SJames Smart 	u32			flags;
113e399441dSJames Smart 	u32			rqno;
114e399441dSJames Smart 	u32			nents;
115e399441dSJames Smart 
116e399441dSJames Smart 	struct nvme_fc_cmd_iu	cmd_iu;
117e399441dSJames Smart 	struct nvme_fc_ersp_iu	rsp_iu;
118e399441dSJames Smart };
119e399441dSJames Smart 
120e399441dSJames Smart struct nvme_fc_lport {
121e399441dSJames Smart 	struct nvme_fc_local_port	localport;
122e399441dSJames Smart 
123e399441dSJames Smart 	struct ida			endp_cnt;
124e399441dSJames Smart 	struct list_head		port_list;	/* nvme_fc_port_list */
125e399441dSJames Smart 	struct list_head		endp_list;
126e399441dSJames Smart 	struct device			*dev;	/* physical device for dma */
127e399441dSJames Smart 	struct nvme_fc_port_template	*ops;
128e399441dSJames Smart 	struct kref			ref;
129e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
130e399441dSJames Smart 
131e399441dSJames Smart struct nvme_fc_rport {
132e399441dSJames Smart 	struct nvme_fc_remote_port	remoteport;
133e399441dSJames Smart 
134e399441dSJames Smart 	struct list_head		endp_list; /* for lport->endp_list */
135e399441dSJames Smart 	struct list_head		ctrl_list;
136c913a8b0SJames Smart 	struct list_head		ls_req_list;
137c913a8b0SJames Smart 	struct device			*dev;	/* physical device for dma */
138c913a8b0SJames Smart 	struct nvme_fc_lport		*lport;
139e399441dSJames Smart 	spinlock_t			lock;
140e399441dSJames Smart 	struct kref			ref;
141e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
142e399441dSJames Smart 
14361bff8efSJames Smart enum nvme_fcctrl_flags {
14461bff8efSJames Smart 	FCCTRL_TERMIO		= (1 << 0),
145e399441dSJames Smart };
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 
155e399441dSJames Smart 	u64			association_id;
156e399441dSJames Smart 
157e399441dSJames Smart 	struct list_head	ctrl_list;	/* rport->ctrl_list */
158e399441dSJames Smart 
159e399441dSJames Smart 	struct blk_mq_tag_set	admin_tag_set;
160e399441dSJames Smart 	struct blk_mq_tag_set	tag_set;
161e399441dSJames Smart 
16261bff8efSJames Smart 	struct delayed_work	connect_work;
16361bff8efSJames Smart 
164e399441dSJames Smart 	struct kref		ref;
16561bff8efSJames Smart 	u32			flags;
16661bff8efSJames Smart 	u32			iocnt;
16736715cf4SJames Smart 	wait_queue_head_t	ioabort_wait;
168e399441dSJames Smart 
169e399441dSJames Smart 	struct nvme_fc_fcp_op	aen_ops[NVME_FC_NR_AEN_COMMANDS];
170e399441dSJames Smart 
171e399441dSJames Smart 	struct nvme_ctrl	ctrl;
172e399441dSJames Smart };
173e399441dSJames Smart 
174e399441dSJames Smart static inline struct nvme_fc_ctrl *
175e399441dSJames Smart to_fc_ctrl(struct nvme_ctrl *ctrl)
176e399441dSJames Smart {
177e399441dSJames Smart 	return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
178e399441dSJames Smart }
179e399441dSJames Smart 
180e399441dSJames Smart static inline struct nvme_fc_lport *
181e399441dSJames Smart localport_to_lport(struct nvme_fc_local_port *portptr)
182e399441dSJames Smart {
183e399441dSJames Smart 	return container_of(portptr, struct nvme_fc_lport, localport);
184e399441dSJames Smart }
185e399441dSJames Smart 
186e399441dSJames Smart static inline struct nvme_fc_rport *
187e399441dSJames Smart remoteport_to_rport(struct nvme_fc_remote_port *portptr)
188e399441dSJames Smart {
189e399441dSJames Smart 	return container_of(portptr, struct nvme_fc_rport, remoteport);
190e399441dSJames Smart }
191e399441dSJames Smart 
192e399441dSJames Smart static inline struct nvmefc_ls_req_op *
193e399441dSJames Smart ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
194e399441dSJames Smart {
195e399441dSJames Smart 	return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
196e399441dSJames Smart }
197e399441dSJames Smart 
198e399441dSJames Smart static inline struct nvme_fc_fcp_op *
199e399441dSJames Smart fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
200e399441dSJames Smart {
201e399441dSJames Smart 	return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
202e399441dSJames Smart }
203e399441dSJames Smart 
204e399441dSJames Smart 
205e399441dSJames Smart 
206e399441dSJames Smart /* *************************** Globals **************************** */
207e399441dSJames Smart 
208e399441dSJames Smart 
209e399441dSJames Smart static DEFINE_SPINLOCK(nvme_fc_lock);
210e399441dSJames Smart 
211e399441dSJames Smart static LIST_HEAD(nvme_fc_lport_list);
212e399441dSJames Smart static DEFINE_IDA(nvme_fc_local_port_cnt);
213e399441dSJames Smart static DEFINE_IDA(nvme_fc_ctrl_cnt);
214e399441dSJames Smart 
215e399441dSJames Smart 
216e399441dSJames Smart 
2175f568556SJames Smart /*
2185f568556SJames Smart  * These items are short-term. They will eventually be moved into
2195f568556SJames Smart  * a generic FC class. See comments in module init.
2205f568556SJames Smart  */
2215f568556SJames Smart static struct class *fc_class;
2225f568556SJames Smart static struct device *fc_udev_device;
2235f568556SJames Smart 
224e399441dSJames Smart 
225e399441dSJames Smart /* *********************** FC-NVME Port Management ************************ */
226e399441dSJames Smart 
227e399441dSJames Smart static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
228e399441dSJames Smart 			struct nvme_fc_queue *, unsigned int);
229e399441dSJames Smart 
2305533d424SJames Smart static void
2315533d424SJames Smart nvme_fc_free_lport(struct kref *ref)
2325533d424SJames Smart {
2335533d424SJames Smart 	struct nvme_fc_lport *lport =
2345533d424SJames Smart 		container_of(ref, struct nvme_fc_lport, ref);
2355533d424SJames Smart 	unsigned long flags;
2365533d424SJames Smart 
2375533d424SJames Smart 	WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
2385533d424SJames Smart 	WARN_ON(!list_empty(&lport->endp_list));
2395533d424SJames Smart 
2405533d424SJames Smart 	/* remove from transport list */
2415533d424SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
2425533d424SJames Smart 	list_del(&lport->port_list);
2435533d424SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
2445533d424SJames Smart 
2455533d424SJames Smart 	/* let the LLDD know we've finished tearing it down */
2465533d424SJames Smart 	lport->ops->localport_delete(&lport->localport);
2475533d424SJames Smart 
2485533d424SJames Smart 	ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
2495533d424SJames Smart 	ida_destroy(&lport->endp_cnt);
2505533d424SJames Smart 
2515533d424SJames Smart 	put_device(lport->dev);
2525533d424SJames Smart 
2535533d424SJames Smart 	kfree(lport);
2545533d424SJames Smart }
2555533d424SJames Smart 
2565533d424SJames Smart static void
2575533d424SJames Smart nvme_fc_lport_put(struct nvme_fc_lport *lport)
2585533d424SJames Smart {
2595533d424SJames Smart 	kref_put(&lport->ref, nvme_fc_free_lport);
2605533d424SJames Smart }
2615533d424SJames Smart 
2625533d424SJames Smart static int
2635533d424SJames Smart nvme_fc_lport_get(struct nvme_fc_lport *lport)
2645533d424SJames Smart {
2655533d424SJames Smart 	return kref_get_unless_zero(&lport->ref);
2665533d424SJames Smart }
2675533d424SJames Smart 
2685533d424SJames Smart 
2695533d424SJames Smart static struct nvme_fc_lport *
2705533d424SJames Smart nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo)
2715533d424SJames Smart {
2725533d424SJames Smart 	struct nvme_fc_lport *lport;
2735533d424SJames Smart 	unsigned long flags;
2745533d424SJames Smart 
2755533d424SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
2765533d424SJames Smart 
2775533d424SJames Smart 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
2785533d424SJames Smart 		if (lport->localport.node_name != pinfo->node_name ||
2795533d424SJames Smart 		    lport->localport.port_name != pinfo->port_name)
2805533d424SJames Smart 			continue;
2815533d424SJames Smart 
2825533d424SJames Smart 		if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
2835533d424SJames Smart 			lport = ERR_PTR(-EEXIST);
2845533d424SJames Smart 			goto out_done;
2855533d424SJames Smart 		}
2865533d424SJames Smart 
2875533d424SJames Smart 		if (!nvme_fc_lport_get(lport)) {
2885533d424SJames Smart 			/*
2895533d424SJames Smart 			 * fails if ref cnt already 0. If so,
2905533d424SJames Smart 			 * act as if lport already deleted
2915533d424SJames Smart 			 */
2925533d424SJames Smart 			lport = NULL;
2935533d424SJames Smart 			goto out_done;
2945533d424SJames Smart 		}
2955533d424SJames Smart 
2965533d424SJames Smart 		/* resume the lport */
2975533d424SJames Smart 
2985533d424SJames Smart 		lport->localport.port_role = pinfo->port_role;
2995533d424SJames Smart 		lport->localport.port_id = pinfo->port_id;
3005533d424SJames Smart 		lport->localport.port_state = FC_OBJSTATE_ONLINE;
3015533d424SJames Smart 
3025533d424SJames Smart 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
3035533d424SJames Smart 
3045533d424SJames Smart 		return lport;
3055533d424SJames Smart 	}
3065533d424SJames Smart 
3075533d424SJames Smart 	lport = NULL;
3085533d424SJames Smart 
3095533d424SJames Smart out_done:
3105533d424SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
3115533d424SJames Smart 
3125533d424SJames Smart 	return lport;
3135533d424SJames Smart }
314e399441dSJames Smart 
315e399441dSJames Smart /**
316e399441dSJames Smart  * nvme_fc_register_localport - transport entry point called by an
317e399441dSJames Smart  *                              LLDD to register the existence of a NVME
318e399441dSJames Smart  *                              host FC port.
319e399441dSJames Smart  * @pinfo:     pointer to information about the port to be registered
320e399441dSJames Smart  * @template:  LLDD entrypoints and operational parameters for the port
321e399441dSJames Smart  * @dev:       physical hardware device node port corresponds to. Will be
322e399441dSJames Smart  *             used for DMA mappings
323e399441dSJames Smart  * @lport_p:   pointer to a local port pointer. Upon success, the routine
324e399441dSJames Smart  *             will allocate a nvme_fc_local_port structure and place its
325e399441dSJames Smart  *             address in the local port pointer. Upon failure, local port
326e399441dSJames Smart  *             pointer will be set to 0.
327e399441dSJames Smart  *
328e399441dSJames Smart  * Returns:
329e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
330e399441dSJames Smart  * (ex: -ENXIO) upon failure.
331e399441dSJames Smart  */
332e399441dSJames Smart int
333e399441dSJames Smart nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
334e399441dSJames Smart 			struct nvme_fc_port_template *template,
335e399441dSJames Smart 			struct device *dev,
336e399441dSJames Smart 			struct nvme_fc_local_port **portptr)
337e399441dSJames Smart {
338e399441dSJames Smart 	struct nvme_fc_lport *newrec;
339e399441dSJames Smart 	unsigned long flags;
340e399441dSJames Smart 	int ret, idx;
341e399441dSJames Smart 
342e399441dSJames Smart 	if (!template->localport_delete || !template->remoteport_delete ||
343e399441dSJames Smart 	    !template->ls_req || !template->fcp_io ||
344e399441dSJames Smart 	    !template->ls_abort || !template->fcp_abort ||
345e399441dSJames Smart 	    !template->max_hw_queues || !template->max_sgl_segments ||
346e399441dSJames Smart 	    !template->max_dif_sgl_segments || !template->dma_boundary) {
347e399441dSJames Smart 		ret = -EINVAL;
348e399441dSJames Smart 		goto out_reghost_failed;
349e399441dSJames Smart 	}
350e399441dSJames Smart 
3515533d424SJames Smart 	/*
3525533d424SJames Smart 	 * look to see if there is already a localport that had been
3535533d424SJames Smart 	 * deregistered and in the process of waiting for all the
3545533d424SJames Smart 	 * references to fully be removed.  If the references haven't
3555533d424SJames Smart 	 * expired, we can simply re-enable the localport. Remoteports
3565533d424SJames Smart 	 * and controller reconnections should resume naturally.
3575533d424SJames Smart 	 */
3585533d424SJames Smart 	newrec = nvme_fc_attach_to_unreg_lport(pinfo);
3595533d424SJames Smart 
3605533d424SJames Smart 	/* found an lport, but something about its state is bad */
3615533d424SJames Smart 	if (IS_ERR(newrec)) {
3625533d424SJames Smart 		ret = PTR_ERR(newrec);
3635533d424SJames Smart 		goto out_reghost_failed;
3645533d424SJames Smart 
3655533d424SJames Smart 	/* found existing lport, which was resumed */
3665533d424SJames Smart 	} else if (newrec) {
3675533d424SJames Smart 		*portptr = &newrec->localport;
3685533d424SJames Smart 		return 0;
3695533d424SJames Smart 	}
3705533d424SJames Smart 
3715533d424SJames Smart 	/* nothing found - allocate a new localport struct */
3725533d424SJames Smart 
373e399441dSJames Smart 	newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
374e399441dSJames Smart 			 GFP_KERNEL);
375e399441dSJames Smart 	if (!newrec) {
376e399441dSJames Smart 		ret = -ENOMEM;
377e399441dSJames Smart 		goto out_reghost_failed;
378e399441dSJames Smart 	}
379e399441dSJames Smart 
380e399441dSJames Smart 	idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
381e399441dSJames Smart 	if (idx < 0) {
382e399441dSJames Smart 		ret = -ENOSPC;
383e399441dSJames Smart 		goto out_fail_kfree;
384e399441dSJames Smart 	}
385e399441dSJames Smart 
386e399441dSJames Smart 	if (!get_device(dev) && dev) {
387e399441dSJames Smart 		ret = -ENODEV;
388e399441dSJames Smart 		goto out_ida_put;
389e399441dSJames Smart 	}
390e399441dSJames Smart 
391e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->port_list);
392e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->endp_list);
393e399441dSJames Smart 	kref_init(&newrec->ref);
394e399441dSJames Smart 	newrec->ops = template;
395e399441dSJames Smart 	newrec->dev = dev;
396e399441dSJames Smart 	ida_init(&newrec->endp_cnt);
397e399441dSJames Smart 	newrec->localport.private = &newrec[1];
398e399441dSJames Smart 	newrec->localport.node_name = pinfo->node_name;
399e399441dSJames Smart 	newrec->localport.port_name = pinfo->port_name;
400e399441dSJames Smart 	newrec->localport.port_role = pinfo->port_role;
401e399441dSJames Smart 	newrec->localport.port_id = pinfo->port_id;
402e399441dSJames Smart 	newrec->localport.port_state = FC_OBJSTATE_ONLINE;
403e399441dSJames Smart 	newrec->localport.port_num = idx;
404e399441dSJames Smart 
405e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
406e399441dSJames Smart 	list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
407e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
408e399441dSJames Smart 
409e399441dSJames Smart 	if (dev)
410e399441dSJames Smart 		dma_set_seg_boundary(dev, template->dma_boundary);
411e399441dSJames Smart 
412e399441dSJames Smart 	*portptr = &newrec->localport;
413e399441dSJames Smart 	return 0;
414e399441dSJames Smart 
415e399441dSJames Smart out_ida_put:
416e399441dSJames Smart 	ida_simple_remove(&nvme_fc_local_port_cnt, idx);
417e399441dSJames Smart out_fail_kfree:
418e399441dSJames Smart 	kfree(newrec);
419e399441dSJames Smart out_reghost_failed:
420e399441dSJames Smart 	*portptr = NULL;
421e399441dSJames Smart 
422e399441dSJames Smart 	return ret;
423e399441dSJames Smart }
424e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
425e399441dSJames Smart 
426e399441dSJames Smart /**
427e399441dSJames Smart  * nvme_fc_unregister_localport - transport entry point called by an
428e399441dSJames Smart  *                              LLDD to deregister/remove a previously
429e399441dSJames Smart  *                              registered a NVME host FC port.
430e399441dSJames Smart  * @localport: pointer to the (registered) local port that is to be
431e399441dSJames Smart  *             deregistered.
432e399441dSJames Smart  *
433e399441dSJames Smart  * Returns:
434e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
435e399441dSJames Smart  * (ex: -ENXIO) upon failure.
436e399441dSJames Smart  */
437e399441dSJames Smart int
438e399441dSJames Smart nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
439e399441dSJames Smart {
440e399441dSJames Smart 	struct nvme_fc_lport *lport = localport_to_lport(portptr);
441e399441dSJames Smart 	unsigned long flags;
442e399441dSJames Smart 
443e399441dSJames Smart 	if (!portptr)
444e399441dSJames Smart 		return -EINVAL;
445e399441dSJames Smart 
446e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
447e399441dSJames Smart 
448e399441dSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
449e399441dSJames Smart 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
450e399441dSJames Smart 		return -EINVAL;
451e399441dSJames Smart 	}
452e399441dSJames Smart 	portptr->port_state = FC_OBJSTATE_DELETED;
453e399441dSJames Smart 
454e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
455e399441dSJames Smart 
456e399441dSJames Smart 	nvme_fc_lport_put(lport);
457e399441dSJames Smart 
458e399441dSJames Smart 	return 0;
459e399441dSJames Smart }
460e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
461e399441dSJames Smart 
462eaefd5abSJames Smart /*
463eaefd5abSJames Smart  * TRADDR strings, per FC-NVME are fixed format:
464eaefd5abSJames Smart  *   "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
465eaefd5abSJames Smart  * udev event will only differ by prefix of what field is
466eaefd5abSJames Smart  * being specified:
467eaefd5abSJames Smart  *    "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
468eaefd5abSJames Smart  *  19 + 43 + null_fudge = 64 characters
469eaefd5abSJames Smart  */
470eaefd5abSJames Smart #define FCNVME_TRADDR_LENGTH		64
471eaefd5abSJames Smart 
472eaefd5abSJames Smart static void
473eaefd5abSJames Smart nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
474eaefd5abSJames Smart 		struct nvme_fc_rport *rport)
475eaefd5abSJames Smart {
476eaefd5abSJames Smart 	char hostaddr[FCNVME_TRADDR_LENGTH];	/* NVMEFC_HOST_TRADDR=...*/
477eaefd5abSJames Smart 	char tgtaddr[FCNVME_TRADDR_LENGTH];	/* NVMEFC_TRADDR=...*/
478eaefd5abSJames Smart 	char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
479eaefd5abSJames Smart 
480eaefd5abSJames Smart 	if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
481eaefd5abSJames Smart 		return;
482eaefd5abSJames Smart 
483eaefd5abSJames Smart 	snprintf(hostaddr, sizeof(hostaddr),
484eaefd5abSJames Smart 		"NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
485eaefd5abSJames Smart 		lport->localport.node_name, lport->localport.port_name);
486eaefd5abSJames Smart 	snprintf(tgtaddr, sizeof(tgtaddr),
487eaefd5abSJames Smart 		"NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
488eaefd5abSJames Smart 		rport->remoteport.node_name, rport->remoteport.port_name);
489eaefd5abSJames Smart 	kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
490eaefd5abSJames Smart }
491eaefd5abSJames Smart 
492469d0ef0SJames Smart static void
493469d0ef0SJames Smart nvme_fc_free_rport(struct kref *ref)
494469d0ef0SJames Smart {
495469d0ef0SJames Smart 	struct nvme_fc_rport *rport =
496469d0ef0SJames Smart 		container_of(ref, struct nvme_fc_rport, ref);
497469d0ef0SJames Smart 	struct nvme_fc_lport *lport =
498469d0ef0SJames Smart 			localport_to_lport(rport->remoteport.localport);
499469d0ef0SJames Smart 	unsigned long flags;
500469d0ef0SJames Smart 
501469d0ef0SJames Smart 	WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
502469d0ef0SJames Smart 	WARN_ON(!list_empty(&rport->ctrl_list));
503469d0ef0SJames Smart 
504469d0ef0SJames Smart 	/* remove from lport list */
505469d0ef0SJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
506469d0ef0SJames Smart 	list_del(&rport->endp_list);
507469d0ef0SJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
508469d0ef0SJames Smart 
509469d0ef0SJames Smart 	/* let the LLDD know we've finished tearing it down */
510469d0ef0SJames Smart 	lport->ops->remoteport_delete(&rport->remoteport);
511469d0ef0SJames Smart 
512469d0ef0SJames Smart 	ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
513469d0ef0SJames Smart 
514469d0ef0SJames Smart 	kfree(rport);
515469d0ef0SJames Smart 
516469d0ef0SJames Smart 	nvme_fc_lport_put(lport);
517469d0ef0SJames Smart }
518469d0ef0SJames Smart 
519469d0ef0SJames Smart static void
520469d0ef0SJames Smart nvme_fc_rport_put(struct nvme_fc_rport *rport)
521469d0ef0SJames Smart {
522469d0ef0SJames Smart 	kref_put(&rport->ref, nvme_fc_free_rport);
523469d0ef0SJames Smart }
524469d0ef0SJames Smart 
525469d0ef0SJames Smart static int
526469d0ef0SJames Smart nvme_fc_rport_get(struct nvme_fc_rport *rport)
527469d0ef0SJames Smart {
528469d0ef0SJames Smart 	return kref_get_unless_zero(&rport->ref);
529469d0ef0SJames Smart }
530469d0ef0SJames Smart 
531e399441dSJames Smart /**
532e399441dSJames Smart  * nvme_fc_register_remoteport - transport entry point called by an
533e399441dSJames Smart  *                              LLDD to register the existence of a NVME
534e399441dSJames Smart  *                              subsystem FC port on its fabric.
535e399441dSJames Smart  * @localport: pointer to the (registered) local port that the remote
536e399441dSJames Smart  *             subsystem port is connected to.
537e399441dSJames Smart  * @pinfo:     pointer to information about the port to be registered
538e399441dSJames Smart  * @rport_p:   pointer to a remote port pointer. Upon success, the routine
539e399441dSJames Smart  *             will allocate a nvme_fc_remote_port structure and place its
540e399441dSJames Smart  *             address in the remote port pointer. Upon failure, remote port
541e399441dSJames Smart  *             pointer will be set to 0.
542e399441dSJames Smart  *
543e399441dSJames Smart  * Returns:
544e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
545e399441dSJames Smart  * (ex: -ENXIO) upon failure.
546e399441dSJames Smart  */
547e399441dSJames Smart int
548e399441dSJames Smart nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
549e399441dSJames Smart 				struct nvme_fc_port_info *pinfo,
550e399441dSJames Smart 				struct nvme_fc_remote_port **portptr)
551e399441dSJames Smart {
552e399441dSJames Smart 	struct nvme_fc_lport *lport = localport_to_lport(localport);
553e399441dSJames Smart 	struct nvme_fc_rport *newrec;
554e399441dSJames Smart 	unsigned long flags;
555e399441dSJames Smart 	int ret, idx;
556e399441dSJames Smart 
557e399441dSJames Smart 	newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
558e399441dSJames Smart 			 GFP_KERNEL);
559e399441dSJames Smart 	if (!newrec) {
560e399441dSJames Smart 		ret = -ENOMEM;
561e399441dSJames Smart 		goto out_reghost_failed;
562e399441dSJames Smart 	}
563e399441dSJames Smart 
564e399441dSJames Smart 	if (!nvme_fc_lport_get(lport)) {
565e399441dSJames Smart 		ret = -ESHUTDOWN;
566e399441dSJames Smart 		goto out_kfree_rport;
567e399441dSJames Smart 	}
568e399441dSJames Smart 
569e399441dSJames Smart 	idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
570e399441dSJames Smart 	if (idx < 0) {
571e399441dSJames Smart 		ret = -ENOSPC;
572e399441dSJames Smart 		goto out_lport_put;
573e399441dSJames Smart 	}
574e399441dSJames Smart 
575e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->endp_list);
576e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->ctrl_list);
577c913a8b0SJames Smart 	INIT_LIST_HEAD(&newrec->ls_req_list);
578e399441dSJames Smart 	kref_init(&newrec->ref);
579e399441dSJames Smart 	spin_lock_init(&newrec->lock);
580e399441dSJames Smart 	newrec->remoteport.localport = &lport->localport;
581c913a8b0SJames Smart 	newrec->dev = lport->dev;
582c913a8b0SJames Smart 	newrec->lport = lport;
583e399441dSJames Smart 	newrec->remoteport.private = &newrec[1];
584e399441dSJames Smart 	newrec->remoteport.port_role = pinfo->port_role;
585e399441dSJames Smart 	newrec->remoteport.node_name = pinfo->node_name;
586e399441dSJames Smart 	newrec->remoteport.port_name = pinfo->port_name;
587e399441dSJames Smart 	newrec->remoteport.port_id = pinfo->port_id;
588e399441dSJames Smart 	newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
589e399441dSJames Smart 	newrec->remoteport.port_num = idx;
590ac7fe82bSJames Smart 	/* a registration value of dev_loss_tmo=0 results in the default */
591ac7fe82bSJames Smart 	if (pinfo->dev_loss_tmo)
592ac7fe82bSJames Smart 		newrec->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
593ac7fe82bSJames Smart 	else
594ac7fe82bSJames Smart 		newrec->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
595e399441dSJames Smart 
596e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
597e399441dSJames Smart 	list_add_tail(&newrec->endp_list, &lport->endp_list);
598e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
599e399441dSJames Smart 
600eaefd5abSJames Smart 	nvme_fc_signal_discovery_scan(lport, newrec);
601eaefd5abSJames Smart 
602e399441dSJames Smart 	*portptr = &newrec->remoteport;
603e399441dSJames Smart 	return 0;
604e399441dSJames Smart 
605e399441dSJames Smart out_lport_put:
606e399441dSJames Smart 	nvme_fc_lport_put(lport);
607e399441dSJames Smart out_kfree_rport:
608e399441dSJames Smart 	kfree(newrec);
609e399441dSJames Smart out_reghost_failed:
610e399441dSJames Smart 	*portptr = NULL;
611e399441dSJames Smart 	return ret;
612e399441dSJames Smart }
613e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
614e399441dSJames Smart 
6158d64daf7SJames Smart static int
6168d64daf7SJames Smart nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
6178d64daf7SJames Smart {
6188d64daf7SJames Smart 	struct nvmefc_ls_req_op *lsop;
6198d64daf7SJames Smart 	unsigned long flags;
6208d64daf7SJames Smart 
6218d64daf7SJames Smart restart:
6228d64daf7SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
6238d64daf7SJames Smart 
6248d64daf7SJames Smart 	list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
6258d64daf7SJames Smart 		if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
6268d64daf7SJames Smart 			lsop->flags |= FCOP_FLAGS_TERMIO;
6278d64daf7SJames Smart 			spin_unlock_irqrestore(&rport->lock, flags);
6288d64daf7SJames Smart 			rport->lport->ops->ls_abort(&rport->lport->localport,
6298d64daf7SJames Smart 						&rport->remoteport,
6308d64daf7SJames Smart 						&lsop->ls_req);
6318d64daf7SJames Smart 			goto restart;
6328d64daf7SJames Smart 		}
6338d64daf7SJames Smart 	}
6348d64daf7SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
6358d64daf7SJames Smart 
6368d64daf7SJames Smart 	return 0;
6378d64daf7SJames Smart }
6388d64daf7SJames Smart 
639e399441dSJames Smart /**
640e399441dSJames Smart  * nvme_fc_unregister_remoteport - transport entry point called by an
641e399441dSJames Smart  *                              LLDD to deregister/remove a previously
642e399441dSJames Smart  *                              registered a NVME subsystem FC port.
643e399441dSJames Smart  * @remoteport: pointer to the (registered) remote port that is to be
644e399441dSJames Smart  *              deregistered.
645e399441dSJames Smart  *
646e399441dSJames Smart  * Returns:
647e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
648e399441dSJames Smart  * (ex: -ENXIO) upon failure.
649e399441dSJames Smart  */
650e399441dSJames Smart int
651e399441dSJames Smart nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
652e399441dSJames Smart {
653e399441dSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
654e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl;
655e399441dSJames Smart 	unsigned long flags;
656e399441dSJames Smart 
657e399441dSJames Smart 	if (!portptr)
658e399441dSJames Smart 		return -EINVAL;
659e399441dSJames Smart 
660e399441dSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
661e399441dSJames Smart 
662e399441dSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
663e399441dSJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
664e399441dSJames Smart 		return -EINVAL;
665e399441dSJames Smart 	}
666e399441dSJames Smart 	portptr->port_state = FC_OBJSTATE_DELETED;
667e399441dSJames Smart 
668e399441dSJames Smart 	/* tear down all associations to the remote port */
669e399441dSJames Smart 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
670c5017e85SChristoph Hellwig 		nvme_delete_ctrl(&ctrl->ctrl);
671e399441dSJames Smart 
672e399441dSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
673e399441dSJames Smart 
6748d64daf7SJames Smart 	nvme_fc_abort_lsops(rport);
6758d64daf7SJames Smart 
676e399441dSJames Smart 	nvme_fc_rport_put(rport);
677e399441dSJames Smart 	return 0;
678e399441dSJames Smart }
679e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
680e399441dSJames Smart 
681eaefd5abSJames Smart /**
682eaefd5abSJames Smart  * nvme_fc_rescan_remoteport - transport entry point called by an
683eaefd5abSJames Smart  *                              LLDD to request a nvme device rescan.
684eaefd5abSJames Smart  * @remoteport: pointer to the (registered) remote port that is to be
685eaefd5abSJames Smart  *              rescanned.
686eaefd5abSJames Smart  *
687eaefd5abSJames Smart  * Returns: N/A
688eaefd5abSJames Smart  */
689eaefd5abSJames Smart void
690eaefd5abSJames Smart nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
691eaefd5abSJames Smart {
692eaefd5abSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
693eaefd5abSJames Smart 
694eaefd5abSJames Smart 	nvme_fc_signal_discovery_scan(rport->lport, rport);
695eaefd5abSJames Smart }
696eaefd5abSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
697eaefd5abSJames Smart 
698ac7fe82bSJames Smart int
699ac7fe82bSJames Smart nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
700ac7fe82bSJames Smart 			u32 dev_loss_tmo)
701ac7fe82bSJames Smart {
702ac7fe82bSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
703ac7fe82bSJames Smart 	struct nvme_fc_ctrl *ctrl;
704ac7fe82bSJames Smart 	unsigned long flags;
705ac7fe82bSJames Smart 
706ac7fe82bSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
707ac7fe82bSJames Smart 
708ac7fe82bSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
709ac7fe82bSJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
710ac7fe82bSJames Smart 		return -EINVAL;
711ac7fe82bSJames Smart 	}
712ac7fe82bSJames Smart 
713ac7fe82bSJames Smart 	/* a dev_loss_tmo of 0 (immediate) is allowed to be set */
714ac7fe82bSJames Smart 	rport->remoteport.dev_loss_tmo = dev_loss_tmo;
715ac7fe82bSJames Smart 
716ac7fe82bSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
717ac7fe82bSJames Smart 
718ac7fe82bSJames Smart 	return 0;
719ac7fe82bSJames Smart }
720ac7fe82bSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
721ac7fe82bSJames Smart 
722e399441dSJames Smart 
723e399441dSJames Smart /* *********************** FC-NVME DMA Handling **************************** */
724e399441dSJames Smart 
725e399441dSJames Smart /*
726e399441dSJames Smart  * The fcloop device passes in a NULL device pointer. Real LLD's will
727e399441dSJames Smart  * pass in a valid device pointer. If NULL is passed to the dma mapping
728e399441dSJames Smart  * routines, depending on the platform, it may or may not succeed, and
729e399441dSJames Smart  * may crash.
730e399441dSJames Smart  *
731e399441dSJames Smart  * As such:
732e399441dSJames Smart  * Wrapper all the dma routines and check the dev pointer.
733e399441dSJames Smart  *
734e399441dSJames Smart  * If simple mappings (return just a dma address, we'll noop them,
735e399441dSJames Smart  * returning a dma address of 0.
736e399441dSJames Smart  *
737e399441dSJames Smart  * On more complex mappings (dma_map_sg), a pseudo routine fills
738e399441dSJames Smart  * in the scatter list, setting all dma addresses to 0.
739e399441dSJames Smart  */
740e399441dSJames Smart 
741e399441dSJames Smart static inline dma_addr_t
742e399441dSJames Smart fc_dma_map_single(struct device *dev, void *ptr, size_t size,
743e399441dSJames Smart 		enum dma_data_direction dir)
744e399441dSJames Smart {
745e399441dSJames Smart 	return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
746e399441dSJames Smart }
747e399441dSJames Smart 
748e399441dSJames Smart static inline int
749e399441dSJames Smart fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
750e399441dSJames Smart {
751e399441dSJames Smart 	return dev ? dma_mapping_error(dev, dma_addr) : 0;
752e399441dSJames Smart }
753e399441dSJames Smart 
754e399441dSJames Smart static inline void
755e399441dSJames Smart fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
756e399441dSJames Smart 	enum dma_data_direction dir)
757e399441dSJames Smart {
758e399441dSJames Smart 	if (dev)
759e399441dSJames Smart 		dma_unmap_single(dev, addr, size, dir);
760e399441dSJames Smart }
761e399441dSJames Smart 
762e399441dSJames Smart static inline void
763e399441dSJames Smart fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
764e399441dSJames Smart 		enum dma_data_direction dir)
765e399441dSJames Smart {
766e399441dSJames Smart 	if (dev)
767e399441dSJames Smart 		dma_sync_single_for_cpu(dev, addr, size, dir);
768e399441dSJames Smart }
769e399441dSJames Smart 
770e399441dSJames Smart static inline void
771e399441dSJames Smart fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
772e399441dSJames Smart 		enum dma_data_direction dir)
773e399441dSJames Smart {
774e399441dSJames Smart 	if (dev)
775e399441dSJames Smart 		dma_sync_single_for_device(dev, addr, size, dir);
776e399441dSJames Smart }
777e399441dSJames Smart 
778e399441dSJames Smart /* pseudo dma_map_sg call */
779e399441dSJames Smart static int
780e399441dSJames Smart fc_map_sg(struct scatterlist *sg, int nents)
781e399441dSJames Smart {
782e399441dSJames Smart 	struct scatterlist *s;
783e399441dSJames Smart 	int i;
784e399441dSJames Smart 
785e399441dSJames Smart 	WARN_ON(nents == 0 || sg[0].length == 0);
786e399441dSJames Smart 
787e399441dSJames Smart 	for_each_sg(sg, s, nents, i) {
788e399441dSJames Smart 		s->dma_address = 0L;
789e399441dSJames Smart #ifdef CONFIG_NEED_SG_DMA_LENGTH
790e399441dSJames Smart 		s->dma_length = s->length;
791e399441dSJames Smart #endif
792e399441dSJames Smart 	}
793e399441dSJames Smart 	return nents;
794e399441dSJames Smart }
795e399441dSJames Smart 
796e399441dSJames Smart static inline int
797e399441dSJames Smart fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
798e399441dSJames Smart 		enum dma_data_direction dir)
799e399441dSJames Smart {
800e399441dSJames Smart 	return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
801e399441dSJames Smart }
802e399441dSJames Smart 
803e399441dSJames Smart static inline void
804e399441dSJames Smart fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
805e399441dSJames Smart 		enum dma_data_direction dir)
806e399441dSJames Smart {
807e399441dSJames Smart 	if (dev)
808e399441dSJames Smart 		dma_unmap_sg(dev, sg, nents, dir);
809e399441dSJames Smart }
810e399441dSJames Smart 
811e399441dSJames Smart 
812e399441dSJames Smart /* *********************** FC-NVME LS Handling **************************** */
813e399441dSJames Smart 
814e399441dSJames Smart static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
815e399441dSJames Smart static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
816e399441dSJames Smart 
817e399441dSJames Smart 
818e399441dSJames Smart static void
819c913a8b0SJames Smart __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
820e399441dSJames Smart {
821c913a8b0SJames Smart 	struct nvme_fc_rport *rport = lsop->rport;
822e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
823e399441dSJames Smart 	unsigned long flags;
824e399441dSJames Smart 
825c913a8b0SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
826e399441dSJames Smart 
827e399441dSJames Smart 	if (!lsop->req_queued) {
828c913a8b0SJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
829e399441dSJames Smart 		return;
830e399441dSJames Smart 	}
831e399441dSJames Smart 
832e399441dSJames Smart 	list_del(&lsop->lsreq_list);
833e399441dSJames Smart 
834e399441dSJames Smart 	lsop->req_queued = false;
835e399441dSJames Smart 
836c913a8b0SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
837e399441dSJames Smart 
838c913a8b0SJames Smart 	fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
839e399441dSJames Smart 				  (lsreq->rqstlen + lsreq->rsplen),
840e399441dSJames Smart 				  DMA_BIDIRECTIONAL);
841e399441dSJames Smart 
842c913a8b0SJames Smart 	nvme_fc_rport_put(rport);
843e399441dSJames Smart }
844e399441dSJames Smart 
845e399441dSJames Smart static int
846c913a8b0SJames Smart __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
847e399441dSJames Smart 		struct nvmefc_ls_req_op *lsop,
848e399441dSJames Smart 		void (*done)(struct nvmefc_ls_req *req, int status))
849e399441dSJames Smart {
850e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
851e399441dSJames Smart 	unsigned long flags;
852c913a8b0SJames Smart 	int ret = 0;
853e399441dSJames Smart 
854c913a8b0SJames Smart 	if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
855c913a8b0SJames Smart 		return -ECONNREFUSED;
856c913a8b0SJames Smart 
857c913a8b0SJames Smart 	if (!nvme_fc_rport_get(rport))
858e399441dSJames Smart 		return -ESHUTDOWN;
859e399441dSJames Smart 
860e399441dSJames Smart 	lsreq->done = done;
861c913a8b0SJames Smart 	lsop->rport = rport;
862e399441dSJames Smart 	lsop->req_queued = false;
863e399441dSJames Smart 	INIT_LIST_HEAD(&lsop->lsreq_list);
864e399441dSJames Smart 	init_completion(&lsop->ls_done);
865e399441dSJames Smart 
866c913a8b0SJames Smart 	lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
867e399441dSJames Smart 				  lsreq->rqstlen + lsreq->rsplen,
868e399441dSJames Smart 				  DMA_BIDIRECTIONAL);
869c913a8b0SJames Smart 	if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
870c913a8b0SJames Smart 		ret = -EFAULT;
871c913a8b0SJames Smart 		goto out_putrport;
872e399441dSJames Smart 	}
873e399441dSJames Smart 	lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
874e399441dSJames Smart 
875c913a8b0SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
876e399441dSJames Smart 
877c913a8b0SJames Smart 	list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
878e399441dSJames Smart 
879e399441dSJames Smart 	lsop->req_queued = true;
880e399441dSJames Smart 
881c913a8b0SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
882e399441dSJames Smart 
883c913a8b0SJames Smart 	ret = rport->lport->ops->ls_req(&rport->lport->localport,
884c913a8b0SJames Smart 					&rport->remoteport, lsreq);
885e399441dSJames Smart 	if (ret)
886c913a8b0SJames Smart 		goto out_unlink;
887c913a8b0SJames Smart 
888c913a8b0SJames Smart 	return 0;
889c913a8b0SJames Smart 
890c913a8b0SJames Smart out_unlink:
891e399441dSJames Smart 	lsop->ls_error = ret;
892c913a8b0SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
893c913a8b0SJames Smart 	lsop->req_queued = false;
894c913a8b0SJames Smart 	list_del(&lsop->lsreq_list);
895c913a8b0SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
896c913a8b0SJames Smart 	fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
897c913a8b0SJames Smart 				  (lsreq->rqstlen + lsreq->rsplen),
898c913a8b0SJames Smart 				  DMA_BIDIRECTIONAL);
899c913a8b0SJames Smart out_putrport:
900c913a8b0SJames Smart 	nvme_fc_rport_put(rport);
901e399441dSJames Smart 
902e399441dSJames Smart 	return ret;
903e399441dSJames Smart }
904e399441dSJames Smart 
905e399441dSJames Smart static void
906e399441dSJames Smart nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
907e399441dSJames Smart {
908e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
909e399441dSJames Smart 
910e399441dSJames Smart 	lsop->ls_error = status;
911e399441dSJames Smart 	complete(&lsop->ls_done);
912e399441dSJames Smart }
913e399441dSJames Smart 
914e399441dSJames Smart static int
915c913a8b0SJames Smart nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
916e399441dSJames Smart {
917e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
918e399441dSJames Smart 	struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
919e399441dSJames Smart 	int ret;
920e399441dSJames Smart 
921c913a8b0SJames Smart 	ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
922e399441dSJames Smart 
923c913a8b0SJames Smart 	if (!ret) {
924e399441dSJames Smart 		/*
925e399441dSJames Smart 		 * No timeout/not interruptible as we need the struct
926e399441dSJames Smart 		 * to exist until the lldd calls us back. Thus mandate
927e399441dSJames Smart 		 * wait until driver calls back. lldd responsible for
928e399441dSJames Smart 		 * the timeout action
929e399441dSJames Smart 		 */
930e399441dSJames Smart 		wait_for_completion(&lsop->ls_done);
931e399441dSJames Smart 
932c913a8b0SJames Smart 		__nvme_fc_finish_ls_req(lsop);
933e399441dSJames Smart 
934c913a8b0SJames Smart 		ret = lsop->ls_error;
935e399441dSJames Smart 	}
936e399441dSJames Smart 
937c913a8b0SJames Smart 	if (ret)
938c913a8b0SJames Smart 		return ret;
939c913a8b0SJames Smart 
940e399441dSJames Smart 	/* ACC or RJT payload ? */
941e399441dSJames Smart 	if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
942e399441dSJames Smart 		return -ENXIO;
943e399441dSJames Smart 
944e399441dSJames Smart 	return 0;
945e399441dSJames Smart }
946e399441dSJames Smart 
947c913a8b0SJames Smart static int
948c913a8b0SJames Smart nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
949e399441dSJames Smart 		struct nvmefc_ls_req_op *lsop,
950e399441dSJames Smart 		void (*done)(struct nvmefc_ls_req *req, int status))
951e399441dSJames Smart {
952e399441dSJames Smart 	/* don't wait for completion */
953e399441dSJames Smart 
954c913a8b0SJames Smart 	return __nvme_fc_send_ls_req(rport, lsop, done);
955e399441dSJames Smart }
956e399441dSJames Smart 
957e399441dSJames Smart /* Validation Error indexes into the string table below */
958e399441dSJames Smart enum {
959e399441dSJames Smart 	VERR_NO_ERROR		= 0,
960e399441dSJames Smart 	VERR_LSACC		= 1,
961e399441dSJames Smart 	VERR_LSDESC_RQST	= 2,
962e399441dSJames Smart 	VERR_LSDESC_RQST_LEN	= 3,
963e399441dSJames Smart 	VERR_ASSOC_ID		= 4,
964e399441dSJames Smart 	VERR_ASSOC_ID_LEN	= 5,
965e399441dSJames Smart 	VERR_CONN_ID		= 6,
966e399441dSJames Smart 	VERR_CONN_ID_LEN	= 7,
967e399441dSJames Smart 	VERR_CR_ASSOC		= 8,
968e399441dSJames Smart 	VERR_CR_ASSOC_ACC_LEN	= 9,
969e399441dSJames Smart 	VERR_CR_CONN		= 10,
970e399441dSJames Smart 	VERR_CR_CONN_ACC_LEN	= 11,
971e399441dSJames Smart 	VERR_DISCONN		= 12,
972e399441dSJames Smart 	VERR_DISCONN_ACC_LEN	= 13,
973e399441dSJames Smart };
974e399441dSJames Smart 
975e399441dSJames Smart static char *validation_errors[] = {
976e399441dSJames Smart 	"OK",
977e399441dSJames Smart 	"Not LS_ACC",
978e399441dSJames Smart 	"Not LSDESC_RQST",
979e399441dSJames Smart 	"Bad LSDESC_RQST Length",
980e399441dSJames Smart 	"Not Association ID",
981e399441dSJames Smart 	"Bad Association ID Length",
982e399441dSJames Smart 	"Not Connection ID",
983e399441dSJames Smart 	"Bad Connection ID Length",
984e399441dSJames Smart 	"Not CR_ASSOC Rqst",
985e399441dSJames Smart 	"Bad CR_ASSOC ACC Length",
986e399441dSJames Smart 	"Not CR_CONN Rqst",
987e399441dSJames Smart 	"Bad CR_CONN ACC Length",
988e399441dSJames Smart 	"Not Disconnect Rqst",
989e399441dSJames Smart 	"Bad Disconnect ACC Length",
990e399441dSJames Smart };
991e399441dSJames Smart 
992e399441dSJames Smart static int
993e399441dSJames Smart nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
994e399441dSJames Smart 	struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
995e399441dSJames Smart {
996e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
997e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
998e399441dSJames Smart 	struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
999e399441dSJames Smart 	struct fcnvme_ls_cr_assoc_acc *assoc_acc;
1000e399441dSJames Smart 	int ret, fcret = 0;
1001e399441dSJames Smart 
1002e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
1003e399441dSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz +
1004e399441dSJames Smart 			 sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
1005e399441dSJames Smart 	if (!lsop) {
1006e399441dSJames Smart 		ret = -ENOMEM;
1007e399441dSJames Smart 		goto out_no_memory;
1008e399441dSJames Smart 	}
1009e399441dSJames Smart 	lsreq = &lsop->ls_req;
1010e399441dSJames Smart 
1011e399441dSJames Smart 	lsreq->private = (void *)&lsop[1];
1012e399441dSJames Smart 	assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
1013e399441dSJames Smart 			(lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1014e399441dSJames Smart 	assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1015e399441dSJames Smart 
1016e399441dSJames Smart 	assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1017e399441dSJames Smart 	assoc_rqst->desc_list_len =
1018e399441dSJames Smart 			cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1019e399441dSJames Smart 
1020e399441dSJames Smart 	assoc_rqst->assoc_cmd.desc_tag =
1021e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1022e399441dSJames Smart 	assoc_rqst->assoc_cmd.desc_len =
1023e399441dSJames Smart 			fcnvme_lsdesc_len(
1024e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1025e399441dSJames Smart 
1026e399441dSJames Smart 	assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1027e399441dSJames Smart 	assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize);
1028e399441dSJames Smart 	/* Linux supports only Dynamic controllers */
1029e399441dSJames Smart 	assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
10308e412263SChristoph Hellwig 	uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1031e399441dSJames Smart 	strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1032e399441dSJames Smart 		min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1033e399441dSJames Smart 	strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1034e399441dSJames Smart 		min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
1035e399441dSJames Smart 
1036e399441dSJames Smart 	lsop->queue = queue;
1037e399441dSJames Smart 	lsreq->rqstaddr = assoc_rqst;
1038e399441dSJames Smart 	lsreq->rqstlen = sizeof(*assoc_rqst);
1039e399441dSJames Smart 	lsreq->rspaddr = assoc_acc;
1040e399441dSJames Smart 	lsreq->rsplen = sizeof(*assoc_acc);
1041e399441dSJames Smart 	lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1042e399441dSJames Smart 
1043c913a8b0SJames Smart 	ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1044e399441dSJames Smart 	if (ret)
1045e399441dSJames Smart 		goto out_free_buffer;
1046e399441dSJames Smart 
1047e399441dSJames Smart 	/* process connect LS completion */
1048e399441dSJames Smart 
1049e399441dSJames Smart 	/* validate the ACC response */
1050e399441dSJames Smart 	if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1051e399441dSJames Smart 		fcret = VERR_LSACC;
1052f77fc87cSJames Smart 	else if (assoc_acc->hdr.desc_list_len !=
1053e399441dSJames Smart 			fcnvme_lsdesc_len(
1054e399441dSJames Smart 				sizeof(struct fcnvme_ls_cr_assoc_acc)))
1055e399441dSJames Smart 		fcret = VERR_CR_ASSOC_ACC_LEN;
1056f77fc87cSJames Smart 	else if (assoc_acc->hdr.rqst.desc_tag !=
1057f77fc87cSJames Smart 			cpu_to_be32(FCNVME_LSDESC_RQST))
1058e399441dSJames Smart 		fcret = VERR_LSDESC_RQST;
1059e399441dSJames Smart 	else if (assoc_acc->hdr.rqst.desc_len !=
1060e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1061e399441dSJames Smart 		fcret = VERR_LSDESC_RQST_LEN;
1062e399441dSJames Smart 	else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1063e399441dSJames Smart 		fcret = VERR_CR_ASSOC;
1064e399441dSJames Smart 	else if (assoc_acc->associd.desc_tag !=
1065e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1066e399441dSJames Smart 		fcret = VERR_ASSOC_ID;
1067e399441dSJames Smart 	else if (assoc_acc->associd.desc_len !=
1068e399441dSJames Smart 			fcnvme_lsdesc_len(
1069e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id)))
1070e399441dSJames Smart 		fcret = VERR_ASSOC_ID_LEN;
1071e399441dSJames Smart 	else if (assoc_acc->connectid.desc_tag !=
1072e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1073e399441dSJames Smart 		fcret = VERR_CONN_ID;
1074e399441dSJames Smart 	else if (assoc_acc->connectid.desc_len !=
1075e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1076e399441dSJames Smart 		fcret = VERR_CONN_ID_LEN;
1077e399441dSJames Smart 
1078e399441dSJames Smart 	if (fcret) {
1079e399441dSJames Smart 		ret = -EBADF;
1080e399441dSJames Smart 		dev_err(ctrl->dev,
1081e399441dSJames Smart 			"q %d connect failed: %s\n",
1082e399441dSJames Smart 			queue->qnum, validation_errors[fcret]);
1083e399441dSJames Smart 	} else {
1084e399441dSJames Smart 		ctrl->association_id =
1085e399441dSJames Smart 			be64_to_cpu(assoc_acc->associd.association_id);
1086e399441dSJames Smart 		queue->connection_id =
1087e399441dSJames Smart 			be64_to_cpu(assoc_acc->connectid.connection_id);
1088e399441dSJames Smart 		set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1089e399441dSJames Smart 	}
1090e399441dSJames Smart 
1091e399441dSJames Smart out_free_buffer:
1092e399441dSJames Smart 	kfree(lsop);
1093e399441dSJames Smart out_no_memory:
1094e399441dSJames Smart 	if (ret)
1095e399441dSJames Smart 		dev_err(ctrl->dev,
1096e399441dSJames Smart 			"queue %d connect admin queue failed (%d).\n",
1097e399441dSJames Smart 			queue->qnum, ret);
1098e399441dSJames Smart 	return ret;
1099e399441dSJames Smart }
1100e399441dSJames Smart 
1101e399441dSJames Smart static int
1102e399441dSJames Smart nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1103e399441dSJames Smart 			u16 qsize, u16 ersp_ratio)
1104e399441dSJames Smart {
1105e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
1106e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
1107e399441dSJames Smart 	struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1108e399441dSJames Smart 	struct fcnvme_ls_cr_conn_acc *conn_acc;
1109e399441dSJames Smart 	int ret, fcret = 0;
1110e399441dSJames Smart 
1111e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
1112e399441dSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz +
1113e399441dSJames Smart 			 sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
1114e399441dSJames Smart 	if (!lsop) {
1115e399441dSJames Smart 		ret = -ENOMEM;
1116e399441dSJames Smart 		goto out_no_memory;
1117e399441dSJames Smart 	}
1118e399441dSJames Smart 	lsreq = &lsop->ls_req;
1119e399441dSJames Smart 
1120e399441dSJames Smart 	lsreq->private = (void *)&lsop[1];
1121e399441dSJames Smart 	conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
1122e399441dSJames Smart 			(lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1123e399441dSJames Smart 	conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1124e399441dSJames Smart 
1125e399441dSJames Smart 	conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1126e399441dSJames Smart 	conn_rqst->desc_list_len = cpu_to_be32(
1127e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id) +
1128e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1129e399441dSJames Smart 
1130e399441dSJames Smart 	conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1131e399441dSJames Smart 	conn_rqst->associd.desc_len =
1132e399441dSJames Smart 			fcnvme_lsdesc_len(
1133e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id));
1134e399441dSJames Smart 	conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1135e399441dSJames Smart 	conn_rqst->connect_cmd.desc_tag =
1136e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1137e399441dSJames Smart 	conn_rqst->connect_cmd.desc_len =
1138e399441dSJames Smart 			fcnvme_lsdesc_len(
1139e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1140e399441dSJames Smart 	conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1141e399441dSJames Smart 	conn_rqst->connect_cmd.qid  = cpu_to_be16(queue->qnum);
1142e399441dSJames Smart 	conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize);
1143e399441dSJames Smart 
1144e399441dSJames Smart 	lsop->queue = queue;
1145e399441dSJames Smart 	lsreq->rqstaddr = conn_rqst;
1146e399441dSJames Smart 	lsreq->rqstlen = sizeof(*conn_rqst);
1147e399441dSJames Smart 	lsreq->rspaddr = conn_acc;
1148e399441dSJames Smart 	lsreq->rsplen = sizeof(*conn_acc);
1149e399441dSJames Smart 	lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1150e399441dSJames Smart 
1151c913a8b0SJames Smart 	ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1152e399441dSJames Smart 	if (ret)
1153e399441dSJames Smart 		goto out_free_buffer;
1154e399441dSJames Smart 
1155e399441dSJames Smart 	/* process connect LS completion */
1156e399441dSJames Smart 
1157e399441dSJames Smart 	/* validate the ACC response */
1158e399441dSJames Smart 	if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1159e399441dSJames Smart 		fcret = VERR_LSACC;
1160f77fc87cSJames Smart 	else if (conn_acc->hdr.desc_list_len !=
1161e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1162e399441dSJames Smart 		fcret = VERR_CR_CONN_ACC_LEN;
1163f77fc87cSJames Smart 	else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1164e399441dSJames Smart 		fcret = VERR_LSDESC_RQST;
1165e399441dSJames Smart 	else if (conn_acc->hdr.rqst.desc_len !=
1166e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1167e399441dSJames Smart 		fcret = VERR_LSDESC_RQST_LEN;
1168e399441dSJames Smart 	else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1169e399441dSJames Smart 		fcret = VERR_CR_CONN;
1170e399441dSJames Smart 	else if (conn_acc->connectid.desc_tag !=
1171e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1172e399441dSJames Smart 		fcret = VERR_CONN_ID;
1173e399441dSJames Smart 	else if (conn_acc->connectid.desc_len !=
1174e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1175e399441dSJames Smart 		fcret = VERR_CONN_ID_LEN;
1176e399441dSJames Smart 
1177e399441dSJames Smart 	if (fcret) {
1178e399441dSJames Smart 		ret = -EBADF;
1179e399441dSJames Smart 		dev_err(ctrl->dev,
1180e399441dSJames Smart 			"q %d connect failed: %s\n",
1181e399441dSJames Smart 			queue->qnum, validation_errors[fcret]);
1182e399441dSJames Smart 	} else {
1183e399441dSJames Smart 		queue->connection_id =
1184e399441dSJames Smart 			be64_to_cpu(conn_acc->connectid.connection_id);
1185e399441dSJames Smart 		set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1186e399441dSJames Smart 	}
1187e399441dSJames Smart 
1188e399441dSJames Smart out_free_buffer:
1189e399441dSJames Smart 	kfree(lsop);
1190e399441dSJames Smart out_no_memory:
1191e399441dSJames Smart 	if (ret)
1192e399441dSJames Smart 		dev_err(ctrl->dev,
1193e399441dSJames Smart 			"queue %d connect command failed (%d).\n",
1194e399441dSJames Smart 			queue->qnum, ret);
1195e399441dSJames Smart 	return ret;
1196e399441dSJames Smart }
1197e399441dSJames Smart 
1198e399441dSJames Smart static void
1199e399441dSJames Smart nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1200e399441dSJames Smart {
1201e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1202e399441dSJames Smart 
1203c913a8b0SJames Smart 	__nvme_fc_finish_ls_req(lsop);
1204e399441dSJames Smart 
1205e399441dSJames Smart 	/* fc-nvme iniator doesn't care about success or failure of cmd */
1206e399441dSJames Smart 
1207e399441dSJames Smart 	kfree(lsop);
1208e399441dSJames Smart }
1209e399441dSJames Smart 
1210e399441dSJames Smart /*
1211e399441dSJames Smart  * This routine sends a FC-NVME LS to disconnect (aka terminate)
1212e399441dSJames Smart  * the FC-NVME Association.  Terminating the association also
1213e399441dSJames Smart  * terminates the FC-NVME connections (per queue, both admin and io
1214e399441dSJames Smart  * queues) that are part of the association. E.g. things are torn
1215e399441dSJames Smart  * down, and the related FC-NVME Association ID and Connection IDs
1216e399441dSJames Smart  * become invalid.
1217e399441dSJames Smart  *
1218e399441dSJames Smart  * The behavior of the fc-nvme initiator is such that it's
1219e399441dSJames Smart  * understanding of the association and connections will implicitly
1220e399441dSJames Smart  * be torn down. The action is implicit as it may be due to a loss of
1221e399441dSJames Smart  * connectivity with the fc-nvme target, so you may never get a
1222e399441dSJames Smart  * response even if you tried.  As such, the action of this routine
1223e399441dSJames Smart  * is to asynchronously send the LS, ignore any results of the LS, and
1224e399441dSJames Smart  * continue on with terminating the association. If the fc-nvme target
1225e399441dSJames Smart  * is present and receives the LS, it too can tear down.
1226e399441dSJames Smart  */
1227e399441dSJames Smart static void
1228e399441dSJames Smart nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1229e399441dSJames Smart {
1230e399441dSJames Smart 	struct fcnvme_ls_disconnect_rqst *discon_rqst;
1231e399441dSJames Smart 	struct fcnvme_ls_disconnect_acc *discon_acc;
1232e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
1233e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
1234c913a8b0SJames Smart 	int ret;
1235e399441dSJames Smart 
1236e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
1237e399441dSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz +
1238e399441dSJames Smart 			 sizeof(*discon_rqst) + sizeof(*discon_acc)),
1239e399441dSJames Smart 			GFP_KERNEL);
1240e399441dSJames Smart 	if (!lsop)
1241e399441dSJames Smart 		/* couldn't sent it... too bad */
1242e399441dSJames Smart 		return;
1243e399441dSJames Smart 
1244e399441dSJames Smart 	lsreq = &lsop->ls_req;
1245e399441dSJames Smart 
1246e399441dSJames Smart 	lsreq->private = (void *)&lsop[1];
1247e399441dSJames Smart 	discon_rqst = (struct fcnvme_ls_disconnect_rqst *)
1248e399441dSJames Smart 			(lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1249e399441dSJames Smart 	discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1];
1250e399441dSJames Smart 
1251e399441dSJames Smart 	discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT;
1252e399441dSJames Smart 	discon_rqst->desc_list_len = cpu_to_be32(
1253e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id) +
1254e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_disconn_cmd));
1255e399441dSJames Smart 
1256e399441dSJames Smart 	discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1257e399441dSJames Smart 	discon_rqst->associd.desc_len =
1258e399441dSJames Smart 			fcnvme_lsdesc_len(
1259e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id));
1260e399441dSJames Smart 
1261e399441dSJames Smart 	discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1262e399441dSJames Smart 
1263e399441dSJames Smart 	discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
1264e399441dSJames Smart 						FCNVME_LSDESC_DISCONN_CMD);
1265e399441dSJames Smart 	discon_rqst->discon_cmd.desc_len =
1266e399441dSJames Smart 			fcnvme_lsdesc_len(
1267e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_disconn_cmd));
1268e399441dSJames Smart 	discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION;
1269e399441dSJames Smart 	discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id);
1270e399441dSJames Smart 
1271e399441dSJames Smart 	lsreq->rqstaddr = discon_rqst;
1272e399441dSJames Smart 	lsreq->rqstlen = sizeof(*discon_rqst);
1273e399441dSJames Smart 	lsreq->rspaddr = discon_acc;
1274e399441dSJames Smart 	lsreq->rsplen = sizeof(*discon_acc);
1275e399441dSJames Smart 	lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1276e399441dSJames Smart 
1277c913a8b0SJames Smart 	ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1278c913a8b0SJames Smart 				nvme_fc_disconnect_assoc_done);
1279c913a8b0SJames Smart 	if (ret)
1280c913a8b0SJames Smart 		kfree(lsop);
1281e399441dSJames Smart 
1282e399441dSJames Smart 	/* only meaningful part to terminating the association */
1283e399441dSJames Smart 	ctrl->association_id = 0;
1284e399441dSJames Smart }
1285e399441dSJames Smart 
1286e399441dSJames Smart 
1287e399441dSJames Smart /* *********************** NVME Ctrl Routines **************************** */
1288e399441dSJames Smart 
128978a7ac26SJames Smart static void __nvme_fc_final_op_cleanup(struct request *rq);
1290f874d5d0SJames Smart static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
1291e399441dSJames Smart 
1292e399441dSJames Smart static int
1293e399441dSJames Smart nvme_fc_reinit_request(void *data, struct request *rq)
1294e399441dSJames Smart {
1295e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1296e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1297e399441dSJames Smart 
1298e399441dSJames Smart 	memset(cmdiu, 0, sizeof(*cmdiu));
1299e399441dSJames Smart 	cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1300e399441dSJames Smart 	cmdiu->fc_id = NVME_CMD_FC_ID;
1301e399441dSJames Smart 	cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1302e399441dSJames Smart 	memset(&op->rsp_iu, 0, sizeof(op->rsp_iu));
1303e399441dSJames Smart 
1304e399441dSJames Smart 	return 0;
1305e399441dSJames Smart }
1306e399441dSJames Smart 
1307e399441dSJames Smart static void
1308e399441dSJames Smart __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1309e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
1310e399441dSJames Smart {
1311e399441dSJames Smart 	fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1312e399441dSJames Smart 				sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1313e399441dSJames Smart 	fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1314e399441dSJames Smart 				sizeof(op->cmd_iu), DMA_TO_DEVICE);
1315e399441dSJames Smart 
1316e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_UNINIT);
1317e399441dSJames Smart }
1318e399441dSJames Smart 
1319e399441dSJames Smart static void
1320d6296d39SChristoph Hellwig nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1321d6296d39SChristoph Hellwig 		unsigned int hctx_idx)
1322e399441dSJames Smart {
1323e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1324e399441dSJames Smart 
1325d6296d39SChristoph Hellwig 	return __nvme_fc_exit_request(set->driver_data, op);
1326e399441dSJames Smart }
1327e399441dSJames Smart 
132878a7ac26SJames Smart static int
132978a7ac26SJames Smart __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
133078a7ac26SJames Smart {
133178a7ac26SJames Smart 	int state;
133278a7ac26SJames Smart 
133378a7ac26SJames Smart 	state = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
133478a7ac26SJames Smart 	if (state != FCPOP_STATE_ACTIVE) {
133578a7ac26SJames Smart 		atomic_set(&op->state, state);
133678a7ac26SJames Smart 		return -ECANCELED;
133778a7ac26SJames Smart 	}
133878a7ac26SJames Smart 
133978a7ac26SJames Smart 	ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
134078a7ac26SJames Smart 					&ctrl->rport->remoteport,
134178a7ac26SJames Smart 					op->queue->lldd_handle,
134278a7ac26SJames Smart 					&op->fcp_req);
134378a7ac26SJames Smart 
134478a7ac26SJames Smart 	return 0;
134578a7ac26SJames Smart }
134678a7ac26SJames Smart 
1347e399441dSJames Smart static void
134878a7ac26SJames Smart nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1349e399441dSJames Smart {
1350e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
135178a7ac26SJames Smart 	unsigned long flags;
135278a7ac26SJames Smart 	int i, ret;
1353e399441dSJames Smart 
1354e399441dSJames Smart 	for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
135578a7ac26SJames Smart 		if (atomic_read(&aen_op->state) != FCPOP_STATE_ACTIVE)
1356e399441dSJames Smart 			continue;
135778a7ac26SJames Smart 
135878a7ac26SJames Smart 		spin_lock_irqsave(&ctrl->lock, flags);
135961bff8efSJames Smart 		if (ctrl->flags & FCCTRL_TERMIO) {
136061bff8efSJames Smart 			ctrl->iocnt++;
136178a7ac26SJames Smart 			aen_op->flags |= FCOP_FLAGS_TERMIO;
136261bff8efSJames Smart 		}
136378a7ac26SJames Smart 		spin_unlock_irqrestore(&ctrl->lock, flags);
136478a7ac26SJames Smart 
136578a7ac26SJames Smart 		ret = __nvme_fc_abort_op(ctrl, aen_op);
136678a7ac26SJames Smart 		if (ret) {
136778a7ac26SJames Smart 			/*
136878a7ac26SJames Smart 			 * if __nvme_fc_abort_op failed the io wasn't
136978a7ac26SJames Smart 			 * active. Thus this call path is running in
137078a7ac26SJames Smart 			 * parallel to the io complete. Treat as non-error.
137178a7ac26SJames Smart 			 */
137278a7ac26SJames Smart 
137378a7ac26SJames Smart 			/* back out the flags/counters */
137478a7ac26SJames Smart 			spin_lock_irqsave(&ctrl->lock, flags);
137561bff8efSJames Smart 			if (ctrl->flags & FCCTRL_TERMIO)
137661bff8efSJames Smart 				ctrl->iocnt--;
137778a7ac26SJames Smart 			aen_op->flags &= ~FCOP_FLAGS_TERMIO;
137878a7ac26SJames Smart 			spin_unlock_irqrestore(&ctrl->lock, flags);
137978a7ac26SJames Smart 			return;
1380e399441dSJames Smart 		}
1381e399441dSJames Smart 	}
138278a7ac26SJames Smart }
138378a7ac26SJames Smart 
138478a7ac26SJames Smart static inline int
138578a7ac26SJames Smart __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
138678a7ac26SJames Smart 		struct nvme_fc_fcp_op *op)
138778a7ac26SJames Smart {
138878a7ac26SJames Smart 	unsigned long flags;
138978a7ac26SJames Smart 	bool complete_rq = false;
139078a7ac26SJames Smart 
139178a7ac26SJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
139261bff8efSJames Smart 	if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) {
139336715cf4SJames Smart 		if (ctrl->flags & FCCTRL_TERMIO) {
139436715cf4SJames Smart 			if (!--ctrl->iocnt)
139536715cf4SJames Smart 				wake_up(&ctrl->ioabort_wait);
139636715cf4SJames Smart 		}
139761bff8efSJames Smart 	}
139878a7ac26SJames Smart 	if (op->flags & FCOP_FLAGS_RELEASED)
139978a7ac26SJames Smart 		complete_rq = true;
140078a7ac26SJames Smart 	else
140178a7ac26SJames Smart 		op->flags |= FCOP_FLAGS_COMPLETE;
140278a7ac26SJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
140378a7ac26SJames Smart 
140478a7ac26SJames Smart 	return complete_rq;
140578a7ac26SJames Smart }
1406e399441dSJames Smart 
1407baee29acSChristoph Hellwig static void
1408e399441dSJames Smart nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1409e399441dSJames Smart {
1410e399441dSJames Smart 	struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1411e399441dSJames Smart 	struct request *rq = op->rq;
1412e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
1413e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
1414e399441dSJames Smart 	struct nvme_fc_queue *queue = op->queue;
1415e399441dSJames Smart 	struct nvme_completion *cqe = &op->rsp_iu.cqe;
1416458f280dSJames Smart 	struct nvme_command *sqe = &op->cmd_iu.sqe;
1417d663b69fSChristoph Hellwig 	__le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
141827fa9bc5SChristoph Hellwig 	union nvme_result result;
14190a02e39fSJames Smart 	bool terminate_assoc = true;
1420e399441dSJames Smart 
1421e399441dSJames Smart 	/*
1422e399441dSJames Smart 	 * WARNING:
1423e399441dSJames Smart 	 * The current linux implementation of a nvme controller
1424e399441dSJames Smart 	 * allocates a single tag set for all io queues and sizes
1425e399441dSJames Smart 	 * the io queues to fully hold all possible tags. Thus, the
1426e399441dSJames Smart 	 * implementation does not reference or care about the sqhd
1427e399441dSJames Smart 	 * value as it never needs to use the sqhd/sqtail pointers
1428e399441dSJames Smart 	 * for submission pacing.
1429e399441dSJames Smart 	 *
1430e399441dSJames Smart 	 * This affects the FC-NVME implementation in two ways:
1431e399441dSJames Smart 	 * 1) As the value doesn't matter, we don't need to waste
1432e399441dSJames Smart 	 *    cycles extracting it from ERSPs and stamping it in the
1433e399441dSJames Smart 	 *    cases where the transport fabricates CQEs on successful
1434e399441dSJames Smart 	 *    completions.
1435e399441dSJames Smart 	 * 2) The FC-NVME implementation requires that delivery of
1436e399441dSJames Smart 	 *    ERSP completions are to go back to the nvme layer in order
1437e399441dSJames Smart 	 *    relative to the rsn, such that the sqhd value will always
1438e399441dSJames Smart 	 *    be "in order" for the nvme layer. As the nvme layer in
1439e399441dSJames Smart 	 *    linux doesn't care about sqhd, there's no need to return
1440e399441dSJames Smart 	 *    them in order.
1441e399441dSJames Smart 	 *
1442e399441dSJames Smart 	 * Additionally:
1443e399441dSJames Smart 	 * As the core nvme layer in linux currently does not look at
1444e399441dSJames Smart 	 * every field in the cqe - in cases where the FC transport must
1445e399441dSJames Smart 	 * fabricate a CQE, the following fields will not be set as they
1446e399441dSJames Smart 	 * are not referenced:
1447e399441dSJames Smart 	 *      cqe.sqid,  cqe.sqhd,  cqe.command_id
1448f874d5d0SJames Smart 	 *
1449f874d5d0SJames Smart 	 * Failure or error of an individual i/o, in a transport
1450f874d5d0SJames Smart 	 * detected fashion unrelated to the nvme completion status,
1451f874d5d0SJames Smart 	 * potentially cause the initiator and target sides to get out
1452f874d5d0SJames Smart 	 * of sync on SQ head/tail (aka outstanding io count allowed).
1453f874d5d0SJames Smart 	 * Per FC-NVME spec, failure of an individual command requires
1454f874d5d0SJames Smart 	 * the connection to be terminated, which in turn requires the
1455f874d5d0SJames Smart 	 * association to be terminated.
1456e399441dSJames Smart 	 */
1457e399441dSJames Smart 
1458e399441dSJames Smart 	fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1459e399441dSJames Smart 				sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1460e399441dSJames Smart 
14610a02e39fSJames Smart 	if (atomic_read(&op->state) == FCPOP_STATE_ABORTED ||
14620a02e39fSJames Smart 			op->flags & FCOP_FLAGS_TERMIO)
14630a02e39fSJames Smart 		status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
146462eeacb0SJames Smart 	else if (freq->status)
146556b7103aSJames Smart 		status = cpu_to_le16(NVME_SC_INTERNAL << 1);
1466e399441dSJames Smart 
1467e399441dSJames Smart 	/*
1468e399441dSJames Smart 	 * For the linux implementation, if we have an unsuccesful
1469e399441dSJames Smart 	 * status, they blk-mq layer can typically be called with the
1470e399441dSJames Smart 	 * non-zero status and the content of the cqe isn't important.
1471e399441dSJames Smart 	 */
1472e399441dSJames Smart 	if (status)
1473e399441dSJames Smart 		goto done;
1474e399441dSJames Smart 
1475e399441dSJames Smart 	/*
1476e399441dSJames Smart 	 * command completed successfully relative to the wire
1477e399441dSJames Smart 	 * protocol. However, validate anything received and
1478e399441dSJames Smart 	 * extract the status and result from the cqe (create it
1479e399441dSJames Smart 	 * where necessary).
1480e399441dSJames Smart 	 */
1481e399441dSJames Smart 
1482e399441dSJames Smart 	switch (freq->rcv_rsplen) {
1483e399441dSJames Smart 
1484e399441dSJames Smart 	case 0:
1485e399441dSJames Smart 	case NVME_FC_SIZEOF_ZEROS_RSP:
1486e399441dSJames Smart 		/*
1487e399441dSJames Smart 		 * No response payload or 12 bytes of payload (which
1488e399441dSJames Smart 		 * should all be zeros) are considered successful and
1489e399441dSJames Smart 		 * no payload in the CQE by the transport.
1490e399441dSJames Smart 		 */
1491e399441dSJames Smart 		if (freq->transferred_length !=
1492e399441dSJames Smart 			be32_to_cpu(op->cmd_iu.data_len)) {
149356b7103aSJames Smart 			status = cpu_to_le16(NVME_SC_INTERNAL << 1);
1494e399441dSJames Smart 			goto done;
1495e399441dSJames Smart 		}
149627fa9bc5SChristoph Hellwig 		result.u64 = 0;
1497e399441dSJames Smart 		break;
1498e399441dSJames Smart 
1499e399441dSJames Smart 	case sizeof(struct nvme_fc_ersp_iu):
1500e399441dSJames Smart 		/*
1501e399441dSJames Smart 		 * The ERSP IU contains a full completion with CQE.
1502e399441dSJames Smart 		 * Validate ERSP IU and look at cqe.
1503e399441dSJames Smart 		 */
1504e399441dSJames Smart 		if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1505e399441dSJames Smart 					(freq->rcv_rsplen / 4) ||
1506e399441dSJames Smart 			     be32_to_cpu(op->rsp_iu.xfrd_len) !=
1507e399441dSJames Smart 					freq->transferred_length ||
1508726a1080SJames Smart 			     op->rsp_iu.status_code ||
1509458f280dSJames Smart 			     sqe->common.command_id != cqe->command_id)) {
151056b7103aSJames Smart 			status = cpu_to_le16(NVME_SC_INTERNAL << 1);
1511e399441dSJames Smart 			goto done;
1512e399441dSJames Smart 		}
151327fa9bc5SChristoph Hellwig 		result = cqe->result;
1514d663b69fSChristoph Hellwig 		status = cqe->status;
1515e399441dSJames Smart 		break;
1516e399441dSJames Smart 
1517e399441dSJames Smart 	default:
151856b7103aSJames Smart 		status = cpu_to_le16(NVME_SC_INTERNAL << 1);
1519e399441dSJames Smart 		goto done;
1520e399441dSJames Smart 	}
1521e399441dSJames Smart 
1522f874d5d0SJames Smart 	terminate_assoc = false;
1523f874d5d0SJames Smart 
1524e399441dSJames Smart done:
152578a7ac26SJames Smart 	if (op->flags & FCOP_FLAGS_AEN) {
152627fa9bc5SChristoph Hellwig 		nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
15270a02e39fSJames Smart 		__nvme_fc_fcpop_chk_teardowns(ctrl, op);
152878a7ac26SJames Smart 		atomic_set(&op->state, FCPOP_STATE_IDLE);
152978a7ac26SJames Smart 		op->flags = FCOP_FLAGS_AEN;	/* clear other flags */
1530e399441dSJames Smart 		nvme_fc_ctrl_put(ctrl);
1531f874d5d0SJames Smart 		goto check_error;
1532e399441dSJames Smart 	}
1533e399441dSJames Smart 
15340a02e39fSJames Smart 	/*
15350a02e39fSJames Smart 	 * Force failures of commands if we're killing the controller
15360a02e39fSJames Smart 	 * or have an error on a command used to create an new association
15370a02e39fSJames Smart 	 */
15380a02e39fSJames Smart 	if (status &&
15390a02e39fSJames Smart 	    (blk_queue_dying(rq->q) ||
15400a02e39fSJames Smart 	     ctrl->ctrl.state == NVME_CTRL_NEW ||
15410a02e39fSJames Smart 	     ctrl->ctrl.state == NVME_CTRL_RECONNECTING))
1542e392e1f1SJames Smart 		status |= cpu_to_le16(NVME_SC_DNR << 1);
15430a02e39fSJames Smart 
15440a02e39fSJames Smart 	if (__nvme_fc_fcpop_chk_teardowns(ctrl, op))
154578a7ac26SJames Smart 		__nvme_fc_final_op_cleanup(rq);
15460a02e39fSJames Smart 	else
15470a02e39fSJames Smart 		nvme_end_request(rq, status, result);
1548f874d5d0SJames Smart 
1549f874d5d0SJames Smart check_error:
1550f874d5d0SJames Smart 	if (terminate_assoc)
1551f874d5d0SJames Smart 		nvme_fc_error_recovery(ctrl, "transport detected io error");
1552e399441dSJames Smart }
1553e399441dSJames Smart 
1554e399441dSJames Smart static int
1555e399441dSJames Smart __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
1556e399441dSJames Smart 		struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
1557e399441dSJames Smart 		struct request *rq, u32 rqno)
1558e399441dSJames Smart {
1559e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1560e399441dSJames Smart 	int ret = 0;
1561e399441dSJames Smart 
1562e399441dSJames Smart 	memset(op, 0, sizeof(*op));
1563e399441dSJames Smart 	op->fcp_req.cmdaddr = &op->cmd_iu;
1564e399441dSJames Smart 	op->fcp_req.cmdlen = sizeof(op->cmd_iu);
1565e399441dSJames Smart 	op->fcp_req.rspaddr = &op->rsp_iu;
1566e399441dSJames Smart 	op->fcp_req.rsplen = sizeof(op->rsp_iu);
1567e399441dSJames Smart 	op->fcp_req.done = nvme_fc_fcpio_done;
1568e399441dSJames Smart 	op->fcp_req.first_sgl = (struct scatterlist *)&op[1];
1569e399441dSJames Smart 	op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE];
1570e399441dSJames Smart 	op->ctrl = ctrl;
1571e399441dSJames Smart 	op->queue = queue;
1572e399441dSJames Smart 	op->rq = rq;
1573e399441dSJames Smart 	op->rqno = rqno;
1574e399441dSJames Smart 
1575e399441dSJames Smart 	cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1576e399441dSJames Smart 	cmdiu->fc_id = NVME_CMD_FC_ID;
1577e399441dSJames Smart 	cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1578e399441dSJames Smart 
1579e399441dSJames Smart 	op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
1580e399441dSJames Smart 				&op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
1581e399441dSJames Smart 	if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
1582e399441dSJames Smart 		dev_err(ctrl->dev,
1583e399441dSJames Smart 			"FCP Op failed - cmdiu dma mapping failed.\n");
1584e399441dSJames Smart 		ret = EFAULT;
1585e399441dSJames Smart 		goto out_on_error;
1586e399441dSJames Smart 	}
1587e399441dSJames Smart 
1588e399441dSJames Smart 	op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
1589e399441dSJames Smart 				&op->rsp_iu, sizeof(op->rsp_iu),
1590e399441dSJames Smart 				DMA_FROM_DEVICE);
1591e399441dSJames Smart 	if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
1592e399441dSJames Smart 		dev_err(ctrl->dev,
1593e399441dSJames Smart 			"FCP Op failed - rspiu dma mapping failed.\n");
1594e399441dSJames Smart 		ret = EFAULT;
1595e399441dSJames Smart 	}
1596e399441dSJames Smart 
1597e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_IDLE);
1598e399441dSJames Smart out_on_error:
1599e399441dSJames Smart 	return ret;
1600e399441dSJames Smart }
1601e399441dSJames Smart 
1602e399441dSJames Smart static int
1603d6296d39SChristoph Hellwig nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
1604d6296d39SChristoph Hellwig 		unsigned int hctx_idx, unsigned int numa_node)
1605e399441dSJames Smart {
1606d6296d39SChristoph Hellwig 	struct nvme_fc_ctrl *ctrl = set->driver_data;
1607e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
160876f983cbSChristoph Hellwig 	int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
160976f983cbSChristoph Hellwig 	struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
1610e399441dSJames Smart 
1611e399441dSJames Smart 	return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
1612e399441dSJames Smart }
1613e399441dSJames Smart 
1614e399441dSJames Smart static int
1615e399441dSJames Smart nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
1616e399441dSJames Smart {
1617e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op;
1618e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu;
1619e399441dSJames Smart 	struct nvme_command *sqe;
162061bff8efSJames Smart 	void *private;
1621e399441dSJames Smart 	int i, ret;
1622e399441dSJames Smart 
1623e399441dSJames Smart 	aen_op = ctrl->aen_ops;
1624e399441dSJames Smart 	for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
162561bff8efSJames Smart 		private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
162661bff8efSJames Smart 						GFP_KERNEL);
162761bff8efSJames Smart 		if (!private)
162861bff8efSJames Smart 			return -ENOMEM;
162961bff8efSJames Smart 
1630e399441dSJames Smart 		cmdiu = &aen_op->cmd_iu;
1631e399441dSJames Smart 		sqe = &cmdiu->sqe;
1632e399441dSJames Smart 		ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
1633e399441dSJames Smart 				aen_op, (struct request *)NULL,
1634e399441dSJames Smart 				(AEN_CMDID_BASE + i));
163561bff8efSJames Smart 		if (ret) {
163661bff8efSJames Smart 			kfree(private);
1637e399441dSJames Smart 			return ret;
163861bff8efSJames Smart 		}
1639e399441dSJames Smart 
164078a7ac26SJames Smart 		aen_op->flags = FCOP_FLAGS_AEN;
164161bff8efSJames Smart 		aen_op->fcp_req.first_sgl = NULL; /* no sg list */
164261bff8efSJames Smart 		aen_op->fcp_req.private = private;
164378a7ac26SJames Smart 
1644e399441dSJames Smart 		memset(sqe, 0, sizeof(*sqe));
1645e399441dSJames Smart 		sqe->common.opcode = nvme_admin_async_event;
164678a7ac26SJames Smart 		/* Note: core layer may overwrite the sqe.command_id value */
1647e399441dSJames Smart 		sqe->common.command_id = AEN_CMDID_BASE + i;
1648e399441dSJames Smart 	}
1649e399441dSJames Smart 	return 0;
1650e399441dSJames Smart }
1651e399441dSJames Smart 
165261bff8efSJames Smart static void
165361bff8efSJames Smart nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
165461bff8efSJames Smart {
165561bff8efSJames Smart 	struct nvme_fc_fcp_op *aen_op;
165661bff8efSJames Smart 	int i;
165761bff8efSJames Smart 
165861bff8efSJames Smart 	aen_op = ctrl->aen_ops;
165961bff8efSJames Smart 	for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
166061bff8efSJames Smart 		if (!aen_op->fcp_req.private)
166161bff8efSJames Smart 			continue;
166261bff8efSJames Smart 
166361bff8efSJames Smart 		__nvme_fc_exit_request(ctrl, aen_op);
166461bff8efSJames Smart 
166561bff8efSJames Smart 		kfree(aen_op->fcp_req.private);
166661bff8efSJames Smart 		aen_op->fcp_req.private = NULL;
166761bff8efSJames Smart 	}
166861bff8efSJames Smart }
1669e399441dSJames Smart 
1670e399441dSJames Smart static inline void
1671e399441dSJames Smart __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
1672e399441dSJames Smart 		unsigned int qidx)
1673e399441dSJames Smart {
1674e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[qidx];
1675e399441dSJames Smart 
1676e399441dSJames Smart 	hctx->driver_data = queue;
1677e399441dSJames Smart 	queue->hctx = hctx;
1678e399441dSJames Smart }
1679e399441dSJames Smart 
1680e399441dSJames Smart static int
1681e399441dSJames Smart nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1682e399441dSJames Smart 		unsigned int hctx_idx)
1683e399441dSJames Smart {
1684e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = data;
1685e399441dSJames Smart 
1686e399441dSJames Smart 	__nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
1687e399441dSJames Smart 
1688e399441dSJames Smart 	return 0;
1689e399441dSJames Smart }
1690e399441dSJames Smart 
1691e399441dSJames Smart static int
1692e399441dSJames Smart nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1693e399441dSJames Smart 		unsigned int hctx_idx)
1694e399441dSJames Smart {
1695e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = data;
1696e399441dSJames Smart 
1697e399441dSJames Smart 	__nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
1698e399441dSJames Smart 
1699e399441dSJames Smart 	return 0;
1700e399441dSJames Smart }
1701e399441dSJames Smart 
1702e399441dSJames Smart static void
1703e399441dSJames Smart nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx, size_t queue_size)
1704e399441dSJames Smart {
1705e399441dSJames Smart 	struct nvme_fc_queue *queue;
1706e399441dSJames Smart 
1707e399441dSJames Smart 	queue = &ctrl->queues[idx];
1708e399441dSJames Smart 	memset(queue, 0, sizeof(*queue));
1709e399441dSJames Smart 	queue->ctrl = ctrl;
1710e399441dSJames Smart 	queue->qnum = idx;
1711e399441dSJames Smart 	atomic_set(&queue->csn, 1);
1712e399441dSJames Smart 	queue->dev = ctrl->dev;
1713e399441dSJames Smart 
1714e399441dSJames Smart 	if (idx > 0)
1715e399441dSJames Smart 		queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1716e399441dSJames Smart 	else
1717e399441dSJames Smart 		queue->cmnd_capsule_len = sizeof(struct nvme_command);
1718e399441dSJames Smart 
1719e399441dSJames Smart 	queue->queue_size = queue_size;
1720e399441dSJames Smart 
1721e399441dSJames Smart 	/*
1722e399441dSJames Smart 	 * Considered whether we should allocate buffers for all SQEs
1723e399441dSJames Smart 	 * and CQEs and dma map them - mapping their respective entries
1724e399441dSJames Smart 	 * into the request structures (kernel vm addr and dma address)
1725e399441dSJames Smart 	 * thus the driver could use the buffers/mappings directly.
1726e399441dSJames Smart 	 * It only makes sense if the LLDD would use them for its
1727e399441dSJames Smart 	 * messaging api. It's very unlikely most adapter api's would use
1728e399441dSJames Smart 	 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1729e399441dSJames Smart 	 * structures were used instead.
1730e399441dSJames Smart 	 */
1731e399441dSJames Smart }
1732e399441dSJames Smart 
1733e399441dSJames Smart /*
1734e399441dSJames Smart  * This routine terminates a queue at the transport level.
1735e399441dSJames Smart  * The transport has already ensured that all outstanding ios on
1736e399441dSJames Smart  * the queue have been terminated.
1737e399441dSJames Smart  * The transport will send a Disconnect LS request to terminate
1738e399441dSJames Smart  * the queue's connection. Termination of the admin queue will also
1739e399441dSJames Smart  * terminate the association at the target.
1740e399441dSJames Smart  */
1741e399441dSJames Smart static void
1742e399441dSJames Smart nvme_fc_free_queue(struct nvme_fc_queue *queue)
1743e399441dSJames Smart {
1744e399441dSJames Smart 	if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
1745e399441dSJames Smart 		return;
1746e399441dSJames Smart 
1747e399441dSJames Smart 	/*
1748e399441dSJames Smart 	 * Current implementation never disconnects a single queue.
1749e399441dSJames Smart 	 * It always terminates a whole association. So there is never
1750e399441dSJames Smart 	 * a disconnect(queue) LS sent to the target.
1751e399441dSJames Smart 	 */
1752e399441dSJames Smart 
1753e399441dSJames Smart 	queue->connection_id = 0;
1754e399441dSJames Smart 	clear_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1755e399441dSJames Smart }
1756e399441dSJames Smart 
1757e399441dSJames Smart static void
1758e399441dSJames Smart __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
1759e399441dSJames Smart 	struct nvme_fc_queue *queue, unsigned int qidx)
1760e399441dSJames Smart {
1761e399441dSJames Smart 	if (ctrl->lport->ops->delete_queue)
1762e399441dSJames Smart 		ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
1763e399441dSJames Smart 				queue->lldd_handle);
1764e399441dSJames Smart 	queue->lldd_handle = NULL;
1765e399441dSJames Smart }
1766e399441dSJames Smart 
1767e399441dSJames Smart static void
1768e399441dSJames Smart nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
1769e399441dSJames Smart {
1770e399441dSJames Smart 	int i;
1771e399441dSJames Smart 
1772d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
1773e399441dSJames Smart 		nvme_fc_free_queue(&ctrl->queues[i]);
1774e399441dSJames Smart }
1775e399441dSJames Smart 
1776e399441dSJames Smart static int
1777e399441dSJames Smart __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
1778e399441dSJames Smart 	struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
1779e399441dSJames Smart {
1780e399441dSJames Smart 	int ret = 0;
1781e399441dSJames Smart 
1782e399441dSJames Smart 	queue->lldd_handle = NULL;
1783e399441dSJames Smart 	if (ctrl->lport->ops->create_queue)
1784e399441dSJames Smart 		ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
1785e399441dSJames Smart 				qidx, qsize, &queue->lldd_handle);
1786e399441dSJames Smart 
1787e399441dSJames Smart 	return ret;
1788e399441dSJames Smart }
1789e399441dSJames Smart 
1790e399441dSJames Smart static void
1791e399441dSJames Smart nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
1792e399441dSJames Smart {
1793d858e5f0SSagi Grimberg 	struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
1794e399441dSJames Smart 	int i;
1795e399441dSJames Smart 
1796d858e5f0SSagi Grimberg 	for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
1797e399441dSJames Smart 		__nvme_fc_delete_hw_queue(ctrl, queue, i);
1798e399441dSJames Smart }
1799e399441dSJames Smart 
1800e399441dSJames Smart static int
1801e399441dSJames Smart nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1802e399441dSJames Smart {
1803e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[1];
180417a1ec08SJohannes Thumshirn 	int i, ret;
1805e399441dSJames Smart 
1806d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
1807e399441dSJames Smart 		ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
180817a1ec08SJohannes Thumshirn 		if (ret)
180917a1ec08SJohannes Thumshirn 			goto delete_queues;
1810e399441dSJames Smart 	}
1811e399441dSJames Smart 
1812e399441dSJames Smart 	return 0;
181317a1ec08SJohannes Thumshirn 
181417a1ec08SJohannes Thumshirn delete_queues:
181517a1ec08SJohannes Thumshirn 	for (; i >= 0; i--)
181617a1ec08SJohannes Thumshirn 		__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
181717a1ec08SJohannes Thumshirn 	return ret;
1818e399441dSJames Smart }
1819e399441dSJames Smart 
1820e399441dSJames Smart static int
1821e399441dSJames Smart nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1822e399441dSJames Smart {
1823e399441dSJames Smart 	int i, ret = 0;
1824e399441dSJames Smart 
1825d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
1826e399441dSJames Smart 		ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
1827e399441dSJames Smart 					(qsize / 5));
1828e399441dSJames Smart 		if (ret)
1829e399441dSJames Smart 			break;
1830e399441dSJames Smart 		ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
1831e399441dSJames Smart 		if (ret)
1832e399441dSJames Smart 			break;
1833e399441dSJames Smart 	}
1834e399441dSJames Smart 
1835e399441dSJames Smart 	return ret;
1836e399441dSJames Smart }
1837e399441dSJames Smart 
1838e399441dSJames Smart static void
1839e399441dSJames Smart nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
1840e399441dSJames Smart {
1841e399441dSJames Smart 	int i;
1842e399441dSJames Smart 
1843d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
1844e399441dSJames Smart 		nvme_fc_init_queue(ctrl, i, ctrl->ctrl.sqsize);
1845e399441dSJames Smart }
1846e399441dSJames Smart 
1847e399441dSJames Smart static void
1848e399441dSJames Smart nvme_fc_ctrl_free(struct kref *ref)
1849e399441dSJames Smart {
1850e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl =
1851e399441dSJames Smart 		container_of(ref, struct nvme_fc_ctrl, ref);
1852e399441dSJames Smart 	unsigned long flags;
1853e399441dSJames Smart 
185461bff8efSJames Smart 	if (ctrl->ctrl.tagset) {
185561bff8efSJames Smart 		blk_cleanup_queue(ctrl->ctrl.connect_q);
185661bff8efSJames Smart 		blk_mq_free_tag_set(&ctrl->tag_set);
185761bff8efSJames Smart 	}
185861bff8efSJames Smart 
1859e399441dSJames Smart 	/* remove from rport list */
1860e399441dSJames Smart 	spin_lock_irqsave(&ctrl->rport->lock, flags);
1861e399441dSJames Smart 	list_del(&ctrl->ctrl_list);
1862e399441dSJames Smart 	spin_unlock_irqrestore(&ctrl->rport->lock, flags);
186361bff8efSJames Smart 
1864f9c5af5fSSagi Grimberg 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
186561bff8efSJames Smart 	blk_cleanup_queue(ctrl->ctrl.admin_q);
186661bff8efSJames Smart 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
186761bff8efSJames Smart 
186861bff8efSJames Smart 	kfree(ctrl->queues);
1869e399441dSJames Smart 
1870e399441dSJames Smart 	put_device(ctrl->dev);
1871e399441dSJames Smart 	nvme_fc_rport_put(ctrl->rport);
1872e399441dSJames Smart 
1873e399441dSJames Smart 	ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
1874de41447aSEwan D. Milne 	if (ctrl->ctrl.opts)
1875e399441dSJames Smart 		nvmf_free_options(ctrl->ctrl.opts);
1876e399441dSJames Smart 	kfree(ctrl);
1877e399441dSJames Smart }
1878e399441dSJames Smart 
1879e399441dSJames Smart static void
1880e399441dSJames Smart nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
1881e399441dSJames Smart {
1882e399441dSJames Smart 	kref_put(&ctrl->ref, nvme_fc_ctrl_free);
1883e399441dSJames Smart }
1884e399441dSJames Smart 
1885e399441dSJames Smart static int
1886e399441dSJames Smart nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
1887e399441dSJames Smart {
1888e399441dSJames Smart 	return kref_get_unless_zero(&ctrl->ref);
1889e399441dSJames Smart }
1890e399441dSJames Smart 
1891e399441dSJames Smart /*
1892e399441dSJames Smart  * All accesses from nvme core layer done - can now free the
1893e399441dSJames Smart  * controller. Called after last nvme_put_ctrl() call
1894e399441dSJames Smart  */
1895e399441dSJames Smart static void
189661bff8efSJames Smart nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
1897e399441dSJames Smart {
1898e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
1899e399441dSJames Smart 
1900e399441dSJames Smart 	WARN_ON(nctrl != &ctrl->ctrl);
1901e399441dSJames Smart 
1902e399441dSJames Smart 	nvme_fc_ctrl_put(ctrl);
1903e399441dSJames Smart }
1904e399441dSJames Smart 
190561bff8efSJames Smart static void
190661bff8efSJames Smart nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
190761bff8efSJames Smart {
190869fa9646SJames Smart 	/* only proceed if in LIVE state - e.g. on first error */
190969fa9646SJames Smart 	if (ctrl->ctrl.state != NVME_CTRL_LIVE)
191069fa9646SJames Smart 		return;
191169fa9646SJames Smart 
191261bff8efSJames Smart 	dev_warn(ctrl->ctrl.device,
191361bff8efSJames Smart 		"NVME-FC{%d}: transport association error detected: %s\n",
191461bff8efSJames Smart 		ctrl->cnum, errmsg);
1915589ff775SJames Smart 	dev_warn(ctrl->ctrl.device,
191661bff8efSJames Smart 		"NVME-FC{%d}: resetting controller\n", ctrl->cnum);
191761bff8efSJames Smart 
1918d86c4d8eSChristoph Hellwig 	nvme_reset_ctrl(&ctrl->ctrl);
191961bff8efSJames Smart }
192061bff8efSJames Smart 
1921baee29acSChristoph Hellwig static enum blk_eh_timer_return
1922e399441dSJames Smart nvme_fc_timeout(struct request *rq, bool reserved)
1923e399441dSJames Smart {
1924e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1925e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
1926e399441dSJames Smart 	int ret;
1927e399441dSJames Smart 
1928134aedc9SJames Smart 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
1929134aedc9SJames Smart 			atomic_read(&op->state) == FCPOP_STATE_ABORTED)
1930e399441dSJames Smart 		return BLK_EH_RESET_TIMER;
1931e399441dSJames Smart 
1932e399441dSJames Smart 	ret = __nvme_fc_abort_op(ctrl, op);
1933e399441dSJames Smart 	if (ret)
1934134aedc9SJames Smart 		/* io wasn't active to abort */
1935134aedc9SJames Smart 		return BLK_EH_NOT_HANDLED;
1936e399441dSJames Smart 
1937e399441dSJames Smart 	/*
193861bff8efSJames Smart 	 * we can't individually ABTS an io without affecting the queue,
193961bff8efSJames Smart 	 * thus killing the queue, adn thus the association.
194061bff8efSJames Smart 	 * So resolve by performing a controller reset, which will stop
194161bff8efSJames Smart 	 * the host/io stack, terminate the association on the link,
194261bff8efSJames Smart 	 * and recreate an association on the link.
1943e399441dSJames Smart 	 */
194461bff8efSJames Smart 	nvme_fc_error_recovery(ctrl, "io timeout error");
1945e399441dSJames Smart 
1946134aedc9SJames Smart 	/*
1947134aedc9SJames Smart 	 * the io abort has been initiated. Have the reset timer
1948134aedc9SJames Smart 	 * restarted and the abort completion will complete the io
1949134aedc9SJames Smart 	 * shortly. Avoids a synchronous wait while the abort finishes.
1950134aedc9SJames Smart 	 */
1951134aedc9SJames Smart 	return BLK_EH_RESET_TIMER;
1952e399441dSJames Smart }
1953e399441dSJames Smart 
1954e399441dSJames Smart static int
1955e399441dSJames Smart nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1956e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
1957e399441dSJames Smart {
1958e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
1959e399441dSJames Smart 	enum dma_data_direction dir;
1960e399441dSJames Smart 	int ret;
1961e399441dSJames Smart 
1962e399441dSJames Smart 	freq->sg_cnt = 0;
1963e399441dSJames Smart 
1964b131c61dSChristoph Hellwig 	if (!blk_rq_payload_bytes(rq))
1965e399441dSJames Smart 		return 0;
1966e399441dSJames Smart 
1967e399441dSJames Smart 	freq->sg_table.sgl = freq->first_sgl;
196819e420bbSChristoph Hellwig 	ret = sg_alloc_table_chained(&freq->sg_table,
196919e420bbSChristoph Hellwig 			blk_rq_nr_phys_segments(rq), freq->sg_table.sgl);
1970e399441dSJames Smart 	if (ret)
1971e399441dSJames Smart 		return -ENOMEM;
1972e399441dSJames Smart 
1973e399441dSJames Smart 	op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
197419e420bbSChristoph Hellwig 	WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
1975e399441dSJames Smart 	dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1976e399441dSJames Smart 	freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
1977e399441dSJames Smart 				op->nents, dir);
1978e399441dSJames Smart 	if (unlikely(freq->sg_cnt <= 0)) {
1979e399441dSJames Smart 		sg_free_table_chained(&freq->sg_table, true);
1980e399441dSJames Smart 		freq->sg_cnt = 0;
1981e399441dSJames Smart 		return -EFAULT;
1982e399441dSJames Smart 	}
1983e399441dSJames Smart 
1984e399441dSJames Smart 	/*
1985e399441dSJames Smart 	 * TODO: blk_integrity_rq(rq)  for DIF
1986e399441dSJames Smart 	 */
1987e399441dSJames Smart 	return 0;
1988e399441dSJames Smart }
1989e399441dSJames Smart 
1990e399441dSJames Smart static void
1991e399441dSJames Smart nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1992e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
1993e399441dSJames Smart {
1994e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
1995e399441dSJames Smart 
1996e399441dSJames Smart 	if (!freq->sg_cnt)
1997e399441dSJames Smart 		return;
1998e399441dSJames Smart 
1999e399441dSJames Smart 	fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2000e399441dSJames Smart 				((rq_data_dir(rq) == WRITE) ?
2001e399441dSJames Smart 					DMA_TO_DEVICE : DMA_FROM_DEVICE));
2002e399441dSJames Smart 
2003e399441dSJames Smart 	nvme_cleanup_cmd(rq);
2004e399441dSJames Smart 
2005e399441dSJames Smart 	sg_free_table_chained(&freq->sg_table, true);
2006e399441dSJames Smart 
2007e399441dSJames Smart 	freq->sg_cnt = 0;
2008e399441dSJames Smart }
2009e399441dSJames Smart 
2010e399441dSJames Smart /*
2011e399441dSJames Smart  * In FC, the queue is a logical thing. At transport connect, the target
2012e399441dSJames Smart  * creates its "queue" and returns a handle that is to be given to the
2013e399441dSJames Smart  * target whenever it posts something to the corresponding SQ.  When an
2014e399441dSJames Smart  * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2015e399441dSJames Smart  * command contained within the SQE, an io, and assigns a FC exchange
2016e399441dSJames Smart  * to it. The SQE and the associated SQ handle are sent in the initial
2017e399441dSJames Smart  * CMD IU sents on the exchange. All transfers relative to the io occur
2018e399441dSJames Smart  * as part of the exchange.  The CQE is the last thing for the io,
2019e399441dSJames Smart  * which is transferred (explicitly or implicitly) with the RSP IU
2020e399441dSJames Smart  * sent on the exchange. After the CQE is received, the FC exchange is
2021e399441dSJames Smart  * terminaed and the Exchange may be used on a different io.
2022e399441dSJames Smart  *
2023e399441dSJames Smart  * The transport to LLDD api has the transport making a request for a
2024e399441dSJames Smart  * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2025e399441dSJames Smart  * resource and transfers the command. The LLDD will then process all
2026e399441dSJames Smart  * steps to complete the io. Upon completion, the transport done routine
2027e399441dSJames Smart  * is called.
2028e399441dSJames Smart  *
2029e399441dSJames Smart  * So - while the operation is outstanding to the LLDD, there is a link
2030e399441dSJames Smart  * level FC exchange resource that is also outstanding. This must be
2031e399441dSJames Smart  * considered in all cleanup operations.
2032e399441dSJames Smart  */
2033fc17b653SChristoph Hellwig static blk_status_t
2034e399441dSJames Smart nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2035e399441dSJames Smart 	struct nvme_fc_fcp_op *op, u32 data_len,
2036e399441dSJames Smart 	enum nvmefc_fcp_datadir	io_dir)
2037e399441dSJames Smart {
2038e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2039e399441dSJames Smart 	struct nvme_command *sqe = &cmdiu->sqe;
2040e399441dSJames Smart 	u32 csn;
2041e399441dSJames Smart 	int ret;
2042e399441dSJames Smart 
204361bff8efSJames Smart 	/*
204461bff8efSJames Smart 	 * before attempting to send the io, check to see if we believe
204561bff8efSJames Smart 	 * the target device is present
204661bff8efSJames Smart 	 */
204761bff8efSJames Smart 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
20488b25f351SJames Smart 		goto busy;
204961bff8efSJames Smart 
2050e399441dSJames Smart 	if (!nvme_fc_ctrl_get(ctrl))
2051fc17b653SChristoph Hellwig 		return BLK_STS_IOERR;
2052e399441dSJames Smart 
2053e399441dSJames Smart 	/* format the FC-NVME CMD IU and fcp_req */
2054e399441dSJames Smart 	cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2055e399441dSJames Smart 	csn = atomic_inc_return(&queue->csn);
2056e399441dSJames Smart 	cmdiu->csn = cpu_to_be32(csn);
2057e399441dSJames Smart 	cmdiu->data_len = cpu_to_be32(data_len);
2058e399441dSJames Smart 	switch (io_dir) {
2059e399441dSJames Smart 	case NVMEFC_FCP_WRITE:
2060e399441dSJames Smart 		cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2061e399441dSJames Smart 		break;
2062e399441dSJames Smart 	case NVMEFC_FCP_READ:
2063e399441dSJames Smart 		cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2064e399441dSJames Smart 		break;
2065e399441dSJames Smart 	case NVMEFC_FCP_NODATA:
2066e399441dSJames Smart 		cmdiu->flags = 0;
2067e399441dSJames Smart 		break;
2068e399441dSJames Smart 	}
2069e399441dSJames Smart 	op->fcp_req.payload_length = data_len;
2070e399441dSJames Smart 	op->fcp_req.io_dir = io_dir;
2071e399441dSJames Smart 	op->fcp_req.transferred_length = 0;
2072e399441dSJames Smart 	op->fcp_req.rcv_rsplen = 0;
207362eeacb0SJames Smart 	op->fcp_req.status = NVME_SC_SUCCESS;
2074e399441dSJames Smart 	op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2075e399441dSJames Smart 
2076e399441dSJames Smart 	/*
2077e399441dSJames Smart 	 * validate per fabric rules, set fields mandated by fabric spec
2078e399441dSJames Smart 	 * as well as those by FC-NVME spec.
2079e399441dSJames Smart 	 */
2080e399441dSJames Smart 	WARN_ON_ONCE(sqe->common.metadata);
2081e399441dSJames Smart 	sqe->common.flags |= NVME_CMD_SGL_METABUF;
2082e399441dSJames Smart 
2083e399441dSJames Smart 	/*
2084d9d34c0bSJames Smart 	 * format SQE DPTR field per FC-NVME rules:
2085d9d34c0bSJames Smart 	 *    type=0x5     Transport SGL Data Block Descriptor
2086d9d34c0bSJames Smart 	 *    subtype=0xA  Transport-specific value
2087d9d34c0bSJames Smart 	 *    address=0
2088d9d34c0bSJames Smart 	 *    length=length of the data series
2089e399441dSJames Smart 	 */
2090d9d34c0bSJames Smart 	sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2091d9d34c0bSJames Smart 					NVME_SGL_FMT_TRANSPORT_A;
2092e399441dSJames Smart 	sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2093e399441dSJames Smart 	sqe->rw.dptr.sgl.addr = 0;
2094e399441dSJames Smart 
209578a7ac26SJames Smart 	if (!(op->flags & FCOP_FLAGS_AEN)) {
2096e399441dSJames Smart 		ret = nvme_fc_map_data(ctrl, op->rq, op);
2097e399441dSJames Smart 		if (ret < 0) {
2098e399441dSJames Smart 			nvme_cleanup_cmd(op->rq);
2099e399441dSJames Smart 			nvme_fc_ctrl_put(ctrl);
2100fc17b653SChristoph Hellwig 			if (ret == -ENOMEM || ret == -EAGAIN)
2101fc17b653SChristoph Hellwig 				return BLK_STS_RESOURCE;
2102fc17b653SChristoph Hellwig 			return BLK_STS_IOERR;
2103e399441dSJames Smart 		}
2104e399441dSJames Smart 	}
2105e399441dSJames Smart 
2106e399441dSJames Smart 	fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2107e399441dSJames Smart 				  sizeof(op->cmd_iu), DMA_TO_DEVICE);
2108e399441dSJames Smart 
2109e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2110e399441dSJames Smart 
211178a7ac26SJames Smart 	if (!(op->flags & FCOP_FLAGS_AEN))
2112e399441dSJames Smart 		blk_mq_start_request(op->rq);
2113e399441dSJames Smart 
2114e399441dSJames Smart 	ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2115e399441dSJames Smart 					&ctrl->rport->remoteport,
2116e399441dSJames Smart 					queue->lldd_handle, &op->fcp_req);
2117e399441dSJames Smart 
2118e399441dSJames Smart 	if (ret) {
21198b25f351SJames Smart 		if (!(op->flags & FCOP_FLAGS_AEN))
2120e399441dSJames Smart 			nvme_fc_unmap_data(ctrl, op->rq, op);
2121e399441dSJames Smart 
2122e399441dSJames Smart 		nvme_fc_ctrl_put(ctrl);
2123e399441dSJames Smart 
21248b25f351SJames Smart 		if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
21258b25f351SJames Smart 				ret != -EBUSY)
2126fc17b653SChristoph Hellwig 			return BLK_STS_IOERR;
2127e399441dSJames Smart 
21288b25f351SJames Smart 		goto busy;
2129e399441dSJames Smart 	}
2130e399441dSJames Smart 
2131fc17b653SChristoph Hellwig 	return BLK_STS_OK;
21328b25f351SJames Smart 
21338b25f351SJames Smart busy:
21348b25f351SJames Smart 	if (!(op->flags & FCOP_FLAGS_AEN) && queue->hctx)
21358b25f351SJames Smart 		blk_mq_delay_run_hw_queue(queue->hctx, NVMEFC_QUEUE_DELAY);
21368b25f351SJames Smart 
21378b25f351SJames Smart 	return BLK_STS_RESOURCE;
2138e399441dSJames Smart }
2139e399441dSJames Smart 
2140fc17b653SChristoph Hellwig static blk_status_t
2141e399441dSJames Smart nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2142e399441dSJames Smart 			const struct blk_mq_queue_data *bd)
2143e399441dSJames Smart {
2144e399441dSJames Smart 	struct nvme_ns *ns = hctx->queue->queuedata;
2145e399441dSJames Smart 	struct nvme_fc_queue *queue = hctx->driver_data;
2146e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = queue->ctrl;
2147e399441dSJames Smart 	struct request *rq = bd->rq;
2148e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2149e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2150e399441dSJames Smart 	struct nvme_command *sqe = &cmdiu->sqe;
2151e399441dSJames Smart 	enum nvmefc_fcp_datadir	io_dir;
2152e399441dSJames Smart 	u32 data_len;
2153fc17b653SChristoph Hellwig 	blk_status_t ret;
2154e399441dSJames Smart 
2155e399441dSJames Smart 	ret = nvme_setup_cmd(ns, rq, sqe);
2156e399441dSJames Smart 	if (ret)
2157e399441dSJames Smart 		return ret;
2158e399441dSJames Smart 
2159b131c61dSChristoph Hellwig 	data_len = blk_rq_payload_bytes(rq);
2160e399441dSJames Smart 	if (data_len)
2161e399441dSJames Smart 		io_dir = ((rq_data_dir(rq) == WRITE) ?
2162e399441dSJames Smart 					NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2163e399441dSJames Smart 	else
2164e399441dSJames Smart 		io_dir = NVMEFC_FCP_NODATA;
2165e399441dSJames Smart 
2166e399441dSJames Smart 	return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2167e399441dSJames Smart }
2168e399441dSJames Smart 
2169e399441dSJames Smart static struct blk_mq_tags *
2170e399441dSJames Smart nvme_fc_tagset(struct nvme_fc_queue *queue)
2171e399441dSJames Smart {
2172e399441dSJames Smart 	if (queue->qnum == 0)
2173e399441dSJames Smart 		return queue->ctrl->admin_tag_set.tags[queue->qnum];
2174e399441dSJames Smart 
2175e399441dSJames Smart 	return queue->ctrl->tag_set.tags[queue->qnum - 1];
2176e399441dSJames Smart }
2177e399441dSJames Smart 
2178e399441dSJames Smart static int
2179e399441dSJames Smart nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
2180e399441dSJames Smart 
2181e399441dSJames Smart {
2182e399441dSJames Smart 	struct nvme_fc_queue *queue = hctx->driver_data;
2183e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = queue->ctrl;
2184e399441dSJames Smart 	struct request *req;
2185e399441dSJames Smart 	struct nvme_fc_fcp_op *op;
2186e399441dSJames Smart 
2187e399441dSJames Smart 	req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag);
218861bff8efSJames Smart 	if (!req)
2189e399441dSJames Smart 		return 0;
2190e399441dSJames Smart 
2191e399441dSJames Smart 	op = blk_mq_rq_to_pdu(req);
2192e399441dSJames Smart 
2193e399441dSJames Smart 	if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) &&
2194e399441dSJames Smart 		 (ctrl->lport->ops->poll_queue))
2195e399441dSJames Smart 		ctrl->lport->ops->poll_queue(&ctrl->lport->localport,
2196e399441dSJames Smart 						 queue->lldd_handle);
2197e399441dSJames Smart 
2198e399441dSJames Smart 	return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE));
2199e399441dSJames Smart }
2200e399441dSJames Smart 
2201e399441dSJames Smart static void
2202e399441dSJames Smart nvme_fc_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
2203e399441dSJames Smart {
2204e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2205e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op;
220661bff8efSJames Smart 	unsigned long flags;
220761bff8efSJames Smart 	bool terminating = false;
2208fc17b653SChristoph Hellwig 	blk_status_t ret;
2209e399441dSJames Smart 
2210e399441dSJames Smart 	if (aer_idx > NVME_FC_NR_AEN_COMMANDS)
2211e399441dSJames Smart 		return;
2212e399441dSJames Smart 
221361bff8efSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
221461bff8efSJames Smart 	if (ctrl->flags & FCCTRL_TERMIO)
221561bff8efSJames Smart 		terminating = true;
221661bff8efSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
221761bff8efSJames Smart 
221861bff8efSJames Smart 	if (terminating)
221961bff8efSJames Smart 		return;
222061bff8efSJames Smart 
2221e399441dSJames Smart 	aen_op = &ctrl->aen_ops[aer_idx];
2222e399441dSJames Smart 
2223e399441dSJames Smart 	ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2224e399441dSJames Smart 					NVMEFC_FCP_NODATA);
2225e399441dSJames Smart 	if (ret)
2226e399441dSJames Smart 		dev_err(ctrl->ctrl.device,
2227e399441dSJames Smart 			"failed async event work [%d]\n", aer_idx);
2228e399441dSJames Smart }
2229e399441dSJames Smart 
2230e399441dSJames Smart static void
223178a7ac26SJames Smart __nvme_fc_final_op_cleanup(struct request *rq)
2232e399441dSJames Smart {
2233e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2234e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
2235e399441dSJames Smart 
223678a7ac26SJames Smart 	atomic_set(&op->state, FCPOP_STATE_IDLE);
223778a7ac26SJames Smart 	op->flags &= ~(FCOP_FLAGS_TERMIO | FCOP_FLAGS_RELEASED |
223878a7ac26SJames Smart 			FCOP_FLAGS_COMPLETE);
2239e399441dSJames Smart 
2240e399441dSJames Smart 	nvme_fc_unmap_data(ctrl, rq, op);
224177f02a7aSChristoph Hellwig 	nvme_complete_rq(rq);
2242e399441dSJames Smart 	nvme_fc_ctrl_put(ctrl);
2243e399441dSJames Smart 
2244e399441dSJames Smart }
2245e399441dSJames Smart 
224678a7ac26SJames Smart static void
224778a7ac26SJames Smart nvme_fc_complete_rq(struct request *rq)
224878a7ac26SJames Smart {
224978a7ac26SJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
225078a7ac26SJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
225178a7ac26SJames Smart 	unsigned long flags;
225278a7ac26SJames Smart 	bool completed = false;
225378a7ac26SJames Smart 
225478a7ac26SJames Smart 	/*
225578a7ac26SJames Smart 	 * the core layer, on controller resets after calling
225678a7ac26SJames Smart 	 * nvme_shutdown_ctrl(), calls complete_rq without our
225778a7ac26SJames Smart 	 * calling blk_mq_complete_request(), thus there may still
225878a7ac26SJames Smart 	 * be live i/o outstanding with the LLDD. Means transport has
225978a7ac26SJames Smart 	 * to track complete calls vs fcpio_done calls to know what
226078a7ac26SJames Smart 	 * path to take on completes and dones.
226178a7ac26SJames Smart 	 */
226278a7ac26SJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
226378a7ac26SJames Smart 	if (op->flags & FCOP_FLAGS_COMPLETE)
226478a7ac26SJames Smart 		completed = true;
226578a7ac26SJames Smart 	else
226678a7ac26SJames Smart 		op->flags |= FCOP_FLAGS_RELEASED;
226778a7ac26SJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
226878a7ac26SJames Smart 
226978a7ac26SJames Smart 	if (completed)
227078a7ac26SJames Smart 		__nvme_fc_final_op_cleanup(rq);
227178a7ac26SJames Smart }
227278a7ac26SJames Smart 
2273e399441dSJames Smart /*
2274e399441dSJames Smart  * This routine is used by the transport when it needs to find active
2275e399441dSJames Smart  * io on a queue that is to be terminated. The transport uses
2276e399441dSJames Smart  * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2277e399441dSJames Smart  * this routine to kill them on a 1 by 1 basis.
2278e399441dSJames Smart  *
2279e399441dSJames Smart  * As FC allocates FC exchange for each io, the transport must contact
2280e399441dSJames Smart  * the LLDD to terminate the exchange, thus releasing the FC exchange.
2281e399441dSJames Smart  * After terminating the exchange the LLDD will call the transport's
2282e399441dSJames Smart  * normal io done path for the request, but it will have an aborted
2283e399441dSJames Smart  * status. The done path will return the io request back to the block
2284e399441dSJames Smart  * layer with an error status.
2285e399441dSJames Smart  */
2286e399441dSJames Smart static void
2287e399441dSJames Smart nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2288e399441dSJames Smart {
2289e399441dSJames Smart 	struct nvme_ctrl *nctrl = data;
2290e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2291e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
229278a7ac26SJames Smart 	unsigned long flags;
2293e399441dSJames Smart 	int status;
2294e399441dSJames Smart 
2295e399441dSJames Smart 	if (!blk_mq_request_started(req))
2296e399441dSJames Smart 		return;
2297e399441dSJames Smart 
229878a7ac26SJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
229961bff8efSJames Smart 	if (ctrl->flags & FCCTRL_TERMIO) {
230061bff8efSJames Smart 		ctrl->iocnt++;
230178a7ac26SJames Smart 		op->flags |= FCOP_FLAGS_TERMIO;
230261bff8efSJames Smart 	}
230378a7ac26SJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
230478a7ac26SJames Smart 
2305e399441dSJames Smart 	status = __nvme_fc_abort_op(ctrl, op);
230678a7ac26SJames Smart 	if (status) {
2307e399441dSJames Smart 		/*
230878a7ac26SJames Smart 		 * if __nvme_fc_abort_op failed the io wasn't
230978a7ac26SJames Smart 		 * active. Thus this call path is running in
231078a7ac26SJames Smart 		 * parallel to the io complete. Treat as non-error.
2311e399441dSJames Smart 		 */
231278a7ac26SJames Smart 
231378a7ac26SJames Smart 		/* back out the flags/counters */
231478a7ac26SJames Smart 		spin_lock_irqsave(&ctrl->lock, flags);
231561bff8efSJames Smart 		if (ctrl->flags & FCCTRL_TERMIO)
231661bff8efSJames Smart 			ctrl->iocnt--;
231778a7ac26SJames Smart 		op->flags &= ~FCOP_FLAGS_TERMIO;
231878a7ac26SJames Smart 		spin_unlock_irqrestore(&ctrl->lock, flags);
2319e399441dSJames Smart 		return;
2320e399441dSJames Smart 	}
232178a7ac26SJames Smart }
2322e399441dSJames Smart 
2323e399441dSJames Smart 
232461bff8efSJames Smart static const struct blk_mq_ops nvme_fc_mq_ops = {
232561bff8efSJames Smart 	.queue_rq	= nvme_fc_queue_rq,
232661bff8efSJames Smart 	.complete	= nvme_fc_complete_rq,
232761bff8efSJames Smart 	.init_request	= nvme_fc_init_request,
232861bff8efSJames Smart 	.exit_request	= nvme_fc_exit_request,
232961bff8efSJames Smart 	.init_hctx	= nvme_fc_init_hctx,
233061bff8efSJames Smart 	.poll		= nvme_fc_poll,
233161bff8efSJames Smart 	.timeout	= nvme_fc_timeout,
2332e399441dSJames Smart };
2333e399441dSJames Smart 
2334e399441dSJames Smart static int
2335e399441dSJames Smart nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2336e399441dSJames Smart {
2337e399441dSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
23387314183dSSagi Grimberg 	unsigned int nr_io_queues;
2339e399441dSJames Smart 	int ret;
2340e399441dSJames Smart 
23417314183dSSagi Grimberg 	nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
23427314183dSSagi Grimberg 				ctrl->lport->ops->max_hw_queues);
23437314183dSSagi Grimberg 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2344e399441dSJames Smart 	if (ret) {
2345e399441dSJames Smart 		dev_info(ctrl->ctrl.device,
2346e399441dSJames Smart 			"set_queue_count failed: %d\n", ret);
2347e399441dSJames Smart 		return ret;
2348e399441dSJames Smart 	}
2349e399441dSJames Smart 
23507314183dSSagi Grimberg 	ctrl->ctrl.queue_count = nr_io_queues + 1;
23517314183dSSagi Grimberg 	if (!nr_io_queues)
2352e399441dSJames Smart 		return 0;
2353e399441dSJames Smart 
2354e399441dSJames Smart 	nvme_fc_init_io_queues(ctrl);
2355e399441dSJames Smart 
2356e399441dSJames Smart 	memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2357e399441dSJames Smart 	ctrl->tag_set.ops = &nvme_fc_mq_ops;
2358e399441dSJames Smart 	ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2359e399441dSJames Smart 	ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2360e399441dSJames Smart 	ctrl->tag_set.numa_node = NUMA_NO_NODE;
2361e399441dSJames Smart 	ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2362e399441dSJames Smart 	ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2363e399441dSJames Smart 					(SG_CHUNK_SIZE *
2364e399441dSJames Smart 						sizeof(struct scatterlist)) +
2365e399441dSJames Smart 					ctrl->lport->ops->fcprqst_priv_sz;
2366e399441dSJames Smart 	ctrl->tag_set.driver_data = ctrl;
2367d858e5f0SSagi Grimberg 	ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
2368e399441dSJames Smart 	ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2369e399441dSJames Smart 
2370e399441dSJames Smart 	ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2371e399441dSJames Smart 	if (ret)
2372e399441dSJames Smart 		return ret;
2373e399441dSJames Smart 
2374e399441dSJames Smart 	ctrl->ctrl.tagset = &ctrl->tag_set;
2375e399441dSJames Smart 
2376e399441dSJames Smart 	ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2377e399441dSJames Smart 	if (IS_ERR(ctrl->ctrl.connect_q)) {
2378e399441dSJames Smart 		ret = PTR_ERR(ctrl->ctrl.connect_q);
2379e399441dSJames Smart 		goto out_free_tag_set;
2380e399441dSJames Smart 	}
2381e399441dSJames Smart 
2382e399441dSJames Smart 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2383e399441dSJames Smart 	if (ret)
2384e399441dSJames Smart 		goto out_cleanup_blk_queue;
2385e399441dSJames Smart 
2386e399441dSJames Smart 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2387e399441dSJames Smart 	if (ret)
2388e399441dSJames Smart 		goto out_delete_hw_queues;
2389e399441dSJames Smart 
2390e399441dSJames Smart 	return 0;
2391e399441dSJames Smart 
2392e399441dSJames Smart out_delete_hw_queues:
2393e399441dSJames Smart 	nvme_fc_delete_hw_io_queues(ctrl);
2394e399441dSJames Smart out_cleanup_blk_queue:
2395e399441dSJames Smart 	blk_cleanup_queue(ctrl->ctrl.connect_q);
2396e399441dSJames Smart out_free_tag_set:
2397e399441dSJames Smart 	blk_mq_free_tag_set(&ctrl->tag_set);
2398e399441dSJames Smart 	nvme_fc_free_io_queues(ctrl);
2399e399441dSJames Smart 
2400e399441dSJames Smart 	/* force put free routine to ignore io queues */
2401e399441dSJames Smart 	ctrl->ctrl.tagset = NULL;
2402e399441dSJames Smart 
2403e399441dSJames Smart 	return ret;
2404e399441dSJames Smart }
2405e399441dSJames Smart 
240661bff8efSJames Smart static int
240761bff8efSJames Smart nvme_fc_reinit_io_queues(struct nvme_fc_ctrl *ctrl)
240861bff8efSJames Smart {
240961bff8efSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
24107314183dSSagi Grimberg 	unsigned int nr_io_queues;
241161bff8efSJames Smart 	int ret;
241261bff8efSJames Smart 
24137314183dSSagi Grimberg 	nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
24147314183dSSagi Grimberg 				ctrl->lport->ops->max_hw_queues);
24157314183dSSagi Grimberg 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
241661bff8efSJames Smart 	if (ret) {
241761bff8efSJames Smart 		dev_info(ctrl->ctrl.device,
241861bff8efSJames Smart 			"set_queue_count failed: %d\n", ret);
241961bff8efSJames Smart 		return ret;
242061bff8efSJames Smart 	}
242161bff8efSJames Smart 
24227314183dSSagi Grimberg 	ctrl->ctrl.queue_count = nr_io_queues + 1;
242361bff8efSJames Smart 	/* check for io queues existing */
2424d858e5f0SSagi Grimberg 	if (ctrl->ctrl.queue_count == 1)
242561bff8efSJames Smart 		return 0;
242661bff8efSJames Smart 
242761bff8efSJames Smart 	nvme_fc_init_io_queues(ctrl);
242861bff8efSJames Smart 
242931b84460SSagi Grimberg 	ret = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
243061bff8efSJames Smart 	if (ret)
243161bff8efSJames Smart 		goto out_free_io_queues;
243261bff8efSJames Smart 
243361bff8efSJames Smart 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
243461bff8efSJames Smart 	if (ret)
243561bff8efSJames Smart 		goto out_free_io_queues;
243661bff8efSJames Smart 
243761bff8efSJames Smart 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
243861bff8efSJames Smart 	if (ret)
243961bff8efSJames Smart 		goto out_delete_hw_queues;
244061bff8efSJames Smart 
2441cda5fd1aSSagi Grimberg 	blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2442cda5fd1aSSagi Grimberg 
244361bff8efSJames Smart 	return 0;
244461bff8efSJames Smart 
244561bff8efSJames Smart out_delete_hw_queues:
244661bff8efSJames Smart 	nvme_fc_delete_hw_io_queues(ctrl);
244761bff8efSJames Smart out_free_io_queues:
244861bff8efSJames Smart 	nvme_fc_free_io_queues(ctrl);
244961bff8efSJames Smart 	return ret;
245061bff8efSJames Smart }
245161bff8efSJames Smart 
245261bff8efSJames Smart /*
245361bff8efSJames Smart  * This routine restarts the controller on the host side, and
245461bff8efSJames Smart  * on the link side, recreates the controller association.
245561bff8efSJames Smart  */
245661bff8efSJames Smart static int
245761bff8efSJames Smart nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
245861bff8efSJames Smart {
245961bff8efSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
246061bff8efSJames Smart 	int ret;
246161bff8efSJames Smart 	bool changed;
246261bff8efSJames Smart 
2463fdf9dfa8SSagi Grimberg 	++ctrl->ctrl.nr_reconnects;
246461bff8efSJames Smart 
246561bff8efSJames Smart 	/*
246661bff8efSJames Smart 	 * Create the admin queue
246761bff8efSJames Smart 	 */
246861bff8efSJames Smart 
246961bff8efSJames Smart 	nvme_fc_init_queue(ctrl, 0, NVME_FC_AQ_BLKMQ_DEPTH);
247061bff8efSJames Smart 
247161bff8efSJames Smart 	ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
247261bff8efSJames Smart 				NVME_FC_AQ_BLKMQ_DEPTH);
247361bff8efSJames Smart 	if (ret)
247461bff8efSJames Smart 		goto out_free_queue;
247561bff8efSJames Smart 
247661bff8efSJames Smart 	ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
247761bff8efSJames Smart 				NVME_FC_AQ_BLKMQ_DEPTH,
247861bff8efSJames Smart 				(NVME_FC_AQ_BLKMQ_DEPTH / 4));
247961bff8efSJames Smart 	if (ret)
248061bff8efSJames Smart 		goto out_delete_hw_queue;
248161bff8efSJames Smart 
248261bff8efSJames Smart 	if (ctrl->ctrl.state != NVME_CTRL_NEW)
2483f9c5af5fSSagi Grimberg 		blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
248461bff8efSJames Smart 
248561bff8efSJames Smart 	ret = nvmf_connect_admin_queue(&ctrl->ctrl);
248661bff8efSJames Smart 	if (ret)
248761bff8efSJames Smart 		goto out_disconnect_admin_queue;
248861bff8efSJames Smart 
248961bff8efSJames Smart 	/*
249061bff8efSJames Smart 	 * Check controller capabilities
249161bff8efSJames Smart 	 *
249261bff8efSJames Smart 	 * todo:- add code to check if ctrl attributes changed from
249361bff8efSJames Smart 	 * prior connection values
249461bff8efSJames Smart 	 */
249561bff8efSJames Smart 
249620d0dfe6SSagi Grimberg 	ret = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap);
249761bff8efSJames Smart 	if (ret) {
249861bff8efSJames Smart 		dev_err(ctrl->ctrl.device,
249961bff8efSJames Smart 			"prop_get NVME_REG_CAP failed\n");
250061bff8efSJames Smart 		goto out_disconnect_admin_queue;
250161bff8efSJames Smart 	}
250261bff8efSJames Smart 
250361bff8efSJames Smart 	ctrl->ctrl.sqsize =
250420d0dfe6SSagi Grimberg 		min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap) + 1, ctrl->ctrl.sqsize);
250561bff8efSJames Smart 
250620d0dfe6SSagi Grimberg 	ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
250761bff8efSJames Smart 	if (ret)
250861bff8efSJames Smart 		goto out_disconnect_admin_queue;
250961bff8efSJames Smart 
2510ecad0d2cSJames Smart 	ctrl->ctrl.max_hw_sectors =
2511ecad0d2cSJames Smart 		(ctrl->lport->ops->max_sgl_segments - 1) << (PAGE_SHIFT - 9);
251261bff8efSJames Smart 
251361bff8efSJames Smart 	ret = nvme_init_identify(&ctrl->ctrl);
251461bff8efSJames Smart 	if (ret)
251561bff8efSJames Smart 		goto out_disconnect_admin_queue;
251661bff8efSJames Smart 
251761bff8efSJames Smart 	/* sanity checks */
251861bff8efSJames Smart 
251961bff8efSJames Smart 	/* FC-NVME does not have other data in the capsule */
252061bff8efSJames Smart 	if (ctrl->ctrl.icdoff) {
252161bff8efSJames Smart 		dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
252261bff8efSJames Smart 				ctrl->ctrl.icdoff);
252361bff8efSJames Smart 		goto out_disconnect_admin_queue;
252461bff8efSJames Smart 	}
252561bff8efSJames Smart 
252661bff8efSJames Smart 	/* FC-NVME supports normal SGL Data Block Descriptors */
252761bff8efSJames Smart 
252861bff8efSJames Smart 	if (opts->queue_size > ctrl->ctrl.maxcmd) {
252961bff8efSJames Smart 		/* warn if maxcmd is lower than queue_size */
253061bff8efSJames Smart 		dev_warn(ctrl->ctrl.device,
253161bff8efSJames Smart 			"queue_size %zu > ctrl maxcmd %u, reducing "
253261bff8efSJames Smart 			"to queue_size\n",
253361bff8efSJames Smart 			opts->queue_size, ctrl->ctrl.maxcmd);
253461bff8efSJames Smart 		opts->queue_size = ctrl->ctrl.maxcmd;
253561bff8efSJames Smart 	}
253661bff8efSJames Smart 
253761bff8efSJames Smart 	ret = nvme_fc_init_aen_ops(ctrl);
253861bff8efSJames Smart 	if (ret)
253961bff8efSJames Smart 		goto out_term_aen_ops;
254061bff8efSJames Smart 
254161bff8efSJames Smart 	/*
254261bff8efSJames Smart 	 * Create the io queues
254361bff8efSJames Smart 	 */
254461bff8efSJames Smart 
2545d858e5f0SSagi Grimberg 	if (ctrl->ctrl.queue_count > 1) {
254661bff8efSJames Smart 		if (ctrl->ctrl.state == NVME_CTRL_NEW)
254761bff8efSJames Smart 			ret = nvme_fc_create_io_queues(ctrl);
254861bff8efSJames Smart 		else
254961bff8efSJames Smart 			ret = nvme_fc_reinit_io_queues(ctrl);
255061bff8efSJames Smart 		if (ret)
255161bff8efSJames Smart 			goto out_term_aen_ops;
255261bff8efSJames Smart 	}
255361bff8efSJames Smart 
255461bff8efSJames Smart 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
255561bff8efSJames Smart 
2556fdf9dfa8SSagi Grimberg 	ctrl->ctrl.nr_reconnects = 0;
255761bff8efSJames Smart 
255844c6ec77SJames Smart 	if (changed)
2559d09f2b45SSagi Grimberg 		nvme_start_ctrl(&ctrl->ctrl);
256061bff8efSJames Smart 
256161bff8efSJames Smart 	return 0;	/* Success */
256261bff8efSJames Smart 
256361bff8efSJames Smart out_term_aen_ops:
256461bff8efSJames Smart 	nvme_fc_term_aen_ops(ctrl);
256561bff8efSJames Smart out_disconnect_admin_queue:
256661bff8efSJames Smart 	/* send a Disconnect(association) LS to fc-nvme target */
256761bff8efSJames Smart 	nvme_fc_xmt_disconnect_assoc(ctrl);
256861bff8efSJames Smart out_delete_hw_queue:
256961bff8efSJames Smart 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
257061bff8efSJames Smart out_free_queue:
257161bff8efSJames Smart 	nvme_fc_free_queue(&ctrl->queues[0]);
257261bff8efSJames Smart 
257361bff8efSJames Smart 	return ret;
257461bff8efSJames Smart }
257561bff8efSJames Smart 
257661bff8efSJames Smart /*
257761bff8efSJames Smart  * This routine stops operation of the controller on the host side.
257861bff8efSJames Smart  * On the host os stack side: Admin and IO queues are stopped,
257961bff8efSJames Smart  *   outstanding ios on them terminated via FC ABTS.
258061bff8efSJames Smart  * On the link side: the association is terminated.
258161bff8efSJames Smart  */
258261bff8efSJames Smart static void
258361bff8efSJames Smart nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
258461bff8efSJames Smart {
258561bff8efSJames Smart 	unsigned long flags;
258661bff8efSJames Smart 
258761bff8efSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
258861bff8efSJames Smart 	ctrl->flags |= FCCTRL_TERMIO;
258961bff8efSJames Smart 	ctrl->iocnt = 0;
259061bff8efSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
259161bff8efSJames Smart 
259261bff8efSJames Smart 	/*
259361bff8efSJames Smart 	 * If io queues are present, stop them and terminate all outstanding
259461bff8efSJames Smart 	 * ios on them. As FC allocates FC exchange for each io, the
259561bff8efSJames Smart 	 * transport must contact the LLDD to terminate the exchange,
259661bff8efSJames Smart 	 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
259761bff8efSJames Smart 	 * to tell us what io's are busy and invoke a transport routine
259861bff8efSJames Smart 	 * to kill them with the LLDD.  After terminating the exchange
259961bff8efSJames Smart 	 * the LLDD will call the transport's normal io done path, but it
260061bff8efSJames Smart 	 * will have an aborted status. The done path will return the
260161bff8efSJames Smart 	 * io requests back to the block layer as part of normal completions
260261bff8efSJames Smart 	 * (but with error status).
260361bff8efSJames Smart 	 */
2604d858e5f0SSagi Grimberg 	if (ctrl->ctrl.queue_count > 1) {
260561bff8efSJames Smart 		nvme_stop_queues(&ctrl->ctrl);
260661bff8efSJames Smart 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
260761bff8efSJames Smart 				nvme_fc_terminate_exchange, &ctrl->ctrl);
260861bff8efSJames Smart 	}
260961bff8efSJames Smart 
261061bff8efSJames Smart 	/*
261161bff8efSJames Smart 	 * Other transports, which don't have link-level contexts bound
261261bff8efSJames Smart 	 * to sqe's, would try to gracefully shutdown the controller by
261361bff8efSJames Smart 	 * writing the registers for shutdown and polling (call
261461bff8efSJames Smart 	 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
261561bff8efSJames Smart 	 * just aborted and we will wait on those contexts, and given
261661bff8efSJames Smart 	 * there was no indication of how live the controlelr is on the
261761bff8efSJames Smart 	 * link, don't send more io to create more contexts for the
261861bff8efSJames Smart 	 * shutdown. Let the controller fail via keepalive failure if
261961bff8efSJames Smart 	 * its still present.
262061bff8efSJames Smart 	 */
262161bff8efSJames Smart 
262261bff8efSJames Smart 	/*
262361bff8efSJames Smart 	 * clean up the admin queue. Same thing as above.
262461bff8efSJames Smart 	 * use blk_mq_tagset_busy_itr() and the transport routine to
262561bff8efSJames Smart 	 * terminate the exchanges.
262661bff8efSJames Smart 	 */
262744c6ec77SJames Smart 	if (ctrl->ctrl.state != NVME_CTRL_NEW)
2628f9c5af5fSSagi Grimberg 		blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
262961bff8efSJames Smart 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
263061bff8efSJames Smart 				nvme_fc_terminate_exchange, &ctrl->ctrl);
263161bff8efSJames Smart 
263261bff8efSJames Smart 	/* kill the aens as they are a separate path */
263361bff8efSJames Smart 	nvme_fc_abort_aen_ops(ctrl);
263461bff8efSJames Smart 
263561bff8efSJames Smart 	/* wait for all io that had to be aborted */
263661bff8efSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
263736715cf4SJames Smart 	wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
263861bff8efSJames Smart 	ctrl->flags &= ~FCCTRL_TERMIO;
263961bff8efSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
264061bff8efSJames Smart 
264161bff8efSJames Smart 	nvme_fc_term_aen_ops(ctrl);
264261bff8efSJames Smart 
264361bff8efSJames Smart 	/*
264461bff8efSJames Smart 	 * send a Disconnect(association) LS to fc-nvme target
264561bff8efSJames Smart 	 * Note: could have been sent at top of process, but
264661bff8efSJames Smart 	 * cleaner on link traffic if after the aborts complete.
264761bff8efSJames Smart 	 * Note: if association doesn't exist, association_id will be 0
264861bff8efSJames Smart 	 */
264961bff8efSJames Smart 	if (ctrl->association_id)
265061bff8efSJames Smart 		nvme_fc_xmt_disconnect_assoc(ctrl);
265161bff8efSJames Smart 
265261bff8efSJames Smart 	if (ctrl->ctrl.tagset) {
265361bff8efSJames Smart 		nvme_fc_delete_hw_io_queues(ctrl);
265461bff8efSJames Smart 		nvme_fc_free_io_queues(ctrl);
265561bff8efSJames Smart 	}
265661bff8efSJames Smart 
265761bff8efSJames Smart 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
265861bff8efSJames Smart 	nvme_fc_free_queue(&ctrl->queues[0]);
265961bff8efSJames Smart }
266061bff8efSJames Smart 
266161bff8efSJames Smart static void
2662c5017e85SChristoph Hellwig nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
266361bff8efSJames Smart {
2664c5017e85SChristoph Hellwig 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
266561bff8efSJames Smart 
266661bff8efSJames Smart 	cancel_delayed_work_sync(&ctrl->connect_work);
266761bff8efSJames Smart 	/*
266861bff8efSJames Smart 	 * kill the association on the link side.  this will block
266961bff8efSJames Smart 	 * waiting for io to terminate
267061bff8efSJames Smart 	 */
267161bff8efSJames Smart 	nvme_fc_delete_association(ctrl);
267261bff8efSJames Smart }
267361bff8efSJames Smart 
267461bff8efSJames Smart static void
26755bbecdbcSJames Smart nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
26765bbecdbcSJames Smart {
267744c6ec77SJames Smart 	if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING)
26785bbecdbcSJames Smart 		return;
26795bbecdbcSJames Smart 
2680589ff775SJames Smart 	dev_info(ctrl->ctrl.device,
26815bbecdbcSJames Smart 		"NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
26825bbecdbcSJames Smart 		ctrl->cnum, status);
26835bbecdbcSJames Smart 
26845bbecdbcSJames Smart 	if (nvmf_should_reconnect(&ctrl->ctrl)) {
26855bbecdbcSJames Smart 		dev_info(ctrl->ctrl.device,
26865bbecdbcSJames Smart 			"NVME-FC{%d}: Reconnect attempt in %d seconds.\n",
26875bbecdbcSJames Smart 			ctrl->cnum, ctrl->ctrl.opts->reconnect_delay);
26889a6327d2SSagi Grimberg 		queue_delayed_work(nvme_wq, &ctrl->connect_work,
26895bbecdbcSJames Smart 				ctrl->ctrl.opts->reconnect_delay * HZ);
26905bbecdbcSJames Smart 	} else {
2691589ff775SJames Smart 		dev_warn(ctrl->ctrl.device,
26925bbecdbcSJames Smart 				"NVME-FC{%d}: Max reconnect attempts (%d) "
26935bbecdbcSJames Smart 				"reached. Removing controller\n",
2694fdf9dfa8SSagi Grimberg 				ctrl->cnum, ctrl->ctrl.nr_reconnects);
2695c5017e85SChristoph Hellwig 		WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
26965bbecdbcSJames Smart 	}
26975bbecdbcSJames Smart }
26985bbecdbcSJames Smart 
26995bbecdbcSJames Smart static void
270061bff8efSJames Smart nvme_fc_reset_ctrl_work(struct work_struct *work)
270161bff8efSJames Smart {
270261bff8efSJames Smart 	struct nvme_fc_ctrl *ctrl =
2703d86c4d8eSChristoph Hellwig 		container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
270461bff8efSJames Smart 	int ret;
270561bff8efSJames Smart 
2706d09f2b45SSagi Grimberg 	nvme_stop_ctrl(&ctrl->ctrl);
270744c6ec77SJames Smart 
270861bff8efSJames Smart 	/* will block will waiting for io to terminate */
270961bff8efSJames Smart 	nvme_fc_delete_association(ctrl);
271061bff8efSJames Smart 
271144c6ec77SJames Smart 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) {
271244c6ec77SJames Smart 		dev_err(ctrl->ctrl.device,
271344c6ec77SJames Smart 			"NVME-FC{%d}: error_recovery: Couldn't change state "
271444c6ec77SJames Smart 			"to RECONNECTING\n", ctrl->cnum);
271544c6ec77SJames Smart 		return;
271644c6ec77SJames Smart 	}
271744c6ec77SJames Smart 
271861bff8efSJames Smart 	ret = nvme_fc_create_association(ctrl);
27195bbecdbcSJames Smart 	if (ret)
27205bbecdbcSJames Smart 		nvme_fc_reconnect_or_delete(ctrl, ret);
27215bbecdbcSJames Smart 	else
272261bff8efSJames Smart 		dev_info(ctrl->ctrl.device,
272361bff8efSJames Smart 			"NVME-FC{%d}: controller reset complete\n", ctrl->cnum);
272461bff8efSJames Smart }
272561bff8efSJames Smart 
272661bff8efSJames Smart static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
272761bff8efSJames Smart 	.name			= "fc",
272861bff8efSJames Smart 	.module			= THIS_MODULE,
2729d3d5b87dSChristoph Hellwig 	.flags			= NVME_F_FABRICS,
273061bff8efSJames Smart 	.reg_read32		= nvmf_reg_read32,
273161bff8efSJames Smart 	.reg_read64		= nvmf_reg_read64,
273261bff8efSJames Smart 	.reg_write32		= nvmf_reg_write32,
273361bff8efSJames Smart 	.free_ctrl		= nvme_fc_nvme_ctrl_freed,
273461bff8efSJames Smart 	.submit_async_event	= nvme_fc_submit_async_event,
2735c5017e85SChristoph Hellwig 	.delete_ctrl		= nvme_fc_delete_ctrl,
273661bff8efSJames Smart 	.get_address		= nvmf_get_address,
273731b84460SSagi Grimberg 	.reinit_request		= nvme_fc_reinit_request,
273861bff8efSJames Smart };
273961bff8efSJames Smart 
274061bff8efSJames Smart static void
274161bff8efSJames Smart nvme_fc_connect_ctrl_work(struct work_struct *work)
274261bff8efSJames Smart {
274361bff8efSJames Smart 	int ret;
274461bff8efSJames Smart 
274561bff8efSJames Smart 	struct nvme_fc_ctrl *ctrl =
274661bff8efSJames Smart 			container_of(to_delayed_work(work),
274761bff8efSJames Smart 				struct nvme_fc_ctrl, connect_work);
274861bff8efSJames Smart 
274961bff8efSJames Smart 	ret = nvme_fc_create_association(ctrl);
27505bbecdbcSJames Smart 	if (ret)
27515bbecdbcSJames Smart 		nvme_fc_reconnect_or_delete(ctrl, ret);
27525bbecdbcSJames Smart 	else
275361bff8efSJames Smart 		dev_info(ctrl->ctrl.device,
275461bff8efSJames Smart 			"NVME-FC{%d}: controller reconnect complete\n",
275561bff8efSJames Smart 			ctrl->cnum);
275661bff8efSJames Smart }
275761bff8efSJames Smart 
275861bff8efSJames Smart 
275961bff8efSJames Smart static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
276061bff8efSJames Smart 	.queue_rq	= nvme_fc_queue_rq,
276161bff8efSJames Smart 	.complete	= nvme_fc_complete_rq,
276276f983cbSChristoph Hellwig 	.init_request	= nvme_fc_init_request,
276361bff8efSJames Smart 	.exit_request	= nvme_fc_exit_request,
276461bff8efSJames Smart 	.init_hctx	= nvme_fc_init_admin_hctx,
276561bff8efSJames Smart 	.timeout	= nvme_fc_timeout,
276661bff8efSJames Smart };
276761bff8efSJames Smart 
2768e399441dSJames Smart 
276956d5f4f1SJames Smart /*
277056d5f4f1SJames Smart  * Fails a controller request if it matches an existing controller
277156d5f4f1SJames Smart  * (association) with the same tuple:
277256d5f4f1SJames Smart  * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
277356d5f4f1SJames Smart  *
277456d5f4f1SJames Smart  * The ports don't need to be compared as they are intrinsically
277556d5f4f1SJames Smart  * already matched by the port pointers supplied.
277656d5f4f1SJames Smart  */
277756d5f4f1SJames Smart static bool
277856d5f4f1SJames Smart nvme_fc_existing_controller(struct nvme_fc_rport *rport,
277956d5f4f1SJames Smart 		struct nvmf_ctrl_options *opts)
278056d5f4f1SJames Smart {
278156d5f4f1SJames Smart 	struct nvme_fc_ctrl *ctrl;
278256d5f4f1SJames Smart 	unsigned long flags;
278356d5f4f1SJames Smart 	bool found = false;
278456d5f4f1SJames Smart 
278556d5f4f1SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
278656d5f4f1SJames Smart 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
278756d5f4f1SJames Smart 		found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
278856d5f4f1SJames Smart 		if (found)
278956d5f4f1SJames Smart 			break;
279056d5f4f1SJames Smart 	}
279156d5f4f1SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
279256d5f4f1SJames Smart 
279356d5f4f1SJames Smart 	return found;
279456d5f4f1SJames Smart }
279556d5f4f1SJames Smart 
2796e399441dSJames Smart static struct nvme_ctrl *
279761bff8efSJames Smart nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
2798e399441dSJames Smart 	struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
2799e399441dSJames Smart {
2800e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl;
2801e399441dSJames Smart 	unsigned long flags;
2802e399441dSJames Smart 	int ret, idx;
2803e399441dSJames Smart 
280485e6a6adSJames Smart 	if (!(rport->remoteport.port_role &
280585e6a6adSJames Smart 	    (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
280685e6a6adSJames Smart 		ret = -EBADR;
280785e6a6adSJames Smart 		goto out_fail;
280885e6a6adSJames Smart 	}
280985e6a6adSJames Smart 
281056d5f4f1SJames Smart 	if (!opts->duplicate_connect &&
281156d5f4f1SJames Smart 	    nvme_fc_existing_controller(rport, opts)) {
281256d5f4f1SJames Smart 		ret = -EALREADY;
281356d5f4f1SJames Smart 		goto out_fail;
281456d5f4f1SJames Smart 	}
281556d5f4f1SJames Smart 
2816e399441dSJames Smart 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2817e399441dSJames Smart 	if (!ctrl) {
2818e399441dSJames Smart 		ret = -ENOMEM;
2819e399441dSJames Smart 		goto out_fail;
2820e399441dSJames Smart 	}
2821e399441dSJames Smart 
2822e399441dSJames Smart 	idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
2823e399441dSJames Smart 	if (idx < 0) {
2824e399441dSJames Smart 		ret = -ENOSPC;
2825e399441dSJames Smart 		goto out_free_ctrl;
2826e399441dSJames Smart 	}
2827e399441dSJames Smart 
2828e399441dSJames Smart 	ctrl->ctrl.opts = opts;
2829e399441dSJames Smart 	INIT_LIST_HEAD(&ctrl->ctrl_list);
2830e399441dSJames Smart 	ctrl->lport = lport;
2831e399441dSJames Smart 	ctrl->rport = rport;
2832e399441dSJames Smart 	ctrl->dev = lport->dev;
2833e399441dSJames Smart 	ctrl->cnum = idx;
2834e399441dSJames Smart 
2835e399441dSJames Smart 	get_device(ctrl->dev);
2836e399441dSJames Smart 	kref_init(&ctrl->ref);
2837e399441dSJames Smart 
2838d86c4d8eSChristoph Hellwig 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
283961bff8efSJames Smart 	INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
2840e399441dSJames Smart 	spin_lock_init(&ctrl->lock);
2841e399441dSJames Smart 
2842e399441dSJames Smart 	/* io queue count */
2843d858e5f0SSagi Grimberg 	ctrl->ctrl.queue_count = min_t(unsigned int,
2844e399441dSJames Smart 				opts->nr_io_queues,
2845e399441dSJames Smart 				lport->ops->max_hw_queues);
2846d858e5f0SSagi Grimberg 	ctrl->ctrl.queue_count++;	/* +1 for admin queue */
2847e399441dSJames Smart 
2848e399441dSJames Smart 	ctrl->ctrl.sqsize = opts->queue_size - 1;
2849e399441dSJames Smart 	ctrl->ctrl.kato = opts->kato;
2850e399441dSJames Smart 
2851e399441dSJames Smart 	ret = -ENOMEM;
2852d858e5f0SSagi Grimberg 	ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
2853d858e5f0SSagi Grimberg 				sizeof(struct nvme_fc_queue), GFP_KERNEL);
2854e399441dSJames Smart 	if (!ctrl->queues)
285561bff8efSJames Smart 		goto out_free_ida;
2856e399441dSJames Smart 
285761bff8efSJames Smart 	memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
285861bff8efSJames Smart 	ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
285961bff8efSJames Smart 	ctrl->admin_tag_set.queue_depth = NVME_FC_AQ_BLKMQ_DEPTH;
286061bff8efSJames Smart 	ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
286161bff8efSJames Smart 	ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
286261bff8efSJames Smart 	ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
286361bff8efSJames Smart 					(SG_CHUNK_SIZE *
286461bff8efSJames Smart 						sizeof(struct scatterlist)) +
286561bff8efSJames Smart 					ctrl->lport->ops->fcprqst_priv_sz;
286661bff8efSJames Smart 	ctrl->admin_tag_set.driver_data = ctrl;
286761bff8efSJames Smart 	ctrl->admin_tag_set.nr_hw_queues = 1;
286861bff8efSJames Smart 	ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
28695a22e2bfSIsrael Rukshin 	ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
287061bff8efSJames Smart 
287161bff8efSJames Smart 	ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
2872e399441dSJames Smart 	if (ret)
287361bff8efSJames Smart 		goto out_free_queues;
287434b6c231SSagi Grimberg 	ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
2875e399441dSJames Smart 
287661bff8efSJames Smart 	ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
287761bff8efSJames Smart 	if (IS_ERR(ctrl->ctrl.admin_q)) {
287861bff8efSJames Smart 		ret = PTR_ERR(ctrl->ctrl.admin_q);
287961bff8efSJames Smart 		goto out_free_admin_tag_set;
2880e399441dSJames Smart 	}
2881e399441dSJames Smart 
288261bff8efSJames Smart 	/*
288361bff8efSJames Smart 	 * Would have been nice to init io queues tag set as well.
288461bff8efSJames Smart 	 * However, we require interaction from the controller
288561bff8efSJames Smart 	 * for max io queue count before we can do so.
288661bff8efSJames Smart 	 * Defer this to the connect path.
288761bff8efSJames Smart 	 */
2888e399441dSJames Smart 
288961bff8efSJames Smart 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
2890e399441dSJames Smart 	if (ret)
289161bff8efSJames Smart 		goto out_cleanup_admin_q;
2892e399441dSJames Smart 
289361bff8efSJames Smart 	/* at this point, teardown path changes to ref counting on nvme ctrl */
2894e399441dSJames Smart 
2895e399441dSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
2896e399441dSJames Smart 	list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
2897e399441dSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
2898e399441dSJames Smart 
289961bff8efSJames Smart 	ret = nvme_fc_create_association(ctrl);
290061bff8efSJames Smart 	if (ret) {
2901de41447aSEwan D. Milne 		ctrl->ctrl.opts = NULL;
290261bff8efSJames Smart 		/* initiate nvme ctrl ref counting teardown */
290361bff8efSJames Smart 		nvme_uninit_ctrl(&ctrl->ctrl);
290424b7f059SJames Smart 		nvme_put_ctrl(&ctrl->ctrl);
290561bff8efSJames Smart 
29060b5a7669SJames Smart 		/* Remove core ctrl ref. */
29070b5a7669SJames Smart 		nvme_put_ctrl(&ctrl->ctrl);
29080b5a7669SJames Smart 
290961bff8efSJames Smart 		/* as we're past the point where we transition to the ref
291061bff8efSJames Smart 		 * counting teardown path, if we return a bad pointer here,
291161bff8efSJames Smart 		 * the calling routine, thinking it's prior to the
291261bff8efSJames Smart 		 * transition, will do an rport put. Since the teardown
291361bff8efSJames Smart 		 * path also does a rport put, we do an extra get here to
291461bff8efSJames Smart 		 * so proper order/teardown happens.
291561bff8efSJames Smart 		 */
291661bff8efSJames Smart 		nvme_fc_rport_get(rport);
291761bff8efSJames Smart 
291861bff8efSJames Smart 		if (ret > 0)
291961bff8efSJames Smart 			ret = -EIO;
292061bff8efSJames Smart 		return ERR_PTR(ret);
2921e399441dSJames Smart 	}
2922e399441dSJames Smart 
2923d22524a4SChristoph Hellwig 	nvme_get_ctrl(&ctrl->ctrl);
29242cb657bcSJames Smart 
292561bff8efSJames Smart 	dev_info(ctrl->ctrl.device,
292661bff8efSJames Smart 		"NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
292761bff8efSJames Smart 		ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
292861bff8efSJames Smart 
2929e399441dSJames Smart 	return &ctrl->ctrl;
2930e399441dSJames Smart 
293161bff8efSJames Smart out_cleanup_admin_q:
293261bff8efSJames Smart 	blk_cleanup_queue(ctrl->ctrl.admin_q);
293361bff8efSJames Smart out_free_admin_tag_set:
293461bff8efSJames Smart 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
293561bff8efSJames Smart out_free_queues:
293661bff8efSJames Smart 	kfree(ctrl->queues);
2937e399441dSJames Smart out_free_ida:
293861bff8efSJames Smart 	put_device(ctrl->dev);
2939e399441dSJames Smart 	ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2940e399441dSJames Smart out_free_ctrl:
2941e399441dSJames Smart 	kfree(ctrl);
2942e399441dSJames Smart out_fail:
2943e399441dSJames Smart 	/* exit via here doesn't follow ctlr ref points */
2944e399441dSJames Smart 	return ERR_PTR(ret);
2945e399441dSJames Smart }
2946e399441dSJames Smart 
2947e399441dSJames Smart 
2948e399441dSJames Smart struct nvmet_fc_traddr {
2949e399441dSJames Smart 	u64	nn;
2950e399441dSJames Smart 	u64	pn;
2951e399441dSJames Smart };
2952e399441dSJames Smart 
2953e399441dSJames Smart static int
29549c5358e1SJames Smart __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
2955e399441dSJames Smart {
2956e399441dSJames Smart 	u64 token64;
2957e399441dSJames Smart 
29589c5358e1SJames Smart 	if (match_u64(sstr, &token64))
29599c5358e1SJames Smart 		return -EINVAL;
29609c5358e1SJames Smart 	*val = token64;
2961e399441dSJames Smart 
29629c5358e1SJames Smart 	return 0;
2963e399441dSJames Smart }
2964e399441dSJames Smart 
29659c5358e1SJames Smart /*
29669c5358e1SJames Smart  * This routine validates and extracts the WWN's from the TRADDR string.
29679c5358e1SJames Smart  * As kernel parsers need the 0x to determine number base, universally
29689c5358e1SJames Smart  * build string to parse with 0x prefix before parsing name strings.
29699c5358e1SJames Smart  */
29709c5358e1SJames Smart static int
29719c5358e1SJames Smart nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
29729c5358e1SJames Smart {
29739c5358e1SJames Smart 	char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
29749c5358e1SJames Smart 	substring_t wwn = { name, &name[sizeof(name)-1] };
29759c5358e1SJames Smart 	int nnoffset, pnoffset;
29769c5358e1SJames Smart 
29779c5358e1SJames Smart 	/* validate it string one of the 2 allowed formats */
29789c5358e1SJames Smart 	if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
29799c5358e1SJames Smart 			!strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
29809c5358e1SJames Smart 			!strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
29819c5358e1SJames Smart 				"pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
29829c5358e1SJames Smart 		nnoffset = NVME_FC_TRADDR_OXNNLEN;
29839c5358e1SJames Smart 		pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
29849c5358e1SJames Smart 						NVME_FC_TRADDR_OXNNLEN;
29859c5358e1SJames Smart 	} else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
29869c5358e1SJames Smart 			!strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
29879c5358e1SJames Smart 			!strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
29889c5358e1SJames Smart 				"pn-", NVME_FC_TRADDR_NNLEN))) {
29899c5358e1SJames Smart 		nnoffset = NVME_FC_TRADDR_NNLEN;
29909c5358e1SJames Smart 		pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
29919c5358e1SJames Smart 	} else
29929c5358e1SJames Smart 		goto out_einval;
29939c5358e1SJames Smart 
29949c5358e1SJames Smart 	name[0] = '0';
29959c5358e1SJames Smart 	name[1] = 'x';
29969c5358e1SJames Smart 	name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
29979c5358e1SJames Smart 
29989c5358e1SJames Smart 	memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
29999c5358e1SJames Smart 	if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
30009c5358e1SJames Smart 		goto out_einval;
30019c5358e1SJames Smart 
30029c5358e1SJames Smart 	memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
30039c5358e1SJames Smart 	if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
30049c5358e1SJames Smart 		goto out_einval;
30059c5358e1SJames Smart 
30069c5358e1SJames Smart 	return 0;
30079c5358e1SJames Smart 
30089c5358e1SJames Smart out_einval:
30099c5358e1SJames Smart 	pr_warn("%s: bad traddr string\n", __func__);
30109c5358e1SJames Smart 	return -EINVAL;
3011e399441dSJames Smart }
3012e399441dSJames Smart 
3013e399441dSJames Smart static struct nvme_ctrl *
3014e399441dSJames Smart nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3015e399441dSJames Smart {
3016e399441dSJames Smart 	struct nvme_fc_lport *lport;
3017e399441dSJames Smart 	struct nvme_fc_rport *rport;
301861bff8efSJames Smart 	struct nvme_ctrl *ctrl;
3019e399441dSJames Smart 	struct nvmet_fc_traddr laddr = { 0L, 0L };
3020e399441dSJames Smart 	struct nvmet_fc_traddr raddr = { 0L, 0L };
3021e399441dSJames Smart 	unsigned long flags;
3022e399441dSJames Smart 	int ret;
3023e399441dSJames Smart 
30249c5358e1SJames Smart 	ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3025e399441dSJames Smart 	if (ret || !raddr.nn || !raddr.pn)
3026e399441dSJames Smart 		return ERR_PTR(-EINVAL);
3027e399441dSJames Smart 
30289c5358e1SJames Smart 	ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3029e399441dSJames Smart 	if (ret || !laddr.nn || !laddr.pn)
3030e399441dSJames Smart 		return ERR_PTR(-EINVAL);
3031e399441dSJames Smart 
3032e399441dSJames Smart 	/* find the host and remote ports to connect together */
3033e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
3034e399441dSJames Smart 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3035e399441dSJames Smart 		if (lport->localport.node_name != laddr.nn ||
3036e399441dSJames Smart 		    lport->localport.port_name != laddr.pn)
3037e399441dSJames Smart 			continue;
3038e399441dSJames Smart 
3039e399441dSJames Smart 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
3040e399441dSJames Smart 			if (rport->remoteport.node_name != raddr.nn ||
3041e399441dSJames Smart 			    rport->remoteport.port_name != raddr.pn)
3042e399441dSJames Smart 				continue;
3043e399441dSJames Smart 
3044e399441dSJames Smart 			/* if fail to get reference fall through. Will error */
3045e399441dSJames Smart 			if (!nvme_fc_rport_get(rport))
3046e399441dSJames Smart 				break;
3047e399441dSJames Smart 
3048e399441dSJames Smart 			spin_unlock_irqrestore(&nvme_fc_lock, flags);
3049e399441dSJames Smart 
305061bff8efSJames Smart 			ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
305161bff8efSJames Smart 			if (IS_ERR(ctrl))
305261bff8efSJames Smart 				nvme_fc_rport_put(rport);
305361bff8efSJames Smart 			return ctrl;
3054e399441dSJames Smart 		}
3055e399441dSJames Smart 	}
3056e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
3057e399441dSJames Smart 
3058e399441dSJames Smart 	return ERR_PTR(-ENOENT);
3059e399441dSJames Smart }
3060e399441dSJames Smart 
3061e399441dSJames Smart 
3062e399441dSJames Smart static struct nvmf_transport_ops nvme_fc_transport = {
3063e399441dSJames Smart 	.name		= "fc",
3064e399441dSJames Smart 	.required_opts	= NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
30655bbecdbcSJames Smart 	.allowed_opts	= NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3066e399441dSJames Smart 	.create_ctrl	= nvme_fc_create_ctrl,
3067e399441dSJames Smart };
3068e399441dSJames Smart 
3069e399441dSJames Smart static int __init nvme_fc_init_module(void)
3070e399441dSJames Smart {
30715f568556SJames Smart 	int ret;
30725f568556SJames Smart 
30735f568556SJames Smart 	/*
30745f568556SJames Smart 	 * NOTE:
30755f568556SJames Smart 	 * It is expected that in the future the kernel will combine
30765f568556SJames Smart 	 * the FC-isms that are currently under scsi and now being
30775f568556SJames Smart 	 * added to by NVME into a new standalone FC class. The SCSI
30785f568556SJames Smart 	 * and NVME protocols and their devices would be under this
30795f568556SJames Smart 	 * new FC class.
30805f568556SJames Smart 	 *
30815f568556SJames Smart 	 * As we need something to post FC-specific udev events to,
30825f568556SJames Smart 	 * specifically for nvme probe events, start by creating the
30835f568556SJames Smart 	 * new device class.  When the new standalone FC class is
30845f568556SJames Smart 	 * put in place, this code will move to a more generic
30855f568556SJames Smart 	 * location for the class.
30865f568556SJames Smart 	 */
30875f568556SJames Smart 	fc_class = class_create(THIS_MODULE, "fc");
30885f568556SJames Smart 	if (IS_ERR(fc_class)) {
30895f568556SJames Smart 		pr_err("couldn't register class fc\n");
30905f568556SJames Smart 		return PTR_ERR(fc_class);
30915f568556SJames Smart 	}
30925f568556SJames Smart 
30935f568556SJames Smart 	/*
30945f568556SJames Smart 	 * Create a device for the FC-centric udev events
30955f568556SJames Smart 	 */
30965f568556SJames Smart 	fc_udev_device = device_create(fc_class, NULL, MKDEV(0, 0), NULL,
30975f568556SJames Smart 				"fc_udev_device");
30985f568556SJames Smart 	if (IS_ERR(fc_udev_device)) {
30995f568556SJames Smart 		pr_err("couldn't create fc_udev device!\n");
31005f568556SJames Smart 		ret = PTR_ERR(fc_udev_device);
31015f568556SJames Smart 		goto out_destroy_class;
31025f568556SJames Smart 	}
31035f568556SJames Smart 
31045f568556SJames Smart 	ret = nvmf_register_transport(&nvme_fc_transport);
31055f568556SJames Smart 	if (ret)
31065f568556SJames Smart 		goto out_destroy_device;
31075f568556SJames Smart 
31085f568556SJames Smart 	return 0;
31095f568556SJames Smart 
31105f568556SJames Smart out_destroy_device:
31115f568556SJames Smart 	device_destroy(fc_class, MKDEV(0, 0));
31125f568556SJames Smart out_destroy_class:
31135f568556SJames Smart 	class_destroy(fc_class);
31145f568556SJames Smart 	return ret;
3115e399441dSJames Smart }
3116e399441dSJames Smart 
3117e399441dSJames Smart static void __exit nvme_fc_exit_module(void)
3118e399441dSJames Smart {
3119e399441dSJames Smart 	/* sanity check - all lports should be removed */
3120e399441dSJames Smart 	if (!list_empty(&nvme_fc_lport_list))
3121e399441dSJames Smart 		pr_warn("%s: localport list not empty\n", __func__);
3122e399441dSJames Smart 
3123e399441dSJames Smart 	nvmf_unregister_transport(&nvme_fc_transport);
3124e399441dSJames Smart 
3125e399441dSJames Smart 	ida_destroy(&nvme_fc_local_port_cnt);
3126e399441dSJames Smart 	ida_destroy(&nvme_fc_ctrl_cnt);
31275f568556SJames Smart 
31285f568556SJames Smart 	device_destroy(fc_class, MKDEV(0, 0));
31295f568556SJames Smart 	class_destroy(fc_class);
3130e399441dSJames Smart }
3131e399441dSJames Smart 
3132e399441dSJames Smart module_init(nvme_fc_init_module);
3133e399441dSJames Smart module_exit(nvme_fc_exit_module);
3134e399441dSJames Smart 
3135e399441dSJames Smart MODULE_LICENSE("GPL v2");
3136