xref: /linux/drivers/target/loopback/tcm_loop.c (revision 4f87e9068bf3aaf45f226261d5efd50bec42c12c)
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  */
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 	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 
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 
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  */
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  */
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 
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  */
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 
273 static const struct scsi_host_template tcm_loop_driver_template = {
274 	.show_info		= tcm_loop_show_info,
275 	.proc_name		= "tcm_loopback",
276 	.name			= "TCM_Loopback",
277 	.queuecommand		= tcm_loop_queuecommand,
278 	.change_queue_depth	= scsi_change_queue_depth,
279 	.eh_abort_handler = tcm_loop_abort_task,
280 	.eh_device_reset_handler = tcm_loop_device_reset,
281 	.this_id		= -1,
282 	.sg_tablesize		= 256,
283 	.max_sectors		= 0xFFFF,
284 	.dma_boundary		= PAGE_SIZE - 1,
285 	.module			= THIS_MODULE,
286 	.track_queue_depth	= 1,
287 	.cmd_size		= sizeof(struct tcm_loop_cmd),
288 };
289 
290 static int tcm_loop_driver_probe(struct device *dev)
291 {
292 	struct tcm_loop_hba *tl_hba;
293 	struct Scsi_Host *sh;
294 	int error, host_prot;
295 
296 	tl_hba = to_tcm_loop_hba(dev);
297 
298 	sh = scsi_host_alloc(&tcm_loop_driver_template,
299 			sizeof(struct tcm_loop_hba));
300 	if (!sh) {
301 		pr_err("Unable to allocate struct scsi_host\n");
302 		return -ENODEV;
303 	}
304 	tl_hba->sh = sh;
305 
306 	/*
307 	 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
308 	 */
309 	*((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
310 	/*
311 	 * Setup single ID, Channel and LUN for now..
312 	 */
313 	sh->max_id = 2;
314 	sh->max_lun = 0;
315 	sh->max_channel = 0;
316 	sh->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
317 	sh->nr_hw_queues = tcm_loop_nr_hw_queues;
318 	sh->can_queue = tcm_loop_can_queue;
319 	sh->cmd_per_lun = tcm_loop_cmd_per_lun;
320 
321 	host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
322 		    SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
323 		    SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
324 
325 	scsi_host_set_prot(sh, host_prot);
326 	scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC);
327 
328 	error = scsi_add_host(sh, &tl_hba->dev);
329 	if (error) {
330 		pr_err("%s: scsi_add_host failed\n", __func__);
331 		scsi_host_put(sh);
332 		tl_hba->sh = NULL;
333 		return -ENODEV;
334 	}
335 	return 0;
336 }
337 
338 static void tcm_loop_driver_remove(struct device *dev)
339 {
340 	struct tcm_loop_hba *tl_hba;
341 	struct Scsi_Host *sh;
342 
343 	tl_hba = to_tcm_loop_hba(dev);
344 	sh = tl_hba->sh;
345 
346 	if (sh) {
347 		scsi_remove_host(sh);
348 		scsi_host_put(sh);
349 	}
350 }
351 
352 static void tcm_loop_release_adapter(struct device *dev)
353 {
354 	struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
355 
356 	kfree(tl_hba);
357 }
358 
359 /*
360  * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
361  */
362 static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
363 {
364 	int ret;
365 
366 	tl_hba->dev.bus = &tcm_loop_lld_bus;
367 	tl_hba->dev.parent = tcm_loop_primary;
368 	tl_hba->dev.release = &tcm_loop_release_adapter;
369 	dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
370 
371 	ret = device_register(&tl_hba->dev);
372 	if (ret) {
373 		pr_err("device_register() failed for tl_hba->dev: %d\n", ret);
374 		put_device(&tl_hba->dev);
375 		return -ENODEV;
376 	}
377 
378 	if (!tl_hba->sh) {
379 		device_unregister(&tl_hba->dev);
380 		return -ENODEV;
381 	}
382 
383 	return 0;
384 }
385 
386 /*
387  * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
388  * tcm_loop SCSI bus.
389  */
390 static int tcm_loop_alloc_core_bus(void)
391 {
392 	int ret;
393 
394 	tcm_loop_primary = root_device_register("tcm_loop_0");
395 	if (IS_ERR(tcm_loop_primary)) {
396 		pr_err("Unable to allocate tcm_loop_primary\n");
397 		return PTR_ERR(tcm_loop_primary);
398 	}
399 
400 	ret = bus_register(&tcm_loop_lld_bus);
401 	if (ret) {
402 		pr_err("bus_register() failed for tcm_loop_lld_bus\n");
403 		goto dev_unreg;
404 	}
405 
406 	ret = driver_register(&tcm_loop_driverfs);
407 	if (ret) {
408 		pr_err("driver_register() failed for tcm_loop_driverfs\n");
409 		goto bus_unreg;
410 	}
411 
412 	pr_debug("Initialized TCM Loop Core Bus\n");
413 	return ret;
414 
415 bus_unreg:
416 	bus_unregister(&tcm_loop_lld_bus);
417 dev_unreg:
418 	root_device_unregister(tcm_loop_primary);
419 	return ret;
420 }
421 
422 static void tcm_loop_release_core_bus(void)
423 {
424 	driver_unregister(&tcm_loop_driverfs);
425 	bus_unregister(&tcm_loop_lld_bus);
426 	root_device_unregister(tcm_loop_primary);
427 
428 	pr_debug("Releasing TCM Loop Core BUS\n");
429 }
430 
431 static inline struct tcm_loop_tpg *tl_tpg(struct se_portal_group *se_tpg)
432 {
433 	return container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
434 }
435 
436 static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
437 {
438 	/*
439 	 * Return the passed NAA identifier for the Target Port
440 	 */
441 	return &tl_tpg(se_tpg)->tl_hba->tl_wwn_address[0];
442 }
443 
444 static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
445 {
446 	/*
447 	 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
448 	 * to represent the SCSI Target Port.
449 	 */
450 	return tl_tpg(se_tpg)->tl_tpgt;
451 }
452 
453 /*
454  * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
455  * based upon the incoming fabric dependent SCSI Initiator Port
456  */
457 static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
458 {
459 	return 1;
460 }
461 
462 static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg)
463 {
464 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
465 						   tl_se_tpg);
466 	return tl_tpg->tl_fabric_prot_type;
467 }
468 
469 static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
470 {
471 	return 1;
472 }
473 
474 static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
475 {
476 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
477 			struct tcm_loop_cmd, tl_se_cmd);
478 
479 	return tl_cmd->sc_cmd_state;
480 }
481 
482 static int tcm_loop_write_pending(struct se_cmd *se_cmd)
483 {
484 	/*
485 	 * Since Linux/SCSI has already sent down a struct scsi_cmnd
486 	 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
487 	 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
488 	 * format with transport_generic_map_mem_to_cmd().
489 	 *
490 	 * We now tell TCM to add this WRITE CDB directly into the TCM storage
491 	 * object execution queue.
492 	 */
493 	target_execute_cmd(se_cmd);
494 	return 0;
495 }
496 
497 static int tcm_loop_queue_data_or_status(const char *func,
498 		struct se_cmd *se_cmd, u8 scsi_status)
499 {
500 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
501 				struct tcm_loop_cmd, tl_se_cmd);
502 	struct scsi_cmnd *sc = tl_cmd->sc;
503 
504 	pr_debug("%s() called for scsi_cmnd: %p cdb: 0x%02x\n",
505 		 func, sc, sc->cmnd[0]);
506 
507 	if (se_cmd->sense_buffer &&
508 	   ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
509 	    (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
510 
511 		memcpy(sc->sense_buffer, se_cmd->sense_buffer,
512 				SCSI_SENSE_BUFFERSIZE);
513 		sc->result = SAM_STAT_CHECK_CONDITION;
514 	} else
515 		sc->result = scsi_status;
516 
517 	set_host_byte(sc, DID_OK);
518 	if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
519 	    (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
520 		scsi_set_resid(sc, se_cmd->residual_count);
521 	return 0;
522 }
523 
524 static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
525 {
526 	return tcm_loop_queue_data_or_status(__func__, se_cmd, SAM_STAT_GOOD);
527 }
528 
529 static int tcm_loop_queue_status(struct se_cmd *se_cmd)
530 {
531 	return tcm_loop_queue_data_or_status(__func__,
532 					     se_cmd, se_cmd->scsi_status);
533 }
534 
535 static void tcm_loop_queue_tm_rsp(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 	/* Wake up tcm_loop_issue_tmr(). */
541 	complete(&tl_cmd->tmr_done);
542 }
543 
544 static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
545 {
546 	return;
547 }
548 
549 static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
550 {
551 	switch (tl_hba->tl_proto_id) {
552 	case SCSI_PROTOCOL_SAS:
553 		return "SAS";
554 	case SCSI_PROTOCOL_FCP:
555 		return "FCP";
556 	case SCSI_PROTOCOL_ISCSI:
557 		return "iSCSI";
558 	default:
559 		break;
560 	}
561 
562 	return "Unknown";
563 }
564 
565 /* Start items for tcm_loop_port_cit */
566 
567 static int tcm_loop_port_link(
568 	struct se_portal_group *se_tpg,
569 	struct se_lun *lun)
570 {
571 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
572 				struct tcm_loop_tpg, tl_se_tpg);
573 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
574 
575 	atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
576 	/*
577 	 * Add Linux/SCSI struct scsi_device by HCTL
578 	 */
579 	scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
580 
581 	pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
582 	return 0;
583 }
584 
585 static void tcm_loop_port_unlink(
586 	struct se_portal_group *se_tpg,
587 	struct se_lun *se_lun)
588 {
589 	struct scsi_device *sd;
590 	struct tcm_loop_hba *tl_hba;
591 	struct tcm_loop_tpg *tl_tpg;
592 
593 	tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
594 	tl_hba = tl_tpg->tl_hba;
595 
596 	sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
597 				se_lun->unpacked_lun);
598 	if (!sd) {
599 		pr_err("Unable to locate struct scsi_device for %d:%d:%llu\n",
600 		       0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
601 		return;
602 	}
603 	/*
604 	 * Remove Linux/SCSI struct scsi_device by HCTL
605 	 */
606 	scsi_remove_device(sd);
607 	scsi_device_put(sd);
608 
609 	atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
610 
611 	pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
612 }
613 
614 /* End items for tcm_loop_port_cit */
615 
616 static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_show(
617 		struct config_item *item, char *page)
618 {
619 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
620 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
621 						   tl_se_tpg);
622 
623 	return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type);
624 }
625 
626 static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_store(
627 		struct config_item *item, const char *page, size_t count)
628 {
629 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
630 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
631 						   tl_se_tpg);
632 	unsigned long val;
633 	int ret = kstrtoul(page, 0, &val);
634 
635 	if (ret) {
636 		pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
637 		return ret;
638 	}
639 	if (val != 0 && val != 1 && val != 3) {
640 		pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
641 		return -EINVAL;
642 	}
643 	tl_tpg->tl_fabric_prot_type = val;
644 
645 	return count;
646 }
647 
648 CONFIGFS_ATTR(tcm_loop_tpg_attrib_, fabric_prot_type);
649 
650 static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = {
651 	&tcm_loop_tpg_attrib_attr_fabric_prot_type,
652 	NULL,
653 };
654 
655 /* Start items for tcm_loop_nexus_cit */
656 
657 static int tcm_loop_alloc_sess_cb(struct se_portal_group *se_tpg,
658 				  struct se_session *se_sess, void *p)
659 {
660 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
661 					struct tcm_loop_tpg, tl_se_tpg);
662 
663 	tl_tpg->tl_nexus = p;
664 	return 0;
665 }
666 
667 static int tcm_loop_make_nexus(
668 	struct tcm_loop_tpg *tl_tpg,
669 	const char *name)
670 {
671 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
672 	struct tcm_loop_nexus *tl_nexus;
673 	int ret;
674 
675 	if (tl_tpg->tl_nexus) {
676 		pr_debug("tl_tpg->tl_nexus already exists\n");
677 		return -EEXIST;
678 	}
679 
680 	tl_nexus = kzalloc_obj(*tl_nexus);
681 	if (!tl_nexus)
682 		return -ENOMEM;
683 
684 	tl_nexus->se_sess = target_setup_session(&tl_tpg->tl_se_tpg, 0, 0,
685 					TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS,
686 					name, tl_nexus, tcm_loop_alloc_sess_cb);
687 	if (IS_ERR(tl_nexus->se_sess)) {
688 		ret = PTR_ERR(tl_nexus->se_sess);
689 		kfree(tl_nexus);
690 		return ret;
691 	}
692 
693 	pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated %s Initiator Port: %s\n",
694 		 tcm_loop_dump_proto_id(tl_hba), name);
695 	return 0;
696 }
697 
698 static int tcm_loop_drop_nexus(
699 	struct tcm_loop_tpg *tpg)
700 {
701 	struct se_session *se_sess;
702 	struct tcm_loop_nexus *tl_nexus;
703 
704 	tl_nexus = tpg->tl_nexus;
705 	if (!tl_nexus)
706 		return -ENODEV;
707 
708 	se_sess = tl_nexus->se_sess;
709 	if (!se_sess)
710 		return -ENODEV;
711 
712 	if (atomic_read(&tpg->tl_tpg_port_count)) {
713 		pr_err("Unable to remove TCM_Loop I_T Nexus with active TPG port count: %d\n",
714 		       atomic_read(&tpg->tl_tpg_port_count));
715 		return -EPERM;
716 	}
717 
718 	pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated %s Initiator Port: %s\n",
719 		 tcm_loop_dump_proto_id(tpg->tl_hba),
720 		 tl_nexus->se_sess->se_node_acl->initiatorname);
721 	/*
722 	 * Release the SCSI I_T Nexus to the emulated Target Port
723 	 */
724 	target_remove_session(se_sess);
725 	tpg->tl_nexus = NULL;
726 	kfree(tl_nexus);
727 	return 0;
728 }
729 
730 /* End items for tcm_loop_nexus_cit */
731 
732 static ssize_t tcm_loop_tpg_nexus_show(struct config_item *item, char *page)
733 {
734 	struct se_portal_group *se_tpg = to_tpg(item);
735 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
736 			struct tcm_loop_tpg, tl_se_tpg);
737 	struct tcm_loop_nexus *tl_nexus;
738 	ssize_t ret;
739 
740 	tl_nexus = tl_tpg->tl_nexus;
741 	if (!tl_nexus)
742 		return -ENODEV;
743 
744 	ret = snprintf(page, PAGE_SIZE, "%s\n",
745 		tl_nexus->se_sess->se_node_acl->initiatorname);
746 
747 	return ret;
748 }
749 
750 static ssize_t tcm_loop_tpg_nexus_store(struct config_item *item,
751 		const char *page, size_t count)
752 {
753 	struct se_portal_group *se_tpg = to_tpg(item);
754 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
755 			struct tcm_loop_tpg, tl_se_tpg);
756 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
757 	unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
758 	int ret;
759 	/*
760 	 * Shutdown the active I_T nexus if 'NULL' is passed..
761 	 */
762 	if (!strncmp(page, "NULL", 4)) {
763 		ret = tcm_loop_drop_nexus(tl_tpg);
764 		return (!ret) ? count : ret;
765 	}
766 	/*
767 	 * Otherwise make sure the passed virtual Initiator port WWN matches
768 	 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
769 	 * tcm_loop_make_nexus()
770 	 */
771 	if (strlen(page) >= TL_WWN_ADDR_LEN) {
772 		pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
773 		       page, TL_WWN_ADDR_LEN);
774 		return -EINVAL;
775 	}
776 	snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
777 
778 	ptr = strstr(i_port, "naa.");
779 	if (ptr) {
780 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
781 			pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
782 			       i_port, tcm_loop_dump_proto_id(tl_hba));
783 			return -EINVAL;
784 		}
785 		port_ptr = &i_port[0];
786 		goto check_newline;
787 	}
788 	ptr = strstr(i_port, "fc.");
789 	if (ptr) {
790 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
791 			pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
792 			       i_port, tcm_loop_dump_proto_id(tl_hba));
793 			return -EINVAL;
794 		}
795 		port_ptr = &i_port[3]; /* Skip over "fc." */
796 		goto check_newline;
797 	}
798 	ptr = strstr(i_port, "iqn.");
799 	if (ptr) {
800 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
801 			pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
802 			       i_port, tcm_loop_dump_proto_id(tl_hba));
803 			return -EINVAL;
804 		}
805 		port_ptr = &i_port[0];
806 		goto check_newline;
807 	}
808 	pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
809 	       i_port);
810 	return -EINVAL;
811 	/*
812 	 * Clear any trailing newline for the NAA WWN
813 	 */
814 check_newline:
815 	if (i_port[strlen(i_port)-1] == '\n')
816 		i_port[strlen(i_port)-1] = '\0';
817 
818 	ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
819 	if (ret < 0)
820 		return ret;
821 
822 	return count;
823 }
824 
825 static ssize_t tcm_loop_tpg_transport_status_show(struct config_item *item,
826 		char *page)
827 {
828 	struct se_portal_group *se_tpg = to_tpg(item);
829 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
830 			struct tcm_loop_tpg, tl_se_tpg);
831 	const char *status = NULL;
832 	ssize_t ret = -EINVAL;
833 
834 	switch (tl_tpg->tl_transport_status) {
835 	case TCM_TRANSPORT_ONLINE:
836 		status = "online";
837 		break;
838 	case TCM_TRANSPORT_OFFLINE:
839 		status = "offline";
840 		break;
841 	default:
842 		break;
843 	}
844 
845 	if (status)
846 		ret = snprintf(page, PAGE_SIZE, "%s\n", status);
847 
848 	return ret;
849 }
850 
851 static ssize_t tcm_loop_tpg_transport_status_store(struct config_item *item,
852 		const char *page, size_t count)
853 {
854 	struct se_portal_group *se_tpg = to_tpg(item);
855 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
856 			struct tcm_loop_tpg, tl_se_tpg);
857 
858 	if (!strncmp(page, "online", 6)) {
859 		tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
860 		return count;
861 	}
862 	if (!strncmp(page, "offline", 7)) {
863 		tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
864 		if (tl_tpg->tl_nexus) {
865 			struct se_session *tl_sess = tl_tpg->tl_nexus->se_sess;
866 
867 			core_allocate_nexus_loss_ua(tl_sess->se_node_acl);
868 		}
869 		return count;
870 	}
871 	return -EINVAL;
872 }
873 
874 static ssize_t tcm_loop_tpg_address_show(struct config_item *item,
875 					 char *page)
876 {
877 	struct se_portal_group *se_tpg = to_tpg(item);
878 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
879 			struct tcm_loop_tpg, tl_se_tpg);
880 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
881 
882 	if (!tl_hba->sh)
883 		return -ENODEV;
884 
885 	return snprintf(page, PAGE_SIZE, "%d:0:%d\n",
886 			tl_hba->sh->host_no, tl_tpg->tl_tpgt);
887 }
888 
889 CONFIGFS_ATTR(tcm_loop_tpg_, nexus);
890 CONFIGFS_ATTR(tcm_loop_tpg_, transport_status);
891 CONFIGFS_ATTR_RO(tcm_loop_tpg_, address);
892 
893 static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
894 	&tcm_loop_tpg_attr_nexus,
895 	&tcm_loop_tpg_attr_transport_status,
896 	&tcm_loop_tpg_attr_address,
897 	NULL,
898 };
899 
900 /* Start items for tcm_loop_naa_cit */
901 
902 static struct se_portal_group *tcm_loop_make_naa_tpg(struct se_wwn *wwn,
903 						     const char *name)
904 {
905 	struct tcm_loop_hba *tl_hba = container_of(wwn,
906 			struct tcm_loop_hba, tl_hba_wwn);
907 	struct tcm_loop_tpg *tl_tpg;
908 	int ret;
909 	unsigned long tpgt;
910 
911 	if (strstr(name, "tpgt_") != name) {
912 		pr_err("Unable to locate \"tpgt_#\" directory group\n");
913 		return ERR_PTR(-EINVAL);
914 	}
915 	if (kstrtoul(name+5, 10, &tpgt))
916 		return ERR_PTR(-EINVAL);
917 
918 	if (tpgt >= TL_TPGS_PER_HBA) {
919 		pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA: %u\n",
920 		       tpgt, TL_TPGS_PER_HBA);
921 		return ERR_PTR(-EINVAL);
922 	}
923 	tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
924 	tl_tpg->tl_hba = tl_hba;
925 	tl_tpg->tl_tpgt = tpgt;
926 	/*
927 	 * Register the tl_tpg as a emulated TCM Target Endpoint
928 	 */
929 	ret = core_tpg_register(wwn, &tl_tpg->tl_se_tpg, tl_hba->tl_proto_id);
930 	if (ret < 0)
931 		return ERR_PTR(-ENOMEM);
932 
933 	pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s Target Port %s,t,0x%04lx\n",
934 		 tcm_loop_dump_proto_id(tl_hba),
935 		 config_item_name(&wwn->wwn_group.cg_item), tpgt);
936 	return &tl_tpg->tl_se_tpg;
937 }
938 
939 static void tcm_loop_drop_naa_tpg(
940 	struct se_portal_group *se_tpg)
941 {
942 	struct se_wwn *wwn = se_tpg->se_tpg_wwn;
943 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
944 				struct tcm_loop_tpg, tl_se_tpg);
945 	struct tcm_loop_hba *tl_hba;
946 	unsigned short tpgt;
947 
948 	tl_hba = tl_tpg->tl_hba;
949 	tpgt = tl_tpg->tl_tpgt;
950 	/*
951 	 * Release the I_T Nexus for the Virtual target link if present
952 	 */
953 	tcm_loop_drop_nexus(tl_tpg);
954 	/*
955 	 * Deregister the tl_tpg as a emulated TCM Target Endpoint
956 	 */
957 	core_tpg_deregister(se_tpg);
958 
959 	tl_tpg->tl_hba = NULL;
960 	tl_tpg->tl_tpgt = 0;
961 
962 	pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s Target Port %s,t,0x%04x\n",
963 		 tcm_loop_dump_proto_id(tl_hba),
964 		 config_item_name(&wwn->wwn_group.cg_item), tpgt);
965 }
966 
967 /* End items for tcm_loop_naa_cit */
968 
969 /* Start items for tcm_loop_cit */
970 
971 static struct se_wwn *tcm_loop_make_scsi_hba(
972 	struct target_fabric_configfs *tf,
973 	struct config_group *group,
974 	const char *name)
975 {
976 	struct tcm_loop_hba *tl_hba;
977 	struct Scsi_Host *sh;
978 	char *ptr;
979 	int ret, off = 0;
980 
981 	tl_hba = kzalloc_obj(*tl_hba);
982 	if (!tl_hba)
983 		return ERR_PTR(-ENOMEM);
984 
985 	/*
986 	 * Determine the emulated Protocol Identifier and Target Port Name
987 	 * based on the incoming configfs directory name.
988 	 */
989 	ptr = strstr(name, "naa.");
990 	if (ptr) {
991 		tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
992 		goto check_len;
993 	}
994 	ptr = strstr(name, "fc.");
995 	if (ptr) {
996 		tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
997 		off = 3; /* Skip over "fc." */
998 		goto check_len;
999 	}
1000 	ptr = strstr(name, "iqn.");
1001 	if (!ptr) {
1002 		pr_err("Unable to locate prefix for emulated Target Port: %s\n",
1003 		       name);
1004 		ret = -EINVAL;
1005 		goto out;
1006 	}
1007 	tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
1008 
1009 check_len:
1010 	if (strlen(name) >= TL_WWN_ADDR_LEN) {
1011 		pr_err("Emulated NAA %s Address: %s, exceeds max: %d\n",
1012 		       name, tcm_loop_dump_proto_id(tl_hba), TL_WWN_ADDR_LEN);
1013 		ret = -EINVAL;
1014 		goto out;
1015 	}
1016 	snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1017 
1018 	/*
1019 	 * Call device_register(tl_hba->dev) to register the emulated
1020 	 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1021 	 * device_register() callbacks in tcm_loop_driver_probe()
1022 	 */
1023 	ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1024 	if (ret)
1025 		return ERR_PTR(ret);
1026 
1027 	sh = tl_hba->sh;
1028 	tcm_loop_hba_no_cnt++;
1029 	pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
1030 		 tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1031 	return &tl_hba->tl_hba_wwn;
1032 out:
1033 	kfree(tl_hba);
1034 	return ERR_PTR(ret);
1035 }
1036 
1037 static void tcm_loop_drop_scsi_hba(
1038 	struct se_wwn *wwn)
1039 {
1040 	struct tcm_loop_hba *tl_hba = container_of(wwn,
1041 				struct tcm_loop_hba, tl_hba_wwn);
1042 
1043 	pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target %s Address: %s at Linux/SCSI Host ID: %d\n",
1044 		 tcm_loop_dump_proto_id(tl_hba), tl_hba->tl_wwn_address,
1045 		 tl_hba->sh->host_no);
1046 	/*
1047 	 * Call device_unregister() on the original tl_hba->dev.
1048 	 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1049 	 * release *tl_hba;
1050 	 */
1051 	device_unregister(&tl_hba->dev);
1052 }
1053 
1054 /* Start items for tcm_loop_cit */
1055 static ssize_t tcm_loop_wwn_version_show(struct config_item *item, char *page)
1056 {
1057 	return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1058 }
1059 
1060 CONFIGFS_ATTR_RO(tcm_loop_wwn_, version);
1061 
1062 static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1063 	&tcm_loop_wwn_attr_version,
1064 	NULL,
1065 };
1066 
1067 /* End items for tcm_loop_cit */
1068 
1069 static const struct target_core_fabric_ops loop_ops = {
1070 	.module				= THIS_MODULE,
1071 	.fabric_name			= "loopback",
1072 	.tpg_get_wwn			= tcm_loop_get_endpoint_wwn,
1073 	.tpg_get_tag			= tcm_loop_get_tag,
1074 	.tpg_check_demo_mode		= tcm_loop_check_demo_mode,
1075 	.tpg_check_prot_fabric_only	= tcm_loop_check_prot_fabric_only,
1076 	.check_stop_free		= tcm_loop_check_stop_free,
1077 	.release_cmd			= tcm_loop_release_cmd,
1078 	.sess_get_index			= tcm_loop_sess_get_index,
1079 	.write_pending			= tcm_loop_write_pending,
1080 	.get_cmd_state			= tcm_loop_get_cmd_state,
1081 	.queue_data_in			= tcm_loop_queue_data_in,
1082 	.queue_status			= tcm_loop_queue_status,
1083 	.queue_tm_rsp			= tcm_loop_queue_tm_rsp,
1084 	.aborted_task			= tcm_loop_aborted_task,
1085 	.fabric_make_wwn		= tcm_loop_make_scsi_hba,
1086 	.fabric_drop_wwn		= tcm_loop_drop_scsi_hba,
1087 	.fabric_make_tpg		= tcm_loop_make_naa_tpg,
1088 	.fabric_drop_tpg		= tcm_loop_drop_naa_tpg,
1089 	.fabric_post_link		= tcm_loop_port_link,
1090 	.fabric_pre_unlink		= tcm_loop_port_unlink,
1091 	.tfc_wwn_attrs			= tcm_loop_wwn_attrs,
1092 	.tfc_tpg_base_attrs		= tcm_loop_tpg_attrs,
1093 	.tfc_tpg_attrib_attrs		= tcm_loop_tpg_attrib_attrs,
1094 	.default_compl_type		= TARGET_QUEUE_COMPL,
1095 	.default_submit_type		= TARGET_QUEUE_SUBMIT,
1096 	.direct_submit_supp		= 0,
1097 };
1098 
1099 static int __init tcm_loop_fabric_init(void)
1100 {
1101 	int ret = -ENOMEM;
1102 
1103 	tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1104 				sizeof(struct tcm_loop_cmd),
1105 				__alignof__(struct tcm_loop_cmd),
1106 				0, NULL);
1107 	if (!tcm_loop_cmd_cache) {
1108 		pr_debug("kmem_cache_create() for tcm_loop_cmd_cache failed\n");
1109 		goto out;
1110 	}
1111 
1112 	ret = tcm_loop_alloc_core_bus();
1113 	if (ret)
1114 		goto out_destroy_cache;
1115 
1116 	ret = target_register_template(&loop_ops);
1117 	if (ret)
1118 		goto out_release_core_bus;
1119 
1120 	return 0;
1121 
1122 out_release_core_bus:
1123 	tcm_loop_release_core_bus();
1124 out_destroy_cache:
1125 	kmem_cache_destroy(tcm_loop_cmd_cache);
1126 out:
1127 	return ret;
1128 }
1129 
1130 static void __exit tcm_loop_fabric_exit(void)
1131 {
1132 	target_unregister_template(&loop_ops);
1133 	tcm_loop_release_core_bus();
1134 	kmem_cache_destroy(tcm_loop_cmd_cache);
1135 }
1136 
1137 MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1138 MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
1139 MODULE_LICENSE("GPL");
1140 module_init(tcm_loop_fabric_init);
1141 module_exit(tcm_loop_fabric_exit);
1142