Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) /*******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * This file contains the Linux/SCSI LLD virtual SCSI initiator driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * for emulated SAS initiator ports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * © Copyright 2011-2013 Datera, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * the Free Software Foundation; either version 2 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  ****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/configfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <scsi/scsi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <scsi/scsi_tcq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <scsi/scsi_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <scsi/scsi_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <scsi/scsi_cmnd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <target/target_core_base.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <target/target_core_fabric.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include "tcm_loop.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define to_tcm_loop_hba(hba)	container_of(hba, struct tcm_loop_hba, dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) static struct workqueue_struct *tcm_loop_workqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) static struct kmem_cache *tcm_loop_cmd_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) static int tcm_loop_hba_no_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) static int tcm_loop_queue_status(struct se_cmd *se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * Called from struct target_core_fabric_ops->check_stop_free()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	return transport_generic_free_cmd(se_cmd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 				struct tcm_loop_cmd, tl_se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	seq_puts(m, "tcm_loop_proc_info()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) static int tcm_loop_driver_probe(struct device *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) static int tcm_loop_driver_remove(struct device *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) static int pseudo_lld_bus_match(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 				struct device_driver *dev_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) static struct bus_type tcm_loop_lld_bus = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	.name			= "tcm_loop_bus",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	.match			= pseudo_lld_bus_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	.probe			= tcm_loop_driver_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	.remove			= tcm_loop_driver_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) static struct device_driver tcm_loop_driverfs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	.name			= "tcm_loop",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	.bus			= &tcm_loop_lld_bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * Used with root_device_register() in tcm_loop_alloc_core_bus() below
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) static struct device *tcm_loop_primary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) static void tcm_loop_submission_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	struct tcm_loop_cmd *tl_cmd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		container_of(work, struct tcm_loop_cmd, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	struct scsi_cmnd *sc = tl_cmd->sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	struct tcm_loop_nexus *tl_nexus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	struct tcm_loop_hba *tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	struct tcm_loop_tpg *tl_tpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	struct scatterlist *sgl_bidi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	u32 sgl_bidi_count = 0, transfer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	 * Ensure that this tl_tpg reference from the incoming sc->device->id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	 * has already been configured via tcm_loop_make_naa_tpg().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	if (!tl_tpg->tl_hba) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		set_host_byte(sc, DID_NO_CONNECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		goto out_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 		goto out_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	tl_nexus = tl_tpg->tl_nexus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	if (!tl_nexus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 		scmd_printk(KERN_ERR, sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 			    "TCM_Loop I_T Nexus does not exist\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		set_host_byte(sc, DID_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		goto out_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	transfer_length = scsi_transfer_length(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	if (!scsi_prot_sg_count(sc) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	    scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		se_cmd->prot_pto = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 		 * loopback transport doesn't support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		 * WRITE_GENERATE, READ_STRIP protection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 		 * information operations, go ahead unprotected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		transfer_length = scsi_bufflen(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	se_cmd->tag = tl_cmd->sc_cmd_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	rc = target_submit_cmd_map_sgls(se_cmd, tl_nexus->se_sess, sc->cmnd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 			&tl_cmd->tl_sense_buf[0], tl_cmd->sc->device->lun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 			transfer_length, TCM_SIMPLE_TAG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 			sc->sc_data_direction, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 			scsi_sglist(sc), scsi_sg_count(sc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 			sgl_bidi, sgl_bidi_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 			scsi_prot_sglist(sc), scsi_prot_sg_count(sc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		set_host_byte(sc, DID_NO_CONNECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		goto out_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) out_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	sc->scsi_done(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  * ->queuecommand can be and usually is called from interrupt context, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165)  * defer the actual submission to a workqueue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	struct tcm_loop_cmd *tl_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	pr_debug("%s() %d:%d:%d:%llu got CDB: 0x%02x scsi_buf_len: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		 __func__, sc->device->host->host_no, sc->device->id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		 sc->device->channel, sc->device->lun, sc->cmnd[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 		 scsi_bufflen(sc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	if (!tl_cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		set_host_byte(sc, DID_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		sc->scsi_done(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	tl_cmd->sc = sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	tl_cmd->sc_cmd_tag = sc->request->tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	queue_work(tcm_loop_workqueue, &tl_cmd->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191)  * Called from SCSI EH process context to issue a LUN_RESET TMR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192)  * to struct scsi_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 			      u64 lun, int task, enum tcm_tmreq_table tmr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	struct se_cmd *se_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	struct se_session *se_sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	struct tcm_loop_nexus *tl_nexus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	struct tcm_loop_cmd *tl_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	int ret = TMR_FUNCTION_FAILED, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	 * Locate the tl_nexus and se_sess pointers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	tl_nexus = tl_tpg->tl_nexus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	if (!tl_nexus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		pr_err("Unable to perform device reset without active I_T Nexus\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	if (!tl_cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	init_completion(&tl_cmd->tmr_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	se_cmd = &tl_cmd->tl_se_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	se_sess = tl_tpg->tl_nexus->se_sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	rc = target_submit_tmr(se_cmd, se_sess, tl_cmd->tl_sense_buf, lun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 			       NULL, tmr, GFP_KERNEL, task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 			       TARGET_SCF_ACK_KREF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		goto release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	wait_for_completion(&tl_cmd->tmr_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	ret = se_cmd->se_tmr_req->response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	target_put_sess_cmd(se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) static int tcm_loop_abort_task(struct scsi_cmnd *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	struct tcm_loop_hba *tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	struct tcm_loop_tpg *tl_tpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	int ret = FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	 * Locate the tcm_loop_hba_t pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 				 sc->request->tag, TMR_ABORT_TASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255)  * Called from SCSI EH process context to issue a LUN_RESET TMR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256)  * to struct scsi_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) static int tcm_loop_device_reset(struct scsi_cmnd *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	struct tcm_loop_hba *tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	struct tcm_loop_tpg *tl_tpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	int ret = FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	 * Locate the tcm_loop_hba_t pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 				 0, TMR_LUN_RESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) static int tcm_loop_target_reset(struct scsi_cmnd *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	struct tcm_loop_hba *tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	struct tcm_loop_tpg *tl_tpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	 * Locate the tcm_loop_hba_t pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	if (!tl_hba) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		pr_err("Unable to perform device reset without active I_T Nexus\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		return FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	 * Locate the tl_tpg pointer from TargetID in sc->device->id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	if (tl_tpg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		return SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	return FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) static struct scsi_host_template tcm_loop_driver_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	.show_info		= tcm_loop_show_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	.proc_name		= "tcm_loopback",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	.name			= "TCM_Loopback",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	.queuecommand		= tcm_loop_queuecommand,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	.change_queue_depth	= scsi_change_queue_depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	.eh_abort_handler = tcm_loop_abort_task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	.eh_device_reset_handler = tcm_loop_device_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	.eh_target_reset_handler = tcm_loop_target_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	.can_queue		= 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	.this_id		= -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	.sg_tablesize		= 256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	.cmd_per_lun		= 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	.max_sectors		= 0xFFFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	.dma_boundary		= PAGE_SIZE - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	.module			= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	.track_queue_depth	= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) static int tcm_loop_driver_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	struct tcm_loop_hba *tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	struct Scsi_Host *sh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	int error, host_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	tl_hba = to_tcm_loop_hba(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	sh = scsi_host_alloc(&tcm_loop_driver_template,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 			sizeof(struct tcm_loop_hba));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	if (!sh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		pr_err("Unable to allocate struct scsi_host\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	tl_hba->sh = sh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	*((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	 * Setup single ID, Channel and LUN for now..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	sh->max_id = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	sh->max_lun = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	sh->max_channel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	sh->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		    SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		    SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	scsi_host_set_prot(sh, host_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	error = scsi_add_host(sh, &tl_hba->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		pr_err("%s: scsi_add_host failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		scsi_host_put(sh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) static int tcm_loop_driver_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	struct tcm_loop_hba *tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	struct Scsi_Host *sh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	tl_hba = to_tcm_loop_hba(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	sh = tl_hba->sh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	scsi_remove_host(sh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	scsi_host_put(sh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) static void tcm_loop_release_adapter(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	kfree(tl_hba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383)  * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	tl_hba->dev.bus = &tcm_loop_lld_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	tl_hba->dev.parent = tcm_loop_primary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	tl_hba->dev.release = &tcm_loop_release_adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	ret = device_register(&tl_hba->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		pr_err("device_register() failed for tl_hba->dev: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404)  * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405)  * tcm_loop SCSI bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) static int tcm_loop_alloc_core_bus(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	tcm_loop_primary = root_device_register("tcm_loop_0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	if (IS_ERR(tcm_loop_primary)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		pr_err("Unable to allocate tcm_loop_primary\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		return PTR_ERR(tcm_loop_primary);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	ret = bus_register(&tcm_loop_lld_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		pr_err("bus_register() failed for tcm_loop_lld_bus\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		goto dev_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	ret = driver_register(&tcm_loop_driverfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		pr_err("driver_register() failed for tcm_loop_driverfs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		goto bus_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	pr_debug("Initialized TCM Loop Core Bus\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) bus_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	bus_unregister(&tcm_loop_lld_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) dev_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	root_device_unregister(tcm_loop_primary);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) static void tcm_loop_release_core_bus(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	driver_unregister(&tcm_loop_driverfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	bus_unregister(&tcm_loop_lld_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	root_device_unregister(tcm_loop_primary);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	pr_debug("Releasing TCM Loop Core BUS\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) static inline struct tcm_loop_tpg *tl_tpg(struct se_portal_group *se_tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	return container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	 * Return the passed NAA identifier for the Target Port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	return &tl_tpg(se_tpg)->tl_hba->tl_wwn_address[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	 * to represent the SCSI Target Port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	return tl_tpg(se_tpg)->tl_tpgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471)  * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472)  * based upon the incoming fabric dependent SCSI Initiator Port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485)  * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486)  * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494)  * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495)  * never be called for TCM_Loop by target_core_fabric_configfs.c code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  * It has been added here as a nop for target_fabric_tf_ops_check()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 						   tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	return tl_tpg->tl_fabric_prot_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 			struct tcm_loop_cmd, tl_se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	return tl_cmd->sc_cmd_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) static int tcm_loop_write_pending(struct se_cmd *se_cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	 * Since Linux/SCSI has already sent down a struct scsi_cmnd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	 * format with transport_generic_map_mem_to_cmd().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	 * We now tell TCM to add this WRITE CDB directly into the TCM storage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	 * object execution queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	target_execute_cmd(se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) static int tcm_loop_queue_data_or_status(const char *func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		struct se_cmd *se_cmd, u8 scsi_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 				struct tcm_loop_cmd, tl_se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	struct scsi_cmnd *sc = tl_cmd->sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	pr_debug("%s() called for scsi_cmnd: %p cdb: 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		 func, sc, sc->cmnd[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	if (se_cmd->sense_buffer &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	   ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	    (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		memcpy(sc->sense_buffer, se_cmd->sense_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 				SCSI_SENSE_BUFFERSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		sc->result = SAM_STAT_CHECK_CONDITION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		set_driver_byte(sc, DRIVER_SENSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		sc->result = scsi_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	set_host_byte(sc, DID_OK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	    (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		scsi_set_resid(sc, se_cmd->residual_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	sc->scsi_done(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	return tcm_loop_queue_data_or_status(__func__, se_cmd, SAM_STAT_GOOD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) static int tcm_loop_queue_status(struct se_cmd *se_cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	return tcm_loop_queue_data_or_status(__func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 					     se_cmd, se_cmd->scsi_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 				struct tcm_loop_cmd, tl_se_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	/* Wake up tcm_loop_issue_tmr(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	complete(&tl_cmd->tmr_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	switch (tl_hba->tl_proto_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	case SCSI_PROTOCOL_SAS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		return "SAS";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	case SCSI_PROTOCOL_FCP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		return "FCP";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	case SCSI_PROTOCOL_ISCSI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		return "iSCSI";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	return "Unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) /* Start items for tcm_loop_port_cit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) static int tcm_loop_port_link(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	struct se_portal_group *se_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	struct se_lun *lun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 				struct tcm_loop_tpg, tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	 * Add Linux/SCSI struct scsi_device by HCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) static void tcm_loop_port_unlink(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	struct se_portal_group *se_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	struct se_lun *se_lun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	struct scsi_device *sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	struct tcm_loop_hba *tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	struct tcm_loop_tpg *tl_tpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	tl_hba = tl_tpg->tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 				se_lun->unpacked_lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	if (!sd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		pr_err("Unable to locate struct scsi_device for %d:%d:%llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		       0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	 * Remove Linux/SCSI struct scsi_device by HCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	scsi_remove_device(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	scsi_device_put(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) /* End items for tcm_loop_port_cit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_show(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		struct config_item *item, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 						   tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_store(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		struct config_item *item, const char *page, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 						   tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	int ret = kstrtoul(page, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	if (val != 0 && val != 1 && val != 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	tl_tpg->tl_fabric_prot_type = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) CONFIGFS_ATTR(tcm_loop_tpg_attrib_, fabric_prot_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	&tcm_loop_tpg_attrib_attr_fabric_prot_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) /* Start items for tcm_loop_nexus_cit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) static int tcm_loop_alloc_sess_cb(struct se_portal_group *se_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 				  struct se_session *se_sess, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 					struct tcm_loop_tpg, tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	tl_tpg->tl_nexus = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) static int tcm_loop_make_nexus(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	struct tcm_loop_tpg *tl_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	struct tcm_loop_nexus *tl_nexus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	if (tl_tpg->tl_nexus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		pr_debug("tl_tpg->tl_nexus already exists\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	tl_nexus = kzalloc(sizeof(*tl_nexus), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	if (!tl_nexus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	tl_nexus->se_sess = target_setup_session(&tl_tpg->tl_se_tpg, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 					TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 					name, tl_nexus, tcm_loop_alloc_sess_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	if (IS_ERR(tl_nexus->se_sess)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		ret = PTR_ERR(tl_nexus->se_sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		kfree(tl_nexus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated %s Initiator Port: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		 tcm_loop_dump_proto_id(tl_hba), name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) static int tcm_loop_drop_nexus(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	struct tcm_loop_tpg *tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	struct se_session *se_sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	struct tcm_loop_nexus *tl_nexus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	tl_nexus = tpg->tl_nexus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	if (!tl_nexus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	se_sess = tl_nexus->se_sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	if (!se_sess)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	if (atomic_read(&tpg->tl_tpg_port_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		pr_err("Unable to remove TCM_Loop I_T Nexus with active TPG port count: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		       atomic_read(&tpg->tl_tpg_port_count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated %s Initiator Port: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		 tcm_loop_dump_proto_id(tpg->tl_hba),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		 tl_nexus->se_sess->se_node_acl->initiatorname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	 * Release the SCSI I_T Nexus to the emulated Target Port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	target_remove_session(se_sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	tpg->tl_nexus = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	kfree(tl_nexus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) /* End items for tcm_loop_nexus_cit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) static ssize_t tcm_loop_tpg_nexus_show(struct config_item *item, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	struct se_portal_group *se_tpg = to_tpg(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 			struct tcm_loop_tpg, tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	struct tcm_loop_nexus *tl_nexus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	tl_nexus = tl_tpg->tl_nexus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	if (!tl_nexus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	ret = snprintf(page, PAGE_SIZE, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		tl_nexus->se_sess->se_node_acl->initiatorname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) static ssize_t tcm_loop_tpg_nexus_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		const char *page, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	struct se_portal_group *se_tpg = to_tpg(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			struct tcm_loop_tpg, tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	 * Shutdown the active I_T nexus if 'NULL' is passed..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	if (!strncmp(page, "NULL", 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		ret = tcm_loop_drop_nexus(tl_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		return (!ret) ? count : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	 * Otherwise make sure the passed virtual Initiator port WWN matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	 * tcm_loop_make_nexus()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	if (strlen(page) >= TL_WWN_ADDR_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		       page, TL_WWN_ADDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	ptr = strstr(i_port, "naa.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	if (ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 			pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			       i_port, tcm_loop_dump_proto_id(tl_hba));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		port_ptr = &i_port[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		goto check_newline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	ptr = strstr(i_port, "fc.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	if (ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 			pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			       i_port, tcm_loop_dump_proto_id(tl_hba));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		port_ptr = &i_port[3]; /* Skip over "fc." */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		goto check_newline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	ptr = strstr(i_port, "iqn.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	if (ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 			pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			       i_port, tcm_loop_dump_proto_id(tl_hba));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		port_ptr = &i_port[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		goto check_newline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	       i_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	 * Clear any trailing newline for the NAA WWN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) check_newline:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	if (i_port[strlen(i_port)-1] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		i_port[strlen(i_port)-1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) static ssize_t tcm_loop_tpg_transport_status_show(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	struct se_portal_group *se_tpg = to_tpg(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			struct tcm_loop_tpg, tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	const char *status = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	ssize_t ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	switch (tl_tpg->tl_transport_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	case TCM_TRANSPORT_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		status = "online";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	case TCM_TRANSPORT_OFFLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		status = "offline";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		ret = snprintf(page, PAGE_SIZE, "%s\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) static ssize_t tcm_loop_tpg_transport_status_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		const char *page, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	struct se_portal_group *se_tpg = to_tpg(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			struct tcm_loop_tpg, tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	if (!strncmp(page, "online", 6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	if (!strncmp(page, "offline", 7)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		if (tl_tpg->tl_nexus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			struct se_session *tl_sess = tl_tpg->tl_nexus->se_sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			core_allocate_nexus_loss_ua(tl_sess->se_node_acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) static ssize_t tcm_loop_tpg_address_show(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 					 char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	struct se_portal_group *se_tpg = to_tpg(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 			struct tcm_loop_tpg, tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	return snprintf(page, PAGE_SIZE, "%d:0:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			tl_hba->sh->host_no, tl_tpg->tl_tpgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) CONFIGFS_ATTR(tcm_loop_tpg_, nexus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) CONFIGFS_ATTR(tcm_loop_tpg_, transport_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) CONFIGFS_ATTR_RO(tcm_loop_tpg_, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	&tcm_loop_tpg_attr_nexus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	&tcm_loop_tpg_attr_transport_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	&tcm_loop_tpg_attr_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) /* Start items for tcm_loop_naa_cit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) static struct se_portal_group *tcm_loop_make_naa_tpg(struct se_wwn *wwn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 						     const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	struct tcm_loop_hba *tl_hba = container_of(wwn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 			struct tcm_loop_hba, tl_hba_wwn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	struct tcm_loop_tpg *tl_tpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	unsigned long tpgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	if (strstr(name, "tpgt_") != name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		pr_err("Unable to locate \"tpgt_#\" directory group\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	if (kstrtoul(name+5, 10, &tpgt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	if (tpgt >= TL_TPGS_PER_HBA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		       tpgt, TL_TPGS_PER_HBA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	tl_tpg->tl_hba = tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	tl_tpg->tl_tpgt = tpgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	 * Register the tl_tpg as a emulated TCM Target Endpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	ret = core_tpg_register(wwn, &tl_tpg->tl_se_tpg, tl_hba->tl_proto_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s Target Port %s,t,0x%04lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		 tcm_loop_dump_proto_id(tl_hba),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		 config_item_name(&wwn->wwn_group.cg_item), tpgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	return &tl_tpg->tl_se_tpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) static void tcm_loop_drop_naa_tpg(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	struct se_portal_group *se_tpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	struct se_wwn *wwn = se_tpg->se_tpg_wwn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 				struct tcm_loop_tpg, tl_se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	struct tcm_loop_hba *tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	unsigned short tpgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	tl_hba = tl_tpg->tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	tpgt = tl_tpg->tl_tpgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	 * Release the I_T Nexus for the Virtual target link if present
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	tcm_loop_drop_nexus(tl_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	 * Deregister the tl_tpg as a emulated TCM Target Endpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	core_tpg_deregister(se_tpg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	tl_tpg->tl_hba = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	tl_tpg->tl_tpgt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s Target Port %s,t,0x%04x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		 tcm_loop_dump_proto_id(tl_hba),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		 config_item_name(&wwn->wwn_group.cg_item), tpgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) /* End items for tcm_loop_naa_cit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) /* Start items for tcm_loop_cit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) static struct se_wwn *tcm_loop_make_scsi_hba(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	struct target_fabric_configfs *tf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	struct config_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	struct tcm_loop_hba *tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	struct Scsi_Host *sh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	int ret, off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	tl_hba = kzalloc(sizeof(*tl_hba), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (!tl_hba)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	 * Determine the emulated Protocol Identifier and Target Port Name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	 * based on the incoming configfs directory name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	ptr = strstr(name, "naa.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	if (ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		goto check_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	ptr = strstr(name, "fc.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	if (ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		off = 3; /* Skip over "fc." */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		goto check_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	ptr = strstr(name, "iqn.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	if (!ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		pr_err("Unable to locate prefix for emulated Target Port: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		       name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) check_len:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	if (strlen(name) >= TL_WWN_ADDR_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		pr_err("Emulated NAA %s Address: %s, exceeds max: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		       name, tcm_loop_dump_proto_id(tl_hba), TL_WWN_ADDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	 * Call device_register(tl_hba->dev) to register the emulated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	 * device_register() callbacks in tcm_loop_driver_probe()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	sh = tl_hba->sh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	tcm_loop_hba_no_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	return &tl_hba->tl_hba_wwn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	kfree(tl_hba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) static void tcm_loop_drop_scsi_hba(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	struct se_wwn *wwn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	struct tcm_loop_hba *tl_hba = container_of(wwn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 				struct tcm_loop_hba, tl_hba_wwn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		 tcm_loop_dump_proto_id(tl_hba), tl_hba->tl_wwn_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		 tl_hba->sh->host_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	 * Call device_unregister() on the original tl_hba->dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	 * release *tl_hba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	device_unregister(&tl_hba->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) /* Start items for tcm_loop_cit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) static ssize_t tcm_loop_wwn_version_show(struct config_item *item, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) CONFIGFS_ATTR_RO(tcm_loop_wwn_, version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	&tcm_loop_wwn_attr_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) /* End items for tcm_loop_cit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) static const struct target_core_fabric_ops loop_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	.module				= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	.fabric_name			= "loopback",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	.tpg_get_wwn			= tcm_loop_get_endpoint_wwn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	.tpg_get_tag			= tcm_loop_get_tag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	.tpg_check_demo_mode		= tcm_loop_check_demo_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	.tpg_check_demo_mode_cache	= tcm_loop_check_demo_mode_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	.tpg_check_demo_mode_write_protect =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 				tcm_loop_check_demo_mode_write_protect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	.tpg_check_prod_mode_write_protect =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 				tcm_loop_check_prod_mode_write_protect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	.tpg_check_prot_fabric_only	= tcm_loop_check_prot_fabric_only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	.tpg_get_inst_index		= tcm_loop_get_inst_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	.check_stop_free		= tcm_loop_check_stop_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	.release_cmd			= tcm_loop_release_cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	.sess_get_index			= tcm_loop_sess_get_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	.write_pending			= tcm_loop_write_pending,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	.set_default_node_attributes	= tcm_loop_set_default_node_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	.get_cmd_state			= tcm_loop_get_cmd_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	.queue_data_in			= tcm_loop_queue_data_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	.queue_status			= tcm_loop_queue_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	.queue_tm_rsp			= tcm_loop_queue_tm_rsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	.aborted_task			= tcm_loop_aborted_task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	.fabric_make_wwn		= tcm_loop_make_scsi_hba,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	.fabric_drop_wwn		= tcm_loop_drop_scsi_hba,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	.fabric_make_tpg		= tcm_loop_make_naa_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	.fabric_drop_tpg		= tcm_loop_drop_naa_tpg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	.fabric_post_link		= tcm_loop_port_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	.fabric_pre_unlink		= tcm_loop_port_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	.tfc_wwn_attrs			= tcm_loop_wwn_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	.tfc_tpg_base_attrs		= tcm_loop_tpg_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	.tfc_tpg_attrib_attrs		= tcm_loop_tpg_attrib_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) static int __init tcm_loop_fabric_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	if (!tcm_loop_workqueue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 				sizeof(struct tcm_loop_cmd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 				__alignof__(struct tcm_loop_cmd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 				0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	if (!tcm_loop_cmd_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		pr_debug("kmem_cache_create() for tcm_loop_cmd_cache failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		goto out_destroy_workqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	ret = tcm_loop_alloc_core_bus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		goto out_destroy_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	ret = target_register_template(&loop_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		goto out_release_core_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) out_release_core_bus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	tcm_loop_release_core_bus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) out_destroy_cache:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	kmem_cache_destroy(tcm_loop_cmd_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) out_destroy_workqueue:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	destroy_workqueue(tcm_loop_workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) static void __exit tcm_loop_fabric_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	target_unregister_template(&loop_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	tcm_loop_release_core_bus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	kmem_cache_destroy(tcm_loop_cmd_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	destroy_workqueue(tcm_loop_workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) module_init(tcm_loop_fabric_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) module_exit(tcm_loop_fabric_exit);