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 <[email protected]>
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
864ad94653SRicardo B. Marliere static const 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
157e2f4ea40SMike Christie target_submit(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;
179*1909b643SGuixin Liu tl_cmd->sc_cmd_tag = blk_mq_unique_tag(scsi_cmd_to_rq(sc));
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,
245*1909b643SGuixin Liu blk_mq_unique_tag(scsi_cmd_to_rq(sc)),
246*1909b643SGuixin Liu TMR_ABORT_TASK);
247969871cdSHannes Reinecke return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
248969871cdSHannes Reinecke }
249969871cdSHannes Reinecke
250a314d700SHannes Reinecke /*
251a314d700SHannes Reinecke * Called from SCSI EH process context to issue a LUN_RESET TMR
252a314d700SHannes Reinecke * to struct scsi_device
253a314d700SHannes Reinecke */
tcm_loop_device_reset(struct scsi_cmnd * sc)254a314d700SHannes Reinecke static int tcm_loop_device_reset(struct scsi_cmnd *sc)
255a314d700SHannes Reinecke {
2563703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
2573703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg;
2588f13142aSColin Ian King int ret;
259a314d700SHannes Reinecke
2603703b2c5SNicholas Bellinger /*
2613703b2c5SNicholas Bellinger * Locate the tcm_loop_hba_t pointer
2623703b2c5SNicholas Bellinger */
2633703b2c5SNicholas Bellinger tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
2643703b2c5SNicholas Bellinger tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
265506787a2SHannes Reinecke
266506787a2SHannes Reinecke ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
267969871cdSHannes Reinecke 0, TMR_LUN_RESET);
268a314d700SHannes Reinecke return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
2693703b2c5SNicholas Bellinger }
2703703b2c5SNicholas Bellinger
tcm_loop_target_reset(struct scsi_cmnd * sc)2718f4a1fb0SHannes Reinecke static int tcm_loop_target_reset(struct scsi_cmnd *sc)
2728f4a1fb0SHannes Reinecke {
2738f4a1fb0SHannes Reinecke struct tcm_loop_hba *tl_hba;
2748f4a1fb0SHannes Reinecke struct tcm_loop_tpg *tl_tpg;
2758f4a1fb0SHannes Reinecke
2768f4a1fb0SHannes Reinecke /*
2778f4a1fb0SHannes Reinecke * Locate the tcm_loop_hba_t pointer
2788f4a1fb0SHannes Reinecke */
2798f4a1fb0SHannes Reinecke tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
2808f4a1fb0SHannes Reinecke if (!tl_hba) {
281c8d1f4b7SMarkus Elfring pr_err("Unable to perform device reset without active I_T Nexus\n");
2828f4a1fb0SHannes Reinecke return FAILED;
2838f4a1fb0SHannes Reinecke }
2848f4a1fb0SHannes Reinecke /*
2858f4a1fb0SHannes Reinecke * Locate the tl_tpg pointer from TargetID in sc->device->id
2868f4a1fb0SHannes Reinecke */
2878f4a1fb0SHannes Reinecke tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
2888f4a1fb0SHannes Reinecke if (tl_tpg) {
2898f4a1fb0SHannes Reinecke tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
2908f4a1fb0SHannes Reinecke return SUCCESS;
2918f4a1fb0SHannes Reinecke }
2928f4a1fb0SHannes Reinecke return FAILED;
2938f4a1fb0SHannes Reinecke }
2948f4a1fb0SHannes Reinecke
2958e2ab8cdSBart Van Assche static const struct scsi_host_template tcm_loop_driver_template = {
2968946b077SAl Viro .show_info = tcm_loop_show_info,
2973703b2c5SNicholas Bellinger .proc_name = "tcm_loopback",
2983703b2c5SNicholas Bellinger .name = "TCM_Loopback",
2993703b2c5SNicholas Bellinger .queuecommand = tcm_loop_queuecommand,
300db5ed4dfSChristoph Hellwig .change_queue_depth = scsi_change_queue_depth,
301969871cdSHannes Reinecke .eh_abort_handler = tcm_loop_abort_task,
3023703b2c5SNicholas Bellinger .eh_device_reset_handler = tcm_loop_device_reset,
3038f4a1fb0SHannes Reinecke .eh_target_reset_handler = tcm_loop_target_reset,
3043703b2c5SNicholas Bellinger .this_id = -1,
3052e88efd3SChristoph Hellwig .sg_tablesize = 256,
3062e88efd3SChristoph Hellwig .max_sectors = 0xFFFF,
3074af14d11SChristoph Hellwig .dma_boundary = PAGE_SIZE - 1,
3083703b2c5SNicholas Bellinger .module = THIS_MODULE,
309c40ecc12SChristoph Hellwig .track_queue_depth = 1,
310e0eb5d38SMike Christie .cmd_size = sizeof(struct tcm_loop_cmd),
3113703b2c5SNicholas Bellinger };
3123703b2c5SNicholas Bellinger
tcm_loop_driver_probe(struct device * dev)3133703b2c5SNicholas Bellinger static int tcm_loop_driver_probe(struct device *dev)
3143703b2c5SNicholas Bellinger {
3153703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
3163703b2c5SNicholas Bellinger struct Scsi_Host *sh;
31759dedde2SNicholas Bellinger int error, host_prot;
3183703b2c5SNicholas Bellinger
3193703b2c5SNicholas Bellinger tl_hba = to_tcm_loop_hba(dev);
3203703b2c5SNicholas Bellinger
3213703b2c5SNicholas Bellinger sh = scsi_host_alloc(&tcm_loop_driver_template,
3223703b2c5SNicholas Bellinger sizeof(struct tcm_loop_hba));
3233703b2c5SNicholas Bellinger if (!sh) {
3246708bb27SAndy Grover pr_err("Unable to allocate struct scsi_host\n");
3253703b2c5SNicholas Bellinger return -ENODEV;
3263703b2c5SNicholas Bellinger }
3273703b2c5SNicholas Bellinger tl_hba->sh = sh;
3283703b2c5SNicholas Bellinger
3293703b2c5SNicholas Bellinger /*
3303703b2c5SNicholas Bellinger * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
3313703b2c5SNicholas Bellinger */
3323703b2c5SNicholas Bellinger *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
3333703b2c5SNicholas Bellinger /*
3343703b2c5SNicholas Bellinger * Setup single ID, Channel and LUN for now..
3353703b2c5SNicholas Bellinger */
3363703b2c5SNicholas Bellinger sh->max_id = 2;
3373703b2c5SNicholas Bellinger sh->max_lun = 0;
3383703b2c5SNicholas Bellinger sh->max_channel = 0;
3399736f4adSIlias Tsitsimpis sh->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
34094a0dfcfSMike Christie sh->nr_hw_queues = tcm_loop_nr_hw_queues;
34194a0dfcfSMike Christie sh->can_queue = tcm_loop_can_queue;
34294a0dfcfSMike Christie sh->cmd_per_lun = tcm_loop_cmd_per_lun;
3433703b2c5SNicholas Bellinger
34459dedde2SNicholas Bellinger host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
34559dedde2SNicholas Bellinger SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
34659dedde2SNicholas Bellinger SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
34759dedde2SNicholas Bellinger
34859dedde2SNicholas Bellinger scsi_host_set_prot(sh, host_prot);
34959dedde2SNicholas Bellinger scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC);
35059dedde2SNicholas Bellinger
3513703b2c5SNicholas Bellinger error = scsi_add_host(sh, &tl_hba->dev);
3523703b2c5SNicholas Bellinger if (error) {
3536708bb27SAndy Grover pr_err("%s: scsi_add_host failed\n", __func__);
3543703b2c5SNicholas Bellinger scsi_host_put(sh);
3553703b2c5SNicholas Bellinger return -ENODEV;
3563703b2c5SNicholas Bellinger }
3573703b2c5SNicholas Bellinger return 0;
3583703b2c5SNicholas Bellinger }
3593703b2c5SNicholas Bellinger
tcm_loop_driver_remove(struct device * dev)360fc7a6209SUwe Kleine-König static void tcm_loop_driver_remove(struct device *dev)
3613703b2c5SNicholas Bellinger {
3623703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
3633703b2c5SNicholas Bellinger struct Scsi_Host *sh;
3643703b2c5SNicholas Bellinger
3653703b2c5SNicholas Bellinger tl_hba = to_tcm_loop_hba(dev);
3663703b2c5SNicholas Bellinger sh = tl_hba->sh;
3673703b2c5SNicholas Bellinger
3683703b2c5SNicholas Bellinger scsi_remove_host(sh);
3693703b2c5SNicholas Bellinger scsi_host_put(sh);
3703703b2c5SNicholas Bellinger }
3713703b2c5SNicholas Bellinger
tcm_loop_release_adapter(struct device * dev)3723703b2c5SNicholas Bellinger static void tcm_loop_release_adapter(struct device *dev)
3733703b2c5SNicholas Bellinger {
3743703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
3753703b2c5SNicholas Bellinger
3763703b2c5SNicholas Bellinger kfree(tl_hba);
3773703b2c5SNicholas Bellinger }
3783703b2c5SNicholas Bellinger
3793703b2c5SNicholas Bellinger /*
3803703b2c5SNicholas Bellinger * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
3813703b2c5SNicholas Bellinger */
tcm_loop_setup_hba_bus(struct tcm_loop_hba * tl_hba,int tcm_loop_host_id)3823703b2c5SNicholas Bellinger static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
3833703b2c5SNicholas Bellinger {
3843703b2c5SNicholas Bellinger int ret;
3853703b2c5SNicholas Bellinger
3863703b2c5SNicholas Bellinger tl_hba->dev.bus = &tcm_loop_lld_bus;
3873703b2c5SNicholas Bellinger tl_hba->dev.parent = tcm_loop_primary;
3883703b2c5SNicholas Bellinger tl_hba->dev.release = &tcm_loop_release_adapter;
3893703b2c5SNicholas Bellinger dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
3903703b2c5SNicholas Bellinger
3913703b2c5SNicholas Bellinger ret = device_register(&tl_hba->dev);
3923703b2c5SNicholas Bellinger if (ret) {
393c8d1f4b7SMarkus Elfring pr_err("device_register() failed for tl_hba->dev: %d\n", ret);
394bc68e428SYang Yingliang put_device(&tl_hba->dev);
3953703b2c5SNicholas Bellinger return -ENODEV;
3963703b2c5SNicholas Bellinger }
3973703b2c5SNicholas Bellinger
3983703b2c5SNicholas Bellinger return 0;
3993703b2c5SNicholas Bellinger }
4003703b2c5SNicholas Bellinger
4013703b2c5SNicholas Bellinger /*
4023703b2c5SNicholas Bellinger * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
4033703b2c5SNicholas Bellinger * tcm_loop SCSI bus.
4043703b2c5SNicholas Bellinger */
tcm_loop_alloc_core_bus(void)4053703b2c5SNicholas Bellinger static int tcm_loop_alloc_core_bus(void)
4063703b2c5SNicholas Bellinger {
4073703b2c5SNicholas Bellinger int ret;
4083703b2c5SNicholas Bellinger
4093703b2c5SNicholas Bellinger tcm_loop_primary = root_device_register("tcm_loop_0");
4103703b2c5SNicholas Bellinger if (IS_ERR(tcm_loop_primary)) {
4116708bb27SAndy Grover pr_err("Unable to allocate tcm_loop_primary\n");
4123703b2c5SNicholas Bellinger return PTR_ERR(tcm_loop_primary);
4133703b2c5SNicholas Bellinger }
4143703b2c5SNicholas Bellinger
4153703b2c5SNicholas Bellinger ret = bus_register(&tcm_loop_lld_bus);
4163703b2c5SNicholas Bellinger if (ret) {
4176708bb27SAndy Grover pr_err("bus_register() failed for tcm_loop_lld_bus\n");
4183703b2c5SNicholas Bellinger goto dev_unreg;
4193703b2c5SNicholas Bellinger }
4203703b2c5SNicholas Bellinger
4213703b2c5SNicholas Bellinger ret = driver_register(&tcm_loop_driverfs);
4223703b2c5SNicholas Bellinger if (ret) {
423c8d1f4b7SMarkus Elfring pr_err("driver_register() failed for tcm_loop_driverfs\n");
4243703b2c5SNicholas Bellinger goto bus_unreg;
4253703b2c5SNicholas Bellinger }
4263703b2c5SNicholas Bellinger
4276708bb27SAndy Grover pr_debug("Initialized TCM Loop Core Bus\n");
4283703b2c5SNicholas Bellinger return ret;
4293703b2c5SNicholas Bellinger
4303703b2c5SNicholas Bellinger bus_unreg:
4313703b2c5SNicholas Bellinger bus_unregister(&tcm_loop_lld_bus);
4323703b2c5SNicholas Bellinger dev_unreg:
4333703b2c5SNicholas Bellinger root_device_unregister(tcm_loop_primary);
4343703b2c5SNicholas Bellinger return ret;
4353703b2c5SNicholas Bellinger }
4363703b2c5SNicholas Bellinger
tcm_loop_release_core_bus(void)4373703b2c5SNicholas Bellinger static void tcm_loop_release_core_bus(void)
4383703b2c5SNicholas Bellinger {
4393703b2c5SNicholas Bellinger driver_unregister(&tcm_loop_driverfs);
4403703b2c5SNicholas Bellinger bus_unregister(&tcm_loop_lld_bus);
4413703b2c5SNicholas Bellinger root_device_unregister(tcm_loop_primary);
4423703b2c5SNicholas Bellinger
4436708bb27SAndy Grover pr_debug("Releasing TCM Loop Core BUS\n");
4443703b2c5SNicholas Bellinger }
4453703b2c5SNicholas Bellinger
tl_tpg(struct se_portal_group * se_tpg)4461667a459SChristoph Hellwig static inline struct tcm_loop_tpg *tl_tpg(struct se_portal_group *se_tpg)
4471667a459SChristoph Hellwig {
4481667a459SChristoph Hellwig return container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
4491667a459SChristoph Hellwig }
4501667a459SChristoph Hellwig
tcm_loop_get_endpoint_wwn(struct se_portal_group * se_tpg)4513703b2c5SNicholas Bellinger static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
4523703b2c5SNicholas Bellinger {
4533703b2c5SNicholas Bellinger /*
454b7446cacSHannes Reinecke * Return the passed NAA identifier for the Target Port
4553703b2c5SNicholas Bellinger */
4561667a459SChristoph Hellwig return &tl_tpg(se_tpg)->tl_hba->tl_wwn_address[0];
4573703b2c5SNicholas Bellinger }
4583703b2c5SNicholas Bellinger
tcm_loop_get_tag(struct se_portal_group * se_tpg)4593703b2c5SNicholas Bellinger static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
4603703b2c5SNicholas Bellinger {
4613703b2c5SNicholas Bellinger /*
4623703b2c5SNicholas Bellinger * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
4633703b2c5SNicholas Bellinger * to represent the SCSI Target Port.
4643703b2c5SNicholas Bellinger */
4651667a459SChristoph Hellwig return tl_tpg(se_tpg)->tl_tpgt;
4663703b2c5SNicholas Bellinger }
4673703b2c5SNicholas Bellinger
4683703b2c5SNicholas Bellinger /*
4693703b2c5SNicholas Bellinger * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
4703703b2c5SNicholas Bellinger * based upon the incoming fabric dependent SCSI Initiator Port
4713703b2c5SNicholas Bellinger */
tcm_loop_check_demo_mode(struct se_portal_group * se_tpg)4723703b2c5SNicholas Bellinger static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
4733703b2c5SNicholas Bellinger {
4743703b2c5SNicholas Bellinger return 1;
4753703b2c5SNicholas Bellinger }
4763703b2c5SNicholas Bellinger
tcm_loop_check_prot_fabric_only(struct se_portal_group * se_tpg)477436f4a0aSNicholas Bellinger static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg)
478436f4a0aSNicholas Bellinger {
479436f4a0aSNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
480436f4a0aSNicholas Bellinger tl_se_tpg);
481436f4a0aSNicholas Bellinger return tl_tpg->tl_fabric_prot_type;
482436f4a0aSNicholas Bellinger }
483436f4a0aSNicholas Bellinger
tcm_loop_sess_get_index(struct se_session * se_sess)4843703b2c5SNicholas Bellinger static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
4853703b2c5SNicholas Bellinger {
4863703b2c5SNicholas Bellinger return 1;
4873703b2c5SNicholas Bellinger }
4883703b2c5SNicholas Bellinger
tcm_loop_get_cmd_state(struct se_cmd * se_cmd)4893703b2c5SNicholas Bellinger static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
4903703b2c5SNicholas Bellinger {
4913703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
4923703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd);
4933703b2c5SNicholas Bellinger
4943703b2c5SNicholas Bellinger return tl_cmd->sc_cmd_state;
4953703b2c5SNicholas Bellinger }
4963703b2c5SNicholas Bellinger
tcm_loop_write_pending(struct se_cmd * se_cmd)4973703b2c5SNicholas Bellinger static int tcm_loop_write_pending(struct se_cmd *se_cmd)
4983703b2c5SNicholas Bellinger {
4993703b2c5SNicholas Bellinger /*
5003703b2c5SNicholas Bellinger * Since Linux/SCSI has already sent down a struct scsi_cmnd
5013703b2c5SNicholas Bellinger * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
5023703b2c5SNicholas Bellinger * memory, and memory has already been mapped to struct se_cmd->t_mem_list
5033703b2c5SNicholas Bellinger * format with transport_generic_map_mem_to_cmd().
5043703b2c5SNicholas Bellinger *
5053703b2c5SNicholas Bellinger * We now tell TCM to add this WRITE CDB directly into the TCM storage
5063703b2c5SNicholas Bellinger * object execution queue.
5073703b2c5SNicholas Bellinger */
50870baf0abSChristoph Hellwig target_execute_cmd(se_cmd);
5093703b2c5SNicholas Bellinger return 0;
5103703b2c5SNicholas Bellinger }
5113703b2c5SNicholas Bellinger
tcm_loop_queue_data_or_status(const char * func,struct se_cmd * se_cmd,u8 scsi_status)512c68a5673SBodo Stroesser static int tcm_loop_queue_data_or_status(const char *func,
513c68a5673SBodo Stroesser struct se_cmd *se_cmd, u8 scsi_status)
5143703b2c5SNicholas Bellinger {
5153703b2c5SNicholas Bellinger struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
5163703b2c5SNicholas Bellinger struct tcm_loop_cmd, tl_se_cmd);
5173703b2c5SNicholas Bellinger struct scsi_cmnd *sc = tl_cmd->sc;
5183703b2c5SNicholas Bellinger
519c8d1f4b7SMarkus Elfring pr_debug("%s() called for scsi_cmnd: %p cdb: 0x%02x\n",
520c68a5673SBodo Stroesser func, sc, sc->cmnd[0]);
5213703b2c5SNicholas Bellinger
5223703b2c5SNicholas Bellinger if (se_cmd->sense_buffer &&
5233703b2c5SNicholas Bellinger ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
5243703b2c5SNicholas Bellinger (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
5253703b2c5SNicholas Bellinger
5265951146dSAndy Grover memcpy(sc->sense_buffer, se_cmd->sense_buffer,
5273703b2c5SNicholas Bellinger SCSI_SENSE_BUFFERSIZE);
5283703b2c5SNicholas Bellinger sc->result = SAM_STAT_CHECK_CONDITION;
5293703b2c5SNicholas Bellinger } else
530c68a5673SBodo Stroesser sc->result = scsi_status;
5313703b2c5SNicholas Bellinger
5323703b2c5SNicholas Bellinger set_host_byte(sc, DID_OK);
5336cf3fa69SRoland Dreier if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
5346cf3fa69SRoland Dreier (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
5356cf3fa69SRoland Dreier scsi_set_resid(sc, se_cmd->residual_count);
5363703b2c5SNicholas Bellinger return 0;
5373703b2c5SNicholas Bellinger }
5383703b2c5SNicholas Bellinger
tcm_loop_queue_data_in(struct se_cmd * se_cmd)539c68a5673SBodo Stroesser static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
540c68a5673SBodo Stroesser {
541c68a5673SBodo Stroesser return tcm_loop_queue_data_or_status(__func__, se_cmd, SAM_STAT_GOOD);
542c68a5673SBodo Stroesser }
543c68a5673SBodo Stroesser
tcm_loop_queue_status(struct se_cmd * se_cmd)544c68a5673SBodo Stroesser static int tcm_loop_queue_status(struct se_cmd *se_cmd)
545c68a5673SBodo Stroesser {
546c68a5673SBodo Stroesser return tcm_loop_queue_data_or_status(__func__,
547c68a5673SBodo Stroesser se_cmd, se_cmd->scsi_status);
548c68a5673SBodo Stroesser }
549c68a5673SBodo Stroesser
tcm_loop_queue_tm_rsp(struct se_cmd * se_cmd)550b79fafacSJoern Engel static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
5513703b2c5SNicholas Bellinger {
5524d3895d5SBart Van Assche struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
5534d3895d5SBart Van Assche struct tcm_loop_cmd, tl_se_cmd);
5544d3895d5SBart Van Assche
555d17203c4SBart Van Assche /* Wake up tcm_loop_issue_tmr(). */
556d17203c4SBart Van Assche complete(&tl_cmd->tmr_done);
5573703b2c5SNicholas Bellinger }
5583703b2c5SNicholas Bellinger
tcm_loop_aborted_task(struct se_cmd * se_cmd)559131e6abcSNicholas Bellinger static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
560131e6abcSNicholas Bellinger {
561131e6abcSNicholas Bellinger return;
562131e6abcSNicholas Bellinger }
563131e6abcSNicholas Bellinger
tcm_loop_dump_proto_id(struct tcm_loop_hba * tl_hba)5643703b2c5SNicholas Bellinger static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
5653703b2c5SNicholas Bellinger {
5663703b2c5SNicholas Bellinger switch (tl_hba->tl_proto_id) {
5673703b2c5SNicholas Bellinger case SCSI_PROTOCOL_SAS:
5683703b2c5SNicholas Bellinger return "SAS";
5693703b2c5SNicholas Bellinger case SCSI_PROTOCOL_FCP:
5703703b2c5SNicholas Bellinger return "FCP";
5713703b2c5SNicholas Bellinger case SCSI_PROTOCOL_ISCSI:
5723703b2c5SNicholas Bellinger return "iSCSI";
5733703b2c5SNicholas Bellinger default:
5743703b2c5SNicholas Bellinger break;
5753703b2c5SNicholas Bellinger }
5763703b2c5SNicholas Bellinger
5773703b2c5SNicholas Bellinger return "Unknown";
5783703b2c5SNicholas Bellinger }
5793703b2c5SNicholas Bellinger
5803703b2c5SNicholas Bellinger /* Start items for tcm_loop_port_cit */
5813703b2c5SNicholas Bellinger
tcm_loop_port_link(struct se_portal_group * se_tpg,struct se_lun * lun)5823703b2c5SNicholas Bellinger static int tcm_loop_port_link(
5833703b2c5SNicholas Bellinger struct se_portal_group *se_tpg,
5843703b2c5SNicholas Bellinger struct se_lun *lun)
5853703b2c5SNicholas Bellinger {
5863703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
5873703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg);
5883703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
5893703b2c5SNicholas Bellinger
59033940d09SJoern Engel atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
5913703b2c5SNicholas Bellinger /*
5923703b2c5SNicholas Bellinger * Add Linux/SCSI struct scsi_device by HCTL
5933703b2c5SNicholas Bellinger */
5943703b2c5SNicholas Bellinger scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
5953703b2c5SNicholas Bellinger
5966708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
5973703b2c5SNicholas Bellinger return 0;
5983703b2c5SNicholas Bellinger }
5993703b2c5SNicholas Bellinger
tcm_loop_port_unlink(struct se_portal_group * se_tpg,struct se_lun * se_lun)6003703b2c5SNicholas Bellinger static void tcm_loop_port_unlink(
6013703b2c5SNicholas Bellinger struct se_portal_group *se_tpg,
6023703b2c5SNicholas Bellinger struct se_lun *se_lun)
6033703b2c5SNicholas Bellinger {
6043703b2c5SNicholas Bellinger struct scsi_device *sd;
6053703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
6063703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg;
6073703b2c5SNicholas Bellinger
6083703b2c5SNicholas Bellinger tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
6093703b2c5SNicholas Bellinger tl_hba = tl_tpg->tl_hba;
6103703b2c5SNicholas Bellinger
6113703b2c5SNicholas Bellinger sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
6123703b2c5SNicholas Bellinger se_lun->unpacked_lun);
6133703b2c5SNicholas Bellinger if (!sd) {
614c8d1f4b7SMarkus Elfring pr_err("Unable to locate struct scsi_device for %d:%d:%llu\n",
615c8d1f4b7SMarkus Elfring 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
6163703b2c5SNicholas Bellinger return;
6173703b2c5SNicholas Bellinger }
6183703b2c5SNicholas Bellinger /*
6193703b2c5SNicholas Bellinger * Remove Linux/SCSI struct scsi_device by HCTL
6203703b2c5SNicholas Bellinger */
6213703b2c5SNicholas Bellinger scsi_remove_device(sd);
6223703b2c5SNicholas Bellinger scsi_device_put(sd);
6233703b2c5SNicholas Bellinger
62433940d09SJoern Engel atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
6253703b2c5SNicholas Bellinger
6266708bb27SAndy Grover pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
6273703b2c5SNicholas Bellinger }
6283703b2c5SNicholas Bellinger
6293703b2c5SNicholas Bellinger /* End items for tcm_loop_port_cit */
6303703b2c5SNicholas Bellinger
tcm_loop_tpg_attrib_fabric_prot_type_show(struct config_item * item,char * page)6312eafd729SChristoph Hellwig static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_show(
6322eafd729SChristoph Hellwig struct config_item *item, char *page)
633436f4a0aSNicholas Bellinger {
6342eafd729SChristoph Hellwig struct se_portal_group *se_tpg = attrib_to_tpg(item);
635436f4a0aSNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
636436f4a0aSNicholas Bellinger tl_se_tpg);
637436f4a0aSNicholas Bellinger
638436f4a0aSNicholas Bellinger return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type);
639436f4a0aSNicholas Bellinger }
640436f4a0aSNicholas Bellinger
tcm_loop_tpg_attrib_fabric_prot_type_store(struct config_item * item,const char * page,size_t count)6412eafd729SChristoph Hellwig static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_store(
6422eafd729SChristoph Hellwig struct config_item *item, const char *page, size_t count)
643436f4a0aSNicholas Bellinger {
6442eafd729SChristoph Hellwig struct se_portal_group *se_tpg = attrib_to_tpg(item);
645436f4a0aSNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
646436f4a0aSNicholas Bellinger tl_se_tpg);
647436f4a0aSNicholas Bellinger unsigned long val;
648436f4a0aSNicholas Bellinger int ret = kstrtoul(page, 0, &val);
649436f4a0aSNicholas Bellinger
650436f4a0aSNicholas Bellinger if (ret) {
651436f4a0aSNicholas Bellinger pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
652436f4a0aSNicholas Bellinger return ret;
653436f4a0aSNicholas Bellinger }
654436f4a0aSNicholas Bellinger if (val != 0 && val != 1 && val != 3) {
655436f4a0aSNicholas Bellinger pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
656436f4a0aSNicholas Bellinger return -EINVAL;
657436f4a0aSNicholas Bellinger }
658436f4a0aSNicholas Bellinger tl_tpg->tl_fabric_prot_type = val;
659436f4a0aSNicholas Bellinger
660436f4a0aSNicholas Bellinger return count;
661436f4a0aSNicholas Bellinger }
662436f4a0aSNicholas Bellinger
6632eafd729SChristoph Hellwig CONFIGFS_ATTR(tcm_loop_tpg_attrib_, fabric_prot_type);
664436f4a0aSNicholas Bellinger
665436f4a0aSNicholas Bellinger static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = {
6662eafd729SChristoph Hellwig &tcm_loop_tpg_attrib_attr_fabric_prot_type,
667436f4a0aSNicholas Bellinger NULL,
668436f4a0aSNicholas Bellinger };
669436f4a0aSNicholas Bellinger
6703703b2c5SNicholas Bellinger /* Start items for tcm_loop_nexus_cit */
6713703b2c5SNicholas Bellinger
tcm_loop_alloc_sess_cb(struct se_portal_group * se_tpg,struct se_session * se_sess,void * p)672fb444abeSChristoph Hellwig static int tcm_loop_alloc_sess_cb(struct se_portal_group *se_tpg,
673fb444abeSChristoph Hellwig struct se_session *se_sess, void *p)
674fb444abeSChristoph Hellwig {
675fb444abeSChristoph Hellwig struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
676fb444abeSChristoph Hellwig struct tcm_loop_tpg, tl_se_tpg);
677fb444abeSChristoph Hellwig
678fb444abeSChristoph Hellwig tl_tpg->tl_nexus = p;
679fb444abeSChristoph Hellwig return 0;
680fb444abeSChristoph Hellwig }
681fb444abeSChristoph Hellwig
tcm_loop_make_nexus(struct tcm_loop_tpg * tl_tpg,const char * name)6823703b2c5SNicholas Bellinger static int tcm_loop_make_nexus(
6833703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg,
6843703b2c5SNicholas Bellinger const char *name)
6853703b2c5SNicholas Bellinger {
6863703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
6873703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus;
688fb444abeSChristoph Hellwig int ret;
6893703b2c5SNicholas Bellinger
690506787a2SHannes Reinecke if (tl_tpg->tl_nexus) {
691506787a2SHannes Reinecke pr_debug("tl_tpg->tl_nexus already exists\n");
6923703b2c5SNicholas Bellinger return -EEXIST;
6933703b2c5SNicholas Bellinger }
6943703b2c5SNicholas Bellinger
695a572dba9SMarkus Elfring tl_nexus = kzalloc(sizeof(*tl_nexus), GFP_KERNEL);
696cd3fb32aSMarkus Elfring if (!tl_nexus)
6973703b2c5SNicholas Bellinger return -ENOMEM;
698fb444abeSChristoph Hellwig
699fa834287SMike Christie tl_nexus->se_sess = target_setup_session(&tl_tpg->tl_se_tpg, 0, 0,
700fb444abeSChristoph Hellwig TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS,
701fb444abeSChristoph Hellwig name, tl_nexus, tcm_loop_alloc_sess_cb);
702552523dcSDan Carpenter if (IS_ERR(tl_nexus->se_sess)) {
703552523dcSDan Carpenter ret = PTR_ERR(tl_nexus->se_sess);
704fb444abeSChristoph Hellwig kfree(tl_nexus);
705fb444abeSChristoph Hellwig return ret;
706552523dcSDan Carpenter }
707fb444abeSChristoph Hellwig
708c8d1f4b7SMarkus Elfring pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated %s Initiator Port: %s\n",
709c8d1f4b7SMarkus Elfring tcm_loop_dump_proto_id(tl_hba), name);
7103703b2c5SNicholas Bellinger return 0;
7113703b2c5SNicholas Bellinger }
7123703b2c5SNicholas Bellinger
tcm_loop_drop_nexus(struct tcm_loop_tpg * tpg)7133703b2c5SNicholas Bellinger static int tcm_loop_drop_nexus(
7143703b2c5SNicholas Bellinger struct tcm_loop_tpg *tpg)
7153703b2c5SNicholas Bellinger {
7163703b2c5SNicholas Bellinger struct se_session *se_sess;
7173703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus;
7183703b2c5SNicholas Bellinger
719506787a2SHannes Reinecke tl_nexus = tpg->tl_nexus;
7203703b2c5SNicholas Bellinger if (!tl_nexus)
7213703b2c5SNicholas Bellinger return -ENODEV;
7223703b2c5SNicholas Bellinger
7233703b2c5SNicholas Bellinger se_sess = tl_nexus->se_sess;
7243703b2c5SNicholas Bellinger if (!se_sess)
7253703b2c5SNicholas Bellinger return -ENODEV;
7263703b2c5SNicholas Bellinger
7273703b2c5SNicholas Bellinger if (atomic_read(&tpg->tl_tpg_port_count)) {
728c8d1f4b7SMarkus Elfring pr_err("Unable to remove TCM_Loop I_T Nexus with active TPG port count: %d\n",
7293703b2c5SNicholas Bellinger atomic_read(&tpg->tl_tpg_port_count));
7303703b2c5SNicholas Bellinger return -EPERM;
7313703b2c5SNicholas Bellinger }
7323703b2c5SNicholas Bellinger
733c8d1f4b7SMarkus Elfring pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated %s Initiator Port: %s\n",
734c8d1f4b7SMarkus Elfring tcm_loop_dump_proto_id(tpg->tl_hba),
7353703b2c5SNicholas Bellinger tl_nexus->se_sess->se_node_acl->initiatorname);
7363703b2c5SNicholas Bellinger /*
737b7446cacSHannes Reinecke * Release the SCSI I_T Nexus to the emulated Target Port
7383703b2c5SNicholas Bellinger */
73925b88550SMike Christie target_remove_session(se_sess);
740506787a2SHannes Reinecke tpg->tl_nexus = NULL;
7413703b2c5SNicholas Bellinger kfree(tl_nexus);
7423703b2c5SNicholas Bellinger return 0;
7433703b2c5SNicholas Bellinger }
7443703b2c5SNicholas Bellinger
7453703b2c5SNicholas Bellinger /* End items for tcm_loop_nexus_cit */
7463703b2c5SNicholas Bellinger
tcm_loop_tpg_nexus_show(struct config_item * item,char * page)7472eafd729SChristoph Hellwig static ssize_t tcm_loop_tpg_nexus_show(struct config_item *item, char *page)
7483703b2c5SNicholas Bellinger {
7492eafd729SChristoph Hellwig struct se_portal_group *se_tpg = to_tpg(item);
7503703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
7513703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg);
7523703b2c5SNicholas Bellinger struct tcm_loop_nexus *tl_nexus;
7533703b2c5SNicholas Bellinger ssize_t ret;
7543703b2c5SNicholas Bellinger
755506787a2SHannes Reinecke tl_nexus = tl_tpg->tl_nexus;
7563703b2c5SNicholas Bellinger if (!tl_nexus)
7573703b2c5SNicholas Bellinger return -ENODEV;
7583703b2c5SNicholas Bellinger
7593703b2c5SNicholas Bellinger ret = snprintf(page, PAGE_SIZE, "%s\n",
7603703b2c5SNicholas Bellinger tl_nexus->se_sess->se_node_acl->initiatorname);
7613703b2c5SNicholas Bellinger
7623703b2c5SNicholas Bellinger return ret;
7633703b2c5SNicholas Bellinger }
7643703b2c5SNicholas Bellinger
tcm_loop_tpg_nexus_store(struct config_item * item,const char * page,size_t count)7652eafd729SChristoph Hellwig static ssize_t tcm_loop_tpg_nexus_store(struct config_item *item,
7662eafd729SChristoph Hellwig const char *page, size_t count)
7673703b2c5SNicholas Bellinger {
7682eafd729SChristoph Hellwig struct se_portal_group *se_tpg = to_tpg(item);
7693703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
7703703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg);
7713703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
7723703b2c5SNicholas Bellinger unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
7733703b2c5SNicholas Bellinger int ret;
7743703b2c5SNicholas Bellinger /*
7753703b2c5SNicholas Bellinger * Shutdown the active I_T nexus if 'NULL' is passed..
7763703b2c5SNicholas Bellinger */
7773703b2c5SNicholas Bellinger if (!strncmp(page, "NULL", 4)) {
7783703b2c5SNicholas Bellinger ret = tcm_loop_drop_nexus(tl_tpg);
7793703b2c5SNicholas Bellinger return (!ret) ? count : ret;
7803703b2c5SNicholas Bellinger }
7813703b2c5SNicholas Bellinger /*
7823703b2c5SNicholas Bellinger * Otherwise make sure the passed virtual Initiator port WWN matches
7833703b2c5SNicholas Bellinger * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
7843703b2c5SNicholas Bellinger * tcm_loop_make_nexus()
7853703b2c5SNicholas Bellinger */
78660d645a4SDan Carpenter if (strlen(page) >= TL_WWN_ADDR_LEN) {
787c8d1f4b7SMarkus Elfring pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
788c8d1f4b7SMarkus Elfring page, TL_WWN_ADDR_LEN);
7893703b2c5SNicholas Bellinger return -EINVAL;
7903703b2c5SNicholas Bellinger }
7913703b2c5SNicholas Bellinger snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
7923703b2c5SNicholas Bellinger
7933703b2c5SNicholas Bellinger ptr = strstr(i_port, "naa.");
7943703b2c5SNicholas Bellinger if (ptr) {
7953703b2c5SNicholas Bellinger if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
796c8d1f4b7SMarkus Elfring pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
797c8d1f4b7SMarkus Elfring i_port, tcm_loop_dump_proto_id(tl_hba));
7983703b2c5SNicholas Bellinger return -EINVAL;
7993703b2c5SNicholas Bellinger }
8003703b2c5SNicholas Bellinger port_ptr = &i_port[0];
8013703b2c5SNicholas Bellinger goto check_newline;
8023703b2c5SNicholas Bellinger }
8033703b2c5SNicholas Bellinger ptr = strstr(i_port, "fc.");
8043703b2c5SNicholas Bellinger if (ptr) {
8053703b2c5SNicholas Bellinger if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
806c8d1f4b7SMarkus Elfring pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
807c8d1f4b7SMarkus Elfring i_port, tcm_loop_dump_proto_id(tl_hba));
8083703b2c5SNicholas Bellinger return -EINVAL;
8093703b2c5SNicholas Bellinger }
8103703b2c5SNicholas Bellinger port_ptr = &i_port[3]; /* Skip over "fc." */
8113703b2c5SNicholas Bellinger goto check_newline;
8123703b2c5SNicholas Bellinger }
8133703b2c5SNicholas Bellinger ptr = strstr(i_port, "iqn.");
8143703b2c5SNicholas Bellinger if (ptr) {
8153703b2c5SNicholas Bellinger if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
816c8d1f4b7SMarkus Elfring pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
817c8d1f4b7SMarkus Elfring i_port, tcm_loop_dump_proto_id(tl_hba));
8183703b2c5SNicholas Bellinger return -EINVAL;
8193703b2c5SNicholas Bellinger }
8203703b2c5SNicholas Bellinger port_ptr = &i_port[0];
8213703b2c5SNicholas Bellinger goto check_newline;
8223703b2c5SNicholas Bellinger }
823c8d1f4b7SMarkus Elfring pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
824c8d1f4b7SMarkus Elfring i_port);
8253703b2c5SNicholas Bellinger return -EINVAL;
8263703b2c5SNicholas Bellinger /*
8273703b2c5SNicholas Bellinger * Clear any trailing newline for the NAA WWN
8283703b2c5SNicholas Bellinger */
8293703b2c5SNicholas Bellinger check_newline:
8303703b2c5SNicholas Bellinger if (i_port[strlen(i_port)-1] == '\n')
8313703b2c5SNicholas Bellinger i_port[strlen(i_port)-1] = '\0';
8323703b2c5SNicholas Bellinger
8333703b2c5SNicholas Bellinger ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
8343703b2c5SNicholas Bellinger if (ret < 0)
8353703b2c5SNicholas Bellinger return ret;
8363703b2c5SNicholas Bellinger
8373703b2c5SNicholas Bellinger return count;
8383703b2c5SNicholas Bellinger }
8393703b2c5SNicholas Bellinger
tcm_loop_tpg_transport_status_show(struct config_item * item,char * page)8402eafd729SChristoph Hellwig static ssize_t tcm_loop_tpg_transport_status_show(struct config_item *item,
841fb2b2844SHannes Reinecke char *page)
842fb2b2844SHannes Reinecke {
8432eafd729SChristoph Hellwig struct se_portal_group *se_tpg = to_tpg(item);
844fb2b2844SHannes Reinecke struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
845fb2b2844SHannes Reinecke struct tcm_loop_tpg, tl_se_tpg);
846fb2b2844SHannes Reinecke const char *status = NULL;
847fb2b2844SHannes Reinecke ssize_t ret = -EINVAL;
848fb2b2844SHannes Reinecke
849fb2b2844SHannes Reinecke switch (tl_tpg->tl_transport_status) {
850fb2b2844SHannes Reinecke case TCM_TRANSPORT_ONLINE:
851fb2b2844SHannes Reinecke status = "online";
852fb2b2844SHannes Reinecke break;
853fb2b2844SHannes Reinecke case TCM_TRANSPORT_OFFLINE:
854fb2b2844SHannes Reinecke status = "offline";
855fb2b2844SHannes Reinecke break;
856fb2b2844SHannes Reinecke default:
857fb2b2844SHannes Reinecke break;
858fb2b2844SHannes Reinecke }
859fb2b2844SHannes Reinecke
860fb2b2844SHannes Reinecke if (status)
861fb2b2844SHannes Reinecke ret = snprintf(page, PAGE_SIZE, "%s\n", status);
862fb2b2844SHannes Reinecke
863fb2b2844SHannes Reinecke return ret;
864fb2b2844SHannes Reinecke }
865fb2b2844SHannes Reinecke
tcm_loop_tpg_transport_status_store(struct config_item * item,const char * page,size_t count)8662eafd729SChristoph Hellwig static ssize_t tcm_loop_tpg_transport_status_store(struct config_item *item,
8672eafd729SChristoph Hellwig const char *page, size_t count)
868fb2b2844SHannes Reinecke {
8692eafd729SChristoph Hellwig struct se_portal_group *se_tpg = to_tpg(item);
870fb2b2844SHannes Reinecke struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
871fb2b2844SHannes Reinecke struct tcm_loop_tpg, tl_se_tpg);
872fb2b2844SHannes Reinecke
873fb2b2844SHannes Reinecke if (!strncmp(page, "online", 6)) {
874fb2b2844SHannes Reinecke tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
875fb2b2844SHannes Reinecke return count;
876fb2b2844SHannes Reinecke }
877fb2b2844SHannes Reinecke if (!strncmp(page, "offline", 7)) {
878fb2b2844SHannes Reinecke tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
879e986a35aSHannes Reinecke if (tl_tpg->tl_nexus) {
880e986a35aSHannes Reinecke struct se_session *tl_sess = tl_tpg->tl_nexus->se_sess;
881e986a35aSHannes Reinecke
882e986a35aSHannes Reinecke core_allocate_nexus_loss_ua(tl_sess->se_node_acl);
883e986a35aSHannes Reinecke }
884fb2b2844SHannes Reinecke return count;
885fb2b2844SHannes Reinecke }
886fb2b2844SHannes Reinecke return -EINVAL;
887fb2b2844SHannes Reinecke }
888fb2b2844SHannes Reinecke
tcm_loop_tpg_address_show(struct config_item * item,char * page)8892628b352SSheng Yang static ssize_t tcm_loop_tpg_address_show(struct config_item *item,
8902628b352SSheng Yang char *page)
8912628b352SSheng Yang {
8922628b352SSheng Yang struct se_portal_group *se_tpg = to_tpg(item);
8932628b352SSheng Yang struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
8942628b352SSheng Yang struct tcm_loop_tpg, tl_se_tpg);
8952628b352SSheng Yang struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
8962628b352SSheng Yang
8972628b352SSheng Yang return snprintf(page, PAGE_SIZE, "%d:0:%d\n",
8982628b352SSheng Yang tl_hba->sh->host_no, tl_tpg->tl_tpgt);
8992628b352SSheng Yang }
9002628b352SSheng Yang
9012eafd729SChristoph Hellwig CONFIGFS_ATTR(tcm_loop_tpg_, nexus);
9022eafd729SChristoph Hellwig CONFIGFS_ATTR(tcm_loop_tpg_, transport_status);
9032628b352SSheng Yang CONFIGFS_ATTR_RO(tcm_loop_tpg_, address);
904fb2b2844SHannes Reinecke
9053703b2c5SNicholas Bellinger static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
9062eafd729SChristoph Hellwig &tcm_loop_tpg_attr_nexus,
9072eafd729SChristoph Hellwig &tcm_loop_tpg_attr_transport_status,
9082628b352SSheng Yang &tcm_loop_tpg_attr_address,
9093703b2c5SNicholas Bellinger NULL,
9103703b2c5SNicholas Bellinger };
9113703b2c5SNicholas Bellinger
9123703b2c5SNicholas Bellinger /* Start items for tcm_loop_naa_cit */
9133703b2c5SNicholas Bellinger
tcm_loop_make_naa_tpg(struct se_wwn * wwn,const char * name)914aa090eabSBart Van Assche static struct se_portal_group *tcm_loop_make_naa_tpg(struct se_wwn *wwn,
9153703b2c5SNicholas Bellinger const char *name)
9163703b2c5SNicholas Bellinger {
9173703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = container_of(wwn,
9183703b2c5SNicholas Bellinger struct tcm_loop_hba, tl_hba_wwn);
9193703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg;
9203703b2c5SNicholas Bellinger int ret;
9212e1cd90dSMing Lin unsigned long tpgt;
9223703b2c5SNicholas Bellinger
9232e1cd90dSMing Lin if (strstr(name, "tpgt_") != name) {
924c8d1f4b7SMarkus Elfring pr_err("Unable to locate \"tpgt_#\" directory group\n");
9253703b2c5SNicholas Bellinger return ERR_PTR(-EINVAL);
9263703b2c5SNicholas Bellinger }
9272e1cd90dSMing Lin if (kstrtoul(name+5, 10, &tpgt))
9282e1cd90dSMing Lin return ERR_PTR(-EINVAL);
9293703b2c5SNicholas Bellinger
93012f09ccbSDan Carpenter if (tpgt >= TL_TPGS_PER_HBA) {
931c8d1f4b7SMarkus Elfring pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA: %u\n",
932c8d1f4b7SMarkus Elfring tpgt, TL_TPGS_PER_HBA);
9333703b2c5SNicholas Bellinger return ERR_PTR(-EINVAL);
9343703b2c5SNicholas Bellinger }
9353703b2c5SNicholas Bellinger tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
9363703b2c5SNicholas Bellinger tl_tpg->tl_hba = tl_hba;
9373703b2c5SNicholas Bellinger tl_tpg->tl_tpgt = tpgt;
9383703b2c5SNicholas Bellinger /*
939b7446cacSHannes Reinecke * Register the tl_tpg as a emulated TCM Target Endpoint
9403703b2c5SNicholas Bellinger */
941bc0c94b1SNicholas Bellinger ret = core_tpg_register(wwn, &tl_tpg->tl_se_tpg, tl_hba->tl_proto_id);
9423703b2c5SNicholas Bellinger if (ret < 0)
9433703b2c5SNicholas Bellinger return ERR_PTR(-ENOMEM);
9443703b2c5SNicholas Bellinger
945c8d1f4b7SMarkus Elfring pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s Target Port %s,t,0x%04lx\n",
946c8d1f4b7SMarkus Elfring tcm_loop_dump_proto_id(tl_hba),
9473703b2c5SNicholas Bellinger config_item_name(&wwn->wwn_group.cg_item), tpgt);
9483703b2c5SNicholas Bellinger return &tl_tpg->tl_se_tpg;
9493703b2c5SNicholas Bellinger }
9503703b2c5SNicholas Bellinger
tcm_loop_drop_naa_tpg(struct se_portal_group * se_tpg)951f0a6c693SRashika Kheria static void tcm_loop_drop_naa_tpg(
9523703b2c5SNicholas Bellinger struct se_portal_group *se_tpg)
9533703b2c5SNicholas Bellinger {
9543703b2c5SNicholas Bellinger struct se_wwn *wwn = se_tpg->se_tpg_wwn;
9553703b2c5SNicholas Bellinger struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
9563703b2c5SNicholas Bellinger struct tcm_loop_tpg, tl_se_tpg);
9573703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
9583703b2c5SNicholas Bellinger unsigned short tpgt;
9593703b2c5SNicholas Bellinger
9603703b2c5SNicholas Bellinger tl_hba = tl_tpg->tl_hba;
9613703b2c5SNicholas Bellinger tpgt = tl_tpg->tl_tpgt;
9623703b2c5SNicholas Bellinger /*
963b7446cacSHannes Reinecke * Release the I_T Nexus for the Virtual target link if present
9643703b2c5SNicholas Bellinger */
9653703b2c5SNicholas Bellinger tcm_loop_drop_nexus(tl_tpg);
9663703b2c5SNicholas Bellinger /*
967b7446cacSHannes Reinecke * Deregister the tl_tpg as a emulated TCM Target Endpoint
9683703b2c5SNicholas Bellinger */
9693703b2c5SNicholas Bellinger core_tpg_deregister(se_tpg);
9703703b2c5SNicholas Bellinger
9710a020436SNicholas Bellinger tl_tpg->tl_hba = NULL;
9720a020436SNicholas Bellinger tl_tpg->tl_tpgt = 0;
9730a020436SNicholas Bellinger
974c8d1f4b7SMarkus Elfring pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s Target Port %s,t,0x%04x\n",
975c8d1f4b7SMarkus Elfring tcm_loop_dump_proto_id(tl_hba),
9763703b2c5SNicholas Bellinger config_item_name(&wwn->wwn_group.cg_item), tpgt);
9773703b2c5SNicholas Bellinger }
9783703b2c5SNicholas Bellinger
9793703b2c5SNicholas Bellinger /* End items for tcm_loop_naa_cit */
9803703b2c5SNicholas Bellinger
9813703b2c5SNicholas Bellinger /* Start items for tcm_loop_cit */
9823703b2c5SNicholas Bellinger
tcm_loop_make_scsi_hba(struct target_fabric_configfs * tf,struct config_group * group,const char * name)983f0a6c693SRashika Kheria static struct se_wwn *tcm_loop_make_scsi_hba(
9843703b2c5SNicholas Bellinger struct target_fabric_configfs *tf,
9853703b2c5SNicholas Bellinger struct config_group *group,
9863703b2c5SNicholas Bellinger const char *name)
9873703b2c5SNicholas Bellinger {
9883703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba;
9893703b2c5SNicholas Bellinger struct Scsi_Host *sh;
9903703b2c5SNicholas Bellinger char *ptr;
9913703b2c5SNicholas Bellinger int ret, off = 0;
9923703b2c5SNicholas Bellinger
993a572dba9SMarkus Elfring tl_hba = kzalloc(sizeof(*tl_hba), GFP_KERNEL);
994cd3fb32aSMarkus Elfring if (!tl_hba)
9953703b2c5SNicholas Bellinger return ERR_PTR(-ENOMEM);
996cd3fb32aSMarkus Elfring
9973703b2c5SNicholas Bellinger /*
9983703b2c5SNicholas Bellinger * Determine the emulated Protocol Identifier and Target Port Name
9993703b2c5SNicholas Bellinger * based on the incoming configfs directory name.
10003703b2c5SNicholas Bellinger */
10013703b2c5SNicholas Bellinger ptr = strstr(name, "naa.");
10023703b2c5SNicholas Bellinger if (ptr) {
10033703b2c5SNicholas Bellinger tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
10043703b2c5SNicholas Bellinger goto check_len;
10053703b2c5SNicholas Bellinger }
10063703b2c5SNicholas Bellinger ptr = strstr(name, "fc.");
10073703b2c5SNicholas Bellinger if (ptr) {
10083703b2c5SNicholas Bellinger tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
10093703b2c5SNicholas Bellinger off = 3; /* Skip over "fc." */
10103703b2c5SNicholas Bellinger goto check_len;
10113703b2c5SNicholas Bellinger }
10123703b2c5SNicholas Bellinger ptr = strstr(name, "iqn.");
1013a57b5d36SJesper Juhl if (!ptr) {
1014c8d1f4b7SMarkus Elfring pr_err("Unable to locate prefix for emulated Target Port: %s\n",
1015c8d1f4b7SMarkus Elfring name);
1016a57b5d36SJesper Juhl ret = -EINVAL;
1017a57b5d36SJesper Juhl goto out;
10183703b2c5SNicholas Bellinger }
1019a57b5d36SJesper Juhl tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
10203703b2c5SNicholas Bellinger
10213703b2c5SNicholas Bellinger check_len:
102260d645a4SDan Carpenter if (strlen(name) >= TL_WWN_ADDR_LEN) {
1023c8d1f4b7SMarkus Elfring pr_err("Emulated NAA %s Address: %s, exceeds max: %d\n",
1024c8d1f4b7SMarkus Elfring name, tcm_loop_dump_proto_id(tl_hba), TL_WWN_ADDR_LEN);
1025a57b5d36SJesper Juhl ret = -EINVAL;
1026a57b5d36SJesper Juhl goto out;
10273703b2c5SNicholas Bellinger }
10283703b2c5SNicholas Bellinger snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
10293703b2c5SNicholas Bellinger
10303703b2c5SNicholas Bellinger /*
10313703b2c5SNicholas Bellinger * Call device_register(tl_hba->dev) to register the emulated
10323703b2c5SNicholas Bellinger * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
10333703b2c5SNicholas Bellinger * device_register() callbacks in tcm_loop_driver_probe()
10343703b2c5SNicholas Bellinger */
10353703b2c5SNicholas Bellinger ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
10363703b2c5SNicholas Bellinger if (ret)
1037bc68e428SYang Yingliang return ERR_PTR(ret);
10383703b2c5SNicholas Bellinger
10393703b2c5SNicholas Bellinger sh = tl_hba->sh;
10403703b2c5SNicholas Bellinger tcm_loop_hba_no_cnt++;
1041c8d1f4b7SMarkus Elfring pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
10423703b2c5SNicholas Bellinger tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
10433703b2c5SNicholas Bellinger return &tl_hba->tl_hba_wwn;
10443703b2c5SNicholas Bellinger out:
10453703b2c5SNicholas Bellinger kfree(tl_hba);
10463703b2c5SNicholas Bellinger return ERR_PTR(ret);
10473703b2c5SNicholas Bellinger }
10483703b2c5SNicholas Bellinger
tcm_loop_drop_scsi_hba(struct se_wwn * wwn)1049f0a6c693SRashika Kheria static void tcm_loop_drop_scsi_hba(
10503703b2c5SNicholas Bellinger struct se_wwn *wwn)
10513703b2c5SNicholas Bellinger {
10523703b2c5SNicholas Bellinger struct tcm_loop_hba *tl_hba = container_of(wwn,
10533703b2c5SNicholas Bellinger struct tcm_loop_hba, tl_hba_wwn);
10546297b07cSNicholas Bellinger
1055c8d1f4b7SMarkus Elfring pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
1056b7446cacSHannes Reinecke tcm_loop_dump_proto_id(tl_hba), tl_hba->tl_wwn_address,
1057b7446cacSHannes Reinecke tl_hba->sh->host_no);
10583703b2c5SNicholas Bellinger /*
10593703b2c5SNicholas Bellinger * Call device_unregister() on the original tl_hba->dev.
10603703b2c5SNicholas Bellinger * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
10613703b2c5SNicholas Bellinger * release *tl_hba;
10623703b2c5SNicholas Bellinger */
10633703b2c5SNicholas Bellinger device_unregister(&tl_hba->dev);
10643703b2c5SNicholas Bellinger }
10653703b2c5SNicholas Bellinger
10663703b2c5SNicholas Bellinger /* Start items for tcm_loop_cit */
tcm_loop_wwn_version_show(struct config_item * item,char * page)10672eafd729SChristoph Hellwig static ssize_t tcm_loop_wwn_version_show(struct config_item *item, char *page)
10683703b2c5SNicholas Bellinger {
10693703b2c5SNicholas Bellinger return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
10703703b2c5SNicholas Bellinger }
10713703b2c5SNicholas Bellinger
10722eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(tcm_loop_wwn_, version);
10733703b2c5SNicholas Bellinger
10743703b2c5SNicholas Bellinger static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
10752eafd729SChristoph Hellwig &tcm_loop_wwn_attr_version,
10763703b2c5SNicholas Bellinger NULL,
10773703b2c5SNicholas Bellinger };
10783703b2c5SNicholas Bellinger
10793703b2c5SNicholas Bellinger /* End items for tcm_loop_cit */
10803703b2c5SNicholas Bellinger
10819ac8928eSChristoph Hellwig static const struct target_core_fabric_ops loop_ops = {
10829ac8928eSChristoph Hellwig .module = THIS_MODULE,
108330c7ca93SDavid Disseldorp .fabric_name = "loopback",
10849ac8928eSChristoph Hellwig .tpg_get_wwn = tcm_loop_get_endpoint_wwn,
10859ac8928eSChristoph Hellwig .tpg_get_tag = tcm_loop_get_tag,
10869ac8928eSChristoph Hellwig .tpg_check_demo_mode = tcm_loop_check_demo_mode,
10879ac8928eSChristoph Hellwig .tpg_check_prot_fabric_only = tcm_loop_check_prot_fabric_only,
10889ac8928eSChristoph Hellwig .check_stop_free = tcm_loop_check_stop_free,
10899ac8928eSChristoph Hellwig .release_cmd = tcm_loop_release_cmd,
10909ac8928eSChristoph Hellwig .sess_get_index = tcm_loop_sess_get_index,
10919ac8928eSChristoph Hellwig .write_pending = tcm_loop_write_pending,
10929ac8928eSChristoph Hellwig .get_cmd_state = tcm_loop_get_cmd_state,
10939ac8928eSChristoph Hellwig .queue_data_in = tcm_loop_queue_data_in,
10949ac8928eSChristoph Hellwig .queue_status = tcm_loop_queue_status,
10959ac8928eSChristoph Hellwig .queue_tm_rsp = tcm_loop_queue_tm_rsp,
10969ac8928eSChristoph Hellwig .aborted_task = tcm_loop_aborted_task,
10979ac8928eSChristoph Hellwig .fabric_make_wwn = tcm_loop_make_scsi_hba,
10989ac8928eSChristoph Hellwig .fabric_drop_wwn = tcm_loop_drop_scsi_hba,
10999ac8928eSChristoph Hellwig .fabric_make_tpg = tcm_loop_make_naa_tpg,
11009ac8928eSChristoph Hellwig .fabric_drop_tpg = tcm_loop_drop_naa_tpg,
11019ac8928eSChristoph Hellwig .fabric_post_link = tcm_loop_port_link,
11029ac8928eSChristoph Hellwig .fabric_pre_unlink = tcm_loop_port_unlink,
11039ac8928eSChristoph Hellwig .tfc_wwn_attrs = tcm_loop_wwn_attrs,
11049ac8928eSChristoph Hellwig .tfc_tpg_base_attrs = tcm_loop_tpg_attrs,
11059ac8928eSChristoph Hellwig .tfc_tpg_attrib_attrs = tcm_loop_tpg_attrib_attrs,
1106194605d4SMike Christie .default_submit_type = TARGET_QUEUE_SUBMIT,
1107194605d4SMike Christie .direct_submit_supp = 0,
11089ac8928eSChristoph Hellwig };
11093703b2c5SNicholas Bellinger
tcm_loop_fabric_init(void)11103703b2c5SNicholas Bellinger static int __init tcm_loop_fabric_init(void)
11113703b2c5SNicholas Bellinger {
1112afe2cb7fSChristoph Hellwig int ret = -ENOMEM;
1113afe2cb7fSChristoph Hellwig
11143703b2c5SNicholas Bellinger tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
11153703b2c5SNicholas Bellinger sizeof(struct tcm_loop_cmd),
11163703b2c5SNicholas Bellinger __alignof__(struct tcm_loop_cmd),
11173703b2c5SNicholas Bellinger 0, NULL);
11183703b2c5SNicholas Bellinger if (!tcm_loop_cmd_cache) {
1119c8d1f4b7SMarkus Elfring pr_debug("kmem_cache_create() for tcm_loop_cmd_cache failed\n");
11201130b499SMike Christie goto out;
11213703b2c5SNicholas Bellinger }
11223703b2c5SNicholas Bellinger
11233703b2c5SNicholas Bellinger ret = tcm_loop_alloc_core_bus();
11243703b2c5SNicholas Bellinger if (ret)
1125afe2cb7fSChristoph Hellwig goto out_destroy_cache;
11263703b2c5SNicholas Bellinger
11279ac8928eSChristoph Hellwig ret = target_register_template(&loop_ops);
1128afe2cb7fSChristoph Hellwig if (ret)
1129afe2cb7fSChristoph Hellwig goto out_release_core_bus;
11303703b2c5SNicholas Bellinger
11313703b2c5SNicholas Bellinger return 0;
1132afe2cb7fSChristoph Hellwig
1133afe2cb7fSChristoph Hellwig out_release_core_bus:
1134afe2cb7fSChristoph Hellwig tcm_loop_release_core_bus();
1135afe2cb7fSChristoph Hellwig out_destroy_cache:
1136afe2cb7fSChristoph Hellwig kmem_cache_destroy(tcm_loop_cmd_cache);
1137afe2cb7fSChristoph Hellwig out:
1138afe2cb7fSChristoph Hellwig return ret;
11393703b2c5SNicholas Bellinger }
11403703b2c5SNicholas Bellinger
tcm_loop_fabric_exit(void)11413703b2c5SNicholas Bellinger static void __exit tcm_loop_fabric_exit(void)
11423703b2c5SNicholas Bellinger {
11439ac8928eSChristoph Hellwig target_unregister_template(&loop_ops);
11443703b2c5SNicholas Bellinger tcm_loop_release_core_bus();
11453703b2c5SNicholas Bellinger kmem_cache_destroy(tcm_loop_cmd_cache);
11463703b2c5SNicholas Bellinger }
11473703b2c5SNicholas Bellinger
11483703b2c5SNicholas Bellinger MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
11493703b2c5SNicholas Bellinger MODULE_AUTHOR("Nicholas A. Bellinger <[email protected]>");
11503703b2c5SNicholas Bellinger MODULE_LICENSE("GPL");
11513703b2c5SNicholas Bellinger module_init(tcm_loop_fabric_init);
11523703b2c5SNicholas Bellinger module_exit(tcm_loop_fabric_exit);
1153