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