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