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