1 /*******************************************************************************
3 * This file contains the Linux/SCSI LLD virtual SCSI initiator driver
4 * for emulated SAS initiator ports
6 * © Copyright 2011-2013 Datera, Inc.
8 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
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.
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 ****************************************************************************/
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>
35 #include <target/target_core_base.h>
36 #include <target/target_core_fabric.h>
40 #define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev)
42 static struct kmem_cache *tcm_loop_cmd_cache;
44 static int tcm_loop_hba_no_cnt;
46 static int tcm_loop_queue_status(struct se_cmd *se_cmd);
48 static unsigned int tcm_loop_nr_hw_queues = 1;
49 module_param_named(nr_hw_queues, tcm_loop_nr_hw_queues, uint, 0644);
51 static unsigned int tcm_loop_can_queue = 1024;
52 module_param_named(can_queue, tcm_loop_can_queue, uint, 0644);
54 static unsigned int tcm_loop_cmd_per_lun = 1024;
55 module_param_named(cmd_per_lun, tcm_loop_cmd_per_lun, uint, 0644);
58 * Called from struct target_core_fabric_ops->check_stop_free()
60 static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
62 return transport_generic_free_cmd(se_cmd, 0);
65 static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
67 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
68 struct tcm_loop_cmd, tl_se_cmd);
69 struct scsi_cmnd *sc = tl_cmd->sc;
71 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
72 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
77 static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host)
79 seq_puts(m, "tcm_loop_proc_info()\n");
83 static int tcm_loop_driver_probe(struct device *);
84 static void tcm_loop_driver_remove(struct device *);
86 static int pseudo_lld_bus_match(struct device *dev,
87 struct device_driver *dev_driver)
92 static struct bus_type tcm_loop_lld_bus = {
93 .name = "tcm_loop_bus",
94 .match = pseudo_lld_bus_match,
95 .probe = tcm_loop_driver_probe,
96 .remove = tcm_loop_driver_remove,
99 static struct device_driver tcm_loop_driverfs = {
101 .bus = &tcm_loop_lld_bus,
104 * Used with root_device_register() in tcm_loop_alloc_core_bus() below
106 static struct device *tcm_loop_primary;
108 static void tcm_loop_target_queue_cmd(struct tcm_loop_cmd *tl_cmd)
110 struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
111 struct scsi_cmnd *sc = tl_cmd->sc;
112 struct tcm_loop_nexus *tl_nexus;
113 struct tcm_loop_hba *tl_hba;
114 struct tcm_loop_tpg *tl_tpg;
115 struct scatterlist *sgl_bidi = NULL;
116 u32 sgl_bidi_count = 0, transfer_length;
118 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
119 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
122 * Ensure that this tl_tpg reference from the incoming sc->device->id
123 * has already been configured via tcm_loop_make_naa_tpg().
125 if (!tl_tpg->tl_hba) {
126 set_host_byte(sc, DID_NO_CONNECT);
129 if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) {
130 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
133 tl_nexus = tl_tpg->tl_nexus;
135 scmd_printk(KERN_ERR, sc,
136 "TCM_Loop I_T Nexus does not exist\n");
137 set_host_byte(sc, DID_ERROR);
141 transfer_length = scsi_transfer_length(sc);
142 if (!scsi_prot_sg_count(sc) &&
143 scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) {
144 se_cmd->prot_pto = true;
146 * loopback transport doesn't support
147 * WRITE_GENERATE, READ_STRIP protection
148 * information operations, go ahead unprotected.
150 transfer_length = scsi_bufflen(sc);
153 se_cmd->tag = tl_cmd->sc_cmd_tag;
154 target_init_cmd(se_cmd, tl_nexus->se_sess, &tl_cmd->tl_sense_buf[0],
155 tl_cmd->sc->device->lun, transfer_length,
156 TCM_SIMPLE_TAG, sc->sc_data_direction, 0);
158 if (target_submit_prep(se_cmd, sc->cmnd, scsi_sglist(sc),
159 scsi_sg_count(sc), sgl_bidi, sgl_bidi_count,
160 scsi_prot_sglist(sc), scsi_prot_sg_count(sc),
164 target_queue_submission(se_cmd);
172 * ->queuecommand can be and usually is called from interrupt context, so
173 * defer the actual submission to a workqueue.
175 static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
177 struct tcm_loop_cmd *tl_cmd = scsi_cmd_priv(sc);
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],
184 memset(tl_cmd, 0, sizeof(*tl_cmd));
186 tl_cmd->sc_cmd_tag = scsi_cmd_to_rq(sc)->tag;
188 tcm_loop_target_queue_cmd(tl_cmd);
193 * Called from SCSI EH process context to issue a LUN_RESET TMR
194 * to struct scsi_device
196 static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg,
197 u64 lun, int task, enum tcm_tmreq_table tmr)
199 struct se_cmd *se_cmd;
200 struct se_session *se_sess;
201 struct tcm_loop_nexus *tl_nexus;
202 struct tcm_loop_cmd *tl_cmd;
203 int ret = TMR_FUNCTION_FAILED, rc;
206 * Locate the tl_nexus and se_sess pointers
208 tl_nexus = tl_tpg->tl_nexus;
210 pr_err("Unable to perform device reset without active I_T Nexus\n");
214 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
218 init_completion(&tl_cmd->tmr_done);
220 se_cmd = &tl_cmd->tl_se_cmd;
221 se_sess = tl_tpg->tl_nexus->se_sess;
223 rc = target_submit_tmr(se_cmd, se_sess, tl_cmd->tl_sense_buf, lun,
224 NULL, tmr, GFP_KERNEL, task,
225 TARGET_SCF_ACK_KREF);
228 wait_for_completion(&tl_cmd->tmr_done);
229 ret = se_cmd->se_tmr_req->response;
230 target_put_sess_cmd(se_cmd);
236 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
240 static int tcm_loop_abort_task(struct scsi_cmnd *sc)
242 struct tcm_loop_hba *tl_hba;
243 struct tcm_loop_tpg *tl_tpg;
247 * Locate the tcm_loop_hba_t pointer
249 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
250 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
251 ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
252 scsi_cmd_to_rq(sc)->tag, TMR_ABORT_TASK);
253 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
257 * Called from SCSI EH process context to issue a LUN_RESET TMR
258 * to struct scsi_device
260 static int tcm_loop_device_reset(struct scsi_cmnd *sc)
262 struct tcm_loop_hba *tl_hba;
263 struct tcm_loop_tpg *tl_tpg;
267 * Locate the tcm_loop_hba_t pointer
269 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
270 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
272 ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
274 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
277 static int tcm_loop_target_reset(struct scsi_cmnd *sc)
279 struct tcm_loop_hba *tl_hba;
280 struct tcm_loop_tpg *tl_tpg;
283 * Locate the tcm_loop_hba_t pointer
285 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
287 pr_err("Unable to perform device reset without active I_T Nexus\n");
291 * Locate the tl_tpg pointer from TargetID in sc->device->id
293 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
295 tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
301 static const struct scsi_host_template tcm_loop_driver_template = {
302 .show_info = tcm_loop_show_info,
303 .proc_name = "tcm_loopback",
304 .name = "TCM_Loopback",
305 .queuecommand = tcm_loop_queuecommand,
306 .change_queue_depth = scsi_change_queue_depth,
307 .eh_abort_handler = tcm_loop_abort_task,
308 .eh_device_reset_handler = tcm_loop_device_reset,
309 .eh_target_reset_handler = tcm_loop_target_reset,
312 .max_sectors = 0xFFFF,
313 .dma_boundary = PAGE_SIZE - 1,
314 .module = THIS_MODULE,
315 .track_queue_depth = 1,
316 .cmd_size = sizeof(struct tcm_loop_cmd),
319 static int tcm_loop_driver_probe(struct device *dev)
321 struct tcm_loop_hba *tl_hba;
322 struct Scsi_Host *sh;
323 int error, host_prot;
325 tl_hba = to_tcm_loop_hba(dev);
327 sh = scsi_host_alloc(&tcm_loop_driver_template,
328 sizeof(struct tcm_loop_hba));
330 pr_err("Unable to allocate struct scsi_host\n");
336 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
338 *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
340 * Setup single ID, Channel and LUN for now..
345 sh->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
346 sh->nr_hw_queues = tcm_loop_nr_hw_queues;
347 sh->can_queue = tcm_loop_can_queue;
348 sh->cmd_per_lun = tcm_loop_cmd_per_lun;
350 host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
351 SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
352 SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
354 scsi_host_set_prot(sh, host_prot);
355 scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC);
357 error = scsi_add_host(sh, &tl_hba->dev);
359 pr_err("%s: scsi_add_host failed\n", __func__);
366 static void tcm_loop_driver_remove(struct device *dev)
368 struct tcm_loop_hba *tl_hba;
369 struct Scsi_Host *sh;
371 tl_hba = to_tcm_loop_hba(dev);
374 scsi_remove_host(sh);
378 static void tcm_loop_release_adapter(struct device *dev)
380 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
386 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
388 static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
392 tl_hba->dev.bus = &tcm_loop_lld_bus;
393 tl_hba->dev.parent = tcm_loop_primary;
394 tl_hba->dev.release = &tcm_loop_release_adapter;
395 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
397 ret = device_register(&tl_hba->dev);
399 pr_err("device_register() failed for tl_hba->dev: %d\n", ret);
400 put_device(&tl_hba->dev);
408 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
411 static int tcm_loop_alloc_core_bus(void)
415 tcm_loop_primary = root_device_register("tcm_loop_0");
416 if (IS_ERR(tcm_loop_primary)) {
417 pr_err("Unable to allocate tcm_loop_primary\n");
418 return PTR_ERR(tcm_loop_primary);
421 ret = bus_register(&tcm_loop_lld_bus);
423 pr_err("bus_register() failed for tcm_loop_lld_bus\n");
427 ret = driver_register(&tcm_loop_driverfs);
429 pr_err("driver_register() failed for tcm_loop_driverfs\n");
433 pr_debug("Initialized TCM Loop Core Bus\n");
437 bus_unregister(&tcm_loop_lld_bus);
439 root_device_unregister(tcm_loop_primary);
443 static void tcm_loop_release_core_bus(void)
445 driver_unregister(&tcm_loop_driverfs);
446 bus_unregister(&tcm_loop_lld_bus);
447 root_device_unregister(tcm_loop_primary);
449 pr_debug("Releasing TCM Loop Core BUS\n");
452 static inline struct tcm_loop_tpg *tl_tpg(struct se_portal_group *se_tpg)
454 return container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
457 static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
460 * Return the passed NAA identifier for the Target Port
462 return &tl_tpg(se_tpg)->tl_hba->tl_wwn_address[0];
465 static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
468 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
469 * to represent the SCSI Target Port.
471 return tl_tpg(se_tpg)->tl_tpgt;
475 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
476 * based upon the incoming fabric dependent SCSI Initiator Port
478 static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
483 static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg)
485 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
487 return tl_tpg->tl_fabric_prot_type;
490 static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
495 static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
497 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
498 struct tcm_loop_cmd, tl_se_cmd);
500 return tl_cmd->sc_cmd_state;
503 static int tcm_loop_write_pending(struct se_cmd *se_cmd)
506 * Since Linux/SCSI has already sent down a struct scsi_cmnd
507 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
508 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
509 * format with transport_generic_map_mem_to_cmd().
511 * We now tell TCM to add this WRITE CDB directly into the TCM storage
512 * object execution queue.
514 target_execute_cmd(se_cmd);
518 static int tcm_loop_queue_data_or_status(const char *func,
519 struct se_cmd *se_cmd, u8 scsi_status)
521 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
522 struct tcm_loop_cmd, tl_se_cmd);
523 struct scsi_cmnd *sc = tl_cmd->sc;
525 pr_debug("%s() called for scsi_cmnd: %p cdb: 0x%02x\n",
526 func, sc, sc->cmnd[0]);
528 if (se_cmd->sense_buffer &&
529 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
530 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
532 memcpy(sc->sense_buffer, se_cmd->sense_buffer,
533 SCSI_SENSE_BUFFERSIZE);
534 sc->result = SAM_STAT_CHECK_CONDITION;
536 sc->result = scsi_status;
538 set_host_byte(sc, DID_OK);
539 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
540 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
541 scsi_set_resid(sc, se_cmd->residual_count);
545 static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
547 return tcm_loop_queue_data_or_status(__func__, se_cmd, SAM_STAT_GOOD);
550 static int tcm_loop_queue_status(struct se_cmd *se_cmd)
552 return tcm_loop_queue_data_or_status(__func__,
553 se_cmd, se_cmd->scsi_status);
556 static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
558 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
559 struct tcm_loop_cmd, tl_se_cmd);
561 /* Wake up tcm_loop_issue_tmr(). */
562 complete(&tl_cmd->tmr_done);
565 static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
570 static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
572 switch (tl_hba->tl_proto_id) {
573 case SCSI_PROTOCOL_SAS:
575 case SCSI_PROTOCOL_FCP:
577 case SCSI_PROTOCOL_ISCSI:
586 /* Start items for tcm_loop_port_cit */
588 static int tcm_loop_port_link(
589 struct se_portal_group *se_tpg,
592 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
593 struct tcm_loop_tpg, tl_se_tpg);
594 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
596 atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
598 * Add Linux/SCSI struct scsi_device by HCTL
600 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
602 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
606 static void tcm_loop_port_unlink(
607 struct se_portal_group *se_tpg,
608 struct se_lun *se_lun)
610 struct scsi_device *sd;
611 struct tcm_loop_hba *tl_hba;
612 struct tcm_loop_tpg *tl_tpg;
614 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
615 tl_hba = tl_tpg->tl_hba;
617 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
618 se_lun->unpacked_lun);
620 pr_err("Unable to locate struct scsi_device for %d:%d:%llu\n",
621 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
625 * Remove Linux/SCSI struct scsi_device by HCTL
627 scsi_remove_device(sd);
630 atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
632 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
635 /* End items for tcm_loop_port_cit */
637 static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_show(
638 struct config_item *item, char *page)
640 struct se_portal_group *se_tpg = attrib_to_tpg(item);
641 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
644 return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type);
647 static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_store(
648 struct config_item *item, const char *page, size_t count)
650 struct se_portal_group *se_tpg = attrib_to_tpg(item);
651 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
654 int ret = kstrtoul(page, 0, &val);
657 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
660 if (val != 0 && val != 1 && val != 3) {
661 pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
664 tl_tpg->tl_fabric_prot_type = val;
669 CONFIGFS_ATTR(tcm_loop_tpg_attrib_, fabric_prot_type);
671 static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = {
672 &tcm_loop_tpg_attrib_attr_fabric_prot_type,
676 /* Start items for tcm_loop_nexus_cit */
678 static int tcm_loop_alloc_sess_cb(struct se_portal_group *se_tpg,
679 struct se_session *se_sess, void *p)
681 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
682 struct tcm_loop_tpg, tl_se_tpg);
684 tl_tpg->tl_nexus = p;
688 static int tcm_loop_make_nexus(
689 struct tcm_loop_tpg *tl_tpg,
692 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
693 struct tcm_loop_nexus *tl_nexus;
696 if (tl_tpg->tl_nexus) {
697 pr_debug("tl_tpg->tl_nexus already exists\n");
701 tl_nexus = kzalloc(sizeof(*tl_nexus), GFP_KERNEL);
705 tl_nexus->se_sess = target_setup_session(&tl_tpg->tl_se_tpg, 0, 0,
706 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS,
707 name, tl_nexus, tcm_loop_alloc_sess_cb);
708 if (IS_ERR(tl_nexus->se_sess)) {
709 ret = PTR_ERR(tl_nexus->se_sess);
714 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated %s Initiator Port: %s\n",
715 tcm_loop_dump_proto_id(tl_hba), name);
719 static int tcm_loop_drop_nexus(
720 struct tcm_loop_tpg *tpg)
722 struct se_session *se_sess;
723 struct tcm_loop_nexus *tl_nexus;
725 tl_nexus = tpg->tl_nexus;
729 se_sess = tl_nexus->se_sess;
733 if (atomic_read(&tpg->tl_tpg_port_count)) {
734 pr_err("Unable to remove TCM_Loop I_T Nexus with active TPG port count: %d\n",
735 atomic_read(&tpg->tl_tpg_port_count));
739 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated %s Initiator Port: %s\n",
740 tcm_loop_dump_proto_id(tpg->tl_hba),
741 tl_nexus->se_sess->se_node_acl->initiatorname);
743 * Release the SCSI I_T Nexus to the emulated Target Port
745 target_remove_session(se_sess);
746 tpg->tl_nexus = NULL;
751 /* End items for tcm_loop_nexus_cit */
753 static ssize_t tcm_loop_tpg_nexus_show(struct config_item *item, char *page)
755 struct se_portal_group *se_tpg = to_tpg(item);
756 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
757 struct tcm_loop_tpg, tl_se_tpg);
758 struct tcm_loop_nexus *tl_nexus;
761 tl_nexus = tl_tpg->tl_nexus;
765 ret = snprintf(page, PAGE_SIZE, "%s\n",
766 tl_nexus->se_sess->se_node_acl->initiatorname);
771 static ssize_t tcm_loop_tpg_nexus_store(struct config_item *item,
772 const char *page, size_t count)
774 struct se_portal_group *se_tpg = to_tpg(item);
775 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
776 struct tcm_loop_tpg, tl_se_tpg);
777 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
778 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
781 * Shutdown the active I_T nexus if 'NULL' is passed..
783 if (!strncmp(page, "NULL", 4)) {
784 ret = tcm_loop_drop_nexus(tl_tpg);
785 return (!ret) ? count : ret;
788 * Otherwise make sure the passed virtual Initiator port WWN matches
789 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
790 * tcm_loop_make_nexus()
792 if (strlen(page) >= TL_WWN_ADDR_LEN) {
793 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
794 page, TL_WWN_ADDR_LEN);
797 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
799 ptr = strstr(i_port, "naa.");
801 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
802 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
803 i_port, tcm_loop_dump_proto_id(tl_hba));
806 port_ptr = &i_port[0];
809 ptr = strstr(i_port, "fc.");
811 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
812 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
813 i_port, tcm_loop_dump_proto_id(tl_hba));
816 port_ptr = &i_port[3]; /* Skip over "fc." */
819 ptr = strstr(i_port, "iqn.");
821 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
822 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
823 i_port, tcm_loop_dump_proto_id(tl_hba));
826 port_ptr = &i_port[0];
829 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
833 * Clear any trailing newline for the NAA WWN
836 if (i_port[strlen(i_port)-1] == '\n')
837 i_port[strlen(i_port)-1] = '\0';
839 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
846 static ssize_t tcm_loop_tpg_transport_status_show(struct config_item *item,
849 struct se_portal_group *se_tpg = to_tpg(item);
850 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
851 struct tcm_loop_tpg, tl_se_tpg);
852 const char *status = NULL;
853 ssize_t ret = -EINVAL;
855 switch (tl_tpg->tl_transport_status) {
856 case TCM_TRANSPORT_ONLINE:
859 case TCM_TRANSPORT_OFFLINE:
867 ret = snprintf(page, PAGE_SIZE, "%s\n", status);
872 static ssize_t tcm_loop_tpg_transport_status_store(struct config_item *item,
873 const char *page, size_t count)
875 struct se_portal_group *se_tpg = to_tpg(item);
876 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
877 struct tcm_loop_tpg, tl_se_tpg);
879 if (!strncmp(page, "online", 6)) {
880 tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
883 if (!strncmp(page, "offline", 7)) {
884 tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
885 if (tl_tpg->tl_nexus) {
886 struct se_session *tl_sess = tl_tpg->tl_nexus->se_sess;
888 core_allocate_nexus_loss_ua(tl_sess->se_node_acl);
895 static ssize_t tcm_loop_tpg_address_show(struct config_item *item,
898 struct se_portal_group *se_tpg = to_tpg(item);
899 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
900 struct tcm_loop_tpg, tl_se_tpg);
901 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
903 return snprintf(page, PAGE_SIZE, "%d:0:%d\n",
904 tl_hba->sh->host_no, tl_tpg->tl_tpgt);
907 CONFIGFS_ATTR(tcm_loop_tpg_, nexus);
908 CONFIGFS_ATTR(tcm_loop_tpg_, transport_status);
909 CONFIGFS_ATTR_RO(tcm_loop_tpg_, address);
911 static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
912 &tcm_loop_tpg_attr_nexus,
913 &tcm_loop_tpg_attr_transport_status,
914 &tcm_loop_tpg_attr_address,
918 /* Start items for tcm_loop_naa_cit */
920 static struct se_portal_group *tcm_loop_make_naa_tpg(struct se_wwn *wwn,
923 struct tcm_loop_hba *tl_hba = container_of(wwn,
924 struct tcm_loop_hba, tl_hba_wwn);
925 struct tcm_loop_tpg *tl_tpg;
929 if (strstr(name, "tpgt_") != name) {
930 pr_err("Unable to locate \"tpgt_#\" directory group\n");
931 return ERR_PTR(-EINVAL);
933 if (kstrtoul(name+5, 10, &tpgt))
934 return ERR_PTR(-EINVAL);
936 if (tpgt >= TL_TPGS_PER_HBA) {
937 pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA: %u\n",
938 tpgt, TL_TPGS_PER_HBA);
939 return ERR_PTR(-EINVAL);
941 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
942 tl_tpg->tl_hba = tl_hba;
943 tl_tpg->tl_tpgt = tpgt;
945 * Register the tl_tpg as a emulated TCM Target Endpoint
947 ret = core_tpg_register(wwn, &tl_tpg->tl_se_tpg, tl_hba->tl_proto_id);
949 return ERR_PTR(-ENOMEM);
951 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s Target Port %s,t,0x%04lx\n",
952 tcm_loop_dump_proto_id(tl_hba),
953 config_item_name(&wwn->wwn_group.cg_item), tpgt);
954 return &tl_tpg->tl_se_tpg;
957 static void tcm_loop_drop_naa_tpg(
958 struct se_portal_group *se_tpg)
960 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
961 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
962 struct tcm_loop_tpg, tl_se_tpg);
963 struct tcm_loop_hba *tl_hba;
966 tl_hba = tl_tpg->tl_hba;
967 tpgt = tl_tpg->tl_tpgt;
969 * Release the I_T Nexus for the Virtual target link if present
971 tcm_loop_drop_nexus(tl_tpg);
973 * Deregister the tl_tpg as a emulated TCM Target Endpoint
975 core_tpg_deregister(se_tpg);
977 tl_tpg->tl_hba = NULL;
980 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s Target Port %s,t,0x%04x\n",
981 tcm_loop_dump_proto_id(tl_hba),
982 config_item_name(&wwn->wwn_group.cg_item), tpgt);
985 /* End items for tcm_loop_naa_cit */
987 /* Start items for tcm_loop_cit */
989 static struct se_wwn *tcm_loop_make_scsi_hba(
990 struct target_fabric_configfs *tf,
991 struct config_group *group,
994 struct tcm_loop_hba *tl_hba;
995 struct Scsi_Host *sh;
999 tl_hba = kzalloc(sizeof(*tl_hba), GFP_KERNEL);
1001 return ERR_PTR(-ENOMEM);
1004 * Determine the emulated Protocol Identifier and Target Port Name
1005 * based on the incoming configfs directory name.
1007 ptr = strstr(name, "naa.");
1009 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1012 ptr = strstr(name, "fc.");
1014 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1015 off = 3; /* Skip over "fc." */
1018 ptr = strstr(name, "iqn.");
1020 pr_err("Unable to locate prefix for emulated Target Port: %s\n",
1025 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
1028 if (strlen(name) >= TL_WWN_ADDR_LEN) {
1029 pr_err("Emulated NAA %s Address: %s, exceeds max: %d\n",
1030 name, tcm_loop_dump_proto_id(tl_hba), TL_WWN_ADDR_LEN);
1034 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1037 * Call device_register(tl_hba->dev) to register the emulated
1038 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1039 * device_register() callbacks in tcm_loop_driver_probe()
1041 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1043 return ERR_PTR(ret);
1046 tcm_loop_hba_no_cnt++;
1047 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
1048 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1049 return &tl_hba->tl_hba_wwn;
1052 return ERR_PTR(ret);
1055 static void tcm_loop_drop_scsi_hba(
1058 struct tcm_loop_hba *tl_hba = container_of(wwn,
1059 struct tcm_loop_hba, tl_hba_wwn);
1061 pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
1062 tcm_loop_dump_proto_id(tl_hba), tl_hba->tl_wwn_address,
1063 tl_hba->sh->host_no);
1065 * Call device_unregister() on the original tl_hba->dev.
1066 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1069 device_unregister(&tl_hba->dev);
1072 /* Start items for tcm_loop_cit */
1073 static ssize_t tcm_loop_wwn_version_show(struct config_item *item, char *page)
1075 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1078 CONFIGFS_ATTR_RO(tcm_loop_wwn_, version);
1080 static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1081 &tcm_loop_wwn_attr_version,
1085 /* End items for tcm_loop_cit */
1087 static const struct target_core_fabric_ops loop_ops = {
1088 .module = THIS_MODULE,
1089 .fabric_name = "loopback",
1090 .tpg_get_wwn = tcm_loop_get_endpoint_wwn,
1091 .tpg_get_tag = tcm_loop_get_tag,
1092 .tpg_check_demo_mode = tcm_loop_check_demo_mode,
1093 .tpg_check_prot_fabric_only = tcm_loop_check_prot_fabric_only,
1094 .check_stop_free = tcm_loop_check_stop_free,
1095 .release_cmd = tcm_loop_release_cmd,
1096 .sess_get_index = tcm_loop_sess_get_index,
1097 .write_pending = tcm_loop_write_pending,
1098 .get_cmd_state = tcm_loop_get_cmd_state,
1099 .queue_data_in = tcm_loop_queue_data_in,
1100 .queue_status = tcm_loop_queue_status,
1101 .queue_tm_rsp = tcm_loop_queue_tm_rsp,
1102 .aborted_task = tcm_loop_aborted_task,
1103 .fabric_make_wwn = tcm_loop_make_scsi_hba,
1104 .fabric_drop_wwn = tcm_loop_drop_scsi_hba,
1105 .fabric_make_tpg = tcm_loop_make_naa_tpg,
1106 .fabric_drop_tpg = tcm_loop_drop_naa_tpg,
1107 .fabric_post_link = tcm_loop_port_link,
1108 .fabric_pre_unlink = tcm_loop_port_unlink,
1109 .tfc_wwn_attrs = tcm_loop_wwn_attrs,
1110 .tfc_tpg_base_attrs = tcm_loop_tpg_attrs,
1111 .tfc_tpg_attrib_attrs = tcm_loop_tpg_attrib_attrs,
1114 static int __init tcm_loop_fabric_init(void)
1118 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1119 sizeof(struct tcm_loop_cmd),
1120 __alignof__(struct tcm_loop_cmd),
1122 if (!tcm_loop_cmd_cache) {
1123 pr_debug("kmem_cache_create() for tcm_loop_cmd_cache failed\n");
1127 ret = tcm_loop_alloc_core_bus();
1129 goto out_destroy_cache;
1131 ret = target_register_template(&loop_ops);
1133 goto out_release_core_bus;
1137 out_release_core_bus:
1138 tcm_loop_release_core_bus();
1140 kmem_cache_destroy(tcm_loop_cmd_cache);
1145 static void __exit tcm_loop_fabric_exit(void)
1147 target_unregister_template(&loop_ops);
1148 tcm_loop_release_core_bus();
1149 kmem_cache_destroy(tcm_loop_cmd_cache);
1152 MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1154 MODULE_LICENSE("GPL");
1155 module_init(tcm_loop_fabric_init);
1156 module_exit(tcm_loop_fabric_exit);