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  *
63703b2c5SNicholas Bellinger  * © Copyright 2011 RisingTide Systems LLC.
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>
363703b2c5SNicholas Bellinger #include <target/target_core_transport.h>
373703b2c5SNicholas Bellinger #include <target/target_core_fabric_ops.h>
383703b2c5SNicholas Bellinger #include <target/target_core_fabric_configfs.h>
393703b2c5SNicholas Bellinger #include <target/target_core_fabric_lib.h>
403703b2c5SNicholas Bellinger #include <target/target_core_configfs.h>
413703b2c5SNicholas Bellinger #include <target/target_core_device.h>
423703b2c5SNicholas Bellinger #include <target/target_core_tpg.h>
433703b2c5SNicholas Bellinger #include <target/target_core_tmr.h>
443703b2c5SNicholas Bellinger 
453703b2c5SNicholas Bellinger #include "tcm_loop.h"
463703b2c5SNicholas Bellinger 
473703b2c5SNicholas Bellinger #define to_tcm_loop_hba(hba)	container_of(hba, struct tcm_loop_hba, dev)
483703b2c5SNicholas Bellinger 
493703b2c5SNicholas Bellinger /* Local pointer to allocated TCM configfs fabric module */
503703b2c5SNicholas Bellinger static struct target_fabric_configfs *tcm_loop_fabric_configfs;
513703b2c5SNicholas Bellinger 
523703b2c5SNicholas Bellinger static struct kmem_cache *tcm_loop_cmd_cache;
533703b2c5SNicholas Bellinger 
543703b2c5SNicholas Bellinger static int tcm_loop_hba_no_cnt;
553703b2c5SNicholas Bellinger 
563703b2c5SNicholas Bellinger /*
573703b2c5SNicholas Bellinger  * Allocate a tcm_loop cmd descriptor from target_core_mod code
583703b2c5SNicholas Bellinger  *
593703b2c5SNicholas Bellinger  * Can be called from interrupt context in tcm_loop_queuecommand() below
603703b2c5SNicholas Bellinger  */
613703b2c5SNicholas Bellinger static struct se_cmd *tcm_loop_allocate_core_cmd(
623703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba,
633703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg,
643703b2c5SNicholas Bellinger 	struct scsi_cmnd *sc)
653703b2c5SNicholas Bellinger {
663703b2c5SNicholas Bellinger 	struct se_cmd *se_cmd;
673703b2c5SNicholas Bellinger 	struct se_session *se_sess;
683703b2c5SNicholas Bellinger 	struct tcm_loop_nexus *tl_nexus = tl_hba->tl_nexus;
693703b2c5SNicholas Bellinger 	struct tcm_loop_cmd *tl_cmd;
703703b2c5SNicholas Bellinger 	int sam_task_attr;
713703b2c5SNicholas Bellinger 
723703b2c5SNicholas Bellinger 	if (!tl_nexus) {
733703b2c5SNicholas Bellinger 		scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
743703b2c5SNicholas Bellinger 				" does not exist\n");
753703b2c5SNicholas Bellinger 		set_host_byte(sc, DID_ERROR);
763703b2c5SNicholas Bellinger 		return NULL;
773703b2c5SNicholas Bellinger 	}
783703b2c5SNicholas Bellinger 	se_sess = tl_nexus->se_sess;
793703b2c5SNicholas Bellinger 
803703b2c5SNicholas Bellinger 	tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
813703b2c5SNicholas Bellinger 	if (!tl_cmd) {
826708bb27SAndy Grover 		pr_err("Unable to allocate struct tcm_loop_cmd\n");
833703b2c5SNicholas Bellinger 		set_host_byte(sc, DID_ERROR);
843703b2c5SNicholas Bellinger 		return NULL;
853703b2c5SNicholas Bellinger 	}
863703b2c5SNicholas Bellinger 	se_cmd = &tl_cmd->tl_se_cmd;
873703b2c5SNicholas Bellinger 	/*
883703b2c5SNicholas Bellinger 	 * Save the pointer to struct scsi_cmnd *sc
893703b2c5SNicholas Bellinger 	 */
903703b2c5SNicholas Bellinger 	tl_cmd->sc = sc;
913703b2c5SNicholas Bellinger 	/*
923703b2c5SNicholas Bellinger 	 * Locate the SAM Task Attr from struct scsi_cmnd *
933703b2c5SNicholas Bellinger 	 */
943703b2c5SNicholas Bellinger 	if (sc->device->tagged_supported) {
953703b2c5SNicholas Bellinger 		switch (sc->tag) {
963703b2c5SNicholas Bellinger 		case HEAD_OF_QUEUE_TAG:
9761db1802SNicholas Bellinger 			sam_task_attr = MSG_HEAD_TAG;
983703b2c5SNicholas Bellinger 			break;
993703b2c5SNicholas Bellinger 		case ORDERED_QUEUE_TAG:
10061db1802SNicholas Bellinger 			sam_task_attr = MSG_ORDERED_TAG;
1013703b2c5SNicholas Bellinger 			break;
1023703b2c5SNicholas Bellinger 		default:
10361db1802SNicholas Bellinger 			sam_task_attr = MSG_SIMPLE_TAG;
1043703b2c5SNicholas Bellinger 			break;
1053703b2c5SNicholas Bellinger 		}
1063703b2c5SNicholas Bellinger 	} else
10761db1802SNicholas Bellinger 		sam_task_attr = MSG_SIMPLE_TAG;
1083703b2c5SNicholas Bellinger 
1093703b2c5SNicholas Bellinger 	/*
1103703b2c5SNicholas Bellinger 	 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1113703b2c5SNicholas Bellinger 	 */
1123703b2c5SNicholas Bellinger 	transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1133703b2c5SNicholas Bellinger 			scsi_bufflen(sc), sc->sc_data_direction, sam_task_attr,
1143703b2c5SNicholas Bellinger 			&tl_cmd->tl_sense_buf[0]);
1153703b2c5SNicholas Bellinger 
1163703b2c5SNicholas Bellinger 	/*
1173703b2c5SNicholas Bellinger 	 * Signal BIDI usage with T_TASK(cmd)->t_tasks_bidi
1183703b2c5SNicholas Bellinger 	 */
1193703b2c5SNicholas Bellinger 	if (scsi_bidi_cmnd(sc))
120a1d8b49aSAndy Grover 		se_cmd->t_tasks_bidi = 1;
1213703b2c5SNicholas Bellinger 	/*
1223703b2c5SNicholas Bellinger 	 * Locate the struct se_lun pointer and attach it to struct se_cmd
1233703b2c5SNicholas Bellinger 	 */
1245951146dSAndy Grover 	if (transport_lookup_cmd_lun(se_cmd, tl_cmd->sc->device->lun) < 0) {
1253703b2c5SNicholas Bellinger 		kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
1263703b2c5SNicholas Bellinger 		set_host_byte(sc, DID_NO_CONNECT);
1273703b2c5SNicholas Bellinger 		return NULL;
1283703b2c5SNicholas Bellinger 	}
1293703b2c5SNicholas Bellinger 
1303703b2c5SNicholas Bellinger 	return se_cmd;
1313703b2c5SNicholas Bellinger }
1323703b2c5SNicholas Bellinger 
1333703b2c5SNicholas Bellinger /*
1343703b2c5SNicholas Bellinger  * Called by struct target_core_fabric_ops->new_cmd_map()
1353703b2c5SNicholas Bellinger  *
1363703b2c5SNicholas Bellinger  * Always called in process context.  A non zero return value
1373703b2c5SNicholas Bellinger  * here will signal to handle an exception based on the return code.
1383703b2c5SNicholas Bellinger  */
1393703b2c5SNicholas Bellinger static int tcm_loop_new_cmd_map(struct se_cmd *se_cmd)
1403703b2c5SNicholas Bellinger {
1413703b2c5SNicholas Bellinger 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
1423703b2c5SNicholas Bellinger 				struct tcm_loop_cmd, tl_se_cmd);
1433703b2c5SNicholas Bellinger 	struct scsi_cmnd *sc = tl_cmd->sc;
1445951146dSAndy Grover 	struct scatterlist *sgl_bidi = NULL;
1455951146dSAndy Grover 	u32 sgl_bidi_count = 0;
1463703b2c5SNicholas Bellinger 	int ret;
1473703b2c5SNicholas Bellinger 	/*
1483703b2c5SNicholas Bellinger 	 * Allocate the necessary tasks to complete the received CDB+data
1493703b2c5SNicholas Bellinger 	 */
1505951146dSAndy Grover 	ret = transport_generic_allocate_tasks(se_cmd, sc->cmnd);
1515951146dSAndy Grover 	if (ret == -ENOMEM) {
1523703b2c5SNicholas Bellinger 		/* Out of Resources */
1533703b2c5SNicholas Bellinger 		return PYX_TRANSPORT_LU_COMM_FAILURE;
1545951146dSAndy Grover 	} else if (ret == -EINVAL) {
1553703b2c5SNicholas Bellinger 		/*
1563703b2c5SNicholas Bellinger 		 * Handle case for SAM_STAT_RESERVATION_CONFLICT
1573703b2c5SNicholas Bellinger 		 */
1583703b2c5SNicholas Bellinger 		if (se_cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT)
1593703b2c5SNicholas Bellinger 			return PYX_TRANSPORT_RESERVATION_CONFLICT;
1603703b2c5SNicholas Bellinger 		/*
1613703b2c5SNicholas Bellinger 		 * Otherwise, return SAM_STAT_CHECK_CONDITION and return
1623703b2c5SNicholas Bellinger 		 * sense data.
1633703b2c5SNicholas Bellinger 		 */
1643703b2c5SNicholas Bellinger 		return PYX_TRANSPORT_USE_SENSE_REASON;
1653703b2c5SNicholas Bellinger 	}
1665951146dSAndy Grover 
1673703b2c5SNicholas Bellinger 	/*
1683703b2c5SNicholas Bellinger 	 * For BIDI commands, pass in the extra READ buffer
1693703b2c5SNicholas Bellinger 	 * to transport_generic_map_mem_to_cmd() below..
1703703b2c5SNicholas Bellinger 	 */
171a1d8b49aSAndy Grover 	if (se_cmd->t_tasks_bidi) {
1723703b2c5SNicholas Bellinger 		struct scsi_data_buffer *sdb = scsi_in(sc);
1733703b2c5SNicholas Bellinger 
1745951146dSAndy Grover 		sgl_bidi = sdb->table.sgl;
1755951146dSAndy Grover 		sgl_bidi_count = sdb->table.nents;
1763703b2c5SNicholas Bellinger 	}
1775951146dSAndy Grover 
178ec98f782SAndy Grover 	/* Tell the core about our preallocated memory */
1795951146dSAndy Grover 	ret = transport_generic_map_mem_to_cmd(se_cmd, scsi_sglist(sc),
1805951146dSAndy Grover 			scsi_sg_count(sc), sgl_bidi, sgl_bidi_count);
1813703b2c5SNicholas Bellinger 	if (ret < 0)
1823703b2c5SNicholas Bellinger 		return PYX_TRANSPORT_LU_COMM_FAILURE;
1833703b2c5SNicholas Bellinger 
1843703b2c5SNicholas Bellinger 	return 0;
1853703b2c5SNicholas Bellinger }
1863703b2c5SNicholas Bellinger 
1873703b2c5SNicholas Bellinger /*
1883703b2c5SNicholas Bellinger  * Called from struct target_core_fabric_ops->check_stop_free()
1893703b2c5SNicholas Bellinger  */
1903703b2c5SNicholas Bellinger static void tcm_loop_check_stop_free(struct se_cmd *se_cmd)
1913703b2c5SNicholas Bellinger {
1923703b2c5SNicholas Bellinger 	/*
1933703b2c5SNicholas Bellinger 	 * Do not release struct se_cmd's containing a valid TMR
1943703b2c5SNicholas Bellinger 	 * pointer.  These will be released directly in tcm_loop_device_reset()
1953703b2c5SNicholas Bellinger 	 * with transport_generic_free_cmd().
1963703b2c5SNicholas Bellinger 	 */
1973703b2c5SNicholas Bellinger 	if (se_cmd->se_tmr_req)
1983703b2c5SNicholas Bellinger 		return;
1993703b2c5SNicholas Bellinger 	/*
2003703b2c5SNicholas Bellinger 	 * Release the struct se_cmd, which will make a callback to release
2013703b2c5SNicholas Bellinger 	 * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd()
2023703b2c5SNicholas Bellinger 	 */
20382f1c8a4SChristoph Hellwig 	transport_generic_free_cmd(se_cmd, 0);
2043703b2c5SNicholas Bellinger }
2053703b2c5SNicholas Bellinger 
20635462975SChristoph Hellwig static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
2073703b2c5SNicholas Bellinger {
2083703b2c5SNicholas Bellinger 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
2093703b2c5SNicholas Bellinger 				struct tcm_loop_cmd, tl_se_cmd);
2103703b2c5SNicholas Bellinger 
2113703b2c5SNicholas Bellinger 	kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
2123703b2c5SNicholas Bellinger }
2133703b2c5SNicholas Bellinger 
2143703b2c5SNicholas Bellinger static int tcm_loop_proc_info(struct Scsi_Host *host, char *buffer,
2153703b2c5SNicholas Bellinger 				char **start, off_t offset,
2163703b2c5SNicholas Bellinger 				int length, int inout)
2173703b2c5SNicholas Bellinger {
2183703b2c5SNicholas Bellinger 	return sprintf(buffer, "tcm_loop_proc_info()\n");
2193703b2c5SNicholas Bellinger }
2203703b2c5SNicholas Bellinger 
2213703b2c5SNicholas Bellinger static int tcm_loop_driver_probe(struct device *);
2223703b2c5SNicholas Bellinger static int tcm_loop_driver_remove(struct device *);
2233703b2c5SNicholas Bellinger 
2243703b2c5SNicholas Bellinger static int pseudo_lld_bus_match(struct device *dev,
2253703b2c5SNicholas Bellinger 				struct device_driver *dev_driver)
2263703b2c5SNicholas Bellinger {
2273703b2c5SNicholas Bellinger 	return 1;
2283703b2c5SNicholas Bellinger }
2293703b2c5SNicholas Bellinger 
2303703b2c5SNicholas Bellinger static struct bus_type tcm_loop_lld_bus = {
2313703b2c5SNicholas Bellinger 	.name			= "tcm_loop_bus",
2323703b2c5SNicholas Bellinger 	.match			= pseudo_lld_bus_match,
2333703b2c5SNicholas Bellinger 	.probe			= tcm_loop_driver_probe,
2343703b2c5SNicholas Bellinger 	.remove			= tcm_loop_driver_remove,
2353703b2c5SNicholas Bellinger };
2363703b2c5SNicholas Bellinger 
2373703b2c5SNicholas Bellinger static struct device_driver tcm_loop_driverfs = {
2383703b2c5SNicholas Bellinger 	.name			= "tcm_loop",
2393703b2c5SNicholas Bellinger 	.bus			= &tcm_loop_lld_bus,
2403703b2c5SNicholas Bellinger };
2413703b2c5SNicholas Bellinger /*
2423703b2c5SNicholas Bellinger  * Used with root_device_register() in tcm_loop_alloc_core_bus() below
2433703b2c5SNicholas Bellinger  */
2443703b2c5SNicholas Bellinger struct device *tcm_loop_primary;
2453703b2c5SNicholas Bellinger 
2463703b2c5SNicholas Bellinger /*
2473703b2c5SNicholas Bellinger  * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and
2483703b2c5SNicholas Bellinger  * drivers/scsi/libiscsi.c:iscsi_change_queue_depth()
2493703b2c5SNicholas Bellinger  */
2503703b2c5SNicholas Bellinger static int tcm_loop_change_queue_depth(
2513703b2c5SNicholas Bellinger 	struct scsi_device *sdev,
2523703b2c5SNicholas Bellinger 	int depth,
2533703b2c5SNicholas Bellinger 	int reason)
2543703b2c5SNicholas Bellinger {
2553703b2c5SNicholas Bellinger 	switch (reason) {
2563703b2c5SNicholas Bellinger 	case SCSI_QDEPTH_DEFAULT:
2573703b2c5SNicholas Bellinger 		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
2583703b2c5SNicholas Bellinger 		break;
2593703b2c5SNicholas Bellinger 	case SCSI_QDEPTH_QFULL:
2603703b2c5SNicholas Bellinger 		scsi_track_queue_full(sdev, depth);
2613703b2c5SNicholas Bellinger 		break;
2623703b2c5SNicholas Bellinger 	case SCSI_QDEPTH_RAMP_UP:
2633703b2c5SNicholas Bellinger 		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
2643703b2c5SNicholas Bellinger 		break;
2653703b2c5SNicholas Bellinger 	default:
2663703b2c5SNicholas Bellinger 		return -EOPNOTSUPP;
2673703b2c5SNicholas Bellinger 	}
2683703b2c5SNicholas Bellinger 	return sdev->queue_depth;
2693703b2c5SNicholas Bellinger }
2703703b2c5SNicholas Bellinger 
2713703b2c5SNicholas Bellinger /*
2723703b2c5SNicholas Bellinger  * Main entry point from struct scsi_host_template for incoming SCSI CDB+Data
2733703b2c5SNicholas Bellinger  * from Linux/SCSI subsystem for SCSI low level device drivers (LLDs)
2743703b2c5SNicholas Bellinger  */
2753703b2c5SNicholas Bellinger static int tcm_loop_queuecommand(
2763703b2c5SNicholas Bellinger 	struct Scsi_Host *sh,
2773703b2c5SNicholas Bellinger 	struct scsi_cmnd *sc)
2783703b2c5SNicholas Bellinger {
2793703b2c5SNicholas Bellinger 	struct se_cmd *se_cmd;
2803703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg;
2813703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba;
2823703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg;
2833703b2c5SNicholas Bellinger 
2846708bb27SAndy Grover 	pr_debug("tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x"
2853703b2c5SNicholas Bellinger 		" scsi_buf_len: %u\n", sc->device->host->host_no,
2863703b2c5SNicholas Bellinger 		sc->device->id, sc->device->channel, sc->device->lun,
2873703b2c5SNicholas Bellinger 		sc->cmnd[0], scsi_bufflen(sc));
2883703b2c5SNicholas Bellinger 	/*
2893703b2c5SNicholas Bellinger 	 * Locate the tcm_loop_hba_t pointer
2903703b2c5SNicholas Bellinger 	 */
2913703b2c5SNicholas Bellinger 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
2923703b2c5SNicholas Bellinger 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
2930a020436SNicholas Bellinger 	/*
2940a020436SNicholas Bellinger 	 * Ensure that this tl_tpg reference from the incoming sc->device->id
2950a020436SNicholas Bellinger 	 * has already been configured via tcm_loop_make_naa_tpg().
2960a020436SNicholas Bellinger 	 */
2970a020436SNicholas Bellinger 	if (!tl_tpg->tl_hba) {
2980a020436SNicholas Bellinger 		set_host_byte(sc, DID_NO_CONNECT);
2990a020436SNicholas Bellinger 		sc->scsi_done(sc);
3000a020436SNicholas Bellinger 		return 0;
3010a020436SNicholas Bellinger 	}
3023703b2c5SNicholas Bellinger 	se_tpg = &tl_tpg->tl_se_tpg;
3033703b2c5SNicholas Bellinger 	/*
3043703b2c5SNicholas Bellinger 	 * Determine the SAM Task Attribute and allocate tl_cmd and
3053703b2c5SNicholas Bellinger 	 * tl_cmd->tl_se_cmd from TCM infrastructure
3063703b2c5SNicholas Bellinger 	 */
3073703b2c5SNicholas Bellinger 	se_cmd = tcm_loop_allocate_core_cmd(tl_hba, se_tpg, sc);
3083703b2c5SNicholas Bellinger 	if (!se_cmd) {
3093703b2c5SNicholas Bellinger 		sc->scsi_done(sc);
3103703b2c5SNicholas Bellinger 		return 0;
3113703b2c5SNicholas Bellinger 	}
3123703b2c5SNicholas Bellinger 	/*
3133703b2c5SNicholas Bellinger 	 * Queue up the newly allocated to be processed in TCM thread context.
3143703b2c5SNicholas Bellinger 	*/
3153703b2c5SNicholas Bellinger 	transport_generic_handle_cdb_map(se_cmd);
3163703b2c5SNicholas Bellinger 	return 0;
3173703b2c5SNicholas Bellinger }
3183703b2c5SNicholas Bellinger 
3193703b2c5SNicholas Bellinger /*
3203703b2c5SNicholas Bellinger  * Called from SCSI EH process context to issue a LUN_RESET TMR
3213703b2c5SNicholas Bellinger  * to struct scsi_device
3223703b2c5SNicholas Bellinger  */
3233703b2c5SNicholas Bellinger static int tcm_loop_device_reset(struct scsi_cmnd *sc)
3243703b2c5SNicholas Bellinger {
3253703b2c5SNicholas Bellinger 	struct se_cmd *se_cmd = NULL;
3263703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg;
3273703b2c5SNicholas Bellinger 	struct se_session *se_sess;
3283703b2c5SNicholas Bellinger 	struct tcm_loop_cmd *tl_cmd = NULL;
3293703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba;
3303703b2c5SNicholas Bellinger 	struct tcm_loop_nexus *tl_nexus;
3313703b2c5SNicholas Bellinger 	struct tcm_loop_tmr *tl_tmr = NULL;
3323703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg;
3333703b2c5SNicholas Bellinger 	int ret = FAILED;
3343703b2c5SNicholas Bellinger 	/*
3353703b2c5SNicholas Bellinger 	 * Locate the tcm_loop_hba_t pointer
3363703b2c5SNicholas Bellinger 	 */
3373703b2c5SNicholas Bellinger 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
3383703b2c5SNicholas Bellinger 	/*
3393703b2c5SNicholas Bellinger 	 * Locate the tl_nexus and se_sess pointers
3403703b2c5SNicholas Bellinger 	 */
3413703b2c5SNicholas Bellinger 	tl_nexus = tl_hba->tl_nexus;
3423703b2c5SNicholas Bellinger 	if (!tl_nexus) {
3436708bb27SAndy Grover 		pr_err("Unable to perform device reset without"
3443703b2c5SNicholas Bellinger 				" active I_T Nexus\n");
3453703b2c5SNicholas Bellinger 		return FAILED;
3463703b2c5SNicholas Bellinger 	}
3473703b2c5SNicholas Bellinger 	se_sess = tl_nexus->se_sess;
3483703b2c5SNicholas Bellinger 	/*
3493703b2c5SNicholas Bellinger 	 * Locate the tl_tpg and se_tpg pointers from TargetID in sc->device->id
3503703b2c5SNicholas Bellinger 	 */
3513703b2c5SNicholas Bellinger 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
3523703b2c5SNicholas Bellinger 	se_tpg = &tl_tpg->tl_se_tpg;
3533703b2c5SNicholas Bellinger 
3543703b2c5SNicholas Bellinger 	tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
3553703b2c5SNicholas Bellinger 	if (!tl_cmd) {
3566708bb27SAndy Grover 		pr_err("Unable to allocate memory for tl_cmd\n");
3573703b2c5SNicholas Bellinger 		return FAILED;
3583703b2c5SNicholas Bellinger 	}
3593703b2c5SNicholas Bellinger 
3603703b2c5SNicholas Bellinger 	tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
3613703b2c5SNicholas Bellinger 	if (!tl_tmr) {
3626708bb27SAndy Grover 		pr_err("Unable to allocate memory for tl_tmr\n");
3633703b2c5SNicholas Bellinger 		goto release;
3643703b2c5SNicholas Bellinger 	}
3653703b2c5SNicholas Bellinger 	init_waitqueue_head(&tl_tmr->tl_tmr_wait);
3663703b2c5SNicholas Bellinger 
3673703b2c5SNicholas Bellinger 	se_cmd = &tl_cmd->tl_se_cmd;
3683703b2c5SNicholas Bellinger 	/*
3693703b2c5SNicholas Bellinger 	 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
3703703b2c5SNicholas Bellinger 	 */
3713703b2c5SNicholas Bellinger 	transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
37261db1802SNicholas Bellinger 				DMA_NONE, MSG_SIMPLE_TAG,
3733703b2c5SNicholas Bellinger 				&tl_cmd->tl_sense_buf[0]);
3743703b2c5SNicholas Bellinger 	/*
3753703b2c5SNicholas Bellinger 	 * Allocate the LUN_RESET TMR
3763703b2c5SNicholas Bellinger 	 */
3775951146dSAndy Grover 	se_cmd->se_tmr_req = core_tmr_alloc_req(se_cmd, tl_tmr,
378dd503a5fSRoland Dreier 						TMR_LUN_RESET, GFP_KERNEL);
379552523dcSDan Carpenter 	if (IS_ERR(se_cmd->se_tmr_req))
3803703b2c5SNicholas Bellinger 		goto release;
3813703b2c5SNicholas Bellinger 	/*
3823703b2c5SNicholas Bellinger 	 * Locate the underlying TCM struct se_lun from sc->device->lun
3833703b2c5SNicholas Bellinger 	 */
3845951146dSAndy Grover 	if (transport_lookup_tmr_lun(se_cmd, sc->device->lun) < 0)
3853703b2c5SNicholas Bellinger 		goto release;
3863703b2c5SNicholas Bellinger 	/*
3873703b2c5SNicholas Bellinger 	 * Queue the TMR to TCM Core and sleep waiting for tcm_loop_queue_tm_rsp()
3883703b2c5SNicholas Bellinger 	 * to wake us up.
3893703b2c5SNicholas Bellinger 	 */
3903703b2c5SNicholas Bellinger 	transport_generic_handle_tmr(se_cmd);
3913703b2c5SNicholas Bellinger 	wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
3923703b2c5SNicholas Bellinger 	/*
3933703b2c5SNicholas Bellinger 	 * The TMR LUN_RESET has completed, check the response status and
3943703b2c5SNicholas Bellinger 	 * then release allocations.
3953703b2c5SNicholas Bellinger 	 */
3963703b2c5SNicholas Bellinger 	ret = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
3973703b2c5SNicholas Bellinger 		SUCCESS : FAILED;
3983703b2c5SNicholas Bellinger release:
3993703b2c5SNicholas Bellinger 	if (se_cmd)
40082f1c8a4SChristoph Hellwig 		transport_generic_free_cmd(se_cmd, 1);
4013703b2c5SNicholas Bellinger 	else
4023703b2c5SNicholas Bellinger 		kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
4033703b2c5SNicholas Bellinger 	kfree(tl_tmr);
4043703b2c5SNicholas Bellinger 	return ret;
4053703b2c5SNicholas Bellinger }
4063703b2c5SNicholas Bellinger 
4073703b2c5SNicholas Bellinger static int tcm_loop_slave_alloc(struct scsi_device *sd)
4083703b2c5SNicholas Bellinger {
4093703b2c5SNicholas Bellinger 	set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
4103703b2c5SNicholas Bellinger 	return 0;
4113703b2c5SNicholas Bellinger }
4123703b2c5SNicholas Bellinger 
4133703b2c5SNicholas Bellinger static int tcm_loop_slave_configure(struct scsi_device *sd)
4143703b2c5SNicholas Bellinger {
4153703b2c5SNicholas Bellinger 	return 0;
4163703b2c5SNicholas Bellinger }
4173703b2c5SNicholas Bellinger 
4183703b2c5SNicholas Bellinger static struct scsi_host_template tcm_loop_driver_template = {
4193703b2c5SNicholas Bellinger 	.proc_info		= tcm_loop_proc_info,
4203703b2c5SNicholas Bellinger 	.proc_name		= "tcm_loopback",
4213703b2c5SNicholas Bellinger 	.name			= "TCM_Loopback",
4223703b2c5SNicholas Bellinger 	.queuecommand		= tcm_loop_queuecommand,
4233703b2c5SNicholas Bellinger 	.change_queue_depth	= tcm_loop_change_queue_depth,
4243703b2c5SNicholas Bellinger 	.eh_device_reset_handler = tcm_loop_device_reset,
4253703b2c5SNicholas Bellinger 	.can_queue		= TL_SCSI_CAN_QUEUE,
4263703b2c5SNicholas Bellinger 	.this_id		= -1,
4273703b2c5SNicholas Bellinger 	.sg_tablesize		= TL_SCSI_SG_TABLESIZE,
4283703b2c5SNicholas Bellinger 	.cmd_per_lun		= TL_SCSI_CMD_PER_LUN,
4293703b2c5SNicholas Bellinger 	.max_sectors		= TL_SCSI_MAX_SECTORS,
4303703b2c5SNicholas Bellinger 	.use_clustering		= DISABLE_CLUSTERING,
4313703b2c5SNicholas Bellinger 	.slave_alloc		= tcm_loop_slave_alloc,
4323703b2c5SNicholas Bellinger 	.slave_configure	= tcm_loop_slave_configure,
4333703b2c5SNicholas Bellinger 	.module			= THIS_MODULE,
4343703b2c5SNicholas Bellinger };
4353703b2c5SNicholas Bellinger 
4363703b2c5SNicholas Bellinger static int tcm_loop_driver_probe(struct device *dev)
4373703b2c5SNicholas Bellinger {
4383703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba;
4393703b2c5SNicholas Bellinger 	struct Scsi_Host *sh;
4403703b2c5SNicholas Bellinger 	int error;
4413703b2c5SNicholas Bellinger 
4423703b2c5SNicholas Bellinger 	tl_hba = to_tcm_loop_hba(dev);
4433703b2c5SNicholas Bellinger 
4443703b2c5SNicholas Bellinger 	sh = scsi_host_alloc(&tcm_loop_driver_template,
4453703b2c5SNicholas Bellinger 			sizeof(struct tcm_loop_hba));
4463703b2c5SNicholas Bellinger 	if (!sh) {
4476708bb27SAndy Grover 		pr_err("Unable to allocate struct scsi_host\n");
4483703b2c5SNicholas Bellinger 		return -ENODEV;
4493703b2c5SNicholas Bellinger 	}
4503703b2c5SNicholas Bellinger 	tl_hba->sh = sh;
4513703b2c5SNicholas Bellinger 
4523703b2c5SNicholas Bellinger 	/*
4533703b2c5SNicholas Bellinger 	 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
4543703b2c5SNicholas Bellinger 	 */
4553703b2c5SNicholas Bellinger 	*((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
4563703b2c5SNicholas Bellinger 	/*
4573703b2c5SNicholas Bellinger 	 * Setup single ID, Channel and LUN for now..
4583703b2c5SNicholas Bellinger 	 */
4593703b2c5SNicholas Bellinger 	sh->max_id = 2;
4603703b2c5SNicholas Bellinger 	sh->max_lun = 0;
4613703b2c5SNicholas Bellinger 	sh->max_channel = 0;
4623703b2c5SNicholas Bellinger 	sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN;
4633703b2c5SNicholas Bellinger 
4643703b2c5SNicholas Bellinger 	error = scsi_add_host(sh, &tl_hba->dev);
4653703b2c5SNicholas Bellinger 	if (error) {
4666708bb27SAndy Grover 		pr_err("%s: scsi_add_host failed\n", __func__);
4673703b2c5SNicholas Bellinger 		scsi_host_put(sh);
4683703b2c5SNicholas Bellinger 		return -ENODEV;
4693703b2c5SNicholas Bellinger 	}
4703703b2c5SNicholas Bellinger 	return 0;
4713703b2c5SNicholas Bellinger }
4723703b2c5SNicholas Bellinger 
4733703b2c5SNicholas Bellinger static int tcm_loop_driver_remove(struct device *dev)
4743703b2c5SNicholas Bellinger {
4753703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba;
4763703b2c5SNicholas Bellinger 	struct Scsi_Host *sh;
4773703b2c5SNicholas Bellinger 
4783703b2c5SNicholas Bellinger 	tl_hba = to_tcm_loop_hba(dev);
4793703b2c5SNicholas Bellinger 	sh = tl_hba->sh;
4803703b2c5SNicholas Bellinger 
4813703b2c5SNicholas Bellinger 	scsi_remove_host(sh);
4823703b2c5SNicholas Bellinger 	scsi_host_put(sh);
4833703b2c5SNicholas Bellinger 	return 0;
4843703b2c5SNicholas Bellinger }
4853703b2c5SNicholas Bellinger 
4863703b2c5SNicholas Bellinger static void tcm_loop_release_adapter(struct device *dev)
4873703b2c5SNicholas Bellinger {
4883703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
4893703b2c5SNicholas Bellinger 
4903703b2c5SNicholas Bellinger 	kfree(tl_hba);
4913703b2c5SNicholas Bellinger }
4923703b2c5SNicholas Bellinger 
4933703b2c5SNicholas Bellinger /*
4943703b2c5SNicholas Bellinger  * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
4953703b2c5SNicholas Bellinger  */
4963703b2c5SNicholas Bellinger static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
4973703b2c5SNicholas Bellinger {
4983703b2c5SNicholas Bellinger 	int ret;
4993703b2c5SNicholas Bellinger 
5003703b2c5SNicholas Bellinger 	tl_hba->dev.bus = &tcm_loop_lld_bus;
5013703b2c5SNicholas Bellinger 	tl_hba->dev.parent = tcm_loop_primary;
5023703b2c5SNicholas Bellinger 	tl_hba->dev.release = &tcm_loop_release_adapter;
5033703b2c5SNicholas Bellinger 	dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
5043703b2c5SNicholas Bellinger 
5053703b2c5SNicholas Bellinger 	ret = device_register(&tl_hba->dev);
5063703b2c5SNicholas Bellinger 	if (ret) {
5076708bb27SAndy Grover 		pr_err("device_register() failed for"
5083703b2c5SNicholas Bellinger 				" tl_hba->dev: %d\n", ret);
5093703b2c5SNicholas Bellinger 		return -ENODEV;
5103703b2c5SNicholas Bellinger 	}
5113703b2c5SNicholas Bellinger 
5123703b2c5SNicholas Bellinger 	return 0;
5133703b2c5SNicholas Bellinger }
5143703b2c5SNicholas Bellinger 
5153703b2c5SNicholas Bellinger /*
5163703b2c5SNicholas Bellinger  * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
5173703b2c5SNicholas Bellinger  * tcm_loop SCSI bus.
5183703b2c5SNicholas Bellinger  */
5193703b2c5SNicholas Bellinger static int tcm_loop_alloc_core_bus(void)
5203703b2c5SNicholas Bellinger {
5213703b2c5SNicholas Bellinger 	int ret;
5223703b2c5SNicholas Bellinger 
5233703b2c5SNicholas Bellinger 	tcm_loop_primary = root_device_register("tcm_loop_0");
5243703b2c5SNicholas Bellinger 	if (IS_ERR(tcm_loop_primary)) {
5256708bb27SAndy Grover 		pr_err("Unable to allocate tcm_loop_primary\n");
5263703b2c5SNicholas Bellinger 		return PTR_ERR(tcm_loop_primary);
5273703b2c5SNicholas Bellinger 	}
5283703b2c5SNicholas Bellinger 
5293703b2c5SNicholas Bellinger 	ret = bus_register(&tcm_loop_lld_bus);
5303703b2c5SNicholas Bellinger 	if (ret) {
5316708bb27SAndy Grover 		pr_err("bus_register() failed for tcm_loop_lld_bus\n");
5323703b2c5SNicholas Bellinger 		goto dev_unreg;
5333703b2c5SNicholas Bellinger 	}
5343703b2c5SNicholas Bellinger 
5353703b2c5SNicholas Bellinger 	ret = driver_register(&tcm_loop_driverfs);
5363703b2c5SNicholas Bellinger 	if (ret) {
5376708bb27SAndy Grover 		pr_err("driver_register() failed for"
5383703b2c5SNicholas Bellinger 				"tcm_loop_driverfs\n");
5393703b2c5SNicholas Bellinger 		goto bus_unreg;
5403703b2c5SNicholas Bellinger 	}
5413703b2c5SNicholas Bellinger 
5426708bb27SAndy Grover 	pr_debug("Initialized TCM Loop Core Bus\n");
5433703b2c5SNicholas Bellinger 	return ret;
5443703b2c5SNicholas Bellinger 
5453703b2c5SNicholas Bellinger bus_unreg:
5463703b2c5SNicholas Bellinger 	bus_unregister(&tcm_loop_lld_bus);
5473703b2c5SNicholas Bellinger dev_unreg:
5483703b2c5SNicholas Bellinger 	root_device_unregister(tcm_loop_primary);
5493703b2c5SNicholas Bellinger 	return ret;
5503703b2c5SNicholas Bellinger }
5513703b2c5SNicholas Bellinger 
5523703b2c5SNicholas Bellinger static void tcm_loop_release_core_bus(void)
5533703b2c5SNicholas Bellinger {
5543703b2c5SNicholas Bellinger 	driver_unregister(&tcm_loop_driverfs);
5553703b2c5SNicholas Bellinger 	bus_unregister(&tcm_loop_lld_bus);
5563703b2c5SNicholas Bellinger 	root_device_unregister(tcm_loop_primary);
5573703b2c5SNicholas Bellinger 
5586708bb27SAndy Grover 	pr_debug("Releasing TCM Loop Core BUS\n");
5593703b2c5SNicholas Bellinger }
5603703b2c5SNicholas Bellinger 
5613703b2c5SNicholas Bellinger static char *tcm_loop_get_fabric_name(void)
5623703b2c5SNicholas Bellinger {
5633703b2c5SNicholas Bellinger 	return "loopback";
5643703b2c5SNicholas Bellinger }
5653703b2c5SNicholas Bellinger 
5663703b2c5SNicholas Bellinger static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
5673703b2c5SNicholas Bellinger {
5683703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg =
5693703b2c5SNicholas Bellinger 			(struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr;
5703703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
5713703b2c5SNicholas Bellinger 	/*
5723703b2c5SNicholas Bellinger 	 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
5733703b2c5SNicholas Bellinger 	 * time based on the protocol dependent prefix of the passed configfs group.
5743703b2c5SNicholas Bellinger 	 *
5753703b2c5SNicholas Bellinger 	 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
5763703b2c5SNicholas Bellinger 	 * ProtocolID using target_core_fabric_lib.c symbols.
5773703b2c5SNicholas Bellinger 	 */
5783703b2c5SNicholas Bellinger 	switch (tl_hba->tl_proto_id) {
5793703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_SAS:
5803703b2c5SNicholas Bellinger 		return sas_get_fabric_proto_ident(se_tpg);
5813703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_FCP:
5823703b2c5SNicholas Bellinger 		return fc_get_fabric_proto_ident(se_tpg);
5833703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_ISCSI:
5843703b2c5SNicholas Bellinger 		return iscsi_get_fabric_proto_ident(se_tpg);
5853703b2c5SNicholas Bellinger 	default:
5866708bb27SAndy Grover 		pr_err("Unknown tl_proto_id: 0x%02x, using"
5873703b2c5SNicholas Bellinger 			" SAS emulation\n", tl_hba->tl_proto_id);
5883703b2c5SNicholas Bellinger 		break;
5893703b2c5SNicholas Bellinger 	}
5903703b2c5SNicholas Bellinger 
5913703b2c5SNicholas Bellinger 	return sas_get_fabric_proto_ident(se_tpg);
5923703b2c5SNicholas Bellinger }
5933703b2c5SNicholas Bellinger 
5943703b2c5SNicholas Bellinger static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
5953703b2c5SNicholas Bellinger {
5963703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg =
5973703b2c5SNicholas Bellinger 		(struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr;
5983703b2c5SNicholas Bellinger 	/*
5993703b2c5SNicholas Bellinger 	 * Return the passed NAA identifier for the SAS Target Port
6003703b2c5SNicholas Bellinger 	 */
6013703b2c5SNicholas Bellinger 	return &tl_tpg->tl_hba->tl_wwn_address[0];
6023703b2c5SNicholas Bellinger }
6033703b2c5SNicholas Bellinger 
6043703b2c5SNicholas Bellinger static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
6053703b2c5SNicholas Bellinger {
6063703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg =
6073703b2c5SNicholas Bellinger 		(struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr;
6083703b2c5SNicholas Bellinger 	/*
6093703b2c5SNicholas Bellinger 	 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
6103703b2c5SNicholas Bellinger 	 * to represent the SCSI Target Port.
6113703b2c5SNicholas Bellinger 	 */
6123703b2c5SNicholas Bellinger 	return tl_tpg->tl_tpgt;
6133703b2c5SNicholas Bellinger }
6143703b2c5SNicholas Bellinger 
6153703b2c5SNicholas Bellinger static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
6163703b2c5SNicholas Bellinger {
6173703b2c5SNicholas Bellinger 	return 1;
6183703b2c5SNicholas Bellinger }
6193703b2c5SNicholas Bellinger 
6203703b2c5SNicholas Bellinger static u32 tcm_loop_get_pr_transport_id(
6213703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg,
6223703b2c5SNicholas Bellinger 	struct se_node_acl *se_nacl,
6233703b2c5SNicholas Bellinger 	struct t10_pr_registration *pr_reg,
6243703b2c5SNicholas Bellinger 	int *format_code,
6253703b2c5SNicholas Bellinger 	unsigned char *buf)
6263703b2c5SNicholas Bellinger {
6273703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg =
6283703b2c5SNicholas Bellinger 			(struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr;
6293703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
6303703b2c5SNicholas Bellinger 
6313703b2c5SNicholas Bellinger 	switch (tl_hba->tl_proto_id) {
6323703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_SAS:
6333703b2c5SNicholas Bellinger 		return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
6343703b2c5SNicholas Bellinger 					format_code, buf);
6353703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_FCP:
6363703b2c5SNicholas Bellinger 		return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
6373703b2c5SNicholas Bellinger 					format_code, buf);
6383703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_ISCSI:
6393703b2c5SNicholas Bellinger 		return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
6403703b2c5SNicholas Bellinger 					format_code, buf);
6413703b2c5SNicholas Bellinger 	default:
6426708bb27SAndy Grover 		pr_err("Unknown tl_proto_id: 0x%02x, using"
6433703b2c5SNicholas Bellinger 			" SAS emulation\n", tl_hba->tl_proto_id);
6443703b2c5SNicholas Bellinger 		break;
6453703b2c5SNicholas Bellinger 	}
6463703b2c5SNicholas Bellinger 
6473703b2c5SNicholas Bellinger 	return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
6483703b2c5SNicholas Bellinger 			format_code, buf);
6493703b2c5SNicholas Bellinger }
6503703b2c5SNicholas Bellinger 
6513703b2c5SNicholas Bellinger static u32 tcm_loop_get_pr_transport_id_len(
6523703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg,
6533703b2c5SNicholas Bellinger 	struct se_node_acl *se_nacl,
6543703b2c5SNicholas Bellinger 	struct t10_pr_registration *pr_reg,
6553703b2c5SNicholas Bellinger 	int *format_code)
6563703b2c5SNicholas Bellinger {
6573703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg =
6583703b2c5SNicholas Bellinger 			(struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr;
6593703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
6603703b2c5SNicholas Bellinger 
6613703b2c5SNicholas Bellinger 	switch (tl_hba->tl_proto_id) {
6623703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_SAS:
6633703b2c5SNicholas Bellinger 		return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
6643703b2c5SNicholas Bellinger 					format_code);
6653703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_FCP:
6663703b2c5SNicholas Bellinger 		return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
6673703b2c5SNicholas Bellinger 					format_code);
6683703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_ISCSI:
6693703b2c5SNicholas Bellinger 		return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
6703703b2c5SNicholas Bellinger 					format_code);
6713703b2c5SNicholas Bellinger 	default:
6726708bb27SAndy Grover 		pr_err("Unknown tl_proto_id: 0x%02x, using"
6733703b2c5SNicholas Bellinger 			" SAS emulation\n", tl_hba->tl_proto_id);
6743703b2c5SNicholas Bellinger 		break;
6753703b2c5SNicholas Bellinger 	}
6763703b2c5SNicholas Bellinger 
6773703b2c5SNicholas Bellinger 	return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
6783703b2c5SNicholas Bellinger 			format_code);
6793703b2c5SNicholas Bellinger }
6803703b2c5SNicholas Bellinger 
6813703b2c5SNicholas Bellinger /*
6823703b2c5SNicholas Bellinger  * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
6833703b2c5SNicholas Bellinger  * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
6843703b2c5SNicholas Bellinger  */
6853703b2c5SNicholas Bellinger static char *tcm_loop_parse_pr_out_transport_id(
6863703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg,
6873703b2c5SNicholas Bellinger 	const char *buf,
6883703b2c5SNicholas Bellinger 	u32 *out_tid_len,
6893703b2c5SNicholas Bellinger 	char **port_nexus_ptr)
6903703b2c5SNicholas Bellinger {
6913703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg =
6923703b2c5SNicholas Bellinger 			(struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr;
6933703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
6943703b2c5SNicholas Bellinger 
6953703b2c5SNicholas Bellinger 	switch (tl_hba->tl_proto_id) {
6963703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_SAS:
6973703b2c5SNicholas Bellinger 		return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
6983703b2c5SNicholas Bellinger 					port_nexus_ptr);
6993703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_FCP:
7003703b2c5SNicholas Bellinger 		return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
7013703b2c5SNicholas Bellinger 					port_nexus_ptr);
7023703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_ISCSI:
7033703b2c5SNicholas Bellinger 		return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
7043703b2c5SNicholas Bellinger 					port_nexus_ptr);
7053703b2c5SNicholas Bellinger 	default:
7066708bb27SAndy Grover 		pr_err("Unknown tl_proto_id: 0x%02x, using"
7073703b2c5SNicholas Bellinger 			" SAS emulation\n", tl_hba->tl_proto_id);
7083703b2c5SNicholas Bellinger 		break;
7093703b2c5SNicholas Bellinger 	}
7103703b2c5SNicholas Bellinger 
7113703b2c5SNicholas Bellinger 	return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
7123703b2c5SNicholas Bellinger 			port_nexus_ptr);
7133703b2c5SNicholas Bellinger }
7143703b2c5SNicholas Bellinger 
7153703b2c5SNicholas Bellinger /*
7163703b2c5SNicholas Bellinger  * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
7173703b2c5SNicholas Bellinger  * based upon the incoming fabric dependent SCSI Initiator Port
7183703b2c5SNicholas Bellinger  */
7193703b2c5SNicholas Bellinger static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
7203703b2c5SNicholas Bellinger {
7213703b2c5SNicholas Bellinger 	return 1;
7223703b2c5SNicholas Bellinger }
7233703b2c5SNicholas Bellinger 
7243703b2c5SNicholas Bellinger static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
7253703b2c5SNicholas Bellinger {
7263703b2c5SNicholas Bellinger 	return 0;
7273703b2c5SNicholas Bellinger }
7283703b2c5SNicholas Bellinger 
7293703b2c5SNicholas Bellinger /*
7303703b2c5SNicholas Bellinger  * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
7313703b2c5SNicholas Bellinger  * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
7323703b2c5SNicholas Bellinger  */
7333703b2c5SNicholas Bellinger static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
7343703b2c5SNicholas Bellinger {
7353703b2c5SNicholas Bellinger 	return 0;
7363703b2c5SNicholas Bellinger }
7373703b2c5SNicholas Bellinger 
7383703b2c5SNicholas Bellinger /*
7393703b2c5SNicholas Bellinger  * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
7403703b2c5SNicholas Bellinger  * never be called for TCM_Loop by target_core_fabric_configfs.c code.
7413703b2c5SNicholas Bellinger  * It has been added here as a nop for target_fabric_tf_ops_check()
7423703b2c5SNicholas Bellinger  */
7433703b2c5SNicholas Bellinger static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
7443703b2c5SNicholas Bellinger {
7453703b2c5SNicholas Bellinger 	return 0;
7463703b2c5SNicholas Bellinger }
7473703b2c5SNicholas Bellinger 
7483703b2c5SNicholas Bellinger static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
7493703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg)
7503703b2c5SNicholas Bellinger {
7513703b2c5SNicholas Bellinger 	struct tcm_loop_nacl *tl_nacl;
7523703b2c5SNicholas Bellinger 
7533703b2c5SNicholas Bellinger 	tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
7543703b2c5SNicholas Bellinger 	if (!tl_nacl) {
7556708bb27SAndy Grover 		pr_err("Unable to allocate struct tcm_loop_nacl\n");
7563703b2c5SNicholas Bellinger 		return NULL;
7573703b2c5SNicholas Bellinger 	}
7583703b2c5SNicholas Bellinger 
7593703b2c5SNicholas Bellinger 	return &tl_nacl->se_node_acl;
7603703b2c5SNicholas Bellinger }
7613703b2c5SNicholas Bellinger 
7623703b2c5SNicholas Bellinger static void tcm_loop_tpg_release_fabric_acl(
7633703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg,
7643703b2c5SNicholas Bellinger 	struct se_node_acl *se_nacl)
7653703b2c5SNicholas Bellinger {
7663703b2c5SNicholas Bellinger 	struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
7673703b2c5SNicholas Bellinger 				struct tcm_loop_nacl, se_node_acl);
7683703b2c5SNicholas Bellinger 
7693703b2c5SNicholas Bellinger 	kfree(tl_nacl);
7703703b2c5SNicholas Bellinger }
7713703b2c5SNicholas Bellinger 
7723703b2c5SNicholas Bellinger static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
7733703b2c5SNicholas Bellinger {
7743703b2c5SNicholas Bellinger 	return 1;
7753703b2c5SNicholas Bellinger }
7763703b2c5SNicholas Bellinger 
7773703b2c5SNicholas Bellinger static int tcm_loop_is_state_remove(struct se_cmd *se_cmd)
7783703b2c5SNicholas Bellinger {
7793703b2c5SNicholas Bellinger 	/*
7803703b2c5SNicholas Bellinger 	 * Assume struct scsi_cmnd is not in remove state..
7813703b2c5SNicholas Bellinger 	 */
7823703b2c5SNicholas Bellinger 	return 0;
7833703b2c5SNicholas Bellinger }
7843703b2c5SNicholas Bellinger 
7853703b2c5SNicholas Bellinger static int tcm_loop_sess_logged_in(struct se_session *se_sess)
7863703b2c5SNicholas Bellinger {
7873703b2c5SNicholas Bellinger 	/*
7883703b2c5SNicholas Bellinger 	 * Assume that TL Nexus is always active
7893703b2c5SNicholas Bellinger 	 */
7903703b2c5SNicholas Bellinger 	return 1;
7913703b2c5SNicholas Bellinger }
7923703b2c5SNicholas Bellinger 
7933703b2c5SNicholas Bellinger static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
7943703b2c5SNicholas Bellinger {
7953703b2c5SNicholas Bellinger 	return 1;
7963703b2c5SNicholas Bellinger }
7973703b2c5SNicholas Bellinger 
7983703b2c5SNicholas Bellinger static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
7993703b2c5SNicholas Bellinger {
8003703b2c5SNicholas Bellinger 	return;
8013703b2c5SNicholas Bellinger }
8023703b2c5SNicholas Bellinger 
8033703b2c5SNicholas Bellinger static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
8043703b2c5SNicholas Bellinger {
8053703b2c5SNicholas Bellinger 	return 1;
8063703b2c5SNicholas Bellinger }
8073703b2c5SNicholas Bellinger 
8083703b2c5SNicholas Bellinger static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
8093703b2c5SNicholas Bellinger {
8103703b2c5SNicholas Bellinger 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
8113703b2c5SNicholas Bellinger 			struct tcm_loop_cmd, tl_se_cmd);
8123703b2c5SNicholas Bellinger 
8133703b2c5SNicholas Bellinger 	return tl_cmd->sc_cmd_state;
8143703b2c5SNicholas Bellinger }
8153703b2c5SNicholas Bellinger 
8163703b2c5SNicholas Bellinger static int tcm_loop_shutdown_session(struct se_session *se_sess)
8173703b2c5SNicholas Bellinger {
8183703b2c5SNicholas Bellinger 	return 0;
8193703b2c5SNicholas Bellinger }
8203703b2c5SNicholas Bellinger 
8213703b2c5SNicholas Bellinger static void tcm_loop_close_session(struct se_session *se_sess)
8223703b2c5SNicholas Bellinger {
8233703b2c5SNicholas Bellinger 	return;
8243703b2c5SNicholas Bellinger };
8253703b2c5SNicholas Bellinger 
8263703b2c5SNicholas Bellinger static void tcm_loop_stop_session(
8273703b2c5SNicholas Bellinger 	struct se_session *se_sess,
8283703b2c5SNicholas Bellinger 	int sess_sleep,
8293703b2c5SNicholas Bellinger 	int conn_sleep)
8303703b2c5SNicholas Bellinger {
8313703b2c5SNicholas Bellinger 	return;
8323703b2c5SNicholas Bellinger }
8333703b2c5SNicholas Bellinger 
8343703b2c5SNicholas Bellinger static void tcm_loop_fall_back_to_erl0(struct se_session *se_sess)
8353703b2c5SNicholas Bellinger {
8363703b2c5SNicholas Bellinger 	return;
8373703b2c5SNicholas Bellinger }
8383703b2c5SNicholas Bellinger 
8393703b2c5SNicholas Bellinger static int tcm_loop_write_pending(struct se_cmd *se_cmd)
8403703b2c5SNicholas Bellinger {
8413703b2c5SNicholas Bellinger 	/*
8423703b2c5SNicholas Bellinger 	 * Since Linux/SCSI has already sent down a struct scsi_cmnd
8433703b2c5SNicholas Bellinger 	 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
8443703b2c5SNicholas Bellinger 	 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
8453703b2c5SNicholas Bellinger 	 * format with transport_generic_map_mem_to_cmd().
8463703b2c5SNicholas Bellinger 	 *
8473703b2c5SNicholas Bellinger 	 * We now tell TCM to add this WRITE CDB directly into the TCM storage
8483703b2c5SNicholas Bellinger 	 * object execution queue.
8493703b2c5SNicholas Bellinger 	 */
8503703b2c5SNicholas Bellinger 	transport_generic_process_write(se_cmd);
8513703b2c5SNicholas Bellinger 	return 0;
8523703b2c5SNicholas Bellinger }
8533703b2c5SNicholas Bellinger 
8543703b2c5SNicholas Bellinger static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
8553703b2c5SNicholas Bellinger {
8563703b2c5SNicholas Bellinger 	return 0;
8573703b2c5SNicholas Bellinger }
8583703b2c5SNicholas Bellinger 
8593703b2c5SNicholas Bellinger static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
8603703b2c5SNicholas Bellinger {
8613703b2c5SNicholas Bellinger 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
8623703b2c5SNicholas Bellinger 				struct tcm_loop_cmd, tl_se_cmd);
8633703b2c5SNicholas Bellinger 	struct scsi_cmnd *sc = tl_cmd->sc;
8643703b2c5SNicholas Bellinger 
8656708bb27SAndy Grover 	pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
8663703b2c5SNicholas Bellinger 		     " cdb: 0x%02x\n", sc, sc->cmnd[0]);
8673703b2c5SNicholas Bellinger 
8683703b2c5SNicholas Bellinger 	sc->result = SAM_STAT_GOOD;
8693703b2c5SNicholas Bellinger 	set_host_byte(sc, DID_OK);
8703703b2c5SNicholas Bellinger 	sc->scsi_done(sc);
8713703b2c5SNicholas Bellinger 	return 0;
8723703b2c5SNicholas Bellinger }
8733703b2c5SNicholas Bellinger 
8743703b2c5SNicholas Bellinger static int tcm_loop_queue_status(struct se_cmd *se_cmd)
8753703b2c5SNicholas Bellinger {
8763703b2c5SNicholas Bellinger 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
8773703b2c5SNicholas Bellinger 				struct tcm_loop_cmd, tl_se_cmd);
8783703b2c5SNicholas Bellinger 	struct scsi_cmnd *sc = tl_cmd->sc;
8793703b2c5SNicholas Bellinger 
8806708bb27SAndy Grover 	pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
8813703b2c5SNicholas Bellinger 			" cdb: 0x%02x\n", sc, sc->cmnd[0]);
8823703b2c5SNicholas Bellinger 
8833703b2c5SNicholas Bellinger 	if (se_cmd->sense_buffer &&
8843703b2c5SNicholas Bellinger 	   ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
8853703b2c5SNicholas Bellinger 	    (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
8863703b2c5SNicholas Bellinger 
8875951146dSAndy Grover 		memcpy(sc->sense_buffer, se_cmd->sense_buffer,
8883703b2c5SNicholas Bellinger 				SCSI_SENSE_BUFFERSIZE);
8893703b2c5SNicholas Bellinger 		sc->result = SAM_STAT_CHECK_CONDITION;
8903703b2c5SNicholas Bellinger 		set_driver_byte(sc, DRIVER_SENSE);
8913703b2c5SNicholas Bellinger 	} else
8923703b2c5SNicholas Bellinger 		sc->result = se_cmd->scsi_status;
8933703b2c5SNicholas Bellinger 
8943703b2c5SNicholas Bellinger 	set_host_byte(sc, DID_OK);
8953703b2c5SNicholas Bellinger 	sc->scsi_done(sc);
8963703b2c5SNicholas Bellinger 	return 0;
8973703b2c5SNicholas Bellinger }
8983703b2c5SNicholas Bellinger 
8993703b2c5SNicholas Bellinger static int tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
9003703b2c5SNicholas Bellinger {
9013703b2c5SNicholas Bellinger 	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
9023703b2c5SNicholas Bellinger 	struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
9033703b2c5SNicholas Bellinger 	/*
9043703b2c5SNicholas Bellinger 	 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
9053703b2c5SNicholas Bellinger 	 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
9063703b2c5SNicholas Bellinger 	 */
9073703b2c5SNicholas Bellinger 	atomic_set(&tl_tmr->tmr_complete, 1);
9083703b2c5SNicholas Bellinger 	wake_up(&tl_tmr->tl_tmr_wait);
9093703b2c5SNicholas Bellinger 	return 0;
9103703b2c5SNicholas Bellinger }
9113703b2c5SNicholas Bellinger 
9123703b2c5SNicholas Bellinger static u16 tcm_loop_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
9133703b2c5SNicholas Bellinger {
9143703b2c5SNicholas Bellinger 	return 0;
9153703b2c5SNicholas Bellinger }
9163703b2c5SNicholas Bellinger 
9173703b2c5SNicholas Bellinger static u16 tcm_loop_get_fabric_sense_len(void)
9183703b2c5SNicholas Bellinger {
9193703b2c5SNicholas Bellinger 	return 0;
9203703b2c5SNicholas Bellinger }
9213703b2c5SNicholas Bellinger 
9223703b2c5SNicholas Bellinger static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
9233703b2c5SNicholas Bellinger {
9243703b2c5SNicholas Bellinger 	switch (tl_hba->tl_proto_id) {
9253703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_SAS:
9263703b2c5SNicholas Bellinger 		return "SAS";
9273703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_FCP:
9283703b2c5SNicholas Bellinger 		return "FCP";
9293703b2c5SNicholas Bellinger 	case SCSI_PROTOCOL_ISCSI:
9303703b2c5SNicholas Bellinger 		return "iSCSI";
9313703b2c5SNicholas Bellinger 	default:
9323703b2c5SNicholas Bellinger 		break;
9333703b2c5SNicholas Bellinger 	}
9343703b2c5SNicholas Bellinger 
9353703b2c5SNicholas Bellinger 	return "Unknown";
9363703b2c5SNicholas Bellinger }
9373703b2c5SNicholas Bellinger 
9383703b2c5SNicholas Bellinger /* Start items for tcm_loop_port_cit */
9393703b2c5SNicholas Bellinger 
9403703b2c5SNicholas Bellinger static int tcm_loop_port_link(
9413703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg,
9423703b2c5SNicholas Bellinger 	struct se_lun *lun)
9433703b2c5SNicholas Bellinger {
9443703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
9453703b2c5SNicholas Bellinger 				struct tcm_loop_tpg, tl_se_tpg);
9463703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
9473703b2c5SNicholas Bellinger 
9483703b2c5SNicholas Bellinger 	atomic_inc(&tl_tpg->tl_tpg_port_count);
9493703b2c5SNicholas Bellinger 	smp_mb__after_atomic_inc();
9503703b2c5SNicholas Bellinger 	/*
9513703b2c5SNicholas Bellinger 	 * Add Linux/SCSI struct scsi_device by HCTL
9523703b2c5SNicholas Bellinger 	 */
9533703b2c5SNicholas Bellinger 	scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
9543703b2c5SNicholas Bellinger 
9556708bb27SAndy Grover 	pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
9563703b2c5SNicholas Bellinger 	return 0;
9573703b2c5SNicholas Bellinger }
9583703b2c5SNicholas Bellinger 
9593703b2c5SNicholas Bellinger static void tcm_loop_port_unlink(
9603703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg,
9613703b2c5SNicholas Bellinger 	struct se_lun *se_lun)
9623703b2c5SNicholas Bellinger {
9633703b2c5SNicholas Bellinger 	struct scsi_device *sd;
9643703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba;
9653703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg;
9663703b2c5SNicholas Bellinger 
9673703b2c5SNicholas Bellinger 	tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
9683703b2c5SNicholas Bellinger 	tl_hba = tl_tpg->tl_hba;
9693703b2c5SNicholas Bellinger 
9703703b2c5SNicholas Bellinger 	sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
9713703b2c5SNicholas Bellinger 				se_lun->unpacked_lun);
9723703b2c5SNicholas Bellinger 	if (!sd) {
9736708bb27SAndy Grover 		pr_err("Unable to locate struct scsi_device for %d:%d:"
9743703b2c5SNicholas Bellinger 			"%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
9753703b2c5SNicholas Bellinger 		return;
9763703b2c5SNicholas Bellinger 	}
9773703b2c5SNicholas Bellinger 	/*
9783703b2c5SNicholas Bellinger 	 * Remove Linux/SCSI struct scsi_device by HCTL
9793703b2c5SNicholas Bellinger 	 */
9803703b2c5SNicholas Bellinger 	scsi_remove_device(sd);
9813703b2c5SNicholas Bellinger 	scsi_device_put(sd);
9823703b2c5SNicholas Bellinger 
9833703b2c5SNicholas Bellinger 	atomic_dec(&tl_tpg->tl_tpg_port_count);
9843703b2c5SNicholas Bellinger 	smp_mb__after_atomic_dec();
9853703b2c5SNicholas Bellinger 
9866708bb27SAndy Grover 	pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
9873703b2c5SNicholas Bellinger }
9883703b2c5SNicholas Bellinger 
9893703b2c5SNicholas Bellinger /* End items for tcm_loop_port_cit */
9903703b2c5SNicholas Bellinger 
9913703b2c5SNicholas Bellinger /* Start items for tcm_loop_nexus_cit */
9923703b2c5SNicholas Bellinger 
9933703b2c5SNicholas Bellinger static int tcm_loop_make_nexus(
9943703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg,
9953703b2c5SNicholas Bellinger 	const char *name)
9963703b2c5SNicholas Bellinger {
9973703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg;
9983703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
9993703b2c5SNicholas Bellinger 	struct tcm_loop_nexus *tl_nexus;
1000552523dcSDan Carpenter 	int ret = -ENOMEM;
10013703b2c5SNicholas Bellinger 
10023703b2c5SNicholas Bellinger 	if (tl_tpg->tl_hba->tl_nexus) {
10036708bb27SAndy Grover 		pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n");
10043703b2c5SNicholas Bellinger 		return -EEXIST;
10053703b2c5SNicholas Bellinger 	}
10063703b2c5SNicholas Bellinger 	se_tpg = &tl_tpg->tl_se_tpg;
10073703b2c5SNicholas Bellinger 
10083703b2c5SNicholas Bellinger 	tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
10093703b2c5SNicholas Bellinger 	if (!tl_nexus) {
10106708bb27SAndy Grover 		pr_err("Unable to allocate struct tcm_loop_nexus\n");
10113703b2c5SNicholas Bellinger 		return -ENOMEM;
10123703b2c5SNicholas Bellinger 	}
10133703b2c5SNicholas Bellinger 	/*
10143703b2c5SNicholas Bellinger 	 * Initialize the struct se_session pointer
10153703b2c5SNicholas Bellinger 	 */
10163703b2c5SNicholas Bellinger 	tl_nexus->se_sess = transport_init_session();
1017552523dcSDan Carpenter 	if (IS_ERR(tl_nexus->se_sess)) {
1018552523dcSDan Carpenter 		ret = PTR_ERR(tl_nexus->se_sess);
10193703b2c5SNicholas Bellinger 		goto out;
1020552523dcSDan Carpenter 	}
10213703b2c5SNicholas Bellinger 	/*
10223703b2c5SNicholas Bellinger 	 * Since we are running in 'demo mode' this call with generate a
10233703b2c5SNicholas Bellinger 	 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
10243703b2c5SNicholas Bellinger 	 * Initiator port name of the passed configfs group 'name'.
10253703b2c5SNicholas Bellinger 	 */
10263703b2c5SNicholas Bellinger 	tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
10273703b2c5SNicholas Bellinger 				se_tpg, (unsigned char *)name);
10283703b2c5SNicholas Bellinger 	if (!tl_nexus->se_sess->se_node_acl) {
10293703b2c5SNicholas Bellinger 		transport_free_session(tl_nexus->se_sess);
10303703b2c5SNicholas Bellinger 		goto out;
10313703b2c5SNicholas Bellinger 	}
10323703b2c5SNicholas Bellinger 	/*
10333703b2c5SNicholas Bellinger 	 * Now, register the SAS I_T Nexus as active with the call to
10343703b2c5SNicholas Bellinger 	 * transport_register_session()
10353703b2c5SNicholas Bellinger 	 */
10363703b2c5SNicholas Bellinger 	__transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
10375951146dSAndy Grover 			tl_nexus->se_sess, tl_nexus);
10383703b2c5SNicholas Bellinger 	tl_tpg->tl_hba->tl_nexus = tl_nexus;
10396708bb27SAndy Grover 	pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
10403703b2c5SNicholas Bellinger 		" %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
10413703b2c5SNicholas Bellinger 		name);
10423703b2c5SNicholas Bellinger 	return 0;
10433703b2c5SNicholas Bellinger 
10443703b2c5SNicholas Bellinger out:
10453703b2c5SNicholas Bellinger 	kfree(tl_nexus);
1046552523dcSDan Carpenter 	return ret;
10473703b2c5SNicholas Bellinger }
10483703b2c5SNicholas Bellinger 
10493703b2c5SNicholas Bellinger static int tcm_loop_drop_nexus(
10503703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tpg)
10513703b2c5SNicholas Bellinger {
10523703b2c5SNicholas Bellinger 	struct se_session *se_sess;
10533703b2c5SNicholas Bellinger 	struct tcm_loop_nexus *tl_nexus;
10543703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba = tpg->tl_hba;
10553703b2c5SNicholas Bellinger 
10563703b2c5SNicholas Bellinger 	tl_nexus = tpg->tl_hba->tl_nexus;
10573703b2c5SNicholas Bellinger 	if (!tl_nexus)
10583703b2c5SNicholas Bellinger 		return -ENODEV;
10593703b2c5SNicholas Bellinger 
10603703b2c5SNicholas Bellinger 	se_sess = tl_nexus->se_sess;
10613703b2c5SNicholas Bellinger 	if (!se_sess)
10623703b2c5SNicholas Bellinger 		return -ENODEV;
10633703b2c5SNicholas Bellinger 
10643703b2c5SNicholas Bellinger 	if (atomic_read(&tpg->tl_tpg_port_count)) {
10656708bb27SAndy Grover 		pr_err("Unable to remove TCM_Loop I_T Nexus with"
10663703b2c5SNicholas Bellinger 			" active TPG port count: %d\n",
10673703b2c5SNicholas Bellinger 			atomic_read(&tpg->tl_tpg_port_count));
10683703b2c5SNicholas Bellinger 		return -EPERM;
10693703b2c5SNicholas Bellinger 	}
10703703b2c5SNicholas Bellinger 
10716708bb27SAndy Grover 	pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
10723703b2c5SNicholas Bellinger 		" %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
10733703b2c5SNicholas Bellinger 		tl_nexus->se_sess->se_node_acl->initiatorname);
10743703b2c5SNicholas Bellinger 	/*
10753703b2c5SNicholas Bellinger 	 * Release the SCSI I_T Nexus to the emulated SAS Target Port
10763703b2c5SNicholas Bellinger 	 */
10773703b2c5SNicholas Bellinger 	transport_deregister_session(tl_nexus->se_sess);
10783703b2c5SNicholas Bellinger 	tpg->tl_hba->tl_nexus = NULL;
10793703b2c5SNicholas Bellinger 	kfree(tl_nexus);
10803703b2c5SNicholas Bellinger 	return 0;
10813703b2c5SNicholas Bellinger }
10823703b2c5SNicholas Bellinger 
10833703b2c5SNicholas Bellinger /* End items for tcm_loop_nexus_cit */
10843703b2c5SNicholas Bellinger 
10853703b2c5SNicholas Bellinger static ssize_t tcm_loop_tpg_show_nexus(
10863703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg,
10873703b2c5SNicholas Bellinger 	char *page)
10883703b2c5SNicholas Bellinger {
10893703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
10903703b2c5SNicholas Bellinger 			struct tcm_loop_tpg, tl_se_tpg);
10913703b2c5SNicholas Bellinger 	struct tcm_loop_nexus *tl_nexus;
10923703b2c5SNicholas Bellinger 	ssize_t ret;
10933703b2c5SNicholas Bellinger 
10943703b2c5SNicholas Bellinger 	tl_nexus = tl_tpg->tl_hba->tl_nexus;
10953703b2c5SNicholas Bellinger 	if (!tl_nexus)
10963703b2c5SNicholas Bellinger 		return -ENODEV;
10973703b2c5SNicholas Bellinger 
10983703b2c5SNicholas Bellinger 	ret = snprintf(page, PAGE_SIZE, "%s\n",
10993703b2c5SNicholas Bellinger 		tl_nexus->se_sess->se_node_acl->initiatorname);
11003703b2c5SNicholas Bellinger 
11013703b2c5SNicholas Bellinger 	return ret;
11023703b2c5SNicholas Bellinger }
11033703b2c5SNicholas Bellinger 
11043703b2c5SNicholas Bellinger static ssize_t tcm_loop_tpg_store_nexus(
11053703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg,
11063703b2c5SNicholas Bellinger 	const char *page,
11073703b2c5SNicholas Bellinger 	size_t count)
11083703b2c5SNicholas Bellinger {
11093703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
11103703b2c5SNicholas Bellinger 			struct tcm_loop_tpg, tl_se_tpg);
11113703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
11123703b2c5SNicholas Bellinger 	unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
11133703b2c5SNicholas Bellinger 	int ret;
11143703b2c5SNicholas Bellinger 	/*
11153703b2c5SNicholas Bellinger 	 * Shutdown the active I_T nexus if 'NULL' is passed..
11163703b2c5SNicholas Bellinger 	 */
11173703b2c5SNicholas Bellinger 	if (!strncmp(page, "NULL", 4)) {
11183703b2c5SNicholas Bellinger 		ret = tcm_loop_drop_nexus(tl_tpg);
11193703b2c5SNicholas Bellinger 		return (!ret) ? count : ret;
11203703b2c5SNicholas Bellinger 	}
11213703b2c5SNicholas Bellinger 	/*
11223703b2c5SNicholas Bellinger 	 * Otherwise make sure the passed virtual Initiator port WWN matches
11233703b2c5SNicholas Bellinger 	 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
11243703b2c5SNicholas Bellinger 	 * tcm_loop_make_nexus()
11253703b2c5SNicholas Bellinger 	 */
112660d645a4SDan Carpenter 	if (strlen(page) >= TL_WWN_ADDR_LEN) {
11276708bb27SAndy Grover 		pr_err("Emulated NAA Sas Address: %s, exceeds"
11283703b2c5SNicholas Bellinger 				" max: %d\n", page, TL_WWN_ADDR_LEN);
11293703b2c5SNicholas Bellinger 		return -EINVAL;
11303703b2c5SNicholas Bellinger 	}
11313703b2c5SNicholas Bellinger 	snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
11323703b2c5SNicholas Bellinger 
11333703b2c5SNicholas Bellinger 	ptr = strstr(i_port, "naa.");
11343703b2c5SNicholas Bellinger 	if (ptr) {
11353703b2c5SNicholas Bellinger 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
11366708bb27SAndy Grover 			pr_err("Passed SAS Initiator Port %s does not"
11373703b2c5SNicholas Bellinger 				" match target port protoid: %s\n", i_port,
11383703b2c5SNicholas Bellinger 				tcm_loop_dump_proto_id(tl_hba));
11393703b2c5SNicholas Bellinger 			return -EINVAL;
11403703b2c5SNicholas Bellinger 		}
11413703b2c5SNicholas Bellinger 		port_ptr = &i_port[0];
11423703b2c5SNicholas Bellinger 		goto check_newline;
11433703b2c5SNicholas Bellinger 	}
11443703b2c5SNicholas Bellinger 	ptr = strstr(i_port, "fc.");
11453703b2c5SNicholas Bellinger 	if (ptr) {
11463703b2c5SNicholas Bellinger 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
11476708bb27SAndy Grover 			pr_err("Passed FCP Initiator Port %s does not"
11483703b2c5SNicholas Bellinger 				" match target port protoid: %s\n", i_port,
11493703b2c5SNicholas Bellinger 				tcm_loop_dump_proto_id(tl_hba));
11503703b2c5SNicholas Bellinger 			return -EINVAL;
11513703b2c5SNicholas Bellinger 		}
11523703b2c5SNicholas Bellinger 		port_ptr = &i_port[3]; /* Skip over "fc." */
11533703b2c5SNicholas Bellinger 		goto check_newline;
11543703b2c5SNicholas Bellinger 	}
11553703b2c5SNicholas Bellinger 	ptr = strstr(i_port, "iqn.");
11563703b2c5SNicholas Bellinger 	if (ptr) {
11573703b2c5SNicholas Bellinger 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
11586708bb27SAndy Grover 			pr_err("Passed iSCSI Initiator Port %s does not"
11593703b2c5SNicholas Bellinger 				" match target port protoid: %s\n", i_port,
11603703b2c5SNicholas Bellinger 				tcm_loop_dump_proto_id(tl_hba));
11613703b2c5SNicholas Bellinger 			return -EINVAL;
11623703b2c5SNicholas Bellinger 		}
11633703b2c5SNicholas Bellinger 		port_ptr = &i_port[0];
11643703b2c5SNicholas Bellinger 		goto check_newline;
11653703b2c5SNicholas Bellinger 	}
11666708bb27SAndy Grover 	pr_err("Unable to locate prefix for emulated Initiator Port:"
11673703b2c5SNicholas Bellinger 			" %s\n", i_port);
11683703b2c5SNicholas Bellinger 	return -EINVAL;
11693703b2c5SNicholas Bellinger 	/*
11703703b2c5SNicholas Bellinger 	 * Clear any trailing newline for the NAA WWN
11713703b2c5SNicholas Bellinger 	 */
11723703b2c5SNicholas Bellinger check_newline:
11733703b2c5SNicholas Bellinger 	if (i_port[strlen(i_port)-1] == '\n')
11743703b2c5SNicholas Bellinger 		i_port[strlen(i_port)-1] = '\0';
11753703b2c5SNicholas Bellinger 
11763703b2c5SNicholas Bellinger 	ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
11773703b2c5SNicholas Bellinger 	if (ret < 0)
11783703b2c5SNicholas Bellinger 		return ret;
11793703b2c5SNicholas Bellinger 
11803703b2c5SNicholas Bellinger 	return count;
11813703b2c5SNicholas Bellinger }
11823703b2c5SNicholas Bellinger 
11833703b2c5SNicholas Bellinger TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
11843703b2c5SNicholas Bellinger 
11853703b2c5SNicholas Bellinger static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
11863703b2c5SNicholas Bellinger 	&tcm_loop_tpg_nexus.attr,
11873703b2c5SNicholas Bellinger 	NULL,
11883703b2c5SNicholas Bellinger };
11893703b2c5SNicholas Bellinger 
11903703b2c5SNicholas Bellinger /* Start items for tcm_loop_naa_cit */
11913703b2c5SNicholas Bellinger 
11923703b2c5SNicholas Bellinger struct se_portal_group *tcm_loop_make_naa_tpg(
11933703b2c5SNicholas Bellinger 	struct se_wwn *wwn,
11943703b2c5SNicholas Bellinger 	struct config_group *group,
11953703b2c5SNicholas Bellinger 	const char *name)
11963703b2c5SNicholas Bellinger {
11973703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba = container_of(wwn,
11983703b2c5SNicholas Bellinger 			struct tcm_loop_hba, tl_hba_wwn);
11993703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg;
12003703b2c5SNicholas Bellinger 	char *tpgt_str, *end_ptr;
12013703b2c5SNicholas Bellinger 	int ret;
12023703b2c5SNicholas Bellinger 	unsigned short int tpgt;
12033703b2c5SNicholas Bellinger 
12043703b2c5SNicholas Bellinger 	tpgt_str = strstr(name, "tpgt_");
12053703b2c5SNicholas Bellinger 	if (!tpgt_str) {
12066708bb27SAndy Grover 		pr_err("Unable to locate \"tpgt_#\" directory"
12073703b2c5SNicholas Bellinger 				" group\n");
12083703b2c5SNicholas Bellinger 		return ERR_PTR(-EINVAL);
12093703b2c5SNicholas Bellinger 	}
12103703b2c5SNicholas Bellinger 	tpgt_str += 5; /* Skip ahead of "tpgt_" */
12113703b2c5SNicholas Bellinger 	tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
12123703b2c5SNicholas Bellinger 
121312f09ccbSDan Carpenter 	if (tpgt >= TL_TPGS_PER_HBA) {
12146708bb27SAndy Grover 		pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
12153703b2c5SNicholas Bellinger 				" %u\n", tpgt, TL_TPGS_PER_HBA);
12163703b2c5SNicholas Bellinger 		return ERR_PTR(-EINVAL);
12173703b2c5SNicholas Bellinger 	}
12183703b2c5SNicholas Bellinger 	tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
12193703b2c5SNicholas Bellinger 	tl_tpg->tl_hba = tl_hba;
12203703b2c5SNicholas Bellinger 	tl_tpg->tl_tpgt = tpgt;
12213703b2c5SNicholas Bellinger 	/*
12223703b2c5SNicholas Bellinger 	 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
12233703b2c5SNicholas Bellinger 	 */
12243703b2c5SNicholas Bellinger 	ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
12255951146dSAndy Grover 			wwn, &tl_tpg->tl_se_tpg, tl_tpg,
12263703b2c5SNicholas Bellinger 			TRANSPORT_TPG_TYPE_NORMAL);
12273703b2c5SNicholas Bellinger 	if (ret < 0)
12283703b2c5SNicholas Bellinger 		return ERR_PTR(-ENOMEM);
12293703b2c5SNicholas Bellinger 
12306708bb27SAndy Grover 	pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
12313703b2c5SNicholas Bellinger 		" Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
12323703b2c5SNicholas Bellinger 		config_item_name(&wwn->wwn_group.cg_item), tpgt);
12333703b2c5SNicholas Bellinger 
12343703b2c5SNicholas Bellinger 	return &tl_tpg->tl_se_tpg;
12353703b2c5SNicholas Bellinger }
12363703b2c5SNicholas Bellinger 
12373703b2c5SNicholas Bellinger void tcm_loop_drop_naa_tpg(
12383703b2c5SNicholas Bellinger 	struct se_portal_group *se_tpg)
12393703b2c5SNicholas Bellinger {
12403703b2c5SNicholas Bellinger 	struct se_wwn *wwn = se_tpg->se_tpg_wwn;
12413703b2c5SNicholas Bellinger 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
12423703b2c5SNicholas Bellinger 				struct tcm_loop_tpg, tl_se_tpg);
12433703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba;
12443703b2c5SNicholas Bellinger 	unsigned short tpgt;
12453703b2c5SNicholas Bellinger 
12463703b2c5SNicholas Bellinger 	tl_hba = tl_tpg->tl_hba;
12473703b2c5SNicholas Bellinger 	tpgt = tl_tpg->tl_tpgt;
12483703b2c5SNicholas Bellinger 	/*
12493703b2c5SNicholas Bellinger 	 * Release the I_T Nexus for the Virtual SAS link if present
12503703b2c5SNicholas Bellinger 	 */
12513703b2c5SNicholas Bellinger 	tcm_loop_drop_nexus(tl_tpg);
12523703b2c5SNicholas Bellinger 	/*
12533703b2c5SNicholas Bellinger 	 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
12543703b2c5SNicholas Bellinger 	 */
12553703b2c5SNicholas Bellinger 	core_tpg_deregister(se_tpg);
12563703b2c5SNicholas Bellinger 
12570a020436SNicholas Bellinger 	tl_tpg->tl_hba = NULL;
12580a020436SNicholas Bellinger 	tl_tpg->tl_tpgt = 0;
12590a020436SNicholas Bellinger 
12606708bb27SAndy Grover 	pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
12613703b2c5SNicholas Bellinger 		" Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
12623703b2c5SNicholas Bellinger 		config_item_name(&wwn->wwn_group.cg_item), tpgt);
12633703b2c5SNicholas Bellinger }
12643703b2c5SNicholas Bellinger 
12653703b2c5SNicholas Bellinger /* End items for tcm_loop_naa_cit */
12663703b2c5SNicholas Bellinger 
12673703b2c5SNicholas Bellinger /* Start items for tcm_loop_cit */
12683703b2c5SNicholas Bellinger 
12693703b2c5SNicholas Bellinger struct se_wwn *tcm_loop_make_scsi_hba(
12703703b2c5SNicholas Bellinger 	struct target_fabric_configfs *tf,
12713703b2c5SNicholas Bellinger 	struct config_group *group,
12723703b2c5SNicholas Bellinger 	const char *name)
12733703b2c5SNicholas Bellinger {
12743703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba;
12753703b2c5SNicholas Bellinger 	struct Scsi_Host *sh;
12763703b2c5SNicholas Bellinger 	char *ptr;
12773703b2c5SNicholas Bellinger 	int ret, off = 0;
12783703b2c5SNicholas Bellinger 
12793703b2c5SNicholas Bellinger 	tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
12803703b2c5SNicholas Bellinger 	if (!tl_hba) {
12816708bb27SAndy Grover 		pr_err("Unable to allocate struct tcm_loop_hba\n");
12823703b2c5SNicholas Bellinger 		return ERR_PTR(-ENOMEM);
12833703b2c5SNicholas Bellinger 	}
12843703b2c5SNicholas Bellinger 	/*
12853703b2c5SNicholas Bellinger 	 * Determine the emulated Protocol Identifier and Target Port Name
12863703b2c5SNicholas Bellinger 	 * based on the incoming configfs directory name.
12873703b2c5SNicholas Bellinger 	 */
12883703b2c5SNicholas Bellinger 	ptr = strstr(name, "naa.");
12893703b2c5SNicholas Bellinger 	if (ptr) {
12903703b2c5SNicholas Bellinger 		tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
12913703b2c5SNicholas Bellinger 		goto check_len;
12923703b2c5SNicholas Bellinger 	}
12933703b2c5SNicholas Bellinger 	ptr = strstr(name, "fc.");
12943703b2c5SNicholas Bellinger 	if (ptr) {
12953703b2c5SNicholas Bellinger 		tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
12963703b2c5SNicholas Bellinger 		off = 3; /* Skip over "fc." */
12973703b2c5SNicholas Bellinger 		goto check_len;
12983703b2c5SNicholas Bellinger 	}
12993703b2c5SNicholas Bellinger 	ptr = strstr(name, "iqn.");
1300a57b5d36SJesper Juhl 	if (!ptr) {
13016708bb27SAndy Grover 		pr_err("Unable to locate prefix for emulated Target "
1302a57b5d36SJesper Juhl 				"Port: %s\n", name);
1303a57b5d36SJesper Juhl 		ret = -EINVAL;
1304a57b5d36SJesper Juhl 		goto out;
13053703b2c5SNicholas Bellinger 	}
1306a57b5d36SJesper Juhl 	tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
13073703b2c5SNicholas Bellinger 
13083703b2c5SNicholas Bellinger check_len:
130960d645a4SDan Carpenter 	if (strlen(name) >= TL_WWN_ADDR_LEN) {
13106708bb27SAndy Grover 		pr_err("Emulated NAA %s Address: %s, exceeds"
13113703b2c5SNicholas Bellinger 			" max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
13123703b2c5SNicholas Bellinger 			TL_WWN_ADDR_LEN);
1313a57b5d36SJesper Juhl 		ret = -EINVAL;
1314a57b5d36SJesper Juhl 		goto out;
13153703b2c5SNicholas Bellinger 	}
13163703b2c5SNicholas Bellinger 	snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
13173703b2c5SNicholas Bellinger 
13183703b2c5SNicholas Bellinger 	/*
13193703b2c5SNicholas Bellinger 	 * Call device_register(tl_hba->dev) to register the emulated
13203703b2c5SNicholas Bellinger 	 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
13213703b2c5SNicholas Bellinger 	 * device_register() callbacks in tcm_loop_driver_probe()
13223703b2c5SNicholas Bellinger 	 */
13233703b2c5SNicholas Bellinger 	ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
13243703b2c5SNicholas Bellinger 	if (ret)
13253703b2c5SNicholas Bellinger 		goto out;
13263703b2c5SNicholas Bellinger 
13273703b2c5SNicholas Bellinger 	sh = tl_hba->sh;
13283703b2c5SNicholas Bellinger 	tcm_loop_hba_no_cnt++;
13296708bb27SAndy Grover 	pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
13303703b2c5SNicholas Bellinger 		" %s Address: %s at Linux/SCSI Host ID: %d\n",
13313703b2c5SNicholas Bellinger 		tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
13323703b2c5SNicholas Bellinger 
13333703b2c5SNicholas Bellinger 	return &tl_hba->tl_hba_wwn;
13343703b2c5SNicholas Bellinger out:
13353703b2c5SNicholas Bellinger 	kfree(tl_hba);
13363703b2c5SNicholas Bellinger 	return ERR_PTR(ret);
13373703b2c5SNicholas Bellinger }
13383703b2c5SNicholas Bellinger 
13393703b2c5SNicholas Bellinger void tcm_loop_drop_scsi_hba(
13403703b2c5SNicholas Bellinger 	struct se_wwn *wwn)
13413703b2c5SNicholas Bellinger {
13423703b2c5SNicholas Bellinger 	struct tcm_loop_hba *tl_hba = container_of(wwn,
13433703b2c5SNicholas Bellinger 				struct tcm_loop_hba, tl_hba_wwn);
13443703b2c5SNicholas Bellinger 	int host_no = tl_hba->sh->host_no;
13453703b2c5SNicholas Bellinger 	/*
13463703b2c5SNicholas Bellinger 	 * Call device_unregister() on the original tl_hba->dev.
13473703b2c5SNicholas Bellinger 	 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
13483703b2c5SNicholas Bellinger 	 * release *tl_hba;
13493703b2c5SNicholas Bellinger 	 */
13503703b2c5SNicholas Bellinger 	device_unregister(&tl_hba->dev);
13513703b2c5SNicholas Bellinger 
13526708bb27SAndy Grover 	pr_debug("TCM_Loop_ConfigFS: Deallocated emulated Target"
13533703b2c5SNicholas Bellinger 		" SAS Address: %s at Linux/SCSI Host ID: %d\n",
13543703b2c5SNicholas Bellinger 		config_item_name(&wwn->wwn_group.cg_item), host_no);
13553703b2c5SNicholas Bellinger }
13563703b2c5SNicholas Bellinger 
13573703b2c5SNicholas Bellinger /* Start items for tcm_loop_cit */
13583703b2c5SNicholas Bellinger static ssize_t tcm_loop_wwn_show_attr_version(
13593703b2c5SNicholas Bellinger 	struct target_fabric_configfs *tf,
13603703b2c5SNicholas Bellinger 	char *page)
13613703b2c5SNicholas Bellinger {
13623703b2c5SNicholas Bellinger 	return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
13633703b2c5SNicholas Bellinger }
13643703b2c5SNicholas Bellinger 
13653703b2c5SNicholas Bellinger TF_WWN_ATTR_RO(tcm_loop, version);
13663703b2c5SNicholas Bellinger 
13673703b2c5SNicholas Bellinger static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
13683703b2c5SNicholas Bellinger 	&tcm_loop_wwn_version.attr,
13693703b2c5SNicholas Bellinger 	NULL,
13703703b2c5SNicholas Bellinger };
13713703b2c5SNicholas Bellinger 
13723703b2c5SNicholas Bellinger /* End items for tcm_loop_cit */
13733703b2c5SNicholas Bellinger 
13743703b2c5SNicholas Bellinger static int tcm_loop_register_configfs(void)
13753703b2c5SNicholas Bellinger {
13763703b2c5SNicholas Bellinger 	struct target_fabric_configfs *fabric;
13773703b2c5SNicholas Bellinger 	struct config_group *tf_cg;
13783703b2c5SNicholas Bellinger 	int ret;
13793703b2c5SNicholas Bellinger 	/*
13803703b2c5SNicholas Bellinger 	 * Set the TCM Loop HBA counter to zero
13813703b2c5SNicholas Bellinger 	 */
13823703b2c5SNicholas Bellinger 	tcm_loop_hba_no_cnt = 0;
13833703b2c5SNicholas Bellinger 	/*
13843703b2c5SNicholas Bellinger 	 * Register the top level struct config_item_type with TCM core
13853703b2c5SNicholas Bellinger 	 */
13863703b2c5SNicholas Bellinger 	fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
1387e3d6f909SAndy Grover 	if (IS_ERR(fabric)) {
13886708bb27SAndy Grover 		pr_err("tcm_loop_register_configfs() failed!\n");
1389e3d6f909SAndy Grover 		return PTR_ERR(fabric);
13903703b2c5SNicholas Bellinger 	}
13913703b2c5SNicholas Bellinger 	/*
13923703b2c5SNicholas Bellinger 	 * Setup the fabric API of function pointers used by target_core_mod
13933703b2c5SNicholas Bellinger 	 */
13943703b2c5SNicholas Bellinger 	fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
13953703b2c5SNicholas Bellinger 	fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
13963703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
13973703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
13983703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
13993703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
14003703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_get_pr_transport_id_len =
14013703b2c5SNicholas Bellinger 					&tcm_loop_get_pr_transport_id_len;
14023703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_parse_pr_out_transport_id =
14033703b2c5SNicholas Bellinger 					&tcm_loop_parse_pr_out_transport_id;
14043703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
14053703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_check_demo_mode_cache =
14063703b2c5SNicholas Bellinger 					&tcm_loop_check_demo_mode_cache;
14073703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_check_demo_mode_write_protect =
14083703b2c5SNicholas Bellinger 					&tcm_loop_check_demo_mode_write_protect;
14093703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_check_prod_mode_write_protect =
14103703b2c5SNicholas Bellinger 					&tcm_loop_check_prod_mode_write_protect;
14113703b2c5SNicholas Bellinger 	/*
14123703b2c5SNicholas Bellinger 	 * The TCM loopback fabric module runs in demo-mode to a local
14133703b2c5SNicholas Bellinger 	 * virtual SCSI device, so fabric dependent initator ACLs are
14143703b2c5SNicholas Bellinger 	 * not required.
14153703b2c5SNicholas Bellinger 	 */
14163703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
14173703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_release_fabric_acl =
14183703b2c5SNicholas Bellinger 					&tcm_loop_tpg_release_fabric_acl;
14193703b2c5SNicholas Bellinger 	fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
14203703b2c5SNicholas Bellinger 	/*
14213703b2c5SNicholas Bellinger 	 * Used for setting up remaining TCM resources in process context
14223703b2c5SNicholas Bellinger 	 */
14233703b2c5SNicholas Bellinger 	fabric->tf_ops.new_cmd_map = &tcm_loop_new_cmd_map;
14243703b2c5SNicholas Bellinger 	fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
142535462975SChristoph Hellwig 	fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
14263703b2c5SNicholas Bellinger 	fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
14273703b2c5SNicholas Bellinger 	fabric->tf_ops.close_session = &tcm_loop_close_session;
14283703b2c5SNicholas Bellinger 	fabric->tf_ops.stop_session = &tcm_loop_stop_session;
14293703b2c5SNicholas Bellinger 	fabric->tf_ops.fall_back_to_erl0 = &tcm_loop_fall_back_to_erl0;
14303703b2c5SNicholas Bellinger 	fabric->tf_ops.sess_logged_in = &tcm_loop_sess_logged_in;
14313703b2c5SNicholas Bellinger 	fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
14323703b2c5SNicholas Bellinger 	fabric->tf_ops.sess_get_initiator_sid = NULL;
14333703b2c5SNicholas Bellinger 	fabric->tf_ops.write_pending = &tcm_loop_write_pending;
14343703b2c5SNicholas Bellinger 	fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
14353703b2c5SNicholas Bellinger 	/*
14363703b2c5SNicholas Bellinger 	 * Not used for TCM loopback
14373703b2c5SNicholas Bellinger 	 */
14383703b2c5SNicholas Bellinger 	fabric->tf_ops.set_default_node_attributes =
14393703b2c5SNicholas Bellinger 					&tcm_loop_set_default_node_attributes;
14403703b2c5SNicholas Bellinger 	fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
14413703b2c5SNicholas Bellinger 	fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
14423703b2c5SNicholas Bellinger 	fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
14433703b2c5SNicholas Bellinger 	fabric->tf_ops.queue_status = &tcm_loop_queue_status;
14443703b2c5SNicholas Bellinger 	fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
14453703b2c5SNicholas Bellinger 	fabric->tf_ops.set_fabric_sense_len = &tcm_loop_set_fabric_sense_len;
14463703b2c5SNicholas Bellinger 	fabric->tf_ops.get_fabric_sense_len = &tcm_loop_get_fabric_sense_len;
14473703b2c5SNicholas Bellinger 	fabric->tf_ops.is_state_remove = &tcm_loop_is_state_remove;
14483703b2c5SNicholas Bellinger 
14493703b2c5SNicholas Bellinger 	tf_cg = &fabric->tf_group;
14503703b2c5SNicholas Bellinger 	/*
14513703b2c5SNicholas Bellinger 	 * Setup function pointers for generic logic in target_core_fabric_configfs.c
14523703b2c5SNicholas Bellinger 	 */
14533703b2c5SNicholas Bellinger 	fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
14543703b2c5SNicholas Bellinger 	fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
14553703b2c5SNicholas Bellinger 	fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
14563703b2c5SNicholas Bellinger 	fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
14573703b2c5SNicholas Bellinger 	/*
14583703b2c5SNicholas Bellinger 	 * fabric_post_link() and fabric_pre_unlink() are used for
14593703b2c5SNicholas Bellinger 	 * registration and release of TCM Loop Virtual SCSI LUNs.
14603703b2c5SNicholas Bellinger 	 */
14613703b2c5SNicholas Bellinger 	fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
14623703b2c5SNicholas Bellinger 	fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
14633703b2c5SNicholas Bellinger 	fabric->tf_ops.fabric_make_np = NULL;
14643703b2c5SNicholas Bellinger 	fabric->tf_ops.fabric_drop_np = NULL;
14653703b2c5SNicholas Bellinger 	/*
14663703b2c5SNicholas Bellinger 	 * Setup default attribute lists for various fabric->tf_cit_tmpl
14673703b2c5SNicholas Bellinger 	 */
14683703b2c5SNicholas Bellinger 	TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
14693703b2c5SNicholas Bellinger 	TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
14703703b2c5SNicholas Bellinger 	TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
14713703b2c5SNicholas Bellinger 	TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
14723703b2c5SNicholas Bellinger 	TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
14733703b2c5SNicholas Bellinger 	/*
14743703b2c5SNicholas Bellinger 	 * Once fabric->tf_ops has been setup, now register the fabric for
14753703b2c5SNicholas Bellinger 	 * use within TCM
14763703b2c5SNicholas Bellinger 	 */
14773703b2c5SNicholas Bellinger 	ret = target_fabric_configfs_register(fabric);
14783703b2c5SNicholas Bellinger 	if (ret < 0) {
14796708bb27SAndy Grover 		pr_err("target_fabric_configfs_register() for"
14803703b2c5SNicholas Bellinger 				" TCM_Loop failed!\n");
14813703b2c5SNicholas Bellinger 		target_fabric_configfs_free(fabric);
14823703b2c5SNicholas Bellinger 		return -1;
14833703b2c5SNicholas Bellinger 	}
14843703b2c5SNicholas Bellinger 	/*
14853703b2c5SNicholas Bellinger 	 * Setup our local pointer to *fabric.
14863703b2c5SNicholas Bellinger 	 */
14873703b2c5SNicholas Bellinger 	tcm_loop_fabric_configfs = fabric;
14886708bb27SAndy Grover 	pr_debug("TCM_LOOP[0] - Set fabric ->"
14893703b2c5SNicholas Bellinger 			" tcm_loop_fabric_configfs\n");
14903703b2c5SNicholas Bellinger 	return 0;
14913703b2c5SNicholas Bellinger }
14923703b2c5SNicholas Bellinger 
14933703b2c5SNicholas Bellinger static void tcm_loop_deregister_configfs(void)
14943703b2c5SNicholas Bellinger {
14953703b2c5SNicholas Bellinger 	if (!tcm_loop_fabric_configfs)
14963703b2c5SNicholas Bellinger 		return;
14973703b2c5SNicholas Bellinger 
14983703b2c5SNicholas Bellinger 	target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
14993703b2c5SNicholas Bellinger 	tcm_loop_fabric_configfs = NULL;
15006708bb27SAndy Grover 	pr_debug("TCM_LOOP[0] - Cleared"
15013703b2c5SNicholas Bellinger 				" tcm_loop_fabric_configfs\n");
15023703b2c5SNicholas Bellinger }
15033703b2c5SNicholas Bellinger 
15043703b2c5SNicholas Bellinger static int __init tcm_loop_fabric_init(void)
15053703b2c5SNicholas Bellinger {
15063703b2c5SNicholas Bellinger 	int ret;
15073703b2c5SNicholas Bellinger 
15083703b2c5SNicholas Bellinger 	tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
15093703b2c5SNicholas Bellinger 				sizeof(struct tcm_loop_cmd),
15103703b2c5SNicholas Bellinger 				__alignof__(struct tcm_loop_cmd),
15113703b2c5SNicholas Bellinger 				0, NULL);
15123703b2c5SNicholas Bellinger 	if (!tcm_loop_cmd_cache) {
15136708bb27SAndy Grover 		pr_debug("kmem_cache_create() for"
15143703b2c5SNicholas Bellinger 			" tcm_loop_cmd_cache failed\n");
15153703b2c5SNicholas Bellinger 		return -ENOMEM;
15163703b2c5SNicholas Bellinger 	}
15173703b2c5SNicholas Bellinger 
15183703b2c5SNicholas Bellinger 	ret = tcm_loop_alloc_core_bus();
15193703b2c5SNicholas Bellinger 	if (ret)
15203703b2c5SNicholas Bellinger 		return ret;
15213703b2c5SNicholas Bellinger 
15223703b2c5SNicholas Bellinger 	ret = tcm_loop_register_configfs();
15233703b2c5SNicholas Bellinger 	if (ret) {
15243703b2c5SNicholas Bellinger 		tcm_loop_release_core_bus();
15253703b2c5SNicholas Bellinger 		return ret;
15263703b2c5SNicholas Bellinger 	}
15273703b2c5SNicholas Bellinger 
15283703b2c5SNicholas Bellinger 	return 0;
15293703b2c5SNicholas Bellinger }
15303703b2c5SNicholas Bellinger 
15313703b2c5SNicholas Bellinger static void __exit tcm_loop_fabric_exit(void)
15323703b2c5SNicholas Bellinger {
15333703b2c5SNicholas Bellinger 	tcm_loop_deregister_configfs();
15343703b2c5SNicholas Bellinger 	tcm_loop_release_core_bus();
15353703b2c5SNicholas Bellinger 	kmem_cache_destroy(tcm_loop_cmd_cache);
15363703b2c5SNicholas Bellinger }
15373703b2c5SNicholas Bellinger 
15383703b2c5SNicholas Bellinger MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
15393703b2c5SNicholas Bellinger MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
15403703b2c5SNicholas Bellinger MODULE_LICENSE("GPL");
15413703b2c5SNicholas Bellinger module_init(tcm_loop_fabric_init);
15423703b2c5SNicholas Bellinger module_exit(tcm_loop_fabric_exit);
1543