xref: /openbmc/linux/drivers/nvme/host/fc.c (revision 27fa9bc5)
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>
22e399441dSJames Smart 
23e399441dSJames Smart #include "nvme.h"
24e399441dSJames Smart #include "fabrics.h"
25e399441dSJames Smart #include <linux/nvme-fc-driver.h>
26e399441dSJames Smart #include <linux/nvme-fc.h>
27e399441dSJames Smart 
28e399441dSJames Smart 
29e399441dSJames Smart /* *************************** Data Structures/Defines ****************** */
30e399441dSJames Smart 
31e399441dSJames Smart 
32e399441dSJames Smart /*
33e399441dSJames Smart  * We handle AEN commands ourselves and don't even let the
34e399441dSJames Smart  * block layer know about them.
35e399441dSJames Smart  */
36e399441dSJames Smart #define NVME_FC_NR_AEN_COMMANDS	1
37e399441dSJames Smart #define NVME_FC_AQ_BLKMQ_DEPTH	\
38e399441dSJames Smart 	(NVMF_AQ_DEPTH - NVME_FC_NR_AEN_COMMANDS)
39e399441dSJames Smart #define AEN_CMDID_BASE		(NVME_FC_AQ_BLKMQ_DEPTH + 1)
40e399441dSJames Smart 
41e399441dSJames Smart enum nvme_fc_queue_flags {
42e399441dSJames Smart 	NVME_FC_Q_CONNECTED = (1 << 0),
43e399441dSJames Smart };
44e399441dSJames Smart 
45e399441dSJames Smart #define NVMEFC_QUEUE_DELAY	3		/* ms units */
46e399441dSJames Smart 
47e399441dSJames Smart struct nvme_fc_queue {
48e399441dSJames Smart 	struct nvme_fc_ctrl	*ctrl;
49e399441dSJames Smart 	struct device		*dev;
50e399441dSJames Smart 	struct blk_mq_hw_ctx	*hctx;
51e399441dSJames Smart 	void			*lldd_handle;
52e399441dSJames Smart 	int			queue_size;
53e399441dSJames Smart 	size_t			cmnd_capsule_len;
54e399441dSJames Smart 	u32			qnum;
55e399441dSJames Smart 	u32			rqcnt;
56e399441dSJames Smart 	u32			seqno;
57e399441dSJames Smart 
58e399441dSJames Smart 	u64			connection_id;
59e399441dSJames Smart 	atomic_t		csn;
60e399441dSJames Smart 
61e399441dSJames Smart 	unsigned long		flags;
62e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
63e399441dSJames Smart 
64e399441dSJames Smart struct nvmefc_ls_req_op {
65e399441dSJames Smart 	struct nvmefc_ls_req	ls_req;
66e399441dSJames Smart 
67e399441dSJames Smart 	struct nvme_fc_ctrl	*ctrl;
68e399441dSJames Smart 	struct nvme_fc_queue	*queue;
69e399441dSJames Smart 	struct request		*rq;
70e399441dSJames Smart 
71e399441dSJames Smart 	int			ls_error;
72e399441dSJames Smart 	struct completion	ls_done;
73e399441dSJames Smart 	struct list_head	lsreq_list;	/* ctrl->ls_req_list */
74e399441dSJames Smart 	bool			req_queued;
75e399441dSJames Smart };
76e399441dSJames Smart 
77e399441dSJames Smart enum nvme_fcpop_state {
78e399441dSJames Smart 	FCPOP_STATE_UNINIT	= 0,
79e399441dSJames Smart 	FCPOP_STATE_IDLE	= 1,
80e399441dSJames Smart 	FCPOP_STATE_ACTIVE	= 2,
81e399441dSJames Smart 	FCPOP_STATE_ABORTED	= 3,
82e399441dSJames Smart };
83e399441dSJames Smart 
84e399441dSJames Smart struct nvme_fc_fcp_op {
85e399441dSJames Smart 	struct nvme_request	nreq;		/*
86e399441dSJames Smart 						 * nvme/host/core.c
87e399441dSJames Smart 						 * requires this to be
88e399441dSJames Smart 						 * the 1st element in the
89e399441dSJames Smart 						 * private structure
90e399441dSJames Smart 						 * associated with the
91e399441dSJames Smart 						 * request.
92e399441dSJames Smart 						 */
93e399441dSJames Smart 	struct nvmefc_fcp_req	fcp_req;
94e399441dSJames Smart 
95e399441dSJames Smart 	struct nvme_fc_ctrl	*ctrl;
96e399441dSJames Smart 	struct nvme_fc_queue	*queue;
97e399441dSJames Smart 	struct request		*rq;
98e399441dSJames Smart 
99e399441dSJames Smart 	atomic_t		state;
100e399441dSJames Smart 	u32			rqno;
101e399441dSJames Smart 	u32			nents;
102e399441dSJames Smart 
103e399441dSJames Smart 	struct nvme_fc_cmd_iu	cmd_iu;
104e399441dSJames Smart 	struct nvme_fc_ersp_iu	rsp_iu;
105e399441dSJames Smart };
106e399441dSJames Smart 
107e399441dSJames Smart struct nvme_fc_lport {
108e399441dSJames Smart 	struct nvme_fc_local_port	localport;
109e399441dSJames Smart 
110e399441dSJames Smart 	struct ida			endp_cnt;
111e399441dSJames Smart 	struct list_head		port_list;	/* nvme_fc_port_list */
112e399441dSJames Smart 	struct list_head		endp_list;
113e399441dSJames Smart 	struct device			*dev;	/* physical device for dma */
114e399441dSJames Smart 	struct nvme_fc_port_template	*ops;
115e399441dSJames Smart 	struct kref			ref;
116e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
117e399441dSJames Smart 
118e399441dSJames Smart struct nvme_fc_rport {
119e399441dSJames Smart 	struct nvme_fc_remote_port	remoteport;
120e399441dSJames Smart 
121e399441dSJames Smart 	struct list_head		endp_list; /* for lport->endp_list */
122e399441dSJames Smart 	struct list_head		ctrl_list;
123e399441dSJames Smart 	spinlock_t			lock;
124e399441dSJames Smart 	struct kref			ref;
125e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
126e399441dSJames Smart 
127e399441dSJames Smart enum nvme_fcctrl_state {
128e399441dSJames Smart 	FCCTRL_INIT		= 0,
129e399441dSJames Smart 	FCCTRL_ACTIVE		= 1,
130e399441dSJames Smart };
131e399441dSJames Smart 
132e399441dSJames Smart struct nvme_fc_ctrl {
133e399441dSJames Smart 	spinlock_t		lock;
134e399441dSJames Smart 	struct nvme_fc_queue	*queues;
135e399441dSJames Smart 	u32			queue_count;
136e399441dSJames Smart 
137e399441dSJames Smart 	struct device		*dev;
138e399441dSJames Smart 	struct nvme_fc_lport	*lport;
139e399441dSJames Smart 	struct nvme_fc_rport	*rport;
140e399441dSJames Smart 	u32			cnum;
141e399441dSJames Smart 
142e399441dSJames Smart 	u64			association_id;
143e399441dSJames Smart 
144e399441dSJames Smart 	u64			cap;
145e399441dSJames Smart 
146e399441dSJames Smart 	struct list_head	ctrl_list;	/* rport->ctrl_list */
147e399441dSJames Smart 	struct list_head	ls_req_list;
148e399441dSJames Smart 
149e399441dSJames Smart 	struct blk_mq_tag_set	admin_tag_set;
150e399441dSJames Smart 	struct blk_mq_tag_set	tag_set;
151e399441dSJames Smart 
152e399441dSJames Smart 	struct work_struct	delete_work;
153e399441dSJames Smart 	struct kref		ref;
154e399441dSJames Smart 	int			state;
155e399441dSJames Smart 
156e399441dSJames Smart 	struct nvme_fc_fcp_op	aen_ops[NVME_FC_NR_AEN_COMMANDS];
157e399441dSJames Smart 
158e399441dSJames Smart 	struct nvme_ctrl	ctrl;
159e399441dSJames Smart };
160e399441dSJames Smart 
161e399441dSJames Smart static inline struct nvme_fc_ctrl *
162e399441dSJames Smart to_fc_ctrl(struct nvme_ctrl *ctrl)
163e399441dSJames Smart {
164e399441dSJames Smart 	return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
165e399441dSJames Smart }
166e399441dSJames Smart 
167e399441dSJames Smart static inline struct nvme_fc_lport *
168e399441dSJames Smart localport_to_lport(struct nvme_fc_local_port *portptr)
169e399441dSJames Smart {
170e399441dSJames Smart 	return container_of(portptr, struct nvme_fc_lport, localport);
171e399441dSJames Smart }
172e399441dSJames Smart 
173e399441dSJames Smart static inline struct nvme_fc_rport *
174e399441dSJames Smart remoteport_to_rport(struct nvme_fc_remote_port *portptr)
175e399441dSJames Smart {
176e399441dSJames Smart 	return container_of(portptr, struct nvme_fc_rport, remoteport);
177e399441dSJames Smart }
178e399441dSJames Smart 
179e399441dSJames Smart static inline struct nvmefc_ls_req_op *
180e399441dSJames Smart ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
181e399441dSJames Smart {
182e399441dSJames Smart 	return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
183e399441dSJames Smart }
184e399441dSJames Smart 
185e399441dSJames Smart static inline struct nvme_fc_fcp_op *
186e399441dSJames Smart fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
187e399441dSJames Smart {
188e399441dSJames Smart 	return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
189e399441dSJames Smart }
190e399441dSJames Smart 
191e399441dSJames Smart 
192e399441dSJames Smart 
193e399441dSJames Smart /* *************************** Globals **************************** */
194e399441dSJames Smart 
195e399441dSJames Smart 
196e399441dSJames Smart static DEFINE_SPINLOCK(nvme_fc_lock);
197e399441dSJames Smart 
198e399441dSJames Smart static LIST_HEAD(nvme_fc_lport_list);
199e399441dSJames Smart static DEFINE_IDA(nvme_fc_local_port_cnt);
200e399441dSJames Smart static DEFINE_IDA(nvme_fc_ctrl_cnt);
201e399441dSJames Smart 
202e399441dSJames Smart static struct workqueue_struct *nvme_fc_wq;
203e399441dSJames Smart 
204e399441dSJames Smart 
205e399441dSJames Smart 
206e399441dSJames Smart /* *********************** FC-NVME Port Management ************************ */
207e399441dSJames Smart 
208e399441dSJames Smart static int __nvme_fc_del_ctrl(struct nvme_fc_ctrl *);
209e399441dSJames Smart static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
210e399441dSJames Smart 			struct nvme_fc_queue *, unsigned int);
211e399441dSJames Smart 
212e399441dSJames Smart 
213e399441dSJames Smart /**
214e399441dSJames Smart  * nvme_fc_register_localport - transport entry point called by an
215e399441dSJames Smart  *                              LLDD to register the existence of a NVME
216e399441dSJames Smart  *                              host FC port.
217e399441dSJames Smart  * @pinfo:     pointer to information about the port to be registered
218e399441dSJames Smart  * @template:  LLDD entrypoints and operational parameters for the port
219e399441dSJames Smart  * @dev:       physical hardware device node port corresponds to. Will be
220e399441dSJames Smart  *             used for DMA mappings
221e399441dSJames Smart  * @lport_p:   pointer to a local port pointer. Upon success, the routine
222e399441dSJames Smart  *             will allocate a nvme_fc_local_port structure and place its
223e399441dSJames Smart  *             address in the local port pointer. Upon failure, local port
224e399441dSJames Smart  *             pointer will be set to 0.
225e399441dSJames Smart  *
226e399441dSJames Smart  * Returns:
227e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
228e399441dSJames Smart  * (ex: -ENXIO) upon failure.
229e399441dSJames Smart  */
230e399441dSJames Smart int
231e399441dSJames Smart nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
232e399441dSJames Smart 			struct nvme_fc_port_template *template,
233e399441dSJames Smart 			struct device *dev,
234e399441dSJames Smart 			struct nvme_fc_local_port **portptr)
235e399441dSJames Smart {
236e399441dSJames Smart 	struct nvme_fc_lport *newrec;
237e399441dSJames Smart 	unsigned long flags;
238e399441dSJames Smart 	int ret, idx;
239e399441dSJames Smart 
240e399441dSJames Smart 	if (!template->localport_delete || !template->remoteport_delete ||
241e399441dSJames Smart 	    !template->ls_req || !template->fcp_io ||
242e399441dSJames Smart 	    !template->ls_abort || !template->fcp_abort ||
243e399441dSJames Smart 	    !template->max_hw_queues || !template->max_sgl_segments ||
244e399441dSJames Smart 	    !template->max_dif_sgl_segments || !template->dma_boundary) {
245e399441dSJames Smart 		ret = -EINVAL;
246e399441dSJames Smart 		goto out_reghost_failed;
247e399441dSJames Smart 	}
248e399441dSJames Smart 
249e399441dSJames Smart 	newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
250e399441dSJames Smart 			 GFP_KERNEL);
251e399441dSJames Smart 	if (!newrec) {
252e399441dSJames Smart 		ret = -ENOMEM;
253e399441dSJames Smart 		goto out_reghost_failed;
254e399441dSJames Smart 	}
255e399441dSJames Smart 
256e399441dSJames Smart 	idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
257e399441dSJames Smart 	if (idx < 0) {
258e399441dSJames Smart 		ret = -ENOSPC;
259e399441dSJames Smart 		goto out_fail_kfree;
260e399441dSJames Smart 	}
261e399441dSJames Smart 
262e399441dSJames Smart 	if (!get_device(dev) && dev) {
263e399441dSJames Smart 		ret = -ENODEV;
264e399441dSJames Smart 		goto out_ida_put;
265e399441dSJames Smart 	}
266e399441dSJames Smart 
267e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->port_list);
268e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->endp_list);
269e399441dSJames Smart 	kref_init(&newrec->ref);
270e399441dSJames Smart 	newrec->ops = template;
271e399441dSJames Smart 	newrec->dev = dev;
272e399441dSJames Smart 	ida_init(&newrec->endp_cnt);
273e399441dSJames Smart 	newrec->localport.private = &newrec[1];
274e399441dSJames Smart 	newrec->localport.node_name = pinfo->node_name;
275e399441dSJames Smart 	newrec->localport.port_name = pinfo->port_name;
276e399441dSJames Smart 	newrec->localport.port_role = pinfo->port_role;
277e399441dSJames Smart 	newrec->localport.port_id = pinfo->port_id;
278e399441dSJames Smart 	newrec->localport.port_state = FC_OBJSTATE_ONLINE;
279e399441dSJames Smart 	newrec->localport.port_num = idx;
280e399441dSJames Smart 
281e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
282e399441dSJames Smart 	list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
283e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
284e399441dSJames Smart 
285e399441dSJames Smart 	if (dev)
286e399441dSJames Smart 		dma_set_seg_boundary(dev, template->dma_boundary);
287e399441dSJames Smart 
288e399441dSJames Smart 	*portptr = &newrec->localport;
289e399441dSJames Smart 	return 0;
290e399441dSJames Smart 
291e399441dSJames Smart out_ida_put:
292e399441dSJames Smart 	ida_simple_remove(&nvme_fc_local_port_cnt, idx);
293e399441dSJames Smart out_fail_kfree:
294e399441dSJames Smart 	kfree(newrec);
295e399441dSJames Smart out_reghost_failed:
296e399441dSJames Smart 	*portptr = NULL;
297e399441dSJames Smart 
298e399441dSJames Smart 	return ret;
299e399441dSJames Smart }
300e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
301e399441dSJames Smart 
302e399441dSJames Smart static void
303e399441dSJames Smart nvme_fc_free_lport(struct kref *ref)
304e399441dSJames Smart {
305e399441dSJames Smart 	struct nvme_fc_lport *lport =
306e399441dSJames Smart 		container_of(ref, struct nvme_fc_lport, ref);
307e399441dSJames Smart 	unsigned long flags;
308e399441dSJames Smart 
309e399441dSJames Smart 	WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
310e399441dSJames Smart 	WARN_ON(!list_empty(&lport->endp_list));
311e399441dSJames Smart 
312e399441dSJames Smart 	/* remove from transport list */
313e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
314e399441dSJames Smart 	list_del(&lport->port_list);
315e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
316e399441dSJames Smart 
317e399441dSJames Smart 	/* let the LLDD know we've finished tearing it down */
318e399441dSJames Smart 	lport->ops->localport_delete(&lport->localport);
319e399441dSJames Smart 
320e399441dSJames Smart 	ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
321e399441dSJames Smart 	ida_destroy(&lport->endp_cnt);
322e399441dSJames Smart 
323e399441dSJames Smart 	put_device(lport->dev);
324e399441dSJames Smart 
325e399441dSJames Smart 	kfree(lport);
326e399441dSJames Smart }
327e399441dSJames Smart 
328e399441dSJames Smart static void
329e399441dSJames Smart nvme_fc_lport_put(struct nvme_fc_lport *lport)
330e399441dSJames Smart {
331e399441dSJames Smart 	kref_put(&lport->ref, nvme_fc_free_lport);
332e399441dSJames Smart }
333e399441dSJames Smart 
334e399441dSJames Smart static int
335e399441dSJames Smart nvme_fc_lport_get(struct nvme_fc_lport *lport)
336e399441dSJames Smart {
337e399441dSJames Smart 	return kref_get_unless_zero(&lport->ref);
338e399441dSJames Smart }
339e399441dSJames Smart 
340e399441dSJames Smart /**
341e399441dSJames Smart  * nvme_fc_unregister_localport - transport entry point called by an
342e399441dSJames Smart  *                              LLDD to deregister/remove a previously
343e399441dSJames Smart  *                              registered a NVME host FC port.
344e399441dSJames Smart  * @localport: pointer to the (registered) local port that is to be
345e399441dSJames Smart  *             deregistered.
346e399441dSJames Smart  *
347e399441dSJames Smart  * Returns:
348e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
349e399441dSJames Smart  * (ex: -ENXIO) upon failure.
350e399441dSJames Smart  */
351e399441dSJames Smart int
352e399441dSJames Smart nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
353e399441dSJames Smart {
354e399441dSJames Smart 	struct nvme_fc_lport *lport = localport_to_lport(portptr);
355e399441dSJames Smart 	unsigned long flags;
356e399441dSJames Smart 
357e399441dSJames Smart 	if (!portptr)
358e399441dSJames Smart 		return -EINVAL;
359e399441dSJames Smart 
360e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
361e399441dSJames Smart 
362e399441dSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
363e399441dSJames Smart 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
364e399441dSJames Smart 		return -EINVAL;
365e399441dSJames Smart 	}
366e399441dSJames Smart 	portptr->port_state = FC_OBJSTATE_DELETED;
367e399441dSJames Smart 
368e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
369e399441dSJames Smart 
370e399441dSJames Smart 	nvme_fc_lport_put(lport);
371e399441dSJames Smart 
372e399441dSJames Smart 	return 0;
373e399441dSJames Smart }
374e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
375e399441dSJames Smart 
376e399441dSJames Smart /**
377e399441dSJames Smart  * nvme_fc_register_remoteport - transport entry point called by an
378e399441dSJames Smart  *                              LLDD to register the existence of a NVME
379e399441dSJames Smart  *                              subsystem FC port on its fabric.
380e399441dSJames Smart  * @localport: pointer to the (registered) local port that the remote
381e399441dSJames Smart  *             subsystem port is connected to.
382e399441dSJames Smart  * @pinfo:     pointer to information about the port to be registered
383e399441dSJames Smart  * @rport_p:   pointer to a remote port pointer. Upon success, the routine
384e399441dSJames Smart  *             will allocate a nvme_fc_remote_port structure and place its
385e399441dSJames Smart  *             address in the remote port pointer. Upon failure, remote port
386e399441dSJames Smart  *             pointer will be set to 0.
387e399441dSJames Smart  *
388e399441dSJames Smart  * Returns:
389e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
390e399441dSJames Smart  * (ex: -ENXIO) upon failure.
391e399441dSJames Smart  */
392e399441dSJames Smart int
393e399441dSJames Smart nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
394e399441dSJames Smart 				struct nvme_fc_port_info *pinfo,
395e399441dSJames Smart 				struct nvme_fc_remote_port **portptr)
396e399441dSJames Smart {
397e399441dSJames Smart 	struct nvme_fc_lport *lport = localport_to_lport(localport);
398e399441dSJames Smart 	struct nvme_fc_rport *newrec;
399e399441dSJames Smart 	unsigned long flags;
400e399441dSJames Smart 	int ret, idx;
401e399441dSJames Smart 
402e399441dSJames Smart 	newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
403e399441dSJames Smart 			 GFP_KERNEL);
404e399441dSJames Smart 	if (!newrec) {
405e399441dSJames Smart 		ret = -ENOMEM;
406e399441dSJames Smart 		goto out_reghost_failed;
407e399441dSJames Smart 	}
408e399441dSJames Smart 
409e399441dSJames Smart 	if (!nvme_fc_lport_get(lport)) {
410e399441dSJames Smart 		ret = -ESHUTDOWN;
411e399441dSJames Smart 		goto out_kfree_rport;
412e399441dSJames Smart 	}
413e399441dSJames Smart 
414e399441dSJames Smart 	idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
415e399441dSJames Smart 	if (idx < 0) {
416e399441dSJames Smart 		ret = -ENOSPC;
417e399441dSJames Smart 		goto out_lport_put;
418e399441dSJames Smart 	}
419e399441dSJames Smart 
420e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->endp_list);
421e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->ctrl_list);
422e399441dSJames Smart 	kref_init(&newrec->ref);
423e399441dSJames Smart 	spin_lock_init(&newrec->lock);
424e399441dSJames Smart 	newrec->remoteport.localport = &lport->localport;
425e399441dSJames Smart 	newrec->remoteport.private = &newrec[1];
426e399441dSJames Smart 	newrec->remoteport.port_role = pinfo->port_role;
427e399441dSJames Smart 	newrec->remoteport.node_name = pinfo->node_name;
428e399441dSJames Smart 	newrec->remoteport.port_name = pinfo->port_name;
429e399441dSJames Smart 	newrec->remoteport.port_id = pinfo->port_id;
430e399441dSJames Smart 	newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
431e399441dSJames Smart 	newrec->remoteport.port_num = idx;
432e399441dSJames Smart 
433e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
434e399441dSJames Smart 	list_add_tail(&newrec->endp_list, &lport->endp_list);
435e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
436e399441dSJames Smart 
437e399441dSJames Smart 	*portptr = &newrec->remoteport;
438e399441dSJames Smart 	return 0;
439e399441dSJames Smart 
440e399441dSJames Smart out_lport_put:
441e399441dSJames Smart 	nvme_fc_lport_put(lport);
442e399441dSJames Smart out_kfree_rport:
443e399441dSJames Smart 	kfree(newrec);
444e399441dSJames Smart out_reghost_failed:
445e399441dSJames Smart 	*portptr = NULL;
446e399441dSJames Smart 	return ret;
447e399441dSJames Smart 
448e399441dSJames Smart }
449e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
450e399441dSJames Smart 
451e399441dSJames Smart static void
452e399441dSJames Smart nvme_fc_free_rport(struct kref *ref)
453e399441dSJames Smart {
454e399441dSJames Smart 	struct nvme_fc_rport *rport =
455e399441dSJames Smart 		container_of(ref, struct nvme_fc_rport, ref);
456e399441dSJames Smart 	struct nvme_fc_lport *lport =
457e399441dSJames Smart 			localport_to_lport(rport->remoteport.localport);
458e399441dSJames Smart 	unsigned long flags;
459e399441dSJames Smart 
460e399441dSJames Smart 	WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
461e399441dSJames Smart 	WARN_ON(!list_empty(&rport->ctrl_list));
462e399441dSJames Smart 
463e399441dSJames Smart 	/* remove from lport list */
464e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
465e399441dSJames Smart 	list_del(&rport->endp_list);
466e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
467e399441dSJames Smart 
468e399441dSJames Smart 	/* let the LLDD know we've finished tearing it down */
469e399441dSJames Smart 	lport->ops->remoteport_delete(&rport->remoteport);
470e399441dSJames Smart 
471e399441dSJames Smart 	ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
472e399441dSJames Smart 
473e399441dSJames Smart 	kfree(rport);
474e399441dSJames Smart 
475e399441dSJames Smart 	nvme_fc_lport_put(lport);
476e399441dSJames Smart }
477e399441dSJames Smart 
478e399441dSJames Smart static void
479e399441dSJames Smart nvme_fc_rport_put(struct nvme_fc_rport *rport)
480e399441dSJames Smart {
481e399441dSJames Smart 	kref_put(&rport->ref, nvme_fc_free_rport);
482e399441dSJames Smart }
483e399441dSJames Smart 
484e399441dSJames Smart static int
485e399441dSJames Smart nvme_fc_rport_get(struct nvme_fc_rport *rport)
486e399441dSJames Smart {
487e399441dSJames Smart 	return kref_get_unless_zero(&rport->ref);
488e399441dSJames Smart }
489e399441dSJames Smart 
490e399441dSJames Smart /**
491e399441dSJames Smart  * nvme_fc_unregister_remoteport - transport entry point called by an
492e399441dSJames Smart  *                              LLDD to deregister/remove a previously
493e399441dSJames Smart  *                              registered a NVME subsystem FC port.
494e399441dSJames Smart  * @remoteport: pointer to the (registered) remote port that is to be
495e399441dSJames Smart  *              deregistered.
496e399441dSJames Smart  *
497e399441dSJames Smart  * Returns:
498e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
499e399441dSJames Smart  * (ex: -ENXIO) upon failure.
500e399441dSJames Smart  */
501e399441dSJames Smart int
502e399441dSJames Smart nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
503e399441dSJames Smart {
504e399441dSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
505e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl;
506e399441dSJames Smart 	unsigned long flags;
507e399441dSJames Smart 
508e399441dSJames Smart 	if (!portptr)
509e399441dSJames Smart 		return -EINVAL;
510e399441dSJames Smart 
511e399441dSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
512e399441dSJames Smart 
513e399441dSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
514e399441dSJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
515e399441dSJames Smart 		return -EINVAL;
516e399441dSJames Smart 	}
517e399441dSJames Smart 	portptr->port_state = FC_OBJSTATE_DELETED;
518e399441dSJames Smart 
519e399441dSJames Smart 	/* tear down all associations to the remote port */
520e399441dSJames Smart 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
521e399441dSJames Smart 		__nvme_fc_del_ctrl(ctrl);
522e399441dSJames Smart 
523e399441dSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
524e399441dSJames Smart 
525e399441dSJames Smart 	nvme_fc_rport_put(rport);
526e399441dSJames Smart 	return 0;
527e399441dSJames Smart }
528e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
529e399441dSJames Smart 
530e399441dSJames Smart 
531e399441dSJames Smart /* *********************** FC-NVME DMA Handling **************************** */
532e399441dSJames Smart 
533e399441dSJames Smart /*
534e399441dSJames Smart  * The fcloop device passes in a NULL device pointer. Real LLD's will
535e399441dSJames Smart  * pass in a valid device pointer. If NULL is passed to the dma mapping
536e399441dSJames Smart  * routines, depending on the platform, it may or may not succeed, and
537e399441dSJames Smart  * may crash.
538e399441dSJames Smart  *
539e399441dSJames Smart  * As such:
540e399441dSJames Smart  * Wrapper all the dma routines and check the dev pointer.
541e399441dSJames Smart  *
542e399441dSJames Smart  * If simple mappings (return just a dma address, we'll noop them,
543e399441dSJames Smart  * returning a dma address of 0.
544e399441dSJames Smart  *
545e399441dSJames Smart  * On more complex mappings (dma_map_sg), a pseudo routine fills
546e399441dSJames Smart  * in the scatter list, setting all dma addresses to 0.
547e399441dSJames Smart  */
548e399441dSJames Smart 
549e399441dSJames Smart static inline dma_addr_t
550e399441dSJames Smart fc_dma_map_single(struct device *dev, void *ptr, size_t size,
551e399441dSJames Smart 		enum dma_data_direction dir)
552e399441dSJames Smart {
553e399441dSJames Smart 	return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
554e399441dSJames Smart }
555e399441dSJames Smart 
556e399441dSJames Smart static inline int
557e399441dSJames Smart fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
558e399441dSJames Smart {
559e399441dSJames Smart 	return dev ? dma_mapping_error(dev, dma_addr) : 0;
560e399441dSJames Smart }
561e399441dSJames Smart 
562e399441dSJames Smart static inline void
563e399441dSJames Smart fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
564e399441dSJames Smart 	enum dma_data_direction dir)
565e399441dSJames Smart {
566e399441dSJames Smart 	if (dev)
567e399441dSJames Smart 		dma_unmap_single(dev, addr, size, dir);
568e399441dSJames Smart }
569e399441dSJames Smart 
570e399441dSJames Smart static inline void
571e399441dSJames Smart fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
572e399441dSJames Smart 		enum dma_data_direction dir)
573e399441dSJames Smart {
574e399441dSJames Smart 	if (dev)
575e399441dSJames Smart 		dma_sync_single_for_cpu(dev, addr, size, dir);
576e399441dSJames Smart }
577e399441dSJames Smart 
578e399441dSJames Smart static inline void
579e399441dSJames Smart fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
580e399441dSJames Smart 		enum dma_data_direction dir)
581e399441dSJames Smart {
582e399441dSJames Smart 	if (dev)
583e399441dSJames Smart 		dma_sync_single_for_device(dev, addr, size, dir);
584e399441dSJames Smart }
585e399441dSJames Smart 
586e399441dSJames Smart /* pseudo dma_map_sg call */
587e399441dSJames Smart static int
588e399441dSJames Smart fc_map_sg(struct scatterlist *sg, int nents)
589e399441dSJames Smart {
590e399441dSJames Smart 	struct scatterlist *s;
591e399441dSJames Smart 	int i;
592e399441dSJames Smart 
593e399441dSJames Smart 	WARN_ON(nents == 0 || sg[0].length == 0);
594e399441dSJames Smart 
595e399441dSJames Smart 	for_each_sg(sg, s, nents, i) {
596e399441dSJames Smart 		s->dma_address = 0L;
597e399441dSJames Smart #ifdef CONFIG_NEED_SG_DMA_LENGTH
598e399441dSJames Smart 		s->dma_length = s->length;
599e399441dSJames Smart #endif
600e399441dSJames Smart 	}
601e399441dSJames Smart 	return nents;
602e399441dSJames Smart }
603e399441dSJames Smart 
604e399441dSJames Smart static inline int
605e399441dSJames Smart fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
606e399441dSJames Smart 		enum dma_data_direction dir)
607e399441dSJames Smart {
608e399441dSJames Smart 	return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
609e399441dSJames Smart }
610e399441dSJames Smart 
611e399441dSJames Smart static inline void
612e399441dSJames Smart fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
613e399441dSJames Smart 		enum dma_data_direction dir)
614e399441dSJames Smart {
615e399441dSJames Smart 	if (dev)
616e399441dSJames Smart 		dma_unmap_sg(dev, sg, nents, dir);
617e399441dSJames Smart }
618e399441dSJames Smart 
619e399441dSJames Smart 
620e399441dSJames Smart /* *********************** FC-NVME LS Handling **************************** */
621e399441dSJames Smart 
622e399441dSJames Smart static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
623e399441dSJames Smart static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
624e399441dSJames Smart 
625e399441dSJames Smart 
626e399441dSJames Smart static void
627e399441dSJames Smart __nvme_fc_finish_ls_req(struct nvme_fc_ctrl *ctrl,
628e399441dSJames Smart 		struct nvmefc_ls_req_op *lsop)
629e399441dSJames Smart {
630e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
631e399441dSJames Smart 	unsigned long flags;
632e399441dSJames Smart 
633e399441dSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
634e399441dSJames Smart 
635e399441dSJames Smart 	if (!lsop->req_queued) {
636e399441dSJames Smart 		spin_unlock_irqrestore(&ctrl->lock, flags);
637e399441dSJames Smart 		return;
638e399441dSJames Smart 	}
639e399441dSJames Smart 
640e399441dSJames Smart 	list_del(&lsop->lsreq_list);
641e399441dSJames Smart 
642e399441dSJames Smart 	lsop->req_queued = false;
643e399441dSJames Smart 
644e399441dSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
645e399441dSJames Smart 
646e399441dSJames Smart 	fc_dma_unmap_single(ctrl->dev, lsreq->rqstdma,
647e399441dSJames Smart 				  (lsreq->rqstlen + lsreq->rsplen),
648e399441dSJames Smart 				  DMA_BIDIRECTIONAL);
649e399441dSJames Smart 
650e399441dSJames Smart 	nvme_fc_ctrl_put(ctrl);
651e399441dSJames Smart }
652e399441dSJames Smart 
653e399441dSJames Smart static int
654e399441dSJames Smart __nvme_fc_send_ls_req(struct nvme_fc_ctrl *ctrl,
655e399441dSJames Smart 		struct nvmefc_ls_req_op *lsop,
656e399441dSJames Smart 		void (*done)(struct nvmefc_ls_req *req, int status))
657e399441dSJames Smart {
658e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
659e399441dSJames Smart 	unsigned long flags;
660e399441dSJames Smart 	int ret;
661e399441dSJames Smart 
662e399441dSJames Smart 	if (!nvme_fc_ctrl_get(ctrl))
663e399441dSJames Smart 		return -ESHUTDOWN;
664e399441dSJames Smart 
665e399441dSJames Smart 	lsreq->done = done;
666e399441dSJames Smart 	lsop->ctrl = ctrl;
667e399441dSJames Smart 	lsop->req_queued = false;
668e399441dSJames Smart 	INIT_LIST_HEAD(&lsop->lsreq_list);
669e399441dSJames Smart 	init_completion(&lsop->ls_done);
670e399441dSJames Smart 
671e399441dSJames Smart 	lsreq->rqstdma = fc_dma_map_single(ctrl->dev, lsreq->rqstaddr,
672e399441dSJames Smart 				  lsreq->rqstlen + lsreq->rsplen,
673e399441dSJames Smart 				  DMA_BIDIRECTIONAL);
674e399441dSJames Smart 	if (fc_dma_mapping_error(ctrl->dev, lsreq->rqstdma)) {
675e399441dSJames Smart 		nvme_fc_ctrl_put(ctrl);
676e399441dSJames Smart 		dev_err(ctrl->dev,
677e399441dSJames Smart 			"els request command failed EFAULT.\n");
678e399441dSJames Smart 		return -EFAULT;
679e399441dSJames Smart 	}
680e399441dSJames Smart 	lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
681e399441dSJames Smart 
682e399441dSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
683e399441dSJames Smart 
684e399441dSJames Smart 	list_add_tail(&lsop->lsreq_list, &ctrl->ls_req_list);
685e399441dSJames Smart 
686e399441dSJames Smart 	lsop->req_queued = true;
687e399441dSJames Smart 
688e399441dSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
689e399441dSJames Smart 
690e399441dSJames Smart 	ret = ctrl->lport->ops->ls_req(&ctrl->lport->localport,
691e399441dSJames Smart 					&ctrl->rport->remoteport, lsreq);
692e399441dSJames Smart 	if (ret)
693e399441dSJames Smart 		lsop->ls_error = ret;
694e399441dSJames Smart 
695e399441dSJames Smart 	return ret;
696e399441dSJames Smart }
697e399441dSJames Smart 
698e399441dSJames Smart static void
699e399441dSJames Smart nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
700e399441dSJames Smart {
701e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
702e399441dSJames Smart 
703e399441dSJames Smart 	lsop->ls_error = status;
704e399441dSJames Smart 	complete(&lsop->ls_done);
705e399441dSJames Smart }
706e399441dSJames Smart 
707e399441dSJames Smart static int
708e399441dSJames Smart nvme_fc_send_ls_req(struct nvme_fc_ctrl *ctrl, struct nvmefc_ls_req_op *lsop)
709e399441dSJames Smart {
710e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
711e399441dSJames Smart 	struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
712e399441dSJames Smart 	int ret;
713e399441dSJames Smart 
714e399441dSJames Smart 	ret = __nvme_fc_send_ls_req(ctrl, lsop, nvme_fc_send_ls_req_done);
715e399441dSJames Smart 
716e399441dSJames Smart 	if (!ret)
717e399441dSJames Smart 		/*
718e399441dSJames Smart 		 * No timeout/not interruptible as we need the struct
719e399441dSJames Smart 		 * to exist until the lldd calls us back. Thus mandate
720e399441dSJames Smart 		 * wait until driver calls back. lldd responsible for
721e399441dSJames Smart 		 * the timeout action
722e399441dSJames Smart 		 */
723e399441dSJames Smart 		wait_for_completion(&lsop->ls_done);
724e399441dSJames Smart 
725e399441dSJames Smart 	__nvme_fc_finish_ls_req(ctrl, lsop);
726e399441dSJames Smart 
727e399441dSJames Smart 	if (ret) {
728e399441dSJames Smart 		dev_err(ctrl->dev,
729e399441dSJames Smart 			"ls request command failed (%d).\n", ret);
730e399441dSJames Smart 		return ret;
731e399441dSJames Smart 	}
732e399441dSJames Smart 
733e399441dSJames Smart 	/* ACC or RJT payload ? */
734e399441dSJames Smart 	if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
735e399441dSJames Smart 		return -ENXIO;
736e399441dSJames Smart 
737e399441dSJames Smart 	return 0;
738e399441dSJames Smart }
739e399441dSJames Smart 
740e399441dSJames Smart static void
741e399441dSJames Smart nvme_fc_send_ls_req_async(struct nvme_fc_ctrl *ctrl,
742e399441dSJames Smart 		struct nvmefc_ls_req_op *lsop,
743e399441dSJames Smart 		void (*done)(struct nvmefc_ls_req *req, int status))
744e399441dSJames Smart {
745e399441dSJames Smart 	int ret;
746e399441dSJames Smart 
747e399441dSJames Smart 	ret = __nvme_fc_send_ls_req(ctrl, lsop, done);
748e399441dSJames Smart 
749e399441dSJames Smart 	/* don't wait for completion */
750e399441dSJames Smart 
751e399441dSJames Smart 	if (ret)
752e399441dSJames Smart 		done(&lsop->ls_req, ret);
753e399441dSJames Smart }
754e399441dSJames Smart 
755e399441dSJames Smart /* Validation Error indexes into the string table below */
756e399441dSJames Smart enum {
757e399441dSJames Smart 	VERR_NO_ERROR		= 0,
758e399441dSJames Smart 	VERR_LSACC		= 1,
759e399441dSJames Smart 	VERR_LSDESC_RQST	= 2,
760e399441dSJames Smart 	VERR_LSDESC_RQST_LEN	= 3,
761e399441dSJames Smart 	VERR_ASSOC_ID		= 4,
762e399441dSJames Smart 	VERR_ASSOC_ID_LEN	= 5,
763e399441dSJames Smart 	VERR_CONN_ID		= 6,
764e399441dSJames Smart 	VERR_CONN_ID_LEN	= 7,
765e399441dSJames Smart 	VERR_CR_ASSOC		= 8,
766e399441dSJames Smart 	VERR_CR_ASSOC_ACC_LEN	= 9,
767e399441dSJames Smart 	VERR_CR_CONN		= 10,
768e399441dSJames Smart 	VERR_CR_CONN_ACC_LEN	= 11,
769e399441dSJames Smart 	VERR_DISCONN		= 12,
770e399441dSJames Smart 	VERR_DISCONN_ACC_LEN	= 13,
771e399441dSJames Smart };
772e399441dSJames Smart 
773e399441dSJames Smart static char *validation_errors[] = {
774e399441dSJames Smart 	"OK",
775e399441dSJames Smart 	"Not LS_ACC",
776e399441dSJames Smart 	"Not LSDESC_RQST",
777e399441dSJames Smart 	"Bad LSDESC_RQST Length",
778e399441dSJames Smart 	"Not Association ID",
779e399441dSJames Smart 	"Bad Association ID Length",
780e399441dSJames Smart 	"Not Connection ID",
781e399441dSJames Smart 	"Bad Connection ID Length",
782e399441dSJames Smart 	"Not CR_ASSOC Rqst",
783e399441dSJames Smart 	"Bad CR_ASSOC ACC Length",
784e399441dSJames Smart 	"Not CR_CONN Rqst",
785e399441dSJames Smart 	"Bad CR_CONN ACC Length",
786e399441dSJames Smart 	"Not Disconnect Rqst",
787e399441dSJames Smart 	"Bad Disconnect ACC Length",
788e399441dSJames Smart };
789e399441dSJames Smart 
790e399441dSJames Smart static int
791e399441dSJames Smart nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
792e399441dSJames Smart 	struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
793e399441dSJames Smart {
794e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
795e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
796e399441dSJames Smart 	struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
797e399441dSJames Smart 	struct fcnvme_ls_cr_assoc_acc *assoc_acc;
798e399441dSJames Smart 	int ret, fcret = 0;
799e399441dSJames Smart 
800e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
801e399441dSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz +
802e399441dSJames Smart 			 sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
803e399441dSJames Smart 	if (!lsop) {
804e399441dSJames Smart 		ret = -ENOMEM;
805e399441dSJames Smart 		goto out_no_memory;
806e399441dSJames Smart 	}
807e399441dSJames Smart 	lsreq = &lsop->ls_req;
808e399441dSJames Smart 
809e399441dSJames Smart 	lsreq->private = (void *)&lsop[1];
810e399441dSJames Smart 	assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
811e399441dSJames Smart 			(lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
812e399441dSJames Smart 	assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
813e399441dSJames Smart 
814e399441dSJames Smart 	assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
815e399441dSJames Smart 	assoc_rqst->desc_list_len =
816e399441dSJames Smart 			cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
817e399441dSJames Smart 
818e399441dSJames Smart 	assoc_rqst->assoc_cmd.desc_tag =
819e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
820e399441dSJames Smart 	assoc_rqst->assoc_cmd.desc_len =
821e399441dSJames Smart 			fcnvme_lsdesc_len(
822e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
823e399441dSJames Smart 
824e399441dSJames Smart 	assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
825e399441dSJames Smart 	assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize);
826e399441dSJames Smart 	/* Linux supports only Dynamic controllers */
827e399441dSJames Smart 	assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
828e399441dSJames Smart 	memcpy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id,
829e399441dSJames Smart 		min_t(size_t, FCNVME_ASSOC_HOSTID_LEN, sizeof(uuid_be)));
830e399441dSJames Smart 	strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
831e399441dSJames Smart 		min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
832e399441dSJames Smart 	strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
833e399441dSJames Smart 		min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
834e399441dSJames Smart 
835e399441dSJames Smart 	lsop->queue = queue;
836e399441dSJames Smart 	lsreq->rqstaddr = assoc_rqst;
837e399441dSJames Smart 	lsreq->rqstlen = sizeof(*assoc_rqst);
838e399441dSJames Smart 	lsreq->rspaddr = assoc_acc;
839e399441dSJames Smart 	lsreq->rsplen = sizeof(*assoc_acc);
840e399441dSJames Smart 	lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
841e399441dSJames Smart 
842e399441dSJames Smart 	ret = nvme_fc_send_ls_req(ctrl, lsop);
843e399441dSJames Smart 	if (ret)
844e399441dSJames Smart 		goto out_free_buffer;
845e399441dSJames Smart 
846e399441dSJames Smart 	/* process connect LS completion */
847e399441dSJames Smart 
848e399441dSJames Smart 	/* validate the ACC response */
849e399441dSJames Smart 	if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
850e399441dSJames Smart 		fcret = VERR_LSACC;
851f77fc87cSJames Smart 	else if (assoc_acc->hdr.desc_list_len !=
852e399441dSJames Smart 			fcnvme_lsdesc_len(
853e399441dSJames Smart 				sizeof(struct fcnvme_ls_cr_assoc_acc)))
854e399441dSJames Smart 		fcret = VERR_CR_ASSOC_ACC_LEN;
855f77fc87cSJames Smart 	else if (assoc_acc->hdr.rqst.desc_tag !=
856f77fc87cSJames Smart 			cpu_to_be32(FCNVME_LSDESC_RQST))
857e399441dSJames Smart 		fcret = VERR_LSDESC_RQST;
858e399441dSJames Smart 	else if (assoc_acc->hdr.rqst.desc_len !=
859e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
860e399441dSJames Smart 		fcret = VERR_LSDESC_RQST_LEN;
861e399441dSJames Smart 	else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
862e399441dSJames Smart 		fcret = VERR_CR_ASSOC;
863e399441dSJames Smart 	else if (assoc_acc->associd.desc_tag !=
864e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
865e399441dSJames Smart 		fcret = VERR_ASSOC_ID;
866e399441dSJames Smart 	else if (assoc_acc->associd.desc_len !=
867e399441dSJames Smart 			fcnvme_lsdesc_len(
868e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id)))
869e399441dSJames Smart 		fcret = VERR_ASSOC_ID_LEN;
870e399441dSJames Smart 	else if (assoc_acc->connectid.desc_tag !=
871e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CONN_ID))
872e399441dSJames Smart 		fcret = VERR_CONN_ID;
873e399441dSJames Smart 	else if (assoc_acc->connectid.desc_len !=
874e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
875e399441dSJames Smart 		fcret = VERR_CONN_ID_LEN;
876e399441dSJames Smart 
877e399441dSJames Smart 	if (fcret) {
878e399441dSJames Smart 		ret = -EBADF;
879e399441dSJames Smart 		dev_err(ctrl->dev,
880e399441dSJames Smart 			"q %d connect failed: %s\n",
881e399441dSJames Smart 			queue->qnum, validation_errors[fcret]);
882e399441dSJames Smart 	} else {
883e399441dSJames Smart 		ctrl->association_id =
884e399441dSJames Smart 			be64_to_cpu(assoc_acc->associd.association_id);
885e399441dSJames Smart 		queue->connection_id =
886e399441dSJames Smart 			be64_to_cpu(assoc_acc->connectid.connection_id);
887e399441dSJames Smart 		set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
888e399441dSJames Smart 	}
889e399441dSJames Smart 
890e399441dSJames Smart out_free_buffer:
891e399441dSJames Smart 	kfree(lsop);
892e399441dSJames Smart out_no_memory:
893e399441dSJames Smart 	if (ret)
894e399441dSJames Smart 		dev_err(ctrl->dev,
895e399441dSJames Smart 			"queue %d connect admin queue failed (%d).\n",
896e399441dSJames Smart 			queue->qnum, ret);
897e399441dSJames Smart 	return ret;
898e399441dSJames Smart }
899e399441dSJames Smart 
900e399441dSJames Smart static int
901e399441dSJames Smart nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
902e399441dSJames Smart 			u16 qsize, u16 ersp_ratio)
903e399441dSJames Smart {
904e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
905e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
906e399441dSJames Smart 	struct fcnvme_ls_cr_conn_rqst *conn_rqst;
907e399441dSJames Smart 	struct fcnvme_ls_cr_conn_acc *conn_acc;
908e399441dSJames Smart 	int ret, fcret = 0;
909e399441dSJames Smart 
910e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
911e399441dSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz +
912e399441dSJames Smart 			 sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
913e399441dSJames Smart 	if (!lsop) {
914e399441dSJames Smart 		ret = -ENOMEM;
915e399441dSJames Smart 		goto out_no_memory;
916e399441dSJames Smart 	}
917e399441dSJames Smart 	lsreq = &lsop->ls_req;
918e399441dSJames Smart 
919e399441dSJames Smart 	lsreq->private = (void *)&lsop[1];
920e399441dSJames Smart 	conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
921e399441dSJames Smart 			(lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
922e399441dSJames Smart 	conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
923e399441dSJames Smart 
924e399441dSJames Smart 	conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
925e399441dSJames Smart 	conn_rqst->desc_list_len = cpu_to_be32(
926e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id) +
927e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
928e399441dSJames Smart 
929e399441dSJames Smart 	conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
930e399441dSJames Smart 	conn_rqst->associd.desc_len =
931e399441dSJames Smart 			fcnvme_lsdesc_len(
932e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id));
933e399441dSJames Smart 	conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
934e399441dSJames Smart 	conn_rqst->connect_cmd.desc_tag =
935e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
936e399441dSJames Smart 	conn_rqst->connect_cmd.desc_len =
937e399441dSJames Smart 			fcnvme_lsdesc_len(
938e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
939e399441dSJames Smart 	conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
940e399441dSJames Smart 	conn_rqst->connect_cmd.qid  = cpu_to_be16(queue->qnum);
941e399441dSJames Smart 	conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize);
942e399441dSJames Smart 
943e399441dSJames Smart 	lsop->queue = queue;
944e399441dSJames Smart 	lsreq->rqstaddr = conn_rqst;
945e399441dSJames Smart 	lsreq->rqstlen = sizeof(*conn_rqst);
946e399441dSJames Smart 	lsreq->rspaddr = conn_acc;
947e399441dSJames Smart 	lsreq->rsplen = sizeof(*conn_acc);
948e399441dSJames Smart 	lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
949e399441dSJames Smart 
950e399441dSJames Smart 	ret = nvme_fc_send_ls_req(ctrl, lsop);
951e399441dSJames Smart 	if (ret)
952e399441dSJames Smart 		goto out_free_buffer;
953e399441dSJames Smart 
954e399441dSJames Smart 	/* process connect LS completion */
955e399441dSJames Smart 
956e399441dSJames Smart 	/* validate the ACC response */
957e399441dSJames Smart 	if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
958e399441dSJames Smart 		fcret = VERR_LSACC;
959f77fc87cSJames Smart 	else if (conn_acc->hdr.desc_list_len !=
960e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
961e399441dSJames Smart 		fcret = VERR_CR_CONN_ACC_LEN;
962f77fc87cSJames Smart 	else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
963e399441dSJames Smart 		fcret = VERR_LSDESC_RQST;
964e399441dSJames Smart 	else if (conn_acc->hdr.rqst.desc_len !=
965e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
966e399441dSJames Smart 		fcret = VERR_LSDESC_RQST_LEN;
967e399441dSJames Smart 	else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
968e399441dSJames Smart 		fcret = VERR_CR_CONN;
969e399441dSJames Smart 	else if (conn_acc->connectid.desc_tag !=
970e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CONN_ID))
971e399441dSJames Smart 		fcret = VERR_CONN_ID;
972e399441dSJames Smart 	else if (conn_acc->connectid.desc_len !=
973e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
974e399441dSJames Smart 		fcret = VERR_CONN_ID_LEN;
975e399441dSJames Smart 
976e399441dSJames Smart 	if (fcret) {
977e399441dSJames Smart 		ret = -EBADF;
978e399441dSJames Smart 		dev_err(ctrl->dev,
979e399441dSJames Smart 			"q %d connect failed: %s\n",
980e399441dSJames Smart 			queue->qnum, validation_errors[fcret]);
981e399441dSJames Smart 	} else {
982e399441dSJames Smart 		queue->connection_id =
983e399441dSJames Smart 			be64_to_cpu(conn_acc->connectid.connection_id);
984e399441dSJames Smart 		set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
985e399441dSJames Smart 	}
986e399441dSJames Smart 
987e399441dSJames Smart out_free_buffer:
988e399441dSJames Smart 	kfree(lsop);
989e399441dSJames Smart out_no_memory:
990e399441dSJames Smart 	if (ret)
991e399441dSJames Smart 		dev_err(ctrl->dev,
992e399441dSJames Smart 			"queue %d connect command failed (%d).\n",
993e399441dSJames Smart 			queue->qnum, ret);
994e399441dSJames Smart 	return ret;
995e399441dSJames Smart }
996e399441dSJames Smart 
997e399441dSJames Smart static void
998e399441dSJames Smart nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
999e399441dSJames Smart {
1000e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1001e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = lsop->ctrl;
1002e399441dSJames Smart 
1003e399441dSJames Smart 	__nvme_fc_finish_ls_req(ctrl, lsop);
1004e399441dSJames Smart 
1005e399441dSJames Smart 	if (status)
1006e399441dSJames Smart 		dev_err(ctrl->dev,
1007e399441dSJames Smart 			"disconnect assoc ls request command failed (%d).\n",
1008e399441dSJames Smart 			status);
1009e399441dSJames Smart 
1010e399441dSJames Smart 	/* fc-nvme iniator doesn't care about success or failure of cmd */
1011e399441dSJames Smart 
1012e399441dSJames Smart 	kfree(lsop);
1013e399441dSJames Smart }
1014e399441dSJames Smart 
1015e399441dSJames Smart /*
1016e399441dSJames Smart  * This routine sends a FC-NVME LS to disconnect (aka terminate)
1017e399441dSJames Smart  * the FC-NVME Association.  Terminating the association also
1018e399441dSJames Smart  * terminates the FC-NVME connections (per queue, both admin and io
1019e399441dSJames Smart  * queues) that are part of the association. E.g. things are torn
1020e399441dSJames Smart  * down, and the related FC-NVME Association ID and Connection IDs
1021e399441dSJames Smart  * become invalid.
1022e399441dSJames Smart  *
1023e399441dSJames Smart  * The behavior of the fc-nvme initiator is such that it's
1024e399441dSJames Smart  * understanding of the association and connections will implicitly
1025e399441dSJames Smart  * be torn down. The action is implicit as it may be due to a loss of
1026e399441dSJames Smart  * connectivity with the fc-nvme target, so you may never get a
1027e399441dSJames Smart  * response even if you tried.  As such, the action of this routine
1028e399441dSJames Smart  * is to asynchronously send the LS, ignore any results of the LS, and
1029e399441dSJames Smart  * continue on with terminating the association. If the fc-nvme target
1030e399441dSJames Smart  * is present and receives the LS, it too can tear down.
1031e399441dSJames Smart  */
1032e399441dSJames Smart static void
1033e399441dSJames Smart nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1034e399441dSJames Smart {
1035e399441dSJames Smart 	struct fcnvme_ls_disconnect_rqst *discon_rqst;
1036e399441dSJames Smart 	struct fcnvme_ls_disconnect_acc *discon_acc;
1037e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
1038e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
1039e399441dSJames Smart 
1040e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
1041e399441dSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz +
1042e399441dSJames Smart 			 sizeof(*discon_rqst) + sizeof(*discon_acc)),
1043e399441dSJames Smart 			GFP_KERNEL);
1044e399441dSJames Smart 	if (!lsop)
1045e399441dSJames Smart 		/* couldn't sent it... too bad */
1046e399441dSJames Smart 		return;
1047e399441dSJames Smart 
1048e399441dSJames Smart 	lsreq = &lsop->ls_req;
1049e399441dSJames Smart 
1050e399441dSJames Smart 	lsreq->private = (void *)&lsop[1];
1051e399441dSJames Smart 	discon_rqst = (struct fcnvme_ls_disconnect_rqst *)
1052e399441dSJames Smart 			(lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1053e399441dSJames Smart 	discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1];
1054e399441dSJames Smart 
1055e399441dSJames Smart 	discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT;
1056e399441dSJames Smart 	discon_rqst->desc_list_len = cpu_to_be32(
1057e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id) +
1058e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_disconn_cmd));
1059e399441dSJames Smart 
1060e399441dSJames Smart 	discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1061e399441dSJames Smart 	discon_rqst->associd.desc_len =
1062e399441dSJames Smart 			fcnvme_lsdesc_len(
1063e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id));
1064e399441dSJames Smart 
1065e399441dSJames Smart 	discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1066e399441dSJames Smart 
1067e399441dSJames Smart 	discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
1068e399441dSJames Smart 						FCNVME_LSDESC_DISCONN_CMD);
1069e399441dSJames Smart 	discon_rqst->discon_cmd.desc_len =
1070e399441dSJames Smart 			fcnvme_lsdesc_len(
1071e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_disconn_cmd));
1072e399441dSJames Smart 	discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION;
1073e399441dSJames Smart 	discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id);
1074e399441dSJames Smart 
1075e399441dSJames Smart 	lsreq->rqstaddr = discon_rqst;
1076e399441dSJames Smart 	lsreq->rqstlen = sizeof(*discon_rqst);
1077e399441dSJames Smart 	lsreq->rspaddr = discon_acc;
1078e399441dSJames Smart 	lsreq->rsplen = sizeof(*discon_acc);
1079e399441dSJames Smart 	lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1080e399441dSJames Smart 
1081e399441dSJames Smart 	nvme_fc_send_ls_req_async(ctrl, lsop, nvme_fc_disconnect_assoc_done);
1082e399441dSJames Smart 
1083e399441dSJames Smart 	/* only meaningful part to terminating the association */
1084e399441dSJames Smart 	ctrl->association_id = 0;
1085e399441dSJames Smart }
1086e399441dSJames Smart 
1087e399441dSJames Smart 
1088e399441dSJames Smart /* *********************** NVME Ctrl Routines **************************** */
1089e399441dSJames Smart 
1090e399441dSJames Smart 
1091e399441dSJames Smart static int
1092e399441dSJames Smart nvme_fc_reinit_request(void *data, struct request *rq)
1093e399441dSJames Smart {
1094e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1095e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1096e399441dSJames Smart 
1097e399441dSJames Smart 	memset(cmdiu, 0, sizeof(*cmdiu));
1098e399441dSJames Smart 	cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1099e399441dSJames Smart 	cmdiu->fc_id = NVME_CMD_FC_ID;
1100e399441dSJames Smart 	cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1101e399441dSJames Smart 	memset(&op->rsp_iu, 0, sizeof(op->rsp_iu));
1102e399441dSJames Smart 
1103e399441dSJames Smart 	return 0;
1104e399441dSJames Smart }
1105e399441dSJames Smart 
1106e399441dSJames Smart static void
1107e399441dSJames Smart __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1108e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
1109e399441dSJames Smart {
1110e399441dSJames Smart 	fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1111e399441dSJames Smart 				sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1112e399441dSJames Smart 	fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1113e399441dSJames Smart 				sizeof(op->cmd_iu), DMA_TO_DEVICE);
1114e399441dSJames Smart 
1115e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_UNINIT);
1116e399441dSJames Smart }
1117e399441dSJames Smart 
1118e399441dSJames Smart static void
1119e399441dSJames Smart nvme_fc_exit_request(void *data, struct request *rq,
1120e399441dSJames Smart 				unsigned int hctx_idx, unsigned int rq_idx)
1121e399441dSJames Smart {
1122e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1123e399441dSJames Smart 
1124e399441dSJames Smart 	return __nvme_fc_exit_request(data, op);
1125e399441dSJames Smart }
1126e399441dSJames Smart 
1127e399441dSJames Smart static void
1128e399441dSJames Smart nvme_fc_exit_aen_ops(struct nvme_fc_ctrl *ctrl)
1129e399441dSJames Smart {
1130e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
1131e399441dSJames Smart 	int i;
1132e399441dSJames Smart 
1133e399441dSJames Smart 	for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
1134e399441dSJames Smart 		if (atomic_read(&aen_op->state) == FCPOP_STATE_UNINIT)
1135e399441dSJames Smart 			continue;
1136e399441dSJames Smart 		__nvme_fc_exit_request(ctrl, aen_op);
1137e399441dSJames Smart 		nvme_fc_ctrl_put(ctrl);
1138e399441dSJames Smart 	}
1139e399441dSJames Smart }
1140e399441dSJames Smart 
1141e399441dSJames Smart void
1142e399441dSJames Smart nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1143e399441dSJames Smart {
1144e399441dSJames Smart 	struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1145e399441dSJames Smart 	struct request *rq = op->rq;
1146e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
1147e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
1148e399441dSJames Smart 	struct nvme_fc_queue *queue = op->queue;
1149e399441dSJames Smart 	struct nvme_completion *cqe = &op->rsp_iu.cqe;
1150d663b69fSChristoph Hellwig 	__le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
115127fa9bc5SChristoph Hellwig 	union nvme_result result;
1152e399441dSJames Smart 
1153e399441dSJames Smart 	/*
1154e399441dSJames Smart 	 * WARNING:
1155e399441dSJames Smart 	 * The current linux implementation of a nvme controller
1156e399441dSJames Smart 	 * allocates a single tag set for all io queues and sizes
1157e399441dSJames Smart 	 * the io queues to fully hold all possible tags. Thus, the
1158e399441dSJames Smart 	 * implementation does not reference or care about the sqhd
1159e399441dSJames Smart 	 * value as it never needs to use the sqhd/sqtail pointers
1160e399441dSJames Smart 	 * for submission pacing.
1161e399441dSJames Smart 	 *
1162e399441dSJames Smart 	 * This affects the FC-NVME implementation in two ways:
1163e399441dSJames Smart 	 * 1) As the value doesn't matter, we don't need to waste
1164e399441dSJames Smart 	 *    cycles extracting it from ERSPs and stamping it in the
1165e399441dSJames Smart 	 *    cases where the transport fabricates CQEs on successful
1166e399441dSJames Smart 	 *    completions.
1167e399441dSJames Smart 	 * 2) The FC-NVME implementation requires that delivery of
1168e399441dSJames Smart 	 *    ERSP completions are to go back to the nvme layer in order
1169e399441dSJames Smart 	 *    relative to the rsn, such that the sqhd value will always
1170e399441dSJames Smart 	 *    be "in order" for the nvme layer. As the nvme layer in
1171e399441dSJames Smart 	 *    linux doesn't care about sqhd, there's no need to return
1172e399441dSJames Smart 	 *    them in order.
1173e399441dSJames Smart 	 *
1174e399441dSJames Smart 	 * Additionally:
1175e399441dSJames Smart 	 * As the core nvme layer in linux currently does not look at
1176e399441dSJames Smart 	 * every field in the cqe - in cases where the FC transport must
1177e399441dSJames Smart 	 * fabricate a CQE, the following fields will not be set as they
1178e399441dSJames Smart 	 * are not referenced:
1179e399441dSJames Smart 	 *      cqe.sqid,  cqe.sqhd,  cqe.command_id
1180e399441dSJames Smart 	 */
1181e399441dSJames Smart 
1182e399441dSJames Smart 	fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1183e399441dSJames Smart 				sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1184e399441dSJames Smart 
1185e399441dSJames Smart 	if (atomic_read(&op->state) == FCPOP_STATE_ABORTED)
1186d663b69fSChristoph Hellwig 		status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
118762eeacb0SJames Smart 	else if (freq->status)
1188d663b69fSChristoph Hellwig 		status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
1189e399441dSJames Smart 
1190e399441dSJames Smart 	/*
1191e399441dSJames Smart 	 * For the linux implementation, if we have an unsuccesful
1192e399441dSJames Smart 	 * status, they blk-mq layer can typically be called with the
1193e399441dSJames Smart 	 * non-zero status and the content of the cqe isn't important.
1194e399441dSJames Smart 	 */
1195e399441dSJames Smart 	if (status)
1196e399441dSJames Smart 		goto done;
1197e399441dSJames Smart 
1198e399441dSJames Smart 	/*
1199e399441dSJames Smart 	 * command completed successfully relative to the wire
1200e399441dSJames Smart 	 * protocol. However, validate anything received and
1201e399441dSJames Smart 	 * extract the status and result from the cqe (create it
1202e399441dSJames Smart 	 * where necessary).
1203e399441dSJames Smart 	 */
1204e399441dSJames Smart 
1205e399441dSJames Smart 	switch (freq->rcv_rsplen) {
1206e399441dSJames Smart 
1207e399441dSJames Smart 	case 0:
1208e399441dSJames Smart 	case NVME_FC_SIZEOF_ZEROS_RSP:
1209e399441dSJames Smart 		/*
1210e399441dSJames Smart 		 * No response payload or 12 bytes of payload (which
1211e399441dSJames Smart 		 * should all be zeros) are considered successful and
1212e399441dSJames Smart 		 * no payload in the CQE by the transport.
1213e399441dSJames Smart 		 */
1214e399441dSJames Smart 		if (freq->transferred_length !=
1215e399441dSJames Smart 			be32_to_cpu(op->cmd_iu.data_len)) {
1216d663b69fSChristoph Hellwig 			status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
1217e399441dSJames Smart 			goto done;
1218e399441dSJames Smart 		}
121927fa9bc5SChristoph Hellwig 		result.u64 = 0;
1220e399441dSJames Smart 		break;
1221e399441dSJames Smart 
1222e399441dSJames Smart 	case sizeof(struct nvme_fc_ersp_iu):
1223e399441dSJames Smart 		/*
1224e399441dSJames Smart 		 * The ERSP IU contains a full completion with CQE.
1225e399441dSJames Smart 		 * Validate ERSP IU and look at cqe.
1226e399441dSJames Smart 		 */
1227e399441dSJames Smart 		if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1228e399441dSJames Smart 					(freq->rcv_rsplen / 4) ||
1229e399441dSJames Smart 			     be32_to_cpu(op->rsp_iu.xfrd_len) !=
1230e399441dSJames Smart 					freq->transferred_length ||
1231726a1080SJames Smart 			     op->rsp_iu.status_code ||
1232e399441dSJames Smart 			     op->rqno != le16_to_cpu(cqe->command_id))) {
1233d663b69fSChristoph Hellwig 			status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
1234e399441dSJames Smart 			goto done;
1235e399441dSJames Smart 		}
123627fa9bc5SChristoph Hellwig 		result = cqe->result;
1237d663b69fSChristoph Hellwig 		status = cqe->status;
1238e399441dSJames Smart 		break;
1239e399441dSJames Smart 
1240e399441dSJames Smart 	default:
1241d663b69fSChristoph Hellwig 		status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
1242e399441dSJames Smart 		goto done;
1243e399441dSJames Smart 	}
1244e399441dSJames Smart 
1245e399441dSJames Smart done:
1246e399441dSJames Smart 	if (!queue->qnum && op->rqno >= AEN_CMDID_BASE) {
124727fa9bc5SChristoph Hellwig 		nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
1248e399441dSJames Smart 		nvme_fc_ctrl_put(ctrl);
1249e399441dSJames Smart 		return;
1250e399441dSJames Smart 	}
1251e399441dSJames Smart 
125227fa9bc5SChristoph Hellwig 	nvme_end_request(rq, status, result);
1253e399441dSJames Smart }
1254e399441dSJames Smart 
1255e399441dSJames Smart static int
1256e399441dSJames Smart __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
1257e399441dSJames Smart 		struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
1258e399441dSJames Smart 		struct request *rq, u32 rqno)
1259e399441dSJames Smart {
1260e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1261e399441dSJames Smart 	int ret = 0;
1262e399441dSJames Smart 
1263e399441dSJames Smart 	memset(op, 0, sizeof(*op));
1264e399441dSJames Smart 	op->fcp_req.cmdaddr = &op->cmd_iu;
1265e399441dSJames Smart 	op->fcp_req.cmdlen = sizeof(op->cmd_iu);
1266e399441dSJames Smart 	op->fcp_req.rspaddr = &op->rsp_iu;
1267e399441dSJames Smart 	op->fcp_req.rsplen = sizeof(op->rsp_iu);
1268e399441dSJames Smart 	op->fcp_req.done = nvme_fc_fcpio_done;
1269e399441dSJames Smart 	op->fcp_req.first_sgl = (struct scatterlist *)&op[1];
1270e399441dSJames Smart 	op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE];
1271e399441dSJames Smart 	op->ctrl = ctrl;
1272e399441dSJames Smart 	op->queue = queue;
1273e399441dSJames Smart 	op->rq = rq;
1274e399441dSJames Smart 	op->rqno = rqno;
1275e399441dSJames Smart 
1276e399441dSJames Smart 	cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1277e399441dSJames Smart 	cmdiu->fc_id = NVME_CMD_FC_ID;
1278e399441dSJames Smart 	cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1279e399441dSJames Smart 
1280e399441dSJames Smart 	op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
1281e399441dSJames Smart 				&op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
1282e399441dSJames Smart 	if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
1283e399441dSJames Smart 		dev_err(ctrl->dev,
1284e399441dSJames Smart 			"FCP Op failed - cmdiu dma mapping failed.\n");
1285e399441dSJames Smart 		ret = EFAULT;
1286e399441dSJames Smart 		goto out_on_error;
1287e399441dSJames Smart 	}
1288e399441dSJames Smart 
1289e399441dSJames Smart 	op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
1290e399441dSJames Smart 				&op->rsp_iu, sizeof(op->rsp_iu),
1291e399441dSJames Smart 				DMA_FROM_DEVICE);
1292e399441dSJames Smart 	if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
1293e399441dSJames Smart 		dev_err(ctrl->dev,
1294e399441dSJames Smart 			"FCP Op failed - rspiu dma mapping failed.\n");
1295e399441dSJames Smart 		ret = EFAULT;
1296e399441dSJames Smart 	}
1297e399441dSJames Smart 
1298e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_IDLE);
1299e399441dSJames Smart out_on_error:
1300e399441dSJames Smart 	return ret;
1301e399441dSJames Smart }
1302e399441dSJames Smart 
1303e399441dSJames Smart static int
1304e399441dSJames Smart nvme_fc_init_request(void *data, struct request *rq,
1305e399441dSJames Smart 				unsigned int hctx_idx, unsigned int rq_idx,
1306e399441dSJames Smart 				unsigned int numa_node)
1307e399441dSJames Smart {
1308e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = data;
1309e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1310e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[hctx_idx+1];
1311e399441dSJames Smart 
1312e399441dSJames Smart 	return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
1313e399441dSJames Smart }
1314e399441dSJames Smart 
1315e399441dSJames Smart static int
1316e399441dSJames Smart nvme_fc_init_admin_request(void *data, struct request *rq,
1317e399441dSJames Smart 				unsigned int hctx_idx, unsigned int rq_idx,
1318e399441dSJames Smart 				unsigned int numa_node)
1319e399441dSJames Smart {
1320e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = data;
1321e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1322e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[0];
1323e399441dSJames Smart 
1324e399441dSJames Smart 	return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
1325e399441dSJames Smart }
1326e399441dSJames Smart 
1327e399441dSJames Smart static int
1328e399441dSJames Smart nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
1329e399441dSJames Smart {
1330e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op;
1331e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu;
1332e399441dSJames Smart 	struct nvme_command *sqe;
1333e399441dSJames Smart 	int i, ret;
1334e399441dSJames Smart 
1335e399441dSJames Smart 	aen_op = ctrl->aen_ops;
1336e399441dSJames Smart 	for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
1337e399441dSJames Smart 		cmdiu = &aen_op->cmd_iu;
1338e399441dSJames Smart 		sqe = &cmdiu->sqe;
1339e399441dSJames Smart 		ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
1340e399441dSJames Smart 				aen_op, (struct request *)NULL,
1341e399441dSJames Smart 				(AEN_CMDID_BASE + i));
1342e399441dSJames Smart 		if (ret)
1343e399441dSJames Smart 			return ret;
1344e399441dSJames Smart 
1345e399441dSJames Smart 		memset(sqe, 0, sizeof(*sqe));
1346e399441dSJames Smart 		sqe->common.opcode = nvme_admin_async_event;
1347e399441dSJames Smart 		sqe->common.command_id = AEN_CMDID_BASE + i;
1348e399441dSJames Smart 	}
1349e399441dSJames Smart 	return 0;
1350e399441dSJames Smart }
1351e399441dSJames Smart 
1352e399441dSJames Smart 
1353e399441dSJames Smart static inline void
1354e399441dSJames Smart __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
1355e399441dSJames Smart 		unsigned int qidx)
1356e399441dSJames Smart {
1357e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[qidx];
1358e399441dSJames Smart 
1359e399441dSJames Smart 	hctx->driver_data = queue;
1360e399441dSJames Smart 	queue->hctx = hctx;
1361e399441dSJames Smart }
1362e399441dSJames Smart 
1363e399441dSJames Smart static int
1364e399441dSJames Smart nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1365e399441dSJames Smart 		unsigned int hctx_idx)
1366e399441dSJames Smart {
1367e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = data;
1368e399441dSJames Smart 
1369e399441dSJames Smart 	__nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
1370e399441dSJames Smart 
1371e399441dSJames Smart 	return 0;
1372e399441dSJames Smart }
1373e399441dSJames Smart 
1374e399441dSJames Smart static int
1375e399441dSJames Smart nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1376e399441dSJames Smart 		unsigned int hctx_idx)
1377e399441dSJames Smart {
1378e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = data;
1379e399441dSJames Smart 
1380e399441dSJames Smart 	__nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
1381e399441dSJames Smart 
1382e399441dSJames Smart 	return 0;
1383e399441dSJames Smart }
1384e399441dSJames Smart 
1385e399441dSJames Smart static void
1386e399441dSJames Smart nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx, size_t queue_size)
1387e399441dSJames Smart {
1388e399441dSJames Smart 	struct nvme_fc_queue *queue;
1389e399441dSJames Smart 
1390e399441dSJames Smart 	queue = &ctrl->queues[idx];
1391e399441dSJames Smart 	memset(queue, 0, sizeof(*queue));
1392e399441dSJames Smart 	queue->ctrl = ctrl;
1393e399441dSJames Smart 	queue->qnum = idx;
1394e399441dSJames Smart 	atomic_set(&queue->csn, 1);
1395e399441dSJames Smart 	queue->dev = ctrl->dev;
1396e399441dSJames Smart 
1397e399441dSJames Smart 	if (idx > 0)
1398e399441dSJames Smart 		queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1399e399441dSJames Smart 	else
1400e399441dSJames Smart 		queue->cmnd_capsule_len = sizeof(struct nvme_command);
1401e399441dSJames Smart 
1402e399441dSJames Smart 	queue->queue_size = queue_size;
1403e399441dSJames Smart 
1404e399441dSJames Smart 	/*
1405e399441dSJames Smart 	 * Considered whether we should allocate buffers for all SQEs
1406e399441dSJames Smart 	 * and CQEs and dma map them - mapping their respective entries
1407e399441dSJames Smart 	 * into the request structures (kernel vm addr and dma address)
1408e399441dSJames Smart 	 * thus the driver could use the buffers/mappings directly.
1409e399441dSJames Smart 	 * It only makes sense if the LLDD would use them for its
1410e399441dSJames Smart 	 * messaging api. It's very unlikely most adapter api's would use
1411e399441dSJames Smart 	 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1412e399441dSJames Smart 	 * structures were used instead.
1413e399441dSJames Smart 	 */
1414e399441dSJames Smart }
1415e399441dSJames Smart 
1416e399441dSJames Smart /*
1417e399441dSJames Smart  * This routine terminates a queue at the transport level.
1418e399441dSJames Smart  * The transport has already ensured that all outstanding ios on
1419e399441dSJames Smart  * the queue have been terminated.
1420e399441dSJames Smart  * The transport will send a Disconnect LS request to terminate
1421e399441dSJames Smart  * the queue's connection. Termination of the admin queue will also
1422e399441dSJames Smart  * terminate the association at the target.
1423e399441dSJames Smart  */
1424e399441dSJames Smart static void
1425e399441dSJames Smart nvme_fc_free_queue(struct nvme_fc_queue *queue)
1426e399441dSJames Smart {
1427e399441dSJames Smart 	if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
1428e399441dSJames Smart 		return;
1429e399441dSJames Smart 
1430e399441dSJames Smart 	/*
1431e399441dSJames Smart 	 * Current implementation never disconnects a single queue.
1432e399441dSJames Smart 	 * It always terminates a whole association. So there is never
1433e399441dSJames Smart 	 * a disconnect(queue) LS sent to the target.
1434e399441dSJames Smart 	 */
1435e399441dSJames Smart 
1436e399441dSJames Smart 	queue->connection_id = 0;
1437e399441dSJames Smart 	clear_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1438e399441dSJames Smart }
1439e399441dSJames Smart 
1440e399441dSJames Smart static void
1441e399441dSJames Smart __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
1442e399441dSJames Smart 	struct nvme_fc_queue *queue, unsigned int qidx)
1443e399441dSJames Smart {
1444e399441dSJames Smart 	if (ctrl->lport->ops->delete_queue)
1445e399441dSJames Smart 		ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
1446e399441dSJames Smart 				queue->lldd_handle);
1447e399441dSJames Smart 	queue->lldd_handle = NULL;
1448e399441dSJames Smart }
1449e399441dSJames Smart 
1450e399441dSJames Smart static void
1451e399441dSJames Smart nvme_fc_destroy_admin_queue(struct nvme_fc_ctrl *ctrl)
1452e399441dSJames Smart {
1453e399441dSJames Smart 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
1454e399441dSJames Smart 	blk_cleanup_queue(ctrl->ctrl.admin_q);
1455e399441dSJames Smart 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
1456e399441dSJames Smart 	nvme_fc_free_queue(&ctrl->queues[0]);
1457e399441dSJames Smart }
1458e399441dSJames Smart 
1459e399441dSJames Smart static void
1460e399441dSJames Smart nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
1461e399441dSJames Smart {
1462e399441dSJames Smart 	int i;
1463e399441dSJames Smart 
1464e399441dSJames Smart 	for (i = 1; i < ctrl->queue_count; i++)
1465e399441dSJames Smart 		nvme_fc_free_queue(&ctrl->queues[i]);
1466e399441dSJames Smart }
1467e399441dSJames Smart 
1468e399441dSJames Smart static int
1469e399441dSJames Smart __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
1470e399441dSJames Smart 	struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
1471e399441dSJames Smart {
1472e399441dSJames Smart 	int ret = 0;
1473e399441dSJames Smart 
1474e399441dSJames Smart 	queue->lldd_handle = NULL;
1475e399441dSJames Smart 	if (ctrl->lport->ops->create_queue)
1476e399441dSJames Smart 		ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
1477e399441dSJames Smart 				qidx, qsize, &queue->lldd_handle);
1478e399441dSJames Smart 
1479e399441dSJames Smart 	return ret;
1480e399441dSJames Smart }
1481e399441dSJames Smart 
1482e399441dSJames Smart static void
1483e399441dSJames Smart nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
1484e399441dSJames Smart {
1485e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[ctrl->queue_count - 1];
1486e399441dSJames Smart 	int i;
1487e399441dSJames Smart 
1488e399441dSJames Smart 	for (i = ctrl->queue_count - 1; i >= 1; i--, queue--)
1489e399441dSJames Smart 		__nvme_fc_delete_hw_queue(ctrl, queue, i);
1490e399441dSJames Smart }
1491e399441dSJames Smart 
1492e399441dSJames Smart static int
1493e399441dSJames Smart nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1494e399441dSJames Smart {
1495e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[1];
149617a1ec08SJohannes Thumshirn 	int i, ret;
1497e399441dSJames Smart 
1498e399441dSJames Smart 	for (i = 1; i < ctrl->queue_count; i++, queue++) {
1499e399441dSJames Smart 		ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
150017a1ec08SJohannes Thumshirn 		if (ret)
150117a1ec08SJohannes Thumshirn 			goto delete_queues;
1502e399441dSJames Smart 	}
1503e399441dSJames Smart 
1504e399441dSJames Smart 	return 0;
150517a1ec08SJohannes Thumshirn 
150617a1ec08SJohannes Thumshirn delete_queues:
150717a1ec08SJohannes Thumshirn 	for (; i >= 0; i--)
150817a1ec08SJohannes Thumshirn 		__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
150917a1ec08SJohannes Thumshirn 	return ret;
1510e399441dSJames Smart }
1511e399441dSJames Smart 
1512e399441dSJames Smart static int
1513e399441dSJames Smart nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1514e399441dSJames Smart {
1515e399441dSJames Smart 	int i, ret = 0;
1516e399441dSJames Smart 
1517e399441dSJames Smart 	for (i = 1; i < ctrl->queue_count; i++) {
1518e399441dSJames Smart 		ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
1519e399441dSJames Smart 					(qsize / 5));
1520e399441dSJames Smart 		if (ret)
1521e399441dSJames Smart 			break;
1522e399441dSJames Smart 		ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
1523e399441dSJames Smart 		if (ret)
1524e399441dSJames Smart 			break;
1525e399441dSJames Smart 	}
1526e399441dSJames Smart 
1527e399441dSJames Smart 	return ret;
1528e399441dSJames Smart }
1529e399441dSJames Smart 
1530e399441dSJames Smart static void
1531e399441dSJames Smart nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
1532e399441dSJames Smart {
1533e399441dSJames Smart 	int i;
1534e399441dSJames Smart 
1535e399441dSJames Smart 	for (i = 1; i < ctrl->queue_count; i++)
1536e399441dSJames Smart 		nvme_fc_init_queue(ctrl, i, ctrl->ctrl.sqsize);
1537e399441dSJames Smart }
1538e399441dSJames Smart 
1539e399441dSJames Smart static void
1540e399441dSJames Smart nvme_fc_ctrl_free(struct kref *ref)
1541e399441dSJames Smart {
1542e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl =
1543e399441dSJames Smart 		container_of(ref, struct nvme_fc_ctrl, ref);
1544e399441dSJames Smart 	unsigned long flags;
1545e399441dSJames Smart 
1546e399441dSJames Smart 	if (ctrl->state != FCCTRL_INIT) {
1547e399441dSJames Smart 		/* remove from rport list */
1548e399441dSJames Smart 		spin_lock_irqsave(&ctrl->rport->lock, flags);
1549e399441dSJames Smart 		list_del(&ctrl->ctrl_list);
1550e399441dSJames Smart 		spin_unlock_irqrestore(&ctrl->rport->lock, flags);
1551e399441dSJames Smart 	}
1552e399441dSJames Smart 
1553e399441dSJames Smart 	put_device(ctrl->dev);
1554e399441dSJames Smart 	nvme_fc_rport_put(ctrl->rport);
1555e399441dSJames Smart 
1556e399441dSJames Smart 	kfree(ctrl->queues);
1557e399441dSJames Smart 	ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
1558e399441dSJames Smart 	nvmf_free_options(ctrl->ctrl.opts);
1559e399441dSJames Smart 	kfree(ctrl);
1560e399441dSJames Smart }
1561e399441dSJames Smart 
1562e399441dSJames Smart static void
1563e399441dSJames Smart nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
1564e399441dSJames Smart {
1565e399441dSJames Smart 	kref_put(&ctrl->ref, nvme_fc_ctrl_free);
1566e399441dSJames Smart }
1567e399441dSJames Smart 
1568e399441dSJames Smart static int
1569e399441dSJames Smart nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
1570e399441dSJames Smart {
1571e399441dSJames Smart 	return kref_get_unless_zero(&ctrl->ref);
1572e399441dSJames Smart }
1573e399441dSJames Smart 
1574e399441dSJames Smart /*
1575e399441dSJames Smart  * All accesses from nvme core layer done - can now free the
1576e399441dSJames Smart  * controller. Called after last nvme_put_ctrl() call
1577e399441dSJames Smart  */
1578e399441dSJames Smart static void
1579e399441dSJames Smart nvme_fc_free_nvme_ctrl(struct nvme_ctrl *nctrl)
1580e399441dSJames Smart {
1581e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
1582e399441dSJames Smart 
1583e399441dSJames Smart 	WARN_ON(nctrl != &ctrl->ctrl);
1584e399441dSJames Smart 
1585e399441dSJames Smart 	/*
1586e399441dSJames Smart 	 * Tear down the association, which will generate link
1587e399441dSJames Smart 	 * traffic to terminate connections
1588e399441dSJames Smart 	 */
1589e399441dSJames Smart 
1590e399441dSJames Smart 	if (ctrl->state != FCCTRL_INIT) {
1591e399441dSJames Smart 		/* send a Disconnect(association) LS to fc-nvme target */
1592e399441dSJames Smart 		nvme_fc_xmt_disconnect_assoc(ctrl);
1593e399441dSJames Smart 
1594e399441dSJames Smart 		if (ctrl->ctrl.tagset) {
1595e399441dSJames Smart 			blk_cleanup_queue(ctrl->ctrl.connect_q);
1596e399441dSJames Smart 			blk_mq_free_tag_set(&ctrl->tag_set);
1597e399441dSJames Smart 			nvme_fc_delete_hw_io_queues(ctrl);
1598e399441dSJames Smart 			nvme_fc_free_io_queues(ctrl);
1599e399441dSJames Smart 		}
1600e399441dSJames Smart 
1601e399441dSJames Smart 		nvme_fc_exit_aen_ops(ctrl);
1602e399441dSJames Smart 
1603e399441dSJames Smart 		nvme_fc_destroy_admin_queue(ctrl);
1604e399441dSJames Smart 	}
1605e399441dSJames Smart 
1606e399441dSJames Smart 	nvme_fc_ctrl_put(ctrl);
1607e399441dSJames Smart }
1608e399441dSJames Smart 
1609e399441dSJames Smart 
1610e399441dSJames Smart static int
1611e399441dSJames Smart __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1612e399441dSJames Smart {
1613e399441dSJames Smart 	int state;
1614e399441dSJames Smart 
1615e399441dSJames Smart 	state = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1616e399441dSJames Smart 	if (state != FCPOP_STATE_ACTIVE) {
1617e399441dSJames Smart 		atomic_set(&op->state, state);
1618e399441dSJames Smart 		return -ECANCELED; /* fail */
1619e399441dSJames Smart 	}
1620e399441dSJames Smart 
1621e399441dSJames Smart 	ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1622e399441dSJames Smart 					&ctrl->rport->remoteport,
1623e399441dSJames Smart 					op->queue->lldd_handle,
1624e399441dSJames Smart 					&op->fcp_req);
1625e399441dSJames Smart 
1626e399441dSJames Smart 	return 0;
1627e399441dSJames Smart }
1628e399441dSJames Smart 
1629e399441dSJames Smart enum blk_eh_timer_return
1630e399441dSJames Smart nvme_fc_timeout(struct request *rq, bool reserved)
1631e399441dSJames Smart {
1632e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1633e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
1634e399441dSJames Smart 	int ret;
1635e399441dSJames Smart 
1636e399441dSJames Smart 	if (reserved)
1637e399441dSJames Smart 		return BLK_EH_RESET_TIMER;
1638e399441dSJames Smart 
1639e399441dSJames Smart 	ret = __nvme_fc_abort_op(ctrl, op);
1640e399441dSJames Smart 	if (ret)
1641e399441dSJames Smart 		/* io wasn't active to abort consider it done */
1642e399441dSJames Smart 		return BLK_EH_HANDLED;
1643e399441dSJames Smart 
1644e399441dSJames Smart 	/*
1645e399441dSJames Smart 	 * TODO: force a controller reset
1646e399441dSJames Smart 	 *   when that happens, queues will be torn down and outstanding
1647e399441dSJames Smart 	 *   ios will be terminated, and the above abort, on a single io
1648e399441dSJames Smart 	 *   will no longer be needed.
1649e399441dSJames Smart 	 */
1650e399441dSJames Smart 
1651e399441dSJames Smart 	return BLK_EH_HANDLED;
1652e399441dSJames Smart }
1653e399441dSJames Smart 
1654e399441dSJames Smart static int
1655e399441dSJames Smart nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1656e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
1657e399441dSJames Smart {
1658e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
1659e399441dSJames Smart 	enum dma_data_direction dir;
1660e399441dSJames Smart 	int ret;
1661e399441dSJames Smart 
1662e399441dSJames Smart 	freq->sg_cnt = 0;
1663e399441dSJames Smart 
1664b131c61dSChristoph Hellwig 	if (!blk_rq_payload_bytes(rq))
1665e399441dSJames Smart 		return 0;
1666e399441dSJames Smart 
1667e399441dSJames Smart 	freq->sg_table.sgl = freq->first_sgl;
166819e420bbSChristoph Hellwig 	ret = sg_alloc_table_chained(&freq->sg_table,
166919e420bbSChristoph Hellwig 			blk_rq_nr_phys_segments(rq), freq->sg_table.sgl);
1670e399441dSJames Smart 	if (ret)
1671e399441dSJames Smart 		return -ENOMEM;
1672e399441dSJames Smart 
1673e399441dSJames Smart 	op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
167419e420bbSChristoph Hellwig 	WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
1675e399441dSJames Smart 	dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1676e399441dSJames Smart 	freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
1677e399441dSJames Smart 				op->nents, dir);
1678e399441dSJames Smart 	if (unlikely(freq->sg_cnt <= 0)) {
1679e399441dSJames Smart 		sg_free_table_chained(&freq->sg_table, true);
1680e399441dSJames Smart 		freq->sg_cnt = 0;
1681e399441dSJames Smart 		return -EFAULT;
1682e399441dSJames Smart 	}
1683e399441dSJames Smart 
1684e399441dSJames Smart 	/*
1685e399441dSJames Smart 	 * TODO: blk_integrity_rq(rq)  for DIF
1686e399441dSJames Smart 	 */
1687e399441dSJames Smart 	return 0;
1688e399441dSJames Smart }
1689e399441dSJames Smart 
1690e399441dSJames Smart static void
1691e399441dSJames Smart nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1692e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
1693e399441dSJames Smart {
1694e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
1695e399441dSJames Smart 
1696e399441dSJames Smart 	if (!freq->sg_cnt)
1697e399441dSJames Smart 		return;
1698e399441dSJames Smart 
1699e399441dSJames Smart 	fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
1700e399441dSJames Smart 				((rq_data_dir(rq) == WRITE) ?
1701e399441dSJames Smart 					DMA_TO_DEVICE : DMA_FROM_DEVICE));
1702e399441dSJames Smart 
1703e399441dSJames Smart 	nvme_cleanup_cmd(rq);
1704e399441dSJames Smart 
1705e399441dSJames Smart 	sg_free_table_chained(&freq->sg_table, true);
1706e399441dSJames Smart 
1707e399441dSJames Smart 	freq->sg_cnt = 0;
1708e399441dSJames Smart }
1709e399441dSJames Smart 
1710e399441dSJames Smart /*
1711e399441dSJames Smart  * In FC, the queue is a logical thing. At transport connect, the target
1712e399441dSJames Smart  * creates its "queue" and returns a handle that is to be given to the
1713e399441dSJames Smart  * target whenever it posts something to the corresponding SQ.  When an
1714e399441dSJames Smart  * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
1715e399441dSJames Smart  * command contained within the SQE, an io, and assigns a FC exchange
1716e399441dSJames Smart  * to it. The SQE and the associated SQ handle are sent in the initial
1717e399441dSJames Smart  * CMD IU sents on the exchange. All transfers relative to the io occur
1718e399441dSJames Smart  * as part of the exchange.  The CQE is the last thing for the io,
1719e399441dSJames Smart  * which is transferred (explicitly or implicitly) with the RSP IU
1720e399441dSJames Smart  * sent on the exchange. After the CQE is received, the FC exchange is
1721e399441dSJames Smart  * terminaed and the Exchange may be used on a different io.
1722e399441dSJames Smart  *
1723e399441dSJames Smart  * The transport to LLDD api has the transport making a request for a
1724e399441dSJames Smart  * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
1725e399441dSJames Smart  * resource and transfers the command. The LLDD will then process all
1726e399441dSJames Smart  * steps to complete the io. Upon completion, the transport done routine
1727e399441dSJames Smart  * is called.
1728e399441dSJames Smart  *
1729e399441dSJames Smart  * So - while the operation is outstanding to the LLDD, there is a link
1730e399441dSJames Smart  * level FC exchange resource that is also outstanding. This must be
1731e399441dSJames Smart  * considered in all cleanup operations.
1732e399441dSJames Smart  */
1733e399441dSJames Smart static int
1734e399441dSJames Smart nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1735e399441dSJames Smart 	struct nvme_fc_fcp_op *op, u32 data_len,
1736e399441dSJames Smart 	enum nvmefc_fcp_datadir	io_dir)
1737e399441dSJames Smart {
1738e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1739e399441dSJames Smart 	struct nvme_command *sqe = &cmdiu->sqe;
1740e399441dSJames Smart 	u32 csn;
1741e399441dSJames Smart 	int ret;
1742e399441dSJames Smart 
1743e399441dSJames Smart 	if (!nvme_fc_ctrl_get(ctrl))
1744e399441dSJames Smart 		return BLK_MQ_RQ_QUEUE_ERROR;
1745e399441dSJames Smart 
1746e399441dSJames Smart 	/* format the FC-NVME CMD IU and fcp_req */
1747e399441dSJames Smart 	cmdiu->connection_id = cpu_to_be64(queue->connection_id);
1748e399441dSJames Smart 	csn = atomic_inc_return(&queue->csn);
1749e399441dSJames Smart 	cmdiu->csn = cpu_to_be32(csn);
1750e399441dSJames Smart 	cmdiu->data_len = cpu_to_be32(data_len);
1751e399441dSJames Smart 	switch (io_dir) {
1752e399441dSJames Smart 	case NVMEFC_FCP_WRITE:
1753e399441dSJames Smart 		cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
1754e399441dSJames Smart 		break;
1755e399441dSJames Smart 	case NVMEFC_FCP_READ:
1756e399441dSJames Smart 		cmdiu->flags = FCNVME_CMD_FLAGS_READ;
1757e399441dSJames Smart 		break;
1758e399441dSJames Smart 	case NVMEFC_FCP_NODATA:
1759e399441dSJames Smart 		cmdiu->flags = 0;
1760e399441dSJames Smart 		break;
1761e399441dSJames Smart 	}
1762e399441dSJames Smart 	op->fcp_req.payload_length = data_len;
1763e399441dSJames Smart 	op->fcp_req.io_dir = io_dir;
1764e399441dSJames Smart 	op->fcp_req.transferred_length = 0;
1765e399441dSJames Smart 	op->fcp_req.rcv_rsplen = 0;
176662eeacb0SJames Smart 	op->fcp_req.status = NVME_SC_SUCCESS;
1767e399441dSJames Smart 	op->fcp_req.sqid = cpu_to_le16(queue->qnum);
1768e399441dSJames Smart 
1769e399441dSJames Smart 	/*
1770e399441dSJames Smart 	 * validate per fabric rules, set fields mandated by fabric spec
1771e399441dSJames Smart 	 * as well as those by FC-NVME spec.
1772e399441dSJames Smart 	 */
1773e399441dSJames Smart 	WARN_ON_ONCE(sqe->common.metadata);
1774e399441dSJames Smart 	WARN_ON_ONCE(sqe->common.dptr.prp1);
1775e399441dSJames Smart 	WARN_ON_ONCE(sqe->common.dptr.prp2);
1776e399441dSJames Smart 	sqe->common.flags |= NVME_CMD_SGL_METABUF;
1777e399441dSJames Smart 
1778e399441dSJames Smart 	/*
1779e399441dSJames Smart 	 * format SQE DPTR field per FC-NVME rules
1780e399441dSJames Smart 	 *    type=data block descr; subtype=offset;
1781e399441dSJames Smart 	 *    offset is currently 0.
1782e399441dSJames Smart 	 */
1783e399441dSJames Smart 	sqe->rw.dptr.sgl.type = NVME_SGL_FMT_OFFSET;
1784e399441dSJames Smart 	sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
1785e399441dSJames Smart 	sqe->rw.dptr.sgl.addr = 0;
1786e399441dSJames Smart 
1787e399441dSJames Smart 	/* odd that we set the command_id - should come from nvme-fabrics */
1788e399441dSJames Smart 	WARN_ON_ONCE(sqe->common.command_id != cpu_to_le16(op->rqno));
1789e399441dSJames Smart 
1790e399441dSJames Smart 	if (op->rq) {				/* skipped on aens */
1791e399441dSJames Smart 		ret = nvme_fc_map_data(ctrl, op->rq, op);
1792e399441dSJames Smart 		if (ret < 0) {
1793e399441dSJames Smart 			dev_err(queue->ctrl->ctrl.device,
1794e399441dSJames Smart 			     "Failed to map data (%d)\n", ret);
1795e399441dSJames Smart 			nvme_cleanup_cmd(op->rq);
1796e399441dSJames Smart 			nvme_fc_ctrl_put(ctrl);
1797e399441dSJames Smart 			return (ret == -ENOMEM || ret == -EAGAIN) ?
1798e399441dSJames Smart 				BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
1799e399441dSJames Smart 		}
1800e399441dSJames Smart 	}
1801e399441dSJames Smart 
1802e399441dSJames Smart 	fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
1803e399441dSJames Smart 				  sizeof(op->cmd_iu), DMA_TO_DEVICE);
1804e399441dSJames Smart 
1805e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_ACTIVE);
1806e399441dSJames Smart 
1807e399441dSJames Smart 	if (op->rq)
1808e399441dSJames Smart 		blk_mq_start_request(op->rq);
1809e399441dSJames Smart 
1810e399441dSJames Smart 	ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
1811e399441dSJames Smart 					&ctrl->rport->remoteport,
1812e399441dSJames Smart 					queue->lldd_handle, &op->fcp_req);
1813e399441dSJames Smart 
1814e399441dSJames Smart 	if (ret) {
1815e399441dSJames Smart 		dev_err(ctrl->dev,
1816e399441dSJames Smart 			"Send nvme command failed - lldd returned %d.\n", ret);
1817e399441dSJames Smart 
1818e399441dSJames Smart 		if (op->rq) {			/* normal request */
1819e399441dSJames Smart 			nvme_fc_unmap_data(ctrl, op->rq, op);
1820e399441dSJames Smart 			nvme_cleanup_cmd(op->rq);
1821e399441dSJames Smart 		}
1822e399441dSJames Smart 		/* else - aen. no cleanup needed */
1823e399441dSJames Smart 
1824e399441dSJames Smart 		nvme_fc_ctrl_put(ctrl);
1825e399441dSJames Smart 
1826e399441dSJames Smart 		if (ret != -EBUSY)
1827e399441dSJames Smart 			return BLK_MQ_RQ_QUEUE_ERROR;
1828e399441dSJames Smart 
1829e399441dSJames Smart 		if (op->rq) {
1830e399441dSJames Smart 			blk_mq_stop_hw_queues(op->rq->q);
1831e399441dSJames Smart 			blk_mq_delay_queue(queue->hctx, NVMEFC_QUEUE_DELAY);
1832e399441dSJames Smart 		}
1833e399441dSJames Smart 		return BLK_MQ_RQ_QUEUE_BUSY;
1834e399441dSJames Smart 	}
1835e399441dSJames Smart 
1836e399441dSJames Smart 	return BLK_MQ_RQ_QUEUE_OK;
1837e399441dSJames Smart }
1838e399441dSJames Smart 
1839e399441dSJames Smart static int
1840e399441dSJames Smart nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
1841e399441dSJames Smart 			const struct blk_mq_queue_data *bd)
1842e399441dSJames Smart {
1843e399441dSJames Smart 	struct nvme_ns *ns = hctx->queue->queuedata;
1844e399441dSJames Smart 	struct nvme_fc_queue *queue = hctx->driver_data;
1845e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = queue->ctrl;
1846e399441dSJames Smart 	struct request *rq = bd->rq;
1847e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1848e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1849e399441dSJames Smart 	struct nvme_command *sqe = &cmdiu->sqe;
1850e399441dSJames Smart 	enum nvmefc_fcp_datadir	io_dir;
1851e399441dSJames Smart 	u32 data_len;
1852e399441dSJames Smart 	int ret;
1853e399441dSJames Smart 
1854e399441dSJames Smart 	ret = nvme_setup_cmd(ns, rq, sqe);
1855e399441dSJames Smart 	if (ret)
1856e399441dSJames Smart 		return ret;
1857e399441dSJames Smart 
1858b131c61dSChristoph Hellwig 	data_len = blk_rq_payload_bytes(rq);
1859e399441dSJames Smart 	if (data_len)
1860e399441dSJames Smart 		io_dir = ((rq_data_dir(rq) == WRITE) ?
1861e399441dSJames Smart 					NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
1862e399441dSJames Smart 	else
1863e399441dSJames Smart 		io_dir = NVMEFC_FCP_NODATA;
1864e399441dSJames Smart 
1865e399441dSJames Smart 	return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
1866e399441dSJames Smart }
1867e399441dSJames Smart 
1868e399441dSJames Smart static struct blk_mq_tags *
1869e399441dSJames Smart nvme_fc_tagset(struct nvme_fc_queue *queue)
1870e399441dSJames Smart {
1871e399441dSJames Smart 	if (queue->qnum == 0)
1872e399441dSJames Smart 		return queue->ctrl->admin_tag_set.tags[queue->qnum];
1873e399441dSJames Smart 
1874e399441dSJames Smart 	return queue->ctrl->tag_set.tags[queue->qnum - 1];
1875e399441dSJames Smart }
1876e399441dSJames Smart 
1877e399441dSJames Smart static int
1878e399441dSJames Smart nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1879e399441dSJames Smart 
1880e399441dSJames Smart {
1881e399441dSJames Smart 	struct nvme_fc_queue *queue = hctx->driver_data;
1882e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = queue->ctrl;
1883e399441dSJames Smart 	struct request *req;
1884e399441dSJames Smart 	struct nvme_fc_fcp_op *op;
1885e399441dSJames Smart 
1886e399441dSJames Smart 	req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag);
1887e399441dSJames Smart 	if (!req) {
1888e399441dSJames Smart 		dev_err(queue->ctrl->ctrl.device,
1889e399441dSJames Smart 			 "tag 0x%x on QNum %#x not found\n",
1890e399441dSJames Smart 			tag, queue->qnum);
1891e399441dSJames Smart 		return 0;
1892e399441dSJames Smart 	}
1893e399441dSJames Smart 
1894e399441dSJames Smart 	op = blk_mq_rq_to_pdu(req);
1895e399441dSJames Smart 
1896e399441dSJames Smart 	if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) &&
1897e399441dSJames Smart 		 (ctrl->lport->ops->poll_queue))
1898e399441dSJames Smart 		ctrl->lport->ops->poll_queue(&ctrl->lport->localport,
1899e399441dSJames Smart 						 queue->lldd_handle);
1900e399441dSJames Smart 
1901e399441dSJames Smart 	return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE));
1902e399441dSJames Smart }
1903e399441dSJames Smart 
1904e399441dSJames Smart static void
1905e399441dSJames Smart nvme_fc_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1906e399441dSJames Smart {
1907e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
1908e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op;
1909e399441dSJames Smart 	int ret;
1910e399441dSJames Smart 
1911e399441dSJames Smart 	if (aer_idx > NVME_FC_NR_AEN_COMMANDS)
1912e399441dSJames Smart 		return;
1913e399441dSJames Smart 
1914e399441dSJames Smart 	aen_op = &ctrl->aen_ops[aer_idx];
1915e399441dSJames Smart 
1916e399441dSJames Smart 	ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
1917e399441dSJames Smart 					NVMEFC_FCP_NODATA);
1918e399441dSJames Smart 	if (ret)
1919e399441dSJames Smart 		dev_err(ctrl->ctrl.device,
1920e399441dSJames Smart 			"failed async event work [%d]\n", aer_idx);
1921e399441dSJames Smart }
1922e399441dSJames Smart 
1923e399441dSJames Smart static void
1924e399441dSJames Smart nvme_fc_complete_rq(struct request *rq)
1925e399441dSJames Smart {
1926e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1927e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
192877f02a7aSChristoph Hellwig 	int state;
1929e399441dSJames Smart 
1930e399441dSJames Smart 	state = atomic_xchg(&op->state, FCPOP_STATE_IDLE);
1931e399441dSJames Smart 
1932e399441dSJames Smart 	nvme_cleanup_cmd(rq);
1933e399441dSJames Smart 	nvme_fc_unmap_data(ctrl, rq, op);
193477f02a7aSChristoph Hellwig 	nvme_complete_rq(rq);
1935e399441dSJames Smart 	nvme_fc_ctrl_put(ctrl);
1936e399441dSJames Smart 
1937e399441dSJames Smart }
1938e399441dSJames Smart 
1939f363b089SEric Biggers static const struct blk_mq_ops nvme_fc_mq_ops = {
1940e399441dSJames Smart 	.queue_rq	= nvme_fc_queue_rq,
1941e399441dSJames Smart 	.complete	= nvme_fc_complete_rq,
1942e399441dSJames Smart 	.init_request	= nvme_fc_init_request,
1943e399441dSJames Smart 	.exit_request	= nvme_fc_exit_request,
1944e399441dSJames Smart 	.reinit_request	= nvme_fc_reinit_request,
1945e399441dSJames Smart 	.init_hctx	= nvme_fc_init_hctx,
1946e399441dSJames Smart 	.poll		= nvme_fc_poll,
1947e399441dSJames Smart 	.timeout	= nvme_fc_timeout,
1948e399441dSJames Smart };
1949e399441dSJames Smart 
1950f363b089SEric Biggers static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
1951e399441dSJames Smart 	.queue_rq	= nvme_fc_queue_rq,
1952e399441dSJames Smart 	.complete	= nvme_fc_complete_rq,
1953e399441dSJames Smart 	.init_request	= nvme_fc_init_admin_request,
1954e399441dSJames Smart 	.exit_request	= nvme_fc_exit_request,
1955e399441dSJames Smart 	.reinit_request	= nvme_fc_reinit_request,
1956e399441dSJames Smart 	.init_hctx	= nvme_fc_init_admin_hctx,
1957e399441dSJames Smart 	.timeout	= nvme_fc_timeout,
1958e399441dSJames Smart };
1959e399441dSJames Smart 
1960e399441dSJames Smart static int
1961e399441dSJames Smart nvme_fc_configure_admin_queue(struct nvme_fc_ctrl *ctrl)
1962e399441dSJames Smart {
1963e399441dSJames Smart 	u32 segs;
1964e399441dSJames Smart 	int error;
1965e399441dSJames Smart 
1966e399441dSJames Smart 	nvme_fc_init_queue(ctrl, 0, NVME_FC_AQ_BLKMQ_DEPTH);
1967e399441dSJames Smart 
1968e399441dSJames Smart 	error = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
1969e399441dSJames Smart 				NVME_FC_AQ_BLKMQ_DEPTH,
1970e399441dSJames Smart 				(NVME_FC_AQ_BLKMQ_DEPTH / 4));
1971e399441dSJames Smart 	if (error)
1972e399441dSJames Smart 		return error;
1973e399441dSJames Smart 
1974e399441dSJames Smart 	memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1975e399441dSJames Smart 	ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
1976e399441dSJames Smart 	ctrl->admin_tag_set.queue_depth = NVME_FC_AQ_BLKMQ_DEPTH;
1977e399441dSJames Smart 	ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
1978e399441dSJames Smart 	ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1979e399441dSJames Smart 	ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
1980e399441dSJames Smart 					(SG_CHUNK_SIZE *
1981e399441dSJames Smart 						sizeof(struct scatterlist)) +
1982e399441dSJames Smart 					ctrl->lport->ops->fcprqst_priv_sz;
1983e399441dSJames Smart 	ctrl->admin_tag_set.driver_data = ctrl;
1984e399441dSJames Smart 	ctrl->admin_tag_set.nr_hw_queues = 1;
1985e399441dSJames Smart 	ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1986e399441dSJames Smart 
1987e399441dSJames Smart 	error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1988e399441dSJames Smart 	if (error)
1989e399441dSJames Smart 		goto out_free_queue;
1990e399441dSJames Smart 
1991e399441dSJames Smart 	ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1992e399441dSJames Smart 	if (IS_ERR(ctrl->ctrl.admin_q)) {
1993e399441dSJames Smart 		error = PTR_ERR(ctrl->ctrl.admin_q);
1994e399441dSJames Smart 		goto out_free_tagset;
1995e399441dSJames Smart 	}
1996e399441dSJames Smart 
1997e399441dSJames Smart 	error = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
1998e399441dSJames Smart 				NVME_FC_AQ_BLKMQ_DEPTH);
1999e399441dSJames Smart 	if (error)
2000e399441dSJames Smart 		goto out_cleanup_queue;
2001e399441dSJames Smart 
2002e399441dSJames Smart 	error = nvmf_connect_admin_queue(&ctrl->ctrl);
2003e399441dSJames Smart 	if (error)
2004e399441dSJames Smart 		goto out_delete_hw_queue;
2005e399441dSJames Smart 
2006e399441dSJames Smart 	error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
2007e399441dSJames Smart 	if (error) {
2008e399441dSJames Smart 		dev_err(ctrl->ctrl.device,
2009e399441dSJames Smart 			"prop_get NVME_REG_CAP failed\n");
2010e399441dSJames Smart 		goto out_delete_hw_queue;
2011e399441dSJames Smart 	}
2012e399441dSJames Smart 
2013e399441dSJames Smart 	ctrl->ctrl.sqsize =
2014e399441dSJames Smart 		min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
2015e399441dSJames Smart 
2016e399441dSJames Smart 	error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
2017e399441dSJames Smart 	if (error)
2018e399441dSJames Smart 		goto out_delete_hw_queue;
2019e399441dSJames Smart 
2020e399441dSJames Smart 	segs = min_t(u32, NVME_FC_MAX_SEGMENTS,
2021e399441dSJames Smart 			ctrl->lport->ops->max_sgl_segments);
2022e399441dSJames Smart 	ctrl->ctrl.max_hw_sectors = (segs - 1) << (PAGE_SHIFT - 9);
2023e399441dSJames Smart 
2024e399441dSJames Smart 	error = nvme_init_identify(&ctrl->ctrl);
2025e399441dSJames Smart 	if (error)
2026e399441dSJames Smart 		goto out_delete_hw_queue;
2027e399441dSJames Smart 
2028e399441dSJames Smart 	nvme_start_keep_alive(&ctrl->ctrl);
2029e399441dSJames Smart 
2030e399441dSJames Smart 	return 0;
2031e399441dSJames Smart 
2032e399441dSJames Smart out_delete_hw_queue:
2033e399441dSJames Smart 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
2034e399441dSJames Smart out_cleanup_queue:
2035e399441dSJames Smart 	blk_cleanup_queue(ctrl->ctrl.admin_q);
2036e399441dSJames Smart out_free_tagset:
2037e399441dSJames Smart 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
2038e399441dSJames Smart out_free_queue:
2039e399441dSJames Smart 	nvme_fc_free_queue(&ctrl->queues[0]);
2040e399441dSJames Smart 	return error;
2041e399441dSJames Smart }
2042e399441dSJames Smart 
2043e399441dSJames Smart /*
2044e399441dSJames Smart  * This routine is used by the transport when it needs to find active
2045e399441dSJames Smart  * io on a queue that is to be terminated. The transport uses
2046e399441dSJames Smart  * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2047e399441dSJames Smart  * this routine to kill them on a 1 by 1 basis.
2048e399441dSJames Smart  *
2049e399441dSJames Smart  * As FC allocates FC exchange for each io, the transport must contact
2050e399441dSJames Smart  * the LLDD to terminate the exchange, thus releasing the FC exchange.
2051e399441dSJames Smart  * After terminating the exchange the LLDD will call the transport's
2052e399441dSJames Smart  * normal io done path for the request, but it will have an aborted
2053e399441dSJames Smart  * status. The done path will return the io request back to the block
2054e399441dSJames Smart  * layer with an error status.
2055e399441dSJames Smart  */
2056e399441dSJames Smart static void
2057e399441dSJames Smart nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2058e399441dSJames Smart {
2059e399441dSJames Smart 	struct nvme_ctrl *nctrl = data;
2060e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2061e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2062e399441dSJames Smart int status;
2063e399441dSJames Smart 
2064e399441dSJames Smart 	if (!blk_mq_request_started(req))
2065e399441dSJames Smart 		return;
2066e399441dSJames Smart 
2067e399441dSJames Smart 	/* this performs an ABTS-LS on the FC exchange for the io */
2068e399441dSJames Smart 	status = __nvme_fc_abort_op(ctrl, op);
2069e399441dSJames Smart 	/*
2070e399441dSJames Smart 	 * if __nvme_fc_abort_op failed: io wasn't active to abort
2071e399441dSJames Smart 	 * consider it done. Assume completion path already completing
2072e399441dSJames Smart 	 * in parallel
2073e399441dSJames Smart 	 */
2074e399441dSJames Smart 	if (status)
2075e399441dSJames Smart 		/* io wasn't active to abort consider it done */
2076e399441dSJames Smart 		/* assume completion path already completing in parallel */
2077e399441dSJames Smart 		return;
2078e399441dSJames Smart }
2079e399441dSJames Smart 
2080e399441dSJames Smart 
2081e399441dSJames Smart /*
2082e399441dSJames Smart  * This routine stops operation of the controller. Admin and IO queues
2083e399441dSJames Smart  * are stopped, outstanding ios on them terminated, and the nvme ctrl
2084e399441dSJames Smart  * is shutdown.
2085e399441dSJames Smart  */
2086e399441dSJames Smart static void
2087e399441dSJames Smart nvme_fc_shutdown_ctrl(struct nvme_fc_ctrl *ctrl)
2088e399441dSJames Smart {
2089e399441dSJames Smart 	/*
2090e399441dSJames Smart 	 * If io queues are present, stop them and terminate all outstanding
2091e399441dSJames Smart 	 * ios on them. As FC allocates FC exchange for each io, the
2092e399441dSJames Smart 	 * transport must contact the LLDD to terminate the exchange,
2093e399441dSJames Smart 	 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2094e399441dSJames Smart 	 * to tell us what io's are busy and invoke a transport routine
2095e399441dSJames Smart 	 * to kill them with the LLDD.  After terminating the exchange
2096e399441dSJames Smart 	 * the LLDD will call the transport's normal io done path, but it
2097e399441dSJames Smart 	 * will have an aborted status. The done path will return the
2098e399441dSJames Smart 	 * io requests back to the block layer as part of normal completions
2099e399441dSJames Smart 	 * (but with error status).
2100e399441dSJames Smart 	 */
2101e399441dSJames Smart 	if (ctrl->queue_count > 1) {
2102e399441dSJames Smart 		nvme_stop_queues(&ctrl->ctrl);
2103e399441dSJames Smart 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
2104e399441dSJames Smart 				nvme_fc_terminate_exchange, &ctrl->ctrl);
2105e399441dSJames Smart 	}
2106e399441dSJames Smart 
2107e399441dSJames Smart 	if (ctrl->ctrl.state == NVME_CTRL_LIVE)
2108e399441dSJames Smart 		nvme_shutdown_ctrl(&ctrl->ctrl);
2109e399441dSJames Smart 
2110e399441dSJames Smart 	/*
2111e399441dSJames Smart 	 * now clean up the admin queue. Same thing as above.
2112e399441dSJames Smart 	 * use blk_mq_tagset_busy_itr() and the transport routine to
2113e399441dSJames Smart 	 * terminate the exchanges.
2114e399441dSJames Smart 	 */
2115e399441dSJames Smart 	blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
2116e399441dSJames Smart 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2117e399441dSJames Smart 				nvme_fc_terminate_exchange, &ctrl->ctrl);
2118e399441dSJames Smart }
2119e399441dSJames Smart 
2120e399441dSJames Smart /*
2121e399441dSJames Smart  * Called to teardown an association.
2122e399441dSJames Smart  * May be called with association fully in place or partially in place.
2123e399441dSJames Smart  */
2124e399441dSJames Smart static void
2125e399441dSJames Smart __nvme_fc_remove_ctrl(struct nvme_fc_ctrl *ctrl)
2126e399441dSJames Smart {
2127e399441dSJames Smart 	nvme_stop_keep_alive(&ctrl->ctrl);
2128e399441dSJames Smart 
2129e399441dSJames Smart 	/* stop and terminate ios on admin and io queues */
2130e399441dSJames Smart 	nvme_fc_shutdown_ctrl(ctrl);
2131e399441dSJames Smart 
2132e399441dSJames Smart 	/*
2133e399441dSJames Smart 	 * tear down the controller
2134e399441dSJames Smart 	 * This will result in the last reference on the nvme ctrl to
2135e399441dSJames Smart 	 * expire, calling the transport nvme_fc_free_nvme_ctrl() callback.
2136e399441dSJames Smart 	 * From there, the transport will tear down it's logical queues and
2137e399441dSJames Smart 	 * association.
2138e399441dSJames Smart 	 */
2139e399441dSJames Smart 	nvme_uninit_ctrl(&ctrl->ctrl);
2140e399441dSJames Smart 
2141e399441dSJames Smart 	nvme_put_ctrl(&ctrl->ctrl);
2142e399441dSJames Smart }
2143e399441dSJames Smart 
2144e399441dSJames Smart static void
2145e399441dSJames Smart nvme_fc_del_ctrl_work(struct work_struct *work)
2146e399441dSJames Smart {
2147e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl =
2148e399441dSJames Smart 			container_of(work, struct nvme_fc_ctrl, delete_work);
2149e399441dSJames Smart 
2150e399441dSJames Smart 	__nvme_fc_remove_ctrl(ctrl);
2151e399441dSJames Smart }
2152e399441dSJames Smart 
2153e399441dSJames Smart static int
2154e399441dSJames Smart __nvme_fc_del_ctrl(struct nvme_fc_ctrl *ctrl)
2155e399441dSJames Smart {
2156e399441dSJames Smart 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
2157e399441dSJames Smart 		return -EBUSY;
2158e399441dSJames Smart 
2159e399441dSJames Smart 	if (!queue_work(nvme_fc_wq, &ctrl->delete_work))
2160e399441dSJames Smart 		return -EBUSY;
2161e399441dSJames Smart 
2162e399441dSJames Smart 	return 0;
2163e399441dSJames Smart }
2164e399441dSJames Smart 
2165e399441dSJames Smart /*
2166e399441dSJames Smart  * Request from nvme core layer to delete the controller
2167e399441dSJames Smart  */
2168e399441dSJames Smart static int
2169e399441dSJames Smart nvme_fc_del_nvme_ctrl(struct nvme_ctrl *nctrl)
2170e399441dSJames Smart {
2171e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2172e399441dSJames Smart 	struct nvme_fc_rport *rport = ctrl->rport;
2173e399441dSJames Smart 	unsigned long flags;
2174e399441dSJames Smart 	int ret;
2175e399441dSJames Smart 
2176e399441dSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
2177e399441dSJames Smart 	ret = __nvme_fc_del_ctrl(ctrl);
2178e399441dSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
2179e399441dSJames Smart 	if (ret)
2180e399441dSJames Smart 		return ret;
2181e399441dSJames Smart 
2182e399441dSJames Smart 	flush_work(&ctrl->delete_work);
2183e399441dSJames Smart 
2184e399441dSJames Smart 	return 0;
2185e399441dSJames Smart }
2186e399441dSJames Smart 
2187e399441dSJames Smart static int
2188e399441dSJames Smart nvme_fc_reset_nvme_ctrl(struct nvme_ctrl *nctrl)
2189e399441dSJames Smart {
2190e399441dSJames Smart 	return -EIO;
2191e399441dSJames Smart }
2192e399441dSJames Smart 
2193e399441dSJames Smart static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
2194e399441dSJames Smart 	.name			= "fc",
2195e399441dSJames Smart 	.module			= THIS_MODULE,
2196e399441dSJames Smart 	.is_fabrics		= true,
2197e399441dSJames Smart 	.reg_read32		= nvmf_reg_read32,
2198e399441dSJames Smart 	.reg_read64		= nvmf_reg_read64,
2199e399441dSJames Smart 	.reg_write32		= nvmf_reg_write32,
2200e399441dSJames Smart 	.reset_ctrl		= nvme_fc_reset_nvme_ctrl,
2201e399441dSJames Smart 	.free_ctrl		= nvme_fc_free_nvme_ctrl,
2202e399441dSJames Smart 	.submit_async_event	= nvme_fc_submit_async_event,
2203e399441dSJames Smart 	.delete_ctrl		= nvme_fc_del_nvme_ctrl,
2204e399441dSJames Smart 	.get_subsysnqn		= nvmf_get_subsysnqn,
2205e399441dSJames Smart 	.get_address		= nvmf_get_address,
2206e399441dSJames Smart };
2207e399441dSJames Smart 
2208e399441dSJames Smart static int
2209e399441dSJames Smart nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2210e399441dSJames Smart {
2211e399441dSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2212e399441dSJames Smart 	int ret;
2213e399441dSJames Smart 
2214e399441dSJames Smart 	ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
2215e399441dSJames Smart 	if (ret) {
2216e399441dSJames Smart 		dev_info(ctrl->ctrl.device,
2217e399441dSJames Smart 			"set_queue_count failed: %d\n", ret);
2218e399441dSJames Smart 		return ret;
2219e399441dSJames Smart 	}
2220e399441dSJames Smart 
2221e399441dSJames Smart 	ctrl->queue_count = opts->nr_io_queues + 1;
2222e399441dSJames Smart 	if (!opts->nr_io_queues)
2223e399441dSJames Smart 		return 0;
2224e399441dSJames Smart 
2225e399441dSJames Smart 	dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n",
2226e399441dSJames Smart 			opts->nr_io_queues);
2227e399441dSJames Smart 
2228e399441dSJames Smart 	nvme_fc_init_io_queues(ctrl);
2229e399441dSJames Smart 
2230e399441dSJames Smart 	memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2231e399441dSJames Smart 	ctrl->tag_set.ops = &nvme_fc_mq_ops;
2232e399441dSJames Smart 	ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2233e399441dSJames Smart 	ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2234e399441dSJames Smart 	ctrl->tag_set.numa_node = NUMA_NO_NODE;
2235e399441dSJames Smart 	ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2236e399441dSJames Smart 	ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2237e399441dSJames Smart 					(SG_CHUNK_SIZE *
2238e399441dSJames Smart 						sizeof(struct scatterlist)) +
2239e399441dSJames Smart 					ctrl->lport->ops->fcprqst_priv_sz;
2240e399441dSJames Smart 	ctrl->tag_set.driver_data = ctrl;
2241e399441dSJames Smart 	ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
2242e399441dSJames Smart 	ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2243e399441dSJames Smart 
2244e399441dSJames Smart 	ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2245e399441dSJames Smart 	if (ret)
2246e399441dSJames Smart 		return ret;
2247e399441dSJames Smart 
2248e399441dSJames Smart 	ctrl->ctrl.tagset = &ctrl->tag_set;
2249e399441dSJames Smart 
2250e399441dSJames Smart 	ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2251e399441dSJames Smart 	if (IS_ERR(ctrl->ctrl.connect_q)) {
2252e399441dSJames Smart 		ret = PTR_ERR(ctrl->ctrl.connect_q);
2253e399441dSJames Smart 		goto out_free_tag_set;
2254e399441dSJames Smart 	}
2255e399441dSJames Smart 
2256e399441dSJames Smart 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2257e399441dSJames Smart 	if (ret)
2258e399441dSJames Smart 		goto out_cleanup_blk_queue;
2259e399441dSJames Smart 
2260e399441dSJames Smart 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2261e399441dSJames Smart 	if (ret)
2262e399441dSJames Smart 		goto out_delete_hw_queues;
2263e399441dSJames Smart 
2264e399441dSJames Smart 	return 0;
2265e399441dSJames Smart 
2266e399441dSJames Smart out_delete_hw_queues:
2267e399441dSJames Smart 	nvme_fc_delete_hw_io_queues(ctrl);
2268e399441dSJames Smart out_cleanup_blk_queue:
2269e399441dSJames Smart 	nvme_stop_keep_alive(&ctrl->ctrl);
2270e399441dSJames Smart 	blk_cleanup_queue(ctrl->ctrl.connect_q);
2271e399441dSJames Smart out_free_tag_set:
2272e399441dSJames Smart 	blk_mq_free_tag_set(&ctrl->tag_set);
2273e399441dSJames Smart 	nvme_fc_free_io_queues(ctrl);
2274e399441dSJames Smart 
2275e399441dSJames Smart 	/* force put free routine to ignore io queues */
2276e399441dSJames Smart 	ctrl->ctrl.tagset = NULL;
2277e399441dSJames Smart 
2278e399441dSJames Smart 	return ret;
2279e399441dSJames Smart }
2280e399441dSJames Smart 
2281e399441dSJames Smart 
2282e399441dSJames Smart static struct nvme_ctrl *
2283e399441dSJames Smart __nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
2284e399441dSJames Smart 	struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
2285e399441dSJames Smart {
2286e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl;
2287e399441dSJames Smart 	unsigned long flags;
2288e399441dSJames Smart 	int ret, idx;
2289e399441dSJames Smart 	bool changed;
2290e399441dSJames Smart 
2291e399441dSJames Smart 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2292e399441dSJames Smart 	if (!ctrl) {
2293e399441dSJames Smart 		ret = -ENOMEM;
2294e399441dSJames Smart 		goto out_fail;
2295e399441dSJames Smart 	}
2296e399441dSJames Smart 
2297e399441dSJames Smart 	idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
2298e399441dSJames Smart 	if (idx < 0) {
2299e399441dSJames Smart 		ret = -ENOSPC;
2300e399441dSJames Smart 		goto out_free_ctrl;
2301e399441dSJames Smart 	}
2302e399441dSJames Smart 
2303e399441dSJames Smart 	ctrl->ctrl.opts = opts;
2304e399441dSJames Smart 	INIT_LIST_HEAD(&ctrl->ctrl_list);
2305e399441dSJames Smart 	INIT_LIST_HEAD(&ctrl->ls_req_list);
2306e399441dSJames Smart 	ctrl->lport = lport;
2307e399441dSJames Smart 	ctrl->rport = rport;
2308e399441dSJames Smart 	ctrl->dev = lport->dev;
2309e399441dSJames Smart 	ctrl->state = FCCTRL_INIT;
2310e399441dSJames Smart 	ctrl->cnum = idx;
2311e399441dSJames Smart 
2312e399441dSJames Smart 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
2313e399441dSJames Smart 	if (ret)
2314e399441dSJames Smart 		goto out_free_ida;
2315e399441dSJames Smart 
2316e399441dSJames Smart 	get_device(ctrl->dev);
2317e399441dSJames Smart 	kref_init(&ctrl->ref);
2318e399441dSJames Smart 
2319e399441dSJames Smart 	INIT_WORK(&ctrl->delete_work, nvme_fc_del_ctrl_work);
2320e399441dSJames Smart 	spin_lock_init(&ctrl->lock);
2321e399441dSJames Smart 
2322e399441dSJames Smart 	/* io queue count */
2323e399441dSJames Smart 	ctrl->queue_count = min_t(unsigned int,
2324e399441dSJames Smart 				opts->nr_io_queues,
2325e399441dSJames Smart 				lport->ops->max_hw_queues);
2326e399441dSJames Smart 	opts->nr_io_queues = ctrl->queue_count;	/* so opts has valid value */
2327e399441dSJames Smart 	ctrl->queue_count++;	/* +1 for admin queue */
2328e399441dSJames Smart 
2329e399441dSJames Smart 	ctrl->ctrl.sqsize = opts->queue_size - 1;
2330e399441dSJames Smart 	ctrl->ctrl.kato = opts->kato;
2331e399441dSJames Smart 
2332e399441dSJames Smart 	ret = -ENOMEM;
2333e399441dSJames Smart 	ctrl->queues = kcalloc(ctrl->queue_count, sizeof(struct nvme_fc_queue),
2334e399441dSJames Smart 				GFP_KERNEL);
2335e399441dSJames Smart 	if (!ctrl->queues)
2336e399441dSJames Smart 		goto out_uninit_ctrl;
2337e399441dSJames Smart 
2338e399441dSJames Smart 	ret = nvme_fc_configure_admin_queue(ctrl);
2339e399441dSJames Smart 	if (ret)
2340e399441dSJames Smart 		goto out_uninit_ctrl;
2341e399441dSJames Smart 
2342e399441dSJames Smart 	/* sanity checks */
2343e399441dSJames Smart 
2344e399441dSJames Smart 	/* FC-NVME does not have other data in the capsule */
2345e399441dSJames Smart 	if (ctrl->ctrl.icdoff) {
2346e399441dSJames Smart 		dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
2347e399441dSJames Smart 				ctrl->ctrl.icdoff);
2348e399441dSJames Smart 		goto out_remove_admin_queue;
2349e399441dSJames Smart 	}
2350e399441dSJames Smart 
2351e399441dSJames Smart 	/* FC-NVME supports normal SGL Data Block Descriptors */
2352e399441dSJames Smart 
2353e399441dSJames Smart 	if (opts->queue_size > ctrl->ctrl.maxcmd) {
2354e399441dSJames Smart 		/* warn if maxcmd is lower than queue_size */
2355e399441dSJames Smart 		dev_warn(ctrl->ctrl.device,
2356e399441dSJames Smart 			"queue_size %zu > ctrl maxcmd %u, reducing "
2357e399441dSJames Smart 			"to queue_size\n",
2358e399441dSJames Smart 			opts->queue_size, ctrl->ctrl.maxcmd);
2359e399441dSJames Smart 		opts->queue_size = ctrl->ctrl.maxcmd;
2360e399441dSJames Smart 	}
2361e399441dSJames Smart 
2362e399441dSJames Smart 	ret = nvme_fc_init_aen_ops(ctrl);
2363e399441dSJames Smart 	if (ret)
2364e399441dSJames Smart 		goto out_exit_aen_ops;
2365e399441dSJames Smart 
2366e399441dSJames Smart 	if (ctrl->queue_count > 1) {
2367e399441dSJames Smart 		ret = nvme_fc_create_io_queues(ctrl);
2368e399441dSJames Smart 		if (ret)
2369e399441dSJames Smart 			goto out_exit_aen_ops;
2370e399441dSJames Smart 	}
2371e399441dSJames Smart 
2372e399441dSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
2373e399441dSJames Smart 	ctrl->state = FCCTRL_ACTIVE;
2374e399441dSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
2375e399441dSJames Smart 
2376e399441dSJames Smart 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
2377e399441dSJames Smart 	WARN_ON_ONCE(!changed);
2378e399441dSJames Smart 
2379e399441dSJames Smart 	dev_info(ctrl->ctrl.device,
2380c7034898SJames Smart 		"NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
2381c7034898SJames Smart 		ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
2382e399441dSJames Smart 
2383e399441dSJames Smart 	kref_get(&ctrl->ctrl.kref);
2384e399441dSJames Smart 
2385e399441dSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
2386e399441dSJames Smart 	list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
2387e399441dSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
2388e399441dSJames Smart 
2389e399441dSJames Smart 	if (opts->nr_io_queues) {
2390e399441dSJames Smart 		nvme_queue_scan(&ctrl->ctrl);
2391e399441dSJames Smart 		nvme_queue_async_events(&ctrl->ctrl);
2392e399441dSJames Smart 	}
2393e399441dSJames Smart 
2394e399441dSJames Smart 	return &ctrl->ctrl;
2395e399441dSJames Smart 
2396e399441dSJames Smart out_exit_aen_ops:
2397e399441dSJames Smart 	nvme_fc_exit_aen_ops(ctrl);
2398e399441dSJames Smart out_remove_admin_queue:
2399e399441dSJames Smart 	/* send a Disconnect(association) LS to fc-nvme target */
2400e399441dSJames Smart 	nvme_fc_xmt_disconnect_assoc(ctrl);
2401e399441dSJames Smart 	nvme_stop_keep_alive(&ctrl->ctrl);
2402e399441dSJames Smart 	nvme_fc_destroy_admin_queue(ctrl);
2403e399441dSJames Smart out_uninit_ctrl:
2404e399441dSJames Smart 	nvme_uninit_ctrl(&ctrl->ctrl);
2405e399441dSJames Smart 	nvme_put_ctrl(&ctrl->ctrl);
2406e399441dSJames Smart 	if (ret > 0)
2407e399441dSJames Smart 		ret = -EIO;
2408e399441dSJames Smart 	/* exit via here will follow ctlr ref point callbacks to free */
2409e399441dSJames Smart 	return ERR_PTR(ret);
2410e399441dSJames Smart 
2411e399441dSJames Smart out_free_ida:
2412e399441dSJames Smart 	ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2413e399441dSJames Smart out_free_ctrl:
2414e399441dSJames Smart 	kfree(ctrl);
2415e399441dSJames Smart out_fail:
2416e399441dSJames Smart 	nvme_fc_rport_put(rport);
2417e399441dSJames Smart 	/* exit via here doesn't follow ctlr ref points */
2418e399441dSJames Smart 	return ERR_PTR(ret);
2419e399441dSJames Smart }
2420e399441dSJames Smart 
2421e399441dSJames Smart enum {
2422e399441dSJames Smart 	FCT_TRADDR_ERR		= 0,
2423e399441dSJames Smart 	FCT_TRADDR_WWNN		= 1 << 0,
2424e399441dSJames Smart 	FCT_TRADDR_WWPN		= 1 << 1,
2425e399441dSJames Smart };
2426e399441dSJames Smart 
2427e399441dSJames Smart struct nvmet_fc_traddr {
2428e399441dSJames Smart 	u64	nn;
2429e399441dSJames Smart 	u64	pn;
2430e399441dSJames Smart };
2431e399441dSJames Smart 
2432e399441dSJames Smart static const match_table_t traddr_opt_tokens = {
2433e399441dSJames Smart 	{ FCT_TRADDR_WWNN,	"nn-%s"		},
2434e399441dSJames Smart 	{ FCT_TRADDR_WWPN,	"pn-%s"		},
2435e399441dSJames Smart 	{ FCT_TRADDR_ERR,	NULL		}
2436e399441dSJames Smart };
2437e399441dSJames Smart 
2438e399441dSJames Smart static int
2439e399441dSJames Smart nvme_fc_parse_address(struct nvmet_fc_traddr *traddr, char *buf)
2440e399441dSJames Smart {
2441e399441dSJames Smart 	substring_t args[MAX_OPT_ARGS];
2442e399441dSJames Smart 	char *options, *o, *p;
2443e399441dSJames Smart 	int token, ret = 0;
2444e399441dSJames Smart 	u64 token64;
2445e399441dSJames Smart 
2446e399441dSJames Smart 	options = o = kstrdup(buf, GFP_KERNEL);
2447e399441dSJames Smart 	if (!options)
2448e399441dSJames Smart 		return -ENOMEM;
2449e399441dSJames Smart 
2450e399441dSJames Smart 	while ((p = strsep(&o, ":\n")) != NULL) {
2451e399441dSJames Smart 		if (!*p)
2452e399441dSJames Smart 			continue;
2453e399441dSJames Smart 
2454e399441dSJames Smart 		token = match_token(p, traddr_opt_tokens, args);
2455e399441dSJames Smart 		switch (token) {
2456e399441dSJames Smart 		case FCT_TRADDR_WWNN:
2457e399441dSJames Smart 			if (match_u64(args, &token64)) {
2458e399441dSJames Smart 				ret = -EINVAL;
2459e399441dSJames Smart 				goto out;
2460e399441dSJames Smart 			}
2461e399441dSJames Smart 			traddr->nn = token64;
2462e399441dSJames Smart 			break;
2463e399441dSJames Smart 		case FCT_TRADDR_WWPN:
2464e399441dSJames Smart 			if (match_u64(args, &token64)) {
2465e399441dSJames Smart 				ret = -EINVAL;
2466e399441dSJames Smart 				goto out;
2467e399441dSJames Smart 			}
2468e399441dSJames Smart 			traddr->pn = token64;
2469e399441dSJames Smart 			break;
2470e399441dSJames Smart 		default:
2471e399441dSJames Smart 			pr_warn("unknown traddr token or missing value '%s'\n",
2472e399441dSJames Smart 					p);
2473e399441dSJames Smart 			ret = -EINVAL;
2474e399441dSJames Smart 			goto out;
2475e399441dSJames Smart 		}
2476e399441dSJames Smart 	}
2477e399441dSJames Smart 
2478e399441dSJames Smart out:
2479e399441dSJames Smart 	kfree(options);
2480e399441dSJames Smart 	return ret;
2481e399441dSJames Smart }
2482e399441dSJames Smart 
2483e399441dSJames Smart static struct nvme_ctrl *
2484e399441dSJames Smart nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
2485e399441dSJames Smart {
2486e399441dSJames Smart 	struct nvme_fc_lport *lport;
2487e399441dSJames Smart 	struct nvme_fc_rport *rport;
2488e399441dSJames Smart 	struct nvmet_fc_traddr laddr = { 0L, 0L };
2489e399441dSJames Smart 	struct nvmet_fc_traddr raddr = { 0L, 0L };
2490e399441dSJames Smart 	unsigned long flags;
2491e399441dSJames Smart 	int ret;
2492e399441dSJames Smart 
2493e399441dSJames Smart 	ret = nvme_fc_parse_address(&raddr, opts->traddr);
2494e399441dSJames Smart 	if (ret || !raddr.nn || !raddr.pn)
2495e399441dSJames Smart 		return ERR_PTR(-EINVAL);
2496e399441dSJames Smart 
2497e399441dSJames Smart 	ret = nvme_fc_parse_address(&laddr, opts->host_traddr);
2498e399441dSJames Smart 	if (ret || !laddr.nn || !laddr.pn)
2499e399441dSJames Smart 		return ERR_PTR(-EINVAL);
2500e399441dSJames Smart 
2501e399441dSJames Smart 	/* find the host and remote ports to connect together */
2502e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
2503e399441dSJames Smart 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
2504e399441dSJames Smart 		if (lport->localport.node_name != laddr.nn ||
2505e399441dSJames Smart 		    lport->localport.port_name != laddr.pn)
2506e399441dSJames Smart 			continue;
2507e399441dSJames Smart 
2508e399441dSJames Smart 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
2509e399441dSJames Smart 			if (rport->remoteport.node_name != raddr.nn ||
2510e399441dSJames Smart 			    rport->remoteport.port_name != raddr.pn)
2511e399441dSJames Smart 				continue;
2512e399441dSJames Smart 
2513e399441dSJames Smart 			/* if fail to get reference fall through. Will error */
2514e399441dSJames Smart 			if (!nvme_fc_rport_get(rport))
2515e399441dSJames Smart 				break;
2516e399441dSJames Smart 
2517e399441dSJames Smart 			spin_unlock_irqrestore(&nvme_fc_lock, flags);
2518e399441dSJames Smart 
2519e399441dSJames Smart 			return __nvme_fc_create_ctrl(dev, opts, lport, rport);
2520e399441dSJames Smart 		}
2521e399441dSJames Smart 	}
2522e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
2523e399441dSJames Smart 
2524e399441dSJames Smart 	return ERR_PTR(-ENOENT);
2525e399441dSJames Smart }
2526e399441dSJames Smart 
2527e399441dSJames Smart 
2528e399441dSJames Smart static struct nvmf_transport_ops nvme_fc_transport = {
2529e399441dSJames Smart 	.name		= "fc",
2530e399441dSJames Smart 	.required_opts	= NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
2531e399441dSJames Smart 	.allowed_opts	= NVMF_OPT_RECONNECT_DELAY,
2532e399441dSJames Smart 	.create_ctrl	= nvme_fc_create_ctrl,
2533e399441dSJames Smart };
2534e399441dSJames Smart 
2535e399441dSJames Smart static int __init nvme_fc_init_module(void)
2536e399441dSJames Smart {
2537c0e4a6f5SSagi Grimberg 	int ret;
2538c0e4a6f5SSagi Grimberg 
2539e399441dSJames Smart 	nvme_fc_wq = create_workqueue("nvme_fc_wq");
2540e399441dSJames Smart 	if (!nvme_fc_wq)
2541e399441dSJames Smart 		return -ENOMEM;
2542e399441dSJames Smart 
2543c0e4a6f5SSagi Grimberg 	ret = nvmf_register_transport(&nvme_fc_transport);
2544c0e4a6f5SSagi Grimberg 	if (ret)
2545c0e4a6f5SSagi Grimberg 		goto err;
2546c0e4a6f5SSagi Grimberg 
2547c0e4a6f5SSagi Grimberg 	return 0;
2548c0e4a6f5SSagi Grimberg err:
2549c0e4a6f5SSagi Grimberg 	destroy_workqueue(nvme_fc_wq);
2550c0e4a6f5SSagi Grimberg 	return ret;
2551e399441dSJames Smart }
2552e399441dSJames Smart 
2553e399441dSJames Smart static void __exit nvme_fc_exit_module(void)
2554e399441dSJames Smart {
2555e399441dSJames Smart 	/* sanity check - all lports should be removed */
2556e399441dSJames Smart 	if (!list_empty(&nvme_fc_lport_list))
2557e399441dSJames Smart 		pr_warn("%s: localport list not empty\n", __func__);
2558e399441dSJames Smart 
2559e399441dSJames Smart 	nvmf_unregister_transport(&nvme_fc_transport);
2560e399441dSJames Smart 
2561e399441dSJames Smart 	destroy_workqueue(nvme_fc_wq);
2562e399441dSJames Smart 
2563e399441dSJames Smart 	ida_destroy(&nvme_fc_local_port_cnt);
2564e399441dSJames Smart 	ida_destroy(&nvme_fc_ctrl_cnt);
2565e399441dSJames Smart }
2566e399441dSJames Smart 
2567e399441dSJames Smart module_init(nvme_fc_init_module);
2568e399441dSJames Smart module_exit(nvme_fc_exit_module);
2569e399441dSJames Smart 
2570e399441dSJames Smart MODULE_LICENSE("GPL v2");
2571