driver-icarus.c 24 KB

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