driver-bflsc.c 53 KB

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