blob: b1edc517f4e3bb0e7bdac148dcbfd4355d9ff038 [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
47static struct kmem_cache *tcm_loop_cmd_cache;
48
49static int tcm_loop_hba_no_cnt;
50
51/*
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070052 * Called by struct target_core_fabric_ops->new_cmd_map()
53 *
54 * Always called in process context. A non zero return value
55 * here will signal to handle an exception based on the return code.
56 */
57static int tcm_loop_new_cmd_map(struct se_cmd *se_cmd)
58{
59 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
60 struct tcm_loop_cmd, tl_se_cmd);
61 struct scsi_cmnd *sc = tl_cmd->sc;
Andy Grover5951146d2011-07-19 10:26:37 +000062 struct scatterlist *sgl_bidi = NULL;
63 u32 sgl_bidi_count = 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070064 int ret;
65 /*
66 * Allocate the necessary tasks to complete the received CDB+data
67 */
Andy Grover5951146d2011-07-19 10:26:37 +000068 ret = transport_generic_allocate_tasks(se_cmd, sc->cmnd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -070069 if (ret != 0)
70 return ret;
Andy Grover5951146d2011-07-19 10:26:37 +000071 /*
72 * For BIDI commands, pass in the extra READ buffer
73 * to transport_generic_map_mem_to_cmd() below..
74 */
Christoph Hellwig33c3faf2011-11-14 11:36:30 -050075 if (se_cmd->se_cmd_flags & SCF_BIDI) {
Andy Grover5951146d2011-07-19 10:26:37 +000076 struct scsi_data_buffer *sdb = scsi_in(sc);
77
78 sgl_bidi = sdb->table.sgl;
79 sgl_bidi_count = sdb->table.nents;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070080 }
Nicholas Bellinger8cd79f22011-10-24 13:35:37 -070081 /*
82 * Because some userspace code via scsi-generic do not memset their
83 * associated read buffers, go ahead and do that here for type
84 * SCF_SCSI_CONTROL_SG_IO_CDB. Also note that this is currently
85 * guaranteed to be a single SGL for SCF_SCSI_CONTROL_SG_IO_CDB
86 * by target core in transport_generic_allocate_tasks() ->
87 * transport_generic_cmd_sequencer().
88 */
89 if (se_cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB &&
90 se_cmd->data_direction == DMA_FROM_DEVICE) {
91 struct scatterlist *sg = scsi_sglist(sc);
92 unsigned char *buf = kmap(sg_page(sg)) + sg->offset;
93
94 if (buf != NULL) {
95 memset(buf, 0, sg->length);
96 kunmap(sg_page(sg));
97 }
98 }
Andy Grover5951146d2011-07-19 10:26:37 +000099
Andy Groverec98f782011-07-20 19:28:46 +0000100 /* Tell the core about our preallocated memory */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700101 return transport_generic_map_mem_to_cmd(se_cmd, scsi_sglist(sc),
Andy Grover5951146d2011-07-19 10:26:37 +0000102 scsi_sg_count(sc), sgl_bidi, sgl_bidi_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700103}
104
105/*
106 * Called from struct target_core_fabric_ops->check_stop_free()
107 */
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700108static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700109{
110 /*
111 * Do not release struct se_cmd's containing a valid TMR
112 * pointer. These will be released directly in tcm_loop_device_reset()
113 * with transport_generic_free_cmd().
114 */
Andy Groverc8e31f22012-01-19 13:39:17 -0800115 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700116 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700117 /*
118 * Release the struct se_cmd, which will make a callback to release
119 * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd()
120 */
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +0200121 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700122 return 1;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700123}
124
Christoph Hellwig35462972011-05-31 23:56:57 -0400125static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700126{
127 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
128 struct tcm_loop_cmd, tl_se_cmd);
129
130 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
131}
132
133static int tcm_loop_proc_info(struct Scsi_Host *host, char *buffer,
134 char **start, off_t offset,
135 int length, int inout)
136{
137 return sprintf(buffer, "tcm_loop_proc_info()\n");
138}
139
140static int tcm_loop_driver_probe(struct device *);
141static int tcm_loop_driver_remove(struct device *);
142
143static int pseudo_lld_bus_match(struct device *dev,
144 struct device_driver *dev_driver)
145{
146 return 1;
147}
148
149static struct bus_type tcm_loop_lld_bus = {
150 .name = "tcm_loop_bus",
151 .match = pseudo_lld_bus_match,
152 .probe = tcm_loop_driver_probe,
153 .remove = tcm_loop_driver_remove,
154};
155
156static struct device_driver tcm_loop_driverfs = {
157 .name = "tcm_loop",
158 .bus = &tcm_loop_lld_bus,
159};
160/*
161 * Used with root_device_register() in tcm_loop_alloc_core_bus() below
162 */
163struct device *tcm_loop_primary;
164
165/*
166 * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and
167 * drivers/scsi/libiscsi.c:iscsi_change_queue_depth()
168 */
169static int tcm_loop_change_queue_depth(
170 struct scsi_device *sdev,
171 int depth,
172 int reason)
173{
174 switch (reason) {
175 case SCSI_QDEPTH_DEFAULT:
176 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
177 break;
178 case SCSI_QDEPTH_QFULL:
179 scsi_track_queue_full(sdev, depth);
180 break;
181 case SCSI_QDEPTH_RAMP_UP:
182 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
183 break;
184 default:
185 return -EOPNOTSUPP;
186 }
187 return sdev->queue_depth;
188}
189
190/*
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500191 * Locate the SAM Task Attr from struct scsi_cmnd *
192 */
193static int tcm_loop_sam_attr(struct scsi_cmnd *sc)
194{
195 if (sc->device->tagged_supported) {
196 switch (sc->tag) {
197 case HEAD_OF_QUEUE_TAG:
198 return MSG_HEAD_TAG;
199 case ORDERED_QUEUE_TAG:
200 return MSG_ORDERED_TAG;
201 default:
202 break;
203 }
204 }
205
206 return MSG_SIMPLE_TAG;
207}
208
209/*
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700210 * Main entry point from struct scsi_host_template for incoming SCSI CDB+Data
211 * from Linux/SCSI subsystem for SCSI low level device drivers (LLDs)
212 */
213static int tcm_loop_queuecommand(
214 struct Scsi_Host *sh,
215 struct scsi_cmnd *sc)
216{
217 struct se_cmd *se_cmd;
218 struct se_portal_group *se_tpg;
219 struct tcm_loop_hba *tl_hba;
220 struct tcm_loop_tpg *tl_tpg;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500221 struct se_session *se_sess;
222 struct tcm_loop_nexus *tl_nexus;
223 struct tcm_loop_cmd *tl_cmd;
224
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700225
Andy Grover6708bb22011-06-08 10:36:43 -0700226 pr_debug("tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700227 " scsi_buf_len: %u\n", sc->device->host->host_no,
228 sc->device->id, sc->device->channel, sc->device->lun,
229 sc->cmnd[0], scsi_bufflen(sc));
230 /*
231 * Locate the tcm_loop_hba_t pointer
232 */
233 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
234 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
Nicholas Bellinger0a020432011-10-10 19:44:05 -0700235 /*
236 * Ensure that this tl_tpg reference from the incoming sc->device->id
237 * has already been configured via tcm_loop_make_naa_tpg().
238 */
239 if (!tl_tpg->tl_hba) {
240 set_host_byte(sc, DID_NO_CONNECT);
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500241 goto out_done;
Nicholas Bellinger0a020432011-10-10 19:44:05 -0700242 }
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700243 se_tpg = &tl_tpg->tl_se_tpg;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500244
245 tl_nexus = tl_hba->tl_nexus;
246 if (!tl_nexus) {
247 scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
248 " does not exist\n");
249 set_host_byte(sc, DID_ERROR);
250 goto out_done;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700251 }
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500252 se_sess = tl_nexus->se_sess;
253
254 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
255 if (!tl_cmd) {
256 pr_err("Unable to allocate struct tcm_loop_cmd\n");
257 set_host_byte(sc, DID_ERROR);
258 goto out_done;
259 }
260 se_cmd = &tl_cmd->tl_se_cmd;
261 tl_cmd->sc = sc;
262
263 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
264 scsi_bufflen(sc), sc->sc_data_direction,
265 tcm_loop_sam_attr(sc), &tl_cmd->tl_sense_buf[0]);
266
267 if (scsi_bidi_cmnd(sc))
268 se_cmd->se_cmd_flags |= SCF_BIDI;
269
270 if (transport_lookup_cmd_lun(se_cmd, tl_cmd->sc->device->lun) < 0) {
271 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
272 set_host_byte(sc, DID_NO_CONNECT);
273 goto out_done;
274 }
275
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700276 transport_generic_handle_cdb_map(se_cmd);
277 return 0;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500278
279out_done:
280 sc->scsi_done(sc);
281 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700282}
283
284/*
285 * Called from SCSI EH process context to issue a LUN_RESET TMR
286 * to struct scsi_device
287 */
288static int tcm_loop_device_reset(struct scsi_cmnd *sc)
289{
290 struct se_cmd *se_cmd = NULL;
291 struct se_portal_group *se_tpg;
292 struct se_session *se_sess;
293 struct tcm_loop_cmd *tl_cmd = NULL;
294 struct tcm_loop_hba *tl_hba;
295 struct tcm_loop_nexus *tl_nexus;
296 struct tcm_loop_tmr *tl_tmr = NULL;
297 struct tcm_loop_tpg *tl_tpg;
Andy Groverc8e31f22012-01-19 13:39:17 -0800298 int ret = FAILED, rc;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700299 /*
300 * Locate the tcm_loop_hba_t pointer
301 */
302 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
303 /*
304 * Locate the tl_nexus and se_sess pointers
305 */
306 tl_nexus = tl_hba->tl_nexus;
307 if (!tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700308 pr_err("Unable to perform device reset without"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700309 " active I_T Nexus\n");
310 return FAILED;
311 }
312 se_sess = tl_nexus->se_sess;
313 /*
314 * Locate the tl_tpg and se_tpg pointers from TargetID in sc->device->id
315 */
316 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
317 se_tpg = &tl_tpg->tl_se_tpg;
318
319 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
320 if (!tl_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700321 pr_err("Unable to allocate memory for tl_cmd\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700322 return FAILED;
323 }
324
325 tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
326 if (!tl_tmr) {
Andy Grover6708bb22011-06-08 10:36:43 -0700327 pr_err("Unable to allocate memory for tl_tmr\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700328 goto release;
329 }
330 init_waitqueue_head(&tl_tmr->tl_tmr_wait);
331
332 se_cmd = &tl_cmd->tl_se_cmd;
333 /*
334 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
335 */
336 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
Nicholas Bellinger61db1802011-05-19 20:19:14 -0700337 DMA_NONE, MSG_SIMPLE_TAG,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700338 &tl_cmd->tl_sense_buf[0]);
Andy Groverc8e31f22012-01-19 13:39:17 -0800339
340 rc = core_tmr_alloc_req(se_cmd, tl_tmr, TMR_LUN_RESET, GFP_KERNEL);
341 if (rc < 0)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700342 goto release;
343 /*
344 * Locate the underlying TCM struct se_lun from sc->device->lun
345 */
Andy Grover5951146d2011-07-19 10:26:37 +0000346 if (transport_lookup_tmr_lun(se_cmd, sc->device->lun) < 0)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700347 goto release;
348 /*
349 * Queue the TMR to TCM Core and sleep waiting for tcm_loop_queue_tm_rsp()
350 * to wake us up.
351 */
352 transport_generic_handle_tmr(se_cmd);
353 wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
354 /*
355 * The TMR LUN_RESET has completed, check the response status and
356 * then release allocations.
357 */
358 ret = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
359 SUCCESS : FAILED;
360release:
361 if (se_cmd)
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +0200362 transport_generic_free_cmd(se_cmd, 1);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700363 else
364 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
365 kfree(tl_tmr);
366 return ret;
367}
368
369static int tcm_loop_slave_alloc(struct scsi_device *sd)
370{
371 set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
372 return 0;
373}
374
375static int tcm_loop_slave_configure(struct scsi_device *sd)
376{
377 return 0;
378}
379
380static struct scsi_host_template tcm_loop_driver_template = {
381 .proc_info = tcm_loop_proc_info,
382 .proc_name = "tcm_loopback",
383 .name = "TCM_Loopback",
384 .queuecommand = tcm_loop_queuecommand,
385 .change_queue_depth = tcm_loop_change_queue_depth,
386 .eh_device_reset_handler = tcm_loop_device_reset,
Christoph Hellwig2e88efd2011-11-29 03:20:41 -0500387 .can_queue = 1024,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700388 .this_id = -1,
Christoph Hellwig2e88efd2011-11-29 03:20:41 -0500389 .sg_tablesize = 256,
390 .cmd_per_lun = 1024,
391 .max_sectors = 0xFFFF,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700392 .use_clustering = DISABLE_CLUSTERING,
393 .slave_alloc = tcm_loop_slave_alloc,
394 .slave_configure = tcm_loop_slave_configure,
395 .module = THIS_MODULE,
396};
397
398static int tcm_loop_driver_probe(struct device *dev)
399{
400 struct tcm_loop_hba *tl_hba;
401 struct Scsi_Host *sh;
402 int error;
403
404 tl_hba = to_tcm_loop_hba(dev);
405
406 sh = scsi_host_alloc(&tcm_loop_driver_template,
407 sizeof(struct tcm_loop_hba));
408 if (!sh) {
Andy Grover6708bb22011-06-08 10:36:43 -0700409 pr_err("Unable to allocate struct scsi_host\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700410 return -ENODEV;
411 }
412 tl_hba->sh = sh;
413
414 /*
415 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
416 */
417 *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
418 /*
419 * Setup single ID, Channel and LUN for now..
420 */
421 sh->max_id = 2;
422 sh->max_lun = 0;
423 sh->max_channel = 0;
424 sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN;
425
426 error = scsi_add_host(sh, &tl_hba->dev);
427 if (error) {
Andy Grover6708bb22011-06-08 10:36:43 -0700428 pr_err("%s: scsi_add_host failed\n", __func__);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700429 scsi_host_put(sh);
430 return -ENODEV;
431 }
432 return 0;
433}
434
435static int tcm_loop_driver_remove(struct device *dev)
436{
437 struct tcm_loop_hba *tl_hba;
438 struct Scsi_Host *sh;
439
440 tl_hba = to_tcm_loop_hba(dev);
441 sh = tl_hba->sh;
442
443 scsi_remove_host(sh);
444 scsi_host_put(sh);
445 return 0;
446}
447
448static void tcm_loop_release_adapter(struct device *dev)
449{
450 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
451
452 kfree(tl_hba);
453}
454
455/*
456 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
457 */
458static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
459{
460 int ret;
461
462 tl_hba->dev.bus = &tcm_loop_lld_bus;
463 tl_hba->dev.parent = tcm_loop_primary;
464 tl_hba->dev.release = &tcm_loop_release_adapter;
465 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
466
467 ret = device_register(&tl_hba->dev);
468 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700469 pr_err("device_register() failed for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700470 " tl_hba->dev: %d\n", ret);
471 return -ENODEV;
472 }
473
474 return 0;
475}
476
477/*
478 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
479 * tcm_loop SCSI bus.
480 */
481static int tcm_loop_alloc_core_bus(void)
482{
483 int ret;
484
485 tcm_loop_primary = root_device_register("tcm_loop_0");
486 if (IS_ERR(tcm_loop_primary)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700487 pr_err("Unable to allocate tcm_loop_primary\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700488 return PTR_ERR(tcm_loop_primary);
489 }
490
491 ret = bus_register(&tcm_loop_lld_bus);
492 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700493 pr_err("bus_register() failed for tcm_loop_lld_bus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700494 goto dev_unreg;
495 }
496
497 ret = driver_register(&tcm_loop_driverfs);
498 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700499 pr_err("driver_register() failed for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700500 "tcm_loop_driverfs\n");
501 goto bus_unreg;
502 }
503
Andy Grover6708bb22011-06-08 10:36:43 -0700504 pr_debug("Initialized TCM Loop Core Bus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700505 return ret;
506
507bus_unreg:
508 bus_unregister(&tcm_loop_lld_bus);
509dev_unreg:
510 root_device_unregister(tcm_loop_primary);
511 return ret;
512}
513
514static void tcm_loop_release_core_bus(void)
515{
516 driver_unregister(&tcm_loop_driverfs);
517 bus_unregister(&tcm_loop_lld_bus);
518 root_device_unregister(tcm_loop_primary);
519
Andy Grover6708bb22011-06-08 10:36:43 -0700520 pr_debug("Releasing TCM Loop Core BUS\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700521}
522
523static char *tcm_loop_get_fabric_name(void)
524{
525 return "loopback";
526}
527
528static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
529{
Jörn Engel8359cf42011-11-24 02:05:51 +0100530 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700531 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
532 /*
533 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
534 * time based on the protocol dependent prefix of the passed configfs group.
535 *
536 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
537 * ProtocolID using target_core_fabric_lib.c symbols.
538 */
539 switch (tl_hba->tl_proto_id) {
540 case SCSI_PROTOCOL_SAS:
541 return sas_get_fabric_proto_ident(se_tpg);
542 case SCSI_PROTOCOL_FCP:
543 return fc_get_fabric_proto_ident(se_tpg);
544 case SCSI_PROTOCOL_ISCSI:
545 return iscsi_get_fabric_proto_ident(se_tpg);
546 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700547 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700548 " SAS emulation\n", tl_hba->tl_proto_id);
549 break;
550 }
551
552 return sas_get_fabric_proto_ident(se_tpg);
553}
554
555static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
556{
Jörn Engel8359cf42011-11-24 02:05:51 +0100557 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700558 /*
559 * Return the passed NAA identifier for the SAS Target Port
560 */
561 return &tl_tpg->tl_hba->tl_wwn_address[0];
562}
563
564static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
565{
Jörn Engel8359cf42011-11-24 02:05:51 +0100566 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700567 /*
568 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
569 * to represent the SCSI Target Port.
570 */
571 return tl_tpg->tl_tpgt;
572}
573
574static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
575{
576 return 1;
577}
578
579static u32 tcm_loop_get_pr_transport_id(
580 struct se_portal_group *se_tpg,
581 struct se_node_acl *se_nacl,
582 struct t10_pr_registration *pr_reg,
583 int *format_code,
584 unsigned char *buf)
585{
Jörn Engel8359cf42011-11-24 02:05:51 +0100586 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700587 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
588
589 switch (tl_hba->tl_proto_id) {
590 case SCSI_PROTOCOL_SAS:
591 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
592 format_code, buf);
593 case SCSI_PROTOCOL_FCP:
594 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
595 format_code, buf);
596 case SCSI_PROTOCOL_ISCSI:
597 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
598 format_code, buf);
599 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700600 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700601 " SAS emulation\n", tl_hba->tl_proto_id);
602 break;
603 }
604
605 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
606 format_code, buf);
607}
608
609static u32 tcm_loop_get_pr_transport_id_len(
610 struct se_portal_group *se_tpg,
611 struct se_node_acl *se_nacl,
612 struct t10_pr_registration *pr_reg,
613 int *format_code)
614{
Jörn Engel8359cf42011-11-24 02:05:51 +0100615 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700616 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
617
618 switch (tl_hba->tl_proto_id) {
619 case SCSI_PROTOCOL_SAS:
620 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
621 format_code);
622 case SCSI_PROTOCOL_FCP:
623 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
624 format_code);
625 case SCSI_PROTOCOL_ISCSI:
626 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
627 format_code);
628 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700629 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700630 " SAS emulation\n", tl_hba->tl_proto_id);
631 break;
632 }
633
634 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
635 format_code);
636}
637
638/*
639 * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
640 * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
641 */
642static char *tcm_loop_parse_pr_out_transport_id(
643 struct se_portal_group *se_tpg,
644 const char *buf,
645 u32 *out_tid_len,
646 char **port_nexus_ptr)
647{
Jörn Engel8359cf42011-11-24 02:05:51 +0100648 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700649 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
650
651 switch (tl_hba->tl_proto_id) {
652 case SCSI_PROTOCOL_SAS:
653 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
654 port_nexus_ptr);
655 case SCSI_PROTOCOL_FCP:
656 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
657 port_nexus_ptr);
658 case SCSI_PROTOCOL_ISCSI:
659 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
660 port_nexus_ptr);
661 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700662 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700663 " SAS emulation\n", tl_hba->tl_proto_id);
664 break;
665 }
666
667 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
668 port_nexus_ptr);
669}
670
671/*
672 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
673 * based upon the incoming fabric dependent SCSI Initiator Port
674 */
675static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
676{
677 return 1;
678}
679
680static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
681{
682 return 0;
683}
684
685/*
686 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
687 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
688 */
689static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
690{
691 return 0;
692}
693
694/*
695 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
696 * never be called for TCM_Loop by target_core_fabric_configfs.c code.
697 * It has been added here as a nop for target_fabric_tf_ops_check()
698 */
699static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
700{
701 return 0;
702}
703
704static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
705 struct se_portal_group *se_tpg)
706{
707 struct tcm_loop_nacl *tl_nacl;
708
709 tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
710 if (!tl_nacl) {
Andy Grover6708bb22011-06-08 10:36:43 -0700711 pr_err("Unable to allocate struct tcm_loop_nacl\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700712 return NULL;
713 }
714
715 return &tl_nacl->se_node_acl;
716}
717
718static void tcm_loop_tpg_release_fabric_acl(
719 struct se_portal_group *se_tpg,
720 struct se_node_acl *se_nacl)
721{
722 struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
723 struct tcm_loop_nacl, se_node_acl);
724
725 kfree(tl_nacl);
726}
727
728static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
729{
730 return 1;
731}
732
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700733static int tcm_loop_is_state_remove(struct se_cmd *se_cmd)
734{
735 /*
736 * Assume struct scsi_cmnd is not in remove state..
737 */
738 return 0;
739}
740
741static int tcm_loop_sess_logged_in(struct se_session *se_sess)
742{
743 /*
744 * Assume that TL Nexus is always active
745 */
746 return 1;
747}
748
749static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
750{
751 return 1;
752}
753
754static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
755{
756 return;
757}
758
759static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
760{
761 return 1;
762}
763
764static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
765{
766 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
767 struct tcm_loop_cmd, tl_se_cmd);
768
769 return tl_cmd->sc_cmd_state;
770}
771
772static int tcm_loop_shutdown_session(struct se_session *se_sess)
773{
774 return 0;
775}
776
777static void tcm_loop_close_session(struct se_session *se_sess)
778{
779 return;
780};
781
782static void tcm_loop_stop_session(
783 struct se_session *se_sess,
784 int sess_sleep,
785 int conn_sleep)
786{
787 return;
788}
789
790static void tcm_loop_fall_back_to_erl0(struct se_session *se_sess)
791{
792 return;
793}
794
795static int tcm_loop_write_pending(struct se_cmd *se_cmd)
796{
797 /*
798 * Since Linux/SCSI has already sent down a struct scsi_cmnd
799 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
800 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
801 * format with transport_generic_map_mem_to_cmd().
802 *
803 * We now tell TCM to add this WRITE CDB directly into the TCM storage
804 * object execution queue.
805 */
806 transport_generic_process_write(se_cmd);
807 return 0;
808}
809
810static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
811{
812 return 0;
813}
814
815static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
816{
817 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
818 struct tcm_loop_cmd, tl_se_cmd);
819 struct scsi_cmnd *sc = tl_cmd->sc;
820
Andy Grover6708bb22011-06-08 10:36:43 -0700821 pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700822 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
823
824 sc->result = SAM_STAT_GOOD;
825 set_host_byte(sc, DID_OK);
826 sc->scsi_done(sc);
827 return 0;
828}
829
830static int tcm_loop_queue_status(struct se_cmd *se_cmd)
831{
832 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
833 struct tcm_loop_cmd, tl_se_cmd);
834 struct scsi_cmnd *sc = tl_cmd->sc;
835
Andy Grover6708bb22011-06-08 10:36:43 -0700836 pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700837 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
838
839 if (se_cmd->sense_buffer &&
840 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
841 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
842
Andy Grover5951146d2011-07-19 10:26:37 +0000843 memcpy(sc->sense_buffer, se_cmd->sense_buffer,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700844 SCSI_SENSE_BUFFERSIZE);
845 sc->result = SAM_STAT_CHECK_CONDITION;
846 set_driver_byte(sc, DRIVER_SENSE);
847 } else
848 sc->result = se_cmd->scsi_status;
849
850 set_host_byte(sc, DID_OK);
851 sc->scsi_done(sc);
852 return 0;
853}
854
855static int tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
856{
857 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
858 struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
859 /*
860 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
861 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
862 */
863 atomic_set(&tl_tmr->tmr_complete, 1);
864 wake_up(&tl_tmr->tl_tmr_wait);
865 return 0;
866}
867
868static u16 tcm_loop_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
869{
870 return 0;
871}
872
873static u16 tcm_loop_get_fabric_sense_len(void)
874{
875 return 0;
876}
877
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700878static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
879{
880 switch (tl_hba->tl_proto_id) {
881 case SCSI_PROTOCOL_SAS:
882 return "SAS";
883 case SCSI_PROTOCOL_FCP:
884 return "FCP";
885 case SCSI_PROTOCOL_ISCSI:
886 return "iSCSI";
887 default:
888 break;
889 }
890
891 return "Unknown";
892}
893
894/* Start items for tcm_loop_port_cit */
895
896static int tcm_loop_port_link(
897 struct se_portal_group *se_tpg,
898 struct se_lun *lun)
899{
900 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
901 struct tcm_loop_tpg, tl_se_tpg);
902 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
903
904 atomic_inc(&tl_tpg->tl_tpg_port_count);
905 smp_mb__after_atomic_inc();
906 /*
907 * Add Linux/SCSI struct scsi_device by HCTL
908 */
909 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
910
Andy Grover6708bb22011-06-08 10:36:43 -0700911 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700912 return 0;
913}
914
915static void tcm_loop_port_unlink(
916 struct se_portal_group *se_tpg,
917 struct se_lun *se_lun)
918{
919 struct scsi_device *sd;
920 struct tcm_loop_hba *tl_hba;
921 struct tcm_loop_tpg *tl_tpg;
922
923 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
924 tl_hba = tl_tpg->tl_hba;
925
926 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
927 se_lun->unpacked_lun);
928 if (!sd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700929 pr_err("Unable to locate struct scsi_device for %d:%d:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700930 "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
931 return;
932 }
933 /*
934 * Remove Linux/SCSI struct scsi_device by HCTL
935 */
936 scsi_remove_device(sd);
937 scsi_device_put(sd);
938
939 atomic_dec(&tl_tpg->tl_tpg_port_count);
940 smp_mb__after_atomic_dec();
941
Andy Grover6708bb22011-06-08 10:36:43 -0700942 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700943}
944
945/* End items for tcm_loop_port_cit */
946
947/* Start items for tcm_loop_nexus_cit */
948
949static int tcm_loop_make_nexus(
950 struct tcm_loop_tpg *tl_tpg,
951 const char *name)
952{
953 struct se_portal_group *se_tpg;
954 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
955 struct tcm_loop_nexus *tl_nexus;
Dan Carpenter552523d2011-06-15 09:41:33 -0700956 int ret = -ENOMEM;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700957
958 if (tl_tpg->tl_hba->tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700959 pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700960 return -EEXIST;
961 }
962 se_tpg = &tl_tpg->tl_se_tpg;
963
964 tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
965 if (!tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700966 pr_err("Unable to allocate struct tcm_loop_nexus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700967 return -ENOMEM;
968 }
969 /*
970 * Initialize the struct se_session pointer
971 */
972 tl_nexus->se_sess = transport_init_session();
Dan Carpenter552523d2011-06-15 09:41:33 -0700973 if (IS_ERR(tl_nexus->se_sess)) {
974 ret = PTR_ERR(tl_nexus->se_sess);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700975 goto out;
Dan Carpenter552523d2011-06-15 09:41:33 -0700976 }
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700977 /*
978 * Since we are running in 'demo mode' this call with generate a
979 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
980 * Initiator port name of the passed configfs group 'name'.
981 */
982 tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
983 se_tpg, (unsigned char *)name);
984 if (!tl_nexus->se_sess->se_node_acl) {
985 transport_free_session(tl_nexus->se_sess);
986 goto out;
987 }
988 /*
989 * Now, register the SAS I_T Nexus as active with the call to
990 * transport_register_session()
991 */
992 __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
Andy Grover5951146d2011-07-19 10:26:37 +0000993 tl_nexus->se_sess, tl_nexus);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700994 tl_tpg->tl_hba->tl_nexus = tl_nexus;
Andy Grover6708bb22011-06-08 10:36:43 -0700995 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700996 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
997 name);
998 return 0;
999
1000out:
1001 kfree(tl_nexus);
Dan Carpenter552523d2011-06-15 09:41:33 -07001002 return ret;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001003}
1004
1005static int tcm_loop_drop_nexus(
1006 struct tcm_loop_tpg *tpg)
1007{
1008 struct se_session *se_sess;
1009 struct tcm_loop_nexus *tl_nexus;
1010 struct tcm_loop_hba *tl_hba = tpg->tl_hba;
1011
1012 tl_nexus = tpg->tl_hba->tl_nexus;
1013 if (!tl_nexus)
1014 return -ENODEV;
1015
1016 se_sess = tl_nexus->se_sess;
1017 if (!se_sess)
1018 return -ENODEV;
1019
1020 if (atomic_read(&tpg->tl_tpg_port_count)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001021 pr_err("Unable to remove TCM_Loop I_T Nexus with"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001022 " active TPG port count: %d\n",
1023 atomic_read(&tpg->tl_tpg_port_count));
1024 return -EPERM;
1025 }
1026
Andy Grover6708bb22011-06-08 10:36:43 -07001027 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001028 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
1029 tl_nexus->se_sess->se_node_acl->initiatorname);
1030 /*
1031 * Release the SCSI I_T Nexus to the emulated SAS Target Port
1032 */
1033 transport_deregister_session(tl_nexus->se_sess);
1034 tpg->tl_hba->tl_nexus = NULL;
1035 kfree(tl_nexus);
1036 return 0;
1037}
1038
1039/* End items for tcm_loop_nexus_cit */
1040
1041static ssize_t tcm_loop_tpg_show_nexus(
1042 struct se_portal_group *se_tpg,
1043 char *page)
1044{
1045 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1046 struct tcm_loop_tpg, tl_se_tpg);
1047 struct tcm_loop_nexus *tl_nexus;
1048 ssize_t ret;
1049
1050 tl_nexus = tl_tpg->tl_hba->tl_nexus;
1051 if (!tl_nexus)
1052 return -ENODEV;
1053
1054 ret = snprintf(page, PAGE_SIZE, "%s\n",
1055 tl_nexus->se_sess->se_node_acl->initiatorname);
1056
1057 return ret;
1058}
1059
1060static ssize_t tcm_loop_tpg_store_nexus(
1061 struct se_portal_group *se_tpg,
1062 const char *page,
1063 size_t count)
1064{
1065 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1066 struct tcm_loop_tpg, tl_se_tpg);
1067 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
1068 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
1069 int ret;
1070 /*
1071 * Shutdown the active I_T nexus if 'NULL' is passed..
1072 */
1073 if (!strncmp(page, "NULL", 4)) {
1074 ret = tcm_loop_drop_nexus(tl_tpg);
1075 return (!ret) ? count : ret;
1076 }
1077 /*
1078 * Otherwise make sure the passed virtual Initiator port WWN matches
1079 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
1080 * tcm_loop_make_nexus()
1081 */
Dan Carpenter60d645a2011-06-15 10:03:05 -07001082 if (strlen(page) >= TL_WWN_ADDR_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001083 pr_err("Emulated NAA Sas Address: %s, exceeds"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001084 " max: %d\n", page, TL_WWN_ADDR_LEN);
1085 return -EINVAL;
1086 }
1087 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
1088
1089 ptr = strstr(i_port, "naa.");
1090 if (ptr) {
1091 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
Andy Grover6708bb22011-06-08 10:36:43 -07001092 pr_err("Passed SAS Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001093 " match target port protoid: %s\n", i_port,
1094 tcm_loop_dump_proto_id(tl_hba));
1095 return -EINVAL;
1096 }
1097 port_ptr = &i_port[0];
1098 goto check_newline;
1099 }
1100 ptr = strstr(i_port, "fc.");
1101 if (ptr) {
1102 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
Andy Grover6708bb22011-06-08 10:36:43 -07001103 pr_err("Passed FCP Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001104 " match target port protoid: %s\n", i_port,
1105 tcm_loop_dump_proto_id(tl_hba));
1106 return -EINVAL;
1107 }
1108 port_ptr = &i_port[3]; /* Skip over "fc." */
1109 goto check_newline;
1110 }
1111 ptr = strstr(i_port, "iqn.");
1112 if (ptr) {
1113 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
Andy Grover6708bb22011-06-08 10:36:43 -07001114 pr_err("Passed iSCSI Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001115 " match target port protoid: %s\n", i_port,
1116 tcm_loop_dump_proto_id(tl_hba));
1117 return -EINVAL;
1118 }
1119 port_ptr = &i_port[0];
1120 goto check_newline;
1121 }
Andy Grover6708bb22011-06-08 10:36:43 -07001122 pr_err("Unable to locate prefix for emulated Initiator Port:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001123 " %s\n", i_port);
1124 return -EINVAL;
1125 /*
1126 * Clear any trailing newline for the NAA WWN
1127 */
1128check_newline:
1129 if (i_port[strlen(i_port)-1] == '\n')
1130 i_port[strlen(i_port)-1] = '\0';
1131
1132 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
1133 if (ret < 0)
1134 return ret;
1135
1136 return count;
1137}
1138
1139TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
1140
1141static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
1142 &tcm_loop_tpg_nexus.attr,
1143 NULL,
1144};
1145
1146/* Start items for tcm_loop_naa_cit */
1147
1148struct se_portal_group *tcm_loop_make_naa_tpg(
1149 struct se_wwn *wwn,
1150 struct config_group *group,
1151 const char *name)
1152{
1153 struct tcm_loop_hba *tl_hba = container_of(wwn,
1154 struct tcm_loop_hba, tl_hba_wwn);
1155 struct tcm_loop_tpg *tl_tpg;
1156 char *tpgt_str, *end_ptr;
1157 int ret;
1158 unsigned short int tpgt;
1159
1160 tpgt_str = strstr(name, "tpgt_");
1161 if (!tpgt_str) {
Andy Grover6708bb22011-06-08 10:36:43 -07001162 pr_err("Unable to locate \"tpgt_#\" directory"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001163 " group\n");
1164 return ERR_PTR(-EINVAL);
1165 }
1166 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1167 tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1168
Dan Carpenter12f09cc2011-04-02 14:32:47 -07001169 if (tpgt >= TL_TPGS_PER_HBA) {
Andy Grover6708bb22011-06-08 10:36:43 -07001170 pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001171 " %u\n", tpgt, TL_TPGS_PER_HBA);
1172 return ERR_PTR(-EINVAL);
1173 }
1174 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
1175 tl_tpg->tl_hba = tl_hba;
1176 tl_tpg->tl_tpgt = tpgt;
1177 /*
1178 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
1179 */
1180 ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
Andy Grover5951146d2011-07-19 10:26:37 +00001181 wwn, &tl_tpg->tl_se_tpg, tl_tpg,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001182 TRANSPORT_TPG_TYPE_NORMAL);
1183 if (ret < 0)
1184 return ERR_PTR(-ENOMEM);
1185
Andy Grover6708bb22011-06-08 10:36:43 -07001186 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001187 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1188 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1189
1190 return &tl_tpg->tl_se_tpg;
1191}
1192
1193void tcm_loop_drop_naa_tpg(
1194 struct se_portal_group *se_tpg)
1195{
1196 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1197 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1198 struct tcm_loop_tpg, tl_se_tpg);
1199 struct tcm_loop_hba *tl_hba;
1200 unsigned short tpgt;
1201
1202 tl_hba = tl_tpg->tl_hba;
1203 tpgt = tl_tpg->tl_tpgt;
1204 /*
1205 * Release the I_T Nexus for the Virtual SAS link if present
1206 */
1207 tcm_loop_drop_nexus(tl_tpg);
1208 /*
1209 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
1210 */
1211 core_tpg_deregister(se_tpg);
1212
Nicholas Bellinger0a020432011-10-10 19:44:05 -07001213 tl_tpg->tl_hba = NULL;
1214 tl_tpg->tl_tpgt = 0;
1215
Andy Grover6708bb22011-06-08 10:36:43 -07001216 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001217 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1218 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1219}
1220
1221/* End items for tcm_loop_naa_cit */
1222
1223/* Start items for tcm_loop_cit */
1224
1225struct se_wwn *tcm_loop_make_scsi_hba(
1226 struct target_fabric_configfs *tf,
1227 struct config_group *group,
1228 const char *name)
1229{
1230 struct tcm_loop_hba *tl_hba;
1231 struct Scsi_Host *sh;
1232 char *ptr;
1233 int ret, off = 0;
1234
1235 tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
1236 if (!tl_hba) {
Andy Grover6708bb22011-06-08 10:36:43 -07001237 pr_err("Unable to allocate struct tcm_loop_hba\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001238 return ERR_PTR(-ENOMEM);
1239 }
1240 /*
1241 * Determine the emulated Protocol Identifier and Target Port Name
1242 * based on the incoming configfs directory name.
1243 */
1244 ptr = strstr(name, "naa.");
1245 if (ptr) {
1246 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1247 goto check_len;
1248 }
1249 ptr = strstr(name, "fc.");
1250 if (ptr) {
1251 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1252 off = 3; /* Skip over "fc." */
1253 goto check_len;
1254 }
1255 ptr = strstr(name, "iqn.");
Jesper Juhla57b5d32011-06-28 00:30:17 +02001256 if (!ptr) {
Andy Grover6708bb22011-06-08 10:36:43 -07001257 pr_err("Unable to locate prefix for emulated Target "
Jesper Juhla57b5d32011-06-28 00:30:17 +02001258 "Port: %s\n", name);
1259 ret = -EINVAL;
1260 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001261 }
Jesper Juhla57b5d32011-06-28 00:30:17 +02001262 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001263
1264check_len:
Dan Carpenter60d645a2011-06-15 10:03:05 -07001265 if (strlen(name) >= TL_WWN_ADDR_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001266 pr_err("Emulated NAA %s Address: %s, exceeds"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001267 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1268 TL_WWN_ADDR_LEN);
Jesper Juhla57b5d32011-06-28 00:30:17 +02001269 ret = -EINVAL;
1270 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001271 }
1272 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1273
1274 /*
1275 * Call device_register(tl_hba->dev) to register the emulated
1276 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1277 * device_register() callbacks in tcm_loop_driver_probe()
1278 */
1279 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1280 if (ret)
1281 goto out;
1282
1283 sh = tl_hba->sh;
1284 tcm_loop_hba_no_cnt++;
Andy Grover6708bb22011-06-08 10:36:43 -07001285 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001286 " %s Address: %s at Linux/SCSI Host ID: %d\n",
1287 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1288
1289 return &tl_hba->tl_hba_wwn;
1290out:
1291 kfree(tl_hba);
1292 return ERR_PTR(ret);
1293}
1294
1295void tcm_loop_drop_scsi_hba(
1296 struct se_wwn *wwn)
1297{
1298 struct tcm_loop_hba *tl_hba = container_of(wwn,
1299 struct tcm_loop_hba, tl_hba_wwn);
Nicholas Bellinger6297b072011-11-12 09:29:51 -08001300
1301 pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
1302 " SAS Address: %s at Linux/SCSI Host ID: %d\n",
1303 tl_hba->tl_wwn_address, tl_hba->sh->host_no);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001304 /*
1305 * Call device_unregister() on the original tl_hba->dev.
1306 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1307 * release *tl_hba;
1308 */
1309 device_unregister(&tl_hba->dev);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001310}
1311
1312/* Start items for tcm_loop_cit */
1313static ssize_t tcm_loop_wwn_show_attr_version(
1314 struct target_fabric_configfs *tf,
1315 char *page)
1316{
1317 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1318}
1319
1320TF_WWN_ATTR_RO(tcm_loop, version);
1321
1322static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1323 &tcm_loop_wwn_version.attr,
1324 NULL,
1325};
1326
1327/* End items for tcm_loop_cit */
1328
1329static int tcm_loop_register_configfs(void)
1330{
1331 struct target_fabric_configfs *fabric;
1332 struct config_group *tf_cg;
1333 int ret;
1334 /*
1335 * Set the TCM Loop HBA counter to zero
1336 */
1337 tcm_loop_hba_no_cnt = 0;
1338 /*
1339 * Register the top level struct config_item_type with TCM core
1340 */
1341 fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
Andy Grovere3d6f902011-07-19 08:55:10 +00001342 if (IS_ERR(fabric)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001343 pr_err("tcm_loop_register_configfs() failed!\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001344 return PTR_ERR(fabric);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001345 }
1346 /*
1347 * Setup the fabric API of function pointers used by target_core_mod
1348 */
1349 fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
1350 fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
1351 fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
1352 fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
1353 fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
1354 fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
1355 fabric->tf_ops.tpg_get_pr_transport_id_len =
1356 &tcm_loop_get_pr_transport_id_len;
1357 fabric->tf_ops.tpg_parse_pr_out_transport_id =
1358 &tcm_loop_parse_pr_out_transport_id;
1359 fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
1360 fabric->tf_ops.tpg_check_demo_mode_cache =
1361 &tcm_loop_check_demo_mode_cache;
1362 fabric->tf_ops.tpg_check_demo_mode_write_protect =
1363 &tcm_loop_check_demo_mode_write_protect;
1364 fabric->tf_ops.tpg_check_prod_mode_write_protect =
1365 &tcm_loop_check_prod_mode_write_protect;
1366 /*
1367 * The TCM loopback fabric module runs in demo-mode to a local
1368 * virtual SCSI device, so fabric dependent initator ACLs are
1369 * not required.
1370 */
1371 fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
1372 fabric->tf_ops.tpg_release_fabric_acl =
1373 &tcm_loop_tpg_release_fabric_acl;
1374 fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
1375 /*
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001376 * Used for setting up remaining TCM resources in process context
1377 */
1378 fabric->tf_ops.new_cmd_map = &tcm_loop_new_cmd_map;
1379 fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
Christoph Hellwig35462972011-05-31 23:56:57 -04001380 fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001381 fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
1382 fabric->tf_ops.close_session = &tcm_loop_close_session;
1383 fabric->tf_ops.stop_session = &tcm_loop_stop_session;
1384 fabric->tf_ops.fall_back_to_erl0 = &tcm_loop_fall_back_to_erl0;
1385 fabric->tf_ops.sess_logged_in = &tcm_loop_sess_logged_in;
1386 fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
1387 fabric->tf_ops.sess_get_initiator_sid = NULL;
1388 fabric->tf_ops.write_pending = &tcm_loop_write_pending;
1389 fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
1390 /*
1391 * Not used for TCM loopback
1392 */
1393 fabric->tf_ops.set_default_node_attributes =
1394 &tcm_loop_set_default_node_attributes;
1395 fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
1396 fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001397 fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
1398 fabric->tf_ops.queue_status = &tcm_loop_queue_status;
1399 fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
1400 fabric->tf_ops.set_fabric_sense_len = &tcm_loop_set_fabric_sense_len;
1401 fabric->tf_ops.get_fabric_sense_len = &tcm_loop_get_fabric_sense_len;
1402 fabric->tf_ops.is_state_remove = &tcm_loop_is_state_remove;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001403
1404 tf_cg = &fabric->tf_group;
1405 /*
1406 * Setup function pointers for generic logic in target_core_fabric_configfs.c
1407 */
1408 fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
1409 fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
1410 fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
1411 fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
1412 /*
1413 * fabric_post_link() and fabric_pre_unlink() are used for
1414 * registration and release of TCM Loop Virtual SCSI LUNs.
1415 */
1416 fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
1417 fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
1418 fabric->tf_ops.fabric_make_np = NULL;
1419 fabric->tf_ops.fabric_drop_np = NULL;
1420 /*
1421 * Setup default attribute lists for various fabric->tf_cit_tmpl
1422 */
1423 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
1424 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
1425 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1426 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1427 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1428 /*
1429 * Once fabric->tf_ops has been setup, now register the fabric for
1430 * use within TCM
1431 */
1432 ret = target_fabric_configfs_register(fabric);
1433 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001434 pr_err("target_fabric_configfs_register() for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001435 " TCM_Loop failed!\n");
1436 target_fabric_configfs_free(fabric);
1437 return -1;
1438 }
1439 /*
1440 * Setup our local pointer to *fabric.
1441 */
1442 tcm_loop_fabric_configfs = fabric;
Andy Grover6708bb22011-06-08 10:36:43 -07001443 pr_debug("TCM_LOOP[0] - Set fabric ->"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001444 " tcm_loop_fabric_configfs\n");
1445 return 0;
1446}
1447
1448static void tcm_loop_deregister_configfs(void)
1449{
1450 if (!tcm_loop_fabric_configfs)
1451 return;
1452
1453 target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
1454 tcm_loop_fabric_configfs = NULL;
Andy Grover6708bb22011-06-08 10:36:43 -07001455 pr_debug("TCM_LOOP[0] - Cleared"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001456 " tcm_loop_fabric_configfs\n");
1457}
1458
1459static int __init tcm_loop_fabric_init(void)
1460{
1461 int ret;
1462
1463 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1464 sizeof(struct tcm_loop_cmd),
1465 __alignof__(struct tcm_loop_cmd),
1466 0, NULL);
1467 if (!tcm_loop_cmd_cache) {
Andy Grover6708bb22011-06-08 10:36:43 -07001468 pr_debug("kmem_cache_create() for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001469 " tcm_loop_cmd_cache failed\n");
1470 return -ENOMEM;
1471 }
1472
1473 ret = tcm_loop_alloc_core_bus();
1474 if (ret)
1475 return ret;
1476
1477 ret = tcm_loop_register_configfs();
1478 if (ret) {
1479 tcm_loop_release_core_bus();
1480 return ret;
1481 }
1482
1483 return 0;
1484}
1485
1486static void __exit tcm_loop_fabric_exit(void)
1487{
1488 tcm_loop_deregister_configfs();
1489 tcm_loop_release_core_bus();
1490 kmem_cache_destroy(tcm_loop_cmd_cache);
1491}
1492
1493MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1494MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1495MODULE_LICENSE("GPL");
1496module_init(tcm_loop_fabric_init);
1497module_exit(tcm_loop_fabric_exit);