|
root / base / usr / src / cmd / pcieadm
pcieadm Plain Text 8789 lines 287.1 KB
   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
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2026 Oxide Computer Company
#

PROG= pcieadm

include ../Makefile.cmd
include ../Makefile.cmd.64
include ../Makefile.ctf

CFLAGS += $(CCVERBOSE)
CSTD = $(CSTD_GNU17)
LDLIBS += -ldevinfo -lpcidb -lofmt
LDLIBS += -L$(ROOT)/usr/lib/fm/64 -R/usr/lib/fm/64 -ltopo
OBJS = \
	pcieadm.o \
	pcieadm_bar.o \
	pcieadm_cfgspace.o \
	pcieadm_devs.o \
	bitext.o
ROOTCMDDIR = $(ROOTLIB)/pci

.KEEP_STATE:

$(PROG): $(OBJS)
	$(LINK.c) -o $@ $(OBJS) $(LDLIBS)
	$(POST_PROCESS)

%.o: %.c
	$(COMPILE.c) $<
	$(POST_PROCESS_O)

%.o: $(SRC)/common/bitext/%.c
	$(COMPILE.c) $<
	$(POST_PROCESS_O)

all: $(PROG)

install: all $(ROOTCMD)

clean:
	$(RM) $(OBJS)

include ../Makefile.targ
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2026 Oxide Computer Company
 */

/*
 * PCIe shenanigans
 *
 * Currently this implements several different views at seeing into PCIe devices
 * and is designed to (hopefully) replace pcitool and be a vector for new system
 * functionality such as dealing with multicast filtering, ACS, etc.
 *
 * While most subcommands have their own implementations, there are a couple of
 * things that are worth bearing in mind:
 *
 *  1) Where possible, prefer the use of libofmt. In particular, having good,
 *  parsable output is important. New subcommands should strive to meet that.
 *
 *  2) Because we're often processing binary data (and it's good hygiene),
 *  subcommands should make sure to drop privileges as early as they can by
 *  calling pcieadm_init_privs(). More on privileges below.
 *
 * Privilege Management
 * --------------------
 *
 * In an attempt to minimize privilege exposure, but to allow subcommands
 * flexibility when required (e.g. show-cfgspace needs full privs to read from
 * the kernel), we have two privilege sets that we maintain. One which is the
 * minimial privs, which basically is a set that has stripped everything. This
 * is 'pia_priv_min'. The second is one that allows a subcommand to add in
 * privileges that it requires which will be left in the permitted set. These
 * are in 'pia_priv_eff'. It's important to know that this set is always
 * intersected with what the user actually has, so this is not meant to be a way
 * for a caller to get more privileges than they already have.
 *
 * A subcommand is expected to call pcieadm_init_privs() once they have
 * processed enough arguments that they can set an upper bound on privileges.
 * It's worth noting that a subcommand will be executed in an already minimial
 * environment; however, we will have already set up a libdevinfo handle for
 * them, which should make the need to do much more not so bad.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <err.h>
#include <libdevinfo.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/pci_tools.h>
#include <sys/pci.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/debug.h>
#include <upanic.h>
#include <libgen.h>
#include <stdnoreturn.h>

#include "pcieadm.h"

pcieadm_t pcieadm;
const char *pcieadm_progname;

void
pcieadm_init_privs(pcieadm_t *pcip)
{
	static const char *msg = "attempted to re-initialize privileges";
	if (pcip->pia_priv_init == NULL) {
		upanic(msg, strlen(msg));
	}

	priv_intersect(pcip->pia_priv_init, pcip->pia_priv_eff);

	if (setppriv(PRIV_SET, PRIV_PERMITTED, pcieadm.pia_priv_eff) != 0) {
		err(EXIT_FAILURE, "failed to reduce privileges");
	}

	if (setppriv(PRIV_SET, PRIV_LIMIT, pcieadm.pia_priv_eff) != 0) {
		err(EXIT_FAILURE, "failed to reduce privileges");
	}

	priv_freeset(pcip->pia_priv_init);
	pcip->pia_priv_init = NULL;
}

void
pcieadm_indent(void)
{
	pcieadm.pia_indent += 2;
}

void
pcieadm_deindent(void)
{
	VERIFY3U(pcieadm.pia_indent, >, 0);
	pcieadm.pia_indent -= 2;
}

void
pcieadm_print(const char *fmt, ...)
{
	va_list ap;

	if (pcieadm.pia_indent > 0) {
		(void) printf("%*s", pcieadm.pia_indent, "");
	}

	va_start(ap, fmt);
	(void) vprintf(fmt, ap);
	va_end(ap);
}

void
pcieadm_ofmt_errx(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	verrx(EXIT_FAILURE, fmt, ap);
}

/*
 * We determine if a node is PCI in a two step process. The first is to see if
 * the node's name starts with pci, and has an additional character that
 * indicates it's not the synthetic root of the tree. However, the node name
 * changes for some classes of devices such as GPUs. As such, for those we try
 * to look at the compatible property and see if we have a pciexclass or
 * pciclass entry. We look specifically for the class to make sure that we don't
 * fall for the synthetic nodes that have a compatible property of
 * 'pciex_root_complex'.
 *
 * The compatible property is a single string that is actually a compressed
 * string. That is, there are multiple strings concatenated together in a single
 * pointer.
 */
static boolean_t
pcieadm_di_node_is_pci(di_node_t node)
{
	const char *name;
	char *compat;
	int nents;

	name = di_node_name(node);
	if (strncmp("pci", name, 3) == 0) {
		return (name[3] != '\0');
	}

	nents = di_prop_lookup_strings(DDI_DEV_T_ANY, node, "compatible",
	    &compat);
	if (nents <= 0) {
		return (B_FALSE);
	}

	for (int i = 0; i < nents; i++) {
		if (strncmp("pciclass,", compat, strlen("pciclass,")) == 0 ||
		    strncmp("pciexclass,", compat, strlen("pciexclass,")) ==
		    0) {
			return (B_TRUE);
		}

		compat += strlen(compat) + 1;
	}

	return (B_FALSE);
}

static int
pcieadm_di_walk_cb(di_node_t node, void *arg)
{
	pcieadm_di_walk_t *walk = arg;

	if (!pcieadm_di_node_is_pci(node)) {
		return (DI_WALK_CONTINUE);
	}

	return (walk->pdw_func(node, walk->pdw_arg));
}

static di_node_t
pcieadm_di_root(pcieadm_t *pcip)
{
	if (pcip->pia_root == DI_NODE_NIL) {
		pcip->pia_root = di_init("/", DINFOCPYALL);
		if (pcip->pia_root == DI_NODE_NIL) {
			err(EXIT_FAILURE, "failed to initialize devinfo tree");
		}
	}

	return (pcip->pia_root);
}

void
pcieadm_di_walk(pcieadm_t *pcip, pcieadm_di_walk_t *arg)
{
	(void) di_walk_node(pcieadm_di_root(pcip), DI_WALK_CLDFIRST, arg,
	    pcieadm_di_walk_cb);
}

/*
 * Attempt to find the nexus that corresponds to this device. To do this, we
 * walk up and walk the minors until we find a "reg" minor.
 */
void
pcieadm_find_nexus(pcieadm_t *pia)
{
	di_node_t cur;

	for (cur = di_parent_node(pia->pia_devi); cur != DI_NODE_NIL;
	    cur = di_parent_node(cur)) {
		di_minor_t minor = DI_MINOR_NIL;

		while ((minor = di_minor_next(cur, minor)) != DI_MINOR_NIL) {
			if (di_minor_spectype(minor) == S_IFCHR &&
			    strcmp(di_minor_name(minor), "reg") == 0) {
				pia->pia_nexus = cur;
				return;
			}
		}
	}
}

static int
pcieadm_find_dip_cb(di_node_t node, void *arg)
{
	char *path = NULL, *driver;
	char dinst[128], bdf[128], altbdf[128];
	int inst, nprop, *regs;
	pcieadm_t *pia = arg;

	path = di_devfs_path(node);
	if (path == NULL) {
		err(EXIT_FAILURE, "failed to construct devfs path for node: "
		    "%s", di_node_name(node));
	}

	driver = di_driver_name(node);
	inst = di_instance(node);
	if (driver != NULL && inst != -1) {
		(void) snprintf(dinst, sizeof (dinst), "%s%d", driver, inst);
	}

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &regs);
	if (nprop <= 0) {
		errx(EXIT_FAILURE, "failed to lookup regs array for %s",
		    path);
	}
	(void) snprintf(bdf, sizeof (bdf), "%x/%x/%x", PCI_REG_BUS_G(regs[0]),
	    PCI_REG_DEV_G(regs[0]), PCI_REG_FUNC_G(regs[0]));
	(void) snprintf(altbdf, sizeof (altbdf), "%02x/%02x/%02x",
	    PCI_REG_BUS_G(regs[0]), PCI_REG_DEV_G(regs[0]),
	    PCI_REG_FUNC_G(regs[0]));

	if (strcmp(pia->pia_devstr, path) == 0 ||
	    strcmp(pia->pia_devstr, bdf) == 0 ||
	    strcmp(pia->pia_devstr, altbdf) == 0 ||
	    (driver != NULL && inst != -1 &&
	    strcmp(pia->pia_devstr, dinst) == 0)) {
		if (pia->pia_devi != DI_NODE_NIL) {
			errx(EXIT_FAILURE, "device name matched two device "
			    "nodes: %s and %s", di_node_name(pia->pia_devi),
			    di_node_name(node));
		}

		pia->pia_devi = node;
	}

	if (path != NULL) {
		di_devfs_path_free(path);
	}

	return (DI_WALK_CONTINUE);
}

void
pcieadm_find_dip(pcieadm_t *pcip, const char *device)
{
	pcieadm_di_walk_t walk;

	/*
	 * If someone specifies /devices, just skip over it.
	 */
	pcip->pia_devstr = device;
	if (strncmp("/devices", device, strlen("/devices")) == 0) {
		pcip->pia_devstr += strlen("/devices");
	}

	pcip->pia_devi = DI_NODE_NIL;
	walk.pdw_arg = pcip;
	walk.pdw_func = pcieadm_find_dip_cb;
	pcieadm_di_walk(pcip, &walk);

	if (pcip->pia_devi == DI_NODE_NIL) {
		errx(EXIT_FAILURE, "failed to find device node %s", device);
	}

	pcip->pia_nexus = DI_NODE_NIL;
	pcieadm_find_nexus(pcip);
	if (pcip->pia_nexus == DI_NODE_NIL) {
		errx(EXIT_FAILURE, "failed to find nexus for %s", device);
	}
}

typedef struct pcieadm_cfgspace_file {
	int pcfi_fd;
} pcieadm_cfgspace_file_t;

static boolean_t
pcieadm_pop_cfgspace_file(uint32_t off, uint8_t len, void *buf, void *arg)
{
	uint32_t bufoff = 0;
	pcieadm_cfgspace_file_t *pcfi = arg;

	while (len > 0) {
		ssize_t ret = pread(pcfi->pcfi_fd, buf + bufoff, len, off);
		if (ret < 0) {
			err(EXIT_FAILURE, "failed to read %u bytes at %"
			    PRIu32, len, off);
		} else if (ret == 0) {
			warnx("hit unexpected EOF reading cfgspace from file "
			    "at offest %" PRIu32 ", still wanted to read %u "
			    "bytes", off, len);
			return (B_FALSE);
		} else {
			len -= ret;
			off += ret;
			bufoff += ret;
		}

	}

	return (B_TRUE);
}

static noreturn boolean_t
pcieadm_pop_bar_notsup(uint8_t bar, uint8_t len, uint64_t off, void *buf,
    void *arg, boolean_t write)
{
	errx(EXIT_FAILURE, "encountered unsupported request to %s %u bytes "
	    "from BAR %u at offset %" PRIx64, write ? "write" : "read", len,
	    bar, off);
}

static const pcieadm_ops_t pcieadm_file_ops = {
	.pop_cfg = pcieadm_pop_cfgspace_file,
	.pop_bar = pcieadm_pop_bar_notsup
};

void
pcieadm_init_ops_file(pcieadm_t *pcip, const char *path,
    const pcieadm_ops_t **opsp, void **arg)
{
	int fd;
	struct stat st;
	pcieadm_cfgspace_file_t *pcfi;

	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcip->pia_priv_eff) != 0) {
		err(EXIT_FAILURE, "failed to raise privileges");
	}

	if ((fd = open(path, O_RDONLY)) < 0) {
		err(EXIT_FAILURE, "failed to open input file %s", path);
	}

	if (fstat(fd, &st) != 0) {
		err(EXIT_FAILURE, "failed to get stat information for %s",
		    path);
	}

	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcip->pia_priv_min) != 0) {
		err(EXIT_FAILURE, "failed to reduce privileges");
	}

	if (S_ISDIR(st.st_mode)) {
		errx(EXIT_FAILURE, "input file %s is a directory, unable "
		    "to read data", path);
	}

	if (S_ISLNK(st.st_mode)) {
		errx(EXIT_FAILURE, "input file %s is a symbolic link, unable "
		    "to read data", path);
	}

	if (S_ISDOOR(st.st_mode)) {
		errx(EXIT_FAILURE, "input file %s is a door, unable "
		    "to read data", path);
	}

	if (S_ISPORT(st.st_mode)) {
		errx(EXIT_FAILURE, "input file %s is an event port, unable "
		    "to read data", path);
	}

	/*
	 * Assume if we were given a FIFO, character/block device, socket, or
	 * something else that it's probably fine.
	 */
	pcfi = calloc(1, sizeof (*pcfi));
	if (pcfi == NULL) {
		err(EXIT_FAILURE, "failed to allocate memory for reading "
		    "cfgspace data from a file");
	}

	pcfi->pcfi_fd = fd;
	*arg = pcfi;
	*opsp = &pcieadm_file_ops;
}

void
pcieadm_fini_ops_file(void *arg)
{
	pcieadm_cfgspace_file_t *pcfi = arg;
	VERIFY0(close(pcfi->pcfi_fd));
	free(pcfi);
}

typedef struct pcieadm_cfgspace_kernel {
	pcieadm_t *pck_pci;
	int pck_fd;
	uint8_t pck_bus;
	uint8_t pck_dev;
	uint8_t pck_func;
} pcieadm_cfgspace_kernel_t;

static boolean_t
pcieadm_pop_kernel_common(uint8_t ptb, uint8_t len, uint64_t off, void *buf,
    void *arg, boolean_t write)
{
	pcieadm_cfgspace_kernel_t *pck = arg;
	pcieadm_t *pcip = pck->pck_pci;
	pcitool_reg_t pci_reg;

	bzero(&pci_reg, sizeof (pci_reg));
	pci_reg.user_version = PCITOOL_VERSION;
	pci_reg.bus_no = pck->pck_bus;
	pci_reg.dev_no = pck->pck_dev;
	pci_reg.func_no = pck->pck_func;
	pci_reg.barnum = ptb;
	pci_reg.offset = off;
	pci_reg.acc_attr = PCITOOL_ACC_ATTR_ENDN_LTL;

	switch (len) {
	case 1:
		pci_reg.acc_attr += PCITOOL_ACC_ATTR_SIZE_1;
		break;
	case 2:
		pci_reg.acc_attr += PCITOOL_ACC_ATTR_SIZE_2;
		break;
	case 4:
		pci_reg.acc_attr += PCITOOL_ACC_ATTR_SIZE_4;
		break;
	case 8:
		pci_reg.acc_attr += PCITOOL_ACC_ATTR_SIZE_8;
		break;
	default:
		errx(EXIT_FAILURE, "asked to read invalid size from kernel: %u",
		    len);
	}

	if (write) {
		switch (len) {
		case 1:
			pci_reg.data = *(uint8_t *)buf;
			break;
		case 2:
			pci_reg.data = *(uint16_t *)buf;
			break;
		case 4:
			pci_reg.data = *(uint32_t *)buf;
			break;
		case 8:
			pci_reg.data = *(uint64_t *)buf;
			break;
		}
	}

	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcip->pia_priv_eff) != 0) {
		err(EXIT_FAILURE, "failed to raise privileges");
	}

	int cmd = write ? PCITOOL_DEVICE_SET_REG : PCITOOL_DEVICE_GET_REG;
	if (ioctl(pck->pck_fd, cmd, &pci_reg) != 0) {
		err(EXIT_FAILURE, "failed to read device offset 0x%" PRIx64
		    ": 0x%x", off, pci_reg.status);
	}

	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcip->pia_priv_min) != 0) {
		err(EXIT_FAILURE, "failed to reduce privileges");
	}

	if (!write) {
		switch (len) {
		case 1:
			*(uint8_t *)buf = (uint8_t)pci_reg.data;
			break;
		case 2:
			*(uint16_t *)buf = (uint16_t)pci_reg.data;
			break;
		case 4:
			*(uint32_t *)buf = (uint32_t)pci_reg.data;
			break;
		case 8:
			*(uint64_t *)buf = (uint64_t)pci_reg.data;
			break;
		}
	}

	return (B_TRUE);

}

static boolean_t
pcieadm_pop_cfgspace_kernel(uint32_t off, uint8_t len, void *buf, void *arg)
{
	return (pcieadm_pop_kernel_common(PCITOOL_CONFIG, len, off, buf, arg,
	    B_FALSE));
}


static boolean_t
pcieadm_pop_bar_kernel(uint8_t bar, uint8_t len, uint64_t off, void *buf,
    void *arg, boolean_t write)
{
	if (bar >= PCI_BASE_NUM) {
		errx(EXIT_FAILURE, "requested to read %u bytes at 0x%" PRIx64
		    " from non-existent BAR %u", len, off, bar);
	}

	/*
	 * The ioctl interface uses 0 for configuration space and so the ioctl
	 * number is the bar plus one.
	 */
	return (pcieadm_pop_kernel_common(bar + 1, len, off, buf, arg, write));
}

static const pcieadm_ops_t pcieadm_kernel_ops = {
	.pop_cfg = pcieadm_pop_cfgspace_kernel,
	.pop_bar = pcieadm_pop_bar_kernel
};

void
pcieadm_init_ops_kernel(pcieadm_t *pcip, const pcieadm_ops_t **opsp, void **arg)
{
	char *nexus_base;
	char nexus_reg[PATH_MAX];
	int fd, nregs, *regs;
	pcieadm_cfgspace_kernel_t *pck;

	if ((nexus_base = di_devfs_path(pcip->pia_nexus)) == NULL) {
		err(EXIT_FAILURE, "failed to get path to nexus node");
	}

	if (snprintf(nexus_reg, sizeof (nexus_reg), "/devices%s:reg",
	    nexus_base) >= sizeof (nexus_reg)) {
		errx(EXIT_FAILURE, "failed to construct nexus path, path "
		    "overflow");
	}
	free(nexus_base);

	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcip->pia_priv_eff) != 0) {
		err(EXIT_FAILURE, "failed to raise privileges");
	}

	if ((fd = open(nexus_reg, O_RDONLY)) < 0) {
		err(EXIT_FAILURE, "failed to open %s", nexus_reg);
	}

	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcip->pia_priv_min) != 0) {
		err(EXIT_FAILURE, "failed to reduce privileges");
	}

	nregs = di_prop_lookup_ints(DDI_DEV_T_ANY, pcip->pia_devi, "reg",
	    &regs);
	if (nregs <= 0) {
		errx(EXIT_FAILURE, "failed to lookup regs array for %s",
		    pcip->pia_devstr);
	}

	pck = calloc(1, sizeof (pcieadm_cfgspace_kernel_t));
	if (pck == NULL) {
		err(EXIT_FAILURE, "failed to allocate memory for reading "
		    "kernel cfgspace data");
	}

	pck->pck_pci = pcip;
	pck->pck_fd = fd;
	pck->pck_bus = PCI_REG_BUS_G(regs[0]);
	pck->pck_dev = PCI_REG_DEV_G(regs[0]);
	pck->pck_func = PCI_REG_FUNC_G(regs[0]);

	*opsp = &pcieadm_kernel_ops;
	*arg = pck;
}

void
pcieadm_fini_ops_kernel(void *arg)
{
	pcieadm_cfgspace_kernel_t *pck = arg;

	VERIFY0(close(pck->pck_fd));
	free(pck);
}

void
pcieadm_walk_usage(const pcieadm_cmdtab_t *tab, FILE *f)
{
	for (; tab->pct_name != NULL; tab++) {
		tab->pct_use(f);
	}
}

static void
pcieadm_usage(const pcieadm_cmdtab_t *tab, const char *format, ...)
{
	if (format != NULL) {
		va_list ap;

		va_start(ap, format);
		vwarnx(format, ap);
		va_end(ap);
	}

	(void) fprintf(stderr, "usage:  %s <subcommand> <args> ...\n\n",
	    pcieadm_progname);
	if (tab == NULL)
		return;
	pcieadm_walk_usage(tab, stderr);
}

int
pcieadm_walk_tab(pcieadm_t *pcip, const pcieadm_cmdtab_t *tab, int argc,
    char *argv[])
{
	uint32_t cmd;

	if (argc == 0) {
		pcieadm_usage(tab, "missing required sub-command");
		return (EXIT_FAILURE);
	}

	for (cmd = 0; tab[cmd].pct_name != NULL; cmd++) {
		if (strcmp(argv[0], tab[cmd].pct_name) == 0) {
			break;
		}
	}

	if (tab[cmd].pct_name == NULL) {
		pcieadm_usage(tab, "unknown subcommand %s", argv[0]);
		return (EXIT_USAGE);
	}

	argc--;
	argv++;
	optind = 0;
	pcieadm.pia_cmdtab = &tab[cmd];

	return (tab[cmd].pct_func(pcip, argc, argv));

}

static const pcieadm_cmdtab_t pcieadm_cmds[] = {
	{ "bar", pcieadm_bar, pcieadm_bar_usage },
	{ "show-cfgspace", pcieadm_show_cfgspace, pcieadm_show_cfgspace_usage },
	{ "save-cfgspace", pcieadm_save_cfgspace, pcieadm_save_cfgspace_usage },
	{ "show-devs", pcieadm_show_devs, pcieadm_show_devs_usage },
	{ NULL }
};

int
main(int argc, char *argv[])
{
	pcieadm_progname = basename(argv[0]);

	if (argc < 2) {
		pcieadm_usage(pcieadm_cmds, "missing required sub-command");
		exit(EXIT_USAGE);
	}

	argc--;
	argv++;

	/*
	 * Set up common things that all of pcieadm needs before dispatching to
	 * a specific sub-command.
	 */
	pcieadm.pia_pcidb = pcidb_open(PCIDB_VERSION);
	if (pcieadm.pia_pcidb == NULL) {
		err(EXIT_FAILURE, "failed to open PCI ID database");
	}

	/*
	 * Set up privileges now that we have already opened our core libraries.
	 * We first set up the minimum actual privilege set that we use while
	 * running. We next set up a second privilege set that has additional
	 * privileges that are intersected with the users actual privileges and
	 * are appended to by the underlying command backends.
	 */
	if ((pcieadm.pia_priv_init = priv_allocset()) == NULL) {
		err(EXIT_FAILURE, "failed to allocate privilege set");
	}

	if (getppriv(PRIV_EFFECTIVE, pcieadm.pia_priv_init) != 0) {
		err(EXIT_FAILURE, "failed to get current privileges");
	}

	if ((pcieadm.pia_priv_min = priv_allocset()) == NULL) {
		err(EXIT_FAILURE, "failed to allocate privilege set");
	}

	if ((pcieadm.pia_priv_eff = priv_allocset()) == NULL) {
		err(EXIT_FAILURE, "failed to allocate privilege set");
	}

	/*
	 * Note, PRIV_FILE_READ is not removed from the basic set so that way we
	 * can still open libraries that are required due to lazy loading.
	 */
	priv_basicset(pcieadm.pia_priv_min);
	VERIFY0(priv_delset(pcieadm.pia_priv_min, PRIV_FILE_LINK_ANY));
	VERIFY0(priv_delset(pcieadm.pia_priv_min, PRIV_PROC_INFO));
	VERIFY0(priv_delset(pcieadm.pia_priv_min, PRIV_PROC_SESSION));
	VERIFY0(priv_delset(pcieadm.pia_priv_min, PRIV_PROC_FORK));
	VERIFY0(priv_delset(pcieadm.pia_priv_min, PRIV_NET_ACCESS));
	VERIFY0(priv_delset(pcieadm.pia_priv_min, PRIV_FILE_WRITE));
	VERIFY0(priv_delset(pcieadm.pia_priv_min, PRIV_PROC_EXEC));
	VERIFY0(priv_delset(pcieadm.pia_priv_min, PRIV_PROC_EXEC));

	priv_copyset(pcieadm.pia_priv_min, pcieadm.pia_priv_eff);
	priv_intersect(pcieadm.pia_priv_init, pcieadm.pia_priv_eff);

	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcieadm.pia_priv_min) != 0) {
		err(EXIT_FAILURE, "failed to reduce privileges");
	}

	return (pcieadm_walk_tab(&pcieadm, pcieadm_cmds, argc, argv));
}
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2026 Oxide Computer Company
 */

#ifndef _PCIEADM_H
#define	_PCIEADM_H

/*
 * Common definitions for pcieadm(8).
 */

#include <libdevinfo.h>
#include <pcidb.h>
#include <priv.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct pcieadm pcieadm_t;

typedef struct pcieadm_cmdtab {
	const char *pct_name;
	int (*pct_func)(pcieadm_t *, int, char **);
	void (*pct_use)(FILE *);
} pcieadm_cmdtab_t;

struct pcieadm {
	uint_t pia_indent;
	di_node_t pia_root;
	const char *pia_devstr;
	di_node_t pia_devi;
	di_node_t pia_nexus;
	pcidb_hdl_t *pia_pcidb;
	const pcieadm_cmdtab_t *pia_cmdtab;
	priv_set_t *pia_priv_init;
	priv_set_t *pia_priv_min;
	priv_set_t *pia_priv_eff;
};

typedef struct {
	void *pdw_arg;
	int (*pdw_func)(di_node_t, void *);
} pcieadm_di_walk_t;

/*
 * Config space related
 */
typedef boolean_t (*pcieadm_cfgspace_f)(uint32_t, uint8_t, void *, void *);
typedef boolean_t (*pcieadm_bar_f)(uint8_t, uint8_t, uint64_t, void *, void *,
    boolean_t);

typedef struct {
	pcieadm_cfgspace_f pop_cfg;
	pcieadm_bar_f pop_bar;
} pcieadm_ops_t;

/*
 * Utilities
 */
extern void pcieadm_di_walk(pcieadm_t *, pcieadm_di_walk_t *);
extern void pcieadm_init_ops_kernel(pcieadm_t *, const pcieadm_ops_t **,
    void **);
extern void pcieadm_fini_ops_kernel(void *);
extern void pcieadm_init_ops_file(pcieadm_t *, const char *,
    const pcieadm_ops_t **, void **);
extern void pcieadm_fini_ops_file(void *);
extern void pcieadm_find_nexus(pcieadm_t *);
extern void pcieadm_find_dip(pcieadm_t *, const char *);

/*
 * Output related
 */
extern const char *pcieadm_progname;
extern void pcieadm_indent(void);
extern void pcieadm_deindent(void);
extern void pcieadm_print(const char *, ...);
extern void pcieadm_ofmt_errx(const char *, ...);

/*
 * Command tabs
 */
extern void pcieadm_walk_usage(const pcieadm_cmdtab_t *, FILE *);
extern int pcieadm_walk_tab(pcieadm_t *, const pcieadm_cmdtab_t *, int,
    char *[]);
extern int pcieadm_bar(pcieadm_t *, int, char *[]);
extern void pcieadm_bar_usage(FILE *);
extern int pcieadm_save_cfgspace(pcieadm_t *, int, char *[]);
extern void pcieadm_save_cfgspace_usage(FILE *);
extern int pcieadm_show_cfgspace(pcieadm_t *, int, char *[]);
extern void pcieadm_show_cfgspace_usage(FILE *);
extern int pcieadm_show_devs(pcieadm_t *, int, char *[]);
extern void pcieadm_show_devs_usage(FILE *);

#define	EXIT_USAGE	2

/*
 * Privilege related. Note there are no centralized functions around raising and
 * lowering privs as that unfortunately makes ROPs more easy to execute.
 */
extern void pcieadm_init_privs(pcieadm_t *);

#ifdef __cplusplus
}
#endif

#endif /* _PCIEADM_H */
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2026 Oxide Computer Company
 */

/*
 * Implement logic related to listing, reading, and writing BARs.
 */

#include <err.h>
#include <stdio.h>
#include <sys/pci.h>
#include <ofmt.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

#include "pcieadm.h"

static void
pcieadm_bar_list_usage(FILE *f)
{
	(void) fprintf(f, "\tbar list\t[-H] [-o field,... [-p]] -d device "
	    "[filter...]\n");
}

static void
pcieadm_bar_list_help(const char *fmt, ...)
{
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vwarnx(fmt, ap);
		va_end(ap);
		(void) fprintf(stderr, "\n");
	}

	(void) fprintf(stderr, "Usage:  %s bar list [-H] [-o field,... [-p]] "
	    "-d device [filter,...]\n", pcieadm_progname);
	(void) fprintf(stderr, "List BARs specific to a single device.\n\n"
	    "\t-d device\tlist BARs from the specified device (driver instance,"
	    "\n\t\t\t/devices path, or b/d/f)\n"
	    "\t-H\t\tomit the column header\n"
	    "\t-o field\toutput fields to print (required for -p)\n"
	    "\t-p\t\tparsable output (requires -o)\n\n");
	(void) fprintf(stderr, "The following fields are supported:\n"
	    "\taddress\t\tthe address programmed in the BAR\n"
	    "\tbar\t\tthe bar's numeric identifier\n"
	    "\tdesc\t\ta human description of the BAR\n"
	    "\tmtype\t\tthe memory type of the BAR\n"
	    "\tprefetech\tindicates whether or not the BAR is pre-fetchable\n"
	    "\traw\t\tthe raw contents of the hardware BAR register\n"
	    "\tsize\t\tthe size of the bar\n"
	    "\tspace\t\tthe type of space the BAR represents\n"
	    "\twidth\t\tindicates the width of the BAR in bytes\n");
	(void) fprintf(stderr, "The following filters are supported:\n"
	    "\t<index>\t\tthe BAR matches the specified index\n"
	    "\tio\t\tthe BAR is an I/O BAR\n"
	    "\tmem\t\tthe BAR is a memory BAR\n"
	    "\tmem32\t\tthe BAR is a 32-bit memory BAR\n"
	    "\tmem64\t\tthe BAR is a 64-bit memory BAR\n"
	    "\tprefetch\tthe BAR is prefetchable\n");
}

typedef enum pcieadm_bar_list_otype {
	PCIEADM_BAR_LIST_BAR,
	PCIEADM_BAR_LIST_ADDRESS,
	PCIEADM_BAR_LIST_DESC,
	PCIEADM_BAR_LIST_MTYPE,
	PCIEADM_BAR_LIST_SIZE,
	PCIEADM_BAR_LIST_SPACE,
	PCIEADM_BAR_LIST_PREFETCH,
	PCIEADM_BAR_LIST_WIDTH,
	PCIEADM_BAR_LIST_RAW
} pcieadm_bar_list_otpye_t;

typedef struct pcieadm_bar_list_ofmt {
	uint8_t pblo_idx;
	uint8_t pblo_width;
	bool pblo_mem;
	bool pblo_prefetch;
	uint64_t pblo_addr;
	uint64_t pblo_size;
	uint64_t pblo_raw;
	const char *pblo_mtype;
} pcieadm_bar_list_ofmt_t;

static boolean_t
pcieadm_bar_list_ofmt_cb(ofmt_arg_t *ofarg, char *buf, uint_t buflen)
{
	size_t ret;
	const pcieadm_bar_list_ofmt_t *pblo = ofarg->ofmt_cbarg;

	switch (ofarg->ofmt_id) {
	case PCIEADM_BAR_LIST_BAR:
		ret = snprintf(buf, buflen, "%u", pblo->pblo_idx);
		break;
	case PCIEADM_BAR_LIST_ADDRESS:
		ret = snprintf(buf, buflen, "0x%" PRIx64, pblo->pblo_addr);
		break;
	case PCIEADM_BAR_LIST_DESC:
		if (pblo->pblo_mem) {
			ret = snprintf(buf, buflen, "%s%s Memory",
			    pblo->pblo_mtype, pblo->pblo_prefetch ?
			    " Prefetchable" : "");
		} else {
			ret = strlcat(buf, "I/O", buflen);
		}
		break;
	case PCIEADM_BAR_LIST_MTYPE:
		ret = strlcat(buf, pblo->pblo_mtype, buflen);
		break;
	case PCIEADM_BAR_LIST_SIZE:
		ret = snprintf(buf, buflen, "0x%" PRIx64, pblo->pblo_size);
		break;
	case PCIEADM_BAR_LIST_SPACE:
		ret = strlcat(buf, pblo->pblo_mem ? "Memory" : "I/O", buflen);
		break;
	case PCIEADM_BAR_LIST_PREFETCH:
		ret = strlcat(buf, pblo->pblo_prefetch ? "yes" : "no", buflen);
		break;
	case PCIEADM_BAR_LIST_WIDTH:
		ret = snprintf(buf, buflen, "%u", pblo->pblo_width);
		break;
	case PCIEADM_BAR_LIST_RAW:
		ret = snprintf(buf, buflen, "0x%" PRIx64, pblo->pblo_raw);
		break;
	default:
		return (B_FALSE);
	}

	return (buflen > ret);
}

static const char *pcieadm_bar_list_fields = "bar,size,address,desc";
static const ofmt_field_t pcieadm_bar_list_ofmt[] = {
	{ "BAR", 8, PCIEADM_BAR_LIST_BAR, pcieadm_bar_list_ofmt_cb },
	{ "ADDRESS", 16, PCIEADM_BAR_LIST_ADDRESS, pcieadm_bar_list_ofmt_cb },
	{ "DESC", 32, PCIEADM_BAR_LIST_DESC, pcieadm_bar_list_ofmt_cb },
	{ "MTYPE", 8, PCIEADM_BAR_LIST_MTYPE, pcieadm_bar_list_ofmt_cb },
	{ "SIZE", 12, PCIEADM_BAR_LIST_SIZE, pcieadm_bar_list_ofmt_cb },
	{ "SPACE", 10, PCIEADM_BAR_LIST_SPACE, pcieadm_bar_list_ofmt_cb },
	{ "PREFETCH", 4, PCIEADM_BAR_LIST_PREFETCH, pcieadm_bar_list_ofmt_cb },
	{ "WIDTH", 6, PCIEADM_BAR_LIST_WIDTH, pcieadm_bar_list_ofmt_cb },
	{ "RAW", 16, PCIEADM_BAR_LIST_RAW, pcieadm_bar_list_ofmt_cb },
};

static bool
pcieadm_show_bar_match(const pcieadm_bar_list_ofmt_t *pblo, int nfilts,
    char **filts, bool *used)
{
	bool match = false;

	if (nfilts <= 0) {
		return (true);
	}

	for (int i = 0; i < nfilts; i++) {
		if (strcmp(filts[i], "io") == 0 && !pblo->pblo_mem) {
			used[i] = true;
			match = true;
			continue;
		}

		if (strcmp(filts[i], "mem32") == 0 &&
		    strcmp(pblo->pblo_mtype, "32-bit") == 0) {
			used[i] = true;
			match = true;
			continue;
		}

		if (strcmp(filts[i], "mem64") == 0 &&
		    strcmp(pblo->pblo_mtype, "64-bit") == 0) {
			used[i] = true;
			match = true;
			continue;
		}

		if (strcmp(filts[i], "mem") == 0 && pblo->pblo_mem) {
			used[i] = true;
			match = true;
			continue;
		}

		if (strcmp(filts[i], "prefetch") == 0 && pblo->pblo_prefetch) {
			used[i] = true;
			match = true;
			continue;
		}

		/*
		 * Attempt to parse anything left as an integer indicating a BAR
		 * index.
		 */
		const char *errstr;
		long long l = strtonumx(filts[i], 0, UINT32_MAX, &errstr, 0);
		if (errstr == NULL && l == pblo->pblo_idx) {
			used[i] = true;
			match = true;
			continue;
		}
	}

	return (match);
}

/*
 * Read information about BARs. There are basically two different sources that
 * we want to combine information from: reg[] and the device itself. We prefer
 * reg[] over assigned-addresses[] because the latter may not exist if we have
 * some kind of resource error.
 *
 * We prefer to walk the device itself and then augment it with size information
 * from reg[] as reg[] will skip unimplemented BARs and we want to be able to
 * accurately indicate the type and other information of a BAR with zero size.
 */
static int
pcieadm_bar_list(pcieadm_t *pcip, int argc, char *argv[])
{
	int c, ret = EXIT_SUCCESS;
	const char *device = NULL;
	const pcieadm_ops_t *ops;
	void *readarg;
	uint8_t hdr, nbar;
	uint_t flags = 0;
	bool parse = false, *filts = NULL, found = false;
	const char *fields = NULL;
	ofmt_status_t oferr;
	ofmt_handle_t ofmt;

	while ((c = getopt(argc, argv, ":d:Ho:p")) != -1) {
		switch (c) {
		case 'd':
			device = optarg;
			break;
		case 'H':
			flags |= OFMT_NOHEADER;
			break;
		case 'o':
			fields = optarg;
			break;
		case 'p':
			parse = true;
			flags |= OFMT_PARSABLE;
			break;
		case ':':
			pcieadm_bar_list_help("Option -%c requires an "
			    "argument", optopt);
			exit(EXIT_USAGE);
		case '?':
		default:
			pcieadm_bar_list_help("unknown option: -%c",
			    optopt);
			exit(EXIT_USAGE);
		}
	}

	if (device == NULL) {
		pcieadm_bar_list_help("missing required device argument (-d)");
		exit(EXIT_USAGE);
	}

	if (parse && fields == NULL) {
		errx(EXIT_USAGE, "-p requires fields specified with -o");
	}

	if (fields == NULL) {
		fields = pcieadm_bar_list_fields;
	}

	argc -= optind;
	argv += optind;

	if (argc > 0) {
		filts = calloc(argc, sizeof (bool));
		if (filts == NULL) {
			err(EXIT_FAILURE, "failed to allocate filter tracking "
			    "memory");
		}
	}

	oferr = ofmt_open(fields, pcieadm_bar_list_ofmt, flags, 0, &ofmt);
	ofmt_check(oferr, parse, ofmt, pcieadm_ofmt_errx, warnx);

	/*
	 * We will need full privileges to get information from the device.
	 */
	priv_fillset(pcip->pia_priv_eff);

	pcieadm_find_dip(pcip, device);
	int *reg;
	int nreg = di_prop_lookup_ints(DDI_DEV_T_ANY, pcip->pia_devi,
	    "reg", &reg);
	if (nreg < 0 && errno != ENXIO) {
		err(EXIT_FAILURE, "failed to look up reg[] property");
	} else if (nreg < 0) {
		nreg = 0;
	} else if (nreg % 5 != 0) {
		errx(EXIT_FAILURE, "reg[] property has wrong shape, found %d "
		    "integers but expected a multiple of 5", nreg);
	}
	nreg /= 5;

	pcieadm_init_ops_kernel(pcip, &ops, &readarg);
	if (!ops->pop_cfg(PCI_CONF_HEADER, sizeof (hdr), &hdr, readarg)) {
		errx(EXIT_FAILURE, "failed to read offset %u from device "
		    "configuration space", PCI_CONF_HEADER);
	}

	switch (hdr & PCI_HEADER_TYPE_M) {
	case PCI_HEADER_ZERO:
		nbar = PCI_BASE_NUM;
		break;
	case PCI_HEADER_ONE:
		nbar = PCI_BCNF_BASE_NUM;
		break;
	case PCI_HEADER_TWO:
	default:
		errx(EXIT_FAILURE, "unsupported PCI header type: 0x%x",
		    hdr & PCI_HEADER_TYPE_M);
	}

	/*
	 * First do a pass where we read all of the raw BAR registers from the
	 * device.
	 */
	uint32_t raw[PCI_BASE_NUM];
	for (uint8_t i = 0; i < nbar; i++) {
		if (!ops->pop_cfg(PCI_CONF_BASE0 + i * 4, sizeof (uint32_t),
		    &raw[i], readarg)) {
			errx(EXIT_FAILURE, "failed to read BAR data form "
			    "device at offset 0x%x", PCI_CONF_BASE0 + i * 4);
		}
	}

	/*
	 * Go through and process each BAR entry. Determine where we have a
	 * 64-bit BAR and therefore need to account for two entries here. This
	 * is also where we try to marry things up to assigned-addresses to
	 * determine the size.
	 */
	for (uint8_t i = 0; i < nbar; i++) {
		pcieadm_bar_list_ofmt_t arg;

		(void) memset(&arg, 0, sizeof (arg));
		arg.pblo_idx = i;
		arg.pblo_width = PCI_BAR_SZ_32;
		arg.pblo_raw = raw[i];
		arg.pblo_prefetch = false;

		if ((raw[i] & PCI_BASE_SPACE_M) == PCI_BASE_SPACE_IO) {
			arg.pblo_mem = false;
			arg.pblo_addr = raw[i] & PCI_BASE_IO_ADDR_M;
			arg.pblo_mtype = "--";
		} else {
			arg.pblo_mem = true;
			arg.pblo_addr = raw[i] & PCI_BASE_M_ADDR_M;

			if ((raw[i] & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL) {
				i++;
				if (i == nbar) {
					warnx("device %s has corrupt BAR %u: "
					    "no additional BARs exist for "
					    "upper 32-bit data", device,
					    arg.pblo_idx);
					ret = EXIT_FAILURE;
					continue;
				}
				arg.pblo_width = PCI_BAR_SZ_64;
				arg.pblo_raw |= (uint64_t)raw[i] << 32;
				arg.pblo_addr |= (uint64_t)raw[i] << 32;
			}

			if ((arg.pblo_raw & PCI_BASE_PREF_M) != 0) {
				arg.pblo_prefetch = true;
			}

			switch (arg.pblo_raw & PCI_BASE_TYPE_M) {
			case PCI_BASE_TYPE_MEM:
				arg.pblo_mtype = "32-bit";
				break;
			case PCI_BASE_TYPE_LOW:
				arg.pblo_mtype = "1M";
				break;
			case PCI_BASE_TYPE_ALL:
				arg.pblo_mtype = "64-bit";
				break;
			case PCI_BASE_TYPE_RES:
				arg.pblo_mtype = "reserved";
				break;
			}
		}

		/*
		 * Finally, before we call back, determine what the size of this
		 * BAR is. We walk reg[] to try to find a matching register
		 * index, which is considered the offset into configuration
		 * space of the start of this BAR.
		 *
		 * If we can't find a match, then it likely is because the BAR
		 * has no size. Using reg[] versus assigned-addresses[] gives us
		 * a reasonable confidence here, though this doesn't account for
		 * resizable BAR support.
		 */
		arg.pblo_size = 0;
		uint32_t targ = PCI_CONF_BASE0 + arg.pblo_idx *
		    sizeof (uint32_t);
		const pci_regspec_t *rsp = (pci_regspec_t *)reg;
		for (int ridx = 0; ridx < nreg; ridx++, rsp++) {
			uint32_t check = PCI_REG_REG_G(rsp->pci_phys_hi);

			if (targ == check) {
				arg.pblo_size = (uint64_t)rsp->pci_size_hi <<
				    32;
				arg.pblo_size |= rsp->pci_size_low;
				break;
			}
		}

		if (pcieadm_show_bar_match(&arg, argc, argv, filts)) {
			found = true;
			ofmt_print(ofmt, &arg);
		}
	}

	for (int i = 0; i < argc; i++) {
		if (!filts[i]) {
			warnx("filter '%s' did not match any BARs", argv[i]);
			ret = EXIT_FAILURE;
		}
	}

	if (!found) {
		ret = EXIT_FAILURE;
	}

	free(filts);
	pcieadm_fini_ops_kernel(readarg);
	ofmt_close(ofmt);

	return (ret);
}

static void
pcieadm_bar_read_usage(FILE *f)
{
	(void) fprintf(f, "\tbar read\t[-l length] -d device -b bar reg\n");
}

static void
pcieadm_bar_read_help(const char *fmt, ...)
{
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vwarnx(fmt, ap);
		va_end(ap);
		(void) fprintf(stderr, "\n");
	}

	(void) fprintf(stderr, "Usage:  %s bar read [-l length] -d device "
	    "-b bar reg\n", pcieadm_progname);
	(void) fprintf(stderr, "Read data at offset <reg> from a device "
	    "BAR.\n\n"
	    "\t-b bar\t\tthe index of the BAR to read from\n"
	    "\t-d device\tread BAR from the specified device (driver instance,"
	    "\n\t\t\t/devices path, or b/d/f)\n"
	    "\t-l length\tspecify the number of bytes to read: 1, 2, 4 "
	    "(default),\n\t\t\tor 8\n");
}

/*
 * We can't use strtonumx here as its maximum values are all based in signed
 * integers.
 */
static uint64_t
pcieadm_bar_parse_u64(const char *reg, const char *desc)
{
	char *eptr;
	unsigned long long ull;

	errno = 0;
	ull = strtoull(reg, &eptr, 0);
	if (errno != 0 || *eptr != '\0') {
		errx(EXIT_FAILURE, "failed to parse %s %s", desc, reg);
	}

	return ((uint64_t)ull);
}

static uint8_t
pcieadm_bar_parse_len(const char *len)
{
	char *eptr;
	unsigned long ul;

	errno = 0;
	ul = strtoul(len, &eptr, 0);
	if (errno != 0 || *eptr != '\0') {
		errx(EXIT_FAILURE, "failed to parse length %s", len);
	}

	if (ul != 1 && ul != 2 && ul != 4 && ul != 8) {
		errx(EXIT_FAILURE, "invalid byte length 0x%lx: only 1, 2, 4, "
		    "and 8 byte I/Os are supported", ul);
	}

	return ((uint8_t)ul);
}

static uint8_t
pcieadm_bar_parse_bar(const char *bar)
{
	uint8_t val;
	const char *errstr;
	val = (uint8_t)strtonumx(bar, 0, PCI_BASE_NUM - 1, &errstr, 0);
	if (errstr != NULL) {
		errx(EXIT_FAILURE, "failed to parse BAR %s: value is %s", bar,
		    errstr);
	}

	return (val);
}

static int
pcieadm_bar_read(pcieadm_t *pcip, int argc, char *argv[])
{
	int c, ret = EXIT_SUCCESS;
	const char *device = NULL, *barstr = NULL, *lenstr = NULL;
	const pcieadm_ops_t *ops;
	void *karg;

	while ((c = getopt(argc, argv, ":b:d:l:")) != -1) {
		switch (c) {
		case 'b':
			barstr = optarg;
			break;
		case 'd':
			device = optarg;
			break;
		case 'l':
			lenstr = optarg;
			break;
		case ':':
			pcieadm_bar_read_help("Option -%c requires an "
			    "argument", optopt);
			exit(EXIT_USAGE);
		case '?':
		default:
			pcieadm_bar_read_help("unknown option: -%c",
			    optopt);
			exit(EXIT_USAGE);
		}
	}

	if (device == NULL) {
		pcieadm_bar_read_help("missing required device argument (-d)");
		exit(EXIT_USAGE);
	}

	if (barstr == NULL) {
		pcieadm_bar_read_help("missing required bar argument (-b)");
		exit(EXIT_USAGE);
	}

	argc -= optind;
	argv += optind;

	if (argc == 0) {
		errx(EXIT_FAILURE, "missing required register");
	} else if (argc > 1) {
		errx(EXIT_FAILURE, "only a single register may be read");
	}

	uint8_t bar = pcieadm_bar_parse_bar(barstr);
	uint8_t len = 4;
	if (lenstr != NULL) {
		len = pcieadm_bar_parse_len(lenstr);
	}
	uint64_t reg = pcieadm_bar_parse_u64(argv[0], "register");
	void *buf = calloc(1, len);
	if (buf == NULL) {
		err(EXIT_FAILURE, "failed to allocate memory for read request");
	}

	/*
	 * We will need full privileges to read a BAR.
	 */
	priv_fillset(pcip->pia_priv_eff);

	pcieadm_find_dip(pcip, device);
	pcieadm_init_ops_kernel(pcip, &ops, &karg);

	if (!ops->pop_bar(bar, len, reg, buf, karg, B_FALSE)) {
		errx(EXIT_FAILURE, "failed to read %u bytes at 0x%" PRIx64
		    " from BAR %u", len, reg, bar);
	}

	switch (len) {
	case 1:
		(void) printf("0x%x\n", *(uint8_t *)buf);
		break;
	case 2:
		(void) printf("0x%x\n", *(uint16_t *)buf);
		break;
	case 4:
		(void) printf("0x%x\n", *(uint32_t *)buf);
		break;
	case 8:
		(void) printf("0x%x\n", *(uint64_t *)buf);
		break;
	default:
		abort();
	}

	free(buf);
	pcieadm_fini_ops_kernel(karg);

	return (ret);
}

static void
pcieadm_bar_write_usage(FILE *f)
{
	(void) fprintf(f, "\tbar write\t[-l length] -d device -b bar "
	    "reg=value\n");
}

static void
pcieadm_bar_write_help(const char *fmt, ...)
{
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vwarnx(fmt, ap);
		va_end(ap);
		(void) fprintf(stderr, "\n");
	}

	(void) fprintf(stderr, "Usage:  %s bar write [-l length] -d device "
	    "-b bar reg=value", pcieadm_progname);
	(void) fprintf(stderr, "Usage:  %s bar write [-l length] -d device "
	    "-b bar reg=value\n", pcieadm_progname);
	(void) fprintf(stderr, "Write data to a device BAR at offset <reg>.\n\n"
	    "\t-b bar\t\tthe index of the BAR to write to\n"
	    "\t-d device\tread BAR from the specified device (driver instance,"
	    "\n\t\t\t/devices path, or b/d/f)\n"
	    "\t-l length\tspecify the number of bytes to read: 1, 2, 4 "
	    "(default),\n\t\t\tor 8\n");

}

static int
pcieadm_bar_write(pcieadm_t *pcip, int argc, char *argv[])
{
	int c, ret = EXIT_SUCCESS;
	const char *device = NULL, *barstr = NULL, *lenstr = NULL;
	const pcieadm_ops_t *ops;
	void *karg;

	while ((c = getopt(argc, argv, ":b:d:l:")) != -1) {
		switch (c) {
		case 'b':
			barstr = optarg;
			break;
		case 'd':
			device = optarg;
			break;
		case 'l':
			lenstr = optarg;
			break;
		case ':':
			pcieadm_bar_write_help("Option -%c requires an "
			    "argument", optopt);
			exit(EXIT_USAGE);
		case '?':
		default:
			pcieadm_bar_write_help("unknown option: -%c",
			    optopt);
			exit(EXIT_USAGE);
		}
	}

	if (device == NULL) {
		pcieadm_bar_write_help("missing required device argument (-d)");
		exit(EXIT_USAGE);
	}

	if (barstr == NULL) {
		pcieadm_bar_write_help("missing required bar argument (-b)");
		exit(EXIT_USAGE);
	}

	argc -= optind;
	argv += optind;

	if (argc == 0) {
		errx(EXIT_FAILURE, "missing required register");
	} else if (argc > 1) {
		errx(EXIT_FAILURE, "only a single register may be read");
	}
	char *eq = strchr(argv[0], '=');
	if (eq == NULL) {
		errx(EXIT_FAILURE, "failed to parse value string %s: missing "
		    "equals ('=') separator", argv[0]);
	} else if (eq[1] == '\0') {
		errx(EXIT_FAILURE, "missing a value after the equals ('=') "
		    "separator");
	}
	*eq = '\0';

	uint8_t bar = pcieadm_bar_parse_bar(barstr);
	uint8_t len = 4;
	if (lenstr != NULL) {
		len = pcieadm_bar_parse_len(lenstr);
	}
	void *buf = malloc(len);
	if (buf == NULL) {
		err(EXIT_FAILURE, "failed to allocate memory for read request");
	}
	uint64_t reg = pcieadm_bar_parse_u64(argv[0], "register");
	uint64_t value = pcieadm_bar_parse_u64(eq + 1, "value");

	uint8_t u8;
	uint16_t u16;
	uint32_t u32;
	switch (len) {
	case 1:
		if (value > UINT8_MAX) {
			errx(EXIT_FAILURE, "value %s is too large for a "
			    "1 byte write", eq + 1);
		}
		u8 = (uint8_t)value;
		(void) memcpy(buf, &u8, sizeof (uint8_t));
		break;
	case 2:
		if (value > UINT16_MAX) {
			errx(EXIT_FAILURE, "value %s is too large for a "
			    "2 byte write", eq + 1);
		}
		u16 = (uint8_t)value;
		(void) memcpy(buf, &u16, sizeof (uint16_t));
		break;
	case 4:
		if (value > UINT32_MAX) {
			errx(EXIT_FAILURE, "value %s is too large for a "
			    "4 byte write", eq + 1);
		}
		u32 = (uint32_t)value;
		(void) memcpy(buf, &u32, sizeof (uint32_t));
		break;
	case 8:
		(void) memcpy(buf, &value, sizeof (uint64_t));
		break;
	default:
		abort();
	}


	/*
	 * We will need full privileges to read a BAR.
	 */
	priv_fillset(pcip->pia_priv_eff);

	pcieadm_find_dip(pcip, device);
	pcieadm_init_ops_kernel(pcip, &ops, &karg);

	if (!ops->pop_bar(bar, len, reg, buf, karg, B_TRUE)) {
		errx(EXIT_FAILURE, "failed to write %u bytes at 0x%" PRIx64
		    " from BAR %u", len, reg, bar);
	}

	pcieadm_fini_ops_kernel(karg);

	return (ret);
}


static const pcieadm_cmdtab_t pcieadm_cmds_dev[] = {
	{ "list", pcieadm_bar_list, pcieadm_bar_list_usage },
	{ "read", pcieadm_bar_read, pcieadm_bar_read_usage },
	{ "write", pcieadm_bar_write, pcieadm_bar_write_usage },
	{ NULL }
};

int
pcieadm_bar(pcieadm_t *pcip, int argc, char *argv[])
{
	return (pcieadm_walk_tab(pcip, pcieadm_cmds_dev, argc, argv));
}

void
pcieadm_bar_usage(FILE *f)
{
	pcieadm_walk_usage(pcieadm_cmds_dev, f);
}
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2026 Oxide Computer Company
 */

/*
 * This file contains logic to walk and print a large chunk of configuration
 * space and many of the capabilities. There are multiple sub-commands that
 * vector into the same logic (e.g. 'save-cfgspace' and 'show-cfgspace'). In
 * general, there are a few major goals with this bit of code:
 *
 *  o Every field should strive to be parsable and therefore selectable for
 *    output. This drove the idea that every field has both a short name and a
 *    human name. The short name is a dot-delineated name. When in parsable
 *    mode, the name will always refer to a single field. However, for
 *    convenience for humans, when not trying to be parsable, we show the
 *    parents in the tree. That is if you specify something like
 *    'pcie.linkcap.maxspeed', in parsable mode you'll only get that; however,
 *    in non-parsable mode, you'll get an indication of the capability and
 *    register that field was in.
 *
 *  o Related to the above, parsable mode always outputs a raw, uninterpreted
 *    value. This was done on purpose. Some fields require interpreting multiple
 *    registers to have meaning and long strings aren't always the most useful.
 *
 *  o Every field isn't always pretty printed. This was generally just a
 *    decision based upon the field itself and how much work it'd be to fit it
 *    into the framework we have. In general, the ones we're mostly guilty of
 *    doing this with are related to cases where there's a scaling value in a
 *    subsequent register. If you find yourself wanting this, feel free to add
 *    it.
 *
 *  o Currently designated vendor-specific capabilities aren't included here (or
 *    any specific vendor-specific capabilities for that matter). If they are
 *    added, they should follow the same angle of using a name to represent a
 *    sub-capability as we did with HyperTransport.
 */

#include <err.h>
#include <strings.h>
#include <sys/sysmacros.h>
#include <sys/pci.h>
#include <sys/pcie.h>
#include <sys/debug.h>
#include <ofmt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/bitext.h>
#include <sys/ccompile.h>

#include "pcieadm.h"

/*
 * A few required forwards
 */
typedef struct pcieadm_cfgspace_walk pcieadm_cfgspace_walk_t;
typedef struct pcieadm_cfgspace_print pcieadm_cfgspace_print_t;
typedef struct pcieadm_regdef pcieadm_regdef_t;

typedef enum pcieadm_cfgspace_op {
	PCIEADM_CFGSPACE_OP_PRINT,
	PCIEADM_CFGSPACE_OP_WRITE
} pcieadm_cfgspace_op_t;

typedef enum piceadm_cfgspace_flag {
	PCIEADM_CFGSPACE_F_PARSE	= 1 << 0,
	PCIEADM_CFGSPACE_F_SHORT	= 1 << 1,
} pcieadm_cfgspace_flags_t;

typedef enum pcieadm_cfgspace_otype {
	PCIEADM_CFGSPACE_OT_SHORT,
	PCIEADM_CFGSPACE_OT_HUMAN,
	PCIEADM_CFGSPACE_OT_VALUE,
	PCIEADM_CFGSPACE_OT_BYTEOFF,
	PCIEADM_CFGSPACE_OT_BITOFF,
	PCIEADM_CFGSPACE_OT_BYTELEN,
	PCIEADM_CFGSPACE_OT_BITLEN
} pcieadm_cfgsapce_otype_t;

typedef struct pcieadm_cfgspace_ofmt {
	const char *pco_base;
	const char *pco_short;
	const char *pco_human;
	uint64_t pco_value;
	const char *pco_strval;
	uint32_t pco_off;
	uint32_t pco_bitoff;
	uint32_t pco_len;
	uint32_t pco_bitlen;
} pcieadm_cfgspace_ofmt_t;

typedef enum pcieadm_regdef_val {
	PRDV_STRVAL,
	PRDV_BITFIELD,
	PRDV_HEX,
	PRDV_CUSTOM
} pcieadm_regdef_val_t;

typedef struct pcieadm_regdef_addend {
	uint8_t pra_shift;
	int64_t pra_addend;
} pcieadm_regdef_addend_t;

struct pcieadm_regdef {
	uint8_t prd_lowbit;
	uint8_t prd_hibit;
	const char *prd_short;
	const char *prd_human;
	pcieadm_regdef_val_t prd_valtype;
	union {
		/*
		 * Enough space for up to an 8-bit fields worth of values
		 * (though we expect most to be sparse).
		 */
		const char *prdv_strval[128];
		pcieadm_regdef_addend_t prdv_hex;
		void (*prdv_func)(pcieadm_cfgspace_walk_t *,
		    const pcieadm_cfgspace_print_t *, const pcieadm_regdef_t *,
		    uint64_t);
	} prd_val;
};

typedef struct pcieadm_unitdef {
	const char *pcd_unit;
	uint32_t pcd_mult;
} pcieadm_unitdef_t;

typedef struct pcieadm_strmap {
	const char *psr_str;
	uint64_t psr_val;
} pcieadm_strmap_t;

typedef struct pcieadm_cfgspace_filter {
	const char *pcf_string;
	size_t pcf_len;
	boolean_t pcf_used;
} pcieadm_cfgspace_filter_t;

typedef struct pcieadm_strfilt {
	struct pcieadm_strfilt *pstr_next;
	const char *pstr_str;
	char pstr_curgen[256];
} pcieadm_strfilt_t;

/*
 * Data is sized to be large enough that we can hold all of PCIe extended
 * configuration space.
 */
typedef union pcieadm_cfgspace_data {
	uint8_t pcb_u8[PCIE_CONF_HDR_SIZE];
	uint32_t pcb_u32[PCIE_CONF_HDR_SIZE / 4];
} pcieadm_cfgspace_data_t;

struct pcieadm_cfgspace_walk {
	pcieadm_t *pcw_pcieadm;
	pcieadm_cfgspace_op_t pcw_op;
	uint32_t pcw_valid;
	pcieadm_cfgspace_data_t *pcw_data;
	uint16_t pcw_capoff;
	uint32_t pcw_caplen;
	int pcw_outfd;
	uint_t pcw_dtype;
	uint_t pcw_nlanes;
	uint_t pcw_pcietype;
	uint_t pcw_nfilters;
	pcieadm_cfgspace_filter_t *pcw_filters;
	pcieadm_cfgspace_flags_t pcw_flags;
	ofmt_handle_t pcw_ofmt;
	pcieadm_strfilt_t *pcw_filt;
};

void
pcieadm_strfilt_pop(pcieadm_cfgspace_walk_t *walkp)
{
	pcieadm_strfilt_t *filt;

	VERIFY3P(walkp->pcw_filt, !=, NULL);
	filt = walkp->pcw_filt;
	walkp->pcw_filt = filt->pstr_next;
	free(filt);
}

void
pcieadm_strfilt_push(pcieadm_cfgspace_walk_t *walkp, const char *str)
{
	pcieadm_strfilt_t *filt;
	size_t len;

	filt = calloc(1, sizeof (*filt));
	if (filt == NULL) {
		errx(EXIT_FAILURE, "failed to allocate memory for string "
		    "filter");
	}

	filt->pstr_str = str;
	if (walkp->pcw_filt == NULL) {
		len = strlcat(filt->pstr_curgen, str,
		    sizeof (filt->pstr_curgen));
	} else {
		len = snprintf(filt->pstr_curgen, sizeof (filt->pstr_curgen),
		    "%s.%s", walkp->pcw_filt->pstr_curgen, str);
		filt->pstr_next = walkp->pcw_filt;
	}

	if (len >= sizeof (filt->pstr_curgen)) {
		errx(EXIT_FAILURE, "overflowed internal string buffer "
		    "appending %s", str);
	}

	walkp->pcw_filt = filt;
}

static boolean_t
pcieadm_cfgspace_filter(pcieadm_cfgspace_walk_t *walkp, const char *str)
{
	char buf[1024];
	size_t len;

	if (walkp->pcw_nfilters == 0) {
		return (B_TRUE);
	}

	if (str == NULL) {
		return (B_FALSE);
	}

	if (walkp->pcw_filt != NULL) {
		len = snprintf(buf, sizeof (buf), "%s.%s",
		    walkp->pcw_filt->pstr_curgen, str);
	} else {
		len = snprintf(buf, sizeof (buf), "%s", str);
	}

	if (len >= sizeof (buf)) {
		abort();
	}

	for (uint_t i = 0; i < walkp->pcw_nfilters; i++) {
		if (strcmp(buf, walkp->pcw_filters[i].pcf_string) == 0) {
			walkp->pcw_filters[i].pcf_used = B_TRUE;
			return (B_TRUE);
		}

		/*
		 * If we're in non-parsable mode, we want to do a little bit
		 * more in a few cases. We want to make sure that we print the
		 * parents of more-specific entries. That is, if someone
		 * specified 'header.command.serr', then we want to print
		 * 'header', and 'header.command'. Similarly, if someone
		 * specifies an individual field, we want to print all of its
		 * subfields, that is asking for 'header.command', really gets
		 * that and all of 'header.command.*'.
		 */
		if ((walkp->pcw_flags & PCIEADM_CFGSPACE_F_PARSE) != 0) {
			continue;
		}

		if (len >= walkp->pcw_filters[i].pcf_len) {
			if (strncmp(buf, walkp->pcw_filters[i].pcf_string,
			    walkp->pcw_filters[i].pcf_len) == 0 &&
			    buf[walkp->pcw_filters[i].pcf_len] == '.') {
				return (B_TRUE);
			}
		} else {
			if (strncmp(buf, walkp->pcw_filters[i].pcf_string,
			    len) == 0 &&
			    walkp->pcw_filters[i].pcf_string[len] == '.') {
				return (B_TRUE);
			}
		}
	}

	return (B_FALSE);
}

static boolean_t
pcieadm_cfgspace_ofmt_cb(ofmt_arg_t *ofarg, char *buf, uint_t buflen)
{
	pcieadm_cfgspace_ofmt_t *pco = ofarg->ofmt_cbarg;

	switch (ofarg->ofmt_id) {
	case PCIEADM_CFGSPACE_OT_SHORT:
		if (snprintf(buf, buflen, "%s.%s", pco->pco_base,
		    pco->pco_short) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_CFGSPACE_OT_HUMAN:
		if (strlcpy(buf, pco->pco_human, buflen) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_CFGSPACE_OT_VALUE:
		if (pco->pco_strval != NULL) {
			if (strlcpy(buf, pco->pco_strval, buflen) >= buflen) {
				return (B_FALSE);
			}
		} else {
			if (snprintf(buf, buflen, "0x%" PRIx64,
			    pco->pco_value) >= buflen) {
				return (B_FALSE);
			}
		}
		break;
	case PCIEADM_CFGSPACE_OT_BYTEOFF:
		if (snprintf(buf, buflen, "%u", pco->pco_off) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_CFGSPACE_OT_BITOFF:
		if (snprintf(buf, buflen, "%u", pco->pco_bitoff) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_CFGSPACE_OT_BYTELEN:
		if (snprintf(buf, buflen, "%u", pco->pco_len) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_CFGSPACE_OT_BITLEN:
		if (snprintf(buf, buflen, "%u", pco->pco_bitlen) >= buflen) {
			return (B_FALSE);
		}
		break;
	default:
		abort();
	}

	return (B_TRUE);
}


static const ofmt_field_t pcieadm_cfgspace_ofmt[] = {
	{ "SHORT", 30, PCIEADM_CFGSPACE_OT_SHORT, pcieadm_cfgspace_ofmt_cb },
	{ "HUMAN", 30, PCIEADM_CFGSPACE_OT_HUMAN, pcieadm_cfgspace_ofmt_cb },
	{ "VALUE", 20, PCIEADM_CFGSPACE_OT_VALUE, pcieadm_cfgspace_ofmt_cb },
	{ "OFFSET", 8, PCIEADM_CFGSPACE_OT_BYTEOFF, pcieadm_cfgspace_ofmt_cb },
	{ "BITOFF", 8, PCIEADM_CFGSPACE_OT_BITOFF, pcieadm_cfgspace_ofmt_cb },
	{ "LENGTH", 8, PCIEADM_CFGSPACE_OT_BYTELEN, pcieadm_cfgspace_ofmt_cb },
	{ "BITLEN", 8, PCIEADM_CFGSPACE_OT_BITLEN, pcieadm_cfgspace_ofmt_cb },
	{ NULL, 0, 0, NULL }
};

static void
pcieadm_cfgspace_print_parse(pcieadm_cfgspace_walk_t *walkp,
    const char *sname, const char *human, uint64_t value, uint32_t len,
    uint32_t bitlen, uint32_t off, uint32_t bitoff)
{
	pcieadm_cfgspace_ofmt_t pco;

	VERIFY3P(walkp->pcw_filt, !=, NULL);
	pco.pco_base = walkp->pcw_filt->pstr_curgen;
	pco.pco_short = sname;
	pco.pco_human = human;
	pco.pco_value = value;
	pco.pco_strval = NULL;
	pco.pco_off = walkp->pcw_capoff + off;
	pco.pco_bitoff = bitoff;
	pco.pco_len = len;
	pco.pco_bitlen = bitlen;
	ofmt_print(walkp->pcw_ofmt, &pco);
}

typedef void (*pcieadm_cfgspace_print_f)(pcieadm_cfgspace_walk_t *,
    const pcieadm_cfgspace_print_t *, const void *);

struct pcieadm_cfgspace_print {
	uint8_t pcp_off;
	uint8_t pcp_len;
	const char *pcp_short;
	const char *pcp_human;
	pcieadm_cfgspace_print_f pcp_print;
	const void *pcp_arg;
};

static uint32_t
pcieadm_cfgspace_getcap32(pcieadm_cfgspace_walk_t *walkp, uint32_t off)
{
	off += walkp->pcw_capoff;
	VERIFY3U(off, <, PCIE_CONF_HDR_SIZE);
	VERIFY0(off % 4);
	return (walkp->pcw_data->pcb_u32[off / 4]);
}

static void
pcieadm_field_vprintf(pcieadm_cfgspace_walk_t *walkp, const char *shortf,
    const char *humanf, uint64_t val, uint32_t len, uint32_t bitlen,
    uint32_t off, uint32_t bitoff, const char *fmt, va_list ap)
{
	if (!pcieadm_cfgspace_filter(walkp, shortf))
		return;

	if (walkp->pcw_ofmt != NULL) {
		pcieadm_cfgspace_print_parse(walkp, shortf, humanf, val, len,
		    bitlen, off, bitoff);
		return;
	}

	if (walkp->pcw_pcieadm->pia_indent > 0) {
		(void) printf("%*s", walkp->pcw_pcieadm->pia_indent, "");
	}

	if ((walkp->pcw_flags & PCIEADM_CFGSPACE_F_SHORT) != 0) {
		(void) printf("|--> %s (%s.%s): ", humanf,
		    walkp->pcw_filt->pstr_curgen, shortf);
	} else {
		(void) printf("|--> %s: ", humanf);
	}

	(void) vprintf(fmt, ap);
}

static void __PRINTFLIKE(5)
pcieadm_regdef_printf(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const pcieadm_regdef_t *regdef,
    uint64_t val, const char *fmt, ...)
{
	va_list ap;
	uint32_t off = print->pcp_off + (regdef->prd_lowbit / NBBY);
	uint32_t bitoff = regdef->prd_lowbit % NBBY;
	uint32_t len = regdef->prd_hibit - regdef->prd_lowbit + 1;

	va_start(ap, fmt);
	pcieadm_field_vprintf(walkp, regdef->prd_short, regdef->prd_human, val,
	    len / NBBY, len % NBBY, off, bitoff, fmt, ap);
	va_end(ap);
}

static void __PRINTFLIKE(9)
pcieadm_field_printf(pcieadm_cfgspace_walk_t *walkp, const char *shortf,
    const char *humanf, uint64_t val, uint32_t len, uint32_t bitlen,
    uint32_t off, uint32_t bitoff, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	pcieadm_field_vprintf(walkp, shortf, humanf, val, len, bitlen, off,
	    bitoff, fmt, ap);
	va_end(ap);
}

static void __PRINTFLIKE(4)
pcieadm_cfgspace_printf(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, uint64_t val, const char *fmt, ...)
{
	va_list ap;

	if (!pcieadm_cfgspace_filter(walkp, print->pcp_short))
		return;

	if (walkp->pcw_ofmt != NULL) {
		pcieadm_cfgspace_print_parse(walkp, print->pcp_short,
		    print->pcp_human, val, print->pcp_len, 0, print->pcp_off,
		    0);
		return;
	}

	if (walkp->pcw_pcieadm->pia_indent > 0) {
		(void) printf("%*s", walkp->pcw_pcieadm->pia_indent, "");
	}

	if ((walkp->pcw_flags & PCIEADM_CFGSPACE_F_SHORT) != 0) {
		(void) printf("%s (%s.%s): ", print->pcp_human,
		    walkp->pcw_filt->pstr_curgen, print->pcp_short);
	} else {
		(void) printf("%s: ", print->pcp_human);
	}

	va_start(ap, fmt);
	(void) vprintf(fmt, ap);
	va_end(ap);
}

static void
pcieadm_cfgspace_puts(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const char *str)
{
	if (!pcieadm_cfgspace_filter(walkp, print->pcp_short))
		return;

	if (walkp->pcw_ofmt != NULL) {
		pcieadm_cfgspace_ofmt_t pco;

		VERIFY3P(walkp->pcw_filt, !=, NULL);
		pco.pco_base = walkp->pcw_filt->pstr_curgen;
		pco.pco_short = print->pcp_short;
		pco.pco_human = print->pcp_human;
		pco.pco_strval = str;
		pco.pco_off = walkp->pcw_capoff + print->pcp_off;
		pco.pco_bitoff = 0;
		pco.pco_len = print->pcp_len;
		pco.pco_bitlen = 0;
		ofmt_print(walkp->pcw_ofmt, &pco);
		return;
	}

	if (walkp->pcw_pcieadm->pia_indent > 0) {
		(void) printf("%*s", walkp->pcw_pcieadm->pia_indent, "");
	}

	if ((walkp->pcw_flags & PCIEADM_CFGSPACE_F_SHORT) != 0) {
		(void) printf("%s (%s.%s): %s\n", print->pcp_human,
		    walkp->pcw_filt->pstr_curgen, print->pcp_short, str);
	} else {
		(void) printf("%s: %s\n", print->pcp_human, str);
	}
}

static uint64_t
pcieadm_cfgspace_extract(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print)
{
	uint32_t val = 0;

	VERIFY3U(print->pcp_len, <=, 8);
	VERIFY3U(print->pcp_off + print->pcp_len + walkp->pcw_capoff, <=,
	    walkp->pcw_valid);
	for (uint8_t i = print->pcp_len; i > 0; i--) {
		val <<= 8;
		val |= walkp->pcw_data->pcb_u8[walkp->pcw_capoff +
		    print->pcp_off + i - 1];
	}

	return (val);
}

static uint16_t
pcieadm_cfgspace_extract_u16(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print)
{
	VERIFY(print->pcp_len == 2);
	return ((uint16_t)pcieadm_cfgspace_extract(walkp, print));
}

static void
pcieadm_cfgspace_print_unit(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	const pcieadm_unitdef_t *unit = arg;
	uint64_t rawval = pcieadm_cfgspace_extract(walkp, print);
	uint64_t val = rawval;

	if (unit->pcd_mult > 1) {
		val *= unit->pcd_mult;
	}
	pcieadm_cfgspace_printf(walkp, print, rawval, "0x%" PRIx64 " %s%s\n",
	    val, unit->pcd_unit, val != 1 ? "s" : "");
}

static void
pcieadm_cfgspace_print_regdef(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	const pcieadm_regdef_t *regdef = arg;
	uint64_t val = pcieadm_cfgspace_extract(walkp, print);

	pcieadm_cfgspace_printf(walkp, print, val, "0x%" PRIx64 "\n", val);

	pcieadm_indent();
	pcieadm_strfilt_push(walkp, print->pcp_short);

	for (regdef = arg; regdef->prd_short != NULL; regdef++) {
		uint32_t nbits = regdef->prd_hibit - regdef->prd_lowbit + 1UL;
		uint32_t bitmask = (1UL << nbits) - 1UL;
		uint64_t regval = (val >> regdef->prd_lowbit) & bitmask;
		const char *strval;
		uint64_t actval;

		if (!pcieadm_cfgspace_filter(walkp, regdef->prd_short)) {
			continue;
		}

		switch (regdef->prd_valtype) {
		case PRDV_STRVAL:
			strval = regdef->prd_val.prdv_strval[regval];
			if (strval == NULL) {
				strval = "reserved";
			}

			pcieadm_regdef_printf(walkp, print, regdef, regval,
			    "%s (0x%" PRIx64 ")\n", strval, regval <<
			    regdef->prd_lowbit);
			break;
		case PRDV_HEX:
			actval = regval;
			if (regdef->prd_val.prdv_hex.pra_shift > 0) {
				actval <<= regdef->prd_val.prdv_hex.pra_shift;
			}
			actval += regdef->prd_val.prdv_hex.pra_addend;

			pcieadm_regdef_printf(walkp, print, regdef, regval,
			    "0x%" PRIx64 "\n", actval);
			break;
		case PRDV_BITFIELD:
			pcieadm_regdef_printf(walkp, print, regdef, regval,
			    "0x%" PRIx64 "\n", regval << regdef->prd_lowbit);

			if (walkp->pcw_ofmt == NULL) {
				pcieadm_indent();
				for (uint32_t i = 0; i < nbits; i++) {
					if (((1 << i) & regval) == 0)
						continue;
					pcieadm_print("|--> %s (0x%x)\n",
					    regdef->prd_val.prdv_strval[i],
					    1UL << (i + regdef->prd_lowbit));
				}
				pcieadm_deindent();
			}
			break;
		case PRDV_CUSTOM:
			regdef->prd_val.prdv_func(walkp, print, regdef, regval);
			break;
		}
	}

	pcieadm_strfilt_pop(walkp);
	pcieadm_deindent();
}

static void
pcieadm_cfgspace_print_strmap(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	const pcieadm_strmap_t *strmap = arg;
	uint64_t val = pcieadm_cfgspace_extract(walkp, print);
	const char *str = "reserved";

	for (uint_t i = 0; strmap[i].psr_str != NULL; i++) {
		if (strmap[i].psr_val == val) {
			str = strmap[i].psr_str;
			break;
		}
	}

	pcieadm_cfgspace_printf(walkp, print, val, "0x%" PRIx64 " -- %s\n",
	    val, str);
}

static void
pcieadm_cfgspace_print_hex(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint64_t val = pcieadm_cfgspace_extract(walkp, print);

	pcieadm_cfgspace_printf(walkp, print, val, "0x%" PRIx64 "\n", val);
}

static void
pcieadm_cfgspace_print_vendor(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	pcidb_vendor_t *vend;
	uint16_t vid = pcieadm_cfgspace_extract_u16(walkp, print);

	vend = pcidb_lookup_vendor(walkp->pcw_pcieadm->pia_pcidb, vid);
	if (vend != NULL) {
		pcieadm_cfgspace_printf(walkp, print, vid, "0x%x -- %s\n", vid,
		    pcidb_vendor_name(vend));
	} else {
		pcieadm_cfgspace_printf(walkp, print, vid, "0x%x\n", vid);
	}
}

static void
pcieadm_cfgspace_print_device(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	pcidb_device_t *dev;
	uint16_t did = pcieadm_cfgspace_extract_u16(walkp, print);
	uint16_t vid = walkp->pcw_data->pcb_u8[PCI_CONF_VENID] +
	    (walkp->pcw_data->pcb_u8[PCI_CONF_VENID + 1] << 8);

	dev = pcidb_lookup_device(walkp->pcw_pcieadm->pia_pcidb, vid, did);
	if (dev != NULL) {
		pcieadm_cfgspace_printf(walkp, print, did, "0x%x -- %s\n", did,
		    pcidb_device_name(dev));
	} else {
		pcieadm_cfgspace_printf(walkp, print, did, "0x%x\n", did);
	}
}

/*
 * To print out detailed information about a subsystem vendor or device, we need
 * all of the information about the vendor and device due to the organization of
 * the PCI IDs db.
 */
static void
pcieadm_cfgspace_print_subid(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint16_t vid = walkp->pcw_data->pcb_u8[PCI_CONF_VENID] +
	    (walkp->pcw_data->pcb_u8[PCI_CONF_VENID + 1] << 8);
	uint16_t did = walkp->pcw_data->pcb_u8[PCI_CONF_DEVID] +
	    (walkp->pcw_data->pcb_u8[PCI_CONF_DEVID + 1] << 8);
	uint16_t svid = walkp->pcw_data->pcb_u8[PCI_CONF_SUBVENID] +
	    (walkp->pcw_data->pcb_u8[PCI_CONF_SUBVENID + 1] << 8);
	uint16_t sdid = walkp->pcw_data->pcb_u8[PCI_CONF_SUBSYSID] +
	    (walkp->pcw_data->pcb_u8[PCI_CONF_SUBSYSID + 1] << 8);
	uint16_t val = pcieadm_cfgspace_extract_u16(walkp, print);
	boolean_t isvendor = print->pcp_off == PCI_CONF_SUBVENID;

	if (isvendor) {
		pcidb_vendor_t *vend;
		vend = pcidb_lookup_vendor(walkp->pcw_pcieadm->pia_pcidb,
		    svid);
		if (vend != NULL) {
			pcieadm_cfgspace_printf(walkp, print, val,
			    "0x%x -- %s\n", val, pcidb_vendor_name(vend));
		} else {
			pcieadm_cfgspace_printf(walkp, print, val,
			    "0x%x\n", val);
		}
	} else {
		pcidb_subvd_t *subvd;
		subvd = pcidb_lookup_subvd(walkp->pcw_pcieadm->pia_pcidb, vid,
		    did, svid, sdid);
		if (subvd != NULL) {
			pcieadm_cfgspace_printf(walkp, print, val,
			    "0x%x -- %s\n", val, pcidb_subvd_name(subvd));
		} else {
			pcieadm_cfgspace_printf(walkp, print, val, "0x%x\n",
			    val);
		}
	}
}

/*
 * The variable natures of BARs is a pain. This makes printing this out and the
 * fields all a bit gross.
 */
static void
pcieadm_cfgspace_print_bars(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint32_t *barp = &walkp->pcw_data->pcb_u32[(walkp->pcw_capoff +
	    print->pcp_off) / 4];
	char barname[32];
	const char *typestrs[2] = { "Memory Space", "I/O Space" };

	for (uint_t i = 0; i < print->pcp_len / 4; i++) {
		uint_t type;
		uint32_t off = print->pcp_off + i * 4;
		(void) snprintf(barname, sizeof (barname), "%s%u",
		    print->pcp_short, i);

		type = barp[i] & PCI_BASE_SPACE_M;

		if (pcieadm_cfgspace_filter(walkp, barname) &&
		    walkp->pcw_ofmt == NULL) {
			if ((walkp->pcw_flags & PCIEADM_CFGSPACE_F_SHORT) !=
			    0) {
				pcieadm_print("%s %u (%s.%s)\n",
				    print->pcp_human, i,
				    walkp->pcw_filt->pstr_curgen, barname);
			} else {
				pcieadm_print("%s %u\n", print->pcp_human, i);
			}
		}

		pcieadm_strfilt_push(walkp, barname);
		pcieadm_indent();

		pcieadm_field_printf(walkp, "space", "Space", type, 0, 1, off,
		    0, "%s (0x%x)\n", typestrs[type], type);

		if (type == PCI_BASE_SPACE_IO) {
			uint32_t addr = barp[i] & PCI_BASE_IO_ADDR_M;

			pcieadm_field_printf(walkp, "addr", "Address", addr,
			    3, 30, off, 2, "0x%" PRIx32 "\n", addr);
		} else {
			uint8_t type, pre;
			uint64_t addr;
			uint32_t blen = 3;
			const char *locstr;

			type = barp[i] & PCI_BASE_TYPE_M;
			pre = barp[i] & PCI_BASE_PREF_M;
			addr = barp[i] & PCI_BASE_M_ADDR_M;

			if (type == PCI_BASE_TYPE_ALL) {
				addr += (uint64_t)barp[i+1] << 32;
				blen += 4;
				i++;
			}

			pcieadm_field_printf(walkp, "addr", "Address", addr,
			    blen, 28, off, 4, "0x%" PRIx64 "\n", addr);

			switch (type) {
			case PCI_BASE_TYPE_MEM:
				locstr = "32-bit";
				break;
			case PCI_BASE_TYPE_LOW:
				locstr = "Sub-1 MiB";
				break;
			case PCI_BASE_TYPE_ALL:
				locstr = "64-bit";
				break;
			case PCI_BASE_TYPE_RES:
			default:
				locstr = "Reserved";
				break;
			}

			pcieadm_field_printf(walkp, "type", "Memory Type",
			    type >> 1, 0, 2, off, 1, "%s (0x%x)\n", locstr,
			    type >> 1);
			pcieadm_field_printf(walkp, "prefetch", "Prefetchable",
			    pre != 0, 0, 1, off, 3, "%s (0x%x)\n", pre != 0 ?
			    "yes" : "no", pre != 0);
		}

		pcieadm_deindent();
		pcieadm_strfilt_pop(walkp);
	}
}

static void
pcieadm_cfgspace_print_ecv(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint16_t bitlen, nwords;

	if (bitx8(walkp->pcw_data->pcb_u8[walkp->pcw_capoff + 4], 5, 5) == 0) {
		return;
	}

	bitlen = walkp->pcw_data->pcb_u8[walkp->pcw_capoff + 5];
	if (bitlen == 0) {
		bitlen = 256;
	}

	nwords = bitlen / 32;
	if ((bitlen % 8) != 0) {
		nwords++;
	}

	for (uint16_t i = 0; i < nwords; i++) {
		char tshort[32], thuman[128];
		pcieadm_cfgspace_print_t p;

		(void) snprintf(tshort, sizeof (tshort), "ecv%u", i);
		(void) snprintf(thuman, sizeof (thuman), "Egress Control "
		    "Vector %u", i);
		p.pcp_off = print->pcp_off + i * 4;
		p.pcp_len = 4;
		p.pcp_short = tshort;
		p.pcp_human = thuman;
		p.pcp_print = pcieadm_cfgspace_print_hex;
		p.pcp_arg = NULL;

		p.pcp_print(walkp, &p, p.pcp_arg);
	}
}

static void
pcieadm_cfgspace_print_dpa_paa(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint8_t nents;

	nents = bitx8(walkp->pcw_data->pcb_u8[walkp->pcw_capoff + 4], 4, 0) + 1;
	if (nents == 0) {
		return;
	}

	for (uint8_t i = 0; i < nents; i++) {
		char tshort[32], thuman[128];
		pcieadm_cfgspace_print_t p;

		(void) snprintf(tshort, sizeof (tshort), "%s%u",
		    print->pcp_short, i);
		(void) snprintf(thuman, sizeof (thuman), "%s %u",
		    print->pcp_human, i);

		p.pcp_off = print->pcp_off + i;
		p.pcp_len = 1;
		p.pcp_short = tshort;
		p.pcp_human = thuman;
		p.pcp_print = pcieadm_cfgspace_print_hex;
		p.pcp_arg = NULL;

		p.pcp_print(walkp, &p, p.pcp_arg);
	}
}

static void
pcieadm_cfgspace_print_table(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print)
{
	for (; print->pcp_short != NULL; print++) {
		VERIFY3P(print->pcp_print, !=, NULL);
		print->pcp_print(walkp, print, print->pcp_arg);
	}
}

/*
 * Config Space Header Table Definitions
 */
static const pcieadm_regdef_t pcieadm_regdef_command[] = {
	{ 0, 0, "io", "I/O Space", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "mem", "Memory Space", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "bus", "Bus Master", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 3, 3, "spec", "Special Cycle", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 4, "mwi", "Memory Write and Invalidate", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 5, 5, "vga", "VGA Palette Snoop", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 6, 6, "per", "Parity Error Response", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 7, 7, "idsel", "IDSEL Stepping/Wait Cycle Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 8, "serr", "SERR# Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } }, },
	{ 9, 9, "fbtx", "Fast Back-to-Back Transactions", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } }, },
	{ 10, 10, "intx", "Interrupt X", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "enabled", "disabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_status[] = {
	{ 0, 0, "imm", "Immediate Readiness", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } }, },
	{ 3, 3, "istat", "Interrupt Status", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not pending", "pending" } }, },
	{ 4, 4, "capsup", "Capabilities List", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } }, },
	{ 5, 5, "66mhz", "66 MHz Capable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } }, },
	{ 7, 7, "fbtxcap", "Fast Back-to-Back Capable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } }, },
	{ 8, 8, "mdperr", "Master Data Parity Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no error", "error detected" } }, },
	{ 9, 10, "devsel", "DEVSEL# Timing", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "fast", "medium", "slow",
	    "reserved" } } },
	{ 11, 11, "sta", "Signaled Target Abort", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 12, 12, "rta", "Received Target Abort", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 13, 13, "rma", "Received Master Abort", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 14, 14, "sse", "Signaled System Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 15, 15, "dpe", "Detected Parity Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};

/*
 * It might be interesting to translate these into numbers at a future point.
 */
static const pcieadm_regdef_t pcieadm_regdef_class[] = {
	{ 16, 23, "class", "Class Code", PRDV_HEX },
	{ 8, 15, "sclass", "Sub-Class Code", PRDV_HEX },
	{ 0, 7, "pi", "Programming Interface", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_bridge_iobase[] = {
	{ 0, 3, "cap", "Addressing Capability", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "16-bit", "32-bit" } } },
	{ 4, 7, "base", "Base", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 12 } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_bridge_iolim[] = {
	{ 0, 3, "cap", "Addressing Capability", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "16-bit", "32-bit" } } },
	{ 4, 7, "limit", "Limit", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 12, 0xfff } } },
	{ -1, -1, NULL }
};


static const pcieadm_regdef_t pcieadm_regdef_bridgests[] = {
	{ 5, 5, "66mhz", "66 MHz", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 7, 7, "fastb2b", "Fast Back-to-Back Transactions", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 8, "mdperr", "Master Data Parity Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no error", "error detected" } } },
	{ 9, 10, "devsel", "DEVSEL# Timing", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "fast", "medium", "slow" } } },
	{ 11, 11, "sta", "Signaled Target Abort", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no abort", "aborted" } } },
	{ 12, 12, "rta", "Received Target Abort", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no abort", "aborted" } } },
	{ 13, 13, "rma", "Received Master Abort", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no abort", "aborted" } } },
	{ 14, 14, "rsyserr", "Received System Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no error", "error received" } } },
	{ 15, 15, "dperr", "Detected Parity Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no error", "error detected" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_bridge_membase[] = {
	{ 4, 16, "base", "Base", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 20 } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_bridge_memlim[] = {
	{ 4, 16, "limit", "Limit", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 20, 0xfffff } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_bridge_pfbase[] = {
	{ 0, 3, "cap", "Addressing Capability", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "32-bit", "64-bit" } } },
	{ 4, 16, "base", "Base", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 20 } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_bridge_pflim[] = {
	{ 0, 3, "cap", "Addressing Capability", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "32-bit", "64-bit" } } },
	{ 4, 16, "limit", "Limit", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 20, 0xfffff } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_bridge_ctl[] = {
	{ 0, 0, "perrresp", "Parity Error Response", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "serr", "SERR#", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "isa", "ISA", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 3, 3, "vga", "VGA", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 4, "vgadec", "VGA 16-bit Decode", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "10-bit", "16-bit" } } },
	{ 5, 5, "mabort", "Master Abort", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 6, 6, "secrst", "Secondary Bus Reset", PRDV_HEX },
	{ 7, 7, "fastb2b", "Fast Back-to-Back Transactions", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 8, 8, "pridisc", "Primary Discard Timer", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "2^15 cycles", "2^10 cycles" } } },
	{ 9, 9, "secdisc", "Secondary Discard Timer", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "2^15 cycles", "2^10 cycles" } } },
	{ 10, 10, "disctimer", "Discard Timer Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 11, 11, "discserr", "Discard Timer SERR#", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static pcieadm_unitdef_t pcieadm_unitdef_cache = {
	"byte", 4
};

static pcieadm_unitdef_t pcieadm_unitdef_latreg = { "cycle" };

static const pcieadm_regdef_t pcieadm_regdef_header[] = {
	{ 0, 6, "layout", "Header Layout", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "Device", "Bridge", "PC Card" } } },
	{ 7, 7, "mfd", "Multi-Function Device", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_bist[] = {
	{ 0, 3, "code", "Completion Code", PRDV_HEX },
	{ 6, 6, "start", "Start BIST", PRDV_HEX },
	{ 7, 7, "cap", "BIST Capable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_exprom[] = {
	{ 0, 0, "enable", "Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 3, "valsts", "Validation Status", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not supported", "in progress",
	    "valid contents, no trust test performed",
	    "valid and trusted contents",
	    "invalid contents",
	    "valid but untrusted contents",
	    "valid contents with warning, no trust test performed",
	    "valid and trusted contents with warning" } } },
	{ 4, 7, "valdet", "Validation Details", PRDV_HEX },
	{ 11, 31, "addr", "Base Address", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 11 } } },
	{ -1, -1, NULL }
};

static const pcieadm_strmap_t pcieadm_strmap_ipin[] = {
	{ "none", 0 },
	{ "INTA", PCI_INTA },
	{ "INTB", PCI_INTB },
	{ "INTC", PCI_INTC },
	{ "INTD", PCI_INTD },
	{ NULL }
};


static const pcieadm_cfgspace_print_t pcieadm_cfgspace_type0[] = {
	{ 0x0, 2, "vendor", "Vendor ID", pcieadm_cfgspace_print_vendor },
	{ 0x2, 2, "device", "Device ID", pcieadm_cfgspace_print_device },
	{ 0x4, 2, "command", "Command", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_command },
	{ 0x6, 2, "status", "Status", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_status },
	{ 0x8, 1, "revision", "Revision ID", pcieadm_cfgspace_print_hex },
	{ 0x9, 3, "class", "Class Code", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_class },
	{ 0xc, 1, "cache", "Cache Line Size", pcieadm_cfgspace_print_unit,
	    &pcieadm_unitdef_cache },
	{ 0xd, 1, "latency", "Latency Timer", pcieadm_cfgspace_print_unit,
	    &pcieadm_unitdef_latreg },
	{ 0xe, 1, "type", "Header Type", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_header },
	{ 0xf, 1, "bist", "BIST", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_bist },
	{ 0x10, 24, "bar", "Base Address Register",
	    pcieadm_cfgspace_print_bars },
	{ 0x28, 4, "cis", "Cardbus CIS Pointer", pcieadm_cfgspace_print_hex },
	{ 0x2c, 2, "subvid", "Subsystem Vendor ID",
	    pcieadm_cfgspace_print_subid },
	{ 0x2e, 2, "subdev", "Subsystem Device ID",
	    pcieadm_cfgspace_print_subid },
	{ 0x30, 4, "rom", "Expansion ROM", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_exprom },
	{ 0x34, 1, "cap", "Capabilities Pointer", pcieadm_cfgspace_print_hex },
	{ 0x3c, 1, "iline", "Interrupt Line", pcieadm_cfgspace_print_hex },
	{ 0x3d, 1, "ipin", "Interrupt Pin", pcieadm_cfgspace_print_strmap,
	    pcieadm_strmap_ipin },
	{ 0x3e, 1, "gnt", "Min_Gnt", pcieadm_cfgspace_print_hex },
	{ 0x3f, 1, "lat", "Min_Lat", pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cfgspace_type1[] = {
	{ 0x0, 2, "vendor", "Vendor ID", pcieadm_cfgspace_print_vendor },
	{ 0x2, 2, "device", "Device ID", pcieadm_cfgspace_print_device },
	{ 0x4, 2, "command", "Command", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_command },
	{ 0x6, 2, "status", "Status", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_status },
	{ 0x8, 1, "revision", "Revision ID", pcieadm_cfgspace_print_hex },
	{ 0x9, 3, "class", "Class Code", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_class },
	{ 0xc, 1, "cache", "Cache Line Size", pcieadm_cfgspace_print_unit,
	    &pcieadm_unitdef_cache },
	{ 0xd, 1, "latency", "Latency Timer", pcieadm_cfgspace_print_unit,
	    &pcieadm_unitdef_latreg },
	{ 0xe, 1, "type", "Header Type", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_header },
	{ 0xf, 1, "bist", "BIST", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_bist },
	{ 0x10, 8, "bar", "Base Address Register",
	    pcieadm_cfgspace_print_bars },
	{ PCI_BCNF_PRIBUS, 1, "pribus", "Primary Bus Number",
	    pcieadm_cfgspace_print_hex },
	{ PCI_BCNF_SECBUS, 1, "secbus", "Secondary Bus Number",
	    pcieadm_cfgspace_print_hex },
	{ PCI_BCNF_SUBBUS, 1, "subbus", "Subordinate Bus Number",
	    pcieadm_cfgspace_print_hex },
	{ PCI_BCNF_LATENCY_TIMER, 1, "latency2", "Secondary Latency timer",
	    pcieadm_cfgspace_print_unit, &pcieadm_unitdef_latreg },
	{ PCI_BCNF_IO_BASE_LOW, 1, "iobase", "I/O Base Low",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_bridge_iobase },
	{ PCI_BCNF_IO_LIMIT_LOW, 1, "iolimit", "I/O Limit Low",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_bridge_iolim },
	{ PCI_BCNF_SEC_STATUS, 2, "status2", "Secondary Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_bridgests },
	{ PCI_BCNF_MEM_BASE, 2, "membase", "Memory Base",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_bridge_membase },
	{ PCI_BCNF_MEM_LIMIT, 2, "memlimit", "Memory Limit",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_bridge_memlim },
	{ PCI_BCNF_PF_BASE_LOW, 2, "pfbase", "Prefetchable Memory Base",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_bridge_pfbase },
	{ PCI_BCNF_PF_LIMIT_LOW, 2, "pflimit", "Prefetchable Memory Limit",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_bridge_pflim },
	{ PCI_BCNF_PF_BASE_HIGH, 4, "pfbasehi",
	    "Prefetchable Base Upper 32 bits",
	    pcieadm_cfgspace_print_hex },
	{ PCI_BCNF_PF_LIMIT_HIGH, 4, "pflimihi",
	    "Prefetchable Limit Upper 32 bits",
	    pcieadm_cfgspace_print_hex },
	{ PCI_BCNF_IO_BASE_HI, 2, "iobasehi", "I/O Base Upper 16 bits",
	    pcieadm_cfgspace_print_hex },
	{ PCI_BCNF_IO_LIMIT_HI, 2, "iolimithi", "I/O Limit Upper 16 bits",
	    pcieadm_cfgspace_print_hex },
	{ PCI_BCNF_CAP_PTR, 1, "cap", "Capabilities Pointer",
	    pcieadm_cfgspace_print_hex },
	{ PCI_BCNF_ROM, 4, "rom", "Expansion ROM",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_exprom },
	{ PCI_BCNF_ILINE, 1, "iline", "Interrupt Line",
	    pcieadm_cfgspace_print_hex },
	{ PCI_BCNF_IPIN, 1, "ipin", "Interrupt Pin",
	    pcieadm_cfgspace_print_strmap, pcieadm_strmap_ipin },
	{ PCI_BCNF_BCNTRL, 2, "bctl", "Bridge Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_bridge_ctl },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cfgspace_unknown[] = {
	{ 0x0, 2, "vendor", "Vendor ID", pcieadm_cfgspace_print_vendor },
	{ 0x2, 2, "device", "Device ID", pcieadm_cfgspace_print_device },
	{ 0x8, 1, "revision", "Revision ID", pcieadm_cfgspace_print_hex },
	{ 0xe, 1, "type", "Header Type", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_header },
	{ -1, -1, NULL }
};

/*
 * Power Management Capability Version 3. Note versions two and three seem to be
 * the same, but are used to indicate compliance to different revisions of the
 * PCI power management specification.
 */
static const pcieadm_regdef_t pcieadm_regdef_pmcap[] = {
	{ 0, 2, "vers", "Version", PRDV_HEX },
	{ 3, 3, "clock", "PME Clock", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not required", "required" } } },
	{ 4, 4, "irrd0", "Immediate Readiness on Return to D0", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 5, 5, "dsi", "Device Specific Initialization", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 6, 8, "auxcur", "Auxiliary Current", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "0", "55 mA", "100 mA", "160 mA",
	    "220 mA", "270 mA", "320 mA", "375 mA" } } },
	{ 9, 9, "d1", "D1", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 10, 10, "d2", "D2", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 11, 15, "pme", "PME Support", PRDV_BITFIELD,
	    .prd_val = { .prdv_strval = { "D0", "D1", "D2", "D3hot",
	    "D3cold" } } },
	{ -1, -1, NULL }
};


static const pcieadm_cfgspace_print_t pcieadm_cap_pcipm_v3[] = {
	{ PCI_PMCAP, 2, "pmcap", "Power Management Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pmcap },
	{ -1, -1, NULL }
};

/*
 * PCI Bridge Subsystem Capability
 */
static const pcieadm_cfgspace_print_t pcieadm_cap_bridge_subsys[] = {
	{ 0x4, 2, "subvid", "Subsystem Vendor ID", pcieadm_cfgspace_print_hex },
	{ 0x6, 2, "subdev", "Subsystem Device ID", pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

/*
 * MSI Capability
 */
static const pcieadm_regdef_t pcieadm_regdef_msictrl[] = {
	{ 0, 0, "enable", "MSI Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 3, "mmsgcap", "Multiple Message Capable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "1 vector", "2 vectors",
	    "4 vectors", "8 vectors", "16 vectors", "32 vectors" } } },
	{ 4, 6, "mmsgen", "Multiple Message Enabled", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "1 vector", "2 vectors",
	    "4 vectors", "8 vectors", "16 vectors", "32 vectors" } } },
	{ 7, 7, "addr64", "64-bit Address Capable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 8, "pvm", "Per-Vector Masking Capable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 9, 9, "extmdcap", "Extended Message Data Capable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 10, 10, "extmden", "extended Message Data Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_msi_32[] = {
	{ PCI_MSI_CTRL, 2, "ctrl", "Message Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_msictrl },
	{ PCI_MSI_ADDR_OFFSET, 4, "addr", "Message Address",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_32BIT_DATA, 2, "data", "Message Data",
	    pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_msi_32ext[] = {
	{ PCI_MSI_CTRL, 2, "ctrl", "Message Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_msictrl },
	{ PCI_MSI_ADDR_OFFSET, 4, "addr", "Message Address",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_32BIT_DATA, 2, "data", "Message Data",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_32BIT_EXTDATA, 2, "extdata", "Extended Message Data",
	    pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_msi_32pvm[] = {
	{ PCI_MSI_CTRL, 2, "ctrl", "Message Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_msictrl },
	{ PCI_MSI_ADDR_OFFSET, 4, "addr", "Message Address",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_32BIT_DATA, 2, "data", "Message Data",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_32BIT_EXTDATA, 2, "extdata", "Extended Message Data",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_32BIT_MASK, 4, "mask", "Mask Bits",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_32BIT_PENDING, 4, "pend", "Pending Bits",
	    pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_msi_64[] = {
	{ PCI_MSI_CTRL, 2, "ctrl", "Message Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_msictrl },
	{ PCI_MSI_ADDR_OFFSET, 4, "addr", "Message Address",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_64BIT_ADDR, 4, "upadd", "Upper Message Address",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_64BIT_DATA, 2, "data", "Message Data",
	    pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_msi_64ext[] = {
	{ PCI_MSI_CTRL, 2, "ctrl", "Message Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_msictrl },
	{ PCI_MSI_ADDR_OFFSET, 4, "addr", "Message Address",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_64BIT_ADDR, 4, "upadd", "Upper Message Address",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_64BIT_DATA, 2, "data", "Message Data",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_64BIT_EXTDATA, 2, "extdata", "Extended Message Data",
	    pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_msi_64pvm[] = {
	{ PCI_MSI_CTRL, 2, "ctrl", "Message Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_msictrl },
	{ PCI_MSI_ADDR_OFFSET, 4, "addr", "Message Address",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_64BIT_ADDR, 4, "upadd", "Upper Message Address",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_64BIT_DATA, 2, "data", "Message Data",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_64BIT_EXTDATA, 2, "extdata", "Extended Message Data",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_64BIT_MASKBITS, 4, "mask", "Mask Bits",
	    pcieadm_cfgspace_print_hex },
	{ PCI_MSI_64BIT_PENDING, 4, "pend", "Pending Bits",
	    pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

/*
 * MSI-X Capability
 */
static const pcieadm_regdef_t pcieadm_regdef_msixctrl[] = {
	{ 0, 10, "size", "Table Size", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 0, 1 } } },
	{ 14, 14, "mask", "Function Mask", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unmasked", "masked" } } },
	{ 15, 15, "enable", "MSI-X Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_msixtable[] = {
	{ 0, 2, "bir", "Table BIR", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "BAR 0", "BAR 1", "BAR 2", "BAR 3",
	    "BAR 4", "BAR 5" } } },
	{ 3, 31, "offset", "Table Offset", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 3 } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_msixpba[] = {
	{ 0, 2, "bir", "PBA BIR", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "BAR 0", "BAR 1", "BAR 2", "BAR 3",
	    "BAR 4", "BAR 5" } } },
	{ 3, 31, "offset", "PBA Offset", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 3 } } },
	{ -1, -1, NULL }
};


static const pcieadm_cfgspace_print_t pcieadm_cap_msix[] = {
	{ PCI_MSIX_CTRL, 2, "ctrl", "Control Register",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_msixctrl },
	{ PCI_MSIX_TBL_OFFSET, 4, "table", "Table Offset",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_msixtable },
	{ PCI_MSIX_PBA_OFFSET, 4, "pba", "PBA Offset",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_msixpba },
	{ -1, -1, NULL }
};

/*
 * PCI Express Capability
 */
static const pcieadm_regdef_t pcieadm_regdef_pcie_cap[] = {
	{ 0, 3, "vers", "Version", PRDV_HEX },
	{ 4, 7, "type", "Device/Port Type", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "PCIe Endpoint",
	    "Legacy PCIe Endpoint", NULL, NULL,
	    "Root Port of PCIe Root Complex",
	    "Upstream Port of PCIe Switch",
	    "Downstream Port of PCIe Switch",
	    "PCIe to PCI/PCI-X Bridge",
	    "PCI/PCI-x to PCIe Bridge",
	    "RCiEP",
	    "Root Complex Event Collector" } } },
	{ 8, 8, "slot", "Slot Implemented", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "No", "Yes" } } },
	{ 9, 13, "intno", "Interrupt Message Number", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_devcap[] = {
	{ 0, 2, "mps", "Max Payload Size Supported", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "128 bytes", "256 bytes",
	    "512 bytes", "1024 bytes", "2048 bytes", "4096 bytes" } } },
	{ 3, 4, "pfunc", "Phantom Functions Supported", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "No", "1-bit", "2-bits",
	    "3-bits" } } },
	{ 5, 5, "exttag", "Extended Tag Field", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "5-bit", "8-bit" } } },
	{ 6, 8, "l0slat", "L0s Acceptable Latency", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "64 ns", "128 ns", "256 ns",
	    "512 ns", "1 us", "2 us", "4 us", "No limit" } } },
	{ 9, 11, "l1lat", "L1 Acceptable Latency", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "1 us", "2 us", "4 us", "8 us",
	    "16 us", "32 us", "64 us", "No limit" } } },
	{ 15, 15, "rber", "Role Based Error Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 16, 16, "errcor", "ERR_COR Subclass", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 18, 25, "csplv", "Captured Slot Power Limit", PRDV_HEX },
	{ 26, 27, "cspls", "Captured Slot Power Limit Scale", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "1.0x", "0.1x", "0.01x",
	    "0.001x" } } },
	{ 28, 28, "flr", "Function Level Reset", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_devctl[] = {
	{ 0, 0, "corerr", "Correctable Error Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "nferr", "Non-Fatal Error Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "ferr", "Fatal Error Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 3, 3, "unsupreq", "Unsupported Request Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 4, "relord", "Relaxed Ordering", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 5, 7, "mps", "Max Payload Size", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "128 bytes", "256 bytes",
	    "512 bytes", "1024 bytes", "2048 bytes", "4096 bytes" } } },
	{ 8, 8, "exttag", "Extended Tag Field", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 9, 9, "pfunc", "Phantom Functions", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 9, 9, "auxpm", "Aux Power PM", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 11, 11, "nosnoop", "No Snoop", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 12, 14, "mrrs", "Max Read Request Size", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "128 bytes", "256 bytes",
	    "512 bytes", "1024 bytes", "2048 bytes", "4096 bytes" } } },
	{ 15, 15, "bcrflr", "Bridge Configuration Retry / Function Level Reset",
	    PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_devsts[] = {
	{ 0, 0, "corerr", "Correctable Error Detected", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 1, 1, "nferr", "Non-Fatal Error Detected", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 2, 2, "ferr", "Fatal Error Detected", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 3, 3, "unsupreq", "Unsupported Request Detected", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 4, 4, "auxpm", "AUX Power Detected", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 5, 5, "txpend", "Transactions Pending", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 6, 6, "eprd", "Emergency Power Reduction Detected", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_linkcap[] = {
	{ 0, 3, "maxspeed", "Maximum Link Speed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { NULL, "2.5 GT/s", "5.0 GT/s",
	    "8.0 GT/s", "16.0 GT/s", "32.0 GT/s", "64.0 GT/s" } } },
	{ 4, 9, "maxwidth", "Maximum Link Width", PRDV_HEX },
	{ 10, 11, "aspm", "ASPM Support", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "None", "L0s", "L1", "L0s/L1" } } },
	{ 12, 14, "l0slat", "L0s Exit Latency", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "<64ns", "64-128ns", "128-256ns",
	    "256-512ns", "512ns-1us", "1-2us", "2-4us", ">4us" } } },
	{ 15, 17, "l1lat", "L1 Exit Latency", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "<1us", "1-2us", "2-4us", "4-8us",
	    "8-16us", "16-32us" "32-64us", ">64us" } } },
	{ 18, 18, "clockpm", "Clock Power Management", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 19, 19, "supdown", "Surprise Down Error Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 20, 20, "dlact", "Data Link Layer Active Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 21, 21, "linkbw", "Link Bandwidth Notification Capability",
	    PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 22, 22, "aspmcomp", "ASPM Optionality Compliance", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not compliant", "compliant" } } },
	{ 24, 31, "portno", "Port Number", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_linkctl[] = {
	{ 0, 1, "aspmctl", "ASPM Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "None", "L0s", "L1", "L0s/L1" } } },
	{ 3, 3, "rcb", "Read Completion Boundary", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "64 byte", "128 byte" } } },
	{ 4, 4, "disable", "Link Disable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not force disabled",
	    "force disabled" } } },
	{ 5, 5, "retrain", "Retrain Link", PRDV_HEX },
	{ 6, 6, "ccc", "Common Clock Configuration", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "asynchronous", "common" } } },
	{ 7, 7, "extsync", "Extended Sync", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 8, 8, "clkpm", "Clock Power Management", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 9, 9, "hwawd", "Hardware Autonomous Width", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "enabled", "disabled" } } },
	{ 10, 10, "linkbwint", "Link Bandwidth Management Interrupt",
	    PRDV_STRVAL, .prd_val = { .prdv_strval = { "disabled",
	    "enabled" } } },
	{ 11, 11, "linkabwint", "Link Autonomous Bandwidth Interrupt",
	    PRDV_STRVAL, .prd_val = { .prdv_strval = { "disabled",
	    "enabled" } } },
	{ 14, 15, "drs", "DRS Signaling Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not reported", "Interrupt enabled",
	    "DRS->FRS enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_linksts[] = {
	{ 0, 3, "speed", "Link Speed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { NULL, "2.5 GT/s", "5.0 GT/s",
	    "8.0 GT/s", "16.0 GT/s", "32.0 GT/s", "64.0 GT/s" } } },
	{ 4, 9, "width", "Link Width", PRDV_HEX },
	{ 11, 11, "training", "Link Training", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 12, 12, "slotclk", "Slot Clock Configuration", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "asynchronous", "common" } } },
	{ 13, 13, "dllact", "Data Link Layer Link Active", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 14, 14, "linkbw", "Link Bandwidth Management Status", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no change", "change occurred" } } },
	{ 15, 15, "linkabw", "Link Autonomous Bandwidth Status", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no change", "change occurred" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_slotcap[] = {
	{ 0, 0, "attnbtn", "Attention Button Present", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 1, 1, "pwrctrl", "Power Controller Present", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 2, 2, "mrlsen", "MRL Sensor Present", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 3, 3, "attnind", "Attention Indicator Present", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 4, 4, "pwrind", "Power Indicator Present", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 5, 5, "hpsup", "Hot-Plug Surprise", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 6, 6, "hpcap", "Hot-Plug Capable ", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 7, 14, "slotplv", "Slot Power Limit Value", PRDV_HEX },
	{ 15, 16, "slotpls", "Slot Power Limit Scale", PRDV_HEX },
	{ 17, 17, "emi", "Electromechanical Interlock Present", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 18, 18, "ncc", "No Command Completed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 19, 31, "slotno", "Physical Slot Number", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_slotctl[] = {
	{ 0, 0, "attnbtn", "Attention Button Pressed Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "pwrflt", "Power Fault Detected Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "mrlchg", "MRL Sensor Changed Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 3, 3, "preschg", "Presence Detect Changed Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 4, "ccmpltint", "Command Complete Interrupt", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 5, 5, "hpi", "Hot Plug Interrupt Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 6, 7, "attnind", "Attention Indicator Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { NULL, "on", "blink", "off" } } },
	{ 8, 9, "pwrin", "Power Indicator Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { NULL, "on", "blink", "off" } } },
	{ 10, 10, "pwrctrl", "Power Controller Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "power on", "power off" } } },
	{ 11, 11, "emi", "Electromechanical Interlock Control", PRDV_HEX },
	{ 12, 12, "dll", "Data Link Layer State Changed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 13, 13, "autopowdis", "Auto Slot Power Limit", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "enabled", "disabled" } } },
	{ 14, 14, "ibpddis", "In-Band PD", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "enabled", "disabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_slotsts[] = {
	{ 0, 0, "attnbtn", "Attention Button Pressed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 1, 1, "pwrflt", "Power Fault Detected", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 2, 2, "mrlchg", "MRL Sensor Changed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 3, 3, "preschg", "Presence Detect Changed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 4, 4, "ccmplt", "Command Complete", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 5, 5, "mrlsen", "MRL Sensor State", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "closed", "open" } } },
	{ 6, 6, "presdet", "Presence Detect State", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not present", "present" } } },
	{ 7, 7, "emi", "Electromechanical Interlock", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disengaged", "engaged" } } },
	{ 8, 8, "dll", "Data Link Layer State Changed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_rootcap[] = {
	{ 0, 0, "syscorerr", "System Error on Correctable Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "sysnonftl", "System Error on Non-Fatal Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "sysfatal", "System Error on Fatal Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 3, 3, "pmeie", "PME Interrupt", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 4,  "crssw", "CRS Software Visibility", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_rootctl[] = {
	{ 0, 0, "crssw", "CRS Software Visibility", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_rootsts[] = {
	{ 0, 15, "pmereqid", "PME Requester ID", PRDV_HEX },
	{ 16, 16, "pmests", "PME Status", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "deasserted", "asserted" } } },
	{ 17, 17, "pmepend", "PME Pending", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_devcap2[] = {
	{ 0, 3, "cmpto", "Completion Timeout Ranges Supported", PRDV_BITFIELD,
	    .prd_val = { .prdv_strval = { "50us-10ms", "10ms-250ms",
	    "250ms-4s", "4s-64s" } } },
	{ 4, 4, "cmptodis", "Completion Timeout Disable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 5, 5, "ari", "ARI Forwarding", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 6, 6, "atomroute", "AtomicOp Routing", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 7, 7, "atom32", "32-bit AtomicOp Completer", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 8, "atom64", "64-bit AtomicOp Completer", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 9, 9, "cas128", "128-bit CAS Completer", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 10, 10, "norelord", "No Ro-enabld PR-PR Passing", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 11, 11, "ltr", "LTR Mechanism", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 12, 13, "tph", "TPH Completer", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "TPH supported",
	    NULL, "TPH and Extended TPH supported" } } },
	{ 14, 15, "lncls", "LN System CLS", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported",
	    "LN with 64-byte cachelines", "LN with 128-byte cachelines" } } },
	{ 16, 16, "tag10comp", "10-bit Tag Completer", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 17, 17, "tag10req", "10-bit Tag Requester", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 18, 19, "obff", "OBFF", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "Message Signaling",
	    "WAKE# Signaling", "WAKE# and Message Signaling" } } },
	{ 20, 20, "extfmt", "Extended Fmt Field Supported", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 21, 21, "eetlp", "End-End TLP Prefix Supported", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 22, 23, "maxeetlp", "Max End-End TLP Prefixes", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "4", "1", "2", "3" } } },
	{ 24, 25, "empr", "Emergency Power Reduction", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported",
	    "supported, device-specific",
	    "supported, from factor or device-specific" } } },
	{ 21, 21, "emprinit",
	    "Emergency Power Reduction Initialization Required", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 31, 31, "frs", "Function Readiness Status", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_devctl2[] = {
	{ 0, 3, "cmpto", "Completion Timeout", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "50us-50ms", "50us-100us",
	    "1ms-10ms", NULL, NULL, "16ms-55ms", "65ms-210ms", NULL, NULL,
	    "260ms-900ms", "1s-3.5s", NULL, NULL, "4s-13s", "17s-64s" } } },
	{ 4, 4, "cmptodis", "Completion Timeout Disabled", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not disabled", "disabled" } } },
	{ 5, 5, "ari", "ARI Forwarding", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 6, 6, "atomreq", "AtomicOp Requester", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 7, 7, "atomblock", "AtomicOp Egress Blocking", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unblocked", "blocked" } } },
	{ 8, 8, "idoreq", "ID-Based Ordering Request", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 9, 9, "idocomp", "ID-Based Ordering Completion", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 10, 10, "ltr", "LTR Mechanism", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 11, 11, "empowred", "Emergency Power Reduction", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not requested", "requested" } } },
	{ 12, 12, "tag10req", "10-bit Tag Requester", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 13, 14, "obff", "OBFF", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "message signaling - A",
	    "message signaling - B", "WAKE# signaling" } } },
	{ 15, 15, "eetlpblk", "End-End TLP Prefix Blocking", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unblocked", "blocked" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_devsts2[] = {
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_linkcap2[] = {
	{ 1, 7, "supspeeds", "Supported Link Speeds", PRDV_BITFIELD,
	    .prd_val = { .prdv_strval = { "2.5 GT/s", "5.0 GT/s", "8.0 GT/s",
	    "16.0 GT/s", "32.0 GT/s", "64.0 GT/s" } } },
	{ 8, 8, "crosslink", "Crosslink", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 9, 15, "skposgen", "Lower SKP OS Generation Supported Speeds Vector",
	    PRDV_BITFIELD,
	    .prd_val = { .prdv_strval = { "2.5 GT/s", "5.0 GT/s", "8.0 GT/s",
	    "16.0 GT/s", "32.0 GT/s", "64.0 GT/s" } } },
	{ 16, 22, "skposrecv", "Lower SKP OS Reception Supported Speeds Vector",
	    PRDV_BITFIELD,
	    .prd_val = { .prdv_strval = { "2.5 GT/s", "5.0 GT/s", "8.0 GT/s",
	    "16.0 GT/s", "32.0 GT/s", "64.0 GT/s" } } },
	{ 23, 23, "retimedet", "Retimer Presence Detect Supported", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 24, 24, "retime2det", "Two Retimers Presence Detect Supported",
	    PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 31, 31, "drs", "Device Readiness Status", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_linkctl2[] = {
	{ 0, 3, "targspeed", "Target Link Speed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { NULL, "2.5 GT/s", "5.0 GT/s",
	    "8.0 GT/s", "16.0 GT/s", "32.0 GT/s", "64.0 GT/s" } } },
	{ 4, 4, "comp", "Enter Compliance", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 5, 5, "hwautosp", "Hardware Autonomous Speed Disable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not disabled", "disabled" } } },
	{ 6, 6, "seldeemph", "Selectable De-emphasis", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "-6 dB", "-3.5 dB" } } },
	{ 7, 9, "txmarg", "TX Margin", PRDV_HEX },
	{ 10, 10, "modcomp", "Enter Modified Compliance", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 11, 11, "compsos", "Compliance SOS",
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 12, 15, "compemph", "Compliance Preset/De-emphasis", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_linksts2[] = {
	{ 0, 0, "curdeemph", "Current De-emphasis Level", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "-6 dB", "-3.5 dB" } } },
	{ 1, 1, "eq8comp", "Equalization 8.0 GT/s Complete", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 2, 2, "eq8p1comp", "Equalization 8.0 GT/s Phase 1", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsuccessful", "successful" } } },
	{ 3, 3, "eq8p2comp", "Equalization 8.0 GT/s Phase 2", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsuccessful", "successful" } } },
	{ 4, 4, "eq8p3comp", "Equalization 8.0 GT/s Phase 3", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsuccessful", "successful" } } },
	{ 5, 5, "linkeq8req", "Link Equalization Request 8.0 GT/s", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not requested", "requested" } } },
	{ 6, 6, "retimedet", "Retimer Presence Detected", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 7, 7, "retime2det", "Two Retimers Presence Detected", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 8, 9, "crosslink", "Crosslink Resolution", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "upstream port",
	    "downstream port", "incomplete" } } },
	{ 12, 14, "dscomppres", "Downstream Component Presence", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "link down - undetermined",
	    "link down - not present", "link down - present", NULL,
	    "link up - present", "link up - present and DRS" } } },
	{ 15, 15, "drsrx", "DRS Message Received", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_slotcap2[] = {
	{ 0, 0, "ibpddis", "In-Band PD Disable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_slotctl2[] = {
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie_slotsts2[] = {
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_pcie_v1_dev[] = {
	{ PCIE_PCIECAP, 2, "cap", "Capability Register",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_cap },
	{ PCIE_DEVCAP, 4, "devcap", "Device Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devcap },
	{ PCIE_DEVSTS, 2, "devsts", "Device Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devsts },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_pcie_v1_link[] = {
	{ PCIE_PCIECAP, 2, "cap", "Capability Register",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_cap },
	{ PCIE_DEVCAP, 4, "devcap", "Device Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devcap },
	{ PCIE_DEVSTS, 2, "devsts", "Device Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devsts },
	{ PCIE_LINKCAP, 4, "linkcap", "Link Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linkcap },
	{ PCIE_LINKCTL, 2, "linkctl", "Link Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linkctl },
	{ PCIE_LINKSTS, 2, "linksts", "Link Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linksts },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_pcie_v1_slot[] = {
	{ PCIE_PCIECAP, 2, "cap", "Capability Register",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_cap },
	{ PCIE_DEVCAP, 4, "devcap", "Device Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devcap },
	{ PCIE_DEVSTS, 2, "devsts", "Device Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devsts },
	{ PCIE_LINKCAP, 4, "linkcap", "Link Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linkcap },
	{ PCIE_LINKCTL, 2, "linkctl", "Link Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linkctl },
	{ PCIE_LINKSTS, 2, "linksts", "Link Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linksts },
	{ PCIE_SLOTCAP, 4, "slotcap", "Slot Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_slotcap },
	{ PCIE_SLOTCTL, 2, "slotctl", "Slot Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_slotctl },
	{ PCIE_SLOTSTS, 2, "slotsts", "Slot Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_slotsts },
	{ -1, -1, NULL }
};


static const pcieadm_cfgspace_print_t pcieadm_cap_pcie_v1_all[] = {
	{ PCIE_PCIECAP, 2, "cap", "Capability Register",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_cap },
	{ PCIE_DEVCAP, 4, "devcap", "Device Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devcap },
	{ PCIE_DEVSTS, 2, "devsts", "Device Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devsts },
	{ PCIE_LINKCAP, 4, "linkcap", "Link Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linkcap },
	{ PCIE_LINKCTL, 2, "linkctl", "Link Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linkctl },
	{ PCIE_LINKSTS, 2, "linksts", "Link Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linksts },
	{ PCIE_SLOTCAP, 4, "slotcap", "Slot Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_slotcap },
	{ PCIE_SLOTCTL, 2, "slotctl", "Slot Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_slotctl },
	{ PCIE_SLOTSTS, 2, "slotsts", "Slot Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_slotsts },
	{ PCIE_ROOTCTL, 2, "rootctl", "Root control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_rootctl },
	{ PCIE_ROOTCAP, 2, "rootcap", "Root Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_rootcap },
	{ PCIE_ROOTSTS, 4, "rootsts", "Root Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_rootsts },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_pcie_v2[] = {
	{ PCIE_PCIECAP, 2, "cap", "Capability Register",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_cap },
	{ PCIE_DEVCAP, 4, "devcap", "Device Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devcap },
	{ PCIE_DEVCTL, 2, "devctl", "Device Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devctl },
	{ PCIE_DEVSTS, 2, "devsts", "Device Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devsts },
	{ PCIE_LINKCAP, 4, "linkcap", "Link Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linkcap },
	{ PCIE_LINKCTL, 2, "linkctl", "Link Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linkctl },
	{ PCIE_LINKSTS, 2, "linksts", "Link Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linksts },
	{ PCIE_SLOTCAP, 4, "slotcap", "Slot Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_slotcap },
	{ PCIE_SLOTCTL, 2, "slotctl", "Slot Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_slotctl },
	{ PCIE_SLOTSTS, 2, "slotsts", "Slot Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_slotsts },
	{ PCIE_ROOTCTL, 2, "rootctl", "Root Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_rootctl },
	{ PCIE_ROOTCAP, 2, "rootcap", "Root Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_rootcap },
	{ PCIE_ROOTSTS, 4, "rootsts", "Root Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_rootsts },
	{ PCIE_DEVCAP2, 4, "devcap2", "Device Capabilities 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devcap2 },
	{ PCIE_DEVCTL2, 2, "devctl2", "Device Control 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devctl2 },
	{ PCIE_DEVSTS2, 2, "devsts2", "Device Status 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_devsts2 },
	{ PCIE_LINKCAP2, 4, "linkcap2", "Link Capabilities 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linkcap2 },
	{ PCIE_LINKCTL2, 2, "linkctl2", "Link Control 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linkctl2 },
	{ PCIE_LINKSTS2, 2, "linksts2", "Link Status 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_linksts2 },
	{ PCIE_SLOTCAP2, 4, "slotcap2", "Slot Capabilities 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_slotcap2 },
	{ PCIE_SLOTCTL2, 2, "slotctl2", "Slot Control 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_slotctl2 },
	{ PCIE_SLOTSTS2, 2, "slotsts2", "Slot Status 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_slotsts2 },
	{ -1, -1, NULL }
};

/*
 * PCIe Extended Capability Header
 */
static const pcieadm_regdef_t pcieadm_regdef_pcie_caphdr[] = {
	{ 0, 15, "capid", "Capability ID", PRDV_HEX },
	{ 16, 19, "version", "Capability Version", PRDV_HEX },
	{ 20, 32, "offset", "Next Capability Offset", PRDV_HEX },
	{ -1, -1, NULL }
};

/*
 * VPD Capability
 */
static const pcieadm_regdef_t pcieadm_regdef_vpd_addr[] = {
	{ 0, 14, "addr", "VPD Address", PRDV_HEX },
	{ 15, 15, "flag", "Flag", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_vpd[] = {
	{ 0x2, 2, "addr", "VPD Address Register",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_vpd_addr },
	{ 0x4, 4, "data", "VPD Data", pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

/*
 * SATA Capability per AHCI 1.3.1
 */
static const pcieadm_regdef_t pcieadm_regdef_sata_cr0[] = {
	{ 0, 3, "minrev", "Minor Revision", PRDV_HEX },
	{ 4, 7, "majrev", "Major Revision", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_sata_cr1[] = {
	{ 0, 3, "bar", "BAR Location", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 2 } } },
	{ 4, 23, "offset", "BAR Offset", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 2 } } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_sata[] = {
	{ 0x2, 2, "satacr0", "SATA Capability Register 0",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_sata_cr0 },
	{ 0x4, 4, "satacr1", "SATA Capability Register 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_sata_cr1 },
	{ -1, -1, NULL }
};

/*
 * Debug Capability per EHCI
 */
static const pcieadm_regdef_t pcieadm_regdef_debug[] = {
	{ 0, 12, "offset", "BAR Offset", PRDV_HEX },
	{ 13, 15, "bar", "BAR Location ", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { NULL, "BAR 0", "BAR 1", "BAR 2",
	    "BAR 3", "BAR 4", "BAR 5" } } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_debug[] = {
	{ 0x2, 2, "port", "Debug Port",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_debug },
	{ -1, -1, NULL }
};

/*
 * AER Capability
 */
static const pcieadm_regdef_t pcieadm_regdef_aer_ue[] = {
	{ 4, 4, "dlp", "Data Link Protocol Error", PRDV_HEX },
	{ 5, 5, "sde", "Surprise Down Error", PRDV_HEX },
	{ 12, 12, "ptlp", "Poisoned TLP Received", PRDV_HEX },
	{ 13, 13, "fcp", "Flow Control Protocol Error", PRDV_HEX },
	{ 14, 14, "cto", "Completion Timeout", PRDV_HEX },
	{ 15, 15, "cab", "Completion Abort", PRDV_HEX },
	{ 16, 16, "unco", "Unexpected Completion", PRDV_HEX },
	{ 17, 17, "rxov", "Receiver Overflow", PRDV_HEX },
	{ 18, 18, "maltlp", "Malformed TLP", PRDV_HEX },
	{ 19, 19, "ecrc", "ECRC Error", PRDV_HEX },
	{ 20, 20, "usuprx", "Unsupported Request Error", PRDV_HEX },
	{ 21, 21, "acs", "ACS Violation", PRDV_HEX },
	{ 22, 22, "ueint", "Uncorrectable Internal Error", PRDV_HEX },
	{ 23, 23, "mcbtlp", "MC Blocked TLP", PRDV_HEX },
	{ 24, 24, "atoomeb", "AtomicOp Egress Blocked", PRDV_HEX },
	{ 25, 25, "tlppb", "TLP Prefix Blocked Error", PRDV_HEX },
	{ 26, 26, "ptlpeb", "Poisoned TLP Egress Blocked", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_aer_ce[] = {
	{ 0, 0, "rxerr", "Receiver Error", PRDV_HEX },
	{ 6, 6, "badtlp", "Bad TLP", PRDV_HEX },
	{ 7, 7, "baddllp", "Bad DLLP", PRDV_HEX },
	{ 8, 8, "replayro", "REPLAY_NUM Rollover", PRDV_HEX },
	{ 12, 12, "rtto", "Replay timer Timeout", PRDV_HEX },
	{ 13, 13, "advnfe", "Advisory Non-Fatal Error", PRDV_HEX },
	{ 14, 14, "ceint", "Correctable Internal Error", PRDV_HEX },
	{ 15, 15, "headlov", "Header Log Overflow", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_aer_ctrl[] = {
	{ 0, 4, "feptr", "First Error Pointer", PRDV_HEX },
	{ 5, 5, "ecgencap", "ECRC Generation Capable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 6, 6, "ecgenen", "ECRC Generation Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 7, 7, "ecchkcap", "ECRC Check Capable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 8, "ecchken", "ECRC Check Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_aer_rootcom[] = {
	{ 0, 0, "corerr", "Correctable Error Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "nferr", "Non-Fatal Error Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "faterr", "Fatal Error Reporting", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_aer_rootsts[] = {
	{ 0, 0, "errcor", "ERR_COR Received", PRDV_HEX },
	{ 1, 1, "merrcor", "Multiple ERR_COR Received", PRDV_HEX },
	{ 2, 2, "errfnf", "ERR_FATAL/NONFATAL Received", PRDV_HEX },
	{ 3, 3, "merrfnf", "Multiple ERR_FATAL/NONFATAL Received", PRDV_HEX },
	{ 4, 4, "fuefat", "First Uncorrectable Fatal", PRDV_HEX },
	{ 5, 5, "nferrrx", "Non-Fatal Error Messages Received", PRDV_HEX },
	{ 6, 6, "faterrx", "Fatal Error Messages Received", PRDV_HEX },
	{ 7, 8, "errcorsc", "ERR_COR Subclass", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "ECS Legacy", "ECS SIG_SFW",
	    "ECS SIG_OS", "ECS Extended" } } },
	{ 27, 31, "inum", "Advanced Error Interrupt Message", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_aer_esi[] = {
	{ 0, 15, "errcorr", "ERR_COR Source", PRDV_HEX },
	{ 16, 31, "errfnf", "ERR_FATAL/NONFATAL Source", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_aer_secue[] = {
	{ 0, 0, "taosc", "Target-Abort on Split Completion", PRDV_HEX },
	{ 1, 1, "maosc", "Master-Abort on Split Completion", PRDV_HEX },
	{ 2, 2, "rxta", "Received Target-Abort", PRDV_HEX },
	{ 3, 3, "rxma", "Received Master-Abort", PRDV_HEX },
	{ 5, 5, "unsce", "Unexpected Split Completion Error", PRDV_HEX },
	{ 6, 6, "uescmd", "Uncorrectable Split Completion Message Data Error",
	    PRDV_HEX },
	{ 7, 7, "uede", "Uncorrectable Data Error", PRDV_HEX },
	{ 8, 8, "ueattre", "Uncorrectable Attribute Error", PRDV_HEX },
	{ 9, 9, "ueaddre", "Uncorrectable Address Error", PRDV_HEX },
	{ 10, 10, "dtdte", "Delayed Transaction Discard Timer Expired",
	    PRDV_HEX },
	{ 11, 11, "perr", "PERR# Assertion", PRDV_HEX },
	{ 12, 12, "serr", "SERR# Assertion", PRDV_HEX },
	{ 13, 13, "internal", "Internal Bridge Error", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_aer_secctl[] = {
	{ 0, 4, "feptr", "Secondary Uncorrectable First Error Pointer",
	    PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_aer_v1[] = {
	{ PCIE_AER_CAP, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ PCIE_AER_UCE_STS, 4, "uestatus", "Uncorrectable Error Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ue },
	{ PCIE_AER_UCE_MASK, 4, "uemask", "Uncorrectable Error Mask",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ue },
	{ PCIE_AER_UCE_SERV, 4, "ueserv", "Uncorrectable Error Severity",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ue },
	{ PCIE_AER_CE_STS, 4, "cestatus", "Correctable Error Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ce },
	{ PCIE_AER_CE_MASK, 4, "cemask", "Correctable Error Mask",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ce },
	{ PCIE_AER_CTL, 4, "ctrl", "Advanced Error Capabilities and Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ctrl },
	{ PCIE_AER_HDR_LOG + 4, 4, "hl0", "Header Log 0",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_HDR_LOG + 8, 4, "hl1", "Header Log 1",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_HDR_LOG + 12, 4, "hl2", "Header Log 2",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_HDR_LOG + 12, 4, "hl3", "Header Log 3",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_RE_CMD, 4, "rootcmd", "Root Error Command",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_rootcom },
	{ PCIE_AER_RE_STS, 4, "rootsts", "Root Error Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_rootsts },
	{ PCIE_AER_CE_SRC_ID, 4, "esi", "Error Source Identification",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_esi },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_aer_v2[] = {
	{ PCIE_AER_CAP, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ PCIE_AER_UCE_STS, 4, "uestatus", "Uncorrectable Error Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ue },
	{ PCIE_AER_UCE_MASK, 4, "uemask", "Uncorrectable Error Mask",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ue },
	{ PCIE_AER_UCE_SERV, 4, "ueserv", "Uncorrectable Error Severity",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ue },
	{ PCIE_AER_CE_STS, 4, "cestatus", "Correctable Error Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ce },
	{ PCIE_AER_CE_MASK, 4, "cemask", "Correctable Error Mask",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ce },
	{ PCIE_AER_CTL, 4, "ctrl", "Advanced Error Capabilities and Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ctrl },
	{ PCIE_AER_HDR_LOG + 4, 4, "hl0", "Header Log 0",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_HDR_LOG + 8, 4, "hl1", "Header Log 1",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_HDR_LOG + 12, 4, "hl2", "Header Log 2",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_HDR_LOG + 12, 4, "hl3", "Header Log 3",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_RE_CMD, 4, "rootcmd", "Root Error Command",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_rootcom },
	{ PCIE_AER_RE_STS, 4, "rootsts", "Root Error Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_rootsts },
	{ PCIE_AER_TLP_PRE_LOG, 4, "tlplog0", "TLP Prefix Log 0",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_TLP_PRE_LOG + 4, 4, "tlplog1", "TLP Prefix Log 1",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_TLP_PRE_LOG + 8, 4, "tlplog2", "TLP Prefix Log 2",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_TLP_PRE_LOG + 12, 4, "tlplog3", "TLP Prefix Log 3",
	    pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_aer_bridge[] = {
	{ PCIE_AER_CAP, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ PCIE_AER_UCE_STS, 4, "uestatus", "Uncorrectable Error Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ue },
	{ PCIE_AER_UCE_MASK, 4, "uemask", "Uncorrectable Error Mask",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ue },
	{ PCIE_AER_UCE_SERV, 4, "ueserv", "Uncorrectable Error Severity",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ue },
	{ PCIE_AER_CE_STS, 4, "cestatus", "Correctable Error Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ce },
	{ PCIE_AER_CE_MASK, 4, "cemask", "Correctable Error Mask",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ce },
	{ PCIE_AER_CTL, 4, "ctrl", "Advanced Error Capabilities and Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_ctrl },
	{ PCIE_AER_HDR_LOG + 4, 4, "hl0", "Header Log 0",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_HDR_LOG + 8, 4, "hl1", "Header Log 1",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_HDR_LOG + 12, 4, "hl2", "Header Log 2",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_HDR_LOG + 12, 4, "hl3", "Header Log 3",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_RE_CMD, 4, "rootcmd", "Root Error Command",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_rootcom },
	{ PCIE_AER_RE_STS, 4, "rootsts", "Root Error Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_rootsts },
	{ PCIE_AER_CE_SRC_ID, 4, "esi", "Error Source Identification",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_esi },
	{ PCIE_AER_SUCE_STS, 4, "secuests",
	    "Secondary Uncorrectable Error Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_secue },
	{ PCIE_AER_SUCE_MASK, 4, "secuests",
	    "Secondary Uncorrectable Error Mask",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_secue },
	{ PCIE_AER_SUCE_SERV, 4, "secuests",
	    "Secondary Uncorrectable Error Severity",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_secue },
	{ PCIE_AER_SCTL, 4, "secctrl",
	    "Secondary Error Capabilityes and Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_aer_secctl },
	{ PCIE_AER_SHDR_LOG, 4, "shl0", "Secondary Header Log 0",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_SHDR_LOG + 4, 4, "shl1", "Secondary Header Log 1",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_SHDR_LOG + 8, 4, "shl1", "Secondary Header Log 2",
	    pcieadm_cfgspace_print_hex },
	{ PCIE_AER_SHDR_LOG + 12, 4, "shl1", "Secondary Header Log 3",
	    pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

/*
 * Secondary PCI Express Extended Capability
 */
static const pcieadm_regdef_t pcieadm_regdef_pcie2_linkctl3[] = {
	{ 0, 0, "peq", "Perform Equalization", PRDV_HEX },
	{ 1, 1, "leqrie", "Link Equalization Request Interrupt Enable",
	    PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 9, 15, "elskpos", "Enable Lower SKP OS Generation Vector",
	    PRDV_BITFIELD,
	    .prd_val = { .prdv_strval = { "2.5 GT/s", "5.0 GT/s", "8.0 GT/s",
	    "16.0 GT/s", "32.0 GT/s" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcie2_linkeq[] = {
	{ 0, 3, "dstxpre", "Downstream Port 8.0 GT/s Transmitter Preset",
	    PRDV_HEX },
	{ 4, 6, "dstxhint", "Downstream Port 8.0 GT/s Receiver Hint",
	    PRDV_HEX },
	{ 8, 11, "ustxpre", "Upstream Port 8.0 GT/s Transmitter Preset",
	    PRDV_HEX },
	{ 12, 14, "ustxhint", "Upstream Port 8.0 GT/s Receiver Hint",
	    PRDV_HEX },
	{ -1, -1, NULL }
};

static void
pcieadm_cfgspace_print_laneq(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	if (walkp->pcw_nlanes == 0) {
		warnx("failed to capture lane count, but somehow have "
		    "secondary PCIe cap");
		return;
	}

	for (uint_t i = 0; i < walkp->pcw_nlanes; i++) {
		char eqshort[32], eqhuman[128];
		pcieadm_cfgspace_print_t p;

		(void) snprintf(eqshort, sizeof (eqshort), "lane%u", i);
		(void) snprintf(eqhuman, sizeof (eqhuman), "Lane %u EQ Control",
		    i);
		p.pcp_off = print->pcp_off + i * 2;
		p.pcp_len = 2;
		p.pcp_short = eqshort;
		p.pcp_human = eqhuman;
		p.pcp_print = pcieadm_cfgspace_print_regdef;
		p.pcp_arg = pcieadm_regdef_pcie2_linkeq;

		p.pcp_print(walkp, &p, p.pcp_arg);
	}
}

static const pcieadm_cfgspace_print_t pcieadm_cap_pcie2[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "linkctl3", "Link Control 3",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie2_linkctl3 },
	{ 0x8, 4, "laneerr", "Lane Error Status", pcieadm_cfgspace_print_hex },
	{ 0xc, 2, "eqctl", "Lane Equalization Control",
	    pcieadm_cfgspace_print_laneq },
	{ -1, -1, NULL }
};

/*
 * Access Control Services
 */
static const pcieadm_regdef_t pcieadm_regdef_acs_cap[] = {
	{ 0, 0, "srcvd", "ACS Source Validation", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 1, 1, "tranblk", "ACS Transaction Blocking", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 2, 2, "p2prr", "ACS P2P Request Redirect", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 3, 3, "p2pcr", "ACS P2P Completion Redirect", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 4, 4, "upfwd", "ACS Upstream Forwarding", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 5, 5, "p2pegctl", "ACS P2P Egress Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 6, 6, "dtp2p", "ACS Direct Translated P2P", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 7, 7, "enhcap", "ACS Enhanced Capability", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 15, "ecvsz", "Egress Control Vector Size", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_acs_ctl[] = {
	{ 0, 0, "srcvd", "ACS Source Validation", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "tranblk", "ACS Transaction Blocking", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "p2prr", "ACS P2P Request Redirect", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 3, 3, "p2pcr", "ACS P2P Completion Redirect", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 4, "upfwd", "ACS Upstream Forwarding", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 5, 5, "p2pegctl", "ACS P2P Egress Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 6, 6, "dtp2p", "ACS Direct Translated P2P", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 7, 7, "iorb", "ACS I/O Request Blocking", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 8, 9, "dspmta", "ACS DSP Memory Target Access Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "Direct Request access",
	    "Request blocking", "Request redirect" } } },
	{ 10, 11, "uspmta", "ACS USP Memory Target Access Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "Direct Request access",
	    "Request blocking", "Request redirect" } } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_acs[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 2, "cap", "ACS Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_acs_cap },
	{ 0x6, 2, "ctl", "ACS Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_acs_ctl },
	{ 0x8, 4, "ecv", "Egress Control Vector", pcieadm_cfgspace_print_ecv },
	{ -1, -1, NULL }
};

/*
 * L1 PM Substates
 */
static const pcieadm_regdef_t pcieadm_regdef_l1pm_cap[] = {
	{ 0, 0, "pcil1.2", "PCI-PM L1.2", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 1, 1, "pcil1.1", "PCI-PM L1.1", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 2, 2, "aspml1.2", "ASPM L1.2", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 3, 3, "aspml1.1", "ASPM L1.1", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 4, 4, "l1pmsub", "L1 PM Substates", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 5, 5, "linkact", "Link Activation", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 15, "pcmrt", "Port Common_Mode_Restore_Time", PRDV_HEX },
	{ 16, 17, "poscale", "Port T_POWER_ON Scale", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "2 us", "10 us", "100 us" } } },
	{ 19, 23, "portpo", "Port T_POWER_ON Value", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_l1pm_ctl1[] = {
	{ 0, 0, "pcil1.2", "PCI-PM L1.2", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "pcil1.1", "PCI-PM L1.1", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "aspml1.2", "ASPM L1.2", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 3, 3, "aspml1.1", "ASPM L1.1", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 4, "laie", "Link Activation Interrupt Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 5, 5, "lactl", "Link Activation Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 8, 15, "cmrt", "Common_Mode_Restore_Time", PRDV_HEX },
	{ 16, 25, "ltrl1.2", "LTR L1.2 Threshold Value", PRDV_HEX },
	{ 29, 31, "ltrl1.2s", "LTR L1.2 Threshold Scale", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "1 ns", "32 ns", "1024 ns",
	    "32,768 ns", "1,048,576 ns", "33,554,432 ns" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_l1pm_ctl2[] = {
	{ 0, 1, "poscale", "T_POWER_ON Scale", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "2 us", "10 us", "100 us" } } },
	{ 3, 7, "portpo", "T_POWER_ON Value", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_l1pm_sts[] = {
	{ 0, 0, "la", "Link Activation", PRDV_HEX },
	{ -1, -1, NULL }
};


static const pcieadm_cfgspace_print_t pcieadm_cap_l1pm_v1[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "caps", "L1 PM Substates Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_l1pm_cap },
	{ 0x8, 4, "ctl1", "L1 PM Substates Control 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_l1pm_ctl1 },
	{ 0xc, 4, "ctl2", "L1 PM Substates Control 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_l1pm_ctl2 },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_l1pm_v2[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "caps", "L1 PM Substates Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_l1pm_cap },
	{ 0x8, 4, "ctl1", "L1 PM Substates Control 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_l1pm_ctl1 },
	{ 0xc, 4, "ctl2", "L1 PM Substates Control 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_l1pm_ctl2 },
	{ 0x10, 4, "sts", "L1 PM Substates Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_l1pm_sts },
	{ -1, -1, NULL }
};

/*
 * Latency Tolerance Reporting (LTR)
 */
static const pcieadm_regdef_t pcieadm_regdef_ltr[] = {
	{ 0, 9, "latval", "Latency Value", PRDV_HEX },
	{ 10, 12, "latscale", "Latency Scale", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "1 ns", "32 ns", "1024 ns",
	    "32,768 ns", "1,048,576 ns", "33,554,432 ns" } } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_ltr[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 2, "snoop", "Max Snoop Latency",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ltr },
	{ 0x6, 2, "nosnoop", "Max No-Snoop Latency",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ltr },
	{ -1, -1, NULL }
};

/*
 * Alternative Routing ID
 */
static const pcieadm_regdef_t pcieadm_regdef_ari_cap[] = {
	{ 0, 0, "mfvcfg", "MFVC Function Groups", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 1, 1, "acsfg", "ACS Function Groups", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 15, "nfunc", "Next Function Number", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ari_ctl[] = {
	{ 0, 0, "mfvcfg", "MFVC Function Groups", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "acsfg", "ACS Function Groups", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 6, "fgrp", "Function Group", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_ari[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 2, "cap", "ARI Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ari_cap },
	{ 0x6, 2, "ctl", "ARI Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ari_ctl },
	{ -1, -1, NULL }
};

/*
 * PASID
 */
static const pcieadm_regdef_t pcieadm_regdef_pasid_cap[] = {
	{ 1, 1, "exec", "Execution Permission", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 2, 2, "priv", "Privileged Mode", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 12, "width", "Max PASID Width", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pasid_ctl[] = {
	{ 0, 0, "pasid", "PASID", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "exec", "Execution Permission", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "priv", "Privileged Mode", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};


static const pcieadm_cfgspace_print_t pcieadm_cap_pasid[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 2, "cap", "PASID Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pasid_cap },
	{ 0x6, 2, "ctl", "PASID Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pasid_ctl },
	{ -1, -1, NULL }
};

/*
 * "Advanced Features"
 */
static const pcieadm_regdef_t pcieadm_regdef_af_cap[] = {
	{ 0, 0, "tp", "Transactions Pending", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 1, 1, "flr", "Function Level Reset", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_af_ctl[] = {
	{ 0, 0, "flr", "Function Level Reset", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_af_sts[] = {
	{ 0, 0, "tp", "Transactions Pending", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "none pending", "pending" } } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_af[] = {
	{ 0x2, 2, "cap", "AF Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_af_cap },
	{ 0x4, 1, "ctl", "AF Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_af_ctl },
	{ 0x5, 1, "sts", "AF Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_af_sts },
	{ -1, -1, NULL }
};

/*
 * Multicast
 */
static const pcieadm_regdef_t pcieadm_regdef_mcast_cap[] = {
	{ 0, 5, "maxgrp", "Max Group", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 0, 1 } } },
	{ 8, 13, "winsize", "Window Size (raw)", PRDV_HEX },
	{ 15, 15, "ecrc", "ECRC Regeneration", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_mcast_ctl[] = {
	{ 0, 5, "numgrp", "Number of Groups", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 0, 1 } } },
	{ 15, 15, "enable", "Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_mcast_base[] = {
	{ 0, 5, "index", "Multicast Index Position", PRDV_HEX },
	{ 12, 63, "addr", "Base Address", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 12 } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_mcast_overlay[] = {
	{ 0, 5, "size", "Overlay Size (raw)", PRDV_HEX },
	{ 6, 63, "addr", "Overlay Base Address", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 6 } } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_mcast[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 2, "cap", "Multicast Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_mcast_cap },
	{ 0x6, 2, "ctl", "Multicast Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_mcast_ctl },
	{ 0x8, 8, "base", "Multicast Base Address",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_mcast_base },
	{ 0x10, 8, "rx", "Multicast Receive", pcieadm_cfgspace_print_hex },
	{ 0x18, 8, "block", "Multicast Block All", pcieadm_cfgspace_print_hex },
	{ 0x20, 8, "blockun", "Multicast Block Untranslated",
	    pcieadm_cfgspace_print_hex },
	{ 0x28, 8, "overlay", "Multicast Overlay BAR",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_mcast_overlay },
	{ -1, -1, NULL }
};

/*
 * Various vendor extensions
 */

/*
 * Red Hat Virtio capabilities.
 */

#define	VIRTIO_PCI_CAP_COMMON_CFG	0x1	/* Common config */
#define	VIRTIO_PCI_CAP_NOTIFY_CFG	0x2	/* Notifications */
#define	VIRTIO_PCI_CAP_ISR_CFG		0x3	/* ISR Status */
#define	VIRTIO_PCI_CAP_DEVICE_CFG	0x4	/* Device-specific config */
#define	VIRTIO_PCI_CAP_PCI_CFG		0x5	/* PCI config access */

static const pcieadm_strmap_t pcieadm_strmap_vs_virtio_type[] = {
	{ "Virtio Common Configuration", VIRTIO_PCI_CAP_COMMON_CFG },
	{ "Virtio Notifications", VIRTIO_PCI_CAP_NOTIFY_CFG },
	{ "Virtio ISR Status", VIRTIO_PCI_CAP_ISR_CFG },
	{ "Virtio Device-specific Configuration", VIRTIO_PCI_CAP_DEVICE_CFG },
	{ "Virtio PCI Configuration Access", VIRTIO_PCI_CAP_PCI_CFG },
	{ NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_vs_virtio_common[] = {
	{ 0x3, 1, "type", "Type",
	    pcieadm_cfgspace_print_strmap, pcieadm_strmap_vs_virtio_type },
	{ 0x4, 1, "bar", "BAR", pcieadm_cfgspace_print_hex },
	{ 0x8, 2, "offset", "Offset", pcieadm_cfgspace_print_hex },
	{ 0xc, 2, "len", "Length", pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_vs_virtio_notify[] = {
	{ 0x10, 2, "mult", "Multiplier", pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_vs_virtio_pci[] = {
	{ 0x10, 4, "data", "Data", pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static boolean_t
pcieadm_cfgspace_print_vs_virtio(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint8_t caplen, type;

	caplen = walkp->pcw_data->pcb_u8[walkp->pcw_capoff + 2];
	if (caplen < 0x10)
		return (B_FALSE);

	type = walkp->pcw_data->pcb_u8[walkp->pcw_capoff + 3];

	if (type > VIRTIO_PCI_CAP_PCI_CFG)
		return (B_FALSE);

	pcieadm_cfgspace_print_table(walkp, pcieadm_cap_vs_virtio_common);

	switch (type) {
	case VIRTIO_PCI_CAP_NOTIFY_CFG:
		pcieadm_cfgspace_print_table(walkp,
		    pcieadm_cap_vs_virtio_notify);
		break;
	case VIRTIO_PCI_CAP_PCI_CFG:
		pcieadm_cfgspace_print_table(walkp,
		    pcieadm_cap_vs_virtio_pci);
		break;
	default:
		break;
	}

	return (B_TRUE);
}

static const pcieadm_cfgspace_print_t pcieadm_cap_vs_unknown[] = {
	{ 0x2, 1, "length", "Length", pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static void
pcieadm_cfgspace_print_vs(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint16_t vid = walkp->pcw_data->pcb_u8[PCI_CONF_VENID] +
	    (walkp->pcw_data->pcb_u8[PCI_CONF_VENID + 1] << 8);
	uint16_t did = walkp->pcw_data->pcb_u8[PCI_CONF_DEVID] +
	    (walkp->pcw_data->pcb_u8[PCI_CONF_DEVID + 1] << 8);

	/*
	 * Red Hat virtio
	 */
	if (vid == 0x1af4 && did >= 0x1000 && did <= 0x107f) {
		if (pcieadm_cfgspace_print_vs_virtio(walkp, print, arg))
			return;
	}

	/*
	 * For any unknown vendor-specific capability there is not a lot we can
	 * print since all of the data after the first three bytes is
	 * vendor-specific.
	 */
	pcieadm_cfgspace_print_table(walkp, pcieadm_cap_vs_unknown);
}

static const pcieadm_cfgspace_print_t pcieadm_cap_vs[] = {
	{ 0x0, 1, "vs", "Vendor Specific", pcieadm_cfgspace_print_vs },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_vsec[] = {
	{ 0, 15, "id", "ID", PRDV_HEX },
	{ 16, 19, "rev", "Revision", PRDV_HEX },
	{ 20, 31, "len", "Length", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_vsec[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "header", "Vendor-Specific Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_vsec },
	{ -1, -1, NULL }
};

/*
 * Data Link Feature
 */
static const pcieadm_regdef_t pcieadm_regdef_dlf_cap[] = {
	{ 0, 0, "lsfc", "Local Scaled Flow Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 31, 31, "dlex", "Data Link Exchange", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_dlf_sts[] = {
	{ 0, 0, "rsfc", "Remote Scaled Flow Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 31, 31, "valid", "Remote Data Link Feature Valid", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "invalid", "valid" } } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_dlf[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "cap", "Data Link Feature Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_dlf_cap },
	{ 0x8, 4, "sts", "Data Link Feature Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_dlf_sts },
	{ -1, -1, NULL }
};

/*
 * 16.0 GT/s cap
 */
static const pcieadm_regdef_t pcieadm_regdef_16g_cap[] = {
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_16g_ctl[] = {
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_16g_sts[] = {
	{ 0, 0, "eqcomp", "Equalization 16.0 GT/s Complete", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "incomplete", "complete" } } },
	{ 1, 1, "eqp1", "Equalization 16.0 GT/s Phase 1", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "incomplete", "complete" } } },
	{ 2, 2, "eqp2", "Equalization 16.0 GT/s Phase 2", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "incomplete", "complete" } } },
	{ 3, 3, "eqp3", "Equalization 16.0 GT/s Phase 3", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "incomplete", "complete" } } },
	{ 4, 4, "req", "Link Equalization Request 16.0 GT/s", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_16g_eq[] = {
	{ 0, 3, "dstxpre", "Downstream Port 16.0 GT/s Transmitter Preset",
	    PRDV_HEX },
	{ 4, 7, "ustxpre", "Upstream Port 16.0 GT/s Transmitter Preset",
	    PRDV_HEX },
	{ -1, -1, NULL }
};

static void
pcieadm_cfgspace_print_16geq(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	if (walkp->pcw_nlanes == 0) {
		warnx("failed to capture lane count, but somehow have "
		    "Physical Layer 16.0 GT/s cap");
		return;
	}

	for (uint_t i = 0; i < walkp->pcw_nlanes; i++) {
		char eqshort[32], eqhuman[128];
		pcieadm_cfgspace_print_t p;

		(void) snprintf(eqshort, sizeof (eqshort), "lane%u", i);
		(void) snprintf(eqhuman, sizeof (eqhuman), "Lane %u EQ Control",
		    i);
		p.pcp_off = print->pcp_off + i * 1;
		p.pcp_len = 1;
		p.pcp_short = eqshort;
		p.pcp_human = eqhuman;
		p.pcp_print = pcieadm_cfgspace_print_regdef;
		p.pcp_arg = pcieadm_regdef_16g_eq;

		p.pcp_print(walkp, &p, p.pcp_arg);
	}
}

static const pcieadm_cfgspace_print_t pcieadm_cap_16g[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "cap", "16.0 GT/s Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_16g_cap },
	{ 0x8, 4, "ctl", "16.0 GT/s Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_16g_ctl },
	{ 0xc, 4, "sts", "16.0 GT/s Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_16g_sts },
	{ 0x10, 4, "ldpmis", "16.0 GT/s Local Data Parity Mismatch",
	    pcieadm_cfgspace_print_hex },
	{ 0x14, 4, "frpmis", "16.0 GT/s First Retimer Data Parity Mismatch",
	    pcieadm_cfgspace_print_hex },
	{ 0x18, 4, "srpmis", "16.0 GT/s Second Retimer Data Parity Mismatch",
	    pcieadm_cfgspace_print_hex },
	{ 0x1c, 4, "rsvd", "16.0 GT/s Second Retimer Data Parity Mismatch",
	    pcieadm_cfgspace_print_hex },
	{ 0x20, 1, "eqctl", "16.0 GT/s EQ Control",
	    pcieadm_cfgspace_print_16geq },
	{ -1, -1, NULL }
};

/*
 * Receiver Margining
 */
static const pcieadm_regdef_t pcieadm_regdef_margin_cap[] = {
	{ 0, 0, "sw", "Margining uses Driver Software", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_margin_sts[] = {
	{ 0, 0, "ready", "Margining Ready", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 1, 1, "sw", "Margining Software Ready", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_margin_lane[] = {
	{ 0, 2, "rxno", "Receiver Number", PRDV_HEX },
	{ 3, 5, "type", "Margin Type", PRDV_HEX },
	{ 6, 6, "model", "Usage Model", PRDV_HEX },
	{ 8, 15, "payload", "Margin Payload", PRDV_HEX },
	{ -1, -1, NULL }
};

static void
pcieadm_cfgspace_print_margin(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	if (walkp->pcw_nlanes == 0) {
		warnx("failed to capture lane count, but somehow have "
		    "lane margining capability");
		return;
	}

	for (uint_t i = 0; i < walkp->pcw_nlanes; i++) {
		char mshort[32], mhuman[128];
		pcieadm_cfgspace_print_t p;

		(void) snprintf(mshort, sizeof (mshort), "lane%uctl", i);
		(void) snprintf(mhuman, sizeof (mhuman), "Lane %u Margining "
		    "Control", i);
		p.pcp_off = print->pcp_off + i * 4;
		p.pcp_len = 2;
		p.pcp_short = mshort;
		p.pcp_human = mhuman;
		p.pcp_print = pcieadm_cfgspace_print_regdef;
		p.pcp_arg = pcieadm_regdef_margin_lane;

		p.pcp_print(walkp, &p, p.pcp_arg);

		(void) snprintf(mshort, sizeof (mshort), "lane%usts", i);
		(void) snprintf(mhuman, sizeof (mhuman), "Lane %u Margining "
		    "Status", i);
		p.pcp_off = print->pcp_off + 2 + i * 4;
		p.pcp_len = 2;
		p.pcp_short = mshort;
		p.pcp_human = mhuman;
		p.pcp_print = pcieadm_cfgspace_print_regdef;
		p.pcp_arg = pcieadm_regdef_margin_lane;

		p.pcp_print(walkp, &p, p.pcp_arg);
	}
}

static const pcieadm_cfgspace_print_t pcieadm_cap_margin[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 2, "cap", "Margining Port Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_margin_cap },
	{ 0x6, 2, "sts", "Margining Port Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_margin_sts },
	{ 0x8, 4, "lane", "Margining Lane", pcieadm_cfgspace_print_margin },
	{ -1, -1, NULL }
};

/*
 * Serial Number Capability
 */
static void
pcieadm_cfgspace_print_sn(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	char sn[64];
	uint16_t off = walkp->pcw_capoff + print->pcp_off;

	(void) snprintf(sn, sizeof (sn),
	    "%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x",
	    walkp->pcw_data->pcb_u8[off + 7], walkp->pcw_data->pcb_u8[off + 6],
	    walkp->pcw_data->pcb_u8[off + 5], walkp->pcw_data->pcb_u8[off + 4],
	    walkp->pcw_data->pcb_u8[off + 3], walkp->pcw_data->pcb_u8[off + 2],
	    walkp->pcw_data->pcb_u8[off + 1], walkp->pcw_data->pcb_u8[off]);

	pcieadm_cfgspace_puts(walkp, print, sn);
}

static const pcieadm_cfgspace_print_t pcieadm_cap_sn[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 8, "sn", "Serial Number", pcieadm_cfgspace_print_sn },
	{ -1, -1, NULL }
};

/*
 * TLP Processing Hints (TPH)
 */
static const pcieadm_regdef_t pcieadm_regdef_tph_cap[] = {
	{ 0, 0, "nost", "No ST Mode", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 1, 1, "ivec", "Interrupt Vector Mode", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 2, 2, "dev", "Device Specific Mode", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 8, "exttph", "Extended TPH Requester", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 9, 10, "loc", "ST Table Location", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "Not Present",
	    "In Capability Structure", "MSI-X" } } },
	{ 16, 26, "size", "ST Table Size", PRDV_HEX, { .prdv_hex = { 0, 1 } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_tph_ctl[] = {
	{ 0, 2, "mode", "ST Mode Select", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "No ST", "Interrupt Vector",
	    "Device Specific" } } },
	{ 8, 9, "en", "TPH Requester", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "Not Permitted", "TPH", NULL,
	    "TPH and Extended TPH" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_tph_st[] = {
	{ 0, 7, "low", "ST Lower", PRDV_HEX },
	{ 8, 15, "up", "ST Upper", PRDV_HEX },
	{ -1, -1, NULL }
};

/*
 * The TPH ST table is only conditionally present in the capability. So we need
 * to read the TPH capability register and then check if the table location and
 * size are set here.
 */
static void
pcieadm_cfgspace_print_tphst(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint_t nents;
	uint32_t tphcap = pcieadm_cfgspace_getcap32(walkp, 4);

	if (bitx32(tphcap, 10, 9) != 1) {
		return;
	}

	nents = bitx32(tphcap, 26, 16) + 1;
	for (uint_t i = 0; i < nents; i++) {
		char tshort[32], thuman[128];
		pcieadm_cfgspace_print_t p;

		(void) snprintf(tshort, sizeof (tshort), "st%u", i);
		(void) snprintf(thuman, sizeof (thuman), "ST Table %u",
		    i);
		p.pcp_off = print->pcp_off + i * 2;
		p.pcp_len = 2;
		p.pcp_short = tshort;
		p.pcp_human = thuman;
		p.pcp_print = pcieadm_cfgspace_print_regdef;
		p.pcp_arg = pcieadm_regdef_tph_st;

		p.pcp_print(walkp, &p, p.pcp_arg);
	}
}

static const pcieadm_cfgspace_print_t pcieadm_cap_tph[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "cap", "TPH Requester Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_tph_cap },
	{ 0x8, 4, "ctl", "TPH Requester Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_tph_ctl },
	{ 0xc, 2, "table", "ST Table", pcieadm_cfgspace_print_tphst },
	{ -1, -1, NULL }
};

/*
 * SR-IOV
 */
static const pcieadm_regdef_t pcieadm_regdef_sriov_cap[] = {
	{ 0, 0, "migration", "Migration", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 1, 1, "ari", "ARI Capable Hierarchy Preserved", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unpreserved", "preserved" } } },
	{ 2, 2, "vf10b", "VF 10-bit Tag Requester", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unpreserved", "preserved" } } },
	{ 21, 31, "inum", "VF Migration Interrupt Message Number", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_sriov_ctl[] = {
	{ 0, 0, "vf", "VF", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "vfm", "VF Migration", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "vfmi", "VF Migration Interrupt", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 3, 3, "ari", "ARI Capable Hierarchy", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 4, "vf10b", "VF 10-bit Tag Requester", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_sriov_sts[] = {
	{ 0, 0, "vfm", "VF Migration", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "none", "requested" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_sriov_pgsup[] = {
	{ 0, 31, "pgsz", "Supported Page Sizes", PRDV_BITFIELD,
	    .prd_val = { .prdv_strval = { "4 KB", "8 KB", "16 KB", "32 KB",
	    "64 KB", "128 KB", "256 KB", "512 KB", "1 MB", "2 MB", "4 MB",
	    "8 MB", "16 MB", "32 MB", "64 MB", "128 MB", "256 MB", "512 MB",
	    "1 GB", "2 GB", "4 GB", "8 GB", "16 GB", "32 GB", "64 GB",
	    "128 GB", "256 GB", "512 GB", "1 TB", "2 TB", "4 TB", "8 TB" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_sriov_pgen[] = {
	{ 0, 31, "pgsz", "System Page Sizes", PRDV_BITFIELD,
	    .prd_val = { .prdv_strval = { "4 KB", "8 KB", "16 KB", "32 KB",
	    "64 KB", "128 KB", "256 KB", "512 KB", "1 MB", "2 MB", "4 MB",
	    "8 MB", "16 MB", "32 MB", "64 MB", "128 MB", "256 MB", "512 MB",
	    "1 GB", "2 GB", "4 GB", "8 GB", "16 GB", "32 GB", "64 GB",
	    "128 GB", "256 GB", "512 GB", "1 TB", "2 TB", "4 TB", "8 TB" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_sriov_mig[] = {
	{ 0, 2, "bir", "VF Migration State BIR", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "BAR 0", "BAR 1", "BAR 2", "BAR 3",
	    "BAR 4", "BAR 5" } } },
	{ 3, 31, "offset", "VF Migration State Offset", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 3 } } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_sriov[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "cap", "SR-IOV Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_sriov_cap },
	{ 0x8, 2, "ctl", "SR-IOV Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_sriov_ctl },
	{ 0xa, 2, "sts", "SR-IOV Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_sriov_sts },
	{ 0xc, 2, "initvfs", "Initial VFs", pcieadm_cfgspace_print_hex },
	{ 0xe, 2, "totvfs", "Total VFs", pcieadm_cfgspace_print_hex },
	{ 0x10, 2, "numvfs", "Number VFs", pcieadm_cfgspace_print_hex },
	{ 0x12, 1, "dep", "Function Dependency Link",
	    pcieadm_cfgspace_print_hex },
	{ 0x14, 2, "offset", "First VF Offset", pcieadm_cfgspace_print_hex },
	{ 0x16, 2, "stride", "VF Stride", pcieadm_cfgspace_print_hex },
	{ 0x1a, 2, "devid", "VF Device ID", pcieadm_cfgspace_print_hex },
	{ 0x1c, 4, "pgsz", "Supported Page Sizes",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_sriov_pgsup },
	{ 0x20, 4, "pgsz", "System Page Sizes",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_sriov_pgen },
	{ 0x24, 24, "vfbar", "Virtual Base Address Register",
	    pcieadm_cfgspace_print_bars },
	{ 0x3c, 4, "migration", "VF Migration State Array",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_sriov_mig },
	{ -1, -1, NULL }
};

/*
 * PCI-X
 */
static const pcieadm_regdef_t pcieadm_regdef_pcix_dev_ctl[] = {
	{ 0, 0, "dper", "Data Parity Error Recovery", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "ro", "Relaxed Ordering", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 3, "maxread", "Maximum Memory Read Byte Count", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "512 bytes", "1024 bytes",
	    "2048 bytes", "4096 bytes" } } },
	{ 4, 6, "maxsplit", "Maximum Outstanding Split Transactions",
	    PRDV_STRVAL, .prd_val = { .prdv_strval = { "1", "2", "3", "4", "8",
	    "12", "16", "32" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcix_dev_sts[] = {
	{ 0, 2, "func", "Function Number", PRDV_HEX },
	{ 3, 7, "dev", "Device Number", PRDV_HEX },
	{ 8, 15, "bus", "Bus Number", PRDV_HEX },
	{ 16, 16, "64bit", "64-bit Device", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported (32-bit)",
	    "supported" } } },
	{ 17, 17, "133mhz", "133 MHz Capable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported (66 MHz)",
	    "supported" } } },
	{ 18, 18, "spcodis", "Split Completion Discarded", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 19, 19, "unspco", "Unexpected Split Completion", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 20, 20, "complex", "Device Complexity", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "simple", "bridge" } } },
	{ 21, 22, "maxread", "Designed Maximum Memory Read Byte Count",
	    PRDV_STRVAL, .prd_val = { .prdv_strval = { "512 bytes",
	    "1024 bytes", "2048 bytes", "4096 bytes" } } },
	{ 23, 25, "maxsplit", "Designed Maximum Outstanding Split Transactions",
	    PRDV_STRVAL, .prd_val = { .prdv_strval = { "1", "2", "3", "4", "8",
	    "12", "16", "32" } } },
	{ 26, 28, "maxcread", "Designed Maximum Cumulative Read Size",
	    PRDV_STRVAL, .prd_val = { .prdv_strval = { "8/1KB", "16/2KB",
	    "32/4KB", "64/8KB", "128/16KB", "256/32KB", "512/64KB",
	    "1024/128KB" } } },
	{ 29, 29, "rxspcoer", "Received Split Completion Error Message",
	    PRDV_STRVAL, .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcix_sec_sts[] = {
	{ 0, 0, "64bit", "64-bit Device", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported (32-bit)",
	    "supported" } } },
	{ 1, 1, "133mhz", "133 MHz Capable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported (66 MHz)",
	    "supported" } } },
	{ 2, 2, "spcodis", "Split Completion Discarded", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 3, 3, "unspco", "Unexpected Split Completion", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 4, 4, "spcoor", "Split Completion Overrun", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 5, 5, "sprde", "Split Request Delayed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 6, 8, "freq", "Secondary Clock Frequency", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "conventional", "66 MHz", "100 Mhz",
	    "133 MHz" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcix_bridge_sts[] = {
	{ 0, 2, "func", "Function Number", PRDV_HEX },
	{ 3, 7, "dev", "Device Number", PRDV_HEX },
	{ 8, 15, "bus", "Bus Number", PRDV_HEX },
	{ 16, 16, "64bit", "64-bit Device", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported (32-bit)",
	    "supported" } } },
	{ 17, 17, "133mhz", "133 MHz Capable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported (66 MHz)",
	    "supported" } } },
	{ 18, 18, "spcodis", "Split Completion Discarded", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 19, 19, "unspco", "Unexpected Split Completion", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 20, 20, "spcoor", "Split Completion Overrun", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 21, 21, "sprde", "Split Request Delayed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pcix_bridge_split[] = {
	{ 0, 15, "cap", "Split Transaction Capacity", PRDV_HEX },
	{ 16, 31, "limit", "Split Transaction Commitment Limit", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_pcix_dev[] = {
	{ 0x2, 2, "ctl", "PCI-X Command",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcix_dev_ctl },
	{ 0x4, 4, "sts", "PCI-X Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcix_dev_sts },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_pcix_bridge[] = {
	{ 0x2, 2, "secsts", "PCI-X Secondary Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcix_sec_sts },
	{ 0x4, 4, "sts", "PCI-X Bridge Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcix_bridge_sts },
	{ 0x8, 4, "ussplit", "Upstream Split Transaction",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcix_bridge_split },
	{ 0x8, 4, "dssplit", "Downstream Split Transaction",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcix_bridge_split },
	{ -1, -1, NULL }
};

/*
 * Dynamic Power Allocation
 */
static const pcieadm_regdef_t pcieadm_regdef_dpa_cap[] = {
	{ 0, 4, "substates", "Substate Max", PRDV_HEX,
	    { .prdv_hex = { 0, 1 } } },
	{ 8, 9, "tlu", "Transition Latency Unit", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "1 ms", "10 ms", "100 ms" } } },
	{ 12, 13, "pas", "Power Allocation Scale", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "10.0x", "1.0x", "0.1x",
	    "0.01x" } } },
	{ 16, 23, "tlv0", "Transition Latency Value 0", PRDV_HEX },
	{ 24, 31, "tlv0", "Transition Latency Value 1", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_dpa_sts[] = {
	{ 0, 4, "substate", "Substate Status", PRDV_HEX },
	{ 8, 8, "ctlen", "Substate Control Enabled", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_dpa_ctl[] = {
	{ 0, 4, "substate", "Substate Control", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_dpa[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "cap", "DPA Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_dpa_cap },
	{ 0x8, 4, "lat", "DPA Latency Indicator", pcieadm_cfgspace_print_hex },
	{ 0xc, 2, "sts", "DPA Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_dpa_sts },
	{ 0xe, 2, "sts", "DPA Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_dpa_ctl },
	{ 0x10, 1, "paa", "DPA Power Allocation Array",
	    pcieadm_cfgspace_print_dpa_paa },
	{ -1, -1, NULL }
};

/*
 * Power Budgeting
 */
static const pcieadm_regdef_t pcieadm_regdef_powbudg_data[] = {
	{ 0, 7, "base", "Base Power", PRDV_HEX },
	{ 8, 9, "scale", "Data Scale", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "1.0x", "0.1x", "0.01x",
	    "0.001x" } } },
	{ 10, 12, "pmsub", "PM Substate", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "Default", "Device Specific",
	    "Device Specific", "Device Specific", "Device Specific",
	    "Device Specific", "Device Specific", "Device Specific" } } },
	{ 13, 14, "pmstate", "PM State", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "D0", "D1", "D2", "D3" } } },
	{ 15, 17, "type", "Type", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "PME Aux", "Axiliary", "Idle",
	    "Sustained", "Sustained - EPRS", "Maximum - EPRS", NULL,
	    "Maximum" } } },
	{ 18, 20, "rail", "Power Rail", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "Power (12V)", "Power (3.3V)",
	    "Power (1.5V or 1.8V)", NULL, NULL, NULL, NULL, "Thermal" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_powbudg_cap[] = {
	{ 0, 0, "sa", "System Allocated", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};


static const pcieadm_cfgspace_print_t pcieadm_cap_powbudg[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 1, "sel", "Data Select", pcieadm_cfgspace_print_hex },
	{ 0x8, 4, "data", "Data Regiser", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_powbudg_data },
	{ 0xc, 0x1, "cap", "Power Budget Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_powbudg_cap },
	{ -1, -1, NULL }
};

/*
 * Precision Time Management
 */
static const pcieadm_regdef_t pcieadm_regdef_ptm_cap[] = {
	{ 0, 0, "req", "PTM Requester", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 1, 1, "resp", "PTM Responder", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 2, 2, "root", "PTM Root", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 3, 3, "eptm", "ePTM", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 15, "gran", "Local Clock Granularity", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ptm_ctl[] = {
	{ 0, 0, "en", "PTM Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "root", "Root Select", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 8, 15, "gran", "Effective Granularity", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_info_ptm[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "cap", "PTM Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ptm_cap },
	{ 0x8, 4, "cap", "PTM Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ptm_ctl },
	{ -1, -1, NULL }
};

/*
 * Address Translation Services (ATS)
 */
static const pcieadm_regdef_t pcieadm_regdef_ats_cap[] = {
	{ 0, 4, "invqd", "Invalidate Queue Depth", PRDV_HEX },
	{ 5, 5, "pgalign", "Page Aligned Request", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not required", "required" } } },
	{ 6, 6, "glbinv", "Global Invalidate", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 7, 7, "relo", "Relaxed Ordering", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ats_ctl[] = {
	{ 0, 4, "stu", "Smallest Translation Unit", PRDV_HEX },
	{ 15, 15, "en", "Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_ats[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 2, "cap", "ATS Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ats_cap },
	{ 0x6, 2, "cap", "ATS Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ats_ctl },
	{ -1, -1, NULL }
};

/*
 * Page Request
 */
static const pcieadm_regdef_t pcieadm_regdef_pgreq_ctl[] = {
	{ 0, 0, "en", "Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "reset", "Reset", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_pgreq_sts[] = {
	{ 0, 0, "rf", "Response Failure", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 1, 1, "uprgi", "Unexpected Page Request Group Index", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 8, 8, "stopped", "Stopped", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 15, 15, "prgrpreq", "PRG Response PASID", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not required", "required" } } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_pgreq[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 2, "ctl", "Page Request Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pgreq_ctl },
	{ 0x6, 2, "ctl", "Page Request Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pgreq_sts },
	{ 0x8, 4, "cap", "Outstanding Page Request Capacity",
	    pcieadm_cfgspace_print_hex },
	{ 0xc, 4, "alloc", "Outstanding Page Request Allocation",
	    pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

/*
 * NULL Capability
 */
static const pcieadm_cfgspace_print_t pcieadm_cap_null[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ -1, -1, NULL }
};

/*
 * Downstream Port Containment
 */
static const pcieadm_regdef_t pcieadm_regdef_dpc_cap[] = {
	{ 0, 4, "inum", "DPC Interrupt Message Number", PRDV_HEX },
	{ 5, 5, "rpext", "Root Port Extensions", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 6, 6, "ptlpeb", "Poisoned TLP Egress Blocking", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 7, 7, "swtrig", "Software Triggering", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 11, "logsz", "RP PIO Log Size", PRDV_HEX },
	{ 12, 12, "errcorr", "DL_Active ERR_COR Signaling", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_dpc_ctl[] = {
	{ 0, 1, "trigger", "DPC Trigger", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled, fatal",
	    "enabled, non-fatal" } } },
	{ 2, 2, "comp", "Completion Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "Completer Abort",
	    "Unsupported Request" } } },
	{ 3, 3, "intr", "Interrupt",
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 4, "errcor", "ERR_COR",
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 5, 5, "ptlpeb", "Poisoned TLP Egress Blocking", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 6, 6, "swtrig", "Software Trigger", PRDV_HEX },
	{ 7, 7, "corerr", "DL_Active ERR_COR",
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 8, 8, "sigsfw", "SIG_SFW",
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_dpc_sts[] = {
	{ 0, 0, "trigger", "Trigger Status", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not triggered", "triggered" } } },
	{ 1, 2, "reason", "Trigger Reason", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unmasked uncorrectable",
	    "ERR_NONFATAL received", "ERR_FATAL received",
	    "see extension" } } },
	{ 3, 3, "istatus", "Interrupt Status", PRDV_HEX },
	{ 4, 4, "rpbusy", "RP Busy", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 5, 6, "extreason", "Trigger Reason Extension", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "RP PIO", "Software Trigger" } } },
	{ 8, 12, "feptr", "RP PIO, First Error Pointer", PRDV_HEX },
	{ 13, 13, "sigsfw", "SIG_SFW Status", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_dpc_rppio_bits[] = {
	{ 0, 0, "cfgur", "Configuration Request UR Completion", PRDV_HEX },
	{ 1, 1, "cfgca", "Configuration Request CA Completion", PRDV_HEX },
	{ 2, 2, "cfgcto", "Configuration Request Completion Timeout",
	    PRDV_HEX },
	{ 8, 8, "iour", "I/O UR Completion", PRDV_HEX },
	{ 9, 9, "ioca", "I/O CA Completion", PRDV_HEX },
	{ 10, 10, "iocto", "I/O Completion Timeout", PRDV_HEX },
	{ 8, 8, "memur", "Memory UR Completion", PRDV_HEX },
	{ 9, 9, "memca", "Memory CA Completion", PRDV_HEX },
	{ 10, 10, "memcto", "Memory Completion Timeout", PRDV_HEX },
	{ -1, -1, NULL }
};

static void
pcieadm_cfgspace_print_dpc_rppio(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint32_t cap = pcieadm_cfgspace_getcap32(walkp, 4);

	if (bitx32(cap, 5, 5) == 0) {
		return;
	}

	pcieadm_cfgspace_print_regdef(walkp, print, arg);
}

static void
pcieadm_cfgspace_print_dpc_piohead(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint32_t cap = pcieadm_cfgspace_getcap32(walkp, 4);
	uint32_t nwords = bitx32(cap, 11, 8);

	if (bitx32(cap, 5, 5) == 0 || nwords < 4) {
		return;
	}

	pcieadm_cfgspace_print_hex(walkp, print, NULL);
}

static void
pcieadm_cfgspace_print_dpc_impspec(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint32_t cap = pcieadm_cfgspace_getcap32(walkp, 4);
	uint32_t nwords = bitx32(cap, 11, 8);

	if (bitx32(cap, 5, 5) == 0 || nwords < 5) {
		return;
	}

	pcieadm_cfgspace_print_hex(walkp, print, NULL);
}

static void
pcieadm_cfgspace_print_dpc_tlplog(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint32_t cap = pcieadm_cfgspace_getcap32(walkp, 4);
	int32_t nwords = (int32_t)bitx32(cap, 11, 8);

	if (nwords == 0 || bitx32(cap, 5, 5) == 0) {
		return;
	}

	if (nwords <= 9) {
		nwords -= 5;
	} else {
		nwords -= 4;
	}

	for (int32_t i = 0; i < nwords; i++) {
		char tlpshort[32], tlphuman[128];
		pcieadm_cfgspace_print_t p;

		(void) snprintf(tlpshort, sizeof (tlpshort), "%s%u",
		    print->pcp_short, i);
		(void) snprintf(tlphuman, sizeof (tlphuman), "%s %u",
		    print->pcp_human, i);
		p.pcp_off = print->pcp_off + i * 4;
		p.pcp_len = 4;
		p.pcp_short = tlpshort;
		p.pcp_human = tlphuman;
		p.pcp_print = pcieadm_cfgspace_print_hex;
		p.pcp_arg = NULL;

		p.pcp_print(walkp, &p, p.pcp_arg);
	}
}

static const pcieadm_cfgspace_print_t pcieadm_cap_dpc[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 2, "cap", "DPC Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_dpc_cap },
	{ 0x6, 2, "ctl", "DPC Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_dpc_ctl },
	{ 0x8, 2, "sts", "DPC Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_dpc_sts },
	{ 0xa, 2, "srcid", "DPC Error Source ID",
	    pcieadm_cfgspace_print_hex },
	{ 0x10, 4, "rppiosts", "RP PIO Status",
	    pcieadm_cfgspace_print_dpc_rppio, pcieadm_regdef_dpc_rppio_bits },
	{ 0x14, 4, "rppiomask", "RP PIO Mask ID",
	    pcieadm_cfgspace_print_dpc_rppio, pcieadm_regdef_dpc_rppio_bits },
	{ 0x14, 4, "rppiosev", "RP PIO Severity",
	    pcieadm_cfgspace_print_dpc_rppio, pcieadm_regdef_dpc_rppio_bits },
	{ 0x18, 4, "rppiose", "RP PIO SysError",
	    pcieadm_cfgspace_print_dpc_rppio, pcieadm_regdef_dpc_rppio_bits },
	{ 0x1c, 4, "rppioex", "RP PIO Exception",
	    pcieadm_cfgspace_print_dpc_rppio, pcieadm_regdef_dpc_rppio_bits },
	{ 0x20, 4, "rppiohl0", "RP PIO Header Log 0",
	    pcieadm_cfgspace_print_dpc_piohead },
	{ 0x24, 4, "rppiohl1", "RP PIO Header Log 1",
	    pcieadm_cfgspace_print_dpc_piohead },
	{ 0x28, 4, "rppiohl2", "RP PIO Header Log 2",
	    pcieadm_cfgspace_print_dpc_piohead },
	{ 0x2c, 4, "rppiohl3", "RP PIO Header Log 3",
	    pcieadm_cfgspace_print_dpc_piohead },
	{ 0x30, 4, "impspec", "RP PIO ImpSpec Log",
	    pcieadm_cfgspace_print_dpc_impspec },
	{ 0x34, 16, "tlplog", "RP PIO TLP Prefix Log",
	    pcieadm_cfgspace_print_dpc_tlplog },
	{ -1, -1, NULL }
};

/*
 * Virtual Channel Capability
 */
static const pcieadm_regdef_t pcieadm_regdef_vc_cap1[] = {
	{ 0, 2, "count", "Extended VC Count", PRDV_HEX },
	{ 4, 6, "lpcount", "Low Priority Extended VC Count", PRDV_HEX },
	{ 8, 9, "refclk", "Reference Clock", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "100ns" } } },
	{ 10, 11, "patsz", "Port Arbitration Table Size", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "1 bit", "2 bits", "4 bits",
	    "8 bits" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_vc_cap2[] = {
	{ 0, 7, "arbcap", "VC Arbitration Capability", PRDV_BITFIELD,
	    .prd_val = { .prdv_strval = { "hardware fixed",
	    "32 phase weighted round robin", "64 phase weighted round robin",
	    "128 phase weighted round robin" } } },
	{ 24, 31, "offset", "VC Arbitration Table Offset", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_vc_ctl[] = {
	{ 0, 0, "loadtbl", "Load VC Arbitration Table", PRDV_HEX },
	{ 1, 3, "arbtype", "VC Arbitration Select", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "hardware fixed",
	    "32 phase weighted round robin", "64 phase weighted round robin",
	    "128 phase weighted round robin" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_vc_sts[] = {
	{ 0, 0, "table", "VC Arbitration Table Status", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_vc_rsrccap[] = {
	{ 0, 7, "arbcap", "Port Arbitration Capability", PRDV_BITFIELD,
	    .prd_val = { .prdv_strval = { "hardware fixed",
	    "32 phase weighted round robin", "64 phase weighted round robin",
	    "128 phase weighted round robin",
	    "128 phase time-based weighted round robin",
	    "256 phase weighted round robin" } } },
	{ 14, 14, "aps", "Advanced Packet Switching", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 15, 15, "rstx", "Reject Snoop Transactions", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 16, 22, "nslots", "Maximum Time Slots", PRDV_HEX,
	    { .prdv_hex = { 0, 1 } } },
	{ 24, 31, "offset", "VC Arbitration Table Offset", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_vc_rsrcctl[] = {
	{ 0, 7, "tcmap", "TC/VC Map", PRDV_HEX },
	{ 16, 16, "loadtbl", "Load VC Arbitration Table", PRDV_HEX },
	{ 17, 19, "arbtype", "Port Arbitration Select", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "hardware fixed",
	    "32 phase weighted round robin", "64 phase weighted round robin",
	    "128 phase weighted round robin",
	    "128 phase time-based weighted round robin",
	    "256 phase weighted round robin" } } },
	{ 24, 26, "vcid", "VC ID", PRDV_HEX },
	{ 31, 31, "en", "VC Enable",
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_vc_rsrcsts[] = {
	{ 0, 0, "table", "Port Arbitration Table Status", PRDV_HEX },
	{ -1, -1, NULL }
};

static void
pcieadm_cfgspace_print_vc_rsrc(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint32_t cap = pcieadm_cfgspace_getcap32(walkp, 4);
	uint32_t nents = bitx32(cap, 2, 0) + 1;

	for (uint32_t i = 0; i < nents; i++) {
		char vcshort[32], vchuman[128];
		pcieadm_cfgspace_print_t p;

		(void) snprintf(vcshort, sizeof (vcshort), "rsrccap%u", i);
		(void) snprintf(vchuman, sizeof (vchuman), "VC Resource %u "
		    "Capability", i);
		p.pcp_off = print->pcp_off + i * 0x10;
		p.pcp_len = 4;
		p.pcp_short = vcshort;
		p.pcp_human = vchuman;
		p.pcp_print = pcieadm_cfgspace_print_regdef;
		p.pcp_arg = pcieadm_regdef_vc_rsrccap;

		p.pcp_print(walkp, &p, p.pcp_arg);

		(void) snprintf(vcshort, sizeof (vcshort), "rsrcctl%u", i);
		(void) snprintf(vchuman, sizeof (vchuman), "VC Resource %u "
		    "Control", i);
		p.pcp_off = print->pcp_off + i * 0x10 + 4;
		p.pcp_len = 4;
		p.pcp_short = vcshort;
		p.pcp_human = vchuman;
		p.pcp_print = pcieadm_cfgspace_print_regdef;
		p.pcp_arg = pcieadm_regdef_vc_rsrcctl;

		p.pcp_print(walkp, &p, p.pcp_arg);

		(void) snprintf(vcshort, sizeof (vcshort), "rsrcsts%u", i);
		(void) snprintf(vchuman, sizeof (vchuman), "VC Resource %u "
		    "Status", i);
		p.pcp_off = print->pcp_off + i * 0x10 + 0xa;
		p.pcp_len = 2;
		p.pcp_short = vcshort;
		p.pcp_human = vchuman;
		p.pcp_print = pcieadm_cfgspace_print_regdef;
		p.pcp_arg = pcieadm_regdef_vc_rsrcsts;

		p.pcp_print(walkp, &p, p.pcp_arg);
	}
}

static const pcieadm_cfgspace_print_t pcieadm_cap_vc[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "cap1", "Port VC Capability 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_vc_cap1 },
	{ 0x8, 4, "cap2", "Port VC Capability 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_vc_cap2 },
	{ 0xc, 2, "ctl", "Port VC Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_vc_ctl },
	{ 0xe, 2, "sts", "Port VC Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_vc_sts },
	{ 0x10, 12, "vcrec", "VC Resource", pcieadm_cfgspace_print_vc_rsrc },
	{ -1, -1, NULL }
};

/*
 * HyperTransport
 */
static const pcieadm_cfgspace_print_t pcieadm_cap_ht_intr[] = {
	{ 0x2, 1, "index", "Interrupt Discovery Index",
	    pcieadm_cfgspace_print_hex },
	{ 0x4, 4, "dataport", "Interrupt Dataport",
	    pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ht_command_pri[] = {
	{ 0, 4, "unitid", "Base Unit ID", PRDV_HEX },
	{ 5, 9, "count", "Unit Count", PRDV_HEX },
	{ 10, 10, "host", "Master Host", PRDV_HEX },
	{ 11, 11, "dir", "Default Direction", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "towards host",
	    "away from host" } } },
	{ 12, 12, "drop", "Drop on Uninitialized Link", PRDV_HEX },
	{ 13, 15, "cap", "Capability ID", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ht_command_sec[] = {
	{ 0, 0, "reset", "Warm Reset", PRDV_HEX },
	{ 1, 1, "de", "Double Ended", PRDV_HEX },
	{ 2, 6, "devno", "Device Number", PRDV_HEX },
	{ 7, 7, "chain", "Chain Side", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "from host", "from chain" } } },
	{ 8, 8, "hide", "Host Hide", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "visible", "hidden" } } },
	{ 10, 10, "target", "Act as Target", PRDV_HEX },
	{ 11, 11, "eocerr", "Host Inbound End of Chain Error", PRDV_HEX },
	{ 12, 12, "drop", "Drop on Uninitialized Link", PRDV_HEX },
	{ 13, 15, "cap", "Capability ID", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ht_linkctl[] = {
	{ 0, 0, "srcid", "Source ID", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "cfl", "CRC Flood", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "cst", "CRC Start Test", PRDV_HEX },
	{ 3, 3, "cfer", "CRC Force Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 4, "linkfail", "Link Failure", PRDV_HEX },
	{ 5, 5, "initcmp", "Initialization Complete", PRDV_HEX },
	{ 6, 6, "eoc", "End of Chain", PRDV_HEX },
	{ 7, 7, "txoff", "Transmitter Off", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "transmitter on",
	    "transmitter off" } } },
	{ 8, 11, "crcerr", "CRC Error", PRDV_HEX },
	{ 12, 12, "isoc", "Isochronous Flow Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 13, 13, "ls", "LDTSTOP# Tristate", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 14, 14, "extctl", "Extended CTL Time", PRDV_HEX },
	{ 15, 15, "64b", "64-bit Addressing", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ht_linkcfg[] = {
	{ 0, 2, "maxin", "Maximum Link Width In", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "8 bits", "16 bits", NULL, "32 bits",
	    "2 bits", "4 bits", NULL, "not connected" } } },
	{ 3, 3, "dwfcinsup", "Doubleword Flow Control In", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 4, 6, "maxout", "Maximum Link Width Out", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "8 bits", "16 bits", NULL, "32 bits",
	    "2 bits", "4 bits", NULL, "not connected" } } },
	{ 7, 7, "dwfcoutsup", "Doubleword Flow Control Out", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 10, "linkin", "Link Width In", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "8 bits", "16 bits", NULL, "32 bits",
	    "2 bits", "4 bits", NULL, "not connected" } } },
	{ 11, 11, "dwfcin", "Doubleword Flow Control In", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 12, 14, "linkout", "Link Width Out", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "8 bits", "16 bits", NULL, "32 bits",
	    "2 bits", "4 bits", NULL, "not connected" } } },
	{ 15, 15, "dwfcout", "Doubleword Flow Control Out", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ht_rev[] = {
	{ 0, 4, "minor", "Minor Revision", PRDV_HEX },
	{ 5, 7, "major", "Major Revision", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ht_linkfreq[] = {
	{ 0, 4, "freq", "Link Frequency", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "200 MHz", "300 MHz", "400 MHz",
	    "500 MHz", "600 MHz", "800 MHz", "1000 MHz", "1200 MHz", "1400 MHz",
	    "1600 MHz", "1800 MHz", "2000 MHz", "2200 MHz", "2400 MHz",
	    "2600 MHz", "Vendor Specfic" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ht_linkerr[] = {
	{ 4, 4, "prot", "Protocol Error", PRDV_HEX },
	{ 5, 5, "over", "Overflow Error", PRDV_HEX },
	{ 6, 6, "eoc", "End of Chain Error", PRDV_HEX },
	{ 7, 7, "ctl", "CTL Timeout", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ht_linkcap[] = {
	{ 0, 15, "freq", "Link Frequency", PRDV_BITFIELD,
	    .prd_val = { .prdv_strval = { "200 MHz", "300 MHz", "400 MHz",
	    "500 MHz", "600 MHz", "800 MHz", "1000 MHz", "1200 MHz", "1400 MHz",
	    "1600 MHz", "1800 MHz", "2000 MHz", "2200 MHz", "2400 MHz",
	    "2600 MHz", "Vendor Specfic" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ht_feature[] = {
	{ 0, 0, "isofc", "Isochronous Flow Control", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 1, 1, "ls", "LDTSTOP#", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 2, 2, "crct", "CRC Test Mode", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 3, 3, "ectl", "Extended CTL Time", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not required", "required" } } },
	{ 4, 4, "64b", "64-bit Addressing", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 5, 5, "unitid", "UnitID Reorder", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "enabled", "disabled" } } },
	{ 6, 6, "srcid", "Source Identification Extension", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "not required", "required" } } },
	{ 8, 8, "extreg", "Extended Register Set", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 9, 9, "uscfg", "Upstream Configuration", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ht_error[] = {
	{ 0, 0, "protfl", "Protocol Error Flood", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "ovfl", "Overflow Error Flood", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "protf", "Protocol Error Fatal", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 3, 3, "ovf", "Overflow Error Fatal", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 4, "eocf", "End of Chain Fatal Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 5, 5, "respf", "Response Error Fatal", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 6, 6, "crcf", "CRC Error Fatal", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 7, 7, "sysf", "System Error Fatal", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 8, 8, "chain", "Chain Fail", PRDV_HEX },
	{ 9, 9, "resp", "Response Error", PRDV_HEX },
	{ 10, 10, "protnf", "Protocol Error Non-Fatal", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 11, 11, "ovfnf", "Overflow Error Non-Fatal", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 12, 12, "eocnf", "End of Chain Error Non-Fatal", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 13, 13, "respnf", "Response Error Non-Fatal", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 14, 14, "crcnf", "CRC Error Non-Fatal", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 15, 15, "sysnf", "System Error Non-Fatal", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ht_memory[] = {
	{ 0, 8, "base", "Memory Base Upper 8 Bits", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 32 } } },
	{ 9, 15, "limit", "Memory Limit Upper 8 Bits", PRDV_HEX,
	    .prd_val = { .prdv_hex = { 32 } } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_ht_pri[] = {
	{ 0x2, 2, "command", "Command",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_command_pri },
	{ 0x4, 2, "linkctl0", "Link Control 0",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkctl },
	{ 0x6, 2, "linkcfg0", "Link Configuration 0",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkcfg },
	{ 0x8, 2, "linkctl1", "Link Control 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkctl },
	{ 0xa, 2, "linkcfg1", "Link Configuration 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkcfg },
	{ 0xc, 1, "rev", "Revision",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_rev },
	{ 0xd, 1, "linkfreq0", "Link Frequency 0",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkfreq },
	{ 0xd, 1, "linkerr0", "Link Error 0",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkerr },
	{ 0xe, 2, "linkfcap0", "Link Frequency Cap 0",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkcap },
	{ 0x10, 1, "feature", "Feature Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_feature },
	{ 0x11, 1, "linkfreq1", "Link Frequency 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkfreq },
	{ 0x11, 1, "linkerr1", "Link Error 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkerr },
	{ 0x12, 2, "linkfcap1", "Link Frequency Cap 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkcap },
	{ 0x14, 2, "scratch", "Enumeration Scratchpad",
	    pcieadm_cfgspace_print_hex },
	{ 0x16, 2, "error", "Error Handling",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_error },
	{ 0x18, 2, "memory", "Memory",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_memory },
	{ 0x1a, 1, "bus", "Bus Number", pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_ht_sec[] = {
	{ 0x2, 2, "command", "Command",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_command_sec },
	{ 0x4, 2, "linkctl", "Link Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkctl },
	{ 0x6, 2, "linkcfg", "Link Configuration",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkcfg },
	{ 0x8, 1, "rev", "Revision",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_rev },
	{ 0x9, 1, "linkfreq", "Link Frequency 0",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkfreq },
	{ 0x9, 1, "linkerr", "Link Error 0",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkerr },
	{ 0xa, 2, "linkfcap", "Link Frequency Cap 0",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_linkcap },
	{ 0xc, 2, "feature", "Feature Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_feature },
	{ 0x10, 2, "scratch", "Enumeration Scratchpad",
	    pcieadm_cfgspace_print_hex },
	{ 0x12, 2, "error", "Error Handling",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_error },
	{ 0x14, 2, "memory", "Memory",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_memory },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ht_msi[] = {
	{ 0, 0, "en", "Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "fixed", "Fixed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ -1, -1, NULL }
};

static void
pcieadm_cfgspace_print_ht_msi_addr(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint8_t fixed = walkp->pcw_data->pcb_u8[walkp->pcw_capoff + 2];

	if (bitx8(fixed, 1, 1) != 0)
		return;

	pcieadm_cfgspace_print_hex(walkp, print, arg);
}

static const pcieadm_cfgspace_print_t pcieadm_cap_ht_msi[] = {
	{ 0x2, 2, "command", "Command",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ht_msi },
	{ 0x4, 8, "address", "MSI Address",
	    pcieadm_cfgspace_print_ht_msi_addr },
	{ -1, -1, NULL }
};

/*
 * Capability related tables
 */
typedef struct pcieadm_cap_vers {
	uint32_t ppr_vers;
	uint32_t ppr_len;
	const pcieadm_cfgspace_print_t *ppr_print;
} pcieadm_cap_vers_t;

typedef struct pcieadm_subcap {
	const char *psub_short;
	const char *psub_human;
} pcieadm_subcap_t;

typedef struct pcieadm_pci_cap pcieadm_pci_cap_t;

typedef void (*pcieadm_cap_info_f)(pcieadm_cfgspace_walk_t *,
    const pcieadm_pci_cap_t *, uint32_t, const pcieadm_cap_vers_t **,
    uint32_t *, const pcieadm_subcap_t **);

struct pcieadm_pci_cap {
	uint32_t ppc_id;
	const char *ppc_short;
	const char *ppc_human;
	pcieadm_cap_info_f ppc_info;
	const pcieadm_cap_vers_t ppc_vers[4];
};

/*
 * Capability version determinations.
 */

static void
pcieadm_cap_info_fixed(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_pci_cap_t *cap, uint32_t off,
    const pcieadm_cap_vers_t **versp, uint32_t *lenp,
    const pcieadm_subcap_t **subcap)
{
	*versp = &cap->ppc_vers[0];
	*lenp = cap->ppc_vers[0].ppr_len;
	*subcap = NULL;
}

static void
pcieadm_cap_info_vers(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_pci_cap_t *cap, uint32_t off,
    const pcieadm_cap_vers_t **versp, uint32_t *lenp,
    const pcieadm_subcap_t **subcap)
{
	uint8_t vers;

	*subcap = NULL;
	vers = walkp->pcw_data->pcb_u8[off + 2] & 0xf;
	for (uint32_t i = 0; i < ARRAY_SIZE(cap->ppc_vers); i++) {
		if (vers == cap->ppc_vers[i].ppr_vers &&
		    cap->ppc_vers[i].ppr_vers != 0) {
			*versp = &cap->ppc_vers[i];
			*lenp = cap->ppc_vers[i].ppr_len;
			return;
		}
	}

	*versp = NULL;
	*lenp = 0;
}

/*
 * The PCI Power Management capability uses a 3-bit version ID as opposed to the
 * standard 4-bit version.
 */
static void
pcieadm_cap_info_pcipm(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_pci_cap_t *cap, uint32_t off,
    const pcieadm_cap_vers_t **versp, uint32_t *lenp,
    const pcieadm_subcap_t **subcap)
{
	uint8_t vers;

	*subcap = NULL;
	vers = walkp->pcw_data->pcb_u8[off + 2] & 0x7;
	for (uint32_t i = 0; i < ARRAY_SIZE(cap->ppc_vers); i++) {
		if (vers == cap->ppc_vers[i].ppr_vers) {
			*versp = &cap->ppc_vers[i];
			*lenp = cap->ppc_vers[i].ppr_len;
			return;
		}
	}

	*versp = NULL;
	*lenp = 0;
}

/*
 * The PCIe capability underwent a few changes. In version 1 of the capability,
 * devices were not required to implement the entire capability. In particular,
 * endpoints did not need to implement anything more than the link status
 * register. In the v2 capability, this was changed such that all devices had to
 * implement the entire capbility, but otherwise hardcode registers to zero. As
 * such we get to play guess the length based on the device type.
 */
static const pcieadm_cap_vers_t pcieadm_cap_vers_pcie_v1_dev = {
	1, 0x0c, pcieadm_cap_pcie_v1_dev
};

static const pcieadm_cap_vers_t pcieadm_cap_vers_pcie_v1_link = {
	1, 0x14, pcieadm_cap_pcie_v1_link
};

static const pcieadm_cap_vers_t pcieadm_cap_vers_pcie_v1_slot = {
	1, 0x1c, pcieadm_cap_pcie_v1_slot
};

static const pcieadm_cap_vers_t pcieadm_cap_vers_pcie_v1_all = {
	1, 0x24, pcieadm_cap_pcie_v1_all
};

static const pcieadm_cap_vers_t pcieadm_cap_vers_pcie_v2 = {
	2, 0x4c, pcieadm_cap_pcie_v2
};

static void
pcieadm_cap_info_pcie(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_pci_cap_t *cap, uint32_t off,
    const pcieadm_cap_vers_t **versp, uint32_t *lenp,
    const pcieadm_subcap_t **subcap)
{
	uint8_t vers = walkp->pcw_data->pcb_u8[off + 2] & 0xf;
	uint16_t pcie = walkp->pcw_data->pcb_u8[off + 2] |
	    (walkp->pcw_data->pcb_u8[off + 3] << 8);

	/*
	 * Version 2 is simple. There's only one thing to do, so we do it. For
	 * version 1 we need to look at the device type.
	 */
	*subcap = NULL;
	if (vers == 2) {
		*versp = &pcieadm_cap_vers_pcie_v2;
		*lenp = (*versp)->ppr_len;
		return;
	} else if (vers != 1) {
		*versp = NULL;
		*lenp = 0;
		return;
	}

	switch (pcie & PCIE_PCIECAP_DEV_TYPE_MASK) {
	case PCIE_PCIECAP_DEV_TYPE_PCIE_DEV:
	case PCIE_PCIECAP_DEV_TYPE_PCI_DEV:
		*versp = &pcieadm_cap_vers_pcie_v1_link;
		break;
	case PCIE_PCIECAP_DEV_TYPE_RC_IEP:
		*versp = &pcieadm_cap_vers_pcie_v1_dev;
		break;
	case PCIE_PCIECAP_DEV_TYPE_UP:
	case PCIE_PCIECAP_DEV_TYPE_DOWN:
	case PCIE_PCIECAP_DEV_TYPE_PCIE2PCI:
	case PCIE_PCIECAP_DEV_TYPE_PCI2PCIE:
		if ((pcie & PCIE_PCIECAP_SLOT_IMPL) != 0) {
			*versp = &pcieadm_cap_vers_pcie_v1_slot;
		} else {
			*versp = &pcieadm_cap_vers_pcie_v1_link;
		}
		break;
	case PCIE_PCIECAP_DEV_TYPE_ROOT:
	case PCIE_PCIECAP_DEV_TYPE_RC_EC:
		*versp = &pcieadm_cap_vers_pcie_v1_all;
		break;
	default:
		*versp = NULL;
		*lenp = 0;
		return;
	}

	*lenp = (*versp)->ppr_len;
}

/*
 * The length of the MSI capability depends on bits in its control field. As
 * such we use a custom function to extract the length and treat each of these
 * variants as thought it were a different version.
 */
static pcieadm_cap_vers_t pcieadm_cap_vers_msi_32 = {
	0, 0xa, pcieadm_cap_msi_32
};

static pcieadm_cap_vers_t pcieadm_cap_vers_msi_32ext = {
	0, 0xc, pcieadm_cap_msi_32ext
};

static pcieadm_cap_vers_t pcieadm_cap_vers_msi_64 = {
	0, 0xe, pcieadm_cap_msi_64
};

static pcieadm_cap_vers_t pcieadm_cap_vers_msi_64ext = {
	0, 0x10, pcieadm_cap_msi_64ext
};

static pcieadm_cap_vers_t pcieadm_cap_vers_msi_32pvm = {
	0, 0x14, pcieadm_cap_msi_32pvm
};

static pcieadm_cap_vers_t pcieadm_cap_vers_msi_64pvm = {
	0, 0x18, pcieadm_cap_msi_64pvm
};

static void
pcieadm_cap_info_msi(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_pci_cap_t *cap, uint32_t off,
    const pcieadm_cap_vers_t **versp, uint32_t *lenp,
    const pcieadm_subcap_t **subcap)
{
	uint16_t ctrl;
	boolean_t addr64, pvm, ext;

	*subcap = NULL;
	ctrl = walkp->pcw_data->pcb_u8[off + 2] |
	    (walkp->pcw_data->pcb_u8[off + 3] << 8);
	if (ctrl == PCI_EINVAL16) {
		warnx("failed to read MSI Message Control register");
		*lenp = 0;
		*versp = NULL;
		return;
	}

	/*
	 * The MSI capability has three main things that control its size.
	 * 64-bit addressing adds 4 bytes. Per-Vector Masking adds 8 bytes and
	 * causes the Extended data addressing piece to always be present.
	 * Therefore we check first for pvm as it implies evt, effectively.
	 */
	addr64 = (ctrl & PCI_MSI_64BIT_MASK) != 0;
	pvm = (ctrl & PCI_MSI_PVM_MASK) != 0;
	ext = (ctrl & PCI_MSI_EMD_MASK) != 0;

	if (pvm && addr64) {
		*versp = &pcieadm_cap_vers_msi_64pvm;
	} else if (pvm) {
		*versp = &pcieadm_cap_vers_msi_32pvm;
	} else if (addr64 && ext) {
		*versp = &pcieadm_cap_vers_msi_64ext;
	} else if (addr64) {
		*versp = &pcieadm_cap_vers_msi_64;
	} else if (ext) {
		*versp = &pcieadm_cap_vers_msi_32ext;
	} else {
		*versp = &pcieadm_cap_vers_msi_32;
	}

	*lenp = (*versp)->ppr_len;
}

/*
 * The AER Capability is technically different for PCIe-PCI bridges. If we find
 * that device type here, then we need to use a different version information
 * rather than the actual set defined with the device (which have changed over
 * time).
 */
static const pcieadm_cap_vers_t pcieadm_cap_vers_aer_bridge = {
	1, 0x4c, pcieadm_cap_aer_bridge
};

static void
pcieadm_cap_info_aer(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_pci_cap_t *cap, uint32_t off,
    const pcieadm_cap_vers_t **versp, uint32_t *lenp,
    const pcieadm_subcap_t **subcap)
{
	if (walkp->pcw_pcietype == PCIE_PCIECAP_DEV_TYPE_PCIE2PCI) {
		uint8_t vers;

		*subcap = NULL;
		vers = walkp->pcw_data->pcb_u8[off + 2] & 0xf;
		if (vers != pcieadm_cap_vers_aer_bridge.ppr_vers) {
			warnx("encountered PCIe to PCI bridge with unknown "
			    "AER capability version: %u", vers);
			*lenp = 0;
			*versp = NULL;
			return;
		}
		*lenp = pcieadm_cap_vers_aer_bridge.ppr_len;
		*versp = &pcieadm_cap_vers_aer_bridge;
	}

	return (pcieadm_cap_info_vers(walkp, cap, off, versp, lenp, subcap));
}

/*
 * The PCI-X capability varies depending on the header type of the device.
 * Therefore we simply use the device type to figure out what to do.
 */
static pcieadm_cap_vers_t pcieadm_cap_vers_pcix_dev = {
	0, 0x8, pcieadm_cap_pcix_dev
};

static pcieadm_cap_vers_t pcieadm_cap_vers_pcix_bridge = {
	0, 0x10, pcieadm_cap_pcix_bridge
};

static void
pcieadm_cap_info_pcix(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_pci_cap_t *cap, uint32_t off,
    const pcieadm_cap_vers_t **versp, uint32_t *lenp,
    const pcieadm_subcap_t **subcap)
{

	*subcap = NULL;
	switch (walkp->pcw_dtype) {
	case PCI_HEADER_ZERO:
		*versp = &pcieadm_cap_vers_pcix_dev;
		break;
	case PCI_HEADER_ONE:
		*versp = &pcieadm_cap_vers_pcix_bridge;
		break;
	default:
		warnx("encountered PCI-X capability with unsupported device "
		    "type: 0x%x\n", walkp->pcw_dtype);
		*lenp = 0;
		*versp = NULL;
		return;
	}

	*lenp = (*versp)->ppr_len;
}

typedef struct pcieadm_cap_ht {
	uint32_t pch_capid;
	pcieadm_subcap_t pch_subcap;
	pcieadm_cap_vers_t pch_vers;
} pcieadm_cap_ht_t;

static pcieadm_cap_ht_t pcieadm_ht_cap_pri = {
	0x00, { "pri", "Primary" }, { 0, 0x1c, pcieadm_cap_ht_pri }
};

static pcieadm_cap_ht_t pcieadm_ht_cap_sec = {
	0x01, { "sec", "Secondary" }, { 0, 0x18, pcieadm_cap_ht_sec }
};

static pcieadm_cap_ht_t pcieadm_ht_caps[] = {
	{ 0x08, { "switch", "Switch" } },
	{ 0x10, { "intr", "Interrupt Discovery and Configuration" },
	    { 0, 8, pcieadm_cap_ht_intr } },
	{ 0x11, { "rev", "Revision ID" } },
	{ 0x12, { "unitid", "UnitID Clumping" } },
	{ 0x13, { "extcfg", "Extended Configuration Space Access" } },
	{ 0x14, { "addrmap", "Address Mapping" } },
	{ 0x15, { "msi", "MSI Mapping" },
	    { 0, 4, pcieadm_cap_ht_msi } },
	{ 0x16, { "dir", "DirectRoute" } },
	{ 0x17, { "vcset", "VCSet" } },
	{ 0x18, { "retry", "Retry Mode" } },
	{ 0x19, { "x86", "X86 Encoding" } },
	{ 0x1a, { "gen3", "Gen3" } },
	{ 0x1b, { "fle", "Function-Level Extension" } },
	{ 0x1c, { "pm", "Power Management" } },
	{ UINT32_MAX, NULL },
};

static void
pcieadm_cap_info_ht(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_pci_cap_t *cap, uint32_t off,
    const pcieadm_cap_vers_t **versp, uint32_t *lenp,
    const pcieadm_subcap_t **subcap)
{
	uint32_t base = walkp->pcw_data->pcb_u32[off / 4];
	uint32_t caplo = bitx32(base, 31, 29);
	pcieadm_cap_ht_t *htcap = NULL;

	*versp = NULL;
	*lenp = 0;
	*subcap = NULL;

	if (caplo > 1) {
		uint32_t capid = bitx32(base, 31, 27);

		for (uint32_t i = 0; pcieadm_ht_caps[i].pch_capid != UINT32_MAX;
		    i++) {
			if (capid == pcieadm_ht_caps[i].pch_capid) {
				htcap = &pcieadm_ht_caps[i];
				break;
			}
		}
	} else if (caplo == 0) {
		htcap = &pcieadm_ht_cap_pri;
	} else if (caplo == 1) {
		htcap = &pcieadm_ht_cap_sec;
	}

	if (htcap == NULL) {
		warnx("encountered unknown HyperTransport Capability 0x%x",
		    bitx32(base, 31, 27));
		return;
	}

	*subcap = &htcap->pch_subcap;
	if (htcap->pch_vers.ppr_print != NULL) {
		*versp = &htcap->pch_vers;
		*lenp = htcap->pch_vers.ppr_len;
	}
}

/*
 * Root Complex Link Declaration
 */
static const pcieadm_regdef_t pcieadm_regdef_rcld_desc[] = {
	{ 0, 3, "type", "Element Type", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "Configuration Space Element",
	    "System Egress Port or internal sink",
	    "Internal Root Complex Link" } } },
	{ 8, 15, "num", "Number of Entries", PRDV_HEX },
	{ 16, 23, "id", "Component ID", PRDV_HEX },
	{ 24, 31, "port", "Port Number", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_rcld_link[] = {
	{ 0, 0, "valid", "Link Valid", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 1, 1, "type", "Link Type", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "RCRB", "Configuration Space" } } },
	{ 2, 2, "rcrb", "Assosciate RCRB", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 16, 23, "tid", "Target Component ID", PRDV_HEX },
	{ 24, 31, "tport", "Target Port Number", PRDV_HEX },
	{ -1, -1, NULL }
};

/*
 * Print a variable number of Root Complex Links.
 */
static void
pcieadm_cfgspace_print_rcld(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint_t nlinks = walkp->pcw_data->pcb_u8[walkp->pcw_capoff + 5];

	for (uint_t i = 0; i < nlinks; i++) {
		char mshort[32], mhuman[128];
		pcieadm_cfgspace_print_t p;
		uint16_t off = print->pcp_off + i * 0x10;
		uint8_t type = walkp->pcw_data->pcb_u8[walkp->pcw_capoff + off];

		(void) snprintf(mshort, sizeof (mshort), "link%udesc", i);
		(void) snprintf(mhuman, sizeof (mhuman), "Link %u Description");

		p.pcp_off = off;
		p.pcp_len = 4;
		p.pcp_short = mshort;
		p.pcp_human = mhuman;
		p.pcp_print = pcieadm_cfgspace_print_regdef;
		p.pcp_arg = pcieadm_regdef_rcld_link;

		p.pcp_print(walkp, &p, p.pcp_arg);

		/*
		 * The way that we print the link depends on the actual type of
		 * link which is in bit 2 of the link description.
		 */
		p.pcp_off += 8;

		if ((type & (1 << 1)) == 0) {
			(void) snprintf(mshort, sizeof (mshort),
			    "link%uaddr", i);
			(void) snprintf(mhuman, sizeof (mhuman),
			    "Link %u Address");
			p.pcp_len = 8;
			p.pcp_print = pcieadm_cfgspace_print_hex;
			p.pcp_arg = NULL;

			p.pcp_print(walkp, &p, p.pcp_arg);
		} else {
			warnx("encountered unsupported RCLD Link Address");
		}
	}
}

static const pcieadm_cfgspace_print_t pcieadm_cap_rcld[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "desc", "Self Description",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_rcld_desc },
	{ 0x10, 0x10, "link", "Link Entry", pcieadm_cfgspace_print_rcld },
	{ -1, -1, NULL }
};


/*
 * Physical Layer 32.0 GT/s Capability
 */
static const pcieadm_regdef_t pcieadm_regdef_32g_cap[] = {
	{ 0, 0, "eqbyp", "Equalization Bypass to Highest Rate", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 1, 1, "noeq", "No Equalization Needed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 8, "mts0", "Modified TS Usage Mode 0 - PCI Express", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 9, 9, "mts1", "Modified TS Usage Mode 1 - Training Set", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 10, 10, "mts2", "Modified TS Usage Mode 2 - Alternate Protocol",
	    PRDV_STRVAL, .prd_val = { .prdv_strval = { "unsupported",
	    "supported" } } },
	/*
	 * Bits 11 to 15 are defined as reserved for future use here as
	 * read-only bits. Add them here once they have actual definitions.
	 */
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_32g_ctl[] = {
	{ 0, 0, "eqbyp", "Equalization Bypass to Highest Rate", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "enabled", "disabled" } } },
	{ 1, 1, "noeq", "No Equalization Needed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "enabled", "disabled" } } },
	{ 8, 10, "mts", "Modified TS Usage Mode Selected", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "PCIe", "training set messages",
	    "alternate protocol negotiation" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_32g_sts[] = {
	{ 0, 0, "eqcomp", "Equalization 32.0 GT/s Complete", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "incomplete", "complete" } } },
	{ 1, 1, "eqp1", "Equalization 32.0 GT/s Phase 1", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "incomplete", "complete" } } },
	{ 2, 2, "eqp2", "Equalization 32.0 GT/s Phase 2", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "incomplete", "complete" } } },
	{ 3, 3, "eqp3", "Equalization 32.0 GT/s Phase 3", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "incomplete", "complete" } } },
	{ 4, 4, "req", "Link Equalization Request 32.0 GT/s", PRDV_HEX },
	{ 5, 5, "mts", "Modified TS Received", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 6, 7, "rxelbc", "Received Enhanced Link Behavior Control",
	    PRDV_STRVAL, .prd_val = { .prdv_strval = {
	    "full equalization required", "equalization bypass to highest rate",
	    "no equalization needed", "modified TS1/TS2 ordered sets" } } },
	{ 8, 8, "txpre", "Transmitter Precoding", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 9, 9, "prereq", "Transmitter Precoding Request", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 10, 10, "noeqrx", "No Equalization Needed Received", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_32g_rxts1[] = {
	{ 0, 2, "mts", "Modified TS Usage Mode Selected", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "PCIe", "training set messages",
	    "alternate protocol negotiation" } } },
	{ 3, 15, "info", "Received Modified TS Information 1", PRDV_HEX },
	{ 16, 31, "vendor", "Received Modified TS Vendor ID", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_32g_rxts2[] = {
	{ 0, 23, "info", "Received Modified TS Information 2", PRDV_HEX },
	{ 24, 25, "apnsts", "Alternate Protocol Negotiation Status",
	    PRDV_STRVAL, .prd_val = { .prdv_strval = { "not supported",
	    "disabled", "failed", "succeeded" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_32g_txts1[] = {
	{ 0, 2, "mts", "Transmitted Modified TS Usage Mode", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "PCIe", "training set messages",
	    "alternate protocol negotiation" } } },
	{ 3, 15, "info", "Transmitted Modified TS Information 1", PRDV_HEX },
	{ 16, 31, "vendor", "Transmitted Modified TS Vendor ID", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_32g_txts2[] = {
	{ 0, 23, "info", "Transmitted Modified TS Information 2", PRDV_HEX },
	{ 24, 25, "apnsts", "Alternate Protocol Negotiation Status",
	    PRDV_STRVAL, .prd_val = { .prdv_strval = { "not supported",
	    "disabled", "failed", "succeeded" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_32g_eq[] = {
	{ 0, 3, "dstxpre", "Downstream Port 32.0 GT/s Transmitter Preset",
	    PRDV_HEX },
	{ 4, 7, "ustxpre", "Upstream Port 32.0 GT/s Transmitter Preset",
	    PRDV_HEX },
	{ -1, -1, NULL }
};

static void
pcieadm_cfgspace_print_32geq(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	if (walkp->pcw_nlanes == 0) {
		warnx("failed to capture lane count, but somehow have "
		    "Physical Layer 32.0 GT/s cap");
		return;
	}

	for (uint_t i = 0; i < walkp->pcw_nlanes; i++) {
		char eqshort[32], eqhuman[128];
		pcieadm_cfgspace_print_t p;

		(void) snprintf(eqshort, sizeof (eqshort), "lane%u", i);
		(void) snprintf(eqhuman, sizeof (eqhuman), "Lane %u EQ Control",
		    i);
		p.pcp_off = print->pcp_off + i * 1;
		p.pcp_len = 1;
		p.pcp_short = eqshort;
		p.pcp_human = eqhuman;
		p.pcp_print = pcieadm_cfgspace_print_regdef;
		p.pcp_arg = pcieadm_regdef_32g_eq;

		p.pcp_print(walkp, &p, p.pcp_arg);
	}
}

static const pcieadm_cfgspace_print_t pcieadm_cap_32g[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "cap", "32.0 GT/s Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_32g_cap },
	{ 0x8, 4, "ctl", "32.0 GT/s Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_32g_ctl },
	{ 0xc, 4, "sts", "32.0 GT/s Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_32g_sts },
	{ 0x10, 4, "rxts1", "Received Modified TS Data 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_32g_rxts1 },
	{ 0x14, 4, "rxts2", "Received Modified TS Data 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_32g_rxts2 },
	{ 0x18, 4, "txts1", "Transmitted Modified TS Data 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_32g_txts1 },
	{ 0x1c, 4, "txts2", "Transmitted Modified TS Data 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_32g_txts2 },
	{ 0x20, 1, "eqctl", "32.0 GT/s EQ Control",
	    pcieadm_cfgspace_print_32geq },
	{ -1, -1, NULL }
};

/*
 * Native PCIe Enclosure Management
 */
static const pcieadm_regdef_t pcieadm_regdef_npem_cap[] = {
	{ 0, 0, "npem", "NPEM", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 1, 1, "reset", "NPEM Reset", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 2, 2, "ok", "NPEM OK", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 3, 3, "loc", "NPEM Locate", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 4, 4, "fail", "NPEM Fail", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 5, 5, "rb", "NPEM Rebuild", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 6, 6, "pfa", "NPEM PFA", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 7, 7, "hs", "NPEM Hot Spare", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 8, 8, "crit", "NPEM In a Critical Array", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 9, 9, "fail", "NPEM In a Failed Array", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 10, 10, "invdt", "NPEM Invalid Device type", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 11, 11, "dis", "NPEM Disabled", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 24, 31, "es", "Enclosure-specific Capabilities", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_npem_ctl[] = {
	{ 0, 0, "npem", "NPEM", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 1, "reset", "NPEM Initiate Reset", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "ok", "NPEM OK", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 3, 3, "loc", "NPEM Locate", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 4, 4, "fail", "NPEM Fail", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 5, 5, "rb", "NPEM Rebuild", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 6, 6, "pfa", "NPEM PFA", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 7, 7, "hs", "NPEM Hot Spare", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 8, 8, "crit", "NPEM In a Critical Array", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 9, 9, "fail", "NPEM In a Failed Array", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 10, 10, "invdt", "NPEM Invalid Device type", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 11, 11, "dis", "NPEM Disabled", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 24, 31, "es", "Enclosure-specific Control", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_npem_sts[] = {
	{ 0, 0, "ccmplt", "NPEM Command Complete", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 24, 31, "es", "Enclosure-specific Status", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_npem[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "cap", "NPEM Capability",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_npem_cap },
	{ 0x8, 4, "ctl", "NPEM Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_npem_ctl },
	{ 0xc, 4, "sts", "NPEM Status",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_npem_sts },
	{ -1, -1, NULL }
};

/*
 * Alternate Protocol Capability
 */
static const pcieadm_regdef_t pcieadm_regdef_ap_cap[] = {
	{ 0, 7, "count", "Alternate Protocol Count", PRDV_HEX },
	{ 8, 8, "sen", "Alternate Protocol Select Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ap_ctl[] = {
	{ 0, 7, "index", "Alternate Protocol Index Select", PRDV_HEX },
	{ 8, 8, "apngen", "Alternate Protocol Negotiation Global Enable",
	    PRDV_STRVAL, .prd_val = { .prdv_strval = { "disabled",
	    "enabled" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ap_data1[] = {
	{ 0, 2, "use", "Alternate Protocol Usage Information", PRDV_HEX },
	{ 5, 15, "detail", "Alternate Protocol Details", PRDV_HEX },
	{ 16, 31, "vendor", "Alternate Protocol Vendor ID", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ap_data2[] = {
	{ 0, 23, "mts2", "Modified TS 2 Information", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_ap_sen[] = {
	{ 0, 0, "pcie", "Selective Enable Mask - PCIe", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 1, 31, "other", "Selective Enable Mask - Other", PRDV_HEX },
	{ -1, -1, NULL }
};

/*
 * The Advanced Protocol Selective Enable Mask register is only present if a bit
 * in the capabilities register is present. As such, we need to check if it is
 * here before we try to read and print it.
 */
static void
pcieadm_cfgspace_print_ap_sen(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const void *arg)
{
	uint32_t ap_cap = pcieadm_cfgspace_getcap32(walkp, 4);
	pcieadm_cfgspace_print_t p;

	if (bitx32(ap_cap, 8, 8) == 0)
		return;

	(void) memcpy(&p, print, sizeof (*print));
	p.pcp_print = pcieadm_cfgspace_print_regdef;
	p.pcp_arg = pcieadm_regdef_ap_sen;

	p.pcp_print(walkp, &p, p.pcp_arg);
}

static const pcieadm_cfgspace_print_t pcieadm_cap_ap[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "cap", "Alternate Protocol Capabilities",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ap_cap },
	{ 0x8, 4, "ctl", "Alternate Protocol Control",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ap_ctl },
	{ 0xc, 4, "data1", "Alternate Protocol Data 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ap_data1 },
	{ 0x10, 4, "data2", "Alternate Protocol Data 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_ap_data2 },
	{ 0x14, 4, "sen", "Alternate Protocol Select Enable Mask",
	    pcieadm_cfgspace_print_ap_sen },
	{ -1, -1, NULL }
};

/*
 * Root Complex Event Collector Endpoint Association
 */
static const pcieadm_regdef_t pcieadm_regdef_rcecea_bus[] = {
	{ 8, 15, "next", "RCEC Next Bus", PRDV_HEX },
	{ 16, 23, "last", "RCEC Last Bus", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_rcecea_v1[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "bitmap", "Association Bitmap for RCiEPs",
	    pcieadm_cfgspace_print_hex },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_rcecea_v2[] = {
	{ 0x0, 4, "caphdr", "Capability Header",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "bitmap", "Association Bitmap for RCiEPs",
	    pcieadm_cfgspace_print_hex },
	{ 0x8, 4, "bus", "RCEC Associated Bus Numbers",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_rcecea_bus },
	{ -1, -1, NULL }
};

/*
 * Readiness Time Reporting
 */

/*
 * A single readiness time constitutes a 9 bit value and 3 bit scale. The actual
 * register contains a value in nanosconds which is calculated as value *
 * 32^scale. We take advantage of the fact that this is a power of 2 and
 * therefore we can calculate 32^scale by taking advantage that 32 is 2^5 and
 * that this is really 2^(scale * 5) which becomes a simple bit shift.
 */
static void
pcieadm_regdef_print_rtr(pcieadm_cfgspace_walk_t *walkp,
    const pcieadm_cfgspace_print_t *print, const pcieadm_regdef_t *regdef,
    uint64_t reg)
{
	uint64_t scale = bitx64(reg, 11, 9);
	uint64_t time = bitx64(reg, 8, 0);

	time *= 1ULL << (scale * 5);
	pcieadm_regdef_printf(walkp, print, regdef, reg, "%" PRIu64 " ns\n",
	    time);
}

static const pcieadm_regdef_t pcieadm_regdef_rtr1[] = {
	{ 0, 11, "reset", "Reset Time", PRDV_CUSTOM,
	    .prd_val = { .prdv_func = pcieadm_regdef_print_rtr } },
	{ 12, 23, "dlup", "DL_Up Time", PRDV_CUSTOM,
	    .prd_val = { .prdv_func = pcieadm_regdef_print_rtr } },
	{ 31, 31, "valid", "Valid", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "invalid", "valid" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_rtr2[] = {
	{ 0, 11, "flr", "FLR Time", PRDV_CUSTOM,
	    .prd_val = { .prdv_func = pcieadm_regdef_print_rtr } },
	{ 12, 23, "d3d0", "D3_HOT to D0 Time", PRDV_CUSTOM,
	    .prd_val = { .prdv_func = pcieadm_regdef_print_rtr } },
	{ -1, -1, NULL }
};

static const pcieadm_cfgspace_print_t pcieadm_cap_rtr[] = {
	{ 0x0, 4, "caphdr", "Capability Header", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "rtr1", "Readiness Time Reporting Register 1",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_rtr1 },
	{ 0x8, 4, "rtr2", "Readiness Time Reporting Register 2",
	    pcieadm_cfgspace_print_regdef, pcieadm_regdef_rtr2 },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_doe_cap[] = {
	{ 0, 0, "intr", "Interrupt", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 1, 11, "inum", "Interrupt Message Number", PRDV_HEX },
	{ 12, 12, "attn", "Attention Mechanism", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ 13, 13, "async", "Async Message", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "unsupported", "supported" } } },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_doe_ctl[] = {
	{ 0, 0, "abort", "Abort", PRDV_HEX },
	{ 1, 1, "intr", "Interrupt Enable", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 2, 2, "attn", "Attention Not Needed", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 3, 3, "async", "Async Message", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "disabled", "enabled" } } },
	{ 31, 31, "go", "Go", PRDV_HEX },
	{ -1, -1, NULL }
};

static const pcieadm_regdef_t pcieadm_regdef_doe_sts[] = {
	{ 0, 0, "busy", "Busy", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 1, 1, "intr", "Interrupt Status", PRDV_HEX },
	{ 2, 2, "error", "Error", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ 3, 3, "async", "Async Message Status", PRDV_HEX },
	{ 4, 4, "attn", "Attention", PRDV_HEX },
	{ 31, 31, "ready", "Data Object Ready", PRDV_STRVAL,
	    .prd_val = { .prdv_strval = { "no", "yes" } } },
	{ -1, -1, NULL }
};

/*
 * PCIe Data Object Exchange (DOE). There are two wrinkles to this capability
 * worth being aware of:
 *
 * 1. This capability contains two registers that we don't describe which are
 *    the read and write data mailboxes. These registers are designed as FIFOs.
 *    Critically reading the DOE read mailbox will consume data that a DOE
 *    consumer expects to be able to read. This is a very bad side effect.
 *    We do not include it (or write) at all to ensure that we can't end up
 *    accidentally mucking things up.
 * 2. There are two versions of the capability. Right now those are used to
 *    indicate whether a few features are present related to asynchronous
 *    messages and attention bits. The total number of registers and their
 *    offsets are the same. As such we opt to print both versions the same way
 *    right now.
 */
static const pcieadm_cfgspace_print_t pcieadm_cap_doe[] = {
	{ 0x0, 4, "caphdr", "Capability Header", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_pcie_caphdr },
	{ 0x4, 4, "cap", "DOE Capabilities", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_doe_cap },
	{ 0x8, 4, "ctl", "DOE Control", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_doe_ctl },
	{ 0xc, 4, "sts", "DOE Status", pcieadm_cfgspace_print_regdef,
	    pcieadm_regdef_doe_sts },
	{ -1, -1, NULL }
};

static const pcieadm_pci_cap_t pcieadm_pci_caps[] = {
	{ PCI_CAP_ID_PM, "pcipm", "PCI Power Management",
	    pcieadm_cap_info_pcipm, { { 2, 8, pcieadm_cap_pcipm_v3 },
	    { 3, 8, pcieadm_cap_pcipm_v3 } } },
	{ PCI_CAP_ID_AGP, "agp", "Accelerated Graphics Port" },
	{ PCI_CAP_ID_VPD, "vpd", "Vital Product Data", pcieadm_cap_info_fixed,
	    { { 0, 8, pcieadm_cap_vpd } } },
	{ PCI_CAP_ID_SLOT_ID, "slot", "Slot Identification" },
	{ PCI_CAP_ID_MSI, "msi", "Message Signaled Interrupts",
	    pcieadm_cap_info_msi },
	{ PCI_CAP_ID_cPCI_HS, "cpci", "CompactPCI Hot Swap" },
	{ PCI_CAP_ID_PCIX, "pcix", "PCI-X", pcieadm_cap_info_pcix },
	{ PCI_CAP_ID_HT, "ht", "HyperTransport", pcieadm_cap_info_ht },
	{ PCI_CAP_ID_VS, "vs", "Vendor Specific", pcieadm_cap_info_fixed,
	    { { 0, 3, pcieadm_cap_vs } } },
	{ PCI_CAP_ID_DEBUG_PORT, "dbg", "Debug Port", pcieadm_cap_info_fixed,
	    { { 0, 4, pcieadm_cap_debug } } },
	{ PCI_CAP_ID_cPCI_CRC, "cpcicrc",
	    "CompactPCI Central Resource Control" },
	{ PCI_CAP_ID_PCI_HOTPLUG, "pcihp", "PCI Hot-Plug" },
	{ PCI_CAP_ID_P2P_SUBSYS, "bdgsub", "PCI Bridge Subsystem Vendor ID",
	    pcieadm_cap_info_fixed, { 0, 8, pcieadm_cap_bridge_subsys } },
	{ PCI_CAP_ID_AGP_8X, "agp8x", "AGP 8x" },
	{ PCI_CAP_ID_SECURE_DEV, "secdev", "Secure Device" },
	{ PCI_CAP_ID_PCI_E, "pcie", "PCI Express", pcieadm_cap_info_pcie },
	{ PCI_CAP_ID_MSI_X, "msix", "MSI-X", pcieadm_cap_info_fixed,
	    { { 0, 12, pcieadm_cap_msix } } },
	{ PCI_CAP_ID_SATA, "sata", "Serial ATA Configuration",
	    pcieadm_cap_info_fixed, { { 0, 8, pcieadm_cap_sata } } },
	/*
	 * Note, the AF feature doesn't have a version but encodes a length in
	 * the version field, so we cheat and use that.
	 */
	{ PCI_CAP_ID_FLR, "af", "Advanced Features", pcieadm_cap_info_vers,
	    { { 6, 6, pcieadm_cap_af } } },
	{ PCI_CAP_ID_EA, "ea", "Enhanced Allocation" },
	{ PCI_CAP_ID_FPB, "fpb", "Flattening Portal Bridge" }
};

static const pcieadm_pci_cap_t pcieadm_pcie_caps[] = {
	{ 0, "null", "NULL Capability", pcieadm_cap_info_fixed,
	    { { 0, 0x4, pcieadm_cap_null } } },
	{ PCIE_EXT_CAP_ID_AER, "aer", "Advanced Error Reporting",
	    pcieadm_cap_info_aer, { { 1, 0x38, pcieadm_cap_aer_v1 },
	    { 2, 0x48, pcieadm_cap_aer_v2 } } },
	{ PCIE_EXT_CAP_ID_VC, "vc", "Virtual Channel", pcieadm_cap_info_vers,
	    { { 0x1, 0x1c, pcieadm_cap_vc } } },
	{ PCIE_EXT_CAP_ID_SER, "sn", "Serial Number", pcieadm_cap_info_vers,
	    { { 1, 0xc, pcieadm_cap_sn } } },
	{ PCIE_EXT_CAP_ID_PWR_BUDGET, "pwrbudg", "Power Budgeting",
	    pcieadm_cap_info_vers, { { 1, 0x10, pcieadm_cap_powbudg } } },
	{ PCIE_EXT_CAP_ID_RC_LINK_DECL, "rcld",
	    "Root Complex Link Declaration",  pcieadm_cap_info_vers,
	    { { 1, 0x1c, pcieadm_cap_rcld } } },
	{ PCIE_EXT_CAP_ID_RC_INT_LINKCTRL, "rcilc",
	    "Root Complex Internal Link Control" },
	{ PCIE_EXT_CAP_ID_RC_EVNT_CEA, "rcecea",
	    "Root Complex Event Collector Endpoint Association",
	    pcieadm_cap_info_vers, { { 1, 0x8, pcieadm_cap_rcecea_v1 },
	    { 2, 0xc, pcieadm_cap_rcecea_v2 } } },
	{ PCIE_EXT_CAP_ID_MFVC, "mfvc", "Multi-Function Virtual Channel" },
	{ PCIE_EXT_CAP_ID_VC_WITH_MFVC, "vcwmfvc", "Virtual Channel with MFVC",
	    pcieadm_cap_info_vers, { { 0x1, 0x1c, pcieadm_cap_vc } } },
	{ PCIE_EXT_CAP_ID_RCRB, "rcrb", "Root Complex Register Block" },
	{ PCIE_EXT_CAP_ID_VS, "vsec", "Vendor Specific Extended Capability",
	    pcieadm_cap_info_vers, { { 1, 0x8, pcieadm_cap_vsec } } },
	{ PCIE_EXT_CAP_ID_CAC, "cac", "Configuration Access Correlation" },
	{ PCIE_EXT_CAP_ID_ACS, "acs", "Access Control Services",
	    pcieadm_cap_info_vers, { { 1, 0x8, pcieadm_cap_acs } } },
	{ PCIE_EXT_CAP_ID_ARI, "ari", "Alternative Routing-ID Interpretation",
	    pcieadm_cap_info_vers, { { 1, 0x8, pcieadm_cap_ari } } },
	{ PCIE_EXT_CAP_ID_ATS, "ats", "Access Translation Services",
	    pcieadm_cap_info_vers, { { 1, 0x8, pcieadm_cap_ats } } },
	{ PCIE_EXT_CAP_ID_SRIOV, "sriov", "Single Root I/O Virtualization",
	    pcieadm_cap_info_vers, { { 1, 0x40, pcieadm_cap_sriov } } },
	{ PCIE_EXT_CAP_ID_MRIOV, "mriov", "Multi-Root I/O Virtualization" },
	{ PCIE_EXT_CAP_ID_MULTICAST, "mcast", "Multicast",
	    pcieadm_cap_info_vers, { { 1, 0x30, pcieadm_cap_mcast } } },
	{ PCIE_EXT_CAP_ID_PGREQ, "pgreq", "Page Request",
	    pcieadm_cap_info_vers, { { 1, 0x10, pcieadm_cap_pgreq } } },
	{ PCIE_EXT_CAP_ID_EA, "ea", "Enhanced Allocation" },
	{ PCIE_EXT_CAP_ID_RESIZE_BAR, "rbar", "Resizable Bar" },
	{ PCIE_EXT_CAP_ID_DPA, "dpa", "Dynamic Power Allocation",
	    pcieadm_cap_info_vers, { { 1, 0x10, pcieadm_cap_dpa } } },
	{ PCIE_EXT_CAP_ID_TPH_REQ, "tph", "TPH Requester",
	    pcieadm_cap_info_vers, { { 1, 0xc, pcieadm_cap_tph } } },
	{ PCIE_EXT_CAP_ID_LTR, "ltr", "Latency Tolerance Reporting",
	    pcieadm_cap_info_vers, { { 1, 0x8, pcieadm_cap_ltr } } },
	{ PCIE_EXT_CAP_ID_PCIE2, "pcie2", "Secondary PCI Express",
	    pcieadm_cap_info_vers, { { 1, 0xc, pcieadm_cap_pcie2 } } },
	{ PCIE_EXT_CAP_ID_PASID, "pasid", "Process Address Space ID",
	    pcieadm_cap_info_vers, { { 1, 0x8, pcieadm_cap_pasid } } },
	{ PCIE_EXT_CAP_ID_LNR, "lnr", "LN Requester" },
	{ PCIE_EXT_CAP_ID_DPC, "dpc", "Downstream Port Containment",
	    pcieadm_cap_info_vers, { { 1, 0x30, pcieadm_cap_dpc } } },
	{ PCIE_EXT_CAP_ID_L1PM, "l1pm", "L1 PM Substates",
	    pcieadm_cap_info_vers, { { 1, 0x10, pcieadm_cap_l1pm_v1 },
	    { 2, 0x14, pcieadm_cap_l1pm_v2 } } },
	{ PCIE_EXT_CAP_ID_PTM, "ptm", "Precision Time Management",
	    pcieadm_cap_info_vers, { { 1, 0xc, pcieadm_cap_info_ptm } } },
	{ PCIE_EXT_CAP_ID_FRS, "frs", "FRS Queueing" },
	{ PCIE_EXT_CAP_ID_RTR, "rtr", "Readiness Time Reporting",
	    pcieadm_cap_info_vers, { { 1, 0xc, pcieadm_cap_rtr } } },
	/*
	 * When we encounter a designated vendor specification, in particular,
	 * for CXL, we'll want to set ppc_subcap so we can use reasonable
	 * filtering.
	 */
	{ PCIE_EXT_CAP_ID_DVS, "dvsec",
	    "Designated Vendor-Specific Extended Capability" },
	{ PCIE_EXT_CAP_ID_VFRBAR, "vfrbar", "Virtual Function Resizable BAR" },
	{ PCIE_EXT_CAP_ID_DLF, "dlf", "Data Link Feature",
	    pcieadm_cap_info_vers, { { 1, 0xc, pcieadm_cap_dlf } } },
	{ PCIE_EXT_CAP_ID_PL16GT, "pl16g", "Physical Layer 16.0 GT/s",
	    pcieadm_cap_info_vers, { { 1, 0x22, pcieadm_cap_16g } } },
	{ PCIE_EXT_CAP_ID_LANE_MARGIN, "margin",
	    "Lane Margining at the Receiver", pcieadm_cap_info_vers,
	    { { 1, 0x8, pcieadm_cap_margin } } },
	{ PCIE_EXT_CAP_ID_HIEARCHY_ID, "hierid", "Hierarchy ID" },
	{ PCIE_EXT_CAP_ID_NPEM, "npem", "Native PCIe Enclosure Management",
	    pcieadm_cap_info_vers, { { 1, 0x10, pcieadm_cap_npem } } },
	/*
	 * The sizing of the 32.0 GT/s physical layer requires that there's at
	 * least one lane's worth of information and the device is required to
	 * pad that out to 4-byte alignment.
	 */
	{ PCIE_EXT_CAP_ID_PL32GT, "pl32g", "Physical Layer 32.0 GT/s",
	    pcieadm_cap_info_vers, { { 1, 0x24, pcieadm_cap_32g } } },
	{ PCIE_EXT_CAP_ID_AP, "ap", "Alternative Protocol",
	    pcieadm_cap_info_vers, { { 1, 0x14, pcieadm_cap_ap } } },
	{ PCIE_EXT_CAP_ID_SFI, "sfi", "System Firmware Intermediary" },
	{ PCIE_EXT_CAP_ID_SHDW_FUNC, "sfunc", "Shadow Functions" },
	{ PCIE_EXT_CAP_ID_DOE, "doe", "Data Object Exchange",
	    pcieadm_cap_info_vers, { { 1, 0x10, pcieadm_cap_doe },
	    { 2, 0x10, pcieadm_cap_doe } } },
	{ PCIE_EXT_CAP_ID_DEV3, "dev3", "Device 3" },
	{ PCIE_EXT_CAP_ID_IDE, "ide", "Integrity and Data Encryption" },
	{ PCIE_EXT_CAP_ID_PL64GT, "pl64g", "Physical Layer 64.0 GT/s" },
	{ PCIE_EXT_CAP_ID_FLIT_LOG, "fltlog", "Flit Logging" },
	{ PCIE_EXT_CAP_ID_FLIT_PERF, "fltperf",
	    "Flit Performance Measurement" },
	{ PCIE_EXT_CAP_ID_FLIT_ERR, "flterr", "Flit Error Injection" },
	{ PCIE_EXT_CAP_ID_SVC, "svc", "Streamlined Virtual Channel" },
	{ PCIE_EXT_CAP_ID_MMIO_RBL, "mrbl", "MMIO Register Block Locator" },
	{ PCIE_EXT_CAP_ID_NOP_FLIT, "nop", "NOP Flit" },
	{ PCIE_EXT_CAP_ID_SIOV, "siov", "Scalable I/O Virtualization" },
	{ PCIE_EXT_CAP_ID_PL128GT, "pl128g", "Physical Layer 128.0 GT/s" },
	{ PCIE_EXT_CAP_ID_CAP_DATA, "cdata", "Captured Data" }
};

static const pcieadm_pci_cap_t *
pcieadm_cfgspace_match_cap(uint32_t capid, boolean_t pcie)
{
	uint_t ncaps;
	const pcieadm_pci_cap_t *caps;

	if (pcie) {
		ncaps = ARRAY_SIZE(pcieadm_pcie_caps);
		caps = pcieadm_pcie_caps;
	} else {
		ncaps = ARRAY_SIZE(pcieadm_pci_caps);
		caps = pcieadm_pci_caps;
	}

	for (uint_t i = 0; i < ncaps; i++) {
		if (caps[i].ppc_id == capid) {
			return (&caps[i]);
		}
	}

	return (NULL);
}

static void
pcieadm_cfgspace_print_cap(pcieadm_cfgspace_walk_t *walkp, uint_t capid,
    const pcieadm_pci_cap_t *cap_info, const pcieadm_cap_vers_t *vers_info,
    const pcieadm_subcap_t *subcap)
{
	boolean_t filter = B_FALSE;

	/*
	 * If we don't recognize the capability, print out the ID if we're not
	 * filtering and not in parsable mode.
	 */
	if (cap_info == NULL) {
		if (walkp->pcw_ofmt == NULL &&
		    pcieadm_cfgspace_filter(walkp, NULL)) {
			warnx("encountered unknown capability ID 0x%x "
			    "unable to print or list", capid);
			pcieadm_print("Unknown Capability (0x%x)\n", capid);
		}
		return;
	}

	/*
	 * Check to see if we should print this and in particular, if there's
	 * both a capability or subcapability, we need to try and match both.
	 * The reason that the calls to check the filters are conditioned on
	 * pcw_ofmt is that when we're in parsable mode, we cannot match a
	 * top-level capability since it's an arbitrary number of fields.
	 */
	if (walkp->pcw_ofmt == NULL) {
		filter = pcieadm_cfgspace_filter(walkp, cap_info->ppc_short);
	}
	pcieadm_strfilt_push(walkp, cap_info->ppc_short);
	if (subcap != NULL) {
		if (walkp->pcw_ofmt == NULL) {
			boolean_t subfilt = pcieadm_cfgspace_filter(walkp,
			    subcap->psub_short);
			filter = subfilt || filter;
		}
		pcieadm_strfilt_push(walkp, subcap->psub_short);
	}


	if (walkp->pcw_ofmt == NULL && filter) {
		if ((walkp->pcw_flags & PCIEADM_CFGSPACE_F_SHORT) != 0) {
			if (subcap != NULL) {
				pcieadm_print("%s Capability - %s (%s) "
				    "(0x%x)\n", cap_info->ppc_human,
				    subcap->psub_human,
				    walkp->pcw_filt->pstr_curgen, capid);
			} else {
				pcieadm_print("%s Capability (%s) (0x%x)\n",
				    cap_info->ppc_human,
				    walkp->pcw_filt->pstr_curgen, capid);
			}
		} else {
			if (subcap != NULL) {
				pcieadm_print("%s Capability - %s (0x%x)\n",
				    cap_info->ppc_human, subcap->psub_human,
				    capid);
			} else {
				pcieadm_print("%s Capability (0x%x)\n",
				    cap_info->ppc_human, capid);
			}
		}
	}

	if (vers_info != NULL) {
		pcieadm_indent();
		pcieadm_cfgspace_print_table(walkp, vers_info->ppr_print);
		pcieadm_deindent();
	} else {
		if (subcap != NULL) {
			warnx("Unable to print or list %s - %s (no support or "
			    "missing version info)", cap_info->ppc_human,
			    subcap->psub_human);
		} else {
			warnx("Unable to print or list %s (no support or "
			    "missing version info)", cap_info->ppc_human);
		}
	}

	if (subcap != NULL) {
		pcieadm_strfilt_pop(walkp);
	}
	pcieadm_strfilt_pop(walkp);
}

static void
pcieadm_cfgspace_write(int fd, const uint8_t *source, size_t len)
{
	size_t off = 0;

	while (len > 0) {
		ssize_t ret = write(fd, source + off, len - off);
		if (ret < 0) {
			err(EXIT_FAILURE, "failed to write config space to "
			    "output file");
		}

		off += ret;
		len -= ret;
	}
}

void
pcieadm_cfgspace(pcieadm_t *pcip, pcieadm_cfgspace_op_t op,
    pcieadm_cfgspace_f readf, int fd, void *readarg, uint_t nfilts,
    pcieadm_cfgspace_filter_t *filters, pcieadm_cfgspace_flags_t flags,
    ofmt_handle_t ofmt)
{
	uint_t type;
	uint16_t cap;
	pcieadm_cfgspace_data_t data;
	pcieadm_cfgspace_walk_t walk;
	const char *headstr, *headshort;
	const pcieadm_cfgspace_print_t *header;
	boolean_t capsup = B_FALSE, extcfg = B_FALSE;
	uint_t ncaps;

	walk.pcw_pcieadm = pcip;
	walk.pcw_op = op;
	walk.pcw_data = &data;
	walk.pcw_outfd = fd;
	walk.pcw_capoff = 0;
	walk.pcw_nlanes = 0;
	walk.pcw_nfilters = nfilts;
	walk.pcw_filters = filters;
	walk.pcw_flags = flags;
	walk.pcw_ofmt = ofmt;
	walk.pcw_filt = NULL;

	/*
	 * Start by reading all of the basic 40-byte config space header in one
	 * fell swoop.
	 */
	for (uint32_t i = 0; i < PCI_CAP_PTR_OFF / 4; i++) {
		if (!readf(i * 4, 4, &data.pcb_u32[i], readarg)) {
			errx(EXIT_FAILURE, "failed to read offset %u from "
			    "configuration space", i * 4);
		}
	}
	walk.pcw_valid = PCI_CAP_PTR_OFF;
	walk.pcw_caplen = PCI_CAP_PTR_OFF;

	/*
	 * Grab the information from the header that we need to figure out what
	 * kind of device this is, how to print it, if there are any
	 * capabilities, and go from there.
	 */
	type = data.pcb_u8[PCI_CONF_HEADER] & PCI_HEADER_TYPE_M;
	switch (type) {
	case PCI_HEADER_ZERO:
		headstr = "Type 0 Header";
		headshort = "header0";
		header = pcieadm_cfgspace_type0;
		capsup = (data.pcb_u8[PCI_CONF_STAT] & PCI_STAT_CAP) != 0;
		break;
	case PCI_HEADER_ONE:
		headstr = "Type 1 Header";
		headshort = "header1";
		header = pcieadm_cfgspace_type1;
		capsup = (data.pcb_u8[PCI_CONF_STAT] & PCI_STAT_CAP) != 0;
		break;
	case PCI_HEADER_TWO:
	default:
		headstr = "Unknown Header";
		headshort = "headerX";
		header = pcieadm_cfgspace_unknown;
		warnx("unsupported PCI header type: 0x%x, output limited to "
		    "data configuration space", type);
	}

	walk.pcw_dtype = type;

	if (op == PCIEADM_CFGSPACE_OP_WRITE) {
		pcieadm_cfgspace_write(fd, &data.pcb_u8[0], PCI_CAP_PTR_OFF);
	} else if (op == PCIEADM_CFGSPACE_OP_PRINT) {
		if (walk.pcw_ofmt == NULL &&
		    pcieadm_cfgspace_filter(&walk, headshort)) {
			if ((flags & PCIEADM_CFGSPACE_F_SHORT) != 0) {
				pcieadm_print("Device %s -- %s (%s)\n",
				    pcip->pia_devstr, headstr, headshort);
			} else {
				pcieadm_print("Device %s -- %s\n",
				    pcip->pia_devstr, headstr);
			}
		}

		pcieadm_strfilt_push(&walk, headshort);
		pcieadm_indent();
		pcieadm_cfgspace_print_table(&walk, header);
		pcieadm_deindent();
		pcieadm_strfilt_pop(&walk);
	}


	if (!capsup) {
		return;
	}

	for (uint32_t i = PCI_CAP_PTR_OFF / 4; i < PCI_CONF_HDR_SIZE / 4; i++) {
		if (!readf(i * 4, 4, &data.pcb_u32[i], readarg)) {
			errx(EXIT_FAILURE, "failed to read offset %u from "
			    "configuration space", i * 4);
		}
	}
	walk.pcw_valid = PCIE_EXT_CAP;
	VERIFY3P(walk.pcw_filt, ==, NULL);

	if (op == PCIEADM_CFGSPACE_OP_WRITE) {
		pcieadm_cfgspace_write(fd, &data.pcb_u8[PCI_CAP_PTR_OFF],
		    PCI_CONF_HDR_SIZE - PCI_CAP_PTR_OFF);
	}

	ncaps = 0;
	cap = data.pcb_u8[PCI_CONF_CAP_PTR];
	while (cap != 0 && cap != PCI_EINVAL8) {
		const pcieadm_pci_cap_t *cap_info;
		const pcieadm_cap_vers_t *vers_info = NULL;
		const pcieadm_subcap_t *subcap = NULL;
		uint8_t cap_id, nextcap;
		uint32_t read_len = 0;

		/*
		 * The PCI specification requires that the caller mask off the
		 * bottom two bits. Always check for an invalid value (all 1s)
		 * before this.
		 */
		cap &= PCI_CAP_PTR_MASK;
		cap_id = data.pcb_u8[cap + PCI_CAP_ID];
		nextcap = data.pcb_u8[cap + PCI_CAP_NEXT_PTR];
		cap_info = pcieadm_cfgspace_match_cap(cap_id, B_FALSE);
		if (cap_info != NULL && cap_info->ppc_info != NULL) {
			cap_info->ppc_info(&walk, cap_info, cap, &vers_info,
			    &read_len, &subcap);
		}

		walk.pcw_caplen = read_len;
		walk.pcw_capoff = cap;

		if (cap_id == PCI_CAP_ID_PCI_E) {
			extcfg = B_TRUE;
			if (walk.pcw_valid != 0) {
				walk.pcw_pcietype = data.pcb_u8[cap +
				    PCIE_PCIECAP] & PCIE_PCIECAP_DEV_TYPE_MASK;
				walk.pcw_nlanes = (data.pcb_u8[cap +
				    PCIE_LINKCAP] & 0xf0) >> 4;
				walk.pcw_nlanes |= (data.pcb_u8[cap +
				    PCIE_LINKCAP + 1] & 0x01) << 4;
			} else {
				walk.pcw_pcietype = UINT_MAX;
			}
		}

		if (op == PCIEADM_CFGSPACE_OP_PRINT) {
			pcieadm_cfgspace_print_cap(&walk, cap_id, cap_info,
			    vers_info, subcap);
		}

		cap = nextcap;
		ncaps++;
		if (ncaps >= PCI_CAP_MAX_PTR) {
			errx(EXIT_FAILURE, "encountered more PCI capabilities "
			    "than fit in configuration space");
		}
	}

	if (!extcfg) {
		return;
	}

	for (uint_t i = PCIE_EXT_CAP / 4; i < PCIE_CONF_HDR_SIZE / 4; i++) {
		if (!readf(i * 4, 4, &data.pcb_u32[i], readarg)) {
			errx(EXIT_FAILURE, "failed to read offset %u from "
			    "configuration space", i * 4);
		}
	}
	walk.pcw_valid = PCIE_CONF_HDR_SIZE;

	if (op == PCIEADM_CFGSPACE_OP_WRITE) {
		pcieadm_cfgspace_write(fd, &data.pcb_u8[PCIE_EXT_CAP],
		    PCIE_CONF_HDR_SIZE - PCIE_EXT_CAP);
		return;
	}

	cap = PCIE_EXT_CAP;
	ncaps = 0;
	while (cap != 0 && cap != PCI_EINVAL16) {
		uint16_t cap_id, nextcap;
		const pcieadm_pci_cap_t *cap_info;
		const pcieadm_cap_vers_t *vers_info = NULL;
		const pcieadm_subcap_t *subcap = NULL;
		uint32_t read_len = 0;

		/*
		 * PCIe has the same masking as PCI. Note, sys/pcie.h currently
		 * has PCIE_EXT_CAP_NEXT_PTR_MASK as 0xfff, instead of the
		 * below. This should be switched to PCIE_EXT_CAP_NEXT_PTR_MASK
		 * when the kernel headers are fixed.
		 */
		cap &= 0xffc;

		/*
		 * While this seems duplicative of the loop condition, a device
		 * without capabilities indicates it with a zero for the first
		 * cap.
		 */
		if (data.pcb_u32[cap / 4] == 0 ||
		    data.pcb_u32[cap / 4] == PCI_EINVAL32)
			break;

		cap_id = data.pcb_u32[cap / 4] & PCIE_EXT_CAP_ID_MASK;
		nextcap = (data.pcb_u32[cap / 4] >>
		    PCIE_EXT_CAP_NEXT_PTR_SHIFT) & PCIE_EXT_CAP_NEXT_PTR_MASK;

		cap_info = pcieadm_cfgspace_match_cap(cap_id, B_TRUE);
		if (cap_info != NULL && cap_info->ppc_info != NULL) {
			cap_info->ppc_info(&walk, cap_info, cap, &vers_info,
			    &read_len, &subcap);
		}

		walk.pcw_caplen = read_len;
		walk.pcw_capoff = cap;

		if (op == PCIEADM_CFGSPACE_OP_PRINT) {
			pcieadm_cfgspace_print_cap(&walk, cap_id, cap_info,
			    vers_info, subcap);
		}

		cap = nextcap;
		ncaps++;
		if (ncaps >= PCIE_EXT_CAP_MAX_PTR) {
			errx(EXIT_FAILURE, "encountered more PCI capabilities "
			    "than fit in configuration space");
		}
	}
}

void
pcieadm_show_cfgspace_usage(FILE *f)
{
	(void) fprintf(f, "\tshow-cfgspace\t[-L] [-n] [-H] -d device | -f file "
	    "[filter...]\n");
	(void) fprintf(f, "\tshow-cfgspace\t-p -o field[,...] [-H] -d device | "
	    "-f file [filter...]\n");
}

static void
pcieadm_show_cfgspace_help(const char *fmt, ...)
{
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vwarnx(fmt, ap);
		va_end(ap);
		(void) fprintf(stderr, "\n");
	}

	(void) fprintf(stderr, "Usage:  %s show-cfgspace [-L] [-n] [-H] -d "
	    "device | -f file [filter...]\n", pcieadm_progname);
	(void) fprintf(stderr, "        %s show-cfgspace -p -o field[,...] "
	    "[-H] -d device | -f file\n\t\t\t      [filter...]\n",
	    pcieadm_progname);

	(void) fprintf(stderr, "\nPrint and decode PCI configuration space "
	    "data from a device or file. Each\n<filter> selects a given "
	    "capability, sub-capability, register, or field to print.\n\n"
	    "\t-d device\tread data from the specified device (driver instance,"
	    "\n\t\t\t/devices path, or b/d/f)\n"
	    "\t-f file\t\tread data from the specified file\n"
	    "\t-L\t\tlist printable fields\n"
	    "\t-n\t\tshow printable short names\n"
	    "\t-H\t\tomit the column header (for -L and -p)\n"
	    "\t-p\t\tparsable output (requires -o)\n"
	    "\t-o field\toutput fields to print (required for -p)\n\n"
	    "The following fields are supported:\n"
	    "\thuman\t\ta human-readable description of the specific output\n"
	    "\tshort\t\tthe short name of the value used for filters\n"
	    "\tvalue\t\tthe value of a the given capability, register, etc.\n"
	    "\tlength\t\tthe length of the field in bytes\n"
	    "\tbitlen\t\tthe number of additional bits long the field is\n"
	    "\toffset\t\tthe offset in bytes to the start of the field\n"
	    "\tbitoff\t\tthe additional number of bits to the start of the "
	    "field\n");
}

int
pcieadm_show_cfgspace(pcieadm_t *pcip, int argc, char *argv[])
{
	int c, ret;
	const pcieadm_ops_t *ops;
	void *readarg;
	boolean_t list = B_FALSE, parse = B_FALSE;
	const char *device = NULL, *file = NULL, *fields = NULL;
	uint_t nfilts = 0;
	pcieadm_cfgspace_filter_t *filts = NULL;
	pcieadm_cfgspace_flags_t flags = 0;
	uint_t oflags = 0;
	ofmt_handle_t ofmt = NULL;

	while ((c = getopt(argc, argv, ":HLd:f:o:np")) != -1) {
		switch (c) {
		case 'd':
			device = optarg;
			break;
		case 'L':
			list = B_TRUE;
			break;
		case 'f':
			file = optarg;
			break;
		case 'p':
			parse = B_TRUE;
			flags |= PCIEADM_CFGSPACE_F_PARSE;
			oflags |= OFMT_PARSABLE;
			break;
		case 'n':
			flags |= PCIEADM_CFGSPACE_F_SHORT;
			break;
		case 'H':
			oflags |= OFMT_NOHEADER;
			break;
		case 'o':
			fields = optarg;
			break;
		case ':':
			pcieadm_show_cfgspace_help("Option -%c requires an "
			    "argument", optopt);
			exit(EXIT_USAGE);
		case '?':
		default:
			pcieadm_show_cfgspace_help("unknown option: -%c",
			    optopt);
			exit(EXIT_USAGE);
		}
	}

	argc -= optind;
	argv += optind;

	if (device == NULL && file == NULL) {
		pcieadm_show_cfgspace_help("one of -d or -f must be specified");
		exit(EXIT_USAGE);
	}

	if (device != NULL && file != NULL) {
		pcieadm_show_cfgspace_help("only one of -d and -f must be "
		    "specified");
		exit(EXIT_USAGE);
	}

	if (parse && fields == NULL) {
		pcieadm_show_cfgspace_help("-p requires fields specified with "
		    "-o");
		exit(EXIT_USAGE);
	}

	if (!parse && fields != NULL) {
		pcieadm_show_cfgspace_help("-o can only be used with -p");
		exit(EXIT_USAGE);
	}

	if ((oflags & OFMT_NOHEADER) && !(list || parse)) {
		pcieadm_show_cfgspace_help("-H must be used with either -L or "
		    "-p");
		exit(EXIT_USAGE);
	}

	if ((flags & PCIEADM_CFGSPACE_F_SHORT) && (list || parse)) {
		pcieadm_show_cfgspace_help("-n cannot be used with either -L "
		    "or -p");
		exit(EXIT_USAGE);
	}

	if (list && parse != 0) {
		pcieadm_show_cfgspace_help("-L and -p cannot be used together");
		exit(EXIT_USAGE);
	}

	if (list && fields != NULL) {
		pcieadm_show_cfgspace_help("-L and -o cannot be used together");
		exit(EXIT_USAGE);
	}

	if (list) {
		fields = "short,human";
	}

	if (argc > 0) {
		nfilts = argc;
		filts = calloc(nfilts, sizeof (pcieadm_cfgspace_filter_t));

		for (int i = 0; i < argc; i++) {
			filts[i].pcf_string = argv[i];
			filts[i].pcf_len = strlen(argv[i]);
		}
	}

	if (list || parse) {
		ofmt_status_t oferr;
		oferr = ofmt_open(fields, pcieadm_cfgspace_ofmt, oflags, 0,
		    &ofmt);
		ofmt_check(oferr, parse, ofmt, pcieadm_ofmt_errx, warnx);
	}

	/*
	 * Initialize privileges that we require. For reading from the kernel
	 * we require all privileges. For a file, we just intersect with things
	 * that would allow someone to read from any file.
	 */
	if (device != NULL) {
		/*
		 * We need full privileges if reading from a device,
		 * unfortunately.
		 */
		priv_fillset(pcip->pia_priv_eff);
	} else {
		VERIFY0(priv_addset(pcip->pia_priv_eff, PRIV_FILE_DAC_READ));
		VERIFY0(priv_addset(pcip->pia_priv_eff, PRIV_FILE_DAC_SEARCH));
	}
	pcieadm_init_privs(pcip);

	if (device != NULL) {
		pcieadm_find_dip(pcip, device);
		pcieadm_init_ops_kernel(pcip, &ops, &readarg);
	} else {
		pcip->pia_devstr = file;
		pcieadm_init_ops_file(pcip, file, &ops, &readarg);
	}
	pcieadm_cfgspace(pcip, PCIEADM_CFGSPACE_OP_PRINT, ops->pop_cfg, -1,
	    readarg, nfilts, filts, flags, ofmt);
	if (device != NULL) {
		pcieadm_fini_ops_kernel(readarg);
	} else {
		pcieadm_fini_ops_file(readarg);
	}

	ofmt_close(ofmt);
	ret = EXIT_SUCCESS;
	for (uint_t i = 0; i < nfilts; i++) {
		if (!filts[i].pcf_used) {
			warnx("filter '%s' did not match any fields",
			    filts[i].pcf_string);
			ret = EXIT_FAILURE;
		}
	}

	return (ret);
}

typedef struct pcieadm_save_cfgspace {
	pcieadm_t *psc_pci;
	int psc_dirfd;
	uint_t psc_nsaved;
	int psc_ret;
} pcieadm_save_cfgspace_t;

static int
pcieadm_save_cfgspace_cb(di_node_t devi, void *arg)
{
	int fd, nregs, *regs;
	pcieadm_save_cfgspace_t *psc = arg;
	const pcieadm_ops_t *ops;
	void *readarg;
	char fname[128];

	psc->psc_pci->pia_devstr = di_node_name(devi);
	psc->psc_pci->pia_devi = devi;
	psc->psc_pci->pia_nexus = DI_NODE_NIL;
	pcieadm_find_nexus(psc->psc_pci);
	if (psc->psc_pci->pia_nexus == DI_NODE_NIL) {
		warnx("failed to find nexus for %s", di_node_name(devi));
		psc->psc_ret = EXIT_FAILURE;
		return (DI_WALK_CONTINUE);
	}

	nregs = di_prop_lookup_ints(DDI_DEV_T_ANY, devi, "reg", &regs);
	if (nregs <= 0) {
		warnx("failed to lookup regs array for %s",
		    psc->psc_pci->pia_devstr);
		psc->psc_ret = EXIT_FAILURE;
		return (DI_WALK_CONTINUE);
	}

	(void) snprintf(fname, sizeof (fname), "%02x-%02x-%02x.pci",
	    PCI_REG_BUS_G(regs[0]), PCI_REG_DEV_G(regs[0]),
	    PCI_REG_FUNC_G(regs[0]));

	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, psc->psc_pci->pia_priv_eff) !=
	    0) {
		err(EXIT_FAILURE, "failed to raise privileges");
	}
	fd = openat(psc->psc_dirfd, fname, O_WRONLY | O_TRUNC | O_CREAT, 0666);
	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, psc->psc_pci->pia_priv_min) !=
	    0) {
		err(EXIT_FAILURE, "failed to reduce privileges");
	}

	if (fd < 0) {
		warn("failed to create output file %s", fname);
		psc->psc_ret = EXIT_FAILURE;
		return (DI_WALK_CONTINUE);
	}

	pcieadm_init_ops_kernel(psc->psc_pci, &ops, &readarg);
	pcieadm_cfgspace(psc->psc_pci, PCIEADM_CFGSPACE_OP_WRITE, ops->pop_cfg,
	    fd, readarg, 0, NULL, 0, NULL);
	pcieadm_fini_ops_kernel(readarg);

	if (close(fd) != 0) {
		warn("failed to close output fd for %s", fname);
		psc->psc_ret = EXIT_FAILURE;
	} else {
		psc->psc_nsaved++;
	}

	return (DI_WALK_CONTINUE);
}

void
pcieadm_save_cfgspace_usage(FILE *f)
{
	(void) fprintf(f, "\tsave-cfgspace\t-d device output-file\n");
	(void) fprintf(f, "\tsave-cfgspace\t-a output-directory\n");
}

static void
pcieadm_save_cfgspace_help(const char *fmt, ...)
{
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vwarnx(fmt, ap);
		va_end(ap);
		(void) fprintf(stderr, "\n");
	}

	(void) fprintf(stderr, "Usage:  %s save-cfgspace -d device "
	    "output-file\n", pcieadm_progname);
	(void) fprintf(stderr, "        %s save-cfgspace -a "
	    "output-directory\n", pcieadm_progname);

	(void) fprintf(stderr, "\nSave PCI configuration space data from a "
	    "device to a file or\nsave all devices to a specified directory."
	    "\n\n"
	    "\t-a\t\tsave data from all devices\n"
	    "\t-d device\tread data from the specified device (driver instance,"
	    "\n\t\t\t/devices path, or b/d/f)\n");
}

int
pcieadm_save_cfgspace(pcieadm_t *pcip, int argc, char *argv[])
{
	int c;
	void *readarg;
	const char *device = NULL;
	boolean_t do_all = B_FALSE;

	while ((c = getopt(argc, argv, ":ad:")) != -1) {
		switch (c) {
		case 'a':
			do_all = B_TRUE;
			break;
		case 'd':
			device = optarg;
			break;
		case ':':
			pcieadm_save_cfgspace_help("Option -%c requires an "
			    "argument", optopt);
			exit(EXIT_USAGE);
		case '?':
		default:
			pcieadm_save_cfgspace_help("unknown option: -%c",
			    optopt);
			exit(EXIT_USAGE);
		}
	}

	argc -= optind;
	argv += optind;

	if (device == NULL && !do_all) {
		pcieadm_save_cfgspace_help("missing required -d option to "
		    "indicate device to dump");
		exit(EXIT_USAGE);
	}

	if (argc != 1) {
		pcieadm_save_cfgspace_help("missing required output path");
		exit(EXIT_USAGE);
	}

	/*
	 * For reading from devices, we need to full privileges, unfortunately.
	 */
	priv_fillset(pcip->pia_priv_eff);
	pcieadm_init_privs(pcip);

	if (!do_all) {
		int fd;
		const pcieadm_ops_t *ops;

		pcieadm_find_dip(pcip, device);

		if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcip->pia_priv_eff) !=
		    0) {
			err(EXIT_FAILURE, "failed to raise privileges");
		}

		if ((fd = open(argv[0], O_WRONLY | O_CREAT | O_TRUNC, 0666)) <
		    0) {
			err(EXIT_FAILURE, "failed to open output file %s",
			    argv[0]);
		}

		if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcip->pia_priv_min) !=
		    0) {
			err(EXIT_FAILURE, "failed to reduce privileges");
		}

		pcieadm_init_ops_kernel(pcip, &ops, &readarg);
		pcieadm_cfgspace(pcip, PCIEADM_CFGSPACE_OP_WRITE, ops->pop_cfg,
		    fd, readarg, 0, NULL, 0, NULL);
		pcieadm_fini_ops_kernel(readarg);

		if (close(fd) != 0) {
			err(EXIT_FAILURE, "failed to close output file "
			    "descriptor");
		}

		return (EXIT_SUCCESS);
	} else {
		pcieadm_save_cfgspace_t psc;
		pcieadm_di_walk_t walk;

		if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcip->pia_priv_eff) !=
		    0) {
			err(EXIT_FAILURE, "failed to raise privileges");
		}

		if ((psc.psc_dirfd = open(argv[0], O_RDONLY | O_DIRECTORY)) <
		    0) {
			err(EXIT_FAILURE, "failed to open output directory %s",
			    argv[0]);
		}

		if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcip->pia_priv_min) !=
		    0) {
			err(EXIT_FAILURE, "failed to reduce privileges");
		}

		psc.psc_nsaved = 0;
		psc.psc_ret = EXIT_SUCCESS;
		psc.psc_pci = pcip;

		walk.pdw_arg = &psc;
		walk.pdw_func = pcieadm_save_cfgspace_cb;
		pcieadm_di_walk(pcip, &walk);

		VERIFY0(close(psc.psc_dirfd));

		if (psc.psc_nsaved == 0) {
			warnx("failed to save any PCI devices");
			return (EXIT_FAILURE);
		}

		pcieadm_print("successfully saved %u devices to %s\n",
		    psc.psc_nsaved, argv[0]);
		return (psc.psc_ret);
	}
}
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2026 Oxide Computer Company
 */

#include <err.h>
#include <stdio.h>
#include <unistd.h>
#include <ofmt.h>
#include <strings.h>
#include <sys/pci.h>
#include <fm/libtopo.h>
#include <sys/fm/protocol.h>
#include <fm/topo_pcie.h>

#include "pcieadm.h"

typedef struct pcieadm_show_devs {
	pcieadm_t *psd_pia;
	ofmt_handle_t psd_ofmt;
	boolean_t psd_funcs;
	int psd_nfilts;
	char **psd_filts;
	boolean_t *psd_used;
	uint_t psd_nprint;
	topo_hdl_t *psd_topo;
} pcieadm_show_devs_t;

typedef enum pcieadm_show_devs_otype {
	PCIEADM_SDO_VID,
	PCIEADM_SDO_DID,
	PCIEADM_SDO_REV,
	PCIEADM_SDO_SUBVID,
	PCIEADM_SDO_SUBSYS,
	PCIEADM_SDO_BDF,
	PCIEADM_SDO_BDF_BUS,
	PCIEADM_SDO_BDF_DEV,
	PCIEADM_SDO_BDF_FUNC,
	PCIEADM_SDO_DRIVER,
	PCIEADM_SDO_INSTANCE,
	PCIEADM_SDO_INSTNUM,
	PCIEADM_SDO_TYPE,
	PCIEADM_SDO_VENDOR,
	PCIEADM_SDO_DEVICE,
	PCIEADM_SDO_SUBVENDOR,
	PCIEADM_SDO_SUBSYSTEM,
	PCIEADM_SDO_PATH,
	PCIEADM_SDO_MAXSPEED,
	PCIEADM_SDO_MAXWIDTH,
	PCIEADM_SDO_CURSPEED,
	PCIEADM_SDO_CURWIDTH,
	PCIEADM_SDO_SUPSPEEDS,
	PCIEADM_SDO_SLOTNO,
	PCIEADM_SDO_AP,
	PCIEADM_SDO_CTLAP,
	PCIEADM_SDO_LOC
} pcieadm_show_devs_otype_t;

typedef struct pcieadm_show_devs_ofmt {
	int psdo_vid;
	int psdo_did;
	int psdo_rev;
	int psdo_subvid;
	int psdo_subsys;
	uint_t psdo_bus;
	uint_t psdo_dev;
	uint_t psdo_func;
	const char *psdo_path;
	const char *psdo_vendor;
	const char *psdo_device;
	const char *psdo_subvendor;
	const char *psdo_subsystem;
	const char *psdo_driver;
	int psdo_instance;
	int psdo_mwidth;
	int psdo_cwidth;
	int64_t psdo_mspeed;
	int64_t psdo_cspeed;
	int psdo_nspeeds;
	int64_t *psdo_sspeeds;
	int32_t psdo_slotno;
	char *psdo_ap;
	char *psdo_ctlap;
	char *psdo_loc;
} pcieadm_show_devs_ofmt_t;

static uint_t
pcieadm_speed2gen(int64_t speed)
{
	if (speed == 2500000000LL) {
		return (1);
	} else if (speed == 5000000000LL) {
		return (2);
	} else if (speed == 8000000000LL) {
		return (3);
	} else if (speed == 16000000000LL) {
		return (4);
	} else if (speed == 32000000000LL) {
		return (5);
	} else {
		return (0);
	}
}

static const char *
pcieadm_speed2str(int64_t speed)
{
	if (speed == 2500000000LL) {
		return ("2.5");
	} else if (speed == 5000000000LL) {
		return ("5.0");
	} else if (speed == 8000000000LL) {
		return ("8.0");
	} else if (speed == 16000000000LL) {
		return ("16.0");
	} else if (speed == 32000000000LL) {
		return ("32.0");
	} else {
		return (NULL);
	}
}

static boolean_t
pcieadm_show_devs_ofmt_cb(ofmt_arg_t *ofarg, char *buf, uint_t buflen)
{
	const char *str;
	pcieadm_show_devs_ofmt_t *psdo = ofarg->ofmt_cbarg;
	boolean_t first = B_TRUE;
	size_t ret;

	switch (ofarg->ofmt_id) {
	case PCIEADM_SDO_BDF:
		ret = snprintf(buf, buflen, "%x/%x/%x", psdo->psdo_bus,
		    psdo->psdo_dev, psdo->psdo_func);
		break;
	case PCIEADM_SDO_BDF_BUS:
		ret = snprintf(buf, buflen, "%x", psdo->psdo_bus);
		break;
	case PCIEADM_SDO_BDF_DEV:
		ret = snprintf(buf, buflen, "%x", psdo->psdo_dev);
		break;
	case PCIEADM_SDO_BDF_FUNC:
		ret = snprintf(buf, buflen, "%x", psdo->psdo_func);
		break;
	case PCIEADM_SDO_INSTANCE:
		if (psdo->psdo_driver == NULL || psdo->psdo_instance == -1) {
			ret = snprintf(buf, buflen, "--");
		} else {
			ret = snprintf(buf, buflen, "%s%d", psdo->psdo_driver,
			    psdo->psdo_instance);
		}
		break;
	case PCIEADM_SDO_DRIVER:
		if (psdo->psdo_driver == NULL) {
			ret = snprintf(buf, buflen, "--");
		} else {
			ret = strlcpy(buf, psdo->psdo_driver, buflen);
		}
		break;
	case PCIEADM_SDO_INSTNUM:
		if (psdo->psdo_instance == -1) {
			ret = snprintf(buf, buflen, "--");
		} else {
			ret = snprintf(buf, buflen, "%d", psdo->psdo_instance);
		}
		break;
	case PCIEADM_SDO_PATH:
		ret = strlcat(buf, psdo->psdo_path, buflen);
		break;
	case PCIEADM_SDO_VID:
		if (psdo->psdo_vid == -1) {
			ret = strlcat(buf, "--", buflen);
		} else {
			ret = snprintf(buf, buflen, "%x", psdo->psdo_vid);
		}
		break;
	case PCIEADM_SDO_DID:
		if (psdo->psdo_did == -1) {
			ret = strlcat(buf, "--", buflen);
		} else {
			ret = snprintf(buf, buflen, "%x", psdo->psdo_did);
		}
		break;
	case PCIEADM_SDO_REV:
		if (psdo->psdo_rev == -1) {
			ret = strlcat(buf, "--", buflen);
		} else {
			ret = snprintf(buf, buflen, "%x", psdo->psdo_rev);
		}
		break;
	case PCIEADM_SDO_SUBVID:
		if (psdo->psdo_subvid == -1) {
			ret = strlcat(buf, "--", buflen);
		} else {
			ret = snprintf(buf, buflen, "%x", psdo->psdo_subvid);
		}
		break;
	case PCIEADM_SDO_SUBSYS:
		if (psdo->psdo_subsys == -1) {
			ret = strlcat(buf, "--", buflen);
		} else {
			ret = snprintf(buf, buflen, "%x", psdo->psdo_subsys);
		}
		break;
	case PCIEADM_SDO_VENDOR:
		ret = strlcat(buf, psdo->psdo_vendor, buflen);
		break;
	case PCIEADM_SDO_DEVICE:
		ret = strlcat(buf, psdo->psdo_device, buflen);
		break;
	case PCIEADM_SDO_SUBVENDOR:
		ret = strlcat(buf, psdo->psdo_subvendor, buflen);
		break;
	case PCIEADM_SDO_SUBSYSTEM:
		ret = strlcat(buf, psdo->psdo_subsystem, buflen);
		break;
	case PCIEADM_SDO_MAXWIDTH:
		if (psdo->psdo_mwidth <= 0) {
			ret = strlcat(buf, "--", buflen);
		} else {
			ret = snprintf(buf, buflen, "x%d", psdo->psdo_mwidth);
		}
		break;
	case PCIEADM_SDO_CURWIDTH:
		if (psdo->psdo_cwidth <= 0) {
			ret = strlcat(buf, "--", buflen);
		} else {
			ret = snprintf(buf, buflen, "x%d", psdo->psdo_cwidth);
		}
		break;
	case PCIEADM_SDO_MAXSPEED:
		str = pcieadm_speed2str(psdo->psdo_mspeed);
		if (str == NULL) {
			ret = strlcat(buf, "--", buflen);
		} else {
			ret = snprintf(buf, buflen, "%s GT/s", str);
		}
		break;
	case PCIEADM_SDO_CURSPEED:
		str = pcieadm_speed2str(psdo->psdo_cspeed);
		if (str == NULL) {
			ret = strlcat(buf, "--", buflen);
		} else {
			ret = snprintf(buf, buflen, "%s GT/s", str);
		}
		break;
	case PCIEADM_SDO_SUPSPEEDS:
		buf[0] = 0;
		ret = 0;
		for (int i = 0; i < psdo->psdo_nspeeds; i++) {
			const char *str;

			str = pcieadm_speed2str(psdo->psdo_sspeeds[i]);
			if (str == NULL) {
				continue;
			}

			if (!first) {
				ret = strlcat(buf, ",", buflen);
			}

			first = B_FALSE;
			ret = strlcat(buf, str, buflen);
		}
		break;
	case PCIEADM_SDO_TYPE:
		/*
		 * We need to distinguish three different groups of things here:
		 *
		 *  - Something is a PCI device.
		 *  - We have a PCIe device where the link is down and therefore
		 *    have no current width or speed.
		 *  - We have a PCIe device which is up.
		 *
		 * A PCIe device should always have a maximum width value. This
		 * is required and therefore should be a good proxy for whether
		 * or not we have a PCIe device.
		 */
		if (psdo->psdo_mwidth == -1) {
			ret = strlcat(buf, "PCI", buflen);
			break;
		}

		/*
		 * If we don't have a valid link up, indicate we don't know.
		 * While the link is probably down, we don't have that as a
		 * guarantee right now.
		 */
		if (psdo->psdo_cspeed == -1 || psdo->psdo_cwidth == -1) {
			ret = strlcat(buf, "PCIe unknown", buflen);
			break;
		}

		ret = snprintf(buf, buflen, "PCIe Gen %ux%d",
		    pcieadm_speed2gen(psdo->psdo_cspeed), psdo->psdo_cwidth);
		break;
	case PCIEADM_SDO_SLOTNO:
		if (psdo->psdo_slotno < 0) {
			ret = strlcat(buf, "--", buflen);
		} else {
			ret = snprintf(buf, buflen, "0x%x", psdo->psdo_slotno);
		}
		break;
	case PCIEADM_SDO_AP:
		ret = strlcat(buf, psdo->psdo_ap != NULL ? psdo->psdo_ap : "--",
		    buflen);
		break;
	case PCIEADM_SDO_CTLAP:
		ret = strlcat(buf, psdo->psdo_ctlap != NULL ? psdo->psdo_ctlap :
		    "--", buflen);
		break;
	case PCIEADM_SDO_LOC:
		ret = strlcat(buf, psdo->psdo_loc != NULL ? psdo->psdo_loc :
		    "--", buflen);
		break;
	default:
		abort();
	}

	/*
	 * While the str* functions can't really fail, snprintf() returns an
	 * int. If it fails, that negative value will be cast up to UINT64_MAX
	 * (e.g. (size_t)-1), which will cause the check to always fail.
	 */
	return (ret < buflen);
}

static const char *pcieadm_show_dev_fields = "bdf,type,instance,device";
static const char *pcieadm_show_dev_speeds =
	"bdf,driver,maxspeed,curspeed,maxwidth,curwidth,supspeeds";
static const ofmt_field_t pcieadm_show_dev_ofmt[] = {
	{ "VID", 6, PCIEADM_SDO_VID, pcieadm_show_devs_ofmt_cb },
	{ "DID", 6, PCIEADM_SDO_DID, pcieadm_show_devs_ofmt_cb },
	{ "REV", 6, PCIEADM_SDO_REV, pcieadm_show_devs_ofmt_cb },
	{ "SUBVID", 6, PCIEADM_SDO_SUBVID, pcieadm_show_devs_ofmt_cb },
	{ "SUBSYS", 6, PCIEADM_SDO_SUBSYS, pcieadm_show_devs_ofmt_cb },
	{ "BDF", 8, PCIEADM_SDO_BDF, pcieadm_show_devs_ofmt_cb },
	{ "DRIVER", 15, PCIEADM_SDO_DRIVER, pcieadm_show_devs_ofmt_cb },
	{ "INSTANCE", 15, PCIEADM_SDO_INSTANCE, pcieadm_show_devs_ofmt_cb },
	{ "INSTNUM", 8, PCIEADM_SDO_INSTNUM, pcieadm_show_devs_ofmt_cb },
	{ "TYPE", 15, PCIEADM_SDO_TYPE, pcieadm_show_devs_ofmt_cb },
	{ "VENDOR", 30, PCIEADM_SDO_VENDOR, pcieadm_show_devs_ofmt_cb },
	{ "DEVICE", 30, PCIEADM_SDO_DEVICE, pcieadm_show_devs_ofmt_cb },
	{ "SUBVENDOR", 30, PCIEADM_SDO_SUBVENDOR, pcieadm_show_devs_ofmt_cb },
	{ "SUBSYSTEM", 30, PCIEADM_SDO_SUBSYSTEM, pcieadm_show_devs_ofmt_cb },
	{ "PATH", 30, PCIEADM_SDO_PATH, pcieadm_show_devs_ofmt_cb },
	{ "BUS", 4, PCIEADM_SDO_BDF_BUS, pcieadm_show_devs_ofmt_cb },
	{ "DEV", 4, PCIEADM_SDO_BDF_DEV, pcieadm_show_devs_ofmt_cb },
	{ "FUNC", 4, PCIEADM_SDO_BDF_FUNC, pcieadm_show_devs_ofmt_cb },
	{ "MAXSPEED", 10, PCIEADM_SDO_MAXSPEED, pcieadm_show_devs_ofmt_cb },
	{ "MAXWIDTH", 10, PCIEADM_SDO_MAXWIDTH, pcieadm_show_devs_ofmt_cb },
	{ "CURSPEED", 10, PCIEADM_SDO_CURSPEED, pcieadm_show_devs_ofmt_cb },
	{ "CURWIDTH", 10, PCIEADM_SDO_CURWIDTH, pcieadm_show_devs_ofmt_cb },
	{ "SUPSPEEDS", 20, PCIEADM_SDO_SUPSPEEDS, pcieadm_show_devs_ofmt_cb },
	{ "SLOTNO", 8, PCIEADM_SDO_SLOTNO, pcieadm_show_devs_ofmt_cb },
	{ "AP", 10, PCIEADM_SDO_AP, pcieadm_show_devs_ofmt_cb },
	{ "CTLAP", 10, PCIEADM_SDO_CTLAP, pcieadm_show_devs_ofmt_cb },
	{ NULL, 0, 0, NULL }
};

/*
 * This is a variant of the above fields when -L is in use. We don't always want
 * to take a topo snapshot to show devices so these fields are only available
 * when requested.
 */
static const char *pcieadm_show_dev_loc =
	"bdf,instance,location,slotno,ap,ctlap";
static const ofmt_field_t pcieadm_show_dev_loc_ofmt[] = {
	{ "VID", 6, PCIEADM_SDO_VID, pcieadm_show_devs_ofmt_cb },
	{ "DID", 6, PCIEADM_SDO_DID, pcieadm_show_devs_ofmt_cb },
	{ "REV", 6, PCIEADM_SDO_REV, pcieadm_show_devs_ofmt_cb },
	{ "SUBVID", 6, PCIEADM_SDO_SUBVID, pcieadm_show_devs_ofmt_cb },
	{ "SUBSYS", 6, PCIEADM_SDO_SUBSYS, pcieadm_show_devs_ofmt_cb },
	{ "BDF", 8, PCIEADM_SDO_BDF, pcieadm_show_devs_ofmt_cb },
	{ "DRIVER", 15, PCIEADM_SDO_DRIVER, pcieadm_show_devs_ofmt_cb },
	{ "INSTANCE", 15, PCIEADM_SDO_INSTANCE, pcieadm_show_devs_ofmt_cb },
	{ "INSTNUM", 8, PCIEADM_SDO_INSTNUM, pcieadm_show_devs_ofmt_cb },
	{ "TYPE", 15, PCIEADM_SDO_TYPE, pcieadm_show_devs_ofmt_cb },
	{ "VENDOR", 30, PCIEADM_SDO_VENDOR, pcieadm_show_devs_ofmt_cb },
	{ "DEVICE", 30, PCIEADM_SDO_DEVICE, pcieadm_show_devs_ofmt_cb },
	{ "SUBVENDOR", 30, PCIEADM_SDO_SUBVENDOR, pcieadm_show_devs_ofmt_cb },
	{ "SUBSYSTEM", 30, PCIEADM_SDO_SUBSYSTEM, pcieadm_show_devs_ofmt_cb },
	{ "PATH", 30, PCIEADM_SDO_PATH, pcieadm_show_devs_ofmt_cb },
	{ "BUS", 4, PCIEADM_SDO_BDF_BUS, pcieadm_show_devs_ofmt_cb },
	{ "DEV", 4, PCIEADM_SDO_BDF_DEV, pcieadm_show_devs_ofmt_cb },
	{ "FUNC", 4, PCIEADM_SDO_BDF_FUNC, pcieadm_show_devs_ofmt_cb },
	{ "MAXSPEED", 10, PCIEADM_SDO_MAXSPEED, pcieadm_show_devs_ofmt_cb },
	{ "MAXWIDTH", 10, PCIEADM_SDO_MAXWIDTH, pcieadm_show_devs_ofmt_cb },
	{ "CURSPEED", 10, PCIEADM_SDO_CURSPEED, pcieadm_show_devs_ofmt_cb },
	{ "CURWIDTH", 10, PCIEADM_SDO_CURWIDTH, pcieadm_show_devs_ofmt_cb },
	{ "SUPSPEEDS", 20, PCIEADM_SDO_SUPSPEEDS, pcieadm_show_devs_ofmt_cb },
	{ "SLOTNO", 8, PCIEADM_SDO_SLOTNO, pcieadm_show_devs_ofmt_cb },
	{ "AP", 10, PCIEADM_SDO_AP, pcieadm_show_devs_ofmt_cb },
	{ "CTLAP", 10, PCIEADM_SDO_CTLAP, pcieadm_show_devs_ofmt_cb },
	{ "LOCATION", 15, PCIEADM_SDO_LOC, pcieadm_show_devs_ofmt_cb },
	{ NULL, 0, 0, NULL }
};

static boolean_t
pcieadm_show_devs_match(pcieadm_show_devs_t *psd,
    pcieadm_show_devs_ofmt_t *psdo)
{
	char dinst[128], bdf[128];
	boolean_t match = B_FALSE;

	if (psd->psd_nfilts == 0) {
		return (B_TRUE);
	}

	if (psdo->psdo_driver != NULL && psdo->psdo_instance != -1) {
		(void) snprintf(dinst, sizeof (dinst), "%s%d",
		    psdo->psdo_driver, psdo->psdo_instance);
	}
	(void) snprintf(bdf, sizeof (bdf), "%x/%x/%x", psdo->psdo_bus,
	    psdo->psdo_dev, psdo->psdo_func);

	for (uint_t i = 0; i < psd->psd_nfilts; i++) {
		const char *filt = psd->psd_filts[i];

		if (strcmp(filt, psdo->psdo_path) == 0) {
			psd->psd_used[i] = B_TRUE;
			match = B_TRUE;
			continue;
		}

		if (strcmp(filt, bdf) == 0) {
			psd->psd_used[i] = B_TRUE;
			match = B_TRUE;
			continue;
		}

		if (psdo->psdo_driver != NULL &&
		    strcmp(filt, psdo->psdo_driver) == 0) {
			psd->psd_used[i] = B_TRUE;
			match = B_TRUE;
			continue;
		}

		if (psdo->psdo_driver != NULL && psdo->psdo_instance != -1 &&
		    strcmp(filt, dinst) == 0) {
			psd->psd_used[i] = B_TRUE;
			match = B_TRUE;
			continue;
		}

		if (strncmp("/devices", filt, strlen("/devices")) == 0) {
			filt += strlen("/devices");
		}

		if (strcmp(filt, psdo->psdo_path) == 0) {
			psd->psd_used[i] = B_TRUE;
			match = B_TRUE;
			continue;
		}

		if (psdo->psdo_loc != NULL && strcmp(filt, psdo->psdo_loc) ==
		    0) {
			psd->psd_used[i] = B_TRUE;
			match = B_TRUE;
			continue;
		}
	}

	return (match);
}

/*
 * Check if there is an attachment point on this node that we know how to
 * display, which today is generally in the PCIe style where there is only a
 * single entry in the bitfield.
 */
static void
pcieadm_show_devs_ap(di_node_t node, char **outp)
{
	int *ap_names, *slot_names;

	/*
	 * The AP names property is a bitfield that indicates which entry in the
	 * slot-names. A given device can in theory support multiples lots, but
	 * PCIe is always going to be a value of 1.
	 */
	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "ap-names", &ap_names) !=
	    1 || *ap_names != 1) {
		return;
	}

	/*
	 * The slot-names property is structured with the first word as a
	 * bitfield mask and then a series of names which are guaranteed to be
	 * NULL terminated.
	 */
	int nu32 = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "slot-names",
	    &slot_names);
	if (nu32 <= 1 || slot_names[0] != 1) {
		return;
	}

	*outp = calloc(nu32 - 1, sizeof (uint32_t));
	if (*outp == NULL) {
		err(EXIT_FAILURE, "failed to allocate memory for AP name");
	}

	(void) memcpy(*outp, &slot_names[1], sizeof (uint32_t) * (nu32 - 1));
}

/*
 * Walk through the 'pcie' schema tree to try to find a matching entry. There is
 * not a header file with node names, so we hard code these here for now. We
 * start this by walking function nodes and matching them with our device via
 * the b/d/f.
 */
static int
pcieadm_show_devs_loc_pcie(topo_hdl_t *hp, tnode_t *tn, void *arg)
{
	pcieadm_show_devs_ofmt_t *oarg = arg;
	uint32_t bus, dev, func;
	char *label, *type;
	int err;

	if (strcmp(topo_node_name(tn), "function") != 0) {
		return (TOPO_WALK_NEXT);
	}

	if (topo_prop_get_uint32(tn, TOPO_PCIE_PGROUP_PCI_CFG,
	    TOPO_PCIE_PCI_BUS, &bus, &err) != 0 ||
	    topo_prop_get_uint32(tn, TOPO_PCIE_PGROUP_PCI_CFG,
	    TOPO_PCIE_PCI_DEVICE, &dev, &err) != 0 ||
	    topo_prop_get_uint32(tn, TOPO_PCIE_PGROUP_PCI_CFG,
	    TOPO_PCIE_PCI_FUNCTION, &func, &err) != 0 ||
	    bus != oarg->psdo_bus || dev != oarg->psdo_dev ||
	    func != oarg->psdo_func) {
		return (TOPO_WALK_NEXT);
	}

	/*
	 * Now that we know that we have a matching device, we have the trickier
	 * part of finding a matching label. In general, ports are labeled. From
	 * a function, we know that our parent is going to either be a
	 * root-complex or a device. If it's a device, we'll look for a
	 * port/link/port pair and see if we can find a label on the upstream
	 * port. If our parent is a root-complex, then we will see if the parent
	 * CPU has a label.
	 *
	 * However, if we're a root-port, then we need to look at our port child
	 * to see what the most useful label is. Technically a root-port is
	 * really part of the root-complex/CPU and that's the more accurate
	 * label; however, most folks are looking at this so they can figure out
	 * what this corresponds to.
	 *
	 * We'll look for labels at the different hops along the way in case
	 * things change in this regard in the future.
	 */
	if (topo_prop_get_string(tn, TOPO_PGROUP_PROTOCOL, TOPO_PROP_LABEL,
	    &label, &err) == 0) {
		oarg->psdo_loc = label;
		return (TOPO_WALK_TERMINATE);
	}

	if (topo_prop_get_string(tn, TOPO_PCIE_PGROUP_PCI, TOPO_PCIE_PCI_TYPE,
	    &type, &err) != 0) {
		return (TOPO_WALK_TERMINATE);
	}

	if (strcmp(type, "root-port") == 0) {
		tnode_t *port = topo_node_lookup(tn, "port", 0);
		if (port != NULL) {
			if (topo_prop_get_string(port, TOPO_PGROUP_PROTOCOL,
			    TOPO_PROP_LABEL, &label, &err) == 0) {
				topo_hdl_strfree(hp, type);
				oarg->psdo_loc = label;
				return (TOPO_WALK_TERMINATE);
			}
		}
	}
	topo_hdl_strfree(hp, type);

	const char *pname = topo_node_name(topo_node_parent(tn));
	const char *final;
	uint32_t fcount, count = 0;
	if (strcmp(pname, "device") == 0) {
		final = "port";
		fcount = 2;
	} else if (strcmp(pname, "root-complex") == 0) {
		final = "cpu";
		fcount = 1;
	} else {
		/*
		 * We don't know where we are so we're done.
		 */
		return (TOPO_WALK_TERMINATE);
	}

	for (tnode_t *pn = topo_node_parent(tn); pn != NULL;
	    pn = topo_node_parent(pn)) {
		if (topo_prop_get_string(pn, TOPO_PGROUP_PROTOCOL,
		    TOPO_PROP_LABEL, &label, &err) == 0) {
			oarg->psdo_loc = label;
			return (TOPO_WALK_TERMINATE);
		}

		if (strcmp(topo_node_name(pn), final) == 0) {
			count++;
			if (fcount == count) {
				break;
			}
		}
	}

	return (TOPO_WALK_TERMINATE);
}

/*
 * Walk the 'pcie' tree to find a matching function.  From there, we walk up a
 * bit of the tree to find the relevant label to use.  We prefer the pcie tree
 * to the 'hc' tree as some versions of the 'hc' tree don't call out every
 * single device.
 */
static void
pcieadm_show_devs_loc(topo_hdl_t *thp, di_node_t node,
    pcieadm_show_devs_ofmt_t *oarg)
{
	topo_walk_t *wp;
	int err;

	wp = topo_walk_init(thp, FM_FMRI_SCHEME_PCIE,
	    pcieadm_show_devs_loc_pcie, oarg, &err);
	if (wp == NULL) {
		/*
		 * It might be nice to at some point swallow this and attempt to
		 * fall back to 'hc', but we would need to get error values into
		 * a header that makes it into the proto area.
		 */
		errx(EXIT_FAILURE, "failed to initialize topology walk: %s",
		    topo_strerror(err));
	}

	err = topo_walk_step(wp, TOPO_WALK_SIBLING);
	if (err == TOPO_WALK_ERR) {
		errx(EXIT_FAILURE, "failed to perform topology walk");
	}

	topo_walk_fini(wp);
}

static int
pcieadm_show_devs_walk_cb(di_node_t node, void *arg)
{
	int nprop, *regs = NULL, *did, *vid, *mwidth, *cwidth, *rev;
	int *subvid, *subsys, *slotno;
	int64_t *mspeed, *cspeed, *sspeeds;
	char *path = NULL;
	pcieadm_show_devs_t *psd = arg;
	int ret = DI_WALK_CONTINUE;
	char venstr[64], devstr[64], subvenstr[64], subsysstr[64];
	pcieadm_show_devs_ofmt_t oarg;
	pcidb_hdl_t *pcidb = psd->psd_pia->pia_pcidb;

	bzero(&oarg, sizeof (oarg));

	path = di_devfs_path(node);
	if (path == NULL) {
		err(EXIT_FAILURE, "failed to construct devfs path for node: "
		    "%s", di_node_name(node));
	}

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &regs);
	if (nprop <= 0) {
		errx(EXIT_FAILURE, "failed to lookup regs array for %s",
		    path);
	}

	oarg.psdo_path = path;
	oarg.psdo_bus = PCI_REG_BUS_G(regs[0]);
	oarg.psdo_dev = PCI_REG_DEV_G(regs[0]);
	oarg.psdo_func = PCI_REG_FUNC_G(regs[0]);

	if (oarg.psdo_func != 0 && !psd->psd_funcs) {
		goto done;
	}

	oarg.psdo_driver = di_driver_name(node);
	oarg.psdo_instance = di_instance(node);

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "device-id", &did);
	if (nprop != 1) {
		oarg.psdo_did = -1;
	} else {
		oarg.psdo_did = (uint16_t)*did;
	}

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "vendor-id", &vid);
	if (nprop != 1) {
		oarg.psdo_vid = -1;
	} else {
		oarg.psdo_vid = (uint16_t)*vid;
	}

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "revision-id", &rev);
	if (nprop != 1) {
		oarg.psdo_rev = -1;
	} else {
		oarg.psdo_rev = (uint16_t)*rev;
	}

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "subsystem-vendor-id",
	    &subvid);
	if (nprop != 1) {
		oarg.psdo_subvid = -1;
	} else {
		oarg.psdo_subvid = (uint16_t)*subvid;
	}

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "subsystem-id",
	    &subsys);
	if (nprop != 1) {
		oarg.psdo_subsys = -1;
	} else {
		oarg.psdo_subsys = (uint16_t)*subsys;
	}

	oarg.psdo_vendor = "--";
	if (oarg.psdo_vid != -1) {
		pcidb_vendor_t *vend = pcidb_lookup_vendor(pcidb,
		    oarg.psdo_vid);
		if (vend != NULL) {
			oarg.psdo_vendor = pcidb_vendor_name(vend);
		} else {
			(void) snprintf(venstr, sizeof (venstr),
			    "Unknown vendor: 0x%x", oarg.psdo_vid);
			oarg.psdo_vendor = venstr;
		}
	}

	oarg.psdo_device = "--";
	if (oarg.psdo_vid != -1 && oarg.psdo_did != -1) {
		pcidb_device_t *dev = pcidb_lookup_device(pcidb,
		    oarg.psdo_vid, oarg.psdo_did);
		if (dev != NULL) {
			oarg.psdo_device = pcidb_device_name(dev);
		} else {
			(void) snprintf(devstr, sizeof (devstr),
			    "Unknown device: 0x%x", oarg.psdo_did);
			oarg.psdo_device = devstr;
		}
	}

	/*
	 * The pci.ids database organizes subsystems under devices. We look at
	 * the subsystem vendor separately because even if the device or other
	 * information is not known, we may still be able to figure out what it
	 * is.
	 */
	oarg.psdo_subvendor = "--";
	oarg.psdo_subsystem = "--";
	if (oarg.psdo_subvid != -1) {
		pcidb_vendor_t *vend = pcidb_lookup_vendor(pcidb,
		    oarg.psdo_subvid);
		if (vend != NULL) {
			oarg.psdo_subvendor = pcidb_vendor_name(vend);
		} else {
			(void) snprintf(subvenstr, sizeof (subvenstr),
			    "Unknown vendor: 0x%x", oarg.psdo_vid);
			oarg.psdo_subvendor = subvenstr;
		}
	}

	if (oarg.psdo_vid != -1 && oarg.psdo_did != -1 &&
	    oarg.psdo_subvid != -1 && oarg.psdo_subsys != -1) {
		pcidb_subvd_t *subvd = pcidb_lookup_subvd(pcidb, oarg.psdo_vid,
		    oarg.psdo_did, oarg.psdo_subvid, oarg.psdo_subsys);
		if (subvd != NULL) {
			oarg.psdo_subsystem = pcidb_subvd_name(subvd);
		} else {
			(void) snprintf(subsysstr, sizeof (subsysstr),
			    "Unknown subsystem: 0x%x", oarg.psdo_subsys);
			oarg.psdo_subsystem = subsysstr;
		}
	}


	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node,
	    "pcie-link-maximum-width", &mwidth);
	if (nprop != 1) {
		oarg.psdo_mwidth = -1;
	} else {
		oarg.psdo_mwidth = *mwidth;
	}

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node,
	    "pcie-link-current-width", &cwidth);
	if (nprop != 1) {
		oarg.psdo_cwidth = -1;
	} else {
		oarg.psdo_cwidth = *cwidth;
	}

	nprop = di_prop_lookup_int64(DDI_DEV_T_ANY, node,
	    "pcie-link-maximum-speed", &mspeed);
	if (nprop != 1) {
		oarg.psdo_mspeed = -1;
	} else {
		oarg.psdo_mspeed = *mspeed;
	}

	nprop = di_prop_lookup_int64(DDI_DEV_T_ANY, node,
	    "pcie-link-current-speed", &cspeed);
	if (nprop != 1) {
		oarg.psdo_cspeed = -1;
	} else {
		oarg.psdo_cspeed = *cspeed;
	}

	nprop = di_prop_lookup_int64(DDI_DEV_T_ANY, node,
	    "pcie-link-supported-speeds", &sspeeds);
	if (nprop > 0) {
		oarg.psdo_nspeeds = nprop;
		oarg.psdo_sspeeds = sspeeds;
	} else {
		oarg.psdo_nspeeds = 0;
		oarg.psdo_sspeeds = NULL;
	}

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node,
	    "physical-slot#", &slotno);
	if (nprop != 1) {
		oarg.psdo_slotno = -1;
	} else {
		oarg.psdo_slotno = *slotno;
	}

	/*
	 * Look for the AP and if the parent is an instance of pcieb, check it
	 * for the controlling AP.
	 */
	pcieadm_show_devs_ap(node, &oarg.psdo_ap);
	di_node_t pdip = di_parent_node(node);
	const char *pdrv = di_driver_name(pdip);
	if (pdrv != NULL && strcmp(pdrv, "pcieb") == 0) {
		pcieadm_show_devs_ap(pdip, &oarg.psdo_ctlap);
	}

	/*
	 * If we have a topo handle, e.g. -L was passed, look to see if we can
	 * cons up a label for this.
	 */
	if (psd->psd_topo != NULL) {
		pcieadm_show_devs_loc(psd->psd_topo, node, &oarg);
	}

	if (pcieadm_show_devs_match(psd, &oarg)) {
		ofmt_print(psd->psd_ofmt, &oarg);
		psd->psd_nprint++;
	}

done:
	if (psd->psd_topo != NULL) {
		topo_hdl_strfree(psd->psd_topo, oarg.psdo_loc);
	}
	free(oarg.psdo_ctlap);
	free(oarg.psdo_ap);
	if (path != NULL) {
		di_devfs_path_free(path);
	}

	return (ret);
}

void
pcieadm_show_devs_usage(FILE *f)
{
	(void) fprintf(f, "\tshow-devs\t[-F] [-H] [-s | -o field[,...] [-p]] "
	    "[filter...]\n");
}

static void
pcieadm_show_devs_help(const char *fmt, ...)
{
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vwarnx(fmt, ap);
		va_end(ap);
		(void) fprintf(stderr, "\n");
	}

	(void) fprintf(stderr, "Usage:  %s show-devs [-F] [-H] [-L | -s ] [-o "
	    "field[,...] [-p]] [filter...]\n", pcieadm_progname);

	(void) fprintf(stderr, "\nList PCI devices and functions in the "
	    "system. Each <filter> selects a set\nof devices to show and "
	    "can be a driver name, instance, /devices path,\nb/d/f, or "
	    "location string (-L only).\n\n"
	    "\t-F\t\tdo not display PCI functions\n"
	    "\t-H\t\tomit the column header\n"
	    "\t-o field\toutput fields to print\n"
	    "\t-p\t\tparsable output (requires -o)\n"
	    "\t-s\t\tlist speeds and widths\n\n"
	    "The following fields are supported:\n"
	    "\tvid\t\tthe PCI vendor ID in hex\n"
	    "\tdid\t\tthe PCI device ID in hex\n"
	    "\trev\t\tthe PCI device revision in hex\n"
	    "\tsubvid\t\tthe PCI subsystem vendor ID in hex\n"
	    "\tsubsys\t\tthe PCI subsystem ID in hex\n"
	    "\tvendor\t\tthe name of the PCI vendor\n"
	    "\tdevice\t\tthe name of the PCI device\n"
	    "\tsubvendor\tthe name of the PCI subsystem vendor\n"
	    "\tsubsystem\tthe name of the PCI subsystem\n"
	    "\tinstance\tthe name of this particular instance, e.g. igb0\n"
	    "\tdriver\t\tthe name of the driver attached to the device\n"
	    "\tinstnum\t\tthe instance number of a device, e.g. 2 for nvme2\n"
	    "\tpath\t\tthe /devices path of the device\n"
	    "\tbdf\t\tthe PCI bus/device/function, with values in hex\n"
	    "\tbus\t\tthe PCI bus number of the device in hex\n"
	    "\tdev\t\tthe PCI device number of the device in hex\n"
	    "\tfunc\t\tthe PCI function number of the device in hex\n"
	    "\ttype\t\ta string describing the PCIe generation and width\n"
	    "\tmaxspeed\tthe maximum supported PCIe speed of the device\n"
	    "\tcurspeed\tthe current PCIe speed of the device\n"
	    "\tmaxwidth\tthe maximum supported PCIe lane count of the device\n"
	    "\tcurwidth\tthe current lane count of the PCIe device\n"
	    "\tsupspeeds\tthe list of speeds the device supports\n"
	    "\tslotno\t\tthe device's slot number\n"
	    "\tap\t\tthe attachment point provided by device\n"
	    "\tctlap\t\tthe controlling attachment point\n\n"
	    "The following fields are additionally available with -L:\n"
	    "\tlocation\tthe device's topology location string\n");
}

int
pcieadm_show_devs(pcieadm_t *pcip, int argc, char *argv[])
{
	int c, ret;
	uint_t flags = 0;
	const char *fields = NULL;
	pcieadm_show_devs_t psd;
	pcieadm_di_walk_t walk;
	ofmt_status_t oferr;
	const ofmt_field_t *ofmt;
	boolean_t parse = B_FALSE;
	boolean_t speeds = B_FALSE;
	boolean_t loc = B_FALSE;

	bzero(&psd, sizeof (psd));
	psd.psd_pia = pcip;
	psd.psd_funcs = B_TRUE;

	while ((c = getopt(argc, argv, ":FHLo:ps")) != -1) {
		switch (c) {
		case 'F':
			psd.psd_funcs = B_FALSE;
			break;
		case 'H':
			flags |= OFMT_NOHEADER;
			break;
		case 'L':
			loc = B_TRUE;
			break;
		case 'o':
			fields = optarg;
			break;
		case 'p':
			parse = B_TRUE;
			flags |= OFMT_PARSABLE;
			break;
		case 's':
			speeds = B_TRUE;
			break;
		case ':':
			pcieadm_show_devs_help("option -%c requires an "
			    "argument", optopt);
			exit(EXIT_USAGE);
		case '?':
			pcieadm_show_devs_help("unknown option: -%c", optopt);
			exit(EXIT_USAGE);
		}
	}

	if (speeds && loc) {
		errx(EXIT_USAGE, "-L and -s cannot both be used");
	}

	if (parse && fields == NULL) {
		errx(EXIT_USAGE, "-p requires fields specified with -o");
	}

	if (fields != NULL && speeds) {
		errx(EXIT_USAGE, "-s cannot be used with with -o");
	}

	if (fields == NULL) {
		if (speeds) {
			fields = pcieadm_show_dev_speeds;
		} else if (loc) {
			fields = pcieadm_show_dev_loc;
		} else {
			fields = pcieadm_show_dev_fields;
		}
	}

	if (loc) {
		ofmt = pcieadm_show_dev_loc_ofmt;
	} else {
		ofmt = pcieadm_show_dev_ofmt;
	}

	argc -= optind;
	argv += optind;

	if (argc > 0) {
		psd.psd_nfilts = argc;
		psd.psd_filts = argv;
		psd.psd_used = calloc(argc, sizeof (boolean_t));
		if (psd.psd_used == NULL) {
			err(EXIT_FAILURE, "failed to allocate filter tracking "
			    "memory");
		}
	}

	oferr = ofmt_open(fields, ofmt, flags, 0, &psd.psd_ofmt);
	ofmt_check(oferr, parse, psd.psd_ofmt, pcieadm_ofmt_errx, warnx);

	if (loc) {
		/*
		 * This helps with taking a topo snapshot reliably.
		 */
		priv_fillset(pcip->pia_priv_eff);
	}
	pcieadm_init_privs(pcip);

	/*
	 * Before we perform our walk, if we have requested location information
	 * raise privileges to take the snapshot.
	 */
	if (loc) {
		int terr;

		if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcip->pia_priv_eff) !=
		    0) {
			err(EXIT_FAILURE, "failed to raise privileges");
		}

		psd.psd_topo = topo_open(TOPO_VERSION, NULL, &terr);
		if (psd.psd_topo == NULL) {
			errx(EXIT_FAILURE, "failed to obtain topo handle: %s",
			    topo_strerror(terr));
		}

		(void) topo_snap_hold(psd.psd_topo, NULL, &terr);
		if (terr != 0) {
			errx(EXIT_FAILURE, "failed to obtain topology "
			    "snapshot: %s", topo_strerror(terr));
		}

		if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pcip->pia_priv_min) !=
		    0) {
			err(EXIT_FAILURE, "failed to reduce privileges");
		}
	}

	walk.pdw_arg = &psd;
	walk.pdw_func = pcieadm_show_devs_walk_cb;

	pcieadm_di_walk(pcip, &walk);

	ret = EXIT_SUCCESS;
	for (int i = 0; i < psd.psd_nfilts; i++) {
		if (!psd.psd_used[i]) {
			warnx("filter '%s' did not match any devices",
			    psd.psd_filts[i]);
			ret = EXIT_FAILURE;
		}
	}

	if (psd.psd_nprint == 0) {
		ret = EXIT_FAILURE;
	}

	if (psd.psd_topo != NULL) {
		topo_snap_release(psd.psd_topo);
		topo_close(psd.psd_topo);
	}

	return (ret);
}