1 /*******************************************************************************
3 * This file contains the Linux/SCSI LLD virtual SCSI initiator driver
4 * for emulated SAS initiator ports
6 * © Copyright 2011-2013 Datera, Inc.
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>
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)
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);
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_show_info(struct seq_file *m, struct Scsi_Host *host)
84 seq_printf(m, "tcm_loop_proc_info()\n");
88 static int tcm_loop_driver_probe(struct device *);
89 static int tcm_loop_driver_remove(struct device *);
91 static int pseudo_lld_bus_match(struct device *dev,
92 struct device_driver *dev_driver)
97 static struct bus_type tcm_loop_lld_bus = {
98 .name = "tcm_loop_bus",
99 .match = pseudo_lld_bus_match,
100 .probe = tcm_loop_driver_probe,
101 .remove = tcm_loop_driver_remove,
104 static struct device_driver tcm_loop_driverfs = {
106 .bus = &tcm_loop_lld_bus,
109 * Used with root_device_register() in tcm_loop_alloc_core_bus() below
111 struct device *tcm_loop_primary;
114 * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and
115 * drivers/scsi/libiscsi.c:iscsi_change_queue_depth()
117 static int tcm_loop_change_queue_depth(
118 struct scsi_device *sdev,
123 case SCSI_QDEPTH_DEFAULT:
124 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
126 case SCSI_QDEPTH_QFULL:
127 scsi_track_queue_full(sdev, depth);
129 case SCSI_QDEPTH_RAMP_UP:
130 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
135 return sdev->queue_depth;
138 static int tcm_loop_change_queue_type(struct scsi_device *sdev, int tag)
140 if (sdev->tagged_supported) {
141 scsi_set_tag_type(sdev, tag);
144 scsi_activate_tcq(sdev, sdev->queue_depth);
146 scsi_deactivate_tcq(sdev, sdev->queue_depth);
154 * Locate the SAM Task Attr from struct scsi_cmnd *
156 static int tcm_loop_sam_attr(struct scsi_cmnd *sc)
158 if (sc->device->tagged_supported) {
160 case HEAD_OF_QUEUE_TAG:
162 case ORDERED_QUEUE_TAG:
163 return MSG_ORDERED_TAG;
169 return MSG_SIMPLE_TAG;
172 static void tcm_loop_submission_work(struct work_struct *work)
174 struct tcm_loop_cmd *tl_cmd =
175 container_of(work, struct tcm_loop_cmd, work);
176 struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
177 struct scsi_cmnd *sc = tl_cmd->sc;
178 struct tcm_loop_nexus *tl_nexus;
179 struct tcm_loop_hba *tl_hba;
180 struct tcm_loop_tpg *tl_tpg;
181 struct scatterlist *sgl_bidi = NULL;
182 u32 sgl_bidi_count = 0;
185 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
186 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
189 * Ensure that this tl_tpg reference from the incoming sc->device->id
190 * has already been configured via tcm_loop_make_naa_tpg().
192 if (!tl_tpg->tl_hba) {
193 set_host_byte(sc, DID_NO_CONNECT);
196 if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) {
197 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
200 tl_nexus = tl_hba->tl_nexus;
202 scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
203 " does not exist\n");
204 set_host_byte(sc, DID_ERROR);
207 if (scsi_bidi_cmnd(sc)) {
208 struct scsi_data_buffer *sdb = scsi_in(sc);
210 sgl_bidi = sdb->table.sgl;
211 sgl_bidi_count = sdb->table.nents;
212 se_cmd->se_cmd_flags |= SCF_BIDI;
215 rc = target_submit_cmd_map_sgls(se_cmd, tl_nexus->se_sess, sc->cmnd,
216 &tl_cmd->tl_sense_buf[0], tl_cmd->sc->device->lun,
217 scsi_bufflen(sc), tcm_loop_sam_attr(sc),
218 sc->sc_data_direction, 0,
219 scsi_sglist(sc), scsi_sg_count(sc),
220 sgl_bidi, sgl_bidi_count);
222 set_host_byte(sc, DID_NO_CONNECT);
233 * ->queuecommand can be and usually is called from interrupt context, so
234 * defer the actual submission to a workqueue.
236 static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
238 struct tcm_loop_cmd *tl_cmd;
240 pr_debug("tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x"
241 " scsi_buf_len: %u\n", sc->device->host->host_no,
242 sc->device->id, sc->device->channel, sc->device->lun,
243 sc->cmnd[0], scsi_bufflen(sc));
245 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
247 pr_err("Unable to allocate struct tcm_loop_cmd\n");
248 set_host_byte(sc, DID_ERROR);
254 tl_cmd->sc_cmd_tag = sc->tag;
255 INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
256 queue_work(tcm_loop_workqueue, &tl_cmd->work);
261 * Called from SCSI EH process context to issue a LUN_RESET TMR
262 * to struct scsi_device
264 static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg,
265 struct tcm_loop_nexus *tl_nexus,
266 int lun, int task, enum tcm_tmreq_table tmr)
268 struct se_cmd *se_cmd = NULL;
269 struct se_session *se_sess;
270 struct se_portal_group *se_tpg;
271 struct tcm_loop_cmd *tl_cmd = NULL;
272 struct tcm_loop_tmr *tl_tmr = NULL;
273 int ret = TMR_FUNCTION_FAILED, rc;
275 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
277 pr_err("Unable to allocate memory for tl_cmd\n");
281 tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
283 pr_err("Unable to allocate memory for tl_tmr\n");
286 init_waitqueue_head(&tl_tmr->tl_tmr_wait);
288 se_cmd = &tl_cmd->tl_se_cmd;
289 se_tpg = &tl_tpg->tl_se_tpg;
290 se_sess = tl_nexus->se_sess;
292 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
294 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
295 DMA_NONE, MSG_SIMPLE_TAG,
296 &tl_cmd->tl_sense_buf[0]);
298 rc = core_tmr_alloc_req(se_cmd, tl_tmr, tmr, GFP_KERNEL);
302 if (tmr == TMR_ABORT_TASK)
303 se_cmd->se_tmr_req->ref_task_tag = task;
306 * Locate the underlying TCM struct se_lun
308 if (transport_lookup_tmr_lun(se_cmd, lun) < 0) {
309 ret = TMR_LUN_DOES_NOT_EXIST;
313 * Queue the TMR to TCM Core and sleep waiting for
314 * tcm_loop_queue_tm_rsp() to wake us up.
316 transport_generic_handle_tmr(se_cmd);
317 wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
319 * The TMR LUN_RESET has completed, check the response status and
320 * then release allocations.
322 ret = se_cmd->se_tmr_req->response;
325 transport_generic_free_cmd(se_cmd, 1);
327 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
332 static int tcm_loop_abort_task(struct scsi_cmnd *sc)
334 struct tcm_loop_hba *tl_hba;
335 struct tcm_loop_nexus *tl_nexus;
336 struct tcm_loop_tpg *tl_tpg;
340 * Locate the tcm_loop_hba_t pointer
342 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
344 * Locate the tl_nexus and se_sess pointers
346 tl_nexus = tl_hba->tl_nexus;
348 pr_err("Unable to perform device reset without"
349 " active I_T Nexus\n");
354 * Locate the tl_tpg pointer from TargetID in sc->device->id
356 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
357 ret = tcm_loop_issue_tmr(tl_tpg, tl_nexus, sc->device->lun,
358 sc->tag, TMR_ABORT_TASK);
359 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
363 * Called from SCSI EH process context to issue a LUN_RESET TMR
364 * to struct scsi_device
366 static int tcm_loop_device_reset(struct scsi_cmnd *sc)
368 struct tcm_loop_hba *tl_hba;
369 struct tcm_loop_nexus *tl_nexus;
370 struct tcm_loop_tpg *tl_tpg;
374 * Locate the tcm_loop_hba_t pointer
376 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
378 * Locate the tl_nexus and se_sess pointers
380 tl_nexus = tl_hba->tl_nexus;
382 pr_err("Unable to perform device reset without"
383 " active I_T Nexus\n");
387 * Locate the tl_tpg pointer from TargetID in sc->device->id
389 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
390 ret = tcm_loop_issue_tmr(tl_tpg, tl_nexus, sc->device->lun,
392 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
395 static int tcm_loop_slave_alloc(struct scsi_device *sd)
397 set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
401 static int tcm_loop_slave_configure(struct scsi_device *sd)
403 if (sd->tagged_supported) {
404 scsi_activate_tcq(sd, sd->queue_depth);
405 scsi_adjust_queue_depth(sd, MSG_SIMPLE_TAG,
406 sd->host->cmd_per_lun);
408 scsi_adjust_queue_depth(sd, 0,
409 sd->host->cmd_per_lun);
415 static struct scsi_host_template tcm_loop_driver_template = {
416 .show_info = tcm_loop_show_info,
417 .proc_name = "tcm_loopback",
418 .name = "TCM_Loopback",
419 .queuecommand = tcm_loop_queuecommand,
420 .change_queue_depth = tcm_loop_change_queue_depth,
421 .change_queue_type = tcm_loop_change_queue_type,
422 .eh_abort_handler = tcm_loop_abort_task,
423 .eh_device_reset_handler = tcm_loop_device_reset,
428 .max_sectors = 0xFFFF,
429 .use_clustering = DISABLE_CLUSTERING,
430 .slave_alloc = tcm_loop_slave_alloc,
431 .slave_configure = tcm_loop_slave_configure,
432 .module = THIS_MODULE,
435 static int tcm_loop_driver_probe(struct device *dev)
437 struct tcm_loop_hba *tl_hba;
438 struct Scsi_Host *sh;
441 tl_hba = to_tcm_loop_hba(dev);
443 sh = scsi_host_alloc(&tcm_loop_driver_template,
444 sizeof(struct tcm_loop_hba));
446 pr_err("Unable to allocate struct scsi_host\n");
452 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
454 *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
456 * Setup single ID, Channel and LUN for now..
461 sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN;
463 error = scsi_add_host(sh, &tl_hba->dev);
465 pr_err("%s: scsi_add_host failed\n", __func__);
472 static int tcm_loop_driver_remove(struct device *dev)
474 struct tcm_loop_hba *tl_hba;
475 struct Scsi_Host *sh;
477 tl_hba = to_tcm_loop_hba(dev);
480 scsi_remove_host(sh);
485 static void tcm_loop_release_adapter(struct device *dev)
487 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
493 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
495 static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
499 tl_hba->dev.bus = &tcm_loop_lld_bus;
500 tl_hba->dev.parent = tcm_loop_primary;
501 tl_hba->dev.release = &tcm_loop_release_adapter;
502 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
504 ret = device_register(&tl_hba->dev);
506 pr_err("device_register() failed for"
507 " tl_hba->dev: %d\n", ret);
515 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
518 static int tcm_loop_alloc_core_bus(void)
522 tcm_loop_primary = root_device_register("tcm_loop_0");
523 if (IS_ERR(tcm_loop_primary)) {
524 pr_err("Unable to allocate tcm_loop_primary\n");
525 return PTR_ERR(tcm_loop_primary);
528 ret = bus_register(&tcm_loop_lld_bus);
530 pr_err("bus_register() failed for tcm_loop_lld_bus\n");
534 ret = driver_register(&tcm_loop_driverfs);
536 pr_err("driver_register() failed for"
537 "tcm_loop_driverfs\n");
541 pr_debug("Initialized TCM Loop Core Bus\n");
545 bus_unregister(&tcm_loop_lld_bus);
547 root_device_unregister(tcm_loop_primary);
551 static void tcm_loop_release_core_bus(void)
553 driver_unregister(&tcm_loop_driverfs);
554 bus_unregister(&tcm_loop_lld_bus);
555 root_device_unregister(tcm_loop_primary);
557 pr_debug("Releasing TCM Loop Core BUS\n");
560 static char *tcm_loop_get_fabric_name(void)
565 static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
567 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
568 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
570 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
571 * time based on the protocol dependent prefix of the passed configfs group.
573 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
574 * ProtocolID using target_core_fabric_lib.c symbols.
576 switch (tl_hba->tl_proto_id) {
577 case SCSI_PROTOCOL_SAS:
578 return sas_get_fabric_proto_ident(se_tpg);
579 case SCSI_PROTOCOL_FCP:
580 return fc_get_fabric_proto_ident(se_tpg);
581 case SCSI_PROTOCOL_ISCSI:
582 return iscsi_get_fabric_proto_ident(se_tpg);
584 pr_err("Unknown tl_proto_id: 0x%02x, using"
585 " SAS emulation\n", tl_hba->tl_proto_id);
589 return sas_get_fabric_proto_ident(se_tpg);
592 static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
594 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
596 * Return the passed NAA identifier for the SAS Target Port
598 return &tl_tpg->tl_hba->tl_wwn_address[0];
601 static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
603 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
605 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
606 * to represent the SCSI Target Port.
608 return tl_tpg->tl_tpgt;
611 static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
616 static u32 tcm_loop_get_pr_transport_id(
617 struct se_portal_group *se_tpg,
618 struct se_node_acl *se_nacl,
619 struct t10_pr_registration *pr_reg,
623 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
624 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
626 switch (tl_hba->tl_proto_id) {
627 case SCSI_PROTOCOL_SAS:
628 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
630 case SCSI_PROTOCOL_FCP:
631 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
633 case SCSI_PROTOCOL_ISCSI:
634 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
637 pr_err("Unknown tl_proto_id: 0x%02x, using"
638 " SAS emulation\n", tl_hba->tl_proto_id);
642 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
646 static u32 tcm_loop_get_pr_transport_id_len(
647 struct se_portal_group *se_tpg,
648 struct se_node_acl *se_nacl,
649 struct t10_pr_registration *pr_reg,
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_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
659 case SCSI_PROTOCOL_FCP:
660 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
662 case SCSI_PROTOCOL_ISCSI:
663 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
666 pr_err("Unknown tl_proto_id: 0x%02x, using"
667 " SAS emulation\n", tl_hba->tl_proto_id);
671 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
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.
679 static char *tcm_loop_parse_pr_out_transport_id(
680 struct se_portal_group *se_tpg,
683 char **port_nexus_ptr)
685 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
686 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
688 switch (tl_hba->tl_proto_id) {
689 case SCSI_PROTOCOL_SAS:
690 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
692 case SCSI_PROTOCOL_FCP:
693 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
695 case SCSI_PROTOCOL_ISCSI:
696 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
699 pr_err("Unknown tl_proto_id: 0x%02x, using"
700 " SAS emulation\n", tl_hba->tl_proto_id);
704 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
709 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
710 * based upon the incoming fabric dependent SCSI Initiator Port
712 static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
717 static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
723 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
724 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
726 static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
732 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
733 * never be called for TCM_Loop by target_core_fabric_configfs.c code.
734 * It has been added here as a nop for target_fabric_tf_ops_check()
736 static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
741 static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
742 struct se_portal_group *se_tpg)
744 struct tcm_loop_nacl *tl_nacl;
746 tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
748 pr_err("Unable to allocate struct tcm_loop_nacl\n");
752 return &tl_nacl->se_node_acl;
755 static void tcm_loop_tpg_release_fabric_acl(
756 struct se_portal_group *se_tpg,
757 struct se_node_acl *se_nacl)
759 struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
760 struct tcm_loop_nacl, se_node_acl);
765 static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
770 static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
775 static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
780 static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
782 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
783 struct tcm_loop_cmd, tl_se_cmd);
785 return tl_cmd->sc_cmd_tag;
788 static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
790 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
791 struct tcm_loop_cmd, tl_se_cmd);
793 return tl_cmd->sc_cmd_state;
796 static int tcm_loop_shutdown_session(struct se_session *se_sess)
801 static void tcm_loop_close_session(struct se_session *se_sess)
806 static int tcm_loop_write_pending(struct se_cmd *se_cmd)
809 * Since Linux/SCSI has already sent down a struct scsi_cmnd
810 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
811 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
812 * format with transport_generic_map_mem_to_cmd().
814 * We now tell TCM to add this WRITE CDB directly into the TCM storage
815 * object execution queue.
817 target_execute_cmd(se_cmd);
821 static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
826 static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
828 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
829 struct tcm_loop_cmd, tl_se_cmd);
830 struct scsi_cmnd *sc = tl_cmd->sc;
832 pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
833 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
835 sc->result = SAM_STAT_GOOD;
836 set_host_byte(sc, DID_OK);
837 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
838 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
839 scsi_set_resid(sc, se_cmd->residual_count);
844 static int tcm_loop_queue_status(struct se_cmd *se_cmd)
846 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
847 struct tcm_loop_cmd, tl_se_cmd);
848 struct scsi_cmnd *sc = tl_cmd->sc;
850 pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
851 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
853 if (se_cmd->sense_buffer &&
854 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
855 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
857 memcpy(sc->sense_buffer, se_cmd->sense_buffer,
858 SCSI_SENSE_BUFFERSIZE);
859 sc->result = SAM_STAT_CHECK_CONDITION;
860 set_driver_byte(sc, DRIVER_SENSE);
862 sc->result = se_cmd->scsi_status;
864 set_host_byte(sc, DID_OK);
865 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
866 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
867 scsi_set_resid(sc, se_cmd->residual_count);
872 static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
874 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
875 struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
877 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
878 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
880 atomic_set(&tl_tmr->tmr_complete, 1);
881 wake_up(&tl_tmr->tl_tmr_wait);
884 static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
886 switch (tl_hba->tl_proto_id) {
887 case SCSI_PROTOCOL_SAS:
889 case SCSI_PROTOCOL_FCP:
891 case SCSI_PROTOCOL_ISCSI:
900 /* Start items for tcm_loop_port_cit */
902 static int tcm_loop_port_link(
903 struct se_portal_group *se_tpg,
906 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
907 struct tcm_loop_tpg, tl_se_tpg);
908 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
910 atomic_inc(&tl_tpg->tl_tpg_port_count);
911 smp_mb__after_atomic_inc();
913 * Add Linux/SCSI struct scsi_device by HCTL
915 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
917 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
921 static void tcm_loop_port_unlink(
922 struct se_portal_group *se_tpg,
923 struct se_lun *se_lun)
925 struct scsi_device *sd;
926 struct tcm_loop_hba *tl_hba;
927 struct tcm_loop_tpg *tl_tpg;
929 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
930 tl_hba = tl_tpg->tl_hba;
932 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
933 se_lun->unpacked_lun);
935 pr_err("Unable to locate struct scsi_device for %d:%d:"
936 "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
940 * Remove Linux/SCSI struct scsi_device by HCTL
942 scsi_remove_device(sd);
945 atomic_dec(&tl_tpg->tl_tpg_port_count);
946 smp_mb__after_atomic_dec();
948 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
951 /* End items for tcm_loop_port_cit */
953 /* Start items for tcm_loop_nexus_cit */
955 static int tcm_loop_make_nexus(
956 struct tcm_loop_tpg *tl_tpg,
959 struct se_portal_group *se_tpg;
960 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
961 struct tcm_loop_nexus *tl_nexus;
964 if (tl_tpg->tl_hba->tl_nexus) {
965 pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n");
968 se_tpg = &tl_tpg->tl_se_tpg;
970 tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
972 pr_err("Unable to allocate struct tcm_loop_nexus\n");
976 * Initialize the struct se_session pointer
978 tl_nexus->se_sess = transport_init_session();
979 if (IS_ERR(tl_nexus->se_sess)) {
980 ret = PTR_ERR(tl_nexus->se_sess);
984 * Since we are running in 'demo mode' this call with generate a
985 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
986 * Initiator port name of the passed configfs group 'name'.
988 tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
989 se_tpg, (unsigned char *)name);
990 if (!tl_nexus->se_sess->se_node_acl) {
991 transport_free_session(tl_nexus->se_sess);
995 * Now, register the SAS I_T Nexus as active with the call to
996 * transport_register_session()
998 __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
999 tl_nexus->se_sess, tl_nexus);
1000 tl_tpg->tl_hba->tl_nexus = tl_nexus;
1001 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
1002 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
1011 static int tcm_loop_drop_nexus(
1012 struct tcm_loop_tpg *tpg)
1014 struct se_session *se_sess;
1015 struct tcm_loop_nexus *tl_nexus;
1016 struct tcm_loop_hba *tl_hba = tpg->tl_hba;
1021 tl_nexus = tl_hba->tl_nexus;
1025 se_sess = tl_nexus->se_sess;
1029 if (atomic_read(&tpg->tl_tpg_port_count)) {
1030 pr_err("Unable to remove TCM_Loop I_T Nexus with"
1031 " active TPG port count: %d\n",
1032 atomic_read(&tpg->tl_tpg_port_count));
1036 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
1037 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
1038 tl_nexus->se_sess->se_node_acl->initiatorname);
1040 * Release the SCSI I_T Nexus to the emulated SAS Target Port
1042 transport_deregister_session(tl_nexus->se_sess);
1043 tpg->tl_hba->tl_nexus = NULL;
1048 /* End items for tcm_loop_nexus_cit */
1050 static ssize_t tcm_loop_tpg_show_nexus(
1051 struct se_portal_group *se_tpg,
1054 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1055 struct tcm_loop_tpg, tl_se_tpg);
1056 struct tcm_loop_nexus *tl_nexus;
1059 tl_nexus = tl_tpg->tl_hba->tl_nexus;
1063 ret = snprintf(page, PAGE_SIZE, "%s\n",
1064 tl_nexus->se_sess->se_node_acl->initiatorname);
1069 static ssize_t tcm_loop_tpg_store_nexus(
1070 struct se_portal_group *se_tpg,
1074 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1075 struct tcm_loop_tpg, tl_se_tpg);
1076 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
1077 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
1080 * Shutdown the active I_T nexus if 'NULL' is passed..
1082 if (!strncmp(page, "NULL", 4)) {
1083 ret = tcm_loop_drop_nexus(tl_tpg);
1084 return (!ret) ? count : ret;
1087 * Otherwise make sure the passed virtual Initiator port WWN matches
1088 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
1089 * tcm_loop_make_nexus()
1091 if (strlen(page) >= TL_WWN_ADDR_LEN) {
1092 pr_err("Emulated NAA Sas Address: %s, exceeds"
1093 " max: %d\n", page, TL_WWN_ADDR_LEN);
1096 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
1098 ptr = strstr(i_port, "naa.");
1100 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
1101 pr_err("Passed SAS Initiator Port %s does not"
1102 " match target port protoid: %s\n", i_port,
1103 tcm_loop_dump_proto_id(tl_hba));
1106 port_ptr = &i_port[0];
1109 ptr = strstr(i_port, "fc.");
1111 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
1112 pr_err("Passed FCP Initiator Port %s does not"
1113 " match target port protoid: %s\n", i_port,
1114 tcm_loop_dump_proto_id(tl_hba));
1117 port_ptr = &i_port[3]; /* Skip over "fc." */
1120 ptr = strstr(i_port, "iqn.");
1122 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
1123 pr_err("Passed iSCSI Initiator Port %s does not"
1124 " match target port protoid: %s\n", i_port,
1125 tcm_loop_dump_proto_id(tl_hba));
1128 port_ptr = &i_port[0];
1131 pr_err("Unable to locate prefix for emulated Initiator Port:"
1135 * Clear any trailing newline for the NAA WWN
1138 if (i_port[strlen(i_port)-1] == '\n')
1139 i_port[strlen(i_port)-1] = '\0';
1141 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
1148 TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
1150 static ssize_t tcm_loop_tpg_show_transport_status(
1151 struct se_portal_group *se_tpg,
1154 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1155 struct tcm_loop_tpg, tl_se_tpg);
1156 const char *status = NULL;
1157 ssize_t ret = -EINVAL;
1159 switch (tl_tpg->tl_transport_status) {
1160 case TCM_TRANSPORT_ONLINE:
1163 case TCM_TRANSPORT_OFFLINE:
1171 ret = snprintf(page, PAGE_SIZE, "%s\n", status);
1176 static ssize_t tcm_loop_tpg_store_transport_status(
1177 struct se_portal_group *se_tpg,
1181 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1182 struct tcm_loop_tpg, tl_se_tpg);
1184 if (!strncmp(page, "online", 6)) {
1185 tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
1188 if (!strncmp(page, "offline", 7)) {
1189 tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
1195 TF_TPG_BASE_ATTR(tcm_loop, transport_status, S_IRUGO | S_IWUSR);
1197 static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
1198 &tcm_loop_tpg_nexus.attr,
1199 &tcm_loop_tpg_transport_status.attr,
1203 /* Start items for tcm_loop_naa_cit */
1205 struct se_portal_group *tcm_loop_make_naa_tpg(
1207 struct config_group *group,
1210 struct tcm_loop_hba *tl_hba = container_of(wwn,
1211 struct tcm_loop_hba, tl_hba_wwn);
1212 struct tcm_loop_tpg *tl_tpg;
1213 char *tpgt_str, *end_ptr;
1215 unsigned short int tpgt;
1217 tpgt_str = strstr(name, "tpgt_");
1219 pr_err("Unable to locate \"tpgt_#\" directory"
1221 return ERR_PTR(-EINVAL);
1223 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1224 tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1226 if (tpgt >= TL_TPGS_PER_HBA) {
1227 pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
1228 " %u\n", tpgt, TL_TPGS_PER_HBA);
1229 return ERR_PTR(-EINVAL);
1231 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
1232 tl_tpg->tl_hba = tl_hba;
1233 tl_tpg->tl_tpgt = tpgt;
1235 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
1237 ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
1238 wwn, &tl_tpg->tl_se_tpg, tl_tpg,
1239 TRANSPORT_TPG_TYPE_NORMAL);
1241 return ERR_PTR(-ENOMEM);
1243 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
1244 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1245 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1247 return &tl_tpg->tl_se_tpg;
1250 void tcm_loop_drop_naa_tpg(
1251 struct se_portal_group *se_tpg)
1253 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1254 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1255 struct tcm_loop_tpg, tl_se_tpg);
1256 struct tcm_loop_hba *tl_hba;
1257 unsigned short tpgt;
1259 tl_hba = tl_tpg->tl_hba;
1260 tpgt = tl_tpg->tl_tpgt;
1262 * Release the I_T Nexus for the Virtual SAS link if present
1264 tcm_loop_drop_nexus(tl_tpg);
1266 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
1268 core_tpg_deregister(se_tpg);
1270 tl_tpg->tl_hba = NULL;
1271 tl_tpg->tl_tpgt = 0;
1273 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
1274 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1275 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1278 /* End items for tcm_loop_naa_cit */
1280 /* Start items for tcm_loop_cit */
1282 struct se_wwn *tcm_loop_make_scsi_hba(
1283 struct target_fabric_configfs *tf,
1284 struct config_group *group,
1287 struct tcm_loop_hba *tl_hba;
1288 struct Scsi_Host *sh;
1292 tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
1294 pr_err("Unable to allocate struct tcm_loop_hba\n");
1295 return ERR_PTR(-ENOMEM);
1298 * Determine the emulated Protocol Identifier and Target Port Name
1299 * based on the incoming configfs directory name.
1301 ptr = strstr(name, "naa.");
1303 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1306 ptr = strstr(name, "fc.");
1308 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1309 off = 3; /* Skip over "fc." */
1312 ptr = strstr(name, "iqn.");
1314 pr_err("Unable to locate prefix for emulated Target "
1315 "Port: %s\n", name);
1319 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
1322 if (strlen(name) >= TL_WWN_ADDR_LEN) {
1323 pr_err("Emulated NAA %s Address: %s, exceeds"
1324 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1329 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1332 * Call device_register(tl_hba->dev) to register the emulated
1333 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1334 * device_register() callbacks in tcm_loop_driver_probe()
1336 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1341 tcm_loop_hba_no_cnt++;
1342 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
1343 " %s Address: %s at Linux/SCSI Host ID: %d\n",
1344 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1346 return &tl_hba->tl_hba_wwn;
1349 return ERR_PTR(ret);
1352 void tcm_loop_drop_scsi_hba(
1355 struct tcm_loop_hba *tl_hba = container_of(wwn,
1356 struct tcm_loop_hba, tl_hba_wwn);
1358 pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
1359 " SAS Address: %s at Linux/SCSI Host ID: %d\n",
1360 tl_hba->tl_wwn_address, tl_hba->sh->host_no);
1362 * Call device_unregister() on the original tl_hba->dev.
1363 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1366 device_unregister(&tl_hba->dev);
1369 /* Start items for tcm_loop_cit */
1370 static ssize_t tcm_loop_wwn_show_attr_version(
1371 struct target_fabric_configfs *tf,
1374 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1377 TF_WWN_ATTR_RO(tcm_loop, version);
1379 static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1380 &tcm_loop_wwn_version.attr,
1384 /* End items for tcm_loop_cit */
1386 static int tcm_loop_register_configfs(void)
1388 struct target_fabric_configfs *fabric;
1391 * Set the TCM Loop HBA counter to zero
1393 tcm_loop_hba_no_cnt = 0;
1395 * Register the top level struct config_item_type with TCM core
1397 fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
1398 if (IS_ERR(fabric)) {
1399 pr_err("tcm_loop_register_configfs() failed!\n");
1400 return PTR_ERR(fabric);
1403 * Setup the fabric API of function pointers used by target_core_mod
1405 fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
1406 fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
1407 fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
1408 fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
1409 fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
1410 fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
1411 fabric->tf_ops.tpg_get_pr_transport_id_len =
1412 &tcm_loop_get_pr_transport_id_len;
1413 fabric->tf_ops.tpg_parse_pr_out_transport_id =
1414 &tcm_loop_parse_pr_out_transport_id;
1415 fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
1416 fabric->tf_ops.tpg_check_demo_mode_cache =
1417 &tcm_loop_check_demo_mode_cache;
1418 fabric->tf_ops.tpg_check_demo_mode_write_protect =
1419 &tcm_loop_check_demo_mode_write_protect;
1420 fabric->tf_ops.tpg_check_prod_mode_write_protect =
1421 &tcm_loop_check_prod_mode_write_protect;
1423 * The TCM loopback fabric module runs in demo-mode to a local
1424 * virtual SCSI device, so fabric dependent initator ACLs are
1427 fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
1428 fabric->tf_ops.tpg_release_fabric_acl =
1429 &tcm_loop_tpg_release_fabric_acl;
1430 fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
1432 * Used for setting up remaining TCM resources in process context
1434 fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
1435 fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
1436 fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
1437 fabric->tf_ops.close_session = &tcm_loop_close_session;
1438 fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
1439 fabric->tf_ops.sess_get_initiator_sid = NULL;
1440 fabric->tf_ops.write_pending = &tcm_loop_write_pending;
1441 fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
1443 * Not used for TCM loopback
1445 fabric->tf_ops.set_default_node_attributes =
1446 &tcm_loop_set_default_node_attributes;
1447 fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
1448 fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
1449 fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
1450 fabric->tf_ops.queue_status = &tcm_loop_queue_status;
1451 fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
1454 * Setup function pointers for generic logic in target_core_fabric_configfs.c
1456 fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
1457 fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
1458 fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
1459 fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
1461 * fabric_post_link() and fabric_pre_unlink() are used for
1462 * registration and release of TCM Loop Virtual SCSI LUNs.
1464 fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
1465 fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
1466 fabric->tf_ops.fabric_make_np = NULL;
1467 fabric->tf_ops.fabric_drop_np = NULL;
1469 * Setup default attribute lists for various fabric->tf_cit_tmpl
1471 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
1472 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
1473 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
1474 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
1475 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
1477 * Once fabric->tf_ops has been setup, now register the fabric for
1480 ret = target_fabric_configfs_register(fabric);
1482 pr_err("target_fabric_configfs_register() for"
1483 " TCM_Loop failed!\n");
1484 target_fabric_configfs_free(fabric);
1488 * Setup our local pointer to *fabric.
1490 tcm_loop_fabric_configfs = fabric;
1491 pr_debug("TCM_LOOP[0] - Set fabric ->"
1492 " tcm_loop_fabric_configfs\n");
1496 static void tcm_loop_deregister_configfs(void)
1498 if (!tcm_loop_fabric_configfs)
1501 target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
1502 tcm_loop_fabric_configfs = NULL;
1503 pr_debug("TCM_LOOP[0] - Cleared"
1504 " tcm_loop_fabric_configfs\n");
1507 static int __init tcm_loop_fabric_init(void)
1511 tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
1512 if (!tcm_loop_workqueue)
1515 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1516 sizeof(struct tcm_loop_cmd),
1517 __alignof__(struct tcm_loop_cmd),
1519 if (!tcm_loop_cmd_cache) {
1520 pr_debug("kmem_cache_create() for"
1521 " tcm_loop_cmd_cache failed\n");
1522 goto out_destroy_workqueue;
1525 ret = tcm_loop_alloc_core_bus();
1527 goto out_destroy_cache;
1529 ret = tcm_loop_register_configfs();
1531 goto out_release_core_bus;
1535 out_release_core_bus:
1536 tcm_loop_release_core_bus();
1538 kmem_cache_destroy(tcm_loop_cmd_cache);
1539 out_destroy_workqueue:
1540 destroy_workqueue(tcm_loop_workqueue);
1545 static void __exit tcm_loop_fabric_exit(void)
1547 tcm_loop_deregister_configfs();
1548 tcm_loop_release_core_bus();
1549 kmem_cache_destroy(tcm_loop_cmd_cache);
1550 destroy_workqueue(tcm_loop_workqueue);
1553 MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1554 MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1555 MODULE_LICENSE("GPL");
1556 module_init(tcm_loop_fabric_init);
1557 module_exit(tcm_loop_fabric_exit);