driver-icarus.c 41 KB

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