driver-avalon.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * Copyright 2012 2013 Xiangfu <xiangfu@openmobilefree.com>
  3. * Copyright 2012 Andrew Smith
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <limits.h>
  12. #include <pthread.h>
  13. #include <stdio.h>
  14. #include <sys/time.h>
  15. #include <sys/types.h>
  16. #include <dirent.h>
  17. #include <unistd.h>
  18. #ifndef WIN32
  19. #include <termios.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #ifndef O_CLOEXEC
  23. #define O_CLOEXEC 0
  24. #endif
  25. #else
  26. #include <windows.h>
  27. #include <io.h>
  28. #endif
  29. #include "elist.h"
  30. #include "miner.h"
  31. #include "fpgautils.h"
  32. #include "driver-avalon.h"
  33. #include "hexdump.c"
  34. static int option_offset = -1;
  35. struct avalon_info **avalon_info;
  36. struct device_api avalon_api;
  37. static int avalon_init_task(struct thr_info *thr, struct avalon_task *at,
  38. uint8_t reset, uint8_t ff, uint8_t fan,
  39. uint8_t timeout_p, uint8_t asic_num_p,
  40. uint8_t miner_num_p)
  41. {
  42. static bool first = true;
  43. uint8_t timeout;
  44. uint8_t asic_num;
  45. uint8_t miner_num;
  46. struct cgpu_info *avalon;
  47. struct avalon_info *info;
  48. if (!at)
  49. return -1;
  50. if (!thr && (timeout_p <= 0 || asic_num_p <= 0 || miner_num_p <= 0))
  51. return -1;
  52. timeout = timeout_p;
  53. miner_num = miner_num_p;
  54. asic_num = asic_num_p;
  55. if (thr) {
  56. avalon = thr->cgpu;
  57. info = avalon_info[avalon->device_id];
  58. timeout = info->timeout;
  59. miner_num = info->miner_count;
  60. asic_num = info->asic_count;
  61. }
  62. memset(at, 0, sizeof(struct avalon_task));
  63. if (reset) {
  64. at->reset = 1;
  65. at->fan_eft = 1;
  66. at->timer_eft = 1;
  67. first = true;
  68. }
  69. at->flush_fifo = (ff ? 1 : 0);
  70. at->fan_eft = (fan ? 1 : 0);
  71. if (first && !at->reset) {
  72. at->fan_eft = 1;
  73. at->timer_eft = 1;
  74. first = false;
  75. }
  76. at->fan_pwm_data = (fan ? fan : AVALON_DEFAULT_FAN_PWM);
  77. at->timeout_data = timeout;
  78. at->asic_num = asic_num;
  79. at->miner_num = miner_num;
  80. at->nonce_elf = 1;
  81. return 0;
  82. }
  83. static inline void avalon_create_task(struct avalon_task *at,
  84. struct work *work)
  85. {
  86. memcpy(at->midstate, work->midstate, 32);
  87. memcpy(at->data, work->data + 64, 12);
  88. }
  89. static int avalon_send_task(int fd, const struct avalon_task *at,
  90. struct thr_info *thr)
  91. {
  92. size_t ret;
  93. int full;
  94. struct timespec p;
  95. uint8_t *buf;
  96. size_t nr_len;
  97. struct cgpu_info *avalon;
  98. struct avalon_info *info;
  99. long delay = 32 * 1000 * 1000; /* default 32ms for B19200 */
  100. nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
  101. buf = calloc(1, AVALON_WRITE_SIZE + nr_len);
  102. if (!buf)
  103. return AVA_SEND_ERROR;
  104. memcpy(buf, at, AVALON_WRITE_SIZE);
  105. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  106. uint8_t tt = 0;
  107. tt = (buf[0] & 0x0f) << 4;
  108. tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
  109. tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
  110. tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
  111. tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
  112. buf[0] = tt;
  113. buf[4] = rev8(buf[4]);
  114. #endif
  115. if (opt_debug) {
  116. applog(LOG_DEBUG, "Avalon: Sent(%d):", nr_len);
  117. hexdump((uint8_t *)buf, nr_len);
  118. }
  119. ret = write(fd, buf, nr_len);
  120. free(buf);
  121. if (unlikely(ret != nr_len))
  122. return AVA_SEND_ERROR;
  123. if (thr) {
  124. avalon = thr->cgpu;
  125. info = avalon_info[avalon->device_id];
  126. delay = AVALON_WRITE_SIZE * 10 * 1000000000 / info->baud;
  127. }
  128. p.tv_sec = 0;
  129. p.tv_nsec = delay;
  130. nanosleep(&p, NULL);
  131. applog(LOG_DEBUG, "Avalon: Sent: Buffer delay: %ld",
  132. delay);
  133. full = avalon_buffer_full(fd);
  134. applog(LOG_DEBUG, "Avalon: Sent: Buffer full: %s",
  135. ((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
  136. if (full == AVA_BUFFER_EMPTY)
  137. return AVA_SEND_BUFFER_EMPTY;
  138. return AVA_SEND_BUFFER_FULL;
  139. }
  140. static int avalon_gets(int fd, uint8_t *buf, int read_count,
  141. struct thr_info *thr, struct timeval *tv_finish)
  142. {
  143. ssize_t ret = 0;
  144. int rc = 0;
  145. int read_amount = AVALON_READ_SIZE;
  146. bool first = true;
  147. /* Read reply 1 byte at a time to get earliest tv_finish */
  148. while (true) {
  149. ret = read(fd, buf, 1);
  150. if (ret < 0)
  151. return AVA_GETS_ERROR;
  152. if (first && tv_finish != NULL)
  153. gettimeofday(tv_finish, NULL);
  154. if (ret >= read_amount)
  155. return AVA_GETS_OK;
  156. if (ret > 0) {
  157. buf += ret;
  158. read_amount -= ret;
  159. first = false;
  160. continue;
  161. }
  162. rc++;
  163. if (rc >= read_count) {
  164. if (opt_debug) {
  165. applog(LOG_ERR,
  166. "Avalon: No data in %.2f seconds",
  167. (float)rc/(float)TIME_FACTOR);
  168. }
  169. return AVA_GETS_TIMEOUT;
  170. }
  171. if (thr && thr->work_restart) {
  172. if (opt_debug) {
  173. applog(LOG_ERR,
  174. "Avalon: Work restart at %.2f seconds",
  175. (float)(rc)/(float)TIME_FACTOR);
  176. }
  177. return AVA_GETS_RESTART;
  178. }
  179. }
  180. }
  181. static int avalon_get_result(int fd, struct avalon_result *ar,
  182. struct thr_info *thr, struct timeval *tv_finish)
  183. {
  184. struct cgpu_info *avalon;
  185. struct avalon_info *info;
  186. uint8_t result[AVALON_READ_SIZE];
  187. int ret, read_count = AVALON_RESET_FAULT_DECISECONDS * TIME_FACTOR;
  188. if (thr) {
  189. avalon = thr->cgpu;
  190. info = avalon_info[avalon->device_id];
  191. read_count = info->read_count;
  192. }
  193. memset(result, 0, AVALON_READ_SIZE);
  194. ret = avalon_gets(fd, result, read_count, thr, tv_finish);
  195. if (ret == AVA_GETS_OK) {
  196. if (opt_debug) {
  197. applog(LOG_DEBUG, "Avalon: get:");
  198. hexdump((uint8_t *)result, AVALON_READ_SIZE);
  199. }
  200. memcpy((uint8_t *)ar, result, AVALON_READ_SIZE);
  201. }
  202. return ret;
  203. }
  204. static int avalon_decode_nonce(struct thr_info *thr, struct work **work,
  205. struct avalon_result *ar, uint32_t *nonce)
  206. {
  207. struct cgpu_info *avalon;
  208. struct avalon_info *info;
  209. int avalon_get_work_count, i;
  210. if (!work)
  211. return -1;
  212. avalon = thr->cgpu;
  213. info = avalon_info[avalon->device_id];
  214. avalon_get_work_count = info->miner_count;
  215. for (i = 0; i < avalon_get_work_count; i++) {
  216. if (work[i] &&
  217. !memcmp(ar->data, work[i]->data + 64, 12) &&
  218. !memcmp(ar->midstate, work[i]->midstate, 32))
  219. break;
  220. }
  221. if (i == avalon_get_work_count)
  222. return -1;
  223. *nonce = ar->nonce;
  224. #if defined (__BIG_ENDIAN__) || defined(MIPSEB)
  225. *nonce = swab32(*nonce);
  226. #endif
  227. applog(LOG_DEBUG, "Avalon: match to work[%d]: %p", i, work[i]);
  228. return i;
  229. }
  230. static int avalon_reset(int fd, uint8_t timeout_p, uint8_t asic_num_p,
  231. uint8_t miner_num_p)
  232. {
  233. struct avalon_task at;
  234. struct avalon_result ar;
  235. uint8_t *buf;
  236. int ret, i;
  237. struct timespec p;
  238. avalon_init_task(NULL,
  239. &at, 1, 0,
  240. AVALON_DEFAULT_FAN_PWM,
  241. timeout_p, asic_num_p, miner_num_p);
  242. ret = avalon_send_task(fd, &at, NULL);
  243. if (ret == AVA_SEND_ERROR)
  244. return 1;
  245. avalon_get_result(fd, &ar, NULL, NULL);
  246. buf = (uint8_t *)&ar;
  247. for (i = 0; i < 11; i++)
  248. if (buf[i] != 0)
  249. break;
  250. /* FIXME: add more avalon info base on return */
  251. if (i != 11) {
  252. applog(LOG_ERR, "Avalon: Reset failed! not a Avalon?");
  253. return 1;
  254. }
  255. p.tv_sec = 1;
  256. p.tv_nsec = AVALON_RESET_PITCH;
  257. nanosleep(&p, NULL);
  258. applog(LOG_ERR,
  259. "Avalon: Fan1: %d, Fan2: %d, Fan3: %d. Temp1: %d, Temp2: %d, Temp3: %d",
  260. ar.fan0, ar.fan1, ar.fan2, ar.temp0, ar.temp1, ar.temp2);
  261. applog(LOG_ERR, "Avalon: Reset succeeded");
  262. return 0;
  263. }
  264. static void do_avalon_close(struct thr_info *thr)
  265. {
  266. struct cgpu_info *avalon = thr->cgpu;
  267. avalon_close(avalon->device_fd);
  268. avalon->device_fd = -1;
  269. /* FIXME: we should free the bulk0/1/2 */
  270. }
  271. static void set_timing_mode(struct cgpu_info *avalon)
  272. {
  273. struct avalon_info *info = avalon_info[avalon->device_id];
  274. info->Hs = AVALON_HASH_TIME;
  275. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  276. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  277. }
  278. static void get_options(int this_option_offset, int *baud, int *miner_count,
  279. int *asic_count, int *timeout)
  280. {
  281. char err_buf[BUFSIZ+1];
  282. char buf[BUFSIZ+1];
  283. char *ptr, *comma, *colon, *colon2, *colon3;
  284. size_t max;
  285. int i, tmp;
  286. if (opt_avalon_options == NULL)
  287. buf[0] = '\0';
  288. else {
  289. ptr = opt_avalon_options;
  290. for (i = 0; i < this_option_offset; i++) {
  291. comma = strchr(ptr, ',');
  292. if (comma == NULL)
  293. break;
  294. ptr = comma + 1;
  295. }
  296. comma = strchr(ptr, ',');
  297. if (comma == NULL)
  298. max = strlen(ptr);
  299. else
  300. max = comma - ptr;
  301. if (max > BUFSIZ)
  302. max = BUFSIZ;
  303. strncpy(buf, ptr, max);
  304. buf[max] = '\0';
  305. }
  306. *baud = AVALON_IO_SPEED;
  307. *miner_count = AVALON_DEFAULT_MINER_NUM;
  308. *asic_count = AVALON_DEFAULT_ASIC_NUM;
  309. *timeout = AVALON_DEFAULT_TIMEOUT;
  310. if (!(*buf))
  311. return;
  312. colon = strchr(buf, ':');
  313. if (colon)
  314. *(colon++) = '\0';
  315. tmp = atoi(buf);
  316. switch (tmp) {
  317. case 115200:
  318. *baud = 115200;
  319. break;
  320. case 57600:
  321. *baud = 57600;
  322. break;
  323. case 19200:
  324. *baud = 19200;
  325. break;
  326. default:
  327. sprintf(err_buf,
  328. "Invalid avalon-options for baud (%s) "
  329. "must be 115200, 57600 or 19200", buf);
  330. quit(1, err_buf);
  331. }
  332. if (colon && *colon) {
  333. colon2 = strchr(colon, ':');
  334. if (colon2)
  335. *(colon2++) = '\0';
  336. if (*colon) {
  337. tmp = atoi(colon);
  338. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  339. *miner_count = tmp;
  340. } else {
  341. sprintf(err_buf,
  342. "Invalid avalon-options for "
  343. "miner_count (%s) must be 1 ~ %d",
  344. colon, AVALON_DEFAULT_MINER_NUM);
  345. quit(1, err_buf);
  346. }
  347. }
  348. if (colon2 && *colon2) {
  349. colon3 = strchr(colon2, ':');
  350. if (colon3)
  351. *(colon3++) = '\0';
  352. tmp = atoi(colon2);
  353. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  354. *asic_count = tmp;
  355. else {
  356. sprintf(err_buf,
  357. "Invalid avalon-options for "
  358. "asic_count (%s) must be 1 ~ %d",
  359. colon2, AVALON_DEFAULT_ASIC_NUM);
  360. quit(1, err_buf);
  361. }
  362. if (colon3 && *colon3) {
  363. tmp = atoi(colon3);
  364. if (tmp > 0 && tmp <= 0xff)
  365. *timeout = tmp;
  366. else {
  367. sprintf(err_buf,
  368. "Invalid avalon-options for "
  369. "timeout (%s) must be 1 ~ %d",
  370. colon3, 0xff);
  371. quit(1, err_buf);
  372. }
  373. }
  374. }
  375. }
  376. }
  377. static bool avalon_detect_one(const char *devpath)
  378. {
  379. struct avalon_info *info;
  380. int fd, ret;
  381. int baud, miner_count, asic_count, timeout;
  382. int this_option_offset = ++option_offset;
  383. get_options(this_option_offset, &baud, &miner_count, &asic_count,
  384. &timeout);
  385. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s "
  386. "(baud=%d miner_count=%d asic_count=%d timeout=%d)",
  387. devpath, baud, miner_count, asic_count, timeout);
  388. fd = avalon_open2(devpath, baud, true);
  389. if (unlikely(fd == -1)) {
  390. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  391. return false;
  392. }
  393. ret = avalon_reset(fd, timeout, asic_count, miner_count);
  394. avalon_close(fd);
  395. if (ret)
  396. return false;
  397. /* We have a real Avalon! */
  398. struct cgpu_info *avalon;
  399. avalon = calloc(1, sizeof(struct cgpu_info));
  400. avalon->api = &avalon_api;
  401. avalon->device_path = strdup(devpath);
  402. avalon->device_fd = -1;
  403. avalon->threads = AVALON_MINER_THREADS;
  404. add_cgpu(avalon);
  405. avalon_info = realloc(avalon_info,
  406. sizeof(struct avalon_info *) *
  407. (total_devices + 1));
  408. applog(LOG_INFO, "Avalon Detect: Found at %s, mark as %d",
  409. devpath, avalon->device_id);
  410. avalon_info[avalon->device_id] = (struct avalon_info *)
  411. malloc(sizeof(struct avalon_info));
  412. if (unlikely(!(avalon_info[avalon->device_id])))
  413. quit(1, "Failed to malloc avalon_info");
  414. info = avalon_info[avalon->device_id];
  415. memset(info, 0, sizeof(struct avalon_info));
  416. info->baud = baud;
  417. info->miner_count = miner_count;
  418. info->asic_count = asic_count;
  419. info->timeout = timeout;
  420. set_timing_mode(avalon);
  421. return true;
  422. }
  423. static inline void avalon_detect()
  424. {
  425. serial_detect(&avalon_api, avalon_detect_one);
  426. }
  427. static bool avalon_prepare(struct thr_info *thr)
  428. {
  429. struct cgpu_info *avalon = thr->cgpu;
  430. struct timeval now;
  431. int fd, ret;
  432. struct avalon_info *info = avalon_info[avalon->device_id];
  433. avalon->device_fd = -1;
  434. fd = avalon_open(avalon->device_path,
  435. avalon_info[avalon->device_id]->baud);
  436. if (unlikely(fd == -1)) {
  437. applog(LOG_ERR, "Avalon: Failed to open on %s",
  438. avalon->device_path);
  439. return false;
  440. }
  441. ret = avalon_reset(fd, info->timeout, info->asic_count,
  442. info->miner_count);
  443. if (ret)
  444. return false;
  445. avalon->device_fd = fd;
  446. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  447. gettimeofday(&now, NULL);
  448. get_datestamp(avalon->init, &now);
  449. return true;
  450. }
  451. static void avalon_free_work(struct thr_info *thr, struct work **work)
  452. {
  453. struct cgpu_info *avalon;
  454. struct avalon_info *info;
  455. int avalon_get_work_count, i;
  456. if (!work)
  457. return;
  458. avalon = thr->cgpu;
  459. info = avalon_info[avalon->device_id];
  460. avalon_get_work_count = info->miner_count;
  461. for (i = 0; i < avalon_get_work_count; i++)
  462. if (work[i]) {
  463. free_work(work[i]);
  464. work[i] = NULL;
  465. }
  466. }
  467. static int64_t avalon_scanhash(struct thr_info *thr, struct work **bulk_work,
  468. __maybe_unused int64_t max_nonce)
  469. {
  470. struct cgpu_info *avalon;
  471. int fd;
  472. int ret;
  473. int full;
  474. struct avalon_info *info;
  475. struct avalon_task at;
  476. struct avalon_result ar;
  477. static struct work *bulk0[AVALON_DEFAULT_MINER_NUM] = {
  478. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  479. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  480. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  481. static struct work *bulk1[AVALON_DEFAULT_MINER_NUM] = {
  482. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  483. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  484. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  485. static struct work *bulk2[AVALON_DEFAULT_MINER_NUM] = {
  486. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  487. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  488. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  489. struct work **work = NULL;
  490. int i, work_i0, work_i1, work_i2;
  491. int avalon_get_work_count;
  492. uint32_t nonce;
  493. int64_t hash_count;
  494. struct timeval tv_start, tv_finish, elapsed;
  495. int curr_hw_errors;
  496. bool was_hw_error;
  497. int64_t estimate_hashes;
  498. avalon = thr->cgpu;
  499. info = avalon_info[avalon->device_id];
  500. avalon_get_work_count = info->miner_count;
  501. if (avalon->device_fd == -1)
  502. if (!avalon_prepare(thr)) {
  503. applog(LOG_ERR, "AVA%i: Comms error",
  504. avalon->device_id);
  505. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  506. /* fail the device if the reopen attempt fails */
  507. return -1;
  508. }
  509. fd = avalon->device_fd;
  510. #ifndef WIN32
  511. tcflush(fd, TCOFLUSH);
  512. #endif
  513. work = bulk_work;
  514. for (i = 0; i < avalon_get_work_count; i++) {
  515. bulk0[i] = bulk1[i];
  516. bulk1[i] = bulk2[i];
  517. bulk2[i] = work[i];
  518. applog(LOG_DEBUG, "Avalon: bulk0/1/2 buffer [%d]: %p, %p, %p",
  519. i, bulk0[i], bulk1[i], bulk2[i]);
  520. }
  521. i = 0;
  522. while (true) {
  523. avalon_init_task(thr, &at, 0, 0, 0, 0, 0, 0);
  524. avalon_create_task(&at, work[i]);
  525. ret = avalon_send_task(fd, &at, thr);
  526. if (ret == AVA_SEND_ERROR ||
  527. (ret == AVA_SEND_BUFFER_EMPTY &&
  528. (i + 1 == avalon_get_work_count))) {
  529. avalon_free_work(thr, bulk0);
  530. avalon_free_work(thr, bulk1);
  531. avalon_free_work(thr, bulk2);
  532. do_avalon_close(thr);
  533. applog(LOG_ERR, "AVA%i: Comms error",
  534. avalon->device_id);
  535. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  536. sleep(1);
  537. return 0; /* This should never happen */
  538. }
  539. work[i]->blk.nonce = 0xffffffff;
  540. if (ret == AVA_SEND_BUFFER_FULL)
  541. break;
  542. i++;
  543. }
  544. elapsed.tv_sec = elapsed.tv_usec = 0;
  545. gettimeofday(&tv_start, NULL);
  546. while(true) {
  547. full = avalon_buffer_full(fd);
  548. applog(LOG_DEBUG, "Avalon: Buffer full: %s",
  549. ((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
  550. if (full == AVA_BUFFER_EMPTY)
  551. break;
  552. work_i0 = work_i1 = work_i2 = -1;
  553. ret = avalon_get_result(fd, &ar, thr, &tv_finish);
  554. if (ret == AVA_GETS_ERROR) {
  555. avalon_free_work(thr, bulk0);
  556. avalon_free_work(thr, bulk1);
  557. avalon_free_work(thr, bulk2);
  558. do_avalon_close(thr);
  559. applog(LOG_ERR,
  560. "AVA%i: Comms error", avalon->device_id);
  561. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  562. return 0;
  563. }
  564. /* aborted before becoming idle, get new work */
  565. if (ret == AVA_GETS_TIMEOUT) {
  566. timersub(&tv_finish, &tv_start, &elapsed);
  567. estimate_hashes = ((double)(elapsed.tv_sec) +
  568. ((double)(elapsed.tv_usec)) /
  569. ((double)1000000)) / info->Hs;
  570. /* If Serial-USB delay allowed the full nonce range to
  571. * complete it can't have done more than a full nonce
  572. */
  573. if (unlikely(estimate_hashes > 0xffffffff))
  574. estimate_hashes = 0xffffffff;
  575. applog(LOG_DEBUG,
  576. "Avalon: no nonce = 0x%08llx hashes "
  577. "(%ld.%06lds)",
  578. estimate_hashes, elapsed.tv_sec,
  579. elapsed.tv_usec);
  580. continue;
  581. }
  582. if (ret == AVA_GETS_RESTART) {
  583. avalon_free_work(thr, bulk0);
  584. avalon_free_work(thr, bulk1);
  585. avalon_free_work(thr, bulk2);
  586. continue;
  587. }
  588. avalon->temp = ar.temp0;
  589. work_i0 = avalon_decode_nonce(thr, bulk0, &ar, &nonce);
  590. work_i1 = avalon_decode_nonce(thr, bulk1, &ar, &nonce);
  591. work_i2 = avalon_decode_nonce(thr, bulk2, &ar, &nonce);
  592. curr_hw_errors = avalon->hw_errors;
  593. if (work_i0 >= 0)
  594. submit_nonce(thr, bulk0[work_i0], nonce);
  595. if (work_i1 >= 0)
  596. submit_nonce(thr, bulk1[work_i1], nonce);
  597. if (work_i2 >= 0)
  598. submit_nonce(thr, bulk2[work_i2], nonce);
  599. was_hw_error = (curr_hw_errors > avalon->hw_errors);
  600. /* Force a USB close/reopen on any hw error */
  601. if (was_hw_error)
  602. do_avalon_close(thr);
  603. hash_count = nonce;
  604. hash_count++;
  605. hash_count *= info->asic_count;
  606. }
  607. avalon_free_work(thr, bulk0);
  608. if (opt_debug) {
  609. timersub(&tv_finish, &tv_start, &elapsed);
  610. applog(LOG_DEBUG,
  611. "Avalon: nonce = 0x%08x = 0x%08llx hashes "
  612. "(%ld.%06lds)",
  613. nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  614. }
  615. applog(LOG_ERR,
  616. "Avalon: Fan1: %d, Fan2: %d, Fan3: %d. Temp1: %d, Temp2: %d, Temp3: %d",
  617. ar.fan0, ar.fan1, ar.fan2, ar.temp0, ar.temp1, ar.temp2);
  618. return hash_count;
  619. }
  620. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  621. {
  622. struct api_data *root = NULL;
  623. struct avalon_info *info = avalon_info[cgpu->device_id];
  624. root = api_add_int(root, "read_count", &(info->read_count), false);
  625. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  626. root = api_add_int(root, "baud", &(info->baud), false);
  627. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  628. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  629. return root;
  630. }
  631. static void avalon_shutdown(struct thr_info *thr)
  632. {
  633. do_avalon_close(thr);
  634. }
  635. struct device_api avalon_api = {
  636. .dname = "avalon",
  637. .name = "AVA",
  638. .api_detect = avalon_detect,
  639. .thread_prepare = avalon_prepare,
  640. .scanhash_queue = avalon_scanhash,
  641. .get_api_stats = avalon_api_stats,
  642. .thread_shutdown = avalon_shutdown,
  643. };