-
Notifications
You must be signed in to change notification settings - Fork 102
/
efct_lio.c
1698 lines (1429 loc) · 44.8 KB
/
efct_lio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2021 Broadcom. All Rights Reserved. The term
* “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
*/
#include <target/target_core_base.h>
#include <target/target_core_fabric.h>
#include "efct_driver.h"
#include "efct_lio.h"
/*
* lio_wq is used to call the LIO backed during creation or deletion of
* sessions. This brings serialization to the session management as we create
* single threaded work queue.
*/
static struct workqueue_struct *lio_wq;
static int
efct_format_wwn(char *str, size_t len, const char *pre, u64 wwn)
{
u8 a[8];
put_unaligned_be64(wwn, a);
return snprintf(str, len, "%s%8phC", pre, a);
}
static int
efct_lio_parse_wwn(const char *name, u64 *wwp, u8 npiv)
{
int num;
u8 b[8];
if (npiv) {
num = sscanf(name,
"%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
&b[0], &b[1], &b[2], &b[3], &b[4], &b[5], &b[6],
&b[7]);
} else {
num = sscanf(name,
"%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
&b[0], &b[1], &b[2], &b[3], &b[4], &b[5], &b[6],
&b[7]);
}
if (num != 8)
return -EINVAL;
*wwp = get_unaligned_be64(b);
return 0;
}
static int
efct_lio_parse_npiv_wwn(const char *name, size_t size, u64 *wwpn, u64 *wwnn)
{
unsigned int cnt = size;
int rc;
*wwpn = *wwnn = 0;
if (name[cnt - 1] == '\n' || name[cnt - 1] == 0)
cnt--;
/* validate we have enough characters for WWPN */
if ((cnt != (16 + 1 + 16)) || (name[16] != ':'))
return -EINVAL;
rc = efct_lio_parse_wwn(&name[0], wwpn, 1);
if (rc)
return rc;
rc = efct_lio_parse_wwn(&name[17], wwnn, 1);
if (rc)
return rc;
return 0;
}
static ssize_t
efct_lio_tpg_enable_show(struct config_item *item, char *page)
{
struct se_portal_group *se_tpg = to_tpg(item);
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
return snprintf(page, PAGE_SIZE, "%d\n", tpg->enabled);
}
static ssize_t
efct_lio_tpg_enable_store(struct config_item *item, const char *page,
size_t count)
{
struct se_portal_group *se_tpg = to_tpg(item);
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
struct efct *efct;
struct efc *efc;
unsigned long op;
if (!tpg->nport || !tpg->nport->efct) {
pr_err("%s: Unable to find EFCT device\n", __func__);
return -EINVAL;
}
efct = tpg->nport->efct;
efc = efct->efcport;
if (kstrtoul(page, 0, &op) < 0)
return -EINVAL;
if (op == 1) {
int ret;
tpg->enabled = true;
efc_log_debug(efct, "enable portal group %d\n", tpg->tpgt);
ret = efct_xport_control(efct->xport, EFCT_XPORT_PORT_ONLINE);
if (ret) {
efct->tgt_efct.lio_nport = NULL;
efc_log_debug(efct, "cannot bring port online\n");
return ret;
}
} else if (op == 0) {
efc_log_debug(efct, "disable portal group %d\n", tpg->tpgt);
if (efc->domain && efc->domain->nport)
efct_scsi_tgt_del_nport(efc, efc->domain->nport);
tpg->enabled = false;
} else {
return -EINVAL;
}
return count;
}
static ssize_t
efct_lio_npiv_tpg_enable_show(struct config_item *item, char *page)
{
struct se_portal_group *se_tpg = to_tpg(item);
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
return snprintf(page, PAGE_SIZE, "%d\n", tpg->enabled);
}
static ssize_t
efct_lio_npiv_tpg_enable_store(struct config_item *item, const char *page,
size_t count)
{
struct se_portal_group *se_tpg = to_tpg(item);
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
struct efct_lio_vport *lio_vport = tpg->vport;
struct efct *efct;
struct efc *efc;
unsigned long op;
if (kstrtoul(page, 0, &op) < 0)
return -EINVAL;
if (!lio_vport) {
pr_err("Unable to find vport\n");
return -EINVAL;
}
efct = lio_vport->efct;
efc = efct->efcport;
if (op == 1) {
tpg->enabled = true;
efc_log_debug(efct, "enable portal group %d\n", tpg->tpgt);
if (efc->domain) {
int ret;
ret = efc_nport_vport_new(efc->domain,
lio_vport->npiv_wwpn,
lio_vport->npiv_wwnn,
U32_MAX, false, true,
NULL, NULL);
if (ret != 0) {
efc_log_err(efct, "Failed to create Vport\n");
return ret;
}
return count;
}
if (!(efc_vport_create_spec(efc, lio_vport->npiv_wwnn,
lio_vport->npiv_wwpn, U32_MAX,
false, true, NULL, NULL)))
return -ENOMEM;
} else if (op == 0) {
efc_log_debug(efct, "disable portal group %d\n", tpg->tpgt);
tpg->enabled = false;
/* only physical nport should exist, free lio_nport
* allocated in efct_lio_make_nport
*/
if (efc->domain) {
efc_nport_vport_del(efct->efcport, efc->domain,
lio_vport->npiv_wwpn,
lio_vport->npiv_wwnn);
return count;
}
} else {
return -EINVAL;
}
return count;
}
static char *efct_lio_get_fabric_wwn(struct se_portal_group *se_tpg)
{
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
return tpg->nport->wwpn_str;
}
static char *efct_lio_get_npiv_fabric_wwn(struct se_portal_group *se_tpg)
{
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
return tpg->vport->wwpn_str;
}
static u16 efct_lio_get_tag(struct se_portal_group *se_tpg)
{
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
return tpg->tpgt;
}
static u16 efct_lio_get_npiv_tag(struct se_portal_group *se_tpg)
{
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
return tpg->tpgt;
}
static int efct_lio_check_demo_mode(struct se_portal_group *se_tpg)
{
return 1;
}
static int efct_lio_check_demo_mode_cache(struct se_portal_group *se_tpg)
{
return 1;
}
static int efct_lio_check_demo_write_protect(struct se_portal_group *se_tpg)
{
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
return tpg->tpg_attrib.demo_mode_write_protect;
}
static int
efct_lio_npiv_check_demo_write_protect(struct se_portal_group *se_tpg)
{
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
return tpg->tpg_attrib.demo_mode_write_protect;
}
static int efct_lio_check_prod_write_protect(struct se_portal_group *se_tpg)
{
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
return tpg->tpg_attrib.prod_mode_write_protect;
}
static int
efct_lio_npiv_check_prod_write_protect(struct se_portal_group *se_tpg)
{
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
return tpg->tpg_attrib.prod_mode_write_protect;
}
static u32 efct_lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
{
return 1;
}
static int efct_lio_check_stop_free(struct se_cmd *se_cmd)
{
struct efct_scsi_tgt_io *ocp =
container_of(se_cmd, struct efct_scsi_tgt_io, cmd);
struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_CHK_STOP_FREE);
return target_put_sess_cmd(se_cmd);
}
static int
efct_lio_abort_tgt_cb(struct efct_io *io,
enum efct_scsi_io_status scsi_status,
u32 flags, void *arg)
{
efct_lio_io_printf(io, "Abort done, status:%d\n", scsi_status);
return 0;
}
static void
efct_lio_aborted_task(struct se_cmd *se_cmd)
{
struct efct_scsi_tgt_io *ocp =
container_of(se_cmd, struct efct_scsi_tgt_io, cmd);
struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_ABORTED_TASK);
if (ocp->rsp_sent)
return;
/* command has been aborted, cleanup here */
ocp->aborting = true;
ocp->err = EFCT_SCSI_STATUS_ABORTED;
/* terminate the exchange */
efct_scsi_tgt_abort_io(io, efct_lio_abort_tgt_cb, NULL);
}
static void efct_lio_release_cmd(struct se_cmd *se_cmd)
{
struct efct_scsi_tgt_io *ocp =
container_of(se_cmd, struct efct_scsi_tgt_io, cmd);
struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
struct efct *efct = io->efct;
efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_RELEASE_CMD);
efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_CMPL_CMD);
efct_scsi_io_complete(io);
atomic_sub_return(1, &efct->tgt_efct.ios_in_use);
}
static void efct_lio_close_session(struct se_session *se_sess)
{
struct efc_node *node = se_sess->fabric_sess_ptr;
pr_debug("se_sess=%p node=%p", se_sess, node);
if (!node) {
pr_debug("node is NULL");
return;
}
efc_node_post_shutdown(node, NULL);
}
static u32 efct_lio_sess_get_index(struct se_session *se_sess)
{
return 0;
}
static void efct_lio_set_default_node_attrs(struct se_node_acl *nacl)
{
}
static int efct_lio_get_cmd_state(struct se_cmd *cmd)
{
struct efct_scsi_tgt_io *ocp =
container_of(cmd, struct efct_scsi_tgt_io, cmd);
struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
if (!io)
return 0;
return io->tgt_io.state;
}
static int
efct_lio_sg_map(struct efct_io *io)
{
struct efct_scsi_tgt_io *ocp = &io->tgt_io;
struct se_cmd *cmd = &ocp->cmd;
ocp->seg_map_cnt = pci_map_sg(io->efct->pci, cmd->t_data_sg,
cmd->t_data_nents, cmd->data_direction);
if (ocp->seg_map_cnt == 0)
return -EFAULT;
return 0;
}
static void
efct_lio_sg_unmap(struct efct_io *io)
{
struct efct_scsi_tgt_io *ocp = &io->tgt_io;
struct se_cmd *cmd = &ocp->cmd;
if (WARN_ON(!ocp->seg_map_cnt || !cmd->t_data_sg))
return;
pci_unmap_sg(io->efct->pci, cmd->t_data_sg,
ocp->seg_map_cnt, cmd->data_direction);
ocp->seg_map_cnt = 0;
}
static int
efct_lio_status_done(struct efct_io *io,
enum efct_scsi_io_status scsi_status,
u32 flags, void *arg)
{
struct efct_scsi_tgt_io *ocp = &io->tgt_io;
efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_RSP_DONE);
if (scsi_status != EFCT_SCSI_STATUS_GOOD) {
efct_lio_io_printf(io, "callback completed with error=%d\n",
scsi_status);
ocp->err = scsi_status;
}
if (ocp->seg_map_cnt)
efct_lio_sg_unmap(io);
efct_lio_io_printf(io, "status=%d, err=%d flags=0x%x, dir=%d\n",
scsi_status, ocp->err, flags, ocp->ddir);
efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_GENERIC_FREE);
transport_generic_free_cmd(&io->tgt_io.cmd, 0);
return 0;
}
static int
efct_lio_datamove_done(struct efct_io *io, enum efct_scsi_io_status scsi_status,
u32 flags, void *arg);
static int
efct_lio_write_pending(struct se_cmd *cmd)
{
struct efct_scsi_tgt_io *ocp =
container_of(cmd, struct efct_scsi_tgt_io, cmd);
struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
struct efct_scsi_sgl *sgl = io->sgl;
struct scatterlist *sg;
u32 flags = 0, cnt, curcnt;
u64 length = 0;
efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_WRITE_PENDING);
efct_lio_io_printf(io, "trans_state=0x%x se_cmd_flags=0x%x\n",
cmd->transport_state, cmd->se_cmd_flags);
if (ocp->seg_cnt == 0) {
ocp->seg_cnt = cmd->t_data_nents;
ocp->cur_seg = 0;
if (efct_lio_sg_map(io)) {
efct_lio_io_printf(io, "efct_lio_sg_map failed\n");
return -EFAULT;
}
}
curcnt = (ocp->seg_map_cnt - ocp->cur_seg);
curcnt = (curcnt < io->sgl_allocated) ? curcnt : io->sgl_allocated;
/* find current sg */
for (cnt = 0, sg = cmd->t_data_sg; cnt < ocp->cur_seg; cnt++,
sg = sg_next(sg))
;/* do nothing */
for (cnt = 0; cnt < curcnt; cnt++, sg = sg_next(sg)) {
sgl[cnt].addr = sg_dma_address(sg);
sgl[cnt].dif_addr = 0;
sgl[cnt].len = sg_dma_len(sg);
length += sgl[cnt].len;
ocp->cur_seg++;
}
if (ocp->cur_seg == ocp->seg_cnt)
flags = EFCT_SCSI_LAST_DATAPHASE;
return efct_scsi_recv_wr_data(io, flags, sgl, curcnt, length,
efct_lio_datamove_done, NULL);
}
static int
efct_lio_queue_data_in(struct se_cmd *cmd)
{
struct efct_scsi_tgt_io *ocp =
container_of(cmd, struct efct_scsi_tgt_io, cmd);
struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
struct efct_scsi_sgl *sgl = io->sgl;
struct scatterlist *sg = NULL;
uint flags = 0, cnt = 0, curcnt = 0;
u64 length = 0;
efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_QUEUE_DATA_IN);
if (ocp->seg_cnt == 0) {
if (cmd->data_length) {
ocp->seg_cnt = cmd->t_data_nents;
ocp->cur_seg = 0;
if (efct_lio_sg_map(io)) {
efct_lio_io_printf(io,
"efct_lio_sg_map failed\n");
return -EAGAIN;
}
} else {
/* If command length is 0, send the response status */
struct efct_scsi_cmd_resp rsp;
memset(&rsp, 0, sizeof(rsp));
efct_lio_io_printf(io,
"cmd : %p length 0, send status\n",
cmd);
return efct_scsi_send_resp(io, 0, &rsp,
efct_lio_status_done, NULL);
}
}
curcnt = min(ocp->seg_map_cnt - ocp->cur_seg, io->sgl_allocated);
while (cnt < curcnt) {
sg = &cmd->t_data_sg[ocp->cur_seg];
sgl[cnt].addr = sg_dma_address(sg);
sgl[cnt].dif_addr = 0;
if (ocp->transferred_len + sg_dma_len(sg) >= cmd->data_length)
sgl[cnt].len = cmd->data_length - ocp->transferred_len;
else
sgl[cnt].len = sg_dma_len(sg);
ocp->transferred_len += sgl[cnt].len;
length += sgl[cnt].len;
ocp->cur_seg++;
cnt++;
if (ocp->transferred_len == cmd->data_length)
break;
}
if (ocp->transferred_len == cmd->data_length) {
flags = EFCT_SCSI_LAST_DATAPHASE;
ocp->seg_cnt = ocp->cur_seg;
}
/* If there is residual, disable Auto Good Response */
if (cmd->residual_count)
flags |= EFCT_SCSI_NO_AUTO_RESPONSE;
efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_SEND_RD_DATA);
return efct_scsi_send_rd_data(io, flags, sgl, curcnt, length,
efct_lio_datamove_done, NULL);
}
static void
efct_lio_send_resp(struct efct_io *io, enum efct_scsi_io_status scsi_status,
u32 flags)
{
struct efct_scsi_cmd_resp rsp;
struct efct_scsi_tgt_io *ocp = &io->tgt_io;
struct se_cmd *cmd = &io->tgt_io.cmd;
int rc;
if (flags & EFCT_SCSI_IO_CMPL_RSP_SENT) {
ocp->rsp_sent = true;
efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_GENERIC_FREE);
transport_generic_free_cmd(&io->tgt_io.cmd, 0);
return;
}
/* send check condition if an error occurred */
memset(&rsp, 0, sizeof(rsp));
rsp.scsi_status = cmd->scsi_status;
rsp.sense_data = (uint8_t *)io->tgt_io.sense_buffer;
rsp.sense_data_length = cmd->scsi_sense_length;
/* Check for residual underrun or overrun */
if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT)
rsp.residual = -cmd->residual_count;
else if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT)
rsp.residual = cmd->residual_count;
rc = efct_scsi_send_resp(io, 0, &rsp, efct_lio_status_done, NULL);
efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_SEND_RSP);
if (rc != 0) {
efct_lio_io_printf(io, "Read done, send rsp failed %d\n", rc);
efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_GENERIC_FREE);
transport_generic_free_cmd(&io->tgt_io.cmd, 0);
} else {
ocp->rsp_sent = true;
}
}
static int
efct_lio_datamove_done(struct efct_io *io, enum efct_scsi_io_status scsi_status,
u32 flags, void *arg)
{
struct efct_scsi_tgt_io *ocp = &io->tgt_io;
efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_DATA_DONE);
if (scsi_status != EFCT_SCSI_STATUS_GOOD) {
efct_lio_io_printf(io, "callback completed with error=%d\n",
scsi_status);
ocp->err = scsi_status;
}
efct_lio_io_printf(io, "seg_map_cnt=%d\n", ocp->seg_map_cnt);
if (ocp->seg_map_cnt) {
if (ocp->err == EFCT_SCSI_STATUS_GOOD &&
ocp->cur_seg < ocp->seg_cnt) {
int rc;
efct_lio_io_printf(io, "continuing cmd at segm=%d\n",
ocp->cur_seg);
if (ocp->ddir == DMA_TO_DEVICE)
rc = efct_lio_write_pending(&ocp->cmd);
else
rc = efct_lio_queue_data_in(&ocp->cmd);
if (!rc)
return 0;
ocp->err = EFCT_SCSI_STATUS_ERROR;
efct_lio_io_printf(io, "could not continue command\n");
}
efct_lio_sg_unmap(io);
}
if (io->tgt_io.aborting) {
efct_lio_io_printf(io, "IO done aborted\n");
return 0;
}
if (ocp->ddir == DMA_TO_DEVICE) {
efct_lio_io_printf(io, "Write done, trans_state=0x%x\n",
io->tgt_io.cmd.transport_state);
if (scsi_status != EFCT_SCSI_STATUS_GOOD) {
transport_generic_request_failure(&io->tgt_io.cmd,
TCM_CHECK_CONDITION_ABORT_CMD);
efct_set_lio_io_state(io,
EFCT_LIO_STATE_TGT_GENERIC_REQ_FAILURE);
} else {
efct_set_lio_io_state(io,
EFCT_LIO_STATE_TGT_EXECUTE_CMD);
target_execute_cmd(&io->tgt_io.cmd);
}
} else {
efct_lio_send_resp(io, scsi_status, flags);
}
return 0;
}
static int
efct_lio_tmf_done(struct efct_io *io, enum efct_scsi_io_status scsi_status,
u32 flags, void *arg)
{
efct_lio_tmfio_printf(io, "cmd=%p status=%d, flags=0x%x\n",
&io->tgt_io.cmd, scsi_status, flags);
efct_set_lio_io_state(io, EFCT_LIO_STATE_TGT_GENERIC_FREE);
transport_generic_free_cmd(&io->tgt_io.cmd, 0);
return 0;
}
static int
efct_lio_null_tmf_done(struct efct_io *tmfio,
enum efct_scsi_io_status scsi_status,
u32 flags, void *arg)
{
efct_lio_tmfio_printf(tmfio, "cmd=%p status=%d, flags=0x%x\n",
&tmfio->tgt_io.cmd, scsi_status, flags);
/* free struct efct_io only, no active se_cmd */
efct_scsi_io_complete(tmfio);
return 0;
}
static int
efct_lio_queue_status(struct se_cmd *cmd)
{
struct efct_scsi_cmd_resp rsp;
struct efct_scsi_tgt_io *ocp =
container_of(cmd, struct efct_scsi_tgt_io, cmd);
struct efct_io *io = container_of(ocp, struct efct_io, tgt_io);
int rc = 0;
efct_set_lio_io_state(io, EFCT_LIO_STATE_TFO_QUEUE_STATUS);
efct_lio_io_printf(io,
"status=0x%x trans_state=0x%x se_cmd_flags=0x%x sns_len=%d\n",
cmd->scsi_status, cmd->transport_state, cmd->se_cmd_flags,
cmd->scsi_sense_length);
memset(&rsp, 0, sizeof(rsp));
rsp.scsi_status = cmd->scsi_status;
rsp.sense_data = (u8 *)io->tgt_io.sense_buffer;
rsp.sense_data_length = cmd->scsi_sense_length;
/* Check for residual underrun or overrun, mark negitive value for
* underrun to recognize in HW
*/
if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT)
rsp.residual = -cmd->residual_count;
else if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT)
rsp.residual = cmd->residual_count;
rc = efct_scsi_send_resp(io, 0, &rsp, efct_lio_status_done, NULL);
efct_set_lio_io_state(io, EFCT_LIO_STATE_SCSI_SEND_RSP);
if (rc == 0)
ocp->rsp_sent = true;
return rc;
}
static void efct_lio_queue_tm_rsp(struct se_cmd *cmd)
{
struct efct_scsi_tgt_io *ocp =
container_of(cmd, struct efct_scsi_tgt_io, cmd);
struct efct_io *tmfio = container_of(ocp, struct efct_io, tgt_io);
struct se_tmr_req *se_tmr = cmd->se_tmr_req;
u8 rspcode;
efct_lio_tmfio_printf(tmfio, "cmd=%p function=0x%x tmr->response=%d\n",
cmd, se_tmr->function, se_tmr->response);
switch (se_tmr->response) {
case TMR_FUNCTION_COMPLETE:
rspcode = EFCT_SCSI_TMF_FUNCTION_COMPLETE;
break;
case TMR_TASK_DOES_NOT_EXIST:
rspcode = EFCT_SCSI_TMF_FUNCTION_IO_NOT_FOUND;
break;
case TMR_LUN_DOES_NOT_EXIST:
rspcode = EFCT_SCSI_TMF_INCORRECT_LOGICAL_UNIT_NUMBER;
break;
case TMR_FUNCTION_REJECTED:
default:
rspcode = EFCT_SCSI_TMF_FUNCTION_REJECTED;
break;
}
efct_scsi_send_tmf_resp(tmfio, rspcode, NULL, efct_lio_tmf_done, NULL);
}
static struct efct *efct_find_wwpn(u64 wwpn)
{
struct efct *efct;
/* Search for the HBA that has this WWPN */
list_for_each_entry(efct, &efct_devices, list_entry) {
if (wwpn == efct_get_wwpn(&efct->hw))
return efct;
}
return NULL;
}
static struct se_wwn *
efct_lio_make_nport(struct target_fabric_configfs *tf,
struct config_group *group, const char *name)
{
struct efct_lio_nport *lio_nport;
struct efct *efct;
int ret;
u64 wwpn;
ret = efct_lio_parse_wwn(name, &wwpn, 0);
if (ret)
return ERR_PTR(ret);
efct = efct_find_wwpn(wwpn);
if (!efct) {
pr_err("cannot find EFCT for base wwpn %s\n", name);
return ERR_PTR(-ENXIO);
}
lio_nport = kzalloc(sizeof(*lio_nport), GFP_KERNEL);
if (!lio_nport)
return ERR_PTR(-ENOMEM);
lio_nport->efct = efct;
lio_nport->wwpn = wwpn;
efct_format_wwn(lio_nport->wwpn_str, sizeof(lio_nport->wwpn_str),
"naa.", wwpn);
efct->tgt_efct.lio_nport = lio_nport;
return &lio_nport->nport_wwn;
}
static struct se_wwn *
efct_lio_npiv_make_nport(struct target_fabric_configfs *tf,
struct config_group *group, const char *name)
{
struct efct_lio_vport *lio_vport;
struct efct *efct;
int ret = -1;
u64 p_wwpn, npiv_wwpn, npiv_wwnn;
char *p, *pbuf, tmp[128];
struct efct_lio_vport_list_t *vport_list;
struct fc_vport *new_fc_vport;
struct fc_vport_identifiers vport_id;
unsigned long flags = 0;
snprintf(tmp, sizeof(tmp), "%s", name);
pbuf = &tmp[0];
p = strsep(&pbuf, "@");
if (!p || !pbuf) {
pr_err("Unable to find separator operator(@)\n");
return ERR_PTR(-EINVAL);
}
ret = efct_lio_parse_wwn(p, &p_wwpn, 0);
if (ret)
return ERR_PTR(ret);
ret = efct_lio_parse_npiv_wwn(pbuf, strlen(pbuf), &npiv_wwpn,
&npiv_wwnn);
if (ret)
return ERR_PTR(ret);
efct = efct_find_wwpn(p_wwpn);
if (!efct) {
pr_err("cannot find EFCT for base wwpn %s\n", name);
return ERR_PTR(-ENXIO);
}
lio_vport = kzalloc(sizeof(*lio_vport), GFP_KERNEL);
if (!lio_vport)
return ERR_PTR(-ENOMEM);
lio_vport->efct = efct;
lio_vport->wwpn = p_wwpn;
lio_vport->npiv_wwpn = npiv_wwpn;
lio_vport->npiv_wwnn = npiv_wwnn;
efct_format_wwn(lio_vport->wwpn_str, sizeof(lio_vport->wwpn_str),
"naa.", npiv_wwpn);
vport_list = kzalloc(sizeof(*vport_list), GFP_KERNEL);
if (!vport_list) {
kfree(lio_vport);
return ERR_PTR(-ENOMEM);
}
vport_list->lio_vport = lio_vport;
memset(&vport_id, 0, sizeof(vport_id));
vport_id.port_name = npiv_wwpn;
vport_id.node_name = npiv_wwnn;
vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
vport_id.vport_type = FC_PORTTYPE_NPIV;
vport_id.disable = false;
new_fc_vport = fc_vport_create(efct->shost, 0, &vport_id);
if (!new_fc_vport) {
efc_log_err(efct, "fc_vport_create failed\n");
kfree(lio_vport);
kfree(vport_list);
return ERR_PTR(-ENOMEM);
}
lio_vport->fc_vport = new_fc_vport;
spin_lock_irqsave(&efct->tgt_efct.efct_lio_lock, flags);
INIT_LIST_HEAD(&vport_list->list_entry);
list_add_tail(&vport_list->list_entry, &efct->tgt_efct.vport_list);
spin_unlock_irqrestore(&efct->tgt_efct.efct_lio_lock, flags);
return &lio_vport->vport_wwn;
}
static void
efct_lio_drop_nport(struct se_wwn *wwn)
{
struct efct_lio_nport *lio_nport =
container_of(wwn, struct efct_lio_nport, nport_wwn);
struct efct *efct = lio_nport->efct;
/* only physical nport should exist, free lio_nport allocated
* in efct_lio_make_nport.
*/
kfree(efct->tgt_efct.lio_nport);
efct->tgt_efct.lio_nport = NULL;
}
static void
efct_lio_npiv_drop_nport(struct se_wwn *wwn)
{
struct efct_lio_vport *lio_vport =
container_of(wwn, struct efct_lio_vport, vport_wwn);
struct efct_lio_vport_list_t *vport, *next_vport;
struct efct *efct = lio_vport->efct;
unsigned long flags = 0;
if (lio_vport->fc_vport)
fc_vport_terminate(lio_vport->fc_vport);
spin_lock_irqsave(&efct->tgt_efct.efct_lio_lock, flags);
list_for_each_entry_safe(vport, next_vport, &efct->tgt_efct.vport_list,
list_entry) {
if (vport->lio_vport == lio_vport) {
list_del(&vport->list_entry);
kfree(vport->lio_vport);
kfree(vport);
break;
}
}
spin_unlock_irqrestore(&efct->tgt_efct.efct_lio_lock, flags);
}
static struct se_portal_group *
efct_lio_make_tpg(struct se_wwn *wwn, const char *name)
{
struct efct_lio_nport *lio_nport =
container_of(wwn, struct efct_lio_nport, nport_wwn);
struct efct_lio_tpg *tpg;
struct efct *efct;
unsigned long n;
int ret;
if (strstr(name, "tpgt_") != name)
return ERR_PTR(-EINVAL);
if (kstrtoul(name + 5, 10, &n) || n > USHRT_MAX)
return ERR_PTR(-EINVAL);
tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
if (!tpg)
return ERR_PTR(-ENOMEM);
tpg->nport = lio_nport;
tpg->tpgt = n;
tpg->enabled = false;
tpg->tpg_attrib.generate_node_acls = 1;
tpg->tpg_attrib.demo_mode_write_protect = 1;
tpg->tpg_attrib.cache_dynamic_acls = 1;
tpg->tpg_attrib.demo_mode_login_only = 1;
tpg->tpg_attrib.session_deletion_wait = 1;
ret = core_tpg_register(wwn, &tpg->tpg, SCSI_PROTOCOL_FCP);
if (ret < 0) {
kfree(tpg);
return NULL;
}
efct = lio_nport->efct;
efct->tgt_efct.tpg = tpg;
efc_log_debug(efct, "create portal group %d\n", tpg->tpgt);
xa_init(&efct->lookup);
return &tpg->tpg;
}
static void
efct_lio_drop_tpg(struct se_portal_group *se_tpg)
{
struct efct_lio_tpg *tpg =
container_of(se_tpg, struct efct_lio_tpg, tpg);
struct efct *efct = tpg->nport->efct;
efc_log_debug(efct, "drop portal group %d\n", tpg->tpgt);
tpg->nport->efct->tgt_efct.tpg = NULL;
core_tpg_deregister(se_tpg);
xa_destroy(&efct->lookup);
kfree(tpg);
}
static struct se_portal_group *
efct_lio_npiv_make_tpg(struct se_wwn *wwn, const char *name)
{
struct efct_lio_vport *lio_vport =
container_of(wwn, struct efct_lio_vport, vport_wwn);
struct efct_lio_tpg *tpg;
struct efct *efct;
unsigned long n;
int ret;
efct = lio_vport->efct;
if (strstr(name, "tpgt_") != name)
return ERR_PTR(-EINVAL);
if (kstrtoul(name + 5, 10, &n) || n > USHRT_MAX)
return ERR_PTR(-EINVAL);
if (n != 1) {
efc_log_err(efct, "Invalid tpgt index: %ld provided\n", n);
return ERR_PTR(-EINVAL);
}
tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
if (!tpg)
return ERR_PTR(-ENOMEM);
tpg->vport = lio_vport;
tpg->tpgt = n;
tpg->enabled = false;
tpg->tpg_attrib.generate_node_acls = 1;
tpg->tpg_attrib.demo_mode_write_protect = 1;
tpg->tpg_attrib.cache_dynamic_acls = 1;
tpg->tpg_attrib.demo_mode_login_only = 1;
tpg->tpg_attrib.session_deletion_wait = 1;
ret = core_tpg_register(wwn, &tpg->tpg, SCSI_PROTOCOL_FCP);
if (ret < 0) {
kfree(tpg);
return NULL;
}
lio_vport->tpg = tpg;
efc_log_debug(efct, "create vport portal group %d\n", tpg->tpgt);