driver-icarus.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. * Copyright 2012 Xiangfu
  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 "miner.h"
  32. #ifdef WIN32
  33. #include <winsock2.h>
  34. #endif
  35. #include <limits.h>
  36. #include <pthread.h>
  37. #include <stdbool.h>
  38. #include <stdint.h>
  39. #include <stdio.h>
  40. #include <sys/time.h>
  41. #include <sys/types.h>
  42. #include <dirent.h>
  43. #include <unistd.h>
  44. #ifndef WIN32
  45. #include <termios.h>
  46. #include <sys/stat.h>
  47. #include <fcntl.h>
  48. #ifndef O_CLOEXEC
  49. #define O_CLOEXEC 0
  50. #endif
  51. #else
  52. #include <windows.h>
  53. #include <io.h>
  54. #endif
  55. #ifdef HAVE_SYS_EPOLL_H
  56. #include <sys/epoll.h>
  57. #define HAVE_EPOLL
  58. #endif
  59. #include "compat.h"
  60. #include "dynclock.h"
  61. #include "icarus-common.h"
  62. #include "fpgautils.h"
  63. // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
  64. #define ICARUS_IO_SPEED 115200
  65. // The size of a successful nonce read
  66. #define ICARUS_READ_SIZE 4
  67. // Ensure the sizes are correct for the Serial read
  68. #if (ICARUS_READ_SIZE != 4)
  69. #error ICARUS_READ_SIZE must be 4
  70. #endif
  71. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  72. ASSERT1(sizeof(uint32_t) == 4);
  73. #define ICARUS_READ_TIME(baud) ((double)ICARUS_READ_SIZE * (double)8.0 / (double)(baud))
  74. // In timing mode: Default starting value until an estimate can be obtained
  75. // 5 seconds allows for up to a ~840MH/s device
  76. #define ICARUS_READ_COUNT_TIMING (5 * TIME_FACTOR)
  77. // For a standard Icarus REV3
  78. #define ICARUS_REV3_HASH_TIME 0.00000000264083
  79. // Icarus Rev3 doesn't send a completion message when it finishes
  80. // the full nonce range, so to avoid being idle we must abort the
  81. // work (by starting a new work) shortly before it finishes
  82. //
  83. // Thus we need to estimate 2 things:
  84. // 1) How many hashes were done if the work was aborted
  85. // 2) How high can the timeout be before the Icarus is idle,
  86. // to minimise the number of work started
  87. // We set 2) to 'the calculated estimate' - 1
  88. // to ensure the estimate ends before idle
  89. //
  90. // The simple calculation used is:
  91. // Tn = Total time in seconds to calculate n hashes
  92. // Hs = seconds per hash
  93. // Xn = number of hashes
  94. // W = code overhead per work
  95. //
  96. // Rough but reasonable estimate:
  97. // Tn = Hs * Xn + W (of the form y = mx + b)
  98. //
  99. // Thus:
  100. // Line of best fit (using least squares)
  101. //
  102. // Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2)
  103. // W = Sum(Ti)/n - (Hs*Sum(Xi))/n
  104. //
  105. // N.B. W is less when aborting work since we aren't waiting for the reply
  106. // to be transferred back (ICARUS_READ_TIME)
  107. // Calculating the hashes aborted at n seconds is thus just n/Hs
  108. // (though this is still a slight overestimate due to code delays)
  109. //
  110. // Both below must be exceeded to complete a set of data
  111. // Minimum how long after the first, the last data point must be
  112. #define HISTORY_SEC 60
  113. // Minimum how many points a single ICARUS_HISTORY should have
  114. #define MIN_DATA_COUNT 5
  115. // The value above used is doubled each history until it exceeds:
  116. #define MAX_MIN_DATA_COUNT 100
  117. #if (TIME_FACTOR != 10)
  118. #error TIME_FACTOR must be 10
  119. #endif
  120. static struct timeval history_sec = { HISTORY_SEC, 0 };
  121. static const char *MODE_DEFAULT_STR = "default";
  122. static const char *MODE_SHORT_STR = "short";
  123. static const char *MODE_LONG_STR = "long";
  124. static const char *MODE_VALUE_STR = "value";
  125. static const char *MODE_UNKNOWN_STR = "unknown";
  126. #define END_CONDITION 0x0000ffff
  127. #define DEFAULT_DETECT_THRESHOLD 1
  128. // Looking for options in --icarus-timing and --icarus-options:
  129. //
  130. // Code increments this each time we start to look at a device
  131. // However, this means that if other devices are checked by
  132. // the Icarus code (e.g. BFL) they will count in the option offset
  133. //
  134. // This, however, is deterministic so that's OK
  135. //
  136. // If we were to increment after successfully finding an Icarus
  137. // that would be random since an Icarus may fail and thus we'd
  138. // not be able to predict the option order
  139. //
  140. // This also assumes that serial_detect() checks them sequentially
  141. // and in the order specified on the command line
  142. //
  143. static int option_offset = -1;
  144. struct device_drv icarus_drv;
  145. extern void convert_icarus_to_cairnsmore(struct cgpu_info *);
  146. static void rev(unsigned char *s, size_t l)
  147. {
  148. size_t i, j;
  149. unsigned char t;
  150. for (i = 0, j = l - 1; i < j; i++, j--) {
  151. t = s[i];
  152. s[i] = s[j];
  153. s[j] = t;
  154. }
  155. }
  156. #define icarus_open2(devpath, baud, purge) serial_open(devpath, baud, ICARUS_READ_FAULT_DECISECONDS, purge)
  157. #define icarus_open(devpath, baud) icarus_open2(devpath, baud, false)
  158. #define ICA_GETS_ERROR -1
  159. #define ICA_GETS_OK 0
  160. #define ICA_GETS_RESTART 1
  161. #define ICA_GETS_TIMEOUT 2
  162. int icarus_gets(unsigned char *buf, int fd, struct timeval *tv_finish, struct thr_info *thr, int read_count)
  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 = ICARUS_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. if (opt_debug) {
  235. applog(LOG_DEBUG,
  236. "Icarus Read: Interrupted by work restart");
  237. }
  238. return ICA_GETS_RESTART;
  239. }
  240. rc++;
  241. if (rc >= read_count) {
  242. if (epollfd != -1)
  243. close(epollfd);
  244. if (opt_debug) {
  245. applog(LOG_DEBUG,
  246. "Icarus Read: No data in %.2f seconds",
  247. (float)rc * epoll_timeout / 1000.);
  248. }
  249. return ICA_GETS_TIMEOUT;
  250. }
  251. }
  252. }
  253. static int icarus_write(int fd, const void *buf, size_t bufLen)
  254. {
  255. size_t ret;
  256. if (unlikely(fd == -1))
  257. return 1;
  258. ret = write(fd, buf, bufLen);
  259. if (unlikely(ret != bufLen))
  260. return 1;
  261. return 0;
  262. }
  263. #define icarus_close(fd) serial_close(fd)
  264. static void do_icarus_close(struct thr_info *thr)
  265. {
  266. struct cgpu_info *icarus = thr->cgpu;
  267. const int fd = icarus->device_fd;
  268. if (fd == -1)
  269. return;
  270. icarus_close(fd);
  271. icarus->device_fd = -1;
  272. }
  273. static const char *timing_mode_str(enum timing_mode timing_mode)
  274. {
  275. switch(timing_mode) {
  276. case MODE_DEFAULT:
  277. return MODE_DEFAULT_STR;
  278. case MODE_SHORT:
  279. return MODE_SHORT_STR;
  280. case MODE_LONG:
  281. return MODE_LONG_STR;
  282. case MODE_VALUE:
  283. return MODE_VALUE_STR;
  284. default:
  285. return MODE_UNKNOWN_STR;
  286. }
  287. }
  288. static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
  289. {
  290. struct ICARUS_INFO *info = icarus->device_data;
  291. double Hs;
  292. char buf[BUFSIZ+1];
  293. char *ptr, *comma, *eq;
  294. size_t max;
  295. int i;
  296. if (opt_icarus_timing == NULL)
  297. buf[0] = '\0';
  298. else {
  299. ptr = opt_icarus_timing;
  300. for (i = 0; i < this_option_offset; i++) {
  301. comma = strchr(ptr, ',');
  302. if (comma == NULL)
  303. break;
  304. ptr = comma + 1;
  305. }
  306. comma = strchr(ptr, ',');
  307. if (comma == NULL)
  308. max = strlen(ptr);
  309. else
  310. max = comma - ptr;
  311. if (max > BUFSIZ)
  312. max = BUFSIZ;
  313. strncpy(buf, ptr, max);
  314. buf[max] = '\0';
  315. }
  316. info->read_count = 0;
  317. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  318. info->read_count = ICARUS_READ_COUNT_TIMING;
  319. info->timing_mode = MODE_SHORT;
  320. info->do_icarus_timing = true;
  321. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  322. info->read_count = ICARUS_READ_COUNT_TIMING;
  323. info->timing_mode = MODE_LONG;
  324. info->do_icarus_timing = true;
  325. } else if ((Hs = atof(buf)) != 0) {
  326. info->Hs = Hs / NANOSEC;
  327. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  328. if ((eq = strchr(buf, '=')) != NULL)
  329. info->read_count = atoi(eq+1);
  330. if (info->read_count < 1)
  331. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  332. if (unlikely(info->read_count < 1))
  333. info->read_count = 1;
  334. info->timing_mode = MODE_VALUE;
  335. info->do_icarus_timing = false;
  336. } else {
  337. // Anything else in buf just uses DEFAULT mode
  338. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  339. if ((eq = strchr(buf, '=')) != NULL)
  340. info->read_count = atoi(eq+1);
  341. int def_read_count = ICARUS_READ_COUNT_TIMING;
  342. if (info->timing_mode == MODE_DEFAULT) {
  343. if (icarus->drv == &icarus_drv) {
  344. info->do_default_detection = 0x10;
  345. } else {
  346. def_read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  347. }
  348. info->do_icarus_timing = false;
  349. }
  350. if (info->read_count < 1)
  351. info->read_count = def_read_count;
  352. }
  353. info->min_data_count = MIN_DATA_COUNT;
  354. applog(LOG_DEBUG, "%"PRIpreprv": Init: mode=%s read_count=%d Hs=%e",
  355. icarus->proc_repr,
  356. timing_mode_str(info->timing_mode), info->read_count, info->Hs);
  357. }
  358. static uint32_t mask(int work_division)
  359. {
  360. uint32_t nonce_mask = 0x7fffffff;
  361. // yes we can calculate these, but this way it's easy to see what they are
  362. switch (work_division) {
  363. case 1:
  364. nonce_mask = 0xffffffff;
  365. break;
  366. case 2:
  367. nonce_mask = 0x7fffffff;
  368. break;
  369. case 4:
  370. nonce_mask = 0x3fffffff;
  371. break;
  372. case 8:
  373. nonce_mask = 0x1fffffff;
  374. break;
  375. default:
  376. quit(1, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division);
  377. }
  378. return nonce_mask;
  379. }
  380. static void get_options(int this_option_offset, struct ICARUS_INFO *info)
  381. {
  382. int *baud = &info->baud;
  383. int *work_division = &info->work_division;
  384. int *fpga_count = &info->fpga_count;
  385. char buf[BUFSIZ+1];
  386. char *ptr, *comma, *colon, *colon2;
  387. size_t max;
  388. int i, tmp;
  389. if (opt_icarus_options == NULL)
  390. buf[0] = '\0';
  391. else {
  392. ptr = opt_icarus_options;
  393. for (i = 0; i < this_option_offset; i++) {
  394. comma = strchr(ptr, ',');
  395. if (comma == NULL)
  396. break;
  397. ptr = comma + 1;
  398. }
  399. comma = strchr(ptr, ',');
  400. if (comma == NULL)
  401. max = strlen(ptr);
  402. else
  403. max = comma - ptr;
  404. if (max > BUFSIZ)
  405. max = BUFSIZ;
  406. strncpy(buf, ptr, max);
  407. buf[max] = '\0';
  408. }
  409. if (*buf) {
  410. colon = strchr(buf, ':');
  411. if (colon)
  412. *(colon++) = '\0';
  413. if (*buf) {
  414. tmp = atoi(buf);
  415. if (!valid_baud(*baud = tmp))
  416. quit(1, "Invalid icarus-options for baud (%s)", buf);
  417. }
  418. if (colon && *colon) {
  419. colon2 = strchr(colon, ':');
  420. if (colon2)
  421. *(colon2++) = '\0';
  422. if (*colon) {
  423. info->user_set |= 1;
  424. tmp = atoi(colon);
  425. if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) {
  426. *work_division = tmp;
  427. *fpga_count = tmp; // default to the same
  428. } else {
  429. quit(1, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
  430. }
  431. }
  432. if (colon2 && *colon2) {
  433. colon = strchr(colon2, ':');
  434. if (colon)
  435. *(colon++) = '\0';
  436. if (*colon2) {
  437. info->user_set |= 2;
  438. tmp = atoi(colon2);
  439. if (tmp > 0 && tmp <= *work_division)
  440. *fpga_count = tmp;
  441. else {
  442. quit(1, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
  443. }
  444. }
  445. if (colon && *colon) {
  446. colon2 = strchr(colon, '-') ?: "";
  447. if (*colon2)
  448. *(colon2++) = '\0';
  449. if (strchr(colon, 'r'))
  450. info->quirk_reopen = 2;
  451. if (strchr(colon2, 'r'))
  452. info->quirk_reopen = 0;
  453. }
  454. }
  455. }
  456. }
  457. }
  458. bool icarus_detect_custom(const char *devpath, struct device_drv *api, struct ICARUS_INFO *info)
  459. {
  460. int this_option_offset = ++option_offset;
  461. struct timeval tv_start, tv_finish;
  462. int fd;
  463. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  464. // N.B. golden_ob MUST take less time to calculate
  465. // than the timeout set in icarus_open()
  466. // This one takes ~0.53ms on Rev3 Icarus
  467. const char golden_ob[] =
  468. "4679ba4ec99876bf4bfe086082b40025"
  469. "4df6c356451471139a3afa71e48f544a"
  470. "00000000000000000000000000000000"
  471. "0000000087320b1a1426674f2fa722ce";
  472. /* NOTE: This gets sent to basically every port specified in --scan-serial,
  473. * even ones that aren't Icarus; be sure they can all handle it, when
  474. * this is changed...
  475. * BitForce: Ignores entirely
  476. * ModMiner: Starts (useless) work, gets back to clean state
  477. */
  478. const char golden_nonce[] = "000187a2";
  479. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  480. char nonce_hex[(sizeof(nonce_bin) * 2) + 1];
  481. get_options(this_option_offset, info);
  482. int baud = info->baud;
  483. int work_division = info->work_division;
  484. int fpga_count = info->fpga_count;
  485. applog(LOG_DEBUG, "Icarus Detect: Attempting to open %s", devpath);
  486. fd = icarus_open2(devpath, baud, true);
  487. if (unlikely(fd == -1)) {
  488. applog(LOG_DEBUG, "Icarus Detect: Failed to open %s", devpath);
  489. return false;
  490. }
  491. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  492. icarus_write(fd, ob_bin, sizeof(ob_bin));
  493. cgtime(&tv_start);
  494. memset(nonce_bin, 0, sizeof(nonce_bin));
  495. icarus_gets(nonce_bin, fd, &tv_finish, NULL, 1);
  496. icarus_close(fd);
  497. bin2hex(nonce_hex, nonce_bin, sizeof(nonce_bin));
  498. if (strncmp(nonce_hex, golden_nonce, 8)) {
  499. applog(LOG_DEBUG,
  500. "Icarus Detect: "
  501. "Test failed at %s: get %s, should: %s",
  502. devpath, nonce_hex, golden_nonce);
  503. return false;
  504. }
  505. applog(LOG_DEBUG,
  506. "Icarus Detect: "
  507. "Test succeeded at %s: got %s",
  508. devpath, nonce_hex);
  509. if (serial_claim_v(devpath, api))
  510. return false;
  511. /* We have a real Icarus! */
  512. struct cgpu_info *icarus;
  513. icarus = calloc(1, sizeof(struct cgpu_info));
  514. icarus->drv = api;
  515. icarus->device_path = strdup(devpath);
  516. icarus->device_fd = -1;
  517. icarus->threads = 1;
  518. add_cgpu(icarus);
  519. applog(LOG_INFO, "Found %"PRIpreprv" at %s",
  520. icarus->proc_repr,
  521. devpath);
  522. applog(LOG_DEBUG, "%"PRIpreprv": Init: baud=%d work_division=%d fpga_count=%d",
  523. icarus->proc_repr,
  524. baud, work_division, fpga_count);
  525. icarus->device_data = info;
  526. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  527. set_timing_mode(this_option_offset, icarus);
  528. return true;
  529. }
  530. static bool icarus_detect_one(const char *devpath)
  531. {
  532. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  533. if (unlikely(!info))
  534. quit(1, "Failed to malloc ICARUS_INFO");
  535. // TODO: try some higher speeds with the Icarus and BFL to see
  536. // if they support them and if setting them makes any difference
  537. // N.B. B3000000 doesn't work on Icarus
  538. info->baud = ICARUS_IO_SPEED;
  539. info->quirk_reopen = 1;
  540. info->Hs = ICARUS_REV3_HASH_TIME;
  541. info->timing_mode = MODE_DEFAULT;
  542. if (!icarus_detect_custom(devpath, &icarus_drv, info)) {
  543. free(info);
  544. return false;
  545. }
  546. return true;
  547. }
  548. static void icarus_detect()
  549. {
  550. serial_detect(&icarus_drv, icarus_detect_one);
  551. }
  552. static bool icarus_prepare(struct thr_info *thr)
  553. {
  554. struct cgpu_info *icarus = thr->cgpu;
  555. struct ICARUS_INFO *info = icarus->device_data;
  556. icarus->device_fd = -1;
  557. int fd = icarus_open2(icarus->device_path, info->baud, true);
  558. if (unlikely(-1 == fd)) {
  559. applog(LOG_ERR, "Failed to open Icarus on %s",
  560. icarus->device_path);
  561. return false;
  562. }
  563. icarus->device_fd = fd;
  564. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  565. struct icarus_state *state;
  566. thr->cgpu_data = state = calloc(1, sizeof(*state));
  567. state->firstrun = true;
  568. #ifdef HAVE_EPOLL
  569. int epollfd = epoll_create(2);
  570. if (epollfd != -1)
  571. {
  572. close(epollfd);
  573. notifier_init(thr->work_restart_notifier);
  574. }
  575. #endif
  576. icarus->status = LIFE_INIT2;
  577. return true;
  578. }
  579. static bool icarus_init(struct thr_info *thr)
  580. {
  581. struct cgpu_info *icarus = thr->cgpu;
  582. struct ICARUS_INFO *info = icarus->device_data;
  583. int fd = icarus->device_fd;
  584. if (!info->work_division)
  585. {
  586. struct timeval tv_finish;
  587. uint32_t res;
  588. applog(LOG_DEBUG, "%"PRIpreprv": Work division not specified - autodetecting", icarus->proc_repr);
  589. // Special packet to probe work_division
  590. unsigned char pkt[64] =
  591. "\x2e\x4c\x8f\x91\xfd\x59\x5d\x2d\x7e\xa2\x0a\xaa\xcb\x64\xa2\xa0"
  592. "\x43\x82\x86\x02\x77\xcf\x26\xb6\xa1\xee\x04\xc5\x6a\x5b\x50\x4a"
  593. "BFGMiner Probe\0\0"
  594. "BFG\0\x64\x61\x01\x1a\xc9\x06\xa9\x51\xfb\x9b\x3c\x73";
  595. icarus_write(fd, pkt, sizeof(pkt));
  596. if (ICA_GETS_OK == icarus_gets((unsigned char*)&res, fd, &tv_finish, NULL, info->read_count))
  597. res = be32toh(res);
  598. else
  599. res = 0;
  600. switch (res) {
  601. case 0x04C0FDB4:
  602. info->work_division = 1;
  603. break;
  604. case 0x82540E46:
  605. info->work_division = 2;
  606. break;
  607. case 0x417C0F36:
  608. info->work_division = 4;
  609. break;
  610. case 0x60C994D5:
  611. info->work_division = 8;
  612. break;
  613. default:
  614. applog(LOG_ERR, "%"PRIpreprv": Work division autodetection failed (assuming 2): got %08x", icarus->proc_repr, res);
  615. info->work_division = 2;
  616. }
  617. applog(LOG_DEBUG, "%"PRIpreprv": Work division autodetection got %08x (=%d)", icarus->proc_repr, res, info->work_division);
  618. }
  619. if (!info->fpga_count)
  620. info->fpga_count = info->work_division;
  621. info->nonce_mask = mask(info->work_division);
  622. return true;
  623. }
  624. static bool icarus_reopen(struct cgpu_info *icarus, struct icarus_state *state, int *fdp)
  625. {
  626. struct ICARUS_INFO *info = icarus->device_data;
  627. // Reopen the serial port to workaround a USB-host-chipset-specific issue with the Icarus's buggy USB-UART
  628. do_icarus_close(icarus->thr[0]);
  629. *fdp = icarus->device_fd = icarus_open(icarus->device_path, info->baud);
  630. if (unlikely(-1 == *fdp)) {
  631. applog(LOG_ERR, "%"PRIpreprv": Failed to reopen on %s", icarus->proc_repr, icarus->device_path);
  632. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  633. state->firstrun = true;
  634. return false;
  635. }
  636. return true;
  637. }
  638. static
  639. bool icarus_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  640. {
  641. struct cgpu_info * const icarus = thr->cgpu;
  642. struct icarus_state * const state = thr->cgpu_data;
  643. uint8_t * const ob_bin = state->ob_bin;
  644. memcpy(ob_bin, work->midstate, 32);
  645. memcpy(ob_bin + 52, work->data + 64, 12);
  646. if (!(memcmp(&ob_bin[56], "\xff\xff\xff\xff", 4)
  647. || 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))) {
  648. // This sequence is used on cairnsmore bitstreams for commands, NEVER send it otherwise
  649. applog(LOG_WARNING, "%"PRIpreprv": Received job attempting to send a command, corrupting it!",
  650. icarus->proc_repr);
  651. ob_bin[56] = 0;
  652. }
  653. rev(ob_bin, 32);
  654. rev(ob_bin + 52, 12);
  655. return true;
  656. }
  657. static bool icarus_job_start(struct thr_info *thr)
  658. {
  659. struct cgpu_info *icarus = thr->cgpu;
  660. struct ICARUS_INFO *info = icarus->device_data;
  661. struct icarus_state *state = thr->cgpu_data;
  662. const uint8_t * const ob_bin = state->ob_bin;
  663. int fd = icarus->device_fd;
  664. int ret;
  665. // Handle dynamic clocking for "subclass" devices
  666. // This needs to run before sending next job, since it hashes the command too
  667. if (info->dclk.freqM && likely(!state->firstrun)) {
  668. dclk_preUpdate(&info->dclk);
  669. dclk_updateFreq(&info->dclk, info->dclk_change_clock_func, thr);
  670. }
  671. cgtime(&state->tv_workstart);
  672. ret = icarus_write(fd, ob_bin, 64);
  673. if (ret) {
  674. do_icarus_close(thr);
  675. applog(LOG_ERR, "%"PRIpreprv": Comms error (werr=%d)", icarus->proc_repr, ret);
  676. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  677. return false; /* This should never happen */
  678. }
  679. if (opt_debug) {
  680. char ob_hex[129];
  681. bin2hex(ob_hex, ob_bin, 64);
  682. applog(LOG_DEBUG, "%"PRIpreprv" sent: %s",
  683. icarus->proc_repr,
  684. ob_hex);
  685. }
  686. return true;
  687. }
  688. static
  689. struct work *icarus_process_worknonce(struct icarus_state *state, uint32_t *nonce)
  690. {
  691. *nonce = be32toh(*nonce);
  692. if (test_nonce(state->last_work, *nonce, false))
  693. return state->last_work;
  694. if (likely(state->last2_work && test_nonce(state->last2_work, *nonce, false)))
  695. return state->last2_work;
  696. return NULL;
  697. }
  698. static
  699. void handle_identify(struct thr_info * const thr, int ret, const bool was_first_run)
  700. {
  701. const struct cgpu_info * const icarus = thr->cgpu;
  702. const struct ICARUS_INFO * const info = icarus->device_data;
  703. struct icarus_state * const state = thr->cgpu_data;
  704. int fd = icarus->device_fd;
  705. struct timeval tv_now;
  706. double delapsed;
  707. uint32_t nonce;
  708. if (fd == -1)
  709. return;
  710. // If identify is requested (block erupters):
  711. // 1. Don't start the next job right away (above)
  712. // 2. Wait for the current job to complete 100%
  713. if (!was_first_run)
  714. {
  715. applog(LOG_DEBUG, "%"PRIpreprv": Identify: Waiting for current job to finish", icarus->proc_repr);
  716. while (true)
  717. {
  718. cgtime(&tv_now);
  719. delapsed = tdiff(&tv_now, &state->tv_workstart);
  720. if (delapsed + 0.1 > info->fullnonce)
  721. break;
  722. // Try to get more nonces (ignoring work restart)
  723. ret = icarus_gets((void *)&nonce, fd, &tv_now, NULL, (info->fullnonce - delapsed) * 10);
  724. if (ret == ICA_GETS_OK)
  725. {
  726. nonce = be32toh(nonce);
  727. submit_nonce(thr, state->last_work, nonce);
  728. }
  729. }
  730. }
  731. else
  732. applog(LOG_DEBUG, "%"PRIpreprv": Identify: Current job should already be finished", icarus->proc_repr);
  733. // 3. Delay 3 more seconds
  734. applog(LOG_DEBUG, "%"PRIpreprv": Identify: Leaving idle for 3 seconds", icarus->proc_repr);
  735. cgsleep_ms(3000);
  736. // Check for work restart in the meantime
  737. if (thr->work_restart)
  738. {
  739. applog(LOG_DEBUG, "%"PRIpreprv": Identify: Work restart requested during delay", icarus->proc_repr);
  740. goto no_job_start;
  741. }
  742. // 4. Start next job
  743. if (!state->firstrun)
  744. {
  745. applog(LOG_DEBUG, "%"PRIpreprv": Identify: Starting next job", icarus->proc_repr);
  746. if (!icarus_job_start(thr))
  747. no_job_start:
  748. state->firstrun = true;
  749. }
  750. state->identify = false;
  751. }
  752. static
  753. void icarus_transition_work(struct icarus_state *state, struct work *work)
  754. {
  755. if (state->last2_work)
  756. free_work(state->last2_work);
  757. state->last2_work = state->last_work;
  758. state->last_work = copy_work(work);
  759. }
  760. static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  761. __maybe_unused int64_t max_nonce)
  762. {
  763. struct cgpu_info *icarus;
  764. int fd;
  765. int ret;
  766. struct ICARUS_INFO *info;
  767. uint32_t nonce;
  768. struct work *nonce_work;
  769. int64_t hash_count;
  770. struct timeval tv_start = {.tv_sec=0}, elapsed;
  771. struct timeval tv_history_start, tv_history_finish;
  772. double Ti, Xi;
  773. int i;
  774. bool was_hw_error = false;
  775. bool was_first_run;
  776. struct ICARUS_HISTORY *history0, *history;
  777. int count;
  778. double Hs, W, fullnonce;
  779. int read_count;
  780. int64_t estimate_hashes;
  781. uint32_t values;
  782. int64_t hash_count_range;
  783. elapsed.tv_sec = elapsed.tv_usec = 0;
  784. icarus = thr->cgpu;
  785. struct icarus_state *state = thr->cgpu_data;
  786. was_first_run = state->firstrun;
  787. icarus_job_prepare(thr, work, max_nonce);
  788. // Wait for the previous run's result
  789. fd = icarus->device_fd;
  790. info = icarus->device_data;
  791. if (unlikely(fd == -1) && !icarus_reopen(icarus, state, &fd))
  792. return -1;
  793. if (!state->firstrun) {
  794. if (state->changework)
  795. {
  796. state->changework = false;
  797. ret = ICA_GETS_RESTART;
  798. }
  799. else
  800. {
  801. read_count = info->read_count;
  802. keepwaiting:
  803. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  804. ret = icarus_gets((void*)&nonce, fd, &state->tv_workfinish, thr, read_count);
  805. switch (ret) {
  806. case ICA_GETS_RESTART:
  807. // The prepared work is invalid, and the current work is abandoned
  808. // Go back to the main loop to get the next work, and stuff
  809. // Returning to the main loop will clear work_restart, so use a flag...
  810. state->changework = true;
  811. return 0;
  812. case ICA_GETS_ERROR:
  813. do_icarus_close(thr);
  814. applog(LOG_ERR, "%"PRIpreprv": Comms error (rerr)", icarus->proc_repr);
  815. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  816. if (!icarus_reopen(icarus, state, &fd))
  817. return -1;
  818. break;
  819. case ICA_GETS_TIMEOUT:
  820. if (info->quirk_reopen == 1 && !icarus_reopen(icarus, state, &fd))
  821. return -1;
  822. case ICA_GETS_OK:
  823. break;
  824. }
  825. }
  826. tv_start = state->tv_workstart;
  827. timersub(&state->tv_workfinish, &tv_start, &elapsed);
  828. }
  829. else
  830. {
  831. if (fd == -1 && !icarus_reopen(icarus, state, &fd))
  832. return -1;
  833. // First run; no nonce, no hashes done
  834. ret = ICA_GETS_ERROR;
  835. }
  836. #ifndef WIN32
  837. tcflush(fd, TCOFLUSH);
  838. #endif
  839. if (ret == ICA_GETS_OK)
  840. {
  841. nonce_work = icarus_process_worknonce(state, &nonce);
  842. if (likely(nonce_work))
  843. {
  844. if (nonce_work == state->last2_work)
  845. {
  846. // nonce was for the last job; submit and keep processing the current one
  847. submit_nonce(thr, nonce_work, nonce);
  848. goto keepwaiting;
  849. }
  850. if (info->continue_search)
  851. {
  852. read_count = info->read_count - ((timer_elapsed_us(&state->tv_workstart, NULL) / (1000000 / TIME_FACTOR)) + 1);
  853. if (read_count)
  854. {
  855. submit_nonce(thr, nonce_work, nonce);
  856. goto keepwaiting;
  857. }
  858. }
  859. }
  860. else
  861. was_hw_error = true;
  862. }
  863. // Handle dynamic clocking for "subclass" devices
  864. // This needs to run before sending next job, since it hashes the command too
  865. if (info->dclk.freqM && likely(ret == ICA_GETS_OK || ret == ICA_GETS_TIMEOUT)) {
  866. int qsec = ((4 * elapsed.tv_sec) + (elapsed.tv_usec / 250000)) ?: 1;
  867. for (int n = qsec; n; --n)
  868. dclk_gotNonces(&info->dclk);
  869. if (was_hw_error)
  870. dclk_errorCount(&info->dclk, qsec);
  871. }
  872. // Force a USB close/reopen on any hw error
  873. if (was_hw_error && info->quirk_reopen != 2) {
  874. if (!icarus_reopen(icarus, state, &fd))
  875. state->firstrun = true;
  876. }
  877. if (unlikely(state->identify))
  878. {
  879. // Delay job start until later...
  880. }
  881. else
  882. if (unlikely(icarus->deven != DEV_ENABLED || !icarus_job_start(thr)))
  883. state->firstrun = true;
  884. if (info->quirk_reopen == 2 && !icarus_reopen(icarus, state, &fd))
  885. state->firstrun = true;
  886. work->blk.nonce = 0xffffffff;
  887. if (ret == ICA_GETS_ERROR) {
  888. state->firstrun = false;
  889. icarus_transition_work(state, work);
  890. hash_count = 0;
  891. goto out;
  892. }
  893. // OK, done starting Icarus's next job... now process the last run's result!
  894. // aborted before becoming idle, get new work
  895. if (ret == ICA_GETS_TIMEOUT || ret == ICA_GETS_RESTART) {
  896. icarus_transition_work(state, work);
  897. // ONLY up to just when it aborted
  898. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  899. estimate_hashes = ((double)(elapsed.tv_sec)
  900. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  901. // If some Serial-USB delay allowed the full nonce range to
  902. // complete it can't have done more than a full nonce
  903. if (unlikely(estimate_hashes > 0xffffffff))
  904. estimate_hashes = 0xffffffff;
  905. if (opt_debug) {
  906. applog(LOG_DEBUG, "%"PRIpreprv" no nonce = 0x%08"PRIx64" hashes (%"PRId64".%06lus)",
  907. icarus->proc_repr,
  908. (uint64_t)estimate_hashes,
  909. (int64_t)elapsed.tv_sec, (unsigned long)elapsed.tv_usec);
  910. }
  911. hash_count = estimate_hashes;
  912. goto out;
  913. }
  914. // Only ICA_GETS_OK gets here
  915. if (likely(!was_hw_error))
  916. submit_nonce(thr, nonce_work, nonce);
  917. else
  918. inc_hw_errors(thr, state->last_work, nonce);
  919. icarus_transition_work(state, work);
  920. hash_count = (nonce & info->nonce_mask);
  921. hash_count++;
  922. hash_count *= info->fpga_count;
  923. if (opt_debug) {
  924. applog(LOG_DEBUG, "%"PRIpreprv" nonce = 0x%08x = 0x%08" PRIx64 " hashes (%"PRId64".%06lus)",
  925. icarus->proc_repr,
  926. nonce,
  927. (uint64_t)hash_count,
  928. (int64_t)elapsed.tv_sec, (unsigned long)elapsed.tv_usec);
  929. }
  930. if (info->do_default_detection && elapsed.tv_sec >= DEFAULT_DETECT_THRESHOLD) {
  931. int MHs = (double)hash_count / ((double)elapsed.tv_sec * 1e6 + (double)elapsed.tv_usec);
  932. --info->do_default_detection;
  933. applog(LOG_DEBUG, "%"PRIpreprv": Autodetect device speed: %d MH/s", icarus->proc_repr, MHs);
  934. if (MHs <= 370 || MHs > 420) {
  935. // Not a real Icarus: enable short timing
  936. applog(LOG_WARNING, "%"PRIpreprv": Seems too %s to be an Icarus; calibrating with short timing", icarus->proc_repr, MHs>380?"fast":"slow");
  937. info->timing_mode = MODE_SHORT;
  938. info->do_icarus_timing = true;
  939. info->do_default_detection = 0;
  940. }
  941. else
  942. if (MHs <= 380) {
  943. // Real Icarus?
  944. if (!info->do_default_detection) {
  945. applog(LOG_DEBUG, "%"PRIpreprv": Seems to be a real Icarus", icarus->proc_repr);
  946. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  947. }
  948. }
  949. else
  950. if (MHs <= 420) {
  951. // Enterpoint Cairnsmore1
  952. size_t old_repr_len = strlen(icarus->proc_repr);
  953. char old_repr[old_repr_len + 1];
  954. strcpy(old_repr, icarus->proc_repr);
  955. convert_icarus_to_cairnsmore(icarus);
  956. info->do_default_detection = 0;
  957. applog(LOG_WARNING, "%"PRIpreprv": Detected Cairnsmore1 device, upgrading driver to %"PRIpreprv, old_repr, icarus->proc_repr);
  958. }
  959. }
  960. // ignore possible end condition values ... and hw errors
  961. if (info->do_icarus_timing
  962. && !was_hw_error
  963. && ((nonce & info->nonce_mask) > END_CONDITION)
  964. && ((nonce & info->nonce_mask) < (info->nonce_mask & ~END_CONDITION))) {
  965. cgtime(&tv_history_start);
  966. history0 = &(info->history[0]);
  967. if (history0->values == 0)
  968. timeradd(&tv_start, &history_sec, &(history0->finish));
  969. Ti = (double)(elapsed.tv_sec)
  970. + ((double)(elapsed.tv_usec))/((double)1000000)
  971. - ((double)ICARUS_READ_TIME(info->baud));
  972. Xi = (double)hash_count;
  973. history0->sumXiTi += Xi * Ti;
  974. history0->sumXi += Xi;
  975. history0->sumTi += Ti;
  976. history0->sumXi2 += Xi * Xi;
  977. history0->values++;
  978. if (history0->hash_count_max < hash_count)
  979. history0->hash_count_max = hash_count;
  980. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  981. history0->hash_count_min = hash_count;
  982. if (history0->values >= info->min_data_count
  983. && timercmp(&tv_start, &(history0->finish), >)) {
  984. for (i = INFO_HISTORY; i > 0; i--)
  985. memcpy(&(info->history[i]),
  986. &(info->history[i-1]),
  987. sizeof(struct ICARUS_HISTORY));
  988. // Initialise history0 to zero for summary calculation
  989. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  990. // We just completed a history data set
  991. // So now recalc read_count based on the whole history thus we will
  992. // initially get more accurate until it completes INFO_HISTORY
  993. // total data sets
  994. count = 0;
  995. for (i = 1 ; i <= INFO_HISTORY; i++) {
  996. history = &(info->history[i]);
  997. if (history->values >= MIN_DATA_COUNT) {
  998. count++;
  999. history0->sumXiTi += history->sumXiTi;
  1000. history0->sumXi += history->sumXi;
  1001. history0->sumTi += history->sumTi;
  1002. history0->sumXi2 += history->sumXi2;
  1003. history0->values += history->values;
  1004. if (history0->hash_count_max < history->hash_count_max)
  1005. history0->hash_count_max = history->hash_count_max;
  1006. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  1007. history0->hash_count_min = history->hash_count_min;
  1008. }
  1009. }
  1010. // All history data
  1011. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  1012. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  1013. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  1014. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  1015. values = history0->values;
  1016. // Initialise history0 to zero for next data set
  1017. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  1018. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  1019. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  1020. info->Hs = Hs;
  1021. info->read_count = read_count;
  1022. info->fullnonce = fullnonce;
  1023. info->count = count;
  1024. info->W = W;
  1025. info->values = values;
  1026. info->hash_count_range = hash_count_range;
  1027. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  1028. info->min_data_count *= 2;
  1029. else if (info->timing_mode == MODE_SHORT)
  1030. info->do_icarus_timing = false;
  1031. // applog(LOG_DEBUG, "%"PRIpreprv" 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->proc_repr, read_count, fullnonce, count, Hs, W, values, hash_count_range, info->min_data_count);
  1032. applog(LOG_DEBUG, "%"PRIpreprv" Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  1033. icarus->proc_repr,
  1034. Hs, W, read_count, fullnonce);
  1035. }
  1036. info->history_count++;
  1037. cgtime(&tv_history_finish);
  1038. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  1039. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  1040. }
  1041. out:
  1042. if (unlikely(state->identify))
  1043. handle_identify(thr, ret, was_first_run);
  1044. return hash_count;
  1045. }
  1046. static struct api_data *icarus_drv_stats(struct cgpu_info *cgpu)
  1047. {
  1048. struct api_data *root = NULL;
  1049. struct ICARUS_INFO *info = cgpu->device_data;
  1050. // Warning, access to these is not locked - but we don't really
  1051. // care since hashing performance is way more important than
  1052. // locking access to displaying API debug 'stats'
  1053. // If locking becomes an issue for any of them, use copy_data=true also
  1054. root = api_add_int(root, "read_count", &(info->read_count), false);
  1055. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  1056. root = api_add_int(root, "count", &(info->count), false);
  1057. root = api_add_hs(root, "Hs", &(info->Hs), false);
  1058. root = api_add_double(root, "W", &(info->W), false);
  1059. root = api_add_uint(root, "total_values", &(info->values), false);
  1060. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  1061. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  1062. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  1063. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  1064. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  1065. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  1066. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  1067. root = api_add_int(root, "baud", &(info->baud), false);
  1068. root = api_add_int(root, "work_division", &(info->work_division), false);
  1069. root = api_add_int(root, "fpga_count", &(info->fpga_count), false);
  1070. return root;
  1071. }
  1072. static void icarus_shutdown(struct thr_info *thr)
  1073. {
  1074. do_icarus_close(thr);
  1075. free(thr->cgpu_data);
  1076. }
  1077. struct device_drv icarus_drv = {
  1078. .dname = "icarus",
  1079. .name = "ICA",
  1080. .drv_detect = icarus_detect,
  1081. .get_api_stats = icarus_drv_stats,
  1082. .thread_prepare = icarus_prepare,
  1083. .thread_init = icarus_init,
  1084. .scanhash = icarus_scanhash,
  1085. .thread_disable = close_device_fd,
  1086. .thread_shutdown = icarus_shutdown,
  1087. };