driver-bflsc.c 51 KB

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