blob: cb8893f303a5cfe77731c60129acd74b1ced046b [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 *
6 * © Copyright 2011 RisingTide Systems LLC.
7 *
8 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
9 *
10 * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 ****************************************************************************/
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/types.h>
28#include <linux/configfs.h>
29#include <scsi/scsi.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi_host.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_cmnd.h>
Nicholas 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
52/*
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070053 * Called by struct target_core_fabric_ops->new_cmd_map()
54 *
55 * Always called in process context. A non zero return value
56 * here will signal to handle an exception based on the return code.
57 */
58static int tcm_loop_new_cmd_map(struct se_cmd *se_cmd)
59{
60 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
61 struct tcm_loop_cmd, tl_se_cmd);
62 struct scsi_cmnd *sc = tl_cmd->sc;
Andy Grover5951146d2011-07-19 10:26:37 +000063 struct scatterlist *sgl_bidi = NULL;
64 u32 sgl_bidi_count = 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070065 int ret;
66 /*
67 * Allocate the necessary tasks to complete the received CDB+data
68 */
Andy Grover5951146d2011-07-19 10:26:37 +000069 ret = transport_generic_allocate_tasks(se_cmd, sc->cmnd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -070070 if (ret != 0)
71 return ret;
Andy Grover5951146d2011-07-19 10:26:37 +000072 /*
73 * For BIDI commands, pass in the extra READ buffer
74 * to transport_generic_map_mem_to_cmd() below..
75 */
Christoph Hellwig33c3faf2011-11-14 11:36:30 -050076 if (se_cmd->se_cmd_flags & SCF_BIDI) {
Andy Grover5951146d2011-07-19 10:26:37 +000077 struct scsi_data_buffer *sdb = scsi_in(sc);
78
79 sgl_bidi = sdb->table.sgl;
80 sgl_bidi_count = sdb->table.nents;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070081 }
Nicholas Bellinger8cd79f22011-10-24 13:35:37 -070082 /*
83 * Because some userspace code via scsi-generic do not memset their
84 * associated read buffers, go ahead and do that here for type
85 * SCF_SCSI_CONTROL_SG_IO_CDB. Also note that this is currently
86 * guaranteed to be a single SGL for SCF_SCSI_CONTROL_SG_IO_CDB
87 * by target core in transport_generic_allocate_tasks() ->
88 * transport_generic_cmd_sequencer().
89 */
90 if (se_cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB &&
91 se_cmd->data_direction == DMA_FROM_DEVICE) {
92 struct scatterlist *sg = scsi_sglist(sc);
93 unsigned char *buf = kmap(sg_page(sg)) + sg->offset;
94
95 if (buf != NULL) {
96 memset(buf, 0, sg->length);
97 kunmap(sg_page(sg));
98 }
99 }
Andy Grover5951146d2011-07-19 10:26:37 +0000100
Andy Groverec98f782011-07-20 19:28:46 +0000101 /* Tell the core about our preallocated memory */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700102 return transport_generic_map_mem_to_cmd(se_cmd, scsi_sglist(sc),
Andy Grover5951146d2011-07-19 10:26:37 +0000103 scsi_sg_count(sc), sgl_bidi, sgl_bidi_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700104}
105
106/*
107 * Called from struct target_core_fabric_ops->check_stop_free()
108 */
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700109static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700110{
111 /*
112 * Do not release struct se_cmd's containing a valid TMR
113 * pointer. These will be released directly in tcm_loop_device_reset()
114 * with transport_generic_free_cmd().
115 */
Andy Groverc8e31f22012-01-19 13:39:17 -0800116 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700117 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700118 /*
119 * Release the struct se_cmd, which will make a callback to release
120 * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd()
121 */
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +0200122 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700123 return 1;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700124}
125
Christoph Hellwig35462972011-05-31 23:56:57 -0400126static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700127{
128 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
129 struct tcm_loop_cmd, tl_se_cmd);
130
131 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
132}
133
134static int tcm_loop_proc_info(struct Scsi_Host *host, char *buffer,
135 char **start, off_t offset,
136 int length, int inout)
137{
138 return sprintf(buffer, "tcm_loop_proc_info()\n");
139}
140
141static int tcm_loop_driver_probe(struct device *);
142static int tcm_loop_driver_remove(struct device *);
143
144static int pseudo_lld_bus_match(struct device *dev,
145 struct device_driver *dev_driver)
146{
147 return 1;
148}
149
150static struct bus_type tcm_loop_lld_bus = {
151 .name = "tcm_loop_bus",
152 .match = pseudo_lld_bus_match,
153 .probe = tcm_loop_driver_probe,
154 .remove = tcm_loop_driver_remove,
155};
156
157static struct device_driver tcm_loop_driverfs = {
158 .name = "tcm_loop",
159 .bus = &tcm_loop_lld_bus,
160};
161/*
162 * Used with root_device_register() in tcm_loop_alloc_core_bus() below
163 */
164struct device *tcm_loop_primary;
165
166/*
167 * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and
168 * drivers/scsi/libiscsi.c:iscsi_change_queue_depth()
169 */
170static int tcm_loop_change_queue_depth(
171 struct scsi_device *sdev,
172 int depth,
173 int reason)
174{
175 switch (reason) {
176 case SCSI_QDEPTH_DEFAULT:
177 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
178 break;
179 case SCSI_QDEPTH_QFULL:
180 scsi_track_queue_full(sdev, depth);
181 break;
182 case SCSI_QDEPTH_RAMP_UP:
183 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
184 break;
185 default:
186 return -EOPNOTSUPP;
187 }
188 return sdev->queue_depth;
189}
190
191/*
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500192 * Locate the SAM Task Attr from struct scsi_cmnd *
193 */
194static int tcm_loop_sam_attr(struct scsi_cmnd *sc)
195{
196 if (sc->device->tagged_supported) {
197 switch (sc->tag) {
198 case HEAD_OF_QUEUE_TAG:
199 return MSG_HEAD_TAG;
200 case ORDERED_QUEUE_TAG:
201 return MSG_ORDERED_TAG;
202 default:
203 break;
204 }
205 }
206
207 return MSG_SIMPLE_TAG;
208}
209
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500210static void tcm_loop_submission_work(struct work_struct *work)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700211{
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500212 struct tcm_loop_cmd *tl_cmd =
213 container_of(work, struct tcm_loop_cmd, work);
214 struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
215 struct scsi_cmnd *sc = tl_cmd->sc;
216 struct tcm_loop_nexus *tl_nexus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700217 struct tcm_loop_hba *tl_hba;
218 struct tcm_loop_tpg *tl_tpg;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500219
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700220 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
221 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500222
Nicholas Bellinger0a020432011-10-10 19:44:05 -0700223 /*
224 * Ensure that this tl_tpg reference from the incoming sc->device->id
225 * has already been configured via tcm_loop_make_naa_tpg().
226 */
227 if (!tl_tpg->tl_hba) {
228 set_host_byte(sc, DID_NO_CONNECT);
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500229 goto out_done;
Nicholas Bellinger0a020432011-10-10 19:44:05 -0700230 }
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500231
232 tl_nexus = tl_hba->tl_nexus;
233 if (!tl_nexus) {
234 scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
235 " does not exist\n");
236 set_host_byte(sc, DID_ERROR);
237 goto out_done;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700238 }
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500239
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500240 transport_init_se_cmd(se_cmd, tl_tpg->tl_se_tpg.se_tpg_tfo,
241 tl_nexus->se_sess,
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500242 scsi_bufflen(sc), sc->sc_data_direction,
243 tcm_loop_sam_attr(sc), &tl_cmd->tl_sense_buf[0]);
244
245 if (scsi_bidi_cmnd(sc))
246 se_cmd->se_cmd_flags |= SCF_BIDI;
247
248 if (transport_lookup_cmd_lun(se_cmd, tl_cmd->sc->device->lun) < 0) {
249 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
250 set_host_byte(sc, DID_NO_CONNECT);
251 goto out_done;
252 }
253
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700254 transport_generic_handle_cdb_map(se_cmd);
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500255 return;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500256
257out_done:
258 sc->scsi_done(sc);
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500259 return;
260}
261
262/*
263 * ->queuecommand can be and usually is called from interrupt context, so
264 * defer the actual submission to a workqueue.
265 */
266static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
267{
268 struct tcm_loop_cmd *tl_cmd;
269
270 pr_debug("tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x"
271 " scsi_buf_len: %u\n", sc->device->host->host_no,
272 sc->device->id, sc->device->channel, sc->device->lun,
273 sc->cmnd[0], scsi_bufflen(sc));
274
275 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
276 if (!tl_cmd) {
277 pr_err("Unable to allocate struct tcm_loop_cmd\n");
278 set_host_byte(sc, DID_ERROR);
279 sc->scsi_done(sc);
280 return 0;
281 }
282
283 tl_cmd->sc = sc;
284 INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
285 queue_work(tcm_loop_workqueue, &tl_cmd->work);
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500286 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700287}
288
289/*
290 * Called from SCSI EH process context to issue a LUN_RESET TMR
291 * to struct scsi_device
292 */
293static int tcm_loop_device_reset(struct scsi_cmnd *sc)
294{
295 struct se_cmd *se_cmd = NULL;
296 struct se_portal_group *se_tpg;
297 struct se_session *se_sess;
298 struct tcm_loop_cmd *tl_cmd = NULL;
299 struct tcm_loop_hba *tl_hba;
300 struct tcm_loop_nexus *tl_nexus;
301 struct tcm_loop_tmr *tl_tmr = NULL;
302 struct tcm_loop_tpg *tl_tpg;
Andy Groverc8e31f22012-01-19 13:39:17 -0800303 int ret = FAILED, rc;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700304 /*
305 * Locate the tcm_loop_hba_t pointer
306 */
307 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
308 /*
309 * Locate the tl_nexus and se_sess pointers
310 */
311 tl_nexus = tl_hba->tl_nexus;
312 if (!tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700313 pr_err("Unable to perform device reset without"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700314 " active I_T Nexus\n");
315 return FAILED;
316 }
317 se_sess = tl_nexus->se_sess;
318 /*
319 * Locate the tl_tpg and se_tpg pointers from TargetID in sc->device->id
320 */
321 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
322 se_tpg = &tl_tpg->tl_se_tpg;
323
324 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
325 if (!tl_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700326 pr_err("Unable to allocate memory for tl_cmd\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700327 return FAILED;
328 }
329
330 tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
331 if (!tl_tmr) {
Andy Grover6708bb22011-06-08 10:36:43 -0700332 pr_err("Unable to allocate memory for tl_tmr\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700333 goto release;
334 }
335 init_waitqueue_head(&tl_tmr->tl_tmr_wait);
336
337 se_cmd = &tl_cmd->tl_se_cmd;
338 /*
339 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
340 */
341 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
Nicholas Bellinger61db1802011-05-19 20:19:14 -0700342 DMA_NONE, MSG_SIMPLE_TAG,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700343 &tl_cmd->tl_sense_buf[0]);
Andy Groverc8e31f22012-01-19 13:39:17 -0800344
345 rc = core_tmr_alloc_req(se_cmd, tl_tmr, TMR_LUN_RESET, GFP_KERNEL);
346 if (rc < 0)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700347 goto release;
348 /*
349 * Locate the underlying TCM struct se_lun from sc->device->lun
350 */
Andy Grover5951146d2011-07-19 10:26:37 +0000351 if (transport_lookup_tmr_lun(se_cmd, sc->device->lun) < 0)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700352 goto release;
353 /*
354 * Queue the TMR to TCM Core and sleep waiting for tcm_loop_queue_tm_rsp()
355 * to wake us up.
356 */
357 transport_generic_handle_tmr(se_cmd);
358 wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
359 /*
360 * The TMR LUN_RESET has completed, check the response status and
361 * then release allocations.
362 */
363 ret = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
364 SUCCESS : FAILED;
365release:
366 if (se_cmd)
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +0200367 transport_generic_free_cmd(se_cmd, 1);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700368 else
369 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
370 kfree(tl_tmr);
371 return ret;
372}
373
374static int tcm_loop_slave_alloc(struct scsi_device *sd)
375{
376 set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
377 return 0;
378}
379
380static int tcm_loop_slave_configure(struct scsi_device *sd)
381{
382 return 0;
383}
384
385static struct scsi_host_template tcm_loop_driver_template = {
386 .proc_info = tcm_loop_proc_info,
387 .proc_name = "tcm_loopback",
388 .name = "TCM_Loopback",
389 .queuecommand = tcm_loop_queuecommand,
390 .change_queue_depth = tcm_loop_change_queue_depth,
391 .eh_device_reset_handler = tcm_loop_device_reset,
Christoph Hellwig2e88efd2011-11-29 03:20:41 -0500392 .can_queue = 1024,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700393 .this_id = -1,
Christoph Hellwig2e88efd2011-11-29 03:20:41 -0500394 .sg_tablesize = 256,
395 .cmd_per_lun = 1024,
396 .max_sectors = 0xFFFF,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700397 .use_clustering = DISABLE_CLUSTERING,
398 .slave_alloc = tcm_loop_slave_alloc,
399 .slave_configure = tcm_loop_slave_configure,
400 .module = THIS_MODULE,
401};
402
403static int tcm_loop_driver_probe(struct device *dev)
404{
405 struct tcm_loop_hba *tl_hba;
406 struct Scsi_Host *sh;
407 int error;
408
409 tl_hba = to_tcm_loop_hba(dev);
410
411 sh = scsi_host_alloc(&tcm_loop_driver_template,
412 sizeof(struct tcm_loop_hba));
413 if (!sh) {
Andy 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
431 error = scsi_add_host(sh, &tl_hba->dev);
432 if (error) {
Andy Grover6708bb22011-06-08 10:36:43 -0700433 pr_err("%s: scsi_add_host failed\n", __func__);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700434 scsi_host_put(sh);
435 return -ENODEV;
436 }
437 return 0;
438}
439
440static int tcm_loop_driver_remove(struct device *dev)
441{
442 struct tcm_loop_hba *tl_hba;
443 struct Scsi_Host *sh;
444
445 tl_hba = to_tcm_loop_hba(dev);
446 sh = tl_hba->sh;
447
448 scsi_remove_host(sh);
449 scsi_host_put(sh);
450 return 0;
451}
452
453static void tcm_loop_release_adapter(struct device *dev)
454{
455 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
456
457 kfree(tl_hba);
458}
459
460/*
461 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
462 */
463static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
464{
465 int ret;
466
467 tl_hba->dev.bus = &tcm_loop_lld_bus;
468 tl_hba->dev.parent = tcm_loop_primary;
469 tl_hba->dev.release = &tcm_loop_release_adapter;
470 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
471
472 ret = device_register(&tl_hba->dev);
473 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700474 pr_err("device_register() failed for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700475 " tl_hba->dev: %d\n", ret);
476 return -ENODEV;
477 }
478
479 return 0;
480}
481
482/*
483 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
484 * tcm_loop SCSI bus.
485 */
486static int tcm_loop_alloc_core_bus(void)
487{
488 int ret;
489
490 tcm_loop_primary = root_device_register("tcm_loop_0");
491 if (IS_ERR(tcm_loop_primary)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700492 pr_err("Unable to allocate tcm_loop_primary\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700493 return PTR_ERR(tcm_loop_primary);
494 }
495
496 ret = bus_register(&tcm_loop_lld_bus);
497 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700498 pr_err("bus_register() failed for tcm_loop_lld_bus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700499 goto dev_unreg;
500 }
501
502 ret = driver_register(&tcm_loop_driverfs);
503 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700504 pr_err("driver_register() failed for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700505 "tcm_loop_driverfs\n");
506 goto bus_unreg;
507 }
508
Andy Grover6708bb22011-06-08 10:36:43 -0700509 pr_debug("Initialized TCM Loop Core Bus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700510 return ret;
511
512bus_unreg:
513 bus_unregister(&tcm_loop_lld_bus);
514dev_unreg:
515 root_device_unregister(tcm_loop_primary);
516 return ret;
517}
518
519static void tcm_loop_release_core_bus(void)
520{
521 driver_unregister(&tcm_loop_driverfs);
522 bus_unregister(&tcm_loop_lld_bus);
523 root_device_unregister(tcm_loop_primary);
524
Andy Grover6708bb22011-06-08 10:36:43 -0700525 pr_debug("Releasing TCM Loop Core BUS\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700526}
527
528static char *tcm_loop_get_fabric_name(void)
529{
530 return "loopback";
531}
532
533static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
534{
Jörn Engel8359cf42011-11-24 02:05:51 +0100535 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700536 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
537 /*
538 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
539 * time based on the protocol dependent prefix of the passed configfs group.
540 *
541 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
542 * ProtocolID using target_core_fabric_lib.c symbols.
543 */
544 switch (tl_hba->tl_proto_id) {
545 case SCSI_PROTOCOL_SAS:
546 return sas_get_fabric_proto_ident(se_tpg);
547 case SCSI_PROTOCOL_FCP:
548 return fc_get_fabric_proto_ident(se_tpg);
549 case SCSI_PROTOCOL_ISCSI:
550 return iscsi_get_fabric_proto_ident(se_tpg);
551 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700552 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700553 " SAS emulation\n", tl_hba->tl_proto_id);
554 break;
555 }
556
557 return sas_get_fabric_proto_ident(se_tpg);
558}
559
560static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
561{
Jörn Engel8359cf42011-11-24 02:05:51 +0100562 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700563 /*
564 * Return the passed NAA identifier for the SAS Target Port
565 */
566 return &tl_tpg->tl_hba->tl_wwn_address[0];
567}
568
569static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
570{
Jörn Engel8359cf42011-11-24 02:05:51 +0100571 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700572 /*
573 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
574 * to represent the SCSI Target Port.
575 */
576 return tl_tpg->tl_tpgt;
577}
578
579static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
580{
581 return 1;
582}
583
584static u32 tcm_loop_get_pr_transport_id(
585 struct se_portal_group *se_tpg,
586 struct se_node_acl *se_nacl,
587 struct t10_pr_registration *pr_reg,
588 int *format_code,
589 unsigned char *buf)
590{
Jörn Engel8359cf42011-11-24 02:05:51 +0100591 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700592 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
593
594 switch (tl_hba->tl_proto_id) {
595 case SCSI_PROTOCOL_SAS:
596 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
597 format_code, buf);
598 case SCSI_PROTOCOL_FCP:
599 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
600 format_code, buf);
601 case SCSI_PROTOCOL_ISCSI:
602 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
603 format_code, buf);
604 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700605 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700606 " SAS emulation\n", tl_hba->tl_proto_id);
607 break;
608 }
609
610 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
611 format_code, buf);
612}
613
614static u32 tcm_loop_get_pr_transport_id_len(
615 struct se_portal_group *se_tpg,
616 struct se_node_acl *se_nacl,
617 struct t10_pr_registration *pr_reg,
618 int *format_code)
619{
Jörn Engel8359cf42011-11-24 02:05:51 +0100620 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700621 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
622
623 switch (tl_hba->tl_proto_id) {
624 case SCSI_PROTOCOL_SAS:
625 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
626 format_code);
627 case SCSI_PROTOCOL_FCP:
628 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
629 format_code);
630 case SCSI_PROTOCOL_ISCSI:
631 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
632 format_code);
633 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700634 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700635 " SAS emulation\n", tl_hba->tl_proto_id);
636 break;
637 }
638
639 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
640 format_code);
641}
642
643/*
644 * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
645 * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
646 */
647static char *tcm_loop_parse_pr_out_transport_id(
648 struct se_portal_group *se_tpg,
649 const char *buf,
650 u32 *out_tid_len,
651 char **port_nexus_ptr)
652{
Jörn Engel8359cf42011-11-24 02:05:51 +0100653 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700654 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
655
656 switch (tl_hba->tl_proto_id) {
657 case SCSI_PROTOCOL_SAS:
658 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
659 port_nexus_ptr);
660 case SCSI_PROTOCOL_FCP:
661 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
662 port_nexus_ptr);
663 case SCSI_PROTOCOL_ISCSI:
664 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
665 port_nexus_ptr);
666 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700667 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700668 " SAS emulation\n", tl_hba->tl_proto_id);
669 break;
670 }
671
672 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
673 port_nexus_ptr);
674}
675
676/*
677 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
678 * based upon the incoming fabric dependent SCSI Initiator Port
679 */
680static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
681{
682 return 1;
683}
684
685static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
686{
687 return 0;
688}
689
690/*
691 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
692 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
693 */
694static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
695{
696 return 0;
697}
698
699/*
700 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
701 * never be called for TCM_Loop by target_core_fabric_configfs.c code.
702 * It has been added here as a nop for target_fabric_tf_ops_check()
703 */
704static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
705{
706 return 0;
707}
708
709static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
710 struct se_portal_group *se_tpg)
711{
712 struct tcm_loop_nacl *tl_nacl;
713
714 tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
715 if (!tl_nacl) {
Andy Grover6708bb22011-06-08 10:36:43 -0700716 pr_err("Unable to allocate struct tcm_loop_nacl\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700717 return NULL;
718 }
719
720 return &tl_nacl->se_node_acl;
721}
722
723static void tcm_loop_tpg_release_fabric_acl(
724 struct se_portal_group *se_tpg,
725 struct se_node_acl *se_nacl)
726{
727 struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
728 struct tcm_loop_nacl, se_node_acl);
729
730 kfree(tl_nacl);
731}
732
733static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
734{
735 return 1;
736}
737
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700738static int tcm_loop_is_state_remove(struct se_cmd *se_cmd)
739{
740 /*
741 * Assume struct scsi_cmnd is not in remove state..
742 */
743 return 0;
744}
745
746static int tcm_loop_sess_logged_in(struct se_session *se_sess)
747{
748 /*
749 * Assume that TL Nexus is always active
750 */
751 return 1;
752}
753
754static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
755{
756 return 1;
757}
758
759static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
760{
761 return;
762}
763
764static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
765{
766 return 1;
767}
768
769static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
770{
771 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
772 struct tcm_loop_cmd, tl_se_cmd);
773
774 return tl_cmd->sc_cmd_state;
775}
776
777static int tcm_loop_shutdown_session(struct se_session *se_sess)
778{
779 return 0;
780}
781
782static void tcm_loop_close_session(struct se_session *se_sess)
783{
784 return;
785};
786
787static void tcm_loop_stop_session(
788 struct se_session *se_sess,
789 int sess_sleep,
790 int conn_sleep)
791{
792 return;
793}
794
795static void tcm_loop_fall_back_to_erl0(struct se_session *se_sess)
796{
797 return;
798}
799
800static int tcm_loop_write_pending(struct se_cmd *se_cmd)
801{
802 /*
803 * Since Linux/SCSI has already sent down a struct scsi_cmnd
804 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
805 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
806 * format with transport_generic_map_mem_to_cmd().
807 *
808 * We now tell TCM to add this WRITE CDB directly into the TCM storage
809 * object execution queue.
810 */
811 transport_generic_process_write(se_cmd);
812 return 0;
813}
814
815static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
816{
817 return 0;
818}
819
820static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
821{
822 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
823 struct tcm_loop_cmd, tl_se_cmd);
824 struct scsi_cmnd *sc = tl_cmd->sc;
825
Andy Grover6708bb22011-06-08 10:36:43 -0700826 pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700827 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
828
829 sc->result = SAM_STAT_GOOD;
830 set_host_byte(sc, DID_OK);
831 sc->scsi_done(sc);
832 return 0;
833}
834
835static int tcm_loop_queue_status(struct se_cmd *se_cmd)
836{
837 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
838 struct tcm_loop_cmd, tl_se_cmd);
839 struct scsi_cmnd *sc = tl_cmd->sc;
840
Andy Grover6708bb22011-06-08 10:36:43 -0700841 pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700842 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
843
844 if (se_cmd->sense_buffer &&
845 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
846 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
847
Andy Grover5951146d2011-07-19 10:26:37 +0000848 memcpy(sc->sense_buffer, se_cmd->sense_buffer,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700849 SCSI_SENSE_BUFFERSIZE);
850 sc->result = SAM_STAT_CHECK_CONDITION;
851 set_driver_byte(sc, DRIVER_SENSE);
852 } else
853 sc->result = se_cmd->scsi_status;
854
855 set_host_byte(sc, DID_OK);
856 sc->scsi_done(sc);
857 return 0;
858}
859
860static int tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
861{
862 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
863 struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
864 /*
865 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
866 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
867 */
868 atomic_set(&tl_tmr->tmr_complete, 1);
869 wake_up(&tl_tmr->tl_tmr_wait);
870 return 0;
871}
872
873static u16 tcm_loop_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
874{
875 return 0;
876}
877
878static u16 tcm_loop_get_fabric_sense_len(void)
879{
880 return 0;
881}
882
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700883static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
884{
885 switch (tl_hba->tl_proto_id) {
886 case SCSI_PROTOCOL_SAS:
887 return "SAS";
888 case SCSI_PROTOCOL_FCP:
889 return "FCP";
890 case SCSI_PROTOCOL_ISCSI:
891 return "iSCSI";
892 default:
893 break;
894 }
895
896 return "Unknown";
897}
898
899/* Start items for tcm_loop_port_cit */
900
901static int tcm_loop_port_link(
902 struct se_portal_group *se_tpg,
903 struct se_lun *lun)
904{
905 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
906 struct tcm_loop_tpg, tl_se_tpg);
907 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
908
909 atomic_inc(&tl_tpg->tl_tpg_port_count);
910 smp_mb__after_atomic_inc();
911 /*
912 * Add Linux/SCSI struct scsi_device by HCTL
913 */
914 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
915
Andy Grover6708bb22011-06-08 10:36:43 -0700916 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700917 return 0;
918}
919
920static void tcm_loop_port_unlink(
921 struct se_portal_group *se_tpg,
922 struct se_lun *se_lun)
923{
924 struct scsi_device *sd;
925 struct tcm_loop_hba *tl_hba;
926 struct tcm_loop_tpg *tl_tpg;
927
928 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
929 tl_hba = tl_tpg->tl_hba;
930
931 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
932 se_lun->unpacked_lun);
933 if (!sd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700934 pr_err("Unable to locate struct scsi_device for %d:%d:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700935 "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
936 return;
937 }
938 /*
939 * Remove Linux/SCSI struct scsi_device by HCTL
940 */
941 scsi_remove_device(sd);
942 scsi_device_put(sd);
943
944 atomic_dec(&tl_tpg->tl_tpg_port_count);
945 smp_mb__after_atomic_dec();
946
Andy Grover6708bb22011-06-08 10:36:43 -0700947 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700948}
949
950/* End items for tcm_loop_port_cit */
951
952/* Start items for tcm_loop_nexus_cit */
953
954static int tcm_loop_make_nexus(
955 struct tcm_loop_tpg *tl_tpg,
956 const char *name)
957{
958 struct se_portal_group *se_tpg;
959 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
960 struct tcm_loop_nexus *tl_nexus;
Dan Carpenter552523d2011-06-15 09:41:33 -0700961 int ret = -ENOMEM;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700962
963 if (tl_tpg->tl_hba->tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700964 pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700965 return -EEXIST;
966 }
967 se_tpg = &tl_tpg->tl_se_tpg;
968
969 tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
970 if (!tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700971 pr_err("Unable to allocate struct tcm_loop_nexus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700972 return -ENOMEM;
973 }
974 /*
975 * Initialize the struct se_session pointer
976 */
977 tl_nexus->se_sess = transport_init_session();
Dan Carpenter552523d2011-06-15 09:41:33 -0700978 if (IS_ERR(tl_nexus->se_sess)) {
979 ret = PTR_ERR(tl_nexus->se_sess);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700980 goto out;
Dan Carpenter552523d2011-06-15 09:41:33 -0700981 }
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700982 /*
983 * Since we are running in 'demo mode' this call with generate a
984 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
985 * Initiator port name of the passed configfs group 'name'.
986 */
987 tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
988 se_tpg, (unsigned char *)name);
989 if (!tl_nexus->se_sess->se_node_acl) {
990 transport_free_session(tl_nexus->se_sess);
991 goto out;
992 }
993 /*
994 * Now, register the SAS I_T Nexus as active with the call to
995 * transport_register_session()
996 */
997 __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
Andy Grover5951146d2011-07-19 10:26:37 +0000998 tl_nexus->se_sess, tl_nexus);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700999 tl_tpg->tl_hba->tl_nexus = tl_nexus;
Andy Grover6708bb22011-06-08 10:36:43 -07001000 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001001 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
1002 name);
1003 return 0;
1004
1005out:
1006 kfree(tl_nexus);
Dan Carpenter552523d2011-06-15 09:41:33 -07001007 return ret;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001008}
1009
1010static int tcm_loop_drop_nexus(
1011 struct tcm_loop_tpg *tpg)
1012{
1013 struct se_session *se_sess;
1014 struct tcm_loop_nexus *tl_nexus;
1015 struct tcm_loop_hba *tl_hba = tpg->tl_hba;
1016
1017 tl_nexus = tpg->tl_hba->tl_nexus;
1018 if (!tl_nexus)
1019 return -ENODEV;
1020
1021 se_sess = tl_nexus->se_sess;
1022 if (!se_sess)
1023 return -ENODEV;
1024
1025 if (atomic_read(&tpg->tl_tpg_port_count)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001026 pr_err("Unable to remove TCM_Loop I_T Nexus with"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001027 " active TPG port count: %d\n",
1028 atomic_read(&tpg->tl_tpg_port_count));
1029 return -EPERM;
1030 }
1031
Andy Grover6708bb22011-06-08 10:36:43 -07001032 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001033 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
1034 tl_nexus->se_sess->se_node_acl->initiatorname);
1035 /*
1036 * Release the SCSI I_T Nexus to the emulated SAS Target Port
1037 */
1038 transport_deregister_session(tl_nexus->se_sess);
1039 tpg->tl_hba->tl_nexus = NULL;
1040 kfree(tl_nexus);
1041 return 0;
1042}
1043
1044/* End items for tcm_loop_nexus_cit */
1045
1046static ssize_t tcm_loop_tpg_show_nexus(
1047 struct se_portal_group *se_tpg,
1048 char *page)
1049{
1050 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1051 struct tcm_loop_tpg, tl_se_tpg);
1052 struct tcm_loop_nexus *tl_nexus;
1053 ssize_t ret;
1054
1055 tl_nexus = tl_tpg->tl_hba->tl_nexus;
1056 if (!tl_nexus)
1057 return -ENODEV;
1058
1059 ret = snprintf(page, PAGE_SIZE, "%s\n",
1060 tl_nexus->se_sess->se_node_acl->initiatorname);
1061
1062 return ret;
1063}
1064
1065static ssize_t tcm_loop_tpg_store_nexus(
1066 struct se_portal_group *se_tpg,
1067 const char *page,
1068 size_t count)
1069{
1070 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1071 struct tcm_loop_tpg, tl_se_tpg);
1072 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
1073 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
1074 int ret;
1075 /*
1076 * Shutdown the active I_T nexus if 'NULL' is passed..
1077 */
1078 if (!strncmp(page, "NULL", 4)) {
1079 ret = tcm_loop_drop_nexus(tl_tpg);
1080 return (!ret) ? count : ret;
1081 }
1082 /*
1083 * Otherwise make sure the passed virtual Initiator port WWN matches
1084 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
1085 * tcm_loop_make_nexus()
1086 */
Dan Carpenter60d645a2011-06-15 10:03:05 -07001087 if (strlen(page) >= TL_WWN_ADDR_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001088 pr_err("Emulated NAA Sas Address: %s, exceeds"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001089 " max: %d\n", page, TL_WWN_ADDR_LEN);
1090 return -EINVAL;
1091 }
1092 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
1093
1094 ptr = strstr(i_port, "naa.");
1095 if (ptr) {
1096 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
Andy Grover6708bb22011-06-08 10:36:43 -07001097 pr_err("Passed SAS Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001098 " match target port protoid: %s\n", i_port,
1099 tcm_loop_dump_proto_id(tl_hba));
1100 return -EINVAL;
1101 }
1102 port_ptr = &i_port[0];
1103 goto check_newline;
1104 }
1105 ptr = strstr(i_port, "fc.");
1106 if (ptr) {
1107 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
Andy Grover6708bb22011-06-08 10:36:43 -07001108 pr_err("Passed FCP Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001109 " match target port protoid: %s\n", i_port,
1110 tcm_loop_dump_proto_id(tl_hba));
1111 return -EINVAL;
1112 }
1113 port_ptr = &i_port[3]; /* Skip over "fc." */
1114 goto check_newline;
1115 }
1116 ptr = strstr(i_port, "iqn.");
1117 if (ptr) {
1118 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
Andy Grover6708bb22011-06-08 10:36:43 -07001119 pr_err("Passed iSCSI Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001120 " match target port protoid: %s\n", i_port,
1121 tcm_loop_dump_proto_id(tl_hba));
1122 return -EINVAL;
1123 }
1124 port_ptr = &i_port[0];
1125 goto check_newline;
1126 }
Andy Grover6708bb22011-06-08 10:36:43 -07001127 pr_err("Unable to locate prefix for emulated Initiator Port:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001128 " %s\n", i_port);
1129 return -EINVAL;
1130 /*
1131 * Clear any trailing newline for the NAA WWN
1132 */
1133check_newline:
1134 if (i_port[strlen(i_port)-1] == '\n')
1135 i_port[strlen(i_port)-1] = '\0';
1136
1137 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
1138 if (ret < 0)
1139 return ret;
1140
1141 return count;
1142}
1143
1144TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
1145
1146static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
1147 &tcm_loop_tpg_nexus.attr,
1148 NULL,
1149};
1150
1151/* Start items for tcm_loop_naa_cit */
1152
1153struct se_portal_group *tcm_loop_make_naa_tpg(
1154 struct se_wwn *wwn,
1155 struct config_group *group,
1156 const char *name)
1157{
1158 struct tcm_loop_hba *tl_hba = container_of(wwn,
1159 struct tcm_loop_hba, tl_hba_wwn);
1160 struct tcm_loop_tpg *tl_tpg;
1161 char *tpgt_str, *end_ptr;
1162 int ret;
1163 unsigned short int tpgt;
1164
1165 tpgt_str = strstr(name, "tpgt_");
1166 if (!tpgt_str) {
Andy Grover6708bb22011-06-08 10:36:43 -07001167 pr_err("Unable to locate \"tpgt_#\" directory"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001168 " group\n");
1169 return ERR_PTR(-EINVAL);
1170 }
1171 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1172 tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1173
Dan Carpenter12f09cc2011-04-02 14:32:47 -07001174 if (tpgt >= TL_TPGS_PER_HBA) {
Andy Grover6708bb22011-06-08 10:36:43 -07001175 pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001176 " %u\n", tpgt, TL_TPGS_PER_HBA);
1177 return ERR_PTR(-EINVAL);
1178 }
1179 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
1180 tl_tpg->tl_hba = tl_hba;
1181 tl_tpg->tl_tpgt = tpgt;
1182 /*
1183 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
1184 */
1185 ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
Andy Grover5951146d2011-07-19 10:26:37 +00001186 wwn, &tl_tpg->tl_se_tpg, tl_tpg,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001187 TRANSPORT_TPG_TYPE_NORMAL);
1188 if (ret < 0)
1189 return ERR_PTR(-ENOMEM);
1190
Andy Grover6708bb22011-06-08 10:36:43 -07001191 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001192 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1193 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1194
1195 return &tl_tpg->tl_se_tpg;
1196}
1197
1198void tcm_loop_drop_naa_tpg(
1199 struct se_portal_group *se_tpg)
1200{
1201 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1202 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1203 struct tcm_loop_tpg, tl_se_tpg);
1204 struct tcm_loop_hba *tl_hba;
1205 unsigned short tpgt;
1206
1207 tl_hba = tl_tpg->tl_hba;
1208 tpgt = tl_tpg->tl_tpgt;
1209 /*
1210 * Release the I_T Nexus for the Virtual SAS link if present
1211 */
1212 tcm_loop_drop_nexus(tl_tpg);
1213 /*
1214 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
1215 */
1216 core_tpg_deregister(se_tpg);
1217
Nicholas Bellinger0a020432011-10-10 19:44:05 -07001218 tl_tpg->tl_hba = NULL;
1219 tl_tpg->tl_tpgt = 0;
1220
Andy Grover6708bb22011-06-08 10:36:43 -07001221 pr_debug("TCM_Loop_ConfigFS: Deallocated 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
1226/* End items for tcm_loop_naa_cit */
1227
1228/* Start items for tcm_loop_cit */
1229
1230struct se_wwn *tcm_loop_make_scsi_hba(
1231 struct target_fabric_configfs *tf,
1232 struct config_group *group,
1233 const char *name)
1234{
1235 struct tcm_loop_hba *tl_hba;
1236 struct Scsi_Host *sh;
1237 char *ptr;
1238 int ret, off = 0;
1239
1240 tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
1241 if (!tl_hba) {
Andy Grover6708bb22011-06-08 10:36:43 -07001242 pr_err("Unable to allocate struct tcm_loop_hba\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001243 return ERR_PTR(-ENOMEM);
1244 }
1245 /*
1246 * Determine the emulated Protocol Identifier and Target Port Name
1247 * based on the incoming configfs directory name.
1248 */
1249 ptr = strstr(name, "naa.");
1250 if (ptr) {
1251 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1252 goto check_len;
1253 }
1254 ptr = strstr(name, "fc.");
1255 if (ptr) {
1256 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1257 off = 3; /* Skip over "fc." */
1258 goto check_len;
1259 }
1260 ptr = strstr(name, "iqn.");
Jesper Juhla57b5d32011-06-28 00:30:17 +02001261 if (!ptr) {
Andy Grover6708bb22011-06-08 10:36:43 -07001262 pr_err("Unable to locate prefix for emulated Target "
Jesper Juhla57b5d32011-06-28 00:30:17 +02001263 "Port: %s\n", name);
1264 ret = -EINVAL;
1265 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001266 }
Jesper Juhla57b5d32011-06-28 00:30:17 +02001267 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001268
1269check_len:
Dan Carpenter60d645a2011-06-15 10:03:05 -07001270 if (strlen(name) >= TL_WWN_ADDR_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001271 pr_err("Emulated NAA %s Address: %s, exceeds"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001272 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1273 TL_WWN_ADDR_LEN);
Jesper Juhla57b5d32011-06-28 00:30:17 +02001274 ret = -EINVAL;
1275 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001276 }
1277 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1278
1279 /*
1280 * Call device_register(tl_hba->dev) to register the emulated
1281 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1282 * device_register() callbacks in tcm_loop_driver_probe()
1283 */
1284 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1285 if (ret)
1286 goto out;
1287
1288 sh = tl_hba->sh;
1289 tcm_loop_hba_no_cnt++;
Andy Grover6708bb22011-06-08 10:36:43 -07001290 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001291 " %s Address: %s at Linux/SCSI Host ID: %d\n",
1292 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1293
1294 return &tl_hba->tl_hba_wwn;
1295out:
1296 kfree(tl_hba);
1297 return ERR_PTR(ret);
1298}
1299
1300void tcm_loop_drop_scsi_hba(
1301 struct se_wwn *wwn)
1302{
1303 struct tcm_loop_hba *tl_hba = container_of(wwn,
1304 struct tcm_loop_hba, tl_hba_wwn);
Nicholas Bellinger6297b072011-11-12 09:29:51 -08001305
1306 pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
1307 " SAS Address: %s at Linux/SCSI Host ID: %d\n",
1308 tl_hba->tl_wwn_address, tl_hba->sh->host_no);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001309 /*
1310 * Call device_unregister() on the original tl_hba->dev.
1311 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1312 * release *tl_hba;
1313 */
1314 device_unregister(&tl_hba->dev);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001315}
1316
1317/* Start items for tcm_loop_cit */
1318static ssize_t tcm_loop_wwn_show_attr_version(
1319 struct target_fabric_configfs *tf,
1320 char *page)
1321{
1322 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1323}
1324
1325TF_WWN_ATTR_RO(tcm_loop, version);
1326
1327static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1328 &tcm_loop_wwn_version.attr,
1329 NULL,
1330};
1331
1332/* End items for tcm_loop_cit */
1333
1334static int tcm_loop_register_configfs(void)
1335{
1336 struct target_fabric_configfs *fabric;
1337 struct config_group *tf_cg;
1338 int ret;
1339 /*
1340 * Set the TCM Loop HBA counter to zero
1341 */
1342 tcm_loop_hba_no_cnt = 0;
1343 /*
1344 * Register the top level struct config_item_type with TCM core
1345 */
1346 fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
Andy Grovere3d6f902011-07-19 08:55:10 +00001347 if (IS_ERR(fabric)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001348 pr_err("tcm_loop_register_configfs() failed!\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001349 return PTR_ERR(fabric);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001350 }
1351 /*
1352 * Setup the fabric API of function pointers used by target_core_mod
1353 */
1354 fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
1355 fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
1356 fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
1357 fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
1358 fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
1359 fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
1360 fabric->tf_ops.tpg_get_pr_transport_id_len =
1361 &tcm_loop_get_pr_transport_id_len;
1362 fabric->tf_ops.tpg_parse_pr_out_transport_id =
1363 &tcm_loop_parse_pr_out_transport_id;
1364 fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
1365 fabric->tf_ops.tpg_check_demo_mode_cache =
1366 &tcm_loop_check_demo_mode_cache;
1367 fabric->tf_ops.tpg_check_demo_mode_write_protect =
1368 &tcm_loop_check_demo_mode_write_protect;
1369 fabric->tf_ops.tpg_check_prod_mode_write_protect =
1370 &tcm_loop_check_prod_mode_write_protect;
1371 /*
1372 * The TCM loopback fabric module runs in demo-mode to a local
1373 * virtual SCSI device, so fabric dependent initator ACLs are
1374 * not required.
1375 */
1376 fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
1377 fabric->tf_ops.tpg_release_fabric_acl =
1378 &tcm_loop_tpg_release_fabric_acl;
1379 fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
1380 /*
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001381 * Used for setting up remaining TCM resources in process context
1382 */
1383 fabric->tf_ops.new_cmd_map = &tcm_loop_new_cmd_map;
1384 fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
Christoph Hellwig35462972011-05-31 23:56:57 -04001385 fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001386 fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
1387 fabric->tf_ops.close_session = &tcm_loop_close_session;
1388 fabric->tf_ops.stop_session = &tcm_loop_stop_session;
1389 fabric->tf_ops.fall_back_to_erl0 = &tcm_loop_fall_back_to_erl0;
1390 fabric->tf_ops.sess_logged_in = &tcm_loop_sess_logged_in;
1391 fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
1392 fabric->tf_ops.sess_get_initiator_sid = NULL;
1393 fabric->tf_ops.write_pending = &tcm_loop_write_pending;
1394 fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
1395 /*
1396 * Not used for TCM loopback
1397 */
1398 fabric->tf_ops.set_default_node_attributes =
1399 &tcm_loop_set_default_node_attributes;
1400 fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
1401 fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001402 fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
1403 fabric->tf_ops.queue_status = &tcm_loop_queue_status;
1404 fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
1405 fabric->tf_ops.set_fabric_sense_len = &tcm_loop_set_fabric_sense_len;
1406 fabric->tf_ops.get_fabric_sense_len = &tcm_loop_get_fabric_sense_len;
1407 fabric->tf_ops.is_state_remove = &tcm_loop_is_state_remove;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001408
1409 tf_cg = &fabric->tf_group;
1410 /*
1411 * Setup function pointers for generic logic in target_core_fabric_configfs.c
1412 */
1413 fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
1414 fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
1415 fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
1416 fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
1417 /*
1418 * fabric_post_link() and fabric_pre_unlink() are used for
1419 * registration and release of TCM Loop Virtual SCSI LUNs.
1420 */
1421 fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
1422 fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
1423 fabric->tf_ops.fabric_make_np = NULL;
1424 fabric->tf_ops.fabric_drop_np = NULL;
1425 /*
1426 * Setup default attribute lists for various fabric->tf_cit_tmpl
1427 */
1428 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
1429 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
1430 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1431 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1432 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1433 /*
1434 * Once fabric->tf_ops has been setup, now register the fabric for
1435 * use within TCM
1436 */
1437 ret = target_fabric_configfs_register(fabric);
1438 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001439 pr_err("target_fabric_configfs_register() for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001440 " TCM_Loop failed!\n");
1441 target_fabric_configfs_free(fabric);
1442 return -1;
1443 }
1444 /*
1445 * Setup our local pointer to *fabric.
1446 */
1447 tcm_loop_fabric_configfs = fabric;
Andy Grover6708bb22011-06-08 10:36:43 -07001448 pr_debug("TCM_LOOP[0] - Set fabric ->"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001449 " tcm_loop_fabric_configfs\n");
1450 return 0;
1451}
1452
1453static void tcm_loop_deregister_configfs(void)
1454{
1455 if (!tcm_loop_fabric_configfs)
1456 return;
1457
1458 target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
1459 tcm_loop_fabric_configfs = NULL;
Andy Grover6708bb22011-06-08 10:36:43 -07001460 pr_debug("TCM_LOOP[0] - Cleared"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001461 " tcm_loop_fabric_configfs\n");
1462}
1463
1464static int __init tcm_loop_fabric_init(void)
1465{
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001466 int ret = -ENOMEM;
1467
1468 tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
1469 if (!tcm_loop_workqueue)
1470 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001471
1472 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1473 sizeof(struct tcm_loop_cmd),
1474 __alignof__(struct tcm_loop_cmd),
1475 0, NULL);
1476 if (!tcm_loop_cmd_cache) {
Andy Grover6708bb22011-06-08 10:36:43 -07001477 pr_debug("kmem_cache_create() for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001478 " tcm_loop_cmd_cache failed\n");
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001479 goto out_destroy_workqueue;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001480 }
1481
1482 ret = tcm_loop_alloc_core_bus();
1483 if (ret)
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001484 goto out_destroy_cache;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001485
1486 ret = tcm_loop_register_configfs();
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001487 if (ret)
1488 goto out_release_core_bus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001489
1490 return 0;
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001491
1492out_release_core_bus:
1493 tcm_loop_release_core_bus();
1494out_destroy_cache:
1495 kmem_cache_destroy(tcm_loop_cmd_cache);
1496out_destroy_workqueue:
1497 destroy_workqueue(tcm_loop_workqueue);
1498out:
1499 return ret;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001500}
1501
1502static void __exit tcm_loop_fabric_exit(void)
1503{
1504 tcm_loop_deregister_configfs();
1505 tcm_loop_release_core_bus();
1506 kmem_cache_destroy(tcm_loop_cmd_cache);
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001507 destroy_workqueue(tcm_loop_workqueue);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001508}
1509
1510MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1511MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1512MODULE_LICENSE("GPL");
1513module_init(tcm_loop_fabric_init);
1514module_exit(tcm_loop_fabric_exit);