driver-bflsc.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  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. char temp_buf[BFLSC_BUFSIZ+1];
  831. char volt_buf[BFLSC_BUFSIZ+1];
  832. char *tmp;
  833. int err, amount;
  834. char *firstname, **fields, *lf;
  835. char xlink[17];
  836. int count;
  837. bool res;
  838. float temp, temp1, temp2;
  839. float vcc1, vcc2, vmain;
  840. // Device is gone
  841. if (bflsc->usbinfo.nodev)
  842. return false;
  843. if (dev >= sc_info->sc_count) {
  844. applog(LOG_ERR, "%s%i: temp invalid xlink device %d - limit %d",
  845. bflsc->drv->name, bflsc->device_id, dev, sc_info->sc_count - 1);
  846. return false;
  847. }
  848. // Flash instead of Temp
  849. if (sc_info->flash_led) {
  850. bflsc_flash_led(bflsc, dev);
  851. return true;
  852. }
  853. /* It is not very critical getting temp so don't get stuck if we
  854. * can't grab the mutex here */
  855. if (mutex_trylock(&bflsc->device_mutex))
  856. return false;
  857. xlinkstr(&(xlink[0]), dev, sc_info);
  858. err = write_to_dev(bflsc, dev, BFLSC_TEMPERATURE, BFLSC_TEMPERATURE_LEN, &amount, C_REQUESTTEMPERATURE);
  859. if (err < 0 || amount != BFLSC_TEMPERATURE_LEN) {
  860. mutex_unlock(&(bflsc->device_mutex));
  861. applog(LOG_ERR, "%s%i: Error: Request%s temp invalid/timed out (%d:%d)",
  862. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  863. return false;
  864. }
  865. err = usb_ftdi_read_nl(bflsc, temp_buf, sizeof(temp_buf)-1, &amount, C_GETTEMPERATURE);
  866. if (err < 0 || amount < 1) {
  867. mutex_unlock(&(bflsc->device_mutex));
  868. if (err < 0) {
  869. applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)",
  870. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  871. } else {
  872. applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)",
  873. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  874. }
  875. return false;
  876. }
  877. // N.B. we only get the voltages if the temp succeeds - temp is the important one
  878. err = write_to_dev(bflsc, dev, BFLSC_VOLTAGE, BFLSC_VOLTAGE_LEN, &amount, C_REQUESTVOLTS);
  879. if (err < 0 || amount != BFLSC_VOLTAGE_LEN) {
  880. mutex_unlock(&(bflsc->device_mutex));
  881. applog(LOG_ERR, "%s%i: Error: Request%s volts invalid/timed out (%d:%d)",
  882. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  883. return false;
  884. }
  885. err = usb_ftdi_read_nl(bflsc, volt_buf, sizeof(volt_buf)-1, &amount, C_GETTEMPERATURE);
  886. if (err < 0 || amount < 1) {
  887. mutex_unlock(&(bflsc->device_mutex));
  888. if (err < 0) {
  889. applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)",
  890. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  891. } else {
  892. applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)",
  893. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  894. }
  895. return false;
  896. }
  897. mutex_unlock(&(bflsc->device_mutex));
  898. res = breakdown(ALLCOLON, temp_buf, &count, &firstname, &fields, &lf);
  899. if (lf)
  900. *lf = '\0';
  901. if (!res || count != 2 || !lf) {
  902. tmp = str_text(temp_buf);
  903. applog(LOG_WARNING, "%s%i: Invalid%s temp reply: '%s'",
  904. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  905. free(tmp);
  906. freebreakdown(&count, &firstname, &fields);
  907. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  908. return false;
  909. }
  910. temp = temp1 = (float)atoi(fields[0]);
  911. temp2 = (float)atoi(fields[1]);
  912. res = breakdown(NOCOLON, volt_buf, &count, &firstname, &fields, &lf);
  913. if (lf)
  914. *lf = '\0';
  915. if (!res || count != 3 || !lf) {
  916. tmp = str_text(volt_buf);
  917. applog(LOG_WARNING, "%s%i: Invalid%s volt reply: '%s'",
  918. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  919. free(tmp);
  920. freebreakdown(&count, &firstname, &fields);
  921. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  922. return false;
  923. }
  924. vcc1 = (float)atoi(fields[0]) / 1000.0;
  925. vcc2 = (float)atoi(fields[1]) / 1000.0;
  926. vmain = (float)atoi(fields[2]) / 1000.0;
  927. if (vcc1 > 0 || vcc2 > 0 || vmain > 0) {
  928. wr_lock(&(sc_info->stat_lock));
  929. if (vcc1 > 0)
  930. sc_info->sc_devs[dev].vcc1 = vcc1;
  931. if (vcc2 > 0)
  932. sc_info->sc_devs[dev].vcc2 = vcc2;
  933. if (vmain > 0)
  934. sc_info->sc_devs[dev].vmain = vmain;
  935. wr_unlock(&(sc_info->stat_lock));
  936. }
  937. if (temp1 > 0 || temp2 > 0) {
  938. wr_lock(&(sc_info->stat_lock));
  939. if (unlikely(!sc_info->sc_devs[dev].temp1))
  940. sc_info->sc_devs[dev].temp1 = temp1;
  941. else {
  942. sc_info->sc_devs[dev].temp1 += temp1 * 0.63;
  943. sc_info->sc_devs[dev].temp1 /= 1.63;
  944. }
  945. if (unlikely(!sc_info->sc_devs[dev].temp2))
  946. sc_info->sc_devs[dev].temp2 = temp2;
  947. else {
  948. sc_info->sc_devs[dev].temp2 += temp2 * 0.63;
  949. sc_info->sc_devs[dev].temp2 /= 1.63;
  950. }
  951. if (temp1 > sc_info->sc_devs[dev].temp1_max) {
  952. sc_info->sc_devs[dev].temp1_max = temp1;
  953. sc_info->sc_devs[dev].temp1_max_time = time(NULL);
  954. }
  955. if (temp2 > sc_info->sc_devs[dev].temp2_max) {
  956. sc_info->sc_devs[dev].temp2_max = temp2;
  957. sc_info->sc_devs[dev].temp2_max_time = time(NULL);
  958. }
  959. sc_info->sc_devs[dev].temp1_sum += temp1;
  960. sc_info->sc_devs[dev].temp2_sum += temp2;
  961. sc_info->sc_devs[dev].temp_count++;
  962. sc_info->sc_devs[dev].temp_time = time(NULL);
  963. // TODO: 5min av
  964. // also will be useful for adjusting the fans by code
  965. wr_unlock(&(sc_info->stat_lock));
  966. if (temp < temp2)
  967. temp = temp2;
  968. bflsc->temp = temp;
  969. if (bflsc->cutofftemp > 0 && temp > bflsc->cutofftemp) {
  970. applog(LOG_WARNING, "%s%i:%s temp (%.1f) hit thermal cutoff limit %d, stopping work!",
  971. bflsc->drv->name, bflsc->device_id, xlink,
  972. temp, bflsc->cutofftemp);
  973. dev_error(bflsc, REASON_DEV_THERMAL_CUTOFF);
  974. sc_info->sc_devs[dev].overheat = true;
  975. flush_one_dev(bflsc, dev);
  976. return false;
  977. }
  978. if (bflsc->cutofftemp > 0 && temp < (bflsc->cutofftemp - BFLSC_TEMP_RECOVER))
  979. sc_info->sc_devs[dev].overheat = false;
  980. }
  981. freebreakdown(&count, &firstname, &fields);
  982. return true;
  983. }
  984. static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char *data, int count, char **fields, int *nonces)
  985. {
  986. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
  987. char midstate[MIDSTATE_BYTES], blockdata[MERKLE_BYTES];
  988. struct work *work;
  989. uint32_t nonce;
  990. int i, num;
  991. bool res;
  992. char *tmp;
  993. if (count < QUE_FLD_MIN) {
  994. tmp = str_text(data);
  995. applog(LOG_ERR, "%s%i:%s work returned too small (%d,%s)",
  996. bflsc->drv->name, bflsc->device_id, xlink, count, tmp);
  997. free(tmp);
  998. return;
  999. }
  1000. if (count > QUE_FLD_MAX) {
  1001. applog(LOG_ERR, "%s%i:%s work returned too large (%d) processing %d anyway",
  1002. bflsc->drv->name, bflsc->device_id, xlink, count, QUE_FLD_MAX);
  1003. count = QUE_FLD_MAX;
  1004. }
  1005. num = atoi(fields[QUE_NONCECOUNT]);
  1006. if (num != count - QUE_FLD_MIN) {
  1007. applog(LOG_ERR, "%s%i:%s incorrect data count (%d) will use %d instead",
  1008. bflsc->drv->name, bflsc->device_id, xlink, num, count - QUE_FLD_MAX);
  1009. }
  1010. memset(midstate, 0, MIDSTATE_BYTES);
  1011. memset(blockdata, 0, MERKLE_BYTES);
  1012. hex2bin((unsigned char *)midstate, fields[QUE_MIDSTATE], MIDSTATE_BYTES);
  1013. hex2bin((unsigned char *)blockdata, fields[QUE_BLOCKDATA], MERKLE_BYTES);
  1014. work = find_queued_work_bymidstate(bflsc, midstate, MIDSTATE_BYTES,
  1015. blockdata, MERKLE_OFFSET, MERKLE_BYTES);
  1016. if (!work) {
  1017. applog(LOG_ERR, "%s%i:%s failed to find work - can't be processed - ignored",
  1018. bflsc->drv->name, bflsc->device_id, xlink);
  1019. return;
  1020. }
  1021. res = false;
  1022. for (i = QUE_FLD_MIN; i < count; i++) {
  1023. if (strlen(fields[i]) != 8) {
  1024. tmp = str_text(data);
  1025. applog(LOG_ERR, "%s%i:%s invalid nonce (%s) will try to process anyway",
  1026. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1027. free(tmp);
  1028. }
  1029. hex2bin((void*)&nonce, fields[i], 4);
  1030. nonce = htobe32(nonce);
  1031. wr_lock(&(sc_info->stat_lock));
  1032. sc_info->sc_devs[dev].nonces_found++;
  1033. wr_unlock(&(sc_info->stat_lock));
  1034. submit_nonce(bflsc->thr[0], work, nonce);
  1035. (*nonces)++;
  1036. res = true;
  1037. }
  1038. wr_lock(&(sc_info->stat_lock));
  1039. if (res)
  1040. sc_info->sc_devs[dev].result_id++;
  1041. sc_info->sc_devs[dev].work_complete++;
  1042. sc_info->sc_devs[dev].hashes_unsent += FULLNONCE;
  1043. // If not flushed (stale)
  1044. if (!(work->devflag))
  1045. sc_info->sc_devs[dev].work_queued -= 1;
  1046. wr_unlock(&(sc_info->stat_lock));
  1047. work_completed(bflsc, work);
  1048. }
  1049. static int process_results(struct cgpu_info *bflsc, int dev, char *buf, int *nonces)
  1050. {
  1051. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
  1052. char **items, *firstname, **fields, *lf;
  1053. int que, i, lines, count;
  1054. char xlink[17];
  1055. bool res;
  1056. char *tmp, *tmp2;
  1057. *nonces = 0;
  1058. xlinkstr(&(xlink[0]), dev, sc_info);
  1059. res = tolines(bflsc, dev, buf, &lines, &items, C_GETRESULTS);
  1060. if (lines < 1) {
  1061. tmp = str_text(buf);
  1062. applog(LOG_ERR, "%s%i:%s empty result (%s) ignored",
  1063. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1064. free(tmp);
  1065. que = 0;
  1066. goto arigatou;
  1067. }
  1068. if (lines < QUE_RES_LINES_MIN) {
  1069. tmp = str_text(buf);
  1070. applog(LOG_ERR, "%s%i:%s result too small (%s) ignored",
  1071. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1072. free(tmp);
  1073. que = 0;
  1074. goto arigatou;
  1075. }
  1076. res = breakdown(ONECOLON, items[1], &count, &firstname, &fields, &lf);
  1077. if (count < 1) {
  1078. tmp = str_text(buf);
  1079. tmp2 = str_text(items[1]);
  1080. applog(LOG_ERR, "%s%i:%s empty result count (%s) in (%s) will try anyway",
  1081. bflsc->drv->name, bflsc->device_id, xlink, tmp2, tmp);
  1082. free(tmp2);
  1083. free(tmp);
  1084. } else if (count != 1) {
  1085. tmp = str_text(buf);
  1086. tmp2 = str_text(items[1]);
  1087. applog(LOG_ERR, "%s%i:%s incorrect result count (%s) in (%s) will try anyway",
  1088. bflsc->drv->name, bflsc->device_id, xlink, tmp2, tmp);
  1089. free(tmp2);
  1090. free(tmp);
  1091. }
  1092. que = atoi(fields[0]);
  1093. if (que != (lines - QUE_RES_LINES_MIN)) {
  1094. i = que;
  1095. // 1+ In case the last line isn't 'OK' - try to process it
  1096. que = 1 + lines - QUE_RES_LINES_MIN;
  1097. tmp = str_text(items[0]);
  1098. applog(LOG_ERR, "%s%i:%s incorrect result count (%s) %d but should be %d will try %d anyway",
  1099. bflsc->drv->name, bflsc->device_id, xlink, tmp, i, que, que);
  1100. free(tmp);
  1101. }
  1102. freebreakdown(&count, &firstname, &fields);
  1103. for (i = 0; i < que; i++) {
  1104. res = breakdown(NOCOLON, items[i + QUE_RES_LINES_MIN - 1], &count, &firstname, &fields, &lf);
  1105. process_nonces(bflsc, dev, &(xlink[0]), items[i], count, fields, nonces);
  1106. freebreakdown(&count, &firstname, &fields);
  1107. }
  1108. arigatou:
  1109. freetolines(&lines, &items);
  1110. return que;
  1111. }
  1112. #define TVF(tv) ((float)((tv)->tv_sec) + ((float)((tv)->tv_usec) / 1000000.0))
  1113. #define TVFMS(tv) (TVF(tv) * 1000.0)
  1114. // Thread to simply keep looking for results
  1115. static void *bflsc_get_results(void *userdata)
  1116. {
  1117. struct cgpu_info *bflsc = (struct cgpu_info *)userdata;
  1118. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
  1119. struct timeval elapsed, now;
  1120. float oldest, f;
  1121. char buf[BFLSC_BUFSIZ+1];
  1122. int err, amount;
  1123. int i, que, dev, nonces;
  1124. cgtime(&now);
  1125. for (i = 0; i < sc_info->sc_count; i++) {
  1126. copy_time(&(sc_info->sc_devs[i].last_check_result), &now);
  1127. copy_time(&(sc_info->sc_devs[i].last_dev_result), &now);
  1128. copy_time(&(sc_info->sc_devs[i].last_nonce_result), &now);
  1129. }
  1130. while (sc_info->shutdown == false) {
  1131. if (bflsc->usbinfo.nodev)
  1132. return NULL;
  1133. dev = -1;
  1134. oldest = FLT_MAX;
  1135. cgtime(&now);
  1136. // Find the first oldest ... that also needs checking
  1137. for (i = 0; i < sc_info->sc_count; i++) {
  1138. timersub(&now, &(sc_info->sc_devs[i].last_check_result), &elapsed);
  1139. f = TVFMS(&elapsed);
  1140. if (f < oldest && f >= sc_info->sc_devs[i].ms_work) {
  1141. f = oldest;
  1142. dev = i;
  1143. }
  1144. }
  1145. if (bflsc->usbinfo.nodev)
  1146. return NULL;
  1147. if (dev == -1)
  1148. goto utsura;
  1149. mutex_lock(&(bflsc->device_mutex));
  1150. cgtime(&(sc_info->sc_devs[dev].last_check_result));
  1151. err = write_to_dev(bflsc, dev, BFLSC_QRES, BFLSC_QRES_LEN, &amount, C_REQUESTRESULTS);
  1152. if (err < 0 || amount != BFLSC_QRES_LEN) {
  1153. mutex_unlock(&(bflsc->device_mutex));
  1154. bflsc_applog(bflsc, dev, C_REQUESTRESULTS, amount, err);
  1155. // TODO: do what? flag as dead device?
  1156. // count how many times it has happened and reset/fail it
  1157. // or even make sure it is all x-link and that means device
  1158. // has failed after some limit of this?
  1159. // of course all other I/O must also be failing ...
  1160. } else {
  1161. err = usb_ftdi_read_ok(bflsc, buf, sizeof(buf)-1, &amount, C_GETRESULTS);
  1162. mutex_unlock(&(bflsc->device_mutex));
  1163. if (err < 0 || amount < 1) {
  1164. bflsc_applog(bflsc, dev, C_GETRESULTS, amount, err);
  1165. // TODO: do what? ... see above
  1166. } else {
  1167. que = process_results(bflsc, dev, buf, &nonces);
  1168. if (que > 0)
  1169. cgtime(&(sc_info->sc_devs[dev].last_dev_result));
  1170. if (nonces > 0)
  1171. cgtime(&(sc_info->sc_devs[dev].last_nonce_result));
  1172. // TODO: if not getting results ...
  1173. }
  1174. // TODO: re-estimate the wait times (results_sleep_time and ms_work) based on ?
  1175. }
  1176. utsura:
  1177. nmsleep(sc_info->results_sleep_time);
  1178. }
  1179. return NULL;
  1180. }
  1181. static bool bflsc_thread_prepare(struct thr_info *thr)
  1182. {
  1183. struct cgpu_info *bflsc = thr->cgpu;
  1184. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
  1185. struct timeval now;
  1186. if (thr_info_create(&(sc_info->results_thr), NULL, bflsc_get_results, (void *)bflsc)) {
  1187. applog(LOG_ERR, "%s%i: thread create failed", bflsc->drv->name, bflsc->device_id);
  1188. return false;
  1189. }
  1190. pthread_detach(sc_info->results_thr.pth);
  1191. cgtime(&now);
  1192. get_datestamp(bflsc->init, &now);
  1193. return true;
  1194. }
  1195. static void bflsc_shutdown(struct thr_info *thr)
  1196. {
  1197. struct cgpu_info *bflsc = thr->cgpu;
  1198. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
  1199. sc_info->shutdown = true;
  1200. }
  1201. static void bflsc_thread_enable(struct thr_info *thr)
  1202. {
  1203. struct cgpu_info *bflsc = thr->cgpu;
  1204. if (bflsc->usbinfo.nodev)
  1205. return;
  1206. bflsc_initialise(bflsc);
  1207. }
  1208. static bool bflsc_send_work(struct cgpu_info *bflsc, int dev, struct work *work)
  1209. {
  1210. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
  1211. struct FullNonceRangeJob data;
  1212. char buf[BFLSC_BUFSIZ+1];
  1213. int err, amount;
  1214. int len;
  1215. int try;
  1216. // Device is gone
  1217. if (bflsc->usbinfo.nodev)
  1218. return false;
  1219. // TODO: handle this everywhere
  1220. if (sc_info->sc_devs[dev].overheat == true)
  1221. return false;
  1222. // Initially code only deals with sending one work item
  1223. data.payloadSize = BFLSC_JOBSIZ;
  1224. memcpy(data.midState, work->midstate, MIDSTATE_BYTES);
  1225. memcpy(data.blockData, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1226. data.endOfBlock = BFLSC_EOB;
  1227. try = 0;
  1228. mutex_lock(&(bflsc->device_mutex));
  1229. re_send:
  1230. err = write_to_dev(bflsc, dev, BFLSC_QJOB, BFLSC_QJOB_LEN, &amount, C_REQUESTQUEJOB);
  1231. if (err < 0 || amount != BFLSC_QJOB_LEN) {
  1232. mutex_unlock(&(bflsc->device_mutex));
  1233. bflsc_applog(bflsc, dev, C_REQUESTQUEJOB, amount, err);
  1234. return false;
  1235. }
  1236. if (!getok(bflsc, C_REQUESTQUEJOBSTATUS, &err, &amount)) {
  1237. mutex_unlock(&(bflsc->device_mutex));
  1238. bflsc_applog(bflsc, dev, C_REQUESTQUEJOBSTATUS, amount, err);
  1239. return false;
  1240. }
  1241. len = sizeof(struct FullNonceRangeJob);
  1242. err = write_to_dev(bflsc, dev, (char *)&data, len, &amount, C_QUEJOB);
  1243. if (err < 0 || amount != len) {
  1244. mutex_unlock(&(bflsc->device_mutex));
  1245. bflsc_applog(bflsc, dev, C_QUEJOB, amount, err);
  1246. return false;
  1247. }
  1248. if (!getokerr(bflsc, C_QUEJOBSTATUS, &err, &amount, buf, sizeof(buf))) {
  1249. // TODO: check for QUEUE FULL and set work_queued to BFLSC_QUE_SIZE
  1250. // and report a code bug LOG_ERR - coz it should never happen
  1251. // Try twice
  1252. if (try++ < 1 && amount > 1 &&
  1253. strncasecmp(buf, BFLSC_TIMEOUT, BFLSC_TIMEOUT_LEN) == 0)
  1254. goto re_send;
  1255. mutex_unlock(&(bflsc->device_mutex));
  1256. bflsc_applog(bflsc, dev, C_QUEJOBSTATUS, amount, err);
  1257. return false;
  1258. }
  1259. mutex_unlock(&(bflsc->device_mutex));
  1260. wr_lock(&(sc_info->stat_lock));
  1261. sc_info->sc_devs[dev].work_queued++;
  1262. wr_unlock(&(sc_info->stat_lock));
  1263. work->subid = dev;
  1264. return true;
  1265. }
  1266. static bool bflsc_queue_full(struct cgpu_info *bflsc)
  1267. {
  1268. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
  1269. struct work *work = NULL;
  1270. int i, dev, tried, que;
  1271. bool ret = false;
  1272. int tries = 0;
  1273. tried = -1;
  1274. // if something is wrong with a device try the next one available
  1275. // TODO: try them all? Add an unavailable flag to sc_devs[i] init to 0 here first
  1276. while (++tries < 3) {
  1277. // Device is gone
  1278. if (bflsc->usbinfo.nodev)
  1279. return false;
  1280. dev = -1;
  1281. rd_lock(&(sc_info->stat_lock));
  1282. // Anything waiting - gets the work first
  1283. for (i = 0; i < sc_info->sc_count; i++) {
  1284. // TODO: and ignore x-link dead - once I work out how to decide it is dead
  1285. if (i != tried && sc_info->sc_devs[i].work_queued == 0 &&
  1286. !sc_info->sc_devs[i].overheat) {
  1287. dev = i;
  1288. break;
  1289. }
  1290. }
  1291. if (dev == -1) {
  1292. que = BFLSC_QUE_SIZE * 10; // 10x is certainly above the MAX it could be
  1293. // The first device with the smallest amount queued
  1294. for (i = 0; i < sc_info->sc_count; i++) {
  1295. if (i != tried && sc_info->sc_devs[i].work_queued < que &&
  1296. !sc_info->sc_devs[i].overheat) {
  1297. dev = i;
  1298. que = sc_info->sc_devs[i].work_queued;
  1299. }
  1300. }
  1301. if (que > BFLSC_QUE_FULL_ENOUGH)
  1302. dev = -1;
  1303. }
  1304. rd_unlock(&(sc_info->stat_lock));
  1305. // nothing needs work yet
  1306. if (dev == -1) {
  1307. ret = true;
  1308. break;
  1309. }
  1310. if (!work)
  1311. work = get_queued(bflsc);
  1312. if (unlikely(!work))
  1313. break;
  1314. if (bflsc_send_work(bflsc, dev, work)) {
  1315. work = NULL;
  1316. break;
  1317. } else
  1318. tried = dev;
  1319. }
  1320. if (unlikely(work))
  1321. work_completed(bflsc, work);
  1322. return ret;
  1323. }
  1324. static int64_t bflsc_scanwork(struct thr_info *thr)
  1325. {
  1326. struct cgpu_info *bflsc = thr->cgpu;
  1327. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
  1328. int64_t ret, unsent;
  1329. bool flushed, cleanup;
  1330. struct work *work, *tmp;
  1331. int dev;
  1332. // Device is gone
  1333. if (bflsc->usbinfo.nodev)
  1334. return -1;
  1335. flushed = false;
  1336. // Single lock check if any are flagged as flushed
  1337. rd_lock(&(sc_info->stat_lock));
  1338. for (dev = 0; dev < sc_info->sc_count; dev++)
  1339. flushed |= sc_info->sc_devs[dev].flushed;
  1340. rd_unlock(&(sc_info->stat_lock));
  1341. // > 0 flagged as flushed
  1342. if (flushed) {
  1343. // TODO: something like this ......
  1344. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1345. cleanup = false;
  1346. // Is there any flushed work that can be removed?
  1347. rd_lock(&(sc_info->stat_lock));
  1348. if (sc_info->sc_devs[dev].flushed) {
  1349. if (sc_info->sc_devs[dev].result_id > (sc_info->sc_devs[dev].flush_id + 1))
  1350. cleanup = true;
  1351. }
  1352. rd_unlock(&(sc_info->stat_lock));
  1353. // yes remove the flushed work that can be removed
  1354. if (cleanup) {
  1355. wr_lock(&bflsc->qlock);
  1356. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  1357. if (work->devflag && work->subid == dev) {
  1358. bflsc->queued_count--;
  1359. HASH_DEL(bflsc->queued_work, work);
  1360. discard_work(work);
  1361. }
  1362. }
  1363. wr_unlock(&bflsc->qlock);
  1364. wr_lock(&(sc_info->stat_lock));
  1365. sc_info->sc_devs[dev].flushed = false;
  1366. wr_unlock(&(sc_info->stat_lock));
  1367. }
  1368. }
  1369. }
  1370. // avoid a hard loop
  1371. if (sc_info->scan_sleep_time > 0)
  1372. restart_wait(sc_info->scan_sleep_time);
  1373. // Count up the work done since we last were here
  1374. ret = 0;
  1375. wr_lock(&(sc_info->stat_lock));
  1376. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1377. unsent = sc_info->sc_devs[dev].hashes_unsent;
  1378. sc_info->sc_devs[dev].hashes_unsent = 0;
  1379. sc_info->sc_devs[dev].hashes_sent += unsent;
  1380. sc_info->hashes_sent += unsent;
  1381. ret += unsent;
  1382. }
  1383. wr_unlock(&(sc_info->stat_lock));
  1384. return ret;
  1385. }
  1386. static bool bflsc_get_stats(struct cgpu_info *bflsc)
  1387. {
  1388. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
  1389. bool allok = true;
  1390. int i;
  1391. // Device is gone
  1392. if (bflsc->usbinfo.nodev)
  1393. return false;
  1394. for (i = 0; i < sc_info->sc_count; i++) {
  1395. if (!bflsc_get_temp(bflsc, i))
  1396. allok = false;
  1397. // Device is gone
  1398. if (bflsc->usbinfo.nodev)
  1399. return false;
  1400. if (i < (sc_info->sc_count - 1))
  1401. nmsleep(BFLSC_TEMP_SLEEPMS);
  1402. }
  1403. return allok;
  1404. }
  1405. static void bflsc_identify(struct cgpu_info *bflsc)
  1406. {
  1407. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
  1408. // TODO: handle x-link
  1409. sc_info->flash_led = true;
  1410. }
  1411. static bool bflsc_thread_init(struct thr_info *thr)
  1412. {
  1413. struct cgpu_info *bflsc = thr->cgpu;
  1414. if (bflsc->usbinfo.nodev)
  1415. return false;
  1416. bflsc_initialise(bflsc);
  1417. return true;
  1418. }
  1419. // there should be a new API function to return device info that isn't the standard stuff
  1420. // instead of bflsc_api_stats - since the stats should really just be internal code info
  1421. // and the new one should be UNusual device stats/extra details - like the stuff below
  1422. static struct api_data *bflsc_api_stats(struct cgpu_info *bflsc)
  1423. {
  1424. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
  1425. struct api_data *root = NULL;
  1426. //if no x-link ... etc
  1427. rd_lock(&(sc_info->stat_lock));
  1428. root = api_add_temp(root, "Temp1", &(sc_info->sc_devs[0].temp1), true);
  1429. root = api_add_temp(root, "Temp2", &(sc_info->sc_devs[0].temp2), true);
  1430. root = api_add_volts(root, "Vcc1", &(sc_info->sc_devs[0].vcc1), true);
  1431. root = api_add_volts(root, "Vcc2", &(sc_info->sc_devs[0].vcc2), true);
  1432. root = api_add_volts(root, "Vmain", &(sc_info->sc_devs[0].vmain), true);
  1433. root = api_add_temp(root, "Temp1 Max", &(sc_info->sc_devs[0].temp1_max), true);
  1434. root = api_add_temp(root, "Temp2 Max", &(sc_info->sc_devs[0].temp2_max), true);
  1435. root = api_add_time(root, "Temp1 Max Time", &(sc_info->sc_devs[0].temp1_max_time), true);
  1436. root = api_add_time(root, "Temp2 Max Time", &(sc_info->sc_devs[0].temp2_max_time), true);
  1437. rd_unlock(&(sc_info->stat_lock));
  1438. /*
  1439. else a whole lot of something like these ... etc
  1440. root = api_add_temp(root, "X-%d-Temp1", &(sc_info->temp1), false);
  1441. root = api_add_temp(root, "X-%d-Temp2", &(sc_info->temp2), false);
  1442. root = api_add_volts(root, "X-%d-Vcc1", &(sc_info->vcc1), false);
  1443. root = api_add_volts(root, "X-%d-Vcc2", &(sc_info->vcc2), false);
  1444. root = api_add_volts(root, "X-%d-Vmain", &(sc_info->vmain), false);
  1445. */
  1446. return root;
  1447. }
  1448. struct device_drv bflsc_drv = {
  1449. .drv_id = DRIVER_BFLSC,
  1450. .dname = "BitForceSC",
  1451. .name = BFLSC_SINGLE,
  1452. .drv_detect = bflsc_detect,
  1453. .get_api_stats = bflsc_api_stats,
  1454. .get_statline_before = get_bflsc_statline_before,
  1455. .get_stats = bflsc_get_stats,
  1456. .identify_device = bflsc_identify,
  1457. .thread_prepare = bflsc_thread_prepare,
  1458. .thread_init = bflsc_thread_init,
  1459. .hash_work = hash_queued_work,
  1460. .scanwork = bflsc_scanwork,
  1461. .queue_full = bflsc_queue_full,
  1462. .flush_work = bflsc_flush_work,
  1463. .thread_shutdown = bflsc_shutdown,
  1464. .thread_enable = bflsc_thread_enable
  1465. };