driver-icarus.c 27 KB

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