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
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
|
/* $NetBSD: sbp.c,v 1.42 2022/04/12 21:05:37 andvar Exp $ */
/*-
* Copyright (c) 2003 Hidetoshi Shimokawa
* Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
* All rights reserved.
*
* 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 acknowledgement as bellow:
*
* This product includes software developed by K. Kobayashi and H. Shimokawa
*
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*
* $FreeBSD: src/sys/dev/firewire/sbp.c,v 1.100 2009/02/18 18:41:34 sbruno Exp $
*
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sbp.c,v 1.42 2022/04/12 21:05:37 andvar Exp $");
#include <sys/param.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/buf.h>
#include <sys/callout.h>
#include <sys/condvar.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <sys/bus.h>
#include <dev/scsipi/scsi_spc.h>
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsiconf.h>
#include <dev/scsipi/scsipiconf.h>
#include <dev/ieee1394/firewire.h>
#include <dev/ieee1394/firewirereg.h>
#include <dev/ieee1394/fwdma.h>
#include <dev/ieee1394/iec13213.h>
#include <dev/ieee1394/sbp.h>
#include "locators.h"
#define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
&& crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
#define SBP_NUM_TARGETS 8 /* MAX 64 */
#define SBP_NUM_LUNS 64
#define SBP_MAXPHYS MIN(MAXPHYS, (512*1024) /* 512KB */)
#define SBP_DMA_SIZE PAGE_SIZE
#define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
#define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
#define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
/*
* STATUS FIFO addressing
* bit
* -----------------------
* 0- 1( 2): 0 (alignment)
* 2- 9( 8): lun
* 10-31(14): unit
* 32-47(16): SBP_BIND_HI
* 48-64(16): bus_id, node_id
*/
#define SBP_BIND_HI 0x1
#define SBP_DEV2ADDR(u, l) \
(((uint64_t)SBP_BIND_HI << 32) |\
(((u) & 0x3fff) << 10) |\
(((l) & 0xff) << 2))
#define SBP_ADDR2UNIT(a) (((a) >> 10) & 0x3fff)
#define SBP_ADDR2LUN(a) (((a) >> 2) & 0xff)
#define SBP_INITIATOR 7
static const char *orb_fun_name[] = {
ORB_FUN_NAMES
};
static int debug = 0;
static int auto_login = 1;
static int max_speed = -1;
static int sbp_cold = 1;
static int ex_login = 1;
static int login_delay = 1000; /* msec */
static int scan_delay = 500; /* msec */
static int use_doorbell = 0;
static int sbp_tags = 0;
static int sysctl_sbp_verify(SYSCTLFN_PROTO, int lower, int upper);
static int sysctl_sbp_verify_max_speed(SYSCTLFN_PROTO);
static int sysctl_sbp_verify_tags(SYSCTLFN_PROTO);
/*
* Setup sysctl(3) MIB, hw.sbp.*
*
* TBD condition CTLFLAG_PERMANENT on being a module or not
*/
SYSCTL_SETUP(sysctl_sbp, "sysctl sbp(4) subtree setup")
{
int rc, sbp_node_num;
const struct sysctlnode *node;
if ((rc = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT, CTLTYPE_NODE, "sbp",
SYSCTL_DESCR("sbp controls"), NULL, 0, NULL,
0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
goto err;
sbp_node_num = node->sysctl_num;
/* sbp auto login flag */
if ((rc = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
"auto_login", SYSCTL_DESCR("SBP perform login automatically"),
NULL, 0, &auto_login,
0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
goto err;
/* sbp max speed */
if ((rc = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
"max_speed", SYSCTL_DESCR("SBP transfer max speed"),
sysctl_sbp_verify_max_speed, 0, &max_speed,
0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
goto err;
/* sbp exclusive login flag */
if ((rc = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
"exclusive_login", SYSCTL_DESCR("SBP enable exclusive login"),
NULL, 0, &ex_login,
0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
goto err;
/* sbp login delay */
if ((rc = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
"login_delay", SYSCTL_DESCR("SBP login delay in msec"),
NULL, 0, &login_delay,
0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
goto err;
/* sbp scan delay */
if ((rc = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
"scan_delay", SYSCTL_DESCR("SBP scan delay in msec"),
NULL, 0, &scan_delay,
0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
goto err;
/* sbp use doorbell flag */
if ((rc = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
"use_doorbell", SYSCTL_DESCR("SBP use doorbell request"),
NULL, 0, &use_doorbell,
0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
goto err;
/* sbp force tagged queuing */
if ((rc = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
"tags", SYSCTL_DESCR("SBP tagged queuing support"),
sysctl_sbp_verify_tags, 0, &sbp_tags,
0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
goto err;
/* sbp driver debug flag */
if ((rc = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
"sbp_debug", SYSCTL_DESCR("SBP debug flag"),
NULL, 0, &debug,
0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
goto err;
return;
err:
aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
}
static int
sysctl_sbp_verify(SYSCTLFN_ARGS, int lower, int upper)
{
int error, t;
struct sysctlnode node;
node = *rnode;
t = *(int*)rnode->sysctl_data;
node.sysctl_data = &t;
error = sysctl_lookup(SYSCTLFN_CALL(&node));
if (error || newp == NULL)
return error;
if (t < lower || t > upper)
return EINVAL;
*(int*)rnode->sysctl_data = t;
return 0;
}
static int
sysctl_sbp_verify_max_speed(SYSCTLFN_ARGS)
{
return sysctl_sbp_verify(SYSCTLFN_CALL(rnode), 0, FWSPD_S400);
}
static int
sysctl_sbp_verify_tags(SYSCTLFN_ARGS)
{
return sysctl_sbp_verify(SYSCTLFN_CALL(rnode), -1, 1);
}
#define NEED_RESPONSE 0
#define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
#ifdef __sparc64__ /* iommu */
#define SBP_IND_MAX howmany(SBP_MAXPHYS, SBP_SEG_MAX)
#else
#define SBP_IND_MAX howmany(SBP_MAXPHYS, PAGE_SIZE)
#endif
struct sbp_ocb {
uint32_t orb[8];
#define IND_PTR_OFFSET (sizeof(uint32_t) * 8)
struct ind_ptr ind_ptr[SBP_IND_MAX];
struct scsipi_xfer *xs;
struct sbp_dev *sdev;
uint16_t index;
uint16_t flags; /* XXX should be removed */
bus_dmamap_t dmamap;
bus_addr_t bus_addr;
STAILQ_ENTRY(sbp_ocb) ocb;
};
#define SBP_ORB_DMA_SYNC(dma, i, op) \
bus_dmamap_sync((dma).dma_tag, (dma).dma_map, \
sizeof(struct sbp_ocb) * (i), \
sizeof(ocb->orb) + sizeof(ocb->ind_ptr), (op));
#define OCB_ACT_MGM 0
#define OCB_ACT_CMD 1
#define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo))
struct sbp_dev{
#define SBP_DEV_RESET 0 /* accept login */
#define SBP_DEV_LOGIN 1 /* to login */
#if 0
#define SBP_DEV_RECONN 2 /* to reconnect */
#endif
#define SBP_DEV_TOATTACH 3 /* to attach */
#define SBP_DEV_PROBE 4 /* scan lun */
#define SBP_DEV_ATTACHED 5 /* in operation */
#define SBP_DEV_DEAD 6 /* unavailable unit */
#define SBP_DEV_RETRY 7 /* unavailable unit */
uint8_t status:4,
timeout:4;
uint8_t type;
uint16_t lun_id;
uint16_t freeze;
#define ORB_LINK_DEAD (1 << 0)
#define VALID_LUN (1 << 1)
#define ORB_POINTER_ACTIVE (1 << 2)
#define ORB_POINTER_NEED (1 << 3)
#define ORB_DOORBELL_ACTIVE (1 << 4)
#define ORB_DOORBELL_NEED (1 << 5)
#define ORB_SHORTAGE (1 << 6)
uint16_t flags;
struct scsipi_periph *periph;
struct sbp_target *target;
struct fwdma_alloc dma;
struct sbp_login_res *login;
struct callout login_callout;
struct sbp_ocb *ocb;
STAILQ_HEAD(, sbp_ocb) ocbs;
STAILQ_HEAD(, sbp_ocb) free_ocbs;
struct sbp_ocb *last_ocb;
char vendor[32];
char product[32];
char revision[10];
char bustgtlun[32];
};
struct sbp_target {
int target_id;
int num_lun;
struct sbp_dev **luns;
struct sbp_softc *sbp;
struct fw_device *fwdev;
uint32_t mgm_hi, mgm_lo;
struct sbp_ocb *mgm_ocb_cur;
STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
struct callout mgm_ocb_timeout;
STAILQ_HEAD(, fw_xfer) xferlist;
int n_xfer;
};
struct sbp_softc {
struct firewire_dev_comm sc_fd;
struct scsipi_adapter sc_adapter;
struct scsipi_channel sc_channel;
device_t sc_bus;
struct lwp *sc_lwp;
struct sbp_target sc_target;
struct fw_bind sc_fwb;
bus_dma_tag_t sc_dmat;
struct timeval sc_last_busreset;
int sc_flags;
kmutex_t sc_mtx;
kcondvar_t sc_cv;
};
MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/IEEE1394");
MALLOC_DECLARE(M_SBP);
static int sbpmatch(device_t, cfdata_t, void *);
static void sbpattach(device_t, device_t, void *);
static int sbpdetach(device_t, int);
static void sbp_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
void *);
static void sbp_minphys(struct buf *);
static void sbp_show_sdev_info(struct sbp_dev *);
static void sbp_alloc_lun(struct sbp_target *);
static struct sbp_target *sbp_alloc_target(struct sbp_softc *,
struct fw_device *);
static void sbp_probe_lun(struct sbp_dev *);
static void sbp_login_callout(void *);
static void sbp_login(struct sbp_dev *);
static void sbp_probe_target(void *);
static void sbp_post_busreset(void *);
static void sbp_post_explore(void *);
#if NEED_RESPONSE
static void sbp_loginres_callback(struct fw_xfer *);
#endif
static inline void sbp_xfer_free(struct fw_xfer *);
static void sbp_reset_start_callback(struct fw_xfer *);
static void sbp_reset_start(struct sbp_dev *);
static void sbp_mgm_callback(struct fw_xfer *);
static void sbp_scsipi_scan_target(void *);
static inline void sbp_scan_dev(struct sbp_dev *);
static void sbp_do_attach(struct fw_xfer *);
static void sbp_agent_reset_callback(struct fw_xfer *);
static void sbp_agent_reset(struct sbp_dev *);
static void sbp_busy_timeout_callback(struct fw_xfer *);
static void sbp_busy_timeout(struct sbp_dev *);
static void sbp_orb_pointer_callback(struct fw_xfer *);
static void sbp_orb_pointer(struct sbp_dev *, struct sbp_ocb *);
static void sbp_doorbell_callback(struct fw_xfer *);
static void sbp_doorbell(struct sbp_dev *);
static struct fw_xfer *sbp_write_cmd(struct sbp_dev *, int, int);
static void sbp_mgm_orb(struct sbp_dev *, int, struct sbp_ocb *);
static void sbp_print_scsi_cmd(struct sbp_ocb *);
static void sbp_scsi_status(struct sbp_status *, struct sbp_ocb *);
static void sbp_fix_inq_data(struct sbp_ocb *);
static void sbp_recv(struct fw_xfer *);
static int sbp_logout_all(struct sbp_softc *);
static void sbp_free_sdev(struct sbp_dev *);
static void sbp_free_target(struct sbp_target *);
static void sbp_scsipi_detach_sdev(struct sbp_dev *);
static void sbp_scsipi_detach_target(struct sbp_target *);
static void sbp_target_reset(struct sbp_dev *, int);
static void sbp_mgm_timeout(void *);
static void sbp_timeout(void *);
static void sbp_action1(struct sbp_softc *, struct scsipi_xfer *);
static void sbp_execute_ocb(struct sbp_ocb *, bus_dma_segment_t *, int);
static struct sbp_ocb *sbp_dequeue_ocb(struct sbp_dev *, struct sbp_status *);
static struct sbp_ocb *sbp_enqueue_ocb(struct sbp_dev *, struct sbp_ocb *);
static struct sbp_ocb *sbp_get_ocb(struct sbp_dev *);
static void sbp_free_ocb(struct sbp_dev *, struct sbp_ocb *);
static void sbp_abort_ocb(struct sbp_ocb *, int);
static void sbp_abort_all_ocbs(struct sbp_dev *, int);
static const char *orb_status0[] = {
/* 0 */ "No additional information to report",
/* 1 */ "Request type not supported",
/* 2 */ "Speed not supported",
/* 3 */ "Page size not supported",
/* 4 */ "Access denied",
/* 5 */ "Logical unit not supported",
/* 6 */ "Maximum payload too small",
/* 7 */ "Reserved for future standardization",
/* 8 */ "Resources unavailable",
/* 9 */ "Function rejected",
/* A */ "Login ID not recognized",
/* B */ "Dummy ORB completed",
/* C */ "Request aborted",
/* FF */ "Unspecified error"
#define MAX_ORB_STATUS0 0xd
};
static const char *orb_status1_object[] = {
/* 0 */ "Operation request block (ORB)",
/* 1 */ "Data buffer",
/* 2 */ "Page table",
/* 3 */ "Unable to specify"
};
static const char *orb_status1_serial_bus_error[] = {
/* 0 */ "Missing acknowledge",
/* 1 */ "Reserved; not to be used",
/* 2 */ "Time-out error",
/* 3 */ "Reserved; not to be used",
/* 4 */ "Busy retry limit exceeded(X)",
/* 5 */ "Busy retry limit exceeded(A)",
/* 6 */ "Busy retry limit exceeded(B)",
/* 7 */ "Reserved for future standardization",
/* 8 */ "Reserved for future standardization",
/* 9 */ "Reserved for future standardization",
/* A */ "Reserved for future standardization",
/* B */ "Tardy retry limit exceeded",
/* C */ "Conflict error",
/* D */ "Data error",
/* E */ "Type error",
/* F */ "Address error"
};
CFATTACH_DECL_NEW(sbp, sizeof(struct sbp_softc),
sbpmatch, sbpattach, sbpdetach, NULL);
int
sbpmatch(device_t parent, cfdata_t cf, void *aux)
{
struct fw_attach_args *fwa = aux;
if (strcmp(fwa->name, "sbp") == 0)
return 1;
return 0;
}
static void
sbpattach(device_t parent, device_t self, void *aux)
{
struct sbp_softc *sc = device_private(self);
struct fw_attach_args *fwa = (struct fw_attach_args *)aux;
struct firewire_comm *fc;
struct scsipi_adapter *sc_adapter = &sc->sc_adapter;
struct scsipi_channel *sc_channel = &sc->sc_channel;
struct sbp_target *target = &sc->sc_target;
int dv_unit;
aprint_naive("\n");
aprint_normal(": SBP-2/SCSI over IEEE1394\n");
sc->sc_fd.dev = self;
if (cold)
sbp_cold++;
sc->sc_fd.fc = fc = fwa->fc;
mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_VM);
cv_init(&sc->sc_cv, "sbp");
if (max_speed < 0)
max_speed = fc->speed;
sc->sc_dmat = fc->dmat;
sc->sc_target.fwdev = NULL;
sc->sc_target.luns = NULL;
/* Initialize mutexes and lists before we can error out
* to prevent crashes on detach
*/
mutex_init(&sc->sc_fwb.fwb_mtx, MUTEX_DEFAULT, IPL_VM);
STAILQ_INIT(&sc->sc_fwb.xferlist);
if (sbp_alloc_target(sc, fwa->fwdev) == NULL)
return;
sc_adapter->adapt_dev = sc->sc_fd.dev;
sc_adapter->adapt_nchannels = 1;
sc_adapter->adapt_max_periph = 1;
sc_adapter->adapt_request = sbp_scsipi_request;
sc_adapter->adapt_minphys = sbp_minphys;
sc_adapter->adapt_openings = 8;
sc_channel->chan_adapter = sc_adapter;
sc_channel->chan_bustype = &scsi_bustype;
sc_channel->chan_defquirks = PQUIRK_ONLYBIG;
sc_channel->chan_channel = 0;
sc_channel->chan_flags = SCSIPI_CHAN_CANGROW | SCSIPI_CHAN_NOSETTLE;
sc_channel->chan_ntargets = 1;
sc_channel->chan_nluns = target->num_lun; /* We set nluns 0 now */
sc_channel->chan_id = 1;
sc->sc_bus = config_found(sc->sc_fd.dev, sc_channel, scsiprint,
CFARGS_NONE);
if (sc->sc_bus == NULL) {
aprint_error_dev(self, "attach failed\n");
return;
}
/* We reserve 16 bit space (4 bytes X 64 unit X 256 luns) */
dv_unit = device_unit(sc->sc_fd.dev);
sc->sc_fwb.start = SBP_DEV2ADDR(dv_unit, 0);
sc->sc_fwb.end = SBP_DEV2ADDR(dv_unit, -1);
/* pre-allocate xfer */
fw_xferlist_add(&sc->sc_fwb.xferlist, M_SBP,
/*send*/ 0, /*recv*/ SBP_RECV_LEN, SBP_NUM_OCB / 2,
fc, (void *)sc, sbp_recv);
fw_bindadd(fc, &sc->sc_fwb);
sc->sc_fd.post_busreset = sbp_post_busreset;
sc->sc_fd.post_explore = sbp_post_explore;
if (fc->status != FWBUSNOTREADY) {
sbp_post_busreset((void *)sc);
sbp_post_explore((void *)sc);
}
}
static int
sbpdetach(device_t self, int flags)
{
struct sbp_softc *sc = device_private(self);
struct firewire_comm *fc = sc->sc_fd.fc;
sbp_scsipi_detach_target(&sc->sc_target);
if (sc->sc_target.fwdev && SBP_FWDEV_ALIVE(sc->sc_target.fwdev)) {
sbp_logout_all(sc);
/* XXX wait for logout completion */
mutex_enter(&sc->sc_mtx);
cv_timedwait_sig(&sc->sc_cv, &sc->sc_mtx, hz/2);
mutex_exit(&sc->sc_mtx);
}
sbp_free_target(&sc->sc_target);
fw_bindremove(fc, &sc->sc_fwb);
fw_xferlist_remove(&sc->sc_fwb.xferlist);
mutex_destroy(&sc->sc_fwb.fwb_mtx);
mutex_destroy(&sc->sc_mtx);
cv_destroy(&sc->sc_cv);
return 0;
}
static void
sbp_scsipi_request(struct scsipi_channel *channel, scsipi_adapter_req_t req,
void *arg)
{
struct sbp_softc *sc = device_private(channel->chan_adapter->adapt_dev);
struct scsipi_xfer *xs = arg;
int i;
SBP_DEBUG(1)
printf("Called sbp_scsipi_request\n");
END_DEBUG
switch (req) {
case ADAPTER_REQ_RUN_XFER:
SBP_DEBUG(1)
printf("Got req_run_xfer\n");
printf("xs control: 0x%08x, timeout: %d\n",
xs->xs_control, xs->timeout);
printf("opcode: 0x%02x\n", (int)xs->cmd->opcode);
for (i = 0; i < 15; i++)
printf("0x%02x ",(int)xs->cmd->bytes[i]);
printf("\n");
END_DEBUG
if (xs->xs_control & XS_CTL_RESET) {
SBP_DEBUG(1)
printf("XS_CTL_RESET not support\n");
END_DEBUG
break;
}
#define SBPSCSI_SBP2_MAX_CDB 12
if (xs->cmdlen > SBPSCSI_SBP2_MAX_CDB) {
SBP_DEBUG(0)
printf(
"sbp doesn't support cdb's larger than %d bytes\n",
SBPSCSI_SBP2_MAX_CDB);
END_DEBUG
xs->error = XS_DRIVER_STUFFUP;
scsipi_done(xs);
return;
}
sbp_action1(sc, xs);
break;
case ADAPTER_REQ_GROW_RESOURCES:
SBP_DEBUG(1)
printf("Got req_grow_resources\n");
END_DEBUG
break;
case ADAPTER_REQ_SET_XFER_MODE:
SBP_DEBUG(1)
printf("Got set xfer mode\n");
END_DEBUG
break;
default:
panic("Unknown request: %d\n", (int)req);
}
}
static void
sbp_minphys(struct buf *bp)
{
minphys(bp);
}
/*
* Display device characteristics on the console
*/
static void
sbp_show_sdev_info(struct sbp_dev *sdev)
{
struct fw_device *fwdev = sdev->target->fwdev;
struct sbp_softc *sc = sdev->target->sbp;
aprint_normal_dev(sc->sc_fd.dev,
"ordered:%d type:%d EUI:%08x%08x node:%d speed:%d maxrec:%d\n",
(sdev->type & 0x40) >> 6,
(sdev->type & 0x1f),
fwdev->eui.hi,
fwdev->eui.lo,
fwdev->dst,
fwdev->speed,
fwdev->maxrec);
aprint_normal_dev(sc->sc_fd.dev, "%s '%s' '%s' '%s'\n",
sdev->bustgtlun, sdev->vendor, sdev->product, sdev->revision);
}
static void
sbp_alloc_lun(struct sbp_target *target)
{
struct crom_context cc;
struct csrreg *reg;
struct sbp_dev *sdev, **newluns;
struct sbp_softc *sc;
int maxlun, lun, i;
sc = target->sbp;
crom_init_context(&cc, target->fwdev->csrrom);
/* XXX should parse appropriate unit directories only */
maxlun = -1;
while (cc.depth >= 0) {
reg = crom_search_key(&cc, CROM_LUN);
if (reg == NULL)
break;
lun = reg->val & 0xffff;
SBP_DEBUG(0)
printf("target %d lun %d found\n", target->target_id, lun);
END_DEBUG
if (maxlun < lun)
maxlun = lun;
crom_next(&cc);
}
if (maxlun < 0)
aprint_normal_dev(sc->sc_fd.dev, "%d: no LUN found\n",
target->target_id);
maxlun++;
if (maxlun >= SBP_NUM_LUNS)
maxlun = SBP_NUM_LUNS;
/* Invalidiate stale devices */
for (lun = 0; lun < target->num_lun; lun++) {
sdev = target->luns[lun];
if (sdev == NULL)
continue;
sdev->flags &= ~VALID_LUN;
if (lun >= maxlun) {
/* lost device */
sbp_scsipi_detach_sdev(sdev);
sbp_free_sdev(sdev);
target->luns[lun] = NULL;
}
}
/* Reallocate */
if (maxlun != target->num_lun) {
newluns = (struct sbp_dev **) realloc(target->luns,
sizeof(struct sbp_dev *) * maxlun,
M_SBP, M_WAITOK | M_ZERO);
/*
* We must zero the extended region for the case
* realloc() doesn't allocate new buffer.
*/
if (maxlun > target->num_lun) {
const int sbp_dev_p_sz = sizeof(struct sbp_dev *);
memset(&newluns[target->num_lun], 0,
sbp_dev_p_sz * (maxlun - target->num_lun));
}
target->luns = newluns;
target->num_lun = maxlun;
}
crom_init_context(&cc, target->fwdev->csrrom);
while (cc.depth >= 0) {
int new = 0;
reg = crom_search_key(&cc, CROM_LUN);
if (reg == NULL)
break;
lun = reg->val & 0xffff;
if (lun >= SBP_NUM_LUNS) {
aprint_error_dev(sc->sc_fd.dev, "too large lun %d\n",
lun);
goto next;
}
sdev = target->luns[lun];
if (sdev == NULL) {
sdev = malloc(sizeof(struct sbp_dev),
M_SBP, M_WAITOK | M_ZERO);
target->luns[lun] = sdev;
sdev->lun_id = lun;
sdev->target = target;
STAILQ_INIT(&sdev->ocbs);
callout_init(&sdev->login_callout, CALLOUT_MPSAFE);
callout_setfunc(&sdev->login_callout,
sbp_login_callout, sdev);
sdev->status = SBP_DEV_RESET;
new = 1;
snprintf(sdev->bustgtlun, 32, "%s:%d:%d",
device_xname(sc->sc_fd.dev),
sdev->target->target_id,
sdev->lun_id);
if (!sc->sc_lwp)
if (kthread_create(
PRI_NONE, KTHREAD_MPSAFE, NULL,
sbp_scsipi_scan_target, &sc->sc_target,
&sc->sc_lwp,
"sbp%d_attach", device_unit(sc->sc_fd.dev)))
aprint_error_dev(sc->sc_fd.dev,
"unable to create thread");
}
sdev->flags |= VALID_LUN;
sdev->type = (reg->val & 0xff0000) >> 16;
if (new == 0)
goto next;
fwdma_alloc_setup(sc->sc_fd.dev, sc->sc_dmat, SBP_DMA_SIZE,
&sdev->dma, sizeof(uint32_t), BUS_DMA_NOWAIT);
if (sdev->dma.v_addr == NULL) {
free(sdev, M_SBP);
target->luns[lun] = NULL;
goto next;
}
sdev->ocb = (struct sbp_ocb *)sdev->dma.v_addr;
sdev->login = (struct sbp_login_res *)&sdev->ocb[SBP_QUEUE_LEN];
memset((char *)sdev->ocb, 0,
sizeof(struct sbp_ocb) * SBP_QUEUE_LEN);
STAILQ_INIT(&sdev->free_ocbs);
for (i = 0; i < SBP_QUEUE_LEN; i++) {
struct sbp_ocb *ocb = &sdev->ocb[i];
ocb->index = i;
ocb->bus_addr =
sdev->dma.bus_addr + sizeof(struct sbp_ocb) * i;
if (bus_dmamap_create(sc->sc_dmat, 0x100000,
SBP_IND_MAX, SBP_SEG_MAX, 0, 0, &ocb->dmamap)) {
aprint_error_dev(sc->sc_fd.dev,
"cannot create dmamap %d\n", i);
/* XXX */
goto next;
}
sbp_free_ocb(sdev, ocb); /* into free queue */
}
next:
crom_next(&cc);
}
for (lun = 0; lun < target->num_lun; lun++) {
sdev = target->luns[lun];
if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
sbp_scsipi_detach_sdev(sdev);
sbp_free_sdev(sdev);
target->luns[lun] = NULL;
}
}
}
static struct sbp_target *
sbp_alloc_target(struct sbp_softc *sc, struct fw_device *fwdev)
{
struct sbp_target *target;
struct crom_context cc;
struct csrreg *reg;
SBP_DEBUG(1)
printf("sbp_alloc_target\n");
END_DEBUG
/* new target */
target = &sc->sc_target;
target->sbp = sc;
target->fwdev = fwdev;
target->target_id = 0;
target->mgm_ocb_cur = NULL;
SBP_DEBUG(1)
printf("target: mgm_port: %x\n", target->mgm_lo);
END_DEBUG
STAILQ_INIT(&target->xferlist);
target->n_xfer = 0;
STAILQ_INIT(&target->mgm_ocb_queue);
callout_init(&target->mgm_ocb_timeout, CALLOUT_MPSAFE);
target->luns = NULL;
target->num_lun = 0;
/* XXX we may want to reload mgm port after each bus reset */
/* XXX there might be multiple management agents */
crom_init_context(&cc, target->fwdev->csrrom);
reg = crom_search_key(&cc, CROM_MGM);
if (reg == NULL || reg->val == 0) {
aprint_error_dev(sc->sc_fd.dev, "NULL management address\n");
target->fwdev = NULL;
return NULL;
}
target->mgm_hi = 0xffff;
target->mgm_lo = 0xf0000000 | (reg->val << 2);
return target;
}
static void
sbp_probe_lun(struct sbp_dev *sdev)
{
struct fw_device *fwdev;
struct crom_context c, *cc = &c;
struct csrreg *reg;
memset(sdev->vendor, 0, sizeof(sdev->vendor));
memset(sdev->product, 0, sizeof(sdev->product));
fwdev = sdev->target->fwdev;
crom_init_context(cc, fwdev->csrrom);
/* get vendor string */
crom_search_key(cc, CSRKEY_VENDOR);
crom_next(cc);
crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
/* skip to the unit directory for SBP-2 */
while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
if (reg->val == CSRVAL_T10SBP2)
break;
crom_next(cc);
}
/* get firmware revision */
reg = crom_search_key(cc, CSRKEY_FIRM_VER);
if (reg != NULL)
snprintf(sdev->revision, sizeof(sdev->revision), "%06x",
reg->val);
/* get product string */
crom_search_key(cc, CSRKEY_MODEL);
crom_next(cc);
crom_parse_text(cc, sdev->product, sizeof(sdev->product));
}
static void
sbp_login_callout(void *arg)
{
struct sbp_dev *sdev = (struct sbp_dev *)arg;
sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
}
static void
sbp_login(struct sbp_dev *sdev)
{
struct sbp_softc *sc = sdev->target->sbp;
struct timeval delta;
struct timeval t;
int ticks = 0;
microtime(&delta);
timersub(&delta, &sc->sc_last_busreset, &delta);
t.tv_sec = login_delay / 1000;
t.tv_usec = (login_delay % 1000) * 1000;
timersub(&t, &delta, &t);
if (t.tv_sec >= 0 && t.tv_usec > 0)
ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
SBP_DEBUG(0)
printf("%s: sec = %lld usec = %ld ticks = %d\n", __func__,
(long long)t.tv_sec, (long)t.tv_usec, ticks);
END_DEBUG
callout_schedule(&sdev->login_callout, ticks);
}
static void
sbp_probe_target(void *arg)
{
struct sbp_target *target = (struct sbp_target *)arg;
struct sbp_dev *sdev;
int i;
SBP_DEBUG(1)
printf("%s %d\n", __func__, target->target_id);
END_DEBUG
sbp_alloc_lun(target);
/* XXX untimeout mgm_ocb and dequeue */
for (i = 0; i < target->num_lun; i++) {
sdev = target->luns[i];
if (sdev == NULL || sdev->status == SBP_DEV_DEAD)
continue;
if (sdev->periph != NULL) {
scsipi_periph_freeze(sdev->periph, 1);
sdev->freeze++;
}
sbp_probe_lun(sdev);
sbp_show_sdev_info(sdev);
sbp_abort_all_ocbs(sdev, XS_RESET);
switch (sdev->status) {
case SBP_DEV_RESET:
/* new or revived target */
if (auto_login)
sbp_login(sdev);
break;
case SBP_DEV_TOATTACH:
case SBP_DEV_PROBE:
case SBP_DEV_ATTACHED:
case SBP_DEV_RETRY:
default:
sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
break;
}
}
}
static void
sbp_post_busreset(void *arg)
{
struct sbp_softc *sc = (struct sbp_softc *)arg;
struct sbp_target *target = &sc->sc_target;
struct fw_device *fwdev = target->fwdev;
int alive;
alive = SBP_FWDEV_ALIVE(fwdev);
SBP_DEBUG(0)
printf("sbp_post_busreset\n");
if (!alive)
printf("not alive\n");
END_DEBUG
microtime(&sc->sc_last_busreset);
if (!alive)
return;
scsipi_channel_freeze(&sc->sc_channel, 1);
}
static void
sbp_post_explore(void *arg)
{
struct sbp_softc *sc = (struct sbp_softc *)arg;
struct sbp_target *target = &sc->sc_target;
struct fw_device *fwdev = target->fwdev;
int alive;
alive = SBP_FWDEV_ALIVE(fwdev);
SBP_DEBUG(0)
printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
if (!alive)
printf("not alive\n");
END_DEBUG
if (!alive)
return;
if (!firewire_phydma_enable)
return;
if (sbp_cold > 0)
sbp_cold--;
SBP_DEBUG(0)
printf("sbp_post_explore: EUI:%08x%08x ", fwdev->eui.hi, fwdev->eui.lo);
END_DEBUG
sbp_probe_target((void *)target);
if (target->num_lun == 0)
sbp_free_target(target);
scsipi_channel_thaw(&sc->sc_channel, 1);
}
#if NEED_RESPONSE
static void
sbp_loginres_callback(struct fw_xfer *xfer)
{
struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc;
struct sbp_softc *sc = sdev->target->sbp;
SBP_DEBUG(1)
printf("sbp_loginres_callback\n");
END_DEBUG
/* recycle */
mutex_enter(&sc->sc_fwb.fwb_mtx);
STAILQ_INSERT_TAIL(&sc->sc_fwb.xferlist, xfer, link);
mutex_exit(&sc->sc_fwb.fwb_mtx);
return;
}
#endif
static inline void
sbp_xfer_free(struct fw_xfer *xfer)
{
struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc;
struct sbp_softc *sc = sdev->target->sbp;
fw_xfer_unload(xfer);
mutex_enter(&sc->sc_mtx);
STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
mutex_exit(&sc->sc_mtx);
}
static void
sbp_reset_start_callback(struct fw_xfer *xfer)
{
struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
struct sbp_target *target = sdev->target;
int i;
if (xfer->resp != 0)
aprint_error("%s: sbp_reset_start failed: resp=%d\n",
sdev->bustgtlun, xfer->resp);
for (i = 0; i < target->num_lun; i++) {
tsdev = target->luns[i];
if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
sbp_login(tsdev);
}
}
static void
sbp_reset_start(struct sbp_dev *sdev)
{
struct fw_xfer *xfer;
struct fw_pkt *fp;
SBP_DEBUG(0)
printf("%s: sbp_reset_start: %s\n",
device_xname(sdev->target->sbp->sc_fd.dev), sdev->bustgtlun);
END_DEBUG
xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
if (xfer == NULL)
return;
xfer->hand = sbp_reset_start_callback;
fp = &xfer->send.hdr;
fp->mode.wreqq.dest_hi = 0xffff;
fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
fp->mode.wreqq.data = htonl(0xf);
if (fw_asyreq(xfer->fc, -1, xfer) != 0)
sbp_xfer_free(xfer);
}
static void
sbp_mgm_callback(struct fw_xfer *xfer)
{
struct sbp_dev *sdev;
sdev = (struct sbp_dev *)xfer->sc;
SBP_DEBUG(1)
printf("%s: sbp_mgm_callback: %s\n",
device_xname(sdev->target->sbp->sc_fd.dev), sdev->bustgtlun);
END_DEBUG
sbp_xfer_free(xfer);
return;
}
static void
sbp_scsipi_scan_target(void *arg)
{
struct sbp_target *target = (struct sbp_target *)arg;
struct sbp_softc *sc = target->sbp;
struct sbp_dev *sdev;
struct scsipi_channel *chan = &sc->sc_channel;
struct scsibus_softc *sc_bus = device_private(sc->sc_bus);
int lun, yet;
do {
mutex_enter(&sc->sc_mtx);
cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
mutex_exit(&sc->sc_mtx);
yet = 0;
for (lun = 0; lun < target->num_lun; lun++) {
sdev = target->luns[lun];
if (sdev == NULL)
continue;
if (sdev->status != SBP_DEV_PROBE) {
yet++;
continue;
}
if (sdev->periph == NULL) {
if (chan->chan_nluns < target->num_lun)
chan->chan_nluns = target->num_lun;
scsi_probe_bus(sc_bus, target->target_id,
sdev->lun_id);
sdev->periph = scsipi_lookup_periph(chan,
target->target_id, lun);
}
sdev->status = SBP_DEV_ATTACHED;
}
} while (yet > 0);
sc->sc_lwp = NULL;
kthread_exit(0);
/* NOTREACHED */
}
static inline void
sbp_scan_dev(struct sbp_dev *sdev)
{
struct sbp_softc *sc = sdev->target->sbp;
sdev->status = SBP_DEV_PROBE;
mutex_enter(&sc->sc_mtx);
cv_signal(&sdev->target->sbp->sc_cv);
mutex_exit(&sc->sc_mtx);
}
static void
sbp_do_attach(struct fw_xfer *xfer)
{
struct sbp_dev *sdev;
struct sbp_target *target;
struct sbp_softc *sc;
sdev = (struct sbp_dev *)xfer->sc;
target = sdev->target;
sc = target->sbp;
SBP_DEBUG(0)
printf("%s:%s:%s\n", device_xname(sc->sc_fd.dev), __func__,
sdev->bustgtlun);
END_DEBUG
sbp_xfer_free(xfer);
sbp_scan_dev(sdev);
return;
}
static void
sbp_agent_reset_callback(struct fw_xfer *xfer)
{
struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc;
struct sbp_softc *sc = sdev->target->sbp;
SBP_DEBUG(1)
printf("%s:%s:%s\n", device_xname(sc->sc_fd.dev), __func__,
sdev->bustgtlun);
END_DEBUG
if (xfer->resp != 0)
aprint_error_dev(sc->sc_fd.dev, "%s:%s: resp=%d\n", __func__,
sdev->bustgtlun, xfer->resp);
sbp_xfer_free(xfer);
if (sdev->periph != NULL) {
scsipi_periph_thaw(sdev->periph, sdev->freeze);
scsipi_channel_thaw(&sc->sc_channel, 0);
sdev->freeze = 0;
}
}
static void
sbp_agent_reset(struct sbp_dev *sdev)
{
struct fw_xfer *xfer;
struct fw_pkt *fp;
SBP_DEBUG(0)
printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev),
__func__, sdev->bustgtlun);
END_DEBUG
xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
if (xfer == NULL)
return;
if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
xfer->hand = sbp_agent_reset_callback;
else
xfer->hand = sbp_do_attach;
fp = &xfer->send.hdr;
fp->mode.wreqq.data = htonl(0xf);
if (fw_asyreq(xfer->fc, -1, xfer) != 0)
sbp_xfer_free(xfer);
sbp_abort_all_ocbs(sdev, XS_RESET);
}
static void
sbp_busy_timeout_callback(struct fw_xfer *xfer)
{
struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc;
SBP_DEBUG(1)
printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev),
__func__, sdev->bustgtlun);
END_DEBUG
sbp_xfer_free(xfer);
sbp_agent_reset(sdev);
}
static void
sbp_busy_timeout(struct sbp_dev *sdev)
{
struct fw_pkt *fp;
struct fw_xfer *xfer;
SBP_DEBUG(0)
printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev),
__func__, sdev->bustgtlun);
END_DEBUG
xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
if (xfer == NULL)
return;
xfer->hand = sbp_busy_timeout_callback;
fp = &xfer->send.hdr;
fp->mode.wreqq.dest_hi = 0xffff;
fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
if (fw_asyreq(xfer->fc, -1, xfer) != 0)
sbp_xfer_free(xfer);
}
static void
sbp_orb_pointer_callback(struct fw_xfer *xfer)
{
struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc;
struct sbp_softc *sc = sdev->target->sbp;
SBP_DEBUG(1)
printf("%s:%s:%s\n", device_xname(sc->sc_fd.dev), __func__,
sdev->bustgtlun);
END_DEBUG
if (xfer->resp != 0)
aprint_error_dev(sc->sc_fd.dev, "%s:%s: xfer->resp = %d\n",
__func__, sdev->bustgtlun, xfer->resp);
sbp_xfer_free(xfer);
sdev->flags &= ~ORB_POINTER_ACTIVE;
if ((sdev->flags & ORB_POINTER_NEED) != 0) {
struct sbp_ocb *ocb;
sdev->flags &= ~ORB_POINTER_NEED;
ocb = STAILQ_FIRST(&sdev->ocbs);
if (ocb != NULL)
sbp_orb_pointer(sdev, ocb);
}
return;
}
static void
sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
{
struct sbp_softc *sc = sdev->target->sbp;
struct fw_xfer *xfer;
struct fw_pkt *fp;
SBP_DEBUG(1)
printf("%s:%s:%s: 0x%08x\n", device_xname(sc->sc_fd.dev), __func__,
sdev->bustgtlun, (uint32_t)ocb->bus_addr);
END_DEBUG
if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
SBP_DEBUG(0)
printf("%s: orb pointer active\n", __func__);
END_DEBUG
sdev->flags |= ORB_POINTER_NEED;
return;
}
sdev->flags |= ORB_POINTER_ACTIVE;
xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
if (xfer == NULL)
return;
xfer->hand = sbp_orb_pointer_callback;
fp = &xfer->send.hdr;
fp->mode.wreqb.len = 8;
fp->mode.wreqb.extcode = 0;
xfer->send.payload[0] =
htonl(((sc->sc_fd.fc->nodeid | FWLOCALBUS) << 16));
xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr);
if (fw_asyreq(xfer->fc, -1, xfer) != 0) {
sbp_xfer_free(xfer);
ocb->xs->error = XS_DRIVER_STUFFUP;
scsipi_done(ocb->xs);
}
}
static void
sbp_doorbell_callback(struct fw_xfer *xfer)
{
struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc;
struct sbp_softc *sc = sdev->target->sbp;
SBP_DEBUG(1)
printf("%s:%s:%s\n", device_xname(sc->sc_fd.dev), __func__,
sdev->bustgtlun);
END_DEBUG
if (xfer->resp != 0) {
aprint_error_dev(sc->sc_fd.dev, "%s: xfer->resp = %d\n",
__func__, xfer->resp);
}
sbp_xfer_free(xfer);
sdev->flags &= ~ORB_DOORBELL_ACTIVE;
if ((sdev->flags & ORB_DOORBELL_NEED) != 0) {
sdev->flags &= ~ORB_DOORBELL_NEED;
sbp_doorbell(sdev);
}
return;
}
static void
sbp_doorbell(struct sbp_dev *sdev)
{
struct fw_xfer *xfer;
struct fw_pkt *fp;
SBP_DEBUG(1)
printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev),
__func__, sdev->bustgtlun);
END_DEBUG
if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) {
sdev->flags |= ORB_DOORBELL_NEED;
return;
}
sdev->flags |= ORB_DOORBELL_ACTIVE;
xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
if (xfer == NULL)
return;
xfer->hand = sbp_doorbell_callback;
fp = &xfer->send.hdr;
fp->mode.wreqq.data = htonl(0xf);
if (fw_asyreq(xfer->fc, -1, xfer) != 0)
sbp_xfer_free(xfer);
}
static struct fw_xfer *
sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
{
struct sbp_softc *sc;
struct fw_xfer *xfer;
struct fw_pkt *fp;
struct sbp_target *target;
int new = 0;
target = sdev->target;
sc = target->sbp;
mutex_enter(&sc->sc_mtx);
xfer = STAILQ_FIRST(&target->xferlist);
if (xfer == NULL) {
if (target->n_xfer > 5 /* XXX */) {
aprint_error_dev(sc->sc_fd.dev,
"no more xfer for this target\n");
mutex_exit(&sc->sc_mtx);
return NULL;
}
xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
if (xfer == NULL) {
aprint_error_dev(sc->sc_fd.dev,
"fw_xfer_alloc_buf failed\n");
mutex_exit(&sc->sc_mtx);
return NULL;
}
target->n_xfer++;
SBP_DEBUG(0)
printf("sbp: alloc %d xfer\n", target->n_xfer);
END_DEBUG
new = 1;
} else
STAILQ_REMOVE_HEAD(&target->xferlist, link);
mutex_exit(&sc->sc_mtx);
microtime(&xfer->tv);
if (new) {
xfer->recv.pay_len = 0;
xfer->send.spd = uimin(target->fwdev->speed, max_speed);
xfer->fc = target->sbp->sc_fd.fc;
}
if (tcode == FWTCODE_WREQB)
xfer->send.pay_len = 8;
else
xfer->send.pay_len = 0;
xfer->sc = (void *)sdev;
fp = &xfer->send.hdr;
fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
fp->mode.wreqq.tlrt = 0;
fp->mode.wreqq.tcode = tcode;
fp->mode.wreqq.pri = 0;
fp->mode.wreqq.dst = FWLOCALBUS | target->fwdev->dst;
return xfer;
}
static void
sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
{
struct fw_xfer *xfer;
struct fw_pkt *fp;
struct sbp_ocb *ocb;
struct sbp_target *target;
int nid, dv_unit;
target = sdev->target;
nid = target->sbp->sc_fd.fc->nodeid | FWLOCALBUS;
dv_unit = device_unit(target->sbp->sc_fd.dev);
mutex_enter(&target->sbp->sc_mtx);
if (func == ORB_FUN_RUNQUEUE) {
ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
if (target->mgm_ocb_cur != NULL || ocb == NULL) {
mutex_exit(&target->sbp->sc_mtx);
return;
}
STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
mutex_exit(&target->sbp->sc_mtx);
goto start;
}
if ((ocb = sbp_get_ocb(sdev)) == NULL) {
mutex_exit(&target->sbp->sc_mtx);
/* XXX */
return;
}
mutex_exit(&target->sbp->sc_mtx);
ocb->flags = OCB_ACT_MGM;
ocb->sdev = sdev;
memset(ocb->orb, 0, sizeof(ocb->orb));
ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
ocb->orb[7] = htonl(SBP_DEV2ADDR(dv_unit, sdev->lun_id));
SBP_DEBUG(0)
printf("%s:%s:%s: %s\n", device_xname(sdev->target->sbp->sc_fd.dev),
__func__, sdev->bustgtlun, orb_fun_name[(func>>16)&0xf]);
END_DEBUG
switch (func) {
case ORB_FUN_LGI:
{
const off_t sbp_login_off =
sizeof(struct sbp_ocb) * SBP_QUEUE_LEN;
ocb->orb[0] = ocb->orb[1] = 0; /* password */
ocb->orb[2] = htonl(nid << 16);
ocb->orb[3] = htonl(sdev->dma.bus_addr + sbp_login_off);
ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
if (ex_login)
ocb->orb[4] |= htonl(ORB_EXV);
ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
bus_dmamap_sync(sdev->dma.dma_tag, sdev->dma.dma_map,
sbp_login_off, SBP_LOGIN_SIZE, BUS_DMASYNC_PREREAD);
break;
}
case ORB_FUN_ATA:
ocb->orb[0] = htonl((0 << 16) | 0);
ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
/* fall through */
case ORB_FUN_RCN:
case ORB_FUN_LGO:
case ORB_FUN_LUR:
case ORB_FUN_RST:
case ORB_FUN_ATS:
ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
break;
}
if (target->mgm_ocb_cur != NULL) {
/* there is a standing ORB */
mutex_enter(&target->sbp->sc_mtx);
STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
mutex_exit(&target->sbp->sc_mtx);
return;
}
start:
target->mgm_ocb_cur = ocb;
callout_reset(&target->mgm_ocb_timeout, 5 * hz, sbp_mgm_timeout, ocb);
xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
if (xfer == NULL)
return;
xfer->hand = sbp_mgm_callback;
fp = &xfer->send.hdr;
fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
fp->mode.wreqb.len = 8;
fp->mode.wreqb.extcode = 0;
xfer->send.payload[0] = htonl(nid << 16);
xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
/* cache writeback & invalidate(required ORB_FUN_LGI func) */
/* when abort_ocb, should sync POST ope ? */
SBP_ORB_DMA_SYNC(sdev->dma, ocb->index, BUS_DMASYNC_PREWRITE);
if (fw_asyreq(xfer->fc, -1, xfer) != 0)
sbp_xfer_free(xfer);
}
static void
sbp_print_scsi_cmd(struct sbp_ocb *ocb)
{
struct scsipi_xfer *xs = ocb->xs;
printf("%s:%d:%d:"
" cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x,"
" flags: 0x%02x, %db cmd/%db data\n",
device_xname(ocb->sdev->target->sbp->sc_fd.dev),
xs->xs_periph->periph_target,
xs->xs_periph->periph_lun,
xs->cmd->opcode,
xs->cmd->bytes[0], xs->cmd->bytes[1],
xs->cmd->bytes[2], xs->cmd->bytes[3],
xs->cmd->bytes[4], xs->cmd->bytes[5],
xs->cmd->bytes[6], xs->cmd->bytes[7],
xs->cmd->bytes[8],
xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT),
xs->cmdlen, xs->datalen);
}
static void
sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
{
struct sbp_cmd_status *sbp_cmd_status;
struct scsi_sense_data *sense = &ocb->xs->sense.scsi_sense;
sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
SBP_DEBUG(0)
sbp_print_scsi_cmd(ocb);
/* XXX need decode status */
printf("%s:"
" SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
ocb->sdev->bustgtlun,
sbp_cmd_status->status,
sbp_cmd_status->sfmt,
sbp_cmd_status->valid,
sbp_cmd_status->s_key,
sbp_cmd_status->s_code,
sbp_cmd_status->s_qlfr,
sbp_status->len);
END_DEBUG
switch (sbp_cmd_status->status) {
case SCSI_CHECK:
case SCSI_BUSY:
case SCSI_TERMINATED:
if (sbp_cmd_status->sfmt == SBP_SFMT_CURR)
sense->response_code = SSD_RCODE_CURRENT;
else
sense->response_code = SSD_RCODE_DEFERRED;
if (sbp_cmd_status->valid)
sense->response_code |= SSD_RCODE_VALID;
sense->flags = sbp_cmd_status->s_key;
if (sbp_cmd_status->mark)
sense->flags |= SSD_FILEMARK;
if (sbp_cmd_status->eom)
sense->flags |= SSD_EOM;
if (sbp_cmd_status->ill_len)
sense->flags |= SSD_ILI;
memcpy(sense->info, &sbp_cmd_status->info, 4);
if (sbp_status->len <= 1)
/* XXX not scsi status. shouldn't be happened */
sense->extra_len = 0;
else if (sbp_status->len <= 4)
/* add_sense_code(_qual), info, cmd_spec_info */
sense->extra_len = 6;
else
/* fru, sense_key_spec */
sense->extra_len = 10;
memcpy(sense->csi, &sbp_cmd_status->cdb, 4);
sense->asc = sbp_cmd_status->s_code;
sense->ascq = sbp_cmd_status->s_qlfr;
sense->fru = sbp_cmd_status->fru;
memcpy(sense->sks.sks_bytes, sbp_cmd_status->s_keydep, 3);
ocb->xs->error = XS_SENSE;
ocb->xs->xs_status = sbp_cmd_status->status;
/*
{
uint8_t j, *tmp;
tmp = sense;
for (j = 0; j < 32; j += 8)
aprint_normal(
"sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
}
*/
break;
default:
aprint_error_dev(ocb->sdev->target->sbp->sc_fd.dev,
"%s:%s: unknown scsi status 0x%x\n",
__func__, ocb->sdev->bustgtlun, sbp_cmd_status->status);
}
}
static void
sbp_fix_inq_data(struct sbp_ocb *ocb)
{
struct scsipi_xfer *xs = ocb->xs;
struct sbp_dev *sdev;
struct scsipi_inquiry_data *inq =
(struct scsipi_inquiry_data *)xs->data;
sdev = ocb->sdev;
#if 0
/*
* NetBSD is assuming always 0 for EVPD-bit and 'Page Code'.
*/
#define SI_EVPD 0x01
if (xs->cmd->bytes[0] & SI_EVPD)
return;
#endif
SBP_DEBUG(1)
printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev),
__func__, sdev->bustgtlun);
END_DEBUG
switch (inq->device & SID_TYPE) {
case T_DIRECT:
#if 0
/*
* XXX Convert Direct Access device to RBC.
* I've never seen FireWire DA devices which support READ_6.
*/
if ((inq->device & SID_TYPE) == T_DIRECT)
inq->device |= T_SIMPLE_DIRECT; /* T_DIRECT == 0 */
#endif
/* FALLTHROUGH */
case T_SIMPLE_DIRECT:
/*
* Override vendor/product/revision information.
* Some devices sometimes return strange strings.
*/
#if 1
memcpy(inq->vendor, sdev->vendor, sizeof(inq->vendor));
memcpy(inq->product, sdev->product, sizeof(inq->product));
memcpy(inq->revision + 2, sdev->revision,
sizeof(inq->revision));
#endif
break;
}
/*
* Force to enable/disable tagged queuing.
* XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page.
*/
if (sbp_tags > 0)
inq->flags3 |= SID_CmdQue;
else if (sbp_tags < 0)
inq->flags3 &= ~SID_CmdQue;
}
static void
sbp_recv(struct fw_xfer *xfer)
{
struct fw_pkt *rfp;
#if NEED_RESPONSE
struct fw_pkt *sfp;
#endif
struct sbp_softc *sc;
struct sbp_dev *sdev;
struct sbp_ocb *ocb;
struct sbp_login_res *login_res = NULL;
struct sbp_status *sbp_status;
struct sbp_target *target;
int orb_fun, status_valid0, status_valid, l, reset_agent = 0;
uint32_t addr;
/*
uint32_t *ld;
ld = xfer->recv.buf;
printf("sbp %x %d %d %08x %08x %08x %08x\n",
xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
*/
sc = (struct sbp_softc *)xfer->sc;
if (xfer->resp != 0) {
aprint_error_dev(sc->sc_fd.dev,
"sbp_recv: xfer->resp = %d\n", xfer->resp);
goto done0;
}
if (xfer->recv.payload == NULL) {
aprint_error_dev(sc->sc_fd.dev,
"sbp_recv: xfer->recv.payload == NULL\n");
goto done0;
}
rfp = &xfer->recv.hdr;
if (rfp->mode.wreqb.tcode != FWTCODE_WREQB) {
aprint_error_dev(sc->sc_fd.dev,
"sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
goto done0;
}
sbp_status = (struct sbp_status *)xfer->recv.payload;
addr = rfp->mode.wreqb.dest_lo;
SBP_DEBUG(2)
printf("received address 0x%x\n", addr);
END_DEBUG
target = &sc->sc_target;
l = SBP_ADDR2LUN(addr);
if (l >= target->num_lun || target->luns[l] == NULL) {
aprint_error_dev(sc->sc_fd.dev,
"sbp_recv1: invalid lun %d (target=%d)\n",
l, target->target_id);
goto done0;
}
sdev = target->luns[l];
ocb = NULL;
switch (sbp_status->src) {
case SRC_NEXT_EXISTS:
case SRC_NO_NEXT:
/* check mgm_ocb_cur first */
ocb = target->mgm_ocb_cur;
if (ocb != NULL)
if (OCB_MATCH(ocb, sbp_status)) {
callout_stop(&target->mgm_ocb_timeout);
target->mgm_ocb_cur = NULL;
break;
}
ocb = sbp_dequeue_ocb(sdev, sbp_status);
if (ocb == NULL)
aprint_error_dev(sc->sc_fd.dev,
"%s:%s: No ocb(%x) on the queue\n", __func__,
sdev->bustgtlun, ntohl(sbp_status->orb_lo));
break;
case SRC_UNSOL:
/* unsolicit */
aprint_error_dev(sc->sc_fd.dev,
"%s:%s: unsolicit status received\n",
__func__, sdev->bustgtlun);
break;
default:
aprint_error_dev(sc->sc_fd.dev,
"%s:%s: unknown sbp_status->src\n",
__func__, sdev->bustgtlun);
}
status_valid0 = (sbp_status->src < 2
&& sbp_status->resp == SBP_REQ_CMP
&& sbp_status->dead == 0);
status_valid = (status_valid0 && sbp_status->status == 0);
if (!status_valid0 || debug > 2) {
int status;
SBP_DEBUG(0)
printf("%s:%s:%s: ORB status src:%x resp:%x dead:%x"
" len:%x stat:%x orb:%x%08x\n",
device_xname(sc->sc_fd.dev), __func__, sdev->bustgtlun,
sbp_status->src, sbp_status->resp, sbp_status->dead,
sbp_status->len, sbp_status->status,
ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
END_DEBUG
printf("%s:%s\n", device_xname(sc->sc_fd.dev), sdev->bustgtlun);
status = sbp_status->status;
switch (sbp_status->resp) {
case SBP_REQ_CMP:
if (status > MAX_ORB_STATUS0)
printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
else
printf("%s\n", orb_status0[status]);
break;
case SBP_TRANS_FAIL:
printf("Obj: %s, Error: %s\n",
orb_status1_object[(status>>6) & 3],
orb_status1_serial_bus_error[status & 0xf]);
break;
case SBP_ILLE_REQ:
printf("Illegal request\n");
break;
case SBP_VEND_DEP:
printf("Vendor dependent\n");
break;
default:
printf("unknown respose code %d\n", sbp_status->resp);
}
}
/* we have to reset the fetch agent if it's dead */
if (sbp_status->dead) {
if (sdev->periph != NULL) {
scsipi_periph_freeze(sdev->periph, 1);
sdev->freeze++;
}
reset_agent = 1;
}
if (ocb == NULL)
goto done;
switch (ntohl(ocb->orb[4]) & ORB_FMT_MSK) {
case ORB_FMT_NOP:
break;
case ORB_FMT_VED:
break;
case ORB_FMT_STD:
switch (ocb->flags) {
case OCB_ACT_MGM:
orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
reset_agent = 0;
switch (orb_fun) {
case ORB_FUN_LGI:
{
const struct fwdma_alloc *dma = &sdev->dma;
const off_t sbp_login_off =
sizeof(struct sbp_ocb) * SBP_QUEUE_LEN;
bus_dmamap_sync(dma->dma_tag, dma->dma_map,
sbp_login_off, SBP_LOGIN_SIZE,
BUS_DMASYNC_POSTREAD);
login_res = sdev->login;
login_res->len = ntohs(login_res->len);
login_res->id = ntohs(login_res->id);
login_res->cmd_hi = ntohs(login_res->cmd_hi);
login_res->cmd_lo = ntohl(login_res->cmd_lo);
if (status_valid) {
SBP_DEBUG(0)
printf("%s:%s:%s: login:"
" len %d, ID %d, cmd %08x%08x,"
" recon_hold %d\n",
device_xname(sc->sc_fd.dev),
__func__, sdev->bustgtlun,
login_res->len, login_res->id,
login_res->cmd_hi,
login_res->cmd_lo,
ntohs(login_res->recon_hold));
END_DEBUG
sbp_busy_timeout(sdev);
} else {
/* forgot logout? */
aprint_error_dev(sc->sc_fd.dev,
"%s:%s: login failed\n",
__func__, sdev->bustgtlun);
sdev->status = SBP_DEV_RESET;
}
break;
}
case ORB_FUN_RCN:
login_res = sdev->login;
if (status_valid) {
SBP_DEBUG(0)
printf("%s:%s:%s: reconnect:"
" len %d, ID %d, cmd %08x%08x\n",
device_xname(sc->sc_fd.dev),
__func__, sdev->bustgtlun,
login_res->len, login_res->id,
login_res->cmd_hi,
login_res->cmd_lo);
END_DEBUG
sbp_agent_reset(sdev);
} else {
/* reconnection hold time exceed? */
SBP_DEBUG(0)
aprint_error_dev(sc->sc_fd.dev,
"%s:%s: reconnect failed\n",
__func__, sdev->bustgtlun);
END_DEBUG
sbp_login(sdev);
}
break;
case ORB_FUN_LGO:
sdev->status = SBP_DEV_RESET;
break;
case ORB_FUN_RST:
sbp_busy_timeout(sdev);
break;
case ORB_FUN_LUR:
case ORB_FUN_ATA:
case ORB_FUN_ATS:
sbp_agent_reset(sdev);
break;
default:
aprint_error_dev(sc->sc_fd.dev,
"%s:%s: unknown function %d\n",
__func__, sdev->bustgtlun, orb_fun);
break;
}
sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
break;
case OCB_ACT_CMD:
sdev->timeout = 0;
if (ocb->xs != NULL) {
struct scsipi_xfer *xs = ocb->xs;
if (sbp_status->len > 1)
sbp_scsi_status(sbp_status, ocb);
else
if (sbp_status->resp != SBP_REQ_CMP)
xs->error = XS_DRIVER_STUFFUP;
else {
xs->error = XS_NOERROR;
xs->resid = 0;
}
/* fix up inq data */
if (xs->cmd->opcode == INQUIRY)
sbp_fix_inq_data(ocb);
scsipi_done(xs);
}
break;
default:
break;
}
}
if (!use_doorbell)
sbp_free_ocb(sdev, ocb);
done:
if (reset_agent)
sbp_agent_reset(sdev);
done0:
xfer->recv.pay_len = SBP_RECV_LEN;
/* The received packet is usually small enough to be stored within
* the buffer. In that case, the controller return ack_complete and
* no respose is necessary.
*
* XXX fwohci.c and firewire.c should inform event_code such as
* ack_complete or ack_pending to upper driver.
*/
#if NEED_RESPONSE
xfer->send.off = 0;
sfp = (struct fw_pkt *)xfer->send.buf;
sfp->mode.wres.dst = rfp->mode.wreqb.src;
xfer->dst = sfp->mode.wres.dst;
xfer->spd = uimin(sdev->target->fwdev->speed, max_speed);
xfer->hand = sbp_loginres_callback;
sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
sfp->mode.wres.tcode = FWTCODE_WRES;
sfp->mode.wres.rtcode = 0;
sfp->mode.wres.pri = 0;
if (fw_asyreq(xfer->fc, -1, xfer) != 0) {
aprint_error_dev(sc->sc_fd.dev, "mgm_orb failed\n");
mutex_enter(&sc->sc_fwb.fwb_mtx);
STAILQ_INSERT_TAIL(&sc->sc_fwb.xferlist, xfer, link);
mutex_exit(&sc->sc_fwb.fwb_mtx);
}
#else
/* recycle */
mutex_enter(&sc->sc_fwb.fwb_mtx);
STAILQ_INSERT_TAIL(&sc->sc_fwb.xferlist, xfer, link);
mutex_exit(&sc->sc_fwb.fwb_mtx);
#endif
return;
}
static int
sbp_logout_all(struct sbp_softc *sbp)
{
struct sbp_target *target;
struct sbp_dev *sdev;
int i;
SBP_DEBUG(0)
printf("sbp_logout_all\n");
END_DEBUG
target = &sbp->sc_target;
if (target->luns != NULL) {
for (i = 0; i < target->num_lun; i++) {
sdev = target->luns[i];
if (sdev == NULL)
continue;
callout_stop(&sdev->login_callout);
if (sdev->status >= SBP_DEV_TOATTACH &&
sdev->status <= SBP_DEV_ATTACHED)
sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
}
}
return 0;
}
static void
sbp_free_sdev(struct sbp_dev *sdev)
{
struct sbp_softc *sc = sdev->target->sbp;
int i;
if (sdev == NULL)
return;
for (i = 0; i < SBP_QUEUE_LEN; i++)
bus_dmamap_destroy(sc->sc_dmat, sdev->ocb[i].dmamap);
fwdma_free(sdev->dma.dma_tag, sdev->dma.dma_map, sdev->dma.v_addr);
free(sdev, M_SBP);
}
static void
sbp_free_target(struct sbp_target *target)
{
struct fw_xfer *xfer, *next;
int i;
if (target->luns == NULL)
return;
callout_stop(&target->mgm_ocb_timeout);
for (i = 0; i < target->num_lun; i++)
sbp_free_sdev(target->luns[i]);
for (xfer = STAILQ_FIRST(&target->xferlist);
xfer != NULL; xfer = next) {
next = STAILQ_NEXT(xfer, link);
fw_xfer_free_buf(xfer);
}
STAILQ_INIT(&target->xferlist);
free(target->luns, M_SBP);
target->num_lun = 0;
target->luns = NULL;
target->fwdev = NULL;
}
static void
sbp_scsipi_detach_sdev(struct sbp_dev *sdev)
{
struct sbp_target *target;
struct sbp_softc *sbp;
if (sdev == NULL)
return;
target = sdev->target;
if (target == NULL)
return;
sbp = target->sbp;
if (sdev->status == SBP_DEV_DEAD)
return;
if (sdev->status == SBP_DEV_RESET)
return;
if (sdev->periph != NULL) {
scsipi_periph_thaw(sdev->periph, sdev->freeze);
scsipi_channel_thaw(&sbp->sc_channel, 0); /* XXXX */
sdev->freeze = 0;
if (scsipi_target_detach(&sbp->sc_channel,
target->target_id, sdev->lun_id, DETACH_FORCE) != 0) {
aprint_error_dev(sbp->sc_fd.dev, "detach failed\n");
}
sdev->periph = NULL;
}
sbp_abort_all_ocbs(sdev, XS_DRIVER_STUFFUP);
}
static void
sbp_scsipi_detach_target(struct sbp_target *target)
{
struct sbp_softc *sbp = target->sbp;
int i;
if (target->luns != NULL) {
SBP_DEBUG(0)
printf("sbp_detach_target %d\n", target->target_id);
END_DEBUG
for (i = 0; i < target->num_lun; i++)
sbp_scsipi_detach_sdev(target->luns[i]);
if (config_detach(sbp->sc_bus, DETACH_FORCE) != 0)
aprint_error_dev(sbp->sc_fd.dev, "%d detach failed\n",
target->target_id);
sbp->sc_bus = NULL;
}
}
static void
sbp_target_reset(struct sbp_dev *sdev, int method)
{
struct sbp_target *target = sdev->target;
struct sbp_dev *tsdev;
int i;
for (i = 0; i < target->num_lun; i++) {
tsdev = target->luns[i];
if (tsdev == NULL)
continue;
if (tsdev->status == SBP_DEV_DEAD)
continue;
if (tsdev->status == SBP_DEV_RESET)
continue;
if (sdev->periph != NULL) {
scsipi_periph_freeze(tsdev->periph, 1);
tsdev->freeze++;
}
sbp_abort_all_ocbs(tsdev, XS_TIMEOUT);
if (method == 2)
tsdev->status = SBP_DEV_LOGIN;
}
switch (method) {
case 1:
aprint_error("target reset\n");
sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
break;
case 2:
aprint_error("reset start\n");
sbp_reset_start(sdev);
break;
}
}
static void
sbp_mgm_timeout(void *arg)
{
struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
struct sbp_dev *sdev = ocb->sdev;
struct sbp_target *target = sdev->target;
aprint_error_dev(sdev->target->sbp->sc_fd.dev,
"%s:%s: request timeout(mgm orb:0x%08x) ... ",
__func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr);
target->mgm_ocb_cur = NULL;
sbp_free_ocb(sdev, ocb);
#if 0
/* XXX */
aprint_error("run next request\n");
sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
#endif
aprint_error_dev(sdev->target->sbp->sc_fd.dev,
"%s:%s: reset start\n", __func__, sdev->bustgtlun);
sbp_reset_start(sdev);
}
static void
sbp_timeout(void *arg)
{
struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
struct sbp_dev *sdev = ocb->sdev;
aprint_error_dev(sdev->target->sbp->sc_fd.dev,
"%s:%s: request timeout(cmd orb:0x%08x) ... ",
__func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr);
sdev->timeout++;
switch (sdev->timeout) {
case 1:
aprint_error("agent reset\n");
if (sdev->periph != NULL) {
scsipi_periph_freeze(sdev->periph, 1);
sdev->freeze++;
}
sbp_abort_all_ocbs(sdev, XS_TIMEOUT);
sbp_agent_reset(sdev);
break;
case 2:
case 3:
sbp_target_reset(sdev, sdev->timeout - 1);
break;
default:
aprint_error("\n");
#if 0
/* XXX give up */
sbp_scsipi_detach_target(target);
if (target->luns != NULL)
free(target->luns, M_SBP);
target->num_lun = 0;
target->luns = NULL;
target->fwdev = NULL;
#endif
}
}
static void
sbp_action1(struct sbp_softc *sc, struct scsipi_xfer *xs)
{
struct sbp_target *target = &sc->sc_target;
struct sbp_dev *sdev = NULL;
struct sbp_ocb *ocb;
int speed, flag, error;
void *cdb;
/* target:lun -> sdev mapping */
if (target->fwdev != NULL &&
xs->xs_periph->periph_lun < target->num_lun) {
sdev = target->luns[xs->xs_periph->periph_lun];
if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
sdev->status != SBP_DEV_PROBE)
sdev = NULL;
}
if (sdev == NULL) {
SBP_DEBUG(1)
printf("%s:%d:%d: Invalid target (target needed)\n",
sc ? device_xname(sc->sc_fd.dev) : "???",
xs->xs_periph->periph_target,
xs->xs_periph->periph_lun);
END_DEBUG
xs->error = XS_DRIVER_STUFFUP;
scsipi_done(xs);
return;
}
SBP_DEBUG(2)
printf("%s:%d:%d:"
" cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x,"
" flags: 0x%02x, %db cmd/%db data\n",
device_xname(sc->sc_fd.dev),
xs->xs_periph->periph_target,
xs->xs_periph->periph_lun,
xs->cmd->opcode,
xs->cmd->bytes[0], xs->cmd->bytes[1],
xs->cmd->bytes[2], xs->cmd->bytes[3],
xs->cmd->bytes[4], xs->cmd->bytes[5],
xs->cmd->bytes[6], xs->cmd->bytes[7],
xs->cmd->bytes[8],
xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT),
xs->cmdlen, xs->datalen);
END_DEBUG
mutex_enter(&sc->sc_mtx);
ocb = sbp_get_ocb(sdev);
mutex_exit(&sc->sc_mtx);
if (ocb == NULL) {
xs->error = XS_REQUEUE;
if (sdev->freeze == 0) {
scsipi_periph_freeze(sdev->periph, 1);
sdev->freeze++;
}
scsipi_done(xs);
return;
}
ocb->flags = OCB_ACT_CMD;
ocb->sdev = sdev;
ocb->xs = xs;
ocb->orb[0] = htonl(1 << 31);
ocb->orb[1] = 0;
ocb->orb[2] = htonl(((sc->sc_fd.fc->nodeid | FWLOCALBUS) << 16));
ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
speed = uimin(target->fwdev->speed, max_speed);
ocb->orb[4] =
htonl(ORB_NOTIFY | ORB_CMD_SPD(speed) | ORB_CMD_MAXP(speed + 7));
if ((xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) ==
XS_CTL_DATA_IN) {
ocb->orb[4] |= htonl(ORB_CMD_IN);
flag = BUS_DMA_READ;
} else
flag = BUS_DMA_WRITE;
cdb = xs->cmd;
memcpy((void *)&ocb->orb[5], cdb, xs->cmdlen);
/*
printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
*/
if (xs->datalen > 0) {
error = bus_dmamap_load(sc->sc_dmat, ocb->dmamap,
xs->data, xs->datalen, NULL, BUS_DMA_NOWAIT | flag);
if (error) {
aprint_error_dev(sc->sc_fd.dev,
"DMA map load error %d\n", error);
xs->error = XS_DRIVER_STUFFUP;
scsipi_done(xs);
} else
sbp_execute_ocb(ocb, ocb->dmamap->dm_segs,
ocb->dmamap->dm_nsegs);
} else
sbp_execute_ocb(ocb, NULL, 0);
return;
}
static void
sbp_execute_ocb(struct sbp_ocb *ocb, bus_dma_segment_t *segments, int seg)
{
struct sbp_ocb *prev;
bus_dma_segment_t *s;
int i;
SBP_DEBUG(2)
printf("sbp_execute_ocb: seg %d", seg);
for (i = 0; i < seg; i++)
printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
(uintmax_t)segments[i].ds_len);
printf("\n");
END_DEBUG
if (seg == 1) {
/* direct pointer */
s = segments;
if (s->ds_len > SBP_SEG_MAX)
panic("ds_len > SBP_SEG_MAX, fix busdma code");
ocb->orb[3] = htonl(s->ds_addr);
ocb->orb[4] |= htonl(s->ds_len);
} else if (seg > 1) {
/* page table */
for (i = 0; i < seg; i++) {
s = &segments[i];
SBP_DEBUG(0)
/* XXX LSI Logic "< 16 byte" bug might be hit */
if (s->ds_len < 16)
printf("sbp_execute_ocb: warning, "
"segment length(%jd) is less than 16."
"(seg=%d/%d)\n",
(uintmax_t)s->ds_len, i + 1, seg);
END_DEBUG
if (s->ds_len > SBP_SEG_MAX)
panic("ds_len > SBP_SEG_MAX, fix busdma code");
ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
ocb->ind_ptr[i].lo = htonl(s->ds_addr);
}
ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
}
if (seg > 0) {
struct sbp_softc *sc = ocb->sdev->target->sbp;
const int flag = (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE;
bus_dmamap_sync(sc->sc_dmat, ocb->dmamap,
0, ocb->dmamap->dm_mapsize, flag);
}
prev = sbp_enqueue_ocb(ocb->sdev, ocb);
SBP_ORB_DMA_SYNC(ocb->sdev->dma, ocb->index, BUS_DMASYNC_PREWRITE);
if (use_doorbell) {
if (prev == NULL) {
if (ocb->sdev->last_ocb != NULL)
sbp_doorbell(ocb->sdev);
else
sbp_orb_pointer(ocb->sdev, ocb);
}
} else
if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
ocb->sdev->flags &= ~ORB_LINK_DEAD;
sbp_orb_pointer(ocb->sdev, ocb);
}
}
static struct sbp_ocb *
sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
{
struct sbp_softc *sc = sdev->target->sbp;
struct sbp_ocb *ocb;
struct sbp_ocb *next;
int order = 0;
SBP_DEBUG(1)
printf("%s:%s:%s: 0x%08x src %d\n", device_xname(sc->sc_fd.dev),
__func__, sdev->bustgtlun, ntohl(sbp_status->orb_lo),
sbp_status->src);
END_DEBUG
mutex_enter(&sc->sc_mtx);
for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
next = STAILQ_NEXT(ocb, ocb);
if (OCB_MATCH(ocb, sbp_status)) {
/* found */
SBP_ORB_DMA_SYNC(sdev->dma, ocb->index,
BUS_DMASYNC_POSTWRITE);
STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
if (ocb->xs != NULL)
callout_stop(&ocb->xs->xs_callout);
if (ntohl(ocb->orb[4]) & 0xffff) {
const int flag =
(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
BUS_DMASYNC_POSTREAD :
BUS_DMASYNC_POSTWRITE;
bus_dmamap_sync(sc->sc_dmat, ocb->dmamap,
0, ocb->dmamap->dm_mapsize, flag);
bus_dmamap_unload(sc->sc_dmat, ocb->dmamap);
}
if (!use_doorbell) {
if (sbp_status->src == SRC_NO_NEXT) {
if (next != NULL)
sbp_orb_pointer(sdev, next);
else if (order > 0)
/*
* Unordered execution
* We need to send pointer for
* next ORB
*/
sdev->flags |= ORB_LINK_DEAD;
}
}
break;
} else
order++;
}
mutex_exit(&sc->sc_mtx);
if (ocb && use_doorbell) {
/*
* XXX this is not correct for unordered
* execution.
*/
if (sdev->last_ocb != NULL)
sbp_free_ocb(sdev, sdev->last_ocb);
sdev->last_ocb = ocb;
if (next != NULL &&
sbp_status->src == SRC_NO_NEXT)
sbp_doorbell(sdev);
}
SBP_DEBUG(0)
if (ocb && order > 0)
printf("%s:%s:%s: unordered execution order:%d\n",
device_xname(sc->sc_fd.dev), __func__, sdev->bustgtlun,
order);
END_DEBUG
return ocb;
}
static struct sbp_ocb *
sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
{
struct sbp_softc *sc = sdev->target->sbp;
struct sbp_ocb *tocb, *prev, *prev2;
SBP_DEBUG(1)
printf("%s:%s:%s: 0x%08jx\n", device_xname(sc->sc_fd.dev),
__func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr);
END_DEBUG
mutex_enter(&sc->sc_mtx);
prev = NULL;
STAILQ_FOREACH(tocb, &sdev->ocbs, ocb)
prev = tocb;
prev2 = prev;
STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
mutex_exit(&sc->sc_mtx);
callout_reset(&ocb->xs->xs_callout, mstohz(ocb->xs->timeout),
sbp_timeout, ocb);
if (use_doorbell && prev == NULL)
prev2 = sdev->last_ocb;
if (prev2 != NULL) {
SBP_DEBUG(2)
printf("linking chain 0x%jx -> 0x%jx\n",
(uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr);
END_DEBUG
/*
* Suppress compiler optimization so that orb[1] must be
* written first.
* XXX We may need an explicit memory barrier for other
* architectures other than i386/amd64.
*/
*(volatile uint32_t *)&prev2->orb[1] = htonl(ocb->bus_addr);
*(volatile uint32_t *)&prev2->orb[0] = 0;
}
return prev;
}
static struct sbp_ocb *
sbp_get_ocb(struct sbp_dev *sdev)
{
struct sbp_softc *sc = sdev->target->sbp;
struct sbp_ocb *ocb;
KASSERT(mutex_owned(&sc->sc_mtx));
ocb = STAILQ_FIRST(&sdev->free_ocbs);
if (ocb == NULL) {
sdev->flags |= ORB_SHORTAGE;
aprint_error_dev(sc->sc_fd.dev,
"ocb shortage!!!\n");
return NULL;
}
STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
ocb->xs = NULL;
return ocb;
}
static void
sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
{
struct sbp_softc *sc = sdev->target->sbp;
int count;
ocb->flags = 0;
ocb->xs = NULL;
mutex_enter(&sc->sc_mtx);
STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
mutex_exit(&sc->sc_mtx);
if (sdev->flags & ORB_SHORTAGE) {
sdev->flags &= ~ORB_SHORTAGE;
count = sdev->freeze;
sdev->freeze = 0;
if (sdev->periph)
scsipi_periph_thaw(sdev->periph, count);
scsipi_channel_thaw(&sc->sc_channel, 0);
}
}
static void
sbp_abort_ocb(struct sbp_ocb *ocb, int status)
{
struct sbp_softc *sc;
struct sbp_dev *sdev;
sdev = ocb->sdev;
sc = sdev->target->sbp;
SBP_DEBUG(0)
printf("%s:%s:%s: sbp_abort_ocb 0x%jx\n", device_xname(sc->sc_fd.dev),
__func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr);
END_DEBUG
SBP_DEBUG(1)
if (ocb->xs != NULL)
sbp_print_scsi_cmd(ocb);
END_DEBUG
if (ntohl(ocb->orb[4]) & 0xffff) {
const int flag = (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE;
bus_dmamap_sync(sc->sc_dmat, ocb->dmamap,
0, ocb->dmamap->dm_mapsize, flag);
bus_dmamap_unload(sc->sc_dmat, ocb->dmamap);
}
if (ocb->xs != NULL) {
callout_stop(&ocb->xs->xs_callout);
ocb->xs->error = status;
scsipi_done(ocb->xs);
}
sbp_free_ocb(sdev, ocb);
}
static void
sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
{
struct sbp_softc *sc = sdev->target->sbp;
struct sbp_ocb *ocb, *next;
STAILQ_HEAD(, sbp_ocb) temp;
mutex_enter(&sc->sc_mtx);
STAILQ_INIT(&temp);
STAILQ_CONCAT(&temp, &sdev->ocbs);
STAILQ_INIT(&sdev->ocbs);
mutex_exit(&sc->sc_mtx);
for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
next = STAILQ_NEXT(ocb, ocb);
sbp_abort_ocb(ocb, status);
}
if (sdev->last_ocb != NULL) {
sbp_free_ocb(sdev, sdev->last_ocb);
sdev->last_ocb = NULL;
}
}
|