xref: /linux/drivers/nvme/target/fcloop.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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 
10 #include "../host/nvme.h"
11 #include "../target/nvmet.h"
12 #include <linux/nvme-fc-driver.h>
13 #include <linux/nvme-fc.h>
14 
15 
16 enum {
17 	NVMF_OPT_ERR		= 0,
18 	NVMF_OPT_WWNN		= 1 << 0,
19 	NVMF_OPT_WWPN		= 1 << 1,
20 	NVMF_OPT_ROLES		= 1 << 2,
21 	NVMF_OPT_FCADDR		= 1 << 3,
22 	NVMF_OPT_LPWWNN		= 1 << 4,
23 	NVMF_OPT_LPWWPN		= 1 << 5,
24 };
25 
26 struct fcloop_ctrl_options {
27 	int			mask;
28 	u64			wwnn;
29 	u64			wwpn;
30 	u32			roles;
31 	u32			fcaddr;
32 	u64			lpwwnn;
33 	u64			lpwwpn;
34 };
35 
36 static const match_table_t opt_tokens = {
37 	{ NVMF_OPT_WWNN,	"wwnn=%s"	},
38 	{ NVMF_OPT_WWPN,	"wwpn=%s"	},
39 	{ NVMF_OPT_ROLES,	"roles=%d"	},
40 	{ NVMF_OPT_FCADDR,	"fcaddr=%x"	},
41 	{ NVMF_OPT_LPWWNN,	"lpwwnn=%s"	},
42 	{ NVMF_OPT_LPWWPN,	"lpwwpn=%s"	},
43 	{ NVMF_OPT_ERR,		NULL		}
44 };
45 
46 static int fcloop_verify_addr(substring_t *s)
47 {
48 	size_t blen = s->to - s->from + 1;
49 
50 	if (strnlen(s->from, blen) != NVME_FC_TRADDR_HEXNAMELEN + 2 ||
51 	    strncmp(s->from, "0x", 2))
52 		return -EINVAL;
53 
54 	return 0;
55 }
56 
57 static int
58 fcloop_parse_options(struct fcloop_ctrl_options *opts,
59 		const char *buf)
60 {
61 	substring_t args[MAX_OPT_ARGS];
62 	char *options, *o, *p;
63 	int token, ret = 0;
64 	u64 token64;
65 
66 	options = o = kstrdup(buf, GFP_KERNEL);
67 	if (!options)
68 		return -ENOMEM;
69 
70 	while ((p = strsep(&o, ",\n")) != NULL) {
71 		if (!*p)
72 			continue;
73 
74 		token = match_token(p, opt_tokens, args);
75 		opts->mask |= token;
76 		switch (token) {
77 		case NVMF_OPT_WWNN:
78 			if (fcloop_verify_addr(args) ||
79 			    match_u64(args, &token64)) {
80 				ret = -EINVAL;
81 				goto out_free_options;
82 			}
83 			opts->wwnn = token64;
84 			break;
85 		case NVMF_OPT_WWPN:
86 			if (fcloop_verify_addr(args) ||
87 			    match_u64(args, &token64)) {
88 				ret = -EINVAL;
89 				goto out_free_options;
90 			}
91 			opts->wwpn = token64;
92 			break;
93 		case NVMF_OPT_ROLES:
94 			if (match_int(args, &token)) {
95 				ret = -EINVAL;
96 				goto out_free_options;
97 			}
98 			opts->roles = token;
99 			break;
100 		case NVMF_OPT_FCADDR:
101 			if (match_hex(args, &token)) {
102 				ret = -EINVAL;
103 				goto out_free_options;
104 			}
105 			opts->fcaddr = token;
106 			break;
107 		case NVMF_OPT_LPWWNN:
108 			if (fcloop_verify_addr(args) ||
109 			    match_u64(args, &token64)) {
110 				ret = -EINVAL;
111 				goto out_free_options;
112 			}
113 			opts->lpwwnn = token64;
114 			break;
115 		case NVMF_OPT_LPWWPN:
116 			if (fcloop_verify_addr(args) ||
117 			    match_u64(args, &token64)) {
118 				ret = -EINVAL;
119 				goto out_free_options;
120 			}
121 			opts->lpwwpn = token64;
122 			break;
123 		default:
124 			pr_warn("unknown parameter or missing value '%s'\n", p);
125 			ret = -EINVAL;
126 			goto out_free_options;
127 		}
128 	}
129 
130 out_free_options:
131 	kfree(options);
132 	return ret;
133 }
134 
135 
136 static int
137 fcloop_parse_nm_options(struct device *dev, u64 *nname, u64 *pname,
138 		const char *buf)
139 {
140 	substring_t args[MAX_OPT_ARGS];
141 	char *options, *o, *p;
142 	int token, ret = 0;
143 	u64 token64;
144 
145 	*nname = -1;
146 	*pname = -1;
147 
148 	options = o = kstrdup(buf, GFP_KERNEL);
149 	if (!options)
150 		return -ENOMEM;
151 
152 	while ((p = strsep(&o, ",\n")) != NULL) {
153 		if (!*p)
154 			continue;
155 
156 		token = match_token(p, opt_tokens, args);
157 		switch (token) {
158 		case NVMF_OPT_WWNN:
159 			if (fcloop_verify_addr(args) ||
160 			    match_u64(args, &token64)) {
161 				ret = -EINVAL;
162 				goto out_free_options;
163 			}
164 			*nname = token64;
165 			break;
166 		case NVMF_OPT_WWPN:
167 			if (fcloop_verify_addr(args) ||
168 			    match_u64(args, &token64)) {
169 				ret = -EINVAL;
170 				goto out_free_options;
171 			}
172 			*pname = token64;
173 			break;
174 		default:
175 			pr_warn("unknown parameter or missing value '%s'\n", p);
176 			ret = -EINVAL;
177 			goto out_free_options;
178 		}
179 	}
180 
181 out_free_options:
182 	kfree(options);
183 
184 	if (!ret) {
185 		if (*nname == -1)
186 			return -EINVAL;
187 		if (*pname == -1)
188 			return -EINVAL;
189 	}
190 
191 	return ret;
192 }
193 
194 
195 #define LPORT_OPTS	(NVMF_OPT_WWNN | NVMF_OPT_WWPN)
196 
197 #define RPORT_OPTS	(NVMF_OPT_WWNN | NVMF_OPT_WWPN |  \
198 			 NVMF_OPT_LPWWNN | NVMF_OPT_LPWWPN)
199 
200 #define TGTPORT_OPTS	(NVMF_OPT_WWNN | NVMF_OPT_WWPN)
201 
202 
203 static DEFINE_SPINLOCK(fcloop_lock);
204 static LIST_HEAD(fcloop_lports);
205 static LIST_HEAD(fcloop_nports);
206 
207 struct fcloop_lport {
208 	struct nvme_fc_local_port *localport;
209 	struct list_head lport_list;
210 	struct completion unreg_done;
211 };
212 
213 struct fcloop_lport_priv {
214 	struct fcloop_lport *lport;
215 };
216 
217 struct fcloop_rport {
218 	struct nvme_fc_remote_port	*remoteport;
219 	struct nvmet_fc_target_port	*targetport;
220 	struct fcloop_nport		*nport;
221 	struct fcloop_lport		*lport;
222 	spinlock_t			lock;
223 	struct list_head		ls_list;
224 	struct work_struct		ls_work;
225 };
226 
227 struct fcloop_tport {
228 	struct nvmet_fc_target_port	*targetport;
229 	struct nvme_fc_remote_port	*remoteport;
230 	struct fcloop_nport		*nport;
231 	struct fcloop_lport		*lport;
232 	spinlock_t			lock;
233 	struct list_head		ls_list;
234 	struct work_struct		ls_work;
235 };
236 
237 struct fcloop_nport {
238 	struct fcloop_rport *rport;
239 	struct fcloop_tport *tport;
240 	struct fcloop_lport *lport;
241 	struct list_head nport_list;
242 	struct kref ref;
243 	u64 node_name;
244 	u64 port_name;
245 	u32 port_role;
246 	u32 port_id;
247 };
248 
249 struct fcloop_lsreq {
250 	struct nvmefc_ls_req		*lsreq;
251 	struct nvmefc_ls_rsp		ls_rsp;
252 	int				lsdir;	/* H2T or T2H */
253 	int				status;
254 	struct list_head		ls_list; /* fcloop_rport->ls_list */
255 };
256 
257 struct fcloop_rscn {
258 	struct fcloop_tport		*tport;
259 	struct work_struct		work;
260 };
261 
262 enum {
263 	INI_IO_START		= 0,
264 	INI_IO_ACTIVE		= 1,
265 	INI_IO_ABORTED		= 2,
266 	INI_IO_COMPLETED	= 3,
267 };
268 
269 struct fcloop_fcpreq {
270 	struct fcloop_tport		*tport;
271 	struct nvmefc_fcp_req		*fcpreq;
272 	spinlock_t			reqlock;
273 	u16				status;
274 	u32				inistate;
275 	bool				active;
276 	bool				aborted;
277 	struct kref			ref;
278 	struct work_struct		fcp_rcv_work;
279 	struct work_struct		abort_rcv_work;
280 	struct work_struct		tio_done_work;
281 	struct nvmefc_tgt_fcp_req	tgt_fcp_req;
282 };
283 
284 struct fcloop_ini_fcpreq {
285 	struct nvmefc_fcp_req		*fcpreq;
286 	struct fcloop_fcpreq		*tfcp_req;
287 	spinlock_t			inilock;
288 };
289 
290 static inline struct fcloop_lsreq *
291 ls_rsp_to_lsreq(struct nvmefc_ls_rsp *lsrsp)
292 {
293 	return container_of(lsrsp, struct fcloop_lsreq, ls_rsp);
294 }
295 
296 static inline struct fcloop_fcpreq *
297 tgt_fcp_req_to_fcpreq(struct nvmefc_tgt_fcp_req *tgt_fcpreq)
298 {
299 	return container_of(tgt_fcpreq, struct fcloop_fcpreq, tgt_fcp_req);
300 }
301 
302 
303 static int
304 fcloop_create_queue(struct nvme_fc_local_port *localport,
305 			unsigned int qidx, u16 qsize,
306 			void **handle)
307 {
308 	*handle = localport;
309 	return 0;
310 }
311 
312 static void
313 fcloop_delete_queue(struct nvme_fc_local_port *localport,
314 			unsigned int idx, void *handle)
315 {
316 }
317 
318 static void
319 fcloop_rport_lsrqst_work(struct work_struct *work)
320 {
321 	struct fcloop_rport *rport =
322 		container_of(work, struct fcloop_rport, ls_work);
323 	struct fcloop_lsreq *tls_req;
324 
325 	spin_lock(&rport->lock);
326 	for (;;) {
327 		tls_req = list_first_entry_or_null(&rport->ls_list,
328 				struct fcloop_lsreq, ls_list);
329 		if (!tls_req)
330 			break;
331 
332 		list_del(&tls_req->ls_list);
333 		spin_unlock(&rport->lock);
334 
335 		tls_req->lsreq->done(tls_req->lsreq, tls_req->status);
336 		/*
337 		 * callee may free memory containing tls_req.
338 		 * do not reference lsreq after this.
339 		 */
340 
341 		spin_lock(&rport->lock);
342 	}
343 	spin_unlock(&rport->lock);
344 }
345 
346 static int
347 fcloop_h2t_ls_req(struct nvme_fc_local_port *localport,
348 			struct nvme_fc_remote_port *remoteport,
349 			struct nvmefc_ls_req *lsreq)
350 {
351 	struct fcloop_lsreq *tls_req = lsreq->private;
352 	struct fcloop_rport *rport = remoteport->private;
353 	int ret = 0;
354 
355 	tls_req->lsreq = lsreq;
356 	INIT_LIST_HEAD(&tls_req->ls_list);
357 
358 	if (!rport->targetport) {
359 		tls_req->status = -ECONNREFUSED;
360 		spin_lock(&rport->lock);
361 		list_add_tail(&tls_req->ls_list, &rport->ls_list);
362 		spin_unlock(&rport->lock);
363 		queue_work(nvmet_wq, &rport->ls_work);
364 		return ret;
365 	}
366 
367 	tls_req->status = 0;
368 	ret = nvmet_fc_rcv_ls_req(rport->targetport, rport,
369 				  &tls_req->ls_rsp,
370 				  lsreq->rqstaddr, lsreq->rqstlen);
371 
372 	return ret;
373 }
374 
375 static int
376 fcloop_h2t_xmt_ls_rsp(struct nvmet_fc_target_port *targetport,
377 			struct nvmefc_ls_rsp *lsrsp)
378 {
379 	struct fcloop_lsreq *tls_req = ls_rsp_to_lsreq(lsrsp);
380 	struct nvmefc_ls_req *lsreq = tls_req->lsreq;
381 	struct fcloop_tport *tport = targetport->private;
382 	struct nvme_fc_remote_port *remoteport = tport->remoteport;
383 	struct fcloop_rport *rport;
384 
385 	memcpy(lsreq->rspaddr, lsrsp->rspbuf,
386 		((lsreq->rsplen < lsrsp->rsplen) ?
387 				lsreq->rsplen : lsrsp->rsplen));
388 
389 	lsrsp->done(lsrsp);
390 
391 	if (remoteport) {
392 		rport = remoteport->private;
393 		spin_lock(&rport->lock);
394 		list_add_tail(&tls_req->ls_list, &rport->ls_list);
395 		spin_unlock(&rport->lock);
396 		queue_work(nvmet_wq, &rport->ls_work);
397 	}
398 
399 	return 0;
400 }
401 
402 static void
403 fcloop_tport_lsrqst_work(struct work_struct *work)
404 {
405 	struct fcloop_tport *tport =
406 		container_of(work, struct fcloop_tport, ls_work);
407 	struct fcloop_lsreq *tls_req;
408 
409 	spin_lock(&tport->lock);
410 	for (;;) {
411 		tls_req = list_first_entry_or_null(&tport->ls_list,
412 				struct fcloop_lsreq, ls_list);
413 		if (!tls_req)
414 			break;
415 
416 		list_del(&tls_req->ls_list);
417 		spin_unlock(&tport->lock);
418 
419 		tls_req->lsreq->done(tls_req->lsreq, tls_req->status);
420 		/*
421 		 * callee may free memory containing tls_req.
422 		 * do not reference lsreq after this.
423 		 */
424 
425 		spin_lock(&tport->lock);
426 	}
427 	spin_unlock(&tport->lock);
428 }
429 
430 static int
431 fcloop_t2h_ls_req(struct nvmet_fc_target_port *targetport, void *hosthandle,
432 			struct nvmefc_ls_req *lsreq)
433 {
434 	struct fcloop_lsreq *tls_req = lsreq->private;
435 	struct fcloop_tport *tport = targetport->private;
436 	int ret = 0;
437 
438 	/*
439 	 * hosthandle should be the dst.rport value.
440 	 * hosthandle ignored as fcloop currently is
441 	 * 1:1 tgtport vs remoteport
442 	 */
443 	tls_req->lsreq = lsreq;
444 	INIT_LIST_HEAD(&tls_req->ls_list);
445 
446 	if (!tport->remoteport) {
447 		tls_req->status = -ECONNREFUSED;
448 		spin_lock(&tport->lock);
449 		list_add_tail(&tls_req->ls_list, &tport->ls_list);
450 		spin_unlock(&tport->lock);
451 		queue_work(nvmet_wq, &tport->ls_work);
452 		return ret;
453 	}
454 
455 	tls_req->status = 0;
456 	ret = nvme_fc_rcv_ls_req(tport->remoteport, &tls_req->ls_rsp,
457 				 lsreq->rqstaddr, lsreq->rqstlen);
458 
459 	return ret;
460 }
461 
462 static int
463 fcloop_t2h_xmt_ls_rsp(struct nvme_fc_local_port *localport,
464 			struct nvme_fc_remote_port *remoteport,
465 			struct nvmefc_ls_rsp *lsrsp)
466 {
467 	struct fcloop_lsreq *tls_req = ls_rsp_to_lsreq(lsrsp);
468 	struct nvmefc_ls_req *lsreq = tls_req->lsreq;
469 	struct fcloop_rport *rport = remoteport->private;
470 	struct nvmet_fc_target_port *targetport = rport->targetport;
471 	struct fcloop_tport *tport;
472 
473 	memcpy(lsreq->rspaddr, lsrsp->rspbuf,
474 		((lsreq->rsplen < lsrsp->rsplen) ?
475 				lsreq->rsplen : lsrsp->rsplen));
476 	lsrsp->done(lsrsp);
477 
478 	if (targetport) {
479 		tport = targetport->private;
480 		spin_lock(&tport->lock);
481 		list_add_tail(&tport->ls_list, &tls_req->ls_list);
482 		spin_unlock(&tport->lock);
483 		queue_work(nvmet_wq, &tport->ls_work);
484 	}
485 
486 	return 0;
487 }
488 
489 static void
490 fcloop_t2h_host_release(void *hosthandle)
491 {
492 	/* host handle ignored for now */
493 }
494 
495 static int
496 fcloop_t2h_host_traddr(void *hosthandle, u64 *wwnn, u64 *wwpn)
497 {
498 	struct fcloop_rport *rport = hosthandle;
499 
500 	*wwnn = rport->lport->localport->node_name;
501 	*wwpn = rport->lport->localport->port_name;
502 	return 0;
503 }
504 
505 /*
506  * Simulate reception of RSCN and converting it to a initiator transport
507  * call to rescan a remote port.
508  */
509 static void
510 fcloop_tgt_rscn_work(struct work_struct *work)
511 {
512 	struct fcloop_rscn *tgt_rscn =
513 		container_of(work, struct fcloop_rscn, work);
514 	struct fcloop_tport *tport = tgt_rscn->tport;
515 
516 	if (tport->remoteport)
517 		nvme_fc_rescan_remoteport(tport->remoteport);
518 	kfree(tgt_rscn);
519 }
520 
521 static void
522 fcloop_tgt_discovery_evt(struct nvmet_fc_target_port *tgtport)
523 {
524 	struct fcloop_rscn *tgt_rscn;
525 
526 	tgt_rscn = kzalloc(sizeof(*tgt_rscn), GFP_KERNEL);
527 	if (!tgt_rscn)
528 		return;
529 
530 	tgt_rscn->tport = tgtport->private;
531 	INIT_WORK(&tgt_rscn->work, fcloop_tgt_rscn_work);
532 
533 	queue_work(nvmet_wq, &tgt_rscn->work);
534 }
535 
536 static void
537 fcloop_tfcp_req_free(struct kref *ref)
538 {
539 	struct fcloop_fcpreq *tfcp_req =
540 		container_of(ref, struct fcloop_fcpreq, ref);
541 
542 	kfree(tfcp_req);
543 }
544 
545 static void
546 fcloop_tfcp_req_put(struct fcloop_fcpreq *tfcp_req)
547 {
548 	kref_put(&tfcp_req->ref, fcloop_tfcp_req_free);
549 }
550 
551 static int
552 fcloop_tfcp_req_get(struct fcloop_fcpreq *tfcp_req)
553 {
554 	return kref_get_unless_zero(&tfcp_req->ref);
555 }
556 
557 static void
558 fcloop_call_host_done(struct nvmefc_fcp_req *fcpreq,
559 			struct fcloop_fcpreq *tfcp_req, int status)
560 {
561 	struct fcloop_ini_fcpreq *inireq = NULL;
562 
563 	if (fcpreq) {
564 		inireq = fcpreq->private;
565 		spin_lock(&inireq->inilock);
566 		inireq->tfcp_req = NULL;
567 		spin_unlock(&inireq->inilock);
568 
569 		fcpreq->status = status;
570 		fcpreq->done(fcpreq);
571 	}
572 
573 	/* release original io reference on tgt struct */
574 	fcloop_tfcp_req_put(tfcp_req);
575 }
576 
577 static bool drop_fabric_opcode;
578 #define DROP_OPCODE_MASK	0x00FF
579 /* fabrics opcode will have a bit set above 1st byte */
580 static int drop_opcode = -1;
581 static int drop_instance;
582 static int drop_amount;
583 static int drop_current_cnt;
584 
585 /*
586  * Routine to parse io and determine if the io is to be dropped.
587  * Returns:
588  *  0 if io is not obstructed
589  *  1 if io was dropped
590  */
591 static int check_for_drop(struct fcloop_fcpreq *tfcp_req)
592 {
593 	struct nvmefc_fcp_req *fcpreq = tfcp_req->fcpreq;
594 	struct nvme_fc_cmd_iu *cmdiu = fcpreq->cmdaddr;
595 	struct nvme_command *sqe = &cmdiu->sqe;
596 
597 	if (drop_opcode == -1)
598 		return 0;
599 
600 	pr_info("%s: seq opcd x%02x fctype x%02x: drop F %s op x%02x "
601 		"inst %d start %d amt %d\n",
602 		__func__, sqe->common.opcode, sqe->fabrics.fctype,
603 		drop_fabric_opcode ? "y" : "n",
604 		drop_opcode, drop_current_cnt, drop_instance, drop_amount);
605 
606 	if ((drop_fabric_opcode &&
607 	     (sqe->common.opcode != nvme_fabrics_command ||
608 	      sqe->fabrics.fctype != drop_opcode)) ||
609 	    (!drop_fabric_opcode && sqe->common.opcode != drop_opcode))
610 		return 0;
611 
612 	if (++drop_current_cnt >= drop_instance) {
613 		if (drop_current_cnt >= drop_instance + drop_amount)
614 			drop_opcode = -1;
615 		return 1;
616 	}
617 
618 	return 0;
619 }
620 
621 static void
622 fcloop_fcp_recv_work(struct work_struct *work)
623 {
624 	struct fcloop_fcpreq *tfcp_req =
625 		container_of(work, struct fcloop_fcpreq, fcp_rcv_work);
626 	struct nvmefc_fcp_req *fcpreq = tfcp_req->fcpreq;
627 	unsigned long flags;
628 	int ret = 0;
629 	bool aborted = false;
630 
631 	spin_lock_irqsave(&tfcp_req->reqlock, flags);
632 	switch (tfcp_req->inistate) {
633 	case INI_IO_START:
634 		tfcp_req->inistate = INI_IO_ACTIVE;
635 		break;
636 	case INI_IO_ABORTED:
637 		aborted = true;
638 		break;
639 	default:
640 		spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
641 		WARN_ON(1);
642 		return;
643 	}
644 	spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
645 
646 	if (unlikely(aborted))
647 		ret = -ECANCELED;
648 	else {
649 		if (likely(!check_for_drop(tfcp_req)))
650 			ret = nvmet_fc_rcv_fcp_req(tfcp_req->tport->targetport,
651 				&tfcp_req->tgt_fcp_req,
652 				fcpreq->cmdaddr, fcpreq->cmdlen);
653 		else
654 			pr_info("%s: dropped command ********\n", __func__);
655 	}
656 	if (ret)
657 		fcloop_call_host_done(fcpreq, tfcp_req, ret);
658 }
659 
660 static void
661 fcloop_fcp_abort_recv_work(struct work_struct *work)
662 {
663 	struct fcloop_fcpreq *tfcp_req =
664 		container_of(work, struct fcloop_fcpreq, abort_rcv_work);
665 	struct nvmefc_fcp_req *fcpreq;
666 	bool completed = false;
667 	unsigned long flags;
668 
669 	spin_lock_irqsave(&tfcp_req->reqlock, flags);
670 	fcpreq = tfcp_req->fcpreq;
671 	switch (tfcp_req->inistate) {
672 	case INI_IO_ABORTED:
673 		break;
674 	case INI_IO_COMPLETED:
675 		completed = true;
676 		break;
677 	default:
678 		spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
679 		WARN_ON(1);
680 		return;
681 	}
682 	spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
683 
684 	if (unlikely(completed)) {
685 		/* remove reference taken in original abort downcall */
686 		fcloop_tfcp_req_put(tfcp_req);
687 		return;
688 	}
689 
690 	if (tfcp_req->tport->targetport)
691 		nvmet_fc_rcv_fcp_abort(tfcp_req->tport->targetport,
692 					&tfcp_req->tgt_fcp_req);
693 
694 	spin_lock_irqsave(&tfcp_req->reqlock, flags);
695 	tfcp_req->fcpreq = NULL;
696 	spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
697 
698 	fcloop_call_host_done(fcpreq, tfcp_req, -ECANCELED);
699 	/* call_host_done releases reference for abort downcall */
700 }
701 
702 /*
703  * FCP IO operation done by target completion.
704  * call back up initiator "done" flows.
705  */
706 static void
707 fcloop_tgt_fcprqst_done_work(struct work_struct *work)
708 {
709 	struct fcloop_fcpreq *tfcp_req =
710 		container_of(work, struct fcloop_fcpreq, tio_done_work);
711 	struct nvmefc_fcp_req *fcpreq;
712 	unsigned long flags;
713 
714 	spin_lock_irqsave(&tfcp_req->reqlock, flags);
715 	fcpreq = tfcp_req->fcpreq;
716 	tfcp_req->inistate = INI_IO_COMPLETED;
717 	spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
718 
719 	fcloop_call_host_done(fcpreq, tfcp_req, tfcp_req->status);
720 }
721 
722 
723 static int
724 fcloop_fcp_req(struct nvme_fc_local_port *localport,
725 			struct nvme_fc_remote_port *remoteport,
726 			void *hw_queue_handle,
727 			struct nvmefc_fcp_req *fcpreq)
728 {
729 	struct fcloop_rport *rport = remoteport->private;
730 	struct fcloop_ini_fcpreq *inireq = fcpreq->private;
731 	struct fcloop_fcpreq *tfcp_req;
732 
733 	if (!rport->targetport)
734 		return -ECONNREFUSED;
735 
736 	tfcp_req = kzalloc(sizeof(*tfcp_req), GFP_ATOMIC);
737 	if (!tfcp_req)
738 		return -ENOMEM;
739 
740 	inireq->fcpreq = fcpreq;
741 	inireq->tfcp_req = tfcp_req;
742 	spin_lock_init(&inireq->inilock);
743 
744 	tfcp_req->fcpreq = fcpreq;
745 	tfcp_req->tport = rport->targetport->private;
746 	tfcp_req->inistate = INI_IO_START;
747 	spin_lock_init(&tfcp_req->reqlock);
748 	INIT_WORK(&tfcp_req->fcp_rcv_work, fcloop_fcp_recv_work);
749 	INIT_WORK(&tfcp_req->abort_rcv_work, fcloop_fcp_abort_recv_work);
750 	INIT_WORK(&tfcp_req->tio_done_work, fcloop_tgt_fcprqst_done_work);
751 	kref_init(&tfcp_req->ref);
752 
753 	queue_work(nvmet_wq, &tfcp_req->fcp_rcv_work);
754 
755 	return 0;
756 }
757 
758 static void
759 fcloop_fcp_copy_data(u8 op, struct scatterlist *data_sg,
760 			struct scatterlist *io_sg, u32 offset, u32 length)
761 {
762 	void *data_p, *io_p;
763 	u32 data_len, io_len, tlen;
764 
765 	io_p = sg_virt(io_sg);
766 	io_len = io_sg->length;
767 
768 	for ( ; offset; ) {
769 		tlen = min_t(u32, offset, io_len);
770 		offset -= tlen;
771 		io_len -= tlen;
772 		if (!io_len) {
773 			io_sg = sg_next(io_sg);
774 			io_p = sg_virt(io_sg);
775 			io_len = io_sg->length;
776 		} else
777 			io_p += tlen;
778 	}
779 
780 	data_p = sg_virt(data_sg);
781 	data_len = data_sg->length;
782 
783 	for ( ; length; ) {
784 		tlen = min_t(u32, io_len, data_len);
785 		tlen = min_t(u32, tlen, length);
786 
787 		if (op == NVMET_FCOP_WRITEDATA)
788 			memcpy(data_p, io_p, tlen);
789 		else
790 			memcpy(io_p, data_p, tlen);
791 
792 		length -= tlen;
793 
794 		io_len -= tlen;
795 		if ((!io_len) && (length)) {
796 			io_sg = sg_next(io_sg);
797 			io_p = sg_virt(io_sg);
798 			io_len = io_sg->length;
799 		} else
800 			io_p += tlen;
801 
802 		data_len -= tlen;
803 		if ((!data_len) && (length)) {
804 			data_sg = sg_next(data_sg);
805 			data_p = sg_virt(data_sg);
806 			data_len = data_sg->length;
807 		} else
808 			data_p += tlen;
809 	}
810 }
811 
812 static int
813 fcloop_fcp_op(struct nvmet_fc_target_port *tgtport,
814 			struct nvmefc_tgt_fcp_req *tgt_fcpreq)
815 {
816 	struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
817 	struct nvmefc_fcp_req *fcpreq;
818 	u32 rsplen = 0, xfrlen = 0;
819 	int fcp_err = 0, active, aborted;
820 	u8 op = tgt_fcpreq->op;
821 	unsigned long flags;
822 
823 	spin_lock_irqsave(&tfcp_req->reqlock, flags);
824 	fcpreq = tfcp_req->fcpreq;
825 	active = tfcp_req->active;
826 	aborted = tfcp_req->aborted;
827 	tfcp_req->active = true;
828 	spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
829 
830 	if (unlikely(active))
831 		/* illegal - call while i/o active */
832 		return -EALREADY;
833 
834 	if (unlikely(aborted)) {
835 		/* target transport has aborted i/o prior */
836 		spin_lock_irqsave(&tfcp_req->reqlock, flags);
837 		tfcp_req->active = false;
838 		spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
839 		tgt_fcpreq->transferred_length = 0;
840 		tgt_fcpreq->fcp_error = -ECANCELED;
841 		tgt_fcpreq->done(tgt_fcpreq);
842 		return 0;
843 	}
844 
845 	/*
846 	 * if fcpreq is NULL, the I/O has been aborted (from
847 	 * initiator side). For the target side, act as if all is well
848 	 * but don't actually move data.
849 	 */
850 
851 	switch (op) {
852 	case NVMET_FCOP_WRITEDATA:
853 		xfrlen = tgt_fcpreq->transfer_length;
854 		if (fcpreq) {
855 			fcloop_fcp_copy_data(op, tgt_fcpreq->sg,
856 					fcpreq->first_sgl, tgt_fcpreq->offset,
857 					xfrlen);
858 			fcpreq->transferred_length += xfrlen;
859 		}
860 		break;
861 
862 	case NVMET_FCOP_READDATA:
863 	case NVMET_FCOP_READDATA_RSP:
864 		xfrlen = tgt_fcpreq->transfer_length;
865 		if (fcpreq) {
866 			fcloop_fcp_copy_data(op, tgt_fcpreq->sg,
867 					fcpreq->first_sgl, tgt_fcpreq->offset,
868 					xfrlen);
869 			fcpreq->transferred_length += xfrlen;
870 		}
871 		if (op == NVMET_FCOP_READDATA)
872 			break;
873 
874 		/* Fall-Thru to RSP handling */
875 		fallthrough;
876 
877 	case NVMET_FCOP_RSP:
878 		if (fcpreq) {
879 			rsplen = ((fcpreq->rsplen < tgt_fcpreq->rsplen) ?
880 					fcpreq->rsplen : tgt_fcpreq->rsplen);
881 			memcpy(fcpreq->rspaddr, tgt_fcpreq->rspaddr, rsplen);
882 			if (rsplen < tgt_fcpreq->rsplen)
883 				fcp_err = -E2BIG;
884 			fcpreq->rcv_rsplen = rsplen;
885 			fcpreq->status = 0;
886 		}
887 		tfcp_req->status = 0;
888 		break;
889 
890 	default:
891 		fcp_err = -EINVAL;
892 		break;
893 	}
894 
895 	spin_lock_irqsave(&tfcp_req->reqlock, flags);
896 	tfcp_req->active = false;
897 	spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
898 
899 	tgt_fcpreq->transferred_length = xfrlen;
900 	tgt_fcpreq->fcp_error = fcp_err;
901 	tgt_fcpreq->done(tgt_fcpreq);
902 
903 	return 0;
904 }
905 
906 static void
907 fcloop_tgt_fcp_abort(struct nvmet_fc_target_port *tgtport,
908 			struct nvmefc_tgt_fcp_req *tgt_fcpreq)
909 {
910 	struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
911 	unsigned long flags;
912 
913 	/*
914 	 * mark aborted only in case there were 2 threads in transport
915 	 * (one doing io, other doing abort) and only kills ops posted
916 	 * after the abort request
917 	 */
918 	spin_lock_irqsave(&tfcp_req->reqlock, flags);
919 	tfcp_req->aborted = true;
920 	spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
921 
922 	tfcp_req->status = NVME_SC_INTERNAL;
923 
924 	/*
925 	 * nothing more to do. If io wasn't active, the transport should
926 	 * immediately call the req_release. If it was active, the op
927 	 * will complete, and the lldd should call req_release.
928 	 */
929 }
930 
931 static void
932 fcloop_fcp_req_release(struct nvmet_fc_target_port *tgtport,
933 			struct nvmefc_tgt_fcp_req *tgt_fcpreq)
934 {
935 	struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
936 
937 	queue_work(nvmet_wq, &tfcp_req->tio_done_work);
938 }
939 
940 static void
941 fcloop_h2t_ls_abort(struct nvme_fc_local_port *localport,
942 			struct nvme_fc_remote_port *remoteport,
943 				struct nvmefc_ls_req *lsreq)
944 {
945 }
946 
947 static void
948 fcloop_t2h_ls_abort(struct nvmet_fc_target_port *targetport,
949 			void *hosthandle, struct nvmefc_ls_req *lsreq)
950 {
951 }
952 
953 static void
954 fcloop_fcp_abort(struct nvme_fc_local_port *localport,
955 			struct nvme_fc_remote_port *remoteport,
956 			void *hw_queue_handle,
957 			struct nvmefc_fcp_req *fcpreq)
958 {
959 	struct fcloop_ini_fcpreq *inireq = fcpreq->private;
960 	struct fcloop_fcpreq *tfcp_req;
961 	bool abortio = true;
962 	unsigned long flags;
963 
964 	spin_lock(&inireq->inilock);
965 	tfcp_req = inireq->tfcp_req;
966 	if (tfcp_req)
967 		fcloop_tfcp_req_get(tfcp_req);
968 	spin_unlock(&inireq->inilock);
969 
970 	if (!tfcp_req)
971 		/* abort has already been called */
972 		return;
973 
974 	/* break initiator/target relationship for io */
975 	spin_lock_irqsave(&tfcp_req->reqlock, flags);
976 	switch (tfcp_req->inistate) {
977 	case INI_IO_START:
978 	case INI_IO_ACTIVE:
979 		tfcp_req->inistate = INI_IO_ABORTED;
980 		break;
981 	case INI_IO_COMPLETED:
982 		abortio = false;
983 		break;
984 	default:
985 		spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
986 		WARN_ON(1);
987 		return;
988 	}
989 	spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
990 
991 	if (abortio)
992 		/* leave the reference while the work item is scheduled */
993 		WARN_ON(!queue_work(nvmet_wq, &tfcp_req->abort_rcv_work));
994 	else  {
995 		/*
996 		 * as the io has already had the done callback made,
997 		 * nothing more to do. So release the reference taken above
998 		 */
999 		fcloop_tfcp_req_put(tfcp_req);
1000 	}
1001 }
1002 
1003 static void
1004 fcloop_nport_free(struct kref *ref)
1005 {
1006 	struct fcloop_nport *nport =
1007 		container_of(ref, struct fcloop_nport, ref);
1008 
1009 	kfree(nport);
1010 }
1011 
1012 static void
1013 fcloop_nport_put(struct fcloop_nport *nport)
1014 {
1015 	kref_put(&nport->ref, fcloop_nport_free);
1016 }
1017 
1018 static int
1019 fcloop_nport_get(struct fcloop_nport *nport)
1020 {
1021 	return kref_get_unless_zero(&nport->ref);
1022 }
1023 
1024 static void
1025 fcloop_localport_delete(struct nvme_fc_local_port *localport)
1026 {
1027 	struct fcloop_lport_priv *lport_priv = localport->private;
1028 	struct fcloop_lport *lport = lport_priv->lport;
1029 
1030 	/* release any threads waiting for the unreg to complete */
1031 	complete(&lport->unreg_done);
1032 }
1033 
1034 static void
1035 fcloop_remoteport_delete(struct nvme_fc_remote_port *remoteport)
1036 {
1037 	struct fcloop_rport *rport = remoteport->private;
1038 
1039 	flush_work(&rport->ls_work);
1040 	fcloop_nport_put(rport->nport);
1041 }
1042 
1043 static void
1044 fcloop_targetport_delete(struct nvmet_fc_target_port *targetport)
1045 {
1046 	struct fcloop_tport *tport = targetport->private;
1047 
1048 	flush_work(&tport->ls_work);
1049 	fcloop_nport_put(tport->nport);
1050 }
1051 
1052 #define	FCLOOP_HW_QUEUES		4
1053 #define	FCLOOP_SGL_SEGS			256
1054 #define FCLOOP_DMABOUND_4G		0xFFFFFFFF
1055 
1056 static struct nvme_fc_port_template fctemplate = {
1057 	.localport_delete	= fcloop_localport_delete,
1058 	.remoteport_delete	= fcloop_remoteport_delete,
1059 	.create_queue		= fcloop_create_queue,
1060 	.delete_queue		= fcloop_delete_queue,
1061 	.ls_req			= fcloop_h2t_ls_req,
1062 	.fcp_io			= fcloop_fcp_req,
1063 	.ls_abort		= fcloop_h2t_ls_abort,
1064 	.fcp_abort		= fcloop_fcp_abort,
1065 	.xmt_ls_rsp		= fcloop_t2h_xmt_ls_rsp,
1066 	.max_hw_queues		= FCLOOP_HW_QUEUES,
1067 	.max_sgl_segments	= FCLOOP_SGL_SEGS,
1068 	.max_dif_sgl_segments	= FCLOOP_SGL_SEGS,
1069 	.dma_boundary		= FCLOOP_DMABOUND_4G,
1070 	/* sizes of additional private data for data structures */
1071 	.local_priv_sz		= sizeof(struct fcloop_lport_priv),
1072 	.remote_priv_sz		= sizeof(struct fcloop_rport),
1073 	.lsrqst_priv_sz		= sizeof(struct fcloop_lsreq),
1074 	.fcprqst_priv_sz	= sizeof(struct fcloop_ini_fcpreq),
1075 };
1076 
1077 static struct nvmet_fc_target_template tgttemplate = {
1078 	.targetport_delete	= fcloop_targetport_delete,
1079 	.xmt_ls_rsp		= fcloop_h2t_xmt_ls_rsp,
1080 	.fcp_op			= fcloop_fcp_op,
1081 	.fcp_abort		= fcloop_tgt_fcp_abort,
1082 	.fcp_req_release	= fcloop_fcp_req_release,
1083 	.discovery_event	= fcloop_tgt_discovery_evt,
1084 	.ls_req			= fcloop_t2h_ls_req,
1085 	.ls_abort		= fcloop_t2h_ls_abort,
1086 	.host_release		= fcloop_t2h_host_release,
1087 	.host_traddr		= fcloop_t2h_host_traddr,
1088 	.max_hw_queues		= FCLOOP_HW_QUEUES,
1089 	.max_sgl_segments	= FCLOOP_SGL_SEGS,
1090 	.max_dif_sgl_segments	= FCLOOP_SGL_SEGS,
1091 	.dma_boundary		= FCLOOP_DMABOUND_4G,
1092 	/* optional features */
1093 	.target_features	= 0,
1094 	/* sizes of additional private data for data structures */
1095 	.target_priv_sz		= sizeof(struct fcloop_tport),
1096 	.lsrqst_priv_sz		= sizeof(struct fcloop_lsreq),
1097 };
1098 
1099 static ssize_t
1100 fcloop_create_local_port(struct device *dev, struct device_attribute *attr,
1101 		const char *buf, size_t count)
1102 {
1103 	struct nvme_fc_port_info pinfo;
1104 	struct fcloop_ctrl_options *opts;
1105 	struct nvme_fc_local_port *localport;
1106 	struct fcloop_lport *lport;
1107 	struct fcloop_lport_priv *lport_priv;
1108 	unsigned long flags;
1109 	int ret = -ENOMEM;
1110 
1111 	lport = kzalloc(sizeof(*lport), GFP_KERNEL);
1112 	if (!lport)
1113 		return -ENOMEM;
1114 
1115 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1116 	if (!opts)
1117 		goto out_free_lport;
1118 
1119 	ret = fcloop_parse_options(opts, buf);
1120 	if (ret)
1121 		goto out_free_opts;
1122 
1123 	/* everything there ? */
1124 	if ((opts->mask & LPORT_OPTS) != LPORT_OPTS) {
1125 		ret = -EINVAL;
1126 		goto out_free_opts;
1127 	}
1128 
1129 	memset(&pinfo, 0, sizeof(pinfo));
1130 	pinfo.node_name = opts->wwnn;
1131 	pinfo.port_name = opts->wwpn;
1132 	pinfo.port_role = opts->roles;
1133 	pinfo.port_id = opts->fcaddr;
1134 
1135 	ret = nvme_fc_register_localport(&pinfo, &fctemplate, NULL, &localport);
1136 	if (!ret) {
1137 		/* success */
1138 		lport_priv = localport->private;
1139 		lport_priv->lport = lport;
1140 
1141 		lport->localport = localport;
1142 		INIT_LIST_HEAD(&lport->lport_list);
1143 
1144 		spin_lock_irqsave(&fcloop_lock, flags);
1145 		list_add_tail(&lport->lport_list, &fcloop_lports);
1146 		spin_unlock_irqrestore(&fcloop_lock, flags);
1147 	}
1148 
1149 out_free_opts:
1150 	kfree(opts);
1151 out_free_lport:
1152 	/* free only if we're going to fail */
1153 	if (ret)
1154 		kfree(lport);
1155 
1156 	return ret ? ret : count;
1157 }
1158 
1159 
1160 static void
1161 __unlink_local_port(struct fcloop_lport *lport)
1162 {
1163 	list_del(&lport->lport_list);
1164 }
1165 
1166 static int
1167 __wait_localport_unreg(struct fcloop_lport *lport)
1168 {
1169 	int ret;
1170 
1171 	init_completion(&lport->unreg_done);
1172 
1173 	ret = nvme_fc_unregister_localport(lport->localport);
1174 
1175 	if (!ret)
1176 		wait_for_completion(&lport->unreg_done);
1177 
1178 	kfree(lport);
1179 
1180 	return ret;
1181 }
1182 
1183 
1184 static ssize_t
1185 fcloop_delete_local_port(struct device *dev, struct device_attribute *attr,
1186 		const char *buf, size_t count)
1187 {
1188 	struct fcloop_lport *tlport, *lport = NULL;
1189 	u64 nodename, portname;
1190 	unsigned long flags;
1191 	int ret;
1192 
1193 	ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
1194 	if (ret)
1195 		return ret;
1196 
1197 	spin_lock_irqsave(&fcloop_lock, flags);
1198 
1199 	list_for_each_entry(tlport, &fcloop_lports, lport_list) {
1200 		if (tlport->localport->node_name == nodename &&
1201 		    tlport->localport->port_name == portname) {
1202 			lport = tlport;
1203 			__unlink_local_port(lport);
1204 			break;
1205 		}
1206 	}
1207 	spin_unlock_irqrestore(&fcloop_lock, flags);
1208 
1209 	if (!lport)
1210 		return -ENOENT;
1211 
1212 	ret = __wait_localport_unreg(lport);
1213 
1214 	return ret ? ret : count;
1215 }
1216 
1217 static struct fcloop_nport *
1218 fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
1219 {
1220 	struct fcloop_nport *newnport, *nport = NULL;
1221 	struct fcloop_lport *tmplport, *lport = NULL;
1222 	struct fcloop_ctrl_options *opts;
1223 	unsigned long flags;
1224 	u32 opts_mask = (remoteport) ? RPORT_OPTS : TGTPORT_OPTS;
1225 	int ret;
1226 
1227 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1228 	if (!opts)
1229 		return NULL;
1230 
1231 	ret = fcloop_parse_options(opts, buf);
1232 	if (ret)
1233 		goto out_free_opts;
1234 
1235 	/* everything there ? */
1236 	if ((opts->mask & opts_mask) != opts_mask) {
1237 		ret = -EINVAL;
1238 		goto out_free_opts;
1239 	}
1240 
1241 	newnport = kzalloc(sizeof(*newnport), GFP_KERNEL);
1242 	if (!newnport)
1243 		goto out_free_opts;
1244 
1245 	INIT_LIST_HEAD(&newnport->nport_list);
1246 	newnport->node_name = opts->wwnn;
1247 	newnport->port_name = opts->wwpn;
1248 	if (opts->mask & NVMF_OPT_ROLES)
1249 		newnport->port_role = opts->roles;
1250 	if (opts->mask & NVMF_OPT_FCADDR)
1251 		newnport->port_id = opts->fcaddr;
1252 	kref_init(&newnport->ref);
1253 
1254 	spin_lock_irqsave(&fcloop_lock, flags);
1255 
1256 	list_for_each_entry(tmplport, &fcloop_lports, lport_list) {
1257 		if (tmplport->localport->node_name == opts->wwnn &&
1258 		    tmplport->localport->port_name == opts->wwpn)
1259 			goto out_invalid_opts;
1260 
1261 		if (tmplport->localport->node_name == opts->lpwwnn &&
1262 		    tmplport->localport->port_name == opts->lpwwpn)
1263 			lport = tmplport;
1264 	}
1265 
1266 	if (remoteport) {
1267 		if (!lport)
1268 			goto out_invalid_opts;
1269 		newnport->lport = lport;
1270 	}
1271 
1272 	list_for_each_entry(nport, &fcloop_nports, nport_list) {
1273 		if (nport->node_name == opts->wwnn &&
1274 		    nport->port_name == opts->wwpn) {
1275 			if ((remoteport && nport->rport) ||
1276 			    (!remoteport && nport->tport)) {
1277 				nport = NULL;
1278 				goto out_invalid_opts;
1279 			}
1280 
1281 			fcloop_nport_get(nport);
1282 
1283 			spin_unlock_irqrestore(&fcloop_lock, flags);
1284 
1285 			if (remoteport)
1286 				nport->lport = lport;
1287 			if (opts->mask & NVMF_OPT_ROLES)
1288 				nport->port_role = opts->roles;
1289 			if (opts->mask & NVMF_OPT_FCADDR)
1290 				nport->port_id = opts->fcaddr;
1291 			goto out_free_newnport;
1292 		}
1293 	}
1294 
1295 	list_add_tail(&newnport->nport_list, &fcloop_nports);
1296 
1297 	spin_unlock_irqrestore(&fcloop_lock, flags);
1298 
1299 	kfree(opts);
1300 	return newnport;
1301 
1302 out_invalid_opts:
1303 	spin_unlock_irqrestore(&fcloop_lock, flags);
1304 out_free_newnport:
1305 	kfree(newnport);
1306 out_free_opts:
1307 	kfree(opts);
1308 	return nport;
1309 }
1310 
1311 static ssize_t
1312 fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
1313 		const char *buf, size_t count)
1314 {
1315 	struct nvme_fc_remote_port *remoteport;
1316 	struct fcloop_nport *nport;
1317 	struct fcloop_rport *rport;
1318 	struct nvme_fc_port_info pinfo;
1319 	int ret;
1320 
1321 	nport = fcloop_alloc_nport(buf, count, true);
1322 	if (!nport)
1323 		return -EIO;
1324 
1325 	memset(&pinfo, 0, sizeof(pinfo));
1326 	pinfo.node_name = nport->node_name;
1327 	pinfo.port_name = nport->port_name;
1328 	pinfo.port_role = nport->port_role;
1329 	pinfo.port_id = nport->port_id;
1330 
1331 	ret = nvme_fc_register_remoteport(nport->lport->localport,
1332 						&pinfo, &remoteport);
1333 	if (ret || !remoteport) {
1334 		fcloop_nport_put(nport);
1335 		return ret;
1336 	}
1337 
1338 	/* success */
1339 	rport = remoteport->private;
1340 	rport->remoteport = remoteport;
1341 	rport->targetport = (nport->tport) ?  nport->tport->targetport : NULL;
1342 	if (nport->tport) {
1343 		nport->tport->remoteport = remoteport;
1344 		nport->tport->lport = nport->lport;
1345 	}
1346 	rport->nport = nport;
1347 	rport->lport = nport->lport;
1348 	nport->rport = rport;
1349 	spin_lock_init(&rport->lock);
1350 	INIT_WORK(&rport->ls_work, fcloop_rport_lsrqst_work);
1351 	INIT_LIST_HEAD(&rport->ls_list);
1352 
1353 	return count;
1354 }
1355 
1356 
1357 static struct fcloop_rport *
1358 __unlink_remote_port(struct fcloop_nport *nport)
1359 {
1360 	struct fcloop_rport *rport = nport->rport;
1361 
1362 	if (rport && nport->tport)
1363 		nport->tport->remoteport = NULL;
1364 	nport->rport = NULL;
1365 
1366 	list_del(&nport->nport_list);
1367 
1368 	return rport;
1369 }
1370 
1371 static int
1372 __remoteport_unreg(struct fcloop_nport *nport, struct fcloop_rport *rport)
1373 {
1374 	if (!rport)
1375 		return -EALREADY;
1376 
1377 	return nvme_fc_unregister_remoteport(rport->remoteport);
1378 }
1379 
1380 static ssize_t
1381 fcloop_delete_remote_port(struct device *dev, struct device_attribute *attr,
1382 		const char *buf, size_t count)
1383 {
1384 	struct fcloop_nport *nport = NULL, *tmpport;
1385 	static struct fcloop_rport *rport;
1386 	u64 nodename, portname;
1387 	unsigned long flags;
1388 	int ret;
1389 
1390 	ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
1391 	if (ret)
1392 		return ret;
1393 
1394 	spin_lock_irqsave(&fcloop_lock, flags);
1395 
1396 	list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
1397 		if (tmpport->node_name == nodename &&
1398 		    tmpport->port_name == portname && tmpport->rport) {
1399 			nport = tmpport;
1400 			rport = __unlink_remote_port(nport);
1401 			break;
1402 		}
1403 	}
1404 
1405 	spin_unlock_irqrestore(&fcloop_lock, flags);
1406 
1407 	if (!nport)
1408 		return -ENOENT;
1409 
1410 	ret = __remoteport_unreg(nport, rport);
1411 
1412 	return ret ? ret : count;
1413 }
1414 
1415 static ssize_t
1416 fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
1417 		const char *buf, size_t count)
1418 {
1419 	struct nvmet_fc_target_port *targetport;
1420 	struct fcloop_nport *nport;
1421 	struct fcloop_tport *tport;
1422 	struct nvmet_fc_port_info tinfo;
1423 	int ret;
1424 
1425 	nport = fcloop_alloc_nport(buf, count, false);
1426 	if (!nport)
1427 		return -EIO;
1428 
1429 	tinfo.node_name = nport->node_name;
1430 	tinfo.port_name = nport->port_name;
1431 	tinfo.port_id = nport->port_id;
1432 
1433 	ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL,
1434 						&targetport);
1435 	if (ret) {
1436 		fcloop_nport_put(nport);
1437 		return ret;
1438 	}
1439 
1440 	/* success */
1441 	tport = targetport->private;
1442 	tport->targetport = targetport;
1443 	tport->remoteport = (nport->rport) ?  nport->rport->remoteport : NULL;
1444 	if (nport->rport)
1445 		nport->rport->targetport = targetport;
1446 	tport->nport = nport;
1447 	tport->lport = nport->lport;
1448 	nport->tport = tport;
1449 	spin_lock_init(&tport->lock);
1450 	INIT_WORK(&tport->ls_work, fcloop_tport_lsrqst_work);
1451 	INIT_LIST_HEAD(&tport->ls_list);
1452 
1453 	return count;
1454 }
1455 
1456 
1457 static struct fcloop_tport *
1458 __unlink_target_port(struct fcloop_nport *nport)
1459 {
1460 	struct fcloop_tport *tport = nport->tport;
1461 
1462 	if (tport && nport->rport)
1463 		nport->rport->targetport = NULL;
1464 	nport->tport = NULL;
1465 
1466 	return tport;
1467 }
1468 
1469 static int
1470 __targetport_unreg(struct fcloop_nport *nport, struct fcloop_tport *tport)
1471 {
1472 	if (!tport)
1473 		return -EALREADY;
1474 
1475 	return nvmet_fc_unregister_targetport(tport->targetport);
1476 }
1477 
1478 static ssize_t
1479 fcloop_delete_target_port(struct device *dev, struct device_attribute *attr,
1480 		const char *buf, size_t count)
1481 {
1482 	struct fcloop_nport *nport = NULL, *tmpport;
1483 	struct fcloop_tport *tport = NULL;
1484 	u64 nodename, portname;
1485 	unsigned long flags;
1486 	int ret;
1487 
1488 	ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
1489 	if (ret)
1490 		return ret;
1491 
1492 	spin_lock_irqsave(&fcloop_lock, flags);
1493 
1494 	list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
1495 		if (tmpport->node_name == nodename &&
1496 		    tmpport->port_name == portname && tmpport->tport) {
1497 			nport = tmpport;
1498 			tport = __unlink_target_port(nport);
1499 			break;
1500 		}
1501 	}
1502 
1503 	spin_unlock_irqrestore(&fcloop_lock, flags);
1504 
1505 	if (!nport)
1506 		return -ENOENT;
1507 
1508 	ret = __targetport_unreg(nport, tport);
1509 
1510 	return ret ? ret : count;
1511 }
1512 
1513 static ssize_t
1514 fcloop_set_cmd_drop(struct device *dev, struct device_attribute *attr,
1515 		const char *buf, size_t count)
1516 {
1517 	unsigned int opcode;
1518 	int starting, amount;
1519 
1520 	if (sscanf(buf, "%x:%d:%d", &opcode, &starting, &amount) != 3)
1521 		return -EBADRQC;
1522 
1523 	drop_current_cnt = 0;
1524 	drop_fabric_opcode = (opcode & ~DROP_OPCODE_MASK) ? true : false;
1525 	drop_opcode = (opcode & DROP_OPCODE_MASK);
1526 	drop_instance = starting;
1527 	/* the check to drop routine uses instance + count to know when
1528 	 * to end. Thus, if dropping 1 instance, count should be 0.
1529 	 * so subtract 1 from the count.
1530 	 */
1531 	drop_amount = amount - 1;
1532 
1533 	pr_info("%s: DROP: Starting at instance %d of%s opcode x%x drop +%d "
1534 		"instances\n",
1535 		__func__, drop_instance, drop_fabric_opcode ? " fabric" : "",
1536 		drop_opcode, drop_amount);
1537 
1538 	return count;
1539 }
1540 
1541 
1542 static DEVICE_ATTR(add_local_port, 0200, NULL, fcloop_create_local_port);
1543 static DEVICE_ATTR(del_local_port, 0200, NULL, fcloop_delete_local_port);
1544 static DEVICE_ATTR(add_remote_port, 0200, NULL, fcloop_create_remote_port);
1545 static DEVICE_ATTR(del_remote_port, 0200, NULL, fcloop_delete_remote_port);
1546 static DEVICE_ATTR(add_target_port, 0200, NULL, fcloop_create_target_port);
1547 static DEVICE_ATTR(del_target_port, 0200, NULL, fcloop_delete_target_port);
1548 static DEVICE_ATTR(set_cmd_drop, 0200, NULL, fcloop_set_cmd_drop);
1549 
1550 static struct attribute *fcloop_dev_attrs[] = {
1551 	&dev_attr_add_local_port.attr,
1552 	&dev_attr_del_local_port.attr,
1553 	&dev_attr_add_remote_port.attr,
1554 	&dev_attr_del_remote_port.attr,
1555 	&dev_attr_add_target_port.attr,
1556 	&dev_attr_del_target_port.attr,
1557 	&dev_attr_set_cmd_drop.attr,
1558 	NULL
1559 };
1560 
1561 static const struct attribute_group fclopp_dev_attrs_group = {
1562 	.attrs		= fcloop_dev_attrs,
1563 };
1564 
1565 static const struct attribute_group *fcloop_dev_attr_groups[] = {
1566 	&fclopp_dev_attrs_group,
1567 	NULL,
1568 };
1569 
1570 static const struct class fcloop_class = {
1571 	.name = "fcloop",
1572 };
1573 static struct device *fcloop_device;
1574 
1575 
1576 static int __init fcloop_init(void)
1577 {
1578 	int ret;
1579 
1580 	ret = class_register(&fcloop_class);
1581 	if (ret) {
1582 		pr_err("couldn't register class fcloop\n");
1583 		return ret;
1584 	}
1585 
1586 	fcloop_device = device_create_with_groups(
1587 				&fcloop_class, NULL, MKDEV(0, 0), NULL,
1588 				fcloop_dev_attr_groups, "ctl");
1589 	if (IS_ERR(fcloop_device)) {
1590 		pr_err("couldn't create ctl device!\n");
1591 		ret = PTR_ERR(fcloop_device);
1592 		goto out_destroy_class;
1593 	}
1594 
1595 	get_device(fcloop_device);
1596 
1597 	return 0;
1598 
1599 out_destroy_class:
1600 	class_unregister(&fcloop_class);
1601 	return ret;
1602 }
1603 
1604 static void __exit fcloop_exit(void)
1605 {
1606 	struct fcloop_lport *lport = NULL;
1607 	struct fcloop_nport *nport = NULL;
1608 	struct fcloop_tport *tport;
1609 	struct fcloop_rport *rport;
1610 	unsigned long flags;
1611 	int ret;
1612 
1613 	spin_lock_irqsave(&fcloop_lock, flags);
1614 
1615 	for (;;) {
1616 		nport = list_first_entry_or_null(&fcloop_nports,
1617 						typeof(*nport), nport_list);
1618 		if (!nport)
1619 			break;
1620 
1621 		tport = __unlink_target_port(nport);
1622 		rport = __unlink_remote_port(nport);
1623 
1624 		spin_unlock_irqrestore(&fcloop_lock, flags);
1625 
1626 		ret = __targetport_unreg(nport, tport);
1627 		if (ret)
1628 			pr_warn("%s: Failed deleting target port\n", __func__);
1629 
1630 		ret = __remoteport_unreg(nport, rport);
1631 		if (ret)
1632 			pr_warn("%s: Failed deleting remote port\n", __func__);
1633 
1634 		spin_lock_irqsave(&fcloop_lock, flags);
1635 	}
1636 
1637 	for (;;) {
1638 		lport = list_first_entry_or_null(&fcloop_lports,
1639 						typeof(*lport), lport_list);
1640 		if (!lport)
1641 			break;
1642 
1643 		__unlink_local_port(lport);
1644 
1645 		spin_unlock_irqrestore(&fcloop_lock, flags);
1646 
1647 		ret = __wait_localport_unreg(lport);
1648 		if (ret)
1649 			pr_warn("%s: Failed deleting local port\n", __func__);
1650 
1651 		spin_lock_irqsave(&fcloop_lock, flags);
1652 	}
1653 
1654 	spin_unlock_irqrestore(&fcloop_lock, flags);
1655 
1656 	put_device(fcloop_device);
1657 
1658 	device_destroy(&fcloop_class, MKDEV(0, 0));
1659 	class_unregister(&fcloop_class);
1660 }
1661 
1662 module_init(fcloop_init);
1663 module_exit(fcloop_exit);
1664 
1665 MODULE_DESCRIPTION("NVMe target FC loop transport driver");
1666 MODULE_LICENSE("GPL v2");
1667