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