blob: dda9a08c939f3716ccbc958046ca516a838b6573 [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 Bellinger4c76251e2013-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
44/* Local pointer to allocated TCM configfs fabric module */
45static struct target_fabric_configfs *tcm_loop_fabric_configfs;
46
Christoph Hellwigafe2cb72012-02-02 17:04:41 -050047static struct workqueue_struct *tcm_loop_workqueue;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070048static struct kmem_cache *tcm_loop_cmd_cache;
49
50static int tcm_loop_hba_no_cnt;
51
Christoph Hellwig167864542012-02-02 17:04:42 -050052static int tcm_loop_queue_status(struct se_cmd *se_cmd);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070053
54/*
55 * Called from struct target_core_fabric_ops->check_stop_free()
56 */
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -070057static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070058{
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 Groverc8e31f22012-01-19 13:39:17 -080064 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -070065 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070066 /*
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 Hellwig82f1c8a2011-09-13 23:09:01 +020070 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -070071 return 1;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070072}
73
Christoph Hellwig35462972011-05-31 23:56:57 -040074static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070075{
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 Viro8946b072013-03-31 02:01:55 -040082static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070083{
Al Viro8946b072013-03-31 02:01:55 -040084 seq_printf(m, "tcm_loop_proc_info()\n");
85 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070086}
87
88static int tcm_loop_driver_probe(struct device *);
89static int tcm_loop_driver_remove(struct device *);
90
91static int pseudo_lld_bus_match(struct device *dev,
92 struct device_driver *dev_driver)
93{
94 return 1;
95}
96
97static 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
104static 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 */
111struct device *tcm_loop_primary;
112
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500113static void tcm_loop_submission_work(struct work_struct *work)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700114{
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500115 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 Bellinger3703b2c2011-03-18 15:39:17 -0700120 struct tcm_loop_hba *tl_hba;
121 struct tcm_loop_tpg *tl_tpg;
Christoph Hellwig167864542012-02-02 17:04:42 -0500122 struct scatterlist *sgl_bidi = NULL;
Sagi Grimberge2a4f552014-06-11 12:09:59 +0300123 u32 sgl_bidi_count = 0, transfer_length;
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700124 int rc;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500125
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700126 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
127 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500128
Nicholas Bellinger0a020432011-10-10 19:44:05 -0700129 /*
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 Hellwigf872c9f2012-02-02 17:04:40 -0500135 goto out_done;
Nicholas Bellinger0a020432011-10-10 19:44:05 -0700136 }
Hannes Reineckefb2b2842013-10-16 09:12:53 +0200137 if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) {
138 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
139 goto out_done;
140 }
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500141 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 Bellinger3703b2c2011-03-18 15:39:17 -0700147 }
Christoph Hellwig167864542012-02-02 17:04:42 -0500148 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 Hellwigf872c9f2012-02-02 17:04:40 -0500153 se_cmd->se_cmd_flags |= SCF_BIDI;
154
Christoph Hellwig167864542012-02-02 17:04:42 -0500155 }
Sagi Grimbergb5b8e292014-02-19 17:50:17 +0200156
Sagi Grimberge2a4f552014-06-11 12:09:59 +0300157 transfer_length = scsi_transfer_length(sc);
158 if (!scsi_prot_sg_count(sc) &&
159 scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) {
Sagi Grimbergb5b8e292014-02-19 17:50:17 +0200160 se_cmd->prot_pto = true;
Sagi Grimberge2a4f552014-06-11 12:09:59 +0300161 /*
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 Grimbergb5b8e292014-02-19 17:50:17 +0200168
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700169 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 Hellwig68d81f42014-11-24 07:07:25 -0800171 transfer_length, TCM_SIMPLE_TAG,
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700172 sc->sc_data_direction, 0,
173 scsi_sglist(sc), scsi_sg_count(sc),
Nicholas Bellinger59dedde2013-12-23 20:39:23 +0000174 sgl_bidi, sgl_bidi_count,
175 scsi_prot_sglist(sc), scsi_prot_sg_count(sc));
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700176 if (rc < 0) {
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500177 set_host_byte(sc, DID_NO_CONNECT);
178 goto out_done;
179 }
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500180 return;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500181
182out_done:
Nicholas Bellingerb43f1882014-06-17 22:23:03 +0000183 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500184 sc->scsi_done(sc);
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500185 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 */
192static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
193{
194 struct tcm_loop_cmd *tl_cmd;
195
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200196 pr_debug("tcm_loop_queuecommand() %d:%d:%d:%llu got CDB: 0x%02x"
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500197 " 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 Reinecke6375f892014-10-02 09:30:55 +0200210 tl_cmd->sc_cmd_tag = sc->request->tag;
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500211 INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
212 queue_work(tcm_loop_workqueue, &tl_cmd->work);
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500213 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700214}
215
216/*
217 * Called from SCSI EH process context to issue a LUN_RESET TMR
218 * to struct scsi_device
219 */
Hannes Reineckea314d702013-10-16 09:12:54 +0200220static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg,
221 struct tcm_loop_nexus *tl_nexus,
Hannes Reinecke969871c2013-10-16 09:12:55 +0200222 int lun, int task, enum tcm_tmreq_table tmr)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700223{
224 struct se_cmd *se_cmd = NULL;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700225 struct se_session *se_sess;
Hannes Reineckea314d702013-10-16 09:12:54 +0200226 struct se_portal_group *se_tpg;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700227 struct tcm_loop_cmd *tl_cmd = NULL;
Hannes Reineckea314d702013-10-16 09:12:54 +0200228 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 Hellwig68d81f42014-11-24 07:07:25 -0800251 DMA_NONE, TCM_SIMPLE_TAG,
Hannes Reineckea314d702013-10-16 09:12:54 +0200252 &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 Reinecke969871c2013-10-16 09:12:55 +0200258 if (tmr == TMR_ABORT_TASK)
259 se_cmd->se_tmr_req->ref_task_tag = task;
260
Hannes Reineckea314d702013-10-16 09:12:54 +0200261 /*
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;
279release:
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 Reinecke969871c2013-10-16 09:12:55 +0200288static 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 Reinecke6375f892014-10-02 09:30:55 +0200314 sc->request->tag, TMR_ABORT_TASK);
Hannes Reinecke969871c2013-10-16 09:12:55 +0200315 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
316}
317
Hannes Reineckea314d702013-10-16 09:12:54 +0200318/*
319 * Called from SCSI EH process context to issue a LUN_RESET TMR
320 * to struct scsi_device
321 */
322static int tcm_loop_device_reset(struct scsi_cmnd *sc)
323{
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700324 struct tcm_loop_hba *tl_hba;
325 struct tcm_loop_nexus *tl_nexus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700326 struct tcm_loop_tpg *tl_tpg;
Hannes Reineckea314d702013-10-16 09:12:54 +0200327 int ret = FAILED;
328
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700329 /*
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 Grover6708bb22011-06-08 10:36:43 -0700338 pr_err("Unable to perform device reset without"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700339 " active I_T Nexus\n");
340 return FAILED;
341 }
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700342 /*
Hannes Reineckea314d702013-10-16 09:12:54 +0200343 * Locate the tl_tpg pointer from TargetID in sc->device->id
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700344 */
345 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
Hannes Reinecke969871c2013-10-16 09:12:55 +0200346 ret = tcm_loop_issue_tmr(tl_tpg, tl_nexus, sc->device->lun,
347 0, TMR_LUN_RESET);
Hannes Reineckea314d702013-10-16 09:12:54 +0200348 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700349}
350
Hannes Reinecke8f4a1fb2013-10-16 09:12:56 +0200351static 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 Bellinger3703b2c2011-03-18 15:39:17 -0700376static 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 Bellinger3703b2c2011-03-18 15:39:17 -0700382static struct scsi_host_template tcm_loop_driver_template = {
Al Viro8946b072013-03-31 02:01:55 -0400383 .show_info = tcm_loop_show_info,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700384 .proc_name = "tcm_loopback",
385 .name = "TCM_Loopback",
386 .queuecommand = tcm_loop_queuecommand,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100387 .change_queue_depth = scsi_change_queue_depth,
Hannes Reinecke969871c2013-10-16 09:12:55 +0200388 .eh_abort_handler = tcm_loop_abort_task,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700389 .eh_device_reset_handler = tcm_loop_device_reset,
Hannes Reinecke8f4a1fb2013-10-16 09:12:56 +0200390 .eh_target_reset_handler = tcm_loop_target_reset,
Christoph Hellwig2e88efd2011-11-29 03:20:41 -0500391 .can_queue = 1024,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700392 .this_id = -1,
Christoph Hellwig2e88efd2011-11-29 03:20:41 -0500393 .sg_tablesize = 256,
394 .cmd_per_lun = 1024,
395 .max_sectors = 0xFFFF,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700396 .use_clustering = DISABLE_CLUSTERING,
397 .slave_alloc = tcm_loop_slave_alloc,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700398 .module = THIS_MODULE,
Christoph Hellwig2ecb2042014-11-03 14:09:02 +0100399 .use_blk_tags = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100400 .track_queue_depth = 1,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700401};
402
403static int tcm_loop_driver_probe(struct device *dev)
404{
405 struct tcm_loop_hba *tl_hba;
406 struct Scsi_Host *sh;
Nicholas Bellinger59dedde2013-12-23 20:39:23 +0000407 int error, host_prot;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700408
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 Grover6708bb22011-06-08 10:36:43 -0700414 pr_err("Unable to allocate struct scsi_host\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700415 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 Bellinger59dedde2013-12-23 20:39:23 +0000431 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 Bellinger3703b2c2011-03-18 15:39:17 -0700438 error = scsi_add_host(sh, &tl_hba->dev);
439 if (error) {
Andy Grover6708bb22011-06-08 10:36:43 -0700440 pr_err("%s: scsi_add_host failed\n", __func__);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700441 scsi_host_put(sh);
442 return -ENODEV;
443 }
444 return 0;
445}
446
447static 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
460static 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 */
470static 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 Grover6708bb22011-06-08 10:36:43 -0700481 pr_err("device_register() failed for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700482 " 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 */
493static 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 Grover6708bb22011-06-08 10:36:43 -0700499 pr_err("Unable to allocate tcm_loop_primary\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700500 return PTR_ERR(tcm_loop_primary);
501 }
502
503 ret = bus_register(&tcm_loop_lld_bus);
504 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700505 pr_err("bus_register() failed for tcm_loop_lld_bus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700506 goto dev_unreg;
507 }
508
509 ret = driver_register(&tcm_loop_driverfs);
510 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700511 pr_err("driver_register() failed for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700512 "tcm_loop_driverfs\n");
513 goto bus_unreg;
514 }
515
Andy Grover6708bb22011-06-08 10:36:43 -0700516 pr_debug("Initialized TCM Loop Core Bus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700517 return ret;
518
519bus_unreg:
520 bus_unregister(&tcm_loop_lld_bus);
521dev_unreg:
522 root_device_unregister(tcm_loop_primary);
523 return ret;
524}
525
526static 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 Grover6708bb22011-06-08 10:36:43 -0700532 pr_debug("Releasing TCM Loop Core BUS\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700533}
534
535static char *tcm_loop_get_fabric_name(void)
536{
537 return "loopback";
538}
539
540static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
541{
Jörn Engel8359cf42011-11-24 02:05:51 +0100542 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700543 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 Grover6708bb22011-06-08 10:36:43 -0700559 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700560 " SAS emulation\n", tl_hba->tl_proto_id);
561 break;
562 }
563
564 return sas_get_fabric_proto_ident(se_tpg);
565}
566
567static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
568{
Jörn Engel8359cf42011-11-24 02:05:51 +0100569 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700570 /*
571 * Return the passed NAA identifier for the SAS Target Port
572 */
573 return &tl_tpg->tl_hba->tl_wwn_address[0];
574}
575
576static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
577{
Jörn Engel8359cf42011-11-24 02:05:51 +0100578 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700579 /*
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
586static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
587{
588 return 1;
589}
590
591static 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 Engel8359cf42011-11-24 02:05:51 +0100598 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700599 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 Grover6708bb22011-06-08 10:36:43 -0700612 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700613 " 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
621static 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 Engel8359cf42011-11-24 02:05:51 +0100627 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700628 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 Grover6708bb22011-06-08 10:36:43 -0700641 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700642 " 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 */
654static 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 Engel8359cf42011-11-24 02:05:51 +0100660 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700661 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 Grover6708bb22011-06-08 10:36:43 -0700674 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700675 " 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 */
687static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
688{
689 return 1;
690}
691
692static 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 */
701static 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 */
711static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
712{
713 return 0;
714}
715
716static 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 Grover6708bb22011-06-08 10:36:43 -0700723 pr_err("Unable to allocate struct tcm_loop_nacl\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700724 return NULL;
725 }
726
727 return &tl_nacl->se_node_acl;
728}
729
730static 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
740static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
741{
742 return 1;
743}
744
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700745static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
746{
747 return 1;
748}
749
750static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
751{
752 return;
753}
754
755static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
756{
Hannes Reinecke969871c2013-10-16 09:12:55 +0200757 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 Bellinger3703b2c2011-03-18 15:39:17 -0700761}
762
763static 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
771static int tcm_loop_shutdown_session(struct se_session *se_sess)
772{
773 return 0;
774}
775
776static void tcm_loop_close_session(struct se_session *se_sess)
777{
778 return;
779};
780
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700781static 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 Hellwig70baf0a2012-07-08 15:58:39 -0400792 target_execute_cmd(se_cmd);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700793 return 0;
794}
795
796static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
797{
798 return 0;
799}
800
801static 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 Grover6708bb22011-06-08 10:36:43 -0700807 pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700808 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
809
810 sc->result = SAM_STAT_GOOD;
811 set_host_byte(sc, DID_OK);
Roland Dreier6cf3fa62012-02-14 15:30:31 -0800812 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 Bellinger3703b2c2011-03-18 15:39:17 -0700815 sc->scsi_done(sc);
816 return 0;
817}
818
819static 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 Grover6708bb22011-06-08 10:36:43 -0700825 pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700826 " 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 Grover5951146d2011-07-19 10:26:37 +0000832 memcpy(sc->sense_buffer, se_cmd->sense_buffer,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700833 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 Dreier6cf3fa62012-02-14 15:30:31 -0800840 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 Bellinger3703b2c2011-03-18 15:39:17 -0700843 sc->scsi_done(sc);
844 return 0;
845}
846
Joern Engelb79fafa2013-07-03 11:22:17 -0400847static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700848{
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 Bellinger3703b2c2011-03-18 15:39:17 -0700857}
858
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700859static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
860{
861 return;
862}
863
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700864static 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
882static 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 Engel33940d02014-09-16 16:23:12 -0400890 atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700891 /*
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 Grover6708bb22011-06-08 10:36:43 -0700896 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700897 return 0;
898}
899
900static 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 Grover6708bb22011-06-08 10:36:43 -0700914 pr_err("Unable to locate struct scsi_device for %d:%d:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700915 "%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 Engel33940d02014-09-16 16:23:12 -0400924 atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700925
Andy Grover6708bb22011-06-08 10:36:43 -0700926 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700927}
928
929/* End items for tcm_loop_port_cit */
930
931/* Start items for tcm_loop_nexus_cit */
932
933static 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 Carpenter552523d2011-06-15 09:41:33 -0700940 int ret = -ENOMEM;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700941
942 if (tl_tpg->tl_hba->tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700943 pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700944 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 Grover6708bb22011-06-08 10:36:43 -0700950 pr_err("Unable to allocate struct tcm_loop_nexus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700951 return -ENOMEM;
952 }
953 /*
954 * Initialize the struct se_session pointer
955 */
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700956 tl_nexus->se_sess = transport_init_session(TARGET_PROT_ALL);
Dan Carpenter552523d2011-06-15 09:41:33 -0700957 if (IS_ERR(tl_nexus->se_sess)) {
958 ret = PTR_ERR(tl_nexus->se_sess);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700959 goto out;
Dan Carpenter552523d2011-06-15 09:41:33 -0700960 }
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700961 /*
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 Grover5951146d2011-07-19 10:26:37 +0000977 tl_nexus->se_sess, tl_nexus);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700978 tl_tpg->tl_hba->tl_nexus = tl_nexus;
Andy Grover6708bb22011-06-08 10:36:43 -0700979 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700980 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
981 name);
982 return 0;
983
984out:
985 kfree(tl_nexus);
Dan Carpenter552523d2011-06-15 09:41:33 -0700986 return ret;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700987}
988
989static 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 Reinecke1ec59fe2013-10-16 09:12:52 +0200996 if (!tl_hba)
997 return -ENODEV;
998
999 tl_nexus = tl_hba->tl_nexus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001000 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 Grover6708bb22011-06-08 10:36:43 -07001008 pr_err("Unable to remove TCM_Loop I_T Nexus with"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001009 " active TPG port count: %d\n",
1010 atomic_read(&tpg->tl_tpg_port_count));
1011 return -EPERM;
1012 }
1013
Andy Grover6708bb22011-06-08 10:36:43 -07001014 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001015 " %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
1028static 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
1047static 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 Carpenter60d645a2011-06-15 10:03:05 -07001069 if (strlen(page) >= TL_WWN_ADDR_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001070 pr_err("Emulated NAA Sas Address: %s, exceeds"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001071 " 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 Grover6708bb22011-06-08 10:36:43 -07001079 pr_err("Passed SAS Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001080 " 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 Grover6708bb22011-06-08 10:36:43 -07001090 pr_err("Passed FCP Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001091 " 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 Grover6708bb22011-06-08 10:36:43 -07001101 pr_err("Passed iSCSI Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001102 " 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 Grover6708bb22011-06-08 10:36:43 -07001109 pr_err("Unable to locate prefix for emulated Initiator Port:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001110 " %s\n", i_port);
1111 return -EINVAL;
1112 /*
1113 * Clear any trailing newline for the NAA WWN
1114 */
1115check_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
1126TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
1127
Hannes Reineckefb2b2842013-10-16 09:12:53 +02001128static 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
1154static 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
1173TF_TPG_BASE_ATTR(tcm_loop, transport_status, S_IRUGO | S_IWUSR);
1174
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001175static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
1176 &tcm_loop_tpg_nexus.attr,
Hannes Reineckefb2b2842013-10-16 09:12:53 +02001177 &tcm_loop_tpg_transport_status.attr,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001178 NULL,
1179};
1180
1181/* Start items for tcm_loop_naa_cit */
1182
Rashika Kheriaf0a6c692013-12-19 00:02:54 +05301183static struct se_portal_group *tcm_loop_make_naa_tpg(
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001184 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 Grover6708bb22011-06-08 10:36:43 -07001197 pr_err("Unable to locate \"tpgt_#\" directory"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001198 " 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 Carpenter12f09cc2011-04-02 14:32:47 -07001204 if (tpgt >= TL_TPGS_PER_HBA) {
Andy Grover6708bb22011-06-08 10:36:43 -07001205 pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001206 " %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 Grover5951146d2011-07-19 10:26:37 +00001216 wwn, &tl_tpg->tl_se_tpg, tl_tpg,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001217 TRANSPORT_TPG_TYPE_NORMAL);
1218 if (ret < 0)
1219 return ERR_PTR(-ENOMEM);
1220
Andy Grover6708bb22011-06-08 10:36:43 -07001221 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001222 " 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 Kheriaf0a6c692013-12-19 00:02:54 +05301228static void tcm_loop_drop_naa_tpg(
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001229 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 Bellinger0a020432011-10-10 19:44:05 -07001248 tl_tpg->tl_hba = NULL;
1249 tl_tpg->tl_tpgt = 0;
1250
Andy Grover6708bb22011-06-08 10:36:43 -07001251 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001252 " 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 Kheriaf0a6c692013-12-19 00:02:54 +05301260static struct se_wwn *tcm_loop_make_scsi_hba(
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001261 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 Grover6708bb22011-06-08 10:36:43 -07001272 pr_err("Unable to allocate struct tcm_loop_hba\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001273 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 Juhla57b5d32011-06-28 00:30:17 +02001291 if (!ptr) {
Andy Grover6708bb22011-06-08 10:36:43 -07001292 pr_err("Unable to locate prefix for emulated Target "
Jesper Juhla57b5d32011-06-28 00:30:17 +02001293 "Port: %s\n", name);
1294 ret = -EINVAL;
1295 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001296 }
Jesper Juhla57b5d32011-06-28 00:30:17 +02001297 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001298
1299check_len:
Dan Carpenter60d645a2011-06-15 10:03:05 -07001300 if (strlen(name) >= TL_WWN_ADDR_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001301 pr_err("Emulated NAA %s Address: %s, exceeds"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001302 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1303 TL_WWN_ADDR_LEN);
Jesper Juhla57b5d32011-06-28 00:30:17 +02001304 ret = -EINVAL;
1305 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001306 }
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 Grover6708bb22011-06-08 10:36:43 -07001320 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001321 " %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;
1325out:
1326 kfree(tl_hba);
1327 return ERR_PTR(ret);
1328}
1329
Rashika Kheriaf0a6c692013-12-19 00:02:54 +05301330static void tcm_loop_drop_scsi_hba(
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001331 struct se_wwn *wwn)
1332{
1333 struct tcm_loop_hba *tl_hba = container_of(wwn,
1334 struct tcm_loop_hba, tl_hba_wwn);
Nicholas Bellinger6297b072011-11-12 09:29:51 -08001335
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 Bellinger3703b2c2011-03-18 15:39:17 -07001339 /*
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 Bellinger3703b2c2011-03-18 15:39:17 -07001345}
1346
1347/* Start items for tcm_loop_cit */
1348static 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
1355TF_WWN_ATTR_RO(tcm_loop, version);
1356
1357static 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
1364static int tcm_loop_register_configfs(void)
1365{
1366 struct target_fabric_configfs *fabric;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001367 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 Grovere3d6f902011-07-19 08:55:10 +00001376 if (IS_ERR(fabric)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001377 pr_err("tcm_loop_register_configfs() failed!\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001378 return PTR_ERR(fabric);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001379 }
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 Bellinger3703b2c2011-03-18 15:39:17 -07001410 * Used for setting up remaining TCM resources in process context
1411 */
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001412 fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
Christoph Hellwig35462972011-05-31 23:56:57 -04001413 fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001414 fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
1415 fabric->tf_ops.close_session = &tcm_loop_close_session;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001416 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 Bellinger3703b2c2011-03-18 15:39:17 -07001427 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 Bellinger131e6ab2014-03-22 14:55:56 -07001430 fabric->tf_ops.aborted_task = &tcm_loop_aborted_task;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001431
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001432 /*
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 Groverd80e224d2013-10-09 11:05:56 -07001450 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 Bellinger3703b2c2011-03-18 15:39:17 -07001455 /*
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 Grover6708bb22011-06-08 10:36:43 -07001461 pr_err("target_fabric_configfs_register() for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001462 " 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 Grover6708bb22011-06-08 10:36:43 -07001470 pr_debug("TCM_LOOP[0] - Set fabric ->"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001471 " tcm_loop_fabric_configfs\n");
1472 return 0;
1473}
1474
1475static 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 Grover6708bb22011-06-08 10:36:43 -07001482 pr_debug("TCM_LOOP[0] - Cleared"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001483 " tcm_loop_fabric_configfs\n");
1484}
1485
1486static int __init tcm_loop_fabric_init(void)
1487{
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001488 int ret = -ENOMEM;
1489
1490 tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
1491 if (!tcm_loop_workqueue)
1492 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001493
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 Grover6708bb22011-06-08 10:36:43 -07001499 pr_debug("kmem_cache_create() for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001500 " tcm_loop_cmd_cache failed\n");
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001501 goto out_destroy_workqueue;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001502 }
1503
1504 ret = tcm_loop_alloc_core_bus();
1505 if (ret)
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001506 goto out_destroy_cache;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001507
1508 ret = tcm_loop_register_configfs();
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001509 if (ret)
1510 goto out_release_core_bus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001511
1512 return 0;
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001513
1514out_release_core_bus:
1515 tcm_loop_release_core_bus();
1516out_destroy_cache:
1517 kmem_cache_destroy(tcm_loop_cmd_cache);
1518out_destroy_workqueue:
1519 destroy_workqueue(tcm_loop_workqueue);
1520out:
1521 return ret;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001522}
1523
1524static 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 Hellwigafe2cb72012-02-02 17:04:41 -05001529 destroy_workqueue(tcm_loop_workqueue);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001530}
1531
1532MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1533MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1534MODULE_LICENSE("GPL");
1535module_init(tcm_loop_fabric_init);
1536module_exit(tcm_loop_fabric_exit);