driver-icarus.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. * Copyright 2012 Xiangfu
  4. * Copyright 2014 Nate Woolls
  5. * Copyright 2012 Andrew Smith
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. /*
  13. * Those code should be works fine with V2 and V3 bitstream of Icarus.
  14. * Operation:
  15. * No detection implement.
  16. * Input: 64B = 32B midstate + 20B fill bytes + last 12 bytes of block head.
  17. * Return: send back 32bits immediately when Icarus found a valid nonce.
  18. * no query protocol implemented here, if no data send back in ~11.3
  19. * seconds (full cover time on 32bit nonce range by 380MH/s speed)
  20. * just send another work.
  21. * Notice:
  22. * 1. Icarus will start calculate when you push a work to them, even they
  23. * are busy.
  24. * 2. The 2 FPGAs on Icarus will distribute the job, one will calculate the
  25. * 0 ~ 7FFFFFFF, another one will cover the 80000000 ~ FFFFFFFF.
  26. * 3. It's possible for 2 FPGAs both find valid nonce in the meantime, the 2
  27. * valid nonce will all be send back.
  28. * 4. Icarus will stop work when: a valid nonce has been found or 32 bits
  29. * nonce range is completely calculated.
  30. */
  31. #include "config.h"
  32. #include "miner.h"
  33. #include <limits.h>
  34. #include <pthread.h>
  35. #include <stdbool.h>
  36. #include <stdint.h>
  37. #include <stdio.h>
  38. #include <sys/time.h>
  39. #include <sys/types.h>
  40. #include <dirent.h>
  41. #include <unistd.h>
  42. #ifndef WIN32
  43. #include <termios.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #ifndef O_CLOEXEC
  47. #define O_CLOEXEC 0
  48. #endif
  49. #else
  50. #include <windows.h>
  51. #include <io.h>
  52. #endif
  53. #ifdef HAVE_SYS_EPOLL_H
  54. #include <sys/epoll.h>
  55. #define HAVE_EPOLL
  56. #endif
  57. #include "compat.h"
  58. #include "dynclock.h"
  59. #include "icarus-common.h"
  60. #include "lowl-vcom.h"
  61. // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
  62. #define ICARUS_IO_SPEED 115200
  63. // The number of bytes in a nonce (always 4)
  64. // This is NOT the read-size for the Icarus driver
  65. // That is defined in ICARUS_INFO->read_size
  66. #define ICARUS_NONCE_SIZE 4
  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(baud, read_size) ((double)read_size * (double)8.0 / (double)(baud))
  70. // Defined in deciseconds
  71. // There's no need to have this bigger, since the overhead/latency of extra work
  72. // is pretty small once you get beyond a 10s nonce range time and 10s also
  73. // means that nothing slower than 429MH/s can go idle so most icarus devices
  74. // will always mine without idling
  75. #define ICARUS_READ_COUNT_LIMIT_MAX 100
  76. // In timing mode: Default starting value until an estimate can be obtained
  77. // 5 seconds allows for up to a ~840MH/s device
  78. #define ICARUS_READ_COUNT_TIMING (5 * TIME_FACTOR)
  79. // For a standard Icarus REV3
  80. #define ICARUS_REV3_HASH_TIME 0.00000000264083
  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. #if (TIME_FACTOR != 10)
  120. #error TIME_FACTOR must be 10
  121. #endif
  122. static struct timeval history_sec = { HISTORY_SEC, 0 };
  123. static const char *MODE_DEFAULT_STR = "default";
  124. static const char *MODE_SHORT_STR = "short";
  125. static const char *MODE_SHORT_STREQ = "short=";
  126. static const char *MODE_LONG_STR = "long";
  127. static const char *MODE_LONG_STREQ = "long=";
  128. static const char *MODE_VALUE_STR = "value";
  129. static const char *MODE_UNKNOWN_STR = "unknown";
  130. #define END_CONDITION 0x0000ffff
  131. #define DEFAULT_DETECT_THRESHOLD 1
  132. // Looking for options in --icarus-timing and --icarus-options:
  133. //
  134. // Code increments this each time we start to look at a device
  135. // However, this means that if other devices are checked by
  136. // the Icarus code (e.g. BFL) they will count in the option offset
  137. //
  138. // This, however, is deterministic so that's OK
  139. //
  140. // If we were to increment after successfully finding an Icarus
  141. // that would be random since an Icarus may fail and thus we'd
  142. // not be able to predict the option order
  143. //
  144. // This also assumes that serial_detect() checks them sequentially
  145. // and in the order specified on the command line
  146. //
  147. static int option_offset = -1;
  148. BFG_REGISTER_DRIVER(icarus_drv)
  149. extern void convert_icarus_to_cairnsmore(struct cgpu_info *);
  150. static void rev(unsigned char *s, size_t l)
  151. {
  152. size_t i, j;
  153. unsigned char t;
  154. for (i = 0, j = l - 1; i < j; i++, j--) {
  155. t = s[i];
  156. s[i] = s[j];
  157. s[j] = t;
  158. }
  159. }
  160. #define icarus_open2(devpath, baud, purge) serial_open(devpath, baud, ICARUS_READ_FAULT_DECISECONDS, purge)
  161. #define icarus_open(devpath, baud) icarus_open2(devpath, baud, false)
  162. int icarus_gets(unsigned char *buf, int fd, struct timeval *tv_finish, struct thr_info *thr, int read_count, int read_size)
  163. {
  164. ssize_t ret = 0;
  165. int rc = 0;
  166. int epollfd = -1;
  167. int epoll_timeout = ICARUS_READ_FAULT_DECISECONDS * 100;
  168. int read_amount = read_size;
  169. bool first = true;
  170. #ifdef HAVE_EPOLL
  171. struct epoll_event ev = {
  172. .events = EPOLLIN,
  173. .data.fd = fd,
  174. };
  175. struct epoll_event evr[2];
  176. if (thr && thr->work_restart_notifier[1] != -1) {
  177. epollfd = epoll_create(2);
  178. if (epollfd != -1) {
  179. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
  180. close(epollfd);
  181. epollfd = -1;
  182. }
  183. {
  184. ev.data.fd = thr->work_restart_notifier[0];
  185. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, thr->work_restart_notifier[0], &ev))
  186. applog(LOG_ERR, "Icarus: Error adding work restart fd to epoll");
  187. else
  188. {
  189. epoll_timeout *= read_count;
  190. read_count = 1;
  191. }
  192. }
  193. }
  194. else
  195. applog(LOG_ERR, "Icarus: Error creating epoll");
  196. }
  197. #endif
  198. // Read reply 1 byte at a time to get earliest tv_finish
  199. while (true) {
  200. #ifdef HAVE_EPOLL
  201. if (epollfd != -1 && (ret = epoll_wait(epollfd, evr, 2, epoll_timeout)) != -1)
  202. {
  203. if (ret == 1 && evr[0].data.fd == fd)
  204. ret = read(fd, buf, 1);
  205. else
  206. {
  207. if (ret)
  208. notifier_read(thr->work_restart_notifier);
  209. ret = 0;
  210. }
  211. }
  212. else
  213. #endif
  214. ret = read(fd, buf, 1);
  215. if (ret < 0)
  216. return ICA_GETS_ERROR;
  217. if (first)
  218. cgtime(tv_finish);
  219. if (ret >= read_amount)
  220. {
  221. if (epollfd != -1)
  222. close(epollfd);
  223. return ICA_GETS_OK;
  224. }
  225. if (ret > 0) {
  226. buf += ret;
  227. read_amount -= ret;
  228. first = false;
  229. continue;
  230. }
  231. if (thr && thr->work_restart) {
  232. if (epollfd != -1)
  233. close(epollfd);
  234. applog(LOG_DEBUG, "Icarus Read: Interrupted by work restart");
  235. return ICA_GETS_RESTART;
  236. }
  237. rc++;
  238. if (rc >= read_count) {
  239. if (epollfd != -1)
  240. close(epollfd);
  241. applog(LOG_DEBUG, "Icarus Read: No data in %.2f seconds",
  242. (float)rc * epoll_timeout / 1000.);
  243. return ICA_GETS_TIMEOUT;
  244. }
  245. }
  246. }
  247. int icarus_write(int fd, const void *buf, size_t bufLen)
  248. {
  249. size_t ret;
  250. if (unlikely(fd == -1))
  251. return 1;
  252. ret = write(fd, buf, bufLen);
  253. if (unlikely(ret != bufLen))
  254. return 1;
  255. return 0;
  256. }
  257. #define icarus_close(fd) serial_close(fd)
  258. static void do_icarus_close(struct thr_info *thr)
  259. {
  260. struct cgpu_info *icarus = thr->cgpu;
  261. const int fd = icarus->device_fd;
  262. if (fd == -1)
  263. return;
  264. icarus_close(fd);
  265. icarus->device_fd = -1;
  266. }
  267. static const char *timing_mode_str(enum timing_mode timing_mode)
  268. {
  269. switch(timing_mode) {
  270. case MODE_DEFAULT:
  271. return MODE_DEFAULT_STR;
  272. case MODE_SHORT:
  273. return MODE_SHORT_STR;
  274. case MODE_LONG:
  275. return MODE_LONG_STR;
  276. case MODE_VALUE:
  277. return MODE_VALUE_STR;
  278. default:
  279. return MODE_UNKNOWN_STR;
  280. }
  281. }
  282. static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
  283. {
  284. struct ICARUS_INFO *info = icarus->device_data;
  285. double Hs;
  286. char buf[BUFSIZ+1];
  287. char *ptr, *comma, *eq;
  288. size_t max;
  289. int i;
  290. if (opt_icarus_timing == NULL)
  291. buf[0] = '\0';
  292. else {
  293. ptr = opt_icarus_timing;
  294. for (i = 0; i < this_option_offset; i++) {
  295. comma = strchr(ptr, ',');
  296. if (comma == NULL)
  297. break;
  298. ptr = comma + 1;
  299. }
  300. comma = strchr(ptr, ',');
  301. if (comma == NULL)
  302. max = strlen(ptr);
  303. else
  304. max = comma - ptr;
  305. if (max > BUFSIZ)
  306. max = BUFSIZ;
  307. strncpy(buf, ptr, max);
  308. buf[max] = '\0';
  309. }
  310. info->read_count = 0;
  311. info->read_count_limit = 0; // 0 = no limit
  312. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  313. // short
  314. info->read_count = ICARUS_READ_COUNT_TIMING;
  315. info->timing_mode = MODE_SHORT;
  316. info->do_icarus_timing = true;
  317. } else if (strncasecmp(buf, MODE_SHORT_STREQ, strlen(MODE_SHORT_STREQ)) == 0) {
  318. // short=limit
  319. info->read_count = ICARUS_READ_COUNT_TIMING;
  320. info->timing_mode = MODE_SHORT;
  321. info->do_icarus_timing = true;
  322. info->read_count_limit = atoi(&buf[strlen(MODE_SHORT_STREQ)]);
  323. if (info->read_count_limit < 0)
  324. info->read_count_limit = 0;
  325. if (info->read_count_limit > ICARUS_READ_COUNT_LIMIT_MAX)
  326. info->read_count_limit = ICARUS_READ_COUNT_LIMIT_MAX;
  327. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  328. // long
  329. info->read_count = ICARUS_READ_COUNT_TIMING;
  330. info->timing_mode = MODE_LONG;
  331. info->do_icarus_timing = true;
  332. } else if (strncasecmp(buf, MODE_LONG_STREQ, strlen(MODE_LONG_STREQ)) == 0) {
  333. // long=limit
  334. info->read_count = ICARUS_READ_COUNT_TIMING;
  335. info->timing_mode = MODE_LONG;
  336. info->do_icarus_timing = true;
  337. info->read_count_limit = atoi(&buf[strlen(MODE_LONG_STREQ)]);
  338. if (info->read_count_limit < 0)
  339. info->read_count_limit = 0;
  340. if (info->read_count_limit > ICARUS_READ_COUNT_LIMIT_MAX)
  341. info->read_count_limit = ICARUS_READ_COUNT_LIMIT_MAX;
  342. } else if ((Hs = atof(buf)) != 0) {
  343. // ns[=read_count]
  344. info->Hs = Hs / NANOSEC;
  345. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  346. if ((eq = strchr(buf, '=')) != NULL)
  347. info->read_count = atoi(eq+1);
  348. if (info->read_count < 1)
  349. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  350. if (unlikely(info->read_count < 1))
  351. info->read_count = 1;
  352. info->timing_mode = MODE_VALUE;
  353. info->do_icarus_timing = false;
  354. } else {
  355. // Anything else in buf just uses DEFAULT mode
  356. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  357. if ((eq = strchr(buf, '=')) != NULL)
  358. info->read_count = atoi(eq+1);
  359. int def_read_count = ICARUS_READ_COUNT_TIMING;
  360. if (info->timing_mode == MODE_DEFAULT) {
  361. if (icarus->drv == &icarus_drv) {
  362. info->do_default_detection = 0x10;
  363. } else {
  364. def_read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  365. }
  366. info->do_icarus_timing = false;
  367. }
  368. if (info->read_count < 1)
  369. info->read_count = def_read_count;
  370. }
  371. info->min_data_count = MIN_DATA_COUNT;
  372. applog(LOG_DEBUG, "%"PRIpreprv": Init: mode=%s read_count=%d limit=%dms Hs=%e",
  373. icarus->proc_repr,
  374. timing_mode_str(info->timing_mode),
  375. info->read_count, info->read_count_limit, info->Hs);
  376. }
  377. static uint32_t mask(int work_division)
  378. {
  379. uint32_t nonce_mask = 0x7fffffff;
  380. // yes we can calculate these, but this way it's easy to see what they are
  381. switch (work_division) {
  382. case 1:
  383. nonce_mask = 0xffffffff;
  384. break;
  385. case 2:
  386. nonce_mask = 0x7fffffff;
  387. break;
  388. case 4:
  389. nonce_mask = 0x3fffffff;
  390. break;
  391. case 8:
  392. nonce_mask = 0x1fffffff;
  393. break;
  394. default:
  395. quit(1, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division);
  396. }
  397. return nonce_mask;
  398. }
  399. static void get_options(int this_option_offset, struct ICARUS_INFO *info)
  400. {
  401. int *baud = &info->baud;
  402. int *work_division = &info->work_division;
  403. int *fpga_count = &info->fpga_count;
  404. char buf[BUFSIZ+1];
  405. char *ptr, *comma, *colon, *colon2;
  406. size_t max;
  407. int i, tmp;
  408. if (opt_icarus_options == NULL)
  409. buf[0] = '\0';
  410. else {
  411. ptr = opt_icarus_options;
  412. for (i = 0; i < this_option_offset; i++) {
  413. comma = strchr(ptr, ',');
  414. if (comma == NULL)
  415. break;
  416. ptr = comma + 1;
  417. }
  418. comma = strchr(ptr, ',');
  419. if (comma == NULL)
  420. max = strlen(ptr);
  421. else
  422. max = comma - ptr;
  423. if (max > BUFSIZ)
  424. max = BUFSIZ;
  425. strncpy(buf, ptr, max);
  426. buf[max] = '\0';
  427. }
  428. if (*buf) {
  429. colon = strchr(buf, ':');
  430. if (colon)
  431. *(colon++) = '\0';
  432. if (*buf) {
  433. tmp = atoi(buf);
  434. if (!valid_baud(*baud = tmp))
  435. quit(1, "Invalid icarus-options for baud (%s)", buf);
  436. }
  437. if (colon && *colon) {
  438. colon2 = strchr(colon, ':');
  439. if (colon2)
  440. *(colon2++) = '\0';
  441. if (*colon) {
  442. info->user_set |= 1;
  443. tmp = atoi(colon);
  444. if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) {
  445. *work_division = tmp;
  446. *fpga_count = tmp; // default to the same
  447. } else {
  448. quit(1, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
  449. }
  450. }
  451. if (colon2 && *colon2) {
  452. colon = strchr(colon2, ':');
  453. if (colon)
  454. *(colon++) = '\0';
  455. if (*colon2) {
  456. info->user_set |= 2;
  457. tmp = atoi(colon2);
  458. if (tmp > 0 && tmp <= *work_division)
  459. *fpga_count = tmp;
  460. else {
  461. quit(1, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
  462. }
  463. }
  464. if (colon && *colon) {
  465. colon2 = strchr(colon, '-') ?: "";
  466. if (*colon2)
  467. *(colon2++) = '\0';
  468. if (strchr(colon, 'r'))
  469. info->quirk_reopen = 2;
  470. if (strchr(colon2, 'r'))
  471. info->quirk_reopen = 0;
  472. }
  473. }
  474. }
  475. }
  476. }
  477. // Number of bytes remaining after reading a nonce from Icarus
  478. int icarus_excess_nonce_size(int fd, struct ICARUS_INFO *info)
  479. {
  480. // How big a buffer?
  481. int excess_size = info->read_size - ICARUS_NONCE_SIZE;
  482. // Try to read one more to ensure the device doesn't return
  483. // more than we want for this driver
  484. excess_size++;
  485. unsigned char excess_bin[excess_size];
  486. // Read excess_size from Icarus
  487. struct timeval tv_now;
  488. timer_set_now(&tv_now);
  489. //icarus_gets(excess_bin, fd, &tv_now, NULL, 1, excess_size);
  490. int bytes_read = read(fd, excess_bin, excess_size);
  491. // Number of bytes that were still available
  492. return bytes_read;
  493. }
  494. bool icarus_detect_custom(const char *devpath, struct device_drv *api, struct ICARUS_INFO *info)
  495. {
  496. int this_option_offset = ++option_offset;
  497. struct timeval tv_start, tv_finish;
  498. int fd;
  499. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  500. // N.B. golden_ob MUST take less time to calculate
  501. // than the timeout set in icarus_open()
  502. // This one takes ~0.53ms on Rev3 Icarus
  503. const char golden_ob[] =
  504. "4679ba4ec99876bf4bfe086082b40025"
  505. "4df6c356451471139a3afa71e48f544a"
  506. "00000000000000000000000000000000"
  507. "0000000087320b1a1426674f2fa722ce";
  508. /* NOTE: This gets sent to basically every port specified in --scan-serial,
  509. * even ones that aren't Icarus; be sure they can all handle it, when
  510. * this is changed...
  511. * BitForce: Ignores entirely
  512. * ModMiner: Starts (useless) work, gets back to clean state
  513. */
  514. const char golden_nonce[] = "000187a2";
  515. unsigned char ob_bin[64], nonce_bin[ICARUS_NONCE_SIZE];
  516. char nonce_hex[(sizeof(nonce_bin) * 2) + 1];
  517. get_options(this_option_offset, info);
  518. int baud = info->baud;
  519. int work_division = info->work_division;
  520. int fpga_count = info->fpga_count;
  521. applog(LOG_DEBUG, "Icarus Detect: Attempting to open %s", devpath);
  522. fd = icarus_open2(devpath, baud, true);
  523. if (unlikely(fd == -1)) {
  524. applog(LOG_DEBUG, "Icarus Detect: Failed to open %s", devpath);
  525. return false;
  526. }
  527. // Set a default so that individual drivers need not specify
  528. // e.g. Cairnsmore
  529. if (info->read_size == 0)
  530. info->read_size = ICARUS_DEFAULT_READ_SIZE;
  531. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  532. icarus_write(fd, ob_bin, sizeof(ob_bin));
  533. cgtime(&tv_start);
  534. memset(nonce_bin, 0, sizeof(nonce_bin));
  535. // Do not use info->read_size here, instead read exactly ICARUS_NONCE_SIZE
  536. // We will then compare the bytes left in fd with info->read_size to determine
  537. // if this is a valid device
  538. icarus_gets(nonce_bin, fd, &tv_finish, NULL, 1, ICARUS_NONCE_SIZE);
  539. // How many bytes were left after reading the above nonce
  540. int bytes_left = icarus_excess_nonce_size(fd, info);
  541. icarus_close(fd);
  542. bin2hex(nonce_hex, nonce_bin, sizeof(nonce_bin));
  543. if (strncmp(nonce_hex, golden_nonce, 8)) {
  544. applog(LOG_DEBUG,
  545. "Icarus Detect: "
  546. "Test failed at %s: get %s, should: %s",
  547. devpath, nonce_hex, golden_nonce);
  548. return false;
  549. }
  550. if (info->read_size - ICARUS_NONCE_SIZE != bytes_left)
  551. {
  552. applog(LOG_DEBUG,
  553. "Icarus Detect: "
  554. "Test failed at %s: expected %d bytes, got %d",
  555. devpath, info->read_size, ICARUS_NONCE_SIZE + bytes_left);
  556. return false;
  557. }
  558. applog(LOG_DEBUG,
  559. "Icarus Detect: "
  560. "Test succeeded at %s: got %s",
  561. devpath, nonce_hex);
  562. if (serial_claim_v(devpath, api))
  563. return false;
  564. /* We have a real Icarus! */
  565. struct cgpu_info *icarus;
  566. icarus = calloc(1, sizeof(struct cgpu_info));
  567. icarus->drv = api;
  568. icarus->device_path = strdup(devpath);
  569. icarus->device_fd = -1;
  570. icarus->threads = 1;
  571. add_cgpu(icarus);
  572. applog(LOG_INFO, "Found %"PRIpreprv" at %s",
  573. icarus->proc_repr,
  574. devpath);
  575. applog(LOG_DEBUG, "%"PRIpreprv": Init: baud=%d work_division=%d fpga_count=%d",
  576. icarus->proc_repr,
  577. baud, work_division, fpga_count);
  578. icarus->device_data = info;
  579. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  580. set_timing_mode(this_option_offset, icarus);
  581. return true;
  582. }
  583. static bool icarus_detect_one(const char *devpath)
  584. {
  585. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  586. if (unlikely(!info))
  587. quit(1, "Failed to malloc ICARUS_INFO");
  588. // TODO: try some higher speeds with the Icarus and BFL to see
  589. // if they support them and if setting them makes any difference
  590. // N.B. B3000000 doesn't work on Icarus
  591. info->baud = ICARUS_IO_SPEED;
  592. info->quirk_reopen = 1;
  593. info->Hs = ICARUS_REV3_HASH_TIME;
  594. info->timing_mode = MODE_DEFAULT;
  595. info->read_size = ICARUS_DEFAULT_READ_SIZE;
  596. if (!icarus_detect_custom(devpath, &icarus_drv, info)) {
  597. free(info);
  598. return false;
  599. }
  600. return true;
  601. }
  602. static
  603. bool icarus_lowl_probe(const struct lowlevel_device_info * const info)
  604. {
  605. return vcom_lowl_probe_wrapper(info, icarus_detect_one);
  606. }
  607. static bool icarus_prepare(struct thr_info *thr)
  608. {
  609. struct cgpu_info *icarus = thr->cgpu;
  610. struct ICARUS_INFO *info = icarus->device_data;
  611. icarus->device_fd = -1;
  612. int fd = icarus_open2(icarus->device_path, info->baud, true);
  613. if (unlikely(-1 == fd)) {
  614. applog(LOG_ERR, "Failed to open Icarus on %s",
  615. icarus->device_path);
  616. return false;
  617. }
  618. icarus->device_fd = fd;
  619. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  620. struct icarus_state *state;
  621. thr->cgpu_data = state = calloc(1, sizeof(*state));
  622. state->firstrun = true;
  623. #ifdef HAVE_EPOLL
  624. int epollfd = epoll_create(2);
  625. if (epollfd != -1)
  626. {
  627. close(epollfd);
  628. notifier_init(thr->work_restart_notifier);
  629. }
  630. #endif
  631. icarus->status = LIFE_INIT2;
  632. return true;
  633. }
  634. static bool icarus_init(struct thr_info *thr)
  635. {
  636. struct cgpu_info *icarus = thr->cgpu;
  637. struct ICARUS_INFO *info = icarus->device_data;
  638. int fd = icarus->device_fd;
  639. if (!info->work_division)
  640. {
  641. struct timeval tv_finish;
  642. // For reading the nonce from Icarus
  643. unsigned char res_bin[info->read_size];
  644. // For storing the the 32-bit nonce
  645. uint32_t res;
  646. applog(LOG_DEBUG, "%"PRIpreprv": Work division not specified - autodetecting", icarus->proc_repr);
  647. // Special packet to probe work_division
  648. unsigned char pkt[64] =
  649. "\x2e\x4c\x8f\x91\xfd\x59\x5d\x2d\x7e\xa2\x0a\xaa\xcb\x64\xa2\xa0"
  650. "\x43\x82\x86\x02\x77\xcf\x26\xb6\xa1\xee\x04\xc5\x6a\x5b\x50\x4a"
  651. "BFGMiner Probe\0\0"
  652. "BFG\0\x64\x61\x01\x1a\xc9\x06\xa9\x51\xfb\x9b\x3c\x73";
  653. icarus_write(fd, pkt, sizeof(pkt));
  654. memset(res_bin, 0, sizeof(res_bin));
  655. if (ICA_GETS_OK == icarus_gets(res_bin, fd, &tv_finish, NULL, info->read_count, info->read_size))
  656. {
  657. memcpy(&res, res_bin, sizeof(res));
  658. res = be32toh(res);
  659. }
  660. else
  661. res = 0;
  662. switch (res) {
  663. case 0x04C0FDB4:
  664. info->work_division = 1;
  665. break;
  666. case 0x82540E46:
  667. info->work_division = 2;
  668. break;
  669. case 0x417C0F36:
  670. info->work_division = 4;
  671. break;
  672. case 0x60C994D5:
  673. info->work_division = 8;
  674. break;
  675. default:
  676. applog(LOG_ERR, "%"PRIpreprv": Work division autodetection failed (assuming 2): got %08x", icarus->proc_repr, res);
  677. info->work_division = 2;
  678. }
  679. applog(LOG_DEBUG, "%"PRIpreprv": Work division autodetection got %08x (=%d)", icarus->proc_repr, res, info->work_division);
  680. }
  681. if (!info->fpga_count)
  682. info->fpga_count = info->work_division;
  683. info->nonce_mask = mask(info->work_division);
  684. return true;
  685. }
  686. static bool icarus_reopen(struct cgpu_info *icarus, struct icarus_state *state, int *fdp)
  687. {
  688. struct ICARUS_INFO *info = icarus->device_data;
  689. // Reopen the serial port to workaround a USB-host-chipset-specific issue with the Icarus's buggy USB-UART
  690. do_icarus_close(icarus->thr[0]);
  691. *fdp = icarus->device_fd = icarus_open(icarus->device_path, info->baud);
  692. if (unlikely(-1 == *fdp)) {
  693. applog(LOG_ERR, "%"PRIpreprv": Failed to reopen on %s", icarus->proc_repr, icarus->device_path);
  694. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  695. state->firstrun = true;
  696. return false;
  697. }
  698. return true;
  699. }
  700. static
  701. bool icarus_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  702. {
  703. struct cgpu_info * const icarus = thr->cgpu;
  704. struct icarus_state * const state = thr->cgpu_data;
  705. uint8_t * const ob_bin = state->ob_bin;
  706. memcpy(ob_bin, work->midstate, 32);
  707. memcpy(ob_bin + 52, work->data + 64, 12);
  708. if (!(memcmp(&ob_bin[56], "\xff\xff\xff\xff", 4)
  709. || memcmp(&ob_bin, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 32))) {
  710. // This sequence is used on cairnsmore bitstreams for commands, NEVER send it otherwise
  711. applog(LOG_WARNING, "%"PRIpreprv": Received job attempting to send a command, corrupting it!",
  712. icarus->proc_repr);
  713. ob_bin[56] = 0;
  714. }
  715. rev(ob_bin, 32);
  716. rev(ob_bin + 52, 12);
  717. return true;
  718. }
  719. static bool icarus_job_start(struct thr_info *thr)
  720. {
  721. struct cgpu_info *icarus = thr->cgpu;
  722. struct ICARUS_INFO *info = icarus->device_data;
  723. struct icarus_state *state = thr->cgpu_data;
  724. const uint8_t * const ob_bin = state->ob_bin;
  725. int fd = icarus->device_fd;
  726. int ret;
  727. // Handle dynamic clocking for "subclass" devices
  728. // This needs to run before sending next job, since it hashes the command too
  729. if (info->dclk.freqM && likely(!state->firstrun)) {
  730. dclk_preUpdate(&info->dclk);
  731. dclk_updateFreq(&info->dclk, info->dclk_change_clock_func, thr);
  732. }
  733. cgtime(&state->tv_workstart);
  734. ret = icarus_write(fd, ob_bin, 64);
  735. if (ret) {
  736. do_icarus_close(thr);
  737. applog(LOG_ERR, "%"PRIpreprv": Comms error (werr=%d)", icarus->proc_repr, ret);
  738. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  739. return false; /* This should never happen */
  740. }
  741. if (opt_debug) {
  742. char ob_hex[129];
  743. bin2hex(ob_hex, ob_bin, 64);
  744. applog(LOG_DEBUG, "%"PRIpreprv" sent: %s",
  745. icarus->proc_repr,
  746. ob_hex);
  747. }
  748. return true;
  749. }
  750. static
  751. struct work *icarus_process_worknonce(struct icarus_state *state, uint32_t *nonce)
  752. {
  753. *nonce = be32toh(*nonce);
  754. if (test_nonce(state->last_work, *nonce, false))
  755. return state->last_work;
  756. if (likely(state->last2_work && test_nonce(state->last2_work, *nonce, false)))
  757. return state->last2_work;
  758. return NULL;
  759. }
  760. static
  761. void handle_identify(struct thr_info * const thr, int ret, const bool was_first_run)
  762. {
  763. const struct cgpu_info * const icarus = thr->cgpu;
  764. const struct ICARUS_INFO * const info = icarus->device_data;
  765. struct icarus_state * const state = thr->cgpu_data;
  766. int fd = icarus->device_fd;
  767. struct timeval tv_now;
  768. double delapsed;
  769. // For reading the nonce from Icarus
  770. unsigned char nonce_bin[info->read_size];
  771. // For storing the the 32-bit nonce
  772. uint32_t nonce;
  773. if (fd == -1)
  774. return;
  775. // If identify is requested (block erupters):
  776. // 1. Don't start the next job right away (above)
  777. // 2. Wait for the current job to complete 100%
  778. if (!was_first_run)
  779. {
  780. applog(LOG_DEBUG, "%"PRIpreprv": Identify: Waiting for current job to finish", icarus->proc_repr);
  781. while (true)
  782. {
  783. cgtime(&tv_now);
  784. delapsed = tdiff(&tv_now, &state->tv_workstart);
  785. if (delapsed + 0.1 > info->fullnonce)
  786. break;
  787. // Try to get more nonces (ignoring work restart)
  788. memset(nonce_bin, 0, sizeof(nonce_bin));
  789. ret = icarus_gets(nonce_bin, fd, &tv_now, NULL, (info->fullnonce - delapsed) * 10, info->read_size);
  790. if (ret == ICA_GETS_OK)
  791. {
  792. memcpy(&nonce, nonce_bin, sizeof(nonce));
  793. nonce = be32toh(nonce);
  794. submit_nonce(thr, state->last_work, nonce);
  795. }
  796. }
  797. }
  798. else
  799. applog(LOG_DEBUG, "%"PRIpreprv": Identify: Current job should already be finished", icarus->proc_repr);
  800. // 3. Delay 3 more seconds
  801. applog(LOG_DEBUG, "%"PRIpreprv": Identify: Leaving idle for 3 seconds", icarus->proc_repr);
  802. cgsleep_ms(3000);
  803. // Check for work restart in the meantime
  804. if (thr->work_restart)
  805. {
  806. applog(LOG_DEBUG, "%"PRIpreprv": Identify: Work restart requested during delay", icarus->proc_repr);
  807. goto no_job_start;
  808. }
  809. // 4. Start next job
  810. if (!state->firstrun)
  811. {
  812. applog(LOG_DEBUG, "%"PRIpreprv": Identify: Starting next job", icarus->proc_repr);
  813. if (!icarus_job_start(thr))
  814. no_job_start:
  815. state->firstrun = true;
  816. }
  817. state->identify = false;
  818. }
  819. static
  820. void icarus_transition_work(struct icarus_state *state, struct work *work)
  821. {
  822. if (state->last2_work)
  823. free_work(state->last2_work);
  824. state->last2_work = state->last_work;
  825. state->last_work = copy_work(work);
  826. }
  827. static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  828. __maybe_unused int64_t max_nonce)
  829. {
  830. struct cgpu_info *icarus;
  831. int fd;
  832. int ret;
  833. struct ICARUS_INFO *info;
  834. struct work *nonce_work;
  835. int64_t hash_count;
  836. struct timeval tv_start = {.tv_sec=0}, elapsed;
  837. struct timeval tv_history_start, tv_history_finish;
  838. double Ti, Xi;
  839. int i;
  840. bool was_hw_error = false;
  841. bool was_first_run;
  842. struct ICARUS_HISTORY *history0, *history;
  843. int count;
  844. double Hs, W, fullnonce;
  845. int read_count;
  846. bool limited;
  847. int64_t estimate_hashes;
  848. uint32_t values;
  849. int64_t hash_count_range;
  850. elapsed.tv_sec = elapsed.tv_usec = 0;
  851. icarus = thr->cgpu;
  852. struct icarus_state *state = thr->cgpu_data;
  853. was_first_run = state->firstrun;
  854. icarus_job_prepare(thr, work, max_nonce);
  855. // Wait for the previous run's result
  856. fd = icarus->device_fd;
  857. info = icarus->device_data;
  858. // For reading the nonce from Icarus
  859. unsigned char nonce_bin[info->read_size];
  860. // For storing the the 32-bit nonce
  861. uint32_t nonce;
  862. if (unlikely(fd == -1) && !icarus_reopen(icarus, state, &fd))
  863. return -1;
  864. if (!state->firstrun) {
  865. if (state->changework)
  866. {
  867. state->changework = false;
  868. ret = ICA_GETS_RESTART;
  869. }
  870. else
  871. {
  872. read_count = info->read_count;
  873. keepwaiting:
  874. /* Icarus will return info->read_size bytes nonces or nothing */
  875. memset(nonce_bin, 0, sizeof(nonce_bin));
  876. ret = icarus_gets(nonce_bin, fd, &state->tv_workfinish, thr, read_count, info->read_size);
  877. switch (ret) {
  878. case ICA_GETS_RESTART:
  879. // The prepared work is invalid, and the current work is abandoned
  880. // Go back to the main loop to get the next work, and stuff
  881. // Returning to the main loop will clear work_restart, so use a flag...
  882. state->changework = true;
  883. return 0;
  884. case ICA_GETS_ERROR:
  885. do_icarus_close(thr);
  886. applog(LOG_ERR, "%"PRIpreprv": Comms error (rerr)", icarus->proc_repr);
  887. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  888. if (!icarus_reopen(icarus, state, &fd))
  889. return -1;
  890. break;
  891. case ICA_GETS_TIMEOUT:
  892. if (info->quirk_reopen == 1 && !icarus_reopen(icarus, state, &fd))
  893. return -1;
  894. case ICA_GETS_OK:
  895. break;
  896. }
  897. }
  898. tv_start = state->tv_workstart;
  899. timersub(&state->tv_workfinish, &tv_start, &elapsed);
  900. }
  901. else
  902. {
  903. if (fd == -1 && !icarus_reopen(icarus, state, &fd))
  904. return -1;
  905. // First run; no nonce, no hashes done
  906. ret = ICA_GETS_ERROR;
  907. }
  908. #ifndef WIN32
  909. tcflush(fd, TCOFLUSH);
  910. #endif
  911. if (ret == ICA_GETS_OK)
  912. {
  913. memcpy(&nonce, nonce_bin, sizeof(nonce));
  914. nonce_work = icarus_process_worknonce(state, &nonce);
  915. if (likely(nonce_work))
  916. {
  917. if (nonce_work == state->last2_work)
  918. {
  919. // nonce was for the last job; submit and keep processing the current one
  920. submit_nonce(thr, nonce_work, nonce);
  921. goto keepwaiting;
  922. }
  923. if (info->continue_search)
  924. {
  925. read_count = info->read_count - ((timer_elapsed_us(&state->tv_workstart, NULL) / (1000000 / TIME_FACTOR)) + 1);
  926. if (read_count)
  927. {
  928. submit_nonce(thr, nonce_work, nonce);
  929. goto keepwaiting;
  930. }
  931. }
  932. }
  933. else
  934. was_hw_error = true;
  935. }
  936. // Handle dynamic clocking for "subclass" devices
  937. // This needs to run before sending next job, since it hashes the command too
  938. if (info->dclk.freqM && likely(ret == ICA_GETS_OK || ret == ICA_GETS_TIMEOUT)) {
  939. int qsec = ((4 * elapsed.tv_sec) + (elapsed.tv_usec / 250000)) ?: 1;
  940. for (int n = qsec; n; --n)
  941. dclk_gotNonces(&info->dclk);
  942. if (was_hw_error)
  943. dclk_errorCount(&info->dclk, qsec);
  944. }
  945. // Force a USB close/reopen on any hw error
  946. if (was_hw_error && info->quirk_reopen != 2) {
  947. if (!icarus_reopen(icarus, state, &fd))
  948. state->firstrun = true;
  949. }
  950. if (unlikely(state->identify))
  951. {
  952. // Delay job start until later...
  953. }
  954. else
  955. if (unlikely(icarus->deven != DEV_ENABLED || !icarus_job_start(thr)))
  956. state->firstrun = true;
  957. if (info->quirk_reopen == 2 && !icarus_reopen(icarus, state, &fd))
  958. state->firstrun = true;
  959. work->blk.nonce = 0xffffffff;
  960. if (ret == ICA_GETS_ERROR) {
  961. state->firstrun = false;
  962. icarus_transition_work(state, work);
  963. hash_count = 0;
  964. goto out;
  965. }
  966. // OK, done starting Icarus's next job... now process the last run's result!
  967. // aborted before becoming idle, get new work
  968. if (ret == ICA_GETS_TIMEOUT || ret == ICA_GETS_RESTART) {
  969. icarus_transition_work(state, work);
  970. // ONLY up to just when it aborted
  971. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  972. estimate_hashes = ((double)(elapsed.tv_sec)
  973. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  974. // If some Serial-USB delay allowed the full nonce range to
  975. // complete it can't have done more than a full nonce
  976. if (unlikely(estimate_hashes > 0xffffffff))
  977. estimate_hashes = 0xffffffff;
  978. applog(LOG_DEBUG, "%"PRIpreprv" no nonce = 0x%08"PRIx64" hashes (%"PRId64".%06lus)",
  979. icarus->proc_repr,
  980. (uint64_t)estimate_hashes,
  981. (int64_t)elapsed.tv_sec, (unsigned long)elapsed.tv_usec);
  982. hash_count = estimate_hashes;
  983. goto out;
  984. }
  985. // Only ICA_GETS_OK gets here
  986. if (likely(!was_hw_error))
  987. submit_nonce(thr, nonce_work, nonce);
  988. else
  989. inc_hw_errors(thr, state->last_work, nonce);
  990. icarus_transition_work(state, work);
  991. hash_count = (nonce & info->nonce_mask);
  992. hash_count++;
  993. hash_count *= info->fpga_count;
  994. applog(LOG_DEBUG, "%"PRIpreprv" nonce = 0x%08x = 0x%08" PRIx64 " hashes (%"PRId64".%06lus)",
  995. icarus->proc_repr,
  996. nonce,
  997. (uint64_t)hash_count,
  998. (int64_t)elapsed.tv_sec, (unsigned long)elapsed.tv_usec);
  999. if (info->do_default_detection && elapsed.tv_sec >= DEFAULT_DETECT_THRESHOLD) {
  1000. int MHs = (double)hash_count / ((double)elapsed.tv_sec * 1e6 + (double)elapsed.tv_usec);
  1001. --info->do_default_detection;
  1002. applog(LOG_DEBUG, "%"PRIpreprv": Autodetect device speed: %d MH/s", icarus->proc_repr, MHs);
  1003. if (MHs <= 370 || MHs > 420) {
  1004. // Not a real Icarus: enable short timing
  1005. applog(LOG_WARNING, "%"PRIpreprv": Seems too %s to be an Icarus; calibrating with short timing", icarus->proc_repr, MHs>380?"fast":"slow");
  1006. info->timing_mode = MODE_SHORT;
  1007. info->do_icarus_timing = true;
  1008. info->do_default_detection = 0;
  1009. }
  1010. else
  1011. if (MHs <= 380) {
  1012. // Real Icarus?
  1013. if (!info->do_default_detection) {
  1014. applog(LOG_DEBUG, "%"PRIpreprv": Seems to be a real Icarus", icarus->proc_repr);
  1015. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  1016. }
  1017. }
  1018. else
  1019. if (MHs <= 420) {
  1020. // Enterpoint Cairnsmore1
  1021. size_t old_repr_len = strlen(icarus->proc_repr);
  1022. char old_repr[old_repr_len + 1];
  1023. strcpy(old_repr, icarus->proc_repr);
  1024. convert_icarus_to_cairnsmore(icarus);
  1025. info->do_default_detection = 0;
  1026. applog(LOG_WARNING, "%"PRIpreprv": Detected Cairnsmore1 device, upgrading driver to %"PRIpreprv, old_repr, icarus->proc_repr);
  1027. }
  1028. }
  1029. // Ignore possible end condition values ... and hw errors
  1030. // TODO: set limitations on calculated values depending on the device
  1031. // to avoid crap values caused by CPU/Task Switching/Swapping/etc
  1032. if (info->do_icarus_timing
  1033. && !was_hw_error
  1034. && ((nonce & info->nonce_mask) > END_CONDITION)
  1035. && ((nonce & info->nonce_mask) < (info->nonce_mask & ~END_CONDITION))) {
  1036. cgtime(&tv_history_start);
  1037. history0 = &(info->history[0]);
  1038. if (history0->values == 0)
  1039. timeradd(&tv_start, &history_sec, &(history0->finish));
  1040. Ti = (double)(elapsed.tv_sec)
  1041. + ((double)(elapsed.tv_usec))/((double)1000000)
  1042. - ((double)ICARUS_READ_TIME(info->baud, info->read_size));
  1043. Xi = (double)hash_count;
  1044. history0->sumXiTi += Xi * Ti;
  1045. history0->sumXi += Xi;
  1046. history0->sumTi += Ti;
  1047. history0->sumXi2 += Xi * Xi;
  1048. history0->values++;
  1049. if (history0->hash_count_max < hash_count)
  1050. history0->hash_count_max = hash_count;
  1051. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  1052. history0->hash_count_min = hash_count;
  1053. if (history0->values >= info->min_data_count
  1054. && timercmp(&tv_start, &(history0->finish), >)) {
  1055. for (i = INFO_HISTORY; i > 0; i--)
  1056. memcpy(&(info->history[i]),
  1057. &(info->history[i-1]),
  1058. sizeof(struct ICARUS_HISTORY));
  1059. // Initialise history0 to zero for summary calculation
  1060. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  1061. // We just completed a history data set
  1062. // So now recalc read_count based on the whole history thus we will
  1063. // initially get more accurate until it completes INFO_HISTORY
  1064. // total data sets
  1065. count = 0;
  1066. for (i = 1 ; i <= INFO_HISTORY; i++) {
  1067. history = &(info->history[i]);
  1068. if (history->values >= MIN_DATA_COUNT) {
  1069. count++;
  1070. history0->sumXiTi += history->sumXiTi;
  1071. history0->sumXi += history->sumXi;
  1072. history0->sumTi += history->sumTi;
  1073. history0->sumXi2 += history->sumXi2;
  1074. history0->values += history->values;
  1075. if (history0->hash_count_max < history->hash_count_max)
  1076. history0->hash_count_max = history->hash_count_max;
  1077. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  1078. history0->hash_count_min = history->hash_count_min;
  1079. }
  1080. }
  1081. // All history data
  1082. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  1083. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  1084. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  1085. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  1086. values = history0->values;
  1087. // Initialise history0 to zero for next data set
  1088. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  1089. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  1090. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  1091. if (info->read_count_limit > 0 && read_count > info->read_count_limit) {
  1092. read_count = info->read_count_limit;
  1093. limited = true;
  1094. } else
  1095. limited = false;
  1096. info->Hs = Hs;
  1097. info->read_count = read_count;
  1098. info->fullnonce = fullnonce;
  1099. info->count = count;
  1100. info->W = W;
  1101. info->values = values;
  1102. info->hash_count_range = hash_count_range;
  1103. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  1104. info->min_data_count *= 2;
  1105. else if (info->timing_mode == MODE_SHORT)
  1106. info->do_icarus_timing = false;
  1107. // applog(LOG_DEBUG, "%"PRIpreprv" Re-estimate: read_count=%d%s fullnonce=%fs history count=%d Hs=%e W=%e values=%d hash range=0x%08lx min data count=%u", icarus->proc_repr, read_count, limited ? " (limited)" : "", fullnonce, count, Hs, W, values, hash_count_range, info->min_data_count);
  1108. applog(LOG_DEBUG, "%"PRIpreprv" Re-estimate: Hs=%e W=%e read_count=%d%s fullnonce=%.3fs",
  1109. icarus->proc_repr,
  1110. Hs, W, read_count,
  1111. limited ? " (limited)" : "", fullnonce);
  1112. }
  1113. info->history_count++;
  1114. cgtime(&tv_history_finish);
  1115. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  1116. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  1117. }
  1118. out:
  1119. if (unlikely(state->identify))
  1120. handle_identify(thr, ret, was_first_run);
  1121. return hash_count;
  1122. }
  1123. static struct api_data *icarus_drv_stats(struct cgpu_info *cgpu)
  1124. {
  1125. struct api_data *root = NULL;
  1126. struct ICARUS_INFO *info = cgpu->device_data;
  1127. // Warning, access to these is not locked - but we don't really
  1128. // care since hashing performance is way more important than
  1129. // locking access to displaying API debug 'stats'
  1130. // If locking becomes an issue for any of them, use copy_data=true also
  1131. root = api_add_int(root, "read_count", &(info->read_count), false);
  1132. root = api_add_int(root, "read_count_limit", &(info->read_count_limit), false);
  1133. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  1134. root = api_add_int(root, "count", &(info->count), false);
  1135. root = api_add_hs(root, "Hs", &(info->Hs), false);
  1136. root = api_add_double(root, "W", &(info->W), false);
  1137. root = api_add_uint(root, "total_values", &(info->values), false);
  1138. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  1139. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  1140. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  1141. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  1142. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  1143. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  1144. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  1145. root = api_add_int(root, "baud", &(info->baud), false);
  1146. root = api_add_int(root, "work_division", &(info->work_division), false);
  1147. root = api_add_int(root, "fpga_count", &(info->fpga_count), false);
  1148. return root;
  1149. }
  1150. static void icarus_shutdown(struct thr_info *thr)
  1151. {
  1152. do_icarus_close(thr);
  1153. free(thr->cgpu_data);
  1154. }
  1155. struct device_drv icarus_drv = {
  1156. .dname = "icarus",
  1157. .name = "ICA",
  1158. .probe_priority = -120,
  1159. .lowl_probe = icarus_lowl_probe,
  1160. .get_api_stats = icarus_drv_stats,
  1161. .thread_prepare = icarus_prepare,
  1162. .thread_init = icarus_init,
  1163. .scanhash = icarus_scanhash,
  1164. .thread_disable = close_device_fd,
  1165. .thread_shutdown = icarus_shutdown,
  1166. };