driver-bflsc.c 56 KB

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