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