mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / target / loopback / tcm_loop.c
blobc54dd828431fb1d277febe66820c47b59ea986da
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>
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_show_info(struct seq_file *m, struct Scsi_Host *host)
84 seq_printf(m, "tcm_loop_proc_info()\n");
85 return 0;
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)
94 return 1;
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 = {
105 .name = "tcm_loop",
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,
119 int depth,
120 int reason)
122 switch (reason) {
123 case SCSI_QDEPTH_DEFAULT:
124 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
125 break;
126 case SCSI_QDEPTH_QFULL:
127 scsi_track_queue_full(sdev, depth);
128 break;
129 case SCSI_QDEPTH_RAMP_UP:
130 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
131 break;
132 default:
133 return -EOPNOTSUPP;
135 return sdev->queue_depth;
139 * Locate the SAM Task Attr from struct scsi_cmnd *
141 static int tcm_loop_sam_attr(struct scsi_cmnd *sc)
143 if (sc->device->tagged_supported) {
144 switch (sc->tag) {
145 case HEAD_OF_QUEUE_TAG:
146 return MSG_HEAD_TAG;
147 case ORDERED_QUEUE_TAG:
148 return MSG_ORDERED_TAG;
149 default:
150 break;
154 return MSG_SIMPLE_TAG;
157 static void tcm_loop_submission_work(struct work_struct *work)
159 struct tcm_loop_cmd *tl_cmd =
160 container_of(work, struct tcm_loop_cmd, work);
161 struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
162 struct scsi_cmnd *sc = tl_cmd->sc;
163 struct tcm_loop_nexus *tl_nexus;
164 struct tcm_loop_hba *tl_hba;
165 struct tcm_loop_tpg *tl_tpg;
166 struct scatterlist *sgl_bidi = NULL;
167 u32 sgl_bidi_count = 0;
168 int rc;
170 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
171 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
174 * Ensure that this tl_tpg reference from the incoming sc->device->id
175 * has already been configured via tcm_loop_make_naa_tpg().
177 if (!tl_tpg->tl_hba) {
178 set_host_byte(sc, DID_NO_CONNECT);
179 goto out_done;
182 tl_nexus = tl_tpg->tl_nexus;
183 if (!tl_nexus) {
184 scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
185 " does not exist\n");
186 set_host_byte(sc, DID_ERROR);
187 goto out_done;
189 if (scsi_bidi_cmnd(sc)) {
190 struct scsi_data_buffer *sdb = scsi_in(sc);
192 sgl_bidi = sdb->table.sgl;
193 sgl_bidi_count = sdb->table.nents;
194 se_cmd->se_cmd_flags |= SCF_BIDI;
197 rc = target_submit_cmd_map_sgls(se_cmd, tl_nexus->se_sess, sc->cmnd,
198 &tl_cmd->tl_sense_buf[0], tl_cmd->sc->device->lun,
199 scsi_bufflen(sc), tcm_loop_sam_attr(sc),
200 sc->sc_data_direction, 0,
201 scsi_sglist(sc), scsi_sg_count(sc),
202 sgl_bidi, sgl_bidi_count);
203 if (rc < 0) {
204 set_host_byte(sc, DID_NO_CONNECT);
205 goto out_done;
207 return;
209 out_done:
210 sc->scsi_done(sc);
211 return;
215 * ->queuecommand can be and usually is called from interrupt context, so
216 * defer the actual submission to a workqueue.
218 static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
220 struct tcm_loop_cmd *tl_cmd;
222 pr_debug("tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x"
223 " scsi_buf_len: %u\n", sc->device->host->host_no,
224 sc->device->id, sc->device->channel, sc->device->lun,
225 sc->cmnd[0], scsi_bufflen(sc));
227 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
228 if (!tl_cmd) {
229 pr_err("Unable to allocate struct tcm_loop_cmd\n");
230 set_host_byte(sc, DID_ERROR);
231 sc->scsi_done(sc);
232 return 0;
235 tl_cmd->sc = sc;
236 INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
237 queue_work(tcm_loop_workqueue, &tl_cmd->work);
238 return 0;
242 * Called from SCSI EH process context to issue a LUN_RESET TMR
243 * to struct scsi_device
245 static int tcm_loop_device_reset(struct scsi_cmnd *sc)
247 struct se_cmd *se_cmd = NULL;
248 struct se_portal_group *se_tpg;
249 struct se_session *se_sess;
250 struct tcm_loop_cmd *tl_cmd = NULL;
251 struct tcm_loop_hba *tl_hba;
252 struct tcm_loop_nexus *tl_nexus;
253 struct tcm_loop_tmr *tl_tmr = NULL;
254 struct tcm_loop_tpg *tl_tpg;
255 int ret = FAILED, rc;
257 * Locate the tcm_loop_hba_t pointer
259 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
261 * Locate the tl_tpg and se_tpg pointers from TargetID in sc->device->id
263 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
264 se_tpg = &tl_tpg->tl_se_tpg;
266 * Locate the tl_nexus and se_sess pointers
268 tl_nexus = tl_tpg->tl_nexus;
269 if (!tl_nexus) {
270 pr_err("Unable to perform device reset without"
271 " active I_T Nexus\n");
272 return FAILED;
274 se_sess = tl_nexus->se_sess;
276 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
277 if (!tl_cmd) {
278 pr_err("Unable to allocate memory for tl_cmd\n");
279 return FAILED;
282 tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
283 if (!tl_tmr) {
284 pr_err("Unable to allocate memory for tl_tmr\n");
285 goto release;
287 init_waitqueue_head(&tl_tmr->tl_tmr_wait);
289 se_cmd = &tl_cmd->tl_se_cmd;
291 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
293 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
294 DMA_NONE, MSG_SIMPLE_TAG,
295 &tl_cmd->tl_sense_buf[0]);
297 rc = core_tmr_alloc_req(se_cmd, tl_tmr, TMR_LUN_RESET, GFP_KERNEL);
298 if (rc < 0)
299 goto release;
301 * Locate the underlying TCM struct se_lun from sc->device->lun
303 if (transport_lookup_tmr_lun(se_cmd, sc->device->lun) < 0)
304 goto release;
306 * Queue the TMR to TCM Core and sleep waiting for tcm_loop_queue_tm_rsp()
307 * to wake us up.
309 transport_generic_handle_tmr(se_cmd);
310 wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
312 * The TMR LUN_RESET has completed, check the response status and
313 * then release allocations.
315 ret = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
316 SUCCESS : FAILED;
317 release:
318 if (se_cmd)
319 transport_generic_free_cmd(se_cmd, 1);
320 else
321 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
322 kfree(tl_tmr);
323 return ret;
326 static int tcm_loop_slave_alloc(struct scsi_device *sd)
328 set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
329 return 0;
332 static int tcm_loop_slave_configure(struct scsi_device *sd)
334 return 0;
337 static struct scsi_host_template tcm_loop_driver_template = {
338 .show_info = tcm_loop_show_info,
339 .proc_name = "tcm_loopback",
340 .name = "TCM_Loopback",
341 .queuecommand = tcm_loop_queuecommand,
342 .change_queue_depth = tcm_loop_change_queue_depth,
343 .eh_device_reset_handler = tcm_loop_device_reset,
344 .can_queue = 1024,
345 .this_id = -1,
346 .sg_tablesize = 256,
347 .cmd_per_lun = 1024,
348 .max_sectors = 0xFFFF,
349 .use_clustering = DISABLE_CLUSTERING,
350 .slave_alloc = tcm_loop_slave_alloc,
351 .slave_configure = tcm_loop_slave_configure,
352 .module = THIS_MODULE,
355 static int tcm_loop_driver_probe(struct device *dev)
357 struct tcm_loop_hba *tl_hba;
358 struct Scsi_Host *sh;
359 int error;
361 tl_hba = to_tcm_loop_hba(dev);
363 sh = scsi_host_alloc(&tcm_loop_driver_template,
364 sizeof(struct tcm_loop_hba));
365 if (!sh) {
366 pr_err("Unable to allocate struct scsi_host\n");
367 return -ENODEV;
369 tl_hba->sh = sh;
372 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
374 *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
376 * Setup single ID, Channel and LUN for now..
378 sh->max_id = 2;
379 sh->max_lun = 0;
380 sh->max_channel = 0;
381 sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN;
383 error = scsi_add_host(sh, &tl_hba->dev);
384 if (error) {
385 pr_err("%s: scsi_add_host failed\n", __func__);
386 scsi_host_put(sh);
387 return -ENODEV;
389 return 0;
392 static int tcm_loop_driver_remove(struct device *dev)
394 struct tcm_loop_hba *tl_hba;
395 struct Scsi_Host *sh;
397 tl_hba = to_tcm_loop_hba(dev);
398 sh = tl_hba->sh;
400 scsi_remove_host(sh);
401 scsi_host_put(sh);
402 return 0;
405 static void tcm_loop_release_adapter(struct device *dev)
407 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
409 kfree(tl_hba);
413 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
415 static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
417 int ret;
419 tl_hba->dev.bus = &tcm_loop_lld_bus;
420 tl_hba->dev.parent = tcm_loop_primary;
421 tl_hba->dev.release = &tcm_loop_release_adapter;
422 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
424 ret = device_register(&tl_hba->dev);
425 if (ret) {
426 pr_err("device_register() failed for"
427 " tl_hba->dev: %d\n", ret);
428 return -ENODEV;
431 return 0;
435 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
436 * tcm_loop SCSI bus.
438 static int tcm_loop_alloc_core_bus(void)
440 int ret;
442 tcm_loop_primary = root_device_register("tcm_loop_0");
443 if (IS_ERR(tcm_loop_primary)) {
444 pr_err("Unable to allocate tcm_loop_primary\n");
445 return PTR_ERR(tcm_loop_primary);
448 ret = bus_register(&tcm_loop_lld_bus);
449 if (ret) {
450 pr_err("bus_register() failed for tcm_loop_lld_bus\n");
451 goto dev_unreg;
454 ret = driver_register(&tcm_loop_driverfs);
455 if (ret) {
456 pr_err("driver_register() failed for"
457 "tcm_loop_driverfs\n");
458 goto bus_unreg;
461 pr_debug("Initialized TCM Loop Core Bus\n");
462 return ret;
464 bus_unreg:
465 bus_unregister(&tcm_loop_lld_bus);
466 dev_unreg:
467 root_device_unregister(tcm_loop_primary);
468 return ret;
471 static void tcm_loop_release_core_bus(void)
473 driver_unregister(&tcm_loop_driverfs);
474 bus_unregister(&tcm_loop_lld_bus);
475 root_device_unregister(tcm_loop_primary);
477 pr_debug("Releasing TCM Loop Core BUS\n");
480 static char *tcm_loop_get_fabric_name(void)
482 return "loopback";
485 static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
487 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
488 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
490 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
491 * time based on the protocol dependent prefix of the passed configfs group.
493 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
494 * ProtocolID using target_core_fabric_lib.c symbols.
496 switch (tl_hba->tl_proto_id) {
497 case SCSI_PROTOCOL_SAS:
498 return sas_get_fabric_proto_ident(se_tpg);
499 case SCSI_PROTOCOL_FCP:
500 return fc_get_fabric_proto_ident(se_tpg);
501 case SCSI_PROTOCOL_ISCSI:
502 return iscsi_get_fabric_proto_ident(se_tpg);
503 default:
504 pr_err("Unknown tl_proto_id: 0x%02x, using"
505 " SAS emulation\n", tl_hba->tl_proto_id);
506 break;
509 return sas_get_fabric_proto_ident(se_tpg);
512 static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
514 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
516 * Return the passed NAA identifier for the SAS Target Port
518 return &tl_tpg->tl_hba->tl_wwn_address[0];
521 static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
523 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
525 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
526 * to represent the SCSI Target Port.
528 return tl_tpg->tl_tpgt;
531 static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
533 return 1;
536 static u32 tcm_loop_get_pr_transport_id(
537 struct se_portal_group *se_tpg,
538 struct se_node_acl *se_nacl,
539 struct t10_pr_registration *pr_reg,
540 int *format_code,
541 unsigned char *buf)
543 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
544 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
546 switch (tl_hba->tl_proto_id) {
547 case SCSI_PROTOCOL_SAS:
548 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
549 format_code, buf);
550 case SCSI_PROTOCOL_FCP:
551 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
552 format_code, buf);
553 case SCSI_PROTOCOL_ISCSI:
554 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
555 format_code, buf);
556 default:
557 pr_err("Unknown tl_proto_id: 0x%02x, using"
558 " SAS emulation\n", tl_hba->tl_proto_id);
559 break;
562 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
563 format_code, buf);
566 static u32 tcm_loop_get_pr_transport_id_len(
567 struct se_portal_group *se_tpg,
568 struct se_node_acl *se_nacl,
569 struct t10_pr_registration *pr_reg,
570 int *format_code)
572 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
573 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
575 switch (tl_hba->tl_proto_id) {
576 case SCSI_PROTOCOL_SAS:
577 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
578 format_code);
579 case SCSI_PROTOCOL_FCP:
580 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
581 format_code);
582 case SCSI_PROTOCOL_ISCSI:
583 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
584 format_code);
585 default:
586 pr_err("Unknown tl_proto_id: 0x%02x, using"
587 " SAS emulation\n", tl_hba->tl_proto_id);
588 break;
591 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
592 format_code);
596 * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
597 * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
599 static char *tcm_loop_parse_pr_out_transport_id(
600 struct se_portal_group *se_tpg,
601 const char *buf,
602 u32 *out_tid_len,
603 char **port_nexus_ptr)
605 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
606 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
608 switch (tl_hba->tl_proto_id) {
609 case SCSI_PROTOCOL_SAS:
610 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
611 port_nexus_ptr);
612 case SCSI_PROTOCOL_FCP:
613 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
614 port_nexus_ptr);
615 case SCSI_PROTOCOL_ISCSI:
616 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
617 port_nexus_ptr);
618 default:
619 pr_err("Unknown tl_proto_id: 0x%02x, using"
620 " SAS emulation\n", tl_hba->tl_proto_id);
621 break;
624 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
625 port_nexus_ptr);
629 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
630 * based upon the incoming fabric dependent SCSI Initiator Port
632 static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
634 return 1;
637 static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
639 return 0;
643 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
644 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
646 static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
648 return 0;
652 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
653 * never be called for TCM_Loop by target_core_fabric_configfs.c code.
654 * It has been added here as a nop for target_fabric_tf_ops_check()
656 static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
658 return 0;
661 static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
662 struct se_portal_group *se_tpg)
664 struct tcm_loop_nacl *tl_nacl;
666 tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
667 if (!tl_nacl) {
668 pr_err("Unable to allocate struct tcm_loop_nacl\n");
669 return NULL;
672 return &tl_nacl->se_node_acl;
675 static void tcm_loop_tpg_release_fabric_acl(
676 struct se_portal_group *se_tpg,
677 struct se_node_acl *se_nacl)
679 struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
680 struct tcm_loop_nacl, se_node_acl);
682 kfree(tl_nacl);
685 static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
687 return 1;
690 static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
692 return 1;
695 static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
697 return;
700 static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
702 return 1;
705 static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
707 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
708 struct tcm_loop_cmd, tl_se_cmd);
710 return tl_cmd->sc_cmd_state;
713 static int tcm_loop_shutdown_session(struct se_session *se_sess)
715 return 0;
718 static void tcm_loop_close_session(struct se_session *se_sess)
720 return;
723 static int tcm_loop_write_pending(struct se_cmd *se_cmd)
726 * Since Linux/SCSI has already sent down a struct scsi_cmnd
727 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
728 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
729 * format with transport_generic_map_mem_to_cmd().
731 * We now tell TCM to add this WRITE CDB directly into the TCM storage
732 * object execution queue.
734 target_execute_cmd(se_cmd);
735 return 0;
738 static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
740 return 0;
743 static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
745 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
746 struct tcm_loop_cmd, tl_se_cmd);
747 struct scsi_cmnd *sc = tl_cmd->sc;
749 pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
750 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
752 sc->result = SAM_STAT_GOOD;
753 set_host_byte(sc, DID_OK);
754 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
755 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
756 scsi_set_resid(sc, se_cmd->residual_count);
757 sc->scsi_done(sc);
758 return 0;
761 static int tcm_loop_queue_status(struct se_cmd *se_cmd)
763 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
764 struct tcm_loop_cmd, tl_se_cmd);
765 struct scsi_cmnd *sc = tl_cmd->sc;
767 pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
768 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
770 if (se_cmd->sense_buffer &&
771 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
772 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
774 memcpy(sc->sense_buffer, se_cmd->sense_buffer,
775 SCSI_SENSE_BUFFERSIZE);
776 sc->result = SAM_STAT_CHECK_CONDITION;
777 set_driver_byte(sc, DRIVER_SENSE);
778 } else
779 sc->result = se_cmd->scsi_status;
781 set_host_byte(sc, DID_OK);
782 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
783 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
784 scsi_set_resid(sc, se_cmd->residual_count);
785 sc->scsi_done(sc);
786 return 0;
789 static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
791 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
792 struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
794 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
795 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
797 atomic_set(&tl_tmr->tmr_complete, 1);
798 wake_up(&tl_tmr->tl_tmr_wait);
801 static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
803 switch (tl_hba->tl_proto_id) {
804 case SCSI_PROTOCOL_SAS:
805 return "SAS";
806 case SCSI_PROTOCOL_FCP:
807 return "FCP";
808 case SCSI_PROTOCOL_ISCSI:
809 return "iSCSI";
810 default:
811 break;
814 return "Unknown";
817 /* Start items for tcm_loop_port_cit */
819 static int tcm_loop_port_link(
820 struct se_portal_group *se_tpg,
821 struct se_lun *lun)
823 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
824 struct tcm_loop_tpg, tl_se_tpg);
825 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
827 atomic_inc(&tl_tpg->tl_tpg_port_count);
828 smp_mb__after_atomic_inc();
830 * Add Linux/SCSI struct scsi_device by HCTL
832 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
834 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
835 return 0;
838 static void tcm_loop_port_unlink(
839 struct se_portal_group *se_tpg,
840 struct se_lun *se_lun)
842 struct scsi_device *sd;
843 struct tcm_loop_hba *tl_hba;
844 struct tcm_loop_tpg *tl_tpg;
846 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
847 tl_hba = tl_tpg->tl_hba;
849 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
850 se_lun->unpacked_lun);
851 if (!sd) {
852 pr_err("Unable to locate struct scsi_device for %d:%d:"
853 "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
854 return;
857 * Remove Linux/SCSI struct scsi_device by HCTL
859 scsi_remove_device(sd);
860 scsi_device_put(sd);
862 atomic_dec(&tl_tpg->tl_tpg_port_count);
863 smp_mb__after_atomic_dec();
865 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
868 /* End items for tcm_loop_port_cit */
870 /* Start items for tcm_loop_nexus_cit */
872 static int tcm_loop_make_nexus(
873 struct tcm_loop_tpg *tl_tpg,
874 const char *name)
876 struct se_portal_group *se_tpg;
877 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
878 struct tcm_loop_nexus *tl_nexus;
879 int ret = -ENOMEM;
881 if (tl_tpg->tl_nexus) {
882 pr_debug("tl_tpg->tl_nexus already exists\n");
883 return -EEXIST;
885 se_tpg = &tl_tpg->tl_se_tpg;
887 tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
888 if (!tl_nexus) {
889 pr_err("Unable to allocate struct tcm_loop_nexus\n");
890 return -ENOMEM;
893 * Initialize the struct se_session pointer
895 tl_nexus->se_sess = transport_init_session();
896 if (IS_ERR(tl_nexus->se_sess)) {
897 ret = PTR_ERR(tl_nexus->se_sess);
898 goto out;
901 * Since we are running in 'demo mode' this call with generate a
902 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
903 * Initiator port name of the passed configfs group 'name'.
905 tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
906 se_tpg, (unsigned char *)name);
907 if (!tl_nexus->se_sess->se_node_acl) {
908 transport_free_session(tl_nexus->se_sess);
909 goto out;
912 * Now, register the SAS I_T Nexus as active with the call to
913 * transport_register_session()
915 __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
916 tl_nexus->se_sess, tl_nexus);
917 tl_tpg->tl_nexus = tl_nexus;
918 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
919 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
920 name);
921 return 0;
923 out:
924 kfree(tl_nexus);
925 return ret;
928 static int tcm_loop_drop_nexus(
929 struct tcm_loop_tpg *tpg)
931 struct se_session *se_sess;
932 struct tcm_loop_nexus *tl_nexus;
934 tl_nexus = tpg->tl_nexus;
935 if (!tl_nexus)
936 return -ENODEV;
938 se_sess = tl_nexus->se_sess;
939 if (!se_sess)
940 return -ENODEV;
942 if (atomic_read(&tpg->tl_tpg_port_count)) {
943 pr_err("Unable to remove TCM_Loop I_T Nexus with"
944 " active TPG port count: %d\n",
945 atomic_read(&tpg->tl_tpg_port_count));
946 return -EPERM;
949 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
950 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tpg->tl_hba),
951 tl_nexus->se_sess->se_node_acl->initiatorname);
953 * Release the SCSI I_T Nexus to the emulated SAS Target Port
955 transport_deregister_session(tl_nexus->se_sess);
956 tpg->tl_nexus = NULL;
957 kfree(tl_nexus);
958 return 0;
961 /* End items for tcm_loop_nexus_cit */
963 static ssize_t tcm_loop_tpg_show_nexus(
964 struct se_portal_group *se_tpg,
965 char *page)
967 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
968 struct tcm_loop_tpg, tl_se_tpg);
969 struct tcm_loop_nexus *tl_nexus;
970 ssize_t ret;
972 tl_nexus = tl_tpg->tl_nexus;
973 if (!tl_nexus)
974 return -ENODEV;
976 ret = snprintf(page, PAGE_SIZE, "%s\n",
977 tl_nexus->se_sess->se_node_acl->initiatorname);
979 return ret;
982 static ssize_t tcm_loop_tpg_store_nexus(
983 struct se_portal_group *se_tpg,
984 const char *page,
985 size_t count)
987 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
988 struct tcm_loop_tpg, tl_se_tpg);
989 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
990 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
991 int ret;
993 * Shutdown the active I_T nexus if 'NULL' is passed..
995 if (!strncmp(page, "NULL", 4)) {
996 ret = tcm_loop_drop_nexus(tl_tpg);
997 return (!ret) ? count : ret;
1000 * Otherwise make sure the passed virtual Initiator port WWN matches
1001 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
1002 * tcm_loop_make_nexus()
1004 if (strlen(page) >= TL_WWN_ADDR_LEN) {
1005 pr_err("Emulated NAA Sas Address: %s, exceeds"
1006 " max: %d\n", page, TL_WWN_ADDR_LEN);
1007 return -EINVAL;
1009 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
1011 ptr = strstr(i_port, "naa.");
1012 if (ptr) {
1013 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
1014 pr_err("Passed SAS Initiator Port %s does not"
1015 " match target port protoid: %s\n", i_port,
1016 tcm_loop_dump_proto_id(tl_hba));
1017 return -EINVAL;
1019 port_ptr = &i_port[0];
1020 goto check_newline;
1022 ptr = strstr(i_port, "fc.");
1023 if (ptr) {
1024 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
1025 pr_err("Passed FCP Initiator Port %s does not"
1026 " match target port protoid: %s\n", i_port,
1027 tcm_loop_dump_proto_id(tl_hba));
1028 return -EINVAL;
1030 port_ptr = &i_port[3]; /* Skip over "fc." */
1031 goto check_newline;
1033 ptr = strstr(i_port, "iqn.");
1034 if (ptr) {
1035 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
1036 pr_err("Passed iSCSI Initiator Port %s does not"
1037 " match target port protoid: %s\n", i_port,
1038 tcm_loop_dump_proto_id(tl_hba));
1039 return -EINVAL;
1041 port_ptr = &i_port[0];
1042 goto check_newline;
1044 pr_err("Unable to locate prefix for emulated Initiator Port:"
1045 " %s\n", i_port);
1046 return -EINVAL;
1048 * Clear any trailing newline for the NAA WWN
1050 check_newline:
1051 if (i_port[strlen(i_port)-1] == '\n')
1052 i_port[strlen(i_port)-1] = '\0';
1054 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
1055 if (ret < 0)
1056 return ret;
1058 return count;
1061 TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
1063 static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
1064 &tcm_loop_tpg_nexus.attr,
1065 NULL,
1068 /* Start items for tcm_loop_naa_cit */
1070 struct se_portal_group *tcm_loop_make_naa_tpg(
1071 struct se_wwn *wwn,
1072 struct config_group *group,
1073 const char *name)
1075 struct tcm_loop_hba *tl_hba = container_of(wwn,
1076 struct tcm_loop_hba, tl_hba_wwn);
1077 struct tcm_loop_tpg *tl_tpg;
1078 char *tpgt_str, *end_ptr;
1079 int ret;
1080 unsigned short int tpgt;
1082 tpgt_str = strstr(name, "tpgt_");
1083 if (!tpgt_str) {
1084 pr_err("Unable to locate \"tpgt_#\" directory"
1085 " group\n");
1086 return ERR_PTR(-EINVAL);
1088 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1089 tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1091 if (tpgt >= TL_TPGS_PER_HBA) {
1092 pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
1093 " %u\n", tpgt, TL_TPGS_PER_HBA);
1094 return ERR_PTR(-EINVAL);
1096 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
1097 tl_tpg->tl_hba = tl_hba;
1098 tl_tpg->tl_tpgt = tpgt;
1100 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
1102 ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
1103 wwn, &tl_tpg->tl_se_tpg, tl_tpg,
1104 TRANSPORT_TPG_TYPE_NORMAL);
1105 if (ret < 0)
1106 return ERR_PTR(-ENOMEM);
1108 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
1109 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1110 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1112 return &tl_tpg->tl_se_tpg;
1115 void tcm_loop_drop_naa_tpg(
1116 struct se_portal_group *se_tpg)
1118 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1119 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1120 struct tcm_loop_tpg, tl_se_tpg);
1121 struct tcm_loop_hba *tl_hba;
1122 unsigned short tpgt;
1124 tl_hba = tl_tpg->tl_hba;
1125 tpgt = tl_tpg->tl_tpgt;
1127 * Release the I_T Nexus for the Virtual SAS link if present
1129 tcm_loop_drop_nexus(tl_tpg);
1131 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
1133 core_tpg_deregister(se_tpg);
1135 tl_tpg->tl_hba = NULL;
1136 tl_tpg->tl_tpgt = 0;
1138 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
1139 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1140 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1143 /* End items for tcm_loop_naa_cit */
1145 /* Start items for tcm_loop_cit */
1147 struct se_wwn *tcm_loop_make_scsi_hba(
1148 struct target_fabric_configfs *tf,
1149 struct config_group *group,
1150 const char *name)
1152 struct tcm_loop_hba *tl_hba;
1153 struct Scsi_Host *sh;
1154 char *ptr;
1155 int ret, off = 0;
1157 tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
1158 if (!tl_hba) {
1159 pr_err("Unable to allocate struct tcm_loop_hba\n");
1160 return ERR_PTR(-ENOMEM);
1163 * Determine the emulated Protocol Identifier and Target Port Name
1164 * based on the incoming configfs directory name.
1166 ptr = strstr(name, "naa.");
1167 if (ptr) {
1168 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1169 goto check_len;
1171 ptr = strstr(name, "fc.");
1172 if (ptr) {
1173 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1174 off = 3; /* Skip over "fc." */
1175 goto check_len;
1177 ptr = strstr(name, "iqn.");
1178 if (!ptr) {
1179 pr_err("Unable to locate prefix for emulated Target "
1180 "Port: %s\n", name);
1181 ret = -EINVAL;
1182 goto out;
1184 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
1186 check_len:
1187 if (strlen(name) >= TL_WWN_ADDR_LEN) {
1188 pr_err("Emulated NAA %s Address: %s, exceeds"
1189 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1190 TL_WWN_ADDR_LEN);
1191 ret = -EINVAL;
1192 goto out;
1194 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1197 * Call device_register(tl_hba->dev) to register the emulated
1198 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1199 * device_register() callbacks in tcm_loop_driver_probe()
1201 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1202 if (ret)
1203 goto out;
1205 sh = tl_hba->sh;
1206 tcm_loop_hba_no_cnt++;
1207 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
1208 " %s Address: %s at Linux/SCSI Host ID: %d\n",
1209 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1211 return &tl_hba->tl_hba_wwn;
1212 out:
1213 kfree(tl_hba);
1214 return ERR_PTR(ret);
1217 void tcm_loop_drop_scsi_hba(
1218 struct se_wwn *wwn)
1220 struct tcm_loop_hba *tl_hba = container_of(wwn,
1221 struct tcm_loop_hba, tl_hba_wwn);
1223 pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
1224 " SAS Address: %s at Linux/SCSI Host ID: %d\n",
1225 tl_hba->tl_wwn_address, tl_hba->sh->host_no);
1227 * Call device_unregister() on the original tl_hba->dev.
1228 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1229 * release *tl_hba;
1231 device_unregister(&tl_hba->dev);
1234 /* Start items for tcm_loop_cit */
1235 static ssize_t tcm_loop_wwn_show_attr_version(
1236 struct target_fabric_configfs *tf,
1237 char *page)
1239 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1242 TF_WWN_ATTR_RO(tcm_loop, version);
1244 static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1245 &tcm_loop_wwn_version.attr,
1246 NULL,
1249 /* End items for tcm_loop_cit */
1251 static int tcm_loop_register_configfs(void)
1253 struct target_fabric_configfs *fabric;
1254 int ret;
1256 * Set the TCM Loop HBA counter to zero
1258 tcm_loop_hba_no_cnt = 0;
1260 * Register the top level struct config_item_type with TCM core
1262 fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
1263 if (IS_ERR(fabric)) {
1264 pr_err("tcm_loop_register_configfs() failed!\n");
1265 return PTR_ERR(fabric);
1268 * Setup the fabric API of function pointers used by target_core_mod
1270 fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
1271 fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
1272 fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
1273 fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
1274 fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
1275 fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
1276 fabric->tf_ops.tpg_get_pr_transport_id_len =
1277 &tcm_loop_get_pr_transport_id_len;
1278 fabric->tf_ops.tpg_parse_pr_out_transport_id =
1279 &tcm_loop_parse_pr_out_transport_id;
1280 fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
1281 fabric->tf_ops.tpg_check_demo_mode_cache =
1282 &tcm_loop_check_demo_mode_cache;
1283 fabric->tf_ops.tpg_check_demo_mode_write_protect =
1284 &tcm_loop_check_demo_mode_write_protect;
1285 fabric->tf_ops.tpg_check_prod_mode_write_protect =
1286 &tcm_loop_check_prod_mode_write_protect;
1288 * The TCM loopback fabric module runs in demo-mode to a local
1289 * virtual SCSI device, so fabric dependent initator ACLs are
1290 * not required.
1292 fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
1293 fabric->tf_ops.tpg_release_fabric_acl =
1294 &tcm_loop_tpg_release_fabric_acl;
1295 fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
1297 * Used for setting up remaining TCM resources in process context
1299 fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
1300 fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
1301 fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
1302 fabric->tf_ops.close_session = &tcm_loop_close_session;
1303 fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
1304 fabric->tf_ops.sess_get_initiator_sid = NULL;
1305 fabric->tf_ops.write_pending = &tcm_loop_write_pending;
1306 fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
1308 * Not used for TCM loopback
1310 fabric->tf_ops.set_default_node_attributes =
1311 &tcm_loop_set_default_node_attributes;
1312 fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
1313 fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
1314 fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
1315 fabric->tf_ops.queue_status = &tcm_loop_queue_status;
1316 fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
1319 * Setup function pointers for generic logic in target_core_fabric_configfs.c
1321 fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
1322 fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
1323 fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
1324 fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
1326 * fabric_post_link() and fabric_pre_unlink() are used for
1327 * registration and release of TCM Loop Virtual SCSI LUNs.
1329 fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
1330 fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
1331 fabric->tf_ops.fabric_make_np = NULL;
1332 fabric->tf_ops.fabric_drop_np = NULL;
1334 * Setup default attribute lists for various fabric->tf_cit_tmpl
1336 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
1337 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
1338 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1339 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1340 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1342 * Once fabric->tf_ops has been setup, now register the fabric for
1343 * use within TCM
1345 ret = target_fabric_configfs_register(fabric);
1346 if (ret < 0) {
1347 pr_err("target_fabric_configfs_register() for"
1348 " TCM_Loop failed!\n");
1349 target_fabric_configfs_free(fabric);
1350 return -1;
1353 * Setup our local pointer to *fabric.
1355 tcm_loop_fabric_configfs = fabric;
1356 pr_debug("TCM_LOOP[0] - Set fabric ->"
1357 " tcm_loop_fabric_configfs\n");
1358 return 0;
1361 static void tcm_loop_deregister_configfs(void)
1363 if (!tcm_loop_fabric_configfs)
1364 return;
1366 target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
1367 tcm_loop_fabric_configfs = NULL;
1368 pr_debug("TCM_LOOP[0] - Cleared"
1369 " tcm_loop_fabric_configfs\n");
1372 static int __init tcm_loop_fabric_init(void)
1374 int ret = -ENOMEM;
1376 tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
1377 if (!tcm_loop_workqueue)
1378 goto out;
1380 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1381 sizeof(struct tcm_loop_cmd),
1382 __alignof__(struct tcm_loop_cmd),
1383 0, NULL);
1384 if (!tcm_loop_cmd_cache) {
1385 pr_debug("kmem_cache_create() for"
1386 " tcm_loop_cmd_cache failed\n");
1387 goto out_destroy_workqueue;
1390 ret = tcm_loop_alloc_core_bus();
1391 if (ret)
1392 goto out_destroy_cache;
1394 ret = tcm_loop_register_configfs();
1395 if (ret)
1396 goto out_release_core_bus;
1398 return 0;
1400 out_release_core_bus:
1401 tcm_loop_release_core_bus();
1402 out_destroy_cache:
1403 kmem_cache_destroy(tcm_loop_cmd_cache);
1404 out_destroy_workqueue:
1405 destroy_workqueue(tcm_loop_workqueue);
1406 out:
1407 return ret;
1410 static void __exit tcm_loop_fabric_exit(void)
1412 tcm_loop_deregister_configfs();
1413 tcm_loop_release_core_bus();
1414 kmem_cache_destroy(tcm_loop_cmd_cache);
1415 destroy_workqueue(tcm_loop_workqueue);
1418 MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1419 MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1420 MODULE_LICENSE("GPL");
1421 module_init(tcm_loop_fabric_init);
1422 module_exit(tcm_loop_fabric_exit);