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