blob: e30932f989a1265523cd28a7d7d51a54f003d020 [file] [log] [blame]
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001/*******************************************************************************
2 *
3 * This file contains the Linux/SCSI LLD virtual SCSI initiator driver
4 * for emulated SAS initiator ports
5 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07006 * © Copyright 2011-2013 Datera, Inc.
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07007 *
8 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
9 *
10 * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 ****************************************************************************/
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/types.h>
28#include <linux/configfs.h>
29#include <scsi/scsi.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi_host.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_cmnd.h>
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070034
35#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050036#include <target/target_core_fabric.h>
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070037#include <target/target_core_fabric_configfs.h>
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070038#include <target/target_core_configfs.h>
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070039
40#include "tcm_loop.h"
41
42#define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev)
43
44/* Local pointer to allocated TCM configfs fabric module */
45static struct target_fabric_configfs *tcm_loop_fabric_configfs;
46
Christoph Hellwigafe2cb72012-02-02 17:04:41 -050047static struct workqueue_struct *tcm_loop_workqueue;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070048static struct kmem_cache *tcm_loop_cmd_cache;
49
50static int tcm_loop_hba_no_cnt;
51
Christoph Hellwig167864542012-02-02 17:04:42 -050052static int tcm_loop_queue_status(struct se_cmd *se_cmd);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070053
54/*
55 * Called from struct target_core_fabric_ops->check_stop_free()
56 */
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -070057static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070058{
59 /*
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().
63 */
Andy Groverc8e31f22012-01-19 13:39:17 -080064 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -070065 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070066 /*
67 * Release the struct se_cmd, which will make a callback to release
68 * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd()
69 */
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +020070 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -070071 return 1;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070072}
73
Christoph Hellwig35462972011-05-31 23:56:57 -040074static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070075{
76 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
77 struct tcm_loop_cmd, tl_se_cmd);
78
79 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
80}
81
Al Viro8946b072013-03-31 02:01:55 -040082static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070083{
Al Viro8946b072013-03-31 02:01:55 -040084 seq_printf(m, "tcm_loop_proc_info()\n");
85 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -070086}
87
88static int tcm_loop_driver_probe(struct device *);
89static int tcm_loop_driver_remove(struct device *);
90
91static int pseudo_lld_bus_match(struct device *dev,
92 struct device_driver *dev_driver)
93{
94 return 1;
95}
96
97static 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,
102};
103
104static struct device_driver tcm_loop_driverfs = {
105 .name = "tcm_loop",
106 .bus = &tcm_loop_lld_bus,
107};
108/*
109 * Used with root_device_register() in tcm_loop_alloc_core_bus() below
110 */
111struct device *tcm_loop_primary;
112
113/*
114 * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and
115 * drivers/scsi/libiscsi.c:iscsi_change_queue_depth()
116 */
117static int tcm_loop_change_queue_depth(
118 struct scsi_device *sdev,
119 int depth,
120 int reason)
121{
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;
134 }
135 return sdev->queue_depth;
136}
137
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500138static void tcm_loop_submission_work(struct work_struct *work)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700139{
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500140 struct tcm_loop_cmd *tl_cmd =
141 container_of(work, struct tcm_loop_cmd, work);
142 struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
143 struct scsi_cmnd *sc = tl_cmd->sc;
144 struct tcm_loop_nexus *tl_nexus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700145 struct tcm_loop_hba *tl_hba;
146 struct tcm_loop_tpg *tl_tpg;
Christoph Hellwig167864542012-02-02 17:04:42 -0500147 struct scatterlist *sgl_bidi = NULL;
Sagi Grimberge2a4f552014-06-11 12:09:59 +0300148 u32 sgl_bidi_count = 0, transfer_length;
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700149 int rc;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500150
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700151 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
152 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500153
Nicholas Bellinger0a020432011-10-10 19:44:05 -0700154 /*
155 * Ensure that this tl_tpg reference from the incoming sc->device->id
156 * has already been configured via tcm_loop_make_naa_tpg().
157 */
158 if (!tl_tpg->tl_hba) {
159 set_host_byte(sc, DID_NO_CONNECT);
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500160 goto out_done;
Nicholas Bellinger0a020432011-10-10 19:44:05 -0700161 }
Hannes Reineckefb2b2842013-10-16 09:12:53 +0200162 if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) {
163 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
164 goto out_done;
165 }
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500166 tl_nexus = tl_hba->tl_nexus;
167 if (!tl_nexus) {
168 scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
169 " does not exist\n");
170 set_host_byte(sc, DID_ERROR);
171 goto out_done;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700172 }
Christoph Hellwig167864542012-02-02 17:04:42 -0500173 if (scsi_bidi_cmnd(sc)) {
174 struct scsi_data_buffer *sdb = scsi_in(sc);
175
176 sgl_bidi = sdb->table.sgl;
177 sgl_bidi_count = sdb->table.nents;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500178 se_cmd->se_cmd_flags |= SCF_BIDI;
179
Christoph Hellwig167864542012-02-02 17:04:42 -0500180 }
Sagi Grimbergb5b8e292014-02-19 17:50:17 +0200181
Sagi Grimberge2a4f552014-06-11 12:09:59 +0300182 transfer_length = scsi_transfer_length(sc);
183 if (!scsi_prot_sg_count(sc) &&
184 scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) {
Sagi Grimbergb5b8e292014-02-19 17:50:17 +0200185 se_cmd->prot_pto = true;
Sagi Grimberge2a4f552014-06-11 12:09:59 +0300186 /*
187 * loopback transport doesn't support
188 * WRITE_GENERATE, READ_STRIP protection
189 * information operations, go ahead unprotected.
190 */
191 transfer_length = scsi_bufflen(sc);
192 }
Sagi Grimbergb5b8e292014-02-19 17:50:17 +0200193
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700194 rc = target_submit_cmd_map_sgls(se_cmd, tl_nexus->se_sess, sc->cmnd,
195 &tl_cmd->tl_sense_buf[0], tl_cmd->sc->device->lun,
Christoph Hellwig609aa222014-10-30 11:54:58 +0100196 transfer_length, MSG_SIMPLE_TAG,
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700197 sc->sc_data_direction, 0,
198 scsi_sglist(sc), scsi_sg_count(sc),
Nicholas Bellinger59dedde2013-12-23 20:39:23 +0000199 sgl_bidi, sgl_bidi_count,
200 scsi_prot_sglist(sc), scsi_prot_sg_count(sc));
Nicholas Bellinger8f9f44f2012-10-01 23:29:49 -0700201 if (rc < 0) {
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500202 set_host_byte(sc, DID_NO_CONNECT);
203 goto out_done;
204 }
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500205 return;
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500206
207out_done:
Nicholas Bellingerb43f1882014-06-17 22:23:03 +0000208 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500209 sc->scsi_done(sc);
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500210 return;
211}
212
213/*
214 * ->queuecommand can be and usually is called from interrupt context, so
215 * defer the actual submission to a workqueue.
216 */
217static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
218{
219 struct tcm_loop_cmd *tl_cmd;
220
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200221 pr_debug("tcm_loop_queuecommand() %d:%d:%d:%llu got CDB: 0x%02x"
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500222 " scsi_buf_len: %u\n", sc->device->host->host_no,
223 sc->device->id, sc->device->channel, sc->device->lun,
224 sc->cmnd[0], scsi_bufflen(sc));
225
226 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
227 if (!tl_cmd) {
228 pr_err("Unable to allocate struct tcm_loop_cmd\n");
229 set_host_byte(sc, DID_ERROR);
230 sc->scsi_done(sc);
231 return 0;
232 }
233
234 tl_cmd->sc = sc;
Hannes Reinecke6375f892014-10-02 09:30:55 +0200235 tl_cmd->sc_cmd_tag = sc->request->tag;
Christoph Hellwigafe2cb72012-02-02 17:04:41 -0500236 INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
237 queue_work(tcm_loop_workqueue, &tl_cmd->work);
Christoph Hellwigf872c9f2012-02-02 17:04:40 -0500238 return 0;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700239}
240
241/*
242 * Called from SCSI EH process context to issue a LUN_RESET TMR
243 * to struct scsi_device
244 */
Hannes Reineckea314d702013-10-16 09:12:54 +0200245static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg,
246 struct tcm_loop_nexus *tl_nexus,
Hannes Reinecke969871c2013-10-16 09:12:55 +0200247 int lun, int task, enum tcm_tmreq_table tmr)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700248{
249 struct se_cmd *se_cmd = NULL;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700250 struct se_session *se_sess;
Hannes Reineckea314d702013-10-16 09:12:54 +0200251 struct se_portal_group *se_tpg;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700252 struct tcm_loop_cmd *tl_cmd = NULL;
Hannes Reineckea314d702013-10-16 09:12:54 +0200253 struct tcm_loop_tmr *tl_tmr = NULL;
254 int ret = TMR_FUNCTION_FAILED, rc;
255
256 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
257 if (!tl_cmd) {
258 pr_err("Unable to allocate memory for tl_cmd\n");
259 return ret;
260 }
261
262 tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
263 if (!tl_tmr) {
264 pr_err("Unable to allocate memory for tl_tmr\n");
265 goto release;
266 }
267 init_waitqueue_head(&tl_tmr->tl_tmr_wait);
268
269 se_cmd = &tl_cmd->tl_se_cmd;
270 se_tpg = &tl_tpg->tl_se_tpg;
271 se_sess = tl_nexus->se_sess;
272 /*
273 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
274 */
275 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
276 DMA_NONE, MSG_SIMPLE_TAG,
277 &tl_cmd->tl_sense_buf[0]);
278
279 rc = core_tmr_alloc_req(se_cmd, tl_tmr, tmr, GFP_KERNEL);
280 if (rc < 0)
281 goto release;
282
Hannes Reinecke969871c2013-10-16 09:12:55 +0200283 if (tmr == TMR_ABORT_TASK)
284 se_cmd->se_tmr_req->ref_task_tag = task;
285
Hannes Reineckea314d702013-10-16 09:12:54 +0200286 /*
287 * Locate the underlying TCM struct se_lun
288 */
289 if (transport_lookup_tmr_lun(se_cmd, lun) < 0) {
290 ret = TMR_LUN_DOES_NOT_EXIST;
291 goto release;
292 }
293 /*
294 * Queue the TMR to TCM Core and sleep waiting for
295 * tcm_loop_queue_tm_rsp() to wake us up.
296 */
297 transport_generic_handle_tmr(se_cmd);
298 wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
299 /*
300 * The TMR LUN_RESET has completed, check the response status and
301 * then release allocations.
302 */
303 ret = se_cmd->se_tmr_req->response;
304release:
305 if (se_cmd)
306 transport_generic_free_cmd(se_cmd, 1);
307 else
308 kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
309 kfree(tl_tmr);
310 return ret;
311}
312
Hannes Reinecke969871c2013-10-16 09:12:55 +0200313static int tcm_loop_abort_task(struct scsi_cmnd *sc)
314{
315 struct tcm_loop_hba *tl_hba;
316 struct tcm_loop_nexus *tl_nexus;
317 struct tcm_loop_tpg *tl_tpg;
318 int ret = FAILED;
319
320 /*
321 * Locate the tcm_loop_hba_t pointer
322 */
323 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
324 /*
325 * Locate the tl_nexus and se_sess pointers
326 */
327 tl_nexus = tl_hba->tl_nexus;
328 if (!tl_nexus) {
329 pr_err("Unable to perform device reset without"
330 " active I_T Nexus\n");
331 return FAILED;
332 }
333
334 /*
335 * Locate the tl_tpg pointer from TargetID in sc->device->id
336 */
337 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
338 ret = tcm_loop_issue_tmr(tl_tpg, tl_nexus, sc->device->lun,
Hannes Reinecke6375f892014-10-02 09:30:55 +0200339 sc->request->tag, TMR_ABORT_TASK);
Hannes Reinecke969871c2013-10-16 09:12:55 +0200340 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
341}
342
Hannes Reineckea314d702013-10-16 09:12:54 +0200343/*
344 * Called from SCSI EH process context to issue a LUN_RESET TMR
345 * to struct scsi_device
346 */
347static int tcm_loop_device_reset(struct scsi_cmnd *sc)
348{
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700349 struct tcm_loop_hba *tl_hba;
350 struct tcm_loop_nexus *tl_nexus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700351 struct tcm_loop_tpg *tl_tpg;
Hannes Reineckea314d702013-10-16 09:12:54 +0200352 int ret = FAILED;
353
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700354 /*
355 * Locate the tcm_loop_hba_t pointer
356 */
357 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
358 /*
359 * Locate the tl_nexus and se_sess pointers
360 */
361 tl_nexus = tl_hba->tl_nexus;
362 if (!tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700363 pr_err("Unable to perform device reset without"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700364 " active I_T Nexus\n");
365 return FAILED;
366 }
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700367 /*
Hannes Reineckea314d702013-10-16 09:12:54 +0200368 * Locate the tl_tpg pointer from TargetID in sc->device->id
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700369 */
370 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
Hannes Reinecke969871c2013-10-16 09:12:55 +0200371 ret = tcm_loop_issue_tmr(tl_tpg, tl_nexus, sc->device->lun,
372 0, TMR_LUN_RESET);
Hannes Reineckea314d702013-10-16 09:12:54 +0200373 return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700374}
375
Hannes Reinecke8f4a1fb2013-10-16 09:12:56 +0200376static int tcm_loop_target_reset(struct scsi_cmnd *sc)
377{
378 struct tcm_loop_hba *tl_hba;
379 struct tcm_loop_tpg *tl_tpg;
380
381 /*
382 * Locate the tcm_loop_hba_t pointer
383 */
384 tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
385 if (!tl_hba) {
386 pr_err("Unable to perform device reset without"
387 " active I_T Nexus\n");
388 return FAILED;
389 }
390 /*
391 * Locate the tl_tpg pointer from TargetID in sc->device->id
392 */
393 tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
394 if (tl_tpg) {
395 tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
396 return SUCCESS;
397 }
398 return FAILED;
399}
400
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700401static int tcm_loop_slave_alloc(struct scsi_device *sd)
402{
403 set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
404 return 0;
405}
406
407static int tcm_loop_slave_configure(struct scsi_device *sd)
408{
Hannes Reinecke969871c2013-10-16 09:12:55 +0200409 if (sd->tagged_supported) {
410 scsi_activate_tcq(sd, sd->queue_depth);
411 scsi_adjust_queue_depth(sd, MSG_SIMPLE_TAG,
412 sd->host->cmd_per_lun);
413 } else {
414 scsi_adjust_queue_depth(sd, 0,
415 sd->host->cmd_per_lun);
416 }
417
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700418 return 0;
419}
420
421static struct scsi_host_template tcm_loop_driver_template = {
Al Viro8946b072013-03-31 02:01:55 -0400422 .show_info = tcm_loop_show_info,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700423 .proc_name = "tcm_loopback",
424 .name = "TCM_Loopback",
425 .queuecommand = tcm_loop_queuecommand,
426 .change_queue_depth = tcm_loop_change_queue_depth,
Christoph Hellwiga62182f2014-10-02 14:39:55 +0200427 .change_queue_type = scsi_change_queue_type,
Hannes Reinecke969871c2013-10-16 09:12:55 +0200428 .eh_abort_handler = tcm_loop_abort_task,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700429 .eh_device_reset_handler = tcm_loop_device_reset,
Hannes Reinecke8f4a1fb2013-10-16 09:12:56 +0200430 .eh_target_reset_handler = tcm_loop_target_reset,
Christoph Hellwig2e88efd2011-11-29 03:20:41 -0500431 .can_queue = 1024,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700432 .this_id = -1,
Christoph Hellwig2e88efd2011-11-29 03:20:41 -0500433 .sg_tablesize = 256,
434 .cmd_per_lun = 1024,
435 .max_sectors = 0xFFFF,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700436 .use_clustering = DISABLE_CLUSTERING,
437 .slave_alloc = tcm_loop_slave_alloc,
438 .slave_configure = tcm_loop_slave_configure,
439 .module = THIS_MODULE,
440};
441
442static int tcm_loop_driver_probe(struct device *dev)
443{
444 struct tcm_loop_hba *tl_hba;
445 struct Scsi_Host *sh;
Nicholas Bellinger59dedde2013-12-23 20:39:23 +0000446 int error, host_prot;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700447
448 tl_hba = to_tcm_loop_hba(dev);
449
450 sh = scsi_host_alloc(&tcm_loop_driver_template,
451 sizeof(struct tcm_loop_hba));
452 if (!sh) {
Andy Grover6708bb22011-06-08 10:36:43 -0700453 pr_err("Unable to allocate struct scsi_host\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700454 return -ENODEV;
455 }
456 tl_hba->sh = sh;
457
458 /*
459 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
460 */
461 *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
462 /*
463 * Setup single ID, Channel and LUN for now..
464 */
465 sh->max_id = 2;
466 sh->max_lun = 0;
467 sh->max_channel = 0;
468 sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN;
469
Nicholas Bellinger59dedde2013-12-23 20:39:23 +0000470 host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
471 SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
472 SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
473
474 scsi_host_set_prot(sh, host_prot);
475 scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC);
476
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700477 error = scsi_add_host(sh, &tl_hba->dev);
478 if (error) {
Andy Grover6708bb22011-06-08 10:36:43 -0700479 pr_err("%s: scsi_add_host failed\n", __func__);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700480 scsi_host_put(sh);
481 return -ENODEV;
482 }
483 return 0;
484}
485
486static int tcm_loop_driver_remove(struct device *dev)
487{
488 struct tcm_loop_hba *tl_hba;
489 struct Scsi_Host *sh;
490
491 tl_hba = to_tcm_loop_hba(dev);
492 sh = tl_hba->sh;
493
494 scsi_remove_host(sh);
495 scsi_host_put(sh);
496 return 0;
497}
498
499static void tcm_loop_release_adapter(struct device *dev)
500{
501 struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
502
503 kfree(tl_hba);
504}
505
506/*
507 * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
508 */
509static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
510{
511 int ret;
512
513 tl_hba->dev.bus = &tcm_loop_lld_bus;
514 tl_hba->dev.parent = tcm_loop_primary;
515 tl_hba->dev.release = &tcm_loop_release_adapter;
516 dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
517
518 ret = device_register(&tl_hba->dev);
519 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700520 pr_err("device_register() failed for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700521 " tl_hba->dev: %d\n", ret);
522 return -ENODEV;
523 }
524
525 return 0;
526}
527
528/*
529 * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
530 * tcm_loop SCSI bus.
531 */
532static int tcm_loop_alloc_core_bus(void)
533{
534 int ret;
535
536 tcm_loop_primary = root_device_register("tcm_loop_0");
537 if (IS_ERR(tcm_loop_primary)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700538 pr_err("Unable to allocate tcm_loop_primary\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700539 return PTR_ERR(tcm_loop_primary);
540 }
541
542 ret = bus_register(&tcm_loop_lld_bus);
543 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700544 pr_err("bus_register() failed for tcm_loop_lld_bus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700545 goto dev_unreg;
546 }
547
548 ret = driver_register(&tcm_loop_driverfs);
549 if (ret) {
Andy Grover6708bb22011-06-08 10:36:43 -0700550 pr_err("driver_register() failed for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700551 "tcm_loop_driverfs\n");
552 goto bus_unreg;
553 }
554
Andy Grover6708bb22011-06-08 10:36:43 -0700555 pr_debug("Initialized TCM Loop Core Bus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700556 return ret;
557
558bus_unreg:
559 bus_unregister(&tcm_loop_lld_bus);
560dev_unreg:
561 root_device_unregister(tcm_loop_primary);
562 return ret;
563}
564
565static void tcm_loop_release_core_bus(void)
566{
567 driver_unregister(&tcm_loop_driverfs);
568 bus_unregister(&tcm_loop_lld_bus);
569 root_device_unregister(tcm_loop_primary);
570
Andy Grover6708bb22011-06-08 10:36:43 -0700571 pr_debug("Releasing TCM Loop Core BUS\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700572}
573
574static char *tcm_loop_get_fabric_name(void)
575{
576 return "loopback";
577}
578
579static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
580{
Jörn Engel8359cf42011-11-24 02:05:51 +0100581 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700582 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
583 /*
584 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
585 * time based on the protocol dependent prefix of the passed configfs group.
586 *
587 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
588 * ProtocolID using target_core_fabric_lib.c symbols.
589 */
590 switch (tl_hba->tl_proto_id) {
591 case SCSI_PROTOCOL_SAS:
592 return sas_get_fabric_proto_ident(se_tpg);
593 case SCSI_PROTOCOL_FCP:
594 return fc_get_fabric_proto_ident(se_tpg);
595 case SCSI_PROTOCOL_ISCSI:
596 return iscsi_get_fabric_proto_ident(se_tpg);
597 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700598 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700599 " SAS emulation\n", tl_hba->tl_proto_id);
600 break;
601 }
602
603 return sas_get_fabric_proto_ident(se_tpg);
604}
605
606static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
607{
Jörn Engel8359cf42011-11-24 02:05:51 +0100608 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700609 /*
610 * Return the passed NAA identifier for the SAS Target Port
611 */
612 return &tl_tpg->tl_hba->tl_wwn_address[0];
613}
614
615static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
616{
Jörn Engel8359cf42011-11-24 02:05:51 +0100617 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700618 /*
619 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
620 * to represent the SCSI Target Port.
621 */
622 return tl_tpg->tl_tpgt;
623}
624
625static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
626{
627 return 1;
628}
629
630static u32 tcm_loop_get_pr_transport_id(
631 struct se_portal_group *se_tpg,
632 struct se_node_acl *se_nacl,
633 struct t10_pr_registration *pr_reg,
634 int *format_code,
635 unsigned char *buf)
636{
Jörn Engel8359cf42011-11-24 02:05:51 +0100637 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700638 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
639
640 switch (tl_hba->tl_proto_id) {
641 case SCSI_PROTOCOL_SAS:
642 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
643 format_code, buf);
644 case SCSI_PROTOCOL_FCP:
645 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
646 format_code, buf);
647 case SCSI_PROTOCOL_ISCSI:
648 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
649 format_code, buf);
650 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700651 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700652 " SAS emulation\n", tl_hba->tl_proto_id);
653 break;
654 }
655
656 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
657 format_code, buf);
658}
659
660static u32 tcm_loop_get_pr_transport_id_len(
661 struct se_portal_group *se_tpg,
662 struct se_node_acl *se_nacl,
663 struct t10_pr_registration *pr_reg,
664 int *format_code)
665{
Jörn Engel8359cf42011-11-24 02:05:51 +0100666 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700667 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
668
669 switch (tl_hba->tl_proto_id) {
670 case SCSI_PROTOCOL_SAS:
671 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
672 format_code);
673 case SCSI_PROTOCOL_FCP:
674 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
675 format_code);
676 case SCSI_PROTOCOL_ISCSI:
677 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
678 format_code);
679 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700680 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700681 " SAS emulation\n", tl_hba->tl_proto_id);
682 break;
683 }
684
685 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
686 format_code);
687}
688
689/*
690 * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
691 * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
692 */
693static char *tcm_loop_parse_pr_out_transport_id(
694 struct se_portal_group *se_tpg,
695 const char *buf,
696 u32 *out_tid_len,
697 char **port_nexus_ptr)
698{
Jörn Engel8359cf42011-11-24 02:05:51 +0100699 struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700700 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
701
702 switch (tl_hba->tl_proto_id) {
703 case SCSI_PROTOCOL_SAS:
704 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
705 port_nexus_ptr);
706 case SCSI_PROTOCOL_FCP:
707 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
708 port_nexus_ptr);
709 case SCSI_PROTOCOL_ISCSI:
710 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
711 port_nexus_ptr);
712 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700713 pr_err("Unknown tl_proto_id: 0x%02x, using"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700714 " SAS emulation\n", tl_hba->tl_proto_id);
715 break;
716 }
717
718 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
719 port_nexus_ptr);
720}
721
722/*
723 * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
724 * based upon the incoming fabric dependent SCSI Initiator Port
725 */
726static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
727{
728 return 1;
729}
730
731static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
732{
733 return 0;
734}
735
736/*
737 * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
738 * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
739 */
740static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
741{
742 return 0;
743}
744
745/*
746 * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
747 * never be called for TCM_Loop by target_core_fabric_configfs.c code.
748 * It has been added here as a nop for target_fabric_tf_ops_check()
749 */
750static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
751{
752 return 0;
753}
754
755static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
756 struct se_portal_group *se_tpg)
757{
758 struct tcm_loop_nacl *tl_nacl;
759
760 tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
761 if (!tl_nacl) {
Andy Grover6708bb22011-06-08 10:36:43 -0700762 pr_err("Unable to allocate struct tcm_loop_nacl\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700763 return NULL;
764 }
765
766 return &tl_nacl->se_node_acl;
767}
768
769static void tcm_loop_tpg_release_fabric_acl(
770 struct se_portal_group *se_tpg,
771 struct se_node_acl *se_nacl)
772{
773 struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
774 struct tcm_loop_nacl, se_node_acl);
775
776 kfree(tl_nacl);
777}
778
779static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
780{
781 return 1;
782}
783
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700784static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
785{
786 return 1;
787}
788
789static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
790{
791 return;
792}
793
794static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
795{
Hannes Reinecke969871c2013-10-16 09:12:55 +0200796 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
797 struct tcm_loop_cmd, tl_se_cmd);
798
799 return tl_cmd->sc_cmd_tag;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700800}
801
802static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
803{
804 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
805 struct tcm_loop_cmd, tl_se_cmd);
806
807 return tl_cmd->sc_cmd_state;
808}
809
810static int tcm_loop_shutdown_session(struct se_session *se_sess)
811{
812 return 0;
813}
814
815static void tcm_loop_close_session(struct se_session *se_sess)
816{
817 return;
818};
819
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700820static int tcm_loop_write_pending(struct se_cmd *se_cmd)
821{
822 /*
823 * Since Linux/SCSI has already sent down a struct scsi_cmnd
824 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
825 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
826 * format with transport_generic_map_mem_to_cmd().
827 *
828 * We now tell TCM to add this WRITE CDB directly into the TCM storage
829 * object execution queue.
830 */
Christoph Hellwig70baf0a2012-07-08 15:58:39 -0400831 target_execute_cmd(se_cmd);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700832 return 0;
833}
834
835static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
836{
837 return 0;
838}
839
840static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
841{
842 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
843 struct tcm_loop_cmd, tl_se_cmd);
844 struct scsi_cmnd *sc = tl_cmd->sc;
845
Andy Grover6708bb22011-06-08 10:36:43 -0700846 pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700847 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
848
849 sc->result = SAM_STAT_GOOD;
850 set_host_byte(sc, DID_OK);
Roland Dreier6cf3fa62012-02-14 15:30:31 -0800851 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
852 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
853 scsi_set_resid(sc, se_cmd->residual_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700854 sc->scsi_done(sc);
855 return 0;
856}
857
858static int tcm_loop_queue_status(struct se_cmd *se_cmd)
859{
860 struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
861 struct tcm_loop_cmd, tl_se_cmd);
862 struct scsi_cmnd *sc = tl_cmd->sc;
863
Andy Grover6708bb22011-06-08 10:36:43 -0700864 pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700865 " cdb: 0x%02x\n", sc, sc->cmnd[0]);
866
867 if (se_cmd->sense_buffer &&
868 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
869 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
870
Andy Grover5951146d2011-07-19 10:26:37 +0000871 memcpy(sc->sense_buffer, se_cmd->sense_buffer,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700872 SCSI_SENSE_BUFFERSIZE);
873 sc->result = SAM_STAT_CHECK_CONDITION;
874 set_driver_byte(sc, DRIVER_SENSE);
875 } else
876 sc->result = se_cmd->scsi_status;
877
878 set_host_byte(sc, DID_OK);
Roland Dreier6cf3fa62012-02-14 15:30:31 -0800879 if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
880 (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
881 scsi_set_resid(sc, se_cmd->residual_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700882 sc->scsi_done(sc);
883 return 0;
884}
885
Joern Engelb79fafa2013-07-03 11:22:17 -0400886static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700887{
888 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
889 struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
890 /*
891 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
892 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
893 */
894 atomic_set(&tl_tmr->tmr_complete, 1);
895 wake_up(&tl_tmr->tl_tmr_wait);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700896}
897
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700898static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
899{
900 return;
901}
902
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700903static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
904{
905 switch (tl_hba->tl_proto_id) {
906 case SCSI_PROTOCOL_SAS:
907 return "SAS";
908 case SCSI_PROTOCOL_FCP:
909 return "FCP";
910 case SCSI_PROTOCOL_ISCSI:
911 return "iSCSI";
912 default:
913 break;
914 }
915
916 return "Unknown";
917}
918
919/* Start items for tcm_loop_port_cit */
920
921static int tcm_loop_port_link(
922 struct se_portal_group *se_tpg,
923 struct se_lun *lun)
924{
925 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
926 struct tcm_loop_tpg, tl_se_tpg);
927 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
928
Joern Engel33940d02014-09-16 16:23:12 -0400929 atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700930 /*
931 * Add Linux/SCSI struct scsi_device by HCTL
932 */
933 scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
934
Andy Grover6708bb22011-06-08 10:36:43 -0700935 pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700936 return 0;
937}
938
939static void tcm_loop_port_unlink(
940 struct se_portal_group *se_tpg,
941 struct se_lun *se_lun)
942{
943 struct scsi_device *sd;
944 struct tcm_loop_hba *tl_hba;
945 struct tcm_loop_tpg *tl_tpg;
946
947 tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
948 tl_hba = tl_tpg->tl_hba;
949
950 sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
951 se_lun->unpacked_lun);
952 if (!sd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700953 pr_err("Unable to locate struct scsi_device for %d:%d:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700954 "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
955 return;
956 }
957 /*
958 * Remove Linux/SCSI struct scsi_device by HCTL
959 */
960 scsi_remove_device(sd);
961 scsi_device_put(sd);
962
Joern Engel33940d02014-09-16 16:23:12 -0400963 atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700964
Andy Grover6708bb22011-06-08 10:36:43 -0700965 pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700966}
967
968/* End items for tcm_loop_port_cit */
969
970/* Start items for tcm_loop_nexus_cit */
971
972static int tcm_loop_make_nexus(
973 struct tcm_loop_tpg *tl_tpg,
974 const char *name)
975{
976 struct se_portal_group *se_tpg;
977 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
978 struct tcm_loop_nexus *tl_nexus;
Dan Carpenter552523d2011-06-15 09:41:33 -0700979 int ret = -ENOMEM;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700980
981 if (tl_tpg->tl_hba->tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700982 pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700983 return -EEXIST;
984 }
985 se_tpg = &tl_tpg->tl_se_tpg;
986
987 tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
988 if (!tl_nexus) {
Andy Grover6708bb22011-06-08 10:36:43 -0700989 pr_err("Unable to allocate struct tcm_loop_nexus\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700990 return -ENOMEM;
991 }
992 /*
993 * Initialize the struct se_session pointer
994 */
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700995 tl_nexus->se_sess = transport_init_session(TARGET_PROT_ALL);
Dan Carpenter552523d2011-06-15 09:41:33 -0700996 if (IS_ERR(tl_nexus->se_sess)) {
997 ret = PTR_ERR(tl_nexus->se_sess);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -0700998 goto out;
Dan Carpenter552523d2011-06-15 09:41:33 -0700999 }
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001000 /*
1001 * Since we are running in 'demo mode' this call with generate a
1002 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
1003 * Initiator port name of the passed configfs group 'name'.
1004 */
1005 tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1006 se_tpg, (unsigned char *)name);
1007 if (!tl_nexus->se_sess->se_node_acl) {
1008 transport_free_session(tl_nexus->se_sess);
1009 goto out;
1010 }
1011 /*
1012 * Now, register the SAS I_T Nexus as active with the call to
1013 * transport_register_session()
1014 */
1015 __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
Andy Grover5951146d2011-07-19 10:26:37 +00001016 tl_nexus->se_sess, tl_nexus);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001017 tl_tpg->tl_hba->tl_nexus = tl_nexus;
Andy Grover6708bb22011-06-08 10:36:43 -07001018 pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001019 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
1020 name);
1021 return 0;
1022
1023out:
1024 kfree(tl_nexus);
Dan Carpenter552523d2011-06-15 09:41:33 -07001025 return ret;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001026}
1027
1028static int tcm_loop_drop_nexus(
1029 struct tcm_loop_tpg *tpg)
1030{
1031 struct se_session *se_sess;
1032 struct tcm_loop_nexus *tl_nexus;
1033 struct tcm_loop_hba *tl_hba = tpg->tl_hba;
1034
Hannes Reinecke1ec59fe2013-10-16 09:12:52 +02001035 if (!tl_hba)
1036 return -ENODEV;
1037
1038 tl_nexus = tl_hba->tl_nexus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001039 if (!tl_nexus)
1040 return -ENODEV;
1041
1042 se_sess = tl_nexus->se_sess;
1043 if (!se_sess)
1044 return -ENODEV;
1045
1046 if (atomic_read(&tpg->tl_tpg_port_count)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001047 pr_err("Unable to remove TCM_Loop I_T Nexus with"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001048 " active TPG port count: %d\n",
1049 atomic_read(&tpg->tl_tpg_port_count));
1050 return -EPERM;
1051 }
1052
Andy Grover6708bb22011-06-08 10:36:43 -07001053 pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001054 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
1055 tl_nexus->se_sess->se_node_acl->initiatorname);
1056 /*
1057 * Release the SCSI I_T Nexus to the emulated SAS Target Port
1058 */
1059 transport_deregister_session(tl_nexus->se_sess);
1060 tpg->tl_hba->tl_nexus = NULL;
1061 kfree(tl_nexus);
1062 return 0;
1063}
1064
1065/* End items for tcm_loop_nexus_cit */
1066
1067static ssize_t tcm_loop_tpg_show_nexus(
1068 struct se_portal_group *se_tpg,
1069 char *page)
1070{
1071 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1072 struct tcm_loop_tpg, tl_se_tpg);
1073 struct tcm_loop_nexus *tl_nexus;
1074 ssize_t ret;
1075
1076 tl_nexus = tl_tpg->tl_hba->tl_nexus;
1077 if (!tl_nexus)
1078 return -ENODEV;
1079
1080 ret = snprintf(page, PAGE_SIZE, "%s\n",
1081 tl_nexus->se_sess->se_node_acl->initiatorname);
1082
1083 return ret;
1084}
1085
1086static ssize_t tcm_loop_tpg_store_nexus(
1087 struct se_portal_group *se_tpg,
1088 const char *page,
1089 size_t count)
1090{
1091 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1092 struct tcm_loop_tpg, tl_se_tpg);
1093 struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
1094 unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
1095 int ret;
1096 /*
1097 * Shutdown the active I_T nexus if 'NULL' is passed..
1098 */
1099 if (!strncmp(page, "NULL", 4)) {
1100 ret = tcm_loop_drop_nexus(tl_tpg);
1101 return (!ret) ? count : ret;
1102 }
1103 /*
1104 * Otherwise make sure the passed virtual Initiator port WWN matches
1105 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
1106 * tcm_loop_make_nexus()
1107 */
Dan Carpenter60d645a2011-06-15 10:03:05 -07001108 if (strlen(page) >= TL_WWN_ADDR_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001109 pr_err("Emulated NAA Sas Address: %s, exceeds"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001110 " max: %d\n", page, TL_WWN_ADDR_LEN);
1111 return -EINVAL;
1112 }
1113 snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
1114
1115 ptr = strstr(i_port, "naa.");
1116 if (ptr) {
1117 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
Andy Grover6708bb22011-06-08 10:36:43 -07001118 pr_err("Passed SAS Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001119 " match target port protoid: %s\n", i_port,
1120 tcm_loop_dump_proto_id(tl_hba));
1121 return -EINVAL;
1122 }
1123 port_ptr = &i_port[0];
1124 goto check_newline;
1125 }
1126 ptr = strstr(i_port, "fc.");
1127 if (ptr) {
1128 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
Andy Grover6708bb22011-06-08 10:36:43 -07001129 pr_err("Passed FCP Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001130 " match target port protoid: %s\n", i_port,
1131 tcm_loop_dump_proto_id(tl_hba));
1132 return -EINVAL;
1133 }
1134 port_ptr = &i_port[3]; /* Skip over "fc." */
1135 goto check_newline;
1136 }
1137 ptr = strstr(i_port, "iqn.");
1138 if (ptr) {
1139 if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
Andy Grover6708bb22011-06-08 10:36:43 -07001140 pr_err("Passed iSCSI Initiator Port %s does not"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001141 " match target port protoid: %s\n", i_port,
1142 tcm_loop_dump_proto_id(tl_hba));
1143 return -EINVAL;
1144 }
1145 port_ptr = &i_port[0];
1146 goto check_newline;
1147 }
Andy Grover6708bb22011-06-08 10:36:43 -07001148 pr_err("Unable to locate prefix for emulated Initiator Port:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001149 " %s\n", i_port);
1150 return -EINVAL;
1151 /*
1152 * Clear any trailing newline for the NAA WWN
1153 */
1154check_newline:
1155 if (i_port[strlen(i_port)-1] == '\n')
1156 i_port[strlen(i_port)-1] = '\0';
1157
1158 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
1159 if (ret < 0)
1160 return ret;
1161
1162 return count;
1163}
1164
1165TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
1166
Hannes Reineckefb2b2842013-10-16 09:12:53 +02001167static ssize_t tcm_loop_tpg_show_transport_status(
1168 struct se_portal_group *se_tpg,
1169 char *page)
1170{
1171 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1172 struct tcm_loop_tpg, tl_se_tpg);
1173 const char *status = NULL;
1174 ssize_t ret = -EINVAL;
1175
1176 switch (tl_tpg->tl_transport_status) {
1177 case TCM_TRANSPORT_ONLINE:
1178 status = "online";
1179 break;
1180 case TCM_TRANSPORT_OFFLINE:
1181 status = "offline";
1182 break;
1183 default:
1184 break;
1185 }
1186
1187 if (status)
1188 ret = snprintf(page, PAGE_SIZE, "%s\n", status);
1189
1190 return ret;
1191}
1192
1193static ssize_t tcm_loop_tpg_store_transport_status(
1194 struct se_portal_group *se_tpg,
1195 const char *page,
1196 size_t count)
1197{
1198 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1199 struct tcm_loop_tpg, tl_se_tpg);
1200
1201 if (!strncmp(page, "online", 6)) {
1202 tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
1203 return count;
1204 }
1205 if (!strncmp(page, "offline", 7)) {
1206 tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
1207 return count;
1208 }
1209 return -EINVAL;
1210}
1211
1212TF_TPG_BASE_ATTR(tcm_loop, transport_status, S_IRUGO | S_IWUSR);
1213
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001214static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
1215 &tcm_loop_tpg_nexus.attr,
Hannes Reineckefb2b2842013-10-16 09:12:53 +02001216 &tcm_loop_tpg_transport_status.attr,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001217 NULL,
1218};
1219
1220/* Start items for tcm_loop_naa_cit */
1221
Rashika Kheriaf0a6c692013-12-19 00:02:54 +05301222static struct se_portal_group *tcm_loop_make_naa_tpg(
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001223 struct se_wwn *wwn,
1224 struct config_group *group,
1225 const char *name)
1226{
1227 struct tcm_loop_hba *tl_hba = container_of(wwn,
1228 struct tcm_loop_hba, tl_hba_wwn);
1229 struct tcm_loop_tpg *tl_tpg;
1230 char *tpgt_str, *end_ptr;
1231 int ret;
1232 unsigned short int tpgt;
1233
1234 tpgt_str = strstr(name, "tpgt_");
1235 if (!tpgt_str) {
Andy Grover6708bb22011-06-08 10:36:43 -07001236 pr_err("Unable to locate \"tpgt_#\" directory"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001237 " group\n");
1238 return ERR_PTR(-EINVAL);
1239 }
1240 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1241 tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1242
Dan Carpenter12f09cc2011-04-02 14:32:47 -07001243 if (tpgt >= TL_TPGS_PER_HBA) {
Andy Grover6708bb22011-06-08 10:36:43 -07001244 pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001245 " %u\n", tpgt, TL_TPGS_PER_HBA);
1246 return ERR_PTR(-EINVAL);
1247 }
1248 tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
1249 tl_tpg->tl_hba = tl_hba;
1250 tl_tpg->tl_tpgt = tpgt;
1251 /*
1252 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
1253 */
1254 ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
Andy Grover5951146d2011-07-19 10:26:37 +00001255 wwn, &tl_tpg->tl_se_tpg, tl_tpg,
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001256 TRANSPORT_TPG_TYPE_NORMAL);
1257 if (ret < 0)
1258 return ERR_PTR(-ENOMEM);
1259
Andy Grover6708bb22011-06-08 10:36:43 -07001260 pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001261 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1262 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1263
1264 return &tl_tpg->tl_se_tpg;
1265}
1266
Rashika Kheriaf0a6c692013-12-19 00:02:54 +05301267static void tcm_loop_drop_naa_tpg(
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001268 struct se_portal_group *se_tpg)
1269{
1270 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1271 struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1272 struct tcm_loop_tpg, tl_se_tpg);
1273 struct tcm_loop_hba *tl_hba;
1274 unsigned short tpgt;
1275
1276 tl_hba = tl_tpg->tl_hba;
1277 tpgt = tl_tpg->tl_tpgt;
1278 /*
1279 * Release the I_T Nexus for the Virtual SAS link if present
1280 */
1281 tcm_loop_drop_nexus(tl_tpg);
1282 /*
1283 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
1284 */
1285 core_tpg_deregister(se_tpg);
1286
Nicholas Bellinger0a020432011-10-10 19:44:05 -07001287 tl_tpg->tl_hba = NULL;
1288 tl_tpg->tl_tpgt = 0;
1289
Andy Grover6708bb22011-06-08 10:36:43 -07001290 pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001291 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1292 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1293}
1294
1295/* End items for tcm_loop_naa_cit */
1296
1297/* Start items for tcm_loop_cit */
1298
Rashika Kheriaf0a6c692013-12-19 00:02:54 +05301299static struct se_wwn *tcm_loop_make_scsi_hba(
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001300 struct target_fabric_configfs *tf,
1301 struct config_group *group,
1302 const char *name)
1303{
1304 struct tcm_loop_hba *tl_hba;
1305 struct Scsi_Host *sh;
1306 char *ptr;
1307 int ret, off = 0;
1308
1309 tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
1310 if (!tl_hba) {
Andy Grover6708bb22011-06-08 10:36:43 -07001311 pr_err("Unable to allocate struct tcm_loop_hba\n");
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001312 return ERR_PTR(-ENOMEM);
1313 }
1314 /*
1315 * Determine the emulated Protocol Identifier and Target Port Name
1316 * based on the incoming configfs directory name.
1317 */
1318 ptr = strstr(name, "naa.");
1319 if (ptr) {
1320 tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1321 goto check_len;
1322 }
1323 ptr = strstr(name, "fc.");
1324 if (ptr) {
1325 tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1326 off = 3; /* Skip over "fc." */
1327 goto check_len;
1328 }
1329 ptr = strstr(name, "iqn.");
Jesper Juhla57b5d32011-06-28 00:30:17 +02001330 if (!ptr) {
Andy Grover6708bb22011-06-08 10:36:43 -07001331 pr_err("Unable to locate prefix for emulated Target "
Jesper Juhla57b5d32011-06-28 00:30:17 +02001332 "Port: %s\n", name);
1333 ret = -EINVAL;
1334 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001335 }
Jesper Juhla57b5d32011-06-28 00:30:17 +02001336 tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001337
1338check_len:
Dan Carpenter60d645a2011-06-15 10:03:05 -07001339 if (strlen(name) >= TL_WWN_ADDR_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001340 pr_err("Emulated NAA %s Address: %s, exceeds"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001341 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1342 TL_WWN_ADDR_LEN);
Jesper Juhla57b5d32011-06-28 00:30:17 +02001343 ret = -EINVAL;
1344 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001345 }
1346 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1347
1348 /*
1349 * Call device_register(tl_hba->dev) to register the emulated
1350 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1351 * device_register() callbacks in tcm_loop_driver_probe()
1352 */
1353 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1354 if (ret)
1355 goto out;
1356
1357 sh = tl_hba->sh;
1358 tcm_loop_hba_no_cnt++;
Andy Grover6708bb22011-06-08 10:36:43 -07001359 pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001360 " %s Address: %s at Linux/SCSI Host ID: %d\n",
1361 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1362
1363 return &tl_hba->tl_hba_wwn;
1364out:
1365 kfree(tl_hba);
1366 return ERR_PTR(ret);
1367}
1368
Rashika Kheriaf0a6c692013-12-19 00:02:54 +05301369static void tcm_loop_drop_scsi_hba(
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001370 struct se_wwn *wwn)
1371{
1372 struct tcm_loop_hba *tl_hba = container_of(wwn,
1373 struct tcm_loop_hba, tl_hba_wwn);
Nicholas Bellinger6297b072011-11-12 09:29:51 -08001374
1375 pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
1376 " SAS Address: %s at Linux/SCSI Host ID: %d\n",
1377 tl_hba->tl_wwn_address, tl_hba->sh->host_no);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001378 /*
1379 * Call device_unregister() on the original tl_hba->dev.
1380 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1381 * release *tl_hba;
1382 */
1383 device_unregister(&tl_hba->dev);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001384}
1385
1386/* Start items for tcm_loop_cit */
1387static ssize_t tcm_loop_wwn_show_attr_version(
1388 struct target_fabric_configfs *tf,
1389 char *page)
1390{
1391 return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1392}
1393
1394TF_WWN_ATTR_RO(tcm_loop, version);
1395
1396static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1397 &tcm_loop_wwn_version.attr,
1398 NULL,
1399};
1400
1401/* End items for tcm_loop_cit */
1402
1403static int tcm_loop_register_configfs(void)
1404{
1405 struct target_fabric_configfs *fabric;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001406 int ret;
1407 /*
1408 * Set the TCM Loop HBA counter to zero
1409 */
1410 tcm_loop_hba_no_cnt = 0;
1411 /*
1412 * Register the top level struct config_item_type with TCM core
1413 */
1414 fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
Andy Grovere3d6f902011-07-19 08:55:10 +00001415 if (IS_ERR(fabric)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001416 pr_err("tcm_loop_register_configfs() failed!\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001417 return PTR_ERR(fabric);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001418 }
1419 /*
1420 * Setup the fabric API of function pointers used by target_core_mod
1421 */
1422 fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
1423 fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
1424 fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
1425 fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
1426 fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
1427 fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
1428 fabric->tf_ops.tpg_get_pr_transport_id_len =
1429 &tcm_loop_get_pr_transport_id_len;
1430 fabric->tf_ops.tpg_parse_pr_out_transport_id =
1431 &tcm_loop_parse_pr_out_transport_id;
1432 fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
1433 fabric->tf_ops.tpg_check_demo_mode_cache =
1434 &tcm_loop_check_demo_mode_cache;
1435 fabric->tf_ops.tpg_check_demo_mode_write_protect =
1436 &tcm_loop_check_demo_mode_write_protect;
1437 fabric->tf_ops.tpg_check_prod_mode_write_protect =
1438 &tcm_loop_check_prod_mode_write_protect;
1439 /*
1440 * The TCM loopback fabric module runs in demo-mode to a local
1441 * virtual SCSI device, so fabric dependent initator ACLs are
1442 * not required.
1443 */
1444 fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
1445 fabric->tf_ops.tpg_release_fabric_acl =
1446 &tcm_loop_tpg_release_fabric_acl;
1447 fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
1448 /*
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001449 * Used for setting up remaining TCM resources in process context
1450 */
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001451 fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
Christoph Hellwig35462972011-05-31 23:56:57 -04001452 fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001453 fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
1454 fabric->tf_ops.close_session = &tcm_loop_close_session;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001455 fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
1456 fabric->tf_ops.sess_get_initiator_sid = NULL;
1457 fabric->tf_ops.write_pending = &tcm_loop_write_pending;
1458 fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
1459 /*
1460 * Not used for TCM loopback
1461 */
1462 fabric->tf_ops.set_default_node_attributes =
1463 &tcm_loop_set_default_node_attributes;
1464 fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
1465 fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001466 fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
1467 fabric->tf_ops.queue_status = &tcm_loop_queue_status;
1468 fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07001469 fabric->tf_ops.aborted_task = &tcm_loop_aborted_task;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001470
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001471 /*
1472 * Setup function pointers for generic logic in target_core_fabric_configfs.c
1473 */
1474 fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
1475 fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
1476 fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
1477 fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
1478 /*
1479 * fabric_post_link() and fabric_pre_unlink() are used for
1480 * registration and release of TCM Loop Virtual SCSI LUNs.
1481 */
1482 fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
1483 fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
1484 fabric->tf_ops.fabric_make_np = NULL;
1485 fabric->tf_ops.fabric_drop_np = NULL;
1486 /*
1487 * Setup default attribute lists for various fabric->tf_cit_tmpl
1488 */
Andy Groverd80e224d2013-10-09 11:05:56 -07001489 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
1490 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
1491 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
1492 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
1493 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001494 /*
1495 * Once fabric->tf_ops has been setup, now register the fabric for
1496 * use within TCM
1497 */
1498 ret = target_fabric_configfs_register(fabric);
1499 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001500 pr_err("target_fabric_configfs_register() for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001501 " TCM_Loop failed!\n");
1502 target_fabric_configfs_free(fabric);
1503 return -1;
1504 }
1505 /*
1506 * Setup our local pointer to *fabric.
1507 */
1508 tcm_loop_fabric_configfs = fabric;
Andy Grover6708bb22011-06-08 10:36:43 -07001509 pr_debug("TCM_LOOP[0] - Set fabric ->"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001510 " tcm_loop_fabric_configfs\n");
1511 return 0;
1512}
1513
1514static void tcm_loop_deregister_configfs(void)
1515{
1516 if (!tcm_loop_fabric_configfs)
1517 return;
1518
1519 target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
1520 tcm_loop_fabric_configfs = NULL;
Andy Grover6708bb22011-06-08 10:36:43 -07001521 pr_debug("TCM_LOOP[0] - Cleared"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001522 " tcm_loop_fabric_configfs\n");
1523}
1524
1525static int __init tcm_loop_fabric_init(void)
1526{
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001527 int ret = -ENOMEM;
1528
1529 tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
1530 if (!tcm_loop_workqueue)
1531 goto out;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001532
1533 tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1534 sizeof(struct tcm_loop_cmd),
1535 __alignof__(struct tcm_loop_cmd),
1536 0, NULL);
1537 if (!tcm_loop_cmd_cache) {
Andy Grover6708bb22011-06-08 10:36:43 -07001538 pr_debug("kmem_cache_create() for"
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001539 " tcm_loop_cmd_cache failed\n");
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001540 goto out_destroy_workqueue;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001541 }
1542
1543 ret = tcm_loop_alloc_core_bus();
1544 if (ret)
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001545 goto out_destroy_cache;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001546
1547 ret = tcm_loop_register_configfs();
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001548 if (ret)
1549 goto out_release_core_bus;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001550
1551 return 0;
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001552
1553out_release_core_bus:
1554 tcm_loop_release_core_bus();
1555out_destroy_cache:
1556 kmem_cache_destroy(tcm_loop_cmd_cache);
1557out_destroy_workqueue:
1558 destroy_workqueue(tcm_loop_workqueue);
1559out:
1560 return ret;
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001561}
1562
1563static void __exit tcm_loop_fabric_exit(void)
1564{
1565 tcm_loop_deregister_configfs();
1566 tcm_loop_release_core_bus();
1567 kmem_cache_destroy(tcm_loop_cmd_cache);
Christoph Hellwigafe2cb72012-02-02 17:04:41 -05001568 destroy_workqueue(tcm_loop_workqueue);
Nicholas Bellinger3703b2c2011-03-18 15:39:17 -07001569}
1570
1571MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1572MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1573MODULE_LICENSE("GPL");
1574module_init(tcm_loop_fabric_init);
1575module_exit(tcm_loop_fabric_exit);