driver-avalon.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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 inline 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 int avalon_gets(uint8_t *buf, int fd, struct timeval *tv_finish,
  89. struct thr_info *thr, int read_count)
  90. {
  91. ssize_t ret = 0;
  92. int rc = 0;
  93. int read_amount = AVALON_READ_SIZE;
  94. bool first = true;
  95. /* FIXME: we should set RTS to 0 and CTS to be 1, before read? */
  96. int done = avalon_task_done(fd);
  97. if (opt_debug)
  98. applog(LOG_DEBUG, "Avalon: Finished all task? %s", done ? "Yes" : "no");
  99. if (done) {
  100. /* FIXME: return here. ask new task */
  101. ;
  102. }
  103. /* Read reply 1 byte at a time to get earliest tv_finish */
  104. while (true) {
  105. ret = read(fd, buf, 1);
  106. if (ret < 0)
  107. return AVA_GETS_ERROR;
  108. if (first)
  109. gettimeofday(tv_finish, NULL);
  110. if (ret >= read_amount)
  111. return AVA_GETS_OK;
  112. if (ret > 0) {
  113. buf += ret;
  114. read_amount -= ret;
  115. first = false;
  116. continue;
  117. }
  118. rc++;
  119. if (rc >= read_count) {
  120. if (opt_debug) {
  121. applog(LOG_ERR,
  122. "Avalon: No data in %.2f seconds",
  123. (float)rc/(float)TIME_FACTOR);
  124. }
  125. return AVA_GETS_TIMEOUT;
  126. }
  127. if (thr && thr->work_restart) {
  128. if (opt_debug) {
  129. applog(LOG_ERR,
  130. "Avalon: Work restart at %.2f seconds",
  131. (float)(rc)/(float)TIME_FACTOR);
  132. }
  133. return AVA_GETS_RESTART;
  134. }
  135. }
  136. }
  137. static int avalon_get_result(uint8_t *nonce_bin, int fd,
  138. struct timeval *tv_finish, struct thr_info *thr)
  139. {
  140. struct cgpu_info *avalon = thr->cgpu;
  141. struct AVALON_INFO *info = avalon_info[avalon->device_id];
  142. int ret;
  143. memset(nonce_bin, 0, AVALON_READ_SIZE);
  144. ret = avalon_gets(nonce_bin, fd, tv_finish, thr, info->read_count);
  145. return ret;
  146. }
  147. static int avalon_decode_nonce(struct work **work, uint32_t *nonce,
  148. uint8_t *nonce_bin)
  149. {
  150. /* FIXME: should be modify to avalon data format */
  151. memcpy((uint8_t *)nonce, nonce_bin, AVALON_READ_SIZE);
  152. #if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
  153. *nonce = swab32(*nonce);
  154. #endif
  155. /* TODO: find the nonce work, return index */
  156. return 0;
  157. }
  158. static inline void avalon_create_task(uint8_t *ob_bin, struct work *work)
  159. {
  160. memset(ob_bin, 0, AVALON_WRITE_SIZE);
  161. memcpy(ob_bin, work->midstate, 32);
  162. memcpy(ob_bin + 52, work->data + 64, 12);
  163. rev(ob_bin, 32);
  164. rev(ob_bin + 52, 12);
  165. }
  166. static int avalon_send_task(int fd, const void *buf, size_t bufLen)
  167. {
  168. size_t ret;
  169. char *ob_hex = NULL;
  170. /* FIXME: we should set RTS to 1 and wait CTS became 1, before write? */
  171. int empty = avalon_buffer_empty(fd);
  172. if (empty < 0)
  173. return AVA_SEND_ERROR;
  174. if (!empty) {
  175. /* FIXME: the buffer was full; return AVA_SEND_FULL; */
  176. ;
  177. }
  178. if (opt_debug) {
  179. ob_hex = bin2hex(buf, bufLen);
  180. applog(LOG_DEBUG, "Avalon: Sent %s", ob_hex);
  181. free(ob_hex);
  182. }
  183. ret = write(fd, buf, bufLen);
  184. if (unlikely(ret != bufLen))
  185. return AVA_SEND_ERROR;
  186. /* From the document. avalon needs some time space between two write */
  187. struct timespec p;
  188. p.tv_sec = 0;
  189. /* FIXME: */
  190. p.tv_nsec = 0;
  191. nanosleep(&p, NULL);
  192. return AVA_SEND_OK;
  193. }
  194. static void do_avalon_close(struct thr_info *thr)
  195. {
  196. struct cgpu_info *avalon = thr->cgpu;
  197. avalon_close(avalon->device_fd);
  198. avalon->device_fd = -1;
  199. }
  200. static const char *timing_mode_str(enum timing_mode timing_mode)
  201. {
  202. switch(timing_mode) {
  203. case MODE_DEFAULT:
  204. return MODE_DEFAULT_STR;
  205. case MODE_SHORT:
  206. return MODE_SHORT_STR;
  207. case MODE_LONG:
  208. return MODE_LONG_STR;
  209. case MODE_VALUE:
  210. return MODE_VALUE_STR;
  211. default:
  212. return MODE_UNKNOWN_STR;
  213. }
  214. }
  215. static void set_timing_mode(int this_option_offset, struct cgpu_info *avalon)
  216. {
  217. struct AVALON_INFO *info = avalon_info[avalon->device_id];
  218. double Hs;
  219. char buf[BUFSIZ+1];
  220. char *ptr, *comma, *eq;
  221. size_t max;
  222. int i;
  223. if (opt_icarus_timing == NULL)
  224. buf[0] = '\0';
  225. else {
  226. ptr = opt_icarus_timing;
  227. for (i = 0; i < this_option_offset; i++) {
  228. comma = strchr(ptr, ',');
  229. if (comma == NULL)
  230. break;
  231. ptr = comma + 1;
  232. }
  233. comma = strchr(ptr, ',');
  234. if (comma == NULL)
  235. max = strlen(ptr);
  236. else
  237. max = comma - ptr;
  238. if (max > BUFSIZ)
  239. max = BUFSIZ;
  240. strncpy(buf, ptr, max);
  241. buf[max] = '\0';
  242. }
  243. info->Hs = 0;
  244. info->read_count = 0;
  245. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  246. info->Hs = AVALON_REV3_HASH_TIME;
  247. info->read_count = AVALON_READ_COUNT_TIMING;
  248. info->timing_mode = MODE_SHORT;
  249. info->do_avalon_timing = true;
  250. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  251. info->Hs = AVALON_REV3_HASH_TIME;
  252. info->read_count = AVALON_READ_COUNT_TIMING;
  253. info->timing_mode = MODE_LONG;
  254. info->do_avalon_timing = true;
  255. } else if ((Hs = atof(buf)) != 0) {
  256. info->Hs = Hs / NANOSEC;
  257. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  258. if ((eq = strchr(buf, '=')) != NULL)
  259. info->read_count = atoi(eq+1);
  260. if (info->read_count < 1)
  261. info->read_count =
  262. (int)(info->fullnonce * TIME_FACTOR) - 1;
  263. if (unlikely(info->read_count < 1))
  264. info->read_count = 1;
  265. info->timing_mode = MODE_VALUE;
  266. info->do_avalon_timing = false;
  267. } else {
  268. // Anything else in buf just uses DEFAULT mode
  269. info->Hs = AVALON_REV3_HASH_TIME;
  270. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  271. if ((eq = strchr(buf, '=')) != NULL)
  272. info->read_count = atoi(eq+1);
  273. if (info->read_count < 1)
  274. info->read_count =
  275. (int)(info->fullnonce * TIME_FACTOR) - 1;
  276. info->timing_mode = MODE_DEFAULT;
  277. info->do_avalon_timing = false;
  278. }
  279. info->min_data_count = MIN_DATA_COUNT;
  280. applog(LOG_DEBUG, "Avalon: Init: %d mode=%s read_count=%d Hs=%e",
  281. avalon->device_id, timing_mode_str(info->timing_mode),
  282. info->read_count, info->Hs);
  283. }
  284. static uint32_t mask(int work_division)
  285. {
  286. char err_buf[BUFSIZ+1];
  287. uint32_t nonce_mask = 0x7fffffff;
  288. // yes we can calculate these,
  289. // but this way it's easy to see what they are
  290. switch (work_division) {
  291. case 1:
  292. nonce_mask = 0xffffffff;
  293. break;
  294. case 2:
  295. nonce_mask = 0x7fffffff;
  296. break;
  297. case 4:
  298. nonce_mask = 0x3fffffff;
  299. break;
  300. case 8:
  301. nonce_mask = 0x1fffffff;
  302. break;
  303. default:
  304. sprintf(err_buf,
  305. "Invalid2 avalon-options for work_division (%d)"
  306. " must be 1, 2, 4 or 8", work_division);
  307. quit(1, err_buf);
  308. }
  309. return nonce_mask;
  310. }
  311. static void get_options(int this_option_offset, int *baud, int *work_division,
  312. int *asic_count)
  313. {
  314. char err_buf[BUFSIZ+1];
  315. char buf[BUFSIZ+1];
  316. char *ptr, *comma, *colon, *colon2;
  317. size_t max;
  318. int i, tmp;
  319. if (opt_icarus_options == NULL)
  320. buf[0] = '\0';
  321. else {
  322. ptr = opt_icarus_options;
  323. for (i = 0; i < this_option_offset; i++) {
  324. comma = strchr(ptr, ',');
  325. if (comma == NULL)
  326. break;
  327. ptr = comma + 1;
  328. }
  329. comma = strchr(ptr, ',');
  330. if (comma == NULL)
  331. max = strlen(ptr);
  332. else
  333. max = comma - ptr;
  334. if (max > BUFSIZ)
  335. max = BUFSIZ;
  336. strncpy(buf, ptr, max);
  337. buf[max] = '\0';
  338. }
  339. *baud = AVALON_IO_SPEED;
  340. *work_division = 2;
  341. *asic_count = 2;
  342. if (*buf) {
  343. colon = strchr(buf, ':');
  344. if (colon)
  345. *(colon++) = '\0';
  346. if (*buf) {
  347. tmp = atoi(buf);
  348. switch (tmp) {
  349. case 115200:
  350. *baud = 115200;
  351. break;
  352. case 57600:
  353. *baud = 57600;
  354. break;
  355. default:
  356. sprintf(err_buf,
  357. "Invalid avalon-options for baud (%s) "
  358. "must be 115200 or 57600", buf);
  359. quit(1, err_buf);
  360. }
  361. }
  362. if (colon && *colon) {
  363. colon2 = strchr(colon, ':');
  364. if (colon2)
  365. *(colon2++) = '\0';
  366. if (*colon) {
  367. tmp = atoi(colon);
  368. if (tmp == 1 || tmp == 2 ||
  369. tmp == 4 || tmp == 8) {
  370. *work_division = tmp;
  371. // default to the same
  372. *asic_count = tmp;
  373. } else {
  374. sprintf(err_buf,
  375. "Invalid avalon-options for "
  376. "work_division (%s) must be 1,"
  377. " 2, 4 or 8", colon);
  378. quit(1, err_buf);
  379. }
  380. }
  381. if (colon2 && *colon2) {
  382. tmp = atoi(colon2);
  383. if (tmp > 0 && tmp <= *work_division)
  384. *asic_count = tmp;
  385. else {
  386. sprintf(err_buf,
  387. "Invalid avalon-options for "
  388. "asic_count (%s) must be >0 "
  389. "and <=work_division (%d)",
  390. colon2, *work_division);
  391. quit(1, err_buf);
  392. }
  393. }
  394. }
  395. }
  396. }
  397. static bool avalon_detect_one(const char *devpath)
  398. {
  399. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  400. // N.B. golden_ob MUST take less time to calculate
  401. // than the timeout set in avalon_open()
  402. // This one takes ~0.53ms on Rev3 Avalon
  403. const char golden_ob[] =
  404. "4679ba4ec99876bf4bfe086082b40025"
  405. "4df6c356451471139a3afa71e48f544a"
  406. "00000000000000000000000000000000"
  407. "0000000087320b1a1426674f2fa722ce";
  408. const char golden_nonce[] = "000187a2";
  409. const uint32_t golden_nonce_val = 0x000187a2;
  410. uint8_t ob_bin[AVALON_WRITE_SIZE], nonce_bin[AVALON_READ_SIZE];
  411. char *nonce_hex;
  412. struct AVALON_INFO *info;
  413. struct timeval tv_start, tv_finish;
  414. int fd;
  415. int baud, work_division, asic_count;
  416. int this_option_offset = ++option_offset;
  417. get_options(this_option_offset, &baud, &work_division, &asic_count);
  418. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s", devpath);
  419. fd = avalon_open2(devpath, baud, true);
  420. if (unlikely(fd == -1)) {
  421. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  422. return false;
  423. }
  424. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  425. avalon_send_task(fd, ob_bin, sizeof(ob_bin));
  426. gettimeofday(&tv_start, NULL);
  427. memset(nonce_bin, 0, sizeof(nonce_bin));
  428. /* FIXME: how much time on avalon finish reset */
  429. avalon_gets(nonce_bin, fd, &tv_finish, NULL, 10/* set to 1s now */);
  430. avalon_close(fd);
  431. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  432. if (strncmp(nonce_hex, golden_nonce, 8)) {
  433. applog(LOG_ERR,
  434. "Avalon Detect: "
  435. "Test failed at %s: get %s, should: %s",
  436. devpath, nonce_hex, golden_nonce);
  437. free(nonce_hex);
  438. return false;
  439. }
  440. applog(LOG_DEBUG,
  441. "Avalon Detect: Test succeeded at %s: got %s",
  442. devpath, nonce_hex);
  443. free(nonce_hex);
  444. /* We have a real Avalon! */
  445. struct cgpu_info *avalon;
  446. avalon = calloc(1, sizeof(struct cgpu_info));
  447. avalon->api = &avalon_api;
  448. avalon->device_path = strdup(devpath);
  449. avalon->device_fd = -1;
  450. avalon->threads = AVALON_MINER_THREADS;
  451. add_cgpu(avalon);
  452. avalon_info = realloc(avalon_info,
  453. sizeof(struct AVALON_INFO *) *
  454. (total_devices + 1));
  455. applog(LOG_INFO, "Found Avalon at %s, mark as %d",
  456. devpath, avalon->device_id);
  457. applog(LOG_DEBUG,
  458. "Avalon: Init: %d baud=%d work_division=%d asic_count=%d",
  459. avalon->device_id, baud, work_division, asic_count);
  460. // Since we are adding a new device on the end it
  461. // needs to always be allocated
  462. avalon_info[avalon->device_id] = (struct AVALON_INFO *)
  463. malloc(sizeof(struct AVALON_INFO));
  464. if (unlikely(!(avalon_info[avalon->device_id])))
  465. quit(1, "Failed to malloc AVALON_INFO");
  466. info = avalon_info[avalon->device_id];
  467. // Initialise everything to zero for a new device
  468. memset(info, 0, sizeof(struct AVALON_INFO));
  469. info->baud = baud;
  470. info->work_division = work_division;
  471. info->asic_count = asic_count;
  472. info->nonce_mask = mask(work_division);
  473. info->golden_hashes =
  474. (golden_nonce_val & info->nonce_mask) * asic_count;
  475. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  476. set_timing_mode(this_option_offset, avalon);
  477. return true;
  478. }
  479. static inline void avalon_detect()
  480. {
  481. serial_detect(&avalon_api, avalon_detect_one);
  482. }
  483. static bool avalon_prepare(struct thr_info *thr)
  484. {
  485. struct cgpu_info *avalon = thr->cgpu;
  486. struct timeval now;
  487. int fd;
  488. avalon->device_fd = -1;
  489. fd = avalon_open(avalon->device_path,
  490. avalon_info[avalon->device_id]->baud);
  491. if (unlikely(fd == -1)) {
  492. applog(LOG_ERR, "Avalon: Failed to open on %s",
  493. avalon->device_path);
  494. return false;
  495. }
  496. avalon->device_fd = fd;
  497. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  498. gettimeofday(&now, NULL);
  499. get_datestamp(avalon->init, &now);
  500. return true;
  501. }
  502. static int64_t avalon_scanhash(struct thr_info *thr, struct work **work,
  503. __maybe_unused int64_t max_nonce)
  504. {
  505. struct cgpu_info *avalon;
  506. int fd;
  507. int ret;
  508. struct AVALON_INFO *info;
  509. uint8_t ob_bin[AVALON_WRITE_SIZE], nonce_bin[AVALON_READ_SIZE];
  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, AVALON_WRITE_SIZE);
  543. if (ret == AVA_SEND_ERROR) {
  544. do_avalon_close(thr);
  545. applog(LOG_ERR, "AVA%i: Comms error",
  546. avalon->device_id);
  547. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  548. return 0; /* This should never happen */
  549. }
  550. }
  551. elapsed.tv_sec = elapsed.tv_usec = 0;
  552. gettimeofday(&tv_start, NULL);
  553. /* count may != AVALON_GET_WORK_COUNT */
  554. for (i = 0; i < AVALON_GET_WORK_COUNT; i++) {
  555. ret = avalon_get_result(nonce_bin, fd, &tv_finish, thr);
  556. if (ret == AVA_GETS_ERROR ) {
  557. do_avalon_close(thr);
  558. applog(LOG_ERR, "AVA%i: Comms error", avalon->device_id);
  559. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  560. return 0;
  561. }
  562. work_i = avalon_decode_nonce(work, &nonce, nonce_bin);
  563. /* FIXME: Should be a check on return, no work_i maybe hardware error */
  564. work[work_i]->blk.nonce = 0xffffffff;
  565. // aborted before becoming idle, get new work
  566. if (ret == AVA_GETS_TIMEOUT || ret == AVA_GETS_RESTART) {
  567. timersub(&tv_finish, &tv_start, &elapsed);
  568. // ONLY up to just when it aborted
  569. // We didn't read a reply so we don't subtract AVALON_READ_TIME
  570. estimate_hashes = ((double)(elapsed.tv_sec) +
  571. ((double)(elapsed.tv_usec)) /
  572. ((double)1000000)) / info->Hs;
  573. // If some Serial-USB delay allowed the full nonce range to
  574. // complete it can't have done more than a full nonce
  575. if (unlikely(estimate_hashes > 0xffffffff))
  576. estimate_hashes = 0xffffffff;
  577. if (opt_debug) {
  578. applog(LOG_DEBUG,
  579. "Avalon: no nonce = 0x%08llx hashes "
  580. "(%ld.%06lds)",
  581. estimate_hashes,
  582. elapsed.tv_sec, elapsed.tv_usec);
  583. }
  584. return estimate_hashes;
  585. }
  586. curr_hw_errors = avalon->hw_errors;
  587. submit_nonce(thr, work[work_i], nonce);
  588. was_hw_error = (curr_hw_errors > avalon->hw_errors);
  589. // Force a USB close/reopen on any hw error
  590. if (was_hw_error)
  591. do_avalon_close(thr);
  592. hash_count = (nonce & info->nonce_mask);
  593. hash_count++;
  594. hash_count *= info->asic_count;
  595. }
  596. if (opt_debug || info->do_avalon_timing)
  597. timersub(&tv_finish, &tv_start, &elapsed);
  598. if (opt_debug) {
  599. applog(LOG_DEBUG,
  600. "Avalon: nonce = 0x%08x = 0x%08llx hashes "
  601. "(%ld.%06lds)",
  602. nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  603. }
  604. // ignore possible end condition values ... and hw errors
  605. if (info->do_avalon_timing
  606. && !was_hw_error
  607. && ((nonce & info->nonce_mask) > END_CONDITION)
  608. && ((nonce & info->nonce_mask) <
  609. (info->nonce_mask & ~END_CONDITION))) {
  610. gettimeofday(&tv_history_start, NULL);
  611. history0 = &(info->history[0]);
  612. if (history0->values == 0)
  613. timeradd(&tv_start, &history_sec, &(history0->finish));
  614. Ti = (double)(elapsed.tv_sec)
  615. + ((double)(elapsed.tv_usec))/((double)1000000)
  616. - ((double)AVALON_READ_TIME(info->baud));
  617. Xi = (double)hash_count;
  618. history0->sumXiTi += Xi * Ti;
  619. history0->sumXi += Xi;
  620. history0->sumTi += Ti;
  621. history0->sumXi2 += Xi * Xi;
  622. history0->values++;
  623. if (history0->hash_count_max < hash_count)
  624. history0->hash_count_max = hash_count;
  625. if (history0->hash_count_min > hash_count ||
  626. history0->hash_count_min == 0)
  627. history0->hash_count_min = hash_count;
  628. if (history0->values >= info->min_data_count
  629. && timercmp(&tv_start, &(history0->finish), >)) {
  630. for (i = INFO_HISTORY; i > 0; i--)
  631. memcpy(&(info->history[i]),
  632. &(info->history[i-1]),
  633. sizeof(struct AVALON_HISTORY));
  634. // Initialise history0 to zero for summary calculation
  635. memset(history0, 0, sizeof(struct AVALON_HISTORY));
  636. // We just completed a history data set
  637. // So now recalc read_count based on the
  638. // whole history thus we will
  639. // initially get more accurate until it
  640. // completes INFO_HISTORY
  641. // total data sets
  642. count = 0;
  643. for (i = 1 ; i <= INFO_HISTORY; i++) {
  644. history = &(info->history[i]);
  645. if (history->values >= MIN_DATA_COUNT) {
  646. count++;
  647. history0->sumXiTi += history->sumXiTi;
  648. history0->sumXi += history->sumXi;
  649. history0->sumTi += history->sumTi;
  650. history0->sumXi2 += history->sumXi2;
  651. history0->values += history->values;
  652. if (history0->hash_count_max < history->hash_count_max)
  653. history0->hash_count_max = history->hash_count_max;
  654. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  655. history0->hash_count_min = history->hash_count_min;
  656. }
  657. }
  658. // All history data
  659. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  660. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  661. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  662. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  663. values = history0->values;
  664. // Initialise history0 to zero for next data set
  665. memset(history0, 0, sizeof(struct AVALON_HISTORY));
  666. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  667. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  668. info->Hs = Hs;
  669. info->read_count = read_count;
  670. info->fullnonce = fullnonce;
  671. info->count = count;
  672. info->W = W;
  673. info->values = values;
  674. info->hash_count_range = hash_count_range;
  675. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  676. info->min_data_count *= 2;
  677. else if (info->timing_mode == MODE_SHORT)
  678. info->do_avalon_timing = false;
  679. // 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);
  680. applog(LOG_WARNING, "Avalon %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  681. avalon->device_id, Hs, W, read_count, fullnonce);
  682. }
  683. info->history_count++;
  684. gettimeofday(&tv_history_finish, NULL);
  685. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  686. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  687. }
  688. return hash_count;
  689. }
  690. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  691. {
  692. struct api_data *root = NULL;
  693. struct AVALON_INFO *info = avalon_info[cgpu->device_id];
  694. // Warning, access to these is not locked - but we don't really
  695. // care since hashing performance is way more important than
  696. // locking access to displaying API debug 'stats'
  697. // If locking becomes an issue for any of them, use copy_data=true also
  698. root = api_add_int(root, "read_count", &(info->read_count), false);
  699. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  700. root = api_add_int(root, "count", &(info->count), false);
  701. root = api_add_hs(root, "Hs", &(info->Hs), false);
  702. root = api_add_double(root, "W", &(info->W), false);
  703. root = api_add_uint(root, "total_values", &(info->values), false);
  704. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  705. root = api_add_uint64(root, "history_count", &(info->history_count),
  706. false);
  707. root = api_add_timeval(root, "history_time", &(info->history_time),
  708. false);
  709. root = api_add_uint(root, "min_data_count", &(info->min_data_count),
  710. false);
  711. root = api_add_uint(root, "timing_values", &(info->history[0].values),
  712. false);
  713. root = api_add_const(root, "timing_mode",
  714. timing_mode_str(info->timing_mode), false);
  715. root = api_add_bool(root, "is_timing", &(info->do_avalon_timing),
  716. false);
  717. root = api_add_int(root, "baud", &(info->baud), false);
  718. root = api_add_int(root, "work_division", &(info->work_division),
  719. false);
  720. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  721. return root;
  722. }
  723. static void avalon_shutdown(struct thr_info *thr)
  724. {
  725. do_avalon_close(thr);
  726. }
  727. struct device_api avalon_api = {
  728. .dname = "avalon",
  729. .name = "AVA",
  730. .api_detect = avalon_detect,
  731. .get_api_stats = avalon_api_stats,
  732. .thread_prepare = avalon_prepare,
  733. .scanhash_queue = avalon_scanhash,
  734. .thread_shutdown = avalon_shutdown,
  735. };