driver-bflsc.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  1. /*
  2. * Copyright 2013 Andrew Smith
  3. * Copyright 2013 Con Kolivas
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include <float.h>
  11. #include <limits.h>
  12. #include <pthread.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <strings.h>
  17. #include <sys/time.h>
  18. #include <unistd.h>
  19. #include "config.h"
  20. #ifdef WIN32
  21. #include <windows.h>
  22. #endif
  23. #include "compat.h"
  24. #include "miner.h"
  25. #include "usbutils.h"
  26. #include "driver-bflsc.h"
  27. int opt_bflsc_overheat = BFLSC_TEMP_OVERHEAT;
  28. static const char *blank = "";
  29. struct device_drv bflsc_drv;
  30. static enum driver_version drv_ver(struct cgpu_info *bflsc, const char *ver)
  31. {
  32. char *tmp;
  33. if (strstr(ver, "1.0.0"))
  34. return BFLSC_DRV1;
  35. if (strstr(ver, "1.0.") || strstr(ver, "1.1.")) {
  36. applog(LOG_WARNING, "%s detect (%s) Warning assuming firmware '%s' is Ver1",
  37. bflsc->drv->dname, bflsc->device_path, ver);
  38. return BFLSC_DRV1;
  39. }
  40. if (strstr(ver, "1.2."))
  41. return BFLSC_DRV2;
  42. tmp = str_text((char *)ver);
  43. applog(LOG_WARNING, "%s detect (%s) Warning unknown firmware '%s' using Ver2",
  44. bflsc->drv->dname, bflsc->device_path, tmp);
  45. free(tmp);
  46. return BFLSC_DRV2;
  47. }
  48. static void xlinkstr(char *xlink, size_t siz, int dev, struct bflsc_info *sc_info)
  49. {
  50. if (dev > 0)
  51. snprintf(xlink, siz, " x-%d", dev);
  52. else {
  53. if (sc_info->sc_count > 1)
  54. strcpy(xlink, " master");
  55. else
  56. *xlink = '\0';
  57. }
  58. }
  59. static void bflsc_applog(struct cgpu_info *bflsc, int dev, enum usb_cmds cmd, int amount, int err)
  60. {
  61. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  62. char xlink[17];
  63. xlinkstr(xlink, sizeof(xlink), dev, sc_info);
  64. usb_applog(bflsc, cmd, xlink, amount, err);
  65. }
  66. // Break an input up into lines with LFs removed
  67. // false means an error, but if *lines > 0 then data was also found
  68. // error would be no data or missing LF at the end
  69. static bool tolines(struct cgpu_info *bflsc, int dev, char *buf, int *lines, char ***items, enum usb_cmds cmd)
  70. {
  71. bool ok = false;
  72. char *ptr;
  73. #define p_lines (*lines)
  74. #define p_items (*items)
  75. p_lines = 0;
  76. p_items = NULL;
  77. if (!buf || !(*buf)) {
  78. applog(LOG_DEBUG, "USB: %s%i: (%d) empty %s",
  79. bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
  80. return ok;
  81. }
  82. ptr = strdup(buf);
  83. while (ptr && *ptr) {
  84. p_items = realloc(p_items, ++p_lines * sizeof(*p_items));
  85. if (unlikely(!p_items))
  86. quit(1, "Failed to realloc p_items in tolines");
  87. p_items[p_lines-1] = ptr;
  88. ptr = strchr(ptr, '\n');
  89. if (ptr)
  90. *(ptr++) = '\0';
  91. else {
  92. applog(LOG_DEBUG, "USB: %s%i: (%d) missing lf(s) in %s",
  93. bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
  94. return ok;
  95. }
  96. }
  97. ok = true;
  98. return ok;
  99. }
  100. static void freetolines(int *lines, char ***items)
  101. {
  102. if (*lines > 0) {
  103. free(**items);
  104. free(*items);
  105. }
  106. *lines = 0;
  107. *items = NULL;
  108. }
  109. enum breakmode {
  110. NOCOLON,
  111. ONECOLON,
  112. ALLCOLON // Temperature uses this
  113. };
  114. // Break down a single line into 'fields'
  115. // 'lf' will be a pointer to the final LF if it is there (or NULL)
  116. // firstname will be the allocated buf copy pointer which is also
  117. // the string before ':' for ONECOLON and ALLCOLON
  118. // If any string is missing the ':' when it was expected, false is returned
  119. static bool breakdown(enum breakmode mode, char *buf, int *count, char **firstname, char ***fields, char **lf)
  120. {
  121. char *ptr, *colon, *comma;
  122. bool ok = false;
  123. #define p_count (*count)
  124. #define p_firstname (*firstname)
  125. #define p_fields (*fields)
  126. #define p_lf (*lf)
  127. p_count = 0;
  128. p_firstname = NULL;
  129. p_fields = NULL;
  130. p_lf = NULL;
  131. if (!buf || !(*buf))
  132. return ok;
  133. ptr = p_firstname = strdup(buf);
  134. p_lf = strchr(p_firstname, '\n');
  135. if (mode == ONECOLON) {
  136. colon = strchr(ptr, ':');
  137. if (colon) {
  138. ptr = colon;
  139. *(ptr++) = '\0';
  140. } else
  141. return ok;
  142. }
  143. while (ptr && *ptr) {
  144. if (mode == ALLCOLON) {
  145. colon = strchr(ptr, ':');
  146. if (colon)
  147. ptr = colon + 1;
  148. else
  149. return ok;
  150. }
  151. comma = strchr(ptr, ',');
  152. if (comma)
  153. *(comma++) = '\0';
  154. p_fields = realloc(p_fields, ++p_count * sizeof(*p_fields));
  155. if (unlikely(!p_fields))
  156. quit(1, "Failed to realloc p_fields in breakdown");
  157. p_fields[p_count-1] = ptr;
  158. ptr = comma;
  159. }
  160. ok = true;
  161. return ok;
  162. }
  163. static void freebreakdown(int *count, char **firstname, char ***fields)
  164. {
  165. if (*firstname)
  166. free(*firstname);
  167. if (*count > 0)
  168. free(*fields);
  169. *count = 0;
  170. *firstname = NULL;
  171. *fields = NULL;
  172. }
  173. static bool isokerr(int err, char *buf, int amount)
  174. {
  175. if (err < 0 || amount < (int)BFLSC_OK_LEN)
  176. return false;
  177. else {
  178. if (strstr(buf, BFLSC_ANERR))
  179. return false;
  180. else
  181. return true;
  182. }
  183. }
  184. // send+receive dual stage - always single line replies
  185. 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)
  186. {
  187. struct DataForwardToChain data;
  188. int len, err, tried;
  189. if (dev == 0) {
  190. usb_buffer_clear(bflsc);
  191. *stage = 1;
  192. *sent = false;
  193. err = usb_write(bflsc, send1, send1_len, amount, send1_cmd);
  194. if (err < 0 || *amount < send1_len)
  195. return err;
  196. *sent = true;
  197. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
  198. if (!isokerr(err, recv, *amount))
  199. return err;
  200. usb_buffer_clear(bflsc);
  201. *stage = 2;
  202. *sent = false;
  203. err = usb_write(bflsc, send2, send2_len, amount, send2_cmd);
  204. if (err < 0 || *amount < send2_len)
  205. return err;
  206. *sent = true;
  207. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
  208. return err;
  209. }
  210. data.header = BFLSC_XLINKHDR;
  211. data.deviceAddress = (uint8_t)dev;
  212. tried = 0;
  213. while (tried++ < 3) {
  214. data.payloadSize = send1_len;
  215. memcpy(data.payloadData, send1, send1_len);
  216. len = DATAFORWARDSIZE(data);
  217. usb_buffer_clear(bflsc);
  218. *stage = 1;
  219. *sent = false;
  220. err = usb_write(bflsc, (char *)&data, len, amount, send1_cmd);
  221. if (err < 0 || *amount < send1_len)
  222. return err;
  223. *sent = true;
  224. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
  225. if (err != LIBUSB_SUCCESS)
  226. return err;
  227. // x-link timeout? - try again?
  228. if (strstr(recv, BFLSC_XTIMEOUT))
  229. continue;
  230. if (!isokerr(err, recv, *amount))
  231. return err;
  232. data.payloadSize = send2_len;
  233. memcpy(data.payloadData, send2, send2_len);
  234. len = DATAFORWARDSIZE(data);
  235. usb_buffer_clear(bflsc);
  236. *stage = 2;
  237. *sent = false;
  238. err = usb_write(bflsc, (char *)&data, len, amount, send2_cmd);
  239. if (err < 0 || *amount < send2_len)
  240. return err;
  241. *sent = true;
  242. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
  243. if (err != LIBUSB_SUCCESS)
  244. return err;
  245. // x-link timeout? - try again?
  246. if (strstr(recv, BFLSC_XTIMEOUT))
  247. continue;
  248. // SUCCESS - return it
  249. break;
  250. }
  251. return err;
  252. }
  253. #define READ_OK true
  254. #define READ_NL false
  255. // send+receive single stage
  256. 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)
  257. {
  258. struct DataForwardToChain data;
  259. int len, err, tried;
  260. if (dev == 0) {
  261. usb_buffer_clear(bflsc);
  262. *sent = false;
  263. err = usb_write(bflsc, send, send_len, amount, send_cmd);
  264. if (err < 0 || *amount < send_len) {
  265. // N.B. thus !(*sent) directly implies err < 0 or *amount < send_len
  266. return err;
  267. }
  268. *sent = true;
  269. if (read_ok == READ_OK)
  270. err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
  271. else
  272. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
  273. return err;
  274. }
  275. data.header = BFLSC_XLINKHDR;
  276. data.deviceAddress = (uint8_t)dev;
  277. data.payloadSize = send_len;
  278. memcpy(data.payloadData, send, send_len);
  279. len = DATAFORWARDSIZE(data);
  280. tried = 0;
  281. while (tried++ < 3) {
  282. usb_buffer_clear(bflsc);
  283. *sent = false;
  284. err = usb_write(bflsc, (char *)&data, len, amount, recv_cmd);
  285. if (err < 0 || *amount < send_len)
  286. return err;
  287. *sent = true;
  288. if (read_ok == READ_OK)
  289. err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
  290. else
  291. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
  292. if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_TIMEOUT)
  293. return err;
  294. // read_ok can err timeout if it's looking for OK<LF>
  295. // TODO: add a usb_read() option to spot the ERR: and convert end=OK<LF> to just <LF>
  296. // x-link timeout? - try again?
  297. if ((err == LIBUSB_SUCCESS || (read_ok == READ_OK && err == LIBUSB_ERROR_TIMEOUT)) &&
  298. strstr(recv, BFLSC_XTIMEOUT))
  299. continue;
  300. // SUCCESS or TIMEOUT - return it
  301. break;
  302. }
  303. return err;
  304. }
  305. static int write_to_dev(struct cgpu_info *bflsc, int dev, char *buf, int buflen, int *amount, enum usb_cmds cmd)
  306. {
  307. struct DataForwardToChain data;
  308. int len;
  309. /*
  310. * The protocol is syncronous so any previous excess can be
  311. * discarded and assumed corrupt data or failed USB transfers
  312. */
  313. usb_buffer_clear(bflsc);
  314. if (dev == 0)
  315. return usb_write(bflsc, buf, buflen, amount, cmd);
  316. data.header = BFLSC_XLINKHDR;
  317. data.deviceAddress = (uint8_t)dev;
  318. data.payloadSize = buflen;
  319. memcpy(data.payloadData, buf, buflen);
  320. len = DATAFORWARDSIZE(data);
  321. return usb_write(bflsc, (char *)&data, len, amount, cmd);
  322. }
  323. static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
  324. {
  325. char buf[BFLSC_BUFSIZ+1];
  326. int err, amount;
  327. bool sent;
  328. // Device is gone
  329. if (bflsc->usbinfo.nodev)
  330. return;
  331. mutex_lock(&bflsc->device_mutex);
  332. err = send_recv_ss(bflsc, dev, &sent, &amount,
  333. BFLSC_QFLUSH, BFLSC_QFLUSH_LEN, C_QUEFLUSH,
  334. buf, sizeof(buf)-1, C_QUEFLUSHREPLY, READ_NL);
  335. mutex_unlock(&bflsc->device_mutex);
  336. if (!sent)
  337. bflsc_applog(bflsc, dev, C_QUEFLUSH, amount, err);
  338. else {
  339. // TODO: do we care if we don't get 'OK'? (always will in normal processing)
  340. }
  341. }
  342. /* return True = attempted usb_read_ok()
  343. * set ignore to true means no applog/ignore errors */
  344. static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore)
  345. {
  346. bool readok = false;
  347. mutex_lock(&(bflsc->device_mutex));
  348. *err = send_recv_ss(bflsc, dev, &readok, amount,
  349. BFLSC_QRES, BFLSC_QRES_LEN, C_REQUESTRESULTS,
  350. buf, bufsiz-1, C_GETRESULTS, READ_OK);
  351. mutex_unlock(&(bflsc->device_mutex));
  352. if (!readok) {
  353. if (!ignore)
  354. bflsc_applog(bflsc, dev, C_REQUESTRESULTS, *amount, *err);
  355. // TODO: do what? flag as dead device?
  356. // count how many times it has happened and reset/fail it
  357. // or even make sure it is all x-link and that means device
  358. // has failed after some limit of this?
  359. // of course all other I/O must also be failing ...
  360. } else {
  361. if (*err < 0 || *amount < 1) {
  362. if (!ignore)
  363. bflsc_applog(bflsc, dev, C_GETRESULTS, *amount, *err);
  364. // TODO: do what? ... see above
  365. }
  366. }
  367. return readok;
  368. }
  369. static void __bflsc_initialise(struct cgpu_info *bflsc)
  370. {
  371. int err;
  372. // TODO: does x-link bypass the other device FTDI? (I think it does)
  373. // So no initialisation required except for the master device?
  374. if (bflsc->usbinfo.nodev)
  375. return;
  376. // Reset
  377. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  378. FTDI_VALUE_RESET, bflsc->usbdev->found->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, bflsc->usbdev->found->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) | bflsc->usbdev->found->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, bflsc->usbdev->found->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, bflsc->usbdev->found->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, bflsc->usbdev->found->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, bflsc->usbdev->found->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. strcpy(sc_dev.getinfo, tmp);
  495. free(tmp);
  496. for (i = 0; i < lines-2; i++) {
  497. res = breakdown(ONECOLON, items[i], &count, &firstname, &fields, &lf);
  498. if (lf)
  499. *lf = '\0';
  500. if (!res || count != 1) {
  501. tmp = str_text(items[i]);
  502. applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
  503. "%s detect (%s) invalid details line: '%s' %d",
  504. bflsc->drv->dname, bflsc->device_path, tmp, count);
  505. free(tmp);
  506. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  507. goto mata;
  508. }
  509. if (strstr(firstname, BFLSC_DI_FIRMWARE)) {
  510. sc_dev.firmware = strdup(fields[0]);
  511. sc_info->driver_version = drv_ver(bflsc, sc_dev.firmware);
  512. }
  513. else if (strstr(firstname, BFLSC_DI_ENGINES)) {
  514. sc_dev.engines = atoi(fields[0]);
  515. if (sc_dev.engines < 1) {
  516. tmp = str_text(items[i]);
  517. applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
  518. "%s detect (%s) invalid engine count: '%s'",
  519. bflsc->drv->dname, bflsc->device_path, tmp);
  520. free(tmp);
  521. goto mata;
  522. }
  523. }
  524. else if (strstr(firstname, BFLSC_DI_XLINKMODE))
  525. sc_dev.xlink_mode = strdup(fields[0]);
  526. else if (strstr(firstname, BFLSC_DI_XLINKPRESENT))
  527. sc_dev.xlink_present = strdup(fields[0]);
  528. else if (strstr(firstname, BFLSC_DI_DEVICESINCHAIN)) {
  529. sc_info->sc_count = atoi(fields[0]);
  530. if (sc_info->sc_count < 1 || sc_info->sc_count > 30) {
  531. tmp = str_text(items[i]);
  532. applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
  533. "%s detect (%s) invalid s-link count: '%s'",
  534. bflsc->drv->dname, bflsc->device_path, tmp);
  535. free(tmp);
  536. goto mata;
  537. }
  538. else if (strstr(firstname, BFLSC_DI_CHIPS))
  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. cgsleep_ms(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. cgsleep_ms(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 = BFLSC_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->default_ms_work = BAM_WORK_TIME;
  686. bflsc->usbdev->ident = IDENT_BAM;
  687. latency = BAM_LATENCY;
  688. } else {
  689. if (sc_info->sc_devs[0].engines < 34) { // 16 * 2 + 2
  690. newname = BFLSC_JALAPENO;
  691. sc_info->scan_sleep_time = BAJ_SCAN_TIME;
  692. sc_info->default_ms_work = BAJ_WORK_TIME;
  693. bflsc->usbdev->ident = IDENT_BAJ;
  694. latency = BAJ_LATENCY;
  695. } else if (sc_info->sc_devs[0].engines < 130) { // 16 * 8 + 2
  696. newname = BFLSC_LITTLESINGLE;
  697. sc_info->scan_sleep_time = BAL_SCAN_TIME;
  698. sc_info->default_ms_work = BAL_WORK_TIME;
  699. bflsc->usbdev->ident = IDENT_BAL;
  700. latency = BAL_LATENCY;
  701. }
  702. }
  703. if (latency != bflsc->usbdev->found->latency) {
  704. bflsc->usbdev->found->latency = latency;
  705. usb_ftdi_set_latency(bflsc);
  706. }
  707. for (i = 0; i < sc_info->sc_count; i++)
  708. sc_info->sc_devs[i].ms_work = sc_info->default_ms_work;
  709. if (newname) {
  710. if (!bflsc->drv->copy)
  711. bflsc->drv = copy_drv(bflsc->drv);
  712. bflsc->drv->name = newname;
  713. }
  714. // We have a real BFLSC!
  715. applog(LOG_DEBUG, "%s (%s) identified as: '%s'",
  716. bflsc->drv->dname, bflsc->device_path, bflsc->drv->name);
  717. if (!add_cgpu(bflsc))
  718. goto unshin;
  719. update_usb_stats(bflsc);
  720. mutex_init(&bflsc->device_mutex);
  721. rwlock_init(&sc_info->stat_lock);
  722. usb_buffer_enable(bflsc);
  723. return true;
  724. unshin:
  725. usb_uninit(bflsc);
  726. shin:
  727. free(bflsc->device_data);
  728. bflsc->device_data = NULL;
  729. if (bflsc->name != blank) {
  730. free(bflsc->name);
  731. bflsc->name = NULL;
  732. }
  733. bflsc = usb_free_cgpu(bflsc);
  734. return false;
  735. }
  736. static void bflsc_detect(void)
  737. {
  738. usb_detect(&bflsc_drv, bflsc_detect_one);
  739. }
  740. static void get_bflsc_statline_before(char *buf, size_t bufsiz, struct cgpu_info *bflsc)
  741. {
  742. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  743. float temp = 0;
  744. float vcc1 = 0;
  745. int i;
  746. rd_lock(&(sc_info->stat_lock));
  747. for (i = 0; i < sc_info->sc_count; i++) {
  748. if (sc_info->sc_devs[i].temp1 > temp)
  749. temp = sc_info->sc_devs[i].temp1;
  750. if (sc_info->sc_devs[i].temp2 > temp)
  751. temp = sc_info->sc_devs[i].temp2;
  752. if (sc_info->sc_devs[i].vcc1 > vcc1)
  753. vcc1 = sc_info->sc_devs[i].vcc1;
  754. }
  755. rd_unlock(&(sc_info->stat_lock));
  756. tailsprintf(buf, bufsiz, " max%3.0fC %4.2fV | ", temp, vcc1);
  757. }
  758. static void flush_one_dev(struct cgpu_info *bflsc, int dev)
  759. {
  760. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  761. struct work *work, *tmp;
  762. bool did = false;
  763. bflsc_send_flush_work(bflsc, dev);
  764. rd_lock(&bflsc->qlock);
  765. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  766. if (work->queued && work->subid == dev) {
  767. // devflag is used to flag stale work
  768. work->devflag = true;
  769. did = true;
  770. }
  771. }
  772. rd_unlock(&bflsc->qlock);
  773. if (did) {
  774. wr_lock(&(sc_info->stat_lock));
  775. sc_info->sc_devs[dev].flushed = true;
  776. sc_info->sc_devs[dev].flush_id = sc_info->sc_devs[dev].result_id;
  777. sc_info->sc_devs[dev].work_queued = 0;
  778. wr_unlock(&(sc_info->stat_lock));
  779. }
  780. }
  781. static void bflsc_flush_work(struct cgpu_info *bflsc)
  782. {
  783. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  784. int dev;
  785. for (dev = 0; dev < sc_info->sc_count; dev++)
  786. flush_one_dev(bflsc, dev);
  787. }
  788. static void bflsc_flash_led(struct cgpu_info *bflsc, int dev)
  789. {
  790. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  791. char buf[BFLSC_BUFSIZ+1];
  792. int err, amount;
  793. bool sent;
  794. // Device is gone
  795. if (bflsc->usbinfo.nodev)
  796. return;
  797. // It is not critical flashing the led so don't get stuck if we
  798. // can't grab the mutex now
  799. if (mutex_trylock(&bflsc->device_mutex))
  800. return;
  801. err = send_recv_ss(bflsc, dev, &sent, &amount,
  802. BFLSC_FLASH, BFLSC_FLASH_LEN, C_REQUESTFLASH,
  803. buf, sizeof(buf)-1, C_FLASHREPLY, READ_NL);
  804. mutex_unlock(&(bflsc->device_mutex));
  805. if (!sent)
  806. bflsc_applog(bflsc, dev, C_REQUESTFLASH, amount, err);
  807. else {
  808. // Don't care
  809. }
  810. // Once we've tried - don't do it until told to again
  811. // - even if it failed
  812. sc_info->flash_led = false;
  813. return;
  814. }
  815. static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
  816. {
  817. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  818. struct bflsc_dev *sc_dev;
  819. char temp_buf[BFLSC_BUFSIZ+1];
  820. char volt_buf[BFLSC_BUFSIZ+1];
  821. char *tmp;
  822. int err, amount;
  823. char *firstname, **fields, *lf;
  824. char xlink[17];
  825. int count;
  826. bool res, sent;
  827. float temp, temp1, temp2;
  828. float vcc1, vcc2, vmain;
  829. // Device is gone
  830. if (bflsc->usbinfo.nodev)
  831. return false;
  832. if (dev >= sc_info->sc_count) {
  833. applog(LOG_ERR, "%s%i: temp invalid xlink device %d - limit %d",
  834. bflsc->drv->name, bflsc->device_id, dev, sc_info->sc_count - 1);
  835. return false;
  836. }
  837. // Flash instead of Temp
  838. if (sc_info->flash_led) {
  839. bflsc_flash_led(bflsc, dev);
  840. return true;
  841. }
  842. xlinkstr(xlink, sizeof(xlink), dev, sc_info);
  843. /* It is not very critical getting temp so don't get stuck if we
  844. * can't grab the mutex here */
  845. if (mutex_trylock(&bflsc->device_mutex))
  846. return false;
  847. err = send_recv_ss(bflsc, dev, &sent, &amount,
  848. BFLSC_TEMPERATURE, BFLSC_TEMPERATURE_LEN, C_REQUESTTEMPERATURE,
  849. temp_buf, sizeof(temp_buf)-1, C_GETTEMPERATURE, READ_NL);
  850. mutex_unlock(&(bflsc->device_mutex));
  851. if (!sent) {
  852. applog(LOG_ERR, "%s%i: Error: Request%s temp invalid/timed out (%d:%d)",
  853. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  854. return false;
  855. } else {
  856. if (err < 0 || amount < 1) {
  857. if (err < 0) {
  858. applog(LOG_ERR, "%s%i: Error: Get%s temp return invalid/timed out (%d:%d)",
  859. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  860. } else {
  861. applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing (%d:%d)",
  862. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  863. }
  864. return false;
  865. }
  866. }
  867. // Ignore it if we can't get the V
  868. if (mutex_trylock(&bflsc->device_mutex))
  869. return false;
  870. err = send_recv_ss(bflsc, dev, &sent, &amount,
  871. BFLSC_VOLTAGE, BFLSC_VOLTAGE_LEN, C_REQUESTVOLTS,
  872. volt_buf, sizeof(volt_buf)-1, C_GETVOLTS, READ_NL);
  873. mutex_unlock(&(bflsc->device_mutex));
  874. if (!sent) {
  875. applog(LOG_ERR, "%s%i: Error: Request%s volts invalid/timed out (%d:%d)",
  876. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  877. return false;
  878. } else {
  879. if (err < 0 || amount < 1) {
  880. if (err < 0) {
  881. applog(LOG_ERR, "%s%i: Error: Get%s volt return invalid/timed out (%d:%d)",
  882. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  883. } else {
  884. applog(LOG_ERR, "%s%i: Error: Get%s volt returned nothing (%d:%d)",
  885. bflsc->drv->name, bflsc->device_id, xlink, amount, err);
  886. }
  887. return false;
  888. }
  889. }
  890. res = breakdown(ALLCOLON, temp_buf, &count, &firstname, &fields, &lf);
  891. if (lf)
  892. *lf = '\0';
  893. if (!res || count != 2 || !lf) {
  894. tmp = str_text(temp_buf);
  895. applog(LOG_WARNING, "%s%i: Invalid%s temp reply: '%s'",
  896. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  897. free(tmp);
  898. freebreakdown(&count, &firstname, &fields);
  899. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  900. return false;
  901. }
  902. temp = temp1 = (float)atoi(fields[0]);
  903. temp2 = (float)atoi(fields[1]);
  904. freebreakdown(&count, &firstname, &fields);
  905. res = breakdown(NOCOLON, volt_buf, &count, &firstname, &fields, &lf);
  906. if (lf)
  907. *lf = '\0';
  908. if (!res || count != 3 || !lf) {
  909. tmp = str_text(volt_buf);
  910. applog(LOG_WARNING, "%s%i: Invalid%s volt reply: '%s'",
  911. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  912. free(tmp);
  913. freebreakdown(&count, &firstname, &fields);
  914. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  915. return false;
  916. }
  917. sc_dev = &sc_info->sc_devs[dev];
  918. vcc1 = (float)atoi(fields[0]) / 1000.0;
  919. vcc2 = (float)atoi(fields[1]) / 1000.0;
  920. vmain = (float)atoi(fields[2]) / 1000.0;
  921. freebreakdown(&count, &firstname, &fields);
  922. if (vcc1 > 0 || vcc2 > 0 || vmain > 0) {
  923. wr_lock(&(sc_info->stat_lock));
  924. if (vcc1 > 0) {
  925. if (unlikely(sc_dev->vcc1 == 0))
  926. sc_dev->vcc1 = vcc1;
  927. else {
  928. sc_dev->vcc1 += vcc1 * 0.63;
  929. sc_dev->vcc1 /= 1.63;
  930. }
  931. }
  932. if (vcc2 > 0) {
  933. if (unlikely(sc_dev->vcc2 == 0))
  934. sc_dev->vcc2 = vcc2;
  935. else {
  936. sc_dev->vcc2 += vcc2 * 0.63;
  937. sc_dev->vcc2 /= 1.63;
  938. }
  939. }
  940. if (vmain > 0) {
  941. if (unlikely(sc_dev->vmain == 0))
  942. sc_dev->vmain = vmain;
  943. else {
  944. sc_dev->vmain += vmain * 0.63;
  945. sc_dev->vmain /= 1.63;
  946. }
  947. }
  948. wr_unlock(&(sc_info->stat_lock));
  949. }
  950. if (temp1 > 0 || temp2 > 0) {
  951. wr_lock(&(sc_info->stat_lock));
  952. if (unlikely(!sc_dev->temp1))
  953. sc_dev->temp1 = temp1;
  954. else {
  955. sc_dev->temp1 += temp1 * 0.63;
  956. sc_dev->temp1 /= 1.63;
  957. }
  958. if (unlikely(!sc_dev->temp2))
  959. sc_dev->temp2 = temp2;
  960. else {
  961. sc_dev->temp2 += temp2 * 0.63;
  962. sc_dev->temp2 /= 1.63;
  963. }
  964. if (temp1 > sc_dev->temp1_max) {
  965. sc_dev->temp1_max = temp1;
  966. sc_dev->temp1_max_time = time(NULL);
  967. }
  968. if (temp2 > sc_dev->temp2_max) {
  969. sc_dev->temp2_max = temp2;
  970. sc_dev->temp2_max_time = time(NULL);
  971. }
  972. if (unlikely(sc_dev->temp1_5min_av == 0))
  973. sc_dev->temp1_5min_av = temp1;
  974. else {
  975. sc_dev->temp1_5min_av += temp1 * .0042;
  976. sc_dev->temp1_5min_av /= 1.0042;
  977. }
  978. if (unlikely(sc_dev->temp2_5min_av == 0))
  979. sc_dev->temp2_5min_av = temp2;
  980. else {
  981. sc_dev->temp2_5min_av += temp2 * .0042;
  982. sc_dev->temp2_5min_av /= 1.0042;
  983. }
  984. wr_unlock(&(sc_info->stat_lock));
  985. if (temp < temp2)
  986. temp = temp2;
  987. bflsc->temp = temp;
  988. if (bflsc->cutofftemp > 0 && temp >= bflsc->cutofftemp) {
  989. applog(LOG_WARNING, "%s%i:%s temp (%.1f) hit thermal cutoff limit %d, stopping work!",
  990. bflsc->drv->name, bflsc->device_id, xlink,
  991. temp, bflsc->cutofftemp);
  992. dev_error(bflsc, REASON_DEV_THERMAL_CUTOFF);
  993. sc_dev->overheat = true;
  994. flush_one_dev(bflsc, dev);
  995. return false;
  996. }
  997. if (bflsc->cutofftemp > 0 && temp < (bflsc->cutofftemp - BFLSC_TEMP_RECOVER))
  998. sc_dev->overheat = false;
  999. }
  1000. return true;
  1001. }
  1002. static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char *data, int count, char **fields, int *nonces)
  1003. {
  1004. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1005. char midstate[MIDSTATE_BYTES], blockdata[MERKLE_BYTES];
  1006. struct work *work;
  1007. uint32_t nonce;
  1008. int i, num, x;
  1009. bool res;
  1010. char *tmp;
  1011. if (count < sc_info->que_fld_min) {
  1012. tmp = str_text(data);
  1013. applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
  1014. "%s%i:%s work returned too small (%d,%s)",
  1015. bflsc->drv->name, bflsc->device_id, xlink, count, tmp);
  1016. free(tmp);
  1017. inc_hw_errors(bflsc->thr[0]);
  1018. return;
  1019. }
  1020. if (count > sc_info->que_fld_max) {
  1021. applog(LOG_INFO, "%s%i:%s work returned too large (%d) processing %d anyway",
  1022. bflsc->drv->name, bflsc->device_id, xlink, count, sc_info->que_fld_max);
  1023. count = sc_info->que_fld_max;
  1024. inc_hw_errors(bflsc->thr[0]);
  1025. }
  1026. num = atoi(fields[sc_info->que_noncecount]);
  1027. if (num != count - sc_info->que_fld_min) {
  1028. tmp = str_text(data);
  1029. applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
  1030. "%s%i:%s incorrect data count (%d) will use %d instead from (%s)",
  1031. bflsc->drv->name, bflsc->device_id, xlink, num,
  1032. 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 = take_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. applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
  1061. "%s%i:%s invalid nonce (%s) will try to process anyway",
  1062. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1063. free(tmp);
  1064. }
  1065. hex2bin((void*)&nonce, fields[i], 4);
  1066. nonce = htobe32(nonce);
  1067. res = submit_nonce(bflsc->thr[0], work, nonce);
  1068. if (res) {
  1069. wr_lock(&(sc_info->stat_lock));
  1070. sc_info->sc_devs[dev].nonces_found++;
  1071. wr_unlock(&(sc_info->stat_lock));
  1072. (*nonces)++;
  1073. x++;
  1074. }
  1075. }
  1076. wr_lock(&(sc_info->stat_lock));
  1077. if (res)
  1078. sc_info->sc_devs[dev].result_id++;
  1079. if (x > QUE_MAX_RESULTS)
  1080. x = QUE_MAX_RESULTS + 1;
  1081. (sc_info->result_size[x])++;
  1082. sc_info->sc_devs[dev].work_complete++;
  1083. sc_info->sc_devs[dev].hashes_unsent += FULLNONCE;
  1084. // If not flushed (stale)
  1085. if (!(work->devflag))
  1086. sc_info->sc_devs[dev].work_queued -= 1;
  1087. wr_unlock(&(sc_info->stat_lock));
  1088. free_work(work);
  1089. }
  1090. static int process_results(struct cgpu_info *bflsc, int dev, char *pbuf, int *nonces)
  1091. {
  1092. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1093. char **items, *firstname, **fields, *lf;
  1094. int que = 0, i, lines, count;
  1095. char *tmp, *tmp2, *buf;
  1096. char xlink[17];
  1097. bool res;
  1098. *nonces = 0;
  1099. xlinkstr(xlink, sizeof(xlink), dev, sc_info);
  1100. buf = strdup(pbuf);
  1101. res = tolines(bflsc, dev, buf, &lines, &items, C_GETRESULTS);
  1102. free(buf);
  1103. if (!res || lines < 1) {
  1104. tmp = str_text(pbuf);
  1105. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1106. "%s%i:%s empty result (%s) ignored",
  1107. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1108. free(tmp);
  1109. goto arigatou;
  1110. }
  1111. if (lines < QUE_RES_LINES_MIN) {
  1112. tmp = str_text(pbuf);
  1113. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1114. "%s%i:%s result of %d too small (%s) ignored",
  1115. bflsc->drv->name, bflsc->device_id, xlink, lines, tmp);
  1116. free(tmp);
  1117. goto arigatou;
  1118. }
  1119. breakdown(ONECOLON, items[1], &count, &firstname, &fields, &lf);
  1120. if (count < 1) {
  1121. tmp = str_text(pbuf);
  1122. tmp2 = str_text(items[1]);
  1123. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1124. "%s%i:%s empty result count (%s) in (%s) ignoring",
  1125. bflsc->drv->name, bflsc->device_id, xlink, tmp2, tmp);
  1126. free(tmp2);
  1127. free(tmp);
  1128. goto arigatou;
  1129. } else if (count != 1) {
  1130. tmp = str_text(pbuf);
  1131. tmp2 = str_text(items[1]);
  1132. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1133. "%s%i:%s incorrect result count %d (%s) in (%s) will try anyway",
  1134. bflsc->drv->name, bflsc->device_id, xlink, count, tmp2, tmp);
  1135. free(tmp2);
  1136. free(tmp);
  1137. }
  1138. que = atoi(fields[0]);
  1139. if (que != (lines - QUE_RES_LINES_MIN)) {
  1140. i = que;
  1141. // 1+ In case the last line isn't 'OK' - try to process it
  1142. que = 1 + lines - QUE_RES_LINES_MIN;
  1143. tmp = str_text(pbuf);
  1144. tmp2 = str_text(items[0]);
  1145. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1146. "%s%i:%s incorrect result count %d (%s) will try %d (%s)",
  1147. bflsc->drv->name, bflsc->device_id, xlink, i, tmp2, que, tmp);
  1148. free(tmp2);
  1149. free(tmp);
  1150. }
  1151. freebreakdown(&count, &firstname, &fields);
  1152. for (i = 0; i < que; i++) {
  1153. res = breakdown(NOCOLON, items[i + QUE_RES_LINES_MIN - 1], &count, &firstname, &fields, &lf);
  1154. if (likely(res))
  1155. process_nonces(bflsc, dev, &(xlink[0]), items[i], count, fields, nonces);
  1156. else
  1157. applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
  1158. "%s%i:%s failed to process nonce %s",
  1159. bflsc->drv->name, bflsc->device_id, xlink, items[i]);
  1160. freebreakdown(&count, &firstname, &fields);
  1161. sc_info->not_first_work = true;
  1162. }
  1163. arigatou:
  1164. freetolines(&lines, &items);
  1165. return que;
  1166. }
  1167. #define TVF(tv) ((float)((tv)->tv_sec) + ((float)((tv)->tv_usec) / 1000000.0))
  1168. #define TVFMS(tv) (TVF(tv) * 1000.0)
  1169. // Thread to simply keep looking for results
  1170. static void *bflsc_get_results(void *userdata)
  1171. {
  1172. struct cgpu_info *bflsc = (struct cgpu_info *)userdata;
  1173. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1174. struct timeval elapsed, now;
  1175. float oldest, f;
  1176. char buf[BFLSC_BUFSIZ+1];
  1177. int err, amount;
  1178. int i, que, dev, nonces;
  1179. bool readok;
  1180. cgtime(&now);
  1181. for (i = 0; i < sc_info->sc_count; i++) {
  1182. copy_time(&(sc_info->sc_devs[i].last_check_result), &now);
  1183. copy_time(&(sc_info->sc_devs[i].last_dev_result), &now);
  1184. copy_time(&(sc_info->sc_devs[i].last_nonce_result), &now);
  1185. }
  1186. while (sc_info->shutdown == false) {
  1187. cgtimer_t ts_start;
  1188. if (bflsc->usbinfo.nodev)
  1189. return NULL;
  1190. dev = -1;
  1191. oldest = FLT_MAX;
  1192. cgtime(&now);
  1193. // Find the first oldest ... that also needs checking
  1194. for (i = 0; i < sc_info->sc_count; i++) {
  1195. timersub(&now, &(sc_info->sc_devs[i].last_check_result), &elapsed);
  1196. f = TVFMS(&elapsed);
  1197. if (f < oldest && f >= sc_info->sc_devs[i].ms_work) {
  1198. f = oldest;
  1199. dev = i;
  1200. }
  1201. }
  1202. if (bflsc->usbinfo.nodev)
  1203. return NULL;
  1204. cgsleep_prepare_r(&ts_start);
  1205. if (dev == -1)
  1206. goto utsura;
  1207. cgtime(&(sc_info->sc_devs[dev].last_check_result));
  1208. readok = bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, false);
  1209. if (err < 0 || (!readok && amount != BFLSC_QRES_LEN) || (readok && amount < 1)) {
  1210. // TODO: do what else?
  1211. } else {
  1212. que = process_results(bflsc, dev, buf, &nonces);
  1213. sc_info->not_first_work = true; // in case it failed processing it
  1214. if (que > 0)
  1215. cgtime(&(sc_info->sc_devs[dev].last_dev_result));
  1216. if (nonces > 0)
  1217. cgtime(&(sc_info->sc_devs[dev].last_nonce_result));
  1218. // TODO: if not getting results ... reinit?
  1219. }
  1220. utsura:
  1221. cgsleep_ms_r(&ts_start, sc_info->results_sleep_time);
  1222. }
  1223. return NULL;
  1224. }
  1225. static bool bflsc_thread_prepare(struct thr_info *thr)
  1226. {
  1227. struct cgpu_info *bflsc = thr->cgpu;
  1228. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1229. if (thr_info_create(&(sc_info->results_thr), NULL, bflsc_get_results, (void *)bflsc)) {
  1230. applog(LOG_ERR, "%s%i: thread create failed", bflsc->drv->name, bflsc->device_id);
  1231. return false;
  1232. }
  1233. pthread_detach(sc_info->results_thr.pth);
  1234. return true;
  1235. }
  1236. static void bflsc_shutdown(struct thr_info *thr)
  1237. {
  1238. struct cgpu_info *bflsc = thr->cgpu;
  1239. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1240. bflsc_flush_work(bflsc);
  1241. sc_info->shutdown = true;
  1242. }
  1243. static void bflsc_thread_enable(struct thr_info *thr)
  1244. {
  1245. struct cgpu_info *bflsc = thr->cgpu;
  1246. if (bflsc->usbinfo.nodev)
  1247. return;
  1248. bflsc_initialise(bflsc);
  1249. }
  1250. static bool bflsc_send_work(struct cgpu_info *bflsc, int dev, bool mandatory)
  1251. {
  1252. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1253. struct FullNonceRangeJob data;
  1254. char buf[BFLSC_BUFSIZ+1];
  1255. bool sent, ret = false;
  1256. struct work *work;
  1257. int err, amount;
  1258. int len, try;
  1259. int stage;
  1260. // Device is gone
  1261. if (bflsc->usbinfo.nodev)
  1262. return false;
  1263. // TODO: handle this everywhere
  1264. if (sc_info->sc_devs[dev].overheat == true)
  1265. return false;
  1266. // Initially code only deals with sending one work item
  1267. data.payloadSize = BFLSC_JOBSIZ;
  1268. data.endOfBlock = BFLSC_EOB;
  1269. len = sizeof(struct FullNonceRangeJob);
  1270. /* On faster devices we have a lot of lock contention so only
  1271. * mandatorily grab the lock and send work if the queue is empty since
  1272. * we have a submit queue. */
  1273. if (mandatory)
  1274. mutex_lock(&(bflsc->device_mutex));
  1275. else {
  1276. if (mutex_trylock(&bflsc->device_mutex))
  1277. return ret;
  1278. }
  1279. work = get_queued(bflsc);
  1280. if (unlikely(!work)) {
  1281. mutex_unlock(&bflsc->device_mutex);
  1282. return ret;
  1283. }
  1284. memcpy(data.midState, work->midstate, MIDSTATE_BYTES);
  1285. memcpy(data.blockData, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1286. try = 0;
  1287. re_send:
  1288. err = send_recv_ds(bflsc, dev, &stage, &sent, &amount,
  1289. BFLSC_QJOB, BFLSC_QJOB_LEN, C_REQUESTQUEJOB, C_REQUESTQUEJOBSTATUS,
  1290. (char *)&data, len, C_QUEJOB, C_QUEJOBSTATUS,
  1291. buf, sizeof(buf)-1);
  1292. mutex_unlock(&(bflsc->device_mutex));
  1293. switch (stage) {
  1294. case 1:
  1295. if (!sent) {
  1296. bflsc_applog(bflsc, dev, C_REQUESTQUEJOB, amount, err);
  1297. goto out;
  1298. } else {
  1299. // TODO: handle other errors ...
  1300. // Try twice
  1301. if (try++ < 1 && amount > 1 &&
  1302. strstr(buf, BFLSC_TIMEOUT))
  1303. goto re_send;
  1304. bflsc_applog(bflsc, dev, C_REQUESTQUEJOBSTATUS, amount, err);
  1305. goto out;
  1306. }
  1307. break;
  1308. case 2:
  1309. if (!sent) {
  1310. bflsc_applog(bflsc, dev, C_QUEJOB, amount, err);
  1311. goto out;
  1312. } else {
  1313. if (!isokerr(err, buf, amount)) {
  1314. // TODO: check for QUEUE FULL and set work_queued to sc_info->que_size
  1315. // and report a code bug LOG_ERR - coz it should never happen
  1316. // TODO: handle other errors ...
  1317. // Try twice
  1318. if (try++ < 1 && amount > 1 &&
  1319. strstr(buf, BFLSC_TIMEOUT))
  1320. goto re_send;
  1321. bflsc_applog(bflsc, dev, C_QUEJOBSTATUS, amount, err);
  1322. goto out;
  1323. }
  1324. }
  1325. break;
  1326. }
  1327. wr_lock(&(sc_info->stat_lock));
  1328. sc_info->sc_devs[dev].work_queued++;
  1329. wr_unlock(&(sc_info->stat_lock));
  1330. work->subid = dev;
  1331. ret = true;
  1332. out:
  1333. if (unlikely(!ret))
  1334. work_completed(bflsc, work);
  1335. return ret;
  1336. }
  1337. static bool bflsc_queue_full(struct cgpu_info *bflsc)
  1338. {
  1339. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1340. int i, dev, tried, que;
  1341. bool ret = false;
  1342. int tries = 0;
  1343. tried = -1;
  1344. // if something is wrong with a device try the next one available
  1345. // TODO: try them all? Add an unavailable flag to sc_devs[i] init to 0 here first
  1346. while (++tries < 3) {
  1347. bool mandatory = false;
  1348. // Device is gone - shouldn't normally get here
  1349. if (bflsc->usbinfo.nodev) {
  1350. ret = true;
  1351. break;
  1352. }
  1353. dev = -1;
  1354. rd_lock(&(sc_info->stat_lock));
  1355. // Anything waiting - gets the work first
  1356. for (i = 0; i < sc_info->sc_count; i++) {
  1357. // TODO: and ignore x-link dead - once I work out how to decide it is dead
  1358. if (i != tried && sc_info->sc_devs[i].work_queued == 0 &&
  1359. !sc_info->sc_devs[i].overheat) {
  1360. dev = i;
  1361. break;
  1362. }
  1363. }
  1364. if (dev == -1) {
  1365. que = sc_info->que_size * 10; // 10x is certainly above the MAX it could be
  1366. // The first device with the smallest amount queued
  1367. for (i = 0; i < sc_info->sc_count; i++) {
  1368. if (i != tried && sc_info->sc_devs[i].work_queued < que &&
  1369. !sc_info->sc_devs[i].overheat) {
  1370. dev = i;
  1371. que = sc_info->sc_devs[i].work_queued;
  1372. }
  1373. }
  1374. if (que > sc_info->que_full_enough)
  1375. dev = -1;
  1376. else if (que < sc_info->que_low)
  1377. mandatory = true;
  1378. }
  1379. rd_unlock(&(sc_info->stat_lock));
  1380. // nothing needs work yet
  1381. if (dev == -1) {
  1382. ret = true;
  1383. break;
  1384. }
  1385. if (bflsc_send_work(bflsc, dev, mandatory))
  1386. break;
  1387. else
  1388. tried = dev;
  1389. }
  1390. return ret;
  1391. }
  1392. static int64_t bflsc_scanwork(struct thr_info *thr)
  1393. {
  1394. struct cgpu_info *bflsc = thr->cgpu;
  1395. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1396. int64_t ret, unsent;
  1397. bool flushed, cleanup;
  1398. struct work *work, *tmp;
  1399. int dev, waited, i;
  1400. // Device is gone
  1401. if (bflsc->usbinfo.nodev)
  1402. return -1;
  1403. flushed = false;
  1404. // Single lock check if any are flagged as flushed
  1405. rd_lock(&(sc_info->stat_lock));
  1406. for (dev = 0; dev < sc_info->sc_count; dev++)
  1407. flushed |= sc_info->sc_devs[dev].flushed;
  1408. rd_unlock(&(sc_info->stat_lock));
  1409. // > 0 flagged as flushed
  1410. if (flushed) {
  1411. // TODO: something like this ......
  1412. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1413. cleanup = false;
  1414. // Is there any flushed work that can be removed?
  1415. rd_lock(&(sc_info->stat_lock));
  1416. if (sc_info->sc_devs[dev].flushed) {
  1417. if (sc_info->sc_devs[dev].result_id > (sc_info->sc_devs[dev].flush_id + sc_info->flush_size))
  1418. cleanup = true;
  1419. }
  1420. rd_unlock(&(sc_info->stat_lock));
  1421. // yes remove the flushed work that can be removed
  1422. if (cleanup) {
  1423. wr_lock(&bflsc->qlock);
  1424. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  1425. if (work->devflag && work->subid == dev) {
  1426. bflsc->queued_count--;
  1427. HASH_DEL(bflsc->queued_work, work);
  1428. discard_work(work);
  1429. }
  1430. }
  1431. wr_unlock(&bflsc->qlock);
  1432. wr_lock(&(sc_info->stat_lock));
  1433. sc_info->sc_devs[dev].flushed = false;
  1434. wr_unlock(&(sc_info->stat_lock));
  1435. }
  1436. }
  1437. }
  1438. waited = restart_wait(thr, sc_info->scan_sleep_time);
  1439. if (waited == ETIMEDOUT) {
  1440. unsigned int old_sleep_time, new_sleep_time = 0;
  1441. int min_queued = sc_info->que_size;
  1442. /* Only adjust the scan_sleep_time if we did not receive a
  1443. * restart message while waiting. Try to adjust sleep time
  1444. * so we drop to sc_info->que_watermark before getting more work.
  1445. */
  1446. rd_lock(&sc_info->stat_lock);
  1447. old_sleep_time = sc_info->scan_sleep_time;
  1448. for (i = 0; i < sc_info->sc_count; i++) {
  1449. if (sc_info->sc_devs[i].work_queued < min_queued)
  1450. min_queued = sc_info->sc_devs[i].work_queued;
  1451. }
  1452. rd_unlock(&sc_info->stat_lock);
  1453. new_sleep_time = old_sleep_time;
  1454. /* Increase slowly but decrease quickly */
  1455. if (min_queued > sc_info->que_full_enough && old_sleep_time < BFLSC_MAX_SLEEP)
  1456. new_sleep_time = old_sleep_time * 21 / 20;
  1457. else if (min_queued < sc_info->que_low)
  1458. new_sleep_time = old_sleep_time * 2 / 3;
  1459. /* Do not sleep more than BFLSC_MAX_SLEEP so we can always
  1460. * report in at least 2 results per 5s log interval. */
  1461. if (new_sleep_time != old_sleep_time) {
  1462. if (new_sleep_time > BFLSC_MAX_SLEEP)
  1463. new_sleep_time = BFLSC_MAX_SLEEP;
  1464. else if (new_sleep_time == 0)
  1465. new_sleep_time = 1;
  1466. applog(LOG_DEBUG, "%s%i: Changed scan sleep time to %d",
  1467. bflsc->drv->name, bflsc->device_id, new_sleep_time);
  1468. wr_lock(&sc_info->stat_lock);
  1469. sc_info->scan_sleep_time = new_sleep_time;
  1470. wr_unlock(&sc_info->stat_lock);
  1471. }
  1472. }
  1473. // Count up the work done since we last were here
  1474. ret = 0;
  1475. wr_lock(&(sc_info->stat_lock));
  1476. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1477. unsent = sc_info->sc_devs[dev].hashes_unsent;
  1478. sc_info->sc_devs[dev].hashes_unsent = 0;
  1479. sc_info->sc_devs[dev].hashes_sent += unsent;
  1480. sc_info->hashes_sent += unsent;
  1481. ret += unsent;
  1482. }
  1483. wr_unlock(&(sc_info->stat_lock));
  1484. return ret;
  1485. }
  1486. #define BFLSC_OVER_TEMP 60
  1487. /* Set the fanspeed to auto for any valid value <= BFLSC_OVER_TEMP,
  1488. * or max for any value > BFLSC_OVER_TEMP or if we don't know the temperature. */
  1489. static void bflsc_set_fanspeed(struct cgpu_info *bflsc)
  1490. {
  1491. struct bflsc_info *sc_info = (struct bflsc_info *)bflsc->device_data;
  1492. char buf[BFLSC_BUFSIZ+1];
  1493. char data[16+1];
  1494. int amount;
  1495. bool sent;
  1496. if ((bflsc->temp <= BFLSC_OVER_TEMP && bflsc->temp > 0 && sc_info->fanauto) ||
  1497. ((bflsc->temp > BFLSC_OVER_TEMP || !bflsc->temp) && !sc_info->fanauto))
  1498. return;
  1499. if (bflsc->temp > BFLSC_OVER_TEMP || !bflsc->temp) {
  1500. strcpy(data, BFLSC_FAN4);
  1501. sc_info->fanauto = false;
  1502. } else {
  1503. strcpy(data, BFLSC_FANAUTO);
  1504. sc_info->fanauto = true;
  1505. }
  1506. applog(LOG_DEBUG, "%s%i: temp=%.0f over=%d set fan to %s",
  1507. bflsc->drv->name, bflsc->device_id, bflsc->temp,
  1508. BFLSC_OVER_TEMP, data);
  1509. mutex_lock(&bflsc->device_mutex);
  1510. send_recv_ss(bflsc, 0, &sent, &amount,
  1511. data, strlen(data), C_SETFAN,
  1512. buf, sizeof(buf)-1, C_FANREPLY, READ_NL);
  1513. mutex_unlock(&bflsc->device_mutex);
  1514. }
  1515. static bool bflsc_get_stats(struct cgpu_info *bflsc)
  1516. {
  1517. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1518. bool allok = true;
  1519. int i;
  1520. // Device is gone
  1521. if (bflsc->usbinfo.nodev)
  1522. return false;
  1523. for (i = 0; i < sc_info->sc_count; i++) {
  1524. if (!bflsc_get_temp(bflsc, i))
  1525. allok = false;
  1526. // Device is gone
  1527. if (bflsc->usbinfo.nodev)
  1528. return false;
  1529. if (i < (sc_info->sc_count - 1))
  1530. cgsleep_ms(BFLSC_TEMP_SLEEPMS);
  1531. }
  1532. bflsc_set_fanspeed(bflsc);
  1533. return allok;
  1534. }
  1535. static void bflsc_identify(struct cgpu_info *bflsc)
  1536. {
  1537. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1538. // TODO: handle x-link
  1539. sc_info->flash_led = true;
  1540. }
  1541. static bool bflsc_thread_init(struct thr_info *thr)
  1542. {
  1543. struct cgpu_info *bflsc = thr->cgpu;
  1544. if (bflsc->usbinfo.nodev)
  1545. return false;
  1546. bflsc_initialise(bflsc);
  1547. return true;
  1548. }
  1549. // there should be a new API function to return device info that isn't the standard stuff
  1550. // instead of bflsc_api_stats - since the stats should really just be internal code info
  1551. // and the new one should be UNusual device stats/extra details - like the stuff below
  1552. static struct api_data *bflsc_api_stats(struct cgpu_info *bflsc)
  1553. {
  1554. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1555. struct api_data *root = NULL;
  1556. char buf[256];
  1557. int i;
  1558. //if no x-link ... etc
  1559. rd_lock(&(sc_info->stat_lock));
  1560. root = api_add_temp(root, "Temp1", &(sc_info->sc_devs[0].temp1), true);
  1561. root = api_add_temp(root, "Temp2", &(sc_info->sc_devs[0].temp2), true);
  1562. root = api_add_volts(root, "Vcc1", &(sc_info->sc_devs[0].vcc1), true);
  1563. root = api_add_volts(root, "Vcc2", &(sc_info->sc_devs[0].vcc2), true);
  1564. root = api_add_volts(root, "Vmain", &(sc_info->sc_devs[0].vmain), true);
  1565. root = api_add_temp(root, "Temp1 Max", &(sc_info->sc_devs[0].temp1_max), true);
  1566. root = api_add_temp(root, "Temp2 Max", &(sc_info->sc_devs[0].temp2_max), true);
  1567. root = api_add_time(root, "Temp1 Max Time", &(sc_info->sc_devs[0].temp1_max_time), true);
  1568. root = api_add_time(root, "Temp2 Max Time", &(sc_info->sc_devs[0].temp2_max_time), true);
  1569. root = api_add_int(root, "Work Queued", &(sc_info->sc_devs[0].work_queued), true);
  1570. root = api_add_int(root, "Work Complete", &(sc_info->sc_devs[0].work_complete), true);
  1571. root = api_add_bool(root, "Overheat", &(sc_info->sc_devs[0].overheat), true);
  1572. root = api_add_uint64(root, "Flush ID", &(sc_info->sc_devs[0].flush_id), true);
  1573. root = api_add_uint64(root, "Result ID", &(sc_info->sc_devs[0].result_id), true);
  1574. root = api_add_bool(root, "Flushed", &(sc_info->sc_devs[0].flushed), true);
  1575. root = api_add_uint(root, "Scan Sleep", &(sc_info->scan_sleep_time), true);
  1576. root = api_add_uint(root, "Results Sleep", &(sc_info->results_sleep_time), true);
  1577. root = api_add_uint(root, "Work ms", &(sc_info->default_ms_work), true);
  1578. buf[0] = '\0';
  1579. for (i = 0; i <= QUE_MAX_RESULTS + 1; i++)
  1580. tailsprintf(buf, sizeof(buf), "%s%"PRIu64, (i > 0) ? "/" : "", sc_info->result_size[i]);
  1581. root = api_add_string(root, "Result Size", buf, true);
  1582. rd_unlock(&(sc_info->stat_lock));
  1583. i = (int)(sc_info->driver_version);
  1584. root = api_add_int(root, "Driver", &i, true);
  1585. root = api_add_string(root, "Firmware", sc_info->sc_devs[0].firmware, false);
  1586. root = api_add_string(root, "Chips", sc_info->sc_devs[0].chips, false);
  1587. root = api_add_int(root, "Que Size", &(sc_info->que_size), false);
  1588. root = api_add_int(root, "Que Full", &(sc_info->que_full_enough), false);
  1589. root = api_add_int(root, "Que Watermark", &(sc_info->que_watermark), false);
  1590. root = api_add_int(root, "Que Low", &(sc_info->que_low), false);
  1591. root = api_add_escape(root, "GetInfo", sc_info->sc_devs[0].getinfo, false);
  1592. /*
  1593. else a whole lot of something like these ... etc
  1594. root = api_add_temp(root, "X-%d-Temp1", &(sc_info->temp1), false);
  1595. root = api_add_temp(root, "X-%d-Temp2", &(sc_info->temp2), false);
  1596. root = api_add_volts(root, "X-%d-Vcc1", &(sc_info->vcc1), false);
  1597. root = api_add_volts(root, "X-%d-Vcc2", &(sc_info->vcc2), false);
  1598. root = api_add_volts(root, "X-%d-Vmain", &(sc_info->vmain), false);
  1599. */
  1600. return root;
  1601. }
  1602. struct device_drv bflsc_drv = {
  1603. .drv_id = DRIVER_BFLSC,
  1604. .dname = "BitForceSC",
  1605. .name = BFLSC_SINGLE,
  1606. .drv_detect = bflsc_detect,
  1607. .get_api_stats = bflsc_api_stats,
  1608. .get_statline_before = get_bflsc_statline_before,
  1609. .get_stats = bflsc_get_stats,
  1610. .identify_device = bflsc_identify,
  1611. .thread_prepare = bflsc_thread_prepare,
  1612. .thread_init = bflsc_thread_init,
  1613. .hash_work = hash_queued_work,
  1614. .scanwork = bflsc_scanwork,
  1615. .queue_full = bflsc_queue_full,
  1616. .flush_work = bflsc_flush_work,
  1617. .thread_shutdown = bflsc_shutdown,
  1618. .thread_enable = bflsc_thread_enable
  1619. };