driver-bflsc.c 48 KB

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