driver-bflsc.c 52 KB

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