driver-bflsc.c 56 KB

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