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