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
|
// SPDX-License-Identifier: LGPL-2.1
/* Copyright(c) 2019 Intel Corporation. All rights reserved. */
#include <poll.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include <errno.h>
#include <ctype.h>
#include <libgen.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <ccan/list/list.h>
#include <ccan/minmax/minmax.h>
#include <ccan/array_size/array_size.h>
#include <ccan/build_assert/build_assert.h>
#include <util/sysfs.h>
#include <accfg/libaccel_config.h>
#include <fnmatch.h>
#include "private.h"
#include <accfg/idxd.h>
#define IDXD_DRIVER_BIND_PATH "/sys/bus/dsa/drivers/idxd"
#define IDXD_DRIVER(d) ((d)->ctx->compat ? \
(d)->bus_type_str : "idxd")
#define IDXD_WQ_DEVICE_PORTAL(d, w) ((d)->ctx->compat ? \
(d)->bus_type_str : accfg_wq_device_portals[(w)->type])
char *accfg_wq_device_portals[] = {
[ACCFG_WQT_KERNEL] = "dmaengine",
[ACCFG_WQT_USER] = "user",
NULL
};
char *accfg_bus_types[] = {
"dsa",
"iax",
NULL
};
const char *accfg_wq_mode_str[] = {
[ACCFG_WQ_SHARED] = "shared",
[ACCFG_WQ_DEDICATED] = "dedicated",
};
static char *filename_prefix;
static int filename_prefix_len;
ACCFG_EXPORT char *accfg_basenames[] = {
[ACCFG_DEVICE_DSA] = "dsa",
[ACCFG_DEVICE_IAX] = "iax",
NULL
};
static unsigned int accfg_device_compl_size[] = {
[ACCFG_DEVICE_DSA] = 32,
[ACCFG_DEVICE_IAX] = 64,
};
#define ACCFG_CMD_STATUS_MAX 0x45
#define ACCFG_CMD_STATUS_ERROR 0x80010000
static long init_cmd_status;
#define SCMD_STAT(x) (((x) & ~IDXD_SCMD_SOFTERR_MASK) >> \
IDXD_SCMD_SOFTERR_SHIFT)
const char *accfg_device_cmd_status[] = {
[0x0] = "Successful completion",
[0x1] = "Invalid command code",
[0x2] = "Invalid WQ index",
[0x3] = "Internal or platform hardware error",
[0x4] = "Non-zero reserved field in command",
[0x10] = "Device not disabled",
[0x11] = "Unspecified error in config for device enable",
[0x12] = "Bus master enable is 0",
[0x13] = "PRSREQALLOC value unsupported",
[0x14] = "Sum of WQCFG size fields out of range",
[0x15] = "Invalid group config: lack of wq or engines",
[0x16] = "Invalid group config: wq misconfigured",
[0x17] = "Invalid group config: engine misconfigured",
[0x18] = "Invalid group config: invalid read buffers config",
[0x20] = "Device not enabled",
[0x21] = "WQ is not disabled",
[0x22] = "WQ size is 0",
[0x23] = "WQ priority is 0",
[0x24] = "Invalid WQ mode",
[0x25] = "Invalid block on fault setting",
[0x26] = "Invalid value for WQ pasid enable",
[0x27] = "Invalid WQ max batch size",
[0x28] = "Invalid WQ max transfer size",
[0x2a] = "PCIe pasid cap Priv mode enable = 0",
[0x2b] = "Invalid WQ Occupancy Interrupt table or handle",
[0x2c] = "WQ ATS config mismatched",
[0x31] = "Device is not enabled",
[0x32] = "WQ(s) not enabled",
[0x41] = "Invalid interrupt table index",
[0x42] = "No interrupt handle available",
[0x43] = "No interrupt handles associated with the index",
[0x44] = "No revoked handles associated with the index",
[ACCFG_CMD_STATUS_MAX] = "Unknown error",
};
const char *accfg_sw_cmd_status[] = {
[0x01] = "Error reading cmd_status (library error)",
[SCMD_STAT(IDXD_SCMD_DEV_DMA_ERR)] = "DMA device registration error (driver error)",
[SCMD_STAT(IDXD_SCMD_WQ_NO_GRP)] = "wq error - group not set",
[SCMD_STAT(IDXD_SCMD_WQ_NO_NAME)] = "wq error - name not set",
[SCMD_STAT(IDXD_SCMD_WQ_NO_SVM)] =
"wq error - no SVM support and needed (platform configuration error)",
[SCMD_STAT(IDXD_SCMD_WQ_NO_THRESH)] = "wq error - threshold not set",
[SCMD_STAT(IDXD_SCMD_WQ_PORTAL_ERR)] = "wq portal mapping error (driver error)",
[SCMD_STAT(IDXD_SCMD_WQ_RES_ALLOC_ERR)] = "wq resource allocation error (driver error)",
[SCMD_STAT(IDXD_SCMD_PERCPU_ERR)] = "wq percpu counter allocation error (driver error)",
[SCMD_STAT(IDXD_SCMD_DMA_CHAN_ERR)] = "wq DMA channel registration error (driver error)",
[SCMD_STAT(IDXD_SCMD_CDEV_ERR)] = "wq char dev registration error (driver error)",
[SCMD_STAT(IDXD_SCMD_WQ_NO_SWQ_SUPPORT)] =
"wq error - no shared wq support (platform configuration error)",
[SCMD_STAT(IDXD_SCMD_WQ_NONE_CONFIGURED)] = "wq error - no wqs configured",
[SCMD_STAT(IDXD_SCMD_WQ_NO_SIZE)] = "wq error - size not set",
};
struct attr_dict_entry {
char *key;
char *val;
};
static const struct attr_dict_entry attr_dict[] = {
{"read_buffers_allowed", "tokens_allowed"},
{"read_buffers_reserved", "tokens_reserved"},
{"use_read_buffer_limit", "use_token_limit"},
{"max_read_buffers", "max_tokens"},
{"read_buffer_limit", "token_limit"}};
static const char *deprecated_attr(char *attr)
{
int i;
for (i = 0; i < (int) ARRAY_SIZE(attr_dict); i++)
if (!strcmp(attr, attr_dict[i].key))
return attr_dict[i].val;
return NULL;
}
static void save_last_error(struct accfg_device *device, struct accfg_wq *wq,
struct accfg_group *group, struct accfg_engine *engine)
{
struct accfg_ctx *ctx = device->ctx;
if (!ctx->error_ctx)
ctx->error_ctx = calloc(1, sizeof(struct accfg_error_ctx));
if (ctx->error_ctx) {
ctx->error_ctx->cmd_status = accfg_device_get_cmd_status(device);
ctx->error_ctx->device = device;
ctx->error_ctx->wq = wq;
ctx->error_ctx->group = group;
ctx->error_ctx->engine = engine;
}
}
static int accfg_set_param(struct accfg_ctx *ctx, int dfd, char *name,
const void *buf, int len)
{
int fd = openat(dfd, name, O_RDWR);
int n;
if (fd == -1)
return -errno;
n = write(fd, buf, len);
close(fd);
if (n != len)
return -errno;
return 0;
}
static long accfg_get_param_long(struct accfg_ctx *ctx, int dfd, char *name)
{
int fd = openat(dfd, name, O_RDONLY);
char buf[MAX_PARAM_LEN];
int n;
const char *p;
if (fd == -1 && errno == ENOENT) {
p = deprecated_attr(name);
if (!p)
return -errno;
fd = openat(dfd, p, O_RDONLY);
}
if (fd == -1)
return -errno;
n = read(fd, buf, MAX_PARAM_LEN - 1);
close(fd);
if (n <= 0)
return -ENXIO;
if (buf[n - 1] == '\n')
buf[n - 1] = '\0';
else
buf[n] = '\0';
return strtol(buf, NULL, 0);
}
static uint64_t accfg_get_param_unsigned_llong(
struct accfg_ctx *ctx, int dfd, char *name)
{
int fd = openat(dfd, name, O_RDONLY);
char buf[MAX_PARAM_LEN];
int n;
if (fd == -1)
return -errno;
n = read(fd, buf, MAX_PARAM_LEN - 1);
close(fd);
if (n <= 0)
return -ENXIO;
if (buf[n - 1] == '\n')
buf[n - 1] = '\0';
else
buf[n] = '\0';
return strtoull(buf, NULL, 0);
}
static char *accfg_get_param_str(struct accfg_ctx *ctx, int dfd, char *name)
{
int fd = openat(dfd, name, O_RDONLY);
char buf[MAX_PARAM_LEN];
int n;
if (fd == -1)
return NULL;
n = read(fd, buf, MAX_PARAM_LEN - 1);
close(fd);
if (n <= 0)
return NULL;
buf[n] = '\0';
*strchrnul(buf, '\n') = '\0';
return strdup(buf);
}
static void free_engine(struct accfg_engine *engine)
{
struct accfg_device *device = engine->device;
list_del_from(&device->engines, &engine->list);
free(engine->engine_path);
free(engine->engine_buf);
free(engine);
}
static void free_wq(struct accfg_wq *wq)
{
struct accfg_device *device = wq->device;
list_del_from(&device->wqs, &wq->list);
free(wq->wq_path);
free(wq->wq_buf);
free(wq->mode);
free(wq->state);
free(wq->name);
free(wq->driver_name);
free(wq);
}
static void free_group(struct accfg_group *group)
{
struct accfg_device *device = group->device;
list_del_from(&device->groups, &group->list);
free(group->group_buf);
free(group->group_path);
free(group->group_engines);
free(group->group_wqs);
free(group);
}
static void free_device(struct accfg_device *device, struct list_head *head)
{
struct accfg_group *group, *_r;
struct accfg_wq *wq, *wq_next;
struct accfg_engine *engine, *engine_next;
list_for_each_safe(&device->groups, group, _r, list)
free_group(group);
list_for_each_safe(&device->wqs, wq, wq_next, list)
free_wq(wq);
list_for_each_safe(&device->engines, engine, engine_next, list)
free_engine(engine);
if (head)
list_del_from(head, &device->list);
free(device->device_path);
free(device->device_buf);
free(device);
}
static void free_context(struct accfg_ctx *ctx)
{
struct accfg_device *device, *_b;
list_for_each_safe(&ctx->devices, device, _b, list)
free_device(device, &ctx->devices);
free(ctx->error_ctx);
free(ctx);
}
ACCFG_EXPORT enum accfg_wq_mode accfg_wq_get_mode(struct accfg_wq *wq)
{
enum accfg_wq_mode wq_mode;
char *read_mode;
int dfd;
struct accfg_ctx *ctx = accfg_wq_get_ctx(wq);
dfd = open(wq->wq_path, O_PATH);
if (dfd < 0)
return -errno;
read_mode = accfg_get_param_str(ctx, dfd, "mode");
if (strcmp(read_mode, accfg_wq_mode_str[ACCFG_WQ_SHARED]) == 0)
wq_mode = ACCFG_WQ_SHARED;
else
wq_mode = ACCFG_WQ_DEDICATED;
free(read_mode);
close(dfd);
return wq_mode;
}
ACCFG_EXPORT const char *accfg_engine_get_devname(
struct accfg_engine *engine)
{
return devpath_to_devname(engine->engine_path);
}
static int is_enabled(struct accfg_device *device, const char *drvpath)
{
struct stat st;
struct accfg_ctx *ctx;
if (!device)
return -EINVAL;
ctx = accfg_device_get_ctx(device);
if (lstat(drvpath, &st) < 0) {
err(ctx, "find symbolic link of device failed\n");
return 0;
} else
return 1;
}
ACCFG_EXPORT struct accfg_ctx *accfg_group_get_ctx(
struct accfg_group *group)
{
return group->device->ctx;
}
ACCFG_EXPORT int accfg_wq_is_enabled(struct accfg_wq *wq)
{
struct accfg_ctx *ctx = accfg_wq_get_ctx(wq);
char *path = wq->wq_buf;
int len = wq->buf_len;
if (snprintf(path, len, "%s/mode", wq->wq_path) >= len) {
err(ctx, "%s: buffer too small!\n", accfg_wq_get_devname(wq));
return -ENOMEM;
}
return is_enabled(accfg_wq_get_device(wq), path);
}
/**
* accfg_new - instantiate a new library context
* @ctx: context to establish
*
* Returns zero on success and stores an opaque pointer in ctx. The
* context is freed by accfg_unref(), i.e. accfg_new() implies an
* internal accfg_ref().
*/
ACCFG_EXPORT int accfg_new(struct accfg_ctx **ctx)
{
struct accfg_ctx *c;
const char *env;
c = calloc(1, sizeof(struct accfg_ctx));
if (!c)
return -ENOMEM;
c->refcount = 1;
log_init(&c->ctx, "libaccfg", "ACCFG_LOG");
c->timeout = 5000;
if (access(IDXD_DRIVER_BIND_PATH, F_OK))
c->compat = true;
list_head_init(&c->devices);
info(c, "ctx %p created\n", c);
dbg(c, "log_priority=%d\n", c->ctx.log_priority);
*ctx = c;
env = secure_getenv("ACCFG_TIMEOUT");
if (env != NULL) {
uint64_t tmo;
char *end;
tmo = strtoul(env, &end, 0);
if (tmo < ULONG_MAX && !end)
c->timeout = tmo;
dbg(c, "timeout = %" PRIu64 "\n", tmo);
}
return 0;
}
/**
* accfg_ref - take an additional reference on the context
* @ctx: context established by accfg_new()
*/
ACCFG_EXPORT struct accfg_ctx *accfg_ref(struct accfg_ctx *ctx)
{
if (ctx == NULL)
return NULL;
ctx->refcount++;
return ctx;
}
/**
* accfg_unref - drop a context reference count
* @ctx: context established by accfg_new()
*
* Drop a reference and if the resulting reference count is 0 destroy
* the context.
*/
ACCFG_EXPORT struct accfg_ctx *accfg_unref(struct accfg_ctx *ctx)
{
if (ctx == NULL)
return NULL;
ctx->refcount--;
if (ctx->refcount > 0)
return NULL;
info(ctx, "context %p released\n", ctx);
free_context(ctx);
return NULL;
}
/**
* accfg_set_log_fn - override default log routine
* @ctx: accfg library context
* @log_fn: function to be called for logging messages
*
* The built-in logging writes to stderr. It can be overridden by a
* custom function, to plug log messages into the user's logging
* functionality.
*/
ACCFG_EXPORT void accfg_set_log_fn(struct accfg_ctx *ctx,
void (*accfg_log_fn)(struct accfg_ctx *ctx,
int priority, const char *file,
int line, const char *fn, const char *format,
va_list args))
{
ctx->ctx.log_fn = (log_fn) accfg_log_fn;
info(ctx, "ctx is %p, custom logging function %p registered\n",
ctx, accfg_log_fn);
}
/**
* accfg_get_log_priority - retrieve current library loglevel (syslog)
* @ctx: accfg library context
*/
ACCFG_EXPORT int accfg_get_log_priority(struct accfg_ctx *ctx)
{
return ctx->ctx.log_priority;
}
/**
* accfg_set_log_priority - set log verbosity
* @priority: from syslog.h, LOG_ERR, LOG_INFO, LOG_DEBUG
*
* Note: LOG_DEBUG requires library be built with "configure --enable-debug"
*/
ACCFG_EXPORT void accfg_set_log_priority(struct accfg_ctx *ctx,
int priority)
{
ctx->ctx.log_priority = priority;
}
static int device_parse(struct accfg_ctx *ctx, const char *base_path,
char *dev_prefix, char *bus_type,
int (*filter)(const struct dirent *),
void *parent, add_dev_fn add_dev)
{
return sysfs_device_parse(ctx, base_path, dev_prefix, bus_type,
filter, parent, add_dev);
}
static int device_parse_type(struct accfg_device *device)
{
char **b;
int i;
for (b = accfg_basenames, i = 0; *b != NULL; b++, i++) {
if (!strcmp(device->device_type_str, *b)) {
device->type = i;
device->compl_size = accfg_device_compl_size[device->type];
return 0;
}
}
return -ENODEV;
}
static void *add_device(void *parent, int id, const char *ctl_base,
char *dev_prefix, char *bus_type)
{
struct accfg_ctx *ctx = parent;
struct accfg_device *device;
int dfd;
int rc;
dfd = open(ctl_base, O_PATH);
if (dfd == -1) {
err(ctx, "%s open failed: %s\n", __func__, strerror(errno));
return NULL;
}
accfg_set_param(ctx, dfd, "cmd_status", "1", 1);
/*
* Clearing could have failed due to RO file system. When checking
* cmd_status, validate initial value = 0 or is different from new
* value.
*/
init_cmd_status = accfg_get_param_long(ctx, dfd, "cmd_status");
device = calloc(1, sizeof(*device));
if (!device) {
err(ctx, "allocation of device failed\n");
close(dfd);
goto err_device;
}
list_head_init(&device->groups);
list_head_init(&device->wqs);
list_head_init(&device->engines);
device->ctx = ctx;
device->id = id;
device->max_groups = accfg_get_param_long(ctx, dfd, "max_groups");
device->max_work_queues = accfg_get_param_long(ctx, dfd,
"max_work_queues");
device->max_engines = accfg_get_param_long(ctx, dfd, "max_engines");
device->max_work_queues_size =
accfg_get_param_long(ctx, dfd, "max_work_queues_size");
device->numa_node = accfg_get_param_long(ctx, dfd, "numa_node");
device->ims_size = accfg_get_param_long(ctx, dfd, "ims_size");
device->max_batch_size = accfg_get_param_long(ctx, dfd,
"max_batch_size");
device->max_transfer_size =
accfg_get_param_unsigned_llong(ctx, dfd, "max_transfer_size");
device->gencap = accfg_get_param_unsigned_llong(ctx, dfd, "gen_cap");
device->configurable = accfg_get_param_unsigned_llong(ctx, dfd,
"configurable");
device->pasid_enabled = accfg_get_param_long(ctx, dfd, "pasid_enabled");
device->max_read_buffers = accfg_get_param_long(ctx, dfd, "max_read_buffers");
device->read_buffer_limit = accfg_get_param_long(ctx, dfd, "read_buffer_limit");
device->event_log_size = accfg_get_param_long(ctx, dfd, "event_log_size");
device->cdev_major = accfg_get_param_long(ctx, dfd, "cdev_major");
device->version = accfg_get_param_unsigned_llong(ctx, dfd, "version");
device->device_path = realpath(ctl_base, NULL);
close(dfd);
if (!device->device_path) {
err(ctx, "get realpath of device_path failed\n");
goto err_dev_path;
}
device->device_buf = calloc(1, strlen(device->device_path) +
MAX_BUF_LEN);
if (!device->device_buf) {
err(ctx, "allocation of device buffer failed\n");
goto err_read;
}
device->buf_len = strlen(device->device_path) + MAX_BUF_LEN;
device->device_type_str = dev_prefix;
rc = device_parse_type(device);
if (rc < 0)
goto err_dev_path;
device->bus_type_str = bus_type;
list_add_tail(&ctx->devices, &device->list);
return device;
err_dev_path:
err_read:
free(device->device_buf);
free(device);
err_device:
return NULL;
}
static int wq_parse_type(struct accfg_wq *wq, char *wq_type)
{
char *type;
char *ptype;
type = strdup(wq_type);
if (!type)
return -ENOMEM;
ptype = strtok(type, ":");
if (!ptype) {
free(type);
return -EINVAL;
}
if (strcmp(ptype, "kernel") == 0)
wq->type = ACCFG_WQT_KERNEL;
else if (strcmp(ptype, "user") == 0)
wq->type = ACCFG_WQT_USER;
else
wq->type = ACCFG_WQT_NONE;
free(type);
return 0;
}
static void *add_wq(void *parent, int id, const char *wq_base,
char *dev_prefix, char *bus_type)
{
struct accfg_wq *wq;
struct accfg_device *device = parent;
struct accfg_group *group;
struct accfg_ctx *ctx;
char *wq_base_string;
uint64_t device_id, wq_id;
int dfd;
char *wq_type;
if (!device)
return NULL;
group = device->group;
ctx = accfg_device_get_ctx(device);
dfd = open(wq_base, O_PATH);
if (dfd < 0)
return NULL;
wq = calloc(1, sizeof(*wq));
if (!wq) {
err(ctx, "allocation of wq failed\n");
close(dfd);
return NULL;
}
wq_base_string = strdup(wq_base);
if (!wq_base_string) {
err(ctx, "conversion of wq_base_string failed\n");
close(dfd);
goto err_wq;
}
if (sscanf(basename(wq_base_string),
"wq%" SCNu64 ".%" SCNu64, &device_id, &wq_id) != 2) {
free(wq_base_string);
close(dfd);
goto err_wq;
}
free(wq_base_string);
wq->id = wq_id;
wq->group = group;
wq->device = device;
wq->group_id = accfg_get_param_long(ctx, dfd, "group_id");
wq->size = accfg_get_param_long(ctx, dfd, "size");
wq->priority = accfg_get_param_long(ctx, dfd, "priority");
wq->block_on_fault = accfg_get_param_long(ctx, dfd,
"block_on_fault");
wq->mode = accfg_get_param_str(ctx, dfd, "mode");
wq->state = accfg_get_param_str(ctx, dfd, "state");
wq->cdev_minor = accfg_get_param_long(ctx, dfd, "cdev_minor");
wq_type = accfg_get_param_str(ctx, dfd, "type");
wq->name = accfg_get_param_str(ctx, dfd, "name");
wq->driver_name = accfg_get_param_str(ctx, dfd, "driver_name");
wq->threshold = accfg_get_param_long(ctx, dfd, "threshold");
wq->max_batch_size = accfg_get_param_long(ctx, dfd, "max_batch_size");
wq->max_transfer_size = accfg_get_param_long(ctx, dfd, "max_transfer_size");
wq->ats_disable = accfg_get_param_long(ctx, dfd, "ats_disable");
wq->prs_disable = accfg_get_param_long(ctx, dfd, "prs_disable");
wq_parse_type(wq, wq_type);
free(wq_type);
close(dfd);
wq->wq_path = strdup(wq_base);
if (!wq->wq_path) {
err(ctx, "forming of wq path failed\n");
goto err_read;
}
wq->wq_buf = calloc(1, strlen(wq_base) + MAX_BUF_LEN);
if (!wq->wq_buf) {
err(ctx, "allocation of wq buffer failed\n");
goto err_path;
}
wq->buf_len = strlen(wq_base) + MAX_BUF_LEN;
list_add_tail(&device->wqs, &wq->list);
return wq;
err_path:
free(wq->wq_path);
err_read:
free(wq->mode);
free(wq->state);
free(wq->name);
free(wq->driver_name);
err_wq:
free(wq);
return NULL;
}
static void *add_group(void *parent, int id, const char *group_base,
char *dev_prefix, char *bus_type)
{
struct accfg_group *group;
struct accfg_device *device = parent;
struct accfg_ctx *ctx;
char *group_base_string;
int dfd;
uint64_t device_id, group_id;
if (!device)
return NULL;
ctx = accfg_device_get_ctx(device);
dfd = open(group_base, O_PATH);
if (dfd < 0)
return NULL;
group = calloc(1, sizeof(*group));
if (!group) {
err(ctx, "allocation of group failed\n");
close(dfd);
return NULL;
}
group_base_string = strdup(group_base);
if (!group_base_string) {
err(ctx, "conversion of group_base_string failed\n");
close(dfd);
goto err_group;
}
if (sscanf(basename(group_base_string),
"group%" SCNu64 ".%" SCNu64, &device_id, &group_id) != 2) {
free(group_base_string);
close(dfd);
goto err_group;
}
free(group_base_string);
group->device = device;
device->group = group;
group->id = group_id;
group->group_engines = accfg_get_param_str(ctx, dfd, "engines");
group->group_wqs = accfg_get_param_str(ctx, dfd, "work_queues");
group->read_buffers_reserved = accfg_get_param_long(ctx, dfd,
"read_buffers_reserved");
group->read_buffers_allowed = accfg_get_param_long(ctx, dfd,
"read_buffers_allowed");
group->use_read_buffer_limit = accfg_get_param_long(ctx, dfd,
"use_read_buffer_limit");
group->traffic_class_a = accfg_get_param_long(ctx, dfd,
"traffic_class_a");
group->traffic_class_b = accfg_get_param_long(ctx, dfd,
"traffic_class_b");
group->desc_progress_limit = accfg_get_param_long(ctx, dfd,
"desc_progress_limit");
group->batch_progress_limit = accfg_get_param_long(ctx, dfd,
"batch_progress_limit");
close(dfd);
group->group_buf = calloc(1, strlen(group_base) + MAX_BUF_LEN);
if (!group->group_buf) {
err(ctx, "allocation of group buffer failed\n");
goto err_read;
}
group->buf_len = strlen(group_base) + MAX_BUF_LEN;
group->group_path = strdup(group_base);
if (!group->group_path) {
err(ctx, "forming of group path failed\n");
goto err_buf;
}
list_add_tail(&device->groups, &group->list);
return group;
err_buf:
free(group->group_buf);
err_read:
free(group->group_engines);
free(group->group_wqs);
err_group:
free(group);
return NULL;
}
static void *add_engine(void *parent, int id, const char *engine_base,
char *dev_prefix, char *bus_type)
{
struct accfg_engine *engine;
struct accfg_device *device = parent;
struct accfg_ctx *ctx;
struct accfg_group *group;
char *engine_base_string;
int dfd;
uint64_t device_id, engine_id;
if (!device)
return NULL;
group = device->group;
ctx = accfg_device_get_ctx(device);
dfd = open(engine_base, O_PATH);
if (dfd < 0)
return NULL;
engine = calloc(1, sizeof(*engine));
if (!engine) {
err(ctx, "allocation of engine failed\n");
close(dfd);
return NULL;
}
engine_base_string = strdup(engine_base);
if (!engine_base_string) {
err(ctx, "conversion of engine_base_string failed\n");
close(dfd);
goto err_engine;
}
if (sscanf(basename(engine_base_string),
"engine%" SCNu64 ".%" SCNu64, &device_id, &engine_id) != 2) {
free(engine_base_string);
close(dfd);
goto err_engine;
}
free(engine_base_string);
engine->id = engine_id;
engine->group = group;
engine->device = device;
engine->group_id = accfg_get_param_long(ctx, dfd, "group_id");
close(dfd);
engine->engine_path = strdup(engine_base);
if (!engine->engine_path) {
err(ctx, "forming of engine path failed\n");
goto err_engine;
}
engine->engine_buf = calloc(1, strlen(engine_base) + MAX_BUF_LEN);
if (!engine->engine_buf) {
err(ctx, "allocation of engine buffer failed\n");
goto err_path;
}
engine->buf_len = strlen(engine_base) + MAX_BUF_LEN;
list_add_tail(&device->engines, &engine->list);
return engine;
err_path:
free(engine->engine_path);
err_engine:
free(engine);
return NULL;
}
static void set_filename_prefix(char *pfx)
{
filename_prefix = pfx;
filename_prefix_len = strlen(pfx);
}
static int filter_file_name_prefix(const struct dirent *d)
{
return !strncmp(filename_prefix, d->d_name, filename_prefix_len);
}
static void groups_init(struct accfg_device *device)
{
struct accfg_ctx *ctx = device->ctx;
if (device->group_init) {
dbg(ctx, "group is initialized already\n");
return;
}
device->group_init = 1;
set_filename_prefix("group");
device_parse(device->ctx, device->device_path, "group",
device->bus_type_str, filter_file_name_prefix,
device, add_group);
}
static void devices_init(struct accfg_ctx *ctx)
{
char **accel_name;
char **bus_type;
char path[PATH_MAX];
struct accfg_device *device;
if (ctx->devices_init) {
dbg(ctx, "device is initialized already\n");
return;
}
ctx->devices_init = 1;
for (bus_type = accfg_bus_types; *bus_type != NULL; bus_type++) {
sprintf(path, "/sys/bus/%s/devices", *bus_type);
for (accel_name = accfg_basenames; *accel_name != NULL;
accel_name++) {
set_filename_prefix(*accel_name);
device_parse(ctx, path, *accel_name, *bus_type,
filter_file_name_prefix, ctx,
add_device);
}
}
accfg_device_foreach(ctx, device) {
groups_init(device);
}
}
static void engines_init(struct accfg_device *device)
{
struct accfg_group *group = device->group;
struct accfg_ctx *ctx = device->ctx;
if (group) {
if (group->engines_init) {
dbg(ctx, "engine is initialized already\n");
return;
}
group->engines_init = 1;
}
set_filename_prefix("engine");
device_parse(ctx, device->device_path, "engine",
device->bus_type_str, filter_file_name_prefix, device,
add_engine);
}
/**
* accfg_device_get_first - retrieve first device in the system
* @ctx: context established by accfg_new
*
* Returns an accfg_device if a device exists in the system. This return
* value can be used to iterate to the next available device in the system
* ia accfg_device_get_next()
*/
ACCFG_EXPORT struct accfg_device *accfg_device_get_first(struct accfg_ctx *ctx)
{
devices_init(ctx);
return list_top(&ctx->devices, struct accfg_device, list);
}
ACCFG_EXPORT struct accfg_ctx *accfg_device_get_ctx(struct accfg_device *device)
{
return device->ctx;
}
/**
* accfg_device_get_next - retrieve the "next" device in the system
* @device: accfg_device instance returned from
* accfg_device_get_{first|next}
*
* Returns NULL if @device was the "last" device available in the system
*/
ACCFG_EXPORT struct accfg_device *accfg_device_get_next(
struct accfg_device *device)
{
struct accfg_ctx *ctx = device->ctx;
return list_next(&ctx->devices, device, list);
}
ACCFG_EXPORT const char *accfg_device_get_devname(struct accfg_device *device)
{
return devpath_to_devname(device->device_path);
}
ACCFG_EXPORT int accfg_device_get_id(struct accfg_device *device)
{
return device->id;
}
ACCFG_EXPORT struct accfg_device *accfg_ctx_device_get_by_id(
struct accfg_ctx *ctx, int id)
{
struct accfg_device *dev;
accfg_device_foreach(ctx, dev)
if (accfg_device_get_id(dev) == id)
return dev;
return NULL;
}
ACCFG_EXPORT unsigned int accfg_device_get_max_groups(
struct accfg_device *device)
{
return device->max_groups;
}
ACCFG_EXPORT unsigned int accfg_device_get_max_work_queues(
struct accfg_device *device)
{
return device->max_work_queues;
}
ACCFG_EXPORT unsigned int accfg_device_get_max_engines(
struct accfg_device *device)
{
return device->max_engines;
}
ACCFG_EXPORT unsigned int accfg_device_get_max_work_queues_size(
struct accfg_device *device)
{
return device->max_work_queues_size;
}
ACCFG_EXPORT int accfg_device_get_numa_node(struct accfg_device *device)
{
return device->numa_node;
}
ACCFG_EXPORT unsigned int accfg_device_get_ims_size(
struct accfg_device *device)
{
return device->ims_size;
}
ACCFG_EXPORT unsigned int accfg_device_get_max_batch_size(
struct accfg_device *device)
{
return device->max_batch_size;
}
ACCFG_EXPORT uint64_t accfg_device_get_max_transfer_size(
struct accfg_device *device)
{
return device->max_transfer_size;
}
/* Helper function to retrieve completion record size */
ACCFG_EXPORT unsigned int accfg_device_get_compl_size(struct accfg_device *device)
{
return device->compl_size;
}
ACCFG_EXPORT int accfg_device_get_op_cap(struct accfg_device *device,
struct accfg_op_cap *op_cap)
{
char *oc;
int dfd;
int rc;
struct accfg_ctx *ctx;
if (!device)
return -EINVAL;
ctx = accfg_device_get_ctx(device);
dfd = open(device->device_path, O_PATH);
if (dfd < 0)
return -errno;
oc = accfg_get_param_str(ctx, dfd, "op_cap");
close(dfd);
if (!oc)
return -EIO;
rc = sscanf(oc, "%" SCNx32 ",%" SCNx32 ",%" SCNx32 ",%" SCNx32
",%" SCNx32 ",%" SCNx32 ",%" SCNx32 ",%" SCNx32,
&op_cap->bits[0], &op_cap->bits[1],
&op_cap->bits[2], &op_cap->bits[3],
&op_cap->bits[4], &op_cap->bits[5],
&op_cap->bits[6], &op_cap->bits[7]);
free(oc);
if (rc != 8)
return errno ? -errno : -EIO;
return 0;
}
ACCFG_EXPORT uint64_t accfg_device_get_gen_cap(struct accfg_device *device)
{
return device->gencap;
}
ACCFG_EXPORT int accfg_device_get_iaa_cap(struct accfg_device *device,
uint64_t *iaa_cap)
{
int fd;
int rc;
struct accfg_ctx *ctx;
char *buf;
if (!device)
return -EINVAL;
ctx = accfg_device_get_ctx(device);
fd = open(device->device_path, O_PATH);
if (fd < 0)
return -errno;
buf = accfg_get_param_str(ctx, fd, "iaa_cap");
close(fd);
if (!buf)
return -EIO;
errno = 0;
*iaa_cap = strtoull(buf, NULL, 0);
rc = -errno;
free(buf);
return rc;
}
ACCFG_EXPORT unsigned int accfg_device_get_configurable(
struct accfg_device *device)
{
return device->configurable;
}
ACCFG_EXPORT bool accfg_device_get_pasid_enabled(
struct accfg_device *device)
{
return device->pasid_enabled;
}
ACCFG_EXPORT int accfg_device_get_errors(struct accfg_device *device,
struct accfg_error *error)
{
char *read_error;
int dfd;
int rc;
struct accfg_ctx *ctx;
if (!device)
return -EINVAL;
ctx = accfg_device_get_ctx(device);
dfd = open(device->device_path, O_PATH);
if (dfd < 0)
return -errno;
read_error = accfg_get_param_str(ctx, dfd, "errors");
close(dfd);
rc = sscanf(read_error, "%" SCNx32 ",%" SCNx32 ",%" SCNx32 ",%" SCNx32
",%" SCNx32 ",%" SCNx32 ",%" SCNx32 ",%" SCNx32,
&error->val[0], &error->val[1],
&error->val[2], &error->val[3],
&error->val[4], &error->val[5],
&error->val[6], &error->val[7]);
free(read_error);
if (rc != 8)
return errno ? -errno : -EIO;
return 0;
}
ACCFG_EXPORT enum accfg_device_state accfg_device_get_state(
struct accfg_device *device)
{
struct accfg_ctx *ctx;
char read_state[SYSFS_ATTR_SIZE];
char *path;
int len;
if (!device)
return ACCFG_DEVICE_UNKNOWN;
ctx = accfg_device_get_ctx(device);
path = device->device_buf;
len = device->buf_len;
if (snprintf(path, len, "%s/state", device->device_path) >= len) {
err(ctx, "%s: buffer too small!\n", __func__);
return ACCFG_DEVICE_UNKNOWN;
}
if (sysfs_read_attr(ctx, path, read_state) < 0) {
err(ctx, "%s: sysfs_read_attr failed '%s': %s\n",
__func__, device->device_path,
strerror(errno));
save_last_error(device, NULL, NULL, NULL);
return ACCFG_DEVICE_UNKNOWN;
}
if (strcmp(read_state, "disabled") == 0)
return ACCFG_DEVICE_DISABLED;
else if (strcmp(read_state, "enabled") == 0)
return ACCFG_DEVICE_ENABLED;
return ACCFG_DEVICE_UNKNOWN;
}
ACCFG_EXPORT unsigned int accfg_device_get_max_read_buffers(
struct accfg_device *device)
{
return device->max_read_buffers;
}
ACCFG_EXPORT unsigned int accfg_device_get_read_buffer_limit(
struct accfg_device *device)
{
return device->read_buffer_limit;
}
ACCFG_EXPORT int accfg_device_get_event_log_size(struct accfg_device *device)
{
return device->event_log_size;
}
ACCFG_EXPORT unsigned int accfg_device_get_cdev_major(
struct accfg_device *device)
{
return device->cdev_major;
}
ACCFG_EXPORT unsigned int accfg_device_get_version(
struct accfg_device *device)
{
return device->version;
}
ACCFG_EXPORT int accfg_device_get_clients(struct accfg_device *device)
{
struct accfg_ctx *ctx;
char buf[SYSFS_ATTR_SIZE];
char *path;
int len;
int rc;
if (!device)
return -EINVAL;
ctx = accfg_device_get_ctx(device);
path = device->device_buf;
len = device->buf_len;
rc = snprintf(path, len, "%s/clients", device->device_path);
if (rc >= len || rc < 0) {
err(ctx, "%s: snprintf error: %d\n", __func__, -errno);
return -errno;
}
if (sysfs_read_attr(ctx, path, buf) < 0) {
err(ctx, "%s: retrieve clients failed '%s': %s\n",
__func__, path, strerror(errno));
save_last_error(device, NULL, NULL, NULL);
return -errno;
}
return atoi(buf);
}
ACCFG_EXPORT int accfg_device_set_read_buffer_limit(struct accfg_device *dev, int val)
{
struct accfg_ctx *ctx;
char *path;
char buf[SYSFS_ATTR_SIZE];
if (!dev)
return -EINVAL;
path = dev->device_buf;
ctx = accfg_device_get_ctx(dev);
if (sprintf(path, "%s/read_buffer_limit", dev->device_path) >=
(int)dev->buf_len) {
err(ctx, "%s; buf len exceeded.\n",
accfg_device_get_devname(dev));
return -errno;
}
if (access(path, F_OK)) {
const char *attr = deprecated_attr("read_buffer_limit");
if (!attr) {
err(ctx, "%s; invalid attr 'read_buffer_limit'.\n",
accfg_device_get_devname(dev));
return -errno;
}
if (sprintf(path, "%s/%s", dev->device_path, attr) >=
(int)dev->buf_len) {
err(ctx, "%s; buf len exceeded.\n",
accfg_device_get_devname(dev));
return -errno;
}
}
if (sprintf(buf, "%d", val) < 0) {
err(ctx, "%s: sprintf to buf failed: %s\n",
accfg_device_get_devname(dev), strerror(errno));
return -errno;
}
if (sysfs_write_attr(ctx, path, buf) < 0) {
err(ctx, "%s: write failed: %s\n",
accfg_device_get_devname(dev), strerror(errno));
save_last_error(dev, NULL, NULL, NULL);
return -errno;
}
dev->read_buffer_limit = val;
return 0;
}
ACCFG_EXPORT int accfg_device_set_event_log_size(struct accfg_device *dev, int val)
{
struct accfg_ctx *ctx;
char *path;
char buf[SYSFS_ATTR_SIZE];
if (!dev)
return -EINVAL;
path = dev->device_buf;
ctx = accfg_device_get_ctx(dev);
if (sprintf(path, "%s/event_log_size", dev->device_path) >=
(int)dev->buf_len) {
err(ctx, "%s; buf len exceeded.\n",
accfg_device_get_devname(dev));
return -errno;
}
if (access(path, F_OK))
return 0;
if (sprintf(buf, "%d", val) < 0) {
err(ctx, "%s: sprintf to buf failed: %s\n",
accfg_device_get_devname(dev), strerror(errno));
return -errno;
}
if (sysfs_write_attr(ctx, path, buf) < 0) {
err(ctx, "%s: write failed: %s\n",
accfg_device_get_devname(dev), strerror(errno));
save_last_error(dev, NULL, NULL, NULL);
return -errno;
}
dev->event_log_size = val;
return 0;
}
ACCFG_EXPORT int accfg_device_is_active(struct accfg_device *device)
{
struct accfg_ctx *ctx;
char *path;
int len;
char buf[SYSFS_ATTR_SIZE];
if (!device)
return -EINVAL;
ctx = accfg_device_get_ctx(device);
path = device->device_buf;
len = device->buf_len;
if (snprintf(path, len, "%s/state", device->device_path) >= len) {
err(ctx, "%s: buffer too small!\n",
accfg_device_get_devname(device));
return 0;
}
if (sysfs_read_attr(ctx, path, buf) < 0) {
save_last_error(device, NULL, NULL, NULL);
return 0;
}
if (strcmp(buf, "enabled") == 0)
return 1;
return 0;
}
ACCFG_EXPORT unsigned int accfg_device_get_cmd_status(struct accfg_device *device)
{
struct accfg_ctx *ctx;
long status;
char *path;
int len;
char buf[SYSFS_ATTR_SIZE], *end_ptr;
if (!device)
return ACCFG_CMD_STATUS_ERROR;
ctx = accfg_device_get_ctx(device);
path = device->device_buf;
len = device->buf_len;
if (snprintf(path, len, "%s/cmd_status", device->device_path) >= len) {
err(ctx, "%s: buffer too small!\n",
accfg_device_get_devname(device));
return ACCFG_CMD_STATUS_ERROR;
}
if (sysfs_read_attr(ctx, path, buf))
return ACCFG_CMD_STATUS_ERROR;
status = strtol(buf, &end_ptr, 0);
if (errno == ERANGE || end_ptr == buf)
return ACCFG_CMD_STATUS_ERROR;
/*
* If cmd_status initialization had failed, return error if new
* cmd_status is same as initial cmd_status
*/
if (init_cmd_status != 0 && init_cmd_status == status)
return ACCFG_CMD_STATUS_ERROR;
return (unsigned int)status;
}
static const char *get_cmd_status_str(unsigned int status)
{
unsigned int sw_status;
if (status & IDXD_SCMD_SOFTERR_MASK) {
sw_status = SCMD_STAT(status);
if (sw_status) {
if (sw_status >= (sizeof(accfg_sw_cmd_status) /
sizeof(char *))) {
status = ACCFG_CMD_STATUS_MAX;
goto ext;
}
return accfg_sw_cmd_status[sw_status];
}
status &= ~IDXD_SCMD_SOFTERR_MASK;
}
if (status > ACCFG_CMD_STATUS_MAX)
status = ACCFG_CMD_STATUS_MAX;
ext:
return accfg_device_cmd_status[status];
}
ACCFG_EXPORT const char *accfg_device_get_cmd_status_str(struct accfg_device *device)
{
return get_cmd_status_str(accfg_device_get_cmd_status(device));
}
ACCFG_EXPORT unsigned int accfg_ctx_get_last_error(struct accfg_ctx *ctx)
{
if (ctx->error_ctx)
return ctx->error_ctx->cmd_status;
return 0;
}
ACCFG_EXPORT const char *accfg_ctx_get_last_error_str(struct accfg_ctx *ctx)
{
return get_cmd_status_str(accfg_ctx_get_last_error(ctx));
}
ACCFG_EXPORT struct accfg_device *accfg_ctx_get_last_error_device(
struct accfg_ctx *ctx)
{
if (ctx->error_ctx)
return ctx->error_ctx->device;
return NULL;
}
ACCFG_EXPORT struct accfg_wq *accfg_ctx_get_last_error_wq(struct accfg_ctx *ctx)
{
if (ctx->error_ctx)
return ctx->error_ctx->wq;
return NULL;
}
ACCFG_EXPORT struct accfg_group *accfg_ctx_get_last_error_group(
struct accfg_ctx *ctx)
{
if (ctx->error_ctx)
return ctx->error_ctx->group;
return NULL;
}
ACCFG_EXPORT struct accfg_engine *accfg_ctx_get_last_error_engine(
struct accfg_ctx *ctx)
{
if (ctx->error_ctx)
return ctx->error_ctx->engine;
return NULL;
}
/* Helper function to validate device type in the defined device array based on
* device name */
ACCFG_EXPORT int accfg_device_type_validate(const char *dev_name)
{
char **accel_type;
for (accel_type = accfg_basenames;
*accel_type != NULL;
accel_type++) {
if (strstr(dev_name, *accel_type))
return 1;
}
if (*accel_type == NULL) {
fprintf(stderr, "no such device type\n");
return 0;
}
return 0;
}
/* Helper function to retrieve device_type */
ACCFG_EXPORT enum accfg_device_type accfg_device_get_type(struct accfg_device *device)
{
return device->type;
}
/* Helper function to retrieve device_type_str */
ACCFG_EXPORT char *accfg_device_get_type_str(struct accfg_device *device)
{
return device->device_type_str;
}
static int get_driver_bind_path(char *bus_name, const char *drv_name,
char **path)
{
char p[PATH_MAX];
sprintf(p, "/sys/bus/%s/drivers/%s/bind", bus_name, drv_name);
*path = strdup(p);
if (!*path)
return -errno;
return 0;
}
static int get_driver_unbind_path(char *bus_name, const char *dev_name,
char **path)
{
char p[PATH_MAX];
sprintf(p, "/sys/bus/%s/devices/%s/driver/unbind", bus_name, dev_name);
*path = realpath(p, NULL);
if (!*path)
return -errno;
return 0;
}
/* Helper function to parse the device enable flag */
static int accfg_device_control(struct accfg_device *device,
enum accfg_control_flag flag, bool force)
{
int rc = 0;
struct accfg_ctx *ctx;
char *path = NULL;
if (!device)
return -EINVAL;
ctx = accfg_device_get_ctx(device);
if (flag == ACCFG_DEVICE_ENABLE) {
rc = get_driver_bind_path(device->bus_type_str,
IDXD_DRIVER(device), &path);
if (rc < 0)
return rc;
} else if (flag == ACCFG_DEVICE_DISABLE) {
int clients;
rc = get_driver_unbind_path(device->bus_type_str,
accfg_device_get_devname(device), &path);
if (rc < 0)
return rc;
clients = accfg_device_get_clients(device);
if (clients > 0) {
err(ctx, "Device has clients: %d.\n", clients);
if (force) {
err(ctx, "\n");
} else {
err(ctx, "Device disable refused.\n");
return -EPERM;
}
}
}
if (path) {
rc = sysfs_write_attr(ctx, path, accfg_device_get_devname(device));
free(path);
if (rc < 0) {
save_last_error(device, NULL, NULL, NULL);
return rc;
}
}
return 0;
}
ACCFG_EXPORT int accfg_device_enable(struct accfg_device *device)
{
return accfg_device_control(device, ACCFG_DEVICE_ENABLE, false);
}
ACCFG_EXPORT int accfg_device_disable(struct accfg_device *device, bool force)
{
return accfg_device_control(device, ACCFG_DEVICE_DISABLE, force);
}
ACCFG_EXPORT struct accfg_device *accfg_group_get_device(
struct accfg_group *group)
{
return group->device;
}
ACCFG_EXPORT struct accfg_group *accfg_group_get_first(
struct accfg_device *device)
{
groups_init(device);
return list_top(&device->groups, struct accfg_group, list);
}
ACCFG_EXPORT struct accfg_group *accfg_group_get_next(
struct accfg_group *group)
{
struct accfg_device *device = group->device;
return list_next(&device->groups, group, list);
}
ACCFG_EXPORT int accfg_group_get_id(struct accfg_group *group)
{
return group->id;
}
ACCFG_EXPORT struct accfg_group *accfg_device_group_get_by_id(
struct accfg_device *dev, int id)
{
struct accfg_group *group;
accfg_group_foreach(dev, group)
if (accfg_group_get_id(group) == id)
return group;
return NULL;
}
ACCFG_EXPORT int accfg_group_get_device_id(
struct accfg_group *group)
{
struct accfg_device *device = group->device;
return device->id;
}
ACCFG_EXPORT const char *accfg_group_get_devname(struct accfg_group *group)
{
return devpath_to_devname(group->group_path);
}
ACCFG_EXPORT uint64_t accfg_group_get_size(struct accfg_group *group)
{
return group->size;
}
ACCFG_EXPORT uint64_t accfg_group_get_available_size(
struct accfg_group *group)
{
struct accfg_ctx *ctx = accfg_group_get_ctx(group);
char *path = group->group_buf;
int len = group->buf_len;
char buf[SYSFS_ATTR_SIZE];
if (snprintf(path, len, "%s/available_size",
group->group_path) >= len) {
err(ctx, "%s: buffer too small!\n",
accfg_group_get_devname(group));
return ULLONG_MAX;
}
if (sysfs_read_attr(ctx, path, buf) < 0) {
save_last_error(group->device, NULL, group, NULL);
return ULLONG_MAX;
}
return strtoull(buf, NULL, 0);
}
#define accfg_group_set_field(group, val, field) \
ACCFG_EXPORT int accfg_group_set_##field( \
struct accfg_group *group, int val) \
{ \
struct accfg_ctx *ctx = accfg_group_get_ctx(group); \
char *path = group->group_buf; \
char buf[SYSFS_ATTR_SIZE]; \
int rc; \
rc = sprintf(group->group_buf, "%s/%s", group->group_path, #field); \
if (rc < 0) \
return -errno; \
if (access(path, F_OK)) { \
const char *attr = deprecated_attr(#field); \
if (!attr) { \
err(ctx, "%s; invalid attr '%s'.\n", \
accfg_group_get_devname(group), \
#field); \
return -errno; \
} \
rc = sprintf(group->group_buf, "%s/%s", \
group->group_path, attr); \
if (rc < 0) \
return -errno; \
} \
if (sprintf(buf, "%d", val) < 0) { \
err(ctx, "%s: sprintf to buf failed: %s\n", \
accfg_group_get_devname(group), \
strerror(errno)); \
return -errno; \
} \
if (sysfs_write_attr(ctx, path, buf) < 0) { \
err(ctx, "%s: %s attribute write failed: %s\n", \
accfg_group_get_devname(group), \
#field, \
strerror(errno)); \
save_last_error(group->device, NULL, group, NULL); \
return -errno; \
} \
group->field = val; \
return 0; \
}
accfg_group_set_field(group, val, read_buffers_reserved)
accfg_group_set_field(group, val, read_buffers_allowed)
accfg_group_set_field(group, val, use_read_buffer_limit)
accfg_group_set_field(group, val, traffic_class_a)
accfg_group_set_field(group, val, traffic_class_b)
accfg_group_set_field(group, val, desc_progress_limit)
accfg_group_set_field(group, val, batch_progress_limit)
#define accfg_group_get_field(group, field) \
ACCFG_EXPORT int accfg_group_get_##field( \
struct accfg_group *group) \
{ \
return group->field; \
}
accfg_group_get_field(group, read_buffers_reserved)
accfg_group_get_field(group, read_buffers_allowed)
accfg_group_get_field(group, use_read_buffer_limit)
accfg_group_get_field(group, traffic_class_a)
accfg_group_get_field(group, traffic_class_b)
accfg_group_get_field(group, desc_progress_limit)
accfg_group_get_field(group, batch_progress_limit)
static void wqs_init(struct accfg_device *device)
{
struct accfg_ctx *ctx = device->ctx;
struct accfg_group *group = device->group;
if (group) {
if (group->wqs_init) {
dbg(ctx, "wq is initialized already\n");
return;
}
group->wqs_init = 1;
}
set_filename_prefix("wq");
device_parse(ctx, device->device_path, "wq", device->bus_type_str,
filter_file_name_prefix, device, add_wq);
}
ACCFG_EXPORT struct accfg_wq *accfg_wq_get_first(
struct accfg_device *device)
{
wqs_init(device);
return list_top(&device->wqs, struct accfg_wq, list);
}
ACCFG_EXPORT struct accfg_wq *accfg_wq_get_next(struct accfg_wq *wq)
{
struct accfg_device *device = wq->device;
return list_next(&device->wqs, wq, list);
}
ACCFG_EXPORT int accfg_wq_get_id(struct accfg_wq *wq)
{
return wq->id;
}
ACCFG_EXPORT struct accfg_wq *accfg_device_wq_get_by_id(
struct accfg_device *dev, int id)
{
struct accfg_wq *wq;
accfg_wq_foreach(dev, wq)
if (accfg_wq_get_id(wq) == id)
return wq;
return NULL;
}
ACCFG_EXPORT struct accfg_device *accfg_ctx_device_get_by_name(
struct accfg_ctx *ctx, const char *dev_name)
{
struct accfg_device *dev;
accfg_device_foreach(ctx, dev)
if (!strcmp(accfg_device_get_devname(dev), dev_name))
return dev;
return NULL;
}
ACCFG_EXPORT unsigned int accfg_wq_get_priv(struct accfg_wq *wq)
{
return wq->priv;
}
ACCFG_EXPORT struct accfg_group *accfg_wq_get_group(
struct accfg_wq *wq)
{
return wq->group;
}
ACCFG_EXPORT struct accfg_device *accfg_wq_get_device(
struct accfg_wq *wq)
{
return wq->device;
}
ACCFG_EXPORT struct accfg_ctx *accfg_wq_get_ctx(struct accfg_wq *wq)
{
return wq->device->ctx;
}
ACCFG_EXPORT const char *accfg_wq_get_devname(struct accfg_wq *wq)
{
return devpath_to_devname(wq->wq_path);
}
ACCFG_EXPORT int accfg_wq_get_cdev_minor(struct accfg_wq *wq)
{
return wq->cdev_minor;
}
ACCFG_EXPORT enum accfg_wq_type accfg_wq_get_type(struct accfg_wq *wq)
{
return wq->type;
}
ACCFG_EXPORT const char *accfg_wq_get_type_name(struct accfg_wq *wq)
{
return wq->name;
}
ACCFG_EXPORT const char *accfg_wq_get_driver_name(struct accfg_wq *wq)
{
return wq->driver_name;
}
ACCFG_EXPORT int accfg_wq_driver_name_validate(struct accfg_wq *wq,
const char *drv_name)
{
int rc = 0;
char *path = NULL;
struct accfg_device *device = accfg_wq_get_device(wq);
rc = get_driver_bind_path(device->bus_type_str, drv_name, &path);
if (rc < 0)
return 0;
rc = access(path, F_OK);
free(path);
return !rc;
}
ACCFG_EXPORT uint64_t accfg_wq_get_size(struct accfg_wq *wq)
{
return wq->size;
}
ACCFG_EXPORT unsigned int accfg_wq_get_max_batch_size(struct accfg_wq *wq)
{
return wq->max_batch_size;
}
ACCFG_EXPORT uint64_t accfg_wq_get_max_transfer_size(struct accfg_wq *wq)
{
return wq->max_transfer_size;
}
ACCFG_EXPORT int accfg_wq_get_occupancy(struct accfg_wq *wq)
{
long occ;
int dfd;
struct accfg_ctx *ctx = accfg_wq_get_ctx(wq);
dfd = open(wq->wq_path, O_PATH);
if (dfd < 0)
return -ENXIO;
occ = accfg_get_param_long(ctx, dfd, "occupancy");
close(dfd);
return occ;
}
ACCFG_EXPORT int accfg_wq_get_clients(struct accfg_wq *wq)
{
struct accfg_ctx *ctx = accfg_wq_get_ctx(wq);
char *path = wq->wq_buf;
char buf[SYSFS_ATTR_SIZE];
int len = wq->buf_len;
int rc;
rc = snprintf(wq->wq_buf, len, "%s/%s", wq->wq_path, "clients");
if (rc < 0 || rc >= len) {
err(ctx, "%s: snprintf error: %d\n", __func__, -errno);
return -errno;
}
if (sysfs_read_attr(ctx, path, buf) < 0) {
err(ctx, "%s: retrieve clients failed: '%s': %s\n",
__func__, wq->wq_path, strerror(errno));
save_last_error(wq->device, wq, NULL, NULL);
return -errno;
}
return atoi(buf);
}
ACCFG_EXPORT int accfg_wq_priority_boundary(struct accfg_wq *wq)
{
struct accfg_ctx *ctx = accfg_wq_get_ctx(wq);
if (wq->priority > WQ_PRIORITY_LIMIT) {
err(ctx, "wq_priority exceeds %d\n", WQ_PRIORITY_LIMIT);
return -ERANGE;
}
return 0;
}
ACCFG_EXPORT int accfg_wq_get_op_config(struct accfg_wq *wq,
struct accfg_op_config *op_config)
{
char *oc;
int dfd;
int rc;
struct accfg_ctx *ctx;
if (!wq || !op_config)
return -EINVAL;
ctx = accfg_wq_get_ctx(wq);
dfd = open(wq->wq_path, O_PATH);
if (dfd < 0)
return -errno;
oc = accfg_get_param_str(ctx, dfd, "op_config");
close(dfd);
if (!oc)
return -EIO;
rc = sscanf(oc, "%" SCNx32 ",%" SCNx32 ",%" SCNx32 ",%" SCNx32
",%" SCNx32 ",%" SCNx32 ",%" SCNx32 ",%" SCNx32,
&op_config->bits[0], &op_config->bits[1],
&op_config->bits[2], &op_config->bits[3],
&op_config->bits[4], &op_config->bits[5],
&op_config->bits[6], &op_config->bits[7]);
free(oc);
if (rc != 8)
return -EIO;
return 0;
}
ACCFG_EXPORT int accfg_wq_set_op_config(struct accfg_wq *wq,
struct accfg_op_config *op_config)
{
char oc[MAX_PARAM_LEN];
if (!wq || !op_config)
return -EINVAL;
snprintf(oc, MAX_PARAM_LEN,
"%" SCNx32 ",%" SCNx32 ",%" SCNx32 ",%" SCNx32
",%" SCNx32 ",%" SCNx32 ",%" SCNx32 ",%" SCNx32,
op_config->bits[0], op_config->bits[1],
op_config->bits[2], op_config->bits[3],
op_config->bits[4], op_config->bits[5],
op_config->bits[6], op_config->bits[7]);
return accfg_wq_set_op_config_str(wq, oc);
}
ACCFG_EXPORT int accfg_wq_set_op_config_str(struct accfg_wq *wq,
const char *op_config)
{
struct accfg_ctx *ctx;
int dfd;
int rc;
if (!wq || !op_config)
return -EINVAL;
ctx = accfg_wq_get_ctx(wq);
dfd = open(wq->wq_path, O_PATH);
if (dfd < 0)
return -errno;
rc = accfg_set_param(ctx, dfd, "op_config", op_config,
strlen(op_config));
close(dfd);
if (rc)
return -EIO;
return 0;
}
static int accfg_wq_retrieve_cdev_minor(struct accfg_wq *wq)
{
int dfd;
struct accfg_ctx *ctx = accfg_wq_get_ctx(wq);
dfd = open(wq->wq_path, O_PATH);
if (dfd < 0)
return -ENXIO;
wq->cdev_minor = accfg_get_param_long(ctx, dfd, "cdev_minor");
close(dfd);
return 0;
}
static bool accfg_wq_state_expected(struct accfg_wq *wq,
enum accfg_control_flag flag)
{
enum accfg_wq_state state, expected;
if (flag == ACCFG_WQ_DISABLE)
expected = ACCFG_WQ_DISABLED;
else if (flag == ACCFG_WQ_ENABLE)
expected = ACCFG_WQ_ENABLED;
else
return false;
state = accfg_wq_get_state(wq);
if (state != expected)
return false;
return true;
}
/* Helper function to parse the wq enable flag */
static int accfg_wq_control(struct accfg_wq *wq, enum accfg_control_flag flag,
bool force)
{
int rc = 0;
struct accfg_ctx *ctx = accfg_wq_get_ctx(wq);
char *path = NULL;
struct accfg_device *device = accfg_wq_get_device(wq);
const char *wq_name = accfg_wq_get_devname(wq);
if (flag == ACCFG_WQ_ENABLE) {
rc = get_driver_bind_path(device->bus_type_str,
wq->driver_name && strlen(wq->driver_name) ?
wq->driver_name :
IDXD_WQ_DEVICE_PORTAL(device, wq), &path);
if (rc < 0)
return rc;
if (wq->driver_name && access(path, F_OK)) {
fprintf(stderr, "Invalid wq driver name \"%s\"\n",
wq->driver_name);
free(path);
return -ENOENT;
}
} else if (flag == ACCFG_WQ_DISABLE) {
int clients;
rc = get_driver_unbind_path(device->bus_type_str, wq_name,
&path);
if (rc < 0)
return rc;
clients = accfg_wq_get_clients(wq);
if (clients > 0) {
err(ctx, "wq has clients: %d.\n", clients);
if (!force) {
err(ctx, " wq disable refused.\n");
return -EPERM;
}
}
}
if (path) {
rc = sysfs_write_attr(ctx, path, wq_name);
if (rc < 0) {
save_last_error(wq->device, wq, NULL, NULL);
free(path);
return rc;
}
}
free(path);
/* verify state */
if (!accfg_wq_state_expected(wq, flag)) {
err(ctx, "WQ not in expected state\n");
return -ENXIO;
}
if (accfg_wq_retrieve_cdev_minor(wq))
return -ENXIO;
return 0;
}
ACCFG_EXPORT int accfg_wq_enable(struct accfg_wq *wq)
{
return accfg_wq_control(wq, ACCFG_WQ_ENABLE, false);
}
ACCFG_EXPORT int accfg_wq_disable(struct accfg_wq *wq, bool force)
{
return accfg_wq_control(wq, ACCFG_WQ_DISABLE, force);
}
ACCFG_EXPORT enum accfg_wq_state accfg_wq_get_state(struct accfg_wq *wq)
{
char read_state[SYSFS_ATTR_SIZE];
struct accfg_ctx *ctx = accfg_wq_get_ctx(wq);
char *path = wq->wq_buf;
int len = wq->buf_len;
if (snprintf(path, len, "%s/state", wq->wq_path) >= len) {
err(ctx, "%s: buffer too small!\n", __func__);
return ACCFG_WQ_UNKNOWN;
}
if (sysfs_read_attr(ctx, path, read_state) < 0) {
err(ctx, "%s: sysfs_read_attr failed '%s': %s\n",
__func__, wq->wq_path, strerror(errno));
save_last_error(wq->device, wq, NULL, NULL);
return ACCFG_WQ_UNKNOWN;
}
if (strcmp(read_state, "disabled") == 0)
return ACCFG_WQ_DISABLED;
else if (strcmp(read_state, "enabled") == 0)
return ACCFG_WQ_ENABLED;
else if (strcmp(read_state, "quiescing") == 0)
return ACCFG_WQ_QUIESCING;
else if (strcmp(read_state, "locked") == 0)
return ACCFG_WQ_LOCKED;
return ACCFG_WQ_UNKNOWN;
}
ACCFG_EXPORT int accfg_wq_size_boundary(struct accfg_device *device,
int wq_num)
{
int max_wqs, total_wq_size = 0;
struct accfg_wq *wq, *next;
struct accfg_ctx *ctx;
if (!device)
return -EINVAL;
ctx = accfg_device_get_ctx(device);
max_wqs = accfg_device_get_max_work_queues(device);
if (wq_num > max_wqs) {
err(ctx, "number of wq in the device exceeds %d\n", max_wqs);
return -ERANGE;
}
list_for_each_safe(&device->wqs, wq, next, list) {
total_wq_size += wq->size;
if (total_wq_size > device->max_work_queues_size) {
err(ctx, "accumulated wq size exceeds %d\n",
device->max_work_queues_size);
return -ERANGE;
}
}
return 0;
}
ACCFG_EXPORT int accfg_wq_get_user_dev_path(struct accfg_wq *wq, char *buf,
size_t size)
{
struct dirent **d;
int n, n1;
char *f;
char p[PATH_MAX];
struct accfg_ctx *ctx;
int rc = 0;
ctx = accfg_device_get_ctx(wq->device);
sprintf(p, "/dev/%s", wq->device->device_type_str);
n1 = n = scandir(p, &d, NULL, alphasort);
if (n < 0) {
err(ctx, "Device path not found %s\n", p);
return -ENOENT;
}
sprintf(p, "%s-*", accfg_wq_get_devname(wq));
while (n--) {
f = &d[n]->d_name[0];
if (!fnmatch(p, f, 0) || !strcmp(accfg_wq_get_devname(wq), f))
break;
}
if (n < 0) {
err(ctx, "Device for %s not found at /dev/%s\n",
accfg_wq_get_devname(wq),
wq->device->device_type_str);
rc = -ENOENT;
goto ext_uacce;
}
n = sprintf(p, "/dev/%s/%s", wq->device->device_type_str, f);
if ((size_t)n >= size) {
err(ctx, "Buffer size too small. Need %d bytes\n", n + 1);
rc = -ERANGE;
goto ext_uacce;
}
strcpy(buf, p);
ext_uacce:
while (n1--)
free(d[n1]);
free(d);
return rc;
}
#define accfg_wq_set_field(wq, val, field) \
ACCFG_EXPORT int accfg_wq_set_##field( \
struct accfg_wq *wq, int val) \
{ \
struct accfg_ctx *ctx = accfg_wq_get_ctx(wq); \
char *path = wq->wq_buf; \
char buf[SYSFS_ATTR_SIZE]; \
int rc; \
rc = sprintf(wq->wq_buf, "%s/%s", wq->wq_path, #field); \
if (rc < 0) \
return -errno; \
if (sprintf(buf, "%d", val) < 0) { \
err(ctx, "%s: sprintf to buf failed: %s\n", \
accfg_wq_get_devname(wq), \
strerror(errno)); \
return -errno; \
} \
if (!accfg_device_get_configurable(wq->device)) { \
if (!strcmp(#field, "threshold")) { \
err(ctx, "device is not configurable\n"); \
return -errno; \
} \
} \
if (sysfs_write_attr(ctx, path, buf) < 0) { \
err(ctx, "%s: %s attribute write failed: %s\n", \
accfg_wq_get_devname(wq), \
#field, \
strerror(errno)); \
save_last_error(wq->device, wq, NULL, NULL); \
return -errno; \
} \
wq->field = val; \
return 0; \
}
accfg_wq_set_field(wq, val, size)
accfg_wq_set_field(wq, val, priority)
accfg_wq_set_field(wq, val, group_id)
accfg_wq_set_field(wq, val, block_on_fault)
accfg_wq_set_field(wq, val, threshold)
accfg_wq_set_field(wq, val, max_batch_size)
accfg_wq_set_field(wq, val, ats_disable)
accfg_wq_set_field(wq, val, prs_disable)
#define accfg_wq_set_long_field(wq, val, field) \
ACCFG_EXPORT int accfg_wq_set_##field( \
struct accfg_wq *wq, uint64_t val) \
{ \
struct accfg_ctx *ctx = accfg_wq_get_ctx(wq); \
char *path = wq->wq_buf; \
char buf[SYSFS_ATTR_SIZE]; \
int rc; \
rc = sprintf(wq->wq_buf, "%s/%s", wq->wq_path, #field); \
if (rc < 0) \
return -errno; \
if (sprintf(buf, "%" PRIu64, val) < 0) { \
err(ctx, "%s: sprintf to buf failed: %s\n", \
accfg_wq_get_devname(wq), \
strerror(errno)); \
return -errno; \
} \
if (!accfg_device_get_configurable(wq->device)) { \
err(ctx, "device is not configurable\n"); \
return -errno; \
} \
if (sysfs_write_attr(ctx, path, buf) < 0) { \
err(ctx, "%s: write failed: %s\n", \
accfg_wq_get_devname(wq), \
strerror(errno)); \
save_last_error(wq->device, wq, NULL, NULL); \
return -errno; \
} \
wq->field = val; \
return 0; \
}
accfg_wq_set_long_field(wq, val, max_transfer_size)
#define accfg_wq_set_str_field(wq, val, field) \
ACCFG_EXPORT int accfg_wq_set_str_##field( \
struct accfg_wq *wq, const char *val) \
{ \
struct accfg_ctx *ctx = accfg_wq_get_ctx(wq); \
char *path = wq->wq_buf; \
char buf[SYSFS_ATTR_SIZE]; \
int rc; \
rc = sprintf(wq->wq_buf, "%s/%s", wq->wq_path, #field); \
if (rc < 0) \
return -errno; \
if (sprintf(buf, "%s", val) < 0) { \
err(ctx, "%s: sprintf to buf failed: %s\n", \
accfg_wq_get_devname(wq), \
strerror(errno)); \
return -errno; \
} \
if (!accfg_device_get_configurable(wq->device)) { \
if (strcmp(#field, "name") != 0 && \
strcmp(#field, "driver_name") != 0) { \
err(ctx, "device is not configurable\n"); \
return -errno; \
} \
} \
rc = sysfs_write_attr(ctx, path, buf); \
if (rc < 0) { \
/* Silently skip attrs not supported by driver */ \
if (rc == -ENOENT && !strcmp(#field, "driver_name")) \
return 0; \
err(ctx, "%s: write failed: %s\n", \
accfg_wq_get_devname(wq), \
strerror(errno)); \
save_last_error(wq->device, wq, NULL, NULL); \
return -errno; \
} \
if (wq->field) \
free(wq->field); \
wq->field = strdup(val); \
if (!wq->field) \
return -ENOMEM; \
return 0; \
}
accfg_wq_set_str_field(wq, val, mode)
accfg_wq_set_str_field(wq, val, name)
accfg_wq_set_str_field(wq, val, driver_name)
static int wq_parse_type(struct accfg_wq *wq, char *wq_type);
ACCFG_EXPORT int accfg_wq_set_str_type(struct accfg_wq *wq, const char *val)
{
struct accfg_ctx *ctx = accfg_wq_get_ctx(wq);
char *path = wq->wq_buf;
char buf[SYSFS_ATTR_SIZE];
int rc;
char *tmp;
rc = sprintf(wq->wq_buf, "%s/%s", wq->wq_path, "type");
if (rc < 0)
return -errno;
if (sprintf(buf, "%s", val) < 0) {
err(ctx, "%s: sprintf to buf failed: %s\n",
accfg_wq_get_devname(wq),
strerror(errno));
return -errno;
}
if (sysfs_write_attr(ctx, path, buf) < 0) {
err(ctx, "%s: write failed: %s\n",
accfg_wq_get_devname(wq),
strerror(errno));
save_last_error(wq->device, wq, NULL, NULL);
return -errno;
}
tmp = strdup(val);
if (!tmp)
return -ENOMEM;
rc = wq_parse_type(wq, tmp);
free(tmp);
if (rc < 0)
return rc;
return 0;
}
#define accfg_wq_get_field(wq, field) \
ACCFG_EXPORT int accfg_wq_get_##field( \
struct accfg_wq *wq) \
{ \
return wq->field; \
}
accfg_wq_get_field(wq, priority)
accfg_wq_get_field(wq, threshold)
accfg_wq_get_field(wq, group_id)
accfg_wq_get_field(wq, block_on_fault)
accfg_wq_get_field(wq, ats_disable)
accfg_wq_get_field(wq, prs_disable)
ACCFG_EXPORT int accfg_wq_set_mode(struct accfg_wq *wq,
enum accfg_wq_mode wq_mode)
{
if (wq_mode >= ACCFG_WQ_MODE_UNKNOWN)
return -EINVAL;
return accfg_wq_set_str_mode(wq, accfg_wq_mode_str[wq_mode]);
}
ACCFG_EXPORT struct accfg_engine *accfg_engine_get_first(
struct accfg_device *device)
{
engines_init(device);
return list_top(&device->engines, struct accfg_engine, list);
}
ACCFG_EXPORT struct accfg_engine *accfg_engine_get_next(
struct accfg_engine *engine)
{
struct accfg_device *device = engine->device;
return list_next(&device->engines, engine, list);
}
ACCFG_EXPORT int accfg_engine_get_id(
struct accfg_engine *engine)
{
return engine->id;
}
ACCFG_EXPORT struct accfg_engine *accfg_device_engine_get_by_id(
struct accfg_device *dev, int id)
{
struct accfg_engine *engine;
accfg_engine_foreach(dev, engine)
if (accfg_engine_get_id(engine) == id)
return engine;
return NULL;
}
ACCFG_EXPORT struct accfg_group *accfg_engine_get_group(
struct accfg_engine *engine)
{
return engine->group;
}
ACCFG_EXPORT struct accfg_device *accfg_engine_get_device(
struct accfg_engine *engine)
{
return engine->group->device;
}
ACCFG_EXPORT struct accfg_ctx *accfg_engine_get_ctx(
struct accfg_engine *engine)
{
return engine->device->ctx;
}
#define accfg_engine_set_field(engine, val, field) \
ACCFG_EXPORT int accfg_engine_set_##field( \
struct accfg_engine *engine, int val) \
{ \
struct accfg_ctx *ctx = accfg_engine_get_ctx(engine); \
char *path = engine->engine_buf; \
char buf[SYSFS_ATTR_SIZE]; \
int rc; \
rc = sprintf(engine->engine_buf, "%s/%s", \
engine->engine_path, #field); \
if (rc < 0) \
return -errno; \
if (sprintf(buf, "%d", val) < 0) { \
err(ctx, "%s: sprintf to buf failed: %s\n", \
accfg_engine_get_devname(engine), \
strerror(errno)); \
return -errno; \
} \
if (sysfs_write_attr(ctx, path, buf) < 0) { \
err(ctx, "%s: %s attribute write failed: %s\n", \
accfg_engine_get_devname(engine), \
#field, \
strerror(errno)); \
save_last_error(engine->device, NULL, NULL, engine); \
return -errno; \
} \
engine->field = val; \
return 0; \
}
accfg_engine_set_field(engine, val, group_id)
#define accfg_engine_get_field(engine, field) \
ACCFG_EXPORT int accfg_engine_get_##field( \
struct accfg_engine *engine) \
{ \
return engine->field; \
}
accfg_engine_get_field(engine, group_id)
|