blob: 4b5421b391da0500ea1e1b7f8cc8bf227415d40a [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 Bellinger61db1802011-05-19 20:19:14 -070034#include <scsi/scsi_tcq.h>
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070035
36#include <target/target_core_base.h>
37#include <target/target_core_transport.h>
38#include <target/target_core_fabric_ops.h>
39#include <target/target_core_fabric_configfs.h>
40#include <target/target_core_fabric_lib.h>
41#include <target/target_core_configfs.h>
42#include <target/target_core_device.h>
43#include <target/target_core_tpg.h>
44#include <target/target_core_tmr.h>
45
46#include "tcm_loop.h"
47
48#define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev)
49
50/* Local pointer to allocated TCM configfs fabric module */
51static struct target_fabric_configfs *tcm_loop_fabric_configfs;
52
53static struct kmem_cache *tcm_loop_cmd_cache;
54
55static int tcm_loop_hba_no_cnt;
56
57/*
58 * Allocate a tcm_loop cmd descriptor from target_core_mod code
59 *
60 * Can be called from interrupt context in tcm_loop_queuecommand() below
61 */
62static struct se_cmd *tcm_loop_allocate_core_cmd(
63 struct tcm_loop_hba *tl_hba,
64 struct se_portal_group *se_tpg,
65 struct scsi_cmnd *sc)
66{
67 struct se_cmd *se_cmd;
68 struct se_session *se_sess;
69 struct tcm_loop_nexus *tl_nexus = tl_hba->tl_nexus;
70 struct tcm_loop_cmd *tl_cmd;
71 int sam_task_attr;
72
73 if (!tl_nexus) {
74 scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
75 " does not exist\n");
76 set_host_byte(sc, DID_ERROR);
77 return NULL;
78 }
79 se_sess = tl_nexus->se_sess;
80
81 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
82 if (!tl_cmd) {
83 printk(KERN_ERR "Unable to allocate struct tcm_loop_cmd\n");
84 set_host_byte(sc, DID_ERROR);
85 return NULL;
86 }
87 se_cmd = &tl_cmd->tl_se_cmd;
88 /*
89 * Save the pointer to struct scsi_cmnd *sc
90 */
91 tl_cmd->sc = sc;
92 /*
93 * Locate the SAM Task Attr from struct scsi_cmnd *
94 */
95 if (sc->device->tagged_supported) {
96 switch (sc->tag) {
97 case HEAD_OF_QUEUE_TAG:
Nicholas Bellinger61db1802011-05-19 20:19:14 -070098 sam_task_attr = MSG_HEAD_TAG;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070099 break;
100 case ORDERED_QUEUE_TAG:
Nicholas Bellinger61db1802011-05-19 20:19:14 -0700101 sam_task_attr = MSG_ORDERED_TAG;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700102 break;
103 default:
Nicholas Bellinger61db1802011-05-19 20:19:14 -0700104 sam_task_attr = MSG_SIMPLE_TAG;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700105 break;
106 }
107 } else
Nicholas Bellinger61db1802011-05-19 20:19:14 -0700108 sam_task_attr = MSG_SIMPLE_TAG;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700109
110 /*
111 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
112 */
113 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
114 scsi_bufflen(sc), sc->sc_data_direction, sam_task_attr,
115 &tl_cmd->tl_sense_buf[0]);
116
117 /*
118 * Signal BIDI usage with T_TASK(cmd)->t_tasks_bidi
119 */
120 if (scsi_bidi_cmnd(sc))
121 T_TASK(se_cmd)->t_tasks_bidi = 1;
122 /*
123 * Locate the struct se_lun pointer and attach it to struct se_cmd
124 */
125 if (transport_get_lun_for_cmd(se_cmd, NULL, tl_cmd->sc->device->lun) < 0) {
126 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
127 set_host_byte(sc, DID_NO_CONNECT);
128 return NULL;
129 }
Nicholas Bellingerfef54712011-10-24 13:35:37 -0700130 /*
131 * Because some userspace code via scsi-generic do not memset their
132 * associated read buffers, go ahead and do that here for type
133 * SCF_SCSI_CONTROL_SG_IO_CDB. Also note that this is currently
134 * guaranteed to be a single SGL for SCF_SCSI_CONTROL_SG_IO_CDB
135 * by target core in transport_generic_allocate_tasks() ->
136 * transport_generic_cmd_sequencer().
137 */
138 if (se_cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB &&
139 se_cmd->data_direction == DMA_FROM_DEVICE) {
140 struct scatterlist *sg = scsi_sglist(sc);
141 unsigned char *buf = kmap(sg_page(sg)) + sg->offset;
142
143 if (buf != NULL) {
144 memset(buf, 0, sg->length);
145 kunmap(sg_page(sg));
146 }
147 }
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700148
149 transport_device_setup_cmd(se_cmd);
150 return se_cmd;
151}
152
153/*
154 * Called by struct target_core_fabric_ops->new_cmd_map()
155 *
156 * Always called in process context. A non zero return value
157 * here will signal to handle an exception based on the return code.
158 */
159static int tcm_loop_new_cmd_map(struct se_cmd *se_cmd)
160{
161 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
162 struct tcm_loop_cmd, tl_se_cmd);
163 struct scsi_cmnd *sc = tl_cmd->sc;
164 void *mem_ptr, *mem_bidi_ptr = NULL;
165 u32 sg_no_bidi = 0;
166 int ret;
167 /*
168 * Allocate the necessary tasks to complete the received CDB+data
169 */
170 ret = transport_generic_allocate_tasks(se_cmd, tl_cmd->sc->cmnd);
171 if (ret == -1) {
172 /* Out of Resources */
173 return PYX_TRANSPORT_LU_COMM_FAILURE;
174 } else if (ret == -2) {
175 /*
176 * Handle case for SAM_STAT_RESERVATION_CONFLICT
177 */
178 if (se_cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT)
179 return PYX_TRANSPORT_RESERVATION_CONFLICT;
180 /*
181 * Otherwise, return SAM_STAT_CHECK_CONDITION and return
182 * sense data.
183 */
184 return PYX_TRANSPORT_USE_SENSE_REASON;
185 }
186 /*
187 * Setup the struct scatterlist memory from the received
188 * struct scsi_cmnd.
189 */
190 if (scsi_sg_count(sc)) {
191 se_cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM;
192 mem_ptr = (void *)scsi_sglist(sc);
193 /*
194 * For BIDI commands, pass in the extra READ buffer
195 * to transport_generic_map_mem_to_cmd() below..
196 */
197 if (T_TASK(se_cmd)->t_tasks_bidi) {
198 struct scsi_data_buffer *sdb = scsi_in(sc);
199
200 mem_bidi_ptr = (void *)sdb->table.sgl;
201 sg_no_bidi = sdb->table.nents;
202 }
203 } else {
204 /*
205 * Used for DMA_NONE
206 */
207 mem_ptr = NULL;
208 }
209 /*
210 * Map the SG memory into struct se_mem->page linked list using the same
211 * physical memory at sg->page_link.
212 */
213 ret = transport_generic_map_mem_to_cmd(se_cmd, mem_ptr,
214 scsi_sg_count(sc), mem_bidi_ptr, sg_no_bidi);
215 if (ret < 0)
216 return PYX_TRANSPORT_LU_COMM_FAILURE;
217
218 return 0;
219}
220
221/*
222 * Called from struct target_core_fabric_ops->check_stop_free()
223 */
224static void tcm_loop_check_stop_free(struct se_cmd *se_cmd)
225{
226 /*
227 * Do not release struct se_cmd's containing a valid TMR
228 * pointer. These will be released directly in tcm_loop_device_reset()
229 * with transport_generic_free_cmd().
230 */
231 if (se_cmd->se_tmr_req)
232 return;
233 /*
234 * Release the struct se_cmd, which will make a callback to release
235 * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd()
236 */
237 transport_generic_free_cmd(se_cmd, 0, 1, 0);
238}
239
240/*
241 * Called from struct target_core_fabric_ops->release_cmd_to_pool()
242 */
243static void tcm_loop_deallocate_core_cmd(struct se_cmd *se_cmd)
244{
245 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
246 struct tcm_loop_cmd, tl_se_cmd);
247
248 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
249}
250
251static int tcm_loop_proc_info(struct Scsi_Host *host, char *buffer,
252 char **start, off_t offset,
253 int length, int inout)
254{
255 return sprintf(buffer, "tcm_loop_proc_info()\n");
256}
257
258static int tcm_loop_driver_probe(struct device *);
259static int tcm_loop_driver_remove(struct device *);
260
261static int pseudo_lld_bus_match(struct device *dev,
262 struct device_driver *dev_driver)
263{
264 return 1;
265}
266
267static struct bus_type tcm_loop_lld_bus = {
268 .name = "tcm_loop_bus",
269 .match = pseudo_lld_bus_match,
270 .probe = tcm_loop_driver_probe,
271 .remove = tcm_loop_driver_remove,
272};
273
274static struct device_driver tcm_loop_driverfs = {
275 .name = "tcm_loop",
276 .bus = &tcm_loop_lld_bus,
277};
278/*
279 * Used with root_device_register() in tcm_loop_alloc_core_bus() below
280 */
281struct device *tcm_loop_primary;
282
283/*
284 * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and
285 * drivers/scsi/libiscsi.c:iscsi_change_queue_depth()
286 */
287static int tcm_loop_change_queue_depth(
288 struct scsi_device *sdev,
289 int depth,
290 int reason)
291{
292 switch (reason) {
293 case SCSI_QDEPTH_DEFAULT:
294 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
295 break;
296 case SCSI_QDEPTH_QFULL:
297 scsi_track_queue_full(sdev, depth);
298 break;
299 case SCSI_QDEPTH_RAMP_UP:
300 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
301 break;
302 default:
303 return -EOPNOTSUPP;
304 }
305 return sdev->queue_depth;
306}
307
308/*
309 * Main entry point from struct scsi_host_template for incoming SCSI CDB+Data
310 * from Linux/SCSI subsystem for SCSI low level device drivers (LLDs)
311 */
312static int tcm_loop_queuecommand(
313 struct Scsi_Host *sh,
314 struct scsi_cmnd *sc)
315{
316 struct se_cmd *se_cmd;
317 struct se_portal_group *se_tpg;
318 struct tcm_loop_hba *tl_hba;
319 struct tcm_loop_tpg *tl_tpg;
320
321 TL_CDB_DEBUG("tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x"
322 " scsi_buf_len: %u\n", sc->device->host->host_no,
323 sc->device->id, sc->device->channel, sc->device->lun,
324 sc->cmnd[0], scsi_bufflen(sc));
325 /*
326 * Locate the tcm_loop_hba_t pointer
327 */
328 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
329 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
330 se_tpg = &tl_tpg->tl_se_tpg;
331 /*
332 * Determine the SAM Task Attribute and allocate tl_cmd and
333 * tl_cmd->tl_se_cmd from TCM infrastructure
334 */
335 se_cmd = tcm_loop_allocate_core_cmd(tl_hba, se_tpg, sc);
336 if (!se_cmd) {
337 sc->scsi_done(sc);
338 return 0;
339 }
340 /*
341 * Queue up the newly allocated to be processed in TCM thread context.
342 */
343 transport_generic_handle_cdb_map(se_cmd);
344 return 0;
345}
346
347/*
348 * Called from SCSI EH process context to issue a LUN_RESET TMR
349 * to struct scsi_device
350 */
351static int tcm_loop_device_reset(struct scsi_cmnd *sc)
352{
353 struct se_cmd *se_cmd = NULL;
354 struct se_portal_group *se_tpg;
355 struct se_session *se_sess;
356 struct tcm_loop_cmd *tl_cmd = NULL;
357 struct tcm_loop_hba *tl_hba;
358 struct tcm_loop_nexus *tl_nexus;
359 struct tcm_loop_tmr *tl_tmr = NULL;
360 struct tcm_loop_tpg *tl_tpg;
361 int ret = FAILED;
362 /*
363 * Locate the tcm_loop_hba_t pointer
364 */
365 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
366 /*
367 * Locate the tl_nexus and se_sess pointers
368 */
369 tl_nexus = tl_hba->tl_nexus;
370 if (!tl_nexus) {
371 printk(KERN_ERR "Unable to perform device reset without"
372 " active I_T Nexus\n");
373 return FAILED;
374 }
375 se_sess = tl_nexus->se_sess;
376 /*
377 * Locate the tl_tpg and se_tpg pointers from TargetID in sc->device->id
378 */
379 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
380 se_tpg = &tl_tpg->tl_se_tpg;
381
382 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
383 if (!tl_cmd) {
384 printk(KERN_ERR "Unable to allocate memory for tl_cmd\n");
385 return FAILED;
386 }
387
388 tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
389 if (!tl_tmr) {
390 printk(KERN_ERR "Unable to allocate memory for tl_tmr\n");
391 goto release;
392 }
393 init_waitqueue_head(&tl_tmr->tl_tmr_wait);
394
395 se_cmd = &tl_cmd->tl_se_cmd;
396 /*
397 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
398 */
399 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
Nicholas Bellinger61db1802011-05-19 20:19:14 -0700400 DMA_NONE, MSG_SIMPLE_TAG,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700401 &tl_cmd->tl_sense_buf[0]);
402 /*
403 * Allocate the LUN_RESET TMR
404 */
405 se_cmd->se_tmr_req = core_tmr_alloc_req(se_cmd, (void *)tl_tmr,
406 TMR_LUN_RESET);
Dan Carpenter552523d2011-06-15 09:41:33 -0700407 if (IS_ERR(se_cmd->se_tmr_req))
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700408 goto release;
409 /*
410 * Locate the underlying TCM struct se_lun from sc->device->lun
411 */
412 if (transport_get_lun_for_tmr(se_cmd, sc->device->lun) < 0)
413 goto release;
414 /*
415 * Queue the TMR to TCM Core and sleep waiting for tcm_loop_queue_tm_rsp()
416 * to wake us up.
417 */
418 transport_generic_handle_tmr(se_cmd);
419 wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
420 /*
421 * The TMR LUN_RESET has completed, check the response status and
422 * then release allocations.
423 */
424 ret = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
425 SUCCESS : FAILED;
426release:
427 if (se_cmd)
428 transport_generic_free_cmd(se_cmd, 1, 1, 0);
429 else
430 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
431 kfree(tl_tmr);
432 return ret;
433}
434
435static int tcm_loop_slave_alloc(struct scsi_device *sd)
436{
437 set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
438 return 0;
439}
440
441static int tcm_loop_slave_configure(struct scsi_device *sd)
442{
443 return 0;
444}
445
446static struct scsi_host_template tcm_loop_driver_template = {
447 .proc_info = tcm_loop_proc_info,
448 .proc_name = "tcm_loopback",
449 .name = "TCM_Loopback",
450 .queuecommand = tcm_loop_queuecommand,
451 .change_queue_depth = tcm_loop_change_queue_depth,
452 .eh_device_reset_handler = tcm_loop_device_reset,
453 .can_queue = TL_SCSI_CAN_QUEUE,
454 .this_id = -1,
455 .sg_tablesize = TL_SCSI_SG_TABLESIZE,
456 .cmd_per_lun = TL_SCSI_CMD_PER_LUN,
457 .max_sectors = TL_SCSI_MAX_SECTORS,
458 .use_clustering = DISABLE_CLUSTERING,
459 .slave_alloc = tcm_loop_slave_alloc,
460 .slave_configure = tcm_loop_slave_configure,
461 .module = THIS_MODULE,
462};
463
464static int tcm_loop_driver_probe(struct device *dev)
465{
466 struct tcm_loop_hba *tl_hba;
467 struct Scsi_Host *sh;
468 int error;
469
470 tl_hba = to_tcm_loop_hba(dev);
471
472 sh = scsi_host_alloc(&tcm_loop_driver_template,
473 sizeof(struct tcm_loop_hba));
474 if (!sh) {
475 printk(KERN_ERR "Unable to allocate struct scsi_host\n");
476 return -ENODEV;
477 }
478 tl_hba->sh = sh;
479
480 /*
481 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
482 */
483 *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
484 /*
485 * Setup single ID, Channel and LUN for now..
486 */
487 sh->max_id = 2;
488 sh->max_lun = 0;
489 sh->max_channel = 0;
490 sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN;
491
492 error = scsi_add_host(sh, &tl_hba->dev);
493 if (error) {
494 printk(KERN_ERR "%s: scsi_add_host failed\n", __func__);
495 scsi_host_put(sh);
496 return -ENODEV;
497 }
498 return 0;
499}
500
501static int tcm_loop_driver_remove(struct device *dev)
502{
503 struct tcm_loop_hba *tl_hba;
504 struct Scsi_Host *sh;
505
506 tl_hba = to_tcm_loop_hba(dev);
507 sh = tl_hba->sh;
508
509 scsi_remove_host(sh);
510 scsi_host_put(sh);
511 return 0;
512}
513
514static void tcm_loop_release_adapter(struct device *dev)
515{
516 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
517
518 kfree(tl_hba);
519}
520
521/*
522 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
523 */
524static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
525{
526 int ret;
527
528 tl_hba->dev.bus = &tcm_loop_lld_bus;
529 tl_hba->dev.parent = tcm_loop_primary;
530 tl_hba->dev.release = &tcm_loop_release_adapter;
531 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
532
533 ret = device_register(&tl_hba->dev);
534 if (ret) {
535 printk(KERN_ERR "device_register() failed for"
536 " tl_hba->dev: %d\n", ret);
537 return -ENODEV;
538 }
539
540 return 0;
541}
542
543/*
544 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
545 * tcm_loop SCSI bus.
546 */
547static int tcm_loop_alloc_core_bus(void)
548{
549 int ret;
550
551 tcm_loop_primary = root_device_register("tcm_loop_0");
552 if (IS_ERR(tcm_loop_primary)) {
553 printk(KERN_ERR "Unable to allocate tcm_loop_primary\n");
554 return PTR_ERR(tcm_loop_primary);
555 }
556
557 ret = bus_register(&tcm_loop_lld_bus);
558 if (ret) {
559 printk(KERN_ERR "bus_register() failed for tcm_loop_lld_bus\n");
560 goto dev_unreg;
561 }
562
563 ret = driver_register(&tcm_loop_driverfs);
564 if (ret) {
565 printk(KERN_ERR "driver_register() failed for"
566 "tcm_loop_driverfs\n");
567 goto bus_unreg;
568 }
569
570 printk(KERN_INFO "Initialized TCM Loop Core Bus\n");
571 return ret;
572
573bus_unreg:
574 bus_unregister(&tcm_loop_lld_bus);
575dev_unreg:
576 root_device_unregister(tcm_loop_primary);
577 return ret;
578}
579
580static void tcm_loop_release_core_bus(void)
581{
582 driver_unregister(&tcm_loop_driverfs);
583 bus_unregister(&tcm_loop_lld_bus);
584 root_device_unregister(tcm_loop_primary);
585
586 printk(KERN_INFO "Releasing TCM Loop Core BUS\n");
587}
588
589static char *tcm_loop_get_fabric_name(void)
590{
591 return "loopback";
592}
593
594static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
595{
596 struct tcm_loop_tpg *tl_tpg =
597 (struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr;
598 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
599 /*
600 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
601 * time based on the protocol dependent prefix of the passed configfs group.
602 *
603 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
604 * ProtocolID using target_core_fabric_lib.c symbols.
605 */
606 switch (tl_hba->tl_proto_id) {
607 case SCSI_PROTOCOL_SAS:
608 return sas_get_fabric_proto_ident(se_tpg);
609 case SCSI_PROTOCOL_FCP:
610 return fc_get_fabric_proto_ident(se_tpg);
611 case SCSI_PROTOCOL_ISCSI:
612 return iscsi_get_fabric_proto_ident(se_tpg);
613 default:
614 printk(KERN_ERR "Unknown tl_proto_id: 0x%02x, using"
615 " SAS emulation\n", tl_hba->tl_proto_id);
616 break;
617 }
618
619 return sas_get_fabric_proto_ident(se_tpg);
620}
621
622static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
623{
624 struct tcm_loop_tpg *tl_tpg =
625 (struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr;
626 /*
627 * Return the passed NAA identifier for the SAS Target Port
628 */
629 return &tl_tpg->tl_hba->tl_wwn_address[0];
630}
631
632static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
633{
634 struct tcm_loop_tpg *tl_tpg =
635 (struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr;
636 /*
637 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
638 * to represent the SCSI Target Port.
639 */
640 return tl_tpg->tl_tpgt;
641}
642
643static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
644{
645 return 1;
646}
647
648static u32 tcm_loop_get_pr_transport_id(
649 struct se_portal_group *se_tpg,
650 struct se_node_acl *se_nacl,
651 struct t10_pr_registration *pr_reg,
652 int *format_code,
653 unsigned char *buf)
654{
655 struct tcm_loop_tpg *tl_tpg =
656 (struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr;
657 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
658
659 switch (tl_hba->tl_proto_id) {
660 case SCSI_PROTOCOL_SAS:
661 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
662 format_code, buf);
663 case SCSI_PROTOCOL_FCP:
664 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
665 format_code, buf);
666 case SCSI_PROTOCOL_ISCSI:
667 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
668 format_code, buf);
669 default:
670 printk(KERN_ERR "Unknown tl_proto_id: 0x%02x, using"
671 " SAS emulation\n", tl_hba->tl_proto_id);
672 break;
673 }
674
675 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
676 format_code, buf);
677}
678
679static u32 tcm_loop_get_pr_transport_id_len(
680 struct se_portal_group *se_tpg,
681 struct se_node_acl *se_nacl,
682 struct t10_pr_registration *pr_reg,
683 int *format_code)
684{
685 struct tcm_loop_tpg *tl_tpg =
686 (struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr;
687 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
688
689 switch (tl_hba->tl_proto_id) {
690 case SCSI_PROTOCOL_SAS:
691 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
692 format_code);
693 case SCSI_PROTOCOL_FCP:
694 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
695 format_code);
696 case SCSI_PROTOCOL_ISCSI:
697 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
698 format_code);
699 default:
700 printk(KERN_ERR "Unknown tl_proto_id: 0x%02x, using"
701 " SAS emulation\n", tl_hba->tl_proto_id);
702 break;
703 }
704
705 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
706 format_code);
707}
708
709/*
710 * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
711 * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
712 */
713static char *tcm_loop_parse_pr_out_transport_id(
714 struct se_portal_group *se_tpg,
715 const char *buf,
716 u32 *out_tid_len,
717 char **port_nexus_ptr)
718{
719 struct tcm_loop_tpg *tl_tpg =
720 (struct tcm_loop_tpg *)se_tpg->se_tpg_fabric_ptr;
721 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
722
723 switch (tl_hba->tl_proto_id) {
724 case SCSI_PROTOCOL_SAS:
725 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
726 port_nexus_ptr);
727 case SCSI_PROTOCOL_FCP:
728 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
729 port_nexus_ptr);
730 case SCSI_PROTOCOL_ISCSI:
731 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
732 port_nexus_ptr);
733 default:
734 printk(KERN_ERR "Unknown tl_proto_id: 0x%02x, using"
735 " SAS emulation\n", tl_hba->tl_proto_id);
736 break;
737 }
738
739 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
740 port_nexus_ptr);
741}
742
743/*
744 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
745 * based upon the incoming fabric dependent SCSI Initiator Port
746 */
747static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
748{
749 return 1;
750}
751
752static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
753{
754 return 0;
755}
756
757/*
758 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
759 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
760 */
761static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
762{
763 return 0;
764}
765
766/*
767 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
768 * never be called for TCM_Loop by target_core_fabric_configfs.c code.
769 * It has been added here as a nop for target_fabric_tf_ops_check()
770 */
771static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
772{
773 return 0;
774}
775
776static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
777 struct se_portal_group *se_tpg)
778{
779 struct tcm_loop_nacl *tl_nacl;
780
781 tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
782 if (!tl_nacl) {
783 printk(KERN_ERR "Unable to allocate struct tcm_loop_nacl\n");
784 return NULL;
785 }
786
787 return &tl_nacl->se_node_acl;
788}
789
790static void tcm_loop_tpg_release_fabric_acl(
791 struct se_portal_group *se_tpg,
792 struct se_node_acl *se_nacl)
793{
794 struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
795 struct tcm_loop_nacl, se_node_acl);
796
797 kfree(tl_nacl);
798}
799
800static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
801{
802 return 1;
803}
804
805static void tcm_loop_new_cmd_failure(struct se_cmd *se_cmd)
806{
807 /*
808 * Since TCM_loop is already passing struct scatterlist data from
809 * struct scsi_cmnd, no more Linux/SCSI failure dependent state need
810 * to be handled here.
811 */
812 return;
813}
814
815static int tcm_loop_is_state_remove(struct se_cmd *se_cmd)
816{
817 /*
818 * Assume struct scsi_cmnd is not in remove state..
819 */
820 return 0;
821}
822
823static int tcm_loop_sess_logged_in(struct se_session *se_sess)
824{
825 /*
826 * Assume that TL Nexus is always active
827 */
828 return 1;
829}
830
831static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
832{
833 return 1;
834}
835
836static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
837{
838 return;
839}
840
841static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
842{
843 return 1;
844}
845
846static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
847{
848 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
849 struct tcm_loop_cmd, tl_se_cmd);
850
851 return tl_cmd->sc_cmd_state;
852}
853
854static int tcm_loop_shutdown_session(struct se_session *se_sess)
855{
856 return 0;
857}
858
859static void tcm_loop_close_session(struct se_session *se_sess)
860{
861 return;
862};
863
864static void tcm_loop_stop_session(
865 struct se_session *se_sess,
866 int sess_sleep,
867 int conn_sleep)
868{
869 return;
870}
871
872static void tcm_loop_fall_back_to_erl0(struct se_session *se_sess)
873{
874 return;
875}
876
877static int tcm_loop_write_pending(struct se_cmd *se_cmd)
878{
879 /*
880 * Since Linux/SCSI has already sent down a struct scsi_cmnd
881 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
882 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
883 * format with transport_generic_map_mem_to_cmd().
884 *
885 * We now tell TCM to add this WRITE CDB directly into the TCM storage
886 * object execution queue.
887 */
888 transport_generic_process_write(se_cmd);
889 return 0;
890}
891
892static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
893{
894 return 0;
895}
896
897static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
898{
899 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
900 struct tcm_loop_cmd, tl_se_cmd);
901 struct scsi_cmnd *sc = tl_cmd->sc;
902
903 TL_CDB_DEBUG("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
904 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
905
906 sc->result = SAM_STAT_GOOD;
907 set_host_byte(sc, DID_OK);
908 sc->scsi_done(sc);
909 return 0;
910}
911
912static int tcm_loop_queue_status(struct se_cmd *se_cmd)
913{
914 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
915 struct tcm_loop_cmd, tl_se_cmd);
916 struct scsi_cmnd *sc = tl_cmd->sc;
917
918 TL_CDB_DEBUG("tcm_loop_queue_status() called for scsi_cmnd: %p"
919 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
920
921 if (se_cmd->sense_buffer &&
922 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
923 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
924
925 memcpy((void *)sc->sense_buffer, (void *)se_cmd->sense_buffer,
926 SCSI_SENSE_BUFFERSIZE);
927 sc->result = SAM_STAT_CHECK_CONDITION;
928 set_driver_byte(sc, DRIVER_SENSE);
929 } else
930 sc->result = se_cmd->scsi_status;
931
932 set_host_byte(sc, DID_OK);
933 sc->scsi_done(sc);
934 return 0;
935}
936
937static int tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
938{
939 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
940 struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
941 /*
942 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
943 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
944 */
945 atomic_set(&tl_tmr->tmr_complete, 1);
946 wake_up(&tl_tmr->tl_tmr_wait);
947 return 0;
948}
949
950static u16 tcm_loop_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
951{
952 return 0;
953}
954
955static u16 tcm_loop_get_fabric_sense_len(void)
956{
957 return 0;
958}
959
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700960static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
961{
962 switch (tl_hba->tl_proto_id) {
963 case SCSI_PROTOCOL_SAS:
964 return "SAS";
965 case SCSI_PROTOCOL_FCP:
966 return "FCP";
967 case SCSI_PROTOCOL_ISCSI:
968 return "iSCSI";
969 default:
970 break;
971 }
972
973 return "Unknown";
974}
975
976/* Start items for tcm_loop_port_cit */
977
978static int tcm_loop_port_link(
979 struct se_portal_group *se_tpg,
980 struct se_lun *lun)
981{
982 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
983 struct tcm_loop_tpg, tl_se_tpg);
984 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
985
986 atomic_inc(&tl_tpg->tl_tpg_port_count);
987 smp_mb__after_atomic_inc();
988 /*
989 * Add Linux/SCSI struct scsi_device by HCTL
990 */
991 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
992
993 printk(KERN_INFO "TCM_Loop_ConfigFS: Port Link Successful\n");
994 return 0;
995}
996
997static void tcm_loop_port_unlink(
998 struct se_portal_group *se_tpg,
999 struct se_lun *se_lun)
1000{
1001 struct scsi_device *sd;
1002 struct tcm_loop_hba *tl_hba;
1003 struct tcm_loop_tpg *tl_tpg;
1004
1005 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
1006 tl_hba = tl_tpg->tl_hba;
1007
1008 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
1009 se_lun->unpacked_lun);
1010 if (!sd) {
1011 printk(KERN_ERR "Unable to locate struct scsi_device for %d:%d:"
1012 "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
1013 return;
1014 }
1015 /*
1016 * Remove Linux/SCSI struct scsi_device by HCTL
1017 */
1018 scsi_remove_device(sd);
1019 scsi_device_put(sd);
1020
1021 atomic_dec(&tl_tpg->tl_tpg_port_count);
1022 smp_mb__after_atomic_dec();
1023
1024 printk(KERN_INFO "TCM_Loop_ConfigFS: Port Unlink Successful\n");
1025}
1026
1027/* End items for tcm_loop_port_cit */
1028
1029/* Start items for tcm_loop_nexus_cit */
1030
1031static int tcm_loop_make_nexus(
1032 struct tcm_loop_tpg *tl_tpg,
1033 const char *name)
1034{
1035 struct se_portal_group *se_tpg;
1036 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
1037 struct tcm_loop_nexus *tl_nexus;
Dan Carpenter552523d2011-06-15 09:41:33 -07001038 int ret = -ENOMEM;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001039
1040 if (tl_tpg->tl_hba->tl_nexus) {
1041 printk(KERN_INFO "tl_tpg->tl_hba->tl_nexus already exists\n");
1042 return -EEXIST;
1043 }
1044 se_tpg = &tl_tpg->tl_se_tpg;
1045
1046 tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
1047 if (!tl_nexus) {
1048 printk(KERN_ERR "Unable to allocate struct tcm_loop_nexus\n");
1049 return -ENOMEM;
1050 }
1051 /*
1052 * Initialize the struct se_session pointer
1053 */
1054 tl_nexus->se_sess = transport_init_session();
Dan Carpenter552523d2011-06-15 09:41:33 -07001055 if (IS_ERR(tl_nexus->se_sess)) {
1056 ret = PTR_ERR(tl_nexus->se_sess);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001057 goto out;
Dan Carpenter552523d2011-06-15 09:41:33 -07001058 }
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001059 /*
1060 * Since we are running in 'demo mode' this call with generate a
1061 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
1062 * Initiator port name of the passed configfs group 'name'.
1063 */
1064 tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1065 se_tpg, (unsigned char *)name);
1066 if (!tl_nexus->se_sess->se_node_acl) {
1067 transport_free_session(tl_nexus->se_sess);
1068 goto out;
1069 }
1070 /*
1071 * Now, register the SAS I_T Nexus as active with the call to
1072 * transport_register_session()
1073 */
1074 __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
1075 tl_nexus->se_sess, (void *)tl_nexus);
1076 tl_tpg->tl_hba->tl_nexus = tl_nexus;
1077 printk(KERN_INFO "TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
1078 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
1079 name);
1080 return 0;
1081
1082out:
1083 kfree(tl_nexus);
Dan Carpenter552523d2011-06-15 09:41:33 -07001084 return ret;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001085}
1086
1087static int tcm_loop_drop_nexus(
1088 struct tcm_loop_tpg *tpg)
1089{
1090 struct se_session *se_sess;
1091 struct tcm_loop_nexus *tl_nexus;
1092 struct tcm_loop_hba *tl_hba = tpg->tl_hba;
1093
1094 tl_nexus = tpg->tl_hba->tl_nexus;
1095 if (!tl_nexus)
1096 return -ENODEV;
1097
1098 se_sess = tl_nexus->se_sess;
1099 if (!se_sess)
1100 return -ENODEV;
1101
1102 if (atomic_read(&tpg->tl_tpg_port_count)) {
1103 printk(KERN_ERR "Unable to remove TCM_Loop I_T Nexus with"
1104 " active TPG port count: %d\n",
1105 atomic_read(&tpg->tl_tpg_port_count));
1106 return -EPERM;
1107 }
1108
1109 printk(KERN_INFO "TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
1110 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
1111 tl_nexus->se_sess->se_node_acl->initiatorname);
1112 /*
1113 * Release the SCSI I_T Nexus to the emulated SAS Target Port
1114 */
1115 transport_deregister_session(tl_nexus->se_sess);
1116 tpg->tl_hba->tl_nexus = NULL;
1117 kfree(tl_nexus);
1118 return 0;
1119}
1120
1121/* End items for tcm_loop_nexus_cit */
1122
1123static ssize_t tcm_loop_tpg_show_nexus(
1124 struct se_portal_group *se_tpg,
1125 char *page)
1126{
1127 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1128 struct tcm_loop_tpg, tl_se_tpg);
1129 struct tcm_loop_nexus *tl_nexus;
1130 ssize_t ret;
1131
1132 tl_nexus = tl_tpg->tl_hba->tl_nexus;
1133 if (!tl_nexus)
1134 return -ENODEV;
1135
1136 ret = snprintf(page, PAGE_SIZE, "%s\n",
1137 tl_nexus->se_sess->se_node_acl->initiatorname);
1138
1139 return ret;
1140}
1141
1142static ssize_t tcm_loop_tpg_store_nexus(
1143 struct se_portal_group *se_tpg,
1144 const char *page,
1145 size_t count)
1146{
1147 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1148 struct tcm_loop_tpg, tl_se_tpg);
1149 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
1150 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
1151 int ret;
1152 /*
1153 * Shutdown the active I_T nexus if 'NULL' is passed..
1154 */
1155 if (!strncmp(page, "NULL", 4)) {
1156 ret = tcm_loop_drop_nexus(tl_tpg);
1157 return (!ret) ? count : ret;
1158 }
1159 /*
1160 * Otherwise make sure the passed virtual Initiator port WWN matches
1161 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
1162 * tcm_loop_make_nexus()
1163 */
Dan Carpenter60d645a2011-06-15 10:03:05 -07001164 if (strlen(page) >= TL_WWN_ADDR_LEN) {
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001165 printk(KERN_ERR "Emulated NAA Sas Address: %s, exceeds"
1166 " max: %d\n", page, TL_WWN_ADDR_LEN);
1167 return -EINVAL;
1168 }
1169 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
1170
1171 ptr = strstr(i_port, "naa.");
1172 if (ptr) {
1173 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
1174 printk(KERN_ERR "Passed SAS Initiator Port %s does not"
1175 " match target port protoid: %s\n", i_port,
1176 tcm_loop_dump_proto_id(tl_hba));
1177 return -EINVAL;
1178 }
1179 port_ptr = &i_port[0];
1180 goto check_newline;
1181 }
1182 ptr = strstr(i_port, "fc.");
1183 if (ptr) {
1184 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
1185 printk(KERN_ERR "Passed FCP Initiator Port %s does not"
1186 " match target port protoid: %s\n", i_port,
1187 tcm_loop_dump_proto_id(tl_hba));
1188 return -EINVAL;
1189 }
1190 port_ptr = &i_port[3]; /* Skip over "fc." */
1191 goto check_newline;
1192 }
1193 ptr = strstr(i_port, "iqn.");
1194 if (ptr) {
1195 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
1196 printk(KERN_ERR "Passed iSCSI Initiator Port %s does not"
1197 " match target port protoid: %s\n", i_port,
1198 tcm_loop_dump_proto_id(tl_hba));
1199 return -EINVAL;
1200 }
1201 port_ptr = &i_port[0];
1202 goto check_newline;
1203 }
1204 printk(KERN_ERR "Unable to locate prefix for emulated Initiator Port:"
1205 " %s\n", i_port);
1206 return -EINVAL;
1207 /*
1208 * Clear any trailing newline for the NAA WWN
1209 */
1210check_newline:
1211 if (i_port[strlen(i_port)-1] == '\n')
1212 i_port[strlen(i_port)-1] = '\0';
1213
1214 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
1215 if (ret < 0)
1216 return ret;
1217
1218 return count;
1219}
1220
1221TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
1222
1223static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
1224 &tcm_loop_tpg_nexus.attr,
1225 NULL,
1226};
1227
1228/* Start items for tcm_loop_naa_cit */
1229
1230struct se_portal_group *tcm_loop_make_naa_tpg(
1231 struct se_wwn *wwn,
1232 struct config_group *group,
1233 const char *name)
1234{
1235 struct tcm_loop_hba *tl_hba = container_of(wwn,
1236 struct tcm_loop_hba, tl_hba_wwn);
1237 struct tcm_loop_tpg *tl_tpg;
1238 char *tpgt_str, *end_ptr;
1239 int ret;
1240 unsigned short int tpgt;
1241
1242 tpgt_str = strstr(name, "tpgt_");
1243 if (!tpgt_str) {
1244 printk(KERN_ERR "Unable to locate \"tpgt_#\" directory"
1245 " group\n");
1246 return ERR_PTR(-EINVAL);
1247 }
1248 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1249 tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1250
1251 if (tpgt > TL_TPGS_PER_HBA) {
1252 printk(KERN_ERR "Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
1253 " %u\n", tpgt, TL_TPGS_PER_HBA);
1254 return ERR_PTR(-EINVAL);
1255 }
1256 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
1257 tl_tpg->tl_hba = tl_hba;
1258 tl_tpg->tl_tpgt = tpgt;
1259 /*
1260 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
1261 */
1262 ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
1263 wwn, &tl_tpg->tl_se_tpg, (void *)tl_tpg,
1264 TRANSPORT_TPG_TYPE_NORMAL);
1265 if (ret < 0)
1266 return ERR_PTR(-ENOMEM);
1267
1268 printk(KERN_INFO "TCM_Loop_ConfigFS: Allocated Emulated %s"
1269 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1270 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1271
1272 return &tl_tpg->tl_se_tpg;
1273}
1274
1275void tcm_loop_drop_naa_tpg(
1276 struct se_portal_group *se_tpg)
1277{
1278 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1279 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1280 struct tcm_loop_tpg, tl_se_tpg);
1281 struct tcm_loop_hba *tl_hba;
1282 unsigned short tpgt;
1283
1284 tl_hba = tl_tpg->tl_hba;
1285 tpgt = tl_tpg->tl_tpgt;
1286 /*
1287 * Release the I_T Nexus for the Virtual SAS link if present
1288 */
1289 tcm_loop_drop_nexus(tl_tpg);
1290 /*
1291 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
1292 */
1293 core_tpg_deregister(se_tpg);
1294
1295 printk(KERN_INFO "TCM_Loop_ConfigFS: Deallocated Emulated %s"
1296 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1297 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1298}
1299
1300/* End items for tcm_loop_naa_cit */
1301
1302/* Start items for tcm_loop_cit */
1303
1304struct se_wwn *tcm_loop_make_scsi_hba(
1305 struct target_fabric_configfs *tf,
1306 struct config_group *group,
1307 const char *name)
1308{
1309 struct tcm_loop_hba *tl_hba;
1310 struct Scsi_Host *sh;
1311 char *ptr;
1312 int ret, off = 0;
1313
1314 tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
1315 if (!tl_hba) {
1316 printk(KERN_ERR "Unable to allocate struct tcm_loop_hba\n");
1317 return ERR_PTR(-ENOMEM);
1318 }
1319 /*
1320 * Determine the emulated Protocol Identifier and Target Port Name
1321 * based on the incoming configfs directory name.
1322 */
1323 ptr = strstr(name, "naa.");
1324 if (ptr) {
1325 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1326 goto check_len;
1327 }
1328 ptr = strstr(name, "fc.");
1329 if (ptr) {
1330 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1331 off = 3; /* Skip over "fc." */
1332 goto check_len;
1333 }
1334 ptr = strstr(name, "iqn.");
1335 if (ptr) {
1336 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
1337 goto check_len;
1338 }
1339
1340 printk(KERN_ERR "Unable to locate prefix for emulated Target Port:"
1341 " %s\n", name);
1342 return ERR_PTR(-EINVAL);
1343
1344check_len:
Dan Carpenter60d645a2011-06-15 10:03:05 -07001345 if (strlen(name) >= TL_WWN_ADDR_LEN) {
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001346 printk(KERN_ERR "Emulated NAA %s Address: %s, exceeds"
1347 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1348 TL_WWN_ADDR_LEN);
1349 kfree(tl_hba);
1350 return ERR_PTR(-EINVAL);
1351 }
1352 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1353
1354 /*
1355 * Call device_register(tl_hba->dev) to register the emulated
1356 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1357 * device_register() callbacks in tcm_loop_driver_probe()
1358 */
1359 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1360 if (ret)
1361 goto out;
1362
1363 sh = tl_hba->sh;
1364 tcm_loop_hba_no_cnt++;
1365 printk(KERN_INFO "TCM_Loop_ConfigFS: Allocated emulated Target"
1366 " %s Address: %s at Linux/SCSI Host ID: %d\n",
1367 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1368
1369 return &tl_hba->tl_hba_wwn;
1370out:
1371 kfree(tl_hba);
1372 return ERR_PTR(ret);
1373}
1374
1375void tcm_loop_drop_scsi_hba(
1376 struct se_wwn *wwn)
1377{
1378 struct tcm_loop_hba *tl_hba = container_of(wwn,
1379 struct tcm_loop_hba, tl_hba_wwn);
1380 int host_no = tl_hba->sh->host_no;
1381 /*
1382 * Call device_unregister() on the original tl_hba->dev.
1383 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1384 * release *tl_hba;
1385 */
1386 device_unregister(&tl_hba->dev);
1387
1388 printk(KERN_INFO "TCM_Loop_ConfigFS: Deallocated emulated Target"
1389 " SAS Address: %s at Linux/SCSI Host ID: %d\n",
1390 config_item_name(&wwn->wwn_group.cg_item), host_no);
1391}
1392
1393/* Start items for tcm_loop_cit */
1394static ssize_t tcm_loop_wwn_show_attr_version(
1395 struct target_fabric_configfs *tf,
1396 char *page)
1397{
1398 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1399}
1400
1401TF_WWN_ATTR_RO(tcm_loop, version);
1402
1403static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1404 &tcm_loop_wwn_version.attr,
1405 NULL,
1406};
1407
1408/* End items for tcm_loop_cit */
1409
1410static int tcm_loop_register_configfs(void)
1411{
1412 struct target_fabric_configfs *fabric;
1413 struct config_group *tf_cg;
1414 int ret;
1415 /*
1416 * Set the TCM Loop HBA counter to zero
1417 */
1418 tcm_loop_hba_no_cnt = 0;
1419 /*
1420 * Register the top level struct config_item_type with TCM core
1421 */
1422 fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
1423 if (!fabric) {
1424 printk(KERN_ERR "tcm_loop_register_configfs() failed!\n");
1425 return -1;
1426 }
1427 /*
1428 * Setup the fabric API of function pointers used by target_core_mod
1429 */
1430 fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
1431 fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
1432 fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
1433 fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
1434 fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
1435 fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
1436 fabric->tf_ops.tpg_get_pr_transport_id_len =
1437 &tcm_loop_get_pr_transport_id_len;
1438 fabric->tf_ops.tpg_parse_pr_out_transport_id =
1439 &tcm_loop_parse_pr_out_transport_id;
1440 fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
1441 fabric->tf_ops.tpg_check_demo_mode_cache =
1442 &tcm_loop_check_demo_mode_cache;
1443 fabric->tf_ops.tpg_check_demo_mode_write_protect =
1444 &tcm_loop_check_demo_mode_write_protect;
1445 fabric->tf_ops.tpg_check_prod_mode_write_protect =
1446 &tcm_loop_check_prod_mode_write_protect;
1447 /*
1448 * The TCM loopback fabric module runs in demo-mode to a local
1449 * virtual SCSI device, so fabric dependent initator ACLs are
1450 * not required.
1451 */
1452 fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
1453 fabric->tf_ops.tpg_release_fabric_acl =
1454 &tcm_loop_tpg_release_fabric_acl;
1455 fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
1456 /*
1457 * Since tcm_loop is mapping physical memory from Linux/SCSI
1458 * struct scatterlist arrays for each struct scsi_cmnd I/O,
1459 * we do not need TCM to allocate a iovec array for
1460 * virtual memory address mappings
1461 */
1462 fabric->tf_ops.alloc_cmd_iovecs = NULL;
1463 /*
1464 * Used for setting up remaining TCM resources in process context
1465 */
1466 fabric->tf_ops.new_cmd_map = &tcm_loop_new_cmd_map;
1467 fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
1468 fabric->tf_ops.release_cmd_to_pool = &tcm_loop_deallocate_core_cmd;
1469 fabric->tf_ops.release_cmd_direct = &tcm_loop_deallocate_core_cmd;
1470 fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
1471 fabric->tf_ops.close_session = &tcm_loop_close_session;
1472 fabric->tf_ops.stop_session = &tcm_loop_stop_session;
1473 fabric->tf_ops.fall_back_to_erl0 = &tcm_loop_fall_back_to_erl0;
1474 fabric->tf_ops.sess_logged_in = &tcm_loop_sess_logged_in;
1475 fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
1476 fabric->tf_ops.sess_get_initiator_sid = NULL;
1477 fabric->tf_ops.write_pending = &tcm_loop_write_pending;
1478 fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
1479 /*
1480 * Not used for TCM loopback
1481 */
1482 fabric->tf_ops.set_default_node_attributes =
1483 &tcm_loop_set_default_node_attributes;
1484 fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
1485 fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
1486 fabric->tf_ops.new_cmd_failure = &tcm_loop_new_cmd_failure;
1487 fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
1488 fabric->tf_ops.queue_status = &tcm_loop_queue_status;
1489 fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
1490 fabric->tf_ops.set_fabric_sense_len = &tcm_loop_set_fabric_sense_len;
1491 fabric->tf_ops.get_fabric_sense_len = &tcm_loop_get_fabric_sense_len;
1492 fabric->tf_ops.is_state_remove = &tcm_loop_is_state_remove;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001493
1494 tf_cg = &fabric->tf_group;
1495 /*
1496 * Setup function pointers for generic logic in target_core_fabric_configfs.c
1497 */
1498 fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
1499 fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
1500 fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
1501 fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
1502 /*
1503 * fabric_post_link() and fabric_pre_unlink() are used for
1504 * registration and release of TCM Loop Virtual SCSI LUNs.
1505 */
1506 fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
1507 fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
1508 fabric->tf_ops.fabric_make_np = NULL;
1509 fabric->tf_ops.fabric_drop_np = NULL;
1510 /*
1511 * Setup default attribute lists for various fabric->tf_cit_tmpl
1512 */
1513 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
1514 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
1515 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1516 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1517 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1518 /*
1519 * Once fabric->tf_ops has been setup, now register the fabric for
1520 * use within TCM
1521 */
1522 ret = target_fabric_configfs_register(fabric);
1523 if (ret < 0) {
1524 printk(KERN_ERR "target_fabric_configfs_register() for"
1525 " TCM_Loop failed!\n");
1526 target_fabric_configfs_free(fabric);
1527 return -1;
1528 }
1529 /*
1530 * Setup our local pointer to *fabric.
1531 */
1532 tcm_loop_fabric_configfs = fabric;
1533 printk(KERN_INFO "TCM_LOOP[0] - Set fabric ->"
1534 " tcm_loop_fabric_configfs\n");
1535 return 0;
1536}
1537
1538static void tcm_loop_deregister_configfs(void)
1539{
1540 if (!tcm_loop_fabric_configfs)
1541 return;
1542
1543 target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
1544 tcm_loop_fabric_configfs = NULL;
1545 printk(KERN_INFO "TCM_LOOP[0] - Cleared"
1546 " tcm_loop_fabric_configfs\n");
1547}
1548
1549static int __init tcm_loop_fabric_init(void)
1550{
1551 int ret;
1552
1553 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1554 sizeof(struct tcm_loop_cmd),
1555 __alignof__(struct tcm_loop_cmd),
1556 0, NULL);
1557 if (!tcm_loop_cmd_cache) {
1558 printk(KERN_ERR "kmem_cache_create() for"
1559 " tcm_loop_cmd_cache failed\n");
1560 return -ENOMEM;
1561 }
1562
1563 ret = tcm_loop_alloc_core_bus();
1564 if (ret)
1565 return ret;
1566
1567 ret = tcm_loop_register_configfs();
1568 if (ret) {
1569 tcm_loop_release_core_bus();
1570 return ret;
1571 }
1572
1573 return 0;
1574}
1575
1576static void __exit tcm_loop_fabric_exit(void)
1577{
1578 tcm_loop_deregister_configfs();
1579 tcm_loop_release_core_bus();
1580 kmem_cache_destroy(tcm_loop_cmd_cache);
1581}
1582
1583MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1584MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1585MODULE_LICENSE("GPL");
1586module_init(tcm_loop_fabric_init);
1587module_exit(tcm_loop_fabric_exit);