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