Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2016 Avago Technologies.  All rights reserved.
0004  */
0005 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0006 #include <linux/module.h>
0007 #include <linux/parser.h>
0008 #include <uapi/scsi/fc/fc_fs.h>
0009 
0010 #include "../host/nvme.h"
0011 #include "../target/nvmet.h"
0012 #include <linux/nvme-fc-driver.h>
0013 #include <linux/nvme-fc.h>
0014 
0015 
0016 enum {
0017     NVMF_OPT_ERR        = 0,
0018     NVMF_OPT_WWNN       = 1 << 0,
0019     NVMF_OPT_WWPN       = 1 << 1,
0020     NVMF_OPT_ROLES      = 1 << 2,
0021     NVMF_OPT_FCADDR     = 1 << 3,
0022     NVMF_OPT_LPWWNN     = 1 << 4,
0023     NVMF_OPT_LPWWPN     = 1 << 5,
0024 };
0025 
0026 struct fcloop_ctrl_options {
0027     int         mask;
0028     u64         wwnn;
0029     u64         wwpn;
0030     u32         roles;
0031     u32         fcaddr;
0032     u64         lpwwnn;
0033     u64         lpwwpn;
0034 };
0035 
0036 static const match_table_t opt_tokens = {
0037     { NVMF_OPT_WWNN,    "wwnn=%s"   },
0038     { NVMF_OPT_WWPN,    "wwpn=%s"   },
0039     { NVMF_OPT_ROLES,   "roles=%d"  },
0040     { NVMF_OPT_FCADDR,  "fcaddr=%x" },
0041     { NVMF_OPT_LPWWNN,  "lpwwnn=%s" },
0042     { NVMF_OPT_LPWWPN,  "lpwwpn=%s" },
0043     { NVMF_OPT_ERR,     NULL        }
0044 };
0045 
0046 static int fcloop_verify_addr(substring_t *s)
0047 {
0048     size_t blen = s->to - s->from + 1;
0049 
0050     if (strnlen(s->from, blen) != NVME_FC_TRADDR_HEXNAMELEN + 2 ||
0051         strncmp(s->from, "0x", 2))
0052         return -EINVAL;
0053 
0054     return 0;
0055 }
0056 
0057 static int
0058 fcloop_parse_options(struct fcloop_ctrl_options *opts,
0059         const char *buf)
0060 {
0061     substring_t args[MAX_OPT_ARGS];
0062     char *options, *o, *p;
0063     int token, ret = 0;
0064     u64 token64;
0065 
0066     options = o = kstrdup(buf, GFP_KERNEL);
0067     if (!options)
0068         return -ENOMEM;
0069 
0070     while ((p = strsep(&o, ",\n")) != NULL) {
0071         if (!*p)
0072             continue;
0073 
0074         token = match_token(p, opt_tokens, args);
0075         opts->mask |= token;
0076         switch (token) {
0077         case NVMF_OPT_WWNN:
0078             if (fcloop_verify_addr(args) ||
0079                 match_u64(args, &token64)) {
0080                 ret = -EINVAL;
0081                 goto out_free_options;
0082             }
0083             opts->wwnn = token64;
0084             break;
0085         case NVMF_OPT_WWPN:
0086             if (fcloop_verify_addr(args) ||
0087                 match_u64(args, &token64)) {
0088                 ret = -EINVAL;
0089                 goto out_free_options;
0090             }
0091             opts->wwpn = token64;
0092             break;
0093         case NVMF_OPT_ROLES:
0094             if (match_int(args, &token)) {
0095                 ret = -EINVAL;
0096                 goto out_free_options;
0097             }
0098             opts->roles = token;
0099             break;
0100         case NVMF_OPT_FCADDR:
0101             if (match_hex(args, &token)) {
0102                 ret = -EINVAL;
0103                 goto out_free_options;
0104             }
0105             opts->fcaddr = token;
0106             break;
0107         case NVMF_OPT_LPWWNN:
0108             if (fcloop_verify_addr(args) ||
0109                 match_u64(args, &token64)) {
0110                 ret = -EINVAL;
0111                 goto out_free_options;
0112             }
0113             opts->lpwwnn = token64;
0114             break;
0115         case NVMF_OPT_LPWWPN:
0116             if (fcloop_verify_addr(args) ||
0117                 match_u64(args, &token64)) {
0118                 ret = -EINVAL;
0119                 goto out_free_options;
0120             }
0121             opts->lpwwpn = token64;
0122             break;
0123         default:
0124             pr_warn("unknown parameter or missing value '%s'\n", p);
0125             ret = -EINVAL;
0126             goto out_free_options;
0127         }
0128     }
0129 
0130 out_free_options:
0131     kfree(options);
0132     return ret;
0133 }
0134 
0135 
0136 static int
0137 fcloop_parse_nm_options(struct device *dev, u64 *nname, u64 *pname,
0138         const char *buf)
0139 {
0140     substring_t args[MAX_OPT_ARGS];
0141     char *options, *o, *p;
0142     int token, ret = 0;
0143     u64 token64;
0144 
0145     *nname = -1;
0146     *pname = -1;
0147 
0148     options = o = kstrdup(buf, GFP_KERNEL);
0149     if (!options)
0150         return -ENOMEM;
0151 
0152     while ((p = strsep(&o, ",\n")) != NULL) {
0153         if (!*p)
0154             continue;
0155 
0156         token = match_token(p, opt_tokens, args);
0157         switch (token) {
0158         case NVMF_OPT_WWNN:
0159             if (fcloop_verify_addr(args) ||
0160                 match_u64(args, &token64)) {
0161                 ret = -EINVAL;
0162                 goto out_free_options;
0163             }
0164             *nname = token64;
0165             break;
0166         case NVMF_OPT_WWPN:
0167             if (fcloop_verify_addr(args) ||
0168                 match_u64(args, &token64)) {
0169                 ret = -EINVAL;
0170                 goto out_free_options;
0171             }
0172             *pname = token64;
0173             break;
0174         default:
0175             pr_warn("unknown parameter or missing value '%s'\n", p);
0176             ret = -EINVAL;
0177             goto out_free_options;
0178         }
0179     }
0180 
0181 out_free_options:
0182     kfree(options);
0183 
0184     if (!ret) {
0185         if (*nname == -1)
0186             return -EINVAL;
0187         if (*pname == -1)
0188             return -EINVAL;
0189     }
0190 
0191     return ret;
0192 }
0193 
0194 
0195 #define LPORT_OPTS  (NVMF_OPT_WWNN | NVMF_OPT_WWPN)
0196 
0197 #define RPORT_OPTS  (NVMF_OPT_WWNN | NVMF_OPT_WWPN |  \
0198              NVMF_OPT_LPWWNN | NVMF_OPT_LPWWPN)
0199 
0200 #define TGTPORT_OPTS    (NVMF_OPT_WWNN | NVMF_OPT_WWPN)
0201 
0202 
0203 static DEFINE_SPINLOCK(fcloop_lock);
0204 static LIST_HEAD(fcloop_lports);
0205 static LIST_HEAD(fcloop_nports);
0206 
0207 struct fcloop_lport {
0208     struct nvme_fc_local_port *localport;
0209     struct list_head lport_list;
0210     struct completion unreg_done;
0211 };
0212 
0213 struct fcloop_lport_priv {
0214     struct fcloop_lport *lport;
0215 };
0216 
0217 struct fcloop_rport {
0218     struct nvme_fc_remote_port  *remoteport;
0219     struct nvmet_fc_target_port *targetport;
0220     struct fcloop_nport     *nport;
0221     struct fcloop_lport     *lport;
0222     spinlock_t          lock;
0223     struct list_head        ls_list;
0224     struct work_struct      ls_work;
0225 };
0226 
0227 struct fcloop_tport {
0228     struct nvmet_fc_target_port *targetport;
0229     struct nvme_fc_remote_port  *remoteport;
0230     struct fcloop_nport     *nport;
0231     struct fcloop_lport     *lport;
0232     spinlock_t          lock;
0233     struct list_head        ls_list;
0234     struct work_struct      ls_work;
0235 };
0236 
0237 struct fcloop_nport {
0238     struct fcloop_rport *rport;
0239     struct fcloop_tport *tport;
0240     struct fcloop_lport *lport;
0241     struct list_head nport_list;
0242     struct kref ref;
0243     u64 node_name;
0244     u64 port_name;
0245     u32 port_role;
0246     u32 port_id;
0247 };
0248 
0249 struct fcloop_lsreq {
0250     struct nvmefc_ls_req        *lsreq;
0251     struct nvmefc_ls_rsp        ls_rsp;
0252     int             lsdir;  /* H2T or T2H */
0253     int             status;
0254     struct list_head        ls_list; /* fcloop_rport->ls_list */
0255 };
0256 
0257 struct fcloop_rscn {
0258     struct fcloop_tport     *tport;
0259     struct work_struct      work;
0260 };
0261 
0262 enum {
0263     INI_IO_START        = 0,
0264     INI_IO_ACTIVE       = 1,
0265     INI_IO_ABORTED      = 2,
0266     INI_IO_COMPLETED    = 3,
0267 };
0268 
0269 struct fcloop_fcpreq {
0270     struct fcloop_tport     *tport;
0271     struct nvmefc_fcp_req       *fcpreq;
0272     spinlock_t          reqlock;
0273     u16             status;
0274     u32             inistate;
0275     bool                active;
0276     bool                aborted;
0277     struct kref         ref;
0278     struct work_struct      fcp_rcv_work;
0279     struct work_struct      abort_rcv_work;
0280     struct work_struct      tio_done_work;
0281     struct nvmefc_tgt_fcp_req   tgt_fcp_req;
0282 };
0283 
0284 struct fcloop_ini_fcpreq {
0285     struct nvmefc_fcp_req       *fcpreq;
0286     struct fcloop_fcpreq        *tfcp_req;
0287     spinlock_t          inilock;
0288 };
0289 
0290 static inline struct fcloop_lsreq *
0291 ls_rsp_to_lsreq(struct nvmefc_ls_rsp *lsrsp)
0292 {
0293     return container_of(lsrsp, struct fcloop_lsreq, ls_rsp);
0294 }
0295 
0296 static inline struct fcloop_fcpreq *
0297 tgt_fcp_req_to_fcpreq(struct nvmefc_tgt_fcp_req *tgt_fcpreq)
0298 {
0299     return container_of(tgt_fcpreq, struct fcloop_fcpreq, tgt_fcp_req);
0300 }
0301 
0302 
0303 static int
0304 fcloop_create_queue(struct nvme_fc_local_port *localport,
0305             unsigned int qidx, u16 qsize,
0306             void **handle)
0307 {
0308     *handle = localport;
0309     return 0;
0310 }
0311 
0312 static void
0313 fcloop_delete_queue(struct nvme_fc_local_port *localport,
0314             unsigned int idx, void *handle)
0315 {
0316 }
0317 
0318 static void
0319 fcloop_rport_lsrqst_work(struct work_struct *work)
0320 {
0321     struct fcloop_rport *rport =
0322         container_of(work, struct fcloop_rport, ls_work);
0323     struct fcloop_lsreq *tls_req;
0324 
0325     spin_lock(&rport->lock);
0326     for (;;) {
0327         tls_req = list_first_entry_or_null(&rport->ls_list,
0328                 struct fcloop_lsreq, ls_list);
0329         if (!tls_req)
0330             break;
0331 
0332         list_del(&tls_req->ls_list);
0333         spin_unlock(&rport->lock);
0334 
0335         tls_req->lsreq->done(tls_req->lsreq, tls_req->status);
0336         /*
0337          * callee may free memory containing tls_req.
0338          * do not reference lsreq after this.
0339          */
0340 
0341         spin_lock(&rport->lock);
0342     }
0343     spin_unlock(&rport->lock);
0344 }
0345 
0346 static int
0347 fcloop_h2t_ls_req(struct nvme_fc_local_port *localport,
0348             struct nvme_fc_remote_port *remoteport,
0349             struct nvmefc_ls_req *lsreq)
0350 {
0351     struct fcloop_lsreq *tls_req = lsreq->private;
0352     struct fcloop_rport *rport = remoteport->private;
0353     int ret = 0;
0354 
0355     tls_req->lsreq = lsreq;
0356     INIT_LIST_HEAD(&tls_req->ls_list);
0357 
0358     if (!rport->targetport) {
0359         tls_req->status = -ECONNREFUSED;
0360         spin_lock(&rport->lock);
0361         list_add_tail(&rport->ls_list, &tls_req->ls_list);
0362         spin_unlock(&rport->lock);
0363         queue_work(nvmet_wq, &rport->ls_work);
0364         return ret;
0365     }
0366 
0367     tls_req->status = 0;
0368     ret = nvmet_fc_rcv_ls_req(rport->targetport, rport,
0369                   &tls_req->ls_rsp,
0370                   lsreq->rqstaddr, lsreq->rqstlen);
0371 
0372     return ret;
0373 }
0374 
0375 static int
0376 fcloop_h2t_xmt_ls_rsp(struct nvmet_fc_target_port *targetport,
0377             struct nvmefc_ls_rsp *lsrsp)
0378 {
0379     struct fcloop_lsreq *tls_req = ls_rsp_to_lsreq(lsrsp);
0380     struct nvmefc_ls_req *lsreq = tls_req->lsreq;
0381     struct fcloop_tport *tport = targetport->private;
0382     struct nvme_fc_remote_port *remoteport = tport->remoteport;
0383     struct fcloop_rport *rport;
0384 
0385     memcpy(lsreq->rspaddr, lsrsp->rspbuf,
0386         ((lsreq->rsplen < lsrsp->rsplen) ?
0387                 lsreq->rsplen : lsrsp->rsplen));
0388 
0389     lsrsp->done(lsrsp);
0390 
0391     if (remoteport) {
0392         rport = remoteport->private;
0393         spin_lock(&rport->lock);
0394         list_add_tail(&rport->ls_list, &tls_req->ls_list);
0395         spin_unlock(&rport->lock);
0396         queue_work(nvmet_wq, &rport->ls_work);
0397     }
0398 
0399     return 0;
0400 }
0401 
0402 static void
0403 fcloop_tport_lsrqst_work(struct work_struct *work)
0404 {
0405     struct fcloop_tport *tport =
0406         container_of(work, struct fcloop_tport, ls_work);
0407     struct fcloop_lsreq *tls_req;
0408 
0409     spin_lock(&tport->lock);
0410     for (;;) {
0411         tls_req = list_first_entry_or_null(&tport->ls_list,
0412                 struct fcloop_lsreq, ls_list);
0413         if (!tls_req)
0414             break;
0415 
0416         list_del(&tls_req->ls_list);
0417         spin_unlock(&tport->lock);
0418 
0419         tls_req->lsreq->done(tls_req->lsreq, tls_req->status);
0420         /*
0421          * callee may free memory containing tls_req.
0422          * do not reference lsreq after this.
0423          */
0424 
0425         spin_lock(&tport->lock);
0426     }
0427     spin_unlock(&tport->lock);
0428 }
0429 
0430 static int
0431 fcloop_t2h_ls_req(struct nvmet_fc_target_port *targetport, void *hosthandle,
0432             struct nvmefc_ls_req *lsreq)
0433 {
0434     struct fcloop_lsreq *tls_req = lsreq->private;
0435     struct fcloop_tport *tport = targetport->private;
0436     int ret = 0;
0437 
0438     /*
0439      * hosthandle should be the dst.rport value.
0440      * hosthandle ignored as fcloop currently is
0441      * 1:1 tgtport vs remoteport
0442      */
0443     tls_req->lsreq = lsreq;
0444     INIT_LIST_HEAD(&tls_req->ls_list);
0445 
0446     if (!tport->remoteport) {
0447         tls_req->status = -ECONNREFUSED;
0448         spin_lock(&tport->lock);
0449         list_add_tail(&tport->ls_list, &tls_req->ls_list);
0450         spin_unlock(&tport->lock);
0451         queue_work(nvmet_wq, &tport->ls_work);
0452         return ret;
0453     }
0454 
0455     tls_req->status = 0;
0456     ret = nvme_fc_rcv_ls_req(tport->remoteport, &tls_req->ls_rsp,
0457                  lsreq->rqstaddr, lsreq->rqstlen);
0458 
0459     return ret;
0460 }
0461 
0462 static int
0463 fcloop_t2h_xmt_ls_rsp(struct nvme_fc_local_port *localport,
0464             struct nvme_fc_remote_port *remoteport,
0465             struct nvmefc_ls_rsp *lsrsp)
0466 {
0467     struct fcloop_lsreq *tls_req = ls_rsp_to_lsreq(lsrsp);
0468     struct nvmefc_ls_req *lsreq = tls_req->lsreq;
0469     struct fcloop_rport *rport = remoteport->private;
0470     struct nvmet_fc_target_port *targetport = rport->targetport;
0471     struct fcloop_tport *tport;
0472 
0473     memcpy(lsreq->rspaddr, lsrsp->rspbuf,
0474         ((lsreq->rsplen < lsrsp->rsplen) ?
0475                 lsreq->rsplen : lsrsp->rsplen));
0476     lsrsp->done(lsrsp);
0477 
0478     if (targetport) {
0479         tport = targetport->private;
0480         spin_lock(&tport->lock);
0481         list_add_tail(&tport->ls_list, &tls_req->ls_list);
0482         spin_unlock(&tport->lock);
0483         queue_work(nvmet_wq, &tport->ls_work);
0484     }
0485 
0486     return 0;
0487 }
0488 
0489 static void
0490 fcloop_t2h_host_release(void *hosthandle)
0491 {
0492     /* host handle ignored for now */
0493 }
0494 
0495 /*
0496  * Simulate reception of RSCN and converting it to a initiator transport
0497  * call to rescan a remote port.
0498  */
0499 static void
0500 fcloop_tgt_rscn_work(struct work_struct *work)
0501 {
0502     struct fcloop_rscn *tgt_rscn =
0503         container_of(work, struct fcloop_rscn, work);
0504     struct fcloop_tport *tport = tgt_rscn->tport;
0505 
0506     if (tport->remoteport)
0507         nvme_fc_rescan_remoteport(tport->remoteport);
0508     kfree(tgt_rscn);
0509 }
0510 
0511 static void
0512 fcloop_tgt_discovery_evt(struct nvmet_fc_target_port *tgtport)
0513 {
0514     struct fcloop_rscn *tgt_rscn;
0515 
0516     tgt_rscn = kzalloc(sizeof(*tgt_rscn), GFP_KERNEL);
0517     if (!tgt_rscn)
0518         return;
0519 
0520     tgt_rscn->tport = tgtport->private;
0521     INIT_WORK(&tgt_rscn->work, fcloop_tgt_rscn_work);
0522 
0523     queue_work(nvmet_wq, &tgt_rscn->work);
0524 }
0525 
0526 static void
0527 fcloop_tfcp_req_free(struct kref *ref)
0528 {
0529     struct fcloop_fcpreq *tfcp_req =
0530         container_of(ref, struct fcloop_fcpreq, ref);
0531 
0532     kfree(tfcp_req);
0533 }
0534 
0535 static void
0536 fcloop_tfcp_req_put(struct fcloop_fcpreq *tfcp_req)
0537 {
0538     kref_put(&tfcp_req->ref, fcloop_tfcp_req_free);
0539 }
0540 
0541 static int
0542 fcloop_tfcp_req_get(struct fcloop_fcpreq *tfcp_req)
0543 {
0544     return kref_get_unless_zero(&tfcp_req->ref);
0545 }
0546 
0547 static void
0548 fcloop_call_host_done(struct nvmefc_fcp_req *fcpreq,
0549             struct fcloop_fcpreq *tfcp_req, int status)
0550 {
0551     struct fcloop_ini_fcpreq *inireq = NULL;
0552 
0553     if (fcpreq) {
0554         inireq = fcpreq->private;
0555         spin_lock(&inireq->inilock);
0556         inireq->tfcp_req = NULL;
0557         spin_unlock(&inireq->inilock);
0558 
0559         fcpreq->status = status;
0560         fcpreq->done(fcpreq);
0561     }
0562 
0563     /* release original io reference on tgt struct */
0564     fcloop_tfcp_req_put(tfcp_req);
0565 }
0566 
0567 static bool drop_fabric_opcode;
0568 #define DROP_OPCODE_MASK    0x00FF
0569 /* fabrics opcode will have a bit set above 1st byte */
0570 static int drop_opcode = -1;
0571 static int drop_instance;
0572 static int drop_amount;
0573 static int drop_current_cnt;
0574 
0575 /*
0576  * Routine to parse io and determine if the io is to be dropped.
0577  * Returns:
0578  *  0 if io is not obstructed
0579  *  1 if io was dropped
0580  */
0581 static int check_for_drop(struct fcloop_fcpreq *tfcp_req)
0582 {
0583     struct nvmefc_fcp_req *fcpreq = tfcp_req->fcpreq;
0584     struct nvme_fc_cmd_iu *cmdiu = fcpreq->cmdaddr;
0585     struct nvme_command *sqe = &cmdiu->sqe;
0586 
0587     if (drop_opcode == -1)
0588         return 0;
0589 
0590     pr_info("%s: seq opcd x%02x fctype x%02x: drop F %s op x%02x "
0591         "inst %d start %d amt %d\n",
0592         __func__, sqe->common.opcode, sqe->fabrics.fctype,
0593         drop_fabric_opcode ? "y" : "n",
0594         drop_opcode, drop_current_cnt, drop_instance, drop_amount);
0595 
0596     if ((drop_fabric_opcode &&
0597          (sqe->common.opcode != nvme_fabrics_command ||
0598           sqe->fabrics.fctype != drop_opcode)) ||
0599         (!drop_fabric_opcode && sqe->common.opcode != drop_opcode))
0600         return 0;
0601 
0602     if (++drop_current_cnt >= drop_instance) {
0603         if (drop_current_cnt >= drop_instance + drop_amount)
0604             drop_opcode = -1;
0605         return 1;
0606     }
0607 
0608     return 0;
0609 }
0610 
0611 static void
0612 fcloop_fcp_recv_work(struct work_struct *work)
0613 {
0614     struct fcloop_fcpreq *tfcp_req =
0615         container_of(work, struct fcloop_fcpreq, fcp_rcv_work);
0616     struct nvmefc_fcp_req *fcpreq = tfcp_req->fcpreq;
0617     int ret = 0;
0618     bool aborted = false;
0619 
0620     spin_lock_irq(&tfcp_req->reqlock);
0621     switch (tfcp_req->inistate) {
0622     case INI_IO_START:
0623         tfcp_req->inistate = INI_IO_ACTIVE;
0624         break;
0625     case INI_IO_ABORTED:
0626         aborted = true;
0627         break;
0628     default:
0629         spin_unlock_irq(&tfcp_req->reqlock);
0630         WARN_ON(1);
0631         return;
0632     }
0633     spin_unlock_irq(&tfcp_req->reqlock);
0634 
0635     if (unlikely(aborted))
0636         ret = -ECANCELED;
0637     else {
0638         if (likely(!check_for_drop(tfcp_req)))
0639             ret = nvmet_fc_rcv_fcp_req(tfcp_req->tport->targetport,
0640                 &tfcp_req->tgt_fcp_req,
0641                 fcpreq->cmdaddr, fcpreq->cmdlen);
0642         else
0643             pr_info("%s: dropped command ********\n", __func__);
0644     }
0645     if (ret)
0646         fcloop_call_host_done(fcpreq, tfcp_req, ret);
0647 
0648     return;
0649 }
0650 
0651 static void
0652 fcloop_fcp_abort_recv_work(struct work_struct *work)
0653 {
0654     struct fcloop_fcpreq *tfcp_req =
0655         container_of(work, struct fcloop_fcpreq, abort_rcv_work);
0656     struct nvmefc_fcp_req *fcpreq;
0657     bool completed = false;
0658 
0659     spin_lock_irq(&tfcp_req->reqlock);
0660     fcpreq = tfcp_req->fcpreq;
0661     switch (tfcp_req->inistate) {
0662     case INI_IO_ABORTED:
0663         break;
0664     case INI_IO_COMPLETED:
0665         completed = true;
0666         break;
0667     default:
0668         spin_unlock_irq(&tfcp_req->reqlock);
0669         WARN_ON(1);
0670         return;
0671     }
0672     spin_unlock_irq(&tfcp_req->reqlock);
0673 
0674     if (unlikely(completed)) {
0675         /* remove reference taken in original abort downcall */
0676         fcloop_tfcp_req_put(tfcp_req);
0677         return;
0678     }
0679 
0680     if (tfcp_req->tport->targetport)
0681         nvmet_fc_rcv_fcp_abort(tfcp_req->tport->targetport,
0682                     &tfcp_req->tgt_fcp_req);
0683 
0684     spin_lock_irq(&tfcp_req->reqlock);
0685     tfcp_req->fcpreq = NULL;
0686     spin_unlock_irq(&tfcp_req->reqlock);
0687 
0688     fcloop_call_host_done(fcpreq, tfcp_req, -ECANCELED);
0689     /* call_host_done releases reference for abort downcall */
0690 }
0691 
0692 /*
0693  * FCP IO operation done by target completion.
0694  * call back up initiator "done" flows.
0695  */
0696 static void
0697 fcloop_tgt_fcprqst_done_work(struct work_struct *work)
0698 {
0699     struct fcloop_fcpreq *tfcp_req =
0700         container_of(work, struct fcloop_fcpreq, tio_done_work);
0701     struct nvmefc_fcp_req *fcpreq;
0702 
0703     spin_lock_irq(&tfcp_req->reqlock);
0704     fcpreq = tfcp_req->fcpreq;
0705     tfcp_req->inistate = INI_IO_COMPLETED;
0706     spin_unlock_irq(&tfcp_req->reqlock);
0707 
0708     fcloop_call_host_done(fcpreq, tfcp_req, tfcp_req->status);
0709 }
0710 
0711 
0712 static int
0713 fcloop_fcp_req(struct nvme_fc_local_port *localport,
0714             struct nvme_fc_remote_port *remoteport,
0715             void *hw_queue_handle,
0716             struct nvmefc_fcp_req *fcpreq)
0717 {
0718     struct fcloop_rport *rport = remoteport->private;
0719     struct fcloop_ini_fcpreq *inireq = fcpreq->private;
0720     struct fcloop_fcpreq *tfcp_req;
0721 
0722     if (!rport->targetport)
0723         return -ECONNREFUSED;
0724 
0725     tfcp_req = kzalloc(sizeof(*tfcp_req), GFP_ATOMIC);
0726     if (!tfcp_req)
0727         return -ENOMEM;
0728 
0729     inireq->fcpreq = fcpreq;
0730     inireq->tfcp_req = tfcp_req;
0731     spin_lock_init(&inireq->inilock);
0732 
0733     tfcp_req->fcpreq = fcpreq;
0734     tfcp_req->tport = rport->targetport->private;
0735     tfcp_req->inistate = INI_IO_START;
0736     spin_lock_init(&tfcp_req->reqlock);
0737     INIT_WORK(&tfcp_req->fcp_rcv_work, fcloop_fcp_recv_work);
0738     INIT_WORK(&tfcp_req->abort_rcv_work, fcloop_fcp_abort_recv_work);
0739     INIT_WORK(&tfcp_req->tio_done_work, fcloop_tgt_fcprqst_done_work);
0740     kref_init(&tfcp_req->ref);
0741 
0742     queue_work(nvmet_wq, &tfcp_req->fcp_rcv_work);
0743 
0744     return 0;
0745 }
0746 
0747 static void
0748 fcloop_fcp_copy_data(u8 op, struct scatterlist *data_sg,
0749             struct scatterlist *io_sg, u32 offset, u32 length)
0750 {
0751     void *data_p, *io_p;
0752     u32 data_len, io_len, tlen;
0753 
0754     io_p = sg_virt(io_sg);
0755     io_len = io_sg->length;
0756 
0757     for ( ; offset; ) {
0758         tlen = min_t(u32, offset, io_len);
0759         offset -= tlen;
0760         io_len -= tlen;
0761         if (!io_len) {
0762             io_sg = sg_next(io_sg);
0763             io_p = sg_virt(io_sg);
0764             io_len = io_sg->length;
0765         } else
0766             io_p += tlen;
0767     }
0768 
0769     data_p = sg_virt(data_sg);
0770     data_len = data_sg->length;
0771 
0772     for ( ; length; ) {
0773         tlen = min_t(u32, io_len, data_len);
0774         tlen = min_t(u32, tlen, length);
0775 
0776         if (op == NVMET_FCOP_WRITEDATA)
0777             memcpy(data_p, io_p, tlen);
0778         else
0779             memcpy(io_p, data_p, tlen);
0780 
0781         length -= tlen;
0782 
0783         io_len -= tlen;
0784         if ((!io_len) && (length)) {
0785             io_sg = sg_next(io_sg);
0786             io_p = sg_virt(io_sg);
0787             io_len = io_sg->length;
0788         } else
0789             io_p += tlen;
0790 
0791         data_len -= tlen;
0792         if ((!data_len) && (length)) {
0793             data_sg = sg_next(data_sg);
0794             data_p = sg_virt(data_sg);
0795             data_len = data_sg->length;
0796         } else
0797             data_p += tlen;
0798     }
0799 }
0800 
0801 static int
0802 fcloop_fcp_op(struct nvmet_fc_target_port *tgtport,
0803             struct nvmefc_tgt_fcp_req *tgt_fcpreq)
0804 {
0805     struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
0806     struct nvmefc_fcp_req *fcpreq;
0807     u32 rsplen = 0, xfrlen = 0;
0808     int fcp_err = 0, active, aborted;
0809     u8 op = tgt_fcpreq->op;
0810 
0811     spin_lock_irq(&tfcp_req->reqlock);
0812     fcpreq = tfcp_req->fcpreq;
0813     active = tfcp_req->active;
0814     aborted = tfcp_req->aborted;
0815     tfcp_req->active = true;
0816     spin_unlock_irq(&tfcp_req->reqlock);
0817 
0818     if (unlikely(active))
0819         /* illegal - call while i/o active */
0820         return -EALREADY;
0821 
0822     if (unlikely(aborted)) {
0823         /* target transport has aborted i/o prior */
0824         spin_lock_irq(&tfcp_req->reqlock);
0825         tfcp_req->active = false;
0826         spin_unlock_irq(&tfcp_req->reqlock);
0827         tgt_fcpreq->transferred_length = 0;
0828         tgt_fcpreq->fcp_error = -ECANCELED;
0829         tgt_fcpreq->done(tgt_fcpreq);
0830         return 0;
0831     }
0832 
0833     /*
0834      * if fcpreq is NULL, the I/O has been aborted (from
0835      * initiator side). For the target side, act as if all is well
0836      * but don't actually move data.
0837      */
0838 
0839     switch (op) {
0840     case NVMET_FCOP_WRITEDATA:
0841         xfrlen = tgt_fcpreq->transfer_length;
0842         if (fcpreq) {
0843             fcloop_fcp_copy_data(op, tgt_fcpreq->sg,
0844                     fcpreq->first_sgl, tgt_fcpreq->offset,
0845                     xfrlen);
0846             fcpreq->transferred_length += xfrlen;
0847         }
0848         break;
0849 
0850     case NVMET_FCOP_READDATA:
0851     case NVMET_FCOP_READDATA_RSP:
0852         xfrlen = tgt_fcpreq->transfer_length;
0853         if (fcpreq) {
0854             fcloop_fcp_copy_data(op, tgt_fcpreq->sg,
0855                     fcpreq->first_sgl, tgt_fcpreq->offset,
0856                     xfrlen);
0857             fcpreq->transferred_length += xfrlen;
0858         }
0859         if (op == NVMET_FCOP_READDATA)
0860             break;
0861 
0862         /* Fall-Thru to RSP handling */
0863         fallthrough;
0864 
0865     case NVMET_FCOP_RSP:
0866         if (fcpreq) {
0867             rsplen = ((fcpreq->rsplen < tgt_fcpreq->rsplen) ?
0868                     fcpreq->rsplen : tgt_fcpreq->rsplen);
0869             memcpy(fcpreq->rspaddr, tgt_fcpreq->rspaddr, rsplen);
0870             if (rsplen < tgt_fcpreq->rsplen)
0871                 fcp_err = -E2BIG;
0872             fcpreq->rcv_rsplen = rsplen;
0873             fcpreq->status = 0;
0874         }
0875         tfcp_req->status = 0;
0876         break;
0877 
0878     default:
0879         fcp_err = -EINVAL;
0880         break;
0881     }
0882 
0883     spin_lock_irq(&tfcp_req->reqlock);
0884     tfcp_req->active = false;
0885     spin_unlock_irq(&tfcp_req->reqlock);
0886 
0887     tgt_fcpreq->transferred_length = xfrlen;
0888     tgt_fcpreq->fcp_error = fcp_err;
0889     tgt_fcpreq->done(tgt_fcpreq);
0890 
0891     return 0;
0892 }
0893 
0894 static void
0895 fcloop_tgt_fcp_abort(struct nvmet_fc_target_port *tgtport,
0896             struct nvmefc_tgt_fcp_req *tgt_fcpreq)
0897 {
0898     struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
0899 
0900     /*
0901      * mark aborted only in case there were 2 threads in transport
0902      * (one doing io, other doing abort) and only kills ops posted
0903      * after the abort request
0904      */
0905     spin_lock_irq(&tfcp_req->reqlock);
0906     tfcp_req->aborted = true;
0907     spin_unlock_irq(&tfcp_req->reqlock);
0908 
0909     tfcp_req->status = NVME_SC_INTERNAL;
0910 
0911     /*
0912      * nothing more to do. If io wasn't active, the transport should
0913      * immediately call the req_release. If it was active, the op
0914      * will complete, and the lldd should call req_release.
0915      */
0916 }
0917 
0918 static void
0919 fcloop_fcp_req_release(struct nvmet_fc_target_port *tgtport,
0920             struct nvmefc_tgt_fcp_req *tgt_fcpreq)
0921 {
0922     struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
0923 
0924     queue_work(nvmet_wq, &tfcp_req->tio_done_work);
0925 }
0926 
0927 static void
0928 fcloop_h2t_ls_abort(struct nvme_fc_local_port *localport,
0929             struct nvme_fc_remote_port *remoteport,
0930                 struct nvmefc_ls_req *lsreq)
0931 {
0932 }
0933 
0934 static void
0935 fcloop_t2h_ls_abort(struct nvmet_fc_target_port *targetport,
0936             void *hosthandle, struct nvmefc_ls_req *lsreq)
0937 {
0938 }
0939 
0940 static void
0941 fcloop_fcp_abort(struct nvme_fc_local_port *localport,
0942             struct nvme_fc_remote_port *remoteport,
0943             void *hw_queue_handle,
0944             struct nvmefc_fcp_req *fcpreq)
0945 {
0946     struct fcloop_ini_fcpreq *inireq = fcpreq->private;
0947     struct fcloop_fcpreq *tfcp_req;
0948     bool abortio = true;
0949 
0950     spin_lock(&inireq->inilock);
0951     tfcp_req = inireq->tfcp_req;
0952     if (tfcp_req)
0953         fcloop_tfcp_req_get(tfcp_req);
0954     spin_unlock(&inireq->inilock);
0955 
0956     if (!tfcp_req)
0957         /* abort has already been called */
0958         return;
0959 
0960     /* break initiator/target relationship for io */
0961     spin_lock_irq(&tfcp_req->reqlock);
0962     switch (tfcp_req->inistate) {
0963     case INI_IO_START:
0964     case INI_IO_ACTIVE:
0965         tfcp_req->inistate = INI_IO_ABORTED;
0966         break;
0967     case INI_IO_COMPLETED:
0968         abortio = false;
0969         break;
0970     default:
0971         spin_unlock_irq(&tfcp_req->reqlock);
0972         WARN_ON(1);
0973         return;
0974     }
0975     spin_unlock_irq(&tfcp_req->reqlock);
0976 
0977     if (abortio)
0978         /* leave the reference while the work item is scheduled */
0979         WARN_ON(!queue_work(nvmet_wq, &tfcp_req->abort_rcv_work));
0980     else  {
0981         /*
0982          * as the io has already had the done callback made,
0983          * nothing more to do. So release the reference taken above
0984          */
0985         fcloop_tfcp_req_put(tfcp_req);
0986     }
0987 }
0988 
0989 static void
0990 fcloop_nport_free(struct kref *ref)
0991 {
0992     struct fcloop_nport *nport =
0993         container_of(ref, struct fcloop_nport, ref);
0994     unsigned long flags;
0995 
0996     spin_lock_irqsave(&fcloop_lock, flags);
0997     list_del(&nport->nport_list);
0998     spin_unlock_irqrestore(&fcloop_lock, flags);
0999 
1000     kfree(nport);
1001 }
1002 
1003 static void
1004 fcloop_nport_put(struct fcloop_nport *nport)
1005 {
1006     kref_put(&nport->ref, fcloop_nport_free);
1007 }
1008 
1009 static int
1010 fcloop_nport_get(struct fcloop_nport *nport)
1011 {
1012     return kref_get_unless_zero(&nport->ref);
1013 }
1014 
1015 static void
1016 fcloop_localport_delete(struct nvme_fc_local_port *localport)
1017 {
1018     struct fcloop_lport_priv *lport_priv = localport->private;
1019     struct fcloop_lport *lport = lport_priv->lport;
1020 
1021     /* release any threads waiting for the unreg to complete */
1022     complete(&lport->unreg_done);
1023 }
1024 
1025 static void
1026 fcloop_remoteport_delete(struct nvme_fc_remote_port *remoteport)
1027 {
1028     struct fcloop_rport *rport = remoteport->private;
1029 
1030     flush_work(&rport->ls_work);
1031     fcloop_nport_put(rport->nport);
1032 }
1033 
1034 static void
1035 fcloop_targetport_delete(struct nvmet_fc_target_port *targetport)
1036 {
1037     struct fcloop_tport *tport = targetport->private;
1038 
1039     flush_work(&tport->ls_work);
1040     fcloop_nport_put(tport->nport);
1041 }
1042 
1043 #define FCLOOP_HW_QUEUES        4
1044 #define FCLOOP_SGL_SEGS         256
1045 #define FCLOOP_DMABOUND_4G      0xFFFFFFFF
1046 
1047 static struct nvme_fc_port_template fctemplate = {
1048     .localport_delete   = fcloop_localport_delete,
1049     .remoteport_delete  = fcloop_remoteport_delete,
1050     .create_queue       = fcloop_create_queue,
1051     .delete_queue       = fcloop_delete_queue,
1052     .ls_req         = fcloop_h2t_ls_req,
1053     .fcp_io         = fcloop_fcp_req,
1054     .ls_abort       = fcloop_h2t_ls_abort,
1055     .fcp_abort      = fcloop_fcp_abort,
1056     .xmt_ls_rsp     = fcloop_t2h_xmt_ls_rsp,
1057     .max_hw_queues      = FCLOOP_HW_QUEUES,
1058     .max_sgl_segments   = FCLOOP_SGL_SEGS,
1059     .max_dif_sgl_segments   = FCLOOP_SGL_SEGS,
1060     .dma_boundary       = FCLOOP_DMABOUND_4G,
1061     /* sizes of additional private data for data structures */
1062     .local_priv_sz      = sizeof(struct fcloop_lport_priv),
1063     .remote_priv_sz     = sizeof(struct fcloop_rport),
1064     .lsrqst_priv_sz     = sizeof(struct fcloop_lsreq),
1065     .fcprqst_priv_sz    = sizeof(struct fcloop_ini_fcpreq),
1066 };
1067 
1068 static struct nvmet_fc_target_template tgttemplate = {
1069     .targetport_delete  = fcloop_targetport_delete,
1070     .xmt_ls_rsp     = fcloop_h2t_xmt_ls_rsp,
1071     .fcp_op         = fcloop_fcp_op,
1072     .fcp_abort      = fcloop_tgt_fcp_abort,
1073     .fcp_req_release    = fcloop_fcp_req_release,
1074     .discovery_event    = fcloop_tgt_discovery_evt,
1075     .ls_req         = fcloop_t2h_ls_req,
1076     .ls_abort       = fcloop_t2h_ls_abort,
1077     .host_release       = fcloop_t2h_host_release,
1078     .max_hw_queues      = FCLOOP_HW_QUEUES,
1079     .max_sgl_segments   = FCLOOP_SGL_SEGS,
1080     .max_dif_sgl_segments   = FCLOOP_SGL_SEGS,
1081     .dma_boundary       = FCLOOP_DMABOUND_4G,
1082     /* optional features */
1083     .target_features    = 0,
1084     /* sizes of additional private data for data structures */
1085     .target_priv_sz     = sizeof(struct fcloop_tport),
1086     .lsrqst_priv_sz     = sizeof(struct fcloop_lsreq),
1087 };
1088 
1089 static ssize_t
1090 fcloop_create_local_port(struct device *dev, struct device_attribute *attr,
1091         const char *buf, size_t count)
1092 {
1093     struct nvme_fc_port_info pinfo;
1094     struct fcloop_ctrl_options *opts;
1095     struct nvme_fc_local_port *localport;
1096     struct fcloop_lport *lport;
1097     struct fcloop_lport_priv *lport_priv;
1098     unsigned long flags;
1099     int ret = -ENOMEM;
1100 
1101     lport = kzalloc(sizeof(*lport), GFP_KERNEL);
1102     if (!lport)
1103         return -ENOMEM;
1104 
1105     opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1106     if (!opts)
1107         goto out_free_lport;
1108 
1109     ret = fcloop_parse_options(opts, buf);
1110     if (ret)
1111         goto out_free_opts;
1112 
1113     /* everything there ? */
1114     if ((opts->mask & LPORT_OPTS) != LPORT_OPTS) {
1115         ret = -EINVAL;
1116         goto out_free_opts;
1117     }
1118 
1119     memset(&pinfo, 0, sizeof(pinfo));
1120     pinfo.node_name = opts->wwnn;
1121     pinfo.port_name = opts->wwpn;
1122     pinfo.port_role = opts->roles;
1123     pinfo.port_id = opts->fcaddr;
1124 
1125     ret = nvme_fc_register_localport(&pinfo, &fctemplate, NULL, &localport);
1126     if (!ret) {
1127         /* success */
1128         lport_priv = localport->private;
1129         lport_priv->lport = lport;
1130 
1131         lport->localport = localport;
1132         INIT_LIST_HEAD(&lport->lport_list);
1133 
1134         spin_lock_irqsave(&fcloop_lock, flags);
1135         list_add_tail(&lport->lport_list, &fcloop_lports);
1136         spin_unlock_irqrestore(&fcloop_lock, flags);
1137     }
1138 
1139 out_free_opts:
1140     kfree(opts);
1141 out_free_lport:
1142     /* free only if we're going to fail */
1143     if (ret)
1144         kfree(lport);
1145 
1146     return ret ? ret : count;
1147 }
1148 
1149 
1150 static void
1151 __unlink_local_port(struct fcloop_lport *lport)
1152 {
1153     list_del(&lport->lport_list);
1154 }
1155 
1156 static int
1157 __wait_localport_unreg(struct fcloop_lport *lport)
1158 {
1159     int ret;
1160 
1161     init_completion(&lport->unreg_done);
1162 
1163     ret = nvme_fc_unregister_localport(lport->localport);
1164 
1165     wait_for_completion(&lport->unreg_done);
1166 
1167     kfree(lport);
1168 
1169     return ret;
1170 }
1171 
1172 
1173 static ssize_t
1174 fcloop_delete_local_port(struct device *dev, struct device_attribute *attr,
1175         const char *buf, size_t count)
1176 {
1177     struct fcloop_lport *tlport, *lport = NULL;
1178     u64 nodename, portname;
1179     unsigned long flags;
1180     int ret;
1181 
1182     ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
1183     if (ret)
1184         return ret;
1185 
1186     spin_lock_irqsave(&fcloop_lock, flags);
1187 
1188     list_for_each_entry(tlport, &fcloop_lports, lport_list) {
1189         if (tlport->localport->node_name == nodename &&
1190             tlport->localport->port_name == portname) {
1191             lport = tlport;
1192             __unlink_local_port(lport);
1193             break;
1194         }
1195     }
1196     spin_unlock_irqrestore(&fcloop_lock, flags);
1197 
1198     if (!lport)
1199         return -ENOENT;
1200 
1201     ret = __wait_localport_unreg(lport);
1202 
1203     return ret ? ret : count;
1204 }
1205 
1206 static struct fcloop_nport *
1207 fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
1208 {
1209     struct fcloop_nport *newnport, *nport = NULL;
1210     struct fcloop_lport *tmplport, *lport = NULL;
1211     struct fcloop_ctrl_options *opts;
1212     unsigned long flags;
1213     u32 opts_mask = (remoteport) ? RPORT_OPTS : TGTPORT_OPTS;
1214     int ret;
1215 
1216     opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1217     if (!opts)
1218         return NULL;
1219 
1220     ret = fcloop_parse_options(opts, buf);
1221     if (ret)
1222         goto out_free_opts;
1223 
1224     /* everything there ? */
1225     if ((opts->mask & opts_mask) != opts_mask) {
1226         ret = -EINVAL;
1227         goto out_free_opts;
1228     }
1229 
1230     newnport = kzalloc(sizeof(*newnport), GFP_KERNEL);
1231     if (!newnport)
1232         goto out_free_opts;
1233 
1234     INIT_LIST_HEAD(&newnport->nport_list);
1235     newnport->node_name = opts->wwnn;
1236     newnport->port_name = opts->wwpn;
1237     if (opts->mask & NVMF_OPT_ROLES)
1238         newnport->port_role = opts->roles;
1239     if (opts->mask & NVMF_OPT_FCADDR)
1240         newnport->port_id = opts->fcaddr;
1241     kref_init(&newnport->ref);
1242 
1243     spin_lock_irqsave(&fcloop_lock, flags);
1244 
1245     list_for_each_entry(tmplport, &fcloop_lports, lport_list) {
1246         if (tmplport->localport->node_name == opts->wwnn &&
1247             tmplport->localport->port_name == opts->wwpn)
1248             goto out_invalid_opts;
1249 
1250         if (tmplport->localport->node_name == opts->lpwwnn &&
1251             tmplport->localport->port_name == opts->lpwwpn)
1252             lport = tmplport;
1253     }
1254 
1255     if (remoteport) {
1256         if (!lport)
1257             goto out_invalid_opts;
1258         newnport->lport = lport;
1259     }
1260 
1261     list_for_each_entry(nport, &fcloop_nports, nport_list) {
1262         if (nport->node_name == opts->wwnn &&
1263             nport->port_name == opts->wwpn) {
1264             if ((remoteport && nport->rport) ||
1265                 (!remoteport && nport->tport)) {
1266                 nport = NULL;
1267                 goto out_invalid_opts;
1268             }
1269 
1270             fcloop_nport_get(nport);
1271 
1272             spin_unlock_irqrestore(&fcloop_lock, flags);
1273 
1274             if (remoteport)
1275                 nport->lport = lport;
1276             if (opts->mask & NVMF_OPT_ROLES)
1277                 nport->port_role = opts->roles;
1278             if (opts->mask & NVMF_OPT_FCADDR)
1279                 nport->port_id = opts->fcaddr;
1280             goto out_free_newnport;
1281         }
1282     }
1283 
1284     list_add_tail(&newnport->nport_list, &fcloop_nports);
1285 
1286     spin_unlock_irqrestore(&fcloop_lock, flags);
1287 
1288     kfree(opts);
1289     return newnport;
1290 
1291 out_invalid_opts:
1292     spin_unlock_irqrestore(&fcloop_lock, flags);
1293 out_free_newnport:
1294     kfree(newnport);
1295 out_free_opts:
1296     kfree(opts);
1297     return nport;
1298 }
1299 
1300 static ssize_t
1301 fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
1302         const char *buf, size_t count)
1303 {
1304     struct nvme_fc_remote_port *remoteport;
1305     struct fcloop_nport *nport;
1306     struct fcloop_rport *rport;
1307     struct nvme_fc_port_info pinfo;
1308     int ret;
1309 
1310     nport = fcloop_alloc_nport(buf, count, true);
1311     if (!nport)
1312         return -EIO;
1313 
1314     memset(&pinfo, 0, sizeof(pinfo));
1315     pinfo.node_name = nport->node_name;
1316     pinfo.port_name = nport->port_name;
1317     pinfo.port_role = nport->port_role;
1318     pinfo.port_id = nport->port_id;
1319 
1320     ret = nvme_fc_register_remoteport(nport->lport->localport,
1321                         &pinfo, &remoteport);
1322     if (ret || !remoteport) {
1323         fcloop_nport_put(nport);
1324         return ret;
1325     }
1326 
1327     /* success */
1328     rport = remoteport->private;
1329     rport->remoteport = remoteport;
1330     rport->targetport = (nport->tport) ?  nport->tport->targetport : NULL;
1331     if (nport->tport) {
1332         nport->tport->remoteport = remoteport;
1333         nport->tport->lport = nport->lport;
1334     }
1335     rport->nport = nport;
1336     rport->lport = nport->lport;
1337     nport->rport = rport;
1338     spin_lock_init(&rport->lock);
1339     INIT_WORK(&rport->ls_work, fcloop_rport_lsrqst_work);
1340     INIT_LIST_HEAD(&rport->ls_list);
1341 
1342     return count;
1343 }
1344 
1345 
1346 static struct fcloop_rport *
1347 __unlink_remote_port(struct fcloop_nport *nport)
1348 {
1349     struct fcloop_rport *rport = nport->rport;
1350 
1351     if (rport && nport->tport)
1352         nport->tport->remoteport = NULL;
1353     nport->rport = NULL;
1354 
1355     return rport;
1356 }
1357 
1358 static int
1359 __remoteport_unreg(struct fcloop_nport *nport, struct fcloop_rport *rport)
1360 {
1361     if (!rport)
1362         return -EALREADY;
1363 
1364     return nvme_fc_unregister_remoteport(rport->remoteport);
1365 }
1366 
1367 static ssize_t
1368 fcloop_delete_remote_port(struct device *dev, struct device_attribute *attr,
1369         const char *buf, size_t count)
1370 {
1371     struct fcloop_nport *nport = NULL, *tmpport;
1372     static struct fcloop_rport *rport;
1373     u64 nodename, portname;
1374     unsigned long flags;
1375     int ret;
1376 
1377     ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
1378     if (ret)
1379         return ret;
1380 
1381     spin_lock_irqsave(&fcloop_lock, flags);
1382 
1383     list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
1384         if (tmpport->node_name == nodename &&
1385             tmpport->port_name == portname && tmpport->rport) {
1386             nport = tmpport;
1387             rport = __unlink_remote_port(nport);
1388             break;
1389         }
1390     }
1391 
1392     spin_unlock_irqrestore(&fcloop_lock, flags);
1393 
1394     if (!nport)
1395         return -ENOENT;
1396 
1397     ret = __remoteport_unreg(nport, rport);
1398 
1399     return ret ? ret : count;
1400 }
1401 
1402 static ssize_t
1403 fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
1404         const char *buf, size_t count)
1405 {
1406     struct nvmet_fc_target_port *targetport;
1407     struct fcloop_nport *nport;
1408     struct fcloop_tport *tport;
1409     struct nvmet_fc_port_info tinfo;
1410     int ret;
1411 
1412     nport = fcloop_alloc_nport(buf, count, false);
1413     if (!nport)
1414         return -EIO;
1415 
1416     tinfo.node_name = nport->node_name;
1417     tinfo.port_name = nport->port_name;
1418     tinfo.port_id = nport->port_id;
1419 
1420     ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL,
1421                         &targetport);
1422     if (ret) {
1423         fcloop_nport_put(nport);
1424         return ret;
1425     }
1426 
1427     /* success */
1428     tport = targetport->private;
1429     tport->targetport = targetport;
1430     tport->remoteport = (nport->rport) ?  nport->rport->remoteport : NULL;
1431     if (nport->rport)
1432         nport->rport->targetport = targetport;
1433     tport->nport = nport;
1434     tport->lport = nport->lport;
1435     nport->tport = tport;
1436     spin_lock_init(&tport->lock);
1437     INIT_WORK(&tport->ls_work, fcloop_tport_lsrqst_work);
1438     INIT_LIST_HEAD(&tport->ls_list);
1439 
1440     return count;
1441 }
1442 
1443 
1444 static struct fcloop_tport *
1445 __unlink_target_port(struct fcloop_nport *nport)
1446 {
1447     struct fcloop_tport *tport = nport->tport;
1448 
1449     if (tport && nport->rport)
1450         nport->rport->targetport = NULL;
1451     nport->tport = NULL;
1452 
1453     return tport;
1454 }
1455 
1456 static int
1457 __targetport_unreg(struct fcloop_nport *nport, struct fcloop_tport *tport)
1458 {
1459     if (!tport)
1460         return -EALREADY;
1461 
1462     return nvmet_fc_unregister_targetport(tport->targetport);
1463 }
1464 
1465 static ssize_t
1466 fcloop_delete_target_port(struct device *dev, struct device_attribute *attr,
1467         const char *buf, size_t count)
1468 {
1469     struct fcloop_nport *nport = NULL, *tmpport;
1470     struct fcloop_tport *tport = NULL;
1471     u64 nodename, portname;
1472     unsigned long flags;
1473     int ret;
1474 
1475     ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
1476     if (ret)
1477         return ret;
1478 
1479     spin_lock_irqsave(&fcloop_lock, flags);
1480 
1481     list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
1482         if (tmpport->node_name == nodename &&
1483             tmpport->port_name == portname && tmpport->tport) {
1484             nport = tmpport;
1485             tport = __unlink_target_port(nport);
1486             break;
1487         }
1488     }
1489 
1490     spin_unlock_irqrestore(&fcloop_lock, flags);
1491 
1492     if (!nport)
1493         return -ENOENT;
1494 
1495     ret = __targetport_unreg(nport, tport);
1496 
1497     return ret ? ret : count;
1498 }
1499 
1500 static ssize_t
1501 fcloop_set_cmd_drop(struct device *dev, struct device_attribute *attr,
1502         const char *buf, size_t count)
1503 {
1504     unsigned int opcode;
1505     int starting, amount;
1506 
1507     if (sscanf(buf, "%x:%d:%d", &opcode, &starting, &amount) != 3)
1508         return -EBADRQC;
1509 
1510     drop_current_cnt = 0;
1511     drop_fabric_opcode = (opcode & ~DROP_OPCODE_MASK) ? true : false;
1512     drop_opcode = (opcode & DROP_OPCODE_MASK);
1513     drop_instance = starting;
1514     /* the check to drop routine uses instance + count to know when
1515      * to end. Thus, if dropping 1 instance, count should be 0.
1516      * so subtract 1 from the count.
1517      */
1518     drop_amount = amount - 1;
1519 
1520     pr_info("%s: DROP: Starting at instance %d of%s opcode x%x drop +%d "
1521         "instances\n",
1522         __func__, drop_instance, drop_fabric_opcode ? " fabric" : "",
1523         drop_opcode, drop_amount);
1524 
1525     return count;
1526 }
1527 
1528 
1529 static DEVICE_ATTR(add_local_port, 0200, NULL, fcloop_create_local_port);
1530 static DEVICE_ATTR(del_local_port, 0200, NULL, fcloop_delete_local_port);
1531 static DEVICE_ATTR(add_remote_port, 0200, NULL, fcloop_create_remote_port);
1532 static DEVICE_ATTR(del_remote_port, 0200, NULL, fcloop_delete_remote_port);
1533 static DEVICE_ATTR(add_target_port, 0200, NULL, fcloop_create_target_port);
1534 static DEVICE_ATTR(del_target_port, 0200, NULL, fcloop_delete_target_port);
1535 static DEVICE_ATTR(set_cmd_drop, 0200, NULL, fcloop_set_cmd_drop);
1536 
1537 static struct attribute *fcloop_dev_attrs[] = {
1538     &dev_attr_add_local_port.attr,
1539     &dev_attr_del_local_port.attr,
1540     &dev_attr_add_remote_port.attr,
1541     &dev_attr_del_remote_port.attr,
1542     &dev_attr_add_target_port.attr,
1543     &dev_attr_del_target_port.attr,
1544     &dev_attr_set_cmd_drop.attr,
1545     NULL
1546 };
1547 
1548 static const struct attribute_group fclopp_dev_attrs_group = {
1549     .attrs      = fcloop_dev_attrs,
1550 };
1551 
1552 static const struct attribute_group *fcloop_dev_attr_groups[] = {
1553     &fclopp_dev_attrs_group,
1554     NULL,
1555 };
1556 
1557 static struct class *fcloop_class;
1558 static struct device *fcloop_device;
1559 
1560 
1561 static int __init fcloop_init(void)
1562 {
1563     int ret;
1564 
1565     fcloop_class = class_create(THIS_MODULE, "fcloop");
1566     if (IS_ERR(fcloop_class)) {
1567         pr_err("couldn't register class fcloop\n");
1568         ret = PTR_ERR(fcloop_class);
1569         return ret;
1570     }
1571 
1572     fcloop_device = device_create_with_groups(
1573                 fcloop_class, NULL, MKDEV(0, 0), NULL,
1574                 fcloop_dev_attr_groups, "ctl");
1575     if (IS_ERR(fcloop_device)) {
1576         pr_err("couldn't create ctl device!\n");
1577         ret = PTR_ERR(fcloop_device);
1578         goto out_destroy_class;
1579     }
1580 
1581     get_device(fcloop_device);
1582 
1583     return 0;
1584 
1585 out_destroy_class:
1586     class_destroy(fcloop_class);
1587     return ret;
1588 }
1589 
1590 static void __exit fcloop_exit(void)
1591 {
1592     struct fcloop_lport *lport = NULL;
1593     struct fcloop_nport *nport = NULL;
1594     struct fcloop_tport *tport;
1595     struct fcloop_rport *rport;
1596     unsigned long flags;
1597     int ret;
1598 
1599     spin_lock_irqsave(&fcloop_lock, flags);
1600 
1601     for (;;) {
1602         nport = list_first_entry_or_null(&fcloop_nports,
1603                         typeof(*nport), nport_list);
1604         if (!nport)
1605             break;
1606 
1607         tport = __unlink_target_port(nport);
1608         rport = __unlink_remote_port(nport);
1609 
1610         spin_unlock_irqrestore(&fcloop_lock, flags);
1611 
1612         ret = __targetport_unreg(nport, tport);
1613         if (ret)
1614             pr_warn("%s: Failed deleting target port\n", __func__);
1615 
1616         ret = __remoteport_unreg(nport, rport);
1617         if (ret)
1618             pr_warn("%s: Failed deleting remote port\n", __func__);
1619 
1620         spin_lock_irqsave(&fcloop_lock, flags);
1621     }
1622 
1623     for (;;) {
1624         lport = list_first_entry_or_null(&fcloop_lports,
1625                         typeof(*lport), lport_list);
1626         if (!lport)
1627             break;
1628 
1629         __unlink_local_port(lport);
1630 
1631         spin_unlock_irqrestore(&fcloop_lock, flags);
1632 
1633         ret = __wait_localport_unreg(lport);
1634         if (ret)
1635             pr_warn("%s: Failed deleting local port\n", __func__);
1636 
1637         spin_lock_irqsave(&fcloop_lock, flags);
1638     }
1639 
1640     spin_unlock_irqrestore(&fcloop_lock, flags);
1641 
1642     put_device(fcloop_device);
1643 
1644     device_destroy(fcloop_class, MKDEV(0, 0));
1645     class_destroy(fcloop_class);
1646 }
1647 
1648 module_init(fcloop_init);
1649 module_exit(fcloop_exit);
1650 
1651 MODULE_LICENSE("GPL v2");