driver-icarus.c 24 KB

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