blob: 2d444b1ccd33231a57a6bd8f8eb57456ed8f61da [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
Christoph Hellwig167864542012-02-02 17:04:42 -050052static int tcm_loop_queue_status(struct se_cmd *se_cmd);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070053
54/*
55 * Called from struct target_core_fabric_ops->check_stop_free()
56 */
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -070057static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070058{
59 /*
60 * Do not release struct se_cmd's containing a valid TMR
61 * pointer. These will be released directly in tcm_loop_device_reset()
62 * with transport_generic_free_cmd().
63 */
Andy Groverc8e31f22012-01-19 13:39:17 -080064 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -070065 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070066 /*
67 * Release the struct se_cmd, which will make a callback to release
68 * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd()
69 */
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +020070 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -070071 return 1;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070072}
73
Christoph Hellwig35462972011-05-31 23:56:57 -040074static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070075{
76 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
77 struct tcm_loop_cmd, tl_se_cmd);
78
79 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
80}
81
82static int tcm_loop_proc_info(struct Scsi_Host *host, char *buffer,
83 char **start, off_t offset,
84 int length, int inout)
85{
86 return sprintf(buffer, "tcm_loop_proc_info()\n");
87}
88
89static int tcm_loop_driver_probe(struct device *);
90static int tcm_loop_driver_remove(struct device *);
91
92static int pseudo_lld_bus_match(struct device *dev,
93 struct device_driver *dev_driver)
94{
95 return 1;
96}
97
98static struct bus_type tcm_loop_lld_bus = {
99 .name = "tcm_loop_bus",
100 .match = pseudo_lld_bus_match,
101 .probe = tcm_loop_driver_probe,
102 .remove = tcm_loop_driver_remove,
103};
104
105static struct device_driver tcm_loop_driverfs = {
106 .name = "tcm_loop",
107 .bus = &tcm_loop_lld_bus,
108};
109/*
110 * Used with root_device_register() in tcm_loop_alloc_core_bus() below
111 */
112struct device *tcm_loop_primary;
113
114/*
115 * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and
116 * drivers/scsi/libiscsi.c:iscsi_change_queue_depth()
117 */
118static int tcm_loop_change_queue_depth(
119 struct scsi_device *sdev,
120 int depth,
121 int reason)
122{
123 switch (reason) {
124 case SCSI_QDEPTH_DEFAULT:
125 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
126 break;
127 case SCSI_QDEPTH_QFULL:
128 scsi_track_queue_full(sdev, depth);
129 break;
130 case SCSI_QDEPTH_RAMP_UP:
131 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
132 break;
133 default:
134 return -EOPNOTSUPP;
135 }
136 return sdev->queue_depth;
137}
138
139/*
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500140 * Locate the SAM Task Attr from struct scsi_cmnd *
141 */
142static int tcm_loop_sam_attr(struct scsi_cmnd *sc)
143{
144 if (sc->device->tagged_supported) {
145 switch (sc->tag) {
146 case HEAD_OF_QUEUE_TAG:
147 return MSG_HEAD_TAG;
148 case ORDERED_QUEUE_TAG:
149 return MSG_ORDERED_TAG;
150 default:
151 break;
152 }
153 }
154
155 return MSG_SIMPLE_TAG;
156}
157
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500158static void tcm_loop_submission_work(struct work_struct *work)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700159{
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500160 struct tcm_loop_cmd *tl_cmd =
161 container_of(work, struct tcm_loop_cmd, work);
162 struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
163 struct scsi_cmnd *sc = tl_cmd->sc;
164 struct tcm_loop_nexus *tl_nexus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700165 struct tcm_loop_hba *tl_hba;
166 struct tcm_loop_tpg *tl_tpg;
Christoph Hellwig167864542012-02-02 17:04:42 -0500167 struct scatterlist *sgl_bidi = NULL;
168 u32 sgl_bidi_count = 0;
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700169 int rc;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500170
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700171 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
172 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500173
Nicholas Bellinger0a020432011-10-10 19:44:05 -0700174 /*
175 * Ensure that this tl_tpg reference from the incoming sc->device->id
176 * has already been configured via tcm_loop_make_naa_tpg().
177 */
178 if (!tl_tpg->tl_hba) {
179 set_host_byte(sc, DID_NO_CONNECT);
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500180 goto out_done;
Nicholas Bellinger0a020432011-10-10 19:44:05 -0700181 }
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500182
183 tl_nexus = tl_hba->tl_nexus;
184 if (!tl_nexus) {
185 scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
186 " does not exist\n");
187 set_host_byte(sc, DID_ERROR);
188 goto out_done;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700189 }
Christoph Hellwig167864542012-02-02 17:04:42 -0500190 if (scsi_bidi_cmnd(sc)) {
191 struct scsi_data_buffer *sdb = scsi_in(sc);
192
193 sgl_bidi = sdb->table.sgl;
194 sgl_bidi_count = sdb->table.nents;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500195 se_cmd->se_cmd_flags |= SCF_BIDI;
196
Christoph Hellwig167864542012-02-02 17:04:42 -0500197 }
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700198 rc = target_submit_cmd_map_sgls(se_cmd, tl_nexus->se_sess, sc->cmnd,
199 &tl_cmd->tl_sense_buf[0], tl_cmd->sc->device->lun,
200 scsi_bufflen(sc), tcm_loop_sam_attr(sc),
201 sc->sc_data_direction, 0,
202 scsi_sglist(sc), scsi_sg_count(sc),
203 sgl_bidi, sgl_bidi_count);
204 if (rc < 0) {
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500205 set_host_byte(sc, DID_NO_CONNECT);
206 goto out_done;
207 }
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500208 return;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500209
210out_done:
211 sc->scsi_done(sc);
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500212 return;
213}
214
215/*
216 * ->queuecommand can be and usually is called from interrupt context, so
217 * defer the actual submission to a workqueue.
218 */
219static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
220{
221 struct tcm_loop_cmd *tl_cmd;
222
223 pr_debug("tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x"
224 " scsi_buf_len: %u\n", sc->device->host->host_no,
225 sc->device->id, sc->device->channel, sc->device->lun,
226 sc->cmnd[0], scsi_bufflen(sc));
227
228 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
229 if (!tl_cmd) {
230 pr_err("Unable to allocate struct tcm_loop_cmd\n");
231 set_host_byte(sc, DID_ERROR);
232 sc->scsi_done(sc);
233 return 0;
234 }
235
236 tl_cmd->sc = sc;
237 INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
238 queue_work(tcm_loop_workqueue, &tl_cmd->work);
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500239 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700240}
241
242/*
243 * Called from SCSI EH process context to issue a LUN_RESET TMR
244 * to struct scsi_device
245 */
246static int tcm_loop_device_reset(struct scsi_cmnd *sc)
247{
248 struct se_cmd *se_cmd = NULL;
249 struct se_portal_group *se_tpg;
250 struct se_session *se_sess;
251 struct tcm_loop_cmd *tl_cmd = NULL;
252 struct tcm_loop_hba *tl_hba;
253 struct tcm_loop_nexus *tl_nexus;
254 struct tcm_loop_tmr *tl_tmr = NULL;
255 struct tcm_loop_tpg *tl_tpg;
Andy Groverc8e31f22012-01-19 13:39:17 -0800256 int ret = FAILED, rc;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700257 /*
258 * Locate the tcm_loop_hba_t pointer
259 */
260 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
261 /*
262 * Locate the tl_nexus and se_sess pointers
263 */
264 tl_nexus = tl_hba->tl_nexus;
265 if (!tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700266 pr_err("Unable to perform device reset without"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700267 " active I_T Nexus\n");
268 return FAILED;
269 }
270 se_sess = tl_nexus->se_sess;
271 /*
272 * Locate the tl_tpg and se_tpg pointers from TargetID in sc->device->id
273 */
274 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
275 se_tpg = &tl_tpg->tl_se_tpg;
276
277 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
278 if (!tl_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700279 pr_err("Unable to allocate memory for tl_cmd\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700280 return FAILED;
281 }
282
283 tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
284 if (!tl_tmr) {
Andy Grover6708bb22011-06-08 10:36:43 -0700285 pr_err("Unable to allocate memory for tl_tmr\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700286 goto release;
287 }
288 init_waitqueue_head(&tl_tmr->tl_tmr_wait);
289
290 se_cmd = &tl_cmd->tl_se_cmd;
291 /*
292 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
293 */
294 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
Nicholas Bellinger61db1802011-05-19 20:19:14 -0700295 DMA_NONE, MSG_SIMPLE_TAG,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700296 &tl_cmd->tl_sense_buf[0]);
Andy Groverc8e31f22012-01-19 13:39:17 -0800297
298 rc = core_tmr_alloc_req(se_cmd, tl_tmr, TMR_LUN_RESET, GFP_KERNEL);
299 if (rc < 0)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700300 goto release;
301 /*
302 * Locate the underlying TCM struct se_lun from sc->device->lun
303 */
Andy Grover5951146d2011-07-19 10:26:37 +0000304 if (transport_lookup_tmr_lun(se_cmd, sc->device->lun) < 0)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700305 goto release;
306 /*
307 * Queue the TMR to TCM Core and sleep waiting for tcm_loop_queue_tm_rsp()
308 * to wake us up.
309 */
310 transport_generic_handle_tmr(se_cmd);
311 wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
312 /*
313 * The TMR LUN_RESET has completed, check the response status and
314 * then release allocations.
315 */
316 ret = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
317 SUCCESS : FAILED;
318release:
319 if (se_cmd)
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +0200320 transport_generic_free_cmd(se_cmd, 1);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700321 else
322 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
323 kfree(tl_tmr);
324 return ret;
325}
326
327static int tcm_loop_slave_alloc(struct scsi_device *sd)
328{
329 set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
330 return 0;
331}
332
333static int tcm_loop_slave_configure(struct scsi_device *sd)
334{
335 return 0;
336}
337
338static struct scsi_host_template tcm_loop_driver_template = {
339 .proc_info = tcm_loop_proc_info,
340 .proc_name = "tcm_loopback",
341 .name = "TCM_Loopback",
342 .queuecommand = tcm_loop_queuecommand,
343 .change_queue_depth = tcm_loop_change_queue_depth,
344 .eh_device_reset_handler = tcm_loop_device_reset,
Christoph Hellwig2e88efd2011-11-29 03:20:41 -0500345 .can_queue = 1024,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700346 .this_id = -1,
Christoph Hellwig2e88efd2011-11-29 03:20:41 -0500347 .sg_tablesize = 256,
348 .cmd_per_lun = 1024,
349 .max_sectors = 0xFFFF,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700350 .use_clustering = DISABLE_CLUSTERING,
351 .slave_alloc = tcm_loop_slave_alloc,
352 .slave_configure = tcm_loop_slave_configure,
353 .module = THIS_MODULE,
354};
355
356static int tcm_loop_driver_probe(struct device *dev)
357{
358 struct tcm_loop_hba *tl_hba;
359 struct Scsi_Host *sh;
360 int error;
361
362 tl_hba = to_tcm_loop_hba(dev);
363
364 sh = scsi_host_alloc(&tcm_loop_driver_template,
365 sizeof(struct tcm_loop_hba));
366 if (!sh) {
Andy Grover6708bb22011-06-08 10:36:43 -0700367 pr_err("Unable to allocate struct scsi_host\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700368 return -ENODEV;
369 }
370 tl_hba->sh = sh;
371
372 /*
373 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
374 */
375 *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
376 /*
377 * Setup single ID, Channel and LUN for now..
378 */
379 sh->max_id = 2;
380 sh->max_lun = 0;
381 sh->max_channel = 0;
382 sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN;
383
384 error = scsi_add_host(sh, &tl_hba->dev);
385 if (error) {
Andy Grover6708bb22011-06-08 10:36:43 -0700386 pr_err("%s: scsi_add_host failed\n", __func__);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700387 scsi_host_put(sh);
388 return -ENODEV;
389 }
390 return 0;
391}
392
393static int tcm_loop_driver_remove(struct device *dev)
394{
395 struct tcm_loop_hba *tl_hba;
396 struct Scsi_Host *sh;
397
398 tl_hba = to_tcm_loop_hba(dev);
399 sh = tl_hba->sh;
400
401 scsi_remove_host(sh);
402 scsi_host_put(sh);
403 return 0;
404}
405
406static void tcm_loop_release_adapter(struct device *dev)
407{
408 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
409
410 kfree(tl_hba);
411}
412
413/*
414 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
415 */
416static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
417{
418 int ret;
419
420 tl_hba->dev.bus = &tcm_loop_lld_bus;
421 tl_hba->dev.parent = tcm_loop_primary;
422 tl_hba->dev.release = &tcm_loop_release_adapter;
423 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
424
425 ret = device_register(&tl_hba->dev);
426 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700427 pr_err("device_register() failed for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700428 " tl_hba->dev: %d\n", ret);
429 return -ENODEV;
430 }
431
432 return 0;
433}
434
435/*
436 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
437 * tcm_loop SCSI bus.
438 */
439static int tcm_loop_alloc_core_bus(void)
440{
441 int ret;
442
443 tcm_loop_primary = root_device_register("tcm_loop_0");
444 if (IS_ERR(tcm_loop_primary)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700445 pr_err("Unable to allocate tcm_loop_primary\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700446 return PTR_ERR(tcm_loop_primary);
447 }
448
449 ret = bus_register(&tcm_loop_lld_bus);
450 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700451 pr_err("bus_register() failed for tcm_loop_lld_bus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700452 goto dev_unreg;
453 }
454
455 ret = driver_register(&tcm_loop_driverfs);
456 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700457 pr_err("driver_register() failed for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700458 "tcm_loop_driverfs\n");
459 goto bus_unreg;
460 }
461
Andy Grover6708bb22011-06-08 10:36:43 -0700462 pr_debug("Initialized TCM Loop Core Bus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700463 return ret;
464
465bus_unreg:
466 bus_unregister(&tcm_loop_lld_bus);
467dev_unreg:
468 root_device_unregister(tcm_loop_primary);
469 return ret;
470}
471
472static void tcm_loop_release_core_bus(void)
473{
474 driver_unregister(&tcm_loop_driverfs);
475 bus_unregister(&tcm_loop_lld_bus);
476 root_device_unregister(tcm_loop_primary);
477
Andy Grover6708bb22011-06-08 10:36:43 -0700478 pr_debug("Releasing TCM Loop Core BUS\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700479}
480
481static char *tcm_loop_get_fabric_name(void)
482{
483 return "loopback";
484}
485
486static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
487{
Jörn Engel8359cf42011-11-24 02:05:51 +0100488 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700489 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
490 /*
491 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
492 * time based on the protocol dependent prefix of the passed configfs group.
493 *
494 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
495 * ProtocolID using target_core_fabric_lib.c symbols.
496 */
497 switch (tl_hba->tl_proto_id) {
498 case SCSI_PROTOCOL_SAS:
499 return sas_get_fabric_proto_ident(se_tpg);
500 case SCSI_PROTOCOL_FCP:
501 return fc_get_fabric_proto_ident(se_tpg);
502 case SCSI_PROTOCOL_ISCSI:
503 return iscsi_get_fabric_proto_ident(se_tpg);
504 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700505 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700506 " SAS emulation\n", tl_hba->tl_proto_id);
507 break;
508 }
509
510 return sas_get_fabric_proto_ident(se_tpg);
511}
512
513static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
514{
Jörn Engel8359cf42011-11-24 02:05:51 +0100515 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700516 /*
517 * Return the passed NAA identifier for the SAS Target Port
518 */
519 return &tl_tpg->tl_hba->tl_wwn_address[0];
520}
521
522static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
523{
Jörn Engel8359cf42011-11-24 02:05:51 +0100524 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700525 /*
526 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
527 * to represent the SCSI Target Port.
528 */
529 return tl_tpg->tl_tpgt;
530}
531
532static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
533{
534 return 1;
535}
536
537static u32 tcm_loop_get_pr_transport_id(
538 struct se_portal_group *se_tpg,
539 struct se_node_acl *se_nacl,
540 struct t10_pr_registration *pr_reg,
541 int *format_code,
542 unsigned char *buf)
543{
Jörn Engel8359cf42011-11-24 02:05:51 +0100544 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700545 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
546
547 switch (tl_hba->tl_proto_id) {
548 case SCSI_PROTOCOL_SAS:
549 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
550 format_code, buf);
551 case SCSI_PROTOCOL_FCP:
552 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
553 format_code, buf);
554 case SCSI_PROTOCOL_ISCSI:
555 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
556 format_code, buf);
557 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700558 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700559 " SAS emulation\n", tl_hba->tl_proto_id);
560 break;
561 }
562
563 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
564 format_code, buf);
565}
566
567static u32 tcm_loop_get_pr_transport_id_len(
568 struct se_portal_group *se_tpg,
569 struct se_node_acl *se_nacl,
570 struct t10_pr_registration *pr_reg,
571 int *format_code)
572{
Jörn Engel8359cf42011-11-24 02:05:51 +0100573 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700574 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
575
576 switch (tl_hba->tl_proto_id) {
577 case SCSI_PROTOCOL_SAS:
578 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
579 format_code);
580 case SCSI_PROTOCOL_FCP:
581 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
582 format_code);
583 case SCSI_PROTOCOL_ISCSI:
584 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
585 format_code);
586 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700587 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700588 " SAS emulation\n", tl_hba->tl_proto_id);
589 break;
590 }
591
592 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
593 format_code);
594}
595
596/*
597 * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
598 * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
599 */
600static char *tcm_loop_parse_pr_out_transport_id(
601 struct se_portal_group *se_tpg,
602 const char *buf,
603 u32 *out_tid_len,
604 char **port_nexus_ptr)
605{
Jörn Engel8359cf42011-11-24 02:05:51 +0100606 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700607 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
608
609 switch (tl_hba->tl_proto_id) {
610 case SCSI_PROTOCOL_SAS:
611 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
612 port_nexus_ptr);
613 case SCSI_PROTOCOL_FCP:
614 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
615 port_nexus_ptr);
616 case SCSI_PROTOCOL_ISCSI:
617 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
618 port_nexus_ptr);
619 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700620 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700621 " SAS emulation\n", tl_hba->tl_proto_id);
622 break;
623 }
624
625 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
626 port_nexus_ptr);
627}
628
629/*
630 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
631 * based upon the incoming fabric dependent SCSI Initiator Port
632 */
633static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
634{
635 return 1;
636}
637
638static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
639{
640 return 0;
641}
642
643/*
644 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
645 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
646 */
647static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
648{
649 return 0;
650}
651
652/*
653 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
654 * never be called for TCM_Loop by target_core_fabric_configfs.c code.
655 * It has been added here as a nop for target_fabric_tf_ops_check()
656 */
657static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
658{
659 return 0;
660}
661
662static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
663 struct se_portal_group *se_tpg)
664{
665 struct tcm_loop_nacl *tl_nacl;
666
667 tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
668 if (!tl_nacl) {
Andy Grover6708bb22011-06-08 10:36:43 -0700669 pr_err("Unable to allocate struct tcm_loop_nacl\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700670 return NULL;
671 }
672
673 return &tl_nacl->se_node_acl;
674}
675
676static void tcm_loop_tpg_release_fabric_acl(
677 struct se_portal_group *se_tpg,
678 struct se_node_acl *se_nacl)
679{
680 struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
681 struct tcm_loop_nacl, se_node_acl);
682
683 kfree(tl_nacl);
684}
685
686static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
687{
688 return 1;
689}
690
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700691static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
692{
693 return 1;
694}
695
696static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
697{
698 return;
699}
700
701static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
702{
703 return 1;
704}
705
706static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
707{
708 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
709 struct tcm_loop_cmd, tl_se_cmd);
710
711 return tl_cmd->sc_cmd_state;
712}
713
714static int tcm_loop_shutdown_session(struct se_session *se_sess)
715{
716 return 0;
717}
718
719static void tcm_loop_close_session(struct se_session *se_sess)
720{
721 return;
722};
723
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700724static int tcm_loop_write_pending(struct se_cmd *se_cmd)
725{
726 /*
727 * Since Linux/SCSI has already sent down a struct scsi_cmnd
728 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
729 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
730 * format with transport_generic_map_mem_to_cmd().
731 *
732 * We now tell TCM to add this WRITE CDB directly into the TCM storage
733 * object execution queue.
734 */
Christoph Hellwig70baf0a2012-07-08 15:58:39 -0400735 target_execute_cmd(se_cmd);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700736 return 0;
737}
738
739static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
740{
741 return 0;
742}
743
744static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
745{
746 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
747 struct tcm_loop_cmd, tl_se_cmd);
748 struct scsi_cmnd *sc = tl_cmd->sc;
749
Andy Grover6708bb22011-06-08 10:36:43 -0700750 pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700751 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
752
753 sc->result = SAM_STAT_GOOD;
754 set_host_byte(sc, DID_OK);
Roland Dreier6cf3fa62012-02-14 15:30:31 -0800755 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
756 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
757 scsi_set_resid(sc, se_cmd->residual_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700758 sc->scsi_done(sc);
759 return 0;
760}
761
762static int tcm_loop_queue_status(struct se_cmd *se_cmd)
763{
764 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
765 struct tcm_loop_cmd, tl_se_cmd);
766 struct scsi_cmnd *sc = tl_cmd->sc;
767
Andy Grover6708bb22011-06-08 10:36:43 -0700768 pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700769 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
770
771 if (se_cmd->sense_buffer &&
772 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
773 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
774
Andy Grover5951146d2011-07-19 10:26:37 +0000775 memcpy(sc->sense_buffer, se_cmd->sense_buffer,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700776 SCSI_SENSE_BUFFERSIZE);
777 sc->result = SAM_STAT_CHECK_CONDITION;
778 set_driver_byte(sc, DRIVER_SENSE);
779 } else
780 sc->result = se_cmd->scsi_status;
781
782 set_host_byte(sc, DID_OK);
Roland Dreier6cf3fa62012-02-14 15:30:31 -0800783 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
784 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
785 scsi_set_resid(sc, se_cmd->residual_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700786 sc->scsi_done(sc);
787 return 0;
788}
789
790static int tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
791{
792 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
793 struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
794 /*
795 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
796 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
797 */
798 atomic_set(&tl_tmr->tmr_complete, 1);
799 wake_up(&tl_tmr->tl_tmr_wait);
800 return 0;
801}
802
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700803static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
804{
805 switch (tl_hba->tl_proto_id) {
806 case SCSI_PROTOCOL_SAS:
807 return "SAS";
808 case SCSI_PROTOCOL_FCP:
809 return "FCP";
810 case SCSI_PROTOCOL_ISCSI:
811 return "iSCSI";
812 default:
813 break;
814 }
815
816 return "Unknown";
817}
818
819/* Start items for tcm_loop_port_cit */
820
821static int tcm_loop_port_link(
822 struct se_portal_group *se_tpg,
823 struct se_lun *lun)
824{
825 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
826 struct tcm_loop_tpg, tl_se_tpg);
827 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
828
829 atomic_inc(&tl_tpg->tl_tpg_port_count);
830 smp_mb__after_atomic_inc();
831 /*
832 * Add Linux/SCSI struct scsi_device by HCTL
833 */
834 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
835
Andy Grover6708bb22011-06-08 10:36:43 -0700836 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700837 return 0;
838}
839
840static void tcm_loop_port_unlink(
841 struct se_portal_group *se_tpg,
842 struct se_lun *se_lun)
843{
844 struct scsi_device *sd;
845 struct tcm_loop_hba *tl_hba;
846 struct tcm_loop_tpg *tl_tpg;
847
848 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
849 tl_hba = tl_tpg->tl_hba;
850
851 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
852 se_lun->unpacked_lun);
853 if (!sd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700854 pr_err("Unable to locate struct scsi_device for %d:%d:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700855 "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
856 return;
857 }
858 /*
859 * Remove Linux/SCSI struct scsi_device by HCTL
860 */
861 scsi_remove_device(sd);
862 scsi_device_put(sd);
863
864 atomic_dec(&tl_tpg->tl_tpg_port_count);
865 smp_mb__after_atomic_dec();
866
Andy Grover6708bb22011-06-08 10:36:43 -0700867 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700868}
869
870/* End items for tcm_loop_port_cit */
871
872/* Start items for tcm_loop_nexus_cit */
873
874static int tcm_loop_make_nexus(
875 struct tcm_loop_tpg *tl_tpg,
876 const char *name)
877{
878 struct se_portal_group *se_tpg;
879 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
880 struct tcm_loop_nexus *tl_nexus;
Dan Carpenter552523d2011-06-15 09:41:33 -0700881 int ret = -ENOMEM;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700882
883 if (tl_tpg->tl_hba->tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700884 pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700885 return -EEXIST;
886 }
887 se_tpg = &tl_tpg->tl_se_tpg;
888
889 tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
890 if (!tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700891 pr_err("Unable to allocate struct tcm_loop_nexus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700892 return -ENOMEM;
893 }
894 /*
895 * Initialize the struct se_session pointer
896 */
897 tl_nexus->se_sess = transport_init_session();
Dan Carpenter552523d2011-06-15 09:41:33 -0700898 if (IS_ERR(tl_nexus->se_sess)) {
899 ret = PTR_ERR(tl_nexus->se_sess);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700900 goto out;
Dan Carpenter552523d2011-06-15 09:41:33 -0700901 }
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700902 /*
903 * Since we are running in 'demo mode' this call with generate a
904 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
905 * Initiator port name of the passed configfs group 'name'.
906 */
907 tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
908 se_tpg, (unsigned char *)name);
909 if (!tl_nexus->se_sess->se_node_acl) {
910 transport_free_session(tl_nexus->se_sess);
911 goto out;
912 }
913 /*
914 * Now, register the SAS I_T Nexus as active with the call to
915 * transport_register_session()
916 */
917 __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
Andy Grover5951146d2011-07-19 10:26:37 +0000918 tl_nexus->se_sess, tl_nexus);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700919 tl_tpg->tl_hba->tl_nexus = tl_nexus;
Andy Grover6708bb22011-06-08 10:36:43 -0700920 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700921 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
922 name);
923 return 0;
924
925out:
926 kfree(tl_nexus);
Dan Carpenter552523d2011-06-15 09:41:33 -0700927 return ret;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700928}
929
930static int tcm_loop_drop_nexus(
931 struct tcm_loop_tpg *tpg)
932{
933 struct se_session *se_sess;
934 struct tcm_loop_nexus *tl_nexus;
935 struct tcm_loop_hba *tl_hba = tpg->tl_hba;
936
937 tl_nexus = tpg->tl_hba->tl_nexus;
938 if (!tl_nexus)
939 return -ENODEV;
940
941 se_sess = tl_nexus->se_sess;
942 if (!se_sess)
943 return -ENODEV;
944
945 if (atomic_read(&tpg->tl_tpg_port_count)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700946 pr_err("Unable to remove TCM_Loop I_T Nexus with"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700947 " active TPG port count: %d\n",
948 atomic_read(&tpg->tl_tpg_port_count));
949 return -EPERM;
950 }
951
Andy Grover6708bb22011-06-08 10:36:43 -0700952 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700953 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
954 tl_nexus->se_sess->se_node_acl->initiatorname);
955 /*
956 * Release the SCSI I_T Nexus to the emulated SAS Target Port
957 */
958 transport_deregister_session(tl_nexus->se_sess);
959 tpg->tl_hba->tl_nexus = NULL;
960 kfree(tl_nexus);
961 return 0;
962}
963
964/* End items for tcm_loop_nexus_cit */
965
966static ssize_t tcm_loop_tpg_show_nexus(
967 struct se_portal_group *se_tpg,
968 char *page)
969{
970 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
971 struct tcm_loop_tpg, tl_se_tpg);
972 struct tcm_loop_nexus *tl_nexus;
973 ssize_t ret;
974
975 tl_nexus = tl_tpg->tl_hba->tl_nexus;
976 if (!tl_nexus)
977 return -ENODEV;
978
979 ret = snprintf(page, PAGE_SIZE, "%s\n",
980 tl_nexus->se_sess->se_node_acl->initiatorname);
981
982 return ret;
983}
984
985static ssize_t tcm_loop_tpg_store_nexus(
986 struct se_portal_group *se_tpg,
987 const char *page,
988 size_t count)
989{
990 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
991 struct tcm_loop_tpg, tl_se_tpg);
992 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
993 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
994 int ret;
995 /*
996 * Shutdown the active I_T nexus if 'NULL' is passed..
997 */
998 if (!strncmp(page, "NULL", 4)) {
999 ret = tcm_loop_drop_nexus(tl_tpg);
1000 return (!ret) ? count : ret;
1001 }
1002 /*
1003 * Otherwise make sure the passed virtual Initiator port WWN matches
1004 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
1005 * tcm_loop_make_nexus()
1006 */
Dan Carpenter60d645a2011-06-15 10:03:05 -07001007 if (strlen(page) >= TL_WWN_ADDR_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001008 pr_err("Emulated NAA Sas Address: %s, exceeds"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001009 " max: %d\n", page, TL_WWN_ADDR_LEN);
1010 return -EINVAL;
1011 }
1012 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
1013
1014 ptr = strstr(i_port, "naa.");
1015 if (ptr) {
1016 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
Andy Grover6708bb22011-06-08 10:36:43 -07001017 pr_err("Passed SAS Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001018 " match target port protoid: %s\n", i_port,
1019 tcm_loop_dump_proto_id(tl_hba));
1020 return -EINVAL;
1021 }
1022 port_ptr = &i_port[0];
1023 goto check_newline;
1024 }
1025 ptr = strstr(i_port, "fc.");
1026 if (ptr) {
1027 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
Andy Grover6708bb22011-06-08 10:36:43 -07001028 pr_err("Passed FCP Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001029 " match target port protoid: %s\n", i_port,
1030 tcm_loop_dump_proto_id(tl_hba));
1031 return -EINVAL;
1032 }
1033 port_ptr = &i_port[3]; /* Skip over "fc." */
1034 goto check_newline;
1035 }
1036 ptr = strstr(i_port, "iqn.");
1037 if (ptr) {
1038 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
Andy Grover6708bb22011-06-08 10:36:43 -07001039 pr_err("Passed iSCSI Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001040 " match target port protoid: %s\n", i_port,
1041 tcm_loop_dump_proto_id(tl_hba));
1042 return -EINVAL;
1043 }
1044 port_ptr = &i_port[0];
1045 goto check_newline;
1046 }
Andy Grover6708bb22011-06-08 10:36:43 -07001047 pr_err("Unable to locate prefix for emulated Initiator Port:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001048 " %s\n", i_port);
1049 return -EINVAL;
1050 /*
1051 * Clear any trailing newline for the NAA WWN
1052 */
1053check_newline:
1054 if (i_port[strlen(i_port)-1] == '\n')
1055 i_port[strlen(i_port)-1] = '\0';
1056
1057 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
1058 if (ret < 0)
1059 return ret;
1060
1061 return count;
1062}
1063
1064TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
1065
1066static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
1067 &tcm_loop_tpg_nexus.attr,
1068 NULL,
1069};
1070
1071/* Start items for tcm_loop_naa_cit */
1072
1073struct se_portal_group *tcm_loop_make_naa_tpg(
1074 struct se_wwn *wwn,
1075 struct config_group *group,
1076 const char *name)
1077{
1078 struct tcm_loop_hba *tl_hba = container_of(wwn,
1079 struct tcm_loop_hba, tl_hba_wwn);
1080 struct tcm_loop_tpg *tl_tpg;
1081 char *tpgt_str, *end_ptr;
1082 int ret;
1083 unsigned short int tpgt;
1084
1085 tpgt_str = strstr(name, "tpgt_");
1086 if (!tpgt_str) {
Andy Grover6708bb22011-06-08 10:36:43 -07001087 pr_err("Unable to locate \"tpgt_#\" directory"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001088 " group\n");
1089 return ERR_PTR(-EINVAL);
1090 }
1091 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1092 tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1093
Dan Carpenter12f09cc2011-04-02 14:32:47 -07001094 if (tpgt >= TL_TPGS_PER_HBA) {
Andy Grover6708bb22011-06-08 10:36:43 -07001095 pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001096 " %u\n", tpgt, TL_TPGS_PER_HBA);
1097 return ERR_PTR(-EINVAL);
1098 }
1099 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
1100 tl_tpg->tl_hba = tl_hba;
1101 tl_tpg->tl_tpgt = tpgt;
1102 /*
1103 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
1104 */
1105 ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
Andy Grover5951146d2011-07-19 10:26:37 +00001106 wwn, &tl_tpg->tl_se_tpg, tl_tpg,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001107 TRANSPORT_TPG_TYPE_NORMAL);
1108 if (ret < 0)
1109 return ERR_PTR(-ENOMEM);
1110
Andy Grover6708bb22011-06-08 10:36:43 -07001111 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001112 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1113 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1114
1115 return &tl_tpg->tl_se_tpg;
1116}
1117
1118void tcm_loop_drop_naa_tpg(
1119 struct se_portal_group *se_tpg)
1120{
1121 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1122 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1123 struct tcm_loop_tpg, tl_se_tpg);
1124 struct tcm_loop_hba *tl_hba;
1125 unsigned short tpgt;
1126
1127 tl_hba = tl_tpg->tl_hba;
1128 tpgt = tl_tpg->tl_tpgt;
1129 /*
1130 * Release the I_T Nexus for the Virtual SAS link if present
1131 */
1132 tcm_loop_drop_nexus(tl_tpg);
1133 /*
1134 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
1135 */
1136 core_tpg_deregister(se_tpg);
1137
Nicholas Bellinger0a020432011-10-10 19:44:05 -07001138 tl_tpg->tl_hba = NULL;
1139 tl_tpg->tl_tpgt = 0;
1140
Andy Grover6708bb22011-06-08 10:36:43 -07001141 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001142 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1143 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1144}
1145
1146/* End items for tcm_loop_naa_cit */
1147
1148/* Start items for tcm_loop_cit */
1149
1150struct se_wwn *tcm_loop_make_scsi_hba(
1151 struct target_fabric_configfs *tf,
1152 struct config_group *group,
1153 const char *name)
1154{
1155 struct tcm_loop_hba *tl_hba;
1156 struct Scsi_Host *sh;
1157 char *ptr;
1158 int ret, off = 0;
1159
1160 tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
1161 if (!tl_hba) {
Andy Grover6708bb22011-06-08 10:36:43 -07001162 pr_err("Unable to allocate struct tcm_loop_hba\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001163 return ERR_PTR(-ENOMEM);
1164 }
1165 /*
1166 * Determine the emulated Protocol Identifier and Target Port Name
1167 * based on the incoming configfs directory name.
1168 */
1169 ptr = strstr(name, "naa.");
1170 if (ptr) {
1171 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1172 goto check_len;
1173 }
1174 ptr = strstr(name, "fc.");
1175 if (ptr) {
1176 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1177 off = 3; /* Skip over "fc." */
1178 goto check_len;
1179 }
1180 ptr = strstr(name, "iqn.");
Jesper Juhla57b5d32011-06-28 00:30:17 +02001181 if (!ptr) {
Andy Grover6708bb22011-06-08 10:36:43 -07001182 pr_err("Unable to locate prefix for emulated Target "
Jesper Juhla57b5d32011-06-28 00:30:17 +02001183 "Port: %s\n", name);
1184 ret = -EINVAL;
1185 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001186 }
Jesper Juhla57b5d32011-06-28 00:30:17 +02001187 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001188
1189check_len:
Dan Carpenter60d645a2011-06-15 10:03:05 -07001190 if (strlen(name) >= TL_WWN_ADDR_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001191 pr_err("Emulated NAA %s Address: %s, exceeds"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001192 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1193 TL_WWN_ADDR_LEN);
Jesper Juhla57b5d32011-06-28 00:30:17 +02001194 ret = -EINVAL;
1195 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001196 }
1197 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1198
1199 /*
1200 * Call device_register(tl_hba->dev) to register the emulated
1201 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1202 * device_register() callbacks in tcm_loop_driver_probe()
1203 */
1204 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1205 if (ret)
1206 goto out;
1207
1208 sh = tl_hba->sh;
1209 tcm_loop_hba_no_cnt++;
Andy Grover6708bb22011-06-08 10:36:43 -07001210 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001211 " %s Address: %s at Linux/SCSI Host ID: %d\n",
1212 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1213
1214 return &tl_hba->tl_hba_wwn;
1215out:
1216 kfree(tl_hba);
1217 return ERR_PTR(ret);
1218}
1219
1220void tcm_loop_drop_scsi_hba(
1221 struct se_wwn *wwn)
1222{
1223 struct tcm_loop_hba *tl_hba = container_of(wwn,
1224 struct tcm_loop_hba, tl_hba_wwn);
Nicholas Bellinger6297b072011-11-12 09:29:51 -08001225
1226 pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
1227 " SAS Address: %s at Linux/SCSI Host ID: %d\n",
1228 tl_hba->tl_wwn_address, tl_hba->sh->host_no);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001229 /*
1230 * Call device_unregister() on the original tl_hba->dev.
1231 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1232 * release *tl_hba;
1233 */
1234 device_unregister(&tl_hba->dev);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001235}
1236
1237/* Start items for tcm_loop_cit */
1238static ssize_t tcm_loop_wwn_show_attr_version(
1239 struct target_fabric_configfs *tf,
1240 char *page)
1241{
1242 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1243}
1244
1245TF_WWN_ATTR_RO(tcm_loop, version);
1246
1247static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1248 &tcm_loop_wwn_version.attr,
1249 NULL,
1250};
1251
1252/* End items for tcm_loop_cit */
1253
1254static int tcm_loop_register_configfs(void)
1255{
1256 struct target_fabric_configfs *fabric;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001257 int ret;
1258 /*
1259 * Set the TCM Loop HBA counter to zero
1260 */
1261 tcm_loop_hba_no_cnt = 0;
1262 /*
1263 * Register the top level struct config_item_type with TCM core
1264 */
1265 fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
Andy Grovere3d6f902011-07-19 08:55:10 +00001266 if (IS_ERR(fabric)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001267 pr_err("tcm_loop_register_configfs() failed!\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001268 return PTR_ERR(fabric);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001269 }
1270 /*
1271 * Setup the fabric API of function pointers used by target_core_mod
1272 */
1273 fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
1274 fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
1275 fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
1276 fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
1277 fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
1278 fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
1279 fabric->tf_ops.tpg_get_pr_transport_id_len =
1280 &tcm_loop_get_pr_transport_id_len;
1281 fabric->tf_ops.tpg_parse_pr_out_transport_id =
1282 &tcm_loop_parse_pr_out_transport_id;
1283 fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
1284 fabric->tf_ops.tpg_check_demo_mode_cache =
1285 &tcm_loop_check_demo_mode_cache;
1286 fabric->tf_ops.tpg_check_demo_mode_write_protect =
1287 &tcm_loop_check_demo_mode_write_protect;
1288 fabric->tf_ops.tpg_check_prod_mode_write_protect =
1289 &tcm_loop_check_prod_mode_write_protect;
1290 /*
1291 * The TCM loopback fabric module runs in demo-mode to a local
1292 * virtual SCSI device, so fabric dependent initator ACLs are
1293 * not required.
1294 */
1295 fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
1296 fabric->tf_ops.tpg_release_fabric_acl =
1297 &tcm_loop_tpg_release_fabric_acl;
1298 fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
1299 /*
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001300 * Used for setting up remaining TCM resources in process context
1301 */
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001302 fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
Christoph Hellwig35462972011-05-31 23:56:57 -04001303 fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001304 fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
1305 fabric->tf_ops.close_session = &tcm_loop_close_session;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001306 fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
1307 fabric->tf_ops.sess_get_initiator_sid = NULL;
1308 fabric->tf_ops.write_pending = &tcm_loop_write_pending;
1309 fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
1310 /*
1311 * Not used for TCM loopback
1312 */
1313 fabric->tf_ops.set_default_node_attributes =
1314 &tcm_loop_set_default_node_attributes;
1315 fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
1316 fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001317 fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
1318 fabric->tf_ops.queue_status = &tcm_loop_queue_status;
1319 fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001320
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001321 /*
1322 * Setup function pointers for generic logic in target_core_fabric_configfs.c
1323 */
1324 fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
1325 fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
1326 fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
1327 fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
1328 /*
1329 * fabric_post_link() and fabric_pre_unlink() are used for
1330 * registration and release of TCM Loop Virtual SCSI LUNs.
1331 */
1332 fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
1333 fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
1334 fabric->tf_ops.fabric_make_np = NULL;
1335 fabric->tf_ops.fabric_drop_np = NULL;
1336 /*
1337 * Setup default attribute lists for various fabric->tf_cit_tmpl
1338 */
1339 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
1340 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
1341 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1342 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1343 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1344 /*
1345 * Once fabric->tf_ops has been setup, now register the fabric for
1346 * use within TCM
1347 */
1348 ret = target_fabric_configfs_register(fabric);
1349 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001350 pr_err("target_fabric_configfs_register() for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001351 " TCM_Loop failed!\n");
1352 target_fabric_configfs_free(fabric);
1353 return -1;
1354 }
1355 /*
1356 * Setup our local pointer to *fabric.
1357 */
1358 tcm_loop_fabric_configfs = fabric;
Andy Grover6708bb22011-06-08 10:36:43 -07001359 pr_debug("TCM_LOOP[0] - Set fabric ->"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001360 " tcm_loop_fabric_configfs\n");
1361 return 0;
1362}
1363
1364static void tcm_loop_deregister_configfs(void)
1365{
1366 if (!tcm_loop_fabric_configfs)
1367 return;
1368
1369 target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
1370 tcm_loop_fabric_configfs = NULL;
Andy Grover6708bb22011-06-08 10:36:43 -07001371 pr_debug("TCM_LOOP[0] - Cleared"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001372 " tcm_loop_fabric_configfs\n");
1373}
1374
1375static int __init tcm_loop_fabric_init(void)
1376{
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001377 int ret = -ENOMEM;
1378
1379 tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
1380 if (!tcm_loop_workqueue)
1381 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001382
1383 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1384 sizeof(struct tcm_loop_cmd),
1385 __alignof__(struct tcm_loop_cmd),
1386 0, NULL);
1387 if (!tcm_loop_cmd_cache) {
Andy Grover6708bb22011-06-08 10:36:43 -07001388 pr_debug("kmem_cache_create() for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001389 " tcm_loop_cmd_cache failed\n");
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001390 goto out_destroy_workqueue;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001391 }
1392
1393 ret = tcm_loop_alloc_core_bus();
1394 if (ret)
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001395 goto out_destroy_cache;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001396
1397 ret = tcm_loop_register_configfs();
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001398 if (ret)
1399 goto out_release_core_bus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001400
1401 return 0;
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001402
1403out_release_core_bus:
1404 tcm_loop_release_core_bus();
1405out_destroy_cache:
1406 kmem_cache_destroy(tcm_loop_cmd_cache);
1407out_destroy_workqueue:
1408 destroy_workqueue(tcm_loop_workqueue);
1409out:
1410 return ret;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001411}
1412
1413static void __exit tcm_loop_fabric_exit(void)
1414{
1415 tcm_loop_deregister_configfs();
1416 tcm_loop_release_core_bus();
1417 kmem_cache_destroy(tcm_loop_cmd_cache);
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001418 destroy_workqueue(tcm_loop_workqueue);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001419}
1420
1421MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1422MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1423MODULE_LICENSE("GPL");
1424module_init(tcm_loop_fabric_init);
1425module_exit(tcm_loop_fabric_exit);