1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /******************************************************************************* 3 * This file contains tcm implementation using v4 configfs fabric infrastructure 4 * for QLogic target mode HBAs 5 * 6 * (c) Copyright 2010-2013 Datera, Inc. 7 * 8 * Author: Nicholas A. Bellinger <nab@daterainc.com> 9 * 10 * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from 11 * the TCM_FC / Open-FCoE.org fabric module. 12 * 13 * Copyright (c) 2010 Cisco Systems, Inc 14 * 15 ****************************************************************************/ 16 17 18 #include <linux/module.h> 19 #include <linux/utsname.h> 20 #include <linux/vmalloc.h> 21 #include <linux/list.h> 22 #include <linux/slab.h> 23 #include <linux/types.h> 24 #include <linux/string.h> 25 #include <linux/configfs.h> 26 #include <linux/ctype.h> 27 #include <asm/unaligned.h> 28 #include <scsi/scsi_host.h> 29 #include <target/target_core_base.h> 30 #include <target/target_core_fabric.h> 31 32 #include "qla_def.h" 33 #include "qla_target.h" 34 #include "tcm_qla2xxx.h" 35 36 static struct workqueue_struct *tcm_qla2xxx_free_wq; 37 38 /* 39 * Parse WWN. 40 * If strict, we require lower-case hex and colon separators to be sure 41 * the name is the same as what would be generated by ft_format_wwn() 42 * so the name and wwn are mapped one-to-one. 43 */ 44 static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict) 45 { 46 const char *cp; 47 char c; 48 u32 nibble; 49 u32 byte = 0; 50 u32 pos = 0; 51 u32 err; 52 53 *wwn = 0; 54 for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) { 55 c = *cp; 56 if (c == '\n' && cp[1] == '\0') 57 continue; 58 if (strict && pos++ == 2 && byte++ < 7) { 59 pos = 0; 60 if (c == ':') 61 continue; 62 err = 1; 63 goto fail; 64 } 65 if (c == '\0') { 66 err = 2; 67 if (strict && byte != 8) 68 goto fail; 69 return cp - name; 70 } 71 err = 3; 72 if (isdigit(c)) 73 nibble = c - '0'; 74 else if (isxdigit(c) && (islower(c) || !strict)) 75 nibble = tolower(c) - 'a' + 10; 76 else 77 goto fail; 78 *wwn = (*wwn << 4) | nibble; 79 } 80 err = 4; 81 fail: 82 pr_debug("err %u len %zu pos %u byte %u\n", 83 err, cp - name, pos, byte); 84 return -1; 85 } 86 87 static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn) 88 { 89 u8 b[8]; 90 91 put_unaligned_be64(wwn, b); 92 return snprintf(buf, len, 93 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", 94 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]); 95 } 96 97 /* 98 * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn 99 */ 100 static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm) 101 { 102 unsigned int i, j; 103 u8 wwn[8]; 104 105 memset(wwn, 0, sizeof(wwn)); 106 107 /* Validate and store the new name */ 108 for (i = 0, j = 0; i < 16; i++) { 109 int value; 110 111 value = hex_to_bin(*ns++); 112 if (value >= 0) 113 j = (j << 4) | value; 114 else 115 return -EINVAL; 116 117 if (i % 2) { 118 wwn[i/2] = j & 0xff; 119 j = 0; 120 } 121 } 122 123 *nm = wwn_to_u64(wwn); 124 return 0; 125 } 126 127 /* 128 * This parsing logic follows drivers/scsi/scsi_transport_fc.c: 129 * store_fc_host_vport_create() 130 */ 131 static int tcm_qla2xxx_npiv_parse_wwn( 132 const char *name, 133 size_t count, 134 u64 *wwpn, 135 u64 *wwnn) 136 { 137 unsigned int cnt = count; 138 int rc; 139 140 *wwpn = 0; 141 *wwnn = 0; 142 143 /* count may include a LF at end of string */ 144 if (name[cnt-1] == '\n' || name[cnt-1] == 0) 145 cnt--; 146 147 /* validate we have enough characters for WWPN */ 148 if ((cnt != (16+1+16)) || (name[16] != ':')) 149 return -EINVAL; 150 151 rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn); 152 if (rc != 0) 153 return rc; 154 155 rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn); 156 if (rc != 0) 157 return rc; 158 159 return 0; 160 } 161 162 static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg) 163 { 164 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 165 struct tcm_qla2xxx_tpg, se_tpg); 166 struct tcm_qla2xxx_lport *lport = tpg->lport; 167 168 return lport->lport_naa_name; 169 } 170 171 static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg) 172 { 173 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 174 struct tcm_qla2xxx_tpg, se_tpg); 175 return tpg->lport_tpgt; 176 } 177 178 static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg) 179 { 180 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 181 struct tcm_qla2xxx_tpg, se_tpg); 182 183 return tpg->tpg_attrib.generate_node_acls; 184 } 185 186 static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg) 187 { 188 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 189 struct tcm_qla2xxx_tpg, se_tpg); 190 191 return tpg->tpg_attrib.cache_dynamic_acls; 192 } 193 194 static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg) 195 { 196 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 197 struct tcm_qla2xxx_tpg, se_tpg); 198 199 return tpg->tpg_attrib.demo_mode_write_protect; 200 } 201 202 static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg) 203 { 204 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 205 struct tcm_qla2xxx_tpg, se_tpg); 206 207 return tpg->tpg_attrib.prod_mode_write_protect; 208 } 209 210 static int tcm_qla2xxx_check_demo_mode_login_only(struct se_portal_group *se_tpg) 211 { 212 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 213 struct tcm_qla2xxx_tpg, se_tpg); 214 215 return tpg->tpg_attrib.demo_mode_login_only; 216 } 217 218 static int tcm_qla2xxx_check_prot_fabric_only(struct se_portal_group *se_tpg) 219 { 220 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 221 struct tcm_qla2xxx_tpg, se_tpg); 222 223 return tpg->tpg_attrib.fabric_prot_type; 224 } 225 226 static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg) 227 { 228 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 229 struct tcm_qla2xxx_tpg, se_tpg); 230 231 return tpg->lport_tpgt; 232 } 233 234 static void tcm_qla2xxx_complete_mcmd(struct work_struct *work) 235 { 236 struct qla_tgt_mgmt_cmd *mcmd = container_of(work, 237 struct qla_tgt_mgmt_cmd, free_work); 238 239 transport_generic_free_cmd(&mcmd->se_cmd, 0); 240 } 241 242 /* 243 * Called from qla_target_template->free_mcmd(), and will call 244 * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops 245 * release callback. qla_hw_data->hardware_lock is expected to be held 246 */ 247 static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd) 248 { 249 if (!mcmd) 250 return; 251 INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd); 252 queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work); 253 } 254 255 static void tcm_qla2xxx_complete_free(struct work_struct *work) 256 { 257 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work); 258 unsigned long flags; 259 260 cmd->cmd_in_wq = 0; 261 262 WARN_ON(cmd->trc_flags & TRC_CMD_FREE); 263 264 /* To do: protect all tgt_counters manipulations with proper locking. */ 265 cmd->qpair->tgt_counters.qla_core_ret_sta_ctio++; 266 cmd->trc_flags |= TRC_CMD_FREE; 267 cmd->cmd_sent_to_fw = 0; 268 269 spin_lock_irqsave(&cmd->sess->sess_cmd_lock, flags); 270 list_del_init(&cmd->sess_cmd_list); 271 spin_unlock_irqrestore(&cmd->sess->sess_cmd_lock, flags); 272 273 transport_generic_free_cmd(&cmd->se_cmd, 0); 274 } 275 276 static struct qla_tgt_cmd *tcm_qla2xxx_get_cmd(struct fc_port *sess) 277 { 278 struct se_session *se_sess = sess->se_sess; 279 struct qla_tgt_cmd *cmd; 280 int tag, cpu; 281 282 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu); 283 if (tag < 0) 284 return NULL; 285 286 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag]; 287 memset(cmd, 0, sizeof(struct qla_tgt_cmd)); 288 cmd->se_cmd.map_tag = tag; 289 cmd->se_cmd.map_cpu = cpu; 290 291 return cmd; 292 } 293 294 static void tcm_qla2xxx_rel_cmd(struct qla_tgt_cmd *cmd) 295 { 296 target_free_tag(cmd->sess->se_sess, &cmd->se_cmd); 297 } 298 299 /* 300 * Called from qla_target_template->free_cmd(), and will call 301 * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops 302 * release callback. qla_hw_data->hardware_lock is expected to be held 303 */ 304 static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd) 305 { 306 cmd->qpair->tgt_counters.core_qla_free_cmd++; 307 cmd->cmd_in_wq = 1; 308 309 WARN_ON(cmd->trc_flags & TRC_CMD_DONE); 310 cmd->trc_flags |= TRC_CMD_DONE; 311 312 INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free); 313 queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work); 314 } 315 316 /* 317 * Called from struct target_core_fabric_ops->check_stop_free() context 318 */ 319 static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd) 320 { 321 struct qla_tgt_cmd *cmd; 322 323 if ((se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) == 0) { 324 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd); 325 cmd->trc_flags |= TRC_CMD_CHK_STOP; 326 } 327 328 return target_put_sess_cmd(se_cmd); 329 } 330 331 /* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying 332 * fabric descriptor @se_cmd command to release 333 */ 334 static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd) 335 { 336 struct qla_tgt_cmd *cmd; 337 338 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) { 339 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd, 340 struct qla_tgt_mgmt_cmd, se_cmd); 341 qlt_free_mcmd(mcmd); 342 return; 343 } 344 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd); 345 346 if (WARN_ON(cmd->cmd_sent_to_fw)) 347 return; 348 349 qlt_free_cmd(cmd); 350 } 351 352 static void tcm_qla2xxx_release_session(struct kref *kref) 353 { 354 struct fc_port *sess = container_of(kref, 355 struct fc_port, sess_kref); 356 357 qlt_unreg_sess(sess); 358 } 359 360 static void tcm_qla2xxx_put_sess(struct fc_port *sess) 361 { 362 if (!sess) 363 return; 364 365 kref_put(&sess->sess_kref, tcm_qla2xxx_release_session); 366 } 367 368 static void tcm_qla2xxx_close_session(struct se_session *se_sess) 369 { 370 struct fc_port *sess = se_sess->fabric_sess_ptr; 371 372 BUG_ON(!sess); 373 374 target_stop_session(se_sess); 375 376 sess->explicit_logout = 1; 377 tcm_qla2xxx_put_sess(sess); 378 } 379 380 static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess) 381 { 382 return 0; 383 } 384 385 static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd) 386 { 387 struct qla_tgt_cmd *cmd = container_of(se_cmd, 388 struct qla_tgt_cmd, se_cmd); 389 390 if (cmd->aborted) { 391 /* Cmd can loop during Q-full. tcm_qla2xxx_aborted_task 392 * can get ahead of this cmd. tcm_qla2xxx_aborted_task 393 * already kick start the free. 394 */ 395 pr_debug("write_pending aborted cmd[%p] refcount %d " 396 "transport_state %x, t_state %x, se_cmd_flags %x\n", 397 cmd, kref_read(&cmd->se_cmd.cmd_kref), 398 cmd->se_cmd.transport_state, 399 cmd->se_cmd.t_state, 400 cmd->se_cmd.se_cmd_flags); 401 transport_generic_request_failure(&cmd->se_cmd, 402 TCM_CHECK_CONDITION_ABORT_CMD); 403 return 0; 404 } 405 cmd->trc_flags |= TRC_XFR_RDY; 406 cmd->bufflen = se_cmd->data_length; 407 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd); 408 409 cmd->sg_cnt = se_cmd->t_data_nents; 410 cmd->sg = se_cmd->t_data_sg; 411 412 cmd->prot_sg_cnt = se_cmd->t_prot_nents; 413 cmd->prot_sg = se_cmd->t_prot_sg; 414 cmd->blk_sz = se_cmd->se_dev->dev_attrib.block_size; 415 se_cmd->pi_err = 0; 416 417 /* 418 * qla_target.c:qlt_rdy_to_xfer() will call dma_map_sg() to setup 419 * the SGL mappings into PCIe memory for incoming FCP WRITE data. 420 */ 421 return qlt_rdy_to_xfer(cmd); 422 } 423 424 static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl) 425 { 426 return; 427 } 428 429 static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd) 430 { 431 if (!(se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) { 432 struct qla_tgt_cmd *cmd = container_of(se_cmd, 433 struct qla_tgt_cmd, se_cmd); 434 return cmd->state; 435 } 436 437 return 0; 438 } 439 440 /* 441 * Called from process context in qla_target.c:qlt_do_work() code 442 */ 443 static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd, 444 unsigned char *cdb, uint32_t data_length, int fcp_task_attr, 445 int data_dir, int bidi) 446 { 447 struct se_cmd *se_cmd = &cmd->se_cmd; 448 struct se_session *se_sess; 449 struct fc_port *sess; 450 #ifdef CONFIG_TCM_QLA2XXX_DEBUG 451 struct se_portal_group *se_tpg; 452 struct tcm_qla2xxx_tpg *tpg; 453 #endif 454 int rc, target_flags = TARGET_SCF_ACK_KREF; 455 unsigned long flags; 456 457 if (bidi) 458 target_flags |= TARGET_SCF_BIDI_OP; 459 460 if (se_cmd->cpuid != WORK_CPU_UNBOUND) 461 target_flags |= TARGET_SCF_USE_CPUID; 462 463 sess = cmd->sess; 464 if (!sess) { 465 pr_err("Unable to locate struct fc_port from qla_tgt_cmd\n"); 466 return -EINVAL; 467 } 468 469 se_sess = sess->se_sess; 470 if (!se_sess) { 471 pr_err("Unable to locate active struct se_session\n"); 472 return -EINVAL; 473 } 474 475 #ifdef CONFIG_TCM_QLA2XXX_DEBUG 476 se_tpg = se_sess->se_tpg; 477 tpg = container_of(se_tpg, struct tcm_qla2xxx_tpg, se_tpg); 478 if (unlikely(tpg->tpg_attrib.jam_host)) { 479 /* return, and dont run target_submit_cmd,discarding command */ 480 return 0; 481 } 482 #endif 483 cmd->qpair->tgt_counters.qla_core_sbt_cmd++; 484 485 spin_lock_irqsave(&sess->sess_cmd_lock, flags); 486 list_add_tail(&cmd->sess_cmd_list, &sess->sess_cmd_list); 487 spin_unlock_irqrestore(&sess->sess_cmd_lock, flags); 488 489 rc = target_init_cmd(se_cmd, se_sess, &cmd->sense_buffer[0], 490 cmd->unpacked_lun, data_length, fcp_task_attr, 491 data_dir, target_flags); 492 if (rc) 493 return rc; 494 495 if (target_submit_prep(se_cmd, cdb, NULL, 0, NULL, 0, NULL, 0, 496 GFP_KERNEL)) 497 return 0; 498 499 target_submit(se_cmd); 500 return 0; 501 } 502 503 static void tcm_qla2xxx_handle_data_work(struct work_struct *work) 504 { 505 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work); 506 507 /* 508 * Ensure that the complete FCP WRITE payload has been received. 509 * Otherwise return an exception via CHECK_CONDITION status. 510 */ 511 cmd->cmd_in_wq = 0; 512 cmd->cmd_sent_to_fw = 0; 513 if (cmd->aborted) { 514 transport_generic_request_failure(&cmd->se_cmd, 515 TCM_CHECK_CONDITION_ABORT_CMD); 516 return; 517 } 518 519 cmd->qpair->tgt_counters.qla_core_ret_ctio++; 520 if (!cmd->write_data_transferred) { 521 switch (cmd->dif_err_code) { 522 case DIF_ERR_GRD: 523 cmd->se_cmd.pi_err = 524 TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED; 525 break; 526 case DIF_ERR_REF: 527 cmd->se_cmd.pi_err = 528 TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; 529 break; 530 case DIF_ERR_APP: 531 cmd->se_cmd.pi_err = 532 TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED; 533 break; 534 case DIF_ERR_NONE: 535 default: 536 break; 537 } 538 539 if (cmd->se_cmd.pi_err) 540 transport_generic_request_failure(&cmd->se_cmd, 541 cmd->se_cmd.pi_err); 542 else 543 transport_generic_request_failure(&cmd->se_cmd, 544 TCM_CHECK_CONDITION_ABORT_CMD); 545 546 return; 547 } 548 549 return target_execute_cmd(&cmd->se_cmd); 550 } 551 552 /* 553 * Called from qla_target.c:qlt_do_ctio_completion() 554 */ 555 static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd) 556 { 557 cmd->trc_flags |= TRC_DATA_IN; 558 cmd->cmd_in_wq = 1; 559 INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work); 560 queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work); 561 } 562 563 static int tcm_qla2xxx_chk_dif_tags(uint32_t tag) 564 { 565 return 0; 566 } 567 568 static int tcm_qla2xxx_dif_tags(struct qla_tgt_cmd *cmd, 569 uint16_t *pfw_prot_opts) 570 { 571 struct se_cmd *se_cmd = &cmd->se_cmd; 572 573 if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD)) 574 *pfw_prot_opts |= PO_DISABLE_GUARD_CHECK; 575 576 if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG)) 577 *pfw_prot_opts |= PO_DIS_APP_TAG_VALD; 578 579 return 0; 580 } 581 582 /* 583 * Called from qla_target.c:qlt_issue_task_mgmt() 584 */ 585 static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, u64 lun, 586 uint16_t tmr_func, uint32_t tag) 587 { 588 struct fc_port *sess = mcmd->sess; 589 struct se_cmd *se_cmd = &mcmd->se_cmd; 590 int transl_tmr_func = 0; 591 592 switch (tmr_func) { 593 case QLA_TGT_ABTS: 594 pr_debug("%ld: ABTS received\n", sess->vha->host_no); 595 transl_tmr_func = TMR_ABORT_TASK; 596 break; 597 case QLA_TGT_2G_ABORT_TASK: 598 pr_debug("%ld: 2G Abort Task received\n", sess->vha->host_no); 599 transl_tmr_func = TMR_ABORT_TASK; 600 break; 601 case QLA_TGT_CLEAR_ACA: 602 pr_debug("%ld: CLEAR_ACA received\n", sess->vha->host_no); 603 transl_tmr_func = TMR_CLEAR_ACA; 604 break; 605 case QLA_TGT_TARGET_RESET: 606 pr_debug("%ld: TARGET_RESET received\n", sess->vha->host_no); 607 transl_tmr_func = TMR_TARGET_WARM_RESET; 608 break; 609 case QLA_TGT_LUN_RESET: 610 pr_debug("%ld: LUN_RESET received\n", sess->vha->host_no); 611 transl_tmr_func = TMR_LUN_RESET; 612 break; 613 case QLA_TGT_CLEAR_TS: 614 pr_debug("%ld: CLEAR_TS received\n", sess->vha->host_no); 615 transl_tmr_func = TMR_CLEAR_TASK_SET; 616 break; 617 case QLA_TGT_ABORT_TS: 618 pr_debug("%ld: ABORT_TS received\n", sess->vha->host_no); 619 transl_tmr_func = TMR_ABORT_TASK_SET; 620 break; 621 default: 622 pr_debug("%ld: Unknown task mgmt fn 0x%x\n", 623 sess->vha->host_no, tmr_func); 624 return -ENOSYS; 625 } 626 627 return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd, 628 transl_tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF); 629 } 630 631 static struct qla_tgt_cmd *tcm_qla2xxx_find_cmd_by_tag(struct fc_port *sess, 632 uint64_t tag) 633 { 634 struct qla_tgt_cmd *cmd; 635 unsigned long flags; 636 637 if (!sess->se_sess) 638 return NULL; 639 640 spin_lock_irqsave(&sess->sess_cmd_lock, flags); 641 list_for_each_entry(cmd, &sess->sess_cmd_list, sess_cmd_list) { 642 if (cmd->se_cmd.tag == tag) 643 goto done; 644 } 645 cmd = NULL; 646 done: 647 spin_unlock_irqrestore(&sess->sess_cmd_lock, flags); 648 649 return cmd; 650 } 651 652 static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd) 653 { 654 struct qla_tgt_cmd *cmd = container_of(se_cmd, 655 struct qla_tgt_cmd, se_cmd); 656 657 if (cmd->aborted) { 658 /* Cmd can loop during Q-full. tcm_qla2xxx_aborted_task 659 * can get ahead of this cmd. tcm_qla2xxx_aborted_task 660 * already kick start the free. 661 */ 662 pr_debug("queue_data_in aborted cmd[%p] refcount %d " 663 "transport_state %x, t_state %x, se_cmd_flags %x\n", 664 cmd, kref_read(&cmd->se_cmd.cmd_kref), 665 cmd->se_cmd.transport_state, 666 cmd->se_cmd.t_state, 667 cmd->se_cmd.se_cmd_flags); 668 return 0; 669 } 670 671 cmd->trc_flags |= TRC_XMIT_DATA; 672 cmd->bufflen = se_cmd->data_length; 673 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd); 674 675 cmd->sg_cnt = se_cmd->t_data_nents; 676 cmd->sg = se_cmd->t_data_sg; 677 cmd->offset = 0; 678 679 cmd->prot_sg_cnt = se_cmd->t_prot_nents; 680 cmd->prot_sg = se_cmd->t_prot_sg; 681 cmd->blk_sz = se_cmd->se_dev->dev_attrib.block_size; 682 se_cmd->pi_err = 0; 683 684 /* 685 * Now queue completed DATA_IN the qla2xxx LLD and response ring 686 */ 687 return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS, 688 se_cmd->scsi_status); 689 } 690 691 static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd) 692 { 693 struct qla_tgt_cmd *cmd = container_of(se_cmd, 694 struct qla_tgt_cmd, se_cmd); 695 int xmit_type = QLA_TGT_XMIT_STATUS; 696 697 if (cmd->aborted) { 698 /* 699 * Cmd can loop during Q-full. tcm_qla2xxx_aborted_task 700 * can get ahead of this cmd. tcm_qla2xxx_aborted_task 701 * already kick start the free. 702 */ 703 pr_debug( 704 "queue_data_in aborted cmd[%p] refcount %d transport_state %x, t_state %x, se_cmd_flags %x\n", 705 cmd, kref_read(&cmd->se_cmd.cmd_kref), 706 cmd->se_cmd.transport_state, cmd->se_cmd.t_state, 707 cmd->se_cmd.se_cmd_flags); 708 return 0; 709 } 710 cmd->bufflen = se_cmd->data_length; 711 cmd->sg = NULL; 712 cmd->sg_cnt = 0; 713 cmd->offset = 0; 714 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd); 715 cmd->trc_flags |= TRC_XMIT_STATUS; 716 717 if (se_cmd->data_direction == DMA_FROM_DEVICE) { 718 /* 719 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen 720 * for qla_tgt_xmit_response LLD code 721 */ 722 if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) { 723 se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT; 724 se_cmd->residual_count = 0; 725 } 726 se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT; 727 se_cmd->residual_count += se_cmd->data_length; 728 729 cmd->bufflen = 0; 730 } 731 /* 732 * Now queue status response to qla2xxx LLD code and response ring 733 */ 734 return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status); 735 } 736 737 static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd) 738 { 739 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req; 740 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd, 741 struct qla_tgt_mgmt_cmd, se_cmd); 742 743 pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n", 744 mcmd, se_tmr->function, se_tmr->response); 745 /* 746 * Do translation between TCM TM response codes and 747 * QLA2xxx FC TM response codes. 748 */ 749 switch (se_tmr->response) { 750 case TMR_FUNCTION_COMPLETE: 751 mcmd->fc_tm_rsp = FC_TM_SUCCESS; 752 break; 753 case TMR_TASK_DOES_NOT_EXIST: 754 mcmd->fc_tm_rsp = FC_TM_BAD_CMD; 755 break; 756 case TMR_FUNCTION_REJECTED: 757 mcmd->fc_tm_rsp = FC_TM_REJECT; 758 break; 759 case TMR_LUN_DOES_NOT_EXIST: 760 default: 761 mcmd->fc_tm_rsp = FC_TM_FAILED; 762 break; 763 } 764 /* 765 * Queue the TM response to QLA2xxx LLD to build a 766 * CTIO response packet. 767 */ 768 qlt_xmit_tm_rsp(mcmd); 769 } 770 771 static void tcm_qla2xxx_aborted_task(struct se_cmd *se_cmd) 772 { 773 struct qla_tgt_cmd *cmd; 774 unsigned long flags; 775 776 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) 777 return; 778 779 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd); 780 781 spin_lock_irqsave(&cmd->sess->sess_cmd_lock, flags); 782 list_del_init(&cmd->sess_cmd_list); 783 spin_unlock_irqrestore(&cmd->sess->sess_cmd_lock, flags); 784 785 qlt_abort_cmd(cmd); 786 } 787 788 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *, 789 struct tcm_qla2xxx_nacl *, struct fc_port *); 790 /* 791 * Expected to be called with struct qla_hw_data->tgt.sess_lock held 792 */ 793 static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct fc_port *sess) 794 { 795 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl; 796 struct se_portal_group *se_tpg = se_nacl->se_tpg; 797 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn; 798 struct tcm_qla2xxx_lport *lport = container_of(se_wwn, 799 struct tcm_qla2xxx_lport, lport_wwn); 800 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl, 801 struct tcm_qla2xxx_nacl, se_node_acl); 802 void *node; 803 804 pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id); 805 806 node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id); 807 if (WARN_ON(node && (node != se_nacl))) { 808 /* 809 * The nacl no longer matches what we think it should be. 810 * Most likely a new dynamic acl has been added while 811 * someone dropped the hardware lock. It clearly is a 812 * bug elsewhere, but this bit can't make things worse. 813 */ 814 btree_insert32(&lport->lport_fcport_map, nacl->nport_id, 815 node, GFP_ATOMIC); 816 } 817 818 pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n", 819 se_nacl, nacl->nport_wwnn, nacl->nport_id); 820 /* 821 * Now clear the se_nacl and session pointers from our HW lport lookup 822 * table mapping for this initiator's fabric S_ID and LOOP_ID entries. 823 * 824 * This is done ahead of callbacks into tcm_qla2xxx_free_session() -> 825 * target_wait_for_sess_cmds() before the session waits for outstanding 826 * I/O to complete, to avoid a race between session shutdown execution 827 * and incoming ATIOs or TMRs picking up a stale se_node_act reference. 828 */ 829 tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess); 830 } 831 832 static void tcm_qla2xxx_shutdown_sess(struct fc_port *sess) 833 { 834 target_stop_session(sess->se_sess); 835 } 836 837 static int tcm_qla2xxx_init_nodeacl(struct se_node_acl *se_nacl, 838 const char *name) 839 { 840 struct tcm_qla2xxx_nacl *nacl = 841 container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl); 842 u64 wwnn; 843 844 if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0) 845 return -EINVAL; 846 847 nacl->nport_wwnn = wwnn; 848 tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn); 849 850 return 0; 851 } 852 853 /* Start items for tcm_qla2xxx_tpg_attrib_cit */ 854 855 #define DEF_QLA_TPG_ATTRIB(name) \ 856 \ 857 static ssize_t tcm_qla2xxx_tpg_attrib_##name##_show( \ 858 struct config_item *item, char *page) \ 859 { \ 860 struct se_portal_group *se_tpg = attrib_to_tpg(item); \ 861 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \ 862 struct tcm_qla2xxx_tpg, se_tpg); \ 863 \ 864 return sprintf(page, "%d\n", tpg->tpg_attrib.name); \ 865 } \ 866 \ 867 static ssize_t tcm_qla2xxx_tpg_attrib_##name##_store( \ 868 struct config_item *item, const char *page, size_t count) \ 869 { \ 870 struct se_portal_group *se_tpg = attrib_to_tpg(item); \ 871 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \ 872 struct tcm_qla2xxx_tpg, se_tpg); \ 873 struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \ 874 unsigned long val; \ 875 int ret; \ 876 \ 877 ret = kstrtoul(page, 0, &val); \ 878 if (ret < 0) { \ 879 pr_err("kstrtoul() failed with" \ 880 " ret: %d\n", ret); \ 881 return -EINVAL; \ 882 } \ 883 \ 884 if ((val != 0) && (val != 1)) { \ 885 pr_err("Illegal boolean value %lu\n", val); \ 886 return -EINVAL; \ 887 } \ 888 \ 889 a->name = val; \ 890 \ 891 return count; \ 892 } \ 893 CONFIGFS_ATTR(tcm_qla2xxx_tpg_attrib_, name) 894 895 DEF_QLA_TPG_ATTRIB(generate_node_acls); 896 DEF_QLA_TPG_ATTRIB(cache_dynamic_acls); 897 DEF_QLA_TPG_ATTRIB(demo_mode_write_protect); 898 DEF_QLA_TPG_ATTRIB(prod_mode_write_protect); 899 DEF_QLA_TPG_ATTRIB(demo_mode_login_only); 900 #ifdef CONFIG_TCM_QLA2XXX_DEBUG 901 DEF_QLA_TPG_ATTRIB(jam_host); 902 #endif 903 904 static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = { 905 &tcm_qla2xxx_tpg_attrib_attr_generate_node_acls, 906 &tcm_qla2xxx_tpg_attrib_attr_cache_dynamic_acls, 907 &tcm_qla2xxx_tpg_attrib_attr_demo_mode_write_protect, 908 &tcm_qla2xxx_tpg_attrib_attr_prod_mode_write_protect, 909 &tcm_qla2xxx_tpg_attrib_attr_demo_mode_login_only, 910 #ifdef CONFIG_TCM_QLA2XXX_DEBUG 911 &tcm_qla2xxx_tpg_attrib_attr_jam_host, 912 #endif 913 NULL, 914 }; 915 916 /* End items for tcm_qla2xxx_tpg_attrib_cit */ 917 918 static ssize_t tcm_qla2xxx_tpg_enable_show(struct config_item *item, 919 char *page) 920 { 921 struct se_portal_group *se_tpg = to_tpg(item); 922 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 923 struct tcm_qla2xxx_tpg, se_tpg); 924 925 return snprintf(page, PAGE_SIZE, "%d\n", 926 atomic_read(&tpg->lport_tpg_enabled)); 927 } 928 929 static ssize_t tcm_qla2xxx_tpg_enable_store(struct config_item *item, 930 const char *page, size_t count) 931 { 932 struct se_portal_group *se_tpg = to_tpg(item); 933 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn; 934 struct tcm_qla2xxx_lport *lport = container_of(se_wwn, 935 struct tcm_qla2xxx_lport, lport_wwn); 936 struct scsi_qla_host *vha = lport->qla_vha; 937 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 938 struct tcm_qla2xxx_tpg, se_tpg); 939 unsigned long op; 940 int rc; 941 942 rc = kstrtoul(page, 0, &op); 943 if (rc < 0) { 944 pr_err("kstrtoul() returned %d\n", rc); 945 return -EINVAL; 946 } 947 if ((op != 1) && (op != 0)) { 948 pr_err("Illegal value for tpg_enable: %lu\n", op); 949 return -EINVAL; 950 } 951 if (op) { 952 if (atomic_read(&tpg->lport_tpg_enabled)) 953 return -EEXIST; 954 955 atomic_set(&tpg->lport_tpg_enabled, 1); 956 qlt_enable_vha(vha); 957 } else { 958 if (!atomic_read(&tpg->lport_tpg_enabled)) 959 return count; 960 961 atomic_set(&tpg->lport_tpg_enabled, 0); 962 qlt_stop_phase1(vha->vha_tgt.qla_tgt); 963 qlt_stop_phase2(vha->vha_tgt.qla_tgt); 964 } 965 966 return count; 967 } 968 969 static ssize_t tcm_qla2xxx_tpg_dynamic_sessions_show(struct config_item *item, 970 char *page) 971 { 972 return target_show_dynamic_sessions(to_tpg(item), page); 973 } 974 975 static ssize_t tcm_qla2xxx_tpg_fabric_prot_type_store(struct config_item *item, 976 const char *page, size_t count) 977 { 978 struct se_portal_group *se_tpg = to_tpg(item); 979 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 980 struct tcm_qla2xxx_tpg, se_tpg); 981 unsigned long val; 982 int ret = kstrtoul(page, 0, &val); 983 984 if (ret) { 985 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret); 986 return ret; 987 } 988 if (val != 0 && val != 1 && val != 3) { 989 pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val); 990 return -EINVAL; 991 } 992 tpg->tpg_attrib.fabric_prot_type = val; 993 994 return count; 995 } 996 997 static ssize_t tcm_qla2xxx_tpg_fabric_prot_type_show(struct config_item *item, 998 char *page) 999 { 1000 struct se_portal_group *se_tpg = to_tpg(item); 1001 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 1002 struct tcm_qla2xxx_tpg, se_tpg); 1003 1004 return sprintf(page, "%d\n", tpg->tpg_attrib.fabric_prot_type); 1005 } 1006 1007 CONFIGFS_ATTR(tcm_qla2xxx_tpg_, enable); 1008 CONFIGFS_ATTR_RO(tcm_qla2xxx_tpg_, dynamic_sessions); 1009 CONFIGFS_ATTR(tcm_qla2xxx_tpg_, fabric_prot_type); 1010 1011 static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = { 1012 &tcm_qla2xxx_tpg_attr_enable, 1013 &tcm_qla2xxx_tpg_attr_dynamic_sessions, 1014 &tcm_qla2xxx_tpg_attr_fabric_prot_type, 1015 NULL, 1016 }; 1017 1018 static struct se_portal_group *tcm_qla2xxx_make_tpg(struct se_wwn *wwn, 1019 const char *name) 1020 { 1021 struct tcm_qla2xxx_lport *lport = container_of(wwn, 1022 struct tcm_qla2xxx_lport, lport_wwn); 1023 struct tcm_qla2xxx_tpg *tpg; 1024 unsigned long tpgt; 1025 int ret; 1026 1027 if (strstr(name, "tpgt_") != name) 1028 return ERR_PTR(-EINVAL); 1029 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX) 1030 return ERR_PTR(-EINVAL); 1031 1032 if ((tpgt != 1)) { 1033 pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n"); 1034 return ERR_PTR(-ENOSYS); 1035 } 1036 1037 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL); 1038 if (!tpg) { 1039 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n"); 1040 return ERR_PTR(-ENOMEM); 1041 } 1042 tpg->lport = lport; 1043 tpg->lport_tpgt = tpgt; 1044 /* 1045 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic 1046 * NodeACLs 1047 */ 1048 tpg->tpg_attrib.generate_node_acls = 1; 1049 tpg->tpg_attrib.demo_mode_write_protect = 1; 1050 tpg->tpg_attrib.cache_dynamic_acls = 1; 1051 tpg->tpg_attrib.demo_mode_login_only = 1; 1052 tpg->tpg_attrib.jam_host = 0; 1053 1054 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP); 1055 if (ret < 0) { 1056 kfree(tpg); 1057 return NULL; 1058 } 1059 1060 lport->tpg_1 = tpg; 1061 1062 return &tpg->se_tpg; 1063 } 1064 1065 static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg) 1066 { 1067 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 1068 struct tcm_qla2xxx_tpg, se_tpg); 1069 struct tcm_qla2xxx_lport *lport = tpg->lport; 1070 struct scsi_qla_host *vha = lport->qla_vha; 1071 /* 1072 * Call into qla2x_target.c LLD logic to shutdown the active 1073 * FC Nexuses and disable target mode operation for this qla_hw_data 1074 */ 1075 if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stop) 1076 qlt_stop_phase1(vha->vha_tgt.qla_tgt); 1077 1078 core_tpg_deregister(se_tpg); 1079 /* 1080 * Clear local TPG=1 pointer for non NPIV mode. 1081 */ 1082 lport->tpg_1 = NULL; 1083 kfree(tpg); 1084 } 1085 1086 static ssize_t tcm_qla2xxx_npiv_tpg_enable_show(struct config_item *item, 1087 char *page) 1088 { 1089 return tcm_qla2xxx_tpg_enable_show(item, page); 1090 } 1091 1092 static ssize_t tcm_qla2xxx_npiv_tpg_enable_store(struct config_item *item, 1093 const char *page, size_t count) 1094 { 1095 struct se_portal_group *se_tpg = to_tpg(item); 1096 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn; 1097 struct tcm_qla2xxx_lport *lport = container_of(se_wwn, 1098 struct tcm_qla2xxx_lport, lport_wwn); 1099 struct scsi_qla_host *vha = lport->qla_vha; 1100 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 1101 struct tcm_qla2xxx_tpg, se_tpg); 1102 unsigned long op; 1103 int rc; 1104 1105 rc = kstrtoul(page, 0, &op); 1106 if (rc < 0) { 1107 pr_err("kstrtoul() returned %d\n", rc); 1108 return -EINVAL; 1109 } 1110 if ((op != 1) && (op != 0)) { 1111 pr_err("Illegal value for tpg_enable: %lu\n", op); 1112 return -EINVAL; 1113 } 1114 if (op) { 1115 if (atomic_read(&tpg->lport_tpg_enabled)) 1116 return -EEXIST; 1117 1118 atomic_set(&tpg->lport_tpg_enabled, 1); 1119 qlt_enable_vha(vha); 1120 } else { 1121 if (!atomic_read(&tpg->lport_tpg_enabled)) 1122 return count; 1123 1124 atomic_set(&tpg->lport_tpg_enabled, 0); 1125 qlt_stop_phase1(vha->vha_tgt.qla_tgt); 1126 qlt_stop_phase2(vha->vha_tgt.qla_tgt); 1127 } 1128 1129 return count; 1130 } 1131 1132 CONFIGFS_ATTR(tcm_qla2xxx_npiv_tpg_, enable); 1133 1134 static struct configfs_attribute *tcm_qla2xxx_npiv_tpg_attrs[] = { 1135 &tcm_qla2xxx_npiv_tpg_attr_enable, 1136 NULL, 1137 }; 1138 1139 static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(struct se_wwn *wwn, 1140 const char *name) 1141 { 1142 struct tcm_qla2xxx_lport *lport = container_of(wwn, 1143 struct tcm_qla2xxx_lport, lport_wwn); 1144 struct tcm_qla2xxx_tpg *tpg; 1145 unsigned long tpgt; 1146 int ret; 1147 1148 if (strstr(name, "tpgt_") != name) 1149 return ERR_PTR(-EINVAL); 1150 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX) 1151 return ERR_PTR(-EINVAL); 1152 1153 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL); 1154 if (!tpg) { 1155 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n"); 1156 return ERR_PTR(-ENOMEM); 1157 } 1158 tpg->lport = lport; 1159 tpg->lport_tpgt = tpgt; 1160 1161 /* 1162 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic 1163 * NodeACLs 1164 */ 1165 tpg->tpg_attrib.generate_node_acls = 1; 1166 tpg->tpg_attrib.demo_mode_write_protect = 1; 1167 tpg->tpg_attrib.cache_dynamic_acls = 1; 1168 tpg->tpg_attrib.demo_mode_login_only = 1; 1169 1170 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP); 1171 if (ret < 0) { 1172 kfree(tpg); 1173 return NULL; 1174 } 1175 lport->tpg_1 = tpg; 1176 return &tpg->se_tpg; 1177 } 1178 1179 /* 1180 * Expected to be called with struct qla_hw_data->tgt.sess_lock held 1181 */ 1182 static struct fc_port *tcm_qla2xxx_find_sess_by_s_id(scsi_qla_host_t *vha, 1183 const be_id_t s_id) 1184 { 1185 struct tcm_qla2xxx_lport *lport; 1186 struct se_node_acl *se_nacl; 1187 struct tcm_qla2xxx_nacl *nacl; 1188 u32 key; 1189 1190 lport = vha->vha_tgt.target_lport_ptr; 1191 if (!lport) { 1192 pr_err("Unable to locate struct tcm_qla2xxx_lport\n"); 1193 dump_stack(); 1194 return NULL; 1195 } 1196 1197 key = sid_to_key(s_id); 1198 pr_debug("find_sess_by_s_id: 0x%06x\n", key); 1199 1200 se_nacl = btree_lookup32(&lport->lport_fcport_map, key); 1201 if (!se_nacl) { 1202 pr_debug("Unable to locate s_id: 0x%06x\n", key); 1203 return NULL; 1204 } 1205 pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n", 1206 se_nacl, se_nacl->initiatorname); 1207 1208 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl); 1209 if (!nacl->fc_port) { 1210 pr_err("Unable to locate struct fc_port\n"); 1211 return NULL; 1212 } 1213 1214 return nacl->fc_port; 1215 } 1216 1217 /* 1218 * Expected to be called with struct qla_hw_data->tgt.sess_lock held 1219 */ 1220 static void tcm_qla2xxx_set_sess_by_s_id( 1221 struct tcm_qla2xxx_lport *lport, 1222 struct se_node_acl *new_se_nacl, 1223 struct tcm_qla2xxx_nacl *nacl, 1224 struct se_session *se_sess, 1225 struct fc_port *fc_port, 1226 be_id_t s_id) 1227 { 1228 u32 key; 1229 void *slot; 1230 int rc; 1231 1232 key = sid_to_key(s_id); 1233 pr_debug("set_sess_by_s_id: %06x\n", key); 1234 1235 slot = btree_lookup32(&lport->lport_fcport_map, key); 1236 if (!slot) { 1237 if (new_se_nacl) { 1238 pr_debug("Setting up new fc_port entry to new_se_nacl\n"); 1239 nacl->nport_id = key; 1240 rc = btree_insert32(&lport->lport_fcport_map, key, 1241 new_se_nacl, GFP_ATOMIC); 1242 if (rc) 1243 printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n", 1244 (int)key); 1245 } else { 1246 pr_debug("Wiping nonexisting fc_port entry\n"); 1247 } 1248 1249 fc_port->se_sess = se_sess; 1250 nacl->fc_port = fc_port; 1251 return; 1252 } 1253 1254 if (nacl->fc_port) { 1255 if (new_se_nacl == NULL) { 1256 pr_debug("Clearing existing nacl->fc_port and fc_port entry\n"); 1257 btree_remove32(&lport->lport_fcport_map, key); 1258 nacl->fc_port = NULL; 1259 return; 1260 } 1261 pr_debug("Replacing existing nacl->fc_port and fc_port entry\n"); 1262 btree_update32(&lport->lport_fcport_map, key, new_se_nacl); 1263 fc_port->se_sess = se_sess; 1264 nacl->fc_port = fc_port; 1265 return; 1266 } 1267 1268 if (new_se_nacl == NULL) { 1269 pr_debug("Clearing existing fc_port entry\n"); 1270 btree_remove32(&lport->lport_fcport_map, key); 1271 return; 1272 } 1273 1274 pr_debug("Replacing existing fc_port entry w/o active nacl->fc_port\n"); 1275 btree_update32(&lport->lport_fcport_map, key, new_se_nacl); 1276 fc_port->se_sess = se_sess; 1277 nacl->fc_port = fc_port; 1278 1279 pr_debug("Setup nacl->fc_port %p by s_id for se_nacl: %p, initiatorname: %s\n", 1280 nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname); 1281 } 1282 1283 /* 1284 * Expected to be called with struct qla_hw_data->tgt.sess_lock held 1285 */ 1286 static struct fc_port *tcm_qla2xxx_find_sess_by_loop_id( 1287 scsi_qla_host_t *vha, 1288 const uint16_t loop_id) 1289 { 1290 struct tcm_qla2xxx_lport *lport; 1291 struct se_node_acl *se_nacl; 1292 struct tcm_qla2xxx_nacl *nacl; 1293 struct tcm_qla2xxx_fc_loopid *fc_loopid; 1294 1295 lport = vha->vha_tgt.target_lport_ptr; 1296 if (!lport) { 1297 pr_err("Unable to locate struct tcm_qla2xxx_lport\n"); 1298 dump_stack(); 1299 return NULL; 1300 } 1301 1302 pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id); 1303 1304 fc_loopid = lport->lport_loopid_map + loop_id; 1305 se_nacl = fc_loopid->se_nacl; 1306 if (!se_nacl) { 1307 pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n", 1308 loop_id); 1309 return NULL; 1310 } 1311 1312 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl); 1313 1314 if (!nacl->fc_port) { 1315 pr_err("Unable to locate struct fc_port\n"); 1316 return NULL; 1317 } 1318 1319 return nacl->fc_port; 1320 } 1321 1322 /* 1323 * Expected to be called with struct qla_hw_data->tgt.sess_lock held 1324 */ 1325 static void tcm_qla2xxx_set_sess_by_loop_id( 1326 struct tcm_qla2xxx_lport *lport, 1327 struct se_node_acl *new_se_nacl, 1328 struct tcm_qla2xxx_nacl *nacl, 1329 struct se_session *se_sess, 1330 struct fc_port *fc_port, 1331 uint16_t loop_id) 1332 { 1333 struct se_node_acl *saved_nacl; 1334 struct tcm_qla2xxx_fc_loopid *fc_loopid; 1335 1336 pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id); 1337 1338 fc_loopid = &((struct tcm_qla2xxx_fc_loopid *) 1339 lport->lport_loopid_map)[loop_id]; 1340 1341 saved_nacl = fc_loopid->se_nacl; 1342 if (!saved_nacl) { 1343 pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n"); 1344 fc_loopid->se_nacl = new_se_nacl; 1345 if (fc_port->se_sess != se_sess) 1346 fc_port->se_sess = se_sess; 1347 if (nacl->fc_port != fc_port) 1348 nacl->fc_port = fc_port; 1349 return; 1350 } 1351 1352 if (nacl->fc_port) { 1353 if (new_se_nacl == NULL) { 1354 pr_debug("Clearing nacl->fc_port and fc_loopid->se_nacl\n"); 1355 fc_loopid->se_nacl = NULL; 1356 nacl->fc_port = NULL; 1357 return; 1358 } 1359 1360 pr_debug("Replacing existing nacl->fc_port and fc_loopid->se_nacl\n"); 1361 fc_loopid->se_nacl = new_se_nacl; 1362 if (fc_port->se_sess != se_sess) 1363 fc_port->se_sess = se_sess; 1364 if (nacl->fc_port != fc_port) 1365 nacl->fc_port = fc_port; 1366 return; 1367 } 1368 1369 if (new_se_nacl == NULL) { 1370 pr_debug("Clearing fc_loopid->se_nacl\n"); 1371 fc_loopid->se_nacl = NULL; 1372 return; 1373 } 1374 1375 pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->fc_port\n"); 1376 fc_loopid->se_nacl = new_se_nacl; 1377 if (fc_port->se_sess != se_sess) 1378 fc_port->se_sess = se_sess; 1379 if (nacl->fc_port != fc_port) 1380 nacl->fc_port = fc_port; 1381 1382 pr_debug("Setup nacl->fc_port %p by loop_id for se_nacl: %p, initiatorname: %s\n", 1383 nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname); 1384 } 1385 1386 /* 1387 * Should always be called with qla_hw_data->tgt.sess_lock held. 1388 */ 1389 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport, 1390 struct tcm_qla2xxx_nacl *nacl, struct fc_port *sess) 1391 { 1392 struct se_session *se_sess = sess->se_sess; 1393 1394 tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess, 1395 sess, port_id_to_be_id(sess->d_id)); 1396 tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess, 1397 sess, sess->loop_id); 1398 } 1399 1400 static void tcm_qla2xxx_free_session(struct fc_port *sess) 1401 { 1402 struct qla_tgt *tgt = sess->tgt; 1403 struct qla_hw_data *ha = tgt->ha; 1404 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev); 1405 struct se_session *se_sess; 1406 struct tcm_qla2xxx_lport *lport; 1407 1408 se_sess = sess->se_sess; 1409 if (!se_sess) { 1410 pr_err("struct fc_port->se_sess is NULL\n"); 1411 dump_stack(); 1412 return; 1413 } 1414 1415 lport = vha->vha_tgt.target_lport_ptr; 1416 if (!lport) { 1417 pr_err("Unable to locate struct tcm_qla2xxx_lport\n"); 1418 dump_stack(); 1419 return; 1420 } 1421 target_wait_for_sess_cmds(se_sess); 1422 1423 target_remove_session(se_sess); 1424 } 1425 1426 static int tcm_qla2xxx_session_cb(struct se_portal_group *se_tpg, 1427 struct se_session *se_sess, void *p) 1428 { 1429 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, 1430 struct tcm_qla2xxx_tpg, se_tpg); 1431 struct tcm_qla2xxx_lport *lport = tpg->lport; 1432 struct qla_hw_data *ha = lport->qla_vha->hw; 1433 struct se_node_acl *se_nacl = se_sess->se_node_acl; 1434 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl, 1435 struct tcm_qla2xxx_nacl, se_node_acl); 1436 struct fc_port *qlat_sess = p; 1437 uint16_t loop_id = qlat_sess->loop_id; 1438 unsigned long flags; 1439 1440 /* 1441 * And now setup se_nacl and session pointers into HW lport internal 1442 * mappings for fabric S_ID and LOOP_ID. 1443 */ 1444 spin_lock_irqsave(&ha->tgt.sess_lock, flags); 1445 tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess, qlat_sess, 1446 port_id_to_be_id(qlat_sess->d_id)); 1447 tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, 1448 se_sess, qlat_sess, loop_id); 1449 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags); 1450 1451 return 0; 1452 } 1453 1454 /* 1455 * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl() 1456 * to locate struct se_node_acl 1457 */ 1458 static int tcm_qla2xxx_check_initiator_node_acl( 1459 scsi_qla_host_t *vha, 1460 unsigned char *fc_wwpn, 1461 struct fc_port *qlat_sess) 1462 { 1463 struct qla_hw_data *ha = vha->hw; 1464 struct tcm_qla2xxx_lport *lport; 1465 struct tcm_qla2xxx_tpg *tpg; 1466 struct se_session *se_sess; 1467 unsigned char port_name[36]; 1468 int num_tags = (ha->cur_fw_xcb_count) ? ha->cur_fw_xcb_count : 1469 TCM_QLA2XXX_DEFAULT_TAGS; 1470 1471 lport = vha->vha_tgt.target_lport_ptr; 1472 if (!lport) { 1473 pr_err("Unable to locate struct tcm_qla2xxx_lport\n"); 1474 dump_stack(); 1475 return -EINVAL; 1476 } 1477 /* 1478 * Locate the TPG=1 reference.. 1479 */ 1480 tpg = lport->tpg_1; 1481 if (!tpg) { 1482 pr_err("Unable to locate struct tcm_qla2xxx_lport->tpg_1\n"); 1483 return -EINVAL; 1484 } 1485 /* 1486 * Format the FCP Initiator port_name into colon seperated values to 1487 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs. 1488 */ 1489 memset(&port_name, 0, 36); 1490 snprintf(port_name, sizeof(port_name), "%8phC", fc_wwpn); 1491 /* 1492 * Locate our struct se_node_acl either from an explict NodeACL created 1493 * via ConfigFS, or via running in TPG demo mode. 1494 */ 1495 se_sess = target_setup_session(&tpg->se_tpg, num_tags, 1496 sizeof(struct qla_tgt_cmd), 1497 TARGET_PROT_ALL, port_name, 1498 qlat_sess, tcm_qla2xxx_session_cb); 1499 if (IS_ERR(se_sess)) 1500 return PTR_ERR(se_sess); 1501 1502 return 0; 1503 } 1504 1505 static void tcm_qla2xxx_update_sess(struct fc_port *sess, port_id_t s_id, 1506 uint16_t loop_id, bool conf_compl_supported) 1507 { 1508 struct qla_tgt *tgt = sess->tgt; 1509 struct qla_hw_data *ha = tgt->ha; 1510 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev); 1511 struct tcm_qla2xxx_lport *lport = vha->vha_tgt.target_lport_ptr; 1512 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl; 1513 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl, 1514 struct tcm_qla2xxx_nacl, se_node_acl); 1515 u32 key; 1516 1517 1518 if (sess->loop_id != loop_id || sess->d_id.b24 != s_id.b24) 1519 pr_info("Updating session %p from port %8phC loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n", 1520 sess, sess->port_name, 1521 sess->loop_id, loop_id, sess->d_id.b.domain, 1522 sess->d_id.b.area, sess->d_id.b.al_pa, s_id.b.domain, 1523 s_id.b.area, s_id.b.al_pa); 1524 1525 if (sess->loop_id != loop_id) { 1526 /* 1527 * Because we can shuffle loop IDs around and we 1528 * update different sessions non-atomically, we might 1529 * have overwritten this session's old loop ID 1530 * already, and we might end up overwriting some other 1531 * session that will be updated later. So we have to 1532 * be extra careful and we can't warn about those things... 1533 */ 1534 if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl) 1535 lport->lport_loopid_map[sess->loop_id].se_nacl = NULL; 1536 1537 lport->lport_loopid_map[loop_id].se_nacl = se_nacl; 1538 1539 sess->loop_id = loop_id; 1540 } 1541 1542 if (sess->d_id.b24 != s_id.b24) { 1543 key = (((u32) sess->d_id.b.domain << 16) | 1544 ((u32) sess->d_id.b.area << 8) | 1545 ((u32) sess->d_id.b.al_pa)); 1546 1547 if (btree_lookup32(&lport->lport_fcport_map, key)) 1548 WARN(btree_remove32(&lport->lport_fcport_map, key) != 1549 se_nacl, "Found wrong se_nacl when updating s_id %x:%x:%x\n", 1550 sess->d_id.b.domain, sess->d_id.b.area, 1551 sess->d_id.b.al_pa); 1552 else 1553 WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n", 1554 sess->d_id.b.domain, sess->d_id.b.area, 1555 sess->d_id.b.al_pa); 1556 1557 key = (((u32) s_id.b.domain << 16) | 1558 ((u32) s_id.b.area << 8) | 1559 ((u32) s_id.b.al_pa)); 1560 1561 if (btree_lookup32(&lport->lport_fcport_map, key)) { 1562 WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n", 1563 s_id.b.domain, s_id.b.area, s_id.b.al_pa); 1564 btree_update32(&lport->lport_fcport_map, key, se_nacl); 1565 } else { 1566 btree_insert32(&lport->lport_fcport_map, key, se_nacl, 1567 GFP_ATOMIC); 1568 } 1569 1570 sess->d_id = s_id; 1571 nacl->nport_id = key; 1572 } 1573 1574 sess->conf_compl_supported = conf_compl_supported; 1575 1576 } 1577 1578 /* 1579 * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path. 1580 */ 1581 static const struct qla_tgt_func_tmpl tcm_qla2xxx_template = { 1582 .find_cmd_by_tag = tcm_qla2xxx_find_cmd_by_tag, 1583 .handle_cmd = tcm_qla2xxx_handle_cmd, 1584 .handle_data = tcm_qla2xxx_handle_data, 1585 .handle_tmr = tcm_qla2xxx_handle_tmr, 1586 .get_cmd = tcm_qla2xxx_get_cmd, 1587 .rel_cmd = tcm_qla2xxx_rel_cmd, 1588 .free_cmd = tcm_qla2xxx_free_cmd, 1589 .free_mcmd = tcm_qla2xxx_free_mcmd, 1590 .free_session = tcm_qla2xxx_free_session, 1591 .update_sess = tcm_qla2xxx_update_sess, 1592 .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl, 1593 .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id, 1594 .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id, 1595 .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map, 1596 .put_sess = tcm_qla2xxx_put_sess, 1597 .shutdown_sess = tcm_qla2xxx_shutdown_sess, 1598 .get_dif_tags = tcm_qla2xxx_dif_tags, 1599 .chk_dif_tags = tcm_qla2xxx_chk_dif_tags, 1600 }; 1601 1602 static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport) 1603 { 1604 int rc; 1605 1606 rc = btree_init32(&lport->lport_fcport_map); 1607 if (rc) { 1608 pr_err("Unable to initialize lport->lport_fcport_map btree\n"); 1609 return rc; 1610 } 1611 1612 lport->lport_loopid_map = 1613 vzalloc(array_size(65536, 1614 sizeof(struct tcm_qla2xxx_fc_loopid))); 1615 if (!lport->lport_loopid_map) { 1616 pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n", 1617 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536); 1618 btree_destroy32(&lport->lport_fcport_map); 1619 return -ENOMEM; 1620 } 1621 pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n", 1622 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536); 1623 return 0; 1624 } 1625 1626 static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha, 1627 void *target_lport_ptr, 1628 u64 npiv_wwpn, u64 npiv_wwnn) 1629 { 1630 struct qla_hw_data *ha = vha->hw; 1631 struct tcm_qla2xxx_lport *lport = 1632 (struct tcm_qla2xxx_lport *)target_lport_ptr; 1633 /* 1634 * Setup tgt_ops, local pointer to vha and target_lport_ptr 1635 */ 1636 ha->tgt.tgt_ops = &tcm_qla2xxx_template; 1637 vha->vha_tgt.target_lport_ptr = target_lport_ptr; 1638 lport->qla_vha = vha; 1639 1640 return 0; 1641 } 1642 1643 static struct se_wwn *tcm_qla2xxx_make_lport( 1644 struct target_fabric_configfs *tf, 1645 struct config_group *group, 1646 const char *name) 1647 { 1648 struct tcm_qla2xxx_lport *lport; 1649 u64 wwpn; 1650 int ret = -ENODEV; 1651 1652 if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0) 1653 return ERR_PTR(-EINVAL); 1654 1655 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL); 1656 if (!lport) { 1657 pr_err("Unable to allocate struct tcm_qla2xxx_lport\n"); 1658 return ERR_PTR(-ENOMEM); 1659 } 1660 lport->lport_wwpn = wwpn; 1661 tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN, 1662 wwpn); 1663 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn); 1664 1665 ret = tcm_qla2xxx_init_lport(lport); 1666 if (ret != 0) 1667 goto out; 1668 1669 ret = qlt_lport_register(lport, wwpn, 0, 0, 1670 tcm_qla2xxx_lport_register_cb); 1671 if (ret != 0) 1672 goto out_lport; 1673 1674 return &lport->lport_wwn; 1675 out_lport: 1676 vfree(lport->lport_loopid_map); 1677 btree_destroy32(&lport->lport_fcport_map); 1678 out: 1679 kfree(lport); 1680 return ERR_PTR(ret); 1681 } 1682 1683 static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn) 1684 { 1685 struct tcm_qla2xxx_lport *lport = container_of(wwn, 1686 struct tcm_qla2xxx_lport, lport_wwn); 1687 struct scsi_qla_host *vha = lport->qla_vha; 1688 struct se_node_acl *node; 1689 u32 key = 0; 1690 1691 /* 1692 * Call into qla2x_target.c LLD logic to complete the 1693 * shutdown of struct qla_tgt after the call to 1694 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above.. 1695 */ 1696 if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stopped) 1697 qlt_stop_phase2(vha->vha_tgt.qla_tgt); 1698 1699 qlt_lport_deregister(vha); 1700 1701 vfree(lport->lport_loopid_map); 1702 btree_for_each_safe32(&lport->lport_fcport_map, key, node) 1703 btree_remove32(&lport->lport_fcport_map, key); 1704 btree_destroy32(&lport->lport_fcport_map); 1705 kfree(lport); 1706 } 1707 1708 static int tcm_qla2xxx_lport_register_npiv_cb(struct scsi_qla_host *base_vha, 1709 void *target_lport_ptr, 1710 u64 npiv_wwpn, u64 npiv_wwnn) 1711 { 1712 struct fc_vport *vport; 1713 struct Scsi_Host *sh = base_vha->host; 1714 struct scsi_qla_host *npiv_vha; 1715 struct tcm_qla2xxx_lport *lport = 1716 (struct tcm_qla2xxx_lport *)target_lport_ptr; 1717 struct tcm_qla2xxx_lport *base_lport = 1718 (struct tcm_qla2xxx_lport *)base_vha->vha_tgt.target_lport_ptr; 1719 struct fc_vport_identifiers vport_id; 1720 1721 if (qla_ini_mode_enabled(base_vha)) { 1722 pr_err("qla2xxx base_vha not enabled for target mode\n"); 1723 return -EPERM; 1724 } 1725 1726 if (!base_lport || !base_lport->tpg_1 || 1727 !atomic_read(&base_lport->tpg_1->lport_tpg_enabled)) { 1728 pr_err("qla2xxx base_lport or tpg_1 not available\n"); 1729 return -EPERM; 1730 } 1731 1732 memset(&vport_id, 0, sizeof(vport_id)); 1733 vport_id.port_name = npiv_wwpn; 1734 vport_id.node_name = npiv_wwnn; 1735 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR; 1736 vport_id.vport_type = FC_PORTTYPE_NPIV; 1737 vport_id.disable = false; 1738 1739 vport = fc_vport_create(sh, 0, &vport_id); 1740 if (!vport) { 1741 pr_err("fc_vport_create failed for qla2xxx_npiv\n"); 1742 return -ENODEV; 1743 } 1744 /* 1745 * Setup local pointer to NPIV vhba + target_lport_ptr 1746 */ 1747 npiv_vha = (struct scsi_qla_host *)vport->dd_data; 1748 npiv_vha->vha_tgt.target_lport_ptr = target_lport_ptr; 1749 lport->qla_vha = npiv_vha; 1750 scsi_host_get(npiv_vha->host); 1751 return 0; 1752 } 1753 1754 1755 static struct se_wwn *tcm_qla2xxx_npiv_make_lport( 1756 struct target_fabric_configfs *tf, 1757 struct config_group *group, 1758 const char *name) 1759 { 1760 struct tcm_qla2xxx_lport *lport; 1761 u64 phys_wwpn, npiv_wwpn, npiv_wwnn; 1762 char *p, tmp[128]; 1763 int ret; 1764 1765 snprintf(tmp, 128, "%s", name); 1766 1767 p = strchr(tmp, '@'); 1768 if (!p) { 1769 pr_err("Unable to locate NPIV '@' separator\n"); 1770 return ERR_PTR(-EINVAL); 1771 } 1772 *p++ = '\0'; 1773 1774 if (tcm_qla2xxx_parse_wwn(tmp, &phys_wwpn, 1) < 0) 1775 return ERR_PTR(-EINVAL); 1776 1777 if (tcm_qla2xxx_npiv_parse_wwn(p, strlen(p)+1, 1778 &npiv_wwpn, &npiv_wwnn) < 0) 1779 return ERR_PTR(-EINVAL); 1780 1781 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL); 1782 if (!lport) { 1783 pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n"); 1784 return ERR_PTR(-ENOMEM); 1785 } 1786 lport->lport_npiv_wwpn = npiv_wwpn; 1787 lport->lport_npiv_wwnn = npiv_wwnn; 1788 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn); 1789 1790 ret = tcm_qla2xxx_init_lport(lport); 1791 if (ret != 0) 1792 goto out; 1793 1794 ret = qlt_lport_register(lport, phys_wwpn, npiv_wwpn, npiv_wwnn, 1795 tcm_qla2xxx_lport_register_npiv_cb); 1796 if (ret != 0) 1797 goto out_lport; 1798 1799 return &lport->lport_wwn; 1800 out_lport: 1801 vfree(lport->lport_loopid_map); 1802 btree_destroy32(&lport->lport_fcport_map); 1803 out: 1804 kfree(lport); 1805 return ERR_PTR(ret); 1806 } 1807 1808 static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn) 1809 { 1810 struct tcm_qla2xxx_lport *lport = container_of(wwn, 1811 struct tcm_qla2xxx_lport, lport_wwn); 1812 struct scsi_qla_host *npiv_vha = lport->qla_vha; 1813 struct qla_hw_data *ha = npiv_vha->hw; 1814 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev); 1815 1816 scsi_host_put(npiv_vha->host); 1817 /* 1818 * Notify libfc that we want to release the vha->fc_vport 1819 */ 1820 fc_vport_terminate(npiv_vha->fc_vport); 1821 scsi_host_put(base_vha->host); 1822 kfree(lport); 1823 } 1824 1825 1826 static ssize_t tcm_qla2xxx_wwn_version_show(struct config_item *item, 1827 char *page) 1828 { 1829 return sprintf(page, 1830 "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on %s\n", 1831 QLA2XXX_VERSION, utsname()->sysname, 1832 utsname()->machine, utsname()->release); 1833 } 1834 1835 CONFIGFS_ATTR_RO(tcm_qla2xxx_wwn_, version); 1836 1837 static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = { 1838 &tcm_qla2xxx_wwn_attr_version, 1839 NULL, 1840 }; 1841 1842 static const struct target_core_fabric_ops tcm_qla2xxx_ops = { 1843 .module = THIS_MODULE, 1844 .fabric_name = "qla2xxx", 1845 .node_acl_size = sizeof(struct tcm_qla2xxx_nacl), 1846 /* 1847 * XXX: Limit assumes single page per scatter-gather-list entry. 1848 * Current maximum is ~4.9 MB per se_cmd->t_data_sg with PAGE_SIZE=4096 1849 */ 1850 .max_data_sg_nents = 1200, 1851 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn, 1852 .tpg_get_tag = tcm_qla2xxx_get_tag, 1853 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode, 1854 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache, 1855 .tpg_check_demo_mode_write_protect = 1856 tcm_qla2xxx_check_demo_write_protect, 1857 .tpg_check_prod_mode_write_protect = 1858 tcm_qla2xxx_check_prod_write_protect, 1859 .tpg_check_prot_fabric_only = tcm_qla2xxx_check_prot_fabric_only, 1860 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only, 1861 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index, 1862 .check_stop_free = tcm_qla2xxx_check_stop_free, 1863 .release_cmd = tcm_qla2xxx_release_cmd, 1864 .close_session = tcm_qla2xxx_close_session, 1865 .sess_get_index = tcm_qla2xxx_sess_get_index, 1866 .sess_get_initiator_sid = NULL, 1867 .write_pending = tcm_qla2xxx_write_pending, 1868 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs, 1869 .get_cmd_state = tcm_qla2xxx_get_cmd_state, 1870 .queue_data_in = tcm_qla2xxx_queue_data_in, 1871 .queue_status = tcm_qla2xxx_queue_status, 1872 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp, 1873 .aborted_task = tcm_qla2xxx_aborted_task, 1874 /* 1875 * Setup function pointers for generic logic in 1876 * target_core_fabric_configfs.c 1877 */ 1878 .fabric_make_wwn = tcm_qla2xxx_make_lport, 1879 .fabric_drop_wwn = tcm_qla2xxx_drop_lport, 1880 .fabric_make_tpg = tcm_qla2xxx_make_tpg, 1881 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg, 1882 .fabric_init_nodeacl = tcm_qla2xxx_init_nodeacl, 1883 1884 .tfc_wwn_attrs = tcm_qla2xxx_wwn_attrs, 1885 .tfc_tpg_base_attrs = tcm_qla2xxx_tpg_attrs, 1886 .tfc_tpg_attrib_attrs = tcm_qla2xxx_tpg_attrib_attrs, 1887 }; 1888 1889 static const struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = { 1890 .module = THIS_MODULE, 1891 .fabric_name = "qla2xxx_npiv", 1892 .node_acl_size = sizeof(struct tcm_qla2xxx_nacl), 1893 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn, 1894 .tpg_get_tag = tcm_qla2xxx_get_tag, 1895 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode, 1896 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache, 1897 .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_demo_mode, 1898 .tpg_check_prod_mode_write_protect = 1899 tcm_qla2xxx_check_prod_write_protect, 1900 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only, 1901 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index, 1902 .check_stop_free = tcm_qla2xxx_check_stop_free, 1903 .release_cmd = tcm_qla2xxx_release_cmd, 1904 .close_session = tcm_qla2xxx_close_session, 1905 .sess_get_index = tcm_qla2xxx_sess_get_index, 1906 .sess_get_initiator_sid = NULL, 1907 .write_pending = tcm_qla2xxx_write_pending, 1908 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs, 1909 .get_cmd_state = tcm_qla2xxx_get_cmd_state, 1910 .queue_data_in = tcm_qla2xxx_queue_data_in, 1911 .queue_status = tcm_qla2xxx_queue_status, 1912 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp, 1913 .aborted_task = tcm_qla2xxx_aborted_task, 1914 /* 1915 * Setup function pointers for generic logic in 1916 * target_core_fabric_configfs.c 1917 */ 1918 .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport, 1919 .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport, 1920 .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg, 1921 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg, 1922 .fabric_init_nodeacl = tcm_qla2xxx_init_nodeacl, 1923 1924 .tfc_wwn_attrs = tcm_qla2xxx_wwn_attrs, 1925 .tfc_tpg_base_attrs = tcm_qla2xxx_npiv_tpg_attrs, 1926 }; 1927 1928 static int tcm_qla2xxx_register_configfs(void) 1929 { 1930 int ret; 1931 1932 pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on %s\n", 1933 QLA2XXX_VERSION, utsname()->sysname, 1934 utsname()->machine, utsname()->release); 1935 1936 ret = target_register_template(&tcm_qla2xxx_ops); 1937 if (ret) 1938 return ret; 1939 1940 ret = target_register_template(&tcm_qla2xxx_npiv_ops); 1941 if (ret) 1942 goto out_fabric; 1943 1944 tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free", 1945 WQ_MEM_RECLAIM, 0); 1946 if (!tcm_qla2xxx_free_wq) { 1947 ret = -ENOMEM; 1948 goto out_fabric_npiv; 1949 } 1950 1951 return 0; 1952 1953 out_fabric_npiv: 1954 target_unregister_template(&tcm_qla2xxx_npiv_ops); 1955 out_fabric: 1956 target_unregister_template(&tcm_qla2xxx_ops); 1957 return ret; 1958 } 1959 1960 static void tcm_qla2xxx_deregister_configfs(void) 1961 { 1962 destroy_workqueue(tcm_qla2xxx_free_wq); 1963 1964 target_unregister_template(&tcm_qla2xxx_ops); 1965 target_unregister_template(&tcm_qla2xxx_npiv_ops); 1966 } 1967 1968 static int __init tcm_qla2xxx_init(void) 1969 { 1970 int ret; 1971 1972 BUILD_BUG_ON(sizeof(struct abts_recv_from_24xx) != 64); 1973 BUILD_BUG_ON(sizeof(struct abts_resp_from_24xx_fw) != 64); 1974 BUILD_BUG_ON(sizeof(struct atio7_fcp_cmnd) != 32); 1975 BUILD_BUG_ON(sizeof(struct atio_from_isp) != 64); 1976 BUILD_BUG_ON(sizeof(struct ba_acc_le) != 12); 1977 BUILD_BUG_ON(sizeof(struct ba_rjt_le) != 4); 1978 BUILD_BUG_ON(sizeof(struct ctio7_from_24xx) != 64); 1979 BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64); 1980 BUILD_BUG_ON(sizeof(struct ctio_crc2_to_fw) != 64); 1981 BUILD_BUG_ON(sizeof(struct ctio_crc_from_fw) != 64); 1982 BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64); 1983 BUILD_BUG_ON(sizeof(struct fcp_hdr) != 24); 1984 BUILD_BUG_ON(sizeof(struct fcp_hdr_le) != 24); 1985 BUILD_BUG_ON(sizeof(struct nack_to_isp) != 64); 1986 1987 ret = tcm_qla2xxx_register_configfs(); 1988 if (ret < 0) 1989 return ret; 1990 1991 return 0; 1992 } 1993 1994 static void __exit tcm_qla2xxx_exit(void) 1995 { 1996 tcm_qla2xxx_deregister_configfs(); 1997 } 1998 1999 MODULE_DESCRIPTION("TCM QLA24XX+ series NPIV enabled fabric driver"); 2000 MODULE_LICENSE("GPL"); 2001 module_init(tcm_qla2xxx_init); 2002 module_exit(tcm_qla2xxx_exit); 2003