LiteX: driver for MMCM
[linux/fpc-iii.git] / drivers / target / loopback / tcm_loop.c
blobbadba437e5f9143549c437ef5fb29048bd3b9d65
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>
38 #include "tcm_loop.h"
40 #define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev)
42 static struct workqueue_struct *tcm_loop_workqueue;
43 static struct kmem_cache *tcm_loop_cmd_cache;
45 static int tcm_loop_hba_no_cnt;
47 static int tcm_loop_queue_status(struct se_cmd *se_cmd);
49 static unsigned int tcm_loop_nr_hw_queues = 1;
50 module_param_named(nr_hw_queues, tcm_loop_nr_hw_queues, uint, 0644);
52 static unsigned int tcm_loop_can_queue = 1024;
53 module_param_named(can_queue, tcm_loop_can_queue, uint, 0644);
55 static unsigned int tcm_loop_cmd_per_lun = 1024;
56 module_param_named(cmd_per_lun, tcm_loop_cmd_per_lun, uint, 0644);
59 * Called from struct target_core_fabric_ops->check_stop_free()
61 static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
63 return transport_generic_free_cmd(se_cmd, 0);
66 static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
68 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
69 struct tcm_loop_cmd, tl_se_cmd);
71 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
74 static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host)
76 seq_puts(m, "tcm_loop_proc_info()\n");
77 return 0;
80 static int tcm_loop_driver_probe(struct device *);
81 static int tcm_loop_driver_remove(struct device *);
83 static int pseudo_lld_bus_match(struct device *dev,
84 struct device_driver *dev_driver)
86 return 1;
89 static struct bus_type tcm_loop_lld_bus = {
90 .name = "tcm_loop_bus",
91 .match = pseudo_lld_bus_match,
92 .probe = tcm_loop_driver_probe,
93 .remove = tcm_loop_driver_remove,
96 static struct device_driver tcm_loop_driverfs = {
97 .name = "tcm_loop",
98 .bus = &tcm_loop_lld_bus,
101 * Used with root_device_register() in tcm_loop_alloc_core_bus() below
103 static struct device *tcm_loop_primary;
105 static void tcm_loop_submission_work(struct work_struct *work)
107 struct tcm_loop_cmd *tl_cmd =
108 container_of(work, struct tcm_loop_cmd, work);
109 struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
110 struct scsi_cmnd *sc = tl_cmd->sc;
111 struct tcm_loop_nexus *tl_nexus;
112 struct tcm_loop_hba *tl_hba;
113 struct tcm_loop_tpg *tl_tpg;
114 struct scatterlist *sgl_bidi = NULL;
115 u32 sgl_bidi_count = 0, transfer_length;
116 int rc;
118 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
119 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
122 * Ensure that this tl_tpg reference from the incoming sc->device->id
123 * has already been configured via tcm_loop_make_naa_tpg().
125 if (!tl_tpg->tl_hba) {
126 set_host_byte(sc, DID_NO_CONNECT);
127 goto out_done;
129 if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) {
130 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
131 goto out_done;
133 tl_nexus = tl_tpg->tl_nexus;
134 if (!tl_nexus) {
135 scmd_printk(KERN_ERR, sc,
136 "TCM_Loop I_T Nexus does not exist\n");
137 set_host_byte(sc, DID_ERROR);
138 goto out_done;
141 transfer_length = scsi_transfer_length(sc);
142 if (!scsi_prot_sg_count(sc) &&
143 scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) {
144 se_cmd->prot_pto = true;
146 * loopback transport doesn't support
147 * WRITE_GENERATE, READ_STRIP protection
148 * information operations, go ahead unprotected.
150 transfer_length = scsi_bufflen(sc);
153 se_cmd->tag = tl_cmd->sc_cmd_tag;
154 rc = target_submit_cmd_map_sgls(se_cmd, tl_nexus->se_sess, sc->cmnd,
155 &tl_cmd->tl_sense_buf[0], tl_cmd->sc->device->lun,
156 transfer_length, TCM_SIMPLE_TAG,
157 sc->sc_data_direction, 0,
158 scsi_sglist(sc), scsi_sg_count(sc),
159 sgl_bidi, sgl_bidi_count,
160 scsi_prot_sglist(sc), scsi_prot_sg_count(sc));
161 if (rc < 0) {
162 set_host_byte(sc, DID_NO_CONNECT);
163 goto out_done;
165 return;
167 out_done:
168 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
169 sc->scsi_done(sc);
173 * ->queuecommand can be and usually is called from interrupt context, so
174 * defer the actual submission to a workqueue.
176 static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
178 struct tcm_loop_cmd *tl_cmd;
180 pr_debug("%s() %d:%d:%d:%llu got CDB: 0x%02x scsi_buf_len: %u\n",
181 __func__, sc->device->host->host_no, sc->device->id,
182 sc->device->channel, sc->device->lun, sc->cmnd[0],
183 scsi_bufflen(sc));
185 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
186 if (!tl_cmd) {
187 set_host_byte(sc, DID_ERROR);
188 sc->scsi_done(sc);
189 return 0;
192 tl_cmd->sc = sc;
193 tl_cmd->sc_cmd_tag = sc->request->tag;
194 INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
195 queue_work(tcm_loop_workqueue, &tl_cmd->work);
196 return 0;
200 * Called from SCSI EH process context to issue a LUN_RESET TMR
201 * to struct scsi_device
203 static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg,
204 u64 lun, int task, enum tcm_tmreq_table tmr)
206 struct se_cmd *se_cmd;
207 struct se_session *se_sess;
208 struct tcm_loop_nexus *tl_nexus;
209 struct tcm_loop_cmd *tl_cmd;
210 int ret = TMR_FUNCTION_FAILED, rc;
213 * Locate the tl_nexus and se_sess pointers
215 tl_nexus = tl_tpg->tl_nexus;
216 if (!tl_nexus) {
217 pr_err("Unable to perform device reset without active I_T Nexus\n");
218 return ret;
221 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
222 if (!tl_cmd)
223 return ret;
225 init_completion(&tl_cmd->tmr_done);
227 se_cmd = &tl_cmd->tl_se_cmd;
228 se_sess = tl_tpg->tl_nexus->se_sess;
230 rc = target_submit_tmr(se_cmd, se_sess, tl_cmd->tl_sense_buf, lun,
231 NULL, tmr, GFP_KERNEL, task,
232 TARGET_SCF_ACK_KREF);
233 if (rc < 0)
234 goto release;
235 wait_for_completion(&tl_cmd->tmr_done);
236 ret = se_cmd->se_tmr_req->response;
237 target_put_sess_cmd(se_cmd);
239 out:
240 return ret;
242 release:
243 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
244 goto out;
247 static int tcm_loop_abort_task(struct scsi_cmnd *sc)
249 struct tcm_loop_hba *tl_hba;
250 struct tcm_loop_tpg *tl_tpg;
251 int ret = FAILED;
254 * Locate the tcm_loop_hba_t pointer
256 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
257 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
258 ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
259 sc->request->tag, TMR_ABORT_TASK);
260 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
264 * Called from SCSI EH process context to issue a LUN_RESET TMR
265 * to struct scsi_device
267 static int tcm_loop_device_reset(struct scsi_cmnd *sc)
269 struct tcm_loop_hba *tl_hba;
270 struct tcm_loop_tpg *tl_tpg;
271 int ret = FAILED;
274 * Locate the tcm_loop_hba_t pointer
276 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
277 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
279 ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
280 0, TMR_LUN_RESET);
281 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
284 static int tcm_loop_target_reset(struct scsi_cmnd *sc)
286 struct tcm_loop_hba *tl_hba;
287 struct tcm_loop_tpg *tl_tpg;
290 * Locate the tcm_loop_hba_t pointer
292 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
293 if (!tl_hba) {
294 pr_err("Unable to perform device reset without active I_T Nexus\n");
295 return FAILED;
298 * Locate the tl_tpg pointer from TargetID in sc->device->id
300 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
301 if (tl_tpg) {
302 tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
303 return SUCCESS;
305 return FAILED;
308 static struct scsi_host_template tcm_loop_driver_template = {
309 .show_info = tcm_loop_show_info,
310 .proc_name = "tcm_loopback",
311 .name = "TCM_Loopback",
312 .queuecommand = tcm_loop_queuecommand,
313 .change_queue_depth = scsi_change_queue_depth,
314 .eh_abort_handler = tcm_loop_abort_task,
315 .eh_device_reset_handler = tcm_loop_device_reset,
316 .eh_target_reset_handler = tcm_loop_target_reset,
317 .this_id = -1,
318 .sg_tablesize = 256,
319 .max_sectors = 0xFFFF,
320 .dma_boundary = PAGE_SIZE - 1,
321 .module = THIS_MODULE,
322 .track_queue_depth = 1,
325 static int tcm_loop_driver_probe(struct device *dev)
327 struct tcm_loop_hba *tl_hba;
328 struct Scsi_Host *sh;
329 int error, host_prot;
331 tl_hba = to_tcm_loop_hba(dev);
333 sh = scsi_host_alloc(&tcm_loop_driver_template,
334 sizeof(struct tcm_loop_hba));
335 if (!sh) {
336 pr_err("Unable to allocate struct scsi_host\n");
337 return -ENODEV;
339 tl_hba->sh = sh;
342 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
344 *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
346 * Setup single ID, Channel and LUN for now..
348 sh->max_id = 2;
349 sh->max_lun = 0;
350 sh->max_channel = 0;
351 sh->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
352 sh->nr_hw_queues = tcm_loop_nr_hw_queues;
353 sh->can_queue = tcm_loop_can_queue;
354 sh->cmd_per_lun = tcm_loop_cmd_per_lun;
356 host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
357 SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
358 SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
360 scsi_host_set_prot(sh, host_prot);
361 scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC);
363 error = scsi_add_host(sh, &tl_hba->dev);
364 if (error) {
365 pr_err("%s: scsi_add_host failed\n", __func__);
366 scsi_host_put(sh);
367 return -ENODEV;
369 return 0;
372 static int tcm_loop_driver_remove(struct device *dev)
374 struct tcm_loop_hba *tl_hba;
375 struct Scsi_Host *sh;
377 tl_hba = to_tcm_loop_hba(dev);
378 sh = tl_hba->sh;
380 scsi_remove_host(sh);
381 scsi_host_put(sh);
382 return 0;
385 static void tcm_loop_release_adapter(struct device *dev)
387 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
389 kfree(tl_hba);
393 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
395 static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
397 int ret;
399 tl_hba->dev.bus = &tcm_loop_lld_bus;
400 tl_hba->dev.parent = tcm_loop_primary;
401 tl_hba->dev.release = &tcm_loop_release_adapter;
402 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
404 ret = device_register(&tl_hba->dev);
405 if (ret) {
406 pr_err("device_register() failed for tl_hba->dev: %d\n", ret);
407 return -ENODEV;
410 return 0;
414 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
415 * tcm_loop SCSI bus.
417 static int tcm_loop_alloc_core_bus(void)
419 int ret;
421 tcm_loop_primary = root_device_register("tcm_loop_0");
422 if (IS_ERR(tcm_loop_primary)) {
423 pr_err("Unable to allocate tcm_loop_primary\n");
424 return PTR_ERR(tcm_loop_primary);
427 ret = bus_register(&tcm_loop_lld_bus);
428 if (ret) {
429 pr_err("bus_register() failed for tcm_loop_lld_bus\n");
430 goto dev_unreg;
433 ret = driver_register(&tcm_loop_driverfs);
434 if (ret) {
435 pr_err("driver_register() failed for tcm_loop_driverfs\n");
436 goto bus_unreg;
439 pr_debug("Initialized TCM Loop Core Bus\n");
440 return ret;
442 bus_unreg:
443 bus_unregister(&tcm_loop_lld_bus);
444 dev_unreg:
445 root_device_unregister(tcm_loop_primary);
446 return ret;
449 static void tcm_loop_release_core_bus(void)
451 driver_unregister(&tcm_loop_driverfs);
452 bus_unregister(&tcm_loop_lld_bus);
453 root_device_unregister(tcm_loop_primary);
455 pr_debug("Releasing TCM Loop Core BUS\n");
458 static inline struct tcm_loop_tpg *tl_tpg(struct se_portal_group *se_tpg)
460 return container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
463 static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
466 * Return the passed NAA identifier for the Target Port
468 return &tl_tpg(se_tpg)->tl_hba->tl_wwn_address[0];
471 static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
474 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
475 * to represent the SCSI Target Port.
477 return tl_tpg(se_tpg)->tl_tpgt;
481 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
482 * based upon the incoming fabric dependent SCSI Initiator Port
484 static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
486 return 1;
489 static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
491 return 0;
495 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
496 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
498 static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
500 return 0;
504 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
505 * never be called for TCM_Loop by target_core_fabric_configfs.c code.
506 * It has been added here as a nop for target_fabric_tf_ops_check()
508 static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
510 return 0;
513 static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg)
515 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
516 tl_se_tpg);
517 return tl_tpg->tl_fabric_prot_type;
520 static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
522 return 1;
525 static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
527 return 1;
530 static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
532 return;
535 static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
537 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
538 struct tcm_loop_cmd, tl_se_cmd);
540 return tl_cmd->sc_cmd_state;
543 static int tcm_loop_write_pending(struct se_cmd *se_cmd)
546 * Since Linux/SCSI has already sent down a struct scsi_cmnd
547 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
548 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
549 * format with transport_generic_map_mem_to_cmd().
551 * We now tell TCM to add this WRITE CDB directly into the TCM storage
552 * object execution queue.
554 target_execute_cmd(se_cmd);
555 return 0;
558 static int tcm_loop_queue_data_or_status(const char *func,
559 struct se_cmd *se_cmd, u8 scsi_status)
561 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
562 struct tcm_loop_cmd, tl_se_cmd);
563 struct scsi_cmnd *sc = tl_cmd->sc;
565 pr_debug("%s() called for scsi_cmnd: %p cdb: 0x%02x\n",
566 func, sc, sc->cmnd[0]);
568 if (se_cmd->sense_buffer &&
569 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
570 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
572 memcpy(sc->sense_buffer, se_cmd->sense_buffer,
573 SCSI_SENSE_BUFFERSIZE);
574 sc->result = SAM_STAT_CHECK_CONDITION;
575 set_driver_byte(sc, DRIVER_SENSE);
576 } else
577 sc->result = scsi_status;
579 set_host_byte(sc, DID_OK);
580 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
581 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
582 scsi_set_resid(sc, se_cmd->residual_count);
583 sc->scsi_done(sc);
584 return 0;
587 static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
589 return tcm_loop_queue_data_or_status(__func__, se_cmd, SAM_STAT_GOOD);
592 static int tcm_loop_queue_status(struct se_cmd *se_cmd)
594 return tcm_loop_queue_data_or_status(__func__,
595 se_cmd, se_cmd->scsi_status);
598 static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
600 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
601 struct tcm_loop_cmd, tl_se_cmd);
603 /* Wake up tcm_loop_issue_tmr(). */
604 complete(&tl_cmd->tmr_done);
607 static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
609 return;
612 static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
614 switch (tl_hba->tl_proto_id) {
615 case SCSI_PROTOCOL_SAS:
616 return "SAS";
617 case SCSI_PROTOCOL_FCP:
618 return "FCP";
619 case SCSI_PROTOCOL_ISCSI:
620 return "iSCSI";
621 default:
622 break;
625 return "Unknown";
628 /* Start items for tcm_loop_port_cit */
630 static int tcm_loop_port_link(
631 struct se_portal_group *se_tpg,
632 struct se_lun *lun)
634 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
635 struct tcm_loop_tpg, tl_se_tpg);
636 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
638 atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
640 * Add Linux/SCSI struct scsi_device by HCTL
642 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
644 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
645 return 0;
648 static void tcm_loop_port_unlink(
649 struct se_portal_group *se_tpg,
650 struct se_lun *se_lun)
652 struct scsi_device *sd;
653 struct tcm_loop_hba *tl_hba;
654 struct tcm_loop_tpg *tl_tpg;
656 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
657 tl_hba = tl_tpg->tl_hba;
659 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
660 se_lun->unpacked_lun);
661 if (!sd) {
662 pr_err("Unable to locate struct scsi_device for %d:%d:%llu\n",
663 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
664 return;
667 * Remove Linux/SCSI struct scsi_device by HCTL
669 scsi_remove_device(sd);
670 scsi_device_put(sd);
672 atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
674 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
677 /* End items for tcm_loop_port_cit */
679 static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_show(
680 struct config_item *item, char *page)
682 struct se_portal_group *se_tpg = attrib_to_tpg(item);
683 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
684 tl_se_tpg);
686 return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type);
689 static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_store(
690 struct config_item *item, const char *page, size_t count)
692 struct se_portal_group *se_tpg = attrib_to_tpg(item);
693 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
694 tl_se_tpg);
695 unsigned long val;
696 int ret = kstrtoul(page, 0, &val);
698 if (ret) {
699 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
700 return ret;
702 if (val != 0 && val != 1 && val != 3) {
703 pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
704 return -EINVAL;
706 tl_tpg->tl_fabric_prot_type = val;
708 return count;
711 CONFIGFS_ATTR(tcm_loop_tpg_attrib_, fabric_prot_type);
713 static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = {
714 &tcm_loop_tpg_attrib_attr_fabric_prot_type,
715 NULL,
718 /* Start items for tcm_loop_nexus_cit */
720 static int tcm_loop_alloc_sess_cb(struct se_portal_group *se_tpg,
721 struct se_session *se_sess, void *p)
723 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
724 struct tcm_loop_tpg, tl_se_tpg);
726 tl_tpg->tl_nexus = p;
727 return 0;
730 static int tcm_loop_make_nexus(
731 struct tcm_loop_tpg *tl_tpg,
732 const char *name)
734 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
735 struct tcm_loop_nexus *tl_nexus;
736 int ret;
738 if (tl_tpg->tl_nexus) {
739 pr_debug("tl_tpg->tl_nexus already exists\n");
740 return -EEXIST;
743 tl_nexus = kzalloc(sizeof(*tl_nexus), GFP_KERNEL);
744 if (!tl_nexus)
745 return -ENOMEM;
747 tl_nexus->se_sess = target_setup_session(&tl_tpg->tl_se_tpg, 0, 0,
748 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS,
749 name, tl_nexus, tcm_loop_alloc_sess_cb);
750 if (IS_ERR(tl_nexus->se_sess)) {
751 ret = PTR_ERR(tl_nexus->se_sess);
752 kfree(tl_nexus);
753 return ret;
756 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated %s Initiator Port: %s\n",
757 tcm_loop_dump_proto_id(tl_hba), name);
758 return 0;
761 static int tcm_loop_drop_nexus(
762 struct tcm_loop_tpg *tpg)
764 struct se_session *se_sess;
765 struct tcm_loop_nexus *tl_nexus;
767 tl_nexus = tpg->tl_nexus;
768 if (!tl_nexus)
769 return -ENODEV;
771 se_sess = tl_nexus->se_sess;
772 if (!se_sess)
773 return -ENODEV;
775 if (atomic_read(&tpg->tl_tpg_port_count)) {
776 pr_err("Unable to remove TCM_Loop I_T Nexus with active TPG port count: %d\n",
777 atomic_read(&tpg->tl_tpg_port_count));
778 return -EPERM;
781 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated %s Initiator Port: %s\n",
782 tcm_loop_dump_proto_id(tpg->tl_hba),
783 tl_nexus->se_sess->se_node_acl->initiatorname);
785 * Release the SCSI I_T Nexus to the emulated Target Port
787 target_remove_session(se_sess);
788 tpg->tl_nexus = NULL;
789 kfree(tl_nexus);
790 return 0;
793 /* End items for tcm_loop_nexus_cit */
795 static ssize_t tcm_loop_tpg_nexus_show(struct config_item *item, char *page)
797 struct se_portal_group *se_tpg = to_tpg(item);
798 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
799 struct tcm_loop_tpg, tl_se_tpg);
800 struct tcm_loop_nexus *tl_nexus;
801 ssize_t ret;
803 tl_nexus = tl_tpg->tl_nexus;
804 if (!tl_nexus)
805 return -ENODEV;
807 ret = snprintf(page, PAGE_SIZE, "%s\n",
808 tl_nexus->se_sess->se_node_acl->initiatorname);
810 return ret;
813 static ssize_t tcm_loop_tpg_nexus_store(struct config_item *item,
814 const char *page, size_t count)
816 struct se_portal_group *se_tpg = to_tpg(item);
817 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
818 struct tcm_loop_tpg, tl_se_tpg);
819 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
820 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
821 int ret;
823 * Shutdown the active I_T nexus if 'NULL' is passed..
825 if (!strncmp(page, "NULL", 4)) {
826 ret = tcm_loop_drop_nexus(tl_tpg);
827 return (!ret) ? count : ret;
830 * Otherwise make sure the passed virtual Initiator port WWN matches
831 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
832 * tcm_loop_make_nexus()
834 if (strlen(page) >= TL_WWN_ADDR_LEN) {
835 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
836 page, TL_WWN_ADDR_LEN);
837 return -EINVAL;
839 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
841 ptr = strstr(i_port, "naa.");
842 if (ptr) {
843 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
844 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
845 i_port, tcm_loop_dump_proto_id(tl_hba));
846 return -EINVAL;
848 port_ptr = &i_port[0];
849 goto check_newline;
851 ptr = strstr(i_port, "fc.");
852 if (ptr) {
853 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
854 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
855 i_port, tcm_loop_dump_proto_id(tl_hba));
856 return -EINVAL;
858 port_ptr = &i_port[3]; /* Skip over "fc." */
859 goto check_newline;
861 ptr = strstr(i_port, "iqn.");
862 if (ptr) {
863 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
864 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
865 i_port, tcm_loop_dump_proto_id(tl_hba));
866 return -EINVAL;
868 port_ptr = &i_port[0];
869 goto check_newline;
871 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
872 i_port);
873 return -EINVAL;
875 * Clear any trailing newline for the NAA WWN
877 check_newline:
878 if (i_port[strlen(i_port)-1] == '\n')
879 i_port[strlen(i_port)-1] = '\0';
881 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
882 if (ret < 0)
883 return ret;
885 return count;
888 static ssize_t tcm_loop_tpg_transport_status_show(struct config_item *item,
889 char *page)
891 struct se_portal_group *se_tpg = to_tpg(item);
892 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
893 struct tcm_loop_tpg, tl_se_tpg);
894 const char *status = NULL;
895 ssize_t ret = -EINVAL;
897 switch (tl_tpg->tl_transport_status) {
898 case TCM_TRANSPORT_ONLINE:
899 status = "online";
900 break;
901 case TCM_TRANSPORT_OFFLINE:
902 status = "offline";
903 break;
904 default:
905 break;
908 if (status)
909 ret = snprintf(page, PAGE_SIZE, "%s\n", status);
911 return ret;
914 static ssize_t tcm_loop_tpg_transport_status_store(struct config_item *item,
915 const char *page, size_t count)
917 struct se_portal_group *se_tpg = to_tpg(item);
918 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
919 struct tcm_loop_tpg, tl_se_tpg);
921 if (!strncmp(page, "online", 6)) {
922 tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
923 return count;
925 if (!strncmp(page, "offline", 7)) {
926 tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
927 if (tl_tpg->tl_nexus) {
928 struct se_session *tl_sess = tl_tpg->tl_nexus->se_sess;
930 core_allocate_nexus_loss_ua(tl_sess->se_node_acl);
932 return count;
934 return -EINVAL;
937 static ssize_t tcm_loop_tpg_address_show(struct config_item *item,
938 char *page)
940 struct se_portal_group *se_tpg = to_tpg(item);
941 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
942 struct tcm_loop_tpg, tl_se_tpg);
943 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
945 return snprintf(page, PAGE_SIZE, "%d:0:%d\n",
946 tl_hba->sh->host_no, tl_tpg->tl_tpgt);
949 CONFIGFS_ATTR(tcm_loop_tpg_, nexus);
950 CONFIGFS_ATTR(tcm_loop_tpg_, transport_status);
951 CONFIGFS_ATTR_RO(tcm_loop_tpg_, address);
953 static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
954 &tcm_loop_tpg_attr_nexus,
955 &tcm_loop_tpg_attr_transport_status,
956 &tcm_loop_tpg_attr_address,
957 NULL,
960 /* Start items for tcm_loop_naa_cit */
962 static struct se_portal_group *tcm_loop_make_naa_tpg(struct se_wwn *wwn,
963 const char *name)
965 struct tcm_loop_hba *tl_hba = container_of(wwn,
966 struct tcm_loop_hba, tl_hba_wwn);
967 struct tcm_loop_tpg *tl_tpg;
968 int ret;
969 unsigned long tpgt;
971 if (strstr(name, "tpgt_") != name) {
972 pr_err("Unable to locate \"tpgt_#\" directory group\n");
973 return ERR_PTR(-EINVAL);
975 if (kstrtoul(name+5, 10, &tpgt))
976 return ERR_PTR(-EINVAL);
978 if (tpgt >= TL_TPGS_PER_HBA) {
979 pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA: %u\n",
980 tpgt, TL_TPGS_PER_HBA);
981 return ERR_PTR(-EINVAL);
983 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
984 tl_tpg->tl_hba = tl_hba;
985 tl_tpg->tl_tpgt = tpgt;
987 * Register the tl_tpg as a emulated TCM Target Endpoint
989 ret = core_tpg_register(wwn, &tl_tpg->tl_se_tpg, tl_hba->tl_proto_id);
990 if (ret < 0)
991 return ERR_PTR(-ENOMEM);
993 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s Target Port %s,t,0x%04lx\n",
994 tcm_loop_dump_proto_id(tl_hba),
995 config_item_name(&wwn->wwn_group.cg_item), tpgt);
996 return &tl_tpg->tl_se_tpg;
999 static void tcm_loop_drop_naa_tpg(
1000 struct se_portal_group *se_tpg)
1002 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1003 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1004 struct tcm_loop_tpg, tl_se_tpg);
1005 struct tcm_loop_hba *tl_hba;
1006 unsigned short tpgt;
1008 tl_hba = tl_tpg->tl_hba;
1009 tpgt = tl_tpg->tl_tpgt;
1011 * Release the I_T Nexus for the Virtual target link if present
1013 tcm_loop_drop_nexus(tl_tpg);
1015 * Deregister the tl_tpg as a emulated TCM Target Endpoint
1017 core_tpg_deregister(se_tpg);
1019 tl_tpg->tl_hba = NULL;
1020 tl_tpg->tl_tpgt = 0;
1022 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s Target Port %s,t,0x%04x\n",
1023 tcm_loop_dump_proto_id(tl_hba),
1024 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1027 /* End items for tcm_loop_naa_cit */
1029 /* Start items for tcm_loop_cit */
1031 static struct se_wwn *tcm_loop_make_scsi_hba(
1032 struct target_fabric_configfs *tf,
1033 struct config_group *group,
1034 const char *name)
1036 struct tcm_loop_hba *tl_hba;
1037 struct Scsi_Host *sh;
1038 char *ptr;
1039 int ret, off = 0;
1041 tl_hba = kzalloc(sizeof(*tl_hba), GFP_KERNEL);
1042 if (!tl_hba)
1043 return ERR_PTR(-ENOMEM);
1046 * Determine the emulated Protocol Identifier and Target Port Name
1047 * based on the incoming configfs directory name.
1049 ptr = strstr(name, "naa.");
1050 if (ptr) {
1051 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1052 goto check_len;
1054 ptr = strstr(name, "fc.");
1055 if (ptr) {
1056 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1057 off = 3; /* Skip over "fc." */
1058 goto check_len;
1060 ptr = strstr(name, "iqn.");
1061 if (!ptr) {
1062 pr_err("Unable to locate prefix for emulated Target Port: %s\n",
1063 name);
1064 ret = -EINVAL;
1065 goto out;
1067 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
1069 check_len:
1070 if (strlen(name) >= TL_WWN_ADDR_LEN) {
1071 pr_err("Emulated NAA %s Address: %s, exceeds max: %d\n",
1072 name, tcm_loop_dump_proto_id(tl_hba), TL_WWN_ADDR_LEN);
1073 ret = -EINVAL;
1074 goto out;
1076 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1079 * Call device_register(tl_hba->dev) to register the emulated
1080 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1081 * device_register() callbacks in tcm_loop_driver_probe()
1083 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1084 if (ret)
1085 goto out;
1087 sh = tl_hba->sh;
1088 tcm_loop_hba_no_cnt++;
1089 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
1090 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1091 return &tl_hba->tl_hba_wwn;
1092 out:
1093 kfree(tl_hba);
1094 return ERR_PTR(ret);
1097 static void tcm_loop_drop_scsi_hba(
1098 struct se_wwn *wwn)
1100 struct tcm_loop_hba *tl_hba = container_of(wwn,
1101 struct tcm_loop_hba, tl_hba_wwn);
1103 pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
1104 tcm_loop_dump_proto_id(tl_hba), tl_hba->tl_wwn_address,
1105 tl_hba->sh->host_no);
1107 * Call device_unregister() on the original tl_hba->dev.
1108 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1109 * release *tl_hba;
1111 device_unregister(&tl_hba->dev);
1114 /* Start items for tcm_loop_cit */
1115 static ssize_t tcm_loop_wwn_version_show(struct config_item *item, char *page)
1117 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1120 CONFIGFS_ATTR_RO(tcm_loop_wwn_, version);
1122 static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1123 &tcm_loop_wwn_attr_version,
1124 NULL,
1127 /* End items for tcm_loop_cit */
1129 static const struct target_core_fabric_ops loop_ops = {
1130 .module = THIS_MODULE,
1131 .fabric_name = "loopback",
1132 .tpg_get_wwn = tcm_loop_get_endpoint_wwn,
1133 .tpg_get_tag = tcm_loop_get_tag,
1134 .tpg_check_demo_mode = tcm_loop_check_demo_mode,
1135 .tpg_check_demo_mode_cache = tcm_loop_check_demo_mode_cache,
1136 .tpg_check_demo_mode_write_protect =
1137 tcm_loop_check_demo_mode_write_protect,
1138 .tpg_check_prod_mode_write_protect =
1139 tcm_loop_check_prod_mode_write_protect,
1140 .tpg_check_prot_fabric_only = tcm_loop_check_prot_fabric_only,
1141 .tpg_get_inst_index = tcm_loop_get_inst_index,
1142 .check_stop_free = tcm_loop_check_stop_free,
1143 .release_cmd = tcm_loop_release_cmd,
1144 .sess_get_index = tcm_loop_sess_get_index,
1145 .write_pending = tcm_loop_write_pending,
1146 .set_default_node_attributes = tcm_loop_set_default_node_attributes,
1147 .get_cmd_state = tcm_loop_get_cmd_state,
1148 .queue_data_in = tcm_loop_queue_data_in,
1149 .queue_status = tcm_loop_queue_status,
1150 .queue_tm_rsp = tcm_loop_queue_tm_rsp,
1151 .aborted_task = tcm_loop_aborted_task,
1152 .fabric_make_wwn = tcm_loop_make_scsi_hba,
1153 .fabric_drop_wwn = tcm_loop_drop_scsi_hba,
1154 .fabric_make_tpg = tcm_loop_make_naa_tpg,
1155 .fabric_drop_tpg = tcm_loop_drop_naa_tpg,
1156 .fabric_post_link = tcm_loop_port_link,
1157 .fabric_pre_unlink = tcm_loop_port_unlink,
1158 .tfc_wwn_attrs = tcm_loop_wwn_attrs,
1159 .tfc_tpg_base_attrs = tcm_loop_tpg_attrs,
1160 .tfc_tpg_attrib_attrs = tcm_loop_tpg_attrib_attrs,
1163 static int __init tcm_loop_fabric_init(void)
1165 int ret = -ENOMEM;
1167 tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
1168 if (!tcm_loop_workqueue)
1169 goto out;
1171 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1172 sizeof(struct tcm_loop_cmd),
1173 __alignof__(struct tcm_loop_cmd),
1174 0, NULL);
1175 if (!tcm_loop_cmd_cache) {
1176 pr_debug("kmem_cache_create() for tcm_loop_cmd_cache failed\n");
1177 goto out_destroy_workqueue;
1180 ret = tcm_loop_alloc_core_bus();
1181 if (ret)
1182 goto out_destroy_cache;
1184 ret = target_register_template(&loop_ops);
1185 if (ret)
1186 goto out_release_core_bus;
1188 return 0;
1190 out_release_core_bus:
1191 tcm_loop_release_core_bus();
1192 out_destroy_cache:
1193 kmem_cache_destroy(tcm_loop_cmd_cache);
1194 out_destroy_workqueue:
1195 destroy_workqueue(tcm_loop_workqueue);
1196 out:
1197 return ret;
1200 static void __exit tcm_loop_fabric_exit(void)
1202 target_unregister_template(&loop_ops);
1203 tcm_loop_release_core_bus();
1204 kmem_cache_destroy(tcm_loop_cmd_cache);
1205 destroy_workqueue(tcm_loop_workqueue);
1208 MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1209 MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1210 MODULE_LICENSE("GPL");
1211 module_init(tcm_loop_fabric_init);
1212 module_exit(tcm_loop_fabric_exit);