Hash :
567415b5
Author :
Date :
2014-07-02T04:13:51
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
/*
* Copyright 2013 Andrew Smith
* Copyright 2013-2014 Con Kolivas
*
* 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 <float.h>
#include <limits.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include <unistd.h>
#ifdef WIN32
#include <windows.h>
#endif
#include "compat.h"
#include "miner.h"
#include "usbutils.h"
#include "uthash.h"
#include "driver-bflsc.h"
int opt_bflsc_overheat = BFLSC_TEMP_OVERHEAT;
static const char *blank = "";
static enum driver_version drv_ver(struct cgpu_info *bflsc, const char *ver)
{
char *tmp;
if (strstr(ver, "1.0.0"))
return BFLSC_DRV1;
if (strstr(ver, "1.0.") || strstr(ver, "1.1.")) {
applog(LOG_WARNING, "%s detect (%s) Warning assuming firmware '%s' is Ver1",
bflsc->drv->dname, bflsc->device_path, ver);
return BFLSC_DRV1;
}
if (strstr(ver, "1.2."))
return BFLSC_DRV2;
tmp = str_text((char *)ver);
applog(LOG_WARNING, "%s detect (%s) Warning unknown firmware '%s' using Ver2",
bflsc->drv->dname, bflsc->device_path, tmp);
free(tmp);
return BFLSC_DRV2;
}
static void xlinkstr(char *xlink, size_t siz, int dev, struct bflsc_info *sc_info)
{
if (dev > 0)
snprintf(xlink, siz, " x-%d", dev);
else {
if (sc_info->sc_count > 1)
strcpy(xlink, " master");
else
*xlink = '\0';
}
}
static void bflsc_applog(struct cgpu_info *bflsc, int dev, enum usb_cmds cmd, int amount, int err)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char xlink[17];
xlinkstr(xlink, sizeof(xlink), dev, sc_info);
usb_applog(bflsc, cmd, xlink, amount, err);
}
// Break an input up into lines with LFs removed
// false means an error, but if *lines > 0 then data was also found
// error would be no data or missing LF at the end
static bool tolines(struct cgpu_info *bflsc, int dev, char *buf, int *lines, char ***items, enum usb_cmds cmd)
{
bool ok = false;
char *ptr;
#define p_lines (*lines)
#define p_items (*items)
p_lines = 0;
p_items = NULL;
if (!buf || !(*buf)) {
applog(LOG_DEBUG, "USB: %s%i: (%d) empty %s",
bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
return ok;
}
ptr = strdup(buf);
while (ptr && *ptr) {
p_items = realloc(p_items, ++p_lines * sizeof(*p_items));
if (unlikely(!p_items))
quit(1, "Failed to realloc p_items in tolines");
p_items[p_lines-1] = ptr;
ptr = strchr(ptr, '\n');
if (ptr)
*(ptr++) = '\0';
else {
applog(LOG_DEBUG, "USB: %s%i: (%d) missing lf(s) in %s",
bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
return ok;
}
}
ok = true;
return ok;
}
static void freetolines(int *lines, char ***items)
{
if (*lines > 0) {
free(**items);
free(*items);
}
*lines = 0;
*items = NULL;
}
enum breakmode {
NOCOLON,
ONECOLON,
ALLCOLON // Temperature uses this
};
// Break down a single line into 'fields'
// 'lf' will be a pointer to the final LF if it is there (or NULL)
// firstname will be the allocated buf copy pointer which is also
// the string before ':' for ONECOLON and ALLCOLON
// If any string is missing the ':' when it was expected, false is returned
static bool breakdown(enum breakmode mode, char *buf, int *count, char **firstname, char ***fields, char **lf)
{
char *ptr, *colon, *comma;
bool ok = false;
#define p_count (*count)
#define p_firstname (*firstname)
#define p_fields (*fields)
#define p_lf (*lf)
p_count = 0;
p_firstname = NULL;
p_fields = NULL;
p_lf = NULL;
if (!buf || !(*buf))
return ok;
ptr = p_firstname = strdup(buf);
p_lf = strchr(p_firstname, '\n');
if (mode == ONECOLON) {
colon = strchr(ptr, ':');
if (colon) {
ptr = colon;
*(ptr++) = '\0';
} else
return ok;
}
while (ptr && *ptr) {
if (mode == ALLCOLON) {
colon = strchr(ptr, ':');
if (colon)
ptr = colon + 1;
else
return ok;
}
comma = strchr(ptr, ',');
if (comma)
*(comma++) = '\0';
p_fields = realloc(p_fields, ++p_count * sizeof(*p_fields));
if (unlikely(!p_fields))
quit(1, "Failed to realloc p_fields in breakdown");
p_fields[p_count-1] = ptr;
ptr = comma;
}
ok = true;
return ok;
}
static void freebreakdown(int *count, char **firstname, char ***fields)
{
if (*firstname)
free(*firstname);
if (*count > 0)
free(*fields);
*count = 0;
*firstname = NULL;
*fields = NULL;
}
static bool isokerr(int err, char *buf, int amount)
{
if (err < 0 || amount < (int)BFLSC_OK_LEN)
return false;
else {
if (strstr(buf, BFLSC_ANERR)) {
applog(LOG_INFO, "BFLSC not ok err: %s", buf);
return false;
} else
return true;
}
}
// send+receive dual stage - always single line replies
static int send_recv_ds(struct cgpu_info *bflsc, int dev, int *stage, bool *sent, int *amount, char *send1, int send1_len, enum usb_cmds send1_cmd, enum usb_cmds recv1_cmd, char *send2, int send2_len, enum usb_cmds send2_cmd, enum usb_cmds recv2_cmd, char *recv, int recv_siz)
{
struct DataForwardToChain data;
int len, err, tried;
if (dev == 0) {
usb_buffer_clear(bflsc);
*stage = 1;
*sent = false;
err = usb_write(bflsc, send1, send1_len, amount, send1_cmd);
if (err < 0 || *amount < send1_len)
return err;
*sent = true;
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
if (!isokerr(err, recv, *amount))
return err;
usb_buffer_clear(bflsc);
*stage = 2;
*sent = false;
err = usb_write(bflsc, send2, send2_len, amount, send2_cmd);
if (err < 0 || *amount < send2_len)
return err;
*sent = true;
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
return err;
}
data.header = BFLSC_XLINKHDR;
data.deviceAddress = (uint8_t)dev;
tried = 0;
while (tried++ < 3) {
data.payloadSize = send1_len;
memcpy(data.payloadData, send1, send1_len);
len = DATAFORWARDSIZE(data);
usb_buffer_clear(bflsc);
*stage = 1;
*sent = false;
err = usb_write(bflsc, (char *)&data, len, amount, send1_cmd);
if (err < 0 || *amount < send1_len)
return err;
*sent = true;
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
if (err != LIBUSB_SUCCESS)
return err;
// x-link timeout? - try again?
if (strstr(recv, BFLSC_XTIMEOUT))
continue;
if (!isokerr(err, recv, *amount))
return err;
data.payloadSize = send2_len;
memcpy(data.payloadData, send2, send2_len);
len = DATAFORWARDSIZE(data);
usb_buffer_clear(bflsc);
*stage = 2;
*sent = false;
err = usb_write(bflsc, (char *)&data, len, amount, send2_cmd);
if (err < 0 || *amount < send2_len)
return err;
*sent = true;
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
if (err != LIBUSB_SUCCESS)
return err;
// x-link timeout? - try again?
if (strstr(recv, BFLSC_XTIMEOUT))
continue;
// SUCCESS - return it
break;
}
return err;
}
#define READ_OK true
#define READ_NL false
// send+receive single stage
static int send_recv_ss(struct cgpu_info *bflsc, int dev, bool *sent, int *amount, char *send, int send_len, enum usb_cmds send_cmd, char *recv, int recv_siz, enum usb_cmds recv_cmd, bool read_ok)
{
struct DataForwardToChain data;
int len, err, tried;
if (dev == 0) {
usb_buffer_clear(bflsc);
*sent = false;
err = usb_write(bflsc, send, send_len, amount, send_cmd);
if (err < 0 || *amount < send_len) {
// N.B. thus !(*sent) directly implies err < 0 or *amount < send_len
return err;
}
*sent = true;
if (read_ok == READ_OK)
err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
else
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
return err;
}
data.header = BFLSC_XLINKHDR;
data.deviceAddress = (uint8_t)dev;
data.payloadSize = send_len;
memcpy(data.payloadData, send, send_len);
len = DATAFORWARDSIZE(data);
tried = 0;
while (tried++ < 3) {
usb_buffer_clear(bflsc);
*sent = false;
err = usb_write(bflsc, (char *)&data, len, amount, recv_cmd);
if (err < 0 || *amount < send_len)
return err;
*sent = true;
if (read_ok == READ_OK)
err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
else
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_TIMEOUT)
return err;
// read_ok can err timeout if it's looking for OK<LF>
// TODO: add a usb_read() option to spot the ERR: and convert end=OK<LF> to just <LF>
// x-link timeout? - try again?
if ((err == LIBUSB_SUCCESS || (read_ok == READ_OK && err == LIBUSB_ERROR_TIMEOUT)) &&
strstr(recv, BFLSC_XTIMEOUT))
continue;
// SUCCESS or TIMEOUT - return it
break;
}
return err;
}
static int write_to_dev(struct cgpu_info *bflsc, int dev, char *buf, int buflen, int *amount, enum usb_cmds cmd)
{
struct DataForwardToChain data;
int len;
/*
* The protocol is syncronous so any previous excess can be
* discarded and assumed corrupt data or failed USB transfers
*/
usb_buffer_clear(bflsc);
if (dev == 0)
return usb_write(bflsc, buf, buflen, amount, cmd);
data.header = BFLSC_XLINKHDR;
data.deviceAddress = (uint8_t)dev;
data.payloadSize = buflen;
memcpy(data.payloadData, buf, buflen);
len = DATAFORWARDSIZE(data);
return usb_write(bflsc, (char *)&data, len, amount, cmd);
}
static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
{
char buf[BFLSC_BUFSIZ+1];
int err, amount;
bool sent;
// Device is gone
if (bflsc->usbinfo.nodev)
return;
mutex_lock(&bflsc->device_mutex);
err = send_recv_ss(bflsc, dev, &sent, &amount,
BFLSC_QFLUSH, BFLSC_QFLUSH_LEN, C_QUEFLUSH,
buf, sizeof(buf)-1, C_QUEFLUSHREPLY, READ_NL);
mutex_unlock(&bflsc->device_mutex);
if (!sent)
bflsc_applog(bflsc, dev, C_QUEFLUSH, amount, err);
else {
// TODO: do we care if we don't get 'OK'? (always will in normal processing)
}
}
/* return True = attempted usb_read_ok()
* set ignore to true means no applog/ignore errors */
static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore)
{
bool readok = false;
mutex_lock(&(bflsc->device_mutex));
*err = send_recv_ss(bflsc, dev, &readok, amount,
BFLSC_QRES, BFLSC_QRES_LEN, C_REQUESTRESULTS,
buf, bufsiz-1, C_GETRESULTS, READ_OK);
mutex_unlock(&(bflsc->device_mutex));
if (!readok) {
if (!ignore)
bflsc_applog(bflsc, dev, C_REQUESTRESULTS, *amount, *err);
// TODO: do what? flag as dead device?
// count how many times it has happened and reset/fail it
// or even make sure it is all x-link and that means device
// has failed after some limit of this?
// of course all other I/O must also be failing ...
} else {
if (*err < 0 || *amount < 1) {
if (!ignore)
bflsc_applog(bflsc, dev, C_GETRESULTS, *amount, *err);
// TODO: do what? ... see above
}
}
return readok;
}
static void __bflsc_initialise(struct cgpu_info *bflsc)
{
int err, interface;
// TODO: does x-link bypass the other device FTDI? (I think it does)
// So no initialisation required except for the master device?
if (bflsc->usbinfo.nodev)
return;
interface = usb_interface(bflsc);
// Reset
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
FTDI_VALUE_RESET, interface, C_RESET);
applog(LOG_DEBUG, "%s%i: reset got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
usb_ftdi_set_latency(bflsc);
if (bflsc->usbinfo.nodev)
return;
// Set data control
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
FTDI_VALUE_DATA_BAS, interface, C_SETDATA);
applog(LOG_DEBUG, "%s%i: setdata got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
// Set the baud
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BAS,
(FTDI_INDEX_BAUD_BAS & 0xff00) | interface,
C_SETBAUD);
applog(LOG_DEBUG, "%s%i: setbaud got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
// Set Flow Control
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
FTDI_VALUE_FLOW, interface, C_SETFLOW);
applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
// Set Modem Control
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
FTDI_VALUE_MODEM, interface, C_SETMODEM);
applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
// Clear any sent data
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
FTDI_VALUE_PURGE_TX, interface, C_PURGETX);
applog(LOG_DEBUG, "%s%i: purgetx got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
// Clear any received data
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
FTDI_VALUE_PURGE_RX, interface, C_PURGERX);
applog(LOG_DEBUG, "%s%i: purgerx got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (!bflsc->cutofftemp)
bflsc->cutofftemp = opt_bflsc_overheat;
}
static void bflsc_initialise(struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char buf[BFLSC_BUFSIZ+1];
int err, amount;
int dev;
mutex_lock(&(bflsc->device_mutex));
__bflsc_initialise(bflsc);
mutex_unlock(&(bflsc->device_mutex));
for (dev = 0; dev < sc_info->sc_count; dev++) {
bflsc_send_flush_work(bflsc, dev);
bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, true);
}
}
static bool getinfo(struct cgpu_info *bflsc, int dev)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct bflsc_dev sc_dev;
char buf[BFLSC_BUFSIZ+1];
int err, amount;
char **items, *firstname, **fields, *lf;
bool res, ok = false;
int i, lines, count;
char *tmp;
/*
* Kano's first dev Jalapeno output:
* DEVICE: BitFORCE SC<LF>
* FIRMWARE: 1.0.0<LF>
* ENGINES: 30<LF>
* FREQUENCY: [UNKNOWN]<LF>
* XLINK MODE: MASTER<LF>
* XLINK PRESENT: YES<LF>
* --DEVICES IN CHAIN: 0<LF>
* --CHAIN PRESENCE MASK: 00000000<LF>
* OK<LF>
*/
/*
* Don't use send_recv_ss() since we have a different receive timeout
* Also getinfo() is called multiple times if it fails anyway
*/
err = write_to_dev(bflsc, dev, BFLSC_DETAILS, BFLSC_DETAILS_LEN, &amount, C_REQUESTDETAILS);
if (err < 0 || amount != BFLSC_DETAILS_LEN) {
applog(LOG_ERR, "%s detect (%s) send details request failed (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
return ok;
}
err = usb_read_ok_timeout(bflsc, buf, sizeof(buf)-1, &amount,
BFLSC_INFO_TIMEOUT, C_GETDETAILS);
if (err < 0 || amount < 1) {
if (err < 0) {
applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
} else {
applog(LOG_ERR, "%s detect (%s) get details returned nothing (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
}
return ok;
}
memset(&sc_dev, 0, sizeof(struct bflsc_dev));
sc_info->sc_count = 1;
res = tolines(bflsc, dev, &(buf[0]), &lines, &items, C_GETDETAILS);
if (!res)
return ok;
tmp = str_text(buf);
strncpy(sc_dev.getinfo, tmp, sizeof(sc_dev.getinfo));
sc_dev.getinfo[sizeof(sc_dev.getinfo)-1] = '\0';
free(tmp);
for (i = 0; i < lines-2; i++) {
res = breakdown(ONECOLON, items[i], &count, &firstname, &fields, &lf);
if (lf)
*lf = '\0';
if (!res || count != 1) {
tmp = str_text(items[i]);
applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
"%s detect (%s) invalid details line: '%s' %d",
bflsc->drv->dname, bflsc->device_path, tmp, count);
free(tmp);
dev_error(bflsc, REASON_DEV_COMMS_ERROR);
goto mata;
}
if (strstr(firstname, BFLSC_DI_FIRMWARE)) {
sc_dev.firmware = strdup(fields[0]);
sc_info->driver_version = drv_ver(bflsc, sc_dev.firmware);
}
else if (strcasestr(firstname, BFLSC_DI_ENGINES)) {
sc_dev.engines = atoi(fields[0]);
if (sc_dev.engines < 1) {
tmp = str_text(items[i]);
applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
"%s detect (%s) invalid engine count: '%s'",
bflsc->drv->dname, bflsc->device_path, tmp);
free(tmp);
goto mata;
}
}
else if (strstr(firstname, BFLSC_DI_XLINKMODE))
sc_dev.xlink_mode = strdup(fields[0]);
else if (strstr(firstname, BFLSC_DI_XLINKPRESENT))
sc_dev.xlink_present = strdup(fields[0]);
else if (strstr(firstname, BFLSC_DI_DEVICESINCHAIN)) {
if (fields[0][0] == '0' ||
(fields[0][0] == ' ' && fields[0][1] == '0'))
sc_info->sc_count = 1;
else
sc_info->sc_count = atoi(fields[0]);
if (sc_info->sc_count < 1 || sc_info->sc_count > 30) {
tmp = str_text(items[i]);
applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
"%s detect (%s) invalid x-link count: '%s'",
bflsc->drv->dname, bflsc->device_path, tmp);
free(tmp);
goto mata;
}
}
else if (strstr(firstname, BFLSC_DI_CHIPS))
sc_dev.chips = strdup(fields[0]);
else if (strstr(firstname, BFLSC28_DI_ASICS))
sc_dev.chips = strdup(fields[0]);
freebreakdown(&count, &firstname, &fields);
}
if (sc_info->driver_version == BFLSC_DRVUNDEF) {
applog(LOG_WARNING, "%s detect (%s) missing %s",
bflsc->drv->dname, bflsc->device_path, BFLSC_DI_FIRMWARE);
goto ne;
}
sc_info->sc_devs = calloc(sc_info->sc_count, sizeof(struct bflsc_dev));
if (unlikely(!sc_info->sc_devs))
quit(1, "Failed to calloc in getinfo");
memcpy(&(sc_info->sc_devs[0]), &sc_dev, sizeof(sc_dev));
// TODO: do we care about getting this info for the rest if > 0 x-link
ok = true;
goto ne;
mata:
freebreakdown(&count, &firstname, &fields);
ok = false;
ne:
freetolines(&lines, &items);
return ok;
}
static bool bflsc28_queue_full(struct cgpu_info *bflsc);
static struct cgpu_info *bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
{
struct bflsc_info *sc_info = NULL;
char buf[BFLSC_BUFSIZ+1];
int i, err, amount;
struct timeval init_start, init_now;
int init_sleep, init_count;
bool ident_first, sent;
char *newname;
uint16_t latency;
struct cgpu_info *bflsc = usb_alloc_cgpu(&bflsc_drv, 1);
sc_info = calloc(1, sizeof(*sc_info));
if (unlikely(!sc_info))
quit(1, "Failed to calloc sc_info in bflsc_detect_one");
// TODO: fix ... everywhere ...
bflsc->device_data = (FILE *)sc_info;
if (!usb_init(bflsc, dev, found))
goto shin;
// Allow 2 complete attempts if the 1st time returns an unrecognised reply
ident_first = true;
retry:
init_count = 0;
init_sleep = REINIT_TIME_FIRST_MS;
cgtime(&init_start);
reinit:
__bflsc_initialise(bflsc);
err = send_recv_ss(bflsc, 0, &sent, &amount,
BFLSC_IDENTIFY, BFLSC_IDENTIFY_LEN, C_REQUESTIDENTIFY,
buf, sizeof(buf)-1, C_GETIDENTIFY, READ_NL);
if (!sent) {
applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
goto unshin;
}
if (err < 0 || amount < 1) {
init_count++;
cgtime(&init_now);
if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
if (init_count == 2) {
applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying",
bflsc->drv->dname, bflsc->device_path, amount, err);
}
cgsleep_ms(init_sleep);
if ((init_sleep * 2) <= REINIT_TIME_MAX_MS)
init_sleep *= 2;
goto reinit;
}
if (init_count > 0)
applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs",
bflsc->drv->dname, bflsc->device_path, init_count, tdiff(&init_now, &init_start));
if (err < 0) {
applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
} else {
applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
bflsc->drv->dname, bflsc->device_path, amount);
}
goto unshin;
}
buf[amount] = '\0';
if (unlikely(!strstr(buf, BFLSC_BFLSC) && !strstr(buf, BFLSC_BFLSC28))) {
applog(LOG_DEBUG, "%s detect (%s) found an FPGA '%s' ignoring",
bflsc->drv->dname, bflsc->device_path, buf);
goto unshin;
}
if (unlikely(strstr(buf, BFLSC_IDENTITY))) {
if (ident_first) {
applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' trying again ...",
bflsc->drv->dname, bflsc->device_path, buf);
ident_first = false;
goto retry;
}
applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' on 2nd attempt",
bflsc->drv->dname, bflsc->device_path, buf);
goto unshin;
}
int tries = 0;
while (7734) {
if (getinfo(bflsc, 0))
break;
// N.B. we will get displayed errors each time it fails
if (++tries > 2)
goto unshin;
cgsleep_ms(40);
}
switch (sc_info->driver_version) {
case BFLSC_DRV1:
sc_info->que_size = BFLSC_QUE_SIZE_V1;
sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V1;
sc_info->que_watermark = BFLSC_QUE_WATERMARK_V1;
sc_info->que_low = BFLSC_QUE_LOW_V1;
sc_info->que_noncecount = QUE_NONCECOUNT_V1;
sc_info->que_fld_min = QUE_FLD_MIN_V1;
sc_info->que_fld_max = QUE_FLD_MAX_V1;
// Only Jalapeno uses 1.0.0
sc_info->flush_size = 1;
break;
case BFLSC_DRV2:
case BFLSC_DRVUNDEF:
default:
sc_info->driver_version = BFLSC_DRV2;
sc_info->que_size = BFLSC_QUE_SIZE_V2;
sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V2;
sc_info->que_watermark = BFLSC_QUE_WATERMARK_V2;
sc_info->que_low = BFLSC_QUE_LOW_V2;
sc_info->que_noncecount = QUE_NONCECOUNT_V2;
sc_info->que_fld_min = QUE_FLD_MIN_V2;
sc_info->que_fld_max = QUE_FLD_MAX_V2;
// TODO: this can be reduced to total chip count
sc_info->flush_size = 16 * sc_info->sc_count;
break;
}
// Set parallelization based on the getinfo() response if it is present
if (sc_info->sc_devs[0].chips && strlen(sc_info->sc_devs[0].chips)) {
if (strstr(sc_info->sc_devs[0].chips, BFLSC_DI_CHIPS_PARALLEL)) {
sc_info->que_noncecount = QUE_NONCECOUNT_V2;
sc_info->que_fld_min = QUE_FLD_MIN_V2;
sc_info->que_fld_max = QUE_FLD_MAX_V2;
} else {
sc_info->que_noncecount = QUE_NONCECOUNT_V1;
sc_info->que_fld_min = QUE_FLD_MIN_V1;
sc_info->que_fld_max = QUE_FLD_MAX_V1;
}
}
sc_info->scan_sleep_time = BAS_SCAN_TIME;
sc_info->results_sleep_time = BFLSC_RES_TIME;
sc_info->default_ms_work = BAS_WORK_TIME;
latency = BAS_LATENCY;
/* When getinfo() "FREQUENCY: [UNKNOWN]" is fixed -
* use 'freq * engines' to estimate.
* Otherwise for now: */
newname = NULL;
if (sc_info->sc_count > 1) {
newname = BFLSC_MINIRIG;
sc_info->scan_sleep_time = BAM_SCAN_TIME;
sc_info->default_ms_work = BAM_WORK_TIME;
bflsc->usbdev->ident = IDENT_BAM;
latency = BAM_LATENCY;
} else {
if (sc_info->sc_devs[0].engines < 34) { // 16 * 2 + 2
newname = BFLSC_JALAPENO;
sc_info->scan_sleep_time = BAJ_SCAN_TIME;
sc_info->default_ms_work = BAJ_WORK_TIME;
bflsc->usbdev->ident = IDENT_BAJ;
latency = BAJ_LATENCY;
} else if (sc_info->sc_devs[0].engines < 130) { // 16 * 8 + 2
newname = BFLSC_LITTLESINGLE;
sc_info->scan_sleep_time = BAL_SCAN_TIME;
sc_info->default_ms_work = BAL_WORK_TIME;
bflsc->usbdev->ident = IDENT_BAL;
latency = BAL_LATENCY;
}
}
sc_info->ident = usb_ident(bflsc);
if (sc_info->ident == IDENT_BMA) {
bflsc->drv->queue_full = &bflsc28_queue_full;
sc_info->scan_sleep_time = BMA_SCAN_TIME;
sc_info->default_ms_work = BMA_WORK_TIME;
sc_info->results_sleep_time = BMA_RES_TIME;
}
if (latency != bflsc->usbdev->found->latency) {
bflsc->usbdev->found->latency = latency;
usb_ftdi_set_latency(bflsc);
}
for (i = 0; i < sc_info->sc_count; i++)
sc_info->sc_devs[i].ms_work = sc_info->default_ms_work;
if (newname) {
if (!bflsc->drv->copy)
bflsc->drv = copy_drv(bflsc->drv);
bflsc->drv->name = newname;
}
// We have a real BFLSC!
applog(LOG_DEBUG, "%s (%s) identified as: '%s'",
bflsc->drv->dname, bflsc->device_path, bflsc->drv->name);
if (!add_cgpu(bflsc))
goto unshin;
update_usb_stats(bflsc);
mutex_init(&bflsc->device_mutex);
rwlock_init(&sc_info->stat_lock);
return bflsc;
unshin:
usb_uninit(bflsc);
shin:
free(bflsc->device_data);
bflsc->device_data = NULL;
if (bflsc->name != blank) {
free(bflsc->name);
bflsc->name = NULL;
}
bflsc = usb_free_cgpu(bflsc);
return NULL;
}
static void bflsc_detect(bool __maybe_unused hotplug)
{
usb_detect(&bflsc_drv, bflsc_detect_one);
}
static void get_bflsc_statline_before(char *buf, size_t bufsiz, struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
float temp = 0;
float vcc2 = 0;
int i;
rd_lock(&(sc_info->stat_lock));
for (i = 0; i < sc_info->sc_count; i++) {
if (sc_info->sc_devs[i].temp1 > temp)
temp = sc_info->sc_devs[i].temp1;
if (sc_info->sc_devs[i].temp2 > temp)
temp = sc_info->sc_devs[i].temp2;
if (sc_info->sc_devs[i].vcc2 > vcc2)
vcc2 = sc_info->sc_devs[i].vcc2;
}
rd_unlock(&(sc_info->stat_lock));
tailsprintf(buf, bufsiz, "max%3.0fC %4.2fV", temp, vcc2);
}
static void flush_one_dev(struct cgpu_info *bflsc, int dev)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct work *work, *tmp;
bool did = false;
bflsc_send_flush_work(bflsc, dev);
rd_lock(&bflsc->qlock);
HASH_ITER(hh, bflsc->queued_work, work, tmp) {
if (work->subid == dev) {
// devflag is used to flag stale work
work->devflag = true;
did = true;
}
}
rd_unlock(&bflsc->qlock);
if (did) {
wr_lock(&(sc_info->stat_lock));
sc_info->sc_devs[dev].flushed = true;
sc_info->sc_devs[dev].flush_id = sc_info->sc_devs[dev].result_id;
sc_info->sc_devs[dev].work_queued = 0;
wr_unlock(&(sc_info->stat_lock));
}
}
static void bflsc_flush_work(struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
int dev;
for (dev = 0; dev < sc_info->sc_count; dev++)
flush_one_dev(bflsc, dev);
}
static void bflsc_set_volt(struct cgpu_info *bflsc, int dev)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char buf[BFLSC_BUFSIZ+1];
char msg[16];
int err, amount;
bool sent;
// Device is gone
if (bflsc->usbinfo.nodev)
return;
snprintf(msg, sizeof(msg), "V%dX", sc_info->volt_next);
mutex_lock(&bflsc->device_mutex);
err = send_recv_ss(bflsc, dev, &sent, &amount,
msg, strlen(msg), C_SETVOLT,
buf, sizeof(buf)-1, C_REPLYSETVOLT, READ_NL);
mutex_unlock(&(bflsc->device_mutex));
if (!sent)
bflsc_applog(bflsc, dev, C_SETVOLT, amount, err);
else {
// Don't care
}
sc_info->volt_next_stat = false;
return;
}
static void bflsc_set_clock(struct cgpu_info *bflsc, int dev)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char buf[BFLSC_BUFSIZ+1];
char msg[16];
int err, amount;
bool sent;
// Device is gone
if (bflsc->usbinfo.nodev)
return;
snprintf(msg, sizeof(msg), "F%dX", sc_info->clock_next);
mutex_lock(&bflsc->device_mutex);
err = send_recv_ss(bflsc, dev, &sent, &amount,
msg, strlen(msg), C_SETCLOCK,
buf, sizeof(buf)-1, C_REPLYSETCLOCK, READ_NL);
mutex_unlock(&(bflsc->device_mutex));
if (!sent)
bflsc_applog(bflsc, dev, C_SETCLOCK, amount, err);
else {
// Don't care
}
sc_info->clock_next_stat = false;
return;
}
static void bflsc_flash_led(struct cgpu_info *bflsc, int dev)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char buf[BFLSC_BUFSIZ+1];
int err, amount;
bool sent;
// Device is gone
if (bflsc->usbinfo.nodev)
return;
// It is not critical flashing the led so don't get stuck if we
// can't grab the mutex now
if (mutex_trylock(&bflsc->device_mutex))
return;
err = send_recv_ss(bflsc, dev, &sent, &amount,
BFLSC_FLASH, BFLSC_FLASH_LEN, C_REQUESTFLASH,
buf, sizeof(buf)-1, C_FLASHREPLY, READ_NL);
mutex_unlock(&(bflsc->device_mutex));
if (!sent)
bflsc_applog(bflsc, dev, C_REQUESTFLASH, amount, err);
else {
// Don't care
}
// Once we've tried - don't do it until told to again
// - even if it failed
sc_info->flash_led = false;
return;
}
/* Flush and stop all work if the device reaches the thermal cutoff temp, or
* temporarily stop queueing work if it's in the throttling range. */
static void bflsc_manage_temp(struct cgpu_info *bflsc, struct bflsc_dev *sc_dev,
int dev, float temp)
{
bflsc->temp = temp;
if (bflsc->cutofftemp > 0) {
int cutoff = bflsc->cutofftemp;
int throttle = cutoff - BFLSC_TEMP_THROTTLE;
int recover = cutoff - BFLSC_TEMP_RECOVER;
if (sc_dev->overheat) {
if (temp < recover)
sc_dev->overheat = false;
} else if (temp > throttle) {
sc_dev->overheat = true;
if (temp > cutoff) {
applog(LOG_WARNING, "%s%i: temp (%.1f) hit thermal cutoff limit %d, stopping work!",
bflsc->drv->name, bflsc->device_id, temp, cutoff);
dev_error(bflsc, REASON_DEV_THERMAL_CUTOFF);
flush_one_dev(bflsc, dev);
} else {
applog(LOG_NOTICE, "%s%i: temp (%.1f) hit thermal throttle limit %d, throttling",
bflsc->drv->name, bflsc->device_id, temp, throttle);
}
}
}
}
static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct bflsc_dev *sc_dev;
char temp_buf[BFLSC_BUFSIZ+1];
char volt_buf[BFLSC_BUFSIZ+1];
char *tmp;
int err, amount;
char *firstname, **fields, *lf;
char xlink[17];
int count;
bool res, sent;
float temp, temp1, temp2;
float vcc1, vcc2, vmain;
// Device is gone
if (bflsc->usbinfo.nodev)
return false;
if (dev >= sc_info->sc_count) {
applog(LOG_ERR, "%s%i: temp invalid xlink device %d - limit %d",
bflsc->drv->name, bflsc->device_id, dev, sc_info->sc_count - 1);
return false;
}
if (sc_info->volt_next_stat || sc_info->clock_next_stat) {
if (sc_info->volt_next_stat)
bflsc_set_volt(bflsc, dev);
if (sc_info->clock_next_stat)
bflsc_set_clock(bflsc, dev);
return true;
}
// Flash instead of Temp
if (sc_info->flash_led) {
bflsc_flash_led(bflsc, dev);
return true;
}
xlinkstr(xlink, sizeof(xlink), dev, sc_info);
/* It is not very critical getting temp so don't get stuck if we
* can't grab the mutex here */
if (mutex_trylock(&bflsc->device_mutex))
return false;
err = send_recv_ss(bflsc, dev, &sent, &amount,
BFLSC_TEMPERATURE, BFLSC_TEMPERATURE_LEN, C_REQUESTTEMPERATURE,
temp_buf, sizeof(temp_buf)-1, C_GETTEMPERATURE, READ_NL);
mutex_unlock(&(bflsc->device_mutex));
if (!sent) {
applog(LOG_ERR, "%s%i: Error: Request%s temp invalid/timed out (%d:%d)",
bflsc->drv->name, bflsc->device_id, xlink, amount, err);
return false;
} else {
if (err < 0 || amount < 1) {
if (err < 0) {
applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)",
bflsc->drv->name, bflsc->device_id, xlink, amount, err);
} else {
applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)",
bflsc->drv->name, bflsc->device_id, xlink, amount, err);
}
return false;
}
}
// Ignore it if we can't get the V
if (mutex_trylock(&bflsc->device_mutex))
return false;
err = send_recv_ss(bflsc, dev, &sent, &amount,
BFLSC_VOLTAGE, BFLSC_VOLTAGE_LEN, C_REQUESTVOLTS,
volt_buf, sizeof(volt_buf)-1, C_GETVOLTS, READ_NL);
mutex_unlock(&(bflsc->device_mutex));
if (!sent) {
applog(LOG_ERR, "%s%i: Error: Request%s volts invalid/timed out (%d:%d)",
bflsc->drv->name, bflsc->device_id, xlink, amount, err);
return false;
} else {
if (err < 0 || amount < 1) {
if (err < 0) {
applog(LOG_ERR, "%s%i: Error: Get%s volt return invalid/timed out (%d:%d)",
bflsc->drv->name, bflsc->device_id, xlink, amount, err);
} else {
applog(LOG_ERR, "%s%i: Error: Get%s volt returned nothing (%d:%d)",
bflsc->drv->name, bflsc->device_id, xlink, amount, err);
}
return false;
}
}
res = breakdown(ALLCOLON, temp_buf, &count, &firstname, &fields, &lf);
if (lf)
*lf = '\0';
if (!res || count < 2 || !lf) {
tmp = str_text(temp_buf);
applog(LOG_WARNING, "%s%i: Invalid%s temp reply: '%s'",
bflsc->drv->name, bflsc->device_id, xlink, tmp);
free(tmp);
freebreakdown(&count, &firstname, &fields);
dev_error(bflsc, REASON_DEV_COMMS_ERROR);
return false;
}
temp = temp1 = (float)atoi(fields[0]);
temp2 = (float)atoi(fields[1]);
freebreakdown(&count, &firstname, &fields);
res = breakdown(NOCOLON, volt_buf, &count, &firstname, &fields, &lf);
if (lf)
*lf = '\0';
if (!res || count != 3 || !lf) {
tmp = str_text(volt_buf);
applog(LOG_WARNING, "%s%i: Invalid%s volt reply: '%s'",
bflsc->drv->name, bflsc->device_id, xlink, tmp);
free(tmp);
freebreakdown(&count, &firstname, &fields);
dev_error(bflsc, REASON_DEV_COMMS_ERROR);
return false;
}
sc_dev = &sc_info->sc_devs[dev];
vcc1 = (float)atoi(fields[0]) / 1000.0;
vcc2 = (float)atoi(fields[1]) / 1000.0;
vmain = (float)atoi(fields[2]) / 1000.0;
freebreakdown(&count, &firstname, &fields);
if (vcc1 > 0 || vcc2 > 0 || vmain > 0) {
wr_lock(&(sc_info->stat_lock));
if (vcc1 > 0) {
if (unlikely(sc_dev->vcc1 == 0))
sc_dev->vcc1 = vcc1;
else {
sc_dev->vcc1 += vcc1 * 0.63;
sc_dev->vcc1 /= 1.63;
}
}
if (vcc2 > 0) {
if (unlikely(sc_dev->vcc2 == 0))
sc_dev->vcc2 = vcc2;
else {
sc_dev->vcc2 += vcc2 * 0.63;
sc_dev->vcc2 /= 1.63;
}
}
if (vmain > 0) {
if (unlikely(sc_dev->vmain == 0))
sc_dev->vmain = vmain;
else {
sc_dev->vmain += vmain * 0.63;
sc_dev->vmain /= 1.63;
}
}
wr_unlock(&(sc_info->stat_lock));
}
if (temp1 > 0 || temp2 > 0) {
wr_lock(&(sc_info->stat_lock));
if (unlikely(!sc_dev->temp1))
sc_dev->temp1 = temp1;
else {
sc_dev->temp1 += temp1 * 0.63;
sc_dev->temp1 /= 1.63;
}
if (unlikely(!sc_dev->temp2))
sc_dev->temp2 = temp2;
else {
sc_dev->temp2 += temp2 * 0.63;
sc_dev->temp2 /= 1.63;
}
if (temp1 > sc_dev->temp1_max) {
sc_dev->temp1_max = temp1;
sc_dev->temp1_max_time = time(NULL);
}
if (temp2 > sc_dev->temp2_max) {
sc_dev->temp2_max = temp2;
sc_dev->temp2_max_time = time(NULL);
}
if (unlikely(sc_dev->temp1_5min_av == 0))
sc_dev->temp1_5min_av = temp1;
else {
sc_dev->temp1_5min_av += temp1 * .0042;
sc_dev->temp1_5min_av /= 1.0042;
}
if (unlikely(sc_dev->temp2_5min_av == 0))
sc_dev->temp2_5min_av = temp2;
else {
sc_dev->temp2_5min_av += temp2 * .0042;
sc_dev->temp2_5min_av /= 1.0042;
}
wr_unlock(&(sc_info->stat_lock));
if (temp < temp2)
temp = temp2;
bflsc_manage_temp(bflsc, sc_dev, dev, temp);
}
return true;
}
static void inc_core_errors(struct bflsc_info *info, int8_t core)
{
if (info->ident == IDENT_BMA) {
if (core >= 0)
info->cortex_hw[core]++;
} else {
if (core >= 0 && core < 16)
info->core_hw[core]++;
}
}
static void inc_bflsc_errors(struct thr_info *thr, struct bflsc_info *info, int8_t core)
{
inc_hw_errors(thr);
inc_core_errors(info, core);
}
static void inc_bflsc_nonces(struct bflsc_info *info, int8_t core)
{
if (info->ident == IDENT_BMA) {
if (core >= 0)
info->cortex_nonces[core]++;
} else {
if (core >= 0 && core < 16)
info->core_nonces[core]++;
}
}
struct work *bflsc_work_by_uid(struct cgpu_info *bflsc, struct bflsc_info *sc_info, int id)
{
struct bflsc_work *bwork;
struct work *work = NULL;
wr_lock(&bflsc->qlock);
HASH_FIND_INT(sc_info->bworks, &id, bwork);
if (likely(bwork)) {
HASH_DEL(sc_info->bworks, bwork);
work = bwork->work;
free(bwork);
}
wr_unlock(&bflsc->qlock);
return work;
}
static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char *data, int count, char **fields, int *nonces)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct thr_info *thr = bflsc->thr[0];
struct work *work;
int8_t core = -1;
uint32_t nonce;
int i, num, x;
char *tmp;
bool res;
if (count < sc_info->que_fld_min) {
tmp = str_text(data);
applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
"%s%i:%s work returned too small (%d,%s)",
bflsc->drv->name, bflsc->device_id, xlink, count, tmp);
free(tmp);
inc_bflsc_errors(thr, sc_info, core);
return;
}
if (sc_info->ident == IDENT_BMA) {
unsigned int ucore;
if (sscanf(fields[QUE_CC], "%x", &ucore) == 1)
core = ucore;
} else if (sc_info->que_noncecount != QUE_NONCECOUNT_V1) {
unsigned int ucore;
if (sscanf(fields[QUE_CHIP_V2], "%x", &ucore) == 1)
core = ucore;
}
if (count > sc_info->que_fld_max) {
applog(LOG_INFO, "%s%i:%s work returned too large (%d) processing %d anyway",
bflsc->drv->name, bflsc->device_id, xlink, count, sc_info->que_fld_max);
count = sc_info->que_fld_max;
inc_bflsc_errors(thr, sc_info, core);
}
num = atoi(fields[sc_info->que_noncecount]);
if (num != count - sc_info->que_fld_min) {
tmp = str_text(data);
applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
"%s%i:%s incorrect data count (%d) will use %d instead from (%s)",
bflsc->drv->name, bflsc->device_id, xlink, num,
count - sc_info->que_fld_max, tmp);
free(tmp);
inc_bflsc_errors(thr, sc_info, core);
}
if (sc_info->ident == IDENT_BMA) {
int uid;
if (sscanf(fields[QUE_UID], "%04x", &uid) == 1)
work = bflsc_work_by_uid(bflsc, sc_info, uid);
} else {
char midstate[MIDSTATE_BYTES] = {}, blockdata[MERKLE_BYTES] = {};
if (!hex2bin((unsigned char *)midstate, fields[QUE_MIDSTATE], MIDSTATE_BYTES) ||
!hex2bin((unsigned char *)blockdata, fields[QUE_BLOCKDATA], MERKLE_BYTES)) {
applog(LOG_INFO, "%s%i:%s Failed to convert binary data to hex result - ignored",
bflsc->drv->name, bflsc->device_id, xlink);
inc_bflsc_errors(thr, sc_info, core);
return;
}
work = take_queued_work_bymidstate(bflsc, midstate, MIDSTATE_BYTES,
blockdata, MERKLE_OFFSET, MERKLE_BYTES);
}
if (!work) {
if (sc_info->not_first_work) {
applog(LOG_INFO, "%s%i:%s failed to find nonce work - can't be processed - ignored",
bflsc->drv->name, bflsc->device_id, xlink);
inc_bflsc_errors(thr, sc_info, core);
}
return;
}
res = false;
x = 0;
for (i = sc_info->que_fld_min; i < count; i++) {
if (strlen(fields[i]) != 8) {
tmp = str_text(data);
applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
"%s%i:%s invalid nonce (%s) will try to process anyway",
bflsc->drv->name, bflsc->device_id, xlink, tmp);
free(tmp);
}
hex2bin((void*)&nonce, fields[i], 4);
nonce = htobe32(nonce);
res = submit_nonce(thr, work, nonce);
if (res) {
wr_lock(&(sc_info->stat_lock));
sc_info->sc_devs[dev].nonces_found++;
wr_unlock(&(sc_info->stat_lock));
(*nonces)++;
x++;
inc_bflsc_nonces(sc_info, core);
} else
inc_core_errors(sc_info, core);
}
wr_lock(&(sc_info->stat_lock));
if (res)
sc_info->sc_devs[dev].result_id++;
if (x > QUE_MAX_RESULTS)
x = QUE_MAX_RESULTS + 1;
(sc_info->result_size[x])++;
sc_info->sc_devs[dev].work_complete++;
sc_info->sc_devs[dev].hashes_unsent += FULLNONCE;
// If not flushed (stale)
if (!(work->devflag))
sc_info->sc_devs[dev].work_queued -= 1;
wr_unlock(&(sc_info->stat_lock));
free_work(work);
}
static int process_results(struct cgpu_info *bflsc, int dev, char *pbuf, int *nonces, int *in_process)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char **items, *firstname, **fields, *lf;
int que = 0, i, lines, count;
char *tmp, *tmp2, *buf;
char xlink[17];
bool res;
*nonces = 0;
*in_process = 0;
xlinkstr(xlink, sizeof(xlink), dev, sc_info);
buf = strdupa(pbuf);
if (!strncmp(buf, "INPROCESS", 9))
sscanf(buf, "INPROCESS:%d\n%s", in_process, pbuf);
res = tolines(bflsc, dev, buf, &lines, &items, C_GETRESULTS);
if (!res || lines < 1) {
tmp = str_text(pbuf);
applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
"%s%i:%s empty result (%s) ignored",
bflsc->drv->name, bflsc->device_id, xlink, tmp);
free(tmp);
goto arigatou;
}
if (lines < QUE_RES_LINES_MIN) {
tmp = str_text(pbuf);
applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
"%s%i:%s result of %d too small (%s) ignored",
bflsc->drv->name, bflsc->device_id, xlink, lines, tmp);
free(tmp);
goto arigatou;
}
breakdown(ONECOLON, items[1], &count, &firstname, &fields, &lf);
if (count < 1) {
tmp = str_text(pbuf);
tmp2 = str_text(items[1]);
applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
"%s%i:%s empty result count (%s) in (%s) ignoring",
bflsc->drv->name, bflsc->device_id, xlink, tmp2, tmp);
free(tmp2);
free(tmp);
goto arigatou;
} else if (count != 1) {
tmp = str_text(pbuf);
tmp2 = str_text(items[1]);
applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
"%s%i:%s incorrect result count %d (%s) in (%s) will try anyway",
bflsc->drv->name, bflsc->device_id, xlink, count, tmp2, tmp);
free(tmp2);
free(tmp);
}
que = atoi(fields[0]);
if (que != (lines - QUE_RES_LINES_MIN)) {
i = que;
// 1+ In case the last line isn't 'OK' - try to process it
que = 1 + lines - QUE_RES_LINES_MIN;
tmp = str_text(pbuf);
tmp2 = str_text(items[0]);
applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
"%s%i:%s incorrect result count %d (%s) will try %d (%s)",
bflsc->drv->name, bflsc->device_id, xlink, i, tmp2, que, tmp);
free(tmp2);
free(tmp);
}
freebreakdown(&count, &firstname, &fields);
for (i = 0; i < que; i++) {
res = breakdown(NOCOLON, items[i + QUE_RES_LINES_MIN - 1], &count, &firstname, &fields, &lf);
if (likely(res))
process_nonces(bflsc, dev, &(xlink[0]), items[i], count, fields, nonces);
else
applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
"%s%i:%s failed to process nonce %s",
bflsc->drv->name, bflsc->device_id, xlink, items[i]);
freebreakdown(&count, &firstname, &fields);
sc_info->not_first_work = true;
}
arigatou:
freetolines(&lines, &items);
return que;
}
#define TVF(tv) ((float)((tv)->tv_sec) + ((float)((tv)->tv_usec) / 1000000.0))
#define TVFMS(tv) (TVF(tv) * 1000.0)
// Thread to simply keep looking for results
static void *bflsc_get_results(void *userdata)
{
struct cgpu_info *bflsc = (struct cgpu_info *)userdata;
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct timeval elapsed, now;
float oldest, f;
char buf[BFLSC_BUFSIZ+1];
int err, amount;
int i, que, dev, nonces;
bool readok;
cgtime(&now);
for (i = 0; i < sc_info->sc_count; i++) {
copy_time(&(sc_info->sc_devs[i].last_check_result), &now);
copy_time(&(sc_info->sc_devs[i].last_dev_result), &now);
copy_time(&(sc_info->sc_devs[i].last_nonce_result), &now);
}
while (sc_info->shutdown == false) {
cgtimer_t ts_start;
int in_process;
if (bflsc->usbinfo.nodev)
return NULL;
dev = -1;
oldest = FLT_MAX;
cgtime(&now);
// Find the first oldest ... that also needs checking
for (i = 0; i < sc_info->sc_count; i++) {
timersub(&now, &(sc_info->sc_devs[i].last_check_result), &elapsed);
f = TVFMS(&elapsed);
if (f < oldest && f >= sc_info->sc_devs[i].ms_work) {
f = oldest;
dev = i;
}
}
if (bflsc->usbinfo.nodev)
return NULL;
cgsleep_prepare_r(&ts_start);
if (dev == -1)
goto utsura;
cgtime(&(sc_info->sc_devs[dev].last_check_result));
readok = bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, false);
if (err < 0 || (!readok && amount != BFLSC_QRES_LEN) || (readok && amount < 1)) {
// TODO: do what else?
} else {
que = process_results(bflsc, dev, buf, &nonces, &in_process);
sc_info->not_first_work = true; // in case it failed processing it
if (que > 0)
cgtime(&(sc_info->sc_devs[dev].last_dev_result));
if (nonces > 0)
cgtime(&(sc_info->sc_devs[dev].last_nonce_result));
/* There are more results queued so do not sleep */
if (in_process)
continue;
// TODO: if not getting results ... reinit?
}
utsura:
cgsleep_ms_r(&ts_start, sc_info->results_sleep_time);
}
return NULL;
}
static bool bflsc_thread_prepare(struct thr_info *thr)
{
struct cgpu_info *bflsc = thr->cgpu;
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
if (thr_info_create(&(sc_info->results_thr), NULL, bflsc_get_results, (void *)bflsc)) {
applog(LOG_ERR, "%s%i: thread create failed", bflsc->drv->name, bflsc->device_id);
return false;
}
pthread_detach(sc_info->results_thr.pth);
return true;
}
static void bflsc_shutdown(struct thr_info *thr)
{
struct cgpu_info *bflsc = thr->cgpu;
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
bflsc_flush_work(bflsc);
sc_info->shutdown = true;
}
static void bflsc_thread_enable(struct thr_info *thr)
{
struct cgpu_info *bflsc = thr->cgpu;
if (bflsc->usbinfo.nodev)
return;
bflsc_initialise(bflsc);
}
static bool bflsc_send_work(struct cgpu_info *bflsc, int dev, bool mandatory)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct FullNonceRangeJob data;
char buf[BFLSC_BUFSIZ+1];
bool sent, ret = false;
struct work *work;
int err, amount;
int len, try;
int stage;
// Device is gone
if (bflsc->usbinfo.nodev)
return false;
// TODO: handle this everywhere
if (sc_info->sc_devs[dev].overheat == true)
return false;
// Initially code only deals with sending one work item
data.payloadSize = BFLSC_JOBSIZ;
data.endOfBlock = BFLSC_EOB;
len = sizeof(struct FullNonceRangeJob);
/* On faster devices we have a lot of lock contention so only
* mandatorily grab the lock and send work if the queue is empty since
* we have a submit queue. */
if (mandatory)
mutex_lock(&(bflsc->device_mutex));
else {
if (mutex_trylock(&bflsc->device_mutex))
return ret;
}
work = get_queued(bflsc);
if (unlikely(!work)) {
mutex_unlock(&bflsc->device_mutex);
return ret;
}
memcpy(data.midState, work->midstate, MIDSTATE_BYTES);
memcpy(data.blockData, work->data + MERKLE_OFFSET, MERKLE_BYTES);
try = 0;
re_send:
err = send_recv_ds(bflsc, dev, &stage, &sent, &amount,
BFLSC_QJOB, BFLSC_QJOB_LEN, C_REQUESTQUEJOB, C_REQUESTQUEJOBSTATUS,
(char *)&data, len, C_QUEJOB, C_QUEJOBSTATUS,
buf, sizeof(buf)-1);
mutex_unlock(&(bflsc->device_mutex));
switch (stage) {
case 1:
if (!sent) {
bflsc_applog(bflsc, dev, C_REQUESTQUEJOB, amount, err);
goto out;
} else {
// TODO: handle other errors ...
// Try twice
if (try++ < 1 && amount > 1 &&
strstr(buf, BFLSC_TIMEOUT))
goto re_send;
bflsc_applog(bflsc, dev, C_REQUESTQUEJOBSTATUS, amount, err);
goto out;
}
break;
case 2:
if (!sent) {
bflsc_applog(bflsc, dev, C_QUEJOB, amount, err);
goto out;
} else {
if (!isokerr(err, buf, amount)) {
// TODO: check for QUEUE FULL and set work_queued to sc_info->que_size
// and report a code bug LOG_ERR - coz it should never happen
// TODO: handle other errors ...
// Try twice
if (try++ < 1 && amount > 1 &&
strstr(buf, BFLSC_TIMEOUT))
goto re_send;
bflsc_applog(bflsc, dev, C_QUEJOBSTATUS, amount, err);
goto out;
}
}
break;
}
wr_lock(&(sc_info->stat_lock));
sc_info->sc_devs[dev].work_queued++;
wr_unlock(&(sc_info->stat_lock));
work->subid = dev;
ret = true;
out:
if (unlikely(!ret))
work_completed(bflsc, work);
return ret;
}
#define JP_COMMAND 0
#define JP_STREAMLENGTH 2
#define JP_SIGNATURE 4
#define JP_JOBSINARRY 5
#define JP_JOBSARRY 6
#define JP_ARRAYSIZE 45
static bool bflsc28_queue_full(struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = bflsc->device_data;
int created, queued = 0, create, i, offset;
struct work *base_work, *work, *works[10];
char *buf, *field, *ptr;
bool sent, ret = false;
uint16_t *streamlen;
uint8_t *job_pack;
int err, amount;
job_pack = alloca(2 + // Command
2 + // StreamLength
1 + // Signature
1 + // JobsInArray
JP_ARRAYSIZE * 10 +// Array of up to 10 Job Structs
1 // EndOfWrapper
);
if (bflsc->usbinfo.nodev)
return true;
wr_lock(&bflsc->qlock);
base_work = __get_queued(bflsc);
if (likely(base_work))
__work_completed(bflsc, base_work);
wr_unlock(&bflsc->qlock);
if (unlikely(!base_work))
return ret;
created = 1;
create = 9;
if (base_work->drv_rolllimit < create)
create = base_work->drv_rolllimit;
works[0] = base_work;
for (i = 1; i <= create ; i++) {
created++;
work = make_clone(base_work);
roll_work(base_work);
works[i] = work;
}
memcpy(job_pack, "WX", 2);
streamlen = (uint16_t *)&job_pack[JP_STREAMLENGTH];
*streamlen = created * JP_ARRAYSIZE + 7;
job_pack[JP_SIGNATURE] = 0xc1;
job_pack[JP_JOBSINARRY] = created;
offset = JP_JOBSARRY;
/* Create the maximum number of work items we can queue by nrolling one */
for (i = 0; i < created; i++) {
work = works[i];
memcpy(job_pack + offset, work->midstate, MIDSTATE_BYTES);
offset += MIDSTATE_BYTES;
memcpy(job_pack + offset, work->data + MERKLE_OFFSET, MERKLE_BYTES);
offset += MERKLE_BYTES;
job_pack[offset] = 0xaa; // EndOfBlock signature
offset++;
}
job_pack[offset++] = 0xfe; // EndOfWrapper
buf = alloca(BFLSC_BUFSIZ + 1);
mutex_lock(&bflsc->device_mutex);
err = send_recv_ss(bflsc, 0, &sent, &amount, (char *)job_pack, offset,
C_REQUESTQUEJOB, buf, BFLSC_BUFSIZ, C_REQUESTQUEJOBSTATUS, READ_NL);
mutex_unlock(&bflsc->device_mutex);
if (!isokerr(err, buf, amount)) {
if (!strncasecmp(buf, "ERR:QUEUE FULL", 14)) {
applog(LOG_DEBUG, "%s%d: Queue full",
bflsc->drv->name, bflsc->device_id);
ret = true;
} else {
applog(LOG_WARNING, "%s%d: Queue response not ok %s",
bflsc->drv->name, bflsc->device_id, buf);
}
goto out;
}
ptr = alloca(strlen(buf));
if (sscanf(buf, "OK:QUEUED %d:%s", &queued, ptr) != 2) {
applog(LOG_WARNING, "%s%d: Failed to parse queue response %s",
bflsc->drv->name, bflsc->device_id, buf);
goto out;
}
if (queued < 1 || queued > 10) {
applog(LOG_WARNING, "%s%d: Invalid queued count %d",
bflsc->drv->name, bflsc->device_id, queued);
queued = 0;
goto out;
}
for (i = 0; i < queued; i++) {
struct bflsc_work *bwork, *oldbwork;
unsigned int uid;
work = works[i];
field = strsep(&ptr, ",");
if (!field) {
applog(LOG_WARNING, "%s%d: Ran out of queued IDs after %d of %d",
bflsc->drv->name, bflsc->device_id, i, queued);
queued = i - 1;
goto out;
}
sscanf(field, "%04x", &uid);
bwork = calloc(sizeof(struct bflsc_work), 1);
bwork->id = uid;
bwork->work = work;
wr_lock(&bflsc->qlock);
HASH_REPLACE_INT(sc_info->bworks, id, bwork, oldbwork);
if (oldbwork) {
free_work(oldbwork->work);
free(oldbwork);
}
wr_unlock(&bflsc->qlock);
sc_info->sc_devs[0].work_queued++;
}
if (queued < created)
ret = true;
out:
for (i = queued; i < created; i++) {
work = works[i];
discard_work(work);
}
return ret;
}
static bool bflsc_queue_full(struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
int i, dev, tried, que;
bool ret = false;
int tries = 0;
tried = -1;
// if something is wrong with a device try the next one available
// TODO: try them all? Add an unavailable flag to sc_devs[i] init to 0 here first
while (++tries < 3) {
bool mandatory = false;
// Device is gone - shouldn't normally get here
if (bflsc->usbinfo.nodev) {
ret = true;
break;
}
dev = -1;
rd_lock(&(sc_info->stat_lock));
// Anything waiting - gets the work first
for (i = 0; i < sc_info->sc_count; i++) {
// TODO: and ignore x-link dead - once I work out how to decide it is dead
if (i != tried && sc_info->sc_devs[i].work_queued == 0 &&
!sc_info->sc_devs[i].overheat) {
dev = i;
break;
}
}
if (dev == -1) {
que = sc_info->que_size * 10; // 10x is certainly above the MAX it could be
// The first device with the smallest amount queued
for (i = 0; i < sc_info->sc_count; i++) {
if (i != tried && sc_info->sc_devs[i].work_queued < que &&
!sc_info->sc_devs[i].overheat) {
dev = i;
que = sc_info->sc_devs[i].work_queued;
}
}
if (que > sc_info->que_full_enough)
dev = -1;
else if (que < sc_info->que_low)
mandatory = true;
}
rd_unlock(&(sc_info->stat_lock));
// nothing needs work yet
if (dev == -1) {
ret = true;
break;
}
if (bflsc_send_work(bflsc, dev, mandatory))
break;
else
tried = dev;
}
return ret;
}
static int64_t bflsc_scanwork(struct thr_info *thr)
{
struct cgpu_info *bflsc = thr->cgpu;
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
int64_t ret, unsent;
bool flushed, cleanup;
struct work *work, *tmp;
int dev, waited, i;
// Device is gone
if (bflsc->usbinfo.nodev)
return -1;
flushed = false;
// Single lock check if any are flagged as flushed
rd_lock(&(sc_info->stat_lock));
for (dev = 0; dev < sc_info->sc_count; dev++)
flushed |= sc_info->sc_devs[dev].flushed;
rd_unlock(&(sc_info->stat_lock));
// > 0 flagged as flushed
if (flushed) {
// TODO: something like this ......
for (dev = 0; dev < sc_info->sc_count; dev++) {
cleanup = false;
// Is there any flushed work that can be removed?
rd_lock(&(sc_info->stat_lock));
if (sc_info->sc_devs[dev].flushed) {
if (sc_info->sc_devs[dev].result_id > (sc_info->sc_devs[dev].flush_id + sc_info->flush_size))
cleanup = true;
}
rd_unlock(&(sc_info->stat_lock));
// yes remove the flushed work that can be removed
if (cleanup) {
wr_lock(&bflsc->qlock);
HASH_ITER(hh, bflsc->queued_work, work, tmp) {
if (work->devflag && work->subid == dev) {
bflsc->queued_count--;
HASH_DEL(bflsc->queued_work, work);
discard_work(work);
}
}
wr_unlock(&bflsc->qlock);
wr_lock(&(sc_info->stat_lock));
sc_info->sc_devs[dev].flushed = false;
wr_unlock(&(sc_info->stat_lock));
}
}
}
waited = restart_wait(thr, sc_info->scan_sleep_time);
if (waited == ETIMEDOUT && sc_info->ident != IDENT_BMA) {
unsigned int old_sleep_time, new_sleep_time = 0;
int min_queued = sc_info->que_size;
/* Only adjust the scan_sleep_time if we did not receive a
* restart message while waiting. Try to adjust sleep time
* so we drop to sc_info->que_watermark before getting more work.
*/
rd_lock(&sc_info->stat_lock);
old_sleep_time = sc_info->scan_sleep_time;
for (i = 0; i < sc_info->sc_count; i++) {
if (sc_info->sc_devs[i].work_queued < min_queued)
min_queued = sc_info->sc_devs[i].work_queued;
}
rd_unlock(&sc_info->stat_lock);
new_sleep_time = old_sleep_time;
/* Increase slowly but decrease quickly */
if (min_queued > sc_info->que_full_enough && old_sleep_time < BFLSC_MAX_SLEEP)
new_sleep_time = old_sleep_time * 21 / 20;
else if (min_queued < sc_info->que_low)
new_sleep_time = old_sleep_time * 2 / 3;
/* Do not sleep more than BFLSC_MAX_SLEEP so we can always
* report in at least 2 results per 5s log interval. */
if (new_sleep_time != old_sleep_time) {
if (new_sleep_time > BFLSC_MAX_SLEEP)
new_sleep_time = BFLSC_MAX_SLEEP;
else if (new_sleep_time == 0)
new_sleep_time = 1;
applog(LOG_DEBUG, "%s%i: Changed scan sleep time to %d",
bflsc->drv->name, bflsc->device_id, new_sleep_time);
wr_lock(&sc_info->stat_lock);
sc_info->scan_sleep_time = new_sleep_time;
wr_unlock(&sc_info->stat_lock);
}
}
// Count up the work done since we last were here
ret = 0;
wr_lock(&(sc_info->stat_lock));
for (dev = 0; dev < sc_info->sc_count; dev++) {
unsent = sc_info->sc_devs[dev].hashes_unsent;
sc_info->sc_devs[dev].hashes_unsent = 0;
sc_info->sc_devs[dev].hashes_sent += unsent;
sc_info->hashes_sent += unsent;
ret += unsent;
}
wr_unlock(&(sc_info->stat_lock));
return ret;
}
#define BFLSC_OVER_TEMP 75
/* Set the fanspeed to auto for any valid value <= BFLSC_OVER_TEMP,
* or max for any value > BFLSC_OVER_TEMP or if we don't know the temperature. */
static void bflsc_set_fanspeed(struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)bflsc->device_data;
char buf[BFLSC_BUFSIZ+1];
char data[16+1];
int amount;
bool sent;
if ((bflsc->temp <= BFLSC_OVER_TEMP && bflsc->temp > 0 && sc_info->fanauto) ||
((bflsc->temp > BFLSC_OVER_TEMP || !bflsc->temp) && !sc_info->fanauto))
return;
if (bflsc->temp > BFLSC_OVER_TEMP || !bflsc->temp) {
strcpy(data, BFLSC_FAN4);
sc_info->fanauto = false;
} else {
strcpy(data, BFLSC_FANAUTO);
sc_info->fanauto = true;
}
applog(LOG_DEBUG, "%s%i: temp=%.0f over=%d set fan to %s",
bflsc->drv->name, bflsc->device_id, bflsc->temp,
BFLSC_OVER_TEMP, data);
mutex_lock(&bflsc->device_mutex);
send_recv_ss(bflsc, 0, &sent, &amount,
data, strlen(data), C_SETFAN,
buf, sizeof(buf)-1, C_FANREPLY, READ_NL);
mutex_unlock(&bflsc->device_mutex);
}
static bool bflsc_get_stats(struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
bool allok = true;
int i;
// Device is gone
if (bflsc->usbinfo.nodev)
return false;
for (i = 0; i < sc_info->sc_count; i++) {
if (!bflsc_get_temp(bflsc, i))
allok = false;
// Device is gone
if (bflsc->usbinfo.nodev)
return false;
if (i < (sc_info->sc_count - 1))
cgsleep_ms(BFLSC_TEMP_SLEEPMS);
}
bflsc_set_fanspeed(bflsc);
return allok;
}
static char *bflsc_set(struct cgpu_info *bflsc, char *option, char *setting, char *replybuf)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
int val;
if (sc_info->ident != IDENT_BMA) {
strcpy(replybuf, "no set options available");
return replybuf;
}
if (strcasecmp(option, "help") == 0) {
sprintf(replybuf, "volt: range 0-9 clock: range 0-15");
return replybuf;
}
if (strcasecmp(option, "volt") == 0) {
if (!setting || !*setting) {
sprintf(replybuf, "missing volt setting");
return replybuf;
}
val = atoi(setting);
if (val < 0 || val > 9) {
sprintf(replybuf, "invalid volt: '%s' valid range 0-9",
setting);
}
sc_info->volt_next = val;
sc_info->volt_next_stat = true;
return NULL;
}
if (strcasecmp(option, "clock") == 0) {
if (!setting || !*setting) {
sprintf(replybuf, "missing clock setting");
return replybuf;
}
val = atoi(setting);
if (val < 0 || val > 15) {
sprintf(replybuf, "invalid clock: '%s' valid range 0-15",
setting);
}
sc_info->clock_next = val;
sc_info->clock_next_stat = true;
return NULL;
}
sprintf(replybuf, "Unknown option: %s", option);
return replybuf;
}
static void bflsc_identify(struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
// TODO: handle x-link
sc_info->flash_led = true;
}
static bool bflsc_thread_init(struct thr_info *thr)
{
struct cgpu_info *bflsc = thr->cgpu;
if (bflsc->usbinfo.nodev)
return false;
bflsc_initialise(bflsc);
return true;
}
// there should be a new API function to return device info that isn't the standard stuff
// instead of bflsc_api_stats - since the stats should really just be internal code info
// and the new one should be UNusual device stats/extra details - like the stuff below
static struct api_data *bflsc_api_stats(struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct api_data *root = NULL;
char data[4096];
char buf[256];
int i, j, off;
size_t len;
//if no x-link ... etc
rd_lock(&(sc_info->stat_lock));
root = api_add_temp(root, "Temp1", &(sc_info->sc_devs[0].temp1), true);
root = api_add_temp(root, "Temp2", &(sc_info->sc_devs[0].temp2), true);
root = api_add_volts(root, "Vcc1", &(sc_info->sc_devs[0].vcc1), true);
root = api_add_volts(root, "Vcc2", &(sc_info->sc_devs[0].vcc2), true);
root = api_add_volts(root, "Vmain", &(sc_info->sc_devs[0].vmain), true);
root = api_add_temp(root, "Temp1 Max", &(sc_info->sc_devs[0].temp1_max), true);
root = api_add_temp(root, "Temp2 Max", &(sc_info->sc_devs[0].temp2_max), true);
root = api_add_time(root, "Temp1 Max Time", &(sc_info->sc_devs[0].temp1_max_time), true);
root = api_add_time(root, "Temp2 Max Time", &(sc_info->sc_devs[0].temp2_max_time), true);
root = api_add_int(root, "Work Queued", &(sc_info->sc_devs[0].work_queued), true);
root = api_add_int(root, "Work Complete", &(sc_info->sc_devs[0].work_complete), true);
root = api_add_bool(root, "Overheat", &(sc_info->sc_devs[0].overheat), true);
root = api_add_uint64(root, "Flush ID", &(sc_info->sc_devs[0].flush_id), true);
root = api_add_uint64(root, "Result ID", &(sc_info->sc_devs[0].result_id), true);
root = api_add_bool(root, "Flushed", &(sc_info->sc_devs[0].flushed), true);
root = api_add_uint(root, "Scan Sleep", &(sc_info->scan_sleep_time), true);
root = api_add_uint(root, "Results Sleep", &(sc_info->results_sleep_time), true);
root = api_add_uint(root, "Work ms", &(sc_info->default_ms_work), true);
buf[0] = '\0';
for (i = 0; i <= QUE_MAX_RESULTS + 1; i++)
tailsprintf(buf, sizeof(buf), "%s%"PRIu64, (i > 0) ? "/" : "", sc_info->result_size[i]);
root = api_add_string(root, "Result Size", buf, true);
rd_unlock(&(sc_info->stat_lock));
i = (int)(sc_info->driver_version);
root = api_add_int(root, "Driver", &i, true);
root = api_add_string(root, "Firmware", sc_info->sc_devs[0].firmware, false);
root = api_add_string(root, "Chips", sc_info->sc_devs[0].chips, false);
root = api_add_int(root, "Que Size", &(sc_info->que_size), false);
root = api_add_int(root, "Que Full", &(sc_info->que_full_enough), false);
root = api_add_int(root, "Que Watermark", &(sc_info->que_watermark), false);
root = api_add_int(root, "Que Low", &(sc_info->que_low), false);
root = api_add_escape(root, "GetInfo", sc_info->sc_devs[0].getinfo, false);
/*
else a whole lot of something like these ... etc
root = api_add_temp(root, "X-%d-Temp1", &(sc_info->temp1), false);
root = api_add_temp(root, "X-%d-Temp2", &(sc_info->temp2), false);
root = api_add_volts(root, "X-%d-Vcc1", &(sc_info->vcc1), false);
root = api_add_volts(root, "X-%d-Vcc2", &(sc_info->vcc2), false);
root = api_add_volts(root, "X-%d-Vmain", &(sc_info->vmain), false);
*/
if (sc_info->ident == IDENT_BMA) {
for (i = 0; i < 128; i += 16) {
data[0] = '\0';
off = 0;
for (j = 0; j < 16; j++) {
len = snprintf(data+off, sizeof(data)-off,
"%s%"PRIu64,
j > 0 ? " " : "",
sc_info->cortex_nonces[i+j]);
if (len >= (sizeof(data)-off))
off = sizeof(data)-1;
else {
if (len > 0)
off += len;
}
}
sprintf(buf, "Cortex %02x-%02x Nonces", i, i+15);
root = api_add_string(root, buf, data, true);
}
for (i = 0; i < 128; i += 16) {
data[0] = '\0';
off = 0;
for (j = 0; j < 16; j++) {
len = snprintf(data+off, sizeof(data)-off,
"%s%"PRIu64,
j > 0 ? " " : "",
sc_info->cortex_hw[i+j]);
if (len >= (sizeof(data)-off))
off = sizeof(data)-1;
else {
if (len > 0)
off += len;
}
}
sprintf(buf, "Cortex %02x-%02x HW Errors", i, i+15);
root = api_add_string(root, buf, data, true);
}
} else if (sc_info->que_noncecount != QUE_NONCECOUNT_V1) {
for (i = 0; i < 16; i++) {
sprintf(buf, "Core%d Nonces", i);
root = api_add_uint64(root, buf, &sc_info->core_nonces[i], false);
}
for (i = 0; i < 16; i++) {
sprintf(buf, "Core%d HW Errors", i);
root = api_add_uint64(root, buf, &sc_info->core_hw[i], false);
}
}
return root;
}
struct device_drv bflsc_drv = {
.drv_id = DRIVER_bflsc,
.dname = "BitForceSC",
.name = BFLSC_SINGLE,
.drv_detect = bflsc_detect,
.get_api_stats = bflsc_api_stats,
.get_statline_before = get_bflsc_statline_before,
.get_stats = bflsc_get_stats,
.set_device = bflsc_set,
.identify_device = bflsc_identify,
.thread_prepare = bflsc_thread_prepare,
.thread_init = bflsc_thread_init,
.hash_work = hash_queued_work,
.scanwork = bflsc_scanwork,
.queue_full = bflsc_queue_full,
.flush_work = bflsc_flush_work,
.thread_shutdown = bflsc_shutdown,
.thread_enable = bflsc_thread_enable
};