driver-icarus.c 44 KB

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