driver-icarus.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. * Copyright 2012-2013 Andrew Smith
  3. * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
  4. * Copyright 2013 Con Kolivas <kernel@kolivas.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. /*
  12. * Those code should be works fine with V2 and V3 bitstream of Icarus.
  13. * Operation:
  14. * No detection implement.
  15. * Input: 64B = 32B midstate + 20B fill bytes + last 12 bytes of block head.
  16. * Return: send back 32bits immediately when Icarus found a valid nonce.
  17. * no query protocol implemented here, if no data send back in ~11.3
  18. * seconds (full cover time on 32bit nonce range by 380MH/s speed)
  19. * just send another work.
  20. * Notice:
  21. * 1. Icarus will start calculate when you push a work to them, even they
  22. * are busy.
  23. * 2. The 2 FPGAs on Icarus will distribute the job, one will calculate the
  24. * 0 ~ 7FFFFFFF, another one will cover the 80000000 ~ FFFFFFFF.
  25. * 3. It's possible for 2 FPGAs both find valid nonce in the meantime, the 2
  26. * valid nonce will all be send back.
  27. * 4. Icarus will stop work when: a valid nonce has been found or 32 bits
  28. * nonce range is completely calculated.
  29. */
  30. #include <float.h>
  31. #include <limits.h>
  32. #include <pthread.h>
  33. #include <stdint.h>
  34. #include <stdio.h>
  35. #include <strings.h>
  36. #include <sys/time.h>
  37. #include <unistd.h>
  38. #include "config.h"
  39. #ifdef WIN32
  40. #include <windows.h>
  41. #endif
  42. #include "compat.h"
  43. #include "miner.h"
  44. #include "usbutils.h"
  45. // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
  46. #define ICARUS_IO_SPEED 115200
  47. // The size of a successful nonce read
  48. #define ICARUS_READ_SIZE 4
  49. // Ensure the sizes are correct for the Serial read
  50. #if (ICARUS_READ_SIZE != 4)
  51. #error ICARUS_READ_SIZE must be 4
  52. #endif
  53. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  54. ASSERT1(sizeof(uint32_t) == 4);
  55. // TODO: USB? Different calculation? - see usbstats to work it out e.g. 1/2 of normal send time
  56. // or even use that number? 1/2
  57. // #define ICARUS_READ_TIME(baud) ((double)ICARUS_READ_SIZE * (double)8.0 / (double)(baud))
  58. // maybe 1ms?
  59. #define ICARUS_READ_TIME(baud) (0.001)
  60. // USB ms timeout to wait
  61. #define ICARUS_WAIT_TIMEOUT 100
  62. // In timing mode: Default starting value until an estimate can be obtained
  63. // 5000 ms allows for up to a ~840MH/s device
  64. #define ICARUS_READ_COUNT_TIMING 5000
  65. #define ICARUS_READ_COUNT_MIN ICARUS_WAIT_TIMEOUT
  66. #define SECTOMS(s) ((int)((s) * 1000))
  67. // How many ms below the expected completion time to abort work
  68. // extra in case the last read is delayed
  69. #define ICARUS_READ_REDUCE ((int)(ICARUS_WAIT_TIMEOUT * 1.5))
  70. // For a standard Icarus REV3 (to 5 places)
  71. // Since this rounds up a the last digit - it is a slight overestimate
  72. // Thus the hash rate will be a VERY slight underestimate
  73. // (by a lot less than the displayed accuracy)
  74. // Minor inaccuracy of these numbers doesn't affect the work done,
  75. // only the displayed MH/s
  76. #define ICARUS_REV3_HASH_TIME 0.0000000026316
  77. #define LANCELOT_HASH_TIME 0.0000000025000
  78. #define ASICMINERUSB_HASH_TIME 0.0000000029761
  79. // TODO: What is it?
  80. #define CAIRNSMORE1_HASH_TIME 0.0000000026316
  81. #define NANOSEC 1000000000.0
  82. // Icarus Rev3 doesn't send a completion message when it finishes
  83. // the full nonce range, so to avoid being idle we must abort the
  84. // work (by starting a new work item) shortly before it finishes
  85. //
  86. // Thus we need to estimate 2 things:
  87. // 1) How many hashes were done if the work was aborted
  88. // 2) How high can the timeout be before the Icarus is idle,
  89. // to minimise the number of work items started
  90. // We set 2) to 'the calculated estimate' - ICARUS_READ_REDUCE
  91. // to ensure the estimate ends before idle
  92. //
  93. // The simple calculation used is:
  94. // Tn = Total time in seconds to calculate n hashes
  95. // Hs = seconds per hash
  96. // Xn = number of hashes
  97. // W = code/usb overhead per work
  98. //
  99. // Rough but reasonable estimate:
  100. // Tn = Hs * Xn + W (of the form y = mx + b)
  101. //
  102. // Thus:
  103. // Line of best fit (using least squares)
  104. //
  105. // Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2)
  106. // W = Sum(Ti)/n - (Hs*Sum(Xi))/n
  107. //
  108. // N.B. W is less when aborting work since we aren't waiting for the reply
  109. // to be transferred back (ICARUS_READ_TIME)
  110. // Calculating the hashes aborted at n seconds is thus just n/Hs
  111. // (though this is still a slight overestimate due to code delays)
  112. //
  113. // Both below must be exceeded to complete a set of data
  114. // Minimum how long after the first, the last data point must be
  115. #define HISTORY_SEC 60
  116. // Minimum how many points a single ICARUS_HISTORY should have
  117. #define MIN_DATA_COUNT 5
  118. // The value MIN_DATA_COUNT used is doubled each history until it exceeds:
  119. #define MAX_MIN_DATA_COUNT 100
  120. static struct timeval history_sec = { HISTORY_SEC, 0 };
  121. // Store the last INFO_HISTORY data sets
  122. // [0] = current data, not yet ready to be included as an estimate
  123. // Each new data set throws the last old set off the end thus
  124. // keeping a ongoing average of recent data
  125. #define INFO_HISTORY 10
  126. struct ICARUS_HISTORY {
  127. struct timeval finish;
  128. double sumXiTi;
  129. double sumXi;
  130. double sumTi;
  131. double sumXi2;
  132. uint32_t values;
  133. uint32_t hash_count_min;
  134. uint32_t hash_count_max;
  135. };
  136. enum timing_mode { MODE_DEFAULT, MODE_SHORT, MODE_LONG, MODE_VALUE };
  137. static const char *MODE_DEFAULT_STR = "default";
  138. static const char *MODE_SHORT_STR = "short";
  139. static const char *MODE_LONG_STR = "long";
  140. static const char *MODE_VALUE_STR = "value";
  141. static const char *MODE_UNKNOWN_STR = "unknown";
  142. struct ICARUS_INFO {
  143. // time to calculate the golden_ob
  144. uint64_t golden_hashes;
  145. struct timeval golden_tv;
  146. struct ICARUS_HISTORY history[INFO_HISTORY+1];
  147. uint32_t min_data_count;
  148. // seconds per Hash
  149. double Hs;
  150. // ms til we abort
  151. int read_time;
  152. enum timing_mode timing_mode;
  153. bool do_icarus_timing;
  154. double fullnonce;
  155. int count;
  156. double W;
  157. uint32_t values;
  158. uint64_t hash_count_range;
  159. // Determine the cost of history processing
  160. // (which will only affect W)
  161. uint64_t history_count;
  162. struct timeval history_time;
  163. // icarus-options
  164. int baud;
  165. int work_division;
  166. int fpga_count;
  167. uint32_t nonce_mask;
  168. };
  169. #define END_CONDITION 0x0000ffff
  170. // Looking for options in --icarus-timing and --icarus-options:
  171. //
  172. // Code increments this each time we start to look at a device
  173. // However, this means that if other devices are checked by
  174. // the Icarus code (e.g. Avalon only as at 20130517)
  175. // they will count in the option offset
  176. //
  177. // This, however, is deterministic so that's OK
  178. //
  179. // If we were to increment after successfully finding an Icarus
  180. // that would be random since an Icarus may fail and thus we'd
  181. // not be able to predict the option order
  182. //
  183. // Devices are checked in the order libusb finds them which is ?
  184. //
  185. static int option_offset = -1;
  186. struct device_drv icarus_drv;
  187. /*
  188. #define ICA_BUFSIZ (0x200)
  189. static void transfer_read(struct cgpu_info *icarus, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, char *buf, int bufsiz, int *amount, enum usb_cmds cmd)
  190. {
  191. int err;
  192. err = usb_transfer_read(icarus, request_type, bRequest, wValue, wIndex, buf, bufsiz, amount, cmd);
  193. applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
  194. icarus->drv->name, icarus->cgminer_id,
  195. usb_cmdname(cmd), err);
  196. }
  197. */
  198. static void _transfer(struct cgpu_info *icarus, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, enum usb_cmds cmd)
  199. {
  200. int err;
  201. err = usb_transfer_data(icarus, request_type, bRequest, wValue, wIndex, data, siz, cmd);
  202. applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
  203. icarus->drv->name, icarus->cgminer_id,
  204. usb_cmdname(cmd), err);
  205. }
  206. #define transfer(icarus, request_type, bRequest, wValue, wIndex, cmd) \
  207. _transfer(icarus, request_type, bRequest, wValue, wIndex, NULL, 0, cmd)
  208. static void icarus_initialise(struct cgpu_info *icarus, int baud)
  209. {
  210. uint16_t wValue, wIndex;
  211. if (icarus->usbinfo.nodev)
  212. return;
  213. switch (icarus->usbdev->ident) {
  214. case IDENT_BLT:
  215. case IDENT_LLT:
  216. case IDENT_CMR1:
  217. case IDENT_CMR2:
  218. // Latency
  219. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY, FTDI_VALUE_LATENCY,
  220. icarus->usbdev->found->interface, C_LATENCY);
  221. if (icarus->usbinfo.nodev)
  222. return;
  223. // Reset
  224. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_RESET,
  225. icarus->usbdev->found->interface, C_RESET);
  226. if (icarus->usbinfo.nodev)
  227. return;
  228. // Set data control
  229. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, FTDI_VALUE_DATA_BLT,
  230. icarus->usbdev->found->interface, C_SETDATA);
  231. if (icarus->usbinfo.nodev)
  232. return;
  233. // default to BLT/LLT 115200
  234. wValue = FTDI_VALUE_BAUD_BLT;
  235. wIndex = FTDI_INDEX_BAUD_BLT;
  236. if (icarus->usbdev->ident == IDENT_CMR1 ||
  237. icarus->usbdev->ident == IDENT_CMR2) {
  238. switch (baud) {
  239. case 115200:
  240. wValue = FTDI_VALUE_BAUD_CMR_115;
  241. wIndex = FTDI_INDEX_BAUD_CMR_115;
  242. break;
  243. case 57600:
  244. wValue = FTDI_VALUE_BAUD_CMR_57;
  245. wIndex = FTDI_INDEX_BAUD_CMR_57;
  246. break;
  247. default:
  248. quit(1, "icarus_intialise() invalid baud (%d) for Cairnsmore1", baud);
  249. break;
  250. }
  251. }
  252. // Set the baud
  253. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, wValue,
  254. (wIndex & 0xff00) | icarus->usbdev->found->interface,
  255. C_SETBAUD);
  256. if (icarus->usbinfo.nodev)
  257. return;
  258. // Set Modem Control
  259. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM, FTDI_VALUE_MODEM,
  260. icarus->usbdev->found->interface, C_SETMODEM);
  261. if (icarus->usbinfo.nodev)
  262. return;
  263. // Set Flow Control
  264. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW, FTDI_VALUE_FLOW,
  265. icarus->usbdev->found->interface, C_SETFLOW);
  266. if (icarus->usbinfo.nodev)
  267. return;
  268. // Clear any sent data
  269. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_TX,
  270. icarus->usbdev->found->interface, C_PURGETX);
  271. if (icarus->usbinfo.nodev)
  272. return;
  273. // Clear any received data
  274. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_RX,
  275. icarus->usbdev->found->interface, C_PURGERX);
  276. break;
  277. case IDENT_ICA:
  278. // Set Data Control
  279. transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, PL2303_VALUE_CTRL,
  280. icarus->usbdev->found->interface, C_SETDATA);
  281. if (icarus->usbinfo.nodev)
  282. return;
  283. // Set Line Control
  284. uint32_t ica_data[2] = { PL2303_VALUE_LINE0, PL2303_VALUE_LINE1 };
  285. _transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_LINE, PL2303_VALUE_LINE,
  286. icarus->usbdev->found->interface,
  287. &ica_data[0], PL2303_VALUE_LINE_SIZE, C_SETLINE);
  288. if (icarus->usbinfo.nodev)
  289. return;
  290. // Vendor
  291. transfer(icarus, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, PL2303_VALUE_VENDOR,
  292. icarus->usbdev->found->interface, C_VENDOR);
  293. break;
  294. case IDENT_AMU:
  295. // Set data control
  296. transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_DATA, CP210X_VALUE_DATA,
  297. icarus->usbdev->found->interface, C_SETDATA);
  298. if (icarus->usbinfo.nodev)
  299. return;
  300. // Set the baud
  301. uint32_t data = CP210X_DATA_BAUD;
  302. _transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD, 0,
  303. icarus->usbdev->found->interface,
  304. &data, sizeof(data), C_SETBAUD);
  305. break;
  306. default:
  307. quit(1, "icarus_intialise() called with invalid %s cgid %i ident=%d",
  308. icarus->drv->name, icarus->cgminer_id,
  309. icarus->usbdev->ident);
  310. }
  311. }
  312. static void rev(unsigned char *s, size_t l)
  313. {
  314. size_t i, j;
  315. unsigned char t;
  316. for (i = 0, j = l - 1; i < j; i++, j--) {
  317. t = s[i];
  318. s[i] = s[j];
  319. s[j] = t;
  320. }
  321. }
  322. #define ICA_NONCE_ERROR -1
  323. #define ICA_NONCE_OK 0
  324. #define ICA_NONCE_RESTART 1
  325. #define ICA_NONCE_TIMEOUT 2
  326. static int icarus_get_nonce(struct cgpu_info *icarus, unsigned char *buf, struct timeval *tv_start, struct timeval *tv_finish, struct thr_info *thr, int read_time)
  327. {
  328. struct timeval read_start, read_finish;
  329. int err, amt;
  330. int rc = 0;
  331. int read_amount = ICARUS_READ_SIZE;
  332. bool first = true;
  333. cgtime(tv_start);
  334. while (true) {
  335. if (icarus->usbinfo.nodev)
  336. return ICA_NONCE_ERROR;
  337. cgtime(&read_start);
  338. err = usb_read_timeout(icarus, (char *)buf, read_amount, &amt, ICARUS_WAIT_TIMEOUT, C_GETRESULTS);
  339. cgtime(&read_finish);
  340. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
  341. applog(LOG_ERR, "%s%i: Comms error (rerr=%d amt=%d)",
  342. icarus->drv->name, icarus->device_id, err, amt);
  343. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  344. return ICA_NONCE_ERROR;
  345. }
  346. if (first)
  347. copy_time(tv_finish, &read_finish);
  348. // TODO: test if there is more data? to read a 2nd nonce?
  349. if (amt >= read_amount)
  350. return ICA_NONCE_OK;
  351. rc += SECTOMS(tdiff(&read_finish, &read_start));
  352. if (rc >= read_time) {
  353. if (amt > 0)
  354. applog(LOG_DEBUG, "Icarus Read: Timeout reading for %d ms", rc);
  355. else
  356. applog(LOG_DEBUG, "Icarus Read: No data for %d ms", rc);
  357. return ICA_NONCE_TIMEOUT;
  358. }
  359. if (thr && thr->work_restart) {
  360. if (opt_debug) {
  361. applog(LOG_DEBUG,
  362. "Icarus Read: Work restart at %d ms", rc);
  363. }
  364. return ICA_NONCE_RESTART;
  365. }
  366. if (amt > 0) {
  367. buf += amt;
  368. read_amount -= amt;
  369. first = false;
  370. }
  371. }
  372. }
  373. static const char *timing_mode_str(enum timing_mode timing_mode)
  374. {
  375. switch(timing_mode) {
  376. case MODE_DEFAULT:
  377. return MODE_DEFAULT_STR;
  378. case MODE_SHORT:
  379. return MODE_SHORT_STR;
  380. case MODE_LONG:
  381. return MODE_LONG_STR;
  382. case MODE_VALUE:
  383. return MODE_VALUE_STR;
  384. default:
  385. return MODE_UNKNOWN_STR;
  386. }
  387. }
  388. static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
  389. {
  390. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  391. double Hs;
  392. char buf[BUFSIZ+1];
  393. char *ptr, *comma, *eq;
  394. size_t max;
  395. int i;
  396. if (opt_icarus_timing == NULL)
  397. buf[0] = '\0';
  398. else {
  399. ptr = opt_icarus_timing;
  400. for (i = 0; i < this_option_offset; i++) {
  401. comma = strchr(ptr, ',');
  402. if (comma == NULL)
  403. break;
  404. ptr = comma + 1;
  405. }
  406. comma = strchr(ptr, ',');
  407. if (comma == NULL)
  408. max = strlen(ptr);
  409. else
  410. max = comma - ptr;
  411. if (max > BUFSIZ)
  412. max = BUFSIZ;
  413. strncpy(buf, ptr, max);
  414. buf[max] = '\0';
  415. }
  416. switch (icarus->usbdev->ident) {
  417. case IDENT_ICA:
  418. info->Hs = ICARUS_REV3_HASH_TIME;
  419. break;
  420. case IDENT_BLT:
  421. case IDENT_LLT:
  422. info->Hs = LANCELOT_HASH_TIME;
  423. break;
  424. case IDENT_AMU:
  425. info->Hs = ASICMINERUSB_HASH_TIME;
  426. break;
  427. // TODO: ?
  428. case IDENT_CMR1:
  429. case IDENT_CMR2:
  430. info->Hs = CAIRNSMORE1_HASH_TIME;
  431. break;
  432. default:
  433. quit(1, "Icarus get_options() called with invalid %s ident=%d",
  434. icarus->drv->name, icarus->usbdev->ident);
  435. }
  436. info->read_time = 0;
  437. // TODO: allow short=N and long=N
  438. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  439. info->read_time = ICARUS_READ_COUNT_TIMING;
  440. info->timing_mode = MODE_SHORT;
  441. info->do_icarus_timing = true;
  442. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  443. info->read_time = ICARUS_READ_COUNT_TIMING;
  444. info->timing_mode = MODE_LONG;
  445. info->do_icarus_timing = true;
  446. } else if ((Hs = atof(buf)) != 0) {
  447. info->Hs = Hs / NANOSEC;
  448. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  449. if ((eq = strchr(buf, '=')) != NULL)
  450. info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
  451. if (info->read_time < ICARUS_READ_COUNT_MIN)
  452. info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
  453. if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
  454. info->read_time = ICARUS_READ_COUNT_MIN;
  455. info->timing_mode = MODE_VALUE;
  456. info->do_icarus_timing = false;
  457. } else {
  458. // Anything else in buf just uses DEFAULT mode
  459. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  460. if ((eq = strchr(buf, '=')) != NULL)
  461. info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
  462. if (info->read_time < ICARUS_READ_COUNT_MIN)
  463. info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
  464. if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
  465. info->read_time = ICARUS_READ_COUNT_MIN;
  466. info->timing_mode = MODE_DEFAULT;
  467. info->do_icarus_timing = false;
  468. }
  469. info->min_data_count = MIN_DATA_COUNT;
  470. applog(LOG_DEBUG, "%s: cgid %d Init: mode=%s read_time=%dms Hs=%e",
  471. icarus->drv->name, icarus->cgminer_id,
  472. timing_mode_str(info->timing_mode),
  473. info->read_time, info->Hs);
  474. }
  475. static uint32_t mask(int work_division)
  476. {
  477. char err_buf[BUFSIZ+1];
  478. uint32_t nonce_mask = 0x7fffffff;
  479. // yes we can calculate these, but this way it's easy to see what they are
  480. switch (work_division) {
  481. case 1:
  482. nonce_mask = 0xffffffff;
  483. break;
  484. case 2:
  485. nonce_mask = 0x7fffffff;
  486. break;
  487. case 4:
  488. nonce_mask = 0x3fffffff;
  489. break;
  490. case 8:
  491. nonce_mask = 0x1fffffff;
  492. break;
  493. default:
  494. sprintf(err_buf, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division);
  495. quit(1, err_buf);
  496. }
  497. return nonce_mask;
  498. }
  499. static void get_options(int this_option_offset, struct cgpu_info *icarus, int *baud, int *work_division, int *fpga_count)
  500. {
  501. char err_buf[BUFSIZ+1];
  502. char buf[BUFSIZ+1];
  503. char *ptr, *comma, *colon, *colon2;
  504. size_t max;
  505. int i, tmp;
  506. if (opt_icarus_options == NULL)
  507. buf[0] = '\0';
  508. else {
  509. ptr = opt_icarus_options;
  510. for (i = 0; i < this_option_offset; i++) {
  511. comma = strchr(ptr, ',');
  512. if (comma == NULL)
  513. break;
  514. ptr = comma + 1;
  515. }
  516. comma = strchr(ptr, ',');
  517. if (comma == NULL)
  518. max = strlen(ptr);
  519. else
  520. max = comma - ptr;
  521. if (max > BUFSIZ)
  522. max = BUFSIZ;
  523. strncpy(buf, ptr, max);
  524. buf[max] = '\0';
  525. }
  526. switch (icarus->usbdev->ident) {
  527. case IDENT_ICA:
  528. case IDENT_BLT:
  529. case IDENT_LLT:
  530. *baud = ICARUS_IO_SPEED;
  531. *work_division = 2;
  532. *fpga_count = 2;
  533. break;
  534. case IDENT_AMU:
  535. *baud = ICARUS_IO_SPEED;
  536. *work_division = 1;
  537. *fpga_count = 1;
  538. break;
  539. // TODO: ?
  540. case IDENT_CMR1:
  541. case IDENT_CMR2:
  542. *baud = ICARUS_IO_SPEED;
  543. *work_division = 2;
  544. *fpga_count = 2;
  545. break;
  546. default:
  547. quit(1, "Icarus get_options() called with invalid %s ident=%d",
  548. icarus->drv->name, icarus->usbdev->ident);
  549. }
  550. if (*buf) {
  551. colon = strchr(buf, ':');
  552. if (colon)
  553. *(colon++) = '\0';
  554. if (*buf) {
  555. tmp = atoi(buf);
  556. switch (tmp) {
  557. case 115200:
  558. *baud = 115200;
  559. break;
  560. case 57600:
  561. *baud = 57600;
  562. break;
  563. default:
  564. sprintf(err_buf, "Invalid icarus-options for baud (%s) must be 115200 or 57600", buf);
  565. quit(1, err_buf);
  566. }
  567. }
  568. if (colon && *colon) {
  569. colon2 = strchr(colon, ':');
  570. if (colon2)
  571. *(colon2++) = '\0';
  572. if (*colon) {
  573. tmp = atoi(colon);
  574. if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) {
  575. *work_division = tmp;
  576. *fpga_count = tmp; // default to the same
  577. } else {
  578. sprintf(err_buf, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
  579. quit(1, err_buf);
  580. }
  581. }
  582. if (colon2 && *colon2) {
  583. tmp = atoi(colon2);
  584. if (tmp > 0 && tmp <= *work_division)
  585. *fpga_count = tmp;
  586. else {
  587. sprintf(err_buf, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
  588. quit(1, err_buf);
  589. }
  590. }
  591. }
  592. }
  593. }
  594. static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  595. {
  596. int this_option_offset = ++option_offset;
  597. char devpath[20];
  598. struct ICARUS_INFO *info;
  599. struct timeval tv_start, tv_finish;
  600. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  601. // N.B. golden_ob MUST take less time to calculate
  602. // than the timeout set in icarus_open()
  603. // This one takes ~0.53ms on Rev3 Icarus
  604. const char golden_ob[] =
  605. "4679ba4ec99876bf4bfe086082b40025"
  606. "4df6c356451471139a3afa71e48f544a"
  607. "00000000000000000000000000000000"
  608. "0000000087320b1a1426674f2fa722ce";
  609. const char golden_nonce[] = "000187a2";
  610. const uint32_t golden_nonce_val = 0x000187a2;
  611. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  612. char *nonce_hex;
  613. int baud, uninitialised_var(work_division), uninitialised_var(fpga_count);
  614. struct cgpu_info *icarus;
  615. int ret, err, amount, tries;
  616. bool ok;
  617. icarus = calloc(1, sizeof(struct cgpu_info));
  618. if (unlikely(!icarus))
  619. quit(1, "Failed to calloc icarus in icarus_detect_one");
  620. icarus->drv = &icarus_drv;
  621. icarus->deven = DEV_ENABLED;
  622. icarus->threads = 1;
  623. if (!usb_init(icarus, dev, found))
  624. goto shin;
  625. get_options(this_option_offset, icarus, &baud, &work_division, &fpga_count);
  626. sprintf(devpath, "%d:%d",
  627. (int)(icarus->usbinfo.bus_number),
  628. (int)(icarus->usbinfo.device_address));
  629. icarus->device_path = strdup(devpath);
  630. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  631. tries = 2;
  632. ok = false;
  633. while (!ok && tries-- > 0) {
  634. icarus_initialise(icarus, baud);
  635. err = usb_write(icarus, (char *)ob_bin, sizeof(ob_bin), &amount, C_SENDTESTWORK);
  636. if (err != LIBUSB_SUCCESS || amount != sizeof(ob_bin))
  637. continue;
  638. memset(nonce_bin, 0, sizeof(nonce_bin));
  639. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 100);
  640. if (ret != ICA_NONCE_OK)
  641. continue;
  642. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  643. if (strncmp(nonce_hex, golden_nonce, 8) == 0)
  644. ok = true;
  645. else {
  646. if (tries < 0) {
  647. applog(LOG_ERR,
  648. "Icarus Detect: "
  649. "Test failed at %s: get %s, should: %s",
  650. devpath, nonce_hex, golden_nonce);
  651. }
  652. }
  653. free(nonce_hex);
  654. }
  655. if (!ok)
  656. goto unshin;
  657. applog(LOG_DEBUG,
  658. "Icarus Detect: "
  659. "Test succeeded at %s: got %s",
  660. devpath, golden_nonce);
  661. /* We have a real Icarus! */
  662. if (!add_cgpu(icarus))
  663. goto unshin;
  664. update_usb_stats(icarus);
  665. applog(LOG_INFO, "%s%d: Found at %s",
  666. icarus->drv->name, icarus->device_id, devpath);
  667. applog(LOG_DEBUG, "%s%d: Init baud=%d work_division=%d fpga_count=%d",
  668. icarus->drv->name, icarus->device_id, baud, work_division, fpga_count);
  669. info = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  670. if (unlikely(!info))
  671. quit(1, "Failed to malloc ICARUS_INFO");
  672. icarus->device_data = (void *)info;
  673. // Initialise everything to zero for a new device
  674. memset(info, 0, sizeof(struct ICARUS_INFO));
  675. info->baud = baud;
  676. info->work_division = work_division;
  677. info->fpga_count = fpga_count;
  678. info->nonce_mask = mask(work_division);
  679. info->golden_hashes = (golden_nonce_val & info->nonce_mask) * fpga_count;
  680. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  681. set_timing_mode(this_option_offset, icarus);
  682. return true;
  683. unshin:
  684. usb_uninit(icarus);
  685. free(icarus->device_path);
  686. shin:
  687. free(icarus);
  688. return false;
  689. }
  690. static void icarus_detect()
  691. {
  692. usb_detect(&icarus_drv, icarus_detect_one);
  693. }
  694. static bool icarus_prepare(struct thr_info *thr)
  695. {
  696. struct cgpu_info *icarus = thr->cgpu;
  697. struct timeval now;
  698. cgtime(&now);
  699. get_datestamp(icarus->init, &now);
  700. return true;
  701. }
  702. static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  703. __maybe_unused int64_t max_nonce)
  704. {
  705. struct cgpu_info *icarus = thr->cgpu;
  706. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  707. int ret, err, amount;
  708. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  709. char *ob_hex;
  710. uint32_t nonce;
  711. int64_t hash_count;
  712. struct timeval tv_start, tv_finish, elapsed;
  713. struct timeval tv_history_start, tv_history_finish;
  714. double Ti, Xi;
  715. int curr_hw_errors, i;
  716. bool was_hw_error;
  717. struct ICARUS_HISTORY *history0, *history;
  718. int count;
  719. double Hs, W, fullnonce;
  720. int read_time;
  721. int64_t estimate_hashes;
  722. uint32_t values;
  723. int64_t hash_count_range;
  724. // Device is gone
  725. if (icarus->usbinfo.nodev)
  726. return -1;
  727. elapsed.tv_sec = elapsed.tv_usec = 0;
  728. memset(ob_bin, 0, sizeof(ob_bin));
  729. memcpy(ob_bin, work->midstate, 32);
  730. memcpy(ob_bin + 52, work->data + 64, 12);
  731. rev(ob_bin, 32);
  732. rev(ob_bin + 52, 12);
  733. err = usb_write(icarus, (char *)ob_bin, sizeof(ob_bin), &amount, C_SENDWORK);
  734. if (err < 0 || amount != sizeof(ob_bin)) {
  735. applog(LOG_ERR, "%s%i: Comms error (werr=%d amt=%d)",
  736. icarus->drv->name, icarus->device_id, err, amount);
  737. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  738. icarus_initialise(icarus, info->baud);
  739. return 0;
  740. }
  741. if (opt_debug) {
  742. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  743. applog(LOG_DEBUG, "%s%d: sent %s",
  744. icarus->drv->name, icarus->device_id, ob_hex);
  745. free(ob_hex);
  746. }
  747. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  748. memset(nonce_bin, 0, sizeof(nonce_bin));
  749. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, info->read_time);
  750. if (ret == ICA_NONCE_ERROR)
  751. return 0;
  752. work->blk.nonce = 0xffffffff;
  753. // aborted before becoming idle, get new work
  754. if (ret == ICA_NONCE_TIMEOUT || ret == ICA_NONCE_RESTART) {
  755. timersub(&tv_finish, &tv_start, &elapsed);
  756. // ONLY up to just when it aborted
  757. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  758. estimate_hashes = ((double)(elapsed.tv_sec)
  759. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  760. // If some Serial-USB delay allowed the full nonce range to
  761. // complete it can't have done more than a full nonce
  762. if (unlikely(estimate_hashes > 0xffffffff))
  763. estimate_hashes = 0xffffffff;
  764. if (opt_debug) {
  765. applog(LOG_DEBUG, "%s%d: no nonce = 0x%08lX hashes (%ld.%06lds)",
  766. icarus->drv->name, icarus->device_id,
  767. (long unsigned int)estimate_hashes,
  768. elapsed.tv_sec, elapsed.tv_usec);
  769. }
  770. return estimate_hashes;
  771. }
  772. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  773. nonce = htobe32(nonce);
  774. curr_hw_errors = icarus->hw_errors;
  775. submit_nonce(thr, work, nonce);
  776. was_hw_error = (curr_hw_errors > icarus->hw_errors);
  777. hash_count = (nonce & info->nonce_mask);
  778. hash_count++;
  779. hash_count *= info->fpga_count;
  780. if (opt_debug || info->do_icarus_timing)
  781. timersub(&tv_finish, &tv_start, &elapsed);
  782. if (opt_debug) {
  783. applog(LOG_DEBUG, "%s%d: nonce = 0x%08x = 0x%08lX hashes (%ld.%06lds)",
  784. icarus->drv->name, icarus->device_id,
  785. nonce, (long unsigned int)hash_count,
  786. elapsed.tv_sec, elapsed.tv_usec);
  787. }
  788. // ignore possible end condition values ... and hw errors
  789. if (info->do_icarus_timing
  790. && !was_hw_error
  791. && ((nonce & info->nonce_mask) > END_CONDITION)
  792. && ((nonce & info->nonce_mask) < (info->nonce_mask & ~END_CONDITION))) {
  793. cgtime(&tv_history_start);
  794. history0 = &(info->history[0]);
  795. if (history0->values == 0)
  796. timeradd(&tv_start, &history_sec, &(history0->finish));
  797. Ti = (double)(elapsed.tv_sec)
  798. + ((double)(elapsed.tv_usec))/((double)1000000)
  799. - ((double)ICARUS_READ_TIME(info->baud));
  800. Xi = (double)hash_count;
  801. history0->sumXiTi += Xi * Ti;
  802. history0->sumXi += Xi;
  803. history0->sumTi += Ti;
  804. history0->sumXi2 += Xi * Xi;
  805. history0->values++;
  806. if (history0->hash_count_max < hash_count)
  807. history0->hash_count_max = hash_count;
  808. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  809. history0->hash_count_min = hash_count;
  810. if (history0->values >= info->min_data_count
  811. && timercmp(&tv_start, &(history0->finish), >)) {
  812. for (i = INFO_HISTORY; i > 0; i--)
  813. memcpy(&(info->history[i]),
  814. &(info->history[i-1]),
  815. sizeof(struct ICARUS_HISTORY));
  816. // Initialise history0 to zero for summary calculation
  817. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  818. // We just completed a history data set
  819. // So now recalc read_time based on the whole history thus we will
  820. // initially get more accurate until it completes INFO_HISTORY
  821. // total data sets
  822. count = 0;
  823. for (i = 1 ; i <= INFO_HISTORY; i++) {
  824. history = &(info->history[i]);
  825. if (history->values >= MIN_DATA_COUNT) {
  826. count++;
  827. history0->sumXiTi += history->sumXiTi;
  828. history0->sumXi += history->sumXi;
  829. history0->sumTi += history->sumTi;
  830. history0->sumXi2 += history->sumXi2;
  831. history0->values += history->values;
  832. if (history0->hash_count_max < history->hash_count_max)
  833. history0->hash_count_max = history->hash_count_max;
  834. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  835. history0->hash_count_min = history->hash_count_min;
  836. }
  837. }
  838. // All history data
  839. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  840. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  841. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  842. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  843. values = history0->values;
  844. // Initialise history0 to zero for next data set
  845. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  846. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  847. read_time = SECTOMS(fullnonce) - ICARUS_READ_REDUCE;
  848. info->Hs = Hs;
  849. info->read_time = read_time;
  850. info->fullnonce = fullnonce;
  851. info->count = count;
  852. info->W = W;
  853. info->values = values;
  854. info->hash_count_range = hash_count_range;
  855. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  856. info->min_data_count *= 2;
  857. else if (info->timing_mode == MODE_SHORT)
  858. info->do_icarus_timing = false;
  859. applog(LOG_WARNING, "%s%d Re-estimate: Hs=%e W=%e read_time=%dms fullnonce=%.3fs",
  860. icarus->drv->name, icarus->device_id, Hs, W, read_time, fullnonce);
  861. }
  862. info->history_count++;
  863. cgtime(&tv_history_finish);
  864. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  865. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  866. }
  867. return hash_count;
  868. }
  869. static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
  870. {
  871. struct api_data *root = NULL;
  872. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  873. // Warning, access to these is not locked - but we don't really
  874. // care since hashing performance is way more important than
  875. // locking access to displaying API debug 'stats'
  876. // If locking becomes an issue for any of them, use copy_data=true also
  877. root = api_add_int(root, "read_time", &(info->read_time), false);
  878. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  879. root = api_add_int(root, "count", &(info->count), false);
  880. root = api_add_hs(root, "Hs", &(info->Hs), false);
  881. root = api_add_double(root, "W", &(info->W), false);
  882. root = api_add_uint(root, "total_values", &(info->values), false);
  883. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  884. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  885. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  886. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  887. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  888. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  889. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  890. root = api_add_int(root, "baud", &(info->baud), false);
  891. root = api_add_int(root, "work_division", &(info->work_division), false);
  892. root = api_add_int(root, "fpga_count", &(info->fpga_count), false);
  893. return root;
  894. }
  895. static void icarus_shutdown(__maybe_unused struct thr_info *thr)
  896. {
  897. // TODO: ?
  898. }
  899. struct device_drv icarus_drv = {
  900. .drv_id = DRIVER_ICARUS,
  901. .dname = "Icarus",
  902. .name = "ICA",
  903. .drv_detect = icarus_detect,
  904. .get_api_stats = icarus_api_stats,
  905. .thread_prepare = icarus_prepare,
  906. .scanhash = icarus_scanhash,
  907. .thread_shutdown = icarus_shutdown,
  908. };