driver-bflsc.c 49 KB

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