]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/nvme/target/fcloop.c
Merge remote-tracking branch 'regulator/fix/max77802' into regulator-linus
[mirror_ubuntu-artful-kernel.git] / drivers / nvme / target / fcloop.c
CommitLineData
475d0fe7
JS
1/*
2 * Copyright (c) 2016 Avago Technologies. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful.
9 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
10 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
11 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO
12 * THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
13 * See the GNU General Public License for more details, a copy of which
14 * can be found in the file COPYING included with this package
15 */
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17#include <linux/module.h>
18#include <linux/parser.h>
19#include <uapi/scsi/fc/fc_fs.h>
20
21#include "../host/nvme.h"
22#include "../target/nvmet.h"
23#include <linux/nvme-fc-driver.h>
24#include <linux/nvme-fc.h>
25
26
27enum {
28 NVMF_OPT_ERR = 0,
29 NVMF_OPT_WWNN = 1 << 0,
30 NVMF_OPT_WWPN = 1 << 1,
31 NVMF_OPT_ROLES = 1 << 2,
32 NVMF_OPT_FCADDR = 1 << 3,
33 NVMF_OPT_LPWWNN = 1 << 4,
34 NVMF_OPT_LPWWPN = 1 << 5,
35};
36
37struct fcloop_ctrl_options {
38 int mask;
39 u64 wwnn;
40 u64 wwpn;
41 u32 roles;
42 u32 fcaddr;
43 u64 lpwwnn;
44 u64 lpwwpn;
45};
46
47static const match_table_t opt_tokens = {
48 { NVMF_OPT_WWNN, "wwnn=%s" },
49 { NVMF_OPT_WWPN, "wwpn=%s" },
50 { NVMF_OPT_ROLES, "roles=%d" },
51 { NVMF_OPT_FCADDR, "fcaddr=%x" },
52 { NVMF_OPT_LPWWNN, "lpwwnn=%s" },
53 { NVMF_OPT_LPWWPN, "lpwwpn=%s" },
54 { NVMF_OPT_ERR, NULL }
55};
56
57static int
58fcloop_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 (match_u64(args, &token64)) {
79 ret = -EINVAL;
80 goto out_free_options;
81 }
82 opts->wwnn = token64;
83 break;
84 case NVMF_OPT_WWPN:
85 if (match_u64(args, &token64)) {
86 ret = -EINVAL;
87 goto out_free_options;
88 }
89 opts->wwpn = token64;
90 break;
91 case NVMF_OPT_ROLES:
92 if (match_int(args, &token)) {
93 ret = -EINVAL;
94 goto out_free_options;
95 }
96 opts->roles = token;
97 break;
98 case NVMF_OPT_FCADDR:
99 if (match_hex(args, &token)) {
100 ret = -EINVAL;
101 goto out_free_options;
102 }
103 opts->fcaddr = token;
104 break;
105 case NVMF_OPT_LPWWNN:
106 if (match_u64(args, &token64)) {
107 ret = -EINVAL;
108 goto out_free_options;
109 }
110 opts->lpwwnn = token64;
111 break;
112 case NVMF_OPT_LPWWPN:
113 if (match_u64(args, &token64)) {
114 ret = -EINVAL;
115 goto out_free_options;
116 }
117 opts->lpwwpn = token64;
118 break;
119 default:
120 pr_warn("unknown parameter or missing value '%s'\n", p);
121 ret = -EINVAL;
122 goto out_free_options;
123 }
124 }
125
126out_free_options:
127 kfree(options);
128 return ret;
129}
130
131
132static int
133fcloop_parse_nm_options(struct device *dev, u64 *nname, u64 *pname,
134 const char *buf)
135{
136 substring_t args[MAX_OPT_ARGS];
137 char *options, *o, *p;
138 int token, ret = 0;
139 u64 token64;
140
141 *nname = -1;
142 *pname = -1;
143
144 options = o = kstrdup(buf, GFP_KERNEL);
145 if (!options)
146 return -ENOMEM;
147
148 while ((p = strsep(&o, ",\n")) != NULL) {
149 if (!*p)
150 continue;
151
152 token = match_token(p, opt_tokens, args);
153 switch (token) {
154 case NVMF_OPT_WWNN:
155 if (match_u64(args, &token64)) {
156 ret = -EINVAL;
157 goto out_free_options;
158 }
159 *nname = token64;
160 break;
161 case NVMF_OPT_WWPN:
162 if (match_u64(args, &token64)) {
163 ret = -EINVAL;
164 goto out_free_options;
165 }
166 *pname = token64;
167 break;
168 default:
169 pr_warn("unknown parameter or missing value '%s'\n", p);
170 ret = -EINVAL;
171 goto out_free_options;
172 }
173 }
174
175out_free_options:
176 kfree(options);
177
178 if (!ret) {
179 if (*nname == -1)
180 return -EINVAL;
181 if (*pname == -1)
182 return -EINVAL;
183 }
184
185 return ret;
186}
187
188
189#define LPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN)
190
191#define RPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN | \
192 NVMF_OPT_LPWWNN | NVMF_OPT_LPWWPN)
193
194#define TGTPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN)
195
196#define ALL_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN | NVMF_OPT_ROLES | \
197 NVMF_OPT_FCADDR | NVMF_OPT_LPWWNN | NVMF_OPT_LPWWPN)
198
199
200static DEFINE_SPINLOCK(fcloop_lock);
201static LIST_HEAD(fcloop_lports);
202static LIST_HEAD(fcloop_nports);
203
204struct fcloop_lport {
205 struct nvme_fc_local_port *localport;
206 struct list_head lport_list;
207 struct completion unreg_done;
208};
209
210struct fcloop_rport {
211 struct nvme_fc_remote_port *remoteport;
212 struct nvmet_fc_target_port *targetport;
213 struct fcloop_nport *nport;
214 struct fcloop_lport *lport;
215};
216
217struct fcloop_tport {
218 struct nvmet_fc_target_port *targetport;
219 struct nvme_fc_remote_port *remoteport;
220 struct fcloop_nport *nport;
221 struct fcloop_lport *lport;
222};
223
224struct fcloop_nport {
225 struct fcloop_rport *rport;
226 struct fcloop_tport *tport;
227 struct fcloop_lport *lport;
228 struct list_head nport_list;
229 struct kref ref;
230 struct completion rport_unreg_done;
231 struct completion tport_unreg_done;
232 u64 node_name;
233 u64 port_name;
234 u32 port_role;
235 u32 port_id;
236};
237
238struct fcloop_lsreq {
239 struct fcloop_tport *tport;
240 struct nvmefc_ls_req *lsreq;
241 struct work_struct work;
242 struct nvmefc_tgt_ls_req tgt_ls_req;
243 int status;
244};
245
246struct fcloop_fcpreq {
247 struct fcloop_tport *tport;
248 struct nvmefc_fcp_req *fcpreq;
a97ec51b 249 spinlock_t reqlock;
475d0fe7 250 u16 status;
a97ec51b
JS
251 bool active;
252 bool aborted;
475d0fe7
JS
253 struct work_struct work;
254 struct nvmefc_tgt_fcp_req tgt_fcp_req;
255};
256
ce79bfc2
JS
257struct fcloop_ini_fcpreq {
258 struct nvmefc_fcp_req *fcpreq;
259 struct fcloop_fcpreq *tfcp_req;
a97ec51b 260 struct work_struct iniwork;
ce79bfc2 261};
475d0fe7
JS
262
263static inline struct fcloop_lsreq *
264tgt_ls_req_to_lsreq(struct nvmefc_tgt_ls_req *tgt_lsreq)
265{
266 return container_of(tgt_lsreq, struct fcloop_lsreq, tgt_ls_req);
267}
268
269static inline struct fcloop_fcpreq *
270tgt_fcp_req_to_fcpreq(struct nvmefc_tgt_fcp_req *tgt_fcpreq)
271{
272 return container_of(tgt_fcpreq, struct fcloop_fcpreq, tgt_fcp_req);
273}
274
275
276static int
277fcloop_create_queue(struct nvme_fc_local_port *localport,
278 unsigned int qidx, u16 qsize,
279 void **handle)
280{
281 *handle = localport;
282 return 0;
283}
284
285static void
286fcloop_delete_queue(struct nvme_fc_local_port *localport,
287 unsigned int idx, void *handle)
288{
289}
290
291
292/*
293 * Transmit of LS RSP done (e.g. buffers all set). call back up
294 * initiator "done" flows.
295 */
296static void
297fcloop_tgt_lsrqst_done_work(struct work_struct *work)
298{
299 struct fcloop_lsreq *tls_req =
300 container_of(work, struct fcloop_lsreq, work);
301 struct fcloop_tport *tport = tls_req->tport;
302 struct nvmefc_ls_req *lsreq = tls_req->lsreq;
303
304 if (tport->remoteport)
305 lsreq->done(lsreq, tls_req->status);
306}
307
308static int
309fcloop_ls_req(struct nvme_fc_local_port *localport,
310 struct nvme_fc_remote_port *remoteport,
311 struct nvmefc_ls_req *lsreq)
312{
313 struct fcloop_lsreq *tls_req = lsreq->private;
314 struct fcloop_rport *rport = remoteport->private;
315 int ret = 0;
316
317 tls_req->lsreq = lsreq;
318 INIT_WORK(&tls_req->work, fcloop_tgt_lsrqst_done_work);
319
320 if (!rport->targetport) {
321 tls_req->status = -ECONNREFUSED;
322 schedule_work(&tls_req->work);
323 return ret;
324 }
325
326 tls_req->status = 0;
327 tls_req->tport = rport->targetport->private;
328 ret = nvmet_fc_rcv_ls_req(rport->targetport, &tls_req->tgt_ls_req,
329 lsreq->rqstaddr, lsreq->rqstlen);
330
331 return ret;
332}
333
334static int
335fcloop_xmt_ls_rsp(struct nvmet_fc_target_port *tport,
336 struct nvmefc_tgt_ls_req *tgt_lsreq)
337{
338 struct fcloop_lsreq *tls_req = tgt_ls_req_to_lsreq(tgt_lsreq);
339 struct nvmefc_ls_req *lsreq = tls_req->lsreq;
340
341 memcpy(lsreq->rspaddr, tgt_lsreq->rspbuf,
342 ((lsreq->rsplen < tgt_lsreq->rsplen) ?
343 lsreq->rsplen : tgt_lsreq->rsplen));
344 tgt_lsreq->done(tgt_lsreq);
345
346 schedule_work(&tls_req->work);
347
348 return 0;
349}
350
351/*
a97ec51b
JS
352 * FCP IO operation done by initiator abort.
353 * call back up initiator "done" flows.
354 */
355static void
356fcloop_tgt_fcprqst_ini_done_work(struct work_struct *work)
357{
358 struct fcloop_ini_fcpreq *inireq =
359 container_of(work, struct fcloop_ini_fcpreq, iniwork);
360
361 inireq->fcpreq->done(inireq->fcpreq);
362}
363
364/*
365 * FCP IO operation done by target completion.
366 * call back up initiator "done" flows.
475d0fe7
JS
367 */
368static void
369fcloop_tgt_fcprqst_done_work(struct work_struct *work)
370{
371 struct fcloop_fcpreq *tfcp_req =
372 container_of(work, struct fcloop_fcpreq, work);
373 struct fcloop_tport *tport = tfcp_req->tport;
a97ec51b 374 struct nvmefc_fcp_req *fcpreq;
475d0fe7 375
a97ec51b
JS
376 spin_lock(&tfcp_req->reqlock);
377 fcpreq = tfcp_req->fcpreq;
378 spin_unlock(&tfcp_req->reqlock);
379
380 if (tport->remoteport && fcpreq) {
475d0fe7
JS
381 fcpreq->status = tfcp_req->status;
382 fcpreq->done(fcpreq);
383 }
ce79bfc2
JS
384
385 kfree(tfcp_req);
475d0fe7
JS
386}
387
388
389static int
390fcloop_fcp_req(struct nvme_fc_local_port *localport,
391 struct nvme_fc_remote_port *remoteport,
392 void *hw_queue_handle,
393 struct nvmefc_fcp_req *fcpreq)
394{
475d0fe7 395 struct fcloop_rport *rport = remoteport->private;
ce79bfc2
JS
396 struct fcloop_ini_fcpreq *inireq = fcpreq->private;
397 struct fcloop_fcpreq *tfcp_req;
475d0fe7
JS
398 int ret = 0;
399
ce79bfc2
JS
400 if (!rport->targetport)
401 return -ECONNREFUSED;
475d0fe7 402
ce79bfc2
JS
403 tfcp_req = kzalloc(sizeof(*tfcp_req), GFP_KERNEL);
404 if (!tfcp_req)
405 return -ENOMEM;
475d0fe7 406
ce79bfc2
JS
407 inireq->fcpreq = fcpreq;
408 inireq->tfcp_req = tfcp_req;
a97ec51b 409 INIT_WORK(&inireq->iniwork, fcloop_tgt_fcprqst_ini_done_work);
475d0fe7
JS
410 tfcp_req->fcpreq = fcpreq;
411 tfcp_req->tport = rport->targetport->private;
a97ec51b 412 spin_lock_init(&tfcp_req->reqlock);
ce79bfc2 413 INIT_WORK(&tfcp_req->work, fcloop_tgt_fcprqst_done_work);
475d0fe7
JS
414
415 ret = nvmet_fc_rcv_fcp_req(rport->targetport, &tfcp_req->tgt_fcp_req,
416 fcpreq->cmdaddr, fcpreq->cmdlen);
417
418 return ret;
419}
420
421static void
422fcloop_fcp_copy_data(u8 op, struct scatterlist *data_sg,
423 struct scatterlist *io_sg, u32 offset, u32 length)
424{
425 void *data_p, *io_p;
426 u32 data_len, io_len, tlen;
427
428 io_p = sg_virt(io_sg);
429 io_len = io_sg->length;
430
431 for ( ; offset; ) {
432 tlen = min_t(u32, offset, io_len);
433 offset -= tlen;
434 io_len -= tlen;
435 if (!io_len) {
436 io_sg = sg_next(io_sg);
437 io_p = sg_virt(io_sg);
438 io_len = io_sg->length;
439 } else
440 io_p += tlen;
441 }
442
443 data_p = sg_virt(data_sg);
444 data_len = data_sg->length;
445
446 for ( ; length; ) {
447 tlen = min_t(u32, io_len, data_len);
448 tlen = min_t(u32, tlen, length);
449
450 if (op == NVMET_FCOP_WRITEDATA)
451 memcpy(data_p, io_p, tlen);
452 else
453 memcpy(io_p, data_p, tlen);
454
455 length -= tlen;
456
457 io_len -= tlen;
458 if ((!io_len) && (length)) {
459 io_sg = sg_next(io_sg);
460 io_p = sg_virt(io_sg);
461 io_len = io_sg->length;
462 } else
463 io_p += tlen;
464
465 data_len -= tlen;
466 if ((!data_len) && (length)) {
467 data_sg = sg_next(data_sg);
468 data_p = sg_virt(data_sg);
469 data_len = data_sg->length;
470 } else
471 data_p += tlen;
472 }
473}
474
475static int
476fcloop_fcp_op(struct nvmet_fc_target_port *tgtport,
477 struct nvmefc_tgt_fcp_req *tgt_fcpreq)
478{
479 struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
a97ec51b 480 struct nvmefc_fcp_req *fcpreq;
475d0fe7 481 u32 rsplen = 0, xfrlen = 0;
a97ec51b 482 int fcp_err = 0, active, aborted;
475d0fe7
JS
483 u8 op = tgt_fcpreq->op;
484
a97ec51b
JS
485 spin_lock(&tfcp_req->reqlock);
486 fcpreq = tfcp_req->fcpreq;
487 active = tfcp_req->active;
488 aborted = tfcp_req->aborted;
489 tfcp_req->active = true;
490 spin_unlock(&tfcp_req->reqlock);
491
492 if (unlikely(active))
493 /* illegal - call while i/o active */
494 return -EALREADY;
495
496 if (unlikely(aborted)) {
497 /* target transport has aborted i/o prior */
498 spin_lock(&tfcp_req->reqlock);
499 tfcp_req->active = false;
500 spin_unlock(&tfcp_req->reqlock);
501 tgt_fcpreq->transferred_length = 0;
502 tgt_fcpreq->fcp_error = -ECANCELED;
503 tgt_fcpreq->done(tgt_fcpreq);
504 return 0;
505 }
506
507 /*
508 * if fcpreq is NULL, the I/O has been aborted (from
509 * initiator side). For the target side, act as if all is well
510 * but don't actually move data.
511 */
512
475d0fe7
JS
513 switch (op) {
514 case NVMET_FCOP_WRITEDATA:
515 xfrlen = tgt_fcpreq->transfer_length;
a97ec51b
JS
516 if (fcpreq) {
517 fcloop_fcp_copy_data(op, tgt_fcpreq->sg,
518 fcpreq->first_sgl, tgt_fcpreq->offset,
519 xfrlen);
520 fcpreq->transferred_length += xfrlen;
521 }
475d0fe7
JS
522 break;
523
524 case NVMET_FCOP_READDATA:
525 case NVMET_FCOP_READDATA_RSP:
526 xfrlen = tgt_fcpreq->transfer_length;
a97ec51b
JS
527 if (fcpreq) {
528 fcloop_fcp_copy_data(op, tgt_fcpreq->sg,
529 fcpreq->first_sgl, tgt_fcpreq->offset,
530 xfrlen);
531 fcpreq->transferred_length += xfrlen;
532 }
475d0fe7
JS
533 if (op == NVMET_FCOP_READDATA)
534 break;
535
536 /* Fall-Thru to RSP handling */
537
538 case NVMET_FCOP_RSP:
a97ec51b
JS
539 if (fcpreq) {
540 rsplen = ((fcpreq->rsplen < tgt_fcpreq->rsplen) ?
541 fcpreq->rsplen : tgt_fcpreq->rsplen);
542 memcpy(fcpreq->rspaddr, tgt_fcpreq->rspaddr, rsplen);
543 if (rsplen < tgt_fcpreq->rsplen)
544 fcp_err = -E2BIG;
545 fcpreq->rcv_rsplen = rsplen;
546 fcpreq->status = 0;
547 }
475d0fe7
JS
548 tfcp_req->status = 0;
549 break;
550
475d0fe7
JS
551 default:
552 fcp_err = -EINVAL;
553 break;
554 }
555
a97ec51b
JS
556 spin_lock(&tfcp_req->reqlock);
557 tfcp_req->active = false;
558 spin_unlock(&tfcp_req->reqlock);
559
475d0fe7
JS
560 tgt_fcpreq->transferred_length = xfrlen;
561 tgt_fcpreq->fcp_error = fcp_err;
562 tgt_fcpreq->done(tgt_fcpreq);
563
475d0fe7
JS
564 return 0;
565}
566
a97ec51b
JS
567static void
568fcloop_tgt_fcp_abort(struct nvmet_fc_target_port *tgtport,
569 struct nvmefc_tgt_fcp_req *tgt_fcpreq)
570{
571 struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
572 int active;
573
574 /*
575 * mark aborted only in case there were 2 threads in transport
576 * (one doing io, other doing abort) and only kills ops posted
577 * after the abort request
578 */
579 spin_lock(&tfcp_req->reqlock);
580 active = tfcp_req->active;
581 tfcp_req->aborted = true;
582 spin_unlock(&tfcp_req->reqlock);
583
584 tfcp_req->status = NVME_SC_FC_TRANSPORT_ABORTED;
585
586 /*
587 * nothing more to do. If io wasn't active, the transport should
588 * immediately call the req_release. If it was active, the op
589 * will complete, and the lldd should call req_release.
590 */
591}
592
19b58d94
JS
593static void
594fcloop_fcp_req_release(struct nvmet_fc_target_port *tgtport,
595 struct nvmefc_tgt_fcp_req *tgt_fcpreq)
596{
597 struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
598
599 schedule_work(&tfcp_req->work);
600}
601
475d0fe7
JS
602static void
603fcloop_ls_abort(struct nvme_fc_local_port *localport,
604 struct nvme_fc_remote_port *remoteport,
605 struct nvmefc_ls_req *lsreq)
606{
607}
608
609static void
610fcloop_fcp_abort(struct nvme_fc_local_port *localport,
611 struct nvme_fc_remote_port *remoteport,
612 void *hw_queue_handle,
613 struct nvmefc_fcp_req *fcpreq)
614{
a97ec51b
JS
615 struct fcloop_rport *rport = remoteport->private;
616 struct fcloop_ini_fcpreq *inireq = fcpreq->private;
617 struct fcloop_fcpreq *tfcp_req = inireq->tfcp_req;
618
619 if (!tfcp_req)
620 /* abort has already been called */
621 return;
622
623 if (rport->targetport)
624 nvmet_fc_rcv_fcp_abort(rport->targetport,
625 &tfcp_req->tgt_fcp_req);
626
627 /* break initiator/target relationship for io */
628 spin_lock(&tfcp_req->reqlock);
629 inireq->tfcp_req = NULL;
630 tfcp_req->fcpreq = NULL;
631 spin_unlock(&tfcp_req->reqlock);
632
633 /* post the aborted io completion */
634 fcpreq->status = -ECANCELED;
635 schedule_work(&inireq->iniwork);
475d0fe7
JS
636}
637
638static void
639fcloop_localport_delete(struct nvme_fc_local_port *localport)
640{
641 struct fcloop_lport *lport = localport->private;
642
643 /* release any threads waiting for the unreg to complete */
644 complete(&lport->unreg_done);
645}
646
647static void
648fcloop_remoteport_delete(struct nvme_fc_remote_port *remoteport)
649{
650 struct fcloop_rport *rport = remoteport->private;
651
652 /* release any threads waiting for the unreg to complete */
653 complete(&rport->nport->rport_unreg_done);
654}
655
656static void
657fcloop_targetport_delete(struct nvmet_fc_target_port *targetport)
658{
659 struct fcloop_tport *tport = targetport->private;
660
661 /* release any threads waiting for the unreg to complete */
662 complete(&tport->nport->tport_unreg_done);
663}
664
665#define FCLOOP_HW_QUEUES 4
666#define FCLOOP_SGL_SEGS 256
667#define FCLOOP_DMABOUND_4G 0xFFFFFFFF
668
36b8890e 669static struct nvme_fc_port_template fctemplate = {
475d0fe7
JS
670 .localport_delete = fcloop_localport_delete,
671 .remoteport_delete = fcloop_remoteport_delete,
672 .create_queue = fcloop_create_queue,
673 .delete_queue = fcloop_delete_queue,
674 .ls_req = fcloop_ls_req,
675 .fcp_io = fcloop_fcp_req,
676 .ls_abort = fcloop_ls_abort,
677 .fcp_abort = fcloop_fcp_abort,
678 .max_hw_queues = FCLOOP_HW_QUEUES,
679 .max_sgl_segments = FCLOOP_SGL_SEGS,
680 .max_dif_sgl_segments = FCLOOP_SGL_SEGS,
681 .dma_boundary = FCLOOP_DMABOUND_4G,
682 /* sizes of additional private data for data structures */
683 .local_priv_sz = sizeof(struct fcloop_lport),
684 .remote_priv_sz = sizeof(struct fcloop_rport),
685 .lsrqst_priv_sz = sizeof(struct fcloop_lsreq),
ce79bfc2 686 .fcprqst_priv_sz = sizeof(struct fcloop_ini_fcpreq),
475d0fe7
JS
687};
688
36b8890e 689static struct nvmet_fc_target_template tgttemplate = {
475d0fe7
JS
690 .targetport_delete = fcloop_targetport_delete,
691 .xmt_ls_rsp = fcloop_xmt_ls_rsp,
692 .fcp_op = fcloop_fcp_op,
a97ec51b 693 .fcp_abort = fcloop_tgt_fcp_abort,
19b58d94 694 .fcp_req_release = fcloop_fcp_req_release,
475d0fe7
JS
695 .max_hw_queues = FCLOOP_HW_QUEUES,
696 .max_sgl_segments = FCLOOP_SGL_SEGS,
697 .max_dif_sgl_segments = FCLOOP_SGL_SEGS,
698 .dma_boundary = FCLOOP_DMABOUND_4G,
699 /* optional features */
39498fae 700 .target_features = NVMET_FCTGTFEAT_CMD_IN_ISR |
39498fae 701 NVMET_FCTGTFEAT_OPDONE_IN_ISR,
475d0fe7
JS
702 /* sizes of additional private data for data structures */
703 .target_priv_sz = sizeof(struct fcloop_tport),
704};
705
706static ssize_t
707fcloop_create_local_port(struct device *dev, struct device_attribute *attr,
708 const char *buf, size_t count)
709{
710 struct nvme_fc_port_info pinfo;
711 struct fcloop_ctrl_options *opts;
712 struct nvme_fc_local_port *localport;
713 struct fcloop_lport *lport;
714 int ret;
715
716 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
717 if (!opts)
718 return -ENOMEM;
719
720 ret = fcloop_parse_options(opts, buf);
721 if (ret)
722 goto out_free_opts;
723
724 /* everything there ? */
725 if ((opts->mask & LPORT_OPTS) != LPORT_OPTS) {
726 ret = -EINVAL;
727 goto out_free_opts;
728 }
729
730 pinfo.node_name = opts->wwnn;
731 pinfo.port_name = opts->wwpn;
732 pinfo.port_role = opts->roles;
733 pinfo.port_id = opts->fcaddr;
734
735 ret = nvme_fc_register_localport(&pinfo, &fctemplate, NULL, &localport);
736 if (!ret) {
737 unsigned long flags;
738
739 /* success */
740 lport = localport->private;
741 lport->localport = localport;
742 INIT_LIST_HEAD(&lport->lport_list);
743
744 spin_lock_irqsave(&fcloop_lock, flags);
745 list_add_tail(&lport->lport_list, &fcloop_lports);
746 spin_unlock_irqrestore(&fcloop_lock, flags);
747
748 /* mark all of the input buffer consumed */
749 ret = count;
750 }
751
752out_free_opts:
753 kfree(opts);
754 return ret ? ret : count;
755}
756
757
758static void
759__unlink_local_port(struct fcloop_lport *lport)
760{
761 list_del(&lport->lport_list);
762}
763
764static int
765__wait_localport_unreg(struct fcloop_lport *lport)
766{
767 int ret;
768
769 init_completion(&lport->unreg_done);
770
771 ret = nvme_fc_unregister_localport(lport->localport);
772
773 wait_for_completion(&lport->unreg_done);
774
775 return ret;
776}
777
778
779static ssize_t
780fcloop_delete_local_port(struct device *dev, struct device_attribute *attr,
781 const char *buf, size_t count)
782{
783 struct fcloop_lport *tlport, *lport = NULL;
784 u64 nodename, portname;
785 unsigned long flags;
786 int ret;
787
788 ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
789 if (ret)
790 return ret;
791
792 spin_lock_irqsave(&fcloop_lock, flags);
793
794 list_for_each_entry(tlport, &fcloop_lports, lport_list) {
795 if (tlport->localport->node_name == nodename &&
796 tlport->localport->port_name == portname) {
797 lport = tlport;
798 __unlink_local_port(lport);
799 break;
800 }
801 }
802 spin_unlock_irqrestore(&fcloop_lock, flags);
803
804 if (!lport)
805 return -ENOENT;
806
807 ret = __wait_localport_unreg(lport);
808
809 return ret ? ret : count;
810}
811
812static void
813fcloop_nport_free(struct kref *ref)
814{
815 struct fcloop_nport *nport =
816 container_of(ref, struct fcloop_nport, ref);
817 unsigned long flags;
818
819 spin_lock_irqsave(&fcloop_lock, flags);
820 list_del(&nport->nport_list);
821 spin_unlock_irqrestore(&fcloop_lock, flags);
822
823 kfree(nport);
824}
825
826static void
827fcloop_nport_put(struct fcloop_nport *nport)
828{
829 kref_put(&nport->ref, fcloop_nport_free);
830}
831
832static int
833fcloop_nport_get(struct fcloop_nport *nport)
834{
835 return kref_get_unless_zero(&nport->ref);
836}
837
838static struct fcloop_nport *
839fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
840{
841 struct fcloop_nport *newnport, *nport = NULL;
842 struct fcloop_lport *tmplport, *lport = NULL;
843 struct fcloop_ctrl_options *opts;
844 unsigned long flags;
845 u32 opts_mask = (remoteport) ? RPORT_OPTS : TGTPORT_OPTS;
846 int ret;
847
848 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
849 if (!opts)
850 return NULL;
851
852 ret = fcloop_parse_options(opts, buf);
853 if (ret)
854 goto out_free_opts;
855
856 /* everything there ? */
857 if ((opts->mask & opts_mask) != opts_mask) {
858 ret = -EINVAL;
859 goto out_free_opts;
860 }
861
862 newnport = kzalloc(sizeof(*newnport), GFP_KERNEL);
863 if (!newnport)
864 goto out_free_opts;
865
866 INIT_LIST_HEAD(&newnport->nport_list);
867 newnport->node_name = opts->wwnn;
868 newnport->port_name = opts->wwpn;
869 if (opts->mask & NVMF_OPT_ROLES)
870 newnport->port_role = opts->roles;
871 if (opts->mask & NVMF_OPT_FCADDR)
872 newnport->port_id = opts->fcaddr;
873 kref_init(&newnport->ref);
874
875 spin_lock_irqsave(&fcloop_lock, flags);
876
877 list_for_each_entry(tmplport, &fcloop_lports, lport_list) {
878 if (tmplport->localport->node_name == opts->wwnn &&
879 tmplport->localport->port_name == opts->wwpn)
880 goto out_invalid_opts;
881
882 if (tmplport->localport->node_name == opts->lpwwnn &&
883 tmplport->localport->port_name == opts->lpwwpn)
884 lport = tmplport;
885 }
886
887 if (remoteport) {
888 if (!lport)
889 goto out_invalid_opts;
890 newnport->lport = lport;
891 }
892
893 list_for_each_entry(nport, &fcloop_nports, nport_list) {
894 if (nport->node_name == opts->wwnn &&
895 nport->port_name == opts->wwpn) {
896 if ((remoteport && nport->rport) ||
897 (!remoteport && nport->tport)) {
898 nport = NULL;
899 goto out_invalid_opts;
900 }
901
902 fcloop_nport_get(nport);
903
904 spin_unlock_irqrestore(&fcloop_lock, flags);
905
906 if (remoteport)
907 nport->lport = lport;
908 if (opts->mask & NVMF_OPT_ROLES)
909 nport->port_role = opts->roles;
910 if (opts->mask & NVMF_OPT_FCADDR)
911 nport->port_id = opts->fcaddr;
912 goto out_free_newnport;
913 }
914 }
915
916 list_add_tail(&newnport->nport_list, &fcloop_nports);
917
918 spin_unlock_irqrestore(&fcloop_lock, flags);
919
920 kfree(opts);
921 return newnport;
922
923out_invalid_opts:
924 spin_unlock_irqrestore(&fcloop_lock, flags);
925out_free_newnport:
926 kfree(newnport);
927out_free_opts:
928 kfree(opts);
929 return nport;
930}
931
932static ssize_t
933fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
934 const char *buf, size_t count)
935{
936 struct nvme_fc_remote_port *remoteport;
937 struct fcloop_nport *nport;
938 struct fcloop_rport *rport;
939 struct nvme_fc_port_info pinfo;
940 int ret;
941
942 nport = fcloop_alloc_nport(buf, count, true);
943 if (!nport)
944 return -EIO;
945
946 pinfo.node_name = nport->node_name;
947 pinfo.port_name = nport->port_name;
948 pinfo.port_role = nport->port_role;
949 pinfo.port_id = nport->port_id;
950
951 ret = nvme_fc_register_remoteport(nport->lport->localport,
952 &pinfo, &remoteport);
953 if (ret || !remoteport) {
954 fcloop_nport_put(nport);
955 return ret;
956 }
957
958 /* success */
959 rport = remoteport->private;
960 rport->remoteport = remoteport;
961 rport->targetport = (nport->tport) ? nport->tport->targetport : NULL;
962 if (nport->tport) {
963 nport->tport->remoteport = remoteport;
964 nport->tport->lport = nport->lport;
965 }
966 rport->nport = nport;
967 rport->lport = nport->lport;
968 nport->rport = rport;
969
7c3a23b8 970 return count;
475d0fe7
JS
971}
972
973
974static struct fcloop_rport *
975__unlink_remote_port(struct fcloop_nport *nport)
976{
977 struct fcloop_rport *rport = nport->rport;
978
979 if (rport && nport->tport)
980 nport->tport->remoteport = NULL;
981 nport->rport = NULL;
982
983 return rport;
984}
985
986static int
987__wait_remoteport_unreg(struct fcloop_nport *nport, struct fcloop_rport *rport)
988{
989 int ret;
990
991 if (!rport)
992 return -EALREADY;
993
994 init_completion(&nport->rport_unreg_done);
995
996 ret = nvme_fc_unregister_remoteport(rport->remoteport);
997 if (ret)
998 return ret;
999
1000 wait_for_completion(&nport->rport_unreg_done);
1001
1002 fcloop_nport_put(nport);
1003
1004 return ret;
1005}
1006
1007static ssize_t
1008fcloop_delete_remote_port(struct device *dev, struct device_attribute *attr,
1009 const char *buf, size_t count)
1010{
1011 struct fcloop_nport *nport = NULL, *tmpport;
1012 static struct fcloop_rport *rport;
1013 u64 nodename, portname;
1014 unsigned long flags;
1015 int ret;
1016
1017 ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
1018 if (ret)
1019 return ret;
1020
1021 spin_lock_irqsave(&fcloop_lock, flags);
1022
1023 list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
1024 if (tmpport->node_name == nodename &&
1025 tmpport->port_name == portname && tmpport->rport) {
1026 nport = tmpport;
1027 rport = __unlink_remote_port(nport);
1028 break;
1029 }
1030 }
1031
1032 spin_unlock_irqrestore(&fcloop_lock, flags);
1033
1034 if (!nport)
1035 return -ENOENT;
1036
1037 ret = __wait_remoteport_unreg(nport, rport);
1038
1039 return ret ? ret : count;
1040}
1041
1042static ssize_t
1043fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
1044 const char *buf, size_t count)
1045{
1046 struct nvmet_fc_target_port *targetport;
1047 struct fcloop_nport *nport;
1048 struct fcloop_tport *tport;
1049 struct nvmet_fc_port_info tinfo;
1050 int ret;
1051
1052 nport = fcloop_alloc_nport(buf, count, false);
1053 if (!nport)
1054 return -EIO;
1055
1056 tinfo.node_name = nport->node_name;
1057 tinfo.port_name = nport->port_name;
1058 tinfo.port_id = nport->port_id;
1059
1060 ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL,
1061 &targetport);
1062 if (ret) {
1063 fcloop_nport_put(nport);
1064 return ret;
1065 }
1066
1067 /* success */
1068 tport = targetport->private;
1069 tport->targetport = targetport;
1070 tport->remoteport = (nport->rport) ? nport->rport->remoteport : NULL;
1071 if (nport->rport)
1072 nport->rport->targetport = targetport;
1073 tport->nport = nport;
1074 tport->lport = nport->lport;
1075 nport->tport = tport;
1076
7c3a23b8 1077 return count;
475d0fe7
JS
1078}
1079
1080
1081static struct fcloop_tport *
1082__unlink_target_port(struct fcloop_nport *nport)
1083{
1084 struct fcloop_tport *tport = nport->tport;
1085
1086 if (tport && nport->rport)
1087 nport->rport->targetport = NULL;
1088 nport->tport = NULL;
1089
1090 return tport;
1091}
1092
1093static int
1094__wait_targetport_unreg(struct fcloop_nport *nport, struct fcloop_tport *tport)
1095{
1096 int ret;
1097
1098 if (!tport)
1099 return -EALREADY;
1100
1101 init_completion(&nport->tport_unreg_done);
1102
1103 ret = nvmet_fc_unregister_targetport(tport->targetport);
1104 if (ret)
1105 return ret;
1106
1107 wait_for_completion(&nport->tport_unreg_done);
1108
1109 fcloop_nport_put(nport);
1110
1111 return ret;
1112}
1113
1114static ssize_t
1115fcloop_delete_target_port(struct device *dev, struct device_attribute *attr,
1116 const char *buf, size_t count)
1117{
1118 struct fcloop_nport *nport = NULL, *tmpport;
1119 struct fcloop_tport *tport;
1120 u64 nodename, portname;
1121 unsigned long flags;
1122 int ret;
1123
1124 ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
1125 if (ret)
1126 return ret;
1127
1128 spin_lock_irqsave(&fcloop_lock, flags);
1129
1130 list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
1131 if (tmpport->node_name == nodename &&
1132 tmpport->port_name == portname && tmpport->tport) {
1133 nport = tmpport;
1134 tport = __unlink_target_port(nport);
1135 break;
1136 }
1137 }
1138
1139 spin_unlock_irqrestore(&fcloop_lock, flags);
1140
1141 if (!nport)
1142 return -ENOENT;
1143
1144 ret = __wait_targetport_unreg(nport, tport);
1145
1146 return ret ? ret : count;
1147}
1148
1149
1150static DEVICE_ATTR(add_local_port, 0200, NULL, fcloop_create_local_port);
1151static DEVICE_ATTR(del_local_port, 0200, NULL, fcloop_delete_local_port);
1152static DEVICE_ATTR(add_remote_port, 0200, NULL, fcloop_create_remote_port);
1153static DEVICE_ATTR(del_remote_port, 0200, NULL, fcloop_delete_remote_port);
1154static DEVICE_ATTR(add_target_port, 0200, NULL, fcloop_create_target_port);
1155static DEVICE_ATTR(del_target_port, 0200, NULL, fcloop_delete_target_port);
1156
1157static struct attribute *fcloop_dev_attrs[] = {
1158 &dev_attr_add_local_port.attr,
1159 &dev_attr_del_local_port.attr,
1160 &dev_attr_add_remote_port.attr,
1161 &dev_attr_del_remote_port.attr,
1162 &dev_attr_add_target_port.attr,
1163 &dev_attr_del_target_port.attr,
1164 NULL
1165};
1166
1167static struct attribute_group fclopp_dev_attrs_group = {
1168 .attrs = fcloop_dev_attrs,
1169};
1170
1171static const struct attribute_group *fcloop_dev_attr_groups[] = {
1172 &fclopp_dev_attrs_group,
1173 NULL,
1174};
1175
1176static struct class *fcloop_class;
1177static struct device *fcloop_device;
1178
1179
1180static int __init fcloop_init(void)
1181{
1182 int ret;
1183
1184 fcloop_class = class_create(THIS_MODULE, "fcloop");
1185 if (IS_ERR(fcloop_class)) {
1186 pr_err("couldn't register class fcloop\n");
1187 ret = PTR_ERR(fcloop_class);
1188 return ret;
1189 }
1190
1191 fcloop_device = device_create_with_groups(
1192 fcloop_class, NULL, MKDEV(0, 0), NULL,
1193 fcloop_dev_attr_groups, "ctl");
1194 if (IS_ERR(fcloop_device)) {
1195 pr_err("couldn't create ctl device!\n");
1196 ret = PTR_ERR(fcloop_device);
1197 goto out_destroy_class;
1198 }
1199
1200 get_device(fcloop_device);
1201
1202 return 0;
1203
1204out_destroy_class:
1205 class_destroy(fcloop_class);
1206 return ret;
1207}
1208
1209static void __exit fcloop_exit(void)
1210{
1211 struct fcloop_lport *lport;
1212 struct fcloop_nport *nport;
1213 struct fcloop_tport *tport;
1214 struct fcloop_rport *rport;
1215 unsigned long flags;
1216 int ret;
1217
1218 spin_lock_irqsave(&fcloop_lock, flags);
1219
1220 for (;;) {
1221 nport = list_first_entry_or_null(&fcloop_nports,
1222 typeof(*nport), nport_list);
1223 if (!nport)
1224 break;
1225
1226 tport = __unlink_target_port(nport);
1227 rport = __unlink_remote_port(nport);
1228
1229 spin_unlock_irqrestore(&fcloop_lock, flags);
1230
1231 ret = __wait_targetport_unreg(nport, tport);
1232 if (ret)
1233 pr_warn("%s: Failed deleting target port\n", __func__);
1234
1235 ret = __wait_remoteport_unreg(nport, rport);
1236 if (ret)
1237 pr_warn("%s: Failed deleting remote port\n", __func__);
1238
1239 spin_lock_irqsave(&fcloop_lock, flags);
1240 }
1241
1242 for (;;) {
1243 lport = list_first_entry_or_null(&fcloop_lports,
1244 typeof(*lport), lport_list);
1245 if (!lport)
1246 break;
1247
1248 __unlink_local_port(lport);
1249
1250 spin_unlock_irqrestore(&fcloop_lock, flags);
1251
1252 ret = __wait_localport_unreg(lport);
1253 if (ret)
1254 pr_warn("%s: Failed deleting local port\n", __func__);
1255
1256 spin_lock_irqsave(&fcloop_lock, flags);
1257 }
1258
1259 spin_unlock_irqrestore(&fcloop_lock, flags);
1260
1261 put_device(fcloop_device);
1262
1263 device_destroy(fcloop_class, MKDEV(0, 0));
1264 class_destroy(fcloop_class);
1265}
1266
1267module_init(fcloop_init);
1268module_exit(fcloop_exit);
1269
1270MODULE_LICENSE("GPL v2");