driver-icarus.c 40 KB

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