Hash :
1b64d381
Author :
Date :
2014-02-20T20:58:47
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062
/*
* Copyright 2013-2014 Andrew Smith
* Copyright 2013 bitfury
*
* BitFury GPIO code originally based on chainminer code:
* https://github.com/bfsb/chainminer
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include "config.h"
#include "compat.h"
#include "miner.h"
#include "sha2.h"
#include "klist.h"
#include <ctype.h>
/*
* Tested on RPi running both Raspbian and Arch
* with BlackArrow BitFury V1 & V2 GPIO Controller
* with 16 chip BlackArrow BitFury boards
*/
#ifndef LINUX
static void bab_detect(__maybe_unused bool hotplug)
{
}
#else
#include <unistd.h>
#include <linux/spi/spidev.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#define BAB_SPI_BUS 0
#define BAB_SPI_CHIP 0
#define BAB_SPI_SPEED 96000
#define BAB_SPI_BUFSIZ 1024
#define BAB_DELAY_USECS 0
#define BAB_TRF_DELAY 0
#define BAB_ADDR(_n) (*((babinfo->gpio) + (_n)))
#define BAB_INP_GPIO(_n) BAB_ADDR((_n) / 10) &= (~(7 << (((_n) % 10) * 3)))
#define BAB_OUT_GPIO(_n) BAB_ADDR((_n) / 10) |= (1 << (((_n) % 10) * 3))
#define BAB_OUT_GPIO_V(_n, _v) BAB_ADDR((_n) / 10) |= (((_v) <= 3 ? (_v) + 4 : \
((_v) == 4 ? 3 : 2)) << (((_n) % 10) * 3))
#define BAB_GPIO_SET BAB_ADDR(7)
#define BAB_GPIO_CLR BAB_ADDR(10)
#define BAB_GPIO_LEVEL BAB_ADDR(13)
// If the V1 test of this many chips finds no chips it will try V2
#define BAB_V1_CHIP_TEST 32
//maximum number of chips per board
#define BAB_BOARDCHIPS 16
#define BAB_MAXBUF (BAB_MAXCHIPS * 512)
#define BAB_V1_BANK 0
//maximum number of alternative banks
#define BAB_MAXBANKS 4
//maximum number of boards in a bank
#define BAB_BANKBOARDS 6
//maximum number of chips on alternative bank
#define BAB_BANKCHIPS (BAB_BOARDCHIPS * BAB_BANKBOARDS)
//maximum number of chips
#define BAB_MAXCHIPS (BAB_MAXBANKS * BAB_BANKCHIPS)
#define BAB_CORES 16
#define BAB_X_COORD 21
#define BAB_Y_COORD 36
#define BAB_NOOP 0
#define BAB_BREAK ((uint8_t *)"\04")
#define BAB_ASYNC ((uint8_t *)"\05")
#define BAB_SYNC ((uint8_t *)"\06")
#define BAB_FFL " - from %s %s() line %d"
#define BAB_FFL_HERE __FILE__, __func__, __LINE__
#define BAB_FFL_PASS file, func, line
#define bab_reset(_bank, _times) _bab_reset(babcgpu, babinfo, _bank, _times)
#define bab_txrx(_item, _det) _bab_txrx(babcgpu, babinfo, _item, _det, BAB_FFL_HERE)
#define bab_add_buf(_item, _data) _bab_add_buf(_item, _data, sizeof(_data)-1, BAB_FFL_HERE)
#define BAB_ADD_BREAK(_item) _bab_add_buf(_item, BAB_BREAK, 1, BAB_FFL_HERE)
#define BAB_ADD_ASYNC(_item) _bab_add_buf(_item, BAB_ASYNC, 1, BAB_FFL_HERE)
#define bab_config_reg(_item, _reg, _ena) _bab_config_reg(_item, _reg, _ena, BAB_FFL_HERE)
#define bab_add_data(_item, _addr, _data, _siz) _bab_add_data(_item, _addr, (const uint8_t *)(_data), _siz, BAB_FFL_HERE)
#define BAB_ADD_NOOPs(_item, _count) _bab_add_noops(_item, _count, BAB_FFL_HERE)
#define BAB_ADD_MIN 4
#define BAB_ADD_MAX 128
#define BAB_BASEA 4
#define BAB_BASEB 61
#define BAB_COUNTERS 16
static const uint8_t bab_counters[BAB_COUNTERS] = {
64, 64,
BAB_BASEA, BAB_BASEA+4,
BAB_BASEA+2, BAB_BASEA+2+16,
BAB_BASEA, BAB_BASEA+1,
(BAB_BASEB)%65, (BAB_BASEB+1)%65,
(BAB_BASEB+3)%65, (BAB_BASEB+3+16)%65,
(BAB_BASEB+4)%65, (BAB_BASEB+4+4)%65,
(BAB_BASEB+3+3)%65, (BAB_BASEB+3+1+3)%65
};
#define BAB_W1 16
static const uint32_t bab_w1[BAB_W1] = {
0, 0, 0, 0xffffffff,
0x80000000, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0x00000280
};
#define BAB_W2 8
static const uint32_t bab_w2[BAB_W2] = {
0x80000000, 0, 0, 0,
0, 0, 0, 0x00000100
};
#define BAB_TEST_DATA 19
static const uint32_t bab_test_data[BAB_TEST_DATA] = {
0xb0e72d8e, 0x1dc5b862, 0xe9e7c4a6, 0x3050f1f5,
0x8a1a6b7e, 0x7ec384e8, 0x42c1c3fc, 0x8ed158a1,
0x8a1a6b7e, 0x6f484872, 0x4ff0bb9b, 0x12c97f07,
0xb0e72d8e, 0x55d979bc, 0x39403296, 0x40f09e84,
0x8a0bb7b7, 0x33af304f, 0x0b290c1a //, 0xf0c4e61f
};
/*
* maximum chip speed available for auto tuner
* speed/nrate/hrate/watt
* 53/ 97/ 100/ 84
* 54/ 98/ 107/ 88
* 55/ 99/ 115/ 93
* 56/ 101/ 125/ 99
*/
#define BAB_MAXSPEED 57
#define BAB_DEFMAXSPEED 55
#define BAB_DEFSPEED 53
#define BAB_MINSPEED 52
#define BAB_ABSMINSPEED 32
/*
* % of errors to tune the speed up or down
* 1.0 to 10.0 should average around 5.5% errors
*/
#define BAB_TUNEUP 1.0
#define BAB_TUNEDOWN 10.0
#define MIDSTATE_BYTES 32
#define MERKLE_OFFSET 64
#define MERKLE_BYTES 12
#define BLOCK_HEADER_BYTES 80
#define MIDSTATE_UINTS (MIDSTATE_BYTES / sizeof(uint32_t))
#define DATA_UINTS ((BLOCK_HEADER_BYTES / sizeof(uint32_t)) - 1)
// Auto adjust
#define BAB_AUTO_REG 0
#define BAB_AUTO_VAL 0x01
// iclk
#define BAB_ICLK_REG 1
#define BAB_ICLK_VAL 0x02
// No fast clock
#define BAB_FAST_REG 2
#define BAB_FAST_VAL 0x04
// Divide by 2
#define BAB_DIV2_REG 3
#define BAB_DIV2_VAL 0x08
// Slow Clock
#define BAB_SLOW_REG 4
#define BAB_SLOW_VAL 0x10
// No oclk
#define BAB_OCLK_REG 6
#define BAB_OCLK_VAL 0x20
// Has configured
#define BAB_CFGD_VAL 0x40
#define BAB_DEFCONF (BAB_AUTO_VAL | \
BAB_ICLK_VAL | \
BAB_DIV2_VAL | \
BAB_SLOW_VAL)
#define BAB_REG_CLR_FROM 7
#define BAB_REG_CLR_TO 11
#define BAB_AUTO_SET(_c) ((_c) & BAB_AUTO_VAL)
#define BAB_ICLK_SET(_c) ((_c) & BAB_ICLK_VAL)
#define BAB_FAST_SET(_c) ((_c) & BAB_FAST_VAL)
#define BAB_DIV2_SET(_c) ((_c) & BAB_DIV2_VAL)
#define BAB_SLOW_SET(_c) ((_c) & BAB_SLOW_VAL)
#define BAB_OCLK_SET(_c) ((_c) & BAB_OCLK_VAL)
#define BAB_CFGD_SET(_c) ((_c) & BAB_CFGD_VAL)
#define BAB_AUTO_BIT(_c) (BAB_AUTO_SET(_c) ? true : false)
#define BAB_ICLK_BIT(_c) (BAB_ICLK_SET(_c) ? false : true)
#define BAB_FAST_BIT(_c) (BAB_FAST_SET(_c) ? true : false)
#define BAB_DIV2_BIT(_c) (BAB_DIV2_SET(_c) ? false : true)
#define BAB_SLOW_BIT(_c) (BAB_SLOW_SET(_c) ? true : false)
#define BAB_OCLK_BIT(_c) (BAB_OCLK_SET(_c) ? true : false)
#define BAB_COUNT_ADDR 0x0100
#define BAB_W1A_ADDR 0x1000
#define BAB_W1B_ADDR 0x1400
#define BAB_W2_ADDR 0x1900
#define BAB_INP_ADDR 0x3000
#define BAB_OSC_ADDR 0x6000
#define BAB_REG_ADDR 0x7000
/*
* valid: 0x01 0x03 0x07 0x0F 0x1F 0x3F 0x7F 0xFF
* max { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x00 }
* max { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x00 }
* avg { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00 }
* slo { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x00 }
* min { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
* good: 0x1F (97) 0x3F (104) 0x7F (109) 0xFF (104)
*/
#define BAB_OSC 8
static const uint8_t bab_osc_bits[BAB_OSC] =
{ 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
static const uint8_t bab_reg_ena[4] = { 0xc1, 0x6a, 0x59, 0xe3 };
static const uint8_t bab_reg_dis[4] = { 0x00, 0x00, 0x00, 0x00 };
#define BAB_NONCE_OFFSETS 3
#define BAB_OFF_0x1C_STA 2
#define BAB_OFF_0x1C_FIN 2
#define BAB_OFF_OTHER_STA 0
#define BAB_OFF_OTHER_FIN 1
#define BAB_EVIL_NONCE 0xe0
#define BAB_EVIL_MASK 0xff
static const uint32_t bab_nonce_offsets[] = {-0x800000, 0, -0x400000};
struct bab_work_send {
uint32_t midstate[MIDSTATE_UINTS];
uint32_t ms3steps[MIDSTATE_UINTS];
uint32_t merkle7;
uint32_t ntime;
uint32_t bits;
};
#define BAB_REPLY_NONCES 16
struct bab_work_reply {
uint32_t nonce[BAB_REPLY_NONCES];
uint32_t jobsel;
uint32_t spichk;
};
#define BAB_CHIP_MIN (sizeof(struct bab_work_reply)+16)
#define ALLOC_WITEMS 1024
#define LIMIT_WITEMS 0
// Work
typedef struct witem {
struct work *work;
struct bab_work_send chip_input;
bool ci_setup;
bool rolled;
int nonces;
struct timeval work_start;
} WITEM;
#define ALLOC_SITEMS 8
#define LIMIT_SITEMS 0
// SPI I/O
typedef struct sitem {
uint32_t siz;
uint8_t wbuf[BAB_MAXBUF];
uint8_t rbuf[BAB_MAXBUF];
uint32_t chip_off[BAB_MAXCHIPS+1];
uint32_t bank_off[BAB_MAXBANKS+2];
// WITEMs used to build the work
K_ITEM *witems[BAB_MAXCHIPS];
struct timeval work_start;
} SITEM;
#define ALLOC_RITEMS 256
#define LIMIT_RITEMS 0
// Results
typedef struct ritem {
int chip;
int nonces;
uint32_t nonce[BAB_REPLY_NONCES];
bool not_first_reply;
struct timeval when;
} RITEM;
#define ALLOC_NITEMS 102400
#define LIMIT_NITEMS 0
// Nonce History
typedef struct nitem {
struct timeval found;
} NITEM;
#define DATAW(_item) ((WITEM *)(_item->data))
#define DATAS(_item) ((SITEM *)(_item->data))
#define DATAR(_item) ((RITEM *)(_item->data))
#define DATAN(_item) ((NITEM *)(_item->data))
// Record the number of each band between work sends
#define BAB_DELAY_BANDS 10
#define BAB_DELAY_BASE 0.5
#define BAB_DELAY_STEP 0.2
#define BAB_CHIP_SPEEDS 6
// less than or equal GH/s
static double chip_speed_ranges[BAB_CHIP_SPEEDS - 1] =
{ 0.0, 0.8, 1.6, 2.2, 2.8 };
// Greater than the last one above means it's the last speed
static char *chip_speed_names[BAB_CHIP_SPEEDS] =
{ "Dead", "V.Slow", "Slow", "OK", "Good", "Fast" };
/*
* This is required to do chip tuning
* If disabled, it will simply run the chips at default speed
* unless they never return valid results
*/
#define UPDATE_HISTORY 1
struct bab_info {
struct thr_info spi_thr;
struct thr_info res_thr;
pthread_mutex_t did_lock;
pthread_mutex_t nonce_lock;
// All GPIO goes through this
volatile unsigned *gpio;
int version;
int spifd;
int chips;
int chips_per_bank[BAB_MAXBANKS+1];
int missing_chips_per_bank[BAB_MAXBANKS+1];
int bank_first_chip[BAB_MAXBANKS+1];
int bank_last_chip[BAB_MAXBANKS+1];
int boards;
int banks;
uint32_t chip_spis[BAB_MAXCHIPS+1];
int reply_wait;
uint64_t reply_waits;
cgsem_t scan_work;
cgsem_t spi_work;
cgsem_t spi_reply;
cgsem_t process_reply;
bool disabled[BAB_MAXCHIPS];
int total_disabled;
struct bab_work_reply chip_results[BAB_MAXCHIPS];
struct bab_work_reply chip_prev[BAB_MAXCHIPS];
uint8_t chip_fast[BAB_MAXCHIPS];
uint8_t chip_conf[BAB_MAXCHIPS];
uint8_t old_fast[BAB_MAXCHIPS];
uint8_t old_conf[BAB_MAXCHIPS];
uint8_t chip_bank[BAB_MAXCHIPS+1];
uint8_t osc[BAB_OSC];
/*
* Ignore errors in the first work reply since
* they may be from a previous run or random junk
* There can be >100 with just one 16 chip board
*/
uint32_t initial_ignored;
bool not_first_reply[BAB_MAXCHIPS];
// Stats
uint64_t core_good[BAB_MAXCHIPS][BAB_CORES];
uint64_t core_bad[BAB_MAXCHIPS][BAB_CORES];
uint64_t chip_spie[BAB_MAXCHIPS]; // spi errors
uint64_t chip_miso[BAB_MAXCHIPS]; // msio errors
uint64_t chip_nonces[BAB_MAXCHIPS];
uint64_t chip_good[BAB_MAXCHIPS];
uint64_t chip_bad[BAB_MAXCHIPS];
uint64_t chip_ncore[BAB_MAXCHIPS][BAB_X_COORD][BAB_Y_COORD];
uint64_t chip_cont_bad[BAB_MAXCHIPS];
uint64_t chip_max_bad[BAB_MAXCHIPS];
uint64_t discarded_e0s;
uint64_t untested_nonces;
uint64_t tested_nonces;
uint64_t new_nonces;
uint64_t ok_nonces;
uint64_t nonce_offset_count[BAB_NONCE_OFFSETS];
uint64_t total_tests;
uint64_t max_tests_per_nonce;
uint64_t total_links;
uint64_t total_proc_links;
uint64_t max_links;
uint64_t max_proc_links;
uint64_t total_work_links;
uint64_t fail;
uint64_t fail_total_tests;
uint64_t fail_total_links;
uint64_t fail_total_work_links;
uint64_t ign_total_tests;
uint64_t ign_total_links;
uint64_t ign_total_work_links;
struct timeval last_sent_work;
uint64_t delay_count;
double delay_min;
double delay_max;
/*
* 0 is below band ranges
* BAB_DELAY_BANDS+1 is above band ranges
*/
uint64_t delay_bands[BAB_DELAY_BANDS+2];
uint64_t send_count;
double send_total;
double send_min;
double send_max;
// Work
K_LIST *wfree_list;
K_STORE *available_work;
K_STORE *chip_work[BAB_MAXCHIPS];
// SPI I/O
K_LIST *sfree_list;
// Waiting to send
K_STORE *spi_list;
// Sent
K_STORE *spi_sent;
// Results
K_LIST *rfree_list;
K_STORE *res_list;
// Nonce History
K_LIST *nfree_list;
K_STORE *good_nonces[BAB_MAXCHIPS];
K_STORE *bad_nonces[BAB_MAXCHIPS];
struct timeval first_work[BAB_MAXCHIPS];
#if UPDATE_HISTORY
uint32_t work_count[BAB_MAXCHIPS];
struct timeval last_tune[BAB_MAXCHIPS];
uint8_t bad_fast[BAB_MAXCHIPS];
bool dead_msg[BAB_MAXCHIPS];
#endif
uint64_t work_unrolled;
uint64_t work_rolled;
// bab-options (in order)
uint8_t max_speed;
uint8_t def_speed;
uint8_t min_speed;
double tune_up;
double tune_down;
uint32_t speed_hz;
uint16_t delay_usecs;
uint64_t trf_delay;
struct timeval last_did;
bool initialised;
};
/*
* Amount of time for history
* Older items in nonce_history are discarded
* 300s / 5 minutes
*/
#define HISTORY_TIME_S 300
/*
* If the SPI I/O thread waits longer than this long for work
* it will report an error saying how long it's waiting
* and again every BAB_STD_WAIT_mS after that
*/
#define BAB_LONG_uS 1200000
/*
* If work wasn't available early enough,
* report every BAB_LONG_WAIT_mS until it is
*/
#define BAB_LONG_WAIT_mS 888
/*
* Some amount of time to wait for work
* before checking how long we've waited
*/
#define BAB_STD_WAIT_mS 888
/*
* How long to wait for the ioctl() to complete (per BANK)
* This is a failsafe in case the ioctl() fails
* since bab_txrx() will already post a wakeup when it completes
* V1 is set to this x 2
* V2 is set to this x active banks
*/
#define BAB_REPLY_WAIT_mS 160
/*
* Work items older than this should not expect results
* It has to allow for the result buffer returned with the next result
* 0.75GH/s takes 5.727s to do a full nonce range
* If HW is too high, consider increasing this to see if work is being
* expired too early (due to slow chips)
*/
#define BAB_WORK_EXPIRE_mS 7800
// Don't send work more often than this
#define BAB_EXPECTED_WORK_DELAY_mS 899
/*
* If a chip only has bad results after this time limit in seconds,
* then switch it down to min_speed
*/
#define BAB_BAD_TO_MIN (HISTORY_TIME_S + 10)
/*
* Also, just to be sure it's actually mining, it must have got this
* many bad results before considering disabling it
*/
#define BAB_BAD_COUNT 100
/*
* If a chip only has bad results after this time limit in seconds,
* then disable it
* A chip only returning bad results will use a lot more CPU than
* an ok chip since all results will be tested against all unexpired
* work that's been sent to the chip
*/
#define BAB_BAD_DEAD (BAB_BAD_TO_MIN * 2)
/*
* Maximum bab_queue_full() will roll work if it is allowed to
* Since work can somtimes (rarely) queue up with many chips,
* limit it to avoid it getting too much range in the pending work
*/
#define BAB_MAX_ROLLTIME 42
static void bab_ms3steps(uint32_t *p)
{
uint32_t a, b, c, d, e, f, g, h, new_e, new_a;
int i;
a = p[0];
b = p[1];
c = p[2];
d = p[3];
e = p[4];
f = p[5];
g = p[6];
h = p[7];
for (i = 0; i < 3; i++) {
new_e = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) + d;
new_a = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) +
SHA256_F1(a) + MAJ(a,b,c);
d = c;
c = b;
b = a;
a = new_a;
h = g;
g = f;
f = e;
e = new_e;
}
p[15] = a;
p[14] = b;
p[13] = c;
p[12] = d;
p[11] = e;
p[10] = f;
p[9] = g;
p[8] = h;
}
static uint32_t bab_decnonce(uint32_t in)
{
uint32_t out;
/* First part load */
out = (in & 0xFF) << 24;
in >>= 8;
/* Byte reversal */
in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
out |= (in >> 2) & 0x3FFFFF;
/* Extraction */
if (in & 1)
out |= (1 << 23);
if (in & 2)
out |= (1 << 22);
out -= 0x800004;
return out;
}
static void cleanup_older(struct cgpu_info *babcgpu, int chip, K_ITEM *witem)
{
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
struct timeval now;
bool expired_item;
K_ITEM *tail;
cgtime(&now);
K_WLOCK(babinfo->chip_work[chip]);
tail = babinfo->chip_work[chip]->tail;
expired_item = false;
// Discard expired work
while (tail) {
if (ms_tdiff(&now, &(DATAW(tail)->work_start)) < BAB_WORK_EXPIRE_mS)
break;
if (tail == witem)
expired_item = true;
k_unlink_item(babinfo->chip_work[chip], tail);
K_WUNLOCK(babinfo->chip_work[chip]);
if (DATAW(tail)->rolled)
free_work(DATAW(tail)->work);
else
work_completed(babcgpu, DATAW(tail)->work);
K_WLOCK(babinfo->chip_work[chip]);
k_add_head(babinfo->wfree_list, tail);
tail = babinfo->chip_work[chip]->tail;
}
// If we didn't expire witem, then remove all older than it
if (!expired_item && witem && witem->next) {
tail = babinfo->chip_work[chip]->tail;
while (tail && tail != witem) {
k_unlink_item(babinfo->chip_work[chip], tail);
K_WUNLOCK(babinfo->chip_work[chip]);
if (DATAW(tail)->rolled)
free_work(DATAW(tail)->work);
else
work_completed(babcgpu, DATAW(tail)->work);
K_WLOCK(babinfo->chip_work[chip]);
k_add_head(babinfo->wfree_list, tail);
tail = babinfo->chip_work[chip]->tail;
}
}
K_WUNLOCK(babinfo->chip_work[chip]);
}
static void _bab_reset(__maybe_unused struct cgpu_info *babcgpu, struct bab_info *babinfo, int bank, int times)
{
const int banks[BAB_MAXBANKS] = { 18, 23, 24, 25 };
int i;
BAB_INP_GPIO(10);
BAB_OUT_GPIO(10);
BAB_INP_GPIO(11);
BAB_OUT_GPIO(11);
if (bank) {
for (i = 0; i < BAB_MAXBANKS; i++) {
BAB_INP_GPIO(banks[i]);
BAB_OUT_GPIO(banks[i]);
if (bank == i+1)
BAB_GPIO_SET = 1 << banks[i];
else
BAB_GPIO_CLR = 1 << banks[i];
}
cgsleep_us(4096);
} else {
for (i = 0; i < BAB_MAXBANKS; i++)
BAB_INP_GPIO(banks[i]);
}
BAB_GPIO_SET = 1 << 11;
for (i = 0; i < times; i++) { // 1us = 1MHz
BAB_GPIO_SET = 1 << 10;
cgsleep_us(1);
BAB_GPIO_CLR = 1 << 10;
cgsleep_us(1);
}
BAB_GPIO_CLR = 1 << 11;
BAB_INP_GPIO(11);
BAB_INP_GPIO(10);
BAB_INP_GPIO(9);
BAB_OUT_GPIO_V(11, 0);
BAB_OUT_GPIO_V(10, 0);
BAB_OUT_GPIO_V(9, 0);
}
// TODO: handle a false return where this is called?
static bool _bab_txrx(struct cgpu_info *babcgpu, struct bab_info *babinfo, K_ITEM *item, bool detect_ignore, const char *file, const char *func, const int line)
{
int bank, i, count, chip1, chip2;
uint32_t siz, pos;
struct spi_ioc_transfer tran;
uintptr_t rbuf, wbuf;
wbuf = (uintptr_t)(DATAS(item)->wbuf);
rbuf = (uintptr_t)(DATAS(item)->rbuf);
siz = (uint32_t)(DATAS(item)->siz);
memset(&tran, 0, sizeof(tran));
tran.speed_hz = babinfo->speed_hz;
tran.delay_usecs = babinfo->delay_usecs;
i = 0;
pos = 0;
for (bank = 0; bank <= BAB_MAXBANKS; bank++) {
if (DATAS(item)->bank_off[bank]) {
bab_reset(bank, 64);
break;
}
}
if (unlikely(bank > BAB_MAXBANKS)) {
applog(LOG_ERR, "%s%d: %s() failed to find a bank" BAB_FFL,
babcgpu->drv->name, babcgpu->device_id,
__func__, BAB_FFL_PASS);
return false;
}
count = 0;
while (siz > 0) {
tran.tx_buf = wbuf;
tran.rx_buf = rbuf;
tran.speed_hz = BAB_SPI_SPEED;
if (pos == DATAS(item)->bank_off[bank]) {
for (; ++bank <= BAB_MAXBANKS; ) {
if (DATAS(item)->bank_off[bank] > pos) {
bab_reset(bank, 64);
break;
}
}
}
if (siz < BAB_SPI_BUFSIZ)
tran.len = siz;
else
tran.len = BAB_SPI_BUFSIZ;
if (pos < DATAS(item)->bank_off[bank] &&
DATAS(item)->bank_off[bank] < (pos + tran.len))
tran.len = DATAS(item)->bank_off[bank] - pos;
for (; i < babinfo->chips; i++) {
if (!DATAS(item)->chip_off[i])
continue;
if (DATAS(item)->chip_off[i] >= pos + tran.len) {
tran.speed_hz = babinfo->chip_spis[i];
break;
}
}
if (unlikely(i > babinfo->chips)) {
applog(LOG_ERR, "%s%d: %s() failed to find chip" BAB_FFL,
babcgpu->drv->name, babcgpu->device_id,
__func__, BAB_FFL_PASS);
return false;
}
if (unlikely(babinfo->chip_spis[i] == BAB_SPI_SPEED)) {
applog(LOG_DEBUG, "%s%d: %s() chip[%d] speed %d shouldn't be %d" BAB_FFL,
babcgpu->drv->name, babcgpu->device_id,
__func__, i, (int)babinfo->chip_spis[i],
BAB_SPI_SPEED, BAB_FFL_PASS);
}
if (unlikely(tran.speed_hz == BAB_SPI_SPEED)) {
applog(LOG_DEBUG, "%s%d: %s() transfer speed %d shouldn't be %d" BAB_FFL,
babcgpu->drv->name, babcgpu->device_id,
__func__, (int)tran.speed_hz,
BAB_SPI_SPEED, BAB_FFL_PASS);
}
count++;
if (ioctl(babinfo->spifd, SPI_IOC_MESSAGE(1), (void *)&tran) < 0) {
if (!detect_ignore || errno != 110) {
for (bank = BAB_MAXBANKS; bank >= 0; bank--) {
if (DATAS(item)->bank_off[bank] &&
pos >= DATAS(item)->bank_off[bank]) {
break;
}
}
for (chip1 = babinfo->chips-1; chip1 >= 0; chip1--) {
if (DATAS(item)->chip_off[chip1] &&
pos >= DATAS(item)->chip_off[chip1]) {
break;
}
}
for (chip2 = babinfo->chips-1; chip2 >= 0; chip2--) {
if (DATAS(item)->chip_off[chip2] &&
(pos + tran.len) >= DATAS(item)->chip_off[chip2]) {
break;
}
}
applog(LOG_ERR, "%s%d: ioctl (%d) siz=%d bank=%d chip=%d-%d"
" failed err=%d" BAB_FFL,
babcgpu->drv->name,
babcgpu->device_id,
count, (int)(tran.len),
bank, chip1, chip2,
errno, BAB_FFL_PASS);
}
return false;
}
siz -= tran.len;
wbuf += tran.len;
rbuf += tran.len;
pos += tran.len;
if (siz > 0 && babinfo->trf_delay > 0)
cgsleep_us(babinfo->trf_delay);
}
cgtime(&(DATAS(item)->work_start));
mutex_lock(&(babinfo->did_lock));
cgtime(&(babinfo->last_did));
mutex_unlock(&(babinfo->did_lock));
return true;
}
static void _bab_add_buf_rev(K_ITEM *item, const uint8_t *data, uint32_t siz, const char *file, const char *func, const int line)
{
uint32_t now_used, i;
uint8_t tmp;
now_used = DATAS(item)->siz;
if (now_used + siz >= BAB_MAXBUF) {
quitfrom(1, file, func, line,
"%s() buffer limit of %d exceeded=%d siz=%d",
__func__, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
}
for (i = 0; i < siz; i++) {
tmp = data[i];
tmp = ((tmp & 0xaa)>>1) | ((tmp & 0x55) << 1);
tmp = ((tmp & 0xcc)>>2) | ((tmp & 0x33) << 2);
tmp = ((tmp & 0xf0)>>4) | ((tmp & 0x0f) << 4);
DATAS(item)->wbuf[now_used + i] = tmp;
}
DATAS(item)->siz += siz;
}
static void _bab_add_buf(K_ITEM *item, const uint8_t *data, size_t siz, const char *file, const char *func, const int line)
{
uint32_t now_used;
now_used = DATAS(item)->siz;
if (now_used + siz >= BAB_MAXBUF) {
quitfrom(1, file, func, line,
"%s() DATAS buffer limit of %d exceeded=%d siz=%d",
__func__, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
}
memcpy(&(DATAS(item)->wbuf[now_used]), data, siz);
DATAS(item)->siz += siz;
}
static void _bab_add_noops(K_ITEM *item, size_t siz, const char *file, const char *func, const int line)
{
uint32_t now_used;
now_used = DATAS(item)->siz;
if (now_used + siz >= BAB_MAXBUF) {
quitfrom(1, file, func, line,
"%s() DATAS buffer limit of %d exceeded=%d siz=%d",
__func__, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
}
memset(&(DATAS(item)->wbuf[now_used]), BAB_NOOP, siz);
DATAS(item)->siz += siz;
}
static void _bab_add_data(K_ITEM *item, uint32_t addr, const uint8_t *data, size_t siz, const char *file, const char *func, const int line)
{
uint8_t tmp[3];
int trf_siz;
if (siz < BAB_ADD_MIN || siz > BAB_ADD_MAX) {
quitfrom(1, file, func, line,
"%s() called with invalid siz=%d (min=%d max=%d)",
__func__, (int)siz, BAB_ADD_MIN, BAB_ADD_MAX);
}
trf_siz = siz / 4;
tmp[0] = (trf_siz - 1) | 0xE0;
tmp[1] = (addr >> 8) & 0xff;
tmp[2] = addr & 0xff;
_bab_add_buf(item, tmp, sizeof(tmp), BAB_FFL_PASS);
_bab_add_buf_rev(item, data, siz, BAB_FFL_PASS);
}
static void _bab_config_reg(K_ITEM *item, uint32_t reg, bool enable, const char *file, const char *func, const int line)
{
if (enable) {
_bab_add_data(item, BAB_REG_ADDR + reg*32,
bab_reg_ena, sizeof(bab_reg_ena), BAB_FFL_PASS);
} else {
_bab_add_data(item, BAB_REG_ADDR + reg*32,
bab_reg_dis, sizeof(bab_reg_dis), BAB_FFL_PASS);
}
}
static void bab_set_osc(struct bab_info *babinfo, int chip)
{
int fast, i;
fast = babinfo->chip_fast[chip];
for (i = 0; i < BAB_OSC && fast > BAB_OSC; i++, fast -= BAB_OSC) {
babinfo->osc[i] = 0xff;
}
if (i < BAB_OSC && fast > 0 && fast <= BAB_OSC)
babinfo->osc[i++] = bab_osc_bits[fast - 1];
for (; i < BAB_OSC; i++)
babinfo->osc[i] = 0x00;
applog(LOG_DEBUG, "@osc(chip=%d) fast=%d 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x", chip, fast, babinfo->osc[0], babinfo->osc[1], babinfo->osc[2], babinfo->osc[3], babinfo->osc[4], babinfo->osc[5], babinfo->osc[6], babinfo->osc[7]);
}
static void bab_put(struct bab_info *babinfo, K_ITEM *sitem)
{
struct bab_work_send *chip_input;
int i, reg, bank = 0;
size_t chip_siz;
BAB_ADD_BREAK(sitem);
for (i = 0; i < babinfo->chips; i++) {
if (babinfo->chip_bank[i] != bank) {
DATAS(sitem)->bank_off[bank] = DATAS(sitem)->siz;
bank = babinfo->chip_bank[i];
BAB_ADD_BREAK(sitem);
}
if (!(babinfo->disabled[i])) {
if (BAB_CFGD_SET(babinfo->chip_conf[i]) || !babinfo->chip_conf[i]) {
bab_set_osc(babinfo, i);
bab_add_data(sitem, BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
bab_config_reg(sitem, BAB_ICLK_REG, BAB_ICLK_BIT(babinfo->chip_conf[i]));
bab_config_reg(sitem, BAB_FAST_REG, BAB_FAST_BIT(babinfo->chip_conf[i]));
bab_config_reg(sitem, BAB_DIV2_REG, BAB_DIV2_BIT(babinfo->chip_conf[i]));
bab_config_reg(sitem, BAB_SLOW_REG, BAB_SLOW_BIT(babinfo->chip_conf[i]));
bab_config_reg(sitem, BAB_OCLK_REG, BAB_OCLK_BIT(babinfo->chip_conf[i]));
for (reg = BAB_REG_CLR_FROM; reg <= BAB_REG_CLR_TO; reg++)
bab_config_reg(sitem, reg, false);
if (babinfo->chip_conf[i]) {
bab_add_data(sitem, BAB_COUNT_ADDR, bab_counters, sizeof(bab_counters));
bab_add_data(sitem, BAB_W1A_ADDR, bab_w1, sizeof(bab_w1));
bab_add_data(sitem, BAB_W1B_ADDR, bab_w1, sizeof(bab_w1)/2);
bab_add_data(sitem, BAB_W2_ADDR, bab_w2, sizeof(bab_w2));
babinfo->chip_conf[i] ^= BAB_CFGD_VAL;
}
babinfo->old_fast[i] = babinfo->chip_fast[i];
babinfo->old_conf[i] = babinfo->chip_conf[i];
} else {
if (babinfo->old_fast[i] != babinfo->chip_fast[i]) {
bab_set_osc(babinfo, i);
bab_add_data(sitem, BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
babinfo->old_fast[i] = babinfo->chip_fast[i];
}
if (babinfo->old_conf[i] != babinfo->chip_conf[i]) {
if (BAB_ICLK_SET(babinfo->old_conf[i]) !=
BAB_ICLK_SET(babinfo->chip_conf[i]))
bab_config_reg(sitem, BAB_ICLK_REG,
BAB_ICLK_BIT(babinfo->chip_conf[i]));
if (BAB_FAST_SET(babinfo->old_conf[i]) !=
BAB_FAST_SET(babinfo->chip_conf[i]))
bab_config_reg(sitem, BAB_FAST_REG,
BAB_FAST_BIT(babinfo->chip_conf[i]));
if (BAB_DIV2_SET(babinfo->old_conf[i]) !=
BAB_DIV2_SET(babinfo->chip_conf[i]))
bab_config_reg(sitem, BAB_DIV2_REG,
BAB_DIV2_BIT(babinfo->chip_conf[i]));
if (BAB_SLOW_SET(babinfo->old_conf[i]) !=
BAB_SLOW_SET(babinfo->chip_conf[i]))
bab_config_reg(sitem, BAB_SLOW_REG,
BAB_SLOW_BIT(babinfo->chip_conf[i]));
if (BAB_OCLK_SET(babinfo->old_conf[i]) !=
BAB_OCLK_SET(babinfo->chip_conf[i]))
bab_config_reg(sitem, BAB_OCLK_REG,
BAB_OCLK_BIT(babinfo->chip_conf[i]));
babinfo->old_conf[i] = babinfo->chip_conf[i];
}
}
DATAS(sitem)->chip_off[i] = DATAS(sitem)->siz + 3;
chip_input = &(DATAW(DATAS(sitem)->witems[i])->chip_input);
if (babinfo->chip_conf[i])
bab_add_data(sitem, BAB_INP_ADDR, (uint8_t *)chip_input, sizeof(*chip_input));
chip_siz = DATAS(sitem)->siz - babinfo->chip_conf[i];
if (chip_siz < BAB_CHIP_MIN)
BAB_ADD_NOOPs(sitem, BAB_CHIP_MIN - chip_siz);
}
BAB_ADD_ASYNC(sitem);
}
DATAS(sitem)->chip_off[i] = DATAS(sitem)->siz;
DATAS(sitem)->bank_off[bank] = DATAS(sitem)->siz;
K_WLOCK(babinfo->spi_list);
k_add_head(babinfo->spi_list, sitem);
K_WUNLOCK(babinfo->spi_list);
cgsem_post(&(babinfo->spi_work));
}
static bool bab_get(__maybe_unused struct cgpu_info *babcgpu, struct bab_info *babinfo, struct timeval *when)
{
K_ITEM *item;
bool delayed;
int i;
item = NULL;
delayed = false;
while (item == NULL) {
cgsem_mswait(&(babinfo->spi_reply), babinfo->reply_wait);
K_WLOCK(babinfo->spi_sent);
item = k_unlink_tail(babinfo->spi_sent);
K_WUNLOCK(babinfo->spi_sent);
if (!item) {
if (!delayed) {
applog(LOG_WARNING, "%s%d: Delay getting work reply ...",
babcgpu->drv->name,
babcgpu->device_id);
delayed = true;
babinfo->reply_waits++;
}
}
}
for (i = 0; i < babinfo->chips; i++) {
if (babinfo->chip_conf[i] & 0x7f) {
memcpy((void *)&(babinfo->chip_results[i]),
(void *)(DATAS(item)->rbuf + DATAS(item)->chip_off[i]),
sizeof(babinfo->chip_results[0]));
}
}
// work_start is also the time the results were read
memcpy(when, &(DATAS(item)->work_start), sizeof(*when));
K_WLOCK(babinfo->sfree_list);
k_add_head(babinfo->sfree_list, item);
K_WUNLOCK(babinfo->sfree_list);
return true;
}
void bab_detect_chips(struct cgpu_info *babcgpu, struct bab_info *babinfo, int bank, int first, int last)
{
int i, reg, j;
K_ITEM *item;
if (sizeof(struct bab_work_send) != sizeof(bab_test_data)) {
quithere(1, "struct bab_work_send (%d) and bab_test_data (%d)"
" must be the same size",
(int)sizeof(struct bab_work_send),
(int)sizeof(bab_test_data));
}
K_WLOCK(babinfo->sfree_list);
item = k_unlink_head_zero(babinfo->sfree_list);
K_WUNLOCK(babinfo->sfree_list);
BAB_ADD_BREAK(item);
for (i = first; i < last && i < BAB_MAXCHIPS; i++) {
bab_set_osc(babinfo, i);
bab_add_data(item, BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
bab_config_reg(item, BAB_ICLK_REG, BAB_ICLK_BIT(babinfo->chip_conf[i]));
bab_config_reg(item, BAB_FAST_REG, BAB_FAST_BIT(babinfo->chip_conf[i]));
bab_config_reg(item, BAB_DIV2_REG, BAB_DIV2_BIT(babinfo->chip_conf[i]));
bab_config_reg(item, BAB_SLOW_REG, BAB_SLOW_BIT(babinfo->chip_conf[i]));
bab_config_reg(item, BAB_OCLK_REG, BAB_OCLK_BIT(babinfo->chip_conf[i]));
for (reg = BAB_REG_CLR_FROM; reg <= BAB_REG_CLR_TO; reg++)
bab_config_reg(item, reg, false);
bab_add_data(item, BAB_COUNT_ADDR, bab_counters, sizeof(bab_counters));
bab_add_data(item, BAB_W1A_ADDR, bab_w1, sizeof(bab_w1));
bab_add_data(item, BAB_W1B_ADDR, bab_w1, sizeof(bab_w1)/2);
bab_add_data(item, BAB_W2_ADDR, bab_w2, sizeof(bab_w2));
DATAS(item)->chip_off[i] = DATAS(item)->siz + 3;
bab_add_data(item, BAB_INP_ADDR, bab_test_data, sizeof(bab_test_data));
DATAS(item)->chip_off[i+1] = DATAS(item)->siz;
DATAS(item)->bank_off[bank] = DATAS(item)->siz;
babinfo->chips = i + 1;
bab_txrx(item, false);
DATAS(item)->siz = 0;
BAB_ADD_BREAK(item);
for (j = first; j <= i; j++) {
DATAS(item)->chip_off[j] = DATAS(item)->siz + 3;
BAB_ADD_ASYNC(item);
}
}
memset(item->data, 0, babinfo->sfree_list->siz);
BAB_ADD_BREAK(item);
for (i = first; i < last && i < BAB_MAXCHIPS; i++) {
DATAS(item)->chip_off[i] = DATAS(item)->siz + 3;
bab_add_data(item, BAB_INP_ADDR, bab_test_data, sizeof(bab_test_data));
BAB_ADD_ASYNC(item);
}
DATAS(item)->chip_off[i] = DATAS(item)->siz;
DATAS(item)->bank_off[bank] = DATAS(item)->siz;
babinfo->chips = i;
bab_txrx(item, true);
DATAS(item)->siz = 0;
babinfo->chips = first;
for (i = first; i < last && i < BAB_MAXCHIPS; i++) {
uint32_t tmp[DATA_UINTS-1];
memcpy(tmp, DATAS(item)->rbuf + DATAS(item)->chip_off[i], sizeof(tmp));
DATAS(item)->chip_off[i] = 0;
for (j = 0; j < BAB_REPLY_NONCES; j++) {
if (tmp[j] != 0xffffffff && tmp[j] != 0x00000000) {
babinfo->chip_bank[i] = bank;
babinfo->chips = i + 1;
break;
}
}
}
for (i = first ; i < babinfo->chips; i++)
babinfo->chip_bank[i] = bank;
K_WLOCK(babinfo->sfree_list);
k_add_head(babinfo->sfree_list, item);
K_WUNLOCK(babinfo->sfree_list);
}
static const char *bab_modules[] = {
"i2c-dev",
"i2c-bcm2708",
"spidev",
"spi-bcm2708",
NULL
};
static const char *bab_memory = "/dev/mem";
static int bab_memory_addr = 0x20200000;
// TODO: add --bab-options for SPEED_HZ, tran.delay_usecs and an inter transfer delay (default 0)
static struct {
int request;
int value;
} bab_ioc[] = {
{ SPI_IOC_RD_MODE, 0 },
{ SPI_IOC_WR_MODE, 0 },
{ SPI_IOC_RD_BITS_PER_WORD, 8 },
{ SPI_IOC_WR_BITS_PER_WORD, 8 },
{ SPI_IOC_RD_MAX_SPEED_HZ, 1000000 },
{ SPI_IOC_WR_MAX_SPEED_HZ, 1000000 },
{ -1, -1 }
};
static bool bab_init_gpio(struct cgpu_info *babcgpu, struct bab_info *babinfo, int bus, int chip)
{
int i, err, memfd, data;
char buf[64];
bab_ioc[4].value = (int)(babinfo->speed_hz);
bab_ioc[5].value = (int)(babinfo->speed_hz);
for (i = 0; bab_modules[i]; i++) {
snprintf(buf, sizeof(buf), "modprobe %s", bab_modules[i]);
err = system(buf);
if (err) {
applog(LOG_ERR, "%s failed to modprobe %s (%d) - you need to be root?",
babcgpu->drv->dname,
bab_modules[i], err);
goto bad_out;
}
}
memfd = open(bab_memory, O_RDWR | O_SYNC);
if (memfd < 0) {
applog(LOG_ERR, "%s failed open %s (%d)",
babcgpu->drv->dname,
bab_memory, errno);
goto bad_out;
}
babinfo->gpio = (volatile unsigned *)mmap(NULL, BAB_SPI_BUFSIZ,
PROT_READ | PROT_WRITE,
MAP_SHARED, memfd,
bab_memory_addr);
if (babinfo->gpio == MAP_FAILED) {
close(memfd);
applog(LOG_ERR, "%s failed mmap gpio (%d)",
babcgpu->drv->dname,
errno);
goto bad_out;
}
close(memfd);
snprintf(buf, sizeof(buf), "/dev/spidev%d.%d", bus, chip);
babinfo->spifd = open(buf, O_RDWR);
if (babinfo->spifd < 0) {
applog(LOG_ERR, "%s failed to open spidev (%d)",
babcgpu->drv->dname,
errno);
goto map_out;
}
babcgpu->device_path = strdup(buf);
for (i = 0; bab_ioc[i].value != -1; i++) {
data = bab_ioc[i].value;
err = ioctl(babinfo->spifd, bab_ioc[i].request, (void *)&data);
if (err < 0) {
applog(LOG_ERR, "%s failed ioctl (%d) (%d)",
babcgpu->drv->dname,
i, errno);
goto close_out;
}
}
for (i = 0; i < BAB_MAXCHIPS; i++)
babinfo->chip_spis[i] = (int)((1000000.0 / (100.0 + 31.0 * (i + 1))) * 1000);
return true;
close_out:
close(babinfo->spifd);
babinfo->spifd = 0;
free(babcgpu->device_path);
babcgpu->device_path = NULL;
map_out:
munmap((void *)(babinfo->gpio), BAB_SPI_BUFSIZ);
babinfo->gpio = NULL;
bad_out:
return false;
}
static void bab_init_chips(struct cgpu_info *babcgpu, struct bab_info *babinfo)
{
int chip, chipoff, bank, chips, new_chips, boards, mis;
applog(LOG_WARNING, "%s V1 first test for %d chips ...",
babcgpu->drv->dname, BAB_V1_CHIP_TEST);
bab_detect_chips(babcgpu, babinfo, 0, 0, BAB_V1_CHIP_TEST);
if (babinfo->chips > 0) {
babinfo->version = 1;
babinfo->banks = 0;
if (babinfo->chips == BAB_V1_CHIP_TEST) {
applog(LOG_WARNING, "%s V1 test for %d more chips ...",
babcgpu->drv->dname, BAB_MAXCHIPS - BAB_V1_CHIP_TEST);
bab_detect_chips(babcgpu, babinfo, 0, BAB_V1_CHIP_TEST, BAB_MAXCHIPS);
}
babinfo->chips_per_bank[BAB_V1_BANK] = babinfo->chips;
babinfo->bank_first_chip[BAB_V1_BANK] = 0;
babinfo->bank_last_chip[BAB_V1_BANK] = babinfo->chips - 1;
babinfo->boards = (int)((float)(babinfo->chips - 1) / BAB_BOARDCHIPS) + 1;
babinfo->reply_wait = BAB_REPLY_WAIT_mS * 2;
if ((chip = (babinfo->chips_per_bank[BAB_V1_BANK] % BAB_BOARDCHIPS))) {
mis = BAB_BOARDCHIPS - chip;
babinfo->missing_chips_per_bank[BAB_V1_BANK] = mis;
applog(LOG_WARNING, "%s V1: missing %d chip%s",
babcgpu->drv->dname, mis,
(mis == 1) ? "" : "s");
}
} else {
applog(LOG_WARNING, "%s no chips found with V1", babcgpu->drv->dname);
applog(LOG_WARNING, "%s V2 test %d banks %d chips ...",
babcgpu->drv->dname, BAB_MAXBANKS, BAB_MAXCHIPS);
chips = 0;
babinfo->version = 2;
babinfo->banks = 0;
for (bank = 1; bank <= BAB_MAXBANKS; bank++) {
for (chipoff = 0; chipoff < BAB_BANKCHIPS; chipoff++) {
chip = babinfo->chips + chipoff;
babinfo->chip_spis[chip] = 625000;
}
bab_reset(bank, 64);
bab_detect_chips(babcgpu, babinfo, bank, babinfo->chips, babinfo->chips + BAB_BANKCHIPS);
new_chips = babinfo->chips - chips;
babinfo->chips_per_bank[bank] = new_chips;
if (new_chips > 0) {
babinfo->bank_first_chip[bank] = babinfo->chips - new_chips;
babinfo->bank_last_chip[bank] = babinfo->chips - 1;
}
chips = babinfo->chips;
if (new_chips == 0)
boards = 0;
else {
boards = (int)((float)(new_chips - 1) / BAB_BOARDCHIPS) + 1;
babinfo->banks++;
}
applog(LOG_WARNING, "%s V2 bank %d: %d chips %d board%s",
babcgpu->drv->dname, bank, new_chips,
boards, (boards == 1) ? "" : "s");
babinfo->boards += boards;
if ((chip = (babinfo->chips_per_bank[bank] % BAB_BOARDCHIPS))) {
mis = BAB_BOARDCHIPS - chip;
babinfo->missing_chips_per_bank[bank] = mis;
applog(LOG_WARNING, "%s V2: bank %d missing %d chip%s",
babcgpu->drv->dname, bank,
mis, (mis == 1) ? "" : "s");
}
}
babinfo->reply_wait = BAB_REPLY_WAIT_mS * babinfo->banks;
bab_reset(0, 8);
}
memcpy(babinfo->old_conf, babinfo->chip_conf, sizeof(babinfo->old_conf));
memcpy(babinfo->old_fast, babinfo->chip_fast, sizeof(babinfo->old_fast));
}
static char *bab_options[] = {
"MaxSpeed",
"DefaultSpeed",
"MinSpeed",
"TuneUp",
"TuneDown",
"SPISpeed",
"SPIDelayuS",
"TransferDelayuS"
};
#define INVOP " Invalid Option "
static void bab_get_options(struct cgpu_info *babcgpu, struct bab_info *babinfo)
{
char *ptr, *colon;
int which, val;
double fval;
long lval;
if (opt_bab_options == NULL)
return;
which = 0;
ptr = opt_bab_options;
while (ptr && *ptr) {
colon = strchr(ptr, ':');
if (colon)
*(colon++) = '\0';
switch (which) {
case 0:
if (*ptr && tolower(*ptr) != 'd') {
val = atoi(ptr);
if (!isdigit(*ptr) || val < BAB_ABSMINSPEED || val > BAB_MAXSPEED) {
quit(1, "%s"INVOP"%s '%s' must be %d <= %s <= %d",
babcgpu->drv->dname,
bab_options[which],
ptr, BAB_ABSMINSPEED,
bab_options[which],
BAB_MAXSPEED);
}
babinfo->max_speed = (uint8_t)val;
// Adjust def,min down if they are above max specified
if (babinfo->def_speed > babinfo->max_speed)
babinfo->def_speed = babinfo->max_speed;
if (babinfo->min_speed > babinfo->max_speed)
babinfo->min_speed = babinfo->max_speed;
}
break;
case 1:
if (*ptr && tolower(*ptr) != 'd') {
val = atoi(ptr);
if (!isdigit(*ptr) || val < BAB_ABSMINSPEED || val > babinfo->max_speed) {
quit(1, "%s"INVOP"%s '%s' must be %d <= %s <= %d",
babcgpu->drv->dname,
bab_options[which],
ptr, BAB_ABSMINSPEED,
bab_options[which],
babinfo->max_speed);
}
babinfo->def_speed = (uint8_t)val;
// Adjust min down if is is above def specified
if (babinfo->min_speed > babinfo->def_speed)
babinfo->min_speed = babinfo->def_speed;
}
break;
case 2:
if (*ptr && tolower(*ptr) != 'd') {
val = atoi(ptr);
if (!isdigit(*ptr) || val < BAB_ABSMINSPEED || val > babinfo->def_speed) {
quit(1, "%s"INVOP"%s '%s' must be %d <= %s <= %d",
babcgpu->drv->dname,
bab_options[which],
ptr, BAB_ABSMINSPEED,
bab_options[which],
babinfo->def_speed);
}
babinfo->min_speed = (uint8_t)val;
}
break;
case 3:
if (*ptr && tolower(*ptr) != 'd') {
fval = atof(ptr);
if (!isdigit(*ptr) || fval < 0.0 || fval > 100.0) {
quit(1, "%s"INVOP"%s '%s' must be 0.0 <= %s <= 100.0",
babcgpu->drv->dname,
bab_options[which], ptr,
bab_options[which]);
}
babinfo->tune_up = fval;
}
break;
case 4:
if (*ptr && tolower(*ptr) != 'd') {
fval = atof(ptr);
if (!isdigit(*ptr) || fval < 0.0 || fval > 100.0) {
quit(1, "%s"INVOP"%s '%s' must be %f <= %s <= 100.0",
babcgpu->drv->dname,
bab_options[which],
ptr, babinfo->tune_up,
bab_options[which]);
}
babinfo->tune_down = fval;
}
break;
case 5:
if (*ptr && tolower(*ptr) != 'd') {
val = atoi(ptr);
if (!isdigit(*ptr) || val < 10000 || val > 10000000) {
quit(1, "%s"INVOP"%s '%s' must be 10,000 <= %s <= 10,000,000",
babcgpu->drv->dname,
bab_options[which], ptr,
bab_options[which]);
}
babinfo->speed_hz = (uint32_t)val;
}
break;
case 6:
if (*ptr && tolower(*ptr) != 'd') {
val = atoi(ptr);
if (!isdigit(*ptr) || val < 0 || val > 65535) {
quit(1, "%s"INVOP"%s '%s' must be 0 <= %s <= 65535",
babcgpu->drv->dname,
bab_options[which], ptr,
bab_options[which]);
}
babinfo->delay_usecs = (uint16_t)val;
}
break;
case 7:
if (*ptr && tolower(*ptr) != 'd') {
lval = atol(ptr);
if (!isdigit(*ptr) || lval < 0) {
quit(1, "%s"INVOP"%s '%s' must be %s >= 0",
babcgpu->drv->dname,
bab_options[which], ptr,
bab_options[which]);
}
babinfo->trf_delay = (uint64_t)lval;
}
break;
default:
break;
}
ptr = colon;
which++;
}
}
static void bab_detect(bool hotplug)
{
struct cgpu_info *babcgpu = NULL;
struct bab_info *babinfo = NULL;
int i;
if (hotplug)
return;
babcgpu = calloc(1, sizeof(*babcgpu));
if (unlikely(!babcgpu))
quithere(1, "Failed to calloc babcgpu");
babcgpu->drv = &bab_drv;
babcgpu->deven = DEV_ENABLED;
babcgpu->threads = 1;
babinfo = calloc(1, sizeof(*babinfo));
if (unlikely(!babinfo))
quithere(1, "Failed to calloc babinfo");
babcgpu->device_data = (void *)babinfo;
babinfo->max_speed = BAB_DEFMAXSPEED;
babinfo->def_speed = BAB_DEFSPEED;
babinfo->min_speed = BAB_ABSMINSPEED;
babinfo->tune_up = BAB_TUNEUP;
babinfo->tune_down = BAB_TUNEDOWN;
babinfo->speed_hz = BAB_SPI_SPEED;
babinfo->delay_usecs = BAB_DELAY_USECS;
babinfo->trf_delay = BAB_TRF_DELAY;
bab_get_options(babcgpu, babinfo);
for (i = 0; i < BAB_MAXCHIPS; i++) {
babinfo->chip_conf[i] = BAB_DEFCONF;
babinfo->chip_fast[i] = babinfo->def_speed;
#if UPDATE_HISTORY
babinfo->bad_fast[i] = babinfo->max_speed + 1;
#endif
}
if (!bab_init_gpio(babcgpu, babinfo, BAB_SPI_BUS, BAB_SPI_CHIP))
goto unalloc;
babinfo->sfree_list = k_new_list("SPI I/O", sizeof(SITEM),
ALLOC_SITEMS, LIMIT_SITEMS, true);
babinfo->spi_list = k_new_store(babinfo->sfree_list);
babinfo->spi_sent = k_new_store(babinfo->sfree_list);
for (i = 0; i <= BAB_MAXBANKS; i++) {
babinfo->bank_first_chip[i] = -1;
babinfo->bank_last_chip[i] = -1;
}
bab_init_chips(babcgpu, babinfo);
if (babinfo->boards) {
applog(LOG_WARNING, "%s found %d chips %d board%s",
babcgpu->drv->dname, babinfo->chips,
babinfo->boards,
(babinfo->boards == 1) ? "" : "s");
} else {
applog(LOG_WARNING, "%s found %d chips",
babcgpu->drv->dname, babinfo->chips);
}
if (babinfo->chips == 0)
goto cleanup;
if (!add_cgpu(babcgpu))
goto cleanup;
cgsem_init(&(babinfo->scan_work));
cgsem_init(&(babinfo->spi_work));
cgsem_init(&(babinfo->spi_reply));
cgsem_init(&(babinfo->process_reply));
mutex_init(&babinfo->did_lock);
mutex_init(&babinfo->nonce_lock);
babinfo->rfree_list = k_new_list("Results", sizeof(RITEM),
ALLOC_RITEMS, LIMIT_RITEMS, true);
babinfo->res_list = k_new_store(babinfo->rfree_list);
babinfo->wfree_list = k_new_list("Work", sizeof(WITEM),
ALLOC_WITEMS, LIMIT_WITEMS, true);
babinfo->available_work = k_new_store(babinfo->wfree_list);
for (i = 0; i < BAB_MAXCHIPS; i++)
babinfo->chip_work[i] = k_new_store(babinfo->wfree_list);
babinfo->nfree_list = k_new_list("Nonce History", sizeof(WITEM),
ALLOC_NITEMS, LIMIT_NITEMS, true);
for (i = 0; i < BAB_MAXCHIPS; i++) {
babinfo->good_nonces[i] = k_new_store(babinfo->nfree_list);
babinfo->bad_nonces[i] = k_new_store(babinfo->nfree_list);
}
// Exclude detection
cgtime(&(babcgpu->dev_start_tv));
// Ignore detection tests
babinfo->last_did.tv_sec = 0;
babinfo->initialised = true;
return;
cleanup:
close(babinfo->spifd);
munmap((void *)(babinfo->gpio), BAB_SPI_BUFSIZ);
unalloc:
free(babinfo);
free(babcgpu);
}
static void bab_identify(__maybe_unused struct cgpu_info *babcgpu)
{
}
// thread to do spi txrx
static void *bab_spi(void *userdata)
{
struct cgpu_info *babcgpu = (struct cgpu_info *)userdata;
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
struct timeval start, stop, send, now;
K_ITEM *sitem, *witem;
double wait, delay;
int chip, band;
applog(LOG_DEBUG, "%s%i: SPIing...",
babcgpu->drv->name, babcgpu->device_id);
// Wait until we're ready
while (babcgpu->shutdown == false) {
if (babinfo->initialised) {
break;
}
cgsleep_ms(3);
}
cgtime(&start);
while (babcgpu->shutdown == false) {
K_WLOCK(babinfo->spi_list);
sitem = k_unlink_tail(babinfo->spi_list);
K_WUNLOCK(babinfo->spi_list);
if (!sitem) {
cgtime(&stop);
wait = us_tdiff(&stop, &start);
if (wait > BAB_LONG_uS) {
applog(LOG_WARNING, "%s%i: SPI waiting %fs ...",
babcgpu->drv->name,
babcgpu->device_id,
(float)wait / 1000000.0);
cgsem_mswait(&(babinfo->spi_work), BAB_LONG_WAIT_mS);
} else
cgsem_mswait(&(babinfo->spi_work), (int)((BAB_LONG_uS - wait) / 1000));
continue;
}
// TODO: need an LP/urgent flag to skip this possible cgsem_mswait()
// maybe zero last_sent_work.tv_sec ?
while (babinfo->last_sent_work.tv_sec) {
cgtime(&now);
delay = tdiff(&now, &(babinfo->last_sent_work)) * 1000.0;
if (delay < BAB_EXPECTED_WORK_DELAY_mS)
cgsem_mswait(&(babinfo->spi_work), BAB_EXPECTED_WORK_DELAY_mS - delay);
else
break;
}
/*
* TODO: handle if an LP happened after bab_do_work() started
* i.e. we don't want to send the work
* Have an LP counter that at this point would show the work
* is stale - so don't send it
*/
cgtime(&send);
bab_txrx(sitem, false);
cgtime(&start);
// The work isn't added to the chip until it has been sent
K_WLOCK(babinfo->wfree_list);
for (chip = 0; chip < babinfo->chips; chip++) {
witem = DATAS(sitem)->witems[chip];
if (witem) {
memcpy(&(DATAW(witem)->work_start), &(DATAS(sitem)->work_start),
sizeof(DATAW(witem)->work_start));
k_add_head(babinfo->chip_work[chip], witem);
#if UPDATE_HISTORY
babinfo->work_count[chip]++;
#endif
if (babinfo->first_work[chip].tv_sec == 0)
memcpy(&(babinfo->first_work[chip]), &send, sizeof(send));
}
}
K_WUNLOCK(babinfo->wfree_list);
K_WLOCK(babinfo->spi_sent);
k_add_head(babinfo->spi_sent, sitem);
K_WUNLOCK(babinfo->spi_sent);
cgsem_post(&(babinfo->spi_reply));
// Store stats
if (babinfo->last_sent_work.tv_sec) {
delay = tdiff(&send, &(babinfo->last_sent_work));
babinfo->delay_count++;
if (babinfo->delay_min == 0 || babinfo->delay_min > delay)
babinfo->delay_min = delay;
if (babinfo->delay_max < delay)
babinfo->delay_max = delay;
if (delay < BAB_DELAY_BASE)
band = 0;
else if (delay >= (BAB_DELAY_BASE+BAB_DELAY_STEP*(BAB_DELAY_BANDS+1)))
band = BAB_DELAY_BANDS+1;
else
band = (int)(((double)delay - BAB_DELAY_BASE) / BAB_DELAY_STEP) + 1;
babinfo->delay_bands[band]++;
}
memcpy(&(babinfo->last_sent_work), &send, sizeof(start));
delay = tdiff(&start, &send);
babinfo->send_count++;
babinfo->send_total += delay;
if (babinfo->send_min == 0 || babinfo->send_min > delay)
babinfo->send_min = delay;
if (babinfo->send_max < delay)
babinfo->send_max = delay;
cgsem_mswait(&(babinfo->spi_work), BAB_STD_WAIT_mS);
}
return NULL;
}
static void bab_flush_work(struct cgpu_info *babcgpu)
{
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
applog(LOG_DEBUG, "%s%i: flushing work",
babcgpu->drv->name, babcgpu->device_id);
mutex_lock(&(babinfo->did_lock));
babinfo->last_did.tv_sec = 0;
mutex_unlock(&(babinfo->did_lock));
cgsem_post(&(babinfo->scan_work));
}
#define DATA_MERKLE7 16
#define DATA_NTIME 17
#define DATA_BITS 18
#define DATA_NONCE 19
#define WORK_MERKLE7 (16*4)
#define WORK_NTIME (17*4)
#define WORK_BITS (18*4)
#define WORK_NONCE (19*4)
#if UPDATE_HISTORY
static void process_history(struct cgpu_info *babcgpu, int chip, struct timeval *when, bool good, struct timeval *now)
{
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
uint64_t good_nonces, bad_nonces;
uint8_t chip_fast;
double tune;
K_ITEM *item;
int i;
K_WLOCK(babinfo->nfree_list);
item = k_unlink_head(babinfo->nfree_list);
memcpy(&(DATAN(item)->found), when, sizeof(*when));
if (good)
k_add_head(babinfo->good_nonces[chip], item);
else
k_add_head(babinfo->bad_nonces[chip], item);
// Remove all expired history
for (i = 0; i < babinfo->chips; i++) {
item = babinfo->good_nonces[i]->tail;
while (item) {
if (tdiff(now, &(DATAN(item)->found)) < HISTORY_TIME_S)
break;
k_unlink_item(babinfo->good_nonces[i], item);
k_add_head(babinfo->nfree_list, item);
item = babinfo->good_nonces[i]->tail;
}
item = babinfo->bad_nonces[i]->tail;
while (item) {
if (tdiff(now, &(DATAN(item)->found)) < HISTORY_TIME_S)
break;
k_unlink_item(babinfo->bad_nonces[i], item);
k_add_head(babinfo->nfree_list, item);
item = babinfo->bad_nonces[i]->tail;
}
}
good_nonces = babinfo->good_nonces[chip]->count;
bad_nonces = babinfo->bad_nonces[chip]->count;
K_WUNLOCK(babinfo->nfree_list);
// Tuning ...
if (tdiff(now, &(babinfo->first_work[chip])) >= HISTORY_TIME_S &&
tdiff(now, &(babinfo->last_tune[chip])) >= HISTORY_TIME_S &&
(good_nonces + bad_nonces) > 0) {
chip_fast = babinfo->chip_fast[chip];
/*
* If dead then step it down and remember the speed
* TODO: does a speed change reset the chip? Or is there a reset?
*/
if (good_nonces == 0) {
if (chip_fast > babinfo->min_speed) {
if (babinfo->bad_fast[chip] > chip_fast)
babinfo->bad_fast[chip] = chip_fast;
babinfo->chip_fast[chip]--;
applog(LOG_WARNING, "%s%d: Chip %d DEAD - speed down from %d to %d",
babcgpu->drv->name, babcgpu->device_id,
chip, (int)chip_fast, (int)chip_fast - 1);
} else {
/*
* Permanently DEAD since we're already at the minumum speed
* but only getting bad nonces
*/
if (babinfo->dead_msg[chip] == false) {
applog(LOG_WARNING, "%s%d: Chip %d DEAD - at min speed %d",
babcgpu->drv->name, babcgpu->device_id,
chip, (int)chip_fast);
babinfo->dead_msg[chip] = true;
}
}
goto tune_over;
}
/*
* It 'was' permanently DEAD but a good nonce came back!
*/
if (babinfo->dead_msg[chip]) {
applog(LOG_WARNING, "%s%d: Chip %d REVIVED - at speed %d",
babcgpu->drv->name, babcgpu->device_id,
chip, (int)chip_fast);
babinfo->dead_msg[chip] = false;
}
/*
* Since we have found 'some' nonces -
* make sure bad_fast is higher than current chip_fast
*/
if (babinfo->bad_fast[chip] <= chip_fast)
babinfo->bad_fast[chip] = chip_fast + 1;
tune = (double)bad_nonces / (double)(good_nonces + bad_nonces) * 100.0;
/*
* TODO: it appears some chips just get a % bad at low speeds
* so we should handle them by weighting the speed reduction vs
* the HW% gained from the reduction (i.e. GH/s)
* Maybe handle that when they hit min_speed, then do a gradual speed
* up verifying if it is really making GH/s worse or better
*/
// Tune it down if error rate is too high (and it's above min)
if (tune >= babinfo->tune_down && chip_fast > babinfo->min_speed) {
babinfo->chip_fast[chip]--;
applog(LOG_WARNING, "%s%d: Chip %d High errors %.2f%% - speed down %d to %d",
babcgpu->drv->name, babcgpu->device_id,
chip, tune, (int)chip_fast, (int)chip_fast - 1);
goto tune_over;
}
/*
* TODO: if we are at bad_fast-1 and tune_up
* and bad_fast was set more than some time limit ago
* then consider increasing bad_fast by 1?
*/
// Tune it up if error rate is low enough
if (tune <= babinfo->tune_up &&
chip_fast < babinfo->max_speed &&
chip_fast < (babinfo->bad_fast[chip] - 1)) {
babinfo->chip_fast[chip]++;
applog(LOG_WARNING, "%s%d: Chip %d Low errors %.2f%% - speed up %d to %d",
babcgpu->drv->name, babcgpu->device_id,
chip, tune, (int)chip_fast, (int)chip_fast + 1);
goto tune_over;
}
tune_over:
cgtime(&(babinfo->last_tune[chip]));
}
}
#endif
/*
* Find the matching work item by checking each nonce against
* work items for the nonces chip
*/
static K_ITEM *process_nonce(struct thr_info *thr, struct cgpu_info *babcgpu, K_ITEM *ritem, uint32_t raw_nonce, K_ITEM *newest_witem)
{
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
unsigned int links, proc_links, work_links, tests;
int try_sta, try_fin, offset;
K_ITEM *witem, *wtail;
struct timeval now;
bool not_first_reply;
uint32_t nonce;
int chip;
chip = DATAR(ritem)->chip;
not_first_reply = DATAR(ritem)->not_first_reply;
babinfo->chip_nonces[chip]++;
/*
* We can grab the head of the chip work queue and then release
* the lock and follow it to the end and back, since the other
* thread will only add items above the head - it wont touch
* any of the prev/next pointers from the head to the end -
* except the head->prev pointer may get changed
*/
K_RLOCK(babinfo->chip_work[chip]);
witem = babinfo->chip_work[chip]->head;
K_RUNLOCK(babinfo->chip_work[chip]);
if (!witem) {
applog(LOG_ERR, "%s%i: chip %d has no work, 1 nonce discarded!",
babcgpu->drv->name, babcgpu->device_id, chip);
babinfo->untested_nonces++;
return newest_witem;
}
babinfo->tested_nonces++;
if ((raw_nonce & 0xff) < 0x1c) {
// Will only be this offset
try_sta = BAB_OFF_0x1C_STA;
try_fin = BAB_OFF_0x1C_FIN;
} else {
// Will only be one of the other offsets
try_sta = BAB_OFF_OTHER_STA;
try_fin = BAB_OFF_OTHER_FIN;
}
nonce = bab_decnonce(raw_nonce);
cgtime(&now);
tests = links = proc_links = work_links = 0;
wtail = witem;
while (wtail && wtail->next) {
work_links++;
wtail = wtail->next;
}
while (wtail) {
if (!(DATAW(wtail)->work)) {
applog(LOG_ERR, "%s%i: chip %d witem links %d has no work!",
babcgpu->drv->name,
babcgpu->device_id,
chip, links);
} else {
if (ms_tdiff(&now, &(DATAW(wtail)->work_start)) >= BAB_WORK_EXPIRE_mS)
proc_links--;
else {
for (offset = try_sta; offset <= try_fin; offset++) {
tests++;
if (test_nonce(DATAW(wtail)->work, nonce + bab_nonce_offsets[offset])) {
submit_tested_work(thr, DATAW(wtail)->work);
babinfo->nonce_offset_count[offset]++;
babinfo->chip_good[chip]++;
DATAW(wtail)->nonces++;
mutex_lock(&(babinfo->nonce_lock));
babinfo->new_nonces++;
mutex_unlock(&(babinfo->nonce_lock));
babinfo->ok_nonces++;
babinfo->total_tests += tests;
if (babinfo->max_tests_per_nonce < tests)
babinfo->max_tests_per_nonce = tests;
babinfo->total_links += links;
babinfo->total_proc_links += proc_links;
if (babinfo->max_links < links)
babinfo->max_links = links;
if (babinfo->max_proc_links < proc_links)
babinfo->max_proc_links = proc_links;
babinfo->total_work_links += work_links;
babinfo->chip_cont_bad[chip] = 0;
#if UPDATE_HISTORY
process_history(babcgpu, chip,
&(DATAR(ritem)->when),
true, &now);
#endif
if (newest_witem == NULL ||
ms_tdiff(&(DATAW(wtail)->work_start),
&(DATAW(newest_witem)->work_start)) < 0)
return wtail;
return newest_witem;
}
}
}
}
if (wtail == witem)
break;
wtail = wtail->prev;
links++;
proc_links++;
}
if (not_first_reply) {
babinfo->chip_bad[chip]++;
inc_hw_errors(thr);
babinfo->fail++;
babinfo->fail_total_tests += tests;
babinfo->fail_total_links += links;
babinfo->fail_total_work_links += work_links;
babinfo->chip_cont_bad[chip]++;
if (babinfo->chip_max_bad[chip] < babinfo->chip_cont_bad[chip])
babinfo->chip_max_bad[chip] = babinfo->chip_cont_bad[chip];
// Handle chips with only bad results
if (babinfo->disabled[chip] == false &&
babinfo->chip_good[chip] == 0 &&
babinfo->chip_bad[chip] >= BAB_BAD_COUNT &&
tdiff(&now, &(babinfo->first_work[chip])) >= BAB_BAD_TO_MIN) {
if (babinfo->chip_fast[chip] > babinfo->min_speed)
babinfo->chip_fast[chip] = babinfo->min_speed;
else if (tdiff(&now, &(babinfo->first_work[chip])) > BAB_BAD_DEAD) {
babinfo->disabled[chip] = true;
babinfo->total_disabled++;
applog(LOG_ERR, "%s%i: chip %d disabled!",
babcgpu->drv->name,
babcgpu->device_id,
chip);
}
}
#if UPDATE_HISTORY
process_history(babcgpu, chip, &(DATAR(ritem)->when), false, &now);
#endif
} else {
babinfo->initial_ignored++;
babinfo->ign_total_tests += tests;
babinfo->ign_total_links += links;
babinfo->ign_total_work_links += work_links;
}
return newest_witem;
}
/*
* On completion discard any work items older than BAB_WORK_EXPIRE_mS
* and any work items of the chip older than the work of the newest nonce work item
*/
static void oknonces(struct thr_info *thr, struct cgpu_info *babcgpu, K_ITEM *ritem)
{
uint32_t raw_nonce;
K_ITEM *witem;
int nonces;
witem = NULL;
for (nonces = 0; nonces < DATAR(ritem)->nonces; nonces++) {
raw_nonce = DATAR(ritem)->nonce[nonces];
witem = process_nonce(thr, babcgpu, ritem, raw_nonce, witem);
}
cleanup_older(babcgpu, DATAR(ritem)->chip, witem);
}
// Check at least every ...
#define BAB_RESULT_DELAY_mS 999
// Results checking thread
static void *bab_res(void *userdata)
{
struct cgpu_info *babcgpu = (struct cgpu_info *)userdata;
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
struct thr_info *thr = babcgpu->thr[0];
K_ITEM *ritem;
applog(LOG_DEBUG, "%s%i: Results...",
babcgpu->drv->name, babcgpu->device_id);
// Wait until we're ready
while (babcgpu->shutdown == false) {
if (babinfo->initialised) {
break;
}
cgsleep_ms(3);
}
ritem = NULL;
while (babcgpu->shutdown == false) {
K_WLOCK(babinfo->res_list);
if (ritem) {
// Release the old one
k_add_head(babinfo->rfree_list, ritem);
ritem = NULL;
}
// Check for a new one
ritem = k_unlink_tail(babinfo->res_list);
K_WUNLOCK(babinfo->res_list);
if (!ritem) {
cgsem_mswait(&(babinfo->process_reply), BAB_RESULT_DELAY_mS);
continue;
}
oknonces(thr, babcgpu, ritem);
}
return NULL;
}
/*
* 1.0s per nonce = 4.2GH/s
* 0.9s per nonce = 4.8GH/s
* On a slow machine, reducing this may resolve:
* BaB0: SPI waiting 1.2...s
*/
#define BAB_STD_WORK_DELAY_uS 900000
static bool bab_do_work(struct cgpu_info *babcgpu)
{
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
int work_items = 0;
K_ITEM *witem, *sitem, *ritem;
struct timeval when, now;
double delay;
int chip, rep, j, nonces, spie = 0, miso = 0;
uint32_t nonce, spichk;
bool res;
cgtime(&now);
mutex_lock(&(babinfo->did_lock));
delay = us_tdiff(&now, &(babinfo->last_did));
mutex_unlock(&(babinfo->did_lock));
if (delay < BAB_STD_WORK_DELAY_uS)
return false;
K_WLOCK(babinfo->sfree_list);
sitem = k_unlink_head_zero(babinfo->sfree_list);
K_WUNLOCK(babinfo->sfree_list);
for (chip = 0; chip < babinfo->chips; chip++) {
if (!(babinfo->disabled[chip])) {
// TODO: ignore stale work
K_WLOCK(babinfo->available_work);
witem = k_unlink_tail(babinfo->available_work);
K_WUNLOCK(babinfo->available_work);
if (!witem) {
applog(LOG_ERR, "%s%i: short work list (%d) %d expected %d - reset",
babcgpu->drv->name, babcgpu->device_id,
chip, work_items,
babinfo->chips - babinfo->total_disabled);
// Put them back in the order they were taken
K_WLOCK(babinfo->available_work);
for (j = chip-1; j >= 0; j--) {
witem = DATAS(sitem)->witems[j];
if (witem)
k_add_tail(babinfo->available_work, witem);
}
K_WUNLOCK(babinfo->available_work);
K_WLOCK(babinfo->sfree_list);
k_add_head(babinfo->sfree_list, sitem);
K_WUNLOCK(babinfo->sfree_list);
return false;
}
/*
* TODO: do this when we get work except on LP?
* (not LP so we only do ms3steps for work required)
* Though that may more likely trigger the applog(short work list) above?
*/
if (DATAW(witem)->ci_setup == false) {
memcpy((void *)&(DATAW(witem)->chip_input.midstate[0]),
DATAW(witem)->work->midstate,
sizeof(DATAW(witem)->work->midstate));
memcpy((void *)&(DATAW(witem)->chip_input.merkle7),
(void *)&(DATAW(witem)->work->data[WORK_MERKLE7]),
MERKLE_BYTES);
bab_ms3steps((void *)&(DATAW(witem)->chip_input));
DATAW(witem)->ci_setup = true;
}
DATAS(sitem)->witems[chip] = witem;
work_items++;
}
}
// Send
bab_put(babinfo, sitem);
// Receive
res = bab_get(babcgpu, babinfo, &when);
if (!res) {
applog(LOG_DEBUG, "%s%i: didn't get work reply ...",
babcgpu->drv->name, babcgpu->device_id);
return false;
}
applog(LOG_DEBUG, "%s%i: Did get work reply ...",
babcgpu->drv->name, babcgpu->device_id);
for (chip = 0; chip < babinfo->chips; chip++) {
if (!(babinfo->disabled[chip])) {
K_WLOCK(babinfo->rfree_list);
ritem = k_unlink_head(babinfo->rfree_list);
K_WUNLOCK(babinfo->rfree_list);
DATAR(ritem)->chip = chip;
DATAR(ritem)->not_first_reply = babinfo->not_first_reply[chip];
memcpy(&(DATAR(ritem)->when), &when, sizeof(when));
spichk = babinfo->chip_results[chip].spichk;
if (spichk != 0 && spichk != 0xffffffff) {
babinfo->chip_spie[chip]++;
spie++;
// Test the results anyway
}
nonces = 0;
for (rep = 0; rep < BAB_REPLY_NONCES; rep++) {
nonce = babinfo->chip_results[chip].nonce[rep];
if (nonce != babinfo->chip_prev[chip].nonce[rep]) {
if ((nonce & BAB_EVIL_MASK) == BAB_EVIL_NONCE)
babinfo->discarded_e0s++;
else
DATAR(ritem)->nonce[nonces++] = nonce;
}
}
if (nonces == BAB_REPLY_NONCES) {
babinfo->chip_miso[chip]++;
miso++;
// Test the results anyway
}
/*
* Send even with zero nonces
* so cleanup_older() is called for the chip
*/
DATAR(ritem)->nonces = nonces;
K_WLOCK(babinfo->res_list);
k_add_head(babinfo->res_list, ritem);
K_WUNLOCK(babinfo->res_list);
cgsem_post(&(babinfo->process_reply));
babinfo->not_first_reply[chip] = true;
memcpy((void *)(&(babinfo->chip_prev[chip])),
(void *)(&(babinfo->chip_results[chip])),
sizeof(struct bab_work_reply));
}
}
applog(LOG_DEBUG, "Work: items:%d spie:%d miso:%d", work_items, spie, miso);
return true;
}
static bool bab_thread_prepare(struct thr_info *thr)
{
struct cgpu_info *babcgpu = thr->cgpu;
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
if (thr_info_create(&(babinfo->spi_thr), NULL, bab_spi, (void *)babcgpu)) {
applog(LOG_ERR, "%s%i: SPI thread create failed",
babcgpu->drv->name, babcgpu->device_id);
return false;
}
pthread_detach(babinfo->spi_thr.pth);
/*
* We require a seperate results checking thread since there is a lot
* of work done checking the results multiple times - thus we don't
* want that delay affecting sending/receiving work to/from the device
*/
if (thr_info_create(&(babinfo->res_thr), NULL, bab_res, (void *)babcgpu)) {
applog(LOG_ERR, "%s%i: Results thread create failed",
babcgpu->drv->name, babcgpu->device_id);
return false;
}
pthread_detach(babinfo->res_thr.pth);
return true;
}
static void bab_shutdown(struct thr_info *thr)
{
struct cgpu_info *babcgpu = thr->cgpu;
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
int i;
applog(LOG_DEBUG, "%s%i: shutting down",
babcgpu->drv->name, babcgpu->device_id);
for (i = 0; i < babinfo->chips; i++)
// TODO: bab_shutdown(babcgpu, babinfo, i);
;
babcgpu->shutdown = true;
}
static bool bab_queue_full(struct cgpu_info *babcgpu)
{
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
int roll, roll_limit = BAB_MAX_ROLLTIME;
struct work *work, *usework;
K_ITEM *item;
int count, need;
bool ret, rolled;
K_RLOCK(babinfo->available_work);
count = babinfo->available_work->count;
K_RUNLOCK(babinfo->available_work);
if (count >= (babinfo->chips - babinfo->total_disabled))
ret = true;
else {
need = (babinfo->chips - babinfo->total_disabled) - count;
work = get_queued(babcgpu);
if (work) {
if (roll_limit > work->drv_rolllimit)
roll_limit = work->drv_rolllimit;
roll = 0;
do {
if (roll == 0) {
usework = work;
babinfo->work_unrolled++;
rolled = false;
} else {
usework = copy_work_noffset(work, roll);
babinfo->work_rolled++;
rolled = true;
}
K_WLOCK(babinfo->wfree_list);
item = k_unlink_head_zero(babinfo->wfree_list);
DATAW(item)->work = usework;
DATAW(item)->rolled = rolled;
k_add_head(babinfo->available_work, item);
K_WUNLOCK(babinfo->wfree_list);
} while (--need > 0 && ++roll <= roll_limit);
} else {
// Avoid a hard loop when we can't get work fast enough
cgsleep_us(42);
}
if (need > 0)
ret = false;
else
ret = true;
}
return ret;
}
#define BAB_STD_DELAY_mS 100
/*
* TODO: allow this to run through more than once - the second+
* time not sending any new work unless a flush occurs since:
* at the moment we have BAB_STD_WORK_mS latency added to earliest replies
*/
static int64_t bab_scanwork(__maybe_unused struct thr_info *thr)
{
struct cgpu_info *babcgpu = thr->cgpu;
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
int64_t hashcount = 0;
int count;
bab_do_work(babcgpu);
K_RLOCK(babinfo->available_work);
count = babinfo->available_work->count;
K_RUNLOCK(babinfo->available_work);
if (count >= babinfo->chips)
cgsem_mswait(&(babinfo->scan_work), BAB_STD_DELAY_mS);
mutex_lock(&(babinfo->nonce_lock));
if (babinfo->new_nonces) {
hashcount += 0xffffffffull * babinfo->new_nonces;
babinfo->new_nonces = 0;
}
mutex_unlock(&(babinfo->nonce_lock));
return hashcount;
}
#define CHIPS_PER_STAT 16
#define FMT_RANGE "%d-%d"
static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
{
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
uint64_t history_good[BAB_MAXCHIPS], history_bad[BAB_MAXCHIPS];
uint64_t his_good_tot, his_bad_tot;
double history_elapsed[BAB_MAXCHIPS], diff;
bool elapsed_is_good[BAB_MAXCHIPS];
int speeds[BAB_CHIP_SPEEDS];
struct api_data *root = NULL;
char data[2048];
char buf[32];
int spi_work, chip_work, sp, chip, bank, chip_off, board, last_board;
int i, to, j, k;
bool dead;
struct timeval now;
double elapsed, ghs;
float ghs_sum, his_ghs_tot;
float tot, hw;
K_ITEM *item;
if (babinfo->initialised == false)
return NULL;
memset(&speeds, 0, sizeof(speeds));
root = api_add_int(root, "Version", &(babinfo->version), true);
root = api_add_int(root, "Chips", &(babinfo->chips), true);
root = api_add_int(root, "Boards", &(babinfo->boards), true);
root = api_add_int(root, "Banks", &(babinfo->banks), true);
data[0] = '\0';
for (i = 0; i <= BAB_MAXBANKS; i++) {
snprintf(buf, sizeof(buf), "%s%d",
(i == 0) ? "" : " ",
babinfo->chips_per_bank[i]);
strcat(data, buf);
}
root = api_add_string(root, "Chips Per Bank", data, true);
data[0] = '\0';
for (i = 0; i <= BAB_MAXBANKS; i++) {
snprintf(buf, sizeof(buf), "%s%d",
(i == 0) ? "" : " ",
babinfo->missing_chips_per_bank[i]);
strcat(data, buf);
}
root = api_add_string(root, "Missing Chips Per Bank", data, true);
cgtime(&now);
elapsed = tdiff(&now, &(babcgpu->dev_start_tv));
root = api_add_elapsed(root, "Device Elapsed", &elapsed, true);
root = api_add_string(root, "History Enabled",
#if UPDATE_HISTORY
"true",
#else
"false",
#endif
true);
int chs = HISTORY_TIME_S;
root = api_add_int(root, "Chip History Limit", &chs, true);
K_RLOCK(babinfo->nfree_list);
for (i = 0; i < babinfo->chips; i++) {
item = babinfo->good_nonces[i]->tail;
elapsed_is_good[i] = true;
if (!item)
history_elapsed[i] = 0;
else
history_elapsed[i] = tdiff(&now, &(DATAN(item)->found));
item = babinfo->bad_nonces[i]->tail;
if (item) {
diff = tdiff(&now, &(DATAN(item)->found));
if (history_elapsed[i] < diff) {
history_elapsed[i] = diff;
elapsed_is_good[i] = false;
}
}
history_good[i] = babinfo->good_nonces[i]->count;
history_bad[i] = babinfo->bad_nonces[i]->count;
}
K_RUNLOCK(babinfo->nfree_list);
his_ghs_tot = 0;
for (i = 0; i < babinfo->chips; i += CHIPS_PER_STAT) {
to = i + CHIPS_PER_STAT - 1;
if (to >= babinfo->chips)
to = babinfo->chips - 1;
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
babinfo->chip_nonces[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Nonces "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
babinfo->chip_good[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Good "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
babinfo->chip_bad[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Bad "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s0x%02x",
j == i ? "" : " ",
(int)(babinfo->chip_conf[j]));
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Conf "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%d",
j == i ? "" : " ",
(int)(babinfo->chip_fast[j]));
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Fast "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%d",
j == i ? "" : " ",
(int)(babinfo->chip_spie[j]));
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Spie "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%d",
j == i ? "" : " ",
(int)(babinfo->chip_miso[j]));
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Miso "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
tot = (float)(babinfo->chip_good[j] + babinfo->chip_bad[j]);
if (tot != 0)
hw = 100.0 * (float)(babinfo->chip_bad[j]) / tot;
else
hw = 0;
snprintf(buf, sizeof(buf),
"%s%.3f",
j == i ? "" : " ", hw);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "HW%% "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
ghs_sum = 0;
data[0] = '\0';
for (j = i; j <= to; j++) {
if (elapsed > 0) {
ghs = (double)(babinfo->chip_good[j]) * 0xffffffffull /
elapsed / 1000000000.0;
} else
ghs = 0;
snprintf(buf, sizeof(buf),
"%s%.3f",
j == i ? "" : " ", ghs);
strcat(data, buf);
ghs_sum += (float)ghs;
}
snprintf(buf, sizeof(buf), "GHs "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
snprintf(buf, sizeof(buf), "Sum GHs "FMT_RANGE, i, to);
root = api_add_avg(root, buf, &ghs_sum, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
babinfo->chip_cont_bad[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Cont-Bad "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
babinfo->chip_max_bad[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Max-Bad "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
history_good[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "History Good "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
history_bad[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "History Bad "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
tot = (float)(history_good[j] + history_bad[j]);
if (tot != 0)
hw = 100.0 * (float)(history_bad[j]) / tot;
else
hw = 0;
snprintf(buf, sizeof(buf),
"%s%.3f",
j == i ? "" : " ", hw);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "History HW%% "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
ghs_sum = 0;
data[0] = '\0';
for (j = i; j <= to; j++) {
if (history_elapsed[j] > 0) {
double num = history_good[j];
// exclude the first nonce?
if (elapsed_is_good[j])
num--;
ghs = num * 0xffffffffull /
history_elapsed[j] / 1000000000.0;
} else
ghs = 0;
snprintf(buf, sizeof(buf),
"%s%.3f",
j == i ? "" : " ", ghs);
strcat(data, buf);
ghs_sum += (float)ghs;
// Setup speed range data
for (sp = 0; sp < BAB_CHIP_SPEEDS - 1; sp++) {
if (ghs <= chip_speed_ranges[sp]) {
speeds[sp]++;
break;
}
}
if (sp >= (BAB_CHIP_SPEEDS - 1))
speeds[BAB_CHIP_SPEEDS - 1]++;
}
snprintf(buf, sizeof(buf), "History GHs "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
snprintf(buf, sizeof(buf), "Sum History GHs "FMT_RANGE, i, to);
root = api_add_avg(root, buf, &ghs_sum, true);
his_ghs_tot += ghs_sum;
}
root = api_add_avg(root, "Total History GHs", &his_ghs_tot, true);
his_good_tot = his_bad_tot = 0;
for (i = 0; i < babinfo->chips; i++) {
his_good_tot += history_good[i];
his_bad_tot += history_bad[i];
}
if (his_good_tot + his_bad_tot)
tot = 100.0 * (float)his_bad_tot / (float)(his_good_tot + his_bad_tot);
else
tot = 0.0;
root = api_add_avg(root, "Total History HW%", &tot, true);
for (sp = 0; sp < BAB_CHIP_SPEEDS; sp++) {
if (sp < (BAB_CHIP_SPEEDS - 1))
ghs = chip_speed_ranges[sp];
else
ghs = chip_speed_ranges[BAB_CHIP_SPEEDS - 2];
snprintf(buf, sizeof(buf), "History Speed %s%.1f %s",
(sp < (BAB_CHIP_SPEEDS - 1)) ? "" : ">",
ghs, chip_speed_names[sp]);
root = api_add_int(root, buf, &(speeds[sp]), true);
}
int len, str, siz = 1024;
char *tmp = malloc(siz);
if (!tmp)
quithere(1, "OOM tmp1");
for (sp = 0; sp < 2; sp++) {
tmp[0] = '\0';
len = 0;
for (i = 0; i < babinfo->chips; i++) {
if (history_elapsed[i] > 0) {
double num = history_good[i];
// exclude the first nonce?
if (elapsed_is_good[i])
num--;
ghs = num * 0xffffffffull /
history_elapsed[i] / 1000000000.0;
} else
ghs = 0;
if ((sp == 0 || ghs > chip_speed_ranges[sp-1]) &&
(ghs <= chip_speed_ranges[sp])) {
bank = babinfo->chip_bank[i];
chip_off = i;
for (j = 0; j < babinfo->chip_bank[i]; j++)
chip_off -= babinfo->chips_per_bank[j];
/*
* Bank/Board/Chip are all 1 based
* except V1 Bank = BAB_V1_BANK (0)
* If the bank has any missing chips then a "?"
* is placed after the board number
*/
snprintf(buf, sizeof(buf), "%s%d/%d%s/%d",
len ? " " : "", bank,
(int)(chip_off / BAB_BOARDCHIPS)+1,
babinfo->missing_chips_per_bank[bank] ?
"?" : "",
(chip_off % BAB_BOARDCHIPS)+1);
str = strlen(buf);
while ((len + str + 1) > siz) {
siz += 1024;
tmp = realloc(tmp, siz);
if (!tmp)
quithere(1, "OOM tmp2");
}
strcpy(tmp + len, buf);
len += str;
}
}
snprintf(buf, sizeof(buf), "History %s", chip_speed_names[sp]);
root = api_add_string(root, buf, len ? tmp : "None", true);
}
free(tmp);
tmp = NULL;
switch (babinfo->version) {
case 1:
i = j = BAB_V1_BANK;
break;
case 2:
i = 1;
j = BAB_MAXBANKS;
break;
}
data[0] = '\0';
for (bank = i; bank <= j; bank++) {
if (babinfo->bank_first_chip[bank] >= 0) {
chip = babinfo->bank_first_chip[bank];
to = babinfo->bank_last_chip[bank];
for (; chip <= to; chip += BAB_BOARDCHIPS) {
dead = true;
for (k = chip; (k <= to) && (k < (chip+BAB_BOARDCHIPS)); k++) {
if (history_elapsed[k] > 0) {
double num = history_good[k];
// exclude the first nonce?
if (elapsed_is_good[k])
num--;
ghs = num * 0xffffffffull /
history_elapsed[k] / 1000000000.0;
} else
ghs = 0;
if (ghs > 0.0) {
dead = false;
break;
}
}
if (dead) {
board = (int)((float)(chip - babinfo->bank_first_chip[bank]) /
BAB_BOARDCHIPS) + 1;
snprintf(buf, sizeof(buf),
"%s%d/%d%s",
data[0] ? " " : "",
bank, board,
babinfo->missing_chips_per_bank[bank] ?
"?" : "");
strcat(data, buf);
}
}
}
}
root = api_add_string(root, "History Dead Boards", data[0] ? data : "None", true);
data[0] = '\0';
for (bank = i; bank <= j; bank++) {
if (babinfo->bank_first_chip[bank] >= 0) {
to = babinfo->bank_first_chip[bank];
chip = babinfo->bank_last_chip[bank];
for (; chip >= to; chip--) {
dead = true;
if (history_elapsed[chip] > 0) {
double num = history_good[chip];
// exclude the first nonce?
if (elapsed_is_good[chip])
num--;
ghs = num * 0xffffffffull /
history_elapsed[chip] / 1000000000.0;
} else
ghs = 0;
if (ghs > 0.0)
break;
}
/*
* The output here is: a/b+c/d
* a/b is the SPI/board that starts the Dead Chain
* c is the number of boards after a
* d is the total number of chips in the Dead Chain
* A Dead Chain is a continous set of dead chips that
* finish at the end of an SPI chain of boards
* This might be caused by the first board, or the cables attached
* to the first board, in the Dead Chain i.e. a/b
* If c is zero, it's just the last board, so it's the same as any
* other board having dead chips
*/
if (chip < babinfo->bank_last_chip[bank]) {
board = (int)((float)(chip - babinfo->bank_first_chip[bank]) /
BAB_BOARDCHIPS) + 1;
last_board = (int)((float)(babinfo->bank_last_chip[bank] -
babinfo->bank_first_chip[bank]) /
BAB_BOARDCHIPS) + 1;
snprintf(buf, sizeof(buf),
"%s%d/%d%s+%d/%d",
data[0] ? " " : "",
bank, board,
babinfo->missing_chips_per_bank[bank] ?
"?" : "",
last_board - board,
babinfo->bank_last_chip[bank] - chip);
strcat(data, buf);
}
}
}
root = api_add_string(root, "History Dead Chains", data[0] ? data : "None", true);
root = api_add_int(root, "Disabled Chips", &(babinfo->total_disabled), true);
for (i = 0; i < BAB_NONCE_OFFSETS; i++) {
snprintf(buf, sizeof(buf), "Nonce Offset 0x%08x", bab_nonce_offsets[i]);
root = api_add_uint64(root, buf, &(babinfo->nonce_offset_count[i]), true);
}
root = api_add_uint64(root, "Discarded E0s", &(babinfo->discarded_e0s), true);
root = api_add_uint64(root, "Tested", &(babinfo->tested_nonces), true);
root = api_add_uint64(root, "OK", &(babinfo->ok_nonces), true);
root = api_add_uint64(root, "Total Tests", &(babinfo->total_tests), true);
root = api_add_uint64(root, "Max Tests", &(babinfo->max_tests_per_nonce), true);
float avg = babinfo->ok_nonces ? (float)(babinfo->total_tests) /
(float)(babinfo->ok_nonces) : 0;
root = api_add_avg(root, "Avg Tests", &avg, true);
root = api_add_uint64(root, "Untested", &(babinfo->untested_nonces), true);
root = api_add_uint64(root, "Work Links", &(babinfo->total_links), true);
root = api_add_uint64(root, "Work Processed Links", &(babinfo->total_proc_links), true);
root = api_add_uint64(root, "Max Links", &(babinfo->max_links), true);
root = api_add_uint64(root, "Max Processed Links", &(babinfo->max_proc_links), true);
root = api_add_uint64(root, "Total Work Links", &(babinfo->total_work_links), true);
avg = babinfo->ok_nonces ? (float)(babinfo->total_links) /
(float)(babinfo->ok_nonces) : 0;
root = api_add_avg(root, "Avg Links", &avg, true);
avg = babinfo->ok_nonces ? (float)(babinfo->total_proc_links) /
(float)(babinfo->ok_nonces) : 0;
root = api_add_avg(root, "Avg Proc Links", &avg, true);
avg = babinfo->ok_nonces ? (float)(babinfo->total_work_links) /
(float)(babinfo->ok_nonces) : 0;
root = api_add_avg(root, "Avg Work Links", &avg, true);
root = api_add_uint64(root, "Fail", &(babinfo->fail), true);
root = api_add_uint64(root, "Fail Total Tests", &(babinfo->fail_total_tests), true);
avg = babinfo->fail ? (float)(babinfo->fail_total_tests) /
(float)(babinfo->fail) : 0;
root = api_add_avg(root, "Fail Avg Tests", &avg, true);
root = api_add_uint64(root, "Fail Work Links", &(babinfo->fail_total_links), true);
root = api_add_uint64(root, "Fail Total Work Links", &(babinfo->fail_total_work_links), true);
root = api_add_uint32(root, "Initial Ignored", &(babinfo->initial_ignored), true);
root = api_add_uint64(root, "Ign Total Tests", &(babinfo->ign_total_tests), true);
root = api_add_uint64(root, "Ign Work Links", &(babinfo->ign_total_links), true);
root = api_add_uint64(root, "Ign Total Work Links", &(babinfo->ign_total_work_links), true);
chip_work = 0;
for (i = 0; i < babinfo->chips; i++)
chip_work += babinfo->chip_work[i]->count;
spi_work = babinfo->spi_list->count * babinfo->chips;
root = api_add_int(root, "WFree Total", &(babinfo->wfree_list->total), true);
root = api_add_int(root, "WFree Count", &(babinfo->wfree_list->count), true);
root = api_add_int(root, "Available Work", &(babinfo->available_work->count), true);
root = api_add_int(root, "SPI Work", &spi_work, true);
root = api_add_int(root, "Chip Work", &chip_work, true);
root = api_add_int(root, "SFree Total", &(babinfo->sfree_list->total), true);
root = api_add_int(root, "SFree Count", &(babinfo->sfree_list->count), true);
root = api_add_int(root, "SPI Waiting", &(babinfo->spi_list->count), true);
root = api_add_int(root, "SPI Sent", &(babinfo->spi_sent->count), true);
root = api_add_int(root, "RFree Total", &(babinfo->rfree_list->total), true);
root = api_add_int(root, "RFree Count", &(babinfo->rfree_list->count), true);
root = api_add_int(root, "Result Count", &(babinfo->res_list->count), true);
int used = babinfo->nfree_list->total - babinfo->nfree_list->count;
root = api_add_int(root, "NFree Total", &(babinfo->nfree_list->total), true);
root = api_add_int(root, "NFree Used", &used, true);
root = api_add_uint64(root, "Delay Count", &(babinfo->delay_count), true);
root = api_add_double(root, "Delay Min", &(babinfo->delay_min), true);
root = api_add_double(root, "Delay Max", &(babinfo->delay_max), true);
data[0] = '\0';
for (i = 0; i <= BAB_DELAY_BANDS; i++) {
snprintf(buf, sizeof(buf),
"%s<%.1f=%"PRIu64,
i == 0 ? "" : " ",
BAB_DELAY_BASE+(BAB_DELAY_STEP*i),
babinfo->delay_bands[i]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf),
" >=%.1f=%"PRIu64,
BAB_DELAY_BASE+BAB_DELAY_STEP*(BAB_DELAY_BANDS+1),
babinfo->delay_bands[BAB_DELAY_BANDS+1]);
strcat(data, buf);
root = api_add_string(root, "Delay Bands", data, true);
root = api_add_uint64(root, "Send Count", &(babinfo->send_count), true);
root = api_add_double(root, "Send Total", &(babinfo->send_total), true);
avg = babinfo->send_count ? (float)(babinfo->send_total) /
(float)(babinfo->send_count) : 0;
root = api_add_avg(root, "Send Avg", &avg, true);
root = api_add_double(root, "Send Min", &(babinfo->send_min), true);
root = api_add_double(root, "Send Max", &(babinfo->send_max), true);
root = api_add_int(root, "Reply Wait", &(babinfo->reply_wait), true);
root = api_add_uint64(root, "Reply Waits", &(babinfo->reply_waits), true);
root = api_add_uint64(root, "Work Unrolled", &(babinfo->work_unrolled), true);
root = api_add_uint64(root, "Work Rolled", &(babinfo->work_rolled), true);
i = (int)(babinfo->max_speed);
root = api_add_int(root, bab_options[0], &i, true);
i = (int)(babinfo->def_speed);
root = api_add_int(root, bab_options[1], &i, true);
i = (int)(babinfo->min_speed);
root = api_add_int(root, bab_options[2], &i, true);
root = api_add_double(root, bab_options[3], &(babinfo->tune_up), true);
root = api_add_double(root, bab_options[4], &(babinfo->tune_down), true);
i = (int)(babinfo->speed_hz);
root = api_add_int(root, bab_options[5], &i, true);
i = (int)(babinfo->delay_usecs);
root = api_add_int(root, bab_options[6], &i, true);
root = api_add_uint64(root, bab_options[7], &(babinfo->trf_delay), true);
return root;
}
static void bab_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info *babcgpu)
{
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
#if UPDATE_HISTORY
struct timeval now;
double elapsed;
int i, bad = 0;
cgtime(&now);
elapsed = tdiff(&now, &(babcgpu->dev_start_tv));
// At least get 15s of nonces before saying anything is bad
if (elapsed > 15.0) {
K_RLOCK(babinfo->nfree_list);
for (i = 0; i < babinfo->chips; i++) {
if (babinfo->good_nonces[i]->count == 0 &&
babinfo->bad_nonces[i]->count > 1)
bad++;
}
K_RUNLOCK(babinfo->nfree_list);
}
tailsprintf(buf, bufsiz, "%d.%02d.%03d B:%03d D:%03d",
babinfo->banks,
babinfo->boards,
babinfo->chips,
bad,
babinfo->total_disabled);
#else
tailsprintf(buf, bufsiz, "%d.%02d.%03d D:%03d",
babinfo->banks,
babinfo->boards,
babinfo->chips,
babinfo->total_disabled);
#endif
}
#endif
struct device_drv bab_drv = {
.drv_id = DRIVER_bab,
.dname = "BlackArrowBitFuryGPIO",
.name = "BaB",
.drv_detect = bab_detect,
#ifdef LINUX
.get_api_stats = bab_api_stats,
.get_statline_before = bab_get_statline_before,
.identify_device = bab_identify,
.thread_prepare = bab_thread_prepare,
.hash_work = hash_queued_work,
.scanwork = bab_scanwork,
.queue_full = bab_queue_full,
.flush_work = bab_flush_work,
.thread_shutdown = bab_shutdown
#endif
};