driver-icarus.c 43 KB

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