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