driver-icarus.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
  51. #define ICARUS_IO_SPEED 115200
  52. // The size of a successful nonce read
  53. #define ICARUS_READ_SIZE 4
  54. // Ensure the sizes are correct for the Serial read
  55. #if (ICARUS_READ_SIZE != 4)
  56. #error ICARUS_READ_SIZE must be 4
  57. #endif
  58. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  59. ASSERT1(sizeof(uint32_t) == 4);
  60. #define ICARUS_READ_TIME ((double)ICARUS_READ_SIZE * (double)8.0 / (double)ICARUS_IO_SPEED)
  61. // Fraction of a second, USB timeout is measured in
  62. // i.e. 10 means 1/10 of a second
  63. #define TIME_FACTOR 10
  64. // In Linux it's 10 per second, thus value = 10/TIME_FACTOR =
  65. #define LINUX_TIMEOUT_VALUE 1
  66. // In Windows it's 1000 per second, thus value = 1000/TIME_FACTOR =
  67. #define WINDOWS_TIMEOUT_VALUE 100
  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. };
  158. // One for each possible device
  159. static struct ICARUS_INFO *icarus_info[MAX_DEVICES];
  160. struct device_api icarus_api;
  161. static void rev(unsigned char *s, size_t l)
  162. {
  163. size_t i, j;
  164. unsigned char t;
  165. for (i = 0, j = l - 1; i < j; i++, j--) {
  166. t = s[i];
  167. s[i] = s[j];
  168. s[j] = t;
  169. }
  170. }
  171. static int icarus_open(const char *devpath)
  172. {
  173. #ifndef WIN32
  174. struct termios my_termios;
  175. int serialfd = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  176. if (serialfd == -1)
  177. return -1;
  178. tcgetattr(serialfd, &my_termios);
  179. my_termios.c_cflag = B115200;
  180. my_termios.c_cflag |= CS8;
  181. my_termios.c_cflag |= CREAD;
  182. my_termios.c_cflag |= CLOCAL;
  183. my_termios.c_cflag &= ~(CSIZE | PARENB);
  184. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  185. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  186. my_termios.c_oflag &= ~OPOST;
  187. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  188. my_termios.c_cc[VTIME] = LINUX_TIMEOUT_VALUE; /* how long to block */
  189. my_termios.c_cc[VMIN] = 0;
  190. tcsetattr(serialfd, TCSANOW, &my_termios);
  191. tcflush(serialfd, TCOFLUSH);
  192. tcflush(serialfd, TCIFLUSH);
  193. return serialfd;
  194. #else
  195. COMMCONFIG comCfg;
  196. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0,
  197. NULL, OPEN_EXISTING, 0, NULL);
  198. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  199. return -1;
  200. // thanks to af_newbie for pointers about this
  201. memset(&comCfg, 0 , sizeof(comCfg));
  202. comCfg.dwSize = sizeof(COMMCONFIG);
  203. comCfg.wVersion = 1;
  204. comCfg.dcb.DCBlength = sizeof(DCB);
  205. comCfg.dcb.BaudRate = ICARUS_IO_SPEED;
  206. comCfg.dcb.fBinary = 1;
  207. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  208. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  209. comCfg.dcb.ByteSize = 8;
  210. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  211. // How long to block
  212. COMMTIMEOUTS cto = {WINDOWS_TIMEOUT_VALUE, 0, WINDOWS_TIMEOUT_VALUE, 0, WINDOWS_TIMEOUT_VALUE};
  213. SetCommTimeouts(hSerial, &cto);
  214. return _open_osfhandle((LONG)hSerial, 0);
  215. #endif
  216. }
  217. static int icarus_gets(unsigned char *buf, int fd, struct timeval *tv_finish, int thr_id, int read_count)
  218. {
  219. ssize_t ret = 0;
  220. int rc = 0;
  221. int read_amount = ICARUS_READ_SIZE;
  222. bool first = true;
  223. // Read reply 1 byte at a time to get earliest tv_finish
  224. while (true) {
  225. ret = read(fd, buf, 1);
  226. if (first)
  227. gettimeofday(tv_finish, NULL);
  228. if (ret >= read_amount)
  229. return 0;
  230. if (ret > 0) {
  231. buf += ret;
  232. read_amount -= ret;
  233. first = false;
  234. continue;
  235. }
  236. rc++;
  237. if (rc >= read_count) {
  238. if (opt_debug) {
  239. applog(LOG_DEBUG,
  240. "Icarus Read: No data in %.2f seconds",
  241. (float)rc/(float)TIME_FACTOR);
  242. }
  243. return 1;
  244. }
  245. if (thr_id >= 0 && work_restart[thr_id].restart) {
  246. if (opt_debug) {
  247. applog(LOG_DEBUG,
  248. "Icarus Read: Work restart at %.2f seconds",
  249. (float)(rc)/(float)TIME_FACTOR);
  250. }
  251. return 1;
  252. }
  253. }
  254. }
  255. static int icarus_write(int fd, const void *buf, size_t bufLen)
  256. {
  257. size_t ret;
  258. ret = write(fd, buf, bufLen);
  259. if (unlikely(ret != bufLen))
  260. return 1;
  261. return 0;
  262. }
  263. #define icarus_close(fd) close(fd)
  264. static const char *timing_mode_str(enum timing_mode timing_mode)
  265. {
  266. switch(timing_mode) {
  267. case MODE_DEFAULT:
  268. return MODE_DEFAULT_STR;
  269. case MODE_SHORT:
  270. return MODE_SHORT_STR;
  271. case MODE_LONG:
  272. return MODE_LONG_STR;
  273. case MODE_VALUE:
  274. return MODE_VALUE_STR;
  275. default:
  276. return MODE_UNKNOWN_STR;
  277. }
  278. }
  279. static void set_timing_mode(struct cgpu_info *icarus)
  280. {
  281. struct ICARUS_INFO *info = icarus_info[icarus->device_id];
  282. double Hs;
  283. char buf[BUFSIZ+1];
  284. char *ptr, *comma, *eq;
  285. size_t max;
  286. int i;
  287. if (opt_icarus_timing == NULL)
  288. buf[0] = '\0';
  289. else {
  290. ptr = opt_icarus_timing;
  291. for (i = 0; i < icarus->device_id; i++) {
  292. comma = strchr(ptr, ',');
  293. if (comma == NULL)
  294. break;
  295. ptr = comma + 1;
  296. }
  297. comma = strchr(ptr, ',');
  298. if (comma == NULL)
  299. max = strlen(ptr);
  300. else
  301. max = comma - ptr;
  302. if (max > BUFSIZ)
  303. max = BUFSIZ;
  304. strncpy(buf, ptr, max);
  305. buf[max] = '\0';
  306. }
  307. info->Hs = 0;
  308. info->read_count = 0;
  309. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  310. info->Hs = ICARUS_REV3_HASH_TIME;
  311. info->read_count = ICARUS_READ_COUNT_TIMING;
  312. info->timing_mode = MODE_SHORT;
  313. info->do_icarus_timing = true;
  314. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  315. info->Hs = ICARUS_REV3_HASH_TIME;
  316. info->read_count = ICARUS_READ_COUNT_TIMING;
  317. info->timing_mode = MODE_LONG;
  318. info->do_icarus_timing = true;
  319. } else if ((Hs = atof(buf)) != 0) {
  320. info->Hs = Hs / NANOSEC;
  321. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  322. if ((eq = strchr(buf, '=')) != NULL)
  323. info->read_count = atoi(eq+1);
  324. if (info->read_count < 1)
  325. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  326. if (unlikely(info->read_count < 1))
  327. info->read_count = 1;
  328. info->timing_mode = MODE_VALUE;
  329. info->do_icarus_timing = false;
  330. } else {
  331. // Anything else in buf just uses DEFAULT mode
  332. info->Hs = ICARUS_REV3_HASH_TIME;
  333. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  334. if ((eq = strchr(buf, '=')) != NULL)
  335. info->read_count = atoi(eq+1);
  336. if (info->read_count < 1)
  337. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  338. info->timing_mode = MODE_DEFAULT;
  339. info->do_icarus_timing = false;
  340. }
  341. info->min_data_count = MIN_DATA_COUNT;
  342. applog(LOG_DEBUG, "Icarus: Init: %d mode=%s read_count=%d Hs=%e",
  343. icarus->device_id, timing_mode_str(info->timing_mode), info->read_count, info->Hs);
  344. }
  345. static bool icarus_detect_one(const char *devpath)
  346. {
  347. struct ICARUS_INFO *info;
  348. struct timeval tv_start, tv_finish;
  349. int fd;
  350. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  351. // N.B. golden_ob MUST take less time to calculate
  352. // than the timeout set in icarus_open()
  353. // This one takes ~0.53ms on Rev3 Icarus
  354. const char golden_ob[] =
  355. "4679ba4ec99876bf4bfe086082b40025"
  356. "4df6c356451471139a3afa71e48f544a"
  357. "00000000000000000000000000000000"
  358. "0000000087320b1a1426674f2fa722ce";
  359. const char golden_nonce[] = "000187a2";
  360. const uint32_t golden_nonce_val = 0x000187a2;
  361. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  362. char *nonce_hex;
  363. if (total_devices == MAX_DEVICES)
  364. return false;
  365. fd = icarus_open(devpath);
  366. if (unlikely(fd == -1)) {
  367. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  368. return false;
  369. }
  370. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  371. icarus_write(fd, ob_bin, sizeof(ob_bin));
  372. gettimeofday(&tv_start, NULL);
  373. memset(nonce_bin, 0, sizeof(nonce_bin));
  374. icarus_gets(nonce_bin, fd, &tv_finish, -1, 1);
  375. icarus_close(fd);
  376. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  377. if (nonce_hex) {
  378. if (strncmp(nonce_hex, golden_nonce, 8)) {
  379. applog(LOG_ERR,
  380. "Icarus Detect: "
  381. "Test failed at %s: get %s, should: %s",
  382. devpath, nonce_hex, golden_nonce);
  383. free(nonce_hex);
  384. return false;
  385. }
  386. applog(LOG_DEBUG,
  387. "Icarus Detect: "
  388. "Test succeeded at %s: got %s",
  389. devpath, nonce_hex);
  390. free(nonce_hex);
  391. } else
  392. return false;
  393. /* We have a real Icarus! */
  394. struct cgpu_info *icarus;
  395. icarus = calloc(1, sizeof(struct cgpu_info));
  396. icarus->api = &icarus_api;
  397. icarus->device_path = strdup(devpath);
  398. icarus->threads = 1;
  399. add_cgpu(icarus);
  400. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  401. devpath, icarus->device_id);
  402. if (icarus_info[icarus->device_id] == NULL) {
  403. icarus_info[icarus->device_id] = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  404. if (unlikely(!(icarus_info[icarus->device_id])))
  405. quit(1, "Failed to malloc ICARUS_INFO");
  406. }
  407. info = icarus_info[icarus->device_id];
  408. // Initialise everything to zero for a new device
  409. memset(info, 0, sizeof(struct ICARUS_INFO));
  410. info->golden_hashes = (golden_nonce_val & 0x7fffffff) << 1;
  411. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  412. set_timing_mode(icarus);
  413. return true;
  414. }
  415. static void icarus_detect()
  416. {
  417. struct string_elist *iter, *tmp;
  418. const char*s;
  419. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  420. s = iter->string;
  421. if (!strncmp("icarus:", iter->string, 7))
  422. s += 7;
  423. if (!strcmp(s, "auto") || !strcmp(s, "noauto"))
  424. continue;
  425. if (icarus_detect_one(s))
  426. string_elist_del(iter);
  427. }
  428. }
  429. static bool icarus_prepare(struct thr_info *thr)
  430. {
  431. struct cgpu_info *icarus = thr->cgpu;
  432. struct timeval now;
  433. int fd = icarus_open(icarus->device_path);
  434. if (unlikely(-1 == fd)) {
  435. applog(LOG_ERR, "Failed to open Icarus on %s",
  436. icarus->device_path);
  437. return false;
  438. }
  439. icarus->device_fd = fd;
  440. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  441. gettimeofday(&now, NULL);
  442. get_datestamp(icarus->init, &now);
  443. return true;
  444. }
  445. static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  446. __maybe_unused uint64_t max_nonce)
  447. {
  448. const int thr_id = thr->id;
  449. struct cgpu_info *icarus;
  450. int fd;
  451. int ret;
  452. struct ICARUS_INFO *info;
  453. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  454. char *ob_hex;
  455. uint32_t nonce;
  456. uint64_t hash_count;
  457. struct timeval tv_start, tv_finish, elapsed;
  458. struct timeval tv_history_start, tv_history_finish;
  459. double Ti, Xi;
  460. int i;
  461. struct ICARUS_HISTORY *history0, *history;
  462. int count;
  463. double Hs, W, fullnonce;
  464. int read_count;
  465. uint64_t estimate_hashes;
  466. uint32_t values;
  467. uint64_t hash_count_range;
  468. elapsed.tv_sec = elapsed.tv_usec = 0;
  469. icarus = thr->cgpu;
  470. fd = icarus->device_fd;
  471. memset(ob_bin, 0, sizeof(ob_bin));
  472. memcpy(ob_bin, work->midstate, 32);
  473. memcpy(ob_bin + 52, work->data + 64, 12);
  474. rev(ob_bin, 32);
  475. rev(ob_bin + 52, 12);
  476. #ifndef WIN32
  477. tcflush(fd, TCOFLUSH);
  478. #endif
  479. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  480. if (ret)
  481. return 0; /* This should never happen */
  482. gettimeofday(&tv_start, NULL);
  483. if (opt_debug) {
  484. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  485. if (ob_hex) {
  486. applog(LOG_DEBUG, "Icarus %d sent: %s",
  487. icarus->device_id, ob_hex);
  488. free(ob_hex);
  489. }
  490. }
  491. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  492. memset(nonce_bin, 0, sizeof(nonce_bin));
  493. info = icarus_info[icarus->device_id];
  494. ret = icarus_gets(nonce_bin, fd, &tv_finish, thr_id, info->read_count);
  495. work->blk.nonce = 0xffffffff;
  496. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  497. // aborted before becoming idle, get new work
  498. if (nonce == 0 && ret) {
  499. timersub(&tv_finish, &tv_start, &elapsed);
  500. // ONLY up to just when it aborted
  501. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  502. estimate_hashes = ((double)(elapsed.tv_sec)
  503. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  504. // If some Serial-USB delay allowed the full nonce range to
  505. // complete it can't have done more than a full nonce
  506. if (unlikely(estimate_hashes > 0xffffffff))
  507. estimate_hashes = 0xffffffff;
  508. if (opt_debug) {
  509. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08llx hashes (%ld.%06lds)",
  510. icarus->device_id, estimate_hashes,
  511. elapsed.tv_sec, elapsed.tv_usec);
  512. }
  513. return estimate_hashes;
  514. }
  515. #if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
  516. nonce = swab32(nonce);
  517. #endif
  518. submit_nonce(thr, work, nonce);
  519. hash_count = (nonce & 0x7fffffff);
  520. if (hash_count++ == 0x7fffffff)
  521. hash_count = 0xffffffff;
  522. else
  523. hash_count <<= 1;
  524. if (opt_debug || info->do_icarus_timing)
  525. timersub(&tv_finish, &tv_start, &elapsed);
  526. if (opt_debug) {
  527. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08llx hashes (%ld.%06lds)",
  528. icarus->device_id, nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  529. }
  530. // ignore possible end condition values
  531. if (info->do_icarus_timing && (nonce & 0x7fffffff) > 0x000fffff && (nonce & 0x7fffffff) < 0x7ff00000) {
  532. gettimeofday(&tv_history_start, NULL);
  533. history0 = &(info->history[0]);
  534. if (history0->values == 0)
  535. timeradd(&tv_start, &history_sec, &(history0->finish));
  536. Ti = (double)(elapsed.tv_sec)
  537. + ((double)(elapsed.tv_usec))/((double)1000000)
  538. - ICARUS_READ_TIME;
  539. Xi = (double)hash_count;
  540. history0->sumXiTi += Xi * Ti;
  541. history0->sumXi += Xi;
  542. history0->sumTi += Ti;
  543. history0->sumXi2 += Xi * Xi;
  544. history0->values++;
  545. if (history0->hash_count_max < hash_count)
  546. history0->hash_count_max = hash_count;
  547. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  548. history0->hash_count_min = hash_count;
  549. if (history0->values >= info->min_data_count
  550. && timercmp(&tv_start, &(history0->finish), >)) {
  551. for (i = INFO_HISTORY; i > 0; i--)
  552. memcpy(&(info->history[i]),
  553. &(info->history[i-1]),
  554. sizeof(struct ICARUS_HISTORY));
  555. // Initialise history0 to zero for summary calculation
  556. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  557. // We just completed a history data set
  558. // So now recalc read_count based on the whole history thus we will
  559. // initially get more accurate until it completes INFO_HISTORY
  560. // total data sets
  561. count = 0;
  562. for (i = 1 ; i <= INFO_HISTORY; i++) {
  563. history = &(info->history[i]);
  564. if (history->values >= MIN_DATA_COUNT) {
  565. count++;
  566. history0->sumXiTi += history->sumXiTi;
  567. history0->sumXi += history->sumXi;
  568. history0->sumTi += history->sumTi;
  569. history0->sumXi2 += history->sumXi2;
  570. history0->values += history->values;
  571. if (history0->hash_count_max < history->hash_count_max)
  572. history0->hash_count_max = history->hash_count_max;
  573. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  574. history0->hash_count_min = history->hash_count_min;
  575. }
  576. }
  577. // All history data
  578. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  579. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  580. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  581. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  582. values = history0->values;
  583. // Initialise history0 to zero for next data set
  584. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  585. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  586. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  587. info->Hs = Hs;
  588. info->read_count = read_count;
  589. info->fullnonce = fullnonce;
  590. info->count = count;
  591. info->W = W;
  592. info->values = values;
  593. info->hash_count_range = hash_count_range;
  594. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  595. info->min_data_count *= 2;
  596. else if (info->timing_mode == MODE_SHORT)
  597. info->do_icarus_timing = false;
  598. // 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);
  599. applog(LOG_WARNING, "Icarus %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  600. icarus->device_id, Hs, W, read_count, fullnonce);
  601. }
  602. info->history_count++;
  603. gettimeofday(&tv_history_finish, NULL);
  604. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  605. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  606. }
  607. return hash_count;
  608. }
  609. static void icarus_api_stats(char *buf, struct cgpu_info *cgpu, bool isjson)
  610. {
  611. struct ICARUS_INFO *info = icarus_info[cgpu->device_id];
  612. // Warning, access to these is not locked - but we don't really
  613. // care since hashing performance is way more important than
  614. // locking access to displaying API debug 'stats'
  615. sprintf(buf, isjson
  616. ? "\"read_count\":%d,\"fullnonce\":%f,\"count\":%d,\"Hs\":%.15f,\"W\":%f,\"total_values\":%u,\"range\":%ld,\"history_count\":%lu,\"history_time\":%f,\"min_data_count\":%u,\"timing_values\":%u"
  617. : "read_count=%d,fullnonce=%f,count=%d,Hs=%.15f,W=%f,total_values=%u,range=%ld,history_count=%lu,history_time=%f,min_data_count=%u,timing_values=%u",
  618. info->read_count, info->fullnonce,
  619. info->count, info->Hs, info->W,
  620. info->values, info->hash_count_range,
  621. info->history_count,
  622. (double)(info->history_time.tv_sec)
  623. + ((double)(info->history_time.tv_usec))/((double)1000000),
  624. info->min_data_count, info->history[0].values);
  625. }
  626. static void icarus_shutdown(struct thr_info *thr)
  627. {
  628. struct cgpu_info *icarus = thr->cgpu;
  629. icarus_close(icarus->device_fd);
  630. }
  631. struct device_api icarus_api = {
  632. .dname = "icarus",
  633. .name = "ICA",
  634. .api_detect = icarus_detect,
  635. .get_api_stats = icarus_api_stats,
  636. .thread_prepare = icarus_prepare,
  637. .scanhash = icarus_scanhash,
  638. .thread_shutdown = icarus_shutdown,
  639. };