13703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/*******************************************************************************
23703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger *
33703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * This file contains the Linux/SCSI LLD virtual SCSI initiator driver
43703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * for emulated SAS initiator ports
53703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger *
63703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * © Copyright 2011 RisingTide Systems LLC.
73703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger *
83703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
93703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger *
103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger *
123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * This program is free software; you can redistribute it and/or modify
133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * it under the terms of the GNU General Public License as published by
143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * the Free Software Foundation; either version 2 of the License, or
153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * (at your option) any later version.
163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger *
173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * This program is distributed in the hope that it will be useful,
183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * but WITHOUT ANY WARRANTY; without even the implied warranty of
193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * GNU General Public License for more details.
213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger ****************************************************************************/
223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <linux/module.h>
243703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <linux/moduleparam.h>
253703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <linux/init.h>
263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <linux/slab.h>
273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <linux/types.h>
283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <linux/configfs.h>
293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <scsi/scsi.h>
303703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <scsi/scsi_tcq.h>
313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <scsi/scsi_host.h>
323703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <scsi/scsi_device.h>
333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <scsi/scsi_cmnd.h>
343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <target/target_core_base.h>
36c4795fb20edf2fe2c862c8fe9f8b681edeb79ac1Christoph Hellwig#include <target/target_core_fabric.h>
373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <target/target_core_fabric_configfs.h>
383703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include <target/target_core_configfs.h>
393703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
403703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#include "tcm_loop.h"
413703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
423703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger#define to_tcm_loop_hba(hba)	container_of(hba, struct tcm_loop_hba, dev)
433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
443703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/* Local pointer to allocated TCM configfs fabric module */
453703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic struct target_fabric_configfs *tcm_loop_fabric_configfs;
463703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
47afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwigstatic struct workqueue_struct *tcm_loop_workqueue;
483703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic struct kmem_cache *tcm_loop_cmd_cache;
493703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_hba_no_cnt;
513703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5216786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwigstatic int tcm_loop_queue_status(struct se_cmd *se_cmd);
533703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
543703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/*
553703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Called from struct target_core_fabric_ops->check_stop_free()
563703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger */
5788dd9e26d6d3e743f9c7e4562b94b2ad3c2994d3Nicholas Bellingerstatic int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
583703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
603703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Do not release struct se_cmd's containing a valid TMR
613703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * pointer.  These will be released directly in tcm_loop_device_reset()
623703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * with transport_generic_free_cmd().
633703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
64c8e31f26feeb03dc6f51bff68135cc58431e099bAndy Grover	if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
6588dd9e26d6d3e743f9c7e4562b94b2ad3c2994d3Nicholas Bellinger		return 0;
663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
673703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Release the struct se_cmd, which will make a callback to release
683703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd()
693703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
7082f1c8a4e7739eae9f1c32c2c419efdc19b8af41Christoph Hellwig	transport_generic_free_cmd(se_cmd, 0);
7188dd9e26d6d3e743f9c7e4562b94b2ad3c2994d3Nicholas Bellinger	return 1;
723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7435462975b2b197b990fedbb74b81f9bea9d344cbChristoph Hellwigstatic void tcm_loop_release_cmd(struct se_cmd *se_cmd)
753703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
773703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				struct tcm_loop_cmd, tl_se_cmd);
783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
823703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_proc_info(struct Scsi_Host *host, char *buffer,
833703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				char **start, off_t offset,
843703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				int length, int inout)
853703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
863703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return sprintf(buffer, "tcm_loop_proc_info()\n");
873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_driver_probe(struct device *);
903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_driver_remove(struct device *);
913703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
923703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int pseudo_lld_bus_match(struct device *dev,
933703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				struct device_driver *dev_driver)
943703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
953703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 1;
963703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
973703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
983703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic struct bus_type tcm_loop_lld_bus = {
993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.name			= "tcm_loop_bus",
1003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.match			= pseudo_lld_bus_match,
1013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.probe			= tcm_loop_driver_probe,
1023703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.remove			= tcm_loop_driver_remove,
1033703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger};
1043703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
1053703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic struct device_driver tcm_loop_driverfs = {
1063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.name			= "tcm_loop",
1073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.bus			= &tcm_loop_lld_bus,
1083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger};
1093703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/*
1103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Used with root_device_register() in tcm_loop_alloc_core_bus() below
1113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger */
1123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstruct device *tcm_loop_primary;
1133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
1143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/*
1153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and
1163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * drivers/scsi/libiscsi.c:iscsi_change_queue_depth()
1173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger */
1183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_change_queue_depth(
1193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct scsi_device *sdev,
1203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	int depth,
1213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	int reason)
1223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
1233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	switch (reason) {
1243703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_QDEPTH_DEFAULT:
1253703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		break;
1273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_QDEPTH_QFULL:
1283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		scsi_track_queue_full(sdev, depth);
1293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		break;
1303703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_QDEPTH_RAMP_UP:
1313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1323703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		break;
1333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	default:
1343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return -EOPNOTSUPP;
1353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
1363703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return sdev->queue_depth;
1373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
1383703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
1393703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/*
140f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig * Locate the SAM Task Attr from struct scsi_cmnd *
141f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig */
142f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwigstatic int tcm_loop_sam_attr(struct scsi_cmnd *sc)
143f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig{
144f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig	if (sc->device->tagged_supported) {
145f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		switch (sc->tag) {
146f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		case HEAD_OF_QUEUE_TAG:
147f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig			return MSG_HEAD_TAG;
148f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		case ORDERED_QUEUE_TAG:
149f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig			return MSG_ORDERED_TAG;
150f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		default:
151f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig			break;
152f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		}
153f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig	}
154f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig
155f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig	return MSG_SIMPLE_TAG;
156f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig}
157f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig
158afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwigstatic void tcm_loop_submission_work(struct work_struct *work)
1593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
160afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	struct tcm_loop_cmd *tl_cmd =
161afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig		container_of(work, struct tcm_loop_cmd, work);
162afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
163afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	struct scsi_cmnd *sc = tl_cmd->sc;
164afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	struct tcm_loop_nexus *tl_nexus;
1653703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba;
1663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_tpg *tl_tpg;
16716786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	struct scatterlist *sgl_bidi = NULL;
16816786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	u32 sgl_bidi_count = 0;
16916786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	int ret;
1703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
1713703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
1723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
173afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig
1740a020436d8baf412bcf5997aee7796276ea773aeNicholas Bellinger	/*
1750a020436d8baf412bcf5997aee7796276ea773aeNicholas Bellinger	 * Ensure that this tl_tpg reference from the incoming sc->device->id
1760a020436d8baf412bcf5997aee7796276ea773aeNicholas Bellinger	 * has already been configured via tcm_loop_make_naa_tpg().
1770a020436d8baf412bcf5997aee7796276ea773aeNicholas Bellinger	 */
1780a020436d8baf412bcf5997aee7796276ea773aeNicholas Bellinger	if (!tl_tpg->tl_hba) {
1790a020436d8baf412bcf5997aee7796276ea773aeNicholas Bellinger		set_host_byte(sc, DID_NO_CONNECT);
180f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		goto out_done;
1810a020436d8baf412bcf5997aee7796276ea773aeNicholas Bellinger	}
182f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig
183f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig	tl_nexus = tl_hba->tl_nexus;
184f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig	if (!tl_nexus) {
185f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
186f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig				" does not exist\n");
187f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		set_host_byte(sc, DID_ERROR);
188f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		goto out_done;
1893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
190f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig
191afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	transport_init_se_cmd(se_cmd, tl_tpg->tl_se_tpg.se_tpg_tfo,
192afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig			tl_nexus->se_sess,
193f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig			scsi_bufflen(sc), sc->sc_data_direction,
194f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig			tcm_loop_sam_attr(sc), &tl_cmd->tl_sense_buf[0]);
195f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig
19616786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	if (scsi_bidi_cmnd(sc)) {
19716786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		struct scsi_data_buffer *sdb = scsi_in(sc);
19816786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig
19916786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		sgl_bidi = sdb->table.sgl;
20016786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		sgl_bidi_count = sdb->table.nents;
201f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		se_cmd->se_cmd_flags |= SCF_BIDI;
202f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig
20316786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	}
20416786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig
205f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig	if (transport_lookup_cmd_lun(se_cmd, tl_cmd->sc->device->lun) < 0) {
206f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
207f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		set_host_byte(sc, DID_NO_CONNECT);
208f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig		goto out_done;
209f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig	}
210f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig
21116786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	/*
21216786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	 * Because some userspace code via scsi-generic do not memset their
21316786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	 * associated read buffers, go ahead and do that here for type
21416786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	 * SCF_SCSI_CONTROL_SG_IO_CDB.  Also note that this is currently
21516786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	 * guaranteed to be a single SGL for SCF_SCSI_CONTROL_SG_IO_CDB
21616786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	 * by target core in transport_generic_allocate_tasks() ->
21716786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	 * transport_generic_cmd_sequencer().
21816786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	 */
21916786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	if (se_cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB &&
22016786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	    se_cmd->data_direction == DMA_FROM_DEVICE) {
22116786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		struct scatterlist *sg = scsi_sglist(sc);
22216786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		unsigned char *buf = kmap(sg_page(sg)) + sg->offset;
22316786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig
22416786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		if (buf != NULL) {
22516786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig			memset(buf, 0, sg->length);
22616786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig			kunmap(sg_page(sg));
22716786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		}
22816786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	}
22916786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig
23016786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	ret = transport_generic_allocate_tasks(se_cmd, sc->cmnd);
23116786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	if (ret == -ENOMEM) {
23216786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		transport_send_check_condition_and_sense(se_cmd,
23316786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig				TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
23416786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		transport_generic_free_cmd(se_cmd, 0);
23516786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		return;
236ee9b866a3696ae434418348e2f499c41841366d4Nicholas Bellinger	} else if (ret < 0) {
23716786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		if (se_cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT)
23816786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig			tcm_loop_queue_status(se_cmd);
23916786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		else
24016786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig			transport_send_check_condition_and_sense(se_cmd,
24116786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig					se_cmd->scsi_sense_reason, 0);
24216786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		transport_generic_free_cmd(se_cmd, 0);
24316786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		return;
24416786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	}
24516786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig
24616786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	ret = transport_generic_map_mem_to_cmd(se_cmd, scsi_sglist(sc),
24716786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig			scsi_sg_count(sc), sgl_bidi, sgl_bidi_count);
24816786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	if (ret) {
24916786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		transport_send_check_condition_and_sense(se_cmd,
25016786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig					se_cmd->scsi_sense_reason, 0);
25116786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		transport_generic_free_cmd(se_cmd, 0);
25216786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig		return;
25316786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	}
25416786454acec0e0e55e32d508b3058b32c1f23f3Christoph Hellwig	transport_handle_cdb_direct(se_cmd);
255afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	return;
256f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig
257f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwigout_done:
258f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig	sc->scsi_done(sc);
259afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	return;
260afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig}
261afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig
262afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig/*
263afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig * ->queuecommand can be and usually is called from interrupt context, so
264afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig * defer the actual submission to a workqueue.
265afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig */
266afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwigstatic int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
267afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig{
268afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	struct tcm_loop_cmd *tl_cmd;
269afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig
270afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	pr_debug("tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x"
271afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig		" scsi_buf_len: %u\n", sc->device->host->host_no,
272afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig		sc->device->id, sc->device->channel, sc->device->lun,
273afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig		sc->cmnd[0], scsi_bufflen(sc));
274afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig
275afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
276afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	if (!tl_cmd) {
277afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig		pr_err("Unable to allocate struct tcm_loop_cmd\n");
278afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig		set_host_byte(sc, DID_ERROR);
279afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig		sc->scsi_done(sc);
280afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig		return 0;
281afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	}
282afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig
283afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	tl_cmd->sc = sc;
284afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
285afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	queue_work(tcm_loop_workqueue, &tl_cmd->work);
286f872c9f417a38a08b6ffe46e1f937d3db1d22775Christoph Hellwig	return 0;
2873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
2883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
2893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/*
2903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Called from SCSI EH process context to issue a LUN_RESET TMR
2913703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * to struct scsi_device
2923703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger */
2933703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_device_reset(struct scsi_cmnd *sc)
2943703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
2953703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_cmd *se_cmd = NULL;
2963703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_portal_group *se_tpg;
2973703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_session *se_sess;
2983703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_cmd *tl_cmd = NULL;
2993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba;
3003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_nexus *tl_nexus;
3013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_tmr *tl_tmr = NULL;
3023703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_tpg *tl_tpg;
303c8e31f26feeb03dc6f51bff68135cc58431e099bAndy Grover	int ret = FAILED, rc;
3043703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
3053703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Locate the tcm_loop_hba_t pointer
3063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
3073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
3083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
3093703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Locate the tl_nexus and se_sess pointers
3103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
3113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_nexus = tl_hba->tl_nexus;
3123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!tl_nexus) {
3136708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unable to perform device reset without"
3143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				" active I_T Nexus\n");
3153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return FAILED;
3163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
3173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	se_sess = tl_nexus->se_sess;
3183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
3193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Locate the tl_tpg and se_tpg pointers from TargetID in sc->device->id
3203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
3213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
3223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	se_tpg = &tl_tpg->tl_se_tpg;
3233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
3243703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
3253703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!tl_cmd) {
3266708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unable to allocate memory for tl_cmd\n");
3273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return FAILED;
3283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
3293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
3303703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
3313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!tl_tmr) {
3326708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unable to allocate memory for tl_tmr\n");
3333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto release;
3343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
3353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	init_waitqueue_head(&tl_tmr->tl_tmr_wait);
3363703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
3373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	se_cmd = &tl_cmd->tl_se_cmd;
3383703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
3393703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
3403703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
3413703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
34261db1802bf33bf027cd97ba3f79566b2b2fce5c6Nicholas Bellinger				DMA_NONE, MSG_SIMPLE_TAG,
3433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				&tl_cmd->tl_sense_buf[0]);
344c8e31f26feeb03dc6f51bff68135cc58431e099bAndy Grover
345c8e31f26feeb03dc6f51bff68135cc58431e099bAndy Grover	rc = core_tmr_alloc_req(se_cmd, tl_tmr, TMR_LUN_RESET, GFP_KERNEL);
346c8e31f26feeb03dc6f51bff68135cc58431e099bAndy Grover	if (rc < 0)
3473703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto release;
3483703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
3493703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Locate the underlying TCM struct se_lun from sc->device->lun
3503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
3515951146dea1ac8ff2f177477c907084d63913cadAndy Grover	if (transport_lookup_tmr_lun(se_cmd, sc->device->lun) < 0)
3523703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto release;
3533703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
3543703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Queue the TMR to TCM Core and sleep waiting for tcm_loop_queue_tm_rsp()
3553703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * to wake us up.
3563703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
3573703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	transport_generic_handle_tmr(se_cmd);
3583703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
3593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
3603703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * The TMR LUN_RESET has completed, check the response status and
3613703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * then release allocations.
3623703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
3633703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ret = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
3643703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		SUCCESS : FAILED;
3653703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerrelease:
3663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (se_cmd)
36782f1c8a4e7739eae9f1c32c2c419efdc19b8af41Christoph Hellwig		transport_generic_free_cmd(se_cmd, 1);
3683703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	else
3693703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
3703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	kfree(tl_tmr);
3713703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return ret;
3723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
3733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
3743703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_slave_alloc(struct scsi_device *sd)
3753703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
3763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
3773703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
3783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
3793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
3803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_slave_configure(struct scsi_device *sd)
3813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
3823703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
3833703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
3843703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
3853703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic struct scsi_host_template tcm_loop_driver_template = {
3863703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.proc_info		= tcm_loop_proc_info,
3873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.proc_name		= "tcm_loopback",
3883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.name			= "TCM_Loopback",
3893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.queuecommand		= tcm_loop_queuecommand,
3903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.change_queue_depth	= tcm_loop_change_queue_depth,
3913703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.eh_device_reset_handler = tcm_loop_device_reset,
3922e88efd3aaafa0df05593cdfa5fd17afe922781dChristoph Hellwig	.can_queue		= 1024,
3933703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.this_id		= -1,
3942e88efd3aaafa0df05593cdfa5fd17afe922781dChristoph Hellwig	.sg_tablesize		= 256,
3952e88efd3aaafa0df05593cdfa5fd17afe922781dChristoph Hellwig	.cmd_per_lun		= 1024,
3962e88efd3aaafa0df05593cdfa5fd17afe922781dChristoph Hellwig	.max_sectors		= 0xFFFF,
3973703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.use_clustering		= DISABLE_CLUSTERING,
3983703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.slave_alloc		= tcm_loop_slave_alloc,
3993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.slave_configure	= tcm_loop_slave_configure,
4003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	.module			= THIS_MODULE,
4013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger};
4023703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4033703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_driver_probe(struct device *dev)
4043703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
4053703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba;
4063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct Scsi_Host *sh;
4073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	int error;
4083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4093703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_hba = to_tcm_loop_hba(dev);
4103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	sh = scsi_host_alloc(&tcm_loop_driver_template,
4123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			sizeof(struct tcm_loop_hba));
4133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!sh) {
4146708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unable to allocate struct scsi_host\n");
4153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return -ENODEV;
4163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
4173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_hba->sh = sh;
4183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
4203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
4213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
4223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	*((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
4233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
4243703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Setup single ID, Channel and LUN for now..
4253703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
4263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	sh->max_id = 2;
4273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	sh->max_lun = 0;
4283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	sh->max_channel = 0;
4293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN;
4303703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	error = scsi_add_host(sh, &tl_hba->dev);
4323703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (error) {
4336708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("%s: scsi_add_host failed\n", __func__);
4343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		scsi_host_put(sh);
4353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return -ENODEV;
4363703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
4373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
4383703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
4393703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4403703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_driver_remove(struct device *dev)
4413703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
4423703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba;
4433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct Scsi_Host *sh;
4443703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4453703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_hba = to_tcm_loop_hba(dev);
4463703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	sh = tl_hba->sh;
4473703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4483703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	scsi_remove_host(sh);
4493703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	scsi_host_put(sh);
4503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
4513703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
4523703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4533703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic void tcm_loop_release_adapter(struct device *dev)
4543703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
4553703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
4563703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4573703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	kfree(tl_hba);
4583703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
4593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4603703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/*
4613703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
4623703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger */
4633703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
4643703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
4653703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	int ret;
4663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4673703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_hba->dev.bus = &tcm_loop_lld_bus;
4683703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_hba->dev.parent = tcm_loop_primary;
4693703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_hba->dev.release = &tcm_loop_release_adapter;
4703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
4713703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ret = device_register(&tl_hba->dev);
4733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ret) {
4746708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("device_register() failed for"
4753703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				" tl_hba->dev: %d\n", ret);
4763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return -ENODEV;
4773703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
4783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
4803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
4813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4823703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/*
4833703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
4843703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * tcm_loop SCSI bus.
4853703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger */
4863703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_alloc_core_bus(void)
4873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
4883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	int ret;
4893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tcm_loop_primary = root_device_register("tcm_loop_0");
4913703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (IS_ERR(tcm_loop_primary)) {
4926708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unable to allocate tcm_loop_primary\n");
4933703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return PTR_ERR(tcm_loop_primary);
4943703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
4953703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
4963703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ret = bus_register(&tcm_loop_lld_bus);
4973703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ret) {
4986708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("bus_register() failed for tcm_loop_lld_bus\n");
4993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto dev_unreg;
5003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
5013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5023703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ret = driver_register(&tcm_loop_driverfs);
5033703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ret) {
5046708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("driver_register() failed for"
5053703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				"tcm_loop_driverfs\n");
5063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto bus_unreg;
5073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
5083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5096708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("Initialized TCM Loop Core Bus\n");
5103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return ret;
5113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerbus_unreg:
5133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	bus_unregister(&tcm_loop_lld_bus);
5143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerdev_unreg:
5153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	root_device_unregister(tcm_loop_primary);
5163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return ret;
5173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
5183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic void tcm_loop_release_core_bus(void)
5203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
5213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	driver_unregister(&tcm_loop_driverfs);
5223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	bus_unregister(&tcm_loop_lld_bus);
5233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	root_device_unregister(tcm_loop_primary);
5243703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5256708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("Releasing TCM Loop Core BUS\n");
5263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
5273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic char *tcm_loop_get_fabric_name(void)
5293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
5303703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return "loopback";
5313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
5323703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
5343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
5358359cf43b9dccddeebb0d247146719a14ce6371aJörn Engel	struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
5363703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
5373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
5383703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
5393703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * time based on the protocol dependent prefix of the passed configfs group.
5403703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 *
5413703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
5423703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * ProtocolID using target_core_fabric_lib.c symbols.
5433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
5443703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	switch (tl_hba->tl_proto_id) {
5453703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_SAS:
5463703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return sas_get_fabric_proto_ident(se_tpg);
5473703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_FCP:
5483703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return fc_get_fabric_proto_ident(se_tpg);
5493703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_ISCSI:
5503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return iscsi_get_fabric_proto_ident(se_tpg);
5513703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	default:
5526708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unknown tl_proto_id: 0x%02x, using"
5533703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			" SAS emulation\n", tl_hba->tl_proto_id);
5543703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		break;
5553703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
5563703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5573703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return sas_get_fabric_proto_ident(se_tpg);
5583703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
5593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5603703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
5613703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
5628359cf43b9dccddeebb0d247146719a14ce6371aJörn Engel	struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
5633703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
5643703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Return the passed NAA identifier for the SAS Target Port
5653703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
5663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return &tl_tpg->tl_hba->tl_wwn_address[0];
5673703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
5683703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5693703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
5703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
5718359cf43b9dccddeebb0d247146719a14ce6371aJörn Engel	struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
5723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
5733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
5743703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * to represent the SCSI Target Port.
5753703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
5763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return tl_tpg->tl_tpgt;
5773703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
5783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
5803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
5813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 1;
5823703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
5833703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5843703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic u32 tcm_loop_get_pr_transport_id(
5853703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_portal_group *se_tpg,
5863703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_node_acl *se_nacl,
5873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct t10_pr_registration *pr_reg,
5883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	int *format_code,
5893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	unsigned char *buf)
5903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
5918359cf43b9dccddeebb0d247146719a14ce6371aJörn Engel	struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
5923703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
5933703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
5943703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	switch (tl_hba->tl_proto_id) {
5953703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_SAS:
5963703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
5973703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					format_code, buf);
5983703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_FCP:
5993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
6003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					format_code, buf);
6013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_ISCSI:
6023703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
6033703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					format_code, buf);
6043703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	default:
6056708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unknown tl_proto_id: 0x%02x, using"
6063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			" SAS emulation\n", tl_hba->tl_proto_id);
6073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		break;
6083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
6093703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
6103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
6113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			format_code, buf);
6123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
6133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
6143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic u32 tcm_loop_get_pr_transport_id_len(
6153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_portal_group *se_tpg,
6163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_node_acl *se_nacl,
6173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct t10_pr_registration *pr_reg,
6183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	int *format_code)
6193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
6208359cf43b9dccddeebb0d247146719a14ce6371aJörn Engel	struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
6213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
6223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
6233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	switch (tl_hba->tl_proto_id) {
6243703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_SAS:
6253703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
6263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					format_code);
6273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_FCP:
6283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
6293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					format_code);
6303703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_ISCSI:
6313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
6323703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					format_code);
6333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	default:
6346708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unknown tl_proto_id: 0x%02x, using"
6353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			" SAS emulation\n", tl_hba->tl_proto_id);
6363703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		break;
6373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
6383703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
6393703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
6403703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			format_code);
6413703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
6423703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
6433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/*
6443703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
6453703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
6463703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger */
6473703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic char *tcm_loop_parse_pr_out_transport_id(
6483703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_portal_group *se_tpg,
6493703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	const char *buf,
6503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	u32 *out_tid_len,
6513703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	char **port_nexus_ptr)
6523703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
6538359cf43b9dccddeebb0d247146719a14ce6371aJörn Engel	struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
6543703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
6553703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
6563703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	switch (tl_hba->tl_proto_id) {
6573703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_SAS:
6583703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
6593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					port_nexus_ptr);
6603703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_FCP:
6613703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
6623703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					port_nexus_ptr);
6633703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_ISCSI:
6643703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
6653703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					port_nexus_ptr);
6663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	default:
6676708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unknown tl_proto_id: 0x%02x, using"
6683703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			" SAS emulation\n", tl_hba->tl_proto_id);
6693703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		break;
6703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
6713703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
6723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
6733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			port_nexus_ptr);
6743703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
6753703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
6763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/*
6773703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
6783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * based upon the incoming fabric dependent SCSI Initiator Port
6793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger */
6803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
6813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
6823703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 1;
6833703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
6843703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
6853703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
6863703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
6873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
6883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
6893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
6903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/*
6913703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
6923703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
6933703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger */
6943703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
6953703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
6963703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
6973703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
6983703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
6993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/*
7003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
7013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * never be called for TCM_Loop by target_core_fabric_configfs.c code.
7023703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger * It has been added here as a nop for target_fabric_tf_ops_check()
7033703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger */
7043703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
7053703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
7073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
7083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7093703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
7103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_portal_group *se_tpg)
7113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_nacl *tl_nacl;
7133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
7153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!tl_nacl) {
7166708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unable to allocate struct tcm_loop_nacl\n");
7173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return NULL;
7183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
7193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return &tl_nacl->se_node_acl;
7213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
7223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic void tcm_loop_tpg_release_fabric_acl(
7243703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_portal_group *se_tpg,
7253703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_node_acl *se_nacl)
7263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
7283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				struct tcm_loop_nacl, se_node_acl);
7293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7303703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	kfree(tl_nacl);
7313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
7323703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
7343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 1;
7363703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
7373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7383703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic u32 tcm_loop_sess_get_index(struct se_session *se_sess)
7393703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7403703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 1;
7413703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
7423703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
7443703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7453703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return;
7463703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
7473703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7483703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
7493703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 1;
7513703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
7523703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7533703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
7543703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7553703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
7563703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			struct tcm_loop_cmd, tl_se_cmd);
7573703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7583703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return tl_cmd->sc_cmd_state;
7593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
7603703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7613703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_shutdown_session(struct se_session *se_sess)
7623703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7633703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
7643703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
7653703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic void tcm_loop_close_session(struct se_session *se_sess)
7673703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7683703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return;
7693703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger};
7703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7713703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_write_pending(struct se_cmd *se_cmd)
7723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
7743703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Since Linux/SCSI has already sent down a struct scsi_cmnd
7753703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
7763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
7773703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * format with transport_generic_map_mem_to_cmd().
7783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 *
7793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * We now tell TCM to add this WRITE CDB directly into the TCM storage
7803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * object execution queue.
7813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
7823703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	transport_generic_process_write(se_cmd);
7833703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
7843703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
7853703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7863703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
7873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
7893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
7903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7913703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
7923703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
7933703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
7943703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				struct tcm_loop_cmd, tl_se_cmd);
7953703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct scsi_cmnd *sc = tl_cmd->sc;
7963703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
7976708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
7983703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		     " cdb: 0x%02x\n", sc, sc->cmnd[0]);
7993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	sc->result = SAM_STAT_GOOD;
8013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	set_host_byte(sc, DID_OK);
8026cf3fa6918baab0c447f1206f1cef9166ad04864Roland Dreier	if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
8036cf3fa6918baab0c447f1206f1cef9166ad04864Roland Dreier	    (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
8046cf3fa6918baab0c447f1206f1cef9166ad04864Roland Dreier		scsi_set_resid(sc, se_cmd->residual_count);
8053703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	sc->scsi_done(sc);
8063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
8073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
8083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8093703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_queue_status(struct se_cmd *se_cmd)
8103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
8113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
8123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				struct tcm_loop_cmd, tl_se_cmd);
8133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct scsi_cmnd *sc = tl_cmd->sc;
8143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8156708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
8163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			" cdb: 0x%02x\n", sc, sc->cmnd[0]);
8173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (se_cmd->sense_buffer &&
8193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	   ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
8203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	    (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
8213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8225951146dea1ac8ff2f177477c907084d63913cadAndy Grover		memcpy(sc->sense_buffer, se_cmd->sense_buffer,
8233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				SCSI_SENSE_BUFFERSIZE);
8243703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		sc->result = SAM_STAT_CHECK_CONDITION;
8253703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		set_driver_byte(sc, DRIVER_SENSE);
8263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	} else
8273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		sc->result = se_cmd->scsi_status;
8283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	set_host_byte(sc, DID_OK);
8306cf3fa6918baab0c447f1206f1cef9166ad04864Roland Dreier	if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
8316cf3fa6918baab0c447f1206f1cef9166ad04864Roland Dreier	    (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
8326cf3fa6918baab0c447f1206f1cef9166ad04864Roland Dreier		scsi_set_resid(sc, se_cmd->residual_count);
8333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	sc->scsi_done(sc);
8343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
8353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
8363703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
8383703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
8393703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
8403703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
8413703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
8423703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
8433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
8443703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
8453703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	atomic_set(&tl_tmr->tmr_complete, 1);
8463703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	wake_up(&tl_tmr->tl_tmr_wait);
8473703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
8483703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
8493703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic u16 tcm_loop_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
8513703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
8523703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
8533703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
8543703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8553703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic u16 tcm_loop_get_fabric_sense_len(void)
8563703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
8573703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
8583703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
8593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8603703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
8613703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
8623703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	switch (tl_hba->tl_proto_id) {
8633703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_SAS:
8643703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return "SAS";
8653703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_FCP:
8663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return "FCP";
8673703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	case SCSI_PROTOCOL_ISCSI:
8683703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return "iSCSI";
8693703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	default:
8703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		break;
8713703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
8723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return "Unknown";
8743703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
8753703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/* Start items for tcm_loop_port_cit */
8773703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_port_link(
8793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_portal_group *se_tpg,
8803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_lun *lun)
8813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
8823703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
8833703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				struct tcm_loop_tpg, tl_se_tpg);
8843703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
8853703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8863703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	atomic_inc(&tl_tpg->tl_tpg_port_count);
8873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	smp_mb__after_atomic_inc();
8883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
8893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Add Linux/SCSI struct scsi_device by HCTL
8903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
8913703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
8923703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8936708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
8943703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
8953703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
8963703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
8973703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic void tcm_loop_port_unlink(
8983703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_portal_group *se_tpg,
8993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_lun *se_lun)
9003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
9013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct scsi_device *sd;
9023703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba;
9033703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_tpg *tl_tpg;
9043703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9053703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
9063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_hba = tl_tpg->tl_hba;
9073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
9093703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				se_lun->unpacked_lun);
9103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!sd) {
9116708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unable to locate struct scsi_device for %d:%d:"
9123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			"%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
9133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return;
9143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
9153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
9163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Remove Linux/SCSI struct scsi_device by HCTL
9173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
9183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	scsi_remove_device(sd);
9193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	scsi_device_put(sd);
9203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	atomic_dec(&tl_tpg->tl_tpg_port_count);
9223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	smp_mb__after_atomic_dec();
9233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9246708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
9253703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
9263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/* End items for tcm_loop_port_cit */
9283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/* Start items for tcm_loop_nexus_cit */
9303703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_make_nexus(
9323703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_tpg *tl_tpg,
9333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	const char *name)
9343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
9353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_portal_group *se_tpg;
9363703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
9373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_nexus *tl_nexus;
938552523dcbf0f33d44d816da310be8227a2c1502aDan Carpenter	int ret = -ENOMEM;
9393703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9403703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (tl_tpg->tl_hba->tl_nexus) {
9416708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n");
9423703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return -EEXIST;
9433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
9443703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	se_tpg = &tl_tpg->tl_se_tpg;
9453703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9463703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
9473703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!tl_nexus) {
9486708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unable to allocate struct tcm_loop_nexus\n");
9493703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return -ENOMEM;
9503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
9513703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
9523703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Initialize the struct se_session pointer
9533703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
9543703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_nexus->se_sess = transport_init_session();
955552523dcbf0f33d44d816da310be8227a2c1502aDan Carpenter	if (IS_ERR(tl_nexus->se_sess)) {
956552523dcbf0f33d44d816da310be8227a2c1502aDan Carpenter		ret = PTR_ERR(tl_nexus->se_sess);
9573703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto out;
958552523dcbf0f33d44d816da310be8227a2c1502aDan Carpenter	}
9593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
9603703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Since we are running in 'demo mode' this call with generate a
9613703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
9623703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Initiator port name of the passed configfs group 'name'.
9633703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
9643703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
9653703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				se_tpg, (unsigned char *)name);
9663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!tl_nexus->se_sess->se_node_acl) {
9673703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		transport_free_session(tl_nexus->se_sess);
9683703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto out;
9693703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
9703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
9713703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Now, register the SAS I_T Nexus as active with the call to
9723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * transport_register_session()
9733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
9743703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	__transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
9755951146dea1ac8ff2f177477c907084d63913cadAndy Grover			tl_nexus->se_sess, tl_nexus);
9763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_tpg->tl_hba->tl_nexus = tl_nexus;
9776708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
9783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		" %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
9793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		name);
9803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
9813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9823703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerout:
9833703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	kfree(tl_nexus);
984552523dcbf0f33d44d816da310be8227a2c1502aDan Carpenter	return ret;
9853703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
9863703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_drop_nexus(
9883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_tpg *tpg)
9893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
9903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_session *se_sess;
9913703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_nexus *tl_nexus;
9923703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba = tpg->tl_hba;
9933703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9943703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_nexus = tpg->tl_hba->tl_nexus;
9953703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!tl_nexus)
9963703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return -ENODEV;
9973703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
9983703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	se_sess = tl_nexus->se_sess;
9993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!se_sess)
10003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return -ENODEV;
10013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
10023703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (atomic_read(&tpg->tl_tpg_port_count)) {
10036708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unable to remove TCM_Loop I_T Nexus with"
10043703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			" active TPG port count: %d\n",
10053703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			atomic_read(&tpg->tl_tpg_port_count));
10063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return -EPERM;
10073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
10083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
10096708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
10103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		" %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
10113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		tl_nexus->se_sess->se_node_acl->initiatorname);
10123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
10133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Release the SCSI I_T Nexus to the emulated SAS Target Port
10143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
10153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	transport_deregister_session(tl_nexus->se_sess);
10163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tpg->tl_hba->tl_nexus = NULL;
10173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	kfree(tl_nexus);
10183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
10193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
10203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
10213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/* End items for tcm_loop_nexus_cit */
10223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
10233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic ssize_t tcm_loop_tpg_show_nexus(
10243703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_portal_group *se_tpg,
10253703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	char *page)
10263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
10273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
10283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			struct tcm_loop_tpg, tl_se_tpg);
10293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_nexus *tl_nexus;
10303703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ssize_t ret;
10313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
10323703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_nexus = tl_tpg->tl_hba->tl_nexus;
10333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!tl_nexus)
10343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return -ENODEV;
10353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
10363703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ret = snprintf(page, PAGE_SIZE, "%s\n",
10373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		tl_nexus->se_sess->se_node_acl->initiatorname);
10383703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
10393703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return ret;
10403703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
10413703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
10423703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic ssize_t tcm_loop_tpg_store_nexus(
10433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_portal_group *se_tpg,
10443703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	const char *page,
10453703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	size_t count)
10463703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
10473703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
10483703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			struct tcm_loop_tpg, tl_se_tpg);
10493703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
10503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
10513703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	int ret;
10523703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
10533703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Shutdown the active I_T nexus if 'NULL' is passed..
10543703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
10553703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!strncmp(page, "NULL", 4)) {
10563703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		ret = tcm_loop_drop_nexus(tl_tpg);
10573703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return (!ret) ? count : ret;
10583703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
10593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
10603703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Otherwise make sure the passed virtual Initiator port WWN matches
10613703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
10623703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * tcm_loop_make_nexus()
10633703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
106460d645a4e9e7e7ddc20e534fea82aa4e6947f911Dan Carpenter	if (strlen(page) >= TL_WWN_ADDR_LEN) {
10656708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Emulated NAA Sas Address: %s, exceeds"
10663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				" max: %d\n", page, TL_WWN_ADDR_LEN);
10673703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return -EINVAL;
10683703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
10693703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
10703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
10713703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ptr = strstr(i_port, "naa.");
10723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ptr) {
10733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
10746708bb27bb2703da238f21f516034263348af5beAndy Grover			pr_err("Passed SAS Initiator Port %s does not"
10753703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				" match target port protoid: %s\n", i_port,
10763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				tcm_loop_dump_proto_id(tl_hba));
10773703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			return -EINVAL;
10783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		}
10793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		port_ptr = &i_port[0];
10803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto check_newline;
10813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
10823703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ptr = strstr(i_port, "fc.");
10833703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ptr) {
10843703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
10856708bb27bb2703da238f21f516034263348af5beAndy Grover			pr_err("Passed FCP Initiator Port %s does not"
10863703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				" match target port protoid: %s\n", i_port,
10873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				tcm_loop_dump_proto_id(tl_hba));
10883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			return -EINVAL;
10893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		}
10903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		port_ptr = &i_port[3]; /* Skip over "fc." */
10913703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto check_newline;
10923703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
10933703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ptr = strstr(i_port, "iqn.");
10943703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ptr) {
10953703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
10966708bb27bb2703da238f21f516034263348af5beAndy Grover			pr_err("Passed iSCSI Initiator Port %s does not"
10973703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				" match target port protoid: %s\n", i_port,
10983703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				tcm_loop_dump_proto_id(tl_hba));
10993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			return -EINVAL;
11003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		}
11013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		port_ptr = &i_port[0];
11023703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto check_newline;
11033703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
11046708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_err("Unable to locate prefix for emulated Initiator Port:"
11053703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			" %s\n", i_port);
11063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return -EINVAL;
11073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
11083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Clear any trailing newline for the NAA WWN
11093703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
11103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingercheck_newline:
11113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (i_port[strlen(i_port)-1] == '\n')
11123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		i_port[strlen(i_port)-1] = '\0';
11133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
11143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
11153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ret < 0)
11163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return ret;
11173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
11183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return count;
11193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
11203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
11213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas BellingerTF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
11223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
11233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic struct configfs_attribute *tcm_loop_tpg_attrs[] = {
11243703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	&tcm_loop_tpg_nexus.attr,
11253703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	NULL,
11263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger};
11273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
11283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/* Start items for tcm_loop_naa_cit */
11293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
11303703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstruct se_portal_group *tcm_loop_make_naa_tpg(
11313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_wwn *wwn,
11323703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct config_group *group,
11333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	const char *name)
11343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
11353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba = container_of(wwn,
11363703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			struct tcm_loop_hba, tl_hba_wwn);
11373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_tpg *tl_tpg;
11383703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	char *tpgt_str, *end_ptr;
11393703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	int ret;
11403703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	unsigned short int tpgt;
11413703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
11423703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tpgt_str = strstr(name, "tpgt_");
11433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!tpgt_str) {
11446708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unable to locate \"tpgt_#\" directory"
11453703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				" group\n");
11463703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return ERR_PTR(-EINVAL);
11473703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
11483703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tpgt_str += 5; /* Skip ahead of "tpgt_" */
11493703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
11503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
115112f09ccb4612734a53e47ed5302e0479c10a50f8Dan Carpenter	if (tpgt >= TL_TPGS_PER_HBA) {
11526708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
11533703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				" %u\n", tpgt, TL_TPGS_PER_HBA);
11543703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return ERR_PTR(-EINVAL);
11553703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
11563703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
11573703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_tpg->tl_hba = tl_hba;
11583703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_tpg->tl_tpgt = tpgt;
11593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
11603703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
11613703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
11623703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
11635951146dea1ac8ff2f177477c907084d63913cadAndy Grover			wwn, &tl_tpg->tl_se_tpg, tl_tpg,
11643703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			TRANSPORT_TPG_TYPE_NORMAL);
11653703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ret < 0)
11663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return ERR_PTR(-ENOMEM);
11673703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
11686708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
11693703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		" Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
11703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		config_item_name(&wwn->wwn_group.cg_item), tpgt);
11713703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
11723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return &tl_tpg->tl_se_tpg;
11733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
11743703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
11753703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingervoid tcm_loop_drop_naa_tpg(
11763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_portal_group *se_tpg)
11773703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
11783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_wwn *wwn = se_tpg->se_tpg_wwn;
11793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
11803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				struct tcm_loop_tpg, tl_se_tpg);
11813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba;
11823703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	unsigned short tpgt;
11833703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
11843703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_hba = tl_tpg->tl_hba;
11853703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tpgt = tl_tpg->tl_tpgt;
11863703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
11873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Release the I_T Nexus for the Virtual SAS link if present
11883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
11893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tcm_loop_drop_nexus(tl_tpg);
11903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
11913703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
11923703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
11933703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	core_tpg_deregister(se_tpg);
11943703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
11950a020436d8baf412bcf5997aee7796276ea773aeNicholas Bellinger	tl_tpg->tl_hba = NULL;
11960a020436d8baf412bcf5997aee7796276ea773aeNicholas Bellinger	tl_tpg->tl_tpgt = 0;
11970a020436d8baf412bcf5997aee7796276ea773aeNicholas Bellinger
11986708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
11993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		" Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
12003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		config_item_name(&wwn->wwn_group.cg_item), tpgt);
12013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
12023703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
12033703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/* End items for tcm_loop_naa_cit */
12043703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
12053703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/* Start items for tcm_loop_cit */
12063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
12073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstruct se_wwn *tcm_loop_make_scsi_hba(
12083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct target_fabric_configfs *tf,
12093703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct config_group *group,
12103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	const char *name)
12113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
12123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba;
12133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct Scsi_Host *sh;
12143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	char *ptr;
12153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	int ret, off = 0;
12163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
12173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
12183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!tl_hba) {
12196708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unable to allocate struct tcm_loop_hba\n");
12203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return ERR_PTR(-ENOMEM);
12213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
12223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
12233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Determine the emulated Protocol Identifier and Target Port Name
12243703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * based on the incoming configfs directory name.
12253703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
12263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ptr = strstr(name, "naa.");
12273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ptr) {
12283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
12293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto check_len;
12303703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
12313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ptr = strstr(name, "fc.");
12323703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ptr) {
12333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
12343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		off = 3; /* Skip over "fc." */
12353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto check_len;
12363703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
12373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ptr = strstr(name, "iqn.");
1238a57b5d36453cc9335f75ae191ffd682a250d08baJesper Juhl	if (!ptr) {
12396708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Unable to locate prefix for emulated Target "
1240a57b5d36453cc9335f75ae191ffd682a250d08baJesper Juhl				"Port: %s\n", name);
1241a57b5d36453cc9335f75ae191ffd682a250d08baJesper Juhl		ret = -EINVAL;
1242a57b5d36453cc9335f75ae191ffd682a250d08baJesper Juhl		goto out;
12433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
1244a57b5d36453cc9335f75ae191ffd682a250d08baJesper Juhl	tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
12453703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
12463703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingercheck_len:
124760d645a4e9e7e7ddc20e534fea82aa4e6947f911Dan Carpenter	if (strlen(name) >= TL_WWN_ADDR_LEN) {
12486708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("Emulated NAA %s Address: %s, exceeds"
12493703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			" max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
12503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			TL_WWN_ADDR_LEN);
1251a57b5d36453cc9335f75ae191ffd682a250d08baJesper Juhl		ret = -EINVAL;
1252a57b5d36453cc9335f75ae191ffd682a250d08baJesper Juhl		goto out;
12533703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
12543703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
12553703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
12563703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
12573703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Call device_register(tl_hba->dev) to register the emulated
12583703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
12593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * device_register() callbacks in tcm_loop_driver_probe()
12603703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
12613703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
12623703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ret)
12633703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		goto out;
12643703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
12653703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	sh = tl_hba->sh;
12663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tcm_loop_hba_no_cnt++;
12676708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
12683703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		" %s Address: %s at Linux/SCSI Host ID: %d\n",
12693703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
12703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
12713703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return &tl_hba->tl_hba_wwn;
12723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerout:
12733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	kfree(tl_hba);
12743703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return ERR_PTR(ret);
12753703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
12763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
12773703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingervoid tcm_loop_drop_scsi_hba(
12783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct se_wwn *wwn)
12793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
12803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct tcm_loop_hba *tl_hba = container_of(wwn,
12813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				struct tcm_loop_hba, tl_hba_wwn);
12826297b07cbc42eb6b53eb88fce70a4727ea230797Nicholas Bellinger
12836297b07cbc42eb6b53eb88fce70a4727ea230797Nicholas Bellinger	pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
12846297b07cbc42eb6b53eb88fce70a4727ea230797Nicholas Bellinger		" SAS Address: %s at Linux/SCSI Host ID: %d\n",
12856297b07cbc42eb6b53eb88fce70a4727ea230797Nicholas Bellinger		tl_hba->tl_wwn_address, tl_hba->sh->host_no);
12863703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
12873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Call device_unregister() on the original tl_hba->dev.
12883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
12893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * release *tl_hba;
12903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
12913703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	device_unregister(&tl_hba->dev);
12923703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
12933703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
12943703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/* Start items for tcm_loop_cit */
12953703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic ssize_t tcm_loop_wwn_show_attr_version(
12963703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct target_fabric_configfs *tf,
12973703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	char *page)
12983703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
12993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
13003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
13013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
13023703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas BellingerTF_WWN_ATTR_RO(tcm_loop, version);
13033703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
13043703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic struct configfs_attribute *tcm_loop_wwn_attrs[] = {
13053703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	&tcm_loop_wwn_version.attr,
13063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	NULL,
13073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger};
13083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
13093703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger/* End items for tcm_loop_cit */
13103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
13113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int tcm_loop_register_configfs(void)
13123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
13133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	struct target_fabric_configfs *fabric;
13143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	int ret;
13153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
13163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Set the TCM Loop HBA counter to zero
13173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
13183703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tcm_loop_hba_no_cnt = 0;
13193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
13203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Register the top level struct config_item_type with TCM core
13213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
13223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
1323e3d6f909ed803d92a5ac9b4a2c087e0eae9b90d0Andy Grover	if (IS_ERR(fabric)) {
13246708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("tcm_loop_register_configfs() failed!\n");
1325e3d6f909ed803d92a5ac9b4a2c087e0eae9b90d0Andy Grover		return PTR_ERR(fabric);
13263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
13273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
13283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Setup the fabric API of function pointers used by target_core_mod
13293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
13303703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
13313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
13323703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
13333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
13343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
13353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
13363703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_get_pr_transport_id_len =
13373703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					&tcm_loop_get_pr_transport_id_len;
13383703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_parse_pr_out_transport_id =
13393703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					&tcm_loop_parse_pr_out_transport_id;
13403703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
13413703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_check_demo_mode_cache =
13423703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					&tcm_loop_check_demo_mode_cache;
13433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_check_demo_mode_write_protect =
13443703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					&tcm_loop_check_demo_mode_write_protect;
13453703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_check_prod_mode_write_protect =
13463703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					&tcm_loop_check_prod_mode_write_protect;
13473703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
13483703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * The TCM loopback fabric module runs in demo-mode to a local
13493703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * virtual SCSI device, so fabric dependent initator ACLs are
13503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * not required.
13513703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
13523703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
13533703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_release_fabric_acl =
13543703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					&tcm_loop_tpg_release_fabric_acl;
13553703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
13563703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
13573703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Used for setting up remaining TCM resources in process context
13583703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
13593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
136035462975b2b197b990fedbb74b81f9bea9d344cbChristoph Hellwig	fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
13613703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
13623703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.close_session = &tcm_loop_close_session;
13633703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
13643703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.sess_get_initiator_sid = NULL;
13653703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.write_pending = &tcm_loop_write_pending;
13663703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
13673703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
13683703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Not used for TCM loopback
13693703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
13703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.set_default_node_attributes =
13713703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger					&tcm_loop_set_default_node_attributes;
13723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
13733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
13743703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
13753703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.queue_status = &tcm_loop_queue_status;
13763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
13773703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.set_fabric_sense_len = &tcm_loop_set_fabric_sense_len;
13783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.get_fabric_sense_len = &tcm_loop_get_fabric_sense_len;
13793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
13803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
13813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Setup function pointers for generic logic in target_core_fabric_configfs.c
13823703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
13833703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
13843703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
13853703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
13863703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
13873703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
13883703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * fabric_post_link() and fabric_pre_unlink() are used for
13893703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * registration and release of TCM Loop Virtual SCSI LUNs.
13903703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
13913703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
13923703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
13933703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.fabric_make_np = NULL;
13943703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	fabric->tf_ops.fabric_drop_np = NULL;
13953703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
13963703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Setup default attribute lists for various fabric->tf_cit_tmpl
13973703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
13983703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
13993703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
14003703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
14013703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
14023703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
14033703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
14043703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Once fabric->tf_ops has been setup, now register the fabric for
14053703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * use within TCM
14063703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
14073703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ret = target_fabric_configfs_register(fabric);
14083703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ret < 0) {
14096708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_err("target_fabric_configfs_register() for"
14103703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				" TCM_Loop failed!\n");
14113703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		target_fabric_configfs_free(fabric);
14123703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return -1;
14133703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
14143703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	/*
14153703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 * Setup our local pointer to *fabric.
14163703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	 */
14173703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tcm_loop_fabric_configfs = fabric;
14186708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("TCM_LOOP[0] - Set fabric ->"
14193703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			" tcm_loop_fabric_configfs\n");
14203703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
14213703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
14223703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
14233703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic void tcm_loop_deregister_configfs(void)
14243703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
14253703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!tcm_loop_fabric_configfs)
14263703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger		return;
14273703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
14283703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
14293703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tcm_loop_fabric_configfs = NULL;
14306708bb27bb2703da238f21f516034263348af5beAndy Grover	pr_debug("TCM_LOOP[0] - Cleared"
14313703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				" tcm_loop_fabric_configfs\n");
14323703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
14333703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
14343703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic int __init tcm_loop_fabric_init(void)
14353703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
1436afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	int ret = -ENOMEM;
1437afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig
1438afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
1439afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	if (!tcm_loop_workqueue)
1440afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig		goto out;
14413703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
14423703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
14433703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				sizeof(struct tcm_loop_cmd),
14443703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				__alignof__(struct tcm_loop_cmd),
14453703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger				0, NULL);
14463703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (!tcm_loop_cmd_cache) {
14476708bb27bb2703da238f21f516034263348af5beAndy Grover		pr_debug("kmem_cache_create() for"
14483703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger			" tcm_loop_cmd_cache failed\n");
1449afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig		goto out_destroy_workqueue;
14503703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	}
14513703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
14523703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ret = tcm_loop_alloc_core_bus();
14533703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	if (ret)
1454afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig		goto out_destroy_cache;
14553703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
14563703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	ret = tcm_loop_register_configfs();
1457afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	if (ret)
1458afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig		goto out_release_core_bus;
14593703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
14603703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	return 0;
1461afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig
1462afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwigout_release_core_bus:
1463afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	tcm_loop_release_core_bus();
1464afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwigout_destroy_cache:
1465afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	kmem_cache_destroy(tcm_loop_cmd_cache);
1466afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwigout_destroy_workqueue:
1467afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	destroy_workqueue(tcm_loop_workqueue);
1468afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwigout:
1469afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	return ret;
14703703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
14713703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
14723703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingerstatic void __exit tcm_loop_fabric_exit(void)
14733703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger{
14743703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tcm_loop_deregister_configfs();
14753703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	tcm_loop_release_core_bus();
14763703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger	kmem_cache_destroy(tcm_loop_cmd_cache);
1477afe2cb7fb111ac52ec95ab2bfb19d9d9e0d52ed8Christoph Hellwig	destroy_workqueue(tcm_loop_workqueue);
14783703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger}
14793703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellinger
14803703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas BellingerMODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
14813703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas BellingerMODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
14823703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas BellingerMODULE_LICENSE("GPL");
14833703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingermodule_init(tcm_loop_fabric_init);
14843703b2c5d041a68095cdd22380c23ce27d449ad7Nicholas Bellingermodule_exit(tcm_loop_fabric_exit);
1485