driver-bflsc.c 51 KB

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