driver-icarus.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462
  1. /*
  2. * Copyright 2012-2013 Andrew Smith
  3. * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
  4. * Copyright 2013-2014 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 <math.h>
  39. #include "config.h"
  40. #ifdef WIN32
  41. #include <windows.h>
  42. #endif
  43. #include "compat.h"
  44. #include "miner.h"
  45. #include "usbutils.h"
  46. // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
  47. #define ICARUS_IO_SPEED 115200
  48. #define ICARUS_BUF_SIZE 8
  49. // The size of a successful nonce read
  50. #define ANT_READ_SIZE 5
  51. #define ICARUS_READ_SIZE 4
  52. #define ROCK_READ_SIZE 8
  53. // Ensure the sizes are correct for the Serial read
  54. #if (ICARUS_READ_SIZE != 4)
  55. #error ICARUS_READ_SIZE must be 4
  56. #endif
  57. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  58. ASSERT1(sizeof(uint32_t) == 4);
  59. // TODO: USB? Different calculation? - see usbstats to work it out e.g. 1/2 of normal send time
  60. // or even use that number? 1/2
  61. // #define ICARUS_READ_TIME(baud) ((double)ICARUS_READ_SIZE * (double)8.0 / (double)(baud))
  62. // maybe 1ms?
  63. #define ICARUS_READ_TIME(baud) (0.001)
  64. // USB ms timeout to wait - user specified timeouts are multiples of this
  65. #define ICA_WAIT_TIMEOUT 100
  66. #define ANT_WAIT_TIMEOUT 10
  67. #define AU3_WAIT_TIMEOUT 1
  68. #define ICARUS_WAIT_TIMEOUT (info->u3 ? AU3_WAIT_TIMEOUT : (info->ant ? ANT_WAIT_TIMEOUT : ICA_WAIT_TIMEOUT))
  69. #define ICARUS_CMR2_TIMEOUT 1
  70. // Defined in multiples of ICARUS_WAIT_TIMEOUT
  71. // Must of course be greater than ICARUS_READ_COUNT_TIMING/ICARUS_WAIT_TIMEOUT
  72. // There's no need to have this bigger, since the overhead/latency of extra work
  73. // is pretty small once you get beyond a 10s nonce range time and 10s also
  74. // means that nothing slower than 429MH/s can go idle so most icarus devices
  75. // will always mine without idling
  76. #define ICARUS_READ_TIME_LIMIT_MAX 100
  77. // In timing mode: Default starting value until an estimate can be obtained
  78. // 5000 ms allows for up to a ~840MH/s device
  79. #define ICARUS_READ_COUNT_TIMING 5000
  80. // Antminer USB is > 1GH/s so use a shorter limit
  81. // 1000 ms allows for up to ~4GH/s device
  82. #define ANTUSB_READ_COUNT_TIMING 1000
  83. #define ANTU3_READ_COUNT_TIMING 100
  84. #define ICARUS_READ_COUNT_MIN ICARUS_WAIT_TIMEOUT
  85. #define SECTOMS(s) ((int)((s) * 1000))
  86. // How many ms below the expected completion time to abort work
  87. // extra in case the last read is delayed
  88. #define ICARUS_READ_REDUCE ((int)(ICARUS_WAIT_TIMEOUT * 1.5))
  89. // For a standard Icarus REV3 (to 5 places)
  90. // Since this rounds up a the last digit - it is a slight overestimate
  91. // Thus the hash rate will be a VERY slight underestimate
  92. // (by a lot less than the displayed accuracy)
  93. // Minor inaccuracy of these numbers doesn't affect the work done,
  94. // only the displayed MH/s
  95. #define ICARUS_REV3_HASH_TIME 0.0000000026316
  96. #define LANCELOT_HASH_TIME 0.0000000025000
  97. #define ASICMINERUSB_HASH_TIME 0.0000000029761
  98. // TODO: What is it?
  99. #define CAIRNSMORE1_HASH_TIME 0.0000000027000
  100. // Per FPGA
  101. #define CAIRNSMORE2_HASH_TIME 0.0000000066600
  102. #define NANOSEC 1000000000.0
  103. #define ANTMINERUSB_HASH_MHZ 0.000000125
  104. #define ANTMINERUSB_HASH_TIME (ANTMINERUSB_HASH_MHZ / (double)(opt_anu_freq))
  105. #define ANTU3_HASH_MHZ 0.0000000032
  106. #define ANTU3_HASH_TIME (ANTU3_HASH_MHZ / (double)(opt_au3_freq))
  107. #define CAIRNSMORE2_INTS 4
  108. // Icarus Rev3 doesn't send a completion message when it finishes
  109. // the full nonce range, so to avoid being idle we must abort the
  110. // work (by starting a new work item) shortly before it finishes
  111. //
  112. // Thus we need to estimate 2 things:
  113. // 1) How many hashes were done if the work was aborted
  114. // 2) How high can the timeout be before the Icarus is idle,
  115. // to minimise the number of work items started
  116. // We set 2) to 'the calculated estimate' - ICARUS_READ_REDUCE
  117. // to ensure the estimate ends before idle
  118. //
  119. // The simple calculation used is:
  120. // Tn = Total time in seconds to calculate n hashes
  121. // Hs = seconds per hash
  122. // Xn = number of hashes
  123. // W = code/usb overhead per work
  124. //
  125. // Rough but reasonable estimate:
  126. // Tn = Hs * Xn + W (of the form y = mx + b)
  127. //
  128. // Thus:
  129. // Line of best fit (using least squares)
  130. //
  131. // Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2)
  132. // W = Sum(Ti)/n - (Hs*Sum(Xi))/n
  133. //
  134. // N.B. W is less when aborting work since we aren't waiting for the reply
  135. // to be transferred back (ICARUS_READ_TIME)
  136. // Calculating the hashes aborted at n seconds is thus just n/Hs
  137. // (though this is still a slight overestimate due to code delays)
  138. //
  139. // Both below must be exceeded to complete a set of data
  140. // Minimum how long after the first, the last data point must be
  141. #define HISTORY_SEC 60
  142. // Minimum how many points a single ICARUS_HISTORY should have
  143. #define MIN_DATA_COUNT 5
  144. // The value MIN_DATA_COUNT used is doubled each history until it exceeds:
  145. #define MAX_MIN_DATA_COUNT 100
  146. static struct timeval history_sec = { HISTORY_SEC, 0 };
  147. // Store the last INFO_HISTORY data sets
  148. // [0] = current data, not yet ready to be included as an estimate
  149. // Each new data set throws the last old set off the end thus
  150. // keeping a ongoing average of recent data
  151. #define INFO_HISTORY 10
  152. struct ICARUS_HISTORY {
  153. struct timeval finish;
  154. double sumXiTi;
  155. double sumXi;
  156. double sumTi;
  157. double sumXi2;
  158. uint32_t values;
  159. uint32_t hash_count_min;
  160. uint32_t hash_count_max;
  161. };
  162. enum timing_mode { MODE_DEFAULT, MODE_SHORT, MODE_LONG, MODE_VALUE };
  163. static const char *MODE_DEFAULT_STR = "default";
  164. static const char *MODE_SHORT_STR = "short";
  165. static const char *MODE_SHORT_STREQ = "short=";
  166. static const char *MODE_LONG_STR = "long";
  167. static const char *MODE_LONG_STREQ = "long=";
  168. static const char *MODE_VALUE_STR = "value";
  169. static const char *MODE_UNKNOWN_STR = "unknown";
  170. #define MAX_DEVICE_NUM 100
  171. #define MAX_WORK_BUFFER_SIZE 2
  172. #define MAX_CHIP_NUM 24
  173. // Set it to 3, 5 or 9
  174. #define NONCE_CORRECTION_TIMES 5
  175. #define MAX_TRIES 4
  176. #define RM_CMD_MASK 0x0F
  177. #define RM_STATUS_MASK 0xF0
  178. #define RM_CHIP_MASK 0x3F
  179. #define RM_PRODUCT_MASK 0xC0
  180. #define RM_PRODUCT_RBOX 0x00
  181. #define RM_PRODUCT_T1 0x40
  182. #define RM_PRODUCT_T2 0x80
  183. #define RM_PRODUCT_TEST 0xC0
  184. #if (NONCE_CORRECTION_TIMES == 5)
  185. static int32_t rbox_corr_values[] = {0, 1, -1, -2, -4};
  186. #endif
  187. #if (NONCE_CORRECTION_TIMES == 9)
  188. static int32_t rbox_corr_values[] = {0, 1, -1, 2, -2, 3, -3, 4, -4};
  189. #endif
  190. #if (NONCE_CORRECTION_TIMES == 3)
  191. static int32_t rbox_corr_values[] = {0, 1, -1};
  192. #endif
  193. #define ANT_QUEUE_NUM 36
  194. typedef enum {
  195. NONCE_DATA1_OFFSET = 0,
  196. NONCE_DATA2_OFFSET,
  197. NONCE_DATA3_OFFSET,
  198. NONCE_DATA4_OFFSET,
  199. NONCE_TASK_CMD_OFFSET,
  200. NONCE_CHIP_NO_OFFSET,
  201. NONCE_TASK_NO_OFFSET,
  202. NONCE_COMMAND_OFFSET,
  203. NONCE_MAX_OFFSET
  204. } NONCE_OFFSET;
  205. typedef enum {
  206. NONCE_DATA_CMD = 0,
  207. NONCE_TASK_COMPLETE_CMD,
  208. NONCE_GET_TASK_CMD,
  209. } NONCE_COMMAND;
  210. typedef struct nonce_data {
  211. int chip_no;
  212. unsigned int task_no ;
  213. unsigned char work_state;
  214. int cmd_value;
  215. } NONCE_DATA;
  216. typedef enum {
  217. ROCKMINER_RBOX = 0,
  218. ROCKMINER_T1,
  219. ROCKMINER_T2,
  220. ROCKMINER_MAX
  221. } ROCKMINER_PRODUCT_T;
  222. typedef struct rockminer_chip_info {
  223. unsigned char freq;
  224. int error_cnt;
  225. time_t last_received_task_complete_time;
  226. } ROCKMINER_CHIP_INFO;
  227. typedef struct rockminer_device_info {
  228. unsigned char detect_chip_no;
  229. unsigned char chip_max;
  230. unsigned char product_id;
  231. float min_frq;
  232. float def_frq;
  233. float max_frq;
  234. ROCKMINER_CHIP_INFO chip[MAX_CHIP_NUM];
  235. time_t dev_detect_time;
  236. } ROCKMINER_DEVICE_INFO;
  237. struct ICARUS_INFO {
  238. enum sub_ident ident;
  239. int intinfo;
  240. // time to calculate the golden_ob
  241. uint64_t golden_hashes;
  242. struct timeval golden_tv;
  243. struct ICARUS_HISTORY history[INFO_HISTORY+1];
  244. uint32_t min_data_count;
  245. int timeout;
  246. // seconds per Hash
  247. double Hs;
  248. // ms til we abort
  249. int read_time;
  250. // ms limit for (short=/long=) read_time
  251. int read_time_limit;
  252. // How long without hashes is considered a failed device
  253. int fail_time;
  254. enum timing_mode timing_mode;
  255. bool do_icarus_timing;
  256. double fullnonce;
  257. int count;
  258. double W;
  259. uint32_t values;
  260. uint64_t hash_count_range;
  261. // Determine the cost of history processing
  262. // (which will only affect W)
  263. uint64_t history_count;
  264. struct timeval history_time;
  265. // icarus-options
  266. int baud;
  267. int work_division;
  268. int fpga_count;
  269. uint32_t nonce_mask;
  270. uint8_t cmr2_speed;
  271. bool speed_next_work;
  272. bool flash_next_work;
  273. int nonce_size;
  274. bool failing;
  275. pthread_mutex_t lock;
  276. ROCKMINER_DEVICE_INFO rmdev;
  277. struct work *base_work; // For when we roll work
  278. struct work *g_work[MAX_CHIP_NUM][MAX_WORK_BUFFER_SIZE];
  279. uint32_t last_nonce[MAX_CHIP_NUM][MAX_WORK_BUFFER_SIZE];
  280. char rock_init[64];
  281. uint64_t nonces_checked;
  282. uint64_t nonces_correction_times;
  283. uint64_t nonces_correction_tests;
  284. uint64_t nonces_fail;
  285. uint64_t nonces_correction[NONCE_CORRECTION_TIMES];
  286. struct work **antworks;
  287. int nonces;
  288. int workid;
  289. bool ant;
  290. bool u3;
  291. };
  292. #define ICARUS_MIDSTATE_SIZE 32
  293. #define ICARUS_UNUSED_SIZE 16
  294. #define ICARUS_WORK_SIZE 12
  295. #define ICARUS_WORK_DATA_OFFSET 64
  296. #define ICARUS_CMR2_SPEED_FACTOR 2.5
  297. #define ICARUS_CMR2_SPEED_MIN_INT 100
  298. #define ICARUS_CMR2_SPEED_DEF_INT 180
  299. #define ICARUS_CMR2_SPEED_MAX_INT 220
  300. #define CMR2_INT_TO_SPEED(_speed) ((uint8_t)((float)_speed / ICARUS_CMR2_SPEED_FACTOR))
  301. #define ICARUS_CMR2_SPEED_MIN CMR2_INT_TO_SPEED(ICARUS_CMR2_SPEED_MIN_INT)
  302. #define ICARUS_CMR2_SPEED_DEF CMR2_INT_TO_SPEED(ICARUS_CMR2_SPEED_DEF_INT)
  303. #define ICARUS_CMR2_SPEED_MAX CMR2_INT_TO_SPEED(ICARUS_CMR2_SPEED_MAX_INT)
  304. #define ICARUS_CMR2_SPEED_INC 1
  305. #define ICARUS_CMR2_SPEED_DEC -1
  306. #define ICARUS_CMR2_SPEED_FAIL -10
  307. #define ICARUS_CMR2_PREFIX ((uint8_t)0xB7)
  308. #define ICARUS_CMR2_CMD_SPEED ((uint8_t)0)
  309. #define ICARUS_CMR2_CMD_FLASH ((uint8_t)1)
  310. #define ICARUS_CMR2_DATA_FLASH_OFF ((uint8_t)0)
  311. #define ICARUS_CMR2_DATA_FLASH_ON ((uint8_t)1)
  312. #define ICARUS_CMR2_CHECK ((uint8_t)0x6D)
  313. #define ANT_UNUSED_SIZE 15
  314. struct ICARUS_WORK {
  315. uint8_t midstate[ICARUS_MIDSTATE_SIZE];
  316. // These 4 bytes are for CMR2 bitstreams that handle MHz adjustment
  317. uint8_t check;
  318. uint8_t data;
  319. uint8_t cmd;
  320. uint8_t prefix;
  321. uint8_t unused[ANT_UNUSED_SIZE];
  322. uint8_t id; // Used only by ANT, otherwise unused by other icarus
  323. uint8_t work[ICARUS_WORK_SIZE];
  324. };
  325. #define ANT_U1_DEFFREQ 200
  326. #define ANT_U3_DEFFREQ 225
  327. #define ANT_U3_MAXFREQ 250
  328. struct {
  329. float freq;
  330. uint16_t hex;
  331. } u3freqtable[] = {
  332. { 100, 0x0783 },
  333. { 125, 0x0983 },
  334. { 150, 0x0b83 },
  335. { 175, 0x0d83 },
  336. { 193.75, 0x0f03 },
  337. { 196.88, 0x1f07 },
  338. { 200, 0x0782 },
  339. { 206.25, 0x1006 },
  340. { 212.5, 0x1086 },
  341. { 218.75, 0x1106 },
  342. { 225, 0x0882 },
  343. { 237.5, 0x1286 },
  344. { 243.75, 0x1306 },
  345. { 250, 0x0982 },
  346. };
  347. #define END_CONDITION 0x0000ffff
  348. // Looking for options in --icarus-timing and --icarus-options:
  349. //
  350. // Code increments this each time we start to look at a device
  351. // However, this means that if other devices are checked by
  352. // the Icarus code (e.g. Avalon only as at 20130517)
  353. // they will count in the option offset
  354. //
  355. // This, however, is deterministic so that's OK
  356. //
  357. // If we were to increment after successfully finding an Icarus
  358. // that would be random since an Icarus may fail and thus we'd
  359. // not be able to predict the option order
  360. //
  361. // Devices are checked in the order libusb finds them which is ?
  362. //
  363. static int option_offset = -1;
  364. /*
  365. #define ICA_BUFSIZ (0x200)
  366. 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)
  367. {
  368. int err;
  369. err = usb_transfer_read(icarus, request_type, bRequest, wValue, wIndex, buf, bufsiz, amount, cmd);
  370. applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
  371. icarus->drv->name, icarus->cgminer_id,
  372. usb_cmdname(cmd), err);
  373. }
  374. */
  375. 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)
  376. {
  377. int err;
  378. err = usb_transfer_data(icarus, request_type, bRequest, wValue, wIndex, data, siz, cmd);
  379. applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
  380. icarus->drv->name, icarus->cgminer_id,
  381. usb_cmdname(cmd), err);
  382. }
  383. #define transfer(icarus, request_type, bRequest, wValue, wIndex, cmd) \
  384. _transfer(icarus, request_type, bRequest, wValue, wIndex, NULL, 0, cmd)
  385. static void icarus_initialise(struct cgpu_info *icarus, int baud)
  386. {
  387. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  388. uint16_t wValue, wIndex;
  389. enum sub_ident ident;
  390. int interface;
  391. if (icarus->usbinfo.nodev)
  392. return;
  393. interface = _usb_interface(icarus, info->intinfo);
  394. ident = usb_ident(icarus);
  395. switch (ident) {
  396. case IDENT_BLT:
  397. case IDENT_LLT:
  398. case IDENT_CMR1:
  399. case IDENT_CMR2:
  400. // Reset
  401. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_RESET,
  402. interface, C_RESET);
  403. if (icarus->usbinfo.nodev)
  404. return;
  405. // Latency
  406. _usb_ftdi_set_latency(icarus, info->intinfo);
  407. if (icarus->usbinfo.nodev)
  408. return;
  409. // Set data control
  410. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, FTDI_VALUE_DATA_BLT,
  411. interface, C_SETDATA);
  412. if (icarus->usbinfo.nodev)
  413. return;
  414. // default to BLT/LLT 115200
  415. wValue = FTDI_VALUE_BAUD_BLT;
  416. wIndex = FTDI_INDEX_BAUD_BLT;
  417. if (ident == IDENT_CMR1 || ident == IDENT_CMR2) {
  418. switch (baud) {
  419. case 115200:
  420. wValue = FTDI_VALUE_BAUD_CMR_115;
  421. wIndex = FTDI_INDEX_BAUD_CMR_115;
  422. break;
  423. case 57600:
  424. wValue = FTDI_VALUE_BAUD_CMR_57;
  425. wIndex = FTDI_INDEX_BAUD_CMR_57;
  426. break;
  427. default:
  428. quit(1, "icarus_intialise() invalid baud (%d) for Cairnsmore1", baud);
  429. break;
  430. }
  431. }
  432. // Set the baud
  433. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, wValue,
  434. (wIndex & 0xff00) | interface, C_SETBAUD);
  435. if (icarus->usbinfo.nodev)
  436. return;
  437. // Set Modem Control
  438. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM, FTDI_VALUE_MODEM,
  439. interface, C_SETMODEM);
  440. if (icarus->usbinfo.nodev)
  441. return;
  442. // Set Flow Control
  443. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW, FTDI_VALUE_FLOW,
  444. interface, C_SETFLOW);
  445. if (icarus->usbinfo.nodev)
  446. return;
  447. // Clear any sent data
  448. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_TX,
  449. interface, C_PURGETX);
  450. if (icarus->usbinfo.nodev)
  451. return;
  452. // Clear any received data
  453. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_RX,
  454. interface, C_PURGERX);
  455. break;
  456. case IDENT_ICA:
  457. // Set Data Control
  458. transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, PL2303_VALUE_CTRL,
  459. interface, C_SETDATA);
  460. if (icarus->usbinfo.nodev)
  461. return;
  462. // Set Line Control
  463. uint32_t ica_data[2] = { PL2303_VALUE_LINE0, PL2303_VALUE_LINE1 };
  464. _transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_LINE, PL2303_VALUE_LINE,
  465. interface, &ica_data[0], PL2303_VALUE_LINE_SIZE, C_SETLINE);
  466. if (icarus->usbinfo.nodev)
  467. return;
  468. // Vendor
  469. transfer(icarus, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, PL2303_VALUE_VENDOR,
  470. interface, C_VENDOR);
  471. break;
  472. case IDENT_AMU:
  473. case IDENT_ANU:
  474. case IDENT_AU3:
  475. case IDENT_LIN:
  476. // Enable the UART
  477. transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE,
  478. CP210X_VALUE_UART_ENABLE,
  479. interface, C_ENABLE_UART);
  480. if (icarus->usbinfo.nodev)
  481. return;
  482. // Set data control
  483. transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_DATA, CP210X_VALUE_DATA,
  484. interface, C_SETDATA);
  485. if (icarus->usbinfo.nodev)
  486. return;
  487. // Set the baud
  488. uint32_t data = CP210X_DATA_BAUD;
  489. _transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD, 0,
  490. interface, &data, sizeof(data), C_SETBAUD);
  491. break;
  492. case IDENT_AVA:
  493. break;
  494. default:
  495. quit(1, "icarus_intialise() called with invalid %s cgid %i ident=%d",
  496. icarus->drv->name, icarus->cgminer_id, ident);
  497. }
  498. }
  499. static void rev(unsigned char *s, size_t l)
  500. {
  501. size_t i, j;
  502. unsigned char t;
  503. for (i = 0, j = l - 1; i < j; i++, j--) {
  504. t = s[i];
  505. s[i] = s[j];
  506. s[j] = t;
  507. }
  508. }
  509. #define ICA_NONCE_ERROR -1
  510. #define ICA_NONCE_OK 0
  511. #define ICA_NONCE_RESTART 1
  512. #define ICA_NONCE_TIMEOUT 2
  513. static int icarus_get_nonce(struct cgpu_info *icarus, unsigned char *buf, struct timeval *tv_start,
  514. struct timeval *tv_finish, struct thr_info *thr, int read_time)
  515. {
  516. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  517. int err, amt, rc;
  518. if (icarus->usbinfo.nodev)
  519. return ICA_NONCE_ERROR;
  520. cgtime(tv_start);
  521. err = usb_read_ii_timeout_cancellable(icarus, info->intinfo, (char *)buf,
  522. info->nonce_size, &amt, read_time,
  523. C_GETRESULTS);
  524. cgtime(tv_finish);
  525. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
  526. applog(LOG_ERR, "%s %i: Comms error (rerr=%d amt=%d)", icarus->drv->name,
  527. icarus->device_id, err, amt);
  528. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  529. return ICA_NONCE_ERROR;
  530. }
  531. if (amt >= info->nonce_size)
  532. return ICA_NONCE_OK;
  533. rc = SECTOMS(tdiff(tv_finish, tv_start));
  534. if (thr && thr->work_restart) {
  535. applog(LOG_DEBUG, "Icarus Read: Work restart at %d ms", rc);
  536. return ICA_NONCE_RESTART;
  537. }
  538. if (amt > 0)
  539. applog(LOG_DEBUG, "Icarus Read: Timeout reading for %d ms", rc);
  540. else
  541. applog(LOG_DEBUG, "Icarus Read: No data for %d ms", rc);
  542. return ICA_NONCE_TIMEOUT;
  543. }
  544. static const char *timing_mode_str(enum timing_mode timing_mode)
  545. {
  546. switch(timing_mode) {
  547. case MODE_DEFAULT:
  548. return MODE_DEFAULT_STR;
  549. case MODE_SHORT:
  550. return MODE_SHORT_STR;
  551. case MODE_LONG:
  552. return MODE_LONG_STR;
  553. case MODE_VALUE:
  554. return MODE_VALUE_STR;
  555. default:
  556. return MODE_UNKNOWN_STR;
  557. }
  558. }
  559. static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
  560. {
  561. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  562. int read_count_timing = 0;
  563. enum sub_ident ident;
  564. double Hs, fail_time;
  565. char buf[BUFSIZ+1];
  566. char *ptr, *comma, *eq;
  567. size_t max;
  568. int i;
  569. if (opt_icarus_timing == NULL)
  570. buf[0] = '\0';
  571. else {
  572. ptr = opt_icarus_timing;
  573. for (i = 0; i < this_option_offset; i++) {
  574. comma = strchr(ptr, ',');
  575. if (comma == NULL)
  576. break;
  577. ptr = comma + 1;
  578. }
  579. comma = strchr(ptr, ',');
  580. if (comma == NULL)
  581. max = strlen(ptr);
  582. else
  583. max = comma - ptr;
  584. if (max > BUFSIZ)
  585. max = BUFSIZ;
  586. strncpy(buf, ptr, max);
  587. buf[max] = '\0';
  588. }
  589. ident = usb_ident(icarus);
  590. switch (ident) {
  591. case IDENT_ICA:
  592. case IDENT_AVA:
  593. info->Hs = ICARUS_REV3_HASH_TIME;
  594. read_count_timing = ICARUS_READ_COUNT_TIMING;
  595. break;
  596. case IDENT_BLT:
  597. case IDENT_LLT:
  598. info->Hs = LANCELOT_HASH_TIME;
  599. read_count_timing = ICARUS_READ_COUNT_TIMING;
  600. break;
  601. case IDENT_AMU:
  602. info->Hs = ASICMINERUSB_HASH_TIME;
  603. read_count_timing = ICARUS_READ_COUNT_TIMING;
  604. break;
  605. case IDENT_CMR1:
  606. info->Hs = CAIRNSMORE1_HASH_TIME;
  607. read_count_timing = ICARUS_READ_COUNT_TIMING;
  608. break;
  609. case IDENT_CMR2:
  610. info->Hs = CAIRNSMORE2_HASH_TIME;
  611. read_count_timing = ICARUS_READ_COUNT_TIMING;
  612. break;
  613. case IDENT_ANU:
  614. info->Hs = ANTMINERUSB_HASH_TIME;
  615. read_count_timing = ANTUSB_READ_COUNT_TIMING;
  616. break;
  617. case IDENT_AU3:
  618. info->Hs = ANTU3_HASH_TIME;
  619. read_count_timing = ANTU3_READ_COUNT_TIMING;
  620. break;
  621. default:
  622. quit(1, "Icarus get_options() called with invalid %s ident=%d",
  623. icarus->drv->name, ident);
  624. }
  625. info->read_time = 0;
  626. info->read_time_limit = 0; // 0 = no limit
  627. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  628. // short
  629. info->read_time = read_count_timing;
  630. info->timing_mode = MODE_SHORT;
  631. info->do_icarus_timing = true;
  632. } else if (strncasecmp(buf, MODE_SHORT_STREQ, strlen(MODE_SHORT_STREQ)) == 0) {
  633. // short=limit
  634. info->read_time = read_count_timing;
  635. info->timing_mode = MODE_SHORT;
  636. info->do_icarus_timing = true;
  637. info->read_time_limit = atoi(&buf[strlen(MODE_SHORT_STREQ)]);
  638. if (info->read_time_limit < 0)
  639. info->read_time_limit = 0;
  640. if (info->read_time_limit > ICARUS_READ_TIME_LIMIT_MAX)
  641. info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX;
  642. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  643. // long
  644. info->read_time = read_count_timing;
  645. info->timing_mode = MODE_LONG;
  646. info->do_icarus_timing = true;
  647. } else if (strncasecmp(buf, MODE_LONG_STREQ, strlen(MODE_LONG_STREQ)) == 0) {
  648. // long=limit
  649. info->read_time = read_count_timing;
  650. info->timing_mode = MODE_LONG;
  651. info->do_icarus_timing = true;
  652. info->read_time_limit = atoi(&buf[strlen(MODE_LONG_STREQ)]);
  653. if (info->read_time_limit < 0)
  654. info->read_time_limit = 0;
  655. if (info->read_time_limit > ICARUS_READ_TIME_LIMIT_MAX)
  656. info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX;
  657. } else if ((Hs = atof(buf)) != 0) {
  658. // ns[=read_time]
  659. info->Hs = Hs / NANOSEC;
  660. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  661. if ((eq = strchr(buf, '=')) != NULL)
  662. info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
  663. if (info->read_time < ICARUS_READ_COUNT_MIN)
  664. info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
  665. if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
  666. info->read_time = ICARUS_READ_COUNT_MIN;
  667. info->timing_mode = MODE_VALUE;
  668. info->do_icarus_timing = false;
  669. } else {
  670. // Anything else in buf just uses DEFAULT mode
  671. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  672. if ((eq = strchr(buf, '=')) != NULL)
  673. info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
  674. if (info->read_time < ICARUS_READ_COUNT_MIN)
  675. info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
  676. if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
  677. info->read_time = ICARUS_READ_COUNT_MIN;
  678. info->timing_mode = MODE_DEFAULT;
  679. info->do_icarus_timing = false;
  680. }
  681. info->min_data_count = MIN_DATA_COUNT;
  682. // All values are in multiples of ICARUS_WAIT_TIMEOUT
  683. info->read_time_limit *= ICARUS_WAIT_TIMEOUT;
  684. applog(LOG_DEBUG, "%s: cgid %d Init: mode=%s read_time=%dms limit=%dms Hs=%e",
  685. icarus->drv->name, icarus->cgminer_id,
  686. timing_mode_str(info->timing_mode),
  687. info->read_time, info->read_time_limit, info->Hs);
  688. /* Set the time to detect a dead device to 30 full nonce ranges. */
  689. fail_time = info->Hs * 0xffffffffull * 30.0;
  690. /* Integer accuracy is definitely enough. */
  691. info->fail_time = fail_time;
  692. }
  693. static uint32_t mask(int work_division)
  694. {
  695. uint32_t nonce_mask = 0x7fffffff;
  696. // yes we can calculate these, but this way it's easy to see what they are
  697. switch (work_division) {
  698. case 1:
  699. nonce_mask = 0xffffffff;
  700. break;
  701. case 2:
  702. nonce_mask = 0x7fffffff;
  703. break;
  704. case 4:
  705. nonce_mask = 0x3fffffff;
  706. break;
  707. case 8:
  708. nonce_mask = 0x1fffffff;
  709. break;
  710. default:
  711. quit(1, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division);
  712. }
  713. return nonce_mask;
  714. }
  715. static void get_options(int this_option_offset, struct cgpu_info *icarus, int *baud, int *work_division, int *fpga_count)
  716. {
  717. char buf[BUFSIZ+1];
  718. char *ptr, *comma, *colon, *colon2;
  719. enum sub_ident ident;
  720. size_t max;
  721. int i, tmp;
  722. if (opt_icarus_options == NULL)
  723. buf[0] = '\0';
  724. else {
  725. ptr = opt_icarus_options;
  726. for (i = 0; i < this_option_offset; i++) {
  727. comma = strchr(ptr, ',');
  728. if (comma == NULL)
  729. break;
  730. ptr = comma + 1;
  731. }
  732. comma = strchr(ptr, ',');
  733. if (comma == NULL)
  734. max = strlen(ptr);
  735. else
  736. max = comma - ptr;
  737. if (max > BUFSIZ)
  738. max = BUFSIZ;
  739. strncpy(buf, ptr, max);
  740. buf[max] = '\0';
  741. }
  742. ident = usb_ident(icarus);
  743. switch (ident) {
  744. case IDENT_ICA:
  745. case IDENT_BLT:
  746. case IDENT_LLT:
  747. case IDENT_AVA:
  748. *baud = ICARUS_IO_SPEED;
  749. *work_division = 2;
  750. *fpga_count = 2;
  751. break;
  752. case IDENT_AMU:
  753. case IDENT_ANU:
  754. case IDENT_AU3:
  755. *baud = ICARUS_IO_SPEED;
  756. *work_division = 1;
  757. *fpga_count = 1;
  758. break;
  759. case IDENT_CMR1:
  760. *baud = ICARUS_IO_SPEED;
  761. *work_division = 2;
  762. *fpga_count = 2;
  763. break;
  764. case IDENT_CMR2:
  765. *baud = ICARUS_IO_SPEED;
  766. *work_division = 1;
  767. *fpga_count = 1;
  768. break;
  769. default:
  770. quit(1, "Icarus get_options() called with invalid %s ident=%d",
  771. icarus->drv->name, ident);
  772. }
  773. if (*buf) {
  774. colon = strchr(buf, ':');
  775. if (colon)
  776. *(colon++) = '\0';
  777. if (*buf) {
  778. tmp = atoi(buf);
  779. switch (tmp) {
  780. case 115200:
  781. *baud = 115200;
  782. break;
  783. case 57600:
  784. *baud = 57600;
  785. break;
  786. default:
  787. quit(1, "Invalid icarus-options for baud (%s) must be 115200 or 57600", buf);
  788. }
  789. }
  790. if (colon && *colon) {
  791. colon2 = strchr(colon, ':');
  792. if (colon2)
  793. *(colon2++) = '\0';
  794. if (*colon) {
  795. tmp = atoi(colon);
  796. if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) {
  797. *work_division = tmp;
  798. *fpga_count = tmp; // default to the same
  799. } else {
  800. quit(1, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
  801. }
  802. }
  803. if (colon2 && *colon2) {
  804. tmp = atoi(colon2);
  805. if (tmp > 0 && tmp <= *work_division)
  806. *fpga_count = tmp;
  807. else {
  808. quit(1, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
  809. }
  810. }
  811. }
  812. }
  813. }
  814. unsigned char crc5(unsigned char *ptr, unsigned char len)
  815. {
  816. unsigned char i, j, k;
  817. unsigned char crc = 0x1f;
  818. unsigned char crcin[5] = {1, 1, 1, 1, 1};
  819. unsigned char crcout[5] = {1, 1, 1, 1, 1};
  820. unsigned char din = 0;
  821. j = 0x80;
  822. k = 0;
  823. for (i = 0; i < len; i++) {
  824. if (*ptr & j)
  825. din = 1;
  826. else
  827. din = 0;
  828. crcout[0] = crcin[4] ^ din;
  829. crcout[1] = crcin[0];
  830. crcout[2] = crcin[1] ^ crcin[4] ^ din;
  831. crcout[3] = crcin[2];
  832. crcout[4] = crcin[3];
  833. j = j >> 1;
  834. k++;
  835. if (k == 8) {
  836. j = 0x80;
  837. k = 0;
  838. ptr++;
  839. }
  840. memcpy(crcin, crcout, 5);
  841. }
  842. crc = 0;
  843. if (crcin[4])
  844. crc |= 0x10;
  845. if (crcin[3])
  846. crc |= 0x08;
  847. if (crcin[2])
  848. crc |= 0x04;
  849. if (crcin[1])
  850. crc |= 0x02;
  851. if (crcin[0])
  852. crc |= 0x01;
  853. return crc;
  854. }
  855. static uint16_t anu_find_freqhex(void)
  856. {
  857. float fout, best_fout = opt_anu_freq;
  858. int od, nf, nr, no, n, m, bs;
  859. uint16_t anu_freq_hex = 0;
  860. float best_diff = 1000;
  861. if (!best_fout)
  862. best_fout = ANT_U1_DEFFREQ;
  863. for (od = 0; od < 4; od++) {
  864. no = 1 << od;
  865. for (n = 0; n < 16; n++) {
  866. nr = n + 1;
  867. for (m = 0; m < 64; m++) {
  868. nf = m + 1;
  869. fout = 25 * (float)nf /((float)(nr) * (float)(no));
  870. if (fabsf(fout - opt_anu_freq) > best_diff)
  871. continue;
  872. if (500 <= (fout * no) && (fout * no) <= 1000)
  873. bs = 1;
  874. else
  875. bs = 0;
  876. best_diff = fabsf(fout - opt_anu_freq);
  877. best_fout = fout;
  878. anu_freq_hex = (bs << 14) | (m << 7) | (n << 2) | od;
  879. if (fout == opt_anu_freq) {
  880. applog(LOG_DEBUG, "ANU found exact frequency %.1f with hex %04x",
  881. opt_anu_freq, anu_freq_hex);
  882. goto out;
  883. }
  884. }
  885. }
  886. }
  887. applog(LOG_NOTICE, "ANU found nearest frequency %.1f with hex %04x", best_fout,
  888. anu_freq_hex);
  889. out:
  890. return anu_freq_hex;
  891. }
  892. static uint16_t anu3_find_freqhex(void)
  893. {
  894. int i = 0, freq = opt_au3_freq, u3freq;
  895. uint16_t anu_freq_hex = 0x0882;
  896. if (!freq)
  897. freq = ANT_U3_DEFFREQ;
  898. do {
  899. u3freq = u3freqtable[i].freq;
  900. if (u3freq <= freq)
  901. anu_freq_hex = u3freqtable[i].hex;
  902. i++;
  903. } while (u3freq < ANT_U3_MAXFREQ);
  904. return anu_freq_hex;
  905. }
  906. static bool set_anu_freq(struct cgpu_info *icarus, struct ICARUS_INFO *info, uint16_t anu_freq_hex)
  907. {
  908. unsigned char cmd_buf[4], rdreg_buf[4];
  909. int amount, err;
  910. char buf[512];
  911. if (!anu_freq_hex)
  912. anu_freq_hex = anu_find_freqhex();
  913. memset(cmd_buf, 0, 4);
  914. memset(rdreg_buf, 0, 4);
  915. cmd_buf[0] = 2 | 0x80;
  916. cmd_buf[1] = (anu_freq_hex & 0xff00u) >> 8;
  917. cmd_buf[2] = (anu_freq_hex & 0x00ffu);
  918. cmd_buf[3] = crc5(cmd_buf, 27);
  919. rdreg_buf[0] = 4 | 0x80;
  920. rdreg_buf[1] = 0; //16-23
  921. rdreg_buf[2] = 0x04; //8-15
  922. rdreg_buf[3] = crc5(rdreg_buf, 27);
  923. applog(LOG_DEBUG, "%s %i: Send frequency %02x%02x%02x%02x", icarus->drv->name, icarus->device_id,
  924. cmd_buf[0], cmd_buf[1], cmd_buf[2], cmd_buf[3]);
  925. err = usb_write_ii(icarus, info->intinfo, (char *)cmd_buf, 4, &amount, C_ANU_SEND_CMD);
  926. if (err != LIBUSB_SUCCESS || amount != 4) {
  927. applog(LOG_ERR, "%s %i: Write freq Comms error (werr=%d amount=%d)",
  928. icarus->drv->name, icarus->device_id, err, amount);
  929. return false;
  930. }
  931. err = usb_read_ii_timeout(icarus, info->intinfo, buf, 512, &amount, 100, C_GETRESULTS);
  932. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
  933. applog(LOG_ERR, "%s %i: Read freq Comms error (rerr=%d amount=%d)",
  934. icarus->drv->name, icarus->device_id, err, amount);
  935. return false;
  936. }
  937. applog(LOG_DEBUG, "%s %i: Send freq getstatus %02x%02x%02x%02x", icarus->drv->name, icarus->device_id,
  938. rdreg_buf[0], rdreg_buf[1], rdreg_buf[2], rdreg_buf[3]);
  939. err = usb_write_ii(icarus, info->intinfo, (char *)cmd_buf, 4, &amount, C_ANU_SEND_RDREG);
  940. if (err != LIBUSB_SUCCESS || amount != 4) {
  941. applog(LOG_ERR, "%s %i: Write freq Comms error (werr=%d amount=%d)",
  942. icarus->drv->name, icarus->device_id, err, amount);
  943. return false;
  944. }
  945. err = usb_read_ii_timeout(icarus, info->intinfo, buf, 512, &amount, 100, C_GETRESULTS);
  946. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
  947. applog(LOG_ERR, "%s %i: Read freq Comms error (rerr=%d amount=%d)",
  948. icarus->drv->name, icarus->device_id, err, amount);
  949. return false;
  950. }
  951. return true;
  952. }
  953. static void set_anu_volt(struct cgpu_info *icarus)
  954. {
  955. unsigned char voltage_data[2], cmd_buf[4];
  956. char volt_buf[8];
  957. int err, amount;
  958. /* Allow a zero setting to imply not to try and set voltage */
  959. if (!opt_au3_volt)
  960. return;
  961. if (opt_au3_volt < 725 || opt_au3_volt > 850) {
  962. applog(LOG_WARNING, "Invalid ANU voltage %d specified, must be 725-850", opt_au3_volt);
  963. return;
  964. }
  965. sprintf(volt_buf, "%04d", opt_au3_volt);
  966. hex2bin(voltage_data, volt_buf, 2);
  967. cmd_buf[0] = 0xaa;
  968. cmd_buf[1] = voltage_data[0];
  969. cmd_buf[1] &=0x0f;
  970. cmd_buf[1] |=0xb0;
  971. cmd_buf[2] = voltage_data[1];
  972. cmd_buf[3] = 0x00; //0-7
  973. cmd_buf[3] = crc5(cmd_buf, 4*8 - 5);
  974. cmd_buf[3] |= 0xc0;
  975. applog(LOG_INFO, "Send ANU voltage %02x%02x%02x%02x", cmd_buf[0], cmd_buf[1], cmd_buf[2], cmd_buf[3]);
  976. cgsleep_ms(500);
  977. err = usb_write(icarus, (char * )cmd_buf, 4, &amount, C_ANU_SEND_VOLT);
  978. if (err != LIBUSB_SUCCESS || amount != 4)
  979. applog(LOG_ERR, "Write voltage Comms error (werr=%d amount=%d)", err, amount);
  980. }
  981. static void rock_init_last_received_task_complete_time(struct ICARUS_INFO *info)
  982. {
  983. int i;
  984. if (opt_rock_freq < info->rmdev.min_frq ||
  985. opt_rock_freq > info->rmdev.max_frq)
  986. opt_rock_freq = info->rmdev.def_frq;
  987. for (i = 0; i < MAX_CHIP_NUM; ++i) {
  988. info->rmdev.chip[i].last_received_task_complete_time = time(NULL);
  989. info->rmdev.chip[i].freq = opt_rock_freq/10 - 1;
  990. info->rmdev.chip[i].error_cnt = 0;
  991. }
  992. info->rmdev.dev_detect_time = time(NULL);
  993. }
  994. static void icarus_clear(struct cgpu_info *icarus, struct ICARUS_INFO *info)
  995. {
  996. char buf[512];
  997. int amt;
  998. do {
  999. usb_read_ii_timeout(icarus, info->intinfo, buf, 512, &amt, 100, C_GETRESULTS);
  1000. } while (amt > 0);
  1001. }
  1002. static struct cgpu_info *icarus_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  1003. {
  1004. int this_option_offset = ++option_offset;
  1005. struct ICARUS_INFO *info;
  1006. struct timeval tv_start, tv_finish;
  1007. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  1008. // N.B. golden_ob MUST take less time to calculate
  1009. // than the timeout set in icarus_open()
  1010. // This one takes ~0.53ms on Rev3 Icarus
  1011. const char golden_ob[] =
  1012. "4679ba4ec99876bf4bfe086082b40025"
  1013. "4df6c356451471139a3afa71e48f544a"
  1014. "00000000000000000000000000000000"
  1015. "0000000087320b1a1426674f2fa722ce";
  1016. const char golden_nonce[] = "000187a2";
  1017. const uint32_t golden_nonce_val = 0x000187a2;
  1018. unsigned char nonce_bin[ICARUS_READ_SIZE];
  1019. struct ICARUS_WORK workdata;
  1020. char *nonce_hex;
  1021. int baud, uninitialised_var(work_division), uninitialised_var(fpga_count);
  1022. bool anu_freqset = false;
  1023. struct cgpu_info *icarus;
  1024. int ret, err, amount, tries, i;
  1025. bool ok;
  1026. bool cmr2_ok[CAIRNSMORE2_INTS];
  1027. int cmr2_count;
  1028. if ((sizeof(workdata) << 1) != (sizeof(golden_ob) - 1))
  1029. quithere(1, "Data and golden_ob sizes don't match");
  1030. icarus = usb_alloc_cgpu(&icarus_drv, 1);
  1031. if (!usb_init(icarus, dev, found))
  1032. goto shin;
  1033. get_options(this_option_offset, icarus, &baud, &work_division, &fpga_count);
  1034. hex2bin((void *)(&workdata), golden_ob, sizeof(workdata));
  1035. info = (struct ICARUS_INFO *)calloc(1, sizeof(struct ICARUS_INFO));
  1036. if (unlikely(!info))
  1037. quit(1, "Failed to malloc ICARUS_INFO");
  1038. icarus->device_data = (void *)info;
  1039. info->ident = usb_ident(icarus);
  1040. switch (info->ident) {
  1041. case IDENT_ICA:
  1042. case IDENT_AVA:
  1043. case IDENT_BLT:
  1044. case IDENT_LLT:
  1045. case IDENT_AMU:
  1046. case IDENT_CMR1:
  1047. info->timeout = ICARUS_WAIT_TIMEOUT;
  1048. break;
  1049. case IDENT_ANU:
  1050. case IDENT_AU3:
  1051. info->timeout = ANT_WAIT_TIMEOUT;
  1052. break;
  1053. case IDENT_CMR2:
  1054. if (found->intinfo_count != CAIRNSMORE2_INTS) {
  1055. quithere(1, "CMR2 Interface count (%d) isn't expected: %d",
  1056. found->intinfo_count,
  1057. CAIRNSMORE2_INTS);
  1058. }
  1059. info->timeout = ICARUS_CMR2_TIMEOUT;
  1060. cmr2_count = 0;
  1061. for (i = 0; i < CAIRNSMORE2_INTS; i++)
  1062. cmr2_ok[i] = false;
  1063. break;
  1064. default:
  1065. quit(1, "%s icarus_detect_one() invalid %s ident=%d",
  1066. icarus->drv->dname, icarus->drv->dname, info->ident);
  1067. }
  1068. info->nonce_size = ICARUS_READ_SIZE;
  1069. // For CMR2 test each USB Interface
  1070. retry:
  1071. tries = 2;
  1072. ok = false;
  1073. while (!ok && tries-- > 0) {
  1074. icarus_clear(icarus, info);
  1075. icarus_initialise(icarus, baud);
  1076. if (info->u3) {
  1077. uint16_t anu_freq_hex = anu3_find_freqhex();
  1078. set_anu_volt(icarus);
  1079. if (!set_anu_freq(icarus, info, anu_freq_hex)) {
  1080. applog(LOG_WARNING, "%s %i: Failed to set frequency, too much overclock?",
  1081. icarus->drv->name, icarus->device_id);
  1082. continue;
  1083. }
  1084. icarus->usbdev->ident = info->ident = IDENT_AU3;
  1085. info->Hs = ANTU3_HASH_TIME;
  1086. icarus->drv->name = "AU3";
  1087. applog(LOG_DEBUG, "%s %i: Detected Antminer U3", icarus->drv->name,
  1088. icarus->device_id);
  1089. } else if (info->ident == IDENT_ANU && !info->u3) {
  1090. if (!set_anu_freq(icarus, info, 0)) {
  1091. applog(LOG_WARNING, "%s %i: Failed to set frequency, too much overclock?",
  1092. icarus->drv->name, icarus->device_id);
  1093. continue;
  1094. }
  1095. }
  1096. err = usb_write_ii(icarus, info->intinfo,
  1097. (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  1098. if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
  1099. continue;
  1100. memset(nonce_bin, 0, sizeof(nonce_bin));
  1101. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 300);
  1102. if (ret != ICA_NONCE_OK)
  1103. continue;
  1104. if (info->nonce_size == ICARUS_READ_SIZE && usb_buffer_size(icarus) == 4) {
  1105. applog(LOG_DEBUG, "%s %i: Detected Rockminer, deferring detection",
  1106. icarus->drv->name, icarus->device_id);
  1107. usb_buffer_clear(icarus);
  1108. break;
  1109. }
  1110. if (info->nonce_size == ICARUS_READ_SIZE && usb_buffer_size(icarus) == 1) {
  1111. info->ant = true;
  1112. usb_buffer_clear(icarus);
  1113. icarus->usbdev->ident = info->ident = IDENT_ANU;
  1114. info->nonce_size = ANT_READ_SIZE;
  1115. info->Hs = ANTMINERUSB_HASH_TIME;
  1116. icarus->drv->name = "ANU";
  1117. applog(LOG_DEBUG, "%s %i: Detected Antminer U1/2/3, changing nonce size to %d",
  1118. icarus->drv->name, icarus->device_id, ANT_READ_SIZE);
  1119. }
  1120. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  1121. if (strncmp(nonce_hex, golden_nonce, 8) == 0) {
  1122. if (info->ant && !anu_freqset)
  1123. anu_freqset = true;
  1124. else
  1125. ok = true;
  1126. } else {
  1127. if (tries < 0 && info->ident != IDENT_CMR2) {
  1128. applog(LOG_ERR,
  1129. "Icarus Detect: "
  1130. "Test failed at %s: get %s, should: %s",
  1131. icarus->device_path, nonce_hex, golden_nonce);
  1132. }
  1133. }
  1134. free(nonce_hex);
  1135. }
  1136. if (!ok) {
  1137. if (info->ident != IDENT_CMR2) {
  1138. if (info->u3)
  1139. goto unshin;
  1140. info->u3 = true;
  1141. goto retry;
  1142. }
  1143. if (info->intinfo < CAIRNSMORE2_INTS-1) {
  1144. info->intinfo++;
  1145. goto retry;
  1146. }
  1147. } else {
  1148. if (info->ident == IDENT_CMR2) {
  1149. applog(LOG_DEBUG,
  1150. "Icarus Detect: "
  1151. "Test succeeded at %s i%d: got %s",
  1152. icarus->device_path, info->intinfo, golden_nonce);
  1153. cmr2_ok[info->intinfo] = true;
  1154. cmr2_count++;
  1155. if (info->intinfo < CAIRNSMORE2_INTS-1) {
  1156. info->intinfo++;
  1157. goto retry;
  1158. }
  1159. }
  1160. }
  1161. if (info->ident == IDENT_CMR2) {
  1162. if (cmr2_count == 0) {
  1163. applog(LOG_ERR,
  1164. "Icarus Detect: Test failed at %s: for all %d CMR2 Interfaces",
  1165. icarus->device_path, CAIRNSMORE2_INTS);
  1166. goto unshin;
  1167. }
  1168. // set the interface to the first one that succeeded
  1169. for (i = 0; i < CAIRNSMORE2_INTS; i++)
  1170. if (cmr2_ok[i]) {
  1171. info->intinfo = i;
  1172. break;
  1173. }
  1174. } else {
  1175. applog(LOG_DEBUG,
  1176. "Icarus Detect: "
  1177. "Test succeeded at %s: got %s",
  1178. icarus->device_path, golden_nonce);
  1179. }
  1180. /* We have a real Icarus! */
  1181. if (!add_cgpu(icarus))
  1182. goto unshin;
  1183. update_usb_stats(icarus);
  1184. applog(LOG_INFO, "%s %d: Found at %s",
  1185. icarus->drv->name, icarus->device_id, icarus->device_path);
  1186. if (info->ident == IDENT_CMR2) {
  1187. applog(LOG_INFO, "%s %d: with %d Interface%s",
  1188. icarus->drv->name, icarus->device_id,
  1189. cmr2_count, cmr2_count > 1 ? "s" : "");
  1190. // Assume 1 or 2 are running FPGA pairs
  1191. if (cmr2_count < 3) {
  1192. work_division = fpga_count = 2;
  1193. info->Hs /= 2;
  1194. }
  1195. }
  1196. applog(LOG_DEBUG, "%s %d: Init baud=%d work_division=%d fpga_count=%d",
  1197. icarus->drv->name, icarus->device_id, baud, work_division, fpga_count);
  1198. info->baud = baud;
  1199. info->work_division = work_division;
  1200. info->fpga_count = fpga_count;
  1201. info->nonce_mask = mask(work_division);
  1202. info->golden_hashes = (golden_nonce_val & info->nonce_mask) * fpga_count;
  1203. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  1204. set_timing_mode(this_option_offset, icarus);
  1205. if (info->ident == IDENT_CMR2) {
  1206. int i;
  1207. for (i = info->intinfo + 1; i < icarus->usbdev->found->intinfo_count; i++) {
  1208. struct cgpu_info *cgtmp;
  1209. struct ICARUS_INFO *intmp;
  1210. if (!cmr2_ok[i])
  1211. continue;
  1212. cgtmp = usb_copy_cgpu(icarus);
  1213. if (!cgtmp) {
  1214. applog(LOG_ERR, "%s %d: Init failed initinfo %d",
  1215. icarus->drv->name, icarus->device_id, i);
  1216. continue;
  1217. }
  1218. cgtmp->usbinfo.usbstat = USB_NOSTAT;
  1219. intmp = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  1220. if (unlikely(!intmp))
  1221. quit(1, "Failed2 to malloc ICARUS_INFO");
  1222. cgtmp->device_data = (void *)intmp;
  1223. // Initialise everything to match
  1224. memcpy(intmp, info, sizeof(struct ICARUS_INFO));
  1225. intmp->intinfo = i;
  1226. icarus_initialise(cgtmp, baud);
  1227. if (!add_cgpu(cgtmp)) {
  1228. usb_uninit(cgtmp);
  1229. free(intmp);
  1230. continue;
  1231. }
  1232. update_usb_stats(cgtmp);
  1233. }
  1234. }
  1235. return icarus;
  1236. unshin:
  1237. usb_uninit(icarus);
  1238. free(info);
  1239. icarus->device_data = NULL;
  1240. shin:
  1241. icarus = usb_free_cgpu(icarus);
  1242. return NULL;
  1243. }
  1244. static int64_t rock_scanwork(struct thr_info *thr);
  1245. static void rock_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu)
  1246. {
  1247. if (cgpu->temp)
  1248. tailsprintf(buf, bufsiz, "%3.0fMHz %3.0fC", opt_rock_freq, cgpu->temp);
  1249. else
  1250. tailsprintf(buf, bufsiz, "%.0fMHz", opt_rock_freq);
  1251. }
  1252. /* The only thing to do on flush_work is to remove the base work to prevent us
  1253. * rolling what is now stale work */
  1254. static void rock_flush(struct cgpu_info *icarus)
  1255. {
  1256. struct ICARUS_INFO *info = icarus->device_data;
  1257. struct work *work;
  1258. mutex_lock(&info->lock);
  1259. work = info->base_work;
  1260. info->base_work = NULL;
  1261. mutex_unlock(&info->lock);
  1262. if (work)
  1263. free_work(work);
  1264. }
  1265. static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  1266. {
  1267. struct ICARUS_INFO *info;
  1268. struct timeval tv_start, tv_finish;
  1269. char *ob_hex = NULL;
  1270. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  1271. // N.B. golden_ob MUST take less time to calculate
  1272. // than the timeout set in icarus_open()
  1273. // This one takes ~0.53ms on Rev3 Icarus
  1274. const char golden_ob[] =
  1275. "4679ba4ec99876bf4bfe086082b40025"
  1276. "4df6c356451471139a3afa71e48f544a"
  1277. "00000000000000000000000000000000"
  1278. "aa1ff05587320b1a1426674f2fa722ce";
  1279. const char golden_nonce[] = "000187a2";
  1280. const uint32_t golden_nonce_val = 0x000187a2;
  1281. unsigned char nonce_bin[ROCK_READ_SIZE];
  1282. struct ICARUS_WORK workdata;
  1283. char *nonce_hex;
  1284. struct cgpu_info *icarus;
  1285. int ret, err, amount, tries;
  1286. bool ok;
  1287. int correction_times = 0;
  1288. NONCE_DATA nonce_data;
  1289. uint32_t nonce;
  1290. char *newname = NULL;
  1291. if ((sizeof(workdata) << 1) != (sizeof(golden_ob) - 1))
  1292. quithere(1, "Data and golden_ob sizes don't match");
  1293. icarus = usb_alloc_cgpu(&icarus_drv, 1);
  1294. if (!usb_init(icarus, dev, found))
  1295. goto shin;
  1296. hex2bin((void *)(&workdata), golden_ob, sizeof(workdata));
  1297. rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE);
  1298. rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE);
  1299. if (opt_debug) {
  1300. ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
  1301. applog(LOG_WARNING, "%s %d: send_gold_nonce %s",
  1302. icarus->drv->name, icarus->device_id, ob_hex);
  1303. free(ob_hex);
  1304. }
  1305. info = (struct ICARUS_INFO *)calloc(1, sizeof(struct ICARUS_INFO));
  1306. if (unlikely(!info))
  1307. quit(1, "Failed to malloc ICARUS_INFO");
  1308. (void)memset(info, 0, sizeof(struct ICARUS_INFO));
  1309. icarus->device_data = (void *)info;
  1310. icarus->usbdev->ident = info->ident = IDENT_LIN;
  1311. info->nonce_size = ROCK_READ_SIZE;
  1312. info->fail_time = 10;
  1313. info->nonce_mask = 0xffffffff;
  1314. update_usb_stats(icarus);
  1315. tries = MAX_TRIES;
  1316. ok = false;
  1317. while (!ok && tries-- > 0) {
  1318. icarus_initialise(icarus, info->baud);
  1319. applog(LOG_DEBUG, "tries: %d", tries);
  1320. workdata.unused[ICARUS_UNUSED_SIZE - 3] = opt_rock_freq/10 - 1;
  1321. workdata.unused[ICARUS_UNUSED_SIZE - 2] = (MAX_TRIES-1-tries);
  1322. info->rmdev.detect_chip_no++;
  1323. if (info->rmdev.detect_chip_no >= MAX_TRIES)
  1324. info->rmdev.detect_chip_no = 0;
  1325. //g_detect_chip_no = (g_detect_chip_no + 1) & MAX_CHIP_NUM;
  1326. usb_buffer_clear(icarus);
  1327. err = usb_write_ii(icarus, info->intinfo,
  1328. (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  1329. if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
  1330. continue;
  1331. memset(nonce_bin, 0, sizeof(nonce_bin));
  1332. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 100);
  1333. applog(LOG_DEBUG, "Rockminer nonce_bin: %02x %02x %02x %02x %02x %02x %02x %02x",
  1334. nonce_bin[0], nonce_bin[1], nonce_bin[2], nonce_bin[3],
  1335. nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]);
  1336. if (ret != ICA_NONCE_OK) {
  1337. applog(LOG_DEBUG, "detect_one get_gold_nonce error, tries = %d", tries);
  1338. continue;
  1339. }
  1340. if (usb_buffer_size(icarus) == 1) {
  1341. applog(LOG_INFO, "Rock detect found an ANU, skipping");
  1342. usb_buffer_clear(icarus);
  1343. break;
  1344. }
  1345. newname = NULL;
  1346. switch (nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_PRODUCT_MASK) {
  1347. case RM_PRODUCT_T1:
  1348. newname = "LIR"; // Rocketbox
  1349. info->rmdev.product_id = ROCKMINER_T1;
  1350. info->rmdev.chip_max = 12;
  1351. info->rmdev.min_frq = 200;
  1352. info->rmdev.def_frq = 330;
  1353. info->rmdev.max_frq = 400;
  1354. break;
  1355. case RM_PRODUCT_T2: // what's this?
  1356. newname = "LIX";
  1357. info->rmdev.product_id = ROCKMINER_T2;
  1358. info->rmdev.chip_max = 16;
  1359. info->rmdev.min_frq = 200;
  1360. info->rmdev.def_frq = 300;
  1361. info->rmdev.max_frq = 400;
  1362. break;
  1363. case RM_PRODUCT_RBOX:
  1364. newname = "LIN"; // R-Box
  1365. info->rmdev.product_id = ROCKMINER_RBOX;
  1366. info->rmdev.chip_max = 4;
  1367. info->rmdev.min_frq = 200;
  1368. info->rmdev.def_frq = 270;
  1369. info->rmdev.max_frq = 400;
  1370. break;
  1371. default:
  1372. continue;
  1373. }
  1374. snprintf(info->rock_init, sizeof(info->rock_init), "%02x %02x %02x %02x",
  1375. nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]);
  1376. nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_CHIP_MASK;
  1377. if (nonce_data.chip_no >= info->rmdev.chip_max)
  1378. nonce_data.chip_no = 0;
  1379. nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_CMD_MASK;
  1380. if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD) {
  1381. applog(LOG_DEBUG, "complete g_detect_chip_no: %d", info->rmdev.detect_chip_no);
  1382. workdata.unused[ICARUS_UNUSED_SIZE - 3] = opt_rock_freq/10 - 1;
  1383. workdata.unused[ICARUS_UNUSED_SIZE - 2] = info->rmdev.detect_chip_no;
  1384. info->rmdev.detect_chip_no++;
  1385. if (info->rmdev.detect_chip_no >= MAX_TRIES)
  1386. info->rmdev.detect_chip_no = 0;
  1387. err = usb_write_ii(icarus, info->intinfo,
  1388. (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  1389. if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
  1390. continue;
  1391. applog(LOG_DEBUG, "send_gold_nonce usb_write_ii");
  1392. continue;
  1393. }
  1394. memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE);
  1395. nonce = htobe32(nonce);
  1396. applog(LOG_DEBUG, "Rockminer nonce: %08X", nonce);
  1397. correction_times = 0;
  1398. while (correction_times < NONCE_CORRECTION_TIMES) {
  1399. nonce_hex = bin2hex(nonce_bin, 4);
  1400. if (golden_nonce_val == nonce + rbox_corr_values[correction_times]) {
  1401. memset(&(info->g_work[0]), 0, sizeof(info->g_work));
  1402. rock_init_last_received_task_complete_time(info);
  1403. ok = true;
  1404. break;
  1405. } else {
  1406. applog(LOG_DEBUG, "detect_one gold_nonce compare error times = %d",
  1407. correction_times);
  1408. if (tries < 0 && info->ident != IDENT_CMR2) {
  1409. applog(LOG_WARNING,
  1410. "Icarus Detect: "
  1411. "Test failed at %s: get %s, should: %s",
  1412. icarus->device_path, nonce_hex, golden_nonce);
  1413. }
  1414. if (nonce == 0)
  1415. break;
  1416. }
  1417. free(nonce_hex);
  1418. correction_times++;
  1419. }
  1420. }
  1421. if (!ok)
  1422. goto unshin;
  1423. if (newname) {
  1424. if (!icarus->drv->copy)
  1425. icarus->drv = copy_drv(icarus->drv);
  1426. icarus->drv->name = newname;
  1427. }
  1428. applog(LOG_DEBUG, "Icarus Detect: Test succeeded at %s: got %s",
  1429. icarus->device_path, golden_nonce);
  1430. /* We have a real Rockminer! */
  1431. if (!add_cgpu(icarus))
  1432. goto unshin;
  1433. icarus->drv->scanwork = rock_scanwork;
  1434. icarus->drv->dname = "Rockminer";
  1435. icarus->drv->get_statline_before = &rock_statline_before;
  1436. icarus->drv->flush_work = &rock_flush;
  1437. mutex_init(&info->lock);
  1438. applog(LOG_INFO, "%s %d: Found at %s",
  1439. icarus->drv->name, icarus->device_id,
  1440. icarus->device_path);
  1441. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  1442. return icarus;
  1443. unshin:
  1444. usb_uninit(icarus);
  1445. free(info);
  1446. icarus->device_data = NULL;
  1447. shin:
  1448. icarus = usb_free_cgpu(icarus);
  1449. return NULL;
  1450. }
  1451. static void icarus_detect(bool __maybe_unused hotplug)
  1452. {
  1453. usb_detect(&icarus_drv, rock_detect_one);
  1454. usb_detect(&icarus_drv, icarus_detect_one);
  1455. }
  1456. static bool icarus_prepare(struct thr_info *thr)
  1457. {
  1458. struct cgpu_info *icarus = thr->cgpu;
  1459. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  1460. if (info->ant)
  1461. info->antworks = calloc(sizeof(struct work *), ANT_QUEUE_NUM);
  1462. return true;
  1463. }
  1464. static void cmr2_command(struct cgpu_info *icarus, uint8_t cmd, uint8_t data)
  1465. {
  1466. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  1467. struct ICARUS_WORK workdata;
  1468. int amount;
  1469. memset((void *)(&workdata), 0, sizeof(workdata));
  1470. workdata.prefix = ICARUS_CMR2_PREFIX;
  1471. workdata.cmd = cmd;
  1472. workdata.data = data;
  1473. workdata.check = workdata.data ^ workdata.cmd ^ workdata.prefix ^ ICARUS_CMR2_CHECK;
  1474. usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  1475. }
  1476. static void cmr2_commands(struct cgpu_info *icarus)
  1477. {
  1478. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  1479. if (info->speed_next_work) {
  1480. info->speed_next_work = false;
  1481. cmr2_command(icarus, ICARUS_CMR2_CMD_SPEED, info->cmr2_speed);
  1482. return;
  1483. }
  1484. if (info->flash_next_work) {
  1485. info->flash_next_work = false;
  1486. cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_ON);
  1487. cgsleep_ms(250);
  1488. cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_OFF);
  1489. cgsleep_ms(250);
  1490. cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_ON);
  1491. cgsleep_ms(250);
  1492. cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_OFF);
  1493. return;
  1494. }
  1495. }
  1496. void rock_send_task(unsigned char chip_no, unsigned int current_task_id, struct thr_info *thr)
  1497. {
  1498. struct cgpu_info *icarus = thr->cgpu;
  1499. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  1500. int err, amount;
  1501. struct ICARUS_WORK workdata;
  1502. char *ob_hex;
  1503. struct work *work = NULL;
  1504. /* Only base_work needs locking since it can be asynchronously deleted
  1505. * by flush work */
  1506. if (info->g_work[chip_no][current_task_id] == NULL) {
  1507. mutex_lock(&info->lock);
  1508. if (!info->base_work)
  1509. info->base_work = get_work(thr, thr->id);
  1510. if (info->base_work->drv_rolllimit > 0) {
  1511. info->base_work->drv_rolllimit--;
  1512. roll_work(info->base_work);
  1513. work = make_clone(info->base_work);
  1514. } else {
  1515. work = info->base_work;
  1516. info->base_work = NULL;
  1517. }
  1518. mutex_unlock(&info->lock);
  1519. info->g_work[chip_no][current_task_id] = work;
  1520. } else {
  1521. work = info->g_work[chip_no][current_task_id];
  1522. applog(LOG_DEBUG, "::resend work");
  1523. }
  1524. memset((void *)(&workdata), 0, sizeof(workdata));
  1525. memcpy(&(workdata.midstate), work->midstate, ICARUS_MIDSTATE_SIZE);
  1526. memcpy(&(workdata.work), work->data + ICARUS_WORK_DATA_OFFSET, ICARUS_WORK_SIZE);
  1527. workdata.unused[ICARUS_UNUSED_SIZE - 4] = 0xaa;
  1528. if (info->rmdev.chip[chip_no].freq > (info->rmdev.max_frq/10 - 1) ||
  1529. info->rmdev.chip[chip_no].freq < (info->rmdev.min_frq/10 - 1))
  1530. rock_init_last_received_task_complete_time(info);
  1531. workdata.unused[ICARUS_UNUSED_SIZE - 3] = info->rmdev.chip[chip_no].freq; //icarus->freq/10 - 1; ;
  1532. workdata.unused[ICARUS_UNUSED_SIZE - 2] = chip_no ;
  1533. workdata.id = 0x55;
  1534. if (opt_debug) {
  1535. ob_hex = bin2hex((void *)(work->data), 128);
  1536. applog(LOG_WARNING, "%s %d: work->data %s",
  1537. icarus->drv->name, icarus->device_id, ob_hex);
  1538. free(ob_hex);
  1539. }
  1540. // We only want results for the work we are about to send
  1541. usb_buffer_clear(icarus);
  1542. err = usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  1543. if (err < 0 || amount != sizeof(workdata)) {
  1544. applog(LOG_ERR, "%s %i: Comms error (werr=%d amt=%d)",
  1545. icarus->drv->name, icarus->device_id, err, amount);
  1546. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  1547. icarus_initialise(icarus, info->baud);
  1548. if (info->g_work[chip_no][current_task_id])
  1549. {
  1550. free_work(info->g_work[chip_no][current_task_id]);
  1551. info->g_work[chip_no][current_task_id] = NULL;
  1552. }
  1553. return;
  1554. }
  1555. return;
  1556. }
  1557. static void process_history(struct cgpu_info *icarus, struct ICARUS_INFO *info, uint32_t nonce,
  1558. uint64_t hash_count, struct timeval *elapsed, struct timeval *tv_start)
  1559. {
  1560. struct ICARUS_HISTORY *history0, *history;
  1561. struct timeval tv_history_start, tv_history_finish;
  1562. int count;
  1563. double Hs, W, fullnonce;
  1564. int read_time, i;
  1565. bool limited;
  1566. uint32_t values;
  1567. int64_t hash_count_range;
  1568. double Ti, Xi;
  1569. // Ignore possible end condition values ...
  1570. // TODO: set limitations on calculated values depending on the device
  1571. // to avoid crap values caused by CPU/Task Switching/Swapping/etc
  1572. if ((nonce & info->nonce_mask) <= END_CONDITION ||
  1573. (nonce & info->nonce_mask) >= (info->nonce_mask & ~END_CONDITION))
  1574. return;
  1575. cgtime(&tv_history_start);
  1576. history0 = &(info->history[0]);
  1577. if (history0->values == 0)
  1578. timeradd(tv_start, &history_sec, &(history0->finish));
  1579. Ti = (double)(elapsed->tv_sec)
  1580. + ((double)(elapsed->tv_usec))/((double)1000000)
  1581. - ((double)ICARUS_READ_TIME(info->baud));
  1582. Xi = (double)hash_count;
  1583. history0->sumXiTi += Xi * Ti;
  1584. history0->sumXi += Xi;
  1585. history0->sumTi += Ti;
  1586. history0->sumXi2 += Xi * Xi;
  1587. history0->values++;
  1588. if (history0->hash_count_max < hash_count)
  1589. history0->hash_count_max = hash_count;
  1590. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  1591. history0->hash_count_min = hash_count;
  1592. if (history0->values >= info->min_data_count
  1593. && timercmp(tv_start, &(history0->finish), >)) {
  1594. for (i = INFO_HISTORY; i > 0; i--)
  1595. memcpy(&(info->history[i]),
  1596. &(info->history[i-1]),
  1597. sizeof(struct ICARUS_HISTORY));
  1598. // Initialise history0 to zero for summary calculation
  1599. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  1600. // We just completed a history data set
  1601. // So now recalc read_time based on the whole history thus we will
  1602. // initially get more accurate until it completes INFO_HISTORY
  1603. // total data sets
  1604. count = 0;
  1605. for (i = 1 ; i <= INFO_HISTORY; i++) {
  1606. history = &(info->history[i]);
  1607. if (history->values >= MIN_DATA_COUNT) {
  1608. count++;
  1609. history0->sumXiTi += history->sumXiTi;
  1610. history0->sumXi += history->sumXi;
  1611. history0->sumTi += history->sumTi;
  1612. history0->sumXi2 += history->sumXi2;
  1613. history0->values += history->values;
  1614. if (history0->hash_count_max < history->hash_count_max)
  1615. history0->hash_count_max = history->hash_count_max;
  1616. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  1617. history0->hash_count_min = history->hash_count_min;
  1618. }
  1619. }
  1620. // All history data
  1621. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  1622. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  1623. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  1624. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  1625. values = history0->values;
  1626. // Initialise history0 to zero for next data set
  1627. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  1628. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  1629. read_time = SECTOMS(fullnonce) - ICARUS_READ_REDUCE;
  1630. if (info->read_time_limit > 0 && read_time > info->read_time_limit) {
  1631. read_time = info->read_time_limit;
  1632. limited = true;
  1633. } else
  1634. limited = false;
  1635. info->Hs = Hs;
  1636. info->read_time = read_time;
  1637. info->fullnonce = fullnonce;
  1638. info->count = count;
  1639. info->W = W;
  1640. info->values = values;
  1641. info->hash_count_range = hash_count_range;
  1642. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  1643. info->min_data_count *= 2;
  1644. else if (info->timing_mode == MODE_SHORT)
  1645. info->do_icarus_timing = false;
  1646. applog(LOG_WARNING, "%s %d Re-estimate: Hs=%e W=%e read_time=%dms%s fullnonce=%.3fs",
  1647. icarus->drv->name, icarus->device_id, Hs, W, read_time,
  1648. limited ? " (limited)" : "", fullnonce);
  1649. }
  1650. info->history_count++;
  1651. cgtime(&tv_history_finish);
  1652. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  1653. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  1654. }
  1655. static int64_t icarus_scanwork(struct thr_info *thr)
  1656. {
  1657. struct cgpu_info *icarus = thr->cgpu;
  1658. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  1659. int ret, err, amount;
  1660. unsigned char nonce_bin[ICARUS_BUF_SIZE];
  1661. struct ICARUS_WORK workdata;
  1662. char *ob_hex;
  1663. uint32_t nonce;
  1664. int64_t hash_count = 0;
  1665. struct timeval tv_start, tv_finish, elapsed;
  1666. int curr_hw_errors;
  1667. bool was_hw_error;
  1668. struct work *work;
  1669. int64_t estimate_hashes;
  1670. uint8_t workid = 0;
  1671. if (unlikely(share_work_tdiff(icarus) > info->fail_time)) {
  1672. if (info->failing) {
  1673. if (share_work_tdiff(icarus) > info->fail_time + 60) {
  1674. applog(LOG_ERR, "%s %d: Device failed to respond to restart",
  1675. icarus->drv->name, icarus->device_id);
  1676. usb_nodev(icarus);
  1677. return -1;
  1678. }
  1679. } else {
  1680. applog(LOG_WARNING, "%s %d: No valid hashes for over %d secs, attempting to reset",
  1681. icarus->drv->name, icarus->device_id, info->fail_time);
  1682. usb_reset(icarus);
  1683. info->failing = true;
  1684. }
  1685. }
  1686. // Device is gone
  1687. if (icarus->usbinfo.nodev)
  1688. return -1;
  1689. elapsed.tv_sec = elapsed.tv_usec = 0;
  1690. work = get_work(thr, thr->id);
  1691. memset((void *)(&workdata), 0, sizeof(workdata));
  1692. memcpy(&(workdata.midstate), work->midstate, ICARUS_MIDSTATE_SIZE);
  1693. memcpy(&(workdata.work), work->data + ICARUS_WORK_DATA_OFFSET, ICARUS_WORK_SIZE);
  1694. rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE);
  1695. rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE);
  1696. if (info->ant) {
  1697. workid = info->workid;
  1698. if (++info->workid >= 0x1F)
  1699. info->workid = 0;
  1700. if (info->antworks[workid])
  1701. free_work(info->antworks[workid]);
  1702. info->antworks[workid] = work;
  1703. workdata.id = workid;
  1704. }
  1705. if (info->speed_next_work || info->flash_next_work)
  1706. cmr2_commands(icarus);
  1707. // We only want results for the work we are about to send
  1708. usb_buffer_clear(icarus);
  1709. err = usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  1710. if (err < 0 || amount != sizeof(workdata)) {
  1711. applog(LOG_ERR, "%s %i: Comms error (werr=%d amt=%d)",
  1712. icarus->drv->name, icarus->device_id, err, amount);
  1713. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  1714. icarus_initialise(icarus, info->baud);
  1715. goto out;
  1716. }
  1717. if (opt_debug) {
  1718. ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
  1719. applog(LOG_DEBUG, "%s %d: sent %s",
  1720. icarus->drv->name, icarus->device_id, ob_hex);
  1721. free(ob_hex);
  1722. }
  1723. more_nonces:
  1724. /* Icarus will return nonces or nothing. If we know we have enough data
  1725. * for a response in the buffer already, there will be no usb read
  1726. * performed. */
  1727. memset(nonce_bin, 0, sizeof(nonce_bin));
  1728. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, info->read_time);
  1729. if (ret == ICA_NONCE_ERROR)
  1730. goto out;
  1731. // aborted before becoming idle, get new work
  1732. if (ret == ICA_NONCE_TIMEOUT || ret == ICA_NONCE_RESTART) {
  1733. if (info->ant)
  1734. goto out;
  1735. timersub(&tv_finish, &tv_start, &elapsed);
  1736. // ONLY up to just when it aborted
  1737. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  1738. estimate_hashes = ((double)(elapsed.tv_sec)
  1739. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  1740. // If some Serial-USB delay allowed the full nonce range to
  1741. // complete it can't have done more than a full nonce
  1742. if (unlikely(estimate_hashes > 0xffffffff))
  1743. estimate_hashes = 0xffffffff;
  1744. applog(LOG_DEBUG, "%s %d: no nonce = 0x%08lX hashes (%ld.%06lds)",
  1745. icarus->drv->name, icarus->device_id,
  1746. (long unsigned int)estimate_hashes,
  1747. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  1748. hash_count = estimate_hashes;
  1749. goto out;
  1750. }
  1751. if (info->ant) {
  1752. workid = nonce_bin[4] & 0x1F;
  1753. if (info->antworks[workid])
  1754. work = info->antworks[workid];
  1755. else
  1756. goto out;
  1757. }
  1758. memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE);
  1759. nonce = htobe32(nonce);
  1760. curr_hw_errors = icarus->hw_errors;
  1761. if (submit_nonce(thr, work, nonce))
  1762. info->failing = false;
  1763. was_hw_error = (curr_hw_errors < icarus->hw_errors);
  1764. /* U3s return shares fast enough to use just that for hashrate
  1765. * calculation, otherwise the result is inaccurate instead. */
  1766. if (info->ant) {
  1767. info->nonces++;
  1768. if (usb_buffer_size(icarus) >= ANT_READ_SIZE)
  1769. goto more_nonces;
  1770. } else {
  1771. hash_count = (nonce & info->nonce_mask);
  1772. hash_count++;
  1773. hash_count *= info->fpga_count;
  1774. }
  1775. #if 0
  1776. // This appears to only return zero nonce values
  1777. if (usb_buffer_size(icarus) > 3) {
  1778. memcpy((char *)&nonce, icarus->usbdev->buffer, sizeof(nonce_bin));
  1779. nonce = htobe32(nonce);
  1780. applog(LOG_WARNING, "%s %d: attempting to submit 2nd nonce = 0x%08lX",
  1781. icarus->drv->name, icarus->device_id,
  1782. (long unsigned int)nonce);
  1783. curr_hw_errors = icarus->hw_errors;
  1784. submit_nonce(thr, work, nonce);
  1785. was_hw_error = (curr_hw_errors > icarus->hw_errors);
  1786. }
  1787. #endif
  1788. if (opt_debug || info->do_icarus_timing)
  1789. timersub(&tv_finish, &tv_start, &elapsed);
  1790. applog(LOG_DEBUG, "%s %d: nonce = 0x%08x = 0x%08lX hashes (%ld.%06lds)",
  1791. icarus->drv->name, icarus->device_id,
  1792. nonce, (long unsigned int)hash_count,
  1793. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  1794. if (info->do_icarus_timing && !was_hw_error)
  1795. process_history(icarus, info, nonce, hash_count, &elapsed, &tv_start);
  1796. out:
  1797. if (!info->ant)
  1798. free_work(work);
  1799. else {
  1800. /* Ant USBs free the work themselves. Return only one full
  1801. * nonce worth on each pass to smooth out displayed hashrate */
  1802. if (info->nonces) {
  1803. hash_count = 0xffffffff;
  1804. info->nonces--;
  1805. }
  1806. }
  1807. return hash_count;
  1808. }
  1809. static int64_t rock_scanwork(struct thr_info *thr)
  1810. {
  1811. struct cgpu_info *icarus = thr->cgpu;
  1812. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  1813. int ret;
  1814. unsigned char nonce_bin[ICARUS_BUF_SIZE];
  1815. uint32_t nonce;
  1816. int64_t hash_count = 0;
  1817. struct timeval tv_start, tv_finish, elapsed;
  1818. struct work *work = NULL;
  1819. int64_t estimate_hashes;
  1820. int correction_times = 0;
  1821. NONCE_DATA nonce_data;
  1822. double temp;
  1823. int chip_no = 0;
  1824. time_t recv_time = 0;
  1825. if (unlikely(share_work_tdiff(icarus) > info->fail_time)) {
  1826. if (info->failing) {
  1827. if (share_work_tdiff(icarus) > info->fail_time + 60) {
  1828. applog(LOG_ERR, "%s %d: Device failed to respond to restart",
  1829. icarus->drv->name, icarus->device_id);
  1830. usb_nodev(icarus);
  1831. return -1;
  1832. }
  1833. } else {
  1834. applog(LOG_WARNING, "%s %d: No valid hashes for over %d secs, attempting to reset",
  1835. icarus->drv->name, icarus->device_id, info->fail_time);
  1836. usb_reset(icarus);
  1837. info->failing = true;
  1838. }
  1839. }
  1840. // Device is gone
  1841. if (icarus->usbinfo.nodev)
  1842. return -1;
  1843. elapsed.tv_sec = elapsed.tv_usec = 0;
  1844. for (chip_no = 0; chip_no < info->rmdev.chip_max; chip_no++) {
  1845. recv_time = time(NULL);
  1846. if (recv_time > info->rmdev.chip[chip_no].last_received_task_complete_time + 1) {
  1847. info->rmdev.chip[chip_no].last_received_task_complete_time = recv_time;
  1848. rock_send_task(chip_no, 0,thr);
  1849. break;
  1850. }
  1851. }
  1852. memset(nonce_bin, 0, sizeof(nonce_bin));
  1853. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, 3000);//info->read_time);
  1854. nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_CHIP_MASK;
  1855. if (nonce_data.chip_no >= info->rmdev.chip_max)
  1856. nonce_data.chip_no = 0;
  1857. nonce_data.task_no = nonce_bin[NONCE_TASK_NO_OFFSET] & 0x1;
  1858. nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_CMD_MASK;
  1859. nonce_data.work_state = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_STATUS_MASK;
  1860. temp = (double)nonce_bin[NONCE_COMMAND_OFFSET];
  1861. if (temp != 128)
  1862. icarus->temp = temp;
  1863. if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD) {
  1864. info->rmdev.chip[nonce_data.chip_no].last_received_task_complete_time = time(NULL);
  1865. if (info->g_work[nonce_data.chip_no][nonce_data.task_no]) {
  1866. free_work(info->g_work[nonce_data.chip_no][nonce_data.task_no]);
  1867. info->g_work[nonce_data.chip_no][nonce_data.task_no] = NULL;
  1868. }
  1869. goto out;
  1870. }
  1871. if (nonce_data.cmd_value == NONCE_GET_TASK_CMD) {
  1872. rock_send_task(nonce_data.chip_no, nonce_data.task_no, thr);
  1873. goto out;
  1874. }
  1875. if (ret == ICA_NONCE_TIMEOUT)
  1876. rock_send_task(nonce_data.chip_no, nonce_data.task_no, thr);
  1877. work = info->g_work[nonce_data.chip_no][nonce_data.task_no];
  1878. if (work == NULL)
  1879. goto out;
  1880. if (ret == ICA_NONCE_ERROR)
  1881. goto out;
  1882. // aborted before becoming idle, get new work
  1883. if (ret == ICA_NONCE_TIMEOUT || ret == ICA_NONCE_RESTART) {
  1884. timersub(&tv_finish, &tv_start, &elapsed);
  1885. // ONLY up to just when it aborted
  1886. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  1887. estimate_hashes = ((double)(elapsed.tv_sec)
  1888. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  1889. // If some Serial-USB delay allowed the full nonce range to
  1890. // complete it can't have done more than a full nonce
  1891. if (unlikely(estimate_hashes > 0xffffffff))
  1892. estimate_hashes = 0xffffffff;
  1893. applog(LOG_DEBUG, "%s %d: no nonce = 0x%08lX hashes (%ld.%06lds)",
  1894. icarus->drv->name, icarus->device_id,
  1895. (long unsigned int)estimate_hashes,
  1896. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  1897. goto out;
  1898. }
  1899. memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE);
  1900. nonce = htobe32(nonce);
  1901. recv_time = time(NULL);
  1902. if ((recv_time-info->rmdev.dev_detect_time) >= 60) {
  1903. unsigned char i;
  1904. info->rmdev.dev_detect_time = recv_time;
  1905. for (i = 0; i < info->rmdev.chip_max; i ++) {
  1906. if (info->rmdev.chip[i].error_cnt >= 12) {
  1907. if (info->rmdev.chip[i].freq > info->rmdev.min_frq)
  1908. info->rmdev.chip[i].freq--;
  1909. } else if (info->rmdev.chip[i].error_cnt <= 1) {
  1910. if (info->rmdev.chip[i].freq < (info->rmdev.def_frq / 10 - 1))
  1911. info->rmdev.chip[i].freq++;
  1912. }
  1913. info->rmdev.chip[i].error_cnt = 0;
  1914. }
  1915. }
  1916. correction_times = 0;
  1917. info->nonces_checked++;
  1918. while (correction_times < NONCE_CORRECTION_TIMES) {
  1919. uint32_t new_nonce;
  1920. if (correction_times > 0) {
  1921. info->nonces_correction_tests++;
  1922. if (correction_times == 1)
  1923. info->nonces_correction_times++;
  1924. }
  1925. new_nonce = nonce + rbox_corr_values[correction_times];
  1926. /* Basic dupe testing */
  1927. if (new_nonce == info->last_nonce[nonce_data.chip_no][nonce_data.task_no])
  1928. break;
  1929. if (test_nonce(work, new_nonce)) {
  1930. nonce = new_nonce;
  1931. submit_tested_work(thr, work);
  1932. info->last_nonce[nonce_data.chip_no][nonce_data.task_no] = nonce;
  1933. info->nonces_correction[correction_times]++;
  1934. hash_count++;
  1935. info->failing = false;
  1936. applog(LOG_DEBUG, "Rockminer nonce :::OK:::");
  1937. break;
  1938. } else {
  1939. applog(LOG_DEBUG, "Rockminer nonce error times = %d", correction_times);
  1940. if (new_nonce == 0)
  1941. break;
  1942. }
  1943. correction_times++;
  1944. }
  1945. if (correction_times >= NONCE_CORRECTION_TIMES) {
  1946. inc_hw_errors(thr);
  1947. info->nonces_fail++;
  1948. }
  1949. hash_count = (hash_count * info->nonce_mask);
  1950. if (opt_debug || info->do_icarus_timing)
  1951. timersub(&tv_finish, &tv_start, &elapsed);
  1952. applog(LOG_DEBUG, "%s %d: nonce = 0x%08x = 0x%08lX hashes (%ld.%06lds)",
  1953. icarus->drv->name, icarus->device_id,
  1954. nonce, (long unsigned int)hash_count,
  1955. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  1956. out:
  1957. return hash_count;
  1958. }
  1959. static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
  1960. {
  1961. struct api_data *root = NULL;
  1962. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  1963. char data[4096];
  1964. int i, off;
  1965. size_t len;
  1966. float avg;
  1967. // Warning, access to these is not locked - but we don't really
  1968. // care since hashing performance is way more important than
  1969. // locking access to displaying API debug 'stats'
  1970. // If locking becomes an issue for any of them, use copy_data=true also
  1971. root = api_add_int(root, "read_time", &(info->read_time), false);
  1972. root = api_add_int(root, "read_time_limit", &(info->read_time_limit), false);
  1973. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  1974. root = api_add_int(root, "count", &(info->count), false);
  1975. root = api_add_hs(root, "Hs", &(info->Hs), false);
  1976. root = api_add_double(root, "W", &(info->W), false);
  1977. root = api_add_uint(root, "total_values", &(info->values), false);
  1978. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  1979. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  1980. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  1981. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  1982. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  1983. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  1984. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  1985. root = api_add_int(root, "baud", &(info->baud), false);
  1986. root = api_add_int(root, "work_division", &(info->work_division), false);
  1987. root = api_add_int(root, "fpga_count", &(info->fpga_count), false);
  1988. if (info->ident == IDENT_LIN) {
  1989. root = api_add_string(root, "rock_init", info->rock_init, false);
  1990. root = api_add_uint8(root, "rock_chips", &(info->rmdev.detect_chip_no), false);
  1991. root = api_add_uint8(root, "rock_chip_max", &(info->rmdev.chip_max), false);
  1992. root = api_add_uint8(root, "rock_prod_id", &(info->rmdev.product_id), false);
  1993. root = api_add_avg(root, "rock_min_freq", &(info->rmdev.min_frq), false);
  1994. root = api_add_avg(root, "rock_max_freq", &(info->rmdev.max_frq), false);
  1995. root = api_add_uint64(root, "rock_check", &(info->nonces_checked), false);
  1996. root = api_add_uint64(root, "rock_corr", &(info->nonces_correction_times), false);
  1997. root = api_add_uint64(root, "rock_corr_tests", &(info->nonces_correction_tests), false);
  1998. root = api_add_uint64(root, "rock_corr_fail", &(info->nonces_fail), false);
  1999. if (info->nonces_checked <= 0)
  2000. avg = 0;
  2001. else
  2002. avg = (float)(info->nonces_correction_tests) / (float)(info->nonces_checked);
  2003. root = api_add_avg(root, "rock_corr_avg", &avg, true);
  2004. data[0] = '\0';
  2005. off = 0;
  2006. for (i = 0; i < NONCE_CORRECTION_TIMES; i++) {
  2007. len = snprintf(data+off, sizeof(data)-off,
  2008. "%s%"PRIu64,
  2009. i > 0 ? "/" : "",
  2010. info->nonces_correction[i]);
  2011. if (len >= (sizeof(data)-off))
  2012. off = sizeof(data)-1;
  2013. else {
  2014. if (len > 0)
  2015. off += len;
  2016. }
  2017. }
  2018. root = api_add_string(root, "rock_corr_finds", data, true);
  2019. }
  2020. return root;
  2021. }
  2022. static void icarus_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu)
  2023. {
  2024. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  2025. if (info->ant) {
  2026. if (info->u3)
  2027. tailsprintf(buf, bufsiz, "%3.0fMHz %3dmV", opt_au3_freq, opt_au3_volt);
  2028. else
  2029. tailsprintf(buf, bufsiz, "%3.0fMHz", opt_anu_freq);
  2030. } else if (info->ident == IDENT_CMR2 && info->cmr2_speed > 0)
  2031. tailsprintf(buf, bufsiz, "%5.1fMhz", (float)(info->cmr2_speed) * ICARUS_CMR2_SPEED_FACTOR);
  2032. }
  2033. static void icarus_shutdown(__maybe_unused struct thr_info *thr)
  2034. {
  2035. // TODO: ?
  2036. }
  2037. static void icarus_identify(struct cgpu_info *cgpu)
  2038. {
  2039. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  2040. if (info->ident == IDENT_CMR2)
  2041. info->flash_next_work = true;
  2042. }
  2043. static char *icarus_set(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf)
  2044. {
  2045. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  2046. int val;
  2047. if (info->ident != IDENT_CMR2) {
  2048. strcpy(replybuf, "no set options available");
  2049. return replybuf;
  2050. }
  2051. if (strcasecmp(option, "help") == 0) {
  2052. sprintf(replybuf, "clock: range %d-%d",
  2053. ICARUS_CMR2_SPEED_MIN_INT, ICARUS_CMR2_SPEED_MAX_INT);
  2054. return replybuf;
  2055. }
  2056. if (strcasecmp(option, "clock") == 0) {
  2057. if (!setting || !*setting) {
  2058. sprintf(replybuf, "missing clock setting");
  2059. return replybuf;
  2060. }
  2061. val = atoi(setting);
  2062. if (val < ICARUS_CMR2_SPEED_MIN_INT || val > ICARUS_CMR2_SPEED_MAX_INT) {
  2063. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d",
  2064. setting,
  2065. ICARUS_CMR2_SPEED_MIN_INT,
  2066. ICARUS_CMR2_SPEED_MAX_INT);
  2067. }
  2068. info->cmr2_speed = CMR2_INT_TO_SPEED(val);
  2069. info->speed_next_work = true;
  2070. return NULL;
  2071. }
  2072. sprintf(replybuf, "Unknown option: %s", option);
  2073. return replybuf;
  2074. }
  2075. struct device_drv icarus_drv = {
  2076. .drv_id = DRIVER_icarus,
  2077. .dname = "Icarus",
  2078. .name = "ICA",
  2079. .drv_detect = icarus_detect,
  2080. .hash_work = &hash_driver_work,
  2081. .get_api_stats = icarus_api_stats,
  2082. .get_statline_before = icarus_statline_before,
  2083. .set_device = icarus_set,
  2084. .identify_device = icarus_identify,
  2085. .thread_prepare = icarus_prepare,
  2086. .scanwork = icarus_scanwork,
  2087. .thread_shutdown = icarus_shutdown,
  2088. };