xref: /openbmc/linux/drivers/nvme/host/fc.c (revision d858e5f0)
1e399441dSJames Smart /*
2e399441dSJames Smart  * Copyright (c) 2016 Avago Technologies.  All rights reserved.
3e399441dSJames Smart  *
4e399441dSJames Smart  * This program is free software; you can redistribute it and/or modify
5e399441dSJames Smart  * it under the terms of version 2 of the GNU General Public License as
6e399441dSJames Smart  * published by the Free Software Foundation.
7e399441dSJames Smart  *
8e399441dSJames Smart  * This program is distributed in the hope that it will be useful.
9e399441dSJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
10e399441dSJames Smart  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
11e399441dSJames Smart  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO
12e399441dSJames Smart  * THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
13e399441dSJames Smart  * See the GNU General Public License for more details, a copy of which
14e399441dSJames Smart  * can be found in the file COPYING included with this package
15e399441dSJames Smart  *
16e399441dSJames Smart  */
17e399441dSJames Smart #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18e399441dSJames Smart #include <linux/module.h>
19e399441dSJames Smart #include <linux/parser.h>
20e399441dSJames Smart #include <uapi/scsi/fc/fc_fs.h>
21e399441dSJames Smart #include <uapi/scsi/fc/fc_els.h>
2261bff8efSJames Smart #include <linux/delay.h>
23e399441dSJames Smart 
24e399441dSJames Smart #include "nvme.h"
25e399441dSJames Smart #include "fabrics.h"
26e399441dSJames Smart #include <linux/nvme-fc-driver.h>
27e399441dSJames Smart #include <linux/nvme-fc.h>
28e399441dSJames Smart 
29e399441dSJames Smart 
30e399441dSJames Smart /* *************************** Data Structures/Defines ****************** */
31e399441dSJames Smart 
32e399441dSJames Smart 
33e399441dSJames Smart /*
34e399441dSJames Smart  * We handle AEN commands ourselves and don't even let the
35e399441dSJames Smart  * block layer know about them.
36e399441dSJames Smart  */
37e399441dSJames Smart #define NVME_FC_NR_AEN_COMMANDS	1
38e399441dSJames Smart #define NVME_FC_AQ_BLKMQ_DEPTH	\
397aa1f427SSagi Grimberg 	(NVME_AQ_DEPTH - NVME_FC_NR_AEN_COMMANDS)
40e399441dSJames Smart #define AEN_CMDID_BASE		(NVME_FC_AQ_BLKMQ_DEPTH + 1)
41e399441dSJames Smart 
42e399441dSJames Smart enum nvme_fc_queue_flags {
43e399441dSJames Smart 	NVME_FC_Q_CONNECTED = (1 << 0),
44e399441dSJames Smart };
45e399441dSJames Smart 
46e399441dSJames Smart #define NVMEFC_QUEUE_DELAY	3		/* ms units */
47e399441dSJames Smart 
48e399441dSJames Smart struct nvme_fc_queue {
49e399441dSJames Smart 	struct nvme_fc_ctrl	*ctrl;
50e399441dSJames Smart 	struct device		*dev;
51e399441dSJames Smart 	struct blk_mq_hw_ctx	*hctx;
52e399441dSJames Smart 	void			*lldd_handle;
53e399441dSJames Smart 	int			queue_size;
54e399441dSJames Smart 	size_t			cmnd_capsule_len;
55e399441dSJames Smart 	u32			qnum;
56e399441dSJames Smart 	u32			rqcnt;
57e399441dSJames Smart 	u32			seqno;
58e399441dSJames Smart 
59e399441dSJames Smart 	u64			connection_id;
60e399441dSJames Smart 	atomic_t		csn;
61e399441dSJames Smart 
62e399441dSJames Smart 	unsigned long		flags;
63e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
64e399441dSJames Smart 
658d64daf7SJames Smart enum nvme_fcop_flags {
668d64daf7SJames Smart 	FCOP_FLAGS_TERMIO	= (1 << 0),
678d64daf7SJames Smart 	FCOP_FLAGS_RELEASED	= (1 << 1),
688d64daf7SJames Smart 	FCOP_FLAGS_COMPLETE	= (1 << 2),
6978a7ac26SJames Smart 	FCOP_FLAGS_AEN		= (1 << 3),
708d64daf7SJames Smart };
718d64daf7SJames Smart 
72e399441dSJames Smart struct nvmefc_ls_req_op {
73e399441dSJames Smart 	struct nvmefc_ls_req	ls_req;
74e399441dSJames Smart 
75c913a8b0SJames Smart 	struct nvme_fc_rport	*rport;
76e399441dSJames Smart 	struct nvme_fc_queue	*queue;
77e399441dSJames Smart 	struct request		*rq;
788d64daf7SJames Smart 	u32			flags;
79e399441dSJames Smart 
80e399441dSJames Smart 	int			ls_error;
81e399441dSJames Smart 	struct completion	ls_done;
82c913a8b0SJames Smart 	struct list_head	lsreq_list;	/* rport->ls_req_list */
83e399441dSJames Smart 	bool			req_queued;
84e399441dSJames Smart };
85e399441dSJames Smart 
86e399441dSJames Smart enum nvme_fcpop_state {
87e399441dSJames Smart 	FCPOP_STATE_UNINIT	= 0,
88e399441dSJames Smart 	FCPOP_STATE_IDLE	= 1,
89e399441dSJames Smart 	FCPOP_STATE_ACTIVE	= 2,
90e399441dSJames Smart 	FCPOP_STATE_ABORTED	= 3,
9178a7ac26SJames Smart 	FCPOP_STATE_COMPLETE	= 4,
92e399441dSJames Smart };
93e399441dSJames Smart 
94e399441dSJames Smart struct nvme_fc_fcp_op {
95e399441dSJames Smart 	struct nvme_request	nreq;		/*
96e399441dSJames Smart 						 * nvme/host/core.c
97e399441dSJames Smart 						 * requires this to be
98e399441dSJames Smart 						 * the 1st element in the
99e399441dSJames Smart 						 * private structure
100e399441dSJames Smart 						 * associated with the
101e399441dSJames Smart 						 * request.
102e399441dSJames Smart 						 */
103e399441dSJames Smart 	struct nvmefc_fcp_req	fcp_req;
104e399441dSJames Smart 
105e399441dSJames Smart 	struct nvme_fc_ctrl	*ctrl;
106e399441dSJames Smart 	struct nvme_fc_queue	*queue;
107e399441dSJames Smart 	struct request		*rq;
108e399441dSJames Smart 
109e399441dSJames Smart 	atomic_t		state;
11078a7ac26SJames Smart 	u32			flags;
111e399441dSJames Smart 	u32			rqno;
112e399441dSJames Smart 	u32			nents;
113e399441dSJames Smart 
114e399441dSJames Smart 	struct nvme_fc_cmd_iu	cmd_iu;
115e399441dSJames Smart 	struct nvme_fc_ersp_iu	rsp_iu;
116e399441dSJames Smart };
117e399441dSJames Smart 
118e399441dSJames Smart struct nvme_fc_lport {
119e399441dSJames Smart 	struct nvme_fc_local_port	localport;
120e399441dSJames Smart 
121e399441dSJames Smart 	struct ida			endp_cnt;
122e399441dSJames Smart 	struct list_head		port_list;	/* nvme_fc_port_list */
123e399441dSJames Smart 	struct list_head		endp_list;
124e399441dSJames Smart 	struct device			*dev;	/* physical device for dma */
125e399441dSJames Smart 	struct nvme_fc_port_template	*ops;
126e399441dSJames Smart 	struct kref			ref;
127e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
128e399441dSJames Smart 
129e399441dSJames Smart struct nvme_fc_rport {
130e399441dSJames Smart 	struct nvme_fc_remote_port	remoteport;
131e399441dSJames Smart 
132e399441dSJames Smart 	struct list_head		endp_list; /* for lport->endp_list */
133e399441dSJames Smart 	struct list_head		ctrl_list;
134c913a8b0SJames Smart 	struct list_head		ls_req_list;
135c913a8b0SJames Smart 	struct device			*dev;	/* physical device for dma */
136c913a8b0SJames Smart 	struct nvme_fc_lport		*lport;
137e399441dSJames Smart 	spinlock_t			lock;
138e399441dSJames Smart 	struct kref			ref;
139e399441dSJames Smart } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
140e399441dSJames Smart 
14161bff8efSJames Smart enum nvme_fcctrl_flags {
14261bff8efSJames Smart 	FCCTRL_TERMIO		= (1 << 0),
143e399441dSJames Smart };
144e399441dSJames Smart 
145e399441dSJames Smart struct nvme_fc_ctrl {
146e399441dSJames Smart 	spinlock_t		lock;
147e399441dSJames Smart 	struct nvme_fc_queue	*queues;
148e399441dSJames Smart 	struct device		*dev;
149e399441dSJames Smart 	struct nvme_fc_lport	*lport;
150e399441dSJames Smart 	struct nvme_fc_rport	*rport;
151e399441dSJames Smart 	u32			cnum;
152e399441dSJames Smart 
153e399441dSJames Smart 	u64			association_id;
154e399441dSJames Smart 
155e399441dSJames Smart 	u64			cap;
156e399441dSJames Smart 
157e399441dSJames Smart 	struct list_head	ctrl_list;	/* rport->ctrl_list */
158e399441dSJames Smart 
159e399441dSJames Smart 	struct blk_mq_tag_set	admin_tag_set;
160e399441dSJames Smart 	struct blk_mq_tag_set	tag_set;
161e399441dSJames Smart 
162e399441dSJames Smart 	struct work_struct	delete_work;
16361bff8efSJames Smart 	struct delayed_work	connect_work;
16461bff8efSJames Smart 
165e399441dSJames Smart 	struct kref		ref;
16661bff8efSJames Smart 	u32			flags;
16761bff8efSJames Smart 	u32			iocnt;
16836715cf4SJames Smart 	wait_queue_head_t	ioabort_wait;
169e399441dSJames Smart 
170e399441dSJames Smart 	struct nvme_fc_fcp_op	aen_ops[NVME_FC_NR_AEN_COMMANDS];
171e399441dSJames Smart 
172e399441dSJames Smart 	struct nvme_ctrl	ctrl;
173e399441dSJames Smart };
174e399441dSJames Smart 
175e399441dSJames Smart static inline struct nvme_fc_ctrl *
176e399441dSJames Smart to_fc_ctrl(struct nvme_ctrl *ctrl)
177e399441dSJames Smart {
178e399441dSJames Smart 	return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
179e399441dSJames Smart }
180e399441dSJames Smart 
181e399441dSJames Smart static inline struct nvme_fc_lport *
182e399441dSJames Smart localport_to_lport(struct nvme_fc_local_port *portptr)
183e399441dSJames Smart {
184e399441dSJames Smart 	return container_of(portptr, struct nvme_fc_lport, localport);
185e399441dSJames Smart }
186e399441dSJames Smart 
187e399441dSJames Smart static inline struct nvme_fc_rport *
188e399441dSJames Smart remoteport_to_rport(struct nvme_fc_remote_port *portptr)
189e399441dSJames Smart {
190e399441dSJames Smart 	return container_of(portptr, struct nvme_fc_rport, remoteport);
191e399441dSJames Smart }
192e399441dSJames Smart 
193e399441dSJames Smart static inline struct nvmefc_ls_req_op *
194e399441dSJames Smart ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
195e399441dSJames Smart {
196e399441dSJames Smart 	return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
197e399441dSJames Smart }
198e399441dSJames Smart 
199e399441dSJames Smart static inline struct nvme_fc_fcp_op *
200e399441dSJames Smart fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
201e399441dSJames Smart {
202e399441dSJames Smart 	return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
203e399441dSJames Smart }
204e399441dSJames Smart 
205e399441dSJames Smart 
206e399441dSJames Smart 
207e399441dSJames Smart /* *************************** Globals **************************** */
208e399441dSJames Smart 
209e399441dSJames Smart 
210e399441dSJames Smart static DEFINE_SPINLOCK(nvme_fc_lock);
211e399441dSJames Smart 
212e399441dSJames Smart static LIST_HEAD(nvme_fc_lport_list);
213e399441dSJames Smart static DEFINE_IDA(nvme_fc_local_port_cnt);
214e399441dSJames Smart static DEFINE_IDA(nvme_fc_ctrl_cnt);
215e399441dSJames Smart 
216e399441dSJames Smart 
217e399441dSJames Smart 
218e399441dSJames Smart 
219e399441dSJames Smart /* *********************** FC-NVME Port Management ************************ */
220e399441dSJames Smart 
221e399441dSJames Smart static int __nvme_fc_del_ctrl(struct nvme_fc_ctrl *);
222e399441dSJames Smart static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
223e399441dSJames Smart 			struct nvme_fc_queue *, unsigned int);
224e399441dSJames Smart 
225e399441dSJames Smart 
226e399441dSJames Smart /**
227e399441dSJames Smart  * nvme_fc_register_localport - transport entry point called by an
228e399441dSJames Smart  *                              LLDD to register the existence of a NVME
229e399441dSJames Smart  *                              host FC port.
230e399441dSJames Smart  * @pinfo:     pointer to information about the port to be registered
231e399441dSJames Smart  * @template:  LLDD entrypoints and operational parameters for the port
232e399441dSJames Smart  * @dev:       physical hardware device node port corresponds to. Will be
233e399441dSJames Smart  *             used for DMA mappings
234e399441dSJames Smart  * @lport_p:   pointer to a local port pointer. Upon success, the routine
235e399441dSJames Smart  *             will allocate a nvme_fc_local_port structure and place its
236e399441dSJames Smart  *             address in the local port pointer. Upon failure, local port
237e399441dSJames Smart  *             pointer will be set to 0.
238e399441dSJames Smart  *
239e399441dSJames Smart  * Returns:
240e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
241e399441dSJames Smart  * (ex: -ENXIO) upon failure.
242e399441dSJames Smart  */
243e399441dSJames Smart int
244e399441dSJames Smart nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
245e399441dSJames Smart 			struct nvme_fc_port_template *template,
246e399441dSJames Smart 			struct device *dev,
247e399441dSJames Smart 			struct nvme_fc_local_port **portptr)
248e399441dSJames Smart {
249e399441dSJames Smart 	struct nvme_fc_lport *newrec;
250e399441dSJames Smart 	unsigned long flags;
251e399441dSJames Smart 	int ret, idx;
252e399441dSJames Smart 
253e399441dSJames Smart 	if (!template->localport_delete || !template->remoteport_delete ||
254e399441dSJames Smart 	    !template->ls_req || !template->fcp_io ||
255e399441dSJames Smart 	    !template->ls_abort || !template->fcp_abort ||
256e399441dSJames Smart 	    !template->max_hw_queues || !template->max_sgl_segments ||
257e399441dSJames Smart 	    !template->max_dif_sgl_segments || !template->dma_boundary) {
258e399441dSJames Smart 		ret = -EINVAL;
259e399441dSJames Smart 		goto out_reghost_failed;
260e399441dSJames Smart 	}
261e399441dSJames Smart 
262e399441dSJames Smart 	newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
263e399441dSJames Smart 			 GFP_KERNEL);
264e399441dSJames Smart 	if (!newrec) {
265e399441dSJames Smart 		ret = -ENOMEM;
266e399441dSJames Smart 		goto out_reghost_failed;
267e399441dSJames Smart 	}
268e399441dSJames Smart 
269e399441dSJames Smart 	idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
270e399441dSJames Smart 	if (idx < 0) {
271e399441dSJames Smart 		ret = -ENOSPC;
272e399441dSJames Smart 		goto out_fail_kfree;
273e399441dSJames Smart 	}
274e399441dSJames Smart 
275e399441dSJames Smart 	if (!get_device(dev) && dev) {
276e399441dSJames Smart 		ret = -ENODEV;
277e399441dSJames Smart 		goto out_ida_put;
278e399441dSJames Smart 	}
279e399441dSJames Smart 
280e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->port_list);
281e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->endp_list);
282e399441dSJames Smart 	kref_init(&newrec->ref);
283e399441dSJames Smart 	newrec->ops = template;
284e399441dSJames Smart 	newrec->dev = dev;
285e399441dSJames Smart 	ida_init(&newrec->endp_cnt);
286e399441dSJames Smart 	newrec->localport.private = &newrec[1];
287e399441dSJames Smart 	newrec->localport.node_name = pinfo->node_name;
288e399441dSJames Smart 	newrec->localport.port_name = pinfo->port_name;
289e399441dSJames Smart 	newrec->localport.port_role = pinfo->port_role;
290e399441dSJames Smart 	newrec->localport.port_id = pinfo->port_id;
291e399441dSJames Smart 	newrec->localport.port_state = FC_OBJSTATE_ONLINE;
292e399441dSJames Smart 	newrec->localport.port_num = idx;
293e399441dSJames Smart 
294e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
295e399441dSJames Smart 	list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
296e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
297e399441dSJames Smart 
298e399441dSJames Smart 	if (dev)
299e399441dSJames Smart 		dma_set_seg_boundary(dev, template->dma_boundary);
300e399441dSJames Smart 
301e399441dSJames Smart 	*portptr = &newrec->localport;
302e399441dSJames Smart 	return 0;
303e399441dSJames Smart 
304e399441dSJames Smart out_ida_put:
305e399441dSJames Smart 	ida_simple_remove(&nvme_fc_local_port_cnt, idx);
306e399441dSJames Smart out_fail_kfree:
307e399441dSJames Smart 	kfree(newrec);
308e399441dSJames Smart out_reghost_failed:
309e399441dSJames Smart 	*portptr = NULL;
310e399441dSJames Smart 
311e399441dSJames Smart 	return ret;
312e399441dSJames Smart }
313e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
314e399441dSJames Smart 
315e399441dSJames Smart static void
316e399441dSJames Smart nvme_fc_free_lport(struct kref *ref)
317e399441dSJames Smart {
318e399441dSJames Smart 	struct nvme_fc_lport *lport =
319e399441dSJames Smart 		container_of(ref, struct nvme_fc_lport, ref);
320e399441dSJames Smart 	unsigned long flags;
321e399441dSJames Smart 
322e399441dSJames Smart 	WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
323e399441dSJames Smart 	WARN_ON(!list_empty(&lport->endp_list));
324e399441dSJames Smart 
325e399441dSJames Smart 	/* remove from transport list */
326e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
327e399441dSJames Smart 	list_del(&lport->port_list);
328e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
329e399441dSJames Smart 
330e399441dSJames Smart 	/* let the LLDD know we've finished tearing it down */
331e399441dSJames Smart 	lport->ops->localport_delete(&lport->localport);
332e399441dSJames Smart 
333e399441dSJames Smart 	ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
334e399441dSJames Smart 	ida_destroy(&lport->endp_cnt);
335e399441dSJames Smart 
336e399441dSJames Smart 	put_device(lport->dev);
337e399441dSJames Smart 
338e399441dSJames Smart 	kfree(lport);
339e399441dSJames Smart }
340e399441dSJames Smart 
341e399441dSJames Smart static void
342e399441dSJames Smart nvme_fc_lport_put(struct nvme_fc_lport *lport)
343e399441dSJames Smart {
344e399441dSJames Smart 	kref_put(&lport->ref, nvme_fc_free_lport);
345e399441dSJames Smart }
346e399441dSJames Smart 
347e399441dSJames Smart static int
348e399441dSJames Smart nvme_fc_lport_get(struct nvme_fc_lport *lport)
349e399441dSJames Smart {
350e399441dSJames Smart 	return kref_get_unless_zero(&lport->ref);
351e399441dSJames Smart }
352e399441dSJames Smart 
353e399441dSJames Smart /**
354e399441dSJames Smart  * nvme_fc_unregister_localport - transport entry point called by an
355e399441dSJames Smart  *                              LLDD to deregister/remove a previously
356e399441dSJames Smart  *                              registered a NVME host FC port.
357e399441dSJames Smart  * @localport: pointer to the (registered) local port that is to be
358e399441dSJames Smart  *             deregistered.
359e399441dSJames Smart  *
360e399441dSJames Smart  * Returns:
361e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
362e399441dSJames Smart  * (ex: -ENXIO) upon failure.
363e399441dSJames Smart  */
364e399441dSJames Smart int
365e399441dSJames Smart nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
366e399441dSJames Smart {
367e399441dSJames Smart 	struct nvme_fc_lport *lport = localport_to_lport(portptr);
368e399441dSJames Smart 	unsigned long flags;
369e399441dSJames Smart 
370e399441dSJames Smart 	if (!portptr)
371e399441dSJames Smart 		return -EINVAL;
372e399441dSJames Smart 
373e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
374e399441dSJames Smart 
375e399441dSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
376e399441dSJames Smart 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
377e399441dSJames Smart 		return -EINVAL;
378e399441dSJames Smart 	}
379e399441dSJames Smart 	portptr->port_state = FC_OBJSTATE_DELETED;
380e399441dSJames Smart 
381e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
382e399441dSJames Smart 
383e399441dSJames Smart 	nvme_fc_lport_put(lport);
384e399441dSJames Smart 
385e399441dSJames Smart 	return 0;
386e399441dSJames Smart }
387e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
388e399441dSJames Smart 
389e399441dSJames Smart /**
390e399441dSJames Smart  * nvme_fc_register_remoteport - transport entry point called by an
391e399441dSJames Smart  *                              LLDD to register the existence of a NVME
392e399441dSJames Smart  *                              subsystem FC port on its fabric.
393e399441dSJames Smart  * @localport: pointer to the (registered) local port that the remote
394e399441dSJames Smart  *             subsystem port is connected to.
395e399441dSJames Smart  * @pinfo:     pointer to information about the port to be registered
396e399441dSJames Smart  * @rport_p:   pointer to a remote port pointer. Upon success, the routine
397e399441dSJames Smart  *             will allocate a nvme_fc_remote_port structure and place its
398e399441dSJames Smart  *             address in the remote port pointer. Upon failure, remote port
399e399441dSJames Smart  *             pointer will be set to 0.
400e399441dSJames Smart  *
401e399441dSJames Smart  * Returns:
402e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
403e399441dSJames Smart  * (ex: -ENXIO) upon failure.
404e399441dSJames Smart  */
405e399441dSJames Smart int
406e399441dSJames Smart nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
407e399441dSJames Smart 				struct nvme_fc_port_info *pinfo,
408e399441dSJames Smart 				struct nvme_fc_remote_port **portptr)
409e399441dSJames Smart {
410e399441dSJames Smart 	struct nvme_fc_lport *lport = localport_to_lport(localport);
411e399441dSJames Smart 	struct nvme_fc_rport *newrec;
412e399441dSJames Smart 	unsigned long flags;
413e399441dSJames Smart 	int ret, idx;
414e399441dSJames Smart 
415e399441dSJames Smart 	newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
416e399441dSJames Smart 			 GFP_KERNEL);
417e399441dSJames Smart 	if (!newrec) {
418e399441dSJames Smart 		ret = -ENOMEM;
419e399441dSJames Smart 		goto out_reghost_failed;
420e399441dSJames Smart 	}
421e399441dSJames Smart 
422e399441dSJames Smart 	if (!nvme_fc_lport_get(lport)) {
423e399441dSJames Smart 		ret = -ESHUTDOWN;
424e399441dSJames Smart 		goto out_kfree_rport;
425e399441dSJames Smart 	}
426e399441dSJames Smart 
427e399441dSJames Smart 	idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
428e399441dSJames Smart 	if (idx < 0) {
429e399441dSJames Smart 		ret = -ENOSPC;
430e399441dSJames Smart 		goto out_lport_put;
431e399441dSJames Smart 	}
432e399441dSJames Smart 
433e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->endp_list);
434e399441dSJames Smart 	INIT_LIST_HEAD(&newrec->ctrl_list);
435c913a8b0SJames Smart 	INIT_LIST_HEAD(&newrec->ls_req_list);
436e399441dSJames Smart 	kref_init(&newrec->ref);
437e399441dSJames Smart 	spin_lock_init(&newrec->lock);
438e399441dSJames Smart 	newrec->remoteport.localport = &lport->localport;
439c913a8b0SJames Smart 	newrec->dev = lport->dev;
440c913a8b0SJames Smart 	newrec->lport = lport;
441e399441dSJames Smart 	newrec->remoteport.private = &newrec[1];
442e399441dSJames Smart 	newrec->remoteport.port_role = pinfo->port_role;
443e399441dSJames Smart 	newrec->remoteport.node_name = pinfo->node_name;
444e399441dSJames Smart 	newrec->remoteport.port_name = pinfo->port_name;
445e399441dSJames Smart 	newrec->remoteport.port_id = pinfo->port_id;
446e399441dSJames Smart 	newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
447e399441dSJames Smart 	newrec->remoteport.port_num = idx;
448e399441dSJames Smart 
449e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
450e399441dSJames Smart 	list_add_tail(&newrec->endp_list, &lport->endp_list);
451e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
452e399441dSJames Smart 
453e399441dSJames Smart 	*portptr = &newrec->remoteport;
454e399441dSJames Smart 	return 0;
455e399441dSJames Smart 
456e399441dSJames Smart out_lport_put:
457e399441dSJames Smart 	nvme_fc_lport_put(lport);
458e399441dSJames Smart out_kfree_rport:
459e399441dSJames Smart 	kfree(newrec);
460e399441dSJames Smart out_reghost_failed:
461e399441dSJames Smart 	*portptr = NULL;
462e399441dSJames Smart 	return ret;
463e399441dSJames Smart }
464e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
465e399441dSJames Smart 
466e399441dSJames Smart static void
467e399441dSJames Smart nvme_fc_free_rport(struct kref *ref)
468e399441dSJames Smart {
469e399441dSJames Smart 	struct nvme_fc_rport *rport =
470e399441dSJames Smart 		container_of(ref, struct nvme_fc_rport, ref);
471e399441dSJames Smart 	struct nvme_fc_lport *lport =
472e399441dSJames Smart 			localport_to_lport(rport->remoteport.localport);
473e399441dSJames Smart 	unsigned long flags;
474e399441dSJames Smart 
475e399441dSJames Smart 	WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
476e399441dSJames Smart 	WARN_ON(!list_empty(&rport->ctrl_list));
477e399441dSJames Smart 
478e399441dSJames Smart 	/* remove from lport list */
479e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
480e399441dSJames Smart 	list_del(&rport->endp_list);
481e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
482e399441dSJames Smart 
483e399441dSJames Smart 	/* let the LLDD know we've finished tearing it down */
484e399441dSJames Smart 	lport->ops->remoteport_delete(&rport->remoteport);
485e399441dSJames Smart 
486e399441dSJames Smart 	ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
487e399441dSJames Smart 
488e399441dSJames Smart 	kfree(rport);
489e399441dSJames Smart 
490e399441dSJames Smart 	nvme_fc_lport_put(lport);
491e399441dSJames Smart }
492e399441dSJames Smart 
493e399441dSJames Smart static void
494e399441dSJames Smart nvme_fc_rport_put(struct nvme_fc_rport *rport)
495e399441dSJames Smart {
496e399441dSJames Smart 	kref_put(&rport->ref, nvme_fc_free_rport);
497e399441dSJames Smart }
498e399441dSJames Smart 
499e399441dSJames Smart static int
500e399441dSJames Smart nvme_fc_rport_get(struct nvme_fc_rport *rport)
501e399441dSJames Smart {
502e399441dSJames Smart 	return kref_get_unless_zero(&rport->ref);
503e399441dSJames Smart }
504e399441dSJames Smart 
5058d64daf7SJames Smart static int
5068d64daf7SJames Smart nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
5078d64daf7SJames Smart {
5088d64daf7SJames Smart 	struct nvmefc_ls_req_op *lsop;
5098d64daf7SJames Smart 	unsigned long flags;
5108d64daf7SJames Smart 
5118d64daf7SJames Smart restart:
5128d64daf7SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
5138d64daf7SJames Smart 
5148d64daf7SJames Smart 	list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
5158d64daf7SJames Smart 		if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
5168d64daf7SJames Smart 			lsop->flags |= FCOP_FLAGS_TERMIO;
5178d64daf7SJames Smart 			spin_unlock_irqrestore(&rport->lock, flags);
5188d64daf7SJames Smart 			rport->lport->ops->ls_abort(&rport->lport->localport,
5198d64daf7SJames Smart 						&rport->remoteport,
5208d64daf7SJames Smart 						&lsop->ls_req);
5218d64daf7SJames Smart 			goto restart;
5228d64daf7SJames Smart 		}
5238d64daf7SJames Smart 	}
5248d64daf7SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
5258d64daf7SJames Smart 
5268d64daf7SJames Smart 	return 0;
5278d64daf7SJames Smart }
5288d64daf7SJames Smart 
529e399441dSJames Smart /**
530e399441dSJames Smart  * nvme_fc_unregister_remoteport - transport entry point called by an
531e399441dSJames Smart  *                              LLDD to deregister/remove a previously
532e399441dSJames Smart  *                              registered a NVME subsystem FC port.
533e399441dSJames Smart  * @remoteport: pointer to the (registered) remote port that is to be
534e399441dSJames Smart  *              deregistered.
535e399441dSJames Smart  *
536e399441dSJames Smart  * Returns:
537e399441dSJames Smart  * a completion status. Must be 0 upon success; a negative errno
538e399441dSJames Smart  * (ex: -ENXIO) upon failure.
539e399441dSJames Smart  */
540e399441dSJames Smart int
541e399441dSJames Smart nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
542e399441dSJames Smart {
543e399441dSJames Smart 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
544e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl;
545e399441dSJames Smart 	unsigned long flags;
546e399441dSJames Smart 
547e399441dSJames Smart 	if (!portptr)
548e399441dSJames Smart 		return -EINVAL;
549e399441dSJames Smart 
550e399441dSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
551e399441dSJames Smart 
552e399441dSJames Smart 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
553e399441dSJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
554e399441dSJames Smart 		return -EINVAL;
555e399441dSJames Smart 	}
556e399441dSJames Smart 	portptr->port_state = FC_OBJSTATE_DELETED;
557e399441dSJames Smart 
558e399441dSJames Smart 	/* tear down all associations to the remote port */
559e399441dSJames Smart 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
560e399441dSJames Smart 		__nvme_fc_del_ctrl(ctrl);
561e399441dSJames Smart 
562e399441dSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
563e399441dSJames Smart 
5648d64daf7SJames Smart 	nvme_fc_abort_lsops(rport);
5658d64daf7SJames Smart 
566e399441dSJames Smart 	nvme_fc_rport_put(rport);
567e399441dSJames Smart 	return 0;
568e399441dSJames Smart }
569e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
570e399441dSJames Smart 
571e399441dSJames Smart 
572e399441dSJames Smart /* *********************** FC-NVME DMA Handling **************************** */
573e399441dSJames Smart 
574e399441dSJames Smart /*
575e399441dSJames Smart  * The fcloop device passes in a NULL device pointer. Real LLD's will
576e399441dSJames Smart  * pass in a valid device pointer. If NULL is passed to the dma mapping
577e399441dSJames Smart  * routines, depending on the platform, it may or may not succeed, and
578e399441dSJames Smart  * may crash.
579e399441dSJames Smart  *
580e399441dSJames Smart  * As such:
581e399441dSJames Smart  * Wrapper all the dma routines and check the dev pointer.
582e399441dSJames Smart  *
583e399441dSJames Smart  * If simple mappings (return just a dma address, we'll noop them,
584e399441dSJames Smart  * returning a dma address of 0.
585e399441dSJames Smart  *
586e399441dSJames Smart  * On more complex mappings (dma_map_sg), a pseudo routine fills
587e399441dSJames Smart  * in the scatter list, setting all dma addresses to 0.
588e399441dSJames Smart  */
589e399441dSJames Smart 
590e399441dSJames Smart static inline dma_addr_t
591e399441dSJames Smart fc_dma_map_single(struct device *dev, void *ptr, size_t size,
592e399441dSJames Smart 		enum dma_data_direction dir)
593e399441dSJames Smart {
594e399441dSJames Smart 	return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
595e399441dSJames Smart }
596e399441dSJames Smart 
597e399441dSJames Smart static inline int
598e399441dSJames Smart fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
599e399441dSJames Smart {
600e399441dSJames Smart 	return dev ? dma_mapping_error(dev, dma_addr) : 0;
601e399441dSJames Smart }
602e399441dSJames Smart 
603e399441dSJames Smart static inline void
604e399441dSJames Smart fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
605e399441dSJames Smart 	enum dma_data_direction dir)
606e399441dSJames Smart {
607e399441dSJames Smart 	if (dev)
608e399441dSJames Smart 		dma_unmap_single(dev, addr, size, dir);
609e399441dSJames Smart }
610e399441dSJames Smart 
611e399441dSJames Smart static inline void
612e399441dSJames Smart fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
613e399441dSJames Smart 		enum dma_data_direction dir)
614e399441dSJames Smart {
615e399441dSJames Smart 	if (dev)
616e399441dSJames Smart 		dma_sync_single_for_cpu(dev, addr, size, dir);
617e399441dSJames Smart }
618e399441dSJames Smart 
619e399441dSJames Smart static inline void
620e399441dSJames Smart fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
621e399441dSJames Smart 		enum dma_data_direction dir)
622e399441dSJames Smart {
623e399441dSJames Smart 	if (dev)
624e399441dSJames Smart 		dma_sync_single_for_device(dev, addr, size, dir);
625e399441dSJames Smart }
626e399441dSJames Smart 
627e399441dSJames Smart /* pseudo dma_map_sg call */
628e399441dSJames Smart static int
629e399441dSJames Smart fc_map_sg(struct scatterlist *sg, int nents)
630e399441dSJames Smart {
631e399441dSJames Smart 	struct scatterlist *s;
632e399441dSJames Smart 	int i;
633e399441dSJames Smart 
634e399441dSJames Smart 	WARN_ON(nents == 0 || sg[0].length == 0);
635e399441dSJames Smart 
636e399441dSJames Smart 	for_each_sg(sg, s, nents, i) {
637e399441dSJames Smart 		s->dma_address = 0L;
638e399441dSJames Smart #ifdef CONFIG_NEED_SG_DMA_LENGTH
639e399441dSJames Smart 		s->dma_length = s->length;
640e399441dSJames Smart #endif
641e399441dSJames Smart 	}
642e399441dSJames Smart 	return nents;
643e399441dSJames Smart }
644e399441dSJames Smart 
645e399441dSJames Smart static inline int
646e399441dSJames Smart fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
647e399441dSJames Smart 		enum dma_data_direction dir)
648e399441dSJames Smart {
649e399441dSJames Smart 	return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
650e399441dSJames Smart }
651e399441dSJames Smart 
652e399441dSJames Smart static inline void
653e399441dSJames Smart fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
654e399441dSJames Smart 		enum dma_data_direction dir)
655e399441dSJames Smart {
656e399441dSJames Smart 	if (dev)
657e399441dSJames Smart 		dma_unmap_sg(dev, sg, nents, dir);
658e399441dSJames Smart }
659e399441dSJames Smart 
660e399441dSJames Smart 
661e399441dSJames Smart /* *********************** FC-NVME LS Handling **************************** */
662e399441dSJames Smart 
663e399441dSJames Smart static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
664e399441dSJames Smart static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
665e399441dSJames Smart 
666e399441dSJames Smart 
667e399441dSJames Smart static void
668c913a8b0SJames Smart __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
669e399441dSJames Smart {
670c913a8b0SJames Smart 	struct nvme_fc_rport *rport = lsop->rport;
671e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
672e399441dSJames Smart 	unsigned long flags;
673e399441dSJames Smart 
674c913a8b0SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
675e399441dSJames Smart 
676e399441dSJames Smart 	if (!lsop->req_queued) {
677c913a8b0SJames Smart 		spin_unlock_irqrestore(&rport->lock, flags);
678e399441dSJames Smart 		return;
679e399441dSJames Smart 	}
680e399441dSJames Smart 
681e399441dSJames Smart 	list_del(&lsop->lsreq_list);
682e399441dSJames Smart 
683e399441dSJames Smart 	lsop->req_queued = false;
684e399441dSJames Smart 
685c913a8b0SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
686e399441dSJames Smart 
687c913a8b0SJames Smart 	fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
688e399441dSJames Smart 				  (lsreq->rqstlen + lsreq->rsplen),
689e399441dSJames Smart 				  DMA_BIDIRECTIONAL);
690e399441dSJames Smart 
691c913a8b0SJames Smart 	nvme_fc_rport_put(rport);
692e399441dSJames Smart }
693e399441dSJames Smart 
694e399441dSJames Smart static int
695c913a8b0SJames Smart __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
696e399441dSJames Smart 		struct nvmefc_ls_req_op *lsop,
697e399441dSJames Smart 		void (*done)(struct nvmefc_ls_req *req, int status))
698e399441dSJames Smart {
699e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
700e399441dSJames Smart 	unsigned long flags;
701c913a8b0SJames Smart 	int ret = 0;
702e399441dSJames Smart 
703c913a8b0SJames Smart 	if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
704c913a8b0SJames Smart 		return -ECONNREFUSED;
705c913a8b0SJames Smart 
706c913a8b0SJames Smart 	if (!nvme_fc_rport_get(rport))
707e399441dSJames Smart 		return -ESHUTDOWN;
708e399441dSJames Smart 
709e399441dSJames Smart 	lsreq->done = done;
710c913a8b0SJames Smart 	lsop->rport = rport;
711e399441dSJames Smart 	lsop->req_queued = false;
712e399441dSJames Smart 	INIT_LIST_HEAD(&lsop->lsreq_list);
713e399441dSJames Smart 	init_completion(&lsop->ls_done);
714e399441dSJames Smart 
715c913a8b0SJames Smart 	lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
716e399441dSJames Smart 				  lsreq->rqstlen + lsreq->rsplen,
717e399441dSJames Smart 				  DMA_BIDIRECTIONAL);
718c913a8b0SJames Smart 	if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
719c913a8b0SJames Smart 		ret = -EFAULT;
720c913a8b0SJames Smart 		goto out_putrport;
721e399441dSJames Smart 	}
722e399441dSJames Smart 	lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
723e399441dSJames Smart 
724c913a8b0SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
725e399441dSJames Smart 
726c913a8b0SJames Smart 	list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
727e399441dSJames Smart 
728e399441dSJames Smart 	lsop->req_queued = true;
729e399441dSJames Smart 
730c913a8b0SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
731e399441dSJames Smart 
732c913a8b0SJames Smart 	ret = rport->lport->ops->ls_req(&rport->lport->localport,
733c913a8b0SJames Smart 					&rport->remoteport, lsreq);
734e399441dSJames Smart 	if (ret)
735c913a8b0SJames Smart 		goto out_unlink;
736c913a8b0SJames Smart 
737c913a8b0SJames Smart 	return 0;
738c913a8b0SJames Smart 
739c913a8b0SJames Smart out_unlink:
740e399441dSJames Smart 	lsop->ls_error = ret;
741c913a8b0SJames Smart 	spin_lock_irqsave(&rport->lock, flags);
742c913a8b0SJames Smart 	lsop->req_queued = false;
743c913a8b0SJames Smart 	list_del(&lsop->lsreq_list);
744c913a8b0SJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
745c913a8b0SJames Smart 	fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
746c913a8b0SJames Smart 				  (lsreq->rqstlen + lsreq->rsplen),
747c913a8b0SJames Smart 				  DMA_BIDIRECTIONAL);
748c913a8b0SJames Smart out_putrport:
749c913a8b0SJames Smart 	nvme_fc_rport_put(rport);
750e399441dSJames Smart 
751e399441dSJames Smart 	return ret;
752e399441dSJames Smart }
753e399441dSJames Smart 
754e399441dSJames Smart static void
755e399441dSJames Smart nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
756e399441dSJames Smart {
757e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
758e399441dSJames Smart 
759e399441dSJames Smart 	lsop->ls_error = status;
760e399441dSJames Smart 	complete(&lsop->ls_done);
761e399441dSJames Smart }
762e399441dSJames Smart 
763e399441dSJames Smart static int
764c913a8b0SJames Smart nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
765e399441dSJames Smart {
766e399441dSJames Smart 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
767e399441dSJames Smart 	struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
768e399441dSJames Smart 	int ret;
769e399441dSJames Smart 
770c913a8b0SJames Smart 	ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
771e399441dSJames Smart 
772c913a8b0SJames Smart 	if (!ret) {
773e399441dSJames Smart 		/*
774e399441dSJames Smart 		 * No timeout/not interruptible as we need the struct
775e399441dSJames Smart 		 * to exist until the lldd calls us back. Thus mandate
776e399441dSJames Smart 		 * wait until driver calls back. lldd responsible for
777e399441dSJames Smart 		 * the timeout action
778e399441dSJames Smart 		 */
779e399441dSJames Smart 		wait_for_completion(&lsop->ls_done);
780e399441dSJames Smart 
781c913a8b0SJames Smart 		__nvme_fc_finish_ls_req(lsop);
782e399441dSJames Smart 
783c913a8b0SJames Smart 		ret = lsop->ls_error;
784e399441dSJames Smart 	}
785e399441dSJames Smart 
786c913a8b0SJames Smart 	if (ret)
787c913a8b0SJames Smart 		return ret;
788c913a8b0SJames Smart 
789e399441dSJames Smart 	/* ACC or RJT payload ? */
790e399441dSJames Smart 	if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
791e399441dSJames Smart 		return -ENXIO;
792e399441dSJames Smart 
793e399441dSJames Smart 	return 0;
794e399441dSJames Smart }
795e399441dSJames Smart 
796c913a8b0SJames Smart static int
797c913a8b0SJames Smart nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
798e399441dSJames Smart 		struct nvmefc_ls_req_op *lsop,
799e399441dSJames Smart 		void (*done)(struct nvmefc_ls_req *req, int status))
800e399441dSJames Smart {
801e399441dSJames Smart 	/* don't wait for completion */
802e399441dSJames Smart 
803c913a8b0SJames Smart 	return __nvme_fc_send_ls_req(rport, lsop, done);
804e399441dSJames Smart }
805e399441dSJames Smart 
806e399441dSJames Smart /* Validation Error indexes into the string table below */
807e399441dSJames Smart enum {
808e399441dSJames Smart 	VERR_NO_ERROR		= 0,
809e399441dSJames Smart 	VERR_LSACC		= 1,
810e399441dSJames Smart 	VERR_LSDESC_RQST	= 2,
811e399441dSJames Smart 	VERR_LSDESC_RQST_LEN	= 3,
812e399441dSJames Smart 	VERR_ASSOC_ID		= 4,
813e399441dSJames Smart 	VERR_ASSOC_ID_LEN	= 5,
814e399441dSJames Smart 	VERR_CONN_ID		= 6,
815e399441dSJames Smart 	VERR_CONN_ID_LEN	= 7,
816e399441dSJames Smart 	VERR_CR_ASSOC		= 8,
817e399441dSJames Smart 	VERR_CR_ASSOC_ACC_LEN	= 9,
818e399441dSJames Smart 	VERR_CR_CONN		= 10,
819e399441dSJames Smart 	VERR_CR_CONN_ACC_LEN	= 11,
820e399441dSJames Smart 	VERR_DISCONN		= 12,
821e399441dSJames Smart 	VERR_DISCONN_ACC_LEN	= 13,
822e399441dSJames Smart };
823e399441dSJames Smart 
824e399441dSJames Smart static char *validation_errors[] = {
825e399441dSJames Smart 	"OK",
826e399441dSJames Smart 	"Not LS_ACC",
827e399441dSJames Smart 	"Not LSDESC_RQST",
828e399441dSJames Smart 	"Bad LSDESC_RQST Length",
829e399441dSJames Smart 	"Not Association ID",
830e399441dSJames Smart 	"Bad Association ID Length",
831e399441dSJames Smart 	"Not Connection ID",
832e399441dSJames Smart 	"Bad Connection ID Length",
833e399441dSJames Smart 	"Not CR_ASSOC Rqst",
834e399441dSJames Smart 	"Bad CR_ASSOC ACC Length",
835e399441dSJames Smart 	"Not CR_CONN Rqst",
836e399441dSJames Smart 	"Bad CR_CONN ACC Length",
837e399441dSJames Smart 	"Not Disconnect Rqst",
838e399441dSJames Smart 	"Bad Disconnect ACC Length",
839e399441dSJames Smart };
840e399441dSJames Smart 
841e399441dSJames Smart static int
842e399441dSJames Smart nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
843e399441dSJames Smart 	struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
844e399441dSJames Smart {
845e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
846e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
847e399441dSJames Smart 	struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
848e399441dSJames Smart 	struct fcnvme_ls_cr_assoc_acc *assoc_acc;
849e399441dSJames Smart 	int ret, fcret = 0;
850e399441dSJames Smart 
851e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
852e399441dSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz +
853e399441dSJames Smart 			 sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
854e399441dSJames Smart 	if (!lsop) {
855e399441dSJames Smart 		ret = -ENOMEM;
856e399441dSJames Smart 		goto out_no_memory;
857e399441dSJames Smart 	}
858e399441dSJames Smart 	lsreq = &lsop->ls_req;
859e399441dSJames Smart 
860e399441dSJames Smart 	lsreq->private = (void *)&lsop[1];
861e399441dSJames Smart 	assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
862e399441dSJames Smart 			(lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
863e399441dSJames Smart 	assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
864e399441dSJames Smart 
865e399441dSJames Smart 	assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
866e399441dSJames Smart 	assoc_rqst->desc_list_len =
867e399441dSJames Smart 			cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
868e399441dSJames Smart 
869e399441dSJames Smart 	assoc_rqst->assoc_cmd.desc_tag =
870e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
871e399441dSJames Smart 	assoc_rqst->assoc_cmd.desc_len =
872e399441dSJames Smart 			fcnvme_lsdesc_len(
873e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
874e399441dSJames Smart 
875e399441dSJames Smart 	assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
876e399441dSJames Smart 	assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize);
877e399441dSJames Smart 	/* Linux supports only Dynamic controllers */
878e399441dSJames Smart 	assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
8798e412263SChristoph Hellwig 	uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
880e399441dSJames Smart 	strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
881e399441dSJames Smart 		min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
882e399441dSJames Smart 	strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
883e399441dSJames Smart 		min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
884e399441dSJames Smart 
885e399441dSJames Smart 	lsop->queue = queue;
886e399441dSJames Smart 	lsreq->rqstaddr = assoc_rqst;
887e399441dSJames Smart 	lsreq->rqstlen = sizeof(*assoc_rqst);
888e399441dSJames Smart 	lsreq->rspaddr = assoc_acc;
889e399441dSJames Smart 	lsreq->rsplen = sizeof(*assoc_acc);
890e399441dSJames Smart 	lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
891e399441dSJames Smart 
892c913a8b0SJames Smart 	ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
893e399441dSJames Smart 	if (ret)
894e399441dSJames Smart 		goto out_free_buffer;
895e399441dSJames Smart 
896e399441dSJames Smart 	/* process connect LS completion */
897e399441dSJames Smart 
898e399441dSJames Smart 	/* validate the ACC response */
899e399441dSJames Smart 	if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
900e399441dSJames Smart 		fcret = VERR_LSACC;
901f77fc87cSJames Smart 	else if (assoc_acc->hdr.desc_list_len !=
902e399441dSJames Smart 			fcnvme_lsdesc_len(
903e399441dSJames Smart 				sizeof(struct fcnvme_ls_cr_assoc_acc)))
904e399441dSJames Smart 		fcret = VERR_CR_ASSOC_ACC_LEN;
905f77fc87cSJames Smart 	else if (assoc_acc->hdr.rqst.desc_tag !=
906f77fc87cSJames Smart 			cpu_to_be32(FCNVME_LSDESC_RQST))
907e399441dSJames Smart 		fcret = VERR_LSDESC_RQST;
908e399441dSJames Smart 	else if (assoc_acc->hdr.rqst.desc_len !=
909e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
910e399441dSJames Smart 		fcret = VERR_LSDESC_RQST_LEN;
911e399441dSJames Smart 	else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
912e399441dSJames Smart 		fcret = VERR_CR_ASSOC;
913e399441dSJames Smart 	else if (assoc_acc->associd.desc_tag !=
914e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
915e399441dSJames Smart 		fcret = VERR_ASSOC_ID;
916e399441dSJames Smart 	else if (assoc_acc->associd.desc_len !=
917e399441dSJames Smart 			fcnvme_lsdesc_len(
918e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id)))
919e399441dSJames Smart 		fcret = VERR_ASSOC_ID_LEN;
920e399441dSJames Smart 	else if (assoc_acc->connectid.desc_tag !=
921e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CONN_ID))
922e399441dSJames Smart 		fcret = VERR_CONN_ID;
923e399441dSJames Smart 	else if (assoc_acc->connectid.desc_len !=
924e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
925e399441dSJames Smart 		fcret = VERR_CONN_ID_LEN;
926e399441dSJames Smart 
927e399441dSJames Smart 	if (fcret) {
928e399441dSJames Smart 		ret = -EBADF;
929e399441dSJames Smart 		dev_err(ctrl->dev,
930e399441dSJames Smart 			"q %d connect failed: %s\n",
931e399441dSJames Smart 			queue->qnum, validation_errors[fcret]);
932e399441dSJames Smart 	} else {
933e399441dSJames Smart 		ctrl->association_id =
934e399441dSJames Smart 			be64_to_cpu(assoc_acc->associd.association_id);
935e399441dSJames Smart 		queue->connection_id =
936e399441dSJames Smart 			be64_to_cpu(assoc_acc->connectid.connection_id);
937e399441dSJames Smart 		set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
938e399441dSJames Smart 	}
939e399441dSJames Smart 
940e399441dSJames Smart out_free_buffer:
941e399441dSJames Smart 	kfree(lsop);
942e399441dSJames Smart out_no_memory:
943e399441dSJames Smart 	if (ret)
944e399441dSJames Smart 		dev_err(ctrl->dev,
945e399441dSJames Smart 			"queue %d connect admin queue failed (%d).\n",
946e399441dSJames Smart 			queue->qnum, ret);
947e399441dSJames Smart 	return ret;
948e399441dSJames Smart }
949e399441dSJames Smart 
950e399441dSJames Smart static int
951e399441dSJames Smart nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
952e399441dSJames Smart 			u16 qsize, u16 ersp_ratio)
953e399441dSJames Smart {
954e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
955e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
956e399441dSJames Smart 	struct fcnvme_ls_cr_conn_rqst *conn_rqst;
957e399441dSJames Smart 	struct fcnvme_ls_cr_conn_acc *conn_acc;
958e399441dSJames Smart 	int ret, fcret = 0;
959e399441dSJames Smart 
960e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
961e399441dSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz +
962e399441dSJames Smart 			 sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
963e399441dSJames Smart 	if (!lsop) {
964e399441dSJames Smart 		ret = -ENOMEM;
965e399441dSJames Smart 		goto out_no_memory;
966e399441dSJames Smart 	}
967e399441dSJames Smart 	lsreq = &lsop->ls_req;
968e399441dSJames Smart 
969e399441dSJames Smart 	lsreq->private = (void *)&lsop[1];
970e399441dSJames Smart 	conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
971e399441dSJames Smart 			(lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
972e399441dSJames Smart 	conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
973e399441dSJames Smart 
974e399441dSJames Smart 	conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
975e399441dSJames Smart 	conn_rqst->desc_list_len = cpu_to_be32(
976e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id) +
977e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
978e399441dSJames Smart 
979e399441dSJames Smart 	conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
980e399441dSJames Smart 	conn_rqst->associd.desc_len =
981e399441dSJames Smart 			fcnvme_lsdesc_len(
982e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id));
983e399441dSJames Smart 	conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
984e399441dSJames Smart 	conn_rqst->connect_cmd.desc_tag =
985e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
986e399441dSJames Smart 	conn_rqst->connect_cmd.desc_len =
987e399441dSJames Smart 			fcnvme_lsdesc_len(
988e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
989e399441dSJames Smart 	conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
990e399441dSJames Smart 	conn_rqst->connect_cmd.qid  = cpu_to_be16(queue->qnum);
991e399441dSJames Smart 	conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize);
992e399441dSJames Smart 
993e399441dSJames Smart 	lsop->queue = queue;
994e399441dSJames Smart 	lsreq->rqstaddr = conn_rqst;
995e399441dSJames Smart 	lsreq->rqstlen = sizeof(*conn_rqst);
996e399441dSJames Smart 	lsreq->rspaddr = conn_acc;
997e399441dSJames Smart 	lsreq->rsplen = sizeof(*conn_acc);
998e399441dSJames Smart 	lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
999e399441dSJames Smart 
1000c913a8b0SJames Smart 	ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1001e399441dSJames Smart 	if (ret)
1002e399441dSJames Smart 		goto out_free_buffer;
1003e399441dSJames Smart 
1004e399441dSJames Smart 	/* process connect LS completion */
1005e399441dSJames Smart 
1006e399441dSJames Smart 	/* validate the ACC response */
1007e399441dSJames Smart 	if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1008e399441dSJames Smart 		fcret = VERR_LSACC;
1009f77fc87cSJames Smart 	else if (conn_acc->hdr.desc_list_len !=
1010e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1011e399441dSJames Smart 		fcret = VERR_CR_CONN_ACC_LEN;
1012f77fc87cSJames Smart 	else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1013e399441dSJames Smart 		fcret = VERR_LSDESC_RQST;
1014e399441dSJames Smart 	else if (conn_acc->hdr.rqst.desc_len !=
1015e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1016e399441dSJames Smart 		fcret = VERR_LSDESC_RQST_LEN;
1017e399441dSJames Smart 	else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1018e399441dSJames Smart 		fcret = VERR_CR_CONN;
1019e399441dSJames Smart 	else if (conn_acc->connectid.desc_tag !=
1020e399441dSJames Smart 			cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1021e399441dSJames Smart 		fcret = VERR_CONN_ID;
1022e399441dSJames Smart 	else if (conn_acc->connectid.desc_len !=
1023e399441dSJames Smart 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1024e399441dSJames Smart 		fcret = VERR_CONN_ID_LEN;
1025e399441dSJames Smart 
1026e399441dSJames Smart 	if (fcret) {
1027e399441dSJames Smart 		ret = -EBADF;
1028e399441dSJames Smart 		dev_err(ctrl->dev,
1029e399441dSJames Smart 			"q %d connect failed: %s\n",
1030e399441dSJames Smart 			queue->qnum, validation_errors[fcret]);
1031e399441dSJames Smart 	} else {
1032e399441dSJames Smart 		queue->connection_id =
1033e399441dSJames Smart 			be64_to_cpu(conn_acc->connectid.connection_id);
1034e399441dSJames Smart 		set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1035e399441dSJames Smart 	}
1036e399441dSJames Smart 
1037e399441dSJames Smart out_free_buffer:
1038e399441dSJames Smart 	kfree(lsop);
1039e399441dSJames Smart out_no_memory:
1040e399441dSJames Smart 	if (ret)
1041e399441dSJames Smart 		dev_err(ctrl->dev,
1042e399441dSJames Smart 			"queue %d connect command failed (%d).\n",
1043e399441dSJames Smart 			queue->qnum, ret);
1044e399441dSJames Smart 	return ret;
1045e399441dSJames Smart }
1046e399441dSJames Smart 
1047e399441dSJames Smart static void
1048e399441dSJames Smart nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1049e399441dSJames Smart {
1050e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1051e399441dSJames Smart 
1052c913a8b0SJames Smart 	__nvme_fc_finish_ls_req(lsop);
1053e399441dSJames Smart 
1054e399441dSJames Smart 	/* fc-nvme iniator doesn't care about success or failure of cmd */
1055e399441dSJames Smart 
1056e399441dSJames Smart 	kfree(lsop);
1057e399441dSJames Smart }
1058e399441dSJames Smart 
1059e399441dSJames Smart /*
1060e399441dSJames Smart  * This routine sends a FC-NVME LS to disconnect (aka terminate)
1061e399441dSJames Smart  * the FC-NVME Association.  Terminating the association also
1062e399441dSJames Smart  * terminates the FC-NVME connections (per queue, both admin and io
1063e399441dSJames Smart  * queues) that are part of the association. E.g. things are torn
1064e399441dSJames Smart  * down, and the related FC-NVME Association ID and Connection IDs
1065e399441dSJames Smart  * become invalid.
1066e399441dSJames Smart  *
1067e399441dSJames Smart  * The behavior of the fc-nvme initiator is such that it's
1068e399441dSJames Smart  * understanding of the association and connections will implicitly
1069e399441dSJames Smart  * be torn down. The action is implicit as it may be due to a loss of
1070e399441dSJames Smart  * connectivity with the fc-nvme target, so you may never get a
1071e399441dSJames Smart  * response even if you tried.  As such, the action of this routine
1072e399441dSJames Smart  * is to asynchronously send the LS, ignore any results of the LS, and
1073e399441dSJames Smart  * continue on with terminating the association. If the fc-nvme target
1074e399441dSJames Smart  * is present and receives the LS, it too can tear down.
1075e399441dSJames Smart  */
1076e399441dSJames Smart static void
1077e399441dSJames Smart nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1078e399441dSJames Smart {
1079e399441dSJames Smart 	struct fcnvme_ls_disconnect_rqst *discon_rqst;
1080e399441dSJames Smart 	struct fcnvme_ls_disconnect_acc *discon_acc;
1081e399441dSJames Smart 	struct nvmefc_ls_req_op *lsop;
1082e399441dSJames Smart 	struct nvmefc_ls_req *lsreq;
1083c913a8b0SJames Smart 	int ret;
1084e399441dSJames Smart 
1085e399441dSJames Smart 	lsop = kzalloc((sizeof(*lsop) +
1086e399441dSJames Smart 			 ctrl->lport->ops->lsrqst_priv_sz +
1087e399441dSJames Smart 			 sizeof(*discon_rqst) + sizeof(*discon_acc)),
1088e399441dSJames Smart 			GFP_KERNEL);
1089e399441dSJames Smart 	if (!lsop)
1090e399441dSJames Smart 		/* couldn't sent it... too bad */
1091e399441dSJames Smart 		return;
1092e399441dSJames Smart 
1093e399441dSJames Smart 	lsreq = &lsop->ls_req;
1094e399441dSJames Smart 
1095e399441dSJames Smart 	lsreq->private = (void *)&lsop[1];
1096e399441dSJames Smart 	discon_rqst = (struct fcnvme_ls_disconnect_rqst *)
1097e399441dSJames Smart 			(lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
1098e399441dSJames Smart 	discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1];
1099e399441dSJames Smart 
1100e399441dSJames Smart 	discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT;
1101e399441dSJames Smart 	discon_rqst->desc_list_len = cpu_to_be32(
1102e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id) +
1103e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_disconn_cmd));
1104e399441dSJames Smart 
1105e399441dSJames Smart 	discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1106e399441dSJames Smart 	discon_rqst->associd.desc_len =
1107e399441dSJames Smart 			fcnvme_lsdesc_len(
1108e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_assoc_id));
1109e399441dSJames Smart 
1110e399441dSJames Smart 	discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1111e399441dSJames Smart 
1112e399441dSJames Smart 	discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
1113e399441dSJames Smart 						FCNVME_LSDESC_DISCONN_CMD);
1114e399441dSJames Smart 	discon_rqst->discon_cmd.desc_len =
1115e399441dSJames Smart 			fcnvme_lsdesc_len(
1116e399441dSJames Smart 				sizeof(struct fcnvme_lsdesc_disconn_cmd));
1117e399441dSJames Smart 	discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION;
1118e399441dSJames Smart 	discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id);
1119e399441dSJames Smart 
1120e399441dSJames Smart 	lsreq->rqstaddr = discon_rqst;
1121e399441dSJames Smart 	lsreq->rqstlen = sizeof(*discon_rqst);
1122e399441dSJames Smart 	lsreq->rspaddr = discon_acc;
1123e399441dSJames Smart 	lsreq->rsplen = sizeof(*discon_acc);
1124e399441dSJames Smart 	lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
1125e399441dSJames Smart 
1126c913a8b0SJames Smart 	ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1127c913a8b0SJames Smart 				nvme_fc_disconnect_assoc_done);
1128c913a8b0SJames Smart 	if (ret)
1129c913a8b0SJames Smart 		kfree(lsop);
1130e399441dSJames Smart 
1131e399441dSJames Smart 	/* only meaningful part to terminating the association */
1132e399441dSJames Smart 	ctrl->association_id = 0;
1133e399441dSJames Smart }
1134e399441dSJames Smart 
1135e399441dSJames Smart 
1136e399441dSJames Smart /* *********************** NVME Ctrl Routines **************************** */
1137e399441dSJames Smart 
113878a7ac26SJames Smart static void __nvme_fc_final_op_cleanup(struct request *rq);
1139f874d5d0SJames Smart static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
1140e399441dSJames Smart 
1141e399441dSJames Smart static int
1142e399441dSJames Smart nvme_fc_reinit_request(void *data, struct request *rq)
1143e399441dSJames Smart {
1144e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1145e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1146e399441dSJames Smart 
1147e399441dSJames Smart 	memset(cmdiu, 0, sizeof(*cmdiu));
1148e399441dSJames Smart 	cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1149e399441dSJames Smart 	cmdiu->fc_id = NVME_CMD_FC_ID;
1150e399441dSJames Smart 	cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1151e399441dSJames Smart 	memset(&op->rsp_iu, 0, sizeof(op->rsp_iu));
1152e399441dSJames Smart 
1153e399441dSJames Smart 	return 0;
1154e399441dSJames Smart }
1155e399441dSJames Smart 
1156e399441dSJames Smart static void
1157e399441dSJames Smart __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1158e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
1159e399441dSJames Smart {
1160e399441dSJames Smart 	fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1161e399441dSJames Smart 				sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1162e399441dSJames Smart 	fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1163e399441dSJames Smart 				sizeof(op->cmd_iu), DMA_TO_DEVICE);
1164e399441dSJames Smart 
1165e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_UNINIT);
1166e399441dSJames Smart }
1167e399441dSJames Smart 
1168e399441dSJames Smart static void
1169d6296d39SChristoph Hellwig nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1170d6296d39SChristoph Hellwig 		unsigned int hctx_idx)
1171e399441dSJames Smart {
1172e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1173e399441dSJames Smart 
1174d6296d39SChristoph Hellwig 	return __nvme_fc_exit_request(set->driver_data, op);
1175e399441dSJames Smart }
1176e399441dSJames Smart 
117778a7ac26SJames Smart static int
117878a7ac26SJames Smart __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
117978a7ac26SJames Smart {
118078a7ac26SJames Smart 	int state;
118178a7ac26SJames Smart 
118278a7ac26SJames Smart 	state = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
118378a7ac26SJames Smart 	if (state != FCPOP_STATE_ACTIVE) {
118478a7ac26SJames Smart 		atomic_set(&op->state, state);
118578a7ac26SJames Smart 		return -ECANCELED;
118678a7ac26SJames Smart 	}
118778a7ac26SJames Smart 
118878a7ac26SJames Smart 	ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
118978a7ac26SJames Smart 					&ctrl->rport->remoteport,
119078a7ac26SJames Smart 					op->queue->lldd_handle,
119178a7ac26SJames Smart 					&op->fcp_req);
119278a7ac26SJames Smart 
119378a7ac26SJames Smart 	return 0;
119478a7ac26SJames Smart }
119578a7ac26SJames Smart 
1196e399441dSJames Smart static void
119778a7ac26SJames Smart nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1198e399441dSJames Smart {
1199e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
120078a7ac26SJames Smart 	unsigned long flags;
120178a7ac26SJames Smart 	int i, ret;
1202e399441dSJames Smart 
1203e399441dSJames Smart 	for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
120478a7ac26SJames Smart 		if (atomic_read(&aen_op->state) != FCPOP_STATE_ACTIVE)
1205e399441dSJames Smart 			continue;
120678a7ac26SJames Smart 
120778a7ac26SJames Smart 		spin_lock_irqsave(&ctrl->lock, flags);
120861bff8efSJames Smart 		if (ctrl->flags & FCCTRL_TERMIO) {
120961bff8efSJames Smart 			ctrl->iocnt++;
121078a7ac26SJames Smart 			aen_op->flags |= FCOP_FLAGS_TERMIO;
121161bff8efSJames Smart 		}
121278a7ac26SJames Smart 		spin_unlock_irqrestore(&ctrl->lock, flags);
121378a7ac26SJames Smart 
121478a7ac26SJames Smart 		ret = __nvme_fc_abort_op(ctrl, aen_op);
121578a7ac26SJames Smart 		if (ret) {
121678a7ac26SJames Smart 			/*
121778a7ac26SJames Smart 			 * if __nvme_fc_abort_op failed the io wasn't
121878a7ac26SJames Smart 			 * active. Thus this call path is running in
121978a7ac26SJames Smart 			 * parallel to the io complete. Treat as non-error.
122078a7ac26SJames Smart 			 */
122178a7ac26SJames Smart 
122278a7ac26SJames Smart 			/* back out the flags/counters */
122378a7ac26SJames Smart 			spin_lock_irqsave(&ctrl->lock, flags);
122461bff8efSJames Smart 			if (ctrl->flags & FCCTRL_TERMIO)
122561bff8efSJames Smart 				ctrl->iocnt--;
122678a7ac26SJames Smart 			aen_op->flags &= ~FCOP_FLAGS_TERMIO;
122778a7ac26SJames Smart 			spin_unlock_irqrestore(&ctrl->lock, flags);
122878a7ac26SJames Smart 			return;
1229e399441dSJames Smart 		}
1230e399441dSJames Smart 	}
123178a7ac26SJames Smart }
123278a7ac26SJames Smart 
123378a7ac26SJames Smart static inline int
123478a7ac26SJames Smart __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
123578a7ac26SJames Smart 		struct nvme_fc_fcp_op *op)
123678a7ac26SJames Smart {
123778a7ac26SJames Smart 	unsigned long flags;
123878a7ac26SJames Smart 	bool complete_rq = false;
123978a7ac26SJames Smart 
124078a7ac26SJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
124161bff8efSJames Smart 	if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) {
124236715cf4SJames Smart 		if (ctrl->flags & FCCTRL_TERMIO) {
124336715cf4SJames Smart 			if (!--ctrl->iocnt)
124436715cf4SJames Smart 				wake_up(&ctrl->ioabort_wait);
124536715cf4SJames Smart 		}
124661bff8efSJames Smart 	}
124778a7ac26SJames Smart 	if (op->flags & FCOP_FLAGS_RELEASED)
124878a7ac26SJames Smart 		complete_rq = true;
124978a7ac26SJames Smart 	else
125078a7ac26SJames Smart 		op->flags |= FCOP_FLAGS_COMPLETE;
125178a7ac26SJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
125278a7ac26SJames Smart 
125378a7ac26SJames Smart 	return complete_rq;
125478a7ac26SJames Smart }
1255e399441dSJames Smart 
1256baee29acSChristoph Hellwig static void
1257e399441dSJames Smart nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1258e399441dSJames Smart {
1259e399441dSJames Smart 	struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1260e399441dSJames Smart 	struct request *rq = op->rq;
1261e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
1262e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
1263e399441dSJames Smart 	struct nvme_fc_queue *queue = op->queue;
1264e399441dSJames Smart 	struct nvme_completion *cqe = &op->rsp_iu.cqe;
1265458f280dSJames Smart 	struct nvme_command *sqe = &op->cmd_iu.sqe;
1266d663b69fSChristoph Hellwig 	__le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
126727fa9bc5SChristoph Hellwig 	union nvme_result result;
1268f874d5d0SJames Smart 	bool complete_rq, terminate_assoc = true;
1269e399441dSJames Smart 
1270e399441dSJames Smart 	/*
1271e399441dSJames Smart 	 * WARNING:
1272e399441dSJames Smart 	 * The current linux implementation of a nvme controller
1273e399441dSJames Smart 	 * allocates a single tag set for all io queues and sizes
1274e399441dSJames Smart 	 * the io queues to fully hold all possible tags. Thus, the
1275e399441dSJames Smart 	 * implementation does not reference or care about the sqhd
1276e399441dSJames Smart 	 * value as it never needs to use the sqhd/sqtail pointers
1277e399441dSJames Smart 	 * for submission pacing.
1278e399441dSJames Smart 	 *
1279e399441dSJames Smart 	 * This affects the FC-NVME implementation in two ways:
1280e399441dSJames Smart 	 * 1) As the value doesn't matter, we don't need to waste
1281e399441dSJames Smart 	 *    cycles extracting it from ERSPs and stamping it in the
1282e399441dSJames Smart 	 *    cases where the transport fabricates CQEs on successful
1283e399441dSJames Smart 	 *    completions.
1284e399441dSJames Smart 	 * 2) The FC-NVME implementation requires that delivery of
1285e399441dSJames Smart 	 *    ERSP completions are to go back to the nvme layer in order
1286e399441dSJames Smart 	 *    relative to the rsn, such that the sqhd value will always
1287e399441dSJames Smart 	 *    be "in order" for the nvme layer. As the nvme layer in
1288e399441dSJames Smart 	 *    linux doesn't care about sqhd, there's no need to return
1289e399441dSJames Smart 	 *    them in order.
1290e399441dSJames Smart 	 *
1291e399441dSJames Smart 	 * Additionally:
1292e399441dSJames Smart 	 * As the core nvme layer in linux currently does not look at
1293e399441dSJames Smart 	 * every field in the cqe - in cases where the FC transport must
1294e399441dSJames Smart 	 * fabricate a CQE, the following fields will not be set as they
1295e399441dSJames Smart 	 * are not referenced:
1296e399441dSJames Smart 	 *      cqe.sqid,  cqe.sqhd,  cqe.command_id
1297f874d5d0SJames Smart 	 *
1298f874d5d0SJames Smart 	 * Failure or error of an individual i/o, in a transport
1299f874d5d0SJames Smart 	 * detected fashion unrelated to the nvme completion status,
1300f874d5d0SJames Smart 	 * potentially cause the initiator and target sides to get out
1301f874d5d0SJames Smart 	 * of sync on SQ head/tail (aka outstanding io count allowed).
1302f874d5d0SJames Smart 	 * Per FC-NVME spec, failure of an individual command requires
1303f874d5d0SJames Smart 	 * the connection to be terminated, which in turn requires the
1304f874d5d0SJames Smart 	 * association to be terminated.
1305e399441dSJames Smart 	 */
1306e399441dSJames Smart 
1307e399441dSJames Smart 	fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1308e399441dSJames Smart 				sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1309e399441dSJames Smart 
1310e399441dSJames Smart 	if (atomic_read(&op->state) == FCPOP_STATE_ABORTED)
1311d663b69fSChristoph Hellwig 		status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
131262eeacb0SJames Smart 	else if (freq->status)
1313d663b69fSChristoph Hellwig 		status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
1314e399441dSJames Smart 
1315e399441dSJames Smart 	/*
1316e399441dSJames Smart 	 * For the linux implementation, if we have an unsuccesful
1317e399441dSJames Smart 	 * status, they blk-mq layer can typically be called with the
1318e399441dSJames Smart 	 * non-zero status and the content of the cqe isn't important.
1319e399441dSJames Smart 	 */
1320e399441dSJames Smart 	if (status)
1321e399441dSJames Smart 		goto done;
1322e399441dSJames Smart 
1323e399441dSJames Smart 	/*
1324e399441dSJames Smart 	 * command completed successfully relative to the wire
1325e399441dSJames Smart 	 * protocol. However, validate anything received and
1326e399441dSJames Smart 	 * extract the status and result from the cqe (create it
1327e399441dSJames Smart 	 * where necessary).
1328e399441dSJames Smart 	 */
1329e399441dSJames Smart 
1330e399441dSJames Smart 	switch (freq->rcv_rsplen) {
1331e399441dSJames Smart 
1332e399441dSJames Smart 	case 0:
1333e399441dSJames Smart 	case NVME_FC_SIZEOF_ZEROS_RSP:
1334e399441dSJames Smart 		/*
1335e399441dSJames Smart 		 * No response payload or 12 bytes of payload (which
1336e399441dSJames Smart 		 * should all be zeros) are considered successful and
1337e399441dSJames Smart 		 * no payload in the CQE by the transport.
1338e399441dSJames Smart 		 */
1339e399441dSJames Smart 		if (freq->transferred_length !=
1340e399441dSJames Smart 			be32_to_cpu(op->cmd_iu.data_len)) {
1341d663b69fSChristoph Hellwig 			status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
1342e399441dSJames Smart 			goto done;
1343e399441dSJames Smart 		}
134427fa9bc5SChristoph Hellwig 		result.u64 = 0;
1345e399441dSJames Smart 		break;
1346e399441dSJames Smart 
1347e399441dSJames Smart 	case sizeof(struct nvme_fc_ersp_iu):
1348e399441dSJames Smart 		/*
1349e399441dSJames Smart 		 * The ERSP IU contains a full completion with CQE.
1350e399441dSJames Smart 		 * Validate ERSP IU and look at cqe.
1351e399441dSJames Smart 		 */
1352e399441dSJames Smart 		if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
1353e399441dSJames Smart 					(freq->rcv_rsplen / 4) ||
1354e399441dSJames Smart 			     be32_to_cpu(op->rsp_iu.xfrd_len) !=
1355e399441dSJames Smart 					freq->transferred_length ||
1356726a1080SJames Smart 			     op->rsp_iu.status_code ||
1357458f280dSJames Smart 			     sqe->common.command_id != cqe->command_id)) {
1358d663b69fSChristoph Hellwig 			status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
1359e399441dSJames Smart 			goto done;
1360e399441dSJames Smart 		}
136127fa9bc5SChristoph Hellwig 		result = cqe->result;
1362d663b69fSChristoph Hellwig 		status = cqe->status;
1363e399441dSJames Smart 		break;
1364e399441dSJames Smart 
1365e399441dSJames Smart 	default:
1366d663b69fSChristoph Hellwig 		status = cpu_to_le16(NVME_SC_FC_TRANSPORT_ERROR << 1);
1367e399441dSJames Smart 		goto done;
1368e399441dSJames Smart 	}
1369e399441dSJames Smart 
1370f874d5d0SJames Smart 	terminate_assoc = false;
1371f874d5d0SJames Smart 
1372e399441dSJames Smart done:
137378a7ac26SJames Smart 	if (op->flags & FCOP_FLAGS_AEN) {
137427fa9bc5SChristoph Hellwig 		nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
137578a7ac26SJames Smart 		complete_rq = __nvme_fc_fcpop_chk_teardowns(ctrl, op);
137678a7ac26SJames Smart 		atomic_set(&op->state, FCPOP_STATE_IDLE);
137778a7ac26SJames Smart 		op->flags = FCOP_FLAGS_AEN;	/* clear other flags */
1378e399441dSJames Smart 		nvme_fc_ctrl_put(ctrl);
1379f874d5d0SJames Smart 		goto check_error;
1380e399441dSJames Smart 	}
1381e399441dSJames Smart 
138278a7ac26SJames Smart 	complete_rq = __nvme_fc_fcpop_chk_teardowns(ctrl, op);
138378a7ac26SJames Smart 	if (!complete_rq) {
138478a7ac26SJames Smart 		if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) {
1385e392e1f1SJames Smart 			status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
138678a7ac26SJames Smart 			if (blk_queue_dying(rq->q))
1387e392e1f1SJames Smart 				status |= cpu_to_le16(NVME_SC_DNR << 1);
138878a7ac26SJames Smart 		}
138927fa9bc5SChristoph Hellwig 		nvme_end_request(rq, status, result);
139078a7ac26SJames Smart 	} else
139178a7ac26SJames Smart 		__nvme_fc_final_op_cleanup(rq);
1392f874d5d0SJames Smart 
1393f874d5d0SJames Smart check_error:
1394f874d5d0SJames Smart 	if (terminate_assoc)
1395f874d5d0SJames Smart 		nvme_fc_error_recovery(ctrl, "transport detected io error");
1396e399441dSJames Smart }
1397e399441dSJames Smart 
1398e399441dSJames Smart static int
1399e399441dSJames Smart __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
1400e399441dSJames Smart 		struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
1401e399441dSJames Smart 		struct request *rq, u32 rqno)
1402e399441dSJames Smart {
1403e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1404e399441dSJames Smart 	int ret = 0;
1405e399441dSJames Smart 
1406e399441dSJames Smart 	memset(op, 0, sizeof(*op));
1407e399441dSJames Smart 	op->fcp_req.cmdaddr = &op->cmd_iu;
1408e399441dSJames Smart 	op->fcp_req.cmdlen = sizeof(op->cmd_iu);
1409e399441dSJames Smart 	op->fcp_req.rspaddr = &op->rsp_iu;
1410e399441dSJames Smart 	op->fcp_req.rsplen = sizeof(op->rsp_iu);
1411e399441dSJames Smart 	op->fcp_req.done = nvme_fc_fcpio_done;
1412e399441dSJames Smart 	op->fcp_req.first_sgl = (struct scatterlist *)&op[1];
1413e399441dSJames Smart 	op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE];
1414e399441dSJames Smart 	op->ctrl = ctrl;
1415e399441dSJames Smart 	op->queue = queue;
1416e399441dSJames Smart 	op->rq = rq;
1417e399441dSJames Smart 	op->rqno = rqno;
1418e399441dSJames Smart 
1419e399441dSJames Smart 	cmdiu->scsi_id = NVME_CMD_SCSI_ID;
1420e399441dSJames Smart 	cmdiu->fc_id = NVME_CMD_FC_ID;
1421e399441dSJames Smart 	cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
1422e399441dSJames Smart 
1423e399441dSJames Smart 	op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
1424e399441dSJames Smart 				&op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
1425e399441dSJames Smart 	if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
1426e399441dSJames Smart 		dev_err(ctrl->dev,
1427e399441dSJames Smart 			"FCP Op failed - cmdiu dma mapping failed.\n");
1428e399441dSJames Smart 		ret = EFAULT;
1429e399441dSJames Smart 		goto out_on_error;
1430e399441dSJames Smart 	}
1431e399441dSJames Smart 
1432e399441dSJames Smart 	op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
1433e399441dSJames Smart 				&op->rsp_iu, sizeof(op->rsp_iu),
1434e399441dSJames Smart 				DMA_FROM_DEVICE);
1435e399441dSJames Smart 	if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
1436e399441dSJames Smart 		dev_err(ctrl->dev,
1437e399441dSJames Smart 			"FCP Op failed - rspiu dma mapping failed.\n");
1438e399441dSJames Smart 		ret = EFAULT;
1439e399441dSJames Smart 	}
1440e399441dSJames Smart 
1441e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_IDLE);
1442e399441dSJames Smart out_on_error:
1443e399441dSJames Smart 	return ret;
1444e399441dSJames Smart }
1445e399441dSJames Smart 
1446e399441dSJames Smart static int
1447d6296d39SChristoph Hellwig nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
1448d6296d39SChristoph Hellwig 		unsigned int hctx_idx, unsigned int numa_node)
1449e399441dSJames Smart {
1450d6296d39SChristoph Hellwig 	struct nvme_fc_ctrl *ctrl = set->driver_data;
1451e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
145276f983cbSChristoph Hellwig 	int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
145376f983cbSChristoph Hellwig 	struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
1454e399441dSJames Smart 
1455e399441dSJames Smart 	return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
1456e399441dSJames Smart }
1457e399441dSJames Smart 
1458e399441dSJames Smart static int
1459e399441dSJames Smart nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
1460e399441dSJames Smart {
1461e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op;
1462e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu;
1463e399441dSJames Smart 	struct nvme_command *sqe;
146461bff8efSJames Smart 	void *private;
1465e399441dSJames Smart 	int i, ret;
1466e399441dSJames Smart 
1467e399441dSJames Smart 	aen_op = ctrl->aen_ops;
1468e399441dSJames Smart 	for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
146961bff8efSJames Smart 		private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
147061bff8efSJames Smart 						GFP_KERNEL);
147161bff8efSJames Smart 		if (!private)
147261bff8efSJames Smart 			return -ENOMEM;
147361bff8efSJames Smart 
1474e399441dSJames Smart 		cmdiu = &aen_op->cmd_iu;
1475e399441dSJames Smart 		sqe = &cmdiu->sqe;
1476e399441dSJames Smart 		ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
1477e399441dSJames Smart 				aen_op, (struct request *)NULL,
1478e399441dSJames Smart 				(AEN_CMDID_BASE + i));
147961bff8efSJames Smart 		if (ret) {
148061bff8efSJames Smart 			kfree(private);
1481e399441dSJames Smart 			return ret;
148261bff8efSJames Smart 		}
1483e399441dSJames Smart 
148478a7ac26SJames Smart 		aen_op->flags = FCOP_FLAGS_AEN;
148561bff8efSJames Smart 		aen_op->fcp_req.first_sgl = NULL; /* no sg list */
148661bff8efSJames Smart 		aen_op->fcp_req.private = private;
148778a7ac26SJames Smart 
1488e399441dSJames Smart 		memset(sqe, 0, sizeof(*sqe));
1489e399441dSJames Smart 		sqe->common.opcode = nvme_admin_async_event;
149078a7ac26SJames Smart 		/* Note: core layer may overwrite the sqe.command_id value */
1491e399441dSJames Smart 		sqe->common.command_id = AEN_CMDID_BASE + i;
1492e399441dSJames Smart 	}
1493e399441dSJames Smart 	return 0;
1494e399441dSJames Smart }
1495e399441dSJames Smart 
149661bff8efSJames Smart static void
149761bff8efSJames Smart nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
149861bff8efSJames Smart {
149961bff8efSJames Smart 	struct nvme_fc_fcp_op *aen_op;
150061bff8efSJames Smart 	int i;
150161bff8efSJames Smart 
150261bff8efSJames Smart 	aen_op = ctrl->aen_ops;
150361bff8efSJames Smart 	for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
150461bff8efSJames Smart 		if (!aen_op->fcp_req.private)
150561bff8efSJames Smart 			continue;
150661bff8efSJames Smart 
150761bff8efSJames Smart 		__nvme_fc_exit_request(ctrl, aen_op);
150861bff8efSJames Smart 
150961bff8efSJames Smart 		kfree(aen_op->fcp_req.private);
151061bff8efSJames Smart 		aen_op->fcp_req.private = NULL;
151161bff8efSJames Smart 	}
151261bff8efSJames Smart }
1513e399441dSJames Smart 
1514e399441dSJames Smart static inline void
1515e399441dSJames Smart __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
1516e399441dSJames Smart 		unsigned int qidx)
1517e399441dSJames Smart {
1518e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[qidx];
1519e399441dSJames Smart 
1520e399441dSJames Smart 	hctx->driver_data = queue;
1521e399441dSJames Smart 	queue->hctx = hctx;
1522e399441dSJames Smart }
1523e399441dSJames Smart 
1524e399441dSJames Smart static int
1525e399441dSJames Smart nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1526e399441dSJames Smart 		unsigned int hctx_idx)
1527e399441dSJames Smart {
1528e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = data;
1529e399441dSJames Smart 
1530e399441dSJames Smart 	__nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
1531e399441dSJames Smart 
1532e399441dSJames Smart 	return 0;
1533e399441dSJames Smart }
1534e399441dSJames Smart 
1535e399441dSJames Smart static int
1536e399441dSJames Smart nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
1537e399441dSJames Smart 		unsigned int hctx_idx)
1538e399441dSJames Smart {
1539e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = data;
1540e399441dSJames Smart 
1541e399441dSJames Smart 	__nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
1542e399441dSJames Smart 
1543e399441dSJames Smart 	return 0;
1544e399441dSJames Smart }
1545e399441dSJames Smart 
1546e399441dSJames Smart static void
1547e399441dSJames Smart nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx, size_t queue_size)
1548e399441dSJames Smart {
1549e399441dSJames Smart 	struct nvme_fc_queue *queue;
1550e399441dSJames Smart 
1551e399441dSJames Smart 	queue = &ctrl->queues[idx];
1552e399441dSJames Smart 	memset(queue, 0, sizeof(*queue));
1553e399441dSJames Smart 	queue->ctrl = ctrl;
1554e399441dSJames Smart 	queue->qnum = idx;
1555e399441dSJames Smart 	atomic_set(&queue->csn, 1);
1556e399441dSJames Smart 	queue->dev = ctrl->dev;
1557e399441dSJames Smart 
1558e399441dSJames Smart 	if (idx > 0)
1559e399441dSJames Smart 		queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1560e399441dSJames Smart 	else
1561e399441dSJames Smart 		queue->cmnd_capsule_len = sizeof(struct nvme_command);
1562e399441dSJames Smart 
1563e399441dSJames Smart 	queue->queue_size = queue_size;
1564e399441dSJames Smart 
1565e399441dSJames Smart 	/*
1566e399441dSJames Smart 	 * Considered whether we should allocate buffers for all SQEs
1567e399441dSJames Smart 	 * and CQEs and dma map them - mapping their respective entries
1568e399441dSJames Smart 	 * into the request structures (kernel vm addr and dma address)
1569e399441dSJames Smart 	 * thus the driver could use the buffers/mappings directly.
1570e399441dSJames Smart 	 * It only makes sense if the LLDD would use them for its
1571e399441dSJames Smart 	 * messaging api. It's very unlikely most adapter api's would use
1572e399441dSJames Smart 	 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
1573e399441dSJames Smart 	 * structures were used instead.
1574e399441dSJames Smart 	 */
1575e399441dSJames Smart }
1576e399441dSJames Smart 
1577e399441dSJames Smart /*
1578e399441dSJames Smart  * This routine terminates a queue at the transport level.
1579e399441dSJames Smart  * The transport has already ensured that all outstanding ios on
1580e399441dSJames Smart  * the queue have been terminated.
1581e399441dSJames Smart  * The transport will send a Disconnect LS request to terminate
1582e399441dSJames Smart  * the queue's connection. Termination of the admin queue will also
1583e399441dSJames Smart  * terminate the association at the target.
1584e399441dSJames Smart  */
1585e399441dSJames Smart static void
1586e399441dSJames Smart nvme_fc_free_queue(struct nvme_fc_queue *queue)
1587e399441dSJames Smart {
1588e399441dSJames Smart 	if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
1589e399441dSJames Smart 		return;
1590e399441dSJames Smart 
1591e399441dSJames Smart 	/*
1592e399441dSJames Smart 	 * Current implementation never disconnects a single queue.
1593e399441dSJames Smart 	 * It always terminates a whole association. So there is never
1594e399441dSJames Smart 	 * a disconnect(queue) LS sent to the target.
1595e399441dSJames Smart 	 */
1596e399441dSJames Smart 
1597e399441dSJames Smart 	queue->connection_id = 0;
1598e399441dSJames Smart 	clear_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1599e399441dSJames Smart }
1600e399441dSJames Smart 
1601e399441dSJames Smart static void
1602e399441dSJames Smart __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
1603e399441dSJames Smart 	struct nvme_fc_queue *queue, unsigned int qidx)
1604e399441dSJames Smart {
1605e399441dSJames Smart 	if (ctrl->lport->ops->delete_queue)
1606e399441dSJames Smart 		ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
1607e399441dSJames Smart 				queue->lldd_handle);
1608e399441dSJames Smart 	queue->lldd_handle = NULL;
1609e399441dSJames Smart }
1610e399441dSJames Smart 
1611e399441dSJames Smart static void
1612e399441dSJames Smart nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
1613e399441dSJames Smart {
1614e399441dSJames Smart 	int i;
1615e399441dSJames Smart 
1616d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
1617e399441dSJames Smart 		nvme_fc_free_queue(&ctrl->queues[i]);
1618e399441dSJames Smart }
1619e399441dSJames Smart 
1620e399441dSJames Smart static int
1621e399441dSJames Smart __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
1622e399441dSJames Smart 	struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
1623e399441dSJames Smart {
1624e399441dSJames Smart 	int ret = 0;
1625e399441dSJames Smart 
1626e399441dSJames Smart 	queue->lldd_handle = NULL;
1627e399441dSJames Smart 	if (ctrl->lport->ops->create_queue)
1628e399441dSJames Smart 		ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
1629e399441dSJames Smart 				qidx, qsize, &queue->lldd_handle);
1630e399441dSJames Smart 
1631e399441dSJames Smart 	return ret;
1632e399441dSJames Smart }
1633e399441dSJames Smart 
1634e399441dSJames Smart static void
1635e399441dSJames Smart nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
1636e399441dSJames Smart {
1637d858e5f0SSagi Grimberg 	struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
1638e399441dSJames Smart 	int i;
1639e399441dSJames Smart 
1640d858e5f0SSagi Grimberg 	for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
1641e399441dSJames Smart 		__nvme_fc_delete_hw_queue(ctrl, queue, i);
1642e399441dSJames Smart }
1643e399441dSJames Smart 
1644e399441dSJames Smart static int
1645e399441dSJames Smart nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1646e399441dSJames Smart {
1647e399441dSJames Smart 	struct nvme_fc_queue *queue = &ctrl->queues[1];
164817a1ec08SJohannes Thumshirn 	int i, ret;
1649e399441dSJames Smart 
1650d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
1651e399441dSJames Smart 		ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
165217a1ec08SJohannes Thumshirn 		if (ret)
165317a1ec08SJohannes Thumshirn 			goto delete_queues;
1654e399441dSJames Smart 	}
1655e399441dSJames Smart 
1656e399441dSJames Smart 	return 0;
165717a1ec08SJohannes Thumshirn 
165817a1ec08SJohannes Thumshirn delete_queues:
165917a1ec08SJohannes Thumshirn 	for (; i >= 0; i--)
166017a1ec08SJohannes Thumshirn 		__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
166117a1ec08SJohannes Thumshirn 	return ret;
1662e399441dSJames Smart }
1663e399441dSJames Smart 
1664e399441dSJames Smart static int
1665e399441dSJames Smart nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
1666e399441dSJames Smart {
1667e399441dSJames Smart 	int i, ret = 0;
1668e399441dSJames Smart 
1669d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
1670e399441dSJames Smart 		ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
1671e399441dSJames Smart 					(qsize / 5));
1672e399441dSJames Smart 		if (ret)
1673e399441dSJames Smart 			break;
1674e399441dSJames Smart 		ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
1675e399441dSJames Smart 		if (ret)
1676e399441dSJames Smart 			break;
1677e399441dSJames Smart 	}
1678e399441dSJames Smart 
1679e399441dSJames Smart 	return ret;
1680e399441dSJames Smart }
1681e399441dSJames Smart 
1682e399441dSJames Smart static void
1683e399441dSJames Smart nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
1684e399441dSJames Smart {
1685e399441dSJames Smart 	int i;
1686e399441dSJames Smart 
1687d858e5f0SSagi Grimberg 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
1688e399441dSJames Smart 		nvme_fc_init_queue(ctrl, i, ctrl->ctrl.sqsize);
1689e399441dSJames Smart }
1690e399441dSJames Smart 
1691e399441dSJames Smart static void
1692e399441dSJames Smart nvme_fc_ctrl_free(struct kref *ref)
1693e399441dSJames Smart {
1694e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl =
1695e399441dSJames Smart 		container_of(ref, struct nvme_fc_ctrl, ref);
1696e399441dSJames Smart 	unsigned long flags;
1697e399441dSJames Smart 
169861bff8efSJames Smart 	if (ctrl->ctrl.tagset) {
169961bff8efSJames Smart 		blk_cleanup_queue(ctrl->ctrl.connect_q);
170061bff8efSJames Smart 		blk_mq_free_tag_set(&ctrl->tag_set);
170161bff8efSJames Smart 	}
170261bff8efSJames Smart 
1703e399441dSJames Smart 	/* remove from rport list */
1704e399441dSJames Smart 	spin_lock_irqsave(&ctrl->rport->lock, flags);
1705e399441dSJames Smart 	list_del(&ctrl->ctrl_list);
1706e399441dSJames Smart 	spin_unlock_irqrestore(&ctrl->rport->lock, flags);
170761bff8efSJames Smart 
170861bff8efSJames Smart 	blk_cleanup_queue(ctrl->ctrl.admin_q);
170961bff8efSJames Smart 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
171061bff8efSJames Smart 
171161bff8efSJames Smart 	kfree(ctrl->queues);
1712e399441dSJames Smart 
1713e399441dSJames Smart 	put_device(ctrl->dev);
1714e399441dSJames Smart 	nvme_fc_rport_put(ctrl->rport);
1715e399441dSJames Smart 
1716e399441dSJames Smart 	ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
1717de41447aSEwan D. Milne 	if (ctrl->ctrl.opts)
1718e399441dSJames Smart 		nvmf_free_options(ctrl->ctrl.opts);
1719e399441dSJames Smart 	kfree(ctrl);
1720e399441dSJames Smart }
1721e399441dSJames Smart 
1722e399441dSJames Smart static void
1723e399441dSJames Smart nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
1724e399441dSJames Smart {
1725e399441dSJames Smart 	kref_put(&ctrl->ref, nvme_fc_ctrl_free);
1726e399441dSJames Smart }
1727e399441dSJames Smart 
1728e399441dSJames Smart static int
1729e399441dSJames Smart nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
1730e399441dSJames Smart {
1731e399441dSJames Smart 	return kref_get_unless_zero(&ctrl->ref);
1732e399441dSJames Smart }
1733e399441dSJames Smart 
1734e399441dSJames Smart /*
1735e399441dSJames Smart  * All accesses from nvme core layer done - can now free the
1736e399441dSJames Smart  * controller. Called after last nvme_put_ctrl() call
1737e399441dSJames Smart  */
1738e399441dSJames Smart static void
173961bff8efSJames Smart nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl)
1740e399441dSJames Smart {
1741e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
1742e399441dSJames Smart 
1743e399441dSJames Smart 	WARN_ON(nctrl != &ctrl->ctrl);
1744e399441dSJames Smart 
1745e399441dSJames Smart 	nvme_fc_ctrl_put(ctrl);
1746e399441dSJames Smart }
1747e399441dSJames Smart 
174861bff8efSJames Smart static void
174961bff8efSJames Smart nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
175061bff8efSJames Smart {
175169fa9646SJames Smart 	/* only proceed if in LIVE state - e.g. on first error */
175269fa9646SJames Smart 	if (ctrl->ctrl.state != NVME_CTRL_LIVE)
175369fa9646SJames Smart 		return;
175469fa9646SJames Smart 
175561bff8efSJames Smart 	dev_warn(ctrl->ctrl.device,
175661bff8efSJames Smart 		"NVME-FC{%d}: transport association error detected: %s\n",
175761bff8efSJames Smart 		ctrl->cnum, errmsg);
1758589ff775SJames Smart 	dev_warn(ctrl->ctrl.device,
175961bff8efSJames Smart 		"NVME-FC{%d}: resetting controller\n", ctrl->cnum);
176061bff8efSJames Smart 
176161bff8efSJames Smart 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) {
176261bff8efSJames Smart 		dev_err(ctrl->ctrl.device,
176361bff8efSJames Smart 			"NVME-FC{%d}: error_recovery: Couldn't change state "
176461bff8efSJames Smart 			"to RECONNECTING\n", ctrl->cnum);
176561bff8efSJames Smart 		return;
176661bff8efSJames Smart 	}
176761bff8efSJames Smart 
1768d86c4d8eSChristoph Hellwig 	nvme_reset_ctrl(&ctrl->ctrl);
176961bff8efSJames Smart }
177061bff8efSJames Smart 
1771baee29acSChristoph Hellwig static enum blk_eh_timer_return
1772e399441dSJames Smart nvme_fc_timeout(struct request *rq, bool reserved)
1773e399441dSJames Smart {
1774e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1775e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
1776e399441dSJames Smart 	int ret;
1777e399441dSJames Smart 
1778e399441dSJames Smart 	if (reserved)
1779e399441dSJames Smart 		return BLK_EH_RESET_TIMER;
1780e399441dSJames Smart 
1781e399441dSJames Smart 	ret = __nvme_fc_abort_op(ctrl, op);
1782e399441dSJames Smart 	if (ret)
1783e399441dSJames Smart 		/* io wasn't active to abort consider it done */
1784e399441dSJames Smart 		return BLK_EH_HANDLED;
1785e399441dSJames Smart 
1786e399441dSJames Smart 	/*
178761bff8efSJames Smart 	 * we can't individually ABTS an io without affecting the queue,
178861bff8efSJames Smart 	 * thus killing the queue, adn thus the association.
178961bff8efSJames Smart 	 * So resolve by performing a controller reset, which will stop
179061bff8efSJames Smart 	 * the host/io stack, terminate the association on the link,
179161bff8efSJames Smart 	 * and recreate an association on the link.
1792e399441dSJames Smart 	 */
179361bff8efSJames Smart 	nvme_fc_error_recovery(ctrl, "io timeout error");
1794e399441dSJames Smart 
1795e399441dSJames Smart 	return BLK_EH_HANDLED;
1796e399441dSJames Smart }
1797e399441dSJames Smart 
1798e399441dSJames Smart static int
1799e399441dSJames Smart nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1800e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
1801e399441dSJames Smart {
1802e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
1803e399441dSJames Smart 	enum dma_data_direction dir;
1804e399441dSJames Smart 	int ret;
1805e399441dSJames Smart 
1806e399441dSJames Smart 	freq->sg_cnt = 0;
1807e399441dSJames Smart 
1808b131c61dSChristoph Hellwig 	if (!blk_rq_payload_bytes(rq))
1809e399441dSJames Smart 		return 0;
1810e399441dSJames Smart 
1811e399441dSJames Smart 	freq->sg_table.sgl = freq->first_sgl;
181219e420bbSChristoph Hellwig 	ret = sg_alloc_table_chained(&freq->sg_table,
181319e420bbSChristoph Hellwig 			blk_rq_nr_phys_segments(rq), freq->sg_table.sgl);
1814e399441dSJames Smart 	if (ret)
1815e399441dSJames Smart 		return -ENOMEM;
1816e399441dSJames Smart 
1817e399441dSJames Smart 	op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
181819e420bbSChristoph Hellwig 	WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
1819e399441dSJames Smart 	dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1820e399441dSJames Smart 	freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
1821e399441dSJames Smart 				op->nents, dir);
1822e399441dSJames Smart 	if (unlikely(freq->sg_cnt <= 0)) {
1823e399441dSJames Smart 		sg_free_table_chained(&freq->sg_table, true);
1824e399441dSJames Smart 		freq->sg_cnt = 0;
1825e399441dSJames Smart 		return -EFAULT;
1826e399441dSJames Smart 	}
1827e399441dSJames Smart 
1828e399441dSJames Smart 	/*
1829e399441dSJames Smart 	 * TODO: blk_integrity_rq(rq)  for DIF
1830e399441dSJames Smart 	 */
1831e399441dSJames Smart 	return 0;
1832e399441dSJames Smart }
1833e399441dSJames Smart 
1834e399441dSJames Smart static void
1835e399441dSJames Smart nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
1836e399441dSJames Smart 		struct nvme_fc_fcp_op *op)
1837e399441dSJames Smart {
1838e399441dSJames Smart 	struct nvmefc_fcp_req *freq = &op->fcp_req;
1839e399441dSJames Smart 
1840e399441dSJames Smart 	if (!freq->sg_cnt)
1841e399441dSJames Smart 		return;
1842e399441dSJames Smart 
1843e399441dSJames Smart 	fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
1844e399441dSJames Smart 				((rq_data_dir(rq) == WRITE) ?
1845e399441dSJames Smart 					DMA_TO_DEVICE : DMA_FROM_DEVICE));
1846e399441dSJames Smart 
1847e399441dSJames Smart 	nvme_cleanup_cmd(rq);
1848e399441dSJames Smart 
1849e399441dSJames Smart 	sg_free_table_chained(&freq->sg_table, true);
1850e399441dSJames Smart 
1851e399441dSJames Smart 	freq->sg_cnt = 0;
1852e399441dSJames Smart }
1853e399441dSJames Smart 
1854e399441dSJames Smart /*
1855e399441dSJames Smart  * In FC, the queue is a logical thing. At transport connect, the target
1856e399441dSJames Smart  * creates its "queue" and returns a handle that is to be given to the
1857e399441dSJames Smart  * target whenever it posts something to the corresponding SQ.  When an
1858e399441dSJames Smart  * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
1859e399441dSJames Smart  * command contained within the SQE, an io, and assigns a FC exchange
1860e399441dSJames Smart  * to it. The SQE and the associated SQ handle are sent in the initial
1861e399441dSJames Smart  * CMD IU sents on the exchange. All transfers relative to the io occur
1862e399441dSJames Smart  * as part of the exchange.  The CQE is the last thing for the io,
1863e399441dSJames Smart  * which is transferred (explicitly or implicitly) with the RSP IU
1864e399441dSJames Smart  * sent on the exchange. After the CQE is received, the FC exchange is
1865e399441dSJames Smart  * terminaed and the Exchange may be used on a different io.
1866e399441dSJames Smart  *
1867e399441dSJames Smart  * The transport to LLDD api has the transport making a request for a
1868e399441dSJames Smart  * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
1869e399441dSJames Smart  * resource and transfers the command. The LLDD will then process all
1870e399441dSJames Smart  * steps to complete the io. Upon completion, the transport done routine
1871e399441dSJames Smart  * is called.
1872e399441dSJames Smart  *
1873e399441dSJames Smart  * So - while the operation is outstanding to the LLDD, there is a link
1874e399441dSJames Smart  * level FC exchange resource that is also outstanding. This must be
1875e399441dSJames Smart  * considered in all cleanup operations.
1876e399441dSJames Smart  */
1877fc17b653SChristoph Hellwig static blk_status_t
1878e399441dSJames Smart nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1879e399441dSJames Smart 	struct nvme_fc_fcp_op *op, u32 data_len,
1880e399441dSJames Smart 	enum nvmefc_fcp_datadir	io_dir)
1881e399441dSJames Smart {
1882e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1883e399441dSJames Smart 	struct nvme_command *sqe = &cmdiu->sqe;
1884e399441dSJames Smart 	u32 csn;
1885e399441dSJames Smart 	int ret;
1886e399441dSJames Smart 
188761bff8efSJames Smart 	/*
188861bff8efSJames Smart 	 * before attempting to send the io, check to see if we believe
188961bff8efSJames Smart 	 * the target device is present
189061bff8efSJames Smart 	 */
189161bff8efSJames Smart 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1892fc17b653SChristoph Hellwig 		return BLK_STS_IOERR;
189361bff8efSJames Smart 
1894e399441dSJames Smart 	if (!nvme_fc_ctrl_get(ctrl))
1895fc17b653SChristoph Hellwig 		return BLK_STS_IOERR;
1896e399441dSJames Smart 
1897e399441dSJames Smart 	/* format the FC-NVME CMD IU and fcp_req */
1898e399441dSJames Smart 	cmdiu->connection_id = cpu_to_be64(queue->connection_id);
1899e399441dSJames Smart 	csn = atomic_inc_return(&queue->csn);
1900e399441dSJames Smart 	cmdiu->csn = cpu_to_be32(csn);
1901e399441dSJames Smart 	cmdiu->data_len = cpu_to_be32(data_len);
1902e399441dSJames Smart 	switch (io_dir) {
1903e399441dSJames Smart 	case NVMEFC_FCP_WRITE:
1904e399441dSJames Smart 		cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
1905e399441dSJames Smart 		break;
1906e399441dSJames Smart 	case NVMEFC_FCP_READ:
1907e399441dSJames Smart 		cmdiu->flags = FCNVME_CMD_FLAGS_READ;
1908e399441dSJames Smart 		break;
1909e399441dSJames Smart 	case NVMEFC_FCP_NODATA:
1910e399441dSJames Smart 		cmdiu->flags = 0;
1911e399441dSJames Smart 		break;
1912e399441dSJames Smart 	}
1913e399441dSJames Smart 	op->fcp_req.payload_length = data_len;
1914e399441dSJames Smart 	op->fcp_req.io_dir = io_dir;
1915e399441dSJames Smart 	op->fcp_req.transferred_length = 0;
1916e399441dSJames Smart 	op->fcp_req.rcv_rsplen = 0;
191762eeacb0SJames Smart 	op->fcp_req.status = NVME_SC_SUCCESS;
1918e399441dSJames Smart 	op->fcp_req.sqid = cpu_to_le16(queue->qnum);
1919e399441dSJames Smart 
1920e399441dSJames Smart 	/*
1921e399441dSJames Smart 	 * validate per fabric rules, set fields mandated by fabric spec
1922e399441dSJames Smart 	 * as well as those by FC-NVME spec.
1923e399441dSJames Smart 	 */
1924e399441dSJames Smart 	WARN_ON_ONCE(sqe->common.metadata);
1925e399441dSJames Smart 	WARN_ON_ONCE(sqe->common.dptr.prp1);
1926e399441dSJames Smart 	WARN_ON_ONCE(sqe->common.dptr.prp2);
1927e399441dSJames Smart 	sqe->common.flags |= NVME_CMD_SGL_METABUF;
1928e399441dSJames Smart 
1929e399441dSJames Smart 	/*
1930e399441dSJames Smart 	 * format SQE DPTR field per FC-NVME rules
1931e399441dSJames Smart 	 *    type=data block descr; subtype=offset;
1932e399441dSJames Smart 	 *    offset is currently 0.
1933e399441dSJames Smart 	 */
1934e399441dSJames Smart 	sqe->rw.dptr.sgl.type = NVME_SGL_FMT_OFFSET;
1935e399441dSJames Smart 	sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
1936e399441dSJames Smart 	sqe->rw.dptr.sgl.addr = 0;
1937e399441dSJames Smart 
193878a7ac26SJames Smart 	if (!(op->flags & FCOP_FLAGS_AEN)) {
1939e399441dSJames Smart 		ret = nvme_fc_map_data(ctrl, op->rq, op);
1940e399441dSJames Smart 		if (ret < 0) {
1941e399441dSJames Smart 			nvme_cleanup_cmd(op->rq);
1942e399441dSJames Smart 			nvme_fc_ctrl_put(ctrl);
1943fc17b653SChristoph Hellwig 			if (ret == -ENOMEM || ret == -EAGAIN)
1944fc17b653SChristoph Hellwig 				return BLK_STS_RESOURCE;
1945fc17b653SChristoph Hellwig 			return BLK_STS_IOERR;
1946e399441dSJames Smart 		}
1947e399441dSJames Smart 	}
1948e399441dSJames Smart 
1949e399441dSJames Smart 	fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
1950e399441dSJames Smart 				  sizeof(op->cmd_iu), DMA_TO_DEVICE);
1951e399441dSJames Smart 
1952e399441dSJames Smart 	atomic_set(&op->state, FCPOP_STATE_ACTIVE);
1953e399441dSJames Smart 
195478a7ac26SJames Smart 	if (!(op->flags & FCOP_FLAGS_AEN))
1955e399441dSJames Smart 		blk_mq_start_request(op->rq);
1956e399441dSJames Smart 
1957e399441dSJames Smart 	ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
1958e399441dSJames Smart 					&ctrl->rport->remoteport,
1959e399441dSJames Smart 					queue->lldd_handle, &op->fcp_req);
1960e399441dSJames Smart 
1961e399441dSJames Smart 	if (ret) {
1962b4dfd6eeSJames Smart 		if (op->rq)			/* normal request */
1963e399441dSJames Smart 			nvme_fc_unmap_data(ctrl, op->rq, op);
1964e399441dSJames Smart 		/* else - aen. no cleanup needed */
1965e399441dSJames Smart 
1966e399441dSJames Smart 		nvme_fc_ctrl_put(ctrl);
1967e399441dSJames Smart 
1968e399441dSJames Smart 		if (ret != -EBUSY)
1969fc17b653SChristoph Hellwig 			return BLK_STS_IOERR;
1970e399441dSJames Smart 
1971e399441dSJames Smart 		if (op->rq) {
1972e399441dSJames Smart 			blk_mq_stop_hw_queues(op->rq->q);
1973e399441dSJames Smart 			blk_mq_delay_queue(queue->hctx, NVMEFC_QUEUE_DELAY);
1974e399441dSJames Smart 		}
1975fc17b653SChristoph Hellwig 		return BLK_STS_RESOURCE;
1976e399441dSJames Smart 	}
1977e399441dSJames Smart 
1978fc17b653SChristoph Hellwig 	return BLK_STS_OK;
1979e399441dSJames Smart }
1980e399441dSJames Smart 
1981fc17b653SChristoph Hellwig static blk_status_t
1982e399441dSJames Smart nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
1983e399441dSJames Smart 			const struct blk_mq_queue_data *bd)
1984e399441dSJames Smart {
1985e399441dSJames Smart 	struct nvme_ns *ns = hctx->queue->queuedata;
1986e399441dSJames Smart 	struct nvme_fc_queue *queue = hctx->driver_data;
1987e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = queue->ctrl;
1988e399441dSJames Smart 	struct request *rq = bd->rq;
1989e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1990e399441dSJames Smart 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
1991e399441dSJames Smart 	struct nvme_command *sqe = &cmdiu->sqe;
1992e399441dSJames Smart 	enum nvmefc_fcp_datadir	io_dir;
1993e399441dSJames Smart 	u32 data_len;
1994fc17b653SChristoph Hellwig 	blk_status_t ret;
1995e399441dSJames Smart 
1996e399441dSJames Smart 	ret = nvme_setup_cmd(ns, rq, sqe);
1997e399441dSJames Smart 	if (ret)
1998e399441dSJames Smart 		return ret;
1999e399441dSJames Smart 
2000b131c61dSChristoph Hellwig 	data_len = blk_rq_payload_bytes(rq);
2001e399441dSJames Smart 	if (data_len)
2002e399441dSJames Smart 		io_dir = ((rq_data_dir(rq) == WRITE) ?
2003e399441dSJames Smart 					NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2004e399441dSJames Smart 	else
2005e399441dSJames Smart 		io_dir = NVMEFC_FCP_NODATA;
2006e399441dSJames Smart 
2007e399441dSJames Smart 	return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2008e399441dSJames Smart }
2009e399441dSJames Smart 
2010e399441dSJames Smart static struct blk_mq_tags *
2011e399441dSJames Smart nvme_fc_tagset(struct nvme_fc_queue *queue)
2012e399441dSJames Smart {
2013e399441dSJames Smart 	if (queue->qnum == 0)
2014e399441dSJames Smart 		return queue->ctrl->admin_tag_set.tags[queue->qnum];
2015e399441dSJames Smart 
2016e399441dSJames Smart 	return queue->ctrl->tag_set.tags[queue->qnum - 1];
2017e399441dSJames Smart }
2018e399441dSJames Smart 
2019e399441dSJames Smart static int
2020e399441dSJames Smart nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
2021e399441dSJames Smart 
2022e399441dSJames Smart {
2023e399441dSJames Smart 	struct nvme_fc_queue *queue = hctx->driver_data;
2024e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = queue->ctrl;
2025e399441dSJames Smart 	struct request *req;
2026e399441dSJames Smart 	struct nvme_fc_fcp_op *op;
2027e399441dSJames Smart 
2028e399441dSJames Smart 	req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag);
202961bff8efSJames Smart 	if (!req)
2030e399441dSJames Smart 		return 0;
2031e399441dSJames Smart 
2032e399441dSJames Smart 	op = blk_mq_rq_to_pdu(req);
2033e399441dSJames Smart 
2034e399441dSJames Smart 	if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) &&
2035e399441dSJames Smart 		 (ctrl->lport->ops->poll_queue))
2036e399441dSJames Smart 		ctrl->lport->ops->poll_queue(&ctrl->lport->localport,
2037e399441dSJames Smart 						 queue->lldd_handle);
2038e399441dSJames Smart 
2039e399441dSJames Smart 	return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE));
2040e399441dSJames Smart }
2041e399441dSJames Smart 
2042e399441dSJames Smart static void
2043e399441dSJames Smart nvme_fc_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
2044e399441dSJames Smart {
2045e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2046e399441dSJames Smart 	struct nvme_fc_fcp_op *aen_op;
204761bff8efSJames Smart 	unsigned long flags;
204861bff8efSJames Smart 	bool terminating = false;
2049fc17b653SChristoph Hellwig 	blk_status_t ret;
2050e399441dSJames Smart 
2051e399441dSJames Smart 	if (aer_idx > NVME_FC_NR_AEN_COMMANDS)
2052e399441dSJames Smart 		return;
2053e399441dSJames Smart 
205461bff8efSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
205561bff8efSJames Smart 	if (ctrl->flags & FCCTRL_TERMIO)
205661bff8efSJames Smart 		terminating = true;
205761bff8efSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
205861bff8efSJames Smart 
205961bff8efSJames Smart 	if (terminating)
206061bff8efSJames Smart 		return;
206161bff8efSJames Smart 
2062e399441dSJames Smart 	aen_op = &ctrl->aen_ops[aer_idx];
2063e399441dSJames Smart 
2064e399441dSJames Smart 	ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2065e399441dSJames Smart 					NVMEFC_FCP_NODATA);
2066e399441dSJames Smart 	if (ret)
2067e399441dSJames Smart 		dev_err(ctrl->ctrl.device,
2068e399441dSJames Smart 			"failed async event work [%d]\n", aer_idx);
2069e399441dSJames Smart }
2070e399441dSJames Smart 
2071e399441dSJames Smart static void
207278a7ac26SJames Smart __nvme_fc_final_op_cleanup(struct request *rq)
2073e399441dSJames Smart {
2074e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2075e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
2076e399441dSJames Smart 
207778a7ac26SJames Smart 	atomic_set(&op->state, FCPOP_STATE_IDLE);
207878a7ac26SJames Smart 	op->flags &= ~(FCOP_FLAGS_TERMIO | FCOP_FLAGS_RELEASED |
207978a7ac26SJames Smart 			FCOP_FLAGS_COMPLETE);
2080e399441dSJames Smart 
2081e399441dSJames Smart 	nvme_fc_unmap_data(ctrl, rq, op);
208277f02a7aSChristoph Hellwig 	nvme_complete_rq(rq);
2083e399441dSJames Smart 	nvme_fc_ctrl_put(ctrl);
2084e399441dSJames Smart 
2085e399441dSJames Smart }
2086e399441dSJames Smart 
208778a7ac26SJames Smart static void
208878a7ac26SJames Smart nvme_fc_complete_rq(struct request *rq)
208978a7ac26SJames Smart {
209078a7ac26SJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
209178a7ac26SJames Smart 	struct nvme_fc_ctrl *ctrl = op->ctrl;
209278a7ac26SJames Smart 	unsigned long flags;
209378a7ac26SJames Smart 	bool completed = false;
209478a7ac26SJames Smart 
209578a7ac26SJames Smart 	/*
209678a7ac26SJames Smart 	 * the core layer, on controller resets after calling
209778a7ac26SJames Smart 	 * nvme_shutdown_ctrl(), calls complete_rq without our
209878a7ac26SJames Smart 	 * calling blk_mq_complete_request(), thus there may still
209978a7ac26SJames Smart 	 * be live i/o outstanding with the LLDD. Means transport has
210078a7ac26SJames Smart 	 * to track complete calls vs fcpio_done calls to know what
210178a7ac26SJames Smart 	 * path to take on completes and dones.
210278a7ac26SJames Smart 	 */
210378a7ac26SJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
210478a7ac26SJames Smart 	if (op->flags & FCOP_FLAGS_COMPLETE)
210578a7ac26SJames Smart 		completed = true;
210678a7ac26SJames Smart 	else
210778a7ac26SJames Smart 		op->flags |= FCOP_FLAGS_RELEASED;
210878a7ac26SJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
210978a7ac26SJames Smart 
211078a7ac26SJames Smart 	if (completed)
211178a7ac26SJames Smart 		__nvme_fc_final_op_cleanup(rq);
211278a7ac26SJames Smart }
211378a7ac26SJames Smart 
2114e399441dSJames Smart /*
2115e399441dSJames Smart  * This routine is used by the transport when it needs to find active
2116e399441dSJames Smart  * io on a queue that is to be terminated. The transport uses
2117e399441dSJames Smart  * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2118e399441dSJames Smart  * this routine to kill them on a 1 by 1 basis.
2119e399441dSJames Smart  *
2120e399441dSJames Smart  * As FC allocates FC exchange for each io, the transport must contact
2121e399441dSJames Smart  * the LLDD to terminate the exchange, thus releasing the FC exchange.
2122e399441dSJames Smart  * After terminating the exchange the LLDD will call the transport's
2123e399441dSJames Smart  * normal io done path for the request, but it will have an aborted
2124e399441dSJames Smart  * status. The done path will return the io request back to the block
2125e399441dSJames Smart  * layer with an error status.
2126e399441dSJames Smart  */
2127e399441dSJames Smart static void
2128e399441dSJames Smart nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
2129e399441dSJames Smart {
2130e399441dSJames Smart 	struct nvme_ctrl *nctrl = data;
2131e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2132e399441dSJames Smart 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
213378a7ac26SJames Smart 	unsigned long flags;
2134e399441dSJames Smart 	int status;
2135e399441dSJames Smart 
2136e399441dSJames Smart 	if (!blk_mq_request_started(req))
2137e399441dSJames Smart 		return;
2138e399441dSJames Smart 
213978a7ac26SJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
214061bff8efSJames Smart 	if (ctrl->flags & FCCTRL_TERMIO) {
214161bff8efSJames Smart 		ctrl->iocnt++;
214278a7ac26SJames Smart 		op->flags |= FCOP_FLAGS_TERMIO;
214361bff8efSJames Smart 	}
214478a7ac26SJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
214578a7ac26SJames Smart 
2146e399441dSJames Smart 	status = __nvme_fc_abort_op(ctrl, op);
214778a7ac26SJames Smart 	if (status) {
2148e399441dSJames Smart 		/*
214978a7ac26SJames Smart 		 * if __nvme_fc_abort_op failed the io wasn't
215078a7ac26SJames Smart 		 * active. Thus this call path is running in
215178a7ac26SJames Smart 		 * parallel to the io complete. Treat as non-error.
2152e399441dSJames Smart 		 */
215378a7ac26SJames Smart 
215478a7ac26SJames Smart 		/* back out the flags/counters */
215578a7ac26SJames Smart 		spin_lock_irqsave(&ctrl->lock, flags);
215661bff8efSJames Smart 		if (ctrl->flags & FCCTRL_TERMIO)
215761bff8efSJames Smart 			ctrl->iocnt--;
215878a7ac26SJames Smart 		op->flags &= ~FCOP_FLAGS_TERMIO;
215978a7ac26SJames Smart 		spin_unlock_irqrestore(&ctrl->lock, flags);
2160e399441dSJames Smart 		return;
2161e399441dSJames Smart 	}
216278a7ac26SJames Smart }
2163e399441dSJames Smart 
2164e399441dSJames Smart 
216561bff8efSJames Smart static const struct blk_mq_ops nvme_fc_mq_ops = {
216661bff8efSJames Smart 	.queue_rq	= nvme_fc_queue_rq,
216761bff8efSJames Smart 	.complete	= nvme_fc_complete_rq,
216861bff8efSJames Smart 	.init_request	= nvme_fc_init_request,
216961bff8efSJames Smart 	.exit_request	= nvme_fc_exit_request,
217061bff8efSJames Smart 	.reinit_request	= nvme_fc_reinit_request,
217161bff8efSJames Smart 	.init_hctx	= nvme_fc_init_hctx,
217261bff8efSJames Smart 	.poll		= nvme_fc_poll,
217361bff8efSJames Smart 	.timeout	= nvme_fc_timeout,
2174e399441dSJames Smart };
2175e399441dSJames Smart 
2176e399441dSJames Smart static int
2177e399441dSJames Smart nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2178e399441dSJames Smart {
2179e399441dSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2180e399441dSJames Smart 	int ret;
2181e399441dSJames Smart 
2182e399441dSJames Smart 	ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
2183e399441dSJames Smart 	if (ret) {
2184e399441dSJames Smart 		dev_info(ctrl->ctrl.device,
2185e399441dSJames Smart 			"set_queue_count failed: %d\n", ret);
2186e399441dSJames Smart 		return ret;
2187e399441dSJames Smart 	}
2188e399441dSJames Smart 
2189d858e5f0SSagi Grimberg 	ctrl->ctrl.queue_count = opts->nr_io_queues + 1;
2190e399441dSJames Smart 	if (!opts->nr_io_queues)
2191e399441dSJames Smart 		return 0;
2192e399441dSJames Smart 
2193e399441dSJames Smart 	nvme_fc_init_io_queues(ctrl);
2194e399441dSJames Smart 
2195e399441dSJames Smart 	memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
2196e399441dSJames Smart 	ctrl->tag_set.ops = &nvme_fc_mq_ops;
2197e399441dSJames Smart 	ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
2198e399441dSJames Smart 	ctrl->tag_set.reserved_tags = 1; /* fabric connect */
2199e399441dSJames Smart 	ctrl->tag_set.numa_node = NUMA_NO_NODE;
2200e399441dSJames Smart 	ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2201e399441dSJames Smart 	ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
2202e399441dSJames Smart 					(SG_CHUNK_SIZE *
2203e399441dSJames Smart 						sizeof(struct scatterlist)) +
2204e399441dSJames Smart 					ctrl->lport->ops->fcprqst_priv_sz;
2205e399441dSJames Smart 	ctrl->tag_set.driver_data = ctrl;
2206d858e5f0SSagi Grimberg 	ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
2207e399441dSJames Smart 	ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
2208e399441dSJames Smart 
2209e399441dSJames Smart 	ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
2210e399441dSJames Smart 	if (ret)
2211e399441dSJames Smart 		return ret;
2212e399441dSJames Smart 
2213e399441dSJames Smart 	ctrl->ctrl.tagset = &ctrl->tag_set;
2214e399441dSJames Smart 
2215e399441dSJames Smart 	ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
2216e399441dSJames Smart 	if (IS_ERR(ctrl->ctrl.connect_q)) {
2217e399441dSJames Smart 		ret = PTR_ERR(ctrl->ctrl.connect_q);
2218e399441dSJames Smart 		goto out_free_tag_set;
2219e399441dSJames Smart 	}
2220e399441dSJames Smart 
2221e399441dSJames Smart 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2222e399441dSJames Smart 	if (ret)
2223e399441dSJames Smart 		goto out_cleanup_blk_queue;
2224e399441dSJames Smart 
2225e399441dSJames Smart 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
2226e399441dSJames Smart 	if (ret)
2227e399441dSJames Smart 		goto out_delete_hw_queues;
2228e399441dSJames Smart 
2229e399441dSJames Smart 	return 0;
2230e399441dSJames Smart 
2231e399441dSJames Smart out_delete_hw_queues:
2232e399441dSJames Smart 	nvme_fc_delete_hw_io_queues(ctrl);
2233e399441dSJames Smart out_cleanup_blk_queue:
2234e399441dSJames Smart 	nvme_stop_keep_alive(&ctrl->ctrl);
2235e399441dSJames Smart 	blk_cleanup_queue(ctrl->ctrl.connect_q);
2236e399441dSJames Smart out_free_tag_set:
2237e399441dSJames Smart 	blk_mq_free_tag_set(&ctrl->tag_set);
2238e399441dSJames Smart 	nvme_fc_free_io_queues(ctrl);
2239e399441dSJames Smart 
2240e399441dSJames Smart 	/* force put free routine to ignore io queues */
2241e399441dSJames Smart 	ctrl->ctrl.tagset = NULL;
2242e399441dSJames Smart 
2243e399441dSJames Smart 	return ret;
2244e399441dSJames Smart }
2245e399441dSJames Smart 
224661bff8efSJames Smart static int
224761bff8efSJames Smart nvme_fc_reinit_io_queues(struct nvme_fc_ctrl *ctrl)
224861bff8efSJames Smart {
224961bff8efSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
225061bff8efSJames Smart 	int ret;
225161bff8efSJames Smart 
225261bff8efSJames Smart 	ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
225361bff8efSJames Smart 	if (ret) {
225461bff8efSJames Smart 		dev_info(ctrl->ctrl.device,
225561bff8efSJames Smart 			"set_queue_count failed: %d\n", ret);
225661bff8efSJames Smart 		return ret;
225761bff8efSJames Smart 	}
225861bff8efSJames Smart 
225961bff8efSJames Smart 	/* check for io queues existing */
2260d858e5f0SSagi Grimberg 	if (ctrl->ctrl.queue_count == 1)
226161bff8efSJames Smart 		return 0;
226261bff8efSJames Smart 
226361bff8efSJames Smart 	nvme_fc_init_io_queues(ctrl);
226461bff8efSJames Smart 
226561bff8efSJames Smart 	ret = blk_mq_reinit_tagset(&ctrl->tag_set);
226661bff8efSJames Smart 	if (ret)
226761bff8efSJames Smart 		goto out_free_io_queues;
226861bff8efSJames Smart 
226961bff8efSJames Smart 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
227061bff8efSJames Smart 	if (ret)
227161bff8efSJames Smart 		goto out_free_io_queues;
227261bff8efSJames Smart 
227361bff8efSJames Smart 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
227461bff8efSJames Smart 	if (ret)
227561bff8efSJames Smart 		goto out_delete_hw_queues;
227661bff8efSJames Smart 
227761bff8efSJames Smart 	return 0;
227861bff8efSJames Smart 
227961bff8efSJames Smart out_delete_hw_queues:
228061bff8efSJames Smart 	nvme_fc_delete_hw_io_queues(ctrl);
228161bff8efSJames Smart out_free_io_queues:
228261bff8efSJames Smart 	nvme_fc_free_io_queues(ctrl);
228361bff8efSJames Smart 	return ret;
228461bff8efSJames Smart }
228561bff8efSJames Smart 
228661bff8efSJames Smart /*
228761bff8efSJames Smart  * This routine restarts the controller on the host side, and
228861bff8efSJames Smart  * on the link side, recreates the controller association.
228961bff8efSJames Smart  */
229061bff8efSJames Smart static int
229161bff8efSJames Smart nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
229261bff8efSJames Smart {
229361bff8efSJames Smart 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
229461bff8efSJames Smart 	u32 segs;
229561bff8efSJames Smart 	int ret;
229661bff8efSJames Smart 	bool changed;
229761bff8efSJames Smart 
2298fdf9dfa8SSagi Grimberg 	++ctrl->ctrl.nr_reconnects;
229961bff8efSJames Smart 
230061bff8efSJames Smart 	/*
230161bff8efSJames Smart 	 * Create the admin queue
230261bff8efSJames Smart 	 */
230361bff8efSJames Smart 
230461bff8efSJames Smart 	nvme_fc_init_queue(ctrl, 0, NVME_FC_AQ_BLKMQ_DEPTH);
230561bff8efSJames Smart 
230661bff8efSJames Smart 	ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
230761bff8efSJames Smart 				NVME_FC_AQ_BLKMQ_DEPTH);
230861bff8efSJames Smart 	if (ret)
230961bff8efSJames Smart 		goto out_free_queue;
231061bff8efSJames Smart 
231161bff8efSJames Smart 	ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
231261bff8efSJames Smart 				NVME_FC_AQ_BLKMQ_DEPTH,
231361bff8efSJames Smart 				(NVME_FC_AQ_BLKMQ_DEPTH / 4));
231461bff8efSJames Smart 	if (ret)
231561bff8efSJames Smart 		goto out_delete_hw_queue;
231661bff8efSJames Smart 
231761bff8efSJames Smart 	if (ctrl->ctrl.state != NVME_CTRL_NEW)
231861bff8efSJames Smart 		blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
231961bff8efSJames Smart 
232061bff8efSJames Smart 	ret = nvmf_connect_admin_queue(&ctrl->ctrl);
232161bff8efSJames Smart 	if (ret)
232261bff8efSJames Smart 		goto out_disconnect_admin_queue;
232361bff8efSJames Smart 
232461bff8efSJames Smart 	/*
232561bff8efSJames Smart 	 * Check controller capabilities
232661bff8efSJames Smart 	 *
232761bff8efSJames Smart 	 * todo:- add code to check if ctrl attributes changed from
232861bff8efSJames Smart 	 * prior connection values
232961bff8efSJames Smart 	 */
233061bff8efSJames Smart 
233161bff8efSJames Smart 	ret = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
233261bff8efSJames Smart 	if (ret) {
233361bff8efSJames Smart 		dev_err(ctrl->ctrl.device,
233461bff8efSJames Smart 			"prop_get NVME_REG_CAP failed\n");
233561bff8efSJames Smart 		goto out_disconnect_admin_queue;
233661bff8efSJames Smart 	}
233761bff8efSJames Smart 
233861bff8efSJames Smart 	ctrl->ctrl.sqsize =
233961bff8efSJames Smart 		min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
234061bff8efSJames Smart 
234161bff8efSJames Smart 	ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
234261bff8efSJames Smart 	if (ret)
234361bff8efSJames Smart 		goto out_disconnect_admin_queue;
234461bff8efSJames Smart 
234561bff8efSJames Smart 	segs = min_t(u32, NVME_FC_MAX_SEGMENTS,
234661bff8efSJames Smart 			ctrl->lport->ops->max_sgl_segments);
234761bff8efSJames Smart 	ctrl->ctrl.max_hw_sectors = (segs - 1) << (PAGE_SHIFT - 9);
234861bff8efSJames Smart 
234961bff8efSJames Smart 	ret = nvme_init_identify(&ctrl->ctrl);
235061bff8efSJames Smart 	if (ret)
235161bff8efSJames Smart 		goto out_disconnect_admin_queue;
235261bff8efSJames Smart 
235361bff8efSJames Smart 	/* sanity checks */
235461bff8efSJames Smart 
235561bff8efSJames Smart 	/* FC-NVME does not have other data in the capsule */
235661bff8efSJames Smart 	if (ctrl->ctrl.icdoff) {
235761bff8efSJames Smart 		dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
235861bff8efSJames Smart 				ctrl->ctrl.icdoff);
235961bff8efSJames Smart 		goto out_disconnect_admin_queue;
236061bff8efSJames Smart 	}
236161bff8efSJames Smart 
236261bff8efSJames Smart 	nvme_start_keep_alive(&ctrl->ctrl);
236361bff8efSJames Smart 
236461bff8efSJames Smart 	/* FC-NVME supports normal SGL Data Block Descriptors */
236561bff8efSJames Smart 
236661bff8efSJames Smart 	if (opts->queue_size > ctrl->ctrl.maxcmd) {
236761bff8efSJames Smart 		/* warn if maxcmd is lower than queue_size */
236861bff8efSJames Smart 		dev_warn(ctrl->ctrl.device,
236961bff8efSJames Smart 			"queue_size %zu > ctrl maxcmd %u, reducing "
237061bff8efSJames Smart 			"to queue_size\n",
237161bff8efSJames Smart 			opts->queue_size, ctrl->ctrl.maxcmd);
237261bff8efSJames Smart 		opts->queue_size = ctrl->ctrl.maxcmd;
237361bff8efSJames Smart 	}
237461bff8efSJames Smart 
237561bff8efSJames Smart 	ret = nvme_fc_init_aen_ops(ctrl);
237661bff8efSJames Smart 	if (ret)
237761bff8efSJames Smart 		goto out_term_aen_ops;
237861bff8efSJames Smart 
237961bff8efSJames Smart 	/*
238061bff8efSJames Smart 	 * Create the io queues
238161bff8efSJames Smart 	 */
238261bff8efSJames Smart 
2383d858e5f0SSagi Grimberg 	if (ctrl->ctrl.queue_count > 1) {
238461bff8efSJames Smart 		if (ctrl->ctrl.state == NVME_CTRL_NEW)
238561bff8efSJames Smart 			ret = nvme_fc_create_io_queues(ctrl);
238661bff8efSJames Smart 		else
238761bff8efSJames Smart 			ret = nvme_fc_reinit_io_queues(ctrl);
238861bff8efSJames Smart 		if (ret)
238961bff8efSJames Smart 			goto out_term_aen_ops;
239061bff8efSJames Smart 	}
239161bff8efSJames Smart 
239261bff8efSJames Smart 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
239361bff8efSJames Smart 	WARN_ON_ONCE(!changed);
239461bff8efSJames Smart 
2395fdf9dfa8SSagi Grimberg 	ctrl->ctrl.nr_reconnects = 0;
239661bff8efSJames Smart 
2397d858e5f0SSagi Grimberg 	if (ctrl->ctrl.queue_count > 1) {
239861bff8efSJames Smart 		nvme_start_queues(&ctrl->ctrl);
239961bff8efSJames Smart 		nvme_queue_scan(&ctrl->ctrl);
240061bff8efSJames Smart 		nvme_queue_async_events(&ctrl->ctrl);
240161bff8efSJames Smart 	}
240261bff8efSJames Smart 
240361bff8efSJames Smart 	return 0;	/* Success */
240461bff8efSJames Smart 
240561bff8efSJames Smart out_term_aen_ops:
240661bff8efSJames Smart 	nvme_fc_term_aen_ops(ctrl);
240761bff8efSJames Smart 	nvme_stop_keep_alive(&ctrl->ctrl);
240861bff8efSJames Smart out_disconnect_admin_queue:
240961bff8efSJames Smart 	/* send a Disconnect(association) LS to fc-nvme target */
241061bff8efSJames Smart 	nvme_fc_xmt_disconnect_assoc(ctrl);
241161bff8efSJames Smart out_delete_hw_queue:
241261bff8efSJames Smart 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
241361bff8efSJames Smart out_free_queue:
241461bff8efSJames Smart 	nvme_fc_free_queue(&ctrl->queues[0]);
241561bff8efSJames Smart 
241661bff8efSJames Smart 	return ret;
241761bff8efSJames Smart }
241861bff8efSJames Smart 
241961bff8efSJames Smart /*
242061bff8efSJames Smart  * This routine stops operation of the controller on the host side.
242161bff8efSJames Smart  * On the host os stack side: Admin and IO queues are stopped,
242261bff8efSJames Smart  *   outstanding ios on them terminated via FC ABTS.
242361bff8efSJames Smart  * On the link side: the association is terminated.
242461bff8efSJames Smart  */
242561bff8efSJames Smart static void
242661bff8efSJames Smart nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
242761bff8efSJames Smart {
242861bff8efSJames Smart 	unsigned long flags;
242961bff8efSJames Smart 
243061bff8efSJames Smart 	nvme_stop_keep_alive(&ctrl->ctrl);
243161bff8efSJames Smart 
243261bff8efSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
243361bff8efSJames Smart 	ctrl->flags |= FCCTRL_TERMIO;
243461bff8efSJames Smart 	ctrl->iocnt = 0;
243561bff8efSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
243661bff8efSJames Smart 
243761bff8efSJames Smart 	/*
243861bff8efSJames Smart 	 * If io queues are present, stop them and terminate all outstanding
243961bff8efSJames Smart 	 * ios on them. As FC allocates FC exchange for each io, the
244061bff8efSJames Smart 	 * transport must contact the LLDD to terminate the exchange,
244161bff8efSJames Smart 	 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
244261bff8efSJames Smart 	 * to tell us what io's are busy and invoke a transport routine
244361bff8efSJames Smart 	 * to kill them with the LLDD.  After terminating the exchange
244461bff8efSJames Smart 	 * the LLDD will call the transport's normal io done path, but it
244561bff8efSJames Smart 	 * will have an aborted status. The done path will return the
244661bff8efSJames Smart 	 * io requests back to the block layer as part of normal completions
244761bff8efSJames Smart 	 * (but with error status).
244861bff8efSJames Smart 	 */
2449d858e5f0SSagi Grimberg 	if (ctrl->ctrl.queue_count > 1) {
245061bff8efSJames Smart 		nvme_stop_queues(&ctrl->ctrl);
245161bff8efSJames Smart 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
245261bff8efSJames Smart 				nvme_fc_terminate_exchange, &ctrl->ctrl);
245361bff8efSJames Smart 	}
245461bff8efSJames Smart 
245561bff8efSJames Smart 	/*
245661bff8efSJames Smart 	 * Other transports, which don't have link-level contexts bound
245761bff8efSJames Smart 	 * to sqe's, would try to gracefully shutdown the controller by
245861bff8efSJames Smart 	 * writing the registers for shutdown and polling (call
245961bff8efSJames Smart 	 * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially
246061bff8efSJames Smart 	 * just aborted and we will wait on those contexts, and given
246161bff8efSJames Smart 	 * there was no indication of how live the controlelr is on the
246261bff8efSJames Smart 	 * link, don't send more io to create more contexts for the
246361bff8efSJames Smart 	 * shutdown. Let the controller fail via keepalive failure if
246461bff8efSJames Smart 	 * its still present.
246561bff8efSJames Smart 	 */
246661bff8efSJames Smart 
246761bff8efSJames Smart 	/*
246861bff8efSJames Smart 	 * clean up the admin queue. Same thing as above.
246961bff8efSJames Smart 	 * use blk_mq_tagset_busy_itr() and the transport routine to
247061bff8efSJames Smart 	 * terminate the exchanges.
247161bff8efSJames Smart 	 */
247261bff8efSJames Smart 	blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
247361bff8efSJames Smart 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
247461bff8efSJames Smart 				nvme_fc_terminate_exchange, &ctrl->ctrl);
247561bff8efSJames Smart 
247661bff8efSJames Smart 	/* kill the aens as they are a separate path */
247761bff8efSJames Smart 	nvme_fc_abort_aen_ops(ctrl);
247861bff8efSJames Smart 
247961bff8efSJames Smart 	/* wait for all io that had to be aborted */
248061bff8efSJames Smart 	spin_lock_irqsave(&ctrl->lock, flags);
248136715cf4SJames Smart 	wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
248261bff8efSJames Smart 	ctrl->flags &= ~FCCTRL_TERMIO;
248361bff8efSJames Smart 	spin_unlock_irqrestore(&ctrl->lock, flags);
248461bff8efSJames Smart 
248561bff8efSJames Smart 	nvme_fc_term_aen_ops(ctrl);
248661bff8efSJames Smart 
248761bff8efSJames Smart 	/*
248861bff8efSJames Smart 	 * send a Disconnect(association) LS to fc-nvme target
248961bff8efSJames Smart 	 * Note: could have been sent at top of process, but
249061bff8efSJames Smart 	 * cleaner on link traffic if after the aborts complete.
249161bff8efSJames Smart 	 * Note: if association doesn't exist, association_id will be 0
249261bff8efSJames Smart 	 */
249361bff8efSJames Smart 	if (ctrl->association_id)
249461bff8efSJames Smart 		nvme_fc_xmt_disconnect_assoc(ctrl);
249561bff8efSJames Smart 
249661bff8efSJames Smart 	if (ctrl->ctrl.tagset) {
249761bff8efSJames Smart 		nvme_fc_delete_hw_io_queues(ctrl);
249861bff8efSJames Smart 		nvme_fc_free_io_queues(ctrl);
249961bff8efSJames Smart 	}
250061bff8efSJames Smart 
250161bff8efSJames Smart 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
250261bff8efSJames Smart 	nvme_fc_free_queue(&ctrl->queues[0]);
250361bff8efSJames Smart }
250461bff8efSJames Smart 
250561bff8efSJames Smart static void
250661bff8efSJames Smart nvme_fc_delete_ctrl_work(struct work_struct *work)
250761bff8efSJames Smart {
250861bff8efSJames Smart 	struct nvme_fc_ctrl *ctrl =
250961bff8efSJames Smart 		container_of(work, struct nvme_fc_ctrl, delete_work);
251061bff8efSJames Smart 
2511d86c4d8eSChristoph Hellwig 	cancel_work_sync(&ctrl->ctrl.reset_work);
251261bff8efSJames Smart 	cancel_delayed_work_sync(&ctrl->connect_work);
251361bff8efSJames Smart 
251461bff8efSJames Smart 	/*
251561bff8efSJames Smart 	 * kill the association on the link side.  this will block
251661bff8efSJames Smart 	 * waiting for io to terminate
251761bff8efSJames Smart 	 */
251861bff8efSJames Smart 	nvme_fc_delete_association(ctrl);
251961bff8efSJames Smart 
252061bff8efSJames Smart 	/*
252161bff8efSJames Smart 	 * tear down the controller
2522a5321aa5SJames Smart 	 * After the last reference on the nvme ctrl is removed,
2523a5321aa5SJames Smart 	 * the transport nvme_fc_nvme_ctrl_freed() callback will be
2524a5321aa5SJames Smart 	 * invoked. From there, the transport will tear down it's
2525a5321aa5SJames Smart 	 * logical queues and association.
252661bff8efSJames Smart 	 */
252761bff8efSJames Smart 	nvme_uninit_ctrl(&ctrl->ctrl);
252861bff8efSJames Smart 
252961bff8efSJames Smart 	nvme_put_ctrl(&ctrl->ctrl);
253061bff8efSJames Smart }
253161bff8efSJames Smart 
25325bbecdbcSJames Smart static bool
25335bbecdbcSJames Smart __nvme_fc_schedule_delete_work(struct nvme_fc_ctrl *ctrl)
25345bbecdbcSJames Smart {
25355bbecdbcSJames Smart 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
25365bbecdbcSJames Smart 		return true;
25375bbecdbcSJames Smart 
25389a6327d2SSagi Grimberg 	if (!queue_work(nvme_wq, &ctrl->delete_work))
25395bbecdbcSJames Smart 		return true;
25405bbecdbcSJames Smart 
25415bbecdbcSJames Smart 	return false;
25425bbecdbcSJames Smart }
25435bbecdbcSJames Smart 
254461bff8efSJames Smart static int
254561bff8efSJames Smart __nvme_fc_del_ctrl(struct nvme_fc_ctrl *ctrl)
254661bff8efSJames Smart {
25475bbecdbcSJames Smart 	return __nvme_fc_schedule_delete_work(ctrl) ? -EBUSY : 0;
254861bff8efSJames Smart }
254961bff8efSJames Smart 
255061bff8efSJames Smart /*
255161bff8efSJames Smart  * Request from nvme core layer to delete the controller
255261bff8efSJames Smart  */
255361bff8efSJames Smart static int
255461bff8efSJames Smart nvme_fc_del_nvme_ctrl(struct nvme_ctrl *nctrl)
255561bff8efSJames Smart {
255661bff8efSJames Smart 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
255761bff8efSJames Smart 	int ret;
255861bff8efSJames Smart 
255961bff8efSJames Smart 	if (!kref_get_unless_zero(&ctrl->ctrl.kref))
256061bff8efSJames Smart 		return -EBUSY;
256161bff8efSJames Smart 
256261bff8efSJames Smart 	ret = __nvme_fc_del_ctrl(ctrl);
256361bff8efSJames Smart 
256461bff8efSJames Smart 	if (!ret)
25659a6327d2SSagi Grimberg 		flush_workqueue(nvme_wq);
256661bff8efSJames Smart 
256761bff8efSJames Smart 	nvme_put_ctrl(&ctrl->ctrl);
256861bff8efSJames Smart 
256961bff8efSJames Smart 	return ret;
257061bff8efSJames Smart }
257161bff8efSJames Smart 
257261bff8efSJames Smart static void
25735bbecdbcSJames Smart nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
25745bbecdbcSJames Smart {
25755bbecdbcSJames Smart 	/* If we are resetting/deleting then do nothing */
25765bbecdbcSJames Smart 	if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
25775bbecdbcSJames Smart 		WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
25785bbecdbcSJames Smart 			ctrl->ctrl.state == NVME_CTRL_LIVE);
25795bbecdbcSJames Smart 		return;
25805bbecdbcSJames Smart 	}
25815bbecdbcSJames Smart 
2582589ff775SJames Smart 	dev_info(ctrl->ctrl.device,
25835bbecdbcSJames Smart 		"NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
25845bbecdbcSJames Smart 		ctrl->cnum, status);
25855bbecdbcSJames Smart 
25865bbecdbcSJames Smart 	if (nvmf_should_reconnect(&ctrl->ctrl)) {
25875bbecdbcSJames Smart 		dev_info(ctrl->ctrl.device,
25885bbecdbcSJames Smart 			"NVME-FC{%d}: Reconnect attempt in %d seconds.\n",
25895bbecdbcSJames Smart 			ctrl->cnum, ctrl->ctrl.opts->reconnect_delay);
25909a6327d2SSagi Grimberg 		queue_delayed_work(nvme_wq, &ctrl->connect_work,
25915bbecdbcSJames Smart 				ctrl->ctrl.opts->reconnect_delay * HZ);
25925bbecdbcSJames Smart 	} else {
2593589ff775SJames Smart 		dev_warn(ctrl->ctrl.device,
25945bbecdbcSJames Smart 				"NVME-FC{%d}: Max reconnect attempts (%d) "
25955bbecdbcSJames Smart 				"reached. Removing controller\n",
2596fdf9dfa8SSagi Grimberg 				ctrl->cnum, ctrl->ctrl.nr_reconnects);
25975bbecdbcSJames Smart 		WARN_ON(__nvme_fc_schedule_delete_work(ctrl));
25985bbecdbcSJames Smart 	}
25995bbecdbcSJames Smart }
26005bbecdbcSJames Smart 
26015bbecdbcSJames Smart static void
260261bff8efSJames Smart nvme_fc_reset_ctrl_work(struct work_struct *work)
260361bff8efSJames Smart {
260461bff8efSJames Smart 	struct nvme_fc_ctrl *ctrl =
2605d86c4d8eSChristoph Hellwig 		container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
260661bff8efSJames Smart 	int ret;
260761bff8efSJames Smart 
260861bff8efSJames Smart 	/* will block will waiting for io to terminate */
260961bff8efSJames Smart 	nvme_fc_delete_association(ctrl);
261061bff8efSJames Smart 
261161bff8efSJames Smart 	ret = nvme_fc_create_association(ctrl);
26125bbecdbcSJames Smart 	if (ret)
26135bbecdbcSJames Smart 		nvme_fc_reconnect_or_delete(ctrl, ret);
26145bbecdbcSJames Smart 	else
261561bff8efSJames Smart 		dev_info(ctrl->ctrl.device,
261661bff8efSJames Smart 			"NVME-FC{%d}: controller reset complete\n", ctrl->cnum);
261761bff8efSJames Smart }
261861bff8efSJames Smart 
261961bff8efSJames Smart static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
262061bff8efSJames Smart 	.name			= "fc",
262161bff8efSJames Smart 	.module			= THIS_MODULE,
2622d3d5b87dSChristoph Hellwig 	.flags			= NVME_F_FABRICS,
262361bff8efSJames Smart 	.reg_read32		= nvmf_reg_read32,
262461bff8efSJames Smart 	.reg_read64		= nvmf_reg_read64,
262561bff8efSJames Smart 	.reg_write32		= nvmf_reg_write32,
262661bff8efSJames Smart 	.free_ctrl		= nvme_fc_nvme_ctrl_freed,
262761bff8efSJames Smart 	.submit_async_event	= nvme_fc_submit_async_event,
262861bff8efSJames Smart 	.delete_ctrl		= nvme_fc_del_nvme_ctrl,
262961bff8efSJames Smart 	.get_address		= nvmf_get_address,
263061bff8efSJames Smart };
263161bff8efSJames Smart 
263261bff8efSJames Smart static void
263361bff8efSJames Smart nvme_fc_connect_ctrl_work(struct work_struct *work)
263461bff8efSJames Smart {
263561bff8efSJames Smart 	int ret;
263661bff8efSJames Smart 
263761bff8efSJames Smart 	struct nvme_fc_ctrl *ctrl =
263861bff8efSJames Smart 			container_of(to_delayed_work(work),
263961bff8efSJames Smart 				struct nvme_fc_ctrl, connect_work);
264061bff8efSJames Smart 
264161bff8efSJames Smart 	ret = nvme_fc_create_association(ctrl);
26425bbecdbcSJames Smart 	if (ret)
26435bbecdbcSJames Smart 		nvme_fc_reconnect_or_delete(ctrl, ret);
26445bbecdbcSJames Smart 	else
264561bff8efSJames Smart 		dev_info(ctrl->ctrl.device,
264661bff8efSJames Smart 			"NVME-FC{%d}: controller reconnect complete\n",
264761bff8efSJames Smart 			ctrl->cnum);
264861bff8efSJames Smart }
264961bff8efSJames Smart 
265061bff8efSJames Smart 
265161bff8efSJames Smart static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
265261bff8efSJames Smart 	.queue_rq	= nvme_fc_queue_rq,
265361bff8efSJames Smart 	.complete	= nvme_fc_complete_rq,
265476f983cbSChristoph Hellwig 	.init_request	= nvme_fc_init_request,
265561bff8efSJames Smart 	.exit_request	= nvme_fc_exit_request,
265661bff8efSJames Smart 	.reinit_request	= nvme_fc_reinit_request,
265761bff8efSJames Smart 	.init_hctx	= nvme_fc_init_admin_hctx,
265861bff8efSJames Smart 	.timeout	= nvme_fc_timeout,
265961bff8efSJames Smart };
266061bff8efSJames Smart 
2661e399441dSJames Smart 
2662e399441dSJames Smart static struct nvme_ctrl *
266361bff8efSJames Smart nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
2664e399441dSJames Smart 	struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
2665e399441dSJames Smart {
2666e399441dSJames Smart 	struct nvme_fc_ctrl *ctrl;
2667e399441dSJames Smart 	unsigned long flags;
2668e399441dSJames Smart 	int ret, idx;
2669e399441dSJames Smart 
267085e6a6adSJames Smart 	if (!(rport->remoteport.port_role &
267185e6a6adSJames Smart 	    (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
267285e6a6adSJames Smart 		ret = -EBADR;
267385e6a6adSJames Smart 		goto out_fail;
267485e6a6adSJames Smart 	}
267585e6a6adSJames Smart 
2676e399441dSJames Smart 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2677e399441dSJames Smart 	if (!ctrl) {
2678e399441dSJames Smart 		ret = -ENOMEM;
2679e399441dSJames Smart 		goto out_fail;
2680e399441dSJames Smart 	}
2681e399441dSJames Smart 
2682e399441dSJames Smart 	idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
2683e399441dSJames Smart 	if (idx < 0) {
2684e399441dSJames Smart 		ret = -ENOSPC;
2685e399441dSJames Smart 		goto out_free_ctrl;
2686e399441dSJames Smart 	}
2687e399441dSJames Smart 
2688e399441dSJames Smart 	ctrl->ctrl.opts = opts;
2689e399441dSJames Smart 	INIT_LIST_HEAD(&ctrl->ctrl_list);
2690e399441dSJames Smart 	ctrl->lport = lport;
2691e399441dSJames Smart 	ctrl->rport = rport;
2692e399441dSJames Smart 	ctrl->dev = lport->dev;
2693e399441dSJames Smart 	ctrl->cnum = idx;
2694e399441dSJames Smart 
2695e399441dSJames Smart 	get_device(ctrl->dev);
2696e399441dSJames Smart 	kref_init(&ctrl->ref);
2697e399441dSJames Smart 
269861bff8efSJames Smart 	INIT_WORK(&ctrl->delete_work, nvme_fc_delete_ctrl_work);
2699d86c4d8eSChristoph Hellwig 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
270061bff8efSJames Smart 	INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
2701e399441dSJames Smart 	spin_lock_init(&ctrl->lock);
2702e399441dSJames Smart 
2703e399441dSJames Smart 	/* io queue count */
2704d858e5f0SSagi Grimberg 	ctrl->ctrl.queue_count = min_t(unsigned int,
2705e399441dSJames Smart 				opts->nr_io_queues,
2706e399441dSJames Smart 				lport->ops->max_hw_queues);
2707d858e5f0SSagi Grimberg 	opts->nr_io_queues = ctrl->ctrl.queue_count;	/* so opts has valid value */
2708d858e5f0SSagi Grimberg 	ctrl->ctrl.queue_count++;	/* +1 for admin queue */
2709e399441dSJames Smart 
2710e399441dSJames Smart 	ctrl->ctrl.sqsize = opts->queue_size - 1;
2711e399441dSJames Smart 	ctrl->ctrl.kato = opts->kato;
2712e399441dSJames Smart 
2713e399441dSJames Smart 	ret = -ENOMEM;
2714d858e5f0SSagi Grimberg 	ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
2715d858e5f0SSagi Grimberg 				sizeof(struct nvme_fc_queue), GFP_KERNEL);
2716e399441dSJames Smart 	if (!ctrl->queues)
271761bff8efSJames Smart 		goto out_free_ida;
2718e399441dSJames Smart 
271961bff8efSJames Smart 	memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
272061bff8efSJames Smart 	ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
272161bff8efSJames Smart 	ctrl->admin_tag_set.queue_depth = NVME_FC_AQ_BLKMQ_DEPTH;
272261bff8efSJames Smart 	ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
272361bff8efSJames Smart 	ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
272461bff8efSJames Smart 	ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
272561bff8efSJames Smart 					(SG_CHUNK_SIZE *
272661bff8efSJames Smart 						sizeof(struct scatterlist)) +
272761bff8efSJames Smart 					ctrl->lport->ops->fcprqst_priv_sz;
272861bff8efSJames Smart 	ctrl->admin_tag_set.driver_data = ctrl;
272961bff8efSJames Smart 	ctrl->admin_tag_set.nr_hw_queues = 1;
273061bff8efSJames Smart 	ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
273161bff8efSJames Smart 
273261bff8efSJames Smart 	ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
2733e399441dSJames Smart 	if (ret)
273461bff8efSJames Smart 		goto out_free_queues;
2735e399441dSJames Smart 
273661bff8efSJames Smart 	ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
273761bff8efSJames Smart 	if (IS_ERR(ctrl->ctrl.admin_q)) {
273861bff8efSJames Smart 		ret = PTR_ERR(ctrl->ctrl.admin_q);
273961bff8efSJames Smart 		goto out_free_admin_tag_set;
2740e399441dSJames Smart 	}
2741e399441dSJames Smart 
274261bff8efSJames Smart 	/*
274361bff8efSJames Smart 	 * Would have been nice to init io queues tag set as well.
274461bff8efSJames Smart 	 * However, we require interaction from the controller
274561bff8efSJames Smart 	 * for max io queue count before we can do so.
274661bff8efSJames Smart 	 * Defer this to the connect path.
274761bff8efSJames Smart 	 */
2748e399441dSJames Smart 
274961bff8efSJames Smart 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
2750e399441dSJames Smart 	if (ret)
275161bff8efSJames Smart 		goto out_cleanup_admin_q;
2752e399441dSJames Smart 
275361bff8efSJames Smart 	/* at this point, teardown path changes to ref counting on nvme ctrl */
2754e399441dSJames Smart 
2755e399441dSJames Smart 	spin_lock_irqsave(&rport->lock, flags);
2756e399441dSJames Smart 	list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
2757e399441dSJames Smart 	spin_unlock_irqrestore(&rport->lock, flags);
2758e399441dSJames Smart 
275961bff8efSJames Smart 	ret = nvme_fc_create_association(ctrl);
276061bff8efSJames Smart 	if (ret) {
2761de41447aSEwan D. Milne 		ctrl->ctrl.opts = NULL;
276261bff8efSJames Smart 		/* initiate nvme ctrl ref counting teardown */
276361bff8efSJames Smart 		nvme_uninit_ctrl(&ctrl->ctrl);
276424b7f059SJames Smart 		nvme_put_ctrl(&ctrl->ctrl);
276561bff8efSJames Smart 
27660b5a7669SJames Smart 		/* Remove core ctrl ref. */
27670b5a7669SJames Smart 		nvme_put_ctrl(&ctrl->ctrl);
27680b5a7669SJames Smart 
276961bff8efSJames Smart 		/* as we're past the point where we transition to the ref
277061bff8efSJames Smart 		 * counting teardown path, if we return a bad pointer here,
277161bff8efSJames Smart 		 * the calling routine, thinking it's prior to the
277261bff8efSJames Smart 		 * transition, will do an rport put. Since the teardown
277361bff8efSJames Smart 		 * path also does a rport put, we do an extra get here to
277461bff8efSJames Smart 		 * so proper order/teardown happens.
277561bff8efSJames Smart 		 */
277661bff8efSJames Smart 		nvme_fc_rport_get(rport);
277761bff8efSJames Smart 
277861bff8efSJames Smart 		if (ret > 0)
277961bff8efSJames Smart 			ret = -EIO;
278061bff8efSJames Smart 		return ERR_PTR(ret);
2781e399441dSJames Smart 	}
2782e399441dSJames Smart 
27832cb657bcSJames Smart 	kref_get(&ctrl->ctrl.kref);
27842cb657bcSJames Smart 
278561bff8efSJames Smart 	dev_info(ctrl->ctrl.device,
278661bff8efSJames Smart 		"NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
278761bff8efSJames Smart 		ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
278861bff8efSJames Smart 
2789e399441dSJames Smart 	return &ctrl->ctrl;
2790e399441dSJames Smart 
279161bff8efSJames Smart out_cleanup_admin_q:
279261bff8efSJames Smart 	blk_cleanup_queue(ctrl->ctrl.admin_q);
279361bff8efSJames Smart out_free_admin_tag_set:
279461bff8efSJames Smart 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
279561bff8efSJames Smart out_free_queues:
279661bff8efSJames Smart 	kfree(ctrl->queues);
2797e399441dSJames Smart out_free_ida:
279861bff8efSJames Smart 	put_device(ctrl->dev);
2799e399441dSJames Smart 	ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
2800e399441dSJames Smart out_free_ctrl:
2801e399441dSJames Smart 	kfree(ctrl);
2802e399441dSJames Smart out_fail:
2803e399441dSJames Smart 	/* exit via here doesn't follow ctlr ref points */
2804e399441dSJames Smart 	return ERR_PTR(ret);
2805e399441dSJames Smart }
2806e399441dSJames Smart 
2807e399441dSJames Smart enum {
2808e399441dSJames Smart 	FCT_TRADDR_ERR		= 0,
2809e399441dSJames Smart 	FCT_TRADDR_WWNN		= 1 << 0,
2810e399441dSJames Smart 	FCT_TRADDR_WWPN		= 1 << 1,
2811e399441dSJames Smart };
2812e399441dSJames Smart 
2813e399441dSJames Smart struct nvmet_fc_traddr {
2814e399441dSJames Smart 	u64	nn;
2815e399441dSJames Smart 	u64	pn;
2816e399441dSJames Smart };
2817e399441dSJames Smart 
2818e399441dSJames Smart static const match_table_t traddr_opt_tokens = {
2819e399441dSJames Smart 	{ FCT_TRADDR_WWNN,	"nn-%s"		},
2820e399441dSJames Smart 	{ FCT_TRADDR_WWPN,	"pn-%s"		},
2821e399441dSJames Smart 	{ FCT_TRADDR_ERR,	NULL		}
2822e399441dSJames Smart };
2823e399441dSJames Smart 
2824e399441dSJames Smart static int
2825e399441dSJames Smart nvme_fc_parse_address(struct nvmet_fc_traddr *traddr, char *buf)
2826e399441dSJames Smart {
2827e399441dSJames Smart 	substring_t args[MAX_OPT_ARGS];
2828e399441dSJames Smart 	char *options, *o, *p;
2829e399441dSJames Smart 	int token, ret = 0;
2830e399441dSJames Smart 	u64 token64;
2831e399441dSJames Smart 
2832e399441dSJames Smart 	options = o = kstrdup(buf, GFP_KERNEL);
2833e399441dSJames Smart 	if (!options)
2834e399441dSJames Smart 		return -ENOMEM;
2835e399441dSJames Smart 
2836e399441dSJames Smart 	while ((p = strsep(&o, ":\n")) != NULL) {
2837e399441dSJames Smart 		if (!*p)
2838e399441dSJames Smart 			continue;
2839e399441dSJames Smart 
2840e399441dSJames Smart 		token = match_token(p, traddr_opt_tokens, args);
2841e399441dSJames Smart 		switch (token) {
2842e399441dSJames Smart 		case FCT_TRADDR_WWNN:
2843e399441dSJames Smart 			if (match_u64(args, &token64)) {
2844e399441dSJames Smart 				ret = -EINVAL;
2845e399441dSJames Smart 				goto out;
2846e399441dSJames Smart 			}
2847e399441dSJames Smart 			traddr->nn = token64;
2848e399441dSJames Smart 			break;
2849e399441dSJames Smart 		case FCT_TRADDR_WWPN:
2850e399441dSJames Smart 			if (match_u64(args, &token64)) {
2851e399441dSJames Smart 				ret = -EINVAL;
2852e399441dSJames Smart 				goto out;
2853e399441dSJames Smart 			}
2854e399441dSJames Smart 			traddr->pn = token64;
2855e399441dSJames Smart 			break;
2856e399441dSJames Smart 		default:
2857e399441dSJames Smart 			pr_warn("unknown traddr token or missing value '%s'\n",
2858e399441dSJames Smart 					p);
2859e399441dSJames Smart 			ret = -EINVAL;
2860e399441dSJames Smart 			goto out;
2861e399441dSJames Smart 		}
2862e399441dSJames Smart 	}
2863e399441dSJames Smart 
2864e399441dSJames Smart out:
2865e399441dSJames Smart 	kfree(options);
2866e399441dSJames Smart 	return ret;
2867e399441dSJames Smart }
2868e399441dSJames Smart 
2869e399441dSJames Smart static struct nvme_ctrl *
2870e399441dSJames Smart nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
2871e399441dSJames Smart {
2872e399441dSJames Smart 	struct nvme_fc_lport *lport;
2873e399441dSJames Smart 	struct nvme_fc_rport *rport;
287461bff8efSJames Smart 	struct nvme_ctrl *ctrl;
2875e399441dSJames Smart 	struct nvmet_fc_traddr laddr = { 0L, 0L };
2876e399441dSJames Smart 	struct nvmet_fc_traddr raddr = { 0L, 0L };
2877e399441dSJames Smart 	unsigned long flags;
2878e399441dSJames Smart 	int ret;
2879e399441dSJames Smart 
2880e399441dSJames Smart 	ret = nvme_fc_parse_address(&raddr, opts->traddr);
2881e399441dSJames Smart 	if (ret || !raddr.nn || !raddr.pn)
2882e399441dSJames Smart 		return ERR_PTR(-EINVAL);
2883e399441dSJames Smart 
2884e399441dSJames Smart 	ret = nvme_fc_parse_address(&laddr, opts->host_traddr);
2885e399441dSJames Smart 	if (ret || !laddr.nn || !laddr.pn)
2886e399441dSJames Smart 		return ERR_PTR(-EINVAL);
2887e399441dSJames Smart 
2888e399441dSJames Smart 	/* find the host and remote ports to connect together */
2889e399441dSJames Smart 	spin_lock_irqsave(&nvme_fc_lock, flags);
2890e399441dSJames Smart 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
2891e399441dSJames Smart 		if (lport->localport.node_name != laddr.nn ||
2892e399441dSJames Smart 		    lport->localport.port_name != laddr.pn)
2893e399441dSJames Smart 			continue;
2894e399441dSJames Smart 
2895e399441dSJames Smart 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
2896e399441dSJames Smart 			if (rport->remoteport.node_name != raddr.nn ||
2897e399441dSJames Smart 			    rport->remoteport.port_name != raddr.pn)
2898e399441dSJames Smart 				continue;
2899e399441dSJames Smart 
2900e399441dSJames Smart 			/* if fail to get reference fall through. Will error */
2901e399441dSJames Smart 			if (!nvme_fc_rport_get(rport))
2902e399441dSJames Smart 				break;
2903e399441dSJames Smart 
2904e399441dSJames Smart 			spin_unlock_irqrestore(&nvme_fc_lock, flags);
2905e399441dSJames Smart 
290661bff8efSJames Smart 			ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
290761bff8efSJames Smart 			if (IS_ERR(ctrl))
290861bff8efSJames Smart 				nvme_fc_rport_put(rport);
290961bff8efSJames Smart 			return ctrl;
2910e399441dSJames Smart 		}
2911e399441dSJames Smart 	}
2912e399441dSJames Smart 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
2913e399441dSJames Smart 
2914e399441dSJames Smart 	return ERR_PTR(-ENOENT);
2915e399441dSJames Smart }
2916e399441dSJames Smart 
2917e399441dSJames Smart 
2918e399441dSJames Smart static struct nvmf_transport_ops nvme_fc_transport = {
2919e399441dSJames Smart 	.name		= "fc",
2920e399441dSJames Smart 	.required_opts	= NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
29215bbecdbcSJames Smart 	.allowed_opts	= NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
2922e399441dSJames Smart 	.create_ctrl	= nvme_fc_create_ctrl,
2923e399441dSJames Smart };
2924e399441dSJames Smart 
2925e399441dSJames Smart static int __init nvme_fc_init_module(void)
2926e399441dSJames Smart {
29279a6327d2SSagi Grimberg 	return nvmf_register_transport(&nvme_fc_transport);
2928e399441dSJames Smart }
2929e399441dSJames Smart 
2930e399441dSJames Smart static void __exit nvme_fc_exit_module(void)
2931e399441dSJames Smart {
2932e399441dSJames Smart 	/* sanity check - all lports should be removed */
2933e399441dSJames Smart 	if (!list_empty(&nvme_fc_lport_list))
2934e399441dSJames Smart 		pr_warn("%s: localport list not empty\n", __func__);
2935e399441dSJames Smart 
2936e399441dSJames Smart 	nvmf_unregister_transport(&nvme_fc_transport);
2937e399441dSJames Smart 
2938e399441dSJames Smart 	ida_destroy(&nvme_fc_local_port_cnt);
2939e399441dSJames Smart 	ida_destroy(&nvme_fc_ctrl_cnt);
2940e399441dSJames Smart }
2941e399441dSJames Smart 
2942e399441dSJames Smart module_init(nvme_fc_init_module);
2943e399441dSJames Smart module_exit(nvme_fc_exit_module);
2944e399441dSJames Smart 
2945e399441dSJames Smart MODULE_LICENSE("GPL v2");
2946