driver-avalon.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. #include "config.h"
  12. #include <limits.h>
  13. #include <pthread.h>
  14. #include <stdio.h>
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <dirent.h>
  18. #include <unistd.h>
  19. #ifndef WIN32
  20. #include <termios.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #ifndef O_CLOEXEC
  24. #define O_CLOEXEC 0
  25. #endif
  26. #else
  27. #include <windows.h>
  28. #include <io.h>
  29. #endif
  30. #include "elist.h"
  31. #include "miner.h"
  32. #include "fpgautils.h"
  33. #include "driver-avalon.h"
  34. #include "hexdump.c"
  35. static struct timeval history_sec = { HISTORY_SEC, 0 };
  36. static const char *MODE_DEFAULT_STR = "default";
  37. static const char *MODE_SHORT_STR = "short";
  38. static const char *MODE_LONG_STR = "long";
  39. static const char *MODE_VALUE_STR = "value";
  40. static const char *MODE_UNKNOWN_STR = "unknown";
  41. static int option_offset = -1;
  42. static struct AVALON_INFO **avalon_info;
  43. struct device_api avalon_api;
  44. static int avalon_init_task(struct avalon_task *at,
  45. uint8_t reset, uint8_t ff, uint8_t fan,
  46. uint8_t timeout, uint8_t chip_num, uint8_t miner_num)
  47. {
  48. if (!at)
  49. return -1;
  50. memset(at, 0, sizeof(struct avalon_task));
  51. at->reset = (reset ? 1 : 0);
  52. at->flush_fifo = (ff ? 1 : 0);
  53. at->fan_eft = (fan ? 1 : 0); /* 1: fan_pwm_data */
  54. at->fan_pwm_data = (fan ? (0xFF & fan) : 0xFF); /* by default: 0xFF */
  55. /* 1: timeout_data miner_num, chip_num */
  56. at->timer_eft = (timeout ? 1 : 0);
  57. at->timer_eft = (chip_num ? 1 : 0);
  58. at->timer_eft = (miner_num ? 1 : 0);
  59. at->timeout_data = (timeout ? timeout : 0x27); /* by default: 0x27 */
  60. at->chip_num = (chip_num ? chip_num : 0xA); /* by default: 0x0A */
  61. at->miner_num = (miner_num ? miner_num : 0x18); /* by default: 0x18 */
  62. /* FIXME: Not support nonce range yet */
  63. at->nonce_elf = 0; /* 1: nonce_range*/
  64. return 0;
  65. }
  66. static inline void avalon_create_task(struct avalon_task *at, struct work *work)
  67. {
  68. memcpy(at->midstate, work->midstate, 32);
  69. rev(at->midstate, 32);
  70. memcpy(at->data, work->data + 64, 12);
  71. rev(at->data, 12);
  72. }
  73. static int avalon_send_task(int fd, const struct avalon_task *at)
  74. {
  75. size_t ret;
  76. struct timespec p;
  77. if (opt_debug) {
  78. applog(LOG_DEBUG, "Avalon: Sent:");
  79. hexdump((uint8_t *)at, sizeof(struct avalon_task));
  80. }
  81. ret = write(fd, (uint8_t *)at, AVALON_WRITE_SIZE);
  82. if (unlikely(ret != AVALON_WRITE_SIZE))
  83. return AVA_SEND_ERROR;
  84. p.tv_sec = 0;
  85. p.tv_nsec = AVALON_SEND_WORK_PITCH;
  86. nanosleep(&p, NULL);
  87. return AVA_SEND_OK;
  88. }
  89. static int avalon_gets(int fd, uint8_t *buf, int read_count,
  90. struct thr_info *thr, struct timeval *tv_finish)
  91. {
  92. ssize_t ret = 0;
  93. int rc = 0;
  94. int read_amount = AVALON_READ_SIZE;
  95. bool first = true;
  96. int full = avalon_buffer_full(fd);
  97. applog(LOG_DEBUG, "Avalon: Buffer full: %s",
  98. full == AVA_BUFFER_FULL? "Yes" : "no");
  99. if (full == AVA_BUFFER_EMPTY) {
  100. applog(LOG_DEBUG, "Avalon: Finished hash!");
  101. return AVA_GETS_DONE;
  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 && tv_finish != NULL)
  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(int fd, struct avalon_result *ar,
  138. struct thr_info *thr, struct timeval *tv_finish)
  139. {
  140. struct cgpu_info *avalon;
  141. struct AVALON_INFO *info;
  142. uint8_t result[AVALON_READ_SIZE];
  143. int ret, read_count = 16; /* FIXME: Set to 1.6s now? */
  144. if (thr) {
  145. avalon = thr->cgpu;
  146. info = avalon_info[avalon->device_id];
  147. read_count = info->read_count;
  148. }
  149. memset(result, 0, AVALON_READ_SIZE);
  150. ret = avalon_gets(fd, result, read_count, thr, tv_finish);
  151. if (opt_debug) {
  152. applog(LOG_DEBUG, "Avalon: get:");
  153. hexdump((uint8_t *)result, AVALON_READ_SIZE);
  154. }
  155. if (ret == AVA_GETS_OK) /* FIXME: maybe some decode/swab here? */
  156. memcpy((uint8_t *)ar, result, AVALON_READ_SIZE);
  157. return ret;
  158. }
  159. static int avalon_decode_nonce(struct work **work, struct avalon_result *ar,
  160. uint32_t *nonce)
  161. {
  162. uint8_t data[12];
  163. int i;
  164. *nonce = ar->nonce;
  165. #if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
  166. *nonce = swab32(*nonce);
  167. #endif
  168. memcpy(data, ar->data, 12);
  169. rev(data, 12);
  170. for (i = 0; i < AVALON_GET_WORK_COUNT; i++) {
  171. if (!memcmp(data, work[i]->data + 64, 12))
  172. break;
  173. }
  174. if (i == AVALON_GET_WORK_COUNT)
  175. return -1;
  176. i -= 1;
  177. /* FIXME: */
  178. return 3;
  179. }
  180. static int avalon_reset(int fd)
  181. {
  182. struct avalon_task at;
  183. struct avalon_result ar;
  184. uint8_t *buf;
  185. int ret, i;
  186. avalon_init_task(&at, 1, 0, 0, 0, 0, 0);
  187. ret = avalon_send_task(fd, &at);
  188. if (ret != AVA_SEND_OK)
  189. return 1;
  190. avalon_get_result(fd, &ar, NULL, NULL);
  191. buf = (uint8_t *)&ar;
  192. for (i = 0; i < 11; i++)
  193. if (buf[i] != 0)
  194. break;
  195. /* FIXME: add more avalon info base on return */
  196. if (i != 11) {
  197. applog(LOG_ERR, "Avalon: Reset failed! not a Avalon?");
  198. return 1;
  199. }
  200. applog(LOG_ERR, "Avalon: Reset succeeded");
  201. return 0;
  202. }
  203. static void do_avalon_close(struct thr_info *thr)
  204. {
  205. struct cgpu_info *avalon = thr->cgpu;
  206. avalon_close(avalon->device_fd);
  207. avalon->device_fd = -1;
  208. }
  209. static const char *timing_mode_str(enum timing_mode timing_mode)
  210. {
  211. switch(timing_mode) {
  212. case MODE_DEFAULT:
  213. return MODE_DEFAULT_STR;
  214. case MODE_SHORT:
  215. return MODE_SHORT_STR;
  216. case MODE_LONG:
  217. return MODE_LONG_STR;
  218. case MODE_VALUE:
  219. return MODE_VALUE_STR;
  220. default:
  221. return MODE_UNKNOWN_STR;
  222. }
  223. }
  224. static void set_timing_mode(int this_option_offset, struct cgpu_info *avalon)
  225. {
  226. struct AVALON_INFO *info = avalon_info[avalon->device_id];
  227. double Hs;
  228. char buf[BUFSIZ+1];
  229. char *ptr, *comma, *eq;
  230. size_t max;
  231. int i;
  232. if (opt_icarus_timing == NULL)
  233. buf[0] = '\0';
  234. else {
  235. ptr = opt_icarus_timing;
  236. for (i = 0; i < this_option_offset; i++) {
  237. comma = strchr(ptr, ',');
  238. if (comma == NULL)
  239. break;
  240. ptr = comma + 1;
  241. }
  242. comma = strchr(ptr, ',');
  243. if (comma == NULL)
  244. max = strlen(ptr);
  245. else
  246. max = comma - ptr;
  247. if (max > BUFSIZ)
  248. max = BUFSIZ;
  249. strncpy(buf, ptr, max);
  250. buf[max] = '\0';
  251. }
  252. info->Hs = 0;
  253. info->read_count = 0;
  254. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  255. info->Hs = AVALON_REV3_HASH_TIME;
  256. info->read_count = AVALON_READ_COUNT_TIMING;
  257. info->timing_mode = MODE_SHORT;
  258. info->do_avalon_timing = true;
  259. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  260. info->Hs = AVALON_REV3_HASH_TIME;
  261. info->read_count = AVALON_READ_COUNT_TIMING;
  262. info->timing_mode = MODE_LONG;
  263. info->do_avalon_timing = true;
  264. } else if ((Hs = atof(buf)) != 0) {
  265. info->Hs = Hs / NANOSEC;
  266. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  267. if ((eq = strchr(buf, '=')) != NULL)
  268. info->read_count = atoi(eq+1);
  269. if (info->read_count < 1)
  270. info->read_count =
  271. (int)(info->fullnonce * TIME_FACTOR) - 1;
  272. if (unlikely(info->read_count < 1))
  273. info->read_count = 1;
  274. info->timing_mode = MODE_VALUE;
  275. info->do_avalon_timing = false;
  276. } else {
  277. // Anything else in buf just uses DEFAULT mode
  278. info->Hs = AVALON_REV3_HASH_TIME;
  279. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  280. if ((eq = strchr(buf, '=')) != NULL)
  281. info->read_count = atoi(eq+1);
  282. if (info->read_count < 1)
  283. info->read_count =
  284. (int)(info->fullnonce * TIME_FACTOR) - 1;
  285. info->timing_mode = MODE_DEFAULT;
  286. info->do_avalon_timing = false;
  287. }
  288. info->min_data_count = MIN_DATA_COUNT;
  289. applog(LOG_DEBUG, "Avalon: Init: %d mode=%s read_count=%d Hs=%e",
  290. avalon->device_id, timing_mode_str(info->timing_mode),
  291. info->read_count, info->Hs);
  292. }
  293. static uint32_t mask(int work_division)
  294. {
  295. char err_buf[BUFSIZ+1];
  296. uint32_t nonce_mask = 0x7fffffff;
  297. // yes we can calculate these,
  298. // but this way it's easy to see what they are
  299. switch (work_division) {
  300. case 1:
  301. nonce_mask = 0xffffffff;
  302. break;
  303. case 2:
  304. nonce_mask = 0x7fffffff;
  305. break;
  306. case 4:
  307. nonce_mask = 0x3fffffff;
  308. break;
  309. case 8:
  310. nonce_mask = 0x1fffffff;
  311. break;
  312. default:
  313. sprintf(err_buf,
  314. "Invalid2 avalon-options for work_division (%d)"
  315. " must be 1, 2, 4 or 8", work_division);
  316. quit(1, err_buf);
  317. }
  318. return nonce_mask;
  319. }
  320. static void get_options(int this_option_offset, int *baud, int *work_division,
  321. int *asic_count)
  322. {
  323. char err_buf[BUFSIZ+1];
  324. char buf[BUFSIZ+1];
  325. char *ptr, *comma, *colon, *colon2;
  326. size_t max;
  327. int i, tmp;
  328. if (opt_icarus_options == NULL)
  329. buf[0] = '\0';
  330. else {
  331. ptr = opt_icarus_options;
  332. for (i = 0; i < this_option_offset; i++) {
  333. comma = strchr(ptr, ',');
  334. if (comma == NULL)
  335. break;
  336. ptr = comma + 1;
  337. }
  338. comma = strchr(ptr, ',');
  339. if (comma == NULL)
  340. max = strlen(ptr);
  341. else
  342. max = comma - ptr;
  343. if (max > BUFSIZ)
  344. max = BUFSIZ;
  345. strncpy(buf, ptr, max);
  346. buf[max] = '\0';
  347. }
  348. *baud = AVALON_IO_SPEED;
  349. *work_division = 2;
  350. *asic_count = 2;
  351. if (*buf) {
  352. colon = strchr(buf, ':');
  353. if (colon)
  354. *(colon++) = '\0';
  355. if (*buf) {
  356. tmp = atoi(buf);
  357. switch (tmp) {
  358. case 115200:
  359. *baud = 115200;
  360. break;
  361. case 57600:
  362. *baud = 57600;
  363. break;
  364. default:
  365. sprintf(err_buf,
  366. "Invalid avalon-options for baud (%s) "
  367. "must be 115200 or 57600", buf);
  368. quit(1, err_buf);
  369. }
  370. }
  371. if (colon && *colon) {
  372. colon2 = strchr(colon, ':');
  373. if (colon2)
  374. *(colon2++) = '\0';
  375. if (*colon) {
  376. tmp = atoi(colon);
  377. if (tmp == 1 || tmp == 2 ||
  378. tmp == 4 || tmp == 8) {
  379. *work_division = tmp;
  380. // default to the same
  381. *asic_count = tmp;
  382. } else {
  383. sprintf(err_buf,
  384. "Invalid avalon-options for "
  385. "work_division (%s) must be 1,"
  386. " 2, 4 or 8", colon);
  387. quit(1, err_buf);
  388. }
  389. }
  390. if (colon2 && *colon2) {
  391. tmp = atoi(colon2);
  392. if (tmp > 0 && tmp <= *work_division)
  393. *asic_count = tmp;
  394. else {
  395. sprintf(err_buf,
  396. "Invalid avalon-options for "
  397. "asic_count (%s) must be >0 "
  398. "and <=work_division (%d)",
  399. colon2, *work_division);
  400. quit(1, err_buf);
  401. }
  402. }
  403. }
  404. }
  405. }
  406. static bool avalon_detect_one(const char *devpath)
  407. {
  408. struct AVALON_INFO *info;
  409. int fd, ret;
  410. int baud, work_division, asic_count;
  411. int this_option_offset = ++option_offset;
  412. get_options(this_option_offset, &baud, &work_division, &asic_count);
  413. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s", devpath);
  414. fd = avalon_open2(devpath, baud, true);
  415. if (unlikely(fd == -1)) {
  416. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  417. return false;
  418. }
  419. ret = avalon_reset(fd);
  420. avalon_close(fd);
  421. if (ret)
  422. return false;
  423. /* We have a real Avalon! */
  424. struct cgpu_info *avalon;
  425. avalon = calloc(1, sizeof(struct cgpu_info));
  426. avalon->api = &avalon_api;
  427. avalon->device_path = strdup(devpath);
  428. avalon->device_fd = -1;
  429. avalon->threads = AVALON_MINER_THREADS;
  430. add_cgpu(avalon);
  431. avalon_info = realloc(avalon_info,
  432. sizeof(struct AVALON_INFO *) *
  433. (total_devices + 1));
  434. applog(LOG_INFO, "Avalon Detect: Found at %s, mark as %d",
  435. devpath, avalon->device_id);
  436. applog(LOG_DEBUG,
  437. "Avalon: Init: %d baud=%d work_division=%d asic_count=%d",
  438. avalon->device_id, baud, work_division, asic_count);
  439. avalon_info[avalon->device_id] = (struct AVALON_INFO *)
  440. malloc(sizeof(struct AVALON_INFO));
  441. if (unlikely(!(avalon_info[avalon->device_id])))
  442. quit(1, "Failed to malloc AVALON_INFO");
  443. info = avalon_info[avalon->device_id];
  444. // Initialise everything to zero for a new device
  445. memset(info, 0, sizeof(struct AVALON_INFO));
  446. info->baud = baud;
  447. info->work_division = work_division;
  448. info->asic_count = asic_count;
  449. info->nonce_mask = mask(work_division);
  450. set_timing_mode(this_option_offset, avalon);
  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 cgpu_info *avalon = thr->cgpu;
  460. struct timeval now;
  461. int fd;
  462. avalon->device_fd = -1;
  463. fd = avalon_open(avalon->device_path,
  464. avalon_info[avalon->device_id]->baud);
  465. if (unlikely(fd == -1)) {
  466. applog(LOG_ERR, "Avalon: Failed to open on %s",
  467. avalon->device_path);
  468. return false;
  469. }
  470. avalon->device_fd = fd;
  471. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  472. gettimeofday(&now, NULL);
  473. get_datestamp(avalon->init, &now);
  474. return true;
  475. }
  476. static int64_t avalon_scanhash(struct thr_info *thr, struct work **work,
  477. __maybe_unused int64_t max_nonce)
  478. {
  479. struct cgpu_info *avalon;
  480. int fd;
  481. int ret;
  482. struct AVALON_INFO *info;
  483. struct avalon_task at;
  484. struct avalon_result ar;
  485. uint32_t nonce;
  486. int64_t hash_count;
  487. int i, work_i;
  488. int read_count;
  489. int count;
  490. struct timeval tv_start, tv_finish, elapsed;
  491. struct timeval tv_history_start, tv_history_finish;
  492. double Ti, Xi;
  493. int curr_hw_errors;
  494. bool was_hw_error;
  495. struct AVALON_HISTORY *history0, *history;
  496. double Hs, W, fullnonce;
  497. int64_t estimate_hashes;
  498. uint32_t values;
  499. int64_t hash_count_range;
  500. avalon = thr->cgpu;
  501. info = avalon_info[avalon->device_id];
  502. if (avalon->device_fd == -1)
  503. if (!avalon_prepare(thr)) {
  504. applog(LOG_ERR, "AVA%i: Comms error",
  505. avalon->device_id);
  506. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  507. // fail the device if the reopen attempt fails
  508. return -1;
  509. }
  510. fd = avalon->device_fd;
  511. #ifndef WIN32
  512. tcflush(fd, TCOFLUSH);
  513. #endif
  514. /* Write task to device one by one */
  515. for (i = 0; i < AVALON_GET_WORK_COUNT; i++) {
  516. avalon_init_default_task(&at);
  517. avalon_create_task(&at, work[i]);
  518. ret = avalon_send_task(fd, &at);
  519. if (ret == AVA_SEND_ERROR) {
  520. do_avalon_close(thr);
  521. applog(LOG_ERR, "AVA%i: Comms error",
  522. avalon->device_id);
  523. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  524. return 0; /* This should never happen */
  525. }
  526. }
  527. elapsed.tv_sec = elapsed.tv_usec = 0;
  528. gettimeofday(&tv_start, NULL);
  529. /* count may != AVALON_GET_WORK_COUNT */
  530. for (i = 0; i < AVALON_GET_WORK_COUNT; i++) {
  531. ret = avalon_get_result(fd, &ar, thr, &tv_finish);
  532. if (ret == AVA_GETS_ERROR ) {
  533. do_avalon_close(thr);
  534. applog(LOG_ERR, "AVA%i: Comms error", avalon->device_id);
  535. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  536. return 0;
  537. }
  538. work_i = avalon_decode_nonce(work, &ar, &nonce);
  539. if (work_i < 0) {
  540. continue; /* FIXME: the result maybe less then AVALON_GET_WORK_COUNT!!!! */
  541. }
  542. work[work_i]->blk.nonce = 0xffffffff;
  543. // aborted before becoming idle, get new work
  544. if (ret == AVA_GETS_TIMEOUT || ret == AVA_GETS_RESTART) {
  545. timersub(&tv_finish, &tv_start, &elapsed);
  546. // ONLY up to just when it aborted
  547. // We didn't read a reply so we don't subtract AVALON_READ_TIME
  548. estimate_hashes = ((double)(elapsed.tv_sec) +
  549. ((double)(elapsed.tv_usec)) /
  550. ((double)1000000)) / info->Hs;
  551. // If some Serial-USB delay allowed the full nonce range to
  552. // complete it can't have done more than a full nonce
  553. if (unlikely(estimate_hashes > 0xffffffff))
  554. estimate_hashes = 0xffffffff;
  555. applog(LOG_DEBUG,
  556. "Avalon: no nonce = 0x%08llx hashes "
  557. "(%ld.%06lds)",
  558. estimate_hashes, elapsed.tv_sec, elapsed.tv_usec);
  559. return estimate_hashes;
  560. }
  561. curr_hw_errors = avalon->hw_errors;
  562. submit_nonce(thr, work[work_i], nonce);
  563. was_hw_error = (curr_hw_errors > avalon->hw_errors);
  564. // Force a USB close/reopen on any hw error
  565. if (was_hw_error)
  566. do_avalon_close(thr);
  567. hash_count = (nonce & info->nonce_mask);
  568. hash_count++;
  569. hash_count *= info->asic_count;
  570. }
  571. /* FIXME: */
  572. return -1;
  573. if (opt_debug || info->do_avalon_timing)
  574. timersub(&tv_finish, &tv_start, &elapsed);
  575. if (opt_debug) {
  576. applog(LOG_DEBUG,
  577. "Avalon: nonce = 0x%08x = 0x%08llx hashes "
  578. "(%ld.%06lds)",
  579. nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  580. }
  581. // ignore possible end condition values ... and hw errors
  582. if (info->do_avalon_timing
  583. && !was_hw_error
  584. && ((nonce & info->nonce_mask) > END_CONDITION)
  585. && ((nonce & info->nonce_mask) <
  586. (info->nonce_mask & ~END_CONDITION))) {
  587. gettimeofday(&tv_history_start, NULL);
  588. history0 = &(info->history[0]);
  589. if (history0->values == 0)
  590. timeradd(&tv_start, &history_sec, &(history0->finish));
  591. Ti = (double)(elapsed.tv_sec)
  592. + ((double)(elapsed.tv_usec))/((double)1000000)
  593. - ((double)AVALON_READ_TIME(info->baud));
  594. Xi = (double)hash_count;
  595. history0->sumXiTi += Xi * Ti;
  596. history0->sumXi += Xi;
  597. history0->sumTi += Ti;
  598. history0->sumXi2 += Xi * Xi;
  599. history0->values++;
  600. if (history0->hash_count_max < hash_count)
  601. history0->hash_count_max = hash_count;
  602. if (history0->hash_count_min > hash_count ||
  603. history0->hash_count_min == 0)
  604. history0->hash_count_min = hash_count;
  605. if (history0->values >= info->min_data_count
  606. && timercmp(&tv_start, &(history0->finish), >)) {
  607. for (i = INFO_HISTORY; i > 0; i--)
  608. memcpy(&(info->history[i]),
  609. &(info->history[i-1]),
  610. sizeof(struct AVALON_HISTORY));
  611. // Initialise history0 to zero for summary calculation
  612. memset(history0, 0, sizeof(struct AVALON_HISTORY));
  613. // We just completed a history data set
  614. // So now recalc read_count based on the
  615. // whole history thus we will
  616. // initially get more accurate until it
  617. // completes INFO_HISTORY
  618. // total data sets
  619. count = 0;
  620. for (i = 1 ; i <= INFO_HISTORY; i++) {
  621. history = &(info->history[i]);
  622. if (history->values >= MIN_DATA_COUNT) {
  623. count++;
  624. history0->sumXiTi += history->sumXiTi;
  625. history0->sumXi += history->sumXi;
  626. history0->sumTi += history->sumTi;
  627. history0->sumXi2 += history->sumXi2;
  628. history0->values += history->values;
  629. if (history0->hash_count_max < history->hash_count_max)
  630. history0->hash_count_max = history->hash_count_max;
  631. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  632. history0->hash_count_min = history->hash_count_min;
  633. }
  634. }
  635. // All history data
  636. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  637. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  638. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  639. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  640. values = history0->values;
  641. // Initialise history0 to zero for next data set
  642. memset(history0, 0, sizeof(struct AVALON_HISTORY));
  643. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  644. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  645. info->Hs = Hs;
  646. info->read_count = read_count;
  647. info->fullnonce = fullnonce;
  648. info->count = count;
  649. info->W = W;
  650. info->values = values;
  651. info->hash_count_range = hash_count_range;
  652. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  653. info->min_data_count *= 2;
  654. else if (info->timing_mode == MODE_SHORT)
  655. info->do_avalon_timing = false;
  656. // 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);
  657. applog(LOG_WARNING, "Avalon %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  658. avalon->device_id, Hs, W, read_count, fullnonce);
  659. }
  660. info->history_count++;
  661. gettimeofday(&tv_history_finish, NULL);
  662. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  663. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  664. }
  665. return hash_count;
  666. }
  667. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  668. {
  669. struct api_data *root = NULL;
  670. struct AVALON_INFO *info = avalon_info[cgpu->device_id];
  671. // Warning, access to these is not locked - but we don't really
  672. // care since hashing performance is way more important than
  673. // locking access to displaying API debug 'stats'
  674. // If locking becomes an issue for any of them, use copy_data=true also
  675. root = api_add_int(root, "read_count", &(info->read_count), false);
  676. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  677. root = api_add_int(root, "count", &(info->count), false);
  678. root = api_add_hs(root, "Hs", &(info->Hs), false);
  679. root = api_add_double(root, "W", &(info->W), false);
  680. root = api_add_uint(root, "total_values", &(info->values), false);
  681. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  682. root = api_add_uint64(root, "history_count", &(info->history_count),
  683. false);
  684. root = api_add_timeval(root, "history_time", &(info->history_time),
  685. false);
  686. root = api_add_uint(root, "min_data_count", &(info->min_data_count),
  687. false);
  688. root = api_add_uint(root, "timing_values", &(info->history[0].values),
  689. false);
  690. root = api_add_const(root, "timing_mode",
  691. timing_mode_str(info->timing_mode), false);
  692. root = api_add_bool(root, "is_timing", &(info->do_avalon_timing),
  693. false);
  694. root = api_add_int(root, "baud", &(info->baud), false);
  695. root = api_add_int(root, "work_division", &(info->work_division),
  696. false);
  697. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  698. return root;
  699. }
  700. static void avalon_shutdown(struct thr_info *thr)
  701. {
  702. do_avalon_close(thr);
  703. }
  704. struct device_api avalon_api = {
  705. .dname = "avalon",
  706. .name = "AVA",
  707. .api_detect = avalon_detect,
  708. .thread_prepare = avalon_prepare,
  709. .scanhash_queue = avalon_scanhash,
  710. .get_api_stats = avalon_api_stats,
  711. .thread_shutdown = avalon_shutdown,
  712. };