13703b2c5SNicholas Bellinger /******************************************************************************* 23703b2c5SNicholas Bellinger * 33703b2c5SNicholas Bellinger * This file contains the Linux/SCSI LLD virtual SCSI initiator driver 43703b2c5SNicholas Bellinger * for emulated SAS initiator ports 53703b2c5SNicholas Bellinger * 64c76251eSNicholas Bellinger * © Copyright 2011-2013 Datera, Inc. 73703b2c5SNicholas Bellinger * 83703b2c5SNicholas Bellinger * Licensed to the Linux Foundation under the General Public License (GPL) version 2. 93703b2c5SNicholas Bellinger * 103703b2c5SNicholas Bellinger * Author: Nicholas A. Bellinger <nab@risingtidesystems.com> 113703b2c5SNicholas Bellinger * 123703b2c5SNicholas Bellinger * This program is free software; you can redistribute it and/or modify 133703b2c5SNicholas Bellinger * it under the terms of the GNU General Public License as published by 143703b2c5SNicholas Bellinger * the Free Software Foundation; either version 2 of the License, or 153703b2c5SNicholas Bellinger * (at your option) any later version. 163703b2c5SNicholas Bellinger * 173703b2c5SNicholas Bellinger * This program is distributed in the hope that it will be useful, 183703b2c5SNicholas Bellinger * but WITHOUT ANY WARRANTY; without even the implied warranty of 193703b2c5SNicholas Bellinger * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 203703b2c5SNicholas Bellinger * GNU General Public License for more details. 213703b2c5SNicholas Bellinger ****************************************************************************/ 223703b2c5SNicholas Bellinger 233703b2c5SNicholas Bellinger #include <linux/module.h> 243703b2c5SNicholas Bellinger #include <linux/moduleparam.h> 253703b2c5SNicholas Bellinger #include <linux/init.h> 263703b2c5SNicholas Bellinger #include <linux/slab.h> 273703b2c5SNicholas Bellinger #include <linux/types.h> 283703b2c5SNicholas Bellinger #include <linux/configfs.h> 293703b2c5SNicholas Bellinger #include <scsi/scsi.h> 303703b2c5SNicholas Bellinger #include <scsi/scsi_tcq.h> 313703b2c5SNicholas Bellinger #include <scsi/scsi_host.h> 323703b2c5SNicholas Bellinger #include <scsi/scsi_device.h> 333703b2c5SNicholas Bellinger #include <scsi/scsi_cmnd.h> 343703b2c5SNicholas Bellinger 353703b2c5SNicholas Bellinger #include <target/target_core_base.h> 36c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h> 373703b2c5SNicholas Bellinger #include <target/target_core_fabric_configfs.h> 383703b2c5SNicholas Bellinger #include <target/target_core_configfs.h> 393703b2c5SNicholas Bellinger 403703b2c5SNicholas Bellinger #include "tcm_loop.h" 413703b2c5SNicholas Bellinger 423703b2c5SNicholas Bellinger #define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev) 433703b2c5SNicholas Bellinger 449ac8928eSChristoph Hellwig static const struct target_core_fabric_ops loop_ops; 453703b2c5SNicholas Bellinger 46afe2cb7fSChristoph Hellwig static struct workqueue_struct *tcm_loop_workqueue; 473703b2c5SNicholas Bellinger static struct kmem_cache *tcm_loop_cmd_cache; 483703b2c5SNicholas Bellinger 493703b2c5SNicholas Bellinger static int tcm_loop_hba_no_cnt; 503703b2c5SNicholas Bellinger 5116786454SChristoph Hellwig static int tcm_loop_queue_status(struct se_cmd *se_cmd); 523703b2c5SNicholas Bellinger 533703b2c5SNicholas Bellinger /* 543703b2c5SNicholas Bellinger * Called from struct target_core_fabric_ops->check_stop_free() 553703b2c5SNicholas Bellinger */ 5688dd9e26SNicholas Bellinger static int tcm_loop_check_stop_free(struct se_cmd *se_cmd) 573703b2c5SNicholas Bellinger { 583703b2c5SNicholas Bellinger /* 593703b2c5SNicholas Bellinger * Do not release struct se_cmd's containing a valid TMR 603703b2c5SNicholas Bellinger * pointer. These will be released directly in tcm_loop_device_reset() 613703b2c5SNicholas Bellinger * with transport_generic_free_cmd(). 623703b2c5SNicholas Bellinger */ 63c8e31f26SAndy Grover if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) 6488dd9e26SNicholas Bellinger return 0; 653703b2c5SNicholas Bellinger /* 663703b2c5SNicholas Bellinger * Release the struct se_cmd, which will make a callback to release 673703b2c5SNicholas Bellinger * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd() 683703b2c5SNicholas Bellinger */ 6982f1c8a4SChristoph Hellwig transport_generic_free_cmd(se_cmd, 0); 7088dd9e26SNicholas Bellinger return 1; 713703b2c5SNicholas Bellinger } 723703b2c5SNicholas Bellinger 7335462975SChristoph Hellwig static void tcm_loop_release_cmd(struct se_cmd *se_cmd) 743703b2c5SNicholas Bellinger { 753703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 763703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd); 773703b2c5SNicholas Bellinger 783703b2c5SNicholas Bellinger kmem_cache_free(tcm_loop_cmd_cache, tl_cmd); 793703b2c5SNicholas Bellinger } 803703b2c5SNicholas Bellinger 818946b077SAl Viro static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host) 823703b2c5SNicholas Bellinger { 838946b077SAl Viro seq_printf(m, "tcm_loop_proc_info()\n"); 848946b077SAl Viro return 0; 853703b2c5SNicholas Bellinger } 863703b2c5SNicholas Bellinger 873703b2c5SNicholas Bellinger static int tcm_loop_driver_probe(struct device *); 883703b2c5SNicholas Bellinger static int tcm_loop_driver_remove(struct device *); 893703b2c5SNicholas Bellinger 903703b2c5SNicholas Bellinger static int pseudo_lld_bus_match(struct device *dev, 913703b2c5SNicholas Bellinger struct device_driver *dev_driver) 923703b2c5SNicholas Bellinger { 933703b2c5SNicholas Bellinger return 1; 943703b2c5SNicholas Bellinger } 953703b2c5SNicholas Bellinger 963703b2c5SNicholas Bellinger static struct bus_type tcm_loop_lld_bus = { 973703b2c5SNicholas Bellinger .name = "tcm_loop_bus", 983703b2c5SNicholas Bellinger .match = pseudo_lld_bus_match, 993703b2c5SNicholas Bellinger .probe = tcm_loop_driver_probe, 1003703b2c5SNicholas Bellinger .remove = tcm_loop_driver_remove, 1013703b2c5SNicholas Bellinger }; 1023703b2c5SNicholas Bellinger 1033703b2c5SNicholas Bellinger static struct device_driver tcm_loop_driverfs = { 1043703b2c5SNicholas Bellinger .name = "tcm_loop", 1053703b2c5SNicholas Bellinger .bus = &tcm_loop_lld_bus, 1063703b2c5SNicholas Bellinger }; 1073703b2c5SNicholas Bellinger /* 1083703b2c5SNicholas Bellinger * Used with root_device_register() in tcm_loop_alloc_core_bus() below 1093703b2c5SNicholas Bellinger */ 1106e1a27b9SChristoph Hellwig static struct device *tcm_loop_primary; 1113703b2c5SNicholas Bellinger 112afe2cb7fSChristoph Hellwig static void tcm_loop_submission_work(struct work_struct *work) 1133703b2c5SNicholas Bellinger { 114afe2cb7fSChristoph Hellwig struct tcm_loop_cmd *tl_cmd = 115afe2cb7fSChristoph Hellwig container_of(work, struct tcm_loop_cmd, work); 116afe2cb7fSChristoph Hellwig struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd; 117afe2cb7fSChristoph Hellwig struct scsi_cmnd *sc = tl_cmd->sc; 118afe2cb7fSChristoph Hellwig struct tcm_loop_nexus *tl_nexus; 1193703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 1203703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg; 12116786454SChristoph Hellwig struct scatterlist *sgl_bidi = NULL; 122e2a4f55cSSagi Grimberg u32 sgl_bidi_count = 0, transfer_length; 1238f9f44f8SNicholas Bellinger int rc; 124f872c9f4SChristoph Hellwig 1253703b2c5SNicholas Bellinger tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 1263703b2c5SNicholas Bellinger tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 127afe2cb7fSChristoph Hellwig 1280a020436SNicholas Bellinger /* 1290a020436SNicholas Bellinger * Ensure that this tl_tpg reference from the incoming sc->device->id 1300a020436SNicholas Bellinger * has already been configured via tcm_loop_make_naa_tpg(). 1310a020436SNicholas Bellinger */ 1320a020436SNicholas Bellinger if (!tl_tpg->tl_hba) { 1330a020436SNicholas Bellinger set_host_byte(sc, DID_NO_CONNECT); 134f872c9f4SChristoph Hellwig goto out_done; 1350a020436SNicholas Bellinger } 136fb2b2844SHannes Reinecke if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) { 137fb2b2844SHannes Reinecke set_host_byte(sc, DID_TRANSPORT_DISRUPTED); 138fb2b2844SHannes Reinecke goto out_done; 139fb2b2844SHannes Reinecke } 140506787a2SHannes Reinecke tl_nexus = tl_tpg->tl_nexus; 141f872c9f4SChristoph Hellwig if (!tl_nexus) { 142f872c9f4SChristoph Hellwig scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus" 143f872c9f4SChristoph Hellwig " does not exist\n"); 144f872c9f4SChristoph Hellwig set_host_byte(sc, DID_ERROR); 145f872c9f4SChristoph Hellwig goto out_done; 1463703b2c5SNicholas Bellinger } 14716786454SChristoph Hellwig if (scsi_bidi_cmnd(sc)) { 14816786454SChristoph Hellwig struct scsi_data_buffer *sdb = scsi_in(sc); 14916786454SChristoph Hellwig 15016786454SChristoph Hellwig sgl_bidi = sdb->table.sgl; 15116786454SChristoph Hellwig sgl_bidi_count = sdb->table.nents; 152f872c9f4SChristoph Hellwig se_cmd->se_cmd_flags |= SCF_BIDI; 153f872c9f4SChristoph Hellwig 15416786454SChristoph Hellwig } 155b5b8e298SSagi Grimberg 156e2a4f55cSSagi Grimberg transfer_length = scsi_transfer_length(sc); 157e2a4f55cSSagi Grimberg if (!scsi_prot_sg_count(sc) && 158e2a4f55cSSagi Grimberg scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) { 159b5b8e298SSagi Grimberg se_cmd->prot_pto = true; 160e2a4f55cSSagi Grimberg /* 161e2a4f55cSSagi Grimberg * loopback transport doesn't support 162e2a4f55cSSagi Grimberg * WRITE_GENERATE, READ_STRIP protection 163e2a4f55cSSagi Grimberg * information operations, go ahead unprotected. 164e2a4f55cSSagi Grimberg */ 165e2a4f55cSSagi Grimberg transfer_length = scsi_bufflen(sc); 166e2a4f55cSSagi Grimberg } 167b5b8e298SSagi Grimberg 168649ee054SBart Van Assche se_cmd->tag = tl_cmd->sc_cmd_tag; 1698f9f44f8SNicholas Bellinger rc = target_submit_cmd_map_sgls(se_cmd, tl_nexus->se_sess, sc->cmnd, 1708f9f44f8SNicholas Bellinger &tl_cmd->tl_sense_buf[0], tl_cmd->sc->device->lun, 17168d81f40SChristoph Hellwig transfer_length, TCM_SIMPLE_TAG, 1728f9f44f8SNicholas Bellinger sc->sc_data_direction, 0, 1738f9f44f8SNicholas Bellinger scsi_sglist(sc), scsi_sg_count(sc), 17459dedde2SNicholas Bellinger sgl_bidi, sgl_bidi_count, 17559dedde2SNicholas Bellinger scsi_prot_sglist(sc), scsi_prot_sg_count(sc)); 1768f9f44f8SNicholas Bellinger if (rc < 0) { 177f872c9f4SChristoph Hellwig set_host_byte(sc, DID_NO_CONNECT); 178f872c9f4SChristoph Hellwig goto out_done; 179f872c9f4SChristoph Hellwig } 180afe2cb7fSChristoph Hellwig return; 181f872c9f4SChristoph Hellwig 182f872c9f4SChristoph Hellwig out_done: 183b43f1886SNicholas Bellinger kmem_cache_free(tcm_loop_cmd_cache, tl_cmd); 184f872c9f4SChristoph Hellwig sc->scsi_done(sc); 185afe2cb7fSChristoph Hellwig return; 186afe2cb7fSChristoph Hellwig } 187afe2cb7fSChristoph Hellwig 188afe2cb7fSChristoph Hellwig /* 189afe2cb7fSChristoph Hellwig * ->queuecommand can be and usually is called from interrupt context, so 190afe2cb7fSChristoph Hellwig * defer the actual submission to a workqueue. 191afe2cb7fSChristoph Hellwig */ 192afe2cb7fSChristoph Hellwig static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc) 193afe2cb7fSChristoph Hellwig { 194afe2cb7fSChristoph Hellwig struct tcm_loop_cmd *tl_cmd; 195afe2cb7fSChristoph Hellwig 1969cb78c16SHannes Reinecke pr_debug("tcm_loop_queuecommand() %d:%d:%d:%llu got CDB: 0x%02x" 197afe2cb7fSChristoph Hellwig " scsi_buf_len: %u\n", sc->device->host->host_no, 198afe2cb7fSChristoph Hellwig sc->device->id, sc->device->channel, sc->device->lun, 199afe2cb7fSChristoph Hellwig sc->cmnd[0], scsi_bufflen(sc)); 200afe2cb7fSChristoph Hellwig 201afe2cb7fSChristoph Hellwig tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC); 202afe2cb7fSChristoph Hellwig if (!tl_cmd) { 203afe2cb7fSChristoph Hellwig pr_err("Unable to allocate struct tcm_loop_cmd\n"); 204afe2cb7fSChristoph Hellwig set_host_byte(sc, DID_ERROR); 205afe2cb7fSChristoph Hellwig sc->scsi_done(sc); 206afe2cb7fSChristoph Hellwig return 0; 207afe2cb7fSChristoph Hellwig } 208afe2cb7fSChristoph Hellwig 209afe2cb7fSChristoph Hellwig tl_cmd->sc = sc; 2106375f890SHannes Reinecke tl_cmd->sc_cmd_tag = sc->request->tag; 211afe2cb7fSChristoph Hellwig INIT_WORK(&tl_cmd->work, tcm_loop_submission_work); 212afe2cb7fSChristoph Hellwig queue_work(tcm_loop_workqueue, &tl_cmd->work); 213f872c9f4SChristoph Hellwig return 0; 2143703b2c5SNicholas Bellinger } 2153703b2c5SNicholas Bellinger 2163703b2c5SNicholas Bellinger /* 2173703b2c5SNicholas Bellinger * Called from SCSI EH process context to issue a LUN_RESET TMR 2183703b2c5SNicholas Bellinger * to struct scsi_device 2193703b2c5SNicholas Bellinger */ 220a314d700SHannes Reinecke static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg, 221969871cdSHannes Reinecke int lun, int task, enum tcm_tmreq_table tmr) 2223703b2c5SNicholas Bellinger { 2233703b2c5SNicholas Bellinger struct se_cmd *se_cmd = NULL; 2243703b2c5SNicholas Bellinger struct se_session *se_sess; 225a314d700SHannes Reinecke struct se_portal_group *se_tpg; 226506787a2SHannes Reinecke struct tcm_loop_nexus *tl_nexus; 2273703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = NULL; 228a314d700SHannes Reinecke struct tcm_loop_tmr *tl_tmr = NULL; 229a314d700SHannes Reinecke int ret = TMR_FUNCTION_FAILED, rc; 230a314d700SHannes Reinecke 231506787a2SHannes Reinecke /* 232506787a2SHannes Reinecke * Locate the tl_nexus and se_sess pointers 233506787a2SHannes Reinecke */ 234506787a2SHannes Reinecke tl_nexus = tl_tpg->tl_nexus; 235506787a2SHannes Reinecke if (!tl_nexus) { 236506787a2SHannes Reinecke pr_err("Unable to perform device reset without" 237506787a2SHannes Reinecke " active I_T Nexus\n"); 238506787a2SHannes Reinecke return ret; 239506787a2SHannes Reinecke } 240506787a2SHannes Reinecke 241a314d700SHannes Reinecke tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL); 242a314d700SHannes Reinecke if (!tl_cmd) { 243a314d700SHannes Reinecke pr_err("Unable to allocate memory for tl_cmd\n"); 244a314d700SHannes Reinecke return ret; 245a314d700SHannes Reinecke } 246a314d700SHannes Reinecke 247a314d700SHannes Reinecke tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL); 248a314d700SHannes Reinecke if (!tl_tmr) { 249a314d700SHannes Reinecke pr_err("Unable to allocate memory for tl_tmr\n"); 250a314d700SHannes Reinecke goto release; 251a314d700SHannes Reinecke } 252a314d700SHannes Reinecke init_waitqueue_head(&tl_tmr->tl_tmr_wait); 253a314d700SHannes Reinecke 254a314d700SHannes Reinecke se_cmd = &tl_cmd->tl_se_cmd; 255a314d700SHannes Reinecke se_tpg = &tl_tpg->tl_se_tpg; 256506787a2SHannes Reinecke se_sess = tl_tpg->tl_nexus->se_sess; 257a314d700SHannes Reinecke /* 258a314d700SHannes Reinecke * Initialize struct se_cmd descriptor from target_core_mod infrastructure 259a314d700SHannes Reinecke */ 260a314d700SHannes Reinecke transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0, 26168d81f40SChristoph Hellwig DMA_NONE, TCM_SIMPLE_TAG, 262a314d700SHannes Reinecke &tl_cmd->tl_sense_buf[0]); 263a314d700SHannes Reinecke 264a314d700SHannes Reinecke rc = core_tmr_alloc_req(se_cmd, tl_tmr, tmr, GFP_KERNEL); 265a314d700SHannes Reinecke if (rc < 0) 266a314d700SHannes Reinecke goto release; 267a314d700SHannes Reinecke 268969871cdSHannes Reinecke if (tmr == TMR_ABORT_TASK) 269969871cdSHannes Reinecke se_cmd->se_tmr_req->ref_task_tag = task; 270969871cdSHannes Reinecke 271a314d700SHannes Reinecke /* 272a314d700SHannes Reinecke * Locate the underlying TCM struct se_lun 273a314d700SHannes Reinecke */ 274a314d700SHannes Reinecke if (transport_lookup_tmr_lun(se_cmd, lun) < 0) { 275a314d700SHannes Reinecke ret = TMR_LUN_DOES_NOT_EXIST; 276a314d700SHannes Reinecke goto release; 277a314d700SHannes Reinecke } 278a314d700SHannes Reinecke /* 279a314d700SHannes Reinecke * Queue the TMR to TCM Core and sleep waiting for 280a314d700SHannes Reinecke * tcm_loop_queue_tm_rsp() to wake us up. 281a314d700SHannes Reinecke */ 282a314d700SHannes Reinecke transport_generic_handle_tmr(se_cmd); 283a314d700SHannes Reinecke wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete)); 284a314d700SHannes Reinecke /* 285a314d700SHannes Reinecke * The TMR LUN_RESET has completed, check the response status and 286a314d700SHannes Reinecke * then release allocations. 287a314d700SHannes Reinecke */ 288a314d700SHannes Reinecke ret = se_cmd->se_tmr_req->response; 289a314d700SHannes Reinecke release: 290a314d700SHannes Reinecke if (se_cmd) 291a314d700SHannes Reinecke transport_generic_free_cmd(se_cmd, 1); 292a314d700SHannes Reinecke else 293a314d700SHannes Reinecke kmem_cache_free(tcm_loop_cmd_cache, tl_cmd); 294a314d700SHannes Reinecke kfree(tl_tmr); 295a314d700SHannes Reinecke return ret; 296a314d700SHannes Reinecke } 297a314d700SHannes Reinecke 298969871cdSHannes Reinecke static int tcm_loop_abort_task(struct scsi_cmnd *sc) 299969871cdSHannes Reinecke { 300969871cdSHannes Reinecke struct tcm_loop_hba *tl_hba; 301969871cdSHannes Reinecke struct tcm_loop_tpg *tl_tpg; 302969871cdSHannes Reinecke int ret = FAILED; 303969871cdSHannes Reinecke 304969871cdSHannes Reinecke /* 305969871cdSHannes Reinecke * Locate the tcm_loop_hba_t pointer 306969871cdSHannes Reinecke */ 307969871cdSHannes Reinecke tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 308969871cdSHannes Reinecke tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 309506787a2SHannes Reinecke ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun, 3106375f890SHannes Reinecke sc->request->tag, TMR_ABORT_TASK); 311969871cdSHannes Reinecke return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED; 312969871cdSHannes Reinecke } 313969871cdSHannes Reinecke 314a314d700SHannes Reinecke /* 315a314d700SHannes Reinecke * Called from SCSI EH process context to issue a LUN_RESET TMR 316a314d700SHannes Reinecke * to struct scsi_device 317a314d700SHannes Reinecke */ 318a314d700SHannes Reinecke static int tcm_loop_device_reset(struct scsi_cmnd *sc) 319a314d700SHannes Reinecke { 3203703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 3213703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg; 322a314d700SHannes Reinecke int ret = FAILED; 323a314d700SHannes Reinecke 3243703b2c5SNicholas Bellinger /* 3253703b2c5SNicholas Bellinger * Locate the tcm_loop_hba_t pointer 3263703b2c5SNicholas Bellinger */ 3273703b2c5SNicholas Bellinger tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 3283703b2c5SNicholas Bellinger tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 329506787a2SHannes Reinecke 330506787a2SHannes Reinecke ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun, 331969871cdSHannes Reinecke 0, TMR_LUN_RESET); 332a314d700SHannes Reinecke return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED; 3333703b2c5SNicholas Bellinger } 3343703b2c5SNicholas Bellinger 3358f4a1fb0SHannes Reinecke static int tcm_loop_target_reset(struct scsi_cmnd *sc) 3368f4a1fb0SHannes Reinecke { 3378f4a1fb0SHannes Reinecke struct tcm_loop_hba *tl_hba; 3388f4a1fb0SHannes Reinecke struct tcm_loop_tpg *tl_tpg; 3398f4a1fb0SHannes Reinecke 3408f4a1fb0SHannes Reinecke /* 3418f4a1fb0SHannes Reinecke * Locate the tcm_loop_hba_t pointer 3428f4a1fb0SHannes Reinecke */ 3438f4a1fb0SHannes Reinecke tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 3448f4a1fb0SHannes Reinecke if (!tl_hba) { 3458f4a1fb0SHannes Reinecke pr_err("Unable to perform device reset without" 3468f4a1fb0SHannes Reinecke " active I_T Nexus\n"); 3478f4a1fb0SHannes Reinecke return FAILED; 3488f4a1fb0SHannes Reinecke } 3498f4a1fb0SHannes Reinecke /* 3508f4a1fb0SHannes Reinecke * Locate the tl_tpg pointer from TargetID in sc->device->id 3518f4a1fb0SHannes Reinecke */ 3528f4a1fb0SHannes Reinecke tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 3538f4a1fb0SHannes Reinecke if (tl_tpg) { 3548f4a1fb0SHannes Reinecke tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE; 3558f4a1fb0SHannes Reinecke return SUCCESS; 3568f4a1fb0SHannes Reinecke } 3578f4a1fb0SHannes Reinecke return FAILED; 3588f4a1fb0SHannes Reinecke } 3598f4a1fb0SHannes Reinecke 3603703b2c5SNicholas Bellinger static int tcm_loop_slave_alloc(struct scsi_device *sd) 3613703b2c5SNicholas Bellinger { 3623703b2c5SNicholas Bellinger set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags); 3633703b2c5SNicholas Bellinger return 0; 3643703b2c5SNicholas Bellinger } 3653703b2c5SNicholas Bellinger 3663703b2c5SNicholas Bellinger static struct scsi_host_template tcm_loop_driver_template = { 3678946b077SAl Viro .show_info = tcm_loop_show_info, 3683703b2c5SNicholas Bellinger .proc_name = "tcm_loopback", 3693703b2c5SNicholas Bellinger .name = "TCM_Loopback", 3703703b2c5SNicholas Bellinger .queuecommand = tcm_loop_queuecommand, 371db5ed4dfSChristoph Hellwig .change_queue_depth = scsi_change_queue_depth, 372969871cdSHannes Reinecke .eh_abort_handler = tcm_loop_abort_task, 3733703b2c5SNicholas Bellinger .eh_device_reset_handler = tcm_loop_device_reset, 3748f4a1fb0SHannes Reinecke .eh_target_reset_handler = tcm_loop_target_reset, 3752e88efd3SChristoph Hellwig .can_queue = 1024, 3763703b2c5SNicholas Bellinger .this_id = -1, 3772e88efd3SChristoph Hellwig .sg_tablesize = 256, 3782e88efd3SChristoph Hellwig .cmd_per_lun = 1024, 3792e88efd3SChristoph Hellwig .max_sectors = 0xFFFF, 3803703b2c5SNicholas Bellinger .use_clustering = DISABLE_CLUSTERING, 3813703b2c5SNicholas Bellinger .slave_alloc = tcm_loop_slave_alloc, 3823703b2c5SNicholas Bellinger .module = THIS_MODULE, 3832ecb204dSChristoph Hellwig .use_blk_tags = 1, 384c40ecc12SChristoph Hellwig .track_queue_depth = 1, 3853703b2c5SNicholas Bellinger }; 3863703b2c5SNicholas Bellinger 3873703b2c5SNicholas Bellinger static int tcm_loop_driver_probe(struct device *dev) 3883703b2c5SNicholas Bellinger { 3893703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 3903703b2c5SNicholas Bellinger struct Scsi_Host *sh; 39159dedde2SNicholas Bellinger int error, host_prot; 3923703b2c5SNicholas Bellinger 3933703b2c5SNicholas Bellinger tl_hba = to_tcm_loop_hba(dev); 3943703b2c5SNicholas Bellinger 3953703b2c5SNicholas Bellinger sh = scsi_host_alloc(&tcm_loop_driver_template, 3963703b2c5SNicholas Bellinger sizeof(struct tcm_loop_hba)); 3973703b2c5SNicholas Bellinger if (!sh) { 3986708bb27SAndy Grover pr_err("Unable to allocate struct scsi_host\n"); 3993703b2c5SNicholas Bellinger return -ENODEV; 4003703b2c5SNicholas Bellinger } 4013703b2c5SNicholas Bellinger tl_hba->sh = sh; 4023703b2c5SNicholas Bellinger 4033703b2c5SNicholas Bellinger /* 4043703b2c5SNicholas Bellinger * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata 4053703b2c5SNicholas Bellinger */ 4063703b2c5SNicholas Bellinger *((struct tcm_loop_hba **)sh->hostdata) = tl_hba; 4073703b2c5SNicholas Bellinger /* 4083703b2c5SNicholas Bellinger * Setup single ID, Channel and LUN for now.. 4093703b2c5SNicholas Bellinger */ 4103703b2c5SNicholas Bellinger sh->max_id = 2; 4113703b2c5SNicholas Bellinger sh->max_lun = 0; 4123703b2c5SNicholas Bellinger sh->max_channel = 0; 4139736f4adSIlias Tsitsimpis sh->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE; 4143703b2c5SNicholas Bellinger 41559dedde2SNicholas Bellinger host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION | 41659dedde2SNicholas Bellinger SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION | 41759dedde2SNicholas Bellinger SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION; 41859dedde2SNicholas Bellinger 41959dedde2SNicholas Bellinger scsi_host_set_prot(sh, host_prot); 42059dedde2SNicholas Bellinger scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC); 42159dedde2SNicholas Bellinger 4223703b2c5SNicholas Bellinger error = scsi_add_host(sh, &tl_hba->dev); 4233703b2c5SNicholas Bellinger if (error) { 4246708bb27SAndy Grover pr_err("%s: scsi_add_host failed\n", __func__); 4253703b2c5SNicholas Bellinger scsi_host_put(sh); 4263703b2c5SNicholas Bellinger return -ENODEV; 4273703b2c5SNicholas Bellinger } 4283703b2c5SNicholas Bellinger return 0; 4293703b2c5SNicholas Bellinger } 4303703b2c5SNicholas Bellinger 4313703b2c5SNicholas Bellinger static int tcm_loop_driver_remove(struct device *dev) 4323703b2c5SNicholas Bellinger { 4333703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 4343703b2c5SNicholas Bellinger struct Scsi_Host *sh; 4353703b2c5SNicholas Bellinger 4363703b2c5SNicholas Bellinger tl_hba = to_tcm_loop_hba(dev); 4373703b2c5SNicholas Bellinger sh = tl_hba->sh; 4383703b2c5SNicholas Bellinger 4393703b2c5SNicholas Bellinger scsi_remove_host(sh); 4403703b2c5SNicholas Bellinger scsi_host_put(sh); 4413703b2c5SNicholas Bellinger return 0; 4423703b2c5SNicholas Bellinger } 4433703b2c5SNicholas Bellinger 4443703b2c5SNicholas Bellinger static void tcm_loop_release_adapter(struct device *dev) 4453703b2c5SNicholas Bellinger { 4463703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev); 4473703b2c5SNicholas Bellinger 4483703b2c5SNicholas Bellinger kfree(tl_hba); 4493703b2c5SNicholas Bellinger } 4503703b2c5SNicholas Bellinger 4513703b2c5SNicholas Bellinger /* 4523703b2c5SNicholas Bellinger * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c 4533703b2c5SNicholas Bellinger */ 4543703b2c5SNicholas Bellinger static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id) 4553703b2c5SNicholas Bellinger { 4563703b2c5SNicholas Bellinger int ret; 4573703b2c5SNicholas Bellinger 4583703b2c5SNicholas Bellinger tl_hba->dev.bus = &tcm_loop_lld_bus; 4593703b2c5SNicholas Bellinger tl_hba->dev.parent = tcm_loop_primary; 4603703b2c5SNicholas Bellinger tl_hba->dev.release = &tcm_loop_release_adapter; 4613703b2c5SNicholas Bellinger dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id); 4623703b2c5SNicholas Bellinger 4633703b2c5SNicholas Bellinger ret = device_register(&tl_hba->dev); 4643703b2c5SNicholas Bellinger if (ret) { 4656708bb27SAndy Grover pr_err("device_register() failed for" 4663703b2c5SNicholas Bellinger " tl_hba->dev: %d\n", ret); 4673703b2c5SNicholas Bellinger return -ENODEV; 4683703b2c5SNicholas Bellinger } 4693703b2c5SNicholas Bellinger 4703703b2c5SNicholas Bellinger return 0; 4713703b2c5SNicholas Bellinger } 4723703b2c5SNicholas Bellinger 4733703b2c5SNicholas Bellinger /* 4743703b2c5SNicholas Bellinger * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated 4753703b2c5SNicholas Bellinger * tcm_loop SCSI bus. 4763703b2c5SNicholas Bellinger */ 4773703b2c5SNicholas Bellinger static int tcm_loop_alloc_core_bus(void) 4783703b2c5SNicholas Bellinger { 4793703b2c5SNicholas Bellinger int ret; 4803703b2c5SNicholas Bellinger 4813703b2c5SNicholas Bellinger tcm_loop_primary = root_device_register("tcm_loop_0"); 4823703b2c5SNicholas Bellinger if (IS_ERR(tcm_loop_primary)) { 4836708bb27SAndy Grover pr_err("Unable to allocate tcm_loop_primary\n"); 4843703b2c5SNicholas Bellinger return PTR_ERR(tcm_loop_primary); 4853703b2c5SNicholas Bellinger } 4863703b2c5SNicholas Bellinger 4873703b2c5SNicholas Bellinger ret = bus_register(&tcm_loop_lld_bus); 4883703b2c5SNicholas Bellinger if (ret) { 4896708bb27SAndy Grover pr_err("bus_register() failed for tcm_loop_lld_bus\n"); 4903703b2c5SNicholas Bellinger goto dev_unreg; 4913703b2c5SNicholas Bellinger } 4923703b2c5SNicholas Bellinger 4933703b2c5SNicholas Bellinger ret = driver_register(&tcm_loop_driverfs); 4943703b2c5SNicholas Bellinger if (ret) { 4956708bb27SAndy Grover pr_err("driver_register() failed for" 4963703b2c5SNicholas Bellinger "tcm_loop_driverfs\n"); 4973703b2c5SNicholas Bellinger goto bus_unreg; 4983703b2c5SNicholas Bellinger } 4993703b2c5SNicholas Bellinger 5006708bb27SAndy Grover pr_debug("Initialized TCM Loop Core Bus\n"); 5013703b2c5SNicholas Bellinger return ret; 5023703b2c5SNicholas Bellinger 5033703b2c5SNicholas Bellinger bus_unreg: 5043703b2c5SNicholas Bellinger bus_unregister(&tcm_loop_lld_bus); 5053703b2c5SNicholas Bellinger dev_unreg: 5063703b2c5SNicholas Bellinger root_device_unregister(tcm_loop_primary); 5073703b2c5SNicholas Bellinger return ret; 5083703b2c5SNicholas Bellinger } 5093703b2c5SNicholas Bellinger 5103703b2c5SNicholas Bellinger static void tcm_loop_release_core_bus(void) 5113703b2c5SNicholas Bellinger { 5123703b2c5SNicholas Bellinger driver_unregister(&tcm_loop_driverfs); 5133703b2c5SNicholas Bellinger bus_unregister(&tcm_loop_lld_bus); 5143703b2c5SNicholas Bellinger root_device_unregister(tcm_loop_primary); 5153703b2c5SNicholas Bellinger 5166708bb27SAndy Grover pr_debug("Releasing TCM Loop Core BUS\n"); 5173703b2c5SNicholas Bellinger } 5183703b2c5SNicholas Bellinger 5193703b2c5SNicholas Bellinger static char *tcm_loop_get_fabric_name(void) 5203703b2c5SNicholas Bellinger { 5213703b2c5SNicholas Bellinger return "loopback"; 5223703b2c5SNicholas Bellinger } 5233703b2c5SNicholas Bellinger 5241667a459SChristoph Hellwig static inline struct tcm_loop_tpg *tl_tpg(struct se_portal_group *se_tpg) 5251667a459SChristoph Hellwig { 5261667a459SChristoph Hellwig return container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg); 5271667a459SChristoph Hellwig } 5281667a459SChristoph Hellwig 5293703b2c5SNicholas Bellinger static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg) 5303703b2c5SNicholas Bellinger { 5313703b2c5SNicholas Bellinger /* 5323703b2c5SNicholas Bellinger * Return the passed NAA identifier for the SAS Target Port 5333703b2c5SNicholas Bellinger */ 5341667a459SChristoph Hellwig return &tl_tpg(se_tpg)->tl_hba->tl_wwn_address[0]; 5353703b2c5SNicholas Bellinger } 5363703b2c5SNicholas Bellinger 5373703b2c5SNicholas Bellinger static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg) 5383703b2c5SNicholas Bellinger { 5393703b2c5SNicholas Bellinger /* 5403703b2c5SNicholas Bellinger * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83 5413703b2c5SNicholas Bellinger * to represent the SCSI Target Port. 5423703b2c5SNicholas Bellinger */ 5431667a459SChristoph Hellwig return tl_tpg(se_tpg)->tl_tpgt; 5443703b2c5SNicholas Bellinger } 5453703b2c5SNicholas Bellinger 5463703b2c5SNicholas Bellinger /* 5473703b2c5SNicholas Bellinger * Returning (1) here allows for target_core_mod struct se_node_acl to be generated 5483703b2c5SNicholas Bellinger * based upon the incoming fabric dependent SCSI Initiator Port 5493703b2c5SNicholas Bellinger */ 5503703b2c5SNicholas Bellinger static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg) 5513703b2c5SNicholas Bellinger { 5523703b2c5SNicholas Bellinger return 1; 5533703b2c5SNicholas Bellinger } 5543703b2c5SNicholas Bellinger 5553703b2c5SNicholas Bellinger static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg) 5563703b2c5SNicholas Bellinger { 5573703b2c5SNicholas Bellinger return 0; 5583703b2c5SNicholas Bellinger } 5593703b2c5SNicholas Bellinger 5603703b2c5SNicholas Bellinger /* 5613703b2c5SNicholas Bellinger * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for 5623703b2c5SNicholas Bellinger * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest 5633703b2c5SNicholas Bellinger */ 5643703b2c5SNicholas Bellinger static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg) 5653703b2c5SNicholas Bellinger { 5663703b2c5SNicholas Bellinger return 0; 5673703b2c5SNicholas Bellinger } 5683703b2c5SNicholas Bellinger 5693703b2c5SNicholas Bellinger /* 5703703b2c5SNicholas Bellinger * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will 5713703b2c5SNicholas Bellinger * never be called for TCM_Loop by target_core_fabric_configfs.c code. 5723703b2c5SNicholas Bellinger * It has been added here as a nop for target_fabric_tf_ops_check() 5733703b2c5SNicholas Bellinger */ 5743703b2c5SNicholas Bellinger static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg) 5753703b2c5SNicholas Bellinger { 5763703b2c5SNicholas Bellinger return 0; 5773703b2c5SNicholas Bellinger } 5783703b2c5SNicholas Bellinger 579436f4a0aSNicholas Bellinger static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg) 580436f4a0aSNicholas Bellinger { 581436f4a0aSNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, 582436f4a0aSNicholas Bellinger tl_se_tpg); 583436f4a0aSNicholas Bellinger return tl_tpg->tl_fabric_prot_type; 584436f4a0aSNicholas Bellinger } 585436f4a0aSNicholas Bellinger 5863703b2c5SNicholas Bellinger static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg) 5873703b2c5SNicholas Bellinger { 5883703b2c5SNicholas Bellinger return 1; 5893703b2c5SNicholas Bellinger } 5903703b2c5SNicholas Bellinger 5913703b2c5SNicholas Bellinger static u32 tcm_loop_sess_get_index(struct se_session *se_sess) 5923703b2c5SNicholas Bellinger { 5933703b2c5SNicholas Bellinger return 1; 5943703b2c5SNicholas Bellinger } 5953703b2c5SNicholas Bellinger 5963703b2c5SNicholas Bellinger static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl) 5973703b2c5SNicholas Bellinger { 5983703b2c5SNicholas Bellinger return; 5993703b2c5SNicholas Bellinger } 6003703b2c5SNicholas Bellinger 6013703b2c5SNicholas Bellinger static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd) 6023703b2c5SNicholas Bellinger { 6033703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 6043703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd); 6053703b2c5SNicholas Bellinger 6063703b2c5SNicholas Bellinger return tl_cmd->sc_cmd_state; 6073703b2c5SNicholas Bellinger } 6083703b2c5SNicholas Bellinger 6093703b2c5SNicholas Bellinger static int tcm_loop_shutdown_session(struct se_session *se_sess) 6103703b2c5SNicholas Bellinger { 6113703b2c5SNicholas Bellinger return 0; 6123703b2c5SNicholas Bellinger } 6133703b2c5SNicholas Bellinger 6143703b2c5SNicholas Bellinger static void tcm_loop_close_session(struct se_session *se_sess) 6153703b2c5SNicholas Bellinger { 6163703b2c5SNicholas Bellinger return; 6173703b2c5SNicholas Bellinger }; 6183703b2c5SNicholas Bellinger 6193703b2c5SNicholas Bellinger static int tcm_loop_write_pending(struct se_cmd *se_cmd) 6203703b2c5SNicholas Bellinger { 6213703b2c5SNicholas Bellinger /* 6223703b2c5SNicholas Bellinger * Since Linux/SCSI has already sent down a struct scsi_cmnd 6233703b2c5SNicholas Bellinger * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array 6243703b2c5SNicholas Bellinger * memory, and memory has already been mapped to struct se_cmd->t_mem_list 6253703b2c5SNicholas Bellinger * format with transport_generic_map_mem_to_cmd(). 6263703b2c5SNicholas Bellinger * 6273703b2c5SNicholas Bellinger * We now tell TCM to add this WRITE CDB directly into the TCM storage 6283703b2c5SNicholas Bellinger * object execution queue. 6293703b2c5SNicholas Bellinger */ 63070baf0abSChristoph Hellwig target_execute_cmd(se_cmd); 6313703b2c5SNicholas Bellinger return 0; 6323703b2c5SNicholas Bellinger } 6333703b2c5SNicholas Bellinger 6343703b2c5SNicholas Bellinger static int tcm_loop_write_pending_status(struct se_cmd *se_cmd) 6353703b2c5SNicholas Bellinger { 6363703b2c5SNicholas Bellinger return 0; 6373703b2c5SNicholas Bellinger } 6383703b2c5SNicholas Bellinger 6393703b2c5SNicholas Bellinger static int tcm_loop_queue_data_in(struct se_cmd *se_cmd) 6403703b2c5SNicholas Bellinger { 6413703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 6423703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd); 6433703b2c5SNicholas Bellinger struct scsi_cmnd *sc = tl_cmd->sc; 6443703b2c5SNicholas Bellinger 6456708bb27SAndy Grover pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p" 6463703b2c5SNicholas Bellinger " cdb: 0x%02x\n", sc, sc->cmnd[0]); 6473703b2c5SNicholas Bellinger 6483703b2c5SNicholas Bellinger sc->result = SAM_STAT_GOOD; 6493703b2c5SNicholas Bellinger set_host_byte(sc, DID_OK); 6506cf3fa69SRoland Dreier if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) || 6516cf3fa69SRoland Dreier (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT)) 6526cf3fa69SRoland Dreier scsi_set_resid(sc, se_cmd->residual_count); 6533703b2c5SNicholas Bellinger sc->scsi_done(sc); 6543703b2c5SNicholas Bellinger return 0; 6553703b2c5SNicholas Bellinger } 6563703b2c5SNicholas Bellinger 6573703b2c5SNicholas Bellinger static int tcm_loop_queue_status(struct se_cmd *se_cmd) 6583703b2c5SNicholas Bellinger { 6593703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 6603703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd); 6613703b2c5SNicholas Bellinger struct scsi_cmnd *sc = tl_cmd->sc; 6623703b2c5SNicholas Bellinger 6636708bb27SAndy Grover pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p" 6643703b2c5SNicholas Bellinger " cdb: 0x%02x\n", sc, sc->cmnd[0]); 6653703b2c5SNicholas Bellinger 6663703b2c5SNicholas Bellinger if (se_cmd->sense_buffer && 6673703b2c5SNicholas Bellinger ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || 6683703b2c5SNicholas Bellinger (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) { 6693703b2c5SNicholas Bellinger 6705951146dSAndy Grover memcpy(sc->sense_buffer, se_cmd->sense_buffer, 6713703b2c5SNicholas Bellinger SCSI_SENSE_BUFFERSIZE); 6723703b2c5SNicholas Bellinger sc->result = SAM_STAT_CHECK_CONDITION; 6733703b2c5SNicholas Bellinger set_driver_byte(sc, DRIVER_SENSE); 6743703b2c5SNicholas Bellinger } else 6753703b2c5SNicholas Bellinger sc->result = se_cmd->scsi_status; 6763703b2c5SNicholas Bellinger 6773703b2c5SNicholas Bellinger set_host_byte(sc, DID_OK); 6786cf3fa69SRoland Dreier if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) || 6796cf3fa69SRoland Dreier (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT)) 6806cf3fa69SRoland Dreier scsi_set_resid(sc, se_cmd->residual_count); 6813703b2c5SNicholas Bellinger sc->scsi_done(sc); 6823703b2c5SNicholas Bellinger return 0; 6833703b2c5SNicholas Bellinger } 6843703b2c5SNicholas Bellinger 685b79fafacSJoern Engel static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd) 6863703b2c5SNicholas Bellinger { 6873703b2c5SNicholas Bellinger struct se_tmr_req *se_tmr = se_cmd->se_tmr_req; 6883703b2c5SNicholas Bellinger struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr; 6893703b2c5SNicholas Bellinger /* 6903703b2c5SNicholas Bellinger * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead 6913703b2c5SNicholas Bellinger * and wake up the wait_queue_head_t in tcm_loop_device_reset() 6923703b2c5SNicholas Bellinger */ 6933703b2c5SNicholas Bellinger atomic_set(&tl_tmr->tmr_complete, 1); 6943703b2c5SNicholas Bellinger wake_up(&tl_tmr->tl_tmr_wait); 6953703b2c5SNicholas Bellinger } 6963703b2c5SNicholas Bellinger 697131e6abcSNicholas Bellinger static void tcm_loop_aborted_task(struct se_cmd *se_cmd) 698131e6abcSNicholas Bellinger { 699131e6abcSNicholas Bellinger return; 700131e6abcSNicholas Bellinger } 701131e6abcSNicholas Bellinger 7023703b2c5SNicholas Bellinger static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba) 7033703b2c5SNicholas Bellinger { 7043703b2c5SNicholas Bellinger switch (tl_hba->tl_proto_id) { 7053703b2c5SNicholas Bellinger case SCSI_PROTOCOL_SAS: 7063703b2c5SNicholas Bellinger return "SAS"; 7073703b2c5SNicholas Bellinger case SCSI_PROTOCOL_FCP: 7083703b2c5SNicholas Bellinger return "FCP"; 7093703b2c5SNicholas Bellinger case SCSI_PROTOCOL_ISCSI: 7103703b2c5SNicholas Bellinger return "iSCSI"; 7113703b2c5SNicholas Bellinger default: 7123703b2c5SNicholas Bellinger break; 7133703b2c5SNicholas Bellinger } 7143703b2c5SNicholas Bellinger 7153703b2c5SNicholas Bellinger return "Unknown"; 7163703b2c5SNicholas Bellinger } 7173703b2c5SNicholas Bellinger 7183703b2c5SNicholas Bellinger /* Start items for tcm_loop_port_cit */ 7193703b2c5SNicholas Bellinger 7203703b2c5SNicholas Bellinger static int tcm_loop_port_link( 7213703b2c5SNicholas Bellinger struct se_portal_group *se_tpg, 7223703b2c5SNicholas Bellinger struct se_lun *lun) 7233703b2c5SNicholas Bellinger { 7243703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 7253703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg); 7263703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 7273703b2c5SNicholas Bellinger 72833940d09SJoern Engel atomic_inc_mb(&tl_tpg->tl_tpg_port_count); 7293703b2c5SNicholas Bellinger /* 7303703b2c5SNicholas Bellinger * Add Linux/SCSI struct scsi_device by HCTL 7313703b2c5SNicholas Bellinger */ 7323703b2c5SNicholas Bellinger scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun); 7333703b2c5SNicholas Bellinger 7346708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n"); 7353703b2c5SNicholas Bellinger return 0; 7363703b2c5SNicholas Bellinger } 7373703b2c5SNicholas Bellinger 7383703b2c5SNicholas Bellinger static void tcm_loop_port_unlink( 7393703b2c5SNicholas Bellinger struct se_portal_group *se_tpg, 7403703b2c5SNicholas Bellinger struct se_lun *se_lun) 7413703b2c5SNicholas Bellinger { 7423703b2c5SNicholas Bellinger struct scsi_device *sd; 7433703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 7443703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg; 7453703b2c5SNicholas Bellinger 7463703b2c5SNicholas Bellinger tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg); 7473703b2c5SNicholas Bellinger tl_hba = tl_tpg->tl_hba; 7483703b2c5SNicholas Bellinger 7493703b2c5SNicholas Bellinger sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt, 7503703b2c5SNicholas Bellinger se_lun->unpacked_lun); 7513703b2c5SNicholas Bellinger if (!sd) { 7526708bb27SAndy Grover pr_err("Unable to locate struct scsi_device for %d:%d:" 7533703b2c5SNicholas Bellinger "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun); 7543703b2c5SNicholas Bellinger return; 7553703b2c5SNicholas Bellinger } 7563703b2c5SNicholas Bellinger /* 7573703b2c5SNicholas Bellinger * Remove Linux/SCSI struct scsi_device by HCTL 7583703b2c5SNicholas Bellinger */ 7593703b2c5SNicholas Bellinger scsi_remove_device(sd); 7603703b2c5SNicholas Bellinger scsi_device_put(sd); 7613703b2c5SNicholas Bellinger 76233940d09SJoern Engel atomic_dec_mb(&tl_tpg->tl_tpg_port_count); 7633703b2c5SNicholas Bellinger 7646708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n"); 7653703b2c5SNicholas Bellinger } 7663703b2c5SNicholas Bellinger 7673703b2c5SNicholas Bellinger /* End items for tcm_loop_port_cit */ 7683703b2c5SNicholas Bellinger 769436f4a0aSNicholas Bellinger static ssize_t tcm_loop_tpg_attrib_show_fabric_prot_type( 770436f4a0aSNicholas Bellinger struct se_portal_group *se_tpg, 771436f4a0aSNicholas Bellinger char *page) 772436f4a0aSNicholas Bellinger { 773436f4a0aSNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, 774436f4a0aSNicholas Bellinger tl_se_tpg); 775436f4a0aSNicholas Bellinger 776436f4a0aSNicholas Bellinger return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type); 777436f4a0aSNicholas Bellinger } 778436f4a0aSNicholas Bellinger 779436f4a0aSNicholas Bellinger static ssize_t tcm_loop_tpg_attrib_store_fabric_prot_type( 780436f4a0aSNicholas Bellinger struct se_portal_group *se_tpg, 781436f4a0aSNicholas Bellinger const char *page, 782436f4a0aSNicholas Bellinger size_t count) 783436f4a0aSNicholas Bellinger { 784436f4a0aSNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, 785436f4a0aSNicholas Bellinger tl_se_tpg); 786436f4a0aSNicholas Bellinger unsigned long val; 787436f4a0aSNicholas Bellinger int ret = kstrtoul(page, 0, &val); 788436f4a0aSNicholas Bellinger 789436f4a0aSNicholas Bellinger if (ret) { 790436f4a0aSNicholas Bellinger pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret); 791436f4a0aSNicholas Bellinger return ret; 792436f4a0aSNicholas Bellinger } 793436f4a0aSNicholas Bellinger if (val != 0 && val != 1 && val != 3) { 794436f4a0aSNicholas Bellinger pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val); 795436f4a0aSNicholas Bellinger return -EINVAL; 796436f4a0aSNicholas Bellinger } 797436f4a0aSNicholas Bellinger tl_tpg->tl_fabric_prot_type = val; 798436f4a0aSNicholas Bellinger 799436f4a0aSNicholas Bellinger return count; 800436f4a0aSNicholas Bellinger } 801436f4a0aSNicholas Bellinger 802436f4a0aSNicholas Bellinger TF_TPG_ATTRIB_ATTR(tcm_loop, fabric_prot_type, S_IRUGO | S_IWUSR); 803436f4a0aSNicholas Bellinger 804436f4a0aSNicholas Bellinger static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = { 805436f4a0aSNicholas Bellinger &tcm_loop_tpg_attrib_fabric_prot_type.attr, 806436f4a0aSNicholas Bellinger NULL, 807436f4a0aSNicholas Bellinger }; 808436f4a0aSNicholas Bellinger 8093703b2c5SNicholas Bellinger /* Start items for tcm_loop_nexus_cit */ 8103703b2c5SNicholas Bellinger 8113703b2c5SNicholas Bellinger static int tcm_loop_make_nexus( 8123703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg, 8133703b2c5SNicholas Bellinger const char *name) 8143703b2c5SNicholas Bellinger { 8153703b2c5SNicholas Bellinger struct se_portal_group *se_tpg; 8163703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 8173703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus; 818552523dcSDan Carpenter int ret = -ENOMEM; 8193703b2c5SNicholas Bellinger 820506787a2SHannes Reinecke if (tl_tpg->tl_nexus) { 821506787a2SHannes Reinecke pr_debug("tl_tpg->tl_nexus already exists\n"); 8223703b2c5SNicholas Bellinger return -EEXIST; 8233703b2c5SNicholas Bellinger } 8243703b2c5SNicholas Bellinger se_tpg = &tl_tpg->tl_se_tpg; 8253703b2c5SNicholas Bellinger 8263703b2c5SNicholas Bellinger tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL); 8273703b2c5SNicholas Bellinger if (!tl_nexus) { 8286708bb27SAndy Grover pr_err("Unable to allocate struct tcm_loop_nexus\n"); 8293703b2c5SNicholas Bellinger return -ENOMEM; 8303703b2c5SNicholas Bellinger } 8313703b2c5SNicholas Bellinger /* 8323703b2c5SNicholas Bellinger * Initialize the struct se_session pointer 8333703b2c5SNicholas Bellinger */ 834436f4a0aSNicholas Bellinger tl_nexus->se_sess = transport_init_session( 835436f4a0aSNicholas Bellinger TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS); 836552523dcSDan Carpenter if (IS_ERR(tl_nexus->se_sess)) { 837552523dcSDan Carpenter ret = PTR_ERR(tl_nexus->se_sess); 8383703b2c5SNicholas Bellinger goto out; 839552523dcSDan Carpenter } 8403703b2c5SNicholas Bellinger /* 8413703b2c5SNicholas Bellinger * Since we are running in 'demo mode' this call with generate a 8423703b2c5SNicholas Bellinger * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI 8433703b2c5SNicholas Bellinger * Initiator port name of the passed configfs group 'name'. 8443703b2c5SNicholas Bellinger */ 8453703b2c5SNicholas Bellinger tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl( 8463703b2c5SNicholas Bellinger se_tpg, (unsigned char *)name); 8473703b2c5SNicholas Bellinger if (!tl_nexus->se_sess->se_node_acl) { 8483703b2c5SNicholas Bellinger transport_free_session(tl_nexus->se_sess); 8493703b2c5SNicholas Bellinger goto out; 8503703b2c5SNicholas Bellinger } 8512f450cc1SBart Van Assche /* Now, register the SAS I_T Nexus as active. */ 8522f450cc1SBart Van Assche transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl, 8535951146dSAndy Grover tl_nexus->se_sess, tl_nexus); 854506787a2SHannes Reinecke tl_tpg->tl_nexus = tl_nexus; 8556708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated" 8563703b2c5SNicholas Bellinger " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba), 8573703b2c5SNicholas Bellinger name); 8583703b2c5SNicholas Bellinger return 0; 8593703b2c5SNicholas Bellinger 8603703b2c5SNicholas Bellinger out: 8613703b2c5SNicholas Bellinger kfree(tl_nexus); 862552523dcSDan Carpenter return ret; 8633703b2c5SNicholas Bellinger } 8643703b2c5SNicholas Bellinger 8653703b2c5SNicholas Bellinger static int tcm_loop_drop_nexus( 8663703b2c5SNicholas Bellinger struct tcm_loop_tpg *tpg) 8673703b2c5SNicholas Bellinger { 8683703b2c5SNicholas Bellinger struct se_session *se_sess; 8693703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus; 8703703b2c5SNicholas Bellinger 871506787a2SHannes Reinecke tl_nexus = tpg->tl_nexus; 8723703b2c5SNicholas Bellinger if (!tl_nexus) 8733703b2c5SNicholas Bellinger return -ENODEV; 8743703b2c5SNicholas Bellinger 8753703b2c5SNicholas Bellinger se_sess = tl_nexus->se_sess; 8763703b2c5SNicholas Bellinger if (!se_sess) 8773703b2c5SNicholas Bellinger return -ENODEV; 8783703b2c5SNicholas Bellinger 8793703b2c5SNicholas Bellinger if (atomic_read(&tpg->tl_tpg_port_count)) { 8806708bb27SAndy Grover pr_err("Unable to remove TCM_Loop I_T Nexus with" 8813703b2c5SNicholas Bellinger " active TPG port count: %d\n", 8823703b2c5SNicholas Bellinger atomic_read(&tpg->tl_tpg_port_count)); 8833703b2c5SNicholas Bellinger return -EPERM; 8843703b2c5SNicholas Bellinger } 8853703b2c5SNicholas Bellinger 8866708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated" 887506787a2SHannes Reinecke " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tpg->tl_hba), 8883703b2c5SNicholas Bellinger tl_nexus->se_sess->se_node_acl->initiatorname); 8893703b2c5SNicholas Bellinger /* 8903703b2c5SNicholas Bellinger * Release the SCSI I_T Nexus to the emulated SAS Target Port 8913703b2c5SNicholas Bellinger */ 8923703b2c5SNicholas Bellinger transport_deregister_session(tl_nexus->se_sess); 893506787a2SHannes Reinecke tpg->tl_nexus = NULL; 8943703b2c5SNicholas Bellinger kfree(tl_nexus); 8953703b2c5SNicholas Bellinger return 0; 8963703b2c5SNicholas Bellinger } 8973703b2c5SNicholas Bellinger 8983703b2c5SNicholas Bellinger /* End items for tcm_loop_nexus_cit */ 8993703b2c5SNicholas Bellinger 9003703b2c5SNicholas Bellinger static ssize_t tcm_loop_tpg_show_nexus( 9013703b2c5SNicholas Bellinger struct se_portal_group *se_tpg, 9023703b2c5SNicholas Bellinger char *page) 9033703b2c5SNicholas Bellinger { 9043703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 9053703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg); 9063703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus; 9073703b2c5SNicholas Bellinger ssize_t ret; 9083703b2c5SNicholas Bellinger 909506787a2SHannes Reinecke tl_nexus = tl_tpg->tl_nexus; 9103703b2c5SNicholas Bellinger if (!tl_nexus) 9113703b2c5SNicholas Bellinger return -ENODEV; 9123703b2c5SNicholas Bellinger 9133703b2c5SNicholas Bellinger ret = snprintf(page, PAGE_SIZE, "%s\n", 9143703b2c5SNicholas Bellinger tl_nexus->se_sess->se_node_acl->initiatorname); 9153703b2c5SNicholas Bellinger 9163703b2c5SNicholas Bellinger return ret; 9173703b2c5SNicholas Bellinger } 9183703b2c5SNicholas Bellinger 9193703b2c5SNicholas Bellinger static ssize_t tcm_loop_tpg_store_nexus( 9203703b2c5SNicholas Bellinger struct se_portal_group *se_tpg, 9213703b2c5SNicholas Bellinger const char *page, 9223703b2c5SNicholas Bellinger size_t count) 9233703b2c5SNicholas Bellinger { 9243703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 9253703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg); 9263703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 9273703b2c5SNicholas Bellinger unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr; 9283703b2c5SNicholas Bellinger int ret; 9293703b2c5SNicholas Bellinger /* 9303703b2c5SNicholas Bellinger * Shutdown the active I_T nexus if 'NULL' is passed.. 9313703b2c5SNicholas Bellinger */ 9323703b2c5SNicholas Bellinger if (!strncmp(page, "NULL", 4)) { 9333703b2c5SNicholas Bellinger ret = tcm_loop_drop_nexus(tl_tpg); 9343703b2c5SNicholas Bellinger return (!ret) ? count : ret; 9353703b2c5SNicholas Bellinger } 9363703b2c5SNicholas Bellinger /* 9373703b2c5SNicholas Bellinger * Otherwise make sure the passed virtual Initiator port WWN matches 9383703b2c5SNicholas Bellinger * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call 9393703b2c5SNicholas Bellinger * tcm_loop_make_nexus() 9403703b2c5SNicholas Bellinger */ 94160d645a4SDan Carpenter if (strlen(page) >= TL_WWN_ADDR_LEN) { 9426708bb27SAndy Grover pr_err("Emulated NAA Sas Address: %s, exceeds" 9433703b2c5SNicholas Bellinger " max: %d\n", page, TL_WWN_ADDR_LEN); 9443703b2c5SNicholas Bellinger return -EINVAL; 9453703b2c5SNicholas Bellinger } 9463703b2c5SNicholas Bellinger snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page); 9473703b2c5SNicholas Bellinger 9483703b2c5SNicholas Bellinger ptr = strstr(i_port, "naa."); 9493703b2c5SNicholas Bellinger if (ptr) { 9503703b2c5SNicholas Bellinger if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) { 9516708bb27SAndy Grover pr_err("Passed SAS Initiator Port %s does not" 9523703b2c5SNicholas Bellinger " match target port protoid: %s\n", i_port, 9533703b2c5SNicholas Bellinger tcm_loop_dump_proto_id(tl_hba)); 9543703b2c5SNicholas Bellinger return -EINVAL; 9553703b2c5SNicholas Bellinger } 9563703b2c5SNicholas Bellinger port_ptr = &i_port[0]; 9573703b2c5SNicholas Bellinger goto check_newline; 9583703b2c5SNicholas Bellinger } 9593703b2c5SNicholas Bellinger ptr = strstr(i_port, "fc."); 9603703b2c5SNicholas Bellinger if (ptr) { 9613703b2c5SNicholas Bellinger if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) { 9626708bb27SAndy Grover pr_err("Passed FCP Initiator Port %s does not" 9633703b2c5SNicholas Bellinger " match target port protoid: %s\n", i_port, 9643703b2c5SNicholas Bellinger tcm_loop_dump_proto_id(tl_hba)); 9653703b2c5SNicholas Bellinger return -EINVAL; 9663703b2c5SNicholas Bellinger } 9673703b2c5SNicholas Bellinger port_ptr = &i_port[3]; /* Skip over "fc." */ 9683703b2c5SNicholas Bellinger goto check_newline; 9693703b2c5SNicholas Bellinger } 9703703b2c5SNicholas Bellinger ptr = strstr(i_port, "iqn."); 9713703b2c5SNicholas Bellinger if (ptr) { 9723703b2c5SNicholas Bellinger if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) { 9736708bb27SAndy Grover pr_err("Passed iSCSI Initiator Port %s does not" 9743703b2c5SNicholas Bellinger " match target port protoid: %s\n", i_port, 9753703b2c5SNicholas Bellinger tcm_loop_dump_proto_id(tl_hba)); 9763703b2c5SNicholas Bellinger return -EINVAL; 9773703b2c5SNicholas Bellinger } 9783703b2c5SNicholas Bellinger port_ptr = &i_port[0]; 9793703b2c5SNicholas Bellinger goto check_newline; 9803703b2c5SNicholas Bellinger } 9816708bb27SAndy Grover pr_err("Unable to locate prefix for emulated Initiator Port:" 9823703b2c5SNicholas Bellinger " %s\n", i_port); 9833703b2c5SNicholas Bellinger return -EINVAL; 9843703b2c5SNicholas Bellinger /* 9853703b2c5SNicholas Bellinger * Clear any trailing newline for the NAA WWN 9863703b2c5SNicholas Bellinger */ 9873703b2c5SNicholas Bellinger check_newline: 9883703b2c5SNicholas Bellinger if (i_port[strlen(i_port)-1] == '\n') 9893703b2c5SNicholas Bellinger i_port[strlen(i_port)-1] = '\0'; 9903703b2c5SNicholas Bellinger 9913703b2c5SNicholas Bellinger ret = tcm_loop_make_nexus(tl_tpg, port_ptr); 9923703b2c5SNicholas Bellinger if (ret < 0) 9933703b2c5SNicholas Bellinger return ret; 9943703b2c5SNicholas Bellinger 9953703b2c5SNicholas Bellinger return count; 9963703b2c5SNicholas Bellinger } 9973703b2c5SNicholas Bellinger 9983703b2c5SNicholas Bellinger TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR); 9993703b2c5SNicholas Bellinger 1000fb2b2844SHannes Reinecke static ssize_t tcm_loop_tpg_show_transport_status( 1001fb2b2844SHannes Reinecke struct se_portal_group *se_tpg, 1002fb2b2844SHannes Reinecke char *page) 1003fb2b2844SHannes Reinecke { 1004fb2b2844SHannes Reinecke struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 1005fb2b2844SHannes Reinecke struct tcm_loop_tpg, tl_se_tpg); 1006fb2b2844SHannes Reinecke const char *status = NULL; 1007fb2b2844SHannes Reinecke ssize_t ret = -EINVAL; 1008fb2b2844SHannes Reinecke 1009fb2b2844SHannes Reinecke switch (tl_tpg->tl_transport_status) { 1010fb2b2844SHannes Reinecke case TCM_TRANSPORT_ONLINE: 1011fb2b2844SHannes Reinecke status = "online"; 1012fb2b2844SHannes Reinecke break; 1013fb2b2844SHannes Reinecke case TCM_TRANSPORT_OFFLINE: 1014fb2b2844SHannes Reinecke status = "offline"; 1015fb2b2844SHannes Reinecke break; 1016fb2b2844SHannes Reinecke default: 1017fb2b2844SHannes Reinecke break; 1018fb2b2844SHannes Reinecke } 1019fb2b2844SHannes Reinecke 1020fb2b2844SHannes Reinecke if (status) 1021fb2b2844SHannes Reinecke ret = snprintf(page, PAGE_SIZE, "%s\n", status); 1022fb2b2844SHannes Reinecke 1023fb2b2844SHannes Reinecke return ret; 1024fb2b2844SHannes Reinecke } 1025fb2b2844SHannes Reinecke 1026fb2b2844SHannes Reinecke static ssize_t tcm_loop_tpg_store_transport_status( 1027fb2b2844SHannes Reinecke struct se_portal_group *se_tpg, 1028fb2b2844SHannes Reinecke const char *page, 1029fb2b2844SHannes Reinecke size_t count) 1030fb2b2844SHannes Reinecke { 1031fb2b2844SHannes Reinecke struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 1032fb2b2844SHannes Reinecke struct tcm_loop_tpg, tl_se_tpg); 1033fb2b2844SHannes Reinecke 1034fb2b2844SHannes Reinecke if (!strncmp(page, "online", 6)) { 1035fb2b2844SHannes Reinecke tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE; 1036fb2b2844SHannes Reinecke return count; 1037fb2b2844SHannes Reinecke } 1038fb2b2844SHannes Reinecke if (!strncmp(page, "offline", 7)) { 1039fb2b2844SHannes Reinecke tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE; 1040fb2b2844SHannes Reinecke return count; 1041fb2b2844SHannes Reinecke } 1042fb2b2844SHannes Reinecke return -EINVAL; 1043fb2b2844SHannes Reinecke } 1044fb2b2844SHannes Reinecke 1045fb2b2844SHannes Reinecke TF_TPG_BASE_ATTR(tcm_loop, transport_status, S_IRUGO | S_IWUSR); 1046fb2b2844SHannes Reinecke 10473703b2c5SNicholas Bellinger static struct configfs_attribute *tcm_loop_tpg_attrs[] = { 10483703b2c5SNicholas Bellinger &tcm_loop_tpg_nexus.attr, 1049fb2b2844SHannes Reinecke &tcm_loop_tpg_transport_status.attr, 10503703b2c5SNicholas Bellinger NULL, 10513703b2c5SNicholas Bellinger }; 10523703b2c5SNicholas Bellinger 10533703b2c5SNicholas Bellinger /* Start items for tcm_loop_naa_cit */ 10543703b2c5SNicholas Bellinger 1055f0a6c693SRashika Kheria static struct se_portal_group *tcm_loop_make_naa_tpg( 10563703b2c5SNicholas Bellinger struct se_wwn *wwn, 10573703b2c5SNicholas Bellinger struct config_group *group, 10583703b2c5SNicholas Bellinger const char *name) 10593703b2c5SNicholas Bellinger { 10603703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = container_of(wwn, 10613703b2c5SNicholas Bellinger struct tcm_loop_hba, tl_hba_wwn); 10623703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg; 10633703b2c5SNicholas Bellinger int ret; 10642e1cd90dSMing Lin unsigned long tpgt; 10653703b2c5SNicholas Bellinger 10662e1cd90dSMing Lin if (strstr(name, "tpgt_") != name) { 10676708bb27SAndy Grover pr_err("Unable to locate \"tpgt_#\" directory" 10683703b2c5SNicholas Bellinger " group\n"); 10693703b2c5SNicholas Bellinger return ERR_PTR(-EINVAL); 10703703b2c5SNicholas Bellinger } 10712e1cd90dSMing Lin if (kstrtoul(name+5, 10, &tpgt)) 10722e1cd90dSMing Lin return ERR_PTR(-EINVAL); 10733703b2c5SNicholas Bellinger 107412f09ccbSDan Carpenter if (tpgt >= TL_TPGS_PER_HBA) { 10752e1cd90dSMing Lin pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA:" 10763703b2c5SNicholas Bellinger " %u\n", tpgt, TL_TPGS_PER_HBA); 10773703b2c5SNicholas Bellinger return ERR_PTR(-EINVAL); 10783703b2c5SNicholas Bellinger } 10793703b2c5SNicholas Bellinger tl_tpg = &tl_hba->tl_hba_tpgs[tpgt]; 10803703b2c5SNicholas Bellinger tl_tpg->tl_hba = tl_hba; 10813703b2c5SNicholas Bellinger tl_tpg->tl_tpgt = tpgt; 10823703b2c5SNicholas Bellinger /* 10833703b2c5SNicholas Bellinger * Register the tl_tpg as a emulated SAS TCM Target Endpoint 10843703b2c5SNicholas Bellinger */ 1085e4aae5afSChristoph Hellwig ret = core_tpg_register(&loop_ops, wwn, &tl_tpg->tl_se_tpg, 1086e4aae5afSChristoph Hellwig tl_hba->tl_proto_id); 10873703b2c5SNicholas Bellinger if (ret < 0) 10883703b2c5SNicholas Bellinger return ERR_PTR(-ENOMEM); 10893703b2c5SNicholas Bellinger 10906708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s" 10912e1cd90dSMing Lin " Target Port %s,t,0x%04lx\n", tcm_loop_dump_proto_id(tl_hba), 10923703b2c5SNicholas Bellinger config_item_name(&wwn->wwn_group.cg_item), tpgt); 10933703b2c5SNicholas Bellinger 10943703b2c5SNicholas Bellinger return &tl_tpg->tl_se_tpg; 10953703b2c5SNicholas Bellinger } 10963703b2c5SNicholas Bellinger 1097f0a6c693SRashika Kheria static void tcm_loop_drop_naa_tpg( 10983703b2c5SNicholas Bellinger struct se_portal_group *se_tpg) 10993703b2c5SNicholas Bellinger { 11003703b2c5SNicholas Bellinger struct se_wwn *wwn = se_tpg->se_tpg_wwn; 11013703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 11023703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg); 11033703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 11043703b2c5SNicholas Bellinger unsigned short tpgt; 11053703b2c5SNicholas Bellinger 11063703b2c5SNicholas Bellinger tl_hba = tl_tpg->tl_hba; 11073703b2c5SNicholas Bellinger tpgt = tl_tpg->tl_tpgt; 11083703b2c5SNicholas Bellinger /* 11093703b2c5SNicholas Bellinger * Release the I_T Nexus for the Virtual SAS link if present 11103703b2c5SNicholas Bellinger */ 11113703b2c5SNicholas Bellinger tcm_loop_drop_nexus(tl_tpg); 11123703b2c5SNicholas Bellinger /* 11133703b2c5SNicholas Bellinger * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint 11143703b2c5SNicholas Bellinger */ 11153703b2c5SNicholas Bellinger core_tpg_deregister(se_tpg); 11163703b2c5SNicholas Bellinger 11170a020436SNicholas Bellinger tl_tpg->tl_hba = NULL; 11180a020436SNicholas Bellinger tl_tpg->tl_tpgt = 0; 11190a020436SNicholas Bellinger 11206708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s" 11213703b2c5SNicholas Bellinger " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba), 11223703b2c5SNicholas Bellinger config_item_name(&wwn->wwn_group.cg_item), tpgt); 11233703b2c5SNicholas Bellinger } 11243703b2c5SNicholas Bellinger 11253703b2c5SNicholas Bellinger /* End items for tcm_loop_naa_cit */ 11263703b2c5SNicholas Bellinger 11273703b2c5SNicholas Bellinger /* Start items for tcm_loop_cit */ 11283703b2c5SNicholas Bellinger 1129f0a6c693SRashika Kheria static struct se_wwn *tcm_loop_make_scsi_hba( 11303703b2c5SNicholas Bellinger struct target_fabric_configfs *tf, 11313703b2c5SNicholas Bellinger struct config_group *group, 11323703b2c5SNicholas Bellinger const char *name) 11333703b2c5SNicholas Bellinger { 11343703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 11353703b2c5SNicholas Bellinger struct Scsi_Host *sh; 11363703b2c5SNicholas Bellinger char *ptr; 11373703b2c5SNicholas Bellinger int ret, off = 0; 11383703b2c5SNicholas Bellinger 11393703b2c5SNicholas Bellinger tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL); 11403703b2c5SNicholas Bellinger if (!tl_hba) { 11416708bb27SAndy Grover pr_err("Unable to allocate struct tcm_loop_hba\n"); 11423703b2c5SNicholas Bellinger return ERR_PTR(-ENOMEM); 11433703b2c5SNicholas Bellinger } 11443703b2c5SNicholas Bellinger /* 11453703b2c5SNicholas Bellinger * Determine the emulated Protocol Identifier and Target Port Name 11463703b2c5SNicholas Bellinger * based on the incoming configfs directory name. 11473703b2c5SNicholas Bellinger */ 11483703b2c5SNicholas Bellinger ptr = strstr(name, "naa."); 11493703b2c5SNicholas Bellinger if (ptr) { 11503703b2c5SNicholas Bellinger tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS; 11513703b2c5SNicholas Bellinger goto check_len; 11523703b2c5SNicholas Bellinger } 11533703b2c5SNicholas Bellinger ptr = strstr(name, "fc."); 11543703b2c5SNicholas Bellinger if (ptr) { 11553703b2c5SNicholas Bellinger tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP; 11563703b2c5SNicholas Bellinger off = 3; /* Skip over "fc." */ 11573703b2c5SNicholas Bellinger goto check_len; 11583703b2c5SNicholas Bellinger } 11593703b2c5SNicholas Bellinger ptr = strstr(name, "iqn."); 1160a57b5d36SJesper Juhl if (!ptr) { 11616708bb27SAndy Grover pr_err("Unable to locate prefix for emulated Target " 1162a57b5d36SJesper Juhl "Port: %s\n", name); 1163a57b5d36SJesper Juhl ret = -EINVAL; 1164a57b5d36SJesper Juhl goto out; 11653703b2c5SNicholas Bellinger } 1166a57b5d36SJesper Juhl tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI; 11673703b2c5SNicholas Bellinger 11683703b2c5SNicholas Bellinger check_len: 116960d645a4SDan Carpenter if (strlen(name) >= TL_WWN_ADDR_LEN) { 11706708bb27SAndy Grover pr_err("Emulated NAA %s Address: %s, exceeds" 11713703b2c5SNicholas Bellinger " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba), 11723703b2c5SNicholas Bellinger TL_WWN_ADDR_LEN); 1173a57b5d36SJesper Juhl ret = -EINVAL; 1174a57b5d36SJesper Juhl goto out; 11753703b2c5SNicholas Bellinger } 11763703b2c5SNicholas Bellinger snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]); 11773703b2c5SNicholas Bellinger 11783703b2c5SNicholas Bellinger /* 11793703b2c5SNicholas Bellinger * Call device_register(tl_hba->dev) to register the emulated 11803703b2c5SNicholas Bellinger * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after 11813703b2c5SNicholas Bellinger * device_register() callbacks in tcm_loop_driver_probe() 11823703b2c5SNicholas Bellinger */ 11833703b2c5SNicholas Bellinger ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt); 11843703b2c5SNicholas Bellinger if (ret) 11853703b2c5SNicholas Bellinger goto out; 11863703b2c5SNicholas Bellinger 11873703b2c5SNicholas Bellinger sh = tl_hba->sh; 11883703b2c5SNicholas Bellinger tcm_loop_hba_no_cnt++; 11896708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target" 11903703b2c5SNicholas Bellinger " %s Address: %s at Linux/SCSI Host ID: %d\n", 11913703b2c5SNicholas Bellinger tcm_loop_dump_proto_id(tl_hba), name, sh->host_no); 11923703b2c5SNicholas Bellinger 11933703b2c5SNicholas Bellinger return &tl_hba->tl_hba_wwn; 11943703b2c5SNicholas Bellinger out: 11953703b2c5SNicholas Bellinger kfree(tl_hba); 11963703b2c5SNicholas Bellinger return ERR_PTR(ret); 11973703b2c5SNicholas Bellinger } 11983703b2c5SNicholas Bellinger 1199f0a6c693SRashika Kheria static void tcm_loop_drop_scsi_hba( 12003703b2c5SNicholas Bellinger struct se_wwn *wwn) 12013703b2c5SNicholas Bellinger { 12023703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = container_of(wwn, 12033703b2c5SNicholas Bellinger struct tcm_loop_hba, tl_hba_wwn); 12046297b07cSNicholas Bellinger 12056297b07cSNicholas Bellinger pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target" 12066297b07cSNicholas Bellinger " SAS Address: %s at Linux/SCSI Host ID: %d\n", 12076297b07cSNicholas Bellinger tl_hba->tl_wwn_address, tl_hba->sh->host_no); 12083703b2c5SNicholas Bellinger /* 12093703b2c5SNicholas Bellinger * Call device_unregister() on the original tl_hba->dev. 12103703b2c5SNicholas Bellinger * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will 12113703b2c5SNicholas Bellinger * release *tl_hba; 12123703b2c5SNicholas Bellinger */ 12133703b2c5SNicholas Bellinger device_unregister(&tl_hba->dev); 12143703b2c5SNicholas Bellinger } 12153703b2c5SNicholas Bellinger 12163703b2c5SNicholas Bellinger /* Start items for tcm_loop_cit */ 12173703b2c5SNicholas Bellinger static ssize_t tcm_loop_wwn_show_attr_version( 12183703b2c5SNicholas Bellinger struct target_fabric_configfs *tf, 12193703b2c5SNicholas Bellinger char *page) 12203703b2c5SNicholas Bellinger { 12213703b2c5SNicholas Bellinger return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION); 12223703b2c5SNicholas Bellinger } 12233703b2c5SNicholas Bellinger 12243703b2c5SNicholas Bellinger TF_WWN_ATTR_RO(tcm_loop, version); 12253703b2c5SNicholas Bellinger 12263703b2c5SNicholas Bellinger static struct configfs_attribute *tcm_loop_wwn_attrs[] = { 12273703b2c5SNicholas Bellinger &tcm_loop_wwn_version.attr, 12283703b2c5SNicholas Bellinger NULL, 12293703b2c5SNicholas Bellinger }; 12303703b2c5SNicholas Bellinger 12313703b2c5SNicholas Bellinger /* End items for tcm_loop_cit */ 12323703b2c5SNicholas Bellinger 12339ac8928eSChristoph Hellwig static const struct target_core_fabric_ops loop_ops = { 12349ac8928eSChristoph Hellwig .module = THIS_MODULE, 12359ac8928eSChristoph Hellwig .name = "loopback", 12369ac8928eSChristoph Hellwig .get_fabric_name = tcm_loop_get_fabric_name, 12379ac8928eSChristoph Hellwig .tpg_get_wwn = tcm_loop_get_endpoint_wwn, 12389ac8928eSChristoph Hellwig .tpg_get_tag = tcm_loop_get_tag, 12399ac8928eSChristoph Hellwig .tpg_check_demo_mode = tcm_loop_check_demo_mode, 12409ac8928eSChristoph Hellwig .tpg_check_demo_mode_cache = tcm_loop_check_demo_mode_cache, 12419ac8928eSChristoph Hellwig .tpg_check_demo_mode_write_protect = 12429ac8928eSChristoph Hellwig tcm_loop_check_demo_mode_write_protect, 12439ac8928eSChristoph Hellwig .tpg_check_prod_mode_write_protect = 12449ac8928eSChristoph Hellwig tcm_loop_check_prod_mode_write_protect, 12459ac8928eSChristoph Hellwig .tpg_check_prot_fabric_only = tcm_loop_check_prot_fabric_only, 12469ac8928eSChristoph Hellwig .tpg_get_inst_index = tcm_loop_get_inst_index, 12479ac8928eSChristoph Hellwig .check_stop_free = tcm_loop_check_stop_free, 12489ac8928eSChristoph Hellwig .release_cmd = tcm_loop_release_cmd, 12499ac8928eSChristoph Hellwig .shutdown_session = tcm_loop_shutdown_session, 12509ac8928eSChristoph Hellwig .close_session = tcm_loop_close_session, 12519ac8928eSChristoph Hellwig .sess_get_index = tcm_loop_sess_get_index, 12529ac8928eSChristoph Hellwig .write_pending = tcm_loop_write_pending, 12539ac8928eSChristoph Hellwig .write_pending_status = tcm_loop_write_pending_status, 12549ac8928eSChristoph Hellwig .set_default_node_attributes = tcm_loop_set_default_node_attributes, 12559ac8928eSChristoph Hellwig .get_cmd_state = tcm_loop_get_cmd_state, 12569ac8928eSChristoph Hellwig .queue_data_in = tcm_loop_queue_data_in, 12579ac8928eSChristoph Hellwig .queue_status = tcm_loop_queue_status, 12589ac8928eSChristoph Hellwig .queue_tm_rsp = tcm_loop_queue_tm_rsp, 12599ac8928eSChristoph Hellwig .aborted_task = tcm_loop_aborted_task, 12609ac8928eSChristoph Hellwig .fabric_make_wwn = tcm_loop_make_scsi_hba, 12619ac8928eSChristoph Hellwig .fabric_drop_wwn = tcm_loop_drop_scsi_hba, 12629ac8928eSChristoph Hellwig .fabric_make_tpg = tcm_loop_make_naa_tpg, 12639ac8928eSChristoph Hellwig .fabric_drop_tpg = tcm_loop_drop_naa_tpg, 12649ac8928eSChristoph Hellwig .fabric_post_link = tcm_loop_port_link, 12659ac8928eSChristoph Hellwig .fabric_pre_unlink = tcm_loop_port_unlink, 12669ac8928eSChristoph Hellwig .tfc_wwn_attrs = tcm_loop_wwn_attrs, 12679ac8928eSChristoph Hellwig .tfc_tpg_base_attrs = tcm_loop_tpg_attrs, 12689ac8928eSChristoph Hellwig .tfc_tpg_attrib_attrs = tcm_loop_tpg_attrib_attrs, 12699ac8928eSChristoph Hellwig }; 12703703b2c5SNicholas Bellinger 12713703b2c5SNicholas Bellinger static int __init tcm_loop_fabric_init(void) 12723703b2c5SNicholas Bellinger { 1273afe2cb7fSChristoph Hellwig int ret = -ENOMEM; 1274afe2cb7fSChristoph Hellwig 1275afe2cb7fSChristoph Hellwig tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0); 1276afe2cb7fSChristoph Hellwig if (!tcm_loop_workqueue) 1277afe2cb7fSChristoph Hellwig goto out; 12783703b2c5SNicholas Bellinger 12793703b2c5SNicholas Bellinger tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache", 12803703b2c5SNicholas Bellinger sizeof(struct tcm_loop_cmd), 12813703b2c5SNicholas Bellinger __alignof__(struct tcm_loop_cmd), 12823703b2c5SNicholas Bellinger 0, NULL); 12833703b2c5SNicholas Bellinger if (!tcm_loop_cmd_cache) { 12846708bb27SAndy Grover pr_debug("kmem_cache_create() for" 12853703b2c5SNicholas Bellinger " tcm_loop_cmd_cache failed\n"); 1286afe2cb7fSChristoph Hellwig goto out_destroy_workqueue; 12873703b2c5SNicholas Bellinger } 12883703b2c5SNicholas Bellinger 12893703b2c5SNicholas Bellinger ret = tcm_loop_alloc_core_bus(); 12903703b2c5SNicholas Bellinger if (ret) 1291afe2cb7fSChristoph Hellwig goto out_destroy_cache; 12923703b2c5SNicholas Bellinger 12939ac8928eSChristoph Hellwig ret = target_register_template(&loop_ops); 1294afe2cb7fSChristoph Hellwig if (ret) 1295afe2cb7fSChristoph Hellwig goto out_release_core_bus; 12963703b2c5SNicholas Bellinger 12973703b2c5SNicholas Bellinger return 0; 1298afe2cb7fSChristoph Hellwig 1299afe2cb7fSChristoph Hellwig out_release_core_bus: 1300afe2cb7fSChristoph Hellwig tcm_loop_release_core_bus(); 1301afe2cb7fSChristoph Hellwig out_destroy_cache: 1302afe2cb7fSChristoph Hellwig kmem_cache_destroy(tcm_loop_cmd_cache); 1303afe2cb7fSChristoph Hellwig out_destroy_workqueue: 1304afe2cb7fSChristoph Hellwig destroy_workqueue(tcm_loop_workqueue); 1305afe2cb7fSChristoph Hellwig out: 1306afe2cb7fSChristoph Hellwig return ret; 13073703b2c5SNicholas Bellinger } 13083703b2c5SNicholas Bellinger 13093703b2c5SNicholas Bellinger static void __exit tcm_loop_fabric_exit(void) 13103703b2c5SNicholas Bellinger { 13119ac8928eSChristoph Hellwig target_unregister_template(&loop_ops); 13123703b2c5SNicholas Bellinger tcm_loop_release_core_bus(); 13133703b2c5SNicholas Bellinger kmem_cache_destroy(tcm_loop_cmd_cache); 1314afe2cb7fSChristoph Hellwig destroy_workqueue(tcm_loop_workqueue); 13153703b2c5SNicholas Bellinger } 13163703b2c5SNicholas Bellinger 13173703b2c5SNicholas Bellinger MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module"); 13183703b2c5SNicholas Bellinger MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>"); 13193703b2c5SNicholas Bellinger MODULE_LICENSE("GPL"); 13203703b2c5SNicholas Bellinger module_init(tcm_loop_fabric_init); 13213703b2c5SNicholas Bellinger module_exit(tcm_loop_fabric_exit); 1322