driver-icarus.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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 "fpgautils.h"
  50. #include "miner.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. static int icarus_gets(unsigned char *buf, int fd, struct timeval *tv_finish, struct thr_info *thr, int read_count)
  195. {
  196. ssize_t ret = 0;
  197. int rc = 0;
  198. int read_amount = ICARUS_READ_SIZE;
  199. bool first = true;
  200. // Read reply 1 byte at a time to get earliest tv_finish
  201. while (true) {
  202. ret = read(fd, buf, 1);
  203. if (first)
  204. gettimeofday(tv_finish, NULL);
  205. if (ret >= read_amount)
  206. return 0;
  207. if (ret > 0) {
  208. buf += ret;
  209. read_amount -= ret;
  210. first = false;
  211. continue;
  212. }
  213. rc++;
  214. if (rc >= read_count) {
  215. if (opt_debug) {
  216. applog(LOG_DEBUG,
  217. "Icarus Read: No data in %.2f seconds",
  218. (float)rc/(float)TIME_FACTOR);
  219. }
  220. return 1;
  221. }
  222. if (thr->work_restart) {
  223. if (opt_debug) {
  224. applog(LOG_DEBUG,
  225. "Icarus Read: Work restart at %.2f seconds",
  226. (float)(rc)/(float)TIME_FACTOR);
  227. }
  228. return 1;
  229. }
  230. }
  231. }
  232. static int icarus_write(int fd, const void *buf, size_t bufLen)
  233. {
  234. size_t ret;
  235. ret = write(fd, buf, bufLen);
  236. if (unlikely(ret != bufLen))
  237. return 1;
  238. return 0;
  239. }
  240. #define icarus_close(fd) close(fd)
  241. static const char *timing_mode_str(enum timing_mode timing_mode)
  242. {
  243. switch(timing_mode) {
  244. case MODE_DEFAULT:
  245. return MODE_DEFAULT_STR;
  246. case MODE_SHORT:
  247. return MODE_SHORT_STR;
  248. case MODE_LONG:
  249. return MODE_LONG_STR;
  250. case MODE_VALUE:
  251. return MODE_VALUE_STR;
  252. default:
  253. return MODE_UNKNOWN_STR;
  254. }
  255. }
  256. static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
  257. {
  258. struct ICARUS_INFO *info = icarus_info[icarus->device_id];
  259. double Hs;
  260. char buf[BUFSIZ+1];
  261. char *ptr, *comma, *eq;
  262. size_t max;
  263. int i;
  264. if (opt_icarus_timing == NULL)
  265. buf[0] = '\0';
  266. else {
  267. ptr = opt_icarus_timing;
  268. for (i = 0; i < this_option_offset; i++) {
  269. comma = strchr(ptr, ',');
  270. if (comma == NULL)
  271. break;
  272. ptr = comma + 1;
  273. }
  274. comma = strchr(ptr, ',');
  275. if (comma == NULL)
  276. max = strlen(ptr);
  277. else
  278. max = comma - ptr;
  279. if (max > BUFSIZ)
  280. max = BUFSIZ;
  281. strncpy(buf, ptr, max);
  282. buf[max] = '\0';
  283. }
  284. info->Hs = 0;
  285. info->read_count = 0;
  286. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  287. info->Hs = ICARUS_REV3_HASH_TIME;
  288. info->read_count = ICARUS_READ_COUNT_TIMING;
  289. info->timing_mode = MODE_SHORT;
  290. info->do_icarus_timing = true;
  291. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  292. info->Hs = ICARUS_REV3_HASH_TIME;
  293. info->read_count = ICARUS_READ_COUNT_TIMING;
  294. info->timing_mode = MODE_LONG;
  295. info->do_icarus_timing = true;
  296. } else if ((Hs = atof(buf)) != 0) {
  297. info->Hs = Hs / NANOSEC;
  298. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  299. if ((eq = strchr(buf, '=')) != NULL)
  300. info->read_count = atoi(eq+1);
  301. if (info->read_count < 1)
  302. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  303. if (unlikely(info->read_count < 1))
  304. info->read_count = 1;
  305. info->timing_mode = MODE_VALUE;
  306. info->do_icarus_timing = false;
  307. } else {
  308. // Anything else in buf just uses DEFAULT mode
  309. info->Hs = ICARUS_REV3_HASH_TIME;
  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. info->timing_mode = MODE_DEFAULT;
  316. info->do_icarus_timing = false;
  317. }
  318. info->min_data_count = MIN_DATA_COUNT;
  319. applog(LOG_DEBUG, "Icarus: Init: %d mode=%s read_count=%d Hs=%e",
  320. icarus->device_id, timing_mode_str(info->timing_mode), info->read_count, info->Hs);
  321. }
  322. static uint32_t mask(int work_division)
  323. {
  324. char err_buf[BUFSIZ+1];
  325. uint32_t nonce_mask = 0x7fffffff;
  326. // yes we can calculate these, but this way it's easy to see what they are
  327. switch (work_division) {
  328. case 1:
  329. nonce_mask = 0xffffffff;
  330. break;
  331. case 2:
  332. nonce_mask = 0x7fffffff;
  333. break;
  334. case 4:
  335. nonce_mask = 0x3fffffff;
  336. break;
  337. case 8:
  338. nonce_mask = 0x1fffffff;
  339. break;
  340. default:
  341. sprintf(err_buf, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division);
  342. quit(1, err_buf);
  343. }
  344. return nonce_mask;
  345. }
  346. static void get_options(int this_option_offset, int *baud, int *work_division, int *fpga_count)
  347. {
  348. char err_buf[BUFSIZ+1];
  349. char buf[BUFSIZ+1];
  350. char *ptr, *comma, *colon, *colon2;
  351. size_t max;
  352. int i, tmp;
  353. if (opt_icarus_options == NULL)
  354. buf[0] = '\0';
  355. else {
  356. ptr = opt_icarus_options;
  357. for (i = 0; i < this_option_offset; i++) {
  358. comma = strchr(ptr, ',');
  359. if (comma == NULL)
  360. break;
  361. ptr = comma + 1;
  362. }
  363. comma = strchr(ptr, ',');
  364. if (comma == NULL)
  365. max = strlen(ptr);
  366. else
  367. max = comma - ptr;
  368. if (max > BUFSIZ)
  369. max = BUFSIZ;
  370. strncpy(buf, ptr, max);
  371. buf[max] = '\0';
  372. }
  373. *baud = ICARUS_IO_SPEED;
  374. *work_division = 2;
  375. *fpga_count = 2;
  376. if (*buf) {
  377. colon = strchr(buf, ':');
  378. if (colon)
  379. *(colon++) = '\0';
  380. if (*buf) {
  381. tmp = atoi(buf);
  382. switch (tmp) {
  383. case 115200:
  384. *baud = 115200;
  385. break;
  386. case 57600:
  387. *baud = 57600;
  388. break;
  389. default:
  390. sprintf(err_buf, "Invalid icarus-options for baud (%s) must be 115200 or 57600", buf);
  391. quit(1, err_buf);
  392. }
  393. }
  394. if (colon && *colon) {
  395. colon2 = strchr(colon, ':');
  396. if (colon2)
  397. *(colon2++) = '\0';
  398. if (*colon) {
  399. tmp = atoi(colon);
  400. if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) {
  401. *work_division = tmp;
  402. *fpga_count = tmp; // default to the same
  403. } else {
  404. sprintf(err_buf, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
  405. quit(1, err_buf);
  406. }
  407. }
  408. if (colon2 && *colon2) {
  409. tmp = atoi(colon2);
  410. if (tmp > 0 && tmp <= *work_division)
  411. *fpga_count = tmp;
  412. else {
  413. sprintf(err_buf, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
  414. quit(1, err_buf);
  415. }
  416. }
  417. }
  418. }
  419. }
  420. static bool icarus_detect_one(const char *devpath)
  421. {
  422. int this_option_offset = ++option_offset;
  423. struct ICARUS_INFO *info;
  424. struct timeval tv_start, tv_finish;
  425. int fd;
  426. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  427. // N.B. golden_ob MUST take less time to calculate
  428. // than the timeout set in icarus_open()
  429. // This one takes ~0.53ms on Rev3 Icarus
  430. const char golden_ob[] =
  431. "4679ba4ec99876bf4bfe086082b40025"
  432. "4df6c356451471139a3afa71e48f544a"
  433. "00000000000000000000000000000000"
  434. "0000000087320b1a1426674f2fa722ce";
  435. const char golden_nonce[] = "000187a2";
  436. const uint32_t golden_nonce_val = 0x000187a2;
  437. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  438. char *nonce_hex;
  439. int baud, work_division, fpga_count;
  440. get_options(this_option_offset, &baud, &work_division, &fpga_count);
  441. applog(LOG_DEBUG, "Icarus Detect: Attempting to open %s", devpath);
  442. fd = icarus_open2(devpath, baud, true);
  443. if (unlikely(fd == -1)) {
  444. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  445. return false;
  446. }
  447. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  448. icarus_write(fd, ob_bin, sizeof(ob_bin));
  449. gettimeofday(&tv_start, NULL);
  450. memset(nonce_bin, 0, sizeof(nonce_bin));
  451. struct thr_info dummy = {
  452. .work_restart = false,
  453. };
  454. icarus_gets(nonce_bin, fd, &tv_finish, &dummy, 1);
  455. icarus_close(fd);
  456. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  457. if (nonce_hex) {
  458. if (strncmp(nonce_hex, golden_nonce, 8)) {
  459. applog(LOG_ERR,
  460. "Icarus Detect: "
  461. "Test failed at %s: get %s, should: %s",
  462. devpath, nonce_hex, golden_nonce);
  463. free(nonce_hex);
  464. return false;
  465. }
  466. applog(LOG_DEBUG,
  467. "Icarus Detect: "
  468. "Test succeeded at %s: got %s",
  469. devpath, nonce_hex);
  470. free(nonce_hex);
  471. } else
  472. return false;
  473. /* We have a real Icarus! */
  474. struct cgpu_info *icarus;
  475. icarus = calloc(1, sizeof(struct cgpu_info));
  476. icarus->api = &icarus_api;
  477. icarus->device_path = strdup(devpath);
  478. icarus->threads = 1;
  479. add_cgpu(icarus);
  480. icarus_info = realloc(icarus_info, sizeof(struct ICARUS_INFO *) * (total_devices + 1));
  481. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  482. devpath, icarus->device_id);
  483. applog(LOG_DEBUG, "Icarus: Init: %d baud=%d work_division=%d fpga_count=%d",
  484. icarus->device_id, baud, work_division, fpga_count);
  485. // Since we are adding a new device on the end it needs to always be allocated
  486. icarus_info[icarus->device_id] = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  487. if (unlikely(!(icarus_info[icarus->device_id])))
  488. quit(1, "Failed to malloc ICARUS_INFO");
  489. info = icarus_info[icarus->device_id];
  490. // Initialise everything to zero for a new device
  491. memset(info, 0, sizeof(struct ICARUS_INFO));
  492. info->baud = baud;
  493. info->work_division = work_division;
  494. info->fpga_count = fpga_count;
  495. info->nonce_mask = mask(work_division);
  496. info->golden_hashes = (golden_nonce_val & info->nonce_mask) * fpga_count;
  497. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  498. set_timing_mode(this_option_offset, icarus);
  499. return true;
  500. }
  501. static void icarus_detect()
  502. {
  503. serial_detect(icarus_api.dname, icarus_detect_one);
  504. }
  505. static bool icarus_prepare(struct thr_info *thr)
  506. {
  507. struct cgpu_info *icarus = thr->cgpu;
  508. struct timeval now;
  509. int fd = icarus_open(icarus->device_path, icarus_info[icarus->device_id]->baud);
  510. if (unlikely(-1 == fd)) {
  511. applog(LOG_ERR, "Failed to open Icarus on %s",
  512. icarus->device_path);
  513. return false;
  514. }
  515. icarus->device_fd = fd;
  516. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  517. gettimeofday(&now, NULL);
  518. get_datestamp(icarus->init, &now);
  519. return true;
  520. }
  521. static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  522. __maybe_unused int64_t max_nonce)
  523. {
  524. struct cgpu_info *icarus;
  525. int fd;
  526. int ret;
  527. struct ICARUS_INFO *info;
  528. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  529. char *ob_hex;
  530. uint32_t nonce;
  531. int64_t hash_count;
  532. struct timeval tv_start, tv_finish, elapsed;
  533. struct timeval tv_history_start, tv_history_finish;
  534. double Ti, Xi;
  535. int i;
  536. struct ICARUS_HISTORY *history0, *history;
  537. int count;
  538. double Hs, W, fullnonce;
  539. int read_count;
  540. int64_t estimate_hashes;
  541. uint32_t values;
  542. int64_t hash_count_range;
  543. elapsed.tv_sec = elapsed.tv_usec = 0;
  544. icarus = thr->cgpu;
  545. fd = icarus->device_fd;
  546. memset(ob_bin, 0, sizeof(ob_bin));
  547. memcpy(ob_bin, work->midstate, 32);
  548. memcpy(ob_bin + 52, work->data + 64, 12);
  549. rev(ob_bin, 32);
  550. rev(ob_bin + 52, 12);
  551. #ifndef WIN32
  552. tcflush(fd, TCOFLUSH);
  553. #endif
  554. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  555. if (ret)
  556. return -1; /* This should never happen */
  557. gettimeofday(&tv_start, NULL);
  558. if (opt_debug) {
  559. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  560. if (ob_hex) {
  561. applog(LOG_DEBUG, "Icarus %d sent: %s",
  562. icarus->device_id, ob_hex);
  563. free(ob_hex);
  564. }
  565. }
  566. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  567. memset(nonce_bin, 0, sizeof(nonce_bin));
  568. info = icarus_info[icarus->device_id];
  569. ret = icarus_gets(nonce_bin, fd, &tv_finish, thr, info->read_count);
  570. work->blk.nonce = 0xffffffff;
  571. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  572. // aborted before becoming idle, get new work
  573. if (nonce == 0 && ret) {
  574. timersub(&tv_finish, &tv_start, &elapsed);
  575. // ONLY up to just when it aborted
  576. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  577. estimate_hashes = ((double)(elapsed.tv_sec)
  578. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  579. // If some Serial-USB delay allowed the full nonce range to
  580. // complete it can't have done more than a full nonce
  581. if (unlikely(estimate_hashes > 0xffffffff))
  582. estimate_hashes = 0xffffffff;
  583. if (opt_debug) {
  584. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08llx hashes (%ld.%06lds)",
  585. icarus->device_id, estimate_hashes,
  586. elapsed.tv_sec, elapsed.tv_usec);
  587. }
  588. return estimate_hashes;
  589. }
  590. #if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
  591. nonce = swab32(nonce);
  592. #endif
  593. submit_nonce(thr, work, nonce);
  594. hash_count = (nonce & info->nonce_mask);
  595. hash_count++;
  596. hash_count *= info->fpga_count;
  597. if (opt_debug || info->do_icarus_timing)
  598. timersub(&tv_finish, &tv_start, &elapsed);
  599. if (opt_debug) {
  600. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08llx hashes (%ld.%06lds)",
  601. icarus->device_id, nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  602. }
  603. // ignore possible end condition values
  604. if (info->do_icarus_timing
  605. && ((nonce & info->nonce_mask) > END_CONDITION)
  606. && ((nonce & info->nonce_mask) < (info->nonce_mask & ~END_CONDITION))) {
  607. gettimeofday(&tv_history_start, NULL);
  608. history0 = &(info->history[0]);
  609. if (history0->values == 0)
  610. timeradd(&tv_start, &history_sec, &(history0->finish));
  611. Ti = (double)(elapsed.tv_sec)
  612. + ((double)(elapsed.tv_usec))/((double)1000000)
  613. - ((double)ICARUS_READ_TIME(info->baud));
  614. Xi = (double)hash_count;
  615. history0->sumXiTi += Xi * Ti;
  616. history0->sumXi += Xi;
  617. history0->sumTi += Ti;
  618. history0->sumXi2 += Xi * Xi;
  619. history0->values++;
  620. if (history0->hash_count_max < hash_count)
  621. history0->hash_count_max = hash_count;
  622. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  623. history0->hash_count_min = hash_count;
  624. if (history0->values >= info->min_data_count
  625. && timercmp(&tv_start, &(history0->finish), >)) {
  626. for (i = INFO_HISTORY; i > 0; i--)
  627. memcpy(&(info->history[i]),
  628. &(info->history[i-1]),
  629. sizeof(struct ICARUS_HISTORY));
  630. // Initialise history0 to zero for summary calculation
  631. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  632. // We just completed a history data set
  633. // So now recalc read_count based on the whole history thus we will
  634. // initially get more accurate until it completes INFO_HISTORY
  635. // total data sets
  636. count = 0;
  637. for (i = 1 ; i <= INFO_HISTORY; i++) {
  638. history = &(info->history[i]);
  639. if (history->values >= MIN_DATA_COUNT) {
  640. count++;
  641. history0->sumXiTi += history->sumXiTi;
  642. history0->sumXi += history->sumXi;
  643. history0->sumTi += history->sumTi;
  644. history0->sumXi2 += history->sumXi2;
  645. history0->values += history->values;
  646. if (history0->hash_count_max < history->hash_count_max)
  647. history0->hash_count_max = history->hash_count_max;
  648. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  649. history0->hash_count_min = history->hash_count_min;
  650. }
  651. }
  652. // All history data
  653. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  654. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  655. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  656. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  657. values = history0->values;
  658. // Initialise history0 to zero for next data set
  659. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  660. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  661. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  662. info->Hs = Hs;
  663. info->read_count = read_count;
  664. info->fullnonce = fullnonce;
  665. info->count = count;
  666. info->W = W;
  667. info->values = values;
  668. info->hash_count_range = hash_count_range;
  669. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  670. info->min_data_count *= 2;
  671. else if (info->timing_mode == MODE_SHORT)
  672. info->do_icarus_timing = false;
  673. // 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);
  674. applog(LOG_WARNING, "Icarus %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  675. icarus->device_id, Hs, W, read_count, fullnonce);
  676. }
  677. info->history_count++;
  678. gettimeofday(&tv_history_finish, NULL);
  679. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  680. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  681. }
  682. return hash_count;
  683. }
  684. static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
  685. {
  686. struct api_data *root = NULL;
  687. struct ICARUS_INFO *info = icarus_info[cgpu->device_id];
  688. // Warning, access to these is not locked - but we don't really
  689. // care since hashing performance is way more important than
  690. // locking access to displaying API debug 'stats'
  691. // If locking becomes an issue for any of them, use copy_data=true also
  692. root = api_add_int(root, "read_count", &(info->read_count), false);
  693. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  694. root = api_add_int(root, "count", &(info->count), false);
  695. root = api_add_hs(root, "Hs", &(info->Hs), false);
  696. root = api_add_double(root, "W", &(info->W), false);
  697. root = api_add_uint(root, "total_values", &(info->values), false);
  698. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  699. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  700. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  701. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  702. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  703. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  704. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  705. root = api_add_int(root, "baud", &(info->baud), false);
  706. root = api_add_int(root, "work_division", &(info->work_division), false);
  707. root = api_add_int(root, "fpga_count", &(info->fpga_count), false);
  708. return root;
  709. }
  710. static void icarus_shutdown(struct thr_info *thr)
  711. {
  712. struct cgpu_info *icarus = thr->cgpu;
  713. icarus_close(icarus->device_fd);
  714. }
  715. struct device_api icarus_api = {
  716. .dname = "icarus",
  717. .name = "ICA",
  718. .api_detect = icarus_detect,
  719. .get_api_stats = icarus_api_stats,
  720. .thread_prepare = icarus_prepare,
  721. .scanhash = icarus_scanhash,
  722. .thread_shutdown = icarus_shutdown,
  723. };