driver-bflsc.c 52 KB

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