driver-icarus.c 33 KB

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