driver-bflsc.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  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. int tries = 0;
  818. while (7734) {
  819. if (getinfo(bflsc, 0))
  820. break;
  821. // N.B. we will get displayed errors each time it fails
  822. if (++tries > 2)
  823. goto unshin;
  824. nmsleep(40);
  825. }
  826. switch (sc_info->driver_version) {
  827. case BFLSC_DRV1:
  828. sc_info->que_size = BFLSC_QUE_SIZE_V1;
  829. sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V1;
  830. sc_info->que_watermark = BFLSC_QUE_WATERMARK_V1;
  831. sc_info->que_low = BFLSC_QUE_LOW_V1;
  832. sc_info->que_noncecount = QUE_NONCECOUNT_V1;
  833. sc_info->que_fld_min = QUE_FLD_MIN_V1;
  834. sc_info->que_fld_max = QUE_FLD_MAX_V1;
  835. break;
  836. case BFLSC_DRV2:
  837. case BFLSC_DRVUNDEF:
  838. default:
  839. sc_info->driver_version = BFLSC_DRV2;
  840. sc_info->que_size = BFLSC_QUE_SIZE_V2;
  841. sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V2;
  842. sc_info->que_watermark = BFLSC_QUE_WATERMARK_V2;
  843. sc_info->que_low = BFLSC_QUE_LOW_V2;
  844. sc_info->que_noncecount = QUE_NONCECOUNT_V2;
  845. sc_info->que_fld_min = QUE_FLD_MIN_V2;
  846. sc_info->que_fld_max = QUE_FLD_MAX_V2;
  847. break;
  848. }
  849. sc_info->scan_sleep_time = BAS_SCAN_TIME;
  850. sc_info->results_sleep_time = BAS_RES_TIME;
  851. sc_info->default_ms_work = BAS_WORK_TIME;
  852. latency = BAS_LATENCY;
  853. /* When getinfo() "FREQUENCY: [UNKNOWN]" is fixed -
  854. * use 'freq * engines' to estimate.
  855. * Otherwise for now: */
  856. newname = NULL;
  857. if (sc_info->sc_count > 1) {
  858. newname = BFLSC_MINIRIG;
  859. sc_info->scan_sleep_time = BAM_SCAN_TIME;
  860. sc_info->results_sleep_time = BAM_RES_TIME;
  861. sc_info->default_ms_work = BAM_WORK_TIME;
  862. bflsc->usbdev->ident = IDENT_BAM;
  863. latency = BAM_LATENCY;
  864. } else {
  865. if (sc_info->sc_devs[0].engines < 34) { // 16 * 2 + 2
  866. newname = BFLSC_JALAPENO;
  867. sc_info->scan_sleep_time = BAJ_SCAN_TIME;
  868. sc_info->results_sleep_time = BAJ_RES_TIME;
  869. sc_info->default_ms_work = BAJ_WORK_TIME;
  870. bflsc->usbdev->ident = IDENT_BAJ;
  871. latency = BAJ_LATENCY;
  872. } else if (sc_info->sc_devs[0].engines < 130) { // 16 * 8 + 2
  873. newname = BFLSC_LITTLESINGLE;
  874. sc_info->scan_sleep_time = BAL_SCAN_TIME;
  875. sc_info->results_sleep_time = BAL_RES_TIME;
  876. sc_info->default_ms_work = BAL_WORK_TIME;
  877. bflsc->usbdev->ident = IDENT_BAL;
  878. latency = BAL_LATENCY;
  879. }
  880. }
  881. if (latency != bflsc->usbdev->found->latency) {
  882. bflsc->usbdev->found->latency = latency;
  883. usb_ftdi_set_latency(bflsc);
  884. }
  885. for (i = 0; i < sc_info->sc_count; i++)
  886. sc_info->sc_devs[i].ms_work = sc_info->default_ms_work;
  887. if (newname) {
  888. if (!bflsc->drv->copy)
  889. bflsc->drv = copy_drv(bflsc->drv);
  890. bflsc->drv->name = newname;
  891. }
  892. // We have a real BFLSC!
  893. applog(LOG_DEBUG, "%s (%s) identified as: '%s'",
  894. bflsc->drv->dname, bflsc->device_path, bflsc->drv->name);
  895. if (!add_cgpu(bflsc))
  896. goto unshin;
  897. update_usb_stats(bflsc);
  898. mutex_init(&bflsc->device_mutex);
  899. rwlock_init(&sc_info->stat_lock);
  900. usb_buffer_enable(bflsc);
  901. return true;
  902. unshin:
  903. usb_uninit(bflsc);
  904. shin:
  905. free(bflsc->device_data);
  906. bflsc->device_data = NULL;
  907. if (bflsc->name != blank) {
  908. free(bflsc->name);
  909. bflsc->name = NULL;
  910. }
  911. bflsc = usb_free_cgpu(bflsc);
  912. return false;
  913. }
  914. static void bflsc_detect(void)
  915. {
  916. usb_detect(&bflsc_drv, bflsc_detect_one);
  917. }
  918. static void get_bflsc_statline_before(char *buf, struct cgpu_info *bflsc)
  919. {
  920. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  921. float temp = 0;
  922. float vcc1 = 0;
  923. int i;
  924. rd_lock(&(sc_info->stat_lock));
  925. for (i = 0; i < sc_info->sc_count; i++) {
  926. if (sc_info->sc_devs[i].temp1 > temp)
  927. temp = sc_info->sc_devs[i].temp1;
  928. if (sc_info->sc_devs[i].temp2 > temp)
  929. temp = sc_info->sc_devs[i].temp2;
  930. if (sc_info->sc_devs[i].vcc1 > vcc1)
  931. vcc1 = sc_info->sc_devs[i].vcc1;
  932. }
  933. rd_unlock(&(sc_info->stat_lock));
  934. tailsprintf(buf, " max%3.0fC %4.2fV | ", temp, vcc1);
  935. }
  936. static void flush_one_dev(struct cgpu_info *bflsc, int dev)
  937. {
  938. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  939. struct work *work, *tmp;
  940. bool did = false;
  941. bflsc_send_flush_work(bflsc, dev);
  942. rd_lock(&bflsc->qlock);
  943. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  944. if (work->queued && work->subid == dev) {
  945. // devflag is used to flag stale work
  946. work->devflag = true;
  947. did = true;
  948. }
  949. }
  950. rd_unlock(&bflsc->qlock);
  951. if (did) {
  952. wr_lock(&(sc_info->stat_lock));
  953. sc_info->sc_devs[dev].flushed = true;
  954. sc_info->sc_devs[dev].flush_id = sc_info->sc_devs[dev].result_id;
  955. sc_info->sc_devs[dev].work_queued = 0;
  956. wr_unlock(&(sc_info->stat_lock));
  957. }
  958. }
  959. static void bflsc_flush_work(struct cgpu_info *bflsc)
  960. {
  961. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  962. int dev;
  963. for (dev = 0; dev < sc_info->sc_count; dev++)
  964. flush_one_dev(bflsc, dev);
  965. }
  966. static void bflsc_flash_led(struct cgpu_info *bflsc, int dev)
  967. {
  968. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  969. int err, amount;
  970. // Device is gone
  971. if (bflsc->usbinfo.nodev)
  972. return;
  973. // It is not critical flashing the led so don't get stuck if we
  974. // can't grab the mutex now
  975. if (mutex_trylock(&bflsc->device_mutex))
  976. return;
  977. err = write_to_dev(bflsc, dev, BFLSC_FLASH, BFLSC_FLASH_LEN, &amount, C_REQUESTFLASH);
  978. if (err < 0 || amount != BFLSC_FLASH_LEN) {
  979. mutex_unlock(&(bflsc->device_mutex));
  980. bflsc_applog(bflsc, dev, C_REQUESTFLASH, amount, err);
  981. } else {
  982. getok(bflsc, C_FLASHREPLY, &err, &amount);
  983. mutex_unlock(&(bflsc->device_mutex));
  984. }
  985. // Once we've tried - don't do it until told to again
  986. // - even if it failed
  987. sc_info->flash_led = false;
  988. return;
  989. }
  990. static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
  991. {
  992. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  993. struct bflsc_dev *sc_dev;
  994. char temp_buf[BFLSC_BUFSIZ+1];
  995. char volt_buf[BFLSC_BUFSIZ+1];
  996. char *tmp;
  997. int err, amount;
  998. char *firstname, **fields, *lf;
  999. char xlink[17];
  1000. int count;
  1001. bool res;
  1002. float temp, temp1, temp2;
  1003. float vcc1, vcc2, vmain;
  1004. // Device is gone
  1005. if (bflsc->usbinfo.nodev)
  1006. return false;
  1007. if (dev >= sc_info->sc_count) {
  1008. applog(LOG_ERR, "%s%i: temp invalid xlink device %d - limit %d",
  1009. bflsc->drv->name, bflsc->device_id, dev, sc_info->sc_count - 1);
  1010. return false;
  1011. }
  1012. // Flash instead of Temp
  1013. if (sc_info->flash_led) {
  1014. bflsc_flash_led(bflsc, dev);
  1015. return true;
  1016. }
  1017. /* It is not very critical getting temp so don't get stuck if we
  1018. * can't grab the mutex here */
  1019. if (mutex_trylock(&bflsc->device_mutex))
  1020. return false;
  1021. xlinkstr(&(xlink[0]), dev, sc_info);
  1022. err = write_to_dev(bflsc, dev, BFLSC_TEMPERATURE, BFLSC_TEMPERATURE_LEN, &amount, C_REQUESTTEMPERATURE);
  1023. if (err < 0 || amount != BFLSC_TEMPERATURE_LEN) {
  1024. mutex_unlock(&(bflsc->device_mutex));
  1025. applog(LOG_ERR, "%s%i: Error: Request%s temp invalid/timed out (%d:%d)",
  1026. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  1027. return false;
  1028. }
  1029. err = usb_read_nl(bflsc, temp_buf, sizeof(temp_buf)-1, &amount, C_GETTEMPERATURE);
  1030. if (err < 0 || amount < 1) {
  1031. mutex_unlock(&(bflsc->device_mutex));
  1032. if (err < 0) {
  1033. applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)",
  1034. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  1035. } else {
  1036. applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)",
  1037. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  1038. }
  1039. return false;
  1040. }
  1041. // N.B. we only get the voltages if the temp succeeds - temp is the important one
  1042. err = write_to_dev(bflsc, dev, BFLSC_VOLTAGE, BFLSC_VOLTAGE_LEN, &amount, C_REQUESTVOLTS);
  1043. if (err < 0 || amount != BFLSC_VOLTAGE_LEN) {
  1044. mutex_unlock(&(bflsc->device_mutex));
  1045. applog(LOG_ERR, "%s%i: Error: Request%s volts invalid/timed out (%d:%d)",
  1046. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  1047. return false;
  1048. }
  1049. err = usb_read_nl(bflsc, volt_buf, sizeof(volt_buf)-1, &amount, C_GETTEMPERATURE);
  1050. if (err < 0 || amount < 1) {
  1051. mutex_unlock(&(bflsc->device_mutex));
  1052. if (err < 0) {
  1053. applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)",
  1054. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  1055. } else {
  1056. applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)",
  1057. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  1058. }
  1059. return false;
  1060. }
  1061. mutex_unlock(&(bflsc->device_mutex));
  1062. res = breakdown(ALLCOLON, temp_buf, &count, &firstname, &fields, &lf);
  1063. if (lf)
  1064. *lf = '\0';
  1065. if (!res || count != 2 || !lf) {
  1066. tmp = str_text(temp_buf);
  1067. applog(LOG_WARNING, "%s%i: Invalid%s temp reply: '%s'",
  1068. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1069. free(tmp);
  1070. freebreakdown(&count, &firstname, &fields);
  1071. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  1072. return false;
  1073. }
  1074. temp = temp1 = (float)atoi(fields[0]);
  1075. temp2 = (float)atoi(fields[1]);
  1076. res = breakdown(NOCOLON, volt_buf, &count, &firstname, &fields, &lf);
  1077. if (lf)
  1078. *lf = '\0';
  1079. if (!res || count != 3 || !lf) {
  1080. tmp = str_text(volt_buf);
  1081. applog(LOG_WARNING, "%s%i: Invalid%s volt reply: '%s'",
  1082. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1083. free(tmp);
  1084. freebreakdown(&count, &firstname, &fields);
  1085. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  1086. return false;
  1087. }
  1088. sc_dev = &sc_info->sc_devs[dev];
  1089. vcc1 = (float)atoi(fields[0]) / 1000.0;
  1090. vcc2 = (float)atoi(fields[1]) / 1000.0;
  1091. vmain = (float)atoi(fields[2]) / 1000.0;
  1092. if (vcc1 > 0 || vcc2 > 0 || vmain > 0) {
  1093. wr_lock(&(sc_info->stat_lock));
  1094. if (vcc1 > 0) {
  1095. if (unlikely(sc_dev->vcc1 == 0))
  1096. sc_dev->vcc1 = vcc1;
  1097. else {
  1098. sc_dev->vcc1 += vcc1 * 0.63;
  1099. sc_dev->vcc1 /= 1.63;
  1100. }
  1101. }
  1102. if (vcc2 > 0) {
  1103. if (unlikely(sc_dev->vcc2 == 0))
  1104. sc_dev->vcc2 = vcc2;
  1105. else {
  1106. sc_dev->vcc2 += vcc2 * 0.63;
  1107. sc_dev->vcc2 /= 1.63;
  1108. }
  1109. }
  1110. if (vmain > 0) {
  1111. if (unlikely(sc_dev->vmain == 0))
  1112. sc_dev->vmain = vmain;
  1113. else {
  1114. sc_dev->vmain += vmain * 0.63;
  1115. sc_dev->vmain /= 1.63;
  1116. }
  1117. }
  1118. wr_unlock(&(sc_info->stat_lock));
  1119. }
  1120. if (temp1 > 0 || temp2 > 0) {
  1121. wr_lock(&(sc_info->stat_lock));
  1122. if (unlikely(!sc_dev->temp1))
  1123. sc_dev->temp1 = temp1;
  1124. else {
  1125. sc_dev->temp1 += temp1 * 0.63;
  1126. sc_dev->temp1 /= 1.63;
  1127. }
  1128. if (unlikely(!sc_dev->temp2))
  1129. sc_dev->temp2 = temp2;
  1130. else {
  1131. sc_dev->temp2 += temp2 * 0.63;
  1132. sc_dev->temp2 /= 1.63;
  1133. }
  1134. if (temp1 > sc_dev->temp1_max) {
  1135. sc_dev->temp1_max = temp1;
  1136. sc_dev->temp1_max_time = time(NULL);
  1137. }
  1138. if (temp2 > sc_dev->temp2_max) {
  1139. sc_dev->temp2_max = temp2;
  1140. sc_dev->temp2_max_time = time(NULL);
  1141. }
  1142. if (unlikely(sc_dev->temp1_5min_av == 0))
  1143. sc_dev->temp1_5min_av = temp1;
  1144. else {
  1145. sc_dev->temp1_5min_av += temp1 * .0042;
  1146. sc_dev->temp1_5min_av /= 1.0042;
  1147. }
  1148. if (unlikely(sc_dev->temp2_5min_av == 0))
  1149. sc_dev->temp2_5min_av = temp2;
  1150. else {
  1151. sc_dev->temp2_5min_av += temp2 * .0042;
  1152. sc_dev->temp2_5min_av /= 1.0042;
  1153. }
  1154. wr_unlock(&(sc_info->stat_lock));
  1155. if (temp < temp2)
  1156. temp = temp2;
  1157. bflsc->temp = temp;
  1158. if (bflsc->cutofftemp > 0 && temp >= bflsc->cutofftemp) {
  1159. applog(LOG_WARNING, "%s%i:%s temp (%.1f) hit thermal cutoff limit %d, stopping work!",
  1160. bflsc->drv->name, bflsc->device_id, xlink,
  1161. temp, bflsc->cutofftemp);
  1162. dev_error(bflsc, REASON_DEV_THERMAL_CUTOFF);
  1163. sc_dev->overheat = true;
  1164. flush_one_dev(bflsc, dev);
  1165. return false;
  1166. }
  1167. if (bflsc->cutofftemp > 0 && temp < (bflsc->cutofftemp - BFLSC_TEMP_RECOVER))
  1168. sc_dev->overheat = false;
  1169. }
  1170. freebreakdown(&count, &firstname, &fields);
  1171. return true;
  1172. }
  1173. static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char *data, int count, char **fields, int *nonces)
  1174. {
  1175. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1176. char midstate[MIDSTATE_BYTES], blockdata[MERKLE_BYTES];
  1177. struct work *work;
  1178. uint32_t nonce;
  1179. int i, num;
  1180. bool res;
  1181. char *tmp;
  1182. if (count < sc_info->que_fld_min) {
  1183. tmp = str_text(data);
  1184. applog(LOG_ERR, "%s%i:%s work returned too small (%d,%s)",
  1185. bflsc->drv->name, bflsc->device_id, xlink, count, tmp);
  1186. free(tmp);
  1187. inc_hw_errors(bflsc->thr[0]);
  1188. return;
  1189. }
  1190. if (count > sc_info->que_fld_max) {
  1191. applog(LOG_ERR, "%s%i:%s work returned too large (%d) processing %d anyway",
  1192. bflsc->drv->name, bflsc->device_id, xlink, count, sc_info->que_fld_max);
  1193. count = sc_info->que_fld_max;
  1194. inc_hw_errors(bflsc->thr[0]);
  1195. }
  1196. num = atoi(fields[sc_info->que_noncecount]);
  1197. if (num != count - sc_info->que_fld_min) {
  1198. tmp = str_text(data);
  1199. applog(LOG_ERR, "%s%i:%s incorrect data count (%d) will use %d instead from (%s)",
  1200. bflsc->drv->name, bflsc->device_id, xlink, num, count - sc_info->que_fld_max, tmp);
  1201. free(tmp);
  1202. inc_hw_errors(bflsc->thr[0]);
  1203. }
  1204. memset(midstate, 0, MIDSTATE_BYTES);
  1205. memset(blockdata, 0, MERKLE_BYTES);
  1206. if (!hex2bin((unsigned char *)midstate, fields[QUE_MIDSTATE], MIDSTATE_BYTES) ||
  1207. !hex2bin((unsigned char *)blockdata, fields[QUE_BLOCKDATA], MERKLE_BYTES)) {
  1208. applog(LOG_ERR, "%s%i:%s Failed to convert binary data to hex result - ignored",
  1209. bflsc->drv->name, bflsc->device_id, xlink);
  1210. inc_hw_errors(bflsc->thr[0]);
  1211. return;
  1212. }
  1213. work = find_queued_work_bymidstate(bflsc, midstate, MIDSTATE_BYTES,
  1214. blockdata, MERKLE_OFFSET, MERKLE_BYTES);
  1215. if (!work) {
  1216. if (sc_info->not_first_work) {
  1217. applog(LOG_ERR, "%s%i:%s failed to find nonce work - can't be processed - ignored",
  1218. bflsc->drv->name, bflsc->device_id, xlink);
  1219. inc_hw_errors(bflsc->thr[0]);
  1220. }
  1221. return;
  1222. }
  1223. res = false;
  1224. for (i = sc_info->que_fld_min; i < count; i++) {
  1225. if (strlen(fields[i]) != 8) {
  1226. tmp = str_text(data);
  1227. applog(LOG_ERR, "%s%i:%s invalid nonce (%s) will try to process anyway",
  1228. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1229. free(tmp);
  1230. }
  1231. hex2bin((void*)&nonce, fields[i], 4);
  1232. nonce = htobe32(nonce);
  1233. wr_lock(&(sc_info->stat_lock));
  1234. sc_info->sc_devs[dev].nonces_found++;
  1235. wr_unlock(&(sc_info->stat_lock));
  1236. submit_nonce(bflsc->thr[0], work, nonce);
  1237. (*nonces)++;
  1238. res = true;
  1239. }
  1240. wr_lock(&(sc_info->stat_lock));
  1241. if (res)
  1242. sc_info->sc_devs[dev].result_id++;
  1243. sc_info->sc_devs[dev].work_complete++;
  1244. sc_info->sc_devs[dev].hashes_unsent += FULLNONCE;
  1245. // If not flushed (stale)
  1246. if (!(work->devflag))
  1247. sc_info->sc_devs[dev].work_queued -= 1;
  1248. wr_unlock(&(sc_info->stat_lock));
  1249. work_completed(bflsc, work);
  1250. }
  1251. static int process_results(struct cgpu_info *bflsc, int dev, char *buf, int *nonces)
  1252. {
  1253. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1254. char **items, *firstname, **fields, *lf;
  1255. int que, i, lines, count;
  1256. char xlink[17];
  1257. char *tmp, *tmp2;
  1258. *nonces = 0;
  1259. xlinkstr(&(xlink[0]), dev, sc_info);
  1260. tolines(bflsc, dev, buf, &lines, &items, C_GETRESULTS);
  1261. if (lines < 1) {
  1262. tmp = str_text(buf);
  1263. applog(LOG_ERR, "%s%i:%s empty result (%s) ignored",
  1264. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1265. free(tmp);
  1266. que = 0;
  1267. goto arigatou;
  1268. }
  1269. if (lines < QUE_RES_LINES_MIN) {
  1270. tmp = str_text(buf);
  1271. applog(LOG_ERR, "%s%i:%s result too small (%s) ignored",
  1272. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1273. free(tmp);
  1274. que = 0;
  1275. goto arigatou;
  1276. }
  1277. breakdown(ONECOLON, items[1], &count, &firstname, &fields, &lf);
  1278. if (count < 1) {
  1279. tmp = str_text(buf);
  1280. tmp2 = str_text(items[1]);
  1281. applog(LOG_ERR, "%s%i:%s empty result count (%s) in (%s) will try anyway",
  1282. bflsc->drv->name, bflsc->device_id, xlink, tmp2, tmp);
  1283. free(tmp2);
  1284. free(tmp);
  1285. } else if (count != 1) {
  1286. tmp = str_text(buf);
  1287. tmp2 = str_text(items[1]);
  1288. applog(LOG_ERR, "%s%i:%s incorrect result count %d (%s) in (%s) will try anyway",
  1289. bflsc->drv->name, bflsc->device_id, xlink, count, tmp2, tmp);
  1290. free(tmp2);
  1291. free(tmp);
  1292. }
  1293. que = atoi(fields[0]);
  1294. if (que != (lines - QUE_RES_LINES_MIN)) {
  1295. i = que;
  1296. // 1+ In case the last line isn't 'OK' - try to process it
  1297. que = 1 + lines - QUE_RES_LINES_MIN;
  1298. tmp = str_text(buf);
  1299. tmp2 = str_text(items[0]);
  1300. applog(LOG_ERR, "%s%i:%s incorrect result count %d (%s) will try %d (%s)",
  1301. bflsc->drv->name, bflsc->device_id, xlink, i, tmp2, que, tmp);
  1302. free(tmp2);
  1303. free(tmp);
  1304. }
  1305. freebreakdown(&count, &firstname, &fields);
  1306. for (i = 0; i < que; i++) {
  1307. breakdown(NOCOLON, items[i + QUE_RES_LINES_MIN - 1], &count, &firstname, &fields, &lf);
  1308. process_nonces(bflsc, dev, &(xlink[0]), items[i], count, fields, nonces);
  1309. freebreakdown(&count, &firstname, &fields);
  1310. sc_info->not_first_work = true;
  1311. }
  1312. arigatou:
  1313. freetolines(&lines, &items);
  1314. return que;
  1315. }
  1316. #define TVF(tv) ((float)((tv)->tv_sec) + ((float)((tv)->tv_usec) / 1000000.0))
  1317. #define TVFMS(tv) (TVF(tv) * 1000.0)
  1318. // Thread to simply keep looking for results
  1319. static void *bflsc_get_results(void *userdata)
  1320. {
  1321. struct cgpu_info *bflsc = (struct cgpu_info *)userdata;
  1322. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1323. struct timeval elapsed, now;
  1324. float oldest, f;
  1325. char buf[BFLSC_BUFSIZ+1];
  1326. int err, amount;
  1327. int i, que, dev, nonces;
  1328. bool readok;
  1329. cgtime(&now);
  1330. for (i = 0; i < sc_info->sc_count; i++) {
  1331. copy_time(&(sc_info->sc_devs[i].last_check_result), &now);
  1332. copy_time(&(sc_info->sc_devs[i].last_dev_result), &now);
  1333. copy_time(&(sc_info->sc_devs[i].last_nonce_result), &now);
  1334. }
  1335. while (sc_info->shutdown == false) {
  1336. if (bflsc->usbinfo.nodev)
  1337. return NULL;
  1338. dev = -1;
  1339. oldest = FLT_MAX;
  1340. cgtime(&now);
  1341. // Find the first oldest ... that also needs checking
  1342. for (i = 0; i < sc_info->sc_count; i++) {
  1343. timersub(&now, &(sc_info->sc_devs[i].last_check_result), &elapsed);
  1344. f = TVFMS(&elapsed);
  1345. if (f < oldest && f >= sc_info->sc_devs[i].ms_work) {
  1346. f = oldest;
  1347. dev = i;
  1348. }
  1349. }
  1350. if (bflsc->usbinfo.nodev)
  1351. return NULL;
  1352. if (dev == -1)
  1353. goto utsura;
  1354. cgtime(&(sc_info->sc_devs[dev].last_check_result));
  1355. readok = bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, false);
  1356. if (err < 0 || (!readok && amount != BFLSC_QRES_LEN) || (readok && amount < 1)) {
  1357. // TODO: do what else?
  1358. } else {
  1359. que = process_results(bflsc, dev, buf, &nonces);
  1360. sc_info->not_first_work = true; // in case it failed processing it
  1361. if (que > 0)
  1362. cgtime(&(sc_info->sc_devs[dev].last_dev_result));
  1363. if (nonces > 0)
  1364. cgtime(&(sc_info->sc_devs[dev].last_nonce_result));
  1365. // TODO: if not getting results ... reinit?
  1366. }
  1367. utsura:
  1368. nmsleep(sc_info->results_sleep_time);
  1369. }
  1370. return NULL;
  1371. }
  1372. static bool bflsc_thread_prepare(struct thr_info *thr)
  1373. {
  1374. struct cgpu_info *bflsc = thr->cgpu;
  1375. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1376. struct timeval now;
  1377. if (thr_info_create(&(sc_info->results_thr), NULL, bflsc_get_results, (void *)bflsc)) {
  1378. applog(LOG_ERR, "%s%i: thread create failed", bflsc->drv->name, bflsc->device_id);
  1379. return false;
  1380. }
  1381. pthread_detach(sc_info->results_thr.pth);
  1382. cgtime(&now);
  1383. get_datestamp(bflsc->init, &now);
  1384. return true;
  1385. }
  1386. static void bflsc_shutdown(struct thr_info *thr)
  1387. {
  1388. struct cgpu_info *bflsc = thr->cgpu;
  1389. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1390. bflsc_flush_work(bflsc);
  1391. sc_info->shutdown = true;
  1392. }
  1393. static void bflsc_thread_enable(struct thr_info *thr)
  1394. {
  1395. struct cgpu_info *bflsc = thr->cgpu;
  1396. if (bflsc->usbinfo.nodev)
  1397. return;
  1398. bflsc_initialise(bflsc);
  1399. }
  1400. static bool bflsc_send_work(struct cgpu_info *bflsc, int dev, struct work *work,
  1401. bool mandatory)
  1402. {
  1403. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1404. struct FullNonceRangeJob data;
  1405. char buf[BFLSC_BUFSIZ+1];
  1406. int err, amount;
  1407. int len;
  1408. int try;
  1409. // Device is gone
  1410. if (bflsc->usbinfo.nodev)
  1411. return false;
  1412. // TODO: handle this everywhere
  1413. if (sc_info->sc_devs[dev].overheat == true)
  1414. return false;
  1415. // Initially code only deals with sending one work item
  1416. data.payloadSize = BFLSC_JOBSIZ;
  1417. memcpy(data.midState, work->midstate, MIDSTATE_BYTES);
  1418. memcpy(data.blockData, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1419. data.endOfBlock = BFLSC_EOB;
  1420. try = 0;
  1421. /* On faster devices we have a lot of lock contention so only
  1422. * mandatorily grab the lock and send work if the queue is empty since
  1423. * we have a submit queue. */
  1424. if (mandatory)
  1425. mutex_lock(&(bflsc->device_mutex));
  1426. else {
  1427. if (mutex_trylock(&bflsc->device_mutex))
  1428. return false;
  1429. }
  1430. re_send:
  1431. err = write_to_dev(bflsc, dev, BFLSC_QJOB, BFLSC_QJOB_LEN, &amount, C_REQUESTQUEJOB);
  1432. if (err < 0 || amount != BFLSC_QJOB_LEN) {
  1433. mutex_unlock(&(bflsc->device_mutex));
  1434. bflsc_applog(bflsc, dev, C_REQUESTQUEJOB, amount, err);
  1435. return false;
  1436. }
  1437. if (!getok(bflsc, C_REQUESTQUEJOBSTATUS, &err, &amount)) {
  1438. mutex_unlock(&(bflsc->device_mutex));
  1439. bflsc_applog(bflsc, dev, C_REQUESTQUEJOBSTATUS, amount, err);
  1440. return false;
  1441. }
  1442. len = sizeof(struct FullNonceRangeJob);
  1443. err = write_to_dev(bflsc, dev, (char *)&data, len, &amount, C_QUEJOB);
  1444. if (err < 0 || amount != len) {
  1445. mutex_unlock(&(bflsc->device_mutex));
  1446. bflsc_applog(bflsc, dev, C_QUEJOB, amount, err);
  1447. return false;
  1448. }
  1449. if (!getokerr(bflsc, C_QUEJOBSTATUS, &err, &amount, buf, sizeof(buf))) {
  1450. // TODO: check for QUEUE FULL and set work_queued to sc_info->que_size
  1451. // and report a code bug LOG_ERR - coz it should never happen
  1452. // Try twice
  1453. if (try++ < 1 && amount > 1 &&
  1454. strncasecmp(buf, BFLSC_TIMEOUT, BFLSC_TIMEOUT_LEN) == 0)
  1455. goto re_send;
  1456. mutex_unlock(&(bflsc->device_mutex));
  1457. bflsc_applog(bflsc, dev, C_QUEJOBSTATUS, amount, err);
  1458. return false;
  1459. }
  1460. mutex_unlock(&(bflsc->device_mutex));
  1461. wr_lock(&(sc_info->stat_lock));
  1462. sc_info->sc_devs[dev].work_queued++;
  1463. wr_unlock(&(sc_info->stat_lock));
  1464. work->subid = dev;
  1465. return true;
  1466. }
  1467. static bool bflsc_queue_full(struct cgpu_info *bflsc)
  1468. {
  1469. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1470. struct work *work = NULL;
  1471. int i, dev, tried, que;
  1472. bool ret = false;
  1473. int tries = 0;
  1474. tried = -1;
  1475. // if something is wrong with a device try the next one available
  1476. // TODO: try them all? Add an unavailable flag to sc_devs[i] init to 0 here first
  1477. while (++tries < 3) {
  1478. bool mandatory = false;
  1479. // Device is gone - shouldn't normally get here
  1480. if (bflsc->usbinfo.nodev) {
  1481. ret = true;
  1482. break;
  1483. }
  1484. dev = -1;
  1485. rd_lock(&(sc_info->stat_lock));
  1486. // Anything waiting - gets the work first
  1487. for (i = 0; i < sc_info->sc_count; i++) {
  1488. // TODO: and ignore x-link dead - once I work out how to decide it is dead
  1489. if (i != tried && sc_info->sc_devs[i].work_queued == 0 &&
  1490. !sc_info->sc_devs[i].overheat) {
  1491. dev = i;
  1492. break;
  1493. }
  1494. }
  1495. if (dev == -1) {
  1496. que = sc_info->que_size * 10; // 10x is certainly above the MAX it could be
  1497. // The first device with the smallest amount queued
  1498. for (i = 0; i < sc_info->sc_count; i++) {
  1499. if (i != tried && sc_info->sc_devs[i].work_queued < que &&
  1500. !sc_info->sc_devs[i].overheat) {
  1501. dev = i;
  1502. que = sc_info->sc_devs[i].work_queued;
  1503. }
  1504. }
  1505. if (que > sc_info->que_full_enough)
  1506. dev = -1;
  1507. else if (que < sc_info->que_low)
  1508. mandatory = true;
  1509. }
  1510. rd_unlock(&(sc_info->stat_lock));
  1511. // nothing needs work yet
  1512. if (dev == -1) {
  1513. ret = true;
  1514. break;
  1515. }
  1516. if (!work)
  1517. work = get_queued(bflsc);
  1518. if (unlikely(!work))
  1519. break;
  1520. if (bflsc_send_work(bflsc, dev, work, mandatory)) {
  1521. work = NULL;
  1522. break;
  1523. } else
  1524. tried = dev;
  1525. }
  1526. if (unlikely(work))
  1527. work_completed(bflsc, work);
  1528. return ret;
  1529. }
  1530. static int64_t bflsc_scanwork(struct thr_info *thr)
  1531. {
  1532. struct cgpu_info *bflsc = thr->cgpu;
  1533. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1534. int64_t ret, unsent;
  1535. bool flushed, cleanup;
  1536. struct work *work, *tmp;
  1537. int dev, waited, i;
  1538. // Device is gone
  1539. if (bflsc->usbinfo.nodev)
  1540. return -1;
  1541. flushed = false;
  1542. // Single lock check if any are flagged as flushed
  1543. rd_lock(&(sc_info->stat_lock));
  1544. for (dev = 0; dev < sc_info->sc_count; dev++)
  1545. flushed |= sc_info->sc_devs[dev].flushed;
  1546. rd_unlock(&(sc_info->stat_lock));
  1547. // > 0 flagged as flushed
  1548. if (flushed) {
  1549. // TODO: something like this ......
  1550. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1551. cleanup = false;
  1552. // Is there any flushed work that can be removed?
  1553. rd_lock(&(sc_info->stat_lock));
  1554. if (sc_info->sc_devs[dev].flushed) {
  1555. if (sc_info->sc_devs[dev].result_id > (sc_info->sc_devs[dev].flush_id + 1))
  1556. cleanup = true;
  1557. }
  1558. rd_unlock(&(sc_info->stat_lock));
  1559. // yes remove the flushed work that can be removed
  1560. if (cleanup) {
  1561. wr_lock(&bflsc->qlock);
  1562. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  1563. if (work->devflag && work->subid == dev) {
  1564. bflsc->queued_count--;
  1565. HASH_DEL(bflsc->queued_work, work);
  1566. discard_work(work);
  1567. }
  1568. }
  1569. wr_unlock(&bflsc->qlock);
  1570. wr_lock(&(sc_info->stat_lock));
  1571. sc_info->sc_devs[dev].flushed = false;
  1572. wr_unlock(&(sc_info->stat_lock));
  1573. }
  1574. }
  1575. }
  1576. waited = restart_wait(sc_info->scan_sleep_time);
  1577. if (waited == ETIMEDOUT) {
  1578. unsigned int old_sleep_time, new_sleep_time = 0;
  1579. int min_queued = sc_info->que_size;
  1580. /* Only adjust the scan_sleep_time if we did not receive a
  1581. * restart message while waiting. Try to adjust sleep time
  1582. * so we drop to sc_info->que_watermark before getting more work.
  1583. */
  1584. rd_lock(&sc_info->stat_lock);
  1585. old_sleep_time = sc_info->scan_sleep_time;
  1586. for (i = 0; i < sc_info->sc_count; i++) {
  1587. if (sc_info->sc_devs[i].work_queued < min_queued)
  1588. min_queued = sc_info->sc_devs[i].work_queued;
  1589. }
  1590. rd_unlock(&sc_info->stat_lock);
  1591. new_sleep_time = old_sleep_time;
  1592. /* Increase slowly but decrease quickly */
  1593. if (min_queued > sc_info->que_full_enough && old_sleep_time < BFLSC_MAX_SLEEP)
  1594. new_sleep_time = old_sleep_time * 21 / 20;
  1595. else if (min_queued < sc_info->que_watermark)
  1596. new_sleep_time = old_sleep_time * 2 / 3;
  1597. /* Do not sleep more than BFLSC_MAX_SLEEP so we can always
  1598. * report in at least 2 results per 5s log interval. */
  1599. if (new_sleep_time != old_sleep_time) {
  1600. if (new_sleep_time > BFLSC_MAX_SLEEP)
  1601. new_sleep_time = BFLSC_MAX_SLEEP;
  1602. else if (new_sleep_time == 0)
  1603. new_sleep_time = 1;
  1604. applog(LOG_DEBUG, "%s%i: Changed scan sleep time to %d",
  1605. bflsc->drv->name, bflsc->device_id, new_sleep_time);
  1606. wr_lock(&sc_info->stat_lock);
  1607. sc_info->scan_sleep_time = new_sleep_time;
  1608. wr_unlock(&sc_info->stat_lock);
  1609. }
  1610. }
  1611. // Count up the work done since we last were here
  1612. ret = 0;
  1613. wr_lock(&(sc_info->stat_lock));
  1614. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1615. unsent = sc_info->sc_devs[dev].hashes_unsent;
  1616. sc_info->sc_devs[dev].hashes_unsent = 0;
  1617. sc_info->sc_devs[dev].hashes_sent += unsent;
  1618. sc_info->hashes_sent += unsent;
  1619. ret += unsent;
  1620. }
  1621. wr_unlock(&(sc_info->stat_lock));
  1622. return ret;
  1623. }
  1624. /* Set the fanspeed to auto for any valid value under 60, or max for any value
  1625. * above 60 or if we don't know the temperature. */
  1626. static void bflsc_set_fanspeed(struct cgpu_info *bflsc)
  1627. {
  1628. struct bflsc_info *sc_info = (struct bflsc_info *)bflsc->device_data;
  1629. int amount, err;
  1630. if ((bflsc->temp <= 60 && bflsc->temp > 0 && sc_info->fanauto) ||
  1631. ((bflsc->temp > 60 || !bflsc->temp) && !sc_info->fanauto))
  1632. return;
  1633. mutex_lock(&bflsc->device_mutex);
  1634. if (bflsc->temp > 60 || !bflsc->temp) {
  1635. write_to_dev(bflsc, 0, BFLSC_FAN4, BFLSC_FAN4_LEN, &amount,
  1636. C_SETFAN);
  1637. sc_info->fanauto = false;
  1638. } else {
  1639. write_to_dev(bflsc, 0, BFLSC_FANAUTO, BFLSC_FANOUT_LEN,
  1640. &amount, C_SETFAN);
  1641. sc_info->fanauto = true;
  1642. }
  1643. getok(bflsc, C_FANREPLY, &err, &amount);
  1644. mutex_unlock(&bflsc->device_mutex);
  1645. }
  1646. static bool bflsc_get_stats(struct cgpu_info *bflsc)
  1647. {
  1648. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1649. bool allok = true;
  1650. int i;
  1651. // Device is gone
  1652. if (bflsc->usbinfo.nodev)
  1653. return false;
  1654. for (i = 0; i < sc_info->sc_count; i++) {
  1655. if (!bflsc_get_temp(bflsc, i))
  1656. allok = false;
  1657. // Device is gone
  1658. if (bflsc->usbinfo.nodev)
  1659. return false;
  1660. if (i < (sc_info->sc_count - 1))
  1661. nmsleep(BFLSC_TEMP_SLEEPMS);
  1662. }
  1663. bflsc_set_fanspeed(bflsc);
  1664. return allok;
  1665. }
  1666. static void bflsc_identify(struct cgpu_info *bflsc)
  1667. {
  1668. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1669. // TODO: handle x-link
  1670. sc_info->flash_led = true;
  1671. }
  1672. static bool bflsc_thread_init(struct thr_info *thr)
  1673. {
  1674. struct cgpu_info *bflsc = thr->cgpu;
  1675. if (bflsc->usbinfo.nodev)
  1676. return false;
  1677. bflsc_initialise(bflsc);
  1678. return true;
  1679. }
  1680. // there should be a new API function to return device info that isn't the standard stuff
  1681. // instead of bflsc_api_stats - since the stats should really just be internal code info
  1682. // and the new one should be UNusual device stats/extra details - like the stuff below
  1683. static struct api_data *bflsc_api_stats(struct cgpu_info *bflsc)
  1684. {
  1685. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1686. struct api_data *root = NULL;
  1687. int i;
  1688. //if no x-link ... etc
  1689. rd_lock(&(sc_info->stat_lock));
  1690. root = api_add_temp(root, "Temp1", &(sc_info->sc_devs[0].temp1), true);
  1691. root = api_add_temp(root, "Temp2", &(sc_info->sc_devs[0].temp2), true);
  1692. root = api_add_volts(root, "Vcc1", &(sc_info->sc_devs[0].vcc1), true);
  1693. root = api_add_volts(root, "Vcc2", &(sc_info->sc_devs[0].vcc2), true);
  1694. root = api_add_volts(root, "Vmain", &(sc_info->sc_devs[0].vmain), true);
  1695. root = api_add_temp(root, "Temp1 Max", &(sc_info->sc_devs[0].temp1_max), true);
  1696. root = api_add_temp(root, "Temp2 Max", &(sc_info->sc_devs[0].temp2_max), true);
  1697. root = api_add_time(root, "Temp1 Max Time", &(sc_info->sc_devs[0].temp1_max_time), true);
  1698. root = api_add_time(root, "Temp2 Max Time", &(sc_info->sc_devs[0].temp2_max_time), true);
  1699. root = api_add_int(root, "Work Queued", &(sc_info->sc_devs[0].work_queued), true);
  1700. root = api_add_int(root, "Work Complete", &(sc_info->sc_devs[0].work_complete), true);
  1701. root = api_add_bool(root, "Overheat", &(sc_info->sc_devs[0].overheat), true);
  1702. root = api_add_uint64(root, "Flush ID", &(sc_info->sc_devs[0].flush_id), true);
  1703. root = api_add_uint64(root, "Result ID", &(sc_info->sc_devs[0].result_id), true);
  1704. root = api_add_bool(root, "Flushed", &(sc_info->sc_devs[0].flushed), true);
  1705. root = api_add_uint(root, "Scan Sleep", &(sc_info->scan_sleep_time), true);
  1706. root = api_add_uint(root, "Results Sleep", &(sc_info->results_sleep_time), true);
  1707. root = api_add_uint(root, "Work ms", &(sc_info->default_ms_work), true);
  1708. rd_unlock(&(sc_info->stat_lock));
  1709. i = (int)(sc_info->driver_version);
  1710. root = api_add_int(root, "Driver", &i, true);
  1711. root = api_add_string(root, "Firmware", sc_info->sc_devs[0].firmware, false);
  1712. root = api_add_string(root, "Chips", sc_info->sc_devs[0].chips, false);
  1713. root = api_add_int(root, "Que Size", &(sc_info->que_size), false);
  1714. root = api_add_int(root, "Que Full", &(sc_info->que_full_enough), false);
  1715. root = api_add_int(root, "Que Watermark", &(sc_info->que_watermark), false);
  1716. root = api_add_int(root, "Que Low", &(sc_info->que_low), false);
  1717. root = api_add_escape(root, "GetInfo", sc_info->sc_devs[0].getinfo, false);
  1718. /*
  1719. else a whole lot of something like these ... etc
  1720. root = api_add_temp(root, "X-%d-Temp1", &(sc_info->temp1), false);
  1721. root = api_add_temp(root, "X-%d-Temp2", &(sc_info->temp2), false);
  1722. root = api_add_volts(root, "X-%d-Vcc1", &(sc_info->vcc1), false);
  1723. root = api_add_volts(root, "X-%d-Vcc2", &(sc_info->vcc2), false);
  1724. root = api_add_volts(root, "X-%d-Vmain", &(sc_info->vmain), false);
  1725. */
  1726. return root;
  1727. }
  1728. struct device_drv bflsc_drv = {
  1729. .drv_id = DRIVER_BFLSC,
  1730. .dname = "BitForceSC",
  1731. .name = BFLSC_SINGLE,
  1732. .drv_detect = bflsc_detect,
  1733. .get_api_stats = bflsc_api_stats,
  1734. .get_statline_before = get_bflsc_statline_before,
  1735. .get_stats = bflsc_get_stats,
  1736. .identify_device = bflsc_identify,
  1737. .thread_prepare = bflsc_thread_prepare,
  1738. .thread_init = bflsc_thread_init,
  1739. .hash_work = hash_queued_work,
  1740. .scanwork = bflsc_scanwork,
  1741. .queue_full = bflsc_queue_full,
  1742. .flush_work = bflsc_flush_work,
  1743. .thread_shutdown = bflsc_shutdown,
  1744. .thread_enable = bflsc_thread_enable
  1745. };