driver-avalon.c 19 KB

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