xref: /linux/drivers/target/loopback/tcm_loop.c (revision 2c142b63c8ee982cdfdba49a616027c266294838)
1 /*******************************************************************************
2  *
3  * This file contains the Linux/SCSI LLD virtual SCSI initiator driver
4  * for emulated SAS initiator ports
5  *
6  * © Copyright 2011-2013 Datera, Inc.
7  *
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 <linux/blk-mq.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_tcq.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_cmnd.h>
35 
36 #include <target/target_core_base.h>
37 #include <target/target_core_fabric.h>
38 
39 #include "tcm_loop.h"
40 
41 #define to_tcm_loop_hba(hba)	container_of(hba, struct tcm_loop_hba, dev)
42 
43 static struct kmem_cache *tcm_loop_cmd_cache;
44 
45 static int tcm_loop_hba_no_cnt;
46 
47 static int tcm_loop_queue_status(struct se_cmd *se_cmd);
48 
49 static unsigned int tcm_loop_nr_hw_queues = 1;
50 module_param_named(nr_hw_queues, tcm_loop_nr_hw_queues, uint, 0644);
51 
52 static unsigned int tcm_loop_can_queue = 1024;
53 module_param_named(can_queue, tcm_loop_can_queue, uint, 0644);
54 
55 static unsigned int tcm_loop_cmd_per_lun = 1024;
56 module_param_named(cmd_per_lun, tcm_loop_cmd_per_lun, uint, 0644);
57 
58 /*
59  * Called from struct target_core_fabric_ops->check_stop_free()
60  */
tcm_loop_check_stop_free(struct se_cmd * se_cmd)61 static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
62 {
63 	return transport_generic_free_cmd(se_cmd, 0);
64 }
65 
tcm_loop_release_cmd(struct se_cmd * se_cmd)66 static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
67 {
68 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
69 				struct tcm_loop_cmd, tl_se_cmd);
70 	struct scsi_cmnd *sc = tl_cmd->sc;
71 
72 	if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
73 		kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
74 	else
75 		scsi_done(sc);
76 }
77 
tcm_loop_show_info(struct seq_file * m,struct Scsi_Host * host)78 static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host)
79 {
80 	seq_puts(m, "tcm_loop_proc_info()\n");
81 	return 0;
82 }
83 
84 static int tcm_loop_driver_probe(struct device *);
85 static void tcm_loop_driver_remove(struct device *);
86 
87 static const struct bus_type tcm_loop_lld_bus = {
88 	.name			= "tcm_loop_bus",
89 	.probe			= tcm_loop_driver_probe,
90 	.remove			= tcm_loop_driver_remove,
91 };
92 
93 static struct device_driver tcm_loop_driverfs = {
94 	.name			= "tcm_loop",
95 	.bus			= &tcm_loop_lld_bus,
96 };
97 /*
98  * Used with root_device_register() in tcm_loop_alloc_core_bus() below
99  */
100 static struct device *tcm_loop_primary;
101 
tcm_loop_target_queue_cmd(struct tcm_loop_cmd * tl_cmd)102 static void tcm_loop_target_queue_cmd(struct tcm_loop_cmd *tl_cmd)
103 {
104 	struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
105 	struct scsi_cmnd *sc = tl_cmd->sc;
106 	struct tcm_loop_nexus *tl_nexus;
107 	struct tcm_loop_hba *tl_hba;
108 	struct tcm_loop_tpg *tl_tpg;
109 	struct scatterlist *sgl_bidi = NULL;
110 	u32 sgl_bidi_count = 0, transfer_length;
111 
112 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
113 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
114 
115 	/*
116 	 * Ensure that this tl_tpg reference from the incoming sc->device->id
117 	 * has already been configured via tcm_loop_make_naa_tpg().
118 	 */
119 	if (!tl_tpg->tl_hba) {
120 		set_host_byte(sc, DID_NO_CONNECT);
121 		goto out_done;
122 	}
123 	if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) {
124 		set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
125 		goto out_done;
126 	}
127 	tl_nexus = tl_tpg->tl_nexus;
128 	if (!tl_nexus) {
129 		scmd_printk(KERN_ERR, sc,
130 			    "TCM_Loop I_T Nexus does not exist\n");
131 		set_host_byte(sc, DID_ERROR);
132 		goto out_done;
133 	}
134 
135 	transfer_length = scsi_transfer_length(sc);
136 	if (!scsi_prot_sg_count(sc) &&
137 	    scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) {
138 		se_cmd->prot_pto = true;
139 		/*
140 		 * loopback transport doesn't support
141 		 * WRITE_GENERATE, READ_STRIP protection
142 		 * information operations, go ahead unprotected.
143 		 */
144 		transfer_length = scsi_bufflen(sc);
145 	}
146 
147 	se_cmd->tag = tl_cmd->sc_cmd_tag;
148 	target_init_cmd(se_cmd, tl_nexus->se_sess, &tl_cmd->tl_sense_buf[0],
149 			tl_cmd->sc->device->lun, transfer_length,
150 			TCM_SIMPLE_TAG, sc->sc_data_direction, 0);
151 
152 	if (target_submit_prep(se_cmd, sc->cmnd, scsi_sglist(sc),
153 			       scsi_sg_count(sc), sgl_bidi, sgl_bidi_count,
154 			       scsi_prot_sglist(sc), scsi_prot_sg_count(sc),
155 			       GFP_ATOMIC))
156 		return;
157 
158 	target_submit(se_cmd);
159 	return;
160 
161 out_done:
162 	scsi_done(sc);
163 }
164 
165 /*
166  * ->queuecommand can be and usually is called from interrupt context, so
167  * defer the actual submission to a workqueue.
168  */
tcm_loop_queuecommand(struct Scsi_Host * sh,struct scsi_cmnd * sc)169 static enum scsi_qc_status tcm_loop_queuecommand(struct Scsi_Host *sh,
170 						 struct scsi_cmnd *sc)
171 {
172 	struct tcm_loop_cmd *tl_cmd = scsi_cmd_priv(sc);
173 
174 	pr_debug("%s() %d:%d:%d:%llu got CDB: 0x%02x scsi_buf_len: %u\n",
175 		 __func__, sc->device->host->host_no, sc->device->id,
176 		 sc->device->channel, sc->device->lun, sc->cmnd[0],
177 		 scsi_bufflen(sc));
178 
179 	memset(tl_cmd, 0, sizeof(*tl_cmd));
180 	tl_cmd->sc = sc;
181 	tl_cmd->sc_cmd_tag = blk_mq_unique_tag(scsi_cmd_to_rq(sc));
182 
183 	tcm_loop_target_queue_cmd(tl_cmd);
184 	return 0;
185 }
186 
187 /*
188  * Called from SCSI EH process context to issue a LUN_RESET TMR
189  * to struct scsi_device
190  */
tcm_loop_issue_tmr(struct tcm_loop_tpg * tl_tpg,u64 lun,int task,enum tcm_tmreq_table tmr)191 static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg,
192 			      u64 lun, int task, enum tcm_tmreq_table tmr)
193 {
194 	struct se_cmd *se_cmd;
195 	struct se_session *se_sess;
196 	struct tcm_loop_nexus *tl_nexus;
197 	struct tcm_loop_cmd *tl_cmd;
198 	int ret = TMR_FUNCTION_FAILED, rc;
199 
200 	/*
201 	 * Locate the tl_nexus and se_sess pointers
202 	 */
203 	tl_nexus = tl_tpg->tl_nexus;
204 	if (!tl_nexus) {
205 		pr_err("Unable to perform device reset without active I_T Nexus\n");
206 		return ret;
207 	}
208 
209 	tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
210 	if (!tl_cmd)
211 		return ret;
212 
213 	init_completion(&tl_cmd->tmr_done);
214 
215 	se_cmd = &tl_cmd->tl_se_cmd;
216 	se_sess = tl_tpg->tl_nexus->se_sess;
217 
218 	rc = target_submit_tmr(se_cmd, se_sess, tl_cmd->tl_sense_buf, lun,
219 			       NULL, tmr, GFP_KERNEL, task,
220 			       TARGET_SCF_ACK_KREF);
221 	if (rc < 0)
222 		goto release;
223 	wait_for_completion(&tl_cmd->tmr_done);
224 	ret = se_cmd->se_tmr_req->response;
225 	target_put_sess_cmd(se_cmd);
226 
227 out:
228 	return ret;
229 
230 release:
231 	kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
232 	goto out;
233 }
234 
tcm_loop_abort_task(struct scsi_cmnd * sc)235 static int tcm_loop_abort_task(struct scsi_cmnd *sc)
236 {
237 	struct tcm_loop_hba *tl_hba;
238 	struct tcm_loop_tpg *tl_tpg;
239 	int ret;
240 
241 	/*
242 	 * Locate the tcm_loop_hba_t pointer
243 	 */
244 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
245 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
246 	ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
247 				 blk_mq_unique_tag(scsi_cmd_to_rq(sc)),
248 				 TMR_ABORT_TASK);
249 	return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
250 }
251 
252 /*
253  * Called from SCSI EH process context to issue a LUN_RESET TMR
254  * to struct scsi_device
255  */
tcm_loop_device_reset(struct scsi_cmnd * sc)256 static int tcm_loop_device_reset(struct scsi_cmnd *sc)
257 {
258 	struct tcm_loop_hba *tl_hba;
259 	struct tcm_loop_tpg *tl_tpg;
260 	int ret;
261 
262 	/*
263 	 * Locate the tcm_loop_hba_t pointer
264 	 */
265 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
266 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
267 
268 	ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
269 				 0, TMR_LUN_RESET);
270 	return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
271 }
272 
tcm_loop_flush_work_iter(struct request * rq,void * data)273 static bool tcm_loop_flush_work_iter(struct request *rq, void *data)
274 {
275 	struct scsi_cmnd *sc = blk_mq_rq_to_pdu(rq);
276 	struct tcm_loop_cmd *tl_cmd = scsi_cmd_priv(sc);
277 	struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
278 
279 	flush_work(&se_cmd->work);
280 	return true;
281 }
282 
tcm_loop_target_reset(struct scsi_cmnd * sc)283 static int tcm_loop_target_reset(struct scsi_cmnd *sc)
284 {
285 	struct tcm_loop_hba *tl_hba;
286 	struct tcm_loop_tpg *tl_tpg;
287 	struct Scsi_Host *sh = sc->device->host;
288 	int ret;
289 
290 	/*
291 	 * Locate the tcm_loop_hba_t pointer
292 	 */
293 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sh);
294 	if (!tl_hba) {
295 		pr_err("Unable to perform device reset without active I_T Nexus\n");
296 		return FAILED;
297 	}
298 	/*
299 	 * Locate the tl_tpg pointer from TargetID in sc->device->id
300 	 */
301 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
302 	if (!tl_tpg)
303 		return FAILED;
304 
305 	/*
306 	 * Issue a LUN_RESET to drain all commands that the target core
307 	 * knows about.  This handles commands not yet marked CMD_T_COMPLETE.
308 	 */
309 	ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun, 0, TMR_LUN_RESET);
310 	if (ret != TMR_FUNCTION_COMPLETE)
311 		return FAILED;
312 
313 	/*
314 	 * Flush any deferred target core completion work that may still be
315 	 * queued.  Commands that already had CMD_T_COMPLETE set before the TMR
316 	 * are skipped by the TMR drain, but their async completion work
317 	 * (transport_lun_remove_cmd → percpu_ref_put, release_cmd → scsi_done)
318 	 * may still be pending in target_completion_wq.
319 	 *
320 	 * The SCSI EH will reuse in-flight scsi_cmnd structures for recovery
321 	 * commands (e.g. TUR) immediately after this handler returns SUCCESS —
322 	 * if deferred work is still pending, the memset in queuecommand would
323 	 * zero the se_cmd while the work accesses it, leaking the LUN
324 	 * percpu_ref and hanging configfs unlink forever.
325 	 *
326 	 * Use blk_mq_tagset_busy_iter() to find all started requests and
327 	 * flush_work() on each — the same pattern used by mpi3mr, scsi_debug,
328 	 * and other SCSI drivers to drain outstanding commands during reset.
329 	 */
330 	blk_mq_tagset_busy_iter(&sh->tag_set, tcm_loop_flush_work_iter, NULL);
331 
332 	tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
333 	return SUCCESS;
334 }
335 
336 static const struct scsi_host_template tcm_loop_driver_template = {
337 	.show_info		= tcm_loop_show_info,
338 	.proc_name		= "tcm_loopback",
339 	.name			= "TCM_Loopback",
340 	.queuecommand		= tcm_loop_queuecommand,
341 	.change_queue_depth	= scsi_change_queue_depth,
342 	.eh_abort_handler = tcm_loop_abort_task,
343 	.eh_device_reset_handler = tcm_loop_device_reset,
344 	.eh_target_reset_handler = tcm_loop_target_reset,
345 	.this_id		= -1,
346 	.sg_tablesize		= 256,
347 	.max_sectors		= 0xFFFF,
348 	.dma_boundary		= PAGE_SIZE - 1,
349 	.module			= THIS_MODULE,
350 	.track_queue_depth	= 1,
351 	.cmd_size		= sizeof(struct tcm_loop_cmd),
352 };
353 
tcm_loop_driver_probe(struct device * dev)354 static int tcm_loop_driver_probe(struct device *dev)
355 {
356 	struct tcm_loop_hba *tl_hba;
357 	struct Scsi_Host *sh;
358 	int error, host_prot;
359 
360 	tl_hba = to_tcm_loop_hba(dev);
361 
362 	sh = scsi_host_alloc(&tcm_loop_driver_template,
363 			sizeof(struct tcm_loop_hba));
364 	if (!sh) {
365 		pr_err("Unable to allocate struct scsi_host\n");
366 		return -ENODEV;
367 	}
368 	tl_hba->sh = sh;
369 
370 	/*
371 	 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
372 	 */
373 	*((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
374 	/*
375 	 * Setup single ID, Channel and LUN for now..
376 	 */
377 	sh->max_id = 2;
378 	sh->max_lun = 0;
379 	sh->max_channel = 0;
380 	sh->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
381 	sh->nr_hw_queues = tcm_loop_nr_hw_queues;
382 	sh->can_queue = tcm_loop_can_queue;
383 	sh->cmd_per_lun = tcm_loop_cmd_per_lun;
384 
385 	host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
386 		    SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
387 		    SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
388 
389 	scsi_host_set_prot(sh, host_prot);
390 	scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC);
391 
392 	error = scsi_add_host(sh, &tl_hba->dev);
393 	if (error) {
394 		pr_err("%s: scsi_add_host failed\n", __func__);
395 		scsi_host_put(sh);
396 		tl_hba->sh = NULL;
397 		return -ENODEV;
398 	}
399 	return 0;
400 }
401 
tcm_loop_driver_remove(struct device * dev)402 static void tcm_loop_driver_remove(struct device *dev)
403 {
404 	struct tcm_loop_hba *tl_hba;
405 	struct Scsi_Host *sh;
406 
407 	tl_hba = to_tcm_loop_hba(dev);
408 	sh = tl_hba->sh;
409 
410 	if (sh) {
411 		scsi_remove_host(sh);
412 		scsi_host_put(sh);
413 	}
414 }
415 
tcm_loop_release_adapter(struct device * dev)416 static void tcm_loop_release_adapter(struct device *dev)
417 {
418 	struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
419 
420 	kfree(tl_hba);
421 }
422 
423 /*
424  * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
425  */
tcm_loop_setup_hba_bus(struct tcm_loop_hba * tl_hba,int tcm_loop_host_id)426 static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
427 {
428 	int ret;
429 
430 	tl_hba->dev.bus = &tcm_loop_lld_bus;
431 	tl_hba->dev.parent = tcm_loop_primary;
432 	tl_hba->dev.release = &tcm_loop_release_adapter;
433 	dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
434 
435 	ret = device_register(&tl_hba->dev);
436 	if (ret) {
437 		pr_err("device_register() failed for tl_hba->dev: %d\n", ret);
438 		put_device(&tl_hba->dev);
439 		return -ENODEV;
440 	}
441 
442 	if (!tl_hba->sh) {
443 		device_unregister(&tl_hba->dev);
444 		return -ENODEV;
445 	}
446 
447 	return 0;
448 }
449 
450 /*
451  * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
452  * tcm_loop SCSI bus.
453  */
tcm_loop_alloc_core_bus(void)454 static int tcm_loop_alloc_core_bus(void)
455 {
456 	int ret;
457 
458 	tcm_loop_primary = root_device_register("tcm_loop_0");
459 	if (IS_ERR(tcm_loop_primary)) {
460 		pr_err("Unable to allocate tcm_loop_primary\n");
461 		return PTR_ERR(tcm_loop_primary);
462 	}
463 
464 	ret = bus_register(&tcm_loop_lld_bus);
465 	if (ret) {
466 		pr_err("bus_register() failed for tcm_loop_lld_bus\n");
467 		goto dev_unreg;
468 	}
469 
470 	ret = driver_register(&tcm_loop_driverfs);
471 	if (ret) {
472 		pr_err("driver_register() failed for tcm_loop_driverfs\n");
473 		goto bus_unreg;
474 	}
475 
476 	pr_debug("Initialized TCM Loop Core Bus\n");
477 	return ret;
478 
479 bus_unreg:
480 	bus_unregister(&tcm_loop_lld_bus);
481 dev_unreg:
482 	root_device_unregister(tcm_loop_primary);
483 	return ret;
484 }
485 
tcm_loop_release_core_bus(void)486 static void tcm_loop_release_core_bus(void)
487 {
488 	driver_unregister(&tcm_loop_driverfs);
489 	bus_unregister(&tcm_loop_lld_bus);
490 	root_device_unregister(tcm_loop_primary);
491 
492 	pr_debug("Releasing TCM Loop Core BUS\n");
493 }
494 
tl_tpg(struct se_portal_group * se_tpg)495 static inline struct tcm_loop_tpg *tl_tpg(struct se_portal_group *se_tpg)
496 {
497 	return container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
498 }
499 
tcm_loop_get_endpoint_wwn(struct se_portal_group * se_tpg)500 static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
501 {
502 	/*
503 	 * Return the passed NAA identifier for the Target Port
504 	 */
505 	return &tl_tpg(se_tpg)->tl_hba->tl_wwn_address[0];
506 }
507 
tcm_loop_get_tag(struct se_portal_group * se_tpg)508 static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
509 {
510 	/*
511 	 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
512 	 * to represent the SCSI Target Port.
513 	 */
514 	return tl_tpg(se_tpg)->tl_tpgt;
515 }
516 
517 /*
518  * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
519  * based upon the incoming fabric dependent SCSI Initiator Port
520  */
tcm_loop_check_demo_mode(struct se_portal_group * se_tpg)521 static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
522 {
523 	return 1;
524 }
525 
tcm_loop_check_prot_fabric_only(struct se_portal_group * se_tpg)526 static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg)
527 {
528 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
529 						   tl_se_tpg);
530 	return tl_tpg->tl_fabric_prot_type;
531 }
532 
tcm_loop_sess_get_index(struct se_session * se_sess)533 static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
534 {
535 	return 1;
536 }
537 
tcm_loop_get_cmd_state(struct se_cmd * se_cmd)538 static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
539 {
540 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
541 			struct tcm_loop_cmd, tl_se_cmd);
542 
543 	return tl_cmd->sc_cmd_state;
544 }
545 
tcm_loop_write_pending(struct se_cmd * se_cmd)546 static int tcm_loop_write_pending(struct se_cmd *se_cmd)
547 {
548 	/*
549 	 * Since Linux/SCSI has already sent down a struct scsi_cmnd
550 	 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
551 	 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
552 	 * format with transport_generic_map_mem_to_cmd().
553 	 *
554 	 * We now tell TCM to add this WRITE CDB directly into the TCM storage
555 	 * object execution queue.
556 	 */
557 	target_execute_cmd(se_cmd);
558 	return 0;
559 }
560 
tcm_loop_queue_data_or_status(const char * func,struct se_cmd * se_cmd,u8 scsi_status)561 static int tcm_loop_queue_data_or_status(const char *func,
562 		struct se_cmd *se_cmd, u8 scsi_status)
563 {
564 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
565 				struct tcm_loop_cmd, tl_se_cmd);
566 	struct scsi_cmnd *sc = tl_cmd->sc;
567 
568 	pr_debug("%s() called for scsi_cmnd: %p cdb: 0x%02x\n",
569 		 func, sc, sc->cmnd[0]);
570 
571 	if (se_cmd->sense_buffer &&
572 	   ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
573 	    (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
574 
575 		memcpy(sc->sense_buffer, se_cmd->sense_buffer,
576 				SCSI_SENSE_BUFFERSIZE);
577 		sc->result = SAM_STAT_CHECK_CONDITION;
578 	} else
579 		sc->result = scsi_status;
580 
581 	set_host_byte(sc, DID_OK);
582 	if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
583 	    (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
584 		scsi_set_resid(sc, se_cmd->residual_count);
585 	return 0;
586 }
587 
tcm_loop_queue_data_in(struct se_cmd * se_cmd)588 static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
589 {
590 	return tcm_loop_queue_data_or_status(__func__, se_cmd, SAM_STAT_GOOD);
591 }
592 
tcm_loop_queue_status(struct se_cmd * se_cmd)593 static int tcm_loop_queue_status(struct se_cmd *se_cmd)
594 {
595 	return tcm_loop_queue_data_or_status(__func__,
596 					     se_cmd, se_cmd->scsi_status);
597 }
598 
tcm_loop_queue_tm_rsp(struct se_cmd * se_cmd)599 static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
600 {
601 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
602 				struct tcm_loop_cmd, tl_se_cmd);
603 
604 	/* Wake up tcm_loop_issue_tmr(). */
605 	complete(&tl_cmd->tmr_done);
606 }
607 
tcm_loop_aborted_task(struct se_cmd * se_cmd)608 static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
609 {
610 	return;
611 }
612 
tcm_loop_dump_proto_id(struct tcm_loop_hba * tl_hba)613 static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
614 {
615 	switch (tl_hba->tl_proto_id) {
616 	case SCSI_PROTOCOL_SAS:
617 		return "SAS";
618 	case SCSI_PROTOCOL_FCP:
619 		return "FCP";
620 	case SCSI_PROTOCOL_ISCSI:
621 		return "iSCSI";
622 	default:
623 		break;
624 	}
625 
626 	return "Unknown";
627 }
628 
629 /* Start items for tcm_loop_port_cit */
630 
tcm_loop_port_link(struct se_portal_group * se_tpg,struct se_lun * lun)631 static int tcm_loop_port_link(
632 	struct se_portal_group *se_tpg,
633 	struct se_lun *lun)
634 {
635 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
636 				struct tcm_loop_tpg, tl_se_tpg);
637 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
638 
639 	atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
640 	/*
641 	 * Add Linux/SCSI struct scsi_device by HCTL
642 	 */
643 	scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
644 
645 	pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
646 	return 0;
647 }
648 
tcm_loop_port_unlink(struct se_portal_group * se_tpg,struct se_lun * se_lun)649 static void tcm_loop_port_unlink(
650 	struct se_portal_group *se_tpg,
651 	struct se_lun *se_lun)
652 {
653 	struct scsi_device *sd;
654 	struct tcm_loop_hba *tl_hba;
655 	struct tcm_loop_tpg *tl_tpg;
656 
657 	tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
658 	tl_hba = tl_tpg->tl_hba;
659 
660 	sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
661 				se_lun->unpacked_lun);
662 	if (!sd) {
663 		pr_err("Unable to locate struct scsi_device for %d:%d:%llu\n",
664 		       0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
665 		return;
666 	}
667 	/*
668 	 * Remove Linux/SCSI struct scsi_device by HCTL
669 	 */
670 	scsi_remove_device(sd);
671 	scsi_device_put(sd);
672 
673 	atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
674 
675 	pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
676 }
677 
678 /* End items for tcm_loop_port_cit */
679 
tcm_loop_tpg_attrib_fabric_prot_type_show(struct config_item * item,char * page)680 static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_show(
681 		struct config_item *item, char *page)
682 {
683 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
684 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
685 						   tl_se_tpg);
686 
687 	return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type);
688 }
689 
tcm_loop_tpg_attrib_fabric_prot_type_store(struct config_item * item,const char * page,size_t count)690 static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_store(
691 		struct config_item *item, const char *page, size_t count)
692 {
693 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
694 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
695 						   tl_se_tpg);
696 	unsigned long val;
697 	int ret = kstrtoul(page, 0, &val);
698 
699 	if (ret) {
700 		pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
701 		return ret;
702 	}
703 	if (val != 0 && val != 1 && val != 3) {
704 		pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
705 		return -EINVAL;
706 	}
707 	tl_tpg->tl_fabric_prot_type = val;
708 
709 	return count;
710 }
711 
712 CONFIGFS_ATTR(tcm_loop_tpg_attrib_, fabric_prot_type);
713 
714 static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = {
715 	&tcm_loop_tpg_attrib_attr_fabric_prot_type,
716 	NULL,
717 };
718 
719 /* Start items for tcm_loop_nexus_cit */
720 
tcm_loop_alloc_sess_cb(struct se_portal_group * se_tpg,struct se_session * se_sess,void * p)721 static int tcm_loop_alloc_sess_cb(struct se_portal_group *se_tpg,
722 				  struct se_session *se_sess, void *p)
723 {
724 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
725 					struct tcm_loop_tpg, tl_se_tpg);
726 
727 	tl_tpg->tl_nexus = p;
728 	return 0;
729 }
730 
tcm_loop_make_nexus(struct tcm_loop_tpg * tl_tpg,const char * name)731 static int tcm_loop_make_nexus(
732 	struct tcm_loop_tpg *tl_tpg,
733 	const char *name)
734 {
735 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
736 	struct tcm_loop_nexus *tl_nexus;
737 	int ret;
738 
739 	if (tl_tpg->tl_nexus) {
740 		pr_debug("tl_tpg->tl_nexus already exists\n");
741 		return -EEXIST;
742 	}
743 
744 	tl_nexus = kzalloc_obj(*tl_nexus);
745 	if (!tl_nexus)
746 		return -ENOMEM;
747 
748 	tl_nexus->se_sess = target_setup_session(&tl_tpg->tl_se_tpg, 0, 0,
749 					TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS,
750 					name, tl_nexus, tcm_loop_alloc_sess_cb);
751 	if (IS_ERR(tl_nexus->se_sess)) {
752 		ret = PTR_ERR(tl_nexus->se_sess);
753 		kfree(tl_nexus);
754 		return ret;
755 	}
756 
757 	pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated %s Initiator Port: %s\n",
758 		 tcm_loop_dump_proto_id(tl_hba), name);
759 	return 0;
760 }
761 
tcm_loop_drop_nexus(struct tcm_loop_tpg * tpg)762 static int tcm_loop_drop_nexus(
763 	struct tcm_loop_tpg *tpg)
764 {
765 	struct se_session *se_sess;
766 	struct tcm_loop_nexus *tl_nexus;
767 
768 	tl_nexus = tpg->tl_nexus;
769 	if (!tl_nexus)
770 		return -ENODEV;
771 
772 	se_sess = tl_nexus->se_sess;
773 	if (!se_sess)
774 		return -ENODEV;
775 
776 	if (atomic_read(&tpg->tl_tpg_port_count)) {
777 		pr_err("Unable to remove TCM_Loop I_T Nexus with active TPG port count: %d\n",
778 		       atomic_read(&tpg->tl_tpg_port_count));
779 		return -EPERM;
780 	}
781 
782 	pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated %s Initiator Port: %s\n",
783 		 tcm_loop_dump_proto_id(tpg->tl_hba),
784 		 tl_nexus->se_sess->se_node_acl->initiatorname);
785 	/*
786 	 * Release the SCSI I_T Nexus to the emulated Target Port
787 	 */
788 	target_remove_session(se_sess);
789 	tpg->tl_nexus = NULL;
790 	kfree(tl_nexus);
791 	return 0;
792 }
793 
794 /* End items for tcm_loop_nexus_cit */
795 
tcm_loop_tpg_nexus_show(struct config_item * item,char * page)796 static ssize_t tcm_loop_tpg_nexus_show(struct config_item *item, char *page)
797 {
798 	struct se_portal_group *se_tpg = to_tpg(item);
799 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
800 			struct tcm_loop_tpg, tl_se_tpg);
801 	struct tcm_loop_nexus *tl_nexus;
802 	ssize_t ret;
803 
804 	tl_nexus = tl_tpg->tl_nexus;
805 	if (!tl_nexus)
806 		return -ENODEV;
807 
808 	ret = snprintf(page, PAGE_SIZE, "%s\n",
809 		tl_nexus->se_sess->se_node_acl->initiatorname);
810 
811 	return ret;
812 }
813 
tcm_loop_tpg_nexus_store(struct config_item * item,const char * page,size_t count)814 static ssize_t tcm_loop_tpg_nexus_store(struct config_item *item,
815 		const char *page, size_t count)
816 {
817 	struct se_portal_group *se_tpg = to_tpg(item);
818 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
819 			struct tcm_loop_tpg, tl_se_tpg);
820 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
821 	unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
822 	int ret;
823 	/*
824 	 * Shutdown the active I_T nexus if 'NULL' is passed..
825 	 */
826 	if (!strncmp(page, "NULL", 4)) {
827 		ret = tcm_loop_drop_nexus(tl_tpg);
828 		return (!ret) ? count : ret;
829 	}
830 	/*
831 	 * Otherwise make sure the passed virtual Initiator port WWN matches
832 	 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
833 	 * tcm_loop_make_nexus()
834 	 */
835 	if (strlen(page) >= TL_WWN_ADDR_LEN) {
836 		pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
837 		       page, TL_WWN_ADDR_LEN);
838 		return -EINVAL;
839 	}
840 	snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
841 
842 	ptr = strstr(i_port, "naa.");
843 	if (ptr) {
844 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
845 			pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
846 			       i_port, tcm_loop_dump_proto_id(tl_hba));
847 			return -EINVAL;
848 		}
849 		port_ptr = &i_port[0];
850 		goto check_newline;
851 	}
852 	ptr = strstr(i_port, "fc.");
853 	if (ptr) {
854 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
855 			pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
856 			       i_port, tcm_loop_dump_proto_id(tl_hba));
857 			return -EINVAL;
858 		}
859 		port_ptr = &i_port[3]; /* Skip over "fc." */
860 		goto check_newline;
861 	}
862 	ptr = strstr(i_port, "iqn.");
863 	if (ptr) {
864 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
865 			pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
866 			       i_port, tcm_loop_dump_proto_id(tl_hba));
867 			return -EINVAL;
868 		}
869 		port_ptr = &i_port[0];
870 		goto check_newline;
871 	}
872 	pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
873 	       i_port);
874 	return -EINVAL;
875 	/*
876 	 * Clear any trailing newline for the NAA WWN
877 	 */
878 check_newline:
879 	if (i_port[strlen(i_port)-1] == '\n')
880 		i_port[strlen(i_port)-1] = '\0';
881 
882 	ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
883 	if (ret < 0)
884 		return ret;
885 
886 	return count;
887 }
888 
tcm_loop_tpg_transport_status_show(struct config_item * item,char * page)889 static ssize_t tcm_loop_tpg_transport_status_show(struct config_item *item,
890 		char *page)
891 {
892 	struct se_portal_group *se_tpg = to_tpg(item);
893 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
894 			struct tcm_loop_tpg, tl_se_tpg);
895 	const char *status = NULL;
896 	ssize_t ret = -EINVAL;
897 
898 	switch (tl_tpg->tl_transport_status) {
899 	case TCM_TRANSPORT_ONLINE:
900 		status = "online";
901 		break;
902 	case TCM_TRANSPORT_OFFLINE:
903 		status = "offline";
904 		break;
905 	default:
906 		break;
907 	}
908 
909 	if (status)
910 		ret = snprintf(page, PAGE_SIZE, "%s\n", status);
911 
912 	return ret;
913 }
914 
tcm_loop_tpg_transport_status_store(struct config_item * item,const char * page,size_t count)915 static ssize_t tcm_loop_tpg_transport_status_store(struct config_item *item,
916 		const char *page, size_t count)
917 {
918 	struct se_portal_group *se_tpg = to_tpg(item);
919 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
920 			struct tcm_loop_tpg, tl_se_tpg);
921 
922 	if (!strncmp(page, "online", 6)) {
923 		tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
924 		return count;
925 	}
926 	if (!strncmp(page, "offline", 7)) {
927 		tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
928 		if (tl_tpg->tl_nexus) {
929 			struct se_session *tl_sess = tl_tpg->tl_nexus->se_sess;
930 
931 			core_allocate_nexus_loss_ua(tl_sess->se_node_acl);
932 		}
933 		return count;
934 	}
935 	return -EINVAL;
936 }
937 
tcm_loop_tpg_address_show(struct config_item * item,char * page)938 static ssize_t tcm_loop_tpg_address_show(struct config_item *item,
939 					 char *page)
940 {
941 	struct se_portal_group *se_tpg = to_tpg(item);
942 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
943 			struct tcm_loop_tpg, tl_se_tpg);
944 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
945 
946 	if (!tl_hba->sh)
947 		return -ENODEV;
948 
949 	return snprintf(page, PAGE_SIZE, "%d:0:%d\n",
950 			tl_hba->sh->host_no, tl_tpg->tl_tpgt);
951 }
952 
953 CONFIGFS_ATTR(tcm_loop_tpg_, nexus);
954 CONFIGFS_ATTR(tcm_loop_tpg_, transport_status);
955 CONFIGFS_ATTR_RO(tcm_loop_tpg_, address);
956 
957 static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
958 	&tcm_loop_tpg_attr_nexus,
959 	&tcm_loop_tpg_attr_transport_status,
960 	&tcm_loop_tpg_attr_address,
961 	NULL,
962 };
963 
964 /* Start items for tcm_loop_naa_cit */
965 
tcm_loop_make_naa_tpg(struct se_wwn * wwn,const char * name)966 static struct se_portal_group *tcm_loop_make_naa_tpg(struct se_wwn *wwn,
967 						     const char *name)
968 {
969 	struct tcm_loop_hba *tl_hba = container_of(wwn,
970 			struct tcm_loop_hba, tl_hba_wwn);
971 	struct tcm_loop_tpg *tl_tpg;
972 	int ret;
973 	unsigned long tpgt;
974 
975 	if (strstr(name, "tpgt_") != name) {
976 		pr_err("Unable to locate \"tpgt_#\" directory group\n");
977 		return ERR_PTR(-EINVAL);
978 	}
979 	if (kstrtoul(name+5, 10, &tpgt))
980 		return ERR_PTR(-EINVAL);
981 
982 	if (tpgt >= TL_TPGS_PER_HBA) {
983 		pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA: %u\n",
984 		       tpgt, TL_TPGS_PER_HBA);
985 		return ERR_PTR(-EINVAL);
986 	}
987 	tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
988 	tl_tpg->tl_hba = tl_hba;
989 	tl_tpg->tl_tpgt = tpgt;
990 	/*
991 	 * Register the tl_tpg as a emulated TCM Target Endpoint
992 	 */
993 	ret = core_tpg_register(wwn, &tl_tpg->tl_se_tpg, tl_hba->tl_proto_id);
994 	if (ret < 0)
995 		return ERR_PTR(-ENOMEM);
996 
997 	pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s Target Port %s,t,0x%04lx\n",
998 		 tcm_loop_dump_proto_id(tl_hba),
999 		 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1000 	return &tl_tpg->tl_se_tpg;
1001 }
1002 
tcm_loop_drop_naa_tpg(struct se_portal_group * se_tpg)1003 static void tcm_loop_drop_naa_tpg(
1004 	struct se_portal_group *se_tpg)
1005 {
1006 	struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1007 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1008 				struct tcm_loop_tpg, tl_se_tpg);
1009 	struct tcm_loop_hba *tl_hba;
1010 	unsigned short tpgt;
1011 
1012 	tl_hba = tl_tpg->tl_hba;
1013 	tpgt = tl_tpg->tl_tpgt;
1014 	/*
1015 	 * Release the I_T Nexus for the Virtual target link if present
1016 	 */
1017 	tcm_loop_drop_nexus(tl_tpg);
1018 	/*
1019 	 * Deregister the tl_tpg as a emulated TCM Target Endpoint
1020 	 */
1021 	core_tpg_deregister(se_tpg);
1022 
1023 	tl_tpg->tl_hba = NULL;
1024 	tl_tpg->tl_tpgt = 0;
1025 
1026 	pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s Target Port %s,t,0x%04x\n",
1027 		 tcm_loop_dump_proto_id(tl_hba),
1028 		 config_item_name(&wwn->wwn_group.cg_item), tpgt);
1029 }
1030 
1031 /* End items for tcm_loop_naa_cit */
1032 
1033 /* Start items for tcm_loop_cit */
1034 
tcm_loop_make_scsi_hba(struct target_fabric_configfs * tf,struct config_group * group,const char * name)1035 static struct se_wwn *tcm_loop_make_scsi_hba(
1036 	struct target_fabric_configfs *tf,
1037 	struct config_group *group,
1038 	const char *name)
1039 {
1040 	struct tcm_loop_hba *tl_hba;
1041 	struct Scsi_Host *sh;
1042 	char *ptr;
1043 	int ret, off = 0;
1044 
1045 	tl_hba = kzalloc_obj(*tl_hba);
1046 	if (!tl_hba)
1047 		return ERR_PTR(-ENOMEM);
1048 
1049 	/*
1050 	 * Determine the emulated Protocol Identifier and Target Port Name
1051 	 * based on the incoming configfs directory name.
1052 	 */
1053 	ptr = strstr(name, "naa.");
1054 	if (ptr) {
1055 		tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1056 		goto check_len;
1057 	}
1058 	ptr = strstr(name, "fc.");
1059 	if (ptr) {
1060 		tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1061 		off = 3; /* Skip over "fc." */
1062 		goto check_len;
1063 	}
1064 	ptr = strstr(name, "iqn.");
1065 	if (!ptr) {
1066 		pr_err("Unable to locate prefix for emulated Target Port: %s\n",
1067 		       name);
1068 		ret = -EINVAL;
1069 		goto out;
1070 	}
1071 	tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
1072 
1073 check_len:
1074 	if (strlen(name) >= TL_WWN_ADDR_LEN) {
1075 		pr_err("Emulated NAA %s Address: %s, exceeds max: %d\n",
1076 		       name, tcm_loop_dump_proto_id(tl_hba), TL_WWN_ADDR_LEN);
1077 		ret = -EINVAL;
1078 		goto out;
1079 	}
1080 	snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1081 
1082 	/*
1083 	 * Call device_register(tl_hba->dev) to register the emulated
1084 	 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1085 	 * device_register() callbacks in tcm_loop_driver_probe()
1086 	 */
1087 	ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1088 	if (ret)
1089 		return ERR_PTR(ret);
1090 
1091 	sh = tl_hba->sh;
1092 	tcm_loop_hba_no_cnt++;
1093 	pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
1094 		 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1095 	return &tl_hba->tl_hba_wwn;
1096 out:
1097 	kfree(tl_hba);
1098 	return ERR_PTR(ret);
1099 }
1100 
tcm_loop_drop_scsi_hba(struct se_wwn * wwn)1101 static void tcm_loop_drop_scsi_hba(
1102 	struct se_wwn *wwn)
1103 {
1104 	struct tcm_loop_hba *tl_hba = container_of(wwn,
1105 				struct tcm_loop_hba, tl_hba_wwn);
1106 
1107 	pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
1108 		 tcm_loop_dump_proto_id(tl_hba), tl_hba->tl_wwn_address,
1109 		 tl_hba->sh->host_no);
1110 	/*
1111 	 * Call device_unregister() on the original tl_hba->dev.
1112 	 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1113 	 * release *tl_hba;
1114 	 */
1115 	device_unregister(&tl_hba->dev);
1116 }
1117 
1118 /* Start items for tcm_loop_cit */
tcm_loop_wwn_version_show(struct config_item * item,char * page)1119 static ssize_t tcm_loop_wwn_version_show(struct config_item *item, char *page)
1120 {
1121 	return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1122 }
1123 
1124 CONFIGFS_ATTR_RO(tcm_loop_wwn_, version);
1125 
1126 static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1127 	&tcm_loop_wwn_attr_version,
1128 	NULL,
1129 };
1130 
1131 /* End items for tcm_loop_cit */
1132 
1133 static const struct target_core_fabric_ops loop_ops = {
1134 	.module				= THIS_MODULE,
1135 	.fabric_name			= "loopback",
1136 	.tpg_get_wwn			= tcm_loop_get_endpoint_wwn,
1137 	.tpg_get_tag			= tcm_loop_get_tag,
1138 	.tpg_check_demo_mode		= tcm_loop_check_demo_mode,
1139 	.tpg_check_prot_fabric_only	= tcm_loop_check_prot_fabric_only,
1140 	.check_stop_free		= tcm_loop_check_stop_free,
1141 	.release_cmd			= tcm_loop_release_cmd,
1142 	.sess_get_index			= tcm_loop_sess_get_index,
1143 	.write_pending			= tcm_loop_write_pending,
1144 	.get_cmd_state			= tcm_loop_get_cmd_state,
1145 	.queue_data_in			= tcm_loop_queue_data_in,
1146 	.queue_status			= tcm_loop_queue_status,
1147 	.queue_tm_rsp			= tcm_loop_queue_tm_rsp,
1148 	.aborted_task			= tcm_loop_aborted_task,
1149 	.fabric_make_wwn		= tcm_loop_make_scsi_hba,
1150 	.fabric_drop_wwn		= tcm_loop_drop_scsi_hba,
1151 	.fabric_make_tpg		= tcm_loop_make_naa_tpg,
1152 	.fabric_drop_tpg		= tcm_loop_drop_naa_tpg,
1153 	.fabric_post_link		= tcm_loop_port_link,
1154 	.fabric_pre_unlink		= tcm_loop_port_unlink,
1155 	.tfc_wwn_attrs			= tcm_loop_wwn_attrs,
1156 	.tfc_tpg_base_attrs		= tcm_loop_tpg_attrs,
1157 	.tfc_tpg_attrib_attrs		= tcm_loop_tpg_attrib_attrs,
1158 	.default_compl_type		= TARGET_QUEUE_COMPL,
1159 	.default_submit_type		= TARGET_QUEUE_SUBMIT,
1160 	.direct_submit_supp		= 0,
1161 };
1162 
tcm_loop_fabric_init(void)1163 static int __init tcm_loop_fabric_init(void)
1164 {
1165 	int ret = -ENOMEM;
1166 
1167 	tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1168 				sizeof(struct tcm_loop_cmd),
1169 				__alignof__(struct tcm_loop_cmd),
1170 				0, NULL);
1171 	if (!tcm_loop_cmd_cache) {
1172 		pr_debug("kmem_cache_create() for tcm_loop_cmd_cache failed\n");
1173 		goto out;
1174 	}
1175 
1176 	ret = tcm_loop_alloc_core_bus();
1177 	if (ret)
1178 		goto out_destroy_cache;
1179 
1180 	ret = target_register_template(&loop_ops);
1181 	if (ret)
1182 		goto out_release_core_bus;
1183 
1184 	return 0;
1185 
1186 out_release_core_bus:
1187 	tcm_loop_release_core_bus();
1188 out_destroy_cache:
1189 	kmem_cache_destroy(tcm_loop_cmd_cache);
1190 out:
1191 	return ret;
1192 }
1193 
tcm_loop_fabric_exit(void)1194 static void __exit tcm_loop_fabric_exit(void)
1195 {
1196 	target_unregister_template(&loop_ops);
1197 	tcm_loop_release_core_bus();
1198 	kmem_cache_destroy(tcm_loop_cmd_cache);
1199 }
1200 
1201 MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1202 MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1203 MODULE_LICENSE("GPL");
1204 module_init(tcm_loop_fabric_init);
1205 module_exit(tcm_loop_fabric_exit);
1206