driver-icarus.c 41 KB

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