driver-icarus.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. * Copyright 2012 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 Icarus.
  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 Icarus 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. Icarus will start calculate when you push a work to them, even they
  22. * are busy.
  23. * 2. The 2 FPGAs on Icarus 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. Icarus 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. #ifdef HAVE_SYS_EPOLL_H
  50. #include <sys/epoll.h>
  51. #define HAVE_EPOLL
  52. #endif
  53. #include "elist.h"
  54. #include "fpgautils.h"
  55. #include "miner.h"
  56. // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
  57. #define ICARUS_IO_SPEED 115200
  58. // The size of a successful nonce read
  59. #define ICARUS_READ_SIZE 4
  60. // Ensure the sizes are correct for the Serial read
  61. #if (ICARUS_READ_SIZE != 4)
  62. #error ICARUS_READ_SIZE must be 4
  63. #endif
  64. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  65. ASSERT1(sizeof(uint32_t) == 4);
  66. #define ICARUS_READ_TIME ((double)ICARUS_READ_SIZE * (double)8.0 / (double)ICARUS_IO_SPEED)
  67. // Fraction of a second, USB timeout is measured in
  68. // i.e. 10 means 1/10 of a second
  69. // Right now, it MUST be 10 due to other assumptions.
  70. #define TIME_FACTOR 10
  71. // It's 10 per second, thus value = 10/TIME_FACTOR =
  72. #define ICARUS_READ_FAULT_DECISECONDS 1
  73. // In timing mode: Default starting value until an estimate can be obtained
  74. // 5 seconds allows for up to a ~840MH/s device
  75. #define ICARUS_READ_COUNT_TIMING (5 * TIME_FACTOR)
  76. // For a standard Icarus REV3
  77. #define ICARUS_REV3_HASH_TIME 0.00000000264083
  78. #define NANOSEC 1000000000.0
  79. // Icarus Rev3 doesn't send a completion message when it finishes
  80. // the full nonce range, so to avoid being idle we must abort the
  81. // work (by starting a new work) shortly before it finishes
  82. //
  83. // Thus we need to estimate 2 things:
  84. // 1) How many hashes were done if the work was aborted
  85. // 2) How high can the timeout be before the Icarus is idle,
  86. // to minimise the number of work started
  87. // We set 2) to 'the calculated estimate' - 1
  88. // to ensure the estimate ends before idle
  89. //
  90. // The simple calculation used is:
  91. // Tn = Total time in seconds to calculate n hashes
  92. // Hs = seconds per hash
  93. // Xn = number of hashes
  94. // W = code overhead per work
  95. //
  96. // Rough but reasonable estimate:
  97. // Tn = Hs * Xn + W (of the form y = mx + b)
  98. //
  99. // Thus:
  100. // Line of best fit (using least squares)
  101. //
  102. // Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2)
  103. // W = Sum(Ti)/n - (Hs*Sum(Xi))/n
  104. //
  105. // N.B. W is less when aborting work since we aren't waiting for the reply
  106. // to be transferred back (ICARUS_READ_TIME)
  107. // Calculating the hashes aborted at n seconds is thus just n/Hs
  108. // (though this is still a slight overestimate due to code delays)
  109. //
  110. // Both below must be exceeded to complete a set of data
  111. // Minimum how long after the first, the last data point must be
  112. #define HISTORY_SEC 60
  113. // Minimum how many points a single ICARUS_HISTORY should have
  114. #define MIN_DATA_COUNT 5
  115. // The value above used is doubled each history until it exceeds:
  116. #define MAX_MIN_DATA_COUNT 100
  117. #if (TIME_FACTOR != 10)
  118. #error TIME_FACTOR must be 10
  119. #endif
  120. static struct timeval history_sec = { HISTORY_SEC, 0 };
  121. // Store the last INFO_HISTORY data sets
  122. // [0] = current data, not yet ready to be included as an estimate
  123. // Each new data set throws the last old set off the end thus
  124. // keeping a ongoing average of recent data
  125. #define INFO_HISTORY 10
  126. struct ICARUS_HISTORY {
  127. struct timeval finish;
  128. double sumXiTi;
  129. double sumXi;
  130. double sumTi;
  131. double sumXi2;
  132. uint32_t values;
  133. uint32_t hash_count_min;
  134. uint32_t hash_count_max;
  135. };
  136. enum timing_mode { MODE_DEFAULT, MODE_SHORT, MODE_LONG, MODE_VALUE };
  137. static const char *MODE_DEFAULT_STR = "default";
  138. static const char *MODE_SHORT_STR = "short";
  139. static const char *MODE_LONG_STR = "long";
  140. static const char *MODE_VALUE_STR = "value";
  141. static const char *MODE_UNKNOWN_STR = "unknown";
  142. struct ICARUS_INFO {
  143. // time to calculate the golden_ob
  144. uint64_t golden_hashes;
  145. struct timeval golden_tv;
  146. struct ICARUS_HISTORY history[INFO_HISTORY+1];
  147. uint32_t min_data_count;
  148. // seconds per Hash
  149. double Hs;
  150. int read_count;
  151. enum timing_mode timing_mode;
  152. bool do_icarus_timing;
  153. double fullnonce;
  154. int count;
  155. double W;
  156. uint32_t values;
  157. uint64_t hash_count_range;
  158. // Determine the cost of history processing
  159. // (which will only affect W)
  160. uint64_t history_count;
  161. struct timeval history_time;
  162. };
  163. // One for each possible device
  164. static struct ICARUS_INFO **icarus_info;
  165. struct device_api icarus_api;
  166. static void rev(unsigned char *s, size_t l)
  167. {
  168. size_t i, j;
  169. unsigned char t;
  170. for (i = 0, j = l - 1; i < j; i++, j--) {
  171. t = s[i];
  172. s[i] = s[j];
  173. s[j] = t;
  174. }
  175. }
  176. #define icarus_open2(devpath, purge) serial_open(devpath, 115200, ICARUS_READ_FAULT_DECISECONDS, purge)
  177. #define icarus_open(devpath) icarus_open2(devpath, false)
  178. static int icarus_gets(unsigned char *buf, int fd, struct timeval *tv_finish, struct thr_info *thr, int read_count)
  179. {
  180. ssize_t ret = 0;
  181. int rc = 0;
  182. int epollfd = -1;
  183. int read_amount = ICARUS_READ_SIZE;
  184. bool first = true;
  185. #ifdef HAVE_EPOLL
  186. struct epoll_event ev;
  187. struct epoll_event evr[2];
  188. int epoll_timeout = ICARUS_READ_FAULT_DECISECONDS * 100;
  189. epollfd = epoll_create(2);
  190. if (epollfd != -1) {
  191. ev.events = EPOLLIN;
  192. ev.data.fd = fd;
  193. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
  194. close(epollfd);
  195. epollfd = -1;
  196. }
  197. if (thr->work_restart_fd != -1)
  198. {
  199. ev.data.fd = thr->work_restart_fd;
  200. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, thr->work_restart_fd, &ev))
  201. applog(LOG_ERR, "Icarus: Error adding work restart fd to epoll");
  202. else
  203. {
  204. epoll_timeout *= read_count;
  205. read_count = 1;
  206. }
  207. }
  208. }
  209. else
  210. applog(LOG_ERR, "Icarus: Error creating epoll");
  211. #endif
  212. // Read reply 1 byte at a time to get earliest tv_finish
  213. while (true) {
  214. #ifdef HAVE_EPOLL
  215. if (epollfd != -1 && (ret = epoll_wait(epollfd, evr, 2, epoll_timeout)) != -1)
  216. {
  217. if (ret == 1 && evr[0].data.fd == fd)
  218. ret = read(fd, buf, 1);
  219. else
  220. {
  221. if (ret)
  222. // work restart trigger
  223. (void)read(thr->work_restart_fd, buf, read_amount);
  224. ret = 0;
  225. }
  226. }
  227. else
  228. #endif
  229. ret = read(fd, buf, 1);
  230. if (first)
  231. gettimeofday(tv_finish, NULL);
  232. if (ret >= read_amount)
  233. {
  234. if (epollfd != -1)
  235. close(epollfd);
  236. return 0;
  237. }
  238. if (ret > 0) {
  239. buf += ret;
  240. read_amount -= ret;
  241. first = false;
  242. continue;
  243. }
  244. rc++;
  245. if (rc >= read_count || thr->work_restart) {
  246. if (epollfd != -1)
  247. close(epollfd);
  248. if (opt_debug) {
  249. rc *= ICARUS_READ_FAULT_DECISECONDS;
  250. applog(LOG_DEBUG,
  251. "Icarus Read: %s %d.%d seconds",
  252. thr->work_restart ? "Work restart at" : "No data in",
  253. rc / 10, rc % 10);
  254. }
  255. return 1;
  256. }
  257. }
  258. }
  259. static int icarus_write(int fd, const void *buf, size_t bufLen)
  260. {
  261. size_t ret;
  262. ret = write(fd, buf, bufLen);
  263. if (unlikely(ret != bufLen))
  264. return 1;
  265. return 0;
  266. }
  267. #define icarus_close(fd) close(fd)
  268. static const char *timing_mode_str(enum timing_mode timing_mode)
  269. {
  270. switch(timing_mode) {
  271. case MODE_DEFAULT:
  272. return MODE_DEFAULT_STR;
  273. case MODE_SHORT:
  274. return MODE_SHORT_STR;
  275. case MODE_LONG:
  276. return MODE_LONG_STR;
  277. case MODE_VALUE:
  278. return MODE_VALUE_STR;
  279. default:
  280. return MODE_UNKNOWN_STR;
  281. }
  282. }
  283. static void set_timing_mode(struct cgpu_info *icarus)
  284. {
  285. struct ICARUS_INFO *info = icarus_info[icarus->device_id];
  286. double Hs;
  287. char buf[BUFSIZ+1];
  288. char *ptr, *comma, *eq;
  289. size_t max;
  290. int i;
  291. if (opt_icarus_timing == NULL)
  292. buf[0] = '\0';
  293. else {
  294. ptr = opt_icarus_timing;
  295. for (i = 0; i < icarus->device_id; i++) {
  296. comma = strchr(ptr, ',');
  297. if (comma == NULL)
  298. break;
  299. ptr = comma + 1;
  300. }
  301. comma = strchr(ptr, ',');
  302. if (comma == NULL)
  303. max = strlen(ptr);
  304. else
  305. max = comma - ptr;
  306. if (max > BUFSIZ)
  307. max = BUFSIZ;
  308. strncpy(buf, ptr, max);
  309. buf[max] = '\0';
  310. }
  311. info->Hs = 0;
  312. info->read_count = 0;
  313. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  314. info->Hs = ICARUS_REV3_HASH_TIME;
  315. info->read_count = ICARUS_READ_COUNT_TIMING;
  316. info->timing_mode = MODE_SHORT;
  317. info->do_icarus_timing = true;
  318. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  319. info->Hs = ICARUS_REV3_HASH_TIME;
  320. info->read_count = ICARUS_READ_COUNT_TIMING;
  321. info->timing_mode = MODE_LONG;
  322. info->do_icarus_timing = true;
  323. } else if ((Hs = atof(buf)) != 0) {
  324. info->Hs = Hs / NANOSEC;
  325. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  326. if ((eq = strchr(buf, '=')) != NULL)
  327. info->read_count = atoi(eq+1);
  328. if (info->read_count < 1)
  329. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  330. if (unlikely(info->read_count < 1))
  331. info->read_count = 1;
  332. info->timing_mode = MODE_VALUE;
  333. info->do_icarus_timing = false;
  334. } else {
  335. // Anything else in buf just uses DEFAULT mode
  336. info->Hs = ICARUS_REV3_HASH_TIME;
  337. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  338. if ((eq = strchr(buf, '=')) != NULL)
  339. info->read_count = atoi(eq+1);
  340. if (info->read_count < 1)
  341. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  342. info->timing_mode = MODE_DEFAULT;
  343. info->do_icarus_timing = false;
  344. }
  345. info->min_data_count = MIN_DATA_COUNT;
  346. applog(LOG_DEBUG, "Icarus: Init: %d mode=%s read_count=%d Hs=%e",
  347. icarus->device_id, timing_mode_str(info->timing_mode), info->read_count, info->Hs);
  348. }
  349. static bool icarus_detect_one(const char *devpath)
  350. {
  351. struct ICARUS_INFO *info;
  352. struct timeval tv_start, tv_finish;
  353. int fd;
  354. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  355. // N.B. golden_ob MUST take less time to calculate
  356. // than the timeout set in icarus_open()
  357. // This one takes ~0.53ms on Rev3 Icarus
  358. const char golden_ob[] =
  359. "4679ba4ec99876bf4bfe086082b40025"
  360. "4df6c356451471139a3afa71e48f544a"
  361. "00000000000000000000000000000000"
  362. "0000000087320b1a1426674f2fa722ce";
  363. const char golden_nonce[] = "000187a2";
  364. const uint32_t golden_nonce_val = 0x000187a2;
  365. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  366. char *nonce_hex;
  367. applog(LOG_DEBUG, "Icarus Detect: Attempting to open %s", devpath);
  368. fd = icarus_open2(devpath, true);
  369. if (unlikely(fd == -1)) {
  370. applog(LOG_DEBUG, "Icarus Detect: Failed to open %s", devpath);
  371. return false;
  372. }
  373. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  374. icarus_write(fd, ob_bin, sizeof(ob_bin));
  375. gettimeofday(&tv_start, NULL);
  376. memset(nonce_bin, 0, sizeof(nonce_bin));
  377. struct thr_info dummy = {
  378. .work_restart = false,
  379. .work_restart_fd = -1,
  380. };
  381. icarus_gets(nonce_bin, fd, &tv_finish, &dummy, 1);
  382. icarus_close(fd);
  383. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  384. if (nonce_hex) {
  385. if (strncmp(nonce_hex, golden_nonce, 8)) {
  386. applog(LOG_DEBUG,
  387. "Icarus Detect: "
  388. "Test failed at %s: get %s, should: %s",
  389. devpath, nonce_hex, golden_nonce);
  390. free(nonce_hex);
  391. return false;
  392. }
  393. applog(LOG_DEBUG,
  394. "Icarus Detect: "
  395. "Test succeeded at %s: got %s",
  396. devpath, nonce_hex);
  397. free(nonce_hex);
  398. } else
  399. return false;
  400. /* We have a real Icarus! */
  401. struct cgpu_info *icarus;
  402. icarus = calloc(1, sizeof(struct cgpu_info));
  403. icarus->api = &icarus_api;
  404. icarus->device_path = strdup(devpath);
  405. icarus->threads = 1;
  406. add_cgpu(icarus);
  407. icarus_info = realloc(icarus_info, sizeof(struct ICARUS_INFO *) * (total_devices + 1));
  408. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  409. devpath, icarus->device_id);
  410. // Since we are adding a new device on the end it needs to always be allocated
  411. icarus_info[icarus->device_id] = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  412. if (unlikely(!(icarus_info[icarus->device_id])))
  413. quit(1, "Failed to malloc ICARUS_INFO");
  414. info = icarus_info[icarus->device_id];
  415. // Initialise everything to zero for a new device
  416. memset(info, 0, sizeof(struct ICARUS_INFO));
  417. info->golden_hashes = (golden_nonce_val & 0x7fffffff) << 1;
  418. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  419. set_timing_mode(icarus);
  420. return true;
  421. }
  422. static void icarus_detect()
  423. {
  424. serial_detect(icarus_api.dname, icarus_detect_one);
  425. }
  426. struct icarus_state {
  427. bool firstrun;
  428. struct timeval tv_workstart;
  429. struct timeval tv_workfinish;
  430. struct work last_work;
  431. bool changework;
  432. };
  433. static bool icarus_prepare(struct thr_info *thr)
  434. {
  435. struct cgpu_info *icarus = thr->cgpu;
  436. struct timeval now;
  437. int fd = icarus_open2(icarus->device_path, true);
  438. if (unlikely(-1 == fd)) {
  439. applog(LOG_ERR, "Failed to open Icarus on %s",
  440. icarus->device_path);
  441. return false;
  442. }
  443. icarus->device_fd = fd;
  444. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  445. gettimeofday(&now, NULL);
  446. get_datestamp(icarus->init, &now);
  447. struct icarus_state *state;
  448. thr->cgpu_data = state = calloc(1, sizeof(*state));
  449. state->firstrun = true;
  450. #ifdef HAVE_EPOLL
  451. int epollfd = epoll_create(2);
  452. if (epollfd != -1)
  453. {
  454. close(epollfd);
  455. thr->work_restart_fd = 0;
  456. }
  457. #endif
  458. return true;
  459. }
  460. static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  461. __maybe_unused int64_t max_nonce)
  462. {
  463. struct cgpu_info *icarus;
  464. int fd;
  465. int ret, lret;
  466. struct ICARUS_INFO *info;
  467. unsigned char ob_bin[64] = {0}, nonce_bin[ICARUS_READ_SIZE] = {0};
  468. char *ob_hex;
  469. uint32_t nonce;
  470. int64_t hash_count;
  471. struct timeval tv_start, elapsed;
  472. struct timeval tv_history_start, tv_history_finish;
  473. double Ti, Xi;
  474. int i;
  475. struct ICARUS_HISTORY *history0, *history;
  476. int count;
  477. double Hs, W, fullnonce;
  478. int read_count;
  479. int64_t estimate_hashes;
  480. uint32_t values;
  481. int64_t hash_count_range;
  482. elapsed.tv_sec = elapsed.tv_usec = 0;
  483. icarus = thr->cgpu;
  484. struct icarus_state *state = thr->cgpu_data;
  485. // Prepare the next work immediately
  486. memcpy(ob_bin, work->midstate, 32);
  487. memcpy(ob_bin + 52, work->data + 64, 12);
  488. rev(ob_bin, 32);
  489. rev(ob_bin + 52, 12);
  490. // Wait for the previous run's result
  491. fd = icarus->device_fd;
  492. info = icarus_info[icarus->device_id];
  493. if (!state->firstrun) {
  494. if (state->changework)
  495. state->changework = false;
  496. else
  497. {
  498. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  499. lret = icarus_gets(nonce_bin, fd, &state->tv_workfinish, thr, info->read_count);
  500. if (lret && thr->work_restart) {
  501. // The prepared work is invalid, and the current work is abandoned
  502. // Go back to the main loop to get the next work, and stuff
  503. // Returning to the main loop will clear work_restart, so use a flag...
  504. state->changework = true;
  505. return 0;
  506. }
  507. }
  508. tv_start = state->tv_workstart;
  509. timersub(&state->tv_workfinish, &tv_start, &elapsed);
  510. }
  511. #ifndef WIN32
  512. tcflush(fd, TCOFLUSH);
  513. #endif
  514. gettimeofday(&state->tv_workstart, NULL);
  515. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  516. if (ret) {
  517. icarus_close(fd);
  518. return -1; /* This should never happen */
  519. }
  520. if (opt_debug) {
  521. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  522. if (ob_hex) {
  523. applog(LOG_DEBUG, "Icarus %d sent: %s",
  524. icarus->device_id, ob_hex);
  525. free(ob_hex);
  526. }
  527. }
  528. // Reopen the serial port to workaround a USB-host-chipset-specific issue with the Icarus's buggy USB-UART
  529. icarus_close(fd);
  530. fd = icarus_open(icarus->device_path);
  531. if (unlikely(-1 == fd)) {
  532. applog(LOG_ERR, "Failed to reopen Icarus on %s",
  533. icarus->device_path);
  534. return 0;
  535. }
  536. icarus->device_fd = fd;
  537. work->blk.nonce = 0xffffffff;
  538. if (state->firstrun) {
  539. state->firstrun = false;
  540. memcpy(&state->last_work, work, sizeof(state->last_work));
  541. return 0;
  542. }
  543. // OK, done starting Icarus's next job... now process the last run's result!
  544. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  545. // aborted before becoming idle, get new work
  546. if (nonce == 0 && lret) {
  547. memcpy(&state->last_work, work, sizeof(state->last_work));
  548. // ONLY up to just when it aborted
  549. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  550. estimate_hashes = ((double)(elapsed.tv_sec)
  551. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  552. // If some Serial-USB delay allowed the full nonce range to
  553. // complete it can't have done more than a full nonce
  554. if (unlikely(estimate_hashes > 0xffffffff))
  555. estimate_hashes = 0xffffffff;
  556. if (opt_debug) {
  557. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08llx hashes (%ld.%06lds)",
  558. icarus->device_id, estimate_hashes,
  559. elapsed.tv_sec, elapsed.tv_usec);
  560. }
  561. return estimate_hashes;
  562. }
  563. #if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
  564. nonce = swab32(nonce);
  565. #endif
  566. submit_nonce(thr, &state->last_work, nonce);
  567. memcpy(&state->last_work, work, sizeof(state->last_work));
  568. hash_count = (nonce & 0x7fffffff);
  569. if (hash_count++ == 0x7fffffff)
  570. hash_count = 0xffffffff;
  571. else
  572. hash_count <<= 1;
  573. if (opt_debug) {
  574. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08llx hashes (%ld.%06lds)",
  575. icarus->device_id, nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  576. }
  577. // ignore possible end condition values
  578. if (info->do_icarus_timing && (nonce & 0x7fffffff) > 0x000fffff && (nonce & 0x7fffffff) < 0x7ff00000) {
  579. gettimeofday(&tv_history_start, NULL);
  580. history0 = &(info->history[0]);
  581. if (history0->values == 0)
  582. timeradd(&tv_start, &history_sec, &(history0->finish));
  583. Ti = (double)(elapsed.tv_sec)
  584. + ((double)(elapsed.tv_usec))/((double)1000000)
  585. - ICARUS_READ_TIME;
  586. Xi = (double)hash_count;
  587. history0->sumXiTi += Xi * Ti;
  588. history0->sumXi += Xi;
  589. history0->sumTi += Ti;
  590. history0->sumXi2 += Xi * Xi;
  591. history0->values++;
  592. if (history0->hash_count_max < hash_count)
  593. history0->hash_count_max = hash_count;
  594. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  595. history0->hash_count_min = hash_count;
  596. if (history0->values >= info->min_data_count
  597. && timercmp(&tv_start, &(history0->finish), >)) {
  598. for (i = INFO_HISTORY; i > 0; i--)
  599. memcpy(&(info->history[i]),
  600. &(info->history[i-1]),
  601. sizeof(struct ICARUS_HISTORY));
  602. // Initialise history0 to zero for summary calculation
  603. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  604. // We just completed a history data set
  605. // So now recalc read_count based on the whole history thus we will
  606. // initially get more accurate until it completes INFO_HISTORY
  607. // total data sets
  608. count = 0;
  609. for (i = 1 ; i <= INFO_HISTORY; i++) {
  610. history = &(info->history[i]);
  611. if (history->values >= MIN_DATA_COUNT) {
  612. count++;
  613. history0->sumXiTi += history->sumXiTi;
  614. history0->sumXi += history->sumXi;
  615. history0->sumTi += history->sumTi;
  616. history0->sumXi2 += history->sumXi2;
  617. history0->values += history->values;
  618. if (history0->hash_count_max < history->hash_count_max)
  619. history0->hash_count_max = history->hash_count_max;
  620. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  621. history0->hash_count_min = history->hash_count_min;
  622. }
  623. }
  624. // All history data
  625. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  626. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  627. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  628. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  629. values = history0->values;
  630. // Initialise history0 to zero for next data set
  631. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  632. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  633. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  634. info->Hs = Hs;
  635. info->read_count = read_count;
  636. info->fullnonce = fullnonce;
  637. info->count = count;
  638. info->W = W;
  639. info->values = values;
  640. info->hash_count_range = hash_count_range;
  641. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  642. info->min_data_count *= 2;
  643. else if (info->timing_mode == MODE_SHORT)
  644. info->do_icarus_timing = false;
  645. // applog(LOG_WARNING, "Icarus %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", icarus->device_id, read_count, fullnonce, count, Hs, W, values, hash_count_range, info->min_data_count);
  646. applog(LOG_WARNING, "Icarus %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  647. icarus->device_id, Hs, W, read_count, fullnonce);
  648. }
  649. info->history_count++;
  650. gettimeofday(&tv_history_finish, NULL);
  651. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  652. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  653. }
  654. return hash_count;
  655. }
  656. static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
  657. {
  658. struct api_data *root = NULL;
  659. struct ICARUS_INFO *info = icarus_info[cgpu->device_id];
  660. // Warning, access to these is not locked - but we don't really
  661. // care since hashing performance is way more important than
  662. // locking access to displaying API debug 'stats'
  663. // If locking becomes an issue for any of them, use copy_data=true also
  664. root = api_add_int(root, "read_count", &(info->read_count), false);
  665. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  666. root = api_add_int(root, "count", &(info->count), false);
  667. root = api_add_hs(root, "Hs", &(info->Hs), false);
  668. root = api_add_double(root, "W", &(info->W), false);
  669. root = api_add_uint(root, "total_values", &(info->values), false);
  670. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  671. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  672. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  673. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  674. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  675. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  676. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  677. return root;
  678. }
  679. static void icarus_shutdown(struct thr_info *thr)
  680. {
  681. struct cgpu_info *icarus = thr->cgpu;
  682. icarus_close(icarus->device_fd);
  683. free(thr->cgpu_data);
  684. }
  685. struct device_api icarus_api = {
  686. .dname = "icarus",
  687. .name = "ICA",
  688. .api_detect = icarus_detect,
  689. .get_api_stats = icarus_api_stats,
  690. .thread_prepare = icarus_prepare,
  691. .scanhash = icarus_scanhash,
  692. .thread_shutdown = icarus_shutdown,
  693. };