target: Remove unused target_core_fabric_ops.get_fabric_sense_len method
[linux-2.6.git] / drivers / target / loopback / tcm_loop.c
blob4ab1d0b43a85b2d0163dd5f665592d84ae5a6cb9
1 /*******************************************************************************
3 * This file contains the Linux/SCSI LLD virtual SCSI initiator driver
4 * for emulated SAS initiator ports
6 * © Copyright 2011 RisingTide Systems LLC.
8 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
10 * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
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.
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 ****************************************************************************/
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>
35 #include <target/target_core_base.h>
36 #include <target/target_core_fabric.h>
37 #include <target/target_core_fabric_configfs.h>
38 #include <target/target_core_configfs.h>
40 #include "tcm_loop.h"
42 #define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev)
44 /* Local pointer to allocated TCM configfs fabric module */
45 static struct target_fabric_configfs *tcm_loop_fabric_configfs;
47 static struct workqueue_struct *tcm_loop_workqueue;
48 static struct kmem_cache *tcm_loop_cmd_cache;
50 static int tcm_loop_hba_no_cnt;
52 static int tcm_loop_queue_status(struct se_cmd *se_cmd);
55 * Called from struct target_core_fabric_ops->check_stop_free()
57 static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
60 * Do not release struct se_cmd's containing a valid TMR
61 * pointer. These will be released directly in tcm_loop_device_reset()
62 * with transport_generic_free_cmd().
64 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
65 return 0;
67 * Release the struct se_cmd, which will make a callback to release
68 * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd()
70 transport_generic_free_cmd(se_cmd, 0);
71 return 1;
74 static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
76 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
77 struct tcm_loop_cmd, tl_se_cmd);
79 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
82 static int tcm_loop_proc_info(struct Scsi_Host *host, char *buffer,
83 char **start, off_t offset,
84 int length, int inout)
86 return sprintf(buffer, "tcm_loop_proc_info()\n");
89 static int tcm_loop_driver_probe(struct device *);
90 static int tcm_loop_driver_remove(struct device *);
92 static int pseudo_lld_bus_match(struct device *dev,
93 struct device_driver *dev_driver)
95 return 1;
98 static struct bus_type tcm_loop_lld_bus = {
99 .name = "tcm_loop_bus",
100 .match = pseudo_lld_bus_match,
101 .probe = tcm_loop_driver_probe,
102 .remove = tcm_loop_driver_remove,
105 static struct device_driver tcm_loop_driverfs = {
106 .name = "tcm_loop",
107 .bus = &tcm_loop_lld_bus,
110 * Used with root_device_register() in tcm_loop_alloc_core_bus() below
112 struct device *tcm_loop_primary;
115 * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and
116 * drivers/scsi/libiscsi.c:iscsi_change_queue_depth()
118 static int tcm_loop_change_queue_depth(
119 struct scsi_device *sdev,
120 int depth,
121 int reason)
123 switch (reason) {
124 case SCSI_QDEPTH_DEFAULT:
125 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
126 break;
127 case SCSI_QDEPTH_QFULL:
128 scsi_track_queue_full(sdev, depth);
129 break;
130 case SCSI_QDEPTH_RAMP_UP:
131 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
132 break;
133 default:
134 return -EOPNOTSUPP;
136 return sdev->queue_depth;
140 * Locate the SAM Task Attr from struct scsi_cmnd *
142 static int tcm_loop_sam_attr(struct scsi_cmnd *sc)
144 if (sc->device->tagged_supported) {
145 switch (sc->tag) {
146 case HEAD_OF_QUEUE_TAG:
147 return MSG_HEAD_TAG;
148 case ORDERED_QUEUE_TAG:
149 return MSG_ORDERED_TAG;
150 default:
151 break;
155 return MSG_SIMPLE_TAG;
158 static void tcm_loop_submission_work(struct work_struct *work)
160 struct tcm_loop_cmd *tl_cmd =
161 container_of(work, struct tcm_loop_cmd, work);
162 struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
163 struct scsi_cmnd *sc = tl_cmd->sc;
164 struct tcm_loop_nexus *tl_nexus;
165 struct tcm_loop_hba *tl_hba;
166 struct tcm_loop_tpg *tl_tpg;
167 struct scatterlist *sgl_bidi = NULL;
168 u32 sgl_bidi_count = 0;
169 int ret;
171 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
172 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
175 * Ensure that this tl_tpg reference from the incoming sc->device->id
176 * has already been configured via tcm_loop_make_naa_tpg().
178 if (!tl_tpg->tl_hba) {
179 set_host_byte(sc, DID_NO_CONNECT);
180 goto out_done;
183 tl_nexus = tl_hba->tl_nexus;
184 if (!tl_nexus) {
185 scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
186 " does not exist\n");
187 set_host_byte(sc, DID_ERROR);
188 goto out_done;
191 transport_init_se_cmd(se_cmd, tl_tpg->tl_se_tpg.se_tpg_tfo,
192 tl_nexus->se_sess,
193 scsi_bufflen(sc), sc->sc_data_direction,
194 tcm_loop_sam_attr(sc), &tl_cmd->tl_sense_buf[0]);
196 if (scsi_bidi_cmnd(sc)) {
197 struct scsi_data_buffer *sdb = scsi_in(sc);
199 sgl_bidi = sdb->table.sgl;
200 sgl_bidi_count = sdb->table.nents;
201 se_cmd->se_cmd_flags |= SCF_BIDI;
205 if (transport_lookup_cmd_lun(se_cmd, tl_cmd->sc->device->lun) < 0) {
206 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
207 set_host_byte(sc, DID_NO_CONNECT);
208 goto out_done;
212 * Because some userspace code via scsi-generic do not memset their
213 * associated read buffers, go ahead and do that here for type
214 * non-data CDBs. Also note that this is currently guaranteed to be a
215 * single SGL for this case by target core in
216 * target_setup_cmd_from_cdb() -> transport_generic_cmd_sequencer().
218 if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) &&
219 se_cmd->data_direction == DMA_FROM_DEVICE) {
220 struct scatterlist *sg = scsi_sglist(sc);
221 unsigned char *buf = kmap(sg_page(sg)) + sg->offset;
223 if (buf != NULL) {
224 memset(buf, 0, sg->length);
225 kunmap(sg_page(sg));
229 ret = target_setup_cmd_from_cdb(se_cmd, sc->cmnd);
230 if (ret == -ENOMEM) {
231 transport_send_check_condition_and_sense(se_cmd,
232 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
233 transport_generic_free_cmd(se_cmd, 0);
234 return;
235 } else if (ret < 0) {
236 if (se_cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT)
237 tcm_loop_queue_status(se_cmd);
238 else
239 transport_send_check_condition_and_sense(se_cmd,
240 se_cmd->scsi_sense_reason, 0);
241 transport_generic_free_cmd(se_cmd, 0);
242 return;
245 ret = transport_generic_map_mem_to_cmd(se_cmd, scsi_sglist(sc),
246 scsi_sg_count(sc), sgl_bidi, sgl_bidi_count);
247 if (ret) {
248 transport_send_check_condition_and_sense(se_cmd,
249 se_cmd->scsi_sense_reason, 0);
250 transport_generic_free_cmd(se_cmd, 0);
251 return;
253 transport_handle_cdb_direct(se_cmd);
254 return;
256 out_done:
257 sc->scsi_done(sc);
258 return;
262 * ->queuecommand can be and usually is called from interrupt context, so
263 * defer the actual submission to a workqueue.
265 static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
267 struct tcm_loop_cmd *tl_cmd;
269 pr_debug("tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x"
270 " scsi_buf_len: %u\n", sc->device->host->host_no,
271 sc->device->id, sc->device->channel, sc->device->lun,
272 sc->cmnd[0], scsi_bufflen(sc));
274 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
275 if (!tl_cmd) {
276 pr_err("Unable to allocate struct tcm_loop_cmd\n");
277 set_host_byte(sc, DID_ERROR);
278 sc->scsi_done(sc);
279 return 0;
282 tl_cmd->sc = sc;
283 INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
284 queue_work(tcm_loop_workqueue, &tl_cmd->work);
285 return 0;
289 * Called from SCSI EH process context to issue a LUN_RESET TMR
290 * to struct scsi_device
292 static int tcm_loop_device_reset(struct scsi_cmnd *sc)
294 struct se_cmd *se_cmd = NULL;
295 struct se_portal_group *se_tpg;
296 struct se_session *se_sess;
297 struct tcm_loop_cmd *tl_cmd = NULL;
298 struct tcm_loop_hba *tl_hba;
299 struct tcm_loop_nexus *tl_nexus;
300 struct tcm_loop_tmr *tl_tmr = NULL;
301 struct tcm_loop_tpg *tl_tpg;
302 int ret = FAILED, rc;
304 * Locate the tcm_loop_hba_t pointer
306 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
308 * Locate the tl_nexus and se_sess pointers
310 tl_nexus = tl_hba->tl_nexus;
311 if (!tl_nexus) {
312 pr_err("Unable to perform device reset without"
313 " active I_T Nexus\n");
314 return FAILED;
316 se_sess = tl_nexus->se_sess;
318 * Locate the tl_tpg and se_tpg pointers from TargetID in sc->device->id
320 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
321 se_tpg = &tl_tpg->tl_se_tpg;
323 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
324 if (!tl_cmd) {
325 pr_err("Unable to allocate memory for tl_cmd\n");
326 return FAILED;
329 tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
330 if (!tl_tmr) {
331 pr_err("Unable to allocate memory for tl_tmr\n");
332 goto release;
334 init_waitqueue_head(&tl_tmr->tl_tmr_wait);
336 se_cmd = &tl_cmd->tl_se_cmd;
338 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
340 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
341 DMA_NONE, MSG_SIMPLE_TAG,
342 &tl_cmd->tl_sense_buf[0]);
344 rc = core_tmr_alloc_req(se_cmd, tl_tmr, TMR_LUN_RESET, GFP_KERNEL);
345 if (rc < 0)
346 goto release;
348 * Locate the underlying TCM struct se_lun from sc->device->lun
350 if (transport_lookup_tmr_lun(se_cmd, sc->device->lun) < 0)
351 goto release;
353 * Queue the TMR to TCM Core and sleep waiting for tcm_loop_queue_tm_rsp()
354 * to wake us up.
356 transport_generic_handle_tmr(se_cmd);
357 wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
359 * The TMR LUN_RESET has completed, check the response status and
360 * then release allocations.
362 ret = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
363 SUCCESS : FAILED;
364 release:
365 if (se_cmd)
366 transport_generic_free_cmd(se_cmd, 1);
367 else
368 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
369 kfree(tl_tmr);
370 return ret;
373 static int tcm_loop_slave_alloc(struct scsi_device *sd)
375 set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
376 return 0;
379 static int tcm_loop_slave_configure(struct scsi_device *sd)
381 return 0;
384 static struct scsi_host_template tcm_loop_driver_template = {
385 .proc_info = tcm_loop_proc_info,
386 .proc_name = "tcm_loopback",
387 .name = "TCM_Loopback",
388 .queuecommand = tcm_loop_queuecommand,
389 .change_queue_depth = tcm_loop_change_queue_depth,
390 .eh_device_reset_handler = tcm_loop_device_reset,
391 .can_queue = 1024,
392 .this_id = -1,
393 .sg_tablesize = 256,
394 .cmd_per_lun = 1024,
395 .max_sectors = 0xFFFF,
396 .use_clustering = DISABLE_CLUSTERING,
397 .slave_alloc = tcm_loop_slave_alloc,
398 .slave_configure = tcm_loop_slave_configure,
399 .module = THIS_MODULE,
402 static int tcm_loop_driver_probe(struct device *dev)
404 struct tcm_loop_hba *tl_hba;
405 struct Scsi_Host *sh;
406 int error;
408 tl_hba = to_tcm_loop_hba(dev);
410 sh = scsi_host_alloc(&tcm_loop_driver_template,
411 sizeof(struct tcm_loop_hba));
412 if (!sh) {
413 pr_err("Unable to allocate struct scsi_host\n");
414 return -ENODEV;
416 tl_hba->sh = sh;
419 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
421 *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
423 * Setup single ID, Channel and LUN for now..
425 sh->max_id = 2;
426 sh->max_lun = 0;
427 sh->max_channel = 0;
428 sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN;
430 error = scsi_add_host(sh, &tl_hba->dev);
431 if (error) {
432 pr_err("%s: scsi_add_host failed\n", __func__);
433 scsi_host_put(sh);
434 return -ENODEV;
436 return 0;
439 static int tcm_loop_driver_remove(struct device *dev)
441 struct tcm_loop_hba *tl_hba;
442 struct Scsi_Host *sh;
444 tl_hba = to_tcm_loop_hba(dev);
445 sh = tl_hba->sh;
447 scsi_remove_host(sh);
448 scsi_host_put(sh);
449 return 0;
452 static void tcm_loop_release_adapter(struct device *dev)
454 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
456 kfree(tl_hba);
460 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
462 static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
464 int ret;
466 tl_hba->dev.bus = &tcm_loop_lld_bus;
467 tl_hba->dev.parent = tcm_loop_primary;
468 tl_hba->dev.release = &tcm_loop_release_adapter;
469 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
471 ret = device_register(&tl_hba->dev);
472 if (ret) {
473 pr_err("device_register() failed for"
474 " tl_hba->dev: %d\n", ret);
475 return -ENODEV;
478 return 0;
482 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
483 * tcm_loop SCSI bus.
485 static int tcm_loop_alloc_core_bus(void)
487 int ret;
489 tcm_loop_primary = root_device_register("tcm_loop_0");
490 if (IS_ERR(tcm_loop_primary)) {
491 pr_err("Unable to allocate tcm_loop_primary\n");
492 return PTR_ERR(tcm_loop_primary);
495 ret = bus_register(&tcm_loop_lld_bus);
496 if (ret) {
497 pr_err("bus_register() failed for tcm_loop_lld_bus\n");
498 goto dev_unreg;
501 ret = driver_register(&tcm_loop_driverfs);
502 if (ret) {
503 pr_err("driver_register() failed for"
504 "tcm_loop_driverfs\n");
505 goto bus_unreg;
508 pr_debug("Initialized TCM Loop Core Bus\n");
509 return ret;
511 bus_unreg:
512 bus_unregister(&tcm_loop_lld_bus);
513 dev_unreg:
514 root_device_unregister(tcm_loop_primary);
515 return ret;
518 static void tcm_loop_release_core_bus(void)
520 driver_unregister(&tcm_loop_driverfs);
521 bus_unregister(&tcm_loop_lld_bus);
522 root_device_unregister(tcm_loop_primary);
524 pr_debug("Releasing TCM Loop Core BUS\n");
527 static char *tcm_loop_get_fabric_name(void)
529 return "loopback";
532 static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
534 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
535 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
537 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
538 * time based on the protocol dependent prefix of the passed configfs group.
540 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
541 * ProtocolID using target_core_fabric_lib.c symbols.
543 switch (tl_hba->tl_proto_id) {
544 case SCSI_PROTOCOL_SAS:
545 return sas_get_fabric_proto_ident(se_tpg);
546 case SCSI_PROTOCOL_FCP:
547 return fc_get_fabric_proto_ident(se_tpg);
548 case SCSI_PROTOCOL_ISCSI:
549 return iscsi_get_fabric_proto_ident(se_tpg);
550 default:
551 pr_err("Unknown tl_proto_id: 0x%02x, using"
552 " SAS emulation\n", tl_hba->tl_proto_id);
553 break;
556 return sas_get_fabric_proto_ident(se_tpg);
559 static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
561 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
563 * Return the passed NAA identifier for the SAS Target Port
565 return &tl_tpg->tl_hba->tl_wwn_address[0];
568 static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
570 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
572 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
573 * to represent the SCSI Target Port.
575 return tl_tpg->tl_tpgt;
578 static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
580 return 1;
583 static u32 tcm_loop_get_pr_transport_id(
584 struct se_portal_group *se_tpg,
585 struct se_node_acl *se_nacl,
586 struct t10_pr_registration *pr_reg,
587 int *format_code,
588 unsigned char *buf)
590 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
591 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
593 switch (tl_hba->tl_proto_id) {
594 case SCSI_PROTOCOL_SAS:
595 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
596 format_code, buf);
597 case SCSI_PROTOCOL_FCP:
598 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
599 format_code, buf);
600 case SCSI_PROTOCOL_ISCSI:
601 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
602 format_code, buf);
603 default:
604 pr_err("Unknown tl_proto_id: 0x%02x, using"
605 " SAS emulation\n", tl_hba->tl_proto_id);
606 break;
609 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
610 format_code, buf);
613 static u32 tcm_loop_get_pr_transport_id_len(
614 struct se_portal_group *se_tpg,
615 struct se_node_acl *se_nacl,
616 struct t10_pr_registration *pr_reg,
617 int *format_code)
619 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
620 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
622 switch (tl_hba->tl_proto_id) {
623 case SCSI_PROTOCOL_SAS:
624 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
625 format_code);
626 case SCSI_PROTOCOL_FCP:
627 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
628 format_code);
629 case SCSI_PROTOCOL_ISCSI:
630 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
631 format_code);
632 default:
633 pr_err("Unknown tl_proto_id: 0x%02x, using"
634 " SAS emulation\n", tl_hba->tl_proto_id);
635 break;
638 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
639 format_code);
643 * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
644 * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
646 static char *tcm_loop_parse_pr_out_transport_id(
647 struct se_portal_group *se_tpg,
648 const char *buf,
649 u32 *out_tid_len,
650 char **port_nexus_ptr)
652 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
653 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
655 switch (tl_hba->tl_proto_id) {
656 case SCSI_PROTOCOL_SAS:
657 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
658 port_nexus_ptr);
659 case SCSI_PROTOCOL_FCP:
660 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
661 port_nexus_ptr);
662 case SCSI_PROTOCOL_ISCSI:
663 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
664 port_nexus_ptr);
665 default:
666 pr_err("Unknown tl_proto_id: 0x%02x, using"
667 " SAS emulation\n", tl_hba->tl_proto_id);
668 break;
671 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
672 port_nexus_ptr);
676 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
677 * based upon the incoming fabric dependent SCSI Initiator Port
679 static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
681 return 1;
684 static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
686 return 0;
690 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
691 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
693 static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
695 return 0;
699 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
700 * never be called for TCM_Loop by target_core_fabric_configfs.c code.
701 * It has been added here as a nop for target_fabric_tf_ops_check()
703 static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
705 return 0;
708 static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
709 struct se_portal_group *se_tpg)
711 struct tcm_loop_nacl *tl_nacl;
713 tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
714 if (!tl_nacl) {
715 pr_err("Unable to allocate struct tcm_loop_nacl\n");
716 return NULL;
719 return &tl_nacl->se_node_acl;
722 static void tcm_loop_tpg_release_fabric_acl(
723 struct se_portal_group *se_tpg,
724 struct se_node_acl *se_nacl)
726 struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
727 struct tcm_loop_nacl, se_node_acl);
729 kfree(tl_nacl);
732 static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
734 return 1;
737 static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
739 return 1;
742 static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
744 return;
747 static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
749 return 1;
752 static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
754 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
755 struct tcm_loop_cmd, tl_se_cmd);
757 return tl_cmd->sc_cmd_state;
760 static int tcm_loop_shutdown_session(struct se_session *se_sess)
762 return 0;
765 static void tcm_loop_close_session(struct se_session *se_sess)
767 return;
770 static int tcm_loop_write_pending(struct se_cmd *se_cmd)
773 * Since Linux/SCSI has already sent down a struct scsi_cmnd
774 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
775 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
776 * format with transport_generic_map_mem_to_cmd().
778 * We now tell TCM to add this WRITE CDB directly into the TCM storage
779 * object execution queue.
781 target_execute_cmd(se_cmd);
782 return 0;
785 static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
787 return 0;
790 static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
792 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
793 struct tcm_loop_cmd, tl_se_cmd);
794 struct scsi_cmnd *sc = tl_cmd->sc;
796 pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
797 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
799 sc->result = SAM_STAT_GOOD;
800 set_host_byte(sc, DID_OK);
801 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
802 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
803 scsi_set_resid(sc, se_cmd->residual_count);
804 sc->scsi_done(sc);
805 return 0;
808 static int tcm_loop_queue_status(struct se_cmd *se_cmd)
810 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
811 struct tcm_loop_cmd, tl_se_cmd);
812 struct scsi_cmnd *sc = tl_cmd->sc;
814 pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
815 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
817 if (se_cmd->sense_buffer &&
818 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
819 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
821 memcpy(sc->sense_buffer, se_cmd->sense_buffer,
822 SCSI_SENSE_BUFFERSIZE);
823 sc->result = SAM_STAT_CHECK_CONDITION;
824 set_driver_byte(sc, DRIVER_SENSE);
825 } else
826 sc->result = se_cmd->scsi_status;
828 set_host_byte(sc, DID_OK);
829 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
830 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
831 scsi_set_resid(sc, se_cmd->residual_count);
832 sc->scsi_done(sc);
833 return 0;
836 static int tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
838 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
839 struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
841 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
842 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
844 atomic_set(&tl_tmr->tmr_complete, 1);
845 wake_up(&tl_tmr->tl_tmr_wait);
846 return 0;
849 static u16 tcm_loop_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
851 return 0;
854 static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
856 switch (tl_hba->tl_proto_id) {
857 case SCSI_PROTOCOL_SAS:
858 return "SAS";
859 case SCSI_PROTOCOL_FCP:
860 return "FCP";
861 case SCSI_PROTOCOL_ISCSI:
862 return "iSCSI";
863 default:
864 break;
867 return "Unknown";
870 /* Start items for tcm_loop_port_cit */
872 static int tcm_loop_port_link(
873 struct se_portal_group *se_tpg,
874 struct se_lun *lun)
876 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
877 struct tcm_loop_tpg, tl_se_tpg);
878 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
880 atomic_inc(&tl_tpg->tl_tpg_port_count);
881 smp_mb__after_atomic_inc();
883 * Add Linux/SCSI struct scsi_device by HCTL
885 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
887 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
888 return 0;
891 static void tcm_loop_port_unlink(
892 struct se_portal_group *se_tpg,
893 struct se_lun *se_lun)
895 struct scsi_device *sd;
896 struct tcm_loop_hba *tl_hba;
897 struct tcm_loop_tpg *tl_tpg;
899 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
900 tl_hba = tl_tpg->tl_hba;
902 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
903 se_lun->unpacked_lun);
904 if (!sd) {
905 pr_err("Unable to locate struct scsi_device for %d:%d:"
906 "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
907 return;
910 * Remove Linux/SCSI struct scsi_device by HCTL
912 scsi_remove_device(sd);
913 scsi_device_put(sd);
915 atomic_dec(&tl_tpg->tl_tpg_port_count);
916 smp_mb__after_atomic_dec();
918 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
921 /* End items for tcm_loop_port_cit */
923 /* Start items for tcm_loop_nexus_cit */
925 static int tcm_loop_make_nexus(
926 struct tcm_loop_tpg *tl_tpg,
927 const char *name)
929 struct se_portal_group *se_tpg;
930 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
931 struct tcm_loop_nexus *tl_nexus;
932 int ret = -ENOMEM;
934 if (tl_tpg->tl_hba->tl_nexus) {
935 pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n");
936 return -EEXIST;
938 se_tpg = &tl_tpg->tl_se_tpg;
940 tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
941 if (!tl_nexus) {
942 pr_err("Unable to allocate struct tcm_loop_nexus\n");
943 return -ENOMEM;
946 * Initialize the struct se_session pointer
948 tl_nexus->se_sess = transport_init_session();
949 if (IS_ERR(tl_nexus->se_sess)) {
950 ret = PTR_ERR(tl_nexus->se_sess);
951 goto out;
954 * Since we are running in 'demo mode' this call with generate a
955 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
956 * Initiator port name of the passed configfs group 'name'.
958 tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
959 se_tpg, (unsigned char *)name);
960 if (!tl_nexus->se_sess->se_node_acl) {
961 transport_free_session(tl_nexus->se_sess);
962 goto out;
965 * Now, register the SAS I_T Nexus as active with the call to
966 * transport_register_session()
968 __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
969 tl_nexus->se_sess, tl_nexus);
970 tl_tpg->tl_hba->tl_nexus = tl_nexus;
971 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
972 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
973 name);
974 return 0;
976 out:
977 kfree(tl_nexus);
978 return ret;
981 static int tcm_loop_drop_nexus(
982 struct tcm_loop_tpg *tpg)
984 struct se_session *se_sess;
985 struct tcm_loop_nexus *tl_nexus;
986 struct tcm_loop_hba *tl_hba = tpg->tl_hba;
988 tl_nexus = tpg->tl_hba->tl_nexus;
989 if (!tl_nexus)
990 return -ENODEV;
992 se_sess = tl_nexus->se_sess;
993 if (!se_sess)
994 return -ENODEV;
996 if (atomic_read(&tpg->tl_tpg_port_count)) {
997 pr_err("Unable to remove TCM_Loop I_T Nexus with"
998 " active TPG port count: %d\n",
999 atomic_read(&tpg->tl_tpg_port_count));
1000 return -EPERM;
1003 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
1004 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
1005 tl_nexus->se_sess->se_node_acl->initiatorname);
1007 * Release the SCSI I_T Nexus to the emulated SAS Target Port
1009 transport_deregister_session(tl_nexus->se_sess);
1010 tpg->tl_hba->tl_nexus = NULL;
1011 kfree(tl_nexus);
1012 return 0;
1015 /* End items for tcm_loop_nexus_cit */
1017 static ssize_t tcm_loop_tpg_show_nexus(
1018 struct se_portal_group *se_tpg,
1019 char *page)
1021 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1022 struct tcm_loop_tpg, tl_se_tpg);
1023 struct tcm_loop_nexus *tl_nexus;
1024 ssize_t ret;
1026 tl_nexus = tl_tpg->tl_hba->tl_nexus;
1027 if (!tl_nexus)
1028 return -ENODEV;
1030 ret = snprintf(page, PAGE_SIZE, "%s\n",
1031 tl_nexus->se_sess->se_node_acl->initiatorname);
1033 return ret;
1036 static ssize_t tcm_loop_tpg_store_nexus(
1037 struct se_portal_group *se_tpg,
1038 const char *page,
1039 size_t count)
1041 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1042 struct tcm_loop_tpg, tl_se_tpg);
1043 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
1044 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
1045 int ret;
1047 * Shutdown the active I_T nexus if 'NULL' is passed..
1049 if (!strncmp(page, "NULL", 4)) {
1050 ret = tcm_loop_drop_nexus(tl_tpg);
1051 return (!ret) ? count : ret;
1054 * Otherwise make sure the passed virtual Initiator port WWN matches
1055 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
1056 * tcm_loop_make_nexus()
1058 if (strlen(page) >= TL_WWN_ADDR_LEN) {
1059 pr_err("Emulated NAA Sas Address: %s, exceeds"
1060 " max: %d\n", page, TL_WWN_ADDR_LEN);
1061 return -EINVAL;
1063 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
1065 ptr = strstr(i_port, "naa.");
1066 if (ptr) {
1067 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
1068 pr_err("Passed SAS Initiator Port %s does not"
1069 " match target port protoid: %s\n", i_port,
1070 tcm_loop_dump_proto_id(tl_hba));
1071 return -EINVAL;
1073 port_ptr = &i_port[0];
1074 goto check_newline;
1076 ptr = strstr(i_port, "fc.");
1077 if (ptr) {
1078 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
1079 pr_err("Passed FCP Initiator Port %s does not"
1080 " match target port protoid: %s\n", i_port,
1081 tcm_loop_dump_proto_id(tl_hba));
1082 return -EINVAL;
1084 port_ptr = &i_port[3]; /* Skip over "fc." */
1085 goto check_newline;
1087 ptr = strstr(i_port, "iqn.");
1088 if (ptr) {
1089 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
1090 pr_err("Passed iSCSI Initiator Port %s does not"
1091 " match target port protoid: %s\n", i_port,
1092 tcm_loop_dump_proto_id(tl_hba));
1093 return -EINVAL;
1095 port_ptr = &i_port[0];
1096 goto check_newline;
1098 pr_err("Unable to locate prefix for emulated Initiator Port:"
1099 " %s\n", i_port);
1100 return -EINVAL;
1102 * Clear any trailing newline for the NAA WWN
1104 check_newline:
1105 if (i_port[strlen(i_port)-1] == '\n')
1106 i_port[strlen(i_port)-1] = '\0';
1108 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
1109 if (ret < 0)
1110 return ret;
1112 return count;
1115 TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
1117 static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
1118 &tcm_loop_tpg_nexus.attr,
1119 NULL,
1122 /* Start items for tcm_loop_naa_cit */
1124 struct se_portal_group *tcm_loop_make_naa_tpg(
1125 struct se_wwn *wwn,
1126 struct config_group *group,
1127 const char *name)
1129 struct tcm_loop_hba *tl_hba = container_of(wwn,
1130 struct tcm_loop_hba, tl_hba_wwn);
1131 struct tcm_loop_tpg *tl_tpg;
1132 char *tpgt_str, *end_ptr;
1133 int ret;
1134 unsigned short int tpgt;
1136 tpgt_str = strstr(name, "tpgt_");
1137 if (!tpgt_str) {
1138 pr_err("Unable to locate \"tpgt_#\" directory"
1139 " group\n");
1140 return ERR_PTR(-EINVAL);
1142 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1143 tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1145 if (tpgt >= TL_TPGS_PER_HBA) {
1146 pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
1147 " %u\n", tpgt, TL_TPGS_PER_HBA);
1148 return ERR_PTR(-EINVAL);
1150 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
1151 tl_tpg->tl_hba = tl_hba;
1152 tl_tpg->tl_tpgt = tpgt;
1154 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
1156 ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
1157 wwn, &tl_tpg->tl_se_tpg, tl_tpg,
1158 TRANSPORT_TPG_TYPE_NORMAL);
1159 if (ret < 0)
1160 return ERR_PTR(-ENOMEM);
1162 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
1163 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1164 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1166 return &tl_tpg->tl_se_tpg;
1169 void tcm_loop_drop_naa_tpg(
1170 struct se_portal_group *se_tpg)
1172 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1173 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1174 struct tcm_loop_tpg, tl_se_tpg);
1175 struct tcm_loop_hba *tl_hba;
1176 unsigned short tpgt;
1178 tl_hba = tl_tpg->tl_hba;
1179 tpgt = tl_tpg->tl_tpgt;
1181 * Release the I_T Nexus for the Virtual SAS link if present
1183 tcm_loop_drop_nexus(tl_tpg);
1185 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
1187 core_tpg_deregister(se_tpg);
1189 tl_tpg->tl_hba = NULL;
1190 tl_tpg->tl_tpgt = 0;
1192 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
1193 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1194 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1197 /* End items for tcm_loop_naa_cit */
1199 /* Start items for tcm_loop_cit */
1201 struct se_wwn *tcm_loop_make_scsi_hba(
1202 struct target_fabric_configfs *tf,
1203 struct config_group *group,
1204 const char *name)
1206 struct tcm_loop_hba *tl_hba;
1207 struct Scsi_Host *sh;
1208 char *ptr;
1209 int ret, off = 0;
1211 tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
1212 if (!tl_hba) {
1213 pr_err("Unable to allocate struct tcm_loop_hba\n");
1214 return ERR_PTR(-ENOMEM);
1217 * Determine the emulated Protocol Identifier and Target Port Name
1218 * based on the incoming configfs directory name.
1220 ptr = strstr(name, "naa.");
1221 if (ptr) {
1222 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1223 goto check_len;
1225 ptr = strstr(name, "fc.");
1226 if (ptr) {
1227 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1228 off = 3; /* Skip over "fc." */
1229 goto check_len;
1231 ptr = strstr(name, "iqn.");
1232 if (!ptr) {
1233 pr_err("Unable to locate prefix for emulated Target "
1234 "Port: %s\n", name);
1235 ret = -EINVAL;
1236 goto out;
1238 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
1240 check_len:
1241 if (strlen(name) >= TL_WWN_ADDR_LEN) {
1242 pr_err("Emulated NAA %s Address: %s, exceeds"
1243 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1244 TL_WWN_ADDR_LEN);
1245 ret = -EINVAL;
1246 goto out;
1248 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1251 * Call device_register(tl_hba->dev) to register the emulated
1252 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1253 * device_register() callbacks in tcm_loop_driver_probe()
1255 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1256 if (ret)
1257 goto out;
1259 sh = tl_hba->sh;
1260 tcm_loop_hba_no_cnt++;
1261 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
1262 " %s Address: %s at Linux/SCSI Host ID: %d\n",
1263 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1265 return &tl_hba->tl_hba_wwn;
1266 out:
1267 kfree(tl_hba);
1268 return ERR_PTR(ret);
1271 void tcm_loop_drop_scsi_hba(
1272 struct se_wwn *wwn)
1274 struct tcm_loop_hba *tl_hba = container_of(wwn,
1275 struct tcm_loop_hba, tl_hba_wwn);
1277 pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
1278 " SAS Address: %s at Linux/SCSI Host ID: %d\n",
1279 tl_hba->tl_wwn_address, tl_hba->sh->host_no);
1281 * Call device_unregister() on the original tl_hba->dev.
1282 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1283 * release *tl_hba;
1285 device_unregister(&tl_hba->dev);
1288 /* Start items for tcm_loop_cit */
1289 static ssize_t tcm_loop_wwn_show_attr_version(
1290 struct target_fabric_configfs *tf,
1291 char *page)
1293 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1296 TF_WWN_ATTR_RO(tcm_loop, version);
1298 static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1299 &tcm_loop_wwn_version.attr,
1300 NULL,
1303 /* End items for tcm_loop_cit */
1305 static int tcm_loop_register_configfs(void)
1307 struct target_fabric_configfs *fabric;
1308 int ret;
1310 * Set the TCM Loop HBA counter to zero
1312 tcm_loop_hba_no_cnt = 0;
1314 * Register the top level struct config_item_type with TCM core
1316 fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
1317 if (IS_ERR(fabric)) {
1318 pr_err("tcm_loop_register_configfs() failed!\n");
1319 return PTR_ERR(fabric);
1322 * Setup the fabric API of function pointers used by target_core_mod
1324 fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
1325 fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
1326 fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
1327 fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
1328 fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
1329 fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
1330 fabric->tf_ops.tpg_get_pr_transport_id_len =
1331 &tcm_loop_get_pr_transport_id_len;
1332 fabric->tf_ops.tpg_parse_pr_out_transport_id =
1333 &tcm_loop_parse_pr_out_transport_id;
1334 fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
1335 fabric->tf_ops.tpg_check_demo_mode_cache =
1336 &tcm_loop_check_demo_mode_cache;
1337 fabric->tf_ops.tpg_check_demo_mode_write_protect =
1338 &tcm_loop_check_demo_mode_write_protect;
1339 fabric->tf_ops.tpg_check_prod_mode_write_protect =
1340 &tcm_loop_check_prod_mode_write_protect;
1342 * The TCM loopback fabric module runs in demo-mode to a local
1343 * virtual SCSI device, so fabric dependent initator ACLs are
1344 * not required.
1346 fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
1347 fabric->tf_ops.tpg_release_fabric_acl =
1348 &tcm_loop_tpg_release_fabric_acl;
1349 fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
1351 * Used for setting up remaining TCM resources in process context
1353 fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
1354 fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
1355 fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
1356 fabric->tf_ops.close_session = &tcm_loop_close_session;
1357 fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
1358 fabric->tf_ops.sess_get_initiator_sid = NULL;
1359 fabric->tf_ops.write_pending = &tcm_loop_write_pending;
1360 fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
1362 * Not used for TCM loopback
1364 fabric->tf_ops.set_default_node_attributes =
1365 &tcm_loop_set_default_node_attributes;
1366 fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
1367 fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
1368 fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
1369 fabric->tf_ops.queue_status = &tcm_loop_queue_status;
1370 fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
1371 fabric->tf_ops.set_fabric_sense_len = &tcm_loop_set_fabric_sense_len;
1374 * Setup function pointers for generic logic in target_core_fabric_configfs.c
1376 fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
1377 fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
1378 fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
1379 fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
1381 * fabric_post_link() and fabric_pre_unlink() are used for
1382 * registration and release of TCM Loop Virtual SCSI LUNs.
1384 fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
1385 fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
1386 fabric->tf_ops.fabric_make_np = NULL;
1387 fabric->tf_ops.fabric_drop_np = NULL;
1389 * Setup default attribute lists for various fabric->tf_cit_tmpl
1391 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
1392 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
1393 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1394 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1395 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1397 * Once fabric->tf_ops has been setup, now register the fabric for
1398 * use within TCM
1400 ret = target_fabric_configfs_register(fabric);
1401 if (ret < 0) {
1402 pr_err("target_fabric_configfs_register() for"
1403 " TCM_Loop failed!\n");
1404 target_fabric_configfs_free(fabric);
1405 return -1;
1408 * Setup our local pointer to *fabric.
1410 tcm_loop_fabric_configfs = fabric;
1411 pr_debug("TCM_LOOP[0] - Set fabric ->"
1412 " tcm_loop_fabric_configfs\n");
1413 return 0;
1416 static void tcm_loop_deregister_configfs(void)
1418 if (!tcm_loop_fabric_configfs)
1419 return;
1421 target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
1422 tcm_loop_fabric_configfs = NULL;
1423 pr_debug("TCM_LOOP[0] - Cleared"
1424 " tcm_loop_fabric_configfs\n");
1427 static int __init tcm_loop_fabric_init(void)
1429 int ret = -ENOMEM;
1431 tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
1432 if (!tcm_loop_workqueue)
1433 goto out;
1435 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1436 sizeof(struct tcm_loop_cmd),
1437 __alignof__(struct tcm_loop_cmd),
1438 0, NULL);
1439 if (!tcm_loop_cmd_cache) {
1440 pr_debug("kmem_cache_create() for"
1441 " tcm_loop_cmd_cache failed\n");
1442 goto out_destroy_workqueue;
1445 ret = tcm_loop_alloc_core_bus();
1446 if (ret)
1447 goto out_destroy_cache;
1449 ret = tcm_loop_register_configfs();
1450 if (ret)
1451 goto out_release_core_bus;
1453 return 0;
1455 out_release_core_bus:
1456 tcm_loop_release_core_bus();
1457 out_destroy_cache:
1458 kmem_cache_destroy(tcm_loop_cmd_cache);
1459 out_destroy_workqueue:
1460 destroy_workqueue(tcm_loop_workqueue);
1461 out:
1462 return ret;
1465 static void __exit tcm_loop_fabric_exit(void)
1467 tcm_loop_deregister_configfs();
1468 tcm_loop_release_core_bus();
1469 kmem_cache_destroy(tcm_loop_cmd_cache);
1470 destroy_workqueue(tcm_loop_workqueue);
1473 MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1474 MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1475 MODULE_LICENSE("GPL");
1476 module_init(tcm_loop_fabric_init);
1477 module_exit(tcm_loop_fabric_exit);