driver-bflsc.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  1. /*
  2. * Copyright 2013 Andrew Smith
  3. * Copyright 2013-2014 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 "config.h"
  11. #include <float.h>
  12. #include <limits.h>
  13. #include <pthread.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <strings.h>
  18. #include <sys/time.h>
  19. #include <unistd.h>
  20. #ifdef WIN32
  21. #include <windows.h>
  22. #endif
  23. #include "compat.h"
  24. #include "miner.h"
  25. #include "usbutils.h"
  26. #include "uthash.h"
  27. #include "driver-bflsc.h"
  28. int opt_bflsc_overheat = BFLSC_TEMP_OVERHEAT;
  29. static const char *blank = "";
  30. static enum driver_version drv_ver(struct cgpu_info *bflsc, const char *ver)
  31. {
  32. char *tmp;
  33. if (strstr(ver, "1.0.0"))
  34. return BFLSC_DRV1;
  35. if (strstr(ver, "1.0.") || strstr(ver, "1.1.")) {
  36. applog(LOG_WARNING, "%s detect (%s) Warning assuming firmware '%s' is Ver1",
  37. bflsc->drv->dname, bflsc->device_path, ver);
  38. return BFLSC_DRV1;
  39. }
  40. if (strstr(ver, "1.2."))
  41. return BFLSC_DRV2;
  42. tmp = str_text((char *)ver);
  43. applog(LOG_INFO, "%s detect (%s) Warning unknown firmware '%s' using Ver2",
  44. bflsc->drv->dname, bflsc->device_path, tmp);
  45. free(tmp);
  46. return BFLSC_DRV2;
  47. }
  48. static void xlinkstr(char *xlink, size_t siz, int dev, struct bflsc_info *sc_info)
  49. {
  50. if (dev > 0)
  51. snprintf(xlink, siz, " x-%d", dev);
  52. else {
  53. if (sc_info->sc_count > 1)
  54. strcpy(xlink, " master");
  55. else
  56. *xlink = '\0';
  57. }
  58. }
  59. static void bflsc_applog(struct cgpu_info *bflsc, int dev, enum usb_cmds cmd, int amount, int err)
  60. {
  61. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  62. char xlink[17];
  63. xlinkstr(xlink, sizeof(xlink), dev, sc_info);
  64. usb_applog(bflsc, cmd, xlink, amount, err);
  65. }
  66. // Break an input up into lines with LFs removed
  67. // false means an error, but if *lines > 0 then data was also found
  68. // error would be no data or missing LF at the end
  69. static bool tolines(struct cgpu_info *bflsc, int dev, char *buf, int *lines, char ***items, enum usb_cmds cmd)
  70. {
  71. bool ok = false;
  72. char *ptr;
  73. #define p_lines (*lines)
  74. #define p_items (*items)
  75. p_lines = 0;
  76. p_items = NULL;
  77. if (!buf || !(*buf)) {
  78. applog(LOG_DEBUG, "USB: %s%i: (%d) empty %s",
  79. bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
  80. return ok;
  81. }
  82. ptr = strdup(buf);
  83. while (ptr && *ptr) {
  84. p_items = realloc(p_items, ++p_lines * sizeof(*p_items));
  85. if (unlikely(!p_items))
  86. quit(1, "Failed to realloc p_items in tolines");
  87. p_items[p_lines-1] = ptr;
  88. ptr = strchr(ptr, '\n');
  89. if (ptr)
  90. *(ptr++) = '\0';
  91. else {
  92. applog(LOG_DEBUG, "USB: %s%i: (%d) missing lf(s) in %s",
  93. bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
  94. return ok;
  95. }
  96. }
  97. ok = true;
  98. return ok;
  99. }
  100. static void freetolines(int *lines, char ***items)
  101. {
  102. if (*lines > 0) {
  103. free(**items);
  104. free(*items);
  105. }
  106. *lines = 0;
  107. *items = NULL;
  108. }
  109. enum breakmode {
  110. NOCOLON,
  111. ONECOLON,
  112. ALLCOLON // Temperature uses this
  113. };
  114. // Break down a single line into 'fields'
  115. // 'lf' will be a pointer to the final LF if it is there (or NULL)
  116. // firstname will be the allocated buf copy pointer which is also
  117. // the string before ':' for ONECOLON and ALLCOLON
  118. // If any string is missing the ':' when it was expected, false is returned
  119. static bool breakdown(enum breakmode mode, char *buf, int *count, char **firstname, char ***fields, char **lf)
  120. {
  121. char *ptr, *colon, *comma;
  122. bool ok = false;
  123. #define p_count (*count)
  124. #define p_firstname (*firstname)
  125. #define p_fields (*fields)
  126. #define p_lf (*lf)
  127. p_count = 0;
  128. p_firstname = NULL;
  129. p_fields = NULL;
  130. p_lf = NULL;
  131. if (!buf || !(*buf))
  132. return ok;
  133. ptr = p_firstname = strdup(buf);
  134. p_lf = strchr(p_firstname, '\n');
  135. if (mode == ONECOLON) {
  136. colon = strchr(ptr, ':');
  137. if (colon) {
  138. ptr = colon;
  139. *(ptr++) = '\0';
  140. } else
  141. return ok;
  142. }
  143. while (ptr && *ptr) {
  144. if (mode == ALLCOLON) {
  145. colon = strchr(ptr, ':');
  146. if (colon)
  147. ptr = colon + 1;
  148. else
  149. return ok;
  150. }
  151. comma = strchr(ptr, ',');
  152. if (comma)
  153. *(comma++) = '\0';
  154. p_fields = realloc(p_fields, ++p_count * sizeof(*p_fields));
  155. if (unlikely(!p_fields))
  156. quit(1, "Failed to realloc p_fields in breakdown");
  157. p_fields[p_count-1] = ptr;
  158. ptr = comma;
  159. }
  160. ok = true;
  161. return ok;
  162. }
  163. static void freebreakdown(int *count, char **firstname, char ***fields)
  164. {
  165. if (*firstname)
  166. free(*firstname);
  167. if (*count > 0)
  168. free(*fields);
  169. *count = 0;
  170. *firstname = NULL;
  171. *fields = NULL;
  172. }
  173. static bool isokerr(int err, char *buf, int amount)
  174. {
  175. if (err < 0 || amount < (int)BFLSC_OK_LEN)
  176. return false;
  177. else {
  178. if (strstr(buf, BFLSC_ANERR)) {
  179. applog(LOG_INFO, "BFLSC not ok err: %s", buf);
  180. return false;
  181. } else
  182. return true;
  183. }
  184. }
  185. // send+receive dual stage - always single line replies
  186. static int send_recv_ds(struct cgpu_info *bflsc, int dev, int *stage, bool *sent, int *amount, char *send1, int send1_len, enum usb_cmds send1_cmd, enum usb_cmds recv1_cmd, char *send2, int send2_len, enum usb_cmds send2_cmd, enum usb_cmds recv2_cmd, char *recv, int recv_siz)
  187. {
  188. struct DataForwardToChain data;
  189. int len, err, tried;
  190. if (dev == 0) {
  191. usb_buffer_clear(bflsc);
  192. *stage = 1;
  193. *sent = false;
  194. err = usb_write(bflsc, send1, send1_len, amount, send1_cmd);
  195. if (err < 0 || *amount < send1_len)
  196. return err;
  197. *sent = true;
  198. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
  199. if (!isokerr(err, recv, *amount))
  200. return err;
  201. usb_buffer_clear(bflsc);
  202. *stage = 2;
  203. *sent = false;
  204. err = usb_write(bflsc, send2, send2_len, amount, send2_cmd);
  205. if (err < 0 || *amount < send2_len)
  206. return err;
  207. *sent = true;
  208. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
  209. return err;
  210. }
  211. data.header = BFLSC_XLINKHDR;
  212. data.deviceAddress = (uint8_t)dev;
  213. tried = 0;
  214. while (tried++ < 3) {
  215. data.payloadSize = send1_len;
  216. memcpy(data.payloadData, send1, send1_len);
  217. len = DATAFORWARDSIZE(data);
  218. usb_buffer_clear(bflsc);
  219. *stage = 1;
  220. *sent = false;
  221. err = usb_write(bflsc, (char *)&data, len, amount, send1_cmd);
  222. if (err < 0 || *amount < send1_len)
  223. return err;
  224. *sent = true;
  225. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
  226. if (err != LIBUSB_SUCCESS)
  227. return err;
  228. // x-link timeout? - try again?
  229. if (strstr(recv, BFLSC_XTIMEOUT))
  230. continue;
  231. if (!isokerr(err, recv, *amount))
  232. return err;
  233. data.payloadSize = send2_len;
  234. memcpy(data.payloadData, send2, send2_len);
  235. len = DATAFORWARDSIZE(data);
  236. usb_buffer_clear(bflsc);
  237. *stage = 2;
  238. *sent = false;
  239. err = usb_write(bflsc, (char *)&data, len, amount, send2_cmd);
  240. if (err < 0 || *amount < send2_len)
  241. return err;
  242. *sent = true;
  243. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
  244. if (err != LIBUSB_SUCCESS)
  245. return err;
  246. // x-link timeout? - try again?
  247. if (strstr(recv, BFLSC_XTIMEOUT))
  248. continue;
  249. // SUCCESS - return it
  250. break;
  251. }
  252. return err;
  253. }
  254. #define READ_OK true
  255. #define READ_NL false
  256. // send+receive single stage
  257. static int send_recv_ss(struct cgpu_info *bflsc, int dev, bool *sent, int *amount, char *send, int send_len, enum usb_cmds send_cmd, char *recv, int recv_siz, enum usb_cmds recv_cmd, bool read_ok)
  258. {
  259. struct DataForwardToChain data;
  260. int len, err, tried;
  261. if (dev == 0) {
  262. usb_buffer_clear(bflsc);
  263. *sent = false;
  264. err = usb_write(bflsc, send, send_len, amount, send_cmd);
  265. if (err < 0 || *amount < send_len) {
  266. // N.B. thus !(*sent) directly implies err < 0 or *amount < send_len
  267. return err;
  268. }
  269. *sent = true;
  270. if (read_ok == READ_OK)
  271. err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
  272. else
  273. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
  274. return err;
  275. }
  276. data.header = BFLSC_XLINKHDR;
  277. data.deviceAddress = (uint8_t)dev;
  278. data.payloadSize = send_len;
  279. memcpy(data.payloadData, send, send_len);
  280. len = DATAFORWARDSIZE(data);
  281. tried = 0;
  282. while (tried++ < 3) {
  283. usb_buffer_clear(bflsc);
  284. *sent = false;
  285. err = usb_write(bflsc, (char *)&data, len, amount, recv_cmd);
  286. if (err < 0 || *amount < send_len)
  287. return err;
  288. *sent = true;
  289. if (read_ok == READ_OK)
  290. err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
  291. else
  292. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
  293. if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_TIMEOUT)
  294. return err;
  295. // read_ok can err timeout if it's looking for OK<LF>
  296. // TODO: add a usb_read() option to spot the ERR: and convert end=OK<LF> to just <LF>
  297. // x-link timeout? - try again?
  298. if ((err == LIBUSB_SUCCESS || (read_ok == READ_OK && err == LIBUSB_ERROR_TIMEOUT)) &&
  299. strstr(recv, BFLSC_XTIMEOUT))
  300. continue;
  301. // SUCCESS or TIMEOUT - return it
  302. break;
  303. }
  304. return err;
  305. }
  306. static int write_to_dev(struct cgpu_info *bflsc, int dev, char *buf, int buflen, int *amount, enum usb_cmds cmd)
  307. {
  308. struct DataForwardToChain data;
  309. int len;
  310. /*
  311. * The protocol is syncronous so any previous excess can be
  312. * discarded and assumed corrupt data or failed USB transfers
  313. */
  314. usb_buffer_clear(bflsc);
  315. if (dev == 0)
  316. return usb_write(bflsc, buf, buflen, amount, cmd);
  317. data.header = BFLSC_XLINKHDR;
  318. data.deviceAddress = (uint8_t)dev;
  319. data.payloadSize = buflen;
  320. memcpy(data.payloadData, buf, buflen);
  321. len = DATAFORWARDSIZE(data);
  322. return usb_write(bflsc, (char *)&data, len, amount, cmd);
  323. }
  324. static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
  325. {
  326. char buf[BFLSC_BUFSIZ+1];
  327. int err, amount;
  328. bool sent;
  329. // Device is gone
  330. if (bflsc->usbinfo.nodev)
  331. return;
  332. mutex_lock(&bflsc->device_mutex);
  333. err = send_recv_ss(bflsc, dev, &sent, &amount,
  334. BFLSC_QFLUSH, BFLSC_QFLUSH_LEN, C_QUEFLUSH,
  335. buf, sizeof(buf)-1, C_QUEFLUSHREPLY, READ_NL);
  336. mutex_unlock(&bflsc->device_mutex);
  337. if (!sent)
  338. bflsc_applog(bflsc, dev, C_QUEFLUSH, amount, err);
  339. else {
  340. // TODO: do we care if we don't get 'OK'? (always will in normal processing)
  341. }
  342. }
  343. /* return True = attempted usb_read_ok()
  344. * set ignore to true means no applog/ignore errors */
  345. static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore)
  346. {
  347. bool readok = false;
  348. mutex_lock(&(bflsc->device_mutex));
  349. *err = send_recv_ss(bflsc, dev, &readok, amount,
  350. BFLSC_QRES, BFLSC_QRES_LEN, C_REQUESTRESULTS,
  351. buf, bufsiz-1, C_GETRESULTS, READ_OK);
  352. mutex_unlock(&(bflsc->device_mutex));
  353. if (!readok) {
  354. if (!ignore)
  355. bflsc_applog(bflsc, dev, C_REQUESTRESULTS, *amount, *err);
  356. // TODO: do what? flag as dead device?
  357. // count how many times it has happened and reset/fail it
  358. // or even make sure it is all x-link and that means device
  359. // has failed after some limit of this?
  360. // of course all other I/O must also be failing ...
  361. } else {
  362. if (*err < 0 || *amount < 1) {
  363. if (!ignore)
  364. bflsc_applog(bflsc, dev, C_GETRESULTS, *amount, *err);
  365. // TODO: do what? ... see above
  366. }
  367. }
  368. return readok;
  369. }
  370. static void __bflsc_initialise(struct cgpu_info *bflsc)
  371. {
  372. int err, interface;
  373. // TODO: does x-link bypass the other device FTDI? (I think it does)
  374. // So no initialisation required except for the master device?
  375. if (bflsc->usbinfo.nodev)
  376. return;
  377. interface = usb_interface(bflsc);
  378. // Reset
  379. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  380. FTDI_VALUE_RESET, interface, C_RESET);
  381. applog(LOG_DEBUG, "%s%i: reset got err %d",
  382. bflsc->drv->name, bflsc->device_id, err);
  383. if (bflsc->usbinfo.nodev)
  384. return;
  385. usb_ftdi_set_latency(bflsc);
  386. if (bflsc->usbinfo.nodev)
  387. return;
  388. // Set data control
  389. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  390. FTDI_VALUE_DATA_BAS, interface, C_SETDATA);
  391. applog(LOG_DEBUG, "%s%i: setdata got err %d",
  392. bflsc->drv->name, bflsc->device_id, err);
  393. if (bflsc->usbinfo.nodev)
  394. return;
  395. // Set the baud
  396. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BAS,
  397. (FTDI_INDEX_BAUD_BAS & 0xff00) | interface,
  398. C_SETBAUD);
  399. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  400. bflsc->drv->name, bflsc->device_id, err);
  401. if (bflsc->usbinfo.nodev)
  402. return;
  403. // Set Flow Control
  404. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  405. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  406. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  407. bflsc->drv->name, bflsc->device_id, err);
  408. if (bflsc->usbinfo.nodev)
  409. return;
  410. // Set Modem Control
  411. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  412. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  413. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  414. bflsc->drv->name, bflsc->device_id, err);
  415. if (bflsc->usbinfo.nodev)
  416. return;
  417. // Clear any sent data
  418. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  419. FTDI_VALUE_PURGE_TX, interface, C_PURGETX);
  420. applog(LOG_DEBUG, "%s%i: purgetx got err %d",
  421. bflsc->drv->name, bflsc->device_id, err);
  422. if (bflsc->usbinfo.nodev)
  423. return;
  424. // Clear any received data
  425. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  426. FTDI_VALUE_PURGE_RX, interface, C_PURGERX);
  427. applog(LOG_DEBUG, "%s%i: purgerx got err %d",
  428. bflsc->drv->name, bflsc->device_id, err);
  429. if (!bflsc->cutofftemp)
  430. bflsc->cutofftemp = opt_bflsc_overheat;
  431. }
  432. static void bflsc_initialise(struct cgpu_info *bflsc)
  433. {
  434. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  435. char buf[BFLSC_BUFSIZ+1];
  436. int err, amount;
  437. int dev;
  438. mutex_lock(&(bflsc->device_mutex));
  439. __bflsc_initialise(bflsc);
  440. mutex_unlock(&(bflsc->device_mutex));
  441. for (dev = 0; dev < sc_info->sc_count; dev++) {
  442. bflsc_send_flush_work(bflsc, dev);
  443. bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, true);
  444. }
  445. }
  446. static bool getinfo(struct cgpu_info *bflsc, int dev)
  447. {
  448. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  449. struct bflsc_dev sc_dev;
  450. char buf[BFLSC_BUFSIZ+1];
  451. int err, amount;
  452. char **items, *firstname, **fields, *lf;
  453. bool res, ok = false;
  454. int i, lines, count;
  455. char *tmp;
  456. /*
  457. * Kano's first dev Jalapeno output:
  458. * DEVICE: BitFORCE SC<LF>
  459. * FIRMWARE: 1.0.0<LF>
  460. * ENGINES: 30<LF>
  461. * FREQUENCY: [UNKNOWN]<LF>
  462. * XLINK MODE: MASTER<LF>
  463. * XLINK PRESENT: YES<LF>
  464. * --DEVICES IN CHAIN: 0<LF>
  465. * --CHAIN PRESENCE MASK: 00000000<LF>
  466. * OK<LF>
  467. */
  468. /*
  469. * Don't use send_recv_ss() since we have a different receive timeout
  470. * Also getinfo() is called multiple times if it fails anyway
  471. */
  472. err = write_to_dev(bflsc, dev, BFLSC_DETAILS, BFLSC_DETAILS_LEN, &amount, C_REQUESTDETAILS);
  473. if (err < 0 || amount != BFLSC_DETAILS_LEN) {
  474. applog(LOG_ERR, "%s detect (%s) send details request failed (%d:%d)",
  475. bflsc->drv->dname, bflsc->device_path, amount, err);
  476. return ok;
  477. }
  478. err = usb_read_ok_timeout(bflsc, buf, sizeof(buf)-1, &amount,
  479. BFLSC_INFO_TIMEOUT, C_GETDETAILS);
  480. if (err < 0 || amount < 1) {
  481. if (err < 0) {
  482. applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)",
  483. bflsc->drv->dname, bflsc->device_path, amount, err);
  484. } else {
  485. applog(LOG_ERR, "%s detect (%s) get details returned nothing (%d:%d)",
  486. bflsc->drv->dname, bflsc->device_path, amount, err);
  487. }
  488. return ok;
  489. }
  490. memset(&sc_dev, 0, sizeof(struct bflsc_dev));
  491. sc_info->sc_count = 1;
  492. res = tolines(bflsc, dev, &(buf[0]), &lines, &items, C_GETDETAILS);
  493. if (!res)
  494. return ok;
  495. tmp = str_text(buf);
  496. strncpy(sc_dev.getinfo, tmp, sizeof(sc_dev.getinfo));
  497. sc_dev.getinfo[sizeof(sc_dev.getinfo)-1] = '\0';
  498. free(tmp);
  499. for (i = 0; i < lines-2; i++) {
  500. res = breakdown(ONECOLON, items[i], &count, &firstname, &fields, &lf);
  501. if (lf)
  502. *lf = '\0';
  503. if (!res || count != 1) {
  504. tmp = str_text(items[i]);
  505. applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
  506. "%s detect (%s) invalid details line: '%s' %d",
  507. bflsc->drv->dname, bflsc->device_path, tmp, count);
  508. free(tmp);
  509. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  510. goto mata;
  511. }
  512. if (strstr(firstname, BFLSC_DI_FIRMWARE)) {
  513. sc_dev.firmware = strdup(fields[0]);
  514. sc_info->driver_version = drv_ver(bflsc, sc_dev.firmware);
  515. }
  516. else if (Strcasestr(firstname, BFLSC_DI_ENGINES)) {
  517. sc_dev.engines = atoi(fields[0]);
  518. if (sc_dev.engines < 1) {
  519. tmp = str_text(items[i]);
  520. applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
  521. "%s detect (%s) invalid engine count: '%s'",
  522. bflsc->drv->dname, bflsc->device_path, tmp);
  523. free(tmp);
  524. goto mata;
  525. }
  526. }
  527. else if (strstr(firstname, BFLSC_DI_XLINKMODE))
  528. sc_dev.xlink_mode = strdup(fields[0]);
  529. else if (strstr(firstname, BFLSC_DI_XLINKPRESENT))
  530. sc_dev.xlink_present = strdup(fields[0]);
  531. else if (strstr(firstname, BFLSC_DI_DEVICESINCHAIN)) {
  532. if (fields[0][0] == '0' ||
  533. (fields[0][0] == ' ' && fields[0][1] == '0'))
  534. sc_info->sc_count = 1;
  535. else
  536. sc_info->sc_count = atoi(fields[0]);
  537. if (sc_info->sc_count < 1 || sc_info->sc_count > 30) {
  538. tmp = str_text(items[i]);
  539. applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
  540. "%s detect (%s) invalid x-link count: '%s'",
  541. bflsc->drv->dname, bflsc->device_path, tmp);
  542. free(tmp);
  543. goto mata;
  544. }
  545. }
  546. else if (strstr(firstname, BFLSC_DI_CHIPS))
  547. sc_dev.chips = strdup(fields[0]);
  548. else if (strstr(firstname, BFLSC28_DI_ASICS))
  549. sc_dev.chips = strdup(fields[0]);
  550. freebreakdown(&count, &firstname, &fields);
  551. }
  552. if (sc_info->driver_version == BFLSC_DRVUNDEF) {
  553. applog(LOG_WARNING, "%s detect (%s) missing %s",
  554. bflsc->drv->dname, bflsc->device_path, BFLSC_DI_FIRMWARE);
  555. goto ne;
  556. }
  557. sc_info->sc_devs = calloc(sc_info->sc_count, sizeof(struct bflsc_dev));
  558. if (unlikely(!sc_info->sc_devs))
  559. quit(1, "Failed to calloc in getinfo");
  560. memcpy(&(sc_info->sc_devs[0]), &sc_dev, sizeof(sc_dev));
  561. // TODO: do we care about getting this info for the rest if > 0 x-link
  562. ok = true;
  563. goto ne;
  564. mata:
  565. freebreakdown(&count, &firstname, &fields);
  566. ok = false;
  567. ne:
  568. freetolines(&lines, &items);
  569. return ok;
  570. }
  571. static bool bflsc28_queue_full(struct cgpu_info *bflsc);
  572. static struct cgpu_info *bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  573. {
  574. struct bflsc_info *sc_info = NULL;
  575. char buf[BFLSC_BUFSIZ+1];
  576. int i, err, amount;
  577. struct timeval init_start, init_now;
  578. int init_sleep, init_count;
  579. bool ident_first, sent;
  580. char *newname;
  581. uint16_t latency;
  582. struct cgpu_info *bflsc = usb_alloc_cgpu(&bflsc_drv, 1);
  583. sc_info = calloc(1, sizeof(*sc_info));
  584. if (unlikely(!sc_info))
  585. quit(1, "Failed to calloc sc_info in bflsc_detect_one");
  586. // TODO: fix ... everywhere ...
  587. bflsc->device_data = (FILE *)sc_info;
  588. if (!usb_init(bflsc, dev, found))
  589. goto shin;
  590. // Allow 2 complete attempts if the 1st time returns an unrecognised reply
  591. ident_first = true;
  592. retry:
  593. init_count = 0;
  594. init_sleep = REINIT_TIME_FIRST_MS;
  595. cgtime(&init_start);
  596. reinit:
  597. __bflsc_initialise(bflsc);
  598. err = send_recv_ss(bflsc, 0, &sent, &amount,
  599. BFLSC_IDENTIFY, BFLSC_IDENTIFY_LEN, C_REQUESTIDENTIFY,
  600. buf, sizeof(buf)-1, C_GETIDENTIFY, READ_NL);
  601. if (!sent) {
  602. applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
  603. bflsc->drv->dname, bflsc->device_path, amount, err);
  604. goto unshin;
  605. }
  606. if (err < 0 || amount < 1) {
  607. init_count++;
  608. cgtime(&init_now);
  609. if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
  610. if (init_count == 2) {
  611. applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying",
  612. bflsc->drv->dname, bflsc->device_path, amount, err);
  613. }
  614. cgsleep_ms(init_sleep);
  615. if ((init_sleep * 2) <= REINIT_TIME_MAX_MS)
  616. init_sleep *= 2;
  617. goto reinit;
  618. }
  619. if (init_count > 0)
  620. applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs",
  621. bflsc->drv->dname, bflsc->device_path, init_count, tdiff(&init_now, &init_start));
  622. if (err < 0) {
  623. applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
  624. bflsc->drv->dname, bflsc->device_path, amount, err);
  625. } else {
  626. applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
  627. bflsc->drv->dname, bflsc->device_path, amount);
  628. }
  629. goto unshin;
  630. }
  631. buf[amount] = '\0';
  632. if (unlikely(!strstr(buf, BFLSC_BFLSC) && !strstr(buf, BFLSC_BFLSC28))) {
  633. applog(LOG_DEBUG, "%s detect (%s) found an FPGA '%s' ignoring",
  634. bflsc->drv->dname, bflsc->device_path, buf);
  635. goto unshin;
  636. }
  637. if (unlikely(strstr(buf, BFLSC_IDENTITY))) {
  638. if (ident_first) {
  639. applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' trying again ...",
  640. bflsc->drv->dname, bflsc->device_path, buf);
  641. ident_first = false;
  642. goto retry;
  643. }
  644. applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' on 2nd attempt",
  645. bflsc->drv->dname, bflsc->device_path, buf);
  646. goto unshin;
  647. }
  648. int tries = 0;
  649. while (7734) {
  650. if (getinfo(bflsc, 0))
  651. break;
  652. // N.B. we will get displayed errors each time it fails
  653. if (++tries > 2)
  654. goto unshin;
  655. cgsleep_ms(40);
  656. }
  657. switch (sc_info->driver_version) {
  658. case BFLSC_DRV1:
  659. sc_info->que_size = BFLSC_QUE_SIZE_V1;
  660. sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V1;
  661. sc_info->que_watermark = BFLSC_QUE_WATERMARK_V1;
  662. sc_info->que_low = BFLSC_QUE_LOW_V1;
  663. sc_info->que_noncecount = QUE_NONCECOUNT_V1;
  664. sc_info->que_fld_min = QUE_FLD_MIN_V1;
  665. sc_info->que_fld_max = QUE_FLD_MAX_V1;
  666. // Only Jalapeno uses 1.0.0
  667. sc_info->flush_size = 1;
  668. break;
  669. case BFLSC_DRV2:
  670. case BFLSC_DRVUNDEF:
  671. default:
  672. sc_info->driver_version = BFLSC_DRV2;
  673. sc_info->que_size = BFLSC_QUE_SIZE_V2;
  674. sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V2;
  675. sc_info->que_watermark = BFLSC_QUE_WATERMARK_V2;
  676. sc_info->que_low = BFLSC_QUE_LOW_V2;
  677. sc_info->que_noncecount = QUE_NONCECOUNT_V2;
  678. sc_info->que_fld_min = QUE_FLD_MIN_V2;
  679. sc_info->que_fld_max = QUE_FLD_MAX_V2;
  680. // TODO: this can be reduced to total chip count
  681. sc_info->flush_size = 16 * sc_info->sc_count;
  682. break;
  683. }
  684. // Set parallelization based on the getinfo() response if it is present
  685. if (sc_info->sc_devs[0].chips && strlen(sc_info->sc_devs[0].chips)) {
  686. if (strstr(sc_info->sc_devs[0].chips, BFLSC_DI_CHIPS_PARALLEL)) {
  687. sc_info->que_noncecount = QUE_NONCECOUNT_V2;
  688. sc_info->que_fld_min = QUE_FLD_MIN_V2;
  689. sc_info->que_fld_max = QUE_FLD_MAX_V2;
  690. } else {
  691. sc_info->que_noncecount = QUE_NONCECOUNT_V1;
  692. sc_info->que_fld_min = QUE_FLD_MIN_V1;
  693. sc_info->que_fld_max = QUE_FLD_MAX_V1;
  694. }
  695. }
  696. sc_info->scan_sleep_time = BAS_SCAN_TIME;
  697. sc_info->results_sleep_time = BFLSC_RES_TIME;
  698. sc_info->default_ms_work = BAS_WORK_TIME;
  699. latency = BAS_LATENCY;
  700. /* When getinfo() "FREQUENCY: [UNKNOWN]" is fixed -
  701. * use 'freq * engines' to estimate.
  702. * Otherwise for now: */
  703. newname = NULL;
  704. if (sc_info->sc_count > 1) {
  705. newname = BFLSC_MINIRIG;
  706. sc_info->scan_sleep_time = BAM_SCAN_TIME;
  707. sc_info->default_ms_work = BAM_WORK_TIME;
  708. bflsc->usbdev->ident = IDENT_BAM;
  709. latency = BAM_LATENCY;
  710. } else {
  711. if (sc_info->sc_devs[0].engines < 34) { // 16 * 2 + 2
  712. newname = BFLSC_JALAPENO;
  713. sc_info->scan_sleep_time = BAJ_SCAN_TIME;
  714. sc_info->default_ms_work = BAJ_WORK_TIME;
  715. bflsc->usbdev->ident = IDENT_BAJ;
  716. latency = BAJ_LATENCY;
  717. } else if (sc_info->sc_devs[0].engines < 130) { // 16 * 8 + 2
  718. newname = BFLSC_LITTLESINGLE;
  719. sc_info->scan_sleep_time = BAL_SCAN_TIME;
  720. sc_info->default_ms_work = BAL_WORK_TIME;
  721. bflsc->usbdev->ident = IDENT_BAL;
  722. latency = BAL_LATENCY;
  723. }
  724. }
  725. sc_info->ident = usb_ident(bflsc);
  726. if (sc_info->ident == IDENT_BMA) {
  727. bflsc->drv->queue_full = &bflsc28_queue_full;
  728. sc_info->scan_sleep_time = BMA_SCAN_TIME;
  729. sc_info->default_ms_work = BMA_WORK_TIME;
  730. sc_info->results_sleep_time = BMA_RES_TIME;
  731. }
  732. if (latency != bflsc->usbdev->found->latency) {
  733. bflsc->usbdev->found->latency = latency;
  734. usb_ftdi_set_latency(bflsc);
  735. }
  736. for (i = 0; i < sc_info->sc_count; i++)
  737. sc_info->sc_devs[i].ms_work = sc_info->default_ms_work;
  738. if (newname) {
  739. if (!bflsc->drv->copy)
  740. bflsc->drv = copy_drv(bflsc->drv);
  741. bflsc->drv->name = newname;
  742. }
  743. // We have a real BFLSC!
  744. applog(LOG_DEBUG, "%s (%s) identified as: '%s'",
  745. bflsc->drv->dname, bflsc->device_path, bflsc->drv->name);
  746. if (!add_cgpu(bflsc))
  747. goto unshin;
  748. update_usb_stats(bflsc);
  749. mutex_init(&bflsc->device_mutex);
  750. rwlock_init(&sc_info->stat_lock);
  751. return bflsc;
  752. unshin:
  753. usb_uninit(bflsc);
  754. shin:
  755. free(bflsc->device_data);
  756. bflsc->device_data = NULL;
  757. if (bflsc->name != blank) {
  758. free(bflsc->name);
  759. bflsc->name = NULL;
  760. }
  761. bflsc = usb_free_cgpu(bflsc);
  762. return NULL;
  763. }
  764. static void bflsc_detect(bool __maybe_unused hotplug)
  765. {
  766. usb_detect(&bflsc_drv, bflsc_detect_one);
  767. }
  768. static void get_bflsc_statline_before(char *buf, size_t bufsiz, struct cgpu_info *bflsc)
  769. {
  770. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  771. float temp = 0;
  772. float vcc2 = 0;
  773. int i;
  774. rd_lock(&(sc_info->stat_lock));
  775. for (i = 0; i < sc_info->sc_count; i++) {
  776. if (sc_info->sc_devs[i].temp1 > temp)
  777. temp = sc_info->sc_devs[i].temp1;
  778. if (sc_info->sc_devs[i].temp2 > temp)
  779. temp = sc_info->sc_devs[i].temp2;
  780. if (sc_info->sc_devs[i].vcc2 > vcc2)
  781. vcc2 = sc_info->sc_devs[i].vcc2;
  782. }
  783. rd_unlock(&(sc_info->stat_lock));
  784. tailsprintf(buf, bufsiz, "max%3.0fC %4.2fV", temp, vcc2);
  785. }
  786. static void flush_one_dev(struct cgpu_info *bflsc, int dev)
  787. {
  788. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  789. struct work *work, *tmp;
  790. bool did = false;
  791. bflsc_send_flush_work(bflsc, dev);
  792. rd_lock(&bflsc->qlock);
  793. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  794. if (work->subid == dev) {
  795. // devflag is used to flag stale work
  796. work->devflag = true;
  797. did = true;
  798. }
  799. }
  800. rd_unlock(&bflsc->qlock);
  801. if (did) {
  802. wr_lock(&(sc_info->stat_lock));
  803. sc_info->sc_devs[dev].flushed = true;
  804. sc_info->sc_devs[dev].flush_id = sc_info->sc_devs[dev].result_id;
  805. sc_info->sc_devs[dev].work_queued = 0;
  806. wr_unlock(&(sc_info->stat_lock));
  807. }
  808. }
  809. static void bflsc_flush_work(struct cgpu_info *bflsc)
  810. {
  811. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  812. int dev;
  813. for (dev = 0; dev < sc_info->sc_count; dev++)
  814. flush_one_dev(bflsc, dev);
  815. }
  816. static void bflsc_set_volt(struct cgpu_info *bflsc, int dev)
  817. {
  818. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  819. char buf[BFLSC_BUFSIZ+1];
  820. char msg[16];
  821. int err, amount;
  822. bool sent;
  823. // Device is gone
  824. if (bflsc->usbinfo.nodev)
  825. return;
  826. snprintf(msg, sizeof(msg), "V%dX", sc_info->volt_next);
  827. mutex_lock(&bflsc->device_mutex);
  828. err = send_recv_ss(bflsc, dev, &sent, &amount,
  829. msg, strlen(msg), C_SETVOLT,
  830. buf, sizeof(buf)-1, C_REPLYSETVOLT, READ_NL);
  831. mutex_unlock(&(bflsc->device_mutex));
  832. if (!sent)
  833. bflsc_applog(bflsc, dev, C_SETVOLT, amount, err);
  834. else {
  835. // Don't care
  836. }
  837. sc_info->volt_next_stat = false;
  838. return;
  839. }
  840. static void bflsc_set_clock(struct cgpu_info *bflsc, int dev)
  841. {
  842. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  843. char buf[BFLSC_BUFSIZ+1];
  844. char msg[16];
  845. int err, amount;
  846. bool sent;
  847. // Device is gone
  848. if (bflsc->usbinfo.nodev)
  849. return;
  850. snprintf(msg, sizeof(msg), "F%XX", sc_info->clock_next);
  851. mutex_lock(&bflsc->device_mutex);
  852. err = send_recv_ss(bflsc, dev, &sent, &amount,
  853. msg, strlen(msg), C_SETCLOCK,
  854. buf, sizeof(buf)-1, C_REPLYSETCLOCK, READ_NL);
  855. mutex_unlock(&(bflsc->device_mutex));
  856. if (!sent)
  857. bflsc_applog(bflsc, dev, C_SETCLOCK, amount, err);
  858. else {
  859. // Don't care
  860. }
  861. sc_info->clock_next_stat = false;
  862. return;
  863. }
  864. static void bflsc_flash_led(struct cgpu_info *bflsc, int dev)
  865. {
  866. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  867. char buf[BFLSC_BUFSIZ+1];
  868. int err, amount;
  869. bool sent;
  870. // Device is gone
  871. if (bflsc->usbinfo.nodev)
  872. return;
  873. // It is not critical flashing the led so don't get stuck if we
  874. // can't grab the mutex now
  875. if (mutex_trylock(&bflsc->device_mutex))
  876. return;
  877. err = send_recv_ss(bflsc, dev, &sent, &amount,
  878. BFLSC_FLASH, BFLSC_FLASH_LEN, C_REQUESTFLASH,
  879. buf, sizeof(buf)-1, C_FLASHREPLY, READ_NL);
  880. mutex_unlock(&(bflsc->device_mutex));
  881. if (!sent)
  882. bflsc_applog(bflsc, dev, C_REQUESTFLASH, amount, err);
  883. else {
  884. // Don't care
  885. }
  886. // Once we've tried - don't do it until told to again
  887. // - even if it failed
  888. sc_info->flash_led = false;
  889. return;
  890. }
  891. /* Flush and stop all work if the device reaches the thermal cutoff temp, or
  892. * temporarily stop queueing work if it's in the throttling range. */
  893. static void bflsc_manage_temp(struct cgpu_info *bflsc, struct bflsc_dev *sc_dev,
  894. int dev, float temp)
  895. {
  896. bflsc->temp = temp;
  897. if (bflsc->cutofftemp > 0) {
  898. int cutoff = bflsc->cutofftemp;
  899. int throttle = cutoff - BFLSC_TEMP_THROTTLE;
  900. int recover = cutoff - BFLSC_TEMP_RECOVER;
  901. if (sc_dev->overheat) {
  902. if (temp < recover)
  903. sc_dev->overheat = false;
  904. } else if (temp > throttle) {
  905. sc_dev->overheat = true;
  906. if (temp > cutoff) {
  907. applog(LOG_WARNING, "%s%i: temp (%.1f) hit thermal cutoff limit %d, stopping work!",
  908. bflsc->drv->name, bflsc->device_id, temp, cutoff);
  909. dev_error(bflsc, REASON_DEV_THERMAL_CUTOFF);
  910. flush_one_dev(bflsc, dev);
  911. } else {
  912. applog(LOG_NOTICE, "%s%i: temp (%.1f) hit thermal throttle limit %d, throttling",
  913. bflsc->drv->name, bflsc->device_id, temp, throttle);
  914. }
  915. }
  916. }
  917. }
  918. static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
  919. {
  920. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  921. struct bflsc_dev *sc_dev;
  922. char temp_buf[BFLSC_BUFSIZ+1];
  923. char volt_buf[BFLSC_BUFSIZ+1];
  924. char *tmp;
  925. int err, amount;
  926. char *firstname, **fields, *lf;
  927. char xlink[17];
  928. int count;
  929. bool res, sent;
  930. float temp, temp1, temp2;
  931. float vcc1, vcc2, vmain;
  932. // Device is gone
  933. if (bflsc->usbinfo.nodev)
  934. return false;
  935. if (dev >= sc_info->sc_count) {
  936. applog(LOG_ERR, "%s%i: temp invalid xlink device %d - limit %d",
  937. bflsc->drv->name, bflsc->device_id, dev, sc_info->sc_count - 1);
  938. return false;
  939. }
  940. if (sc_info->volt_next_stat || sc_info->clock_next_stat) {
  941. if (sc_info->volt_next_stat)
  942. bflsc_set_volt(bflsc, dev);
  943. if (sc_info->clock_next_stat)
  944. bflsc_set_clock(bflsc, dev);
  945. return true;
  946. }
  947. // Flash instead of Temp
  948. if (sc_info->flash_led) {
  949. bflsc_flash_led(bflsc, dev);
  950. return true;
  951. }
  952. xlinkstr(xlink, sizeof(xlink), dev, sc_info);
  953. /* It is not very critical getting temp so don't get stuck if we
  954. * can't grab the mutex here */
  955. if (mutex_trylock(&bflsc->device_mutex))
  956. return false;
  957. err = send_recv_ss(bflsc, dev, &sent, &amount,
  958. BFLSC_TEMPERATURE, BFLSC_TEMPERATURE_LEN, C_REQUESTTEMPERATURE,
  959. temp_buf, sizeof(temp_buf)-1, C_GETTEMPERATURE, READ_NL);
  960. mutex_unlock(&(bflsc->device_mutex));
  961. if (!sent) {
  962. applog(LOG_ERR, "%s%i: Error: Request%s temp invalid/timed out (%d:%d)",
  963. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  964. return false;
  965. } else {
  966. if (err < 0 || amount < 1) {
  967. if (err < 0) {
  968. applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)",
  969. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  970. } else {
  971. applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)",
  972. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  973. }
  974. return false;
  975. }
  976. }
  977. // Ignore it if we can't get the V
  978. if (mutex_trylock(&bflsc->device_mutex))
  979. return false;
  980. err = send_recv_ss(bflsc, dev, &sent, &amount,
  981. BFLSC_VOLTAGE, BFLSC_VOLTAGE_LEN, C_REQUESTVOLTS,
  982. volt_buf, sizeof(volt_buf)-1, C_GETVOLTS, READ_NL);
  983. mutex_unlock(&(bflsc->device_mutex));
  984. if (!sent) {
  985. applog(LOG_ERR, "%s%i: Error: Request%s volts invalid/timed out (%d:%d)",
  986. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  987. return false;
  988. } else {
  989. if (err < 0 || amount < 1) {
  990. if (err < 0) {
  991. applog(LOG_ERR, "%s%i: Error: Get%s volt return invalid/timed out (%d:%d)",
  992. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  993. } else {
  994. applog(LOG_ERR, "%s%i: Error: Get%s volt returned nothing (%d:%d)",
  995. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  996. }
  997. return false;
  998. }
  999. }
  1000. res = breakdown(ALLCOLON, temp_buf, &count, &firstname, &fields, &lf);
  1001. if (lf)
  1002. *lf = '\0';
  1003. if (!res || count < 2 || !lf) {
  1004. tmp = str_text(temp_buf);
  1005. applog(LOG_WARNING, "%s%i: Invalid%s temp reply: '%s'",
  1006. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1007. free(tmp);
  1008. freebreakdown(&count, &firstname, &fields);
  1009. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  1010. return false;
  1011. }
  1012. temp = temp1 = (float)atoi(fields[0]);
  1013. temp2 = (float)atoi(fields[1]);
  1014. freebreakdown(&count, &firstname, &fields);
  1015. res = breakdown(NOCOLON, volt_buf, &count, &firstname, &fields, &lf);
  1016. if (lf)
  1017. *lf = '\0';
  1018. if (!res || count != 3 || !lf) {
  1019. tmp = str_text(volt_buf);
  1020. applog(LOG_WARNING, "%s%i: Invalid%s volt reply: '%s'",
  1021. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1022. free(tmp);
  1023. freebreakdown(&count, &firstname, &fields);
  1024. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  1025. return false;
  1026. }
  1027. sc_dev = &sc_info->sc_devs[dev];
  1028. vcc1 = (float)atoi(fields[0]) / 1000.0;
  1029. vcc2 = (float)atoi(fields[1]) / 1000.0;
  1030. vmain = (float)atoi(fields[2]) / 1000.0;
  1031. freebreakdown(&count, &firstname, &fields);
  1032. if (vcc1 > 0 || vcc2 > 0 || vmain > 0) {
  1033. wr_lock(&(sc_info->stat_lock));
  1034. if (vcc1 > 0) {
  1035. if (unlikely(sc_dev->vcc1 == 0))
  1036. sc_dev->vcc1 = vcc1;
  1037. else {
  1038. sc_dev->vcc1 += vcc1 * 0.63;
  1039. sc_dev->vcc1 /= 1.63;
  1040. }
  1041. }
  1042. if (vcc2 > 0) {
  1043. if (unlikely(sc_dev->vcc2 == 0))
  1044. sc_dev->vcc2 = vcc2;
  1045. else {
  1046. sc_dev->vcc2 += vcc2 * 0.63;
  1047. sc_dev->vcc2 /= 1.63;
  1048. }
  1049. }
  1050. if (vmain > 0) {
  1051. if (unlikely(sc_dev->vmain == 0))
  1052. sc_dev->vmain = vmain;
  1053. else {
  1054. sc_dev->vmain += vmain * 0.63;
  1055. sc_dev->vmain /= 1.63;
  1056. }
  1057. }
  1058. wr_unlock(&(sc_info->stat_lock));
  1059. }
  1060. if (temp1 > 0 || temp2 > 0) {
  1061. wr_lock(&(sc_info->stat_lock));
  1062. if (unlikely(!sc_dev->temp1))
  1063. sc_dev->temp1 = temp1;
  1064. else {
  1065. sc_dev->temp1 += temp1 * 0.63;
  1066. sc_dev->temp1 /= 1.63;
  1067. }
  1068. if (unlikely(!sc_dev->temp2))
  1069. sc_dev->temp2 = temp2;
  1070. else {
  1071. sc_dev->temp2 += temp2 * 0.63;
  1072. sc_dev->temp2 /= 1.63;
  1073. }
  1074. if (temp1 > sc_dev->temp1_max) {
  1075. sc_dev->temp1_max = temp1;
  1076. sc_dev->temp1_max_time = time(NULL);
  1077. }
  1078. if (temp2 > sc_dev->temp2_max) {
  1079. sc_dev->temp2_max = temp2;
  1080. sc_dev->temp2_max_time = time(NULL);
  1081. }
  1082. if (unlikely(sc_dev->temp1_5min_av == 0))
  1083. sc_dev->temp1_5min_av = temp1;
  1084. else {
  1085. sc_dev->temp1_5min_av += temp1 * .0042;
  1086. sc_dev->temp1_5min_av /= 1.0042;
  1087. }
  1088. if (unlikely(sc_dev->temp2_5min_av == 0))
  1089. sc_dev->temp2_5min_av = temp2;
  1090. else {
  1091. sc_dev->temp2_5min_av += temp2 * .0042;
  1092. sc_dev->temp2_5min_av /= 1.0042;
  1093. }
  1094. wr_unlock(&(sc_info->stat_lock));
  1095. if (temp < temp2)
  1096. temp = temp2;
  1097. bflsc_manage_temp(bflsc, sc_dev, dev, temp);
  1098. }
  1099. return true;
  1100. }
  1101. static void inc_core_errors(struct bflsc_info *info, int8_t core)
  1102. {
  1103. if (info->ident == IDENT_BMA) {
  1104. if (core >= 0)
  1105. info->cortex_hw[core]++;
  1106. } else {
  1107. if (core >= 0 && core < 16)
  1108. info->core_hw[core]++;
  1109. }
  1110. }
  1111. static void inc_bflsc_errors(struct thr_info *thr, struct bflsc_info *info, int8_t core)
  1112. {
  1113. inc_hw_errors(thr);
  1114. inc_core_errors(info, core);
  1115. }
  1116. static void inc_bflsc_nonces(struct bflsc_info *info, int8_t core)
  1117. {
  1118. if (info->ident == IDENT_BMA) {
  1119. if (core >= 0)
  1120. info->cortex_nonces[core]++;
  1121. } else {
  1122. if (core >= 0 && core < 16)
  1123. info->core_nonces[core]++;
  1124. }
  1125. }
  1126. struct work *bflsc_work_by_uid(struct cgpu_info *bflsc, struct bflsc_info *sc_info, int id)
  1127. {
  1128. struct bflsc_work *bwork;
  1129. struct work *work = NULL;
  1130. wr_lock(&bflsc->qlock);
  1131. HASH_FIND_INT(sc_info->bworks, &id, bwork);
  1132. if (likely(bwork)) {
  1133. HASH_DEL(sc_info->bworks, bwork);
  1134. work = bwork->work;
  1135. free(bwork);
  1136. }
  1137. wr_unlock(&bflsc->qlock);
  1138. return work;
  1139. }
  1140. static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char *data, int count, char **fields, int *nonces)
  1141. {
  1142. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1143. struct thr_info *thr = bflsc->thr[0];
  1144. struct work *work = NULL;
  1145. int8_t core = -1;
  1146. uint32_t nonce;
  1147. int i, num, x;
  1148. char *tmp;
  1149. bool res;
  1150. if (count < sc_info->que_fld_min) {
  1151. tmp = str_text(data);
  1152. applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
  1153. "%s%i:%s work returned too small (%d,%s)",
  1154. bflsc->drv->name, bflsc->device_id, xlink, count, tmp);
  1155. free(tmp);
  1156. inc_bflsc_errors(thr, sc_info, core);
  1157. return;
  1158. }
  1159. if (sc_info->ident == IDENT_BMA) {
  1160. unsigned int ucore;
  1161. if (sscanf(fields[QUE_CC], "%x", &ucore) == 1)
  1162. core = ucore;
  1163. } else if (sc_info->que_noncecount != QUE_NONCECOUNT_V1) {
  1164. unsigned int ucore;
  1165. if (sscanf(fields[QUE_CHIP_V2], "%x", &ucore) == 1)
  1166. core = ucore;
  1167. }
  1168. if (count > sc_info->que_fld_max) {
  1169. applog(LOG_INFO, "%s%i:%s work returned too large (%d) processing %d anyway",
  1170. bflsc->drv->name, bflsc->device_id, xlink, count, sc_info->que_fld_max);
  1171. count = sc_info->que_fld_max;
  1172. inc_bflsc_errors(thr, sc_info, core);
  1173. }
  1174. num = atoi(fields[sc_info->que_noncecount]);
  1175. if (num != count - sc_info->que_fld_min) {
  1176. tmp = str_text(data);
  1177. applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
  1178. "%s%i:%s incorrect data count (%d) will use %d instead from (%s)",
  1179. bflsc->drv->name, bflsc->device_id, xlink, num,
  1180. count - sc_info->que_fld_max, tmp);
  1181. free(tmp);
  1182. inc_bflsc_errors(thr, sc_info, core);
  1183. }
  1184. if (sc_info->ident == IDENT_BMA) {
  1185. int uid;
  1186. if (sscanf(fields[QUE_UID], "%04x", &uid) == 1)
  1187. work = bflsc_work_by_uid(bflsc, sc_info, uid);
  1188. } else {
  1189. char midstate[MIDSTATE_BYTES] = {}, blockdata[MERKLE_BYTES] = {};
  1190. if (!hex2bin((unsigned char *)midstate, fields[QUE_MIDSTATE], MIDSTATE_BYTES) ||
  1191. !hex2bin((unsigned char *)blockdata, fields[QUE_BLOCKDATA], MERKLE_BYTES)) {
  1192. applog(LOG_INFO, "%s%i:%s Failed to convert binary data to hex result - ignored",
  1193. bflsc->drv->name, bflsc->device_id, xlink);
  1194. inc_bflsc_errors(thr, sc_info, core);
  1195. return;
  1196. }
  1197. work = take_queued_work_bymidstate(bflsc, midstate, MIDSTATE_BYTES,
  1198. blockdata, MERKLE_OFFSET, MERKLE_BYTES);
  1199. }
  1200. if (!work) {
  1201. if (sc_info->not_first_work) {
  1202. applog(LOG_INFO, "%s%i:%s failed to find nonce work - can't be processed - ignored",
  1203. bflsc->drv->name, bflsc->device_id, xlink);
  1204. inc_bflsc_errors(thr, sc_info, core);
  1205. }
  1206. return;
  1207. }
  1208. res = false;
  1209. x = 0;
  1210. for (i = sc_info->que_fld_min; i < count; i++) {
  1211. if (strlen(fields[i]) != 8) {
  1212. tmp = str_text(data);
  1213. applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
  1214. "%s%i:%s invalid nonce (%s) will try to process anyway",
  1215. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1216. free(tmp);
  1217. }
  1218. hex2bin((void*)&nonce, fields[i], 4);
  1219. nonce = htobe32(nonce);
  1220. res = submit_nonce(thr, work, nonce);
  1221. if (res) {
  1222. wr_lock(&(sc_info->stat_lock));
  1223. sc_info->sc_devs[dev].nonces_found++;
  1224. wr_unlock(&(sc_info->stat_lock));
  1225. (*nonces)++;
  1226. x++;
  1227. inc_bflsc_nonces(sc_info, core);
  1228. } else
  1229. inc_core_errors(sc_info, core);
  1230. }
  1231. wr_lock(&(sc_info->stat_lock));
  1232. if (res)
  1233. sc_info->sc_devs[dev].result_id++;
  1234. if (x > QUE_MAX_RESULTS)
  1235. x = QUE_MAX_RESULTS + 1;
  1236. (sc_info->result_size[x])++;
  1237. sc_info->sc_devs[dev].work_complete++;
  1238. sc_info->sc_devs[dev].hashes_unsent += FULLNONCE;
  1239. // If not flushed (stale)
  1240. if (!(work->devflag))
  1241. sc_info->sc_devs[dev].work_queued -= 1;
  1242. wr_unlock(&(sc_info->stat_lock));
  1243. free_work(work);
  1244. }
  1245. static int process_results(struct cgpu_info *bflsc, int dev, char *pbuf, int *nonces, int *in_process)
  1246. {
  1247. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1248. char **items, *firstname, **fields, *lf;
  1249. int que = 0, i, lines, count;
  1250. char *tmp, *tmp2, *buf;
  1251. char xlink[17];
  1252. bool res;
  1253. *nonces = 0;
  1254. *in_process = 0;
  1255. xlinkstr(xlink, sizeof(xlink), dev, sc_info);
  1256. buf = strdup(pbuf);
  1257. if (!strncmp(buf, "INPROCESS", 9))
  1258. sscanf(buf, "INPROCESS:%d\n%s", in_process, pbuf);
  1259. res = tolines(bflsc, dev, buf, &lines, &items, C_GETRESULTS);
  1260. if (!res || lines < 1) {
  1261. tmp = str_text(pbuf);
  1262. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1263. "%s%i:%s empty result (%s) ignored",
  1264. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1265. free(tmp);
  1266. goto arigatou;
  1267. }
  1268. if (lines < QUE_RES_LINES_MIN) {
  1269. tmp = str_text(pbuf);
  1270. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1271. "%s%i:%s result of %d too small (%s) ignored",
  1272. bflsc->drv->name, bflsc->device_id, xlink, lines, tmp);
  1273. free(tmp);
  1274. goto arigatou;
  1275. }
  1276. breakdown(ONECOLON, items[1], &count, &firstname, &fields, &lf);
  1277. if (count < 1) {
  1278. tmp = str_text(pbuf);
  1279. tmp2 = str_text(items[1]);
  1280. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1281. "%s%i:%s empty result count (%s) in (%s) ignoring",
  1282. bflsc->drv->name, bflsc->device_id, xlink, tmp2, tmp);
  1283. free(tmp2);
  1284. free(tmp);
  1285. goto arigatou;
  1286. } else if (count != 1) {
  1287. tmp = str_text(pbuf);
  1288. tmp2 = str_text(items[1]);
  1289. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1290. "%s%i:%s incorrect result count %d (%s) in (%s) will try anyway",
  1291. bflsc->drv->name, bflsc->device_id, xlink, count, tmp2, tmp);
  1292. free(tmp2);
  1293. free(tmp);
  1294. }
  1295. que = atoi(fields[0]);
  1296. if (que != (lines - QUE_RES_LINES_MIN)) {
  1297. i = que;
  1298. // 1+ In case the last line isn't 'OK' - try to process it
  1299. que = 1 + lines - QUE_RES_LINES_MIN;
  1300. tmp = str_text(pbuf);
  1301. tmp2 = str_text(items[0]);
  1302. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1303. "%s%i:%s incorrect result count %d (%s) will try %d (%s)",
  1304. bflsc->drv->name, bflsc->device_id, xlink, i, tmp2, que, tmp);
  1305. free(tmp2);
  1306. free(tmp);
  1307. }
  1308. freebreakdown(&count, &firstname, &fields);
  1309. for (i = 0; i < que; i++) {
  1310. res = breakdown(NOCOLON, items[i + QUE_RES_LINES_MIN - 1], &count, &firstname, &fields, &lf);
  1311. if (likely(res))
  1312. process_nonces(bflsc, dev, &(xlink[0]), items[i], count, fields, nonces);
  1313. else
  1314. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1315. "%s%i:%s failed to process nonce %s",
  1316. bflsc->drv->name, bflsc->device_id, xlink, items[i]);
  1317. freebreakdown(&count, &firstname, &fields);
  1318. sc_info->not_first_work = true;
  1319. }
  1320. arigatou:
  1321. freetolines(&lines, &items);
  1322. free(buf);
  1323. return que;
  1324. }
  1325. #define TVF(tv) ((float)((tv)->tv_sec) + ((float)((tv)->tv_usec) / 1000000.0))
  1326. #define TVFMS(tv) (TVF(tv) * 1000.0)
  1327. // Thread to simply keep looking for results
  1328. static void *bflsc_get_results(void *userdata)
  1329. {
  1330. struct cgpu_info *bflsc = (struct cgpu_info *)userdata;
  1331. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1332. struct timeval elapsed, now;
  1333. float oldest, f;
  1334. char buf[BFLSC_BUFSIZ+1];
  1335. int err, amount;
  1336. int i, que, dev, nonces;
  1337. bool readok;
  1338. cgtime(&now);
  1339. for (i = 0; i < sc_info->sc_count; i++) {
  1340. copy_time(&(sc_info->sc_devs[i].last_check_result), &now);
  1341. copy_time(&(sc_info->sc_devs[i].last_dev_result), &now);
  1342. copy_time(&(sc_info->sc_devs[i].last_nonce_result), &now);
  1343. }
  1344. while (sc_info->shutdown == false) {
  1345. cgtimer_t ts_start;
  1346. int in_process;
  1347. if (bflsc->usbinfo.nodev)
  1348. return NULL;
  1349. dev = -1;
  1350. oldest = FLT_MAX;
  1351. cgtime(&now);
  1352. // Find the first oldest ... that also needs checking
  1353. for (i = 0; i < sc_info->sc_count; i++) {
  1354. timersub(&now, &(sc_info->sc_devs[i].last_check_result), &elapsed);
  1355. f = TVFMS(&elapsed);
  1356. if (f < oldest && f >= sc_info->sc_devs[i].ms_work) {
  1357. f = oldest;
  1358. dev = i;
  1359. }
  1360. }
  1361. if (bflsc->usbinfo.nodev)
  1362. return NULL;
  1363. cgsleep_prepare_r(&ts_start);
  1364. if (dev == -1)
  1365. goto utsura;
  1366. cgtime(&(sc_info->sc_devs[dev].last_check_result));
  1367. readok = bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, false);
  1368. if (err < 0 || (!readok && amount != BFLSC_QRES_LEN) || (readok && amount < 1)) {
  1369. // TODO: do what else?
  1370. } else {
  1371. que = process_results(bflsc, dev, buf, &nonces, &in_process);
  1372. sc_info->not_first_work = true; // in case it failed processing it
  1373. if (que > 0)
  1374. cgtime(&(sc_info->sc_devs[dev].last_dev_result));
  1375. if (nonces > 0)
  1376. cgtime(&(sc_info->sc_devs[dev].last_nonce_result));
  1377. /* There are more results queued so do not sleep */
  1378. if (in_process)
  1379. continue;
  1380. // TODO: if not getting results ... reinit?
  1381. }
  1382. utsura:
  1383. cgsleep_ms_r(&ts_start, sc_info->results_sleep_time);
  1384. }
  1385. return NULL;
  1386. }
  1387. static bool bflsc_thread_prepare(struct thr_info *thr)
  1388. {
  1389. struct cgpu_info *bflsc = thr->cgpu;
  1390. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1391. if (thr_info_create(&(sc_info->results_thr), NULL, bflsc_get_results, (void *)bflsc)) {
  1392. applog(LOG_ERR, "%s%i: thread create failed", bflsc->drv->name, bflsc->device_id);
  1393. return false;
  1394. }
  1395. pthread_detach(sc_info->results_thr.pth);
  1396. return true;
  1397. }
  1398. static void bflsc_shutdown(struct thr_info *thr)
  1399. {
  1400. struct cgpu_info *bflsc = thr->cgpu;
  1401. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1402. bflsc_flush_work(bflsc);
  1403. sc_info->shutdown = true;
  1404. }
  1405. static void bflsc_thread_enable(struct thr_info *thr)
  1406. {
  1407. struct cgpu_info *bflsc = thr->cgpu;
  1408. if (bflsc->usbinfo.nodev)
  1409. return;
  1410. bflsc_initialise(bflsc);
  1411. }
  1412. static bool bflsc_send_work(struct cgpu_info *bflsc, int dev, bool mandatory)
  1413. {
  1414. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1415. struct FullNonceRangeJob data;
  1416. char buf[BFLSC_BUFSIZ+1];
  1417. bool sent, ret = false;
  1418. struct work *work;
  1419. int err, amount;
  1420. int len, try;
  1421. int stage;
  1422. // Device is gone
  1423. if (bflsc->usbinfo.nodev)
  1424. return false;
  1425. // TODO: handle this everywhere
  1426. if (sc_info->sc_devs[dev].overheat == true)
  1427. return false;
  1428. // Initially code only deals with sending one work item
  1429. data.payloadSize = BFLSC_JOBSIZ;
  1430. data.endOfBlock = BFLSC_EOB;
  1431. len = sizeof(struct FullNonceRangeJob);
  1432. /* On faster devices we have a lot of lock contention so only
  1433. * mandatorily grab the lock and send work if the queue is empty since
  1434. * we have a submit queue. */
  1435. if (mandatory)
  1436. mutex_lock(&(bflsc->device_mutex));
  1437. else {
  1438. if (mutex_trylock(&bflsc->device_mutex))
  1439. return ret;
  1440. }
  1441. work = get_queued(bflsc);
  1442. if (unlikely(!work)) {
  1443. mutex_unlock(&bflsc->device_mutex);
  1444. return ret;
  1445. }
  1446. memcpy(data.midState, work->midstate, MIDSTATE_BYTES);
  1447. memcpy(data.blockData, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1448. try = 0;
  1449. re_send:
  1450. err = send_recv_ds(bflsc, dev, &stage, &sent, &amount,
  1451. BFLSC_QJOB, BFLSC_QJOB_LEN, C_REQUESTQUEJOB, C_REQUESTQUEJOBSTATUS,
  1452. (char *)&data, len, C_QUEJOB, C_QUEJOBSTATUS,
  1453. buf, sizeof(buf)-1);
  1454. mutex_unlock(&(bflsc->device_mutex));
  1455. switch (stage) {
  1456. case 1:
  1457. if (!sent) {
  1458. bflsc_applog(bflsc, dev, C_REQUESTQUEJOB, amount, err);
  1459. goto out;
  1460. } else {
  1461. // TODO: handle other errors ...
  1462. // Try twice
  1463. if (try++ < 1 && amount > 1 &&
  1464. strstr(buf, BFLSC_TIMEOUT))
  1465. goto re_send;
  1466. bflsc_applog(bflsc, dev, C_REQUESTQUEJOBSTATUS, amount, err);
  1467. goto out;
  1468. }
  1469. break;
  1470. case 2:
  1471. if (!sent) {
  1472. bflsc_applog(bflsc, dev, C_QUEJOB, amount, err);
  1473. goto out;
  1474. } else {
  1475. if (!isokerr(err, buf, amount)) {
  1476. // TODO: check for QUEUE FULL and set work_queued to sc_info->que_size
  1477. // and report a code bug LOG_ERR - coz it should never happen
  1478. // TODO: handle other errors ...
  1479. // Try twice
  1480. if (try++ < 1 && amount > 1 &&
  1481. strstr(buf, BFLSC_TIMEOUT))
  1482. goto re_send;
  1483. bflsc_applog(bflsc, dev, C_QUEJOBSTATUS, amount, err);
  1484. goto out;
  1485. }
  1486. }
  1487. break;
  1488. }
  1489. wr_lock(&(sc_info->stat_lock));
  1490. sc_info->sc_devs[dev].work_queued++;
  1491. wr_unlock(&(sc_info->stat_lock));
  1492. work->subid = dev;
  1493. ret = true;
  1494. out:
  1495. if (unlikely(!ret))
  1496. work_completed(bflsc, work);
  1497. return ret;
  1498. }
  1499. #define JP_COMMAND 0
  1500. #define JP_STREAMLENGTH 2
  1501. #define JP_SIGNATURE 4
  1502. #define JP_JOBSINARRY 5
  1503. #define JP_JOBSARRY 6
  1504. #define JP_ARRAYSIZE 45
  1505. static bool bflsc28_queue_full(struct cgpu_info *bflsc)
  1506. {
  1507. struct bflsc_info *sc_info = bflsc->device_data;
  1508. int created, queued = 0, create, i, offset;
  1509. struct work *base_work, *work, *works[10];
  1510. char *buf, *field, *ptr;
  1511. bool sent, ret = false;
  1512. uint16_t *streamlen;
  1513. uint8_t *job_pack;
  1514. int err, amount;
  1515. job_pack = alloca(2 + // Command
  1516. 2 + // StreamLength
  1517. 1 + // Signature
  1518. 1 + // JobsInArray
  1519. JP_ARRAYSIZE * 10 +// Array of up to 10 Job Structs
  1520. 1 // EndOfWrapper
  1521. );
  1522. if (bflsc->usbinfo.nodev)
  1523. return true;
  1524. /* Don't send any work if this device is overheating */
  1525. if (sc_info->sc_devs[0].overheat == true)
  1526. return true;
  1527. wr_lock(&bflsc->qlock);
  1528. base_work = __get_queued(bflsc);
  1529. if (likely(base_work))
  1530. __work_completed(bflsc, base_work);
  1531. wr_unlock(&bflsc->qlock);
  1532. if (unlikely(!base_work))
  1533. return ret;
  1534. created = 1;
  1535. create = 9;
  1536. if (base_work->drv_rolllimit < create)
  1537. create = base_work->drv_rolllimit;
  1538. works[0] = base_work;
  1539. for (i = 1; i <= create ; i++) {
  1540. created++;
  1541. work = make_clone(base_work);
  1542. roll_work(base_work);
  1543. works[i] = work;
  1544. }
  1545. memcpy(job_pack, "WX", 2);
  1546. streamlen = (uint16_t *)&job_pack[JP_STREAMLENGTH];
  1547. *streamlen = created * JP_ARRAYSIZE + 7;
  1548. job_pack[JP_SIGNATURE] = 0xc1;
  1549. job_pack[JP_JOBSINARRY] = created;
  1550. offset = JP_JOBSARRY;
  1551. /* Create the maximum number of work items we can queue by nrolling one */
  1552. for (i = 0; i < created; i++) {
  1553. work = works[i];
  1554. memcpy(job_pack + offset, work->midstate, MIDSTATE_BYTES);
  1555. offset += MIDSTATE_BYTES;
  1556. memcpy(job_pack + offset, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1557. offset += MERKLE_BYTES;
  1558. job_pack[offset] = 0xaa; // EndOfBlock signature
  1559. offset++;
  1560. }
  1561. job_pack[offset++] = 0xfe; // EndOfWrapper
  1562. buf = alloca(BFLSC_BUFSIZ + 1);
  1563. mutex_lock(&bflsc->device_mutex);
  1564. err = send_recv_ss(bflsc, 0, &sent, &amount, (char *)job_pack, offset,
  1565. C_REQUESTQUEJOB, buf, BFLSC_BUFSIZ, C_REQUESTQUEJOBSTATUS, READ_NL);
  1566. mutex_unlock(&bflsc->device_mutex);
  1567. if (!isokerr(err, buf, amount)) {
  1568. if (!strncasecmp(buf, "ERR:QUEUE FULL", 14)) {
  1569. applog(LOG_DEBUG, "%s%d: Queue full",
  1570. bflsc->drv->name, bflsc->device_id);
  1571. ret = true;
  1572. } else {
  1573. applog(LOG_WARNING, "%s%d: Queue response not ok %s",
  1574. bflsc->drv->name, bflsc->device_id, buf);
  1575. }
  1576. goto out;
  1577. }
  1578. ptr = alloca(strlen(buf));
  1579. if (sscanf(buf, "OK:QUEUED %d:%s", &queued, ptr) != 2) {
  1580. applog(LOG_WARNING, "%s%d: Failed to parse queue response %s",
  1581. bflsc->drv->name, bflsc->device_id, buf);
  1582. goto out;
  1583. }
  1584. if (queued < 1 || queued > 10) {
  1585. applog(LOG_WARNING, "%s%d: Invalid queued count %d",
  1586. bflsc->drv->name, bflsc->device_id, queued);
  1587. queued = 0;
  1588. goto out;
  1589. }
  1590. for (i = 0; i < queued; i++) {
  1591. struct bflsc_work *bwork, *oldbwork;
  1592. unsigned int uid;
  1593. work = works[i];
  1594. field = Strsep(&ptr, ",");
  1595. if (!field) {
  1596. applog(LOG_WARNING, "%s%d: Ran out of queued IDs after %d of %d",
  1597. bflsc->drv->name, bflsc->device_id, i, queued);
  1598. queued = i - 1;
  1599. goto out;
  1600. }
  1601. sscanf(field, "%04x", &uid);
  1602. bwork = calloc(sizeof(struct bflsc_work), 1);
  1603. bwork->id = uid;
  1604. bwork->work = work;
  1605. wr_lock(&bflsc->qlock);
  1606. HASH_REPLACE_INT(sc_info->bworks, id, bwork, oldbwork);
  1607. if (oldbwork) {
  1608. free_work(oldbwork->work);
  1609. free(oldbwork);
  1610. }
  1611. wr_unlock(&bflsc->qlock);
  1612. sc_info->sc_devs[0].work_queued++;
  1613. }
  1614. if (queued < created)
  1615. ret = true;
  1616. out:
  1617. for (i = queued; i < created; i++) {
  1618. work = works[i];
  1619. discard_work(work);
  1620. }
  1621. return ret;
  1622. }
  1623. static bool bflsc_queue_full(struct cgpu_info *bflsc)
  1624. {
  1625. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1626. int i, dev, tried, que;
  1627. bool ret = false;
  1628. int tries = 0;
  1629. tried = -1;
  1630. // if something is wrong with a device try the next one available
  1631. // TODO: try them all? Add an unavailable flag to sc_devs[i] init to 0 here first
  1632. while (++tries < 3) {
  1633. bool mandatory = false;
  1634. // Device is gone - shouldn't normally get here
  1635. if (bflsc->usbinfo.nodev) {
  1636. ret = true;
  1637. break;
  1638. }
  1639. dev = -1;
  1640. rd_lock(&(sc_info->stat_lock));
  1641. // Anything waiting - gets the work first
  1642. for (i = 0; i < sc_info->sc_count; i++) {
  1643. // TODO: and ignore x-link dead - once I work out how to decide it is dead
  1644. if (i != tried && sc_info->sc_devs[i].work_queued == 0 &&
  1645. !sc_info->sc_devs[i].overheat) {
  1646. dev = i;
  1647. break;
  1648. }
  1649. }
  1650. if (dev == -1) {
  1651. que = sc_info->que_size * 10; // 10x is certainly above the MAX it could be
  1652. // The first device with the smallest amount queued
  1653. for (i = 0; i < sc_info->sc_count; i++) {
  1654. if (i != tried && sc_info->sc_devs[i].work_queued < que &&
  1655. !sc_info->sc_devs[i].overheat) {
  1656. dev = i;
  1657. que = sc_info->sc_devs[i].work_queued;
  1658. }
  1659. }
  1660. if (que > sc_info->que_full_enough)
  1661. dev = -1;
  1662. else if (que < sc_info->que_low)
  1663. mandatory = true;
  1664. }
  1665. rd_unlock(&(sc_info->stat_lock));
  1666. // nothing needs work yet
  1667. if (dev == -1) {
  1668. ret = true;
  1669. break;
  1670. }
  1671. if (bflsc_send_work(bflsc, dev, mandatory))
  1672. break;
  1673. else
  1674. tried = dev;
  1675. }
  1676. return ret;
  1677. }
  1678. static int64_t bflsc_scanwork(struct thr_info *thr)
  1679. {
  1680. struct cgpu_info *bflsc = thr->cgpu;
  1681. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1682. int64_t ret, unsent;
  1683. bool flushed, cleanup;
  1684. struct work *work, *tmp;
  1685. int dev, waited, i;
  1686. // Device is gone
  1687. if (bflsc->usbinfo.nodev)
  1688. return -1;
  1689. flushed = false;
  1690. // Single lock check if any are flagged as flushed
  1691. rd_lock(&(sc_info->stat_lock));
  1692. for (dev = 0; dev < sc_info->sc_count; dev++)
  1693. flushed |= sc_info->sc_devs[dev].flushed;
  1694. rd_unlock(&(sc_info->stat_lock));
  1695. // > 0 flagged as flushed
  1696. if (flushed) {
  1697. // TODO: something like this ......
  1698. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1699. cleanup = false;
  1700. // Is there any flushed work that can be removed?
  1701. rd_lock(&(sc_info->stat_lock));
  1702. if (sc_info->sc_devs[dev].flushed) {
  1703. if (sc_info->sc_devs[dev].result_id > (sc_info->sc_devs[dev].flush_id + sc_info->flush_size))
  1704. cleanup = true;
  1705. }
  1706. rd_unlock(&(sc_info->stat_lock));
  1707. // yes remove the flushed work that can be removed
  1708. if (cleanup) {
  1709. wr_lock(&bflsc->qlock);
  1710. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  1711. if (work->devflag && work->subid == dev) {
  1712. bflsc->queued_count--;
  1713. HASH_DEL(bflsc->queued_work, work);
  1714. discard_work(work);
  1715. }
  1716. }
  1717. wr_unlock(&bflsc->qlock);
  1718. wr_lock(&(sc_info->stat_lock));
  1719. sc_info->sc_devs[dev].flushed = false;
  1720. wr_unlock(&(sc_info->stat_lock));
  1721. }
  1722. }
  1723. }
  1724. waited = restart_wait(thr, sc_info->scan_sleep_time);
  1725. if (waited == ETIMEDOUT && sc_info->ident != IDENT_BMA) {
  1726. unsigned int old_sleep_time, new_sleep_time = 0;
  1727. int min_queued = sc_info->que_size;
  1728. /* Only adjust the scan_sleep_time if we did not receive a
  1729. * restart message while waiting. Try to adjust sleep time
  1730. * so we drop to sc_info->que_watermark before getting more work.
  1731. */
  1732. rd_lock(&sc_info->stat_lock);
  1733. old_sleep_time = sc_info->scan_sleep_time;
  1734. for (i = 0; i < sc_info->sc_count; i++) {
  1735. if (sc_info->sc_devs[i].work_queued < min_queued)
  1736. min_queued = sc_info->sc_devs[i].work_queued;
  1737. }
  1738. rd_unlock(&sc_info->stat_lock);
  1739. new_sleep_time = old_sleep_time;
  1740. /* Increase slowly but decrease quickly */
  1741. if (min_queued > sc_info->que_full_enough && old_sleep_time < BFLSC_MAX_SLEEP)
  1742. new_sleep_time = old_sleep_time * 21 / 20;
  1743. else if (min_queued < sc_info->que_low)
  1744. new_sleep_time = old_sleep_time * 2 / 3;
  1745. /* Do not sleep more than BFLSC_MAX_SLEEP so we can always
  1746. * report in at least 2 results per 5s log interval. */
  1747. if (new_sleep_time != old_sleep_time) {
  1748. if (new_sleep_time > BFLSC_MAX_SLEEP)
  1749. new_sleep_time = BFLSC_MAX_SLEEP;
  1750. else if (new_sleep_time == 0)
  1751. new_sleep_time = 1;
  1752. applog(LOG_DEBUG, "%s%i: Changed scan sleep time to %d",
  1753. bflsc->drv->name, bflsc->device_id, new_sleep_time);
  1754. wr_lock(&sc_info->stat_lock);
  1755. sc_info->scan_sleep_time = new_sleep_time;
  1756. wr_unlock(&sc_info->stat_lock);
  1757. }
  1758. }
  1759. // Count up the work done since we last were here
  1760. ret = 0;
  1761. wr_lock(&(sc_info->stat_lock));
  1762. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1763. unsent = sc_info->sc_devs[dev].hashes_unsent;
  1764. sc_info->sc_devs[dev].hashes_unsent = 0;
  1765. sc_info->sc_devs[dev].hashes_sent += unsent;
  1766. sc_info->hashes_sent += unsent;
  1767. ret += unsent;
  1768. }
  1769. wr_unlock(&(sc_info->stat_lock));
  1770. return ret;
  1771. }
  1772. #define BFLSC_OVER_TEMP 75
  1773. /* Set the fanspeed to auto for any valid value <= BFLSC_OVER_TEMP,
  1774. * or max for any value > BFLSC_OVER_TEMP or if we don't know the temperature. */
  1775. static void bflsc_set_fanspeed(struct cgpu_info *bflsc)
  1776. {
  1777. struct bflsc_info *sc_info = (struct bflsc_info *)bflsc->device_data;
  1778. char buf[BFLSC_BUFSIZ+1];
  1779. char data[16+1];
  1780. int amount;
  1781. bool sent;
  1782. if ((bflsc->temp <= BFLSC_OVER_TEMP && bflsc->temp > 0 && sc_info->fanauto) ||
  1783. ((bflsc->temp > BFLSC_OVER_TEMP || !bflsc->temp) && !sc_info->fanauto))
  1784. return;
  1785. if (bflsc->temp > BFLSC_OVER_TEMP || !bflsc->temp) {
  1786. strcpy(data, BFLSC_FAN4);
  1787. sc_info->fanauto = false;
  1788. } else {
  1789. strcpy(data, BFLSC_FANAUTO);
  1790. sc_info->fanauto = true;
  1791. }
  1792. applog(LOG_DEBUG, "%s%i: temp=%.0f over=%d set fan to %s",
  1793. bflsc->drv->name, bflsc->device_id, bflsc->temp,
  1794. BFLSC_OVER_TEMP, data);
  1795. mutex_lock(&bflsc->device_mutex);
  1796. send_recv_ss(bflsc, 0, &sent, &amount,
  1797. data, strlen(data), C_SETFAN,
  1798. buf, sizeof(buf)-1, C_FANREPLY, READ_NL);
  1799. mutex_unlock(&bflsc->device_mutex);
  1800. }
  1801. static bool bflsc_get_stats(struct cgpu_info *bflsc)
  1802. {
  1803. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1804. bool allok = true;
  1805. int i;
  1806. // Device is gone
  1807. if (bflsc->usbinfo.nodev)
  1808. return false;
  1809. for (i = 0; i < sc_info->sc_count; i++) {
  1810. if (!bflsc_get_temp(bflsc, i))
  1811. allok = false;
  1812. // Device is gone
  1813. if (bflsc->usbinfo.nodev)
  1814. return false;
  1815. if (i < (sc_info->sc_count - 1))
  1816. cgsleep_ms(BFLSC_TEMP_SLEEPMS);
  1817. }
  1818. bflsc_set_fanspeed(bflsc);
  1819. return allok;
  1820. }
  1821. static char *bflsc_set(struct cgpu_info *bflsc, char *option, char *setting, char *replybuf)
  1822. {
  1823. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1824. int val;
  1825. if (sc_info->ident != IDENT_BMA) {
  1826. strcpy(replybuf, "no set options available");
  1827. return replybuf;
  1828. }
  1829. if (strcasecmp(option, "help") == 0) {
  1830. sprintf(replybuf, "volt: range 0-9 clock: range 0-15");
  1831. return replybuf;
  1832. }
  1833. if (strcasecmp(option, "volt") == 0) {
  1834. if (!setting || !*setting) {
  1835. sprintf(replybuf, "missing volt setting");
  1836. return replybuf;
  1837. }
  1838. val = atoi(setting);
  1839. if (val < 0 || val > 9) {
  1840. sprintf(replybuf, "invalid volt: '%s' valid range 0-9",
  1841. setting);
  1842. }
  1843. sc_info->volt_next = val;
  1844. sc_info->volt_next_stat = true;
  1845. return NULL;
  1846. }
  1847. if (strcasecmp(option, "clock") == 0) {
  1848. if (!setting || !*setting) {
  1849. sprintf(replybuf, "missing clock setting");
  1850. return replybuf;
  1851. }
  1852. val = atoi(setting);
  1853. if (val < 0 || val > 15) {
  1854. sprintf(replybuf, "invalid clock: '%s' valid range 0-15",
  1855. setting);
  1856. }
  1857. sc_info->clock_next = val;
  1858. sc_info->clock_next_stat = true;
  1859. return NULL;
  1860. }
  1861. sprintf(replybuf, "Unknown option: %s", option);
  1862. return replybuf;
  1863. }
  1864. static void bflsc_identify(struct cgpu_info *bflsc)
  1865. {
  1866. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1867. // TODO: handle x-link
  1868. sc_info->flash_led = true;
  1869. }
  1870. static bool bflsc_thread_init(struct thr_info *thr)
  1871. {
  1872. struct cgpu_info *bflsc = thr->cgpu;
  1873. if (bflsc->usbinfo.nodev)
  1874. return false;
  1875. bflsc_initialise(bflsc);
  1876. return true;
  1877. }
  1878. // there should be a new API function to return device info that isn't the standard stuff
  1879. // instead of bflsc_api_stats - since the stats should really just be internal code info
  1880. // and the new one should be UNusual device stats/extra details - like the stuff below
  1881. static struct api_data *bflsc_api_stats(struct cgpu_info *bflsc)
  1882. {
  1883. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1884. struct api_data *root = NULL;
  1885. char data[4096];
  1886. char buf[256];
  1887. int i, j, off;
  1888. size_t len;
  1889. //if no x-link ... etc
  1890. rd_lock(&(sc_info->stat_lock));
  1891. root = api_add_temp(root, "Temp1", &(sc_info->sc_devs[0].temp1), true);
  1892. root = api_add_temp(root, "Temp2", &(sc_info->sc_devs[0].temp2), true);
  1893. root = api_add_volts(root, "Vcc1", &(sc_info->sc_devs[0].vcc1), true);
  1894. root = api_add_volts(root, "Vcc2", &(sc_info->sc_devs[0].vcc2), true);
  1895. root = api_add_volts(root, "Vmain", &(sc_info->sc_devs[0].vmain), true);
  1896. root = api_add_temp(root, "Temp1 Max", &(sc_info->sc_devs[0].temp1_max), true);
  1897. root = api_add_temp(root, "Temp2 Max", &(sc_info->sc_devs[0].temp2_max), true);
  1898. root = api_add_time(root, "Temp1 Max Time", &(sc_info->sc_devs[0].temp1_max_time), true);
  1899. root = api_add_time(root, "Temp2 Max Time", &(sc_info->sc_devs[0].temp2_max_time), true);
  1900. root = api_add_int(root, "Work Queued", &(sc_info->sc_devs[0].work_queued), true);
  1901. root = api_add_int(root, "Work Complete", &(sc_info->sc_devs[0].work_complete), true);
  1902. root = api_add_bool(root, "Overheat", &(sc_info->sc_devs[0].overheat), true);
  1903. root = api_add_uint64(root, "Flush ID", &(sc_info->sc_devs[0].flush_id), true);
  1904. root = api_add_uint64(root, "Result ID", &(sc_info->sc_devs[0].result_id), true);
  1905. root = api_add_bool(root, "Flushed", &(sc_info->sc_devs[0].flushed), true);
  1906. root = api_add_uint(root, "Scan Sleep", &(sc_info->scan_sleep_time), true);
  1907. root = api_add_uint(root, "Results Sleep", &(sc_info->results_sleep_time), true);
  1908. root = api_add_uint(root, "Work ms", &(sc_info->default_ms_work), true);
  1909. buf[0] = '\0';
  1910. for (i = 0; i <= QUE_MAX_RESULTS + 1; i++)
  1911. tailsprintf(buf, sizeof(buf), "%s%"PRIu64, (i > 0) ? "/" : "", sc_info->result_size[i]);
  1912. root = api_add_string(root, "Result Size", buf, true);
  1913. rd_unlock(&(sc_info->stat_lock));
  1914. i = (int)(sc_info->driver_version);
  1915. root = api_add_int(root, "Driver", &i, true);
  1916. root = api_add_string(root, "Firmware", sc_info->sc_devs[0].firmware, false);
  1917. root = api_add_string(root, "Chips", sc_info->sc_devs[0].chips, false);
  1918. root = api_add_int(root, "Que Size", &(sc_info->que_size), false);
  1919. root = api_add_int(root, "Que Full", &(sc_info->que_full_enough), false);
  1920. root = api_add_int(root, "Que Watermark", &(sc_info->que_watermark), false);
  1921. root = api_add_int(root, "Que Low", &(sc_info->que_low), false);
  1922. root = api_add_escape(root, "GetInfo", sc_info->sc_devs[0].getinfo, false);
  1923. /*
  1924. else a whole lot of something like these ... etc
  1925. root = api_add_temp(root, "X-%d-Temp1", &(sc_info->temp1), false);
  1926. root = api_add_temp(root, "X-%d-Temp2", &(sc_info->temp2), false);
  1927. root = api_add_volts(root, "X-%d-Vcc1", &(sc_info->vcc1), false);
  1928. root = api_add_volts(root, "X-%d-Vcc2", &(sc_info->vcc2), false);
  1929. root = api_add_volts(root, "X-%d-Vmain", &(sc_info->vmain), false);
  1930. */
  1931. if (sc_info->ident == IDENT_BMA) {
  1932. for (i = 0; i < 128; i += 16) {
  1933. data[0] = '\0';
  1934. off = 0;
  1935. for (j = 0; j < 16; j++) {
  1936. len = snprintf(data+off, sizeof(data)-off,
  1937. "%s%"PRIu64,
  1938. j > 0 ? " " : "",
  1939. sc_info->cortex_nonces[i+j]);
  1940. if (len >= (sizeof(data)-off))
  1941. off = sizeof(data)-1;
  1942. else {
  1943. if (len > 0)
  1944. off += len;
  1945. }
  1946. }
  1947. sprintf(buf, "Cortex %02x-%02x Nonces", i, i+15);
  1948. root = api_add_string(root, buf, data, true);
  1949. }
  1950. for (i = 0; i < 128; i += 16) {
  1951. data[0] = '\0';
  1952. off = 0;
  1953. for (j = 0; j < 16; j++) {
  1954. len = snprintf(data+off, sizeof(data)-off,
  1955. "%s%"PRIu64,
  1956. j > 0 ? " " : "",
  1957. sc_info->cortex_hw[i+j]);
  1958. if (len >= (sizeof(data)-off))
  1959. off = sizeof(data)-1;
  1960. else {
  1961. if (len > 0)
  1962. off += len;
  1963. }
  1964. }
  1965. sprintf(buf, "Cortex %02x-%02x HW Errors", i, i+15);
  1966. root = api_add_string(root, buf, data, true);
  1967. }
  1968. } else if (sc_info->que_noncecount != QUE_NONCECOUNT_V1) {
  1969. for (i = 0; i < 16; i++) {
  1970. sprintf(buf, "Core%d Nonces", i);
  1971. root = api_add_uint64(root, buf, &sc_info->core_nonces[i], false);
  1972. }
  1973. for (i = 0; i < 16; i++) {
  1974. sprintf(buf, "Core%d HW Errors", i);
  1975. root = api_add_uint64(root, buf, &sc_info->core_hw[i], false);
  1976. }
  1977. }
  1978. return root;
  1979. }
  1980. struct device_drv bflsc_drv = {
  1981. .drv_id = DRIVER_bflsc,
  1982. .dname = "BitForceSC",
  1983. .name = BFLSC_SINGLE,
  1984. .drv_detect = bflsc_detect,
  1985. .get_api_stats = bflsc_api_stats,
  1986. .get_statline_before = get_bflsc_statline_before,
  1987. .get_stats = bflsc_get_stats,
  1988. .set_device = bflsc_set,
  1989. .identify_device = bflsc_identify,
  1990. .thread_prepare = bflsc_thread_prepare,
  1991. .thread_init = bflsc_thread_init,
  1992. .hash_work = hash_queued_work,
  1993. .scanwork = bflsc_scanwork,
  1994. .queue_full = bflsc_queue_full,
  1995. .flush_work = bflsc_flush_work,
  1996. .thread_shutdown = bflsc_shutdown,
  1997. .thread_enable = bflsc_thread_enable
  1998. };