|
root / base / usr / src / common / nvme
nvme Plain Text 2344 lines 73.0 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
/*
 * 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 _NVME_COMMON_H
#define	_NVME_COMMON_H

/*
 * Collection of common files and utilities that can be used for NVMe related
 * functionality. Broadly, these are meant so that the kernel and userland have
 * consistent validation routines.
 *
 * When we perform error checking and validation we use the kernel's set of
 * ioctl errors for more semantic errors. These semantic errors are translated
 * into ones that the library wishes to expose. Our goal is to try to use a
 * mostly uniform error checking framework between the two entities.
 *
 * A consumer must build nvme_version.o and nvme_field.o. Other pieces can be
 * added based on their needs.
 */

#include <sys/stdbool.h>
#include <sys/nvme.h>
#include <sys/nvme/discovery.h>

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Version related pieces from nvme_version.c. The main idea is that consumers
 * such as the kernel and libnvme will wrap up the nvme_vers_atleast() function
 * with an object that contains an NVMe version, thus reducing the likelihood
 * that we'll confuse versions.
 */
extern const nvme_version_t nvme_vers_1v0;
extern const nvme_version_t nvme_vers_1v1;
extern const nvme_version_t nvme_vers_1v2;
extern const nvme_version_t nvme_vers_1v3;
extern const nvme_version_t nvme_vers_1v4;
extern const nvme_version_t nvme_vers_2v0;
extern const nvme_version_t nvme_vers_2v1;

extern bool nvme_vers_atleast(const nvme_version_t *, const nvme_version_t *);

/*
 * This structure contains information about the controller that must be
 * supplied to the various validation functions.
 */
typedef struct nvme_valid_ctrl_data {
	const nvme_version_t *vcd_vers;
	const nvme_identify_ctrl_t *vcd_id;
} nvme_valid_ctrl_data_t;

/*
 * This structure is used to represent a field that is in use in a given
 * command. This allows us to use common validation logic for different classes
 * of commands such as IDENTIFY, GET LOG PAGE, etc. If everything is fine about
 * a field, then it should return true. Otherwise, it should return false and
 * fill out the error message. It is optional to override the specifics of the
 * nvme_ioctl_err_t with a more specific error where appropriate and known. If
 * it is not filled in, the validation default will be used.
 */
struct nvme_field_info;
typedef bool (*nvme_field_sup_f)(const struct nvme_field_info *,
    const nvme_valid_ctrl_data_t *, char *, size_t);
typedef bool (*nvme_field_valid_f)(const struct nvme_field_info *,
    const nvme_valid_ctrl_data_t *, uint64_t, char *, size_t);

typedef struct nvme_field_info {
	const nvme_version_t *nlfi_vers;
	nvme_field_sup_f nlfi_sup;
	uint64_t nlfi_max_size;
	nvme_field_valid_f nlfi_valid;
	/*
	 * Fields below this point are mostly meant to be used by libnvme and by
	 * our printing logic, which we assume is not executed in the kernel.
	 */
	const char *nlfi_spec;
	const char *nlfi_human;
	bool nlfi_def_req;
	bool nlfi_def_allow;
} nvme_field_info_t;

typedef enum {
	NVME_FIELD_ERR_OK = 0,
	NVME_FIELD_ERR_UNSUP_VERSION,
	NVME_FIELD_ERR_UNSUP_FIELD,
	NVME_FIELD_ERR_BAD_VALUE
} nvme_field_error_t;

extern nvme_field_error_t nvme_field_validate(const nvme_field_info_t *,
    const nvme_valid_ctrl_data_t *, uint64_t, char *, size_t);

/*
 * Various common utility routines for field validation and implementation. This
 * version of NSID checking treats the NSID as valid. Currently checking for the
 * validity of the broadcast namespace ID is left to consumers.
 */
extern bool nvme_field_atleast(const nvme_valid_ctrl_data_t *,
    const nvme_version_t *);
extern bool nvme_field_valid_nsid(const nvme_field_info_t *,
    const nvme_valid_ctrl_data_t *, uint64_t, char *, size_t);
extern bool nvme_field_range_check(const nvme_field_info_t *, uint64_t,
    uint64_t, char *, size_t, uint64_t);
extern bool nvme_field_mask_check(const nvme_field_info_t *, uint64_t, char *,
    size_t, uint64_t);

/*
 * Log page request information. The goal with these structures and fields is to
 * be able to validate whether something is valid, both in user/kernel context.
 * This phrasing also makes this much easier to unit test. Because information
 * is shared between libnvme and the kernel, some things are not needed for the
 * kernel. We do not ifdef it out for the moment, to simplify things.
 */

/*
 * This is the set of fields that the driver knows about how to validate that
 * can end up in an NVMe log request. Items should be added here once the kernel
 * knows how to put them in a log request command.
 */
typedef enum {
	NVME_LOG_REQ_FIELD_LID	= 0,
	NVME_LOG_REQ_FIELD_LSP,
	NVME_LOG_REQ_FIELD_LSI,
	NVME_LOG_REQ_FIELD_SIZE,
	NVME_LOG_REQ_FIELD_CSI,
	NVME_LOG_REQ_FIELD_RAE,
	NVME_LOG_REQ_FIELD_OFFSET,
	NVME_LOG_REQ_FIELD_NSID
} nvme_log_req_field_t;

extern const nvme_field_info_t nvme_log_fields[];
extern const size_t nvme_log_nfields;

/*
 * We now use the field based information to have a common structure to define
 * information about standard log pages.
 */
typedef struct nvme_log_page_info nvme_log_page_info_t;
typedef bool (*nvme_log_page_sup_f)(const nvme_valid_ctrl_data_t *,
    const nvme_log_page_info_t *);
typedef uint64_t (*nvme_log_page_len_f)(const nvme_valid_ctrl_data_t *,
    const nvme_log_page_info_t *);
typedef nvme_log_disc_scope_t (*nvme_log_page_scope_f)(
    const nvme_valid_ctrl_data_t *, const nvme_log_page_info_t *);
typedef bool (*nvme_log_page_var_len_f)(uint64_t *, const void *, size_t);

struct nvme_log_page_info {
	const char *nlpi_short;
	const char *nlpi_human;
	const char *const *nlpi_aliases;
	size_t nlpi_naliases;
	uint32_t nlpi_lid;
	nvme_csi_t nlpi_csi;
	/*
	 * These two entries can be used to determine whether a log page is
	 * supported based upon its version or with a supplemental function. A
	 * NULL item means it doesn't need to be checked. This would be the case
	 * for vendor-specific logs.
	 */
	const nvme_version_t *nlpi_vers;
	const nvme_log_page_sup_f nlpi_sup_func;
	nvme_log_disc_kind_t nlpi_kind;
	nvme_log_disc_source_t nlpi_source;
	nvme_log_disc_fields_t nlpi_disc;
	/*
	 * Log pages are valid in certain contexts. This is generally static
	 * information, but if the scope function is implemented, we will use
	 * that and ignore the contents of nlpi_scope.
	 */
	nvme_log_disc_scope_t nlpi_scope;
	nvme_log_page_scope_f nlpi_scope_func;
	/*
	 * The lengths for a log page come in three forms. The first form is
	 * ones where we can determine based on information in the controller
	 * (or at build time) the length of the log page. Many log pages have a
	 * fixed length or they include information in the identify controller
	 * data structure as to their length (e.g. the error log page). To
	 * communicate the log page's length, we will first check if
	 * nlpi_len_func is non-NULL and call that to determine the log page
	 * length. Otherwise we will use the value in nlpi_len. If these return
	 * a non-zero value, the NVME_LOG_DISC_F_SIZE_FIXED will be set
	 * automatically.
	 *
	 * The second form of log pages are those whose length is variable, but
	 * we cannot determine it based on information present in the
	 * controller. Rather we must read some amount of data from the log page
	 * to figure this out at all. For example, many vendor specific logs
	 * have a first uint32_t that indicates the number of valid samples and
	 * therefore you must read that to determine the overall length of the
	 * log page. This case follows the same path as the first case; however,
	 * one must also set the nlpi_var_func function pointer. This results
	 * in the NVME_LOG_DISC_F_SIZE_VAR flag being set.
	 *
	 * The third set of these are ones we just don't know about. In this
	 * case, leave nlpi_len set to zero and nlpi_len_func to NULL. If this
	 * happens or neither path returns a valid size (i.e. 0) then we will
	 * set this to a general size that should be large enough (i.e. the
	 * non-extended NVMe log page size) and not set either size flag.
	 */
	uint64_t nlpi_len;
	nvme_log_page_len_f nlpi_len_func;
	nvme_log_page_var_len_f nlpi_var_func;
};

extern const nvme_log_page_info_t nvme_std_log_pages[];
extern const size_t nvme_std_log_npages;

/*
 * These are functions that can be used to compute information about what's
 * supported and similar information that sometimes requires dynamic support.
 */
extern nvme_log_disc_scope_t nvme_log_page_info_scope(
    const nvme_log_page_info_t *, const nvme_valid_ctrl_data_t *);
extern uint64_t nvme_log_page_info_size(const nvme_log_page_info_t *,
    const nvme_valid_ctrl_data_t *, bool *);
extern bool nvme_log_page_info_supported(const nvme_log_page_info_t *,
    const nvme_valid_ctrl_data_t *);

/*
 * This next section identifies the various fields that make up the NVMe
 * IDENTIFY command and the corresponding pieces that are in use throughout.
 */
typedef enum {
	NVME_ID_REQ_F_CNS = 0,
	NVME_ID_REQ_F_NSID,
	NVME_ID_REQ_F_CTRLID,
	NVME_ID_REQ_F_BUF,
} nvme_identify_req_field_t;

typedef enum {
	/*
	 * Indicates that we allow this identify command to operate on a
	 * namespace minor.
	 */
	NVME_IDENTIFY_INFO_F_NS_OK		= 1 << 0,
	/*
	 * Indicates that if we support namespace management we should attempt
	 * to use the broadcast nsid when asking about the controller.
	 */
	NVME_IDENTIFY_INFO_F_BCAST		= 1 << 1,
	/*
	 * This indicates that we are performing an operation which lists
	 * namespace IDs. As such, we don't need to validate the namespace
	 * against the controller's list. In addition, a zero namespace ID is
	 * allowed.
	 */
	NVME_IDENTIFY_INFO_F_NSID_LIST		= 1 << 2
} nvme_identify_info_flags_t;

typedef struct nvme_identify_info nvme_identify_info_t;
typedef bool (*nvme_identify_sup_f)(const nvme_valid_ctrl_data_t *);
struct nvme_identify_info {
	const char			*nii_name;
	nvme_csi_t			nii_csi;
	uint32_t			nii_cns;
	const nvme_version_t		*nii_vers;
	nvme_identify_sup_f		nii_sup_func;
	nvme_identify_req_field_t	nii_fields;
	nvme_identify_info_flags_t	nii_flags;
};

extern const nvme_field_info_t nvme_identify_fields[];
extern const size_t nvme_identify_nfields;
extern const nvme_identify_info_t nvme_identify_cmds[];
extern const size_t nvme_identify_ncmds;

extern bool nvme_identify_info_supported(const nvme_identify_info_t *,
    const nvme_valid_ctrl_data_t *);

/*
 * NVMe Vendor Unique Commands. Note, unlike others this hasn't really changed
 * since it was introduced in NVMe 1.0. While libnvme wraps these up a bit to
 * construct commands, there is no common vendor unique command discovery
 * information as the kernel more or less stays out of it.
 */
typedef enum {
	NVME_VUC_REQ_FIELD_OPC = 0,
	NVME_VUC_REQ_FIELD_NSID,
	NVME_VUC_REQ_FIELD_CDW12,
	NVME_VUC_REQ_FIELD_CDW13,
	NVME_VUC_REQ_FIELD_CDW14,
	NVME_VUC_REQ_FIELD_CDW15,
	NVME_VUC_REQ_FIELD_NDT,
	/*
	 * While the timeout field here is not actually part of the standard, we
	 * require it as part of the command execution and therefore include it
	 * in here.
	 */
	NVME_VUC_REQ_FIELD_TO
} nvme_vuc_req_field_t;

extern const nvme_field_info_t nvme_vuc_fields[];
extern const size_t nvme_vuc_nfields;

/*
 * Firmware download and commit related fields and routines.
 */
typedef enum {
	NVME_FW_LOAD_REQ_FIELD_NUMD = 0,
	NVME_FW_LOAD_REQ_FIELD_OFFSET
} nvme_fw_load_req_field_t;

extern const nvme_field_info_t nvme_fw_load_fields[];
extern const size_t nvme_fw_load_nfields;

extern bool nvme_fw_cmds_supported(const nvme_valid_ctrl_data_t *);
extern uint32_t nvme_fw_load_granularity(const nvme_valid_ctrl_data_t *);

typedef enum {
	NVME_FW_COMMIT_REQ_FIELD_SLOT = 0,
	NVME_FW_COMMIT_REQ_FIELD_ACT
} nvme_fw_commit_req_field_t;

extern const nvme_field_info_t nvme_fw_commit_fields[];
extern const size_t nvme_fw_commit_nfields;

/*
 * Format NVM operations
 */
typedef enum {
	NVME_FORMAT_REQ_FIELD_LBAF	= 0,
	NVME_FORMAT_REQ_FIELD_SES,
	NVME_FORMAT_REQ_FIELD_NSID
} nvme_format_req_field_t;

extern const nvme_field_info_t nvme_format_fields[];
extern const size_t nvme_format_nfields;

extern bool nvme_format_cmds_supported(const nvme_valid_ctrl_data_t *);

/*
 * Feature related requests
 */
typedef enum {
	NVME_GET_FEAT_REQ_FIELD_FID		= 0,
	NVME_GET_FEAT_REQ_FIELD_SEL,
	NVME_GET_FEAT_REQ_FIELD_DPTR,
	NVME_GET_FEAT_REQ_FIELD_CDW11,
	NVME_GET_FEAT_REQ_FIELD_NSID
} nvme_get_feat_req_field_t;

extern const nvme_field_info_t nvme_get_feat_fields[];
extern const size_t nvme_get_feat_nfields;

/*
 * Common feature information.
 */
typedef struct nvme_feat_info nvme_feat_info_t;
typedef bool (*nvme_feat_sup_f)(const nvme_valid_ctrl_data_t *,
    const nvme_feat_info_t *);

struct nvme_feat_info {
	const char *nfeat_short;
	const char *nfeat_spec;
	uint32_t nfeat_fid;
	/*
	 * These three entries can be used to determine whether a feature is
	 * supported or not based upon its version or supplemental information.
	 */
	const nvme_version_t *nfeat_vers;
	const nvme_feat_sup_f nfeat_sup_func;
	nvme_feat_kind_t nfeat_kind;
	/*
	 * These describe whether the feature operates on namespaces or the
	 * controller and misc. flags and information about them.
	 */
	nvme_feat_scope_t nfeat_scope;
	nvme_feat_csi_t nfeat_csi;
	nvme_feat_flags_t nfeat_flags;
	/*
	 * These four entries describe what an NVMe device uses as input and
	 * output fields.
	 */
	nvme_get_feat_fields_t nfeat_in_get;
	nvme_set_feat_fields_t nfeat_in_set;
	nvme_feat_output_t nfeat_out_get;
	nvme_feat_output_t nfeat_out_set;
	/*
	 * Feature data size. This should be zero if the feature does not use a
	 * data payload. Right now we assume the get and set sizes are identical
	 * as that's how this normally works.
	 */
	uint64_t nfeat_len;
};

extern const nvme_feat_info_t nvme_std_feats[];
extern const size_t nvme_std_nfeats;

extern nvme_feat_impl_t nvme_feat_supported(const nvme_feat_info_t *,
    const nvme_valid_ctrl_data_t *);

/*
 * Namespace Management and Namespace Attach Commands.
 *
 * These operations have their own sets of NVMe admin operations codes.
 * Separately, they then each have a means of selecting what they operate on in
 * dw10. Unlike other operations like Get Features or Get Log Page, these are
 * broken into separate ioctls in the kernel. In libnvme, namespace attach has a
 * single command, but namespace create and delete are treated separately.
 */

typedef enum {
	NVME_NS_CREATE_REQ_FIELD_CSI = 0,
	NVME_NS_CREATE_REQ_FIELD_NSZE,
	NVME_NS_CREATE_REQ_FIELD_NCAP,
	NVME_NS_CREATE_REQ_FIELD_FLBAS,
	NVME_NS_CREATE_REQ_FIELD_NMIC
} nvme_ns_create_req_field_t;

typedef enum {
	NVME_NS_DELETE_REQ_FIELD_NSID = 0
} nvme_ns_delete_req_field_t;

/*
 * Strictly speaking some of these fields, such as the controller list, are
 * specific to the type of sub-command put into the SEL field.
 */
typedef enum {
	NVME_NS_ATTACH_REQ_FIELD_SEL = 0,
	NVME_NS_ATTACH_REQ_FIELD_NSID,
	NVME_NS_ATTACH_REQ_FIELD_DPTR
} nvme_ns_attach_req_field_t;

extern bool nvme_nsmgmt_cmds_supported(const nvme_valid_ctrl_data_t *);
extern const nvme_field_info_t nvme_ns_attach_fields[];
extern const size_t nvme_ns_attach_nfields;
extern const nvme_field_info_t nvme_ns_create_fields[];
extern const size_t nvme_ns_create_nfields;
extern const nvme_field_info_t nvme_ns_delete_fields[];
extern const size_t nvme_ns_delete_nfields;

/*
 * Allowed and required fields by CSI.
 */
extern const nvme_ns_create_req_field_t nvme_ns_create_fields_nvm_req[];
extern const size_t nvme_ns_create_fields_nvm_nreq;
extern const nvme_ns_create_req_field_t nvme_ns_create_fields_nvm_allow[];
extern const size_t nvme_ns_create_fields_nvm_nallow;

#ifdef __cplusplus
}
#endif

#endif /* _NVME_COMMON_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 2025 Oxide Computer Company
 */

/*
 * This file deals with all the knowledge related to supported, standard NVMe
 * features as well as validation of other requests related to features. While
 * there are vendor-specific features, we currently don't support issuing them
 * to the kernel.
 *
 * Like other parts of the common NVMe logic, we have two different sets of data
 * tables to help us drive validation:
 *
 * 1) We have a list of fields that are supported in the kernel ioctl interface
 * and libnvme for features. There are some fields like allowing a specification
 * via UUID which are not currently supported. The field tables are split up
 * among get and set features because they are somewhat different in terms of
 * what they allow (i.e. set features may use cdw12, cdw13, cdw15, etc.) and
 * because the kernel doesn't support issuing set features from userland today.
 *
 * 2) We have a table of NVMe specified required and optional features. This
 * table has dynamic properties related to whether things are supported and the
 * set of fields that are usable because some aspects of this change with the
 * specification version (e.g. the temperature threshold feature had no input
 * argument in cdw11 in NVMe 1.0).
 */

#include "nvme_common.h"

#include <sys/sysmacros.h>
#ifdef	_KERNEL
#include <sys/sunddi.h>
#include <sys/stdint.h>
#else
#include <stdio.h>
#include <inttypes.h>
#endif

static bool
nvme_get_feat_supported_sel(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, char *msg, size_t msglen)
{
	if (data->vcd_id->id_oncs.on_save != 0) {
		return (true);
	}

	(void) snprintf(msg, msglen, "controller does not support field %s "
	    "(%s): missing extended data support in Log Page Attributes (LPA)",
	    field->nlfi_human, field->nlfi_spec);
	return (false);
}

/*
 * An astute observer will note that there is no instance for the DPTR here.
 * While a buffer is required for this command, the common code does not
 * validate buffers. In other pieces we use a length as a proxy for checking the
 * buffer; however, there is no length argument here. The buffer is expected by
 * the controller to be of sufficient size. This is validated by the kernel in
 * nvme_validate_get_feature().
 */
const nvme_field_info_t nvme_get_feat_fields[] = {
	[NVME_GET_FEAT_REQ_FIELD_FID] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_max_size = NVME_FEAT_MAX_FID,
		.nlfi_spec = "fid",
		.nlfi_human = "feature identifier",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	},
	[NVME_GET_FEAT_REQ_FIELD_SEL] = {
		.nlfi_vers = &nvme_vers_1v1,
		.nlfi_sup = nvme_get_feat_supported_sel,
		.nlfi_max_size = NVME_FEAT_MAX_SEL,
		.nlfi_spec = "sel",
		.nlfi_human = "select",
		/*
		 * Because this field was introduced in NVMe 1.1 and because
		 * most of the time we want to assume folks are looking for the
		 * current value, we end up opting to make this a non-required
		 * field and default to getting the current value.
		 */
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_GET_FEAT_REQ_FIELD_CDW11] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_max_size = UINT32_MAX,
		.nlfi_spec = "cdw11",
		.nlfi_human = "control dword 11",
		/*
		 * While this isn't required by default, we will end up setting
		 * it as required based on the specifics of the feature and its
		 * version.
		 */
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_GET_FEAT_REQ_FIELD_NSID] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_field_valid_nsid,
		.nlfi_spec = "nsid",
		.nlfi_human = "namespace ID",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	}
};

const size_t nvme_get_feat_nfields = ARRAY_SIZE(nvme_get_feat_fields);

static bool
nvme_feat_write_cache_sup(const nvme_valid_ctrl_data_t *data,
    const nvme_feat_info_t *feat)
{
	return (data->vcd_id->id_vwc.vwc_present != 0);
}

static bool
nvme_feat_apst_sup(const nvme_valid_ctrl_data_t *data,
    const nvme_feat_info_t *feat)
{
	return (data->vcd_id->id_apsta.ap_sup != 0);
}

/*
 * Note, many of these short names come from the history of nvmeadm(8). If you
 * wish to change them, then you must figure out a way to make sure we can still
 * honor the original names. Most fields here try to use a value of 0 as
 * reasonable default so if something's not specified we'll get a reasonable
 * value. For example, NVME_FEAT_MANDATORY, NVME_FEAT_CSI_NONE, etc. all have a
 * value of zero so when that field isn't present we get something reasonable.
 * This leads us to generally define fields that are exceptions to the norm
 * (e.g. when a feature is specific to the NVM feature set).
 */
const nvme_feat_info_t nvme_std_feats[] = { {
	.nfeat_short = "arb",
	.nfeat_spec = "Arbitration",
	.nfeat_fid = NVME_FEAT_ARBITRATION,
	.nfeat_vers = &nvme_vers_1v0,
	.nfeat_kind = NVME_FEAT_MANDATORY,
	.nfeat_scope = NVME_FEAT_SCOPE_CTRL,
	.nfeat_in_set = NVME_SET_FEAT_F_CDW11,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0
}, {
	.nfeat_short = "pm",
	.nfeat_spec = "Power Management",
	.nfeat_fid = NVME_FEAT_POWER_MGMT,
	.nfeat_vers = &nvme_vers_1v0,
	.nfeat_kind = NVME_FEAT_MANDATORY,
	.nfeat_scope = NVME_FEAT_SCOPE_CTRL,
	.nfeat_in_set = NVME_SET_FEAT_F_CDW11,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0
}, {
	.nfeat_short = "range",
	.nfeat_spec = "LBA Range Type",
	.nfeat_fid = NVME_FEAT_LBA_RANGE,
	.nfeat_vers = &nvme_vers_1v0,
	.nfeat_kind = NVME_FEAT_OPTIONAL,
	.nfeat_scope = NVME_FEAT_SCOPE_NS,
	.nfeat_csi = NVME_FEAT_CSI_NVM,
	.nfeat_in_get = NVME_GET_FEAT_F_NSID | NVME_GET_FEAT_F_DATA,
	.nfeat_in_set = NVME_SET_FEAT_F_NSID | NVME_SET_FEAT_F_CDW11 |
	    NVME_SET_FEAT_F_DATA,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0 | NVME_FEAT_OUTPUT_DATA,
	.nfeat_len = NVME_LBA_RANGE_BUFSIZE
}, {
	.nfeat_short = "temp",
	.nfeat_spec = "Temperature Threshold",
	.nfeat_fid = NVME_FEAT_TEMPERATURE,
	.nfeat_vers = &nvme_vers_1v0,
	.nfeat_kind = NVME_FEAT_MANDATORY,
	.nfeat_scope = NVME_FEAT_SCOPE_CTRL,
	/*
	 * In NVMe 1.0 and NVMe 1.1, there was only a single temperature sensor
	 * that the spec defined and was present in the threshold feature.
	 * However, starting in NVMe 1.2, this was changed so that a sensor was
	 * required to be specified in NVMe 1.2 to identify the sensor. As such
	 * we always end up saying that this is required.
	 */
	.nfeat_in_get = NVME_GET_FEAT_F_CDW11,
	.nfeat_in_set = NVME_SET_FEAT_F_CDW11,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0
}, {
	.nfeat_short = "errrec",
	.nfeat_spec = "Error Recovery",
	.nfeat_fid = NVME_FEAT_ERROR,
	.nfeat_vers = &nvme_vers_1v0,
	.nfeat_csi = NVME_FEAT_CSI_NVM,
	.nfeat_kind = NVME_FEAT_MANDATORY,
	/*
	 * The scope of this feature has a bit of a complicated history.
	 * Originally we always got this on the controller and that works for
	 * most NVMe 1.0-1.2 devices. The introduction of both namespace
	 * management and of the DULBE option which is namespace specific, made
	 * this more nuanced. The NVMe 1.4 specification makes it clear that
	 * this is namespace specific; however, if we ask for this feature on
	 * many NVMe 1.3 devices with namespace support and some NVMe 1.2, it'll
	 * generate an error about missing namespace information. Unfortunately
	 * namespace management is not a good proxy for this as for example the
	 * Samsung 980 Pro is an NVMe 1.3 device without namespace management
	 * and it will error with invalid namespace if we specify zeros.
	 *
	 * However, most devices that we've surveyed will always answer a GET
	 * FEATURES request with a namespace specified. Therefore, given the
	 * changes that have happened, for now we're going to phrase it scoped
	 * to a namespace and requiring a namespace ID.
	 */
	.nfeat_scope = NVME_FEAT_SCOPE_NS,
	.nfeat_in_get = NVME_GET_FEAT_F_NSID,
	.nfeat_in_set = NVME_SET_FEAT_F_NSID | NVME_SET_FEAT_F_CDW11,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0
}, {
	.nfeat_short = "cache",
	.nfeat_spec = "Volatile Write Cache",
	.nfeat_fid = NVME_FEAT_WRITE_CACHE,
	.nfeat_sup_func = nvme_feat_write_cache_sup,
	.nfeat_vers = &nvme_vers_1v0,
	.nfeat_kind = NVME_FEAT_OPTIONAL,
	.nfeat_scope = NVME_FEAT_SCOPE_CTRL,
	.nfeat_in_set = NVME_SET_FEAT_F_CDW11,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0
}, {
	.nfeat_short = "queues",
	.nfeat_spec = "Number of Queues",
	.nfeat_fid = NVME_FEAT_NQUEUES,
	.nfeat_vers = &nvme_vers_1v0,
	.nfeat_kind = NVME_FEAT_MANDATORY,
	.nfeat_scope = NVME_FEAT_SCOPE_CTRL,
	.nfeat_in_set = NVME_SET_FEAT_F_CDW11,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0
}, {
	/*
	 * The interrupt coalescing and the interrupt vector configuration
	 * features are required for all PCIe controllers; however, they are not
	 * supported for other types of controllers. As we only support NVMe
	 * PCIe controllers with this library right now we don't do anything
	 * special to denote that. If we do, we will probably want to create an
	 * optional function for determining the kind of feature and leverage
	 * the existing nfeat_sup_func.
	 */
	.nfeat_short = "coalescing",
	.nfeat_spec = "Interrupt Coalescing",
	.nfeat_fid = NVME_FEAT_INTR_COAL,
	.nfeat_vers = &nvme_vers_1v0,
	.nfeat_kind = NVME_FEAT_MANDATORY,
	.nfeat_scope = NVME_FEAT_SCOPE_CTRL,
	.nfeat_in_set = NVME_SET_FEAT_F_CDW11,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0
}, {
	.nfeat_short = "vector",
	.nfeat_spec = "Interrupt Vector Configuration",
	.nfeat_fid = NVME_FEAT_INTR_VECT,
	.nfeat_vers = &nvme_vers_1v0,
	.nfeat_kind = NVME_FEAT_MANDATORY,
	.nfeat_scope = NVME_FEAT_SCOPE_CTRL,
	.nfeat_in_get = NVME_GET_FEAT_F_CDW11,
	.nfeat_in_set = NVME_SET_FEAT_F_CDW11,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0
}, {
	.nfeat_short = "atomicity",
	.nfeat_spec = "Write Atomicity",
	.nfeat_fid = NVME_FEAT_WRITE_ATOM,
	.nfeat_vers = &nvme_vers_1v0,
	.nfeat_kind = NVME_FEAT_MANDATORY,
	.nfeat_scope = NVME_FEAT_SCOPE_CTRL,
	.nfeat_in_set = NVME_SET_FEAT_F_CDW11,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0
}, {
	.nfeat_short = "event",
	.nfeat_spec = "Asynchronous Event Configuration",
	.nfeat_fid = NVME_FEAT_ASYNC_EVENT,
	.nfeat_vers = &nvme_vers_1v0,
	.nfeat_kind = NVME_FEAT_MANDATORY,
	.nfeat_scope = NVME_FEAT_SCOPE_CTRL,
	.nfeat_in_set = NVME_SET_FEAT_F_CDW11,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0
}, {
	.nfeat_short = "apst",
	.nfeat_spec = "Autonomous Power State Transition",
	.nfeat_fid = NVME_FEAT_AUTO_PST,
	.nfeat_vers = &nvme_vers_1v1,
	.nfeat_sup_func = nvme_feat_apst_sup,
	.nfeat_kind = NVME_FEAT_OPTIONAL,
	.nfeat_scope = NVME_FEAT_SCOPE_CTRL,
	.nfeat_in_get = NVME_GET_FEAT_F_DATA,
	.nfeat_in_set = NVME_SET_FEAT_F_CDW11 | NVME_SET_FEAT_F_DATA,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0 | NVME_FEAT_OUTPUT_DATA,
	.nfeat_len = NVME_AUTO_PST_BUFSIZE
}, {
	.nfeat_short = "progress",
	.nfeat_spec = "Software Progress Marker",
	.nfeat_fid = NVME_FEAT_PROGRESS,
	.nfeat_vers = &nvme_vers_1v0,
	.nfeat_kind = NVME_FEAT_OPTIONAL,
	.nfeat_scope = NVME_FEAT_SCOPE_CTRL,
	.nfeat_in_set = NVME_SET_FEAT_F_CDW11,
	.nfeat_out_get = NVME_FEAT_OUTPUT_CDW0
}, {
	.nfeat_short = "hostsup",
	.nfeat_spec = "Host Behavior Support",
	.nfeat_fid = NVME_FEAT_HOST_BEHAVE,
	.nfeat_vers = &nvme_vers_1v4,
	/*
	 * There is no specific way to tell if this feature is supported other
	 * than to see if all of the various behaviors that a host could enable
	 * are supported through various means.
	 */
	.nfeat_kind = NVME_FEAT_OPTIONAL,
	.nfeat_scope = NVME_FEAT_SCOPE_CTRL,
	.nfeat_in_get = NVME_GET_FEAT_F_DATA,
	.nfeat_in_set = NVME_SET_FEAT_F_DATA,
	.nfeat_out_get = NVME_FEAT_OUTPUT_DATA,
	.nfeat_len = sizeof (nvme_host_behavior_t)
}  };

const size_t nvme_std_nfeats = ARRAY_SIZE(nvme_std_feats);

/*
 * Now it's time to answer the only hard question here: is this feature actually
 * supported by the controller. Prior to NVMe 2.x and the Feature Identifiers
 * Supported and Effects page, we have to use a heuristic for this. Our
 * heuristics rules are as follows:
 *
 * 1) If this is a vendor-specific feature that we have identified is present on
 * this controller based on a datasheet, we assume it's present.
 *
 * 2) If the feature was introduced in an NVMe spec version newer than our
 * controller, then it's clearly unsupported.
 *
 * 3) If it is a mandatory feature, we have the right controller type, and we
 * are past the minimum version, then this is supported.
 *
 * 4) If the feature is optional and has an explicit feature bit that indicates
 * whether it's present or not, then we can use that to determine if it's
 * implemented or not.
 *
 * Otherwise we must conclude that we don't know.
 */
nvme_feat_impl_t
nvme_feat_supported(const nvme_feat_info_t *info,
    const nvme_valid_ctrl_data_t *data)
{
	if (info->nfeat_kind == NVME_FEAT_VENDOR_SPECIFIC) {
		return (NVME_FEAT_IMPL_SUPPORTED);
	}

	if (info->nfeat_vers != NULL &&
	    !nvme_vers_atleast(data->vcd_vers, info->nfeat_vers)) {
		return (NVME_FEAT_IMPL_UNSUPPORTED);
	}

	if (info->nfeat_kind == NVME_FEAT_MANDATORY) {
		ASSERT3P(info->nfeat_sup_func, ==, NULL);
		return (NVME_FEAT_IMPL_SUPPORTED);
	}

	if (info->nfeat_sup_func != NULL) {
		if (info->nfeat_sup_func(data, info)) {
			return (NVME_FEAT_IMPL_SUPPORTED);
		}

		return (NVME_FEAT_IMPL_UNSUPPORTED);
	}

	return (NVME_FEAT_IMPL_UNKNOWN);
}
/*
 * 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 2025 Oxide Computer Company
 */

/*
 * This file contains shared pieces of the NVMe field validation logic and has
 * shared pieces that are used between different parts.
 */

#include "nvme_common.h"

#include <sys/sysmacros.h>
#ifdef	_KERNEL
#include <sys/sunddi.h>
#include <sys/stdint.h>
#else
#include <stdio.h>
#include <inttypes.h>
#endif

bool
nvme_field_atleast(const nvme_valid_ctrl_data_t *data,
    const nvme_version_t *targ)
{
	return (nvme_vers_atleast(data->vcd_vers, targ));
}

/*
 * Note, we rely on external logic to determine if the broadcast nsid is valid.
 * We always accept it.
 */
bool
nvme_field_valid_nsid(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t nsid, char *msg, size_t msglen)
{
	if ((nsid != 0 && nsid <= data->vcd_id->id_nn) ||
	    nsid == NVME_NSID_BCAST) {
		return (true);
	}

	(void) snprintf(msg, msglen, "namespace id %" PRIu64 "is outside the "
	    "valid range [0x%x, 0x%x], the broadcast nsid (0x%x) may be valid",
	    nsid, NVME_NSID_MIN, NVME_NSID_BCAST, data->vcd_id->id_nn);
	return (false);
}

bool
nvme_field_range_check(const nvme_field_info_t *field, uint64_t min,
    uint64_t max, char *msg, size_t msglen, uint64_t value)
{
	if (value >= min && value <= max) {
		return (true);
	}

	(void) snprintf(msg, msglen, "field %s (%s) value 0x%"
	    PRIx64 " is outside the valid range: [0x%" PRIx64 ", 0x%" PRIx64
	    "]", field->nlfi_human, field->nlfi_spec, value, min, max);
	return (false);
}

bool
nvme_field_mask_check(const nvme_field_info_t *field, uint64_t valid_mask,
    char *msg, size_t msglen, uint64_t value)
{
	uint64_t inval = ~valid_mask & value;
	if (inval == 0) {
		return (true);
	}

	(void) snprintf(msg, msglen, "field %s (%s) value 0x%" PRIx64
	    " uses bits outside of the mask 0x%" PRIx64 ": 0x%" PRIx64,
	    field->nlfi_human, field->nlfi_spec, value, valid_mask, inval);
	return (false);
}

/*
 * This is a general validation function for fields that are part of a command.
 * It will check if the field is supported by the controller and if so, that its
 * value is within the expected range. On error, an optional message will be
 * written that explains the error. This is intended to be shared between
 * userland and the kernel. The kernel should pass NULL/0 for msg/msglen because
 * there is no message translation capability in the kernel.
 */
nvme_field_error_t
nvme_field_validate(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t value, char *msg,
    size_t msglen)
{
	ASSERT3P(field->nlfi_vers, !=, NULL);

	if (msglen > 0)
		*msg = '\0';

	if (!nvme_field_atleast(data, field->nlfi_vers)) {
		(void) snprintf(msg, msglen, "field %s (%s) requires "
		    "version %u.%u, but device is at %u.%u", field->nlfi_human,
		    field->nlfi_spec, data->vcd_vers->v_major,
		    data->vcd_vers->v_minor, field->nlfi_vers->v_major,
		    field->nlfi_vers->v_minor);
		return (NVME_FIELD_ERR_UNSUP_VERSION);
	}

	if (field->nlfi_sup != NULL && !field->nlfi_sup(field, data, msg,
	    msglen)) {
		(void) snprintf(msg, msglen, "field %s (%s) is not "
		    "supported by the controller", field->nlfi_human,
		    field->nlfi_spec);
		return (NVME_FIELD_ERR_UNSUP_FIELD);
	}

	if (field->nlfi_valid != NULL) {
		if (!field->nlfi_valid(field, data, value, msg, msglen)) {
			if (msglen > 0 && *msg == '\0') {
				(void) snprintf(msg, msglen,
				    "field %s (%s) value 0x%" PRIx64
				    " is invalid",
				    field->nlfi_human,
				    field->nlfi_spec, value);
			}
			return (NVME_FIELD_ERR_BAD_VALUE);
		}
	} else if (!nvme_field_range_check(field, 0, field->nlfi_max_size, msg,
	    msglen, value)) {
		return (NVME_FIELD_ERR_BAD_VALUE);
	}

	return (NVME_FIELD_ERR_OK);
}
/*
 * 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 2025 Oxide Computer Company
 */

/*
 * Common field and validation for NVMe firmware related pieces.
 */

#include "nvme_common.h"

#include <sys/sysmacros.h>
#ifdef	_KERNEL
#include <sys/sunddi.h>
#include <sys/stdint.h>
#else
#include <stdio.h>
#include <inttypes.h>
#endif

/*
 * The default granularity we enforce prior to the 1.3 spec's introduction of
 * the FWUG (firmware update granularity).
 */
#define	NVME_DEFAULT_FWUG	4096

/*
 * The FWUG is in multiples of 4 KiB.
 */
#define	NVME_FWUG_MULT	4096

/*
 * Answers the question of are firmware commands supported or not in a way
 * that is a bit easier for us to unit test.
 */
bool
nvme_fw_cmds_supported(const nvme_valid_ctrl_data_t *data)
{
	return (data->vcd_id->id_oacs.oa_firmware != 0);
}

/*
 * Validate a length/offset for an NVMe firmware download request.
 * These fields in the NVMe specification are in units of uint32_t values but,
 * since the ioctl interfaces deal with byte counts, so do these validation
 * functions. According to the specification the same constraints hold for
 * both the length and offset fields; experience has shown, however, that we
 * need to be more relaxed when validating the length -- see the comment in the
 * validation function below.
 *
 * Starting in NVMe 1.3, additional constraints about the granularity were
 * added through the FWUG field in the identify controller data structure. This
 * indicates the required alignment in 4 KiB chunks. The controller is allowed
 * to indicate a value of 0 to indicate that this is unknown (which is not
 * particularly helpful) or that it may be 0xff which indicates that there is
 * no alignment constraint other than the natural uint32_t alignment.
 *
 * For devices that exist prior to NVMe 1.3, we assume that we probably need at
 * least 4 KiB granularity for the time being. This may need to change in the
 * future.
 */
uint32_t
nvme_fw_load_granularity(const nvme_valid_ctrl_data_t *data)
{
	uint32_t gran = NVME_DEFAULT_FWUG;

	if (nvme_vers_atleast(data->vcd_vers, &nvme_vers_1v3)) {
		const uint8_t fwug = data->vcd_id->ap_fwug;
		if (fwug == 0xff) {
			gran = NVME_DWORD_SIZE;
		} else if (fwug != 0) {
			gran = fwug * NVME_FWUG_MULT;
		}
	}

	return (gran);
}

static bool
nvme_fw_load_field_valid_len(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t len, char *msg, size_t msglen)
{
	/*
	 * While we would like to validate that the length is consistent with
	 * the firmware upgrade granularity, we have encountered drives where
	 * the vendor's firmware update file sizes are not a multiple of the
	 * required granularity, and where the strategy of padding the last
	 * block out to that required granularity does not always result in a
	 * file that the drive will accept.
	 *
	 * The best we can do is ensure that it is a whole number of dwords.
	 */
	if ((len & NVME_DWORD_MASK) != 0) {
		(void) snprintf(msg, msglen, "%s (%s) value 0x%" PRIx64 " must "
		    "be aligned to the firmware update granularity 0x%x",
		    field->nlfi_human, field->nlfi_spec, len, NVME_DWORD_SIZE);
		return (false);
	}
	return (nvme_field_range_check(field, NVME_DWORD_SIZE,
	    NVME_FW_LENB_MAX, msg, msglen, len));
}

static bool
nvme_fw_load_field_valid_offset(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t off, char *msg, size_t msglen)
{
	uint32_t gran = nvme_fw_load_granularity(data);

	if ((off % gran) != 0) {
		(void) snprintf(msg, msglen, "%s (%s) value 0x%" PRIx64 " must "
		    "be aligned to the firmware update granularity 0x%x",
		    field->nlfi_human, field->nlfi_spec, off, gran);
		return (false);
	}

	return (nvme_field_range_check(field, 0, NVME_FW_OFFSETB_MAX, msg,
	    msglen, off));
}

const nvme_field_info_t nvme_fw_load_fields[] = {
	[NVME_FW_LOAD_REQ_FIELD_NUMD] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_fw_load_field_valid_len,
		.nlfi_spec = "numd",
		.nlfi_human = "number of dwords",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	},
	[NVME_FW_LOAD_REQ_FIELD_OFFSET] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_fw_load_field_valid_offset,
		.nlfi_spec = "ofst",
		.nlfi_human = "offset",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	}
};

const size_t nvme_fw_load_nfields = ARRAY_SIZE(nvme_fw_load_fields);

static bool
nvme_fw_commit_field_valid_slot(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t slot, char *msg, size_t msglen)
{
	return (nvme_field_range_check(field, NVME_FW_SLOT_MIN,
	    data->vcd_id->id_frmw.fw_nslot, msg, msglen, slot));
}

/*
 * This validation function represents an area of improvement that we'd like to
 * figure out in the future. Immediate firmware activations are only supported
 * in NVMe 1.3, so while it's a bad value prior to NVMe 1.3, that is a somewhat
 * confusing error. In addition, the various boot partition updates are not
 * supported, so it's not a bad value to the spec, but just to us.
 */
static bool
nvme_fw_commit_field_valid_act(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t act, char *msg, size_t msglen)
{
	uint64_t max = NVME_FWC_ACTIVATE;

	if (nvme_vers_atleast(data->vcd_vers, &nvme_vers_1v3)) {
		max = NVME_FWC_ACTIVATE_IMMED;
	}

	return (nvme_field_range_check(field, 0, max, msg, msglen, act));
}

const nvme_field_info_t nvme_fw_commit_fields[] = {
	[NVME_FW_COMMIT_REQ_FIELD_SLOT] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_fw_commit_field_valid_slot,
		.nlfi_spec = "fs",
		.nlfi_human = "firmware slot",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	},
	[NVME_FW_COMMIT_REQ_FIELD_ACT] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_fw_commit_field_valid_act,
		.nlfi_spec = "ca",
		.nlfi_human = "commit action",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	}
};

const size_t nvme_fw_commit_nfields = ARRAY_SIZE(nvme_fw_commit_fields);
/*
 * 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 2025 Oxide Computer Company
 */

/*
 * Common field and validation for NVMe Format NVM operations. This covers what
 * nvmeadm(8) calls both secure-erase and format.
 */

#include "nvme_common.h"

#include <sys/sysmacros.h>

const nvme_field_info_t nvme_format_fields[] = {
	[NVME_FORMAT_REQ_FIELD_LBAF] = {
		.nlfi_vers = &nvme_vers_1v0,
		/*
		 * In the future we should plumb through enough information that
		 * we can check this against the common namespace information.
		 */
		.nlfi_max_size = NVME_FRMT_MAX_LBAF,
		.nlfi_spec = "lbaf",
		.nlfi_human = "LBA format",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	},
	[NVME_FORMAT_REQ_FIELD_SES] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_max_size = NVME_FRMT_MAX_SES,
		.nlfi_spec = "ses",
		.nlfi_human = "secure erase settings",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_FORMAT_REQ_FIELD_NSID] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_field_valid_nsid,
		.nlfi_spec = "nsid",
		.nlfi_human = "namespace ID",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	}
};

const size_t nvme_format_nfields = ARRAY_SIZE(nvme_format_fields);

bool
nvme_format_cmds_supported(const nvme_valid_ctrl_data_t *data)
{
	return (data->vcd_id->id_oacs.oa_format != 0);
}
/*
 * 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 2025 Oxide Computer Company
 */

/*
 * Information about supported NVMe identify commands that can be issued.
 */

#include "nvme_common.h"

#include <sys/sysmacros.h>

static bool
nvme_identify_field_valid_cns(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t cns, char *msg, size_t msglen)
{
	uint64_t max;

	if (nvme_field_atleast(data, &nvme_vers_1v2)) {
		max = NVME_IDENTIFY_MAX_CNS_1v2;
	} else if (nvme_field_atleast(data, &nvme_vers_1v1)) {
		max = NVME_IDENTIFY_MAX_CNS_1v1;
	} else {
		max = NVME_IDENTIFY_MAX_CNS;
	}

	return (nvme_field_range_check(field, 0, max, msg, msglen, cns));
}

static bool
nvme_identify_field_valid_buf(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t len, char *msg, size_t msglen)
{
	return (nvme_field_range_check(field, NVME_IDENTIFY_BUFSIZE,
	    NVME_IDENTIFY_BUFSIZE, msg, msglen, len));
}

const nvme_field_info_t nvme_identify_fields[] = {
	[NVME_ID_REQ_F_CNS] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_identify_field_valid_cns,
		.nlfi_spec = "cns",
		.nlfi_human = "Controller or Namespace Structure",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	},
	[NVME_ID_REQ_F_NSID] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_spec = "nsid",
		/*
		 * The NSID for an identify command can have several different
		 * forms.
		 */
		.nlfi_max_size = NVME_IDENTIFY_MAX_NSID,
		.nlfi_human = "namespace ID",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_ID_REQ_F_CTRLID] = {
		.nlfi_vers = &nvme_vers_1v2,
		.nlfi_max_size = NVME_IDENTIFY_MAX_CTRLID,
		.nlfi_spec = "cntid",
		.nlfi_human = "Controller ID",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_ID_REQ_F_BUF] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_identify_field_valid_buf,
		.nlfi_spec = "dptr",
		.nlfi_human = "output",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	}
};

const size_t nvme_identify_nfields = ARRAY_SIZE(nvme_identify_fields);

static bool
nvme_identify_support_nsid(const nvme_valid_ctrl_data_t *data)
{
	return (data->vcd_id->id_oacs.oa_nsmgmt != 0);
}

const nvme_identify_info_t nvme_identify_cmds[] = { {
	.nii_name = "identify namespace",
	.nii_csi = NVME_CSI_NVM,
	.nii_cns = NVME_IDENTIFY_NSID,
	.nii_vers = &nvme_vers_1v0,
	.nii_fields = 1 << NVME_ID_REQ_F_NSID,
	.nii_flags = NVME_IDENTIFY_INFO_F_NS_OK | NVME_IDENTIFY_INFO_F_BCAST
}, {
	.nii_name = "identify controller",
	.nii_csi = NVME_CSI_NVM,
	.nii_cns = NVME_IDENTIFY_CTRL,
	.nii_vers = &nvme_vers_1v0,
}, {
	.nii_name = "active namespace ID list",
	.nii_csi = NVME_CSI_NVM,
	.nii_cns = NVME_IDENTIFY_NSID_LIST,
	.nii_vers = &nvme_vers_1v1,
	.nii_fields = 1 << NVME_ID_REQ_F_NSID,
	.nii_flags = NVME_IDENTIFY_INFO_F_NSID_LIST
}, {
	.nii_name = "namespace identification descriptor list",
	.nii_csi = NVME_CSI_NVM,
	.nii_cns = NVME_IDENTIFY_NSID_DESC,
	.nii_vers = &nvme_vers_1v3,
	.nii_fields = (1 << NVME_ID_REQ_F_NSID),
	.nii_flags = NVME_IDENTIFY_INFO_F_NS_OK
}, {
	.nii_name = "allocated namespace id list",
	.nii_csi = NVME_CSI_NVM,
	.nii_cns = NVME_IDENTIFY_NSID_ALLOC_LIST,
	.nii_vers = &nvme_vers_1v2,
	.nii_sup_func = nvme_identify_support_nsid,
	.nii_fields = 1 << NVME_ID_REQ_F_NSID,
	.nii_flags = NVME_IDENTIFY_INFO_F_NSID_LIST
}, {
	.nii_name = "identify allocated namespace",
	.nii_csi = NVME_CSI_NVM,
	.nii_cns = NVME_IDENTIFY_NSID_ALLOC,
	.nii_vers = &nvme_vers_1v2,
	.nii_sup_func = nvme_identify_support_nsid,
	.nii_fields = 1 << NVME_ID_REQ_F_NSID,
	.nii_flags = NVME_IDENTIFY_INFO_F_NS_OK
}, {
	.nii_name = "namespace attached controller list",
	.nii_csi = NVME_CSI_NVM,
	.nii_cns = NVME_IDENTIFY_NSID_CTRL_LIST,
	.nii_vers = &nvme_vers_1v2,
	.nii_sup_func = nvme_identify_support_nsid,
	.nii_fields = (1 << NVME_ID_REQ_F_NSID) | (1 << NVME_ID_REQ_F_CTRLID),
	.nii_flags = NVME_IDENTIFY_INFO_F_NS_OK
}, {
	.nii_name = "nvm subsystem controller list",
	.nii_csi = NVME_CSI_NVM,
	.nii_cns = NVME_IDENTIFY_CTRL_LIST,
	.nii_vers = &nvme_vers_1v2,
	.nii_sup_func = nvme_identify_support_nsid,
	.nii_fields = (1 << NVME_ID_REQ_F_CTRLID)
} };

const size_t nvme_identify_ncmds = ARRAY_SIZE(nvme_identify_cmds);

bool
nvme_identify_info_supported(const nvme_identify_info_t *info,
    const nvme_valid_ctrl_data_t *data)
{
	if (info->nii_vers != NULL && !nvme_field_atleast(data,
	    info->nii_vers)) {
		return (false);
	}

	if (info->nii_sup_func != NULL && !info->nii_sup_func(data)) {
		return (false);
	}

	return (true);
}
/*
 * 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 deals with validating and issuing various log page requests to an
 * NVMe target. This contains information about all spec-based log pages. The
 * get log page command has added a number of fields that have evolved over
 * time. We validate that we're only sending commands to a device that we expect
 * it to have a chance of understanding. In general, we only allow through
 * unknown log pages that correspond to vendor-specific commands.
 *
 * We have two different tables of information that we use to drive and validate
 * things here:
 *
 * 1) We have a list of fields that exist which include minimum controller
 * versions and related functionality validation routines that operate off of
 * the nvme_t. While this list includes things like the CSI and LID, these are
 * things that may only be specified when we have a non-standard log page.
 *
 * 2) We then have a table of log pages that are supported which list which
 * fields we allow for the device. Not all of this can be static.
 *
 * This file has been designed to be shareable between both userland and the
 * kernel since the logic that libnvme wants to use is quite similar.
 */

#include "nvme_common.h"

#include <sys/sysmacros.h>
#ifdef	_KERNEL
#include <sys/sunddi.h>
#include <sys/stdint.h>
#else
#include <stdio.h>
#include <inttypes.h>
#include <strings.h>
#endif

static bool
nvme_log_field_valid_lsp(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t lsp, char *msg, size_t msglen)
{
	uint64_t max;

	if (nvme_field_atleast(data, &nvme_vers_2v0)) {
		max = NVME_LOG_MAX_LSP_2v0;
	} else {
		max = NVME_LOG_MAX_LSP;
	}

	return (nvme_field_range_check(field, 0, max, msg, msglen, lsp));
}

static bool
nvme_log_field_supported_offset(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, char *msg, size_t msglen)
{
	if (data->vcd_id->id_lpa.lp_extsup != 0) {
		return (true);
	}

	(void) snprintf(msg, msglen, "controller does not support field %s "
	    "(%s): missing extended data support in Log Page Attributes (LPA)",
	    field->nlfi_human, field->nlfi_spec);
	return (false);
}

/*
 * The offset is a full 64-bit byte value; however, it must be 4-byte aligned.
 */
static bool
nvme_log_field_valid_offset(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t size, char *msg, size_t msglen)
{
	if ((size % NVME_DWORD_SIZE) != 0) {
		(void) snprintf(msg, msglen, "%s (%s) value 0x%" PRIx64 " is "
		    "invalid: value must be %u-byte aligned", field->nlfi_human,
		    field->nlfi_spec, size, NVME_DWORD_SIZE);
		return (false);
	}

	return (true);
}

static bool
nvme_log_field_valid_size(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t size, char *msg, size_t msglen)
{
	uint64_t max = NVME_LOG_MAX_SIZE;

	if (nvme_field_atleast(data, &nvme_vers_1v2) &&
	    data->vcd_id->id_lpa.lp_extsup != 0) {
		max = NVME_LOG_MAX_SIZE_1v2;
	}

	/*
	 * The NVMe specification operates in terms of uint32_t (dword) units.
	 * Make sure that we are operating within that constraint.
	 */
	if ((size % 4) != 0) {
		(void) snprintf(msg, msglen, "%s (%s) value 0x%" PRIx64 " is "
		    "invalid: value must be 4-byte aligned", field->nlfi_human,
		    field->nlfi_spec, size);
		return (false);
	}

	return (nvme_field_range_check(field, 4, max, msg, msglen, size));
}

const nvme_field_info_t nvme_log_fields[] = {
	[NVME_LOG_REQ_FIELD_LID] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_max_size = NVME_LOG_MAX_LID,
		.nlfi_spec = "lid",
		.nlfi_human = "log ID",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	},
	[NVME_LOG_REQ_FIELD_LSP] = {
		.nlfi_vers = &nvme_vers_1v3,
		.nlfi_valid = nvme_log_field_valid_lsp,
		.nlfi_spec = "lsp",
		.nlfi_human = "log specific field",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_LOG_REQ_FIELD_LSI] = {
		.nlfi_vers = &nvme_vers_1v4,
		.nlfi_max_size = NVME_LOG_MAX_LSI,
		.nlfi_spec = "lsi",
		.nlfi_human = "log specific ID",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_LOG_REQ_FIELD_SIZE] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_log_field_valid_size,
		.nlfi_spec = "dptr/numd",
		.nlfi_human = "output",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	},
	[NVME_LOG_REQ_FIELD_CSI] = {
		.nlfi_vers = &nvme_vers_2v0,
		/*
		 * This has the field's maximum range right now, though NVMe 2.0
		 * only defines a few values. Because the kernel only allows
		 * through known log pages, we don't really bother to check the
		 * condensed range and allow vendor-specific logs to go wild.
		 */
		.nlfi_max_size = NVME_LOG_MAX_CSI,
		.nlfi_spec = "csi",
		.nlfi_human = "command set ID",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_LOG_REQ_FIELD_RAE] = {
		.nlfi_vers = &nvme_vers_1v3,
		.nlfi_max_size = NVME_LOG_MAX_RAE,
		.nlfi_spec = "rae",
		.nlfi_human = "retain asynchronous event",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_LOG_REQ_FIELD_OFFSET] = {
		.nlfi_vers = &nvme_vers_1v2,
		.nlfi_sup = nvme_log_field_supported_offset,
		.nlfi_valid = nvme_log_field_valid_offset,
		.nlfi_max_size = NVME_LOG_MAX_OFFSET,
		.nlfi_spec = "lpo",
		.nlfi_human = "log offset",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_LOG_REQ_FIELD_NSID] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_field_valid_nsid,
		.nlfi_spec = "nsid",
		.nlfi_human = "namespace ID",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	}
};

const size_t nvme_log_nfields = ARRAY_SIZE(nvme_log_fields);

static uint64_t
nvme_lpd_error_len(const nvme_valid_ctrl_data_t *data,
    const nvme_log_page_info_t *lpi)
{
	const uint64_t nents = data->vcd_id->id_elpe + 1;
	const uint64_t logsz = nents * sizeof (nvme_error_log_entry_t);

	return (logsz);
}

static nvme_log_disc_scope_t
nvme_lpd_health_scope(const nvme_valid_ctrl_data_t *data,
    const nvme_log_page_info_t *lpi)
{
	nvme_log_disc_scope_t ret = NVME_LOG_SCOPE_CTRL;

	if (nvme_field_atleast(data, &nvme_vers_1v0) &&
	    data->vcd_id->id_lpa.lp_smart != 0) {
		ret |= NVME_LOG_SCOPE_NS;
	}

	return (ret);
}

static bool
nvme_lpd_changens_sup(const nvme_valid_ctrl_data_t *data,
    const nvme_log_page_info_t *lpi)
{
	return (nvme_field_atleast(data, &nvme_vers_1v2) &&
	    data->vcd_id->id_oaes.oaes_nsan != 0);
}

static bool
nvme_lpd_cmdeff_sup(const nvme_valid_ctrl_data_t *data,
    const nvme_log_page_info_t *lpi)
{
	return (nvme_field_atleast(data, &nvme_vers_1v2) &&
	    data->vcd_id->id_lpa.lp_cmdeff != 0);
}

static bool
nvme_lpd_pev_sup(const nvme_valid_ctrl_data_t *data,
    const nvme_log_page_info_t *lpi)
{
	return (nvme_field_atleast(data, &nvme_vers_1v4) &&
	    data->vcd_id->id_lpa.lp_persist != 0);
}

static bool
nvme_lpd_pev_len(uint64_t *outp, const void *data, size_t len)
{
	nvme_pev_log_t pev;

	if (len < sizeof (pev)) {
		return (false);
	}

	(void) memcpy(&pev, data, sizeof (pev));
	*outp = pev.pel_tll;
	return (true);
}

static bool
nvme_lpd_telemetry_sup(const nvme_valid_ctrl_data_t *data,
    const nvme_log_page_info_t *lpi)
{
	return (nvme_field_atleast(data, &nvme_vers_1v3) &&
	    data->vcd_id->id_lpa.lp_telemetry != 0);
}

static bool
nvme_lpd_telemetry_len(uint64_t *outp, const void *data, size_t len)
{
	nvme_telemetry_log_t telem;
	uint64_t nblks;

	if (len < sizeof (telem)) {
		return (false);
	}

	(void) memcpy(&telem, data, sizeof (telem));

	/*
	 * See if we have section 4 information, then fall back to 3, 2, and
	 * finally 1 to determine the size. We then have to add the header.
	 */
	nblks = telem.ntl_thda4lb;
	if (nblks == 0)
		nblks = telem.ntl_thda3lb;
	if (nblks == 0)
		nblks = telem.ntl_thda2lb;
	if (nblks == 0)
		nblks = telem.ntl_thda1lb;
	nblks++;
	*outp = nblks * sizeof (telem);
	return (true);
}

static bool
nvme_lpd_eom_len(uint64_t *outp, const void *data, size_t len)
{
	nvme_eom_hdr_t hdr;
	uint64_t out;

	if (len < sizeof (hdr)) {
		return (false);
	}

	(void) memcpy(&hdr, data, sizeof (hdr));

	/*
	 * The valid size depends on the value in EOMIP. If the measurement is
	 * not done then the only amount that is valid is the overall header
	 * length. Currently the header length is required to be 64 bytes. If it
	 * is not that, then we will generate an error as that means something
	 * else is going on that means we shouldn't trust this.
	 */
	if (hdr.eom_hsize != sizeof (hdr)) {
		return (false);
	}

	out = sizeof (hdr);
	switch (hdr.eom_eomip) {
	case NVME_EOM_NOT_STARTED:
	case NVME_EOM_IN_PROGRESS:
		break;
	case NVME_EOM_DONE:
		out += hdr.eom_ds * hdr.eom_nd;
		break;
	default:
		/*
		 * This indicates we have a progress code we don't know how to
		 * handle. It's possible that it would make sense to just return
		 * the header size, but for now we opt to be conservative and
		 * simply fail.
		 */
		return (false);
	}

	*outp = out;
	return (true);
}

/*
 * Short names have evolved a bit over time. When they do we put the old name as
 * an alias record here.
 */
static const char *nvme_supcmd_aliases[] = { "cmdeff" };

/*
 * The short names here correspond to the well defined names in nvmeadm(8) and
 * libnvme(3LIB) that users expect to be able to use. Please do not change them
 * without accounting for aliases and backwards compatibility. The index of the
 * supported log pages entry is expected to be the first entry here. This is
 * relied upon by log discovery in nvme_log_discover_fetch_sup_logs().
 */
const nvme_log_page_info_t nvme_std_log_pages[] = { {
	.nlpi_short = "suplog",
	.nlpi_human = "Supported Log Pages",
	.nlpi_lid = NVME_LOGPAGE_SUP,
	.nlpi_csi = NVME_CSI_NVM,
	.nlpi_vers = &nvme_vers_2v0,
	.nlpi_kind = NVME_LOG_ID_MANDATORY,
	.nlpi_source = NVME_LOG_DISC_S_SPEC,
	.nlpi_scope = NVME_LOG_SCOPE_CTRL,
	.nlpi_len = sizeof (nvme_suplog_log_t)
}, {
	.nlpi_short = "error",
	.nlpi_human = "Error information",
	.nlpi_lid = NVME_LOGPAGE_ERROR,
	.nlpi_csi = NVME_CSI_NVM,
	.nlpi_vers = &nvme_vers_1v0,
	.nlpi_kind = NVME_LOG_ID_MANDATORY,
	.nlpi_source = NVME_LOG_DISC_S_SPEC,
	.nlpi_disc = NVME_LOG_DISC_F_NEED_RAE,
	.nlpi_scope = NVME_LOG_SCOPE_CTRL,
	.nlpi_len_func = nvme_lpd_error_len
}, {
	.nlpi_short = "health",
	.nlpi_human = "SMART / Health information",
	.nlpi_lid = NVME_LOGPAGE_HEALTH,
	.nlpi_csi = NVME_CSI_NVM,
	.nlpi_vers = &nvme_vers_1v0,
	.nlpi_kind = NVME_LOG_ID_MANDATORY,
	.nlpi_source = NVME_LOG_DISC_S_SPEC | NVME_LOG_DISC_S_ID_CTRL,
	.nlpi_disc = NVME_LOG_DISC_F_NEED_RAE,
	.nlpi_scope_func = nvme_lpd_health_scope,
	.nlpi_len = sizeof (nvme_health_log_t)
}, {
	.nlpi_short = "firmware",
	.nlpi_human = "Firmware Slot Information",
	.nlpi_lid = NVME_LOGPAGE_FWSLOT,
	.nlpi_csi = NVME_CSI_NVM,
	.nlpi_vers = &nvme_vers_1v0,
	.nlpi_kind = NVME_LOG_ID_MANDATORY,
	.nlpi_source = NVME_LOG_DISC_S_SPEC,
	.nlpi_disc = 0,
	.nlpi_scope = NVME_LOG_SCOPE_NVM,
	.nlpi_len = sizeof (nvme_fwslot_log_t),
}, {
	.nlpi_short = "changens",
	.nlpi_human = "changed namespaces",
	.nlpi_lid = NVME_LOGPAGE_NSCHANGE,
	.nlpi_csi = NVME_CSI_NVM,
	.nlpi_vers = &nvme_vers_1v2,
	.nlpi_sup_func = nvme_lpd_changens_sup,
	.nlpi_kind = NVME_LOG_ID_OPTIONAL,
	.nlpi_source = NVME_LOG_DISC_S_ID_CTRL,
	.nlpi_disc = NVME_LOG_DISC_F_NEED_RAE,
	.nlpi_scope = NVME_LOG_SCOPE_CTRL,
	.nlpi_len = sizeof (nvme_nschange_list_t)
}, {
	.nlpi_short = "supcmd",
	.nlpi_human = "commands supported and effects",
	.nlpi_aliases = nvme_supcmd_aliases,
	.nlpi_naliases = ARRAY_SIZE(nvme_supcmd_aliases),
	.nlpi_lid = NVME_LOGPAGE_CMDSUP,
	.nlpi_csi = NVME_CSI_NVM,
	.nlpi_vers = &nvme_vers_1v2,
	.nlpi_sup_func = nvme_lpd_cmdeff_sup,
	.nlpi_kind = NVME_LOG_ID_OPTIONAL,
	.nlpi_source = NVME_LOG_DISC_S_ID_CTRL,
	.nlpi_scope = NVME_LOG_SCOPE_CTRL,
	.nlpi_len = sizeof (nvme_cmdeff_log_t)
}, {
	.nlpi_short = "pev",
	.nlpi_human = "persistent event log",
	.nlpi_lid = NVME_LOGPAGE_PEV,
	.nlpi_csi = NVME_CSI_NVM,
	.nlpi_vers = &nvme_vers_1v4,
	.nlpi_sup_func = nvme_lpd_pev_sup,
	.nlpi_kind = NVME_LOG_ID_OPTIONAL,
	.nlpi_source = NVME_LOG_DISC_S_ID_CTRL,
	.nlpi_disc = NVME_LOG_DISC_F_NEED_LSP,
	.nlpi_scope = NVME_LOG_SCOPE_NVM,
	.nlpi_len = sizeof (nvme_pev_log_t),
	.nlpi_var_func = nvme_lpd_pev_len
}, {
	.nlpi_short = "telemetry",
	.nlpi_human = "telemetry host-initiated",
	.nlpi_lid = NVME_LOGPAGE_TELMHOST,
	.nlpi_csi = NVME_CSI_NVM,
	.nlpi_vers = &nvme_vers_1v3,
	.nlpi_sup_func = nvme_lpd_telemetry_sup,
	.nlpi_kind = NVME_LOG_ID_OPTIONAL,
	.nlpi_source = NVME_LOG_DISC_S_ID_CTRL,
	.nlpi_disc = NVME_LOG_DISC_F_NEED_LSP,
	.nlpi_scope = NVME_LOG_SCOPE_CTRL,
	.nlpi_len = sizeof (nvme_telemetry_log_t),
	.nlpi_var_func = nvme_lpd_telemetry_len
}, {
	.nlpi_short = "supfeat",
	.nlpi_human = "Feature Identifiers Supported and Effects",
	.nlpi_lid = NVME_LOGPAGE_SUPFEAT,
	.nlpi_csi = NVME_CSI_NVM,
	.nlpi_vers = &nvme_vers_2v0,
	.nlpi_kind = NVME_LOG_ID_MANDATORY,
	.nlpi_source = NVME_LOG_DISC_S_SPEC,
	.nlpi_scope = NVME_LOG_SCOPE_CTRL,
	.nlpi_len = sizeof (nvme_supfeat_log_t)
}, {
	.nlpi_short = "supmicmd",
	.nlpi_human = "NVMe-MI Commands Supported and Effects",
	.nlpi_lid = NVME_LOGPAGE_SUPMI,
	.nlpi_csi = NVME_CSI_NVM,
	.nlpi_vers = &nvme_vers_2v0,
	.nlpi_kind = NVME_LOG_ID_MANDATORY,
	.nlpi_source = NVME_LOG_DISC_S_SPEC,
	.nlpi_scope = NVME_LOG_SCOPE_CTRL,
	.nlpi_len = sizeof (nvme_supmicmd_log_t)
}, {
	.nlpi_short = "phyeye",
	.nlpi_human = "Physical Interface Receiver Eye Opening Measurement Log",
	.nlpi_lid = NVME_LOGPAGE_PHYEYE,
	.nlpi_csi = NVME_CSI_NVM,
	/*
	 * This log page was introduced in the PCIe 1.1 supplement. There is no
	 * good way to discover it. It has been implemented in NVMe 2.0 based
	 * devices so we basically tag it as a 2.0 minimum version and then rely
	 * on the 'suplog' log page to determine presence.
	 */
	.nlpi_vers = &nvme_vers_2v0,
	.nlpi_kind = NVME_LOG_ID_OPTIONAL,
	.nlpi_source = NVME_LOG_DISC_S_SPEC,
	.nlpi_disc = NVME_LOG_DISC_F_NEED_LSP | NVME_LOG_DISC_F_NEED_LSI,
	.nlpi_scope = NVME_LOG_SCOPE_CTRL,
	.nlpi_len = sizeof (nvme_eom_hdr_t),
	.nlpi_var_func = nvme_lpd_eom_len
} };

const size_t nvme_std_log_npages = ARRAY_SIZE(nvme_std_log_pages);

nvme_log_disc_scope_t
nvme_log_page_info_scope(const nvme_log_page_info_t *info,
    const nvme_valid_ctrl_data_t *data)
{
	if (info->nlpi_scope_func != NULL) {
		return (info->nlpi_scope_func(data, info));
	} else {
		return (info->nlpi_scope);
	}
}

uint64_t
nvme_log_page_info_size(const nvme_log_page_info_t *info,
    const nvme_valid_ctrl_data_t *data, bool *var)
{
	uint64_t len;
	*var = info->nlpi_var_func != NULL;

	if (info->nlpi_len_func != NULL) {
		len = info->nlpi_len_func(data, info);
	} else {
		len = info->nlpi_len;
	}

	/*
	 * Some vendor-specific log pages are not documented to have 4-byte
	 * aligned lengths. This means that to get the full log page you must
	 * round this up to ensure that you end up with a valid request. We opt
	 * to do this here rather than have to check every single log page data
	 * structure and fix it up manually. While it means consumers that are
	 * using this to ignore information about the type itself may
	 * erroneously display extra bytes (e.g. nvmeadm's default hex dumper),
	 * that's better than getting an error or truncating the data.
	 */
	return (P2ROUNDUP(len, NVME_DWORD_SIZE));
}

bool
nvme_log_page_info_supported(const nvme_log_page_info_t *info,
    const nvme_valid_ctrl_data_t *data)
{
	bool vers, sup_func;

	if (info->nlpi_vers != NULL) {
		vers = nvme_field_atleast(data, info->nlpi_vers);
	} else {
		vers = true;
	}

	if (info->nlpi_sup_func != NULL) {
		sup_func = info->nlpi_sup_func(data, info);
	} else {
		sup_func = true;
	}

	return (vers && sup_func);
}
/*
 * 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 2025 Oxide Computer Company
 */

/*
 * Common field and validation for NVMe Namespace related commands. This covers
 * attaching and detaching controllers to namespaces as well as creating and
 * deleting namespaces.
 */

#include "nvme_common.h"

#include <sys/sysmacros.h>

static bool
nvme_ns_attach_field_valid_sel(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t act, char *msg, size_t msglen)
{
	const uint64_t min = NVME_NS_ATTACH_CTRL_ATTACH;
	const uint64_t max = NVME_NS_ATTACH_CTRL_DETACH;

	return (nvme_field_range_check(field, min, max, msg, msglen, act));
}

const nvme_field_info_t nvme_ns_attach_fields[] = {
	[NVME_NS_ATTACH_REQ_FIELD_SEL] = {
		.nlfi_vers = &nvme_vers_1v2,
		.nlfi_valid = nvme_ns_attach_field_valid_sel,
		.nlfi_spec = "sel",
		.nlfi_human = "select",
		.nlfi_def_req = true,
		.nlfi_def_allow = false
	},
	[NVME_NS_ATTACH_REQ_FIELD_NSID] = {
		.nlfi_vers = &nvme_vers_1v2,
		.nlfi_valid = nvme_field_valid_nsid,
		.nlfi_spec = "nsid",
		.nlfi_human = "namespace ID",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	},
	/*
	 * The interpretation of the data pointer is technically specific to the
	 * select command. As we don't actually accept any data, right now this
	 * is here just for libnvme tracking purposes.
	 */
	[NVME_NS_ATTACH_REQ_FIELD_DPTR] = {
		.nlfi_vers = &nvme_vers_1v2,
		.nlfi_spec = "dptr",
		.nlfi_human = "data pointer",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	}
};

const size_t nvme_ns_attach_nfields = ARRAY_SIZE(nvme_ns_attach_fields);

const nvme_field_info_t nvme_ns_delete_fields[] = {
	[NVME_NS_DELETE_REQ_FIELD_NSID] = {
		.nlfi_vers = &nvme_vers_1v2,
		.nlfi_valid = nvme_field_valid_nsid,
		.nlfi_spec = "nsid",
		.nlfi_human = "namespace ID",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	}
};

const size_t nvme_ns_delete_nfields = ARRAY_SIZE(nvme_ns_delete_fields);

/*
 * This is an integer which currently has only a single bit defined, bit 0.
 * Though NVMe 2.1 will change this.
 */
static bool
nvme_ns_create_field_valid_nmic(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t act, char *msg, size_t msglen)
{
	return (nvme_field_mask_check(field, NVME_NS_MGMT_NMIC_MASK, msg,
	    msglen, act));
}

/*
 * The NSZE and NCAP fields are related. Because these are in terms of a
 * formatted LBA which we don't have quite at this moment, we can't confirm the
 * maximum value; however, we do know enough to know a value of zero is wrong.
 */
static bool
nvme_ns_attach_field_valid_nsze(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t act, char *msg, size_t msglen)
{
	return (nvme_field_range_check(field, 1, UINT64_MAX, msg, msglen, act));
}

/*
 * The set of fields that are required to be allowed or defaults varies based
 * upon the CSI that is used. For example the NVM and K/V command sets have
 * different required fields. As such, the notion of what's required and
 * settable is something that is determined by the CSI, hence why the required
 * and allowed fields are missing for everything that isn't the CSI.
 */
const nvme_field_info_t nvme_ns_create_fields[] = {
	[NVME_NS_CREATE_REQ_FIELD_CSI] = {
		/*
		 * The libnvme APIs require that a CSI is passed to create this,
		 * so this 2.0 isn't quite truly enforced.
		 */
		.nlfi_vers = &nvme_vers_2v0,
		.nlfi_max_size = NVME_NS_MGMT_MAX_CSI,
		.nlfi_spec = "csi",
		.nlfi_human = "command set ID",
		.nlfi_def_req = true,
		.nlfi_def_allow = false
	},
	[NVME_NS_CREATE_REQ_FIELD_NSZE] = {
		.nlfi_vers = &nvme_vers_1v2,
		/*
		 * The NSZE and NCAP fields are required to be related by a
		 * namespace granularity record; however, this was not
		 * introduced until NVMe 1.4. Absent this information, the main
		 * constraint is that the two are equivalent unless thin
		 * provisioning is supported. In addition, when setting the
		 * field, we don't know what LBA size someone has selected, so
		 * this can only be checked at command execution time.
		 */
		.nlfi_valid = nvme_ns_attach_field_valid_nsze,
		.nlfi_spec = "nsze",
		.nlfi_human = "namespace size"
	},
	[NVME_NS_CREATE_REQ_FIELD_NCAP] = {
		.nlfi_vers = &nvme_vers_1v2,
		.nlfi_valid = nvme_ns_attach_field_valid_nsze,
		.nlfi_spec = "ncap",
		.nlfi_human = "namespace capacity"
	},
	[NVME_NS_CREATE_REQ_FIELD_FLBAS] = {
		.nlfi_vers = &nvme_vers_1v2,
		/*
		 * See the notes in common/nvme/nvme_format.c around this choice
		 * of maximum.
		 */
		.nlfi_max_size = NVME_NS_MGMT_MAX_FLBAS,
		.nlfi_spec = "flbas",
		.nlfi_human = "formatted LBA size"
	},
	[NVME_NS_CREATE_REQ_FIELD_NMIC] = {
		.nlfi_vers = &nvme_vers_1v2,
		.nlfi_valid = nvme_ns_create_field_valid_nmic,
		.nlfi_max_size = NVME_NS_MGMT_MAX_FLBAS,
		.nlfi_spec = "nmic",
		.nlfi_human = "namespace multi-path I/O and namespace sharing "
		    "capabilities"
	}
};

const size_t nvme_ns_create_nfields = ARRAY_SIZE(nvme_ns_create_fields);

/*
 * These are the default fields that are required for an NVM command. The CSI
 * cannot be set. The we allow to default to zero, unshared, and therefore is
 * optional.
 */
const nvme_ns_create_req_field_t nvme_ns_create_fields_nvm_req[] = {
	NVME_NS_CREATE_REQ_FIELD_NSZE, NVME_NS_CREATE_REQ_FIELD_NCAP,
	NVME_NS_CREATE_REQ_FIELD_FLBAS
};
const size_t nvme_ns_create_fields_nvm_nreq =
    ARRAY_SIZE(nvme_ns_create_fields_nvm_req);

const nvme_ns_create_req_field_t nvme_ns_create_fields_nvm_allow[] = {
	NVME_NS_CREATE_REQ_FIELD_NSZE, NVME_NS_CREATE_REQ_FIELD_NCAP,
	NVME_NS_CREATE_REQ_FIELD_FLBAS, NVME_NS_CREATE_REQ_FIELD_NMIC
};
const size_t nvme_ns_create_fields_nvm_nallow =
    ARRAY_SIZE(nvme_ns_create_fields_nvm_allow);

bool
nvme_nsmgmt_cmds_supported(const nvme_valid_ctrl_data_t *data)
{
	if (nvme_vers_atleast(data->vcd_vers, &nvme_vers_1v2)) {
		return (data->vcd_id->id_oacs.oa_nsmgmt != 0);
	}

	return (false);
}
/*
 * 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 2025 Oxide Computer Company
 */

/*
 * Common definitions and values for NVMe version use.
 */

#include "nvme_common.h"

const nvme_version_t nvme_vers_1v0 = { .v_major = 1, .v_minor = 0 };
const nvme_version_t nvme_vers_1v1 = { .v_major = 1, .v_minor = 1 };
const nvme_version_t nvme_vers_1v2 = { .v_major = 1, .v_minor = 2 };
const nvme_version_t nvme_vers_1v3 = { .v_major = 1, .v_minor = 3 };
const nvme_version_t nvme_vers_1v4 = { .v_major = 1, .v_minor = 4 };
const nvme_version_t nvme_vers_2v0 = { .v_major = 2, .v_minor = 0 };
const nvme_version_t nvme_vers_2v1 = { .v_major = 2, .v_minor = 1 };

bool
nvme_vers_atleast(const nvme_version_t *dev, const nvme_version_t *targ)
{
	return (dev->v_major > targ->v_major ||
	    (dev->v_major == targ->v_major && dev->v_minor >= targ->v_minor));
}
/*
 * 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 2025 Oxide Computer Company
 */

/*
 * Common field and validation pieces for NVMe Vendor Unique Admin and NVM
 * commands.
 */

#include "nvme_common.h"

#include <sys/sysmacros.h>
#ifdef	_KERNEL
#include <sys/sunddi.h>
#include <sys/stdint.h>
#else
#include <stdio.h>
#include <inttypes.h>
#endif

/*
 * Right now this mainly checks for a valid admin command set operation code.
 * When we end up supporting commands that are meant to be submitted to NVM
 * queues, we'll likely need to add a way to know what type of command set we're
 * targeting.
 */
static bool
nvme_vuc_field_valid_opc(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t opcode, char *msg,
    size_t msglen)
{
	return (nvme_field_range_check(field, NVME_PASSTHRU_MIN_ADMIN_OPC,
	    NVME_PASSTHRU_MAX_ADMIN_OPC, msg, msglen, opcode));
}

/*
 * Unlike with log pages, identify commands, features, etc. we do not know how
 * the namespace will be used. It may be zero, it may need to be the broadcast
 * namespace, or it may target a device specific namespace. As such, we accept
 * all of these, which isn't normally the case.
 */
static bool
nvme_vuc_field_valid_nsid(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t nsid, char *msg, size_t msglen)
{
	if (nsid == 0) {
		return (true);
	}

	return (nvme_field_valid_nsid(field, data, nsid, msg, msglen));
}

/*
 * A VUC data length is in dwords. It's our responsibility to make sure that
 * this is properly aligned. Though zero is a valid value.
 */
static bool
nvme_vuc_field_valid_ndt(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t len, char *msg, size_t msglen)
{
	uint64_t max = (uint64_t)UINT32_MAX << NVME_DWORD_SHIFT;

	if ((len % NVME_DWORD_SIZE) != 0) {
		(void) snprintf(msg, msglen, "%s (%s) value 0x%" PRIx64 " is "
		    "invalid: value must be %u-byte aligned", field->nlfi_human,
		    field->nlfi_spec, len, NVME_DWORD_SIZE);
		return (false);
	}

	return (nvme_field_range_check(field, 0, max, msg, msglen, len));
}

/*
 * The maximum timeout is controlled by the kernel. The only real constraint
 * right now is that it fit into the ioctl size and is non-zero.
 */
static bool
nvme_vuc_field_valid_to(const nvme_field_info_t *field,
    const nvme_valid_ctrl_data_t *data, uint64_t to, char *msg, size_t msglen)
{
	return (nvme_field_range_check(field, 1, UINT32_MAX, msg, msglen, to));
}

const nvme_field_info_t nvme_vuc_fields[] = {
	[NVME_VUC_REQ_FIELD_OPC] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_vuc_field_valid_opc,
		.nlfi_spec = "opc",
		.nlfi_human = "opcode",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	},
	[NVME_VUC_REQ_FIELD_NSID] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_vuc_field_valid_nsid,
		.nlfi_spec = "nsid",
		.nlfi_human = "namespace ID",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_VUC_REQ_FIELD_CDW12] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_max_size = UINT32_MAX,
		.nlfi_spec = "cdw12",
		.nlfi_human = "command dword 12",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_VUC_REQ_FIELD_CDW13] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_max_size = UINT32_MAX,
		.nlfi_spec = "cdw13",
		.nlfi_human = "command dword 13",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_VUC_REQ_FIELD_CDW14] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_max_size = UINT32_MAX,
		.nlfi_spec = "cdw14",
		.nlfi_human = "command dword 14",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_VUC_REQ_FIELD_CDW15] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_max_size = UINT32_MAX,
		.nlfi_spec = "cdw15",
		.nlfi_human = "command dword 15",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_VUC_REQ_FIELD_NDT] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_vuc_field_valid_ndt,
		.nlfi_spec = "ndt",
		.nlfi_human = "number of dwords in data transfer",
		.nlfi_def_req = false,
		.nlfi_def_allow = true
	},
	[NVME_VUC_REQ_FIELD_TO] = {
		.nlfi_vers = &nvme_vers_1v0,
		.nlfi_valid = nvme_vuc_field_valid_to,
		.nlfi_spec = "to",
		.nlfi_human = "timeout",
		.nlfi_def_req = true,
		.nlfi_def_allow = true
	}
};

const size_t nvme_vuc_nfields = ARRAY_SIZE(nvme_vuc_fields);