driver-avalon.c 22 KB

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