driver-bflsc.c 43 KB

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