driver-bflsc.c 62 KB

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