driver-icarus.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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. .events = EPOLLIN,
  210. .data.fd = fd,
  211. };
  212. struct epoll_event evr[2];
  213. int epoll_timeout = ICARUS_READ_FAULT_DECISECONDS * 100;
  214. epollfd = epoll_create(2);
  215. if (epollfd != -1) {
  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)
  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. if (opt_icarus_options == NULL)
  404. buf[0] = '\0';
  405. else {
  406. ptr = opt_icarus_options;
  407. for (i = 0; i < this_option_offset; i++) {
  408. comma = strchr(ptr, ',');
  409. if (comma == NULL)
  410. break;
  411. ptr = comma + 1;
  412. }
  413. comma = strchr(ptr, ',');
  414. if (comma == NULL)
  415. max = strlen(ptr);
  416. else
  417. max = comma - ptr;
  418. if (max > BUFSIZ)
  419. max = BUFSIZ;
  420. strncpy(buf, ptr, max);
  421. buf[max] = '\0';
  422. }
  423. *baud = ICARUS_IO_SPEED;
  424. *work_division = 2;
  425. *fpga_count = 2;
  426. if (*buf) {
  427. colon = strchr(buf, ':');
  428. if (colon)
  429. *(colon++) = '\0';
  430. if (*buf) {
  431. tmp = atoi(buf);
  432. switch (tmp) {
  433. case 115200:
  434. *baud = 115200;
  435. break;
  436. case 57600:
  437. *baud = 57600;
  438. break;
  439. default:
  440. sprintf(err_buf, "Invalid icarus-options for baud (%s) must be 115200 or 57600", buf);
  441. quit(1, err_buf);
  442. }
  443. }
  444. if (colon && *colon) {
  445. colon2 = strchr(colon, ':');
  446. if (colon2)
  447. *(colon2++) = '\0';
  448. if (*colon) {
  449. tmp = atoi(colon);
  450. if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) {
  451. *work_division = tmp;
  452. *fpga_count = tmp; // default to the same
  453. } else {
  454. sprintf(err_buf, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
  455. quit(1, err_buf);
  456. }
  457. }
  458. if (colon2 && *colon2) {
  459. tmp = atoi(colon2);
  460. if (tmp > 0 && tmp <= *work_division)
  461. *fpga_count = tmp;
  462. else {
  463. sprintf(err_buf, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
  464. quit(1, err_buf);
  465. }
  466. }
  467. }
  468. }
  469. }
  470. static bool icarus_detect_one(const char *devpath)
  471. {
  472. int this_option_offset = ++option_offset;
  473. struct ICARUS_INFO *info;
  474. struct timeval tv_start, tv_finish;
  475. int fd;
  476. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  477. // N.B. golden_ob MUST take less time to calculate
  478. // than the timeout set in icarus_open()
  479. // This one takes ~0.53ms on Rev3 Icarus
  480. const char golden_ob[] =
  481. "4679ba4ec99876bf4bfe086082b40025"
  482. "4df6c356451471139a3afa71e48f544a"
  483. "00000000000000000000000000000000"
  484. "0000000087320b1a1426674f2fa722ce";
  485. /* NOTE: This gets sent to basically every port specified in --scan-serial,
  486. * even ones that aren't Icarus; be sure they can all handle it, when
  487. * this is changed...
  488. * BitForce: Ignores entirely
  489. * ModMiner: Starts (useless) work, gets back to clean state
  490. */
  491. const char golden_nonce[] = "000187a2";
  492. const uint32_t golden_nonce_val = 0x000187a2;
  493. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  494. char *nonce_hex;
  495. int baud, work_division, fpga_count;
  496. get_options(this_option_offset, &baud, &work_division, &fpga_count);
  497. applog(LOG_DEBUG, "Icarus Detect: Attempting to open %s", devpath);
  498. fd = icarus_open2(devpath, baud, true);
  499. if (unlikely(fd == -1)) {
  500. applog(LOG_DEBUG, "Icarus Detect: Failed to open %s", devpath);
  501. return false;
  502. }
  503. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  504. icarus_write(fd, ob_bin, sizeof(ob_bin));
  505. gettimeofday(&tv_start, NULL);
  506. memset(nonce_bin, 0, sizeof(nonce_bin));
  507. struct thr_info dummy = {
  508. .work_restart = false,
  509. .work_restart_fd = -1,
  510. };
  511. icarus_gets(nonce_bin, fd, &tv_finish, &dummy, 1);
  512. icarus_close(fd);
  513. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  514. if (nonce_hex) {
  515. if (strncmp(nonce_hex, golden_nonce, 8)) {
  516. applog(LOG_DEBUG,
  517. "Icarus Detect: "
  518. "Test failed at %s: get %s, should: %s",
  519. devpath, nonce_hex, golden_nonce);
  520. free(nonce_hex);
  521. return false;
  522. }
  523. applog(LOG_DEBUG,
  524. "Icarus Detect: "
  525. "Test succeeded at %s: got %s",
  526. devpath, nonce_hex);
  527. free(nonce_hex);
  528. } else
  529. return false;
  530. /* We have a real Icarus! */
  531. struct cgpu_info *icarus;
  532. icarus = calloc(1, sizeof(struct cgpu_info));
  533. icarus->api = &icarus_api;
  534. icarus->device_path = strdup(devpath);
  535. icarus->threads = 1;
  536. add_cgpu(icarus);
  537. icarus_info = realloc(icarus_info, sizeof(struct ICARUS_INFO *) * (total_devices + 1));
  538. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  539. devpath, icarus->device_id);
  540. applog(LOG_DEBUG, "Icarus: Init: %d baud=%d work_division=%d fpga_count=%d",
  541. icarus->device_id, baud, work_division, fpga_count);
  542. // Since we are adding a new device on the end it needs to always be allocated
  543. icarus_info[icarus->device_id] = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  544. if (unlikely(!(icarus_info[icarus->device_id])))
  545. quit(1, "Failed to malloc ICARUS_INFO");
  546. info = icarus_info[icarus->device_id];
  547. // Initialise everything to zero for a new device
  548. memset(info, 0, sizeof(struct ICARUS_INFO));
  549. info->baud = baud;
  550. info->work_division = work_division;
  551. info->fpga_count = fpga_count;
  552. info->nonce_mask = mask(work_division);
  553. info->golden_hashes = (golden_nonce_val & info->nonce_mask) * fpga_count;
  554. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  555. set_timing_mode(this_option_offset, icarus);
  556. return true;
  557. }
  558. static void icarus_detect()
  559. {
  560. serial_detect(icarus_api.dname, icarus_detect_one);
  561. }
  562. struct icarus_state {
  563. bool firstrun;
  564. struct timeval tv_workstart;
  565. struct timeval tv_workfinish;
  566. struct work last_work;
  567. bool changework;
  568. };
  569. static bool icarus_prepare(struct thr_info *thr)
  570. {
  571. struct cgpu_info *icarus = thr->cgpu;
  572. struct timeval now;
  573. int fd = icarus_open2(icarus->device_path, icarus_info[icarus->device_id]->baud, true);
  574. if (unlikely(-1 == fd)) {
  575. applog(LOG_ERR, "Failed to open Icarus on %s",
  576. icarus->device_path);
  577. return false;
  578. }
  579. icarus->device_fd = fd;
  580. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  581. gettimeofday(&now, NULL);
  582. get_datestamp(icarus->init, &now);
  583. struct icarus_state *state;
  584. thr->cgpu_data = state = calloc(1, sizeof(*state));
  585. state->firstrun = true;
  586. #ifdef HAVE_EPOLL
  587. int epollfd = epoll_create(2);
  588. if (epollfd != -1)
  589. {
  590. close(epollfd);
  591. thr->work_restart_fd = 0;
  592. }
  593. #endif
  594. return true;
  595. }
  596. static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  597. __maybe_unused int64_t max_nonce)
  598. {
  599. struct cgpu_info *icarus;
  600. int fd;
  601. int ret, lret;
  602. struct ICARUS_INFO *info;
  603. unsigned char ob_bin[64] = {0}, nonce_bin[ICARUS_READ_SIZE] = {0};
  604. char *ob_hex;
  605. uint32_t nonce;
  606. int64_t hash_count;
  607. struct timeval tv_start, elapsed;
  608. struct timeval tv_history_start, tv_history_finish;
  609. double Ti, Xi;
  610. int i;
  611. struct ICARUS_HISTORY *history0, *history;
  612. int count;
  613. double Hs, W, fullnonce;
  614. int read_count;
  615. int64_t estimate_hashes;
  616. uint32_t values;
  617. int64_t hash_count_range;
  618. elapsed.tv_sec = elapsed.tv_usec = 0;
  619. icarus = thr->cgpu;
  620. struct icarus_state *state = thr->cgpu_data;
  621. // Prepare the next work immediately
  622. memcpy(ob_bin, work->midstate, 32);
  623. memcpy(ob_bin + 52, work->data + 64, 12);
  624. rev(ob_bin, 32);
  625. rev(ob_bin + 52, 12);
  626. // Wait for the previous run's result
  627. fd = icarus->device_fd;
  628. info = icarus_info[icarus->device_id];
  629. if (!state->firstrun) {
  630. if (state->changework)
  631. {
  632. state->changework = false;
  633. lret = 1;
  634. }
  635. else
  636. {
  637. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  638. lret = icarus_gets(nonce_bin, fd, &state->tv_workfinish, thr, info->read_count);
  639. if (lret && thr->work_restart) {
  640. // The prepared work is invalid, and the current work is abandoned
  641. // Go back to the main loop to get the next work, and stuff
  642. // Returning to the main loop will clear work_restart, so use a flag...
  643. state->changework = true;
  644. return 0;
  645. }
  646. }
  647. tv_start = state->tv_workstart;
  648. timersub(&state->tv_workfinish, &tv_start, &elapsed);
  649. }
  650. #ifndef WIN32
  651. tcflush(fd, TCOFLUSH);
  652. #endif
  653. gettimeofday(&state->tv_workstart, NULL);
  654. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  655. if (ret) {
  656. icarus_close(fd);
  657. return -1; /* This should never happen */
  658. }
  659. if (opt_debug) {
  660. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  661. if (ob_hex) {
  662. applog(LOG_DEBUG, "Icarus %d sent: %s",
  663. icarus->device_id, ob_hex);
  664. free(ob_hex);
  665. }
  666. }
  667. // Reopen the serial port to workaround a USB-host-chipset-specific issue with the Icarus's buggy USB-UART
  668. icarus_close(fd);
  669. fd = icarus_open(icarus->device_path, icarus_info[icarus->device_id]->baud);
  670. if (unlikely(-1 == fd)) {
  671. applog(LOG_ERR, "Failed to reopen Icarus on %s",
  672. icarus->device_path);
  673. return 0;
  674. }
  675. icarus->device_fd = fd;
  676. work->blk.nonce = 0xffffffff;
  677. if (state->firstrun) {
  678. state->firstrun = false;
  679. memcpy(&state->last_work, work, sizeof(state->last_work));
  680. return 0;
  681. }
  682. // OK, done starting Icarus's next job... now process the last run's result!
  683. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  684. // aborted before becoming idle, get new work
  685. if (nonce == 0 && lret) {
  686. memcpy(&state->last_work, work, sizeof(state->last_work));
  687. // ONLY up to just when it aborted
  688. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  689. estimate_hashes = ((double)(elapsed.tv_sec)
  690. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  691. // If some Serial-USB delay allowed the full nonce range to
  692. // complete it can't have done more than a full nonce
  693. if (unlikely(estimate_hashes > 0xffffffff))
  694. estimate_hashes = 0xffffffff;
  695. if (opt_debug) {
  696. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08llx hashes (%ld.%06lds)",
  697. icarus->device_id, estimate_hashes,
  698. elapsed.tv_sec, elapsed.tv_usec);
  699. }
  700. return estimate_hashes;
  701. }
  702. nonce = be32toh(nonce);
  703. submit_nonce(thr, &state->last_work, nonce);
  704. memcpy(&state->last_work, work, sizeof(state->last_work));
  705. hash_count = (nonce & info->nonce_mask);
  706. hash_count++;
  707. hash_count *= info->fpga_count;
  708. if (opt_debug) {
  709. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08llx hashes (%ld.%06lds)",
  710. icarus->device_id, nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  711. }
  712. // ignore possible end condition values
  713. if (info->do_icarus_timing
  714. && ((nonce & info->nonce_mask) > END_CONDITION)
  715. && ((nonce & info->nonce_mask) < (info->nonce_mask & ~END_CONDITION))) {
  716. gettimeofday(&tv_history_start, NULL);
  717. history0 = &(info->history[0]);
  718. if (history0->values == 0)
  719. timeradd(&tv_start, &history_sec, &(history0->finish));
  720. Ti = (double)(elapsed.tv_sec)
  721. + ((double)(elapsed.tv_usec))/((double)1000000)
  722. - ((double)ICARUS_READ_TIME(info->baud));
  723. Xi = (double)hash_count;
  724. history0->sumXiTi += Xi * Ti;
  725. history0->sumXi += Xi;
  726. history0->sumTi += Ti;
  727. history0->sumXi2 += Xi * Xi;
  728. history0->values++;
  729. if (history0->hash_count_max < hash_count)
  730. history0->hash_count_max = hash_count;
  731. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  732. history0->hash_count_min = hash_count;
  733. if (history0->values >= info->min_data_count
  734. && timercmp(&tv_start, &(history0->finish), >)) {
  735. for (i = INFO_HISTORY; i > 0; i--)
  736. memcpy(&(info->history[i]),
  737. &(info->history[i-1]),
  738. sizeof(struct ICARUS_HISTORY));
  739. // Initialise history0 to zero for summary calculation
  740. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  741. // We just completed a history data set
  742. // So now recalc read_count based on the whole history thus we will
  743. // initially get more accurate until it completes INFO_HISTORY
  744. // total data sets
  745. count = 0;
  746. for (i = 1 ; i <= INFO_HISTORY; i++) {
  747. history = &(info->history[i]);
  748. if (history->values >= MIN_DATA_COUNT) {
  749. count++;
  750. history0->sumXiTi += history->sumXiTi;
  751. history0->sumXi += history->sumXi;
  752. history0->sumTi += history->sumTi;
  753. history0->sumXi2 += history->sumXi2;
  754. history0->values += history->values;
  755. if (history0->hash_count_max < history->hash_count_max)
  756. history0->hash_count_max = history->hash_count_max;
  757. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  758. history0->hash_count_min = history->hash_count_min;
  759. }
  760. }
  761. // All history data
  762. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  763. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  764. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  765. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  766. values = history0->values;
  767. // Initialise history0 to zero for next data set
  768. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  769. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  770. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  771. info->Hs = Hs;
  772. info->read_count = read_count;
  773. info->fullnonce = fullnonce;
  774. info->count = count;
  775. info->W = W;
  776. info->values = values;
  777. info->hash_count_range = hash_count_range;
  778. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  779. info->min_data_count *= 2;
  780. else if (info->timing_mode == MODE_SHORT)
  781. info->do_icarus_timing = false;
  782. // 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);
  783. applog(LOG_WARNING, "Icarus %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  784. icarus->device_id, Hs, W, read_count, fullnonce);
  785. }
  786. info->history_count++;
  787. gettimeofday(&tv_history_finish, NULL);
  788. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  789. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  790. }
  791. return hash_count;
  792. }
  793. static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
  794. {
  795. struct api_data *root = NULL;
  796. struct ICARUS_INFO *info = icarus_info[cgpu->device_id];
  797. // Warning, access to these is not locked - but we don't really
  798. // care since hashing performance is way more important than
  799. // locking access to displaying API debug 'stats'
  800. // If locking becomes an issue for any of them, use copy_data=true also
  801. root = api_add_int(root, "read_count", &(info->read_count), false);
  802. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  803. root = api_add_int(root, "count", &(info->count), false);
  804. root = api_add_hs(root, "Hs", &(info->Hs), false);
  805. root = api_add_double(root, "W", &(info->W), false);
  806. root = api_add_uint(root, "total_values", &(info->values), false);
  807. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  808. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  809. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  810. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  811. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  812. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  813. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  814. root = api_add_int(root, "baud", &(info->baud), false);
  815. root = api_add_int(root, "work_division", &(info->work_division), false);
  816. root = api_add_int(root, "fpga_count", &(info->fpga_count), false);
  817. return root;
  818. }
  819. static void icarus_shutdown(struct thr_info *thr)
  820. {
  821. struct cgpu_info *icarus = thr->cgpu;
  822. icarus_close(icarus->device_fd);
  823. free(thr->cgpu_data);
  824. }
  825. struct device_api icarus_api = {
  826. .dname = "icarus",
  827. .name = "ICA",
  828. .api_detect = icarus_detect,
  829. .get_api_stats = icarus_api_stats,
  830. .thread_prepare = icarus_prepare,
  831. .scanhash = icarus_scanhash,
  832. .thread_shutdown = icarus_shutdown,
  833. };