1 /******************************************************************************* 2 * 3 * This file contains the Linux/SCSI LLD virtual SCSI initiator driver 4 * for emulated SAS initiator ports 5 * 6 * © Copyright 2011 RisingTide Systems LLC. 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_transport.h> 37 #include <target/target_core_fabric_ops.h> 38 #include <target/target_core_fabric_configfs.h> 39 #include <target/target_core_fabric_lib.h> 40 #include <target/target_core_configfs.h> 41 #include <target/target_core_device.h> 42 #include <target/target_core_tpg.h> 43 #include <target/target_core_tmr.h> 44 45 #include "tcm_loop.h" 46 47 #define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev) 48 49 /* Local pointer to allocated TCM configfs fabric module */ 50 static struct target_fabric_configfs *tcm_loop_fabric_configfs; 51 52 static struct kmem_cache *tcm_loop_cmd_cache; 53 54 static int tcm_loop_hba_no_cnt; 55 56 /* 57 * Allocate a tcm_loop cmd descriptor from target_core_mod code 58 * 59 * Can be called from interrupt context in tcm_loop_queuecommand() below 60 */ 61 static struct se_cmd *tcm_loop_allocate_core_cmd( 62 struct tcm_loop_hba *tl_hba, 63 struct se_portal_group *se_tpg, 64 struct scsi_cmnd *sc) 65 { 66 struct se_cmd *se_cmd; 67 struct se_session *se_sess; 68 struct tcm_loop_nexus *tl_nexus = tl_hba->tl_nexus; 69 struct tcm_loop_cmd *tl_cmd; 70 int sam_task_attr; 71 72 if (!tl_nexus) { 73 scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus" 74 " does not exist\n"); 75 set_host_byte(sc, DID_ERROR); 76 return NULL; 77 } 78 se_sess = tl_nexus->se_sess; 79 80 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC); 81 if (!tl_cmd) { 82 pr_err("Unable to allocate struct tcm_loop_cmd\n"); 83 set_host_byte(sc, DID_ERROR); 84 return NULL; 85 } 86 se_cmd = &tl_cmd->tl_se_cmd; 87 /* 88 * Save the pointer to struct scsi_cmnd *sc 89 */ 90 tl_cmd->sc = sc; 91 /* 92 * Locate the SAM Task Attr from struct scsi_cmnd * 93 */ 94 if (sc->device->tagged_supported) { 95 switch (sc->tag) { 96 case HEAD_OF_QUEUE_TAG: 97 sam_task_attr = MSG_HEAD_TAG; 98 break; 99 case ORDERED_QUEUE_TAG: 100 sam_task_attr = MSG_ORDERED_TAG; 101 break; 102 default: 103 sam_task_attr = MSG_SIMPLE_TAG; 104 break; 105 } 106 } else 107 sam_task_attr = MSG_SIMPLE_TAG; 108 109 /* 110 * Initialize struct se_cmd descriptor from target_core_mod infrastructure 111 */ 112 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 113 scsi_bufflen(sc), sc->sc_data_direction, sam_task_attr, 114 &tl_cmd->tl_sense_buf[0]); 115 116 /* 117 * Signal BIDI usage with T_TASK(cmd)->t_tasks_bidi 118 */ 119 if (scsi_bidi_cmnd(sc)) 120 se_cmd->t_tasks_bidi = 1; 121 /* 122 * Locate the struct se_lun pointer and attach it to struct se_cmd 123 */ 124 if (transport_lookup_cmd_lun(se_cmd, tl_cmd->sc->device->lun) < 0) { 125 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd); 126 set_host_byte(sc, DID_NO_CONNECT); 127 return NULL; 128 } 129 130 return se_cmd; 131 } 132 133 /* 134 * Called by struct target_core_fabric_ops->new_cmd_map() 135 * 136 * Always called in process context. A non zero return value 137 * here will signal to handle an exception based on the return code. 138 */ 139 static int tcm_loop_new_cmd_map(struct se_cmd *se_cmd) 140 { 141 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 142 struct tcm_loop_cmd, tl_se_cmd); 143 struct scsi_cmnd *sc = tl_cmd->sc; 144 struct scatterlist *sgl_bidi = NULL; 145 u32 sgl_bidi_count = 0; 146 int ret; 147 /* 148 * Allocate the necessary tasks to complete the received CDB+data 149 */ 150 ret = transport_generic_allocate_tasks(se_cmd, sc->cmnd); 151 if (ret == -ENOMEM) { 152 /* Out of Resources */ 153 return PYX_TRANSPORT_LU_COMM_FAILURE; 154 } else if (ret == -EINVAL) { 155 /* 156 * Handle case for SAM_STAT_RESERVATION_CONFLICT 157 */ 158 if (se_cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT) 159 return PYX_TRANSPORT_RESERVATION_CONFLICT; 160 /* 161 * Otherwise, return SAM_STAT_CHECK_CONDITION and return 162 * sense data. 163 */ 164 return PYX_TRANSPORT_USE_SENSE_REASON; 165 } 166 167 /* 168 * For BIDI commands, pass in the extra READ buffer 169 * to transport_generic_map_mem_to_cmd() below.. 170 */ 171 if (se_cmd->t_tasks_bidi) { 172 struct scsi_data_buffer *sdb = scsi_in(sc); 173 174 sgl_bidi = sdb->table.sgl; 175 sgl_bidi_count = sdb->table.nents; 176 } 177 178 /* Tell the core about our preallocated memory */ 179 ret = transport_generic_map_mem_to_cmd(se_cmd, scsi_sglist(sc), 180 scsi_sg_count(sc), sgl_bidi, sgl_bidi_count); 181 if (ret < 0) 182 return PYX_TRANSPORT_LU_COMM_FAILURE; 183 184 return 0; 185 } 186 187 /* 188 * Called from struct target_core_fabric_ops->check_stop_free() 189 */ 190 static void tcm_loop_check_stop_free(struct se_cmd *se_cmd) 191 { 192 /* 193 * Do not release struct se_cmd's containing a valid TMR 194 * pointer. These will be released directly in tcm_loop_device_reset() 195 * with transport_generic_free_cmd(). 196 */ 197 if (se_cmd->se_tmr_req) 198 return; 199 /* 200 * Release the struct se_cmd, which will make a callback to release 201 * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd() 202 */ 203 transport_generic_free_cmd(se_cmd, 0); 204 } 205 206 static void tcm_loop_release_cmd(struct se_cmd *se_cmd) 207 { 208 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 209 struct tcm_loop_cmd, tl_se_cmd); 210 211 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd); 212 } 213 214 static int tcm_loop_proc_info(struct Scsi_Host *host, char *buffer, 215 char **start, off_t offset, 216 int length, int inout) 217 { 218 return sprintf(buffer, "tcm_loop_proc_info()\n"); 219 } 220 221 static int tcm_loop_driver_probe(struct device *); 222 static int tcm_loop_driver_remove(struct device *); 223 224 static int pseudo_lld_bus_match(struct device *dev, 225 struct device_driver *dev_driver) 226 { 227 return 1; 228 } 229 230 static struct bus_type tcm_loop_lld_bus = { 231 .name = "tcm_loop_bus", 232 .match = pseudo_lld_bus_match, 233 .probe = tcm_loop_driver_probe, 234 .remove = tcm_loop_driver_remove, 235 }; 236 237 static struct device_driver tcm_loop_driverfs = { 238 .name = "tcm_loop", 239 .bus = &tcm_loop_lld_bus, 240 }; 241 /* 242 * Used with root_device_register() in tcm_loop_alloc_core_bus() below 243 */ 244 struct device *tcm_loop_primary; 245 246 /* 247 * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and 248 * drivers/scsi/libiscsi.c:iscsi_change_queue_depth() 249 */ 250 static int tcm_loop_change_queue_depth( 251 struct scsi_device *sdev, 252 int depth, 253 int reason) 254 { 255 switch (reason) { 256 case SCSI_QDEPTH_DEFAULT: 257 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); 258 break; 259 case SCSI_QDEPTH_QFULL: 260 scsi_track_queue_full(sdev, depth); 261 break; 262 case SCSI_QDEPTH_RAMP_UP: 263 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); 264 break; 265 default: 266 return -EOPNOTSUPP; 267 } 268 return sdev->queue_depth; 269 } 270 271 /* 272 * Main entry point from struct scsi_host_template for incoming SCSI CDB+Data 273 * from Linux/SCSI subsystem for SCSI low level device drivers (LLDs) 274 */ 275 static int tcm_loop_queuecommand( 276 struct Scsi_Host *sh, 277 struct scsi_cmnd *sc) 278 { 279 struct se_cmd *se_cmd; 280 struct se_portal_group *se_tpg; 281 struct tcm_loop_hba *tl_hba; 282 struct tcm_loop_tpg *tl_tpg; 283 284 pr_debug("tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x" 285 " scsi_buf_len: %u\n", sc->device->host->host_no, 286 sc->device->id, sc->device->channel, sc->device->lun, 287 sc->cmnd[0], scsi_bufflen(sc)); 288 /* 289 * Locate the tcm_loop_hba_t pointer 290 */ 291 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 292 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 293 /* 294 * Ensure that this tl_tpg reference from the incoming sc->device->id 295 * has already been configured via tcm_loop_make_naa_tpg(). 296 */ 297 if (!tl_tpg->tl_hba) { 298 set_host_byte(sc, DID_NO_CONNECT); 299 sc->scsi_done(sc); 300 return 0; 301 } 302 se_tpg = &tl_tpg->tl_se_tpg; 303 /* 304 * Determine the SAM Task Attribute and allocate tl_cmd and 305 * tl_cmd->tl_se_cmd from TCM infrastructure 306 */ 307 se_cmd = tcm_loop_allocate_core_cmd(tl_hba, se_tpg, sc); 308 if (!se_cmd) { 309 sc->scsi_done(sc); 310 return 0; 311 } 312 /* 313 * Queue up the newly allocated to be processed in TCM thread context. 314 */ 315 transport_generic_handle_cdb_map(se_cmd); 316 return 0; 317 } 318 319 /* 320 * Called from SCSI EH process context to issue a LUN_RESET TMR 321 * to struct scsi_device 322 */ 323 static int tcm_loop_device_reset(struct scsi_cmnd *sc) 324 { 325 struct se_cmd *se_cmd = NULL; 326 struct se_portal_group *se_tpg; 327 struct se_session *se_sess; 328 struct tcm_loop_cmd *tl_cmd = NULL; 329 struct tcm_loop_hba *tl_hba; 330 struct tcm_loop_nexus *tl_nexus; 331 struct tcm_loop_tmr *tl_tmr = NULL; 332 struct tcm_loop_tpg *tl_tpg; 333 int ret = FAILED; 334 /* 335 * Locate the tcm_loop_hba_t pointer 336 */ 337 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 338 /* 339 * Locate the tl_nexus and se_sess pointers 340 */ 341 tl_nexus = tl_hba->tl_nexus; 342 if (!tl_nexus) { 343 pr_err("Unable to perform device reset without" 344 " active I_T Nexus\n"); 345 return FAILED; 346 } 347 se_sess = tl_nexus->se_sess; 348 /* 349 * Locate the tl_tpg and se_tpg pointers from TargetID in sc->device->id 350 */ 351 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 352 se_tpg = &tl_tpg->tl_se_tpg; 353 354 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL); 355 if (!tl_cmd) { 356 pr_err("Unable to allocate memory for tl_cmd\n"); 357 return FAILED; 358 } 359 360 tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL); 361 if (!tl_tmr) { 362 pr_err("Unable to allocate memory for tl_tmr\n"); 363 goto release; 364 } 365 init_waitqueue_head(&tl_tmr->tl_tmr_wait); 366 367 se_cmd = &tl_cmd->tl_se_cmd; 368 /* 369 * Initialize struct se_cmd descriptor from target_core_mod infrastructure 370 */ 371 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0, 372 DMA_NONE, MSG_SIMPLE_TAG, 373 &tl_cmd->tl_sense_buf[0]); 374 /* 375 * Allocate the LUN_RESET TMR 376 */ 377 se_cmd->se_tmr_req = core_tmr_alloc_req(se_cmd, tl_tmr, 378 TMR_LUN_RESET, GFP_KERNEL); 379 if (IS_ERR(se_cmd->se_tmr_req)) 380 goto release; 381 /* 382 * Locate the underlying TCM struct se_lun from sc->device->lun 383 */ 384 if (transport_lookup_tmr_lun(se_cmd, sc->device->lun) < 0) 385 goto release; 386 /* 387 * Queue the TMR to TCM Core and sleep waiting for tcm_loop_queue_tm_rsp() 388 * to wake us up. 389 */ 390 transport_generic_handle_tmr(se_cmd); 391 wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete)); 392 /* 393 * The TMR LUN_RESET has completed, check the response status and 394 * then release allocations. 395 */ 396 ret = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ? 397 SUCCESS : FAILED; 398 release: 399 if (se_cmd) 400 transport_generic_free_cmd(se_cmd, 1); 401 else 402 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd); 403 kfree(tl_tmr); 404 return ret; 405 } 406 407 static int tcm_loop_slave_alloc(struct scsi_device *sd) 408 { 409 set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags); 410 return 0; 411 } 412 413 static int tcm_loop_slave_configure(struct scsi_device *sd) 414 { 415 return 0; 416 } 417 418 static struct scsi_host_template tcm_loop_driver_template = { 419 .proc_info = tcm_loop_proc_info, 420 .proc_name = "tcm_loopback", 421 .name = "TCM_Loopback", 422 .queuecommand = tcm_loop_queuecommand, 423 .change_queue_depth = tcm_loop_change_queue_depth, 424 .eh_device_reset_handler = tcm_loop_device_reset, 425 .can_queue = TL_SCSI_CAN_QUEUE, 426 .this_id = -1, 427 .sg_tablesize = TL_SCSI_SG_TABLESIZE, 428 .cmd_per_lun = TL_SCSI_CMD_PER_LUN, 429 .max_sectors = TL_SCSI_MAX_SECTORS, 430 .use_clustering = DISABLE_CLUSTERING, 431 .slave_alloc = tcm_loop_slave_alloc, 432 .slave_configure = tcm_loop_slave_configure, 433 .module = THIS_MODULE, 434 }; 435 436 static int tcm_loop_driver_probe(struct device *dev) 437 { 438 struct tcm_loop_hba *tl_hba; 439 struct Scsi_Host *sh; 440 int error; 441 442 tl_hba = to_tcm_loop_hba(dev); 443 444 sh = scsi_host_alloc(&tcm_loop_driver_template, 445 sizeof(struct tcm_loop_hba)); 446 if (!sh) { 447 pr_err("Unable to allocate struct scsi_host\n"); 448 return -ENODEV; 449 } 450 tl_hba->sh = sh; 451 452 /* 453 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata 454 */ 455 *((struct tcm_loop_hba **)sh->hostdata) = tl_hba; 456 /* 457 * Setup single ID, Channel and LUN for now.. 458 */ 459 sh->max_id = 2; 460 sh->max_lun = 0; 461 sh->max_channel = 0; 462 sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN; 463 464 error = scsi_add_host(sh, &tl_hba->dev); 465 if (error) { 466 pr_err("%s: scsi_add_host failed\n", __func__); 467 scsi_host_put(sh); 468 return -ENODEV; 469 } 470 return 0; 471 } 472 473 static int tcm_loop_driver_remove(struct device *dev) 474 { 475 struct tcm_loop_hba *tl_hba; 476 struct Scsi_Host *sh; 477 478 tl_hba = to_tcm_loop_hba(dev); 479 sh = tl_hba->sh; 480 481 scsi_remove_host(sh); 482 scsi_host_put(sh); 483 return 0; 484 } 485 486 static void tcm_loop_release_adapter(struct device *dev) 487 { 488 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev); 489 490 kfree(tl_hba); 491 } 492 493 /* 494 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c 495 */ 496 static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id) 497 { 498 int ret; 499 500 tl_hba->dev.bus = &tcm_loop_lld_bus; 501 tl_hba->dev.parent = tcm_loop_primary; 502 tl_hba->dev.release = &tcm_loop_release_adapter; 503 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id); 504 505 ret = device_register(&tl_hba->dev); 506 if (ret) { 507 pr_err("device_register() failed for" 508 " tl_hba->dev: %d\n", ret); 509 return -ENODEV; 510 } 511 512 return 0; 513 } 514 515 /* 516 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated 517 * tcm_loop SCSI bus. 518 */ 519 static int tcm_loop_alloc_core_bus(void) 520 { 521 int ret; 522 523 tcm_loop_primary = root_device_register("tcm_loop_0"); 524 if (IS_ERR(tcm_loop_primary)) { 525 pr_err("Unable to allocate tcm_loop_primary\n"); 526 return PTR_ERR(tcm_loop_primary); 527 } 528 529 ret = bus_register(&tcm_loop_lld_bus); 530 if (ret) { 531 pr_err("bus_register() failed for tcm_loop_lld_bus\n"); 532 goto dev_unreg; 533 } 534 535 ret = driver_register(&tcm_loop_driverfs); 536 if (ret) { 537 pr_err("driver_register() failed for" 538 "tcm_loop_driverfs\n"); 539 goto bus_unreg; 540 } 541 542 pr_debug("Initialized TCM Loop Core Bus\n"); 543 return ret; 544 545 bus_unreg: 546 bus_unregister(&tcm_loop_lld_bus); 547 dev_unreg: 548 root_device_unregister(tcm_loop_primary); 549 return ret; 550 } 551 552 static void tcm_loop_release_core_bus(void) 553 { 554 driver_unregister(&tcm_loop_driverfs); 555 bus_unregister(&tcm_loop_lld_bus); 556 root_device_unregister(tcm_loop_primary); 557 558 pr_debug("Releasing TCM Loop Core BUS\n"); 559 } 560 561 static char *tcm_loop_get_fabric_name(void) 562 { 563 return "loopback"; 564 } 565 566 static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg) 567 { 568 struct tcm_loop_tpg *tl_tpg = 569 (struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr; 570 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 571 /* 572 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba() 573 * time based on the protocol dependent prefix of the passed configfs group. 574 * 575 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric 576 * ProtocolID using target_core_fabric_lib.c symbols. 577 */ 578 switch (tl_hba->tl_proto_id) { 579 case SCSI_PROTOCOL_SAS: 580 return sas_get_fabric_proto_ident(se_tpg); 581 case SCSI_PROTOCOL_FCP: 582 return fc_get_fabric_proto_ident(se_tpg); 583 case SCSI_PROTOCOL_ISCSI: 584 return iscsi_get_fabric_proto_ident(se_tpg); 585 default: 586 pr_err("Unknown tl_proto_id: 0x%02x, using" 587 " SAS emulation\n", tl_hba->tl_proto_id); 588 break; 589 } 590 591 return sas_get_fabric_proto_ident(se_tpg); 592 } 593 594 static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg) 595 { 596 struct tcm_loop_tpg *tl_tpg = 597 (struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr; 598 /* 599 * Return the passed NAA identifier for the SAS Target Port 600 */ 601 return &tl_tpg->tl_hba->tl_wwn_address[0]; 602 } 603 604 static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg) 605 { 606 struct tcm_loop_tpg *tl_tpg = 607 (struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr; 608 /* 609 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83 610 * to represent the SCSI Target Port. 611 */ 612 return tl_tpg->tl_tpgt; 613 } 614 615 static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg) 616 { 617 return 1; 618 } 619 620 static u32 tcm_loop_get_pr_transport_id( 621 struct se_portal_group *se_tpg, 622 struct se_node_acl *se_nacl, 623 struct t10_pr_registration *pr_reg, 624 int *format_code, 625 unsigned char *buf) 626 { 627 struct tcm_loop_tpg *tl_tpg = 628 (struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr; 629 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 630 631 switch (tl_hba->tl_proto_id) { 632 case SCSI_PROTOCOL_SAS: 633 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, 634 format_code, buf); 635 case SCSI_PROTOCOL_FCP: 636 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg, 637 format_code, buf); 638 case SCSI_PROTOCOL_ISCSI: 639 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg, 640 format_code, buf); 641 default: 642 pr_err("Unknown tl_proto_id: 0x%02x, using" 643 " SAS emulation\n", tl_hba->tl_proto_id); 644 break; 645 } 646 647 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, 648 format_code, buf); 649 } 650 651 static u32 tcm_loop_get_pr_transport_id_len( 652 struct se_portal_group *se_tpg, 653 struct se_node_acl *se_nacl, 654 struct t10_pr_registration *pr_reg, 655 int *format_code) 656 { 657 struct tcm_loop_tpg *tl_tpg = 658 (struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr; 659 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 660 661 switch (tl_hba->tl_proto_id) { 662 case SCSI_PROTOCOL_SAS: 663 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, 664 format_code); 665 case SCSI_PROTOCOL_FCP: 666 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, 667 format_code); 668 case SCSI_PROTOCOL_ISCSI: 669 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, 670 format_code); 671 default: 672 pr_err("Unknown tl_proto_id: 0x%02x, using" 673 " SAS emulation\n", tl_hba->tl_proto_id); 674 break; 675 } 676 677 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, 678 format_code); 679 } 680 681 /* 682 * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above 683 * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations. 684 */ 685 static char *tcm_loop_parse_pr_out_transport_id( 686 struct se_portal_group *se_tpg, 687 const char *buf, 688 u32 *out_tid_len, 689 char **port_nexus_ptr) 690 { 691 struct tcm_loop_tpg *tl_tpg = 692 (struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr; 693 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 694 695 switch (tl_hba->tl_proto_id) { 696 case SCSI_PROTOCOL_SAS: 697 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, 698 port_nexus_ptr); 699 case SCSI_PROTOCOL_FCP: 700 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, 701 port_nexus_ptr); 702 case SCSI_PROTOCOL_ISCSI: 703 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, 704 port_nexus_ptr); 705 default: 706 pr_err("Unknown tl_proto_id: 0x%02x, using" 707 " SAS emulation\n", tl_hba->tl_proto_id); 708 break; 709 } 710 711 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, 712 port_nexus_ptr); 713 } 714 715 /* 716 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated 717 * based upon the incoming fabric dependent SCSI Initiator Port 718 */ 719 static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg) 720 { 721 return 1; 722 } 723 724 static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg) 725 { 726 return 0; 727 } 728 729 /* 730 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for 731 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest 732 */ 733 static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg) 734 { 735 return 0; 736 } 737 738 /* 739 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will 740 * never be called for TCM_Loop by target_core_fabric_configfs.c code. 741 * It has been added here as a nop for target_fabric_tf_ops_check() 742 */ 743 static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg) 744 { 745 return 0; 746 } 747 748 static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl( 749 struct se_portal_group *se_tpg) 750 { 751 struct tcm_loop_nacl *tl_nacl; 752 753 tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL); 754 if (!tl_nacl) { 755 pr_err("Unable to allocate struct tcm_loop_nacl\n"); 756 return NULL; 757 } 758 759 return &tl_nacl->se_node_acl; 760 } 761 762 static void tcm_loop_tpg_release_fabric_acl( 763 struct se_portal_group *se_tpg, 764 struct se_node_acl *se_nacl) 765 { 766 struct tcm_loop_nacl *tl_nacl = container_of(se_nacl, 767 struct tcm_loop_nacl, se_node_acl); 768 769 kfree(tl_nacl); 770 } 771 772 static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg) 773 { 774 return 1; 775 } 776 777 static int tcm_loop_is_state_remove(struct se_cmd *se_cmd) 778 { 779 /* 780 * Assume struct scsi_cmnd is not in remove state.. 781 */ 782 return 0; 783 } 784 785 static int tcm_loop_sess_logged_in(struct se_session *se_sess) 786 { 787 /* 788 * Assume that TL Nexus is always active 789 */ 790 return 1; 791 } 792 793 static u32 tcm_loop_sess_get_index(struct se_session *se_sess) 794 { 795 return 1; 796 } 797 798 static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl) 799 { 800 return; 801 } 802 803 static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd) 804 { 805 return 1; 806 } 807 808 static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd) 809 { 810 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 811 struct tcm_loop_cmd, tl_se_cmd); 812 813 return tl_cmd->sc_cmd_state; 814 } 815 816 static int tcm_loop_shutdown_session(struct se_session *se_sess) 817 { 818 return 0; 819 } 820 821 static void tcm_loop_close_session(struct se_session *se_sess) 822 { 823 return; 824 }; 825 826 static void tcm_loop_stop_session( 827 struct se_session *se_sess, 828 int sess_sleep, 829 int conn_sleep) 830 { 831 return; 832 } 833 834 static void tcm_loop_fall_back_to_erl0(struct se_session *se_sess) 835 { 836 return; 837 } 838 839 static int tcm_loop_write_pending(struct se_cmd *se_cmd) 840 { 841 /* 842 * Since Linux/SCSI has already sent down a struct scsi_cmnd 843 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array 844 * memory, and memory has already been mapped to struct se_cmd->t_mem_list 845 * format with transport_generic_map_mem_to_cmd(). 846 * 847 * We now tell TCM to add this WRITE CDB directly into the TCM storage 848 * object execution queue. 849 */ 850 transport_generic_process_write(se_cmd); 851 return 0; 852 } 853 854 static int tcm_loop_write_pending_status(struct se_cmd *se_cmd) 855 { 856 return 0; 857 } 858 859 static int tcm_loop_queue_data_in(struct se_cmd *se_cmd) 860 { 861 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 862 struct tcm_loop_cmd, tl_se_cmd); 863 struct scsi_cmnd *sc = tl_cmd->sc; 864 865 pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p" 866 " cdb: 0x%02x\n", sc, sc->cmnd[0]); 867 868 sc->result = SAM_STAT_GOOD; 869 set_host_byte(sc, DID_OK); 870 sc->scsi_done(sc); 871 return 0; 872 } 873 874 static int tcm_loop_queue_status(struct se_cmd *se_cmd) 875 { 876 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 877 struct tcm_loop_cmd, tl_se_cmd); 878 struct scsi_cmnd *sc = tl_cmd->sc; 879 880 pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p" 881 " cdb: 0x%02x\n", sc, sc->cmnd[0]); 882 883 if (se_cmd->sense_buffer && 884 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || 885 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) { 886 887 memcpy(sc->sense_buffer, se_cmd->sense_buffer, 888 SCSI_SENSE_BUFFERSIZE); 889 sc->result = SAM_STAT_CHECK_CONDITION; 890 set_driver_byte(sc, DRIVER_SENSE); 891 } else 892 sc->result = se_cmd->scsi_status; 893 894 set_host_byte(sc, DID_OK); 895 sc->scsi_done(sc); 896 return 0; 897 } 898 899 static int tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd) 900 { 901 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req; 902 struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr; 903 /* 904 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead 905 * and wake up the wait_queue_head_t in tcm_loop_device_reset() 906 */ 907 atomic_set(&tl_tmr->tmr_complete, 1); 908 wake_up(&tl_tmr->tl_tmr_wait); 909 return 0; 910 } 911 912 static u16 tcm_loop_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length) 913 { 914 return 0; 915 } 916 917 static u16 tcm_loop_get_fabric_sense_len(void) 918 { 919 return 0; 920 } 921 922 static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba) 923 { 924 switch (tl_hba->tl_proto_id) { 925 case SCSI_PROTOCOL_SAS: 926 return "SAS"; 927 case SCSI_PROTOCOL_FCP: 928 return "FCP"; 929 case SCSI_PROTOCOL_ISCSI: 930 return "iSCSI"; 931 default: 932 break; 933 } 934 935 return "Unknown"; 936 } 937 938 /* Start items for tcm_loop_port_cit */ 939 940 static int tcm_loop_port_link( 941 struct se_portal_group *se_tpg, 942 struct se_lun *lun) 943 { 944 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 945 struct tcm_loop_tpg, tl_se_tpg); 946 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 947 948 atomic_inc(&tl_tpg->tl_tpg_port_count); 949 smp_mb__after_atomic_inc(); 950 /* 951 * Add Linux/SCSI struct scsi_device by HCTL 952 */ 953 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun); 954 955 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n"); 956 return 0; 957 } 958 959 static void tcm_loop_port_unlink( 960 struct se_portal_group *se_tpg, 961 struct se_lun *se_lun) 962 { 963 struct scsi_device *sd; 964 struct tcm_loop_hba *tl_hba; 965 struct tcm_loop_tpg *tl_tpg; 966 967 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg); 968 tl_hba = tl_tpg->tl_hba; 969 970 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt, 971 se_lun->unpacked_lun); 972 if (!sd) { 973 pr_err("Unable to locate struct scsi_device for %d:%d:" 974 "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun); 975 return; 976 } 977 /* 978 * Remove Linux/SCSI struct scsi_device by HCTL 979 */ 980 scsi_remove_device(sd); 981 scsi_device_put(sd); 982 983 atomic_dec(&tl_tpg->tl_tpg_port_count); 984 smp_mb__after_atomic_dec(); 985 986 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n"); 987 } 988 989 /* End items for tcm_loop_port_cit */ 990 991 /* Start items for tcm_loop_nexus_cit */ 992 993 static int tcm_loop_make_nexus( 994 struct tcm_loop_tpg *tl_tpg, 995 const char *name) 996 { 997 struct se_portal_group *se_tpg; 998 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 999 struct tcm_loop_nexus *tl_nexus; 1000 int ret = -ENOMEM; 1001 1002 if (tl_tpg->tl_hba->tl_nexus) { 1003 pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n"); 1004 return -EEXIST; 1005 } 1006 se_tpg = &tl_tpg->tl_se_tpg; 1007 1008 tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL); 1009 if (!tl_nexus) { 1010 pr_err("Unable to allocate struct tcm_loop_nexus\n"); 1011 return -ENOMEM; 1012 } 1013 /* 1014 * Initialize the struct se_session pointer 1015 */ 1016 tl_nexus->se_sess = transport_init_session(); 1017 if (IS_ERR(tl_nexus->se_sess)) { 1018 ret = PTR_ERR(tl_nexus->se_sess); 1019 goto out; 1020 } 1021 /* 1022 * Since we are running in 'demo mode' this call with generate a 1023 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI 1024 * Initiator port name of the passed configfs group 'name'. 1025 */ 1026 tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl( 1027 se_tpg, (unsigned char *)name); 1028 if (!tl_nexus->se_sess->se_node_acl) { 1029 transport_free_session(tl_nexus->se_sess); 1030 goto out; 1031 } 1032 /* 1033 * Now, register the SAS I_T Nexus as active with the call to 1034 * transport_register_session() 1035 */ 1036 __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl, 1037 tl_nexus->se_sess, tl_nexus); 1038 tl_tpg->tl_hba->tl_nexus = tl_nexus; 1039 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated" 1040 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba), 1041 name); 1042 return 0; 1043 1044 out: 1045 kfree(tl_nexus); 1046 return ret; 1047 } 1048 1049 static int tcm_loop_drop_nexus( 1050 struct tcm_loop_tpg *tpg) 1051 { 1052 struct se_session *se_sess; 1053 struct tcm_loop_nexus *tl_nexus; 1054 struct tcm_loop_hba *tl_hba = tpg->tl_hba; 1055 1056 tl_nexus = tpg->tl_hba->tl_nexus; 1057 if (!tl_nexus) 1058 return -ENODEV; 1059 1060 se_sess = tl_nexus->se_sess; 1061 if (!se_sess) 1062 return -ENODEV; 1063 1064 if (atomic_read(&tpg->tl_tpg_port_count)) { 1065 pr_err("Unable to remove TCM_Loop I_T Nexus with" 1066 " active TPG port count: %d\n", 1067 atomic_read(&tpg->tl_tpg_port_count)); 1068 return -EPERM; 1069 } 1070 1071 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated" 1072 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba), 1073 tl_nexus->se_sess->se_node_acl->initiatorname); 1074 /* 1075 * Release the SCSI I_T Nexus to the emulated SAS Target Port 1076 */ 1077 transport_deregister_session(tl_nexus->se_sess); 1078 tpg->tl_hba->tl_nexus = NULL; 1079 kfree(tl_nexus); 1080 return 0; 1081 } 1082 1083 /* End items for tcm_loop_nexus_cit */ 1084 1085 static ssize_t tcm_loop_tpg_show_nexus( 1086 struct se_portal_group *se_tpg, 1087 char *page) 1088 { 1089 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 1090 struct tcm_loop_tpg, tl_se_tpg); 1091 struct tcm_loop_nexus *tl_nexus; 1092 ssize_t ret; 1093 1094 tl_nexus = tl_tpg->tl_hba->tl_nexus; 1095 if (!tl_nexus) 1096 return -ENODEV; 1097 1098 ret = snprintf(page, PAGE_SIZE, "%s\n", 1099 tl_nexus->se_sess->se_node_acl->initiatorname); 1100 1101 return ret; 1102 } 1103 1104 static ssize_t tcm_loop_tpg_store_nexus( 1105 struct se_portal_group *se_tpg, 1106 const char *page, 1107 size_t count) 1108 { 1109 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 1110 struct tcm_loop_tpg, tl_se_tpg); 1111 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 1112 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr; 1113 int ret; 1114 /* 1115 * Shutdown the active I_T nexus if 'NULL' is passed.. 1116 */ 1117 if (!strncmp(page, "NULL", 4)) { 1118 ret = tcm_loop_drop_nexus(tl_tpg); 1119 return (!ret) ? count : ret; 1120 } 1121 /* 1122 * Otherwise make sure the passed virtual Initiator port WWN matches 1123 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call 1124 * tcm_loop_make_nexus() 1125 */ 1126 if (strlen(page) >= TL_WWN_ADDR_LEN) { 1127 pr_err("Emulated NAA Sas Address: %s, exceeds" 1128 " max: %d\n", page, TL_WWN_ADDR_LEN); 1129 return -EINVAL; 1130 } 1131 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page); 1132 1133 ptr = strstr(i_port, "naa."); 1134 if (ptr) { 1135 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) { 1136 pr_err("Passed SAS Initiator Port %s does not" 1137 " match target port protoid: %s\n", i_port, 1138 tcm_loop_dump_proto_id(tl_hba)); 1139 return -EINVAL; 1140 } 1141 port_ptr = &i_port[0]; 1142 goto check_newline; 1143 } 1144 ptr = strstr(i_port, "fc."); 1145 if (ptr) { 1146 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) { 1147 pr_err("Passed FCP Initiator Port %s does not" 1148 " match target port protoid: %s\n", i_port, 1149 tcm_loop_dump_proto_id(tl_hba)); 1150 return -EINVAL; 1151 } 1152 port_ptr = &i_port[3]; /* Skip over "fc." */ 1153 goto check_newline; 1154 } 1155 ptr = strstr(i_port, "iqn."); 1156 if (ptr) { 1157 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) { 1158 pr_err("Passed iSCSI Initiator Port %s does not" 1159 " match target port protoid: %s\n", i_port, 1160 tcm_loop_dump_proto_id(tl_hba)); 1161 return -EINVAL; 1162 } 1163 port_ptr = &i_port[0]; 1164 goto check_newline; 1165 } 1166 pr_err("Unable to locate prefix for emulated Initiator Port:" 1167 " %s\n", i_port); 1168 return -EINVAL; 1169 /* 1170 * Clear any trailing newline for the NAA WWN 1171 */ 1172 check_newline: 1173 if (i_port[strlen(i_port)-1] == '\n') 1174 i_port[strlen(i_port)-1] = '\0'; 1175 1176 ret = tcm_loop_make_nexus(tl_tpg, port_ptr); 1177 if (ret < 0) 1178 return ret; 1179 1180 return count; 1181 } 1182 1183 TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR); 1184 1185 static struct configfs_attribute *tcm_loop_tpg_attrs[] = { 1186 &tcm_loop_tpg_nexus.attr, 1187 NULL, 1188 }; 1189 1190 /* Start items for tcm_loop_naa_cit */ 1191 1192 struct se_portal_group *tcm_loop_make_naa_tpg( 1193 struct se_wwn *wwn, 1194 struct config_group *group, 1195 const char *name) 1196 { 1197 struct tcm_loop_hba *tl_hba = container_of(wwn, 1198 struct tcm_loop_hba, tl_hba_wwn); 1199 struct tcm_loop_tpg *tl_tpg; 1200 char *tpgt_str, *end_ptr; 1201 int ret; 1202 unsigned short int tpgt; 1203 1204 tpgt_str = strstr(name, "tpgt_"); 1205 if (!tpgt_str) { 1206 pr_err("Unable to locate \"tpgt_#\" directory" 1207 " group\n"); 1208 return ERR_PTR(-EINVAL); 1209 } 1210 tpgt_str += 5; /* Skip ahead of "tpgt_" */ 1211 tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0); 1212 1213 if (tpgt >= TL_TPGS_PER_HBA) { 1214 pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:" 1215 " %u\n", tpgt, TL_TPGS_PER_HBA); 1216 return ERR_PTR(-EINVAL); 1217 } 1218 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt]; 1219 tl_tpg->tl_hba = tl_hba; 1220 tl_tpg->tl_tpgt = tpgt; 1221 /* 1222 * Register the tl_tpg as a emulated SAS TCM Target Endpoint 1223 */ 1224 ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops, 1225 wwn, &tl_tpg->tl_se_tpg, tl_tpg, 1226 TRANSPORT_TPG_TYPE_NORMAL); 1227 if (ret < 0) 1228 return ERR_PTR(-ENOMEM); 1229 1230 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s" 1231 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba), 1232 config_item_name(&wwn->wwn_group.cg_item), tpgt); 1233 1234 return &tl_tpg->tl_se_tpg; 1235 } 1236 1237 void tcm_loop_drop_naa_tpg( 1238 struct se_portal_group *se_tpg) 1239 { 1240 struct se_wwn *wwn = se_tpg->se_tpg_wwn; 1241 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 1242 struct tcm_loop_tpg, tl_se_tpg); 1243 struct tcm_loop_hba *tl_hba; 1244 unsigned short tpgt; 1245 1246 tl_hba = tl_tpg->tl_hba; 1247 tpgt = tl_tpg->tl_tpgt; 1248 /* 1249 * Release the I_T Nexus for the Virtual SAS link if present 1250 */ 1251 tcm_loop_drop_nexus(tl_tpg); 1252 /* 1253 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint 1254 */ 1255 core_tpg_deregister(se_tpg); 1256 1257 tl_tpg->tl_hba = NULL; 1258 tl_tpg->tl_tpgt = 0; 1259 1260 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s" 1261 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba), 1262 config_item_name(&wwn->wwn_group.cg_item), tpgt); 1263 } 1264 1265 /* End items for tcm_loop_naa_cit */ 1266 1267 /* Start items for tcm_loop_cit */ 1268 1269 struct se_wwn *tcm_loop_make_scsi_hba( 1270 struct target_fabric_configfs *tf, 1271 struct config_group *group, 1272 const char *name) 1273 { 1274 struct tcm_loop_hba *tl_hba; 1275 struct Scsi_Host *sh; 1276 char *ptr; 1277 int ret, off = 0; 1278 1279 tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL); 1280 if (!tl_hba) { 1281 pr_err("Unable to allocate struct tcm_loop_hba\n"); 1282 return ERR_PTR(-ENOMEM); 1283 } 1284 /* 1285 * Determine the emulated Protocol Identifier and Target Port Name 1286 * based on the incoming configfs directory name. 1287 */ 1288 ptr = strstr(name, "naa."); 1289 if (ptr) { 1290 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS; 1291 goto check_len; 1292 } 1293 ptr = strstr(name, "fc."); 1294 if (ptr) { 1295 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP; 1296 off = 3; /* Skip over "fc." */ 1297 goto check_len; 1298 } 1299 ptr = strstr(name, "iqn."); 1300 if (!ptr) { 1301 pr_err("Unable to locate prefix for emulated Target " 1302 "Port: %s\n", name); 1303 ret = -EINVAL; 1304 goto out; 1305 } 1306 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI; 1307 1308 check_len: 1309 if (strlen(name) >= TL_WWN_ADDR_LEN) { 1310 pr_err("Emulated NAA %s Address: %s, exceeds" 1311 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba), 1312 TL_WWN_ADDR_LEN); 1313 ret = -EINVAL; 1314 goto out; 1315 } 1316 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]); 1317 1318 /* 1319 * Call device_register(tl_hba->dev) to register the emulated 1320 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after 1321 * device_register() callbacks in tcm_loop_driver_probe() 1322 */ 1323 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt); 1324 if (ret) 1325 goto out; 1326 1327 sh = tl_hba->sh; 1328 tcm_loop_hba_no_cnt++; 1329 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target" 1330 " %s Address: %s at Linux/SCSI Host ID: %d\n", 1331 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no); 1332 1333 return &tl_hba->tl_hba_wwn; 1334 out: 1335 kfree(tl_hba); 1336 return ERR_PTR(ret); 1337 } 1338 1339 void tcm_loop_drop_scsi_hba( 1340 struct se_wwn *wwn) 1341 { 1342 struct tcm_loop_hba *tl_hba = container_of(wwn, 1343 struct tcm_loop_hba, tl_hba_wwn); 1344 int host_no = tl_hba->sh->host_no; 1345 /* 1346 * Call device_unregister() on the original tl_hba->dev. 1347 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will 1348 * release *tl_hba; 1349 */ 1350 device_unregister(&tl_hba->dev); 1351 1352 pr_debug("TCM_Loop_ConfigFS: Deallocated emulated Target" 1353 " SAS Address: %s at Linux/SCSI Host ID: %d\n", 1354 config_item_name(&wwn->wwn_group.cg_item), host_no); 1355 } 1356 1357 /* Start items for tcm_loop_cit */ 1358 static ssize_t tcm_loop_wwn_show_attr_version( 1359 struct target_fabric_configfs *tf, 1360 char *page) 1361 { 1362 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION); 1363 } 1364 1365 TF_WWN_ATTR_RO(tcm_loop, version); 1366 1367 static struct configfs_attribute *tcm_loop_wwn_attrs[] = { 1368 &tcm_loop_wwn_version.attr, 1369 NULL, 1370 }; 1371 1372 /* End items for tcm_loop_cit */ 1373 1374 static int tcm_loop_register_configfs(void) 1375 { 1376 struct target_fabric_configfs *fabric; 1377 struct config_group *tf_cg; 1378 int ret; 1379 /* 1380 * Set the TCM Loop HBA counter to zero 1381 */ 1382 tcm_loop_hba_no_cnt = 0; 1383 /* 1384 * Register the top level struct config_item_type with TCM core 1385 */ 1386 fabric = target_fabric_configfs_init(THIS_MODULE, "loopback"); 1387 if (IS_ERR(fabric)) { 1388 pr_err("tcm_loop_register_configfs() failed!\n"); 1389 return PTR_ERR(fabric); 1390 } 1391 /* 1392 * Setup the fabric API of function pointers used by target_core_mod 1393 */ 1394 fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name; 1395 fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident; 1396 fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn; 1397 fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag; 1398 fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth; 1399 fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id; 1400 fabric->tf_ops.tpg_get_pr_transport_id_len = 1401 &tcm_loop_get_pr_transport_id_len; 1402 fabric->tf_ops.tpg_parse_pr_out_transport_id = 1403 &tcm_loop_parse_pr_out_transport_id; 1404 fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode; 1405 fabric->tf_ops.tpg_check_demo_mode_cache = 1406 &tcm_loop_check_demo_mode_cache; 1407 fabric->tf_ops.tpg_check_demo_mode_write_protect = 1408 &tcm_loop_check_demo_mode_write_protect; 1409 fabric->tf_ops.tpg_check_prod_mode_write_protect = 1410 &tcm_loop_check_prod_mode_write_protect; 1411 /* 1412 * The TCM loopback fabric module runs in demo-mode to a local 1413 * virtual SCSI device, so fabric dependent initator ACLs are 1414 * not required. 1415 */ 1416 fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl; 1417 fabric->tf_ops.tpg_release_fabric_acl = 1418 &tcm_loop_tpg_release_fabric_acl; 1419 fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index; 1420 /* 1421 * Used for setting up remaining TCM resources in process context 1422 */ 1423 fabric->tf_ops.new_cmd_map = &tcm_loop_new_cmd_map; 1424 fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free; 1425 fabric->tf_ops.release_cmd = &tcm_loop_release_cmd; 1426 fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session; 1427 fabric->tf_ops.close_session = &tcm_loop_close_session; 1428 fabric->tf_ops.stop_session = &tcm_loop_stop_session; 1429 fabric->tf_ops.fall_back_to_erl0 = &tcm_loop_fall_back_to_erl0; 1430 fabric->tf_ops.sess_logged_in = &tcm_loop_sess_logged_in; 1431 fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index; 1432 fabric->tf_ops.sess_get_initiator_sid = NULL; 1433 fabric->tf_ops.write_pending = &tcm_loop_write_pending; 1434 fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status; 1435 /* 1436 * Not used for TCM loopback 1437 */ 1438 fabric->tf_ops.set_default_node_attributes = 1439 &tcm_loop_set_default_node_attributes; 1440 fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag; 1441 fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state; 1442 fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in; 1443 fabric->tf_ops.queue_status = &tcm_loop_queue_status; 1444 fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp; 1445 fabric->tf_ops.set_fabric_sense_len = &tcm_loop_set_fabric_sense_len; 1446 fabric->tf_ops.get_fabric_sense_len = &tcm_loop_get_fabric_sense_len; 1447 fabric->tf_ops.is_state_remove = &tcm_loop_is_state_remove; 1448 1449 tf_cg = &fabric->tf_group; 1450 /* 1451 * Setup function pointers for generic logic in target_core_fabric_configfs.c 1452 */ 1453 fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba; 1454 fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba; 1455 fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg; 1456 fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg; 1457 /* 1458 * fabric_post_link() and fabric_pre_unlink() are used for 1459 * registration and release of TCM Loop Virtual SCSI LUNs. 1460 */ 1461 fabric->tf_ops.fabric_post_link = &tcm_loop_port_link; 1462 fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink; 1463 fabric->tf_ops.fabric_make_np = NULL; 1464 fabric->tf_ops.fabric_drop_np = NULL; 1465 /* 1466 * Setup default attribute lists for various fabric->tf_cit_tmpl 1467 */ 1468 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs; 1469 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs; 1470 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL; 1471 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL; 1472 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL; 1473 /* 1474 * Once fabric->tf_ops has been setup, now register the fabric for 1475 * use within TCM 1476 */ 1477 ret = target_fabric_configfs_register(fabric); 1478 if (ret < 0) { 1479 pr_err("target_fabric_configfs_register() for" 1480 " TCM_Loop failed!\n"); 1481 target_fabric_configfs_free(fabric); 1482 return -1; 1483 } 1484 /* 1485 * Setup our local pointer to *fabric. 1486 */ 1487 tcm_loop_fabric_configfs = fabric; 1488 pr_debug("TCM_LOOP[0] - Set fabric ->" 1489 " tcm_loop_fabric_configfs\n"); 1490 return 0; 1491 } 1492 1493 static void tcm_loop_deregister_configfs(void) 1494 { 1495 if (!tcm_loop_fabric_configfs) 1496 return; 1497 1498 target_fabric_configfs_deregister(tcm_loop_fabric_configfs); 1499 tcm_loop_fabric_configfs = NULL; 1500 pr_debug("TCM_LOOP[0] - Cleared" 1501 " tcm_loop_fabric_configfs\n"); 1502 } 1503 1504 static int __init tcm_loop_fabric_init(void) 1505 { 1506 int ret; 1507 1508 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache", 1509 sizeof(struct tcm_loop_cmd), 1510 __alignof__(struct tcm_loop_cmd), 1511 0, NULL); 1512 if (!tcm_loop_cmd_cache) { 1513 pr_debug("kmem_cache_create() for" 1514 " tcm_loop_cmd_cache failed\n"); 1515 return -ENOMEM; 1516 } 1517 1518 ret = tcm_loop_alloc_core_bus(); 1519 if (ret) 1520 return ret; 1521 1522 ret = tcm_loop_register_configfs(); 1523 if (ret) { 1524 tcm_loop_release_core_bus(); 1525 return ret; 1526 } 1527 1528 return 0; 1529 } 1530 1531 static void __exit tcm_loop_fabric_exit(void) 1532 { 1533 tcm_loop_deregister_configfs(); 1534 tcm_loop_release_core_bus(); 1535 kmem_cache_destroy(tcm_loop_cmd_cache); 1536 } 1537 1538 MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module"); 1539 MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>"); 1540 MODULE_LICENSE("GPL"); 1541 module_init(tcm_loop_fabric_init); 1542 module_exit(tcm_loop_fabric_exit); 1543