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