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