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