1 /******************************************************************************* 2 * 3 * This file contains the Linux/SCSI LLD virtual SCSI initiator driver 4 * for emulated SAS initiator ports 5 * 6 * © Copyright 2011-2013 Datera, Inc. 7 * 8 * Licensed to the Linux Foundation under the General Public License (GPL) version 2. 9 * 10 * Author: Nicholas A. Bellinger <nab@risingtidesystems.com> 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 ****************************************************************************/ 22 23 #include <linux/module.h> 24 #include <linux/moduleparam.h> 25 #include <linux/init.h> 26 #include <linux/slab.h> 27 #include <linux/types.h> 28 #include <linux/configfs.h> 29 #include <scsi/scsi.h> 30 #include <scsi/scsi_tcq.h> 31 #include <scsi/scsi_host.h> 32 #include <scsi/scsi_device.h> 33 #include <scsi/scsi_cmnd.h> 34 35 #include <target/target_core_base.h> 36 #include <target/target_core_fabric.h> 37 38 #include "tcm_loop.h" 39 40 #define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev) 41 42 static struct workqueue_struct *tcm_loop_workqueue; 43 static struct kmem_cache *tcm_loop_cmd_cache; 44 45 static int tcm_loop_hba_no_cnt; 46 47 static int tcm_loop_queue_status(struct se_cmd *se_cmd); 48 49 static unsigned int tcm_loop_nr_hw_queues = 1; 50 module_param_named(nr_hw_queues, tcm_loop_nr_hw_queues, uint, 0644); 51 52 static unsigned int tcm_loop_can_queue = 1024; 53 module_param_named(can_queue, tcm_loop_can_queue, uint, 0644); 54 55 static unsigned int tcm_loop_cmd_per_lun = 1024; 56 module_param_named(cmd_per_lun, tcm_loop_cmd_per_lun, uint, 0644); 57 58 /* 59 * Called from struct target_core_fabric_ops->check_stop_free() 60 */ 61 static int tcm_loop_check_stop_free(struct se_cmd *se_cmd) 62 { 63 return transport_generic_free_cmd(se_cmd, 0); 64 } 65 66 static void tcm_loop_release_cmd(struct se_cmd *se_cmd) 67 { 68 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 69 struct tcm_loop_cmd, tl_se_cmd); 70 71 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd); 72 } 73 74 static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host) 75 { 76 seq_puts(m, "tcm_loop_proc_info()\n"); 77 return 0; 78 } 79 80 static int tcm_loop_driver_probe(struct device *); 81 static int tcm_loop_driver_remove(struct device *); 82 83 static int pseudo_lld_bus_match(struct device *dev, 84 struct device_driver *dev_driver) 85 { 86 return 1; 87 } 88 89 static struct bus_type tcm_loop_lld_bus = { 90 .name = "tcm_loop_bus", 91 .match = pseudo_lld_bus_match, 92 .probe = tcm_loop_driver_probe, 93 .remove = tcm_loop_driver_remove, 94 }; 95 96 static struct device_driver tcm_loop_driverfs = { 97 .name = "tcm_loop", 98 .bus = &tcm_loop_lld_bus, 99 }; 100 /* 101 * Used with root_device_register() in tcm_loop_alloc_core_bus() below 102 */ 103 static struct device *tcm_loop_primary; 104 105 static void tcm_loop_submission_work(struct work_struct *work) 106 { 107 struct tcm_loop_cmd *tl_cmd = 108 container_of(work, struct tcm_loop_cmd, work); 109 struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd; 110 struct scsi_cmnd *sc = tl_cmd->sc; 111 struct tcm_loop_nexus *tl_nexus; 112 struct tcm_loop_hba *tl_hba; 113 struct tcm_loop_tpg *tl_tpg; 114 struct scatterlist *sgl_bidi = NULL; 115 u32 sgl_bidi_count = 0, transfer_length; 116 117 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 118 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 119 120 /* 121 * Ensure that this tl_tpg reference from the incoming sc->device->id 122 * has already been configured via tcm_loop_make_naa_tpg(). 123 */ 124 if (!tl_tpg->tl_hba) { 125 set_host_byte(sc, DID_NO_CONNECT); 126 goto out_done; 127 } 128 if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) { 129 set_host_byte(sc, DID_TRANSPORT_DISRUPTED); 130 goto out_done; 131 } 132 tl_nexus = tl_tpg->tl_nexus; 133 if (!tl_nexus) { 134 scmd_printk(KERN_ERR, sc, 135 "TCM_Loop I_T Nexus does not exist\n"); 136 set_host_byte(sc, DID_ERROR); 137 goto out_done; 138 } 139 140 transfer_length = scsi_transfer_length(sc); 141 if (!scsi_prot_sg_count(sc) && 142 scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) { 143 se_cmd->prot_pto = true; 144 /* 145 * loopback transport doesn't support 146 * WRITE_GENERATE, READ_STRIP protection 147 * information operations, go ahead unprotected. 148 */ 149 transfer_length = scsi_bufflen(sc); 150 } 151 152 se_cmd->tag = tl_cmd->sc_cmd_tag; 153 target_init_cmd(se_cmd, tl_nexus->se_sess, &tl_cmd->tl_sense_buf[0], 154 tl_cmd->sc->device->lun, transfer_length, 155 TCM_SIMPLE_TAG, sc->sc_data_direction, 0); 156 157 if (target_submit_prep(se_cmd, sc->cmnd, scsi_sglist(sc), 158 scsi_sg_count(sc), sgl_bidi, sgl_bidi_count, 159 scsi_prot_sglist(sc), scsi_prot_sg_count(sc), 160 GFP_NOIO)) 161 return; 162 163 target_submit(se_cmd); 164 return; 165 166 out_done: 167 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd); 168 sc->scsi_done(sc); 169 } 170 171 /* 172 * ->queuecommand can be and usually is called from interrupt context, so 173 * defer the actual submission to a workqueue. 174 */ 175 static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc) 176 { 177 struct tcm_loop_cmd *tl_cmd; 178 179 pr_debug("%s() %d:%d:%d:%llu got CDB: 0x%02x scsi_buf_len: %u\n", 180 __func__, sc->device->host->host_no, sc->device->id, 181 sc->device->channel, sc->device->lun, sc->cmnd[0], 182 scsi_bufflen(sc)); 183 184 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC); 185 if (!tl_cmd) { 186 set_host_byte(sc, DID_ERROR); 187 sc->scsi_done(sc); 188 return 0; 189 } 190 191 tl_cmd->sc = sc; 192 tl_cmd->sc_cmd_tag = sc->request->tag; 193 INIT_WORK(&tl_cmd->work, tcm_loop_submission_work); 194 queue_work(tcm_loop_workqueue, &tl_cmd->work); 195 return 0; 196 } 197 198 /* 199 * Called from SCSI EH process context to issue a LUN_RESET TMR 200 * to struct scsi_device 201 */ 202 static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg, 203 u64 lun, int task, enum tcm_tmreq_table tmr) 204 { 205 struct se_cmd *se_cmd; 206 struct se_session *se_sess; 207 struct tcm_loop_nexus *tl_nexus; 208 struct tcm_loop_cmd *tl_cmd; 209 int ret = TMR_FUNCTION_FAILED, rc; 210 211 /* 212 * Locate the tl_nexus and se_sess pointers 213 */ 214 tl_nexus = tl_tpg->tl_nexus; 215 if (!tl_nexus) { 216 pr_err("Unable to perform device reset without active I_T Nexus\n"); 217 return ret; 218 } 219 220 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL); 221 if (!tl_cmd) 222 return ret; 223 224 init_completion(&tl_cmd->tmr_done); 225 226 se_cmd = &tl_cmd->tl_se_cmd; 227 se_sess = tl_tpg->tl_nexus->se_sess; 228 229 rc = target_submit_tmr(se_cmd, se_sess, tl_cmd->tl_sense_buf, lun, 230 NULL, tmr, GFP_KERNEL, task, 231 TARGET_SCF_ACK_KREF); 232 if (rc < 0) 233 goto release; 234 wait_for_completion(&tl_cmd->tmr_done); 235 ret = se_cmd->se_tmr_req->response; 236 target_put_sess_cmd(se_cmd); 237 238 out: 239 return ret; 240 241 release: 242 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd); 243 goto out; 244 } 245 246 static int tcm_loop_abort_task(struct scsi_cmnd *sc) 247 { 248 struct tcm_loop_hba *tl_hba; 249 struct tcm_loop_tpg *tl_tpg; 250 int ret = FAILED; 251 252 /* 253 * Locate the tcm_loop_hba_t pointer 254 */ 255 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 256 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 257 ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun, 258 sc->request->tag, TMR_ABORT_TASK); 259 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED; 260 } 261 262 /* 263 * Called from SCSI EH process context to issue a LUN_RESET TMR 264 * to struct scsi_device 265 */ 266 static int tcm_loop_device_reset(struct scsi_cmnd *sc) 267 { 268 struct tcm_loop_hba *tl_hba; 269 struct tcm_loop_tpg *tl_tpg; 270 int ret = FAILED; 271 272 /* 273 * Locate the tcm_loop_hba_t pointer 274 */ 275 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 276 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 277 278 ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun, 279 0, TMR_LUN_RESET); 280 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED; 281 } 282 283 static int tcm_loop_target_reset(struct scsi_cmnd *sc) 284 { 285 struct tcm_loop_hba *tl_hba; 286 struct tcm_loop_tpg *tl_tpg; 287 288 /* 289 * Locate the tcm_loop_hba_t pointer 290 */ 291 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 292 if (!tl_hba) { 293 pr_err("Unable to perform device reset without active I_T Nexus\n"); 294 return FAILED; 295 } 296 /* 297 * Locate the tl_tpg pointer from TargetID in sc->device->id 298 */ 299 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 300 if (tl_tpg) { 301 tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE; 302 return SUCCESS; 303 } 304 return FAILED; 305 } 306 307 static struct scsi_host_template tcm_loop_driver_template = { 308 .show_info = tcm_loop_show_info, 309 .proc_name = "tcm_loopback", 310 .name = "TCM_Loopback", 311 .queuecommand = tcm_loop_queuecommand, 312 .change_queue_depth = scsi_change_queue_depth, 313 .eh_abort_handler = tcm_loop_abort_task, 314 .eh_device_reset_handler = tcm_loop_device_reset, 315 .eh_target_reset_handler = tcm_loop_target_reset, 316 .this_id = -1, 317 .sg_tablesize = 256, 318 .max_sectors = 0xFFFF, 319 .dma_boundary = PAGE_SIZE - 1, 320 .module = THIS_MODULE, 321 .track_queue_depth = 1, 322 }; 323 324 static int tcm_loop_driver_probe(struct device *dev) 325 { 326 struct tcm_loop_hba *tl_hba; 327 struct Scsi_Host *sh; 328 int error, host_prot; 329 330 tl_hba = to_tcm_loop_hba(dev); 331 332 sh = scsi_host_alloc(&tcm_loop_driver_template, 333 sizeof(struct tcm_loop_hba)); 334 if (!sh) { 335 pr_err("Unable to allocate struct scsi_host\n"); 336 return -ENODEV; 337 } 338 tl_hba->sh = sh; 339 340 /* 341 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata 342 */ 343 *((struct tcm_loop_hba **)sh->hostdata) = tl_hba; 344 /* 345 * Setup single ID, Channel and LUN for now.. 346 */ 347 sh->max_id = 2; 348 sh->max_lun = 0; 349 sh->max_channel = 0; 350 sh->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE; 351 sh->nr_hw_queues = tcm_loop_nr_hw_queues; 352 sh->can_queue = tcm_loop_can_queue; 353 sh->cmd_per_lun = tcm_loop_cmd_per_lun; 354 355 host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION | 356 SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION | 357 SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION; 358 359 scsi_host_set_prot(sh, host_prot); 360 scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC); 361 362 error = scsi_add_host(sh, &tl_hba->dev); 363 if (error) { 364 pr_err("%s: scsi_add_host failed\n", __func__); 365 scsi_host_put(sh); 366 return -ENODEV; 367 } 368 return 0; 369 } 370 371 static int tcm_loop_driver_remove(struct device *dev) 372 { 373 struct tcm_loop_hba *tl_hba; 374 struct Scsi_Host *sh; 375 376 tl_hba = to_tcm_loop_hba(dev); 377 sh = tl_hba->sh; 378 379 scsi_remove_host(sh); 380 scsi_host_put(sh); 381 return 0; 382 } 383 384 static void tcm_loop_release_adapter(struct device *dev) 385 { 386 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev); 387 388 kfree(tl_hba); 389 } 390 391 /* 392 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c 393 */ 394 static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id) 395 { 396 int ret; 397 398 tl_hba->dev.bus = &tcm_loop_lld_bus; 399 tl_hba->dev.parent = tcm_loop_primary; 400 tl_hba->dev.release = &tcm_loop_release_adapter; 401 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id); 402 403 ret = device_register(&tl_hba->dev); 404 if (ret) { 405 pr_err("device_register() failed for tl_hba->dev: %d\n", ret); 406 return -ENODEV; 407 } 408 409 return 0; 410 } 411 412 /* 413 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated 414 * tcm_loop SCSI bus. 415 */ 416 static int tcm_loop_alloc_core_bus(void) 417 { 418 int ret; 419 420 tcm_loop_primary = root_device_register("tcm_loop_0"); 421 if (IS_ERR(tcm_loop_primary)) { 422 pr_err("Unable to allocate tcm_loop_primary\n"); 423 return PTR_ERR(tcm_loop_primary); 424 } 425 426 ret = bus_register(&tcm_loop_lld_bus); 427 if (ret) { 428 pr_err("bus_register() failed for tcm_loop_lld_bus\n"); 429 goto dev_unreg; 430 } 431 432 ret = driver_register(&tcm_loop_driverfs); 433 if (ret) { 434 pr_err("driver_register() failed for tcm_loop_driverfs\n"); 435 goto bus_unreg; 436 } 437 438 pr_debug("Initialized TCM Loop Core Bus\n"); 439 return ret; 440 441 bus_unreg: 442 bus_unregister(&tcm_loop_lld_bus); 443 dev_unreg: 444 root_device_unregister(tcm_loop_primary); 445 return ret; 446 } 447 448 static void tcm_loop_release_core_bus(void) 449 { 450 driver_unregister(&tcm_loop_driverfs); 451 bus_unregister(&tcm_loop_lld_bus); 452 root_device_unregister(tcm_loop_primary); 453 454 pr_debug("Releasing TCM Loop Core BUS\n"); 455 } 456 457 static inline struct tcm_loop_tpg *tl_tpg(struct se_portal_group *se_tpg) 458 { 459 return container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg); 460 } 461 462 static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg) 463 { 464 /* 465 * Return the passed NAA identifier for the Target Port 466 */ 467 return &tl_tpg(se_tpg)->tl_hba->tl_wwn_address[0]; 468 } 469 470 static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg) 471 { 472 /* 473 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83 474 * to represent the SCSI Target Port. 475 */ 476 return tl_tpg(se_tpg)->tl_tpgt; 477 } 478 479 /* 480 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated 481 * based upon the incoming fabric dependent SCSI Initiator Port 482 */ 483 static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg) 484 { 485 return 1; 486 } 487 488 static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg) 489 { 490 return 0; 491 } 492 493 /* 494 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for 495 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest 496 */ 497 static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg) 498 { 499 return 0; 500 } 501 502 /* 503 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will 504 * never be called for TCM_Loop by target_core_fabric_configfs.c code. 505 * It has been added here as a nop for target_fabric_tf_ops_check() 506 */ 507 static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg) 508 { 509 return 0; 510 } 511 512 static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg) 513 { 514 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, 515 tl_se_tpg); 516 return tl_tpg->tl_fabric_prot_type; 517 } 518 519 static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg) 520 { 521 return 1; 522 } 523 524 static u32 tcm_loop_sess_get_index(struct se_session *se_sess) 525 { 526 return 1; 527 } 528 529 static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl) 530 { 531 return; 532 } 533 534 static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd) 535 { 536 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 537 struct tcm_loop_cmd, tl_se_cmd); 538 539 return tl_cmd->sc_cmd_state; 540 } 541 542 static int tcm_loop_write_pending(struct se_cmd *se_cmd) 543 { 544 /* 545 * Since Linux/SCSI has already sent down a struct scsi_cmnd 546 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array 547 * memory, and memory has already been mapped to struct se_cmd->t_mem_list 548 * format with transport_generic_map_mem_to_cmd(). 549 * 550 * We now tell TCM to add this WRITE CDB directly into the TCM storage 551 * object execution queue. 552 */ 553 target_execute_cmd(se_cmd); 554 return 0; 555 } 556 557 static int tcm_loop_queue_data_or_status(const char *func, 558 struct se_cmd *se_cmd, u8 scsi_status) 559 { 560 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 561 struct tcm_loop_cmd, tl_se_cmd); 562 struct scsi_cmnd *sc = tl_cmd->sc; 563 564 pr_debug("%s() called for scsi_cmnd: %p cdb: 0x%02x\n", 565 func, sc, sc->cmnd[0]); 566 567 if (se_cmd->sense_buffer && 568 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || 569 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) { 570 571 memcpy(sc->sense_buffer, se_cmd->sense_buffer, 572 SCSI_SENSE_BUFFERSIZE); 573 sc->result = SAM_STAT_CHECK_CONDITION; 574 set_driver_byte(sc, DRIVER_SENSE); 575 } else 576 sc->result = scsi_status; 577 578 set_host_byte(sc, DID_OK); 579 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) || 580 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT)) 581 scsi_set_resid(sc, se_cmd->residual_count); 582 sc->scsi_done(sc); 583 return 0; 584 } 585 586 static int tcm_loop_queue_data_in(struct se_cmd *se_cmd) 587 { 588 return tcm_loop_queue_data_or_status(__func__, se_cmd, SAM_STAT_GOOD); 589 } 590 591 static int tcm_loop_queue_status(struct se_cmd *se_cmd) 592 { 593 return tcm_loop_queue_data_or_status(__func__, 594 se_cmd, se_cmd->scsi_status); 595 } 596 597 static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd) 598 { 599 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 600 struct tcm_loop_cmd, tl_se_cmd); 601 602 /* Wake up tcm_loop_issue_tmr(). */ 603 complete(&tl_cmd->tmr_done); 604 } 605 606 static void tcm_loop_aborted_task(struct se_cmd *se_cmd) 607 { 608 return; 609 } 610 611 static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba) 612 { 613 switch (tl_hba->tl_proto_id) { 614 case SCSI_PROTOCOL_SAS: 615 return "SAS"; 616 case SCSI_PROTOCOL_FCP: 617 return "FCP"; 618 case SCSI_PROTOCOL_ISCSI: 619 return "iSCSI"; 620 default: 621 break; 622 } 623 624 return "Unknown"; 625 } 626 627 /* Start items for tcm_loop_port_cit */ 628 629 static int tcm_loop_port_link( 630 struct se_portal_group *se_tpg, 631 struct se_lun *lun) 632 { 633 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 634 struct tcm_loop_tpg, tl_se_tpg); 635 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 636 637 atomic_inc_mb(&tl_tpg->tl_tpg_port_count); 638 /* 639 * Add Linux/SCSI struct scsi_device by HCTL 640 */ 641 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun); 642 643 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n"); 644 return 0; 645 } 646 647 static void tcm_loop_port_unlink( 648 struct se_portal_group *se_tpg, 649 struct se_lun *se_lun) 650 { 651 struct scsi_device *sd; 652 struct tcm_loop_hba *tl_hba; 653 struct tcm_loop_tpg *tl_tpg; 654 655 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg); 656 tl_hba = tl_tpg->tl_hba; 657 658 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt, 659 se_lun->unpacked_lun); 660 if (!sd) { 661 pr_err("Unable to locate struct scsi_device for %d:%d:%llu\n", 662 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun); 663 return; 664 } 665 /* 666 * Remove Linux/SCSI struct scsi_device by HCTL 667 */ 668 scsi_remove_device(sd); 669 scsi_device_put(sd); 670 671 atomic_dec_mb(&tl_tpg->tl_tpg_port_count); 672 673 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n"); 674 } 675 676 /* End items for tcm_loop_port_cit */ 677 678 static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_show( 679 struct config_item *item, char *page) 680 { 681 struct se_portal_group *se_tpg = attrib_to_tpg(item); 682 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, 683 tl_se_tpg); 684 685 return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type); 686 } 687 688 static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_store( 689 struct config_item *item, const char *page, size_t count) 690 { 691 struct se_portal_group *se_tpg = attrib_to_tpg(item); 692 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, 693 tl_se_tpg); 694 unsigned long val; 695 int ret = kstrtoul(page, 0, &val); 696 697 if (ret) { 698 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret); 699 return ret; 700 } 701 if (val != 0 && val != 1 && val != 3) { 702 pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val); 703 return -EINVAL; 704 } 705 tl_tpg->tl_fabric_prot_type = val; 706 707 return count; 708 } 709 710 CONFIGFS_ATTR(tcm_loop_tpg_attrib_, fabric_prot_type); 711 712 static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = { 713 &tcm_loop_tpg_attrib_attr_fabric_prot_type, 714 NULL, 715 }; 716 717 /* Start items for tcm_loop_nexus_cit */ 718 719 static int tcm_loop_alloc_sess_cb(struct se_portal_group *se_tpg, 720 struct se_session *se_sess, void *p) 721 { 722 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 723 struct tcm_loop_tpg, tl_se_tpg); 724 725 tl_tpg->tl_nexus = p; 726 return 0; 727 } 728 729 static int tcm_loop_make_nexus( 730 struct tcm_loop_tpg *tl_tpg, 731 const char *name) 732 { 733 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 734 struct tcm_loop_nexus *tl_nexus; 735 int ret; 736 737 if (tl_tpg->tl_nexus) { 738 pr_debug("tl_tpg->tl_nexus already exists\n"); 739 return -EEXIST; 740 } 741 742 tl_nexus = kzalloc(sizeof(*tl_nexus), GFP_KERNEL); 743 if (!tl_nexus) 744 return -ENOMEM; 745 746 tl_nexus->se_sess = target_setup_session(&tl_tpg->tl_se_tpg, 0, 0, 747 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS, 748 name, tl_nexus, tcm_loop_alloc_sess_cb); 749 if (IS_ERR(tl_nexus->se_sess)) { 750 ret = PTR_ERR(tl_nexus->se_sess); 751 kfree(tl_nexus); 752 return ret; 753 } 754 755 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated %s Initiator Port: %s\n", 756 tcm_loop_dump_proto_id(tl_hba), name); 757 return 0; 758 } 759 760 static int tcm_loop_drop_nexus( 761 struct tcm_loop_tpg *tpg) 762 { 763 struct se_session *se_sess; 764 struct tcm_loop_nexus *tl_nexus; 765 766 tl_nexus = tpg->tl_nexus; 767 if (!tl_nexus) 768 return -ENODEV; 769 770 se_sess = tl_nexus->se_sess; 771 if (!se_sess) 772 return -ENODEV; 773 774 if (atomic_read(&tpg->tl_tpg_port_count)) { 775 pr_err("Unable to remove TCM_Loop I_T Nexus with active TPG port count: %d\n", 776 atomic_read(&tpg->tl_tpg_port_count)); 777 return -EPERM; 778 } 779 780 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated %s Initiator Port: %s\n", 781 tcm_loop_dump_proto_id(tpg->tl_hba), 782 tl_nexus->se_sess->se_node_acl->initiatorname); 783 /* 784 * Release the SCSI I_T Nexus to the emulated Target Port 785 */ 786 target_remove_session(se_sess); 787 tpg->tl_nexus = NULL; 788 kfree(tl_nexus); 789 return 0; 790 } 791 792 /* End items for tcm_loop_nexus_cit */ 793 794 static ssize_t tcm_loop_tpg_nexus_show(struct config_item *item, char *page) 795 { 796 struct se_portal_group *se_tpg = to_tpg(item); 797 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 798 struct tcm_loop_tpg, tl_se_tpg); 799 struct tcm_loop_nexus *tl_nexus; 800 ssize_t ret; 801 802 tl_nexus = tl_tpg->tl_nexus; 803 if (!tl_nexus) 804 return -ENODEV; 805 806 ret = snprintf(page, PAGE_SIZE, "%s\n", 807 tl_nexus->se_sess->se_node_acl->initiatorname); 808 809 return ret; 810 } 811 812 static ssize_t tcm_loop_tpg_nexus_store(struct config_item *item, 813 const char *page, size_t count) 814 { 815 struct se_portal_group *se_tpg = to_tpg(item); 816 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 817 struct tcm_loop_tpg, tl_se_tpg); 818 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 819 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr; 820 int ret; 821 /* 822 * Shutdown the active I_T nexus if 'NULL' is passed.. 823 */ 824 if (!strncmp(page, "NULL", 4)) { 825 ret = tcm_loop_drop_nexus(tl_tpg); 826 return (!ret) ? count : ret; 827 } 828 /* 829 * Otherwise make sure the passed virtual Initiator port WWN matches 830 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call 831 * tcm_loop_make_nexus() 832 */ 833 if (strlen(page) >= TL_WWN_ADDR_LEN) { 834 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n", 835 page, TL_WWN_ADDR_LEN); 836 return -EINVAL; 837 } 838 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page); 839 840 ptr = strstr(i_port, "naa."); 841 if (ptr) { 842 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) { 843 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n", 844 i_port, tcm_loop_dump_proto_id(tl_hba)); 845 return -EINVAL; 846 } 847 port_ptr = &i_port[0]; 848 goto check_newline; 849 } 850 ptr = strstr(i_port, "fc."); 851 if (ptr) { 852 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) { 853 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n", 854 i_port, tcm_loop_dump_proto_id(tl_hba)); 855 return -EINVAL; 856 } 857 port_ptr = &i_port[3]; /* Skip over "fc." */ 858 goto check_newline; 859 } 860 ptr = strstr(i_port, "iqn."); 861 if (ptr) { 862 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) { 863 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n", 864 i_port, tcm_loop_dump_proto_id(tl_hba)); 865 return -EINVAL; 866 } 867 port_ptr = &i_port[0]; 868 goto check_newline; 869 } 870 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n", 871 i_port); 872 return -EINVAL; 873 /* 874 * Clear any trailing newline for the NAA WWN 875 */ 876 check_newline: 877 if (i_port[strlen(i_port)-1] == '\n') 878 i_port[strlen(i_port)-1] = '\0'; 879 880 ret = tcm_loop_make_nexus(tl_tpg, port_ptr); 881 if (ret < 0) 882 return ret; 883 884 return count; 885 } 886 887 static ssize_t tcm_loop_tpg_transport_status_show(struct config_item *item, 888 char *page) 889 { 890 struct se_portal_group *se_tpg = to_tpg(item); 891 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 892 struct tcm_loop_tpg, tl_se_tpg); 893 const char *status = NULL; 894 ssize_t ret = -EINVAL; 895 896 switch (tl_tpg->tl_transport_status) { 897 case TCM_TRANSPORT_ONLINE: 898 status = "online"; 899 break; 900 case TCM_TRANSPORT_OFFLINE: 901 status = "offline"; 902 break; 903 default: 904 break; 905 } 906 907 if (status) 908 ret = snprintf(page, PAGE_SIZE, "%s\n", status); 909 910 return ret; 911 } 912 913 static ssize_t tcm_loop_tpg_transport_status_store(struct config_item *item, 914 const char *page, size_t count) 915 { 916 struct se_portal_group *se_tpg = to_tpg(item); 917 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 918 struct tcm_loop_tpg, tl_se_tpg); 919 920 if (!strncmp(page, "online", 6)) { 921 tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE; 922 return count; 923 } 924 if (!strncmp(page, "offline", 7)) { 925 tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE; 926 if (tl_tpg->tl_nexus) { 927 struct se_session *tl_sess = tl_tpg->tl_nexus->se_sess; 928 929 core_allocate_nexus_loss_ua(tl_sess->se_node_acl); 930 } 931 return count; 932 } 933 return -EINVAL; 934 } 935 936 static ssize_t tcm_loop_tpg_address_show(struct config_item *item, 937 char *page) 938 { 939 struct se_portal_group *se_tpg = to_tpg(item); 940 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 941 struct tcm_loop_tpg, tl_se_tpg); 942 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 943 944 return snprintf(page, PAGE_SIZE, "%d:0:%d\n", 945 tl_hba->sh->host_no, tl_tpg->tl_tpgt); 946 } 947 948 CONFIGFS_ATTR(tcm_loop_tpg_, nexus); 949 CONFIGFS_ATTR(tcm_loop_tpg_, transport_status); 950 CONFIGFS_ATTR_RO(tcm_loop_tpg_, address); 951 952 static struct configfs_attribute *tcm_loop_tpg_attrs[] = { 953 &tcm_loop_tpg_attr_nexus, 954 &tcm_loop_tpg_attr_transport_status, 955 &tcm_loop_tpg_attr_address, 956 NULL, 957 }; 958 959 /* Start items for tcm_loop_naa_cit */ 960 961 static struct se_portal_group *tcm_loop_make_naa_tpg(struct se_wwn *wwn, 962 const char *name) 963 { 964 struct tcm_loop_hba *tl_hba = container_of(wwn, 965 struct tcm_loop_hba, tl_hba_wwn); 966 struct tcm_loop_tpg *tl_tpg; 967 int ret; 968 unsigned long tpgt; 969 970 if (strstr(name, "tpgt_") != name) { 971 pr_err("Unable to locate \"tpgt_#\" directory group\n"); 972 return ERR_PTR(-EINVAL); 973 } 974 if (kstrtoul(name+5, 10, &tpgt)) 975 return ERR_PTR(-EINVAL); 976 977 if (tpgt >= TL_TPGS_PER_HBA) { 978 pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA: %u\n", 979 tpgt, TL_TPGS_PER_HBA); 980 return ERR_PTR(-EINVAL); 981 } 982 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt]; 983 tl_tpg->tl_hba = tl_hba; 984 tl_tpg->tl_tpgt = tpgt; 985 /* 986 * Register the tl_tpg as a emulated TCM Target Endpoint 987 */ 988 ret = core_tpg_register(wwn, &tl_tpg->tl_se_tpg, tl_hba->tl_proto_id); 989 if (ret < 0) 990 return ERR_PTR(-ENOMEM); 991 992 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s Target Port %s,t,0x%04lx\n", 993 tcm_loop_dump_proto_id(tl_hba), 994 config_item_name(&wwn->wwn_group.cg_item), tpgt); 995 return &tl_tpg->tl_se_tpg; 996 } 997 998 static void tcm_loop_drop_naa_tpg( 999 struct se_portal_group *se_tpg) 1000 { 1001 struct se_wwn *wwn = se_tpg->se_tpg_wwn; 1002 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 1003 struct tcm_loop_tpg, tl_se_tpg); 1004 struct tcm_loop_hba *tl_hba; 1005 unsigned short tpgt; 1006 1007 tl_hba = tl_tpg->tl_hba; 1008 tpgt = tl_tpg->tl_tpgt; 1009 /* 1010 * Release the I_T Nexus for the Virtual target link if present 1011 */ 1012 tcm_loop_drop_nexus(tl_tpg); 1013 /* 1014 * Deregister the tl_tpg as a emulated TCM Target Endpoint 1015 */ 1016 core_tpg_deregister(se_tpg); 1017 1018 tl_tpg->tl_hba = NULL; 1019 tl_tpg->tl_tpgt = 0; 1020 1021 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s Target Port %s,t,0x%04x\n", 1022 tcm_loop_dump_proto_id(tl_hba), 1023 config_item_name(&wwn->wwn_group.cg_item), tpgt); 1024 } 1025 1026 /* End items for tcm_loop_naa_cit */ 1027 1028 /* Start items for tcm_loop_cit */ 1029 1030 static struct se_wwn *tcm_loop_make_scsi_hba( 1031 struct target_fabric_configfs *tf, 1032 struct config_group *group, 1033 const char *name) 1034 { 1035 struct tcm_loop_hba *tl_hba; 1036 struct Scsi_Host *sh; 1037 char *ptr; 1038 int ret, off = 0; 1039 1040 tl_hba = kzalloc(sizeof(*tl_hba), GFP_KERNEL); 1041 if (!tl_hba) 1042 return ERR_PTR(-ENOMEM); 1043 1044 /* 1045 * Determine the emulated Protocol Identifier and Target Port Name 1046 * based on the incoming configfs directory name. 1047 */ 1048 ptr = strstr(name, "naa."); 1049 if (ptr) { 1050 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS; 1051 goto check_len; 1052 } 1053 ptr = strstr(name, "fc."); 1054 if (ptr) { 1055 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP; 1056 off = 3; /* Skip over "fc." */ 1057 goto check_len; 1058 } 1059 ptr = strstr(name, "iqn."); 1060 if (!ptr) { 1061 pr_err("Unable to locate prefix for emulated Target Port: %s\n", 1062 name); 1063 ret = -EINVAL; 1064 goto out; 1065 } 1066 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI; 1067 1068 check_len: 1069 if (strlen(name) >= TL_WWN_ADDR_LEN) { 1070 pr_err("Emulated NAA %s Address: %s, exceeds max: %d\n", 1071 name, tcm_loop_dump_proto_id(tl_hba), TL_WWN_ADDR_LEN); 1072 ret = -EINVAL; 1073 goto out; 1074 } 1075 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]); 1076 1077 /* 1078 * Call device_register(tl_hba->dev) to register the emulated 1079 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after 1080 * device_register() callbacks in tcm_loop_driver_probe() 1081 */ 1082 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt); 1083 if (ret) 1084 goto out; 1085 1086 sh = tl_hba->sh; 1087 tcm_loop_hba_no_cnt++; 1088 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n", 1089 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no); 1090 return &tl_hba->tl_hba_wwn; 1091 out: 1092 kfree(tl_hba); 1093 return ERR_PTR(ret); 1094 } 1095 1096 static void tcm_loop_drop_scsi_hba( 1097 struct se_wwn *wwn) 1098 { 1099 struct tcm_loop_hba *tl_hba = container_of(wwn, 1100 struct tcm_loop_hba, tl_hba_wwn); 1101 1102 pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n", 1103 tcm_loop_dump_proto_id(tl_hba), tl_hba->tl_wwn_address, 1104 tl_hba->sh->host_no); 1105 /* 1106 * Call device_unregister() on the original tl_hba->dev. 1107 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will 1108 * release *tl_hba; 1109 */ 1110 device_unregister(&tl_hba->dev); 1111 } 1112 1113 /* Start items for tcm_loop_cit */ 1114 static ssize_t tcm_loop_wwn_version_show(struct config_item *item, char *page) 1115 { 1116 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION); 1117 } 1118 1119 CONFIGFS_ATTR_RO(tcm_loop_wwn_, version); 1120 1121 static struct configfs_attribute *tcm_loop_wwn_attrs[] = { 1122 &tcm_loop_wwn_attr_version, 1123 NULL, 1124 }; 1125 1126 /* End items for tcm_loop_cit */ 1127 1128 static const struct target_core_fabric_ops loop_ops = { 1129 .module = THIS_MODULE, 1130 .fabric_name = "loopback", 1131 .tpg_get_wwn = tcm_loop_get_endpoint_wwn, 1132 .tpg_get_tag = tcm_loop_get_tag, 1133 .tpg_check_demo_mode = tcm_loop_check_demo_mode, 1134 .tpg_check_demo_mode_cache = tcm_loop_check_demo_mode_cache, 1135 .tpg_check_demo_mode_write_protect = 1136 tcm_loop_check_demo_mode_write_protect, 1137 .tpg_check_prod_mode_write_protect = 1138 tcm_loop_check_prod_mode_write_protect, 1139 .tpg_check_prot_fabric_only = tcm_loop_check_prot_fabric_only, 1140 .tpg_get_inst_index = tcm_loop_get_inst_index, 1141 .check_stop_free = tcm_loop_check_stop_free, 1142 .release_cmd = tcm_loop_release_cmd, 1143 .sess_get_index = tcm_loop_sess_get_index, 1144 .write_pending = tcm_loop_write_pending, 1145 .set_default_node_attributes = tcm_loop_set_default_node_attributes, 1146 .get_cmd_state = tcm_loop_get_cmd_state, 1147 .queue_data_in = tcm_loop_queue_data_in, 1148 .queue_status = tcm_loop_queue_status, 1149 .queue_tm_rsp = tcm_loop_queue_tm_rsp, 1150 .aborted_task = tcm_loop_aborted_task, 1151 .fabric_make_wwn = tcm_loop_make_scsi_hba, 1152 .fabric_drop_wwn = tcm_loop_drop_scsi_hba, 1153 .fabric_make_tpg = tcm_loop_make_naa_tpg, 1154 .fabric_drop_tpg = tcm_loop_drop_naa_tpg, 1155 .fabric_post_link = tcm_loop_port_link, 1156 .fabric_pre_unlink = tcm_loop_port_unlink, 1157 .tfc_wwn_attrs = tcm_loop_wwn_attrs, 1158 .tfc_tpg_base_attrs = tcm_loop_tpg_attrs, 1159 .tfc_tpg_attrib_attrs = tcm_loop_tpg_attrib_attrs, 1160 }; 1161 1162 static int __init tcm_loop_fabric_init(void) 1163 { 1164 int ret = -ENOMEM; 1165 1166 tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0); 1167 if (!tcm_loop_workqueue) 1168 goto out; 1169 1170 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache", 1171 sizeof(struct tcm_loop_cmd), 1172 __alignof__(struct tcm_loop_cmd), 1173 0, NULL); 1174 if (!tcm_loop_cmd_cache) { 1175 pr_debug("kmem_cache_create() for tcm_loop_cmd_cache failed\n"); 1176 goto out_destroy_workqueue; 1177 } 1178 1179 ret = tcm_loop_alloc_core_bus(); 1180 if (ret) 1181 goto out_destroy_cache; 1182 1183 ret = target_register_template(&loop_ops); 1184 if (ret) 1185 goto out_release_core_bus; 1186 1187 return 0; 1188 1189 out_release_core_bus: 1190 tcm_loop_release_core_bus(); 1191 out_destroy_cache: 1192 kmem_cache_destroy(tcm_loop_cmd_cache); 1193 out_destroy_workqueue: 1194 destroy_workqueue(tcm_loop_workqueue); 1195 out: 1196 return ret; 1197 } 1198 1199 static void __exit tcm_loop_fabric_exit(void) 1200 { 1201 target_unregister_template(&loop_ops); 1202 tcm_loop_release_core_bus(); 1203 kmem_cache_destroy(tcm_loop_cmd_cache); 1204 destroy_workqueue(tcm_loop_workqueue); 1205 } 1206 1207 MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module"); 1208 MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>"); 1209 MODULE_LICENSE("GPL"); 1210 module_init(tcm_loop_fabric_init); 1211 module_exit(tcm_loop_fabric_exit); 1212