driver-icarus.c 24 KB

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