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