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
383703b2c5SNicholas Bellinger #include "tcm_loop.h"
393703b2c5SNicholas Bellinger
403703b2c5SNicholas Bellinger #define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev)
413703b2c5SNicholas Bellinger
423703b2c5SNicholas Bellinger static struct kmem_cache *tcm_loop_cmd_cache;
433703b2c5SNicholas Bellinger
443703b2c5SNicholas Bellinger static int tcm_loop_hba_no_cnt;
453703b2c5SNicholas Bellinger
4616786454SChristoph Hellwig static int tcm_loop_queue_status(struct se_cmd *se_cmd);
473703b2c5SNicholas Bellinger
4894a0dfcfSMike Christie static unsigned int tcm_loop_nr_hw_queues = 1;
4994a0dfcfSMike Christie module_param_named(nr_hw_queues, tcm_loop_nr_hw_queues, uint, 0644);
5094a0dfcfSMike Christie
5194a0dfcfSMike Christie static unsigned int tcm_loop_can_queue = 1024;
5294a0dfcfSMike Christie module_param_named(can_queue, tcm_loop_can_queue, uint, 0644);
5394a0dfcfSMike Christie
5494a0dfcfSMike Christie static unsigned int tcm_loop_cmd_per_lun = 1024;
5594a0dfcfSMike Christie module_param_named(cmd_per_lun, tcm_loop_cmd_per_lun, uint, 0644);
5694a0dfcfSMike Christie
573703b2c5SNicholas Bellinger /*
583703b2c5SNicholas Bellinger * Called from struct target_core_fabric_ops->check_stop_free()
593703b2c5SNicholas Bellinger */
tcm_loop_check_stop_free(struct se_cmd * se_cmd)6088dd9e26SNicholas Bellinger static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
613703b2c5SNicholas Bellinger {
624c1f0e65SBart Van Assche return transport_generic_free_cmd(se_cmd, 0);
633703b2c5SNicholas Bellinger }
643703b2c5SNicholas Bellinger
tcm_loop_release_cmd(struct se_cmd * se_cmd)6535462975SChristoph Hellwig static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
663703b2c5SNicholas Bellinger {
673703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
683703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd);
69e0eb5d38SMike Christie struct scsi_cmnd *sc = tl_cmd->sc;
703703b2c5SNicholas Bellinger
71e0eb5d38SMike Christie if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
723703b2c5SNicholas Bellinger kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
73e0eb5d38SMike Christie else
74b9d82b7dSBart Van Assche scsi_done(sc);
753703b2c5SNicholas Bellinger }
763703b2c5SNicholas Bellinger
tcm_loop_show_info(struct seq_file * m,struct Scsi_Host * host)778946b077SAl Viro static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host)
783703b2c5SNicholas Bellinger {
7909f99a3dSMarkus Elfring seq_puts(m, "tcm_loop_proc_info()\n");
808946b077SAl Viro return 0;
813703b2c5SNicholas Bellinger }
823703b2c5SNicholas Bellinger
833703b2c5SNicholas Bellinger static int tcm_loop_driver_probe(struct device *);
84fc7a6209SUwe Kleine-König static void tcm_loop_driver_remove(struct device *);
853703b2c5SNicholas Bellinger
863703b2c5SNicholas Bellinger static struct bus_type tcm_loop_lld_bus = {
873703b2c5SNicholas Bellinger .name = "tcm_loop_bus",
883703b2c5SNicholas Bellinger .probe = tcm_loop_driver_probe,
893703b2c5SNicholas Bellinger .remove = tcm_loop_driver_remove,
903703b2c5SNicholas Bellinger };
913703b2c5SNicholas Bellinger
923703b2c5SNicholas Bellinger static struct device_driver tcm_loop_driverfs = {
933703b2c5SNicholas Bellinger .name = "tcm_loop",
943703b2c5SNicholas Bellinger .bus = &tcm_loop_lld_bus,
953703b2c5SNicholas Bellinger };
963703b2c5SNicholas Bellinger /*
973703b2c5SNicholas Bellinger * Used with root_device_register() in tcm_loop_alloc_core_bus() below
983703b2c5SNicholas Bellinger */
996e1a27b9SChristoph Hellwig static struct device *tcm_loop_primary;
1003703b2c5SNicholas Bellinger
tcm_loop_target_queue_cmd(struct tcm_loop_cmd * tl_cmd)1011130b499SMike Christie static void tcm_loop_target_queue_cmd(struct tcm_loop_cmd *tl_cmd)
1023703b2c5SNicholas Bellinger {
103afe2cb7fSChristoph Hellwig struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
104afe2cb7fSChristoph Hellwig struct scsi_cmnd *sc = tl_cmd->sc;
105afe2cb7fSChristoph Hellwig struct tcm_loop_nexus *tl_nexus;
1063703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
1073703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg;
10816786454SChristoph Hellwig struct scatterlist *sgl_bidi = NULL;
109e2a4f55cSSagi Grimberg u32 sgl_bidi_count = 0, transfer_length;
110f872c9f4SChristoph Hellwig
1113703b2c5SNicholas Bellinger tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
1123703b2c5SNicholas Bellinger tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
113afe2cb7fSChristoph Hellwig
1140a020436SNicholas Bellinger /*
1150a020436SNicholas Bellinger * Ensure that this tl_tpg reference from the incoming sc->device->id
1160a020436SNicholas Bellinger * has already been configured via tcm_loop_make_naa_tpg().
1170a020436SNicholas Bellinger */
1180a020436SNicholas Bellinger if (!tl_tpg->tl_hba) {
1190a020436SNicholas Bellinger set_host_byte(sc, DID_NO_CONNECT);
120f872c9f4SChristoph Hellwig goto out_done;
1210a020436SNicholas Bellinger }
122fb2b2844SHannes Reinecke if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) {
123fb2b2844SHannes Reinecke set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
124fb2b2844SHannes Reinecke goto out_done;
125fb2b2844SHannes Reinecke }
126506787a2SHannes Reinecke tl_nexus = tl_tpg->tl_nexus;
127f872c9f4SChristoph Hellwig if (!tl_nexus) {
128c8d1f4b7SMarkus Elfring scmd_printk(KERN_ERR, sc,
129c8d1f4b7SMarkus Elfring "TCM_Loop I_T Nexus does not exist\n");
130f872c9f4SChristoph Hellwig set_host_byte(sc, DID_ERROR);
131f872c9f4SChristoph Hellwig goto out_done;
1323703b2c5SNicholas Bellinger }
133b5b8e298SSagi Grimberg
134e2a4f55cSSagi Grimberg transfer_length = scsi_transfer_length(sc);
135e2a4f55cSSagi Grimberg if (!scsi_prot_sg_count(sc) &&
136e2a4f55cSSagi Grimberg scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) {
137b5b8e298SSagi Grimberg se_cmd->prot_pto = true;
138e2a4f55cSSagi Grimberg /*
139e2a4f55cSSagi Grimberg * loopback transport doesn't support
140e2a4f55cSSagi Grimberg * WRITE_GENERATE, READ_STRIP protection
141e2a4f55cSSagi Grimberg * information operations, go ahead unprotected.
142e2a4f55cSSagi Grimberg */
143e2a4f55cSSagi Grimberg transfer_length = scsi_bufflen(sc);
144e2a4f55cSSagi Grimberg }
145b5b8e298SSagi Grimberg
146649ee054SBart Van Assche se_cmd->tag = tl_cmd->sc_cmd_tag;
14717ae18a6SMike Christie target_init_cmd(se_cmd, tl_nexus->se_sess, &tl_cmd->tl_sense_buf[0],
14817ae18a6SMike Christie tl_cmd->sc->device->lun, transfer_length,
14917ae18a6SMike Christie TCM_SIMPLE_TAG, sc->sc_data_direction, 0);
15017ae18a6SMike Christie
15117ae18a6SMike Christie if (target_submit_prep(se_cmd, sc->cmnd, scsi_sglist(sc),
15217ae18a6SMike Christie scsi_sg_count(sc), sgl_bidi, sgl_bidi_count,
15308694199SMike Christie scsi_prot_sglist(sc), scsi_prot_sg_count(sc),
1541130b499SMike Christie GFP_ATOMIC))
15517ae18a6SMike Christie return;
15617ae18a6SMike Christie
1571130b499SMike Christie target_queue_submission(se_cmd);
158afe2cb7fSChristoph Hellwig return;
159f872c9f4SChristoph Hellwig
160f872c9f4SChristoph Hellwig out_done:
161b9d82b7dSBart Van Assche scsi_done(sc);
162afe2cb7fSChristoph Hellwig }
163afe2cb7fSChristoph Hellwig
164afe2cb7fSChristoph Hellwig /*
165afe2cb7fSChristoph Hellwig * ->queuecommand can be and usually is called from interrupt context, so
166afe2cb7fSChristoph Hellwig * defer the actual submission to a workqueue.
167afe2cb7fSChristoph Hellwig */
tcm_loop_queuecommand(struct Scsi_Host * sh,struct scsi_cmnd * sc)168afe2cb7fSChristoph Hellwig static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
169afe2cb7fSChristoph Hellwig {
170e0eb5d38SMike Christie struct tcm_loop_cmd *tl_cmd = scsi_cmd_priv(sc);
171afe2cb7fSChristoph Hellwig
172c8d1f4b7SMarkus Elfring pr_debug("%s() %d:%d:%d:%llu got CDB: 0x%02x scsi_buf_len: %u\n",
173c8d1f4b7SMarkus Elfring __func__, sc->device->host->host_no, sc->device->id,
174c8d1f4b7SMarkus Elfring sc->device->channel, sc->device->lun, sc->cmnd[0],
175c8d1f4b7SMarkus Elfring scsi_bufflen(sc));
176afe2cb7fSChristoph Hellwig
177e0eb5d38SMike Christie memset(tl_cmd, 0, sizeof(*tl_cmd));
178afe2cb7fSChristoph Hellwig tl_cmd->sc = sc;
179cb22f89eSBart Van Assche tl_cmd->sc_cmd_tag = scsi_cmd_to_rq(sc)->tag;
1801130b499SMike Christie
1811130b499SMike Christie tcm_loop_target_queue_cmd(tl_cmd);
182f872c9f4SChristoph Hellwig return 0;
1833703b2c5SNicholas Bellinger }
1843703b2c5SNicholas Bellinger
1853703b2c5SNicholas Bellinger /*
1863703b2c5SNicholas Bellinger * Called from SCSI EH process context to issue a LUN_RESET TMR
1873703b2c5SNicholas Bellinger * to struct scsi_device
1883703b2c5SNicholas Bellinger */
tcm_loop_issue_tmr(struct tcm_loop_tpg * tl_tpg,u64 lun,int task,enum tcm_tmreq_table tmr)189a314d700SHannes Reinecke static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg,
190f2d30680SHannes Reinecke u64 lun, int task, enum tcm_tmreq_table tmr)
1913703b2c5SNicholas Bellinger {
1927deeceb4SMarkus Elfring struct se_cmd *se_cmd;
1933703b2c5SNicholas Bellinger struct se_session *se_sess;
194506787a2SHannes Reinecke struct tcm_loop_nexus *tl_nexus;
1957deeceb4SMarkus Elfring struct tcm_loop_cmd *tl_cmd;
196a314d700SHannes Reinecke int ret = TMR_FUNCTION_FAILED, rc;
197a314d700SHannes Reinecke
198506787a2SHannes Reinecke /*
199506787a2SHannes Reinecke * Locate the tl_nexus and se_sess pointers
200506787a2SHannes Reinecke */
201506787a2SHannes Reinecke tl_nexus = tl_tpg->tl_nexus;
202506787a2SHannes Reinecke if (!tl_nexus) {
203c8d1f4b7SMarkus Elfring pr_err("Unable to perform device reset without active I_T Nexus\n");
204506787a2SHannes Reinecke return ret;
205506787a2SHannes Reinecke }
206506787a2SHannes Reinecke
207a314d700SHannes Reinecke tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
208cd3fb32aSMarkus Elfring if (!tl_cmd)
209a314d700SHannes Reinecke return ret;
210a314d700SHannes Reinecke
211d17203c4SBart Van Assche init_completion(&tl_cmd->tmr_done);
212a314d700SHannes Reinecke
213a314d700SHannes Reinecke se_cmd = &tl_cmd->tl_se_cmd;
214506787a2SHannes Reinecke se_sess = tl_tpg->tl_nexus->se_sess;
215a314d700SHannes Reinecke
21675f141aaSBart Van Assche rc = target_submit_tmr(se_cmd, se_sess, tl_cmd->tl_sense_buf, lun,
2174c1f0e65SBart Van Assche NULL, tmr, GFP_KERNEL, task,
2184c1f0e65SBart Van Assche TARGET_SCF_ACK_KREF);
219a314d700SHannes Reinecke if (rc < 0)
220a314d700SHannes Reinecke goto release;
221d17203c4SBart Van Assche wait_for_completion(&tl_cmd->tmr_done);
222a314d700SHannes Reinecke ret = se_cmd->se_tmr_req->response;
2234c1f0e65SBart Van Assche target_put_sess_cmd(se_cmd);
2244c1f0e65SBart Van Assche
2254c1f0e65SBart Van Assche out:
2264c1f0e65SBart Van Assche return ret;
22775f141aaSBart Van Assche
228a314d700SHannes Reinecke release:
229a314d700SHannes Reinecke kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
2304c1f0e65SBart Van Assche goto out;
231a314d700SHannes Reinecke }
232a314d700SHannes Reinecke
tcm_loop_abort_task(struct scsi_cmnd * sc)233969871cdSHannes Reinecke static int tcm_loop_abort_task(struct scsi_cmnd *sc)
234969871cdSHannes Reinecke {
235969871cdSHannes Reinecke struct tcm_loop_hba *tl_hba;
236969871cdSHannes Reinecke struct tcm_loop_tpg *tl_tpg;
2378f13142aSColin Ian King int ret;
238969871cdSHannes Reinecke
239969871cdSHannes Reinecke /*
240969871cdSHannes Reinecke * Locate the tcm_loop_hba_t pointer
241969871cdSHannes Reinecke */
242969871cdSHannes Reinecke tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
243969871cdSHannes Reinecke tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
244506787a2SHannes Reinecke ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
245cb22f89eSBart Van Assche scsi_cmd_to_rq(sc)->tag, TMR_ABORT_TASK);
246969871cdSHannes Reinecke return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
247969871cdSHannes Reinecke }
248969871cdSHannes Reinecke
249a314d700SHannes Reinecke /*
250a314d700SHannes Reinecke * Called from SCSI EH process context to issue a LUN_RESET TMR
251a314d700SHannes Reinecke * to struct scsi_device
252a314d700SHannes Reinecke */
tcm_loop_device_reset(struct scsi_cmnd * sc)253a314d700SHannes Reinecke static int tcm_loop_device_reset(struct scsi_cmnd *sc)
254a314d700SHannes Reinecke {
2553703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
2563703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg;
2578f13142aSColin Ian King int ret;
258a314d700SHannes Reinecke
2593703b2c5SNicholas Bellinger /*
2603703b2c5SNicholas Bellinger * Locate the tcm_loop_hba_t pointer
2613703b2c5SNicholas Bellinger */
2623703b2c5SNicholas Bellinger tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
2633703b2c5SNicholas Bellinger tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
264506787a2SHannes Reinecke
265506787a2SHannes Reinecke ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
266969871cdSHannes Reinecke 0, TMR_LUN_RESET);
267a314d700SHannes Reinecke return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
2683703b2c5SNicholas Bellinger }
2693703b2c5SNicholas Bellinger
tcm_loop_target_reset(struct scsi_cmnd * sc)2708f4a1fb0SHannes Reinecke static int tcm_loop_target_reset(struct scsi_cmnd *sc)
2718f4a1fb0SHannes Reinecke {
2728f4a1fb0SHannes Reinecke struct tcm_loop_hba *tl_hba;
2738f4a1fb0SHannes Reinecke struct tcm_loop_tpg *tl_tpg;
2748f4a1fb0SHannes Reinecke
2758f4a1fb0SHannes Reinecke /*
2768f4a1fb0SHannes Reinecke * Locate the tcm_loop_hba_t pointer
2778f4a1fb0SHannes Reinecke */
2788f4a1fb0SHannes Reinecke tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
2798f4a1fb0SHannes Reinecke if (!tl_hba) {
280c8d1f4b7SMarkus Elfring pr_err("Unable to perform device reset without active I_T Nexus\n");
2818f4a1fb0SHannes Reinecke return FAILED;
2828f4a1fb0SHannes Reinecke }
2838f4a1fb0SHannes Reinecke /*
2848f4a1fb0SHannes Reinecke * Locate the tl_tpg pointer from TargetID in sc->device->id
2858f4a1fb0SHannes Reinecke */
2868f4a1fb0SHannes Reinecke tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
2878f4a1fb0SHannes Reinecke if (tl_tpg) {
2888f4a1fb0SHannes Reinecke tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
2898f4a1fb0SHannes Reinecke return SUCCESS;
2908f4a1fb0SHannes Reinecke }
2918f4a1fb0SHannes Reinecke return FAILED;
2928f4a1fb0SHannes Reinecke }
2938f4a1fb0SHannes Reinecke
294*8e2ab8cdSBart Van Assche static const struct scsi_host_template tcm_loop_driver_template = {
2958946b077SAl Viro .show_info = tcm_loop_show_info,
2963703b2c5SNicholas Bellinger .proc_name = "tcm_loopback",
2973703b2c5SNicholas Bellinger .name = "TCM_Loopback",
2983703b2c5SNicholas Bellinger .queuecommand = tcm_loop_queuecommand,
299db5ed4dfSChristoph Hellwig .change_queue_depth = scsi_change_queue_depth,
300969871cdSHannes Reinecke .eh_abort_handler = tcm_loop_abort_task,
3013703b2c5SNicholas Bellinger .eh_device_reset_handler = tcm_loop_device_reset,
3028f4a1fb0SHannes Reinecke .eh_target_reset_handler = tcm_loop_target_reset,
3033703b2c5SNicholas Bellinger .this_id = -1,
3042e88efd3SChristoph Hellwig .sg_tablesize = 256,
3052e88efd3SChristoph Hellwig .max_sectors = 0xFFFF,
3064af14d11SChristoph Hellwig .dma_boundary = PAGE_SIZE - 1,
3073703b2c5SNicholas Bellinger .module = THIS_MODULE,
308c40ecc12SChristoph Hellwig .track_queue_depth = 1,
309e0eb5d38SMike Christie .cmd_size = sizeof(struct tcm_loop_cmd),
3103703b2c5SNicholas Bellinger };
3113703b2c5SNicholas Bellinger
tcm_loop_driver_probe(struct device * dev)3123703b2c5SNicholas Bellinger static int tcm_loop_driver_probe(struct device *dev)
3133703b2c5SNicholas Bellinger {
3143703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
3153703b2c5SNicholas Bellinger struct Scsi_Host *sh;
31659dedde2SNicholas Bellinger int error, host_prot;
3173703b2c5SNicholas Bellinger
3183703b2c5SNicholas Bellinger tl_hba = to_tcm_loop_hba(dev);
3193703b2c5SNicholas Bellinger
3203703b2c5SNicholas Bellinger sh = scsi_host_alloc(&tcm_loop_driver_template,
3213703b2c5SNicholas Bellinger sizeof(struct tcm_loop_hba));
3223703b2c5SNicholas Bellinger if (!sh) {
3236708bb27SAndy Grover pr_err("Unable to allocate struct scsi_host\n");
3243703b2c5SNicholas Bellinger return -ENODEV;
3253703b2c5SNicholas Bellinger }
3263703b2c5SNicholas Bellinger tl_hba->sh = sh;
3273703b2c5SNicholas Bellinger
3283703b2c5SNicholas Bellinger /*
3293703b2c5SNicholas Bellinger * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
3303703b2c5SNicholas Bellinger */
3313703b2c5SNicholas Bellinger *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
3323703b2c5SNicholas Bellinger /*
3333703b2c5SNicholas Bellinger * Setup single ID, Channel and LUN for now..
3343703b2c5SNicholas Bellinger */
3353703b2c5SNicholas Bellinger sh->max_id = 2;
3363703b2c5SNicholas Bellinger sh->max_lun = 0;
3373703b2c5SNicholas Bellinger sh->max_channel = 0;
3389736f4adSIlias Tsitsimpis sh->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
33994a0dfcfSMike Christie sh->nr_hw_queues = tcm_loop_nr_hw_queues;
34094a0dfcfSMike Christie sh->can_queue = tcm_loop_can_queue;
34194a0dfcfSMike Christie sh->cmd_per_lun = tcm_loop_cmd_per_lun;
3423703b2c5SNicholas Bellinger
34359dedde2SNicholas Bellinger host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
34459dedde2SNicholas Bellinger SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
34559dedde2SNicholas Bellinger SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
34659dedde2SNicholas Bellinger
34759dedde2SNicholas Bellinger scsi_host_set_prot(sh, host_prot);
34859dedde2SNicholas Bellinger scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC);
34959dedde2SNicholas Bellinger
3503703b2c5SNicholas Bellinger error = scsi_add_host(sh, &tl_hba->dev);
3513703b2c5SNicholas Bellinger if (error) {
3526708bb27SAndy Grover pr_err("%s: scsi_add_host failed\n", __func__);
3533703b2c5SNicholas Bellinger scsi_host_put(sh);
3543703b2c5SNicholas Bellinger return -ENODEV;
3553703b2c5SNicholas Bellinger }
3563703b2c5SNicholas Bellinger return 0;
3573703b2c5SNicholas Bellinger }
3583703b2c5SNicholas Bellinger
tcm_loop_driver_remove(struct device * dev)359fc7a6209SUwe Kleine-König static void tcm_loop_driver_remove(struct device *dev)
3603703b2c5SNicholas Bellinger {
3613703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
3623703b2c5SNicholas Bellinger struct Scsi_Host *sh;
3633703b2c5SNicholas Bellinger
3643703b2c5SNicholas Bellinger tl_hba = to_tcm_loop_hba(dev);
3653703b2c5SNicholas Bellinger sh = tl_hba->sh;
3663703b2c5SNicholas Bellinger
3673703b2c5SNicholas Bellinger scsi_remove_host(sh);
3683703b2c5SNicholas Bellinger scsi_host_put(sh);
3693703b2c5SNicholas Bellinger }
3703703b2c5SNicholas Bellinger
tcm_loop_release_adapter(struct device * dev)3713703b2c5SNicholas Bellinger static void tcm_loop_release_adapter(struct device *dev)
3723703b2c5SNicholas Bellinger {
3733703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
3743703b2c5SNicholas Bellinger
3753703b2c5SNicholas Bellinger kfree(tl_hba);
3763703b2c5SNicholas Bellinger }
3773703b2c5SNicholas Bellinger
3783703b2c5SNicholas Bellinger /*
3793703b2c5SNicholas Bellinger * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
3803703b2c5SNicholas Bellinger */
tcm_loop_setup_hba_bus(struct tcm_loop_hba * tl_hba,int tcm_loop_host_id)3813703b2c5SNicholas Bellinger static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
3823703b2c5SNicholas Bellinger {
3833703b2c5SNicholas Bellinger int ret;
3843703b2c5SNicholas Bellinger
3853703b2c5SNicholas Bellinger tl_hba->dev.bus = &tcm_loop_lld_bus;
3863703b2c5SNicholas Bellinger tl_hba->dev.parent = tcm_loop_primary;
3873703b2c5SNicholas Bellinger tl_hba->dev.release = &tcm_loop_release_adapter;
3883703b2c5SNicholas Bellinger dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
3893703b2c5SNicholas Bellinger
3903703b2c5SNicholas Bellinger ret = device_register(&tl_hba->dev);
3913703b2c5SNicholas Bellinger if (ret) {
392c8d1f4b7SMarkus Elfring pr_err("device_register() failed for tl_hba->dev: %d\n", ret);
393bc68e428SYang Yingliang put_device(&tl_hba->dev);
3943703b2c5SNicholas Bellinger return -ENODEV;
3953703b2c5SNicholas Bellinger }
3963703b2c5SNicholas Bellinger
3973703b2c5SNicholas Bellinger return 0;
3983703b2c5SNicholas Bellinger }
3993703b2c5SNicholas Bellinger
4003703b2c5SNicholas Bellinger /*
4013703b2c5SNicholas Bellinger * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
4023703b2c5SNicholas Bellinger * tcm_loop SCSI bus.
4033703b2c5SNicholas Bellinger */
tcm_loop_alloc_core_bus(void)4043703b2c5SNicholas Bellinger static int tcm_loop_alloc_core_bus(void)
4053703b2c5SNicholas Bellinger {
4063703b2c5SNicholas Bellinger int ret;
4073703b2c5SNicholas Bellinger
4083703b2c5SNicholas Bellinger tcm_loop_primary = root_device_register("tcm_loop_0");
4093703b2c5SNicholas Bellinger if (IS_ERR(tcm_loop_primary)) {
4106708bb27SAndy Grover pr_err("Unable to allocate tcm_loop_primary\n");
4113703b2c5SNicholas Bellinger return PTR_ERR(tcm_loop_primary);
4123703b2c5SNicholas Bellinger }
4133703b2c5SNicholas Bellinger
4143703b2c5SNicholas Bellinger ret = bus_register(&tcm_loop_lld_bus);
4153703b2c5SNicholas Bellinger if (ret) {
4166708bb27SAndy Grover pr_err("bus_register() failed for tcm_loop_lld_bus\n");
4173703b2c5SNicholas Bellinger goto dev_unreg;
4183703b2c5SNicholas Bellinger }
4193703b2c5SNicholas Bellinger
4203703b2c5SNicholas Bellinger ret = driver_register(&tcm_loop_driverfs);
4213703b2c5SNicholas Bellinger if (ret) {
422c8d1f4b7SMarkus Elfring pr_err("driver_register() failed for tcm_loop_driverfs\n");
4233703b2c5SNicholas Bellinger goto bus_unreg;
4243703b2c5SNicholas Bellinger }
4253703b2c5SNicholas Bellinger
4266708bb27SAndy Grover pr_debug("Initialized TCM Loop Core Bus\n");
4273703b2c5SNicholas Bellinger return ret;
4283703b2c5SNicholas Bellinger
4293703b2c5SNicholas Bellinger bus_unreg:
4303703b2c5SNicholas Bellinger bus_unregister(&tcm_loop_lld_bus);
4313703b2c5SNicholas Bellinger dev_unreg:
4323703b2c5SNicholas Bellinger root_device_unregister(tcm_loop_primary);
4333703b2c5SNicholas Bellinger return ret;
4343703b2c5SNicholas Bellinger }
4353703b2c5SNicholas Bellinger
tcm_loop_release_core_bus(void)4363703b2c5SNicholas Bellinger static void tcm_loop_release_core_bus(void)
4373703b2c5SNicholas Bellinger {
4383703b2c5SNicholas Bellinger driver_unregister(&tcm_loop_driverfs);
4393703b2c5SNicholas Bellinger bus_unregister(&tcm_loop_lld_bus);
4403703b2c5SNicholas Bellinger root_device_unregister(tcm_loop_primary);
4413703b2c5SNicholas Bellinger
4426708bb27SAndy Grover pr_debug("Releasing TCM Loop Core BUS\n");
4433703b2c5SNicholas Bellinger }
4443703b2c5SNicholas Bellinger
tl_tpg(struct se_portal_group * se_tpg)4451667a459SChristoph Hellwig static inline struct tcm_loop_tpg *tl_tpg(struct se_portal_group *se_tpg)
4461667a459SChristoph Hellwig {
4471667a459SChristoph Hellwig return container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
4481667a459SChristoph Hellwig }
4491667a459SChristoph Hellwig
tcm_loop_get_endpoint_wwn(struct se_portal_group * se_tpg)4503703b2c5SNicholas Bellinger static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
4513703b2c5SNicholas Bellinger {
4523703b2c5SNicholas Bellinger /*
453b7446cacSHannes Reinecke * Return the passed NAA identifier for the Target Port
4543703b2c5SNicholas Bellinger */
4551667a459SChristoph Hellwig return &tl_tpg(se_tpg)->tl_hba->tl_wwn_address[0];
4563703b2c5SNicholas Bellinger }
4573703b2c5SNicholas Bellinger
tcm_loop_get_tag(struct se_portal_group * se_tpg)4583703b2c5SNicholas Bellinger static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
4593703b2c5SNicholas Bellinger {
4603703b2c5SNicholas Bellinger /*
4613703b2c5SNicholas Bellinger * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
4623703b2c5SNicholas Bellinger * to represent the SCSI Target Port.
4633703b2c5SNicholas Bellinger */
4641667a459SChristoph Hellwig return tl_tpg(se_tpg)->tl_tpgt;
4653703b2c5SNicholas Bellinger }
4663703b2c5SNicholas Bellinger
4673703b2c5SNicholas Bellinger /*
4683703b2c5SNicholas Bellinger * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
4693703b2c5SNicholas Bellinger * based upon the incoming fabric dependent SCSI Initiator Port
4703703b2c5SNicholas Bellinger */
tcm_loop_check_demo_mode(struct se_portal_group * se_tpg)4713703b2c5SNicholas Bellinger static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
4723703b2c5SNicholas Bellinger {
4733703b2c5SNicholas Bellinger return 1;
4743703b2c5SNicholas Bellinger }
4753703b2c5SNicholas Bellinger
tcm_loop_check_prot_fabric_only(struct se_portal_group * se_tpg)476436f4a0aSNicholas Bellinger static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg)
477436f4a0aSNicholas Bellinger {
478436f4a0aSNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
479436f4a0aSNicholas Bellinger tl_se_tpg);
480436f4a0aSNicholas Bellinger return tl_tpg->tl_fabric_prot_type;
481436f4a0aSNicholas Bellinger }
482436f4a0aSNicholas Bellinger
tcm_loop_sess_get_index(struct se_session * se_sess)4833703b2c5SNicholas Bellinger static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
4843703b2c5SNicholas Bellinger {
4853703b2c5SNicholas Bellinger return 1;
4863703b2c5SNicholas Bellinger }
4873703b2c5SNicholas Bellinger
tcm_loop_get_cmd_state(struct se_cmd * se_cmd)4883703b2c5SNicholas Bellinger static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
4893703b2c5SNicholas Bellinger {
4903703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
4913703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd);
4923703b2c5SNicholas Bellinger
4933703b2c5SNicholas Bellinger return tl_cmd->sc_cmd_state;
4943703b2c5SNicholas Bellinger }
4953703b2c5SNicholas Bellinger
tcm_loop_write_pending(struct se_cmd * se_cmd)4963703b2c5SNicholas Bellinger static int tcm_loop_write_pending(struct se_cmd *se_cmd)
4973703b2c5SNicholas Bellinger {
4983703b2c5SNicholas Bellinger /*
4993703b2c5SNicholas Bellinger * Since Linux/SCSI has already sent down a struct scsi_cmnd
5003703b2c5SNicholas Bellinger * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
5013703b2c5SNicholas Bellinger * memory, and memory has already been mapped to struct se_cmd->t_mem_list
5023703b2c5SNicholas Bellinger * format with transport_generic_map_mem_to_cmd().
5033703b2c5SNicholas Bellinger *
5043703b2c5SNicholas Bellinger * We now tell TCM to add this WRITE CDB directly into the TCM storage
5053703b2c5SNicholas Bellinger * object execution queue.
5063703b2c5SNicholas Bellinger */
50770baf0abSChristoph Hellwig target_execute_cmd(se_cmd);
5083703b2c5SNicholas Bellinger return 0;
5093703b2c5SNicholas Bellinger }
5103703b2c5SNicholas Bellinger
tcm_loop_queue_data_or_status(const char * func,struct se_cmd * se_cmd,u8 scsi_status)511c68a5673SBodo Stroesser static int tcm_loop_queue_data_or_status(const char *func,
512c68a5673SBodo Stroesser struct se_cmd *se_cmd, u8 scsi_status)
5133703b2c5SNicholas Bellinger {
5143703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
5153703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd);
5163703b2c5SNicholas Bellinger struct scsi_cmnd *sc = tl_cmd->sc;
5173703b2c5SNicholas Bellinger
518c8d1f4b7SMarkus Elfring pr_debug("%s() called for scsi_cmnd: %p cdb: 0x%02x\n",
519c68a5673SBodo Stroesser func, sc, sc->cmnd[0]);
5203703b2c5SNicholas Bellinger
5213703b2c5SNicholas Bellinger if (se_cmd->sense_buffer &&
5223703b2c5SNicholas Bellinger ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
5233703b2c5SNicholas Bellinger (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
5243703b2c5SNicholas Bellinger
5255951146dSAndy Grover memcpy(sc->sense_buffer, se_cmd->sense_buffer,
5263703b2c5SNicholas Bellinger SCSI_SENSE_BUFFERSIZE);
5273703b2c5SNicholas Bellinger sc->result = SAM_STAT_CHECK_CONDITION;
5283703b2c5SNicholas Bellinger } else
529c68a5673SBodo Stroesser sc->result = scsi_status;
5303703b2c5SNicholas Bellinger
5313703b2c5SNicholas Bellinger set_host_byte(sc, DID_OK);
5326cf3fa69SRoland Dreier if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
5336cf3fa69SRoland Dreier (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
5346cf3fa69SRoland Dreier scsi_set_resid(sc, se_cmd->residual_count);
5353703b2c5SNicholas Bellinger return 0;
5363703b2c5SNicholas Bellinger }
5373703b2c5SNicholas Bellinger
tcm_loop_queue_data_in(struct se_cmd * se_cmd)538c68a5673SBodo Stroesser static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
539c68a5673SBodo Stroesser {
540c68a5673SBodo Stroesser return tcm_loop_queue_data_or_status(__func__, se_cmd, SAM_STAT_GOOD);
541c68a5673SBodo Stroesser }
542c68a5673SBodo Stroesser
tcm_loop_queue_status(struct se_cmd * se_cmd)543c68a5673SBodo Stroesser static int tcm_loop_queue_status(struct se_cmd *se_cmd)
544c68a5673SBodo Stroesser {
545c68a5673SBodo Stroesser return tcm_loop_queue_data_or_status(__func__,
546c68a5673SBodo Stroesser se_cmd, se_cmd->scsi_status);
547c68a5673SBodo Stroesser }
548c68a5673SBodo Stroesser
tcm_loop_queue_tm_rsp(struct se_cmd * se_cmd)549b79fafacSJoern Engel static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
5503703b2c5SNicholas Bellinger {
5514d3895d5SBart Van Assche struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
5524d3895d5SBart Van Assche struct tcm_loop_cmd, tl_se_cmd);
5534d3895d5SBart Van Assche
554d17203c4SBart Van Assche /* Wake up tcm_loop_issue_tmr(). */
555d17203c4SBart Van Assche complete(&tl_cmd->tmr_done);
5563703b2c5SNicholas Bellinger }
5573703b2c5SNicholas Bellinger
tcm_loop_aborted_task(struct se_cmd * se_cmd)558131e6abcSNicholas Bellinger static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
559131e6abcSNicholas Bellinger {
560131e6abcSNicholas Bellinger return;
561131e6abcSNicholas Bellinger }
562131e6abcSNicholas Bellinger
tcm_loop_dump_proto_id(struct tcm_loop_hba * tl_hba)5633703b2c5SNicholas Bellinger static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
5643703b2c5SNicholas Bellinger {
5653703b2c5SNicholas Bellinger switch (tl_hba->tl_proto_id) {
5663703b2c5SNicholas Bellinger case SCSI_PROTOCOL_SAS:
5673703b2c5SNicholas Bellinger return "SAS";
5683703b2c5SNicholas Bellinger case SCSI_PROTOCOL_FCP:
5693703b2c5SNicholas Bellinger return "FCP";
5703703b2c5SNicholas Bellinger case SCSI_PROTOCOL_ISCSI:
5713703b2c5SNicholas Bellinger return "iSCSI";
5723703b2c5SNicholas Bellinger default:
5733703b2c5SNicholas Bellinger break;
5743703b2c5SNicholas Bellinger }
5753703b2c5SNicholas Bellinger
5763703b2c5SNicholas Bellinger return "Unknown";
5773703b2c5SNicholas Bellinger }
5783703b2c5SNicholas Bellinger
5793703b2c5SNicholas Bellinger /* Start items for tcm_loop_port_cit */
5803703b2c5SNicholas Bellinger
tcm_loop_port_link(struct se_portal_group * se_tpg,struct se_lun * lun)5813703b2c5SNicholas Bellinger static int tcm_loop_port_link(
5823703b2c5SNicholas Bellinger struct se_portal_group *se_tpg,
5833703b2c5SNicholas Bellinger struct se_lun *lun)
5843703b2c5SNicholas Bellinger {
5853703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
5863703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg);
5873703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
5883703b2c5SNicholas Bellinger
58933940d09SJoern Engel atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
5903703b2c5SNicholas Bellinger /*
5913703b2c5SNicholas Bellinger * Add Linux/SCSI struct scsi_device by HCTL
5923703b2c5SNicholas Bellinger */
5933703b2c5SNicholas Bellinger scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
5943703b2c5SNicholas Bellinger
5956708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
5963703b2c5SNicholas Bellinger return 0;
5973703b2c5SNicholas Bellinger }
5983703b2c5SNicholas Bellinger
tcm_loop_port_unlink(struct se_portal_group * se_tpg,struct se_lun * se_lun)5993703b2c5SNicholas Bellinger static void tcm_loop_port_unlink(
6003703b2c5SNicholas Bellinger struct se_portal_group *se_tpg,
6013703b2c5SNicholas Bellinger struct se_lun *se_lun)
6023703b2c5SNicholas Bellinger {
6033703b2c5SNicholas Bellinger struct scsi_device *sd;
6043703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
6053703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg;
6063703b2c5SNicholas Bellinger
6073703b2c5SNicholas Bellinger tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
6083703b2c5SNicholas Bellinger tl_hba = tl_tpg->tl_hba;
6093703b2c5SNicholas Bellinger
6103703b2c5SNicholas Bellinger sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
6113703b2c5SNicholas Bellinger se_lun->unpacked_lun);
6123703b2c5SNicholas Bellinger if (!sd) {
613c8d1f4b7SMarkus Elfring pr_err("Unable to locate struct scsi_device for %d:%d:%llu\n",
614c8d1f4b7SMarkus Elfring 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
6153703b2c5SNicholas Bellinger return;
6163703b2c5SNicholas Bellinger }
6173703b2c5SNicholas Bellinger /*
6183703b2c5SNicholas Bellinger * Remove Linux/SCSI struct scsi_device by HCTL
6193703b2c5SNicholas Bellinger */
6203703b2c5SNicholas Bellinger scsi_remove_device(sd);
6213703b2c5SNicholas Bellinger scsi_device_put(sd);
6223703b2c5SNicholas Bellinger
62333940d09SJoern Engel atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
6243703b2c5SNicholas Bellinger
6256708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
6263703b2c5SNicholas Bellinger }
6273703b2c5SNicholas Bellinger
6283703b2c5SNicholas Bellinger /* End items for tcm_loop_port_cit */
6293703b2c5SNicholas Bellinger
tcm_loop_tpg_attrib_fabric_prot_type_show(struct config_item * item,char * page)6302eafd729SChristoph Hellwig static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_show(
6312eafd729SChristoph Hellwig struct config_item *item, char *page)
632436f4a0aSNicholas Bellinger {
6332eafd729SChristoph Hellwig struct se_portal_group *se_tpg = attrib_to_tpg(item);
634436f4a0aSNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
635436f4a0aSNicholas Bellinger tl_se_tpg);
636436f4a0aSNicholas Bellinger
637436f4a0aSNicholas Bellinger return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type);
638436f4a0aSNicholas Bellinger }
639436f4a0aSNicholas Bellinger
tcm_loop_tpg_attrib_fabric_prot_type_store(struct config_item * item,const char * page,size_t count)6402eafd729SChristoph Hellwig static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_store(
6412eafd729SChristoph Hellwig struct config_item *item, const char *page, size_t count)
642436f4a0aSNicholas Bellinger {
6432eafd729SChristoph Hellwig struct se_portal_group *se_tpg = attrib_to_tpg(item);
644436f4a0aSNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
645436f4a0aSNicholas Bellinger tl_se_tpg);
646436f4a0aSNicholas Bellinger unsigned long val;
647436f4a0aSNicholas Bellinger int ret = kstrtoul(page, 0, &val);
648436f4a0aSNicholas Bellinger
649436f4a0aSNicholas Bellinger if (ret) {
650436f4a0aSNicholas Bellinger pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
651436f4a0aSNicholas Bellinger return ret;
652436f4a0aSNicholas Bellinger }
653436f4a0aSNicholas Bellinger if (val != 0 && val != 1 && val != 3) {
654436f4a0aSNicholas Bellinger pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
655436f4a0aSNicholas Bellinger return -EINVAL;
656436f4a0aSNicholas Bellinger }
657436f4a0aSNicholas Bellinger tl_tpg->tl_fabric_prot_type = val;
658436f4a0aSNicholas Bellinger
659436f4a0aSNicholas Bellinger return count;
660436f4a0aSNicholas Bellinger }
661436f4a0aSNicholas Bellinger
6622eafd729SChristoph Hellwig CONFIGFS_ATTR(tcm_loop_tpg_attrib_, fabric_prot_type);
663436f4a0aSNicholas Bellinger
664436f4a0aSNicholas Bellinger static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = {
6652eafd729SChristoph Hellwig &tcm_loop_tpg_attrib_attr_fabric_prot_type,
666436f4a0aSNicholas Bellinger NULL,
667436f4a0aSNicholas Bellinger };
668436f4a0aSNicholas Bellinger
6693703b2c5SNicholas Bellinger /* Start items for tcm_loop_nexus_cit */
6703703b2c5SNicholas Bellinger
tcm_loop_alloc_sess_cb(struct se_portal_group * se_tpg,struct se_session * se_sess,void * p)671fb444abeSChristoph Hellwig static int tcm_loop_alloc_sess_cb(struct se_portal_group *se_tpg,
672fb444abeSChristoph Hellwig struct se_session *se_sess, void *p)
673fb444abeSChristoph Hellwig {
674fb444abeSChristoph Hellwig struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
675fb444abeSChristoph Hellwig struct tcm_loop_tpg, tl_se_tpg);
676fb444abeSChristoph Hellwig
677fb444abeSChristoph Hellwig tl_tpg->tl_nexus = p;
678fb444abeSChristoph Hellwig return 0;
679fb444abeSChristoph Hellwig }
680fb444abeSChristoph Hellwig
tcm_loop_make_nexus(struct tcm_loop_tpg * tl_tpg,const char * name)6813703b2c5SNicholas Bellinger static int tcm_loop_make_nexus(
6823703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg,
6833703b2c5SNicholas Bellinger const char *name)
6843703b2c5SNicholas Bellinger {
6853703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
6863703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus;
687fb444abeSChristoph Hellwig int ret;
6883703b2c5SNicholas Bellinger
689506787a2SHannes Reinecke if (tl_tpg->tl_nexus) {
690506787a2SHannes Reinecke pr_debug("tl_tpg->tl_nexus already exists\n");
6913703b2c5SNicholas Bellinger return -EEXIST;
6923703b2c5SNicholas Bellinger }
6933703b2c5SNicholas Bellinger
694a572dba9SMarkus Elfring tl_nexus = kzalloc(sizeof(*tl_nexus), GFP_KERNEL);
695cd3fb32aSMarkus Elfring if (!tl_nexus)
6963703b2c5SNicholas Bellinger return -ENOMEM;
697fb444abeSChristoph Hellwig
698fa834287SMike Christie tl_nexus->se_sess = target_setup_session(&tl_tpg->tl_se_tpg, 0, 0,
699fb444abeSChristoph Hellwig TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS,
700fb444abeSChristoph Hellwig name, tl_nexus, tcm_loop_alloc_sess_cb);
701552523dcSDan Carpenter if (IS_ERR(tl_nexus->se_sess)) {
702552523dcSDan Carpenter ret = PTR_ERR(tl_nexus->se_sess);
703fb444abeSChristoph Hellwig kfree(tl_nexus);
704fb444abeSChristoph Hellwig return ret;
705552523dcSDan Carpenter }
706fb444abeSChristoph Hellwig
707c8d1f4b7SMarkus Elfring pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated %s Initiator Port: %s\n",
708c8d1f4b7SMarkus Elfring tcm_loop_dump_proto_id(tl_hba), name);
7093703b2c5SNicholas Bellinger return 0;
7103703b2c5SNicholas Bellinger }
7113703b2c5SNicholas Bellinger
tcm_loop_drop_nexus(struct tcm_loop_tpg * tpg)7123703b2c5SNicholas Bellinger static int tcm_loop_drop_nexus(
7133703b2c5SNicholas Bellinger struct tcm_loop_tpg *tpg)
7143703b2c5SNicholas Bellinger {
7153703b2c5SNicholas Bellinger struct se_session *se_sess;
7163703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus;
7173703b2c5SNicholas Bellinger
718506787a2SHannes Reinecke tl_nexus = tpg->tl_nexus;
7193703b2c5SNicholas Bellinger if (!tl_nexus)
7203703b2c5SNicholas Bellinger return -ENODEV;
7213703b2c5SNicholas Bellinger
7223703b2c5SNicholas Bellinger se_sess = tl_nexus->se_sess;
7233703b2c5SNicholas Bellinger if (!se_sess)
7243703b2c5SNicholas Bellinger return -ENODEV;
7253703b2c5SNicholas Bellinger
7263703b2c5SNicholas Bellinger if (atomic_read(&tpg->tl_tpg_port_count)) {
727c8d1f4b7SMarkus Elfring pr_err("Unable to remove TCM_Loop I_T Nexus with active TPG port count: %d\n",
7283703b2c5SNicholas Bellinger atomic_read(&tpg->tl_tpg_port_count));
7293703b2c5SNicholas Bellinger return -EPERM;
7303703b2c5SNicholas Bellinger }
7313703b2c5SNicholas Bellinger
732c8d1f4b7SMarkus Elfring pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated %s Initiator Port: %s\n",
733c8d1f4b7SMarkus Elfring tcm_loop_dump_proto_id(tpg->tl_hba),
7343703b2c5SNicholas Bellinger tl_nexus->se_sess->se_node_acl->initiatorname);
7353703b2c5SNicholas Bellinger /*
736b7446cacSHannes Reinecke * Release the SCSI I_T Nexus to the emulated Target Port
7373703b2c5SNicholas Bellinger */
73825b88550SMike Christie target_remove_session(se_sess);
739506787a2SHannes Reinecke tpg->tl_nexus = NULL;
7403703b2c5SNicholas Bellinger kfree(tl_nexus);
7413703b2c5SNicholas Bellinger return 0;
7423703b2c5SNicholas Bellinger }
7433703b2c5SNicholas Bellinger
7443703b2c5SNicholas Bellinger /* End items for tcm_loop_nexus_cit */
7453703b2c5SNicholas Bellinger
tcm_loop_tpg_nexus_show(struct config_item * item,char * page)7462eafd729SChristoph Hellwig static ssize_t tcm_loop_tpg_nexus_show(struct config_item *item, char *page)
7473703b2c5SNicholas Bellinger {
7482eafd729SChristoph Hellwig struct se_portal_group *se_tpg = to_tpg(item);
7493703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
7503703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg);
7513703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus;
7523703b2c5SNicholas Bellinger ssize_t ret;
7533703b2c5SNicholas Bellinger
754506787a2SHannes Reinecke tl_nexus = tl_tpg->tl_nexus;
7553703b2c5SNicholas Bellinger if (!tl_nexus)
7563703b2c5SNicholas Bellinger return -ENODEV;
7573703b2c5SNicholas Bellinger
7583703b2c5SNicholas Bellinger ret = snprintf(page, PAGE_SIZE, "%s\n",
7593703b2c5SNicholas Bellinger tl_nexus->se_sess->se_node_acl->initiatorname);
7603703b2c5SNicholas Bellinger
7613703b2c5SNicholas Bellinger return ret;
7623703b2c5SNicholas Bellinger }
7633703b2c5SNicholas Bellinger
tcm_loop_tpg_nexus_store(struct config_item * item,const char * page,size_t count)7642eafd729SChristoph Hellwig static ssize_t tcm_loop_tpg_nexus_store(struct config_item *item,
7652eafd729SChristoph Hellwig const char *page, size_t count)
7663703b2c5SNicholas Bellinger {
7672eafd729SChristoph Hellwig struct se_portal_group *se_tpg = to_tpg(item);
7683703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
7693703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg);
7703703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
7713703b2c5SNicholas Bellinger unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
7723703b2c5SNicholas Bellinger int ret;
7733703b2c5SNicholas Bellinger /*
7743703b2c5SNicholas Bellinger * Shutdown the active I_T nexus if 'NULL' is passed..
7753703b2c5SNicholas Bellinger */
7763703b2c5SNicholas Bellinger if (!strncmp(page, "NULL", 4)) {
7773703b2c5SNicholas Bellinger ret = tcm_loop_drop_nexus(tl_tpg);
7783703b2c5SNicholas Bellinger return (!ret) ? count : ret;
7793703b2c5SNicholas Bellinger }
7803703b2c5SNicholas Bellinger /*
7813703b2c5SNicholas Bellinger * Otherwise make sure the passed virtual Initiator port WWN matches
7823703b2c5SNicholas Bellinger * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
7833703b2c5SNicholas Bellinger * tcm_loop_make_nexus()
7843703b2c5SNicholas Bellinger */
78560d645a4SDan Carpenter if (strlen(page) >= TL_WWN_ADDR_LEN) {
786c8d1f4b7SMarkus Elfring pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
787c8d1f4b7SMarkus Elfring page, TL_WWN_ADDR_LEN);
7883703b2c5SNicholas Bellinger return -EINVAL;
7893703b2c5SNicholas Bellinger }
7903703b2c5SNicholas Bellinger snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
7913703b2c5SNicholas Bellinger
7923703b2c5SNicholas Bellinger ptr = strstr(i_port, "naa.");
7933703b2c5SNicholas Bellinger if (ptr) {
7943703b2c5SNicholas Bellinger if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
795c8d1f4b7SMarkus Elfring pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
796c8d1f4b7SMarkus Elfring i_port, tcm_loop_dump_proto_id(tl_hba));
7973703b2c5SNicholas Bellinger return -EINVAL;
7983703b2c5SNicholas Bellinger }
7993703b2c5SNicholas Bellinger port_ptr = &i_port[0];
8003703b2c5SNicholas Bellinger goto check_newline;
8013703b2c5SNicholas Bellinger }
8023703b2c5SNicholas Bellinger ptr = strstr(i_port, "fc.");
8033703b2c5SNicholas Bellinger if (ptr) {
8043703b2c5SNicholas Bellinger if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
805c8d1f4b7SMarkus Elfring pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
806c8d1f4b7SMarkus Elfring i_port, tcm_loop_dump_proto_id(tl_hba));
8073703b2c5SNicholas Bellinger return -EINVAL;
8083703b2c5SNicholas Bellinger }
8093703b2c5SNicholas Bellinger port_ptr = &i_port[3]; /* Skip over "fc." */
8103703b2c5SNicholas Bellinger goto check_newline;
8113703b2c5SNicholas Bellinger }
8123703b2c5SNicholas Bellinger ptr = strstr(i_port, "iqn.");
8133703b2c5SNicholas Bellinger if (ptr) {
8143703b2c5SNicholas Bellinger if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
815c8d1f4b7SMarkus Elfring pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
816c8d1f4b7SMarkus Elfring i_port, tcm_loop_dump_proto_id(tl_hba));
8173703b2c5SNicholas Bellinger return -EINVAL;
8183703b2c5SNicholas Bellinger }
8193703b2c5SNicholas Bellinger port_ptr = &i_port[0];
8203703b2c5SNicholas Bellinger goto check_newline;
8213703b2c5SNicholas Bellinger }
822c8d1f4b7SMarkus Elfring pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
823c8d1f4b7SMarkus Elfring i_port);
8243703b2c5SNicholas Bellinger return -EINVAL;
8253703b2c5SNicholas Bellinger /*
8263703b2c5SNicholas Bellinger * Clear any trailing newline for the NAA WWN
8273703b2c5SNicholas Bellinger */
8283703b2c5SNicholas Bellinger check_newline:
8293703b2c5SNicholas Bellinger if (i_port[strlen(i_port)-1] == '\n')
8303703b2c5SNicholas Bellinger i_port[strlen(i_port)-1] = '\0';
8313703b2c5SNicholas Bellinger
8323703b2c5SNicholas Bellinger ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
8333703b2c5SNicholas Bellinger if (ret < 0)
8343703b2c5SNicholas Bellinger return ret;
8353703b2c5SNicholas Bellinger
8363703b2c5SNicholas Bellinger return count;
8373703b2c5SNicholas Bellinger }
8383703b2c5SNicholas Bellinger
tcm_loop_tpg_transport_status_show(struct config_item * item,char * page)8392eafd729SChristoph Hellwig static ssize_t tcm_loop_tpg_transport_status_show(struct config_item *item,
840fb2b2844SHannes Reinecke char *page)
841fb2b2844SHannes Reinecke {
8422eafd729SChristoph Hellwig struct se_portal_group *se_tpg = to_tpg(item);
843fb2b2844SHannes Reinecke struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
844fb2b2844SHannes Reinecke struct tcm_loop_tpg, tl_se_tpg);
845fb2b2844SHannes Reinecke const char *status = NULL;
846fb2b2844SHannes Reinecke ssize_t ret = -EINVAL;
847fb2b2844SHannes Reinecke
848fb2b2844SHannes Reinecke switch (tl_tpg->tl_transport_status) {
849fb2b2844SHannes Reinecke case TCM_TRANSPORT_ONLINE:
850fb2b2844SHannes Reinecke status = "online";
851fb2b2844SHannes Reinecke break;
852fb2b2844SHannes Reinecke case TCM_TRANSPORT_OFFLINE:
853fb2b2844SHannes Reinecke status = "offline";
854fb2b2844SHannes Reinecke break;
855fb2b2844SHannes Reinecke default:
856fb2b2844SHannes Reinecke break;
857fb2b2844SHannes Reinecke }
858fb2b2844SHannes Reinecke
859fb2b2844SHannes Reinecke if (status)
860fb2b2844SHannes Reinecke ret = snprintf(page, PAGE_SIZE, "%s\n", status);
861fb2b2844SHannes Reinecke
862fb2b2844SHannes Reinecke return ret;
863fb2b2844SHannes Reinecke }
864fb2b2844SHannes Reinecke
tcm_loop_tpg_transport_status_store(struct config_item * item,const char * page,size_t count)8652eafd729SChristoph Hellwig static ssize_t tcm_loop_tpg_transport_status_store(struct config_item *item,
8662eafd729SChristoph Hellwig const char *page, size_t count)
867fb2b2844SHannes Reinecke {
8682eafd729SChristoph Hellwig struct se_portal_group *se_tpg = to_tpg(item);
869fb2b2844SHannes Reinecke struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
870fb2b2844SHannes Reinecke struct tcm_loop_tpg, tl_se_tpg);
871fb2b2844SHannes Reinecke
872fb2b2844SHannes Reinecke if (!strncmp(page, "online", 6)) {
873fb2b2844SHannes Reinecke tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
874fb2b2844SHannes Reinecke return count;
875fb2b2844SHannes Reinecke }
876fb2b2844SHannes Reinecke if (!strncmp(page, "offline", 7)) {
877fb2b2844SHannes Reinecke tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
878e986a35aSHannes Reinecke if (tl_tpg->tl_nexus) {
879e986a35aSHannes Reinecke struct se_session *tl_sess = tl_tpg->tl_nexus->se_sess;
880e986a35aSHannes Reinecke
881e986a35aSHannes Reinecke core_allocate_nexus_loss_ua(tl_sess->se_node_acl);
882e986a35aSHannes Reinecke }
883fb2b2844SHannes Reinecke return count;
884fb2b2844SHannes Reinecke }
885fb2b2844SHannes Reinecke return -EINVAL;
886fb2b2844SHannes Reinecke }
887fb2b2844SHannes Reinecke
tcm_loop_tpg_address_show(struct config_item * item,char * page)8882628b352SSheng Yang static ssize_t tcm_loop_tpg_address_show(struct config_item *item,
8892628b352SSheng Yang char *page)
8902628b352SSheng Yang {
8912628b352SSheng Yang struct se_portal_group *se_tpg = to_tpg(item);
8922628b352SSheng Yang struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
8932628b352SSheng Yang struct tcm_loop_tpg, tl_se_tpg);
8942628b352SSheng Yang struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
8952628b352SSheng Yang
8962628b352SSheng Yang return snprintf(page, PAGE_SIZE, "%d:0:%d\n",
8972628b352SSheng Yang tl_hba->sh->host_no, tl_tpg->tl_tpgt);
8982628b352SSheng Yang }
8992628b352SSheng Yang
9002eafd729SChristoph Hellwig CONFIGFS_ATTR(tcm_loop_tpg_, nexus);
9012eafd729SChristoph Hellwig CONFIGFS_ATTR(tcm_loop_tpg_, transport_status);
9022628b352SSheng Yang CONFIGFS_ATTR_RO(tcm_loop_tpg_, address);
903fb2b2844SHannes Reinecke
9043703b2c5SNicholas Bellinger static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
9052eafd729SChristoph Hellwig &tcm_loop_tpg_attr_nexus,
9062eafd729SChristoph Hellwig &tcm_loop_tpg_attr_transport_status,
9072628b352SSheng Yang &tcm_loop_tpg_attr_address,
9083703b2c5SNicholas Bellinger NULL,
9093703b2c5SNicholas Bellinger };
9103703b2c5SNicholas Bellinger
9113703b2c5SNicholas Bellinger /* Start items for tcm_loop_naa_cit */
9123703b2c5SNicholas Bellinger
tcm_loop_make_naa_tpg(struct se_wwn * wwn,const char * name)913aa090eabSBart Van Assche static struct se_portal_group *tcm_loop_make_naa_tpg(struct se_wwn *wwn,
9143703b2c5SNicholas Bellinger const char *name)
9153703b2c5SNicholas Bellinger {
9163703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = container_of(wwn,
9173703b2c5SNicholas Bellinger struct tcm_loop_hba, tl_hba_wwn);
9183703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg;
9193703b2c5SNicholas Bellinger int ret;
9202e1cd90dSMing Lin unsigned long tpgt;
9213703b2c5SNicholas Bellinger
9222e1cd90dSMing Lin if (strstr(name, "tpgt_") != name) {
923c8d1f4b7SMarkus Elfring pr_err("Unable to locate \"tpgt_#\" directory group\n");
9243703b2c5SNicholas Bellinger return ERR_PTR(-EINVAL);
9253703b2c5SNicholas Bellinger }
9262e1cd90dSMing Lin if (kstrtoul(name+5, 10, &tpgt))
9272e1cd90dSMing Lin return ERR_PTR(-EINVAL);
9283703b2c5SNicholas Bellinger
92912f09ccbSDan Carpenter if (tpgt >= TL_TPGS_PER_HBA) {
930c8d1f4b7SMarkus Elfring pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA: %u\n",
931c8d1f4b7SMarkus Elfring tpgt, TL_TPGS_PER_HBA);
9323703b2c5SNicholas Bellinger return ERR_PTR(-EINVAL);
9333703b2c5SNicholas Bellinger }
9343703b2c5SNicholas Bellinger tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
9353703b2c5SNicholas Bellinger tl_tpg->tl_hba = tl_hba;
9363703b2c5SNicholas Bellinger tl_tpg->tl_tpgt = tpgt;
9373703b2c5SNicholas Bellinger /*
938b7446cacSHannes Reinecke * Register the tl_tpg as a emulated TCM Target Endpoint
9393703b2c5SNicholas Bellinger */
940bc0c94b1SNicholas Bellinger ret = core_tpg_register(wwn, &tl_tpg->tl_se_tpg, tl_hba->tl_proto_id);
9413703b2c5SNicholas Bellinger if (ret < 0)
9423703b2c5SNicholas Bellinger return ERR_PTR(-ENOMEM);
9433703b2c5SNicholas Bellinger
944c8d1f4b7SMarkus Elfring pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s Target Port %s,t,0x%04lx\n",
945c8d1f4b7SMarkus Elfring tcm_loop_dump_proto_id(tl_hba),
9463703b2c5SNicholas Bellinger config_item_name(&wwn->wwn_group.cg_item), tpgt);
9473703b2c5SNicholas Bellinger return &tl_tpg->tl_se_tpg;
9483703b2c5SNicholas Bellinger }
9493703b2c5SNicholas Bellinger
tcm_loop_drop_naa_tpg(struct se_portal_group * se_tpg)950f0a6c693SRashika Kheria static void tcm_loop_drop_naa_tpg(
9513703b2c5SNicholas Bellinger struct se_portal_group *se_tpg)
9523703b2c5SNicholas Bellinger {
9533703b2c5SNicholas Bellinger struct se_wwn *wwn = se_tpg->se_tpg_wwn;
9543703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
9553703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg);
9563703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
9573703b2c5SNicholas Bellinger unsigned short tpgt;
9583703b2c5SNicholas Bellinger
9593703b2c5SNicholas Bellinger tl_hba = tl_tpg->tl_hba;
9603703b2c5SNicholas Bellinger tpgt = tl_tpg->tl_tpgt;
9613703b2c5SNicholas Bellinger /*
962b7446cacSHannes Reinecke * Release the I_T Nexus for the Virtual target link if present
9633703b2c5SNicholas Bellinger */
9643703b2c5SNicholas Bellinger tcm_loop_drop_nexus(tl_tpg);
9653703b2c5SNicholas Bellinger /*
966b7446cacSHannes Reinecke * Deregister the tl_tpg as a emulated TCM Target Endpoint
9673703b2c5SNicholas Bellinger */
9683703b2c5SNicholas Bellinger core_tpg_deregister(se_tpg);
9693703b2c5SNicholas Bellinger
9700a020436SNicholas Bellinger tl_tpg->tl_hba = NULL;
9710a020436SNicholas Bellinger tl_tpg->tl_tpgt = 0;
9720a020436SNicholas Bellinger
973c8d1f4b7SMarkus Elfring pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s Target Port %s,t,0x%04x\n",
974c8d1f4b7SMarkus Elfring tcm_loop_dump_proto_id(tl_hba),
9753703b2c5SNicholas Bellinger config_item_name(&wwn->wwn_group.cg_item), tpgt);
9763703b2c5SNicholas Bellinger }
9773703b2c5SNicholas Bellinger
9783703b2c5SNicholas Bellinger /* End items for tcm_loop_naa_cit */
9793703b2c5SNicholas Bellinger
9803703b2c5SNicholas Bellinger /* Start items for tcm_loop_cit */
9813703b2c5SNicholas Bellinger
tcm_loop_make_scsi_hba(struct target_fabric_configfs * tf,struct config_group * group,const char * name)982f0a6c693SRashika Kheria static struct se_wwn *tcm_loop_make_scsi_hba(
9833703b2c5SNicholas Bellinger struct target_fabric_configfs *tf,
9843703b2c5SNicholas Bellinger struct config_group *group,
9853703b2c5SNicholas Bellinger const char *name)
9863703b2c5SNicholas Bellinger {
9873703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
9883703b2c5SNicholas Bellinger struct Scsi_Host *sh;
9893703b2c5SNicholas Bellinger char *ptr;
9903703b2c5SNicholas Bellinger int ret, off = 0;
9913703b2c5SNicholas Bellinger
992a572dba9SMarkus Elfring tl_hba = kzalloc(sizeof(*tl_hba), GFP_KERNEL);
993cd3fb32aSMarkus Elfring if (!tl_hba)
9943703b2c5SNicholas Bellinger return ERR_PTR(-ENOMEM);
995cd3fb32aSMarkus Elfring
9963703b2c5SNicholas Bellinger /*
9973703b2c5SNicholas Bellinger * Determine the emulated Protocol Identifier and Target Port Name
9983703b2c5SNicholas Bellinger * based on the incoming configfs directory name.
9993703b2c5SNicholas Bellinger */
10003703b2c5SNicholas Bellinger ptr = strstr(name, "naa.");
10013703b2c5SNicholas Bellinger if (ptr) {
10023703b2c5SNicholas Bellinger tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
10033703b2c5SNicholas Bellinger goto check_len;
10043703b2c5SNicholas Bellinger }
10053703b2c5SNicholas Bellinger ptr = strstr(name, "fc.");
10063703b2c5SNicholas Bellinger if (ptr) {
10073703b2c5SNicholas Bellinger tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
10083703b2c5SNicholas Bellinger off = 3; /* Skip over "fc." */
10093703b2c5SNicholas Bellinger goto check_len;
10103703b2c5SNicholas Bellinger }
10113703b2c5SNicholas Bellinger ptr = strstr(name, "iqn.");
1012a57b5d36SJesper Juhl if (!ptr) {
1013c8d1f4b7SMarkus Elfring pr_err("Unable to locate prefix for emulated Target Port: %s\n",
1014c8d1f4b7SMarkus Elfring name);
1015a57b5d36SJesper Juhl ret = -EINVAL;
1016a57b5d36SJesper Juhl goto out;
10173703b2c5SNicholas Bellinger }
1018a57b5d36SJesper Juhl tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
10193703b2c5SNicholas Bellinger
10203703b2c5SNicholas Bellinger check_len:
102160d645a4SDan Carpenter if (strlen(name) >= TL_WWN_ADDR_LEN) {
1022c8d1f4b7SMarkus Elfring pr_err("Emulated NAA %s Address: %s, exceeds max: %d\n",
1023c8d1f4b7SMarkus Elfring name, tcm_loop_dump_proto_id(tl_hba), TL_WWN_ADDR_LEN);
1024a57b5d36SJesper Juhl ret = -EINVAL;
1025a57b5d36SJesper Juhl goto out;
10263703b2c5SNicholas Bellinger }
10273703b2c5SNicholas Bellinger snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
10283703b2c5SNicholas Bellinger
10293703b2c5SNicholas Bellinger /*
10303703b2c5SNicholas Bellinger * Call device_register(tl_hba->dev) to register the emulated
10313703b2c5SNicholas Bellinger * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
10323703b2c5SNicholas Bellinger * device_register() callbacks in tcm_loop_driver_probe()
10333703b2c5SNicholas Bellinger */
10343703b2c5SNicholas Bellinger ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
10353703b2c5SNicholas Bellinger if (ret)
1036bc68e428SYang Yingliang return ERR_PTR(ret);
10373703b2c5SNicholas Bellinger
10383703b2c5SNicholas Bellinger sh = tl_hba->sh;
10393703b2c5SNicholas Bellinger tcm_loop_hba_no_cnt++;
1040c8d1f4b7SMarkus Elfring pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
10413703b2c5SNicholas Bellinger tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
10423703b2c5SNicholas Bellinger return &tl_hba->tl_hba_wwn;
10433703b2c5SNicholas Bellinger out:
10443703b2c5SNicholas Bellinger kfree(tl_hba);
10453703b2c5SNicholas Bellinger return ERR_PTR(ret);
10463703b2c5SNicholas Bellinger }
10473703b2c5SNicholas Bellinger
tcm_loop_drop_scsi_hba(struct se_wwn * wwn)1048f0a6c693SRashika Kheria static void tcm_loop_drop_scsi_hba(
10493703b2c5SNicholas Bellinger struct se_wwn *wwn)
10503703b2c5SNicholas Bellinger {
10513703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = container_of(wwn,
10523703b2c5SNicholas Bellinger struct tcm_loop_hba, tl_hba_wwn);
10536297b07cSNicholas Bellinger
1054c8d1f4b7SMarkus Elfring pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
1055b7446cacSHannes Reinecke tcm_loop_dump_proto_id(tl_hba), tl_hba->tl_wwn_address,
1056b7446cacSHannes Reinecke tl_hba->sh->host_no);
10573703b2c5SNicholas Bellinger /*
10583703b2c5SNicholas Bellinger * Call device_unregister() on the original tl_hba->dev.
10593703b2c5SNicholas Bellinger * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
10603703b2c5SNicholas Bellinger * release *tl_hba;
10613703b2c5SNicholas Bellinger */
10623703b2c5SNicholas Bellinger device_unregister(&tl_hba->dev);
10633703b2c5SNicholas Bellinger }
10643703b2c5SNicholas Bellinger
10653703b2c5SNicholas Bellinger /* Start items for tcm_loop_cit */
tcm_loop_wwn_version_show(struct config_item * item,char * page)10662eafd729SChristoph Hellwig static ssize_t tcm_loop_wwn_version_show(struct config_item *item, char *page)
10673703b2c5SNicholas Bellinger {
10683703b2c5SNicholas Bellinger return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
10693703b2c5SNicholas Bellinger }
10703703b2c5SNicholas Bellinger
10712eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(tcm_loop_wwn_, version);
10723703b2c5SNicholas Bellinger
10733703b2c5SNicholas Bellinger static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
10742eafd729SChristoph Hellwig &tcm_loop_wwn_attr_version,
10753703b2c5SNicholas Bellinger NULL,
10763703b2c5SNicholas Bellinger };
10773703b2c5SNicholas Bellinger
10783703b2c5SNicholas Bellinger /* End items for tcm_loop_cit */
10793703b2c5SNicholas Bellinger
10809ac8928eSChristoph Hellwig static const struct target_core_fabric_ops loop_ops = {
10819ac8928eSChristoph Hellwig .module = THIS_MODULE,
108230c7ca93SDavid Disseldorp .fabric_name = "loopback",
10839ac8928eSChristoph Hellwig .tpg_get_wwn = tcm_loop_get_endpoint_wwn,
10849ac8928eSChristoph Hellwig .tpg_get_tag = tcm_loop_get_tag,
10859ac8928eSChristoph Hellwig .tpg_check_demo_mode = tcm_loop_check_demo_mode,
10869ac8928eSChristoph Hellwig .tpg_check_prot_fabric_only = tcm_loop_check_prot_fabric_only,
10879ac8928eSChristoph Hellwig .check_stop_free = tcm_loop_check_stop_free,
10889ac8928eSChristoph Hellwig .release_cmd = tcm_loop_release_cmd,
10899ac8928eSChristoph Hellwig .sess_get_index = tcm_loop_sess_get_index,
10909ac8928eSChristoph Hellwig .write_pending = tcm_loop_write_pending,
10919ac8928eSChristoph Hellwig .get_cmd_state = tcm_loop_get_cmd_state,
10929ac8928eSChristoph Hellwig .queue_data_in = tcm_loop_queue_data_in,
10939ac8928eSChristoph Hellwig .queue_status = tcm_loop_queue_status,
10949ac8928eSChristoph Hellwig .queue_tm_rsp = tcm_loop_queue_tm_rsp,
10959ac8928eSChristoph Hellwig .aborted_task = tcm_loop_aborted_task,
10969ac8928eSChristoph Hellwig .fabric_make_wwn = tcm_loop_make_scsi_hba,
10979ac8928eSChristoph Hellwig .fabric_drop_wwn = tcm_loop_drop_scsi_hba,
10989ac8928eSChristoph Hellwig .fabric_make_tpg = tcm_loop_make_naa_tpg,
10999ac8928eSChristoph Hellwig .fabric_drop_tpg = tcm_loop_drop_naa_tpg,
11009ac8928eSChristoph Hellwig .fabric_post_link = tcm_loop_port_link,
11019ac8928eSChristoph Hellwig .fabric_pre_unlink = tcm_loop_port_unlink,
11029ac8928eSChristoph Hellwig .tfc_wwn_attrs = tcm_loop_wwn_attrs,
11039ac8928eSChristoph Hellwig .tfc_tpg_base_attrs = tcm_loop_tpg_attrs,
11049ac8928eSChristoph Hellwig .tfc_tpg_attrib_attrs = tcm_loop_tpg_attrib_attrs,
11059ac8928eSChristoph Hellwig };
11063703b2c5SNicholas Bellinger
tcm_loop_fabric_init(void)11073703b2c5SNicholas Bellinger static int __init tcm_loop_fabric_init(void)
11083703b2c5SNicholas Bellinger {
1109afe2cb7fSChristoph Hellwig int ret = -ENOMEM;
1110afe2cb7fSChristoph Hellwig
11113703b2c5SNicholas Bellinger tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
11123703b2c5SNicholas Bellinger sizeof(struct tcm_loop_cmd),
11133703b2c5SNicholas Bellinger __alignof__(struct tcm_loop_cmd),
11143703b2c5SNicholas Bellinger 0, NULL);
11153703b2c5SNicholas Bellinger if (!tcm_loop_cmd_cache) {
1116c8d1f4b7SMarkus Elfring pr_debug("kmem_cache_create() for tcm_loop_cmd_cache failed\n");
11171130b499SMike Christie goto out;
11183703b2c5SNicholas Bellinger }
11193703b2c5SNicholas Bellinger
11203703b2c5SNicholas Bellinger ret = tcm_loop_alloc_core_bus();
11213703b2c5SNicholas Bellinger if (ret)
1122afe2cb7fSChristoph Hellwig goto out_destroy_cache;
11233703b2c5SNicholas Bellinger
11249ac8928eSChristoph Hellwig ret = target_register_template(&loop_ops);
1125afe2cb7fSChristoph Hellwig if (ret)
1126afe2cb7fSChristoph Hellwig goto out_release_core_bus;
11273703b2c5SNicholas Bellinger
11283703b2c5SNicholas Bellinger return 0;
1129afe2cb7fSChristoph Hellwig
1130afe2cb7fSChristoph Hellwig out_release_core_bus:
1131afe2cb7fSChristoph Hellwig tcm_loop_release_core_bus();
1132afe2cb7fSChristoph Hellwig out_destroy_cache:
1133afe2cb7fSChristoph Hellwig kmem_cache_destroy(tcm_loop_cmd_cache);
1134afe2cb7fSChristoph Hellwig out:
1135afe2cb7fSChristoph Hellwig return ret;
11363703b2c5SNicholas Bellinger }
11373703b2c5SNicholas Bellinger
tcm_loop_fabric_exit(void)11383703b2c5SNicholas Bellinger static void __exit tcm_loop_fabric_exit(void)
11393703b2c5SNicholas Bellinger {
11409ac8928eSChristoph Hellwig target_unregister_template(&loop_ops);
11413703b2c5SNicholas Bellinger tcm_loop_release_core_bus();
11423703b2c5SNicholas Bellinger kmem_cache_destroy(tcm_loop_cmd_cache);
11433703b2c5SNicholas Bellinger }
11443703b2c5SNicholas Bellinger
11453703b2c5SNicholas Bellinger MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
11463703b2c5SNicholas Bellinger MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
11473703b2c5SNicholas Bellinger MODULE_LICENSE("GPL");
11483703b2c5SNicholas Bellinger module_init(tcm_loop_fabric_init);
11493703b2c5SNicholas Bellinger module_exit(tcm_loop_fabric_exit);
1150