driver-bflsc.c 61 KB

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