driver-icarus.c 40 KB

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