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