Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2021 Broadcom. All Rights Reserved. The term
0004  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
0005  */
0006 
0007 #include <target/target_core_base.h>
0008 #include <target/target_core_fabric.h>
0009 #include "efct_driver.h"
0010 #include "efct_lio.h"
0011 
0012 /*
0013  * lio_wq is used to call the LIO backed during creation or deletion of
0014  * sessions. This brings serialization to the session management as we create
0015  * single threaded work queue.
0016  */
0017 static struct workqueue_struct *lio_wq;
0018 
0019 static int
0020 efct_format_wwn(char *str, size_t len, const char *pre, u64 wwn)
0021 {
0022     u8 a[8];
0023 
0024     put_unaligned_be64(wwn, a);
0025     return snprintf(str, len, "%s%8phC", pre, a);
0026 }
0027 
0028 static int
0029 efct_lio_parse_wwn(const char *name, u64 *wwp, u8 npiv)
0030 {
0031     int num;
0032     u8 b[8];
0033 
0034     if (npiv) {
0035         num = sscanf(name,
0036                  "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
0037                  &b[0], &b[1], &b[2], &b[3], &b[4], &b[5], &b[6],
0038                  &b[7]);
0039     } else {
0040         num = sscanf(name,
0041               "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
0042                  &b[0], &b[1], &b[2], &b[3], &b[4], &b[5], &b[6],
0043                  &b[7]);
0044     }
0045 
0046     if (num != 8)
0047         return -EINVAL;
0048 
0049     *wwp = get_unaligned_be64(b);
0050     return 0;
0051 }
0052 
0053 static int
0054 efct_lio_parse_npiv_wwn(const char *name, size_t size, u64 *wwpn, u64 *wwnn)
0055 {
0056     unsigned int cnt = size;
0057     int rc;
0058 
0059     *wwpn = *wwnn = 0;
0060     if (name[cnt - 1] == '\n' || name[cnt - 1] == 0)
0061         cnt--;
0062 
0063     /* validate we have enough characters for WWPN */
0064     if ((cnt != (16 + 1 + 16)) || (name[16] != ':'))
0065         return -EINVAL;
0066 
0067     rc = efct_lio_parse_wwn(&name[0], wwpn, 1);
0068     if (rc)
0069         return rc;
0070 
0071     rc = efct_lio_parse_wwn(&name[17], wwnn, 1);
0072     if (rc)
0073         return rc;
0074 
0075     return 0;
0076 }
0077 
0078 static ssize_t
0079 efct_lio_tpg_enable_show(struct config_item *item, char *page)
0080 {
0081     struct se_portal_group *se_tpg = to_tpg(item);
0082     struct efct_lio_tpg *tpg =
0083         container_of(se_tpg, struct efct_lio_tpg, tpg);
0084 
0085     return snprintf(page, PAGE_SIZE, "%d\n", tpg->enabled);
0086 }
0087 
0088 static ssize_t
0089 efct_lio_tpg_enable_store(struct config_item *item, const char *page,
0090               size_t count)
0091 {
0092     struct se_portal_group *se_tpg = to_tpg(item);
0093     struct efct_lio_tpg *tpg =
0094         container_of(se_tpg, struct efct_lio_tpg, tpg);
0095     struct efct *efct;
0096     struct efc *efc;
0097     unsigned long op;
0098 
0099     if (!tpg->nport || !tpg->nport->efct) {
0100         pr_err("%s: Unable to find EFCT device\n", __func__);
0101         return -EINVAL;
0102     }
0103 
0104     efct = tpg->nport->efct;
0105     efc = efct->efcport;
0106 
0107     if (kstrtoul(page, 0, &op) < 0)
0108         return -EINVAL;
0109 
0110     if (op == 1) {
0111         int ret;
0112 
0113         tpg->enabled = true;
0114         efc_log_debug(efct, "enable portal group %d\n", tpg->tpgt);
0115 
0116         ret = efct_xport_control(efct->xport, EFCT_XPORT_PORT_ONLINE);
0117         if (ret) {
0118             efct->tgt_efct.lio_nport = NULL;
0119             efc_log_debug(efct, "cannot bring port online\n");
0120             return ret;
0121         }
0122     } else if (op == 0) {
0123         efc_log_debug(efct, "disable portal group %d\n", tpg->tpgt);
0124 
0125         if (efc->domain && efc->domain->nport)
0126             efct_scsi_tgt_del_nport(efc, efc->domain->nport);
0127 
0128         tpg->enabled = false;
0129     } else {
0130         return -EINVAL;
0131     }
0132 
0133     return count;
0134 }
0135 
0136 static ssize_t
0137 efct_lio_npiv_tpg_enable_show(struct config_item *item, char *page)
0138 {
0139     struct se_portal_group *se_tpg = to_tpg(item);
0140     struct efct_lio_tpg *tpg =
0141         container_of(se_tpg, struct efct_lio_tpg, tpg);
0142 
0143     return snprintf(page, PAGE_SIZE, "%d\n", tpg->enabled);
0144 }
0145 
0146 static ssize_t
0147 efct_lio_npiv_tpg_enable_store(struct config_item *item, const char *page,
0148                    size_t count)
0149 {
0150     struct se_portal_group *se_tpg = to_tpg(item);
0151     struct efct_lio_tpg *tpg =
0152         container_of(se_tpg, struct efct_lio_tpg, tpg);
0153     struct efct_lio_vport *lio_vport = tpg->vport;
0154     struct efct *efct;
0155     struct efc *efc;
0156     unsigned long op;
0157 
0158     if (kstrtoul(page, 0, &op) < 0)
0159         return -EINVAL;
0160 
0161     if (!lio_vport) {
0162         pr_err("Unable to find vport\n");
0163         return -EINVAL;
0164     }
0165 
0166     efct = lio_vport->efct;
0167     efc = efct->efcport;
0168 
0169     if (op == 1) {
0170         tpg->enabled = true;
0171         efc_log_debug(efct, "enable portal group %d\n", tpg->tpgt);
0172 
0173         if (efc->domain) {
0174             int ret;
0175 
0176             ret = efc_nport_vport_new(efc->domain,
0177                           lio_vport->npiv_wwpn,
0178                           lio_vport->npiv_wwnn,
0179                           U32_MAX, false, true,
0180                           NULL, NULL);
0181             if (ret != 0) {
0182                 efc_log_err(efct, "Failed to create Vport\n");
0183                 return ret;
0184             }
0185             return count;
0186         }
0187 
0188         if (!(efc_vport_create_spec(efc, lio_vport->npiv_wwnn,
0189                         lio_vport->npiv_wwpn, U32_MAX,
0190                         false, true, NULL, NULL)))
0191             return -ENOMEM;
0192 
0193     } else if (op == 0) {
0194         efc_log_debug(efct, "disable portal group %d\n", tpg->tpgt);
0195 
0196         tpg->enabled = false;
0197         /* only physical nport should exist, free lio_nport
0198          * allocated in efct_lio_make_nport
0199          */
0200         if (efc->domain) {
0201             efc_nport_vport_del(efct->efcport, efc->domain,
0202                         lio_vport->npiv_wwpn,
0203                         lio_vport->npiv_wwnn);
0204             return count;
0205         }
0206     } else {
0207         return -EINVAL;
0208     }
0209     return count;
0210 }
0211 
0212 static char *efct_lio_get_fabric_wwn(struct se_portal_group *se_tpg)
0213 {
0214     struct efct_lio_tpg *tpg =
0215         container_of(se_tpg, struct efct_lio_tpg, tpg);
0216 
0217     return tpg->nport->wwpn_str;
0218 }
0219 
0220 static char *efct_lio_get_npiv_fabric_wwn(struct se_portal_group *se_tpg)
0221 {
0222     struct efct_lio_tpg *tpg =
0223         container_of(se_tpg, struct efct_lio_tpg, tpg);
0224 
0225     return tpg->vport->wwpn_str;
0226 }
0227 
0228 static u16 efct_lio_get_tag(struct se_portal_group *se_tpg)
0229 {
0230     struct efct_lio_tpg *tpg =
0231         container_of(se_tpg, struct efct_lio_tpg, tpg);
0232 
0233     return tpg->tpgt;
0234 }
0235 
0236 static u16 efct_lio_get_npiv_tag(struct se_portal_group *se_tpg)
0237 {
0238     struct efct_lio_tpg *tpg =
0239         container_of(se_tpg, struct efct_lio_tpg, tpg);
0240 
0241     return tpg->tpgt;
0242 }
0243 
0244 static int efct_lio_check_demo_mode(struct se_portal_group *se_tpg)
0245 {
0246     return 1;
0247 }
0248 
0249 static int efct_lio_check_demo_mode_cache(struct se_portal_group *se_tpg)
0250 {
0251     return 1;
0252 }
0253 
0254 static int efct_lio_check_demo_write_protect(struct se_portal_group *se_tpg)
0255 {
0256     struct efct_lio_tpg *tpg =
0257         container_of(se_tpg, struct efct_lio_tpg, tpg);
0258 
0259     return tpg->tpg_attrib.demo_mode_write_protect;
0260 }
0261 
0262 static int
0263 efct_lio_npiv_check_demo_write_protect(struct se_portal_group *se_tpg)
0264 {
0265     struct efct_lio_tpg *tpg =
0266         container_of(se_tpg, struct efct_lio_tpg, tpg);
0267 
0268     return tpg->tpg_attrib.demo_mode_write_protect;
0269 }
0270 
0271 static int efct_lio_check_prod_write_protect(struct se_portal_group *se_tpg)
0272 {
0273     struct efct_lio_tpg *tpg =
0274         container_of(se_tpg, struct efct_lio_tpg, tpg);
0275 
0276     return tpg->tpg_attrib.prod_mode_write_protect;
0277 }
0278 
0279 static int
0280 efct_lio_npiv_check_prod_write_protect(struct se_portal_group *se_tpg)
0281 {
0282     struct efct_lio_tpg *tpg =
0283         container_of(se_tpg, struct efct_lio_tpg, tpg);
0284 
0285     return tpg->tpg_attrib.prod_mode_write_protect;
0286 }
0287 
0288 static u32 efct_lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
0289 {
0290     return 1;
0291 }
0292 
0293 static int efct_lio_check_stop_free(struct se_cmd *se_cmd)
0294 {
0295     struct efct_scsi_tgt_io *ocp =
0296         container_of(se_cmd, struct efct_scsi_tgt_io, cmd);
0297     struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
0298 
0299     efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_CHK_STOP_FREE);
0300     return target_put_sess_cmd(se_cmd);
0301 }
0302 
0303 static int
0304 efct_lio_abort_tgt_cb(struct efct_io *io,
0305               enum efct_scsi_io_status scsi_status,
0306               u32 flags, void *arg)
0307 {
0308     efct_lio_io_printf(io, "Abort done, status:%d\n", scsi_status);
0309     return 0;
0310 }
0311 
0312 static void
0313 efct_lio_aborted_task(struct se_cmd *se_cmd)
0314 {
0315     struct efct_scsi_tgt_io *ocp =
0316         container_of(se_cmd, struct efct_scsi_tgt_io, cmd);
0317     struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
0318 
0319     efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_ABORTED_TASK);
0320 
0321     if (ocp->rsp_sent)
0322         return;
0323 
0324     /* command has been aborted, cleanup here */
0325     ocp->aborting = true;
0326     ocp->err = EFCT_SCSI_STATUS_ABORTED;
0327     /* terminate the exchange */
0328     efct_scsi_tgt_abort_io(io, efct_lio_abort_tgt_cb, NULL);
0329 }
0330 
0331 static void efct_lio_release_cmd(struct se_cmd *se_cmd)
0332 {
0333     struct efct_scsi_tgt_io *ocp =
0334         container_of(se_cmd, struct efct_scsi_tgt_io, cmd);
0335     struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
0336     struct efct *efct = io->efct;
0337 
0338     efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_RELEASE_CMD);
0339     efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_CMPL_CMD);
0340     efct_scsi_io_complete(io);
0341     atomic_sub_return(1, &efct->tgt_efct.ios_in_use);
0342 }
0343 
0344 static void efct_lio_close_session(struct se_session *se_sess)
0345 {
0346     struct efc_node *node = se_sess->fabric_sess_ptr;
0347 
0348     pr_debug("se_sess=%p node=%p", se_sess, node);
0349 
0350     if (!node) {
0351         pr_debug("node is NULL");
0352         return;
0353     }
0354 
0355     efc_node_post_shutdown(node, NULL);
0356 }
0357 
0358 static u32 efct_lio_sess_get_index(struct se_session *se_sess)
0359 {
0360     return 0;
0361 }
0362 
0363 static void efct_lio_set_default_node_attrs(struct se_node_acl *nacl)
0364 {
0365 }
0366 
0367 static int efct_lio_get_cmd_state(struct se_cmd *cmd)
0368 {
0369     struct efct_scsi_tgt_io *ocp =
0370         container_of(cmd, struct efct_scsi_tgt_io, cmd);
0371     struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
0372 
0373     return io->tgt_io.state;
0374 }
0375 
0376 static int
0377 efct_lio_sg_map(struct efct_io *io)
0378 {
0379     struct efct_scsi_tgt_io *ocp = &io->tgt_io;
0380     struct se_cmd *cmd = &ocp->cmd;
0381 
0382     ocp->seg_map_cnt = dma_map_sg(&io->efct->pci->dev, cmd->t_data_sg,
0383                       cmd->t_data_nents, cmd->data_direction);
0384     if (ocp->seg_map_cnt == 0)
0385         return -EFAULT;
0386     return 0;
0387 }
0388 
0389 static void
0390 efct_lio_sg_unmap(struct efct_io *io)
0391 {
0392     struct efct_scsi_tgt_io *ocp = &io->tgt_io;
0393     struct se_cmd *cmd = &ocp->cmd;
0394 
0395     if (WARN_ON(!ocp->seg_map_cnt || !cmd->t_data_sg))
0396         return;
0397 
0398     dma_unmap_sg(&io->efct->pci->dev, cmd->t_data_sg,
0399              ocp->seg_map_cnt, cmd->data_direction);
0400     ocp->seg_map_cnt = 0;
0401 }
0402 
0403 static int
0404 efct_lio_status_done(struct efct_io *io,
0405              enum efct_scsi_io_status scsi_status,
0406              u32 flags, void *arg)
0407 {
0408     struct efct_scsi_tgt_io *ocp = &io->tgt_io;
0409 
0410     efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_RSP_DONE);
0411     if (scsi_status != EFCT_SCSI_STATUS_GOOD) {
0412         efct_lio_io_printf(io, "callback completed with error=%d\n",
0413                    scsi_status);
0414         ocp->err = scsi_status;
0415     }
0416     if (ocp->seg_map_cnt)
0417         efct_lio_sg_unmap(io);
0418 
0419     efct_lio_io_printf(io, "status=%d, err=%d flags=0x%x, dir=%d\n",
0420                scsi_status, ocp->err, flags, ocp->ddir);
0421 
0422     efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_GENERIC_FREE);
0423     transport_generic_free_cmd(&io->tgt_io.cmd, 0);
0424     return 0;
0425 }
0426 
0427 static int
0428 efct_lio_datamove_done(struct efct_io *io, enum efct_scsi_io_status scsi_status,
0429                u32 flags, void *arg);
0430 
0431 static int
0432 efct_lio_write_pending(struct se_cmd *cmd)
0433 {
0434     struct efct_scsi_tgt_io *ocp =
0435         container_of(cmd, struct efct_scsi_tgt_io, cmd);
0436     struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
0437     struct efct_scsi_sgl *sgl = io->sgl;
0438     struct scatterlist *sg;
0439     u32 flags = 0, cnt, curcnt;
0440     u64 length = 0;
0441 
0442     efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_WRITE_PENDING);
0443     efct_lio_io_printf(io, "trans_state=0x%x se_cmd_flags=0x%x\n",
0444                cmd->transport_state, cmd->se_cmd_flags);
0445 
0446     if (ocp->seg_cnt == 0) {
0447         ocp->seg_cnt = cmd->t_data_nents;
0448         ocp->cur_seg = 0;
0449         if (efct_lio_sg_map(io)) {
0450             efct_lio_io_printf(io, "efct_lio_sg_map failed\n");
0451             return -EFAULT;
0452         }
0453     }
0454     curcnt = (ocp->seg_map_cnt - ocp->cur_seg);
0455     curcnt = (curcnt < io->sgl_allocated) ? curcnt : io->sgl_allocated;
0456     /* find current sg */
0457     for (cnt = 0, sg = cmd->t_data_sg; cnt < ocp->cur_seg; cnt++,
0458          sg = sg_next(sg))
0459         ;/* do nothing */
0460 
0461     for (cnt = 0; cnt < curcnt; cnt++, sg = sg_next(sg)) {
0462         sgl[cnt].addr = sg_dma_address(sg);
0463         sgl[cnt].dif_addr = 0;
0464         sgl[cnt].len = sg_dma_len(sg);
0465         length += sgl[cnt].len;
0466         ocp->cur_seg++;
0467     }
0468 
0469     if (ocp->cur_seg == ocp->seg_cnt)
0470         flags = EFCT_SCSI_LAST_DATAPHASE;
0471 
0472     return efct_scsi_recv_wr_data(io, flags, sgl, curcnt, length,
0473                     efct_lio_datamove_done, NULL);
0474 }
0475 
0476 static int
0477 efct_lio_queue_data_in(struct se_cmd *cmd)
0478 {
0479     struct efct_scsi_tgt_io *ocp =
0480         container_of(cmd, struct efct_scsi_tgt_io, cmd);
0481     struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
0482     struct efct_scsi_sgl *sgl = io->sgl;
0483     struct scatterlist *sg = NULL;
0484     uint flags = 0, cnt = 0, curcnt = 0;
0485     u64 length = 0;
0486 
0487     efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_QUEUE_DATA_IN);
0488 
0489     if (ocp->seg_cnt == 0) {
0490         if (cmd->data_length) {
0491             ocp->seg_cnt = cmd->t_data_nents;
0492             ocp->cur_seg = 0;
0493             if (efct_lio_sg_map(io)) {
0494                 efct_lio_io_printf(io,
0495                            "efct_lio_sg_map failed\n");
0496                 return -EAGAIN;
0497             }
0498         } else {
0499             /* If command length is 0, send the response status */
0500             struct efct_scsi_cmd_resp rsp;
0501 
0502             memset(&rsp, 0, sizeof(rsp));
0503             efct_lio_io_printf(io,
0504                        "cmd : %p length 0, send status\n",
0505                        cmd);
0506             return efct_scsi_send_resp(io, 0, &rsp,
0507                            efct_lio_status_done, NULL);
0508         }
0509     }
0510     curcnt = min(ocp->seg_map_cnt - ocp->cur_seg, io->sgl_allocated);
0511 
0512     while (cnt < curcnt) {
0513         sg = &cmd->t_data_sg[ocp->cur_seg];
0514         sgl[cnt].addr = sg_dma_address(sg);
0515         sgl[cnt].dif_addr = 0;
0516         if (ocp->transferred_len + sg_dma_len(sg) >= cmd->data_length)
0517             sgl[cnt].len = cmd->data_length - ocp->transferred_len;
0518         else
0519             sgl[cnt].len = sg_dma_len(sg);
0520 
0521         ocp->transferred_len += sgl[cnt].len;
0522         length += sgl[cnt].len;
0523         ocp->cur_seg++;
0524         cnt++;
0525         if (ocp->transferred_len == cmd->data_length)
0526             break;
0527     }
0528 
0529     if (ocp->transferred_len == cmd->data_length) {
0530         flags = EFCT_SCSI_LAST_DATAPHASE;
0531         ocp->seg_cnt = ocp->cur_seg;
0532     }
0533 
0534     /* If there is residual, disable Auto Good Response */
0535     if (cmd->residual_count)
0536         flags |= EFCT_SCSI_NO_AUTO_RESPONSE;
0537 
0538     efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_SEND_RD_DATA);
0539 
0540     return efct_scsi_send_rd_data(io, flags, sgl, curcnt, length,
0541                     efct_lio_datamove_done, NULL);
0542 }
0543 
0544 static void
0545 efct_lio_send_resp(struct efct_io *io, enum efct_scsi_io_status scsi_status,
0546            u32 flags)
0547 {
0548     struct efct_scsi_cmd_resp rsp;
0549     struct efct_scsi_tgt_io *ocp = &io->tgt_io;
0550     struct se_cmd *cmd = &io->tgt_io.cmd;
0551     int rc;
0552 
0553     if (flags & EFCT_SCSI_IO_CMPL_RSP_SENT) {
0554         ocp->rsp_sent = true;
0555         efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_GENERIC_FREE);
0556         transport_generic_free_cmd(&io->tgt_io.cmd, 0);
0557         return;
0558     }
0559 
0560     /* send check condition if an error occurred */
0561     memset(&rsp, 0, sizeof(rsp));
0562     rsp.scsi_status = cmd->scsi_status;
0563     rsp.sense_data = (uint8_t *)io->tgt_io.sense_buffer;
0564     rsp.sense_data_length = cmd->scsi_sense_length;
0565 
0566     /* Check for residual underrun or overrun */
0567     if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT)
0568         rsp.residual = -cmd->residual_count;
0569     else if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT)
0570         rsp.residual = cmd->residual_count;
0571 
0572     rc = efct_scsi_send_resp(io, 0, &rsp, efct_lio_status_done, NULL);
0573     efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_SEND_RSP);
0574     if (rc != 0) {
0575         efct_lio_io_printf(io, "Read done, send rsp failed %d\n", rc);
0576         efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_GENERIC_FREE);
0577         transport_generic_free_cmd(&io->tgt_io.cmd, 0);
0578     } else {
0579         ocp->rsp_sent = true;
0580     }
0581 }
0582 
0583 static int
0584 efct_lio_datamove_done(struct efct_io *io, enum efct_scsi_io_status scsi_status,
0585                u32 flags, void *arg)
0586 {
0587     struct efct_scsi_tgt_io *ocp = &io->tgt_io;
0588 
0589     efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_DATA_DONE);
0590     if (scsi_status != EFCT_SCSI_STATUS_GOOD) {
0591         efct_lio_io_printf(io, "callback completed with error=%d\n",
0592                    scsi_status);
0593         ocp->err = scsi_status;
0594     }
0595     efct_lio_io_printf(io, "seg_map_cnt=%d\n", ocp->seg_map_cnt);
0596     if (ocp->seg_map_cnt) {
0597         if (ocp->err == EFCT_SCSI_STATUS_GOOD &&
0598             ocp->cur_seg < ocp->seg_cnt) {
0599             int rc;
0600 
0601             efct_lio_io_printf(io, "continuing cmd at segm=%d\n",
0602                        ocp->cur_seg);
0603             if (ocp->ddir == DMA_TO_DEVICE)
0604                 rc = efct_lio_write_pending(&ocp->cmd);
0605             else
0606                 rc = efct_lio_queue_data_in(&ocp->cmd);
0607             if (!rc)
0608                 return 0;
0609 
0610             ocp->err = EFCT_SCSI_STATUS_ERROR;
0611             efct_lio_io_printf(io, "could not continue command\n");
0612         }
0613         efct_lio_sg_unmap(io);
0614     }
0615 
0616     if (io->tgt_io.aborting) {
0617         efct_lio_io_printf(io, "IO done aborted\n");
0618         return 0;
0619     }
0620 
0621     if (ocp->ddir == DMA_TO_DEVICE) {
0622         efct_lio_io_printf(io, "Write done, trans_state=0x%x\n",
0623                    io->tgt_io.cmd.transport_state);
0624         if (scsi_status != EFCT_SCSI_STATUS_GOOD) {
0625             transport_generic_request_failure(&io->tgt_io.cmd,
0626                     TCM_CHECK_CONDITION_ABORT_CMD);
0627             efct_set_lio_io_state(io,
0628                 EFCT_LIO_STATE_TGT_GENERIC_REQ_FAILURE);
0629         } else {
0630             efct_set_lio_io_state(io,
0631                         EFCT_LIO_STATE_TGT_EXECUTE_CMD);
0632             target_execute_cmd(&io->tgt_io.cmd);
0633         }
0634     } else {
0635         efct_lio_send_resp(io, scsi_status, flags);
0636     }
0637     return 0;
0638 }
0639 
0640 static int
0641 efct_lio_tmf_done(struct efct_io *io, enum efct_scsi_io_status scsi_status,
0642           u32 flags, void *arg)
0643 {
0644     efct_lio_tmfio_printf(io, "cmd=%p status=%d, flags=0x%x\n",
0645                   &io->tgt_io.cmd, scsi_status, flags);
0646 
0647     efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_GENERIC_FREE);
0648     transport_generic_free_cmd(&io->tgt_io.cmd, 0);
0649     return 0;
0650 }
0651 
0652 static int
0653 efct_lio_null_tmf_done(struct efct_io *tmfio,
0654                enum efct_scsi_io_status scsi_status,
0655               u32 flags, void *arg)
0656 {
0657     efct_lio_tmfio_printf(tmfio, "cmd=%p status=%d, flags=0x%x\n",
0658                   &tmfio->tgt_io.cmd, scsi_status, flags);
0659 
0660     /* free struct efct_io only, no active se_cmd */
0661     efct_scsi_io_complete(tmfio);
0662     return 0;
0663 }
0664 
0665 static int
0666 efct_lio_queue_status(struct se_cmd *cmd)
0667 {
0668     struct efct_scsi_cmd_resp rsp;
0669     struct efct_scsi_tgt_io *ocp =
0670         container_of(cmd, struct efct_scsi_tgt_io, cmd);
0671     struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
0672     int rc = 0;
0673 
0674     efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_QUEUE_STATUS);
0675     efct_lio_io_printf(io,
0676         "status=0x%x trans_state=0x%x se_cmd_flags=0x%x sns_len=%d\n",
0677         cmd->scsi_status, cmd->transport_state, cmd->se_cmd_flags,
0678         cmd->scsi_sense_length);
0679 
0680     memset(&rsp, 0, sizeof(rsp));
0681     rsp.scsi_status = cmd->scsi_status;
0682     rsp.sense_data = (u8 *)io->tgt_io.sense_buffer;
0683     rsp.sense_data_length = cmd->scsi_sense_length;
0684 
0685     /* Check for residual underrun or overrun, mark negitive value for
0686      * underrun to recognize in HW
0687      */
0688     if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT)
0689         rsp.residual = -cmd->residual_count;
0690     else if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT)
0691         rsp.residual = cmd->residual_count;
0692 
0693     rc = efct_scsi_send_resp(io, 0, &rsp, efct_lio_status_done, NULL);
0694     efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_SEND_RSP);
0695     if (rc == 0)
0696         ocp->rsp_sent = true;
0697     return rc;
0698 }
0699 
0700 static void efct_lio_queue_tm_rsp(struct se_cmd *cmd)
0701 {
0702     struct efct_scsi_tgt_io *ocp =
0703         container_of(cmd, struct efct_scsi_tgt_io, cmd);
0704     struct efct_io *tmfio = container_of(ocp, struct efct_io, tgt_io);
0705     struct se_tmr_req *se_tmr = cmd->se_tmr_req;
0706     u8 rspcode;
0707 
0708     efct_lio_tmfio_printf(tmfio, "cmd=%p function=0x%x tmr->response=%d\n",
0709                   cmd, se_tmr->function, se_tmr->response);
0710     switch (se_tmr->response) {
0711     case TMR_FUNCTION_COMPLETE:
0712         rspcode = EFCT_SCSI_TMF_FUNCTION_COMPLETE;
0713         break;
0714     case TMR_TASK_DOES_NOT_EXIST:
0715         rspcode = EFCT_SCSI_TMF_FUNCTION_IO_NOT_FOUND;
0716         break;
0717     case TMR_LUN_DOES_NOT_EXIST:
0718         rspcode = EFCT_SCSI_TMF_INCORRECT_LOGICAL_UNIT_NUMBER;
0719         break;
0720     case TMR_FUNCTION_REJECTED:
0721     default:
0722         rspcode = EFCT_SCSI_TMF_FUNCTION_REJECTED;
0723         break;
0724     }
0725     efct_scsi_send_tmf_resp(tmfio, rspcode, NULL, efct_lio_tmf_done, NULL);
0726 }
0727 
0728 static struct efct *efct_find_wwpn(u64 wwpn)
0729 {
0730     struct efct *efct;
0731 
0732      /* Search for the HBA that has this WWPN */
0733     list_for_each_entry(efct, &efct_devices, list_entry) {
0734 
0735         if (wwpn == efct_get_wwpn(&efct->hw))
0736             return efct;
0737     }
0738 
0739     return NULL;
0740 }
0741 
0742 static struct se_wwn *
0743 efct_lio_make_nport(struct target_fabric_configfs *tf,
0744             struct config_group *group, const char *name)
0745 {
0746     struct efct_lio_nport *lio_nport;
0747     struct efct *efct;
0748     int ret;
0749     u64 wwpn;
0750 
0751     ret = efct_lio_parse_wwn(name, &wwpn, 0);
0752     if (ret)
0753         return ERR_PTR(ret);
0754 
0755     efct = efct_find_wwpn(wwpn);
0756     if (!efct) {
0757         pr_err("cannot find EFCT for base wwpn %s\n", name);
0758         return ERR_PTR(-ENXIO);
0759     }
0760 
0761     lio_nport = kzalloc(sizeof(*lio_nport), GFP_KERNEL);
0762     if (!lio_nport)
0763         return ERR_PTR(-ENOMEM);
0764 
0765     lio_nport->efct = efct;
0766     lio_nport->wwpn = wwpn;
0767     efct_format_wwn(lio_nport->wwpn_str, sizeof(lio_nport->wwpn_str),
0768             "naa.", wwpn);
0769     efct->tgt_efct.lio_nport = lio_nport;
0770 
0771     return &lio_nport->nport_wwn;
0772 }
0773 
0774 static struct se_wwn *
0775 efct_lio_npiv_make_nport(struct target_fabric_configfs *tf,
0776              struct config_group *group, const char *name)
0777 {
0778     struct efct_lio_vport *lio_vport;
0779     struct efct *efct;
0780     int ret;
0781     u64 p_wwpn, npiv_wwpn, npiv_wwnn;
0782     char *p, *pbuf, tmp[128];
0783     struct efct_lio_vport_list_t *vport_list;
0784     struct fc_vport *new_fc_vport;
0785     struct fc_vport_identifiers vport_id;
0786     unsigned long flags = 0;
0787 
0788     snprintf(tmp, sizeof(tmp), "%s", name);
0789     pbuf = &tmp[0];
0790 
0791     p = strsep(&pbuf, "@");
0792 
0793     if (!p || !pbuf) {
0794         pr_err("Unable to find separator operator(@)\n");
0795         return ERR_PTR(-EINVAL);
0796     }
0797 
0798     ret = efct_lio_parse_wwn(p, &p_wwpn, 0);
0799     if (ret)
0800         return ERR_PTR(ret);
0801 
0802     ret = efct_lio_parse_npiv_wwn(pbuf, strlen(pbuf), &npiv_wwpn,
0803                       &npiv_wwnn);
0804     if (ret)
0805         return ERR_PTR(ret);
0806 
0807     efct = efct_find_wwpn(p_wwpn);
0808     if (!efct) {
0809         pr_err("cannot find EFCT for base wwpn %s\n", name);
0810         return ERR_PTR(-ENXIO);
0811     }
0812 
0813     lio_vport = kzalloc(sizeof(*lio_vport), GFP_KERNEL);
0814     if (!lio_vport)
0815         return ERR_PTR(-ENOMEM);
0816 
0817     lio_vport->efct = efct;
0818     lio_vport->wwpn = p_wwpn;
0819     lio_vport->npiv_wwpn = npiv_wwpn;
0820     lio_vport->npiv_wwnn = npiv_wwnn;
0821 
0822     efct_format_wwn(lio_vport->wwpn_str, sizeof(lio_vport->wwpn_str),
0823             "naa.", npiv_wwpn);
0824 
0825     vport_list = kzalloc(sizeof(*vport_list), GFP_KERNEL);
0826     if (!vport_list) {
0827         kfree(lio_vport);
0828         return ERR_PTR(-ENOMEM);
0829     }
0830 
0831     vport_list->lio_vport = lio_vport;
0832 
0833     memset(&vport_id, 0, sizeof(vport_id));
0834     vport_id.port_name = npiv_wwpn;
0835     vport_id.node_name = npiv_wwnn;
0836     vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
0837     vport_id.vport_type = FC_PORTTYPE_NPIV;
0838     vport_id.disable = false;
0839 
0840     new_fc_vport = fc_vport_create(efct->shost, 0, &vport_id);
0841     if (!new_fc_vport) {
0842         efc_log_err(efct, "fc_vport_create failed\n");
0843         kfree(lio_vport);
0844         kfree(vport_list);
0845         return ERR_PTR(-ENOMEM);
0846     }
0847 
0848     lio_vport->fc_vport = new_fc_vport;
0849     spin_lock_irqsave(&efct->tgt_efct.efct_lio_lock, flags);
0850     INIT_LIST_HEAD(&vport_list->list_entry);
0851     list_add_tail(&vport_list->list_entry, &efct->tgt_efct.vport_list);
0852     spin_unlock_irqrestore(&efct->tgt_efct.efct_lio_lock, flags);
0853 
0854     return &lio_vport->vport_wwn;
0855 }
0856 
0857 static void
0858 efct_lio_drop_nport(struct se_wwn *wwn)
0859 {
0860     struct efct_lio_nport *lio_nport =
0861         container_of(wwn, struct efct_lio_nport, nport_wwn);
0862     struct efct *efct = lio_nport->efct;
0863 
0864     /* only physical nport should exist, free lio_nport allocated
0865      * in efct_lio_make_nport.
0866      */
0867     kfree(efct->tgt_efct.lio_nport);
0868     efct->tgt_efct.lio_nport = NULL;
0869 }
0870 
0871 static void
0872 efct_lio_npiv_drop_nport(struct se_wwn *wwn)
0873 {
0874     struct efct_lio_vport *lio_vport =
0875         container_of(wwn, struct efct_lio_vport, vport_wwn);
0876     struct efct_lio_vport_list_t *vport, *next_vport;
0877     struct efct *efct = lio_vport->efct;
0878     unsigned long flags = 0;
0879 
0880     if (lio_vport->fc_vport)
0881         fc_vport_terminate(lio_vport->fc_vport);
0882 
0883     spin_lock_irqsave(&efct->tgt_efct.efct_lio_lock, flags);
0884 
0885     list_for_each_entry_safe(vport, next_vport, &efct->tgt_efct.vport_list,
0886                  list_entry) {
0887         if (vport->lio_vport == lio_vport) {
0888             list_del(&vport->list_entry);
0889             kfree(vport->lio_vport);
0890             kfree(vport);
0891             break;
0892         }
0893     }
0894     spin_unlock_irqrestore(&efct->tgt_efct.efct_lio_lock, flags);
0895 }
0896 
0897 static struct se_portal_group *
0898 efct_lio_make_tpg(struct se_wwn *wwn, const char *name)
0899 {
0900     struct efct_lio_nport *lio_nport =
0901         container_of(wwn, struct efct_lio_nport, nport_wwn);
0902     struct efct_lio_tpg *tpg;
0903     struct efct *efct;
0904     unsigned long n;
0905     int ret;
0906 
0907     if (strstr(name, "tpgt_") != name)
0908         return ERR_PTR(-EINVAL);
0909     if (kstrtoul(name + 5, 10, &n) || n > USHRT_MAX)
0910         return ERR_PTR(-EINVAL);
0911 
0912     tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
0913     if (!tpg)
0914         return ERR_PTR(-ENOMEM);
0915 
0916     tpg->nport = lio_nport;
0917     tpg->tpgt = n;
0918     tpg->enabled = false;
0919 
0920     tpg->tpg_attrib.generate_node_acls = 1;
0921     tpg->tpg_attrib.demo_mode_write_protect = 1;
0922     tpg->tpg_attrib.cache_dynamic_acls = 1;
0923     tpg->tpg_attrib.demo_mode_login_only = 1;
0924     tpg->tpg_attrib.session_deletion_wait = 1;
0925 
0926     ret = core_tpg_register(wwn, &tpg->tpg, SCSI_PROTOCOL_FCP);
0927     if (ret < 0) {
0928         kfree(tpg);
0929         return NULL;
0930     }
0931     efct = lio_nport->efct;
0932     efct->tgt_efct.tpg = tpg;
0933     efc_log_debug(efct, "create portal group %d\n", tpg->tpgt);
0934 
0935     xa_init(&efct->lookup);
0936     return &tpg->tpg;
0937 }
0938 
0939 static void
0940 efct_lio_drop_tpg(struct se_portal_group *se_tpg)
0941 {
0942     struct efct_lio_tpg *tpg =
0943         container_of(se_tpg, struct efct_lio_tpg, tpg);
0944 
0945     struct efct *efct = tpg->nport->efct;
0946 
0947     efc_log_debug(efct, "drop portal group %d\n", tpg->tpgt);
0948     tpg->nport->efct->tgt_efct.tpg = NULL;
0949     core_tpg_deregister(se_tpg);
0950     xa_destroy(&efct->lookup);
0951     kfree(tpg);
0952 }
0953 
0954 static struct se_portal_group *
0955 efct_lio_npiv_make_tpg(struct se_wwn *wwn, const char *name)
0956 {
0957     struct efct_lio_vport *lio_vport =
0958         container_of(wwn, struct efct_lio_vport, vport_wwn);
0959     struct efct_lio_tpg *tpg;
0960     struct efct *efct;
0961     unsigned long n;
0962     int ret;
0963 
0964     efct = lio_vport->efct;
0965     if (strstr(name, "tpgt_") != name)
0966         return ERR_PTR(-EINVAL);
0967     if (kstrtoul(name + 5, 10, &n) || n > USHRT_MAX)
0968         return ERR_PTR(-EINVAL);
0969 
0970     if (n != 1) {
0971         efc_log_err(efct, "Invalid tpgt index: %ld provided\n", n);
0972         return ERR_PTR(-EINVAL);
0973     }
0974 
0975     tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
0976     if (!tpg)
0977         return ERR_PTR(-ENOMEM);
0978 
0979     tpg->vport = lio_vport;
0980     tpg->tpgt = n;
0981     tpg->enabled = false;
0982 
0983     tpg->tpg_attrib.generate_node_acls = 1;
0984     tpg->tpg_attrib.demo_mode_write_protect = 1;
0985     tpg->tpg_attrib.cache_dynamic_acls = 1;
0986     tpg->tpg_attrib.demo_mode_login_only = 1;
0987     tpg->tpg_attrib.session_deletion_wait = 1;
0988 
0989     ret = core_tpg_register(wwn, &tpg->tpg, SCSI_PROTOCOL_FCP);
0990 
0991     if (ret < 0) {
0992         kfree(tpg);
0993         return NULL;
0994     }
0995     lio_vport->tpg = tpg;
0996     efc_log_debug(efct, "create vport portal group %d\n", tpg->tpgt);
0997 
0998     return &tpg->tpg;
0999 }
1000 
1001 static void
1002 efct_lio_npiv_drop_tpg(struct se_portal_group *se_tpg)
1003 {
1004     struct efct_lio_tpg *tpg =
1005         container_of(se_tpg, struct efct_lio_tpg, tpg);
1006 
1007     efc_log_debug(tpg->vport->efct, "drop npiv portal group %d\n",
1008                tpg->tpgt);
1009     core_tpg_deregister(se_tpg);
1010     kfree(tpg);
1011 }
1012 
1013 static int
1014 efct_lio_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
1015 {
1016     struct efct_lio_nacl *nacl;
1017     u64 wwnn;
1018 
1019     if (efct_lio_parse_wwn(name, &wwnn, 0) < 0)
1020         return -EINVAL;
1021 
1022     nacl = container_of(se_nacl, struct efct_lio_nacl, se_node_acl);
1023     nacl->nport_wwnn = wwnn;
1024 
1025     efct_format_wwn(nacl->nport_name, sizeof(nacl->nport_name), "", wwnn);
1026     return 0;
1027 }
1028 
1029 static int efct_lio_check_demo_mode_login_only(struct se_portal_group *stpg)
1030 {
1031     struct efct_lio_tpg *tpg = container_of(stpg, struct efct_lio_tpg, tpg);
1032 
1033     return tpg->tpg_attrib.demo_mode_login_only;
1034 }
1035 
1036 static int
1037 efct_lio_npiv_check_demo_mode_login_only(struct se_portal_group *stpg)
1038 {
1039     struct efct_lio_tpg *tpg = container_of(stpg, struct efct_lio_tpg, tpg);
1040 
1041     return tpg->tpg_attrib.demo_mode_login_only;
1042 }
1043 
1044 static struct efct_lio_tpg *
1045 efct_get_vport_tpg(struct efc_node *node)
1046 {
1047     struct efct *efct;
1048     u64 wwpn = node->nport->wwpn;
1049     struct efct_lio_vport_list_t *vport, *next;
1050     struct efct_lio_vport *lio_vport = NULL;
1051     struct efct_lio_tpg *tpg = NULL;
1052     unsigned long flags = 0;
1053 
1054     efct = node->efc->base;
1055     spin_lock_irqsave(&efct->tgt_efct.efct_lio_lock, flags);
1056     list_for_each_entry_safe(vport, next, &efct->tgt_efct.vport_list,
1057                  list_entry) {
1058         lio_vport = vport->lio_vport;
1059         if (wwpn && lio_vport && lio_vport->npiv_wwpn == wwpn) {
1060             efc_log_debug(efct, "found tpg on vport\n");
1061             tpg = lio_vport->tpg;
1062             break;
1063         }
1064     }
1065     spin_unlock_irqrestore(&efct->tgt_efct.efct_lio_lock, flags);
1066     return tpg;
1067 }
1068 
1069 static void
1070 _efct_tgt_node_free(struct kref *arg)
1071 {
1072     struct efct_node *tgt_node = container_of(arg, struct efct_node, ref);
1073     struct efc_node *node = tgt_node->node;
1074 
1075     efc_scsi_del_initiator_complete(node->efc, node);
1076     kfree(tgt_node);
1077 }
1078 
1079 static int efct_session_cb(struct se_portal_group *se_tpg,
1080                struct se_session *se_sess, void *private)
1081 {
1082     struct efc_node *node = private;
1083     struct efct_node *tgt_node;
1084     struct efct *efct = node->efc->base;
1085 
1086     tgt_node = kzalloc(sizeof(*tgt_node), GFP_KERNEL);
1087     if (!tgt_node)
1088         return -ENOMEM;
1089 
1090     kref_init(&tgt_node->ref);
1091     tgt_node->release = _efct_tgt_node_free;
1092 
1093     tgt_node->session = se_sess;
1094     node->tgt_node = tgt_node;
1095     tgt_node->efct = efct;
1096 
1097     tgt_node->node = node;
1098 
1099     tgt_node->node_fc_id = node->rnode.fc_id;
1100     tgt_node->port_fc_id = node->nport->fc_id;
1101     tgt_node->vpi = node->nport->indicator;
1102     tgt_node->rpi = node->rnode.indicator;
1103 
1104     spin_lock_init(&tgt_node->active_ios_lock);
1105     INIT_LIST_HEAD(&tgt_node->active_ios);
1106 
1107     return 0;
1108 }
1109 
1110 int efct_scsi_tgt_new_device(struct efct *efct)
1111 {
1112     u32 total_ios;
1113 
1114     /* Get the max settings */
1115     efct->tgt_efct.max_sge = sli_get_max_sge(&efct->hw.sli);
1116     efct->tgt_efct.max_sgl = sli_get_max_sgl(&efct->hw.sli);
1117 
1118     /* initialize IO watermark fields */
1119     atomic_set(&efct->tgt_efct.ios_in_use, 0);
1120     total_ios = efct->hw.config.n_io;
1121     efc_log_debug(efct, "total_ios=%d\n", total_ios);
1122     efct->tgt_efct.watermark_min =
1123             (total_ios * EFCT_WATERMARK_LOW_PCT) / 100;
1124     efct->tgt_efct.watermark_max =
1125             (total_ios * EFCT_WATERMARK_HIGH_PCT) / 100;
1126     atomic_set(&efct->tgt_efct.io_high_watermark,
1127            efct->tgt_efct.watermark_max);
1128     atomic_set(&efct->tgt_efct.watermark_hit, 0);
1129     atomic_set(&efct->tgt_efct.initiator_count, 0);
1130 
1131     lio_wq = create_singlethread_workqueue("efct_lio_worker");
1132     if (!lio_wq) {
1133         efc_log_err(efct, "workqueue create failed\n");
1134         return -EIO;
1135     }
1136 
1137     spin_lock_init(&efct->tgt_efct.efct_lio_lock);
1138     INIT_LIST_HEAD(&efct->tgt_efct.vport_list);
1139 
1140     return 0;
1141 }
1142 
1143 int efct_scsi_tgt_del_device(struct efct *efct)
1144 {
1145     flush_workqueue(lio_wq);
1146 
1147     return 0;
1148 }
1149 
1150 int
1151 efct_scsi_tgt_new_nport(struct efc *efc, struct efc_nport *nport)
1152 {
1153     struct efct *efct = nport->efc->base;
1154 
1155     efc_log_debug(efct, "New SPORT: %s bound to %s\n", nport->display_name,
1156                efct->tgt_efct.lio_nport->wwpn_str);
1157 
1158     return 0;
1159 }
1160 
1161 void
1162 efct_scsi_tgt_del_nport(struct efc *efc, struct efc_nport *nport)
1163 {
1164     efc_log_debug(efc, "Del SPORT: %s\n", nport->display_name);
1165 }
1166 
1167 static void efct_lio_setup_session(struct work_struct *work)
1168 {
1169     struct efct_lio_wq_data *wq_data =
1170         container_of(work, struct efct_lio_wq_data, work);
1171     struct efct *efct = wq_data->efct;
1172     struct efc_node *node = wq_data->ptr;
1173     char wwpn[WWN_NAME_LEN];
1174     struct efct_lio_tpg *tpg;
1175     struct efct_node *tgt_node;
1176     struct se_portal_group *se_tpg;
1177     struct se_session *se_sess;
1178     int watermark;
1179     int ini_count;
1180     u64 id;
1181 
1182     /* Check to see if it's belongs to vport,
1183      * if not get physical port
1184      */
1185     tpg = efct_get_vport_tpg(node);
1186     if (tpg) {
1187         se_tpg = &tpg->tpg;
1188     } else if (efct->tgt_efct.tpg) {
1189         tpg = efct->tgt_efct.tpg;
1190         se_tpg = &tpg->tpg;
1191     } else {
1192         efc_log_err(efct, "failed to init session\n");
1193         return;
1194     }
1195 
1196     /*
1197      * Format the FCP Initiator port_name into colon
1198      * separated values to match the format by our explicit
1199      * ConfigFS NodeACLs.
1200      */
1201     efct_format_wwn(wwpn, sizeof(wwpn), "", efc_node_get_wwpn(node));
1202 
1203     se_sess = target_setup_session(se_tpg, 0, 0, TARGET_PROT_NORMAL, wwpn,
1204                        node, efct_session_cb);
1205     if (IS_ERR(se_sess)) {
1206         efc_log_err(efct, "failed to setup session\n");
1207         kfree(wq_data);
1208         efc_scsi_sess_reg_complete(node, -EIO);
1209         return;
1210     }
1211 
1212     tgt_node = node->tgt_node;
1213     id = (u64) tgt_node->port_fc_id << 32 | tgt_node->node_fc_id;
1214 
1215     efc_log_debug(efct, "new initiator sess=%p node=%p id: %llx\n",
1216               se_sess, node, id);
1217 
1218     if (xa_err(xa_store(&efct->lookup, id, tgt_node, GFP_KERNEL)))
1219         efc_log_err(efct, "Node lookup store failed\n");
1220 
1221     efc_scsi_sess_reg_complete(node, 0);
1222 
1223     /* update IO watermark: increment initiator count */
1224     ini_count = atomic_add_return(1, &efct->tgt_efct.initiator_count);
1225     watermark = efct->tgt_efct.watermark_max -
1226             ini_count * EFCT_IO_WATERMARK_PER_INITIATOR;
1227     watermark = (efct->tgt_efct.watermark_min > watermark) ?
1228             efct->tgt_efct.watermark_min : watermark;
1229     atomic_set(&efct->tgt_efct.io_high_watermark, watermark);
1230 
1231     kfree(wq_data);
1232 }
1233 
1234 int efct_scsi_new_initiator(struct efc *efc, struct efc_node *node)
1235 {
1236     struct efct *efct = node->efc->base;
1237     struct efct_lio_wq_data *wq_data;
1238 
1239     /*
1240      * Since LIO only supports initiator validation at thread level,
1241      * we are open minded and accept all callers.
1242      */
1243     wq_data = kzalloc(sizeof(*wq_data), GFP_ATOMIC);
1244     if (!wq_data)
1245         return -ENOMEM;
1246 
1247     wq_data->ptr = node;
1248     wq_data->efct = efct;
1249     INIT_WORK(&wq_data->work, efct_lio_setup_session);
1250     queue_work(lio_wq, &wq_data->work);
1251     return EFC_SCSI_CALL_ASYNC;
1252 }
1253 
1254 static void efct_lio_remove_session(struct work_struct *work)
1255 {
1256     struct efct_lio_wq_data *wq_data =
1257         container_of(work, struct efct_lio_wq_data, work);
1258     struct efct *efct = wq_data->efct;
1259     struct efc_node *node = wq_data->ptr;
1260     struct efct_node *tgt_node;
1261     struct se_session *se_sess;
1262 
1263     tgt_node = node->tgt_node;
1264     if (!tgt_node) {
1265         /* base driver has sent back-to-back requests
1266          * to unreg session with no intervening
1267          * register
1268          */
1269         efc_log_err(efct, "unreg session for NULL session\n");
1270         efc_scsi_del_initiator_complete(node->efc, node);
1271         return;
1272     }
1273 
1274     se_sess = tgt_node->session;
1275     efc_log_debug(efct, "unreg session se_sess=%p node=%p\n",
1276                se_sess, node);
1277 
1278     /* first flag all session commands to complete */
1279     target_stop_session(se_sess);
1280 
1281     /* now wait for session commands to complete */
1282     target_wait_for_sess_cmds(se_sess);
1283     target_remove_session(se_sess);
1284     tgt_node->session = NULL;
1285     node->tgt_node = NULL;
1286     kref_put(&tgt_node->ref, tgt_node->release);
1287 
1288     kfree(wq_data);
1289 }
1290 
1291 int efct_scsi_del_initiator(struct efc *efc, struct efc_node *node, int reason)
1292 {
1293     struct efct *efct = node->efc->base;
1294     struct efct_node *tgt_node = node->tgt_node;
1295     struct efct_lio_wq_data *wq_data;
1296     int watermark;
1297     int ini_count;
1298     u64 id;
1299 
1300     if (reason == EFCT_SCSI_INITIATOR_MISSING)
1301         return EFC_SCSI_CALL_COMPLETE;
1302 
1303     if (!tgt_node) {
1304         efc_log_err(efct, "tgt_node is NULL\n");
1305         return -EIO;
1306     }
1307 
1308     wq_data = kzalloc(sizeof(*wq_data), GFP_ATOMIC);
1309     if (!wq_data)
1310         return -ENOMEM;
1311 
1312     id = (u64) tgt_node->port_fc_id << 32 | tgt_node->node_fc_id;
1313     xa_erase(&efct->lookup, id);
1314 
1315     wq_data->ptr = node;
1316     wq_data->efct = efct;
1317     INIT_WORK(&wq_data->work, efct_lio_remove_session);
1318     queue_work(lio_wq, &wq_data->work);
1319 
1320     /*
1321      * update IO watermark: decrement initiator count
1322      */
1323     ini_count = atomic_sub_return(1, &efct->tgt_efct.initiator_count);
1324 
1325     watermark = efct->tgt_efct.watermark_max -
1326             ini_count * EFCT_IO_WATERMARK_PER_INITIATOR;
1327     watermark = (efct->tgt_efct.watermark_min > watermark) ?
1328             efct->tgt_efct.watermark_min : watermark;
1329     atomic_set(&efct->tgt_efct.io_high_watermark, watermark);
1330 
1331     return EFC_SCSI_CALL_ASYNC;
1332 }
1333 
1334 void efct_scsi_recv_cmd(struct efct_io *io, uint64_t lun, u8 *cdb,
1335                u32 cdb_len, u32 flags)
1336 {
1337     struct efct_scsi_tgt_io *ocp = &io->tgt_io;
1338     struct se_cmd *se_cmd = &io->tgt_io.cmd;
1339     struct efct *efct = io->efct;
1340     char *ddir;
1341     struct efct_node *tgt_node;
1342     struct se_session *se_sess;
1343     int rc = 0;
1344 
1345     memset(ocp, 0, sizeof(struct efct_scsi_tgt_io));
1346     efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_RECV_CMD);
1347     atomic_add_return(1, &efct->tgt_efct.ios_in_use);
1348 
1349     /* set target timeout */
1350     io->timeout = efct->target_io_timer_sec;
1351 
1352     if (flags & EFCT_SCSI_CMD_SIMPLE)
1353         ocp->task_attr = TCM_SIMPLE_TAG;
1354     else if (flags & EFCT_SCSI_CMD_HEAD_OF_QUEUE)
1355         ocp->task_attr = TCM_HEAD_TAG;
1356     else if (flags & EFCT_SCSI_CMD_ORDERED)
1357         ocp->task_attr = TCM_ORDERED_TAG;
1358     else if (flags & EFCT_SCSI_CMD_ACA)
1359         ocp->task_attr = TCM_ACA_TAG;
1360 
1361     switch (flags & (EFCT_SCSI_CMD_DIR_IN | EFCT_SCSI_CMD_DIR_OUT)) {
1362     case EFCT_SCSI_CMD_DIR_IN:
1363         ddir = "FROM_INITIATOR";
1364         ocp->ddir = DMA_TO_DEVICE;
1365         break;
1366     case EFCT_SCSI_CMD_DIR_OUT:
1367         ddir = "TO_INITIATOR";
1368         ocp->ddir = DMA_FROM_DEVICE;
1369         break;
1370     case EFCT_SCSI_CMD_DIR_IN | EFCT_SCSI_CMD_DIR_OUT:
1371         ddir = "BIDIR";
1372         ocp->ddir = DMA_BIDIRECTIONAL;
1373         break;
1374     default:
1375         ddir = "NONE";
1376         ocp->ddir = DMA_NONE;
1377         break;
1378     }
1379 
1380     ocp->lun = lun;
1381     efct_lio_io_printf(io, "new cmd=0x%x ddir=%s dl=%u\n",
1382                cdb[0], ddir, io->exp_xfer_len);
1383 
1384     tgt_node = io->node;
1385     se_sess = tgt_node->session;
1386     if (!se_sess) {
1387         efc_log_err(efct, "No session found to submit IO se_cmd: %p\n",
1388                 &ocp->cmd);
1389         efct_scsi_io_free(io);
1390         return;
1391     }
1392 
1393     efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_SUBMIT_CMD);
1394     rc = target_init_cmd(se_cmd, se_sess, &io->tgt_io.sense_buffer[0],
1395                  ocp->lun, io->exp_xfer_len, ocp->task_attr,
1396                  ocp->ddir, TARGET_SCF_ACK_KREF);
1397     if (rc) {
1398         efc_log_err(efct, "failed to init cmd se_cmd: %p\n", se_cmd);
1399         efct_scsi_io_free(io);
1400         return;
1401     }
1402 
1403     if (target_submit_prep(se_cmd, cdb, NULL, 0, NULL, 0,
1404                 NULL, 0, GFP_ATOMIC))
1405         return;
1406 
1407     target_submit(se_cmd);
1408 }
1409 
1410 int
1411 efct_scsi_recv_tmf(struct efct_io *tmfio, u32 lun, enum efct_scsi_tmf_cmd cmd,
1412            struct efct_io *io_to_abort, u32 flags)
1413 {
1414     unsigned char tmr_func;
1415     struct efct *efct = tmfio->efct;
1416     struct efct_scsi_tgt_io *ocp = &tmfio->tgt_io;
1417     struct efct_node *tgt_node;
1418     struct se_session *se_sess;
1419     int rc;
1420 
1421     memset(ocp, 0, sizeof(struct efct_scsi_tgt_io));
1422     efct_set_lio_io_state(tmfio, EFCT_LIO_STATE_SCSI_RECV_TMF);
1423     atomic_add_return(1, &efct->tgt_efct.ios_in_use);
1424     efct_lio_tmfio_printf(tmfio, "%s: new tmf %x lun=%u\n",
1425                   tmfio->display_name, cmd, lun);
1426 
1427     switch (cmd) {
1428     case EFCT_SCSI_TMF_ABORT_TASK:
1429         tmr_func = TMR_ABORT_TASK;
1430         break;
1431     case EFCT_SCSI_TMF_ABORT_TASK_SET:
1432         tmr_func = TMR_ABORT_TASK_SET;
1433         break;
1434     case EFCT_SCSI_TMF_CLEAR_TASK_SET:
1435         tmr_func = TMR_CLEAR_TASK_SET;
1436         break;
1437     case EFCT_SCSI_TMF_LOGICAL_UNIT_RESET:
1438         tmr_func = TMR_LUN_RESET;
1439         break;
1440     case EFCT_SCSI_TMF_CLEAR_ACA:
1441         tmr_func = TMR_CLEAR_ACA;
1442         break;
1443     case EFCT_SCSI_TMF_TARGET_RESET:
1444         tmr_func = TMR_TARGET_WARM_RESET;
1445         break;
1446     case EFCT_SCSI_TMF_QUERY_ASYNCHRONOUS_EVENT:
1447     case EFCT_SCSI_TMF_QUERY_TASK_SET:
1448     default:
1449         goto tmf_fail;
1450     }
1451 
1452     tmfio->tgt_io.tmf = tmr_func;
1453     tmfio->tgt_io.lun = lun;
1454     tmfio->tgt_io.io_to_abort = io_to_abort;
1455 
1456     tgt_node = tmfio->node;
1457 
1458     se_sess = tgt_node->session;
1459     if (!se_sess)
1460         return 0;
1461 
1462     rc = target_submit_tmr(&ocp->cmd, se_sess, NULL, lun, ocp, tmr_func,
1463             GFP_ATOMIC, tmfio->init_task_tag, TARGET_SCF_ACK_KREF);
1464 
1465     efct_set_lio_io_state(tmfio, EFCT_LIO_STATE_TGT_SUBMIT_TMR);
1466     if (rc)
1467         goto tmf_fail;
1468 
1469     return 0;
1470 
1471 tmf_fail:
1472     efct_scsi_send_tmf_resp(tmfio, EFCT_SCSI_TMF_FUNCTION_REJECTED,
1473                 NULL, efct_lio_null_tmf_done, NULL);
1474     return 0;
1475 }
1476 
1477 /* Start items for efct_lio_tpg_attrib_cit */
1478 
1479 #define DEF_EFCT_TPG_ATTRIB(name)                     \
1480                                       \
1481 static ssize_t efct_lio_tpg_attrib_##name##_show(             \
1482         struct config_item *item, char *page)             \
1483 {                                     \
1484     struct se_portal_group *se_tpg = to_tpg(item);            \
1485     struct efct_lio_tpg *tpg = container_of(se_tpg,           \
1486             struct efct_lio_tpg, tpg);            \
1487                                       \
1488     return sprintf(page, "%u\n", tpg->tpg_attrib.name);       \
1489 }                                     \
1490                                       \
1491 static ssize_t efct_lio_tpg_attrib_##name##_store(            \
1492         struct config_item *item, const char *page, size_t count) \
1493 {                                     \
1494     struct se_portal_group *se_tpg = to_tpg(item);            \
1495     struct efct_lio_tpg *tpg = container_of(se_tpg,           \
1496                     struct efct_lio_tpg, tpg);    \
1497     struct efct_lio_tpg_attrib *a = &tpg->tpg_attrib;         \
1498     unsigned long val;                        \
1499     int ret;                              \
1500                                       \
1501     ret = kstrtoul(page, 0, &val);                    \
1502     if (ret < 0) {                            \
1503         pr_err("kstrtoul() failed with ret: %d\n", ret);      \
1504         return ret;                       \
1505     }                                 \
1506                                       \
1507     if (val != 0 && val != 1) {                   \
1508         pr_err("Illegal boolean value %lu\n", val);       \
1509         return -EINVAL;                       \
1510     }                                 \
1511                                       \
1512     a->name = val;                            \
1513                                       \
1514     return count;                             \
1515 }                                     \
1516 CONFIGFS_ATTR(efct_lio_tpg_attrib_, name)
1517 
1518 DEF_EFCT_TPG_ATTRIB(generate_node_acls);
1519 DEF_EFCT_TPG_ATTRIB(cache_dynamic_acls);
1520 DEF_EFCT_TPG_ATTRIB(demo_mode_write_protect);
1521 DEF_EFCT_TPG_ATTRIB(prod_mode_write_protect);
1522 DEF_EFCT_TPG_ATTRIB(demo_mode_login_only);
1523 DEF_EFCT_TPG_ATTRIB(session_deletion_wait);
1524 
1525 static struct configfs_attribute *efct_lio_tpg_attrib_attrs[] = {
1526     &efct_lio_tpg_attrib_attr_generate_node_acls,
1527     &efct_lio_tpg_attrib_attr_cache_dynamic_acls,
1528     &efct_lio_tpg_attrib_attr_demo_mode_write_protect,
1529     &efct_lio_tpg_attrib_attr_prod_mode_write_protect,
1530     &efct_lio_tpg_attrib_attr_demo_mode_login_only,
1531     &efct_lio_tpg_attrib_attr_session_deletion_wait,
1532     NULL,
1533 };
1534 
1535 #define DEF_EFCT_NPIV_TPG_ATTRIB(name)                     \
1536                                        \
1537 static ssize_t efct_lio_npiv_tpg_attrib_##name##_show(             \
1538         struct config_item *item, char *page)              \
1539 {                                      \
1540     struct se_portal_group *se_tpg = to_tpg(item);             \
1541     struct efct_lio_tpg *tpg = container_of(se_tpg,            \
1542             struct efct_lio_tpg, tpg);             \
1543                                        \
1544     return sprintf(page, "%u\n", tpg->tpg_attrib.name);        \
1545 }                                      \
1546                                        \
1547 static ssize_t efct_lio_npiv_tpg_attrib_##name##_store(            \
1548         struct config_item *item, const char *page, size_t count)  \
1549 {                                      \
1550     struct se_portal_group *se_tpg = to_tpg(item);             \
1551     struct efct_lio_tpg *tpg = container_of(se_tpg,            \
1552             struct efct_lio_tpg, tpg);             \
1553     struct efct_lio_tpg_attrib *a = &tpg->tpg_attrib;          \
1554     unsigned long val;                         \
1555     int ret;                               \
1556                                        \
1557     ret = kstrtoul(page, 0, &val);                     \
1558     if (ret < 0) {                             \
1559         pr_err("kstrtoul() failed with ret: %d\n", ret);       \
1560         return ret;                        \
1561     }                                  \
1562                                        \
1563     if (val != 0 && val != 1) {                    \
1564         pr_err("Illegal boolean value %lu\n", val);        \
1565         return -EINVAL;                        \
1566     }                                  \
1567                                        \
1568     a->name = val;                             \
1569                                        \
1570     return count;                              \
1571 }                                      \
1572 CONFIGFS_ATTR(efct_lio_npiv_tpg_attrib_, name)
1573 
1574 DEF_EFCT_NPIV_TPG_ATTRIB(generate_node_acls);
1575 DEF_EFCT_NPIV_TPG_ATTRIB(cache_dynamic_acls);
1576 DEF_EFCT_NPIV_TPG_ATTRIB(demo_mode_write_protect);
1577 DEF_EFCT_NPIV_TPG_ATTRIB(prod_mode_write_protect);
1578 DEF_EFCT_NPIV_TPG_ATTRIB(demo_mode_login_only);
1579 DEF_EFCT_NPIV_TPG_ATTRIB(session_deletion_wait);
1580 
1581 static struct configfs_attribute *efct_lio_npiv_tpg_attrib_attrs[] = {
1582     &efct_lio_npiv_tpg_attrib_attr_generate_node_acls,
1583     &efct_lio_npiv_tpg_attrib_attr_cache_dynamic_acls,
1584     &efct_lio_npiv_tpg_attrib_attr_demo_mode_write_protect,
1585     &efct_lio_npiv_tpg_attrib_attr_prod_mode_write_protect,
1586     &efct_lio_npiv_tpg_attrib_attr_demo_mode_login_only,
1587     &efct_lio_npiv_tpg_attrib_attr_session_deletion_wait,
1588     NULL,
1589 };
1590 
1591 CONFIGFS_ATTR(efct_lio_tpg_, enable);
1592 static struct configfs_attribute *efct_lio_tpg_attrs[] = {
1593                 &efct_lio_tpg_attr_enable, NULL };
1594 CONFIGFS_ATTR(efct_lio_npiv_tpg_, enable);
1595 static struct configfs_attribute *efct_lio_npiv_tpg_attrs[] = {
1596                 &efct_lio_npiv_tpg_attr_enable, NULL };
1597 
1598 static const struct target_core_fabric_ops efct_lio_ops = {
1599     .module             = THIS_MODULE,
1600     .fabric_name            = "efct",
1601     .node_acl_size          = sizeof(struct efct_lio_nacl),
1602     .max_data_sg_nents      = 65535,
1603     .tpg_get_wwn            = efct_lio_get_fabric_wwn,
1604     .tpg_get_tag            = efct_lio_get_tag,
1605     .fabric_init_nodeacl        = efct_lio_init_nodeacl,
1606     .tpg_check_demo_mode        = efct_lio_check_demo_mode,
1607     .tpg_check_demo_mode_cache      = efct_lio_check_demo_mode_cache,
1608     .tpg_check_demo_mode_write_protect = efct_lio_check_demo_write_protect,
1609     .tpg_check_prod_mode_write_protect = efct_lio_check_prod_write_protect,
1610     .tpg_get_inst_index     = efct_lio_tpg_get_inst_index,
1611     .check_stop_free        = efct_lio_check_stop_free,
1612     .aborted_task           = efct_lio_aborted_task,
1613     .release_cmd            = efct_lio_release_cmd,
1614     .close_session          = efct_lio_close_session,
1615     .sess_get_index         = efct_lio_sess_get_index,
1616     .write_pending          = efct_lio_write_pending,
1617     .set_default_node_attributes    = efct_lio_set_default_node_attrs,
1618     .get_cmd_state          = efct_lio_get_cmd_state,
1619     .queue_data_in          = efct_lio_queue_data_in,
1620     .queue_status           = efct_lio_queue_status,
1621     .queue_tm_rsp           = efct_lio_queue_tm_rsp,
1622     .fabric_make_wwn        = efct_lio_make_nport,
1623     .fabric_drop_wwn        = efct_lio_drop_nport,
1624     .fabric_make_tpg        = efct_lio_make_tpg,
1625     .fabric_drop_tpg        = efct_lio_drop_tpg,
1626     .tpg_check_demo_mode_login_only = efct_lio_check_demo_mode_login_only,
1627     .tpg_check_prot_fabric_only = NULL,
1628     .sess_get_initiator_sid     = NULL,
1629     .tfc_tpg_base_attrs     = efct_lio_tpg_attrs,
1630     .tfc_tpg_attrib_attrs           = efct_lio_tpg_attrib_attrs,
1631 };
1632 
1633 static const struct target_core_fabric_ops efct_lio_npiv_ops = {
1634     .module             = THIS_MODULE,
1635     .fabric_name            = "efct_npiv",
1636     .node_acl_size          = sizeof(struct efct_lio_nacl),
1637     .max_data_sg_nents      = 65535,
1638     .tpg_get_wwn            = efct_lio_get_npiv_fabric_wwn,
1639     .tpg_get_tag            = efct_lio_get_npiv_tag,
1640     .fabric_init_nodeacl        = efct_lio_init_nodeacl,
1641     .tpg_check_demo_mode        = efct_lio_check_demo_mode,
1642     .tpg_check_demo_mode_cache      = efct_lio_check_demo_mode_cache,
1643     .tpg_check_demo_mode_write_protect =
1644                     efct_lio_npiv_check_demo_write_protect,
1645     .tpg_check_prod_mode_write_protect =
1646                     efct_lio_npiv_check_prod_write_protect,
1647     .tpg_get_inst_index     = efct_lio_tpg_get_inst_index,
1648     .check_stop_free        = efct_lio_check_stop_free,
1649     .aborted_task           = efct_lio_aborted_task,
1650     .release_cmd            = efct_lio_release_cmd,
1651     .close_session          = efct_lio_close_session,
1652     .sess_get_index         = efct_lio_sess_get_index,
1653     .write_pending          = efct_lio_write_pending,
1654     .set_default_node_attributes    = efct_lio_set_default_node_attrs,
1655     .get_cmd_state          = efct_lio_get_cmd_state,
1656     .queue_data_in          = efct_lio_queue_data_in,
1657     .queue_status           = efct_lio_queue_status,
1658     .queue_tm_rsp           = efct_lio_queue_tm_rsp,
1659     .fabric_make_wwn        = efct_lio_npiv_make_nport,
1660     .fabric_drop_wwn        = efct_lio_npiv_drop_nport,
1661     .fabric_make_tpg        = efct_lio_npiv_make_tpg,
1662     .fabric_drop_tpg        = efct_lio_npiv_drop_tpg,
1663     .tpg_check_demo_mode_login_only =
1664                 efct_lio_npiv_check_demo_mode_login_only,
1665     .tpg_check_prot_fabric_only = NULL,
1666     .sess_get_initiator_sid     = NULL,
1667     .tfc_tpg_base_attrs     = efct_lio_npiv_tpg_attrs,
1668     .tfc_tpg_attrib_attrs       = efct_lio_npiv_tpg_attrib_attrs,
1669 };
1670 
1671 int efct_scsi_tgt_driver_init(void)
1672 {
1673     int rc;
1674 
1675     /* Register the top level struct config_item_type with TCM core */
1676     rc = target_register_template(&efct_lio_ops);
1677     if (rc < 0) {
1678         pr_err("target_fabric_configfs_register failed with %d\n", rc);
1679         return rc;
1680     }
1681     rc = target_register_template(&efct_lio_npiv_ops);
1682     if (rc < 0) {
1683         pr_err("target_fabric_configfs_register failed with %d\n", rc);
1684         target_unregister_template(&efct_lio_ops);
1685         return rc;
1686     }
1687     return 0;
1688 }
1689 
1690 int efct_scsi_tgt_driver_exit(void)
1691 {
1692     target_unregister_template(&efct_lio_ops);
1693     target_unregister_template(&efct_lio_npiv_ops);
1694     return 0;
1695 }