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
|
/*
* nvme.c -- NVM-Express command line utility.
*
* Copyright (c) 2014-2015, Intel Corporation.
*
* Written by Keith Busch <keith.busch@intel.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* This program uses NVMe IOCTLs to run native nvme commands to a device.
*/
#include <endian.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <inttypes.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <dirent.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "nvme-print.h"
#include "nvme-ioctl.h"
#include "nvme-lightnvm.h"
#include "plugin.h"
#include "argconfig.h"
#include "suffix.h"
#include "fabrics.h"
#define array_len(x) ((size_t)(sizeof(x) / sizeof(x[0])))
#define min(x, y) (x) > (y) ? (y) : (x)
#define max(x, y) (x) > (y) ? (x) : (y)
static int fd;
static struct stat nvme_stat;
const char *devicename;
static const char nvme_version_string[] = NVME_VERSION;
#define CREATE_CMD
#include "nvme-builtin.h"
static struct plugin builtin = {
.commands = commands,
.name = NULL,
.desc = NULL,
.next = NULL,
.tail = &builtin,
};
static struct program nvme = {
.name = "nvme",
.version = nvme_version_string,
.usage = "<command> [<device>] [<args>]",
.desc = "The '<device>' may be either an NVMe character "\
"device (ex: /dev/nvme0) or an nvme block device "\
"(ex: /dev/nvme0n1).",
.extensions = &builtin,
};
static unsigned long long elapsed_utime(struct timeval start_time,
struct timeval end_time)
{
unsigned long long ret = (end_time.tv_sec - start_time.tv_sec)*1000000 +
(end_time.tv_usec - start_time.tv_usec);
return ret;
}
static void open_dev(const char *dev)
{
int err;
devicename = basename(dev);
fd = open(dev, O_RDONLY);
if (fd < 0)
goto perror;
err = fstat(fd, &nvme_stat);
if (err < 0)
goto perror;
if (!S_ISCHR(nvme_stat.st_mode) && !S_ISBLK(nvme_stat.st_mode)) {
fprintf(stderr, "%s is not a block or character device\n", dev);
exit(ENODEV);
}
return;
perror:
perror(dev);
exit(errno);
}
static void check_arg_dev(int argc, char **argv)
{
if (optind >= argc) {
errno = EINVAL;
perror(argv[0]);
exit(errno);
}
}
static void get_dev(int argc, char **argv)
{
check_arg_dev(argc, argv);
open_dev((const char *)argv[optind]);
}
int parse_and_open(int argc, char **argv, const char *desc,
const struct argconfig_commandline_options *clo, void *cfg, size_t size)
{
argconfig_parse(argc, argv, desc, clo, cfg, size);
get_dev(argc, argv);
return fd;
}
static const char *output_format = "Output format: normal|json|binary";
enum {
NORMAL,
JSON,
BINARY,
};
static int validate_output_format(char *format)
{
if (!format)
return -EINVAL;
if (!strcmp(format, "normal"))
return NORMAL;
if (!strcmp(format, "json"))
return JSON;
if (!strcmp(format, "binary"))
return BINARY;
return -EINVAL;
}
static int get_smart_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
struct nvme_smart_log smart_log;
const char *desc = "Retrieve SMART log for the given device "\
"(or optionally a namespace) in either decoded format "\
"(default) or binary.";
const char *namespace = "(optional) desired namespace";
const char *raw = "output in binary format";
int err, fmt;
struct config {
__u32 namespace_id;
int raw_binary;
char *output_format;
};
struct config cfg = {
.namespace_id = 0xffffffff,
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace},
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format },
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
fmt = validate_output_format(cfg.output_format);
if (fmt < 0)
return fmt;
if (cfg.raw_binary)
fmt = BINARY;
err = nvme_smart_log(fd, cfg.namespace_id, &smart_log);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)&smart_log, sizeof(smart_log));
else if (fmt == JSON)
json_smart_log(&smart_log, cfg.namespace_id, devicename);
else
show_smart_log(&smart_log, cfg.namespace_id, devicename);
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
static int get_additional_smart_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
struct nvme_additional_smart_log smart_log;
int err, fmt;
char *desc = "Get Intel vendor specific additional smart log (optionally, "\
"for the specified namespace), and show it.";
const char *namespace = "(optional) desired namespace";
const char *raw = "dump output in binary format";
struct config {
__u32 namespace_id;
int raw_binary;
char *output_format;
};
struct config cfg = {
.namespace_id = 0xffffffff,
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace},
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw},
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format },
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
fmt = validate_output_format(cfg.output_format);
if (fmt < 0)
return fmt;
if (cfg.raw_binary)
fmt = BINARY;
err = nvme_intel_smart_log(fd, cfg.namespace_id, &smart_log);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)&smart_log, sizeof(smart_log));
if (fmt == JSON)
json_add_smart_log(&smart_log, cfg.namespace_id, devicename);
else
show_intel_smart_log(&smart_log, cfg.namespace_id, devicename);
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
static int get_error_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve specified number of "\
"error log entries from a given device (or "\
"namespace) in either decoded format (default) or binary.";
const char *namespace_id = "desired namespace";
const char *log_entries = "number of entries to retrieve";
const char *raw_binary = "dump in binary format";
struct nvme_id_ctrl ctrl;
int err, fmt;
struct config {
__u32 namespace_id;
__u32 log_entries;
int raw_binary;
char *output_format;
};
struct config cfg = {
.namespace_id = 0xffffffff,
.log_entries = 64,
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"log-entries", 'e', "NUM", CFG_POSITIVE, &cfg.log_entries, required_argument, log_entries},
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format },
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
fmt = validate_output_format(cfg.output_format);
if (fmt < 0)
return fmt;
if (cfg.raw_binary)
fmt = BINARY;
if (!cfg.log_entries) {
fprintf(stderr, "non-zero log-entries is required param\n");
return EINVAL;
}
err = nvme_identify_ctrl(fd, &ctrl);
cfg.log_entries = min(cfg.log_entries, ctrl.elpe + 1);
if (err) {
fprintf(stderr, "could not identify controller\n");
return ENODEV;
} else {
struct nvme_error_log_page *err_log;
err_log = calloc(cfg.log_entries, sizeof(struct nvme_error_log_page));
if (!err_log) {
fprintf(stderr, "could not alloc buffer for error log\n");
return ENOMEM;
}
err = nvme_error_log(fd, cfg.namespace_id, cfg.log_entries, err_log);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)err_log, sizeof(err_log));
else if (fmt == JSON)
json_error_log(err_log, cfg.log_entries, devicename);
else
show_error_log(err_log, cfg.log_entries, devicename);
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
free(err_log);
}
return err;
}
static int get_fw_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve the firmware log for the "\
"specified device in either decoded format (default) or binary.";
const char *raw_binary = "use binary output";
int err, fmt;
struct nvme_firmware_log_page fw_log;
struct config {
int raw_binary;
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format },
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
fmt = validate_output_format(cfg.output_format);
if (fmt < 0)
return fmt;
if (cfg.raw_binary)
fmt = BINARY;
err = nvme_fw_log(fd, &fw_log);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)&fw_log, sizeof(fw_log));
else if (fmt == JSON)
json_fw_log(&fw_log, devicename);
else
show_fw_log(&fw_log, devicename);
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
else
perror("fw log");
return err;
}
static int get_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve desired number of bytes "\
"from a given log on a specified device in either "\
"hex-dump (default) or binary format";
const char *namespace_id = "desired namespace";
const char *log_id = "identifier of log to retrieve";
const char *log_len = "how many bytes to retrieve";
const char *raw_binary = "output in raw format";
int err;
struct config {
__u32 namespace_id;
__u32 log_id;
__u32 log_len;
int raw_binary;
};
struct config cfg = {
.namespace_id = 0xffffffff,
.log_id = 0,
.log_len = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"log-id", 'i', "NUM", CFG_POSITIVE, &cfg.log_id, required_argument, log_id},
{"log-len", 'l', "NUM", CFG_POSITIVE, &cfg.log_len, required_argument, log_len},
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (!cfg.log_len) {
fprintf(stderr, "non-zero log-len is required param\n");
return EINVAL;
} else {
unsigned char *log;
log = malloc(cfg.log_len);
if (!log) {
fprintf(stderr, "could not alloc buffer for log\n");
return EINVAL;
}
err = nvme_get_log(fd, cfg.namespace_id, cfg.log_id, cfg.log_len, log);
if (!err) {
if (!cfg.raw_binary) {
printf("Device:%s log-id:%d namespace-id:%#x\n",
devicename, cfg.log_id,
cfg.namespace_id);
d(log, cfg.log_len, 16, 1);
} else
d_raw((unsigned char *)log, cfg.log_len);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
free(log);
return err;
}
}
static int list_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Show controller list information for the subsystem the "\
"given device is part of, or optionally controllers attached to a specific namespace.";
const char *controller = "controller to display";
const char *namespace_id = "optional namespace attached to controller";
int err, i;
struct nvme_controller_list *cntlist;
struct config {
__u16 cntid;
__u32 namespace_id;
};
struct config cfg = {
.cntid = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"cntid", 'c', "NUM", CFG_SHORT, &cfg.cntid, required_argument, controller},
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (posix_memalign((void *)&cntlist, getpagesize(), 0x1000))
return ENOMEM;
err = nvme_identify_ctrl_list(fd, cfg.namespace_id, cfg.cntid, cntlist);
if (!err) {
__u16 num = le16_to_cpu(cntlist->num);
for (i = 0; i < (min(num, 2048)); i++)
printf("[%4u]:%#x\n", i, le16_to_cpu(cntlist->identifier[i]));
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x) cntid:%d\n",
nvme_status_to_string(err), err, cfg.cntid);
return err;
}
static int list_ns(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "For the specified device, show the "\
"namespace list in a NVMe subsystem, optionally starting with a given namespace";
const char *namespace_id = "namespace number returned list should to start after";
const char *all = "show all namespaces in the subsystem, whether attached or inactive";
int err, i;
__u32 ns_list[1024];
struct config {
__u32 namespace_id;
int all;
};
struct config cfg = {
.namespace_id = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"all", 'a', "", CFG_NONE, &cfg.all, no_argument, all},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
err = nvme_identify_ns_list(fd, cfg.namespace_id, !!cfg.all, ns_list);
if (!err) {
for (i = 0; i < 1024; i++)
if (ns_list[i])
printf("[%4u]:%#x\n", i, ns_list[i]);
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x) NSID:%d\n",
nvme_status_to_string(err), err, cfg.namespace_id);
return err;
}
static int delete_ns(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Delete the given namespace by "\
"sending a namespace management command to "\
"the provided device. All controllers should be detached from "\
"the namespace prior to namespace deletion. A namespace ID "\
"becomes inactive when that namespace is detached or, if "\
"the namespace is not already inactive, once deleted.";
const char *namespace_id = "namespace to delete";
int err;
struct config {
__u32 namespace_id;
};
struct config cfg = {
.namespace_id = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (!cfg.namespace_id) {
fprintf(stderr, "%s: namespace-id parameter required\n",
cmd->name);
return EINVAL;
}
err = nvme_ns_delete(fd, cfg.namespace_id);
if (!err)
printf("%s: Success, deleted nsid:%d\n", cmd->name,
cfg.namespace_id);
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
else
fprintf(stderr, "system error:(%x)\n", err);
return err;
}
static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, struct command *cmd)
{
int err, num, i, list[2048];
__u16 ctrlist[2048];
const char *namespace_id = "namespace to attach";
const char *cont = "optional comma-sep controllers list";
struct config {
char *cntlist;
__u32 namespace_id;
};
struct config cfg = {
.cntlist = "",
.namespace_id = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"controllers", 'c', "LIST", CFG_STRING, &cfg.cntlist, required_argument, cont},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (!cfg.namespace_id) {
fprintf(stderr, "%s: namespace-id parameter required\n",
cmd->name);
return EINVAL;
}
num = argconfig_parse_comma_sep_array(cfg.cntlist,
list, 2047);
for (i = 0; i < num; i++)
ctrlist[i] = (uint16_t)list[i];
if (attach)
err = nvme_ns_attach_ctrls(fd, cfg.namespace_id, num, ctrlist);
else
err = nvme_ns_detach_ctrls(fd, cfg.namespace_id, num, ctrlist);
if (!err)
printf("%s: Success, nsid:%d\n", cmd->name, cfg.namespace_id);
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
else
fprintf(stderr, "system error:(%x)\n", err);
return err;
}
static int attach_ns(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Attach the given namespace to the "\
"given controller or comma-sep list of controllers. ID of the "\
"given namespace becomes active upon attachment to a "\
"controller. A namespace must be attached to a controller "\
"before IO commands may be directed to that namespace.";
return nvme_attach_ns(argc, argv, 1, desc, cmd);
}
static int detach_ns(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Detach the given namespace from the "\
"given controller; de-activates the given namespace's ID. A "\
"namespace must be attached to a controller before IO "\
"commands may be directed to that namespace.";
return nvme_attach_ns(argc, argv, 0, desc, cmd);
}
static int create_ns(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Send a namespace management command "\
"to the specified device to create a namespace with the given "\
"parameters. The next available namespace ID is used for the "\
"create operation. Note that create-ns does not attach the "\
"namespace to a controller, the attach-ns command is needed.";
int err = 0;
__u32 nsid;
struct config {
__u64 nsze;
__u64 ncap;
__u8 flbas;
__u8 dps;
__u8 nmic;
};
struct config cfg = {
};
const char *nsze = "size of ns";
const char *ncap = "capacity of ns";
const char *flbas = "FLBA size";
const char *dps = "data protection capabilities";
const char *nmic = "multipath and sharing capabilities";
const struct argconfig_commandline_options command_line_options[] = {
{"nsze", 's', "NUM", CFG_LONG_SUFFIX, &cfg.nsze, required_argument, nsze},
{"ncap", 'c', "NUM", CFG_LONG_SUFFIX, &cfg.ncap, required_argument, ncap},
{"flbas", 'f', "NUM", CFG_BYTE, &cfg.flbas, required_argument, flbas},
{"dps", 'd', "NUM", CFG_BYTE, &cfg.dps, required_argument, dps},
{"nmic", 'm', "NUM", CFG_BYTE, &cfg.nmic, required_argument, nmic},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
err = nvme_ns_create(fd, cfg.nsze, cfg.ncap, cfg.flbas, cfg.dps, cfg.nmic, &nsid);
if (!err)
printf("%s: Success, created nsid:%d\n", cmd->name, nsid);
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
static char *nvme_char_from_block(char *block)
{
char slen[16];
unsigned len;
if (strncmp("nvme", block, 4)) {
fprintf(stderr, "Device %s is not a nvme device.", block);
exit(-1);
}
sscanf(block, "nvme%d", &len);
sprintf(slen, "%d", len);
block[4 + strlen(slen)] = 0;
return block;
}
static void *get_registers(void)
{
int pci_fd;
char *base, path[512];
void *membase;
base = nvme_char_from_block((char *)devicename);
sprintf(path, "/sys/class/nvme/%s/device/resource0", base);
pci_fd = open(path, O_RDONLY);
if (pci_fd < 0) {
sprintf(path, "/sys/class/misc/%s/device/resource0", base);
pci_fd = open(path, O_RDONLY);
}
if (pci_fd < 0) {
fprintf(stderr, "%s did not find a pci resource\n", base);
exit(ENODEV);
}
membase = mmap(NULL, getpagesize(), PROT_READ, MAP_SHARED, pci_fd, 0);
if (membase == MAP_FAILED) {
fprintf(stderr, "%s failed to map\n", base);
exit(ENODEV);
}
return membase;
}
static void print_list_item(struct list_item list_item)
{
double nsze = le64_to_cpu(list_item.ns.nsze);
double nuse = le64_to_cpu(list_item.ns.nuse);
long long int lba = list_item.ns.lbaf[(list_item.ns.flbas & 0x0f)].ds;
lba = (1 << lba);
nsze *= lba;
nuse *= lba;
const char *s_suffix = suffix_si_get(&nsze);
const char *u_suffix = suffix_si_get(&nuse);
const char *l_suffix = suffix_binary_get(&lba);
char usage[128];
sprintf(usage,"%6.2f %2sB / %6.2f %2sB", nuse, u_suffix,
nsze, s_suffix);
char format[128];
sprintf(format,"%3.0f %2sB + %2d B", (double)lba, l_suffix,
list_item.ns.lbaf[(list_item.ns.flbas & 0x0f)].ms);
printf("%-16s %-*.*s %-*.*s %-9d %-26s %-16s %-.*s\n", list_item.node,
(int)sizeof(list_item.ctrl.sn), (int)sizeof(list_item.ctrl.sn), list_item.ctrl.sn,
(int)sizeof(list_item.ctrl.mn), (int)sizeof(list_item.ctrl.mn), list_item.ctrl.mn,
list_item.nsid, usage, format, (int)sizeof(list_item.ctrl.fr), list_item.ctrl.fr);
}
static void print_list_items(struct list_item *list_items, unsigned len)
{
unsigned i;
printf("%-16s %-20s %-40s %-9s %-26s %-16s %-8s\n",
"Node", "SN", "Model", "Namespace", "Usage", "Format", "FW Rev");
printf("%-16s %-20s %-40s %-9s %-26s %-16s %-8s\n",
"----------------", "--------------------", "----------------------------------------",
"---------", "--------------------------", "----------------", "--------");
for (i = 0 ; i < len ; i++)
print_list_item(list_items[i]);
}
static int get_nvme_info(int fd, struct list_item *item, const char *node)
{
int err;
err = nvme_identify_ctrl(fd, &item->ctrl);
if (err)
return err;
item->nsid = nvme_get_nsid(fd);
err = nvme_identify_ns(fd, item->nsid,
0, &item->ns);
if (err)
return err;
strcpy(item->node, node);
item->block = S_ISBLK(nvme_stat.st_mode);
return 0;
}
static const char *dev = "/dev/";
/* Assume every block device starting with /dev/nvme is an nvme namespace */
static int scan_dev_filter(const struct dirent *d)
{
char path[256];
struct stat bd;
int ctrl, ns, part;
if (d->d_name[0] == '.')
return 0;
if (strstr(d->d_name, "nvme")) {
snprintf(path, sizeof(path), "%s%s", dev, d->d_name);
if (stat(path, &bd))
return 0;
if (!S_ISBLK(bd.st_mode))
return 0;
if (sscanf(d->d_name, "nvme%dn%dp%d", &ctrl, &ns, &part) == 3)
return 0;
return 1;
}
return 0;
}
static int list(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
char path[256];
struct dirent **devices;
struct list_item *list_items;
unsigned int i, n, fd, ret;
int fmt;
const char *desc = "Retrieve basic information for the given device";
struct config {
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
const struct argconfig_commandline_options opts[] = {
{"output-format", 'o', "FMT",
CFG_STRING, &cfg.output_format,
required_argument, "Output Format: normal|json"},
{NULL}
};
argconfig_parse(argc, argv, desc, opts, &cfg, sizeof(cfg));
fmt = validate_output_format(cfg.output_format);
if (fmt != JSON && fmt != NORMAL)
return -EINVAL;
n = scandir(dev, &devices, scan_dev_filter, alphasort);
if (n <= 0)
return n;
list_items = calloc(n, sizeof(*list_items));
if (!list_items)
return ENOMEM;
for (i = 0; i < n; i++) {
snprintf(path, sizeof(path), "%s%s", dev, devices[i]->d_name);
fd = open(path, O_RDONLY);
ret = get_nvme_info(fd, &list_items[i], path);
if (ret)
return ret;
}
if (fmt == JSON)
json_print_list_items(list_items, n);
else
print_list_items(list_items, n);
for (i = 0; i < n; i++)
free(devices[i]);
free(devices);
free(list_items);
return 0;
}
static int get_nsid(void)
{
int nsid = nvme_get_nsid(fd);
if (nsid <= 0) {
fprintf(stderr,
"%s: failed to return namespace id\n",
devicename);
exit(errno);
}
return nsid;
}
int __id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin, void (*vs)(__u8 *vs))
{
const char *desc = "Send an Identify Controller command to "\
"the given device and report information about the specified "\
"controller in human-readable or "\
"binary format. May also return vendor-specific "\
"controller attributes in hex-dump if requested.";
const char *vendor_specific = "dump binary vendor infos";
const char *raw_binary = "show infos in binary format";
const char *human_readable = "show infos in readable format";
int err, fmt;
unsigned int flags = 0;
struct nvme_id_ctrl ctrl;
struct config {
int vendor_specific;
int raw_binary;
int human_readable;
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"vendor-specific", 'v', "", CFG_NONE, &cfg.vendor_specific, no_argument, vendor_specific},
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{"human-readable", 'H', "", CFG_NONE, &cfg.human_readable, no_argument, human_readable},
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format },
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
fmt = validate_output_format(cfg.output_format);
if (fmt < 0)
return fmt;
if (cfg.raw_binary)
fmt = BINARY;
if (cfg.vendor_specific)
flags |= VS;
if (cfg.human_readable)
flags |= HUMAN;
err = nvme_identify_ctrl(fd, &ctrl);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)&ctrl, sizeof(ctrl));
else if (fmt == JSON)
json_nvme_id_ctrl(&ctrl, flags);
else {
printf("NVME Identify Controller:\n");
__show_nvme_id_ctrl(&ctrl, flags, vs);
}
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
static int id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
return __id_ctrl(argc, argv, cmd, plugin, NULL);
}
static int id_ns(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Send an Identify Namespace command to the "\
"given device, returns properties of the specified namespace "\
"in either human-readable or binary format. Can also return "\
"binary vendor-specific namespace attributes.";
const char *force = "Return this namespace, even if not attaced (1.2 devices only)";
const char *vendor_specific = "dump binary vendor infos";
const char *raw_binary = "show infos in binary format";
const char *human_readable = "show infos in readable format";
const char *namespace_id = "identifier of desired namespace";
struct nvme_id_ns ns;
int err, fmt;
unsigned int flags = 0;
struct config {
__u32 namespace_id;
int vendor_specific;
int raw_binary;
int human_readable;
int force;
char *output_format;
};
struct config cfg = {
.namespace_id = 0,
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"force", 'f', "FLAG", CFG_NONE, &cfg.force, no_argument, force},
{"vendor-specific", 'v', "FLAG", CFG_NONE, &cfg.vendor_specific, no_argument, vendor_specific},
{"raw-binary", 'b', "FLAG", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{"human-readable", 'H', "FLAG", CFG_NONE, &cfg.human_readable, no_argument, human_readable},
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format },
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
fmt = validate_output_format(cfg.output_format);
if (fmt < 0)
return fmt;
if (cfg.raw_binary)
fmt = BINARY;
if (cfg.vendor_specific)
flags |= VS;
if (cfg.human_readable)
flags |= HUMAN;
if (!cfg.namespace_id)
cfg.namespace_id = get_nsid();
err = nvme_identify_ns(fd, cfg.namespace_id, cfg.force, &ns);
if (!err) {
if (fmt == BINARY)
d_raw((unsigned char *)&ns, sizeof(ns));
else if (fmt == JSON)
json_nvme_id_ns(&ns, flags);
else {
printf("NVME Identify Namespace %d:\n", cfg.namespace_id);
show_nvme_id_ns(&ns, flags);
}
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x) NSID:%d\n",
nvme_status_to_string(err), err, cfg.namespace_id);
return err;
}
static int get_ns_id(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
int nsid;
open_dev(argv[1]);
nsid = nvme_get_nsid(fd);
if (nsid <= 0) {
perror(devicename);
exit(errno);
}
printf("%s: namespace-id:%d\n", devicename, nsid);
return 0;
}
static int get_feature(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Read operating parameters of the "\
"specified controller. Operating parameters are grouped "\
"and identified by Feature Identifiers; each Feature "\
"Identifier contains one or more attributes that may affect "\
"behaviour of the feature. Each Feature has three possible "\
"settings: default, saveable, and current. If a Feature is "\
"saveable, it may be modified by set-feature. Default values "\
"are vendor-specific and not changeable. Use set-feature to "\
"change saveable Features.";
const char *raw_binary = "show infos in binary format";
const char *namespace_id = "identifier of desired namespace";
const char *feature_id = "hexadecimal feature name";
const char *sel = "[0-3]: curr./default/saved/supp.";
const char *data_len = "buffer len (if) data is returned";
const char *cdw11 = "dword 11 for interrupt vector config";
const char *human_readable = "show infos in readable format";
int err;
__u32 result;
void *buf = NULL;
struct config {
__u32 namespace_id;
__u32 feature_id;
__u8 sel;
__u32 cdw11;
__u32 data_len;
int raw_binary;
int human_readable;
};
struct config cfg = {
.namespace_id = 1,
.feature_id = 0,
.sel = 0,
.cdw11 = 0,
.data_len = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"feature-id", 'f', "NUM", CFG_POSITIVE, &cfg.feature_id, required_argument, feature_id},
{"sel", 's', "NUM", CFG_BYTE, &cfg.sel, required_argument, sel},
{"data-len", 'l', "NUM", CFG_POSITIVE, &cfg.data_len, required_argument, data_len},
{"raw-binary", 'b', "FLAG",CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{"cdw11", 'c', "NUM", CFG_POSITIVE, &cfg.cdw11, required_argument, cdw11},
{"human-readable", 'H', "FLAG",CFG_NONE, &cfg.human_readable, no_argument, human_readable},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (cfg.sel > 7) {
fprintf(stderr, "invalid 'select' param:%d\n", cfg.sel);
return EINVAL;
}
if (!cfg.feature_id) {
fprintf(stderr, "feature-id required param\n");
return EINVAL;
}
switch (cfg.feature_id) {
case NVME_FEAT_LBA_RANGE:
cfg.data_len = 4096;
break;
case NVME_FEAT_AUTO_PST:
cfg.data_len = 256;
break;
case NVME_FEAT_HOST_MEM_BUF:
cfg.data_len = 4096;
break;
case NVME_FEAT_HOST_ID:
cfg.data_len = 8;
break;
}
if (cfg.data_len) {
if (posix_memalign(&buf, getpagesize(), cfg.data_len))
exit(ENOMEM);
memset(buf, 0, cfg.data_len);
}
err = nvme_get_feature(fd, cfg.namespace_id, cfg.feature_id, cfg.sel, cfg.cdw11,
cfg.data_len, buf, &result);
if (!err) {
if (!cfg.raw_binary || !buf) {
printf("get-feature:%#02x (%s), %s value:%#08x\n", cfg.feature_id,
nvme_feature_to_string(cfg.feature_id),
nvme_select_to_string(cfg.sel), result);
if (cfg.human_readable)
nvme_feature_show_fields(cfg.feature_id, result, buf);
else if (buf)
d(buf, cfg.data_len, 16, 1);
} else if (buf)
d_raw(buf, cfg.data_len);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
if (buf)
free(buf);
return err;
}
static int fw_download(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Copy all or part of a firmware image to "\
"a controller for future update. Optionally, specify how "\
"many KiB of the firmware to transfer at once. The offset will "\
"start at 0 and automatically adjust based on xfer size "\
"unless fw is split across multiple files. May be submitted "\
"while outstanding commands exist on the Admin and IO "\
"Submission Queues. Activate downloaded firmware with "\
"fw-activate, and then reset the device to apply the downloaded firmware.";
const char *fw = "firmware file (required)";
const char *xfer = "transfer chunksize limit";
const char *offset = "starting dword offset, default 0";
int err, fw_fd = -1;
unsigned int fw_size;
struct stat sb;
void *fw_buf;
struct config {
char *fw;
__u32 xfer;
__u32 offset;
};
struct config cfg = {
.fw = "",
.xfer = 4096,
.offset = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"fw", 'f', "FILE", CFG_STRING, &cfg.fw, required_argument, fw},
{"xfer", 'x', "NUM", CFG_POSITIVE, &cfg.xfer, required_argument, xfer},
{"offset", 'o', "NUM", CFG_POSITIVE, &cfg.offset, required_argument, offset},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
fw_fd = open(cfg.fw, O_RDONLY);
cfg.offset <<= 2;
if (fw_fd < 0) {
fprintf(stderr, "no firmware file provided\n");
return EINVAL;
}
err = fstat(fw_fd, &sb);
if (err < 0) {
perror("fstat");
exit(errno);
}
fw_size = sb.st_size;
if (fw_size & 0x3) {
fprintf(stderr, "Invalid size:%d for f/w image\n", fw_size);
return EINVAL;
}
if (posix_memalign(&fw_buf, getpagesize(), fw_size)) {
fprintf(stderr, "No memory for f/w size:%d\n", fw_size);
return ENOMEM;
}
if (cfg.xfer == 0 || cfg.xfer % 4096)
cfg.xfer = 4096;
if (read(fw_fd, fw_buf, fw_size) != ((ssize_t)(fw_size)))
return EIO;
while (fw_size > 0) {
cfg.xfer = min(cfg.xfer, fw_size);
err = nvme_fw_download(fd, cfg.offset, cfg.xfer, fw_buf);
if (err < 0) {
perror("fw-download");
exit(errno);
} else if (err != 0) {
fprintf(stderr, "NVME Admin command error:%s(%x)\n",
nvme_status_to_string(err), err);
break;
}
fw_buf += cfg.xfer;
fw_size -= cfg.xfer;
cfg.offset += cfg.xfer;
}
if (!err)
printf("Firmware download success\n");
return err;
}
static char *nvme_fw_status_reset_type(__u32 status)
{
switch (status) {
case NVME_SC_FW_NEEDS_CONV_RESET: return "conventional";
case NVME_SC_FW_NEEDS_SUBSYS_RESET: return "subsystem";
case NVME_SC_FW_NEEDS_RESET: return "any controller";
default: return "unknown";
}
}
static int fw_activate(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Verify downloaded firmware image and "\
"commit to specific firmware slot. Device is not automatically "\
"reset following firmware activation. A reset may be issued "\
"with an 'echo 1 > /sys/class/nvme/nvmeX/reset_controller'. "\
"Ensure nvmeX is the device you just activated before reset.";
const char *slot = "firmware slot to activate";
const char *action = "[0-2]: replacement action";
int err;
struct config {
__u8 slot;
__u8 action;
};
struct config cfg = {
.slot = 0,
.action = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"slot", 's', "NUM", CFG_BYTE, &cfg.slot, required_argument, slot},
{"action", 'a', "NUM", CFG_BYTE, &cfg.action, required_argument, action},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (cfg.slot > 7) {
fprintf(stderr, "invalid slot:%d\n", cfg.slot);
return EINVAL;
}
if (cfg.action > 3) {
fprintf(stderr, "invalid action:%d\n", cfg.action);
return EINVAL;
}
err = nvme_fw_activate(fd, cfg.slot, cfg.action);
if (err < 0)
perror("fw-activate");
else if (err != 0)
switch (err) {
case NVME_SC_FW_NEEDS_CONV_RESET:
case NVME_SC_FW_NEEDS_SUBSYS_RESET:
case NVME_SC_FW_NEEDS_RESET:
printf("Success activating firmware action:%d slot:%d, but firmware requires %s reset\n",
cfg.action, cfg.slot, nvme_fw_status_reset_type(err));
break;
default:
fprintf(stderr, "NVME Admin command error:%s(%x)\n",
nvme_status_to_string(err), err);
break;
}
else
printf("Success activating firmware action:%d slot:%d\n",
cfg.action, cfg.slot);
return err;
}
static void clear_args(int argc, char **argv)
{
int opt, long_index;
while ((opt = getopt_long(argc, (char **)argv, "", NULL,
&long_index)) != -1);
get_dev(argc, argv);
}
static int subsystem_reset(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
int err;
clear_args(argc, argv);
err = nvme_subsystem_reset(fd);
if (err < 0) {
perror("Subsystem-reset");
return errno;
}
return err;
}
static int reset(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
int err;
clear_args(argc, argv);
err = nvme_reset_controller(fd);
if (err < 0) {
perror("Reset");
return errno;
}
return err;
}
static inline uint32_t mmio_read32(void *addr)
{
__le32 *p = addr;
return le32_to_cpu(*p);
}
/* Access 64-bit registers as 2 32-bit; Some devices fail 64-bit MMIO. */
static inline __u64 mmio_read64(void *addr)
{
__le32 *p = addr;
return le32_to_cpu(*p) | ((uint64_t)le32_to_cpu(*(p + 1)) << 32);
}
static int show_registers(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Reads and shows the defined NVMe controller registers "\
"in binary or human-readable format";
const char *human_readable = "show info in readable format";
uint64_t cap, asq, acq;
uint32_t vs, intms, intmc, cc, csts, nssr, aqa, cmbsz, cmbloc;
void *bar;
struct config {
int human_readable;
};
struct config cfg = {
.human_readable = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"human-readable", 'H', "", CFG_NONE, &cfg.human_readable, no_argument, human_readable},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
bar = get_registers();
cap = mmio_read64(bar + NVME_REG_CAP);
vs = mmio_read32(bar + NVME_REG_VS);
intms = mmio_read32(bar + NVME_REG_INTMS);
intmc = mmio_read32(bar + NVME_REG_INTMC);
cc = mmio_read32(bar + NVME_REG_CC);
csts = mmio_read32(bar + NVME_REG_CSTS);
nssr = mmio_read32(bar + NVME_REG_NSSR);
aqa = mmio_read32(bar + NVME_REG_AQA);
asq = mmio_read64(bar + NVME_REG_ASQ);
acq = mmio_read64(bar + NVME_REG_ACQ);
cmbloc = mmio_read32(bar + NVME_REG_CMBLOC);
cmbsz = mmio_read32(bar + NVME_REG_CMBSZ);
if (cfg.human_readable) {
printf("cap : %"PRIx64"\n", cap);
show_registers_cap((struct nvme_bar_cap *)&cap);
printf("version : %x\n", vs);
show_registers_version(vs);
printf("intms : %x\n", intms);
printf("\tInterrupt Vector Mask Set (IVMS): %x\n\n", intms);
printf("intmc : %x\n", intmc);
printf("\tInterrupt Vector Mask Clear (IVMC): %x\n\n", intmc);
printf("cc : %x\n", cc);
show_registers_cc(cc);
printf("csts : %x\n", csts);
show_registers_csts(csts);
printf("nssr : %x\n", nssr);
printf("\tNVM Subsystem Reset Control (NSSRC): %u\n\n", nssr);
printf("aqa : %x\n", aqa);
show_registers_aqa(aqa);
printf("asq : %"PRIx64"\n", asq);
printf("\tAdmin Submission Queue Base (ASQB): %"PRIx64"\n",
asq);
printf("acq : %"PRIx64"\n", acq);
printf("\tAdmin Completion Queue Base (ACQB): %"PRIx64"\n",
acq);
printf("cmbloc : %x\n", cmbloc);
show_registers_cmbloc(cmbloc, cmbsz);
printf("cmbsz : %x\n", cmbsz);
show_registers_cmbsz(cmbsz);
} else {
printf("cap : %"PRIx64"\n", cap);
printf("version : %x\n", vs);
printf("intms : %x\n", intms);
printf("intmc : %x\n", intmc);
printf("cc : %x\n", cc);
printf("csts : %x\n", csts);
printf("nssr : %x\n", nssr);
printf("aqa : %x\n", aqa);
printf("asq : %"PRIx64"\n", asq);
printf("acq : %"PRIx64"\n", acq);
printf("cmbloc : %x\n", cmbloc);
printf("cmbsz : %x\n", cmbsz);
}
return 0;
}
static int format(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Re-format a specified namespace on the "\
"given device. Can erase all data in namespace (user "\
"data erase) or delete data encryption key if specified. "\
"Can also be used to change LBAF to change the namespaces reported physical block format.";
const char *namespace_id = "identifier of desired namespace";
const char *lbaf = "LBA format to apply (required)";
const char *ses = "[0-2]: secure erase";
const char *pil = "[0-1]: protection info location last/first 8 bytes of metadata";
const char *pi = "[0-3]: protection info off/Type 1/Type 2/Type 3";
const char *ms = "[0-1]: extended format off/on";
const char *reset = "Automatically reset the controller after successful format";
const char *timeout = "timeout value, in milliseconds";
int err;
struct config {
__u32 namespace_id;
__u32 timeout;
__u8 lbaf;
__u8 ses;
__u8 pi;
__u8 pil;
__u8 ms;
int reset;
};
struct config cfg = {
.namespace_id = 0xffffffff,
.timeout = 120000,
.lbaf = 0,
.ses = 0,
.pi = 0,
.reset = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"timeout", 't', "NUM", CFG_POSITIVE, &cfg.timeout, required_argument, timeout},
{"lbaf", 'l', "NUM", CFG_BYTE, &cfg.lbaf, required_argument, lbaf},
{"ses", 's', "NUM", CFG_BYTE, &cfg.ses, required_argument, ses},
{"pi", 'i', "NUM", CFG_BYTE, &cfg.pi, required_argument, pi},
{"pil", 'p', "NUM", CFG_BYTE, &cfg.pil, required_argument, pil},
{"ms", 'm', "NUM", CFG_BYTE, &cfg.ms, required_argument, ms},
{"reset", 'r', "FLAG", CFG_NONE, &cfg.reset, no_argument, reset},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
/* ses & pi checks set to 7 for forward-compatibility */
if (cfg.ses > 7) {
fprintf(stderr, "invalid secure erase settings:%d\n", cfg.ses);
return EINVAL;
}
if (cfg.lbaf > 15) {
fprintf(stderr, "invalid lbaf:%d\n", cfg.lbaf);
return EINVAL;
}
if (cfg.pi > 7) {
fprintf(stderr, "invalid pi:%d\n", cfg.pi);
return EINVAL;
}
if (cfg.pil > 1) {
fprintf(stderr, "invalid pil:%d\n", cfg.pil);
return EINVAL;
}
if (cfg.ms > 1) {
fprintf(stderr, "invalid ms:%d\n", cfg.ms);
return EINVAL;
}
if (S_ISBLK(nvme_stat.st_mode))
cfg.namespace_id = get_nsid();
err = nvme_format(fd, cfg.namespace_id, cfg.lbaf, cfg.ses, cfg.pi,
cfg.pil, cfg.ms, cfg.timeout);
if (err < 0)
perror("format");
else if (err != 0)
fprintf(stderr, "NVME Admin command error:%s(%x)\n",
nvme_status_to_string(err), err);
else {
printf("Success formatting namespace:%x\n", cfg.namespace_id);
ioctl(fd, BLKRRPART);
if (cfg.reset && S_ISCHR(nvme_stat.st_mode))
nvme_reset_controller(fd);
}
return err;
}
static int set_feature(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Modify the saveable or changeable "\
"current operating parameters of the controller. Operating "\
"parameters are grouped and identified by Feature "\
"Identifiers. Feature settings can be applied to the entire "\
"controller and all associated namespaces, or to only a few "\
"namespace(s) associated with the controller. Default values "\
"for each Feature are vendor-specific and may not be modified."\
"Use get-feature to determine which Features are supported by "\
"the controller and are saveable/changeable.";
const char *namespace_id = "desired namespace";
const char *feature_id = "hex feature name (required)";
const char *data_len = "buffer length if data required";
const char *data = "optional file for feature data (default stdin)";
const char *value = "new value of feature (required)";
const char *save = "specifies that the controller shall save the attribute";
int err;
__u32 result;
void *buf = NULL;
int ffd = STDIN_FILENO;
struct config {
char *file;
__u32 namespace_id;
__u32 feature_id;
__u32 value;
__u32 data_len;
int save;
};
struct config cfg = {
.file = "",
.namespace_id = 0,
.feature_id = 0,
.value = 0,
.data_len = 0,
.save = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"feature-id", 'f', "NUM", CFG_POSITIVE, &cfg.feature_id, required_argument, feature_id},
{"value", 'v', "NUM", CFG_POSITIVE, &cfg.value, required_argument, value},
{"data-len", 'l', "NUM", CFG_POSITIVE, &cfg.data_len, required_argument, data_len},
{"data", 'd', "FILE", CFG_STRING, &cfg.file, required_argument, data},
{"save", 's', "FLAG", CFG_NONE, &cfg.save, no_argument, save},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (!cfg.feature_id) {
fprintf(stderr, "feature-id required param\n");
return EINVAL;
}
if (cfg.feature_id == NVME_FEAT_LBA_RANGE)
cfg.data_len = 4096;
if (cfg.data_len) {
if (posix_memalign(&buf, getpagesize(), cfg.data_len))
exit(ENOMEM);
memset(buf, 0, cfg.data_len);
}
if (buf) {
if (strlen(cfg.file)) {
ffd = open(cfg.file, O_RDONLY);
if (ffd <= 0) {
fprintf(stderr, "no firmware file provided\n");
return -EINVAL;
}
}
if (read(ffd, (void *)buf, cfg.data_len) < 0) {
fprintf(stderr, "failed to read data buffer from input file\n");
return EINVAL;
}
}
err = nvme_set_feature(fd, cfg.namespace_id, cfg.feature_id, cfg.value, cfg.save,
cfg.data_len, buf, &result);
if (err < 0) {
perror("set-feature");
return errno;
} else if (!err) {
printf("set-feature:%02x (%s), value:%#08x\n", cfg.feature_id,
nvme_feature_to_string(cfg.feature_id), cfg.value);
if (buf) {
if (cfg.feature_id == NVME_FEAT_LBA_RANGE)
show_lba_range((struct nvme_lba_range_type *)buf,
result);
else
d(buf, cfg.data_len, 16, 1);
}
} else
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
if (buf)
free(buf);
return err;
}
static int sec_send(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
struct stat sb;
const char *desc = "Transfer security protocol data to "\
"a controller. Security Receives for the same protocol should be "\
"performed after Security Sends. The security protocol field "\
"associates Security Sends (security-send) and Security Receives "\
"(security-recv).";
const char *file = "transfer payload";
const char *secp = "security protocol (cf. SPC-4)";
const char *spsp = "security-protocol-specific (cf. SPC-4)";
const char *tl = "transfer length (cf. SPC-4)";
const char *namespace_id = "desired namespace";
const char *nssf = "NVMe Security Specific Field";
int err, sec_fd = -1;
void *sec_buf;
unsigned int sec_size;
__u32 result;
struct config {
__u32 namespace_id;
char *file;
__u8 nssf;
__u8 secp;
__u16 spsp;
__u32 tl;
};
struct config cfg = {
.file = "",
.secp = 0,
.spsp = 0,
.tl = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"file", 'f', "FILE", CFG_STRING, &cfg.file, required_argument, file},
{"nssf", 'N', "NUM", CFG_BYTE, &cfg.nssf, required_argument, nssf},
{"secp", 'p', "NUM", CFG_BYTE, &cfg.secp, required_argument, secp},
{"spsp", 's', "NUM", CFG_SHORT, &cfg.spsp, required_argument, spsp},
{"tl", 't', "NUM", CFG_POSITIVE, &cfg.tl, required_argument, tl},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
sec_fd = open(cfg.file, O_RDONLY);
if (sec_fd < 0) {
fprintf(stderr, "no firmware file provided\n");
return EINVAL;
}
err = fstat(sec_fd, &sb);
if (err < 0) {
perror("fstat");
return errno;
}
sec_size = sb.st_size;
if (posix_memalign(&sec_buf, getpagesize(), sec_size)) {
fprintf(stderr, "No memory for security size:%d\n", sec_size);
return ENOMEM;
}
err = nvme_sec_send(fd, cfg.namespace_id, cfg.nssf, cfg.spsp, cfg.secp,
cfg.tl, sec_size, sec_buf, &result);
if (err < 0)
return errno;
else if (err != 0)
fprintf(stderr, "NVME Security Send Command Error:%d\n", err);
else
printf("NVME Security Send Command Success:%d\n", result);
return err;
}
static int write_uncor(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
int err;
const char *desc = "The Write Uncorrectable command is used to set a "\
"range of logical blocks to invalid.";
const char *namespace_id = "desired namespace";
const char *start_block = "64-bit LBA of first block to access";
const char *block_count = "number of blocks (zeroes based) on device to access";
struct config {
__u64 start_block;
__u32 namespace_id;
__u16 block_count;
};
struct config cfg = {
.start_block = 0,
.namespace_id = 0,
.block_count = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"start-block", 's', "NUM", CFG_LONG_SUFFIX, &cfg.start_block, required_argument, start_block},
{"block-count", 'c', "NUM", CFG_SHORT, &cfg.block_count, required_argument, block_count},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (!cfg.namespace_id)
cfg.namespace_id = get_nsid();
err = nvme_write_uncorrectable(fd, cfg.namespace_id, cfg.start_block,
cfg.block_count);
if (err < 0)
return errno;
else if (err != 0)
fprintf(stderr, "NVME IO command error:%s(%x)\n",
nvme_status_to_string(err), err);
else
printf("NVME Write Uncorrectable Success\n");
return err;
}
static int write_zeroes(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
int err;
__u16 control = 0;
const char *desc = "The Write Zeroes command is used to set a "\
"range of logical blocks to zero.";
const char *namespace_id = "desired namespace";
const char *start_block = "64-bit LBA of first block to access";
const char *block_count = "number of blocks (zeroes based) on device to access";
const char *limited_retry = "limit media access attempts";
const char *force = "force device to commit data before command completes";
const char *prinfo = "PI and check field";
const char *ref_tag = "reference tag (for end to end PI)";
const char *app_tag_mask = "app tag mask (for end to end PI)";
const char *app_tag = "app tag (for end to end PI)";
struct config {
__u64 start_block;
__u32 namespace_id;
__u32 ref_tag;
__u32 app_tag;
__u16 block_count;
__u8 prinfo;
__u8 app_tag_mask;
int limited_retry;
int force_unit_access;
};
struct config cfg = {
.start_block = 0,
.block_count = 0,
.prinfo = 0,
.ref_tag = 0,
.app_tag_mask = 0,
.app_tag = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"start-block", 's', "NUM", CFG_LONG_SUFFIX, &cfg.start_block, required_argument, start_block},
{"block-count", 'c', "NUM", CFG_SHORT, &cfg.block_count, required_argument, block_count},
{"limited-retry", 'l', "", CFG_NONE, &cfg.limited_retry, no_argument, limited_retry},
{"force-unit-access", 'f', "", CFG_NONE, &cfg.force_unit_access, no_argument, force},
{"prinfo", 'p', "NUM", CFG_BYTE, &cfg.prinfo, required_argument, prinfo},
{"ref-tag", 'r', "NUM", CFG_POSITIVE, &cfg.ref_tag, required_argument, ref_tag},
{"app-tag-mask", 'm', "NUM", CFG_BYTE, &cfg.app_tag_mask, required_argument, app_tag_mask},
{"app-tag", 'a', "NUM", CFG_POSITIVE, &cfg.app_tag, required_argument, app_tag},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (cfg.prinfo > 0xf)
return EINVAL;
control |= (cfg.prinfo << 10);
if (cfg.limited_retry)
control |= NVME_RW_LR;
if (cfg.force_unit_access)
control |= NVME_RW_FUA;
if (!cfg.namespace_id)
cfg.namespace_id = get_nsid();
err = nvme_write_zeros(fd, cfg.namespace_id, cfg.start_block, cfg.block_count,
control, cfg.ref_tag, cfg.app_tag, cfg.app_tag_mask);
if (err < 0)
return errno;
else if (err != 0)
fprintf(stderr, "NVME IO command error:%s(%x)\n",
nvme_status_to_string(err), err);
else
printf("NVME Write Zeroes Success\n");
return err;
}
static int dsm(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "The Dataset Management command is used by the host to "\
"indicate attributes for ranges of logical blocks. This includes attributes "\
"for discarding unused blocks, data read and write frequency, access size, and other "\
"information that may be used to optimize performance and reliability.";
const char *namespace_id = "identifier of desired namespace";
const char *blocks = "Comma separated list of the number of blocks in each range";
const char *starting_blocks = "Comma separated list of the starting block in each range";
const char *context_attrs = "Comma separated list of the context attributes in each range";
const char *ad = "Attribute Deallocate";
const char *idw = "Attribute Integral Dataset for Write";
const char *idr = "Attribute Integral Dataset for Read";
const char *cdw11 = "All the command DWORD 11 attributes. Use instead of specifying individual attributes";
int err;
uint16_t nr, nc, nb, ns;
int ctx_attrs[256] = {0,};
int nlbs[256] = {0,};
unsigned long long slbas[256] = {0,};
struct nvme_dsm_range *dsm;
struct config {
char *ctx_attrs;
char *blocks;
char *slbas;
int ad;
int idw;
int idr;
__u32 cdw11;
__u32 namespace_id;
};
struct config cfg = {
.ctx_attrs = "",
.blocks = "",
.slbas = "",
.namespace_id = 0,
.ad = 0,
.idw = 0,
.idr = 0,
.cdw11 = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"ctx-attrs", 'a', "LIST", CFG_STRING, &cfg.ctx_attrs, required_argument, context_attrs},
{"blocks", 'b', "LIST", CFG_STRING, &cfg.blocks, required_argument, blocks},
{"slbs", 's', "LIST", CFG_STRING, &cfg.slbas, required_argument, starting_blocks},
{"ad", 'd', "FLAG", CFG_NONE, &cfg.ad, no_argument, ad},
{"idw", 'w', "FLAG", CFG_NONE, &cfg.idw, no_argument, idw},
{"idr", 'r', "FLAG", CFG_NONE, &cfg.idr, no_argument, idr},
{"cdw11", 'c', "NUM", CFG_POSITIVE, &cfg.cdw11, required_argument, cdw11},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
nc = argconfig_parse_comma_sep_array(cfg.ctx_attrs, ctx_attrs, array_len(ctx_attrs));
nb = argconfig_parse_comma_sep_array(cfg.blocks, nlbs, array_len(nlbs));
ns = argconfig_parse_comma_sep_array_long(cfg.slbas, slbas, array_len(slbas));
nr = max(nc, max(nb, ns));
if (!nr || nr > 256) {
fprintf(stderr, "No range definition provided\n");
return EINVAL;
}
if (!cfg.namespace_id)
cfg.namespace_id = get_nsid();
if (!cfg.cdw11)
cfg.cdw11 = (cfg.ad << 2) | (cfg.idw << 1) | (cfg.idr << 0);
dsm = nvme_setup_dsm_range((__u32 *)ctx_attrs, (__u32 *)nlbs, (__u64 *)slbas, nr);
err = nvme_dsm(fd, cfg.namespace_id, cfg.cdw11, dsm, nr);
if (err < 0) {
fprintf(stderr, "error:%x\n", err);
return errno;
} else if (err != 0)
fprintf(stderr, "NVME IO command error:%s(%x)\n",
nvme_status_to_string(err), err);
else
printf("NVMe DSM: success\n");
return err;
}
static int flush(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Commit data and metadata associated with "\
"given namespaces to nonvolatile media. Applies to all commands "\
"finished before the flush was submitted. Additional data may also be "\
"flushed by the controller, from any namespace, depending on controller and "\
"associated namespace status.";
const char *namespace_id = "identifier of desired namespace";
int err;
struct config {
__u32 namespace_id;
};
struct config cfg = {
.namespace_id = 0xffffffff,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
err = nvme_flush(fd, cfg.namespace_id);
if (err < 0)
return errno;
else if (err != 0)
fprintf(stderr, "NVME IO command error:%s(%x)\n",
nvme_status_to_string(err), err);
else
printf("NVMe Flush: success\n");
return err;
}
static int resv_acquire(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Obtain a reservation on a given "\
"namespace. Only one reservation is allowed at a time on a "\
"given namespace, though multiple controllers may register "\
"with that namespace. Namespace reservation will abort with "\
"status Reservation Conflict if the given namespace is "\
"already reserved.";
const char *namespace_id = "identifier of desired namespace";
const char *crkey = "current reservation key";
const char *prkey = "pre-empt reservation key";
const char *rtype = "hex reservation type";
const char *racqa = "reservation acquiry action";
const char *iekey = "ignore existing res. key";
int err;
struct config {
__u32 namespace_id;
__u64 crkey;
__u64 prkey;
__u8 rtype;
__u8 racqa;
int iekey;
};
struct config cfg = {
.namespace_id = 0,
.crkey = 0,
.prkey = 0,
.rtype = 0,
.racqa = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"crkey", 'c', "NUM", CFG_LONG_SUFFIX, &cfg.crkey, required_argument, crkey},
{"prkey", 'p', "NUM", CFG_LONG_SUFFIX, &cfg.prkey, required_argument, prkey},
{"rtype", 't', "NUM", CFG_BYTE, &cfg.rtype, required_argument, rtype},
{"racqa", 'a', "NUM", CFG_BYTE, &cfg.racqa, required_argument, racqa},
{"iekey", 'i', "", CFG_NONE, &cfg.iekey, no_argument, iekey},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (!cfg.namespace_id)
cfg.namespace_id = get_nsid();
if (cfg.racqa > 7) {
fprintf(stderr, "invalid racqa:%d\n", cfg.racqa);
return EINVAL;
}
err = nvme_resv_acquire(fd, cfg.namespace_id, cfg.rtype, cfg.racqa,
!!cfg.iekey, cfg.crkey, cfg.prkey);
if (err < 0)
return errno;
else if (err != 0)
fprintf(stderr, "NVME IO command error:%04x\n", err);
else
printf("NVME Reservation Acquire success\n");
return err;
}
static int resv_register(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Register, de-register, or "\
"replace a controller's reservation on a given namespace. "\
"Only one reservation at a time is allowed on any namespace.";
const char *namespace_id = "identifier of desired namespace";
const char *crkey = "current reservation key";
const char *iekey = "ignore existing res. key";
const char *nrkey = "new reservation key";
const char *rrega = "reservation registration action";
const char *cptpl = "change persistence through power loss setting";
int err;
struct config {
__u32 namespace_id;
__u64 crkey;
__u64 nrkey;
__u8 rrega;
__u8 cptpl;
int iekey;
};
struct config cfg = {
.namespace_id = 0,
.crkey = 0,
.nrkey = 0,
.rrega = 0,
.cptpl = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"crkey", 'c', "NUM", CFG_LONG_SUFFIX, &cfg.crkey, required_argument, crkey},
{"nrkey", 'k', "NUM", CFG_LONG_SUFFIX, &cfg.nrkey, required_argument, nrkey},
{"rrega", 'r', "NUM", CFG_BYTE, &cfg.rrega, required_argument, rrega},
{"cptpl", 'p', "NUM", CFG_BYTE, &cfg.cptpl, required_argument, cptpl},
{"iekey", 'i', "", CFG_NONE, &cfg.iekey, no_argument, iekey},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (!cfg.namespace_id)
cfg.namespace_id = get_nsid();
if (cfg.cptpl > 3) {
fprintf(stderr, "invalid cptpl:%d\n", cfg.cptpl);
return EINVAL;
}
err = nvme_resv_register(fd, cfg.namespace_id, cfg.rrega, cfg.cptpl,
!!cfg.iekey, cfg.crkey, cfg.nrkey);
if (err < 0)
return errno;
else if (err != 0)
fprintf(stderr, "NVME IO command error:%04x\n", err);
else
printf("NVME Reservation success\n");
return err;
}
static int resv_release(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Releases reservation held on a "\
"namespace by the given controller. If rtype != current reservation"\
"type, release will fails. If the given controller holds no "\
"reservation on the namespace or is not the namespace's current "\
"reservation holder, the release command completes with no "\
"effect. If the reservation type is not Write Exclusive or "\
"Exclusive Access, all registrants on the namespace except "\
"the issuing controller are notified.";
const char *namespace_id = "desired namespace";
const char *crkey = "current reservation key";
const char *iekey = "ignore existing res. key";
const char *rtype = "hex reservation type";
const char *rrela = "reservation release action";
int err;
struct config {
__u32 namespace_id;
__u64 crkey;
__u8 rtype;
__u8 rrela;
__u8 iekey;
};
struct config cfg = {
.namespace_id = 0,
.crkey = 0,
.rtype = 0,
.rrela = 0,
.iekey = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"crkey", 'c', "NUM", CFG_LONG_SUFFIX, &cfg.crkey, required_argument, crkey},
{"rtype", 't', "NUM", CFG_BYTE, &cfg.rtype, required_argument, rtype},
{"rrela", 'a', "NUM", CFG_BYTE, &cfg.rrela, required_argument, rrela},
{"iekey", 'i', "NUM", CFG_BYTE, &cfg.iekey, required_argument, iekey},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (!cfg.namespace_id)
cfg.namespace_id = get_nsid();
if (cfg.iekey > 1) {
fprintf(stderr, "invalid iekey:%d\n", cfg.iekey);
return EINVAL;
}
if (cfg.rrela > 7) {
fprintf(stderr, "invalid rrela:%d\n", cfg.rrela);
return EINVAL;
}
err = nvme_resv_release(fd, cfg.namespace_id, cfg.rtype, cfg.rrela,
!!cfg.iekey, cfg.crkey);
if (err < 0)
return errno;
else if (err != 0)
fprintf(stderr, "NVME IO command error:%04x\n", err);
else
printf("NVME Reservation Release success\n");
return err;
}
static int resv_report(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Returns Reservation Status data "\
"structure describing any existing reservations on and the "\
"status of a given namespace. Namespace Reservation Status "\
"depends on the number of controllers registered for that "\
"namespace.";
const char *namespace_id = "identifier of desired namespace";
const char *numd = "number of dwords to transfer";
const char *raw_binary = "dump output in binary format";
int err, fmt;
struct nvme_reservation_status *status;
struct config {
__u32 namespace_id;
__u32 numd;
int raw_binary;
char *output_format;
};
struct config cfg = {
.namespace_id = 0,
.numd = 0,
.output_format = "normal",
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"numd", 'd', "NUM", CFG_POSITIVE, &cfg.numd, required_argument, numd},
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format, required_argument, output_format },
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
fmt = validate_output_format(cfg.output_format);
if (fmt < 0)
return fmt;
if (cfg.raw_binary)
fmt = BINARY;
if (!cfg.namespace_id)
cfg.namespace_id = get_nsid();
if (!cfg.numd || cfg.numd > (0x1000 >> 2))
cfg.numd = 0x1000 >> 2;
if (posix_memalign((void **)&status, getpagesize(), cfg.numd << 2)) {
fprintf(stderr, "No memory for resv report:%d\n", cfg.numd << 2);
return ENOMEM;
}
err = nvme_resv_report(fd, cfg.namespace_id, cfg.numd, status);
if (err < 0)
return errno;
else if (err != 0)
fprintf(stderr, "NVME IO command error:%04x\n", err);
else {
if (fmt == BINARY)
d_raw((unsigned char *)status, cfg.numd << 2);
else if (fmt == JSON)
json_nvme_resv_report(status);
else {
printf("NVME Reservation Report success\n");
show_nvme_resv_report(status);
}
}
return err;
}
static int submit_io(int opcode, char *command, const char *desc,
int argc, char **argv)
{
struct timeval start_time, end_time;
void *buffer, *mbuffer = NULL;
int err = 0;
int dfd, mfd;
int flags = opcode & 1 ? O_RDONLY : O_WRONLY | O_CREAT;
int mode = S_IRUSR | S_IWUSR |S_IRGRP | S_IWGRP| S_IROTH;
__u16 control = 0;
int phys_sector_size = 0;
long long buffer_size = 0;
const char *start_block = "64-bit addr of first block to access";
const char *block_count = "number of blocks (zeroes based) on device to access";
const char *data_size = "size of data in bytes";
const char *metadata_size = "size of metadata in bytes";
const char *ref_tag = "reference tag (for end to end PI)";
const char *data = "data file";
const char *metadata = "metadata file";
const char *prinfo = "PI and check field";
const char *app_tag_mask = "app tag mask (for end to end PI)";
const char *app_tag = "app tag (for end to end PI)";
const char *limited_retry = "limit num. media access attempts";
const char *latency = "output latency statistics";
const char *force = "force device to commit data before command completes";
const char *show = "show command before sending";
const char *dry = "show command instead of sending";
struct config {
__u64 start_block;
__u16 block_count;
__u64 data_size;
__u64 metadata_size;
__u32 ref_tag;
char *data;
char *metadata;
__u8 prinfo;
__u8 app_tag_mask;
__u32 app_tag;
int limited_retry;
int force_unit_access;
int show;
int dry_run;
int latency;
};
struct config cfg = {
.start_block = 0,
.block_count = 0,
.data_size = 0,
.metadata_size = 0,
.ref_tag = 0,
.data = "",
.metadata = "",
.prinfo = 0,
.app_tag_mask = 0,
.app_tag = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"start-block", 's', "NUM", CFG_LONG_SUFFIX, &cfg.start_block, required_argument, start_block},
{"block-count", 'c', "NUM", CFG_SHORT, &cfg.block_count, required_argument, block_count},
{"data-size", 'z', "NUM", CFG_LONG_SUFFIX, &cfg.data_size, required_argument, data_size},
{"metadata-size", 'y', "NUM", CFG_LONG_SUFFIX, &cfg.metadata_size, required_argument, metadata_size},
{"ref-tag", 'r', "NUM", CFG_POSITIVE, &cfg.ref_tag, required_argument, ref_tag},
{"data", 'd', "FILE", CFG_STRING, &cfg.data, required_argument, data},
{"metadata", 'M', "FILE", CFG_STRING, &cfg.metadata, required_argument, metadata},
{"prinfo", 'p', "NUM", CFG_BYTE, &cfg.prinfo, required_argument, prinfo},
{"app-tag-mask", 'm', "NUM", CFG_BYTE, &cfg.app_tag_mask, required_argument, app_tag_mask},
{"app-tag", 'a', "NUM", CFG_POSITIVE, &cfg.app_tag, required_argument, app_tag},
{"limited-retry", 'l', "", CFG_NONE, &cfg.limited_retry, no_argument, limited_retry},
{"force-unit-access", 'f', "", CFG_NONE, &cfg.force_unit_access, no_argument, force},
{"show-command", 'v', "", CFG_NONE, &cfg.show, no_argument, show},
{"dry-run", 'w', "", CFG_NONE, &cfg.dry_run, no_argument, dry},
{"latency", 't', "", CFG_NONE, &cfg.latency, no_argument, latency},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
dfd = mfd = opcode & 1 ? STDIN_FILENO : STDOUT_FILENO;
if (cfg.prinfo > 0xf)
return EINVAL;
control |= (cfg.prinfo << 10);
if (cfg.limited_retry)
control |= NVME_RW_LR;
if (cfg.force_unit_access)
control |= NVME_RW_FUA;
if (strlen(cfg.data)){
dfd = open(cfg.data, flags, mode);
if (dfd < 0) {
perror(cfg.data);
return EINVAL;
}
mfd = dfd;
}
if (strlen(cfg.metadata)){
mfd = open(cfg.metadata, flags, mode);
if (mfd < 0) {
perror(cfg.data);
return EINVAL;
}
}
if (!cfg.data_size) {
fprintf(stderr, "data size not provided\n");
return EINVAL;
}
if (ioctl(fd, BLKPBSZGET, &phys_sector_size) < 0)
return errno;
buffer_size = (cfg.block_count + 1) * phys_sector_size;
if (cfg.data_size < buffer_size) {
fprintf(stderr, "Rounding data size to fit block count (%lld bytes)\n",
buffer_size);
} else {
buffer_size = cfg.data_size;
}
if (posix_memalign(&buffer, getpagesize(), buffer_size))
return ENOMEM;
memset(buffer, 0, cfg.data_size);
if (cfg.metadata_size) {
mbuffer = malloc(cfg.metadata_size);
if (!mbuffer) {
free(buffer);
return ENOMEM;
}
}
if ((opcode & 1) && read(dfd, (void *)buffer, cfg.data_size) < 0) {
fprintf(stderr, "failed to read data buffer from input file\n");
err = EINVAL;
goto free_and_return;
}
if ((opcode & 1) && cfg.metadata_size &&
read(mfd, (void *)mbuffer, cfg.metadata_size) < 0) {
fprintf(stderr, "failed to read meta-data buffer from input file\n");
err = EINVAL;
goto free_and_return;
}
if (cfg.show) {
printf("opcode : %02x\n", opcode);
printf("flags : %02x\n", 0);
printf("control : %04x\n", control);
printf("nblocks : %04x\n", cfg.block_count);
printf("rsvd : %04x\n", 0);
printf("metadata : %"PRIx64"\n", (uint64_t)(uintptr_t)mbuffer);
printf("addr : %"PRIx64"\n", (uint64_t)(uintptr_t)buffer);
printf("sbla : %"PRIx64"\n", (uint64_t)cfg.start_block);
printf("dsmgmt : %08x\n", 0);
printf("reftag : %08x\n", cfg.ref_tag);
printf("apptag : %04x\n", cfg.app_tag);
printf("appmask : %04x\n", cfg.app_tag_mask);
if (cfg.dry_run)
goto free_and_return;
}
gettimeofday(&start_time, NULL);
err = nvme_io(fd, opcode, cfg.start_block, cfg.block_count, control, 0,
cfg.ref_tag, cfg.app_tag, cfg.app_tag_mask, buffer, mbuffer);
gettimeofday(&end_time, NULL);
if (cfg.latency)
printf(" latency: %s: %llu us\n",
command, elapsed_utime(start_time, end_time));
if (err < 0)
perror("submit-io");
else if (err)
printf("%s:%s(%04x)\n", command, nvme_status_to_string(err), err);
else {
if (!(opcode & 1) && write(dfd, (void *)buffer, cfg.data_size) < 0) {
fprintf(stderr, "failed to write buffer to output file\n");
err = EINVAL;
goto free_and_return;
} else if (!(opcode & 1) && cfg.metadata_size &&
write(mfd, (void *)mbuffer, cfg.metadata_size) < 0) {
fprintf(stderr, "failed to write meta-data buffer to output file\n");
err = EINVAL;
goto free_and_return;
} else
fprintf(stderr, "%s: Success\n", command);
}
free_and_return:
free(buffer);
if (cfg.metadata_size)
free(mbuffer);
return err;
}
static int compare(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Compare specified logical blocks on "\
"device with specified data buffer; return failure if buffer "\
"and block(s) are dissimilar";
return submit_io(nvme_cmd_compare, "compare", desc, argc, argv);
}
static int read_cmd(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Copy specified logical blocks on the given "\
"device to specified data buffer (default buffer is stdout).";
return submit_io(nvme_cmd_read, "read", desc, argc, argv);
}
static int write_cmd(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Copy from provided data buffer (default "\
"buffer is stdin) to specified logical blocks on the given "\
"device.";
return submit_io(nvme_cmd_write, "write", desc, argc, argv);
}
static int sec_recv(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Obtain results of one or more "\
"previously submitted security-sends. Results, and association "\
"between Security Send and Receive, depend on the security "\
"protocol field as they are defined by the security protocol "\
"used. A Security Receive must follow a Security Send made with "\
"the same security protocol.";
const char *size = "size of buffer (prints to stdout on success)";
const char *secp = "security protocol (cf. SPC-4)";
const char *spsp = "security-protocol-specific (cf. SPC-4)";
const char *al = "allocation length (cf. SPC-4)";
const char *raw_binary = "dump output in binary format";
const char *namespace_id = "desired namespace";
const char *nssf = "NVMe Security Specific Field";
int err;
void *sec_buf = NULL;
__u32 result;
struct config {
__u32 namespace_id;
__u32 size;
__u8 secp;
__u8 nssf;
__u16 spsp;
__u32 al;
int raw_binary;
};
struct config cfg = {
.size = 0,
.secp = 0,
.spsp = 0,
.al = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"size", 'x', "NUM", CFG_POSITIVE, &cfg.size, required_argument, size},
{"nssf", 'N', "NUM", CFG_BYTE, &cfg.nssf, required_argument, nssf},
{"secp", 'p', "NUM", CFG_BYTE, &cfg.secp, required_argument, secp},
{"spsp", 's', "NUM", CFG_SHORT, &cfg.spsp, required_argument, spsp},
{"al", 't', "NUM", CFG_POSITIVE, &cfg.al, required_argument, al},
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (cfg.size) {
if (posix_memalign(&sec_buf, getpagesize(), cfg.size)) {
fprintf(stderr, "No memory for security size:%d\n",
cfg.size);
return ENOMEM;
}
}
err = nvme_sec_recv(fd, cfg.namespace_id, cfg.nssf, cfg.spsp,
cfg.secp, cfg.al, cfg.size, sec_buf, &result);
if (err < 0)
return errno;
else if (err != 0)
fprintf(stderr, "NVME Security Receive Command Error:%d\n",
err);
else {
if (!cfg.raw_binary) {
printf("NVME Security Receive Command Success:%d\n",
result);
d(sec_buf, cfg.size, 16, 1);
} else if (cfg.size)
d_raw((unsigned char *)&sec_buf, cfg.size);
}
return err;
}
static int passthru(int argc, char **argv, int ioctl_cmd, const char *desc, struct command *cmd)
{
void *data = NULL, *metadata = NULL;
int err = 0, wfd = STDIN_FILENO;
__u32 result;
struct config {
__u8 opcode;
__u8 flags;
__u16 rsvd;
__u32 namespace_id;
__u32 data_len;
__u32 metadata_len;
__u32 timeout;
__u32 cdw2;
__u32 cdw3;
__u32 cdw10;
__u32 cdw11;
__u32 cdw12;
__u32 cdw13;
__u32 cdw14;
__u32 cdw15;
char *input_file;
int raw_binary;
int show_command;
int dry_run;
int read;
int write;
__u8 prefill;
};
struct config cfg = {
.opcode = 0,
.flags = 0,
.rsvd = 0,
.namespace_id = 0,
.data_len = 0,
.metadata_len = 0,
.timeout = 0,
.cdw2 = 0,
.cdw3 = 0,
.cdw10 = 0,
.cdw11 = 0,
.cdw12 = 0,
.cdw13 = 0,
.cdw14 = 0,
.cdw15 = 0,
.input_file = "",
.prefill = 0,
};
const char *opcode = "hex opcode (required)";
const char *flags = "command flags";
const char *rsvd = "value for reserved field";
const char *namespace_id = "desired namespace";
const char *data_len = "data I/O length (bytes)";
const char *metadata_len = "metadata seg. length (bytes)";
const char *timeout = "timeout value, in milliseconds";
const char *cdw2 = "command dword 2 value";
const char *cdw3 = "command dword 3 value";
const char *cdw10 = "command dword 10 value";
const char *cdw11 = "command dword 11 value";
const char *cdw12 = "command dword 12 value";
const char *cdw13 = "command dword 13 value";
const char *cdw14 = "command dword 14 value";
const char *cdw15 = "command dword 15 value";
const char *input = "write/send file (default stdin)";
const char *raw_binary = "dump output in binary format";
const char *show = "print command before sending";
const char *dry = "show command instead of sending";
const char *re = "set dataflow direction to receive";
const char *wr = "set dataflow direction to send";
const char *prefill = "prefill buffer with known byte-value, default 0";
const struct argconfig_commandline_options command_line_options[] = {
{"opcode", 'o', "NUM", CFG_BYTE, &cfg.opcode, required_argument, opcode},
{"flags", 'f', "NUM", CFG_BYTE, &cfg.flags, required_argument, flags},
{"prefill", 'p', "NUM", CFG_BYTE, &cfg.prefill, required_argument, prefill},
{"rsvd", 'R', "NUM", CFG_SHORT, &cfg.rsvd, required_argument, rsvd},
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"data-len", 'l', "NUM", CFG_POSITIVE, &cfg.data_len, required_argument, data_len},
{"metadata-len", 'm', "NUM", CFG_POSITIVE, &cfg.metadata_len, required_argument, metadata_len},
{"timeout", 't', "NUM", CFG_POSITIVE, &cfg.timeout, required_argument, timeout},
{"cdw2", '2', "NUM", CFG_POSITIVE, &cfg.cdw2, required_argument, cdw2},
{"cdw3", '3', "NUM", CFG_POSITIVE, &cfg.cdw3, required_argument, cdw3},
{"cdw10", '4', "NUM", CFG_POSITIVE, &cfg.cdw10, required_argument, cdw10},
{"cdw11", '5', "NUM", CFG_POSITIVE, &cfg.cdw11, required_argument, cdw11},
{"cdw12", '6', "NUM", CFG_POSITIVE, &cfg.cdw12, required_argument, cdw12},
{"cdw13", '7', "NUM", CFG_POSITIVE, &cfg.cdw13, required_argument, cdw13},
{"cdw14", '8', "NUM", CFG_POSITIVE, &cfg.cdw14, required_argument, cdw14},
{"cdw15", '9', "NUM", CFG_POSITIVE, &cfg.cdw15, required_argument, cdw15},
{"input-file", 'i', "FILE", CFG_STRING, &cfg.input_file, required_argument, input},
{"raw-binary", 'b', "", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{"show-command", 's', "", CFG_NONE, &cfg.show_command, no_argument, show},
{"dry-run", 'd', "", CFG_NONE, &cfg.dry_run, no_argument, dry},
{"read", 'r', "", CFG_NONE, &cfg.read, no_argument, re},
{"write", 'w', "", CFG_NONE, &cfg.write, no_argument, wr},
{NULL}
};
parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (strlen(cfg.input_file)){
wfd = open(cfg.input_file, O_RDONLY,
S_IRUSR | S_IRGRP | S_IROTH);
if (wfd < 0) {
perror(cfg.input_file);
return EINVAL;
}
}
if (cfg.metadata_len)
metadata = malloc(cfg.metadata_len);
if (cfg.data_len) {
if (posix_memalign(&data, getpagesize(), cfg.data_len))
exit(ENOMEM);
memset(data, cfg.prefill, cfg.data_len);
if (!cfg.read && !cfg.write) {
fprintf(stderr, "data direction not given\n");
err = EINVAL;
goto free_and_return;
}
if (cfg.read && cfg.write) {
fprintf(stderr, "command can't be both read and write\n");
err = EINVAL;
goto free_and_return;
}
if (cfg.write) {
if (read(wfd, data, cfg.data_len) < 0) {
fprintf(stderr, "failed to read write buffer\n");
err = EINVAL;
goto free_and_return;
}
}
}
if (cfg.show_command) {
printf("opcode : %02x\n", cfg.opcode);
printf("flags : %02x\n", cfg.flags);
printf("rsvd1 : %04x\n", cfg.rsvd);
printf("nsid : %08x\n", cfg.namespace_id);
printf("cdw2 : %08x\n", cfg.cdw2);
printf("cdw3 : %08x\n", cfg.cdw3);
printf("data_len : %08x\n", cfg.data_len);
printf("metadata_len : %08x\n", cfg.metadata_len);
printf("addr : %"PRIx64"\n", (uint64_t)(uintptr_t)data);
printf("metadata : %"PRIx64"\n", (uint64_t)(uintptr_t)metadata);
printf("cdw10 : %08x\n", cfg.cdw10);
printf("cdw11 : %08x\n", cfg.cdw11);
printf("cdw12 : %08x\n", cfg.cdw12);
printf("cdw13 : %08x\n", cfg.cdw13);
printf("cdw14 : %08x\n", cfg.cdw14);
printf("cdw15 : %08x\n", cfg.cdw15);
printf("timeout_ms : %08x\n", cfg.timeout);
if (cfg.dry_run)
goto free_and_return;
}
err = nvme_passthru(fd, ioctl_cmd, cfg.opcode, cfg.flags, cfg.rsvd,
cfg.namespace_id, cfg.cdw2, cfg.cdw3, cfg.cdw10,
cfg.cdw11, cfg.cdw12, cfg.cdw13, cfg.cdw14, cfg.cdw15,
cfg.data_len, data, cfg.metadata_len, metadata,
cfg.timeout, &result);
if (err < 0)
perror("passthru");
else if (err)
fprintf(stderr, "NVMe Status:%s(%x) Command Result:%08x\n",
nvme_status_to_string(err), err, result);
else {
if (!cfg.raw_binary) {
fprintf(stderr, "NVMe command result:%08x\n", result);
if (data && cfg.read && !err)
d((unsigned char *)data, cfg.data_len, 16, 1);
} else if (data && cfg.read)
d_raw((unsigned char *)data, cfg.data_len);
}
return err;
free_and_return:
free(data);
free(metadata);
return err;
}
static int io_passthru(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Send a user-defined IO command to the specified "\
"device via IOCTL passthrough, return results.";
return passthru(argc, argv, NVME_IOCTL_IO_CMD, desc, cmd);
}
static int admin_passthru(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Send a user-defined Admin command to the specified "\
"device via IOCTL passthrough, return results.";
return passthru(argc, argv, NVME_IOCTL_ADMIN_CMD, desc, cmd);
}
static int discover_cmd(int argc, char **argv, struct command *command, struct plugin *plugin)
{
const char *desc = "Send Get Log Page request to Discovery Controller.";
return discover(desc, argc, argv, false);
}
static int connect_all_cmd(int argc, char **argv, struct command *command, struct plugin *plugin)
{
const char *desc = "Discover NVMeoF subsystems and connect to them";
return discover(desc, argc, argv, true);
}
static int connect_cmd(int argc, char **argv, struct command *command, struct plugin *plugin)
{
const char *desc = "Connect to NVMeoF subsystem";
return connect(desc, argc, argv);
}
static int disconnect_cmd(int argc, char **argv, struct command *command, struct plugin *plugin)
{
const char *desc = "Disconnect from NVMeoF subsystem";
return disconnect(desc, argc, argv);
}
void register_extension(struct plugin *plugin)
{
plugin->parent = &nvme;
nvme.extensions->tail->next = plugin;
nvme.extensions->tail = plugin;
}
int main(int argc, char **argv)
{
int ret;
nvme.extensions->parent = &nvme;
if (argc < 2) {
general_help(&builtin);
return 0;
}
setlocale(LC_ALL, "");
ret = handle_plugin(argc - 1, &argv[1], nvme.extensions);
if (ret == -ENOTTY)
general_help(&builtin);
return ret;
}
|