driver-icarus.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
  4. * Copyright 2012 Andrew Smith
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. /*
  12. * Those code should be works fine with V2 and V3 bitstream of Icarus.
  13. * Operation:
  14. * No detection implement.
  15. * Input: 64B = 32B midstate + 20B fill bytes + last 12 bytes of block head.
  16. * Return: send back 32bits immediately when Icarus found a valid nonce.
  17. * no query protocol implemented here, if no data send back in ~11.3
  18. * seconds (full cover time on 32bit nonce range by 380MH/s speed)
  19. * just send another work.
  20. * Notice:
  21. * 1. Icarus will start calculate when you push a work to them, even they
  22. * are busy.
  23. * 2. The 2 FPGAs on Icarus will distribute the job, one will calculate the
  24. * 0 ~ 7FFFFFFF, another one will cover the 80000000 ~ FFFFFFFF.
  25. * 3. It's possible for 2 FPGAs both find valid nonce in the meantime, the 2
  26. * valid nonce will all be send back.
  27. * 4. Icarus will stop work when: a valid nonce has been found or 32 bits
  28. * nonce range is completely calculated.
  29. */
  30. #include "config.h"
  31. #include <limits.h>
  32. #include <pthread.h>
  33. #include <stdio.h>
  34. #include <sys/time.h>
  35. #include <sys/types.h>
  36. #include <dirent.h>
  37. #include <unistd.h>
  38. #ifndef WIN32
  39. #include <termios.h>
  40. #include <sys/stat.h>
  41. #include <fcntl.h>
  42. #ifndef O_CLOEXEC
  43. #define O_CLOEXEC 0
  44. #endif
  45. #else
  46. #include <windows.h>
  47. #include <io.h>
  48. #endif
  49. #ifdef HAVE_SYS_EPOLL_H
  50. #include <sys/epoll.h>
  51. #define HAVE_EPOLL
  52. #endif
  53. #include "elist.h"
  54. #include "miner.h"
  55. // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
  56. #define ICARUS_IO_SPEED 115200
  57. // The size of a successful nonce read
  58. #define ICARUS_READ_SIZE 4
  59. // A stupid constant that must be 10. Don't change it.
  60. #define TIME_FACTOR 10
  61. // Ensure the sizes are correct for the Serial read
  62. #if (ICARUS_READ_SIZE != 4)
  63. #error ICARUS_READ_SIZE must be 4
  64. #endif
  65. #if (TIME_FACTOR != 10)
  66. #error TIME_FACTOR must be 10
  67. #endif
  68. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  69. ASSERT1(sizeof(uint32_t) == 4);
  70. #define ICARUS_READ_TIME ((double)ICARUS_READ_SIZE * (double)8.0 / (double)ICARUS_IO_SPEED)
  71. // Minimum precision of longpolls, in deciseconds
  72. #define ICARUS_READ_FAULT_DECISECONDS (1)
  73. // In timing mode: Default starting value until an estimate can be obtained
  74. // 5 seconds allows for up to a ~840MH/s device
  75. #define ICARUS_READ_FAULT_COUNT_DEFAULT (50)
  76. // For a standard Icarus REV3
  77. #define ICARUS_REV3_HASH_TIME 0.00000000264083
  78. #define NANOSEC 1000000000.0
  79. // Icarus Rev3 doesn't send a completion message when it finishes
  80. // the full nonce range, so to avoid being idle we must abort the
  81. // work (by starting a new work) shortly before it finishes
  82. //
  83. // Thus we need to estimate 2 things:
  84. // 1) How many hashes were done if the work was aborted
  85. // 2) How high can the timeout be before the Icarus is idle,
  86. // to minimise the number of work started
  87. // We set 2) to 'the calculated estimate' - 1
  88. // to ensure the estimate ends before idle
  89. //
  90. // The simple calculation used is:
  91. // Tn = Total time in seconds to calculate n hashes
  92. // Hs = seconds per hash
  93. // Xn = number of hashes
  94. // W = code overhead per work
  95. //
  96. // Rough but reasonable estimate:
  97. // Tn = Hs * Xn + W (of the form y = mx + b)
  98. //
  99. // Thus:
  100. // Line of best fit (using least squares)
  101. //
  102. // Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2)
  103. // W = Sum(Ti)/n - (Hs*Sum(Xi))/n
  104. //
  105. // N.B. W is less when aborting work since we aren't waiting for the reply
  106. // to be transferred back (ICARUS_READ_TIME)
  107. // Calculating the hashes aborted at n seconds is thus just n/Hs
  108. // (though this is still a slight overestimate due to code delays)
  109. //
  110. // Both below must be exceeded to complete a set of data
  111. // Minimum how long after the first, the last data point must be
  112. #define HISTORY_SEC 60
  113. // Minimum how many points a single ICARUS_HISTORY should have
  114. #define MIN_DATA_COUNT 5
  115. // The value above used is doubled each history until it exceeds:
  116. #define MAX_MIN_DATA_COUNT 100
  117. static struct timeval history_sec = { HISTORY_SEC, 0 };
  118. // Store the last INFO_HISTORY data sets
  119. // [0] = current data, not yet ready to be included as an estimate
  120. // Each new data set throws the last old set off the end thus
  121. // keeping a ongoing average of recent data
  122. #define INFO_HISTORY 10
  123. struct ICARUS_HISTORY {
  124. struct timeval finish;
  125. double sumXiTi;
  126. double sumXi;
  127. double sumTi;
  128. double sumXi2;
  129. uint32_t values;
  130. uint32_t hash_count_min;
  131. uint32_t hash_count_max;
  132. };
  133. enum timing_mode { MODE_DEFAULT, MODE_SHORT, MODE_LONG, MODE_VALUE };
  134. static const char *MODE_DEFAULT_STR = "default";
  135. static const char *MODE_SHORT_STR = "short";
  136. static const char *MODE_LONG_STR = "long";
  137. static const char *MODE_VALUE_STR = "value";
  138. static const char *MODE_UNKNOWN_STR = "unknown";
  139. struct ICARUS_INFO {
  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. };
  157. // One for each possible device
  158. static struct ICARUS_INFO *icarus_info[MAX_DEVICES];
  159. struct device_api icarus_api;
  160. static void rev(unsigned char *s, size_t l)
  161. {
  162. size_t i, j;
  163. unsigned char t;
  164. for (i = 0, j = l - 1; i < j; i++, j--) {
  165. t = s[i];
  166. s[i] = s[j];
  167. s[j] = t;
  168. }
  169. }
  170. static int icarus_open2(const char *devpath, __maybe_unused bool purge)
  171. {
  172. #ifndef WIN32
  173. struct termios my_termios;
  174. int serialfd = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  175. if (serialfd == -1)
  176. return -1;
  177. tcgetattr(serialfd, &my_termios);
  178. my_termios.c_cflag = B115200;
  179. my_termios.c_cflag |= CS8;
  180. my_termios.c_cflag |= CREAD;
  181. my_termios.c_cflag |= CLOCAL;
  182. my_termios.c_cflag &= ~(CSIZE | PARENB);
  183. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  184. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  185. my_termios.c_oflag &= ~OPOST;
  186. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  187. my_termios.c_cc[VTIME] = ICARUS_READ_FAULT_DECISECONDS;
  188. my_termios.c_cc[VMIN] = 0;
  189. tcsetattr(serialfd, TCSANOW, &my_termios);
  190. tcflush(serialfd, TCOFLUSH);
  191. tcflush(serialfd, TCIFLUSH);
  192. return serialfd;
  193. #else
  194. COMMCONFIG comCfg;
  195. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0,
  196. NULL, OPEN_EXISTING, 0, NULL);
  197. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  198. return -1;
  199. // thanks to af_newbie for pointers about this
  200. memset(&comCfg, 0 , sizeof(comCfg));
  201. comCfg.dwSize = sizeof(COMMCONFIG);
  202. comCfg.wVersion = 1;
  203. comCfg.dcb.DCBlength = sizeof(DCB);
  204. comCfg.dcb.BaudRate = ICARUS_IO_SPEED;
  205. comCfg.dcb.fBinary = 1;
  206. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  207. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  208. comCfg.dcb.ByteSize = 8;
  209. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  210. const DWORD ctoms = ICARUS_READ_FAULT_DECISECONDS * 100;
  211. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  212. SetCommTimeouts(hSerial, &cto);
  213. if (purge) {
  214. PurgeComm(hSerial, PURGE_RXABORT);
  215. PurgeComm(hSerial, PURGE_TXABORT);
  216. PurgeComm(hSerial, PURGE_RXCLEAR);
  217. PurgeComm(hSerial, PURGE_TXCLEAR);
  218. }
  219. return _open_osfhandle((LONG)hSerial, 0);
  220. #endif
  221. }
  222. #define icarus_open(devpath) icarus_open2(devpath, false)
  223. static int icarus_gets(unsigned char *buf, int fd, struct timeval *tv_finish, struct thr_info*thr, int read_count)
  224. {
  225. ssize_t ret = 0;
  226. int rc = 0;
  227. int epollfd = -1;
  228. int read_amount = ICARUS_READ_SIZE;
  229. bool first = true;
  230. #ifdef HAVE_EPOLL
  231. struct epoll_event ev;
  232. struct epoll_event evr[2];
  233. int epoll_timeout = ICARUS_READ_FAULT_DECISECONDS * 100;
  234. epollfd = epoll_create(2);
  235. if (epollfd != -1) {
  236. ev.events = EPOLLIN;
  237. ev.data.fd = fd;
  238. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
  239. close(epollfd);
  240. epollfd = -1;
  241. }
  242. if (thr->work_restart_fd != -1)
  243. {
  244. ev.data.fd = thr->work_restart_fd;
  245. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, thr->work_restart_fd, &ev))
  246. applog(LOG_ERR, "Icarus: Error adding work restart fd to epoll");
  247. else
  248. {
  249. epoll_timeout *= read_count;
  250. read_count = 1;
  251. }
  252. }
  253. }
  254. else
  255. applog(LOG_ERR, "Icarus: Error creating epoll");
  256. #endif
  257. // Read reply 1 byte at a time to get earliest tv_finish
  258. while (true) {
  259. #ifdef HAVE_EPOLL
  260. if (epollfd != -1 && (ret = epoll_wait(epollfd, evr, 2, epoll_timeout)) != -1)
  261. {
  262. if (ret == 1 && evr[0].data.fd == fd)
  263. ret = read(fd, buf, 1);
  264. else
  265. {
  266. if (ret)
  267. // work restart trigger
  268. (void)read(thr->work_restart_fd, buf, read_amount);
  269. ret = 0;
  270. }
  271. }
  272. else
  273. #endif
  274. ret = read(fd, buf, 1);
  275. if (first)
  276. gettimeofday(tv_finish, NULL);
  277. if (ret >= read_amount)
  278. {
  279. if (epollfd != -1)
  280. close(epollfd);
  281. return 0;
  282. }
  283. if (ret > 0) {
  284. buf += ret;
  285. read_amount -= ret;
  286. first = false;
  287. continue;
  288. }
  289. rc++;
  290. if (rc >= read_count || thr->work_restart) {
  291. if (epollfd != -1)
  292. close(epollfd);
  293. if (opt_debug) {
  294. rc *= ICARUS_READ_FAULT_DECISECONDS;
  295. applog(LOG_DEBUG,
  296. "Icarus Read: %s %d.%d seconds",
  297. thr->work_restart ? "Work restart at" : "No data in",
  298. rc / 10, rc % 10);
  299. }
  300. return 1;
  301. }
  302. }
  303. }
  304. static int icarus_write(int fd, const void *buf, size_t bufLen)
  305. {
  306. size_t ret;
  307. ret = write(fd, buf, bufLen);
  308. if (unlikely(ret != bufLen))
  309. return 1;
  310. return 0;
  311. }
  312. #define icarus_close(fd) close(fd)
  313. static const char *timing_mode_str(enum timing_mode timing_mode)
  314. {
  315. switch(timing_mode) {
  316. case MODE_DEFAULT:
  317. return MODE_DEFAULT_STR;
  318. case MODE_SHORT:
  319. return MODE_SHORT_STR;
  320. case MODE_LONG:
  321. return MODE_LONG_STR;
  322. case MODE_VALUE:
  323. return MODE_VALUE_STR;
  324. default:
  325. return MODE_UNKNOWN_STR;
  326. }
  327. }
  328. static void set_timing_mode(struct cgpu_info *icarus)
  329. {
  330. struct ICARUS_INFO *info = icarus_info[icarus->device_id];
  331. double Hs;
  332. char buf[BUFSIZ+1];
  333. char *ptr, *comma, *eq;
  334. size_t max;
  335. int i;
  336. if (opt_icarus_timing == NULL)
  337. buf[0] = '\0';
  338. else {
  339. ptr = opt_icarus_timing;
  340. for (i = 0; i < icarus->device_id; i++) {
  341. comma = strchr(ptr, ',');
  342. if (comma == NULL)
  343. break;
  344. ptr = comma + 1;
  345. }
  346. comma = strchr(ptr, ',');
  347. if (comma == NULL)
  348. max = strlen(ptr);
  349. else
  350. max = comma - ptr;
  351. if (max > BUFSIZ)
  352. max = BUFSIZ;
  353. strncpy(buf, ptr, max);
  354. buf[max] = '\0';
  355. }
  356. info->Hs = 0;
  357. info->read_count = 0;
  358. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  359. info->Hs = ICARUS_REV3_HASH_TIME;
  360. info->read_count = ICARUS_READ_FAULT_COUNT_DEFAULT;
  361. info->timing_mode = MODE_SHORT;
  362. info->do_icarus_timing = true;
  363. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  364. info->Hs = ICARUS_REV3_HASH_TIME;
  365. info->read_count = ICARUS_READ_FAULT_COUNT_DEFAULT;
  366. info->timing_mode = MODE_LONG;
  367. info->do_icarus_timing = true;
  368. } else if ((Hs = atof(buf)) != 0) {
  369. info->Hs = Hs / NANOSEC;
  370. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  371. if ((eq = strchr(buf, '=')) != NULL)
  372. info->read_count = atoi(eq+1);
  373. if (info->read_count < 1)
  374. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  375. if (unlikely(info->read_count < 1))
  376. info->read_count = 1;
  377. info->timing_mode = MODE_VALUE;
  378. info->do_icarus_timing = false;
  379. } else {
  380. // Anything else in buf just uses DEFAULT mode
  381. info->Hs = ICARUS_REV3_HASH_TIME;
  382. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  383. if ((eq = strchr(buf, '=')) != NULL)
  384. info->read_count = atoi(eq+1);
  385. if (info->read_count < 1)
  386. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  387. info->timing_mode = MODE_DEFAULT;
  388. info->do_icarus_timing = false;
  389. }
  390. info->min_data_count = MIN_DATA_COUNT;
  391. applog(LOG_DEBUG, "Icarus: Init: %d mode=%s read_count=%d Hs=%e",
  392. icarus->device_id, timing_mode_str(info->timing_mode), info->read_count, info->Hs);
  393. }
  394. static bool icarus_detect_one(const char *devpath)
  395. {
  396. struct ICARUS_INFO *info;
  397. int fd;
  398. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  399. // N.B. golden_ob MUST take less time to calculate
  400. // than the timeout set in icarus_open()
  401. // This one takes ~0.53ms on Rev3 Icarus
  402. const char golden_ob[] =
  403. "4679ba4ec99876bf4bfe086082b40025"
  404. "4df6c356451471139a3afa71e48f544a"
  405. "00000000000000000000000000000000"
  406. "0000000087320b1a1426674f2fa722ce";
  407. const char golden_nonce[] = "000187a2";
  408. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  409. char *nonce_hex;
  410. if (total_devices == MAX_DEVICES)
  411. return false;
  412. fd = icarus_open2(devpath, true);
  413. if (unlikely(fd == -1)) {
  414. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  415. return false;
  416. }
  417. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  418. icarus_write(fd, ob_bin, sizeof(ob_bin));
  419. memset(nonce_bin, 0, sizeof(nonce_bin));
  420. struct thr_info dummy = {
  421. .work_restart_fd = -1,
  422. };
  423. struct timeval tv_finish;
  424. icarus_gets(nonce_bin, fd, &tv_finish, &dummy, 1);
  425. icarus_close(fd);
  426. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  427. if (nonce_hex) {
  428. if (strncmp(nonce_hex, golden_nonce, 8)) {
  429. applog(LOG_ERR,
  430. "Icarus Detect: "
  431. "Test failed at %s: get %s, should: %s",
  432. devpath, nonce_hex, golden_nonce);
  433. free(nonce_hex);
  434. return false;
  435. }
  436. applog(LOG_DEBUG,
  437. "Icarus Detect: "
  438. "Test succeeded at %s: got %s",
  439. devpath, nonce_hex);
  440. free(nonce_hex);
  441. } else
  442. return false;
  443. /* We have a real Icarus! */
  444. struct cgpu_info *icarus;
  445. icarus = calloc(1, sizeof(struct cgpu_info));
  446. icarus->api = &icarus_api;
  447. icarus->device_path = strdup(devpath);
  448. icarus->threads = 1;
  449. add_cgpu(icarus);
  450. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  451. devpath, icarus->device_id);
  452. if (icarus_info[icarus->device_id] == NULL) {
  453. icarus_info[icarus->device_id] = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  454. if (unlikely(!(icarus_info[icarus->device_id])))
  455. quit(1, "Failed to malloc ICARUS_INFO");
  456. }
  457. info = icarus_info[icarus->device_id];
  458. // Initialise everything to zero for a new device
  459. memset(info, 0, sizeof(struct ICARUS_INFO));
  460. set_timing_mode(icarus);
  461. return true;
  462. }
  463. static void icarus_detect()
  464. {
  465. struct string_elist *iter, *tmp;
  466. const char*s;
  467. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  468. s = iter->string;
  469. if (!strncmp("icarus:", iter->string, 7))
  470. s += 7;
  471. if (!strcmp(s, "auto") || !strcmp(s, "noauto"))
  472. continue;
  473. if (icarus_detect_one(s))
  474. string_elist_del(iter);
  475. }
  476. }
  477. struct icarus_state {
  478. bool firstrun;
  479. struct timeval tv_workstart;
  480. struct work last_work;
  481. bool changework;
  482. };
  483. static bool icarus_prepare(struct thr_info *thr)
  484. {
  485. struct cgpu_info *icarus = thr->cgpu;
  486. struct timeval now;
  487. int fd = icarus_open2(icarus->device_path, true);
  488. if (unlikely(-1 == fd)) {
  489. applog(LOG_ERR, "Failed to open Icarus on %s",
  490. icarus->device_path);
  491. return false;
  492. }
  493. icarus->device_fd = fd;
  494. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  495. gettimeofday(&now, NULL);
  496. get_datestamp(icarus->init, &now);
  497. struct icarus_state *state;
  498. thr->cgpu_data = state = calloc(1, sizeof(*state));
  499. state->firstrun = true;
  500. #ifdef HAVE_EPOLL
  501. int epollfd = epoll_create(2);
  502. if (epollfd != -1)
  503. {
  504. close(epollfd);
  505. thr->work_restart_fd = 0;
  506. }
  507. #endif
  508. return true;
  509. }
  510. static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  511. __maybe_unused uint64_t max_nonce)
  512. {
  513. struct cgpu_info *icarus;
  514. int fd;
  515. int ret, lret;
  516. struct ICARUS_INFO *info;
  517. unsigned char ob_bin[64] = {0}, nonce_bin[ICARUS_READ_SIZE] = {0};
  518. char *ob_hex;
  519. uint32_t nonce;
  520. uint64_t hash_count;
  521. struct timeval tv_start, tv_finish, elapsed;
  522. struct timeval tv_history_start, tv_history_finish;
  523. double Ti, Xi;
  524. int i;
  525. struct ICARUS_HISTORY *history0, *history;
  526. int count;
  527. double Hs, W, fullnonce;
  528. int read_count;
  529. uint64_t estimate_hashes;
  530. uint32_t values;
  531. uint64_t hash_count_range;
  532. elapsed.tv_sec = elapsed.tv_usec = 0;
  533. icarus = thr->cgpu;
  534. struct icarus_state *state = thr->cgpu_data;
  535. // Prepare the next work immediately
  536. memcpy(ob_bin, work->midstate, 32);
  537. memcpy(ob_bin + 52, work->data + 64, 12);
  538. rev(ob_bin, 32);
  539. rev(ob_bin + 52, 12);
  540. // Wait for the previous run's result
  541. fd = icarus->device_fd;
  542. if (!state->firstrun) {
  543. if (state->changework)
  544. state->changework = false;
  545. else
  546. {
  547. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  548. info = icarus_info[icarus->device_id];
  549. lret = icarus_gets(nonce_bin, fd, &tv_finish, thr, info->read_count);
  550. if (lret && thr->work_restart) {
  551. // The prepared work is invalid, and the current work is abandoned
  552. // Go back to the main loop to get the next work, and stuff
  553. // Returning to the main loop will clear work_restart, so use a flag...
  554. state->changework = true;
  555. return 1;
  556. }
  557. }
  558. tv_start = state->tv_workstart;
  559. timeval_subtract(&elapsed, &tv_finish, &tv_start);
  560. }
  561. #ifndef WIN32
  562. tcflush(fd, TCOFLUSH);
  563. #endif
  564. gettimeofday(&state->tv_workstart, NULL);
  565. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  566. if (ret) {
  567. icarus_close(fd);
  568. return 0; /* This should never happen */
  569. }
  570. if (opt_debug) {
  571. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  572. if (ob_hex) {
  573. applog(LOG_DEBUG, "Icarus %d sent: %s",
  574. icarus->device_id, ob_hex);
  575. free(ob_hex);
  576. }
  577. }
  578. // Reopen the serial port to workaround a USB-host-chipset-specific issue with the Icarus's buggy USB-UART
  579. icarus_close(fd);
  580. fd = icarus_open(icarus->device_path);
  581. if (unlikely(-1 == fd)) {
  582. applog(LOG_ERR, "Failed to reopen Icarus on %s",
  583. icarus->device_path);
  584. return 0;
  585. }
  586. icarus->device_fd = fd;
  587. work->blk.nonce = 0xffffffff;
  588. if (state->firstrun) {
  589. state->firstrun = false;
  590. memcpy(&state->last_work, work, sizeof(state->last_work));
  591. return 1;
  592. }
  593. // OK, done starting Icarus's next job... now process the last run's result!
  594. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  595. // aborted before becoming idle, get new work
  596. if (nonce == 0 && lret) {
  597. memcpy(&state->last_work, work, sizeof(state->last_work));
  598. // ONLY up to just when it aborted
  599. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  600. estimate_hashes = ((double)(elapsed.tv_sec)
  601. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  602. // If some Serial-USB delay allowed the full nonce range to
  603. // complete it can't have done more than a full nonce
  604. if (unlikely(estimate_hashes > 0xffffffff))
  605. estimate_hashes = 0xffffffff;
  606. if (opt_debug) {
  607. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08llx hashes (%ld.%06lds)",
  608. icarus->device_id, estimate_hashes,
  609. elapsed.tv_sec, elapsed.tv_usec);
  610. }
  611. return estimate_hashes;
  612. }
  613. #if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
  614. nonce = swab32(nonce);
  615. #endif
  616. submit_nonce(thr, &state->last_work, nonce);
  617. memcpy(&state->last_work, work, sizeof(state->last_work));
  618. hash_count = (nonce & 0x7fffffff);
  619. if (hash_count++ == 0x7fffffff)
  620. hash_count = 0xffffffff;
  621. else
  622. hash_count <<= 1;
  623. if (opt_debug) {
  624. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08llx hashes (%ld.%06lds)",
  625. icarus->device_id, nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  626. }
  627. // ignore possible end condition values
  628. if (info->do_icarus_timing && (nonce & 0x7fffffff) > 0x000fffff && (nonce & 0x7fffffff) < 0x7ff00000) {
  629. gettimeofday(&tv_history_start, NULL);
  630. history0 = &(info->history[0]);
  631. if (history0->values == 0)
  632. timeradd(&tv_start, &history_sec, &(history0->finish));
  633. Ti = (double)(elapsed.tv_sec)
  634. + ((double)(elapsed.tv_usec))/((double)1000000)
  635. - ICARUS_READ_TIME;
  636. Xi = (double)hash_count;
  637. history0->sumXiTi += Xi * Ti;
  638. history0->sumXi += Xi;
  639. history0->sumTi += Ti;
  640. history0->sumXi2 += Xi * Xi;
  641. history0->values++;
  642. if (history0->hash_count_max < hash_count)
  643. history0->hash_count_max = hash_count;
  644. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  645. history0->hash_count_min = hash_count;
  646. if (history0->values >= info->min_data_count
  647. && timercmp(&tv_start, &(history0->finish), >)) {
  648. for (i = INFO_HISTORY; i > 0; i--)
  649. memcpy(&(info->history[i]),
  650. &(info->history[i-1]),
  651. sizeof(struct ICARUS_HISTORY));
  652. // Initialise history0 to zero for summary calculation
  653. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  654. // We just completed a history data set
  655. // So now recalc read_count based on the whole history thus we will
  656. // initially get more accurate until it completes INFO_HISTORY
  657. // total data sets
  658. count = 0;
  659. for (i = 1 ; i <= INFO_HISTORY; i++) {
  660. history = &(info->history[i]);
  661. if (history->values >= MIN_DATA_COUNT) {
  662. count++;
  663. history0->sumXiTi += history->sumXiTi;
  664. history0->sumXi += history->sumXi;
  665. history0->sumTi += history->sumTi;
  666. history0->sumXi2 += history->sumXi2;
  667. history0->values += history->values;
  668. if (history0->hash_count_max < history->hash_count_max)
  669. history0->hash_count_max = history->hash_count_max;
  670. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  671. history0->hash_count_min = history->hash_count_min;
  672. }
  673. }
  674. // All history data
  675. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  676. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  677. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  678. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  679. values = history0->values;
  680. // Initialise history0 to zero for next data set
  681. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  682. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  683. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  684. info->Hs = Hs;
  685. info->read_count = read_count;
  686. info->fullnonce = fullnonce;
  687. info->count = count;
  688. info->W = W;
  689. info->values = values;
  690. info->hash_count_range = hash_count_range;
  691. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  692. info->min_data_count *= 2;
  693. else if (info->timing_mode == MODE_SHORT)
  694. info->do_icarus_timing = false;
  695. // 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);
  696. applog(LOG_WARNING, "Icarus %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  697. icarus->device_id, Hs, W, read_count, fullnonce);
  698. }
  699. info->history_count++;
  700. gettimeofday(&tv_history_finish, NULL);
  701. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  702. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  703. }
  704. return hash_count;
  705. }
  706. static json_t*
  707. icarus_perf_stats(struct cgpu_info *cgpu)
  708. {
  709. struct ICARUS_INFO *info = icarus_info[cgpu->device_id];
  710. json_t *ji = json_object();
  711. // Warning, access to these is not locked - but we don't really
  712. // care since hashing performance is way more important than
  713. // locking access to displaying API debug 'stats'
  714. json_object_set(ji, "read_count" , json_integer(info->read_count ));
  715. json_object_set(ji, "fullnonce" , json_real (info->fullnonce ));
  716. json_object_set(ji, "count" , json_integer(info->count ));
  717. json_object_set(ji, "Hs" , json_real (info->Hs ));
  718. json_object_set(ji, "W" , json_real (info->W ));
  719. json_object_set(ji, "total_values" , json_integer(info->values ));
  720. json_object_set(ji, "range" , json_integer(info->hash_count_range));
  721. json_object_set(ji, "history_count" , json_integer(info->history_count ));
  722. json_object_set(ji, "history_time" , json_real (
  723. (double)(info->history_time.tv_sec)
  724. + ((double)(info->history_time.tv_usec))/((double)1000000)
  725. ));
  726. json_object_set(ji, "min_data_count", json_integer(info->min_data_count));
  727. json_object_set(ji, "timing_values" , json_integer(info->history[0].values));
  728. return ji;
  729. }
  730. static void icarus_shutdown(struct thr_info *thr)
  731. {
  732. struct cgpu_info *icarus = thr->cgpu;
  733. icarus_close(icarus->device_fd);
  734. free(thr->cgpu_data);
  735. }
  736. struct device_api icarus_api = {
  737. .dname = "icarus",
  738. .name = "PGA",
  739. .api_detect = icarus_detect,
  740. .get_extra_device_perf_stats = icarus_perf_stats,
  741. .thread_prepare = icarus_prepare,
  742. .scanhash = icarus_scanhash,
  743. .thread_shutdown = icarus_shutdown,
  744. };