driver-icarus.c 42 KB

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