driver-icarus.c 25 KB

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