driver-bflsc.c 54 KB

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