1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2021 Broadcom. All Rights Reserved. The term 4 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 5 */ 6 7 #include <target/target_core_base.h> 8 #include <target/target_core_fabric.h> 9 #include "efct_driver.h" 10 #include "efct_lio.h" 11 12 /* 13 * lio_wq is used to call the LIO backed during creation or deletion of 14 * sessions. This brings serialization to the session management as we create 15 * single threaded work queue. 16 */ 17 static struct workqueue_struct *lio_wq; 18 19 static int 20 efct_format_wwn(char *str, size_t len, const char *pre, u64 wwn) 21 { 22 u8 a[8]; 23 24 put_unaligned_be64(wwn, a); 25 return snprintf(str, len, "%s%8phC", pre, a); 26 } 27 28 static int 29 efct_lio_parse_wwn(const char *name, u64 *wwp, u8 npiv) 30 { 31 int num; 32 u8 b[8]; 33 34 if (npiv) { 35 num = sscanf(name, 36 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", 37 &b[0], &b[1], &b[2], &b[3], &b[4], &b[5], &b[6], 38 &b[7]); 39 } else { 40 num = sscanf(name, 41 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", 42 &b[0], &b[1], &b[2], &b[3], &b[4], &b[5], &b[6], 43 &b[7]); 44 } 45 46 if (num != 8) 47 return -EINVAL; 48 49 *wwp = get_unaligned_be64(b); 50 return 0; 51 } 52 53 static int 54 efct_lio_parse_npiv_wwn(const char *name, size_t size, u64 *wwpn, u64 *wwnn) 55 { 56 unsigned int cnt = size; 57 int rc; 58 59 *wwpn = *wwnn = 0; 60 if (name[cnt - 1] == '\n' || name[cnt - 1] == 0) 61 cnt--; 62 63 /* validate we have enough characters for WWPN */ 64 if ((cnt != (16 + 1 + 16)) || (name[16] != ':')) 65 return -EINVAL; 66 67 rc = efct_lio_parse_wwn(&name[0], wwpn, 1); 68 if (rc) 69 return rc; 70 71 rc = efct_lio_parse_wwn(&name[17], wwnn, 1); 72 if (rc) 73 return rc; 74 75 return 0; 76 } 77 78 static ssize_t 79 efct_lio_tpg_enable_show(struct config_item *item, char *page) 80 { 81 struct se_portal_group *se_tpg = to_tpg(item); 82 struct efct_lio_tpg *tpg = 83 container_of(se_tpg, struct efct_lio_tpg, tpg); 84 85 return snprintf(page, PAGE_SIZE, "%d\n", tpg->enabled); 86 } 87 88 static ssize_t 89 efct_lio_tpg_enable_store(struct config_item *item, const char *page, 90 size_t count) 91 { 92 struct se_portal_group *se_tpg = to_tpg(item); 93 struct efct_lio_tpg *tpg = 94 container_of(se_tpg, struct efct_lio_tpg, tpg); 95 struct efct *efct; 96 struct efc *efc; 97 unsigned long op; 98 99 if (!tpg->nport || !tpg->nport->efct) { 100 pr_err("%s: Unable to find EFCT device\n", __func__); 101 return -EINVAL; 102 } 103 104 efct = tpg->nport->efct; 105 efc = efct->efcport; 106 107 if (kstrtoul(page, 0, &op) < 0) 108 return -EINVAL; 109 110 if (op == 1) { 111 int ret; 112 113 tpg->enabled = true; 114 efc_log_debug(efct, "enable portal group %d\n", tpg->tpgt); 115 116 ret = efct_xport_control(efct->xport, EFCT_XPORT_PORT_ONLINE); 117 if (ret) { 118 efct->tgt_efct.lio_nport = NULL; 119 efc_log_debug(efct, "cannot bring port online\n"); 120 return ret; 121 } 122 } else if (op == 0) { 123 efc_log_debug(efct, "disable portal group %d\n", tpg->tpgt); 124 125 if (efc->domain && efc->domain->nport) 126 efct_scsi_tgt_del_nport(efc, efc->domain->nport); 127 128 tpg->enabled = false; 129 } else { 130 return -EINVAL; 131 } 132 133 return count; 134 } 135 136 static ssize_t 137 efct_lio_npiv_tpg_enable_show(struct config_item *item, char *page) 138 { 139 struct se_portal_group *se_tpg = to_tpg(item); 140 struct efct_lio_tpg *tpg = 141 container_of(se_tpg, struct efct_lio_tpg, tpg); 142 143 return snprintf(page, PAGE_SIZE, "%d\n", tpg->enabled); 144 } 145 146 static ssize_t 147 efct_lio_npiv_tpg_enable_store(struct config_item *item, const char *page, 148 size_t count) 149 { 150 struct se_portal_group *se_tpg = to_tpg(item); 151 struct efct_lio_tpg *tpg = 152 container_of(se_tpg, struct efct_lio_tpg, tpg); 153 struct efct_lio_vport *lio_vport = tpg->vport; 154 struct efct *efct; 155 struct efc *efc; 156 unsigned long op; 157 158 if (kstrtoul(page, 0, &op) < 0) 159 return -EINVAL; 160 161 if (!lio_vport) { 162 pr_err("Unable to find vport\n"); 163 return -EINVAL; 164 } 165 166 efct = lio_vport->efct; 167 efc = efct->efcport; 168 169 if (op == 1) { 170 tpg->enabled = true; 171 efc_log_debug(efct, "enable portal group %d\n", tpg->tpgt); 172 173 if (efc->domain) { 174 int ret; 175 176 ret = efc_nport_vport_new(efc->domain, 177 lio_vport->npiv_wwpn, 178 lio_vport->npiv_wwnn, 179 U32_MAX, false, true, 180 NULL, NULL); 181 if (ret != 0) { 182 efc_log_err(efct, "Failed to create Vport\n"); 183 return ret; 184 } 185 return count; 186 } 187 188 if (!(efc_vport_create_spec(efc, lio_vport->npiv_wwnn, 189 lio_vport->npiv_wwpn, U32_MAX, 190 false, true, NULL, NULL))) 191 return -ENOMEM; 192 193 } else if (op == 0) { 194 efc_log_debug(efct, "disable portal group %d\n", tpg->tpgt); 195 196 tpg->enabled = false; 197 /* only physical nport should exist, free lio_nport 198 * allocated in efct_lio_make_nport 199 */ 200 if (efc->domain) { 201 efc_nport_vport_del(efct->efcport, efc->domain, 202 lio_vport->npiv_wwpn, 203 lio_vport->npiv_wwnn); 204 return count; 205 } 206 } else { 207 return -EINVAL; 208 } 209 return count; 210 } 211 212 static char *efct_lio_get_fabric_wwn(struct se_portal_group *se_tpg) 213 { 214 struct efct_lio_tpg *tpg = 215 container_of(se_tpg, struct efct_lio_tpg, tpg); 216 217 return tpg->nport->wwpn_str; 218 } 219 220 static char *efct_lio_get_npiv_fabric_wwn(struct se_portal_group *se_tpg) 221 { 222 struct efct_lio_tpg *tpg = 223 container_of(se_tpg, struct efct_lio_tpg, tpg); 224 225 return tpg->vport->wwpn_str; 226 } 227 228 static u16 efct_lio_get_tag(struct se_portal_group *se_tpg) 229 { 230 struct efct_lio_tpg *tpg = 231 container_of(se_tpg, struct efct_lio_tpg, tpg); 232 233 return tpg->tpgt; 234 } 235 236 static u16 efct_lio_get_npiv_tag(struct se_portal_group *se_tpg) 237 { 238 struct efct_lio_tpg *tpg = 239 container_of(se_tpg, struct efct_lio_tpg, tpg); 240 241 return tpg->tpgt; 242 } 243 244 static int efct_lio_check_demo_mode(struct se_portal_group *se_tpg) 245 { 246 return 1; 247 } 248 249 static int efct_lio_check_demo_mode_cache(struct se_portal_group *se_tpg) 250 { 251 return 1; 252 } 253 254 static int efct_lio_check_demo_write_protect(struct se_portal_group *se_tpg) 255 { 256 struct efct_lio_tpg *tpg = 257 container_of(se_tpg, struct efct_lio_tpg, tpg); 258 259 return tpg->tpg_attrib.demo_mode_write_protect; 260 } 261 262 static int 263 efct_lio_npiv_check_demo_write_protect(struct se_portal_group *se_tpg) 264 { 265 struct efct_lio_tpg *tpg = 266 container_of(se_tpg, struct efct_lio_tpg, tpg); 267 268 return tpg->tpg_attrib.demo_mode_write_protect; 269 } 270 271 static int efct_lio_check_prod_write_protect(struct se_portal_group *se_tpg) 272 { 273 struct efct_lio_tpg *tpg = 274 container_of(se_tpg, struct efct_lio_tpg, tpg); 275 276 return tpg->tpg_attrib.prod_mode_write_protect; 277 } 278 279 static int 280 efct_lio_npiv_check_prod_write_protect(struct se_portal_group *se_tpg) 281 { 282 struct efct_lio_tpg *tpg = 283 container_of(se_tpg, struct efct_lio_tpg, tpg); 284 285 return tpg->tpg_attrib.prod_mode_write_protect; 286 } 287 288 static u32 efct_lio_tpg_get_inst_index(struct se_portal_group *se_tpg) 289 { 290 return 1; 291 } 292 293 static int efct_lio_check_stop_free(struct se_cmd *se_cmd) 294 { 295 struct efct_scsi_tgt_io *ocp = 296 container_of(se_cmd, struct efct_scsi_tgt_io, cmd); 297 struct efct_io *io = container_of(ocp, struct efct_io, tgt_io); 298 299 efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_CHK_STOP_FREE); 300 return target_put_sess_cmd(se_cmd); 301 } 302 303 static int 304 efct_lio_abort_tgt_cb(struct efct_io *io, 305 enum efct_scsi_io_status scsi_status, 306 u32 flags, void *arg) 307 { 308 efct_lio_io_printf(io, "Abort done, status:%d\n", scsi_status); 309 return 0; 310 } 311 312 static void 313 efct_lio_aborted_task(struct se_cmd *se_cmd) 314 { 315 struct efct_scsi_tgt_io *ocp = 316 container_of(se_cmd, struct efct_scsi_tgt_io, cmd); 317 struct efct_io *io = container_of(ocp, struct efct_io, tgt_io); 318 319 efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_ABORTED_TASK); 320 321 if (ocp->rsp_sent) 322 return; 323 324 /* command has been aborted, cleanup here */ 325 ocp->aborting = true; 326 ocp->err = EFCT_SCSI_STATUS_ABORTED; 327 /* terminate the exchange */ 328 efct_scsi_tgt_abort_io(io, efct_lio_abort_tgt_cb, NULL); 329 } 330 331 static void efct_lio_release_cmd(struct se_cmd *se_cmd) 332 { 333 struct efct_scsi_tgt_io *ocp = 334 container_of(se_cmd, struct efct_scsi_tgt_io, cmd); 335 struct efct_io *io = container_of(ocp, struct efct_io, tgt_io); 336 struct efct *efct = io->efct; 337 338 efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_RELEASE_CMD); 339 efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_CMPL_CMD); 340 efct_scsi_io_complete(io); 341 atomic_sub_return(1, &efct->tgt_efct.ios_in_use); 342 } 343 344 static void efct_lio_close_session(struct se_session *se_sess) 345 { 346 struct efc_node *node = se_sess->fabric_sess_ptr; 347 348 pr_debug("se_sess=%p node=%p", se_sess, node); 349 350 if (!node) { 351 pr_debug("node is NULL"); 352 return; 353 } 354 355 efc_node_post_shutdown(node, NULL); 356 } 357 358 static u32 efct_lio_sess_get_index(struct se_session *se_sess) 359 { 360 return 0; 361 } 362 363 static void efct_lio_set_default_node_attrs(struct se_node_acl *nacl) 364 { 365 } 366 367 static int efct_lio_get_cmd_state(struct se_cmd *cmd) 368 { 369 struct efct_scsi_tgt_io *ocp = 370 container_of(cmd, struct efct_scsi_tgt_io, cmd); 371 struct efct_io *io = container_of(ocp, struct efct_io, tgt_io); 372 373 if (!io) 374 return 0; 375 376 return io->tgt_io.state; 377 } 378 379 static int 380 efct_lio_sg_map(struct efct_io *io) 381 { 382 struct efct_scsi_tgt_io *ocp = &io->tgt_io; 383 struct se_cmd *cmd = &ocp->cmd; 384 385 ocp->seg_map_cnt = dma_map_sg(&io->efct->pci->dev, cmd->t_data_sg, 386 cmd->t_data_nents, cmd->data_direction); 387 if (ocp->seg_map_cnt == 0) 388 return -EFAULT; 389 return 0; 390 } 391 392 static void 393 efct_lio_sg_unmap(struct efct_io *io) 394 { 395 struct efct_scsi_tgt_io *ocp = &io->tgt_io; 396 struct se_cmd *cmd = &ocp->cmd; 397 398 if (WARN_ON(!ocp->seg_map_cnt || !cmd->t_data_sg)) 399 return; 400 401 dma_unmap_sg(&io->efct->pci->dev, cmd->t_data_sg, 402 ocp->seg_map_cnt, cmd->data_direction); 403 ocp->seg_map_cnt = 0; 404 } 405 406 static int 407 efct_lio_status_done(struct efct_io *io, 408 enum efct_scsi_io_status scsi_status, 409 u32 flags, void *arg) 410 { 411 struct efct_scsi_tgt_io *ocp = &io->tgt_io; 412 413 efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_RSP_DONE); 414 if (scsi_status != EFCT_SCSI_STATUS_GOOD) { 415 efct_lio_io_printf(io, "callback completed with error=%d\n", 416 scsi_status); 417 ocp->err = scsi_status; 418 } 419 if (ocp->seg_map_cnt) 420 efct_lio_sg_unmap(io); 421 422 efct_lio_io_printf(io, "status=%d, err=%d flags=0x%x, dir=%d\n", 423 scsi_status, ocp->err, flags, ocp->ddir); 424 425 efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_GENERIC_FREE); 426 transport_generic_free_cmd(&io->tgt_io.cmd, 0); 427 return 0; 428 } 429 430 static int 431 efct_lio_datamove_done(struct efct_io *io, enum efct_scsi_io_status scsi_status, 432 u32 flags, void *arg); 433 434 static int 435 efct_lio_write_pending(struct se_cmd *cmd) 436 { 437 struct efct_scsi_tgt_io *ocp = 438 container_of(cmd, struct efct_scsi_tgt_io, cmd); 439 struct efct_io *io = container_of(ocp, struct efct_io, tgt_io); 440 struct efct_scsi_sgl *sgl = io->sgl; 441 struct scatterlist *sg; 442 u32 flags = 0, cnt, curcnt; 443 u64 length = 0; 444 445 efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_WRITE_PENDING); 446 efct_lio_io_printf(io, "trans_state=0x%x se_cmd_flags=0x%x\n", 447 cmd->transport_state, cmd->se_cmd_flags); 448 449 if (ocp->seg_cnt == 0) { 450 ocp->seg_cnt = cmd->t_data_nents; 451 ocp->cur_seg = 0; 452 if (efct_lio_sg_map(io)) { 453 efct_lio_io_printf(io, "efct_lio_sg_map failed\n"); 454 return -EFAULT; 455 } 456 } 457 curcnt = (ocp->seg_map_cnt - ocp->cur_seg); 458 curcnt = (curcnt < io->sgl_allocated) ? curcnt : io->sgl_allocated; 459 /* find current sg */ 460 for (cnt = 0, sg = cmd->t_data_sg; cnt < ocp->cur_seg; cnt++, 461 sg = sg_next(sg)) 462 ;/* do nothing */ 463 464 for (cnt = 0; cnt < curcnt; cnt++, sg = sg_next(sg)) { 465 sgl[cnt].addr = sg_dma_address(sg); 466 sgl[cnt].dif_addr = 0; 467 sgl[cnt].len = sg_dma_len(sg); 468 length += sgl[cnt].len; 469 ocp->cur_seg++; 470 } 471 472 if (ocp->cur_seg == ocp->seg_cnt) 473 flags = EFCT_SCSI_LAST_DATAPHASE; 474 475 return efct_scsi_recv_wr_data(io, flags, sgl, curcnt, length, 476 efct_lio_datamove_done, NULL); 477 } 478 479 static int 480 efct_lio_queue_data_in(struct se_cmd *cmd) 481 { 482 struct efct_scsi_tgt_io *ocp = 483 container_of(cmd, struct efct_scsi_tgt_io, cmd); 484 struct efct_io *io = container_of(ocp, struct efct_io, tgt_io); 485 struct efct_scsi_sgl *sgl = io->sgl; 486 struct scatterlist *sg = NULL; 487 uint flags = 0, cnt = 0, curcnt = 0; 488 u64 length = 0; 489 490 efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_QUEUE_DATA_IN); 491 492 if (ocp->seg_cnt == 0) { 493 if (cmd->data_length) { 494 ocp->seg_cnt = cmd->t_data_nents; 495 ocp->cur_seg = 0; 496 if (efct_lio_sg_map(io)) { 497 efct_lio_io_printf(io, 498 "efct_lio_sg_map failed\n"); 499 return -EAGAIN; 500 } 501 } else { 502 /* If command length is 0, send the response status */ 503 struct efct_scsi_cmd_resp rsp; 504 505 memset(&rsp, 0, sizeof(rsp)); 506 efct_lio_io_printf(io, 507 "cmd : %p length 0, send status\n", 508 cmd); 509 return efct_scsi_send_resp(io, 0, &rsp, 510 efct_lio_status_done, NULL); 511 } 512 } 513 curcnt = min(ocp->seg_map_cnt - ocp->cur_seg, io->sgl_allocated); 514 515 while (cnt < curcnt) { 516 sg = &cmd->t_data_sg[ocp->cur_seg]; 517 sgl[cnt].addr = sg_dma_address(sg); 518 sgl[cnt].dif_addr = 0; 519 if (ocp->transferred_len + sg_dma_len(sg) >= cmd->data_length) 520 sgl[cnt].len = cmd->data_length - ocp->transferred_len; 521 else 522 sgl[cnt].len = sg_dma_len(sg); 523 524 ocp->transferred_len += sgl[cnt].len; 525 length += sgl[cnt].len; 526 ocp->cur_seg++; 527 cnt++; 528 if (ocp->transferred_len == cmd->data_length) 529 break; 530 } 531 532 if (ocp->transferred_len == cmd->data_length) { 533 flags = EFCT_SCSI_LAST_DATAPHASE; 534 ocp->seg_cnt = ocp->cur_seg; 535 } 536 537 /* If there is residual, disable Auto Good Response */ 538 if (cmd->residual_count) 539 flags |= EFCT_SCSI_NO_AUTO_RESPONSE; 540 541 efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_SEND_RD_DATA); 542 543 return efct_scsi_send_rd_data(io, flags, sgl, curcnt, length, 544 efct_lio_datamove_done, NULL); 545 } 546 547 static void 548 efct_lio_send_resp(struct efct_io *io, enum efct_scsi_io_status scsi_status, 549 u32 flags) 550 { 551 struct efct_scsi_cmd_resp rsp; 552 struct efct_scsi_tgt_io *ocp = &io->tgt_io; 553 struct se_cmd *cmd = &io->tgt_io.cmd; 554 int rc; 555 556 if (flags & EFCT_SCSI_IO_CMPL_RSP_SENT) { 557 ocp->rsp_sent = true; 558 efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_GENERIC_FREE); 559 transport_generic_free_cmd(&io->tgt_io.cmd, 0); 560 return; 561 } 562 563 /* send check condition if an error occurred */ 564 memset(&rsp, 0, sizeof(rsp)); 565 rsp.scsi_status = cmd->scsi_status; 566 rsp.sense_data = (uint8_t *)io->tgt_io.sense_buffer; 567 rsp.sense_data_length = cmd->scsi_sense_length; 568 569 /* Check for residual underrun or overrun */ 570 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) 571 rsp.residual = -cmd->residual_count; 572 else if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) 573 rsp.residual = cmd->residual_count; 574 575 rc = efct_scsi_send_resp(io, 0, &rsp, efct_lio_status_done, NULL); 576 efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_SEND_RSP); 577 if (rc != 0) { 578 efct_lio_io_printf(io, "Read done, send rsp failed %d\n", rc); 579 efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_GENERIC_FREE); 580 transport_generic_free_cmd(&io->tgt_io.cmd, 0); 581 } else { 582 ocp->rsp_sent = true; 583 } 584 } 585 586 static int 587 efct_lio_datamove_done(struct efct_io *io, enum efct_scsi_io_status scsi_status, 588 u32 flags, void *arg) 589 { 590 struct efct_scsi_tgt_io *ocp = &io->tgt_io; 591 592 efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_DATA_DONE); 593 if (scsi_status != EFCT_SCSI_STATUS_GOOD) { 594 efct_lio_io_printf(io, "callback completed with error=%d\n", 595 scsi_status); 596 ocp->err = scsi_status; 597 } 598 efct_lio_io_printf(io, "seg_map_cnt=%d\n", ocp->seg_map_cnt); 599 if (ocp->seg_map_cnt) { 600 if (ocp->err == EFCT_SCSI_STATUS_GOOD && 601 ocp->cur_seg < ocp->seg_cnt) { 602 int rc; 603 604 efct_lio_io_printf(io, "continuing cmd at segm=%d\n", 605 ocp->cur_seg); 606 if (ocp->ddir == DMA_TO_DEVICE) 607 rc = efct_lio_write_pending(&ocp->cmd); 608 else 609 rc = efct_lio_queue_data_in(&ocp->cmd); 610 if (!rc) 611 return 0; 612 613 ocp->err = EFCT_SCSI_STATUS_ERROR; 614 efct_lio_io_printf(io, "could not continue command\n"); 615 } 616 efct_lio_sg_unmap(io); 617 } 618 619 if (io->tgt_io.aborting) { 620 efct_lio_io_printf(io, "IO done aborted\n"); 621 return 0; 622 } 623 624 if (ocp->ddir == DMA_TO_DEVICE) { 625 efct_lio_io_printf(io, "Write done, trans_state=0x%x\n", 626 io->tgt_io.cmd.transport_state); 627 if (scsi_status != EFCT_SCSI_STATUS_GOOD) { 628 transport_generic_request_failure(&io->tgt_io.cmd, 629 TCM_CHECK_CONDITION_ABORT_CMD); 630 efct_set_lio_io_state(io, 631 EFCT_LIO_STATE_TGT_GENERIC_REQ_FAILURE); 632 } else { 633 efct_set_lio_io_state(io, 634 EFCT_LIO_STATE_TGT_EXECUTE_CMD); 635 target_execute_cmd(&io->tgt_io.cmd); 636 } 637 } else { 638 efct_lio_send_resp(io, scsi_status, flags); 639 } 640 return 0; 641 } 642 643 static int 644 efct_lio_tmf_done(struct efct_io *io, enum efct_scsi_io_status scsi_status, 645 u32 flags, void *arg) 646 { 647 efct_lio_tmfio_printf(io, "cmd=%p status=%d, flags=0x%x\n", 648 &io->tgt_io.cmd, scsi_status, flags); 649 650 efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_GENERIC_FREE); 651 transport_generic_free_cmd(&io->tgt_io.cmd, 0); 652 return 0; 653 } 654 655 static int 656 efct_lio_null_tmf_done(struct efct_io *tmfio, 657 enum efct_scsi_io_status scsi_status, 658 u32 flags, void *arg) 659 { 660 efct_lio_tmfio_printf(tmfio, "cmd=%p status=%d, flags=0x%x\n", 661 &tmfio->tgt_io.cmd, scsi_status, flags); 662 663 /* free struct efct_io only, no active se_cmd */ 664 efct_scsi_io_complete(tmfio); 665 return 0; 666 } 667 668 static int 669 efct_lio_queue_status(struct se_cmd *cmd) 670 { 671 struct efct_scsi_cmd_resp rsp; 672 struct efct_scsi_tgt_io *ocp = 673 container_of(cmd, struct efct_scsi_tgt_io, cmd); 674 struct efct_io *io = container_of(ocp, struct efct_io, tgt_io); 675 int rc = 0; 676 677 efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_QUEUE_STATUS); 678 efct_lio_io_printf(io, 679 "status=0x%x trans_state=0x%x se_cmd_flags=0x%x sns_len=%d\n", 680 cmd->scsi_status, cmd->transport_state, cmd->se_cmd_flags, 681 cmd->scsi_sense_length); 682 683 memset(&rsp, 0, sizeof(rsp)); 684 rsp.scsi_status = cmd->scsi_status; 685 rsp.sense_data = (u8 *)io->tgt_io.sense_buffer; 686 rsp.sense_data_length = cmd->scsi_sense_length; 687 688 /* Check for residual underrun or overrun, mark negitive value for 689 * underrun to recognize in HW 690 */ 691 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) 692 rsp.residual = -cmd->residual_count; 693 else if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) 694 rsp.residual = cmd->residual_count; 695 696 rc = efct_scsi_send_resp(io, 0, &rsp, efct_lio_status_done, NULL); 697 efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_SEND_RSP); 698 if (rc == 0) 699 ocp->rsp_sent = true; 700 return rc; 701 } 702 703 static void efct_lio_queue_tm_rsp(struct se_cmd *cmd) 704 { 705 struct efct_scsi_tgt_io *ocp = 706 container_of(cmd, struct efct_scsi_tgt_io, cmd); 707 struct efct_io *tmfio = container_of(ocp, struct efct_io, tgt_io); 708 struct se_tmr_req *se_tmr = cmd->se_tmr_req; 709 u8 rspcode; 710 711 efct_lio_tmfio_printf(tmfio, "cmd=%p function=0x%x tmr->response=%d\n", 712 cmd, se_tmr->function, se_tmr->response); 713 switch (se_tmr->response) { 714 case TMR_FUNCTION_COMPLETE: 715 rspcode = EFCT_SCSI_TMF_FUNCTION_COMPLETE; 716 break; 717 case TMR_TASK_DOES_NOT_EXIST: 718 rspcode = EFCT_SCSI_TMF_FUNCTION_IO_NOT_FOUND; 719 break; 720 case TMR_LUN_DOES_NOT_EXIST: 721 rspcode = EFCT_SCSI_TMF_INCORRECT_LOGICAL_UNIT_NUMBER; 722 break; 723 case TMR_FUNCTION_REJECTED: 724 default: 725 rspcode = EFCT_SCSI_TMF_FUNCTION_REJECTED; 726 break; 727 } 728 efct_scsi_send_tmf_resp(tmfio, rspcode, NULL, efct_lio_tmf_done, NULL); 729 } 730 731 static struct efct *efct_find_wwpn(u64 wwpn) 732 { 733 struct efct *efct; 734 735 /* Search for the HBA that has this WWPN */ 736 list_for_each_entry(efct, &efct_devices, list_entry) { 737 738 if (wwpn == efct_get_wwpn(&efct->hw)) 739 return efct; 740 } 741 742 return NULL; 743 } 744 745 static struct se_wwn * 746 efct_lio_make_nport(struct target_fabric_configfs *tf, 747 struct config_group *group, const char *name) 748 { 749 struct efct_lio_nport *lio_nport; 750 struct efct *efct; 751 int ret; 752 u64 wwpn; 753 754 ret = efct_lio_parse_wwn(name, &wwpn, 0); 755 if (ret) 756 return ERR_PTR(ret); 757 758 efct = efct_find_wwpn(wwpn); 759 if (!efct) { 760 pr_err("cannot find EFCT for base wwpn %s\n", name); 761 return ERR_PTR(-ENXIO); 762 } 763 764 lio_nport = kzalloc(sizeof(*lio_nport), GFP_KERNEL); 765 if (!lio_nport) 766 return ERR_PTR(-ENOMEM); 767 768 lio_nport->efct = efct; 769 lio_nport->wwpn = wwpn; 770 efct_format_wwn(lio_nport->wwpn_str, sizeof(lio_nport->wwpn_str), 771 "naa.", wwpn); 772 efct->tgt_efct.lio_nport = lio_nport; 773 774 return &lio_nport->nport_wwn; 775 } 776 777 static struct se_wwn * 778 efct_lio_npiv_make_nport(struct target_fabric_configfs *tf, 779 struct config_group *group, const char *name) 780 { 781 struct efct_lio_vport *lio_vport; 782 struct efct *efct; 783 int ret; 784 u64 p_wwpn, npiv_wwpn, npiv_wwnn; 785 char *p, *pbuf, tmp[128]; 786 struct efct_lio_vport_list_t *vport_list; 787 struct fc_vport *new_fc_vport; 788 struct fc_vport_identifiers vport_id; 789 unsigned long flags = 0; 790 791 snprintf(tmp, sizeof(tmp), "%s", name); 792 pbuf = &tmp[0]; 793 794 p = strsep(&pbuf, "@"); 795 796 if (!p || !pbuf) { 797 pr_err("Unable to find separator operator(@)\n"); 798 return ERR_PTR(-EINVAL); 799 } 800 801 ret = efct_lio_parse_wwn(p, &p_wwpn, 0); 802 if (ret) 803 return ERR_PTR(ret); 804 805 ret = efct_lio_parse_npiv_wwn(pbuf, strlen(pbuf), &npiv_wwpn, 806 &npiv_wwnn); 807 if (ret) 808 return ERR_PTR(ret); 809 810 efct = efct_find_wwpn(p_wwpn); 811 if (!efct) { 812 pr_err("cannot find EFCT for base wwpn %s\n", name); 813 return ERR_PTR(-ENXIO); 814 } 815 816 lio_vport = kzalloc(sizeof(*lio_vport), GFP_KERNEL); 817 if (!lio_vport) 818 return ERR_PTR(-ENOMEM); 819 820 lio_vport->efct = efct; 821 lio_vport->wwpn = p_wwpn; 822 lio_vport->npiv_wwpn = npiv_wwpn; 823 lio_vport->npiv_wwnn = npiv_wwnn; 824 825 efct_format_wwn(lio_vport->wwpn_str, sizeof(lio_vport->wwpn_str), 826 "naa.", npiv_wwpn); 827 828 vport_list = kzalloc(sizeof(*vport_list), GFP_KERNEL); 829 if (!vport_list) { 830 kfree(lio_vport); 831 return ERR_PTR(-ENOMEM); 832 } 833 834 vport_list->lio_vport = lio_vport; 835 836 memset(&vport_id, 0, sizeof(vport_id)); 837 vport_id.port_name = npiv_wwpn; 838 vport_id.node_name = npiv_wwnn; 839 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR; 840 vport_id.vport_type = FC_PORTTYPE_NPIV; 841 vport_id.disable = false; 842 843 new_fc_vport = fc_vport_create(efct->shost, 0, &vport_id); 844 if (!new_fc_vport) { 845 efc_log_err(efct, "fc_vport_create failed\n"); 846 kfree(lio_vport); 847 kfree(vport_list); 848 return ERR_PTR(-ENOMEM); 849 } 850 851 lio_vport->fc_vport = new_fc_vport; 852 spin_lock_irqsave(&efct->tgt_efct.efct_lio_lock, flags); 853 INIT_LIST_HEAD(&vport_list->list_entry); 854 list_add_tail(&vport_list->list_entry, &efct->tgt_efct.vport_list); 855 spin_unlock_irqrestore(&efct->tgt_efct.efct_lio_lock, flags); 856 857 return &lio_vport->vport_wwn; 858 } 859 860 static void 861 efct_lio_drop_nport(struct se_wwn *wwn) 862 { 863 struct efct_lio_nport *lio_nport = 864 container_of(wwn, struct efct_lio_nport, nport_wwn); 865 struct efct *efct = lio_nport->efct; 866 867 /* only physical nport should exist, free lio_nport allocated 868 * in efct_lio_make_nport. 869 */ 870 kfree(efct->tgt_efct.lio_nport); 871 efct->tgt_efct.lio_nport = NULL; 872 } 873 874 static void 875 efct_lio_npiv_drop_nport(struct se_wwn *wwn) 876 { 877 struct efct_lio_vport *lio_vport = 878 container_of(wwn, struct efct_lio_vport, vport_wwn); 879 struct efct_lio_vport_list_t *vport, *next_vport; 880 struct efct *efct = lio_vport->efct; 881 unsigned long flags = 0; 882 883 if (lio_vport->fc_vport) 884 fc_vport_terminate(lio_vport->fc_vport); 885 886 spin_lock_irqsave(&efct->tgt_efct.efct_lio_lock, flags); 887 888 list_for_each_entry_safe(vport, next_vport, &efct->tgt_efct.vport_list, 889 list_entry) { 890 if (vport->lio_vport == lio_vport) { 891 list_del(&vport->list_entry); 892 kfree(vport->lio_vport); 893 kfree(vport); 894 break; 895 } 896 } 897 spin_unlock_irqrestore(&efct->tgt_efct.efct_lio_lock, flags); 898 } 899 900 static struct se_portal_group * 901 efct_lio_make_tpg(struct se_wwn *wwn, const char *name) 902 { 903 struct efct_lio_nport *lio_nport = 904 container_of(wwn, struct efct_lio_nport, nport_wwn); 905 struct efct_lio_tpg *tpg; 906 struct efct *efct; 907 unsigned long n; 908 int ret; 909 910 if (strstr(name, "tpgt_") != name) 911 return ERR_PTR(-EINVAL); 912 if (kstrtoul(name + 5, 10, &n) || n > USHRT_MAX) 913 return ERR_PTR(-EINVAL); 914 915 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL); 916 if (!tpg) 917 return ERR_PTR(-ENOMEM); 918 919 tpg->nport = lio_nport; 920 tpg->tpgt = n; 921 tpg->enabled = false; 922 923 tpg->tpg_attrib.generate_node_acls = 1; 924 tpg->tpg_attrib.demo_mode_write_protect = 1; 925 tpg->tpg_attrib.cache_dynamic_acls = 1; 926 tpg->tpg_attrib.demo_mode_login_only = 1; 927 tpg->tpg_attrib.session_deletion_wait = 1; 928 929 ret = core_tpg_register(wwn, &tpg->tpg, SCSI_PROTOCOL_FCP); 930 if (ret < 0) { 931 kfree(tpg); 932 return NULL; 933 } 934 efct = lio_nport->efct; 935 efct->tgt_efct.tpg = tpg; 936 efc_log_debug(efct, "create portal group %d\n", tpg->tpgt); 937 938 xa_init(&efct->lookup); 939 return &tpg->tpg; 940 } 941 942 static void 943 efct_lio_drop_tpg(struct se_portal_group *se_tpg) 944 { 945 struct efct_lio_tpg *tpg = 946 container_of(se_tpg, struct efct_lio_tpg, tpg); 947 948 struct efct *efct = tpg->nport->efct; 949 950 efc_log_debug(efct, "drop portal group %d\n", tpg->tpgt); 951 tpg->nport->efct->tgt_efct.tpg = NULL; 952 core_tpg_deregister(se_tpg); 953 xa_destroy(&efct->lookup); 954 kfree(tpg); 955 } 956 957 static struct se_portal_group * 958 efct_lio_npiv_make_tpg(struct se_wwn *wwn, const char *name) 959 { 960 struct efct_lio_vport *lio_vport = 961 container_of(wwn, struct efct_lio_vport, vport_wwn); 962 struct efct_lio_tpg *tpg; 963 struct efct *efct; 964 unsigned long n; 965 int ret; 966 967 efct = lio_vport->efct; 968 if (strstr(name, "tpgt_") != name) 969 return ERR_PTR(-EINVAL); 970 if (kstrtoul(name + 5, 10, &n) || n > USHRT_MAX) 971 return ERR_PTR(-EINVAL); 972 973 if (n != 1) { 974 efc_log_err(efct, "Invalid tpgt index: %ld provided\n", n); 975 return ERR_PTR(-EINVAL); 976 } 977 978 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL); 979 if (!tpg) 980 return ERR_PTR(-ENOMEM); 981 982 tpg->vport = lio_vport; 983 tpg->tpgt = n; 984 tpg->enabled = false; 985 986 tpg->tpg_attrib.generate_node_acls = 1; 987 tpg->tpg_attrib.demo_mode_write_protect = 1; 988 tpg->tpg_attrib.cache_dynamic_acls = 1; 989 tpg->tpg_attrib.demo_mode_login_only = 1; 990 tpg->tpg_attrib.session_deletion_wait = 1; 991 992 ret = core_tpg_register(wwn, &tpg->tpg, SCSI_PROTOCOL_FCP); 993 994 if (ret < 0) { 995 kfree(tpg); 996 return NULL; 997 } 998 lio_vport->tpg = tpg; 999 efc_log_debug(efct, "create vport portal group %d\n", tpg->tpgt); 1000 1001 return &tpg->tpg; 1002 } 1003 1004 static void 1005 efct_lio_npiv_drop_tpg(struct se_portal_group *se_tpg) 1006 { 1007 struct efct_lio_tpg *tpg = 1008 container_of(se_tpg, struct efct_lio_tpg, tpg); 1009 1010 efc_log_debug(tpg->vport->efct, "drop npiv portal group %d\n", 1011 tpg->tpgt); 1012 core_tpg_deregister(se_tpg); 1013 kfree(tpg); 1014 } 1015 1016 static int 1017 efct_lio_init_nodeacl(struct se_node_acl *se_nacl, const char *name) 1018 { 1019 struct efct_lio_nacl *nacl; 1020 u64 wwnn; 1021 1022 if (efct_lio_parse_wwn(name, &wwnn, 0) < 0) 1023 return -EINVAL; 1024 1025 nacl = container_of(se_nacl, struct efct_lio_nacl, se_node_acl); 1026 nacl->nport_wwnn = wwnn; 1027 1028 efct_format_wwn(nacl->nport_name, sizeof(nacl->nport_name), "", wwnn); 1029 return 0; 1030 } 1031 1032 static int efct_lio_check_demo_mode_login_only(struct se_portal_group *stpg) 1033 { 1034 struct efct_lio_tpg *tpg = container_of(stpg, struct efct_lio_tpg, tpg); 1035 1036 return tpg->tpg_attrib.demo_mode_login_only; 1037 } 1038 1039 static int 1040 efct_lio_npiv_check_demo_mode_login_only(struct se_portal_group *stpg) 1041 { 1042 struct efct_lio_tpg *tpg = container_of(stpg, struct efct_lio_tpg, tpg); 1043 1044 return tpg->tpg_attrib.demo_mode_login_only; 1045 } 1046 1047 static struct efct_lio_tpg * 1048 efct_get_vport_tpg(struct efc_node *node) 1049 { 1050 struct efct *efct; 1051 u64 wwpn = node->nport->wwpn; 1052 struct efct_lio_vport_list_t *vport, *next; 1053 struct efct_lio_vport *lio_vport = NULL; 1054 struct efct_lio_tpg *tpg = NULL; 1055 unsigned long flags = 0; 1056 1057 efct = node->efc->base; 1058 spin_lock_irqsave(&efct->tgt_efct.efct_lio_lock, flags); 1059 list_for_each_entry_safe(vport, next, &efct->tgt_efct.vport_list, 1060 list_entry) { 1061 lio_vport = vport->lio_vport; 1062 if (wwpn && lio_vport && lio_vport->npiv_wwpn == wwpn) { 1063 efc_log_debug(efct, "found tpg on vport\n"); 1064 tpg = lio_vport->tpg; 1065 break; 1066 } 1067 } 1068 spin_unlock_irqrestore(&efct->tgt_efct.efct_lio_lock, flags); 1069 return tpg; 1070 } 1071 1072 static void 1073 _efct_tgt_node_free(struct kref *arg) 1074 { 1075 struct efct_node *tgt_node = container_of(arg, struct efct_node, ref); 1076 struct efc_node *node = tgt_node->node; 1077 1078 efc_scsi_del_initiator_complete(node->efc, node); 1079 kfree(tgt_node); 1080 } 1081 1082 static int efct_session_cb(struct se_portal_group *se_tpg, 1083 struct se_session *se_sess, void *private) 1084 { 1085 struct efc_node *node = private; 1086 struct efct_node *tgt_node; 1087 struct efct *efct = node->efc->base; 1088 1089 tgt_node = kzalloc(sizeof(*tgt_node), GFP_KERNEL); 1090 if (!tgt_node) 1091 return -ENOMEM; 1092 1093 kref_init(&tgt_node->ref); 1094 tgt_node->release = _efct_tgt_node_free; 1095 1096 tgt_node->session = se_sess; 1097 node->tgt_node = tgt_node; 1098 tgt_node->efct = efct; 1099 1100 tgt_node->node = node; 1101 1102 tgt_node->node_fc_id = node->rnode.fc_id; 1103 tgt_node->port_fc_id = node->nport->fc_id; 1104 tgt_node->vpi = node->nport->indicator; 1105 tgt_node->rpi = node->rnode.indicator; 1106 1107 spin_lock_init(&tgt_node->active_ios_lock); 1108 INIT_LIST_HEAD(&tgt_node->active_ios); 1109 1110 return 0; 1111 } 1112 1113 int efct_scsi_tgt_new_device(struct efct *efct) 1114 { 1115 u32 total_ios; 1116 1117 /* Get the max settings */ 1118 efct->tgt_efct.max_sge = sli_get_max_sge(&efct->hw.sli); 1119 efct->tgt_efct.max_sgl = sli_get_max_sgl(&efct->hw.sli); 1120 1121 /* initialize IO watermark fields */ 1122 atomic_set(&efct->tgt_efct.ios_in_use, 0); 1123 total_ios = efct->hw.config.n_io; 1124 efc_log_debug(efct, "total_ios=%d\n", total_ios); 1125 efct->tgt_efct.watermark_min = 1126 (total_ios * EFCT_WATERMARK_LOW_PCT) / 100; 1127 efct->tgt_efct.watermark_max = 1128 (total_ios * EFCT_WATERMARK_HIGH_PCT) / 100; 1129 atomic_set(&efct->tgt_efct.io_high_watermark, 1130 efct->tgt_efct.watermark_max); 1131 atomic_set(&efct->tgt_efct.watermark_hit, 0); 1132 atomic_set(&efct->tgt_efct.initiator_count, 0); 1133 1134 lio_wq = create_singlethread_workqueue("efct_lio_worker"); 1135 if (!lio_wq) { 1136 efc_log_err(efct, "workqueue create failed\n"); 1137 return -EIO; 1138 } 1139 1140 spin_lock_init(&efct->tgt_efct.efct_lio_lock); 1141 INIT_LIST_HEAD(&efct->tgt_efct.vport_list); 1142 1143 return 0; 1144 } 1145 1146 int efct_scsi_tgt_del_device(struct efct *efct) 1147 { 1148 flush_workqueue(lio_wq); 1149 1150 return 0; 1151 } 1152 1153 int 1154 efct_scsi_tgt_new_nport(struct efc *efc, struct efc_nport *nport) 1155 { 1156 struct efct *efct = nport->efc->base; 1157 1158 efc_log_debug(efct, "New SPORT: %s bound to %s\n", nport->display_name, 1159 efct->tgt_efct.lio_nport->wwpn_str); 1160 1161 return 0; 1162 } 1163 1164 void 1165 efct_scsi_tgt_del_nport(struct efc *efc, struct efc_nport *nport) 1166 { 1167 efc_log_debug(efc, "Del SPORT: %s\n", nport->display_name); 1168 } 1169 1170 static void efct_lio_setup_session(struct work_struct *work) 1171 { 1172 struct efct_lio_wq_data *wq_data = 1173 container_of(work, struct efct_lio_wq_data, work); 1174 struct efct *efct = wq_data->efct; 1175 struct efc_node *node = wq_data->ptr; 1176 char wwpn[WWN_NAME_LEN]; 1177 struct efct_lio_tpg *tpg; 1178 struct efct_node *tgt_node; 1179 struct se_portal_group *se_tpg; 1180 struct se_session *se_sess; 1181 int watermark; 1182 int ini_count; 1183 u64 id; 1184 1185 /* Check to see if it's belongs to vport, 1186 * if not get physical port 1187 */ 1188 tpg = efct_get_vport_tpg(node); 1189 if (tpg) { 1190 se_tpg = &tpg->tpg; 1191 } else if (efct->tgt_efct.tpg) { 1192 tpg = efct->tgt_efct.tpg; 1193 se_tpg = &tpg->tpg; 1194 } else { 1195 efc_log_err(efct, "failed to init session\n"); 1196 return; 1197 } 1198 1199 /* 1200 * Format the FCP Initiator port_name into colon 1201 * separated values to match the format by our explicit 1202 * ConfigFS NodeACLs. 1203 */ 1204 efct_format_wwn(wwpn, sizeof(wwpn), "", efc_node_get_wwpn(node)); 1205 1206 se_sess = target_setup_session(se_tpg, 0, 0, TARGET_PROT_NORMAL, wwpn, 1207 node, efct_session_cb); 1208 if (IS_ERR(se_sess)) { 1209 efc_log_err(efct, "failed to setup session\n"); 1210 kfree(wq_data); 1211 efc_scsi_sess_reg_complete(node, -EIO); 1212 return; 1213 } 1214 1215 tgt_node = node->tgt_node; 1216 id = (u64) tgt_node->port_fc_id << 32 | tgt_node->node_fc_id; 1217 1218 efc_log_debug(efct, "new initiator sess=%p node=%p id: %llx\n", 1219 se_sess, node, id); 1220 1221 if (xa_err(xa_store(&efct->lookup, id, tgt_node, GFP_KERNEL))) 1222 efc_log_err(efct, "Node lookup store failed\n"); 1223 1224 efc_scsi_sess_reg_complete(node, 0); 1225 1226 /* update IO watermark: increment initiator count */ 1227 ini_count = atomic_add_return(1, &efct->tgt_efct.initiator_count); 1228 watermark = efct->tgt_efct.watermark_max - 1229 ini_count * EFCT_IO_WATERMARK_PER_INITIATOR; 1230 watermark = (efct->tgt_efct.watermark_min > watermark) ? 1231 efct->tgt_efct.watermark_min : watermark; 1232 atomic_set(&efct->tgt_efct.io_high_watermark, watermark); 1233 1234 kfree(wq_data); 1235 } 1236 1237 int efct_scsi_new_initiator(struct efc *efc, struct efc_node *node) 1238 { 1239 struct efct *efct = node->efc->base; 1240 struct efct_lio_wq_data *wq_data; 1241 1242 /* 1243 * Since LIO only supports initiator validation at thread level, 1244 * we are open minded and accept all callers. 1245 */ 1246 wq_data = kzalloc(sizeof(*wq_data), GFP_ATOMIC); 1247 if (!wq_data) 1248 return -ENOMEM; 1249 1250 wq_data->ptr = node; 1251 wq_data->efct = efct; 1252 INIT_WORK(&wq_data->work, efct_lio_setup_session); 1253 queue_work(lio_wq, &wq_data->work); 1254 return EFC_SCSI_CALL_ASYNC; 1255 } 1256 1257 static void efct_lio_remove_session(struct work_struct *work) 1258 { 1259 struct efct_lio_wq_data *wq_data = 1260 container_of(work, struct efct_lio_wq_data, work); 1261 struct efct *efct = wq_data->efct; 1262 struct efc_node *node = wq_data->ptr; 1263 struct efct_node *tgt_node; 1264 struct se_session *se_sess; 1265 1266 tgt_node = node->tgt_node; 1267 if (!tgt_node) { 1268 /* base driver has sent back-to-back requests 1269 * to unreg session with no intervening 1270 * register 1271 */ 1272 efc_log_err(efct, "unreg session for NULL session\n"); 1273 efc_scsi_del_initiator_complete(node->efc, node); 1274 return; 1275 } 1276 1277 se_sess = tgt_node->session; 1278 efc_log_debug(efct, "unreg session se_sess=%p node=%p\n", 1279 se_sess, node); 1280 1281 /* first flag all session commands to complete */ 1282 target_stop_session(se_sess); 1283 1284 /* now wait for session commands to complete */ 1285 target_wait_for_sess_cmds(se_sess); 1286 target_remove_session(se_sess); 1287 tgt_node->session = NULL; 1288 node->tgt_node = NULL; 1289 kref_put(&tgt_node->ref, tgt_node->release); 1290 1291 kfree(wq_data); 1292 } 1293 1294 int efct_scsi_del_initiator(struct efc *efc, struct efc_node *node, int reason) 1295 { 1296 struct efct *efct = node->efc->base; 1297 struct efct_node *tgt_node = node->tgt_node; 1298 struct efct_lio_wq_data *wq_data; 1299 int watermark; 1300 int ini_count; 1301 u64 id; 1302 1303 if (reason == EFCT_SCSI_INITIATOR_MISSING) 1304 return EFC_SCSI_CALL_COMPLETE; 1305 1306 if (!tgt_node) { 1307 efc_log_err(efct, "tgt_node is NULL\n"); 1308 return -EIO; 1309 } 1310 1311 wq_data = kzalloc(sizeof(*wq_data), GFP_ATOMIC); 1312 if (!wq_data) 1313 return -ENOMEM; 1314 1315 id = (u64) tgt_node->port_fc_id << 32 | tgt_node->node_fc_id; 1316 xa_erase(&efct->lookup, id); 1317 1318 wq_data->ptr = node; 1319 wq_data->efct = efct; 1320 INIT_WORK(&wq_data->work, efct_lio_remove_session); 1321 queue_work(lio_wq, &wq_data->work); 1322 1323 /* 1324 * update IO watermark: decrement initiator count 1325 */ 1326 ini_count = atomic_sub_return(1, &efct->tgt_efct.initiator_count); 1327 1328 watermark = efct->tgt_efct.watermark_max - 1329 ini_count * EFCT_IO_WATERMARK_PER_INITIATOR; 1330 watermark = (efct->tgt_efct.watermark_min > watermark) ? 1331 efct->tgt_efct.watermark_min : watermark; 1332 atomic_set(&efct->tgt_efct.io_high_watermark, watermark); 1333 1334 return EFC_SCSI_CALL_ASYNC; 1335 } 1336 1337 void efct_scsi_recv_cmd(struct efct_io *io, uint64_t lun, u8 *cdb, 1338 u32 cdb_len, u32 flags) 1339 { 1340 struct efct_scsi_tgt_io *ocp = &io->tgt_io; 1341 struct se_cmd *se_cmd = &io->tgt_io.cmd; 1342 struct efct *efct = io->efct; 1343 char *ddir; 1344 struct efct_node *tgt_node; 1345 struct se_session *se_sess; 1346 int rc = 0; 1347 1348 memset(ocp, 0, sizeof(struct efct_scsi_tgt_io)); 1349 efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_RECV_CMD); 1350 atomic_add_return(1, &efct->tgt_efct.ios_in_use); 1351 1352 /* set target timeout */ 1353 io->timeout = efct->target_io_timer_sec; 1354 1355 if (flags & EFCT_SCSI_CMD_SIMPLE) 1356 ocp->task_attr = TCM_SIMPLE_TAG; 1357 else if (flags & EFCT_SCSI_CMD_HEAD_OF_QUEUE) 1358 ocp->task_attr = TCM_HEAD_TAG; 1359 else if (flags & EFCT_SCSI_CMD_ORDERED) 1360 ocp->task_attr = TCM_ORDERED_TAG; 1361 else if (flags & EFCT_SCSI_CMD_ACA) 1362 ocp->task_attr = TCM_ACA_TAG; 1363 1364 switch (flags & (EFCT_SCSI_CMD_DIR_IN | EFCT_SCSI_CMD_DIR_OUT)) { 1365 case EFCT_SCSI_CMD_DIR_IN: 1366 ddir = "FROM_INITIATOR"; 1367 ocp->ddir = DMA_TO_DEVICE; 1368 break; 1369 case EFCT_SCSI_CMD_DIR_OUT: 1370 ddir = "TO_INITIATOR"; 1371 ocp->ddir = DMA_FROM_DEVICE; 1372 break; 1373 case EFCT_SCSI_CMD_DIR_IN | EFCT_SCSI_CMD_DIR_OUT: 1374 ddir = "BIDIR"; 1375 ocp->ddir = DMA_BIDIRECTIONAL; 1376 break; 1377 default: 1378 ddir = "NONE"; 1379 ocp->ddir = DMA_NONE; 1380 break; 1381 } 1382 1383 ocp->lun = lun; 1384 efct_lio_io_printf(io, "new cmd=0x%x ddir=%s dl=%u\n", 1385 cdb[0], ddir, io->exp_xfer_len); 1386 1387 tgt_node = io->node; 1388 se_sess = tgt_node->session; 1389 if (!se_sess) { 1390 efc_log_err(efct, "No session found to submit IO se_cmd: %p\n", 1391 &ocp->cmd); 1392 efct_scsi_io_free(io); 1393 return; 1394 } 1395 1396 efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_SUBMIT_CMD); 1397 rc = target_init_cmd(se_cmd, se_sess, &io->tgt_io.sense_buffer[0], 1398 ocp->lun, io->exp_xfer_len, ocp->task_attr, 1399 ocp->ddir, TARGET_SCF_ACK_KREF); 1400 if (rc) { 1401 efc_log_err(efct, "failed to init cmd se_cmd: %p\n", se_cmd); 1402 efct_scsi_io_free(io); 1403 return; 1404 } 1405 1406 if (target_submit_prep(se_cmd, cdb, NULL, 0, NULL, 0, 1407 NULL, 0, GFP_ATOMIC)) 1408 return; 1409 1410 target_submit(se_cmd); 1411 } 1412 1413 int 1414 efct_scsi_recv_tmf(struct efct_io *tmfio, u32 lun, enum efct_scsi_tmf_cmd cmd, 1415 struct efct_io *io_to_abort, u32 flags) 1416 { 1417 unsigned char tmr_func; 1418 struct efct *efct = tmfio->efct; 1419 struct efct_scsi_tgt_io *ocp = &tmfio->tgt_io; 1420 struct efct_node *tgt_node; 1421 struct se_session *se_sess; 1422 int rc; 1423 1424 memset(ocp, 0, sizeof(struct efct_scsi_tgt_io)); 1425 efct_set_lio_io_state(tmfio, EFCT_LIO_STATE_SCSI_RECV_TMF); 1426 atomic_add_return(1, &efct->tgt_efct.ios_in_use); 1427 efct_lio_tmfio_printf(tmfio, "%s: new tmf %x lun=%u\n", 1428 tmfio->display_name, cmd, lun); 1429 1430 switch (cmd) { 1431 case EFCT_SCSI_TMF_ABORT_TASK: 1432 tmr_func = TMR_ABORT_TASK; 1433 break; 1434 case EFCT_SCSI_TMF_ABORT_TASK_SET: 1435 tmr_func = TMR_ABORT_TASK_SET; 1436 break; 1437 case EFCT_SCSI_TMF_CLEAR_TASK_SET: 1438 tmr_func = TMR_CLEAR_TASK_SET; 1439 break; 1440 case EFCT_SCSI_TMF_LOGICAL_UNIT_RESET: 1441 tmr_func = TMR_LUN_RESET; 1442 break; 1443 case EFCT_SCSI_TMF_CLEAR_ACA: 1444 tmr_func = TMR_CLEAR_ACA; 1445 break; 1446 case EFCT_SCSI_TMF_TARGET_RESET: 1447 tmr_func = TMR_TARGET_WARM_RESET; 1448 break; 1449 case EFCT_SCSI_TMF_QUERY_ASYNCHRONOUS_EVENT: 1450 case EFCT_SCSI_TMF_QUERY_TASK_SET: 1451 default: 1452 goto tmf_fail; 1453 } 1454 1455 tmfio->tgt_io.tmf = tmr_func; 1456 tmfio->tgt_io.lun = lun; 1457 tmfio->tgt_io.io_to_abort = io_to_abort; 1458 1459 tgt_node = tmfio->node; 1460 1461 se_sess = tgt_node->session; 1462 if (!se_sess) 1463 return 0; 1464 1465 rc = target_submit_tmr(&ocp->cmd, se_sess, NULL, lun, ocp, tmr_func, 1466 GFP_ATOMIC, tmfio->init_task_tag, TARGET_SCF_ACK_KREF); 1467 1468 efct_set_lio_io_state(tmfio, EFCT_LIO_STATE_TGT_SUBMIT_TMR); 1469 if (rc) 1470 goto tmf_fail; 1471 1472 return 0; 1473 1474 tmf_fail: 1475 efct_scsi_send_tmf_resp(tmfio, EFCT_SCSI_TMF_FUNCTION_REJECTED, 1476 NULL, efct_lio_null_tmf_done, NULL); 1477 return 0; 1478 } 1479 1480 /* Start items for efct_lio_tpg_attrib_cit */ 1481 1482 #define DEF_EFCT_TPG_ATTRIB(name) \ 1483 \ 1484 static ssize_t efct_lio_tpg_attrib_##name##_show( \ 1485 struct config_item *item, char *page) \ 1486 { \ 1487 struct se_portal_group *se_tpg = to_tpg(item); \ 1488 struct efct_lio_tpg *tpg = container_of(se_tpg, \ 1489 struct efct_lio_tpg, tpg); \ 1490 \ 1491 return sprintf(page, "%u\n", tpg->tpg_attrib.name); \ 1492 } \ 1493 \ 1494 static ssize_t efct_lio_tpg_attrib_##name##_store( \ 1495 struct config_item *item, const char *page, size_t count) \ 1496 { \ 1497 struct se_portal_group *se_tpg = to_tpg(item); \ 1498 struct efct_lio_tpg *tpg = container_of(se_tpg, \ 1499 struct efct_lio_tpg, tpg); \ 1500 struct efct_lio_tpg_attrib *a = &tpg->tpg_attrib; \ 1501 unsigned long val; \ 1502 int ret; \ 1503 \ 1504 ret = kstrtoul(page, 0, &val); \ 1505 if (ret < 0) { \ 1506 pr_err("kstrtoul() failed with ret: %d\n", ret); \ 1507 return ret; \ 1508 } \ 1509 \ 1510 if (val != 0 && val != 1) { \ 1511 pr_err("Illegal boolean value %lu\n", val); \ 1512 return -EINVAL; \ 1513 } \ 1514 \ 1515 a->name = val; \ 1516 \ 1517 return count; \ 1518 } \ 1519 CONFIGFS_ATTR(efct_lio_tpg_attrib_, name) 1520 1521 DEF_EFCT_TPG_ATTRIB(generate_node_acls); 1522 DEF_EFCT_TPG_ATTRIB(cache_dynamic_acls); 1523 DEF_EFCT_TPG_ATTRIB(demo_mode_write_protect); 1524 DEF_EFCT_TPG_ATTRIB(prod_mode_write_protect); 1525 DEF_EFCT_TPG_ATTRIB(demo_mode_login_only); 1526 DEF_EFCT_TPG_ATTRIB(session_deletion_wait); 1527 1528 static struct configfs_attribute *efct_lio_tpg_attrib_attrs[] = { 1529 &efct_lio_tpg_attrib_attr_generate_node_acls, 1530 &efct_lio_tpg_attrib_attr_cache_dynamic_acls, 1531 &efct_lio_tpg_attrib_attr_demo_mode_write_protect, 1532 &efct_lio_tpg_attrib_attr_prod_mode_write_protect, 1533 &efct_lio_tpg_attrib_attr_demo_mode_login_only, 1534 &efct_lio_tpg_attrib_attr_session_deletion_wait, 1535 NULL, 1536 }; 1537 1538 #define DEF_EFCT_NPIV_TPG_ATTRIB(name) \ 1539 \ 1540 static ssize_t efct_lio_npiv_tpg_attrib_##name##_show( \ 1541 struct config_item *item, char *page) \ 1542 { \ 1543 struct se_portal_group *se_tpg = to_tpg(item); \ 1544 struct efct_lio_tpg *tpg = container_of(se_tpg, \ 1545 struct efct_lio_tpg, tpg); \ 1546 \ 1547 return sprintf(page, "%u\n", tpg->tpg_attrib.name); \ 1548 } \ 1549 \ 1550 static ssize_t efct_lio_npiv_tpg_attrib_##name##_store( \ 1551 struct config_item *item, const char *page, size_t count) \ 1552 { \ 1553 struct se_portal_group *se_tpg = to_tpg(item); \ 1554 struct efct_lio_tpg *tpg = container_of(se_tpg, \ 1555 struct efct_lio_tpg, tpg); \ 1556 struct efct_lio_tpg_attrib *a = &tpg->tpg_attrib; \ 1557 unsigned long val; \ 1558 int ret; \ 1559 \ 1560 ret = kstrtoul(page, 0, &val); \ 1561 if (ret < 0) { \ 1562 pr_err("kstrtoul() failed with ret: %d\n", ret); \ 1563 return ret; \ 1564 } \ 1565 \ 1566 if (val != 0 && val != 1) { \ 1567 pr_err("Illegal boolean value %lu\n", val); \ 1568 return -EINVAL; \ 1569 } \ 1570 \ 1571 a->name = val; \ 1572 \ 1573 return count; \ 1574 } \ 1575 CONFIGFS_ATTR(efct_lio_npiv_tpg_attrib_, name) 1576 1577 DEF_EFCT_NPIV_TPG_ATTRIB(generate_node_acls); 1578 DEF_EFCT_NPIV_TPG_ATTRIB(cache_dynamic_acls); 1579 DEF_EFCT_NPIV_TPG_ATTRIB(demo_mode_write_protect); 1580 DEF_EFCT_NPIV_TPG_ATTRIB(prod_mode_write_protect); 1581 DEF_EFCT_NPIV_TPG_ATTRIB(demo_mode_login_only); 1582 DEF_EFCT_NPIV_TPG_ATTRIB(session_deletion_wait); 1583 1584 static struct configfs_attribute *efct_lio_npiv_tpg_attrib_attrs[] = { 1585 &efct_lio_npiv_tpg_attrib_attr_generate_node_acls, 1586 &efct_lio_npiv_tpg_attrib_attr_cache_dynamic_acls, 1587 &efct_lio_npiv_tpg_attrib_attr_demo_mode_write_protect, 1588 &efct_lio_npiv_tpg_attrib_attr_prod_mode_write_protect, 1589 &efct_lio_npiv_tpg_attrib_attr_demo_mode_login_only, 1590 &efct_lio_npiv_tpg_attrib_attr_session_deletion_wait, 1591 NULL, 1592 }; 1593 1594 CONFIGFS_ATTR(efct_lio_tpg_, enable); 1595 static struct configfs_attribute *efct_lio_tpg_attrs[] = { 1596 &efct_lio_tpg_attr_enable, NULL }; 1597 CONFIGFS_ATTR(efct_lio_npiv_tpg_, enable); 1598 static struct configfs_attribute *efct_lio_npiv_tpg_attrs[] = { 1599 &efct_lio_npiv_tpg_attr_enable, NULL }; 1600 1601 static const struct target_core_fabric_ops efct_lio_ops = { 1602 .module = THIS_MODULE, 1603 .fabric_name = "efct", 1604 .node_acl_size = sizeof(struct efct_lio_nacl), 1605 .max_data_sg_nents = 65535, 1606 .tpg_get_wwn = efct_lio_get_fabric_wwn, 1607 .tpg_get_tag = efct_lio_get_tag, 1608 .fabric_init_nodeacl = efct_lio_init_nodeacl, 1609 .tpg_check_demo_mode = efct_lio_check_demo_mode, 1610 .tpg_check_demo_mode_cache = efct_lio_check_demo_mode_cache, 1611 .tpg_check_demo_mode_write_protect = efct_lio_check_demo_write_protect, 1612 .tpg_check_prod_mode_write_protect = efct_lio_check_prod_write_protect, 1613 .tpg_get_inst_index = efct_lio_tpg_get_inst_index, 1614 .check_stop_free = efct_lio_check_stop_free, 1615 .aborted_task = efct_lio_aborted_task, 1616 .release_cmd = efct_lio_release_cmd, 1617 .close_session = efct_lio_close_session, 1618 .sess_get_index = efct_lio_sess_get_index, 1619 .write_pending = efct_lio_write_pending, 1620 .set_default_node_attributes = efct_lio_set_default_node_attrs, 1621 .get_cmd_state = efct_lio_get_cmd_state, 1622 .queue_data_in = efct_lio_queue_data_in, 1623 .queue_status = efct_lio_queue_status, 1624 .queue_tm_rsp = efct_lio_queue_tm_rsp, 1625 .fabric_make_wwn = efct_lio_make_nport, 1626 .fabric_drop_wwn = efct_lio_drop_nport, 1627 .fabric_make_tpg = efct_lio_make_tpg, 1628 .fabric_drop_tpg = efct_lio_drop_tpg, 1629 .tpg_check_demo_mode_login_only = efct_lio_check_demo_mode_login_only, 1630 .tpg_check_prot_fabric_only = NULL, 1631 .sess_get_initiator_sid = NULL, 1632 .tfc_tpg_base_attrs = efct_lio_tpg_attrs, 1633 .tfc_tpg_attrib_attrs = efct_lio_tpg_attrib_attrs, 1634 }; 1635 1636 static const struct target_core_fabric_ops efct_lio_npiv_ops = { 1637 .module = THIS_MODULE, 1638 .fabric_name = "efct_npiv", 1639 .node_acl_size = sizeof(struct efct_lio_nacl), 1640 .max_data_sg_nents = 65535, 1641 .tpg_get_wwn = efct_lio_get_npiv_fabric_wwn, 1642 .tpg_get_tag = efct_lio_get_npiv_tag, 1643 .fabric_init_nodeacl = efct_lio_init_nodeacl, 1644 .tpg_check_demo_mode = efct_lio_check_demo_mode, 1645 .tpg_check_demo_mode_cache = efct_lio_check_demo_mode_cache, 1646 .tpg_check_demo_mode_write_protect = 1647 efct_lio_npiv_check_demo_write_protect, 1648 .tpg_check_prod_mode_write_protect = 1649 efct_lio_npiv_check_prod_write_protect, 1650 .tpg_get_inst_index = efct_lio_tpg_get_inst_index, 1651 .check_stop_free = efct_lio_check_stop_free, 1652 .aborted_task = efct_lio_aborted_task, 1653 .release_cmd = efct_lio_release_cmd, 1654 .close_session = efct_lio_close_session, 1655 .sess_get_index = efct_lio_sess_get_index, 1656 .write_pending = efct_lio_write_pending, 1657 .set_default_node_attributes = efct_lio_set_default_node_attrs, 1658 .get_cmd_state = efct_lio_get_cmd_state, 1659 .queue_data_in = efct_lio_queue_data_in, 1660 .queue_status = efct_lio_queue_status, 1661 .queue_tm_rsp = efct_lio_queue_tm_rsp, 1662 .fabric_make_wwn = efct_lio_npiv_make_nport, 1663 .fabric_drop_wwn = efct_lio_npiv_drop_nport, 1664 .fabric_make_tpg = efct_lio_npiv_make_tpg, 1665 .fabric_drop_tpg = efct_lio_npiv_drop_tpg, 1666 .tpg_check_demo_mode_login_only = 1667 efct_lio_npiv_check_demo_mode_login_only, 1668 .tpg_check_prot_fabric_only = NULL, 1669 .sess_get_initiator_sid = NULL, 1670 .tfc_tpg_base_attrs = efct_lio_npiv_tpg_attrs, 1671 .tfc_tpg_attrib_attrs = efct_lio_npiv_tpg_attrib_attrs, 1672 }; 1673 1674 int efct_scsi_tgt_driver_init(void) 1675 { 1676 int rc; 1677 1678 /* Register the top level struct config_item_type with TCM core */ 1679 rc = target_register_template(&efct_lio_ops); 1680 if (rc < 0) { 1681 pr_err("target_fabric_configfs_register failed with %d\n", rc); 1682 return rc; 1683 } 1684 rc = target_register_template(&efct_lio_npiv_ops); 1685 if (rc < 0) { 1686 pr_err("target_fabric_configfs_register failed with %d\n", rc); 1687 target_unregister_template(&efct_lio_ops); 1688 return rc; 1689 } 1690 return 0; 1691 } 1692 1693 int efct_scsi_tgt_driver_exit(void) 1694 { 1695 target_unregister_template(&efct_lio_ops); 1696 target_unregister_template(&efct_lio_npiv_ops); 1697 return 0; 1698 } 1699