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 <[email protected]>
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 #include <target/target_core_fabric_configfs.h>
38 #include <target/target_core_configfs.h>
39 
40 #include "tcm_loop.h"
41 
42 #define to_tcm_loop_hba(hba)	container_of(hba, struct tcm_loop_hba, dev)
43 
44 static const struct target_core_fabric_ops loop_ops;
45 
46 static struct workqueue_struct *tcm_loop_workqueue;
47 static struct kmem_cache *tcm_loop_cmd_cache;
48 
49 static int tcm_loop_hba_no_cnt;
50 
51 static int tcm_loop_queue_status(struct se_cmd *se_cmd);
52 
53 /*
54  * Called from struct target_core_fabric_ops->check_stop_free()
55  */
56 static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
57 {
58 	/*
59 	 * Do not release struct se_cmd's containing a valid TMR
60 	 * pointer.  These will be released directly in tcm_loop_device_reset()
61 	 * with transport_generic_free_cmd().
62 	 */
63 	if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
64 		return 0;
65 	/*
66 	 * Release the struct se_cmd, which will make a callback to release
67 	 * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd()
68 	 */
69 	transport_generic_free_cmd(se_cmd, 0);
70 	return 1;
71 }
72 
73 static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
74 {
75 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
76 				struct tcm_loop_cmd, tl_se_cmd);
77 
78 	kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
79 }
80 
81 static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host)
82 {
83 	seq_printf(m, "tcm_loop_proc_info()\n");
84 	return 0;
85 }
86 
87 static int tcm_loop_driver_probe(struct device *);
88 static int tcm_loop_driver_remove(struct device *);
89 
90 static int pseudo_lld_bus_match(struct device *dev,
91 				struct device_driver *dev_driver)
92 {
93 	return 1;
94 }
95 
96 static struct bus_type tcm_loop_lld_bus = {
97 	.name			= "tcm_loop_bus",
98 	.match			= pseudo_lld_bus_match,
99 	.probe			= tcm_loop_driver_probe,
100 	.remove			= tcm_loop_driver_remove,
101 };
102 
103 static struct device_driver tcm_loop_driverfs = {
104 	.name			= "tcm_loop",
105 	.bus			= &tcm_loop_lld_bus,
106 };
107 /*
108  * Used with root_device_register() in tcm_loop_alloc_core_bus() below
109  */
110 static struct device *tcm_loop_primary;
111 
112 static void tcm_loop_submission_work(struct work_struct *work)
113 {
114 	struct tcm_loop_cmd *tl_cmd =
115 		container_of(work, struct tcm_loop_cmd, work);
116 	struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
117 	struct scsi_cmnd *sc = tl_cmd->sc;
118 	struct tcm_loop_nexus *tl_nexus;
119 	struct tcm_loop_hba *tl_hba;
120 	struct tcm_loop_tpg *tl_tpg;
121 	struct scatterlist *sgl_bidi = NULL;
122 	u32 sgl_bidi_count = 0, transfer_length;
123 	int rc;
124 
125 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
126 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
127 
128 	/*
129 	 * Ensure that this tl_tpg reference from the incoming sc->device->id
130 	 * has already been configured via tcm_loop_make_naa_tpg().
131 	 */
132 	if (!tl_tpg->tl_hba) {
133 		set_host_byte(sc, DID_NO_CONNECT);
134 		goto out_done;
135 	}
136 	if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) {
137 		set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
138 		goto out_done;
139 	}
140 	tl_nexus = tl_tpg->tl_nexus;
141 	if (!tl_nexus) {
142 		scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
143 				" does not exist\n");
144 		set_host_byte(sc, DID_ERROR);
145 		goto out_done;
146 	}
147 	if (scsi_bidi_cmnd(sc)) {
148 		struct scsi_data_buffer *sdb = scsi_in(sc);
149 
150 		sgl_bidi = sdb->table.sgl;
151 		sgl_bidi_count = sdb->table.nents;
152 		se_cmd->se_cmd_flags |= SCF_BIDI;
153 
154 	}
155 
156 	transfer_length = scsi_transfer_length(sc);
157 	if (!scsi_prot_sg_count(sc) &&
158 	    scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) {
159 		se_cmd->prot_pto = true;
160 		/*
161 		 * loopback transport doesn't support
162 		 * WRITE_GENERATE, READ_STRIP protection
163 		 * information operations, go ahead unprotected.
164 		 */
165 		transfer_length = scsi_bufflen(sc);
166 	}
167 
168 	rc = target_submit_cmd_map_sgls(se_cmd, tl_nexus->se_sess, sc->cmnd,
169 			&tl_cmd->tl_sense_buf[0], tl_cmd->sc->device->lun,
170 			transfer_length, TCM_SIMPLE_TAG,
171 			sc->sc_data_direction, 0,
172 			scsi_sglist(sc), scsi_sg_count(sc),
173 			sgl_bidi, sgl_bidi_count,
174 			scsi_prot_sglist(sc), scsi_prot_sg_count(sc));
175 	if (rc < 0) {
176 		set_host_byte(sc, DID_NO_CONNECT);
177 		goto out_done;
178 	}
179 	return;
180 
181 out_done:
182 	kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
183 	sc->scsi_done(sc);
184 	return;
185 }
186 
187 /*
188  * ->queuecommand can be and usually is called from interrupt context, so
189  * defer the actual submission to a workqueue.
190  */
191 static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
192 {
193 	struct tcm_loop_cmd *tl_cmd;
194 
195 	pr_debug("tcm_loop_queuecommand() %d:%d:%d:%llu got CDB: 0x%02x"
196 		" scsi_buf_len: %u\n", sc->device->host->host_no,
197 		sc->device->id, sc->device->channel, sc->device->lun,
198 		sc->cmnd[0], scsi_bufflen(sc));
199 
200 	tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
201 	if (!tl_cmd) {
202 		pr_err("Unable to allocate struct tcm_loop_cmd\n");
203 		set_host_byte(sc, DID_ERROR);
204 		sc->scsi_done(sc);
205 		return 0;
206 	}
207 
208 	tl_cmd->sc = sc;
209 	tl_cmd->sc_cmd_tag = sc->request->tag;
210 	INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
211 	queue_work(tcm_loop_workqueue, &tl_cmd->work);
212 	return 0;
213 }
214 
215 /*
216  * Called from SCSI EH process context to issue a LUN_RESET TMR
217  * to struct scsi_device
218  */
219 static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg,
220 			      int lun, int task, enum tcm_tmreq_table tmr)
221 {
222 	struct se_cmd *se_cmd = NULL;
223 	struct se_session *se_sess;
224 	struct se_portal_group *se_tpg;
225 	struct tcm_loop_nexus *tl_nexus;
226 	struct tcm_loop_cmd *tl_cmd = NULL;
227 	struct tcm_loop_tmr *tl_tmr = NULL;
228 	int ret = TMR_FUNCTION_FAILED, rc;
229 
230 	/*
231 	 * Locate the tl_nexus and se_sess pointers
232 	 */
233 	tl_nexus = tl_tpg->tl_nexus;
234 	if (!tl_nexus) {
235 		pr_err("Unable to perform device reset without"
236 				" active I_T Nexus\n");
237 		return ret;
238 	}
239 
240 	tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
241 	if (!tl_cmd) {
242 		pr_err("Unable to allocate memory for tl_cmd\n");
243 		return ret;
244 	}
245 
246 	tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
247 	if (!tl_tmr) {
248 		pr_err("Unable to allocate memory for tl_tmr\n");
249 		goto release;
250 	}
251 	init_waitqueue_head(&tl_tmr->tl_tmr_wait);
252 
253 	se_cmd = &tl_cmd->tl_se_cmd;
254 	se_tpg = &tl_tpg->tl_se_tpg;
255 	se_sess = tl_tpg->tl_nexus->se_sess;
256 	/*
257 	 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
258 	 */
259 	transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
260 				DMA_NONE, TCM_SIMPLE_TAG,
261 				&tl_cmd->tl_sense_buf[0]);
262 
263 	rc = core_tmr_alloc_req(se_cmd, tl_tmr, tmr, GFP_KERNEL);
264 	if (rc < 0)
265 		goto release;
266 
267 	if (tmr == TMR_ABORT_TASK)
268 		se_cmd->se_tmr_req->ref_task_tag = task;
269 
270 	/*
271 	 * Locate the underlying TCM struct se_lun
272 	 */
273 	if (transport_lookup_tmr_lun(se_cmd, lun) < 0) {
274 		ret = TMR_LUN_DOES_NOT_EXIST;
275 		goto release;
276 	}
277 	/*
278 	 * Queue the TMR to TCM Core and sleep waiting for
279 	 * tcm_loop_queue_tm_rsp() to wake us up.
280 	 */
281 	transport_generic_handle_tmr(se_cmd);
282 	wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
283 	/*
284 	 * The TMR LUN_RESET has completed, check the response status and
285 	 * then release allocations.
286 	 */
287 	ret = se_cmd->se_tmr_req->response;
288 release:
289 	if (se_cmd)
290 		transport_generic_free_cmd(se_cmd, 1);
291 	else
292 		kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
293 	kfree(tl_tmr);
294 	return ret;
295 }
296 
297 static int tcm_loop_abort_task(struct scsi_cmnd *sc)
298 {
299 	struct tcm_loop_hba *tl_hba;
300 	struct tcm_loop_tpg *tl_tpg;
301 	int ret = FAILED;
302 
303 	/*
304 	 * Locate the tcm_loop_hba_t pointer
305 	 */
306 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
307 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
308 	ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
309 				 sc->request->tag, TMR_ABORT_TASK);
310 	return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
311 }
312 
313 /*
314  * Called from SCSI EH process context to issue a LUN_RESET TMR
315  * to struct scsi_device
316  */
317 static int tcm_loop_device_reset(struct scsi_cmnd *sc)
318 {
319 	struct tcm_loop_hba *tl_hba;
320 	struct tcm_loop_tpg *tl_tpg;
321 	int ret = FAILED;
322 
323 	/*
324 	 * Locate the tcm_loop_hba_t pointer
325 	 */
326 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
327 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
328 
329 	ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
330 				 0, TMR_LUN_RESET);
331 	return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
332 }
333 
334 static int tcm_loop_target_reset(struct scsi_cmnd *sc)
335 {
336 	struct tcm_loop_hba *tl_hba;
337 	struct tcm_loop_tpg *tl_tpg;
338 
339 	/*
340 	 * Locate the tcm_loop_hba_t pointer
341 	 */
342 	tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
343 	if (!tl_hba) {
344 		pr_err("Unable to perform device reset without"
345 				" active I_T Nexus\n");
346 		return FAILED;
347 	}
348 	/*
349 	 * Locate the tl_tpg pointer from TargetID in sc->device->id
350 	 */
351 	tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
352 	if (tl_tpg) {
353 		tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
354 		return SUCCESS;
355 	}
356 	return FAILED;
357 }
358 
359 static int tcm_loop_slave_alloc(struct scsi_device *sd)
360 {
361 	set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
362 	return 0;
363 }
364 
365 static struct scsi_host_template tcm_loop_driver_template = {
366 	.show_info		= tcm_loop_show_info,
367 	.proc_name		= "tcm_loopback",
368 	.name			= "TCM_Loopback",
369 	.queuecommand		= tcm_loop_queuecommand,
370 	.change_queue_depth	= scsi_change_queue_depth,
371 	.eh_abort_handler = tcm_loop_abort_task,
372 	.eh_device_reset_handler = tcm_loop_device_reset,
373 	.eh_target_reset_handler = tcm_loop_target_reset,
374 	.can_queue		= 1024,
375 	.this_id		= -1,
376 	.sg_tablesize		= 256,
377 	.cmd_per_lun		= 1024,
378 	.max_sectors		= 0xFFFF,
379 	.use_clustering		= DISABLE_CLUSTERING,
380 	.slave_alloc		= tcm_loop_slave_alloc,
381 	.module			= THIS_MODULE,
382 	.use_blk_tags		= 1,
383 	.track_queue_depth	= 1,
384 };
385 
386 static int tcm_loop_driver_probe(struct device *dev)
387 {
388 	struct tcm_loop_hba *tl_hba;
389 	struct Scsi_Host *sh;
390 	int error, host_prot;
391 
392 	tl_hba = to_tcm_loop_hba(dev);
393 
394 	sh = scsi_host_alloc(&tcm_loop_driver_template,
395 			sizeof(struct tcm_loop_hba));
396 	if (!sh) {
397 		pr_err("Unable to allocate struct scsi_host\n");
398 		return -ENODEV;
399 	}
400 	tl_hba->sh = sh;
401 
402 	/*
403 	 * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
404 	 */
405 	*((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
406 	/*
407 	 * Setup single ID, Channel and LUN for now..
408 	 */
409 	sh->max_id = 2;
410 	sh->max_lun = 0;
411 	sh->max_channel = 0;
412 	sh->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
413 
414 	host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
415 		    SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
416 		    SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
417 
418 	scsi_host_set_prot(sh, host_prot);
419 	scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC);
420 
421 	error = scsi_add_host(sh, &tl_hba->dev);
422 	if (error) {
423 		pr_err("%s: scsi_add_host failed\n", __func__);
424 		scsi_host_put(sh);
425 		return -ENODEV;
426 	}
427 	return 0;
428 }
429 
430 static int tcm_loop_driver_remove(struct device *dev)
431 {
432 	struct tcm_loop_hba *tl_hba;
433 	struct Scsi_Host *sh;
434 
435 	tl_hba = to_tcm_loop_hba(dev);
436 	sh = tl_hba->sh;
437 
438 	scsi_remove_host(sh);
439 	scsi_host_put(sh);
440 	return 0;
441 }
442 
443 static void tcm_loop_release_adapter(struct device *dev)
444 {
445 	struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
446 
447 	kfree(tl_hba);
448 }
449 
450 /*
451  * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
452  */
453 static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
454 {
455 	int ret;
456 
457 	tl_hba->dev.bus = &tcm_loop_lld_bus;
458 	tl_hba->dev.parent = tcm_loop_primary;
459 	tl_hba->dev.release = &tcm_loop_release_adapter;
460 	dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
461 
462 	ret = device_register(&tl_hba->dev);
463 	if (ret) {
464 		pr_err("device_register() failed for"
465 				" tl_hba->dev: %d\n", ret);
466 		return -ENODEV;
467 	}
468 
469 	return 0;
470 }
471 
472 /*
473  * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
474  * tcm_loop SCSI bus.
475  */
476 static int tcm_loop_alloc_core_bus(void)
477 {
478 	int ret;
479 
480 	tcm_loop_primary = root_device_register("tcm_loop_0");
481 	if (IS_ERR(tcm_loop_primary)) {
482 		pr_err("Unable to allocate tcm_loop_primary\n");
483 		return PTR_ERR(tcm_loop_primary);
484 	}
485 
486 	ret = bus_register(&tcm_loop_lld_bus);
487 	if (ret) {
488 		pr_err("bus_register() failed for tcm_loop_lld_bus\n");
489 		goto dev_unreg;
490 	}
491 
492 	ret = driver_register(&tcm_loop_driverfs);
493 	if (ret) {
494 		pr_err("driver_register() failed for"
495 				"tcm_loop_driverfs\n");
496 		goto bus_unreg;
497 	}
498 
499 	pr_debug("Initialized TCM Loop Core Bus\n");
500 	return ret;
501 
502 bus_unreg:
503 	bus_unregister(&tcm_loop_lld_bus);
504 dev_unreg:
505 	root_device_unregister(tcm_loop_primary);
506 	return ret;
507 }
508 
509 static void tcm_loop_release_core_bus(void)
510 {
511 	driver_unregister(&tcm_loop_driverfs);
512 	bus_unregister(&tcm_loop_lld_bus);
513 	root_device_unregister(tcm_loop_primary);
514 
515 	pr_debug("Releasing TCM Loop Core BUS\n");
516 }
517 
518 static char *tcm_loop_get_fabric_name(void)
519 {
520 	return "loopback";
521 }
522 
523 static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
524 {
525 	struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
526 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
527 	/*
528 	 * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
529 	 * time based on the protocol dependent prefix of the passed configfs group.
530 	 *
531 	 * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
532 	 * ProtocolID using target_core_fabric_lib.c symbols.
533 	 */
534 	switch (tl_hba->tl_proto_id) {
535 	case SCSI_PROTOCOL_SAS:
536 		return sas_get_fabric_proto_ident(se_tpg);
537 	case SCSI_PROTOCOL_FCP:
538 		return fc_get_fabric_proto_ident(se_tpg);
539 	case SCSI_PROTOCOL_ISCSI:
540 		return iscsi_get_fabric_proto_ident(se_tpg);
541 	default:
542 		pr_err("Unknown tl_proto_id: 0x%02x, using"
543 			" SAS emulation\n", tl_hba->tl_proto_id);
544 		break;
545 	}
546 
547 	return sas_get_fabric_proto_ident(se_tpg);
548 }
549 
550 static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
551 {
552 	struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
553 	/*
554 	 * Return the passed NAA identifier for the SAS Target Port
555 	 */
556 	return &tl_tpg->tl_hba->tl_wwn_address[0];
557 }
558 
559 static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
560 {
561 	struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
562 	/*
563 	 * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
564 	 * to represent the SCSI Target Port.
565 	 */
566 	return tl_tpg->tl_tpgt;
567 }
568 
569 static u32 tcm_loop_get_pr_transport_id(
570 	struct se_portal_group *se_tpg,
571 	struct se_node_acl *se_nacl,
572 	struct t10_pr_registration *pr_reg,
573 	int *format_code,
574 	unsigned char *buf)
575 {
576 	struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
577 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
578 
579 	switch (tl_hba->tl_proto_id) {
580 	case SCSI_PROTOCOL_SAS:
581 		return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
582 					format_code, buf);
583 	case SCSI_PROTOCOL_FCP:
584 		return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
585 					format_code, buf);
586 	case SCSI_PROTOCOL_ISCSI:
587 		return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
588 					format_code, buf);
589 	default:
590 		pr_err("Unknown tl_proto_id: 0x%02x, using"
591 			" SAS emulation\n", tl_hba->tl_proto_id);
592 		break;
593 	}
594 
595 	return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
596 			format_code, buf);
597 }
598 
599 static u32 tcm_loop_get_pr_transport_id_len(
600 	struct se_portal_group *se_tpg,
601 	struct se_node_acl *se_nacl,
602 	struct t10_pr_registration *pr_reg,
603 	int *format_code)
604 {
605 	struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
606 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
607 
608 	switch (tl_hba->tl_proto_id) {
609 	case SCSI_PROTOCOL_SAS:
610 		return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
611 					format_code);
612 	case SCSI_PROTOCOL_FCP:
613 		return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
614 					format_code);
615 	case SCSI_PROTOCOL_ISCSI:
616 		return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
617 					format_code);
618 	default:
619 		pr_err("Unknown tl_proto_id: 0x%02x, using"
620 			" SAS emulation\n", tl_hba->tl_proto_id);
621 		break;
622 	}
623 
624 	return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
625 			format_code);
626 }
627 
628 /*
629  * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
630  * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
631  */
632 static char *tcm_loop_parse_pr_out_transport_id(
633 	struct se_portal_group *se_tpg,
634 	const char *buf,
635 	u32 *out_tid_len,
636 	char **port_nexus_ptr)
637 {
638 	struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
639 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
640 
641 	switch (tl_hba->tl_proto_id) {
642 	case SCSI_PROTOCOL_SAS:
643 		return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
644 					port_nexus_ptr);
645 	case SCSI_PROTOCOL_FCP:
646 		return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
647 					port_nexus_ptr);
648 	case SCSI_PROTOCOL_ISCSI:
649 		return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
650 					port_nexus_ptr);
651 	default:
652 		pr_err("Unknown tl_proto_id: 0x%02x, using"
653 			" SAS emulation\n", tl_hba->tl_proto_id);
654 		break;
655 	}
656 
657 	return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
658 			port_nexus_ptr);
659 }
660 
661 /*
662  * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
663  * based upon the incoming fabric dependent SCSI Initiator Port
664  */
665 static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
666 {
667 	return 1;
668 }
669 
670 static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
671 {
672 	return 0;
673 }
674 
675 /*
676  * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
677  * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
678  */
679 static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
680 {
681 	return 0;
682 }
683 
684 /*
685  * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
686  * never be called for TCM_Loop by target_core_fabric_configfs.c code.
687  * It has been added here as a nop for target_fabric_tf_ops_check()
688  */
689 static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
690 {
691 	return 0;
692 }
693 
694 static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg)
695 {
696 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
697 						   tl_se_tpg);
698 	return tl_tpg->tl_fabric_prot_type;
699 }
700 
701 static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
702 {
703 	return 1;
704 }
705 
706 static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
707 {
708 	return 1;
709 }
710 
711 static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
712 {
713 	return;
714 }
715 
716 static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
717 {
718 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
719 			struct tcm_loop_cmd, tl_se_cmd);
720 
721 	return tl_cmd->sc_cmd_tag;
722 }
723 
724 static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
725 {
726 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
727 			struct tcm_loop_cmd, tl_se_cmd);
728 
729 	return tl_cmd->sc_cmd_state;
730 }
731 
732 static int tcm_loop_shutdown_session(struct se_session *se_sess)
733 {
734 	return 0;
735 }
736 
737 static void tcm_loop_close_session(struct se_session *se_sess)
738 {
739 	return;
740 };
741 
742 static int tcm_loop_write_pending(struct se_cmd *se_cmd)
743 {
744 	/*
745 	 * Since Linux/SCSI has already sent down a struct scsi_cmnd
746 	 * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
747 	 * memory, and memory has already been mapped to struct se_cmd->t_mem_list
748 	 * format with transport_generic_map_mem_to_cmd().
749 	 *
750 	 * We now tell TCM to add this WRITE CDB directly into the TCM storage
751 	 * object execution queue.
752 	 */
753 	target_execute_cmd(se_cmd);
754 	return 0;
755 }
756 
757 static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
758 {
759 	return 0;
760 }
761 
762 static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
763 {
764 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
765 				struct tcm_loop_cmd, tl_se_cmd);
766 	struct scsi_cmnd *sc = tl_cmd->sc;
767 
768 	pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
769 		     " cdb: 0x%02x\n", sc, sc->cmnd[0]);
770 
771 	sc->result = SAM_STAT_GOOD;
772 	set_host_byte(sc, DID_OK);
773 	if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
774 	    (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
775 		scsi_set_resid(sc, se_cmd->residual_count);
776 	sc->scsi_done(sc);
777 	return 0;
778 }
779 
780 static int tcm_loop_queue_status(struct se_cmd *se_cmd)
781 {
782 	struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
783 				struct tcm_loop_cmd, tl_se_cmd);
784 	struct scsi_cmnd *sc = tl_cmd->sc;
785 
786 	pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
787 			" cdb: 0x%02x\n", sc, sc->cmnd[0]);
788 
789 	if (se_cmd->sense_buffer &&
790 	   ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
791 	    (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
792 
793 		memcpy(sc->sense_buffer, se_cmd->sense_buffer,
794 				SCSI_SENSE_BUFFERSIZE);
795 		sc->result = SAM_STAT_CHECK_CONDITION;
796 		set_driver_byte(sc, DRIVER_SENSE);
797 	} else
798 		sc->result = se_cmd->scsi_status;
799 
800 	set_host_byte(sc, DID_OK);
801 	if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
802 	    (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
803 		scsi_set_resid(sc, se_cmd->residual_count);
804 	sc->scsi_done(sc);
805 	return 0;
806 }
807 
808 static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
809 {
810 	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
811 	struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
812 	/*
813 	 * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
814 	 * and wake up the wait_queue_head_t in tcm_loop_device_reset()
815 	 */
816 	atomic_set(&tl_tmr->tmr_complete, 1);
817 	wake_up(&tl_tmr->tl_tmr_wait);
818 }
819 
820 static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
821 {
822 	return;
823 }
824 
825 static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
826 {
827 	switch (tl_hba->tl_proto_id) {
828 	case SCSI_PROTOCOL_SAS:
829 		return "SAS";
830 	case SCSI_PROTOCOL_FCP:
831 		return "FCP";
832 	case SCSI_PROTOCOL_ISCSI:
833 		return "iSCSI";
834 	default:
835 		break;
836 	}
837 
838 	return "Unknown";
839 }
840 
841 /* Start items for tcm_loop_port_cit */
842 
843 static int tcm_loop_port_link(
844 	struct se_portal_group *se_tpg,
845 	struct se_lun *lun)
846 {
847 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
848 				struct tcm_loop_tpg, tl_se_tpg);
849 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
850 
851 	atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
852 	/*
853 	 * Add Linux/SCSI struct scsi_device by HCTL
854 	 */
855 	scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
856 
857 	pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
858 	return 0;
859 }
860 
861 static void tcm_loop_port_unlink(
862 	struct se_portal_group *se_tpg,
863 	struct se_lun *se_lun)
864 {
865 	struct scsi_device *sd;
866 	struct tcm_loop_hba *tl_hba;
867 	struct tcm_loop_tpg *tl_tpg;
868 
869 	tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
870 	tl_hba = tl_tpg->tl_hba;
871 
872 	sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
873 				se_lun->unpacked_lun);
874 	if (!sd) {
875 		pr_err("Unable to locate struct scsi_device for %d:%d:"
876 			"%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
877 		return;
878 	}
879 	/*
880 	 * Remove Linux/SCSI struct scsi_device by HCTL
881 	 */
882 	scsi_remove_device(sd);
883 	scsi_device_put(sd);
884 
885 	atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
886 
887 	pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
888 }
889 
890 /* End items for tcm_loop_port_cit */
891 
892 static ssize_t tcm_loop_tpg_attrib_show_fabric_prot_type(
893 	struct se_portal_group *se_tpg,
894 	char *page)
895 {
896 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
897 						   tl_se_tpg);
898 
899 	return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type);
900 }
901 
902 static ssize_t tcm_loop_tpg_attrib_store_fabric_prot_type(
903 	struct se_portal_group *se_tpg,
904 	const char *page,
905 	size_t count)
906 {
907 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
908 						   tl_se_tpg);
909 	unsigned long val;
910 	int ret = kstrtoul(page, 0, &val);
911 
912 	if (ret) {
913 		pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
914 		return ret;
915 	}
916 	if (val != 0 && val != 1 && val != 3) {
917 		pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
918 		return -EINVAL;
919 	}
920 	tl_tpg->tl_fabric_prot_type = val;
921 
922 	return count;
923 }
924 
925 TF_TPG_ATTRIB_ATTR(tcm_loop, fabric_prot_type, S_IRUGO | S_IWUSR);
926 
927 static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = {
928 	&tcm_loop_tpg_attrib_fabric_prot_type.attr,
929 	NULL,
930 };
931 
932 /* Start items for tcm_loop_nexus_cit */
933 
934 static int tcm_loop_make_nexus(
935 	struct tcm_loop_tpg *tl_tpg,
936 	const char *name)
937 {
938 	struct se_portal_group *se_tpg;
939 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
940 	struct tcm_loop_nexus *tl_nexus;
941 	int ret = -ENOMEM;
942 
943 	if (tl_tpg->tl_nexus) {
944 		pr_debug("tl_tpg->tl_nexus already exists\n");
945 		return -EEXIST;
946 	}
947 	se_tpg = &tl_tpg->tl_se_tpg;
948 
949 	tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
950 	if (!tl_nexus) {
951 		pr_err("Unable to allocate struct tcm_loop_nexus\n");
952 		return -ENOMEM;
953 	}
954 	/*
955 	 * Initialize the struct se_session pointer
956 	 */
957 	tl_nexus->se_sess = transport_init_session(
958 				TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS);
959 	if (IS_ERR(tl_nexus->se_sess)) {
960 		ret = PTR_ERR(tl_nexus->se_sess);
961 		goto out;
962 	}
963 	/*
964 	 * Since we are running in 'demo mode' this call with generate a
965 	 * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
966 	 * Initiator port name of the passed configfs group 'name'.
967 	 */
968 	tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
969 				se_tpg, (unsigned char *)name);
970 	if (!tl_nexus->se_sess->se_node_acl) {
971 		transport_free_session(tl_nexus->se_sess);
972 		goto out;
973 	}
974 	/* Now, register the SAS I_T Nexus as active. */
975 	transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
976 			tl_nexus->se_sess, tl_nexus);
977 	tl_tpg->tl_nexus = tl_nexus;
978 	pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
979 		" %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
980 		name);
981 	return 0;
982 
983 out:
984 	kfree(tl_nexus);
985 	return ret;
986 }
987 
988 static int tcm_loop_drop_nexus(
989 	struct tcm_loop_tpg *tpg)
990 {
991 	struct se_session *se_sess;
992 	struct tcm_loop_nexus *tl_nexus;
993 
994 	tl_nexus = tpg->tl_nexus;
995 	if (!tl_nexus)
996 		return -ENODEV;
997 
998 	se_sess = tl_nexus->se_sess;
999 	if (!se_sess)
1000 		return -ENODEV;
1001 
1002 	if (atomic_read(&tpg->tl_tpg_port_count)) {
1003 		pr_err("Unable to remove TCM_Loop I_T Nexus with"
1004 			" active TPG port count: %d\n",
1005 			atomic_read(&tpg->tl_tpg_port_count));
1006 		return -EPERM;
1007 	}
1008 
1009 	pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
1010 		" %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tpg->tl_hba),
1011 		tl_nexus->se_sess->se_node_acl->initiatorname);
1012 	/*
1013 	 * Release the SCSI I_T Nexus to the emulated SAS Target Port
1014 	 */
1015 	transport_deregister_session(tl_nexus->se_sess);
1016 	tpg->tl_nexus = NULL;
1017 	kfree(tl_nexus);
1018 	return 0;
1019 }
1020 
1021 /* End items for tcm_loop_nexus_cit */
1022 
1023 static ssize_t tcm_loop_tpg_show_nexus(
1024 	struct se_portal_group *se_tpg,
1025 	char *page)
1026 {
1027 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1028 			struct tcm_loop_tpg, tl_se_tpg);
1029 	struct tcm_loop_nexus *tl_nexus;
1030 	ssize_t ret;
1031 
1032 	tl_nexus = tl_tpg->tl_nexus;
1033 	if (!tl_nexus)
1034 		return -ENODEV;
1035 
1036 	ret = snprintf(page, PAGE_SIZE, "%s\n",
1037 		tl_nexus->se_sess->se_node_acl->initiatorname);
1038 
1039 	return ret;
1040 }
1041 
1042 static ssize_t tcm_loop_tpg_store_nexus(
1043 	struct se_portal_group *se_tpg,
1044 	const char *page,
1045 	size_t count)
1046 {
1047 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1048 			struct tcm_loop_tpg, tl_se_tpg);
1049 	struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
1050 	unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
1051 	int ret;
1052 	/*
1053 	 * Shutdown the active I_T nexus if 'NULL' is passed..
1054 	 */
1055 	if (!strncmp(page, "NULL", 4)) {
1056 		ret = tcm_loop_drop_nexus(tl_tpg);
1057 		return (!ret) ? count : ret;
1058 	}
1059 	/*
1060 	 * Otherwise make sure the passed virtual Initiator port WWN matches
1061 	 * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
1062 	 * tcm_loop_make_nexus()
1063 	 */
1064 	if (strlen(page) >= TL_WWN_ADDR_LEN) {
1065 		pr_err("Emulated NAA Sas Address: %s, exceeds"
1066 				" max: %d\n", page, TL_WWN_ADDR_LEN);
1067 		return -EINVAL;
1068 	}
1069 	snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
1070 
1071 	ptr = strstr(i_port, "naa.");
1072 	if (ptr) {
1073 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
1074 			pr_err("Passed SAS Initiator Port %s does not"
1075 				" match target port protoid: %s\n", i_port,
1076 				tcm_loop_dump_proto_id(tl_hba));
1077 			return -EINVAL;
1078 		}
1079 		port_ptr = &i_port[0];
1080 		goto check_newline;
1081 	}
1082 	ptr = strstr(i_port, "fc.");
1083 	if (ptr) {
1084 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
1085 			pr_err("Passed FCP Initiator Port %s does not"
1086 				" match target port protoid: %s\n", i_port,
1087 				tcm_loop_dump_proto_id(tl_hba));
1088 			return -EINVAL;
1089 		}
1090 		port_ptr = &i_port[3]; /* Skip over "fc." */
1091 		goto check_newline;
1092 	}
1093 	ptr = strstr(i_port, "iqn.");
1094 	if (ptr) {
1095 		if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
1096 			pr_err("Passed iSCSI Initiator Port %s does not"
1097 				" match target port protoid: %s\n", i_port,
1098 				tcm_loop_dump_proto_id(tl_hba));
1099 			return -EINVAL;
1100 		}
1101 		port_ptr = &i_port[0];
1102 		goto check_newline;
1103 	}
1104 	pr_err("Unable to locate prefix for emulated Initiator Port:"
1105 			" %s\n", i_port);
1106 	return -EINVAL;
1107 	/*
1108 	 * Clear any trailing newline for the NAA WWN
1109 	 */
1110 check_newline:
1111 	if (i_port[strlen(i_port)-1] == '\n')
1112 		i_port[strlen(i_port)-1] = '\0';
1113 
1114 	ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
1115 	if (ret < 0)
1116 		return ret;
1117 
1118 	return count;
1119 }
1120 
1121 TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
1122 
1123 static ssize_t tcm_loop_tpg_show_transport_status(
1124 	struct se_portal_group *se_tpg,
1125 	char *page)
1126 {
1127 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1128 			struct tcm_loop_tpg, tl_se_tpg);
1129 	const char *status = NULL;
1130 	ssize_t ret = -EINVAL;
1131 
1132 	switch (tl_tpg->tl_transport_status) {
1133 	case TCM_TRANSPORT_ONLINE:
1134 		status = "online";
1135 		break;
1136 	case TCM_TRANSPORT_OFFLINE:
1137 		status = "offline";
1138 		break;
1139 	default:
1140 		break;
1141 	}
1142 
1143 	if (status)
1144 		ret = snprintf(page, PAGE_SIZE, "%s\n", status);
1145 
1146 	return ret;
1147 }
1148 
1149 static ssize_t tcm_loop_tpg_store_transport_status(
1150 	struct se_portal_group *se_tpg,
1151 	const char *page,
1152 	size_t count)
1153 {
1154 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1155 			struct tcm_loop_tpg, tl_se_tpg);
1156 
1157 	if (!strncmp(page, "online", 6)) {
1158 		tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
1159 		return count;
1160 	}
1161 	if (!strncmp(page, "offline", 7)) {
1162 		tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
1163 		return count;
1164 	}
1165 	return -EINVAL;
1166 }
1167 
1168 TF_TPG_BASE_ATTR(tcm_loop, transport_status, S_IRUGO | S_IWUSR);
1169 
1170 static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
1171 	&tcm_loop_tpg_nexus.attr,
1172 	&tcm_loop_tpg_transport_status.attr,
1173 	NULL,
1174 };
1175 
1176 /* Start items for tcm_loop_naa_cit */
1177 
1178 static struct se_portal_group *tcm_loop_make_naa_tpg(
1179 	struct se_wwn *wwn,
1180 	struct config_group *group,
1181 	const char *name)
1182 {
1183 	struct tcm_loop_hba *tl_hba = container_of(wwn,
1184 			struct tcm_loop_hba, tl_hba_wwn);
1185 	struct tcm_loop_tpg *tl_tpg;
1186 	int ret;
1187 	unsigned long tpgt;
1188 
1189 	if (strstr(name, "tpgt_") != name) {
1190 		pr_err("Unable to locate \"tpgt_#\" directory"
1191 				" group\n");
1192 		return ERR_PTR(-EINVAL);
1193 	}
1194 	if (kstrtoul(name+5, 10, &tpgt))
1195 		return ERR_PTR(-EINVAL);
1196 
1197 	if (tpgt >= TL_TPGS_PER_HBA) {
1198 		pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA:"
1199 				" %u\n", tpgt, TL_TPGS_PER_HBA);
1200 		return ERR_PTR(-EINVAL);
1201 	}
1202 	tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
1203 	tl_tpg->tl_hba = tl_hba;
1204 	tl_tpg->tl_tpgt = tpgt;
1205 	/*
1206 	 * Register the tl_tpg as a emulated SAS TCM Target Endpoint
1207 	 */
1208 	ret = core_tpg_register(&loop_ops, wwn, &tl_tpg->tl_se_tpg, tl_tpg,
1209 			TRANSPORT_TPG_TYPE_NORMAL);
1210 	if (ret < 0)
1211 		return ERR_PTR(-ENOMEM);
1212 
1213 	pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
1214 		" Target Port %s,t,0x%04lx\n", tcm_loop_dump_proto_id(tl_hba),
1215 		config_item_name(&wwn->wwn_group.cg_item), tpgt);
1216 
1217 	return &tl_tpg->tl_se_tpg;
1218 }
1219 
1220 static void tcm_loop_drop_naa_tpg(
1221 	struct se_portal_group *se_tpg)
1222 {
1223 	struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1224 	struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
1225 				struct tcm_loop_tpg, tl_se_tpg);
1226 	struct tcm_loop_hba *tl_hba;
1227 	unsigned short tpgt;
1228 
1229 	tl_hba = tl_tpg->tl_hba;
1230 	tpgt = tl_tpg->tl_tpgt;
1231 	/*
1232 	 * Release the I_T Nexus for the Virtual SAS link if present
1233 	 */
1234 	tcm_loop_drop_nexus(tl_tpg);
1235 	/*
1236 	 * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
1237 	 */
1238 	core_tpg_deregister(se_tpg);
1239 
1240 	tl_tpg->tl_hba = NULL;
1241 	tl_tpg->tl_tpgt = 0;
1242 
1243 	pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
1244 		" Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1245 		config_item_name(&wwn->wwn_group.cg_item), tpgt);
1246 }
1247 
1248 /* End items for tcm_loop_naa_cit */
1249 
1250 /* Start items for tcm_loop_cit */
1251 
1252 static struct se_wwn *tcm_loop_make_scsi_hba(
1253 	struct target_fabric_configfs *tf,
1254 	struct config_group *group,
1255 	const char *name)
1256 {
1257 	struct tcm_loop_hba *tl_hba;
1258 	struct Scsi_Host *sh;
1259 	char *ptr;
1260 	int ret, off = 0;
1261 
1262 	tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
1263 	if (!tl_hba) {
1264 		pr_err("Unable to allocate struct tcm_loop_hba\n");
1265 		return ERR_PTR(-ENOMEM);
1266 	}
1267 	/*
1268 	 * Determine the emulated Protocol Identifier and Target Port Name
1269 	 * based on the incoming configfs directory name.
1270 	 */
1271 	ptr = strstr(name, "naa.");
1272 	if (ptr) {
1273 		tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
1274 		goto check_len;
1275 	}
1276 	ptr = strstr(name, "fc.");
1277 	if (ptr) {
1278 		tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
1279 		off = 3; /* Skip over "fc." */
1280 		goto check_len;
1281 	}
1282 	ptr = strstr(name, "iqn.");
1283 	if (!ptr) {
1284 		pr_err("Unable to locate prefix for emulated Target "
1285 				"Port: %s\n", name);
1286 		ret = -EINVAL;
1287 		goto out;
1288 	}
1289 	tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
1290 
1291 check_len:
1292 	if (strlen(name) >= TL_WWN_ADDR_LEN) {
1293 		pr_err("Emulated NAA %s Address: %s, exceeds"
1294 			" max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1295 			TL_WWN_ADDR_LEN);
1296 		ret = -EINVAL;
1297 		goto out;
1298 	}
1299 	snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
1300 
1301 	/*
1302 	 * Call device_register(tl_hba->dev) to register the emulated
1303 	 * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
1304 	 * device_register() callbacks in tcm_loop_driver_probe()
1305 	 */
1306 	ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1307 	if (ret)
1308 		goto out;
1309 
1310 	sh = tl_hba->sh;
1311 	tcm_loop_hba_no_cnt++;
1312 	pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
1313 		" %s Address: %s at Linux/SCSI Host ID: %d\n",
1314 		tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
1315 
1316 	return &tl_hba->tl_hba_wwn;
1317 out:
1318 	kfree(tl_hba);
1319 	return ERR_PTR(ret);
1320 }
1321 
1322 static void tcm_loop_drop_scsi_hba(
1323 	struct se_wwn *wwn)
1324 {
1325 	struct tcm_loop_hba *tl_hba = container_of(wwn,
1326 				struct tcm_loop_hba, tl_hba_wwn);
1327 
1328 	pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
1329 		" SAS Address: %s at Linux/SCSI Host ID: %d\n",
1330 		tl_hba->tl_wwn_address, tl_hba->sh->host_no);
1331 	/*
1332 	 * Call device_unregister() on the original tl_hba->dev.
1333 	 * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
1334 	 * release *tl_hba;
1335 	 */
1336 	device_unregister(&tl_hba->dev);
1337 }
1338 
1339 /* Start items for tcm_loop_cit */
1340 static ssize_t tcm_loop_wwn_show_attr_version(
1341 	struct target_fabric_configfs *tf,
1342 	char *page)
1343 {
1344 	return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
1345 }
1346 
1347 TF_WWN_ATTR_RO(tcm_loop, version);
1348 
1349 static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
1350 	&tcm_loop_wwn_version.attr,
1351 	NULL,
1352 };
1353 
1354 /* End items for tcm_loop_cit */
1355 
1356 static const struct target_core_fabric_ops loop_ops = {
1357 	.module				= THIS_MODULE,
1358 	.name				= "loopback",
1359 	.get_fabric_name		= tcm_loop_get_fabric_name,
1360 	.get_fabric_proto_ident		= tcm_loop_get_fabric_proto_ident,
1361 	.tpg_get_wwn			= tcm_loop_get_endpoint_wwn,
1362 	.tpg_get_tag			= tcm_loop_get_tag,
1363 	.tpg_get_pr_transport_id	= tcm_loop_get_pr_transport_id,
1364 	.tpg_get_pr_transport_id_len	= tcm_loop_get_pr_transport_id_len,
1365 	.tpg_parse_pr_out_transport_id	= tcm_loop_parse_pr_out_transport_id,
1366 	.tpg_check_demo_mode		= tcm_loop_check_demo_mode,
1367 	.tpg_check_demo_mode_cache	= tcm_loop_check_demo_mode_cache,
1368 	.tpg_check_demo_mode_write_protect =
1369 				tcm_loop_check_demo_mode_write_protect,
1370 	.tpg_check_prod_mode_write_protect =
1371 				tcm_loop_check_prod_mode_write_protect,
1372 	.tpg_check_prot_fabric_only	= tcm_loop_check_prot_fabric_only,
1373 	.tpg_get_inst_index		= tcm_loop_get_inst_index,
1374 	.check_stop_free		= tcm_loop_check_stop_free,
1375 	.release_cmd			= tcm_loop_release_cmd,
1376 	.shutdown_session		= tcm_loop_shutdown_session,
1377 	.close_session			= tcm_loop_close_session,
1378 	.sess_get_index			= tcm_loop_sess_get_index,
1379 	.write_pending			= tcm_loop_write_pending,
1380 	.write_pending_status		= tcm_loop_write_pending_status,
1381 	.set_default_node_attributes	= tcm_loop_set_default_node_attributes,
1382 	.get_task_tag			= tcm_loop_get_task_tag,
1383 	.get_cmd_state			= tcm_loop_get_cmd_state,
1384 	.queue_data_in			= tcm_loop_queue_data_in,
1385 	.queue_status			= tcm_loop_queue_status,
1386 	.queue_tm_rsp			= tcm_loop_queue_tm_rsp,
1387 	.aborted_task			= tcm_loop_aborted_task,
1388 	.fabric_make_wwn		= tcm_loop_make_scsi_hba,
1389 	.fabric_drop_wwn		= tcm_loop_drop_scsi_hba,
1390 	.fabric_make_tpg		= tcm_loop_make_naa_tpg,
1391 	.fabric_drop_tpg		= tcm_loop_drop_naa_tpg,
1392 	.fabric_post_link		= tcm_loop_port_link,
1393 	.fabric_pre_unlink		= tcm_loop_port_unlink,
1394 	.tfc_wwn_attrs			= tcm_loop_wwn_attrs,
1395 	.tfc_tpg_base_attrs		= tcm_loop_tpg_attrs,
1396 	.tfc_tpg_attrib_attrs		= tcm_loop_tpg_attrib_attrs,
1397 };
1398 
1399 static int __init tcm_loop_fabric_init(void)
1400 {
1401 	int ret = -ENOMEM;
1402 
1403 	tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
1404 	if (!tcm_loop_workqueue)
1405 		goto out;
1406 
1407 	tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
1408 				sizeof(struct tcm_loop_cmd),
1409 				__alignof__(struct tcm_loop_cmd),
1410 				0, NULL);
1411 	if (!tcm_loop_cmd_cache) {
1412 		pr_debug("kmem_cache_create() for"
1413 			" tcm_loop_cmd_cache failed\n");
1414 		goto out_destroy_workqueue;
1415 	}
1416 
1417 	ret = tcm_loop_alloc_core_bus();
1418 	if (ret)
1419 		goto out_destroy_cache;
1420 
1421 	ret = target_register_template(&loop_ops);
1422 	if (ret)
1423 		goto out_release_core_bus;
1424 
1425 	return 0;
1426 
1427 out_release_core_bus:
1428 	tcm_loop_release_core_bus();
1429 out_destroy_cache:
1430 	kmem_cache_destroy(tcm_loop_cmd_cache);
1431 out_destroy_workqueue:
1432 	destroy_workqueue(tcm_loop_workqueue);
1433 out:
1434 	return ret;
1435 }
1436 
1437 static void __exit tcm_loop_fabric_exit(void)
1438 {
1439 	target_unregister_template(&loop_ops);
1440 	tcm_loop_release_core_bus();
1441 	kmem_cache_destroy(tcm_loop_cmd_cache);
1442 	destroy_workqueue(tcm_loop_workqueue);
1443 }
1444 
1445 MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
1446 MODULE_AUTHOR("Nicholas A. Bellinger <[email protected]>");
1447 MODULE_LICENSE("GPL");
1448 module_init(tcm_loop_fabric_init);
1449 module_exit(tcm_loop_fabric_exit);
1450