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