xref: /linux/drivers/nvme/host/fc.c (revision 1cbfb828e05171ca2dd77b5988d068e6872480fe)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016 Avago Technologies.  All rights reserved.
4  */
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/module.h>
7 #include <linux/parser.h>
8 #include <uapi/scsi/fc/fc_fs.h>
9 #include <uapi/scsi/fc/fc_els.h>
10 #include <linux/delay.h>
11 #include <linux/overflow.h>
12 #include <linux/blk-cgroup.h>
13 #include "nvme.h"
14 #include "fabrics.h"
15 #include <linux/nvme-fc-driver.h>
16 #include <linux/nvme-fc.h>
17 #include "fc.h"
18 #include <scsi/scsi_transport_fc.h>
19 
20 /* *************************** Data Structures/Defines ****************** */
21 
22 
23 enum nvme_fc_queue_flags {
24 	NVME_FC_Q_CONNECTED = 0,
25 	NVME_FC_Q_LIVE,
26 };
27 
28 #define NVME_FC_DEFAULT_DEV_LOSS_TMO	60	/* seconds */
29 #define NVME_FC_DEFAULT_RECONNECT_TMO	2	/* delay between reconnects
30 						 * when connected and a
31 						 * connection failure.
32 						 */
33 
34 struct nvme_fc_queue {
35 	struct nvme_fc_ctrl	*ctrl;
36 	struct device		*dev;
37 	struct blk_mq_hw_ctx	*hctx;
38 	void			*lldd_handle;
39 	size_t			cmnd_capsule_len;
40 	u32			qnum;
41 	u32			rqcnt;
42 	u32			seqno;
43 
44 	u64			connection_id;
45 	atomic_t		csn;
46 
47 	unsigned long		flags;
48 } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
49 
50 enum nvme_fcop_flags {
51 	FCOP_FLAGS_TERMIO	= (1 << 0),
52 	FCOP_FLAGS_AEN		= (1 << 1),
53 };
54 
55 struct nvmefc_ls_req_op {
56 	struct nvmefc_ls_req	ls_req;
57 
58 	struct nvme_fc_rport	*rport;
59 	struct nvme_fc_queue	*queue;
60 	struct request		*rq;
61 	u32			flags;
62 
63 	int			ls_error;
64 	struct completion	ls_done;
65 	struct list_head	lsreq_list;	/* rport->ls_req_list */
66 	bool			req_queued;
67 };
68 
69 struct nvmefc_ls_rcv_op {
70 	struct nvme_fc_rport		*rport;
71 	struct nvmefc_ls_rsp		*lsrsp;
72 	union nvmefc_ls_requests	*rqstbuf;
73 	union nvmefc_ls_responses	*rspbuf;
74 	u16				rqstdatalen;
75 	bool				handled;
76 	dma_addr_t			rspdma;
77 	struct list_head		lsrcv_list;	/* rport->ls_rcv_list */
78 } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
79 
80 enum nvme_fcpop_state {
81 	FCPOP_STATE_UNINIT	= 0,
82 	FCPOP_STATE_IDLE	= 1,
83 	FCPOP_STATE_ACTIVE	= 2,
84 	FCPOP_STATE_ABORTED	= 3,
85 	FCPOP_STATE_COMPLETE	= 4,
86 };
87 
88 struct nvme_fc_fcp_op {
89 	struct nvme_request	nreq;		/*
90 						 * nvme/host/core.c
91 						 * requires this to be
92 						 * the 1st element in the
93 						 * private structure
94 						 * associated with the
95 						 * request.
96 						 */
97 	struct nvmefc_fcp_req	fcp_req;
98 
99 	struct nvme_fc_ctrl	*ctrl;
100 	struct nvme_fc_queue	*queue;
101 	struct request		*rq;
102 
103 	atomic_t		state;
104 	u32			flags;
105 	u32			rqno;
106 	u32			nents;
107 
108 	struct nvme_fc_cmd_iu	cmd_iu;
109 	struct nvme_fc_ersp_iu	rsp_iu;
110 };
111 
112 struct nvme_fcp_op_w_sgl {
113 	struct nvme_fc_fcp_op	op;
114 	struct scatterlist	sgl[NVME_INLINE_SG_CNT];
115 	uint8_t			priv[];
116 };
117 
118 struct nvme_fc_lport {
119 	struct nvme_fc_local_port	localport;
120 
121 	struct ida			endp_cnt;
122 	struct list_head		port_list;	/* nvme_fc_port_list */
123 	struct list_head		endp_list;
124 	struct device			*dev;	/* physical device for dma */
125 	struct nvme_fc_port_template	*ops;
126 	struct kref			ref;
127 	atomic_t                        act_rport_cnt;
128 } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
129 
130 struct nvme_fc_rport {
131 	struct nvme_fc_remote_port	remoteport;
132 
133 	struct list_head		endp_list; /* for lport->endp_list */
134 	struct list_head		ctrl_list;
135 	struct list_head		ls_req_list;
136 	struct list_head		ls_rcv_list;
137 	struct list_head		disc_list;
138 	struct device			*dev;	/* physical device for dma */
139 	struct nvme_fc_lport		*lport;
140 	spinlock_t			lock;
141 	struct kref			ref;
142 	atomic_t                        act_ctrl_cnt;
143 	unsigned long			dev_loss_end;
144 	struct work_struct		lsrcv_work;
145 } __aligned(sizeof(u64));	/* alignment for other things alloc'd with */
146 
147 /* fc_ctrl flags values - specified as bit positions */
148 #define ASSOC_ACTIVE		0
149 #define ASSOC_FAILED		1
150 #define FCCTRL_TERMIO		2
151 
152 struct nvme_fc_ctrl {
153 	spinlock_t		lock;
154 	struct nvme_fc_queue	*queues;
155 	struct device		*dev;
156 	struct nvme_fc_lport	*lport;
157 	struct nvme_fc_rport	*rport;
158 	u32			cnum;
159 
160 	bool			ioq_live;
161 	u64			association_id;
162 	struct nvmefc_ls_rcv_op	*rcv_disconn;
163 
164 	struct list_head	ctrl_list;	/* rport->ctrl_list */
165 
166 	struct blk_mq_tag_set	admin_tag_set;
167 	struct blk_mq_tag_set	tag_set;
168 
169 	struct work_struct	ioerr_work;
170 	struct delayed_work	connect_work;
171 
172 	struct kref		ref;
173 	unsigned long		flags;
174 	u32			iocnt;
175 	wait_queue_head_t	ioabort_wait;
176 
177 	struct nvme_fc_fcp_op	aen_ops[NVME_NR_AEN_COMMANDS];
178 
179 	struct nvme_ctrl	ctrl;
180 };
181 
182 static inline struct nvme_fc_ctrl *
183 to_fc_ctrl(struct nvme_ctrl *ctrl)
184 {
185 	return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
186 }
187 
188 static inline struct nvme_fc_lport *
189 localport_to_lport(struct nvme_fc_local_port *portptr)
190 {
191 	return container_of(portptr, struct nvme_fc_lport, localport);
192 }
193 
194 static inline struct nvme_fc_rport *
195 remoteport_to_rport(struct nvme_fc_remote_port *portptr)
196 {
197 	return container_of(portptr, struct nvme_fc_rport, remoteport);
198 }
199 
200 static inline struct nvmefc_ls_req_op *
201 ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
202 {
203 	return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
204 }
205 
206 static inline struct nvme_fc_fcp_op *
207 fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
208 {
209 	return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
210 }
211 
212 
213 
214 /* *************************** Globals **************************** */
215 
216 
217 static DEFINE_SPINLOCK(nvme_fc_lock);
218 
219 static LIST_HEAD(nvme_fc_lport_list);
220 static DEFINE_IDA(nvme_fc_local_port_cnt);
221 static DEFINE_IDA(nvme_fc_ctrl_cnt);
222 
223 /*
224  * These items are short-term. They will eventually be moved into
225  * a generic FC class. See comments in module init.
226  */
227 static struct device *fc_udev_device;
228 
229 static void nvme_fc_complete_rq(struct request *rq);
230 
231 /* *********************** FC-NVME Port Management ************************ */
232 
233 static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
234 			struct nvme_fc_queue *, unsigned int);
235 
236 static void nvme_fc_handle_ls_rqst_work(struct work_struct *work);
237 
238 
239 static void
240 nvme_fc_free_lport(struct kref *ref)
241 {
242 	struct nvme_fc_lport *lport =
243 		container_of(ref, struct nvme_fc_lport, ref);
244 	unsigned long flags;
245 
246 	WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
247 	WARN_ON(!list_empty(&lport->endp_list));
248 
249 	/* remove from transport list */
250 	spin_lock_irqsave(&nvme_fc_lock, flags);
251 	list_del(&lport->port_list);
252 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
253 
254 	ida_free(&nvme_fc_local_port_cnt, lport->localport.port_num);
255 	ida_destroy(&lport->endp_cnt);
256 
257 	put_device(lport->dev);
258 
259 	kfree(lport);
260 }
261 
262 static void
263 nvme_fc_lport_put(struct nvme_fc_lport *lport)
264 {
265 	kref_put(&lport->ref, nvme_fc_free_lport);
266 }
267 
268 static int
269 nvme_fc_lport_get(struct nvme_fc_lport *lport)
270 {
271 	return kref_get_unless_zero(&lport->ref);
272 }
273 
274 
275 static struct nvme_fc_lport *
276 nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo,
277 			struct nvme_fc_port_template *ops,
278 			struct device *dev)
279 {
280 	struct nvme_fc_lport *lport;
281 	unsigned long flags;
282 
283 	spin_lock_irqsave(&nvme_fc_lock, flags);
284 
285 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
286 		if (lport->localport.node_name != pinfo->node_name ||
287 		    lport->localport.port_name != pinfo->port_name)
288 			continue;
289 
290 		if (lport->dev != dev) {
291 			lport = ERR_PTR(-EXDEV);
292 			goto out_done;
293 		}
294 
295 		if (lport->localport.port_state != FC_OBJSTATE_DELETED) {
296 			lport = ERR_PTR(-EEXIST);
297 			goto out_done;
298 		}
299 
300 		if (!nvme_fc_lport_get(lport)) {
301 			/*
302 			 * fails if ref cnt already 0. If so,
303 			 * act as if lport already deleted
304 			 */
305 			lport = NULL;
306 			goto out_done;
307 		}
308 
309 		/* resume the lport */
310 
311 		lport->ops = ops;
312 		lport->localport.port_role = pinfo->port_role;
313 		lport->localport.port_id = pinfo->port_id;
314 		lport->localport.port_state = FC_OBJSTATE_ONLINE;
315 
316 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
317 
318 		return lport;
319 	}
320 
321 	lport = NULL;
322 
323 out_done:
324 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
325 
326 	return lport;
327 }
328 
329 /**
330  * nvme_fc_register_localport - transport entry point called by an
331  *                              LLDD to register the existence of a NVME
332  *                              host FC port.
333  * @pinfo:     pointer to information about the port to be registered
334  * @template:  LLDD entrypoints and operational parameters for the port
335  * @dev:       physical hardware device node port corresponds to. Will be
336  *             used for DMA mappings
337  * @portptr:   pointer to a local port pointer. Upon success, the routine
338  *             will allocate a nvme_fc_local_port structure and place its
339  *             address in the local port pointer. Upon failure, local port
340  *             pointer will be set to 0.
341  *
342  * Returns:
343  * a completion status. Must be 0 upon success; a negative errno
344  * (ex: -ENXIO) upon failure.
345  */
346 int
347 nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
348 			struct nvme_fc_port_template *template,
349 			struct device *dev,
350 			struct nvme_fc_local_port **portptr)
351 {
352 	struct nvme_fc_lport *newrec;
353 	unsigned long flags;
354 	int ret, idx;
355 
356 	if (!template->localport_delete || !template->remoteport_delete ||
357 	    !template->ls_req || !template->fcp_io ||
358 	    !template->ls_abort || !template->fcp_abort ||
359 	    !template->max_hw_queues || !template->max_sgl_segments ||
360 	    !template->max_dif_sgl_segments || !template->dma_boundary) {
361 		ret = -EINVAL;
362 		goto out_reghost_failed;
363 	}
364 
365 	/*
366 	 * look to see if there is already a localport that had been
367 	 * deregistered and in the process of waiting for all the
368 	 * references to fully be removed.  If the references haven't
369 	 * expired, we can simply re-enable the localport. Remoteports
370 	 * and controller reconnections should resume naturally.
371 	 */
372 	newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev);
373 
374 	/* found an lport, but something about its state is bad */
375 	if (IS_ERR(newrec)) {
376 		ret = PTR_ERR(newrec);
377 		goto out_reghost_failed;
378 
379 	/* found existing lport, which was resumed */
380 	} else if (newrec) {
381 		*portptr = &newrec->localport;
382 		return 0;
383 	}
384 
385 	/* nothing found - allocate a new localport struct */
386 
387 	newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
388 			 GFP_KERNEL);
389 	if (!newrec) {
390 		ret = -ENOMEM;
391 		goto out_reghost_failed;
392 	}
393 
394 	idx = ida_alloc(&nvme_fc_local_port_cnt, GFP_KERNEL);
395 	if (idx < 0) {
396 		ret = -ENOSPC;
397 		goto out_fail_kfree;
398 	}
399 
400 	if (!get_device(dev) && dev) {
401 		ret = -ENODEV;
402 		goto out_ida_put;
403 	}
404 
405 	INIT_LIST_HEAD(&newrec->port_list);
406 	INIT_LIST_HEAD(&newrec->endp_list);
407 	kref_init(&newrec->ref);
408 	atomic_set(&newrec->act_rport_cnt, 0);
409 	newrec->ops = template;
410 	newrec->dev = dev;
411 	ida_init(&newrec->endp_cnt);
412 	if (template->local_priv_sz)
413 		newrec->localport.private = &newrec[1];
414 	else
415 		newrec->localport.private = NULL;
416 	newrec->localport.node_name = pinfo->node_name;
417 	newrec->localport.port_name = pinfo->port_name;
418 	newrec->localport.port_role = pinfo->port_role;
419 	newrec->localport.port_id = pinfo->port_id;
420 	newrec->localport.port_state = FC_OBJSTATE_ONLINE;
421 	newrec->localport.port_num = idx;
422 
423 	spin_lock_irqsave(&nvme_fc_lock, flags);
424 	list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
425 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
426 
427 	if (dev)
428 		dma_set_seg_boundary(dev, template->dma_boundary);
429 
430 	*portptr = &newrec->localport;
431 	return 0;
432 
433 out_ida_put:
434 	ida_free(&nvme_fc_local_port_cnt, idx);
435 out_fail_kfree:
436 	kfree(newrec);
437 out_reghost_failed:
438 	*portptr = NULL;
439 
440 	return ret;
441 }
442 EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
443 
444 /**
445  * nvme_fc_unregister_localport - transport entry point called by an
446  *                              LLDD to deregister/remove a previously
447  *                              registered a NVME host FC port.
448  * @portptr: pointer to the (registered) local port that is to be deregistered.
449  *
450  * Returns:
451  * a completion status. Must be 0 upon success; a negative errno
452  * (ex: -ENXIO) upon failure.
453  */
454 int
455 nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
456 {
457 	struct nvme_fc_lport *lport = localport_to_lport(portptr);
458 	unsigned long flags;
459 
460 	if (!portptr)
461 		return -EINVAL;
462 
463 	spin_lock_irqsave(&nvme_fc_lock, flags);
464 
465 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
466 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
467 		return -EINVAL;
468 	}
469 	portptr->port_state = FC_OBJSTATE_DELETED;
470 
471 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
472 
473 	if (atomic_read(&lport->act_rport_cnt) == 0)
474 		lport->ops->localport_delete(&lport->localport);
475 
476 	nvme_fc_lport_put(lport);
477 
478 	return 0;
479 }
480 EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
481 
482 /*
483  * TRADDR strings, per FC-NVME are fixed format:
484  *   "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
485  * udev event will only differ by prefix of what field is
486  * being specified:
487  *    "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
488  *  19 + 43 + null_fudge = 64 characters
489  */
490 #define FCNVME_TRADDR_LENGTH		64
491 
492 static void
493 nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
494 		struct nvme_fc_rport *rport)
495 {
496 	char hostaddr[FCNVME_TRADDR_LENGTH];	/* NVMEFC_HOST_TRADDR=...*/
497 	char tgtaddr[FCNVME_TRADDR_LENGTH];	/* NVMEFC_TRADDR=...*/
498 	char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
499 
500 	if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
501 		return;
502 
503 	snprintf(hostaddr, sizeof(hostaddr),
504 		"NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
505 		lport->localport.node_name, lport->localport.port_name);
506 	snprintf(tgtaddr, sizeof(tgtaddr),
507 		"NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
508 		rport->remoteport.node_name, rport->remoteport.port_name);
509 	kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
510 }
511 
512 static void
513 nvme_fc_free_rport(struct kref *ref)
514 {
515 	struct nvme_fc_rport *rport =
516 		container_of(ref, struct nvme_fc_rport, ref);
517 	struct nvme_fc_lport *lport =
518 			localport_to_lport(rport->remoteport.localport);
519 	unsigned long flags;
520 
521 	WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
522 	WARN_ON(!list_empty(&rport->ctrl_list));
523 
524 	/* remove from lport list */
525 	spin_lock_irqsave(&nvme_fc_lock, flags);
526 	list_del(&rport->endp_list);
527 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
528 
529 	WARN_ON(!list_empty(&rport->disc_list));
530 	ida_free(&lport->endp_cnt, rport->remoteport.port_num);
531 
532 	kfree(rport);
533 
534 	nvme_fc_lport_put(lport);
535 }
536 
537 static void
538 nvme_fc_rport_put(struct nvme_fc_rport *rport)
539 {
540 	kref_put(&rport->ref, nvme_fc_free_rport);
541 }
542 
543 static int
544 nvme_fc_rport_get(struct nvme_fc_rport *rport)
545 {
546 	return kref_get_unless_zero(&rport->ref);
547 }
548 
549 static void
550 nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl)
551 {
552 	switch (nvme_ctrl_state(&ctrl->ctrl)) {
553 	case NVME_CTRL_NEW:
554 	case NVME_CTRL_CONNECTING:
555 		/*
556 		 * As all reconnects were suppressed, schedule a
557 		 * connect.
558 		 */
559 		dev_info(ctrl->ctrl.device,
560 			"NVME-FC{%d}: connectivity re-established. "
561 			"Attempting reconnect\n", ctrl->cnum);
562 
563 		queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
564 		break;
565 
566 	case NVME_CTRL_RESETTING:
567 		/*
568 		 * Controller is already in the process of terminating the
569 		 * association. No need to do anything further. The reconnect
570 		 * step will naturally occur after the reset completes.
571 		 */
572 		break;
573 
574 	default:
575 		/* no action to take - let it delete */
576 		break;
577 	}
578 }
579 
580 static struct nvme_fc_rport *
581 nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport,
582 				struct nvme_fc_port_info *pinfo)
583 {
584 	struct nvme_fc_rport *rport;
585 	struct nvme_fc_ctrl *ctrl;
586 	unsigned long flags;
587 
588 	spin_lock_irqsave(&nvme_fc_lock, flags);
589 
590 	list_for_each_entry(rport, &lport->endp_list, endp_list) {
591 		if (rport->remoteport.node_name != pinfo->node_name ||
592 		    rport->remoteport.port_name != pinfo->port_name)
593 			continue;
594 
595 		if (!nvme_fc_rport_get(rport)) {
596 			rport = ERR_PTR(-ENOLCK);
597 			goto out_done;
598 		}
599 
600 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
601 
602 		spin_lock_irqsave(&rport->lock, flags);
603 
604 		/* has it been unregistered */
605 		if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) {
606 			/* means lldd called us twice */
607 			spin_unlock_irqrestore(&rport->lock, flags);
608 			nvme_fc_rport_put(rport);
609 			return ERR_PTR(-ESTALE);
610 		}
611 
612 		rport->remoteport.port_role = pinfo->port_role;
613 		rport->remoteport.port_id = pinfo->port_id;
614 		rport->remoteport.port_state = FC_OBJSTATE_ONLINE;
615 		rport->dev_loss_end = 0;
616 
617 		/*
618 		 * kick off a reconnect attempt on all associations to the
619 		 * remote port. A successful reconnects will resume i/o.
620 		 */
621 		list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
622 			nvme_fc_resume_controller(ctrl);
623 
624 		spin_unlock_irqrestore(&rport->lock, flags);
625 
626 		return rport;
627 	}
628 
629 	rport = NULL;
630 
631 out_done:
632 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
633 
634 	return rport;
635 }
636 
637 static inline void
638 __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport,
639 			struct nvme_fc_port_info *pinfo)
640 {
641 	if (pinfo->dev_loss_tmo)
642 		rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo;
643 	else
644 		rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO;
645 }
646 
647 /**
648  * nvme_fc_register_remoteport - transport entry point called by an
649  *                              LLDD to register the existence of a NVME
650  *                              subsystem FC port on its fabric.
651  * @localport: pointer to the (registered) local port that the remote
652  *             subsystem port is connected to.
653  * @pinfo:     pointer to information about the port to be registered
654  * @portptr:   pointer to a remote port pointer. Upon success, the routine
655  *             will allocate a nvme_fc_remote_port structure and place its
656  *             address in the remote port pointer. Upon failure, remote port
657  *             pointer will be set to 0.
658  *
659  * Returns:
660  * a completion status. Must be 0 upon success; a negative errno
661  * (ex: -ENXIO) upon failure.
662  */
663 int
664 nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
665 				struct nvme_fc_port_info *pinfo,
666 				struct nvme_fc_remote_port **portptr)
667 {
668 	struct nvme_fc_lport *lport = localport_to_lport(localport);
669 	struct nvme_fc_rport *newrec;
670 	unsigned long flags;
671 	int ret, idx;
672 
673 	if (!nvme_fc_lport_get(lport)) {
674 		ret = -ESHUTDOWN;
675 		goto out_reghost_failed;
676 	}
677 
678 	/*
679 	 * look to see if there is already a remoteport that is waiting
680 	 * for a reconnect (within dev_loss_tmo) with the same WWN's.
681 	 * If so, transition to it and reconnect.
682 	 */
683 	newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo);
684 
685 	/* found an rport, but something about its state is bad */
686 	if (IS_ERR(newrec)) {
687 		ret = PTR_ERR(newrec);
688 		goto out_lport_put;
689 
690 	/* found existing rport, which was resumed */
691 	} else if (newrec) {
692 		nvme_fc_lport_put(lport);
693 		__nvme_fc_set_dev_loss_tmo(newrec, pinfo);
694 		nvme_fc_signal_discovery_scan(lport, newrec);
695 		*portptr = &newrec->remoteport;
696 		return 0;
697 	}
698 
699 	/* nothing found - allocate a new remoteport struct */
700 
701 	newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
702 			 GFP_KERNEL);
703 	if (!newrec) {
704 		ret = -ENOMEM;
705 		goto out_lport_put;
706 	}
707 
708 	idx = ida_alloc(&lport->endp_cnt, GFP_KERNEL);
709 	if (idx < 0) {
710 		ret = -ENOSPC;
711 		goto out_kfree_rport;
712 	}
713 
714 	INIT_LIST_HEAD(&newrec->endp_list);
715 	INIT_LIST_HEAD(&newrec->ctrl_list);
716 	INIT_LIST_HEAD(&newrec->ls_req_list);
717 	INIT_LIST_HEAD(&newrec->disc_list);
718 	kref_init(&newrec->ref);
719 	atomic_set(&newrec->act_ctrl_cnt, 0);
720 	spin_lock_init(&newrec->lock);
721 	newrec->remoteport.localport = &lport->localport;
722 	INIT_LIST_HEAD(&newrec->ls_rcv_list);
723 	newrec->dev = lport->dev;
724 	newrec->lport = lport;
725 	if (lport->ops->remote_priv_sz)
726 		newrec->remoteport.private = &newrec[1];
727 	else
728 		newrec->remoteport.private = NULL;
729 	newrec->remoteport.port_role = pinfo->port_role;
730 	newrec->remoteport.node_name = pinfo->node_name;
731 	newrec->remoteport.port_name = pinfo->port_name;
732 	newrec->remoteport.port_id = pinfo->port_id;
733 	newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
734 	newrec->remoteport.port_num = idx;
735 	__nvme_fc_set_dev_loss_tmo(newrec, pinfo);
736 	INIT_WORK(&newrec->lsrcv_work, nvme_fc_handle_ls_rqst_work);
737 
738 	spin_lock_irqsave(&nvme_fc_lock, flags);
739 	list_add_tail(&newrec->endp_list, &lport->endp_list);
740 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
741 
742 	nvme_fc_signal_discovery_scan(lport, newrec);
743 
744 	*portptr = &newrec->remoteport;
745 	return 0;
746 
747 out_kfree_rport:
748 	kfree(newrec);
749 out_lport_put:
750 	nvme_fc_lport_put(lport);
751 out_reghost_failed:
752 	*portptr = NULL;
753 	return ret;
754 }
755 EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
756 
757 static int
758 nvme_fc_abort_lsops(struct nvme_fc_rport *rport)
759 {
760 	struct nvmefc_ls_req_op *lsop;
761 	unsigned long flags;
762 
763 restart:
764 	spin_lock_irqsave(&rport->lock, flags);
765 
766 	list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) {
767 		if (!(lsop->flags & FCOP_FLAGS_TERMIO)) {
768 			lsop->flags |= FCOP_FLAGS_TERMIO;
769 			spin_unlock_irqrestore(&rport->lock, flags);
770 			rport->lport->ops->ls_abort(&rport->lport->localport,
771 						&rport->remoteport,
772 						&lsop->ls_req);
773 			goto restart;
774 		}
775 	}
776 	spin_unlock_irqrestore(&rport->lock, flags);
777 
778 	return 0;
779 }
780 
781 static void
782 nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
783 {
784 	dev_info(ctrl->ctrl.device,
785 		"NVME-FC{%d}: controller connectivity lost. Awaiting "
786 		"Reconnect", ctrl->cnum);
787 
788 	switch (nvme_ctrl_state(&ctrl->ctrl)) {
789 	case NVME_CTRL_NEW:
790 	case NVME_CTRL_LIVE:
791 		/*
792 		 * Schedule a controller reset. The reset will terminate the
793 		 * association and schedule the reconnect timer.  Reconnects
794 		 * will be attempted until either the ctlr_loss_tmo
795 		 * (max_retries * connect_delay) expires or the remoteport's
796 		 * dev_loss_tmo expires.
797 		 */
798 		if (nvme_reset_ctrl(&ctrl->ctrl)) {
799 			dev_warn(ctrl->ctrl.device,
800 				"NVME-FC{%d}: Couldn't schedule reset.\n",
801 				ctrl->cnum);
802 			nvme_delete_ctrl(&ctrl->ctrl);
803 		}
804 		break;
805 
806 	case NVME_CTRL_CONNECTING:
807 		/*
808 		 * The association has already been terminated and the
809 		 * controller is attempting reconnects.  No need to do anything
810 		 * futher.  Reconnects will be attempted until either the
811 		 * ctlr_loss_tmo (max_retries * connect_delay) expires or the
812 		 * remoteport's dev_loss_tmo expires.
813 		 */
814 		break;
815 
816 	case NVME_CTRL_RESETTING:
817 		/*
818 		 * Controller is already in the process of terminating the
819 		 * association.  No need to do anything further. The reconnect
820 		 * step will kick in naturally after the association is
821 		 * terminated.
822 		 */
823 		break;
824 
825 	case NVME_CTRL_DELETING:
826 	case NVME_CTRL_DELETING_NOIO:
827 	default:
828 		/* no action to take - let it delete */
829 		break;
830 	}
831 }
832 
833 /**
834  * nvme_fc_unregister_remoteport - transport entry point called by an
835  *                              LLDD to deregister/remove a previously
836  *                              registered a NVME subsystem FC port.
837  * @portptr: pointer to the (registered) remote port that is to be
838  *           deregistered.
839  *
840  * Returns:
841  * a completion status. Must be 0 upon success; a negative errno
842  * (ex: -ENXIO) upon failure.
843  */
844 int
845 nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
846 {
847 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
848 	struct nvme_fc_ctrl *ctrl;
849 	unsigned long flags;
850 
851 	if (!portptr)
852 		return -EINVAL;
853 
854 	spin_lock_irqsave(&rport->lock, flags);
855 
856 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
857 		spin_unlock_irqrestore(&rport->lock, flags);
858 		return -EINVAL;
859 	}
860 	portptr->port_state = FC_OBJSTATE_DELETED;
861 
862 	rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ);
863 
864 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
865 		/* if dev_loss_tmo==0, dev loss is immediate */
866 		if (!portptr->dev_loss_tmo) {
867 			dev_warn(ctrl->ctrl.device,
868 				"NVME-FC{%d}: controller connectivity lost.\n",
869 				ctrl->cnum);
870 			nvme_delete_ctrl(&ctrl->ctrl);
871 		} else
872 			nvme_fc_ctrl_connectivity_loss(ctrl);
873 	}
874 
875 	spin_unlock_irqrestore(&rport->lock, flags);
876 
877 	nvme_fc_abort_lsops(rport);
878 
879 	if (atomic_read(&rport->act_ctrl_cnt) == 0)
880 		rport->lport->ops->remoteport_delete(portptr);
881 
882 	/*
883 	 * release the reference, which will allow, if all controllers
884 	 * go away, which should only occur after dev_loss_tmo occurs,
885 	 * for the rport to be torn down.
886 	 */
887 	nvme_fc_rport_put(rport);
888 
889 	return 0;
890 }
891 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
892 
893 /**
894  * nvme_fc_rescan_remoteport - transport entry point called by an
895  *                              LLDD to request a nvme device rescan.
896  * @remoteport: pointer to the (registered) remote port that is to be
897  *              rescanned.
898  *
899  * Returns: N/A
900  */
901 void
902 nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
903 {
904 	struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
905 
906 	nvme_fc_signal_discovery_scan(rport->lport, rport);
907 }
908 EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
909 
910 int
911 nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr,
912 			u32 dev_loss_tmo)
913 {
914 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
915 	unsigned long flags;
916 
917 	spin_lock_irqsave(&rport->lock, flags);
918 
919 	if (portptr->port_state != FC_OBJSTATE_ONLINE) {
920 		spin_unlock_irqrestore(&rport->lock, flags);
921 		return -EINVAL;
922 	}
923 
924 	/* a dev_loss_tmo of 0 (immediate) is allowed to be set */
925 	rport->remoteport.dev_loss_tmo = dev_loss_tmo;
926 
927 	spin_unlock_irqrestore(&rport->lock, flags);
928 
929 	return 0;
930 }
931 EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss);
932 
933 
934 /* *********************** FC-NVME DMA Handling **************************** */
935 
936 /*
937  * The fcloop device passes in a NULL device pointer. Real LLD's will
938  * pass in a valid device pointer. If NULL is passed to the dma mapping
939  * routines, depending on the platform, it may or may not succeed, and
940  * may crash.
941  *
942  * As such:
943  * Wrapper all the dma routines and check the dev pointer.
944  *
945  * If simple mappings (return just a dma address, we'll noop them,
946  * returning a dma address of 0.
947  *
948  * On more complex mappings (dma_map_sg), a pseudo routine fills
949  * in the scatter list, setting all dma addresses to 0.
950  */
951 
952 static inline dma_addr_t
953 fc_dma_map_single(struct device *dev, void *ptr, size_t size,
954 		enum dma_data_direction dir)
955 {
956 	return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
957 }
958 
959 static inline int
960 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
961 {
962 	return dev ? dma_mapping_error(dev, dma_addr) : 0;
963 }
964 
965 static inline void
966 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
967 	enum dma_data_direction dir)
968 {
969 	if (dev)
970 		dma_unmap_single(dev, addr, size, dir);
971 }
972 
973 static inline void
974 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
975 		enum dma_data_direction dir)
976 {
977 	if (dev)
978 		dma_sync_single_for_cpu(dev, addr, size, dir);
979 }
980 
981 static inline void
982 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
983 		enum dma_data_direction dir)
984 {
985 	if (dev)
986 		dma_sync_single_for_device(dev, addr, size, dir);
987 }
988 
989 /* pseudo dma_map_sg call */
990 static int
991 fc_map_sg(struct scatterlist *sg, int nents)
992 {
993 	struct scatterlist *s;
994 	int i;
995 
996 	WARN_ON(nents == 0 || sg[0].length == 0);
997 
998 	for_each_sg(sg, s, nents, i) {
999 		s->dma_address = 0L;
1000 #ifdef CONFIG_NEED_SG_DMA_LENGTH
1001 		s->dma_length = s->length;
1002 #endif
1003 	}
1004 	return nents;
1005 }
1006 
1007 static inline int
1008 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1009 		enum dma_data_direction dir)
1010 {
1011 	return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
1012 }
1013 
1014 static inline void
1015 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1016 		enum dma_data_direction dir)
1017 {
1018 	if (dev)
1019 		dma_unmap_sg(dev, sg, nents, dir);
1020 }
1021 
1022 /* *********************** FC-NVME LS Handling **************************** */
1023 
1024 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
1025 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
1026 
1027 static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
1028 
1029 static void
1030 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
1031 {
1032 	struct nvme_fc_rport *rport = lsop->rport;
1033 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1034 	unsigned long flags;
1035 
1036 	spin_lock_irqsave(&rport->lock, flags);
1037 
1038 	if (!lsop->req_queued) {
1039 		spin_unlock_irqrestore(&rport->lock, flags);
1040 		return;
1041 	}
1042 
1043 	list_del(&lsop->lsreq_list);
1044 
1045 	lsop->req_queued = false;
1046 
1047 	spin_unlock_irqrestore(&rport->lock, flags);
1048 
1049 	fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1050 				  (lsreq->rqstlen + lsreq->rsplen),
1051 				  DMA_BIDIRECTIONAL);
1052 
1053 	nvme_fc_rport_put(rport);
1054 }
1055 
1056 static int
1057 __nvme_fc_send_ls_req(struct nvme_fc_rport *rport,
1058 		struct nvmefc_ls_req_op *lsop,
1059 		void (*done)(struct nvmefc_ls_req *req, int status))
1060 {
1061 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1062 	unsigned long flags;
1063 	int ret = 0;
1064 
1065 	if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
1066 		return -ECONNREFUSED;
1067 
1068 	if (!nvme_fc_rport_get(rport))
1069 		return -ESHUTDOWN;
1070 
1071 	lsreq->done = done;
1072 	lsop->rport = rport;
1073 	lsop->req_queued = false;
1074 	INIT_LIST_HEAD(&lsop->lsreq_list);
1075 	init_completion(&lsop->ls_done);
1076 
1077 	lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr,
1078 				  lsreq->rqstlen + lsreq->rsplen,
1079 				  DMA_BIDIRECTIONAL);
1080 	if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) {
1081 		ret = -EFAULT;
1082 		goto out_putrport;
1083 	}
1084 	lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
1085 
1086 	spin_lock_irqsave(&rport->lock, flags);
1087 
1088 	list_add_tail(&lsop->lsreq_list, &rport->ls_req_list);
1089 
1090 	lsop->req_queued = true;
1091 
1092 	spin_unlock_irqrestore(&rport->lock, flags);
1093 
1094 	ret = rport->lport->ops->ls_req(&rport->lport->localport,
1095 					&rport->remoteport, lsreq);
1096 	if (ret)
1097 		goto out_unlink;
1098 
1099 	return 0;
1100 
1101 out_unlink:
1102 	lsop->ls_error = ret;
1103 	spin_lock_irqsave(&rport->lock, flags);
1104 	lsop->req_queued = false;
1105 	list_del(&lsop->lsreq_list);
1106 	spin_unlock_irqrestore(&rport->lock, flags);
1107 	fc_dma_unmap_single(rport->dev, lsreq->rqstdma,
1108 				  (lsreq->rqstlen + lsreq->rsplen),
1109 				  DMA_BIDIRECTIONAL);
1110 out_putrport:
1111 	nvme_fc_rport_put(rport);
1112 
1113 	return ret;
1114 }
1115 
1116 static void
1117 nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
1118 {
1119 	struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1120 
1121 	lsop->ls_error = status;
1122 	complete(&lsop->ls_done);
1123 }
1124 
1125 static int
1126 nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop)
1127 {
1128 	struct nvmefc_ls_req *lsreq = &lsop->ls_req;
1129 	struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
1130 	int ret;
1131 
1132 	ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done);
1133 
1134 	if (!ret) {
1135 		/*
1136 		 * No timeout/not interruptible as we need the struct
1137 		 * to exist until the lldd calls us back. Thus mandate
1138 		 * wait until driver calls back. lldd responsible for
1139 		 * the timeout action
1140 		 */
1141 		wait_for_completion(&lsop->ls_done);
1142 
1143 		__nvme_fc_finish_ls_req(lsop);
1144 
1145 		ret = lsop->ls_error;
1146 	}
1147 
1148 	if (ret)
1149 		return ret;
1150 
1151 	/* ACC or RJT payload ? */
1152 	if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
1153 		return -ENXIO;
1154 
1155 	return 0;
1156 }
1157 
1158 static int
1159 nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport,
1160 		struct nvmefc_ls_req_op *lsop,
1161 		void (*done)(struct nvmefc_ls_req *req, int status))
1162 {
1163 	/* don't wait for completion */
1164 
1165 	return __nvme_fc_send_ls_req(rport, lsop, done);
1166 }
1167 
1168 static int
1169 nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
1170 	struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
1171 {
1172 	struct nvmefc_ls_req_op *lsop;
1173 	struct nvmefc_ls_req *lsreq;
1174 	struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
1175 	struct fcnvme_ls_cr_assoc_acc *assoc_acc;
1176 	unsigned long flags;
1177 	int ret, fcret = 0;
1178 
1179 	lsop = kzalloc((sizeof(*lsop) +
1180 			 sizeof(*assoc_rqst) + sizeof(*assoc_acc) +
1181 			 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1182 	if (!lsop) {
1183 		dev_info(ctrl->ctrl.device,
1184 			"NVME-FC{%d}: send Create Association failed: ENOMEM\n",
1185 			ctrl->cnum);
1186 		ret = -ENOMEM;
1187 		goto out_no_memory;
1188 	}
1189 
1190 	assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1];
1191 	assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
1192 	lsreq = &lsop->ls_req;
1193 	if (ctrl->lport->ops->lsrqst_priv_sz)
1194 		lsreq->private = &assoc_acc[1];
1195 	else
1196 		lsreq->private = NULL;
1197 
1198 	assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
1199 	assoc_rqst->desc_list_len =
1200 			cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1201 
1202 	assoc_rqst->assoc_cmd.desc_tag =
1203 			cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
1204 	assoc_rqst->assoc_cmd.desc_len =
1205 			fcnvme_lsdesc_len(
1206 				sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
1207 
1208 	assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1209 	assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1);
1210 	/* Linux supports only Dynamic controllers */
1211 	assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
1212 	uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
1213 	strscpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1214 		sizeof(assoc_rqst->assoc_cmd.hostnqn));
1215 	strscpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1216 		sizeof(assoc_rqst->assoc_cmd.subnqn));
1217 
1218 	lsop->queue = queue;
1219 	lsreq->rqstaddr = assoc_rqst;
1220 	lsreq->rqstlen = sizeof(*assoc_rqst);
1221 	lsreq->rspaddr = assoc_acc;
1222 	lsreq->rsplen = sizeof(*assoc_acc);
1223 	lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1224 
1225 	ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1226 	if (ret)
1227 		goto out_free_buffer;
1228 
1229 	/* process connect LS completion */
1230 
1231 	/* validate the ACC response */
1232 	if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1233 		fcret = VERR_LSACC;
1234 	else if (assoc_acc->hdr.desc_list_len !=
1235 			fcnvme_lsdesc_len(
1236 				sizeof(struct fcnvme_ls_cr_assoc_acc)))
1237 		fcret = VERR_CR_ASSOC_ACC_LEN;
1238 	else if (assoc_acc->hdr.rqst.desc_tag !=
1239 			cpu_to_be32(FCNVME_LSDESC_RQST))
1240 		fcret = VERR_LSDESC_RQST;
1241 	else if (assoc_acc->hdr.rqst.desc_len !=
1242 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1243 		fcret = VERR_LSDESC_RQST_LEN;
1244 	else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
1245 		fcret = VERR_CR_ASSOC;
1246 	else if (assoc_acc->associd.desc_tag !=
1247 			cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
1248 		fcret = VERR_ASSOC_ID;
1249 	else if (assoc_acc->associd.desc_len !=
1250 			fcnvme_lsdesc_len(
1251 				sizeof(struct fcnvme_lsdesc_assoc_id)))
1252 		fcret = VERR_ASSOC_ID_LEN;
1253 	else if (assoc_acc->connectid.desc_tag !=
1254 			cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1255 		fcret = VERR_CONN_ID;
1256 	else if (assoc_acc->connectid.desc_len !=
1257 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1258 		fcret = VERR_CONN_ID_LEN;
1259 
1260 	if (fcret) {
1261 		ret = -EBADF;
1262 		dev_err(ctrl->dev,
1263 			"q %d Create Association LS failed: %s\n",
1264 			queue->qnum, validation_errors[fcret]);
1265 	} else {
1266 		spin_lock_irqsave(&ctrl->lock, flags);
1267 		ctrl->association_id =
1268 			be64_to_cpu(assoc_acc->associd.association_id);
1269 		queue->connection_id =
1270 			be64_to_cpu(assoc_acc->connectid.connection_id);
1271 		set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1272 		spin_unlock_irqrestore(&ctrl->lock, flags);
1273 	}
1274 
1275 out_free_buffer:
1276 	kfree(lsop);
1277 out_no_memory:
1278 	if (ret)
1279 		dev_err(ctrl->dev,
1280 			"queue %d connect admin queue failed (%d).\n",
1281 			queue->qnum, ret);
1282 	return ret;
1283 }
1284 
1285 static int
1286 nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
1287 			u16 qsize, u16 ersp_ratio)
1288 {
1289 	struct nvmefc_ls_req_op *lsop;
1290 	struct nvmefc_ls_req *lsreq;
1291 	struct fcnvme_ls_cr_conn_rqst *conn_rqst;
1292 	struct fcnvme_ls_cr_conn_acc *conn_acc;
1293 	int ret, fcret = 0;
1294 
1295 	lsop = kzalloc((sizeof(*lsop) +
1296 			 sizeof(*conn_rqst) + sizeof(*conn_acc) +
1297 			 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1298 	if (!lsop) {
1299 		dev_info(ctrl->ctrl.device,
1300 			"NVME-FC{%d}: send Create Connection failed: ENOMEM\n",
1301 			ctrl->cnum);
1302 		ret = -ENOMEM;
1303 		goto out_no_memory;
1304 	}
1305 
1306 	conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)&lsop[1];
1307 	conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
1308 	lsreq = &lsop->ls_req;
1309 	if (ctrl->lport->ops->lsrqst_priv_sz)
1310 		lsreq->private = (void *)&conn_acc[1];
1311 	else
1312 		lsreq->private = NULL;
1313 
1314 	conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
1315 	conn_rqst->desc_list_len = cpu_to_be32(
1316 				sizeof(struct fcnvme_lsdesc_assoc_id) +
1317 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1318 
1319 	conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
1320 	conn_rqst->associd.desc_len =
1321 			fcnvme_lsdesc_len(
1322 				sizeof(struct fcnvme_lsdesc_assoc_id));
1323 	conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
1324 	conn_rqst->connect_cmd.desc_tag =
1325 			cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
1326 	conn_rqst->connect_cmd.desc_len =
1327 			fcnvme_lsdesc_len(
1328 				sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
1329 	conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
1330 	conn_rqst->connect_cmd.qid  = cpu_to_be16(queue->qnum);
1331 	conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1);
1332 
1333 	lsop->queue = queue;
1334 	lsreq->rqstaddr = conn_rqst;
1335 	lsreq->rqstlen = sizeof(*conn_rqst);
1336 	lsreq->rspaddr = conn_acc;
1337 	lsreq->rsplen = sizeof(*conn_acc);
1338 	lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC;
1339 
1340 	ret = nvme_fc_send_ls_req(ctrl->rport, lsop);
1341 	if (ret)
1342 		goto out_free_buffer;
1343 
1344 	/* process connect LS completion */
1345 
1346 	/* validate the ACC response */
1347 	if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
1348 		fcret = VERR_LSACC;
1349 	else if (conn_acc->hdr.desc_list_len !=
1350 			fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
1351 		fcret = VERR_CR_CONN_ACC_LEN;
1352 	else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
1353 		fcret = VERR_LSDESC_RQST;
1354 	else if (conn_acc->hdr.rqst.desc_len !=
1355 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
1356 		fcret = VERR_LSDESC_RQST_LEN;
1357 	else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
1358 		fcret = VERR_CR_CONN;
1359 	else if (conn_acc->connectid.desc_tag !=
1360 			cpu_to_be32(FCNVME_LSDESC_CONN_ID))
1361 		fcret = VERR_CONN_ID;
1362 	else if (conn_acc->connectid.desc_len !=
1363 			fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
1364 		fcret = VERR_CONN_ID_LEN;
1365 
1366 	if (fcret) {
1367 		ret = -EBADF;
1368 		dev_err(ctrl->dev,
1369 			"q %d Create I/O Connection LS failed: %s\n",
1370 			queue->qnum, validation_errors[fcret]);
1371 	} else {
1372 		queue->connection_id =
1373 			be64_to_cpu(conn_acc->connectid.connection_id);
1374 		set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
1375 	}
1376 
1377 out_free_buffer:
1378 	kfree(lsop);
1379 out_no_memory:
1380 	if (ret)
1381 		dev_err(ctrl->dev,
1382 			"queue %d connect I/O queue failed (%d).\n",
1383 			queue->qnum, ret);
1384 	return ret;
1385 }
1386 
1387 static void
1388 nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
1389 {
1390 	struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
1391 
1392 	__nvme_fc_finish_ls_req(lsop);
1393 
1394 	/* fc-nvme initiator doesn't care about success or failure of cmd */
1395 
1396 	kfree(lsop);
1397 }
1398 
1399 /*
1400  * This routine sends a FC-NVME LS to disconnect (aka terminate)
1401  * the FC-NVME Association.  Terminating the association also
1402  * terminates the FC-NVME connections (per queue, both admin and io
1403  * queues) that are part of the association. E.g. things are torn
1404  * down, and the related FC-NVME Association ID and Connection IDs
1405  * become invalid.
1406  *
1407  * The behavior of the fc-nvme initiator is such that it's
1408  * understanding of the association and connections will implicitly
1409  * be torn down. The action is implicit as it may be due to a loss of
1410  * connectivity with the fc-nvme target, so you may never get a
1411  * response even if you tried.  As such, the action of this routine
1412  * is to asynchronously send the LS, ignore any results of the LS, and
1413  * continue on with terminating the association. If the fc-nvme target
1414  * is present and receives the LS, it too can tear down.
1415  */
1416 static void
1417 nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
1418 {
1419 	struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
1420 	struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
1421 	struct nvmefc_ls_req_op *lsop;
1422 	struct nvmefc_ls_req *lsreq;
1423 	int ret;
1424 
1425 	lsop = kzalloc((sizeof(*lsop) +
1426 			sizeof(*discon_rqst) + sizeof(*discon_acc) +
1427 			ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
1428 	if (!lsop) {
1429 		dev_info(ctrl->ctrl.device,
1430 			"NVME-FC{%d}: send Disconnect Association "
1431 			"failed: ENOMEM\n",
1432 			ctrl->cnum);
1433 		return;
1434 	}
1435 
1436 	discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)&lsop[1];
1437 	discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
1438 	lsreq = &lsop->ls_req;
1439 	if (ctrl->lport->ops->lsrqst_priv_sz)
1440 		lsreq->private = (void *)&discon_acc[1];
1441 	else
1442 		lsreq->private = NULL;
1443 
1444 	nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc,
1445 				ctrl->association_id);
1446 
1447 	ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop,
1448 				nvme_fc_disconnect_assoc_done);
1449 	if (ret)
1450 		kfree(lsop);
1451 }
1452 
1453 static void
1454 nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
1455 {
1456 	struct nvmefc_ls_rcv_op *lsop = lsrsp->nvme_fc_private;
1457 	struct nvme_fc_rport *rport = lsop->rport;
1458 	struct nvme_fc_lport *lport = rport->lport;
1459 	unsigned long flags;
1460 
1461 	spin_lock_irqsave(&rport->lock, flags);
1462 	list_del(&lsop->lsrcv_list);
1463 	spin_unlock_irqrestore(&rport->lock, flags);
1464 
1465 	fc_dma_sync_single_for_cpu(lport->dev, lsop->rspdma,
1466 				sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1467 	fc_dma_unmap_single(lport->dev, lsop->rspdma,
1468 			sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1469 
1470 	kfree(lsop->rspbuf);
1471 	kfree(lsop->rqstbuf);
1472 	kfree(lsop);
1473 
1474 	nvme_fc_rport_put(rport);
1475 }
1476 
1477 static void
1478 nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op *lsop)
1479 {
1480 	struct nvme_fc_rport *rport = lsop->rport;
1481 	struct nvme_fc_lport *lport = rport->lport;
1482 	struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
1483 	int ret;
1484 
1485 	fc_dma_sync_single_for_device(lport->dev, lsop->rspdma,
1486 				  sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1487 
1488 	ret = lport->ops->xmt_ls_rsp(&lport->localport, &rport->remoteport,
1489 				     lsop->lsrsp);
1490 	if (ret) {
1491 		dev_warn(lport->dev,
1492 			"LLDD rejected LS RSP xmt: LS %d status %d\n",
1493 			w0->ls_cmd, ret);
1494 		nvme_fc_xmt_ls_rsp_done(lsop->lsrsp);
1495 		return;
1496 	}
1497 }
1498 
1499 static struct nvme_fc_ctrl *
1500 nvme_fc_match_disconn_ls(struct nvme_fc_rport *rport,
1501 		      struct nvmefc_ls_rcv_op *lsop)
1502 {
1503 	struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1504 					&lsop->rqstbuf->rq_dis_assoc;
1505 	struct nvme_fc_ctrl *ctrl, *ret = NULL;
1506 	struct nvmefc_ls_rcv_op *oldls = NULL;
1507 	u64 association_id = be64_to_cpu(rqst->associd.association_id);
1508 	unsigned long flags;
1509 
1510 	spin_lock_irqsave(&rport->lock, flags);
1511 
1512 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
1513 		if (!nvme_fc_ctrl_get(ctrl))
1514 			continue;
1515 		spin_lock(&ctrl->lock);
1516 		if (association_id == ctrl->association_id) {
1517 			oldls = ctrl->rcv_disconn;
1518 			ctrl->rcv_disconn = lsop;
1519 			ret = ctrl;
1520 		}
1521 		spin_unlock(&ctrl->lock);
1522 		if (ret)
1523 			/* leave the ctrl get reference */
1524 			break;
1525 		nvme_fc_ctrl_put(ctrl);
1526 	}
1527 
1528 	spin_unlock_irqrestore(&rport->lock, flags);
1529 
1530 	/* transmit a response for anything that was pending */
1531 	if (oldls) {
1532 		dev_info(rport->lport->dev,
1533 			"NVME-FC{%d}: Multiple Disconnect Association "
1534 			"LS's received\n", ctrl->cnum);
1535 		/* overwrite good response with bogus failure */
1536 		oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf,
1537 						sizeof(*oldls->rspbuf),
1538 						rqst->w0.ls_cmd,
1539 						FCNVME_RJT_RC_UNAB,
1540 						FCNVME_RJT_EXP_NONE, 0);
1541 		nvme_fc_xmt_ls_rsp(oldls);
1542 	}
1543 
1544 	return ret;
1545 }
1546 
1547 /*
1548  * returns true to mean LS handled and ls_rsp can be sent
1549  * returns false to defer ls_rsp xmt (will be done as part of
1550  *     association termination)
1551  */
1552 static bool
1553 nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op *lsop)
1554 {
1555 	struct nvme_fc_rport *rport = lsop->rport;
1556 	struct fcnvme_ls_disconnect_assoc_rqst *rqst =
1557 					&lsop->rqstbuf->rq_dis_assoc;
1558 	struct fcnvme_ls_disconnect_assoc_acc *acc =
1559 					&lsop->rspbuf->rsp_dis_assoc;
1560 	struct nvme_fc_ctrl *ctrl = NULL;
1561 	int ret = 0;
1562 
1563 	memset(acc, 0, sizeof(*acc));
1564 
1565 	ret = nvmefc_vldt_lsreq_discon_assoc(lsop->rqstdatalen, rqst);
1566 	if (!ret) {
1567 		/* match an active association */
1568 		ctrl = nvme_fc_match_disconn_ls(rport, lsop);
1569 		if (!ctrl)
1570 			ret = VERR_NO_ASSOC;
1571 	}
1572 
1573 	if (ret) {
1574 		dev_info(rport->lport->dev,
1575 			"Disconnect LS failed: %s\n",
1576 			validation_errors[ret]);
1577 		lsop->lsrsp->rsplen = nvme_fc_format_rjt(acc,
1578 					sizeof(*acc), rqst->w0.ls_cmd,
1579 					(ret == VERR_NO_ASSOC) ?
1580 						FCNVME_RJT_RC_INV_ASSOC :
1581 						FCNVME_RJT_RC_LOGIC,
1582 					FCNVME_RJT_EXP_NONE, 0);
1583 		return true;
1584 	}
1585 
1586 	/* format an ACCept response */
1587 
1588 	lsop->lsrsp->rsplen = sizeof(*acc);
1589 
1590 	nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
1591 			fcnvme_lsdesc_len(
1592 				sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
1593 			FCNVME_LS_DISCONNECT_ASSOC);
1594 
1595 	/*
1596 	 * the transmit of the response will occur after the exchanges
1597 	 * for the association have been ABTS'd by
1598 	 * nvme_fc_delete_association().
1599 	 */
1600 
1601 	/* fail the association */
1602 	nvme_fc_error_recovery(ctrl, "Disconnect Association LS received");
1603 
1604 	/* release the reference taken by nvme_fc_match_disconn_ls() */
1605 	nvme_fc_ctrl_put(ctrl);
1606 
1607 	return false;
1608 }
1609 
1610 /*
1611  * Actual Processing routine for received FC-NVME LS Requests from the LLD
1612  * returns true if a response should be sent afterward, false if rsp will
1613  * be sent asynchronously.
1614  */
1615 static bool
1616 nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op *lsop)
1617 {
1618 	struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0;
1619 	bool ret = true;
1620 
1621 	lsop->lsrsp->nvme_fc_private = lsop;
1622 	lsop->lsrsp->rspbuf = lsop->rspbuf;
1623 	lsop->lsrsp->rspdma = lsop->rspdma;
1624 	lsop->lsrsp->done = nvme_fc_xmt_ls_rsp_done;
1625 	/* Be preventative. handlers will later set to valid length */
1626 	lsop->lsrsp->rsplen = 0;
1627 
1628 	/*
1629 	 * handlers:
1630 	 *   parse request input, execute the request, and format the
1631 	 *   LS response
1632 	 */
1633 	switch (w0->ls_cmd) {
1634 	case FCNVME_LS_DISCONNECT_ASSOC:
1635 		ret = nvme_fc_ls_disconnect_assoc(lsop);
1636 		break;
1637 	case FCNVME_LS_DISCONNECT_CONN:
1638 		lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1639 				sizeof(*lsop->rspbuf), w0->ls_cmd,
1640 				FCNVME_RJT_RC_UNSUP, FCNVME_RJT_EXP_NONE, 0);
1641 		break;
1642 	case FCNVME_LS_CREATE_ASSOCIATION:
1643 	case FCNVME_LS_CREATE_CONNECTION:
1644 		lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1645 				sizeof(*lsop->rspbuf), w0->ls_cmd,
1646 				FCNVME_RJT_RC_LOGIC, FCNVME_RJT_EXP_NONE, 0);
1647 		break;
1648 	default:
1649 		lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf,
1650 				sizeof(*lsop->rspbuf), w0->ls_cmd,
1651 				FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
1652 		break;
1653 	}
1654 
1655 	return(ret);
1656 }
1657 
1658 static void
1659 nvme_fc_handle_ls_rqst_work(struct work_struct *work)
1660 {
1661 	struct nvme_fc_rport *rport =
1662 		container_of(work, struct nvme_fc_rport, lsrcv_work);
1663 	struct fcnvme_ls_rqst_w0 *w0;
1664 	struct nvmefc_ls_rcv_op *lsop;
1665 	unsigned long flags;
1666 	bool sendrsp;
1667 
1668 restart:
1669 	sendrsp = true;
1670 	spin_lock_irqsave(&rport->lock, flags);
1671 	list_for_each_entry(lsop, &rport->ls_rcv_list, lsrcv_list) {
1672 		if (lsop->handled)
1673 			continue;
1674 
1675 		lsop->handled = true;
1676 		if (rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
1677 			spin_unlock_irqrestore(&rport->lock, flags);
1678 			sendrsp = nvme_fc_handle_ls_rqst(lsop);
1679 		} else {
1680 			spin_unlock_irqrestore(&rport->lock, flags);
1681 			w0 = &lsop->rqstbuf->w0;
1682 			lsop->lsrsp->rsplen = nvme_fc_format_rjt(
1683 						lsop->rspbuf,
1684 						sizeof(*lsop->rspbuf),
1685 						w0->ls_cmd,
1686 						FCNVME_RJT_RC_UNAB,
1687 						FCNVME_RJT_EXP_NONE, 0);
1688 		}
1689 		if (sendrsp)
1690 			nvme_fc_xmt_ls_rsp(lsop);
1691 		goto restart;
1692 	}
1693 	spin_unlock_irqrestore(&rport->lock, flags);
1694 }
1695 
1696 static
1697 void nvme_fc_rcv_ls_req_err_msg(struct nvme_fc_lport *lport,
1698 				struct fcnvme_ls_rqst_w0 *w0)
1699 {
1700 	dev_info(lport->dev, "RCV %s LS failed: No memory\n",
1701 		(w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1702 			nvmefc_ls_names[w0->ls_cmd] : "");
1703 }
1704 
1705 /**
1706  * nvme_fc_rcv_ls_req - transport entry point called by an LLDD
1707  *                       upon the reception of a NVME LS request.
1708  *
1709  * The nvme-fc layer will copy payload to an internal structure for
1710  * processing.  As such, upon completion of the routine, the LLDD may
1711  * immediately free/reuse the LS request buffer passed in the call.
1712  *
1713  * If this routine returns error, the LLDD should abort the exchange.
1714  *
1715  * @portptr:    pointer to the (registered) remote port that the LS
1716  *              was received from. The remoteport is associated with
1717  *              a specific localport.
1718  * @lsrsp:      pointer to a nvmefc_ls_rsp response structure to be
1719  *              used to reference the exchange corresponding to the LS
1720  *              when issuing an ls response.
1721  * @lsreqbuf:   pointer to the buffer containing the LS Request
1722  * @lsreqbuf_len: length, in bytes, of the received LS request
1723  */
1724 int
1725 nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr,
1726 			struct nvmefc_ls_rsp *lsrsp,
1727 			void *lsreqbuf, u32 lsreqbuf_len)
1728 {
1729 	struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
1730 	struct nvme_fc_lport *lport = rport->lport;
1731 	struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf;
1732 	struct nvmefc_ls_rcv_op *lsop;
1733 	unsigned long flags;
1734 	int ret;
1735 
1736 	nvme_fc_rport_get(rport);
1737 
1738 	/* validate there's a routine to transmit a response */
1739 	if (!lport->ops->xmt_ls_rsp) {
1740 		dev_info(lport->dev,
1741 			"RCV %s LS failed: no LLDD xmt_ls_rsp\n",
1742 			(w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1743 				nvmefc_ls_names[w0->ls_cmd] : "");
1744 		ret = -EINVAL;
1745 		goto out_put;
1746 	}
1747 
1748 	if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) {
1749 		dev_info(lport->dev,
1750 			"RCV %s LS failed: payload too large\n",
1751 			(w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1752 				nvmefc_ls_names[w0->ls_cmd] : "");
1753 		ret = -E2BIG;
1754 		goto out_put;
1755 	}
1756 
1757 	lsop = kzalloc(sizeof(*lsop), GFP_KERNEL);
1758 	if (!lsop) {
1759 		nvme_fc_rcv_ls_req_err_msg(lport, w0);
1760 		ret = -ENOMEM;
1761 		goto out_put;
1762 	}
1763 
1764 	lsop->rqstbuf = kzalloc(sizeof(*lsop->rqstbuf), GFP_KERNEL);
1765 	lsop->rspbuf = kzalloc(sizeof(*lsop->rspbuf), GFP_KERNEL);
1766 	if (!lsop->rqstbuf || !lsop->rspbuf) {
1767 		nvme_fc_rcv_ls_req_err_msg(lport, w0);
1768 		ret = -ENOMEM;
1769 		goto out_free;
1770 	}
1771 
1772 	lsop->rspdma = fc_dma_map_single(lport->dev, lsop->rspbuf,
1773 					sizeof(*lsop->rspbuf),
1774 					DMA_TO_DEVICE);
1775 	if (fc_dma_mapping_error(lport->dev, lsop->rspdma)) {
1776 		dev_info(lport->dev,
1777 			"RCV %s LS failed: DMA mapping failure\n",
1778 			(w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
1779 				nvmefc_ls_names[w0->ls_cmd] : "");
1780 		ret = -EFAULT;
1781 		goto out_free;
1782 	}
1783 
1784 	lsop->rport = rport;
1785 	lsop->lsrsp = lsrsp;
1786 
1787 	memcpy(lsop->rqstbuf, lsreqbuf, lsreqbuf_len);
1788 	lsop->rqstdatalen = lsreqbuf_len;
1789 
1790 	spin_lock_irqsave(&rport->lock, flags);
1791 	if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) {
1792 		spin_unlock_irqrestore(&rport->lock, flags);
1793 		ret = -ENOTCONN;
1794 		goto out_unmap;
1795 	}
1796 	list_add_tail(&lsop->lsrcv_list, &rport->ls_rcv_list);
1797 	spin_unlock_irqrestore(&rport->lock, flags);
1798 
1799 	schedule_work(&rport->lsrcv_work);
1800 
1801 	return 0;
1802 
1803 out_unmap:
1804 	fc_dma_unmap_single(lport->dev, lsop->rspdma,
1805 			sizeof(*lsop->rspbuf), DMA_TO_DEVICE);
1806 out_free:
1807 	kfree(lsop->rspbuf);
1808 	kfree(lsop->rqstbuf);
1809 	kfree(lsop);
1810 out_put:
1811 	nvme_fc_rport_put(rport);
1812 	return ret;
1813 }
1814 EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req);
1815 
1816 
1817 /* *********************** NVME Ctrl Routines **************************** */
1818 
1819 static void
1820 __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
1821 		struct nvme_fc_fcp_op *op)
1822 {
1823 	fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
1824 				sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1825 	fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
1826 				sizeof(op->cmd_iu), DMA_TO_DEVICE);
1827 
1828 	atomic_set(&op->state, FCPOP_STATE_UNINIT);
1829 }
1830 
1831 static void
1832 nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq,
1833 		unsigned int hctx_idx)
1834 {
1835 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
1836 
1837 	return __nvme_fc_exit_request(to_fc_ctrl(set->driver_data), op);
1838 }
1839 
1840 static int
1841 __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
1842 {
1843 	unsigned long flags;
1844 	int opstate;
1845 
1846 	spin_lock_irqsave(&ctrl->lock, flags);
1847 	opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
1848 	if (opstate != FCPOP_STATE_ACTIVE)
1849 		atomic_set(&op->state, opstate);
1850 	else if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) {
1851 		op->flags |= FCOP_FLAGS_TERMIO;
1852 		ctrl->iocnt++;
1853 	}
1854 	spin_unlock_irqrestore(&ctrl->lock, flags);
1855 
1856 	if (opstate != FCPOP_STATE_ACTIVE)
1857 		return -ECANCELED;
1858 
1859 	ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
1860 					&ctrl->rport->remoteport,
1861 					op->queue->lldd_handle,
1862 					&op->fcp_req);
1863 
1864 	return 0;
1865 }
1866 
1867 static void
1868 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
1869 {
1870 	struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
1871 	int i;
1872 
1873 	/* ensure we've initialized the ops once */
1874 	if (!(aen_op->flags & FCOP_FLAGS_AEN))
1875 		return;
1876 
1877 	for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++)
1878 		__nvme_fc_abort_op(ctrl, aen_op);
1879 }
1880 
1881 static inline void
1882 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
1883 		struct nvme_fc_fcp_op *op, int opstate)
1884 {
1885 	unsigned long flags;
1886 
1887 	if (opstate == FCPOP_STATE_ABORTED) {
1888 		spin_lock_irqsave(&ctrl->lock, flags);
1889 		if (test_bit(FCCTRL_TERMIO, &ctrl->flags) &&
1890 		    op->flags & FCOP_FLAGS_TERMIO) {
1891 			if (!--ctrl->iocnt)
1892 				wake_up(&ctrl->ioabort_wait);
1893 		}
1894 		spin_unlock_irqrestore(&ctrl->lock, flags);
1895 	}
1896 }
1897 
1898 static void
1899 nvme_fc_ctrl_ioerr_work(struct work_struct *work)
1900 {
1901 	struct nvme_fc_ctrl *ctrl =
1902 			container_of(work, struct nvme_fc_ctrl, ioerr_work);
1903 
1904 	nvme_fc_error_recovery(ctrl, "transport detected io error");
1905 }
1906 
1907 /*
1908  * nvme_fc_io_getuuid - Routine called to get the appid field
1909  * associated with request by the lldd
1910  * @req:IO request from nvme fc to driver
1911  * Returns: UUID if there is an appid associated with VM or
1912  * NULL if the user/libvirt has not set the appid to VM
1913  */
1914 char *nvme_fc_io_getuuid(struct nvmefc_fcp_req *req)
1915 {
1916 	struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1917 	struct request *rq = op->rq;
1918 
1919 	if (!IS_ENABLED(CONFIG_BLK_CGROUP_FC_APPID) || !rq || !rq->bio)
1920 		return NULL;
1921 	return blkcg_get_fc_appid(rq->bio);
1922 }
1923 EXPORT_SYMBOL_GPL(nvme_fc_io_getuuid);
1924 
1925 static void
1926 nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
1927 {
1928 	struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
1929 	struct request *rq = op->rq;
1930 	struct nvmefc_fcp_req *freq = &op->fcp_req;
1931 	struct nvme_fc_ctrl *ctrl = op->ctrl;
1932 	struct nvme_fc_queue *queue = op->queue;
1933 	struct nvme_completion *cqe = &op->rsp_iu.cqe;
1934 	struct nvme_command *sqe = &op->cmd_iu.sqe;
1935 	__le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
1936 	union nvme_result result;
1937 	bool terminate_assoc = true;
1938 	int opstate;
1939 
1940 	/*
1941 	 * WARNING:
1942 	 * The current linux implementation of a nvme controller
1943 	 * allocates a single tag set for all io queues and sizes
1944 	 * the io queues to fully hold all possible tags. Thus, the
1945 	 * implementation does not reference or care about the sqhd
1946 	 * value as it never needs to use the sqhd/sqtail pointers
1947 	 * for submission pacing.
1948 	 *
1949 	 * This affects the FC-NVME implementation in two ways:
1950 	 * 1) As the value doesn't matter, we don't need to waste
1951 	 *    cycles extracting it from ERSPs and stamping it in the
1952 	 *    cases where the transport fabricates CQEs on successful
1953 	 *    completions.
1954 	 * 2) The FC-NVME implementation requires that delivery of
1955 	 *    ERSP completions are to go back to the nvme layer in order
1956 	 *    relative to the rsn, such that the sqhd value will always
1957 	 *    be "in order" for the nvme layer. As the nvme layer in
1958 	 *    linux doesn't care about sqhd, there's no need to return
1959 	 *    them in order.
1960 	 *
1961 	 * Additionally:
1962 	 * As the core nvme layer in linux currently does not look at
1963 	 * every field in the cqe - in cases where the FC transport must
1964 	 * fabricate a CQE, the following fields will not be set as they
1965 	 * are not referenced:
1966 	 *      cqe.sqid,  cqe.sqhd,  cqe.command_id
1967 	 *
1968 	 * Failure or error of an individual i/o, in a transport
1969 	 * detected fashion unrelated to the nvme completion status,
1970 	 * potentially cause the initiator and target sides to get out
1971 	 * of sync on SQ head/tail (aka outstanding io count allowed).
1972 	 * Per FC-NVME spec, failure of an individual command requires
1973 	 * the connection to be terminated, which in turn requires the
1974 	 * association to be terminated.
1975 	 */
1976 
1977 	opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
1978 
1979 	fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
1980 				sizeof(op->rsp_iu), DMA_FROM_DEVICE);
1981 
1982 	if (opstate == FCPOP_STATE_ABORTED)
1983 		status = cpu_to_le16(NVME_SC_HOST_ABORTED_CMD << 1);
1984 	else if (freq->status) {
1985 		status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
1986 		dev_info(ctrl->ctrl.device,
1987 			"NVME-FC{%d}: io failed due to lldd error %d\n",
1988 			ctrl->cnum, freq->status);
1989 	}
1990 
1991 	/*
1992 	 * For the linux implementation, if we have an unsuccesful
1993 	 * status, they blk-mq layer can typically be called with the
1994 	 * non-zero status and the content of the cqe isn't important.
1995 	 */
1996 	if (status)
1997 		goto done;
1998 
1999 	/*
2000 	 * command completed successfully relative to the wire
2001 	 * protocol. However, validate anything received and
2002 	 * extract the status and result from the cqe (create it
2003 	 * where necessary).
2004 	 */
2005 
2006 	switch (freq->rcv_rsplen) {
2007 
2008 	case 0:
2009 	case NVME_FC_SIZEOF_ZEROS_RSP:
2010 		/*
2011 		 * No response payload or 12 bytes of payload (which
2012 		 * should all be zeros) are considered successful and
2013 		 * no payload in the CQE by the transport.
2014 		 */
2015 		if (freq->transferred_length !=
2016 		    be32_to_cpu(op->cmd_iu.data_len)) {
2017 			status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2018 			dev_info(ctrl->ctrl.device,
2019 				"NVME-FC{%d}: io failed due to bad transfer "
2020 				"length: %d vs expected %d\n",
2021 				ctrl->cnum, freq->transferred_length,
2022 				be32_to_cpu(op->cmd_iu.data_len));
2023 			goto done;
2024 		}
2025 		result.u64 = 0;
2026 		break;
2027 
2028 	case sizeof(struct nvme_fc_ersp_iu):
2029 		/*
2030 		 * The ERSP IU contains a full completion with CQE.
2031 		 * Validate ERSP IU and look at cqe.
2032 		 */
2033 		if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
2034 					(freq->rcv_rsplen / 4) ||
2035 			     be32_to_cpu(op->rsp_iu.xfrd_len) !=
2036 					freq->transferred_length ||
2037 			     op->rsp_iu.ersp_result ||
2038 			     sqe->common.command_id != cqe->command_id)) {
2039 			status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2040 			dev_info(ctrl->ctrl.device,
2041 				"NVME-FC{%d}: io failed due to bad NVMe_ERSP: "
2042 				"iu len %d, xfr len %d vs %d, status code "
2043 				"%d, cmdid %d vs %d\n",
2044 				ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len),
2045 				be32_to_cpu(op->rsp_iu.xfrd_len),
2046 				freq->transferred_length,
2047 				op->rsp_iu.ersp_result,
2048 				sqe->common.command_id,
2049 				cqe->command_id);
2050 			goto done;
2051 		}
2052 		result = cqe->result;
2053 		status = cqe->status;
2054 		break;
2055 
2056 	default:
2057 		status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1);
2058 		dev_info(ctrl->ctrl.device,
2059 			"NVME-FC{%d}: io failed due to odd NVMe_xRSP iu "
2060 			"len %d\n",
2061 			ctrl->cnum, freq->rcv_rsplen);
2062 		goto done;
2063 	}
2064 
2065 	terminate_assoc = false;
2066 
2067 done:
2068 	if (op->flags & FCOP_FLAGS_AEN) {
2069 		nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
2070 		__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2071 		atomic_set(&op->state, FCPOP_STATE_IDLE);
2072 		op->flags = FCOP_FLAGS_AEN;	/* clear other flags */
2073 		nvme_fc_ctrl_put(ctrl);
2074 		goto check_error;
2075 	}
2076 
2077 	__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2078 	if (!nvme_try_complete_req(rq, status, result))
2079 		nvme_fc_complete_rq(rq);
2080 
2081 check_error:
2082 	if (terminate_assoc && ctrl->ctrl.state != NVME_CTRL_RESETTING)
2083 		queue_work(nvme_reset_wq, &ctrl->ioerr_work);
2084 }
2085 
2086 static int
2087 __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
2088 		struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
2089 		struct request *rq, u32 rqno)
2090 {
2091 	struct nvme_fcp_op_w_sgl *op_w_sgl =
2092 		container_of(op, typeof(*op_w_sgl), op);
2093 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2094 	int ret = 0;
2095 
2096 	memset(op, 0, sizeof(*op));
2097 	op->fcp_req.cmdaddr = &op->cmd_iu;
2098 	op->fcp_req.cmdlen = sizeof(op->cmd_iu);
2099 	op->fcp_req.rspaddr = &op->rsp_iu;
2100 	op->fcp_req.rsplen = sizeof(op->rsp_iu);
2101 	op->fcp_req.done = nvme_fc_fcpio_done;
2102 	op->ctrl = ctrl;
2103 	op->queue = queue;
2104 	op->rq = rq;
2105 	op->rqno = rqno;
2106 
2107 	cmdiu->format_id = NVME_CMD_FORMAT_ID;
2108 	cmdiu->fc_id = NVME_CMD_FC_ID;
2109 	cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
2110 	if (queue->qnum)
2111 		cmdiu->rsv_cat = fccmnd_set_cat_css(0,
2112 					(NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT));
2113 	else
2114 		cmdiu->rsv_cat = fccmnd_set_cat_admin(0);
2115 
2116 	op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
2117 				&op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
2118 	if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
2119 		dev_err(ctrl->dev,
2120 			"FCP Op failed - cmdiu dma mapping failed.\n");
2121 		ret = -EFAULT;
2122 		goto out_on_error;
2123 	}
2124 
2125 	op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
2126 				&op->rsp_iu, sizeof(op->rsp_iu),
2127 				DMA_FROM_DEVICE);
2128 	if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
2129 		dev_err(ctrl->dev,
2130 			"FCP Op failed - rspiu dma mapping failed.\n");
2131 		ret = -EFAULT;
2132 	}
2133 
2134 	atomic_set(&op->state, FCPOP_STATE_IDLE);
2135 out_on_error:
2136 	return ret;
2137 }
2138 
2139 static int
2140 nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq,
2141 		unsigned int hctx_idx, unsigned int numa_node)
2142 {
2143 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
2144 	struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq);
2145 	int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
2146 	struct nvme_fc_queue *queue = &ctrl->queues[queue_idx];
2147 	int res;
2148 
2149 	res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++);
2150 	if (res)
2151 		return res;
2152 	op->op.fcp_req.first_sgl = op->sgl;
2153 	op->op.fcp_req.private = &op->priv[0];
2154 	nvme_req(rq)->ctrl = &ctrl->ctrl;
2155 	nvme_req(rq)->cmd = &op->op.cmd_iu.sqe;
2156 	return res;
2157 }
2158 
2159 static int
2160 nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
2161 {
2162 	struct nvme_fc_fcp_op *aen_op;
2163 	struct nvme_fc_cmd_iu *cmdiu;
2164 	struct nvme_command *sqe;
2165 	void *private = NULL;
2166 	int i, ret;
2167 
2168 	aen_op = ctrl->aen_ops;
2169 	for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2170 		if (ctrl->lport->ops->fcprqst_priv_sz) {
2171 			private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz,
2172 						GFP_KERNEL);
2173 			if (!private)
2174 				return -ENOMEM;
2175 		}
2176 
2177 		cmdiu = &aen_op->cmd_iu;
2178 		sqe = &cmdiu->sqe;
2179 		ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
2180 				aen_op, (struct request *)NULL,
2181 				(NVME_AQ_BLK_MQ_DEPTH + i));
2182 		if (ret) {
2183 			kfree(private);
2184 			return ret;
2185 		}
2186 
2187 		aen_op->flags = FCOP_FLAGS_AEN;
2188 		aen_op->fcp_req.private = private;
2189 
2190 		memset(sqe, 0, sizeof(*sqe));
2191 		sqe->common.opcode = nvme_admin_async_event;
2192 		/* Note: core layer may overwrite the sqe.command_id value */
2193 		sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i;
2194 	}
2195 	return 0;
2196 }
2197 
2198 static void
2199 nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl)
2200 {
2201 	struct nvme_fc_fcp_op *aen_op;
2202 	int i;
2203 
2204 	cancel_work_sync(&ctrl->ctrl.async_event_work);
2205 	aen_op = ctrl->aen_ops;
2206 	for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) {
2207 		__nvme_fc_exit_request(ctrl, aen_op);
2208 
2209 		kfree(aen_op->fcp_req.private);
2210 		aen_op->fcp_req.private = NULL;
2211 	}
2212 }
2213 
2214 static inline int
2215 __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int qidx)
2216 {
2217 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(data);
2218 	struct nvme_fc_queue *queue = &ctrl->queues[qidx];
2219 
2220 	hctx->driver_data = queue;
2221 	queue->hctx = hctx;
2222 	return 0;
2223 }
2224 
2225 static int
2226 nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int hctx_idx)
2227 {
2228 	return __nvme_fc_init_hctx(hctx, data, hctx_idx + 1);
2229 }
2230 
2231 static int
2232 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
2233 		unsigned int hctx_idx)
2234 {
2235 	return __nvme_fc_init_hctx(hctx, data, hctx_idx);
2236 }
2237 
2238 static void
2239 nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx)
2240 {
2241 	struct nvme_fc_queue *queue;
2242 
2243 	queue = &ctrl->queues[idx];
2244 	memset(queue, 0, sizeof(*queue));
2245 	queue->ctrl = ctrl;
2246 	queue->qnum = idx;
2247 	atomic_set(&queue->csn, 0);
2248 	queue->dev = ctrl->dev;
2249 
2250 	if (idx > 0)
2251 		queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
2252 	else
2253 		queue->cmnd_capsule_len = sizeof(struct nvme_command);
2254 
2255 	/*
2256 	 * Considered whether we should allocate buffers for all SQEs
2257 	 * and CQEs and dma map them - mapping their respective entries
2258 	 * into the request structures (kernel vm addr and dma address)
2259 	 * thus the driver could use the buffers/mappings directly.
2260 	 * It only makes sense if the LLDD would use them for its
2261 	 * messaging api. It's very unlikely most adapter api's would use
2262 	 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
2263 	 * structures were used instead.
2264 	 */
2265 }
2266 
2267 /*
2268  * This routine terminates a queue at the transport level.
2269  * The transport has already ensured that all outstanding ios on
2270  * the queue have been terminated.
2271  * The transport will send a Disconnect LS request to terminate
2272  * the queue's connection. Termination of the admin queue will also
2273  * terminate the association at the target.
2274  */
2275 static void
2276 nvme_fc_free_queue(struct nvme_fc_queue *queue)
2277 {
2278 	if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
2279 		return;
2280 
2281 	clear_bit(NVME_FC_Q_LIVE, &queue->flags);
2282 	/*
2283 	 * Current implementation never disconnects a single queue.
2284 	 * It always terminates a whole association. So there is never
2285 	 * a disconnect(queue) LS sent to the target.
2286 	 */
2287 
2288 	queue->connection_id = 0;
2289 	atomic_set(&queue->csn, 0);
2290 }
2291 
2292 static void
2293 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
2294 	struct nvme_fc_queue *queue, unsigned int qidx)
2295 {
2296 	if (ctrl->lport->ops->delete_queue)
2297 		ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
2298 				queue->lldd_handle);
2299 	queue->lldd_handle = NULL;
2300 }
2301 
2302 static void
2303 nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
2304 {
2305 	int i;
2306 
2307 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
2308 		nvme_fc_free_queue(&ctrl->queues[i]);
2309 }
2310 
2311 static int
2312 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
2313 	struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
2314 {
2315 	int ret = 0;
2316 
2317 	queue->lldd_handle = NULL;
2318 	if (ctrl->lport->ops->create_queue)
2319 		ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
2320 				qidx, qsize, &queue->lldd_handle);
2321 
2322 	return ret;
2323 }
2324 
2325 static void
2326 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
2327 {
2328 	struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1];
2329 	int i;
2330 
2331 	for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--)
2332 		__nvme_fc_delete_hw_queue(ctrl, queue, i);
2333 }
2334 
2335 static int
2336 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2337 {
2338 	struct nvme_fc_queue *queue = &ctrl->queues[1];
2339 	int i, ret;
2340 
2341 	for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) {
2342 		ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
2343 		if (ret)
2344 			goto delete_queues;
2345 	}
2346 
2347 	return 0;
2348 
2349 delete_queues:
2350 	for (; i > 0; i--)
2351 		__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
2352 	return ret;
2353 }
2354 
2355 static int
2356 nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
2357 {
2358 	int i, ret = 0;
2359 
2360 	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
2361 		ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
2362 					(qsize / 5));
2363 		if (ret)
2364 			break;
2365 		ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
2366 		if (ret)
2367 			break;
2368 
2369 		set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags);
2370 	}
2371 
2372 	return ret;
2373 }
2374 
2375 static void
2376 nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
2377 {
2378 	int i;
2379 
2380 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
2381 		nvme_fc_init_queue(ctrl, i);
2382 }
2383 
2384 static void
2385 nvme_fc_ctrl_free(struct kref *ref)
2386 {
2387 	struct nvme_fc_ctrl *ctrl =
2388 		container_of(ref, struct nvme_fc_ctrl, ref);
2389 	unsigned long flags;
2390 
2391 	if (ctrl->ctrl.tagset)
2392 		nvme_remove_io_tag_set(&ctrl->ctrl);
2393 
2394 	/* remove from rport list */
2395 	spin_lock_irqsave(&ctrl->rport->lock, flags);
2396 	list_del(&ctrl->ctrl_list);
2397 	spin_unlock_irqrestore(&ctrl->rport->lock, flags);
2398 
2399 	nvme_unquiesce_admin_queue(&ctrl->ctrl);
2400 	nvme_remove_admin_tag_set(&ctrl->ctrl);
2401 
2402 	kfree(ctrl->queues);
2403 
2404 	put_device(ctrl->dev);
2405 	nvme_fc_rport_put(ctrl->rport);
2406 
2407 	ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
2408 	if (ctrl->ctrl.opts)
2409 		nvmf_free_options(ctrl->ctrl.opts);
2410 	kfree(ctrl);
2411 }
2412 
2413 static void
2414 nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
2415 {
2416 	kref_put(&ctrl->ref, nvme_fc_ctrl_free);
2417 }
2418 
2419 static int
2420 nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
2421 {
2422 	return kref_get_unless_zero(&ctrl->ref);
2423 }
2424 
2425 /*
2426  * All accesses from nvme core layer done - can now free the
2427  * controller. Called after last nvme_put_ctrl() call
2428  */
2429 static void
2430 nvme_fc_free_ctrl(struct nvme_ctrl *nctrl)
2431 {
2432 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2433 
2434 	WARN_ON(nctrl != &ctrl->ctrl);
2435 
2436 	nvme_fc_ctrl_put(ctrl);
2437 }
2438 
2439 /*
2440  * This routine is used by the transport when it needs to find active
2441  * io on a queue that is to be terminated. The transport uses
2442  * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
2443  * this routine to kill them on a 1 by 1 basis.
2444  *
2445  * As FC allocates FC exchange for each io, the transport must contact
2446  * the LLDD to terminate the exchange, thus releasing the FC exchange.
2447  * After terminating the exchange the LLDD will call the transport's
2448  * normal io done path for the request, but it will have an aborted
2449  * status. The done path will return the io request back to the block
2450  * layer with an error status.
2451  */
2452 static bool nvme_fc_terminate_exchange(struct request *req, void *data)
2453 {
2454 	struct nvme_ctrl *nctrl = data;
2455 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
2456 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
2457 
2458 	op->nreq.flags |= NVME_REQ_CANCELLED;
2459 	__nvme_fc_abort_op(ctrl, op);
2460 	return true;
2461 }
2462 
2463 /*
2464  * This routine runs through all outstanding commands on the association
2465  * and aborts them.  This routine is typically be called by the
2466  * delete_association routine. It is also called due to an error during
2467  * reconnect. In that scenario, it is most likely a command that initializes
2468  * the controller, including fabric Connect commands on io queues, that
2469  * may have timed out or failed thus the io must be killed for the connect
2470  * thread to see the error.
2471  */
2472 static void
2473 __nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl *ctrl, bool start_queues)
2474 {
2475 	int q;
2476 
2477 	/*
2478 	 * if aborting io, the queues are no longer good, mark them
2479 	 * all as not live.
2480 	 */
2481 	if (ctrl->ctrl.queue_count > 1) {
2482 		for (q = 1; q < ctrl->ctrl.queue_count; q++)
2483 			clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[q].flags);
2484 	}
2485 	clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
2486 
2487 	/*
2488 	 * If io queues are present, stop them and terminate all outstanding
2489 	 * ios on them. As FC allocates FC exchange for each io, the
2490 	 * transport must contact the LLDD to terminate the exchange,
2491 	 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
2492 	 * to tell us what io's are busy and invoke a transport routine
2493 	 * to kill them with the LLDD.  After terminating the exchange
2494 	 * the LLDD will call the transport's normal io done path, but it
2495 	 * will have an aborted status. The done path will return the
2496 	 * io requests back to the block layer as part of normal completions
2497 	 * (but with error status).
2498 	 */
2499 	if (ctrl->ctrl.queue_count > 1) {
2500 		nvme_quiesce_io_queues(&ctrl->ctrl);
2501 		nvme_sync_io_queues(&ctrl->ctrl);
2502 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
2503 				nvme_fc_terminate_exchange, &ctrl->ctrl);
2504 		blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
2505 		if (start_queues)
2506 			nvme_unquiesce_io_queues(&ctrl->ctrl);
2507 	}
2508 
2509 	/*
2510 	 * Other transports, which don't have link-level contexts bound
2511 	 * to sqe's, would try to gracefully shutdown the controller by
2512 	 * writing the registers for shutdown and polling (call
2513 	 * nvme_disable_ctrl()). Given a bunch of i/o was potentially
2514 	 * just aborted and we will wait on those contexts, and given
2515 	 * there was no indication of how live the controlelr is on the
2516 	 * link, don't send more io to create more contexts for the
2517 	 * shutdown. Let the controller fail via keepalive failure if
2518 	 * its still present.
2519 	 */
2520 
2521 	/*
2522 	 * clean up the admin queue. Same thing as above.
2523 	 */
2524 	nvme_quiesce_admin_queue(&ctrl->ctrl);
2525 	blk_sync_queue(ctrl->ctrl.admin_q);
2526 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
2527 				nvme_fc_terminate_exchange, &ctrl->ctrl);
2528 	blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
2529 	if (start_queues)
2530 		nvme_unquiesce_admin_queue(&ctrl->ctrl);
2531 }
2532 
2533 static void
2534 nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
2535 {
2536 	/*
2537 	 * if an error (io timeout, etc) while (re)connecting, the remote
2538 	 * port requested terminating of the association (disconnect_ls)
2539 	 * or an error (timeout or abort) occurred on an io while creating
2540 	 * the controller.  Abort any ios on the association and let the
2541 	 * create_association error path resolve things.
2542 	 */
2543 	if (ctrl->ctrl.state == NVME_CTRL_CONNECTING) {
2544 		__nvme_fc_abort_outstanding_ios(ctrl, true);
2545 		set_bit(ASSOC_FAILED, &ctrl->flags);
2546 		dev_warn(ctrl->ctrl.device,
2547 			"NVME-FC{%d}: transport error during (re)connect\n",
2548 			ctrl->cnum);
2549 		return;
2550 	}
2551 
2552 	/* Otherwise, only proceed if in LIVE state - e.g. on first error */
2553 	if (ctrl->ctrl.state != NVME_CTRL_LIVE)
2554 		return;
2555 
2556 	dev_warn(ctrl->ctrl.device,
2557 		"NVME-FC{%d}: transport association event: %s\n",
2558 		ctrl->cnum, errmsg);
2559 	dev_warn(ctrl->ctrl.device,
2560 		"NVME-FC{%d}: resetting controller\n", ctrl->cnum);
2561 
2562 	nvme_reset_ctrl(&ctrl->ctrl);
2563 }
2564 
2565 static enum blk_eh_timer_return nvme_fc_timeout(struct request *rq)
2566 {
2567 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2568 	struct nvme_fc_ctrl *ctrl = op->ctrl;
2569 	u16 qnum = op->queue->qnum;
2570 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2571 	struct nvme_command *sqe = &cmdiu->sqe;
2572 
2573 	/*
2574 	 * Attempt to abort the offending command. Command completion
2575 	 * will detect the aborted io and will fail the connection.
2576 	 */
2577 	dev_info(ctrl->ctrl.device,
2578 		"NVME-FC{%d.%d}: io timeout: opcode %d fctype %d (%s) w10/11: "
2579 		"x%08x/x%08x\n",
2580 		ctrl->cnum, qnum, sqe->common.opcode, sqe->fabrics.fctype,
2581 		nvme_fabrics_opcode_str(qnum, sqe),
2582 		sqe->common.cdw10, sqe->common.cdw11);
2583 	if (__nvme_fc_abort_op(ctrl, op))
2584 		nvme_fc_error_recovery(ctrl, "io timeout abort failed");
2585 
2586 	/*
2587 	 * the io abort has been initiated. Have the reset timer
2588 	 * restarted and the abort completion will complete the io
2589 	 * shortly. Avoids a synchronous wait while the abort finishes.
2590 	 */
2591 	return BLK_EH_RESET_TIMER;
2592 }
2593 
2594 static int
2595 nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2596 		struct nvme_fc_fcp_op *op)
2597 {
2598 	struct nvmefc_fcp_req *freq = &op->fcp_req;
2599 	int ret;
2600 
2601 	freq->sg_cnt = 0;
2602 
2603 	if (!blk_rq_nr_phys_segments(rq))
2604 		return 0;
2605 
2606 	freq->sg_table.sgl = freq->first_sgl;
2607 	ret = sg_alloc_table_chained(&freq->sg_table,
2608 			blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
2609 			NVME_INLINE_SG_CNT);
2610 	if (ret)
2611 		return -ENOMEM;
2612 
2613 	op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
2614 	WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2615 	freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2616 				op->nents, rq_dma_dir(rq));
2617 	if (unlikely(freq->sg_cnt <= 0)) {
2618 		sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2619 		freq->sg_cnt = 0;
2620 		return -EFAULT;
2621 	}
2622 
2623 	/*
2624 	 * TODO: blk_integrity_rq(rq)  for DIF
2625 	 */
2626 	return 0;
2627 }
2628 
2629 static void
2630 nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2631 		struct nvme_fc_fcp_op *op)
2632 {
2633 	struct nvmefc_fcp_req *freq = &op->fcp_req;
2634 
2635 	if (!freq->sg_cnt)
2636 		return;
2637 
2638 	fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2639 			rq_dma_dir(rq));
2640 
2641 	sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2642 
2643 	freq->sg_cnt = 0;
2644 }
2645 
2646 /*
2647  * In FC, the queue is a logical thing. At transport connect, the target
2648  * creates its "queue" and returns a handle that is to be given to the
2649  * target whenever it posts something to the corresponding SQ.  When an
2650  * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2651  * command contained within the SQE, an io, and assigns a FC exchange
2652  * to it. The SQE and the associated SQ handle are sent in the initial
2653  * CMD IU sents on the exchange. All transfers relative to the io occur
2654  * as part of the exchange.  The CQE is the last thing for the io,
2655  * which is transferred (explicitly or implicitly) with the RSP IU
2656  * sent on the exchange. After the CQE is received, the FC exchange is
2657  * terminaed and the Exchange may be used on a different io.
2658  *
2659  * The transport to LLDD api has the transport making a request for a
2660  * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2661  * resource and transfers the command. The LLDD will then process all
2662  * steps to complete the io. Upon completion, the transport done routine
2663  * is called.
2664  *
2665  * So - while the operation is outstanding to the LLDD, there is a link
2666  * level FC exchange resource that is also outstanding. This must be
2667  * considered in all cleanup operations.
2668  */
2669 static blk_status_t
2670 nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2671 	struct nvme_fc_fcp_op *op, u32 data_len,
2672 	enum nvmefc_fcp_datadir	io_dir)
2673 {
2674 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2675 	struct nvme_command *sqe = &cmdiu->sqe;
2676 	int ret, opstate;
2677 
2678 	/*
2679 	 * before attempting to send the io, check to see if we believe
2680 	 * the target device is present
2681 	 */
2682 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2683 		return BLK_STS_RESOURCE;
2684 
2685 	if (!nvme_fc_ctrl_get(ctrl))
2686 		return BLK_STS_IOERR;
2687 
2688 	/* format the FC-NVME CMD IU and fcp_req */
2689 	cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2690 	cmdiu->data_len = cpu_to_be32(data_len);
2691 	switch (io_dir) {
2692 	case NVMEFC_FCP_WRITE:
2693 		cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2694 		break;
2695 	case NVMEFC_FCP_READ:
2696 		cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2697 		break;
2698 	case NVMEFC_FCP_NODATA:
2699 		cmdiu->flags = 0;
2700 		break;
2701 	}
2702 	op->fcp_req.payload_length = data_len;
2703 	op->fcp_req.io_dir = io_dir;
2704 	op->fcp_req.transferred_length = 0;
2705 	op->fcp_req.rcv_rsplen = 0;
2706 	op->fcp_req.status = NVME_SC_SUCCESS;
2707 	op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2708 
2709 	/*
2710 	 * validate per fabric rules, set fields mandated by fabric spec
2711 	 * as well as those by FC-NVME spec.
2712 	 */
2713 	WARN_ON_ONCE(sqe->common.metadata);
2714 	sqe->common.flags |= NVME_CMD_SGL_METABUF;
2715 
2716 	/*
2717 	 * format SQE DPTR field per FC-NVME rules:
2718 	 *    type=0x5     Transport SGL Data Block Descriptor
2719 	 *    subtype=0xA  Transport-specific value
2720 	 *    address=0
2721 	 *    length=length of the data series
2722 	 */
2723 	sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2724 					NVME_SGL_FMT_TRANSPORT_A;
2725 	sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2726 	sqe->rw.dptr.sgl.addr = 0;
2727 
2728 	if (!(op->flags & FCOP_FLAGS_AEN)) {
2729 		ret = nvme_fc_map_data(ctrl, op->rq, op);
2730 		if (ret < 0) {
2731 			nvme_cleanup_cmd(op->rq);
2732 			nvme_fc_ctrl_put(ctrl);
2733 			if (ret == -ENOMEM || ret == -EAGAIN)
2734 				return BLK_STS_RESOURCE;
2735 			return BLK_STS_IOERR;
2736 		}
2737 	}
2738 
2739 	fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2740 				  sizeof(op->cmd_iu), DMA_TO_DEVICE);
2741 
2742 	atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2743 
2744 	if (!(op->flags & FCOP_FLAGS_AEN))
2745 		nvme_start_request(op->rq);
2746 
2747 	cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2748 	ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2749 					&ctrl->rport->remoteport,
2750 					queue->lldd_handle, &op->fcp_req);
2751 
2752 	if (ret) {
2753 		/*
2754 		 * If the lld fails to send the command is there an issue with
2755 		 * the csn value?  If the command that fails is the Connect,
2756 		 * no - as the connection won't be live.  If it is a command
2757 		 * post-connect, it's possible a gap in csn may be created.
2758 		 * Does this matter?  As Linux initiators don't send fused
2759 		 * commands, no.  The gap would exist, but as there's nothing
2760 		 * that depends on csn order to be delivered on the target
2761 		 * side, it shouldn't hurt.  It would be difficult for a
2762 		 * target to even detect the csn gap as it has no idea when the
2763 		 * cmd with the csn was supposed to arrive.
2764 		 */
2765 		opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2766 		__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2767 
2768 		if (!(op->flags & FCOP_FLAGS_AEN)) {
2769 			nvme_fc_unmap_data(ctrl, op->rq, op);
2770 			nvme_cleanup_cmd(op->rq);
2771 		}
2772 
2773 		nvme_fc_ctrl_put(ctrl);
2774 
2775 		if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2776 				ret != -EBUSY)
2777 			return BLK_STS_IOERR;
2778 
2779 		return BLK_STS_RESOURCE;
2780 	}
2781 
2782 	return BLK_STS_OK;
2783 }
2784 
2785 static blk_status_t
2786 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2787 			const struct blk_mq_queue_data *bd)
2788 {
2789 	struct nvme_ns *ns = hctx->queue->queuedata;
2790 	struct nvme_fc_queue *queue = hctx->driver_data;
2791 	struct nvme_fc_ctrl *ctrl = queue->ctrl;
2792 	struct request *rq = bd->rq;
2793 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2794 	enum nvmefc_fcp_datadir	io_dir;
2795 	bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2796 	u32 data_len;
2797 	blk_status_t ret;
2798 
2799 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
2800 	    !nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2801 		return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
2802 
2803 	ret = nvme_setup_cmd(ns, rq);
2804 	if (ret)
2805 		return ret;
2806 
2807 	/*
2808 	 * nvme core doesn't quite treat the rq opaquely. Commands such
2809 	 * as WRITE ZEROES will return a non-zero rq payload_bytes yet
2810 	 * there is no actual payload to be transferred.
2811 	 * To get it right, key data transmission on there being 1 or
2812 	 * more physical segments in the sg list. If there is no
2813 	 * physical segments, there is no payload.
2814 	 */
2815 	if (blk_rq_nr_phys_segments(rq)) {
2816 		data_len = blk_rq_payload_bytes(rq);
2817 		io_dir = ((rq_data_dir(rq) == WRITE) ?
2818 					NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2819 	} else {
2820 		data_len = 0;
2821 		io_dir = NVMEFC_FCP_NODATA;
2822 	}
2823 
2824 
2825 	return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2826 }
2827 
2828 static void
2829 nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2830 {
2831 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2832 	struct nvme_fc_fcp_op *aen_op;
2833 	blk_status_t ret;
2834 
2835 	if (test_bit(FCCTRL_TERMIO, &ctrl->flags))
2836 		return;
2837 
2838 	aen_op = &ctrl->aen_ops[0];
2839 
2840 	ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2841 					NVMEFC_FCP_NODATA);
2842 	if (ret)
2843 		dev_err(ctrl->ctrl.device,
2844 			"failed async event work\n");
2845 }
2846 
2847 static void
2848 nvme_fc_complete_rq(struct request *rq)
2849 {
2850 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2851 	struct nvme_fc_ctrl *ctrl = op->ctrl;
2852 
2853 	atomic_set(&op->state, FCPOP_STATE_IDLE);
2854 	op->flags &= ~FCOP_FLAGS_TERMIO;
2855 
2856 	nvme_fc_unmap_data(ctrl, rq, op);
2857 	nvme_complete_rq(rq);
2858 	nvme_fc_ctrl_put(ctrl);
2859 }
2860 
2861 static void nvme_fc_map_queues(struct blk_mq_tag_set *set)
2862 {
2863 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
2864 	int i;
2865 
2866 	for (i = 0; i < set->nr_maps; i++) {
2867 		struct blk_mq_queue_map *map = &set->map[i];
2868 
2869 		if (!map->nr_queues) {
2870 			WARN_ON(i == HCTX_TYPE_DEFAULT);
2871 			continue;
2872 		}
2873 
2874 		/* Call LLDD map queue functionality if defined */
2875 		if (ctrl->lport->ops->map_queues)
2876 			ctrl->lport->ops->map_queues(&ctrl->lport->localport,
2877 						     map);
2878 		else
2879 			blk_mq_map_queues(map);
2880 	}
2881 }
2882 
2883 static const struct blk_mq_ops nvme_fc_mq_ops = {
2884 	.queue_rq	= nvme_fc_queue_rq,
2885 	.complete	= nvme_fc_complete_rq,
2886 	.init_request	= nvme_fc_init_request,
2887 	.exit_request	= nvme_fc_exit_request,
2888 	.init_hctx	= nvme_fc_init_hctx,
2889 	.timeout	= nvme_fc_timeout,
2890 	.map_queues	= nvme_fc_map_queues,
2891 };
2892 
2893 static int
2894 nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2895 {
2896 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2897 	unsigned int nr_io_queues;
2898 	int ret;
2899 
2900 	nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2901 				ctrl->lport->ops->max_hw_queues);
2902 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2903 	if (ret) {
2904 		dev_info(ctrl->ctrl.device,
2905 			"set_queue_count failed: %d\n", ret);
2906 		return ret;
2907 	}
2908 
2909 	ctrl->ctrl.queue_count = nr_io_queues + 1;
2910 	if (!nr_io_queues)
2911 		return 0;
2912 
2913 	nvme_fc_init_io_queues(ctrl);
2914 
2915 	ret = nvme_alloc_io_tag_set(&ctrl->ctrl, &ctrl->tag_set,
2916 			&nvme_fc_mq_ops, 1,
2917 			struct_size_t(struct nvme_fcp_op_w_sgl, priv,
2918 				      ctrl->lport->ops->fcprqst_priv_sz));
2919 	if (ret)
2920 		return ret;
2921 
2922 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2923 	if (ret)
2924 		goto out_cleanup_tagset;
2925 
2926 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2927 	if (ret)
2928 		goto out_delete_hw_queues;
2929 
2930 	ctrl->ioq_live = true;
2931 
2932 	return 0;
2933 
2934 out_delete_hw_queues:
2935 	nvme_fc_delete_hw_io_queues(ctrl);
2936 out_cleanup_tagset:
2937 	nvme_remove_io_tag_set(&ctrl->ctrl);
2938 	nvme_fc_free_io_queues(ctrl);
2939 
2940 	/* force put free routine to ignore io queues */
2941 	ctrl->ctrl.tagset = NULL;
2942 
2943 	return ret;
2944 }
2945 
2946 static int
2947 nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
2948 {
2949 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2950 	u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
2951 	unsigned int nr_io_queues;
2952 	int ret;
2953 
2954 	nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2955 				ctrl->lport->ops->max_hw_queues);
2956 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2957 	if (ret) {
2958 		dev_info(ctrl->ctrl.device,
2959 			"set_queue_count failed: %d\n", ret);
2960 		return ret;
2961 	}
2962 
2963 	if (!nr_io_queues && prior_ioq_cnt) {
2964 		dev_info(ctrl->ctrl.device,
2965 			"Fail Reconnect: At least 1 io queue "
2966 			"required (was %d)\n", prior_ioq_cnt);
2967 		return -ENOSPC;
2968 	}
2969 
2970 	ctrl->ctrl.queue_count = nr_io_queues + 1;
2971 	/* check for io queues existing */
2972 	if (ctrl->ctrl.queue_count == 1)
2973 		return 0;
2974 
2975 	if (prior_ioq_cnt != nr_io_queues) {
2976 		dev_info(ctrl->ctrl.device,
2977 			"reconnect: revising io queue count from %d to %d\n",
2978 			prior_ioq_cnt, nr_io_queues);
2979 		blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2980 	}
2981 
2982 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2983 	if (ret)
2984 		goto out_free_io_queues;
2985 
2986 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2987 	if (ret)
2988 		goto out_delete_hw_queues;
2989 
2990 	return 0;
2991 
2992 out_delete_hw_queues:
2993 	nvme_fc_delete_hw_io_queues(ctrl);
2994 out_free_io_queues:
2995 	nvme_fc_free_io_queues(ctrl);
2996 	return ret;
2997 }
2998 
2999 static void
3000 nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
3001 {
3002 	struct nvme_fc_lport *lport = rport->lport;
3003 
3004 	atomic_inc(&lport->act_rport_cnt);
3005 }
3006 
3007 static void
3008 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
3009 {
3010 	struct nvme_fc_lport *lport = rport->lport;
3011 	u32 cnt;
3012 
3013 	cnt = atomic_dec_return(&lport->act_rport_cnt);
3014 	if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
3015 		lport->ops->localport_delete(&lport->localport);
3016 }
3017 
3018 static int
3019 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
3020 {
3021 	struct nvme_fc_rport *rport = ctrl->rport;
3022 	u32 cnt;
3023 
3024 	if (test_and_set_bit(ASSOC_ACTIVE, &ctrl->flags))
3025 		return 1;
3026 
3027 	cnt = atomic_inc_return(&rport->act_ctrl_cnt);
3028 	if (cnt == 1)
3029 		nvme_fc_rport_active_on_lport(rport);
3030 
3031 	return 0;
3032 }
3033 
3034 static int
3035 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
3036 {
3037 	struct nvme_fc_rport *rport = ctrl->rport;
3038 	struct nvme_fc_lport *lport = rport->lport;
3039 	u32 cnt;
3040 
3041 	/* clearing of ctrl->flags ASSOC_ACTIVE bit is in association delete */
3042 
3043 	cnt = atomic_dec_return(&rport->act_ctrl_cnt);
3044 	if (cnt == 0) {
3045 		if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
3046 			lport->ops->remoteport_delete(&rport->remoteport);
3047 		nvme_fc_rport_inactive_on_lport(rport);
3048 	}
3049 
3050 	return 0;
3051 }
3052 
3053 /*
3054  * This routine restarts the controller on the host side, and
3055  * on the link side, recreates the controller association.
3056  */
3057 static int
3058 nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
3059 {
3060 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
3061 	struct nvmefc_ls_rcv_op *disls = NULL;
3062 	unsigned long flags;
3063 	int ret;
3064 	bool changed;
3065 
3066 	++ctrl->ctrl.nr_reconnects;
3067 
3068 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3069 		return -ENODEV;
3070 
3071 	if (nvme_fc_ctlr_active_on_rport(ctrl))
3072 		return -ENOTUNIQ;
3073 
3074 	dev_info(ctrl->ctrl.device,
3075 		"NVME-FC{%d}: create association : host wwpn 0x%016llx "
3076 		" rport wwpn 0x%016llx: NQN \"%s\"\n",
3077 		ctrl->cnum, ctrl->lport->localport.port_name,
3078 		ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
3079 
3080 	clear_bit(ASSOC_FAILED, &ctrl->flags);
3081 
3082 	/*
3083 	 * Create the admin queue
3084 	 */
3085 
3086 	ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
3087 				NVME_AQ_DEPTH);
3088 	if (ret)
3089 		goto out_free_queue;
3090 
3091 	ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
3092 				NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
3093 	if (ret)
3094 		goto out_delete_hw_queue;
3095 
3096 	ret = nvmf_connect_admin_queue(&ctrl->ctrl);
3097 	if (ret)
3098 		goto out_disconnect_admin_queue;
3099 
3100 	set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
3101 
3102 	/*
3103 	 * Check controller capabilities
3104 	 *
3105 	 * todo:- add code to check if ctrl attributes changed from
3106 	 * prior connection values
3107 	 */
3108 
3109 	ret = nvme_enable_ctrl(&ctrl->ctrl);
3110 	if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
3111 		ret = -EIO;
3112 	if (ret)
3113 		goto out_disconnect_admin_queue;
3114 
3115 	ctrl->ctrl.max_segments = ctrl->lport->ops->max_sgl_segments;
3116 	ctrl->ctrl.max_hw_sectors = ctrl->ctrl.max_segments <<
3117 						(ilog2(SZ_4K) - 9);
3118 
3119 	nvme_unquiesce_admin_queue(&ctrl->ctrl);
3120 
3121 	ret = nvme_init_ctrl_finish(&ctrl->ctrl, false);
3122 	if (ret)
3123 		goto out_disconnect_admin_queue;
3124 	if (test_bit(ASSOC_FAILED, &ctrl->flags)) {
3125 		ret = -EIO;
3126 		goto out_stop_keep_alive;
3127 	}
3128 	/* sanity checks */
3129 
3130 	/* FC-NVME does not have other data in the capsule */
3131 	if (ctrl->ctrl.icdoff) {
3132 		dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
3133 				ctrl->ctrl.icdoff);
3134 		ret = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
3135 		goto out_stop_keep_alive;
3136 	}
3137 
3138 	/* FC-NVME supports normal SGL Data Block Descriptors */
3139 	if (!nvme_ctrl_sgl_supported(&ctrl->ctrl)) {
3140 		dev_err(ctrl->ctrl.device,
3141 			"Mandatory sgls are not supported!\n");
3142 		ret = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
3143 		goto out_stop_keep_alive;
3144 	}
3145 
3146 	if (opts->queue_size > ctrl->ctrl.maxcmd) {
3147 		/* warn if maxcmd is lower than queue_size */
3148 		dev_warn(ctrl->ctrl.device,
3149 			"queue_size %zu > ctrl maxcmd %u, reducing "
3150 			"to maxcmd\n",
3151 			opts->queue_size, ctrl->ctrl.maxcmd);
3152 		opts->queue_size = ctrl->ctrl.maxcmd;
3153 		ctrl->ctrl.sqsize = opts->queue_size - 1;
3154 	}
3155 
3156 	ret = nvme_fc_init_aen_ops(ctrl);
3157 	if (ret)
3158 		goto out_term_aen_ops;
3159 
3160 	/*
3161 	 * Create the io queues
3162 	 */
3163 
3164 	if (ctrl->ctrl.queue_count > 1) {
3165 		if (!ctrl->ioq_live)
3166 			ret = nvme_fc_create_io_queues(ctrl);
3167 		else
3168 			ret = nvme_fc_recreate_io_queues(ctrl);
3169 	}
3170 	if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
3171 		ret = -EIO;
3172 	if (ret)
3173 		goto out_term_aen_ops;
3174 
3175 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
3176 
3177 	ctrl->ctrl.nr_reconnects = 0;
3178 
3179 	if (changed)
3180 		nvme_start_ctrl(&ctrl->ctrl);
3181 
3182 	return 0;	/* Success */
3183 
3184 out_term_aen_ops:
3185 	nvme_fc_term_aen_ops(ctrl);
3186 out_stop_keep_alive:
3187 	nvme_stop_keep_alive(&ctrl->ctrl);
3188 out_disconnect_admin_queue:
3189 	dev_warn(ctrl->ctrl.device,
3190 		"NVME-FC{%d}: create_assoc failed, assoc_id %llx ret %d\n",
3191 		ctrl->cnum, ctrl->association_id, ret);
3192 	/* send a Disconnect(association) LS to fc-nvme target */
3193 	nvme_fc_xmt_disconnect_assoc(ctrl);
3194 	spin_lock_irqsave(&ctrl->lock, flags);
3195 	ctrl->association_id = 0;
3196 	disls = ctrl->rcv_disconn;
3197 	ctrl->rcv_disconn = NULL;
3198 	spin_unlock_irqrestore(&ctrl->lock, flags);
3199 	if (disls)
3200 		nvme_fc_xmt_ls_rsp(disls);
3201 out_delete_hw_queue:
3202 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3203 out_free_queue:
3204 	nvme_fc_free_queue(&ctrl->queues[0]);
3205 	clear_bit(ASSOC_ACTIVE, &ctrl->flags);
3206 	nvme_fc_ctlr_inactive_on_rport(ctrl);
3207 
3208 	return ret;
3209 }
3210 
3211 
3212 /*
3213  * This routine stops operation of the controller on the host side.
3214  * On the host os stack side: Admin and IO queues are stopped,
3215  *   outstanding ios on them terminated via FC ABTS.
3216  * On the link side: the association is terminated.
3217  */
3218 static void
3219 nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
3220 {
3221 	struct nvmefc_ls_rcv_op *disls = NULL;
3222 	unsigned long flags;
3223 
3224 	if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
3225 		return;
3226 
3227 	spin_lock_irqsave(&ctrl->lock, flags);
3228 	set_bit(FCCTRL_TERMIO, &ctrl->flags);
3229 	ctrl->iocnt = 0;
3230 	spin_unlock_irqrestore(&ctrl->lock, flags);
3231 
3232 	__nvme_fc_abort_outstanding_ios(ctrl, false);
3233 
3234 	/* kill the aens as they are a separate path */
3235 	nvme_fc_abort_aen_ops(ctrl);
3236 
3237 	/* wait for all io that had to be aborted */
3238 	spin_lock_irq(&ctrl->lock);
3239 	wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
3240 	clear_bit(FCCTRL_TERMIO, &ctrl->flags);
3241 	spin_unlock_irq(&ctrl->lock);
3242 
3243 	nvme_fc_term_aen_ops(ctrl);
3244 
3245 	/*
3246 	 * send a Disconnect(association) LS to fc-nvme target
3247 	 * Note: could have been sent at top of process, but
3248 	 * cleaner on link traffic if after the aborts complete.
3249 	 * Note: if association doesn't exist, association_id will be 0
3250 	 */
3251 	if (ctrl->association_id)
3252 		nvme_fc_xmt_disconnect_assoc(ctrl);
3253 
3254 	spin_lock_irqsave(&ctrl->lock, flags);
3255 	ctrl->association_id = 0;
3256 	disls = ctrl->rcv_disconn;
3257 	ctrl->rcv_disconn = NULL;
3258 	spin_unlock_irqrestore(&ctrl->lock, flags);
3259 	if (disls)
3260 		/*
3261 		 * if a Disconnect Request was waiting for a response, send
3262 		 * now that all ABTS's have been issued (and are complete).
3263 		 */
3264 		nvme_fc_xmt_ls_rsp(disls);
3265 
3266 	if (ctrl->ctrl.tagset) {
3267 		nvme_fc_delete_hw_io_queues(ctrl);
3268 		nvme_fc_free_io_queues(ctrl);
3269 	}
3270 
3271 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3272 	nvme_fc_free_queue(&ctrl->queues[0]);
3273 
3274 	/* re-enable the admin_q so anything new can fast fail */
3275 	nvme_unquiesce_admin_queue(&ctrl->ctrl);
3276 
3277 	/* resume the io queues so that things will fast fail */
3278 	nvme_unquiesce_io_queues(&ctrl->ctrl);
3279 
3280 	nvme_fc_ctlr_inactive_on_rport(ctrl);
3281 }
3282 
3283 static void
3284 nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
3285 {
3286 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
3287 
3288 	cancel_work_sync(&ctrl->ioerr_work);
3289 	cancel_delayed_work_sync(&ctrl->connect_work);
3290 	/*
3291 	 * kill the association on the link side.  this will block
3292 	 * waiting for io to terminate
3293 	 */
3294 	nvme_fc_delete_association(ctrl);
3295 }
3296 
3297 static void
3298 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
3299 {
3300 	struct nvme_fc_rport *rport = ctrl->rport;
3301 	struct nvme_fc_remote_port *portptr = &rport->remoteport;
3302 	unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
3303 	bool recon = true;
3304 
3305 	if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_CONNECTING)
3306 		return;
3307 
3308 	if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3309 		dev_info(ctrl->ctrl.device,
3310 			"NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
3311 			ctrl->cnum, status);
3312 	} else if (time_after_eq(jiffies, rport->dev_loss_end))
3313 		recon = false;
3314 
3315 	if (recon && nvmf_should_reconnect(&ctrl->ctrl, status)) {
3316 		if (portptr->port_state == FC_OBJSTATE_ONLINE)
3317 			dev_info(ctrl->ctrl.device,
3318 				"NVME-FC{%d}: Reconnect attempt in %ld "
3319 				"seconds\n",
3320 				ctrl->cnum, recon_delay / HZ);
3321 		else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
3322 			recon_delay = rport->dev_loss_end - jiffies;
3323 
3324 		queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
3325 	} else {
3326 		if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3327 			if (status > 0 && (status & NVME_STATUS_DNR))
3328 				dev_warn(ctrl->ctrl.device,
3329 					 "NVME-FC{%d}: reconnect failure\n",
3330 					 ctrl->cnum);
3331 			else
3332 				dev_warn(ctrl->ctrl.device,
3333 					 "NVME-FC{%d}: Max reconnect attempts "
3334 					 "(%d) reached.\n",
3335 					 ctrl->cnum, ctrl->ctrl.nr_reconnects);
3336 		} else
3337 			dev_warn(ctrl->ctrl.device,
3338 				"NVME-FC{%d}: dev_loss_tmo (%d) expired "
3339 				"while waiting for remoteport connectivity.\n",
3340 				ctrl->cnum, min_t(int, portptr->dev_loss_tmo,
3341 					(ctrl->ctrl.opts->max_reconnects *
3342 					 ctrl->ctrl.opts->reconnect_delay)));
3343 		WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
3344 	}
3345 }
3346 
3347 static void
3348 nvme_fc_reset_ctrl_work(struct work_struct *work)
3349 {
3350 	struct nvme_fc_ctrl *ctrl =
3351 		container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
3352 
3353 	nvme_stop_ctrl(&ctrl->ctrl);
3354 
3355 	/* will block will waiting for io to terminate */
3356 	nvme_fc_delete_association(ctrl);
3357 
3358 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
3359 		dev_err(ctrl->ctrl.device,
3360 			"NVME-FC{%d}: error_recovery: Couldn't change state "
3361 			"to CONNECTING\n", ctrl->cnum);
3362 
3363 	if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
3364 		if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3365 			dev_err(ctrl->ctrl.device,
3366 				"NVME-FC{%d}: failed to schedule connect "
3367 				"after reset\n", ctrl->cnum);
3368 		} else {
3369 			flush_delayed_work(&ctrl->connect_work);
3370 		}
3371 	} else {
3372 		nvme_fc_reconnect_or_delete(ctrl, -ENOTCONN);
3373 	}
3374 }
3375 
3376 
3377 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
3378 	.name			= "fc",
3379 	.module			= THIS_MODULE,
3380 	.flags			= NVME_F_FABRICS,
3381 	.reg_read32		= nvmf_reg_read32,
3382 	.reg_read64		= nvmf_reg_read64,
3383 	.reg_write32		= nvmf_reg_write32,
3384 	.subsystem_reset	= nvmf_subsystem_reset,
3385 	.free_ctrl		= nvme_fc_free_ctrl,
3386 	.submit_async_event	= nvme_fc_submit_async_event,
3387 	.delete_ctrl		= nvme_fc_delete_ctrl,
3388 	.get_address		= nvmf_get_address,
3389 };
3390 
3391 static void
3392 nvme_fc_connect_ctrl_work(struct work_struct *work)
3393 {
3394 	int ret;
3395 
3396 	struct nvme_fc_ctrl *ctrl =
3397 			container_of(to_delayed_work(work),
3398 				struct nvme_fc_ctrl, connect_work);
3399 
3400 	ret = nvme_fc_create_association(ctrl);
3401 	if (ret)
3402 		nvme_fc_reconnect_or_delete(ctrl, ret);
3403 	else
3404 		dev_info(ctrl->ctrl.device,
3405 			"NVME-FC{%d}: controller connect complete\n",
3406 			ctrl->cnum);
3407 }
3408 
3409 
3410 static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
3411 	.queue_rq	= nvme_fc_queue_rq,
3412 	.complete	= nvme_fc_complete_rq,
3413 	.init_request	= nvme_fc_init_request,
3414 	.exit_request	= nvme_fc_exit_request,
3415 	.init_hctx	= nvme_fc_init_admin_hctx,
3416 	.timeout	= nvme_fc_timeout,
3417 };
3418 
3419 
3420 /*
3421  * Fails a controller request if it matches an existing controller
3422  * (association) with the same tuple:
3423  * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
3424  *
3425  * The ports don't need to be compared as they are intrinsically
3426  * already matched by the port pointers supplied.
3427  */
3428 static bool
3429 nvme_fc_existing_controller(struct nvme_fc_rport *rport,
3430 		struct nvmf_ctrl_options *opts)
3431 {
3432 	struct nvme_fc_ctrl *ctrl;
3433 	unsigned long flags;
3434 	bool found = false;
3435 
3436 	spin_lock_irqsave(&rport->lock, flags);
3437 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3438 		found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
3439 		if (found)
3440 			break;
3441 	}
3442 	spin_unlock_irqrestore(&rport->lock, flags);
3443 
3444 	return found;
3445 }
3446 
3447 static struct nvme_fc_ctrl *
3448 nvme_fc_alloc_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3449 	struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3450 {
3451 	struct nvme_fc_ctrl *ctrl;
3452 	int ret, idx, ctrl_loss_tmo;
3453 
3454 	if (!(rport->remoteport.port_role &
3455 	    (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
3456 		ret = -EBADR;
3457 		goto out_fail;
3458 	}
3459 
3460 	if (!opts->duplicate_connect &&
3461 	    nvme_fc_existing_controller(rport, opts)) {
3462 		ret = -EALREADY;
3463 		goto out_fail;
3464 	}
3465 
3466 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3467 	if (!ctrl) {
3468 		ret = -ENOMEM;
3469 		goto out_fail;
3470 	}
3471 
3472 	idx = ida_alloc(&nvme_fc_ctrl_cnt, GFP_KERNEL);
3473 	if (idx < 0) {
3474 		ret = -ENOSPC;
3475 		goto out_free_ctrl;
3476 	}
3477 
3478 	/*
3479 	 * if ctrl_loss_tmo is being enforced and the default reconnect delay
3480 	 * is being used, change to a shorter reconnect delay for FC.
3481 	 */
3482 	if (opts->max_reconnects != -1 &&
3483 	    opts->reconnect_delay == NVMF_DEF_RECONNECT_DELAY &&
3484 	    opts->reconnect_delay > NVME_FC_DEFAULT_RECONNECT_TMO) {
3485 		ctrl_loss_tmo = opts->max_reconnects * opts->reconnect_delay;
3486 		opts->reconnect_delay = NVME_FC_DEFAULT_RECONNECT_TMO;
3487 		opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
3488 						opts->reconnect_delay);
3489 	}
3490 
3491 	ctrl->ctrl.opts = opts;
3492 	ctrl->ctrl.nr_reconnects = 0;
3493 	INIT_LIST_HEAD(&ctrl->ctrl_list);
3494 	ctrl->lport = lport;
3495 	ctrl->rport = rport;
3496 	ctrl->dev = lport->dev;
3497 	ctrl->cnum = idx;
3498 	ctrl->ioq_live = false;
3499 	init_waitqueue_head(&ctrl->ioabort_wait);
3500 
3501 	get_device(ctrl->dev);
3502 	kref_init(&ctrl->ref);
3503 
3504 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
3505 	INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
3506 	INIT_WORK(&ctrl->ioerr_work, nvme_fc_ctrl_ioerr_work);
3507 	spin_lock_init(&ctrl->lock);
3508 
3509 	/* io queue count */
3510 	ctrl->ctrl.queue_count = min_t(unsigned int,
3511 				opts->nr_io_queues,
3512 				lport->ops->max_hw_queues);
3513 	ctrl->ctrl.queue_count++;	/* +1 for admin queue */
3514 
3515 	ctrl->ctrl.sqsize = opts->queue_size - 1;
3516 	ctrl->ctrl.kato = opts->kato;
3517 	ctrl->ctrl.cntlid = 0xffff;
3518 
3519 	ret = -ENOMEM;
3520 	ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3521 				sizeof(struct nvme_fc_queue), GFP_KERNEL);
3522 	if (!ctrl->queues)
3523 		goto out_free_ida;
3524 
3525 	nvme_fc_init_queue(ctrl, 0);
3526 
3527 	/*
3528 	 * Would have been nice to init io queues tag set as well.
3529 	 * However, we require interaction from the controller
3530 	 * for max io queue count before we can do so.
3531 	 * Defer this to the connect path.
3532 	 */
3533 
3534 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3535 	if (ret)
3536 		goto out_free_queues;
3537 	if (lport->dev)
3538 		ctrl->ctrl.numa_node = dev_to_node(lport->dev);
3539 
3540 	return ctrl;
3541 
3542 out_free_queues:
3543 	kfree(ctrl->queues);
3544 out_free_ida:
3545 	put_device(ctrl->dev);
3546 	ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
3547 out_free_ctrl:
3548 	kfree(ctrl);
3549 out_fail:
3550 	/* exit via here doesn't follow ctlr ref points */
3551 	return ERR_PTR(ret);
3552 }
3553 
3554 static struct nvme_ctrl *
3555 nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3556 	struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3557 {
3558 	struct nvme_fc_ctrl *ctrl;
3559 	unsigned long flags;
3560 	int ret;
3561 
3562 	ctrl = nvme_fc_alloc_ctrl(dev, opts, lport, rport);
3563 	if (IS_ERR(ctrl))
3564 		return ERR_CAST(ctrl);
3565 
3566 	ret = nvme_add_ctrl(&ctrl->ctrl);
3567 	if (ret)
3568 		goto out_put_ctrl;
3569 
3570 	ret = nvme_alloc_admin_tag_set(&ctrl->ctrl, &ctrl->admin_tag_set,
3571 			&nvme_fc_admin_mq_ops,
3572 			struct_size_t(struct nvme_fcp_op_w_sgl, priv,
3573 				      ctrl->lport->ops->fcprqst_priv_sz));
3574 	if (ret)
3575 		goto fail_ctrl;
3576 
3577 	spin_lock_irqsave(&rport->lock, flags);
3578 	list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3579 	spin_unlock_irqrestore(&rport->lock, flags);
3580 
3581 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING) ||
3582 	    !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
3583 		dev_err(ctrl->ctrl.device,
3584 			"NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
3585 		goto fail_ctrl;
3586 	}
3587 
3588 	if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3589 		dev_err(ctrl->ctrl.device,
3590 			"NVME-FC{%d}: failed to schedule initial connect\n",
3591 			ctrl->cnum);
3592 		goto fail_ctrl;
3593 	}
3594 
3595 	flush_delayed_work(&ctrl->connect_work);
3596 
3597 	dev_info(ctrl->ctrl.device,
3598 		"NVME-FC{%d}: new ctrl: NQN \"%s\", hostnqn: %s\n",
3599 		ctrl->cnum, nvmf_ctrl_subsysnqn(&ctrl->ctrl), opts->host->nqn);
3600 
3601 	return &ctrl->ctrl;
3602 
3603 fail_ctrl:
3604 	nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3605 	cancel_work_sync(&ctrl->ioerr_work);
3606 	cancel_work_sync(&ctrl->ctrl.reset_work);
3607 	cancel_delayed_work_sync(&ctrl->connect_work);
3608 
3609 	ctrl->ctrl.opts = NULL;
3610 
3611 	/* initiate nvme ctrl ref counting teardown */
3612 	nvme_uninit_ctrl(&ctrl->ctrl);
3613 
3614 out_put_ctrl:
3615 	/* Remove core ctrl ref. */
3616 	nvme_put_ctrl(&ctrl->ctrl);
3617 
3618 	/* as we're past the point where we transition to the ref
3619 	 * counting teardown path, if we return a bad pointer here,
3620 	 * the calling routine, thinking it's prior to the
3621 	 * transition, will do an rport put. Since the teardown
3622 	 * path also does a rport put, we do an extra get here to
3623 	 * so proper order/teardown happens.
3624 	 */
3625 	nvme_fc_rport_get(rport);
3626 
3627 	return ERR_PTR(-EIO);
3628 }
3629 
3630 struct nvmet_fc_traddr {
3631 	u64	nn;
3632 	u64	pn;
3633 };
3634 
3635 static int
3636 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3637 {
3638 	u64 token64;
3639 
3640 	if (match_u64(sstr, &token64))
3641 		return -EINVAL;
3642 	*val = token64;
3643 
3644 	return 0;
3645 }
3646 
3647 /*
3648  * This routine validates and extracts the WWN's from the TRADDR string.
3649  * As kernel parsers need the 0x to determine number base, universally
3650  * build string to parse with 0x prefix before parsing name strings.
3651  */
3652 static int
3653 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
3654 {
3655 	char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
3656 	substring_t wwn = { name, &name[sizeof(name)-1] };
3657 	int nnoffset, pnoffset;
3658 
3659 	/* validate if string is one of the 2 allowed formats */
3660 	if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
3661 			!strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
3662 			!strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
3663 				"pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
3664 		nnoffset = NVME_FC_TRADDR_OXNNLEN;
3665 		pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
3666 						NVME_FC_TRADDR_OXNNLEN;
3667 	} else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
3668 			!strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
3669 			!strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
3670 				"pn-", NVME_FC_TRADDR_NNLEN))) {
3671 		nnoffset = NVME_FC_TRADDR_NNLEN;
3672 		pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
3673 	} else
3674 		goto out_einval;
3675 
3676 	name[0] = '0';
3677 	name[1] = 'x';
3678 	name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
3679 
3680 	memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3681 	if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
3682 		goto out_einval;
3683 
3684 	memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3685 	if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
3686 		goto out_einval;
3687 
3688 	return 0;
3689 
3690 out_einval:
3691 	pr_warn("%s: bad traddr string\n", __func__);
3692 	return -EINVAL;
3693 }
3694 
3695 static struct nvme_ctrl *
3696 nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3697 {
3698 	struct nvme_fc_lport *lport;
3699 	struct nvme_fc_rport *rport;
3700 	struct nvme_ctrl *ctrl;
3701 	struct nvmet_fc_traddr laddr = { 0L, 0L };
3702 	struct nvmet_fc_traddr raddr = { 0L, 0L };
3703 	unsigned long flags;
3704 	int ret;
3705 
3706 	ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3707 	if (ret || !raddr.nn || !raddr.pn)
3708 		return ERR_PTR(-EINVAL);
3709 
3710 	ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3711 	if (ret || !laddr.nn || !laddr.pn)
3712 		return ERR_PTR(-EINVAL);
3713 
3714 	/* find the host and remote ports to connect together */
3715 	spin_lock_irqsave(&nvme_fc_lock, flags);
3716 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3717 		if (lport->localport.node_name != laddr.nn ||
3718 		    lport->localport.port_name != laddr.pn ||
3719 		    lport->localport.port_state != FC_OBJSTATE_ONLINE)
3720 			continue;
3721 
3722 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
3723 			if (rport->remoteport.node_name != raddr.nn ||
3724 			    rport->remoteport.port_name != raddr.pn ||
3725 			    rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3726 				continue;
3727 
3728 			/* if fail to get reference fall through. Will error */
3729 			if (!nvme_fc_rport_get(rport))
3730 				break;
3731 
3732 			spin_unlock_irqrestore(&nvme_fc_lock, flags);
3733 
3734 			ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3735 			if (IS_ERR(ctrl))
3736 				nvme_fc_rport_put(rport);
3737 			return ctrl;
3738 		}
3739 	}
3740 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
3741 
3742 	pr_warn("%s: %s - %s combination not found\n",
3743 		__func__, opts->traddr, opts->host_traddr);
3744 	return ERR_PTR(-ENOENT);
3745 }
3746 
3747 
3748 static struct nvmf_transport_ops nvme_fc_transport = {
3749 	.name		= "fc",
3750 	.module		= THIS_MODULE,
3751 	.required_opts	= NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
3752 	.allowed_opts	= NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3753 	.create_ctrl	= nvme_fc_create_ctrl,
3754 };
3755 
3756 /* Arbitrary successive failures max. With lots of subsystems could be high */
3757 #define DISCOVERY_MAX_FAIL	20
3758 
3759 static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
3760 		struct device_attribute *attr, const char *buf, size_t count)
3761 {
3762 	unsigned long flags;
3763 	LIST_HEAD(local_disc_list);
3764 	struct nvme_fc_lport *lport;
3765 	struct nvme_fc_rport *rport;
3766 	int failcnt = 0;
3767 
3768 	spin_lock_irqsave(&nvme_fc_lock, flags);
3769 restart:
3770 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3771 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
3772 			if (!nvme_fc_lport_get(lport))
3773 				continue;
3774 			if (!nvme_fc_rport_get(rport)) {
3775 				/*
3776 				 * This is a temporary condition. Upon restart
3777 				 * this rport will be gone from the list.
3778 				 *
3779 				 * Revert the lport put and retry.  Anything
3780 				 * added to the list already will be skipped (as
3781 				 * they are no longer list_empty).  Loops should
3782 				 * resume at rports that were not yet seen.
3783 				 */
3784 				nvme_fc_lport_put(lport);
3785 
3786 				if (failcnt++ < DISCOVERY_MAX_FAIL)
3787 					goto restart;
3788 
3789 				pr_err("nvme_discovery: too many reference "
3790 				       "failures\n");
3791 				goto process_local_list;
3792 			}
3793 			if (list_empty(&rport->disc_list))
3794 				list_add_tail(&rport->disc_list,
3795 					      &local_disc_list);
3796 		}
3797 	}
3798 
3799 process_local_list:
3800 	while (!list_empty(&local_disc_list)) {
3801 		rport = list_first_entry(&local_disc_list,
3802 					 struct nvme_fc_rport, disc_list);
3803 		list_del_init(&rport->disc_list);
3804 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
3805 
3806 		lport = rport->lport;
3807 		/* signal discovery. Won't hurt if it repeats */
3808 		nvme_fc_signal_discovery_scan(lport, rport);
3809 		nvme_fc_rport_put(rport);
3810 		nvme_fc_lport_put(lport);
3811 
3812 		spin_lock_irqsave(&nvme_fc_lock, flags);
3813 	}
3814 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
3815 
3816 	return count;
3817 }
3818 
3819 static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
3820 
3821 #ifdef CONFIG_BLK_CGROUP_FC_APPID
3822 /* Parse the cgroup id from a buf and return the length of cgrpid */
3823 static int fc_parse_cgrpid(const char *buf, u64 *id)
3824 {
3825 	char cgrp_id[16+1];
3826 	int cgrpid_len, j;
3827 
3828 	memset(cgrp_id, 0x0, sizeof(cgrp_id));
3829 	for (cgrpid_len = 0, j = 0; cgrpid_len < 17; cgrpid_len++) {
3830 		if (buf[cgrpid_len] != ':')
3831 			cgrp_id[cgrpid_len] = buf[cgrpid_len];
3832 		else {
3833 			j = 1;
3834 			break;
3835 		}
3836 	}
3837 	if (!j)
3838 		return -EINVAL;
3839 	if (kstrtou64(cgrp_id, 16, id) < 0)
3840 		return -EINVAL;
3841 	return cgrpid_len;
3842 }
3843 
3844 /*
3845  * Parse and update the appid in the blkcg associated with the cgroupid.
3846  */
3847 static ssize_t fc_appid_store(struct device *dev,
3848 		struct device_attribute *attr, const char *buf, size_t count)
3849 {
3850 	size_t orig_count = count;
3851 	u64 cgrp_id;
3852 	int appid_len = 0;
3853 	int cgrpid_len = 0;
3854 	char app_id[FC_APPID_LEN];
3855 	int ret = 0;
3856 
3857 	if (buf[count-1] == '\n')
3858 		count--;
3859 
3860 	if ((count > (16+1+FC_APPID_LEN)) || (!strchr(buf, ':')))
3861 		return -EINVAL;
3862 
3863 	cgrpid_len = fc_parse_cgrpid(buf, &cgrp_id);
3864 	if (cgrpid_len < 0)
3865 		return -EINVAL;
3866 	appid_len = count - cgrpid_len - 1;
3867 	if (appid_len > FC_APPID_LEN)
3868 		return -EINVAL;
3869 
3870 	memset(app_id, 0x0, sizeof(app_id));
3871 	memcpy(app_id, &buf[cgrpid_len+1], appid_len);
3872 	ret = blkcg_set_fc_appid(app_id, cgrp_id, sizeof(app_id));
3873 	if (ret < 0)
3874 		return ret;
3875 	return orig_count;
3876 }
3877 static DEVICE_ATTR(appid_store, 0200, NULL, fc_appid_store);
3878 #endif /* CONFIG_BLK_CGROUP_FC_APPID */
3879 
3880 static struct attribute *nvme_fc_attrs[] = {
3881 	&dev_attr_nvme_discovery.attr,
3882 #ifdef CONFIG_BLK_CGROUP_FC_APPID
3883 	&dev_attr_appid_store.attr,
3884 #endif
3885 	NULL
3886 };
3887 
3888 static const struct attribute_group nvme_fc_attr_group = {
3889 	.attrs = nvme_fc_attrs,
3890 };
3891 
3892 static const struct attribute_group *nvme_fc_attr_groups[] = {
3893 	&nvme_fc_attr_group,
3894 	NULL
3895 };
3896 
3897 static struct class fc_class = {
3898 	.name = "fc",
3899 	.dev_groups = nvme_fc_attr_groups,
3900 };
3901 
3902 static int __init nvme_fc_init_module(void)
3903 {
3904 	int ret;
3905 
3906 	/*
3907 	 * NOTE:
3908 	 * It is expected that in the future the kernel will combine
3909 	 * the FC-isms that are currently under scsi and now being
3910 	 * added to by NVME into a new standalone FC class. The SCSI
3911 	 * and NVME protocols and their devices would be under this
3912 	 * new FC class.
3913 	 *
3914 	 * As we need something to post FC-specific udev events to,
3915 	 * specifically for nvme probe events, start by creating the
3916 	 * new device class.  When the new standalone FC class is
3917 	 * put in place, this code will move to a more generic
3918 	 * location for the class.
3919 	 */
3920 	ret = class_register(&fc_class);
3921 	if (ret) {
3922 		pr_err("couldn't register class fc\n");
3923 		return ret;
3924 	}
3925 
3926 	/*
3927 	 * Create a device for the FC-centric udev events
3928 	 */
3929 	fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
3930 				"fc_udev_device");
3931 	if (IS_ERR(fc_udev_device)) {
3932 		pr_err("couldn't create fc_udev device!\n");
3933 		ret = PTR_ERR(fc_udev_device);
3934 		goto out_destroy_class;
3935 	}
3936 
3937 	ret = nvmf_register_transport(&nvme_fc_transport);
3938 	if (ret)
3939 		goto out_destroy_device;
3940 
3941 	return 0;
3942 
3943 out_destroy_device:
3944 	device_destroy(&fc_class, MKDEV(0, 0));
3945 out_destroy_class:
3946 	class_unregister(&fc_class);
3947 
3948 	return ret;
3949 }
3950 
3951 static void
3952 nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
3953 {
3954 	struct nvme_fc_ctrl *ctrl;
3955 
3956 	spin_lock(&rport->lock);
3957 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3958 		dev_warn(ctrl->ctrl.device,
3959 			"NVME-FC{%d}: transport unloading: deleting ctrl\n",
3960 			ctrl->cnum);
3961 		nvme_delete_ctrl(&ctrl->ctrl);
3962 	}
3963 	spin_unlock(&rport->lock);
3964 }
3965 
3966 static void __exit nvme_fc_exit_module(void)
3967 {
3968 	struct nvme_fc_lport *lport;
3969 	struct nvme_fc_rport *rport;
3970 	unsigned long flags;
3971 
3972 	spin_lock_irqsave(&nvme_fc_lock, flags);
3973 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list)
3974 		list_for_each_entry(rport, &lport->endp_list, endp_list)
3975 			nvme_fc_delete_controllers(rport);
3976 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
3977 	flush_workqueue(nvme_delete_wq);
3978 
3979 	nvmf_unregister_transport(&nvme_fc_transport);
3980 
3981 	device_destroy(&fc_class, MKDEV(0, 0));
3982 	class_unregister(&fc_class);
3983 }
3984 
3985 module_init(nvme_fc_init_module);
3986 module_exit(nvme_fc_exit_module);
3987 
3988 MODULE_DESCRIPTION("NVMe host FC transport driver");
3989 MODULE_LICENSE("GPL v2");
3990