xref: /openbmc/linux/drivers/nvme/host/fc.c (revision 36db6e8484ed455bbb320d89a119378897ae991c)
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 	strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
1174 		min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
1175 	strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
1176 		min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
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_nvme_ctrl_freed(struct nvme_ctrl * nctrl)2391 nvme_fc_nvme_ctrl_freed(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 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2532 	struct nvme_command *sqe = &cmdiu->sqe;
2533 
2534 	/*
2535 	 * Attempt to abort the offending command. Command completion
2536 	 * will detect the aborted io and will fail the connection.
2537 	 */
2538 	dev_info(ctrl->ctrl.device,
2539 		"NVME-FC{%d.%d}: io timeout: opcode %d fctype %d w10/11: "
2540 		"x%08x/x%08x\n",
2541 		ctrl->cnum, op->queue->qnum, sqe->common.opcode,
2542 		sqe->connect.fctype, sqe->common.cdw10, sqe->common.cdw11);
2543 	if (__nvme_fc_abort_op(ctrl, op))
2544 		nvme_fc_error_recovery(ctrl, "io timeout abort failed");
2545 
2546 	/*
2547 	 * the io abort has been initiated. Have the reset timer
2548 	 * restarted and the abort completion will complete the io
2549 	 * shortly. Avoids a synchronous wait while the abort finishes.
2550 	 */
2551 	return BLK_EH_RESET_TIMER;
2552 }
2553 
2554 static int
nvme_fc_map_data(struct nvme_fc_ctrl * ctrl,struct request * rq,struct nvme_fc_fcp_op * op)2555 nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2556 		struct nvme_fc_fcp_op *op)
2557 {
2558 	struct nvmefc_fcp_req *freq = &op->fcp_req;
2559 	int ret;
2560 
2561 	freq->sg_cnt = 0;
2562 
2563 	if (!blk_rq_nr_phys_segments(rq))
2564 		return 0;
2565 
2566 	freq->sg_table.sgl = freq->first_sgl;
2567 	ret = sg_alloc_table_chained(&freq->sg_table,
2568 			blk_rq_nr_phys_segments(rq), freq->sg_table.sgl,
2569 			NVME_INLINE_SG_CNT);
2570 	if (ret)
2571 		return -ENOMEM;
2572 
2573 	op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
2574 	WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
2575 	freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
2576 				op->nents, rq_dma_dir(rq));
2577 	if (unlikely(freq->sg_cnt <= 0)) {
2578 		sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2579 		freq->sg_cnt = 0;
2580 		return -EFAULT;
2581 	}
2582 
2583 	/*
2584 	 * TODO: blk_integrity_rq(rq)  for DIF
2585 	 */
2586 	return 0;
2587 }
2588 
2589 static void
nvme_fc_unmap_data(struct nvme_fc_ctrl * ctrl,struct request * rq,struct nvme_fc_fcp_op * op)2590 nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
2591 		struct nvme_fc_fcp_op *op)
2592 {
2593 	struct nvmefc_fcp_req *freq = &op->fcp_req;
2594 
2595 	if (!freq->sg_cnt)
2596 		return;
2597 
2598 	fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
2599 			rq_dma_dir(rq));
2600 
2601 	sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
2602 
2603 	freq->sg_cnt = 0;
2604 }
2605 
2606 /*
2607  * In FC, the queue is a logical thing. At transport connect, the target
2608  * creates its "queue" and returns a handle that is to be given to the
2609  * target whenever it posts something to the corresponding SQ.  When an
2610  * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
2611  * command contained within the SQE, an io, and assigns a FC exchange
2612  * to it. The SQE and the associated SQ handle are sent in the initial
2613  * CMD IU sents on the exchange. All transfers relative to the io occur
2614  * as part of the exchange.  The CQE is the last thing for the io,
2615  * which is transferred (explicitly or implicitly) with the RSP IU
2616  * sent on the exchange. After the CQE is received, the FC exchange is
2617  * terminaed and the Exchange may be used on a different io.
2618  *
2619  * The transport to LLDD api has the transport making a request for a
2620  * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
2621  * resource and transfers the command. The LLDD will then process all
2622  * steps to complete the io. Upon completion, the transport done routine
2623  * is called.
2624  *
2625  * So - while the operation is outstanding to the LLDD, there is a link
2626  * level FC exchange resource that is also outstanding. This must be
2627  * considered in all cleanup operations.
2628  */
2629 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)2630 nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
2631 	struct nvme_fc_fcp_op *op, u32 data_len,
2632 	enum nvmefc_fcp_datadir	io_dir)
2633 {
2634 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
2635 	struct nvme_command *sqe = &cmdiu->sqe;
2636 	int ret, opstate;
2637 
2638 	/*
2639 	 * before attempting to send the io, check to see if we believe
2640 	 * the target device is present
2641 	 */
2642 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
2643 		return BLK_STS_RESOURCE;
2644 
2645 	if (!nvme_fc_ctrl_get(ctrl))
2646 		return BLK_STS_IOERR;
2647 
2648 	/* format the FC-NVME CMD IU and fcp_req */
2649 	cmdiu->connection_id = cpu_to_be64(queue->connection_id);
2650 	cmdiu->data_len = cpu_to_be32(data_len);
2651 	switch (io_dir) {
2652 	case NVMEFC_FCP_WRITE:
2653 		cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
2654 		break;
2655 	case NVMEFC_FCP_READ:
2656 		cmdiu->flags = FCNVME_CMD_FLAGS_READ;
2657 		break;
2658 	case NVMEFC_FCP_NODATA:
2659 		cmdiu->flags = 0;
2660 		break;
2661 	}
2662 	op->fcp_req.payload_length = data_len;
2663 	op->fcp_req.io_dir = io_dir;
2664 	op->fcp_req.transferred_length = 0;
2665 	op->fcp_req.rcv_rsplen = 0;
2666 	op->fcp_req.status = NVME_SC_SUCCESS;
2667 	op->fcp_req.sqid = cpu_to_le16(queue->qnum);
2668 
2669 	/*
2670 	 * validate per fabric rules, set fields mandated by fabric spec
2671 	 * as well as those by FC-NVME spec.
2672 	 */
2673 	WARN_ON_ONCE(sqe->common.metadata);
2674 	sqe->common.flags |= NVME_CMD_SGL_METABUF;
2675 
2676 	/*
2677 	 * format SQE DPTR field per FC-NVME rules:
2678 	 *    type=0x5     Transport SGL Data Block Descriptor
2679 	 *    subtype=0xA  Transport-specific value
2680 	 *    address=0
2681 	 *    length=length of the data series
2682 	 */
2683 	sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2684 					NVME_SGL_FMT_TRANSPORT_A;
2685 	sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
2686 	sqe->rw.dptr.sgl.addr = 0;
2687 
2688 	if (!(op->flags & FCOP_FLAGS_AEN)) {
2689 		ret = nvme_fc_map_data(ctrl, op->rq, op);
2690 		if (ret < 0) {
2691 			nvme_cleanup_cmd(op->rq);
2692 			nvme_fc_ctrl_put(ctrl);
2693 			if (ret == -ENOMEM || ret == -EAGAIN)
2694 				return BLK_STS_RESOURCE;
2695 			return BLK_STS_IOERR;
2696 		}
2697 	}
2698 
2699 	fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
2700 				  sizeof(op->cmd_iu), DMA_TO_DEVICE);
2701 
2702 	atomic_set(&op->state, FCPOP_STATE_ACTIVE);
2703 
2704 	if (!(op->flags & FCOP_FLAGS_AEN))
2705 		nvme_start_request(op->rq);
2706 
2707 	cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn));
2708 	ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
2709 					&ctrl->rport->remoteport,
2710 					queue->lldd_handle, &op->fcp_req);
2711 
2712 	if (ret) {
2713 		/*
2714 		 * If the lld fails to send the command is there an issue with
2715 		 * the csn value?  If the command that fails is the Connect,
2716 		 * no - as the connection won't be live.  If it is a command
2717 		 * post-connect, it's possible a gap in csn may be created.
2718 		 * Does this matter?  As Linux initiators don't send fused
2719 		 * commands, no.  The gap would exist, but as there's nothing
2720 		 * that depends on csn order to be delivered on the target
2721 		 * side, it shouldn't hurt.  It would be difficult for a
2722 		 * target to even detect the csn gap as it has no idea when the
2723 		 * cmd with the csn was supposed to arrive.
2724 		 */
2725 		opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
2726 		__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
2727 
2728 		if (!(op->flags & FCOP_FLAGS_AEN)) {
2729 			nvme_fc_unmap_data(ctrl, op->rq, op);
2730 			nvme_cleanup_cmd(op->rq);
2731 		}
2732 
2733 		nvme_fc_ctrl_put(ctrl);
2734 
2735 		if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE &&
2736 				ret != -EBUSY)
2737 			return BLK_STS_IOERR;
2738 
2739 		return BLK_STS_RESOURCE;
2740 	}
2741 
2742 	return BLK_STS_OK;
2743 }
2744 
2745 static blk_status_t
nvme_fc_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)2746 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
2747 			const struct blk_mq_queue_data *bd)
2748 {
2749 	struct nvme_ns *ns = hctx->queue->queuedata;
2750 	struct nvme_fc_queue *queue = hctx->driver_data;
2751 	struct nvme_fc_ctrl *ctrl = queue->ctrl;
2752 	struct request *rq = bd->rq;
2753 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2754 	enum nvmefc_fcp_datadir	io_dir;
2755 	bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags);
2756 	u32 data_len;
2757 	blk_status_t ret;
2758 
2759 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE ||
2760 	    !nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2761 		return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
2762 
2763 	ret = nvme_setup_cmd(ns, rq);
2764 	if (ret)
2765 		return ret;
2766 
2767 	/*
2768 	 * nvme core doesn't quite treat the rq opaquely. Commands such
2769 	 * as WRITE ZEROES will return a non-zero rq payload_bytes yet
2770 	 * there is no actual payload to be transferred.
2771 	 * To get it right, key data transmission on there being 1 or
2772 	 * more physical segments in the sg list. If there is no
2773 	 * physical segments, there is no payload.
2774 	 */
2775 	if (blk_rq_nr_phys_segments(rq)) {
2776 		data_len = blk_rq_payload_bytes(rq);
2777 		io_dir = ((rq_data_dir(rq) == WRITE) ?
2778 					NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
2779 	} else {
2780 		data_len = 0;
2781 		io_dir = NVMEFC_FCP_NODATA;
2782 	}
2783 
2784 
2785 	return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
2786 }
2787 
2788 static void
nvme_fc_submit_async_event(struct nvme_ctrl * arg)2789 nvme_fc_submit_async_event(struct nvme_ctrl *arg)
2790 {
2791 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
2792 	struct nvme_fc_fcp_op *aen_op;
2793 	blk_status_t ret;
2794 
2795 	if (test_bit(FCCTRL_TERMIO, &ctrl->flags))
2796 		return;
2797 
2798 	aen_op = &ctrl->aen_ops[0];
2799 
2800 	ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
2801 					NVMEFC_FCP_NODATA);
2802 	if (ret)
2803 		dev_err(ctrl->ctrl.device,
2804 			"failed async event work\n");
2805 }
2806 
2807 static void
nvme_fc_complete_rq(struct request * rq)2808 nvme_fc_complete_rq(struct request *rq)
2809 {
2810 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
2811 	struct nvme_fc_ctrl *ctrl = op->ctrl;
2812 
2813 	atomic_set(&op->state, FCPOP_STATE_IDLE);
2814 	op->flags &= ~FCOP_FLAGS_TERMIO;
2815 
2816 	nvme_fc_unmap_data(ctrl, rq, op);
2817 	nvme_complete_rq(rq);
2818 	nvme_fc_ctrl_put(ctrl);
2819 }
2820 
nvme_fc_map_queues(struct blk_mq_tag_set * set)2821 static void nvme_fc_map_queues(struct blk_mq_tag_set *set)
2822 {
2823 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data);
2824 	int i;
2825 
2826 	for (i = 0; i < set->nr_maps; i++) {
2827 		struct blk_mq_queue_map *map = &set->map[i];
2828 
2829 		if (!map->nr_queues) {
2830 			WARN_ON(i == HCTX_TYPE_DEFAULT);
2831 			continue;
2832 		}
2833 
2834 		/* Call LLDD map queue functionality if defined */
2835 		if (ctrl->lport->ops->map_queues)
2836 			ctrl->lport->ops->map_queues(&ctrl->lport->localport,
2837 						     map);
2838 		else
2839 			blk_mq_map_queues(map);
2840 	}
2841 }
2842 
2843 static const struct blk_mq_ops nvme_fc_mq_ops = {
2844 	.queue_rq	= nvme_fc_queue_rq,
2845 	.complete	= nvme_fc_complete_rq,
2846 	.init_request	= nvme_fc_init_request,
2847 	.exit_request	= nvme_fc_exit_request,
2848 	.init_hctx	= nvme_fc_init_hctx,
2849 	.timeout	= nvme_fc_timeout,
2850 	.map_queues	= nvme_fc_map_queues,
2851 };
2852 
2853 static int
nvme_fc_create_io_queues(struct nvme_fc_ctrl * ctrl)2854 nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
2855 {
2856 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2857 	unsigned int nr_io_queues;
2858 	int ret;
2859 
2860 	nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2861 				ctrl->lport->ops->max_hw_queues);
2862 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2863 	if (ret) {
2864 		dev_info(ctrl->ctrl.device,
2865 			"set_queue_count failed: %d\n", ret);
2866 		return ret;
2867 	}
2868 
2869 	ctrl->ctrl.queue_count = nr_io_queues + 1;
2870 	if (!nr_io_queues)
2871 		return 0;
2872 
2873 	nvme_fc_init_io_queues(ctrl);
2874 
2875 	ret = nvme_alloc_io_tag_set(&ctrl->ctrl, &ctrl->tag_set,
2876 			&nvme_fc_mq_ops, 1,
2877 			struct_size_t(struct nvme_fcp_op_w_sgl, priv,
2878 				      ctrl->lport->ops->fcprqst_priv_sz));
2879 	if (ret)
2880 		return ret;
2881 
2882 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2883 	if (ret)
2884 		goto out_cleanup_tagset;
2885 
2886 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2887 	if (ret)
2888 		goto out_delete_hw_queues;
2889 
2890 	ctrl->ioq_live = true;
2891 
2892 	return 0;
2893 
2894 out_delete_hw_queues:
2895 	nvme_fc_delete_hw_io_queues(ctrl);
2896 out_cleanup_tagset:
2897 	nvme_remove_io_tag_set(&ctrl->ctrl);
2898 	nvme_fc_free_io_queues(ctrl);
2899 
2900 	/* force put free routine to ignore io queues */
2901 	ctrl->ctrl.tagset = NULL;
2902 
2903 	return ret;
2904 }
2905 
2906 static int
nvme_fc_recreate_io_queues(struct nvme_fc_ctrl * ctrl)2907 nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl)
2908 {
2909 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2910 	u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1;
2911 	unsigned int nr_io_queues;
2912 	int ret;
2913 
2914 	nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()),
2915 				ctrl->lport->ops->max_hw_queues);
2916 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
2917 	if (ret) {
2918 		dev_info(ctrl->ctrl.device,
2919 			"set_queue_count failed: %d\n", ret);
2920 		return ret;
2921 	}
2922 
2923 	if (!nr_io_queues && prior_ioq_cnt) {
2924 		dev_info(ctrl->ctrl.device,
2925 			"Fail Reconnect: At least 1 io queue "
2926 			"required (was %d)\n", prior_ioq_cnt);
2927 		return -ENOSPC;
2928 	}
2929 
2930 	ctrl->ctrl.queue_count = nr_io_queues + 1;
2931 	/* check for io queues existing */
2932 	if (ctrl->ctrl.queue_count == 1)
2933 		return 0;
2934 
2935 	if (prior_ioq_cnt != nr_io_queues) {
2936 		dev_info(ctrl->ctrl.device,
2937 			"reconnect: revising io queue count from %d to %d\n",
2938 			prior_ioq_cnt, nr_io_queues);
2939 		blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues);
2940 	}
2941 
2942 	ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2943 	if (ret)
2944 		goto out_free_io_queues;
2945 
2946 	ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1);
2947 	if (ret)
2948 		goto out_delete_hw_queues;
2949 
2950 	return 0;
2951 
2952 out_delete_hw_queues:
2953 	nvme_fc_delete_hw_io_queues(ctrl);
2954 out_free_io_queues:
2955 	nvme_fc_free_io_queues(ctrl);
2956 	return ret;
2957 }
2958 
2959 static void
nvme_fc_rport_active_on_lport(struct nvme_fc_rport * rport)2960 nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport)
2961 {
2962 	struct nvme_fc_lport *lport = rport->lport;
2963 
2964 	atomic_inc(&lport->act_rport_cnt);
2965 }
2966 
2967 static void
nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport * rport)2968 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport)
2969 {
2970 	struct nvme_fc_lport *lport = rport->lport;
2971 	u32 cnt;
2972 
2973 	cnt = atomic_dec_return(&lport->act_rport_cnt);
2974 	if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED)
2975 		lport->ops->localport_delete(&lport->localport);
2976 }
2977 
2978 static int
nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl * ctrl)2979 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl)
2980 {
2981 	struct nvme_fc_rport *rport = ctrl->rport;
2982 	u32 cnt;
2983 
2984 	if (test_and_set_bit(ASSOC_ACTIVE, &ctrl->flags))
2985 		return 1;
2986 
2987 	cnt = atomic_inc_return(&rport->act_ctrl_cnt);
2988 	if (cnt == 1)
2989 		nvme_fc_rport_active_on_lport(rport);
2990 
2991 	return 0;
2992 }
2993 
2994 static int
nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl * ctrl)2995 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl)
2996 {
2997 	struct nvme_fc_rport *rport = ctrl->rport;
2998 	struct nvme_fc_lport *lport = rport->lport;
2999 	u32 cnt;
3000 
3001 	/* clearing of ctrl->flags ASSOC_ACTIVE bit is in association delete */
3002 
3003 	cnt = atomic_dec_return(&rport->act_ctrl_cnt);
3004 	if (cnt == 0) {
3005 		if (rport->remoteport.port_state == FC_OBJSTATE_DELETED)
3006 			lport->ops->remoteport_delete(&rport->remoteport);
3007 		nvme_fc_rport_inactive_on_lport(rport);
3008 	}
3009 
3010 	return 0;
3011 }
3012 
3013 /*
3014  * This routine restarts the controller on the host side, and
3015  * on the link side, recreates the controller association.
3016  */
3017 static int
nvme_fc_create_association(struct nvme_fc_ctrl * ctrl)3018 nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
3019 {
3020 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
3021 	struct nvmefc_ls_rcv_op *disls = NULL;
3022 	unsigned long flags;
3023 	int ret;
3024 
3025 	++ctrl->ctrl.nr_reconnects;
3026 
3027 	if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3028 		return -ENODEV;
3029 
3030 	if (nvme_fc_ctlr_active_on_rport(ctrl))
3031 		return -ENOTUNIQ;
3032 
3033 	dev_info(ctrl->ctrl.device,
3034 		"NVME-FC{%d}: create association : host wwpn 0x%016llx "
3035 		" rport wwpn 0x%016llx: NQN \"%s\"\n",
3036 		ctrl->cnum, ctrl->lport->localport.port_name,
3037 		ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn);
3038 
3039 	clear_bit(ASSOC_FAILED, &ctrl->flags);
3040 
3041 	/*
3042 	 * Create the admin queue
3043 	 */
3044 
3045 	ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
3046 				NVME_AQ_DEPTH);
3047 	if (ret)
3048 		goto out_free_queue;
3049 
3050 	ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
3051 				NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4));
3052 	if (ret)
3053 		goto out_delete_hw_queue;
3054 
3055 	ret = nvmf_connect_admin_queue(&ctrl->ctrl);
3056 	if (ret)
3057 		goto out_disconnect_admin_queue;
3058 
3059 	set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags);
3060 
3061 	/*
3062 	 * Check controller capabilities
3063 	 *
3064 	 * todo:- add code to check if ctrl attributes changed from
3065 	 * prior connection values
3066 	 */
3067 
3068 	ret = nvme_enable_ctrl(&ctrl->ctrl);
3069 	if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
3070 		ret = -EIO;
3071 	if (ret)
3072 		goto out_disconnect_admin_queue;
3073 
3074 	ctrl->ctrl.max_segments = ctrl->lport->ops->max_sgl_segments;
3075 	ctrl->ctrl.max_hw_sectors = ctrl->ctrl.max_segments <<
3076 						(ilog2(SZ_4K) - 9);
3077 
3078 	nvme_unquiesce_admin_queue(&ctrl->ctrl);
3079 
3080 	ret = nvme_init_ctrl_finish(&ctrl->ctrl, false);
3081 	if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
3082 		ret = -EIO;
3083 	if (ret)
3084 		goto out_disconnect_admin_queue;
3085 
3086 	/* sanity checks */
3087 
3088 	/* FC-NVME does not have other data in the capsule */
3089 	if (ctrl->ctrl.icdoff) {
3090 		dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
3091 				ctrl->ctrl.icdoff);
3092 		ret = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
3093 		goto out_disconnect_admin_queue;
3094 	}
3095 
3096 	/* FC-NVME supports normal SGL Data Block Descriptors */
3097 	if (!nvme_ctrl_sgl_supported(&ctrl->ctrl)) {
3098 		dev_err(ctrl->ctrl.device,
3099 			"Mandatory sgls are not supported!\n");
3100 		ret = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
3101 		goto out_disconnect_admin_queue;
3102 	}
3103 
3104 	if (opts->queue_size > ctrl->ctrl.maxcmd) {
3105 		/* warn if maxcmd is lower than queue_size */
3106 		dev_warn(ctrl->ctrl.device,
3107 			"queue_size %zu > ctrl maxcmd %u, reducing "
3108 			"to maxcmd\n",
3109 			opts->queue_size, ctrl->ctrl.maxcmd);
3110 		opts->queue_size = ctrl->ctrl.maxcmd;
3111 		ctrl->ctrl.sqsize = opts->queue_size - 1;
3112 	}
3113 
3114 	ret = nvme_fc_init_aen_ops(ctrl);
3115 	if (ret)
3116 		goto out_term_aen_ops;
3117 
3118 	/*
3119 	 * Create the io queues
3120 	 */
3121 
3122 	if (ctrl->ctrl.queue_count > 1) {
3123 		if (!ctrl->ioq_live)
3124 			ret = nvme_fc_create_io_queues(ctrl);
3125 		else
3126 			ret = nvme_fc_recreate_io_queues(ctrl);
3127 	}
3128 	if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags))
3129 		ret = -EIO;
3130 	if (ret)
3131 		goto out_term_aen_ops;
3132 
3133 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE)) {
3134 		ret = -EIO;
3135 		goto out_term_aen_ops;
3136 	}
3137 
3138 	ctrl->ctrl.nr_reconnects = 0;
3139 	nvme_start_ctrl(&ctrl->ctrl);
3140 
3141 	return 0;	/* Success */
3142 
3143 out_term_aen_ops:
3144 	nvme_fc_term_aen_ops(ctrl);
3145 out_disconnect_admin_queue:
3146 	dev_warn(ctrl->ctrl.device,
3147 		"NVME-FC{%d}: create_assoc failed, assoc_id %llx ret %d\n",
3148 		ctrl->cnum, ctrl->association_id, ret);
3149 	/* send a Disconnect(association) LS to fc-nvme target */
3150 	nvme_fc_xmt_disconnect_assoc(ctrl);
3151 	spin_lock_irqsave(&ctrl->lock, flags);
3152 	ctrl->association_id = 0;
3153 	disls = ctrl->rcv_disconn;
3154 	ctrl->rcv_disconn = NULL;
3155 	spin_unlock_irqrestore(&ctrl->lock, flags);
3156 	if (disls)
3157 		nvme_fc_xmt_ls_rsp(disls);
3158 out_delete_hw_queue:
3159 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3160 out_free_queue:
3161 	nvme_fc_free_queue(&ctrl->queues[0]);
3162 	clear_bit(ASSOC_ACTIVE, &ctrl->flags);
3163 	nvme_fc_ctlr_inactive_on_rport(ctrl);
3164 
3165 	return ret;
3166 }
3167 
3168 
3169 /*
3170  * This routine stops operation of the controller on the host side.
3171  * On the host os stack side: Admin and IO queues are stopped,
3172  *   outstanding ios on them terminated via FC ABTS.
3173  * On the link side: the association is terminated.
3174  */
3175 static void
nvme_fc_delete_association(struct nvme_fc_ctrl * ctrl)3176 nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
3177 {
3178 	struct nvmefc_ls_rcv_op *disls = NULL;
3179 	unsigned long flags;
3180 
3181 	if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
3182 		return;
3183 
3184 	spin_lock_irqsave(&ctrl->lock, flags);
3185 	set_bit(FCCTRL_TERMIO, &ctrl->flags);
3186 	ctrl->iocnt = 0;
3187 	spin_unlock_irqrestore(&ctrl->lock, flags);
3188 
3189 	__nvme_fc_abort_outstanding_ios(ctrl, false);
3190 
3191 	/* kill the aens as they are a separate path */
3192 	nvme_fc_abort_aen_ops(ctrl);
3193 
3194 	/* wait for all io that had to be aborted */
3195 	spin_lock_irq(&ctrl->lock);
3196 	wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
3197 	clear_bit(FCCTRL_TERMIO, &ctrl->flags);
3198 	spin_unlock_irq(&ctrl->lock);
3199 
3200 	nvme_fc_term_aen_ops(ctrl);
3201 
3202 	/*
3203 	 * send a Disconnect(association) LS to fc-nvme target
3204 	 * Note: could have been sent at top of process, but
3205 	 * cleaner on link traffic if after the aborts complete.
3206 	 * Note: if association doesn't exist, association_id will be 0
3207 	 */
3208 	if (ctrl->association_id)
3209 		nvme_fc_xmt_disconnect_assoc(ctrl);
3210 
3211 	spin_lock_irqsave(&ctrl->lock, flags);
3212 	ctrl->association_id = 0;
3213 	disls = ctrl->rcv_disconn;
3214 	ctrl->rcv_disconn = NULL;
3215 	spin_unlock_irqrestore(&ctrl->lock, flags);
3216 	if (disls)
3217 		/*
3218 		 * if a Disconnect Request was waiting for a response, send
3219 		 * now that all ABTS's have been issued (and are complete).
3220 		 */
3221 		nvme_fc_xmt_ls_rsp(disls);
3222 
3223 	if (ctrl->ctrl.tagset) {
3224 		nvme_fc_delete_hw_io_queues(ctrl);
3225 		nvme_fc_free_io_queues(ctrl);
3226 	}
3227 
3228 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
3229 	nvme_fc_free_queue(&ctrl->queues[0]);
3230 
3231 	/* re-enable the admin_q so anything new can fast fail */
3232 	nvme_unquiesce_admin_queue(&ctrl->ctrl);
3233 
3234 	/* resume the io queues so that things will fast fail */
3235 	nvme_unquiesce_io_queues(&ctrl->ctrl);
3236 
3237 	nvme_fc_ctlr_inactive_on_rport(ctrl);
3238 }
3239 
3240 static void
nvme_fc_delete_ctrl(struct nvme_ctrl * nctrl)3241 nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl)
3242 {
3243 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
3244 
3245 	cancel_work_sync(&ctrl->ioerr_work);
3246 	cancel_delayed_work_sync(&ctrl->connect_work);
3247 	/*
3248 	 * kill the association on the link side.  this will block
3249 	 * waiting for io to terminate
3250 	 */
3251 	nvme_fc_delete_association(ctrl);
3252 }
3253 
3254 static void
nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl * ctrl,int status)3255 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status)
3256 {
3257 	struct nvme_fc_rport *rport = ctrl->rport;
3258 	struct nvme_fc_remote_port *portptr = &rport->remoteport;
3259 	unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ;
3260 	bool recon = true;
3261 
3262 	if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_CONNECTING)
3263 		return;
3264 
3265 	if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3266 		dev_info(ctrl->ctrl.device,
3267 			"NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n",
3268 			ctrl->cnum, status);
3269 		if (status > 0 && (status & NVME_SC_DNR))
3270 			recon = false;
3271 	} else if (time_after_eq(jiffies, rport->dev_loss_end))
3272 		recon = false;
3273 
3274 	if (recon && nvmf_should_reconnect(&ctrl->ctrl)) {
3275 		if (portptr->port_state == FC_OBJSTATE_ONLINE)
3276 			dev_info(ctrl->ctrl.device,
3277 				"NVME-FC{%d}: Reconnect attempt in %ld "
3278 				"seconds\n",
3279 				ctrl->cnum, recon_delay / HZ);
3280 		else if (time_after(jiffies + recon_delay, rport->dev_loss_end))
3281 			recon_delay = rport->dev_loss_end - jiffies;
3282 
3283 		queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay);
3284 	} else {
3285 		if (portptr->port_state == FC_OBJSTATE_ONLINE) {
3286 			if (status > 0 && (status & NVME_SC_DNR))
3287 				dev_warn(ctrl->ctrl.device,
3288 					 "NVME-FC{%d}: reconnect failure\n",
3289 					 ctrl->cnum);
3290 			else
3291 				dev_warn(ctrl->ctrl.device,
3292 					 "NVME-FC{%d}: Max reconnect attempts "
3293 					 "(%d) reached.\n",
3294 					 ctrl->cnum, ctrl->ctrl.nr_reconnects);
3295 		} else
3296 			dev_warn(ctrl->ctrl.device,
3297 				"NVME-FC{%d}: dev_loss_tmo (%d) expired "
3298 				"while waiting for remoteport connectivity.\n",
3299 				ctrl->cnum, min_t(int, portptr->dev_loss_tmo,
3300 					(ctrl->ctrl.opts->max_reconnects *
3301 					 ctrl->ctrl.opts->reconnect_delay)));
3302 		WARN_ON(nvme_delete_ctrl(&ctrl->ctrl));
3303 	}
3304 }
3305 
3306 static void
nvme_fc_reset_ctrl_work(struct work_struct * work)3307 nvme_fc_reset_ctrl_work(struct work_struct *work)
3308 {
3309 	struct nvme_fc_ctrl *ctrl =
3310 		container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
3311 
3312 	nvme_stop_ctrl(&ctrl->ctrl);
3313 
3314 	/* will block will waiting for io to terminate */
3315 	nvme_fc_delete_association(ctrl);
3316 
3317 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
3318 		dev_err(ctrl->ctrl.device,
3319 			"NVME-FC{%d}: error_recovery: Couldn't change state "
3320 			"to CONNECTING\n", ctrl->cnum);
3321 
3322 	if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
3323 		if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3324 			dev_err(ctrl->ctrl.device,
3325 				"NVME-FC{%d}: failed to schedule connect "
3326 				"after reset\n", ctrl->cnum);
3327 		} else {
3328 			flush_delayed_work(&ctrl->connect_work);
3329 		}
3330 	} else {
3331 		nvme_fc_reconnect_or_delete(ctrl, -ENOTCONN);
3332 	}
3333 }
3334 
3335 
3336 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
3337 	.name			= "fc",
3338 	.module			= THIS_MODULE,
3339 	.flags			= NVME_F_FABRICS,
3340 	.reg_read32		= nvmf_reg_read32,
3341 	.reg_read64		= nvmf_reg_read64,
3342 	.reg_write32		= nvmf_reg_write32,
3343 	.free_ctrl		= nvme_fc_nvme_ctrl_freed,
3344 	.submit_async_event	= nvme_fc_submit_async_event,
3345 	.delete_ctrl		= nvme_fc_delete_ctrl,
3346 	.get_address		= nvmf_get_address,
3347 };
3348 
3349 static void
nvme_fc_connect_ctrl_work(struct work_struct * work)3350 nvme_fc_connect_ctrl_work(struct work_struct *work)
3351 {
3352 	int ret;
3353 
3354 	struct nvme_fc_ctrl *ctrl =
3355 			container_of(to_delayed_work(work),
3356 				struct nvme_fc_ctrl, connect_work);
3357 
3358 	ret = nvme_fc_create_association(ctrl);
3359 	if (ret)
3360 		nvme_fc_reconnect_or_delete(ctrl, ret);
3361 	else
3362 		dev_info(ctrl->ctrl.device,
3363 			"NVME-FC{%d}: controller connect complete\n",
3364 			ctrl->cnum);
3365 }
3366 
3367 
3368 static const struct blk_mq_ops nvme_fc_admin_mq_ops = {
3369 	.queue_rq	= nvme_fc_queue_rq,
3370 	.complete	= nvme_fc_complete_rq,
3371 	.init_request	= nvme_fc_init_request,
3372 	.exit_request	= nvme_fc_exit_request,
3373 	.init_hctx	= nvme_fc_init_admin_hctx,
3374 	.timeout	= nvme_fc_timeout,
3375 };
3376 
3377 
3378 /*
3379  * Fails a controller request if it matches an existing controller
3380  * (association) with the same tuple:
3381  * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN>
3382  *
3383  * The ports don't need to be compared as they are intrinsically
3384  * already matched by the port pointers supplied.
3385  */
3386 static bool
nvme_fc_existing_controller(struct nvme_fc_rport * rport,struct nvmf_ctrl_options * opts)3387 nvme_fc_existing_controller(struct nvme_fc_rport *rport,
3388 		struct nvmf_ctrl_options *opts)
3389 {
3390 	struct nvme_fc_ctrl *ctrl;
3391 	unsigned long flags;
3392 	bool found = false;
3393 
3394 	spin_lock_irqsave(&rport->lock, flags);
3395 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3396 		found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts);
3397 		if (found)
3398 			break;
3399 	}
3400 	spin_unlock_irqrestore(&rport->lock, flags);
3401 
3402 	return found;
3403 }
3404 
3405 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)3406 nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
3407 	struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
3408 {
3409 	struct nvme_fc_ctrl *ctrl;
3410 	unsigned long flags;
3411 	int ret, idx, ctrl_loss_tmo;
3412 
3413 	if (!(rport->remoteport.port_role &
3414 	    (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) {
3415 		ret = -EBADR;
3416 		goto out_fail;
3417 	}
3418 
3419 	if (!opts->duplicate_connect &&
3420 	    nvme_fc_existing_controller(rport, opts)) {
3421 		ret = -EALREADY;
3422 		goto out_fail;
3423 	}
3424 
3425 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
3426 	if (!ctrl) {
3427 		ret = -ENOMEM;
3428 		goto out_fail;
3429 	}
3430 
3431 	idx = ida_alloc(&nvme_fc_ctrl_cnt, GFP_KERNEL);
3432 	if (idx < 0) {
3433 		ret = -ENOSPC;
3434 		goto out_free_ctrl;
3435 	}
3436 
3437 	/*
3438 	 * if ctrl_loss_tmo is being enforced and the default reconnect delay
3439 	 * is being used, change to a shorter reconnect delay for FC.
3440 	 */
3441 	if (opts->max_reconnects != -1 &&
3442 	    opts->reconnect_delay == NVMF_DEF_RECONNECT_DELAY &&
3443 	    opts->reconnect_delay > NVME_FC_DEFAULT_RECONNECT_TMO) {
3444 		ctrl_loss_tmo = opts->max_reconnects * opts->reconnect_delay;
3445 		opts->reconnect_delay = NVME_FC_DEFAULT_RECONNECT_TMO;
3446 		opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
3447 						opts->reconnect_delay);
3448 	}
3449 
3450 	ctrl->ctrl.opts = opts;
3451 	ctrl->ctrl.nr_reconnects = 0;
3452 	if (lport->dev)
3453 		ctrl->ctrl.numa_node = dev_to_node(lport->dev);
3454 	else
3455 		ctrl->ctrl.numa_node = NUMA_NO_NODE;
3456 	INIT_LIST_HEAD(&ctrl->ctrl_list);
3457 	ctrl->lport = lport;
3458 	ctrl->rport = rport;
3459 	ctrl->dev = lport->dev;
3460 	ctrl->cnum = idx;
3461 	ctrl->ioq_live = false;
3462 	init_waitqueue_head(&ctrl->ioabort_wait);
3463 
3464 	get_device(ctrl->dev);
3465 	kref_init(&ctrl->ref);
3466 
3467 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
3468 	INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
3469 	INIT_WORK(&ctrl->ioerr_work, nvme_fc_ctrl_ioerr_work);
3470 	spin_lock_init(&ctrl->lock);
3471 
3472 	/* io queue count */
3473 	ctrl->ctrl.queue_count = min_t(unsigned int,
3474 				opts->nr_io_queues,
3475 				lport->ops->max_hw_queues);
3476 	ctrl->ctrl.queue_count++;	/* +1 for admin queue */
3477 
3478 	ctrl->ctrl.sqsize = opts->queue_size - 1;
3479 	ctrl->ctrl.kato = opts->kato;
3480 	ctrl->ctrl.cntlid = 0xffff;
3481 
3482 	ret = -ENOMEM;
3483 	ctrl->queues = kcalloc(ctrl->ctrl.queue_count,
3484 				sizeof(struct nvme_fc_queue), GFP_KERNEL);
3485 	if (!ctrl->queues)
3486 		goto out_free_ida;
3487 
3488 	nvme_fc_init_queue(ctrl, 0);
3489 
3490 	/*
3491 	 * Would have been nice to init io queues tag set as well.
3492 	 * However, we require interaction from the controller
3493 	 * for max io queue count before we can do so.
3494 	 * Defer this to the connect path.
3495 	 */
3496 
3497 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
3498 	if (ret)
3499 		goto out_free_queues;
3500 
3501 	/* at this point, teardown path changes to ref counting on nvme ctrl */
3502 
3503 	ret = nvme_alloc_admin_tag_set(&ctrl->ctrl, &ctrl->admin_tag_set,
3504 			&nvme_fc_admin_mq_ops,
3505 			struct_size_t(struct nvme_fcp_op_w_sgl, priv,
3506 				      ctrl->lport->ops->fcprqst_priv_sz));
3507 	if (ret)
3508 		goto fail_ctrl;
3509 
3510 	spin_lock_irqsave(&rport->lock, flags);
3511 	list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
3512 	spin_unlock_irqrestore(&rport->lock, flags);
3513 
3514 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
3515 		dev_err(ctrl->ctrl.device,
3516 			"NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum);
3517 		goto fail_ctrl;
3518 	}
3519 
3520 	if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) {
3521 		dev_err(ctrl->ctrl.device,
3522 			"NVME-FC{%d}: failed to schedule initial connect\n",
3523 			ctrl->cnum);
3524 		goto fail_ctrl;
3525 	}
3526 
3527 	flush_delayed_work(&ctrl->connect_work);
3528 
3529 	dev_info(ctrl->ctrl.device,
3530 		"NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
3531 		ctrl->cnum, nvmf_ctrl_subsysnqn(&ctrl->ctrl));
3532 
3533 	return &ctrl->ctrl;
3534 
3535 fail_ctrl:
3536 	nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING);
3537 	cancel_work_sync(&ctrl->ioerr_work);
3538 	cancel_work_sync(&ctrl->ctrl.reset_work);
3539 	cancel_delayed_work_sync(&ctrl->connect_work);
3540 
3541 	ctrl->ctrl.opts = NULL;
3542 
3543 	/* initiate nvme ctrl ref counting teardown */
3544 	nvme_uninit_ctrl(&ctrl->ctrl);
3545 
3546 	/* Remove core ctrl ref. */
3547 	nvme_put_ctrl(&ctrl->ctrl);
3548 
3549 	/* as we're past the point where we transition to the ref
3550 	 * counting teardown path, if we return a bad pointer here,
3551 	 * the calling routine, thinking it's prior to the
3552 	 * transition, will do an rport put. Since the teardown
3553 	 * path also does a rport put, we do an extra get here to
3554 	 * so proper order/teardown happens.
3555 	 */
3556 	nvme_fc_rport_get(rport);
3557 
3558 	return ERR_PTR(-EIO);
3559 
3560 out_free_queues:
3561 	kfree(ctrl->queues);
3562 out_free_ida:
3563 	put_device(ctrl->dev);
3564 	ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum);
3565 out_free_ctrl:
3566 	kfree(ctrl);
3567 out_fail:
3568 	/* exit via here doesn't follow ctlr ref points */
3569 	return ERR_PTR(ret);
3570 }
3571 
3572 
3573 struct nvmet_fc_traddr {
3574 	u64	nn;
3575 	u64	pn;
3576 };
3577 
3578 static int
__nvme_fc_parse_u64(substring_t * sstr,u64 * val)3579 __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
3580 {
3581 	u64 token64;
3582 
3583 	if (match_u64(sstr, &token64))
3584 		return -EINVAL;
3585 	*val = token64;
3586 
3587 	return 0;
3588 }
3589 
3590 /*
3591  * This routine validates and extracts the WWN's from the TRADDR string.
3592  * As kernel parsers need the 0x to determine number base, universally
3593  * build string to parse with 0x prefix before parsing name strings.
3594  */
3595 static int
nvme_fc_parse_traddr(struct nvmet_fc_traddr * traddr,char * buf,size_t blen)3596 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
3597 {
3598 	char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
3599 	substring_t wwn = { name, &name[sizeof(name)-1] };
3600 	int nnoffset, pnoffset;
3601 
3602 	/* validate if string is one of the 2 allowed formats */
3603 	if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
3604 			!strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
3605 			!strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
3606 				"pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
3607 		nnoffset = NVME_FC_TRADDR_OXNNLEN;
3608 		pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
3609 						NVME_FC_TRADDR_OXNNLEN;
3610 	} else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
3611 			!strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
3612 			!strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
3613 				"pn-", NVME_FC_TRADDR_NNLEN))) {
3614 		nnoffset = NVME_FC_TRADDR_NNLEN;
3615 		pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
3616 	} else
3617 		goto out_einval;
3618 
3619 	name[0] = '0';
3620 	name[1] = 'x';
3621 	name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
3622 
3623 	memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3624 	if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
3625 		goto out_einval;
3626 
3627 	memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
3628 	if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
3629 		goto out_einval;
3630 
3631 	return 0;
3632 
3633 out_einval:
3634 	pr_warn("%s: bad traddr string\n", __func__);
3635 	return -EINVAL;
3636 }
3637 
3638 static struct nvme_ctrl *
nvme_fc_create_ctrl(struct device * dev,struct nvmf_ctrl_options * opts)3639 nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
3640 {
3641 	struct nvme_fc_lport *lport;
3642 	struct nvme_fc_rport *rport;
3643 	struct nvme_ctrl *ctrl;
3644 	struct nvmet_fc_traddr laddr = { 0L, 0L };
3645 	struct nvmet_fc_traddr raddr = { 0L, 0L };
3646 	unsigned long flags;
3647 	int ret;
3648 
3649 	ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE);
3650 	if (ret || !raddr.nn || !raddr.pn)
3651 		return ERR_PTR(-EINVAL);
3652 
3653 	ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE);
3654 	if (ret || !laddr.nn || !laddr.pn)
3655 		return ERR_PTR(-EINVAL);
3656 
3657 	/* find the host and remote ports to connect together */
3658 	spin_lock_irqsave(&nvme_fc_lock, flags);
3659 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3660 		if (lport->localport.node_name != laddr.nn ||
3661 		    lport->localport.port_name != laddr.pn ||
3662 		    lport->localport.port_state != FC_OBJSTATE_ONLINE)
3663 			continue;
3664 
3665 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
3666 			if (rport->remoteport.node_name != raddr.nn ||
3667 			    rport->remoteport.port_name != raddr.pn ||
3668 			    rport->remoteport.port_state != FC_OBJSTATE_ONLINE)
3669 				continue;
3670 
3671 			/* if fail to get reference fall through. Will error */
3672 			if (!nvme_fc_rport_get(rport))
3673 				break;
3674 
3675 			spin_unlock_irqrestore(&nvme_fc_lock, flags);
3676 
3677 			ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport);
3678 			if (IS_ERR(ctrl))
3679 				nvme_fc_rport_put(rport);
3680 			return ctrl;
3681 		}
3682 	}
3683 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
3684 
3685 	pr_warn("%s: %s - %s combination not found\n",
3686 		__func__, opts->traddr, opts->host_traddr);
3687 	return ERR_PTR(-ENOENT);
3688 }
3689 
3690 
3691 static struct nvmf_transport_ops nvme_fc_transport = {
3692 	.name		= "fc",
3693 	.module		= THIS_MODULE,
3694 	.required_opts	= NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
3695 	.allowed_opts	= NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO,
3696 	.create_ctrl	= nvme_fc_create_ctrl,
3697 };
3698 
3699 /* Arbitrary successive failures max. With lots of subsystems could be high */
3700 #define DISCOVERY_MAX_FAIL	20
3701 
nvme_fc_nvme_discovery_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3702 static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
3703 		struct device_attribute *attr, const char *buf, size_t count)
3704 {
3705 	unsigned long flags;
3706 	LIST_HEAD(local_disc_list);
3707 	struct nvme_fc_lport *lport;
3708 	struct nvme_fc_rport *rport;
3709 	int failcnt = 0;
3710 
3711 	spin_lock_irqsave(&nvme_fc_lock, flags);
3712 restart:
3713 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
3714 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
3715 			if (!nvme_fc_lport_get(lport))
3716 				continue;
3717 			if (!nvme_fc_rport_get(rport)) {
3718 				/*
3719 				 * This is a temporary condition. Upon restart
3720 				 * this rport will be gone from the list.
3721 				 *
3722 				 * Revert the lport put and retry.  Anything
3723 				 * added to the list already will be skipped (as
3724 				 * they are no longer list_empty).  Loops should
3725 				 * resume at rports that were not yet seen.
3726 				 */
3727 				nvme_fc_lport_put(lport);
3728 
3729 				if (failcnt++ < DISCOVERY_MAX_FAIL)
3730 					goto restart;
3731 
3732 				pr_err("nvme_discovery: too many reference "
3733 				       "failures\n");
3734 				goto process_local_list;
3735 			}
3736 			if (list_empty(&rport->disc_list))
3737 				list_add_tail(&rport->disc_list,
3738 					      &local_disc_list);
3739 		}
3740 	}
3741 
3742 process_local_list:
3743 	while (!list_empty(&local_disc_list)) {
3744 		rport = list_first_entry(&local_disc_list,
3745 					 struct nvme_fc_rport, disc_list);
3746 		list_del_init(&rport->disc_list);
3747 		spin_unlock_irqrestore(&nvme_fc_lock, flags);
3748 
3749 		lport = rport->lport;
3750 		/* signal discovery. Won't hurt if it repeats */
3751 		nvme_fc_signal_discovery_scan(lport, rport);
3752 		nvme_fc_rport_put(rport);
3753 		nvme_fc_lport_put(lport);
3754 
3755 		spin_lock_irqsave(&nvme_fc_lock, flags);
3756 	}
3757 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
3758 
3759 	return count;
3760 }
3761 
3762 static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store);
3763 
3764 #ifdef CONFIG_BLK_CGROUP_FC_APPID
3765 /* Parse the cgroup id from a buf and return the length of cgrpid */
fc_parse_cgrpid(const char * buf,u64 * id)3766 static int fc_parse_cgrpid(const char *buf, u64 *id)
3767 {
3768 	char cgrp_id[16+1];
3769 	int cgrpid_len, j;
3770 
3771 	memset(cgrp_id, 0x0, sizeof(cgrp_id));
3772 	for (cgrpid_len = 0, j = 0; cgrpid_len < 17; cgrpid_len++) {
3773 		if (buf[cgrpid_len] != ':')
3774 			cgrp_id[cgrpid_len] = buf[cgrpid_len];
3775 		else {
3776 			j = 1;
3777 			break;
3778 		}
3779 	}
3780 	if (!j)
3781 		return -EINVAL;
3782 	if (kstrtou64(cgrp_id, 16, id) < 0)
3783 		return -EINVAL;
3784 	return cgrpid_len;
3785 }
3786 
3787 /*
3788  * Parse and update the appid in the blkcg associated with the cgroupid.
3789  */
fc_appid_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3790 static ssize_t fc_appid_store(struct device *dev,
3791 		struct device_attribute *attr, const char *buf, size_t count)
3792 {
3793 	size_t orig_count = count;
3794 	u64 cgrp_id;
3795 	int appid_len = 0;
3796 	int cgrpid_len = 0;
3797 	char app_id[FC_APPID_LEN];
3798 	int ret = 0;
3799 
3800 	if (buf[count-1] == '\n')
3801 		count--;
3802 
3803 	if ((count > (16+1+FC_APPID_LEN)) || (!strchr(buf, ':')))
3804 		return -EINVAL;
3805 
3806 	cgrpid_len = fc_parse_cgrpid(buf, &cgrp_id);
3807 	if (cgrpid_len < 0)
3808 		return -EINVAL;
3809 	appid_len = count - cgrpid_len - 1;
3810 	if (appid_len > FC_APPID_LEN)
3811 		return -EINVAL;
3812 
3813 	memset(app_id, 0x0, sizeof(app_id));
3814 	memcpy(app_id, &buf[cgrpid_len+1], appid_len);
3815 	ret = blkcg_set_fc_appid(app_id, cgrp_id, sizeof(app_id));
3816 	if (ret < 0)
3817 		return ret;
3818 	return orig_count;
3819 }
3820 static DEVICE_ATTR(appid_store, 0200, NULL, fc_appid_store);
3821 #endif /* CONFIG_BLK_CGROUP_FC_APPID */
3822 
3823 static struct attribute *nvme_fc_attrs[] = {
3824 	&dev_attr_nvme_discovery.attr,
3825 #ifdef CONFIG_BLK_CGROUP_FC_APPID
3826 	&dev_attr_appid_store.attr,
3827 #endif
3828 	NULL
3829 };
3830 
3831 static const struct attribute_group nvme_fc_attr_group = {
3832 	.attrs = nvme_fc_attrs,
3833 };
3834 
3835 static const struct attribute_group *nvme_fc_attr_groups[] = {
3836 	&nvme_fc_attr_group,
3837 	NULL
3838 };
3839 
3840 static struct class fc_class = {
3841 	.name = "fc",
3842 	.dev_groups = nvme_fc_attr_groups,
3843 };
3844 
nvme_fc_init_module(void)3845 static int __init nvme_fc_init_module(void)
3846 {
3847 	int ret;
3848 
3849 	/*
3850 	 * NOTE:
3851 	 * It is expected that in the future the kernel will combine
3852 	 * the FC-isms that are currently under scsi and now being
3853 	 * added to by NVME into a new standalone FC class. The SCSI
3854 	 * and NVME protocols and their devices would be under this
3855 	 * new FC class.
3856 	 *
3857 	 * As we need something to post FC-specific udev events to,
3858 	 * specifically for nvme probe events, start by creating the
3859 	 * new device class.  When the new standalone FC class is
3860 	 * put in place, this code will move to a more generic
3861 	 * location for the class.
3862 	 */
3863 	ret = class_register(&fc_class);
3864 	if (ret) {
3865 		pr_err("couldn't register class fc\n");
3866 		return ret;
3867 	}
3868 
3869 	/*
3870 	 * Create a device for the FC-centric udev events
3871 	 */
3872 	fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL,
3873 				"fc_udev_device");
3874 	if (IS_ERR(fc_udev_device)) {
3875 		pr_err("couldn't create fc_udev device!\n");
3876 		ret = PTR_ERR(fc_udev_device);
3877 		goto out_destroy_class;
3878 	}
3879 
3880 	ret = nvmf_register_transport(&nvme_fc_transport);
3881 	if (ret)
3882 		goto out_destroy_device;
3883 
3884 	return 0;
3885 
3886 out_destroy_device:
3887 	device_destroy(&fc_class, MKDEV(0, 0));
3888 out_destroy_class:
3889 	class_unregister(&fc_class);
3890 
3891 	return ret;
3892 }
3893 
3894 static void
nvme_fc_delete_controllers(struct nvme_fc_rport * rport)3895 nvme_fc_delete_controllers(struct nvme_fc_rport *rport)
3896 {
3897 	struct nvme_fc_ctrl *ctrl;
3898 
3899 	spin_lock(&rport->lock);
3900 	list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) {
3901 		dev_warn(ctrl->ctrl.device,
3902 			"NVME-FC{%d}: transport unloading: deleting ctrl\n",
3903 			ctrl->cnum);
3904 		nvme_delete_ctrl(&ctrl->ctrl);
3905 	}
3906 	spin_unlock(&rport->lock);
3907 }
3908 
nvme_fc_exit_module(void)3909 static void __exit nvme_fc_exit_module(void)
3910 {
3911 	struct nvme_fc_lport *lport;
3912 	struct nvme_fc_rport *rport;
3913 	unsigned long flags;
3914 
3915 	spin_lock_irqsave(&nvme_fc_lock, flags);
3916 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list)
3917 		list_for_each_entry(rport, &lport->endp_list, endp_list)
3918 			nvme_fc_delete_controllers(rport);
3919 	spin_unlock_irqrestore(&nvme_fc_lock, flags);
3920 	flush_workqueue(nvme_delete_wq);
3921 
3922 	nvmf_unregister_transport(&nvme_fc_transport);
3923 
3924 	device_destroy(&fc_class, MKDEV(0, 0));
3925 	class_unregister(&fc_class);
3926 }
3927 
3928 module_init(nvme_fc_init_module);
3929 module_exit(nvme_fc_exit_module);
3930 
3931 MODULE_LICENSE("GPL v2");
3932