driver-bflsc.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  1. /*
  2. * Copyright 2013 Andrew Smith
  3. * Copyright 2013 Con Kolivas
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include <float.h>
  11. #include <limits.h>
  12. #include <pthread.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <strings.h>
  16. #include <sys/time.h>
  17. #include <unistd.h>
  18. #include "config.h"
  19. #ifdef WIN32
  20. #include <windows.h>
  21. #endif
  22. #include "compat.h"
  23. #include "miner.h"
  24. #include "usbutils.h"
  25. #define BLANK ""
  26. #define LFSTR "<LF>"
  27. /*
  28. * With Firmware 1.0.0 and a result queue of 20 the Max is:
  29. * inprocess = 12
  30. * max count = 9
  31. * 64+1+24+1+1+(1+8)*8+1 per line = 164 * 20
  32. * OK = 3
  33. * Total: 3304
  34. */
  35. #define BFLSC_BUFSIZ (0x1000)
  36. #define BFLSC_INFO_TIMEOUT 999
  37. #define BFLSC_DI_FIRMWARE "FIRMWARE"
  38. #define BFLSC_DI_ENGINES "ENGINES"
  39. #define BFLSC_DI_JOBSINQUE "JOBS IN QUEUE"
  40. #define BFLSC_DI_XLINKMODE "XLINK MODE"
  41. #define BFLSC_DI_XLINKPRESENT "XLINK PRESENT"
  42. #define BFLSC_DI_DEVICESINCHAIN "DEVICES IN CHAIN"
  43. #define BFLSC_DI_CHAINPRESENCE "CHAIN PRESENCE MASK"
  44. #define FULLNONCE 0x100000000ULL
  45. struct bflsc_dev {
  46. // Work
  47. unsigned int ms_work;
  48. int work_queued;
  49. int work_complete;
  50. int nonces_hw; // TODO: this - need to add a paramter to submit_nonce()
  51. // so can pass 'dev' to hw_error
  52. uint64_t hashes_unsent;
  53. uint64_t hashes_sent;
  54. uint64_t nonces_found;
  55. struct timeval last_check_result;
  56. struct timeval last_dev_result; // array > 0
  57. struct timeval last_nonce_result; // > 0 nonce
  58. // Info
  59. char getinfo[(BFLSC_BUFSIZ+4)*4];
  60. char *firmware;
  61. int engines; // each engine represents a 'thread' in a chip
  62. char *xlink_mode;
  63. char *xlink_present;
  64. // Status
  65. bool dead; // TODO: handle seperate x-link devices failing?
  66. bool overheat;
  67. // Stats
  68. float temp1;
  69. float temp2;
  70. float vcc1;
  71. float vcc2;
  72. float vmain;
  73. float temp1_max;
  74. float temp2_max;
  75. time_t temp1_max_time;
  76. time_t temp2_max_time;
  77. float temp1_5min_av; // TODO:
  78. float temp2_5min_av; // TODO:
  79. // To handle the fact that flushing the queue may not remove all work
  80. // (normally one item is still being processed)
  81. // and also that once the queue is flushed, results may still be in
  82. // the output queue - but we don't want to process them at the time of doing an LP
  83. // when result_id > flush_id+1, flushed work can be discarded since it
  84. // is no longer in the device
  85. uint64_t flush_id; // counter when results were last flushed
  86. uint64_t result_id; // counter when results were last checked
  87. bool flushed; // are any flushed?
  88. };
  89. struct bflsc_info {
  90. pthread_rwlock_t stat_lock;
  91. struct thr_info results_thr;
  92. uint64_t hashes_sent;
  93. uint32_t update_count;
  94. struct timeval last_update;
  95. int sc_count;
  96. struct bflsc_dev *sc_devs;
  97. unsigned int scan_sleep_time;
  98. unsigned int results_sleep_time;
  99. unsigned int default_ms_work;
  100. bool shutdown;
  101. bool flash_led;
  102. bool not_first_work; // allow ignoring the first nonce error
  103. bool fanauto;
  104. };
  105. #define BFLSC_XLINKHDR '@'
  106. #define BFLSC_MAXPAYLOAD 255
  107. struct DataForwardToChain {
  108. uint8_t header;
  109. uint8_t deviceAddress;
  110. uint8_t payloadSize;
  111. uint8_t payloadData[BFLSC_MAXPAYLOAD];
  112. };
  113. #define DATAFORWARDSIZE(data) (1 + 1 + 1 + data.payloadSize)
  114. #define MIDSTATE_BYTES 32
  115. #define MERKLE_OFFSET 64
  116. #define MERKLE_BYTES 12
  117. #define BFLSC_QJOBSIZ (MIDSTATE_BYTES+MERKLE_BYTES+1)
  118. #define BFLSC_EOB 0xaa
  119. struct QueueJobStructure {
  120. uint8_t payloadSize;
  121. uint8_t midState[MIDSTATE_BYTES];
  122. uint8_t blockData[MERKLE_BYTES];
  123. uint8_t endOfBlock;
  124. };
  125. #define QUE_RES_LINES_MIN 3
  126. #define QUE_MIDSTATE 0
  127. #define QUE_BLOCKDATA 1
  128. #define QUE_NONCECOUNT 2
  129. #define QUE_FLD_MIN 3
  130. #define QUE_FLD_MAX 11
  131. #define BFLSC_SIGNATURE 0xc1
  132. #define BFLSC_EOW 0xfe
  133. // N.B. this will only work with 5 jobs
  134. // requires a different jobs[N] for each job count
  135. // but really only need to handle 5 anyway
  136. struct QueueJobPackStructure {
  137. uint8_t payloadSize;
  138. uint8_t signature;
  139. uint8_t jobsInArray;
  140. struct QueueJobStructure jobs[5];
  141. uint8_t endOfWrapper;
  142. };
  143. // TODO: Implement in API and also in usb device selection
  144. struct SaveString {
  145. uint8_t payloadSize;
  146. uint8_t payloadData[BFLSC_MAXPAYLOAD];
  147. };
  148. // Commands
  149. #define BFLSC_IDENTIFY "ZGX"
  150. #define BFLSC_IDENTIFY_LEN (sizeof(BFLSC_IDENTIFY)-1)
  151. #define BFLSC_DETAILS "ZCX"
  152. #define BFLSC_DETAILS_LEN (sizeof(BFLSC_DETAILS)-1)
  153. #define BFLSC_FIRMWARE "ZJX"
  154. #define BFLSC_FIRMWARE_LEN (sizeof(BFLSC_FIRMWARE)-1)
  155. #define BFLSC_FLASH "ZMX"
  156. #define BFLSC_FLASH_LEN (sizeof(BFLSC_FLASH)-1)
  157. #define BFLSC_VOLTAGE "ZTX"
  158. #define BFLSC_VOLTAGE_LEN (sizeof(BFLSC_VOLTAGE)-1)
  159. #define BFLSC_TEMPERATURE "ZLX"
  160. #define BFLSC_TEMPERATURE_LEN (sizeof(BFLSC_TEMPERATURE)-1)
  161. #define BFLSC_QJOB "ZNX"
  162. #define BFLSC_QJOB_LEN (sizeof(BFLSC_QJOB)-1)
  163. #define BFLSC_QJOBS "ZWX"
  164. #define BFLSC_QJOBS_LEN (sizeof(BFLSC_QJOBS)-1)
  165. #define BFLSC_QRES "ZOX"
  166. #define BFLSC_QRES_LEN (sizeof(BFLSC_QRES)-1)
  167. #define BFLSC_QFLUSH "ZQX"
  168. #define BFLSC_QFLUSH_LEN (sizeof(BFLSC_QFLUSH)-1)
  169. #define BFLSC_FANAUTO "Z5X"
  170. #define BFLSC_FANOUT_LEN (sizeof(BFLSC_FANAUTO)-1)
  171. #define BFLSC_FAN0 "Z0X"
  172. #define BFLSC_FAN0_LEN (sizeof(BFLSC_FAN0)-1)
  173. #define BFLSC_FAN1 "Z1X"
  174. #define BFLSC_FAN1_LEN (sizeof(BFLSC_FAN1)-1)
  175. #define BFLSC_FAN2 "Z2X"
  176. #define BFLSC_FAN2_LEN (sizeof(BFLSC_FAN2)-1)
  177. #define BFLSC_FAN3 "Z3X"
  178. #define BFLSC_FAN3_LEN (sizeof(BFLSC_FAN3)-1)
  179. #define BFLSC_FAN4 "Z4X"
  180. #define BFLSC_FAN4_LEN (sizeof(BFLSC_FAN4)-1)
  181. #define BFLSC_SAVESTR "ZSX"
  182. #define BFLSC_SAVESTR_LEN (sizeof(BFLSC_SAVESTR)-1)
  183. #define BFLSC_LOADSTR "ZUX"
  184. #define BFLSC_LOADSTR_LEN (sizeof(BFLSC_LOADSTR)-1)
  185. // Replies
  186. #define BFLSC_IDENTITY "BitFORCE SC"
  187. #define BFLSC_BFLSC "SHA256 SC"
  188. #define BFLSC_OK "OK\n"
  189. #define BFLSC_OK_LEN (sizeof(BFLSC_OK)-1)
  190. #define BFLSC_SUCCESS "SUCCESS\n"
  191. #define BFLSC_SUCCESS_LEN (sizeof(BFLSC_SUCCESS)-1)
  192. #define BFLSC_RESULT "COUNT:"
  193. #define BFLSC_RESULT_LEN (sizeof(BFLSC_RESULT)-1)
  194. #define BFLSC_ANERR "ERR:"
  195. #define BFLSC_ANERR_LEN (sizeof(BFLSC_ANERR)-1)
  196. #define BFLSC_TIMEOUT BFLSC_ANERR "TIMEOUT"
  197. #define BFLSC_TIMEOUT_LEN (sizeof(BFLSC_TIMEOUT)-1)
  198. #define BFLSC_INVALID BFLSC_ANERR "INVALID DATA"
  199. #define BFLSC_INVALID_LEN (sizeof(BFLSC_INVALID)-1)
  200. #define BFLSC_ERRSIG BFLSC_ANERR "SIGNATURE"
  201. #define BFLSC_ERRSIG_LEN (sizeof(BFLSC_ERRSIG)-1)
  202. #define BFLSC_OKQ "OK:QUEUED"
  203. #define BFLSC_OKQ_LEN (sizeof(BFLSC_OKQ)-1)
  204. // Followed by N=1..5
  205. #define BFLSC_OKQN "OK:QUEUED "
  206. #define BFLSC_OKQN_LEN (sizeof(BFLSC_OKQN)-1)
  207. #define BFLSC_QFULL "QUEUE FULL"
  208. #define BFLSC_QFULL_LEN (sizeof(BFLSC_QFULL)-1)
  209. #define BFLSC_HITEMP "HIGH TEMPERATURE RECOVERY"
  210. #define BFLSC_HITEMP_LEN (sizeof(BFLSC_HITEMP)-1)
  211. #define BFLSC_EMPTYSTR "MEMORY EMPTY"
  212. #define BFLSC_EMPTYSTR_LEN (sizeof(BFLSC_EMPTYSTR)-1)
  213. // Queued and non-queued are the same
  214. #define FullNonceRangeJob QueueJobStructure
  215. #define BFLSC_JOBSIZ BFLSC_QJOBSIZ
  216. // Non queued commands
  217. #define BFLSC_SENDWORK "ZDX"
  218. #define BFLSC_SENDWORK_LEN (sizeof(BFLSC_SENDWORK)-1)
  219. // Non queued commands (not used)
  220. #define BFLSC_WORKSTATUS "ZFX"
  221. #define BFLSC_WORKSTATUS_LEN (sizeof(BFLSC_WORKSTATUS)-1)
  222. #define BFLSC_SENDRANGE "ZPX"
  223. #define BFLSC_SENDRANGE_LEN (sizeof(BFLSC_SENDRANGE)-1)
  224. // Non queued work replies (not used)
  225. #define BFLSC_NONCE "NONCE-FOUND:"
  226. #define BFLSC_NONCE_LEN (sizeof(BFLSC_NONCE)-1)
  227. #define BFLSC_NO_NONCE "NO-NONCE"
  228. #define BFLSC_NO_NONCE_LEN (sizeof(BFLSC_NO_NONCE)-1)
  229. #define BFLSC_IDLE "IDLE"
  230. #define BFLSC_IDLE_LEN (sizeof(BFLSC_IDLE)-1)
  231. #define BFLSC_BUSY "BUSY"
  232. #define BFLSC_BUSY_LEN (sizeof(BFLSC_BUSY)-1)
  233. #define BFLSC_MINIRIG "BAM"
  234. #define BFLSC_SINGLE "BAS"
  235. #define BFLSC_LITTLESINGLE "BAL"
  236. #define BFLSC_JALAPENO "BAJ"
  237. // Default expected time for a nonce range
  238. // - thus no need to check until this + last time work was found
  239. // 60GH/s MiniRig (1 board) or Single
  240. #define BAM_WORK_TIME 71.58
  241. #define BAS_WORK_TIME 71.58
  242. // 30GH/s Little Single
  243. #define BAL_WORK_TIME 143.17
  244. // 4.5GH/s Jalapeno
  245. #define BAJ_WORK_TIME 954.44
  246. // Defaults (slightly over half the work time) but ensure none are above 100
  247. // SCAN_TIME - delay after sending work
  248. // RES_TIME - delay between checking for results
  249. #define BAM_SCAN_TIME 20
  250. #define BAM_RES_TIME 2
  251. #define BAS_SCAN_TIME 360
  252. #define BAS_RES_TIME 36
  253. #define BAL_SCAN_TIME 720
  254. #define BAL_RES_TIME 72
  255. #define BAJ_SCAN_TIME 1000
  256. #define BAJ_RES_TIME 100
  257. #define BFLSC_MAX_SLEEP 2000
  258. #define BAJ_LATENCY LATENCY_STD
  259. #define BAL_LATENCY LATENCY_STD
  260. #define BAS_LATENCY LATENCY_STD
  261. #define BAM_LATENCY 2
  262. #define BFLSC_TEMP_SLEEPMS 5
  263. #define BFLSC_QUE_SIZE 20
  264. #define BFLSC_QUE_FULL_ENOUGH 13
  265. #define BFLSC_QUE_WATERMARK 6
  266. // Must drop this far below cutoff before resuming work
  267. #define BFLSC_TEMP_RECOVER 5
  268. // If initialisation fails the first time,
  269. // sleep this amount (ms) and try again
  270. #define REINIT_TIME_FIRST_MS 100
  271. // Max ms per sleep
  272. #define REINIT_TIME_MAX_MS 800
  273. // Keep trying up to this many us
  274. #define REINIT_TIME_MAX 3000000
  275. static const char *blank = "";
  276. struct device_drv bflsc_drv;
  277. static void xlinkstr(char *xlink, int dev, struct bflsc_info *sc_info)
  278. {
  279. if (dev > 0)
  280. sprintf(xlink, " x-%d", dev);
  281. else {
  282. if (sc_info->sc_count > 1)
  283. strcpy(xlink, " master");
  284. else
  285. *xlink = '\0';
  286. }
  287. }
  288. static void bflsc_applog(struct cgpu_info *bflsc, int dev, enum usb_cmds cmd, int amount, int err)
  289. {
  290. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  291. char xlink[17];
  292. xlinkstr(xlink, dev, sc_info);
  293. usb_applog(bflsc, cmd, xlink, amount, err);
  294. }
  295. // Break an input up into lines with LFs removed
  296. // false means an error, but if *lines > 0 then data was also found
  297. // error would be no data or missing LF at the end
  298. static bool tolines(struct cgpu_info *bflsc, int dev, char *buf, int *lines, char ***items, enum usb_cmds cmd)
  299. {
  300. bool ok = true;
  301. char *ptr;
  302. #define p_lines (*lines)
  303. #define p_items (*items)
  304. p_lines = 0;
  305. p_items = NULL;
  306. if (!buf || !(*buf)) {
  307. applog(LOG_DEBUG, "USB: %s%i: (%d) empty %s",
  308. bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
  309. return false;
  310. }
  311. ptr = strdup(buf);
  312. while (ptr && *ptr) {
  313. p_items = realloc(p_items, ++p_lines * sizeof(*p_items));
  314. if (unlikely(!p_items))
  315. quit(1, "Failed to realloc p_items in tolines");
  316. p_items[p_lines-1] = ptr;
  317. ptr = strchr(ptr, '\n');
  318. if (ptr)
  319. *(ptr++) = '\0';
  320. else {
  321. if (ok) {
  322. applog(LOG_DEBUG, "USB: %s%i: (%d) missing lf(s) in %s",
  323. bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
  324. }
  325. ok = false;
  326. }
  327. }
  328. return ok;
  329. }
  330. static void freetolines(int *lines, char ***items)
  331. {
  332. if (*lines > 0) {
  333. free(**items);
  334. free(*items);
  335. }
  336. *lines = 0;
  337. *items = NULL;
  338. }
  339. enum breakmode {
  340. NOCOLON,
  341. ONECOLON,
  342. ALLCOLON // Temperature uses this
  343. };
  344. // Break down a single line into 'fields'
  345. // 'lf' will be a pointer to the final LF if it is there (or NULL)
  346. // firstname will be the allocated buf copy pointer which is also
  347. // the string before ':' for ONECOLON and ALLCOLON
  348. // If any string is missing the ':' when it was expected, false is returned
  349. static bool breakdown(enum breakmode mode, char *buf, int *count, char **firstname, char ***fields, char **lf)
  350. {
  351. char *ptr, *colon, *comma;
  352. bool ok;
  353. #define p_count (*count)
  354. #define p_firstname (*firstname)
  355. #define p_fields (*fields)
  356. #define p_lf (*lf)
  357. p_count = 0;
  358. p_firstname = NULL;
  359. p_fields = NULL;
  360. p_lf = NULL;
  361. if (!buf || !(*buf))
  362. return false;
  363. ptr = p_firstname = strdup(buf);
  364. p_lf = strchr(p_firstname, '\n');
  365. if (mode == ONECOLON) {
  366. colon = strchr(ptr, ':');
  367. if (colon) {
  368. ptr = colon;
  369. *(ptr++) = '\0';
  370. } else
  371. ok = false;
  372. }
  373. while (*ptr == ' ')
  374. ptr++;
  375. ok = true;
  376. while (ptr && *ptr) {
  377. if (mode == ALLCOLON) {
  378. colon = strchr(ptr, ':');
  379. if (colon)
  380. ptr = colon + 1;
  381. else
  382. ok = false;
  383. }
  384. while (*ptr == ' ')
  385. ptr++;
  386. comma = strchr(ptr, ',');
  387. if (comma)
  388. *(comma++) = '\0';
  389. p_fields = realloc(p_fields, ++p_count * sizeof(*p_fields));
  390. if (unlikely(!p_fields))
  391. quit(1, "Failed to realloc p_fields in breakdown");
  392. p_fields[p_count-1] = ptr;
  393. ptr = comma;
  394. }
  395. return ok;
  396. }
  397. static void freebreakdown(int *count, char **firstname, char ***fields)
  398. {
  399. if (*firstname)
  400. free(*firstname);
  401. if (*count > 0)
  402. free(*fields);
  403. *count = 0;
  404. *firstname = NULL;
  405. *fields = NULL;
  406. }
  407. static int write_to_dev(struct cgpu_info *bflsc, int dev, char *buf, int buflen, int *amount, enum usb_cmds cmd)
  408. {
  409. struct DataForwardToChain data;
  410. int len;
  411. /*
  412. * The protocol is syncronous so any previous excess can be
  413. * discarded and assumed corrupt data or failed USB transfers
  414. */
  415. usb_buffer_clear(bflsc);
  416. if (dev == 0)
  417. return usb_write(bflsc, buf, buflen, amount, cmd);
  418. data.header = BFLSC_XLINKHDR;
  419. data.deviceAddress = (uint8_t)dev;
  420. data.payloadSize = buflen;
  421. memcpy(data.payloadData, buf, buflen);
  422. len = DATAFORWARDSIZE(data);
  423. // TODO: handle xlink timeout message - here or at call?
  424. return usb_write(bflsc, (char *)&data, len, amount, cmd);
  425. }
  426. static bool getok(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amount)
  427. {
  428. char buf[BFLSC_BUFSIZ+1];
  429. *err = usb_read_nl(bflsc, buf, sizeof(buf)-1, amount, cmd);
  430. if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
  431. return false;
  432. else
  433. return true;
  434. }
  435. static bool getokerr(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amount, char *buf, size_t bufsiz)
  436. {
  437. *err = usb_read_nl(bflsc, buf, bufsiz-1, amount, cmd);
  438. if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
  439. return false;
  440. else {
  441. if (*amount > (int)BFLSC_ANERR_LEN && strncmp(buf, BFLSC_ANERR, BFLSC_ANERR_LEN) == 0)
  442. return false;
  443. else
  444. return true;
  445. }
  446. }
  447. static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
  448. {
  449. int err, amount;
  450. // Device is gone
  451. if (bflsc->usbinfo.nodev)
  452. return;
  453. mutex_lock(&bflsc->device_mutex);
  454. err = write_to_dev(bflsc, dev, BFLSC_QFLUSH, BFLSC_QFLUSH_LEN, &amount, C_QUEFLUSH);
  455. if (err < 0 || amount != BFLSC_QFLUSH_LEN) {
  456. mutex_unlock(&bflsc->device_mutex);
  457. bflsc_applog(bflsc, dev, C_QUEFLUSH, amount, err);
  458. } else {
  459. // TODO: do we care if we don't get 'OK'? (always will in normal processing)
  460. err = getok(bflsc, C_QUEFLUSHREPLY, &err, &amount);
  461. mutex_unlock(&bflsc->device_mutex);
  462. // TODO: report an error if not 'OK' ?
  463. }
  464. }
  465. /* return True = attempted usb_read_ok()
  466. * set ignore to true means no applog/ignore errors */
  467. static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore)
  468. {
  469. bool readok = false;
  470. mutex_lock(&(bflsc->device_mutex));
  471. *err = write_to_dev(bflsc, dev, BFLSC_QRES, BFLSC_QRES_LEN, amount, C_REQUESTRESULTS);
  472. if (*err < 0 || *amount != BFLSC_QRES_LEN) {
  473. mutex_unlock(&(bflsc->device_mutex));
  474. if (!ignore)
  475. bflsc_applog(bflsc, dev, C_REQUESTRESULTS, *amount, *err);
  476. // TODO: do what? flag as dead device?
  477. // count how many times it has happened and reset/fail it
  478. // or even make sure it is all x-link and that means device
  479. // has failed after some limit of this?
  480. // of course all other I/O must also be failing ...
  481. } else {
  482. readok = true;
  483. *err = usb_read_ok(bflsc, buf, bufsiz-1, amount, C_GETRESULTS);
  484. mutex_unlock(&(bflsc->device_mutex));
  485. if (*err < 0 || *amount < 1) {
  486. if (!ignore)
  487. bflsc_applog(bflsc, dev, C_GETRESULTS, *amount, *err);
  488. // TODO: do what? ... see above
  489. }
  490. }
  491. return readok;
  492. }
  493. static void __bflsc_initialise(struct cgpu_info *bflsc)
  494. {
  495. int err;
  496. // TODO: does x-link bypass the other device FTDI? (I think it does)
  497. // So no initialisation required except for the master device?
  498. if (bflsc->usbinfo.nodev)
  499. return;
  500. // Reset
  501. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  502. FTDI_VALUE_RESET, bflsc->usbdev->found->interface, C_RESET);
  503. applog(LOG_DEBUG, "%s%i: reset got err %d",
  504. bflsc->drv->name, bflsc->device_id, err);
  505. if (bflsc->usbinfo.nodev)
  506. return;
  507. usb_ftdi_set_latency(bflsc);
  508. if (bflsc->usbinfo.nodev)
  509. return;
  510. // Set data control
  511. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  512. FTDI_VALUE_DATA_BAS, bflsc->usbdev->found->interface, C_SETDATA);
  513. applog(LOG_DEBUG, "%s%i: setdata got err %d",
  514. bflsc->drv->name, bflsc->device_id, err);
  515. if (bflsc->usbinfo.nodev)
  516. return;
  517. // Set the baud
  518. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BAS,
  519. (FTDI_INDEX_BAUD_BAS & 0xff00) | bflsc->usbdev->found->interface,
  520. C_SETBAUD);
  521. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  522. bflsc->drv->name, bflsc->device_id, err);
  523. if (bflsc->usbinfo.nodev)
  524. return;
  525. // Set Flow Control
  526. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  527. FTDI_VALUE_FLOW, bflsc->usbdev->found->interface, C_SETFLOW);
  528. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  529. bflsc->drv->name, bflsc->device_id, err);
  530. if (bflsc->usbinfo.nodev)
  531. return;
  532. // Set Modem Control
  533. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  534. FTDI_VALUE_MODEM, bflsc->usbdev->found->interface, C_SETMODEM);
  535. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  536. bflsc->drv->name, bflsc->device_id, err);
  537. if (bflsc->usbinfo.nodev)
  538. return;
  539. // Clear any sent data
  540. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  541. FTDI_VALUE_PURGE_TX, bflsc->usbdev->found->interface, C_PURGETX);
  542. applog(LOG_DEBUG, "%s%i: purgetx got err %d",
  543. bflsc->drv->name, bflsc->device_id, err);
  544. if (bflsc->usbinfo.nodev)
  545. return;
  546. // Clear any received data
  547. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  548. FTDI_VALUE_PURGE_RX, bflsc->usbdev->found->interface, C_PURGERX);
  549. applog(LOG_DEBUG, "%s%i: purgerx got err %d",
  550. bflsc->drv->name, bflsc->device_id, err);
  551. if (!bflsc->cutofftemp)
  552. bflsc->cutofftemp = 90;
  553. }
  554. static void bflsc_initialise(struct cgpu_info *bflsc)
  555. {
  556. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  557. char buf[BFLSC_BUFSIZ+1];
  558. int err, amount;
  559. int dev;
  560. mutex_lock(&(bflsc->device_mutex));
  561. __bflsc_initialise(bflsc);
  562. mutex_unlock(&(bflsc->device_mutex));
  563. for (dev = 0; dev < sc_info->sc_count; dev++) {
  564. bflsc_send_flush_work(bflsc, dev);
  565. bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, true);
  566. }
  567. }
  568. static bool getinfo(struct cgpu_info *bflsc, int dev)
  569. {
  570. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  571. struct bflsc_dev sc_dev;
  572. char buf[BFLSC_BUFSIZ+1];
  573. int err, amount;
  574. char **items, *firstname, **fields, *lf;
  575. int i, lines, count;
  576. bool res, ok;
  577. char *tmp;
  578. /*
  579. * Kano's first dev Jalapeno output:
  580. * DEVICE: BitFORCE SC<LF>
  581. * FIRMWARE: 1.0.0<LF>
  582. * ENGINES: 30<LF>
  583. * FREQUENCY: [UNKNOWN]<LF>
  584. * XLINK MODE: MASTER<LF>
  585. * XLINK PRESENT: YES<LF>
  586. * --DEVICES IN CHAIN: 0<LF>
  587. * --CHAIN PRESENCE MASK: 00000000<LF>
  588. * OK<LF>
  589. */
  590. // TODO: if dev is ever > 0 must handle xlink timeout message
  591. err = write_to_dev(bflsc, dev, BFLSC_DETAILS, BFLSC_DETAILS_LEN, &amount, C_REQUESTDETAILS);
  592. if (err < 0 || amount != BFLSC_DETAILS_LEN) {
  593. applog(LOG_ERR, "%s detect (%s) send details request failed (%d:%d)",
  594. bflsc->drv->dname, bflsc->device_path, amount, err);
  595. return false;
  596. }
  597. err = usb_read_ok_timeout(bflsc, buf, sizeof(buf)-1, &amount,
  598. BFLSC_INFO_TIMEOUT, C_GETDETAILS);
  599. if (err < 0 || amount < 1) {
  600. if (err < 0) {
  601. applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)",
  602. bflsc->drv->dname, bflsc->device_path, amount, err);
  603. } else {
  604. applog(LOG_ERR, "%s detect (%s) get details returned nothing (%d:%d)",
  605. bflsc->drv->dname, bflsc->device_path, amount, err);
  606. }
  607. return false;
  608. }
  609. memset(&sc_dev, 0, sizeof(struct bflsc_dev));
  610. sc_info->sc_count = 1;
  611. res = tolines(bflsc, dev, &(buf[0]), &lines, &items, C_GETDETAILS);
  612. if (!res)
  613. return false;
  614. tmp = str_text(buf);
  615. strcpy(sc_dev.getinfo, tmp);
  616. free(tmp);
  617. for (i = 0; i < lines-2; i++) {
  618. res = breakdown(ONECOLON, items[i], &count, &firstname, &fields, &lf);
  619. if (lf)
  620. *lf = '\0';
  621. if (!res || count != 1) {
  622. tmp = str_text(items[i]);
  623. applog(LOG_WARNING, "%s detect (%s) invalid details line: '%s' %d",
  624. bflsc->drv->dname, bflsc->device_path, tmp, count);
  625. free(tmp);
  626. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  627. goto mata;
  628. }
  629. if (strcmp(firstname, BFLSC_DI_FIRMWARE) == 0) {
  630. sc_dev.firmware = strdup(fields[0]);
  631. if (strcmp(sc_dev.firmware, "1.0.0")) {
  632. tmp = str_text(items[i]);
  633. applog(LOG_WARNING, "%s detect (%s) Warning unknown firmware '%s'",
  634. bflsc->drv->dname, bflsc->device_path, tmp);
  635. free(tmp);
  636. }
  637. }
  638. else if (strcmp(firstname, BFLSC_DI_ENGINES) == 0) {
  639. sc_dev.engines = atoi(fields[0]);
  640. if (sc_dev.engines < 1) {
  641. tmp = str_text(items[i]);
  642. applog(LOG_WARNING, "%s detect (%s) invalid engine count: '%s'",
  643. bflsc->drv->dname, bflsc->device_path, tmp);
  644. free(tmp);
  645. goto mata;
  646. }
  647. }
  648. else if (strcmp(firstname, BFLSC_DI_XLINKMODE) == 0)
  649. sc_dev.xlink_mode = strdup(fields[0]);
  650. else if (strcmp(firstname, BFLSC_DI_XLINKPRESENT) == 0)
  651. sc_dev.xlink_present = strdup(fields[0]);
  652. else if (strcmp(firstname, BFLSC_DI_DEVICESINCHAIN) == 0) {
  653. sc_info->sc_count = atoi(fields[0]) + 1;
  654. if (sc_info->sc_count < 1 || sc_info->sc_count > 30) {
  655. tmp = str_text(items[i]);
  656. applog(LOG_WARNING, "%s detect (%s) invalid s-link count: '%s'",
  657. bflsc->drv->dname, bflsc->device_path, tmp);
  658. free(tmp);
  659. goto mata;
  660. }
  661. }
  662. freebreakdown(&count, &firstname, &fields);
  663. }
  664. sc_info->sc_devs = calloc(sc_info->sc_count, sizeof(struct bflsc_dev));
  665. if (unlikely(!sc_info->sc_devs))
  666. quit(1, "Failed to calloc in getinfo");
  667. memcpy(&(sc_info->sc_devs[0]), &sc_dev, sizeof(sc_dev));
  668. // TODO: do we care about getting this info for the rest if > 0 x-link
  669. ok = true;
  670. goto ne;
  671. mata:
  672. freebreakdown(&count, &firstname, &fields);
  673. ok = false;
  674. ne:
  675. freetolines(&lines, &items);
  676. return ok;
  677. }
  678. static bool bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  679. {
  680. struct bflsc_info *sc_info = NULL;
  681. char buf[BFLSC_BUFSIZ+1];
  682. char devpath[20];
  683. int i, err, amount;
  684. struct timeval init_start, init_now;
  685. int init_sleep, init_count;
  686. bool ident_first;
  687. char *newname;
  688. uint16_t latency;
  689. struct cgpu_info *bflsc = calloc(1, sizeof(*bflsc));
  690. if (unlikely(!bflsc))
  691. quit(1, "Failed to calloc bflsc in bflsc_detect_one");
  692. bflsc->drv = &bflsc_drv;
  693. bflsc->deven = DEV_ENABLED;
  694. bflsc->threads = 1;
  695. sc_info = calloc(1, sizeof(*sc_info));
  696. if (unlikely(!sc_info))
  697. quit(1, "Failed to calloc sc_info in bflsc_detect_one");
  698. // TODO: fix ... everywhere ...
  699. bflsc->device_data = (FILE *)sc_info;
  700. if (!usb_init(bflsc, dev, found))
  701. goto shin;
  702. sprintf(devpath, "%d:%d",
  703. (int)(bflsc->usbinfo.bus_number),
  704. (int)(bflsc->usbinfo.device_address));
  705. // Allow 2 complete attempts if the 1st time returns an unrecognised reply
  706. ident_first = true;
  707. retry:
  708. init_count = 0;
  709. init_sleep = REINIT_TIME_FIRST_MS;
  710. cgtime(&init_start);
  711. reinit:
  712. __bflsc_initialise(bflsc);
  713. err = write_to_dev(bflsc, 0, BFLSC_IDENTIFY, BFLSC_IDENTIFY_LEN, &amount, C_REQUESTIDENTIFY);
  714. if (err < 0 || amount != BFLSC_IDENTIFY_LEN) {
  715. applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
  716. bflsc->drv->dname, devpath, amount, err);
  717. goto unshin;
  718. }
  719. err = usb_read_nl(bflsc, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY);
  720. if (err < 0 || amount < 1) {
  721. init_count++;
  722. cgtime(&init_now);
  723. if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
  724. if (init_count == 2) {
  725. applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying",
  726. bflsc->drv->dname, devpath, amount, err);
  727. }
  728. nmsleep(init_sleep);
  729. if ((init_sleep * 2) <= REINIT_TIME_MAX_MS)
  730. init_sleep *= 2;
  731. goto reinit;
  732. }
  733. if (init_count > 0)
  734. applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs",
  735. bflsc->drv->dname, devpath, init_count, tdiff(&init_now, &init_start));
  736. if (err < 0) {
  737. applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
  738. bflsc->drv->dname, devpath, amount, err);
  739. } else {
  740. applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
  741. bflsc->drv->dname, devpath, amount);
  742. }
  743. goto unshin;
  744. }
  745. buf[amount] = '\0';
  746. if (unlikely(!strstr(buf, BFLSC_BFLSC))) {
  747. applog(LOG_DEBUG, "%s detect (%s) found an FPGA '%s' ignoring",
  748. bflsc->drv->dname, devpath, buf);
  749. goto unshin;
  750. }
  751. if (unlikely(strstr(buf, BFLSC_IDENTITY))) {
  752. if (ident_first) {
  753. applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' trying again ...",
  754. bflsc->drv->dname, devpath, buf);
  755. ident_first = false;
  756. goto retry;
  757. }
  758. applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' on 2nd attempt",
  759. bflsc->drv->dname, devpath, buf);
  760. goto unshin;
  761. }
  762. bflsc->device_path = strdup(devpath);
  763. if (!getinfo(bflsc, 0))
  764. goto unshin;
  765. sc_info->scan_sleep_time = BAS_SCAN_TIME;
  766. sc_info->results_sleep_time = BAS_RES_TIME;
  767. sc_info->default_ms_work = BAS_WORK_TIME;
  768. latency = BAS_LATENCY;
  769. /* When getinfo() "FREQUENCY: [UNKNOWN]" is fixed -
  770. * use 'freq * engines' to estimate.
  771. * Otherwise for now: */
  772. newname = NULL;
  773. if (sc_info->sc_count > 1) {
  774. newname = BFLSC_MINIRIG;
  775. sc_info->scan_sleep_time = BAM_SCAN_TIME;
  776. sc_info->results_sleep_time = BAM_RES_TIME;
  777. sc_info->default_ms_work = BAM_WORK_TIME;
  778. bflsc->usbdev->ident = IDENT_BAM;
  779. latency = BAM_LATENCY;
  780. } else {
  781. if (sc_info->sc_devs[0].engines < 34) { // 16 * 2 + 2
  782. newname = BFLSC_JALAPENO;
  783. sc_info->scan_sleep_time = BAJ_SCAN_TIME;
  784. sc_info->results_sleep_time = BAJ_RES_TIME;
  785. sc_info->default_ms_work = BAJ_WORK_TIME;
  786. bflsc->usbdev->ident = IDENT_BAJ;
  787. latency = BAJ_LATENCY;
  788. } else if (sc_info->sc_devs[0].engines < 130) { // 16 * 8 + 2
  789. newname = BFLSC_LITTLESINGLE;
  790. sc_info->scan_sleep_time = BAL_SCAN_TIME;
  791. sc_info->results_sleep_time = BAL_RES_TIME;
  792. sc_info->default_ms_work = BAL_WORK_TIME;
  793. bflsc->usbdev->ident = IDENT_BAL;
  794. latency = BAL_LATENCY;
  795. }
  796. }
  797. if (latency != bflsc->usbdev->found->latency) {
  798. bflsc->usbdev->found->latency = latency;
  799. usb_ftdi_set_latency(bflsc);
  800. }
  801. for (i = 0; i < sc_info->sc_count; i++)
  802. sc_info->sc_devs[i].ms_work = sc_info->default_ms_work;
  803. if (newname) {
  804. if (!bflsc->drv->copy)
  805. bflsc->drv = copy_drv(bflsc->drv);
  806. bflsc->drv->name = newname;
  807. }
  808. // We have a real BFLSC!
  809. applog(LOG_DEBUG, "%s (%s) identified as: '%s'",
  810. bflsc->drv->dname, devpath, bflsc->drv->name);
  811. if (!add_cgpu(bflsc))
  812. goto unshin;
  813. update_usb_stats(bflsc);
  814. mutex_init(&bflsc->device_mutex);
  815. rwlock_init(&sc_info->stat_lock);
  816. usb_buffer_enable(bflsc);
  817. return true;
  818. unshin:
  819. usb_uninit(bflsc);
  820. shin:
  821. free(bflsc->device_path);
  822. free(bflsc->device_data);
  823. if (bflsc->name != blank)
  824. free(bflsc->name);
  825. if (bflsc->drv->copy)
  826. free(bflsc->drv);
  827. free(bflsc);
  828. return false;
  829. }
  830. static void bflsc_detect(void)
  831. {
  832. usb_detect(&bflsc_drv, bflsc_detect_one);
  833. }
  834. static void get_bflsc_statline_before(char *buf, struct cgpu_info *bflsc)
  835. {
  836. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  837. float temp = 0;
  838. float vcc1 = 0;
  839. int i;
  840. rd_lock(&(sc_info->stat_lock));
  841. for (i = 0; i < sc_info->sc_count; i++) {
  842. if (sc_info->sc_devs[i].temp1 > temp)
  843. temp = sc_info->sc_devs[i].temp1;
  844. if (sc_info->sc_devs[i].temp2 > temp)
  845. temp = sc_info->sc_devs[i].temp2;
  846. if (sc_info->sc_devs[i].vcc1 > vcc1)
  847. vcc1 = sc_info->sc_devs[i].vcc1;
  848. }
  849. rd_unlock(&(sc_info->stat_lock));
  850. tailsprintf(buf, " max%3.0fC %4.2fV | ", temp, vcc1);
  851. }
  852. static void flush_one_dev(struct cgpu_info *bflsc, int dev)
  853. {
  854. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  855. struct work *work, *tmp;
  856. bool did = false;
  857. bflsc_send_flush_work(bflsc, dev);
  858. rd_lock(&bflsc->qlock);
  859. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  860. if (work->queued && work->subid == dev) {
  861. // devflag is used to flag stale work
  862. work->devflag = true;
  863. did = true;
  864. }
  865. }
  866. rd_unlock(&bflsc->qlock);
  867. if (did) {
  868. wr_lock(&(sc_info->stat_lock));
  869. sc_info->sc_devs[dev].flushed = true;
  870. sc_info->sc_devs[dev].flush_id = sc_info->sc_devs[dev].result_id;
  871. sc_info->sc_devs[dev].work_queued = 0;
  872. wr_unlock(&(sc_info->stat_lock));
  873. }
  874. }
  875. static void bflsc_flush_work(struct cgpu_info *bflsc)
  876. {
  877. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  878. int dev;
  879. for (dev = 0; dev < sc_info->sc_count; dev++)
  880. flush_one_dev(bflsc, dev);
  881. }
  882. static void bflsc_flash_led(struct cgpu_info *bflsc, int dev)
  883. {
  884. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  885. int err, amount;
  886. // Device is gone
  887. if (bflsc->usbinfo.nodev)
  888. return;
  889. // It is not critical flashing the led so don't get stuck if we
  890. // can't grab the mutex now
  891. if (mutex_trylock(&bflsc->device_mutex))
  892. return;
  893. err = write_to_dev(bflsc, dev, BFLSC_FLASH, BFLSC_FLASH_LEN, &amount, C_REQUESTFLASH);
  894. if (err < 0 || amount != BFLSC_FLASH_LEN) {
  895. mutex_unlock(&(bflsc->device_mutex));
  896. bflsc_applog(bflsc, dev, C_REQUESTFLASH, amount, err);
  897. } else {
  898. getok(bflsc, C_FLASHREPLY, &err, &amount);
  899. mutex_unlock(&(bflsc->device_mutex));
  900. }
  901. // Once we've tried - don't do it until told to again
  902. // - even if it failed
  903. sc_info->flash_led = false;
  904. return;
  905. }
  906. static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
  907. {
  908. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  909. struct bflsc_dev *sc_dev;
  910. char temp_buf[BFLSC_BUFSIZ+1];
  911. char volt_buf[BFLSC_BUFSIZ+1];
  912. char *tmp;
  913. int err, amount;
  914. char *firstname, **fields, *lf;
  915. char xlink[17];
  916. int count;
  917. bool res;
  918. float temp, temp1, temp2;
  919. float vcc1, vcc2, vmain;
  920. // Device is gone
  921. if (bflsc->usbinfo.nodev)
  922. return false;
  923. if (dev >= sc_info->sc_count) {
  924. applog(LOG_ERR, "%s%i: temp invalid xlink device %d - limit %d",
  925. bflsc->drv->name, bflsc->device_id, dev, sc_info->sc_count - 1);
  926. return false;
  927. }
  928. // Flash instead of Temp
  929. if (sc_info->flash_led) {
  930. bflsc_flash_led(bflsc, dev);
  931. return true;
  932. }
  933. /* It is not very critical getting temp so don't get stuck if we
  934. * can't grab the mutex here */
  935. if (mutex_trylock(&bflsc->device_mutex))
  936. return false;
  937. xlinkstr(&(xlink[0]), dev, sc_info);
  938. err = write_to_dev(bflsc, dev, BFLSC_TEMPERATURE, BFLSC_TEMPERATURE_LEN, &amount, C_REQUESTTEMPERATURE);
  939. if (err < 0 || amount != BFLSC_TEMPERATURE_LEN) {
  940. mutex_unlock(&(bflsc->device_mutex));
  941. applog(LOG_ERR, "%s%i: Error: Request%s temp invalid/timed out (%d:%d)",
  942. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  943. return false;
  944. }
  945. err = usb_read_nl(bflsc, temp_buf, sizeof(temp_buf)-1, &amount, C_GETTEMPERATURE);
  946. if (err < 0 || amount < 1) {
  947. mutex_unlock(&(bflsc->device_mutex));
  948. if (err < 0) {
  949. applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)",
  950. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  951. } else {
  952. applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)",
  953. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  954. }
  955. return false;
  956. }
  957. // N.B. we only get the voltages if the temp succeeds - temp is the important one
  958. err = write_to_dev(bflsc, dev, BFLSC_VOLTAGE, BFLSC_VOLTAGE_LEN, &amount, C_REQUESTVOLTS);
  959. if (err < 0 || amount != BFLSC_VOLTAGE_LEN) {
  960. mutex_unlock(&(bflsc->device_mutex));
  961. applog(LOG_ERR, "%s%i: Error: Request%s volts invalid/timed out (%d:%d)",
  962. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  963. return false;
  964. }
  965. err = usb_read_nl(bflsc, volt_buf, sizeof(volt_buf)-1, &amount, C_GETTEMPERATURE);
  966. if (err < 0 || amount < 1) {
  967. mutex_unlock(&(bflsc->device_mutex));
  968. if (err < 0) {
  969. applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)",
  970. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  971. } else {
  972. applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)",
  973. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  974. }
  975. return false;
  976. }
  977. mutex_unlock(&(bflsc->device_mutex));
  978. res = breakdown(ALLCOLON, temp_buf, &count, &firstname, &fields, &lf);
  979. if (lf)
  980. *lf = '\0';
  981. if (!res || count != 2 || !lf) {
  982. tmp = str_text(temp_buf);
  983. applog(LOG_WARNING, "%s%i: Invalid%s temp reply: '%s'",
  984. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  985. free(tmp);
  986. freebreakdown(&count, &firstname, &fields);
  987. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  988. return false;
  989. }
  990. temp = temp1 = (float)atoi(fields[0]);
  991. temp2 = (float)atoi(fields[1]);
  992. res = breakdown(NOCOLON, volt_buf, &count, &firstname, &fields, &lf);
  993. if (lf)
  994. *lf = '\0';
  995. if (!res || count != 3 || !lf) {
  996. tmp = str_text(volt_buf);
  997. applog(LOG_WARNING, "%s%i: Invalid%s volt reply: '%s'",
  998. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  999. free(tmp);
  1000. freebreakdown(&count, &firstname, &fields);
  1001. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  1002. return false;
  1003. }
  1004. sc_dev = &sc_info->sc_devs[dev];
  1005. vcc1 = (float)atoi(fields[0]) / 1000.0;
  1006. vcc2 = (float)atoi(fields[1]) / 1000.0;
  1007. vmain = (float)atoi(fields[2]) / 1000.0;
  1008. if (vcc1 > 0 || vcc2 > 0 || vmain > 0) {
  1009. wr_lock(&(sc_info->stat_lock));
  1010. if (vcc1 > 0) {
  1011. if (unlikely(sc_dev->vcc1 == 0))
  1012. sc_dev->vcc1 = vcc1;
  1013. else {
  1014. sc_dev->vcc1 += vcc1 * 0.63;
  1015. sc_dev->vcc1 /= 1.63;
  1016. }
  1017. }
  1018. if (vcc2 > 0) {
  1019. if (unlikely(sc_dev->vcc2 == 0))
  1020. sc_dev->vcc2 = vcc2;
  1021. else {
  1022. sc_dev->vcc2 += vcc2 * 0.63;
  1023. sc_dev->vcc2 /= 1.63;
  1024. }
  1025. }
  1026. if (vmain > 0) {
  1027. if (unlikely(sc_dev->vmain == 0))
  1028. sc_dev->vmain = vmain;
  1029. else {
  1030. sc_dev->vmain += vmain * 0.63;
  1031. sc_dev->vmain /= 1.63;
  1032. }
  1033. }
  1034. wr_unlock(&(sc_info->stat_lock));
  1035. }
  1036. if (temp1 > 0 || temp2 > 0) {
  1037. wr_lock(&(sc_info->stat_lock));
  1038. if (unlikely(!sc_dev->temp1))
  1039. sc_dev->temp1 = temp1;
  1040. else {
  1041. sc_dev->temp1 += temp1 * 0.63;
  1042. sc_dev->temp1 /= 1.63;
  1043. }
  1044. if (unlikely(!sc_dev->temp2))
  1045. sc_dev->temp2 = temp2;
  1046. else {
  1047. sc_dev->temp2 += temp2 * 0.63;
  1048. sc_dev->temp2 /= 1.63;
  1049. }
  1050. if (temp1 > sc_dev->temp1_max) {
  1051. sc_dev->temp1_max = temp1;
  1052. sc_dev->temp1_max_time = time(NULL);
  1053. }
  1054. if (temp2 > sc_dev->temp2_max) {
  1055. sc_dev->temp2_max = temp2;
  1056. sc_dev->temp2_max_time = time(NULL);
  1057. }
  1058. if (unlikely(sc_dev->temp1_5min_av == 0))
  1059. sc_dev->temp1_5min_av = temp1;
  1060. else {
  1061. sc_dev->temp1_5min_av += temp1 * .0042;
  1062. sc_dev->temp1_5min_av /= 1.0042;
  1063. }
  1064. if (unlikely(sc_dev->temp2_5min_av == 0))
  1065. sc_dev->temp2_5min_av = temp2;
  1066. else {
  1067. sc_dev->temp2_5min_av += temp2 * .0042;
  1068. sc_dev->temp2_5min_av /= 1.0042;
  1069. }
  1070. wr_unlock(&(sc_info->stat_lock));
  1071. if (temp < temp2)
  1072. temp = temp2;
  1073. bflsc->temp = temp;
  1074. if (bflsc->cutofftemp > 0 && temp > bflsc->cutofftemp) {
  1075. applog(LOG_WARNING, "%s%i:%s temp (%.1f) hit thermal cutoff limit %d, stopping work!",
  1076. bflsc->drv->name, bflsc->device_id, xlink,
  1077. temp, bflsc->cutofftemp);
  1078. dev_error(bflsc, REASON_DEV_THERMAL_CUTOFF);
  1079. sc_dev->overheat = true;
  1080. flush_one_dev(bflsc, dev);
  1081. return false;
  1082. }
  1083. if (bflsc->cutofftemp > 0 && temp < (bflsc->cutofftemp - BFLSC_TEMP_RECOVER))
  1084. sc_dev->overheat = false;
  1085. }
  1086. freebreakdown(&count, &firstname, &fields);
  1087. return true;
  1088. }
  1089. static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char *data, int count, char **fields, int *nonces)
  1090. {
  1091. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1092. char midstate[MIDSTATE_BYTES], blockdata[MERKLE_BYTES];
  1093. struct work *work;
  1094. uint32_t nonce;
  1095. int i, num;
  1096. bool res;
  1097. char *tmp;
  1098. if (count < QUE_FLD_MIN) {
  1099. tmp = str_text(data);
  1100. applog(LOG_ERR, "%s%i:%s work returned too small (%d,%s)",
  1101. bflsc->drv->name, bflsc->device_id, xlink, count, tmp);
  1102. free(tmp);
  1103. inc_hw_errors(bflsc->thr[0]);
  1104. return;
  1105. }
  1106. if (count > QUE_FLD_MAX) {
  1107. applog(LOG_ERR, "%s%i:%s work returned too large (%d) processing %d anyway",
  1108. bflsc->drv->name, bflsc->device_id, xlink, count, QUE_FLD_MAX);
  1109. count = QUE_FLD_MAX;
  1110. inc_hw_errors(bflsc->thr[0]);
  1111. }
  1112. num = atoi(fields[QUE_NONCECOUNT]);
  1113. if (num != count - QUE_FLD_MIN) {
  1114. tmp = str_text(data);
  1115. applog(LOG_ERR, "%s%i:%s incorrect data count (%d) will use %d instead from (%s)",
  1116. bflsc->drv->name, bflsc->device_id, xlink, num, count - QUE_FLD_MAX, tmp);
  1117. free(tmp);
  1118. inc_hw_errors(bflsc->thr[0]);
  1119. }
  1120. memset(midstate, 0, MIDSTATE_BYTES);
  1121. memset(blockdata, 0, MERKLE_BYTES);
  1122. if (!hex2bin((unsigned char *)midstate, fields[QUE_MIDSTATE], MIDSTATE_BYTES) ||
  1123. !hex2bin((unsigned char *)blockdata, fields[QUE_BLOCKDATA], MERKLE_BYTES)) {
  1124. applog(LOG_ERR, "%s%i:%s Failed to convert binary data to hex result - ignored",
  1125. bflsc->drv->name, bflsc->device_id, xlink);
  1126. inc_hw_errors(bflsc->thr[0]);
  1127. }
  1128. work = find_queued_work_bymidstate(bflsc, midstate, MIDSTATE_BYTES,
  1129. blockdata, MERKLE_OFFSET, MERKLE_BYTES);
  1130. if (!work) {
  1131. if (sc_info->not_first_work) {
  1132. applog(LOG_ERR, "%s%i:%s failed to find nonce work - can't be processed - ignored",
  1133. bflsc->drv->name, bflsc->device_id, xlink);
  1134. inc_hw_errors(bflsc->thr[0]);
  1135. }
  1136. return;
  1137. }
  1138. res = false;
  1139. for (i = QUE_FLD_MIN; i < count; i++) {
  1140. if (strlen(fields[i]) != 8) {
  1141. tmp = str_text(data);
  1142. applog(LOG_ERR, "%s%i:%s invalid nonce (%s) will try to process anyway",
  1143. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1144. free(tmp);
  1145. }
  1146. hex2bin((void*)&nonce, fields[i], 4);
  1147. nonce = htobe32(nonce);
  1148. wr_lock(&(sc_info->stat_lock));
  1149. sc_info->sc_devs[dev].nonces_found++;
  1150. wr_unlock(&(sc_info->stat_lock));
  1151. submit_nonce(bflsc->thr[0], work, nonce);
  1152. (*nonces)++;
  1153. res = true;
  1154. }
  1155. wr_lock(&(sc_info->stat_lock));
  1156. if (res)
  1157. sc_info->sc_devs[dev].result_id++;
  1158. sc_info->sc_devs[dev].work_complete++;
  1159. sc_info->sc_devs[dev].hashes_unsent += FULLNONCE;
  1160. // If not flushed (stale)
  1161. if (!(work->devflag))
  1162. sc_info->sc_devs[dev].work_queued -= 1;
  1163. wr_unlock(&(sc_info->stat_lock));
  1164. work_completed(bflsc, work);
  1165. }
  1166. static int process_results(struct cgpu_info *bflsc, int dev, char *buf, int *nonces)
  1167. {
  1168. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1169. char **items, *firstname, **fields, *lf;
  1170. int que, i, lines, count;
  1171. char xlink[17];
  1172. char *tmp, *tmp2;
  1173. *nonces = 0;
  1174. xlinkstr(&(xlink[0]), dev, sc_info);
  1175. tolines(bflsc, dev, buf, &lines, &items, C_GETRESULTS);
  1176. if (lines < 1) {
  1177. tmp = str_text(buf);
  1178. applog(LOG_ERR, "%s%i:%s empty result (%s) ignored",
  1179. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1180. free(tmp);
  1181. que = 0;
  1182. goto arigatou;
  1183. }
  1184. if (lines < QUE_RES_LINES_MIN) {
  1185. tmp = str_text(buf);
  1186. applog(LOG_ERR, "%s%i:%s result too small (%s) ignored",
  1187. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1188. free(tmp);
  1189. que = 0;
  1190. goto arigatou;
  1191. }
  1192. breakdown(ONECOLON, items[1], &count, &firstname, &fields, &lf);
  1193. if (count < 1) {
  1194. tmp = str_text(buf);
  1195. tmp2 = str_text(items[1]);
  1196. applog(LOG_ERR, "%s%i:%s empty result count (%s) in (%s) will try anyway",
  1197. bflsc->drv->name, bflsc->device_id, xlink, tmp2, tmp);
  1198. free(tmp2);
  1199. free(tmp);
  1200. } else if (count != 1) {
  1201. tmp = str_text(buf);
  1202. tmp2 = str_text(items[1]);
  1203. applog(LOG_ERR, "%s%i:%s incorrect result count %d (%s) in (%s) will try anyway",
  1204. bflsc->drv->name, bflsc->device_id, xlink, count, tmp2, tmp);
  1205. free(tmp2);
  1206. free(tmp);
  1207. }
  1208. que = atoi(fields[0]);
  1209. if (que != (lines - QUE_RES_LINES_MIN)) {
  1210. i = que;
  1211. // 1+ In case the last line isn't 'OK' - try to process it
  1212. que = 1 + lines - QUE_RES_LINES_MIN;
  1213. tmp = str_text(buf);
  1214. tmp2 = str_text(items[0]);
  1215. applog(LOG_ERR, "%s%i:%s incorrect result count %d (%s) will try %d (%s)",
  1216. bflsc->drv->name, bflsc->device_id, xlink, i, tmp2, que, tmp);
  1217. free(tmp2);
  1218. free(tmp);
  1219. }
  1220. freebreakdown(&count, &firstname, &fields);
  1221. for (i = 0; i < que; i++) {
  1222. breakdown(NOCOLON, items[i + QUE_RES_LINES_MIN - 1], &count, &firstname, &fields, &lf);
  1223. process_nonces(bflsc, dev, &(xlink[0]), items[i], count, fields, nonces);
  1224. freebreakdown(&count, &firstname, &fields);
  1225. sc_info->not_first_work = true;
  1226. }
  1227. arigatou:
  1228. freetolines(&lines, &items);
  1229. return que;
  1230. }
  1231. #define TVF(tv) ((float)((tv)->tv_sec) + ((float)((tv)->tv_usec) / 1000000.0))
  1232. #define TVFMS(tv) (TVF(tv) * 1000.0)
  1233. // Thread to simply keep looking for results
  1234. static void *bflsc_get_results(void *userdata)
  1235. {
  1236. struct cgpu_info *bflsc = (struct cgpu_info *)userdata;
  1237. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1238. struct timeval elapsed, now;
  1239. float oldest, f;
  1240. char buf[BFLSC_BUFSIZ+1];
  1241. int err, amount;
  1242. int i, que, dev, nonces;
  1243. bool readok;
  1244. cgtime(&now);
  1245. for (i = 0; i < sc_info->sc_count; i++) {
  1246. copy_time(&(sc_info->sc_devs[i].last_check_result), &now);
  1247. copy_time(&(sc_info->sc_devs[i].last_dev_result), &now);
  1248. copy_time(&(sc_info->sc_devs[i].last_nonce_result), &now);
  1249. }
  1250. while (sc_info->shutdown == false) {
  1251. if (bflsc->usbinfo.nodev)
  1252. return NULL;
  1253. dev = -1;
  1254. oldest = FLT_MAX;
  1255. cgtime(&now);
  1256. // Find the first oldest ... that also needs checking
  1257. for (i = 0; i < sc_info->sc_count; i++) {
  1258. timersub(&now, &(sc_info->sc_devs[i].last_check_result), &elapsed);
  1259. f = TVFMS(&elapsed);
  1260. if (f < oldest && f >= sc_info->sc_devs[i].ms_work) {
  1261. f = oldest;
  1262. dev = i;
  1263. }
  1264. }
  1265. if (bflsc->usbinfo.nodev)
  1266. return NULL;
  1267. if (dev == -1)
  1268. goto utsura;
  1269. cgtime(&(sc_info->sc_devs[dev].last_check_result));
  1270. readok = bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, false);
  1271. if (err < 0 || (!readok && amount != BFLSC_QRES_LEN) || (readok && amount < 1)) {
  1272. // TODO: do what else?
  1273. } else {
  1274. que = process_results(bflsc, dev, buf, &nonces);
  1275. sc_info->not_first_work = true; // in case it failed processing it
  1276. if (que > 0)
  1277. cgtime(&(sc_info->sc_devs[dev].last_dev_result));
  1278. if (nonces > 0)
  1279. cgtime(&(sc_info->sc_devs[dev].last_nonce_result));
  1280. // TODO: if not getting results ... reinit?
  1281. }
  1282. utsura:
  1283. nmsleep(sc_info->results_sleep_time);
  1284. }
  1285. return NULL;
  1286. }
  1287. static bool bflsc_thread_prepare(struct thr_info *thr)
  1288. {
  1289. struct cgpu_info *bflsc = thr->cgpu;
  1290. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1291. struct timeval now;
  1292. if (thr_info_create(&(sc_info->results_thr), NULL, bflsc_get_results, (void *)bflsc)) {
  1293. applog(LOG_ERR, "%s%i: thread create failed", bflsc->drv->name, bflsc->device_id);
  1294. return false;
  1295. }
  1296. pthread_detach(sc_info->results_thr.pth);
  1297. cgtime(&now);
  1298. get_datestamp(bflsc->init, &now);
  1299. return true;
  1300. }
  1301. static void bflsc_shutdown(struct thr_info *thr)
  1302. {
  1303. struct cgpu_info *bflsc = thr->cgpu;
  1304. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1305. bflsc_flush_work(bflsc);
  1306. sc_info->shutdown = true;
  1307. }
  1308. static void bflsc_thread_enable(struct thr_info *thr)
  1309. {
  1310. struct cgpu_info *bflsc = thr->cgpu;
  1311. if (bflsc->usbinfo.nodev)
  1312. return;
  1313. bflsc_initialise(bflsc);
  1314. }
  1315. static bool bflsc_send_work(struct cgpu_info *bflsc, int dev, struct work *work)
  1316. {
  1317. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1318. struct FullNonceRangeJob data;
  1319. char buf[BFLSC_BUFSIZ+1];
  1320. int err, amount;
  1321. int len;
  1322. int try;
  1323. // Device is gone
  1324. if (bflsc->usbinfo.nodev)
  1325. return false;
  1326. // TODO: handle this everywhere
  1327. if (sc_info->sc_devs[dev].overheat == true)
  1328. return false;
  1329. // Initially code only deals with sending one work item
  1330. data.payloadSize = BFLSC_JOBSIZ;
  1331. memcpy(data.midState, work->midstate, MIDSTATE_BYTES);
  1332. memcpy(data.blockData, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1333. data.endOfBlock = BFLSC_EOB;
  1334. try = 0;
  1335. mutex_lock(&(bflsc->device_mutex));
  1336. re_send:
  1337. err = write_to_dev(bflsc, dev, BFLSC_QJOB, BFLSC_QJOB_LEN, &amount, C_REQUESTQUEJOB);
  1338. if (err < 0 || amount != BFLSC_QJOB_LEN) {
  1339. mutex_unlock(&(bflsc->device_mutex));
  1340. bflsc_applog(bflsc, dev, C_REQUESTQUEJOB, amount, err);
  1341. return false;
  1342. }
  1343. if (!getok(bflsc, C_REQUESTQUEJOBSTATUS, &err, &amount)) {
  1344. mutex_unlock(&(bflsc->device_mutex));
  1345. bflsc_applog(bflsc, dev, C_REQUESTQUEJOBSTATUS, amount, err);
  1346. return false;
  1347. }
  1348. len = sizeof(struct FullNonceRangeJob);
  1349. err = write_to_dev(bflsc, dev, (char *)&data, len, &amount, C_QUEJOB);
  1350. if (err < 0 || amount != len) {
  1351. mutex_unlock(&(bflsc->device_mutex));
  1352. bflsc_applog(bflsc, dev, C_QUEJOB, amount, err);
  1353. return false;
  1354. }
  1355. if (!getokerr(bflsc, C_QUEJOBSTATUS, &err, &amount, buf, sizeof(buf))) {
  1356. // TODO: check for QUEUE FULL and set work_queued to BFLSC_QUE_SIZE
  1357. // and report a code bug LOG_ERR - coz it should never happen
  1358. // Try twice
  1359. if (try++ < 1 && amount > 1 &&
  1360. strncasecmp(buf, BFLSC_TIMEOUT, BFLSC_TIMEOUT_LEN) == 0)
  1361. goto re_send;
  1362. mutex_unlock(&(bflsc->device_mutex));
  1363. bflsc_applog(bflsc, dev, C_QUEJOBSTATUS, amount, err);
  1364. return false;
  1365. }
  1366. mutex_unlock(&(bflsc->device_mutex));
  1367. wr_lock(&(sc_info->stat_lock));
  1368. sc_info->sc_devs[dev].work_queued++;
  1369. wr_unlock(&(sc_info->stat_lock));
  1370. work->subid = dev;
  1371. return true;
  1372. }
  1373. static bool bflsc_queue_full(struct cgpu_info *bflsc)
  1374. {
  1375. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1376. struct work *work = NULL;
  1377. int i, dev, tried, que;
  1378. bool ret = false;
  1379. int tries = 0;
  1380. tried = -1;
  1381. // if something is wrong with a device try the next one available
  1382. // TODO: try them all? Add an unavailable flag to sc_devs[i] init to 0 here first
  1383. while (++tries < 3) {
  1384. // Device is gone - shouldn't normally get here
  1385. if (bflsc->usbinfo.nodev) {
  1386. ret = true;
  1387. break;
  1388. }
  1389. dev = -1;
  1390. rd_lock(&(sc_info->stat_lock));
  1391. // Anything waiting - gets the work first
  1392. for (i = 0; i < sc_info->sc_count; i++) {
  1393. // TODO: and ignore x-link dead - once I work out how to decide it is dead
  1394. if (i != tried && sc_info->sc_devs[i].work_queued == 0 &&
  1395. !sc_info->sc_devs[i].overheat) {
  1396. dev = i;
  1397. break;
  1398. }
  1399. }
  1400. if (dev == -1) {
  1401. que = BFLSC_QUE_SIZE * 10; // 10x is certainly above the MAX it could be
  1402. // The first device with the smallest amount queued
  1403. for (i = 0; i < sc_info->sc_count; i++) {
  1404. if (i != tried && sc_info->sc_devs[i].work_queued < que &&
  1405. !sc_info->sc_devs[i].overheat) {
  1406. dev = i;
  1407. que = sc_info->sc_devs[i].work_queued;
  1408. }
  1409. }
  1410. if (que > BFLSC_QUE_FULL_ENOUGH)
  1411. dev = -1;
  1412. }
  1413. rd_unlock(&(sc_info->stat_lock));
  1414. // nothing needs work yet
  1415. if (dev == -1) {
  1416. ret = true;
  1417. break;
  1418. }
  1419. if (!work)
  1420. work = get_queued(bflsc);
  1421. if (unlikely(!work))
  1422. break;
  1423. if (bflsc_send_work(bflsc, dev, work)) {
  1424. work = NULL;
  1425. break;
  1426. } else
  1427. tried = dev;
  1428. }
  1429. if (unlikely(work))
  1430. work_completed(bflsc, work);
  1431. return ret;
  1432. }
  1433. static int64_t bflsc_scanwork(struct thr_info *thr)
  1434. {
  1435. struct cgpu_info *bflsc = thr->cgpu;
  1436. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1437. int64_t ret, unsent;
  1438. bool flushed, cleanup;
  1439. struct work *work, *tmp;
  1440. int dev, waited, i;
  1441. // Device is gone
  1442. if (bflsc->usbinfo.nodev)
  1443. return -1;
  1444. flushed = false;
  1445. // Single lock check if any are flagged as flushed
  1446. rd_lock(&(sc_info->stat_lock));
  1447. for (dev = 0; dev < sc_info->sc_count; dev++)
  1448. flushed |= sc_info->sc_devs[dev].flushed;
  1449. rd_unlock(&(sc_info->stat_lock));
  1450. // > 0 flagged as flushed
  1451. if (flushed) {
  1452. // TODO: something like this ......
  1453. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1454. cleanup = false;
  1455. // Is there any flushed work that can be removed?
  1456. rd_lock(&(sc_info->stat_lock));
  1457. if (sc_info->sc_devs[dev].flushed) {
  1458. if (sc_info->sc_devs[dev].result_id > (sc_info->sc_devs[dev].flush_id + 1))
  1459. cleanup = true;
  1460. }
  1461. rd_unlock(&(sc_info->stat_lock));
  1462. // yes remove the flushed work that can be removed
  1463. if (cleanup) {
  1464. wr_lock(&bflsc->qlock);
  1465. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  1466. if (work->devflag && work->subid == dev) {
  1467. bflsc->queued_count--;
  1468. HASH_DEL(bflsc->queued_work, work);
  1469. discard_work(work);
  1470. }
  1471. }
  1472. wr_unlock(&bflsc->qlock);
  1473. wr_lock(&(sc_info->stat_lock));
  1474. sc_info->sc_devs[dev].flushed = false;
  1475. wr_unlock(&(sc_info->stat_lock));
  1476. }
  1477. }
  1478. }
  1479. waited = restart_wait(sc_info->scan_sleep_time);
  1480. if (waited == ETIMEDOUT) {
  1481. unsigned int old_sleep_time, new_sleep_time = 0;
  1482. int min_queued = BFLSC_QUE_SIZE;
  1483. /* Only adjust the scan_sleep_time if we did not receive a
  1484. * restart message while waiting. Try to adjust sleep time
  1485. * so we drop to BFLSC_QUE_WATERMARK before getting more work.
  1486. */
  1487. rd_lock(&sc_info->stat_lock);
  1488. old_sleep_time = sc_info->scan_sleep_time;
  1489. for (i = 0; i < sc_info->sc_count; i++) {
  1490. if (sc_info->sc_devs[i].work_queued < min_queued)
  1491. min_queued = sc_info->sc_devs[i].work_queued;
  1492. }
  1493. rd_unlock(&sc_info->stat_lock);
  1494. new_sleep_time = old_sleep_time;
  1495. /* Increase slowly but decrease quickly */
  1496. if (min_queued > BFLSC_QUE_WATERMARK && old_sleep_time < BFLSC_MAX_SLEEP)
  1497. new_sleep_time = old_sleep_time * 21 / 20;
  1498. else if (min_queued < BFLSC_QUE_WATERMARK)
  1499. new_sleep_time = old_sleep_time * 2 / 3;
  1500. /* Do not sleep more than BFLSC_MAX_SLEEP so we can always
  1501. * report in at least 2 results per 5s log interval. */
  1502. if (new_sleep_time != old_sleep_time) {
  1503. if (new_sleep_time > BFLSC_MAX_SLEEP)
  1504. new_sleep_time = BFLSC_MAX_SLEEP;
  1505. else if (new_sleep_time == 0)
  1506. new_sleep_time = 1;
  1507. applog(LOG_DEBUG, "%s%i: Changed scan sleep time to %d",
  1508. bflsc->drv->name, bflsc->device_id, new_sleep_time);
  1509. wr_lock(&sc_info->stat_lock);
  1510. sc_info->scan_sleep_time = new_sleep_time;
  1511. wr_unlock(&sc_info->stat_lock);
  1512. }
  1513. }
  1514. // Count up the work done since we last were here
  1515. ret = 0;
  1516. wr_lock(&(sc_info->stat_lock));
  1517. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1518. unsent = sc_info->sc_devs[dev].hashes_unsent;
  1519. sc_info->sc_devs[dev].hashes_unsent = 0;
  1520. sc_info->sc_devs[dev].hashes_sent += unsent;
  1521. sc_info->hashes_sent += unsent;
  1522. ret += unsent;
  1523. }
  1524. wr_unlock(&(sc_info->stat_lock));
  1525. return ret;
  1526. }
  1527. static void bflsc_set_fanspeed(struct cgpu_info *bflsc)
  1528. {
  1529. struct bflsc_info *sc_info = (struct bflsc_info *)bflsc->device_data;
  1530. int amount, err;
  1531. if ((bflsc->temp <= 60 && sc_info->fanauto) ||
  1532. (bflsc->temp > 60 && !sc_info->fanauto))
  1533. return;
  1534. mutex_lock(&bflsc->device_mutex);
  1535. if (bflsc->temp > 60) {
  1536. write_to_dev(bflsc, 0, BFLSC_FAN4, BFLSC_FAN4_LEN, &amount,
  1537. C_SETFAN);
  1538. sc_info->fanauto = false;
  1539. } else {
  1540. write_to_dev(bflsc, 0, BFLSC_FANAUTO, BFLSC_FANOUT_LEN,
  1541. &amount, C_SETFAN);
  1542. sc_info->fanauto = true;
  1543. }
  1544. getok(bflsc, C_FANREPLY, &err, &amount);
  1545. mutex_unlock(&bflsc->device_mutex);
  1546. }
  1547. static bool bflsc_get_stats(struct cgpu_info *bflsc)
  1548. {
  1549. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1550. bool allok = true;
  1551. int i;
  1552. // Device is gone
  1553. if (bflsc->usbinfo.nodev)
  1554. return false;
  1555. for (i = 0; i < sc_info->sc_count; i++) {
  1556. if (!bflsc_get_temp(bflsc, i))
  1557. allok = false;
  1558. // Device is gone
  1559. if (bflsc->usbinfo.nodev)
  1560. return false;
  1561. if (i < (sc_info->sc_count - 1))
  1562. nmsleep(BFLSC_TEMP_SLEEPMS);
  1563. }
  1564. bflsc_set_fanspeed(bflsc);
  1565. return allok;
  1566. }
  1567. static void bflsc_identify(struct cgpu_info *bflsc)
  1568. {
  1569. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1570. // TODO: handle x-link
  1571. sc_info->flash_led = true;
  1572. }
  1573. static bool bflsc_thread_init(struct thr_info *thr)
  1574. {
  1575. struct cgpu_info *bflsc = thr->cgpu;
  1576. if (bflsc->usbinfo.nodev)
  1577. return false;
  1578. bflsc_initialise(bflsc);
  1579. return true;
  1580. }
  1581. // there should be a new API function to return device info that isn't the standard stuff
  1582. // instead of bflsc_api_stats - since the stats should really just be internal code info
  1583. // and the new one should be UNusual device stats/extra details - like the stuff below
  1584. static struct api_data *bflsc_api_stats(struct cgpu_info *bflsc)
  1585. {
  1586. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1587. struct api_data *root = NULL;
  1588. //if no x-link ... etc
  1589. rd_lock(&(sc_info->stat_lock));
  1590. root = api_add_temp(root, "Temp1", &(sc_info->sc_devs[0].temp1), true);
  1591. root = api_add_temp(root, "Temp2", &(sc_info->sc_devs[0].temp2), true);
  1592. root = api_add_volts(root, "Vcc1", &(sc_info->sc_devs[0].vcc1), true);
  1593. root = api_add_volts(root, "Vcc2", &(sc_info->sc_devs[0].vcc2), true);
  1594. root = api_add_volts(root, "Vmain", &(sc_info->sc_devs[0].vmain), true);
  1595. root = api_add_temp(root, "Temp1 Max", &(sc_info->sc_devs[0].temp1_max), true);
  1596. root = api_add_temp(root, "Temp2 Max", &(sc_info->sc_devs[0].temp2_max), true);
  1597. root = api_add_time(root, "Temp1 Max Time", &(sc_info->sc_devs[0].temp1_max_time), true);
  1598. root = api_add_time(root, "Temp2 Max Time", &(sc_info->sc_devs[0].temp2_max_time), true);
  1599. rd_unlock(&(sc_info->stat_lock));
  1600. root = api_add_escape(root, "GetInfo", sc_info->sc_devs[0].getinfo, false);
  1601. /*
  1602. else a whole lot of something like these ... etc
  1603. root = api_add_temp(root, "X-%d-Temp1", &(sc_info->temp1), false);
  1604. root = api_add_temp(root, "X-%d-Temp2", &(sc_info->temp2), false);
  1605. root = api_add_volts(root, "X-%d-Vcc1", &(sc_info->vcc1), false);
  1606. root = api_add_volts(root, "X-%d-Vcc2", &(sc_info->vcc2), false);
  1607. root = api_add_volts(root, "X-%d-Vmain", &(sc_info->vmain), false);
  1608. */
  1609. return root;
  1610. }
  1611. struct device_drv bflsc_drv = {
  1612. .drv_id = DRIVER_BFLSC,
  1613. .dname = "BitForceSC",
  1614. .name = BFLSC_SINGLE,
  1615. .drv_detect = bflsc_detect,
  1616. .get_api_stats = bflsc_api_stats,
  1617. .get_statline_before = get_bflsc_statline_before,
  1618. .get_stats = bflsc_get_stats,
  1619. .identify_device = bflsc_identify,
  1620. .thread_prepare = bflsc_thread_prepare,
  1621. .thread_init = bflsc_thread_init,
  1622. .hash_work = hash_queued_work,
  1623. .scanwork = bflsc_scanwork,
  1624. .queue_full = bflsc_queue_full,
  1625. .flush_work = bflsc_flush_work,
  1626. .thread_shutdown = bflsc_shutdown,
  1627. .thread_enable = bflsc_thread_enable
  1628. };