blob: 51f0c895c6a58fd1e93badf13a52e5eae4b64877 [file] [log] [blame]
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001/*******************************************************************************
2 *
3 * This file contains the Linux/SCSI LLD virtual SCSI initiator driver
4 * for emulated SAS initiator ports
5 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07006 * © Copyright 2011-2013 Datera, Inc.
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07007 *
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 Bellinger3703b2c2011-03-18 15:39:17 -070034
35#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050036#include <target/target_core_fabric.h>
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070037#include <target/target_core_fabric_configfs.h>
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070038#include <target/target_core_configfs.h>
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070039
40#include "tcm_loop.h"
41
42#define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev)
43
Christoph Hellwig9ac89282015-04-08 20:01:35 +020044static const struct target_core_fabric_ops loop_ops;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070045
Christoph Hellwigafe2cb72012-02-02 17:04:41 -050046static struct workqueue_struct *tcm_loop_workqueue;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070047static struct kmem_cache *tcm_loop_cmd_cache;
48
49static int tcm_loop_hba_no_cnt;
50
Christoph Hellwig167864542012-02-02 17:04:42 -050051static int tcm_loop_queue_status(struct se_cmd *se_cmd);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070052
53/*
54 * Called from struct target_core_fabric_ops->check_stop_free()
55 */
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -070056static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070057{
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 Groverc8e31f22012-01-19 13:39:17 -080063 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -070064 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070065 /*
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 Hellwig82f1c8a2011-09-13 23:09:01 +020069 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -070070 return 1;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070071}
72
Christoph Hellwig35462972011-05-31 23:56:57 -040073static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070074{
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 Viro8946b072013-03-31 02:01:55 -040081static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070082{
Al Viro8946b072013-03-31 02:01:55 -040083 seq_printf(m, "tcm_loop_proc_info()\n");
84 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070085}
86
87static int tcm_loop_driver_probe(struct device *);
88static int tcm_loop_driver_remove(struct device *);
89
90static int pseudo_lld_bus_match(struct device *dev,
91 struct device_driver *dev_driver)
92{
93 return 1;
94}
95
96static 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
103static 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 Hellwig6e1a27b2015-03-26 12:27:32 +0100110static struct device *tcm_loop_primary;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700111
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500112static void tcm_loop_submission_work(struct work_struct *work)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700113{
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500114 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 Bellinger3703b2c2011-03-18 15:39:17 -0700119 struct tcm_loop_hba *tl_hba;
120 struct tcm_loop_tpg *tl_tpg;
Christoph Hellwig167864542012-02-02 17:04:42 -0500121 struct scatterlist *sgl_bidi = NULL;
Sagi Grimberge2a4f552014-06-11 12:09:59 +0300122 u32 sgl_bidi_count = 0, transfer_length;
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700123 int rc;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500124
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700125 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
126 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500127
Nicholas Bellinger0a020432011-10-10 19:44:05 -0700128 /*
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 Hellwigf872c9f2012-02-02 17:04:40 -0500134 goto out_done;
Nicholas Bellinger0a020432011-10-10 19:44:05 -0700135 }
Hannes Reineckefb2b2842013-10-16 09:12:53 +0200136 if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) {
137 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
138 goto out_done;
139 }
Hannes Reinecke506787a2014-11-26 14:58:57 +0100140 tl_nexus = tl_tpg->tl_nexus;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500141 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 Bellinger3703b2c2011-03-18 15:39:17 -0700146 }
Christoph Hellwig167864542012-02-02 17:04:42 -0500147 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 Hellwigf872c9f2012-02-02 17:04:40 -0500152 se_cmd->se_cmd_flags |= SCF_BIDI;
153
Christoph Hellwig167864542012-02-02 17:04:42 -0500154 }
Sagi Grimbergb5b8e292014-02-19 17:50:17 +0200155
Sagi Grimberge2a4f552014-06-11 12:09:59 +0300156 transfer_length = scsi_transfer_length(sc);
157 if (!scsi_prot_sg_count(sc) &&
158 scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) {
Sagi Grimbergb5b8e292014-02-19 17:50:17 +0200159 se_cmd->prot_pto = true;
Sagi Grimberge2a4f552014-06-11 12:09:59 +0300160 /*
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 Grimbergb5b8e292014-02-19 17:50:17 +0200167
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700168 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 Hellwig68d81f42014-11-24 07:07:25 -0800170 transfer_length, TCM_SIMPLE_TAG,
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700171 sc->sc_data_direction, 0,
172 scsi_sglist(sc), scsi_sg_count(sc),
Nicholas Bellinger59dedde2013-12-23 20:39:23 +0000173 sgl_bidi, sgl_bidi_count,
174 scsi_prot_sglist(sc), scsi_prot_sg_count(sc));
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700175 if (rc < 0) {
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500176 set_host_byte(sc, DID_NO_CONNECT);
177 goto out_done;
178 }
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500179 return;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500180
181out_done:
Nicholas Bellingerb43f1882014-06-17 22:23:03 +0000182 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500183 sc->scsi_done(sc);
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500184 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 */
191static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
192{
193 struct tcm_loop_cmd *tl_cmd;
194
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200195 pr_debug("tcm_loop_queuecommand() %d:%d:%d:%llu got CDB: 0x%02x"
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500196 " 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 Reinecke6375f892014-10-02 09:30:55 +0200209 tl_cmd->sc_cmd_tag = sc->request->tag;
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500210 INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
211 queue_work(tcm_loop_workqueue, &tl_cmd->work);
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500212 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700213}
214
215/*
216 * Called from SCSI EH process context to issue a LUN_RESET TMR
217 * to struct scsi_device
218 */
Hannes Reineckea314d702013-10-16 09:12:54 +0200219static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg,
Hannes Reinecke969871c2013-10-16 09:12:55 +0200220 int lun, int task, enum tcm_tmreq_table tmr)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700221{
222 struct se_cmd *se_cmd = NULL;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700223 struct se_session *se_sess;
Hannes Reineckea314d702013-10-16 09:12:54 +0200224 struct se_portal_group *se_tpg;
Hannes Reinecke506787a2014-11-26 14:58:57 +0100225 struct tcm_loop_nexus *tl_nexus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700226 struct tcm_loop_cmd *tl_cmd = NULL;
Hannes Reineckea314d702013-10-16 09:12:54 +0200227 struct tcm_loop_tmr *tl_tmr = NULL;
228 int ret = TMR_FUNCTION_FAILED, rc;
229
Hannes Reinecke506787a2014-11-26 14:58:57 +0100230 /*
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 Reineckea314d702013-10-16 09:12:54 +0200240 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 Reinecke506787a2014-11-26 14:58:57 +0100255 se_sess = tl_tpg->tl_nexus->se_sess;
Hannes Reineckea314d702013-10-16 09:12:54 +0200256 /*
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 Hellwig68d81f42014-11-24 07:07:25 -0800260 DMA_NONE, TCM_SIMPLE_TAG,
Hannes Reineckea314d702013-10-16 09:12:54 +0200261 &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 Reinecke969871c2013-10-16 09:12:55 +0200267 if (tmr == TMR_ABORT_TASK)
268 se_cmd->se_tmr_req->ref_task_tag = task;
269
Hannes Reineckea314d702013-10-16 09:12:54 +0200270 /*
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;
288release:
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 Reinecke969871c2013-10-16 09:12:55 +0200297static int tcm_loop_abort_task(struct scsi_cmnd *sc)
298{
299 struct tcm_loop_hba *tl_hba;
Hannes Reinecke969871c2013-10-16 09:12:55 +0200300 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 Reinecke969871c2013-10-16 09:12:55 +0200307 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
Hannes Reinecke506787a2014-11-26 14:58:57 +0100308 ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
Hannes Reinecke6375f892014-10-02 09:30:55 +0200309 sc->request->tag, TMR_ABORT_TASK);
Hannes Reinecke969871c2013-10-16 09:12:55 +0200310 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
311}
312
Hannes Reineckea314d702013-10-16 09:12:54 +0200313/*
314 * Called from SCSI EH process context to issue a LUN_RESET TMR
315 * to struct scsi_device
316 */
317static int tcm_loop_device_reset(struct scsi_cmnd *sc)
318{
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700319 struct tcm_loop_hba *tl_hba;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700320 struct tcm_loop_tpg *tl_tpg;
Hannes Reineckea314d702013-10-16 09:12:54 +0200321 int ret = FAILED;
322
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700323 /*
324 * Locate the tcm_loop_hba_t pointer
325 */
326 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700327 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
Hannes Reinecke506787a2014-11-26 14:58:57 +0100328
329 ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
Hannes Reinecke969871c2013-10-16 09:12:55 +0200330 0, TMR_LUN_RESET);
Hannes Reineckea314d702013-10-16 09:12:54 +0200331 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700332}
333
Hannes Reinecke8f4a1fb2013-10-16 09:12:56 +0200334static 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 Bellinger3703b2c2011-03-18 15:39:17 -0700359static 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 Bellinger3703b2c2011-03-18 15:39:17 -0700365static struct scsi_host_template tcm_loop_driver_template = {
Al Viro8946b072013-03-31 02:01:55 -0400366 .show_info = tcm_loop_show_info,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700367 .proc_name = "tcm_loopback",
368 .name = "TCM_Loopback",
369 .queuecommand = tcm_loop_queuecommand,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100370 .change_queue_depth = scsi_change_queue_depth,
Hannes Reinecke969871c2013-10-16 09:12:55 +0200371 .eh_abort_handler = tcm_loop_abort_task,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700372 .eh_device_reset_handler = tcm_loop_device_reset,
Hannes Reinecke8f4a1fb2013-10-16 09:12:56 +0200373 .eh_target_reset_handler = tcm_loop_target_reset,
Christoph Hellwig2e88efd2011-11-29 03:20:41 -0500374 .can_queue = 1024,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700375 .this_id = -1,
Christoph Hellwig2e88efd2011-11-29 03:20:41 -0500376 .sg_tablesize = 256,
377 .cmd_per_lun = 1024,
378 .max_sectors = 0xFFFF,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700379 .use_clustering = DISABLE_CLUSTERING,
380 .slave_alloc = tcm_loop_slave_alloc,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700381 .module = THIS_MODULE,
Christoph Hellwig2ecb2042014-11-03 14:09:02 +0100382 .use_blk_tags = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100383 .track_queue_depth = 1,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700384};
385
386static int tcm_loop_driver_probe(struct device *dev)
387{
388 struct tcm_loop_hba *tl_hba;
389 struct Scsi_Host *sh;
Nicholas Bellinger59dedde2013-12-23 20:39:23 +0000390 int error, host_prot;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700391
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 Grover6708bb22011-06-08 10:36:43 -0700397 pr_err("Unable to allocate struct scsi_host\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700398 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;
412 sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN;
413
Nicholas Bellinger59dedde2013-12-23 20:39:23 +0000414 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 Bellinger3703b2c2011-03-18 15:39:17 -0700421 error = scsi_add_host(sh, &tl_hba->dev);
422 if (error) {
Andy Grover6708bb22011-06-08 10:36:43 -0700423 pr_err("%s: scsi_add_host failed\n", __func__);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700424 scsi_host_put(sh);
425 return -ENODEV;
426 }
427 return 0;
428}
429
430static 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
443static 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 */
453static 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 Grover6708bb22011-06-08 10:36:43 -0700464 pr_err("device_register() failed for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700465 " 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 */
476static 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 Grover6708bb22011-06-08 10:36:43 -0700482 pr_err("Unable to allocate tcm_loop_primary\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700483 return PTR_ERR(tcm_loop_primary);
484 }
485
486 ret = bus_register(&tcm_loop_lld_bus);
487 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700488 pr_err("bus_register() failed for tcm_loop_lld_bus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700489 goto dev_unreg;
490 }
491
492 ret = driver_register(&tcm_loop_driverfs);
493 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700494 pr_err("driver_register() failed for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700495 "tcm_loop_driverfs\n");
496 goto bus_unreg;
497 }
498
Andy Grover6708bb22011-06-08 10:36:43 -0700499 pr_debug("Initialized TCM Loop Core Bus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700500 return ret;
501
502bus_unreg:
503 bus_unregister(&tcm_loop_lld_bus);
504dev_unreg:
505 root_device_unregister(tcm_loop_primary);
506 return ret;
507}
508
509static 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 Grover6708bb22011-06-08 10:36:43 -0700515 pr_debug("Releasing TCM Loop Core BUS\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700516}
517
518static char *tcm_loop_get_fabric_name(void)
519{
520 return "loopback";
521}
522
523static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
524{
Jörn Engel8359cf42011-11-24 02:05:51 +0100525 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700526 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
527 /*
528 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
529 * time based on the protocol dependent prefix of the passed configfs group.
530 *
531 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
532 * ProtocolID using target_core_fabric_lib.c symbols.
533 */
534 switch (tl_hba->tl_proto_id) {
535 case SCSI_PROTOCOL_SAS:
536 return sas_get_fabric_proto_ident(se_tpg);
537 case SCSI_PROTOCOL_FCP:
538 return fc_get_fabric_proto_ident(se_tpg);
539 case SCSI_PROTOCOL_ISCSI:
540 return iscsi_get_fabric_proto_ident(se_tpg);
541 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700542 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700543 " SAS emulation\n", tl_hba->tl_proto_id);
544 break;
545 }
546
547 return sas_get_fabric_proto_ident(se_tpg);
548}
549
550static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
551{
Jörn Engel8359cf42011-11-24 02:05:51 +0100552 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700553 /*
554 * Return the passed NAA identifier for the SAS Target Port
555 */
556 return &tl_tpg->tl_hba->tl_wwn_address[0];
557}
558
559static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
560{
Jörn Engel8359cf42011-11-24 02:05:51 +0100561 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700562 /*
563 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
564 * to represent the SCSI Target Port.
565 */
566 return tl_tpg->tl_tpgt;
567}
568
569static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
570{
571 return 1;
572}
573
574static u32 tcm_loop_get_pr_transport_id(
575 struct se_portal_group *se_tpg,
576 struct se_node_acl *se_nacl,
577 struct t10_pr_registration *pr_reg,
578 int *format_code,
579 unsigned char *buf)
580{
Jörn Engel8359cf42011-11-24 02:05:51 +0100581 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700582 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
583
584 switch (tl_hba->tl_proto_id) {
585 case SCSI_PROTOCOL_SAS:
586 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
587 format_code, buf);
588 case SCSI_PROTOCOL_FCP:
589 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
590 format_code, buf);
591 case SCSI_PROTOCOL_ISCSI:
592 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
593 format_code, buf);
594 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700595 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700596 " SAS emulation\n", tl_hba->tl_proto_id);
597 break;
598 }
599
600 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
601 format_code, buf);
602}
603
604static u32 tcm_loop_get_pr_transport_id_len(
605 struct se_portal_group *se_tpg,
606 struct se_node_acl *se_nacl,
607 struct t10_pr_registration *pr_reg,
608 int *format_code)
609{
Jörn Engel8359cf42011-11-24 02:05:51 +0100610 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700611 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
612
613 switch (tl_hba->tl_proto_id) {
614 case SCSI_PROTOCOL_SAS:
615 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
616 format_code);
617 case SCSI_PROTOCOL_FCP:
618 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
619 format_code);
620 case SCSI_PROTOCOL_ISCSI:
621 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
622 format_code);
623 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700624 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700625 " SAS emulation\n", tl_hba->tl_proto_id);
626 break;
627 }
628
629 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
630 format_code);
631}
632
633/*
634 * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
635 * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
636 */
637static char *tcm_loop_parse_pr_out_transport_id(
638 struct se_portal_group *se_tpg,
639 const char *buf,
640 u32 *out_tid_len,
641 char **port_nexus_ptr)
642{
Jörn Engel8359cf42011-11-24 02:05:51 +0100643 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700644 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
645
646 switch (tl_hba->tl_proto_id) {
647 case SCSI_PROTOCOL_SAS:
648 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
649 port_nexus_ptr);
650 case SCSI_PROTOCOL_FCP:
651 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
652 port_nexus_ptr);
653 case SCSI_PROTOCOL_ISCSI:
654 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
655 port_nexus_ptr);
656 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700657 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700658 " SAS emulation\n", tl_hba->tl_proto_id);
659 break;
660 }
661
662 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
663 port_nexus_ptr);
664}
665
666/*
667 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
668 * based upon the incoming fabric dependent SCSI Initiator Port
669 */
670static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
671{
672 return 1;
673}
674
675static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
676{
677 return 0;
678}
679
680/*
681 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
682 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
683 */
684static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
685{
686 return 0;
687}
688
689/*
690 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
691 * never be called for TCM_Loop by target_core_fabric_configfs.c code.
692 * It has been added here as a nop for target_fabric_tf_ops_check()
693 */
694static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
695{
696 return 0;
697}
698
Nicholas Bellinger436f4a02015-02-08 12:31:39 -0800699static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg)
700{
701 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
702 tl_se_tpg);
703 return tl_tpg->tl_fabric_prot_type;
704}
705
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700706static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
707 struct se_portal_group *se_tpg)
708{
709 struct tcm_loop_nacl *tl_nacl;
710
711 tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
712 if (!tl_nacl) {
Andy Grover6708bb22011-06-08 10:36:43 -0700713 pr_err("Unable to allocate struct tcm_loop_nacl\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700714 return NULL;
715 }
716
717 return &tl_nacl->se_node_acl;
718}
719
720static void tcm_loop_tpg_release_fabric_acl(
721 struct se_portal_group *se_tpg,
722 struct se_node_acl *se_nacl)
723{
724 struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
725 struct tcm_loop_nacl, se_node_acl);
726
727 kfree(tl_nacl);
728}
729
730static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
731{
732 return 1;
733}
734
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700735static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
736{
737 return 1;
738}
739
740static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
741{
742 return;
743}
744
745static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
746{
Hannes Reinecke969871c2013-10-16 09:12:55 +0200747 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
748 struct tcm_loop_cmd, tl_se_cmd);
749
750 return tl_cmd->sc_cmd_tag;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700751}
752
753static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
754{
755 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
756 struct tcm_loop_cmd, tl_se_cmd);
757
758 return tl_cmd->sc_cmd_state;
759}
760
761static int tcm_loop_shutdown_session(struct se_session *se_sess)
762{
763 return 0;
764}
765
766static void tcm_loop_close_session(struct se_session *se_sess)
767{
768 return;
769};
770
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700771static int tcm_loop_write_pending(struct se_cmd *se_cmd)
772{
773 /*
774 * Since Linux/SCSI has already sent down a struct scsi_cmnd
775 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
776 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
777 * format with transport_generic_map_mem_to_cmd().
778 *
779 * We now tell TCM to add this WRITE CDB directly into the TCM storage
780 * object execution queue.
781 */
Christoph Hellwig70baf0a2012-07-08 15:58:39 -0400782 target_execute_cmd(se_cmd);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700783 return 0;
784}
785
786static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
787{
788 return 0;
789}
790
791static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
792{
793 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
794 struct tcm_loop_cmd, tl_se_cmd);
795 struct scsi_cmnd *sc = tl_cmd->sc;
796
Andy Grover6708bb22011-06-08 10:36:43 -0700797 pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700798 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
799
800 sc->result = SAM_STAT_GOOD;
801 set_host_byte(sc, DID_OK);
Roland Dreier6cf3fa62012-02-14 15:30:31 -0800802 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
803 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
804 scsi_set_resid(sc, se_cmd->residual_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700805 sc->scsi_done(sc);
806 return 0;
807}
808
809static int tcm_loop_queue_status(struct se_cmd *se_cmd)
810{
811 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
812 struct tcm_loop_cmd, tl_se_cmd);
813 struct scsi_cmnd *sc = tl_cmd->sc;
814
Andy Grover6708bb22011-06-08 10:36:43 -0700815 pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700816 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
817
818 if (se_cmd->sense_buffer &&
819 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
820 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
821
Andy Grover5951146d2011-07-19 10:26:37 +0000822 memcpy(sc->sense_buffer, se_cmd->sense_buffer,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700823 SCSI_SENSE_BUFFERSIZE);
824 sc->result = SAM_STAT_CHECK_CONDITION;
825 set_driver_byte(sc, DRIVER_SENSE);
826 } else
827 sc->result = se_cmd->scsi_status;
828
829 set_host_byte(sc, DID_OK);
Roland Dreier6cf3fa62012-02-14 15:30:31 -0800830 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
831 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
832 scsi_set_resid(sc, se_cmd->residual_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700833 sc->scsi_done(sc);
834 return 0;
835}
836
Joern Engelb79fafa2013-07-03 11:22:17 -0400837static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700838{
839 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
840 struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
841 /*
842 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
843 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
844 */
845 atomic_set(&tl_tmr->tmr_complete, 1);
846 wake_up(&tl_tmr->tl_tmr_wait);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700847}
848
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700849static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
850{
851 return;
852}
853
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700854static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
855{
856 switch (tl_hba->tl_proto_id) {
857 case SCSI_PROTOCOL_SAS:
858 return "SAS";
859 case SCSI_PROTOCOL_FCP:
860 return "FCP";
861 case SCSI_PROTOCOL_ISCSI:
862 return "iSCSI";
863 default:
864 break;
865 }
866
867 return "Unknown";
868}
869
870/* Start items for tcm_loop_port_cit */
871
872static int tcm_loop_port_link(
873 struct se_portal_group *se_tpg,
874 struct se_lun *lun)
875{
876 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
877 struct tcm_loop_tpg, tl_se_tpg);
878 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
879
Joern Engel33940d02014-09-16 16:23:12 -0400880 atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700881 /*
882 * Add Linux/SCSI struct scsi_device by HCTL
883 */
884 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
885
Andy Grover6708bb22011-06-08 10:36:43 -0700886 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700887 return 0;
888}
889
890static void tcm_loop_port_unlink(
891 struct se_portal_group *se_tpg,
892 struct se_lun *se_lun)
893{
894 struct scsi_device *sd;
895 struct tcm_loop_hba *tl_hba;
896 struct tcm_loop_tpg *tl_tpg;
897
898 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
899 tl_hba = tl_tpg->tl_hba;
900
901 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
902 se_lun->unpacked_lun);
903 if (!sd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700904 pr_err("Unable to locate struct scsi_device for %d:%d:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700905 "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
906 return;
907 }
908 /*
909 * Remove Linux/SCSI struct scsi_device by HCTL
910 */
911 scsi_remove_device(sd);
912 scsi_device_put(sd);
913
Joern Engel33940d02014-09-16 16:23:12 -0400914 atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700915
Andy Grover6708bb22011-06-08 10:36:43 -0700916 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700917}
918
919/* End items for tcm_loop_port_cit */
920
Nicholas Bellinger436f4a02015-02-08 12:31:39 -0800921static ssize_t tcm_loop_tpg_attrib_show_fabric_prot_type(
922 struct se_portal_group *se_tpg,
923 char *page)
924{
925 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
926 tl_se_tpg);
927
928 return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type);
929}
930
931static ssize_t tcm_loop_tpg_attrib_store_fabric_prot_type(
932 struct se_portal_group *se_tpg,
933 const char *page,
934 size_t count)
935{
936 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
937 tl_se_tpg);
938 unsigned long val;
939 int ret = kstrtoul(page, 0, &val);
940
941 if (ret) {
942 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
943 return ret;
944 }
945 if (val != 0 && val != 1 && val != 3) {
946 pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
947 return -EINVAL;
948 }
949 tl_tpg->tl_fabric_prot_type = val;
950
951 return count;
952}
953
954TF_TPG_ATTRIB_ATTR(tcm_loop, fabric_prot_type, S_IRUGO | S_IWUSR);
955
956static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = {
957 &tcm_loop_tpg_attrib_fabric_prot_type.attr,
958 NULL,
959};
960
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700961/* Start items for tcm_loop_nexus_cit */
962
963static int tcm_loop_make_nexus(
964 struct tcm_loop_tpg *tl_tpg,
965 const char *name)
966{
967 struct se_portal_group *se_tpg;
968 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
969 struct tcm_loop_nexus *tl_nexus;
Dan Carpenter552523d2011-06-15 09:41:33 -0700970 int ret = -ENOMEM;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700971
Hannes Reinecke506787a2014-11-26 14:58:57 +0100972 if (tl_tpg->tl_nexus) {
973 pr_debug("tl_tpg->tl_nexus already exists\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700974 return -EEXIST;
975 }
976 se_tpg = &tl_tpg->tl_se_tpg;
977
978 tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
979 if (!tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700980 pr_err("Unable to allocate struct tcm_loop_nexus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700981 return -ENOMEM;
982 }
983 /*
984 * Initialize the struct se_session pointer
985 */
Nicholas Bellinger436f4a02015-02-08 12:31:39 -0800986 tl_nexus->se_sess = transport_init_session(
987 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS);
Dan Carpenter552523d2011-06-15 09:41:33 -0700988 if (IS_ERR(tl_nexus->se_sess)) {
989 ret = PTR_ERR(tl_nexus->se_sess);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700990 goto out;
Dan Carpenter552523d2011-06-15 09:41:33 -0700991 }
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700992 /*
993 * Since we are running in 'demo mode' this call with generate a
994 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
995 * Initiator port name of the passed configfs group 'name'.
996 */
997 tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
998 se_tpg, (unsigned char *)name);
999 if (!tl_nexus->se_sess->se_node_acl) {
1000 transport_free_session(tl_nexus->se_sess);
1001 goto out;
1002 }
Bart Van Assche2f450cc2015-02-12 11:48:49 +01001003 /* Now, register the SAS I_T Nexus as active. */
1004 transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
Andy Grover5951146d2011-07-19 10:26:37 +00001005 tl_nexus->se_sess, tl_nexus);
Hannes Reinecke506787a2014-11-26 14:58:57 +01001006 tl_tpg->tl_nexus = tl_nexus;
Andy Grover6708bb22011-06-08 10:36:43 -07001007 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001008 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
1009 name);
1010 return 0;
1011
1012out:
1013 kfree(tl_nexus);
Dan Carpenter552523d2011-06-15 09:41:33 -07001014 return ret;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001015}
1016
1017static int tcm_loop_drop_nexus(
1018 struct tcm_loop_tpg *tpg)
1019{
1020 struct se_session *se_sess;
1021 struct tcm_loop_nexus *tl_nexus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001022
Hannes Reinecke506787a2014-11-26 14:58:57 +01001023 tl_nexus = tpg->tl_nexus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001024 if (!tl_nexus)
1025 return -ENODEV;
1026
1027 se_sess = tl_nexus->se_sess;
1028 if (!se_sess)
1029 return -ENODEV;
1030
1031 if (atomic_read(&tpg->tl_tpg_port_count)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001032 pr_err("Unable to remove TCM_Loop I_T Nexus with"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001033 " active TPG port count: %d\n",
1034 atomic_read(&tpg->tl_tpg_port_count));
1035 return -EPERM;
1036 }
1037
Andy Grover6708bb22011-06-08 10:36:43 -07001038 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
Hannes Reinecke506787a2014-11-26 14:58:57 +01001039 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tpg->tl_hba),
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001040 tl_nexus->se_sess->se_node_acl->initiatorname);
1041 /*
1042 * Release the SCSI I_T Nexus to the emulated SAS Target Port
1043 */
1044 transport_deregister_session(tl_nexus->se_sess);
Hannes Reinecke506787a2014-11-26 14:58:57 +01001045 tpg->tl_nexus = NULL;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001046 kfree(tl_nexus);
1047 return 0;
1048}
1049
1050/* End items for tcm_loop_nexus_cit */
1051
1052static ssize_t tcm_loop_tpg_show_nexus(
1053 struct se_portal_group *se_tpg,
1054 char *page)
1055{
1056 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1057 struct tcm_loop_tpg, tl_se_tpg);
1058 struct tcm_loop_nexus *tl_nexus;
1059 ssize_t ret;
1060
Hannes Reinecke506787a2014-11-26 14:58:57 +01001061 tl_nexus = tl_tpg->tl_nexus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001062 if (!tl_nexus)
1063 return -ENODEV;
1064
1065 ret = snprintf(page, PAGE_SIZE, "%s\n",
1066 tl_nexus->se_sess->se_node_acl->initiatorname);
1067
1068 return ret;
1069}
1070
1071static ssize_t tcm_loop_tpg_store_nexus(
1072 struct se_portal_group *se_tpg,
1073 const char *page,
1074 size_t count)
1075{
1076 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1077 struct tcm_loop_tpg, tl_se_tpg);
1078 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
1079 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
1080 int ret;
1081 /*
1082 * Shutdown the active I_T nexus if 'NULL' is passed..
1083 */
1084 if (!strncmp(page, "NULL", 4)) {
1085 ret = tcm_loop_drop_nexus(tl_tpg);
1086 return (!ret) ? count : ret;
1087 }
1088 /*
1089 * Otherwise make sure the passed virtual Initiator port WWN matches
1090 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
1091 * tcm_loop_make_nexus()
1092 */
Dan Carpenter60d645a2011-06-15 10:03:05 -07001093 if (strlen(page) >= TL_WWN_ADDR_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001094 pr_err("Emulated NAA Sas Address: %s, exceeds"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001095 " max: %d\n", page, TL_WWN_ADDR_LEN);
1096 return -EINVAL;
1097 }
1098 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
1099
1100 ptr = strstr(i_port, "naa.");
1101 if (ptr) {
1102 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
Andy Grover6708bb22011-06-08 10:36:43 -07001103 pr_err("Passed SAS Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001104 " match target port protoid: %s\n", i_port,
1105 tcm_loop_dump_proto_id(tl_hba));
1106 return -EINVAL;
1107 }
1108 port_ptr = &i_port[0];
1109 goto check_newline;
1110 }
1111 ptr = strstr(i_port, "fc.");
1112 if (ptr) {
1113 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
Andy Grover6708bb22011-06-08 10:36:43 -07001114 pr_err("Passed FCP Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001115 " match target port protoid: %s\n", i_port,
1116 tcm_loop_dump_proto_id(tl_hba));
1117 return -EINVAL;
1118 }
1119 port_ptr = &i_port[3]; /* Skip over "fc." */
1120 goto check_newline;
1121 }
1122 ptr = strstr(i_port, "iqn.");
1123 if (ptr) {
1124 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
Andy Grover6708bb22011-06-08 10:36:43 -07001125 pr_err("Passed iSCSI Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001126 " match target port protoid: %s\n", i_port,
1127 tcm_loop_dump_proto_id(tl_hba));
1128 return -EINVAL;
1129 }
1130 port_ptr = &i_port[0];
1131 goto check_newline;
1132 }
Andy Grover6708bb22011-06-08 10:36:43 -07001133 pr_err("Unable to locate prefix for emulated Initiator Port:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001134 " %s\n", i_port);
1135 return -EINVAL;
1136 /*
1137 * Clear any trailing newline for the NAA WWN
1138 */
1139check_newline:
1140 if (i_port[strlen(i_port)-1] == '\n')
1141 i_port[strlen(i_port)-1] = '\0';
1142
1143 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
1144 if (ret < 0)
1145 return ret;
1146
1147 return count;
1148}
1149
1150TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
1151
Hannes Reineckefb2b2842013-10-16 09:12:53 +02001152static ssize_t tcm_loop_tpg_show_transport_status(
1153 struct se_portal_group *se_tpg,
1154 char *page)
1155{
1156 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1157 struct tcm_loop_tpg, tl_se_tpg);
1158 const char *status = NULL;
1159 ssize_t ret = -EINVAL;
1160
1161 switch (tl_tpg->tl_transport_status) {
1162 case TCM_TRANSPORT_ONLINE:
1163 status = "online";
1164 break;
1165 case TCM_TRANSPORT_OFFLINE:
1166 status = "offline";
1167 break;
1168 default:
1169 break;
1170 }
1171
1172 if (status)
1173 ret = snprintf(page, PAGE_SIZE, "%s\n", status);
1174
1175 return ret;
1176}
1177
1178static ssize_t tcm_loop_tpg_store_transport_status(
1179 struct se_portal_group *se_tpg,
1180 const char *page,
1181 size_t count)
1182{
1183 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1184 struct tcm_loop_tpg, tl_se_tpg);
1185
1186 if (!strncmp(page, "online", 6)) {
1187 tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
1188 return count;
1189 }
1190 if (!strncmp(page, "offline", 7)) {
1191 tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
1192 return count;
1193 }
1194 return -EINVAL;
1195}
1196
1197TF_TPG_BASE_ATTR(tcm_loop, transport_status, S_IRUGO | S_IWUSR);
1198
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001199static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
1200 &tcm_loop_tpg_nexus.attr,
Hannes Reineckefb2b2842013-10-16 09:12:53 +02001201 &tcm_loop_tpg_transport_status.attr,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001202 NULL,
1203};
1204
1205/* Start items for tcm_loop_naa_cit */
1206
Rashika Kheriaf0a6c692013-12-19 00:02:54 +05301207static struct se_portal_group *tcm_loop_make_naa_tpg(
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001208 struct se_wwn *wwn,
1209 struct config_group *group,
1210 const char *name)
1211{
1212 struct tcm_loop_hba *tl_hba = container_of(wwn,
1213 struct tcm_loop_hba, tl_hba_wwn);
1214 struct tcm_loop_tpg *tl_tpg;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001215 int ret;
Ming Lin2e1cd902015-03-29 23:11:30 -07001216 unsigned long tpgt;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001217
Ming Lin2e1cd902015-03-29 23:11:30 -07001218 if (strstr(name, "tpgt_") != name) {
Andy Grover6708bb22011-06-08 10:36:43 -07001219 pr_err("Unable to locate \"tpgt_#\" directory"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001220 " group\n");
1221 return ERR_PTR(-EINVAL);
1222 }
Ming Lin2e1cd902015-03-29 23:11:30 -07001223 if (kstrtoul(name+5, 10, &tpgt))
1224 return ERR_PTR(-EINVAL);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001225
Dan Carpenter12f09cc2011-04-02 14:32:47 -07001226 if (tpgt >= TL_TPGS_PER_HBA) {
Ming Lin2e1cd902015-03-29 23:11:30 -07001227 pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001228 " %u\n", tpgt, TL_TPGS_PER_HBA);
1229 return ERR_PTR(-EINVAL);
1230 }
1231 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
1232 tl_tpg->tl_hba = tl_hba;
1233 tl_tpg->tl_tpgt = tpgt;
1234 /*
1235 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
1236 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001237 ret = core_tpg_register(&loop_ops, wwn, &tl_tpg->tl_se_tpg, tl_tpg,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001238 TRANSPORT_TPG_TYPE_NORMAL);
1239 if (ret < 0)
1240 return ERR_PTR(-ENOMEM);
1241
Andy Grover6708bb22011-06-08 10:36:43 -07001242 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
Ming Lin2e1cd902015-03-29 23:11:30 -07001243 " Target Port %s,t,0x%04lx\n", tcm_loop_dump_proto_id(tl_hba),
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001244 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1245
1246 return &tl_tpg->tl_se_tpg;
1247}
1248
Rashika Kheriaf0a6c692013-12-19 00:02:54 +05301249static void tcm_loop_drop_naa_tpg(
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001250 struct se_portal_group *se_tpg)
1251{
1252 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1253 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1254 struct tcm_loop_tpg, tl_se_tpg);
1255 struct tcm_loop_hba *tl_hba;
1256 unsigned short tpgt;
1257
1258 tl_hba = tl_tpg->tl_hba;
1259 tpgt = tl_tpg->tl_tpgt;
1260 /*
1261 * Release the I_T Nexus for the Virtual SAS link if present
1262 */
1263 tcm_loop_drop_nexus(tl_tpg);
1264 /*
1265 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
1266 */
1267 core_tpg_deregister(se_tpg);
1268
Nicholas Bellinger0a020432011-10-10 19:44:05 -07001269 tl_tpg->tl_hba = NULL;
1270 tl_tpg->tl_tpgt = 0;
1271
Andy Grover6708bb22011-06-08 10:36:43 -07001272 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001273 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1274 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1275}
1276
1277/* End items for tcm_loop_naa_cit */
1278
1279/* Start items for tcm_loop_cit */
1280
Rashika Kheriaf0a6c692013-12-19 00:02:54 +05301281static struct se_wwn *tcm_loop_make_scsi_hba(
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001282 struct target_fabric_configfs *tf,
1283 struct config_group *group,
1284 const char *name)
1285{
1286 struct tcm_loop_hba *tl_hba;
1287 struct Scsi_Host *sh;
1288 char *ptr;
1289 int ret, off = 0;
1290
1291 tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
1292 if (!tl_hba) {
Andy Grover6708bb22011-06-08 10:36:43 -07001293 pr_err("Unable to allocate struct tcm_loop_hba\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001294 return ERR_PTR(-ENOMEM);
1295 }
1296 /*
1297 * Determine the emulated Protocol Identifier and Target Port Name
1298 * based on the incoming configfs directory name.
1299 */
1300 ptr = strstr(name, "naa.");
1301 if (ptr) {
1302 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1303 goto check_len;
1304 }
1305 ptr = strstr(name, "fc.");
1306 if (ptr) {
1307 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1308 off = 3; /* Skip over "fc." */
1309 goto check_len;
1310 }
1311 ptr = strstr(name, "iqn.");
Jesper Juhla57b5d32011-06-28 00:30:17 +02001312 if (!ptr) {
Andy Grover6708bb22011-06-08 10:36:43 -07001313 pr_err("Unable to locate prefix for emulated Target "
Jesper Juhla57b5d32011-06-28 00:30:17 +02001314 "Port: %s\n", name);
1315 ret = -EINVAL;
1316 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001317 }
Jesper Juhla57b5d32011-06-28 00:30:17 +02001318 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001319
1320check_len:
Dan Carpenter60d645a2011-06-15 10:03:05 -07001321 if (strlen(name) >= TL_WWN_ADDR_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001322 pr_err("Emulated NAA %s Address: %s, exceeds"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001323 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1324 TL_WWN_ADDR_LEN);
Jesper Juhla57b5d32011-06-28 00:30:17 +02001325 ret = -EINVAL;
1326 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001327 }
1328 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1329
1330 /*
1331 * Call device_register(tl_hba->dev) to register the emulated
1332 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1333 * device_register() callbacks in tcm_loop_driver_probe()
1334 */
1335 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1336 if (ret)
1337 goto out;
1338
1339 sh = tl_hba->sh;
1340 tcm_loop_hba_no_cnt++;
Andy Grover6708bb22011-06-08 10:36:43 -07001341 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001342 " %s Address: %s at Linux/SCSI Host ID: %d\n",
1343 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1344
1345 return &tl_hba->tl_hba_wwn;
1346out:
1347 kfree(tl_hba);
1348 return ERR_PTR(ret);
1349}
1350
Rashika Kheriaf0a6c692013-12-19 00:02:54 +05301351static void tcm_loop_drop_scsi_hba(
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001352 struct se_wwn *wwn)
1353{
1354 struct tcm_loop_hba *tl_hba = container_of(wwn,
1355 struct tcm_loop_hba, tl_hba_wwn);
Nicholas Bellinger6297b072011-11-12 09:29:51 -08001356
1357 pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
1358 " SAS Address: %s at Linux/SCSI Host ID: %d\n",
1359 tl_hba->tl_wwn_address, tl_hba->sh->host_no);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001360 /*
1361 * Call device_unregister() on the original tl_hba->dev.
1362 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1363 * release *tl_hba;
1364 */
1365 device_unregister(&tl_hba->dev);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001366}
1367
1368/* Start items for tcm_loop_cit */
1369static ssize_t tcm_loop_wwn_show_attr_version(
1370 struct target_fabric_configfs *tf,
1371 char *page)
1372{
1373 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1374}
1375
1376TF_WWN_ATTR_RO(tcm_loop, version);
1377
1378static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1379 &tcm_loop_wwn_version.attr,
1380 NULL,
1381};
1382
1383/* End items for tcm_loop_cit */
1384
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001385static const struct target_core_fabric_ops loop_ops = {
1386 .module = THIS_MODULE,
1387 .name = "loopback",
1388 .get_fabric_name = tcm_loop_get_fabric_name,
1389 .get_fabric_proto_ident = tcm_loop_get_fabric_proto_ident,
1390 .tpg_get_wwn = tcm_loop_get_endpoint_wwn,
1391 .tpg_get_tag = tcm_loop_get_tag,
1392 .tpg_get_default_depth = tcm_loop_get_default_depth,
1393 .tpg_get_pr_transport_id = tcm_loop_get_pr_transport_id,
1394 .tpg_get_pr_transport_id_len = tcm_loop_get_pr_transport_id_len,
1395 .tpg_parse_pr_out_transport_id = tcm_loop_parse_pr_out_transport_id,
1396 .tpg_check_demo_mode = tcm_loop_check_demo_mode,
1397 .tpg_check_demo_mode_cache = tcm_loop_check_demo_mode_cache,
1398 .tpg_check_demo_mode_write_protect =
1399 tcm_loop_check_demo_mode_write_protect,
1400 .tpg_check_prod_mode_write_protect =
1401 tcm_loop_check_prod_mode_write_protect,
1402 .tpg_check_prot_fabric_only = tcm_loop_check_prot_fabric_only,
1403 .tpg_alloc_fabric_acl = tcm_loop_tpg_alloc_fabric_acl,
1404 .tpg_release_fabric_acl = tcm_loop_tpg_release_fabric_acl,
1405 .tpg_get_inst_index = tcm_loop_get_inst_index,
1406 .check_stop_free = tcm_loop_check_stop_free,
1407 .release_cmd = tcm_loop_release_cmd,
1408 .shutdown_session = tcm_loop_shutdown_session,
1409 .close_session = tcm_loop_close_session,
1410 .sess_get_index = tcm_loop_sess_get_index,
1411 .write_pending = tcm_loop_write_pending,
1412 .write_pending_status = tcm_loop_write_pending_status,
1413 .set_default_node_attributes = tcm_loop_set_default_node_attributes,
1414 .get_task_tag = tcm_loop_get_task_tag,
1415 .get_cmd_state = tcm_loop_get_cmd_state,
1416 .queue_data_in = tcm_loop_queue_data_in,
1417 .queue_status = tcm_loop_queue_status,
1418 .queue_tm_rsp = tcm_loop_queue_tm_rsp,
1419 .aborted_task = tcm_loop_aborted_task,
1420 .fabric_make_wwn = tcm_loop_make_scsi_hba,
1421 .fabric_drop_wwn = tcm_loop_drop_scsi_hba,
1422 .fabric_make_tpg = tcm_loop_make_naa_tpg,
1423 .fabric_drop_tpg = tcm_loop_drop_naa_tpg,
1424 .fabric_post_link = tcm_loop_port_link,
1425 .fabric_pre_unlink = tcm_loop_port_unlink,
1426 .tfc_wwn_attrs = tcm_loop_wwn_attrs,
1427 .tfc_tpg_base_attrs = tcm_loop_tpg_attrs,
1428 .tfc_tpg_attrib_attrs = tcm_loop_tpg_attrib_attrs,
1429};
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001430
1431static int __init tcm_loop_fabric_init(void)
1432{
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001433 int ret = -ENOMEM;
1434
1435 tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
1436 if (!tcm_loop_workqueue)
1437 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001438
1439 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1440 sizeof(struct tcm_loop_cmd),
1441 __alignof__(struct tcm_loop_cmd),
1442 0, NULL);
1443 if (!tcm_loop_cmd_cache) {
Andy Grover6708bb22011-06-08 10:36:43 -07001444 pr_debug("kmem_cache_create() for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001445 " tcm_loop_cmd_cache failed\n");
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001446 goto out_destroy_workqueue;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001447 }
1448
1449 ret = tcm_loop_alloc_core_bus();
1450 if (ret)
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001451 goto out_destroy_cache;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001452
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001453 ret = target_register_template(&loop_ops);
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001454 if (ret)
1455 goto out_release_core_bus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001456
1457 return 0;
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001458
1459out_release_core_bus:
1460 tcm_loop_release_core_bus();
1461out_destroy_cache:
1462 kmem_cache_destroy(tcm_loop_cmd_cache);
1463out_destroy_workqueue:
1464 destroy_workqueue(tcm_loop_workqueue);
1465out:
1466 return ret;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001467}
1468
1469static void __exit tcm_loop_fabric_exit(void)
1470{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001471 target_unregister_template(&loop_ops);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001472 tcm_loop_release_core_bus();
1473 kmem_cache_destroy(tcm_loop_cmd_cache);
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001474 destroy_workqueue(tcm_loop_workqueue);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001475}
1476
1477MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1478MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1479MODULE_LICENSE("GPL");
1480module_init(tcm_loop_fabric_init);
1481module_exit(tcm_loop_fabric_exit);