driver-bflsc.c 48 KB

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