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