Hash :
dd9233f7
Author :
Date :
2013-08-13T17:24:01
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
/*
* Copyright 2013 Con Kolivas <kernel@kolivas.org>
* Copyright 2012-2013 Xiangfu <xiangfu@openmobilefree.com>
* Copyright 2012 Luke Dashjr
* Copyright 2012 Andrew Smith
*
* 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 <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#ifndef WIN32
#include <sys/select.h>
#include <termios.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#else
#include "compat.h"
#include <windows.h>
#include <io.h>
#endif
#include "elist.h"
#include "miner.h"
#include "usbutils.h"
#include "driver-avalon.h"
#include "hexdump.c"
#include "util.h"
int opt_avalon_temp = AVALON_TEMP_TARGET;
int opt_avalon_overheat = AVALON_TEMP_OVERHEAT;
int opt_avalon_fan_min = AVALON_DEFAULT_FAN_MIN_PWM;
int opt_avalon_fan_max = AVALON_DEFAULT_FAN_MAX_PWM;
int opt_avalon_freq_min = AVALON_MIN_FREQUENCY;
int opt_avalon_freq_max = AVALON_MAX_FREQUENCY;
int opt_bitburner_core_voltage = BITBURNER_DEFAULT_CORE_VOLTAGE;
bool opt_avalon_auto;
static int option_offset = -1;
struct device_drv avalon_drv;
static int avalon_init_task(struct avalon_task *at,
uint8_t reset, uint8_t ff, uint8_t fan,
uint8_t timeout, uint8_t asic_num,
uint8_t miner_num, uint8_t nonce_elf,
uint8_t gate_miner, int frequency)
{
uint16_t *lefreq16;
uint8_t *buf;
static bool first = true;
if (unlikely(!at))
return -1;
if (unlikely(timeout <= 0 || asic_num <= 0 || miner_num <= 0))
return -1;
memset(at, 0, sizeof(struct avalon_task));
if (unlikely(reset)) {
at->reset = 1;
at->fan_eft = 1;
at->timer_eft = 1;
first = true;
}
at->flush_fifo = (ff ? 1 : 0);
at->fan_eft = (fan ? 1 : 0);
if (unlikely(first && !at->reset)) {
at->fan_eft = 1;
at->timer_eft = 1;
first = false;
}
at->fan_pwm_data = (fan ? fan : AVALON_DEFAULT_FAN_MAX_PWM);
at->timeout_data = timeout;
at->asic_num = asic_num;
at->miner_num = miner_num;
at->nonce_elf = nonce_elf;
at->gate_miner_elf = 1;
at->asic_pll = 1;
if (unlikely(gate_miner)) {
at-> gate_miner = 1;
at->asic_pll = 0;
}
buf = (uint8_t *)at;
buf[5] = 0x00;
buf[8] = 0x74;
buf[9] = 0x01;
buf[10] = 0x00;
buf[11] = 0x00;
lefreq16 = (uint16_t *)&buf[6];
*lefreq16 = htole16(frequency * 8);
return 0;
}
static inline void avalon_create_task(struct avalon_task *at,
struct work *work)
{
memcpy(at->midstate, work->midstate, 32);
memcpy(at->data, work->data + 64, 12);
}
static int avalon_write(struct cgpu_info *avalon, char *buf, ssize_t len, int ep)
{
int err, amount;
err = usb_write(avalon, buf, len, &amount, ep);
applog(LOG_DEBUG, "%s%i: usb_write got err %d", avalon->drv->name,
avalon->device_id, err);
if (unlikely(err != 0)) {
applog(LOG_WARNING, "usb_write error on avalon_write");
return AVA_SEND_ERROR;
}
if (amount != len) {
applog(LOG_WARNING, "usb_write length mismatch on avalon_write");
return AVA_SEND_ERROR;
}
return AVA_SEND_OK;
}
static int avalon_send_task(const struct avalon_task *at, struct cgpu_info *avalon)
{
uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
int delay, ret, i, ep = C_AVALON_TASK;
struct avalon_info *info;
uint32_t nonce_range;
size_t nr_len;
if (at->nonce_elf)
nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
else
nr_len = AVALON_WRITE_SIZE;
memcpy(buf, at, AVALON_WRITE_SIZE);
if (at->nonce_elf) {
nonce_range = (uint32_t)0xffffffff / at->asic_num;
for (i = 0; i < at->asic_num; i++) {
buf[AVALON_WRITE_SIZE + (i * 4) + 3] =
(i * nonce_range & 0xff000000) >> 24;
buf[AVALON_WRITE_SIZE + (i * 4) + 2] =
(i * nonce_range & 0x00ff0000) >> 16;
buf[AVALON_WRITE_SIZE + (i * 4) + 1] =
(i * nonce_range & 0x0000ff00) >> 8;
buf[AVALON_WRITE_SIZE + (i * 4) + 0] =
(i * nonce_range & 0x000000ff) >> 0;
}
}
#if defined(__BIG_ENDIAN__) || defined(MIPSEB)
uint8_t tt = 0;
tt = (buf[0] & 0x0f) << 4;
tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
buf[0] = tt;
tt = (buf[4] & 0x0f) << 4;
tt |= ((buf[4] & 0x10) ? (1 << 3) : 0);
tt |= ((buf[4] & 0x20) ? (1 << 2) : 0);
tt |= ((buf[4] & 0x40) ? (1 << 1) : 0);
tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
buf[4] = tt;
#endif
info = avalon->device_data;
delay = nr_len * 10 * 1000000;
delay = delay / info->baud;
if (at->reset) {
ep = C_AVALON_RESET;
nr_len = 1;
}
if (opt_debug) {
applog(LOG_DEBUG, "Avalon: Sent(%u):", (unsigned int)nr_len);
hexdump(buf, nr_len);
}
ret = avalon_write(avalon, (char *)buf, nr_len, ep);
delay += 4000;
nusleep(delay);
applog(LOG_DEBUG, "Avalon: Sent: Buffer delay: %dus", delay);
return ret;
}
static bool avalon_decode_nonce(struct thr_info *thr, struct cgpu_info *avalon,
struct avalon_info *info, struct avalon_result *ar,
struct work *work)
{
uint32_t nonce;
info = avalon->device_data;
info->matching_work[work->subid]++;
nonce = htole32(ar->nonce);
applog(LOG_DEBUG, "Avalon: nonce = %0x08x", nonce);
return submit_nonce(thr, work, nonce);
}
/* Wait until the ftdi chip returns a CTS saying we can send more data. */
static void wait_avalon_ready(struct cgpu_info *avalon)
{
while (avalon_buffer_full(avalon)) {
nmsleep(40);
}
}
#define AVALON_CTS (1 << 4)
static inline bool avalon_cts(char c)
{
return (c & AVALON_CTS);
}
static int avalon_read(struct cgpu_info *avalon, unsigned char *buf,
size_t bufsize, int timeout, int ep)
{
size_t total = 0, readsize = bufsize + 2;
char readbuf[AVALON_READBUF_SIZE];
int err, amount, ofs = 2, cp;
err = usb_read_once_timeout(avalon, readbuf, readsize, &amount, timeout, ep);
applog(LOG_DEBUG, "%s%i: Get avalon read got err %d",
avalon->drv->name, avalon->device_id, err);
if (amount < 2)
goto out;
/* The first 2 of every 64 bytes are status on FTDIRL */
while (amount > 2) {
cp = amount - 2;
if (cp > 62)
cp = 62;
memcpy(&buf[total], &readbuf[ofs], cp);
total += cp;
amount -= cp + 2;
ofs += 64;
}
out:
return total;
}
static int avalon_reset(struct cgpu_info *avalon, bool initial)
{
struct avalon_result ar;
int ret, i, spare;
struct avalon_task at;
uint8_t *buf, *tmp;
struct timespec p;
/* Send reset, then check for result */
avalon_init_task(&at, 1, 0,
AVALON_DEFAULT_FAN_MAX_PWM,
AVALON_DEFAULT_TIMEOUT,
AVALON_DEFAULT_ASIC_NUM,
AVALON_DEFAULT_MINER_NUM,
0, 0,
AVALON_DEFAULT_FREQUENCY);
wait_avalon_ready(avalon);
ret = avalon_send_task(&at, avalon);
if (unlikely(ret == AVA_SEND_ERROR))
return -1;
if (!initial) {
applog(LOG_ERR, "%s%d reset sequence sent", avalon->drv->name, avalon->device_id);
return 0;
}
ret = avalon_read(avalon, (unsigned char *)&ar, AVALON_READ_SIZE,
AVALON_RESET_TIMEOUT, C_GET_AVALON_RESET);
/* What do these sleeps do?? */
p.tv_sec = 0;
p.tv_nsec = AVALON_RESET_PITCH;
nanosleep(&p, NULL);
/* Look for the first occurrence of 0xAA, the reset response should be:
* AA 55 AA 55 00 00 00 00 00 00 */
spare = ret - 10;
buf = tmp = (uint8_t *)&ar;
if (opt_debug) {
applog(LOG_DEBUG, "%s%d reset: get:", avalon->drv->name, avalon->device_id);
hexdump(tmp, AVALON_READ_SIZE);
}
for (i = 0; i <= spare; i++) {
buf = &tmp[i];
if (buf[0] == 0xAA)
break;
}
i = 0;
if (buf[0] == 0xAA && buf[1] == 0x55 &&
buf[2] == 0xAA && buf[3] == 0x55) {
for (i = 4; i < 11; i++)
if (buf[i] != 0)
break;
}
if (i != 11) {
applog(LOG_ERR, "%s%d: Reset failed! not an Avalon?"
" (%d: %02x %02x %02x %02x)", avalon->drv->name, avalon->device_id,
i, buf[0], buf[1], buf[2], buf[3]);
/* FIXME: return 1; */
} else
applog(LOG_WARNING, "%s%d: Reset succeeded",
avalon->drv->name, avalon->device_id);
return 0;
}
static bool get_options(int this_option_offset, int *baud, int *miner_count,
int *asic_count, int *timeout, int *frequency)
{
char buf[BUFSIZ+1];
char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
size_t max;
int i, tmp;
if (opt_avalon_options == NULL)
buf[0] = '\0';
else {
ptr = opt_avalon_options;
for (i = 0; i < this_option_offset; i++) {
comma = strchr(ptr, ',');
if (comma == NULL)
break;
ptr = comma + 1;
}
comma = strchr(ptr, ',');
if (comma == NULL)
max = strlen(ptr);
else
max = comma - ptr;
if (max > BUFSIZ)
max = BUFSIZ;
strncpy(buf, ptr, max);
buf[max] = '\0';
}
if (!(*buf))
return false;
colon = strchr(buf, ':');
if (colon)
*(colon++) = '\0';
tmp = atoi(buf);
switch (tmp) {
case 115200:
*baud = 115200;
break;
case 57600:
*baud = 57600;
break;
case 38400:
*baud = 38400;
break;
case 19200:
*baud = 19200;
break;
default:
quit(1, "Invalid avalon-options for baud (%s) "
"must be 115200, 57600, 38400 or 19200", buf);
}
if (colon && *colon) {
colon2 = strchr(colon, ':');
if (colon2)
*(colon2++) = '\0';
if (*colon) {
tmp = atoi(colon);
if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
*miner_count = tmp;
} else {
quit(1, "Invalid avalon-options for "
"miner_count (%s) must be 1 ~ %d",
colon, AVALON_DEFAULT_MINER_NUM);
}
}
if (colon2 && *colon2) {
colon3 = strchr(colon2, ':');
if (colon3)
*(colon3++) = '\0';
tmp = atoi(colon2);
if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
*asic_count = tmp;
else {
quit(1, "Invalid avalon-options for "
"asic_count (%s) must be 1 ~ %d",
colon2, AVALON_DEFAULT_ASIC_NUM);
}
if (colon3 && *colon3) {
colon4 = strchr(colon3, ':');
if (colon4)
*(colon4++) = '\0';
tmp = atoi(colon3);
if (tmp > 0 && tmp <= 0xff)
*timeout = tmp;
else {
quit(1, "Invalid avalon-options for "
"timeout (%s) must be 1 ~ %d",
colon3, 0xff);
}
if (colon4 && *colon4) {
tmp = atoi(colon4);
if (tmp < AVALON_MIN_FREQUENCY || tmp > AVALON_MAX_FREQUENCY) {
quit(1, "Invalid avalon-options for frequency, must be %d <= frequency <= %d",
AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY);
}
*frequency = tmp;
}
}
}
}
return true;
}
char *set_avalon_fan(char *arg)
{
int val1, val2, ret;
ret = sscanf(arg, "%d-%d", &val1, &val2);
if (ret < 1)
return "No values passed to avalon-fan";
if (ret == 1)
val2 = val1;
if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
return "Invalid value passed to avalon-fan";
opt_avalon_fan_min = val1 * AVALON_PWM_MAX / 100;
opt_avalon_fan_max = val2 * AVALON_PWM_MAX / 100;
return NULL;
}
char *set_avalon_freq(char *arg)
{
int val1, val2, ret;
ret = sscanf(arg, "%d-%d", &val1, &val2);
if (ret < 1)
return "No values passed to avalon-freq";
if (ret == 1)
val2 = val1;
if (val1 < AVALON_MIN_FREQUENCY || val1 > AVALON_MAX_FREQUENCY ||
val2 < AVALON_MIN_FREQUENCY || val2 > AVALON_MAX_FREQUENCY ||
val2 < val1)
return "Invalid value passed to avalon-freq";
opt_avalon_freq_min = val1;
opt_avalon_freq_max = val2;
return NULL;
}
static void avalon_idle(struct cgpu_info *avalon, struct avalon_info *info)
{
int i;
wait_avalon_ready(avalon);
/* Send idle to all miners */
for (i = 0; i < info->miner_count; i++) {
struct avalon_task at;
if (unlikely(avalon_buffer_full(avalon)))
break;
info->idle++;
avalon_init_task(&at, 0, 0, info->fan_pwm, info->timeout,
info->asic_count, info->miner_count, 1, 1,
info->frequency);
avalon_send_task(&at, avalon);
}
applog(LOG_WARNING, "%s%i: Idling %d miners", avalon->drv->name, avalon->device_id, i);
wait_avalon_ready(avalon);
}
static void avalon_initialise(struct cgpu_info *avalon)
{
int err, interface;
if (avalon->usbinfo.nodev)
return;
interface = avalon->usbdev->found->interface;
// Reset
err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
FTDI_VALUE_RESET, interface, C_RESET);
applog(LOG_DEBUG, "%s%i: reset got err %d",
avalon->drv->name, avalon->device_id, err);
if (avalon->usbinfo.nodev)
return;
// Set latency
err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
AVALON_LATENCY, interface, C_LATENCY);
applog(LOG_DEBUG, "%s%i: latency got err %d",
avalon->drv->name, avalon->device_id, err);
if (avalon->usbinfo.nodev)
return;
// Set data
err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
FTDI_VALUE_DATA_AVA, interface, C_SETDATA);
applog(LOG_DEBUG, "%s%i: data got err %d",
avalon->drv->name, avalon->device_id, err);
if (avalon->usbinfo.nodev)
return;
// Set the baud
err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_AVA,
(FTDI_INDEX_BAUD_AVA & 0xff00) | interface,
C_SETBAUD);
applog(LOG_DEBUG, "%s%i: setbaud got err %d",
avalon->drv->name, avalon->device_id, err);
if (avalon->usbinfo.nodev)
return;
// Set Modem Control
err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
FTDI_VALUE_MODEM, interface, C_SETMODEM);
applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
avalon->drv->name, avalon->device_id, err);
if (avalon->usbinfo.nodev)
return;
// Set Flow Control
err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
FTDI_VALUE_FLOW, interface, C_SETFLOW);
applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
avalon->drv->name, avalon->device_id, err);
if (avalon->usbinfo.nodev)
return;
/* Avalon repeats the following */
// Set Modem Control
err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
FTDI_VALUE_MODEM, interface, C_SETMODEM);
applog(LOG_DEBUG, "%s%i: setmodemctrl 2 got err %d",
avalon->drv->name, avalon->device_id, err);
if (avalon->usbinfo.nodev)
return;
// Set Flow Control
err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
FTDI_VALUE_FLOW, interface, C_SETFLOW);
applog(LOG_DEBUG, "%s%i: setflowctrl 2 got err %d",
avalon->drv->name, avalon->device_id, err);
}
static bool bitburner_set_core_voltage(struct cgpu_info *avalon, int core_voltage)
{
uint8_t buf[2];
int err;
if (usb_ident(avalon) == IDENT_BTB) {
buf[0] = (uint8_t)core_voltage;
buf[1] = (uint8_t)(core_voltage >> 8);
err = usb_transfer_data(avalon, FTDI_TYPE_OUT, BITBURNER_REQUEST,
BITBURNER_VALUE, BITBURNER_INDEX_SET_VOLTAGE,
(uint32_t *)buf, sizeof(buf), C_BB_SET_VOLTAGE);
if (unlikely(err < 0)) {
applog(LOG_ERR, "%s%i: SetCoreVoltage failed: err = %d",
avalon->drv->name, avalon->device_id, err);
return false;
} else {
applog(LOG_WARNING, "%s%i: Core voltage set to %d millivolts",
avalon->drv->name, avalon->device_id,
core_voltage);
}
return true;
}
return false;
}
static int bitburner_get_core_voltage(struct cgpu_info *avalon)
{
uint8_t buf[2];
int err;
int amount;
if (usb_ident(avalon) == IDENT_BTB) {
err = usb_transfer_read(avalon, FTDI_TYPE_IN, BITBURNER_REQUEST,
BITBURNER_VALUE, BITBURNER_INDEX_GET_VOLTAGE,
(char *)buf, sizeof(buf), &amount,
C_BB_GET_VOLTAGE);
if (unlikely(err != 0 || amount != 2)) {
applog(LOG_ERR, "%s%i: GetCoreVoltage failed: err = %d, amount = %d",
avalon->drv->name, avalon->device_id, err, amount);
return 0;
} else {
return (int)(buf[0] + ((unsigned int)buf[1] << 8));
}
} else {
return 0;
}
}
static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found)
{
int baud, uninitialised_var(miner_count), uninitialised_var(asic_count),
uninitialised_var(timeout), frequency = 0;
int this_option_offset = ++option_offset;
struct avalon_info *info;
struct cgpu_info *avalon;
bool configured;
int ret;
avalon = usb_alloc_cgpu(&avalon_drv, AVALON_MINER_THREADS);
configured = get_options(this_option_offset, &baud, &miner_count,
&asic_count, &timeout, &frequency);
if (!usb_init(avalon, dev, found))
goto shin;
/* Even though this is an FTDI type chip, we want to do the parsing
* all ourselves so set it to std usb type */
avalon->usbdev->usb_type = USB_TYPE_STD;
avalon->usbdev->PrefPacketSize = AVALON_USB_PACKETSIZE;
/* We have a real Avalon! */
avalon_initialise(avalon);
avalon->device_data = calloc(sizeof(struct avalon_info), 1);
if (unlikely(!(avalon->device_data)))
quit(1, "Failed to calloc avalon_info data");
info = avalon->device_data;
if (configured) {
info->baud = baud;
info->miner_count = miner_count;
info->asic_count = asic_count;
info->timeout = timeout;
info->frequency = frequency;
} else {
info->baud = AVALON_IO_SPEED;
info->miner_count = AVALON_DEFAULT_MINER_NUM;
info->asic_count = AVALON_DEFAULT_ASIC_NUM;
info->timeout = AVALON_DEFAULT_TIMEOUT;
info->frequency = AVALON_DEFAULT_FREQUENCY;
}
info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
info->temp_max = 0;
/* This is for check the temp/fan every 3~4s */
info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
if (info->temp_history_count <= 0)
info->temp_history_count = 1;
info->temp_history_index = 0;
info->temp_sum = 0;
info->temp_old = 0;
if (!add_cgpu(avalon))
goto unshin;
ret = avalon_reset(avalon, true);
if (ret && !configured)
goto unshin;
update_usb_stats(avalon);
avalon_idle(avalon, info);
applog(LOG_DEBUG, "Avalon Detected: %s "
"(miner_count=%d asic_count=%d timeout=%d frequency=%d)",
avalon->device_path, info->miner_count, info->asic_count, info->timeout,
info->frequency);
if (usb_ident(avalon) == IDENT_BTB) {
if (opt_bitburner_core_voltage < BITBURNER_MIN_COREMV ||
opt_bitburner_core_voltage > BITBURNER_MAX_COREMV) {
quit(1, "Invalid bitburner-voltage %d must be %dmv - %dmv",
opt_bitburner_core_voltage,
BITBURNER_MIN_COREMV,
BITBURNER_MAX_COREMV);
} else
bitburner_set_core_voltage(avalon, opt_bitburner_core_voltage);
}
return true;
unshin:
usb_uninit(avalon);
shin:
free(avalon->device_data);
avalon->device_data = NULL;
avalon = usb_free_cgpu(avalon);
return false;
}
static void avalon_detect(void)
{
usb_detect(&avalon_drv, avalon_detect_one);
}
static void avalon_init(struct cgpu_info *avalon)
{
applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
}
static struct work *avalon_valid_result(struct cgpu_info *avalon, struct avalon_result *ar)
{
return find_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
(char *)ar->data, 64, 12);
}
static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
struct avalon_result *ar);
static void avalon_inc_nvw(struct avalon_info *info, struct thr_info *thr)
{
applog(LOG_INFO, "%s%d: No matching work - HW error",
thr->cgpu->drv->name, thr->cgpu->device_id);
inc_hw_errors(thr);
info->no_matching_work++;
}
static void avalon_parse_results(struct cgpu_info *avalon, struct avalon_info *info,
struct thr_info *thr, char *buf, int *offset)
{
int i, spare = *offset - AVALON_READ_SIZE;
bool found = false;
for (i = 0; i <= spare; i++) {
struct avalon_result *ar;
struct work *work;
ar = (struct avalon_result *)&buf[i];
work = avalon_valid_result(avalon, ar);
if (work) {
bool gettemp = false;
found = true;
if (avalon_decode_nonce(thr, avalon, info, ar, work)) {
mutex_lock(&info->lock);
if (!info->nonces++)
gettemp = true;
info->auto_nonces++;
mutex_unlock(&info->lock);
} else if (opt_avalon_auto) {
mutex_lock(&info->lock);
info->auto_hw++;
mutex_unlock(&info->lock);
}
if (gettemp)
avalon_update_temps(avalon, info, ar);
break;
}
}
if (!found) {
spare = *offset - AVALON_READ_SIZE;
/* We are buffering and haven't accumulated one more corrupt
* work result. */
if (spare < (int)AVALON_READ_SIZE)
return;
avalon_inc_nvw(info, thr);
} else {
spare = AVALON_READ_SIZE + i;
if (i) {
if (i >= (int)AVALON_READ_SIZE)
avalon_inc_nvw(info, thr);
else
applog(LOG_WARNING, "Avalon: Discarding %d bytes from buffer", i);
}
}
*offset -= spare;
memmove(buf, buf + spare, *offset);
}
static void avalon_running_reset(struct cgpu_info *avalon,
struct avalon_info *info)
{
avalon_reset(avalon, false);
avalon_idle(avalon, info);
avalon->results = 0;
info->reset = false;
}
static void *avalon_get_results(void *userdata)
{
struct cgpu_info *avalon = (struct cgpu_info *)userdata;
struct avalon_info *info = avalon->device_data;
int offset = 0, read_delay = 0, ret = 0;
const int rsize = AVALON_FTDI_READSIZE;
char readbuf[AVALON_READBUF_SIZE];
struct thr_info *thr = info->thr;
struct timeval tv_start, tv_end;
char threadname[24];
snprintf(threadname, 24, "ava_recv/%d", avalon->device_id);
RenameThread(threadname);
while (likely(!avalon->shutdown)) {
unsigned char buf[rsize];
struct timeval tv_diff;
int us_diff;
if (offset >= (int)AVALON_READ_SIZE)
avalon_parse_results(avalon, info, thr, readbuf, &offset);
if (unlikely(offset + rsize >= AVALON_READBUF_SIZE)) {
/* This should never happen */
applog(LOG_ERR, "Avalon readbuf overflow, resetting buffer");
offset = 0;
}
if (unlikely(info->reset)) {
avalon_running_reset(avalon, info);
/* Discard anything in the buffer */
offset = 0;
}
/* As the usb read returns after just 1ms, sleep long enough
* to leave the interface idle for writes to occur, but do not
* sleep if we have been receiving data as more may be coming. */
if (ret < 1) {
cgtime(&tv_end);
timersub(&tv_end, &tv_start, &tv_diff);
/* Assume it has not been > 1 second so ignore tv_sec */
us_diff = tv_diff.tv_usec;
read_delay = AVALON_READ_TIMEOUT * 1000 - us_diff;
if (likely(read_delay >= 1000))
nusleep(read_delay);
}
cgtime(&tv_start);
ret = avalon_read(avalon, buf, rsize, AVALON_READ_TIMEOUT,
C_AVALON_READ);
if (ret < 1)
continue;
if (opt_debug) {
applog(LOG_DEBUG, "Avalon: get:");
hexdump((uint8_t *)buf, ret);
}
memcpy(&readbuf[offset], &buf, ret);
offset += ret;
}
return NULL;
}
static void avalon_rotate_array(struct cgpu_info *avalon)
{
avalon->queued = 0;
if (++avalon->work_array >= AVALON_ARRAY_SIZE)
avalon->work_array = 0;
}
static void avalon_set_timeout(struct avalon_info *info)
{
info->timeout = AVALON_TIMEOUT_FACTOR / info->frequency;
}
static void avalon_set_freq(struct cgpu_info *avalon, int frequency)
{
struct avalon_info *info = avalon->device_data;
info->frequency = frequency;
if (info->frequency > opt_avalon_freq_max)
info->frequency = opt_avalon_freq_max;
if (info->frequency < opt_avalon_freq_min)
info->frequency = opt_avalon_freq_min;
avalon_set_timeout(info);
applog(LOG_WARNING, "%s%i: Set frequency to %d, timeout %d",
avalon->drv->name, avalon->device_id,
info->frequency, info->timeout);
}
static void avalon_inc_freq(struct avalon_info *info)
{
info->frequency += 2;
if (info->frequency > opt_avalon_freq_max)
info->frequency = opt_avalon_freq_max;
avalon_set_timeout(info);
applog(LOG_NOTICE, "Avalon increasing frequency to %d, timeout %d",
info->frequency, info->timeout);
}
static void avalon_dec_freq(struct avalon_info *info)
{
info->frequency -= 1;
if (info->frequency < opt_avalon_freq_min)
info->frequency = opt_avalon_freq_min;
avalon_set_timeout(info);
applog(LOG_NOTICE, "Avalon decreasing frequency to %d, timeout %d",
info->frequency, info->timeout);
}
static void avalon_reset_auto(struct avalon_info *info)
{
info->auto_queued =
info->auto_nonces =
info->auto_hw = 0;
}
static void *avalon_send_tasks(void *userdata)
{
struct cgpu_info *avalon = (struct cgpu_info *)userdata;
struct avalon_info *info = avalon->device_data;
const int avalon_get_work_count = info->miner_count;
char threadname[24];
snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
RenameThread(threadname);
while (likely(!avalon->shutdown)) {
int start_count, end_count, i, j, ret;
struct avalon_task at;
bool idled = false;
while (avalon_buffer_full(avalon))
nmsleep(40);
if (opt_avalon_auto && info->auto_queued >= AVALON_AUTO_CYCLE) {
mutex_lock(&info->lock);
if (!info->optimal) {
if (info->fan_pwm >= opt_avalon_fan_max) {
applog(LOG_WARNING,
"%s%i: Above optimal temperature, throttling",
avalon->drv->name, avalon->device_id);
avalon_dec_freq(info);
}
} else if (info->auto_nonces >= (AVALON_AUTO_CYCLE * 19 / 20) &&
info->auto_nonces <= (AVALON_AUTO_CYCLE * 21 / 20)) {
int total = info->auto_nonces + info->auto_hw;
/* Try to keep hw errors < 2% */
if (info->auto_hw * 100 < total)
avalon_inc_freq(info);
else if (info->auto_hw * 66 > total)
avalon_dec_freq(info);
}
avalon_reset_auto(info);
mutex_unlock(&info->lock);
}
mutex_lock(&info->qlock);
start_count = avalon->work_array * avalon_get_work_count;
end_count = start_count + avalon_get_work_count;
for (i = start_count, j = 0; i < end_count; i++, j++) {
if (avalon_buffer_full(avalon)) {
applog(LOG_INFO,
"%s%i: Buffer full after only %d of %d work queued",
avalon->drv->name, avalon->device_id, j, avalon_get_work_count);
if (usb_ident(avalon) != IDENT_BTB)
break;
else {
while (avalon_buffer_full(avalon))
nmsleep(40);
}
}
if (likely(j < avalon->queued && !info->overheat && avalon->works[i])) {
avalon_init_task(&at, 0, 0, info->fan_pwm,
info->timeout, info->asic_count,
info->miner_count, 1, 0, info->frequency);
avalon_create_task(&at, avalon->works[i]);
info->auto_queued++;
} else {
int idle_freq = info->frequency;
if (!info->idle++)
idled = true;
if (unlikely(info->overheat && opt_avalon_auto))
idle_freq = AVALON_MIN_FREQUENCY;
avalon_init_task(&at, 0, 0, info->fan_pwm,
info->timeout, info->asic_count,
info->miner_count, 1, 1, idle_freq);
/* Reset the auto_queued count if we end up
* idling any miners. */
avalon_reset_auto(info);
}
ret = avalon_send_task(&at, avalon);
if (unlikely(ret == AVA_SEND_ERROR)) {
applog(LOG_ERR, "%s%i: Comms error(buffer)",
avalon->drv->name, avalon->device_id);
dev_error(avalon, REASON_DEV_COMMS_ERROR);
info->reset = true;
break;
}
}
avalon_rotate_array(avalon);
pthread_cond_signal(&info->qcond);
mutex_unlock(&info->qlock);
if (unlikely(idled)) {
applog(LOG_WARNING, "%s%i: Idled %d miners",
avalon->drv->name, avalon->device_id, idled);
}
}
return NULL;
}
static bool avalon_prepare(struct thr_info *thr)
{
struct cgpu_info *avalon = thr->cgpu;
struct avalon_info *info = avalon->device_data;
free(avalon->works);
avalon->works = calloc(info->miner_count * sizeof(struct work *),
AVALON_ARRAY_SIZE);
if (!avalon->works)
quit(1, "Failed to calloc avalon works in avalon_prepare");
info->thr = thr;
mutex_init(&info->lock);
mutex_init(&info->qlock);
if (unlikely(pthread_cond_init(&info->qcond, NULL)))
quit(1, "Failed to pthread_cond_init avalon qcond");
if (pthread_create(&info->read_thr, NULL, avalon_get_results, (void *)avalon))
quit(1, "Failed to create avalon read_thr");
if (pthread_create(&info->write_thr, NULL, avalon_send_tasks, (void *)avalon))
quit(1, "Failed to create avalon write_thr");
avalon_init(avalon);
return true;
}
static void do_avalon_close(struct thr_info *thr)
{
struct cgpu_info *avalon = thr->cgpu;
struct avalon_info *info = avalon->device_data;
pthread_join(info->read_thr, NULL);
pthread_join(info->write_thr, NULL);
avalon_running_reset(avalon, info);
info->no_matching_work = 0;
}
static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
{
info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
info->temp0 = ar->temp0;
info->temp1 = ar->temp1;
info->temp2 = ar->temp2;
if (ar->temp0 & 0x80) {
ar->temp0 &= 0x7f;
info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
}
if (ar->temp1 & 0x80) {
ar->temp1 &= 0x7f;
info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
}
if (ar->temp2 & 0x80) {
ar->temp2 &= 0x7f;
info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
}
*temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
if (info->temp0 > info->temp_max)
info->temp_max = info->temp0;
if (info->temp1 > info->temp_max)
info->temp_max = info->temp1;
if (info->temp2 > info->temp_max)
info->temp_max = info->temp2;
}
static void temp_rise(struct avalon_info *info, int temp)
{
if (temp >= opt_avalon_temp + AVALON_TEMP_HYSTERESIS * 3) {
info->fan_pwm = AVALON_PWM_MAX;
return;
}
if (temp >= opt_avalon_temp + AVALON_TEMP_HYSTERESIS * 2)
info->fan_pwm += 10;
else if (temp > opt_avalon_temp)
info->fan_pwm += 5;
else if (temp >= opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
info->fan_pwm += 1;
else
return;
if (info->fan_pwm > opt_avalon_fan_max)
info->fan_pwm = opt_avalon_fan_max;
}
static void temp_drop(struct avalon_info *info, int temp)
{
if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS * 3) {
info->fan_pwm = opt_avalon_fan_min;
return;
}
if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS * 2)
info->fan_pwm -= 10;
else if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
info->fan_pwm -= 5;
else if (temp < opt_avalon_temp)
info->fan_pwm -= 1;
if (info->fan_pwm < opt_avalon_fan_min)
info->fan_pwm = opt_avalon_fan_min;
}
static inline void adjust_fan(struct avalon_info *info)
{
int temp_new;
temp_new = info->temp_sum / info->temp_history_count;
if (temp_new > info->temp_old)
temp_rise(info, temp_new);
else if (temp_new < info->temp_old)
temp_drop(info, temp_new);
else {
/* temp_new == info->temp_old */
if (temp_new > opt_avalon_temp)
temp_rise(info, temp_new);
else if (temp_new < opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
temp_drop(info, temp_new);
}
info->temp_old = temp_new;
if (info->temp_old <= opt_avalon_temp)
info->optimal = true;
else
info->optimal = false;
}
static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
struct avalon_result *ar)
{
record_temp_fan(info, ar, &(avalon->temp));
applog(LOG_INFO,
"Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
"Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
info->fan0, info->fan1, info->fan2,
info->temp0, info->temp1, info->temp2, info->temp_max);
info->temp_history_index++;
info->temp_sum += avalon->temp;
applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
info->temp_history_index, info->temp_history_count, info->temp_old);
if (usb_ident(avalon) == IDENT_BTB) {
info->core_voltage = bitburner_get_core_voltage(avalon);
}
if (info->temp_history_index == info->temp_history_count) {
adjust_fan(info);
info->temp_history_index = 0;
info->temp_sum = 0;
}
if (unlikely(info->temp_old >= opt_avalon_overheat)) {
applog(LOG_WARNING, "%s%d overheat! Idling", avalon->drv->name, avalon->device_id);
info->overheat = true;
} else if (info->overheat && info->temp_old <= opt_avalon_temp) {
applog(LOG_WARNING, "%s%d cooled, restarting", avalon->drv->name, avalon->device_id);
info->overheat = false;
}
}
static void get_avalon_statline_before(char *buf, size_t bufsiz, struct cgpu_info *avalon)
{
struct avalon_info *info = avalon->device_data;
int lowfan = 10000;
if (usb_ident(avalon) == IDENT_BTB) {
tailsprintf(buf, bufsiz, "%2d/%3dC %4dmV | ", info->temp0, info->temp2, info->core_voltage);
} else {
/* Find the lowest fan speed of the ASIC cooling fans. */
if (info->fan1 >= 0 && info->fan1 < lowfan)
lowfan = info->fan1;
if (info->fan2 >= 0 && info->fan2 < lowfan)
lowfan = info->fan2;
tailsprintf(buf, bufsiz, "%2d/%3dC %04dR | ", info->temp0, info->temp2, lowfan);
}
}
/* We use a replacement algorithm to only remove references to work done from
* the buffer when we need the extra space for new work. */
static bool avalon_fill(struct cgpu_info *avalon)
{
struct avalon_info *info = avalon->device_data;
int subid, slot, mc;
struct work *work;
bool ret = true;
mc = info->miner_count;
mutex_lock(&info->qlock);
if (avalon->queued >= mc)
goto out_unlock;
work = get_queued(avalon);
if (unlikely(!work)) {
ret = false;
goto out_unlock;
}
subid = avalon->queued++;
work->subid = subid;
slot = avalon->work_array * mc + subid;
if (likely(avalon->works[slot]))
work_completed(avalon, avalon->works[slot]);
avalon->works[slot] = work;
if (avalon->queued < mc)
ret = false;
out_unlock:
mutex_unlock(&info->qlock);
return ret;
}
static int64_t avalon_scanhash(struct thr_info *thr)
{
struct cgpu_info *avalon = thr->cgpu;
struct avalon_info *info = avalon->device_data;
const int miner_count = info->miner_count;
struct timeval now, then, tdiff;
int64_t hash_count, us_timeout;
struct timespec abstime;
/* Half nonce range */
us_timeout = 0x80000000ll / info->asic_count / info->frequency;
tdiff.tv_sec = us_timeout / 1000000;
tdiff.tv_usec = us_timeout - (tdiff.tv_sec * 1000000);
cgtime(&now);
timeradd(&now, &tdiff, &then);
abstime.tv_sec = then.tv_sec;
abstime.tv_nsec = then.tv_usec * 1000;
/* Wait until avalon_send_tasks signals us that it has completed
* sending its work or a full nonce range timeout has occurred */
mutex_lock(&info->qlock);
pthread_cond_timedwait(&info->qcond, &info->qlock, &abstime);
mutex_unlock(&info->qlock);
mutex_lock(&info->lock);
hash_count = 0xffffffffull * (uint64_t)info->nonces;
avalon->results += info->nonces + info->idle;
if (avalon->results > miner_count)
avalon->results = miner_count;
if (!info->reset)
avalon->results--;
info->nonces = info->idle = 0;
mutex_unlock(&info->lock);
/* Check for nothing but consecutive bad results or consistently less
* results than we should be getting and reset the FPGA if necessary */
if (usb_ident(avalon) != IDENT_BTB) {
if (avalon->results < -miner_count && !info->reset) {
applog(LOG_ERR, "%s%d: Result return rate low, resetting!",
avalon->drv->name, avalon->device_id);
info->reset = true;
}
}
if (unlikely(avalon->usbinfo.nodev)) {
applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread",
avalon->drv->name, avalon->device_id);
avalon->shutdown = true;
}
/* This hashmeter is just a utility counter based on returned shares */
return hash_count;
}
static void avalon_flush_work(struct cgpu_info *avalon)
{
struct avalon_info *info = avalon->device_data;
mutex_lock(&info->qlock);
/* Will overwrite any work queued */
avalon->queued = 0;
pthread_cond_signal(&info->qcond);
mutex_unlock(&info->qlock);
}
static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
{
struct api_data *root = NULL;
struct avalon_info *info = cgpu->device_data;
int i;
root = api_add_int(root, "baud", &(info->baud), false);
root = api_add_int(root, "miner_count", &(info->miner_count),false);
root = api_add_int(root, "asic_count", &(info->asic_count), false);
root = api_add_int(root, "timeout", &(info->timeout), false);
root = api_add_int(root, "frequency", &(info->frequency), false);
root = api_add_int(root, "fan1", &(info->fan0), false);
root = api_add_int(root, "fan2", &(info->fan1), false);
root = api_add_int(root, "fan3", &(info->fan2), false);
root = api_add_int(root, "temp1", &(info->temp0), false);
root = api_add_int(root, "temp2", &(info->temp1), false);
root = api_add_int(root, "temp3", &(info->temp2), false);
root = api_add_int(root, "temp_max", &(info->temp_max), false);
root = api_add_int(root, "core_voltage", &(info->core_voltage), false);
root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
for (i = 0; i < info->miner_count; i++) {
char mcw[24];
sprintf(mcw, "match_work_count%d", i + 1);
root = api_add_int(root, mcw, &(info->matching_work[i]), false);
}
return root;
}
static void avalon_shutdown(struct thr_info *thr)
{
do_avalon_close(thr);
}
static char *avalon_set_device(struct cgpu_info *avalon, char *option, char *setting, char *replybuf)
{
int val;
if (strcasecmp(option, "help") == 0) {
sprintf(replybuf, "freq: range %d-%d millivolts: range %d-%d",
AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY,
BITBURNER_MIN_COREMV, BITBURNER_MAX_COREMV);
return replybuf;
}
if (strcasecmp(option, "millivolts") == 0 || strcasecmp(option, "mv") == 0) {
if (usb_ident(avalon) != IDENT_BTB) {
sprintf(replybuf, "%s cannot set millivolts", avalon->drv->name);
return replybuf;
}
if (!setting || !*setting) {
sprintf(replybuf, "missing millivolts setting");
return replybuf;
}
val = atoi(setting);
if (val < BITBURNER_MIN_COREMV || val > BITBURNER_MAX_COREMV) {
sprintf(replybuf, "invalid millivolts: '%s' valid range %d-%d",
setting, BITBURNER_MIN_COREMV, BITBURNER_MAX_COREMV);
return replybuf;
}
if (bitburner_set_core_voltage(avalon, val))
return NULL;
else {
sprintf(replybuf, "Set millivolts failed");
return replybuf;
}
}
if (strcasecmp(option, "freq") == 0) {
if (!setting || !*setting) {
sprintf(replybuf, "missing freq setting");
return replybuf;
}
val = atoi(setting);
if (val < AVALON_MIN_FREQUENCY || val > AVALON_MAX_FREQUENCY) {
sprintf(replybuf, "invalid freq: '%s' valid range %d-%d",
setting, AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY);
return replybuf;
}
avalon_set_freq(avalon, val);
return NULL;
}
sprintf(replybuf, "Unknown option: %s", option);
return replybuf;
}
struct device_drv avalon_drv = {
.drv_id = DRIVER_AVALON,
.dname = "avalon",
.name = "AVA",
.drv_detect = avalon_detect,
.thread_prepare = avalon_prepare,
.hash_work = hash_queued_work,
.queue_full = avalon_fill,
.scanwork = avalon_scanhash,
.flush_work = avalon_flush_work,
.get_api_stats = avalon_api_stats,
.get_statline_before = get_avalon_statline_before,
.set_device = avalon_set_device,
.reinit_device = avalon_init,
.thread_shutdown = avalon_shutdown,
};