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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
|
/* $NetBSD: sbp2.c,v 1.18 2003/06/29 22:30:18 fvdl Exp $ */
/*
* Copyright (c) 2001,2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by James Chacon.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sbp2.c,v 1.18 2003/06/29 22:30:18 fvdl Exp $");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/lock.h>
#include <machine/bus.h>
#include <machine/limits.h>
#include <dev/std/ieee1212reg.h>
#include <dev/std/ieee1212var.h>
#include <dev/ieee1394/ieee1394reg.h>
#include <dev/ieee1394/ieee1394var.h>
#include <dev/ieee1394/sbp2reg.h>
#include <dev/ieee1394/sbp2var.h>
#include <dev/ieee1394/sbpscsireg.h>
static void sbp2_print_data(struct p1212_data *);
static void sbp2_print_dir(struct p1212_dir *);
static void sbp2_login(struct sbp2 *, struct sbp2_lun *);
static void sbp2_login_ans(struct ieee1394_abuf *, int);
static void sbp2_login_resp(struct ieee1394_abuf *, int);
static struct sbp2_lun *sbp2_alloc_lun(u_int32_t, u_int32_t, u_int32_t,
u_int32_t, u_int32_t);
static void sbp2_logout(struct sbp2 *, struct sbp2_lun *);
static void sbp2_relogin(struct ieee1394_softc *, void *);
static struct sbp2_orb *sbp2_alloc_orb(void);
static void sbp2_free_orb(struct sbp2_orb *);
static void sbp2_alloc_data_mapping(struct sbp2 *, struct sbp2_mapping *,
u_char *, u_int32_t, u_int8_t);
static void sbp2_free_data_mapping(struct sbp2 *, struct sbp2_mapping *);
static u_int64_t sbp2_alloc_addr(struct sbp2 *);
static void sbp2_free_addr(struct sbp2 *, u_int64_t);
static void sbp2_orb_resp(struct ieee1394_abuf *, int);
static void sbp2_data_resp(struct ieee1394_abuf *, int);
static void sbp2_enable_status(struct ieee1394_abuf *, int);
static void sbp2_null_resp(struct ieee1394_abuf *, int);
static void sbp2_doorbell_reset(struct ieee1394_abuf *, int);
static void sbp2_status_resp(struct sbp2_status *, void *);
static struct sbp2_pagetable *sbp2_alloc_pt(struct uio *, u_int8_t,
struct sbp2_orb *);
static void sbp2_pt_resp(struct ieee1394_abuf *, int);
static void sbp2_free_pt(struct sbp2_pagetable *);
#ifdef SBP2_DEBUG
static void sbp2_agent_status(struct ieee1394_abuf *, int);
#endif
static CIRCLEQ_HEAD(, sbp2_orb) sbp2_freeorbs =
CIRCLEQ_HEAD_INITIALIZER(sbp2_freeorbs);
static struct simplelock sbp2_freeorbs_lock = SIMPLELOCK_INITIALIZER;
static struct sbp2_orb status_orb;
static TAILQ_HEAD(, sbp2_map) sbp2_maps =
TAILQ_HEAD_INITIALIZER(sbp2_maps);
static struct simplelock sbp2_maps_lock = SIMPLELOCK_INITIALIZER;
#ifdef SBP2_DEBUG
#define DPRINTF(x) if (sbp2debug) printf x
#define DPRINTFN(n,x) if (sbp2debug>(n)) printf x
int sbp2debug = 3;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif
static void
sbp2_print_data(struct p1212_data *data)
{
switch (data->com.key.key_value) {
case SBP2_KEYVALUE_Command_Set:
printf("SBP2 Command Set: ");
if (data->com.key.val == SBPSCSI_COMMAND_SET)
printf("SCSI 2\n");
else
printf("0x%08x\n", data->val);
break;
case SBP2_KEYVALUE_Unit_Characteristics:
printf("SBP2 Unit Characteristics: 0x%08x\n",data->com.key.val);
break;
case SBP2_KEYVALUE_Command_Set_Revision:
printf("SBP2 Command Set Revision: 0x%08x\n",data->com.key.val);
break;
case SBP2_KEYVALUE_Command_Set_Spec_Id:
printf("SBP2 Command Set Spec Id: 0x%08x\n", data->com.key.val);
break;
case SBP2_KEYVALUE_Firmware_Revision:
printf("SBP2 Firmware Revision: 0x%08x\n", data->com.key.val);
break;
case SBP2_KEYVALUE_Reconnect_Timeout:
printf("SBP2 Reconnect Timeout: 0x%08x\n", data->com.key.val);
break;
case SBP2_KEYVALUE_Unit_Unique_Id:
printf("SBP2 Unit Unique Id: 0x%08x\n", data->com.key.val);
break;
case P1212_KEYVALUE_Unit_Dependent_Info:
if (data->com.key.key_type == P1212_KEYTYPE_Immediate)
printf("SBP2 Logical Unit Number: 0x%08x\n",
data->com.key.val);
else if (data->com.key.key_type == P1212_KEYTYPE_Offset)
printf("SBP2 Management Agent: 0x%08x\n",
data->com.key.val);
break;
default:
printf("Unknown SBP2 key and value: 0x%08x 0x%08x\n",
data->com.key.key_value, data->com.key.val);
break;
}
return;
}
static void
sbp2_print_dir(struct p1212_dir *dir)
{
switch(dir->com.key.key_type) {
case SBP2_KEYVALUE_Logical_Unit_Directory:
printf("Logical Unit\n");
break;
default:
printf("Unknown SBP2 key and value: 0x%08x 0x%08x\n",
dir->com.key.key_value, dir->com.key.val);
break;
}
return;
}
#define VALID_SBP2_IMM(data) { \
if (data->com.key.key_type != P1212_KEYTYPE_Immediate) { \
DPRINTF(("Unknown SBP2 key and value: 0x%08x 0x%08x\n", \
data->com.key.key_value, data->com.key.val)); \
break; \
} \
}
struct sbp2 *
sbp2_init(struct ieee1394_softc *sc, struct p1212_dir *udir)
{
struct sbp2 *sbp2;
struct sbp2_map *sbp2_map;
struct p1212_data *data;
struct p1212_dir *dir;
struct sbp2_lun *lun;
u_int32_t *luns;
int32_t cmd_spec_id, cmd_set, cmd_set_rev;
int i, luncnt, found;
sbp2 = malloc(sizeof (struct sbp2), M_1394DATA, M_WAITOK|M_ZERO);
luns = NULL;
luncnt = 0;
sbp2->sc = sc;
sc->sc1394_callback.sc1394_reset = sbp2_relogin;
sc->sc1394_callback.sc1394_resetarg = sbp2;
simple_lock_init(&sbp2->orblist_lock);
found = 0;
simple_lock(&sbp2_maps_lock);
if (!TAILQ_EMPTY(&sbp2_maps)) {
TAILQ_FOREACH(sbp2_map, &sbp2_maps, map_list) {
if (sbp2_map->sc_bus ==
(struct ieee1394_softc *)sc->sc1394_dev.dv_parent) {
sbp2_map->refcnt++;
found = 1;
sbp2->map = sbp2_map;
break;
}
}
}
simple_unlock(&sbp2_maps_lock);
if (!found) {
sbp2_map = malloc(sizeof (struct sbp2_map), M_1394DATA,
M_WAITOK|M_ZERO);
simple_lock_init(&sbp2_map->maplock);
simple_lock(&sbp2_map->maplock);
simple_lock(&sbp2_maps_lock);
TAILQ_INSERT_TAIL(&sbp2_maps, sbp2_map, map_list);
simple_unlock(&sbp2_maps_lock);
sbp2_map->sc_bus =
(struct ieee1394_softc *)sc->sc1394_dev.dv_parent;
sbp2_map->refcnt = 1;
sbp2->map = sbp2_map;
simple_unlock(&sbp2_map->maplock);
status_orb.cmd.ab_addr = sbp2_alloc_addr(sbp2);
status_orb.cmd.ab_length = SBP2_STATUS_SIZE;
status_orb.cmd.ab_data = malloc(SBP2_STATUS_SIZE, M_1394DATA,
M_WAITOK|M_ZERO);
status_orb.cmd.ab_req = sc;
status_orb.cmd.ab_cb = sbp2_orb_resp;
status_orb.cmd.ab_cbarg = &status_orb;
status_orb.cmd.ab_tcode = IEEE1394_TCODE_WRITE_REQ_BLOCK;
status_orb.state = SBP2_ORB_STATUS_STATE;
status_orb.sbp2 = sbp2;
sc->sc1394_callback.sc1394_inreg(&status_orb.cmd, 0);
}
TAILQ_INIT(&sbp2->luns);
CIRCLEQ_INIT(&sbp2->orbs);
TAILQ_FOREACH(data, &udir->data_root, data) {
switch (data->com.key.key_value) {
case SBP2_KEYVALUE_Command_Set_Spec_Id:
VALID_SBP2_IMM(data);
sbp2->cmd_spec_id = data->com.key.val;
data->print = sbp2_print_data;
break;
case SBP2_KEYVALUE_Command_Set:
VALID_SBP2_IMM(data);
sbp2->cmd_set = data->com.key.val;
data->print = sbp2_print_data;
break;
case SBP2_KEYVALUE_Unit_Characteristics:
VALID_SBP2_IMM(data);
sbp2->orb_size = (data->com.key.val & 0xff);
sbp2->orb_timeout = ((data->com.key.val >> 8) & 0xff);
data->print = sbp2_print_data;
break;
case SBP2_KEYVALUE_Command_Set_Revision:
VALID_SBP2_IMM(data);
sbp2->cmd_set_rev = data->com.key.val;
data->print = sbp2_print_data;
break;
case SBP2_KEYVALUE_Firmware_Revision:
VALID_SBP2_IMM(data);
sbp2->firmware_rev = data->com.key.val;
data->print = sbp2_print_data;
break;
case SBP2_KEYVALUE_Reconnect_Timeout:
VALID_SBP2_IMM(data);
sbp2->max_reconnect = (data->com.key.val & 0xffff);
data->print = sbp2_print_data;
break;
case SBP2_KEYVALUE_Logical_Unit_Number:
/*
* This is either lun or management reg offset
* depending on type.
*/
if (data->com.key.key_type == P1212_KEYTYPE_Immediate) {
/* Save until done with data entries. */
luncnt++;
luns = realloc(luns, sizeof(u_int32_t) * luncnt,
M_1394DATA, M_WAITOK);
luns[luncnt - 1] = data->com.key.val;
data->print = sbp2_print_data;
}
if (data->com.key.key_type == P1212_KEYTYPE_Offset) {
if (data->com.key.val < SBP2_MIN_MGMT_OFFSET) {
DPRINTF(("Management reg offset too "
"small: 0x%08x\n",
data->com.key.val));
break;
}
sbp2->mgmtreg = (data->com.key.val * 4) +
CSR_BASE;
data->print = sbp2_print_data;
}
break;
case SBP2_KEYVALUE_Unit_Unique_Id:
if (data->com.key.key_type != P1212_KEYTYPE_Leaf) {
DPRINTF(("Unknown SBP2 key and value: "
"0x%08x 0x%08x\n", \
data->com.key.key_value,
data->com.key.val)); \
break;
}
#ifdef DIAGNOSTIC
if (data->leafdata == NULL) {
printf ("sbp2: No leaf data?\n");
break;
}
if (data->leafdata->len != 2) {
printf ("sbp2: Unique ID must be length 2\n");
break;
}
#endif
memcpy (&sbp2->uniq_id, &data->leafdata->data[0], 4);
memcpy (&sbp2->uniq_id+4, &data->leafdata->data[1], 4);
data->print = sbp2_print_data;
break;
default:
break;
}
}
for (i = 0; i < luncnt; i++) {
lun = sbp2_alloc_lun(luns[i], sbp2->orb_size,
sbp2->cmd_spec_id, sbp2->cmd_set, sbp2->cmd_set_rev);
sbp2->luncnt++;
TAILQ_INSERT_TAIL(&sbp2->luns, lun, lun_list);
}
free(luns, M_1394DATA);
/*
* A little complicated since entries can come in any order in a ROM
* directory.
*
* For each logical unit directory, scan it for overriding command set
* values. Then go back and for each logical unit found setup a lun
* with the appropriate default or overriden cmd_set values.
*/
TAILQ_FOREACH(dir, &udir->subdir_root, dir) {
if (dir->com.key.key_value ==
SBP2_KEYVALUE_Logical_Unit_Directory) {
dir->print = sbp2_print_dir;
cmd_spec_id = -1;
cmd_set = -1;
cmd_set_rev = -1;
TAILQ_FOREACH(data, &dir->data_root, data) {
switch(data->com.key.key_value) {
case SBP2_KEYVALUE_Command_Set_Spec_Id:
cmd_spec_id = data->com.key.val;
data->print = sbp2_print_data;
break;
case SBP2_KEYVALUE_Command_Set:
cmd_set = data->com.key.val;
data->print = sbp2_print_data;
break;
case SBP2_KEYVALUE_Command_Set_Revision:
cmd_set_rev = data->com.key.val;
data->print = sbp2_print_data;
break;
default:
break;
}
}
if (cmd_spec_id == -1)
cmd_spec_id = sbp2->cmd_spec_id;
if (cmd_set == -1)
cmd_set = sbp2->cmd_set;
if (cmd_set_rev == -1)
cmd_set_rev = sbp2->cmd_set_rev;
TAILQ_FOREACH(data, &dir->data_root, data) {
if (data->com.key.key_value ==
SBP2_KEYVALUE_Logical_Unit_Number) {
lun = sbp2_alloc_lun(data->com.key.val,
sbp2->orb_size, cmd_spec_id,
cmd_set, cmd_set_rev);
TAILQ_INSERT_TAIL(&sbp2->luns, lun,
lun_list);
sbp2->luncnt++;
data->print = sbp2_print_data;
}
}
}
}
return sbp2;
}
#undef VALID_SBP2_IMM
static struct sbp2_lun *
sbp2_alloc_lun(u_int32_t luninfo, u_int32_t orb_size, u_int32_t cmd_spec_id,
u_int32_t cmd_set, u_int32_t cmd_set_rev)
{
struct sbp2_lun *lun;
lun = malloc(sizeof(struct sbp2_lun), M_1394DATA, M_WAITOK|M_ZERO);
lun->cmd_spec_id = cmd_spec_id;
lun->cmd_set = cmd_set;
lun->cmd_set_rev = cmd_set_rev;
lun->lun = luninfo & 0xffff;
lun->ordered = ((luninfo >> 22) & 0x1);
lun->dev_type = ((luninfo >> 16) & 0x1f);
lun->command.ab_data = malloc(orb_size, M_1394DATA, M_WAITOK|M_ZERO);
lun->doorbell.ab_data = malloc(orb_size, M_1394DATA, M_WAITOK|M_ZERO);
lun->status.ab_data = malloc(orb_size, M_1394DATA, M_WAITOK|M_ZERO);
return lun;
}
static void
sbp2_login(struct sbp2 *sbp2, struct sbp2_lun *lun)
{
struct sbp2_orb *orb;
u_int64_t respaddr, orbaddr;
orb = sbp2_alloc_orb();
lun->command.ab_length = 8;
lun->command.ab_req = sbp2->sc;
lun->command.ab_cb = sbp2_null_resp;
lun->command.ab_cbarg = sbp2;
orbaddr = sbp2_alloc_addr(sbp2);
lun->command.ab_addr = sbp2->mgmtreg;
lun->command.ab_data[0] = IEEE1394_CREATE_ADDR_HIGH(orbaddr);
lun->command.ab_data[1] = IEEE1394_CREATE_ADDR_LOW(orbaddr);
lun->command.ab_tcode = IEEE1394_TCODE_WRITE_REQ_BLOCK;
respaddr = sbp2_alloc_addr(sbp2);
orb->cmd.ab_req = sbp2->sc;
orb->cmd.ab_length = SBP2_LOGIN_SIZE;
orb->cmd.ab_tcode = IEEE1394_TCODE_READ_REQ_BLOCK;
orb->cmd.ab_data[0] = 0;
orb->cmd.ab_data[1] = 0;
orb->cmd.ab_data[2] = IEEE1394_CREATE_ADDR_HIGH(respaddr);
orb->cmd.ab_data[3] = IEEE1394_CREATE_ADDR_LOW(respaddr);
orb->cmd.ab_data[4] |= SBP2_LOGIN_SET_EXCLUSIVE;
orb->cmd.ab_data[4] |= SBP2_ORB_NOTIFY_MASK;
/* Ask for reasonable reconnect time. */
orb->cmd.ab_data[4] |= SBP2_LOGIN_SET_RECONNECT(SBP2_RECONNECT);
orb->cmd.ab_data[4] |= lun->loginid;
orb->cmd.ab_data[4] = htonl(orb->cmd.ab_data[4]);
/* Allow max login response. */
orb->cmd.ab_data[5] = htonl(SBP2_LOGIN_MAX_RESP);
orb->cmd.ab_data[6] = IEEE1394_CREATE_ADDR_HIGH(status_orb.cmd.ab_addr);
orb->cmd.ab_data[7] = IEEE1394_CREATE_ADDR_LOW(status_orb.cmd.ab_addr);
orb->cmd.ab_addr = orbaddr;
orb->cmd.ab_cb = sbp2_login_ans;
orb->cmd.ab_cbarg = orb;
orb->sbp2 = sbp2;
lun->doorbell.ab_addr = respaddr;
lun->doorbell.ab_length = SBP2_LOGIN_MAX_RESP;
lun->doorbell.ab_data = malloc (SBP2_LOGIN_MAX_RESP, M_1394DATA,
M_WAITOK|M_ZERO);
lun->doorbell.ab_req = sbp2->sc;
lun->doorbell.ab_cb = sbp2_login_resp;
lun->doorbell.ab_cbarg = lun;
lun->doorbell.ab_tcode = IEEE1394_TCODE_WRITE_REQ_BLOCK;
sbp2->sc->sc1394_callback.sc1394_inreg(&orb->cmd, 0);
sbp2->sc->sc1394_callback.sc1394_inreg(&lun->doorbell, 0);
sbp2->sc->sc1394_callback.sc1394_write(&lun->command);
simple_lock(&sbp2->orblist_lock);
orb = sbp2_alloc_orb();
orb->cmd.ab_addr = sbp2_alloc_addr(sbp2);
orb->cmd.ab_req = sbp2->sc;
orb->cmd.ab_tcode = IEEE1394_TCODE_READ_REQ_BLOCK;
orb->cmd.ab_length = (sbp2->orb_size * 4);
orb->cmd.ab_data[0] = htonl(SBP2_ORB_NULL_POINTER);
orb->cmd.ab_data[4] = htonl(SBP2_ORB_FMT_DUMMY_MASK);
/*
* Go ahead and set the notify bit. Some SBP2 devices always
* seem to return status on a dummy orb regardless so don't 2nd guess.
*/
orb->cmd.ab_data[4] |= htonl(SBP2_ORB_NOTIFY_MASK);
orb->cmd.ab_subok = 1;
orb->cmd.ab_cb = sbp2_orb_resp;
orb->cmd.ab_cbarg = orb;
orb->sbp2 = sbp2;
orb->cb = sbp2_status_resp;
CIRCLEQ_INSERT_HEAD(&sbp2->orbs, orb, orb_list);
simple_unlock(&sbp2->orblist_lock);
sbp2->sc->sc1394_callback.sc1394_inreg(&orb->cmd, 0);
return;
}
static void
sbp2_login_ans(struct ieee1394_abuf *ab, int rcode)
{
struct ieee1394_softc *sc = ab->ab_req;
struct sbp2_orb *orb = ab->ab_cbarg;
/* Got a read so allocate the buffer and write out the response. */
if (orb->state != SBP2_ORB_INIT_STATE) {
orb->cmd.ab_tcode = IEEE1394_TCODE_READ_REQ_BLOCK;
sbp2_free_orb(orb);
return;
}
if (rcode) {
DPRINTF(("sbp2_login: Bad return code: %d\n", rcode));
orb->cmd.ab_tcode = IEEE1394_TCODE_READ_REQ_BLOCK;
sbp2_free_orb(orb);
return;
}
orb->cmd.ab_tcode = IEEE1394_TCODE_READ_RESP_BLOCK;
orb->state = SBP2_ORB_SENT_STATE;
sc->sc1394_callback.sc1394_write(&orb->cmd);
return;
}
static void
sbp2_login_resp(struct ieee1394_abuf *ab, int rcode)
{
struct ieee1394_softc *sc = ab->ab_req;
struct sbp2_lun *lun = ab->ab_cbarg;
struct sbp2_orb *orb;
struct sbp2 *sbp2 = lun->command.ab_cbarg;
#ifdef SBP2_DEBUG
int i;
#endif
sc->sc1394_callback.sc1394_unreg(&lun->doorbell, 1);
if (rcode) {
DPRINTF(("Bad return code: %d\n", rcode));
return;
}
DPRINTF(("csr: 0x%016qx\n", (quad_t)ab->ab_addr));
#ifdef SBP2_DEBUG
if (sbp2debug > 2)
for (i = 0; i < (ab->ab_retlen / 4); i++)
DPRINTF(("%d: 0x%08x\n", i, ntohl(ab->ab_data[i])));
#endif
lun->cmdreg = SBP2_LOGINRESP_CREATE_CMDREG(ntohl(ab->ab_data[1]),
ntohl(ab->ab_data[2]));
if (SBP2_LOGINRESP_GET_LENGTH(ntohl(ab->ab_data[0])) ==
SBP2_LOGIN_MAX_RESP)
lun->reconnect =
SBP2_LOGINRESP_GET_RECONNECT(ntohl(ab->ab_data[3]));
lun->loginid = SBP2_LOGINRESP_GET_LOGINID(ntohl(ab->ab_data[0]));
lun->login_flag = SBP2_LOGGED_IN;
lun->doorbell.ab_addr = lun->cmdreg + SBP2_CMDREG_AGENT_RESET;
lun->doorbell.ab_length = 4;
lun->doorbell.ab_req = sc;
lun->doorbell.ab_cb = sbp2_enable_status;
lun->doorbell.ab_cbarg = lun;
lun->doorbell.ab_tcode = IEEE1394_TCODE_WRITE_REQ_QUAD;
lun->doorbell.ab_data[0] = 0xffffffff;
/*
* Reset the state engine, plug the address of the first orb into
* the orb pointer and off we go.
*/
lun->command.ab_addr = lun->cmdreg + SBP2_CMDREG_ORB_POINTER;
lun->command.ab_length = 8;
lun->command.ab_req = sc;
lun->command.ab_cb = sbp2_doorbell_reset;
lun->command.ab_cbarg = lun;
lun->command.ab_tcode = IEEE1394_TCODE_WRITE_REQ_BLOCK;
simple_lock(&sbp2->orblist_lock);
orb = CIRCLEQ_LAST(&sbp2->orbs);
simple_lock(&orb->orb_lock);
lun->command.ab_data[0] = IEEE1394_CREATE_ADDR_HIGH(orb->cmd.ab_addr);
lun->command.ab_data[1] = IEEE1394_CREATE_ADDR_LOW(orb->cmd.ab_addr);
simple_unlock(&orb->orb_lock);
simple_unlock(&sbp2->orblist_lock);
sc->sc1394_callback.sc1394_write(&lun->doorbell);
sc->sc1394_callback.sc1394_write(&lun->command);
lun->state = SBP2_STATE_ACTIVE;
return;
}
static void
sbp2_enable_status(struct ieee1394_abuf *ab, int rcode)
{
struct sbp2_lun *lun = ab->ab_cbarg;
lun->doorbell.ab_cb = sbp2_doorbell_reset;
lun->doorbell.ab_addr = lun->cmdreg +
SBP2_CMDREG_UNSOLICITED_STATUS_ENABLE;
ab->ab_req->sc1394_callback.sc1394_write(&lun->doorbell);
return;
}
static void
sbp2_doorbell_reset(struct ieee1394_abuf *ab, int rcode)
{
struct sbp2_lun *lun = ab->ab_cbarg;
if (lun->cmdreg)
lun->doorbell.ab_addr = lun->cmdreg + SBP2_CMDREG_DOORBELL;
return;
}
static void
sbp2_null_resp(struct ieee1394_abuf *ab, int rcode)
{
return;
}
static void
sbp2_status_resp(struct sbp2_status *status, void *arg)
{
DPRINTFN(1, ("Got a status response in sbp2_status_resp\n"));
DPRINTFN(1, ("status: resp 0x%04x, sbp_status 0x%04x\n", status->resp,
status->sbp_status));
}
int
sbp2_match(struct p1212_dir *udir)
{
struct p1212_key **keys;
keys = p1212_find(udir, P1212_KEYTYPE_Immediate,
P1212_KEYVALUE_Unit_Spec_Id, 0);
if (keys && keys[0]->val == SBP2_UNIT_SPEC_ID) {
keys = p1212_find(udir, P1212_KEYTYPE_Immediate,
P1212_KEYVALUE_Unit_Sw_Version, 0);
if (keys && keys[0]->val == SBP2_UNIT_SW_VERSION)
return 1;
}
return 0;
}
static void
sbp2_logout(struct sbp2 *sbp2, struct sbp2_lun *lun)
{
DPRINTF(("Called sbp2_logout\n"));
return;
}
void
sbp2_free(struct sbp2 *sbp2)
{
struct sbp2_orb *orb;
struct sbp2_lun *lun;
/*
* Try to send an abort for each active orb.
* sbp2_abort won't remove the last one so stop and do the last
* by hand
*/
while (CIRCLEQ_FIRST(&sbp2->orbs) != (void *)&sbp2->orbs) {
orb = CIRCLEQ_FIRST(&sbp2->orbs);
(void)sbp2_abort(orb);
sbp2_free_orb(orb);
}
orb = CIRCLEQ_FIRST(&sbp2->orbs);
(void)sbp2_abort(orb);
CIRCLEQ_REMOVE(&sbp2->orbs, orb, orb_list);
sbp2_free_orb(orb);
while (TAILQ_FIRST(&sbp2->luns) != NULL) {
lun = TAILQ_FIRST(&sbp2->luns);
TAILQ_REMOVE(&sbp2->luns, lun, lun_list);
if (lun->login_flag)
sbp2_logout(sbp2, lun);
free (lun, M_1394DATA);
}
simple_lock(&sbp2_maps_lock);
if (!--sbp2->map->refcnt) {
TAILQ_REMOVE(&sbp2_maps, sbp2->map, map_list);
free(sbp2->map, M_1394DATA);
}
simple_unlock(&sbp2_maps_lock);
sbp2->sc->sc1394_callback.sc1394_unreg(&status_orb.cmd, 1);
free (status_orb.cmd.ab_data, M_1394DATA);
}
#ifdef FW_DEBUG
extern int fwdebug;
#endif
void *
sbp2_runcmd(struct sbp2 *sbp2, struct sbp2_cmd *cmd)
{
#ifdef DIAGNOSTIC
int found = 0;
#endif
struct sbp2_orb *orb, *toporb;
struct ieee1394_softc *psc =
(struct ieee1394_softc *) sbp2->sc->sc1394_dev.dv_parent;
struct sbp2_lun *lun;
struct uio io;
struct iovec iov;
u_int32_t t;
u_int64_t addr;
#if defined(FW_DEBUG) && defined(SBP2_DEBUG)
if (sbp2debug > 2)
fwdebug = 3;
#endif
TAILQ_FOREACH(lun, &sbp2->luns, lun_list)
if (lun->lun == cmd->lun) {
#ifdef DIAGNOSTIC
found = 1;
#endif
break;
}
#ifdef DIAGNOSTIC
if (!found) {
DPRINTF(("Got a request for an invalid lun: %d\n", cmd->lun));
return NULL;
}
if (cmd->cmdlen % 4) {
DPRINTF(("cmdlen is not 4 byte aligned: %d\n", cmd->cmdlen));
return NULL;
}
if ((cmd->cmdlen / 4) > (sbp2->orb_size - 5)) {
DPRINTF(("cmdlen too large: len - %d max - %d\n",
cmd->cmdlen / 4, sbp2->orb_size - 5));
return NULL;
}
#endif
if (lun->login_flag != SBP2_LOGGED_IN)
sbp2_login(sbp2, lun);
orb = sbp2_alloc_orb();
orb->cmd.ab_addr = sbp2_alloc_addr(sbp2);
orb->cb = cmd->cb;
orb->cb_arg = cmd->cb_arg;
orb->cmd.ab_subok = 1;
orb->cmd.ab_req = sbp2->sc;
orb->cmd.ab_tcode = IEEE1394_TCODE_READ_REQ_BLOCK;
orb->cmd.ab_length = (sbp2->orb_size * 4);
orb->cmd.ab_data[0] = htonl(SBP2_ORB_NULL_POINTER);
orb->cmd.ab_data[4] |= SBP2_ORB_NOTIFY_MASK;
if (cmd->rw == SBP_WRITE)
orb->cmd.ab_data[4] |= SBP2_ORB_RW_MASK;
orb->cmd.ab_data[4] |= SBP2_ORB_SET_SPEED(sbp2->sc->sc1394_link_speed);
orb->cmd.ab_data[4] = htonl(orb->cmd.ab_data[4]);
memcpy(&orb->cmd.ab_data[5], cmd->cmd, cmd->cmdlen);
orb->cmd.ab_cb = sbp2_orb_resp;
orb->cmd.ab_cbarg = orb;
orb->lun = lun;
orb->sbp2 = sbp2;
if (cmd->data) {
if ((cmd->datalen == 0) || (cmd->datalen > SBP2_MAXPHYS)) {
/* Handle uio and large data chunks via page tables. */
if (cmd->datalen) {
io.uio_iov = &iov;
io.uio_iovcnt = 1;
io.uio_offset = 0;
io.uio_resid = cmd->datalen;
io.uio_segflg = UIO_SYSSPACE;
if (cmd->rw == SBP_WRITE)
io.uio_rw = UIO_WRITE;
else
io.uio_rw = UIO_READ;
io.uio_procp = NULL;
iov.iov_base = cmd->data;
iov.iov_len = cmd->datalen;
orb->pt = sbp2_alloc_pt(&io, cmd->rw, orb);
} else
orb->pt =
sbp2_alloc_pt((struct uio *)cmd->data,
cmd->rw, orb);
if (orb->pt == NULL) {
sbp2_free_orb (orb);
return NULL;
}
orb->cmd.ab_data[2] =
IEEE1394_CREATE_ADDR_HIGH(orb->pt->pt_ent.ab_addr);
orb->cmd.ab_data[2] |=
htonl((0xffc0 | psc->sc1394_node_id) << 16);
orb->cmd.ab_data[3] =
IEEE1394_CREATE_ADDR_LOW(orb->pt->pt_ent.ab_addr);
t = SBP2_ORB_SET_MAXTRANS(sbp2->sc->sc1394_link_speed);
orb->cmd.ab_data[4] |= htonl(t);
orb->cmd.ab_data[4] |= htonl(SBP2_ORB_PAGETABLE_MASK);
orb->cmd.ab_data[4] |= htonl(orb->pt->pt_cnt);
} else {
sbp2_alloc_data_mapping(sbp2, &orb->data_map, cmd->data,
cmd->datalen, cmd->rw);
orb->data_map.orb = orb;
orb->data.ab_length = cmd->datalen;
orb->data.ab_addr = orb->data_map.fwaddr;
orb->data.ab_cb = sbp2_data_resp;
orb->data.ab_cbarg = &orb->data_map;
orb->data.ab_data = (u_int32_t *)cmd->data;
orb->data.ab_req = sbp2->sc;
if (cmd->rw == SBP_WRITE)
orb->data.ab_tcode =
IEEE1394_TCODE_WRITE_REQ_BLOCK;
else
orb->data.ab_tcode =
IEEE1394_TCODE_READ_REQ_BLOCK;
orb->cmd.ab_data[2] =
IEEE1394_CREATE_ADDR_HIGH(orb->data.ab_addr);
orb->cmd.ab_data[2] |=
htonl((0xffc0 | psc->sc1394_node_id) << 16);
orb->cmd.ab_data[3] =
IEEE1394_CREATE_ADDR_LOW(orb->data.ab_addr);
t = SBP2_ORB_SET_MAXTRANS(sbp2->sc->sc1394_link_speed);
orb->cmd.ab_data[4] |= htonl(t);
orb->cmd.ab_data[4] |= htonl(cmd->datalen);
sbp2->sc->sc1394_callback.sc1394_inreg(&orb->data, 1);
}
}
simple_lock(&sbp2->orblist_lock);
toporb = CIRCLEQ_FIRST(&sbp2->orbs);
simple_lock(&toporb->orb_lock);
addr = orb->cmd.ab_addr;
toporb->cmd.ab_data[0] = IEEE1394_CREATE_ADDR_HIGH(addr);
toporb->cmd.ab_data[1] = IEEE1394_CREATE_ADDR_LOW(addr);
sbp2->sc->sc1394_callback.sc1394_inreg(&orb->cmd, 0);
if ((lun->state == SBP2_STATE_SUSPENDED) ||
((lun->state == SBP2_STATE_ACTIVE) &&
(toporb->state != SBP2_ORB_INIT_STATE))) {
DPRINTFN(1, ("Ringing doorbell\n"));
toporb->state = SBP2_ORB_INIT_STATE;
toporb->db = 1;
toporb->dback = 0;
sbp2->sc->sc1394_callback.sc1394_write(&lun->doorbell);
} else if (lun->state == SBP2_STATE_DEAD) {
CIRCLEQ_FOREACH(toporb, &orb->sbp2->orbs, orb_list) {
toporb->ack = 0;
toporb->status_rec = 0;
toporb->db = 0;
toporb->dback = 0;
}
toporb = CIRCLEQ_LAST(&sbp2->orbs);
lun->doorbell.ab_addr = lun->cmdreg + SBP2_CMDREG_AGENT_RESET;
lun->doorbell.ab_cb = sbp2_enable_status;
lun->command.ab_data[0] =
IEEE1394_CREATE_ADDR_HIGH(toporb->cmd.ab_addr);
lun->command.ab_data[1] =
IEEE1394_CREATE_ADDR_LOW(toporb->cmd.ab_addr);
toporb->state = SBP2_ORB_INIT_STATE;
sbp2->sc->sc1394_callback.sc1394_write(&lun->doorbell);
sbp2->sc->sc1394_callback.sc1394_write(&lun->command);
}
lun->state = SBP2_STATE_ACTIVE;
CIRCLEQ_INSERT_HEAD(&sbp2->orbs, orb, orb_list);
simple_unlock(&toporb->orb_lock);
simple_unlock(&sbp2->orblist_lock);
return orb;
}
#ifdef SBP2_DEBUG
static void
sbp2_agent_status(struct ieee1394_abuf *abuf, int status)
{
DPRINTF(("sbp2_agent_status: 0x%08x\n", ntohl(abuf->ab_data[0])));
return;
}
#endif
static void
sbp2_orb_resp(struct ieee1394_abuf *abuf, int status)
{
struct sbp2_orb *statorb, *orb;
u_int64_t addr;
int found = 0;
u_int32_t t;
orb = abuf->ab_cbarg;
DPRINTFN(1, ("orb addr: 0x%016qx\n", orb->cmd.ab_addr));
DPRINTFN(1, ("orb next ptr: 0x%08x%08x\n", ntohl(orb->cmd.ab_data[0]), ntohl(orb->cmd.ab_data[1])));
DPRINTFN(1, ("retlen: %d, length: %d\n", abuf->ab_retlen,
abuf->ab_length));
#ifdef SBP2_DEBUG
if ((sbp2debug > 3) && orb->lun) {
orb->lun->status.ab_addr =
orb->lun->cmdreg + SBP2_CMDREG_AGENT_STATE;
orb->lun->status.ab_cb = sbp2_agent_status;
orb->lun->status.ab_cbarg = orb->lun;
orb->lun->status.ab_length = 4;
orb->lun->status.ab_req = orb->sbp2->sc;
orb->lun->status.ab_tcode = IEEE1394_TCODE_READ_REQ_QUAD;
orb->sbp2->sc->sc1394_callback.sc1394_read(&orb->lun->status);
}
#endif
simple_lock(&orb->orb_lock);
switch (orb->state) {
case SBP2_ORB_INIT_STATE:
if (abuf->ab_tcode == IEEE1394_TCODE_READ_REQ_BLOCK) {
orb->state = SBP2_ORB_STATUS_STATE;
abuf->ab_tcode = IEEE1394_TCODE_READ_RESP_BLOCK;
abuf->ab_length = abuf->ab_retlen;
abuf->ab_req->sc1394_callback.sc1394_write(&orb->cmd);
break;
}
/* FALL THRU */
case SBP2_ORB_STATUS_STATE:
/*
* If it's not the fifo addr then just resend the orb out as
* the doorbell was rung to reread.
*/
if (orb->cmd.ab_addr != status_orb.cmd.ab_addr) {
/* An ack from a write */
/*
* Change engine state once this has been processed
* and it's next pointer is the null pointer.
*/
if ((orb->cmd.ab_data[0] ==
ntohl(SBP2_ORB_NULL_POINTER)) &&
(orb->lun->state == SBP2_STATE_ACTIVE))
orb->lun->state = SBP2_STATE_SUSPENDED;
if (orb->ack == 0)
orb->ack = 1;
else if ((orb->db == 1) && (orb->dback == 0))
orb->dback = 1;
else
panic ("Unknown packet received!");
} else {
/*
* The orb passed in is the generic status. Find the
* one it goes with so status can be filled in and
* passed back up.
*/
addr = ntohl(abuf->ab_data[0]);
addr &= 0x0000ffff;
addr = (addr << 32);
addr |= ntohl(abuf->ab_data[1]);
simple_lock(&orb->sbp2->orblist_lock);
CIRCLEQ_FOREACH(statorb, &orb->sbp2->orbs, orb_list) {
if (addr == statorb->cmd.ab_addr) {
found = 1;
break;
}
}
simple_unlock(&orb->sbp2->orblist_lock);
simple_lock(&statorb->orb_lock);
/* XXX: Need to handle unsolicited correctly. */
if (SBP2_STATUS_GET_DEAD(ntohl(abuf->ab_data[0]))) {
DPRINTFN(1, ("Transitioning to dead state\n"));
statorb->lun->state = SBP2_STATE_DEAD;
}
if (!found) {
#ifdef SBP2_DEBUG
u_int32_t i = ntohl(abuf->ab_data[0]);
#endif
DPRINTF(("Got a status block for an unknown "
"orb addr: 0x%016qx\n", addr));
DPRINTF(("resp: 0x%x status: 0x%x len: 0x%x\n",
SBP2_STATUS_GET_RESP(i),
SBP2_STATUS_GET_STATUS(i),
SBP2_STATUS_GET_LEN(i) - 1));
return;
}
/*
* After it's been sent turn this into a dummy orb.
* That way if the engine stalls and has to be
* restarted this orb getting reread won't cause
* duplicate work.
*/
statorb->cmd.ab_data[4] |=
htonl(SBP2_ORB_FMT_DUMMY_MASK);
statorb->status.resp =
SBP2_STATUS_GET_RESP(ntohl(abuf->ab_data[0]));
statorb->status.sbp_status =
SBP2_STATUS_GET_STATUS(ntohl(abuf->ab_data[0]));
statorb->status.datalen =
SBP2_STATUS_GET_LEN(ntohl(abuf->ab_data[0])) - 1;
if (statorb->status.datalen)
statorb->status.data = &abuf->ab_data[2];
if ((statorb->status.resp ==
SBP2_STATUS_TRANSPORT_FAIL) &&
(statorb->status.sbp_status ==
SBP2_STATUS_UNSPEC_ERROR)) {
t = statorb->status.sbp_status;
statorb->status.object =
SBP2_STATUS_GET_OBJECT(t);
statorb->status.bus_error =
SBP2_STATUS_GET_BUS_ERROR(t);
}
statorb->status_rec = 1;
simple_unlock(&statorb->orb_lock);
statorb->cb(&statorb->status, statorb->cb_arg);
statorb->cb = sbp2_status_resp;
orb = statorb;
}
/* If it's not the null pointer orb, free it. */
simple_lock(&orb->orb_lock);
if ((orb->cmd.ab_data[0] == ntohl(SBP2_ORB_NULL_POINTER)))
break;
/* Check conditions for free'ing an orb */
if ((orb->status_rec == 0) || (orb->ack == 0) ||
((orb->db == 1) && (orb->dback == 0)))
break;
simple_lock(&orb->sbp2->orblist_lock);
/*
* Always leave the last orb on the list so the doorbell can
* be rang.
*/
if (orb != CIRCLEQ_FIRST(&orb->sbp2->orbs)) {
orb->cmd.ab_tcode = IEEE1394_TCODE_READ_REQ_BLOCK;
CIRCLEQ_REMOVE(&orb->sbp2->orbs, orb, orb_list);
simple_unlock(&orb->sbp2->orblist_lock);
simple_unlock(&orb->orb_lock);
sbp2_free_orb(orb);
return;
}
simple_unlock(&orb->sbp2->orblist_lock);
break;
case SBP2_ORB_FREE_STATE:
break;
default:
panic("Invalid orb state: %d\n", orb->state);
break;
}
simple_unlock(&orb->orb_lock);
}
static void
sbp2_data_resp(struct ieee1394_abuf *abuf, int rcode)
{
struct sbp2_orb *orb;
struct sbp2_mapping *data_map;
u_int32_t offset;
unsigned char *addr;
switch (abuf->ab_tcode) {
case IEEE1394_TCODE_WRITE_REQ_BLOCK:
return;
break;
case IEEE1394_TCODE_READ_REQ_BLOCK:
data_map = abuf->ab_cbarg;
orb = data_map->orb;
simple_lock(&orb->orb_lock);
addr = data_map->laddr;
offset = abuf->ab_retaddr - data_map->fwaddr;
orb->resp.ab_addr = abuf->ab_retaddr;
orb->resp.ab_tcode = IEEE1394_TCODE_READ_RESP_BLOCK;
orb->resp.ab_tlabel = abuf->ab_tlabel;
orb->resp.ab_length = abuf->ab_retlen;
orb->resp.ab_req = abuf->ab_req;
orb->resp.ab_data = (u_int32_t *)(addr + offset);
orb->resp.ab_cb = sbp2_null_resp;
orb->resp.ab_cbarg = data_map;
orb->sbp2->sc->sc1394_callback.sc1394_write(&orb->resp);
simple_unlock(&orb->orb_lock);
break;
default:
panic("Invalid tcode: 0x%0x\n", abuf->ab_tcode);
}
}
void *
sbp2_abort(void *handle)
{
struct sbp2_orb *orb = handle;
void *arg;
int remove;
DPRINTF(("Called sbp2_abort\n"));
remove = 0;
simple_lock(&orb->sbp2->orblist_lock);
simple_lock(&orb->orb_lock);
arg = orb->cb_arg;
if (orb != CIRCLEQ_FIRST(&orb->sbp2->orbs)) {
remove = 1;
CIRCLEQ_REMOVE(&orb->sbp2->orbs, orb, orb_list);
} else {
orb->state = SBP2_ORB_INIT_STATE;
orb->cmd.ab_data[4] |= htonl(SBP2_ORB_FMT_DUMMY_MASK);
}
simple_unlock(&orb->orb_lock);
simple_unlock(&orb->sbp2->orblist_lock);
if (remove)
sbp2_free_orb(orb);
return arg;
}
static void
sbp2_relogin(struct ieee1394_softc *sc, void *arg)
{
struct sbp2 *sbp2 = arg;
DPRINTF(("Called sbp2_relogin\n"));
if (sbp2)
return;
}
void
sbp2_reset_lun(struct sbp2 *sbp2, u_int16_t lun)
{
DPRINTF(("Called sbp2_reset_lun\n"));
return;
}
static struct sbp2_orb *
sbp2_alloc_orb(void)
{
struct sbp2_orb *orb = NULL;
int i;
simple_lock(&sbp2_freeorbs_lock);
if (CIRCLEQ_EMPTY(&sbp2_freeorbs)) {
DPRINTFN(2, ("Alloc'ing more orbs\n"));
for (i = 0; i < SBP2_NUM_ALLOC; i++) {
simple_unlock(&sbp2_freeorbs_lock);
orb = malloc(sizeof(struct sbp2_orb), M_1394DATA,
M_WAITOK|M_ZERO);
orb->cmd.ab_data = malloc(SBP2_MAX_ORB, M_1394DATA,
M_WAITOK|M_ZERO);
simple_lock_init(&orb->orb_lock);
simple_lock(&sbp2_freeorbs_lock);
CIRCLEQ_INSERT_TAIL(&sbp2_freeorbs, orb, orb_list);
}
simple_unlock(&sbp2_freeorbs_lock);
orb = malloc(sizeof(struct sbp2_orb), M_DEVBUF,
M_WAITOK|M_ZERO);
orb->cmd.ab_data = malloc(SBP2_MAX_ORB, M_1394DATA,
M_WAITOK|M_ZERO);
simple_lock_init(&orb->orb_lock);
return orb;
} else {
orb = CIRCLEQ_FIRST(&sbp2_freeorbs);
CIRCLEQ_REMOVE(&sbp2_freeorbs, orb, orb_list);
}
simple_unlock(&sbp2_freeorbs_lock);
orb->state = SBP2_ORB_INIT_STATE;
return orb;
}
static void
sbp2_free_orb(struct sbp2_orb *orb)
{
simple_lock(&orb->orb_lock);
DPRINTFN(2, ("Freeing orb at addr: 0x%016qx status_rec: 0x%0x\n",
orb->cmd.ab_addr, orb->status_rec));
orb->sbp2->sc->sc1394_callback.sc1394_unreg(&orb->cmd, 0);
if (orb->data_map.laddr) {
orb->sbp2->sc->sc1394_callback.sc1394_unreg(&orb->data, 1);
sbp2_free_data_mapping(orb->sbp2, &orb->data_map);
}
if (orb->pt)
sbp2_free_pt(orb->pt);
sbp2_free_addr(orb->sbp2, orb->cmd.ab_addr);
simple_lock(&sbp2_freeorbs_lock);
memset(orb->cmd.ab_data, 0, SBP2_MAX_ORB);
memset(&orb->status, 0, sizeof(struct sbp2_status));
memset(&orb->data_map, 0, sizeof(struct sbp2_mapping));
orb->sbp2 = NULL;
orb->pt = NULL;
orb->lun = NULL;
orb->cb = NULL;
orb->cb_arg = NULL;
orb->status_rec = 0;
orb->db = 0;
orb->dback = 0;
orb->ack = 0;
orb->state = SBP2_ORB_FREE_STATE;
CIRCLEQ_INSERT_TAIL(&sbp2_freeorbs, orb, orb_list);
simple_unlock(&sbp2_freeorbs_lock);
simple_unlock(&orb->orb_lock);
}
static void
sbp2_alloc_data_mapping(struct sbp2 *sbp2, struct sbp2_mapping *map,
u_char *data, u_int32_t datalen, u_int8_t rw)
{
int byte, bitpos, found;
u_int32_t size, count, startbyte, startbit;
unsigned char bit;
size = datalen / SBP_DATA_BLOCK_SIZE;
if (datalen % SBP_DATA_BLOCK_SIZE)
size++;
map->laddr = data;
map->size = datalen;
map->rw = rw;
simple_lock(&sbp2->map->maplock);
count = found = 0;
startbyte = 0;
startbit = 0;
for (byte = 0; byte < sizeof(sbp2->map->datamap); byte++) {
for (bitpos = 0; bitpos < CHAR_BIT; bitpos++) {
bit = 0x1 << bitpos;
if ((sbp2->map->datamap[byte] & bit) == 0) {
if (++count == size) {
found = 1;
break;
}
} else {
count = 0;
if (bitpos != (CHAR_BIT - 1)) {
startbyte = byte;
startbit = bitpos + 1;
} else {
startbyte = byte + 1;
startbit = 0;
}
}
}
if (found)
break;
}
/* Gets a little complicated to handle crossing bytes on the ends. */
/* Handle the bits on the front end if they start in the middle */
if (startbit) {
count = CHAR_BIT - startbit;
if (size < count)
count = size;
for (bitpos = 0; bitpos < count; bitpos++) {
bit = 0x1 << (bitpos + startbit);
size--;
sbp2->map->datamap[startbyte] |= bit;
}
startbyte++;
}
/* Allocate bytes at a time */
if (size) {
count = startbyte + (size / CHAR_BIT);
for (byte = startbyte; byte < count; byte++) {
sbp2->map->datamap[byte] = 0xff;
size -= CHAR_BIT;
}
/* If any bits are left allocate them out of the next byte */
if (size) {
#ifdef DEBUG
if (size >= CHAR_BIT)
panic ("Too many bits left to allocate: %d",
size);
#endif
for (bitpos = 0; bitpos < size; bitpos++) {
bit = 0x1 << bitpos;
sbp2->map->datamap[byte] |= bit;
}
}
}
/* Adjust back one if the bits started 1 byte back */
if (startbit)
startbyte--;
map->fwaddr = SBP_DATA_BEG +
(((startbyte * CHAR_BIT) + startbit) * SBP_DATA_BLOCK_SIZE);
simple_unlock(&sbp2->map->maplock);
}
static u_int64_t
sbp2_alloc_addr(struct sbp2 *sbp2)
{
u_int64_t addr;
int byte, bitpos, found;
unsigned char bit;
found = 0;
simple_lock(&sbp2->map->maplock);
addr = SBP_ADDR_BEG + (sbp2->map->next_addr * SBP_ADDR_BLOCK_SIZE);
byte = sbp2->map->next_addr / CHAR_BIT;
bitpos = sbp2->map->next_addr % CHAR_BIT;
bit = 0x1 << bitpos;
#ifdef DIAGNOSTIC
if (sbp2->map->addrmap[byte] & bit)
panic("Already allocated address 0x%016" PRIx64, addr);
#endif
sbp2->map->addrmap[byte] |= bit;
for (; byte < (sizeof(sbp2->map->addrmap) - byte); byte++) {
for (bitpos = 0; bitpos < CHAR_BIT; bitpos++) {
bit = 0x1 << bitpos;
if ((sbp2->map->addrmap[byte] & bit) == 0) {
found = 1;
break;
}
}
if (found)
break;
}
sbp2->map->next_addr = (byte * CHAR_BIT) + bitpos;
simple_unlock(&sbp2->map->maplock);
#ifdef DIAGNOSTIC
if (sbp2->map->next_addr >= SBP_ADDR_MAX)
panic("XXX: Used 64k of sbp addr's.\n");
#endif
return addr;
}
static void
sbp2_free_data_mapping(struct sbp2 *sbp2, struct sbp2_mapping *map)
{
int byte, bitpos;
u_int32_t size, count, startbyte, off, startbit;
unsigned char bit;
simple_lock(&sbp2->map->maplock);
size = map->size / SBP_DATA_BLOCK_SIZE;
if (map->size % SBP_DATA_BLOCK_SIZE)
size++;
off = ((int)(map->fwaddr - SBP_DATA_BEG) / SBP_DATA_BLOCK_SIZE);
startbyte = off / CHAR_BIT;
startbit = off % CHAR_BIT;
/*
* 3 parts. Any bits in the middle of the first byte.
* Then bytes until whole bytes are done.
* Finally, any left over remaining bits.
*/
if (startbit) {
count = CHAR_BIT - startbit;
if (size < count)
count = size;
for (bitpos = 0; bitpos < count; bitpos++) {
bit = 0x1 << (bitpos + startbit);
#ifdef DIAGNOSTIC
if (!(sbp2->map->datamap[startbyte] & bit))
panic("Freeing addr not allocated: 0x%016"
PRIx64, map->fwaddr);
#endif
bit = ~bit;
size--;
sbp2->map->datamap[startbyte] &= bit;
}
startbyte++;
}
if (size) {
count = startbyte + (size / CHAR_BIT);
for (byte = startbyte; byte < count; byte++) {
#ifdef DIAGNOSTIC
if (!(sbp2->map->datamap[byte]))
panic("Freeing addr not allocated: 0x%016"
PRIx64, map->fwaddr);
#endif
size -= CHAR_BIT;
sbp2->map->datamap[byte] = 0;
}
/* If any bits are left free them out of the next byte */
if (size) {
#ifdef DEBUG
if (size >= CHAR_BIT)
panic ("Too many bits left to free: %d", size);
#endif
for (bitpos = 0; bitpos < size; bitpos++) {
bit = 0x1 << bitpos;
#ifdef DIAGNOSTIC
if (!(sbp2->map->datamap[byte] & bit))
panic("Freeing addr not allocated: "
"0x%016" PRIx64, map->fwaddr);
#endif
bit = ~bit;
sbp2->map->datamap[byte] &= bit;
}
}
}
if (startbit)
startbyte--;
simple_unlock(&sbp2->map->maplock);
return;
}
static void
sbp2_free_addr(struct sbp2 *sbp2, u_int64_t addr)
{
int off, byte, bitpos;
unsigned char bit;
off = ((int)(addr - SBP_ADDR_BEG) / SBP_ADDR_BLOCK_SIZE);
byte = off / CHAR_BIT;
bitpos = off % CHAR_BIT;
bit = 0x1 << bitpos;
simple_lock(&sbp2->map->maplock);
#ifdef DIAGNOSTIC
if (!(sbp2->map->addrmap[byte] & bit))
panic("Freeing addr not allocated: 0x%016" PRIx64, addr);
#endif
bit = ~bit;
sbp2->map->addrmap[byte] &= bit;
if (sbp2->map->next_addr > off)
sbp2->map->next_addr = off;
simple_unlock(&sbp2->map->maplock);
}
static struct sbp2_pagetable *
sbp2_alloc_pt(struct uio *io, u_int8_t rw, struct sbp2_orb *orb)
{
struct sbp2_pagetable *pt;
struct sbp2_mapping *map;
struct ieee1394_softc *sc;
ssize_t len;
char *addr;
int i, j, k, cnt;
pt = malloc (sizeof (*pt), M_1394DATA, M_ZERO | M_WAITOK);
/* Compute number of entries. */
for (i = 0; i < io->uio_iovcnt; i++) {
if (io->uio_iov[i].iov_len <= SBP2_PHYS_SEGMENT)
pt->pt_cnt++;
else {
pt->pt_cnt +=
io->uio_iov[i].iov_len / SBP2_PHYS_SEGMENT;
if (io->uio_iov[i].iov_len % SBP2_PHYS_SEGMENT)
pt->pt_cnt++;
}
#ifdef DEBUG
/* Overflow'd 16 bits. */
if (pt->pt_cnt == 0) {
DPRINTFN(1, ("pt_cnt overflow\n"));
free (pt, M_1394DATA);
return NULL;
}
#endif
}
sc = orb->sbp2->sc;
pt->pt_ent.ab_data = malloc (SBP2_PTENT_SIZE * pt->pt_cnt, M_1394DATA,
M_ZERO | M_WAITOK);
sbp2_alloc_data_mapping(orb->sbp2, &pt->pt_map,
(char *)pt->pt_ent.ab_data, SBP2_PTENT_SIZE * pt->pt_cnt, SBP_READ);
pt->pt_ent.ab_addr = pt->pt_map.fwaddr;
pt->pt_ent.ab_length = SBP2_PTENT_SIZE * pt->pt_cnt;
pt->pt_ent.ab_tcode = IEEE1394_TCODE_READ_REQ_BLOCK;
pt->pt_ent.ab_req = sc;
pt->pt_ent.ab_cb = sbp2_pt_resp;
pt->pt_ent.ab_cbarg = orb;
pt->pt_data = malloc (sizeof (struct ieee1394_abuf) * pt->pt_cnt,
M_1394DATA, M_ZERO | M_WAITOK);
j = 0;
for (i = 0; i < io->uio_iovcnt; i++) {
if (io->uio_iov[i].iov_len <= SBP2_PHYS_SEGMENT) {
cnt = 1;
} else {
cnt = io->uio_iov[i].iov_len / SBP2_PHYS_SEGMENT;
if (io->uio_iov[i].iov_len % SBP2_PHYS_SEGMENT)
cnt++;
}
len = io->uio_iov[i].iov_len;
for (k = 0; k < cnt; k++) {
#ifdef DIAGNOSTIC
if (len == 0)
panic ("len is zero");
#endif
map = malloc (sizeof (struct sbp2_mapping), M_1394DATA,
M_ZERO | M_WAITOK);
pt->pt_data[j].ab_cbarg = map;
addr = (char *)io->uio_iov[i].iov_base +
(k * SBP2_PHYS_SEGMENT);
if (len > SBP2_PHYS_SEGMENT) {
len -= SBP2_PHYS_SEGMENT;
sbp2_alloc_data_mapping(orb->sbp2, map, addr,
SBP2_PHYS_SEGMENT, rw);
pt->pt_data[j].ab_length = SBP2_PHYS_SEGMENT;
pt->pt_ent.ab_data[j * SBP2_PTENT_SIZEQ] =
SBP2_PT_MAKELEN(SBP2_PHYS_SEGMENT);
} else {
sbp2_alloc_data_mapping(orb->sbp2, map, addr,
len, rw);
pt->pt_data[j].ab_length = len;
pt->pt_ent.ab_data[j * SBP2_PTENT_SIZEQ] =
SBP2_PT_MAKELEN(len);
len -= len;
}
pt->pt_data[j].ab_addr = map->fwaddr;
pt->pt_data[j].ab_req = sc;
if (rw == SBP_WRITE)
pt->pt_data[j].ab_tcode =
IEEE1394_TCODE_WRITE_REQ_BLOCK;
else
pt->pt_data[j].ab_tcode =
IEEE1394_TCODE_READ_REQ_BLOCK;
map->orb = orb;
pt->pt_data[j].ab_cb = sbp2_data_resp;
pt->pt_data[j].ab_data = (u_int32_t *)addr;
pt->pt_ent.ab_data[j * SBP2_PTENT_SIZEQ] |=
IEEE1394_CREATE_ADDR_HIGH(map->fwaddr);
pt->pt_ent.ab_data[(j * SBP2_PTENT_SIZEQ) + 1] =
IEEE1394_CREATE_ADDR_LOW(map->fwaddr);
sc->sc1394_callback.sc1394_inreg(&pt->pt_data[j], 1);
j++;
}
}
sc->sc1394_callback.sc1394_inreg(&pt->pt_ent, 1);
return pt;
}
static void
sbp2_pt_resp(struct ieee1394_abuf *abuf, int rcode)
{
struct sbp2_orb *orb = abuf->ab_cbarg;
struct sbp2_pagetable *pt = orb->pt;
u_int32_t offset;
if (rcode) {
DPRINTF(("sbp2_pt_resp: Bad return code: %d\n", rcode));
return;
}
simple_lock(&orb->orb_lock);
/*
* The target is allowed to read these 1 entry at a time so construct
* responses in a different abuf to allow for this.
*/
offset = abuf->ab_retaddr - abuf->ab_addr;
pt->pt_resp.ab_addr = abuf->ab_retaddr;
pt->pt_resp.ab_tcode = IEEE1394_TCODE_READ_RESP_BLOCK;
pt->pt_resp.ab_tlabel = abuf->ab_tlabel;
pt->pt_resp.ab_length = abuf->ab_retlen;
pt->pt_resp.ab_req = abuf->ab_req;
pt->pt_resp.ab_data = (u_int32_t *)((u_int8_t *)abuf->ab_data + offset);
pt->pt_resp.ab_cb = sbp2_null_resp;
pt->pt_resp.ab_cbarg = orb;
abuf->ab_req->sc1394_callback.sc1394_write(&pt->pt_resp);
simple_unlock(&orb->orb_lock);
return;
}
static void
sbp2_free_pt(struct sbp2_pagetable *pt)
{
struct sbp2_orb *orb = pt->pt_ent.ab_cbarg;
struct ieee1394_softc *sc = orb->sbp2->sc;
int i;
sc->sc1394_callback.sc1394_unreg(&pt->pt_ent, 1);
sbp2_free_data_mapping(orb->sbp2, &pt->pt_map);
for (i = 0; i < pt->pt_cnt; i++) {
sc->sc1394_callback.sc1394_unreg(&pt->pt_data[i], 1);
sbp2_free_data_mapping(orb->sbp2, pt->pt_data[i].ab_cbarg);
free (pt->pt_data[i].ab_cbarg, M_1394DATA);
}
free (pt->pt_data, M_1394DATA);
free (pt->pt_ent.ab_data, M_1394DATA);
free (pt, M_1394DATA);
}
|