summaryrefslogtreecommitdiff
path: root/sys/dev/pci/pci_subr.c
blob: f3b6a4df3247d7f98e70bdb0a362ed78d4e5eb80 (plain)
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
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
/*	$NetBSD: pci_subr.c,v 1.242 2022/02/01 01:28:26 msaitoh Exp $	*/

/*
 * Copyright (c) 1997 Zubin D. Dittia.  All rights reserved.
 * Copyright (c) 1995, 1996, 1998, 2000
 *	Christopher G. Demetriou.  All rights reserved.
 * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Charles M. Hannum.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * PCI autoconfiguration support functions.
 *
 * Note: This file is also built into a userland library (libpci).
 * Pay attention to this when you make modifications.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pci_subr.c,v 1.242 2022/02/01 01:28:26 msaitoh Exp $");

#ifdef _KERNEL_OPT
#include "opt_pci.h"
#endif

#include <sys/param.h>

#ifdef _KERNEL
#include <sys/systm.h>
#include <sys/intr.h>
#include <sys/module.h>
#include <sys/kmem.h>

#define MALLOC(sz)	kmem_alloc(sz, KM_SLEEP)
#define FREE(p, sz)	kmem_free(p, sz)

#else
#include <pci.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MALLOC(sz)	malloc(sz)
#define FREE(p, sz)	free(p)

#endif

#include <dev/pci/pcireg.h>
#include <dev/pci/pcidevs.h>
#ifdef _KERNEL
#include <dev/pci/pcivar.h>
#else
#include <dev/pci/pci_verbose.h>
#include <dev/pci/pcidevs_data.h>
#endif

static int pci_conf_find_cap(const pcireg_t *, unsigned int, int *);
static int pci_conf_find_extcap(const pcireg_t *, unsigned int, int *);
static void pci_conf_print_pcie_power(uint8_t, unsigned int);
#define PCIREG_SHIFTOUT(a, b) ((pcireg_t)__SHIFTOUT((a), (b)))

#ifdef _KERNEL
/*
 * Common routines used to match a compatible device by its PCI ID code.
 */

const struct device_compatible_entry *
pci_compatible_lookup_id(pcireg_t const id,
    const struct device_compatible_entry *dce)
{
	return device_compatible_lookup_id(id, PCI_COMPAT_EOL_VALUE, dce);
}

const struct device_compatible_entry *
pci_compatible_lookup(const struct pci_attach_args * const pa,
    const struct device_compatible_entry * const dce)
{
	return pci_compatible_lookup_id(pa->pa_id, dce);
}

const struct device_compatible_entry *
pci_compatible_lookup_subsys(const struct pci_attach_args * const pa,
    const struct device_compatible_entry * const dce)
{
	const pcireg_t subsysid =
	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);

	return pci_compatible_lookup_id(subsysid, dce);
}

int
pci_compatible_match(const struct pci_attach_args * const pa,
    const struct device_compatible_entry * const dce)
{
	return pci_compatible_lookup(pa, dce) != NULL;
}

int
pci_compatible_match_subsys(const struct pci_attach_args * const pa,
    const struct device_compatible_entry * const dce)
{
	return pci_compatible_lookup_subsys(pa, dce) != NULL;
}
#endif /* _KERNEL */

/*
 * Descriptions of known PCI classes and subclasses.
 *
 * Subclasses are described in the same way as classes, but have a
 * NULL subclass pointer.
 */
struct pci_class {
	const char	*name;
	u_int		val;		/* as wide as pci_{,sub}class_t */
	const struct pci_class *subclasses;
};

/*
 * Class 0x00.
 * Before rev. 2.0.
 */
static const struct pci_class pci_subclass_prehistoric[] = {
	{ "miscellaneous",	PCI_SUBCLASS_PREHISTORIC_MISC,	NULL,	},
	{ "VGA",		PCI_SUBCLASS_PREHISTORIC_VGA,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x01.
 * Mass storage controller
 */

/* SCSI programming interface */
static const struct pci_class pci_interface_scsi[] = {
	{ "vendor-specific",	PCI_INTERFACE_SCSI_VND,		NULL,	},
	{ "PQI storage",	PCI_INTERFACE_SCSI_PQI_STORAGE,	NULL,	},
	{ "PQI controller",	PCI_INTERFACE_SCSI_PQI_CNTRL,	NULL,	},
	{ "PQI storage and controller",	PCI_INTERFACE_SCSI_PQI_STORAGE_CNTRL,
	  NULL,	},
	{ "NVME",		PCI_INTERFACE_SCSI_NVME,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/* ATA programming interface */
static const struct pci_class pci_interface_ata[] = {
	{ "with single DMA",	PCI_INTERFACE_ATA_SINGLEDMA,	NULL,	},
	{ "with chained DMA",	PCI_INTERFACE_ATA_CHAINEDDMA,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/* SATA programming interface */
static const struct pci_class pci_interface_sata[] = {
	{ "vendor-specific",	PCI_INTERFACE_SATA_VND,		NULL,	},
	{ "AHCI 1.0",		PCI_INTERFACE_SATA_AHCI10,	NULL,	},
	{ "Serial Storage Bus Interface", PCI_INTERFACE_SATA_SSBI, NULL, },
	{ NULL,			0,				NULL,	},
};

/* Flash programming interface */
static const struct pci_class pci_interface_nvm[] = {
	{ "vendor-specific",	PCI_INTERFACE_NVM_VND,		NULL,	},
	{ "NVMHCI 1.0",		PCI_INTERFACE_NVM_NVMHCI10,	NULL,	},
	{ "NVMe I/O",		PCI_INTERFACE_NVM_NVME_IO,	NULL,	},
	{ "NVMe admin",		PCI_INTERFACE_NVM_NVME_ADMIN,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/* UFS programming interface */
static const struct pci_class pci_interface_ufs[] = {
	{ "vendor-specific",	PCI_INTERFACE_UFS_VND,		NULL,	},
	{ "UFSHCI",		PCI_INTERFACE_UFS_UFSHCI,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/* Subclasses */
static const struct pci_class pci_subclass_mass_storage[] = {
	{ "SCSI",		PCI_SUBCLASS_MASS_STORAGE_SCSI,
	  pci_interface_scsi },
	{ "IDE",		PCI_SUBCLASS_MASS_STORAGE_IDE,	NULL,	},
	{ "floppy",		PCI_SUBCLASS_MASS_STORAGE_FLOPPY, NULL, },
	{ "IPI",		PCI_SUBCLASS_MASS_STORAGE_IPI,	NULL,	},
	{ "RAID",		PCI_SUBCLASS_MASS_STORAGE_RAID,	NULL,	},
	{ "ATA",		PCI_SUBCLASS_MASS_STORAGE_ATA,
	  pci_interface_ata },
	{ "SATA",		PCI_SUBCLASS_MASS_STORAGE_SATA,
	  pci_interface_sata },
	{ "SAS",		PCI_SUBCLASS_MASS_STORAGE_SAS,	NULL,	},
	{ "Flash",		PCI_SUBCLASS_MASS_STORAGE_NVM,
	  pci_interface_nvm,	},
	{ "UFS",		PCI_SUBCLASS_MASS_STORAGE_UFS,
	  pci_interface_ufs,	},
	{ "miscellaneous",	PCI_SUBCLASS_MASS_STORAGE_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x02.
 * Network controller.
 */
static const struct pci_class pci_subclass_network[] = {
	{ "ethernet",		PCI_SUBCLASS_NETWORK_ETHERNET,	NULL,	},
	{ "token ring",		PCI_SUBCLASS_NETWORK_TOKENRING,	NULL,	},
	{ "FDDI",		PCI_SUBCLASS_NETWORK_FDDI,	NULL,	},
	{ "ATM",		PCI_SUBCLASS_NETWORK_ATM,	NULL,	},
	{ "ISDN",		PCI_SUBCLASS_NETWORK_ISDN,	NULL,	},
	{ "WorldFip",		PCI_SUBCLASS_NETWORK_WORLDFIP,	NULL,	},
	{ "PCMIG Multi Computing", PCI_SUBCLASS_NETWORK_PCIMGMULTICOMP, NULL, },
	{ "InfiniBand",		PCI_SUBCLASS_NETWORK_INFINIBAND, NULL, },
	{ "Host fabric",	PCI_SUBCLASS_NETWORK_HFC,	NULL, },
	{ "miscellaneous",	PCI_SUBCLASS_NETWORK_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x03.
 * Display controller.
 */

/* VGA programming interface */
static const struct pci_class pci_interface_vga[] = {
	{ "",			PCI_INTERFACE_VGA_VGA,		NULL,	},
	{ "8514-compat",	PCI_INTERFACE_VGA_8514,		NULL,	},
	{ NULL,			0,				NULL,	},
};
/* Subclasses */
static const struct pci_class pci_subclass_display[] = {
	{ "VGA",		PCI_SUBCLASS_DISPLAY_VGA,  pci_interface_vga,},
	{ "XGA",		PCI_SUBCLASS_DISPLAY_XGA,	NULL,	},
	{ "3D",			PCI_SUBCLASS_DISPLAY_3D,	NULL,	},
	{ "miscellaneous",	PCI_SUBCLASS_DISPLAY_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x04.
 * Multimedia device.
 */

/* HD Audio programming interface */
static const struct pci_class pci_interface_hda[] = {
	{ "HD Audio 1.0",	PCI_INTERFACE_HDAUDIO,		NULL,	},
	{ "HD Audio 1.0 + vendor-ext",	PCI_INTERFACE_HDAUDIO_VND, NULL, },
	{ NULL,			0,				NULL,	},
};

static const struct pci_class pci_subclass_multimedia[] = {
	{ "video",		PCI_SUBCLASS_MULTIMEDIA_VIDEO,	NULL,	},
	{ "audio",		PCI_SUBCLASS_MULTIMEDIA_AUDIO,	NULL,	},
	{ "telephony",		PCI_SUBCLASS_MULTIMEDIA_TELEPHONY, NULL,},
	{ "mixed mode",		PCI_SUBCLASS_MULTIMEDIA_HDAUDIO,
	  pci_interface_hda, },
	{ "miscellaneous",	PCI_SUBCLASS_MULTIMEDIA_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x05.
 * Memory controller.
 */
static const struct pci_class pci_subclass_memory[] = {
	{ "RAM",		PCI_SUBCLASS_MEMORY_RAM,	NULL,	},
	{ "flash",		PCI_SUBCLASS_MEMORY_FLASH,	NULL,	},
	{ "miscellaneous",	PCI_SUBCLASS_MEMORY_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x06.
 * Bridge device.
 */

/* PCI bridge programming interface */
static const struct pci_class pci_interface_pcibridge[] = {
	{ "",			PCI_INTERFACE_BRIDGE_PCI_PCI,	NULL,	},
	{ "subtractive decode",	PCI_INTERFACE_BRIDGE_PCI_SUBDEC, NULL,	},
	{ NULL,			0,				NULL,	},
};

/* Semi-transparent PCI-to-PCI bridge programming interface */
static const struct pci_class pci_interface_stpci[] = {
	{ "primary side facing host",	PCI_INTERFACE_STPCI_PRIMARY, NULL, },
	{ "secondary side facing host",	PCI_INTERFACE_STPCI_SECONDARY, NULL, },
	{ NULL,			0,				NULL,	},
};

/* Advanced Switching programming interface */
static const struct pci_class pci_interface_advsw[] = {
	{ "custom interface",	PCI_INTERFACE_ADVSW_CUSTOM,	NULL, },
	{ "ASI-SIG",		PCI_INTERFACE_ADVSW_ASISIG,	NULL, },
	{ NULL,			0,				NULL,	},
};

/* Subclasses */
static const struct pci_class pci_subclass_bridge[] = {
	{ "host",		PCI_SUBCLASS_BRIDGE_HOST,	NULL,	},
	{ "ISA",		PCI_SUBCLASS_BRIDGE_ISA,	NULL,	},
	{ "EISA",		PCI_SUBCLASS_BRIDGE_EISA,	NULL,	},
	{ "MicroChannel",	PCI_SUBCLASS_BRIDGE_MC,		NULL,	},
	{ "PCI",		PCI_SUBCLASS_BRIDGE_PCI,
	  pci_interface_pcibridge },
	{ "PCMCIA",		PCI_SUBCLASS_BRIDGE_PCMCIA,	NULL,	},
	{ "NuBus",		PCI_SUBCLASS_BRIDGE_NUBUS,	NULL,	},
	{ "CardBus",		PCI_SUBCLASS_BRIDGE_CARDBUS,	NULL,	},
	{ "RACEway",		PCI_SUBCLASS_BRIDGE_RACEWAY,	NULL,	},
	{ "Semi-transparent PCI", PCI_SUBCLASS_BRIDGE_STPCI,
	  pci_interface_stpci, },
	{ "InfiniBand",		PCI_SUBCLASS_BRIDGE_INFINIBAND,	NULL,	},
	{ "advanced switching",	PCI_SUBCLASS_BRIDGE_ADVSW,
	  pci_interface_advsw,	},
	{ "miscellaneous",	PCI_SUBCLASS_BRIDGE_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x07.
 * Simple communications controller.
 */

/* Serial controller programming interface */
static const struct pci_class pci_interface_serial[] = {
	{ "generic XT-compat",	PCI_INTERFACE_SERIAL_XT,	NULL,	},
	{ "16450-compat",	PCI_INTERFACE_SERIAL_16450,	NULL,	},
	{ "16550-compat",	PCI_INTERFACE_SERIAL_16550,	NULL,	},
	{ "16650-compat",	PCI_INTERFACE_SERIAL_16650,	NULL,	},
	{ "16750-compat",	PCI_INTERFACE_SERIAL_16750,	NULL,	},
	{ "16850-compat",	PCI_INTERFACE_SERIAL_16850,	NULL,	},
	{ "16950-compat",	PCI_INTERFACE_SERIAL_16950,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/* Parallel controller programming interface */
static const struct pci_class pci_interface_parallel[] = {
	{ "",			PCI_INTERFACE_PARALLEL,			NULL,},
	{ "bi-directional",	PCI_INTERFACE_PARALLEL_BIDIRECTIONAL,	NULL,},
	{ "ECP 1.X-compat",	PCI_INTERFACE_PARALLEL_ECP1X,		NULL,},
	{ "IEEE1284 controller", PCI_INTERFACE_PARALLEL_IEEE1284_CNTRL,	NULL,},
	{ "IEEE1284 target",	PCI_INTERFACE_PARALLEL_IEEE1284_TGT,	NULL,},
	{ NULL,			0,					NULL,},
};

/* Modem programming interface */
static const struct pci_class pci_interface_modem[] = {
	{ "",			PCI_INTERFACE_MODEM,			NULL,},
	{ "Hayes&16450-compat",	PCI_INTERFACE_MODEM_HAYES16450,		NULL,},
	{ "Hayes&16550-compat",	PCI_INTERFACE_MODEM_HAYES16550,		NULL,},
	{ "Hayes&16650-compat",	PCI_INTERFACE_MODEM_HAYES16650,		NULL,},
	{ "Hayes&16750-compat",	PCI_INTERFACE_MODEM_HAYES16750,		NULL,},
	{ NULL,			0,					NULL,},
};

/* Subclasses */
static const struct pci_class pci_subclass_communications[] = {
	{ "serial",		PCI_SUBCLASS_COMMUNICATIONS_SERIAL,
	  pci_interface_serial, },
	{ "parallel",		PCI_SUBCLASS_COMMUNICATIONS_PARALLEL,
	  pci_interface_parallel, },
	{ "multi-port serial",	PCI_SUBCLASS_COMMUNICATIONS_MPSERIAL,	NULL,},
	{ "modem",		PCI_SUBCLASS_COMMUNICATIONS_MODEM,
	  pci_interface_modem, },
	{ "GPIB",		PCI_SUBCLASS_COMMUNICATIONS_GPIB,	NULL,},
	{ "smartcard",		PCI_SUBCLASS_COMMUNICATIONS_SMARTCARD,	NULL,},
	{ "miscellaneous",	PCI_SUBCLASS_COMMUNICATIONS_MISC,	NULL,},
	{ NULL,			0,					NULL,},
};

/*
 * Class 0x08.
 * Base system peripheral.
 */

/* PIC programming interface */
static const struct pci_class pci_interface_pic[] = {
	{ "generic 8259",	PCI_INTERFACE_PIC_8259,		NULL,	},
	{ "ISA PIC",		PCI_INTERFACE_PIC_ISA,		NULL,	},
	{ "EISA PIC",		PCI_INTERFACE_PIC_EISA,		NULL,	},
	{ "IO APIC",		PCI_INTERFACE_PIC_IOAPIC,	NULL,	},
	{ "IO(x) APIC",		PCI_INTERFACE_PIC_IOXAPIC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/* DMA programming interface */
static const struct pci_class pci_interface_dma[] = {
	{ "generic 8237",	PCI_INTERFACE_DMA_8237,		NULL,	},
	{ "ISA",		PCI_INTERFACE_DMA_ISA,		NULL,	},
	{ "EISA",		PCI_INTERFACE_DMA_EISA,		NULL,	},
	{ NULL,			0,				NULL,	},
};

/* Timer programming interface */
static const struct pci_class pci_interface_tmr[] = {
	{ "generic 8254",	PCI_INTERFACE_TIMER_8254,	NULL,	},
	{ "ISA",		PCI_INTERFACE_TIMER_ISA,	NULL,	},
	{ "EISA",		PCI_INTERFACE_TIMER_EISA,	NULL,	},
	{ "HPET",		PCI_INTERFACE_TIMER_HPET,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/* RTC programming interface */
static const struct pci_class pci_interface_rtc[] = {
	{ "generic",		PCI_INTERFACE_RTC_GENERIC,	NULL,	},
	{ "ISA",		PCI_INTERFACE_RTC_ISA,		NULL,	},
	{ NULL,			0,				NULL,	},
};

/* Subclasses */
static const struct pci_class pci_subclass_system[] = {
	{ "interrupt",		PCI_SUBCLASS_SYSTEM_PIC,   pci_interface_pic,},
	{ "DMA",		PCI_SUBCLASS_SYSTEM_DMA,   pci_interface_dma,},
	{ "timer",		PCI_SUBCLASS_SYSTEM_TIMER, pci_interface_tmr,},
	{ "RTC",		PCI_SUBCLASS_SYSTEM_RTC,   pci_interface_rtc,},
	{ "PCI Hot-Plug",	PCI_SUBCLASS_SYSTEM_PCIHOTPLUG, NULL,	},
	{ "SD Host Controller",	PCI_SUBCLASS_SYSTEM_SDHC,	NULL,	},
	{ "IOMMU",		PCI_SUBCLASS_SYSTEM_IOMMU,	NULL,	},
	{ "Root Complex Event Collector", PCI_SUBCLASS_SYSTEM_RCEC, NULL, },
	{ "miscellaneous",	PCI_SUBCLASS_SYSTEM_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x09.
 * Input device.
 */

/* Gameport programming interface */
static const struct pci_class pci_interface_game[] = {
	{ "generic",		PCI_INTERFACE_GAMEPORT_GENERIC,	NULL,	},
	{ "legacy",		PCI_INTERFACE_GAMEPORT_LEGACY,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/* Subclasses */
static const struct pci_class pci_subclass_input[] = {
	{ "keyboard",		PCI_SUBCLASS_INPUT_KEYBOARD,	NULL,	},
	{ "digitizer",		PCI_SUBCLASS_INPUT_DIGITIZER,	NULL,	},
	{ "mouse",		PCI_SUBCLASS_INPUT_MOUSE,	NULL,	},
	{ "scanner",		PCI_SUBCLASS_INPUT_SCANNER,	NULL,	},
	{ "game port",		PCI_SUBCLASS_INPUT_GAMEPORT,
	  pci_interface_game, },
	{ "miscellaneous",	PCI_SUBCLASS_INPUT_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x0a.
 * Docking station.
 */
static const struct pci_class pci_subclass_dock[] = {
	{ "generic",		PCI_SUBCLASS_DOCK_GENERIC,	NULL,	},
	{ "miscellaneous",	PCI_SUBCLASS_DOCK_MISC,		NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x0b.
 * Processor.
 */
static const struct pci_class pci_subclass_processor[] = {
	{ "386",		PCI_SUBCLASS_PROCESSOR_386,	NULL,	},
	{ "486",		PCI_SUBCLASS_PROCESSOR_486,	NULL,	},
	{ "Pentium",		PCI_SUBCLASS_PROCESSOR_PENTIUM, NULL,	},
	{ "Alpha",		PCI_SUBCLASS_PROCESSOR_ALPHA,	NULL,	},
	{ "PowerPC",		PCI_SUBCLASS_PROCESSOR_POWERPC, NULL,	},
	{ "MIPS",		PCI_SUBCLASS_PROCESSOR_MIPS,	NULL,	},
	{ "Co-processor",	PCI_SUBCLASS_PROCESSOR_COPROC,	NULL,	},
	{ "miscellaneous",	PCI_SUBCLASS_PROCESSOR_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x0c.
 * Serial bus controller.
 */

/* IEEE1394 programming interface */
static const struct pci_class pci_interface_ieee1394[] = {
	{ "Firewire",		PCI_INTERFACE_IEEE1394_FIREWIRE,	NULL,},
	{ "OpenHCI",		PCI_INTERFACE_IEEE1394_OPENHCI,		NULL,},
	{ NULL,			0,					NULL,},
};

/* USB programming interface */
static const struct pci_class pci_interface_usb[] = {
	{ "UHCI",		PCI_INTERFACE_USB_UHCI,		NULL,	},
	{ "OHCI",		PCI_INTERFACE_USB_OHCI,		NULL,	},
	{ "EHCI",		PCI_INTERFACE_USB_EHCI,		NULL,	},
	{ "xHCI",		PCI_INTERFACE_USB_XHCI,		NULL,	},
	{ "USB4 HCI",		PCI_INTERFACE_USB_USB4HCI,	NULL,	},
	{ "other HC",		PCI_INTERFACE_USB_OTHERHC,	NULL,	},
	{ "device",		PCI_INTERFACE_USB_DEVICE,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/* IPMI programming interface */
static const struct pci_class pci_interface_ipmi[] = {
	{ "SMIC",		PCI_INTERFACE_IPMI_SMIC,	NULL,	},
	{ "keyboard",		PCI_INTERFACE_IPMI_KBD,		NULL,	},
	{ "block transfer",	PCI_INTERFACE_IPMI_BLOCKXFER,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/* Subclasses */
static const struct pci_class pci_subclass_serialbus[] = {
	{ "IEEE1394",		PCI_SUBCLASS_SERIALBUS_FIREWIRE,
	  pci_interface_ieee1394, },
	{ "ACCESS.bus",		PCI_SUBCLASS_SERIALBUS_ACCESS,	NULL,	},
	{ "SSA",		PCI_SUBCLASS_SERIALBUS_SSA,	NULL,	},
	{ "USB",		PCI_SUBCLASS_SERIALBUS_USB,
	  pci_interface_usb, },
	/* XXX Fiber Channel/_FIBRECHANNEL */
	{ "Fiber Channel",	PCI_SUBCLASS_SERIALBUS_FIBER,	NULL,	},
	{ "SMBus",		PCI_SUBCLASS_SERIALBUS_SMBUS,	NULL,	},
	{ "InfiniBand",		PCI_SUBCLASS_SERIALBUS_INFINIBAND, NULL,},
	{ "IPMI",		PCI_SUBCLASS_SERIALBUS_IPMI,
	  pci_interface_ipmi, },
	{ "SERCOS",		PCI_SUBCLASS_SERIALBUS_SERCOS,	NULL,	},
	{ "CANbus",		PCI_SUBCLASS_SERIALBUS_CANBUS,	NULL,	},
	{ "MIPI I3C",		PCI_SUBCLASS_SERIALBUS_MIPI_I3C, NULL,	},
	{ "miscellaneous",	PCI_SUBCLASS_SERIALBUS_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x0d.
 * Wireless Controller.
 */
static const struct pci_class pci_subclass_wireless[] = {
	{ "IrDA",		PCI_SUBCLASS_WIRELESS_IRDA,	NULL,	},
	{ "Consumer IR",/*XXX*/	PCI_SUBCLASS_WIRELESS_CONSUMERIR, NULL,	},
	{ "RF",			PCI_SUBCLASS_WIRELESS_RF,	NULL,	},
	{ "bluetooth",		PCI_SUBCLASS_WIRELESS_BLUETOOTH, NULL,	},
	{ "broadband",		PCI_SUBCLASS_WIRELESS_BROADBAND, NULL,	},
	{ "802.11a (5 GHz)",	PCI_SUBCLASS_WIRELESS_802_11A,	NULL,	},
	{ "802.11b (2.4 GHz)",	PCI_SUBCLASS_WIRELESS_802_11B,	NULL,	},
	{ "Cellular",		PCI_SUBCLASS_WIRELESS_CELL,	NULL,	},
	{ "Cellular + Ethernet", PCI_SUBCLASS_WIRELESS_CELL_E,	NULL,	},
	{ "miscellaneous",	PCI_SUBCLASS_WIRELESS_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x0e.
 * Intelligent IO controller.
 */

/* Intelligent IO programming interface */
static const struct pci_class pci_interface_i2o[] = {
	{ "FIFO at offset 0x40", PCI_INTERFACE_I2O_FIFOAT40,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/* Subclasses */
static const struct pci_class pci_subclass_i2o[] = {
	{ "standard",		PCI_SUBCLASS_I2O_STANDARD, pci_interface_i2o,},
	{ "miscellaneous",	PCI_SUBCLASS_I2O_MISC,		NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x0f.
 * Satellite communication controller.
 */
static const struct pci_class pci_subclass_satcom[] = {
	{ "TV",			PCI_SUBCLASS_SATCOM_TV,		NULL,	},
	{ "audio",		PCI_SUBCLASS_SATCOM_AUDIO,	NULL,	},
	{ "voice",		PCI_SUBCLASS_SATCOM_VOICE,	NULL,	},
	{ "data",		PCI_SUBCLASS_SATCOM_DATA,	NULL,	},
	{ "miscellaneous",	PCI_SUBCLASS_SATCOM_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x10.
 * Encryption/Decryption controller.
 */
static const struct pci_class pci_subclass_crypto[] = {
	{ "network/computing",	PCI_SUBCLASS_CRYPTO_NETCOMP,	NULL,	},
	{ "entertainment",	PCI_SUBCLASS_CRYPTO_ENTERTAINMENT, NULL,},
	{ "miscellaneous",	PCI_SUBCLASS_CRYPTO_MISC,	NULL,	},
	{ NULL,			0,				NULL,	},
};

/*
 * Class 0x11.
 * Data aquuisition and signal processing controller.
 */
static const struct pci_class pci_subclass_dasp[] = {
	{ "DPIO",		PCI_SUBCLASS_DASP_DPIO,		NULL,	},
	{ "performance counters", PCI_SUBCLASS_DASP_TIMEFREQ,	NULL,	},
	{ "synchronization",	PCI_SUBCLASS_DASP_SYNC,		NULL,	},
	{ "management",		PCI_SUBCLASS_DASP_MGMT,		NULL,	},
	{ "miscellaneous",	PCI_SUBCLASS_DASP_MISC,		NULL,	},
	{ NULL,			0,				NULL,	},
};

/* List of classes */
static const struct pci_class pci_classes[] = {
	{ "prehistoric",	PCI_CLASS_PREHISTORIC,
	    pci_subclass_prehistoric,				},
	{ "mass storage",	PCI_CLASS_MASS_STORAGE,
	    pci_subclass_mass_storage,				},
	{ "network",		PCI_CLASS_NETWORK,
	    pci_subclass_network,				},
	{ "display",		PCI_CLASS_DISPLAY,
	    pci_subclass_display,				},
	{ "multimedia",		PCI_CLASS_MULTIMEDIA,
	    pci_subclass_multimedia,				},
	{ "memory",		PCI_CLASS_MEMORY,
	    pci_subclass_memory,				},
	{ "bridge",		PCI_CLASS_BRIDGE,
	    pci_subclass_bridge,				},
	{ "communications",	PCI_CLASS_COMMUNICATIONS,
	    pci_subclass_communications,			},
	{ "system",		PCI_CLASS_SYSTEM,
	    pci_subclass_system,				},
	{ "input",		PCI_CLASS_INPUT,
	    pci_subclass_input,					},
	{ "dock",		PCI_CLASS_DOCK,
	    pci_subclass_dock,					},
	{ "processor",		PCI_CLASS_PROCESSOR,
	    pci_subclass_processor,				},
	{ "serial bus",		PCI_CLASS_SERIALBUS,
	    pci_subclass_serialbus,				},
	{ "wireless",		PCI_CLASS_WIRELESS,
	    pci_subclass_wireless,				},
	{ "I2O",		PCI_CLASS_I2O,
	    pci_subclass_i2o,					},
	{ "satellite comm",	PCI_CLASS_SATCOM,
	    pci_subclass_satcom,				},
	{ "crypto",		PCI_CLASS_CRYPTO,
	    pci_subclass_crypto,				},
	{ "DASP",		PCI_CLASS_DASP,
	    pci_subclass_dasp,					},
	{ "processing accelerators", PCI_CLASS_ACCEL,
	    NULL,						},
	{ "non-essential instrumentation", PCI_CLASS_INSTRUMENT,
	    NULL,						},
	{ "undefined",		PCI_CLASS_UNDEFINED,
	    NULL,						},
	{ NULL,			0,
	    NULL,						},
};

DEV_VERBOSE_DEFINE(pci);

/*
 * Append a formatted string to dest without writing more than len
 * characters (including the trailing NUL character).  dest and len
 * are updated for use in subsequent calls to snappendf().
 *
 * Returns 0 on success, a negative value if vnsprintf() fails, or
 * a positive value if the dest buffer would have overflowed.
 */

static int __printflike(3, 4)
snappendf(char **dest, size_t *len, const char * restrict fmt, ...)
{
	va_list	ap;
	int count;

	va_start(ap, fmt);
	count = vsnprintf(*dest, *len, fmt, ap);
	va_end(ap);

	/* Let vsnprintf() errors bubble up to caller */
	if (count < 0 || *len == 0)
		return count;

	/* Handle overflow */
	if ((size_t)count >= *len) {
		*dest += *len - 1;
		*len = 1;
		return 1;
	}

	/* Update dest & len to point at trailing NUL */
	*dest += count;
	*len -= count;

	return 0;
}

void
pci_devinfo(pcireg_t id_reg, pcireg_t class_reg, int showclass, char *cp,
    size_t l)
{
	pci_class_t class;
	pci_subclass_t subclass;
	pci_interface_t interface;
	pci_revision_t revision;
	char vendor[PCI_VENDORSTR_LEN], product[PCI_PRODUCTSTR_LEN];
	const struct pci_class *classp, *subclassp, *interfacep;

	class = PCI_CLASS(class_reg);
	subclass = PCI_SUBCLASS(class_reg);
	interface = PCI_INTERFACE(class_reg);
	revision = PCI_REVISION(class_reg);

	pci_findvendor(vendor, sizeof(vendor), PCI_VENDOR(id_reg));
	pci_findproduct(product, sizeof(product), PCI_VENDOR(id_reg),
	    PCI_PRODUCT(id_reg));

	classp = pci_classes;
	while (classp->name != NULL) {
		if (class == classp->val)
			break;
		classp++;
	}

	subclassp = (classp->name != NULL) ? classp->subclasses : NULL;
	while (subclassp && subclassp->name != NULL) {
		if (subclass == subclassp->val)
			break;
		subclassp++;
	}

	interfacep = (subclassp && subclassp->name != NULL) ?
	    subclassp->subclasses : NULL;
	while (interfacep && interfacep->name != NULL) {
		if (interface == interfacep->val)
			break;
		interfacep++;
	}

	(void)snappendf(&cp, &l, "%s %s", vendor, product);
	if (showclass) {
		(void)snappendf(&cp, &l, " (");
		if (classp->name == NULL)
			(void)snappendf(&cp, &l,
			    "class 0x%02x, subclass 0x%02x",
			    class, subclass);
		else {
			if (subclassp == NULL || subclassp->name == NULL)
				(void)snappendf(&cp, &l,
				    "%s, subclass 0x%02x",
				    classp->name, subclass);
			else
				(void)snappendf(&cp, &l, "%s %s",
				    subclassp->name, classp->name);
		}
		if ((interfacep == NULL) || (interfacep->name == NULL)) {
			if (interface != 0)
				(void)snappendf(&cp, &l, ", interface 0x%02x",
				    interface);
		} else if (strncmp(interfacep->name, "", 1) != 0)
			(void)snappendf(&cp, &l, ", %s", interfacep->name);
		if (revision != 0)
			(void)snappendf(&cp, &l, ", revision 0x%02x", revision);
		(void)snappendf(&cp, &l, ")");
	}
}

#ifdef _KERNEL
void
pci_aprint_devinfo_fancy(const struct pci_attach_args *pa, const char *naive,
			 const char *known, int addrev)
{
	char devinfo[256];

	if (known) {
		aprint_normal(": %s", known);
		if (addrev)
			aprint_normal(" (rev. 0x%02x)",
				      PCI_REVISION(pa->pa_class));
		aprint_normal("\n");
	} else {
		pci_devinfo(pa->pa_id, pa->pa_class, 0,
			    devinfo, sizeof(devinfo));
		aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
			      PCI_REVISION(pa->pa_class));
	}
	if (naive)
		aprint_naive(": %s\n", naive);
	else
		aprint_naive("\n");
}
#endif

/*
 * Print out most of the PCI configuration registers.  Typically used
 * in a device attach routine like this:
 *
 *	#ifdef MYDEV_DEBUG
 *		printf("%s: ", device_xname(sc->sc_dev));
 *		pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
 *	#endif
 */

#define	i2o(i)	((i) * 4)
#define	o2i(o)	((o) / 4)
#define	onoff2(str, rval, bit, onstr, offstr)				\
	/*CONSTCOND*/							\
	printf("      %s: %s\n", (str), ((rval) & (bit)) ? onstr : offstr);
#define	onoff(str, rval, bit)	onoff2(str, rval, bit, "on", "off")

static void
pci_conf_print_common(
#ifdef _KERNEL
    pci_chipset_tag_t pc, pcitag_t tag,
#endif
    const pcireg_t *regs)
{
	pci_class_t class;
	pci_subclass_t subclass;
	pci_interface_t interface;
	pci_revision_t revision;
	char vendor[PCI_VENDORSTR_LEN], product[PCI_PRODUCTSTR_LEN];
	const struct pci_class *classp, *subclassp, *interfacep;
	const char *name;
	pcireg_t rval;
	unsigned int num;

	rval = regs[o2i(PCI_CLASS_REG)];
	class = PCI_CLASS(rval);
	subclass = PCI_SUBCLASS(rval);
	interface = PCI_INTERFACE(rval);
	revision = PCI_REVISION(rval);

	rval = regs[o2i(PCI_ID_REG)];
	name = pci_findvendor(vendor, sizeof(vendor), PCI_VENDOR(rval));
	if (name)
		printf("    Vendor Name: %s (0x%04x)\n", name,
		    PCI_VENDOR(rval));
	else
		printf("    Vendor ID: 0x%04x\n", PCI_VENDOR(rval));
	name = pci_findproduct(product, sizeof(product), PCI_VENDOR(rval),
	    PCI_PRODUCT(rval));
	if (name)
		printf("    Device Name: %s (0x%04x)\n", name,
		    PCI_PRODUCT(rval));
	else
		printf("    Device ID: 0x%04x\n", PCI_PRODUCT(rval));

	rval = regs[o2i(PCI_COMMAND_STATUS_REG)];

	printf("    Command register: 0x%04x\n", rval & 0xffff);
	onoff("I/O space accesses", rval, PCI_COMMAND_IO_ENABLE);
	onoff("Memory space accesses", rval, PCI_COMMAND_MEM_ENABLE);
	onoff("Bus mastering", rval, PCI_COMMAND_MASTER_ENABLE);
	onoff("Special cycles", rval, PCI_COMMAND_SPECIAL_ENABLE);
	onoff("MWI transactions", rval, PCI_COMMAND_INVALIDATE_ENABLE);
	onoff("Palette snooping", rval, PCI_COMMAND_PALETTE_ENABLE);
	onoff("Parity error checking", rval, PCI_COMMAND_PARITY_ENABLE);
	onoff("Address/data stepping", rval, PCI_COMMAND_STEPPING_ENABLE);
	onoff("System error (SERR)", rval, PCI_COMMAND_SERR_ENABLE);
	onoff("Fast back-to-back transactions", rval,
	    PCI_COMMAND_BACKTOBACK_ENABLE);
	onoff("Interrupt disable", rval, PCI_COMMAND_INTERRUPT_DISABLE);

	printf("    Status register: 0x%04x\n", (rval >> 16) & 0xffff);
	onoff("Immediate Readiness", rval, PCI_STATUS_IMMD_READNESS);
	onoff2("Interrupt status", rval, PCI_STATUS_INT_STATUS, "active",
	    "inactive");
	onoff("Capability List support", rval, PCI_STATUS_CAPLIST_SUPPORT);
	onoff("66 MHz capable", rval, PCI_STATUS_66MHZ_SUPPORT);
	onoff("User Definable Features (UDF) support", rval,
	    PCI_STATUS_UDF_SUPPORT);
	onoff("Fast back-to-back capable", rval,
	    PCI_STATUS_BACKTOBACK_SUPPORT);
	onoff("Data parity error detected", rval, PCI_STATUS_PARITY_ERROR);

	printf("      DEVSEL timing: ");
	switch (rval & PCI_STATUS_DEVSEL_MASK) {
	case PCI_STATUS_DEVSEL_FAST:
		printf("fast");
		break;
	case PCI_STATUS_DEVSEL_MEDIUM:
		printf("medium");
		break;
	case PCI_STATUS_DEVSEL_SLOW:
		printf("slow");
		break;
	default:
		printf("unknown/reserved");	/* XXX */
		break;
	}
	printf(" (0x%x)\n", PCIREG_SHIFTOUT(rval, PCI_STATUS_DEVSEL_MASK));

	onoff("Slave signaled Target Abort", rval,
	    PCI_STATUS_TARGET_TARGET_ABORT);
	onoff("Master received Target Abort", rval,
	    PCI_STATUS_MASTER_TARGET_ABORT);
	onoff("Master received Master Abort", rval, PCI_STATUS_MASTER_ABORT);
	onoff("Asserted System Error (SERR)", rval, PCI_STATUS_SPECIAL_ERROR);
	onoff("Parity error detected", rval, PCI_STATUS_PARITY_DETECT);

	rval = regs[o2i(PCI_CLASS_REG)];
	for (classp = pci_classes; classp->name != NULL; classp++) {
		if (class == classp->val)
			break;
	}

	/*
	 * ECN: Change Root Complex Event Collector Class Code
	 * Old RCEC has subclass 0x06. It's the same as IOMMU. Read the type
	 * in PCIe extend capability to know whether it's RCEC or IOMMU.
	 */
	if ((class == PCI_CLASS_SYSTEM)
	    && (subclass == PCI_SUBCLASS_SYSTEM_IOMMU)) {
		int pcie_capoff;
		pcireg_t reg;

		if (pci_conf_find_cap(regs, PCI_CAP_PCIEXPRESS, &pcie_capoff)) {
			reg = regs[o2i(pcie_capoff + PCIE_XCAP)];
			if (PCIE_XCAP_TYPE(reg) == PCIE_XCAP_TYPE_RC_EVNTC)
				subclass = PCI_SUBCLASS_SYSTEM_RCEC;
		}
	}
	subclassp = (classp->name != NULL) ? classp->subclasses : NULL;
	while (subclassp && subclassp->name != NULL) {
		if (subclass == subclassp->val)
			break;
		subclassp++;
	}

	interfacep = (subclassp && subclassp->name != NULL) ?
	    subclassp->subclasses : NULL;
	while (interfacep && interfacep->name != NULL) {
		if (interface == interfacep->val)
			break;
		interfacep++;
	}

	if (classp->name != NULL)
		printf("    Class Name: %s (0x%02x)\n", classp->name, class);
	else
		printf("    Class ID: 0x%02x\n", class);
	if (subclassp != NULL && subclassp->name != NULL)
		printf("    Subclass Name: %s (0x%02x)\n",
		    subclassp->name, PCI_SUBCLASS(rval));
	else
		printf("    Subclass ID: 0x%02x\n", PCI_SUBCLASS(rval));
	if ((interfacep != NULL) && (interfacep->name != NULL)
	    && (strncmp(interfacep->name, "", 1) != 0))
		printf("    Interface Name: %s (0x%02x)\n",
		    interfacep->name, interface);
	else
		printf("    Interface: 0x%02x\n", interface);
	printf("    Revision ID: 0x%02x\n", revision);

	rval = regs[o2i(PCI_BHLC_REG)];
	printf("    BIST: 0x%02x\n", PCI_BIST(rval));
	printf("    Header Type: 0x%02x%s (0x%02x)\n", PCI_HDRTYPE_TYPE(rval),
	    PCI_HDRTYPE_MULTIFN(rval) ? "+multifunction" : "",
	    PCI_HDRTYPE(rval));
	printf("    Latency Timer: 0x%02x\n", PCI_LATTIMER(rval));
	num = PCI_CACHELINE(rval);
	printf("    Cache Line Size: %ubytes (0x%02x)\n", num * 4, num);
}

static int
pci_conf_print_bar(
#ifdef _KERNEL
    pci_chipset_tag_t pc, pcitag_t tag,
#endif
    const pcireg_t *regs, int reg, const char *name)
{
	int width;
	pcireg_t rval, rval64h;
	bool ioen, memen;
#ifdef _KERNEL
	pcireg_t mask, mask64h = 0;
#endif

	rval = regs[o2i(PCI_COMMAND_STATUS_REG)];
	ioen = rval & PCI_COMMAND_IO_ENABLE;
	memen = rval & PCI_COMMAND_MEM_ENABLE;

	width = 4;
	/*
	 * Section 6.2.5.1, `Address Maps', tells us that:
	 *
	 * 1) The builtin software should have already mapped the
	 * device in a reasonable way.
	 *
	 * 2) A device which wants 2^n bytes of memory will hardwire
	 * the bottom n bits of the address to 0.  As recommended,
	 * we write all 1s and see what we get back.
	 */

	rval = regs[o2i(reg)];
	if (PCI_MAPREG_TYPE(rval) == PCI_MAPREG_TYPE_MEM &&
	    PCI_MAPREG_MEM_TYPE(rval) == PCI_MAPREG_MEM_TYPE_64BIT) {
		rval64h = regs[o2i(reg + 4)];
		width = 8;
	} else
		rval64h = 0;

#ifdef _KERNEL
	if (rval != 0 && memen) {
		int s;

		/*
		 * The following sequence seems to make some devices
		 * (e.g. host bus bridges, which don't normally
		 * have their space mapped) very unhappy, to
		 * the point of crashing the system.
		 *
		 * Therefore, if the mapping register is zero to
		 * start out with, don't bother trying.
		 */
		s = splhigh();
		pci_conf_write(pc, tag, reg, 0xffffffff);
		mask = pci_conf_read(pc, tag, reg);
		pci_conf_write(pc, tag, reg, rval);
		if (PCI_MAPREG_TYPE(rval) == PCI_MAPREG_TYPE_MEM &&
		    PCI_MAPREG_MEM_TYPE(rval) == PCI_MAPREG_MEM_TYPE_64BIT) {
			pci_conf_write(pc, tag, reg + 4, 0xffffffff);
			mask64h = pci_conf_read(pc, tag, reg + 4);
			pci_conf_write(pc, tag, reg + 4, rval64h);
		}
		splx(s);
	} else
		mask = mask64h = 0;
#endif /* _KERNEL */

	printf("    Base address register at 0x%02x", reg);
	if (name)
		printf(" (%s)", name);
	printf("\n      ");
	if (rval == 0) {
		printf("not implemented\n");
		return width;
	}
	printf("type: ");
	if (PCI_MAPREG_TYPE(rval) == PCI_MAPREG_TYPE_MEM) {
		const char *type, *prefetch;

		switch (PCI_MAPREG_MEM_TYPE(rval)) {
		case PCI_MAPREG_MEM_TYPE_32BIT:
			type = "32-bit";
			break;
		case PCI_MAPREG_MEM_TYPE_32BIT_1M:
			type = "32-bit-1M";
			break;
		case PCI_MAPREG_MEM_TYPE_64BIT:
			type = "64-bit";
			break;
		default:
			type = "unknown (XXX)";
			break;
		}
		if (PCI_MAPREG_MEM_PREFETCHABLE(rval))
			prefetch = "";
		else
			prefetch = "non";
		printf("%s %sprefetchable memory\n", type, prefetch);
		switch (PCI_MAPREG_MEM_TYPE(rval)) {
		case PCI_MAPREG_MEM_TYPE_64BIT:
			printf("      base: 0x%016llx",
			    PCI_MAPREG_MEM64_ADDR(
				((((long long) rval64h) << 32) | rval)));
			if (!memen)
				printf(", disabled");
			printf("\n");
#ifdef _KERNEL
			printf("      size: 0x%016llx\n",
			    PCI_MAPREG_MEM64_SIZE(
				    ((((long long) mask64h) << 32) | mask)));
#endif
			break;
		case PCI_MAPREG_MEM_TYPE_32BIT:
		case PCI_MAPREG_MEM_TYPE_32BIT_1M:
		default:
			printf("      base: 0x%08x",
			    PCI_MAPREG_MEM_ADDR(rval));
			if (!memen)
				printf(", disabled");
			printf("\n");
#ifdef _KERNEL
			printf("      size: 0x%08x\n",
			    PCI_MAPREG_MEM_SIZE(mask));
#endif
			break;
		}
	} else {
#ifdef _KERNEL
		if (ioen)
			printf("%d-bit ", mask & ~0x0000ffff ? 32 : 16);
#endif
		printf("I/O\n");
		printf("      base: 0x%08x", PCI_MAPREG_IO_ADDR(rval));
		if (!ioen)
			printf(", disabled");
		printf("\n");
#ifdef _KERNEL
		printf("      size: 0x%08x\n", PCI_MAPREG_IO_SIZE(mask));
#endif
	}

	return width;
}

static void
pci_conf_print_regs(const pcireg_t *regs, int first, int pastlast)
{
	int off, needaddr, neednl;

	needaddr = 1;
	neednl = 0;
	for (off = first; off < pastlast; off += 4) {
		if ((off % 16) == 0 || needaddr) {
			printf("    0x%02x:", off);
			needaddr = 0;
		}
		printf(" 0x%08x", regs[o2i(off)]);
		neednl = 1;
		if ((off % 16) == 12) {
			printf("\n");
			neednl = 0;
		}
	}
	if (neednl)
		printf("\n");
}

static const char *
pci_conf_print_agp_calcycle(uint8_t cal)
{

	switch (cal) {
	case 0x0:
		return "4ms";
	case 0x1:
		return "16ms";
	case 0x2:
		return "64ms";
	case 0x3:
		return "256ms";
	case 0x7:
		return "Calibration Cycle Not Needed";
	default:
		return "(reserved)";
	}
}

static void
pci_conf_print_agp_datarate(pcireg_t reg, bool isagp3)
{
	if (isagp3) {
		/* AGP 3.0 */
		if (reg & AGP_MODE_V3_RATE_4x)
			printf("x4");
		if (reg & AGP_MODE_V3_RATE_8x)
			printf("x8");
	} else {
		/* AGP 2.0 */
		if (reg & AGP_MODE_V2_RATE_1x)
			printf("x1");
		if (reg & AGP_MODE_V2_RATE_2x)
			printf("x2");
		if (reg & AGP_MODE_V2_RATE_4x)
			printf("x4");
	}
	printf("\n");
}

static void
pci_conf_print_agp_cap(const pcireg_t *regs, int capoff)
{
	pcireg_t rval;
	bool isagp3;

	printf("\n  AGP Capabilities Register\n");

	rval = regs[o2i(capoff)];
	printf("    Revision: %d.%d\n",
	    PCI_CAP_AGP_MAJOR(rval), PCI_CAP_AGP_MINOR(rval));

	rval = regs[o2i(capoff + PCI_AGP_STATUS)];
	printf("    Status register: 0x%04x\n", rval);
	printf("      RQ: %u\n",
	    PCIREG_SHIFTOUT(rval, AGP_MODE_RQ) + 1);
	printf("      ARQSZ: %u\n",
	    PCIREG_SHIFTOUT(rval, AGP_MODE_ARQSZ));
	printf("      CAL cycle: %s\n",
	       pci_conf_print_agp_calcycle(PCIREG_SHIFTOUT(rval, AGP_MODE_CAL)));
	onoff("SBA", rval, AGP_MODE_SBA);
	onoff("htrans#", rval, AGP_MODE_HTRANS);
	onoff("Over 4G", rval, AGP_MODE_4G);
	onoff("Fast Write", rval, AGP_MODE_FW);
	onoff("AGP 3.0 Mode", rval, AGP_MODE_MODE_3);
	isagp3 = rval & AGP_MODE_MODE_3;
	printf("      Data Rate Support: ");
	pci_conf_print_agp_datarate(rval, isagp3);

	rval = regs[o2i(capoff + PCI_AGP_COMMAND)];
	printf("    Command register: 0x%08x\n", rval);
	printf("      PRQ: %u\n",
	    PCIREG_SHIFTOUT(rval, AGP_MODE_RQ) + 1);
	printf("      PARQSZ: %u\n",
	    PCIREG_SHIFTOUT(rval, AGP_MODE_ARQSZ));
	printf("      PCAL cycle: %s\n",
	       pci_conf_print_agp_calcycle(PCIREG_SHIFTOUT(rval, AGP_MODE_CAL)));
	onoff("SBA", rval, AGP_MODE_SBA);
	onoff("AGP", rval, AGP_MODE_AGP);
	onoff("Over 4G", rval, AGP_MODE_4G);
	onoff("Fast Write", rval, AGP_MODE_FW);
	if (isagp3) {
		printf("      Data Rate Enable: ");
		/*
		 * The Data Rate Enable bits are used only on 3.0 and the
		 * Command register has no AGP_MODE_MODE_3 bit, so pass the
		 * flag to print correctly.
		 */
		pci_conf_print_agp_datarate(rval, isagp3);
	}
}

static const char *
pci_conf_print_pcipm_cap_aux(uint16_t caps)
{

	switch ((caps >> 6) & 7) {
	case 0:	return "self-powered";
	case 1: return "55 mA";
	case 2: return "100 mA";
	case 3: return "160 mA";
	case 4: return "220 mA";
	case 5: return "270 mA";
	case 6: return "320 mA";
	case 7:
	default: return "375 mA";
	}
}

static const char *
pci_conf_print_pcipm_cap_pmrev(uint8_t val)
{
	static const char unk[] = "unknown";
	static const char *pmrev[8] = {
		unk, "1.0", "1.1", "1.2", unk, unk, unk, unk
	};
	if (val > 7)
		return unk;
	return pmrev[val];
}

static void
pci_conf_print_pcipm_cap(const pcireg_t *regs, int capoff)
{
	uint16_t caps, pmcsr;

	caps = regs[o2i(capoff)] >> PCI_PMCR_SHIFT;
	pmcsr = regs[o2i(capoff + PCI_PMCSR)];

	printf("\n  PCI Power Management Capabilities Register\n");

	printf("    Capabilities register: 0x%04x\n", caps);
	printf("      Version: %s\n",
	    pci_conf_print_pcipm_cap_pmrev(caps & PCI_PMCR_VERSION_MASK));
	onoff("PME# clock", caps, PCI_PMCR_PME_CLOCK);
	onoff("Device specific initialization", caps, PCI_PMCR_DSI);
	printf("      3.3V auxiliary current: %s\n",
	    pci_conf_print_pcipm_cap_aux(caps));
	onoff("D1 power management state support", caps, PCI_PMCR_D1SUPP);
	onoff("D2 power management state support", caps, PCI_PMCR_D2SUPP);
	onoff("PME# support D0", caps, PCI_PMCR_PME_D0);
	onoff("PME# support D1", caps, PCI_PMCR_PME_D1);
	onoff("PME# support D2", caps, PCI_PMCR_PME_D2);
	onoff("PME# support D3 hot", caps, PCI_PMCR_PME_D3HOT);
	onoff("PME# support D3 cold", caps, PCI_PMCR_PME_D3COLD);

	printf("    Control/status register: 0x%08x\n", pmcsr);
	printf("      Power state: D%d\n", pmcsr & PCI_PMCSR_STATE_MASK);
	onoff("PCI Express reserved", (pmcsr >> 2), 1);
	onoff("No soft reset", pmcsr, PCI_PMCSR_NO_SOFTRST);
	printf("      PME# assertion: %sabled\n",
	    (pmcsr & PCI_PMCSR_PME_EN) ? "en" : "dis");
	printf("      Data Select: %d\n",
	    PCIREG_SHIFTOUT(pmcsr, PCI_PMCSR_DATASEL_MASK));
	printf("      Data Scale: %d\n",
	    PCIREG_SHIFTOUT(pmcsr, PCI_PMCSR_DATASCL_MASK));
	onoff("PME# status", pmcsr, PCI_PMCSR_PME_STS);
	printf("    Bridge Support Extensions register: 0x%02x\n",
	    (pmcsr >> 16) & 0xff);
	onoff("B2/B3 support", pmcsr, PCI_PMCSR_B2B3_SUPPORT);
	onoff("Bus Power/Clock Control Enable", pmcsr, PCI_PMCSR_BPCC_EN);
	printf("    Data register: 0x%02x\n",
	       PCIREG_SHIFTOUT(pmcsr, PCI_PMCSR_DATA));
}

/* XXX pci_conf_print_vpd_cap */
/* XXX pci_conf_print_slotid_cap */

static void
pci_conf_print_msi_cap(const pcireg_t *regs, int capoff)
{
	uint32_t ctl, mmc, mme;

	regs += o2i(capoff);
	ctl = *regs++;
	mmc = PCIREG_SHIFTOUT(ctl, PCI_MSI_CTL_MMC_MASK);
	mme = PCIREG_SHIFTOUT(ctl, PCI_MSI_CTL_MME_MASK);

	printf("\n  PCI Message Signaled Interrupt\n");

	printf("    Message Control register: 0x%04x\n", ctl >> 16);
	onoff("MSI Enabled", ctl, PCI_MSI_CTL_MSI_ENABLE);
	printf("      Multiple Message Capable: %s (%d vector%s)\n",
	    mmc > 0 ? "yes" : "no", 1 << mmc, mmc > 0 ? "s" : "");
	printf("      Multiple Message Enabled: %s (%d vector%s)\n",
	    mme > 0 ? "on" : "off", 1 << mme, mme > 0 ? "s" : "");
	onoff("64 Bit Address Capable", ctl, PCI_MSI_CTL_64BIT_ADDR);
	onoff("Per-Vector Masking Capable", ctl, PCI_MSI_CTL_PERVEC_MASK);
	onoff("Extended Message Data Capable", ctl, PCI_MSI_CTL_EXTMDATA_CAP);
	onoff("Extended Message Data Enable", ctl, PCI_MSI_CTL_EXTMDATA_EN);
	printf("    Message Address %sregister: 0x%08x\n",
	    ctl & PCI_MSI_CTL_64BIT_ADDR ? "(lower) " : "", *regs++);
	if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
		printf("    Message Address %sregister: 0x%08x\n",
		    "(upper) ", *regs++);
	}
	printf("    Message Data register: ");
	if (ctl & PCI_MSI_CTL_EXTMDATA_CAP)
		printf("0x%08x\n", *regs);
	else
		printf("0x%04x\n", *regs & 0xffff);
	regs++;
	if (ctl & PCI_MSI_CTL_PERVEC_MASK) {
		printf("    Vector Mask register: 0x%08x\n", *regs++);
		printf("    Vector Pending register: 0x%08x\n", *regs++);
	}
}

/* XXX pci_conf_print_cpci_hostwap_cap */

/*
 * For both command register and status register.
 * The argument "idx" is index number (0 to 7).
 */
static int
pcix_split_trans(unsigned int idx)
{
	static int table[8] = {
		1, 2, 3, 4, 8, 12, 16, 32
	};

	if (idx >= __arraycount(table))
		return -1;
	return table[idx];
}

static void
pci_conf_print_pcix_cap_2ndbusmode(int num)
{
	const char *maxfreq, *maxperiod;

	printf("      Mode: ");
	if (num <= 0x07)
		printf("PCI-X Mode 1\n");
	else if (num <= 0x0b)
		printf("PCI-X 266 (Mode 2)\n");
	else
		printf("PCI-X 533 (Mode 2)\n");

	printf("      Error protection: %s\n", (num <= 3) ? "parity" : "ECC");
	switch (num & 0x03) {
	default:
	case 0:
		maxfreq = "N/A";
		maxperiod = "N/A";
		break;
	case 1:
		maxfreq = "66MHz";
		maxperiod = "15ns";
		break;
	case 2:
		maxfreq = "100MHz";
		maxperiod = "10ns";
		break;
	case 3:
		maxfreq = "133MHz";
		maxperiod = "7.5ns";
		break;
	}
	printf("      Max Clock Freq: %s\n", maxfreq);
	printf("      Min Clock Period: %s\n", maxperiod);
}

static void
pci_conf_print_pcix_cap(const pcireg_t *regs, int capoff)
{
	pcireg_t reg;
	int isbridge;
	int i;

	isbridge = (PCI_HDRTYPE_TYPE(regs[o2i(PCI_BHLC_REG)])
	    & PCI_HDRTYPE_PPB) != 0 ? 1 : 0;
	printf("\n  PCI-X %s Capabilities Register\n",
	    isbridge ? "Bridge" : "Non-bridge");

	reg = regs[o2i(capoff)];
	if (isbridge != 0) {
		printf("    Secondary status register: 0x%04x\n",
		    (reg & 0xffff0000) >> 16);
		onoff("64bit device", reg, PCIX_STATUS_64BIT);
		onoff("133MHz capable", reg, PCIX_STATUS_133);
		onoff("Split completion discarded", reg, PCIX_STATUS_SPLDISC);
		onoff("Unexpected split completion", reg, PCIX_STATUS_SPLUNEX);
		onoff("Split completion overrun", reg, PCIX_BRIDGE_ST_SPLOVRN);
		onoff("Split request delayed", reg, PCIX_BRIDGE_ST_SPLRQDL);
		pci_conf_print_pcix_cap_2ndbusmode(
			PCIREG_SHIFTOUT(reg, PCIX_BRIDGE_2NDST_CLKF));
		printf("      Version: 0x%x\n",
		    (reg & PCIX_BRIDGE_2NDST_VER_MASK)
		    >> PCIX_BRIDGE_2NDST_VER_SHIFT);
		onoff("266MHz capable", reg, PCIX_BRIDGE_ST_266);
		onoff("533MHz capable", reg, PCIX_BRIDGE_ST_533);
	} else {
		printf("    Command register: 0x%04x\n",
		    (reg & 0xffff0000) >> 16);
		onoff("Data Parity Error Recovery", reg,
		    PCIX_CMD_PERR_RECOVER);
		onoff("Enable Relaxed Ordering", reg, PCIX_CMD_RELAXED_ORDER);
		printf("      Maximum Burst Read Count: %u\n",
		    PCIX_CMD_BYTECNT(reg));
		printf("      Maximum Split Transactions: %d\n",
		    pcix_split_trans((reg & PCIX_CMD_SPLTRANS_MASK)
			>> PCIX_CMD_SPLTRANS_SHIFT));
	}
	reg = regs[o2i(capoff+PCIX_STATUS)]; /* Or PCIX_BRIDGE_PRI_STATUS */
	printf("    %sStatus register: 0x%08x\n",
	    isbridge ? "Bridge " : "", reg);
	printf("      Function: %d\n", PCIX_STATUS_FN(reg));
	printf("      Device: %d\n", PCIX_STATUS_DEV(reg));
	printf("      Bus: %d\n", PCIX_STATUS_BUS(reg));
	onoff("64bit device", reg, PCIX_STATUS_64BIT);
	onoff("133MHz capable", reg, PCIX_STATUS_133);
	onoff("Split completion discarded", reg, PCIX_STATUS_SPLDISC);
	onoff("Unexpected split completion", reg, PCIX_STATUS_SPLUNEX);
	if (isbridge != 0) {
		onoff("Split completion overrun", reg, PCIX_BRIDGE_ST_SPLOVRN);
		onoff("Split request delayed", reg, PCIX_BRIDGE_ST_SPLRQDL);
	} else {
		onoff2("Device Complexity", reg, PCIX_STATUS_DEVCPLX,
		    "bridge device", "simple device");
		printf("      Designed max memory read byte count: %d\n",
		    512 << ((reg & PCIX_STATUS_MAXB_MASK)
			>> PCIX_STATUS_MAXB_SHIFT));
		printf("      Designed max outstanding split transaction: %d\n",
		    pcix_split_trans((reg & PCIX_STATUS_MAXST_MASK)
			>> PCIX_STATUS_MAXST_SHIFT));
		printf("      MAX cumulative Read Size: %u\n",
		    8 << ((reg & 0x1c000000) >> PCIX_STATUS_MAXRS_SHIFT));
		onoff("Received split completion error", reg,
		    PCIX_STATUS_SCERR);
	}
	onoff("266MHz capable", reg, PCIX_STATUS_266);
	onoff("533MHz capable", reg, PCIX_STATUS_533);

	if (isbridge == 0)
		return;

	/* Only for bridge */
	for (i = 0; i < 2; i++) {
		reg = regs[o2i(capoff + PCIX_BRIDGE_UP_STCR + (4 * i))];
		printf("    %s split transaction control register: 0x%08x\n",
		    (i == 0) ? "Upstream" : "Downstream", reg);
		printf("      Capacity: %d\n", reg & PCIX_BRIDGE_STCAP);
		printf("      Commitment Limit: %d\n",
		    (reg & PCIX_BRIDGE_STCLIM) >> PCIX_BRIDGE_STCLIM_SHIFT);
	}
}

/* pci_conf_print_ht_slave_cap */
/* pci_conf_print_ht_host_cap */
/* pci_conf_print_ht_switch_cap */
/* pci_conf_print_ht_intr_cap */
/* pci_conf_print_ht_revid_cap */
/* pci_conf_print_ht_unitid_cap */
/* pci_conf_print_ht_extcnf_cap */
/* pci_conf_print_ht_addrmap_cap */
/* pci_conf_print_ht_msimap_cap */

static void
pci_conf_print_ht_msimap_cap(const pcireg_t *regs, int capoff)
{
	pcireg_t val;
	uint32_t lo, hi;

	/*
	 * Print the rest of the command register bits. Others are
	 * printed in pci_conf_print_ht_cap().
	 */
	val = regs[o2i(capoff + PCI_HT_CMD)];
	onoff("Enable", val, PCI_HT_MSI_ENABLED);
	onoff("Fixed", val, PCI_HT_MSI_FIXED);

	lo = regs[o2i(capoff + PCI_HT_MSI_ADDR_LO)];
	hi = regs[o2i(capoff + PCI_HT_MSI_ADDR_HI)];
	printf("    Address Low register: 0x%08x\n", lo);
	printf("    Address high register: 0x%08x\n", hi);
	printf("      Address: 0x%016" PRIx64 "\n",
	    (uint64_t)hi << 32 | (lo & PCI_HT_MSI_ADDR_LO_MASK));
}

/* pci_conf_print_ht_droute_cap */
/* pci_conf_print_ht_vcset_cap */
/* pci_conf_print_ht_retry_cap */
/* pci_conf_print_ht_x86enc_cap */
/* pci_conf_print_ht_gen3_cap */
/* pci_conf_print_ht_fle_cap */
/* pci_conf_print_ht_pm_cap */
/* pci_conf_print_ht_hnc_cap */

static const struct ht_types {
	pcireg_t cap;
	const char *name;
	void (*printfunc)(const pcireg_t *, int);
} ht_captab[] = {
	{PCI_HT_CAP_SLAVE,	"Slave or Primary Interface", NULL },
	{PCI_HT_CAP_HOST,	"Host or Secondary Interface", NULL },
	{PCI_HT_CAP_SWITCH,	"Switch", NULL },
	{PCI_HT_CAP_INTERRUPT,	"Interrupt Discovery and Configuration", NULL},
	{PCI_HT_CAP_REVID,	"Revision ID",	NULL },
	{PCI_HT_CAP_UNITID_CLUMP, "UnitID Clumping",	NULL },
	{PCI_HT_CAP_EXTCNFSPACE, "Extended Configuration Space Access",	NULL },
	{PCI_HT_CAP_ADDRMAP,	"Address Mapping",	NULL },
	{PCI_HT_CAP_MSIMAP,	"MSI Mapping",	pci_conf_print_ht_msimap_cap },
	{PCI_HT_CAP_DIRECTROUTE, "Direct Route",	NULL },
	{PCI_HT_CAP_VCSET,	"VCSet",	NULL },
	{PCI_HT_CAP_RETRYMODE,	"Retry Mode",	NULL },
	{PCI_HT_CAP_X86ENCODE,	"X86 Encoding",	NULL },
	{PCI_HT_CAP_GEN3,	"Gen3",	NULL },
	{PCI_HT_CAP_FLE,	"Function-Level Extension",	NULL },
	{PCI_HT_CAP_PM,		"Power Management",	NULL },
	{PCI_HT_CAP_HIGHNODECNT, "High Node Count",	NULL },
};

static void
pci_conf_print_ht_cap(const pcireg_t *regs, int capoff)
{
	pcireg_t val, foundcap;
	unsigned int off;

	val = regs[o2i(capoff + PCI_HT_CMD)];

	printf("\n  HyperTransport Capability Register at 0x%02x\n", capoff);

	printf("    Command register: 0x%04x\n", val >> 16);
	foundcap = PCI_HT_CAP(val);
	for (off = 0; off < __arraycount(ht_captab); off++) {
		if (ht_captab[off].cap == foundcap)
			break;
	}
	printf("      Capability Type: 0x%02x ", foundcap);
	if (off >= __arraycount(ht_captab)) {
		printf("(unknown)\n");
		return;
	}
	printf("(%s)\n", ht_captab[off].name);
	if (ht_captab[off].printfunc != NULL)
		ht_captab[off].printfunc(regs, capoff);
}

static void
pci_conf_print_vendspec_cap(const pcireg_t *regs, int capoff)
{
	uint16_t caps;

	caps = regs[o2i(capoff)] >> PCI_VENDORSPECIFIC_SHIFT;

	printf("\n  PCI Vendor Specific Capabilities Register\n");
	printf("    Capabilities length: 0x%02x\n", caps & 0xff);
}

static void
pci_conf_print_debugport_cap(const pcireg_t *regs, int capoff)
{
	pcireg_t val;

	val = regs[o2i(capoff + PCI_DEBUG_BASER)];

	printf("\n  Debugport Capability Register\n");
	printf("    Debug base Register: 0x%04x\n",
	    val >> PCI_DEBUG_BASER_SHIFT);
	printf("      port offset: 0x%04x\n",
	    (val & PCI_DEBUG_PORTOFF_MASK) >> PCI_DEBUG_PORTOFF_SHIFT);
	printf("      BAR number: %u\n",
	    (val & PCI_DEBUG_BARNUM_MASK) >> PCI_DEBUG_BARNUM_SHIFT);
}

/* XXX pci_conf_print_cpci_rsrcctl_cap */
/* XXX pci_conf_print_hotplug_cap */

static void
pci_conf_print_subsystem_cap(const pcireg_t *regs, int capoff)
{
	pcireg_t reg;

	reg = regs[o2i(capoff + PCI_CAP_SUBSYS_ID)];

	printf("\n  Subsystem ID Capability Register\n");
	printf("    Subsystem ID: 0x%08x\n", reg);
}

/* XXX pci_conf_print_agp8_cap */
static void
pci_conf_print_secure_cap(const pcireg_t *regs, int capoff)
{
	pcireg_t reg, reg2, val;
	bool havemisc1;

	printf("\n  Secure Capability Register\n");
	reg = regs[o2i(capoff + PCI_SECURE_CAP)];
	printf("    Capability Register: 0x%04x\n", reg >> 16);
	val = PCIREG_SHIFTOUT(reg, PCI_SECURE_CAP_TYPE);
	printf("      Capability block type: ");
	/* I know IOMMU Only */
	if (val == PCI_SECURE_CAP_TYPE_IOMMU)
		printf("IOMMU\n");
	else {
		printf("0x%x(unknown)\n", val);
		return;
	}

	val = PCIREG_SHIFTOUT(reg, PCI_SECURE_CAP_REV);
	printf("      Capability revision: 0x%02x ", val);
	if (val == PCI_SECURE_CAP_REV_IOMMU)
		printf("(IOMMU)\n");
	else {
		printf("(unknown)\n");
		return;
	}
	onoff("IOTLB support", reg, PCI_SECURE_CAP_IOTLBSUP);
	onoff("HyperTransport tunnel translation support", reg,
	    PCI_SECURE_CAP_HTTUNNEL);
	onoff("Not present table entries cached", reg, PCI_SECURE_CAP_NPCACHE);
	onoff("IOMMU Extended Feature Register support", reg,
	    PCI_SECURE_CAP_EFRSUP);
	onoff("IOMMU Miscellaneous Information Register 1", reg,
	    PCI_SECURE_CAP_EXT);
	havemisc1 = reg & PCI_SECURE_CAP_EXT;

	reg = regs[o2i(capoff + PCI_SECURE_IOMMU_BAL)];
	printf("    Base Address Low Register: 0x%08x\n", reg);
	onoff("Enable", reg, PCI_SECURE_IOMMU_BAL_EN);
	reg2 = regs[o2i(capoff + PCI_SECURE_IOMMU_BAH)];
	printf("    Base Address High Register: 0x%08x\n", reg2);
	printf("      Base Address: 0x%016" PRIx64 "\n",
	    ((uint64_t)reg2 << 32)
	    | (reg & (PCI_SECURE_IOMMU_BAL_H | PCI_SECURE_IOMMU_BAL_L)));

	reg = regs[o2i(capoff + PCI_SECURE_IOMMU_RANGE)];
	printf("    IOMMU Range Register: 0x%08x\n", reg);
	printf("      HyperTransport UnitID: 0x%02x\n",
	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_RANGE_UNITID));
	onoff("Range valid", reg, PCI_SECURE_IOMMU_RANGE_RNGVALID);
	printf("      Device range bus number: 0x%02x\n",
	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_RANGE_BUSNUM));
	printf("      First device: 0x%04x\n",
	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_RANGE_FIRSTDEV));
	printf("      Last device: 0x%04x\n",
	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_RANGE_LASTDEV));

	reg = regs[o2i(capoff + PCI_SECURE_IOMMU_MISC0)];
	printf("    Miscellaneous Information Register 0: 0x%08x\n", reg);
	printf("      MSI Message number: 0x%02x\n",
	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_MISC0_MSINUM));
	val = PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_MISC0_GVASIZE);
	printf("      Guest Virtual Address size: ");
	if (val == PCI_SECURE_IOMMU_MISC0_GVASIZE_48B)
		printf("48bits\n");
	else
		printf("0x%x(unknown)\n", val);
	val = PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_MISC0_PASIZE);
	printf("      Physical Address size: %dbits\n", val);
	val = PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_MISC0_VASIZE);
	printf("      Virtual Address size: %dbits\n", val);
	onoff("ATS response address range reserved", reg,
	    PCI_SECURE_IOMMU_MISC0_ATSRESV);
	printf("      Peripheral Page Request MSI Message number: 0x%02x\n",
	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_MISC0_MISNPPR));

	if (!havemisc1)
		return;

	reg = regs[o2i(capoff + PCI_SECURE_IOMMU_MISC1)];
	printf("    Miscellaneous Information Register 1: 0x%08x\n", reg);
	printf("      MSI Message number (GA): 0x%02x\n",
	    PCIREG_SHIFTOUT(reg, PCI_SECURE_IOMMU_MISC1_MSINUM));
}

static void
pci_print_pcie_L0s_latency(uint32_t val)
{

	switch (val) {
	case 0x0:
		printf("Less than 64ns\n");
		break;
	case 0x1:
	case 0x2:
	case 0x3:
		printf("%dns to less than %dns\n", 32 << val, 32 << (val + 1));
		break;
	case 0x4:
		printf("512ns to less than 1us\n");
		break;
	case 0x5:
		printf("1us to less than 2us\n");
		break;
	case 0x6:
		printf("2us - 4us\n");
		break;
	case 0x7:
		printf("More than 4us\n");
		break;
	}
}

static void
pci_print_pcie_L1_latency(uint32_t val)
{

	switch (val) {
	case 0x0:
		printf("Less than 1us\n");
		break;
	case 0x6:
		printf("32us - 64us\n");
		break;
	case 0x7:
		printf("More than 64us\n");
		break;
	default:
		printf("%dus to less than %dus\n", 1 << (val - 1), 1 << val);
		break;
	}
}

static void
pci_print_pcie_compl_timeout(uint32_t val)
{

	switch (val) {
	case 0x0:
		printf("50us to 50ms\n");
		break;
	case 0x5:
		printf("16ms to 55ms\n");
		break;
	case 0x6:
		printf("65ms to 210ms\n");
		break;
	case 0x9:
		printf("260ms to 900ms\n");
		break;
	case 0xa:
		printf("1s to 3.5s\n");
		break;
	default:
		printf("unknown %u value\n", val);
		break;
	}
}

static const char * const pcie_linkspeeds[] = {
	"2.5", "5.0", "8.0", "16.0", "32.0"
};

/*
 * Print link speed. This function is used for the following register bits:
 *   Maximum Link Speed in LCAP
 *   Current Link Speed in LCSR
 *   Target Link Speed in LCSR2
 * All of above bitfield's values start from 1.
 * For LCSR2, 0 is allowed for a device which supports 2.5GT/s only (and
 * this check also works for devices which compliant to versions of the base
 * specification prior to 3.0.
 */
static void
pci_print_pcie_linkspeed(int regnum, pcireg_t val)
{

	if ((regnum == PCIE_LCSR2) && (val == 0))
		printf("2.5GT/s\n");
	else if ((val < 1) || (val > __arraycount(pcie_linkspeeds)))
		printf("unknown value (%u)\n", val);
	else
		printf("%sGT/s\n", pcie_linkspeeds[val - 1]);
}

/*
 * Print link speed "vector".
 * This function is used for the following register bits:
 *   Supported Link Speeds Vector in LCAP2
 *   Lower SKP OS Generation Supported Speed Vector  in LCAP2
 *   Lower SKP OS Reception Supported Speed Vector in LCAP2
 *   Enable Lower SKP OS Generation Vector in LCTL3
 * All of above bitfield's values start from 0.
 */
static void
pci_print_pcie_linkspeedvector(pcireg_t val)
{
	unsigned int i;

	/* Start from 0 */
	for (i = 0; i < 16; i++)
		if (((val >> i) & 0x01) != 0) {
			if (i >= __arraycount(pcie_linkspeeds))
				printf(" unknown vector (0x%x)", 1 << i);
			else
				printf(" %sGT/s", pcie_linkspeeds[i]);
		}
}

static void
pci_print_pcie_link_deemphasis(pcireg_t val)
{
	switch (val) {
	case 0:
		printf("-6dB");
		break;
	case 1:
		printf("-3.5dB");
		break;
	default:
		printf("(reserved value)");
	}
}

static const struct _pcie_link_preset_preshoot_deemphasis {
	const char *preshoot;
	const char *deemphasis;
} pcie_link_preset_preshoot_deemphasis[] = {
	{ "0.0",	"-6.0+-1.5" },	/* P0 */
	{ "0.0",	"-3.5+-1" },	/* P1 */
	{ "0.0",	"-4.4+-1.5" },	/* P2 */
	{ "0.0",	"-2.5+-1" },	/* P3 */
	{ "0.0",	"0.0" },	/* P4 */
	{ "1.9+-1",	"0.0" },	/* P5 */
	{ "2.5+-1",	"0.0" },	/* P6 */
	{ "3.5+-1",	"-6.0+-1.5" },	/* P7 */
	{ "3.5+-1",	"-3.5+-1" },	/* P8 */
	{ "3.5+-1",	"0.0" },	/* P9 */
	{ "0.0",	NULL }		/* P10 */
};

static void
pci_print_pcie_link_preset_preshoot_deemphasis(pcireg_t val)
{
	const char *deemphasis;

	if (val >= __arraycount(pcie_link_preset_preshoot_deemphasis)) {
		/*
		 * This may be printed because the default value of some
		 * register fields is 0b1111.
		 */
		printf("reserved value (0x%x)", val);
		return;
	}

	printf("Preshoot %sdB",
	    pcie_link_preset_preshoot_deemphasis[val].preshoot);
	deemphasis = pcie_link_preset_preshoot_deemphasis[val].deemphasis;

	if (deemphasis != NULL)
		printf(", De-emphasis %sdB", deemphasis);
}

static void
pci_conf_print_pcie_cap(const pcireg_t *regs, int capoff)
{
	pcireg_t reg; /* for each register */
	pcireg_t val; /* for each bitfield */
	bool check_slot = false;
	unsigned int pcie_devtype;
	bool check_upstreamport = false;
	unsigned int pciever;
	unsigned int i;

	printf("\n  PCI Express Capabilities Register\n");
	/* Capability Register */
	reg = regs[o2i(capoff)];
	printf("    Capability register: 0x%04x\n", reg >> 16);
	pciever = (unsigned int)(PCIE_XCAP_VER(reg));
	printf("      Capability version: %u\n", pciever);
	printf("      Device type: ");
	pcie_devtype = PCIE_XCAP_TYPE(reg);
	switch (pcie_devtype) {
	case PCIE_XCAP_TYPE_PCIE_DEV:	/* 0x0 */
		printf("PCI Express Endpoint device\n");
		check_upstreamport = true;
		break;
	case PCIE_XCAP_TYPE_PCI_DEV:	/* 0x1 */
		printf("Legacy PCI Express Endpoint device\n");
		check_upstreamport = true;
		break;
	case PCIE_XCAP_TYPE_RP:		/* 0x4 */
		printf("Root Port of PCI Express Root Complex\n");
		check_slot = true;
		break;
	case PCIE_XCAP_TYPE_UP:		/* 0x5 */
		printf("Upstream Port of PCI Express Switch\n");
		check_upstreamport = true;
		break;
	case PCIE_XCAP_TYPE_DOWN:	/* 0x6 */
		printf("Downstream Port of PCI Express Switch\n");
		check_slot = true;
		break;
	case PCIE_XCAP_TYPE_PCIE2PCI:	/* 0x7 */
		printf("PCI Express to PCI/PCI-X Bridge\n");
		check_upstreamport = true;
		break;
	case PCIE_XCAP_TYPE_PCI2PCIE:	/* 0x8 */
		printf("PCI/PCI-X to PCI Express Bridge\n");
		/* Upstream port is not PCIe */
		check_slot = true;
		break;
	case PCIE_XCAP_TYPE_RCIEP:	/* 0x9 */
		printf("Root Complex Integrated Endpoint\n");
		break;
	case PCIE_XCAP_TYPE_RC_EVNTC:	/* 0xa */
		printf("Root Complex Event Collector\n");
		break;
	default:
		printf("unknown\n");
		break;
	}
	onoff("Slot implemented", reg, PCIE_XCAP_SI);
	printf("      Interrupt Message Number: 0x%02x\n",
	    PCIREG_SHIFTOUT(reg, PCIE_XCAP_IRQ));

	/* Device Capability Register */
	reg = regs[o2i(capoff + PCIE_DCAP)];
	printf("    Device Capabilities Register: 0x%08x\n", reg);
	printf("      Max Payload Size Supported: %u bytes max\n",
	    128 << (unsigned int)(reg & PCIE_DCAP_MAX_PAYLOAD));
	printf("      Phantom Functions Supported: ");
	switch (PCIREG_SHIFTOUT(reg, PCIE_DCAP_PHANTOM_FUNCS)) {
	case 0x0:
		printf("not available\n");
		break;
	case 0x1:
		printf("MSB\n");
		break;
	case 0x2:
		printf("two MSB\n");
		break;
	case 0x3:
		printf("All three bits\n");
		break;
	}
	printf("      Extended Tag Field Supported: %dbit\n",
	    (reg & PCIE_DCAP_EXT_TAG_FIELD) == 0 ? 5 : 8);
	printf("      Endpoint L0 Acceptable Latency: ");
	pci_print_pcie_L0s_latency(PCIREG_SHIFTOUT(reg, PCIE_DCAP_L0S_LATENCY));
	printf("      Endpoint L1 Acceptable Latency: ");
	pci_print_pcie_L1_latency(PCIREG_SHIFTOUT(reg, PCIE_DCAP_L1_LATENCY));
	onoff("Attention Button Present", reg, PCIE_DCAP_ATTN_BUTTON);
	onoff("Attention Indicator Present", reg, PCIE_DCAP_ATTN_IND);
	onoff("Power Indicator Present", reg, PCIE_DCAP_PWR_IND);
	onoff("Role-Based Error Report", reg, PCIE_DCAP_ROLE_ERR_RPT);
	if (check_upstreamport) {
		printf("      Captured Slot Power Limit: ");
		pci_conf_print_pcie_power(
			PCIREG_SHIFTOUT(reg, PCIE_DCAP_SLOT_PWR_LIM_VAL),
			PCIREG_SHIFTOUT(reg, PCIE_DCAP_SLOT_PWR_LIM_SCALE));
	}
	onoff("Function-Level Reset Capability", reg, PCIE_DCAP_FLR);

	/* Device Control Register */
	reg = regs[o2i(capoff + PCIE_DCSR)];
	printf("    Device Control Register: 0x%04x\n", reg & 0xffff);
	onoff("Correctable Error Reporting Enable", reg,
	    PCIE_DCSR_ENA_COR_ERR);
	onoff("Non Fatal Error Reporting Enable", reg, PCIE_DCSR_ENA_NFER);
	onoff("Fatal Error Reporting Enable", reg, PCIE_DCSR_ENA_FER);
	onoff("Unsupported Request Reporting Enable", reg, PCIE_DCSR_ENA_URR);
	onoff("Enable Relaxed Ordering", reg, PCIE_DCSR_ENA_RELAX_ORD);
	printf("      Max Payload Size: %d byte\n",
	    128 << PCIREG_SHIFTOUT(reg, PCIE_DCSR_MAX_PAYLOAD));
	onoff("Extended Tag Field Enable", reg, PCIE_DCSR_EXT_TAG_FIELD);
	onoff("Phantom Functions Enable", reg, PCIE_DCSR_PHANTOM_FUNCS);
	onoff("Aux Power PM Enable", reg, PCIE_DCSR_AUX_POWER_PM);
	onoff("Enable No Snoop", reg, PCIE_DCSR_ENA_NO_SNOOP);
	printf("      Max Read Request Size: %d byte\n",
	    128 << PCIREG_SHIFTOUT(reg, PCIE_DCSR_MAX_READ_REQ));
	if (pcie_devtype == PCIE_XCAP_TYPE_PCIE2PCI)
		onoff("Bridge Config Retry Enable", reg,
		    PCIE_DCSR_BRDG_CFG_RETRY);

	/* Device Status Register */
	reg = regs[o2i(capoff + PCIE_DCSR)];
	printf("    Device Status Register: 0x%04x\n", reg >> 16);
	onoff("Correctable Error Detected", reg, PCIE_DCSR_CED);
	onoff("Non Fatal Error Detected", reg, PCIE_DCSR_NFED);
	onoff("Fatal Error Detected", reg, PCIE_DCSR_FED);
	onoff("Unsupported Request Detected", reg, PCIE_DCSR_URD);
	onoff("Aux Power Detected", reg, PCIE_DCSR_AUX_PWR);
	onoff("Transaction Pending", reg, PCIE_DCSR_TRANSACTION_PND);
	onoff("Emergency Power Reduction Detected", reg, PCIE_DCSR_EMGPWRREDD);

	if (PCIE_HAS_LINKREGS(pcie_devtype)) {
		/* Link Capability Register */
		reg = regs[o2i(capoff + PCIE_LCAP)];
		printf("    Link Capabilities Register: 0x%08x\n", reg);
		printf("      Maximum Link Speed: ");
		pci_print_pcie_linkspeed(PCIE_LCAP, reg & PCIE_LCAP_MAX_SPEED);
		printf("      Maximum Link Width: x%u lanes\n",
		    PCIREG_SHIFTOUT(reg, PCIE_LCAP_MAX_WIDTH));
		printf("      Active State PM Support: ");
		switch (PCIREG_SHIFTOUT(reg, PCIE_LCAP_ASPM)) {
		case 0x0:
			printf("No ASPM support\n");
			break;
		case 0x1:
			printf("L0s supported\n");
			break;
		case 0x2:
			printf("L1 supported\n");
			break;
		case 0x3:
			printf("L0s and L1 supported\n");
			break;
		}
		printf("      L0 Exit Latency: ");
		pci_print_pcie_L0s_latency(PCIREG_SHIFTOUT(reg,PCIE_LCAP_L0S_EXIT));
		printf("      L1 Exit Latency: ");
		pci_print_pcie_L1_latency(PCIREG_SHIFTOUT(reg, PCIE_LCAP_L1_EXIT));
		printf("      Port Number: %u\n",
		    PCIREG_SHIFTOUT(reg, PCIE_LCAP_PORT));
		onoff("Clock Power Management", reg, PCIE_LCAP_CLOCK_PM);
		onoff("Surprise Down Error Report", reg,
		    PCIE_LCAP_SURPRISE_DOWN);
		onoff("Data Link Layer Link Active", reg, PCIE_LCAP_DL_ACTIVE);
		onoff("Link BW Notification Capable", reg,
			PCIE_LCAP_LINK_BW_NOTIFY);
		onoff("ASPM Optionally Compliance", reg,
		    PCIE_LCAP_ASPM_COMPLIANCE);

		/* Link Control Register */
		reg = regs[o2i(capoff + PCIE_LCSR)];
		printf("    Link Control Register: 0x%04x\n", reg & 0xffff);
		printf("      Active State PM Control: ");
		switch (reg & (PCIE_LCSR_ASPM_L1 | PCIE_LCSR_ASPM_L0S)) {
		case 0:
			printf("disabled\n");
			break;
		case 1:
			printf("L0s Entry Enabled\n");
			break;
		case 2:
			printf("L1 Entry Enabled\n");
			break;
		case 3:
			printf("L0s and L1 Entry Enabled\n");
			break;
		}
		onoff2("Read Completion Boundary Control", reg, PCIE_LCSR_RCB,
		    "128bytes", "64bytes");
		onoff("Link Disable", reg, PCIE_LCSR_LINK_DIS);
		onoff("Retrain Link", reg, PCIE_LCSR_RETRAIN);
		onoff("Common Clock Configuration", reg, PCIE_LCSR_COMCLKCFG);
		onoff("Extended Synch", reg, PCIE_LCSR_EXTNDSYNC);
		onoff("Enable Clock Power Management", reg, PCIE_LCSR_ENCLKPM);
		onoff("Hardware Autonomous Width Disable", reg,PCIE_LCSR_HAWD);
		onoff("Link Bandwidth Management Interrupt Enable", reg,
		    PCIE_LCSR_LBMIE);
		onoff("Link Autonomous Bandwidth Interrupt Enable", reg,
		    PCIE_LCSR_LABIE);
		printf("      DRS Signaling Control: ");
		switch (PCIREG_SHIFTOUT(reg, PCIE_LCSR_DRSSGNL)) {
		case 0:
			printf("not reported\n");
			break;
		case 1:
			printf("Interrupt Enabled\n");
			break;
		case 2:
			printf("DRS to FRS Signaling Enabled\n");
			break;
		default:
			printf("reserved\n");
			break;
		}

		/* Link Status Register */
		reg = regs[o2i(capoff + PCIE_LCSR)];
		printf("    Link Status Register: 0x%04x\n", reg >> 16);
		printf("      Negotiated Link Speed: ");
		pci_print_pcie_linkspeed(PCIE_LCSR,
		    PCIREG_SHIFTOUT(reg, PCIE_LCSR_LINKSPEED));
		printf("      Negotiated Link Width: x%u lanes\n",
		    PCIREG_SHIFTOUT(reg, PCIE_LCSR_NLW));
		onoff("Training Error", reg, PCIE_LCSR_LINKTRAIN_ERR);
		onoff("Link Training", reg, PCIE_LCSR_LINKTRAIN);
		onoff("Slot Clock Configuration", reg, PCIE_LCSR_SLOTCLKCFG);
		onoff("Data Link Layer Link Active", reg, PCIE_LCSR_DLACTIVE);
		onoff("Link Bandwidth Management Status", reg,
		    PCIE_LCSR_LINK_BW_MGMT);
		onoff("Link Autonomous Bandwidth Status", reg,
		    PCIE_LCSR_LINK_AUTO_BW);
	}

	if (check_slot == true) {
		pcireg_t slcap;

		/* Slot Capability Register */
		slcap = reg = regs[o2i(capoff + PCIE_SLCAP)];
		printf("    Slot Capability Register: 0x%08x\n", reg);
		onoff("Attention Button Present", reg, PCIE_SLCAP_ABP);
		onoff("Power Controller Present", reg, PCIE_SLCAP_PCP);
		onoff("MRL Sensor Present", reg, PCIE_SLCAP_MSP);
		onoff("Attention Indicator Present", reg, PCIE_SLCAP_AIP);
		onoff("Power Indicator Present", reg, PCIE_SLCAP_PIP);
		onoff("Hot-Plug Surprise", reg, PCIE_SLCAP_HPS);
		onoff("Hot-Plug Capable", reg, PCIE_SLCAP_HPC);
		printf("      Slot Power Limit Value: ");
		pci_conf_print_pcie_power(PCIREG_SHIFTOUT(reg, PCIE_SLCAP_SPLV),
		    PCIREG_SHIFTOUT(reg, PCIE_SLCAP_SPLS));
		onoff("Electromechanical Interlock Present", reg,
		    PCIE_SLCAP_EIP);
		onoff("No Command Completed Support", reg, PCIE_SLCAP_NCCS);
		printf("      Physical Slot Number: %d\n",
		    (unsigned int)(reg & PCIE_SLCAP_PSN) >> 19);

		/* Slot Control Register */
		reg = regs[o2i(capoff + PCIE_SLCSR)];
		printf("    Slot Control Register: 0x%04x\n", reg & 0xffff);
		onoff("Attention Button Pressed Enabled", reg, PCIE_SLCSR_ABE);
		onoff("Power Fault Detected Enabled", reg, PCIE_SLCSR_PFE);
		onoff("MRL Sensor Changed Enabled", reg, PCIE_SLCSR_MSE);
		onoff("Presence Detect Changed Enabled", reg, PCIE_SLCSR_PDE);
		onoff("Command Completed Interrupt Enabled", reg,
		    PCIE_SLCSR_CCE);
		onoff("Hot-Plug Interrupt Enabled", reg, PCIE_SLCSR_HPE);
		/*
		 * For Attention Indicator Control and Power Indicator Control,
		 * it's allowed to be a read only value 0 if corresponding
		 * capability register bit is 0.
		 */
		if (slcap & PCIE_SLCAP_AIP) {
			printf("      Attention Indicator Control: ");
			switch ((reg & PCIE_SLCSR_AIC) >> 6) {
			case 0x0:
				printf("reserved\n");
				break;
			case PCIE_SLCSR_IND_ON:
				printf("on\n");
				break;
			case PCIE_SLCSR_IND_BLINK:
				printf("blink\n");
				break;
			case PCIE_SLCSR_IND_OFF:
				printf("off\n");
				break;
			}
		}
		if (slcap & PCIE_SLCAP_PIP) {
			printf("      Power Indicator Control: ");
			switch ((reg & PCIE_SLCSR_PIC) >> 8) {
			case 0x0:
				printf("reserved\n");
				break;
			case PCIE_SLCSR_IND_ON:
				printf("on\n");
				break;
			case PCIE_SLCSR_IND_BLINK:
				printf("blink\n");
				break;
			case PCIE_SLCSR_IND_OFF:
				printf("off\n");
				break;
			}
		}
		printf("      Power Controller Control: Power %s\n",
		    reg & PCIE_SLCSR_PCC ? "off" : "on");
		onoff("Electromechanical Interlock Control",
		    reg, PCIE_SLCSR_EIC);
		onoff("Data Link Layer State Changed Enable", reg,
		    PCIE_SLCSR_DLLSCE);
		onoff("Auto Slot Power Limit Disable", reg,
		    PCIE_SLCSR_AUTOSPLDIS);

		/* Slot Status Register */
		printf("    Slot Status Register: 0x%04x\n", reg >> 16);
		onoff("Attention Button Pressed", reg, PCIE_SLCSR_ABP);
		onoff("Power Fault Detected", reg, PCIE_SLCSR_PFD);
		onoff("MRL Sensor Changed", reg, PCIE_SLCSR_MSC);
		onoff("Presence Detect Changed", reg, PCIE_SLCSR_PDC);
		onoff("Command Completed", reg, PCIE_SLCSR_CC);
		onoff("MRL Open", reg, PCIE_SLCSR_MS);
		onoff("Card Present in slot", reg, PCIE_SLCSR_PDS);
		onoff("Electromechanical Interlock engaged", reg,
		    PCIE_SLCSR_EIS);
		onoff("Data Link Layer State Changed", reg, PCIE_SLCSR_LACS);
	}

	if (PCIE_HAS_ROOTREGS(pcie_devtype)) {
		/* Root Control Register */
		reg = regs[o2i(capoff + PCIE_RCR)];
		printf("    Root Control Register: 0x%04x\n", reg & 0xffff);
		onoff("SERR on Correctable Error Enable", reg,
		    PCIE_RCR_SERR_CER);
		onoff("SERR on Non-Fatal Error Enable", reg,
		    PCIE_RCR_SERR_NFER);
		onoff("SERR on Fatal Error Enable", reg, PCIE_RCR_SERR_FER);
		onoff("PME Interrupt Enable", reg, PCIE_RCR_PME_IE);
		onoff("CRS Software Visibility Enable", reg, PCIE_RCR_CRS_SVE);

		/* Root Capability Register */
		printf("    Root Capability Register: 0x%04x\n",
		    reg >> 16);
		onoff("CRS Software Visibility", reg, PCIE_RCR_CRS_SV);

		/* Root Status Register */
		reg = regs[o2i(capoff + PCIE_RSR)];
		printf("    Root Status Register: 0x%08x\n", reg);
		printf("      PME Requester ID: 0x%04x\n",
		    (unsigned int)(reg & PCIE_RSR_PME_REQESTER));
		onoff("PME was asserted", reg, PCIE_RSR_PME_STAT);
		onoff("another PME is pending", reg, PCIE_RSR_PME_PEND);
	}

	/* PCIe DW9 to DW14 is for PCIe 2.0 and newer */
	if (pciever < 2)
		return;

	/* Device Capabilities 2 */
	reg = regs[o2i(capoff + PCIE_DCAP2)];
	printf("    Device Capabilities 2: 0x%08x\n", reg);
	printf("      Completion Timeout Ranges Supported: ");
	val = reg & PCIE_DCAP2_COMPT_RANGE;
	switch (val) {
	case 0:
		printf("not supported\n");
		break;
	default:
		for (i = 0; i <= 3; i++) {
			if (((val >> i) & 0x01) != 0)
				printf("%c", 'A' + i);
		}
		printf("\n");
	}
	onoff("Completion Timeout Disable Supported", reg,
	    PCIE_DCAP2_COMPT_DIS);
	onoff("ARI Forwarding Supported", reg, PCIE_DCAP2_ARI_FWD);
	onoff("AtomicOp Routing Supported", reg, PCIE_DCAP2_ATOM_ROUT);
	onoff("32bit AtomicOp Completer Supported", reg, PCIE_DCAP2_32ATOM);
	onoff("64bit AtomicOp Completer Supported", reg, PCIE_DCAP2_64ATOM);
	onoff("128-bit CAS Completer Supported", reg, PCIE_DCAP2_128CAS);
	onoff("No RO-enabled PR-PR passing", reg, PCIE_DCAP2_NO_ROPR_PASS);
	onoff("LTR Mechanism Supported", reg, PCIE_DCAP2_LTR_MEC);
	printf("      TPH Completer Supported: ");
	switch (PCIREG_SHIFTOUT(reg, PCIE_DCAP2_TPH_COMP)) {
	case 0:
		printf("Not supported\n");
		break;
	case 1:
		printf("TPH\n");
		break;
	case 3:
		printf("TPH and Extended TPH\n");
		break;
	default:
		printf("(reserved value)\n");
		break;
	}
	printf("      LN System CLS: ");
	switch (PCIREG_SHIFTOUT(reg, PCIE_DCAP2_LNSYSCLS)) {
	case 0x0:
		printf("Not supported or not in effect\n");
		break;
	case 0x1:
		printf("64byte cachelines in effect\n");
		break;
	case 0x2:
		printf("128byte cachelines in effect\n");
		break;
	case 0x3:
		printf("Reserved\n");
		break;
	}
	onoff("10-bit Tag Completer Supported", reg, PCIE_DCAP2_TBT_COMP);
	onoff("10-bit Tag Requester Supported", reg, PCIE_DCAP2_TBT_REQ);
	printf("      OBFF Supported: ");
	switch (PCIREG_SHIFTOUT(reg, PCIE_DCAP2_OBFF)) {
	case 0x0:
		printf("Not supported\n");
		break;
	case 0x1:
		printf("Message only\n");
		break;
	case 0x2:
		printf("WAKE# only\n");
		break;
	case 0x3:
		printf("Both\n");
		break;
	}
	onoff("Extended Fmt Field Supported", reg, PCIE_DCAP2_EXTFMT_FLD);
	onoff("End-End TLP Prefix Supported", reg, PCIE_DCAP2_EETLP_PREF);
	val = PCIREG_SHIFTOUT(reg, PCIE_DCAP2_MAX_EETLP);
	printf("      Max End-End TLP Prefixes: %u\n", (val == 0) ? 4 : val);
	printf("      Emergency Power Reduction Supported: ");
	switch (PCIREG_SHIFTOUT(reg, PCIE_DCAP2_EMGPWRRED)) {
	case 0x0:
		printf("Not supported\n");
		break;
	case 0x1:
		printf("Device Specific mechanism\n");
		break;
	case 0x2:
		printf("Form Factor spec or Device Specific mechanism\n");
		break;
	case 0x3:
		printf("Reserved\n");
		break;
	}
	onoff("Emergency Power Reduction Initialization Required", reg,
	    PCIE_DCAP2_EMGPWRRED_INI);
	onoff("FRS Supported", reg, PCIE_DCAP2_FRS);

	/* Device Control 2 */
	reg = regs[o2i(capoff + PCIE_DCSR2)];
	printf("    Device Control 2: 0x%04x\n", reg & 0xffff);
	printf("      Completion Timeout Value: ");
	pci_print_pcie_compl_timeout(reg & PCIE_DCSR2_COMPT_VAL);
	onoff("Completion Timeout Disabled", reg, PCIE_DCSR2_COMPT_DIS);
	onoff("ARI Forwarding Enabled", reg, PCIE_DCSR2_ARI_FWD);
	onoff("AtomicOp Requester Enabled", reg, PCIE_DCSR2_ATOM_REQ);
	onoff("AtomicOp Egress Blocking", reg, PCIE_DCSR2_ATOM_EBLK);
	onoff("IDO Request Enabled", reg, PCIE_DCSR2_IDO_REQ);
	onoff("IDO Completion Enabled", reg, PCIE_DCSR2_IDO_COMP);
	onoff("LTR Mechanism Enabled", reg, PCIE_DCSR2_LTR_MEC);
	onoff("Emergency Power Reduction Request", reg,
	    PCIE_DCSR2_EMGPWRRED_REQ);
	onoff("10-bit Tag Requester Enabled", reg, PCIE_DCSR2_TBT_REQ);
	printf("      OBFF: ");
	switch (PCIREG_SHIFTOUT(reg, PCIE_DCSR2_OBFF_EN)) {
	case 0x0:
		printf("Disabled\n");
		break;
	case 0x1:
		printf("Enabled with Message Signaling Variation A\n");
		break;
	case 0x2:
		printf("Enabled with Message Signaling Variation B\n");
		break;
	case 0x3:
		printf("Enabled using WAKE# signaling\n");
		break;
	}
	onoff("End-End TLP Prefix Blocking on", reg, PCIE_DCSR2_EETLP);

	if (PCIE_HAS_LINKREGS(pcie_devtype)) {
		bool drs_supported = false;

		/* Link Capability 2 */
		reg = regs[o2i(capoff + PCIE_LCAP2)];
		/* If the vector is 0, LCAP2 is not implemented */
		if ((reg & PCIE_LCAP2_SUP_LNKSV) != 0) {
			printf("    Link Capabilities 2: 0x%08x\n", reg);
			printf("      Supported Link Speeds Vector:");
			pci_print_pcie_linkspeedvector(
				PCIREG_SHIFTOUT(reg, PCIE_LCAP2_SUP_LNKSV));
			printf("\n");
			onoff("Crosslink Supported", reg, PCIE_LCAP2_CROSSLNK);
			printf("      "
			    "Lower SKP OS Generation Supported Speed Vector:");
			pci_print_pcie_linkspeedvector(
				PCIREG_SHIFTOUT(reg, PCIE_LCAP2_LOWSKPOS_GENSUPPSV));
			printf("\n");
			printf("      "
			    "Lower SKP OS Reception Supported Speed Vector:");
			pci_print_pcie_linkspeedvector(
				PCIREG_SHIFTOUT(reg, PCIE_LCAP2_LOWSKPOS_RECSUPPSV));
			printf("\n");
			onoff("Retimer Presence Detect Supported", reg,
			    PCIE_LCAP2_RETIMERPD);
			onoff("DRS Supported", reg, PCIE_LCAP2_DRS);
			drs_supported = (reg & PCIE_LCAP2_DRS) ? true : false;
		}

		/* Link Control 2 */
		reg = regs[o2i(capoff + PCIE_LCSR2)];
		/* If the vector is 0, LCAP2 is not implemented */
		printf("    Link Control 2: 0x%04x\n", reg & 0xffff);
		printf("      Target Link Speed: ");
		pci_print_pcie_linkspeed(PCIE_LCSR2,
		    PCIREG_SHIFTOUT(reg, PCIE_LCSR2_TGT_LSPEED));
		onoff("Enter Compliance Enabled", reg, PCIE_LCSR2_ENT_COMPL);
		onoff("HW Autonomous Speed Disabled", reg,
		    PCIE_LCSR2_HW_AS_DIS);
		printf("      Selectable De-emphasis: ");
		pci_print_pcie_link_deemphasis(
			PCIREG_SHIFTOUT(reg, PCIE_LCSR2_SEL_DEEMP));
		printf("\n");
		printf("      Transmit Margin: %u\n",
		    PCIREG_SHIFTOUT(reg,  PCIE_LCSR2_TX_MARGIN));
		onoff("Enter Modified Compliance", reg, PCIE_LCSR2_EN_MCOMP);
		onoff("Compliance SOS", reg, PCIE_LCSR2_COMP_SOS);
		printf("      Compliance Preset/De-emphasis: ");
		pci_print_pcie_link_preset_preshoot_deemphasis(
			PCIREG_SHIFTOUT(reg, PCIE_LCSR2_COMP_DEEMP));
		printf("\n");

		/* Link Status 2 */
		printf("    Link Status 2: 0x%04x\n", (reg >> 16) & 0xffff);
		printf("      Current De-emphasis Level: ");
		pci_print_pcie_link_deemphasis(
			PCIREG_SHIFTOUT(reg, PCIE_LCSR2_DEEMP_LVL));
		printf("\n");
		onoff("Equalization Complete", reg, PCIE_LCSR2_EQ_COMPL);
		onoff("Equalization Phase 1 Successful", reg,
		    PCIE_LCSR2_EQP1_SUC);
		onoff("Equalization Phase 2 Successful", reg,
		    PCIE_LCSR2_EQP2_SUC);
		onoff("Equalization Phase 3 Successful", reg,
		    PCIE_LCSR2_EQP3_SUC);
		onoff("Link Equalization Request", reg, PCIE_LCSR2_LNKEQ_REQ);
		onoff("Retimer Presence Detected", reg, PCIE_LCSR2_RETIMERPD);
		if (drs_supported) {
			printf("      Downstream Component Presence: ");
			switch (PCIREG_SHIFTOUT(reg, PCIE_LCSR2_DSCOMPN)) {
			case PCIE_DSCOMPN_DOWN_NOTDETERM:
				printf("Link Down - Presence Not"
				    " Determined\n");
				break;
			case PCIE_DSCOMPN_DOWN_NOTPRES:
				printf("Link Down - Component Not Present\n");
				break;
			case PCIE_DSCOMPN_DOWN_PRES:
				printf("Link Down - Component Present\n");
				break;
			case PCIE_DSCOMPN_UP_PRES:
				printf("Link Up - Component Present\n");
				break;
			case PCIE_DSCOMPN_UP_PRES_DRS:
				printf("Link Up - Component Present and DRS"
				    " received\n");
				break;
			default:
				printf("reserved\n");
				break;
			}
			onoff("DRS Message Received", reg, PCIE_LCSR2_DRSRCV);
		}
	}

	/* Slot Capability 2 */
	/* Slot Control 2 */
	/* Slot Status 2 */
}

static void
pci_conf_print_msix_cap(const pcireg_t *regs, int capoff)
{
	pcireg_t reg;

	printf("\n  MSI-X Capability Register\n");

	reg = regs[o2i(capoff + PCI_MSIX_CTL)];
	printf("    Message Control register: 0x%04x\n",
	    (reg >> 16) & 0xff);
	printf("      Table Size: %d\n", PCI_MSIX_CTL_TBLSIZE(reg));
	onoff("Function Mask", reg, PCI_MSIX_CTL_FUNCMASK);
	onoff("MSI-X Enable", reg, PCI_MSIX_CTL_ENABLE);
	reg = regs[o2i(capoff + PCI_MSIX_TBLOFFSET)];
	printf("    Table offset register: 0x%08x\n", reg);
	printf("      Table offset: 0x%08x\n",
	    (pcireg_t)(reg & PCI_MSIX_TBLOFFSET_MASK));
	printf("      BIR: 0x%x\n", (pcireg_t)(reg & PCI_MSIX_TBLBIR_MASK));
	reg = regs[o2i(capoff + PCI_MSIX_PBAOFFSET)];
	printf("    Pending bit array register: 0x%08x\n", reg);
	printf("      Pending bit array offset: 0x%08x\n",
	    (pcireg_t)(reg & PCI_MSIX_PBAOFFSET_MASK));
	printf("      BIR: 0x%x\n", (pcireg_t)(reg & PCI_MSIX_PBABIR_MASK));
}

static void
pci_conf_print_sata_cap(const pcireg_t *regs, int capoff)
{
	pcireg_t reg;

	printf("\n  Serial ATA Capability Register\n");

	reg = regs[o2i(capoff + PCI_SATA_REV)];
	printf("    Revision register: 0x%04x\n", (reg >> 16) & 0xff);
	printf("      Revision: %u.%u\n",
	    PCIREG_SHIFTOUT(reg, PCI_SATA_REV_MAJOR),
	    PCIREG_SHIFTOUT(reg, PCI_SATA_REV_MINOR));

	reg = regs[o2i(capoff + PCI_SATA_BAR)];

	printf("    BAR Register: 0x%08x\n", reg);
	printf("      Register location: ");
	if ((reg & PCI_SATA_BAR_SPEC) == PCI_SATA_BAR_INCONF)
		printf("in config space\n");
	else {
		printf("BAR %d\n", (int)PCI_SATA_BAR_NUM(reg));
		printf("      BAR offset: 0x%08x\n",
		    PCIREG_SHIFTOUT(reg, PCI_SATA_BAR_OFFSET) * 4);
	}
}

static void
pci_conf_print_pciaf_cap(const pcireg_t *regs, int capoff)
{
	pcireg_t reg;

	printf("\n  Advanced Features Capability Register\n");

	reg = regs[o2i(capoff + PCI_AFCAPR)];
	printf("    AF Capabilities register: 0x%02x\n", (reg >> 24) & 0xff);
	printf("    AF Structure Length: 0x%02x\n",
	    PCIREG_SHIFTOUT(reg, PCI_AF_LENGTH));
	onoff("Transaction Pending", reg, PCI_AF_TP_CAP);
	onoff("Function Level Reset", reg, PCI_AF_FLR_CAP);
	reg = regs[o2i(capoff + PCI_AFCSR)];
	printf("    AF Control register: 0x%02x\n", reg & 0xff);
	/*
	 * Only PCI_AFCR_INITIATE_FLR is a member of the AF control register
	 * and it's always 0 on read
	 */
	printf("    AF Status register: 0x%02x\n", (reg >> 8) & 0xff);
	onoff("Transaction Pending", reg, PCI_AFSR_TP);
}

static void
pci_conf_print_ea_cap_prop(unsigned int prop)
{

	switch (prop) {
	case PCI_EA_PROP_MEM_NONPREF:
		printf("Memory Space, Non-Prefetchable\n");
		break;
	case PCI_EA_PROP_MEM_PREF:
		printf("Memory Space, Prefetchable\n");
		break;
	case PCI_EA_PROP_IO:
		printf("I/O Space\n");
		break;
	case PCI_EA_PROP_VF_MEM_NONPREF:
		printf("Resorce for VF use, Memory Space, Non-Prefetchable\n");
		break;
	case PCI_EA_PROP_VF_MEM_PREF:
		printf("Resorce for VF use, Memory Space, Prefetch\n");
		break;
	case PCI_EA_PROP_BB_MEM_NONPREF:
		printf("Behind the Bridge, Memory Space, Non-Pref\n");
		break;
	case PCI_EA_PROP_BB_MEM_PREF:
		printf("Behind the Bridge, Memory Space. Prefetchable\n");
		break;
	case PCI_EA_PROP_BB_IO:
		printf("Behind Bridge, I/O Space\n");
		break;
	case PCI_EA_PROP_MEM_UNAVAIL:
		printf("Memory Space Unavailable\n");
		break;
	case PCI_EA_PROP_IO_UNAVAIL:
		printf("IO Space Unavailable\n");
		break;
	case PCI_EA_PROP_UNAVAIL:
		printf("Entry Unavailable for use\n");
		break;
	default:
		printf("Reserved\n");
		break;
	}
}

static void
pci_conf_print_ea_cap(const pcireg_t *regs, int capoff)
{
	pcireg_t reg, reg2;
	unsigned int entries, entoff, i;

	printf("\n  Enhanced Allocation Capability Register\n");

	reg = regs[o2i(capoff + PCI_EA_CAP1)];
	printf("    EA Num Entries register: 0x%04x\n", reg >> 16);
	entries = PCIREG_SHIFTOUT(reg, PCI_EA_CAP1_NUMENTRIES);
	printf("      EA Num Entries: %u\n", entries);

	/* Type 1 only */
	if (PCI_HDRTYPE_TYPE(regs[o2i(PCI_BHLC_REG)]) == PCI_HDRTYPE_PPB) {
		reg = regs[o2i(capoff + PCI_EA_CAP2)];
		printf("    EA Capability Second register: 0x%08x\n", reg);
		printf("      Fixed Secondary Bus Number: %hhu\n",
		    (uint8_t)PCIREG_SHIFTOUT(reg, PCI_EA_CAP2_SECONDARY));
		printf("      Fixed Subordinate Bus Number: %hhu\n",
		    (uint8_t)PCIREG_SHIFTOUT(reg, PCI_EA_CAP2_SUBORDINATE));
		entoff = capoff + 8;
	} else
		entoff = capoff + 4;

	for (i = 0; i < entries; i++) {
		uint64_t base, offset;
		bool baseis64, offsetis64;
		unsigned int bei, entry_size;

		printf("    Entry %u:\n", i);
		/* The first DW */
		reg = regs[o2i(entoff)];
		printf("      The first register: 0x%08x\n", reg);
		entry_size = PCIREG_SHIFTOUT(reg, PCI_EA_ES);
		printf("        Entry size: %u\n", entry_size);
		printf("        BAR Equivalent Indicator: ");
		bei = PCIREG_SHIFTOUT(reg, PCI_EA_BEI);
		switch (bei) {
		case PCI_EA_BEI_BAR0:
		case PCI_EA_BEI_BAR1:
		case PCI_EA_BEI_BAR2:
		case PCI_EA_BEI_BAR3:
		case PCI_EA_BEI_BAR4:
		case PCI_EA_BEI_BAR5:
			printf("BAR %u\n", bei - PCI_EA_BEI_BAR0);
			break;
		case PCI_EA_BEI_BEHIND:
			printf("Behind the function\n");
			break;
		case PCI_EA_BEI_NOTIND:
			printf("Not Indicated\n");
			break;
		case PCI_EA_BEI_EXPROM:
			printf("Expansion ROM\n");
			break;
		case PCI_EA_BEI_VFBAR0:
		case PCI_EA_BEI_VFBAR1:
		case PCI_EA_BEI_VFBAR2:
		case PCI_EA_BEI_VFBAR3:
		case PCI_EA_BEI_VFBAR4:
		case PCI_EA_BEI_VFBAR5:
			printf("VF BAR %u\n", bei - PCI_EA_BEI_VFBAR0);
			break;
		case PCI_EA_BEI_RESERVED:
		default:
			printf("Reserved\n");
			break;
		}

		printf("      Primary Properties: ");
		pci_conf_print_ea_cap_prop(PCIREG_SHIFTOUT(reg, PCI_EA_PP));
		printf("      Secondary Properties: ");
		pci_conf_print_ea_cap_prop(PCIREG_SHIFTOUT(reg, PCI_EA_SP));
		onoff("Writable", reg, PCI_EA_W);
		onoff("Enable for this entry", reg, PCI_EA_E);

		if (entry_size == 0) {
			entoff += 4;
			continue;
		}

		/* Base addr */
		reg = regs[o2i(entoff + 4)];
		base = reg & PCI_EA_LOWMASK;
		baseis64 = reg & PCI_EA_BASEMAXOFFSET_64BIT;
		printf("      Base Address Register Low: 0x%08x\n", reg);
		if (baseis64) {
			/* 64bit */
			reg2 = regs[o2i(entoff + 12)];
			printf("      Base Address Register high: 0x%08x\n",
			    reg2);
			base |= (uint64_t)reg2 << 32;
		}

		/* Offset addr */
		reg = regs[o2i(entoff + 8)];
		offset = reg & PCI_EA_LOWMASK;
		offsetis64 = reg & PCI_EA_BASEMAXOFFSET_64BIT;
		printf("      Max Offset Register Low: 0x%08x\n", reg);
		if (offsetis64) {
			/* 64bit */
			reg2 = regs[o2i(entoff + (baseis64 ? 16 : 12))];
			printf("      Max Offset Register high: 0x%08x\n",
			    reg2);
			offset |= (uint64_t)reg2 << 32;
		}

		printf("        range: 0x%016" PRIx64 "-0x%016" PRIx64
			    "\n", base, base + offset);

		entoff += 4 + (4 * entry_size);
	}
}

/* XXX pci_conf_print_fpb_cap */

static struct {
	pcireg_t cap;
	const char *name;
	void (*printfunc)(const pcireg_t *, int);
} pci_captab[] = {
	{ PCI_CAP_RESERVED0,	"reserved",	NULL },
	{ PCI_CAP_PWRMGMT,	"Power Management", pci_conf_print_pcipm_cap },
	{ PCI_CAP_AGP,		"AGP",		pci_conf_print_agp_cap },
	{ PCI_CAP_VPD,		"VPD",		NULL },
	{ PCI_CAP_SLOTID,	"SlotID",	NULL },
	{ PCI_CAP_MSI,		"MSI",		pci_conf_print_msi_cap },
	{ PCI_CAP_CPCI_HOTSWAP,	"CompactPCI Hot-swapping", NULL },
	{ PCI_CAP_PCIX,		"PCI-X",	pci_conf_print_pcix_cap },
	{ PCI_CAP_LDT,		"HyperTransport", pci_conf_print_ht_cap },
	{ PCI_CAP_VENDSPEC,	"Vendor-specific",
	  pci_conf_print_vendspec_cap },
	{ PCI_CAP_DEBUGPORT,	"Debug Port",	pci_conf_print_debugport_cap },
	{ PCI_CAP_CPCI_RSRCCTL, "CompactPCI Resource Control", NULL },
	{ PCI_CAP_HOTPLUG,	"Hot-Plug",	NULL },
	{ PCI_CAP_SUBVENDOR,	"Subsystem vendor ID",
	  pci_conf_print_subsystem_cap },
	{ PCI_CAP_AGP8,		"AGP 8x",	NULL },
	{ PCI_CAP_SECURE,	"Secure Device", pci_conf_print_secure_cap },
	{ PCI_CAP_PCIEXPRESS,	"PCI Express",	pci_conf_print_pcie_cap },
	{ PCI_CAP_MSIX,		"MSI-X",	pci_conf_print_msix_cap },
	{ PCI_CAP_SATA,		"SATA",		pci_conf_print_sata_cap },
	{ PCI_CAP_PCIAF,	"Advanced Features", pci_conf_print_pciaf_cap},
	{ PCI_CAP_EA,		"Enhanced Allocation", pci_conf_print_ea_cap },
	{ PCI_CAP_FPB,		"Flattening Portal Bridge", NULL }
};

static int
pci_conf_find_cap(const pcireg_t *regs, unsigned int capid, int *offsetp)
{
	pcireg_t rval;
	unsigned int capptr;
	int off;

	if (!(regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT))
		return 0;

	/* Determine the Capability List Pointer register to start with. */
	switch (PCI_HDRTYPE_TYPE(regs[o2i(PCI_BHLC_REG)])) {
	case 0:	/* standard device header */
	case 1: /* PCI-PCI bridge header */
		capptr = PCI_CAPLISTPTR_REG;
		break;
	case 2:	/* PCI-CardBus Bridge header */
		capptr = PCI_CARDBUS_CAPLISTPTR_REG;
		break;
	default:
		return 0;
	}

	for (off = PCI_CAPLIST_PTR(regs[o2i(capptr)]);
	     off != 0; off = PCI_CAPLIST_NEXT(rval)) {
		rval = regs[o2i(off)];
		if (capid == PCI_CAPLIST_CAP(rval)) {
			if (offsetp != NULL)
				*offsetp = off;
			return 1;
		}
	}
	return 0;
}

static void
pci_conf_print_caplist(
#ifdef _KERNEL
    pci_chipset_tag_t pc, pcitag_t tag,
#endif
    const pcireg_t *regs, int capoff)
{
	int off;
	pcireg_t foundcap;
	pcireg_t rval;
	bool foundtable[__arraycount(pci_captab)];
	unsigned int i;

	/* Clear table */
	for (i = 0; i < __arraycount(pci_captab); i++)
		foundtable[i] = false;

	/* Print capability register's offset and the type first */
	for (off = PCI_CAPLIST_PTR(regs[o2i(capoff)]);
	     off != 0; off = PCI_CAPLIST_NEXT(regs[o2i(off)])) {
		rval = regs[o2i(off)];
		printf("  Capability register at 0x%02x\n", off);

		printf("    type: 0x%02x (", PCI_CAPLIST_CAP(rval));
		foundcap = PCI_CAPLIST_CAP(rval);
		if (foundcap < __arraycount(pci_captab)) {
			printf("%s)\n", pci_captab[foundcap].name);
			/* Mark as found */
			foundtable[foundcap] = true;
		} else
			printf("unknown)\n");
	}

	/*
	 * And then, print the detail of each capability registers
	 * in capability value's order.
	 */
	for (i = 0; i < __arraycount(pci_captab); i++) {
		if (foundtable[i] == false)
			continue;

		/*
		 * The type was found. Search capability list again and
		 * print all capabilities that the capability type is
		 * the same. This is required because some capabilities
		 * appear multiple times (e.g. HyperTransport capability).
		 */
		for (off = PCI_CAPLIST_PTR(regs[o2i(capoff)]);
		     off != 0; off = PCI_CAPLIST_NEXT(regs[o2i(off)])) {
			rval = regs[o2i(off)];
			if ((PCI_CAPLIST_CAP(rval) == i)
			    && (pci_captab[i].printfunc != NULL))
				pci_captab[i].printfunc(regs, off);
		}
	}
}

/* Extended Capability */

static void
pci_conf_print_aer_cap_uc(pcireg_t reg)
{

	onoff("Undefined", reg, PCI_AER_UC_UNDEFINED);
	onoff("Data Link Protocol Error", reg, PCI_AER_UC_DL_PROTOCOL_ERROR);
	onoff("Surprise Down Error", reg, PCI_AER_UC_SURPRISE_DOWN_ERROR);
	onoff("Poisoned TLP Received", reg, PCI_AER_UC_POISONED_TLP);
	onoff("Flow Control Protocol Error", reg, PCI_AER_UC_FC_PROTOCOL_ERROR);
	onoff("Completion Timeout", reg, PCI_AER_UC_COMPLETION_TIMEOUT);
	onoff("Completer Abort", reg, PCI_AER_UC_COMPLETER_ABORT);
	onoff("Unexpected Completion", reg, PCI_AER_UC_UNEXPECTED_COMPLETION);
	onoff("Receiver Overflow", reg, PCI_AER_UC_RECEIVER_OVERFLOW);
	onoff("Malformed TLP", reg, PCI_AER_UC_MALFORMED_TLP);
	onoff("ECRC Error", reg, PCI_AER_UC_ECRC_ERROR);
	onoff("Unsupported Request Error", reg,
	    PCI_AER_UC_UNSUPPORTED_REQUEST_ERROR);
	onoff("ACS Violation", reg, PCI_AER_UC_ACS_VIOLATION);
	onoff("Uncorrectable Internal Error", reg, PCI_AER_UC_INTERNAL_ERROR);
	onoff("MC Blocked TLP", reg, PCI_AER_UC_MC_BLOCKED_TLP);
	onoff("AtomicOp Egress BLK", reg, PCI_AER_UC_ATOMIC_OP_EGRESS_BLOCKED);
	onoff("TLP Prefix Blocked Error", reg,
	    PCI_AER_UC_TLP_PREFIX_BLOCKED_ERROR);
	onoff("Poisoned TLP Egress Blocked", reg,
	    PCI_AER_UC_POISONTLP_EGRESS_BLOCKED);
}

static void
pci_conf_print_aer_cap_cor(pcireg_t reg)
{

	onoff("Receiver Error", reg, PCI_AER_COR_RECEIVER_ERROR);
	onoff("Bad TLP", reg, PCI_AER_COR_BAD_TLP);
	onoff("Bad DLLP", reg, PCI_AER_COR_BAD_DLLP);
	onoff("REPLAY_NUM Rollover", reg, PCI_AER_COR_REPLAY_NUM_ROLLOVER);
	onoff("Replay Timer Timeout", reg, PCI_AER_COR_REPLAY_TIMER_TIMEOUT);
	onoff("Advisory Non-Fatal Error", reg, PCI_AER_COR_ADVISORY_NF_ERROR);
	onoff("Corrected Internal Error", reg, PCI_AER_COR_INTERNAL_ERROR);
	onoff("Header Log Overflow", reg, PCI_AER_COR_HEADER_LOG_OVERFLOW);
}

static void
pci_conf_print_aer_cap_control(pcireg_t reg, bool *tlp_prefix_log)
{

	printf("      First Error Pointer: 0x%04x\n",
	    PCIREG_SHIFTOUT(reg, PCI_AER_FIRST_ERROR_PTR));
	onoff("ECRC Generation Capable", reg, PCI_AER_ECRC_GEN_CAPABLE);
	onoff("ECRC Generation Enable", reg, PCI_AER_ECRC_GEN_ENABLE);
	onoff("ECRC Check Capable", reg, PCI_AER_ECRC_CHECK_CAPABLE);
	onoff("ECRC Check Enable", reg, PCI_AER_ECRC_CHECK_ENABLE);
	onoff("Multiple Header Recording Capable", reg,
	    PCI_AER_MULT_HDR_CAPABLE);
	onoff("Multiple Header Recording Enable", reg,PCI_AER_MULT_HDR_ENABLE);
	onoff("Completion Timeout Prefix/Header Log Capable", reg,
	    PCI_AER_COMPTOUTPRFXHDRLOG_CAP);

	/* This bit is RsvdP if the End-End TLP Prefix Supported bit is Clear */
	if (!tlp_prefix_log)
		return;
	onoff("TLP Prefix Log Present", reg, PCI_AER_TLP_PREFIX_LOG_PRESENT);
	*tlp_prefix_log = (reg & PCI_AER_TLP_PREFIX_LOG_PRESENT) ? true : false;
}

static void
pci_conf_print_aer_cap_rooterr_cmd(pcireg_t reg)
{

	onoff("Correctable Error Reporting Enable", reg,
	    PCI_AER_ROOTERR_COR_ENABLE);
	onoff("Non-Fatal Error Reporting Enable", reg,
	    PCI_AER_ROOTERR_NF_ENABLE);
	onoff("Fatal Error Reporting Enable", reg, PCI_AER_ROOTERR_F_ENABLE);
}

static void
pci_conf_print_aer_cap_rooterr_status(pcireg_t reg)
{

	onoff("ERR_COR Received", reg, PCI_AER_ROOTERR_COR_ERR);
	onoff("Multiple ERR_COR Received", reg, PCI_AER_ROOTERR_MULTI_COR_ERR);
	onoff("ERR_FATAL/NONFATAL_ERR Received", reg, PCI_AER_ROOTERR_UC_ERR);
	onoff("Multiple ERR_FATAL/NONFATAL_ERR Received", reg,
	    PCI_AER_ROOTERR_MULTI_UC_ERR);
	onoff("First Uncorrectable Fatal", reg,PCI_AER_ROOTERR_FIRST_UC_FATAL);
	onoff("Non-Fatal Error Messages Received", reg,PCI_AER_ROOTERR_NF_ERR);
	onoff("Fatal Error Messages Received", reg, PCI_AER_ROOTERR_F_ERR);
	printf("      Advanced Error Interrupt Message Number: 0x%02x\n",
	    PCIREG_SHIFTOUT(reg, PCI_AER_ROOTERR_INT_MESSAGE));
}

static void
pci_conf_print_aer_cap_errsrc_id(pcireg_t reg)
{

	printf("      Correctable Source ID: 0x%04x\n",
	    PCIREG_SHIFTOUT(reg, PCI_AER_ERRSRC_ID_ERR_COR));
	printf("      ERR_FATAL/NONFATAL Source ID: 0x%04x\n",
	    PCIREG_SHIFTOUT(reg, PCI_AER_ERRSRC_ID_ERR_UC));
}

static void
pci_conf_print_aer_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg;
	int pcie_capoff;
	int pcie_devtype = -1;
	bool tlp_prefix_log = false;

	if (pci_conf_find_cap(regs, PCI_CAP_PCIEXPRESS, &pcie_capoff)) {
		reg = regs[o2i(pcie_capoff)];
		pcie_devtype = PCIE_XCAP_TYPE(reg);
		/* PCIe DW9 to DW14 is for PCIe 2.0 and newer */
		if (PCIREG_SHIFTOUT(reg, PCIE_XCAP_VER_MASK) >= 2) {
			reg = regs[o2i(pcie_capoff + PCIE_DCAP2)];
			/* End-End TLP Prefix Supported */
			if (reg & PCIE_DCAP2_EETLP_PREF) {
				tlp_prefix_log = true;
			}
		}
	}

	printf("\n  Advanced Error Reporting Register\n");

	reg = regs[o2i(extcapoff + PCI_AER_UC_STATUS)];
	printf("    Uncorrectable Error Status register: 0x%08x\n", reg);
	pci_conf_print_aer_cap_uc(reg);
	reg = regs[o2i(extcapoff + PCI_AER_UC_MASK)];
	printf("    Uncorrectable Error Mask register: 0x%08x\n", reg);
	pci_conf_print_aer_cap_uc(reg);
	reg = regs[o2i(extcapoff + PCI_AER_UC_SEVERITY)];
	printf("    Uncorrectable Error Severity register: 0x%08x\n", reg);
	pci_conf_print_aer_cap_uc(reg);

	reg = regs[o2i(extcapoff + PCI_AER_COR_STATUS)];
	printf("    Correctable Error Status register: 0x%08x\n", reg);
	pci_conf_print_aer_cap_cor(reg);
	reg = regs[o2i(extcapoff + PCI_AER_COR_MASK)];
	printf("    Correctable Error Mask register: 0x%08x\n", reg);
	pci_conf_print_aer_cap_cor(reg);

	reg = regs[o2i(extcapoff + PCI_AER_CAP_CONTROL)];
	printf("    Advanced Error Capabilities and Control register: 0x%08x\n",
	    reg);
	pci_conf_print_aer_cap_control(reg, &tlp_prefix_log);
	reg = regs[o2i(extcapoff + PCI_AER_HEADER_LOG)];
	printf("    Header Log register:\n");
	pci_conf_print_regs(regs, extcapoff + PCI_AER_HEADER_LOG,
	    extcapoff + PCI_AER_ROOTERR_CMD);

	switch (pcie_devtype) {
	case PCIE_XCAP_TYPE_RP:	/* Root Port of PCI Express Root Complex */
	case PCIE_XCAP_TYPE_RC_EVNTC:	/* Root Complex Event Collector */
		reg = regs[o2i(extcapoff + PCI_AER_ROOTERR_CMD)];
		printf("    Root Error Command register: 0x%08x\n", reg);
		pci_conf_print_aer_cap_rooterr_cmd(reg);
		reg = regs[o2i(extcapoff + PCI_AER_ROOTERR_STATUS)];
		printf("    Root Error Status register: 0x%08x\n", reg);
		pci_conf_print_aer_cap_rooterr_status(reg);

		reg = regs[o2i(extcapoff + PCI_AER_ERRSRC_ID)];
		printf("    Error Source Identification register: 0x%08x\n",
		    reg);
		pci_conf_print_aer_cap_errsrc_id(reg);
		break;
	}

	if (tlp_prefix_log) {
		reg = regs[o2i(extcapoff + PCI_AER_TLP_PREFIX_LOG)];
		printf("    TLP Prefix Log register: 0x%08x\n", reg);
	}
}

/*
 * Helper function to print the arbitration phase register.
 *
 * phases: Number of phases in the arbitration tables.
 * arbsize: Number of bits in each phase.
 * indent: Add more two spaces if it's true.
 */
static void
pci_conf_print_vc_cap_arbtab(const pcireg_t *regs, int off, const char *name,
    const int phases, int arbsize, bool indent)
{
	pcireg_t reg;
	int num_per_reg = 32 / arbsize;
	int i, j;

	printf("%s    %s Arbitration Table:\n", indent ? "  " : "", name);
	for (i = 0; i < phases; i += num_per_reg) {
		reg = regs[o2i(off + (sizeof(uint32_t) * (i / num_per_reg)))];
		for (j = 0; j < num_per_reg; j++) {
			printf("%s      Phase[%d]: 0x%x\n", indent ? "  " : "",
			    i + j,
			    (uint32_t)(reg & __BITS(arbsize - 1, 0)));
			reg >>= arbsize;
		}
	}
}

/* For VC, bit 4-7 are reserved. For Port, bit 6-7 are reserved */
static const int arb_phases[8] = {0, 32, 64, 128, 128, 256, 0, 0 };

static void
pci_conf_print_vc_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg, n;
	int arbtab, parbsize;
	pcireg_t arbsel;
	int i, count;

	printf("\n  Virtual Channel Register\n");
	reg = regs[o2i(extcapoff + PCI_VC_CAP1)];
	printf("    Port VC Capability register 1: 0x%08x\n", reg);
	count = PCIREG_SHIFTOUT(reg, PCI_VC_CAP1_EXT_COUNT);
	printf("      Extended VC Count: %d\n", count);
	n = PCIREG_SHIFTOUT(reg, PCI_VC_CAP1_LOWPRI_EXT_COUNT);
	printf("      Low Priority Extended VC Count: %u\n", n);
	n = PCIREG_SHIFTOUT(reg, PCI_VC_CAP1_REFCLK);
	printf("      Reference Clock: %s\n",
	    (n == PCI_VC_CAP1_REFCLK_100NS) ? "100ns" : "unknown");
	parbsize = 1 << PCIREG_SHIFTOUT(reg, PCI_VC_CAP1_PORT_ARB_TABLE_SIZE);
	printf("      Port Arbitration Table Entry Size: %dbit\n", parbsize);

	reg = regs[o2i(extcapoff + PCI_VC_CAP2)];
	printf("    Port VC Capability register 2: 0x%08x\n", reg);
	onoff("Hardware fixed arbitration scheme",
	    reg, PCI_VC_CAP2_ARB_CAP_HW_FIXED_SCHEME);
	onoff("WRR arbitration with 32 phases",
	    reg, PCI_VC_CAP2_ARB_CAP_WRR_32);
	onoff("WRR arbitration with 64 phases",
	    reg, PCI_VC_CAP2_ARB_CAP_WRR_64);
	onoff("WRR arbitration with 128 phases",
	    reg, PCI_VC_CAP2_ARB_CAP_WRR_128);
	arbtab = PCIREG_SHIFTOUT(reg, PCI_VC_CAP2_ARB_TABLE_OFFSET);
	printf("      VC Arbitration Table Offset: 0x%x\n", arbtab);

	reg = regs[o2i(extcapoff + PCI_VC_CONTROL)] & 0xffff;
	printf("    Port VC Control register: 0x%04x\n", reg);
	arbsel = PCIREG_SHIFTOUT(reg, PCI_VC_CONTROL_VC_ARB_SELECT);
	printf("      VC Arbitration Select: 0x%x\n", arbsel);

	reg = regs[o2i(extcapoff + PCI_VC_STATUS)] >> 16;
	printf("    Port VC Status register: 0x%04x\n", reg);
	onoff("VC Arbitration Table Status",
	    reg, PCI_VC_STATUS_LOAD_VC_ARB_TABLE);

	if ((arbtab != 0) && (arbsel != 0))
		pci_conf_print_vc_cap_arbtab(regs, extcapoff + (arbtab * 16),
		    "VC", arb_phases[arbsel], 4, false);

	for (i = 0; i < count + 1; i++) {
		reg = regs[o2i(extcapoff + PCI_VC_RESOURCE_CAP(i))];
		printf("    VC number %d\n", i);
		printf("      VC Resource Capability Register: 0x%08x\n", reg);
		onoff("  Non-configurable Hardware fixed arbitration scheme",
		    reg, PCI_VC_RESOURCE_CAP_PORT_ARB_CAP_HW_FIXED_SCHEME);
		onoff("  WRR arbitration with 32 phases",
		    reg, PCI_VC_RESOURCE_CAP_PORT_ARB_CAP_WRR_32);
		onoff("  WRR arbitration with 64 phases",
		    reg, PCI_VC_RESOURCE_CAP_PORT_ARB_CAP_WRR_64);
		onoff("  WRR arbitration with 128 phases",
		    reg, PCI_VC_RESOURCE_CAP_PORT_ARB_CAP_WRR_128);
		onoff("  Time-based WRR arbitration with 128 phases",
		    reg, PCI_VC_RESOURCE_CAP_PORT_ARB_CAP_TWRR_128);
		onoff("  WRR arbitration with 256 phases",
		    reg, PCI_VC_RESOURCE_CAP_PORT_ARB_CAP_WRR_256);
		onoff("  Advanced Packet Switching",
		    reg, PCI_VC_RESOURCE_CAP_ADV_PKT_SWITCH);
		onoff("  Reject Snoop Transaction",
		    reg, PCI_VC_RESOURCE_CAP_REJCT_SNOOP_TRANS);
		n = PCIREG_SHIFTOUT(reg, PCI_VC_RESOURCE_CAP_MAX_TIME_SLOTS) + 1;
		printf("        Maximum Time Slots: %d\n", n);
		arbtab = PCIREG_SHIFTOUT(reg,
		    PCI_VC_RESOURCE_CAP_PORT_ARB_TABLE_OFFSET);
		printf("        Port Arbitration Table offset: 0x%02x\n",
		    arbtab);

		reg = regs[o2i(extcapoff + PCI_VC_RESOURCE_CTL(i))];
		printf("      VC Resource Control Register: 0x%08x\n", reg);
		printf("        TC/VC Map: 0x%02x\n",
		    PCIREG_SHIFTOUT(reg, PCI_VC_RESOURCE_CTL_TCVC_MAP));
		/*
		 * The load Port Arbitration Table bit is used to update
		 * the Port Arbitration logic and it's always 0 on read, so
		 * we don't print it.
		 */
		arbsel = PCIREG_SHIFTOUT(reg, PCI_VC_RESOURCE_CTL_PORT_ARB_SELECT);
		printf("        Port Arbitration Select: 0x%x\n", arbsel);
		n = PCIREG_SHIFTOUT(reg, PCI_VC_RESOURCE_CTL_VC_ID);
		printf("        VC ID: %d\n", n);
		onoff("  VC Enable", reg, PCI_VC_RESOURCE_CTL_VC_ENABLE);

		reg = regs[o2i(extcapoff + PCI_VC_RESOURCE_STA(i))] >> 16;
		printf("      VC Resource Status Register: 0x%08x\n", reg);
		onoff("  Port Arbitration Table Status",
		    reg, PCI_VC_RESOURCE_STA_PORT_ARB_TABLE);
		onoff("  VC Negotiation Pending",
		    reg, PCI_VC_RESOURCE_STA_VC_NEG_PENDING);

		if ((arbtab != 0) && (arbsel != 0))
			pci_conf_print_vc_cap_arbtab(regs,
			    extcapoff + (arbtab * 16),
			    "Port", arb_phases[arbsel], parbsize, true);
	}
}

/*
 * Print Power limit. This encoding is the same among the following registers:
 *  - The Captured Slot Power Limit in the PCIe Device Capability Register.
 *  - The Slot Power Limit in the PCIe Slot Capability Register.
 *  - The Base Power in the Data register of Power Budgeting capability.
 */
static void
pci_conf_print_pcie_power(uint8_t base, unsigned int scale)
{
	unsigned int sdiv = 1;

	if ((scale == 0) && (base > 0xef)) {
		const char *s;

		switch (base) {
		case 0xf0:
			s = "239W < x <= 250W";
			break;
		case 0xf1:
			s = "250W < x <= 275W";
			break;
		case 0xf2:
			s = "275W < x <= 300W";
			break;
		default:
			s = "reserved for greater than 300W";
			break;
		}
		printf("%s\n", s);
		return;
	}

	for (unsigned int i = scale; i > 0; i--)
		sdiv *= 10;

	printf("%u", base / sdiv);

	if (scale != 0) {
		printf(".%u", base % sdiv);
	}
	printf ("W\n");
	return;
}

static const char *
pci_conf_print_pwrbdgt_type(uint8_t reg)
{

	switch (reg) {
	case 0x00:
		return "PME Aux";
	case 0x01:
		return "Auxilary";
	case 0x02:
		return "Idle";
	case 0x03:
		return "Sustained";
	case 0x04:
		return "Sustained (Emergency Power Reduction)";
	case 0x05:
		return "Maximum (Emergency Power Reduction)";
	case 0x07:
		return "Maximum";
	default:
		return "Unknown";
	}
}

static const char *
pci_conf_print_pwrbdgt_pwrrail(uint8_t reg)
{

	switch (reg) {
	case 0x00:
		return "Power(12V)";
	case 0x01:
		return "Power(3.3V)";
	case 0x02:
		return "Power(1.5V or 1.8V)";
	case 0x07:
		return "Thermal";
	default:
		return "Unknown";
	}
}

static void
pci_conf_print_pwrbdgt_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg;

	printf("\n  Power Budgeting\n");

	reg = regs[o2i(extcapoff + PCI_PWRBDGT_DSEL)];
	printf("    Data Select register: 0x%08x\n", reg);

	reg = regs[o2i(extcapoff + PCI_PWRBDGT_DATA)];
	printf("    Data register: 0x%08x\n", reg);
	printf("      Base Power: ");
	pci_conf_print_pcie_power(
	    PCIREG_SHIFTOUT(reg, PCI_PWRBDGT_DATA_BASEPWR),
	    PCIREG_SHIFTOUT(reg, PCI_PWRBDGT_DATA_SCALE));
	printf("      PM Sub State: 0x%hhx\n",
	    (uint8_t)PCIREG_SHIFTOUT(reg, PCI_PWRBDGT_PM_SUBSTAT));
	printf("      PM State: D%u\n",
	    PCIREG_SHIFTOUT(reg, PCI_PWRBDGT_PM_STAT));
	printf("      Type: %s\n",
	    pci_conf_print_pwrbdgt_type(
		    (uint8_t)(PCIREG_SHIFTOUT(reg, PCI_PWRBDGT_TYPE))));
	printf("      Power Rail: %s\n",
	    pci_conf_print_pwrbdgt_pwrrail(
		    (uint8_t)(PCIREG_SHIFTOUT(reg, PCI_PWRBDGT_PWRRAIL))));

	reg = regs[o2i(extcapoff + PCI_PWRBDGT_CAP)];
	printf("    Power Budget Capability register: 0x%08x\n", reg);
	onoff("System Allocated",
	    reg, PCI_PWRBDGT_CAP_SYSALLOC);
}

static const char *
pci_conf_print_rclink_dcl_cap_elmtype(unsigned char type)
{

	switch (type) {
	case 0x00:
		return "Configuration Space Element";
	case 0x01:
		return "System Egress Port or internal sink (memory)";
	case 0x02:
		return "Internal Root Complex Link";
	default:
		return "Unknown";
	}
}

static void
pci_conf_print_rclink_dcl_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg;
	unsigned char nent, linktype;
	int i;

	printf("\n  Root Complex Link Declaration\n");

	reg = regs[o2i(extcapoff + PCI_RCLINK_DCL_ESDESC)];
	printf("    Element Self Description Register: 0x%08x\n", reg);
	printf("      Element Type: %s\n",
	    pci_conf_print_rclink_dcl_cap_elmtype((unsigned char)reg));
	nent = PCIREG_SHIFTOUT(reg, PCI_RCLINK_DCL_ESDESC_NUMLINKENT);
	printf("      Number of Link Entries: %hhu\n", nent);
	printf("      Component ID: %hhu\n",
	    (uint8_t)PCIREG_SHIFTOUT(reg, PCI_RCLINK_DCL_ESDESC_COMPID));
	printf("      Port Number: %hhu\n",
	    (uint8_t)PCIREG_SHIFTOUT(reg, PCI_RCLINK_DCL_ESDESC_PORTNUM));
	for (i = 0; i < nent; i++) {
		reg = regs[o2i(extcapoff + PCI_RCLINK_DCL_LINKDESC(i))];
		printf("    Link Entry %d:\n", i + 1);
		printf("      Link Description Register: 0x%08x\n", reg);
		onoff("  Link Valid", reg, PCI_RCLINK_DCL_LINKDESC_LVALID);
		linktype = reg & PCI_RCLINK_DCL_LINKDESC_LTYPE;
		onoff2("  Link Type", reg, PCI_RCLINK_DCL_LINKDESC_LTYPE,
		    "Configuration Space", "Memory-Mapped Space");
		onoff("  Associated RCRB Header", reg,
		    PCI_RCLINK_DCL_LINKDESC_ARCRBH);
		printf("        Target Component ID: %hhu\n",
		    (uint8_t)PCIREG_SHIFTOUT(reg,
		    PCI_RCLINK_DCL_LINKDESC_TCOMPID));
		printf("        Target Port Number: %hhu\n",
		    (uint8_t)PCIREG_SHIFTOUT(reg,
		    PCI_RCLINK_DCL_LINKDESC_TPNUM));

		if (linktype == 0) {
			/* Memory-Mapped Space */
			reg = regs[o2i(extcapoff
				    + PCI_RCLINK_DCL_LINKADDR_LT0_LO(i))];
			printf("      Link Address Low Register: 0x%08x\n",
			    reg);
			reg = regs[o2i(extcapoff
				    + PCI_RCLINK_DCL_LINKADDR_LT0_HI(i))];
			printf("      Link Address High Register: 0x%08x\n",
			    reg);
		} else {
			unsigned int nb;
			pcireg_t lo, hi;

			/* Configuration Space */
			lo = regs[o2i(extcapoff
				    + PCI_RCLINK_DCL_LINKADDR_LT1_LO(i))];
			printf("      Configuration Space Low Register: "
			    "0x%08x\n", lo);
			hi = regs[o2i(extcapoff
				    + PCI_RCLINK_DCL_LINKADDR_LT1_HI(i))];
			printf("      Configuration Space High Register: "
			    "0x%08x\n", hi);
			nb = PCIREG_SHIFTOUT(lo, PCI_RCLINK_DCL_LINKADDR_LT1_N);
			printf("        N: %u\n", nb);
			printf("        Func: %hhu\n",
			    (uint8_t)PCIREG_SHIFTOUT(lo,
			    PCI_RCLINK_DCL_LINKADDR_LT1_FUNC));
			printf("        Dev: %hhu\n",
			    (uint8_t)PCIREG_SHIFTOUT(lo,
			    PCI_RCLINK_DCL_LINKADDR_LT1_DEV));
			printf("        Bus: %hhu\n",
			    (uint8_t)PCIREG_SHIFTOUT(lo,
			    PCI_RCLINK_DCL_LINKADDR_LT1_BUS(nb)));
			lo &= PCI_RCLINK_DCL_LINKADDR_LT1_BAL(i);
			printf("        Configuration Space Base Address: "
			    "0x%016" PRIx64 "\n", ((uint64_t)hi << 32) + lo);
		}
	}
}

/* XXX pci_conf_print_rclink_ctl_cap */

static void
pci_conf_print_rcec_assoc_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg;

	printf("\n  Root Complex Event Collector Association\n");

	reg = regs[o2i(extcapoff + PCI_RCEC_ASSOC_ASSOCBITMAP)];
	printf("    Association Bitmap for Root Complex Integrated Devices:"
	    " 0x%08x\n", reg);

	if (PCI_EXTCAPLIST_VERSION(regs[o2i(extcapoff)]) >= 2) {
		reg = regs[o2i(extcapoff + PCI_RCEC_ASSOC_ASSOCBUSNUM)];
		printf("    RCEC Associated Bus Numbers register: 0x%08x\n",
		    reg);
		printf("      RCEC Next Bus: %u\n",
		    PCIREG_SHIFTOUT(reg,
			PCI_RCEC_ASSOCBUSNUM_RCECNEXT));
		printf("      RCEC Last Bus: %u\n",
		    PCIREG_SHIFTOUT(reg,
			PCI_RCEC_ASSOCBUSNUM_RCECLAST));
	}
}

/* XXX pci_conf_print_mfvc_cap */
/* XXX pci_conf_print_vc2_cap */
/* XXX pci_conf_print_rcrb_cap */
/* XXX pci_conf_print_vendor_cap */
/* XXX pci_conf_print_cac_cap */

static void
pci_conf_print_acs_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg, cap, ctl;
	unsigned int size, i;

	printf("\n  Access Control Services\n");

	reg = regs[o2i(extcapoff + PCI_ACS_CAP)];
	cap = reg & 0xffff;
	ctl = reg >> 16;
	printf("    ACS Capability register: 0x%08x\n", cap);
	onoff("ACS Source Validation", cap, PCI_ACS_CAP_V);
	onoff("ACS Transaction Blocking", cap, PCI_ACS_CAP_B);
	onoff("ACS P2P Request Redirect", cap, PCI_ACS_CAP_R);
	onoff("ACS P2P Completion Redirect", cap, PCI_ACS_CAP_C);
	onoff("ACS Upstream Forwarding", cap, PCI_ACS_CAP_U);
	onoff("ACS Egress Control", cap, PCI_ACS_CAP_E);
	onoff("ACS Direct Translated P2P", cap, PCI_ACS_CAP_T);
	size = PCIREG_SHIFTOUT(cap, PCI_ACS_CAP_ECVSIZE);
	if (size == 0)
		size = 256;
	printf("      Egress Control Vector Size: %u\n", size);
	printf("    ACS Control register: 0x%08x\n", ctl);
	onoff("ACS Source Validation Enable", ctl, PCI_ACS_CTL_V);
	onoff("ACS Transaction Blocking Enable", ctl, PCI_ACS_CTL_B);
	onoff("ACS P2P Request Redirect Enable", ctl, PCI_ACS_CTL_R);
	onoff("ACS P2P Completion Redirect Enable", ctl, PCI_ACS_CTL_C);
	onoff("ACS Upstream Forwarding Enable", ctl, PCI_ACS_CTL_U);
	onoff("ACS Egress Control Enable", ctl, PCI_ACS_CTL_E);
	onoff("ACS Direct Translated P2P Enable", ctl, PCI_ACS_CTL_T);

	/*
	 * If the P2P Egress Control Capability bit is 0, ignore the Egress
	 * Control vector.
	 */
	if ((cap & PCI_ACS_CAP_E) == 0)
		return;
	for (i = 0; i < size; i += 32)
		printf("    Egress Control Vector [%u..%u]: 0x%08x\n", i + 31,
		    i, regs[o2i(extcapoff + PCI_ACS_ECV + (i / 32) * 4 )]);
}

static void
pci_conf_print_ari_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg, cap, ctl;

	printf("\n  Alternative Routing-ID Interpretation Register\n");

	reg = regs[o2i(extcapoff + PCI_ARI_CAP)];
	cap = reg & 0xffff;
	ctl = reg >> 16;
	printf("    Capability register: 0x%08x\n", cap);
	onoff("MVFC Function Groups Capability", reg, PCI_ARI_CAP_M);
	onoff("ACS Function Groups Capability", reg, PCI_ARI_CAP_A);
	printf("      Next Function Number: %u\n",
	    PCIREG_SHIFTOUT(reg, PCI_ARI_CAP_NXTFN));
	printf("    Control register: 0x%08x\n", ctl);
	onoff("MVFC Function Groups Enable", reg, PCI_ARI_CTL_M);
	onoff("ACS Function Groups Enable", reg, PCI_ARI_CTL_A);
	printf("      Function Group: %u\n",
	    PCIREG_SHIFTOUT(reg, PCI_ARI_CTL_FUNCGRP));
}

static void
pci_conf_print_ats_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg, cap, ctl;
	unsigned int num;

	printf("\n  Address Translation Services\n");

	reg = regs[o2i(extcapoff + PCI_ARI_CAP)];
	cap = reg & 0xffff;
	ctl = reg >> 16;
	printf("    Capability register: 0x%04x\n", cap);
	num = PCIREG_SHIFTOUT(reg, PCI_ATS_CAP_INVQDEPTH);
	if (num == 0)
		num = 32;
	printf("      Invalidate Queue Depth: %u\n", num);
	onoff("Page Aligned Request", reg, PCI_ATS_CAP_PALIGNREQ);
	onoff("Global Invalidate", reg, PCI_ATS_CAP_GLOBALINVL);
	onoff("Relaxed Ordering", reg, PCI_ATS_CAP_RELAXORD);

	printf("    Control register: 0x%04x\n", ctl);
	printf("      Smallest Translation Unit: %u\n",
	    PCIREG_SHIFTOUT(reg, PCI_ATS_CTL_STU));
	onoff("Enable", reg, PCI_ATS_CTL_EN);
}

static void
pci_conf_print_sernum_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t lo, hi;

	printf("\n  Device Serial Number Register\n");

	lo = regs[o2i(extcapoff + PCI_SERIAL_LOW)];
	hi = regs[o2i(extcapoff + PCI_SERIAL_HIGH)];
	printf("    Serial Number: %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\n",
	    hi >> 24, (hi >> 16) & 0xff, (hi >> 8) & 0xff, hi & 0xff,
	    lo >> 24, (lo >> 16) & 0xff, (lo >> 8) & 0xff, lo & 0xff);
}

static void
pci_conf_print_sriov_cap(const pcireg_t *regs, int extcapoff)
{
	char buf[sizeof("99999 MB")];
	pcireg_t reg;
	pcireg_t total_vfs;
	int i;
	bool first;

	printf("\n  Single Root IO Virtualization Register\n");

	reg = regs[o2i(extcapoff + PCI_SRIOV_CAP)];
	printf("    Capabilities register: 0x%08x\n", reg);
	onoff("VF Migration Capable", reg, PCI_SRIOV_CAP_VF_MIGRATION);
	onoff("ARI Capable Hierarchy Preserved", reg,
	    PCI_SRIOV_CAP_ARI_CAP_HIER_PRESERVED);
	if (reg & PCI_SRIOV_CAP_VF_MIGRATION) {
		printf("      VF Migration Interrupt Message Number: 0x%03x\n",
		    PCIREG_SHIFTOUT(reg, PCI_SRIOV_CAP_VF_MIGRATION_INTMSG_N));
	}

	reg = regs[o2i(extcapoff + PCI_SRIOV_CTL)] & 0xffff;
	printf("    Control register: 0x%04x\n", reg);
	onoff("VF Enable", reg, PCI_SRIOV_CTL_VF_ENABLE);
	onoff("VF Migration Enable", reg, PCI_SRIOV_CTL_VF_MIGRATION_SUPPORT);
	onoff("VF Migration Interrupt Enable", reg,
	    PCI_SRIOV_CTL_VF_MIGRATION_INT_ENABLE);
	onoff("VF Memory Space Enable", reg, PCI_SRIOV_CTL_VF_MSE);
	onoff("ARI Capable Hierarchy", reg, PCI_SRIOV_CTL_ARI_CAP_HIER);

	reg = regs[o2i(extcapoff + PCI_SRIOV_STA)] >> 16;
	printf("    Status register: 0x%04x\n", reg);
	onoff("VF Migration Status", reg, PCI_SRIOV_STA_VF_MIGRATION);

	reg = regs[o2i(extcapoff + PCI_SRIOV_INITIAL_VFS)] & 0xffff;
	printf("    InitialVFs register: 0x%04x\n", reg);
	total_vfs = reg = regs[o2i(extcapoff + PCI_SRIOV_TOTAL_VFS)] >> 16;
	printf("    TotalVFs register: 0x%04x\n", reg);
	reg = regs[o2i(extcapoff + PCI_SRIOV_NUM_VFS)] & 0xffff;
	printf("    NumVFs register: 0x%04x\n", reg);

	reg = regs[o2i(extcapoff + PCI_SRIOV_FUNC_DEP_LINK)] >> 16;
	printf("    Function Dependency Link register: 0x%04x\n", reg);

	reg = regs[o2i(extcapoff + PCI_SRIOV_VF_OFF)] & 0xffff;
	printf("    First VF Offset register: 0x%04x\n", reg);
	reg = regs[o2i(extcapoff + PCI_SRIOV_VF_STRIDE)] >> 16;
	printf("    VF Stride register: 0x%04x\n", reg);
	reg = regs[o2i(extcapoff + PCI_SRIOV_VF_DID)] >> 16;
	printf("    Device ID: 0x%04x\n", reg);

	reg = regs[o2i(extcapoff + PCI_SRIOV_PAGE_CAP)];
	printf("    Supported Page Sizes register: 0x%08x\n", reg);
	printf("      Supported Page Size:");
	for (i = 0, first = true; i < 32; i++) {
		if (reg & __BIT(i)) {
#ifdef _KERNEL
			format_bytes(buf, sizeof(buf), 1LL << (i + 12));
#else
			humanize_number(buf, sizeof(buf), 1LL << (i + 12), "B",
			    HN_AUTOSCALE, 0);
#endif
			printf("%s %s", first ? "" : ",", buf);
			first = false;
		}
	}
	printf("\n");

	reg = regs[o2i(extcapoff + PCI_SRIOV_PAGE_SIZE)];
	printf("    System Page Sizes register: 0x%08x\n", reg);
	printf("      Page Size: ");
	if (reg != 0) {
		int bitpos = ffs(reg) -1;

		/* Assume only one bit is set. */
#ifdef _KERNEL
		format_bytes(buf, sizeof(buf), 1LL << (bitpos + 12));
#else
		humanize_number(buf, sizeof(buf), 1LL << (bitpos + 12),
		    "B", HN_AUTOSCALE, 0);
#endif
		printf("%s", buf);
	} else {
		printf("unknown");
	}
	printf("\n");

	for (i = 0; i < 6; i++) {
		reg = regs[o2i(extcapoff + PCI_SRIOV_BAR(i))];
		printf("    VF BAR%d register: 0x%08x\n", i, reg);
	}

	if (total_vfs > 0) {
		reg = regs[o2i(extcapoff + PCI_SRIOV_VF_MIG_STA_AR)];
		printf("    VF Migration State Array Offset register: 0x%08x\n",
		    reg);
		printf("      VF Migration State Offset: 0x%08x\n",
		    PCIREG_SHIFTOUT(reg, PCI_SRIOV_VF_MIG_STA_OFFSET));
		i = PCIREG_SHIFTOUT(reg, PCI_SRIOV_VF_MIG_STA_BIR);
		printf("      VF Migration State BIR: ");
		if (i >= 0 && i <= 5) {
			printf("BAR%d", i);
		} else {
			printf("unknown BAR (%d)", i);
		}
		printf("\n");
	}
}

/* XXX pci_conf_print_mriov_cap */

static void
pci_conf_print_multicast_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg, cap, ctl;
	pcireg_t regl, regh;
	uint64_t addr;
	int n;

	printf("\n  Multicast\n");

	reg = regs[o2i(extcapoff + PCI_MCAST_CTL)];
	cap = reg & 0xffff;
	ctl = reg >> 16;
	printf("    Capability Register: 0x%04x\n", cap);
	printf("      Max Group: %u\n",
	    (pcireg_t)(reg & PCI_MCAST_CAP_MAXGRP) + 1);

	/* Endpoint Only */
	n = PCIREG_SHIFTOUT(reg, PCI_MCAST_CAP_WINSIZEREQ);
	if (n > 0)
		printf("      Window Size Requested: %d\n", 1 << (n - 1));

	onoff("ECRC Regeneration Supported", reg, PCI_MCAST_CAP_ECRCREGEN);

	printf("    Control Register: 0x%04x\n", ctl);
	printf("      Num Group: %u\n",
	    PCIREG_SHIFTOUT(reg, PCI_MCAST_CTL_NUMGRP) + 1);
	onoff("Enable", reg, PCI_MCAST_CTL_ENA);

	regl = regs[o2i(extcapoff + PCI_MCAST_BARL)];
	regh = regs[o2i(extcapoff + PCI_MCAST_BARH)];
	printf("    Base Address Register 0: 0x%08x\n", regl);
	printf("    Base Address Register 1: 0x%08x\n", regh);
	printf("      Index Position: %u\n",
	    (unsigned int)(regl & PCI_MCAST_BARL_INDPOS));
	addr = ((uint64_t)regh << 32) | (regl & PCI_MCAST_BARL_ADDR);
	printf("      Base Address: 0x%016" PRIx64 "\n", addr);

	regl = regs[o2i(extcapoff + PCI_MCAST_RECVL)];
	regh = regs[o2i(extcapoff + PCI_MCAST_RECVH)];
	printf("    Receive Register 0: 0x%08x\n", regl);
	printf("    Receive Register 1: 0x%08x\n", regh);

	regl = regs[o2i(extcapoff + PCI_MCAST_BLOCKALLL)];
	regh = regs[o2i(extcapoff + PCI_MCAST_BLOCKALLH)];
	printf("    Block All Register 0: 0x%08x\n", regl);
	printf("    Block All Register 1: 0x%08x\n", regh);

	regl = regs[o2i(extcapoff + PCI_MCAST_BLOCKUNTRNSL)];
	regh = regs[o2i(extcapoff + PCI_MCAST_BLOCKUNTRNSH)];
	printf("    Block Untranslated Register 0: 0x%08x\n", regl);
	printf("    Block Untranslated Register 1: 0x%08x\n", regh);

	regl = regs[o2i(extcapoff + PCI_MCAST_OVERLAYL)];
	regh = regs[o2i(extcapoff + PCI_MCAST_OVERLAYH)];
	printf("    Overlay BAR 0: 0x%08x\n", regl);
	printf("    Overlay BAR 1: 0x%08x\n", regh);

	n = regl & PCI_MCAST_OVERLAYL_SIZE;
	printf("      Overlay Size: ");
	if (n >= 6)
		printf("%d\n", n);
	else
		printf("off\n");
	addr = ((uint64_t)regh << 32) | (regl & PCI_MCAST_OVERLAYL_ADDR);
	printf("      Overlay BAR: 0x%016" PRIx64 "\n", addr);
}

static void
pci_conf_print_page_req_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg, ctl, sta;

	printf("\n  Page Request\n");

	reg = regs[o2i(extcapoff + PCI_PAGE_REQ_CTL)];
	ctl = reg & 0xffff;
	sta = reg >> 16;
	printf("    Control Register: 0x%04x\n", ctl);
	onoff("Enable", reg, PCI_PAGE_REQ_CTL_E);
	onoff("Reset", reg, PCI_PAGE_REQ_CTL_R);

	printf("    Status Register: 0x%04x\n", sta);
	onoff("Response Failure", reg, PCI_PAGE_REQ_STA_RF);
	onoff("Unexpected Page Request Group Index", reg,
	    PCI_PAGE_REQ_STA_UPRGI);
	onoff("Stopped", reg, PCI_PAGE_REQ_STA_S);
	onoff("PRG Response PASID Required", reg, PCI_PAGE_REQ_STA_PASIDR);

	reg = regs[o2i(extcapoff + PCI_PAGE_REQ_OUTSTCAPA)];
	printf("    Outstanding Page Request Capacity: %u\n", reg);
	reg = regs[o2i(extcapoff + PCI_PAGE_REQ_OUTSTALLOC)];
	printf("    Outstanding Page Request Allocation: %u\n", reg);
}

/* XXX pci_conf_print_amd_cap */

#define MEM_PBUFSIZE	sizeof("999GB")

static void
pci_conf_print_resizbar_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t cap, ctl;
	unsigned int bars, i, n;
	char pbuf[MEM_PBUFSIZE];

	printf("\n  Resizable BAR\n");

	/* Get Number of Resizable BARs */
	ctl = regs[o2i(extcapoff + PCI_RESIZBAR_CTL(0))];
	bars = PCIREG_SHIFTOUT(ctl, PCI_RESIZBAR_CTL_NUMBAR);
	printf("    Number of Resizable BARs: ");
	if (bars <= 6)
		printf("%u\n", bars);
	else {
		printf("incorrect (%u)\n", bars);
		return;
	}

	for (n = 0; n < 6; n++) {
		cap = regs[o2i(extcapoff + PCI_RESIZBAR_CAP(n))];
		printf("    Capability register(%u): 0x%08x\n", n, cap);
		if ((cap & PCI_RESIZBAR_CAP_SIZEMASK) == 0)
			continue; /* Not Used */
		printf("      Acceptable BAR sizes:");
		for (i = 4; i <= 23; i++) {
			if ((cap & (1 << i)) != 0) {
				humanize_number(pbuf, MEM_PBUFSIZE,
				    (int64_t)1024 * 1024 << (i - 4), "B",
#ifdef _KERNEL
				    1);
#else
				    HN_AUTOSCALE, HN_NOSPACE);
#endif
				printf(" %s", pbuf);
			}
		}
		printf("\n");

		ctl = regs[o2i(extcapoff + PCI_RESIZBAR_CTL(n))];
		printf("    Control register(%u): 0x%08x\n", n, ctl);
		printf("      BAR Index: %u\n",
		    PCIREG_SHIFTOUT(ctl, PCI_RESIZBAR_CTL_BARIDX));
		humanize_number(pbuf, MEM_PBUFSIZE,
		    (int64_t)1024 * 1024
		    << PCIREG_SHIFTOUT(ctl, PCI_RESIZBAR_CTL_BARSIZ),
		    "B",
#ifdef _KERNEL
		    1);
#else
		    HN_AUTOSCALE, HN_NOSPACE);
#endif
		printf("      BAR Size: %s\n", pbuf);
	}
}

static void
pci_conf_print_dpa_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg;
	unsigned int substmax, i;

	printf("\n  Dynamic Power Allocation\n");

	reg = regs[o2i(extcapoff + PCI_DPA_CAP)];
	printf("    Capability register: 0x%08x\n", reg);
	substmax = PCIREG_SHIFTOUT(reg, PCI_DPA_CAP_SUBSTMAX);
	printf("      Substate Max: %u\n", substmax);
	printf("      Transition Latency Unit: ");
	switch (PCIREG_SHIFTOUT(reg, PCI_DPA_CAP_TLUINT)) {
	case 0:
		printf("1ms\n");
		break;
	case 1:
		printf("10ms\n");
		break;
	case 2:
		printf("100ms\n");
		break;
	default:
		printf("reserved\n");
		break;
	}
	printf("      Power Allocation Scale: ");
	switch (PCIREG_SHIFTOUT(reg, PCI_DPA_CAP_PAS)) {
	case 0:
		printf("10.0x\n");
		break;
	case 1:
		printf("1.0x\n");
		break;
	case 2:
		printf("0.1x\n");
		break;
	case 3:
		printf("0.01x\n");
		break;
	}
	printf("      Transition Latency Value 0: %u\n",
	    PCIREG_SHIFTOUT(reg, PCI_DPA_CAP_XLCY0));
	printf("      Transition Latency Value 1: %u\n",
	    PCIREG_SHIFTOUT(reg, PCI_DPA_CAP_XLCY1));

	reg = regs[o2i(extcapoff + PCI_DPA_LATIND)];
	printf("    Latency Indicator register: 0x%08x\n", reg);

	reg = regs[o2i(extcapoff + PCI_DPA_CS)];
	printf("    Status register: 0x%04x\n", reg & 0xffff);
	printf("      Substate Status: 0x%02x\n",
	    PCIREG_SHIFTOUT(reg, PCI_DPA_CS_SUBSTSTAT));
	onoff("Substate Control Enabled", reg, PCI_DPA_CS_SUBSTCTLEN);
	printf("    Control register: 0x%04x\n", reg >> 16);
	printf("      Substate Control: 0x%02x\n",
	    PCIREG_SHIFTOUT(reg, PCI_DPA_CS_SUBSTCTL));

	for (i = 0; i <= substmax; i++)
		printf("    Substate Power Allocation register %d: 0x%02x\n",
		    i, (regs[PCI_DPA_PWRALLOC + (i / 4)] >> (i % 4) & 0xff));
}

static const char *
pci_conf_print_tph_req_cap_sttabloc(uint8_t val)
{

	switch (val) {
	case PCI_TPH_REQ_STTBLLOC_NONE:
		return "Not Present";
	case PCI_TPH_REQ_STTBLLOC_TPHREQ:
		return "in the TPH Requester Capability Structure";
	case PCI_TPH_REQ_STTBLLOC_MSIX:
		return "in the MSI-X Table";
	default:
		return "Unknown";
	}
}

static void
pci_conf_print_tph_req_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg;
	int size = 0, i, j;
	uint8_t sttbloc;

	printf("\n  TPH Requester Extended Capability\n");

	reg = regs[o2i(extcapoff + PCI_TPH_REQ_CAP)];
	printf("    TPH Requester Capabililty register: 0x%08x\n", reg);
	onoff("No ST Mode Supported", reg, PCI_TPH_REQ_CAP_NOST);
	onoff("Interrupt Vector Mode Supported", reg, PCI_TPH_REQ_CAP_INTVEC);
	onoff("Device Specific Mode Supported", reg, PCI_TPH_REQ_CAP_DEVSPEC);
	onoff("Extend TPH Requester Supported", reg, PCI_TPH_REQ_CAP_XTPHREQ);
	sttbloc = PCIREG_SHIFTOUT(reg, PCI_TPH_REQ_CAP_STTBLLOC);
	printf("      ST Table Location: %s\n",
	    pci_conf_print_tph_req_cap_sttabloc(sttbloc));
	if (sttbloc == PCI_TPH_REQ_STTBLLOC_TPHREQ) {
		size = PCIREG_SHIFTOUT(reg, PCI_TPH_REQ_CAP_STTBLSIZ) + 1;
		printf("      ST Table Size: %d\n", size);
	}

	reg = regs[o2i(extcapoff + PCI_TPH_REQ_CTL)];
	printf("    TPH Requester Control register: 0x%08x\n", reg);
	printf("      ST Mode Select: ");
	switch (PCIREG_SHIFTOUT(reg, PCI_TPH_REQ_CTL_STSEL)) {
	case PCI_TPH_REQ_CTL_STSEL_NO:
		printf("No ST Mode\n");
		break;
	case PCI_TPH_REQ_CTL_STSEL_IV:
		printf("Interrupt Vector Mode\n");
		break;
	case PCI_TPH_REQ_CTL_STSEL_DS:
		printf("Device Specific Mode\n");
		break;
	default:
		printf("(reserved value)\n");
		break;
	}
	printf("      TPH Requester Enable: ");
	switch (PCIREG_SHIFTOUT(reg, PCI_TPH_REQ_CTL_TPHREQEN)) {
	case PCI_TPH_REQ_CTL_TPHREQEN_NO: /* 0x0 */
		printf("Not permitted\n");
		break;
	case PCI_TPH_REQ_CTL_TPHREQEN_TPH:
		printf("TPH and not Extended TPH\n");
		break;
	case PCI_TPH_REQ_CTL_TPHREQEN_ETPH:
		printf("TPH and Extended TPH");
		break;
	default:
		printf("(reserved value)\n");
		break;
	}

	if (sttbloc != PCI_TPH_REQ_STTBLLOC_TPHREQ)
		return;

	for (i = 0; i < size ; i += 2) {
		reg = regs[o2i(extcapoff + PCI_TPH_REQ_STTBL + i / 2)];
		for (j = 0; j < 2 ; j++) {
			uint32_t entry = reg;

			if (j != 0)
				entry >>= 16;
			entry &= 0xffff;
			printf("    TPH ST Table Entry (%d): 0x%04"PRIx32"\n",
			    i + j, entry);
		}
	}
}

static void
pci_conf_print_ltr_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg;

	printf("\n  Latency Tolerance Reporting\n");
	reg = regs[o2i(extcapoff + PCI_LTR_MAXSNOOPLAT)];
	printf("    Max Snoop Latency Register: 0x%04x\n", reg & 0xffff);
	printf("      Max Snoop Latency: %juns\n",
	    (uintmax_t)(PCIREG_SHIFTOUT(reg, PCI_LTR_MAXSNOOPLAT_VAL)
	    * PCI_LTR_SCALETONS(PCIREG_SHIFTOUT(reg, PCI_LTR_MAXSNOOPLAT_SCALE))));
	printf("    Max No-Snoop Latency Register: 0x%04x\n", reg >> 16);
	printf("      Max No-Snoop Latency: %juns\n",
	    (uintmax_t)(PCIREG_SHIFTOUT(reg, PCI_LTR_MAXNOSNOOPLAT_VAL)
	    * PCI_LTR_SCALETONS(PCIREG_SHIFTOUT(reg, PCI_LTR_MAXNOSNOOPLAT_SCALE))));
}

static void
pci_conf_print_sec_pcie_cap(const pcireg_t *regs, int extcapoff)
{
	int pcie_capoff;
	pcireg_t reg;
	int i, maxlinkwidth;

	printf("\n  Secondary PCI Express Register\n");

	reg = regs[o2i(extcapoff + PCI_SECPCIE_LCTL3)];
	printf("    Link Control 3 register: 0x%08x\n", reg);
	onoff("Perform Equalization", reg, PCI_SECPCIE_LCTL3_PERFEQ);
	onoff("Link Equalization Request Interrupt Enable",
	    reg, PCI_SECPCIE_LCTL3_LINKEQREQ_IE);
	printf("      Enable Lower SKP OS Generation Vector:");
	pci_print_pcie_linkspeedvector(
		PCIREG_SHIFTOUT(reg, PCI_SECPCIE_LCTL3_ELSKPOSGENV));
	printf("\n");

	reg = regs[o2i(extcapoff + PCI_SECPCIE_LANEERR_STA)];
	printf("    Lane Error Status register: 0x%08x\n", reg);

	/* Get Max Link Width */
	if (pci_conf_find_cap(regs, PCI_CAP_PCIEXPRESS, &pcie_capoff)) {
		reg = regs[o2i(pcie_capoff + PCIE_LCAP)];
		maxlinkwidth = PCIREG_SHIFTOUT(reg, PCIE_LCAP_MAX_WIDTH);
	} else {
		printf("error: failed to get PCIe capability\n");
		return;
	}
	for (i = 0; i < maxlinkwidth; i++) {
		reg = regs[o2i(extcapoff + PCI_SECPCIE_EQCTL(i))];
		if (i % 2 != 0)
			reg >>= 16;
		else
			reg &= 0xffff;
		printf("    Equalization Control Register (Link %d): 0x%04x\n",
		    i, reg);
		printf("      Downstream Port Transmit Preset: 0x%x\n",
		    PCIREG_SHIFTOUT(reg,
			PCI_SECPCIE_EQCTL_DP_XMIT_PRESET));
		printf("      Downstream Port Receive Hint: 0x%x\n",
		    PCIREG_SHIFTOUT(reg, PCI_SECPCIE_EQCTL_DP_RCV_HINT));
		printf("      Upstream Port Transmit Preset: 0x%x\n",
		    PCIREG_SHIFTOUT(reg,
			PCI_SECPCIE_EQCTL_UP_XMIT_PRESET));
		printf("      Upstream Port Receive Hint: 0x%x\n",
		    PCIREG_SHIFTOUT(reg, PCI_SECPCIE_EQCTL_UP_RCV_HINT));
	}
}

/* XXX pci_conf_print_pmux_cap */

static void
pci_conf_print_pasid_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg, cap, ctl;
	unsigned int num;

	printf("\n  Process Address Space ID\n");

	reg = regs[o2i(extcapoff + PCI_PASID_CAP)];
	cap = reg & 0xffff;
	ctl = reg >> 16;
	printf("    PASID Capability Register: 0x%04x\n", cap);
	onoff("Execute Permission Supported", reg, PCI_PASID_CAP_XPERM);
	onoff("Privileged Mode Supported", reg, PCI_PASID_CAP_PRIVMODE);
	num = (1 << PCIREG_SHIFTOUT(reg, PCI_PASID_CAP_MAXPASIDW)) - 1;
	printf("      Max PASID Width: %u\n", num);

	printf("    PASID Control Register: 0x%04x\n", ctl);
	onoff("PASID Enable", reg, PCI_PASID_CTL_PASID_EN);
	onoff("Execute Permission Enable", reg, PCI_PASID_CTL_XPERM_EN);
	onoff("Privileged Mode Enable", reg, PCI_PASID_CTL_PRIVMODE_EN);
}

static void
pci_conf_print_lnr_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg, cap, ctl;
	unsigned int num;

	printf("\n  LN Requester\n");

	reg = regs[o2i(extcapoff + PCI_LNR_CAP)];
	cap = reg & 0xffff;
	ctl = reg >> 16;
	printf("    LNR Capability register: 0x%04x\n", cap);
	onoff("LNR-64 Supported", reg, PCI_LNR_CAP_64);
	onoff("LNR-128 Supported", reg, PCI_LNR_CAP_128);
	num = 1 << PCIREG_SHIFTOUT(reg, PCI_LNR_CAP_REGISTMAX);
	printf("      LNR Registration MAX: %u\n", num);

	printf("    LNR Control register: 0x%04x\n", ctl);
	onoff("LNR Enable", reg, PCI_LNR_CTL_EN);
	onoff("LNR CLS", reg, PCI_LNR_CTL_CLS);
	num = 1 << PCIREG_SHIFTOUT(reg, PCI_LNR_CTL_REGISTLIM);
	printf("      LNR Registration Limit: %u\n", num);
}

static void
pci_conf_print_dpc_pio(pcireg_t r)
{
	onoff("Cfg Request received UR Completion", r,PCI_DPC_RPPIO_CFGUR_CPL);
	onoff("Cfg Request received CA Completion", r,PCI_DPC_RPPIO_CFGCA_CPL);
	onoff("Cfg Request Completion Timeout", r, PCI_DPC_RPPIO_CFG_CTO);
	onoff("I/O Request received UR Completion", r, PCI_DPC_RPPIO_IOUR_CPL);
	onoff("I/O Request received CA Completion", r, PCI_DPC_RPPIO_IOCA_CPL);
	onoff("I/O Request Completion Timeout", r, PCI_DPC_RPPIO_IO_CTO);
	onoff("Mem Request received UR Completion", r,PCI_DPC_RPPIO_MEMUR_CPL);
	onoff("Mem Request received CA Completion", r,PCI_DPC_RPPIO_MEMCA_CPL);
	onoff("Mem Request Completion Timeout", r, PCI_DPC_RPPIO_MEM_CTO);
}

static void
pci_conf_print_dpc_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg, cap, ctl, stat, errsrc;
	const char *trigstr;
	bool rpext;

	printf("\n  Downstream Port Containment\n");

	reg = regs[o2i(extcapoff + PCI_DPC_CCR)];
	cap = reg & 0xffff;
	ctl = reg >> 16;
	rpext = (reg & PCI_DPCCAP_RPEXT) ? true : false;
	printf("    DPC Capability register: 0x%04x\n", cap);
	printf("      DPC Interrupt Message Number: %02x\n",
	    (unsigned int)(cap & PCI_DPCCAP_IMSGN));
	onoff("RP Extensions for DPC", reg, PCI_DPCCAP_RPEXT);
	onoff("Poisoned TLP Egress Blocking Supported", reg,
	    PCI_DPCCAP_POISONTLPEB);
	onoff("DPC Software Triggering Supported", reg, PCI_DPCCAP_SWTRIG);
	printf("      RP PIO Log Size: %u\n",
	    PCIREG_SHIFTOUT(reg, PCI_DPCCAP_RPPIOLOGSZ));
	onoff("DL_Active ERR_COR Signaling Supported", reg,
	    PCI_DPCCAP_DLACTECORS);
	printf("    DPC Control register: 0x%04x\n", ctl);
	switch (PCIREG_SHIFTOUT(reg, PCI_DPCCTL_TIRGEN)) {
	case 0:
		trigstr = "disabled";
		break;
	case 1:
		trigstr = "enabled(ERR_FATAL)";
		break;
	case 2:
		trigstr = "enabled(ERR_NONFATAL or ERR_FATAL)";
		break;
	default:
		trigstr = "(reserverd)";
		break;
	}
	printf("      DPC Trigger Enable: %s\n", trigstr);
	printf("      DPC Completion Control: %s Completion Status\n",
	    (reg & PCI_DPCCTL_COMPCTL)
	    ? "Unsupported Request(UR)" : "Completer Abort(CA)");
	onoff("DPC Interrupt Enable", reg, PCI_DPCCTL_IE);
	onoff("DPC ERR_COR Enable", reg, PCI_DPCCTL_ERRCOREN);
	onoff("Poisoned TLP Egress Blocking Enable", reg,
	    PCI_DPCCTL_POISONTLPEB);
	onoff("DPC Software Trigger", reg, PCI_DPCCTL_SWTRIG);
	onoff("DL_Active ERR_COR Enable", reg, PCI_DPCCTL_DLACTECOR);

	reg = regs[o2i(extcapoff + PCI_DPC_STATESID)];
	stat = reg & 0xffff;
	errsrc = reg >> 16;
	printf("    DPC Status register: 0x%04x\n", stat);
	onoff("DPC Trigger Status", reg, PCI_DPCSTAT_TSTAT);
	switch (PCIREG_SHIFTOUT(reg, PCI_DPCSTAT_TREASON)) {
	case 0:
		trigstr = "an unmasked uncorrectable error";
		break;
	case 1:
		trigstr = "receiving an ERR_NONFATAL";
		break;
	case 2:
		trigstr = "receiving an ERR_FATAL";
		break;
	case 3:
		trigstr = "DPC Trigger Reason Extension field";
		break;
	}
	printf("      DPC Trigger Reason: Due to %s\n", trigstr);
	onoff("DPC Interrupt Status", reg, PCI_DPCSTAT_ISTAT);
	if (rpext)
		onoff("DPC RP Busy", reg, PCI_DPCSTAT_RPBUSY);
	switch (PCIREG_SHIFTOUT(reg, PCI_DPCSTAT_TREASON)) {
	case 0:
		trigstr = "Due to RP PIO error";
		break;
	case 1:
		trigstr = "Due to the DPC Software trigger bit";
		break;
	default:
		trigstr = "(reserved)";
		break;
	}
	printf("      DPC Trigger Reason Extension: %s\n", trigstr);
	if (rpext)
		printf("      RP PIO First Error Pointer: 0x%02x\n",
		    PCIREG_SHIFTOUT(reg, PCI_DPCSTAT_RPPIOFEP));
	printf("    DPC Error Source ID register: 0x%04x\n", errsrc);

	if (!rpext)
		return;
	/*
	 * All of the following registers are implemented by a device which has
	 * RP Extensions for DPC
	 */

	reg = regs[o2i(extcapoff + PCI_DPC_RPPIO_STAT)];
	printf("    RP PIO Status Register: 0x%08x\n", reg);
	pci_conf_print_dpc_pio(reg);

	reg = regs[o2i(extcapoff + PCI_DPC_RPPIO_MASK)];
	printf("    RP PIO Mask Register: 0x%08x\n", reg);
	pci_conf_print_dpc_pio(reg);

	reg = regs[o2i(extcapoff + PCI_DPC_RPPIO_SEVE)];
	printf("    RP PIO Severity Register: 0x%08x\n", reg);
	pci_conf_print_dpc_pio(reg);

	reg = regs[o2i(extcapoff + PCI_DPC_RPPIO_SYSERR)];
	printf("    RP PIO SysError Register: 0x%08x\n", reg);
	pci_conf_print_dpc_pio(reg);

	reg = regs[o2i(extcapoff + PCI_DPC_RPPIO_EXCPT)];
	printf("    RP PIO Exception Register: 0x%08x\n", reg);
	pci_conf_print_dpc_pio(reg);

	printf("    RP PIO Header Log Register: start from 0x%03x\n",
	    extcapoff + PCI_DPC_RPPIO_HLOG);
	printf("    RP PIO ImpSpec Log Register: start from 0x%03x\n",
	    extcapoff + PCI_DPC_RPPIO_IMPSLOG);
	printf("    RP PIO TLP Prefix Log Register: start from 0x%03x\n",
	    extcapoff + PCI_DPC_RPPIO_TLPPLOG);
}


static int
pci_conf_l1pm_cap_tposcale(unsigned char scale)
{

	/* Return scale in us */
	switch (scale) {
	case 0x0:
		return 2;
	case 0x1:
		return 10;
	case 0x2:
		return 100;
	default:
		return -1;
	}
}

static void
pci_conf_print_l1pm_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg;
	int scale, val;
	int pcie_capoff;

	printf("\n  L1 PM Substates\n");

	reg = regs[o2i(extcapoff + PCI_L1PM_CAP)];
	printf("    L1 PM Substates Capability register: 0x%08x\n", reg);
	onoff("PCI-PM L1.2 Supported", reg, PCI_L1PM_CAP_PCIPM12);
	onoff("PCI-PM L1.1 Supported", reg, PCI_L1PM_CAP_PCIPM11);
	onoff("ASPM L1.2 Supported", reg, PCI_L1PM_CAP_ASPM12);
	onoff("ASPM L1.1 Supported", reg, PCI_L1PM_CAP_ASPM11);
	onoff("L1 PM Substates Supported", reg, PCI_L1PM_CAP_L1PM);
	/* The Link Activation Supported bit is only for Downstream Port */
	if (pci_conf_find_cap(regs, PCI_CAP_PCIEXPRESS, &pcie_capoff)) {
		uint32_t t = regs[o2i(pcie_capoff)];

		if ((t == PCIE_XCAP_TYPE_RP) || (t == PCIE_XCAP_TYPE_DOWN))
			onoff("Link Activation Supported", reg,
			    PCI_L1PM_CAP_LA);
	}
	printf("      Port Common Mode Restore Time: %uus\n",
	    PCIREG_SHIFTOUT(reg, PCI_L1PM_CAP_PCMRT));
	scale = pci_conf_l1pm_cap_tposcale(
	    PCIREG_SHIFTOUT(reg, PCI_L1PM_CAP_PTPOSCALE));
	val = PCIREG_SHIFTOUT(reg, PCI_L1PM_CAP_PTPOVAL);
	printf("      Port T_POWER_ON: ");
	if (scale == -1)
		printf("unknown\n");
	else
		printf("%dus\n", val * scale);

	reg = regs[o2i(extcapoff + PCI_L1PM_CTL1)];
	printf("    L1 PM Substates Control register 1: 0x%08x\n", reg);
	onoff("PCI-PM L1.2 Enable", reg, PCI_L1PM_CTL1_PCIPM12_EN);
	onoff("PCI-PM L1.1 Enable", reg, PCI_L1PM_CTL1_PCIPM11_EN);
	onoff("ASPM L1.2 Enable", reg, PCI_L1PM_CTL1_ASPM12_EN);
	onoff("ASPM L1.1 Enable", reg, PCI_L1PM_CTL1_ASPM11_EN);
	onoff("Link Activation Interrupt Enable", reg, PCI_L1PM_CTL1_LAIE);
	onoff("Link Activation Control", reg, PCI_L1PM_CTL1_LA);
	printf("      Common Mode Restore Time: %uus\n",
	    PCIREG_SHIFTOUT(reg, PCI_L1PM_CTL1_CMRT));
	scale = PCI_LTR_SCALETONS(PCIREG_SHIFTOUT(reg, PCI_L1PM_CTL1_LTRTHSCALE));
	val = PCIREG_SHIFTOUT(reg, PCI_L1PM_CTL1_LTRTHVAL);
	printf("      LTR L1.2 THRESHOLD: %dus\n", val * scale);

	reg = regs[o2i(extcapoff + PCI_L1PM_CTL2)];
	printf("    L1 PM Substates Control register 2: 0x%08x\n", reg);
	scale = pci_conf_l1pm_cap_tposcale(
		PCIREG_SHIFTOUT(reg, PCI_L1PM_CTL2_TPOSCALE));
	val = PCIREG_SHIFTOUT(reg, PCI_L1PM_CTL2_TPOVAL);
	printf("      T_POWER_ON: ");
	if (scale == -1)
		printf("unknown\n");
	else
		printf("%dus\n", val * scale);

	if (PCI_EXTCAPLIST_VERSION(regs[o2i(extcapoff)]) >= 2) {
		reg = regs[o2i(extcapoff + PCI_L1PM_CTL2)];
		printf("    L1 PM Substates Status register: 0x%08x\n", reg);
		onoff("Link Activation Status", reg, PCI_L1PM_STAT_LA);
	}
}

static void
pci_conf_print_ptm_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg;
	uint32_t val;

	printf("\n  Precision Time Measurement\n");

	reg = regs[o2i(extcapoff + PCI_PTM_CAP)];
	printf("    PTM Capability register: 0x%08x\n", reg);
	onoff("PTM Requester Capable", reg, PCI_PTM_CAP_REQ);
	onoff("PTM Responder Capable", reg, PCI_PTM_CAP_RESP);
	onoff("PTM Root Capable", reg, PCI_PTM_CAP_ROOT);
	printf("      Local Clock Granularity: ");
	val = PCIREG_SHIFTOUT(reg, PCI_PTM_CAP_LCLCLKGRNL);
	switch (val) {
	case 0:
		printf("Not implemented\n");
		break;
	case 0xffff:
		printf("> 254ns\n");
		break;
	default:
		printf("%uns\n", val);
		break;
	}

	reg = regs[o2i(extcapoff + PCI_PTM_CTL)];
	printf("    PTM Control register: 0x%08x\n", reg);
	onoff("PTM Enable", reg, PCI_PTM_CTL_EN);
	onoff("Root Select", reg, PCI_PTM_CTL_ROOTSEL);
	printf("      Effective Granularity: ");
	val = PCIREG_SHIFTOUT(reg, PCI_PTM_CTL_EFCTGRNL);
	switch (val) {
	case 0:
		printf("Unknown\n");
		break;
	case 0xffff:
		printf("> 254ns\n");
		break;
	default:
		printf("%uns\n", val);
		break;
	}
}

/* XXX pci_conf_print_mpcie_cap */
/* XXX pci_conf_print_frsq_cap */
/* XXX pci_conf_print_rtr_cap */
/* XXX pci_conf_print_desigvndsp_cap */
/* XXX pci_conf_print_vf_resizbar_cap */

static void
pci_conf_print_dlf_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg;

	printf("\n  Data link Feature Register\n");
	reg = regs[o2i(extcapoff + PCI_DLF_CAP)];
	printf("    Capability register: 0x%08x\n", reg);
	onoff("Scaled Flow Control", reg, PCI_DLF_LFEAT_SCLFCTL);
	onoff("DLF Exchange enable", reg, PCI_DLF_CAP_XCHG);

	reg = regs[o2i(extcapoff + PCI_DLF_STAT)];
	printf("    Status register: 0x%08x\n", reg);
	onoff("Scaled Flow Control", reg, PCI_DLF_LFEAT_SCLFCTL);
	onoff("Remote DLF supported Valid", reg, PCI_DLF_STAT_RMTVALID);
}

static void
pci_conf_print_pl16g_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg, lwidth;
	int pcie_capoff;
	unsigned int i, j;

	printf("\n  Physical Layer 16.0 GT/s\n");
	reg = regs[o2i(extcapoff + PCI_PL16G_CAP)];
	printf("    Capability register: 0x%08x\n", reg);

	reg = regs[o2i(extcapoff + PCI_PL16G_CTL)];
	printf("    Control register: 0x%08x\n", reg);

	reg = regs[o2i(extcapoff + PCI_PL16G_STAT)];
	printf("    Status register: 0x%08x\n", reg);
	onoff("Equalization 16.0 GT/s Complete", reg, PCI_PL16G_STAT_EQ_COMPL);
	onoff("Equalization 16.0 GT/s Phase 1 Successful", reg,
	    PCI_PL16G_STAT_EQ_P1S);
	onoff("Equalization 16.0 GT/s Phase 2 Successful", reg,
	    PCI_PL16G_STAT_EQ_P2S);
	onoff("Equalization 16.0 GT/s Phase 3 Successful", reg,
	    PCI_PL16G_STAT_EQ_P3S);

	reg = regs[o2i(extcapoff + PCI_PL16G_LDPMS)];
	printf("    Local Data Parity Mismatch Status register: 0x%08x\n",
	    reg);

	reg = regs[o2i(extcapoff + PCI_PL16G_FRDPMS)];
	printf("    First Retimer Data Parity Mismatch Status register:"
	    " 0x%08x\n", reg);

	reg = regs[o2i(extcapoff + PCI_PL16G_SRDPMS)];
	printf("    Second Retimer Data Parity Mismatch Status register:"
	    " 0x%08x\n", reg);

	if (pci_conf_find_cap(regs, PCI_CAP_PCIEXPRESS, &pcie_capoff) == 0)
		return; /* error */

	reg = regs[o2i(pcie_capoff + PCIE_LCAP)];
	lwidth = PCIREG_SHIFTOUT(reg, PCIE_LCAP_MAX_WIDTH);

	for (i = 0; i < lwidth;) {
		reg = regs[o2i(extcapoff + PCI_PL16G_LEC + i)];

		for (j = 0; j < 4; j++) {
			pcireg_t up, down;

			down = reg & 0x0000000f;
			up = (reg >> 4) & 0x0000000f;
			printf("      Lane %d downstream: ", i);
			pci_print_pcie_link_preset_preshoot_deemphasis(down);
			printf("\n      Lane %d upstream:   ", i);
			pci_print_pcie_link_preset_preshoot_deemphasis(up);
			printf("\n");

			reg >>= 8;
			i++;
			if (i >= lwidth)
				break;
		}
	}
}

static const char * const pcie_receive_number_dp[] = {
	[0] = ("Broadcast "
	    "(Downstream Port Receiver and all Retimer Pseudo Port Receiver)"),
	[1] =  "Rx(A) (Downstream Port Receiver)",
	[2] = "Rx(B) (Retimer X or Z Upstream Pseudo Port Receiver)",
	[3] = "Rx(C) (Retimer X or Z Downstream Pseudo Port Receiver)",
	[4] = "Rx(D) (Retimer Y Upstream Pseudo Port Receiver)",
	[5] = "Rx(E) (Retimer Y Downstream Pseudo Port Receiver)",
	[6] = "Reserved",
	[7] = "Reserved"
};

static const char * const pcie_receive_number_up[] = {
	"Broadcast (Upstream Port Receiver)",
	"Reserved",
	"Reserved",
	"Reserved",
	"Reserved",
	"Reserved",
	"Rx(F) (Upstream Port Receiver)",
	"Reserved"
};

/*
 * Print PCI_LMR_LANECSR. This function is used for both control and status
 * register. The reg argument in the lower 16bit has the control or status
 * register. The encoding is the same except the receive number, so use _LCTL_
 * macro.
 */
static void
pci_conf_print_lmr_lcsr(pcireg_t reg, bool up, bool dp)
{
	int rnum;

 	printf("      Receive Number: ");
	rnum = PCIREG_SHIFTOUT(reg, PCI_LMR_LCTL_RNUM);
	if (up)
		printf("%s\n", pcie_receive_number_up[rnum]);
	else if (dp)
		printf("%s\n", pcie_receive_number_dp[rnum]);
	else
		printf("%x\n", rnum);

	printf("      Margin Type: %x\n",
	    PCIREG_SHIFTOUT(reg, PCI_LMR_LCTL_MTYPE));
	printf("      Usage Model: %s\n",
	    (PCIREG_SHIFTOUT(reg, PCI_LMR_LCTL_UMODEL) == 0)
	    ? "Lane Margining at Receiver" : "Reserved Encoding");
	printf("      Margin Payload: 0x%02x\n",
	    PCIREG_SHIFTOUT(reg, PCI_LMR_LCTL_MPAYLOAD));
}

static void
pci_conf_print_lmr_cap(const pcireg_t *regs, int extcapoff)
{
	pcireg_t reg, lwidth;
	int pcie_capoff;
	int pcie_devtype;
	unsigned int i;
	bool up, dp;

	printf("\n  Lane Margining at the Receiver\n");
	reg = regs[o2i(extcapoff + PCI_LMR_PCAPSTAT)];
	printf("    Port Capability register: 0x%04x\n", reg & 0xffff);
	onoff("Margining uses Driver Software", reg, PCI_LMR_PCAP_MUDS);
	printf("    Port Status register: 0x%04x\n", (reg >> 16) & 0xffff);
	onoff("Margining Ready", reg, PCI_LMR_PSTAT_MR);
	onoff("Margining Software Ready", reg, PCI_LMR_PSTAT_MSR);

	if (pci_conf_find_cap(regs, PCI_CAP_PCIEXPRESS, &pcie_capoff) == 0)
		return; /* error */

	up = dp = false;
	reg = regs[o2i(pcie_capoff)];
	pcie_devtype = PCIE_XCAP_TYPE(reg);
	switch (pcie_devtype) {
	case PCIE_XCAP_TYPE_PCIE_DEV:	/* 0x0 */
	case PCIE_XCAP_TYPE_PCI_DEV:	/* 0x1 */
	case PCIE_XCAP_TYPE_UP:		/* 0x5 */
	case PCIE_XCAP_TYPE_PCIE2PCI:	/* 0x7 */
		up = true;
		break;
	case PCIE_XCAP_TYPE_RP:		/* 0x4 */
	case PCIE_XCAP_TYPE_DOWN:	/* 0x6 */
		dp = true;
		break;
	default:
		printf("neither upstream nor downstream?(%x)\n", pcie_devtype);
		break;
	}

	reg = regs[o2i(pcie_capoff + PCIE_LCAP)];
	lwidth = PCIREG_SHIFTOUT(reg, PCIE_LCAP_MAX_WIDTH);

	for (i = 0; i < lwidth; i++) {
		pcireg_t lctl, lstat;

		reg = regs[o2i(extcapoff + PCI_LMR_LANECSR + (i * 4))];

		lctl = reg & 0xffff;
		printf("    Lane %d control: 0x%04x\n", i, lctl);
		pci_conf_print_lmr_lcsr(lctl, up, dp);

		lstat = (reg >> 16) & 0xffff;
		printf("    Lane %d status: 0x%04x\n", i, lstat);
		pci_conf_print_lmr_lcsr(lstat, up, dp);
	}
}

/* XXX pci_conf_print_hierarchyid_cap */
/* XXX pci_conf_print_npem_cap */

#undef	MS
#undef	SM
#undef	RW

static struct {
	pcireg_t cap;
	const char *name;
	void (*printfunc)(const pcireg_t *, int);
} pci_extcaptab[] = {
	{ 0,			"reserved",
	  NULL },
	{ PCI_EXTCAP_AER,	"Advanced Error Reporting",
	  pci_conf_print_aer_cap },
	{ PCI_EXTCAP_VC,	"Virtual Channel",
	  pci_conf_print_vc_cap },
	{ PCI_EXTCAP_SERNUM,	"Device Serial Number",
	  pci_conf_print_sernum_cap },
	{ PCI_EXTCAP_PWRBDGT,	"Power Budgeting",
	  pci_conf_print_pwrbdgt_cap },
	{ PCI_EXTCAP_RCLINK_DCL,"Root Complex Link Declaration",
	  pci_conf_print_rclink_dcl_cap },
	{ PCI_EXTCAP_RCLINK_CTL,"Root Complex Internal Link Control",
	  NULL },
	{ PCI_EXTCAP_RCEC_ASSOC,"Root Complex Event Collector Association",
	  pci_conf_print_rcec_assoc_cap },
	{ PCI_EXTCAP_MFVC,	"Multi-Function Virtual Channel",
	  NULL },
	{ PCI_EXTCAP_VC2,	"Virtual Channel",
	  NULL },
	{ PCI_EXTCAP_RCRB,	"RCRB Header",
	  NULL },
	{ PCI_EXTCAP_VENDOR,	"Vendor Unique",
	  NULL },
	{ PCI_EXTCAP_CAC,	"Configuration Access Correction",
	  NULL },
	{ PCI_EXTCAP_ACS,	"Access Control Services",
	  pci_conf_print_acs_cap },
	{ PCI_EXTCAP_ARI,	"Alternative Routing-ID Interpretation",
	  pci_conf_print_ari_cap },
	{ PCI_EXTCAP_ATS,	"Address Translation Services",
	  pci_conf_print_ats_cap },
	{ PCI_EXTCAP_SRIOV,	"Single Root IO Virtualization",
	  pci_conf_print_sriov_cap },
	{ PCI_EXTCAP_MRIOV,	"Multiple Root IO Virtualization",
	  NULL },
	{ PCI_EXTCAP_MCAST,	"Multicast",
	  pci_conf_print_multicast_cap },
	{ PCI_EXTCAP_PAGE_REQ,	"Page Request",
	  pci_conf_print_page_req_cap },
	{ PCI_EXTCAP_AMD,	"Reserved for AMD",
	  NULL },
	{ PCI_EXTCAP_RESIZBAR,	"Resizable BAR",
	  pci_conf_print_resizbar_cap },
	{ PCI_EXTCAP_DPA,	"Dynamic Power Allocation",
	  pci_conf_print_dpa_cap },
	{ PCI_EXTCAP_TPH_REQ,	"TPH Requester",
	  pci_conf_print_tph_req_cap },
	{ PCI_EXTCAP_LTR,	"Latency Tolerance Reporting",
	  pci_conf_print_ltr_cap },
	{ PCI_EXTCAP_SEC_PCIE,	"Secondary PCI Express",
	  pci_conf_print_sec_pcie_cap },
	{ PCI_EXTCAP_PMUX,	"Protocol Multiplexing",
	  NULL },
	{ PCI_EXTCAP_PASID,	"Process Address Space ID",
	  pci_conf_print_pasid_cap },
	{ PCI_EXTCAP_LNR,	"LN Requester",
	  pci_conf_print_lnr_cap },
	{ PCI_EXTCAP_DPC,	"Downstream Port Containment",
	  pci_conf_print_dpc_cap },
	{ PCI_EXTCAP_L1PM,	"L1 PM Substates",
	  pci_conf_print_l1pm_cap },
	{ PCI_EXTCAP_PTM,	"Precision Time Measurement",
	  pci_conf_print_ptm_cap },
	{ PCI_EXTCAP_MPCIE,	"M-PCIe",
	  NULL },
	{ PCI_EXTCAP_FRSQ,	"Function Reading Status Queueing",
	  NULL },
	{ PCI_EXTCAP_RTR,	"Readiness Time Reporting",
	  NULL },
	{ PCI_EXTCAP_DESIGVNDSP, "Designated Vendor-Specific",
	  NULL },
	{ PCI_EXTCAP_VF_RESIZBAR, "VF Resizable BARs",
	  NULL },
	{ PCI_EXTCAP_DLF,	"Data link Feature", pci_conf_print_dlf_cap },
	{ PCI_EXTCAP_PL16G,	"Physical Layer 16.0 GT/s",
	  pci_conf_print_pl16g_cap },
	{ PCI_EXTCAP_LMR,	"Lane Margining at the Receiver",
	  pci_conf_print_lmr_cap },
	{ PCI_EXTCAP_HIERARCHYID, "Hierarchy ID",
	  NULL },
	{ PCI_EXTCAP_NPEM,	"Native PCIe Enclosure Management",
	  NULL },
	{ PCI_EXTCAP_PL32G,	"Physical Layer 32.0 GT/s",
	  NULL },
	{ PCI_EXTCAP_AP,	"Alternate Protocol",
	  NULL },
	{ PCI_EXTCAP_SFI,	"System Firmware Intermediary",
	  NULL },
};

static int
pci_conf_find_extcap(const pcireg_t *regs, unsigned int capid, int *offsetp)
{
	int off;
	pcireg_t rval;

	for (off = PCI_EXTCAPLIST_BASE;
	     off != 0;
	     off = PCI_EXTCAPLIST_NEXT(rval)) {
		rval = regs[o2i(off)];
		if (capid == PCI_EXTCAPLIST_CAP(rval)) {
			if (offsetp != NULL)
				*offsetp = off;
			return 1;
		}
	}
	return 0;
}

static void
pci_conf_print_extcaplist(
#ifdef _KERNEL
    pci_chipset_tag_t pc, pcitag_t tag,
#endif
    const pcireg_t *regs)
{
	int off;
	pcireg_t foundcap;
	pcireg_t rval;
	bool foundtable[__arraycount(pci_extcaptab)];
	unsigned int i;

	/* Check Extended capability structure */
	off = PCI_EXTCAPLIST_BASE;
	rval = regs[o2i(off)];
	if (rval == 0xffffffff || rval == 0)
		return;

	/* Clear table */
	for (i = 0; i < __arraycount(pci_extcaptab); i++)
		foundtable[i] = false;

	/* Print extended capability register's offset and the type first */
	for (;;) {
		printf("  Extended Capability Register at 0x%02x\n", off);

		foundcap = PCI_EXTCAPLIST_CAP(rval);
		printf("    type: 0x%04x (", foundcap);
		if (foundcap < __arraycount(pci_extcaptab)) {
			printf("%s)\n", pci_extcaptab[foundcap].name);
			/* Mark as found */
			foundtable[foundcap] = true;
		} else
			printf("unknown)\n");
		printf("    version: %d\n", PCI_EXTCAPLIST_VERSION(rval));

		off = PCI_EXTCAPLIST_NEXT(rval);
		if (off == 0)
			break;
		else if (off <= PCI_CONF_SIZE) {
			printf("    next pointer: 0x%03x (incorrect)\n", off);
			return;
		}
		rval = regs[o2i(off)];
	}

	/*
	 * And then, print the detail of each capability registers
	 * in capability value's order.
	 */
	for (i = 0; i < __arraycount(pci_extcaptab); i++) {
		if (foundtable[i] == false)
			continue;

		/*
		 * The type was found. Search capability list again and
		 * print all capabilities that the capability type is
		 * the same.
		 */
		if (pci_conf_find_extcap(regs, i, &off) == 0)
			continue;
		rval = regs[o2i(off)];
		if ((PCI_EXTCAPLIST_VERSION(rval) <= 0)
		    || (pci_extcaptab[i].printfunc == NULL))
			continue;

		pci_extcaptab[i].printfunc(regs, off);

	}
}

/* Print the Secondary Status Register. */
static void
pci_conf_print_ssr(pcireg_t rval)
{
	pcireg_t devsel;

	printf("    Secondary status register: 0x%04x\n", rval); /* XXX bits */
	onoff("66 MHz capable", rval, __BIT(5));
	onoff("User Definable Features (UDF) support", rval, __BIT(6));
	onoff("Fast back-to-back capable", rval, __BIT(7));
	onoff("Data parity error detected", rval, __BIT(8));

	printf("      DEVSEL timing: ");
	devsel = PCIREG_SHIFTOUT(rval, __BITS(10, 9));
	switch (devsel) {
	case 0:
		printf("fast");
		break;
	case 1:
		printf("medium");
		break;
	case 2:
		printf("slow");
		break;
	default:
		printf("unknown/reserved");	/* XXX */
		break;
	}
	printf(" (0x%x)\n", devsel);

	onoff("Signalled target abort", rval, __BIT(11));
	onoff("Received target abort", rval, __BIT(12));
	onoff("Received master abort", rval, __BIT(13));
	onoff("Received system error", rval, __BIT(14));
	onoff("Detected parity error", rval, __BIT(15));
}

static void
pci_conf_print_type0(
#ifdef _KERNEL
    pci_chipset_tag_t pc, pcitag_t tag,
#endif
    const pcireg_t *regs)
{
	int off, width;
	pcireg_t rval;
	const char *str;

	for (off = PCI_MAPREG_START; off < PCI_MAPREG_END; off += width) {
#ifdef _KERNEL
		width = pci_conf_print_bar(pc, tag, regs, off, NULL);
#else
		width = pci_conf_print_bar(regs, off, NULL);
#endif
	}

	printf("    Cardbus CIS Pointer: 0x%08x\n",
	    regs[o2i(PCI_CARDBUS_CIS_REG)]);

	rval = regs[o2i(PCI_SUBSYS_ID_REG)];
	printf("    Subsystem vendor ID: 0x%04x\n", PCI_VENDOR(rval));
	printf("    Subsystem ID: 0x%04x\n", PCI_PRODUCT(rval));

	rval = regs[o2i(PCI_MAPREG_ROM)];
	printf("    Expansion ROM Base Address Register: 0x%08x\n", rval);
	printf("      base: 0x%08x\n", (uint32_t)PCI_MAPREG_ROM_ADDR(rval));
	onoff("Expansion ROM Enable", rval, PCI_MAPREG_ROM_ENABLE);
	printf("      Validation Status: ");
	switch (PCIREG_SHIFTOUT(rval, PCI_MAPREG_ROM_VALID_STAT)) {
	case PCI_MAPREG_ROM_VSTAT_NOTSUPP:
		str = "Validation not supported";
		break;
	case PCI_MAPREG_ROM_VSTAT_INPROG:
		str = "Validation in Progress";
		break;
	case PCI_MAPREG_ROM_VSTAT_VPASS:
		str = "Validation Pass. "
		    "Valid contents, trust test was not performed";
		break;
	case PCI_MAPREG_ROM_VSTAT_VPASSTRUST:
		str = "Validation Pass. Valid and trusted contents";
		break;
	case PCI_MAPREG_ROM_VSTAT_VFAIL:
		str = "Validation Fail. Invalid contents";
		break;
	case PCI_MAPREG_ROM_VSTAT_VFAILUNTRUST:
		str = "Validation Fail. Valid but untrusted contents";
		break;
	case PCI_MAPREG_ROM_VSTAT_WPASS:
		str = "Warning Pass. Validation passed with warning. "
		    "Valid contents, trust test was not performed";
		break;
	case PCI_MAPREG_ROM_VSTAT_WPASSTRUST:
		str = "Warning Pass. Validation passed with warning. "
		    "Valid and trusted contents";
		break;
	}
	printf("%s\n", str);
	printf("      Validation Details: 0x%x\n",
	    PCIREG_SHIFTOUT(rval, PCI_MAPREG_ROM_VALID_DETAIL));

	if (regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
		printf("    Capability list pointer: 0x%02x\n",
		    PCI_CAPLIST_PTR(regs[o2i(PCI_CAPLISTPTR_REG)]));
	else
		printf("    Reserved @ 0x34: 0x%08x\n", regs[o2i(0x34)]);

	printf("    Reserved @ 0x38: 0x%08x\n", regs[o2i(0x38)]);

	rval = regs[o2i(PCI_INTERRUPT_REG)];
	printf("    Maximum Latency: 0x%02x\n", PCI_MAX_LAT(rval));
	printf("    Minimum Grant: 0x%02x\n", PCI_MIN_GNT(rval));
	printf("    Interrupt pin: 0x%02x ", PCI_INTERRUPT_PIN(rval));
	switch (PCI_INTERRUPT_PIN(rval)) {
	case PCI_INTERRUPT_PIN_NONE:
		printf("(none)");
		break;
	case PCI_INTERRUPT_PIN_A:
		printf("(pin A)");
		break;
	case PCI_INTERRUPT_PIN_B:
		printf("(pin B)");
		break;
	case PCI_INTERRUPT_PIN_C:
		printf("(pin C)");
		break;
	case PCI_INTERRUPT_PIN_D:
		printf("(pin D)");
		break;
	default:
		printf("(? ? ?)");
		break;
	}
	printf("\n");
	printf("    Interrupt line: 0x%02x\n", PCI_INTERRUPT_LINE(rval));
}

static void
pci_conf_print_type1(
#ifdef _KERNEL
    pci_chipset_tag_t pc, pcitag_t tag,
#endif
    const pcireg_t *regs)
{
	int off, width;
	pcireg_t rval, csreg;
	uint32_t base, limit;
	uint32_t base_h, limit_h;
	uint64_t pbase, plimit;
	int use_upper;

	/*
	 * This layout was cribbed from the TI PCI2030 PCI-to-PCI
	 * Bridge chip documentation, and may not be correct with
	 * respect to various standards. (XXX)
	 */

	for (off = 0x10; off < 0x18; off += width) {
#ifdef _KERNEL
		width = pci_conf_print_bar(pc, tag, regs, off, NULL);
#else
		width = pci_conf_print_bar(regs, off, NULL);
#endif
	}

	rval = regs[o2i(PCI_BRIDGE_BUS_REG)];
	printf("    Primary bus number: 0x%02x\n",
	    PCI_BRIDGE_BUS_NUM_PRIMARY(rval));
	printf("    Secondary bus number: 0x%02x\n",
	    PCI_BRIDGE_BUS_NUM_SECONDARY(rval));
	printf("    Subordinate bus number: 0x%02x\n",
	    PCI_BRIDGE_BUS_NUM_SUBORDINATE(rval));
	printf("    Secondary bus latency timer: 0x%02x\n",
	    PCI_BRIDGE_BUS_SEC_LATTIMER_VAL(rval));

	rval = regs[o2i(PCI_BRIDGE_STATIO_REG)];
	pci_conf_print_ssr(PCIREG_SHIFTOUT(rval, __BITS(31, 16)));

	/* I/O region */
	printf("    I/O region:\n");
	printf("      base register:  0x%02x\n", (rval >> 0) & 0xff);
	printf("      limit register: 0x%02x\n", (rval >> 8) & 0xff);
	if (PCI_BRIDGE_IO_32BITS(rval))
		use_upper = 1;
	else
		use_upper = 0;
	onoff("32bit I/O", rval, use_upper);
	base = PCI_BRIDGE_STATIO_IOBASE_ADDR(rval);
	limit = PCI_BRIDGE_STATIO_IOLIMIT_ADDR(rval);

	rval = regs[o2i(PCI_BRIDGE_IOHIGH_REG)];
	base_h = PCIREG_SHIFTOUT(rval, PCI_BRIDGE_IOHIGH_BASE);
	limit_h = PCIREG_SHIFTOUT(rval, PCI_BRIDGE_IOHIGH_LIMIT);
	printf("      base upper 16 bits register:  0x%04x\n", base_h);
	printf("      limit upper 16 bits register: 0x%04x\n", limit_h);

	if (use_upper == 1) {
		base |= base_h << 16;
		limit |= limit_h << 16;
	}
	if (base < limit) {
		if (use_upper == 1)
			printf("      range: 0x%08x-0x%08x\n", base, limit);
		else
			printf("      range: 0x%04x-0x%04x\n", base, limit);
	} else
		printf("      range:  not set\n");

	/* Non-prefetchable memory region */
	rval = regs[o2i(PCI_BRIDGE_MEMORY_REG)];
	printf("    Memory region:\n");
	printf("      base register:  0x%04hx\n",
	    (uint16_t)PCIREG_SHIFTOUT(rval, PCI_BRIDGE_MEMORY_BASE));
	printf("      limit register: 0x%04hx\n",
	    (uint16_t)PCIREG_SHIFTOUT(rval, PCI_BRIDGE_MEMORY_LIMIT));
	base = PCI_BRIDGE_MEMORY_BASE_ADDR(rval);
	limit = PCI_BRIDGE_MEMORY_LIMIT_ADDR(rval);
	if (base < limit)
		printf("      range: 0x%08x-0x%08x\n", base, limit);
	else
		printf("      range: not set\n");

	/* Prefetchable memory region */
	rval = regs[o2i(PCI_BRIDGE_PREFETCHMEM_REG)];
	printf("    Prefetchable memory region:\n");
	printf("      base register:  0x%04x\n",
	    (rval >> 0) & 0xffff);
	printf("      limit register: 0x%04x\n",
	    (rval >> 16) & 0xffff);
	base_h = regs[o2i(PCI_BRIDGE_PREFETCHBASEUP32_REG)];
	limit_h = regs[o2i(PCI_BRIDGE_PREFETCHLIMITUP32_REG)];
	printf("      base upper 32 bits register:  0x%08x\n",
	    base_h);
	printf("      limit upper 32 bits register: 0x%08x\n",
	    limit_h);
	if (PCI_BRIDGE_PREFETCHMEM_64BITS(rval))
		use_upper = 1;
	else
		use_upper = 0;
	onoff("64bit memory address", rval, use_upper);
	pbase = PCI_BRIDGE_PREFETCHMEM_BASE_ADDR(rval);
	plimit = PCI_BRIDGE_PREFETCHMEM_LIMIT_ADDR(rval);
	if (use_upper == 1) {
		pbase |= (uint64_t)base_h << 32;
		plimit |= (uint64_t)limit_h << 32;
	}
	if (pbase < plimit) {
		if (use_upper == 1)
			printf("      range: 0x%016" PRIx64 "-0x%016" PRIx64
			    "\n", pbase, plimit);
		else
			printf("      range: 0x%08x-0x%08x\n",
			    (uint32_t)pbase, (uint32_t)plimit);
	} else
		printf("      range: not set\n");

	csreg = regs[o2i(PCI_COMMAND_STATUS_REG)];
	if (csreg & PCI_STATUS_CAPLIST_SUPPORT)
		printf("    Capability list pointer: 0x%02x\n",
		    PCI_CAPLIST_PTR(regs[o2i(PCI_CAPLISTPTR_REG)]));
	else
		printf("    Reserved @ 0x34: 0x%08x\n", regs[o2i(0x34)]);

	printf("    Expansion ROM Base Address: 0x%08x\n",
	    regs[o2i(PCI_BRIDGE_EXPROMADDR_REG)]);

	rval = regs[o2i(PCI_INTERRUPT_REG)];
	printf("    Interrupt line: 0x%02x\n",
	    (rval >> 0) & 0xff);
	printf("    Interrupt pin: 0x%02x ",
	    (rval >> 8) & 0xff);
	switch ((rval >> 8) & 0xff) {
	case PCI_INTERRUPT_PIN_NONE:
		printf("(none)");
		break;
	case PCI_INTERRUPT_PIN_A:
		printf("(pin A)");
		break;
	case PCI_INTERRUPT_PIN_B:
		printf("(pin B)");
		break;
	case PCI_INTERRUPT_PIN_C:
		printf("(pin C)");
		break;
	case PCI_INTERRUPT_PIN_D:
		printf("(pin D)");
		break;
	default:
		printf("(? ? ?)");
		break;
	}
	printf("\n");
	rval = regs[o2i(PCI_BRIDGE_CONTROL_REG)];
	printf("    Bridge control register: 0x%04hx\n",
	    (uint16_t)PCIREG_SHIFTOUT(rval, PCI_BRIDGE_CONTROL));
	onoff("Parity error response", rval, PCI_BRIDGE_CONTROL_PERE);
	onoff("Secondary SERR forwarding", rval, PCI_BRIDGE_CONTROL_SERR);
	onoff("ISA enable", rval, PCI_BRIDGE_CONTROL_ISA);
	onoff("VGA enable", rval, PCI_BRIDGE_CONTROL_VGA);
	/*
	 * VGA 16bit decode bit has meaning if the VGA enable bit or the
	 * VGA Palette Snoop Enable bit is set.
	 */
	if (((rval & PCI_BRIDGE_CONTROL_VGA) != 0)
	    || ((csreg & PCI_COMMAND_PALETTE_ENABLE) != 0))
		onoff("VGA 16bit enable", rval, PCI_BRIDGE_CONTROL_VGA16);
	onoff("Master abort reporting", rval, PCI_BRIDGE_CONTROL_MABRT);
	onoff("Secondary bus reset", rval, PCI_BRIDGE_CONTROL_SECBR);
	onoff("Fast back-to-back enable", rval, PCI_BRIDGE_CONTROL_SECFASTB2B);
	onoff("Primary Discard Timer", rval,
	    PCI_BRIDGE_CONTROL_PRI_DISC_TIMER);
	onoff("Secondary Discard Timer",
	    rval, PCI_BRIDGE_CONTROL_SEC_DISC_TIMER);
	onoff("Discard Timer Status", rval,
	    PCI_BRIDGE_CONTROL_DISC_TIMER_STAT);
	onoff("Discard Timer SERR# Enable", rval,
	    PCI_BRIDGE_CONTROL_DISC_TIMER_SERR);
}

static void
pci_conf_print_type2(
#ifdef _KERNEL
    pci_chipset_tag_t pc, pcitag_t tag,
#endif
    const pcireg_t *regs)
{
	pcireg_t rval;

	/*
	 * XXX these need to be printed in more detail, need to be
	 * XXX checked against specs/docs, etc.
	 *
	 * This layout was cribbed from the TI PCI1420 PCI-to-CardBus
	 * controller chip documentation, and may not be correct with
	 * respect to various standards. (XXX)
	 */

#ifdef _KERNEL
	pci_conf_print_bar(pc, tag, regs, 0x10,
	    "CardBus socket/ExCA registers");
#else
	pci_conf_print_bar(regs, 0x10, "CardBus socket/ExCA registers");
#endif

	/* Capability list pointer and secondary status register */
	rval = regs[o2i(PCI_CARDBUS_CAPLISTPTR_REG)];
	if (regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
		printf("    Capability list pointer: 0x%02x\n",
		    PCI_CAPLIST_PTR(rval));
	else
		printf("    Reserved @ 0x14: 0x%04x\n",
		       PCIREG_SHIFTOUT(rval, __BITS(15, 0)));
	pci_conf_print_ssr(PCIREG_SHIFTOUT(rval, __BITS(31, 16)));

	rval = regs[o2i(PCI_BRIDGE_BUS_REG)];
	printf("    PCI bus number: 0x%02x\n",
	    (rval >> 0) & 0xff);
	printf("    CardBus bus number: 0x%02x\n",
	    (rval >> 8) & 0xff);
	printf("    Subordinate bus number: 0x%02x\n",
	    (rval >> 16) & 0xff);
	printf("    CardBus latency timer: 0x%02x\n",
	    (rval >> 24) & 0xff);

	/* XXX Print more prettily */
	printf("    CardBus memory region 0:\n");
	printf("      base register:  0x%08x\n", regs[o2i(0x1c)]);
	printf("      limit register: 0x%08x\n", regs[o2i(0x20)]);
	printf("    CardBus memory region 1:\n");
	printf("      base register:  0x%08x\n", regs[o2i(0x24)]);
	printf("      limit register: 0x%08x\n", regs[o2i(0x28)]);
	printf("    CardBus I/O region 0:\n");
	printf("      base register:  0x%08x\n", regs[o2i(0x2c)]);
	printf("      limit register: 0x%08x\n", regs[o2i(0x30)]);
	printf("    CardBus I/O region 1:\n");
	printf("      base register:  0x%08x\n", regs[o2i(0x34)]);
	printf("      limit register: 0x%08x\n", regs[o2i(0x38)]);

	rval = regs[o2i(PCI_INTERRUPT_REG)];
	printf("    Interrupt line: 0x%02x\n",
	    (rval >> 0) & 0xff);
	printf("    Interrupt pin: 0x%02x ",
	    (rval >> 8) & 0xff);
	switch ((rval >> 8) & 0xff) {
	case PCI_INTERRUPT_PIN_NONE:
		printf("(none)");
		break;
	case PCI_INTERRUPT_PIN_A:
		printf("(pin A)");
		break;
	case PCI_INTERRUPT_PIN_B:
		printf("(pin B)");
		break;
	case PCI_INTERRUPT_PIN_C:
		printf("(pin C)");
		break;
	case PCI_INTERRUPT_PIN_D:
		printf("(pin D)");
		break;
	default:
		printf("(? ? ?)");
		break;
	}
	printf("\n");
	rval = (regs[o2i(PCI_BRIDGE_CONTROL_REG)] >> 16) & 0xffff;
	printf("    Bridge control register: 0x%04x\n", rval);
	onoff("Parity error response", rval, __BIT(0));
	onoff("SERR# enable", rval, __BIT(1));
	onoff("ISA enable", rval, __BIT(2));
	onoff("VGA enable", rval, __BIT(3));
	onoff("Master abort mode", rval, __BIT(5));
	onoff("Secondary (CardBus) bus reset", rval, __BIT(6));
	onoff("Functional interrupts routed by ExCA registers", rval,
	    __BIT(7));
	onoff("Memory window 0 prefetchable", rval, __BIT(8));
	onoff("Memory window 1 prefetchable", rval, __BIT(9));
	onoff("Write posting enable", rval, __BIT(10));

	rval = regs[o2i(0x40)];
	printf("    Subsystem vendor ID: 0x%04x\n", PCI_VENDOR(rval));
	printf("    Subsystem ID: 0x%04x\n", PCI_PRODUCT(rval));

#ifdef _KERNEL
	pci_conf_print_bar(pc, tag, regs, 0x44, "legacy-mode registers");
#else
	pci_conf_print_bar(regs, 0x44, "legacy-mode registers");
#endif
}

void
pci_conf_print(
#ifdef _KERNEL
    pci_chipset_tag_t pc, pcitag_t tag,
    void (*printfn)(pci_chipset_tag_t, pcitag_t, const pcireg_t *)
#else
    int pcifd, u_int bus, u_int dev, u_int func
#endif
    )
{
	pcireg_t *regs;
	int off, capoff, endoff, hdrtype;
	const char *type_name;
#ifdef _KERNEL
	void (*type_printfn)(pci_chipset_tag_t, pcitag_t, const pcireg_t *);
#else
	void (*type_printfn)(const pcireg_t *);
#endif

	regs = MALLOC(PCI_EXTCONF_SIZE);

	printf("PCI configuration registers:\n");

	for (off = 0; off < PCI_EXTCONF_SIZE; off += 4) {
#ifdef _KERNEL
		regs[o2i(off)] = pci_conf_read(pc, tag, off);
#else
		if (pcibus_conf_read(pcifd, bus, dev, func, off,
		    &regs[o2i(off)]) == -1)
			regs[o2i(off)] = 0;
#endif
	}

	/* common header */
	printf("  Common header:\n");
	pci_conf_print_regs(regs, 0, 16);

	printf("\n");
#ifdef _KERNEL
	pci_conf_print_common(pc, tag, regs);
#else
	pci_conf_print_common(regs);
#endif
	printf("\n");

	/* type-dependent header */
	hdrtype = PCI_HDRTYPE_TYPE(regs[o2i(PCI_BHLC_REG)]);
	switch (hdrtype) {		/* XXX make a table, eventually */
	case 0:
		/* Standard device header */
		type_name = "\"normal\" device";
		type_printfn = &pci_conf_print_type0;
		capoff = PCI_CAPLISTPTR_REG;
		endoff = 64;
		break;
	case 1:
		/* PCI-PCI bridge header */
		type_name = "PCI-PCI bridge";
		type_printfn = &pci_conf_print_type1;
		capoff = PCI_CAPLISTPTR_REG;
		endoff = 64;
		break;
	case 2:
		/* PCI-CardBus bridge header */
		type_name = "PCI-CardBus bridge";
		type_printfn = &pci_conf_print_type2;
		capoff = PCI_CARDBUS_CAPLISTPTR_REG;
		endoff = 72;
		break;
	default:
		type_name = NULL;
		type_printfn = 0;
		capoff = -1;
		endoff = 64;
		break;
	}
	printf("  Type %d ", hdrtype);
	if (type_name != NULL)
		printf("(%s) ", type_name);
	printf("header:\n");
	pci_conf_print_regs(regs, 16, endoff);
	printf("\n");
	if (type_printfn) {
#ifdef _KERNEL
		(*type_printfn)(pc, tag, regs);
#else
		(*type_printfn)(regs);
#endif
	} else
		printf("    Don't know how to pretty-print type %d header.\n",
		    hdrtype);
	printf("\n");

	/* capability list, if present */
	if ((regs[o2i(PCI_COMMAND_STATUS_REG)] & PCI_STATUS_CAPLIST_SUPPORT)
		&& (capoff > 0)) {
#ifdef _KERNEL
		pci_conf_print_caplist(pc, tag, regs, capoff);
#else
		pci_conf_print_caplist(regs, capoff);
#endif
		printf("\n");
	}

	/* device-dependent header */
	printf("  Device-dependent header:\n");
	pci_conf_print_regs(regs, endoff, PCI_CONF_SIZE);
#ifdef _KERNEL
	printf("\n");
	if (printfn)
		(*printfn)(pc, tag, regs);
	else
		printf("    Don't know how to pretty-print device-dependent header.\n");
#endif /* _KERNEL */

	if (regs[o2i(PCI_EXTCAPLIST_BASE)] == 0xffffffff ||
	    regs[o2i(PCI_EXTCAPLIST_BASE)] == 0)
		goto out;

	printf("\n");
#ifdef _KERNEL
	pci_conf_print_extcaplist(pc, tag, regs);
#else
	pci_conf_print_extcaplist(regs);
#endif
	printf("\n");

	/* Extended Configuration Space, if present */
	printf("  Extended Configuration Space:\n");
	pci_conf_print_regs(regs, PCI_EXTCAPLIST_BASE, PCI_EXTCONF_SIZE);

out:
	FREE(regs, PCI_EXTCONF_SIZE);
}