1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
|
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
/*
* OPAL Processor Runtime Diagnostics (PRD)
* Runs Hostboot RunTime (HBRT) code in a userspace wrapper
*
* Firmware in userspace? Brilliant!
*
* Copyright 2014-2019 IBM Corp.
*/
#define _GNU_SOURCE
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <poll.h>
#include <signal.h>
#include <dirent.h>
#include <endian.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <linux/ipmi.h>
#include <linux/limits.h>
#include <asm/opal-prd.h>
#include <opal-api.h>
#include <types.h>
#include <ccan/list/list.h>
#include "opal-prd.h"
#include "hostboot-interface.h"
#include "module.h"
#include "pnor.h"
#include "i2c.h"
struct prd_range {
const char *name;
uint64_t physaddr;
uint64_t size;
void *buf;
bool multiple;
uint32_t instance;
};
struct prd_msgq_item {
struct list_node list;
struct opal_prd_msg msg;
};
struct opal_prd_ctx {
int fd;
int socket;
struct opal_prd_info info;
struct prd_range *ranges;
int n_ranges;
bool fw_range_instances;
long page_size;
void *code_addr;
size_t code_size;
bool debug;
struct pnor pnor;
char *hbrt_file_name;
bool use_syslog;
bool expert_mode;
struct list_head msgq;
struct opal_prd_msg *msg;
size_t msg_alloc_len;
void (*vlog)(int, const char *, va_list);
};
enum control_msg_type {
CONTROL_MSG_ENABLE_OCCS = 0x00,
CONTROL_MSG_DISABLE_OCCS = 0x01,
CONTROL_MSG_TEMP_OCC_RESET = 0x02,
CONTROL_MSG_TEMP_OCC_ERROR = 0x03,
CONTROL_MSG_ATTR_OVERRIDE = 0x04,
CONTROL_MSG_HTMGT_PASSTHRU = 0x05,
CONTROL_MSG_RUN_CMD = 0x30,
};
struct control_msg {
enum control_msg_type type;
int response;
union {
struct {
unsigned int argc;
} run_cmd;
struct {
uint64_t chip;
} occ_reset;
struct {
uint64_t chip;
} occ_error;
};
unsigned int data_len;
unsigned char data[];
};
#define MAX_CONTROL_MSG_BUF 4096
static struct opal_prd_ctx *ctx;
static const char *opal_prd_devnode = "/dev/opal-prd";
static const char *opal_prd_socket = "/run/opal-prd-control";
static const char *hbrt_code_region_name = "hbrt-code-image";
static const char *hbrt_code_region_name_ibm = "ibm,hbrt-code-image";
static const int opal_prd_version = 1;
static uint64_t opal_prd_ipoll = 0xf000000000000000;
static const int max_msgq_len = 16;
static const char *ipmi_devnode = "/dev/ipmi0";
static const int ipmi_timeout_ms = 5000;
static const char *devicetree_base =
"/sys/firmware/devicetree/base";
/* Memory error handling */
static const char *mem_offline_soft =
"/sys/devices/system/memory/soft_offline_page";
static const char *mem_offline_hard =
"/sys/devices/system/memory/hard_offline_page";
#define ADDR_STRING_SZ 20 /* Hold %16lx */
/* This is the "real" HBRT call table for calling into HBRT as
* provided by it. It will be used by the assembly thunk
*/
struct runtime_interfaces *hservice_runtime;
struct runtime_interfaces hservice_runtime_fixed;
/* This is the callback table provided by assembly code */
extern struct host_interfaces hinterface;
/* Create opd to call hostservice init */
struct func_desc {
void *addr;
void *toc;
} hbrt_entry;
static int nr_chips;
static u64 chips[256];
static int read_prd_msg(struct opal_prd_ctx *ctx);
static struct prd_range *find_range(const char *name, uint32_t instance)
{
struct prd_range *range;
unsigned int i;
for (i = 0; i < ctx->n_ranges; i++) {
range = &ctx->ranges[i];
if (strcmp(range->name, name))
continue;
if (range->multiple && range->instance != instance)
continue;
return range;
}
return NULL;
}
static void pr_log_stdio(int priority, const char *fmt, va_list ap)
{
if (!ctx->debug && priority >= LOG_DEBUG)
return;
vprintf(fmt, ap);
printf("\n");
if (ctx->debug)
fflush(stdout);
}
/* standard logging prefixes:
* HBRT: Messages from hostboot runtime code
* FW: Interactions with OPAL firmware
* IMAGE: HBRT image loading
* MEM: Memory failure interface
* SCOM: Chip SCOM interface
* IPMI: IPMI interface
* PNOR: PNOR interface
* I2C: i2c interface
* PM: PM/OCC interface
* CTRL: User-triggered control events
* KMOD: Kernel module functions
*/
void pr_log(int priority, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
ctx->vlog(priority, fmt, ap);
va_end(ap);
}
static void pr_log_nocall(const char *name)
{
pr_log(LOG_WARNING, "HBRT: Call %s not provided", name);
}
static void hexdump(const uint8_t *data, uint32_t len)
{
int i;
for (i = 0; i < len; i++) {
if (i % 16 == 0)
printf("\n");
else if(i % 4 == 0)
printf(" ");
printf("%02x", data[i]);
}
printf("\n");
}
static void pr_log_daemon_init(void)
{
if (ctx->use_syslog) {
openlog("opal-prd", LOG_NDELAY, LOG_DAEMON);
ctx->vlog = vsyslog;
}
}
/* Check service processor type */
static bool is_fsp_system(void)
{
bool fsp_system = true;
char *path;
int rc;
rc = asprintf(&path, "%s/fsps", devicetree_base);
if (rc < 0) {
pr_log(LOG_ERR, "FW: error creating '/fsps' path %m");
return false;
}
if (access(path, F_OK))
fsp_system = false;
free(path);
return fsp_system;
}
/**
* ABI check that we can't perform at build-time: we want to ensure that the
* layout of struct host_interfaces matches that defined in the thunk.
*/
static void check_abi(void)
{
extern unsigned char __hinterface_start, __hinterface_pad,
__hinterface_end;
/* ensure our struct size matches the thunk definition */
assert((&__hinterface_end - &__hinterface_start)
== sizeof(struct host_interfaces));
/* ensure the padding layout is as expected */
assert((void *)&__hinterface_start == (void *)&hinterface);
assert((void *)&__hinterface_pad == (void *)&hinterface.reserved);
}
/* HBRT init wrappers */
extern struct runtime_interfaces *call_hbrt_init(struct host_interfaces *);
/* hservice Call wrappers */
extern void call_cxxtestExecute(void *);
extern int call_handle_attns(uint64_t i_proc,
uint64_t i_ipollStatus,
uint64_t i_ipollMask);
extern void call_process_occ_error (uint64_t i_chipId);
extern int call_enable_attns(void);
extern int call_enable_occ_actuation(bool i_occActivation);
extern void call_process_occ_reset(uint64_t i_chipId);
extern int call_mfg_htmgt_pass_thru(uint16_t i_cmdLength, uint8_t *i_cmdData,
uint16_t *o_rspLength, uint8_t *o_rspData);
extern int call_apply_attr_override(uint8_t *i_data, size_t size);
extern int call_run_command(int argc, const char **argv, char **o_outString);
extern int call_sbe_message_passing(uint32_t i_chipId);
extern uint64_t call_get_ipoll_events(void);
extern int call_firmware_notify(uint64_t len, void *data);
extern int call_reset_pm_complex(uint64_t chip);
extern int call_load_pm_complex(u64 chip, u64 homer, u64 occ_common, u32 mode);
extern int call_start_pm_complex(u64 chip);
void hservice_puts(const char *str)
{
int priority = LOG_INFO;
/* Interpret the 2-character ERR_MRK/FAIL_MRK/WARN_MRK prefixes that
* may be present on HBRT log messages, and bump the log priority as
* appropriate.
*/
if (strlen(str) >= 2 && str[1] == '>') {
switch (str[0]) {
case 'E':
case 'F':
priority = LOG_ERR;
break;
case 'W':
priority = LOG_WARNING;
break;
}
}
pr_log(priority, "HBRT: %s", str);
}
void hservice_assert(void)
{
pr_log(LOG_ERR, "HBRT: Failed assertion! exiting.");
exit(EXIT_FAILURE);
}
void *hservice_malloc(size_t size)
{
return malloc(size);
}
void hservice_free(void *ptr)
{
free(ptr);
}
void *hservice_realloc(void *ptr, size_t size)
{
return realloc(ptr, size);
}
int hservice_scom_read(uint64_t chip_id, uint64_t addr, void *buf)
{
int rc;
struct opal_prd_scom scom;
scom.chip = chip_id;
scom.addr = addr;
rc = ioctl(ctx->fd, OPAL_PRD_SCOM_READ, &scom);
if (rc) {
pr_log(LOG_ERR, "SCOM: ioctl read(chip 0x%lx, addr 0x%lx) "
"failed: %m", chip_id, addr);
return 0;
}
rc = (int)scom.rc;
pr_debug("SCOM: read: chip 0x%lx, addr 0x%lx, val 0x%lx, rc %d",
chip_id, addr, scom.data, rc);
*(uint64_t *)buf = htobe64(scom.data);
return rc;
}
int hservice_scom_write(uint64_t chip_id, uint64_t addr,
const void *buf)
{
int rc;
struct opal_prd_scom scom;
scom.chip = chip_id;
scom.addr = addr;
scom.data = be64toh(*(uint64_t *)buf);
rc = ioctl(ctx->fd, OPAL_PRD_SCOM_WRITE, &scom);
if (rc) {
pr_log(LOG_ERR, "SCOM: ioctl write(chip 0x%lx, addr 0x%lx) "
"failed: %m", chip_id, addr);
return 0;
}
rc = (int)scom.rc;
pr_debug("SCOM: write: chip 0x%lx, addr 0x%lx, val 0x%lx, rc %d",
chip_id, addr, scom.data, rc);
return rc;
}
uint64_t hservice_get_reserved_mem(const char *name, uint32_t instance)
{
struct prd_range *range;
pr_debug("IMAGE: hservice_get_reserved_mem: %s, %d", name, instance);
range = find_range(name, instance);
if (!range) {
pr_log(LOG_WARNING, "IMAGE: get_reserved_mem: "
"no such range %s", name);
return 0;
}
if (!range->buf) {
uint64_t align_physaddr, offset;
pr_debug("IMAGE: Mapping 0x%016lx 0x%08lx %s[%d]",
range->physaddr, range->size,
range->name, range->instance);
align_physaddr = range->physaddr & ~(ctx->page_size-1);
offset = range->physaddr & (ctx->page_size-1);
range->buf = mmap(NULL, range->size, PROT_WRITE | PROT_READ,
MAP_SHARED, ctx->fd, align_physaddr);
if (range->buf == MAP_FAILED)
pr_log(LOG_ERR,
"IMAGE: mmap of %s[%d](0x%016lx) failed: %m",
name, instance, range->physaddr);
else
range->buf += offset;
}
if (range->buf == MAP_FAILED) {
pr_log(LOG_WARNING,
"IMAGE: get_reserved_mem: %s[%d] has no vaddr",
name, instance);
return 0;
}
pr_debug(
"IMAGE: hservice_get_reserved_mem: %s[%d](0x%016lx) address %p",
name, range->instance, range->physaddr,
range->buf);
return (uint64_t)range->buf;
}
void hservice_nanosleep(uint64_t i_seconds, uint64_t i_nano_seconds)
{
const struct timespec ns = {
.tv_sec = i_seconds,
.tv_nsec = i_nano_seconds
};
nanosleep(&ns, NULL);
}
int hservice_set_page_execute(void *addr)
{
/* HBRT calls this on the pages that are already being executed,
* nothing to do here */
return -1;
}
int hservice_clock_gettime(clockid_t i_clkId, struct timespec *o_tp)
{
struct timespec tmp;
int rc;
rc = clock_gettime(i_clkId, &tmp);
if (rc)
return rc;
o_tp->tv_sec = htobe64(tmp.tv_sec);
o_tp->tv_nsec = htobe64(tmp.tv_nsec);
return 0;
}
int hservice_pnor_read(uint32_t i_proc, const char* i_partitionName,
uint64_t i_offset, void* o_data, size_t i_sizeBytes)
{
return pnor_operation(&ctx->pnor, i_partitionName, i_offset, o_data,
i_sizeBytes, PNOR_OP_READ);
}
int hservice_pnor_write(uint32_t i_proc, const char* i_partitionName,
uint64_t i_offset, void* o_data, size_t i_sizeBytes)
{
return pnor_operation(&ctx->pnor, i_partitionName, i_offset, o_data,
i_sizeBytes, PNOR_OP_WRITE);
}
int hservice_i2c_read(uint64_t i_master, uint16_t i_devAddr,
uint32_t i_offsetSize, uint32_t i_offset,
uint32_t i_length, void* o_data)
{
uint32_t chip_id;
uint8_t engine, port;
chip_id = (i_master & HBRT_I2C_MASTER_CHIP_MASK) >>
HBRT_I2C_MASTER_CHIP_SHIFT;
engine = (i_master & HBRT_I2C_MASTER_ENGINE_MASK) >>
HBRT_I2C_MASTER_ENGINE_SHIFT;
port = (i_master & HBRT_I2C_MASTER_PORT_MASK) >>
HBRT_I2C_MASTER_PORT_SHIFT;
return i2c_read(chip_id, engine, port, i_devAddr, i_offsetSize,
i_offset, i_length, o_data);
}
int hservice_i2c_write(uint64_t i_master, uint16_t i_devAddr,
uint32_t i_offsetSize, uint32_t i_offset,
uint32_t i_length, void* i_data)
{
uint32_t chip_id;
uint8_t engine, port;
chip_id = (i_master & HBRT_I2C_MASTER_CHIP_MASK) >>
HBRT_I2C_MASTER_CHIP_SHIFT;
engine = (i_master & HBRT_I2C_MASTER_ENGINE_MASK) >>
HBRT_I2C_MASTER_ENGINE_SHIFT;
port = (i_master & HBRT_I2C_MASTER_PORT_MASK) >>
HBRT_I2C_MASTER_PORT_SHIFT;
return i2c_write(chip_id, engine, port, i_devAddr, i_offsetSize,
i_offset, i_length, i_data);
}
int hservice_wakeup(u32 core, u32 mode)
{
struct opal_prd_msg msg;
msg.hdr.type = OPAL_PRD_MSG_TYPE_CORE_SPECIAL_WAKEUP;
msg.hdr.size = htobe16(sizeof(msg));
msg.spl_wakeup.core = htobe32(core);
msg.spl_wakeup.mode = htobe32(mode);
if (write(ctx->fd, &msg, sizeof(msg)) != sizeof(msg)) {
pr_log(LOG_ERR, "FW: Failed to send CORE_SPECIAL_WAKEUP msg %x : %m\n",
core);
return -1;
}
return 0;
}
static void pnor_load_module(struct opal_prd_ctx *ctx)
{
insert_module("powernv_flash");
}
static void ipmi_init(struct opal_prd_ctx *ctx)
{
insert_module("ipmi_devintf");
}
static int ipmi_send(int fd, uint8_t netfn, uint8_t cmd, long seq,
uint8_t *buf, size_t len)
{
struct ipmi_system_interface_addr addr;
struct ipmi_req req;
int rc;
memset(&addr, 0, sizeof(addr));
addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
addr.channel = IPMI_BMC_CHANNEL;
memset(&req, 0, sizeof(req));
req.addr = (unsigned char *)&addr;
req.addr_len = sizeof(addr);
req.msgid = seq;
req.msg.netfn = netfn;
req.msg.cmd = cmd;
req.msg.data = buf;
req.msg.data_len = len;
rc = ioctl(fd, IPMICTL_SEND_COMMAND, &req);
if (rc < 0)
return -1;
return 0;
}
static int ipmi_recv(int fd, uint8_t *netfn, uint8_t *cmd, long *seq,
uint8_t *buf, size_t *len)
{
struct ipmi_recv recv;
struct ipmi_addr addr;
int rc;
recv.addr = (unsigned char *)&addr;
recv.addr_len = sizeof(addr);
recv.msg.data = buf;
recv.msg.data_len = *len;
rc = ioctl(fd, IPMICTL_RECEIVE_MSG_TRUNC, &recv);
if (rc < 0 && errno != EMSGSIZE) {
pr_log(LOG_WARNING, "IPMI: recv (%zd bytes) failed: %m", *len);
return -1;
} else if (rc < 0 && errno == EMSGSIZE) {
pr_log(LOG_NOTICE, "IPMI: truncated message (netfn %d, cmd %d, "
"size %zd), continuing anyway",
recv.msg.netfn, recv.msg.cmd, *len);
}
*netfn = recv.msg.netfn;
*cmd = recv.msg.cmd;
*seq = recv.msgid;
*len = recv.msg.data_len;
return 0;
}
int hservice_ipmi_msg(uint8_t netfn, uint8_t cmd,
void *tx_buf, size_t tx_size,
void *rx_buf, size_t *rx_size)
{
struct timeval start, now, delta;
struct pollfd pollfds[1];
static long seq;
size_t size;
int rc, fd;
size = be64toh(*rx_size);
fd = open(ipmi_devnode, O_RDWR);
if (fd < 0) {
pr_log(LOG_WARNING, "IPMI: Failed to open IPMI device %s: %m",
ipmi_devnode);
return -1;
}
seq++;
pr_debug("IPMI: sending %zd bytes (netfn 0x%02x, cmd 0x%02x)",
tx_size, netfn, cmd);
rc = ipmi_send(fd, netfn, cmd, seq, tx_buf, tx_size);
if (rc) {
pr_log(LOG_WARNING, "IPMI: send failed");
goto out;
}
gettimeofday(&start, NULL);
pollfds[0].fd = fd;
pollfds[0].events = POLLIN;
for (;;) {
long rx_seq;
int timeout;
gettimeofday(&now, NULL);
timersub(&now, &start, &delta);
timeout = ipmi_timeout_ms - ((delta.tv_sec * 1000) +
(delta.tv_usec / 1000));
if (timeout < 0)
timeout = 0;
rc = poll(pollfds, 1, timeout);
if (rc < 0) {
pr_log(LOG_ERR, "IPMI: poll(%s) failed: %m",
ipmi_devnode);
break;
}
if (rc == 0) {
pr_log(LOG_WARNING, "IPMI: response timeout (>%dms)",
ipmi_timeout_ms);
rc = -1;
break;
}
rc = ipmi_recv(fd, &netfn, &cmd, &rx_seq, rx_buf, &size);
if (rc)
break;
if (seq != rx_seq) {
pr_log(LOG_NOTICE, "IPMI: out-of-sequence reply: %ld, "
"expected %ld. Dropping message.",
rx_seq, seq);
continue;
}
pr_debug("IPMI: received %zd bytes", tx_size);
*rx_size = be64toh(size);
rc = 0;
break;
}
out:
close(fd);
return rc;
}
static int memory_error_worker(const char *sysfsfile, const char *type,
uint64_t i_start_addr, uint64_t i_endAddr)
{
int memfd, rc, n, ret = 0;
char buf[ADDR_STRING_SZ];
uint64_t addr;
memfd = open(sysfsfile, O_WRONLY);
if (memfd < 0) {
pr_log(LOG_CRIT, "MEM: Failed to offline memory! "
"Unable to open sysfs node %s: %m", sysfsfile);
return -1;
}
for (addr = i_start_addr; addr <= i_endAddr; addr += ctx->page_size) {
n = snprintf(buf, ADDR_STRING_SZ, "0x%lx", addr);
rc = write(memfd, buf, n);
if (rc != n) {
pr_log(LOG_CRIT, "MEM: Failed to offline memory! "
"page addr: %016lx type: %s: %m",
addr, type);
ret = 1;
}
}
pr_log(LOG_CRIT, "MEM: Offlined %016lx,%016lx, type %s: %m\n",
i_start_addr, addr, type);
close(memfd);
return ret;
}
int hservice_memory_error(uint64_t i_start_addr, uint64_t i_endAddr,
enum MemoryError_t i_errorType)
{
const char *sysfsfile, *typestr;
pid_t pid;
switch(i_errorType) {
case MEMORY_ERROR_CE:
sysfsfile = mem_offline_soft;
typestr = "correctable";
break;
case MEMORY_ERROR_UE:
sysfsfile = mem_offline_hard;
typestr = "uncorrectable";
break;
default:
pr_log(LOG_WARNING, "MEM: Invalid memory error type %d",
i_errorType);
return -1;
}
pr_log(LOG_ERR, "MEM: Memory error: range %016lx-%016lx, type: %s",
i_start_addr, i_endAddr, typestr);
/*
* HBRT expects the memory offlining process to happen in the background
* after the notification is delivered.
*/
pid = fork();
if (pid > 0)
exit(memory_error_worker(sysfsfile, typestr, i_start_addr, i_endAddr));
if (pid < 0) {
perror("MEM: unable to fork worker to offline memory!\n");
return -1;
}
pr_log(LOG_INFO, "MEM: forked off %d to handle mem error\n", pid);
return 0;
}
uint64_t hservice_get_interface_capabilities(uint64_t set)
{
if (set == HBRT_CAPS_SET1_OPAL)
return HBRT_CAPS_OPAL_HAS_XSCOM_RC ||
HBRT_CAPS_OPAL_HAS_WAKEUP_SUPPORT;
return 0;
}
uint64_t hservice_firmware_request(uint64_t req_len, void *req,
uint64_t *resp_lenp, void *resp)
{
struct opal_prd_msg *msg = ctx->msg;
uint64_t resp_len;
size_t size;
int rc, n;
resp_len = be64_to_cpu(*resp_lenp);
pr_log(LOG_DEBUG,
"HBRT: firmware request: %lu bytes req, %lu bytes resp",
req_len, resp_len);
/* sanity check for potential overflows */
if (req_len > 0xffff || resp_len > 0xffff)
return -1;
size = sizeof(msg->hdr) + sizeof(msg->token) +
sizeof(msg->fw_req) + req_len;
/* we need the entire message to fit within the 2-byte size field */
if (size > 0xffff)
return -1;
/* variable sized message, so we may need to expand our buffer */
if (size > ctx->msg_alloc_len) {
msg = realloc(ctx->msg, size);
if (!msg) {
pr_log(LOG_ERR,
"FW: failed to expand message buffer: %m");
return -1;
}
ctx->msg = msg;
ctx->msg_alloc_len = size;
}
memset(msg, 0, size);
/* construct request message... */
msg->hdr.type = OPAL_PRD_MSG_TYPE_FIRMWARE_REQUEST;
msg->hdr.size = htobe16(size);
msg->fw_req.req_len = htobe64(req_len);
msg->fw_req.resp_len = htobe64(resp_len);
memcpy(msg->fw_req.data, req, req_len);
hexdump((void *)msg, size);
/* ... and send to firmware */
rc = write(ctx->fd, msg, size);
if (rc != size) {
pr_log(LOG_WARNING,
"FW: Failed to send FIRMWARE_REQUEST message: %m");
return -1;
}
/* We have an "inner" poll loop here, as we want to ensure that the
* next entry into HBRT is the return from this function. So, only
* read from the prd fd, and queue anything that isn't a response
* to this request
*/
n = 0;
for (;;) {
struct prd_msgq_item *item;
rc = read_prd_msg(ctx);
if (rc)
return -1;
msg = ctx->msg;
if (msg->hdr.type == OPAL_PRD_MSG_TYPE_FIRMWARE_RESPONSE) {
size = be64toh(msg->fw_resp.len);
if (size > resp_len)
return -1;
/* success! a valid response that fits into HBRT's
* resp buffer */
memcpy(resp, msg->fw_resp.data, size);
*resp_lenp = htobe64(size);
return 0;
}
/* not a response? queue up for later consumption */
if (++n > max_msgq_len) {
pr_log(LOG_ERR,
"FW: too many messages queued (%d) while "
"waiting for FIRMWARE_RESPONSE", n);
return -1;
}
size = be16toh(msg->hdr.size);
item = malloc(sizeof(*item) + size);
memcpy(&item->msg, msg, size);
list_add_tail(&ctx->msgq, &item->list);
}
}
int hservices_init(struct opal_prd_ctx *ctx, void *code)
{
uint64_t *s, *d;
int i, sz;
pr_debug("IMAGE: code address: %p", code);
/* We enter at 0x100 into the image. */
/* Load func desc in BE since we reverse it in thunk */
hbrt_entry.addr = (void *)htobe64((unsigned long)code + 0x100);
hbrt_entry.toc = 0; /* No toc for init entry point */
if (memcmp(code, "HBRTVERS", 8) != 0) {
pr_log(LOG_ERR, "IMAGE: Bad signature for "
"ibm,hbrt-code-image! exiting");
return -1;
}
pr_debug("IMAGE: calling ibm,hbrt_init()");
hservice_runtime = call_hbrt_init(&hinterface);
if (!hservice_runtime) {
pr_log(LOG_ERR, "IMAGE: hbrt_init failed, exiting");
return -1;
}
pr_log(LOG_NOTICE, "IMAGE: hbrt_init complete, version %016lx",
hservice_runtime->interface_version);
sz = sizeof(struct runtime_interfaces)/sizeof(uint64_t);
s = (uint64_t *)hservice_runtime;
d = (uint64_t *)&hservice_runtime_fixed;
/* Byte swap the function pointers */
for (i = 0; i < sz; i++)
d[i] = be64toh(s[i]);
return 0;
}
static void fixup_hinterface_table(void)
{
uint64_t *t64;
unsigned int i, sz;
/* Swap interface version */
hinterface.interface_version =
htobe64(hinterface.interface_version);
/* Swap OPDs */
sz = sizeof(struct host_interfaces) / sizeof(uint64_t);
t64 = (uint64_t *)&hinterface;
for (i = 1; i < sz; i++) {
uint64_t *opd = (uint64_t *)t64[i];
if (!opd)
continue;
t64[i] = htobe64(t64[i]);
opd[0] = htobe64(opd[0]);
opd[1] = htobe64(opd[1]);
opd[2] = htobe64(opd[2]);
}
}
static int map_hbrt_file(struct opal_prd_ctx *ctx, const char *name)
{
struct stat statbuf;
int fd, rc;
void *buf;
fd = open(name, O_RDONLY);
if (fd < 0) {
pr_log(LOG_ERR, "IMAGE: HBRT file open(%s) failed: %m", name);
return -1;
}
rc = fstat(fd, &statbuf);
if (rc < 0) {
pr_log(LOG_ERR, "IMAGE: HBRT file fstat(%s) failed: %m", name);
close(fd);
return -1;
}
buf = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE, fd, 0);
close(fd);
if (buf == MAP_FAILED) {
pr_log(LOG_ERR, "IMAGE: HBRT file mmap(%s, 0x%zx) failed: %m",
name, statbuf.st_size);
return -1;
}
ctx->code_addr = buf;
ctx->code_size = statbuf.st_size;
return -0;
}
static int map_hbrt_physmem(struct opal_prd_ctx *ctx, const char *name)
{
struct prd_range *range;
int rc;
void *buf;
void *ro_buf;
range = find_range(name, 0);
if (!range) {
pr_log(LOG_ERR, "IMAGE: can't find code region %s", name);
return -1;
}
ro_buf = mmap(NULL, range->size, PROT_READ,
MAP_PRIVATE, ctx->fd, range->physaddr);
if (ro_buf == MAP_FAILED) {
pr_log(LOG_ERR, "IMAGE: mmap(range:%s, "
"phys:0x%016lx, size:0x%016lx) failed: %m",
name, range->physaddr, range->size);
return -1;
}
buf = mmap(NULL, range->size, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1 , 0);
if (buf == MAP_FAILED) {
pr_log(LOG_ERR, "IMAGE: anon mmap(size:0x%016lx) failed: %m",
range->size);
return -1;
}
memcpy(buf, ro_buf, range->size);
rc = munmap(ro_buf, range->size);
if (rc < 0) {
pr_log(LOG_ERR, "IMAGE: munmap("
"phys:0x%016lx, size:0x%016lx) failed: %m",
range->physaddr, range->size);
return -1;
}
/*
* FIXME: We shouldn't be mapping the memory as RWX, but HBRT appears to
* require the ability to write into the image at runtime.
*/
rc = mprotect(buf, range->size, PROT_READ | PROT_WRITE | PROT_EXEC);
if (rc < 0) {
pr_log(LOG_ERR, "IMAGE: mprotect(phys:%p, "
"size:0x%016lx, rwx) failed: %m",
buf, range->size);
return -1;
}
ctx->code_addr = buf;
ctx->code_size = range->size;
return 0;
}
static void dump_hbrt_map(struct opal_prd_ctx *ctx)
{
const char *dump_name = "hbrt.bin";
int fd, rc;
if (!ctx->debug)
return;
fd = open(dump_name, O_WRONLY | O_CREAT, 0644);
if (fd < 0) {
pr_log(LOG_NOTICE, "IMAGE: couldn't debug image %s for writing",
dump_name);
return;
}
rc = ftruncate(fd, 0);
if (rc < 0) {
pr_log(LOG_NOTICE, "IMAGE: couldn't truncate image %s for writing",
dump_name);
return;
}
rc = write(fd, ctx->code_addr, ctx->code_size);
close(fd);
if (rc != ctx->code_size)
pr_log(LOG_NOTICE, "IMAGE: write to %s failed: %m", dump_name);
else
pr_debug("IMAGE: dumped HBRT binary to %s", dump_name);
}
static int open_and_read(const char *path, void **bufp, int *lenp)
{
struct stat statbuf;
int fd, rc, bytes;
void *buf;
fd = open(path, O_RDONLY);
if (fd < 0)
return -1;
rc = fstat(fd, &statbuf);
if (rc) {
close(fd);
return -1;
}
buf = malloc(statbuf.st_size);
if (!buf) {
close(fd);
return -1;
}
for (rc = bytes = 0; bytes < statbuf.st_size; bytes += rc) {
rc = read(fd, buf + bytes, statbuf.st_size - bytes);
if (rc < 0) {
if (errno == EINTR)
continue;
break;
} else if (rc == 0)
break;
}
if (bytes == statbuf.st_size)
rc = 0;
if (rc == 0) {
if (lenp)
*lenp = bytes;
if (bufp)
*bufp = buf;
} else {
free(buf);
}
close(fd);
return rc == 0 ? 0 : -1;
}
static int prd_init_one_range(struct opal_prd_ctx *ctx, const char *path,
struct dirent *dirent)
{
char *label_path, *reg_path, *instance_path;
struct prd_range *range;
int label_len, len, rc;
__be64 *reg;
char *label;
void *buf;
rc = asprintf(&label_path, "%s/%s/ibm,prd-label", path, dirent->d_name);
if (rc < 0) {
pr_log(LOG_ERR, "FW: error creating 'ibm,prd-label' path "
"node: %m");
return -1;
}
rc = asprintf(&instance_path, "%s/%s/ibm,prd-instance",
path, dirent->d_name);
if (rc < 0) {
pr_log(LOG_ERR, "FW: error creating 'ibm,prd-instance' path "
"node: %m");
return -1;
}
rc = asprintf(®_path, "%s/%s/reg", path, dirent->d_name);
if (rc < 0) {
pr_log(LOG_ERR, "FW: error creating 'reg' path "
" node: %m");
return -1;
}
reg = NULL;
label = NULL;
rc = -1;
rc = open_and_read(label_path, &buf, &label_len);
if (rc)
goto out_free;
label = buf;
if (label[label_len-1] != '\0')
pr_log(LOG_INFO, "FW: node %s has invalid ibm,prd-label - "
"not nul-terminated",
dirent->d_name);
rc = open_and_read(reg_path, &buf, &len);
if (rc)
goto out_free;
reg = buf;
if (len != 2 * sizeof(*reg)) {
pr_log(LOG_ERR, "FW: node %s has invalid 'reg' size: %d",
dirent->d_name, len);
goto out_free;
}
ctx->ranges = realloc(ctx->ranges, ++ctx->n_ranges * sizeof(*range));
range = &ctx->ranges[ctx->n_ranges - 1];
range->name = strndup(label, label_len);
range->physaddr = be64toh(reg[0]);
range->size = be64toh(reg[1]);
range->buf = NULL;
range->multiple = false;
range->instance = 0;
/* optional instance */
rc = open_and_read(instance_path, &buf, &len);
if (!rc && len == sizeof(uint32_t)) {
range->multiple = true;
range->instance = be32toh(*(uint32_t *)buf);
ctx->fw_range_instances = true;
}
rc = 0;
out_free:
free(reg);
free(label);
free(instance_path);
free(reg_path);
free(label_path);
return rc;
}
static int compare_ranges(const void *ap, const void *bp)
{
const struct prd_range *a = ap, *b = bp;
int rc;
rc = strcmp(a->name, b->name);
if (rc)
return rc;
if (a->physaddr < b->physaddr)
return -1;
else if (a->physaddr > b->physaddr)
return 1;
return 0;
}
static void assign_range_instances(struct opal_prd_ctx *ctx)
{
int i;
if (!ctx->n_ranges)
return;
ctx->ranges[0].multiple = false;
ctx->ranges[0].instance = 0;
for (i = 1; i < ctx->n_ranges; i++) {
struct prd_range *cur, *prev;
cur = &ctx->ranges[i];
prev = &ctx->ranges[i-1];
if (!strcmp(cur->name, prev->name)) {
prev->multiple = true;
cur->multiple = true;
cur->instance = prev->instance + 1;
} else {
cur->multiple = false;
cur->instance = 0;
}
}
}
static void print_ranges(struct opal_prd_ctx *ctx)
{
int i;
if (ctx->n_ranges == 0)
pr_log(LOG_INFO, "FW: No PRD ranges");
pr_log(LOG_DEBUG, "FW: %d PRD ranges, instances assigned by %s",
ctx->n_ranges,
ctx->fw_range_instances ? "firmware" : "userspace");
for (i = 0; i < ctx->n_ranges; i++) {
struct prd_range *range = &ctx->ranges[i];
char instance_str[20];
if (range->multiple)
snprintf(instance_str, sizeof(instance_str),
" [%d]", range->instance);
else
instance_str[0] = '\0';
pr_log(LOG_DEBUG, "FW: %016lx-%016lx %s%s", range->physaddr,
range->physaddr + range->size - 1,
range->name,
instance_str);
}
}
static int chip_init(void)
{
struct dirent *dirent;
char *path;
DIR *dir;
__be32 *chipid;
void *buf;
int rc, len, i;
dir = opendir(devicetree_base);
if (!dir) {
pr_log(LOG_ERR, "FW: Can't open %s", devicetree_base);
return -1;
}
for (;;) {
dirent = readdir(dir);
if (!dirent)
break;
if (strncmp("xscom", dirent->d_name, 5))
continue;
rc = asprintf(&path, "%s/%s/ibm,chip-id", devicetree_base,
dirent->d_name);
if (rc < 0) {
pr_log(LOG_ERR, "FW: Failed to create chip-id path");
return -1;
}
rc = open_and_read(path, &buf, &len);
if (rc) {
pr_log(LOG_ERR, "FW; Failed to read chipid");
return -1;
}
chipid = buf;
chips[nr_chips++] = be32toh(*chipid);
}
pr_log(LOG_DEBUG, "FW: Chip init");
for (i = 0; i < nr_chips; i++)
pr_log(LOG_DEBUG, "FW: Chip 0x%lx", chips[i]);
return 0;
}
static int prd_init_ranges(struct opal_prd_ctx *ctx)
{
struct dirent *dirent;
char *path;
DIR *dir;
int rc;
rc = asprintf(&path, "%s/reserved-memory", devicetree_base);
if (rc < 0) {
pr_log(LOG_ERR, "FW: error creating 'reserved-memory' path "
"node: %m");
return -1;
}
rc = -1;
dir = opendir(path);
if (!dir) {
pr_log(LOG_ERR, "FW: can't open reserved-memory device-tree "
"node: %m");
goto out_free;
}
for (;;) {
dirent = readdir(dir);
if (!dirent)
break;
prd_init_one_range(ctx, path, dirent);
}
rc = 0;
/* sort ranges and assign instance numbers for duplicates (if the
* firmware doesn't number instances for us) */
qsort(ctx->ranges, ctx->n_ranges, sizeof(struct prd_range),
compare_ranges);
if (!ctx->fw_range_instances)
assign_range_instances(ctx);
print_ranges(ctx);
out_free:
free(path);
closedir(dir);
return rc;
}
bool find_string(const char *buffer, size_t len, const char *s)
{
const char *c, *end;
if (!buffer)
return false;
c = buffer;
end = c + len;
while (c < end) {
if (!strcasecmp(s, c))
return true;
c += strlen(c) + 1;
}
return false;
}
static int is_prd_supported(void)
{
char *path;
int rc;
int len;
char *buf;
rc = asprintf(&path, "%s/ibm,opal/diagnostics/compatible",
devicetree_base);
if (rc < 0) {
pr_log(LOG_ERR, "FW: error creating 'compatible' node path: %m");
return -1;
}
rc = open_and_read(path, (void *) &buf, &len);
if (rc)
goto out_free;
if (buf[len - 1] != '\0')
pr_log(LOG_INFO, "FW: node %s is not nul-terminated", path);
rc = find_string(buf, len, "ibm,opal-prd") ? 0 : -1;
free(buf);
out_free:
free(path);
return rc;
}
static int prd_init(struct opal_prd_ctx *ctx)
{
int rc;
ctx->page_size = sysconf(_SC_PAGE_SIZE);
/* set up the device, and do our get_info ioctl */
ctx->fd = open(opal_prd_devnode, O_RDWR);
if (ctx->fd < 0) {
pr_log(LOG_ERR, "FW: Can't open PRD device %s: %m",
opal_prd_devnode);
return -1;
}
rc = ioctl(ctx->fd, OPAL_PRD_GET_INFO, &ctx->info);
if (rc) {
pr_log(LOG_ERR, "FW: Can't query PRD information: %m");
return -1;
}
rc = prd_init_ranges(ctx);
if (rc) {
pr_log(LOG_ERR, "FW: can't parse PRD memory information");
return -1;
}
rc = chip_init();
if (rc)
pr_log(LOG_ERR, "FW: Failed to initialize chip IDs");
return 0;
}
static int handle_msg_attn(struct opal_prd_ctx *ctx, struct opal_prd_msg *msg)
{
uint64_t proc, ipoll_mask, ipoll_status;
int rc;
proc = be64toh(msg->attn.proc);
ipoll_status = be64toh(msg->attn.ipoll_status);
ipoll_mask = be64toh(msg->attn.ipoll_mask);
if (!hservice_runtime->handle_attns) {
pr_log_nocall("handle_attns");
return -1;
}
rc = call_handle_attns(proc, ipoll_status, ipoll_mask);
if (rc) {
pr_log(LOG_ERR, "HBRT: handle_attns(%lx,%lx,%lx) failed, rc %d",
proc, ipoll_status, ipoll_mask, rc);
return -1;
}
/* send the response */
msg->hdr.type = OPAL_PRD_MSG_TYPE_ATTN_ACK;
msg->hdr.size = htobe16(sizeof(*msg));
msg->attn_ack.proc = htobe64(proc);
msg->attn_ack.ipoll_ack = htobe64(ipoll_status);
rc = write(ctx->fd, msg, sizeof(*msg));
if (rc != sizeof(*msg)) {
pr_log(LOG_WARNING, "FW: Failed to send ATTN_ACK message: %m");
return -1;
}
return 0;
}
static int handle_msg_occ_error(struct opal_prd_ctx *ctx,
struct opal_prd_msg *msg)
{
uint32_t proc;
proc = be64toh(msg->occ_error.chip);
pr_debug("FW: firmware signaled OCC error for proc 0x%x", proc);
if (!hservice_runtime->process_occ_error) {
pr_log_nocall("process_occ_error");
return -1;
}
call_process_occ_error(proc);
return 0;
}
static int pm_complex_load_start(void)
{
struct prd_range *range;
u64 homer, occ_common;
int rc = -1, i;
if (!hservice_runtime->load_pm_complex) {
pr_log_nocall("load_pm_complex");
return rc;
}
if (!hservice_runtime->start_pm_complex) {
pr_log_nocall("start_pm_complex");
return rc;
}
range = find_range("ibm,occ-common-area", 0);
if (!range) {
pr_log(LOG_ERR, "PM: ibm,occ-common-area not found");
return rc;
}
occ_common = range->physaddr;
for (i = 0; i < nr_chips; i++) {
range = find_range("ibm,homer-image", chips[i]);
if (!range) {
pr_log(LOG_ERR, "PM: ibm,homer-image not found 0x%lx",
chips[i]);
return -1;
}
homer = range->physaddr;
pr_debug("PM: calling load_pm_complex(0x%lx, 0x%lx, 0x%lx, LOAD)",
chips[i], homer, occ_common);
rc = call_load_pm_complex(chips[i], homer, occ_common, 0);
if (rc) {
pr_log(LOG_ERR, "PM: Failed load_pm_complex(0x%lx) %m",
chips[i]);
return rc;
}
}
for (i = 0; i < nr_chips; i++) {
pr_debug("PM: calling start_pm_complex(0x%lx)", chips[i]);
rc = call_start_pm_complex(chips[i]);
if (rc) {
pr_log(LOG_ERR, "PM: Failed start_pm_complex(0x%lx): %m",
chips[i]);
return rc;
}
}
return rc;
}
static int pm_complex_reset(uint64_t chip)
{
int rc;
/*
* FSP system -> reset_pm_complex
* BMC system -> process_occ_reset
*/
if (is_fsp_system()) {
int i;
if (!hservice_runtime->reset_pm_complex) {
pr_log_nocall("reset_pm_complex");
return -1;
}
for (i = 0; i < nr_chips; i++) {
pr_debug("PM: calling pm_complex_reset(%ld)", chips[i]);
rc = call_reset_pm_complex(chip);
if (rc) {
pr_log(LOG_ERR, "PM: Failed pm_complex_reset(%ld): %m",
chips[i]);
return rc;
}
}
rc = pm_complex_load_start();
} else {
if (!hservice_runtime->process_occ_reset) {
pr_log_nocall("process_occ_reset");
return -1;
}
pr_debug("PM: calling process_occ_reset(%ld)", chip);
call_process_occ_reset(chip);
rc = 0;
}
return rc;
}
static int handle_msg_occ_reset(struct opal_prd_ctx *ctx,
struct opal_prd_msg *msg)
{
uint32_t proc;
int rc;
proc = be64toh(msg->occ_reset.chip);
pr_debug("FW: firmware requested OCC reset for proc 0x%x", proc);
rc = pm_complex_reset(proc);
return rc;
}
static int handle_msg_firmware_notify(struct opal_prd_ctx *ctx,
struct opal_prd_msg *msg)
{
uint64_t len;
void *buf;
len = be64toh(msg->fw_notify.len);
buf = msg->fw_notify.data;
pr_debug("FW: firmware notification, %ld bytes", len);
if (!hservice_runtime->firmware_notify) {
pr_log_nocall("firmware_notify");
return -1;
}
call_firmware_notify(len, buf);
return 0;
}
static int handle_msg_sbe_passthrough(struct opal_prd_ctx *ctx,
struct opal_prd_msg *msg)
{
uint32_t proc;
int rc;
proc = be64toh(msg->sbe_passthrough.chip);
pr_debug("FW: firmware sent SBE pass through command for proc 0x%x\n",
proc);
if (!hservice_runtime->sbe_message_passing) {
pr_log_nocall("sbe_message_passing");
return -1;
}
rc = call_sbe_message_passing(proc);
return rc;
}
static int handle_msg_fsp_occ_reset(struct opal_prd_msg *msg)
{
struct opal_prd_msg omsg;
int rc = -1, i;
pr_debug("FW: FSP requested OCC reset");
if (!hservice_runtime->reset_pm_complex) {
pr_log_nocall("reset_pm_complex");
return rc;
}
for (i = 0; i < nr_chips; i++) {
pr_debug("PM: calling pm_complex_reset(0x%lx)", chips[i]);
rc = call_reset_pm_complex(chips[i]);
if (rc) {
pr_log(LOG_ERR, "PM: Failed pm_complex_reset(0x%lx) %m",
chips[i]);
break;
}
}
omsg.hdr.type = OPAL_PRD_MSG_TYPE_FSP_OCC_RESET_STATUS;
omsg.hdr.size = htobe16(sizeof(omsg));
omsg.fsp_occ_reset_status.chip = msg->occ_reset.chip;
omsg.fsp_occ_reset_status.status = htobe64(rc);
if (write(ctx->fd, &omsg, sizeof(omsg)) != sizeof(omsg)) {
pr_log(LOG_ERR, "FW: Failed to send FSP_OCC_RESET_STATUS msg: %m");
return -1;
}
return rc;
}
static int handle_msg_fsp_occ_load_start(struct opal_prd_msg *msg)
{
struct opal_prd_msg omsg;
int rc;
pr_debug("FW: FSP requested OCC load/start");
rc = pm_complex_load_start();
omsg.hdr.type = OPAL_PRD_MSG_TYPE_FSP_OCC_LOAD_START_STATUS;
omsg.hdr.size = htobe16(sizeof(omsg));
omsg.fsp_occ_reset_status.chip = msg->occ_reset.chip;
omsg.fsp_occ_reset_status.status = htobe64(rc);
if (write(ctx->fd, &omsg, sizeof(omsg)) != sizeof(omsg)) {
pr_log(LOG_ERR, "FW: Failed to send FSP_OCC_LOAD_START_STATUS msg: %m");
return -1;
}
return rc;
}
static int handle_prd_msg(struct opal_prd_ctx *ctx, struct opal_prd_msg *msg)
{
int rc = -1;
switch (msg->hdr.type) {
case OPAL_PRD_MSG_TYPE_ATTN:
rc = handle_msg_attn(ctx, msg);
break;
case OPAL_PRD_MSG_TYPE_OCC_RESET:
rc = handle_msg_occ_reset(ctx, msg);
break;
case OPAL_PRD_MSG_TYPE_OCC_ERROR:
rc = handle_msg_occ_error(ctx, msg);
break;
case OPAL_PRD_MSG_TYPE_FIRMWARE_NOTIFY:
rc = handle_msg_firmware_notify(ctx, msg);
break;
case OPAL_PRD_MSG_TYPE_SBE_PASSTHROUGH:
rc = handle_msg_sbe_passthrough(ctx, msg);
break;
case OPAL_PRD_MSG_TYPE_FSP_OCC_RESET:
rc = handle_msg_fsp_occ_reset(msg);
break;
case OPAL_PRD_MSG_TYPE_FSP_OCC_LOAD_START:
rc = handle_msg_fsp_occ_load_start(msg);
break;
default:
pr_log(LOG_WARNING, "Invalid incoming message type 0x%x",
msg->hdr.type);
}
return rc;
}
#define list_for_each_pop(h, i, type, member) \
for (i = list_pop((h), type, member); \
i; \
i = list_pop((h), type, member))
static int process_msgq(struct opal_prd_ctx *ctx)
{
struct prd_msgq_item *item;
list_for_each_pop(&ctx->msgq, item, struct prd_msgq_item, list) {
handle_prd_msg(ctx, &item->msg);
free(item);
}
return 0;
}
static int read_prd_msg(struct opal_prd_ctx *ctx)
{
struct opal_prd_msg *msg;
int size;
int rc;
msg = ctx->msg;
rc = read(ctx->fd, msg, ctx->msg_alloc_len);
if (rc < 0 && errno == EAGAIN)
return -1;
/* we need at least enough for the message header... */
if (rc < 0) {
pr_log(LOG_WARNING, "FW: error reading from firmware: %m");
return -1;
}
if (rc < sizeof(msg->hdr)) {
pr_log(LOG_WARNING, "FW: short message read from firmware");
return -1;
}
/* ... and for the reported message size to be sane */
size = htobe16(msg->hdr.size);
if (size < sizeof(msg->hdr)) {
pr_log(LOG_ERR, "FW: Mismatched message size "
"between opal-prd and firmware "
"(%d from FW, %zd expected)",
size, sizeof(msg->hdr));
return -1;
}
/* expand our message buffer if necessary... */
if (size > ctx->msg_alloc_len) {
msg = realloc(ctx->msg, size);
if (!msg) {
pr_log(LOG_ERR,
"FW: Can't expand PRD message buffer: %m");
return -1;
}
ctx->msg = msg;
ctx->msg_alloc_len = size;
}
/* ... and complete the read */
if (size > rc) {
size_t pos;
for (pos = rc; pos < size;) {
rc = read(ctx->fd, msg + pos, size - pos);
if (rc < 0 && errno == EAGAIN)
continue;
if (rc <= 0) {
pr_log(LOG_WARNING,
"FW: error reading from firmware: %m");
return -1;
}
pos += rc;
}
}
return 0;
}
static void handle_prd_control_occ_error(struct control_msg *send_msg,
struct control_msg *recv_msg)
{
uint64_t chip;
if (!hservice_runtime->process_occ_error) {
pr_log_nocall("process_occ_error");
return;
}
chip = recv_msg->occ_error.chip;
pr_debug("CTRL: calling process_occ_error(%lu)", chip);
call_process_occ_error(chip);
send_msg->data_len = 0;
send_msg->response = 0;
}
static void handle_prd_control_occ_reset(struct control_msg *send_msg,
struct control_msg *msg)
{
struct opal_prd_msg omsg;
uint64_t chip;
int rc;
/* notify OPAL of the impending reset */
memset(&omsg, 0, sizeof(omsg));
omsg.hdr.type = OPAL_PRD_MSG_TYPE_OCC_RESET_NOTIFY;
omsg.hdr.size = htobe16(sizeof(omsg));
rc = write(ctx->fd, &omsg, sizeof(omsg));
if (rc != sizeof(omsg))
pr_log(LOG_WARNING, "FW: Failed to send OCC_RESET message: %m");
chip = msg->occ_reset.chip;
/* do reset */
pr_debug("CTRL: Calling OCC reset on chip %ld", chip);
pm_complex_reset(chip);
send_msg->data_len = 0;
send_msg->response = 0;
}
static void handle_prd_control_occ_actuation(struct control_msg *msg,
bool enable)
{
if (!hservice_runtime->enable_occ_actuation) {
pr_log_nocall("enable_occ_actuation");
return;
}
pr_debug("CTRL: calling enable_occ_actuation(%s)",
enable ? "true" : "false");
msg->data_len = 0;
msg->response = call_enable_occ_actuation(enable);
}
static void handle_prd_control_attr_override(struct control_msg *send_msg,
struct control_msg *recv_msg)
{
if (!hservice_runtime->apply_attr_override) {
pr_log_nocall("apply_attr_override");
return;
}
pr_debug("CTRL: calling apply_attr_override");
send_msg->response = call_apply_attr_override(
recv_msg->data, recv_msg->data_len);
send_msg->data_len = 0;
}
static void handle_prd_control_htmgt_passthru(struct control_msg *send_msg,
struct control_msg *recv_msg)
{
uint16_t rsp_len;
if (!hservice_runtime->mfg_htmgt_pass_thru) {
pr_log_nocall("mfg_htmgt_pass_thru");
return;
}
pr_debug("CTRL: calling mfg_htmgt_pass_thru");
send_msg->response = call_mfg_htmgt_pass_thru(recv_msg->data_len,
recv_msg->data, &rsp_len,
send_msg->data);
send_msg->data_len = be16toh(rsp_len);
if (send_msg->data_len > MAX_CONTROL_MSG_BUF) {
pr_log(LOG_ERR, "CTRL: response buffer overrun, data len: %d",
send_msg->data_len);
send_msg->data_len = MAX_CONTROL_MSG_BUF;
}
}
static void handle_prd_control_run_cmd(struct control_msg *send_msg,
struct control_msg *recv_msg)
{
char *runcmd_output, *s;
const char **argv;
int i, argc;
size_t size;
if (!hservice_runtime->run_command) {
pr_log_nocall("run_command");
return;
}
argc = recv_msg->run_cmd.argc;
pr_debug("CTRL: run_command, argc:%d\n", argc);
argv = malloc(argc * sizeof(*argv));
if (!argv) {
pr_log(LOG_ERR, "CTRL: argv buffer malloc failed: %m");
return;
}
s = (char *)recv_msg->data;
size = 0;
for (i = 0; i < argc; i++) {
argv[i] = (char *)htobe64((uint64_t)&s[size]);
size += (strlen(&s[size]) + 1);
}
/* Call HBRT */
send_msg->response = call_run_command(argc, argv, &runcmd_output);
runcmd_output = (char *)be64toh((uint64_t)runcmd_output);
free(argv);
s = (char *)send_msg->data;
if (runcmd_output) {
size = strlen(runcmd_output);
if (size >= MAX_CONTROL_MSG_BUF) {
pr_log(LOG_WARNING, "CTRL: output message truncated");
runcmd_output[MAX_CONTROL_MSG_BUF] = '\0';
size = MAX_CONTROL_MSG_BUF;
}
strcpy(s, runcmd_output);
send_msg->data_len = size + 1;
free(runcmd_output);
} else {
strcpy(s, "Null");
send_msg->data_len = strlen("Null") + 1;
}
}
static void handle_prd_control(struct opal_prd_ctx *ctx, int fd)
{
struct control_msg msg, *recv_msg, *send_msg;
bool enabled = false;
int rc, size;
/* Default reply, in the error path */
send_msg = &msg;
/* Peek into the socket to ascertain the size of the available data */
rc = recv(fd, &msg, sizeof(msg), MSG_PEEK);
if (rc != sizeof(msg)) {
pr_log(LOG_WARNING, "CTRL: failed to receive control "
"message: %m");
msg.response = -1;
msg.data_len = 0;
goto out_send;
}
size = sizeof(*recv_msg) + msg.data_len;
/* Default reply, in the error path */
msg.data_len = 0;
msg.response = -1;
recv_msg = malloc(size);
if (!recv_msg) {
pr_log(LOG_ERR, "CTRL: message buffer malloc failed: %m");
goto out_send;
}
rc = recv(fd, recv_msg, size, MSG_TRUNC);
if (rc != size) {
pr_log(LOG_WARNING, "CTRL: failed to receive control "
"message: %m");
goto out_free_recv;
}
send_msg = malloc(sizeof(*send_msg) + MAX_CONTROL_MSG_BUF);
if (!send_msg) {
pr_log(LOG_ERR, "CTRL: message buffer malloc failed: %m");
send_msg = &msg;
goto out_free_recv;
}
send_msg->type = recv_msg->type;
send_msg->response = -1;
switch (recv_msg->type) {
case CONTROL_MSG_ENABLE_OCCS:
enabled = true;
/* fall through */
case CONTROL_MSG_DISABLE_OCCS:
handle_prd_control_occ_actuation(send_msg, enabled);
break;
case CONTROL_MSG_TEMP_OCC_RESET:
handle_prd_control_occ_reset(send_msg, recv_msg);
break;
case CONTROL_MSG_TEMP_OCC_ERROR:
handle_prd_control_occ_error(send_msg, recv_msg);
break;
case CONTROL_MSG_ATTR_OVERRIDE:
handle_prd_control_attr_override(send_msg, recv_msg);
break;
case CONTROL_MSG_HTMGT_PASSTHRU:
handle_prd_control_htmgt_passthru(send_msg, recv_msg);
break;
case CONTROL_MSG_RUN_CMD:
handle_prd_control_run_cmd(send_msg, recv_msg);
break;
default:
pr_log(LOG_WARNING, "CTRL: Unknown control message action %d",
recv_msg->type);
send_msg->data_len = 0;
break;
}
out_free_recv:
free(recv_msg);
out_send:
size = sizeof(*send_msg) + send_msg->data_len;
rc = send(fd, send_msg, size, MSG_DONTWAIT | MSG_NOSIGNAL);
if (rc && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EPIPE))
pr_debug("CTRL: control send() returned %d, ignoring failure",
rc);
else if (rc != size)
pr_log(LOG_NOTICE, "CTRL: Failed to send control response: %m");
if (send_msg != &msg)
free(send_msg);
}
static int run_attn_loop(struct opal_prd_ctx *ctx)
{
struct pollfd pollfds[2];
struct opal_prd_msg msg;
int rc, fd;
if (hservice_runtime->enable_attns) {
pr_debug("HBRT: calling enable_attns");
rc = call_enable_attns();
if (rc) {
pr_log(LOG_ERR, "HBRT: enable_attns() failed, "
"aborting");
return -1;
}
}
if (hservice_runtime->get_ipoll_events) {
pr_debug("HBRT: calling get_ipoll_events");
opal_prd_ipoll = call_get_ipoll_events();
}
pr_debug("HBRT: enabling IPOLL events 0x%016lx", opal_prd_ipoll);
/* send init message, to unmask interrupts */
msg.hdr.type = OPAL_PRD_MSG_TYPE_INIT;
msg.hdr.size = htobe16(sizeof(msg));
msg.init.version = htobe64(opal_prd_version);
msg.init.ipoll = htobe64(opal_prd_ipoll);
pr_debug("FW: writing init message");
rc = write(ctx->fd, &msg, sizeof(msg));
if (rc != sizeof(msg)) {
pr_log(LOG_ERR, "FW: Init message failed: %m. Aborting.");
return -1;
}
pollfds[0].fd = ctx->fd;
pollfds[0].events = POLLIN | POLLERR;
pollfds[1].fd = ctx->socket;
pollfds[1].events = POLLIN | POLLERR;
for (;;) {
/* run through any pending messages */
process_msgq(ctx);
rc = poll(pollfds, 2, -1);
if (rc < 0) {
pr_log(LOG_ERR, "FW: event poll failed: %m");
exit(EXIT_FAILURE);
}
if (!rc)
continue;
if (pollfds[0].revents & POLLIN) {
rc = read_prd_msg(ctx);
if (!rc)
handle_prd_msg(ctx, ctx->msg);
}
if (pollfds[1].revents & POLLIN) {
fd = accept(ctx->socket, NULL, NULL);
if (fd < 0) {
pr_log(LOG_NOTICE, "CTRL: accept failed: %m");
continue;
}
handle_prd_control(ctx, fd);
close(fd);
}
}
return 0;
}
static int init_control_socket(struct opal_prd_ctx *ctx)
{
struct sockaddr_un addr;
int fd, rc;
unlink(opal_prd_socket);
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, opal_prd_socket);
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (fd < 0) {
pr_log(LOG_WARNING, "CTRL: Can't open control socket %s: %m",
opal_prd_socket);
return -1;
}
rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
if (rc) {
pr_log(LOG_WARNING, "CTRL: Can't bind control socket %s: %m",
opal_prd_socket);
close(fd);
return -1;
}
rc = listen(fd, 0);
if (rc) {
pr_log(LOG_WARNING, "CTRL: Can't listen on "
"control socket %s: %m", opal_prd_socket);
close(fd);
return -1;
}
pr_log(LOG_INFO, "CTRL: Listening on control socket %s",
opal_prd_socket);
ctx->socket = fd;
return 0;
}
static struct sigaction sigchild_action = {
.sa_flags = SA_NOCLDWAIT | SA_RESTART,
.sa_handler = SIG_DFL,
};
static int run_prd_daemon(struct opal_prd_ctx *ctx)
{
char *opal_msg_path;
void *buf;
int rc, len;
/* log to syslog */
pr_log_daemon_init();
pr_debug("CTRL: Starting PRD daemon\n");
ctx->fd = -1;
ctx->socket = -1;
/*
* Set up our message buffer. Use opal-msg-size device tree
* property to get message buffer size.
*/
rc = asprintf(&opal_msg_path,
"%s/ibm,opal/opal-msg-size", devicetree_base);
if (rc > 0) {
rc = open_and_read(opal_msg_path, &buf, &len);
if (rc == 0) {
ctx->msg_alloc_len = be32toh(*(__be32 *)buf);
free(buf);
}
free(opal_msg_path);
}
if (ctx->msg_alloc_len == 0)
ctx->msg_alloc_len = sizeof(*ctx->msg);
ctx->msg = malloc(ctx->msg_alloc_len);
if (!ctx->msg) {
pr_log(LOG_ERR, "FW: Can't allocate PRD message buffer: %m");
return -1;
}
memset(ctx->msg, 0, ctx->msg_alloc_len);
list_head_init(&ctx->msgq);
i2c_init();
#ifdef DEBUG_I2C
{
uint8_t foo[128];
int i;
rc = i2c_read(0, 1, 2, 0x50, 2, 0x10, 128, foo);
pr_debug("I2C: read rc: %d", rc);
for (i = 0; i < sizeof(foo); i += 8) {
pr_debug("I2C: %02x %02x %02x %02x %02x %02x %02x %02x",
foo[i + 0], foo[i + 1], foo[i + 2], foo[i + 3],
foo[i + 4], foo[i + 5], foo[i + 6], foo[i + 7]);
}
}
#endif
rc = init_control_socket(ctx);
if (rc) {
pr_log(LOG_WARNING, "CTRL: Error initialising PRD control: %m");
goto out_close;
}
rc = prd_init(ctx);
if (rc) {
pr_log(LOG_ERR, "FW: Error initialising PRD channel");
goto out_close;
}
if (ctx->hbrt_file_name) {
rc = map_hbrt_file(ctx, ctx->hbrt_file_name);
if (rc) {
pr_log(LOG_ERR, "IMAGE: Can't access hbrt file %s",
ctx->hbrt_file_name);
goto out_close;
}
} else {
rc = map_hbrt_physmem(ctx, hbrt_code_region_name);
if (rc) {
/* Fallback to old style ibm,prd-label */
rc = map_hbrt_physmem(ctx, hbrt_code_region_name_ibm);
if (rc) {
pr_log(LOG_ERR, "IMAGE: Can't access hbrt "
"physical memory");
goto out_close;
}
}
dump_hbrt_map(ctx);
}
pr_debug("IMAGE: hbrt map at %p, size 0x%zx",
ctx->code_addr, ctx->code_size);
fixup_hinterface_table();
if (!is_fsp_system()) {
pnor_load_module(ctx);
rc = pnor_init(&ctx->pnor);
if (rc) {
pr_log(LOG_ERR, "PNOR: Failed to open pnor: %m");
goto out_close;
}
} else {
/* Disable PNOR function pointers */
hinterface.pnor_read = NULL;
hinterface.pnor_write = NULL;
}
ipmi_init(ctx);
pr_debug("HBRT: calling hservices_init");
rc = hservices_init(ctx, ctx->code_addr);
if (rc) {
pr_log(LOG_ERR, "HBRT: Can't initialise HBRT");
goto out_close;
}
pr_debug("HBRT: hservices_init done");
/* Test a scom */
if (ctx->debug) {
uint64_t val;
pr_debug("SCOM: trying scom read");
fflush(stdout);
hservice_scom_read(0x00, 0xf000f, &val);
pr_debug("SCOM: f00f: %lx", be64toh(val));
}
/*
* Setup the SIGCHLD handler to automatically reap the worker threads
* we use for memory offlining. We can't do this earlier since the
* modprobe helper spawns workers and wants to check their exit status
* with waitpid(). Auto-reaping breaks that so enable it just before
* entering the attn loop.
*
* We also setup system call restarting on SIGCHLD since opal-prd
* doesn't make any real attempt to handle blocking functions exiting
* due to EINTR.
*/
if (sigaction(SIGCHLD, &sigchild_action, NULL)) {
pr_log(LOG_ERR, "CTRL: Failed to register signal handler %m\n");
return -1;
}
run_attn_loop(ctx);
rc = 0;
out_close:
pr_debug("CTRL: stopping PRD daemon\n");
pnor_close(&ctx->pnor);
if (ctx->fd != -1)
close(ctx->fd);
if (ctx->socket != -1)
close(ctx->socket);
if (ctx->msg)
free(ctx->msg);
return rc;
}
static int send_prd_control(struct control_msg *send_msg,
struct control_msg **recv_msg)
{
struct sockaddr_un addr;
struct control_msg *msg;
int sd, rc, size;
sd = socket(AF_UNIX, SOCK_STREAM, 0);
if (!sd) {
pr_log(LOG_ERR, "CTRL: Failed to create control socket: %m");
return -1;
}
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, opal_prd_socket);
rc = connect(sd, (struct sockaddr *)&addr, sizeof(addr));
if (rc) {
pr_log(LOG_ERR, "CTRL: Failed to connect to prd daemon: %m");
goto out_close;
}
size = sizeof(*send_msg) + send_msg->data_len;
rc = send(sd, send_msg, size, 0);
if (rc != size) {
pr_log(LOG_ERR, "CTRL: Failed to send control message: %m");
rc = -1;
goto out_close;
}
size = sizeof(*msg) + MAX_CONTROL_MSG_BUF;
msg = malloc(size);
if (!msg) {
pr_log(LOG_ERR, "CTRL: msg buffer malloc failed: %m");
rc = -1;
goto out_close;
}
*recv_msg = msg;
/* wait for our reply */
rc = recv(sd, msg, size, 0);
if (rc < 0) {
pr_log(LOG_ERR, "CTRL: Failed to receive control message: %m");
goto out_close;
} else if (rc != (sizeof(*msg) + msg->data_len)) {
pr_log(LOG_WARNING, "CTRL: Short read from control socket");
rc = -1;
goto out_close;
}
rc = msg->response;
out_close:
close(sd);
return rc;
}
static int send_occ_control(struct opal_prd_ctx *ctx, int argc, char *argv[])
{
struct control_msg send_msg, *recv_msg = NULL;
unsigned long chip = 0;
const char *op;
int rc;
assert(argc >= 1);
op = argv[0];
/* some commands accept a 'chip' argument, so parse it here */
if (argc > 1) {
char *arg, *end;
arg = argv[1];
chip = strtoul(arg, &end, 0);
if (end == arg) {
pr_log(LOG_ERR, "CTRL: invalid argument %s", arg);
return -1;
}
}
memset(&send_msg, 0, sizeof(send_msg));
if (!strcmp(op, "enable"))
send_msg.type = CONTROL_MSG_ENABLE_OCCS;
else if (!strcmp(op, "disable"))
send_msg.type = CONTROL_MSG_DISABLE_OCCS;
else if (!strcmp(op, "reset")) {
send_msg.type = CONTROL_MSG_TEMP_OCC_RESET;
send_msg.occ_reset.chip = (uint64_t)chip;
} else if (!strcmp(op, "process-error")) {
send_msg.type = CONTROL_MSG_TEMP_OCC_ERROR;
send_msg.occ_error.chip = (uint64_t)chip;
} else {
pr_log(LOG_ERR, "CTRL: Invalid OCC action '%s'", op);
return -1;
}
rc = send_prd_control(&send_msg, &recv_msg);
if (recv_msg) {
if (recv_msg->response || ctx->debug)
pr_debug("CTRL: OCC action %s returned status %d", op,
recv_msg->response);
free(recv_msg);
}
return rc;
}
static int send_attr_override(struct opal_prd_ctx *ctx, uint32_t argc,
char *argv[])
{
struct control_msg *send_msg, *recv_msg = NULL;
struct stat statbuf;
size_t sz;
FILE *fd;
int rc;
rc = stat(argv[0], &statbuf);
if (rc) {
pr_log(LOG_ERR, "CTRL: stat() failed on the file: %m");
return -1;
}
send_msg = malloc(sizeof(*send_msg) + statbuf.st_size);
if (!send_msg) {
pr_log(LOG_ERR, "CTRL: msg buffer malloc failed: %m");
return -1;
}
send_msg->type = CONTROL_MSG_ATTR_OVERRIDE;
send_msg->data_len = statbuf.st_size;
fd = fopen(argv[0], "r");
if (!fd) {
pr_log(LOG_NOTICE, "CTRL: can't open %s: %m", argv[0]);
rc = -1;
goto out_free;
}
sz = fread(send_msg->data, 1, send_msg->data_len, fd);
fclose(fd);
if (sz != statbuf.st_size) {
pr_log(LOG_ERR, "CTRL: short read from the file");
rc = -1;
goto out_free;
}
rc = send_prd_control(send_msg, &recv_msg);
if (recv_msg) {
if (recv_msg->response || ctx->debug)
pr_debug("CTRL: attribute override returned status %d",
recv_msg->response);
free(recv_msg);
}
out_free:
free(send_msg);
return rc;
}
static int send_htmgt_passthru(struct opal_prd_ctx *ctx, int argc, char *argv[])
{
struct control_msg *send_msg, *recv_msg = NULL;
int rc, i;
if (!ctx->expert_mode) {
pr_log(LOG_WARNING, "CTRL: need to be in expert mode");
return -1;
}
send_msg = malloc(sizeof(*send_msg) + argc);
if (!send_msg) {
pr_log(LOG_ERR, "CTRL: message buffer malloc failed: %m");
return -1;
}
send_msg->type = CONTROL_MSG_HTMGT_PASSTHRU;
send_msg->data_len = argc;
if (ctx->debug)
pr_debug("CTRL: HTMGT passthru arguments:");
for (i = 0; i < argc; i++) {
if (ctx->debug)
pr_debug("argv[%d] = %s", i, argv[i]);
sscanf(argv[i], "%hhx", &send_msg->data[i]);
}
rc = send_prd_control(send_msg, &recv_msg);
free(send_msg);
if (recv_msg) {
if (recv_msg->response || ctx->debug)
pr_debug("CTRL: HTMGT passthru returned status %d",
recv_msg->response);
if (recv_msg->response == 0 && recv_msg->data_len)
hexdump(recv_msg->data, recv_msg->data_len);
free(recv_msg);
}
return rc;
}
static int send_run_command(struct opal_prd_ctx *ctx, int argc, char *argv[])
{
struct control_msg *send_msg, *recv_msg = NULL;
uint32_t size = 0;
int rc, i;
char *s;
if (!ctx->expert_mode) {
pr_log(LOG_WARNING, "CTRL: need to be in expert mode");
return -1;
}
if (ctx->debug) {
pr_debug("CTRL: run command arguments:");
for (i=0; i < argc; i++)
pr_debug("argv[%d] = %s", i, argv[i]);
}
for (i = 0; i < argc; i++)
size += (strlen(argv[i]) + 1);
send_msg = malloc(sizeof(*send_msg) + size);
if (!send_msg) {
pr_log(LOG_ERR, "CTRL: msg buffer malloc failed: %m");
return -1;
}
/* Setup message */
send_msg->type = CONTROL_MSG_RUN_CMD;
send_msg->run_cmd.argc = argc;
send_msg->data_len = size;
s = (char *)send_msg->data;
for (i = 0; i < argc; i++) {
strcpy(s, argv[i]);
s = s + strlen(argv[i]) + 1;
}
rc = send_prd_control(send_msg, &recv_msg);
free(send_msg);
if (recv_msg) {
if (!rc)
pr_log(LOG_INFO, "Received: %s", recv_msg->data);
if (recv_msg->response || ctx->debug)
pr_debug("CTRL: run command returned status %d",
recv_msg->response);
free(recv_msg);
}
return rc;
}
static void usage(const char *progname)
{
printf("Usage:\n");
printf("\t%s [--debug] [--file <hbrt-image>] [--pnor <device>]\n",
progname);
printf("\t%s occ <enable|disable|reset [chip]>\n", progname);
printf("\t%s pm-complex reset [chip]>\n", progname);
printf("\t%s htmgt-passthru <bytes...>\n", progname);
printf("\t%s override <FILE>\n", progname);
printf("\t%s run [arg 0] [arg 1]..[arg n]\n", progname);
printf("\n");
printf("Options:\n"
"\t--debug verbose logging for debug information\n"
"\t--pnor DEVICE use PNOR MTD device\n"
"\t--file FILE use FILE for hostboot runtime code (instead of code\n"
"\t exported by firmware)\n"
"\t--stdio log to stdio, instead of syslog\n");
}
static void print_version(void)
{
extern const char version[];
printf("opal-prd %s\n", version);
}
static struct option opal_diag_options[] = {
{"file", required_argument, NULL, 'f'},
{"pnor", required_argument, NULL, 'p'},
{"debug", no_argument, NULL, 'd'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{"stdio", no_argument, NULL, 's'},
{"expert-mode", no_argument, NULL, 'e'},
{ 0 },
};
enum action {
ACTION_RUN_DAEMON,
ACTION_OCC_CONTROL,
ACTION_ATTR_OVERRIDE,
ACTION_HTMGT_PASSTHRU,
ACTION_RUN_COMMAND,
};
static int parse_action(const char *str, enum action *action)
{
int rc;
if (!strcmp(str, "occ")) {
*action = ACTION_OCC_CONTROL;
rc = 0;
if (is_fsp_system()) {
pr_log(LOG_ERR, "CTRL: occ commands are not "
"supported on this system");
rc = -1;
}
} else if (!strcmp(str, "pm-complex")) {
*action = ACTION_OCC_CONTROL;
rc = 0;
if (!is_fsp_system()) {
pr_log(LOG_ERR, "CTRL: pm-complex commands are not "
"supported on this system");
rc = -1;
}
} else if (!strcmp(str, "daemon")) {
*action = ACTION_RUN_DAEMON;
rc = 0;
} else if (!strcmp(str, "override")) {
*action = ACTION_ATTR_OVERRIDE;
rc = 0;
} else if (!strcmp(str, "htmgt-passthru")) {
*action = ACTION_HTMGT_PASSTHRU;
rc = 0;
} else if (!strcmp(str, "run")) {
*action = ACTION_RUN_COMMAND;
return 0;
} else {
pr_log(LOG_ERR, "CTRL: unknown argument '%s'", str);
rc = -1;
}
return rc;
}
int main(int argc, char *argv[])
{
struct opal_prd_ctx _ctx;
enum action action;
int rc;
check_abi();
ctx = &_ctx;
memset(ctx, 0, sizeof(*ctx));
ctx->vlog = pr_log_stdio;
ctx->use_syslog = true;
/* Parse options */
for (;;) {
int c;
c = getopt_long(argc, argv, "f:p:dhse", opal_diag_options, NULL);
if (c == -1)
break;
switch (c) {
case 'f':
ctx->hbrt_file_name = optarg;
break;
case 'd':
ctx->debug = true;
break;
case 'p':
ctx->pnor.path = strndup(optarg, PATH_MAX);
break;
case 's':
ctx->use_syslog = false;
break;
case 'h':
usage(argv[0]);
return EXIT_SUCCESS;
case 'e':
ctx->expert_mode = true;
break;
case 'v':
print_version();
return EXIT_SUCCESS;
case '?':
default:
usage(argv[0]);
return EXIT_FAILURE;
}
}
if (optind < argc) {
rc = parse_action(argv[optind], &action);
if (rc)
return EXIT_FAILURE;
optind++;
} else {
action = ACTION_RUN_DAEMON;
}
if (is_prd_supported() < 0) {
pr_log(LOG_ERR, "CTRL: PowerNV OPAL runtime diagnostic "
"is not supported on this system");
return -1;
}
switch (action) {
case ACTION_RUN_DAEMON:
rc = run_prd_daemon(ctx);
break;
case ACTION_OCC_CONTROL:
if (optind >= argc) {
pr_log(LOG_ERR, "CTRL: occ command requires "
"an argument");
return EXIT_FAILURE;
}
rc = send_occ_control(ctx, argc - optind, &argv[optind]);
break;
case ACTION_ATTR_OVERRIDE:
if (optind >= argc) {
pr_log(LOG_ERR, "CTRL: attribute override command "
"requires an argument");
return EXIT_FAILURE;
}
rc = send_attr_override(ctx, argc - optind, &argv[optind]);
break;
case ACTION_HTMGT_PASSTHRU:
if (optind >= argc) {
pr_log(LOG_ERR, "CTRL: htmgt passthru requires at least "
"one argument");
return EXIT_FAILURE;
}
rc = send_htmgt_passthru(ctx, argc - optind, &argv[optind]);
break;
case ACTION_RUN_COMMAND:
if (optind >= argc) {
pr_log(LOG_ERR, "CTRL: run command requires "
"argument(s)");
return EXIT_FAILURE;
}
rc = send_run_command(ctx, argc - optind, &argv[optind]);
break;
default:
break;
}
return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
|