driver-icarus.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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(baud) ((double)ICARUS_READ_SIZE * (double)8.0 / (double)(baud))
  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. // icarus-options
  163. int baud;
  164. int work_division;
  165. int fpga_count;
  166. uint32_t nonce_mask;
  167. };
  168. #define END_CONDITION 0x0000ffff
  169. // One for each possible device
  170. static struct ICARUS_INFO **icarus_info;
  171. // Looking for options in --icarus-timing and --icarus-options:
  172. //
  173. // Code increments this each time we start to look at a device
  174. // However, this means that if other devices are checked by
  175. // the Icarus code (e.g. BFL) they will count in the option offset
  176. //
  177. // This, however, is deterministic so that's OK
  178. //
  179. // If we were to increment after successfully finding an Icarus
  180. // that would be random since an Icarus may fail and thus we'd
  181. // not be able to predict the option order
  182. //
  183. // This also assumes that serial_detect() checks them sequentially
  184. // and in the order specified on the command line
  185. //
  186. static int option_offset = -1;
  187. struct device_api icarus_api;
  188. static void rev(unsigned char *s, size_t l)
  189. {
  190. size_t i, j;
  191. unsigned char t;
  192. for (i = 0, j = l - 1; i < j; i++, j--) {
  193. t = s[i];
  194. s[i] = s[j];
  195. s[j] = t;
  196. }
  197. }
  198. #define icarus_open2(devpath, baud, purge) serial_open(devpath, baud, ICARUS_READ_FAULT_DECISECONDS, purge)
  199. #define icarus_open(devpath, baud) icarus_open2(devpath, baud, false)
  200. static int icarus_gets(unsigned char *buf, int fd, struct timeval *tv_finish, struct thr_info *thr, int read_count)
  201. {
  202. ssize_t ret = 0;
  203. int rc = 0;
  204. int epollfd = -1;
  205. int read_amount = ICARUS_READ_SIZE;
  206. bool first = true;
  207. #ifdef HAVE_EPOLL
  208. struct epoll_event ev;
  209. struct epoll_event evr[2];
  210. int epoll_timeout = ICARUS_READ_FAULT_DECISECONDS * 100;
  211. epollfd = epoll_create(2);
  212. if (epollfd != -1) {
  213. ev.events = EPOLLIN;
  214. ev.data.fd = fd;
  215. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
  216. close(epollfd);
  217. epollfd = -1;
  218. }
  219. if (thr->work_restart_fd != -1)
  220. {
  221. ev.data.fd = thr->work_restart_fd;
  222. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, thr->work_restart_fd, &ev))
  223. applog(LOG_ERR, "Icarus: Error adding work restart fd to epoll");
  224. else
  225. {
  226. epoll_timeout *= read_count;
  227. read_count = 1;
  228. }
  229. }
  230. }
  231. else
  232. applog(LOG_ERR, "Icarus: Error creating epoll");
  233. #endif
  234. // Read reply 1 byte at a time to get earliest tv_finish
  235. while (true) {
  236. #ifdef HAVE_EPOLL
  237. if (epollfd != -1 && (ret = epoll_wait(epollfd, evr, 2, epoll_timeout)) != -1)
  238. {
  239. if (ret == 1 && evr[0].data.fd == fd)
  240. ret = read(fd, buf, 1);
  241. else
  242. {
  243. if (ret)
  244. // work restart trigger
  245. (void)read(thr->work_restart_fd, buf, read_amount);
  246. ret = 0;
  247. }
  248. }
  249. else
  250. #endif
  251. ret = read(fd, buf, 1);
  252. if (first)
  253. gettimeofday(tv_finish, NULL);
  254. if (ret >= read_amount)
  255. {
  256. if (epollfd != -1)
  257. close(epollfd);
  258. return 0;
  259. }
  260. if (ret > 0) {
  261. buf += ret;
  262. read_amount -= ret;
  263. first = false;
  264. continue;
  265. }
  266. rc++;
  267. if (rc >= read_count || thr->work_restart) {
  268. if (epollfd != -1)
  269. close(epollfd);
  270. if (opt_debug) {
  271. rc *= ICARUS_READ_FAULT_DECISECONDS;
  272. applog(LOG_DEBUG,
  273. "Icarus Read: %s %d.%d seconds",
  274. thr->work_restart ? "Work restart at" : "No data in",
  275. rc / 10, rc % 10);
  276. }
  277. return 1;
  278. }
  279. }
  280. }
  281. static int icarus_write(int fd, const void *buf, size_t bufLen)
  282. {
  283. size_t ret;
  284. ret = write(fd, buf, bufLen);
  285. if (unlikely(ret != bufLen))
  286. return 1;
  287. return 0;
  288. }
  289. #define icarus_close(fd) close(fd)
  290. static const char *timing_mode_str(enum timing_mode timing_mode)
  291. {
  292. switch(timing_mode) {
  293. case MODE_DEFAULT:
  294. return MODE_DEFAULT_STR;
  295. case MODE_SHORT:
  296. return MODE_SHORT_STR;
  297. case MODE_LONG:
  298. return MODE_LONG_STR;
  299. case MODE_VALUE:
  300. return MODE_VALUE_STR;
  301. default:
  302. return MODE_UNKNOWN_STR;
  303. }
  304. }
  305. static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
  306. {
  307. struct ICARUS_INFO *info = icarus_info[icarus->device_id];
  308. double Hs;
  309. char buf[BUFSIZ+1];
  310. char *ptr, *comma, *eq;
  311. size_t max;
  312. int i;
  313. if (opt_icarus_timing == NULL)
  314. buf[0] = '\0';
  315. else {
  316. ptr = opt_icarus_timing;
  317. for (i = 0; i < this_option_offset; i++) {
  318. comma = strchr(ptr, ',');
  319. if (comma == NULL)
  320. break;
  321. ptr = comma + 1;
  322. }
  323. comma = strchr(ptr, ',');
  324. if (comma == NULL)
  325. max = strlen(ptr);
  326. else
  327. max = comma - ptr;
  328. if (max > BUFSIZ)
  329. max = BUFSIZ;
  330. strncpy(buf, ptr, max);
  331. buf[max] = '\0';
  332. }
  333. info->Hs = 0;
  334. info->read_count = 0;
  335. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  336. info->Hs = ICARUS_REV3_HASH_TIME;
  337. info->read_count = ICARUS_READ_COUNT_TIMING;
  338. info->timing_mode = MODE_SHORT;
  339. info->do_icarus_timing = true;
  340. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  341. info->Hs = ICARUS_REV3_HASH_TIME;
  342. info->read_count = ICARUS_READ_COUNT_TIMING;
  343. info->timing_mode = MODE_LONG;
  344. info->do_icarus_timing = true;
  345. } else if ((Hs = atof(buf)) != 0) {
  346. info->Hs = Hs / NANOSEC;
  347. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  348. if ((eq = strchr(buf, '=')) != NULL)
  349. info->read_count = atoi(eq+1);
  350. if (info->read_count < 1)
  351. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  352. if (unlikely(info->read_count < 1))
  353. info->read_count = 1;
  354. info->timing_mode = MODE_VALUE;
  355. info->do_icarus_timing = false;
  356. } else {
  357. // Anything else in buf just uses DEFAULT mode
  358. info->Hs = ICARUS_REV3_HASH_TIME;
  359. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  360. if ((eq = strchr(buf, '=')) != NULL)
  361. info->read_count = atoi(eq+1);
  362. if (info->read_count < 1)
  363. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  364. info->timing_mode = MODE_DEFAULT;
  365. info->do_icarus_timing = false;
  366. }
  367. info->min_data_count = MIN_DATA_COUNT;
  368. applog(LOG_DEBUG, "Icarus: Init: %d mode=%s read_count=%d Hs=%e",
  369. icarus->device_id, timing_mode_str(info->timing_mode), info->read_count, info->Hs);
  370. }
  371. static uint32_t mask(int work_division)
  372. {
  373. char err_buf[BUFSIZ+1];
  374. uint32_t nonce_mask = 0x7fffffff;
  375. // yes we can calculate these, but this way it's easy to see what they are
  376. switch (work_division) {
  377. case 1:
  378. nonce_mask = 0xffffffff;
  379. break;
  380. case 2:
  381. nonce_mask = 0x7fffffff;
  382. break;
  383. case 4:
  384. nonce_mask = 0x3fffffff;
  385. break;
  386. case 8:
  387. nonce_mask = 0x1fffffff;
  388. break;
  389. default:
  390. sprintf(err_buf, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division);
  391. quit(1, err_buf);
  392. }
  393. return nonce_mask;
  394. }
  395. static void get_options(int this_option_offset, int *baud, int *work_division, int *fpga_count)
  396. {
  397. char err_buf[BUFSIZ+1];
  398. char buf[BUFSIZ+1];
  399. char *ptr, *comma, *colon, *colon2;
  400. size_t max;
  401. int i, tmp;
  402. if (opt_icarus_options == NULL)
  403. buf[0] = '\0';
  404. else {
  405. ptr = opt_icarus_options;
  406. for (i = 0; i < this_option_offset; i++) {
  407. comma = strchr(ptr, ',');
  408. if (comma == NULL)
  409. break;
  410. ptr = comma + 1;
  411. }
  412. comma = strchr(ptr, ',');
  413. if (comma == NULL)
  414. max = strlen(ptr);
  415. else
  416. max = comma - ptr;
  417. if (max > BUFSIZ)
  418. max = BUFSIZ;
  419. strncpy(buf, ptr, max);
  420. buf[max] = '\0';
  421. }
  422. *baud = ICARUS_IO_SPEED;
  423. *work_division = 2;
  424. *fpga_count = 2;
  425. if (*buf) {
  426. colon = strchr(buf, ':');
  427. if (colon)
  428. *(colon++) = '\0';
  429. if (*buf) {
  430. tmp = atoi(buf);
  431. switch (tmp) {
  432. case 115200:
  433. *baud = 115200;
  434. break;
  435. case 57600:
  436. *baud = 57600;
  437. break;
  438. default:
  439. sprintf(err_buf, "Invalid icarus-options for baud (%s) must be 115200 or 57600", buf);
  440. quit(1, err_buf);
  441. }
  442. }
  443. if (colon && *colon) {
  444. colon2 = strchr(colon, ':');
  445. if (colon2)
  446. *(colon2++) = '\0';
  447. if (*colon) {
  448. tmp = atoi(colon);
  449. if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) {
  450. *work_division = tmp;
  451. *fpga_count = tmp; // default to the same
  452. } else {
  453. sprintf(err_buf, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
  454. quit(1, err_buf);
  455. }
  456. }
  457. if (colon2 && *colon2) {
  458. tmp = atoi(colon2);
  459. if (tmp > 0 && tmp <= *work_division)
  460. *fpga_count = tmp;
  461. else {
  462. sprintf(err_buf, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
  463. quit(1, err_buf);
  464. }
  465. }
  466. }
  467. }
  468. }
  469. static bool icarus_detect_one(const char *devpath)
  470. {
  471. int this_option_offset = ++option_offset;
  472. struct ICARUS_INFO *info;
  473. struct timeval tv_start, tv_finish;
  474. int fd;
  475. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  476. // N.B. golden_ob MUST take less time to calculate
  477. // than the timeout set in icarus_open()
  478. // This one takes ~0.53ms on Rev3 Icarus
  479. const char golden_ob[] =
  480. "4679ba4ec99876bf4bfe086082b40025"
  481. "4df6c356451471139a3afa71e48f544a"
  482. "00000000000000000000000000000000"
  483. "0000000087320b1a1426674f2fa722ce";
  484. /* NOTE: This gets sent to basically every port specified in --scan-serial,
  485. * even ones that aren't Icarus; be sure they can all handle it, when
  486. * this is changed...
  487. * BitForce: Ignores entirely
  488. * ModMiner: Starts (useless) work, gets back to clean state
  489. */
  490. const char golden_nonce[] = "000187a2";
  491. const uint32_t golden_nonce_val = 0x000187a2;
  492. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  493. char *nonce_hex;
  494. int baud, work_division, fpga_count;
  495. get_options(this_option_offset, &baud, &work_division, &fpga_count);
  496. applog(LOG_DEBUG, "Icarus Detect: Attempting to open %s", devpath);
  497. fd = icarus_open2(devpath, baud, true);
  498. if (unlikely(fd == -1)) {
  499. applog(LOG_DEBUG, "Icarus Detect: Failed to open %s", devpath);
  500. return false;
  501. }
  502. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  503. icarus_write(fd, ob_bin, sizeof(ob_bin));
  504. gettimeofday(&tv_start, NULL);
  505. memset(nonce_bin, 0, sizeof(nonce_bin));
  506. struct thr_info dummy = {
  507. .work_restart = false,
  508. .work_restart_fd = -1,
  509. };
  510. icarus_gets(nonce_bin, fd, &tv_finish, &dummy, 1);
  511. icarus_close(fd);
  512. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  513. if (nonce_hex) {
  514. if (strncmp(nonce_hex, golden_nonce, 8)) {
  515. applog(LOG_DEBUG,
  516. "Icarus Detect: "
  517. "Test failed at %s: get %s, should: %s",
  518. devpath, nonce_hex, golden_nonce);
  519. free(nonce_hex);
  520. return false;
  521. }
  522. applog(LOG_DEBUG,
  523. "Icarus Detect: "
  524. "Test succeeded at %s: got %s",
  525. devpath, nonce_hex);
  526. free(nonce_hex);
  527. } else
  528. return false;
  529. /* We have a real Icarus! */
  530. struct cgpu_info *icarus;
  531. icarus = calloc(1, sizeof(struct cgpu_info));
  532. icarus->api = &icarus_api;
  533. icarus->device_path = strdup(devpath);
  534. icarus->threads = 1;
  535. add_cgpu(icarus);
  536. icarus_info = realloc(icarus_info, sizeof(struct ICARUS_INFO *) * (total_devices + 1));
  537. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  538. devpath, icarus->device_id);
  539. applog(LOG_DEBUG, "Icarus: Init: %d baud=%d work_division=%d fpga_count=%d",
  540. icarus->device_id, baud, work_division, fpga_count);
  541. // Since we are adding a new device on the end it needs to always be allocated
  542. icarus_info[icarus->device_id] = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  543. if (unlikely(!(icarus_info[icarus->device_id])))
  544. quit(1, "Failed to malloc ICARUS_INFO");
  545. info = icarus_info[icarus->device_id];
  546. // Initialise everything to zero for a new device
  547. memset(info, 0, sizeof(struct ICARUS_INFO));
  548. info->baud = baud;
  549. info->work_division = work_division;
  550. info->fpga_count = fpga_count;
  551. info->nonce_mask = mask(work_division);
  552. info->golden_hashes = (golden_nonce_val & info->nonce_mask) * fpga_count;
  553. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  554. set_timing_mode(this_option_offset, icarus);
  555. return true;
  556. }
  557. static void icarus_detect()
  558. {
  559. serial_detect(icarus_api.dname, icarus_detect_one);
  560. }
  561. struct icarus_state {
  562. bool firstrun;
  563. struct timeval tv_workstart;
  564. struct timeval tv_workfinish;
  565. struct work last_work;
  566. bool changework;
  567. };
  568. static bool icarus_prepare(struct thr_info *thr)
  569. {
  570. struct cgpu_info *icarus = thr->cgpu;
  571. struct timeval now;
  572. int fd = icarus_open2(icarus->device_path, icarus_info[icarus->device_id]->baud, true);
  573. if (unlikely(-1 == fd)) {
  574. applog(LOG_ERR, "Failed to open Icarus on %s",
  575. icarus->device_path);
  576. return false;
  577. }
  578. icarus->device_fd = fd;
  579. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  580. gettimeofday(&now, NULL);
  581. get_datestamp(icarus->init, &now);
  582. struct icarus_state *state;
  583. thr->cgpu_data = state = calloc(1, sizeof(*state));
  584. state->firstrun = true;
  585. #ifdef HAVE_EPOLL
  586. int epollfd = epoll_create(2);
  587. if (epollfd != -1)
  588. {
  589. close(epollfd);
  590. thr->work_restart_fd = 0;
  591. }
  592. #endif
  593. return true;
  594. }
  595. static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  596. __maybe_unused int64_t max_nonce)
  597. {
  598. struct cgpu_info *icarus;
  599. int fd;
  600. int ret, lret;
  601. struct ICARUS_INFO *info;
  602. unsigned char ob_bin[64] = {0}, nonce_bin[ICARUS_READ_SIZE] = {0};
  603. char *ob_hex;
  604. uint32_t nonce;
  605. int64_t hash_count;
  606. struct timeval tv_start, elapsed;
  607. struct timeval tv_history_start, tv_history_finish;
  608. double Ti, Xi;
  609. int i;
  610. struct ICARUS_HISTORY *history0, *history;
  611. int count;
  612. double Hs, W, fullnonce;
  613. int read_count;
  614. int64_t estimate_hashes;
  615. uint32_t values;
  616. int64_t hash_count_range;
  617. elapsed.tv_sec = elapsed.tv_usec = 0;
  618. icarus = thr->cgpu;
  619. struct icarus_state *state = thr->cgpu_data;
  620. // Prepare the next work immediately
  621. memcpy(ob_bin, work->midstate, 32);
  622. memcpy(ob_bin + 52, work->data + 64, 12);
  623. rev(ob_bin, 32);
  624. rev(ob_bin + 52, 12);
  625. // Wait for the previous run's result
  626. fd = icarus->device_fd;
  627. info = icarus_info[icarus->device_id];
  628. if (!state->firstrun) {
  629. if (state->changework)
  630. state->changework = false;
  631. else
  632. {
  633. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  634. lret = icarus_gets(nonce_bin, fd, &state->tv_workfinish, thr, info->read_count);
  635. if (lret && thr->work_restart) {
  636. // The prepared work is invalid, and the current work is abandoned
  637. // Go back to the main loop to get the next work, and stuff
  638. // Returning to the main loop will clear work_restart, so use a flag...
  639. state->changework = true;
  640. return 0;
  641. }
  642. }
  643. tv_start = state->tv_workstart;
  644. timersub(&state->tv_workfinish, &tv_start, &elapsed);
  645. }
  646. #ifndef WIN32
  647. tcflush(fd, TCOFLUSH);
  648. #endif
  649. gettimeofday(&state->tv_workstart, NULL);
  650. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  651. if (ret) {
  652. icarus_close(fd);
  653. return -1; /* This should never happen */
  654. }
  655. if (opt_debug) {
  656. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  657. if (ob_hex) {
  658. applog(LOG_DEBUG, "Icarus %d sent: %s",
  659. icarus->device_id, ob_hex);
  660. free(ob_hex);
  661. }
  662. }
  663. // Reopen the serial port to workaround a USB-host-chipset-specific issue with the Icarus's buggy USB-UART
  664. icarus_close(fd);
  665. fd = icarus_open(icarus->device_path, icarus_info[icarus->device_id]->baud);
  666. if (unlikely(-1 == fd)) {
  667. applog(LOG_ERR, "Failed to reopen Icarus on %s",
  668. icarus->device_path);
  669. return 0;
  670. }
  671. icarus->device_fd = fd;
  672. work->blk.nonce = 0xffffffff;
  673. if (state->firstrun) {
  674. state->firstrun = false;
  675. memcpy(&state->last_work, work, sizeof(state->last_work));
  676. return 0;
  677. }
  678. // OK, done starting Icarus's next job... now process the last run's result!
  679. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  680. // aborted before becoming idle, get new work
  681. if (nonce == 0 && lret) {
  682. memcpy(&state->last_work, work, sizeof(state->last_work));
  683. // ONLY up to just when it aborted
  684. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  685. estimate_hashes = ((double)(elapsed.tv_sec)
  686. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  687. // If some Serial-USB delay allowed the full nonce range to
  688. // complete it can't have done more than a full nonce
  689. if (unlikely(estimate_hashes > 0xffffffff))
  690. estimate_hashes = 0xffffffff;
  691. if (opt_debug) {
  692. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08llx hashes (%ld.%06lds)",
  693. icarus->device_id, estimate_hashes,
  694. elapsed.tv_sec, elapsed.tv_usec);
  695. }
  696. return estimate_hashes;
  697. }
  698. nonce = be32toh(nonce);
  699. submit_nonce(thr, &state->last_work, nonce);
  700. memcpy(&state->last_work, work, sizeof(state->last_work));
  701. hash_count = (nonce & info->nonce_mask);
  702. hash_count++;
  703. hash_count *= info->fpga_count;
  704. if (opt_debug) {
  705. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08llx hashes (%ld.%06lds)",
  706. icarus->device_id, nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  707. }
  708. // ignore possible end condition values
  709. if (info->do_icarus_timing
  710. && ((nonce & info->nonce_mask) > END_CONDITION)
  711. && ((nonce & info->nonce_mask) < (info->nonce_mask & ~END_CONDITION))) {
  712. gettimeofday(&tv_history_start, NULL);
  713. history0 = &(info->history[0]);
  714. if (history0->values == 0)
  715. timeradd(&tv_start, &history_sec, &(history0->finish));
  716. Ti = (double)(elapsed.tv_sec)
  717. + ((double)(elapsed.tv_usec))/((double)1000000)
  718. - ((double)ICARUS_READ_TIME(info->baud));
  719. Xi = (double)hash_count;
  720. history0->sumXiTi += Xi * Ti;
  721. history0->sumXi += Xi;
  722. history0->sumTi += Ti;
  723. history0->sumXi2 += Xi * Xi;
  724. history0->values++;
  725. if (history0->hash_count_max < hash_count)
  726. history0->hash_count_max = hash_count;
  727. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  728. history0->hash_count_min = hash_count;
  729. if (history0->values >= info->min_data_count
  730. && timercmp(&tv_start, &(history0->finish), >)) {
  731. for (i = INFO_HISTORY; i > 0; i--)
  732. memcpy(&(info->history[i]),
  733. &(info->history[i-1]),
  734. sizeof(struct ICARUS_HISTORY));
  735. // Initialise history0 to zero for summary calculation
  736. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  737. // We just completed a history data set
  738. // So now recalc read_count based on the whole history thus we will
  739. // initially get more accurate until it completes INFO_HISTORY
  740. // total data sets
  741. count = 0;
  742. for (i = 1 ; i <= INFO_HISTORY; i++) {
  743. history = &(info->history[i]);
  744. if (history->values >= MIN_DATA_COUNT) {
  745. count++;
  746. history0->sumXiTi += history->sumXiTi;
  747. history0->sumXi += history->sumXi;
  748. history0->sumTi += history->sumTi;
  749. history0->sumXi2 += history->sumXi2;
  750. history0->values += history->values;
  751. if (history0->hash_count_max < history->hash_count_max)
  752. history0->hash_count_max = history->hash_count_max;
  753. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  754. history0->hash_count_min = history->hash_count_min;
  755. }
  756. }
  757. // All history data
  758. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  759. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  760. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  761. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  762. values = history0->values;
  763. // Initialise history0 to zero for next data set
  764. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  765. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  766. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  767. info->Hs = Hs;
  768. info->read_count = read_count;
  769. info->fullnonce = fullnonce;
  770. info->count = count;
  771. info->W = W;
  772. info->values = values;
  773. info->hash_count_range = hash_count_range;
  774. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  775. info->min_data_count *= 2;
  776. else if (info->timing_mode == MODE_SHORT)
  777. info->do_icarus_timing = false;
  778. // 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);
  779. applog(LOG_WARNING, "Icarus %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  780. icarus->device_id, Hs, W, read_count, fullnonce);
  781. }
  782. info->history_count++;
  783. gettimeofday(&tv_history_finish, NULL);
  784. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  785. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  786. }
  787. return hash_count;
  788. }
  789. static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
  790. {
  791. struct api_data *root = NULL;
  792. struct ICARUS_INFO *info = icarus_info[cgpu->device_id];
  793. // Warning, access to these is not locked - but we don't really
  794. // care since hashing performance is way more important than
  795. // locking access to displaying API debug 'stats'
  796. // If locking becomes an issue for any of them, use copy_data=true also
  797. root = api_add_int(root, "read_count", &(info->read_count), false);
  798. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  799. root = api_add_int(root, "count", &(info->count), false);
  800. root = api_add_hs(root, "Hs", &(info->Hs), false);
  801. root = api_add_double(root, "W", &(info->W), false);
  802. root = api_add_uint(root, "total_values", &(info->values), false);
  803. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  804. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  805. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  806. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  807. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  808. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  809. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  810. root = api_add_int(root, "baud", &(info->baud), false);
  811. root = api_add_int(root, "work_division", &(info->work_division), false);
  812. root = api_add_int(root, "fpga_count", &(info->fpga_count), false);
  813. return root;
  814. }
  815. static void icarus_shutdown(struct thr_info *thr)
  816. {
  817. struct cgpu_info *icarus = thr->cgpu;
  818. icarus_close(icarus->device_fd);
  819. free(thr->cgpu_data);
  820. }
  821. struct device_api icarus_api = {
  822. .dname = "icarus",
  823. .name = "ICA",
  824. .api_detect = icarus_detect,
  825. .get_api_stats = icarus_api_stats,
  826. .thread_prepare = icarus_prepare,
  827. .scanhash = icarus_scanhash,
  828. .thread_shutdown = icarus_shutdown,
  829. };