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 443703b2c5SNicholas Bellinger /* Local pointer to allocated TCM configfs fabric module */ 453703b2c5SNicholas Bellinger static struct target_fabric_configfs *tcm_loop_fabric_configfs; 463703b2c5SNicholas Bellinger 47afe2cb7fSChristoph Hellwig static struct workqueue_struct *tcm_loop_workqueue; 483703b2c5SNicholas Bellinger static struct kmem_cache *tcm_loop_cmd_cache; 493703b2c5SNicholas Bellinger 503703b2c5SNicholas Bellinger static int tcm_loop_hba_no_cnt; 513703b2c5SNicholas Bellinger 5216786454SChristoph Hellwig static int tcm_loop_queue_status(struct se_cmd *se_cmd); 533703b2c5SNicholas Bellinger 543703b2c5SNicholas Bellinger /* 553703b2c5SNicholas Bellinger * Called from struct target_core_fabric_ops->check_stop_free() 563703b2c5SNicholas Bellinger */ 5788dd9e26SNicholas Bellinger static int tcm_loop_check_stop_free(struct se_cmd *se_cmd) 583703b2c5SNicholas Bellinger { 593703b2c5SNicholas Bellinger /* 603703b2c5SNicholas Bellinger * Do not release struct se_cmd's containing a valid TMR 613703b2c5SNicholas Bellinger * pointer. These will be released directly in tcm_loop_device_reset() 623703b2c5SNicholas Bellinger * with transport_generic_free_cmd(). 633703b2c5SNicholas Bellinger */ 64c8e31f26SAndy Grover if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) 6588dd9e26SNicholas Bellinger return 0; 663703b2c5SNicholas Bellinger /* 673703b2c5SNicholas Bellinger * Release the struct se_cmd, which will make a callback to release 683703b2c5SNicholas Bellinger * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd() 693703b2c5SNicholas Bellinger */ 7082f1c8a4SChristoph Hellwig transport_generic_free_cmd(se_cmd, 0); 7188dd9e26SNicholas Bellinger return 1; 723703b2c5SNicholas Bellinger } 733703b2c5SNicholas Bellinger 7435462975SChristoph Hellwig static void tcm_loop_release_cmd(struct se_cmd *se_cmd) 753703b2c5SNicholas Bellinger { 763703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 773703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd); 783703b2c5SNicholas Bellinger 793703b2c5SNicholas Bellinger kmem_cache_free(tcm_loop_cmd_cache, tl_cmd); 803703b2c5SNicholas Bellinger } 813703b2c5SNicholas Bellinger 828946b077SAl Viro static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host) 833703b2c5SNicholas Bellinger { 848946b077SAl Viro seq_printf(m, "tcm_loop_proc_info()\n"); 858946b077SAl Viro return 0; 863703b2c5SNicholas Bellinger } 873703b2c5SNicholas Bellinger 883703b2c5SNicholas Bellinger static int tcm_loop_driver_probe(struct device *); 893703b2c5SNicholas Bellinger static int tcm_loop_driver_remove(struct device *); 903703b2c5SNicholas Bellinger 913703b2c5SNicholas Bellinger static int pseudo_lld_bus_match(struct device *dev, 923703b2c5SNicholas Bellinger struct device_driver *dev_driver) 933703b2c5SNicholas Bellinger { 943703b2c5SNicholas Bellinger return 1; 953703b2c5SNicholas Bellinger } 963703b2c5SNicholas Bellinger 973703b2c5SNicholas Bellinger static struct bus_type tcm_loop_lld_bus = { 983703b2c5SNicholas Bellinger .name = "tcm_loop_bus", 993703b2c5SNicholas Bellinger .match = pseudo_lld_bus_match, 1003703b2c5SNicholas Bellinger .probe = tcm_loop_driver_probe, 1013703b2c5SNicholas Bellinger .remove = tcm_loop_driver_remove, 1023703b2c5SNicholas Bellinger }; 1033703b2c5SNicholas Bellinger 1043703b2c5SNicholas Bellinger static struct device_driver tcm_loop_driverfs = { 1053703b2c5SNicholas Bellinger .name = "tcm_loop", 1063703b2c5SNicholas Bellinger .bus = &tcm_loop_lld_bus, 1073703b2c5SNicholas Bellinger }; 1083703b2c5SNicholas Bellinger /* 1093703b2c5SNicholas Bellinger * Used with root_device_register() in tcm_loop_alloc_core_bus() below 1103703b2c5SNicholas Bellinger */ 1113703b2c5SNicholas Bellinger struct device *tcm_loop_primary; 1123703b2c5SNicholas Bellinger 1133703b2c5SNicholas Bellinger /* 1143703b2c5SNicholas Bellinger * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and 1153703b2c5SNicholas Bellinger * drivers/scsi/libiscsi.c:iscsi_change_queue_depth() 1163703b2c5SNicholas Bellinger */ 1173703b2c5SNicholas Bellinger static int tcm_loop_change_queue_depth( 1183703b2c5SNicholas Bellinger struct scsi_device *sdev, 1193703b2c5SNicholas Bellinger int depth, 1203703b2c5SNicholas Bellinger int reason) 1213703b2c5SNicholas Bellinger { 1223703b2c5SNicholas Bellinger switch (reason) { 1233703b2c5SNicholas Bellinger case SCSI_QDEPTH_DEFAULT: 1243703b2c5SNicholas Bellinger scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); 1253703b2c5SNicholas Bellinger break; 1263703b2c5SNicholas Bellinger case SCSI_QDEPTH_QFULL: 1273703b2c5SNicholas Bellinger scsi_track_queue_full(sdev, depth); 1283703b2c5SNicholas Bellinger break; 1293703b2c5SNicholas Bellinger case SCSI_QDEPTH_RAMP_UP: 1303703b2c5SNicholas Bellinger scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); 1313703b2c5SNicholas Bellinger break; 1323703b2c5SNicholas Bellinger default: 1333703b2c5SNicholas Bellinger return -EOPNOTSUPP; 1343703b2c5SNicholas Bellinger } 1353703b2c5SNicholas Bellinger return sdev->queue_depth; 1363703b2c5SNicholas Bellinger } 1373703b2c5SNicholas Bellinger 138969871cdSHannes Reinecke static int tcm_loop_change_queue_type(struct scsi_device *sdev, int tag) 139969871cdSHannes Reinecke { 140969871cdSHannes Reinecke if (sdev->tagged_supported) { 141969871cdSHannes Reinecke scsi_set_tag_type(sdev, tag); 142969871cdSHannes Reinecke 143969871cdSHannes Reinecke if (tag) 144969871cdSHannes Reinecke scsi_activate_tcq(sdev, sdev->queue_depth); 145969871cdSHannes Reinecke else 146969871cdSHannes Reinecke scsi_deactivate_tcq(sdev, sdev->queue_depth); 147969871cdSHannes Reinecke } else 148969871cdSHannes Reinecke tag = 0; 149969871cdSHannes Reinecke 150969871cdSHannes Reinecke return tag; 151969871cdSHannes Reinecke } 152969871cdSHannes Reinecke 1533703b2c5SNicholas Bellinger /* 154f872c9f4SChristoph Hellwig * Locate the SAM Task Attr from struct scsi_cmnd * 155f872c9f4SChristoph Hellwig */ 156f872c9f4SChristoph Hellwig static int tcm_loop_sam_attr(struct scsi_cmnd *sc) 157f872c9f4SChristoph Hellwig { 158f872c9f4SChristoph Hellwig if (sc->device->tagged_supported) { 159f872c9f4SChristoph Hellwig switch (sc->tag) { 160f872c9f4SChristoph Hellwig case HEAD_OF_QUEUE_TAG: 161f872c9f4SChristoph Hellwig return MSG_HEAD_TAG; 162f872c9f4SChristoph Hellwig case ORDERED_QUEUE_TAG: 163f872c9f4SChristoph Hellwig return MSG_ORDERED_TAG; 164f872c9f4SChristoph Hellwig default: 165f872c9f4SChristoph Hellwig break; 166f872c9f4SChristoph Hellwig } 167f872c9f4SChristoph Hellwig } 168f872c9f4SChristoph Hellwig 169f872c9f4SChristoph Hellwig return MSG_SIMPLE_TAG; 170f872c9f4SChristoph Hellwig } 171f872c9f4SChristoph Hellwig 172afe2cb7fSChristoph Hellwig static void tcm_loop_submission_work(struct work_struct *work) 1733703b2c5SNicholas Bellinger { 174afe2cb7fSChristoph Hellwig struct tcm_loop_cmd *tl_cmd = 175afe2cb7fSChristoph Hellwig container_of(work, struct tcm_loop_cmd, work); 176afe2cb7fSChristoph Hellwig struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd; 177afe2cb7fSChristoph Hellwig struct scsi_cmnd *sc = tl_cmd->sc; 178afe2cb7fSChristoph Hellwig struct tcm_loop_nexus *tl_nexus; 1793703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 1803703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg; 18116786454SChristoph Hellwig struct scatterlist *sgl_bidi = NULL; 182e2a4f55cSSagi Grimberg u32 sgl_bidi_count = 0, transfer_length; 1838f9f44f8SNicholas Bellinger int rc; 184f872c9f4SChristoph Hellwig 1853703b2c5SNicholas Bellinger tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 1863703b2c5SNicholas Bellinger tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 187afe2cb7fSChristoph Hellwig 1880a020436SNicholas Bellinger /* 1890a020436SNicholas Bellinger * Ensure that this tl_tpg reference from the incoming sc->device->id 1900a020436SNicholas Bellinger * has already been configured via tcm_loop_make_naa_tpg(). 1910a020436SNicholas Bellinger */ 1920a020436SNicholas Bellinger if (!tl_tpg->tl_hba) { 1930a020436SNicholas Bellinger set_host_byte(sc, DID_NO_CONNECT); 194f872c9f4SChristoph Hellwig goto out_done; 1950a020436SNicholas Bellinger } 196fb2b2844SHannes Reinecke if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) { 197fb2b2844SHannes Reinecke set_host_byte(sc, DID_TRANSPORT_DISRUPTED); 198fb2b2844SHannes Reinecke goto out_done; 199fb2b2844SHannes Reinecke } 200f872c9f4SChristoph Hellwig tl_nexus = tl_hba->tl_nexus; 201f872c9f4SChristoph Hellwig if (!tl_nexus) { 202f872c9f4SChristoph Hellwig scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus" 203f872c9f4SChristoph Hellwig " does not exist\n"); 204f872c9f4SChristoph Hellwig set_host_byte(sc, DID_ERROR); 205f872c9f4SChristoph Hellwig goto out_done; 2063703b2c5SNicholas Bellinger } 20716786454SChristoph Hellwig if (scsi_bidi_cmnd(sc)) { 20816786454SChristoph Hellwig struct scsi_data_buffer *sdb = scsi_in(sc); 20916786454SChristoph Hellwig 21016786454SChristoph Hellwig sgl_bidi = sdb->table.sgl; 21116786454SChristoph Hellwig sgl_bidi_count = sdb->table.nents; 212f872c9f4SChristoph Hellwig se_cmd->se_cmd_flags |= SCF_BIDI; 213f872c9f4SChristoph Hellwig 21416786454SChristoph Hellwig } 215b5b8e298SSagi Grimberg 216e2a4f55cSSagi Grimberg transfer_length = scsi_transfer_length(sc); 217e2a4f55cSSagi Grimberg if (!scsi_prot_sg_count(sc) && 218e2a4f55cSSagi Grimberg scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) { 219b5b8e298SSagi Grimberg se_cmd->prot_pto = true; 220e2a4f55cSSagi Grimberg /* 221e2a4f55cSSagi Grimberg * loopback transport doesn't support 222e2a4f55cSSagi Grimberg * WRITE_GENERATE, READ_STRIP protection 223e2a4f55cSSagi Grimberg * information operations, go ahead unprotected. 224e2a4f55cSSagi Grimberg */ 225e2a4f55cSSagi Grimberg transfer_length = scsi_bufflen(sc); 226e2a4f55cSSagi Grimberg } 227b5b8e298SSagi Grimberg 2288f9f44f8SNicholas Bellinger rc = target_submit_cmd_map_sgls(se_cmd, tl_nexus->se_sess, sc->cmnd, 2298f9f44f8SNicholas Bellinger &tl_cmd->tl_sense_buf[0], tl_cmd->sc->device->lun, 230e2a4f55cSSagi Grimberg transfer_length, tcm_loop_sam_attr(sc), 2318f9f44f8SNicholas Bellinger sc->sc_data_direction, 0, 2328f9f44f8SNicholas Bellinger scsi_sglist(sc), scsi_sg_count(sc), 23359dedde2SNicholas Bellinger sgl_bidi, sgl_bidi_count, 23459dedde2SNicholas Bellinger scsi_prot_sglist(sc), scsi_prot_sg_count(sc)); 2358f9f44f8SNicholas Bellinger if (rc < 0) { 236f872c9f4SChristoph Hellwig set_host_byte(sc, DID_NO_CONNECT); 237f872c9f4SChristoph Hellwig goto out_done; 238f872c9f4SChristoph Hellwig } 239afe2cb7fSChristoph Hellwig return; 240f872c9f4SChristoph Hellwig 241f872c9f4SChristoph Hellwig out_done: 242b43f1886SNicholas Bellinger kmem_cache_free(tcm_loop_cmd_cache, tl_cmd); 243f872c9f4SChristoph Hellwig sc->scsi_done(sc); 244afe2cb7fSChristoph Hellwig return; 245afe2cb7fSChristoph Hellwig } 246afe2cb7fSChristoph Hellwig 247afe2cb7fSChristoph Hellwig /* 248afe2cb7fSChristoph Hellwig * ->queuecommand can be and usually is called from interrupt context, so 249afe2cb7fSChristoph Hellwig * defer the actual submission to a workqueue. 250afe2cb7fSChristoph Hellwig */ 251afe2cb7fSChristoph Hellwig static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc) 252afe2cb7fSChristoph Hellwig { 253afe2cb7fSChristoph Hellwig struct tcm_loop_cmd *tl_cmd; 254afe2cb7fSChristoph Hellwig 2559cb78c16SHannes Reinecke pr_debug("tcm_loop_queuecommand() %d:%d:%d:%llu got CDB: 0x%02x" 256afe2cb7fSChristoph Hellwig " scsi_buf_len: %u\n", sc->device->host->host_no, 257afe2cb7fSChristoph Hellwig sc->device->id, sc->device->channel, sc->device->lun, 258afe2cb7fSChristoph Hellwig sc->cmnd[0], scsi_bufflen(sc)); 259afe2cb7fSChristoph Hellwig 260afe2cb7fSChristoph Hellwig tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC); 261afe2cb7fSChristoph Hellwig if (!tl_cmd) { 262afe2cb7fSChristoph Hellwig pr_err("Unable to allocate struct tcm_loop_cmd\n"); 263afe2cb7fSChristoph Hellwig set_host_byte(sc, DID_ERROR); 264afe2cb7fSChristoph Hellwig sc->scsi_done(sc); 265afe2cb7fSChristoph Hellwig return 0; 266afe2cb7fSChristoph Hellwig } 267afe2cb7fSChristoph Hellwig 268afe2cb7fSChristoph Hellwig tl_cmd->sc = sc; 269969871cdSHannes Reinecke tl_cmd->sc_cmd_tag = sc->tag; 270afe2cb7fSChristoph Hellwig INIT_WORK(&tl_cmd->work, tcm_loop_submission_work); 271afe2cb7fSChristoph Hellwig queue_work(tcm_loop_workqueue, &tl_cmd->work); 272f872c9f4SChristoph Hellwig return 0; 2733703b2c5SNicholas Bellinger } 2743703b2c5SNicholas Bellinger 2753703b2c5SNicholas Bellinger /* 2763703b2c5SNicholas Bellinger * Called from SCSI EH process context to issue a LUN_RESET TMR 2773703b2c5SNicholas Bellinger * to struct scsi_device 2783703b2c5SNicholas Bellinger */ 279a314d700SHannes Reinecke static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg, 280a314d700SHannes Reinecke struct tcm_loop_nexus *tl_nexus, 281969871cdSHannes Reinecke int lun, int task, enum tcm_tmreq_table tmr) 2823703b2c5SNicholas Bellinger { 2833703b2c5SNicholas Bellinger struct se_cmd *se_cmd = NULL; 2843703b2c5SNicholas Bellinger struct se_session *se_sess; 285a314d700SHannes Reinecke struct se_portal_group *se_tpg; 2863703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = NULL; 287a314d700SHannes Reinecke struct tcm_loop_tmr *tl_tmr = NULL; 288a314d700SHannes Reinecke int ret = TMR_FUNCTION_FAILED, rc; 289a314d700SHannes Reinecke 290a314d700SHannes Reinecke tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL); 291a314d700SHannes Reinecke if (!tl_cmd) { 292a314d700SHannes Reinecke pr_err("Unable to allocate memory for tl_cmd\n"); 293a314d700SHannes Reinecke return ret; 294a314d700SHannes Reinecke } 295a314d700SHannes Reinecke 296a314d700SHannes Reinecke tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL); 297a314d700SHannes Reinecke if (!tl_tmr) { 298a314d700SHannes Reinecke pr_err("Unable to allocate memory for tl_tmr\n"); 299a314d700SHannes Reinecke goto release; 300a314d700SHannes Reinecke } 301a314d700SHannes Reinecke init_waitqueue_head(&tl_tmr->tl_tmr_wait); 302a314d700SHannes Reinecke 303a314d700SHannes Reinecke se_cmd = &tl_cmd->tl_se_cmd; 304a314d700SHannes Reinecke se_tpg = &tl_tpg->tl_se_tpg; 305a314d700SHannes Reinecke se_sess = tl_nexus->se_sess; 306a314d700SHannes Reinecke /* 307a314d700SHannes Reinecke * Initialize struct se_cmd descriptor from target_core_mod infrastructure 308a314d700SHannes Reinecke */ 309a314d700SHannes Reinecke transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0, 310a314d700SHannes Reinecke DMA_NONE, MSG_SIMPLE_TAG, 311a314d700SHannes Reinecke &tl_cmd->tl_sense_buf[0]); 312a314d700SHannes Reinecke 313a314d700SHannes Reinecke rc = core_tmr_alloc_req(se_cmd, tl_tmr, tmr, GFP_KERNEL); 314a314d700SHannes Reinecke if (rc < 0) 315a314d700SHannes Reinecke goto release; 316a314d700SHannes Reinecke 317969871cdSHannes Reinecke if (tmr == TMR_ABORT_TASK) 318969871cdSHannes Reinecke se_cmd->se_tmr_req->ref_task_tag = task; 319969871cdSHannes Reinecke 320a314d700SHannes Reinecke /* 321a314d700SHannes Reinecke * Locate the underlying TCM struct se_lun 322a314d700SHannes Reinecke */ 323a314d700SHannes Reinecke if (transport_lookup_tmr_lun(se_cmd, lun) < 0) { 324a314d700SHannes Reinecke ret = TMR_LUN_DOES_NOT_EXIST; 325a314d700SHannes Reinecke goto release; 326a314d700SHannes Reinecke } 327a314d700SHannes Reinecke /* 328a314d700SHannes Reinecke * Queue the TMR to TCM Core and sleep waiting for 329a314d700SHannes Reinecke * tcm_loop_queue_tm_rsp() to wake us up. 330a314d700SHannes Reinecke */ 331a314d700SHannes Reinecke transport_generic_handle_tmr(se_cmd); 332a314d700SHannes Reinecke wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete)); 333a314d700SHannes Reinecke /* 334a314d700SHannes Reinecke * The TMR LUN_RESET has completed, check the response status and 335a314d700SHannes Reinecke * then release allocations. 336a314d700SHannes Reinecke */ 337a314d700SHannes Reinecke ret = se_cmd->se_tmr_req->response; 338a314d700SHannes Reinecke release: 339a314d700SHannes Reinecke if (se_cmd) 340a314d700SHannes Reinecke transport_generic_free_cmd(se_cmd, 1); 341a314d700SHannes Reinecke else 342a314d700SHannes Reinecke kmem_cache_free(tcm_loop_cmd_cache, tl_cmd); 343a314d700SHannes Reinecke kfree(tl_tmr); 344a314d700SHannes Reinecke return ret; 345a314d700SHannes Reinecke } 346a314d700SHannes Reinecke 347969871cdSHannes Reinecke static int tcm_loop_abort_task(struct scsi_cmnd *sc) 348969871cdSHannes Reinecke { 349969871cdSHannes Reinecke struct tcm_loop_hba *tl_hba; 350969871cdSHannes Reinecke struct tcm_loop_nexus *tl_nexus; 351969871cdSHannes Reinecke struct tcm_loop_tpg *tl_tpg; 352969871cdSHannes Reinecke int ret = FAILED; 353969871cdSHannes Reinecke 354969871cdSHannes Reinecke /* 355969871cdSHannes Reinecke * Locate the tcm_loop_hba_t pointer 356969871cdSHannes Reinecke */ 357969871cdSHannes Reinecke tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 358969871cdSHannes Reinecke /* 359969871cdSHannes Reinecke * Locate the tl_nexus and se_sess pointers 360969871cdSHannes Reinecke */ 361969871cdSHannes Reinecke tl_nexus = tl_hba->tl_nexus; 362969871cdSHannes Reinecke if (!tl_nexus) { 363969871cdSHannes Reinecke pr_err("Unable to perform device reset without" 364969871cdSHannes Reinecke " active I_T Nexus\n"); 365969871cdSHannes Reinecke return FAILED; 366969871cdSHannes Reinecke } 367969871cdSHannes Reinecke 368969871cdSHannes Reinecke /* 369969871cdSHannes Reinecke * Locate the tl_tpg pointer from TargetID in sc->device->id 370969871cdSHannes Reinecke */ 371969871cdSHannes Reinecke tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 372969871cdSHannes Reinecke ret = tcm_loop_issue_tmr(tl_tpg, tl_nexus, sc->device->lun, 373969871cdSHannes Reinecke sc->tag, TMR_ABORT_TASK); 374969871cdSHannes Reinecke return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED; 375969871cdSHannes Reinecke } 376969871cdSHannes Reinecke 377a314d700SHannes Reinecke /* 378a314d700SHannes Reinecke * Called from SCSI EH process context to issue a LUN_RESET TMR 379a314d700SHannes Reinecke * to struct scsi_device 380a314d700SHannes Reinecke */ 381a314d700SHannes Reinecke static int tcm_loop_device_reset(struct scsi_cmnd *sc) 382a314d700SHannes Reinecke { 3833703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 3843703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus; 3853703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg; 386a314d700SHannes Reinecke int ret = FAILED; 387a314d700SHannes Reinecke 3883703b2c5SNicholas Bellinger /* 3893703b2c5SNicholas Bellinger * Locate the tcm_loop_hba_t pointer 3903703b2c5SNicholas Bellinger */ 3913703b2c5SNicholas Bellinger tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 3923703b2c5SNicholas Bellinger /* 3933703b2c5SNicholas Bellinger * Locate the tl_nexus and se_sess pointers 3943703b2c5SNicholas Bellinger */ 3953703b2c5SNicholas Bellinger tl_nexus = tl_hba->tl_nexus; 3963703b2c5SNicholas Bellinger if (!tl_nexus) { 3976708bb27SAndy Grover pr_err("Unable to perform device reset without" 3983703b2c5SNicholas Bellinger " active I_T Nexus\n"); 3993703b2c5SNicholas Bellinger return FAILED; 4003703b2c5SNicholas Bellinger } 4013703b2c5SNicholas Bellinger /* 402a314d700SHannes Reinecke * Locate the tl_tpg pointer from TargetID in sc->device->id 4033703b2c5SNicholas Bellinger */ 4043703b2c5SNicholas Bellinger tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 405969871cdSHannes Reinecke ret = tcm_loop_issue_tmr(tl_tpg, tl_nexus, sc->device->lun, 406969871cdSHannes Reinecke 0, TMR_LUN_RESET); 407a314d700SHannes Reinecke return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED; 4083703b2c5SNicholas Bellinger } 4093703b2c5SNicholas Bellinger 4108f4a1fb0SHannes Reinecke static int tcm_loop_target_reset(struct scsi_cmnd *sc) 4118f4a1fb0SHannes Reinecke { 4128f4a1fb0SHannes Reinecke struct tcm_loop_hba *tl_hba; 4138f4a1fb0SHannes Reinecke struct tcm_loop_tpg *tl_tpg; 4148f4a1fb0SHannes Reinecke 4158f4a1fb0SHannes Reinecke /* 4168f4a1fb0SHannes Reinecke * Locate the tcm_loop_hba_t pointer 4178f4a1fb0SHannes Reinecke */ 4188f4a1fb0SHannes Reinecke tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host); 4198f4a1fb0SHannes Reinecke if (!tl_hba) { 4208f4a1fb0SHannes Reinecke pr_err("Unable to perform device reset without" 4218f4a1fb0SHannes Reinecke " active I_T Nexus\n"); 4228f4a1fb0SHannes Reinecke return FAILED; 4238f4a1fb0SHannes Reinecke } 4248f4a1fb0SHannes Reinecke /* 4258f4a1fb0SHannes Reinecke * Locate the tl_tpg pointer from TargetID in sc->device->id 4268f4a1fb0SHannes Reinecke */ 4278f4a1fb0SHannes Reinecke tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id]; 4288f4a1fb0SHannes Reinecke if (tl_tpg) { 4298f4a1fb0SHannes Reinecke tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE; 4308f4a1fb0SHannes Reinecke return SUCCESS; 4318f4a1fb0SHannes Reinecke } 4328f4a1fb0SHannes Reinecke return FAILED; 4338f4a1fb0SHannes Reinecke } 4348f4a1fb0SHannes Reinecke 4353703b2c5SNicholas Bellinger static int tcm_loop_slave_alloc(struct scsi_device *sd) 4363703b2c5SNicholas Bellinger { 4373703b2c5SNicholas Bellinger set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags); 4383703b2c5SNicholas Bellinger return 0; 4393703b2c5SNicholas Bellinger } 4403703b2c5SNicholas Bellinger 4413703b2c5SNicholas Bellinger static int tcm_loop_slave_configure(struct scsi_device *sd) 4423703b2c5SNicholas Bellinger { 443969871cdSHannes Reinecke if (sd->tagged_supported) { 444969871cdSHannes Reinecke scsi_activate_tcq(sd, sd->queue_depth); 445969871cdSHannes Reinecke scsi_adjust_queue_depth(sd, MSG_SIMPLE_TAG, 446969871cdSHannes Reinecke sd->host->cmd_per_lun); 447969871cdSHannes Reinecke } else { 448969871cdSHannes Reinecke scsi_adjust_queue_depth(sd, 0, 449969871cdSHannes Reinecke sd->host->cmd_per_lun); 450969871cdSHannes Reinecke } 451969871cdSHannes Reinecke 4523703b2c5SNicholas Bellinger return 0; 4533703b2c5SNicholas Bellinger } 4543703b2c5SNicholas Bellinger 4553703b2c5SNicholas Bellinger static struct scsi_host_template tcm_loop_driver_template = { 4568946b077SAl Viro .show_info = tcm_loop_show_info, 4573703b2c5SNicholas Bellinger .proc_name = "tcm_loopback", 4583703b2c5SNicholas Bellinger .name = "TCM_Loopback", 4593703b2c5SNicholas Bellinger .queuecommand = tcm_loop_queuecommand, 4603703b2c5SNicholas Bellinger .change_queue_depth = tcm_loop_change_queue_depth, 461969871cdSHannes Reinecke .change_queue_type = tcm_loop_change_queue_type, 462969871cdSHannes Reinecke .eh_abort_handler = tcm_loop_abort_task, 4633703b2c5SNicholas Bellinger .eh_device_reset_handler = tcm_loop_device_reset, 4648f4a1fb0SHannes Reinecke .eh_target_reset_handler = tcm_loop_target_reset, 4652e88efd3SChristoph Hellwig .can_queue = 1024, 4663703b2c5SNicholas Bellinger .this_id = -1, 4672e88efd3SChristoph Hellwig .sg_tablesize = 256, 4682e88efd3SChristoph Hellwig .cmd_per_lun = 1024, 4692e88efd3SChristoph Hellwig .max_sectors = 0xFFFF, 4703703b2c5SNicholas Bellinger .use_clustering = DISABLE_CLUSTERING, 4713703b2c5SNicholas Bellinger .slave_alloc = tcm_loop_slave_alloc, 4723703b2c5SNicholas Bellinger .slave_configure = tcm_loop_slave_configure, 4733703b2c5SNicholas Bellinger .module = THIS_MODULE, 4743703b2c5SNicholas Bellinger }; 4753703b2c5SNicholas Bellinger 4763703b2c5SNicholas Bellinger static int tcm_loop_driver_probe(struct device *dev) 4773703b2c5SNicholas Bellinger { 4783703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 4793703b2c5SNicholas Bellinger struct Scsi_Host *sh; 48059dedde2SNicholas Bellinger int error, host_prot; 4813703b2c5SNicholas Bellinger 4823703b2c5SNicholas Bellinger tl_hba = to_tcm_loop_hba(dev); 4833703b2c5SNicholas Bellinger 4843703b2c5SNicholas Bellinger sh = scsi_host_alloc(&tcm_loop_driver_template, 4853703b2c5SNicholas Bellinger sizeof(struct tcm_loop_hba)); 4863703b2c5SNicholas Bellinger if (!sh) { 4876708bb27SAndy Grover pr_err("Unable to allocate struct scsi_host\n"); 4883703b2c5SNicholas Bellinger return -ENODEV; 4893703b2c5SNicholas Bellinger } 4903703b2c5SNicholas Bellinger tl_hba->sh = sh; 4913703b2c5SNicholas Bellinger 4923703b2c5SNicholas Bellinger /* 4933703b2c5SNicholas Bellinger * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata 4943703b2c5SNicholas Bellinger */ 4953703b2c5SNicholas Bellinger *((struct tcm_loop_hba **)sh->hostdata) = tl_hba; 4963703b2c5SNicholas Bellinger /* 4973703b2c5SNicholas Bellinger * Setup single ID, Channel and LUN for now.. 4983703b2c5SNicholas Bellinger */ 4993703b2c5SNicholas Bellinger sh->max_id = 2; 5003703b2c5SNicholas Bellinger sh->max_lun = 0; 5013703b2c5SNicholas Bellinger sh->max_channel = 0; 5023703b2c5SNicholas Bellinger sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN; 5033703b2c5SNicholas Bellinger 50459dedde2SNicholas Bellinger host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION | 50559dedde2SNicholas Bellinger SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION | 50659dedde2SNicholas Bellinger SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION; 50759dedde2SNicholas Bellinger 50859dedde2SNicholas Bellinger scsi_host_set_prot(sh, host_prot); 50959dedde2SNicholas Bellinger scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC); 51059dedde2SNicholas Bellinger 5113703b2c5SNicholas Bellinger error = scsi_add_host(sh, &tl_hba->dev); 5123703b2c5SNicholas Bellinger if (error) { 5136708bb27SAndy Grover pr_err("%s: scsi_add_host failed\n", __func__); 5143703b2c5SNicholas Bellinger scsi_host_put(sh); 5153703b2c5SNicholas Bellinger return -ENODEV; 5163703b2c5SNicholas Bellinger } 5173703b2c5SNicholas Bellinger return 0; 5183703b2c5SNicholas Bellinger } 5193703b2c5SNicholas Bellinger 5203703b2c5SNicholas Bellinger static int tcm_loop_driver_remove(struct device *dev) 5213703b2c5SNicholas Bellinger { 5223703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 5233703b2c5SNicholas Bellinger struct Scsi_Host *sh; 5243703b2c5SNicholas Bellinger 5253703b2c5SNicholas Bellinger tl_hba = to_tcm_loop_hba(dev); 5263703b2c5SNicholas Bellinger sh = tl_hba->sh; 5273703b2c5SNicholas Bellinger 5283703b2c5SNicholas Bellinger scsi_remove_host(sh); 5293703b2c5SNicholas Bellinger scsi_host_put(sh); 5303703b2c5SNicholas Bellinger return 0; 5313703b2c5SNicholas Bellinger } 5323703b2c5SNicholas Bellinger 5333703b2c5SNicholas Bellinger static void tcm_loop_release_adapter(struct device *dev) 5343703b2c5SNicholas Bellinger { 5353703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev); 5363703b2c5SNicholas Bellinger 5373703b2c5SNicholas Bellinger kfree(tl_hba); 5383703b2c5SNicholas Bellinger } 5393703b2c5SNicholas Bellinger 5403703b2c5SNicholas Bellinger /* 5413703b2c5SNicholas Bellinger * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c 5423703b2c5SNicholas Bellinger */ 5433703b2c5SNicholas Bellinger static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id) 5443703b2c5SNicholas Bellinger { 5453703b2c5SNicholas Bellinger int ret; 5463703b2c5SNicholas Bellinger 5473703b2c5SNicholas Bellinger tl_hba->dev.bus = &tcm_loop_lld_bus; 5483703b2c5SNicholas Bellinger tl_hba->dev.parent = tcm_loop_primary; 5493703b2c5SNicholas Bellinger tl_hba->dev.release = &tcm_loop_release_adapter; 5503703b2c5SNicholas Bellinger dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id); 5513703b2c5SNicholas Bellinger 5523703b2c5SNicholas Bellinger ret = device_register(&tl_hba->dev); 5533703b2c5SNicholas Bellinger if (ret) { 5546708bb27SAndy Grover pr_err("device_register() failed for" 5553703b2c5SNicholas Bellinger " tl_hba->dev: %d\n", ret); 5563703b2c5SNicholas Bellinger return -ENODEV; 5573703b2c5SNicholas Bellinger } 5583703b2c5SNicholas Bellinger 5593703b2c5SNicholas Bellinger return 0; 5603703b2c5SNicholas Bellinger } 5613703b2c5SNicholas Bellinger 5623703b2c5SNicholas Bellinger /* 5633703b2c5SNicholas Bellinger * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated 5643703b2c5SNicholas Bellinger * tcm_loop SCSI bus. 5653703b2c5SNicholas Bellinger */ 5663703b2c5SNicholas Bellinger static int tcm_loop_alloc_core_bus(void) 5673703b2c5SNicholas Bellinger { 5683703b2c5SNicholas Bellinger int ret; 5693703b2c5SNicholas Bellinger 5703703b2c5SNicholas Bellinger tcm_loop_primary = root_device_register("tcm_loop_0"); 5713703b2c5SNicholas Bellinger if (IS_ERR(tcm_loop_primary)) { 5726708bb27SAndy Grover pr_err("Unable to allocate tcm_loop_primary\n"); 5733703b2c5SNicholas Bellinger return PTR_ERR(tcm_loop_primary); 5743703b2c5SNicholas Bellinger } 5753703b2c5SNicholas Bellinger 5763703b2c5SNicholas Bellinger ret = bus_register(&tcm_loop_lld_bus); 5773703b2c5SNicholas Bellinger if (ret) { 5786708bb27SAndy Grover pr_err("bus_register() failed for tcm_loop_lld_bus\n"); 5793703b2c5SNicholas Bellinger goto dev_unreg; 5803703b2c5SNicholas Bellinger } 5813703b2c5SNicholas Bellinger 5823703b2c5SNicholas Bellinger ret = driver_register(&tcm_loop_driverfs); 5833703b2c5SNicholas Bellinger if (ret) { 5846708bb27SAndy Grover pr_err("driver_register() failed for" 5853703b2c5SNicholas Bellinger "tcm_loop_driverfs\n"); 5863703b2c5SNicholas Bellinger goto bus_unreg; 5873703b2c5SNicholas Bellinger } 5883703b2c5SNicholas Bellinger 5896708bb27SAndy Grover pr_debug("Initialized TCM Loop Core Bus\n"); 5903703b2c5SNicholas Bellinger return ret; 5913703b2c5SNicholas Bellinger 5923703b2c5SNicholas Bellinger bus_unreg: 5933703b2c5SNicholas Bellinger bus_unregister(&tcm_loop_lld_bus); 5943703b2c5SNicholas Bellinger dev_unreg: 5953703b2c5SNicholas Bellinger root_device_unregister(tcm_loop_primary); 5963703b2c5SNicholas Bellinger return ret; 5973703b2c5SNicholas Bellinger } 5983703b2c5SNicholas Bellinger 5993703b2c5SNicholas Bellinger static void tcm_loop_release_core_bus(void) 6003703b2c5SNicholas Bellinger { 6013703b2c5SNicholas Bellinger driver_unregister(&tcm_loop_driverfs); 6023703b2c5SNicholas Bellinger bus_unregister(&tcm_loop_lld_bus); 6033703b2c5SNicholas Bellinger root_device_unregister(tcm_loop_primary); 6043703b2c5SNicholas Bellinger 6056708bb27SAndy Grover pr_debug("Releasing TCM Loop Core BUS\n"); 6063703b2c5SNicholas Bellinger } 6073703b2c5SNicholas Bellinger 6083703b2c5SNicholas Bellinger static char *tcm_loop_get_fabric_name(void) 6093703b2c5SNicholas Bellinger { 6103703b2c5SNicholas Bellinger return "loopback"; 6113703b2c5SNicholas Bellinger } 6123703b2c5SNicholas Bellinger 6133703b2c5SNicholas Bellinger static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg) 6143703b2c5SNicholas Bellinger { 6158359cf43SJörn Engel struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr; 6163703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 6173703b2c5SNicholas Bellinger /* 6183703b2c5SNicholas Bellinger * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba() 6193703b2c5SNicholas Bellinger * time based on the protocol dependent prefix of the passed configfs group. 6203703b2c5SNicholas Bellinger * 6213703b2c5SNicholas Bellinger * Based upon tl_proto_id, TCM_Loop emulates the requested fabric 6223703b2c5SNicholas Bellinger * ProtocolID using target_core_fabric_lib.c symbols. 6233703b2c5SNicholas Bellinger */ 6243703b2c5SNicholas Bellinger switch (tl_hba->tl_proto_id) { 6253703b2c5SNicholas Bellinger case SCSI_PROTOCOL_SAS: 6263703b2c5SNicholas Bellinger return sas_get_fabric_proto_ident(se_tpg); 6273703b2c5SNicholas Bellinger case SCSI_PROTOCOL_FCP: 6283703b2c5SNicholas Bellinger return fc_get_fabric_proto_ident(se_tpg); 6293703b2c5SNicholas Bellinger case SCSI_PROTOCOL_ISCSI: 6303703b2c5SNicholas Bellinger return iscsi_get_fabric_proto_ident(se_tpg); 6313703b2c5SNicholas Bellinger default: 6326708bb27SAndy Grover pr_err("Unknown tl_proto_id: 0x%02x, using" 6333703b2c5SNicholas Bellinger " SAS emulation\n", tl_hba->tl_proto_id); 6343703b2c5SNicholas Bellinger break; 6353703b2c5SNicholas Bellinger } 6363703b2c5SNicholas Bellinger 6373703b2c5SNicholas Bellinger return sas_get_fabric_proto_ident(se_tpg); 6383703b2c5SNicholas Bellinger } 6393703b2c5SNicholas Bellinger 6403703b2c5SNicholas Bellinger static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg) 6413703b2c5SNicholas Bellinger { 6428359cf43SJörn Engel struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr; 6433703b2c5SNicholas Bellinger /* 6443703b2c5SNicholas Bellinger * Return the passed NAA identifier for the SAS Target Port 6453703b2c5SNicholas Bellinger */ 6463703b2c5SNicholas Bellinger return &tl_tpg->tl_hba->tl_wwn_address[0]; 6473703b2c5SNicholas Bellinger } 6483703b2c5SNicholas Bellinger 6493703b2c5SNicholas Bellinger static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg) 6503703b2c5SNicholas Bellinger { 6518359cf43SJörn Engel struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr; 6523703b2c5SNicholas Bellinger /* 6533703b2c5SNicholas Bellinger * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83 6543703b2c5SNicholas Bellinger * to represent the SCSI Target Port. 6553703b2c5SNicholas Bellinger */ 6563703b2c5SNicholas Bellinger return tl_tpg->tl_tpgt; 6573703b2c5SNicholas Bellinger } 6583703b2c5SNicholas Bellinger 6593703b2c5SNicholas Bellinger static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg) 6603703b2c5SNicholas Bellinger { 6613703b2c5SNicholas Bellinger return 1; 6623703b2c5SNicholas Bellinger } 6633703b2c5SNicholas Bellinger 6643703b2c5SNicholas Bellinger static u32 tcm_loop_get_pr_transport_id( 6653703b2c5SNicholas Bellinger struct se_portal_group *se_tpg, 6663703b2c5SNicholas Bellinger struct se_node_acl *se_nacl, 6673703b2c5SNicholas Bellinger struct t10_pr_registration *pr_reg, 6683703b2c5SNicholas Bellinger int *format_code, 6693703b2c5SNicholas Bellinger unsigned char *buf) 6703703b2c5SNicholas Bellinger { 6718359cf43SJörn Engel struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr; 6723703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 6733703b2c5SNicholas Bellinger 6743703b2c5SNicholas Bellinger switch (tl_hba->tl_proto_id) { 6753703b2c5SNicholas Bellinger case SCSI_PROTOCOL_SAS: 6763703b2c5SNicholas Bellinger return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, 6773703b2c5SNicholas Bellinger format_code, buf); 6783703b2c5SNicholas Bellinger case SCSI_PROTOCOL_FCP: 6793703b2c5SNicholas Bellinger return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg, 6803703b2c5SNicholas Bellinger format_code, buf); 6813703b2c5SNicholas Bellinger case SCSI_PROTOCOL_ISCSI: 6823703b2c5SNicholas Bellinger return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg, 6833703b2c5SNicholas Bellinger format_code, buf); 6843703b2c5SNicholas Bellinger default: 6856708bb27SAndy Grover pr_err("Unknown tl_proto_id: 0x%02x, using" 6863703b2c5SNicholas Bellinger " SAS emulation\n", tl_hba->tl_proto_id); 6873703b2c5SNicholas Bellinger break; 6883703b2c5SNicholas Bellinger } 6893703b2c5SNicholas Bellinger 6903703b2c5SNicholas Bellinger return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, 6913703b2c5SNicholas Bellinger format_code, buf); 6923703b2c5SNicholas Bellinger } 6933703b2c5SNicholas Bellinger 6943703b2c5SNicholas Bellinger static u32 tcm_loop_get_pr_transport_id_len( 6953703b2c5SNicholas Bellinger struct se_portal_group *se_tpg, 6963703b2c5SNicholas Bellinger struct se_node_acl *se_nacl, 6973703b2c5SNicholas Bellinger struct t10_pr_registration *pr_reg, 6983703b2c5SNicholas Bellinger int *format_code) 6993703b2c5SNicholas Bellinger { 7008359cf43SJörn Engel struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr; 7013703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 7023703b2c5SNicholas Bellinger 7033703b2c5SNicholas Bellinger switch (tl_hba->tl_proto_id) { 7043703b2c5SNicholas Bellinger case SCSI_PROTOCOL_SAS: 7053703b2c5SNicholas Bellinger return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, 7063703b2c5SNicholas Bellinger format_code); 7073703b2c5SNicholas Bellinger case SCSI_PROTOCOL_FCP: 7083703b2c5SNicholas Bellinger return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, 7093703b2c5SNicholas Bellinger format_code); 7103703b2c5SNicholas Bellinger case SCSI_PROTOCOL_ISCSI: 7113703b2c5SNicholas Bellinger return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, 7123703b2c5SNicholas Bellinger format_code); 7133703b2c5SNicholas Bellinger default: 7146708bb27SAndy Grover pr_err("Unknown tl_proto_id: 0x%02x, using" 7153703b2c5SNicholas Bellinger " SAS emulation\n", tl_hba->tl_proto_id); 7163703b2c5SNicholas Bellinger break; 7173703b2c5SNicholas Bellinger } 7183703b2c5SNicholas Bellinger 7193703b2c5SNicholas Bellinger return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, 7203703b2c5SNicholas Bellinger format_code); 7213703b2c5SNicholas Bellinger } 7223703b2c5SNicholas Bellinger 7233703b2c5SNicholas Bellinger /* 7243703b2c5SNicholas Bellinger * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above 7253703b2c5SNicholas Bellinger * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations. 7263703b2c5SNicholas Bellinger */ 7273703b2c5SNicholas Bellinger static char *tcm_loop_parse_pr_out_transport_id( 7283703b2c5SNicholas Bellinger struct se_portal_group *se_tpg, 7293703b2c5SNicholas Bellinger const char *buf, 7303703b2c5SNicholas Bellinger u32 *out_tid_len, 7313703b2c5SNicholas Bellinger char **port_nexus_ptr) 7323703b2c5SNicholas Bellinger { 7338359cf43SJörn Engel struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr; 7343703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 7353703b2c5SNicholas Bellinger 7363703b2c5SNicholas Bellinger switch (tl_hba->tl_proto_id) { 7373703b2c5SNicholas Bellinger case SCSI_PROTOCOL_SAS: 7383703b2c5SNicholas Bellinger return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, 7393703b2c5SNicholas Bellinger port_nexus_ptr); 7403703b2c5SNicholas Bellinger case SCSI_PROTOCOL_FCP: 7413703b2c5SNicholas Bellinger return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, 7423703b2c5SNicholas Bellinger port_nexus_ptr); 7433703b2c5SNicholas Bellinger case SCSI_PROTOCOL_ISCSI: 7443703b2c5SNicholas Bellinger return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, 7453703b2c5SNicholas Bellinger port_nexus_ptr); 7463703b2c5SNicholas Bellinger default: 7476708bb27SAndy Grover pr_err("Unknown tl_proto_id: 0x%02x, using" 7483703b2c5SNicholas Bellinger " SAS emulation\n", tl_hba->tl_proto_id); 7493703b2c5SNicholas Bellinger break; 7503703b2c5SNicholas Bellinger } 7513703b2c5SNicholas Bellinger 7523703b2c5SNicholas Bellinger return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, 7533703b2c5SNicholas Bellinger port_nexus_ptr); 7543703b2c5SNicholas Bellinger } 7553703b2c5SNicholas Bellinger 7563703b2c5SNicholas Bellinger /* 7573703b2c5SNicholas Bellinger * Returning (1) here allows for target_core_mod struct se_node_acl to be generated 7583703b2c5SNicholas Bellinger * based upon the incoming fabric dependent SCSI Initiator Port 7593703b2c5SNicholas Bellinger */ 7603703b2c5SNicholas Bellinger static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg) 7613703b2c5SNicholas Bellinger { 7623703b2c5SNicholas Bellinger return 1; 7633703b2c5SNicholas Bellinger } 7643703b2c5SNicholas Bellinger 7653703b2c5SNicholas Bellinger static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg) 7663703b2c5SNicholas Bellinger { 7673703b2c5SNicholas Bellinger return 0; 7683703b2c5SNicholas Bellinger } 7693703b2c5SNicholas Bellinger 7703703b2c5SNicholas Bellinger /* 7713703b2c5SNicholas Bellinger * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for 7723703b2c5SNicholas Bellinger * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest 7733703b2c5SNicholas Bellinger */ 7743703b2c5SNicholas Bellinger static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg) 7753703b2c5SNicholas Bellinger { 7763703b2c5SNicholas Bellinger return 0; 7773703b2c5SNicholas Bellinger } 7783703b2c5SNicholas Bellinger 7793703b2c5SNicholas Bellinger /* 7803703b2c5SNicholas Bellinger * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will 7813703b2c5SNicholas Bellinger * never be called for TCM_Loop by target_core_fabric_configfs.c code. 7823703b2c5SNicholas Bellinger * It has been added here as a nop for target_fabric_tf_ops_check() 7833703b2c5SNicholas Bellinger */ 7843703b2c5SNicholas Bellinger static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg) 7853703b2c5SNicholas Bellinger { 7863703b2c5SNicholas Bellinger return 0; 7873703b2c5SNicholas Bellinger } 7883703b2c5SNicholas Bellinger 7893703b2c5SNicholas Bellinger static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl( 7903703b2c5SNicholas Bellinger struct se_portal_group *se_tpg) 7913703b2c5SNicholas Bellinger { 7923703b2c5SNicholas Bellinger struct tcm_loop_nacl *tl_nacl; 7933703b2c5SNicholas Bellinger 7943703b2c5SNicholas Bellinger tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL); 7953703b2c5SNicholas Bellinger if (!tl_nacl) { 7966708bb27SAndy Grover pr_err("Unable to allocate struct tcm_loop_nacl\n"); 7973703b2c5SNicholas Bellinger return NULL; 7983703b2c5SNicholas Bellinger } 7993703b2c5SNicholas Bellinger 8003703b2c5SNicholas Bellinger return &tl_nacl->se_node_acl; 8013703b2c5SNicholas Bellinger } 8023703b2c5SNicholas Bellinger 8033703b2c5SNicholas Bellinger static void tcm_loop_tpg_release_fabric_acl( 8043703b2c5SNicholas Bellinger struct se_portal_group *se_tpg, 8053703b2c5SNicholas Bellinger struct se_node_acl *se_nacl) 8063703b2c5SNicholas Bellinger { 8073703b2c5SNicholas Bellinger struct tcm_loop_nacl *tl_nacl = container_of(se_nacl, 8083703b2c5SNicholas Bellinger struct tcm_loop_nacl, se_node_acl); 8093703b2c5SNicholas Bellinger 8103703b2c5SNicholas Bellinger kfree(tl_nacl); 8113703b2c5SNicholas Bellinger } 8123703b2c5SNicholas Bellinger 8133703b2c5SNicholas Bellinger static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg) 8143703b2c5SNicholas Bellinger { 8153703b2c5SNicholas Bellinger return 1; 8163703b2c5SNicholas Bellinger } 8173703b2c5SNicholas Bellinger 8183703b2c5SNicholas Bellinger static u32 tcm_loop_sess_get_index(struct se_session *se_sess) 8193703b2c5SNicholas Bellinger { 8203703b2c5SNicholas Bellinger return 1; 8213703b2c5SNicholas Bellinger } 8223703b2c5SNicholas Bellinger 8233703b2c5SNicholas Bellinger static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl) 8243703b2c5SNicholas Bellinger { 8253703b2c5SNicholas Bellinger return; 8263703b2c5SNicholas Bellinger } 8273703b2c5SNicholas Bellinger 8283703b2c5SNicholas Bellinger static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd) 8293703b2c5SNicholas Bellinger { 830969871cdSHannes Reinecke struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 831969871cdSHannes Reinecke struct tcm_loop_cmd, tl_se_cmd); 832969871cdSHannes Reinecke 833969871cdSHannes Reinecke return tl_cmd->sc_cmd_tag; 8343703b2c5SNicholas Bellinger } 8353703b2c5SNicholas Bellinger 8363703b2c5SNicholas Bellinger static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd) 8373703b2c5SNicholas Bellinger { 8383703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 8393703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd); 8403703b2c5SNicholas Bellinger 8413703b2c5SNicholas Bellinger return tl_cmd->sc_cmd_state; 8423703b2c5SNicholas Bellinger } 8433703b2c5SNicholas Bellinger 8443703b2c5SNicholas Bellinger static int tcm_loop_shutdown_session(struct se_session *se_sess) 8453703b2c5SNicholas Bellinger { 8463703b2c5SNicholas Bellinger return 0; 8473703b2c5SNicholas Bellinger } 8483703b2c5SNicholas Bellinger 8493703b2c5SNicholas Bellinger static void tcm_loop_close_session(struct se_session *se_sess) 8503703b2c5SNicholas Bellinger { 8513703b2c5SNicholas Bellinger return; 8523703b2c5SNicholas Bellinger }; 8533703b2c5SNicholas Bellinger 8543703b2c5SNicholas Bellinger static int tcm_loop_write_pending(struct se_cmd *se_cmd) 8553703b2c5SNicholas Bellinger { 8563703b2c5SNicholas Bellinger /* 8573703b2c5SNicholas Bellinger * Since Linux/SCSI has already sent down a struct scsi_cmnd 8583703b2c5SNicholas Bellinger * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array 8593703b2c5SNicholas Bellinger * memory, and memory has already been mapped to struct se_cmd->t_mem_list 8603703b2c5SNicholas Bellinger * format with transport_generic_map_mem_to_cmd(). 8613703b2c5SNicholas Bellinger * 8623703b2c5SNicholas Bellinger * We now tell TCM to add this WRITE CDB directly into the TCM storage 8633703b2c5SNicholas Bellinger * object execution queue. 8643703b2c5SNicholas Bellinger */ 86570baf0abSChristoph Hellwig target_execute_cmd(se_cmd); 8663703b2c5SNicholas Bellinger return 0; 8673703b2c5SNicholas Bellinger } 8683703b2c5SNicholas Bellinger 8693703b2c5SNicholas Bellinger static int tcm_loop_write_pending_status(struct se_cmd *se_cmd) 8703703b2c5SNicholas Bellinger { 8713703b2c5SNicholas Bellinger return 0; 8723703b2c5SNicholas Bellinger } 8733703b2c5SNicholas Bellinger 8743703b2c5SNicholas Bellinger static int tcm_loop_queue_data_in(struct se_cmd *se_cmd) 8753703b2c5SNicholas Bellinger { 8763703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 8773703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd); 8783703b2c5SNicholas Bellinger struct scsi_cmnd *sc = tl_cmd->sc; 8793703b2c5SNicholas Bellinger 8806708bb27SAndy Grover pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p" 8813703b2c5SNicholas Bellinger " cdb: 0x%02x\n", sc, sc->cmnd[0]); 8823703b2c5SNicholas Bellinger 8833703b2c5SNicholas Bellinger sc->result = SAM_STAT_GOOD; 8843703b2c5SNicholas Bellinger set_host_byte(sc, DID_OK); 8856cf3fa69SRoland Dreier if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) || 8866cf3fa69SRoland Dreier (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT)) 8876cf3fa69SRoland Dreier scsi_set_resid(sc, se_cmd->residual_count); 8883703b2c5SNicholas Bellinger sc->scsi_done(sc); 8893703b2c5SNicholas Bellinger return 0; 8903703b2c5SNicholas Bellinger } 8913703b2c5SNicholas Bellinger 8923703b2c5SNicholas Bellinger static int tcm_loop_queue_status(struct se_cmd *se_cmd) 8933703b2c5SNicholas Bellinger { 8943703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd, 8953703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd); 8963703b2c5SNicholas Bellinger struct scsi_cmnd *sc = tl_cmd->sc; 8973703b2c5SNicholas Bellinger 8986708bb27SAndy Grover pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p" 8993703b2c5SNicholas Bellinger " cdb: 0x%02x\n", sc, sc->cmnd[0]); 9003703b2c5SNicholas Bellinger 9013703b2c5SNicholas Bellinger if (se_cmd->sense_buffer && 9023703b2c5SNicholas Bellinger ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || 9033703b2c5SNicholas Bellinger (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) { 9043703b2c5SNicholas Bellinger 9055951146dSAndy Grover memcpy(sc->sense_buffer, se_cmd->sense_buffer, 9063703b2c5SNicholas Bellinger SCSI_SENSE_BUFFERSIZE); 9073703b2c5SNicholas Bellinger sc->result = SAM_STAT_CHECK_CONDITION; 9083703b2c5SNicholas Bellinger set_driver_byte(sc, DRIVER_SENSE); 9093703b2c5SNicholas Bellinger } else 9103703b2c5SNicholas Bellinger sc->result = se_cmd->scsi_status; 9113703b2c5SNicholas Bellinger 9123703b2c5SNicholas Bellinger set_host_byte(sc, DID_OK); 9136cf3fa69SRoland Dreier if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) || 9146cf3fa69SRoland Dreier (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT)) 9156cf3fa69SRoland Dreier scsi_set_resid(sc, se_cmd->residual_count); 9163703b2c5SNicholas Bellinger sc->scsi_done(sc); 9173703b2c5SNicholas Bellinger return 0; 9183703b2c5SNicholas Bellinger } 9193703b2c5SNicholas Bellinger 920b79fafacSJoern Engel static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd) 9213703b2c5SNicholas Bellinger { 9223703b2c5SNicholas Bellinger struct se_tmr_req *se_tmr = se_cmd->se_tmr_req; 9233703b2c5SNicholas Bellinger struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr; 9243703b2c5SNicholas Bellinger /* 9253703b2c5SNicholas Bellinger * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead 9263703b2c5SNicholas Bellinger * and wake up the wait_queue_head_t in tcm_loop_device_reset() 9273703b2c5SNicholas Bellinger */ 9283703b2c5SNicholas Bellinger atomic_set(&tl_tmr->tmr_complete, 1); 9293703b2c5SNicholas Bellinger wake_up(&tl_tmr->tl_tmr_wait); 9303703b2c5SNicholas Bellinger } 9313703b2c5SNicholas Bellinger 932131e6abcSNicholas Bellinger static void tcm_loop_aborted_task(struct se_cmd *se_cmd) 933131e6abcSNicholas Bellinger { 934131e6abcSNicholas Bellinger return; 935131e6abcSNicholas Bellinger } 936131e6abcSNicholas Bellinger 9373703b2c5SNicholas Bellinger static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba) 9383703b2c5SNicholas Bellinger { 9393703b2c5SNicholas Bellinger switch (tl_hba->tl_proto_id) { 9403703b2c5SNicholas Bellinger case SCSI_PROTOCOL_SAS: 9413703b2c5SNicholas Bellinger return "SAS"; 9423703b2c5SNicholas Bellinger case SCSI_PROTOCOL_FCP: 9433703b2c5SNicholas Bellinger return "FCP"; 9443703b2c5SNicholas Bellinger case SCSI_PROTOCOL_ISCSI: 9453703b2c5SNicholas Bellinger return "iSCSI"; 9463703b2c5SNicholas Bellinger default: 9473703b2c5SNicholas Bellinger break; 9483703b2c5SNicholas Bellinger } 9493703b2c5SNicholas Bellinger 9503703b2c5SNicholas Bellinger return "Unknown"; 9513703b2c5SNicholas Bellinger } 9523703b2c5SNicholas Bellinger 9533703b2c5SNicholas Bellinger /* Start items for tcm_loop_port_cit */ 9543703b2c5SNicholas Bellinger 9553703b2c5SNicholas Bellinger static int tcm_loop_port_link( 9563703b2c5SNicholas Bellinger struct se_portal_group *se_tpg, 9573703b2c5SNicholas Bellinger struct se_lun *lun) 9583703b2c5SNicholas Bellinger { 9593703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 9603703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg); 9613703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 9623703b2c5SNicholas Bellinger 9633703b2c5SNicholas Bellinger atomic_inc(&tl_tpg->tl_tpg_port_count); 9644e857c58SPeter Zijlstra smp_mb__after_atomic(); 9653703b2c5SNicholas Bellinger /* 9663703b2c5SNicholas Bellinger * Add Linux/SCSI struct scsi_device by HCTL 9673703b2c5SNicholas Bellinger */ 9683703b2c5SNicholas Bellinger scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun); 9693703b2c5SNicholas Bellinger 9706708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n"); 9713703b2c5SNicholas Bellinger return 0; 9723703b2c5SNicholas Bellinger } 9733703b2c5SNicholas Bellinger 9743703b2c5SNicholas Bellinger static void tcm_loop_port_unlink( 9753703b2c5SNicholas Bellinger struct se_portal_group *se_tpg, 9763703b2c5SNicholas Bellinger struct se_lun *se_lun) 9773703b2c5SNicholas Bellinger { 9783703b2c5SNicholas Bellinger struct scsi_device *sd; 9793703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 9803703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg; 9813703b2c5SNicholas Bellinger 9823703b2c5SNicholas Bellinger tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg); 9833703b2c5SNicholas Bellinger tl_hba = tl_tpg->tl_hba; 9843703b2c5SNicholas Bellinger 9853703b2c5SNicholas Bellinger sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt, 9863703b2c5SNicholas Bellinger se_lun->unpacked_lun); 9873703b2c5SNicholas Bellinger if (!sd) { 9886708bb27SAndy Grover pr_err("Unable to locate struct scsi_device for %d:%d:" 9893703b2c5SNicholas Bellinger "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun); 9903703b2c5SNicholas Bellinger return; 9913703b2c5SNicholas Bellinger } 9923703b2c5SNicholas Bellinger /* 9933703b2c5SNicholas Bellinger * Remove Linux/SCSI struct scsi_device by HCTL 9943703b2c5SNicholas Bellinger */ 9953703b2c5SNicholas Bellinger scsi_remove_device(sd); 9963703b2c5SNicholas Bellinger scsi_device_put(sd); 9973703b2c5SNicholas Bellinger 9983703b2c5SNicholas Bellinger atomic_dec(&tl_tpg->tl_tpg_port_count); 9994e857c58SPeter Zijlstra smp_mb__after_atomic(); 10003703b2c5SNicholas Bellinger 10016708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n"); 10023703b2c5SNicholas Bellinger } 10033703b2c5SNicholas Bellinger 10043703b2c5SNicholas Bellinger /* End items for tcm_loop_port_cit */ 10053703b2c5SNicholas Bellinger 10063703b2c5SNicholas Bellinger /* Start items for tcm_loop_nexus_cit */ 10073703b2c5SNicholas Bellinger 10083703b2c5SNicholas Bellinger static int tcm_loop_make_nexus( 10093703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg, 10103703b2c5SNicholas Bellinger const char *name) 10113703b2c5SNicholas Bellinger { 10123703b2c5SNicholas Bellinger struct se_portal_group *se_tpg; 10133703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 10143703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus; 1015552523dcSDan Carpenter int ret = -ENOMEM; 10163703b2c5SNicholas Bellinger 10173703b2c5SNicholas Bellinger if (tl_tpg->tl_hba->tl_nexus) { 10186708bb27SAndy Grover pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n"); 10193703b2c5SNicholas Bellinger return -EEXIST; 10203703b2c5SNicholas Bellinger } 10213703b2c5SNicholas Bellinger se_tpg = &tl_tpg->tl_se_tpg; 10223703b2c5SNicholas Bellinger 10233703b2c5SNicholas Bellinger tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL); 10243703b2c5SNicholas Bellinger if (!tl_nexus) { 10256708bb27SAndy Grover pr_err("Unable to allocate struct tcm_loop_nexus\n"); 10263703b2c5SNicholas Bellinger return -ENOMEM; 10273703b2c5SNicholas Bellinger } 10283703b2c5SNicholas Bellinger /* 10293703b2c5SNicholas Bellinger * Initialize the struct se_session pointer 10303703b2c5SNicholas Bellinger */ 1031e70beee7SNicholas Bellinger tl_nexus->se_sess = transport_init_session(TARGET_PROT_ALL); 1032552523dcSDan Carpenter if (IS_ERR(tl_nexus->se_sess)) { 1033552523dcSDan Carpenter ret = PTR_ERR(tl_nexus->se_sess); 10343703b2c5SNicholas Bellinger goto out; 1035552523dcSDan Carpenter } 10363703b2c5SNicholas Bellinger /* 10373703b2c5SNicholas Bellinger * Since we are running in 'demo mode' this call with generate a 10383703b2c5SNicholas Bellinger * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI 10393703b2c5SNicholas Bellinger * Initiator port name of the passed configfs group 'name'. 10403703b2c5SNicholas Bellinger */ 10413703b2c5SNicholas Bellinger tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl( 10423703b2c5SNicholas Bellinger se_tpg, (unsigned char *)name); 10433703b2c5SNicholas Bellinger if (!tl_nexus->se_sess->se_node_acl) { 10443703b2c5SNicholas Bellinger transport_free_session(tl_nexus->se_sess); 10453703b2c5SNicholas Bellinger goto out; 10463703b2c5SNicholas Bellinger } 10473703b2c5SNicholas Bellinger /* 10483703b2c5SNicholas Bellinger * Now, register the SAS I_T Nexus as active with the call to 10493703b2c5SNicholas Bellinger * transport_register_session() 10503703b2c5SNicholas Bellinger */ 10513703b2c5SNicholas Bellinger __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl, 10525951146dSAndy Grover tl_nexus->se_sess, tl_nexus); 10533703b2c5SNicholas Bellinger tl_tpg->tl_hba->tl_nexus = tl_nexus; 10546708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated" 10553703b2c5SNicholas Bellinger " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba), 10563703b2c5SNicholas Bellinger name); 10573703b2c5SNicholas Bellinger return 0; 10583703b2c5SNicholas Bellinger 10593703b2c5SNicholas Bellinger out: 10603703b2c5SNicholas Bellinger kfree(tl_nexus); 1061552523dcSDan Carpenter return ret; 10623703b2c5SNicholas Bellinger } 10633703b2c5SNicholas Bellinger 10643703b2c5SNicholas Bellinger static int tcm_loop_drop_nexus( 10653703b2c5SNicholas Bellinger struct tcm_loop_tpg *tpg) 10663703b2c5SNicholas Bellinger { 10673703b2c5SNicholas Bellinger struct se_session *se_sess; 10683703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus; 10693703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tpg->tl_hba; 10703703b2c5SNicholas Bellinger 10711ec59feeSHannes Reinecke if (!tl_hba) 10721ec59feeSHannes Reinecke return -ENODEV; 10731ec59feeSHannes Reinecke 10741ec59feeSHannes Reinecke tl_nexus = tl_hba->tl_nexus; 10753703b2c5SNicholas Bellinger if (!tl_nexus) 10763703b2c5SNicholas Bellinger return -ENODEV; 10773703b2c5SNicholas Bellinger 10783703b2c5SNicholas Bellinger se_sess = tl_nexus->se_sess; 10793703b2c5SNicholas Bellinger if (!se_sess) 10803703b2c5SNicholas Bellinger return -ENODEV; 10813703b2c5SNicholas Bellinger 10823703b2c5SNicholas Bellinger if (atomic_read(&tpg->tl_tpg_port_count)) { 10836708bb27SAndy Grover pr_err("Unable to remove TCM_Loop I_T Nexus with" 10843703b2c5SNicholas Bellinger " active TPG port count: %d\n", 10853703b2c5SNicholas Bellinger atomic_read(&tpg->tl_tpg_port_count)); 10863703b2c5SNicholas Bellinger return -EPERM; 10873703b2c5SNicholas Bellinger } 10883703b2c5SNicholas Bellinger 10896708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated" 10903703b2c5SNicholas Bellinger " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba), 10913703b2c5SNicholas Bellinger tl_nexus->se_sess->se_node_acl->initiatorname); 10923703b2c5SNicholas Bellinger /* 10933703b2c5SNicholas Bellinger * Release the SCSI I_T Nexus to the emulated SAS Target Port 10943703b2c5SNicholas Bellinger */ 10953703b2c5SNicholas Bellinger transport_deregister_session(tl_nexus->se_sess); 10963703b2c5SNicholas Bellinger tpg->tl_hba->tl_nexus = NULL; 10973703b2c5SNicholas Bellinger kfree(tl_nexus); 10983703b2c5SNicholas Bellinger return 0; 10993703b2c5SNicholas Bellinger } 11003703b2c5SNicholas Bellinger 11013703b2c5SNicholas Bellinger /* End items for tcm_loop_nexus_cit */ 11023703b2c5SNicholas Bellinger 11033703b2c5SNicholas Bellinger static ssize_t tcm_loop_tpg_show_nexus( 11043703b2c5SNicholas Bellinger struct se_portal_group *se_tpg, 11053703b2c5SNicholas Bellinger char *page) 11063703b2c5SNicholas Bellinger { 11073703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 11083703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg); 11093703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus; 11103703b2c5SNicholas Bellinger ssize_t ret; 11113703b2c5SNicholas Bellinger 11123703b2c5SNicholas Bellinger tl_nexus = tl_tpg->tl_hba->tl_nexus; 11133703b2c5SNicholas Bellinger if (!tl_nexus) 11143703b2c5SNicholas Bellinger return -ENODEV; 11153703b2c5SNicholas Bellinger 11163703b2c5SNicholas Bellinger ret = snprintf(page, PAGE_SIZE, "%s\n", 11173703b2c5SNicholas Bellinger tl_nexus->se_sess->se_node_acl->initiatorname); 11183703b2c5SNicholas Bellinger 11193703b2c5SNicholas Bellinger return ret; 11203703b2c5SNicholas Bellinger } 11213703b2c5SNicholas Bellinger 11223703b2c5SNicholas Bellinger static ssize_t tcm_loop_tpg_store_nexus( 11233703b2c5SNicholas Bellinger struct se_portal_group *se_tpg, 11243703b2c5SNicholas Bellinger const char *page, 11253703b2c5SNicholas Bellinger size_t count) 11263703b2c5SNicholas Bellinger { 11273703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 11283703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg); 11293703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba; 11303703b2c5SNicholas Bellinger unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr; 11313703b2c5SNicholas Bellinger int ret; 11323703b2c5SNicholas Bellinger /* 11333703b2c5SNicholas Bellinger * Shutdown the active I_T nexus if 'NULL' is passed.. 11343703b2c5SNicholas Bellinger */ 11353703b2c5SNicholas Bellinger if (!strncmp(page, "NULL", 4)) { 11363703b2c5SNicholas Bellinger ret = tcm_loop_drop_nexus(tl_tpg); 11373703b2c5SNicholas Bellinger return (!ret) ? count : ret; 11383703b2c5SNicholas Bellinger } 11393703b2c5SNicholas Bellinger /* 11403703b2c5SNicholas Bellinger * Otherwise make sure the passed virtual Initiator port WWN matches 11413703b2c5SNicholas Bellinger * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call 11423703b2c5SNicholas Bellinger * tcm_loop_make_nexus() 11433703b2c5SNicholas Bellinger */ 114460d645a4SDan Carpenter if (strlen(page) >= TL_WWN_ADDR_LEN) { 11456708bb27SAndy Grover pr_err("Emulated NAA Sas Address: %s, exceeds" 11463703b2c5SNicholas Bellinger " max: %d\n", page, TL_WWN_ADDR_LEN); 11473703b2c5SNicholas Bellinger return -EINVAL; 11483703b2c5SNicholas Bellinger } 11493703b2c5SNicholas Bellinger snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page); 11503703b2c5SNicholas Bellinger 11513703b2c5SNicholas Bellinger ptr = strstr(i_port, "naa."); 11523703b2c5SNicholas Bellinger if (ptr) { 11533703b2c5SNicholas Bellinger if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) { 11546708bb27SAndy Grover pr_err("Passed SAS Initiator Port %s does not" 11553703b2c5SNicholas Bellinger " match target port protoid: %s\n", i_port, 11563703b2c5SNicholas Bellinger tcm_loop_dump_proto_id(tl_hba)); 11573703b2c5SNicholas Bellinger return -EINVAL; 11583703b2c5SNicholas Bellinger } 11593703b2c5SNicholas Bellinger port_ptr = &i_port[0]; 11603703b2c5SNicholas Bellinger goto check_newline; 11613703b2c5SNicholas Bellinger } 11623703b2c5SNicholas Bellinger ptr = strstr(i_port, "fc."); 11633703b2c5SNicholas Bellinger if (ptr) { 11643703b2c5SNicholas Bellinger if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) { 11656708bb27SAndy Grover pr_err("Passed FCP Initiator Port %s does not" 11663703b2c5SNicholas Bellinger " match target port protoid: %s\n", i_port, 11673703b2c5SNicholas Bellinger tcm_loop_dump_proto_id(tl_hba)); 11683703b2c5SNicholas Bellinger return -EINVAL; 11693703b2c5SNicholas Bellinger } 11703703b2c5SNicholas Bellinger port_ptr = &i_port[3]; /* Skip over "fc." */ 11713703b2c5SNicholas Bellinger goto check_newline; 11723703b2c5SNicholas Bellinger } 11733703b2c5SNicholas Bellinger ptr = strstr(i_port, "iqn."); 11743703b2c5SNicholas Bellinger if (ptr) { 11753703b2c5SNicholas Bellinger if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) { 11766708bb27SAndy Grover pr_err("Passed iSCSI Initiator Port %s does not" 11773703b2c5SNicholas Bellinger " match target port protoid: %s\n", i_port, 11783703b2c5SNicholas Bellinger tcm_loop_dump_proto_id(tl_hba)); 11793703b2c5SNicholas Bellinger return -EINVAL; 11803703b2c5SNicholas Bellinger } 11813703b2c5SNicholas Bellinger port_ptr = &i_port[0]; 11823703b2c5SNicholas Bellinger goto check_newline; 11833703b2c5SNicholas Bellinger } 11846708bb27SAndy Grover pr_err("Unable to locate prefix for emulated Initiator Port:" 11853703b2c5SNicholas Bellinger " %s\n", i_port); 11863703b2c5SNicholas Bellinger return -EINVAL; 11873703b2c5SNicholas Bellinger /* 11883703b2c5SNicholas Bellinger * Clear any trailing newline for the NAA WWN 11893703b2c5SNicholas Bellinger */ 11903703b2c5SNicholas Bellinger check_newline: 11913703b2c5SNicholas Bellinger if (i_port[strlen(i_port)-1] == '\n') 11923703b2c5SNicholas Bellinger i_port[strlen(i_port)-1] = '\0'; 11933703b2c5SNicholas Bellinger 11943703b2c5SNicholas Bellinger ret = tcm_loop_make_nexus(tl_tpg, port_ptr); 11953703b2c5SNicholas Bellinger if (ret < 0) 11963703b2c5SNicholas Bellinger return ret; 11973703b2c5SNicholas Bellinger 11983703b2c5SNicholas Bellinger return count; 11993703b2c5SNicholas Bellinger } 12003703b2c5SNicholas Bellinger 12013703b2c5SNicholas Bellinger TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR); 12023703b2c5SNicholas Bellinger 1203fb2b2844SHannes Reinecke static ssize_t tcm_loop_tpg_show_transport_status( 1204fb2b2844SHannes Reinecke struct se_portal_group *se_tpg, 1205fb2b2844SHannes Reinecke char *page) 1206fb2b2844SHannes Reinecke { 1207fb2b2844SHannes Reinecke struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 1208fb2b2844SHannes Reinecke struct tcm_loop_tpg, tl_se_tpg); 1209fb2b2844SHannes Reinecke const char *status = NULL; 1210fb2b2844SHannes Reinecke ssize_t ret = -EINVAL; 1211fb2b2844SHannes Reinecke 1212fb2b2844SHannes Reinecke switch (tl_tpg->tl_transport_status) { 1213fb2b2844SHannes Reinecke case TCM_TRANSPORT_ONLINE: 1214fb2b2844SHannes Reinecke status = "online"; 1215fb2b2844SHannes Reinecke break; 1216fb2b2844SHannes Reinecke case TCM_TRANSPORT_OFFLINE: 1217fb2b2844SHannes Reinecke status = "offline"; 1218fb2b2844SHannes Reinecke break; 1219fb2b2844SHannes Reinecke default: 1220fb2b2844SHannes Reinecke break; 1221fb2b2844SHannes Reinecke } 1222fb2b2844SHannes Reinecke 1223fb2b2844SHannes Reinecke if (status) 1224fb2b2844SHannes Reinecke ret = snprintf(page, PAGE_SIZE, "%s\n", status); 1225fb2b2844SHannes Reinecke 1226fb2b2844SHannes Reinecke return ret; 1227fb2b2844SHannes Reinecke } 1228fb2b2844SHannes Reinecke 1229fb2b2844SHannes Reinecke static ssize_t tcm_loop_tpg_store_transport_status( 1230fb2b2844SHannes Reinecke struct se_portal_group *se_tpg, 1231fb2b2844SHannes Reinecke const char *page, 1232fb2b2844SHannes Reinecke size_t count) 1233fb2b2844SHannes Reinecke { 1234fb2b2844SHannes Reinecke struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 1235fb2b2844SHannes Reinecke struct tcm_loop_tpg, tl_se_tpg); 1236fb2b2844SHannes Reinecke 1237fb2b2844SHannes Reinecke if (!strncmp(page, "online", 6)) { 1238fb2b2844SHannes Reinecke tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE; 1239fb2b2844SHannes Reinecke return count; 1240fb2b2844SHannes Reinecke } 1241fb2b2844SHannes Reinecke if (!strncmp(page, "offline", 7)) { 1242fb2b2844SHannes Reinecke tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE; 1243fb2b2844SHannes Reinecke return count; 1244fb2b2844SHannes Reinecke } 1245fb2b2844SHannes Reinecke return -EINVAL; 1246fb2b2844SHannes Reinecke } 1247fb2b2844SHannes Reinecke 1248fb2b2844SHannes Reinecke TF_TPG_BASE_ATTR(tcm_loop, transport_status, S_IRUGO | S_IWUSR); 1249fb2b2844SHannes Reinecke 12503703b2c5SNicholas Bellinger static struct configfs_attribute *tcm_loop_tpg_attrs[] = { 12513703b2c5SNicholas Bellinger &tcm_loop_tpg_nexus.attr, 1252fb2b2844SHannes Reinecke &tcm_loop_tpg_transport_status.attr, 12533703b2c5SNicholas Bellinger NULL, 12543703b2c5SNicholas Bellinger }; 12553703b2c5SNicholas Bellinger 12563703b2c5SNicholas Bellinger /* Start items for tcm_loop_naa_cit */ 12573703b2c5SNicholas Bellinger 1258f0a6c693SRashika Kheria static struct se_portal_group *tcm_loop_make_naa_tpg( 12593703b2c5SNicholas Bellinger struct se_wwn *wwn, 12603703b2c5SNicholas Bellinger struct config_group *group, 12613703b2c5SNicholas Bellinger const char *name) 12623703b2c5SNicholas Bellinger { 12633703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = container_of(wwn, 12643703b2c5SNicholas Bellinger struct tcm_loop_hba, tl_hba_wwn); 12653703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg; 12663703b2c5SNicholas Bellinger char *tpgt_str, *end_ptr; 12673703b2c5SNicholas Bellinger int ret; 12683703b2c5SNicholas Bellinger unsigned short int tpgt; 12693703b2c5SNicholas Bellinger 12703703b2c5SNicholas Bellinger tpgt_str = strstr(name, "tpgt_"); 12713703b2c5SNicholas Bellinger if (!tpgt_str) { 12726708bb27SAndy Grover pr_err("Unable to locate \"tpgt_#\" directory" 12733703b2c5SNicholas Bellinger " group\n"); 12743703b2c5SNicholas Bellinger return ERR_PTR(-EINVAL); 12753703b2c5SNicholas Bellinger } 12763703b2c5SNicholas Bellinger tpgt_str += 5; /* Skip ahead of "tpgt_" */ 12773703b2c5SNicholas Bellinger tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0); 12783703b2c5SNicholas Bellinger 127912f09ccbSDan Carpenter if (tpgt >= TL_TPGS_PER_HBA) { 12806708bb27SAndy Grover pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:" 12813703b2c5SNicholas Bellinger " %u\n", tpgt, TL_TPGS_PER_HBA); 12823703b2c5SNicholas Bellinger return ERR_PTR(-EINVAL); 12833703b2c5SNicholas Bellinger } 12843703b2c5SNicholas Bellinger tl_tpg = &tl_hba->tl_hba_tpgs[tpgt]; 12853703b2c5SNicholas Bellinger tl_tpg->tl_hba = tl_hba; 12863703b2c5SNicholas Bellinger tl_tpg->tl_tpgt = tpgt; 12873703b2c5SNicholas Bellinger /* 12883703b2c5SNicholas Bellinger * Register the tl_tpg as a emulated SAS TCM Target Endpoint 12893703b2c5SNicholas Bellinger */ 12903703b2c5SNicholas Bellinger ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops, 12915951146dSAndy Grover wwn, &tl_tpg->tl_se_tpg, tl_tpg, 12923703b2c5SNicholas Bellinger TRANSPORT_TPG_TYPE_NORMAL); 12933703b2c5SNicholas Bellinger if (ret < 0) 12943703b2c5SNicholas Bellinger return ERR_PTR(-ENOMEM); 12953703b2c5SNicholas Bellinger 12966708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s" 12973703b2c5SNicholas Bellinger " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba), 12983703b2c5SNicholas Bellinger config_item_name(&wwn->wwn_group.cg_item), tpgt); 12993703b2c5SNicholas Bellinger 13003703b2c5SNicholas Bellinger return &tl_tpg->tl_se_tpg; 13013703b2c5SNicholas Bellinger } 13023703b2c5SNicholas Bellinger 1303f0a6c693SRashika Kheria static void tcm_loop_drop_naa_tpg( 13043703b2c5SNicholas Bellinger struct se_portal_group *se_tpg) 13053703b2c5SNicholas Bellinger { 13063703b2c5SNicholas Bellinger struct se_wwn *wwn = se_tpg->se_tpg_wwn; 13073703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, 13083703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg); 13093703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 13103703b2c5SNicholas Bellinger unsigned short tpgt; 13113703b2c5SNicholas Bellinger 13123703b2c5SNicholas Bellinger tl_hba = tl_tpg->tl_hba; 13133703b2c5SNicholas Bellinger tpgt = tl_tpg->tl_tpgt; 13143703b2c5SNicholas Bellinger /* 13153703b2c5SNicholas Bellinger * Release the I_T Nexus for the Virtual SAS link if present 13163703b2c5SNicholas Bellinger */ 13173703b2c5SNicholas Bellinger tcm_loop_drop_nexus(tl_tpg); 13183703b2c5SNicholas Bellinger /* 13193703b2c5SNicholas Bellinger * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint 13203703b2c5SNicholas Bellinger */ 13213703b2c5SNicholas Bellinger core_tpg_deregister(se_tpg); 13223703b2c5SNicholas Bellinger 13230a020436SNicholas Bellinger tl_tpg->tl_hba = NULL; 13240a020436SNicholas Bellinger tl_tpg->tl_tpgt = 0; 13250a020436SNicholas Bellinger 13266708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s" 13273703b2c5SNicholas Bellinger " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba), 13283703b2c5SNicholas Bellinger config_item_name(&wwn->wwn_group.cg_item), tpgt); 13293703b2c5SNicholas Bellinger } 13303703b2c5SNicholas Bellinger 13313703b2c5SNicholas Bellinger /* End items for tcm_loop_naa_cit */ 13323703b2c5SNicholas Bellinger 13333703b2c5SNicholas Bellinger /* Start items for tcm_loop_cit */ 13343703b2c5SNicholas Bellinger 1335f0a6c693SRashika Kheria static struct se_wwn *tcm_loop_make_scsi_hba( 13363703b2c5SNicholas Bellinger struct target_fabric_configfs *tf, 13373703b2c5SNicholas Bellinger struct config_group *group, 13383703b2c5SNicholas Bellinger const char *name) 13393703b2c5SNicholas Bellinger { 13403703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba; 13413703b2c5SNicholas Bellinger struct Scsi_Host *sh; 13423703b2c5SNicholas Bellinger char *ptr; 13433703b2c5SNicholas Bellinger int ret, off = 0; 13443703b2c5SNicholas Bellinger 13453703b2c5SNicholas Bellinger tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL); 13463703b2c5SNicholas Bellinger if (!tl_hba) { 13476708bb27SAndy Grover pr_err("Unable to allocate struct tcm_loop_hba\n"); 13483703b2c5SNicholas Bellinger return ERR_PTR(-ENOMEM); 13493703b2c5SNicholas Bellinger } 13503703b2c5SNicholas Bellinger /* 13513703b2c5SNicholas Bellinger * Determine the emulated Protocol Identifier and Target Port Name 13523703b2c5SNicholas Bellinger * based on the incoming configfs directory name. 13533703b2c5SNicholas Bellinger */ 13543703b2c5SNicholas Bellinger ptr = strstr(name, "naa."); 13553703b2c5SNicholas Bellinger if (ptr) { 13563703b2c5SNicholas Bellinger tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS; 13573703b2c5SNicholas Bellinger goto check_len; 13583703b2c5SNicholas Bellinger } 13593703b2c5SNicholas Bellinger ptr = strstr(name, "fc."); 13603703b2c5SNicholas Bellinger if (ptr) { 13613703b2c5SNicholas Bellinger tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP; 13623703b2c5SNicholas Bellinger off = 3; /* Skip over "fc." */ 13633703b2c5SNicholas Bellinger goto check_len; 13643703b2c5SNicholas Bellinger } 13653703b2c5SNicholas Bellinger ptr = strstr(name, "iqn."); 1366a57b5d36SJesper Juhl if (!ptr) { 13676708bb27SAndy Grover pr_err("Unable to locate prefix for emulated Target " 1368a57b5d36SJesper Juhl "Port: %s\n", name); 1369a57b5d36SJesper Juhl ret = -EINVAL; 1370a57b5d36SJesper Juhl goto out; 13713703b2c5SNicholas Bellinger } 1372a57b5d36SJesper Juhl tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI; 13733703b2c5SNicholas Bellinger 13743703b2c5SNicholas Bellinger check_len: 137560d645a4SDan Carpenter if (strlen(name) >= TL_WWN_ADDR_LEN) { 13766708bb27SAndy Grover pr_err("Emulated NAA %s Address: %s, exceeds" 13773703b2c5SNicholas Bellinger " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba), 13783703b2c5SNicholas Bellinger TL_WWN_ADDR_LEN); 1379a57b5d36SJesper Juhl ret = -EINVAL; 1380a57b5d36SJesper Juhl goto out; 13813703b2c5SNicholas Bellinger } 13823703b2c5SNicholas Bellinger snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]); 13833703b2c5SNicholas Bellinger 13843703b2c5SNicholas Bellinger /* 13853703b2c5SNicholas Bellinger * Call device_register(tl_hba->dev) to register the emulated 13863703b2c5SNicholas Bellinger * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after 13873703b2c5SNicholas Bellinger * device_register() callbacks in tcm_loop_driver_probe() 13883703b2c5SNicholas Bellinger */ 13893703b2c5SNicholas Bellinger ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt); 13903703b2c5SNicholas Bellinger if (ret) 13913703b2c5SNicholas Bellinger goto out; 13923703b2c5SNicholas Bellinger 13933703b2c5SNicholas Bellinger sh = tl_hba->sh; 13943703b2c5SNicholas Bellinger tcm_loop_hba_no_cnt++; 13956708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target" 13963703b2c5SNicholas Bellinger " %s Address: %s at Linux/SCSI Host ID: %d\n", 13973703b2c5SNicholas Bellinger tcm_loop_dump_proto_id(tl_hba), name, sh->host_no); 13983703b2c5SNicholas Bellinger 13993703b2c5SNicholas Bellinger return &tl_hba->tl_hba_wwn; 14003703b2c5SNicholas Bellinger out: 14013703b2c5SNicholas Bellinger kfree(tl_hba); 14023703b2c5SNicholas Bellinger return ERR_PTR(ret); 14033703b2c5SNicholas Bellinger } 14043703b2c5SNicholas Bellinger 1405f0a6c693SRashika Kheria static void tcm_loop_drop_scsi_hba( 14063703b2c5SNicholas Bellinger struct se_wwn *wwn) 14073703b2c5SNicholas Bellinger { 14083703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = container_of(wwn, 14093703b2c5SNicholas Bellinger struct tcm_loop_hba, tl_hba_wwn); 14106297b07cSNicholas Bellinger 14116297b07cSNicholas Bellinger pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target" 14126297b07cSNicholas Bellinger " SAS Address: %s at Linux/SCSI Host ID: %d\n", 14136297b07cSNicholas Bellinger tl_hba->tl_wwn_address, tl_hba->sh->host_no); 14143703b2c5SNicholas Bellinger /* 14153703b2c5SNicholas Bellinger * Call device_unregister() on the original tl_hba->dev. 14163703b2c5SNicholas Bellinger * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will 14173703b2c5SNicholas Bellinger * release *tl_hba; 14183703b2c5SNicholas Bellinger */ 14193703b2c5SNicholas Bellinger device_unregister(&tl_hba->dev); 14203703b2c5SNicholas Bellinger } 14213703b2c5SNicholas Bellinger 14223703b2c5SNicholas Bellinger /* Start items for tcm_loop_cit */ 14233703b2c5SNicholas Bellinger static ssize_t tcm_loop_wwn_show_attr_version( 14243703b2c5SNicholas Bellinger struct target_fabric_configfs *tf, 14253703b2c5SNicholas Bellinger char *page) 14263703b2c5SNicholas Bellinger { 14273703b2c5SNicholas Bellinger return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION); 14283703b2c5SNicholas Bellinger } 14293703b2c5SNicholas Bellinger 14303703b2c5SNicholas Bellinger TF_WWN_ATTR_RO(tcm_loop, version); 14313703b2c5SNicholas Bellinger 14323703b2c5SNicholas Bellinger static struct configfs_attribute *tcm_loop_wwn_attrs[] = { 14333703b2c5SNicholas Bellinger &tcm_loop_wwn_version.attr, 14343703b2c5SNicholas Bellinger NULL, 14353703b2c5SNicholas Bellinger }; 14363703b2c5SNicholas Bellinger 14373703b2c5SNicholas Bellinger /* End items for tcm_loop_cit */ 14383703b2c5SNicholas Bellinger 14393703b2c5SNicholas Bellinger static int tcm_loop_register_configfs(void) 14403703b2c5SNicholas Bellinger { 14413703b2c5SNicholas Bellinger struct target_fabric_configfs *fabric; 14423703b2c5SNicholas Bellinger int ret; 14433703b2c5SNicholas Bellinger /* 14443703b2c5SNicholas Bellinger * Set the TCM Loop HBA counter to zero 14453703b2c5SNicholas Bellinger */ 14463703b2c5SNicholas Bellinger tcm_loop_hba_no_cnt = 0; 14473703b2c5SNicholas Bellinger /* 14483703b2c5SNicholas Bellinger * Register the top level struct config_item_type with TCM core 14493703b2c5SNicholas Bellinger */ 14503703b2c5SNicholas Bellinger fabric = target_fabric_configfs_init(THIS_MODULE, "loopback"); 1451e3d6f909SAndy Grover if (IS_ERR(fabric)) { 14526708bb27SAndy Grover pr_err("tcm_loop_register_configfs() failed!\n"); 1453e3d6f909SAndy Grover return PTR_ERR(fabric); 14543703b2c5SNicholas Bellinger } 14553703b2c5SNicholas Bellinger /* 14563703b2c5SNicholas Bellinger * Setup the fabric API of function pointers used by target_core_mod 14573703b2c5SNicholas Bellinger */ 14583703b2c5SNicholas Bellinger fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name; 14593703b2c5SNicholas Bellinger fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident; 14603703b2c5SNicholas Bellinger fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn; 14613703b2c5SNicholas Bellinger fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag; 14623703b2c5SNicholas Bellinger fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth; 14633703b2c5SNicholas Bellinger fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id; 14643703b2c5SNicholas Bellinger fabric->tf_ops.tpg_get_pr_transport_id_len = 14653703b2c5SNicholas Bellinger &tcm_loop_get_pr_transport_id_len; 14663703b2c5SNicholas Bellinger fabric->tf_ops.tpg_parse_pr_out_transport_id = 14673703b2c5SNicholas Bellinger &tcm_loop_parse_pr_out_transport_id; 14683703b2c5SNicholas Bellinger fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode; 14693703b2c5SNicholas Bellinger fabric->tf_ops.tpg_check_demo_mode_cache = 14703703b2c5SNicholas Bellinger &tcm_loop_check_demo_mode_cache; 14713703b2c5SNicholas Bellinger fabric->tf_ops.tpg_check_demo_mode_write_protect = 14723703b2c5SNicholas Bellinger &tcm_loop_check_demo_mode_write_protect; 14733703b2c5SNicholas Bellinger fabric->tf_ops.tpg_check_prod_mode_write_protect = 14743703b2c5SNicholas Bellinger &tcm_loop_check_prod_mode_write_protect; 14753703b2c5SNicholas Bellinger /* 14763703b2c5SNicholas Bellinger * The TCM loopback fabric module runs in demo-mode to a local 14773703b2c5SNicholas Bellinger * virtual SCSI device, so fabric dependent initator ACLs are 14783703b2c5SNicholas Bellinger * not required. 14793703b2c5SNicholas Bellinger */ 14803703b2c5SNicholas Bellinger fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl; 14813703b2c5SNicholas Bellinger fabric->tf_ops.tpg_release_fabric_acl = 14823703b2c5SNicholas Bellinger &tcm_loop_tpg_release_fabric_acl; 14833703b2c5SNicholas Bellinger fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index; 14843703b2c5SNicholas Bellinger /* 14853703b2c5SNicholas Bellinger * Used for setting up remaining TCM resources in process context 14863703b2c5SNicholas Bellinger */ 14873703b2c5SNicholas Bellinger fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free; 148835462975SChristoph Hellwig fabric->tf_ops.release_cmd = &tcm_loop_release_cmd; 14893703b2c5SNicholas Bellinger fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session; 14903703b2c5SNicholas Bellinger fabric->tf_ops.close_session = &tcm_loop_close_session; 14913703b2c5SNicholas Bellinger fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index; 14923703b2c5SNicholas Bellinger fabric->tf_ops.sess_get_initiator_sid = NULL; 14933703b2c5SNicholas Bellinger fabric->tf_ops.write_pending = &tcm_loop_write_pending; 14943703b2c5SNicholas Bellinger fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status; 14953703b2c5SNicholas Bellinger /* 14963703b2c5SNicholas Bellinger * Not used for TCM loopback 14973703b2c5SNicholas Bellinger */ 14983703b2c5SNicholas Bellinger fabric->tf_ops.set_default_node_attributes = 14993703b2c5SNicholas Bellinger &tcm_loop_set_default_node_attributes; 15003703b2c5SNicholas Bellinger fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag; 15013703b2c5SNicholas Bellinger fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state; 15023703b2c5SNicholas Bellinger fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in; 15033703b2c5SNicholas Bellinger fabric->tf_ops.queue_status = &tcm_loop_queue_status; 15043703b2c5SNicholas Bellinger fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp; 1505131e6abcSNicholas Bellinger fabric->tf_ops.aborted_task = &tcm_loop_aborted_task; 15063703b2c5SNicholas Bellinger 15073703b2c5SNicholas Bellinger /* 15083703b2c5SNicholas Bellinger * Setup function pointers for generic logic in target_core_fabric_configfs.c 15093703b2c5SNicholas Bellinger */ 15103703b2c5SNicholas Bellinger fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba; 15113703b2c5SNicholas Bellinger fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba; 15123703b2c5SNicholas Bellinger fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg; 15133703b2c5SNicholas Bellinger fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg; 15143703b2c5SNicholas Bellinger /* 15153703b2c5SNicholas Bellinger * fabric_post_link() and fabric_pre_unlink() are used for 15163703b2c5SNicholas Bellinger * registration and release of TCM Loop Virtual SCSI LUNs. 15173703b2c5SNicholas Bellinger */ 15183703b2c5SNicholas Bellinger fabric->tf_ops.fabric_post_link = &tcm_loop_port_link; 15193703b2c5SNicholas Bellinger fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink; 15203703b2c5SNicholas Bellinger fabric->tf_ops.fabric_make_np = NULL; 15213703b2c5SNicholas Bellinger fabric->tf_ops.fabric_drop_np = NULL; 15223703b2c5SNicholas Bellinger /* 15233703b2c5SNicholas Bellinger * Setup default attribute lists for various fabric->tf_cit_tmpl 15243703b2c5SNicholas Bellinger */ 1525d80e224dSAndy Grover fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs; 1526d80e224dSAndy Grover fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs; 1527d80e224dSAndy Grover fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL; 1528d80e224dSAndy Grover fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL; 1529d80e224dSAndy Grover fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL; 15303703b2c5SNicholas Bellinger /* 15313703b2c5SNicholas Bellinger * Once fabric->tf_ops has been setup, now register the fabric for 15323703b2c5SNicholas Bellinger * use within TCM 15333703b2c5SNicholas Bellinger */ 15343703b2c5SNicholas Bellinger ret = target_fabric_configfs_register(fabric); 15353703b2c5SNicholas Bellinger if (ret < 0) { 15366708bb27SAndy Grover pr_err("target_fabric_configfs_register() for" 15373703b2c5SNicholas Bellinger " TCM_Loop failed!\n"); 15383703b2c5SNicholas Bellinger target_fabric_configfs_free(fabric); 15393703b2c5SNicholas Bellinger return -1; 15403703b2c5SNicholas Bellinger } 15413703b2c5SNicholas Bellinger /* 15423703b2c5SNicholas Bellinger * Setup our local pointer to *fabric. 15433703b2c5SNicholas Bellinger */ 15443703b2c5SNicholas Bellinger tcm_loop_fabric_configfs = fabric; 15456708bb27SAndy Grover pr_debug("TCM_LOOP[0] - Set fabric ->" 15463703b2c5SNicholas Bellinger " tcm_loop_fabric_configfs\n"); 15473703b2c5SNicholas Bellinger return 0; 15483703b2c5SNicholas Bellinger } 15493703b2c5SNicholas Bellinger 15503703b2c5SNicholas Bellinger static void tcm_loop_deregister_configfs(void) 15513703b2c5SNicholas Bellinger { 15523703b2c5SNicholas Bellinger if (!tcm_loop_fabric_configfs) 15533703b2c5SNicholas Bellinger return; 15543703b2c5SNicholas Bellinger 15553703b2c5SNicholas Bellinger target_fabric_configfs_deregister(tcm_loop_fabric_configfs); 15563703b2c5SNicholas Bellinger tcm_loop_fabric_configfs = NULL; 15576708bb27SAndy Grover pr_debug("TCM_LOOP[0] - Cleared" 15583703b2c5SNicholas Bellinger " tcm_loop_fabric_configfs\n"); 15593703b2c5SNicholas Bellinger } 15603703b2c5SNicholas Bellinger 15613703b2c5SNicholas Bellinger static int __init tcm_loop_fabric_init(void) 15623703b2c5SNicholas Bellinger { 1563afe2cb7fSChristoph Hellwig int ret = -ENOMEM; 1564afe2cb7fSChristoph Hellwig 1565afe2cb7fSChristoph Hellwig tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0); 1566afe2cb7fSChristoph Hellwig if (!tcm_loop_workqueue) 1567afe2cb7fSChristoph Hellwig goto out; 15683703b2c5SNicholas Bellinger 15693703b2c5SNicholas Bellinger tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache", 15703703b2c5SNicholas Bellinger sizeof(struct tcm_loop_cmd), 15713703b2c5SNicholas Bellinger __alignof__(struct tcm_loop_cmd), 15723703b2c5SNicholas Bellinger 0, NULL); 15733703b2c5SNicholas Bellinger if (!tcm_loop_cmd_cache) { 15746708bb27SAndy Grover pr_debug("kmem_cache_create() for" 15753703b2c5SNicholas Bellinger " tcm_loop_cmd_cache failed\n"); 1576afe2cb7fSChristoph Hellwig goto out_destroy_workqueue; 15773703b2c5SNicholas Bellinger } 15783703b2c5SNicholas Bellinger 15793703b2c5SNicholas Bellinger ret = tcm_loop_alloc_core_bus(); 15803703b2c5SNicholas Bellinger if (ret) 1581afe2cb7fSChristoph Hellwig goto out_destroy_cache; 15823703b2c5SNicholas Bellinger 15833703b2c5SNicholas Bellinger ret = tcm_loop_register_configfs(); 1584afe2cb7fSChristoph Hellwig if (ret) 1585afe2cb7fSChristoph Hellwig goto out_release_core_bus; 15863703b2c5SNicholas Bellinger 15873703b2c5SNicholas Bellinger return 0; 1588afe2cb7fSChristoph Hellwig 1589afe2cb7fSChristoph Hellwig out_release_core_bus: 1590afe2cb7fSChristoph Hellwig tcm_loop_release_core_bus(); 1591afe2cb7fSChristoph Hellwig out_destroy_cache: 1592afe2cb7fSChristoph Hellwig kmem_cache_destroy(tcm_loop_cmd_cache); 1593afe2cb7fSChristoph Hellwig out_destroy_workqueue: 1594afe2cb7fSChristoph Hellwig destroy_workqueue(tcm_loop_workqueue); 1595afe2cb7fSChristoph Hellwig out: 1596afe2cb7fSChristoph Hellwig return ret; 15973703b2c5SNicholas Bellinger } 15983703b2c5SNicholas Bellinger 15993703b2c5SNicholas Bellinger static void __exit tcm_loop_fabric_exit(void) 16003703b2c5SNicholas Bellinger { 16013703b2c5SNicholas Bellinger tcm_loop_deregister_configfs(); 16023703b2c5SNicholas Bellinger tcm_loop_release_core_bus(); 16033703b2c5SNicholas Bellinger kmem_cache_destroy(tcm_loop_cmd_cache); 1604afe2cb7fSChristoph Hellwig destroy_workqueue(tcm_loop_workqueue); 16053703b2c5SNicholas Bellinger } 16063703b2c5SNicholas Bellinger 16073703b2c5SNicholas Bellinger MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module"); 16083703b2c5SNicholas Bellinger MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>"); 16093703b2c5SNicholas Bellinger MODULE_LICENSE("GPL"); 16103703b2c5SNicholas Bellinger module_init(tcm_loop_fabric_init); 16113703b2c5SNicholas Bellinger module_exit(tcm_loop_fabric_exit); 1612