driver-avalon.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. * Copyright 2012 2013 Xiangfu <xiangfu@openmobilefree.com>
  4. * Copyright 2012 Andrew Smith
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. /*
  12. * Those code should be works fine with V2 and V3 bitstream of Avalon.
  13. * Operation:
  14. * No detection implement.
  15. * Input: 64B = 32B midstate + 20B fill bytes + last 12 bytes of block head.
  16. * Return: send back 32bits immediately when Avalon found a valid nonce.
  17. * no query protocol implemented here, if no data send back in ~11.3
  18. * seconds (full cover time on 32bit nonce range by 380MH/s speed)
  19. * just send another work.
  20. * Notice:
  21. * 1. Avalon will start calculate when you push a work to them, even they
  22. * are busy.
  23. * 2. The 2 FPGAs on Avalon will distribute the job, one will calculate the
  24. * 0 ~ 7FFFFFFF, another one will cover the 80000000 ~ FFFFFFFF.
  25. * 3. It's possible for 2 FPGAs both find valid nonce in the meantime, the 2
  26. * valid nonce will all be send back.
  27. * 4. Avalon will stop work when: a valid nonce has been found or 32 bits
  28. * nonce range is completely calculated.
  29. */
  30. #include "config.h"
  31. #include <limits.h>
  32. #include <pthread.h>
  33. #include <stdio.h>
  34. #include <sys/time.h>
  35. #include <sys/types.h>
  36. #include <dirent.h>
  37. #include <unistd.h>
  38. #ifndef WIN32
  39. #include <termios.h>
  40. #include <sys/stat.h>
  41. #include <fcntl.h>
  42. #ifndef O_CLOEXEC
  43. #define O_CLOEXEC 0
  44. #endif
  45. #else
  46. #include <windows.h>
  47. #include <io.h>
  48. #endif
  49. #include "elist.h"
  50. #include "miner.h"
  51. #include "fpgautils.h"
  52. #include "driver-avalon.h"
  53. static struct timeval history_sec = { HISTORY_SEC, 0 };
  54. static const char *MODE_DEFAULT_STR = "default";
  55. static const char *MODE_SHORT_STR = "short";
  56. static const char *MODE_LONG_STR = "long";
  57. static const char *MODE_VALUE_STR = "value";
  58. static const char *MODE_UNKNOWN_STR = "unknown";
  59. // One for each possible device
  60. static struct AVALON_INFO **avalon_info;
  61. // Looking for options in --avalon-timing and --avalon-options:
  62. //
  63. // Code increments this each time we start to look at a device
  64. // However, this means that if other devices are checked by
  65. // the Avalon code (e.g. BFL) they will count in the option offset
  66. //
  67. // This, however, is deterministic so that's OK
  68. //
  69. // If we were to increment after successfully finding an Avalon
  70. // that would be random since an Avalon may fail and thus we'd
  71. // not be able to predict the option order
  72. //
  73. // This also assumes that serial_detect() checks them sequentially
  74. // and in the order specified on the command line
  75. //
  76. static int option_offset = -1;
  77. struct device_api avalon_api;
  78. static void rev(uint8_t *s, size_t l)
  79. {
  80. size_t i, j;
  81. uint8_t t;
  82. for (i = 0, j = l - 1; i < j; i++, j--) {
  83. t = s[i];
  84. s[i] = s[j];
  85. s[j] = t;
  86. }
  87. }
  88. static inline void avalon_create_task(uint8_t *ob_bin, struct work *work)
  89. {
  90. memset(ob_bin, 0, sizeof(ob_bin));
  91. memcpy(ob_bin, work->midstate, 32);
  92. memcpy(ob_bin + 52, work->data + 64, 12);
  93. rev(ob_bin, 32);
  94. rev(ob_bin + 52, 12);
  95. }
  96. static int avalon_gets(uint8_t *buf, int fd, struct timeval *tv_finish,
  97. struct thr_info *thr, int read_count)
  98. {
  99. ssize_t ret = 0;
  100. int rc = 0;
  101. int read_amount = AVALON_READ_SIZE;
  102. bool first = true;
  103. /* FIXME: we should set RTS to 0 and CTS to be 1, before read? */
  104. int done = avalon_task_done(fd);
  105. if (opt_debug)
  106. applog(LOG_DEBUG, "Avalon: finished all task?: %d", done);
  107. if (done) {
  108. ;/* TODO: return here. and tell avalon all task are done */
  109. }
  110. // Read reply 1 byte at a time to get earliest tv_finish
  111. while (true) {
  112. /* FIXME: should be remove!!! */
  113. if (opt_debug) {
  114. applog(LOG_DEBUG,
  115. "Avalon Read: times: %d, %.2f", rc,
  116. (float)rc/(float)TIME_FACTOR);
  117. }
  118. ret = read(fd, buf, 1);
  119. if (ret < 0)
  120. return AVA_GETS_ERROR;
  121. if (first)
  122. gettimeofday(tv_finish, NULL);
  123. if (ret >= read_amount)
  124. return AVA_GETS_OK;
  125. if (ret > 0) {
  126. buf += ret;
  127. read_amount -= ret;
  128. first = false;
  129. continue;
  130. }
  131. rc++;
  132. if (rc >= read_count) {
  133. if (opt_debug) {
  134. applog(LOG_DEBUG,
  135. "Avalon Read: No data in %.2f seconds",
  136. (float)rc/(float)TIME_FACTOR);
  137. }
  138. return AVA_GETS_TIMEOUT;
  139. }
  140. if (thr && thr->work_restart) {
  141. if (opt_debug) {
  142. applog(LOG_DEBUG,
  143. "Avalon Read: Work restart at %.2f seconds",
  144. (float)(rc)/(float)TIME_FACTOR);
  145. }
  146. return AVA_GETS_RESTART;
  147. }
  148. }
  149. }
  150. static int avalon_get_result(uint8_t *nonce_bin, int fd,
  151. struct timeval *tv_finish, struct thr_info *thr)
  152. {
  153. struct cgpu_info *avalon = thr->cgpu;
  154. struct AVALON_INFO *info = avalon_info[avalon->device_id];
  155. int ret;
  156. memset(nonce_bin, 0, sizeof(nonce_bin));
  157. ret = avalon_gets(nonce_bin, fd, tv_finish, thr, info->read_count);
  158. return ret;
  159. }
  160. static int avalon_decode_nonce(struct work **work, uint32_t *nonce,
  161. uint8_t *nonce_bin)
  162. {
  163. /* FIXME: Avalon return: reserved_nonce_midstate_data, */
  164. /* FIXME: should be modify to avalon data format */
  165. memcpy((uint8_t *)nonce, nonce_bin, sizeof(nonce_bin));
  166. #if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
  167. *nonce = swab32(*nonce);
  168. #endif
  169. /* TODO: find the nonce work, return index */
  170. return 0;
  171. }
  172. static int avalon_send_task(int fd, const void *buf, size_t bufLen)
  173. {
  174. size_t ret;
  175. /* FIXME: we should set RTS to 1 and wait CTS became 1, before write? */
  176. int empty = avalon_buffer_empty(fd);
  177. if (empty < 0)
  178. return AVA_SEND_ERROR;
  179. if (!empty) {
  180. /* FIXME: the buffer was full; return AVA_SEND_FULL; */
  181. }
  182. ret = write(fd, buf, bufLen);
  183. if (unlikely(ret != bufLen))
  184. return AVA_SEND_ERROR;
  185. /* From the document. avalon needs some time space between two write */
  186. struct timespec p;
  187. p.tv_sec = 0;
  188. p.tv_nsec = 5 * 1000;
  189. nanosleep(&p, NULL);
  190. return AVA_SEND_OK;
  191. }
  192. #define avalon_close(fd) close(fd)
  193. static void do_avalon_close(struct thr_info *thr)
  194. {
  195. struct cgpu_info *avalon = thr->cgpu;
  196. avalon_close(avalon->device_fd);
  197. avalon->device_fd = -1;
  198. }
  199. static const char *timing_mode_str(enum timing_mode timing_mode)
  200. {
  201. switch(timing_mode) {
  202. case MODE_DEFAULT:
  203. return MODE_DEFAULT_STR;
  204. case MODE_SHORT:
  205. return MODE_SHORT_STR;
  206. case MODE_LONG:
  207. return MODE_LONG_STR;
  208. case MODE_VALUE:
  209. return MODE_VALUE_STR;
  210. default:
  211. return MODE_UNKNOWN_STR;
  212. }
  213. }
  214. static void set_timing_mode(int this_option_offset, struct cgpu_info *avalon)
  215. {
  216. struct AVALON_INFO *info = avalon_info[avalon->device_id];
  217. double Hs;
  218. char buf[BUFSIZ+1];
  219. char *ptr, *comma, *eq;
  220. size_t max;
  221. int i;
  222. if (opt_icarus_timing == NULL)
  223. buf[0] = '\0';
  224. else {
  225. ptr = opt_icarus_timing;
  226. for (i = 0; i < this_option_offset; i++) {
  227. comma = strchr(ptr, ',');
  228. if (comma == NULL)
  229. break;
  230. ptr = comma + 1;
  231. }
  232. comma = strchr(ptr, ',');
  233. if (comma == NULL)
  234. max = strlen(ptr);
  235. else
  236. max = comma - ptr;
  237. if (max > BUFSIZ)
  238. max = BUFSIZ;
  239. strncpy(buf, ptr, max);
  240. buf[max] = '\0';
  241. }
  242. info->Hs = 0;
  243. info->read_count = 0;
  244. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  245. info->Hs = AVALON_REV3_HASH_TIME;
  246. info->read_count = AVALON_READ_COUNT_TIMING;
  247. info->timing_mode = MODE_SHORT;
  248. info->do_avalon_timing = true;
  249. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  250. info->Hs = AVALON_REV3_HASH_TIME;
  251. info->read_count = AVALON_READ_COUNT_TIMING;
  252. info->timing_mode = MODE_LONG;
  253. info->do_avalon_timing = true;
  254. } else if ((Hs = atof(buf)) != 0) {
  255. info->Hs = Hs / NANOSEC;
  256. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  257. if ((eq = strchr(buf, '=')) != NULL)
  258. info->read_count = atoi(eq+1);
  259. if (info->read_count < 1)
  260. info->read_count =
  261. (int)(info->fullnonce * TIME_FACTOR) - 1;
  262. if (unlikely(info->read_count < 1))
  263. info->read_count = 1;
  264. info->timing_mode = MODE_VALUE;
  265. info->do_avalon_timing = false;
  266. } else {
  267. // Anything else in buf just uses DEFAULT mode
  268. info->Hs = AVALON_REV3_HASH_TIME;
  269. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  270. if ((eq = strchr(buf, '=')) != NULL)
  271. info->read_count = atoi(eq+1);
  272. if (info->read_count < 1)
  273. info->read_count =
  274. (int)(info->fullnonce * TIME_FACTOR) - 1;
  275. info->timing_mode = MODE_DEFAULT;
  276. info->do_avalon_timing = false;
  277. }
  278. info->min_data_count = MIN_DATA_COUNT;
  279. applog(LOG_DEBUG, "Avalon: Init: %d mode=%s read_count=%d Hs=%e",
  280. avalon->device_id, timing_mode_str(info->timing_mode),
  281. info->read_count, info->Hs);
  282. }
  283. static uint32_t mask(int work_division)
  284. {
  285. char err_buf[BUFSIZ+1];
  286. uint32_t nonce_mask = 0x7fffffff;
  287. // yes we can calculate these,
  288. // but this way it's easy to see what they are
  289. switch (work_division) {
  290. case 1:
  291. nonce_mask = 0xffffffff;
  292. break;
  293. case 2:
  294. nonce_mask = 0x7fffffff;
  295. break;
  296. case 4:
  297. nonce_mask = 0x3fffffff;
  298. break;
  299. case 8:
  300. nonce_mask = 0x1fffffff;
  301. break;
  302. default:
  303. sprintf(err_buf,
  304. "Invalid2 avalon-options for work_division (%d)"
  305. " must be 1, 2, 4 or 8", work_division);
  306. quit(1, err_buf);
  307. }
  308. return nonce_mask;
  309. }
  310. static void get_options(int this_option_offset, int *baud, int *work_division,
  311. int *asic_count)
  312. {
  313. char err_buf[BUFSIZ+1];
  314. char buf[BUFSIZ+1];
  315. char *ptr, *comma, *colon, *colon2;
  316. size_t max;
  317. int i, tmp;
  318. if (opt_icarus_options == NULL)
  319. buf[0] = '\0';
  320. else {
  321. ptr = opt_icarus_options;
  322. for (i = 0; i < this_option_offset; i++) {
  323. comma = strchr(ptr, ',');
  324. if (comma == NULL)
  325. break;
  326. ptr = comma + 1;
  327. }
  328. comma = strchr(ptr, ',');
  329. if (comma == NULL)
  330. max = strlen(ptr);
  331. else
  332. max = comma - ptr;
  333. if (max > BUFSIZ)
  334. max = BUFSIZ;
  335. strncpy(buf, ptr, max);
  336. buf[max] = '\0';
  337. }
  338. *baud = AVALON_IO_SPEED;
  339. *work_division = 2;
  340. *asic_count = 2;
  341. if (*buf) {
  342. colon = strchr(buf, ':');
  343. if (colon)
  344. *(colon++) = '\0';
  345. if (*buf) {
  346. tmp = atoi(buf);
  347. switch (tmp) {
  348. case 115200:
  349. *baud = 115200;
  350. break;
  351. case 57600:
  352. *baud = 57600;
  353. break;
  354. default:
  355. sprintf(err_buf,
  356. "Invalid avalon-options for baud (%s) "
  357. "must be 115200 or 57600", buf);
  358. quit(1, err_buf);
  359. }
  360. }
  361. if (colon && *colon) {
  362. colon2 = strchr(colon, ':');
  363. if (colon2)
  364. *(colon2++) = '\0';
  365. if (*colon) {
  366. tmp = atoi(colon);
  367. if (tmp == 1 || tmp == 2 ||
  368. tmp == 4 || tmp == 8) {
  369. *work_division = tmp;
  370. // default to the same
  371. *asic_count = tmp;
  372. } else {
  373. sprintf(err_buf,
  374. "Invalid avalon-options for "
  375. "work_division (%s) must be 1,"
  376. " 2, 4 or 8", colon);
  377. quit(1, err_buf);
  378. }
  379. }
  380. if (colon2 && *colon2) {
  381. tmp = atoi(colon2);
  382. if (tmp > 0 && tmp <= *work_division)
  383. *asic_count = tmp;
  384. else {
  385. sprintf(err_buf,
  386. "Invalid avalon-options for "
  387. "asic_count (%s) must be >0 "
  388. "and <=work_division (%d)",
  389. colon2, *work_division);
  390. quit(1, err_buf);
  391. }
  392. }
  393. }
  394. }
  395. }
  396. static bool avalon_detect_one(const char *devpath)
  397. {
  398. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  399. // N.B. golden_ob MUST take less time to calculate
  400. // than the timeout set in avalon_open()
  401. // This one takes ~0.53ms on Rev3 Avalon
  402. const char golden_ob[] =
  403. "4679ba4ec99876bf4bfe086082b40025"
  404. "4df6c356451471139a3afa71e48f544a"
  405. "00000000000000000000000000000000"
  406. "0000000087320b1a1426674f2fa722ce";
  407. const char golden_nonce[] = "000187a2";
  408. const uint32_t golden_nonce_val = 0x000187a2;
  409. uint8_t ob_bin[64], nonce_bin[AVALON_READ_SIZE];
  410. char *nonce_hex;
  411. struct AVALON_INFO *info;
  412. struct timeval tv_start, tv_finish;
  413. int fd;
  414. int baud, work_division, asic_count;
  415. int this_option_offset = ++option_offset;
  416. get_options(this_option_offset, &baud, &work_division, &asic_count);
  417. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s", devpath);
  418. fd = avalon_open2(devpath, baud, true);
  419. if (unlikely(fd == -1)) {
  420. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  421. return false;
  422. }
  423. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  424. avalon_send_task(fd, ob_bin, sizeof(ob_bin));
  425. gettimeofday(&tv_start, NULL);
  426. memset(nonce_bin, 0, sizeof(nonce_bin));
  427. /* FIXME: how much time on avalon finish reset */
  428. avalon_gets(nonce_bin, fd, &tv_finish, NULL, 10/* set to 1s now */);
  429. avalon_close(fd);
  430. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  431. if (strncmp(nonce_hex, golden_nonce, 8)) {
  432. applog(LOG_ERR,
  433. "Avalon Detect: "
  434. "Test failed at %s: get %s, should: %s",
  435. devpath, nonce_hex, golden_nonce);
  436. free(nonce_hex);
  437. return false;
  438. }
  439. applog(LOG_DEBUG,
  440. "Avalon Detect: Test succeeded at %s: got %s",
  441. devpath, nonce_hex);
  442. free(nonce_hex);
  443. /* We have a real Avalon! */
  444. struct cgpu_info *avalon;
  445. avalon = calloc(1, sizeof(struct cgpu_info));
  446. avalon->api = &avalon_api;
  447. avalon->device_path = strdup(devpath);
  448. avalon->device_fd = -1;
  449. avalon->threads = AVALON_MINER_THREADS;
  450. add_cgpu(avalon);
  451. avalon_info = realloc(avalon_info,
  452. sizeof(struct AVALON_INFO *) *
  453. (total_devices + 1));
  454. applog(LOG_INFO, "Found Avalon at %s, mark as %d",
  455. devpath, avalon->device_id);
  456. applog(LOG_DEBUG,
  457. "Avalon: Init: %d baud=%d work_division=%d asic_count=%d",
  458. avalon->device_id, baud, work_division, asic_count);
  459. // Since we are adding a new device on the end it
  460. // needs to always be allocated
  461. avalon_info[avalon->device_id] = (struct AVALON_INFO *)
  462. malloc(sizeof(struct AVALON_INFO));
  463. if (unlikely(!(avalon_info[avalon->device_id])))
  464. quit(1, "Failed to malloc AVALON_INFO");
  465. info = avalon_info[avalon->device_id];
  466. // Initialise everything to zero for a new device
  467. memset(info, 0, sizeof(struct AVALON_INFO));
  468. info->baud = baud;
  469. info->work_division = work_division;
  470. info->asic_count = asic_count;
  471. info->nonce_mask = mask(work_division);
  472. info->golden_hashes =
  473. (golden_nonce_val & info->nonce_mask) * asic_count;
  474. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  475. set_timing_mode(this_option_offset, avalon);
  476. return true;
  477. }
  478. static inline void avalon_detect()
  479. {
  480. serial_detect(&avalon_api, avalon_detect_one);
  481. }
  482. static bool avalon_prepare(struct thr_info *thr)
  483. {
  484. struct cgpu_info *avalon = thr->cgpu;
  485. struct timeval now;
  486. int fd;
  487. avalon->device_fd = -1;
  488. fd = avalon_open(avalon->device_path,
  489. avalon_info[avalon->device_id]->baud);
  490. if (unlikely(fd == -1)) {
  491. applog(LOG_ERR, "Failed to open Avalon on %s",
  492. avalon->device_path);
  493. return false;
  494. }
  495. avalon->device_fd = fd;
  496. applog(LOG_INFO, "Opened Avalon on %s", avalon->device_path);
  497. gettimeofday(&now, NULL);
  498. get_datestamp(avalon->init, &now);
  499. return true;
  500. }
  501. static int64_t avalon_scanhash(struct thr_info *thr, struct work **work,
  502. __maybe_unused int64_t max_nonce)
  503. {
  504. struct cgpu_info *avalon;
  505. int fd;
  506. int ret;
  507. struct AVALON_INFO *info;
  508. uint8_t ob_bin[64], nonce_bin[AVALON_READ_SIZE];
  509. char *ob_hex;
  510. uint32_t nonce;
  511. int64_t hash_count;
  512. int i, work_i;
  513. int read_count;
  514. int count;
  515. struct timeval tv_start, tv_finish, elapsed;
  516. struct timeval tv_history_start, tv_history_finish;
  517. double Ti, Xi;
  518. int curr_hw_errors;
  519. bool was_hw_error;
  520. struct AVALON_HISTORY *history0, *history;
  521. double Hs, W, fullnonce;
  522. int64_t estimate_hashes;
  523. uint32_t values;
  524. int64_t hash_count_range;
  525. avalon = thr->cgpu;
  526. info = avalon_info[avalon->device_id];
  527. if (avalon->device_fd == -1)
  528. if (!avalon_prepare(thr)) {
  529. applog(LOG_ERR, "AVA%i: Comms error",
  530. avalon->device_id);
  531. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  532. // fail the device if the reopen attempt fails
  533. return -1;
  534. }
  535. fd = avalon->device_fd;
  536. #ifndef WIN32
  537. tcflush(fd, TCOFLUSH);
  538. #endif
  539. /* Write task to device one by one */
  540. for (i = 0; i < AVALON_GET_WORK_COUNT; i++) {
  541. avalon_create_task(ob_bin, work[i]);
  542. ret = avalon_send_task(fd, ob_bin, sizeof(ob_bin));
  543. if (opt_debug) {
  544. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  545. applog(LOG_DEBUG, "Avalon %d sent: %s",
  546. avalon->device_id, ob_hex);
  547. free(ob_hex);
  548. }
  549. if (ret == AVA_SEND_ERROR) {
  550. do_avalon_close(thr);
  551. applog(LOG_ERR, "AVA%i: Comms error",
  552. avalon->device_id);
  553. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  554. return 0; /* This should never happen */
  555. }
  556. }
  557. elapsed.tv_sec = elapsed.tv_usec = 0;
  558. gettimeofday(&tv_start, NULL);
  559. /* count may != AVALON_GET_WORK_COUNT */
  560. for (i = 0; i < AVALON_GET_WORK_COUNT; i++) {
  561. ret = avalon_get_result(nonce_bin, fd, &tv_finish, thr);
  562. if (ret == AVA_GETS_ERROR ) {
  563. do_avalon_close(thr);
  564. applog(LOG_ERR, "AVA%i: Comms error", avalon->device_id);
  565. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  566. return 0;
  567. }
  568. // aborted before becoming idle, get new work
  569. if (ret == AVA_GETS_TIMEOUT || ret == AVA_GETS_RESTART) {
  570. timersub(&tv_finish, &tv_start, &elapsed);
  571. // ONLY up to just when it aborted
  572. // We didn't read a reply so we don't subtract AVALON_READ_TIME
  573. estimate_hashes = ((double)(elapsed.tv_sec) +
  574. ((double)(elapsed.tv_usec)) /
  575. ((double)1000000)) / info->Hs;
  576. // If some Serial-USB delay allowed the full nonce range to
  577. // complete it can't have done more than a full nonce
  578. if (unlikely(estimate_hashes > 0xffffffff))
  579. estimate_hashes = 0xffffffff;
  580. if (opt_debug) {
  581. applog(LOG_DEBUG,
  582. "Avalon %d no nonce = 0x%08llx hashes "
  583. "(%ld.%06lds)",
  584. avalon->device_id, estimate_hashes,
  585. elapsed.tv_sec, elapsed.tv_usec);
  586. }
  587. return estimate_hashes;
  588. }
  589. work_i = avalon_decode_nonce(work, &nonce, nonce_bin);
  590. /* FIXME: Should be a check on return, no work_i maybe hardware error */
  591. work[work_i]->blk.nonce = 0xffffffff;
  592. curr_hw_errors = avalon->hw_errors;
  593. submit_nonce(thr, work[work_i], nonce);
  594. was_hw_error = (curr_hw_errors > avalon->hw_errors);
  595. // Force a USB close/reopen on any hw error
  596. if (was_hw_error)
  597. do_avalon_close(thr);
  598. hash_count = (nonce & info->nonce_mask);
  599. hash_count++;
  600. hash_count *= info->asic_count;
  601. }
  602. if (opt_debug || info->do_avalon_timing)
  603. timersub(&tv_finish, &tv_start, &elapsed);
  604. if (opt_debug) {
  605. applog(LOG_DEBUG,
  606. "Avalon %d nonce = 0x%08x = 0x%08llx hashes "
  607. "(%ld.%06lds)",
  608. avalon->device_id, nonce, hash_count,
  609. elapsed.tv_sec, elapsed.tv_usec);
  610. }
  611. // ignore possible end condition values ... and hw errors
  612. if (info->do_avalon_timing
  613. && !was_hw_error
  614. && ((nonce & info->nonce_mask) > END_CONDITION)
  615. && ((nonce & info->nonce_mask) <
  616. (info->nonce_mask & ~END_CONDITION))) {
  617. gettimeofday(&tv_history_start, NULL);
  618. history0 = &(info->history[0]);
  619. if (history0->values == 0)
  620. timeradd(&tv_start, &history_sec, &(history0->finish));
  621. Ti = (double)(elapsed.tv_sec)
  622. + ((double)(elapsed.tv_usec))/((double)1000000)
  623. - ((double)AVALON_READ_TIME(info->baud));
  624. Xi = (double)hash_count;
  625. history0->sumXiTi += Xi * Ti;
  626. history0->sumXi += Xi;
  627. history0->sumTi += Ti;
  628. history0->sumXi2 += Xi * Xi;
  629. history0->values++;
  630. if (history0->hash_count_max < hash_count)
  631. history0->hash_count_max = hash_count;
  632. if (history0->hash_count_min > hash_count ||
  633. history0->hash_count_min == 0)
  634. history0->hash_count_min = hash_count;
  635. if (history0->values >= info->min_data_count
  636. && timercmp(&tv_start, &(history0->finish), >)) {
  637. for (i = INFO_HISTORY; i > 0; i--)
  638. memcpy(&(info->history[i]),
  639. &(info->history[i-1]),
  640. sizeof(struct AVALON_HISTORY));
  641. // Initialise history0 to zero for summary calculation
  642. memset(history0, 0, sizeof(struct AVALON_HISTORY));
  643. // We just completed a history data set
  644. // So now recalc read_count based on the
  645. // whole history thus we will
  646. // initially get more accurate until it
  647. // completes INFO_HISTORY
  648. // total data sets
  649. count = 0;
  650. for (i = 1 ; i <= INFO_HISTORY; i++) {
  651. history = &(info->history[i]);
  652. if (history->values >= MIN_DATA_COUNT) {
  653. count++;
  654. history0->sumXiTi += history->sumXiTi;
  655. history0->sumXi += history->sumXi;
  656. history0->sumTi += history->sumTi;
  657. history0->sumXi2 += history->sumXi2;
  658. history0->values += history->values;
  659. if (history0->hash_count_max < history->hash_count_max)
  660. history0->hash_count_max = history->hash_count_max;
  661. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  662. history0->hash_count_min = history->hash_count_min;
  663. }
  664. }
  665. // All history data
  666. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  667. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  668. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  669. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  670. values = history0->values;
  671. // Initialise history0 to zero for next data set
  672. memset(history0, 0, sizeof(struct AVALON_HISTORY));
  673. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  674. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  675. info->Hs = Hs;
  676. info->read_count = read_count;
  677. info->fullnonce = fullnonce;
  678. info->count = count;
  679. info->W = W;
  680. info->values = values;
  681. info->hash_count_range = hash_count_range;
  682. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  683. info->min_data_count *= 2;
  684. else if (info->timing_mode == MODE_SHORT)
  685. info->do_avalon_timing = false;
  686. // applog(LOG_WARNING, "Avalon %d Re-estimate: read_count=%d fullnonce=%fs history count=%d Hs=%e W=%e values=%d hash range=0x%08lx min data count=%u", avalon->device_id, read_count, fullnonce, count, Hs, W, values, hash_count_range, info->min_data_count);
  687. applog(LOG_WARNING, "Avalon %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  688. avalon->device_id, Hs, W, read_count, fullnonce);
  689. }
  690. info->history_count++;
  691. gettimeofday(&tv_history_finish, NULL);
  692. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  693. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  694. }
  695. return hash_count;
  696. }
  697. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  698. {
  699. struct api_data *root = NULL;
  700. struct AVALON_INFO *info = avalon_info[cgpu->device_id];
  701. // Warning, access to these is not locked - but we don't really
  702. // care since hashing performance is way more important than
  703. // locking access to displaying API debug 'stats'
  704. // If locking becomes an issue for any of them, use copy_data=true also
  705. root = api_add_int(root, "read_count", &(info->read_count), false);
  706. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  707. root = api_add_int(root, "count", &(info->count), false);
  708. root = api_add_hs(root, "Hs", &(info->Hs), false);
  709. root = api_add_double(root, "W", &(info->W), false);
  710. root = api_add_uint(root, "total_values", &(info->values), false);
  711. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  712. root = api_add_uint64(root, "history_count", &(info->history_count),
  713. false);
  714. root = api_add_timeval(root, "history_time", &(info->history_time),
  715. false);
  716. root = api_add_uint(root, "min_data_count", &(info->min_data_count),
  717. false);
  718. root = api_add_uint(root, "timing_values", &(info->history[0].values),
  719. false);
  720. root = api_add_const(root, "timing_mode",
  721. timing_mode_str(info->timing_mode), false);
  722. root = api_add_bool(root, "is_timing", &(info->do_avalon_timing),
  723. false);
  724. root = api_add_int(root, "baud", &(info->baud), false);
  725. root = api_add_int(root, "work_division", &(info->work_division),
  726. false);
  727. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  728. return root;
  729. }
  730. static void avalon_shutdown(struct thr_info *thr)
  731. {
  732. do_avalon_close(thr);
  733. }
  734. struct device_api avalon_api = {
  735. .dname = "avalon",
  736. .name = "AVA",
  737. .api_detect = avalon_detect,
  738. .get_api_stats = avalon_api_stats,
  739. .thread_prepare = avalon_prepare,
  740. .scanhash_queue = avalon_scanhash,
  741. .thread_shutdown = avalon_shutdown,
  742. };