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
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
|
/* $OpenBSD: xhci.c,v 1.113 2020/03/02 16:30:39 visa Exp $ */
/*
* Copyright (c) 2014-2015 Martin Pieuchot
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <sys/queue.h>
#include <sys/timeout.h>
#include <sys/pool.h>
#include <sys/endian.h>
#include <sys/rwlock.h>
#include <machine/bus.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usb_mem.h>
#include <dev/usb/xhcireg.h>
#include <dev/usb/xhcivar.h>
struct cfdriver xhci_cd = {
NULL, "xhci", DV_DULL
};
#ifdef XHCI_DEBUG
#define DPRINTF(x) do { if (xhcidebug) printf x; } while(0)
#define DPRINTFN(n,x) do { if (xhcidebug>(n)) printf x; } while (0)
int xhcidebug = 3;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif
#define DEVNAME(sc) ((sc)->sc_bus.bdev.dv_xname)
#define TRBOFF(r, trb) ((char *)(trb) - (char *)((r)->trbs))
#define DEQPTR(r) ((r).dma.paddr + (sizeof(struct xhci_trb) * (r).index))
struct pool *xhcixfer;
struct xhci_pipe {
struct usbd_pipe pipe;
uint8_t dci;
uint8_t slot; /* Device slot ID */
struct xhci_ring ring;
/*
* XXX used to pass the xfer pointer back to the
* interrupt routine, better way?
*/
struct usbd_xfer *pending_xfers[XHCI_MAX_XFER];
struct usbd_xfer *aborted_xfer;
int halted;
size_t free_trbs;
int skip;
};
int xhci_reset(struct xhci_softc *);
int xhci_intr1(struct xhci_softc *);
void xhci_event_dequeue(struct xhci_softc *);
void xhci_event_xfer(struct xhci_softc *, uint64_t, uint32_t, uint32_t);
int xhci_event_xfer_generic(struct xhci_softc *, struct usbd_xfer *,
struct xhci_pipe *, uint32_t, int, uint8_t, uint8_t, uint8_t);
int xhci_event_xfer_isoc(struct usbd_xfer *, struct xhci_pipe *,
uint32_t, int);
void xhci_event_command(struct xhci_softc *, uint64_t);
void xhci_event_port_change(struct xhci_softc *, uint64_t, uint32_t);
int xhci_pipe_init(struct xhci_softc *, struct usbd_pipe *);
int xhci_context_setup(struct xhci_softc *, struct usbd_pipe *);
int xhci_scratchpad_alloc(struct xhci_softc *, int);
void xhci_scratchpad_free(struct xhci_softc *);
int xhci_softdev_alloc(struct xhci_softc *, uint8_t);
void xhci_softdev_free(struct xhci_softc *, uint8_t);
int xhci_ring_alloc(struct xhci_softc *, struct xhci_ring *, size_t,
size_t);
void xhci_ring_free(struct xhci_softc *, struct xhci_ring *);
void xhci_ring_reset(struct xhci_softc *, struct xhci_ring *);
struct xhci_trb *xhci_ring_consume(struct xhci_softc *, struct xhci_ring *);
struct xhci_trb *xhci_ring_produce(struct xhci_softc *, struct xhci_ring *);
struct xhci_trb *xhci_xfer_get_trb(struct xhci_softc *, struct usbd_xfer*,
uint8_t *, int);
void xhci_xfer_done(struct usbd_xfer *xfer);
/* xHCI command helpers. */
int xhci_command_submit(struct xhci_softc *, struct xhci_trb *, int);
int xhci_command_abort(struct xhci_softc *);
void xhci_cmd_reset_ep_async(struct xhci_softc *, uint8_t, uint8_t);
void xhci_cmd_set_tr_deq_async(struct xhci_softc *, uint8_t, uint8_t, uint64_t);
int xhci_cmd_configure_ep(struct xhci_softc *, uint8_t, uint64_t);
int xhci_cmd_stop_ep(struct xhci_softc *, uint8_t, uint8_t);
int xhci_cmd_slot_control(struct xhci_softc *, uint8_t *, int);
int xhci_cmd_set_address(struct xhci_softc *, uint8_t, uint64_t, uint32_t);
int xhci_cmd_evaluate_ctx(struct xhci_softc *, uint8_t, uint64_t);
#ifdef XHCI_DEBUG
int xhci_cmd_noop(struct xhci_softc *);
#endif
/* XXX should be part of the Bus interface. */
void xhci_abort_xfer(struct usbd_xfer *, usbd_status);
void xhci_pipe_close(struct usbd_pipe *);
void xhci_noop(struct usbd_xfer *);
void xhci_timeout(void *);
void xhci_timeout_task(void *);
/* USBD Bus Interface. */
usbd_status xhci_pipe_open(struct usbd_pipe *);
int xhci_setaddr(struct usbd_device *, int);
void xhci_softintr(void *);
void xhci_poll(struct usbd_bus *);
struct usbd_xfer *xhci_allocx(struct usbd_bus *);
void xhci_freex(struct usbd_bus *, struct usbd_xfer *);
usbd_status xhci_root_ctrl_transfer(struct usbd_xfer *);
usbd_status xhci_root_ctrl_start(struct usbd_xfer *);
usbd_status xhci_root_intr_transfer(struct usbd_xfer *);
usbd_status xhci_root_intr_start(struct usbd_xfer *);
void xhci_root_intr_abort(struct usbd_xfer *);
void xhci_root_intr_done(struct usbd_xfer *);
usbd_status xhci_device_ctrl_transfer(struct usbd_xfer *);
usbd_status xhci_device_ctrl_start(struct usbd_xfer *);
void xhci_device_ctrl_abort(struct usbd_xfer *);
usbd_status xhci_device_generic_transfer(struct usbd_xfer *);
usbd_status xhci_device_generic_start(struct usbd_xfer *);
void xhci_device_generic_abort(struct usbd_xfer *);
void xhci_device_generic_done(struct usbd_xfer *);
usbd_status xhci_device_isoc_transfer(struct usbd_xfer *);
usbd_status xhci_device_isoc_start(struct usbd_xfer *);
#define XHCI_INTR_ENDPT 1
struct usbd_bus_methods xhci_bus_methods = {
.open_pipe = xhci_pipe_open,
.dev_setaddr = xhci_setaddr,
.soft_intr = xhci_softintr,
.do_poll = xhci_poll,
.allocx = xhci_allocx,
.freex = xhci_freex,
};
struct usbd_pipe_methods xhci_root_ctrl_methods = {
.transfer = xhci_root_ctrl_transfer,
.start = xhci_root_ctrl_start,
.abort = xhci_noop,
.close = xhci_pipe_close,
.done = xhci_noop,
};
struct usbd_pipe_methods xhci_root_intr_methods = {
.transfer = xhci_root_intr_transfer,
.start = xhci_root_intr_start,
.abort = xhci_root_intr_abort,
.close = xhci_pipe_close,
.done = xhci_root_intr_done,
};
struct usbd_pipe_methods xhci_device_ctrl_methods = {
.transfer = xhci_device_ctrl_transfer,
.start = xhci_device_ctrl_start,
.abort = xhci_device_ctrl_abort,
.close = xhci_pipe_close,
.done = xhci_noop,
};
struct usbd_pipe_methods xhci_device_intr_methods = {
.transfer = xhci_device_generic_transfer,
.start = xhci_device_generic_start,
.abort = xhci_device_generic_abort,
.close = xhci_pipe_close,
.done = xhci_device_generic_done,
};
struct usbd_pipe_methods xhci_device_bulk_methods = {
.transfer = xhci_device_generic_transfer,
.start = xhci_device_generic_start,
.abort = xhci_device_generic_abort,
.close = xhci_pipe_close,
.done = xhci_device_generic_done,
};
struct usbd_pipe_methods xhci_device_isoc_methods = {
.transfer = xhci_device_isoc_transfer,
.start = xhci_device_isoc_start,
.abort = xhci_device_generic_abort,
.close = xhci_pipe_close,
.done = xhci_noop,
};
#ifdef XHCI_DEBUG
static void
xhci_dump_trb(struct xhci_trb *trb)
{
printf("trb=%p (0x%016llx 0x%08x 0x%b)\n", trb,
(long long)letoh64(trb->trb_paddr), letoh32(trb->trb_status),
(int)letoh32(trb->trb_flags), XHCI_TRB_FLAGS_BITMASK);
}
#endif
int usbd_dma_contig_alloc(struct usbd_bus *, struct usbd_dma_info *,
void **, bus_size_t, bus_size_t, bus_size_t);
void usbd_dma_contig_free(struct usbd_bus *, struct usbd_dma_info *);
int
usbd_dma_contig_alloc(struct usbd_bus *bus, struct usbd_dma_info *dma,
void **kvap, bus_size_t size, bus_size_t alignment, bus_size_t boundary)
{
int error;
dma->tag = bus->dmatag;
dma->size = size;
error = bus_dmamap_create(dma->tag, size, 1, size, boundary,
BUS_DMA_NOWAIT, &dma->map);
if (error != 0)
return (error);
error = bus_dmamem_alloc(dma->tag, size, alignment, boundary, &dma->seg,
1, &dma->nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
if (error != 0)
goto destroy;
error = bus_dmamem_map(dma->tag, &dma->seg, 1, size, &dma->vaddr,
BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
if (error != 0)
goto free;
error = bus_dmamap_load_raw(dma->tag, dma->map, &dma->seg, 1, size,
BUS_DMA_NOWAIT);
if (error != 0)
goto unmap;
bus_dmamap_sync(dma->tag, dma->map, 0, size, BUS_DMASYNC_PREREAD |
BUS_DMASYNC_PREWRITE);
dma->paddr = dma->map->dm_segs[0].ds_addr;
if (kvap != NULL)
*kvap = dma->vaddr;
return (0);
unmap:
bus_dmamem_unmap(dma->tag, dma->vaddr, size);
free:
bus_dmamem_free(dma->tag, &dma->seg, 1);
destroy:
bus_dmamap_destroy(dma->tag, dma->map);
return (error);
}
void
usbd_dma_contig_free(struct usbd_bus *bus, struct usbd_dma_info *dma)
{
if (dma->map != NULL) {
bus_dmamap_sync(bus->dmatag, dma->map, 0, dma->size,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(bus->dmatag, dma->map);
bus_dmamem_unmap(bus->dmatag, dma->vaddr, dma->size);
bus_dmamem_free(bus->dmatag, &dma->seg, 1);
bus_dmamap_destroy(bus->dmatag, dma->map);
dma->map = NULL;
}
}
int
xhci_init(struct xhci_softc *sc)
{
uint32_t hcr;
int npage, error;
sc->sc_bus.usbrev = USBREV_3_0;
sc->sc_bus.methods = &xhci_bus_methods;
sc->sc_bus.pipe_size = sizeof(struct xhci_pipe);
sc->sc_oper_off = XREAD1(sc, XHCI_CAPLENGTH);
sc->sc_door_off = XREAD4(sc, XHCI_DBOFF);
sc->sc_runt_off = XREAD4(sc, XHCI_RTSOFF);
sc->sc_version = XREAD2(sc, XHCI_HCIVERSION);
printf(", xHCI %x.%x\n", sc->sc_version >> 8, sc->sc_version & 0xff);
#ifdef XHCI_DEBUG
printf("%s: CAPLENGTH=%#lx\n", DEVNAME(sc), sc->sc_oper_off);
printf("%s: DOORBELL=%#lx\n", DEVNAME(sc), sc->sc_door_off);
printf("%s: RUNTIME=%#lx\n", DEVNAME(sc), sc->sc_runt_off);
#endif
error = xhci_reset(sc);
if (error)
return (error);
if (xhcixfer == NULL) {
xhcixfer = malloc(sizeof(struct pool), M_DEVBUF, M_NOWAIT);
if (xhcixfer == NULL) {
printf("%s: unable to allocate pool descriptor\n",
DEVNAME(sc));
return (ENOMEM);
}
pool_init(xhcixfer, sizeof(struct xhci_xfer), 0, IPL_SOFTUSB,
0, "xhcixfer", NULL);
}
hcr = XREAD4(sc, XHCI_HCCPARAMS);
sc->sc_ctxsize = XHCI_HCC_CSZ(hcr) ? 64 : 32;
DPRINTF(("%s: %d bytes context\n", DEVNAME(sc), sc->sc_ctxsize));
#ifdef XHCI_DEBUG
hcr = XOREAD4(sc, XHCI_PAGESIZE);
printf("%s: supported page size 0x%08x\n", DEVNAME(sc), hcr);
#endif
/* Use 4K for the moment since it's easier. */
sc->sc_pagesize = 4096;
/* Get port and device slot numbers. */
hcr = XREAD4(sc, XHCI_HCSPARAMS1);
sc->sc_noport = XHCI_HCS1_N_PORTS(hcr);
sc->sc_noslot = XHCI_HCS1_DEVSLOT_MAX(hcr);
DPRINTF(("%s: %d ports and %d slots\n", DEVNAME(sc), sc->sc_noport,
sc->sc_noslot));
/* Setup Device Context Base Address Array. */
error = usbd_dma_contig_alloc(&sc->sc_bus, &sc->sc_dcbaa.dma,
(void **)&sc->sc_dcbaa.segs, (sc->sc_noslot + 1) * sizeof(uint64_t),
XHCI_DCBAA_ALIGN, sc->sc_pagesize);
if (error)
return (ENOMEM);
/* Setup command ring. */
rw_init(&sc->sc_cmd_lock, "xhcicmd");
error = xhci_ring_alloc(sc, &sc->sc_cmd_ring, XHCI_MAX_CMDS,
XHCI_CMDS_RING_ALIGN);
if (error) {
printf("%s: could not allocate command ring.\n", DEVNAME(sc));
usbd_dma_contig_free(&sc->sc_bus, &sc->sc_dcbaa.dma);
return (error);
}
/* Setup one event ring and its segment table (ERST). */
error = xhci_ring_alloc(sc, &sc->sc_evt_ring, XHCI_MAX_EVTS,
XHCI_EVTS_RING_ALIGN);
if (error) {
printf("%s: could not allocate event ring.\n", DEVNAME(sc));
xhci_ring_free(sc, &sc->sc_cmd_ring);
usbd_dma_contig_free(&sc->sc_bus, &sc->sc_dcbaa.dma);
return (error);
}
/* Allocate the required entry for the segment table. */
error = usbd_dma_contig_alloc(&sc->sc_bus, &sc->sc_erst.dma,
(void **)&sc->sc_erst.segs, sizeof(struct xhci_erseg),
XHCI_ERST_ALIGN, XHCI_ERST_BOUNDARY);
if (error) {
printf("%s: could not allocate segment table.\n", DEVNAME(sc));
xhci_ring_free(sc, &sc->sc_evt_ring);
xhci_ring_free(sc, &sc->sc_cmd_ring);
usbd_dma_contig_free(&sc->sc_bus, &sc->sc_dcbaa.dma);
return (ENOMEM);
}
/* Set our ring address and size in its corresponding segment. */
sc->sc_erst.segs[0].er_addr = htole64(sc->sc_evt_ring.dma.paddr);
sc->sc_erst.segs[0].er_size = htole32(XHCI_MAX_EVTS);
sc->sc_erst.segs[0].er_rsvd = 0;
bus_dmamap_sync(sc->sc_erst.dma.tag, sc->sc_erst.dma.map, 0,
sc->sc_erst.dma.size, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
/* Get the number of scratch pages and configure them if necessary. */
hcr = XREAD4(sc, XHCI_HCSPARAMS2);
npage = XHCI_HCS2_SPB_MAX(hcr);
DPRINTF(("%s: %u scratch pages, ETE=%u, IST=0x%x\n", DEVNAME(sc), npage,
XHCI_HCS2_ETE(hcr), XHCI_HCS2_IST(hcr)));
if (npage > 0 && xhci_scratchpad_alloc(sc, npage)) {
printf("%s: could not allocate scratchpad.\n", DEVNAME(sc));
usbd_dma_contig_free(&sc->sc_bus, &sc->sc_erst.dma);
xhci_ring_free(sc, &sc->sc_evt_ring);
xhci_ring_free(sc, &sc->sc_cmd_ring);
usbd_dma_contig_free(&sc->sc_bus, &sc->sc_dcbaa.dma);
return (ENOMEM);
}
return (0);
}
void
xhci_config(struct xhci_softc *sc)
{
uint64_t paddr;
uint32_t hcr;
/* Make sure to program a number of device slots we can handle. */
if (sc->sc_noslot > USB_MAX_DEVICES)
sc->sc_noslot = USB_MAX_DEVICES;
hcr = XOREAD4(sc, XHCI_CONFIG) & ~XHCI_CONFIG_SLOTS_MASK;
XOWRITE4(sc, XHCI_CONFIG, hcr | sc->sc_noslot);
/* Set the device context base array address. */
paddr = (uint64_t)sc->sc_dcbaa.dma.paddr;
XOWRITE4(sc, XHCI_DCBAAP_LO, (uint32_t)paddr);
XOWRITE4(sc, XHCI_DCBAAP_HI, (uint32_t)(paddr >> 32));
DPRINTF(("%s: DCBAAP=%#x%#x\n", DEVNAME(sc),
XOREAD4(sc, XHCI_DCBAAP_HI), XOREAD4(sc, XHCI_DCBAAP_LO)));
/* Set the command ring address. */
paddr = (uint64_t)sc->sc_cmd_ring.dma.paddr;
XOWRITE4(sc, XHCI_CRCR_LO, ((uint32_t)paddr) | XHCI_CRCR_LO_RCS);
XOWRITE4(sc, XHCI_CRCR_HI, (uint32_t)(paddr >> 32));
DPRINTF(("%s: CRCR=%#x%#x (%016llx)\n", DEVNAME(sc),
XOREAD4(sc, XHCI_CRCR_HI), XOREAD4(sc, XHCI_CRCR_LO), paddr));
/* Set the ERST count number to 1, since we use only one event ring. */
XRWRITE4(sc, XHCI_ERSTSZ(0), XHCI_ERSTS_SET(1));
/* Set the segment table address. */
paddr = (uint64_t)sc->sc_erst.dma.paddr;
XRWRITE4(sc, XHCI_ERSTBA_LO(0), (uint32_t)paddr);
XRWRITE4(sc, XHCI_ERSTBA_HI(0), (uint32_t)(paddr >> 32));
DPRINTF(("%s: ERSTBA=%#x%#x\n", DEVNAME(sc),
XRREAD4(sc, XHCI_ERSTBA_HI(0)), XRREAD4(sc, XHCI_ERSTBA_LO(0))));
/* Set the ring dequeue address. */
paddr = (uint64_t)sc->sc_evt_ring.dma.paddr;
XRWRITE4(sc, XHCI_ERDP_LO(0), (uint32_t)paddr);
XRWRITE4(sc, XHCI_ERDP_HI(0), (uint32_t)(paddr >> 32));
DPRINTF(("%s: ERDP=%#x%#x\n", DEVNAME(sc),
XRREAD4(sc, XHCI_ERDP_HI(0)), XRREAD4(sc, XHCI_ERDP_LO(0))));
/* Enable interrupts. */
hcr = XRREAD4(sc, XHCI_IMAN(0));
XRWRITE4(sc, XHCI_IMAN(0), hcr | XHCI_IMAN_INTR_ENA);
/* Set default interrupt moderation. */
XRWRITE4(sc, XHCI_IMOD(0), XHCI_IMOD_DEFAULT);
/* Allow event interrupt and start the controller. */
XOWRITE4(sc, XHCI_USBCMD, XHCI_CMD_INTE|XHCI_CMD_RS);
DPRINTF(("%s: USBCMD=%#x\n", DEVNAME(sc), XOREAD4(sc, XHCI_USBCMD)));
DPRINTF(("%s: IMAN=%#x\n", DEVNAME(sc), XRREAD4(sc, XHCI_IMAN(0))));
}
int
xhci_detach(struct device *self, int flags)
{
struct xhci_softc *sc = (struct xhci_softc *)self;
int rv;
rv = config_detach_children(self, flags);
if (rv != 0) {
printf("%s: error while detaching %d\n", DEVNAME(sc), rv);
return (rv);
}
/* Since the hardware might already be gone, ignore the errors. */
xhci_command_abort(sc);
xhci_reset(sc);
/* Disable interrupts. */
XRWRITE4(sc, XHCI_IMOD(0), 0);
XRWRITE4(sc, XHCI_IMAN(0), 0);
/* Clear the event ring address. */
XRWRITE4(sc, XHCI_ERDP_LO(0), 0);
XRWRITE4(sc, XHCI_ERDP_HI(0), 0);
XRWRITE4(sc, XHCI_ERSTBA_LO(0), 0);
XRWRITE4(sc, XHCI_ERSTBA_HI(0), 0);
XRWRITE4(sc, XHCI_ERSTSZ(0), 0);
/* Clear the command ring address. */
XOWRITE4(sc, XHCI_CRCR_LO, 0);
XOWRITE4(sc, XHCI_CRCR_HI, 0);
XOWRITE4(sc, XHCI_DCBAAP_LO, 0);
XOWRITE4(sc, XHCI_DCBAAP_HI, 0);
if (sc->sc_spad.npage > 0)
xhci_scratchpad_free(sc);
usbd_dma_contig_free(&sc->sc_bus, &sc->sc_erst.dma);
xhci_ring_free(sc, &sc->sc_evt_ring);
xhci_ring_free(sc, &sc->sc_cmd_ring);
usbd_dma_contig_free(&sc->sc_bus, &sc->sc_dcbaa.dma);
return (0);
}
int
xhci_activate(struct device *self, int act)
{
struct xhci_softc *sc = (struct xhci_softc *)self;
int rv = 0;
switch (act) {
case DVACT_RESUME:
sc->sc_bus.use_polling++;
xhci_reset(sc);
xhci_ring_reset(sc, &sc->sc_cmd_ring);
xhci_ring_reset(sc, &sc->sc_evt_ring);
/* Renesas controllers, at least, need more time to resume. */
usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
xhci_config(sc);
sc->sc_bus.use_polling--;
rv = config_activate_children(self, act);
break;
case DVACT_POWERDOWN:
rv = config_activate_children(self, act);
xhci_reset(sc);
break;
default:
rv = config_activate_children(self, act);
break;
}
return (rv);
}
int
xhci_reset(struct xhci_softc *sc)
{
uint32_t hcr;
int i;
XOWRITE4(sc, XHCI_USBCMD, 0); /* Halt controller */
for (i = 0; i < 100; i++) {
usb_delay_ms(&sc->sc_bus, 1);
hcr = XOREAD4(sc, XHCI_USBSTS) & XHCI_STS_HCH;
if (hcr)
break;
}
if (!hcr)
printf("%s: halt timeout\n", DEVNAME(sc));
XOWRITE4(sc, XHCI_USBCMD, XHCI_CMD_HCRST);
for (i = 0; i < 100; i++) {
usb_delay_ms(&sc->sc_bus, 1);
hcr = (XOREAD4(sc, XHCI_USBCMD) & XHCI_CMD_HCRST) |
(XOREAD4(sc, XHCI_USBSTS) & XHCI_STS_CNR);
if (!hcr)
break;
}
if (hcr) {
printf("%s: reset timeout\n", DEVNAME(sc));
return (EIO);
}
return (0);
}
int
xhci_intr(void *v)
{
struct xhci_softc *sc = v;
if (sc == NULL || sc->sc_bus.dying)
return (0);
/* If we get an interrupt while polling, then just ignore it. */
if (sc->sc_bus.use_polling) {
DPRINTFN(16, ("xhci_intr: ignored interrupt while polling\n"));
return (0);
}
return (xhci_intr1(sc));
}
int
xhci_intr1(struct xhci_softc *sc)
{
uint32_t intrs;
intrs = XOREAD4(sc, XHCI_USBSTS);
if (intrs == 0xffffffff) {
sc->sc_bus.dying = 1;
return (0);
}
if ((intrs & XHCI_STS_EINT) == 0)
return (0);
sc->sc_bus.no_intrs++;
if (intrs & XHCI_STS_HSE) {
printf("%s: host system error\n", DEVNAME(sc));
sc->sc_bus.dying = 1;
return (1);
}
XOWRITE4(sc, XHCI_USBSTS, intrs); /* Acknowledge */
usb_schedsoftintr(&sc->sc_bus);
/* Acknowledge PCI interrupt */
intrs = XRREAD4(sc, XHCI_IMAN(0));
XRWRITE4(sc, XHCI_IMAN(0), intrs | XHCI_IMAN_INTR_PEND);
return (1);
}
void
xhci_poll(struct usbd_bus *bus)
{
struct xhci_softc *sc = (struct xhci_softc *)bus;
if (XOREAD4(sc, XHCI_USBSTS))
xhci_intr1(sc);
}
void
xhci_softintr(void *v)
{
struct xhci_softc *sc = v;
if (sc->sc_bus.dying)
return;
sc->sc_bus.intr_context++;
xhci_event_dequeue(sc);
sc->sc_bus.intr_context--;
}
void
xhci_event_dequeue(struct xhci_softc *sc)
{
struct xhci_trb *trb;
uint64_t paddr;
uint32_t status, flags;
while ((trb = xhci_ring_consume(sc, &sc->sc_evt_ring)) != NULL) {
paddr = letoh64(trb->trb_paddr);
status = letoh32(trb->trb_status);
flags = letoh32(trb->trb_flags);
switch (flags & XHCI_TRB_TYPE_MASK) {
case XHCI_EVT_XFER:
xhci_event_xfer(sc, paddr, status, flags);
break;
case XHCI_EVT_CMD_COMPLETE:
memcpy(&sc->sc_result_trb, trb, sizeof(*trb));
xhci_event_command(sc, paddr);
break;
case XHCI_EVT_PORT_CHANGE:
xhci_event_port_change(sc, paddr, status);
break;
case XHCI_EVT_HOST_CTRL:
/* TODO */
break;
default:
#ifdef XHCI_DEBUG
printf("event (%d): ", XHCI_TRB_TYPE(flags));
xhci_dump_trb(trb);
#endif
break;
}
}
paddr = (uint64_t)DEQPTR(sc->sc_evt_ring);
XRWRITE4(sc, XHCI_ERDP_LO(0), ((uint32_t)paddr) | XHCI_ERDP_LO_BUSY);
XRWRITE4(sc, XHCI_ERDP_HI(0), (uint32_t)(paddr >> 32));
}
void
xhci_skip_all(struct xhci_pipe *xp)
{
struct usbd_xfer *xfer, *last;
if (xp->skip) {
/*
* Find the last transfer to skip, this is necessary
* as xhci_xfer_done() posts new transfers which we
* don't want to skip
*/
last = SIMPLEQ_FIRST(&xp->pipe.queue);
if (last == NULL)
goto done;
while ((xfer = SIMPLEQ_NEXT(last, next)) != NULL)
last = xfer;
do {
xfer = SIMPLEQ_FIRST(&xp->pipe.queue);
if (xfer == NULL)
goto done;
DPRINTF(("%s: skipping %p\n", __func__, xfer));
xfer->status = USBD_NORMAL_COMPLETION;
xhci_xfer_done(xfer);
} while (xfer != last);
done:
xp->skip = 0;
}
}
void
xhci_event_xfer(struct xhci_softc *sc, uint64_t paddr, uint32_t status,
uint32_t flags)
{
struct xhci_pipe *xp;
struct usbd_xfer *xfer;
uint8_t dci, slot, code, xfertype;
uint32_t remain;
int trb_idx;
slot = XHCI_TRB_GET_SLOT(flags);
dci = XHCI_TRB_GET_EP(flags);
if (slot > sc->sc_noslot) {
DPRINTF(("%s: incorrect slot (%u)\n", DEVNAME(sc), slot));
return;
}
xp = sc->sc_sdevs[slot].pipes[dci - 1];
if (xp == NULL) {
DPRINTF(("%s: incorrect dci (%u)\n", DEVNAME(sc), dci));
return;
}
code = XHCI_TRB_GET_CODE(status);
remain = XHCI_TRB_REMAIN(status);
switch (code) {
case XHCI_CODE_RING_UNDERRUN:
DPRINTF(("%s: slot %u underrun with %zu TRB\n", DEVNAME(sc),
slot, xp->ring.ntrb - xp->free_trbs));
xhci_skip_all(xp);
return;
case XHCI_CODE_RING_OVERRUN:
DPRINTF(("%s: slot %u overrun with %zu TRB\n", DEVNAME(sc),
slot, xp->ring.ntrb - xp->free_trbs));
xhci_skip_all(xp);
return;
case XHCI_CODE_MISSED_SRV:
DPRINTF(("%s: slot %u missed srv with %zu TRB\n", DEVNAME(sc),
slot, xp->ring.ntrb - xp->free_trbs));
xp->skip = 1;
return;
default:
break;
}
trb_idx = (paddr - xp->ring.dma.paddr) / sizeof(struct xhci_trb);
if (trb_idx < 0 || trb_idx >= xp->ring.ntrb) {
printf("%s: wrong trb index (%u) max is %zu\n", DEVNAME(sc),
trb_idx, xp->ring.ntrb - 1);
return;
}
xfer = xp->pending_xfers[trb_idx];
if (xfer == NULL) {
DPRINTF(("%s: NULL xfer pointer\n", DEVNAME(sc)));
return;
}
if (remain > xfer->length)
remain = xfer->length;
xfertype = UE_GET_XFERTYPE(xfer->pipe->endpoint->edesc->bmAttributes);
switch (xfertype) {
case UE_BULK:
case UE_INTERRUPT:
case UE_CONTROL:
if (xhci_event_xfer_generic(sc, xfer, xp, remain, trb_idx,
code, slot, dci))
return;
break;
case UE_ISOCHRONOUS:
if (xhci_event_xfer_isoc(xfer, xp, remain, trb_idx))
return;
break;
default:
panic("xhci_event_xfer: unknown xfer type %u", xfertype);
}
xhci_xfer_done(xfer);
}
uint32_t
xhci_xfer_length_generic(struct xhci_xfer *xx, struct xhci_pipe *xp,
int trb_idx)
{
int trb0_idx;
uint32_t len = 0, type;
trb0_idx =
((xx->index + xp->ring.ntrb) - xx->ntrb) % (xp->ring.ntrb - 1);
while (1) {
type = letoh32(xp->ring.trbs[trb0_idx].trb_flags) &
XHCI_TRB_TYPE_MASK;
if (type == XHCI_TRB_TYPE_NORMAL || type == XHCI_TRB_TYPE_DATA)
len += XHCI_TRB_LEN(letoh32(
xp->ring.trbs[trb0_idx].trb_status));
if (trb0_idx == trb_idx)
break;
if (++trb0_idx == xp->ring.ntrb)
trb0_idx = 0;
}
return len;
}
int
xhci_event_xfer_generic(struct xhci_softc *sc, struct usbd_xfer *xfer,
struct xhci_pipe *xp, uint32_t remain, int trb_idx,
uint8_t code, uint8_t slot, uint8_t dci)
{
struct xhci_xfer *xx = (struct xhci_xfer *)xfer;
switch (code) {
case XHCI_CODE_SUCCESS:
if (xfer->actlen == 0) {
if (remain)
xfer->actlen =
xhci_xfer_length_generic(xx, xp, trb_idx) -
remain;
else
xfer->actlen = xfer->length;
}
xfer->status = USBD_NORMAL_COMPLETION;
break;
case XHCI_CODE_SHORT_XFER:
/*
* Use values from the transfer TRB instead of the status TRB.
*/
if (xfer->actlen == 0)
xfer->actlen =
xhci_xfer_length_generic(xx, xp, trb_idx) - remain;
/*
* If this is not the last TRB of a transfer, we should
* theoretically clear the IOC at the end of the chain
* but the HC might have already processed it before we
* had a chance to schedule the softinterrupt.
*/
if (xx->index != trb_idx) {
DPRINTF(("%s: short xfer %p for %u\n",
DEVNAME(sc), xfer, xx->index));
return (1);
}
xfer->status = USBD_NORMAL_COMPLETION;
break;
case XHCI_CODE_TXERR:
case XHCI_CODE_SPLITERR:
DPRINTF(("%s: txerr? code %d\n", DEVNAME(sc), code));
xfer->status = USBD_IOERROR;
break;
case XHCI_CODE_STALL:
case XHCI_CODE_BABBLE:
DPRINTF(("%s: babble code %d\n", DEVNAME(sc), code));
/* Prevent any timeout to kick in. */
timeout_del(&xfer->timeout_handle);
usb_rem_task(xfer->device, &xfer->abort_task);
/* We need to report this condition for umass(4). */
if (code == XHCI_CODE_STALL)
xp->halted = USBD_STALLED;
else
xp->halted = USBD_IOERROR;
/*
* Since the stack might try to start a new transfer as
* soon as a pending one finishes, make sure the endpoint
* is fully reset before calling usb_transfer_complete().
*/
xp->aborted_xfer = xfer;
xhci_cmd_reset_ep_async(sc, slot, dci);
return (1);
case XHCI_CODE_XFER_STOPPED:
case XHCI_CODE_XFER_STOPINV:
/* Endpoint stopped while processing a TD. */
if (xfer == xp->aborted_xfer) {
DPRINTF(("%s: stopped xfer=%p\n", __func__, xfer));
return (1);
}
/* FALLTHROUGH */
default:
DPRINTF(("%s: unhandled code %d\n", DEVNAME(sc), code));
xfer->status = USBD_IOERROR;
xp->halted = 1;
break;
}
return (0);
}
int
xhci_event_xfer_isoc(struct usbd_xfer *xfer, struct xhci_pipe *xp,
uint32_t remain, int trb_idx)
{
struct usbd_xfer *skipxfer;
struct xhci_xfer *xx = (struct xhci_xfer *)xfer;
int trb0_idx, frame_idx = 0;
KASSERT(xx->index >= 0);
trb0_idx =
((xx->index + xp->ring.ntrb) - xx->ntrb) % (xp->ring.ntrb - 1);
/* Find the according frame index for this TRB. */
while (trb0_idx != trb_idx) {
if ((letoh32(xp->ring.trbs[trb0_idx].trb_flags) &
XHCI_TRB_TYPE_MASK) == XHCI_TRB_TYPE_ISOCH)
frame_idx++;
if (trb0_idx++ == (xp->ring.ntrb - 1))
trb0_idx = 0;
}
/*
* If we queued two TRBs for a frame and this is the second TRB,
* check if the first TRB needs accounting since it might not have
* raised an interrupt in case of full data received.
*/
if ((letoh32(xp->ring.trbs[trb_idx].trb_flags) & XHCI_TRB_TYPE_MASK) ==
XHCI_TRB_TYPE_NORMAL) {
frame_idx--;
if (trb_idx == 0)
trb0_idx = xp->ring.ntrb - 2;
else
trb0_idx = trb_idx - 1;
if (xfer->frlengths[frame_idx] == 0) {
xfer->frlengths[frame_idx] = XHCI_TRB_LEN(letoh32(
xp->ring.trbs[trb0_idx].trb_status));
}
}
xfer->frlengths[frame_idx] +=
XHCI_TRB_LEN(letoh32(xp->ring.trbs[trb_idx].trb_status)) - remain;
xfer->actlen += xfer->frlengths[frame_idx];
if (xx->index != trb_idx)
return (1);
if (xp->skip) {
while (1) {
skipxfer = SIMPLEQ_FIRST(&xp->pipe.queue);
if (skipxfer == xfer || skipxfer == NULL)
break;
DPRINTF(("%s: skipping %p\n", __func__, skipxfer));
skipxfer->status = USBD_NORMAL_COMPLETION;
xhci_xfer_done(skipxfer);
}
xp->skip = 0;
}
xfer->status = USBD_NORMAL_COMPLETION;
return (0);
}
void
xhci_event_command(struct xhci_softc *sc, uint64_t paddr)
{
struct xhci_trb *trb;
struct xhci_pipe *xp;
uint32_t flags;
uint8_t dci, slot;
int trb_idx, status;
trb_idx = (paddr - sc->sc_cmd_ring.dma.paddr) / sizeof(*trb);
if (trb_idx < 0 || trb_idx >= sc->sc_cmd_ring.ntrb) {
printf("%s: wrong trb index (%u) max is %zu\n", DEVNAME(sc),
trb_idx, sc->sc_cmd_ring.ntrb - 1);
return;
}
trb = &sc->sc_cmd_ring.trbs[trb_idx];
bus_dmamap_sync(sc->sc_cmd_ring.dma.tag, sc->sc_cmd_ring.dma.map,
TRBOFF(&sc->sc_cmd_ring, trb), sizeof(struct xhci_trb),
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
flags = letoh32(trb->trb_flags);
slot = XHCI_TRB_GET_SLOT(flags);
dci = XHCI_TRB_GET_EP(flags);
switch (flags & XHCI_TRB_TYPE_MASK) {
case XHCI_CMD_RESET_EP:
xp = sc->sc_sdevs[slot].pipes[dci - 1];
if (xp == NULL)
break;
/* Update the dequeue pointer past the last TRB. */
xhci_cmd_set_tr_deq_async(sc, xp->slot, xp->dci,
DEQPTR(xp->ring) | xp->ring.toggle);
break;
case XHCI_CMD_SET_TR_DEQ:
xp = sc->sc_sdevs[slot].pipes[dci - 1];
if (xp == NULL)
break;
status = xp->halted;
xp->halted = 0;
if (xp->aborted_xfer != NULL) {
xp->aborted_xfer->status = status;
xhci_xfer_done(xp->aborted_xfer);
wakeup(xp);
}
break;
case XHCI_CMD_CONFIG_EP:
case XHCI_CMD_STOP_EP:
case XHCI_CMD_DISABLE_SLOT:
case XHCI_CMD_ENABLE_SLOT:
case XHCI_CMD_ADDRESS_DEVICE:
case XHCI_CMD_EVAL_CTX:
case XHCI_CMD_NOOP:
/* All these commands are synchronous. */
KASSERT(sc->sc_cmd_trb == trb);
sc->sc_cmd_trb = NULL;
wakeup(&sc->sc_cmd_trb);
break;
default:
DPRINTF(("%s: unexpected command %x\n", DEVNAME(sc), flags));
}
}
void
xhci_event_port_change(struct xhci_softc *sc, uint64_t paddr, uint32_t status)
{
struct usbd_xfer *xfer = sc->sc_intrxfer;
uint32_t port = XHCI_TRB_PORTID(paddr);
uint8_t *p;
if (XHCI_TRB_GET_CODE(status) != XHCI_CODE_SUCCESS) {
DPRINTF(("%s: failed port status event\n", DEVNAME(sc)));
return;
}
if (xfer == NULL)
return;
p = KERNADDR(&xfer->dmabuf, 0);
memset(p, 0, xfer->length);
p[port/8] |= 1 << (port%8);
DPRINTF(("%s: port=%d change=0x%02x\n", DEVNAME(sc), port, *p));
xfer->actlen = xfer->length;
xfer->status = USBD_NORMAL_COMPLETION;
usb_transfer_complete(xfer);
}
void
xhci_xfer_done(struct usbd_xfer *xfer)
{
struct xhci_pipe *xp = (struct xhci_pipe *)xfer->pipe;
struct xhci_xfer *xx = (struct xhci_xfer *)xfer;
int ntrb, i;
splsoftassert(IPL_SOFTUSB);
#ifdef XHCI_DEBUG
if (xx->index < 0 || xp->pending_xfers[xx->index] == NULL) {
printf("%s: xfer=%p done (idx=%d, ntrb=%zd)\n", __func__,
xfer, xx->index, xx->ntrb);
}
#endif
if (xp->aborted_xfer == xfer)
xp->aborted_xfer = NULL;
for (ntrb = 0, i = xx->index; ntrb < xx->ntrb; ntrb++, i--) {
xp->pending_xfers[i] = NULL;
if (i == 0)
i = (xp->ring.ntrb - 1);
}
xp->free_trbs += xx->ntrb;
xx->index = -1;
xx->ntrb = 0;
timeout_del(&xfer->timeout_handle);
usb_rem_task(xfer->device, &xfer->abort_task);
usb_transfer_complete(xfer);
}
/*
* Calculate the Device Context Index (DCI) for endpoints as stated
* in section 4.5.1 of xHCI specification r1.1.
*/
static inline uint8_t
xhci_ed2dci(usb_endpoint_descriptor_t *ed)
{
uint8_t dir;
if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL)
return (UE_GET_ADDR(ed->bEndpointAddress) * 2 + 1);
if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
dir = 1;
else
dir = 0;
return (UE_GET_ADDR(ed->bEndpointAddress) * 2 + dir);
}
usbd_status
xhci_pipe_open(struct usbd_pipe *pipe)
{
struct xhci_softc *sc = (struct xhci_softc *)pipe->device->bus;
struct xhci_pipe *xp = (struct xhci_pipe *)pipe;
usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
uint8_t slot = 0, xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
int error;
KASSERT(xp->slot == 0);
if (sc->sc_bus.dying)
return (USBD_IOERROR);
/* Root Hub */
if (pipe->device->depth == 0) {
switch (ed->bEndpointAddress) {
case USB_CONTROL_ENDPOINT:
pipe->methods = &xhci_root_ctrl_methods;
break;
case UE_DIR_IN | XHCI_INTR_ENDPT:
pipe->methods = &xhci_root_intr_methods;
break;
default:
pipe->methods = NULL;
return (USBD_INVAL);
}
return (USBD_NORMAL_COMPLETION);
}
#if 0
/* Issue a noop to check if the command ring is correctly configured. */
xhci_cmd_noop(sc);
#endif
switch (xfertype) {
case UE_CONTROL:
pipe->methods = &xhci_device_ctrl_methods;
/*
* Get a slot and init the device's contexts.
*
* Since the control enpoint, represented as the default
* pipe, is always opened first we are dealing with a
* new device. Put a new slot in the ENABLED state.
*
*/
error = xhci_cmd_slot_control(sc, &slot, 1);
if (error || slot == 0 || slot > sc->sc_noslot)
return (USBD_INVAL);
if (xhci_softdev_alloc(sc, slot)) {
xhci_cmd_slot_control(sc, &slot, 0);
return (USBD_NOMEM);
}
break;
case UE_ISOCHRONOUS:
pipe->methods = &xhci_device_isoc_methods;
break;
case UE_BULK:
pipe->methods = &xhci_device_bulk_methods;
break;
case UE_INTERRUPT:
pipe->methods = &xhci_device_intr_methods;
break;
default:
return (USBD_INVAL);
}
/*
* Our USBD Bus Interface is pipe-oriented but for most of the
* operations we need to access a device context, so keep track
* of the slot ID in every pipe.
*/
if (slot == 0)
slot = ((struct xhci_pipe *)pipe->device->default_pipe)->slot;
xp->slot = slot;
xp->dci = xhci_ed2dci(ed);
if (xhci_pipe_init(sc, pipe)) {
xhci_cmd_slot_control(sc, &slot, 0);
return (USBD_IOERROR);
}
return (USBD_NORMAL_COMPLETION);
}
/*
* Set the maximum Endpoint Service Interface Time (ESIT) payload and
* the average TRB buffer length for an endpoint.
*/
static inline uint32_t
xhci_get_txinfo(struct xhci_softc *sc, struct usbd_pipe *pipe)
{
usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
uint32_t mep, atl, mps = UGETW(ed->wMaxPacketSize);
switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
case UE_CONTROL:
mep = 0;
atl = 8;
break;
case UE_INTERRUPT:
case UE_ISOCHRONOUS:
if (pipe->device->speed == USB_SPEED_SUPER) {
/* XXX Read the companion descriptor */
}
mep = (UE_GET_TRANS(mps) + 1) * UE_GET_SIZE(mps);
atl = mep;
break;
case UE_BULK:
default:
mep = 0;
atl = 0;
}
return (XHCI_EPCTX_MAX_ESIT_PAYLOAD(mep) | XHCI_EPCTX_AVG_TRB_LEN(atl));
}
static inline uint32_t
xhci_linear_interval(usb_endpoint_descriptor_t *ed)
{
uint32_t ival = min(max(1, ed->bInterval), 255);
return (fls(ival) - 1);
}
static inline uint32_t
xhci_exponential_interval(usb_endpoint_descriptor_t *ed)
{
uint32_t ival = min(max(1, ed->bInterval), 16);
return (ival - 1);
}
/*
* Return interval for endpoint expressed in 2^(ival) * 125us.
*
* See section 6.2.3.6 of xHCI r1.1 Specification for more details.
*/
uint32_t
xhci_pipe_interval(struct usbd_pipe *pipe)
{
usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
uint8_t speed = pipe->device->speed;
uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
uint32_t ival;
if (xfertype == UE_CONTROL || xfertype == UE_BULK) {
/* Control and Bulk endpoints never NAKs. */
ival = 0;
} else {
switch (speed) {
case USB_SPEED_FULL:
if (xfertype == UE_ISOCHRONOUS) {
/* Convert 1-2^(15)ms into 3-18 */
ival = xhci_exponential_interval(ed) + 3;
break;
}
/* FALLTHROUGH */
case USB_SPEED_LOW:
/* Convert 1-255ms into 3-10 */
ival = xhci_linear_interval(ed) + 3;
break;
case USB_SPEED_HIGH:
case USB_SPEED_SUPER:
default:
/* Convert 1-2^(15) * 125us into 0-15 */
ival = xhci_exponential_interval(ed);
break;
}
}
KASSERT(ival <= 15);
return (XHCI_EPCTX_SET_IVAL(ival));
}
uint32_t
xhci_pipe_maxburst(struct usbd_pipe *pipe)
{
usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
uint32_t mps = UGETW(ed->wMaxPacketSize);
uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
uint32_t maxb = 0;
switch (pipe->device->speed) {
case USB_SPEED_HIGH:
if (xfertype == UE_ISOCHRONOUS || xfertype == UE_INTERRUPT)
maxb = UE_GET_TRANS(mps);
break;
case USB_SPEED_SUPER:
/* XXX Read the companion descriptor */
default:
break;
}
return (maxb);
}
static inline uint32_t
xhci_last_valid_dci(struct xhci_pipe **pipes, struct xhci_pipe *ignore)
{
struct xhci_pipe *lxp;
int i;
/* Find the last valid Endpoint Context. */
for (i = 30; i >= 0; i--) {
lxp = pipes[i];
if (lxp != NULL && lxp != ignore)
return XHCI_SCTX_DCI(lxp->dci);
}
return 0;
}
int
xhci_context_setup(struct xhci_softc *sc, struct usbd_pipe *pipe)
{
struct xhci_pipe *xp = (struct xhci_pipe *)pipe;
struct xhci_soft_dev *sdev = &sc->sc_sdevs[xp->slot];
usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
uint32_t mps = UGETW(ed->wMaxPacketSize);
uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
uint8_t speed, cerr = 0;
uint32_t route = 0, rhport = 0;
struct usbd_device *hub;
/*
* Calculate the Route String. Assume that there is no hub with
* more than 15 ports and that they all have a detph < 6. See
* section 8.9 of USB 3.1 Specification for more details.
*/
for (hub = pipe->device; hub->myhub->depth; hub = hub->myhub) {
uint32_t port = hub->powersrc->portno;
uint32_t depth = hub->myhub->depth;
route |= port << (4 * (depth - 1));
}
/* Get Root Hub port */
rhport = hub->powersrc->portno;
switch (pipe->device->speed) {
case USB_SPEED_LOW:
speed = XHCI_SPEED_LOW;
break;
case USB_SPEED_FULL:
speed = XHCI_SPEED_FULL;
break;
case USB_SPEED_HIGH:
speed = XHCI_SPEED_HIGH;
break;
case USB_SPEED_SUPER:
speed = XHCI_SPEED_SUPER;
break;
default:
return (USBD_INVAL);
}
/* Setup the endpoint context */
if (xfertype != UE_ISOCHRONOUS)
cerr = 3;
if ((ed->bEndpointAddress & UE_DIR_IN) || (xfertype == UE_CONTROL))
xfertype |= 0x4;
sdev->ep_ctx[xp->dci-1]->info_lo = htole32(xhci_pipe_interval(pipe));
sdev->ep_ctx[xp->dci-1]->info_hi = htole32(
XHCI_EPCTX_SET_MPS(UE_GET_SIZE(mps)) |
XHCI_EPCTX_SET_MAXB(xhci_pipe_maxburst(pipe)) |
XHCI_EPCTX_SET_EPTYPE(xfertype) | XHCI_EPCTX_SET_CERR(cerr)
);
sdev->ep_ctx[xp->dci-1]->txinfo = htole32(xhci_get_txinfo(sc, pipe));
sdev->ep_ctx[xp->dci-1]->deqp = htole64(
DEQPTR(xp->ring) | xp->ring.toggle
);
/* Unmask the new endoint */
sdev->input_ctx->drop_flags = 0;
sdev->input_ctx->add_flags = htole32(XHCI_INCTX_MASK_DCI(xp->dci));
/* Setup the slot context */
sdev->slot_ctx->info_lo = htole32(
xhci_last_valid_dci(sdev->pipes, NULL) | XHCI_SCTX_SPEED(speed) |
XHCI_SCTX_ROUTE(route)
);
sdev->slot_ctx->info_hi = htole32(XHCI_SCTX_RHPORT(rhport));
sdev->slot_ctx->tt = 0;
sdev->slot_ctx->state = 0;
/* XXX */
#define UHUB_IS_MTT(dev) (dev->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT)
/*
* If we are opening the interrupt pipe of a hub, update its
* context before putting it in the CONFIGURED state.
*/
if (pipe->device->hub != NULL) {
int nports = pipe->device->hub->nports;
sdev->slot_ctx->info_lo |= htole32(XHCI_SCTX_HUB(1));
sdev->slot_ctx->info_hi |= htole32(XHCI_SCTX_NPORTS(nports));
if (UHUB_IS_MTT(pipe->device))
sdev->slot_ctx->info_lo |= htole32(XHCI_SCTX_MTT(1));
sdev->slot_ctx->tt |= htole32(
XHCI_SCTX_TT_THINK_TIME(pipe->device->hub->ttthink)
);
}
/*
* If this is a Low or Full Speed device below an external High
* Speed hub, it needs some TT love.
*/
if (speed < XHCI_SPEED_HIGH && pipe->device->myhsport != NULL) {
struct usbd_device *hshub = pipe->device->myhsport->parent;
uint8_t slot = ((struct xhci_pipe *)hshub->default_pipe)->slot;
if (UHUB_IS_MTT(hshub))
sdev->slot_ctx->info_lo |= htole32(XHCI_SCTX_MTT(1));
sdev->slot_ctx->tt |= htole32(
XHCI_SCTX_TT_HUB_SID(slot) |
XHCI_SCTX_TT_PORT_NUM(pipe->device->myhsport->portno)
);
}
#undef UHUB_IS_MTT
/* Unmask the slot context */
sdev->input_ctx->add_flags |= htole32(XHCI_INCTX_MASK_DCI(0));
bus_dmamap_sync(sdev->ictx_dma.tag, sdev->ictx_dma.map, 0,
sc->sc_pagesize, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
return (0);
}
int
xhci_pipe_init(struct xhci_softc *sc, struct usbd_pipe *pipe)
{
struct xhci_pipe *xp = (struct xhci_pipe *)pipe;
struct xhci_soft_dev *sdev = &sc->sc_sdevs[xp->slot];
int error;
#ifdef XHCI_DEBUG
struct usbd_device *dev = pipe->device;
printf("%s: pipe=%p addr=%d depth=%d port=%d speed=%d dev %d dci %u"
" (epAddr=0x%x)\n", __func__, pipe, dev->address, dev->depth,
dev->powersrc->portno, dev->speed, xp->slot, xp->dci,
pipe->endpoint->edesc->bEndpointAddress);
#endif
if (xhci_ring_alloc(sc, &xp->ring, XHCI_MAX_XFER, XHCI_XFER_RING_ALIGN))
return (ENOMEM);
xp->free_trbs = xp->ring.ntrb;
xp->halted = 0;
sdev->pipes[xp->dci - 1] = xp;
error = xhci_context_setup(sc, pipe);
if (error)
return (error);
if (xp->dci == 1) {
/*
* If we are opening the default pipe, the Slot should
* be in the ENABLED state. Issue an "Address Device"
* with BSR=1 to put the device in the DEFAULT state.
* We cannot jump directly to the ADDRESSED state with
* BSR=0 because some Low/Full speed devices won't accept
* a SET_ADDRESS command before we've read their device
* descriptor.
*/
error = xhci_cmd_set_address(sc, xp->slot,
sdev->ictx_dma.paddr, XHCI_TRB_BSR);
} else {
error = xhci_cmd_configure_ep(sc, xp->slot,
sdev->ictx_dma.paddr);
}
if (error) {
xhci_ring_free(sc, &xp->ring);
return (EIO);
}
return (0);
}
void
xhci_pipe_close(struct usbd_pipe *pipe)
{
struct xhci_softc *sc = (struct xhci_softc *)pipe->device->bus;
struct xhci_pipe *xp = (struct xhci_pipe *)pipe;
struct xhci_soft_dev *sdev = &sc->sc_sdevs[xp->slot];
/* Root Hub */
if (pipe->device->depth == 0)
return;
/* Mask the endpoint */
sdev->input_ctx->drop_flags = htole32(XHCI_INCTX_MASK_DCI(xp->dci));
sdev->input_ctx->add_flags = 0;
/* Update last valid Endpoint Context */
sdev->slot_ctx->info_lo &= htole32(~XHCI_SCTX_DCI(31));
sdev->slot_ctx->info_lo |= htole32(xhci_last_valid_dci(sdev->pipes, xp));
/* Clear the Endpoint Context */
memset(sdev->ep_ctx[xp->dci - 1], 0, sizeof(struct xhci_epctx));
bus_dmamap_sync(sdev->ictx_dma.tag, sdev->ictx_dma.map, 0,
sc->sc_pagesize, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
if (xhci_cmd_configure_ep(sc, xp->slot, sdev->ictx_dma.paddr))
DPRINTF(("%s: error clearing ep (%d)\n", DEVNAME(sc), xp->dci));
xhci_ring_free(sc, &xp->ring);
sdev->pipes[xp->dci - 1] = NULL;
/*
* If we are closing the default pipe, the device is probably
* gone, so put its slot in the DISABLED state.
*/
if (xp->dci == 1) {
xhci_cmd_slot_control(sc, &xp->slot, 0);
xhci_softdev_free(sc, xp->slot);
}
}
/*
* Transition a device from DEFAULT to ADDRESSED Slot state, this hook
* is needed for Low/Full speed devices.
*
* See section 4.5.3 of USB 3.1 Specification for more details.
*/
int
xhci_setaddr(struct usbd_device *dev, int addr)
{
struct xhci_softc *sc = (struct xhci_softc *)dev->bus;
struct xhci_pipe *xp = (struct xhci_pipe *)dev->default_pipe;
struct xhci_soft_dev *sdev = &sc->sc_sdevs[xp->slot];
int error;
/* Root Hub */
if (dev->depth == 0)
return (0);
KASSERT(xp->dci == 1);
error = xhci_context_setup(sc, dev->default_pipe);
if (error)
return (error);
error = xhci_cmd_set_address(sc, xp->slot, sdev->ictx_dma.paddr, 0);
#ifdef XHCI_DEBUG
if (error == 0) {
struct xhci_sctx *sctx;
uint8_t addr;
bus_dmamap_sync(sdev->octx_dma.tag, sdev->octx_dma.map, 0,
sc->sc_pagesize, BUS_DMASYNC_POSTREAD);
/* Get output slot context. */
sctx = (struct xhci_sctx *)sdev->octx_dma.vaddr;
addr = XHCI_SCTX_DEV_ADDR(letoh32(sctx->state));
error = (addr == 0);
printf("%s: dev %d addr %d\n", DEVNAME(sc), xp->slot, addr);
}
#endif
return (error);
}
struct usbd_xfer *
xhci_allocx(struct usbd_bus *bus)
{
return (pool_get(xhcixfer, PR_NOWAIT | PR_ZERO));
}
void
xhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
{
pool_put(xhcixfer, xfer);
}
int
xhci_scratchpad_alloc(struct xhci_softc *sc, int npage)
{
uint64_t *pte;
int error, i;
/* Allocate the required entry for the table. */
error = usbd_dma_contig_alloc(&sc->sc_bus, &sc->sc_spad.table_dma,
(void **)&pte, npage * sizeof(uint64_t), XHCI_SPAD_TABLE_ALIGN,
sc->sc_pagesize);
if (error)
return (ENOMEM);
/* Allocate pages. XXX does not need to be contiguous. */
error = usbd_dma_contig_alloc(&sc->sc_bus, &sc->sc_spad.pages_dma,
NULL, npage * sc->sc_pagesize, sc->sc_pagesize, 0);
if (error) {
usbd_dma_contig_free(&sc->sc_bus, &sc->sc_spad.table_dma);
return (ENOMEM);
}
for (i = 0; i < npage; i++) {
pte[i] = htole64(
sc->sc_spad.pages_dma.paddr + (i * sc->sc_pagesize)
);
}
bus_dmamap_sync(sc->sc_spad.table_dma.tag, sc->sc_spad.table_dma.map, 0,
npage * sizeof(uint64_t), BUS_DMASYNC_PREREAD |
BUS_DMASYNC_PREWRITE);
/* Entry 0 points to the table of scratchpad pointers. */
sc->sc_dcbaa.segs[0] = htole64(sc->sc_spad.table_dma.paddr);
bus_dmamap_sync(sc->sc_dcbaa.dma.tag, sc->sc_dcbaa.dma.map, 0,
sizeof(uint64_t), BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
sc->sc_spad.npage = npage;
return (0);
}
void
xhci_scratchpad_free(struct xhci_softc *sc)
{
sc->sc_dcbaa.segs[0] = 0;
bus_dmamap_sync(sc->sc_dcbaa.dma.tag, sc->sc_dcbaa.dma.map, 0,
sizeof(uint64_t), BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
usbd_dma_contig_free(&sc->sc_bus, &sc->sc_spad.pages_dma);
usbd_dma_contig_free(&sc->sc_bus, &sc->sc_spad.table_dma);
}
int
xhci_ring_alloc(struct xhci_softc *sc, struct xhci_ring *ring, size_t ntrb,
size_t alignment)
{
size_t size;
int error;
size = ntrb * sizeof(struct xhci_trb);
error = usbd_dma_contig_alloc(&sc->sc_bus, &ring->dma,
(void **)&ring->trbs, size, alignment, XHCI_RING_BOUNDARY);
if (error)
return (error);
ring->ntrb = ntrb;
xhci_ring_reset(sc, ring);
return (0);
}
void
xhci_ring_free(struct xhci_softc *sc, struct xhci_ring *ring)
{
usbd_dma_contig_free(&sc->sc_bus, &ring->dma);
}
void
xhci_ring_reset(struct xhci_softc *sc, struct xhci_ring *ring)
{
size_t size;
size = ring->ntrb * sizeof(struct xhci_trb);
memset(ring->trbs, 0, size);
ring->index = 0;
ring->toggle = XHCI_TRB_CYCLE;
/*
* Since all our rings use only one segment, at least for
* the moment, link their tail to their head.
*/
if (ring != &sc->sc_evt_ring) {
struct xhci_trb *trb = &ring->trbs[ring->ntrb - 1];
trb->trb_paddr = htole64(ring->dma.paddr);
trb->trb_flags = htole32(XHCI_TRB_TYPE_LINK | XHCI_TRB_LINKSEG |
XHCI_TRB_CYCLE);
bus_dmamap_sync(ring->dma.tag, ring->dma.map, 0, size,
BUS_DMASYNC_PREWRITE);
} else
bus_dmamap_sync(ring->dma.tag, ring->dma.map, 0, size,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
}
struct xhci_trb*
xhci_ring_consume(struct xhci_softc *sc, struct xhci_ring *ring)
{
struct xhci_trb *trb = &ring->trbs[ring->index];
KASSERT(ring->index < ring->ntrb);
bus_dmamap_sync(ring->dma.tag, ring->dma.map, TRBOFF(ring, trb),
sizeof(struct xhci_trb), BUS_DMASYNC_POSTREAD);
/* Make sure this TRB can be consumed. */
if (ring->toggle != (letoh32(trb->trb_flags) & XHCI_TRB_CYCLE))
return (NULL);
ring->index++;
if (ring->index == ring->ntrb) {
ring->index = 0;
ring->toggle ^= 1;
}
return (trb);
}
struct xhci_trb*
xhci_ring_produce(struct xhci_softc *sc, struct xhci_ring *ring)
{
struct xhci_trb *lnk, *trb;
KASSERT(ring->index < ring->ntrb);
/* Setup the link TRB after the previous TRB is done. */
if (ring->index == 0) {
lnk = &ring->trbs[ring->ntrb - 1];
trb = &ring->trbs[ring->ntrb - 2];
bus_dmamap_sync(ring->dma.tag, ring->dma.map, TRBOFF(ring, lnk),
sizeof(struct xhci_trb), BUS_DMASYNC_POSTREAD |
BUS_DMASYNC_POSTWRITE);
lnk->trb_flags &= htole32(~XHCI_TRB_CHAIN);
if (letoh32(trb->trb_flags) & XHCI_TRB_CHAIN)
lnk->trb_flags |= htole32(XHCI_TRB_CHAIN);
bus_dmamap_sync(ring->dma.tag, ring->dma.map, TRBOFF(ring, lnk),
sizeof(struct xhci_trb), BUS_DMASYNC_PREWRITE);
lnk->trb_flags ^= htole32(XHCI_TRB_CYCLE);
bus_dmamap_sync(ring->dma.tag, ring->dma.map, TRBOFF(ring, lnk),
sizeof(struct xhci_trb), BUS_DMASYNC_PREWRITE);
}
trb = &ring->trbs[ring->index++];
bus_dmamap_sync(ring->dma.tag, ring->dma.map, TRBOFF(ring, trb),
sizeof(struct xhci_trb), BUS_DMASYNC_POSTREAD |
BUS_DMASYNC_POSTWRITE);
/* Toggle cycle state of the link TRB and skip it. */
if (ring->index == (ring->ntrb - 1)) {
ring->index = 0;
ring->toggle ^= 1;
}
return (trb);
}
struct xhci_trb *
xhci_xfer_get_trb(struct xhci_softc *sc, struct usbd_xfer *xfer,
uint8_t *togglep, int last)
{
struct xhci_pipe *xp = (struct xhci_pipe *)xfer->pipe;
struct xhci_xfer *xx = (struct xhci_xfer *)xfer;
KASSERT(xp->free_trbs >= 1);
xp->free_trbs--;
*togglep = xp->ring.toggle;
switch (last) {
case -1: /* This will be a zero-length TD. */
xp->pending_xfers[xp->ring.index] = NULL;
break;
case 0: /* This will be in a chain. */
xp->pending_xfers[xp->ring.index] = xfer;
xx->index = -2;
xx->ntrb += 1;
break;
case 1: /* This will terminate a chain. */
xp->pending_xfers[xp->ring.index] = xfer;
xx->index = xp->ring.index;
xx->ntrb += 1;
break;
}
return (xhci_ring_produce(sc, &xp->ring));
}
int
xhci_command_submit(struct xhci_softc *sc, struct xhci_trb *trb0, int timeout)
{
struct xhci_trb *trb;
int s, error = 0;
KASSERT(timeout == 0 || sc->sc_cmd_trb == NULL);
trb0->trb_flags |= htole32(sc->sc_cmd_ring.toggle);
trb = xhci_ring_produce(sc, &sc->sc_cmd_ring);
if (trb == NULL)
return (EAGAIN);
trb->trb_paddr = trb0->trb_paddr;
trb->trb_status = trb0->trb_status;
bus_dmamap_sync(sc->sc_cmd_ring.dma.tag, sc->sc_cmd_ring.dma.map,
TRBOFF(&sc->sc_cmd_ring, trb), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
trb->trb_flags = trb0->trb_flags;
bus_dmamap_sync(sc->sc_cmd_ring.dma.tag, sc->sc_cmd_ring.dma.map,
TRBOFF(&sc->sc_cmd_ring, trb), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
if (timeout == 0) {
XDWRITE4(sc, XHCI_DOORBELL(0), 0);
return (0);
}
rw_assert_wrlock(&sc->sc_cmd_lock);
s = splusb();
sc->sc_cmd_trb = trb;
XDWRITE4(sc, XHCI_DOORBELL(0), 0);
error = tsleep_nsec(&sc->sc_cmd_trb, PZERO, "xhcicmd", timeout);
if (error) {
#ifdef XHCI_DEBUG
printf("%s: tsleep() = %d\n", __func__, error);
printf("cmd = %d ", XHCI_TRB_TYPE(letoh32(trb->trb_flags)));
xhci_dump_trb(trb);
#endif
KASSERT(sc->sc_cmd_trb == trb);
sc->sc_cmd_trb = NULL;
splx(s);
return (error);
}
splx(s);
memcpy(trb0, &sc->sc_result_trb, sizeof(struct xhci_trb));
if (XHCI_TRB_GET_CODE(letoh32(trb0->trb_status)) == XHCI_CODE_SUCCESS)
return (0);
#ifdef XHCI_DEBUG
printf("%s: event error code=%d, result=%d \n", DEVNAME(sc),
XHCI_TRB_GET_CODE(letoh32(trb0->trb_status)),
XHCI_TRB_TYPE(letoh32(trb0->trb_flags)));
xhci_dump_trb(trb0);
#endif
return (EIO);
}
int
xhci_command_abort(struct xhci_softc *sc)
{
uint32_t reg;
int i;
reg = XOREAD4(sc, XHCI_CRCR_LO);
if ((reg & XHCI_CRCR_LO_CRR) == 0)
return (0);
XOWRITE4(sc, XHCI_CRCR_LO, reg | XHCI_CRCR_LO_CA);
XOWRITE4(sc, XHCI_CRCR_HI, 0);
for (i = 0; i < 250; i++) {
usb_delay_ms(&sc->sc_bus, 1);
reg = XOREAD4(sc, XHCI_CRCR_LO) & XHCI_CRCR_LO_CRR;
if (!reg)
break;
}
if (reg) {
printf("%s: command ring abort timeout\n", DEVNAME(sc));
return (1);
}
return (0);
}
int
xhci_cmd_configure_ep(struct xhci_softc *sc, uint8_t slot, uint64_t addr)
{
struct xhci_trb trb;
int error;
DPRINTF(("%s: %s dev %u\n", DEVNAME(sc), __func__, slot));
trb.trb_paddr = htole64(addr);
trb.trb_status = 0;
trb.trb_flags = htole32(
XHCI_TRB_SET_SLOT(slot) | XHCI_CMD_CONFIG_EP
);
rw_enter_write(&sc->sc_cmd_lock);
error = xhci_command_submit(sc, &trb, XHCI_CMD_TIMEOUT);
rw_exit_write(&sc->sc_cmd_lock);
return (error);
}
int
xhci_cmd_stop_ep(struct xhci_softc *sc, uint8_t slot, uint8_t dci)
{
struct xhci_trb trb;
int error;
DPRINTF(("%s: %s dev %u dci %u\n", DEVNAME(sc), __func__, slot, dci));
trb.trb_paddr = 0;
trb.trb_status = 0;
trb.trb_flags = htole32(
XHCI_TRB_SET_SLOT(slot) | XHCI_TRB_SET_EP(dci) | XHCI_CMD_STOP_EP
);
rw_enter_write(&sc->sc_cmd_lock);
error = xhci_command_submit(sc, &trb, XHCI_CMD_TIMEOUT);
rw_exit_write(&sc->sc_cmd_lock);
return (error);
}
void
xhci_cmd_reset_ep_async(struct xhci_softc *sc, uint8_t slot, uint8_t dci)
{
struct xhci_trb trb;
DPRINTF(("%s: %s dev %u dci %u\n", DEVNAME(sc), __func__, slot, dci));
trb.trb_paddr = 0;
trb.trb_status = 0;
trb.trb_flags = htole32(
XHCI_TRB_SET_SLOT(slot) | XHCI_TRB_SET_EP(dci) | XHCI_CMD_RESET_EP
);
xhci_command_submit(sc, &trb, 0);
}
void
xhci_cmd_set_tr_deq_async(struct xhci_softc *sc, uint8_t slot, uint8_t dci,
uint64_t addr)
{
struct xhci_trb trb;
DPRINTF(("%s: %s dev %u dci %u\n", DEVNAME(sc), __func__, slot, dci));
trb.trb_paddr = htole64(addr);
trb.trb_status = 0;
trb.trb_flags = htole32(
XHCI_TRB_SET_SLOT(slot) | XHCI_TRB_SET_EP(dci) | XHCI_CMD_SET_TR_DEQ
);
xhci_command_submit(sc, &trb, 0);
}
int
xhci_cmd_slot_control(struct xhci_softc *sc, uint8_t *slotp, int enable)
{
struct xhci_trb trb;
int error;
DPRINTF(("%s: %s\n", DEVNAME(sc), __func__));
trb.trb_paddr = 0;
trb.trb_status = 0;
if (enable)
trb.trb_flags = htole32(XHCI_CMD_ENABLE_SLOT);
else
trb.trb_flags = htole32(
XHCI_TRB_SET_SLOT(*slotp) | XHCI_CMD_DISABLE_SLOT
);
rw_enter_write(&sc->sc_cmd_lock);
error = xhci_command_submit(sc, &trb, XHCI_CMD_TIMEOUT);
rw_exit_write(&sc->sc_cmd_lock);
if (error != 0)
return (EIO);
if (enable)
*slotp = XHCI_TRB_GET_SLOT(letoh32(trb.trb_flags));
return (0);
}
int
xhci_cmd_set_address(struct xhci_softc *sc, uint8_t slot, uint64_t addr,
uint32_t bsr)
{
struct xhci_trb trb;
int error;
DPRINTF(("%s: %s BSR=%u\n", DEVNAME(sc), __func__, bsr ? 1 : 0));
trb.trb_paddr = htole64(addr);
trb.trb_status = 0;
trb.trb_flags = htole32(
XHCI_TRB_SET_SLOT(slot) | XHCI_CMD_ADDRESS_DEVICE | bsr
);
rw_enter_write(&sc->sc_cmd_lock);
error = xhci_command_submit(sc, &trb, XHCI_CMD_TIMEOUT);
rw_exit_write(&sc->sc_cmd_lock);
return (error);
}
int
xhci_cmd_evaluate_ctx(struct xhci_softc *sc, uint8_t slot, uint64_t addr)
{
struct xhci_trb trb;
int error;
DPRINTF(("%s: %s dev %u\n", DEVNAME(sc), __func__, slot));
trb.trb_paddr = htole64(addr);
trb.trb_status = 0;
trb.trb_flags = htole32(
XHCI_TRB_SET_SLOT(slot) | XHCI_CMD_EVAL_CTX
);
rw_enter_write(&sc->sc_cmd_lock);
error = xhci_command_submit(sc, &trb, XHCI_CMD_TIMEOUT);
rw_exit_write(&sc->sc_cmd_lock);
return (error);
}
#ifdef XHCI_DEBUG
int
xhci_cmd_noop(struct xhci_softc *sc)
{
struct xhci_trb trb;
int error;
DPRINTF(("%s: %s\n", DEVNAME(sc), __func__));
trb.trb_paddr = 0;
trb.trb_status = 0;
trb.trb_flags = htole32(XHCI_CMD_NOOP);
rw_enter_write(&sc->sc_cmd_lock);
error = xhci_command_submit(sc, &trb, XHCI_CMD_TIMEOUT);
rw_exit_write(&sc->sc_cmd_lock);
return (error);
}
#endif
int
xhci_softdev_alloc(struct xhci_softc *sc, uint8_t slot)
{
struct xhci_soft_dev *sdev = &sc->sc_sdevs[slot];
int i, error;
uint8_t *kva;
/*
* Setup input context. Even with 64 byte context size, it
* fits into the smallest supported page size, so use that.
*/
error = usbd_dma_contig_alloc(&sc->sc_bus, &sdev->ictx_dma,
(void **)&kva, sc->sc_pagesize, XHCI_ICTX_ALIGN, sc->sc_pagesize);
if (error)
return (ENOMEM);
sdev->input_ctx = (struct xhci_inctx *)kva;
sdev->slot_ctx = (struct xhci_sctx *)(kva + sc->sc_ctxsize);
for (i = 0; i < 31; i++)
sdev->ep_ctx[i] =
(struct xhci_epctx *)(kva + (i + 2) * sc->sc_ctxsize);
DPRINTF(("%s: dev %d, input=%p slot=%p ep0=%p\n", DEVNAME(sc),
slot, sdev->input_ctx, sdev->slot_ctx, sdev->ep_ctx[0]));
/* Setup output context */
error = usbd_dma_contig_alloc(&sc->sc_bus, &sdev->octx_dma, NULL,
sc->sc_pagesize, XHCI_OCTX_ALIGN, sc->sc_pagesize);
if (error) {
usbd_dma_contig_free(&sc->sc_bus, &sdev->ictx_dma);
return (ENOMEM);
}
memset(&sdev->pipes, 0, sizeof(sdev->pipes));
DPRINTF(("%s: dev %d, setting DCBAA to 0x%016llx\n", DEVNAME(sc),
slot, (long long)sdev->octx_dma.paddr));
sc->sc_dcbaa.segs[slot] = htole64(sdev->octx_dma.paddr);
bus_dmamap_sync(sc->sc_dcbaa.dma.tag, sc->sc_dcbaa.dma.map,
slot * sizeof(uint64_t), sizeof(uint64_t), BUS_DMASYNC_PREREAD |
BUS_DMASYNC_PREWRITE);
return (0);
}
void
xhci_softdev_free(struct xhci_softc *sc, uint8_t slot)
{
struct xhci_soft_dev *sdev = &sc->sc_sdevs[slot];
sc->sc_dcbaa.segs[slot] = 0;
bus_dmamap_sync(sc->sc_dcbaa.dma.tag, sc->sc_dcbaa.dma.map,
slot * sizeof(uint64_t), sizeof(uint64_t), BUS_DMASYNC_PREREAD |
BUS_DMASYNC_PREWRITE);
usbd_dma_contig_free(&sc->sc_bus, &sdev->octx_dma);
usbd_dma_contig_free(&sc->sc_bus, &sdev->ictx_dma);
memset(sdev, 0, sizeof(struct xhci_soft_dev));
}
/* Root hub descriptors. */
usb_device_descriptor_t xhci_devd = {
USB_DEVICE_DESCRIPTOR_SIZE,
UDESC_DEVICE, /* type */
{0x00, 0x03}, /* USB version */
UDCLASS_HUB, /* class */
UDSUBCLASS_HUB, /* subclass */
UDPROTO_HSHUBSTT, /* protocol */
9, /* max packet */
{0},{0},{0x00,0x01}, /* device id */
1,2,0, /* string indexes */
1 /* # of configurations */
};
const usb_config_descriptor_t xhci_confd = {
USB_CONFIG_DESCRIPTOR_SIZE,
UDESC_CONFIG,
{USB_CONFIG_DESCRIPTOR_SIZE +
USB_INTERFACE_DESCRIPTOR_SIZE +
USB_ENDPOINT_DESCRIPTOR_SIZE},
1,
1,
0,
UC_BUS_POWERED | UC_SELF_POWERED,
0 /* max power */
};
const usb_interface_descriptor_t xhci_ifcd = {
USB_INTERFACE_DESCRIPTOR_SIZE,
UDESC_INTERFACE,
0,
0,
1,
UICLASS_HUB,
UISUBCLASS_HUB,
UIPROTO_HSHUBSTT,
0
};
const usb_endpoint_descriptor_t xhci_endpd = {
USB_ENDPOINT_DESCRIPTOR_SIZE,
UDESC_ENDPOINT,
UE_DIR_IN | XHCI_INTR_ENDPT,
UE_INTERRUPT,
{2, 0}, /* max 15 ports */
255
};
const usb_endpoint_ss_comp_descriptor_t xhci_endpcd = {
USB_ENDPOINT_SS_COMP_DESCRIPTOR_SIZE,
UDESC_ENDPOINT_SS_COMP,
0,
0,
{0, 0}
};
const usb_hub_descriptor_t xhci_hubd = {
USB_HUB_DESCRIPTOR_SIZE,
UDESC_SS_HUB,
0,
{0,0},
0,
0,
{0},
};
void
xhci_abort_xfer(struct usbd_xfer *xfer, usbd_status status)
{
struct xhci_softc *sc = (struct xhci_softc *)xfer->device->bus;
struct xhci_pipe *xp = (struct xhci_pipe *)xfer->pipe;
int error;
splsoftassert(IPL_SOFTUSB);
DPRINTF(("%s: xfer=%p status=%s err=%s actlen=%d len=%d idx=%d\n",
__func__, xfer, usbd_errstr(xfer->status), usbd_errstr(status),
xfer->actlen, xfer->length, ((struct xhci_xfer *)xfer)->index));
/* XXX The stack should not call abort() in this case. */
if (sc->sc_bus.dying || xfer->status == USBD_NOT_STARTED) {
xfer->status = status;
timeout_del(&xfer->timeout_handle);
usb_rem_task(xfer->device, &xfer->abort_task);
usb_transfer_complete(xfer);
return;
}
/* Transfer is already done. */
if (xfer->status != USBD_IN_PROGRESS) {
DPRINTF(("%s: already done \n", __func__));
return;
}
/* Prevent any timeout to kick in. */
timeout_del(&xfer->timeout_handle);
usb_rem_task(xfer->device, &xfer->abort_task);
/* Indicate that we are aborting this transfer. */
xp->halted = status;
xp->aborted_xfer = xfer;
/* Stop the endpoint and wait until the hardware says so. */
if (xhci_cmd_stop_ep(sc, xp->slot, xp->dci)) {
DPRINTF(("%s: error stopping endpoint\n", DEVNAME(sc)));
/* Assume the device is gone. */
xp->halted = 0;
xp->aborted_xfer = NULL;
xfer->status = status;
usb_transfer_complete(xfer);
return;
}
/*
* The transfer was already completed when we stopped the
* endpoint, no need to move the dequeue pointer past its
* TRBs.
*/
if (xp->aborted_xfer == NULL) {
DPRINTF(("%s: done before stopping the endpoint\n", __func__));
xp->halted = 0;
return;
}
/*
* At this stage the endpoint has been stopped, so update its
* dequeue pointer past the last TRB of the transfer.
*
* Note: This assumes that only one transfer per endpoint has
* pending TRBs on the ring.
*/
xhci_cmd_set_tr_deq_async(sc, xp->slot, xp->dci,
DEQPTR(xp->ring) | xp->ring.toggle);
error = tsleep_nsec(xp, PZERO, "xhciab", XHCI_CMD_TIMEOUT);
if (error)
printf("%s: timeout aborting transfer\n", DEVNAME(sc));
}
void
xhci_timeout(void *addr)
{
struct usbd_xfer *xfer = addr;
struct xhci_softc *sc = (struct xhci_softc *)xfer->device->bus;
if (sc->sc_bus.dying) {
xhci_timeout_task(addr);
return;
}
usb_init_task(&xfer->abort_task, xhci_timeout_task, addr,
USB_TASK_TYPE_ABORT);
usb_add_task(xfer->device, &xfer->abort_task);
}
void
xhci_timeout_task(void *addr)
{
struct usbd_xfer *xfer = addr;
int s;
s = splusb();
xhci_abort_xfer(xfer, USBD_TIMEOUT);
splx(s);
}
usbd_status
xhci_root_ctrl_transfer(struct usbd_xfer *xfer)
{
usbd_status err;
err = usb_insert_transfer(xfer);
if (err)
return (err);
return (xhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
}
usbd_status
xhci_root_ctrl_start(struct usbd_xfer *xfer)
{
struct xhci_softc *sc = (struct xhci_softc *)xfer->device->bus;
usb_port_status_t ps;
usb_device_request_t *req;
void *buf = NULL;
usb_hub_descriptor_t hubd;
usbd_status err;
int s, len, value, index;
int l, totlen = 0;
int port, i;
uint32_t v;
KASSERT(xfer->rqflags & URQ_REQUEST);
if (sc->sc_bus.dying)
return (USBD_IOERROR);
req = &xfer->request;
DPRINTFN(4,("%s: type=0x%02x request=%02x\n", __func__,
req->bmRequestType, req->bRequest));
len = UGETW(req->wLength);
value = UGETW(req->wValue);
index = UGETW(req->wIndex);
if (len != 0)
buf = KERNADDR(&xfer->dmabuf, 0);
#define C(x,y) ((x) | ((y) << 8))
switch(C(req->bRequest, req->bmRequestType)) {
case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
/*
* DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
* for the integrated root hub.
*/
break;
case C(UR_GET_CONFIG, UT_READ_DEVICE):
if (len > 0) {
*(uint8_t *)buf = sc->sc_conf;
totlen = 1;
}
break;
case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
DPRINTFN(8,("xhci_root_ctrl_start: wValue=0x%04x\n", value));
switch(value >> 8) {
case UDESC_DEVICE:
if ((value & 0xff) != 0) {
err = USBD_IOERROR;
goto ret;
}
totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
USETW(xhci_devd.idVendor, sc->sc_id_vendor);
memcpy(buf, &xhci_devd, l);
break;
/*
* We can't really operate at another speed, but the spec says
* we need this descriptor.
*/
case UDESC_OTHER_SPEED_CONFIGURATION:
case UDESC_CONFIG:
if ((value & 0xff) != 0) {
err = USBD_IOERROR;
goto ret;
}
totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
memcpy(buf, &xhci_confd, l);
((usb_config_descriptor_t *)buf)->bDescriptorType =
value >> 8;
buf = (char *)buf + l;
len -= l;
l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
totlen += l;
memcpy(buf, &xhci_ifcd, l);
buf = (char *)buf + l;
len -= l;
l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
totlen += l;
memcpy(buf, &xhci_endpd, l);
break;
case UDESC_STRING:
if (len == 0)
break;
*(u_int8_t *)buf = 0;
totlen = 1;
switch (value & 0xff) {
case 0: /* Language table */
totlen = usbd_str(buf, len, "\001");
break;
case 1: /* Vendor */
totlen = usbd_str(buf, len, sc->sc_vendor);
break;
case 2: /* Product */
totlen = usbd_str(buf, len, "xHCI root hub");
break;
}
break;
default:
err = USBD_IOERROR;
goto ret;
}
break;
case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
if (len > 0) {
*(uint8_t *)buf = 0;
totlen = 1;
}
break;
case C(UR_GET_STATUS, UT_READ_DEVICE):
if (len > 1) {
USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
totlen = 2;
}
break;
case C(UR_GET_STATUS, UT_READ_INTERFACE):
case C(UR_GET_STATUS, UT_READ_ENDPOINT):
if (len > 1) {
USETW(((usb_status_t *)buf)->wStatus, 0);
totlen = 2;
}
break;
case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
if (value >= USB_MAX_DEVICES) {
err = USBD_IOERROR;
goto ret;
}
break;
case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
if (value != 0 && value != 1) {
err = USBD_IOERROR;
goto ret;
}
sc->sc_conf = value;
break;
case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
break;
case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
err = USBD_IOERROR;
goto ret;
case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
break;
case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
break;
/* Hub requests */
case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
break;
case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
DPRINTFN(8, ("xhci_root_ctrl_start: UR_CLEAR_PORT_FEATURE "
"port=%d feature=%d\n", index, value));
if (index < 1 || index > sc->sc_noport) {
err = USBD_IOERROR;
goto ret;
}
port = XHCI_PORTSC(index);
v = XOREAD4(sc, port) & ~XHCI_PS_CLEAR;
switch (value) {
case UHF_PORT_ENABLE:
XOWRITE4(sc, port, v | XHCI_PS_PED);
break;
case UHF_PORT_SUSPEND:
/* TODO */
break;
case UHF_PORT_POWER:
XOWRITE4(sc, port, v & ~XHCI_PS_PP);
break;
case UHF_PORT_INDICATOR:
XOWRITE4(sc, port, v & ~XHCI_PS_SET_PIC(3));
break;
case UHF_C_PORT_CONNECTION:
XOWRITE4(sc, port, v | XHCI_PS_CSC);
break;
case UHF_C_PORT_ENABLE:
XOWRITE4(sc, port, v | XHCI_PS_PEC);
break;
case UHF_C_PORT_SUSPEND:
case UHF_C_PORT_LINK_STATE:
XOWRITE4(sc, port, v | XHCI_PS_PLC);
break;
case UHF_C_PORT_OVER_CURRENT:
XOWRITE4(sc, port, v | XHCI_PS_OCC);
break;
case UHF_C_PORT_RESET:
XOWRITE4(sc, port, v | XHCI_PS_PRC);
break;
case UHF_C_BH_PORT_RESET:
XOWRITE4(sc, port, v | XHCI_PS_WRC);
break;
default:
err = USBD_IOERROR;
goto ret;
}
break;
case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
if (len == 0)
break;
if ((value & 0xff) != 0) {
err = USBD_IOERROR;
goto ret;
}
v = XREAD4(sc, XHCI_HCCPARAMS);
hubd = xhci_hubd;
hubd.bNbrPorts = sc->sc_noport;
USETW(hubd.wHubCharacteristics,
(XHCI_HCC_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_GANGED) |
(XHCI_HCC_PIND(v) ? UHD_PORT_IND : 0));
hubd.bPwrOn2PwrGood = 10; /* xHCI section 5.4.9 */
for (i = 1; i <= sc->sc_noport; i++) {
v = XOREAD4(sc, XHCI_PORTSC(i));
if (v & XHCI_PS_DR)
hubd.DeviceRemovable[i / 8] |= 1U << (i % 8);
}
hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
l = min(len, hubd.bDescLength);
totlen = l;
memcpy(buf, &hubd, l);
break;
case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
if (len != 16) {
err = USBD_IOERROR;
goto ret;
}
memset(buf, 0, len);
totlen = len;
break;
case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
DPRINTFN(8,("xhci_root_ctrl_start: get port status i=%d\n",
index));
if (index < 1 || index > sc->sc_noport) {
err = USBD_IOERROR;
goto ret;
}
if (len != 4) {
err = USBD_IOERROR;
goto ret;
}
v = XOREAD4(sc, XHCI_PORTSC(index));
DPRINTFN(8,("xhci_root_ctrl_start: port status=0x%04x\n", v));
i = UPS_PORT_LS_SET(XHCI_PS_GET_PLS(v));
switch (XHCI_PS_SPEED(v)) {
case XHCI_SPEED_FULL:
i |= UPS_FULL_SPEED;
break;
case XHCI_SPEED_LOW:
i |= UPS_LOW_SPEED;
break;
case XHCI_SPEED_HIGH:
i |= UPS_HIGH_SPEED;
break;
case XHCI_SPEED_SUPER:
default:
break;
}
if (v & XHCI_PS_CCS) i |= UPS_CURRENT_CONNECT_STATUS;
if (v & XHCI_PS_PED) i |= UPS_PORT_ENABLED;
if (v & XHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
if (v & XHCI_PS_PR) i |= UPS_RESET;
if (v & XHCI_PS_PP) {
if (XHCI_PS_SPEED(v) >= XHCI_SPEED_FULL &&
XHCI_PS_SPEED(v) <= XHCI_SPEED_HIGH)
i |= UPS_PORT_POWER;
else
i |= UPS_PORT_POWER_SS;
}
USETW(ps.wPortStatus, i);
i = 0;
if (v & XHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
if (v & XHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
if (v & XHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
if (v & XHCI_PS_PRC) i |= UPS_C_PORT_RESET;
if (v & XHCI_PS_WRC) i |= UPS_C_BH_PORT_RESET;
if (v & XHCI_PS_PLC) i |= UPS_C_PORT_LINK_STATE;
if (v & XHCI_PS_CEC) i |= UPS_C_PORT_CONFIG_ERROR;
USETW(ps.wPortChange, i);
l = min(len, sizeof ps);
memcpy(buf, &ps, l);
totlen = l;
break;
case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
err = USBD_IOERROR;
goto ret;
case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
break;
case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
i = index >> 8;
index &= 0x00ff;
if (index < 1 || index > sc->sc_noport) {
err = USBD_IOERROR;
goto ret;
}
port = XHCI_PORTSC(index);
v = XOREAD4(sc, port) & ~XHCI_PS_CLEAR;
switch (value) {
case UHF_PORT_ENABLE:
XOWRITE4(sc, port, v | XHCI_PS_PED);
break;
case UHF_PORT_SUSPEND:
DPRINTFN(6, ("suspend port %u (LPM=%u)\n", index, i));
if (XHCI_PS_SPEED(v) == XHCI_SPEED_SUPER) {
err = USBD_IOERROR;
goto ret;
}
XOWRITE4(sc, port, v |
XHCI_PS_SET_PLS(i ? 2 /* LPM */ : 3) | XHCI_PS_LWS);
break;
case UHF_PORT_RESET:
DPRINTFN(6, ("reset port %d\n", index));
XOWRITE4(sc, port, v | XHCI_PS_PR);
break;
case UHF_PORT_POWER:
DPRINTFN(3, ("set port power %d\n", index));
XOWRITE4(sc, port, v | XHCI_PS_PP);
break;
case UHF_PORT_INDICATOR:
DPRINTFN(3, ("set port indicator %d\n", index));
v &= ~XHCI_PS_SET_PIC(3);
v |= XHCI_PS_SET_PIC(1);
XOWRITE4(sc, port, v);
break;
case UHF_C_PORT_RESET:
XOWRITE4(sc, port, v | XHCI_PS_PRC);
break;
case UHF_C_BH_PORT_RESET:
XOWRITE4(sc, port, v | XHCI_PS_WRC);
break;
default:
err = USBD_IOERROR;
goto ret;
}
break;
case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
break;
default:
err = USBD_IOERROR;
goto ret;
}
xfer->actlen = totlen;
err = USBD_NORMAL_COMPLETION;
ret:
xfer->status = err;
s = splusb();
usb_transfer_complete(xfer);
splx(s);
return (err);
}
void
xhci_noop(struct usbd_xfer *xfer)
{
}
usbd_status
xhci_root_intr_transfer(struct usbd_xfer *xfer)
{
usbd_status err;
err = usb_insert_transfer(xfer);
if (err)
return (err);
return (xhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
}
usbd_status
xhci_root_intr_start(struct usbd_xfer *xfer)
{
struct xhci_softc *sc = (struct xhci_softc *)xfer->device->bus;
if (sc->sc_bus.dying)
return (USBD_IOERROR);
sc->sc_intrxfer = xfer;
return (USBD_IN_PROGRESS);
}
void
xhci_root_intr_abort(struct usbd_xfer *xfer)
{
struct xhci_softc *sc = (struct xhci_softc *)xfer->device->bus;
int s;
sc->sc_intrxfer = NULL;
xfer->status = USBD_CANCELLED;
s = splusb();
usb_transfer_complete(xfer);
splx(s);
}
void
xhci_root_intr_done(struct usbd_xfer *xfer)
{
}
/*
* Number of packets remaining in the TD after the corresponding TRB.
*
* Section 4.11.2.4 of xHCI specification r1.1.
*/
static inline uint32_t
xhci_xfer_tdsize(struct usbd_xfer *xfer, uint32_t remain, uint32_t len)
{
uint32_t npkt, mps = UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize);
if (len == 0)
return XHCI_TRB_TDREM(0);
npkt = howmany(remain - len, UE_GET_SIZE(mps));
if (npkt > 31)
npkt = 31;
return XHCI_TRB_TDREM(npkt);
}
/*
* Transfer Burst Count (TBC) and Transfer Last Burst Packet Count (TLBPC).
*
* Section 4.11.2.3 of xHCI specification r1.1.
*/
static inline uint32_t
xhci_xfer_tbc(struct usbd_xfer *xfer, uint32_t len, uint32_t *tlbpc)
{
uint32_t mps = UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize);
uint32_t maxb, tdpc, residue, tbc;
/* Transfer Descriptor Packet Count, section 4.14.1. */
tdpc = howmany(len, UE_GET_SIZE(mps));
if (tdpc == 0)
tdpc = 1;
/* Transfer Burst Count */
maxb = xhci_pipe_maxburst(xfer->pipe);
tbc = howmany(tdpc, maxb + 1) - 1;
/* Transfer Last Burst Packet Count */
if (xfer->device->speed == USB_SPEED_SUPER) {
residue = tdpc % (maxb + 1);
if (residue == 0)
*tlbpc = maxb;
else
*tlbpc = residue - 1;
} else {
*tlbpc = tdpc - 1;
}
return (tbc);
}
usbd_status
xhci_device_ctrl_transfer(struct usbd_xfer *xfer)
{
usbd_status err;
err = usb_insert_transfer(xfer);
if (err)
return (err);
return (xhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
}
usbd_status
xhci_device_ctrl_start(struct usbd_xfer *xfer)
{
struct xhci_softc *sc = (struct xhci_softc *)xfer->device->bus;
struct xhci_pipe *xp = (struct xhci_pipe *)xfer->pipe;
struct xhci_trb *trb0, *trb;
uint32_t flags, len = UGETW(xfer->request.wLength);
uint8_t toggle;
int s;
KASSERT(xfer->rqflags & URQ_REQUEST);
if (sc->sc_bus.dying || xp->halted)
return (USBD_IOERROR);
if (xp->free_trbs < 3)
return (USBD_NOMEM);
/* We'll toggle the setup TRB once we're finished with the stages. */
trb0 = xhci_xfer_get_trb(sc, xfer, &toggle, 0);
flags = XHCI_TRB_TYPE_SETUP | XHCI_TRB_IDT | (toggle ^ 1);
if (len != 0) {
if (usbd_xfer_isread(xfer))
flags |= XHCI_TRB_TRT_IN;
else
flags |= XHCI_TRB_TRT_OUT;
}
memcpy(&trb0->trb_paddr, &xfer->request, sizeof(trb0->trb_paddr));
trb0->trb_status = htole32(XHCI_TRB_INTR(0) | XHCI_TRB_LEN(8));
trb0->trb_flags = htole32(flags);
bus_dmamap_sync(xp->ring.dma.tag, xp->ring.dma.map,
TRBOFF(&xp->ring, trb0), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
/* Data TRB */
if (len != 0) {
trb = xhci_xfer_get_trb(sc, xfer, &toggle, 0);
flags = XHCI_TRB_TYPE_DATA | toggle;
if (usbd_xfer_isread(xfer))
flags |= XHCI_TRB_DIR_IN | XHCI_TRB_ISP;
trb->trb_paddr = htole64(DMAADDR(&xfer->dmabuf, 0));
trb->trb_status = htole32(
XHCI_TRB_INTR(0) | XHCI_TRB_LEN(len) |
xhci_xfer_tdsize(xfer, len, len)
);
trb->trb_flags = htole32(flags);
bus_dmamap_sync(xp->ring.dma.tag, xp->ring.dma.map,
TRBOFF(&xp->ring, trb), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
}
/* Status TRB */
trb = xhci_xfer_get_trb(sc, xfer, &toggle, 1);
flags = XHCI_TRB_TYPE_STATUS | XHCI_TRB_IOC | toggle;
if (len == 0 || !usbd_xfer_isread(xfer))
flags |= XHCI_TRB_DIR_IN;
trb->trb_paddr = 0;
trb->trb_status = htole32(XHCI_TRB_INTR(0));
trb->trb_flags = htole32(flags);
bus_dmamap_sync(xp->ring.dma.tag, xp->ring.dma.map,
TRBOFF(&xp->ring, trb), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
/* Setup TRB */
trb0->trb_flags ^= htole32(XHCI_TRB_CYCLE);
bus_dmamap_sync(xp->ring.dma.tag, xp->ring.dma.map,
TRBOFF(&xp->ring, trb0), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
s = splusb();
XDWRITE4(sc, XHCI_DOORBELL(xp->slot), xp->dci);
xfer->status = USBD_IN_PROGRESS;
if (xfer->timeout && !sc->sc_bus.use_polling) {
timeout_del(&xfer->timeout_handle);
timeout_set(&xfer->timeout_handle, xhci_timeout, xfer);
timeout_add_msec(&xfer->timeout_handle, xfer->timeout);
}
splx(s);
return (USBD_IN_PROGRESS);
}
void
xhci_device_ctrl_abort(struct usbd_xfer *xfer)
{
xhci_abort_xfer(xfer, USBD_CANCELLED);
}
usbd_status
xhci_device_generic_transfer(struct usbd_xfer *xfer)
{
usbd_status err;
err = usb_insert_transfer(xfer);
if (err)
return (err);
return (xhci_device_generic_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
}
usbd_status
xhci_device_generic_start(struct usbd_xfer *xfer)
{
struct xhci_softc *sc = (struct xhci_softc *)xfer->device->bus;
struct xhci_pipe *xp = (struct xhci_pipe *)xfer->pipe;
struct xhci_trb *trb0, *trb;
uint32_t len, remain, flags;
uint32_t mps = UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize);
uint64_t paddr = DMAADDR(&xfer->dmabuf, 0);
uint8_t toggle;
int s, i, ntrb, zerotd = 0;
KASSERT(!(xfer->rqflags & URQ_REQUEST));
if (sc->sc_bus.dying || xp->halted)
return (USBD_IOERROR);
/* How many TRBs do we need for this transfer? */
ntrb = howmany(xfer->length, XHCI_TRB_MAXSIZE);
/* If the buffer crosses a 64k boundary, we need one more. */
len = XHCI_TRB_MAXSIZE - (paddr & (XHCI_TRB_MAXSIZE - 1));
if (len < xfer->length)
ntrb = howmany(xfer->length - len, XHCI_TRB_MAXSIZE) + 1;
else
len = xfer->length;
/* If we need to append a zero length packet, we need one more. */
if ((xfer->flags & USBD_FORCE_SHORT_XFER || xfer->length == 0) &&
(xfer->length % UE_GET_SIZE(mps) == 0))
zerotd = 1;
if (xp->free_trbs < (ntrb + zerotd))
return (USBD_NOMEM);
/* We'll toggle the first TRB once we're finished with the chain. */
trb0 = xhci_xfer_get_trb(sc, xfer, &toggle, (ntrb == 1));
flags = XHCI_TRB_TYPE_NORMAL | (toggle ^ 1);
if (usbd_xfer_isread(xfer))
flags |= XHCI_TRB_ISP;
flags |= (ntrb == 1) ? XHCI_TRB_IOC : XHCI_TRB_CHAIN;
trb0->trb_paddr = htole64(DMAADDR(&xfer->dmabuf, 0));
trb0->trb_status = htole32(
XHCI_TRB_INTR(0) | XHCI_TRB_LEN(len) |
xhci_xfer_tdsize(xfer, xfer->length, len)
);
trb0->trb_flags = htole32(flags);
bus_dmamap_sync(xp->ring.dma.tag, xp->ring.dma.map,
TRBOFF(&xp->ring, trb0), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
remain = xfer->length - len;
paddr += len;
/* Chain more TRBs if needed. */
for (i = ntrb - 1; i > 0; i--) {
len = min(remain, XHCI_TRB_MAXSIZE);
/* Next (or Last) TRB. */
trb = xhci_xfer_get_trb(sc, xfer, &toggle, (i == 1));
flags = XHCI_TRB_TYPE_NORMAL | toggle;
if (usbd_xfer_isread(xfer))
flags |= XHCI_TRB_ISP;
flags |= (i == 1) ? XHCI_TRB_IOC : XHCI_TRB_CHAIN;
trb->trb_paddr = htole64(paddr);
trb->trb_status = htole32(
XHCI_TRB_INTR(0) | XHCI_TRB_LEN(len) |
xhci_xfer_tdsize(xfer, remain, len)
);
trb->trb_flags = htole32(flags);
bus_dmamap_sync(xp->ring.dma.tag, xp->ring.dma.map,
TRBOFF(&xp->ring, trb), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
remain -= len;
paddr += len;
}
/* Do we need to issue a zero length transfer? */
if (zerotd == 1) {
trb = xhci_xfer_get_trb(sc, xfer, &toggle, -1);
trb->trb_paddr = 0;
trb->trb_status = 0;
trb->trb_flags = htole32(XHCI_TRB_TYPE_NORMAL | XHCI_TRB_IOC | toggle);
bus_dmamap_sync(xp->ring.dma.tag, xp->ring.dma.map,
TRBOFF(&xp->ring, trb), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
}
/* First TRB. */
trb0->trb_flags ^= htole32(XHCI_TRB_CYCLE);
bus_dmamap_sync(xp->ring.dma.tag, xp->ring.dma.map,
TRBOFF(&xp->ring, trb0), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
s = splusb();
XDWRITE4(sc, XHCI_DOORBELL(xp->slot), xp->dci);
xfer->status = USBD_IN_PROGRESS;
if (xfer->timeout && !sc->sc_bus.use_polling) {
timeout_del(&xfer->timeout_handle);
timeout_set(&xfer->timeout_handle, xhci_timeout, xfer);
timeout_add_msec(&xfer->timeout_handle, xfer->timeout);
}
splx(s);
return (USBD_IN_PROGRESS);
}
void
xhci_device_generic_done(struct usbd_xfer *xfer)
{
/* Only happens with interrupt transfers. */
if (xfer->pipe->repeat) {
xfer->actlen = 0;
xhci_device_generic_start(xfer);
}
}
void
xhci_device_generic_abort(struct usbd_xfer *xfer)
{
KASSERT(!xfer->pipe->repeat || xfer->pipe->intrxfer == xfer);
xhci_abort_xfer(xfer, USBD_CANCELLED);
}
usbd_status
xhci_device_isoc_transfer(struct usbd_xfer *xfer)
{
usbd_status err;
err = usb_insert_transfer(xfer);
if (err && err != USBD_IN_PROGRESS)
return (err);
return (xhci_device_isoc_start(xfer));
}
usbd_status
xhci_device_isoc_start(struct usbd_xfer *xfer)
{
struct xhci_softc *sc = (struct xhci_softc *)xfer->device->bus;
struct xhci_pipe *xp = (struct xhci_pipe *)xfer->pipe;
struct xhci_xfer *xx = (struct xhci_xfer *)xfer;
struct xhci_trb *trb0, *trb;
uint32_t len, remain, flags;
uint64_t paddr;
uint32_t tbc, tlbpc;
int s, i, j, ntrb = xfer->nframes;
uint8_t toggle;
KASSERT(!(xfer->rqflags & URQ_REQUEST));
if (sc->sc_bus.dying || xp->halted)
return (USBD_IOERROR);
/* Why would you do that anyway? */
if (sc->sc_bus.use_polling)
return (USBD_INVAL);
/*
* To allow continuous transfers, above we start all transfers
* immediately. However, we're still going to get usbd_start_next call
* this when another xfer completes. So, check if this is already
* in progress or not
*/
if (xx->ntrb > 0)
return (USBD_IN_PROGRESS);
paddr = DMAADDR(&xfer->dmabuf, 0);
/* How many TRBs do for all Transfers? */
for (i = 0, ntrb = 0; i < xfer->nframes; i++) {
/* How many TRBs do we need for this transfer? */
ntrb += howmany(xfer->frlengths[i], XHCI_TRB_MAXSIZE);
/* If the buffer crosses a 64k boundary, we need one more. */
len = XHCI_TRB_MAXSIZE - (paddr & (XHCI_TRB_MAXSIZE - 1));
if (len < xfer->frlengths[i])
ntrb++;
paddr += xfer->frlengths[i];
}
if (xp->free_trbs < ntrb)
return (USBD_NOMEM);
paddr = DMAADDR(&xfer->dmabuf, 0);
for (i = 0, trb0 = NULL; i < xfer->nframes; i++) {
/* How many TRBs do we need for this transfer? */
ntrb = howmany(xfer->frlengths[i], XHCI_TRB_MAXSIZE);
/* If the buffer crosses a 64k boundary, we need one more. */
len = XHCI_TRB_MAXSIZE - (paddr & (XHCI_TRB_MAXSIZE - 1));
if (len < xfer->frlengths[i])
ntrb++;
else
len = xfer->frlengths[i];
KASSERT(ntrb < 3);
/*
* We'll commit the first TRB once we're finished with the
* chain.
*/
trb = xhci_xfer_get_trb(sc, xfer, &toggle, (ntrb == 1));
DPRINTFN(4, ("%s:%d: ring %p trb0_idx %lu ntrb %d paddr %llx "
"len %u\n", __func__, __LINE__,
&xp->ring.trbs[0], (trb - &xp->ring.trbs[0]), ntrb, paddr,
len));
/* Record the first TRB so we can toggle later. */
if (trb0 == NULL) {
trb0 = trb;
toggle ^= 1;
}
flags = XHCI_TRB_TYPE_ISOCH | XHCI_TRB_SIA | toggle;
if (usbd_xfer_isread(xfer))
flags |= XHCI_TRB_ISP;
flags |= (ntrb == 1) ? XHCI_TRB_IOC : XHCI_TRB_CHAIN;
tbc = xhci_xfer_tbc(xfer, xfer->frlengths[i], &tlbpc);
flags |= XHCI_TRB_ISOC_TBC(tbc) | XHCI_TRB_ISOC_TLBPC(tlbpc);
trb->trb_paddr = htole64(paddr);
trb->trb_status = htole32(
XHCI_TRB_INTR(0) | XHCI_TRB_LEN(len) |
xhci_xfer_tdsize(xfer, xfer->frlengths[i], len)
);
trb->trb_flags = htole32(flags);
bus_dmamap_sync(xp->ring.dma.tag, xp->ring.dma.map,
TRBOFF(&xp->ring, trb), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
remain = xfer->frlengths[i] - len;
paddr += len;
/* Chain more TRBs if needed. */
for (j = ntrb - 1; j > 0; j--) {
len = min(remain, XHCI_TRB_MAXSIZE);
/* Next (or Last) TRB. */
trb = xhci_xfer_get_trb(sc, xfer, &toggle, (j == 1));
flags = XHCI_TRB_TYPE_NORMAL | toggle;
if (usbd_xfer_isread(xfer))
flags |= XHCI_TRB_ISP;
flags |= (j == 1) ? XHCI_TRB_IOC : XHCI_TRB_CHAIN;
DPRINTFN(3, ("%s:%d: ring %p trb0_idx %lu ntrb %d "
"paddr %llx len %u\n", __func__, __LINE__,
&xp->ring.trbs[0], (trb - &xp->ring.trbs[0]), ntrb,
paddr, len));
trb->trb_paddr = htole64(paddr);
trb->trb_status = htole32(
XHCI_TRB_INTR(0) | XHCI_TRB_LEN(len) |
xhci_xfer_tdsize(xfer, remain, len)
);
trb->trb_flags = htole32(flags);
bus_dmamap_sync(xp->ring.dma.tag, xp->ring.dma.map,
TRBOFF(&xp->ring, trb), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
remain -= len;
paddr += len;
}
xfer->frlengths[i] = 0;
}
/* First TRB. */
trb0->trb_flags ^= htole32(XHCI_TRB_CYCLE);
bus_dmamap_sync(xp->ring.dma.tag, xp->ring.dma.map,
TRBOFF(&xp->ring, trb0), sizeof(struct xhci_trb),
BUS_DMASYNC_PREWRITE);
s = splusb();
XDWRITE4(sc, XHCI_DOORBELL(xp->slot), xp->dci);
xfer->status = USBD_IN_PROGRESS;
if (xfer->timeout) {
timeout_del(&xfer->timeout_handle);
timeout_set(&xfer->timeout_handle, xhci_timeout, xfer);
timeout_add_msec(&xfer->timeout_handle, xfer->timeout);
}
splx(s);
return (USBD_IN_PROGRESS);
}
|