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