driver-bflsc.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
  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. int opt_bflsc_overheat = BFLSC_TEMP_OVERHEAT;
  27. static const char *blank = "";
  28. struct device_drv bflsc_drv;
  29. static enum driver_version drv_ver(struct cgpu_info *bflsc, const char *ver)
  30. {
  31. char *tmp;
  32. if (strcmp(ver, "1.0.0") == 0)
  33. return BFLSC_DRV1;
  34. if (strncmp(ver, "1.0", 3) == 0 || strncmp(ver, "1.1", 3) == 0) {
  35. applog(LOG_WARNING, "%s detect (%s) Warning assuming firmware '%s' is Ver1",
  36. bflsc->drv->dname, bflsc->device_path, ver);
  37. return BFLSC_DRV1;
  38. }
  39. if (strncmp(ver, "1.2", 3) == 0)
  40. return BFLSC_DRV2;
  41. tmp = str_text((char *)ver);
  42. applog(LOG_WARNING, "%s detect (%s) Warning unknown firmware '%s' using Ver2",
  43. bflsc->drv->dname, bflsc->device_path, tmp);
  44. free(tmp);
  45. return BFLSC_DRV2;
  46. }
  47. static void xlinkstr(char *xlink, size_t siz, int dev, struct bflsc_info *sc_info)
  48. {
  49. if (dev > 0)
  50. snprintf(xlink, siz, " x-%d", dev);
  51. else {
  52. if (sc_info->sc_count > 1)
  53. strcpy(xlink, " master");
  54. else
  55. *xlink = '\0';
  56. }
  57. }
  58. static void bflsc_applog(struct cgpu_info *bflsc, int dev, enum usb_cmds cmd, int amount, int err)
  59. {
  60. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  61. char xlink[17];
  62. xlinkstr(xlink, sizeof(xlink), dev, sc_info);
  63. usb_applog(bflsc, cmd, xlink, amount, err);
  64. }
  65. // Break an input up into lines with LFs removed
  66. // false means an error, but if *lines > 0 then data was also found
  67. // error would be no data or missing LF at the end
  68. static bool tolines(struct cgpu_info *bflsc, int dev, char *buf, int *lines, char ***items, enum usb_cmds cmd)
  69. {
  70. bool ok = false;
  71. char *tok;
  72. #define p_lines (*lines)
  73. #define p_items (*items)
  74. p_lines = 0;
  75. p_items = NULL;
  76. if (!buf || !(*buf)) {
  77. applog(LOG_DEBUG, "USB: %s%i: (%d) empty %s",
  78. bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
  79. return ok;
  80. }
  81. tok = strtok(buf, "\n");
  82. if (!tok) {
  83. applog(LOG_DEBUG, "USB: %s%i: (%d) missing lf(s) in %s",
  84. bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
  85. return ok;
  86. }
  87. while (tok) {
  88. if (strncasecmp(tok, BFLSC_INPROCESS, BFLSC_INPROCESS_LEN)) {
  89. ok = true;
  90. p_items = realloc(p_items, ++p_lines * sizeof(*p_items));
  91. if (unlikely(!p_items))
  92. quit(1, "Failed to realloc p_items in tolines");
  93. p_items[p_lines-1] = strdup(tok);
  94. } else {
  95. applog(LOG_WARNING, "%s%i: in process response (%s) ignored",
  96. bflsc->drv->name, bflsc->device_id, tok);
  97. }
  98. tok = strtok(NULL, "\n");
  99. }
  100. return ok;
  101. }
  102. static void freetolines(int *lines, char ***items)
  103. {
  104. if (*lines > 0) {
  105. free(**items);
  106. free(*items);
  107. }
  108. *lines = 0;
  109. *items = NULL;
  110. }
  111. enum breakmode {
  112. NOCOLON,
  113. ONECOLON,
  114. ALLCOLON // Temperature uses this
  115. };
  116. // Break down a single line into 'fields'
  117. // 'lf' will be a pointer to the final LF if it is there (or NULL)
  118. // firstname will be the allocated buf copy pointer which is also
  119. // the string before ':' for ONECOLON and ALLCOLON
  120. // If any string is missing the ':' when it was expected, false is returned
  121. static bool breakdown(enum breakmode mode, char *buf, int *count, char **firstname, char ***fields, char **lf)
  122. {
  123. char *ptr, *colon, *comma;
  124. bool ok = false;
  125. #define p_count (*count)
  126. #define p_firstname (*firstname)
  127. #define p_fields (*fields)
  128. #define p_lf (*lf)
  129. p_count = 0;
  130. p_firstname = NULL;
  131. p_fields = NULL;
  132. p_lf = NULL;
  133. if (!buf || !(*buf))
  134. return ok;
  135. ptr = p_firstname = strdup(buf);
  136. p_lf = strchr(p_firstname, '\n');
  137. if (mode == ONECOLON) {
  138. colon = strchr(ptr, ':');
  139. if (colon) {
  140. ptr = colon;
  141. *(ptr++) = '\0';
  142. } else
  143. return ok;
  144. }
  145. while (*ptr == ' ')
  146. ptr++;
  147. while (ptr && *ptr) {
  148. if (mode == ALLCOLON) {
  149. colon = strchr(ptr, ':');
  150. if (colon)
  151. ptr = colon + 1;
  152. else
  153. return ok;
  154. }
  155. while (*ptr == ' ')
  156. ptr++;
  157. comma = strchr(ptr, ',');
  158. if (comma)
  159. *(comma++) = '\0';
  160. p_fields = realloc(p_fields, ++p_count * sizeof(*p_fields));
  161. if (unlikely(!p_fields))
  162. quit(1, "Failed to realloc p_fields in breakdown");
  163. p_fields[p_count-1] = ptr;
  164. ptr = comma;
  165. }
  166. ok = true;
  167. return ok;
  168. }
  169. static void freebreakdown(int *count, char **firstname, char ***fields)
  170. {
  171. if (*firstname)
  172. free(*firstname);
  173. if (*count > 0)
  174. free(*fields);
  175. *count = 0;
  176. *firstname = NULL;
  177. *fields = NULL;
  178. }
  179. static bool isokerr(int err, char *buf, int amount)
  180. {
  181. if (err < 0 || amount < (int)BFLSC_OK_LEN)
  182. return false;
  183. else {
  184. if (strncmp(buf, BFLSC_ANERR, BFLSC_ANERR_LEN) == 0)
  185. return false;
  186. else
  187. return true;
  188. }
  189. }
  190. // send+receive dual stage - always single line replies
  191. 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)
  192. {
  193. struct DataForwardToChain data;
  194. int len, err, tried;
  195. if (dev == 0) {
  196. usb_buffer_clear(bflsc);
  197. *stage = 1;
  198. *sent = false;
  199. err = usb_write(bflsc, send1, send1_len, amount, send1_cmd);
  200. if (err < 0 || *amount < send1_len)
  201. return err;
  202. *sent = true;
  203. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
  204. if (!isokerr(err, recv, *amount))
  205. return err;
  206. usb_buffer_clear(bflsc);
  207. *stage = 2;
  208. *sent = false;
  209. err = usb_write(bflsc, send2, send2_len, amount, send2_cmd);
  210. if (err < 0 || *amount < send2_len)
  211. return err;
  212. *sent = true;
  213. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
  214. return err;
  215. }
  216. data.header = BFLSC_XLINKHDR;
  217. data.deviceAddress = (uint8_t)dev;
  218. tried = 0;
  219. while (tried++ < 3) {
  220. data.payloadSize = send1_len;
  221. memcpy(data.payloadData, send1, send1_len);
  222. len = DATAFORWARDSIZE(data);
  223. usb_buffer_clear(bflsc);
  224. *stage = 1;
  225. *sent = false;
  226. err = usb_write(bflsc, (char *)&data, len, amount, send1_cmd);
  227. if (err < 0 || *amount < send1_len)
  228. return err;
  229. *sent = true;
  230. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
  231. if (err != LIBUSB_SUCCESS)
  232. return err;
  233. // x-link timeout? - try again?
  234. if (strncasecmp(recv, BFLSC_XTIMEOUT, BFLSC_XTIMEOUT_LEN) == 0)
  235. continue;
  236. if (!isokerr(err, recv, *amount))
  237. return err;
  238. data.payloadSize = send2_len;
  239. memcpy(data.payloadData, send2, send2_len);
  240. len = DATAFORWARDSIZE(data);
  241. usb_buffer_clear(bflsc);
  242. *stage = 2;
  243. *sent = false;
  244. err = usb_write(bflsc, (char *)&data, len, amount, send2_cmd);
  245. if (err < 0 || *amount < send2_len)
  246. return err;
  247. *sent = true;
  248. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
  249. if (err != LIBUSB_SUCCESS)
  250. return err;
  251. // x-link timeout? - try again?
  252. if (strncasecmp(recv, BFLSC_XTIMEOUT, BFLSC_XTIMEOUT_LEN) == 0)
  253. continue;
  254. // SUCCESS - return it
  255. break;
  256. }
  257. return err;
  258. }
  259. #define READ_OK true
  260. #define READ_NL false
  261. // send+receive single stage
  262. 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)
  263. {
  264. struct DataForwardToChain data;
  265. int len, err, tried;
  266. if (dev == 0) {
  267. usb_buffer_clear(bflsc);
  268. *sent = false;
  269. err = usb_write(bflsc, send, send_len, amount, send_cmd);
  270. if (err < 0 || *amount < send_len) {
  271. // N.B. thus !(*sent) directly implies err < 0 or *amount < send_len
  272. return err;
  273. }
  274. *sent = true;
  275. if (read_ok == READ_OK)
  276. err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
  277. else
  278. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
  279. return err;
  280. }
  281. data.header = BFLSC_XLINKHDR;
  282. data.deviceAddress = (uint8_t)dev;
  283. data.payloadSize = send_len;
  284. memcpy(data.payloadData, send, send_len);
  285. len = DATAFORWARDSIZE(data);
  286. tried = 0;
  287. while (tried++ < 3) {
  288. usb_buffer_clear(bflsc);
  289. *sent = false;
  290. err = usb_write(bflsc, (char *)&data, len, amount, recv_cmd);
  291. if (err < 0 || *amount < send_len)
  292. return err;
  293. *sent = true;
  294. if (read_ok == READ_OK)
  295. err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
  296. else
  297. err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
  298. if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_TIMEOUT)
  299. return err;
  300. // read_ok can err timeout if it's looking for OK<LF>
  301. // TODO: add a usb_read() option to spot the ERR: and convert end=OK<LF> to just <LF>
  302. // x-link timeout? - try again?
  303. if ((err == LIBUSB_SUCCESS || (read_ok == READ_OK && err == LIBUSB_ERROR_TIMEOUT)) &&
  304. strncasecmp(recv, BFLSC_XTIMEOUT, BFLSC_XTIMEOUT_LEN) == 0)
  305. continue;
  306. // SUCCESS or TIMEOUT - return it
  307. break;
  308. }
  309. return err;
  310. }
  311. static int write_to_dev(struct cgpu_info *bflsc, int dev, char *buf, int buflen, int *amount, enum usb_cmds cmd)
  312. {
  313. struct DataForwardToChain data;
  314. int len;
  315. /*
  316. * The protocol is syncronous so any previous excess can be
  317. * discarded and assumed corrupt data or failed USB transfers
  318. */
  319. usb_buffer_clear(bflsc);
  320. if (dev == 0)
  321. return usb_write(bflsc, buf, buflen, amount, cmd);
  322. data.header = BFLSC_XLINKHDR;
  323. data.deviceAddress = (uint8_t)dev;
  324. data.payloadSize = buflen;
  325. memcpy(data.payloadData, buf, buflen);
  326. len = DATAFORWARDSIZE(data);
  327. return usb_write(bflsc, (char *)&data, len, amount, cmd);
  328. }
  329. static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
  330. {
  331. char buf[BFLSC_BUFSIZ+1];
  332. int err, amount;
  333. bool sent;
  334. // Device is gone
  335. if (bflsc->usbinfo.nodev)
  336. return;
  337. mutex_lock(&bflsc->device_mutex);
  338. err = send_recv_ss(bflsc, dev, &sent, &amount,
  339. BFLSC_QFLUSH, BFLSC_QFLUSH_LEN, C_QUEFLUSH,
  340. buf, sizeof(buf)-1, C_QUEFLUSHREPLY, READ_NL);
  341. mutex_unlock(&bflsc->device_mutex);
  342. if (!sent)
  343. bflsc_applog(bflsc, dev, C_QUEFLUSH, amount, err);
  344. else {
  345. // TODO: do we care if we don't get 'OK'? (always will in normal processing)
  346. }
  347. }
  348. /* return True = attempted usb_read_ok()
  349. * set ignore to true means no applog/ignore errors */
  350. static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore)
  351. {
  352. bool readok = false;
  353. mutex_lock(&(bflsc->device_mutex));
  354. *err = send_recv_ss(bflsc, dev, &readok, amount,
  355. BFLSC_QRES, BFLSC_QRES_LEN, C_REQUESTRESULTS,
  356. buf, bufsiz-1, C_GETRESULTS, READ_OK);
  357. mutex_unlock(&(bflsc->device_mutex));
  358. if (!readok) {
  359. if (!ignore)
  360. bflsc_applog(bflsc, dev, C_REQUESTRESULTS, *amount, *err);
  361. // TODO: do what? flag as dead device?
  362. // count how many times it has happened and reset/fail it
  363. // or even make sure it is all x-link and that means device
  364. // has failed after some limit of this?
  365. // of course all other I/O must also be failing ...
  366. } else {
  367. if (*err < 0 || *amount < 1) {
  368. if (!ignore)
  369. bflsc_applog(bflsc, dev, C_GETRESULTS, *amount, *err);
  370. // TODO: do what? ... see above
  371. }
  372. }
  373. return readok;
  374. }
  375. static void __bflsc_initialise(struct cgpu_info *bflsc)
  376. {
  377. int err;
  378. // TODO: does x-link bypass the other device FTDI? (I think it does)
  379. // So no initialisation required except for the master device?
  380. if (bflsc->usbinfo.nodev)
  381. return;
  382. // Reset
  383. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  384. FTDI_VALUE_RESET, bflsc->usbdev->found->interface, C_RESET);
  385. applog(LOG_DEBUG, "%s%i: reset got err %d",
  386. bflsc->drv->name, bflsc->device_id, err);
  387. if (bflsc->usbinfo.nodev)
  388. return;
  389. usb_ftdi_set_latency(bflsc);
  390. if (bflsc->usbinfo.nodev)
  391. return;
  392. // Set data control
  393. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  394. FTDI_VALUE_DATA_BAS, bflsc->usbdev->found->interface, C_SETDATA);
  395. applog(LOG_DEBUG, "%s%i: setdata got err %d",
  396. bflsc->drv->name, bflsc->device_id, err);
  397. if (bflsc->usbinfo.nodev)
  398. return;
  399. // Set the baud
  400. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BAS,
  401. (FTDI_INDEX_BAUD_BAS & 0xff00) | bflsc->usbdev->found->interface,
  402. C_SETBAUD);
  403. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  404. bflsc->drv->name, bflsc->device_id, err);
  405. if (bflsc->usbinfo.nodev)
  406. return;
  407. // Set Flow Control
  408. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  409. FTDI_VALUE_FLOW, bflsc->usbdev->found->interface, C_SETFLOW);
  410. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  411. bflsc->drv->name, bflsc->device_id, err);
  412. if (bflsc->usbinfo.nodev)
  413. return;
  414. // Set Modem Control
  415. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  416. FTDI_VALUE_MODEM, bflsc->usbdev->found->interface, C_SETMODEM);
  417. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  418. bflsc->drv->name, bflsc->device_id, err);
  419. if (bflsc->usbinfo.nodev)
  420. return;
  421. // Clear any sent data
  422. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  423. FTDI_VALUE_PURGE_TX, bflsc->usbdev->found->interface, C_PURGETX);
  424. applog(LOG_DEBUG, "%s%i: purgetx got err %d",
  425. bflsc->drv->name, bflsc->device_id, err);
  426. if (bflsc->usbinfo.nodev)
  427. return;
  428. // Clear any received data
  429. err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  430. FTDI_VALUE_PURGE_RX, bflsc->usbdev->found->interface, C_PURGERX);
  431. applog(LOG_DEBUG, "%s%i: purgerx got err %d",
  432. bflsc->drv->name, bflsc->device_id, err);
  433. if (!bflsc->cutofftemp)
  434. bflsc->cutofftemp = opt_bflsc_overheat;
  435. }
  436. static void bflsc_initialise(struct cgpu_info *bflsc)
  437. {
  438. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  439. char buf[BFLSC_BUFSIZ+1];
  440. int err, amount;
  441. int dev;
  442. mutex_lock(&(bflsc->device_mutex));
  443. __bflsc_initialise(bflsc);
  444. mutex_unlock(&(bflsc->device_mutex));
  445. for (dev = 0; dev < sc_info->sc_count; dev++) {
  446. bflsc_send_flush_work(bflsc, dev);
  447. bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, true);
  448. }
  449. }
  450. static bool getinfo(struct cgpu_info *bflsc, int dev)
  451. {
  452. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  453. struct bflsc_dev sc_dev;
  454. char buf[BFLSC_BUFSIZ+1];
  455. int err, amount;
  456. char **items, *firstname, **fields, *lf;
  457. bool res, ok = false;
  458. int i, lines, count;
  459. char *tmp;
  460. /*
  461. * Kano's first dev Jalapeno output:
  462. * DEVICE: BitFORCE SC<LF>
  463. * FIRMWARE: 1.0.0<LF>
  464. * ENGINES: 30<LF>
  465. * FREQUENCY: [UNKNOWN]<LF>
  466. * XLINK MODE: MASTER<LF>
  467. * XLINK PRESENT: YES<LF>
  468. * --DEVICES IN CHAIN: 0<LF>
  469. * --CHAIN PRESENCE MASK: 00000000<LF>
  470. * OK<LF>
  471. */
  472. /*
  473. * Don't use send_recv_ss() since we have a different receive timeout
  474. * Also getinfo() is called multiple times if it fails anyway
  475. */
  476. err = write_to_dev(bflsc, dev, BFLSC_DETAILS, BFLSC_DETAILS_LEN, &amount, C_REQUESTDETAILS);
  477. if (err < 0 || amount != BFLSC_DETAILS_LEN) {
  478. applog(LOG_ERR, "%s detect (%s) send details request failed (%d:%d)",
  479. bflsc->drv->dname, bflsc->device_path, amount, err);
  480. return ok;
  481. }
  482. err = usb_read_ok_timeout(bflsc, buf, sizeof(buf)-1, &amount,
  483. BFLSC_INFO_TIMEOUT, C_GETDETAILS);
  484. if (err < 0 || amount < 1) {
  485. if (err < 0) {
  486. applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)",
  487. bflsc->drv->dname, bflsc->device_path, amount, err);
  488. } else {
  489. applog(LOG_ERR, "%s detect (%s) get details returned nothing (%d:%d)",
  490. bflsc->drv->dname, bflsc->device_path, amount, err);
  491. }
  492. return ok;
  493. }
  494. memset(&sc_dev, 0, sizeof(struct bflsc_dev));
  495. sc_info->sc_count = 1;
  496. res = tolines(bflsc, dev, &(buf[0]), &lines, &items, C_GETDETAILS);
  497. if (!res)
  498. return ok;
  499. tmp = str_text(buf);
  500. strcpy(sc_dev.getinfo, tmp);
  501. free(tmp);
  502. for (i = 0; i < lines-2; i++) {
  503. res = breakdown(ONECOLON, items[i], &count, &firstname, &fields, &lf);
  504. if (lf)
  505. *lf = '\0';
  506. if (!res || count != 1) {
  507. tmp = str_text(items[i]);
  508. applog(LOG_WARNING, "%s detect (%s) invalid details line: '%s' %d",
  509. bflsc->drv->dname, bflsc->device_path, tmp, count);
  510. free(tmp);
  511. dev_error(bflsc, REASON_DEV_COMMS_ERROR);
  512. goto mata;
  513. }
  514. if (strcmp(firstname, BFLSC_DI_FIRMWARE) == 0) {
  515. sc_dev.firmware = strdup(fields[0]);
  516. sc_info->driver_version = drv_ver(bflsc, sc_dev.firmware);
  517. }
  518. else if (strcmp(firstname, BFLSC_DI_ENGINES) == 0) {
  519. sc_dev.engines = atoi(fields[0]);
  520. if (sc_dev.engines < 1) {
  521. tmp = str_text(items[i]);
  522. applog(LOG_WARNING, "%s detect (%s) invalid engine count: '%s'",
  523. bflsc->drv->dname, bflsc->device_path, tmp);
  524. free(tmp);
  525. goto mata;
  526. }
  527. }
  528. else if (strcmp(firstname, BFLSC_DI_XLINKMODE) == 0)
  529. sc_dev.xlink_mode = strdup(fields[0]);
  530. else if (strcmp(firstname, BFLSC_DI_XLINKPRESENT) == 0)
  531. sc_dev.xlink_present = strdup(fields[0]);
  532. else if (strcmp(firstname, BFLSC_DI_DEVICESINCHAIN) == 0) {
  533. sc_info->sc_count = atoi(fields[0]);
  534. if (sc_info->sc_count < 1 || sc_info->sc_count > 30) {
  535. tmp = str_text(items[i]);
  536. applog(LOG_WARNING, "%s detect (%s) invalid s-link count: '%s'",
  537. bflsc->drv->dname, bflsc->device_path, tmp);
  538. free(tmp);
  539. goto mata;
  540. }
  541. else if (strcmp(firstname, BFLSC_DI_CHIPS) == 0)
  542. sc_dev.chips = strdup(fields[0]);
  543. }
  544. freebreakdown(&count, &firstname, &fields);
  545. }
  546. if (sc_info->driver_version == BFLSC_DRVUNDEF) {
  547. applog(LOG_WARNING, "%s detect (%s) missing %s",
  548. bflsc->drv->dname, bflsc->device_path, BFLSC_DI_FIRMWARE);
  549. goto ne;
  550. }
  551. sc_info->sc_devs = calloc(sc_info->sc_count, sizeof(struct bflsc_dev));
  552. if (unlikely(!sc_info->sc_devs))
  553. quit(1, "Failed to calloc in getinfo");
  554. memcpy(&(sc_info->sc_devs[0]), &sc_dev, sizeof(sc_dev));
  555. // TODO: do we care about getting this info for the rest if > 0 x-link
  556. ok = true;
  557. goto ne;
  558. mata:
  559. freebreakdown(&count, &firstname, &fields);
  560. ok = false;
  561. ne:
  562. freetolines(&lines, &items);
  563. return ok;
  564. }
  565. static bool bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  566. {
  567. struct bflsc_info *sc_info = NULL;
  568. char buf[BFLSC_BUFSIZ+1];
  569. int i, err, amount;
  570. struct timeval init_start, init_now;
  571. int init_sleep, init_count;
  572. bool ident_first, sent;
  573. char *newname;
  574. uint16_t latency;
  575. struct cgpu_info *bflsc = usb_alloc_cgpu(&bflsc_drv, 1);
  576. sc_info = calloc(1, sizeof(*sc_info));
  577. if (unlikely(!sc_info))
  578. quit(1, "Failed to calloc sc_info in bflsc_detect_one");
  579. // TODO: fix ... everywhere ...
  580. bflsc->device_data = (FILE *)sc_info;
  581. if (!usb_init(bflsc, dev, found))
  582. goto shin;
  583. // Allow 2 complete attempts if the 1st time returns an unrecognised reply
  584. ident_first = true;
  585. retry:
  586. init_count = 0;
  587. init_sleep = REINIT_TIME_FIRST_MS;
  588. cgtime(&init_start);
  589. reinit:
  590. __bflsc_initialise(bflsc);
  591. err = send_recv_ss(bflsc, 0, &sent, &amount,
  592. BFLSC_IDENTIFY, BFLSC_IDENTIFY_LEN, C_REQUESTIDENTIFY,
  593. buf, sizeof(buf)-1, C_GETIDENTIFY, READ_NL);
  594. if (!sent) {
  595. applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
  596. bflsc->drv->dname, bflsc->device_path, amount, err);
  597. goto unshin;
  598. }
  599. if (err < 0 || amount < 1) {
  600. init_count++;
  601. cgtime(&init_now);
  602. if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
  603. if (init_count == 2) {
  604. applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying",
  605. bflsc->drv->dname, bflsc->device_path, amount, err);
  606. }
  607. cgsleep_ms(init_sleep);
  608. if ((init_sleep * 2) <= REINIT_TIME_MAX_MS)
  609. init_sleep *= 2;
  610. goto reinit;
  611. }
  612. if (init_count > 0)
  613. applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs",
  614. bflsc->drv->dname, bflsc->device_path, init_count, tdiff(&init_now, &init_start));
  615. if (err < 0) {
  616. applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
  617. bflsc->drv->dname, bflsc->device_path, amount, err);
  618. } else {
  619. applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
  620. bflsc->drv->dname, bflsc->device_path, amount);
  621. }
  622. goto unshin;
  623. }
  624. buf[amount] = '\0';
  625. if (unlikely(!strstr(buf, BFLSC_BFLSC))) {
  626. applog(LOG_DEBUG, "%s detect (%s) found an FPGA '%s' ignoring",
  627. bflsc->drv->dname, bflsc->device_path, buf);
  628. goto unshin;
  629. }
  630. if (unlikely(strstr(buf, BFLSC_IDENTITY))) {
  631. if (ident_first) {
  632. applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' trying again ...",
  633. bflsc->drv->dname, bflsc->device_path, buf);
  634. ident_first = false;
  635. goto retry;
  636. }
  637. applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' on 2nd attempt",
  638. bflsc->drv->dname, bflsc->device_path, buf);
  639. goto unshin;
  640. }
  641. int tries = 0;
  642. while (7734) {
  643. if (getinfo(bflsc, 0))
  644. break;
  645. // N.B. we will get displayed errors each time it fails
  646. if (++tries > 2)
  647. goto unshin;
  648. cgsleep_ms(40);
  649. }
  650. switch (sc_info->driver_version) {
  651. case BFLSC_DRV1:
  652. sc_info->que_size = BFLSC_QUE_SIZE_V1;
  653. sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V1;
  654. sc_info->que_watermark = BFLSC_QUE_WATERMARK_V1;
  655. sc_info->que_low = BFLSC_QUE_LOW_V1;
  656. sc_info->que_noncecount = QUE_NONCECOUNT_V1;
  657. sc_info->que_fld_min = QUE_FLD_MIN_V1;
  658. sc_info->que_fld_max = QUE_FLD_MAX_V1;
  659. // Only Jalapeno uses 1.0.0
  660. sc_info->flush_size = 1;
  661. break;
  662. case BFLSC_DRV2:
  663. case BFLSC_DRVUNDEF:
  664. default:
  665. sc_info->driver_version = BFLSC_DRV2;
  666. sc_info->que_size = BFLSC_QUE_SIZE_V2;
  667. sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V2;
  668. sc_info->que_watermark = BFLSC_QUE_WATERMARK_V2;
  669. sc_info->que_low = BFLSC_QUE_LOW_V2;
  670. sc_info->que_noncecount = QUE_NONCECOUNT_V2;
  671. sc_info->que_fld_min = QUE_FLD_MIN_V2;
  672. sc_info->que_fld_max = QUE_FLD_MAX_V2;
  673. // TODO: this can be reduced to total chip count
  674. sc_info->flush_size = 16 * sc_info->sc_count;
  675. break;
  676. }
  677. sc_info->scan_sleep_time = BAS_SCAN_TIME;
  678. sc_info->results_sleep_time = BFLSC_RES_TIME;
  679. sc_info->default_ms_work = BAS_WORK_TIME;
  680. latency = BAS_LATENCY;
  681. /* When getinfo() "FREQUENCY: [UNKNOWN]" is fixed -
  682. * use 'freq * engines' to estimate.
  683. * Otherwise for now: */
  684. newname = NULL;
  685. if (sc_info->sc_count > 1) {
  686. newname = BFLSC_MINIRIG;
  687. sc_info->scan_sleep_time = BAM_SCAN_TIME;
  688. sc_info->default_ms_work = BAM_WORK_TIME;
  689. bflsc->usbdev->ident = IDENT_BAM;
  690. latency = BAM_LATENCY;
  691. } else {
  692. if (sc_info->sc_devs[0].engines < 34) { // 16 * 2 + 2
  693. newname = BFLSC_JALAPENO;
  694. sc_info->scan_sleep_time = BAJ_SCAN_TIME;
  695. sc_info->default_ms_work = BAJ_WORK_TIME;
  696. bflsc->usbdev->ident = IDENT_BAJ;
  697. latency = BAJ_LATENCY;
  698. } else if (sc_info->sc_devs[0].engines < 130) { // 16 * 8 + 2
  699. newname = BFLSC_LITTLESINGLE;
  700. sc_info->scan_sleep_time = BAL_SCAN_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, sizeof(xlink), 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 = 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. 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. free_work(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 = 0, i, lines, count;
  1094. char xlink[17];
  1095. char *tmp, *tmp2;
  1096. bool res;
  1097. *nonces = 0;
  1098. xlinkstr(xlink, sizeof(xlink), dev, sc_info);
  1099. res = tolines(bflsc, dev, buf, &lines, &items, C_GETRESULTS);
  1100. if (!res || lines < 1) {
  1101. tmp = str_text(buf);
  1102. applog(LOG_ERR, "%s%i:%s empty result (%s) ignored",
  1103. bflsc->drv->name, bflsc->device_id, xlink, tmp);
  1104. free(tmp);
  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. goto arigatou;
  1113. }
  1114. breakdown(ONECOLON, items[1], &count, &firstname, &fields, &lf);
  1115. if (count < 1) {
  1116. tmp = str_text(buf);
  1117. tmp2 = str_text(items[1]);
  1118. applog(LOG_ERR, "%s%i:%s empty result count (%s) in (%s) will try anyway",
  1119. bflsc->drv->name, bflsc->device_id, xlink, tmp2, tmp);
  1120. free(tmp2);
  1121. free(tmp);
  1122. } else if (count != 1) {
  1123. tmp = str_text(buf);
  1124. tmp2 = str_text(items[1]);
  1125. applog(LOG_ERR, "%s%i:%s incorrect result count %d (%s) in (%s) will try anyway",
  1126. bflsc->drv->name, bflsc->device_id, xlink, count, tmp2, tmp);
  1127. free(tmp2);
  1128. free(tmp);
  1129. }
  1130. que = atoi(fields[0]);
  1131. if (que != (lines - QUE_RES_LINES_MIN)) {
  1132. i = que;
  1133. // 1+ In case the last line isn't 'OK' - try to process it
  1134. que = 1 + lines - QUE_RES_LINES_MIN;
  1135. tmp = str_text(buf);
  1136. tmp2 = str_text(items[0]);
  1137. applog(LOG_ERR, "%s%i:%s incorrect result count %d (%s) will try %d (%s)",
  1138. bflsc->drv->name, bflsc->device_id, xlink, i, tmp2, que, tmp);
  1139. free(tmp2);
  1140. free(tmp);
  1141. }
  1142. freebreakdown(&count, &firstname, &fields);
  1143. for (i = 0; i < que; i++) {
  1144. breakdown(NOCOLON, items[i + QUE_RES_LINES_MIN - 1], &count, &firstname, &fields, &lf);
  1145. process_nonces(bflsc, dev, &(xlink[0]), items[i], count, fields, nonces);
  1146. freebreakdown(&count, &firstname, &fields);
  1147. sc_info->not_first_work = true;
  1148. }
  1149. arigatou:
  1150. freetolines(&lines, &items);
  1151. return que;
  1152. }
  1153. #define TVF(tv) ((float)((tv)->tv_sec) + ((float)((tv)->tv_usec) / 1000000.0))
  1154. #define TVFMS(tv) (TVF(tv) * 1000.0)
  1155. // Thread to simply keep looking for results
  1156. static void *bflsc_get_results(void *userdata)
  1157. {
  1158. struct cgpu_info *bflsc = (struct cgpu_info *)userdata;
  1159. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1160. struct timeval elapsed, now;
  1161. float oldest, f;
  1162. char buf[BFLSC_BUFSIZ+1];
  1163. int err, amount;
  1164. int i, que, dev, nonces;
  1165. bool readok;
  1166. cgtime(&now);
  1167. for (i = 0; i < sc_info->sc_count; i++) {
  1168. copy_time(&(sc_info->sc_devs[i].last_check_result), &now);
  1169. copy_time(&(sc_info->sc_devs[i].last_dev_result), &now);
  1170. copy_time(&(sc_info->sc_devs[i].last_nonce_result), &now);
  1171. }
  1172. while (sc_info->shutdown == false) {
  1173. cgtimer_t ts_start;
  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. cgsleep_prepare_r(&ts_start);
  1191. if (dev == -1)
  1192. goto utsura;
  1193. cgtime(&(sc_info->sc_devs[dev].last_check_result));
  1194. readok = bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, false);
  1195. if (err < 0 || (!readok && amount != BFLSC_QRES_LEN) || (readok && amount < 1)) {
  1196. // TODO: do what else?
  1197. } else {
  1198. que = process_results(bflsc, dev, buf, &nonces);
  1199. sc_info->not_first_work = true; // in case it failed processing it
  1200. if (que > 0)
  1201. cgtime(&(sc_info->sc_devs[dev].last_dev_result));
  1202. if (nonces > 0)
  1203. cgtime(&(sc_info->sc_devs[dev].last_nonce_result));
  1204. // TODO: if not getting results ... reinit?
  1205. }
  1206. utsura:
  1207. cgsleep_ms_r(&ts_start, sc_info->results_sleep_time);
  1208. }
  1209. return NULL;
  1210. }
  1211. static bool bflsc_thread_prepare(struct thr_info *thr)
  1212. {
  1213. struct cgpu_info *bflsc = thr->cgpu;
  1214. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1215. if (thr_info_create(&(sc_info->results_thr), NULL, bflsc_get_results, (void *)bflsc)) {
  1216. applog(LOG_ERR, "%s%i: thread create failed", bflsc->drv->name, bflsc->device_id);
  1217. return false;
  1218. }
  1219. pthread_detach(sc_info->results_thr.pth);
  1220. return true;
  1221. }
  1222. static void bflsc_shutdown(struct thr_info *thr)
  1223. {
  1224. struct cgpu_info *bflsc = thr->cgpu;
  1225. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1226. bflsc_flush_work(bflsc);
  1227. sc_info->shutdown = true;
  1228. }
  1229. static void bflsc_thread_enable(struct thr_info *thr)
  1230. {
  1231. struct cgpu_info *bflsc = thr->cgpu;
  1232. if (bflsc->usbinfo.nodev)
  1233. return;
  1234. bflsc_initialise(bflsc);
  1235. }
  1236. static bool bflsc_send_work(struct cgpu_info *bflsc, int dev, 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. bool sent, ret = false;
  1242. struct work *work;
  1243. int err, amount;
  1244. int len, try;
  1245. int stage;
  1246. // Device is gone
  1247. if (bflsc->usbinfo.nodev)
  1248. return false;
  1249. // TODO: handle this everywhere
  1250. if (sc_info->sc_devs[dev].overheat == true)
  1251. return false;
  1252. // Initially code only deals with sending one work item
  1253. data.payloadSize = BFLSC_JOBSIZ;
  1254. data.endOfBlock = BFLSC_EOB;
  1255. len = sizeof(struct FullNonceRangeJob);
  1256. /* On faster devices we have a lot of lock contention so only
  1257. * mandatorily grab the lock and send work if the queue is empty since
  1258. * we have a submit queue. */
  1259. if (mandatory)
  1260. mutex_lock(&(bflsc->device_mutex));
  1261. else {
  1262. if (mutex_trylock(&bflsc->device_mutex))
  1263. return ret;
  1264. }
  1265. work = get_queued(bflsc);
  1266. if (unlikely(!work)) {
  1267. mutex_unlock(&bflsc->device_mutex);
  1268. return ret;
  1269. }
  1270. memcpy(data.midState, work->midstate, MIDSTATE_BYTES);
  1271. memcpy(data.blockData, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1272. try = 0;
  1273. re_send:
  1274. err = send_recv_ds(bflsc, dev, &stage, &sent, &amount,
  1275. BFLSC_QJOB, BFLSC_QJOB_LEN, C_REQUESTQUEJOB, C_REQUESTQUEJOBSTATUS,
  1276. (char *)&data, len, C_QUEJOB, C_QUEJOBSTATUS,
  1277. buf, sizeof(buf)-1);
  1278. mutex_unlock(&(bflsc->device_mutex));
  1279. switch (stage) {
  1280. case 1:
  1281. if (!sent) {
  1282. bflsc_applog(bflsc, dev, C_REQUESTQUEJOB, amount, err);
  1283. goto out;
  1284. } else {
  1285. // TODO: handle other errors ...
  1286. // Try twice
  1287. if (try++ < 1 && amount > 1 &&
  1288. strncasecmp(buf, BFLSC_TIMEOUT, BFLSC_TIMEOUT_LEN) == 0)
  1289. goto re_send;
  1290. bflsc_applog(bflsc, dev, C_REQUESTQUEJOBSTATUS, amount, err);
  1291. goto out;
  1292. }
  1293. break;
  1294. case 2:
  1295. if (!sent) {
  1296. bflsc_applog(bflsc, dev, C_QUEJOB, amount, err);
  1297. goto out;
  1298. } else {
  1299. if (!isokerr(err, buf, amount)) {
  1300. // TODO: check for QUEUE FULL and set work_queued to sc_info->que_size
  1301. // and report a code bug LOG_ERR - coz it should never happen
  1302. // TODO: handle other errors ...
  1303. // Try twice
  1304. if (try++ < 1 && amount > 1 &&
  1305. strncasecmp(buf, BFLSC_TIMEOUT, BFLSC_TIMEOUT_LEN) == 0)
  1306. goto re_send;
  1307. bflsc_applog(bflsc, dev, C_QUEJOBSTATUS, amount, err);
  1308. goto out;
  1309. }
  1310. }
  1311. break;
  1312. }
  1313. /*
  1314. err = write_to_dev(bflsc, dev, BFLSC_QJOB, BFLSC_QJOB_LEN, &amount, C_REQUESTQUEJOB);
  1315. if (err < 0 || amount != BFLSC_QJOB_LEN) {
  1316. mutex_unlock(&(bflsc->device_mutex));
  1317. bflsc_applog(bflsc, dev, C_REQUESTQUEJOB, amount, err);
  1318. goto out;
  1319. }
  1320. if (!getok(bflsc, C_REQUESTQUEJOBSTATUS, &err, &amount)) {
  1321. mutex_unlock(&(bflsc->device_mutex));
  1322. bflsc_applog(bflsc, dev, C_REQUESTQUEJOBSTATUS, amount, err);
  1323. goto out;
  1324. }
  1325. len = sizeof(struct FullNonceRangeJob);
  1326. err = write_to_dev(bflsc, dev, (char *)&data, len, &amount, C_QUEJOB);
  1327. if (err < 0 || amount != len) {
  1328. mutex_unlock(&(bflsc->device_mutex));
  1329. bflsc_applog(bflsc, dev, C_QUEJOB, amount, err);
  1330. goto out;
  1331. }
  1332. if (!getokerr(bflsc, C_QUEJOBSTATUS, &err, &amount, buf, sizeof(buf))) {
  1333. // TODO: check for QUEUE FULL and set work_queued to sc_info->que_size
  1334. // and report a code bug LOG_ERR - coz it should never happen
  1335. // Try twice
  1336. if (try++ < 1 && amount > 1 &&
  1337. strncasecmp(buf, BFLSC_TIMEOUT, BFLSC_TIMEOUT_LEN) == 0)
  1338. goto re_send;
  1339. mutex_unlock(&(bflsc->device_mutex));
  1340. bflsc_applog(bflsc, dev, C_QUEJOBSTATUS, amount, err);
  1341. goto out;
  1342. }
  1343. mutex_unlock(&(bflsc->device_mutex));
  1344. */
  1345. wr_lock(&(sc_info->stat_lock));
  1346. sc_info->sc_devs[dev].work_queued++;
  1347. wr_unlock(&(sc_info->stat_lock));
  1348. work->subid = dev;
  1349. ret = true;
  1350. out:
  1351. if (unlikely(!ret))
  1352. work_completed(bflsc, work);
  1353. return ret;
  1354. }
  1355. static bool bflsc_queue_full(struct cgpu_info *bflsc)
  1356. {
  1357. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1358. int i, dev, tried, que;
  1359. bool ret = false;
  1360. int tries = 0;
  1361. tried = -1;
  1362. // if something is wrong with a device try the next one available
  1363. // TODO: try them all? Add an unavailable flag to sc_devs[i] init to 0 here first
  1364. while (++tries < 3) {
  1365. bool mandatory = false;
  1366. // Device is gone - shouldn't normally get here
  1367. if (bflsc->usbinfo.nodev) {
  1368. ret = true;
  1369. break;
  1370. }
  1371. dev = -1;
  1372. rd_lock(&(sc_info->stat_lock));
  1373. // Anything waiting - gets the work first
  1374. for (i = 0; i < sc_info->sc_count; i++) {
  1375. // TODO: and ignore x-link dead - once I work out how to decide it is dead
  1376. if (i != tried && sc_info->sc_devs[i].work_queued == 0 &&
  1377. !sc_info->sc_devs[i].overheat) {
  1378. dev = i;
  1379. break;
  1380. }
  1381. }
  1382. if (dev == -1) {
  1383. que = sc_info->que_size * 10; // 10x is certainly above the MAX it could be
  1384. // The first device with the smallest amount queued
  1385. for (i = 0; i < sc_info->sc_count; i++) {
  1386. if (i != tried && sc_info->sc_devs[i].work_queued < que &&
  1387. !sc_info->sc_devs[i].overheat) {
  1388. dev = i;
  1389. que = sc_info->sc_devs[i].work_queued;
  1390. }
  1391. }
  1392. if (que > sc_info->que_full_enough)
  1393. dev = -1;
  1394. else if (que < sc_info->que_low)
  1395. mandatory = true;
  1396. }
  1397. rd_unlock(&(sc_info->stat_lock));
  1398. // nothing needs work yet
  1399. if (dev == -1) {
  1400. ret = true;
  1401. break;
  1402. }
  1403. if (bflsc_send_work(bflsc, dev, mandatory))
  1404. break;
  1405. else
  1406. tried = dev;
  1407. }
  1408. return ret;
  1409. }
  1410. static int64_t bflsc_scanwork(struct thr_info *thr)
  1411. {
  1412. struct cgpu_info *bflsc = thr->cgpu;
  1413. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1414. int64_t ret, unsent;
  1415. bool flushed, cleanup;
  1416. struct work *work, *tmp;
  1417. int dev, waited, i;
  1418. // Device is gone
  1419. if (bflsc->usbinfo.nodev)
  1420. return -1;
  1421. flushed = false;
  1422. // Single lock check if any are flagged as flushed
  1423. rd_lock(&(sc_info->stat_lock));
  1424. for (dev = 0; dev < sc_info->sc_count; dev++)
  1425. flushed |= sc_info->sc_devs[dev].flushed;
  1426. rd_unlock(&(sc_info->stat_lock));
  1427. // > 0 flagged as flushed
  1428. if (flushed) {
  1429. // TODO: something like this ......
  1430. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1431. cleanup = false;
  1432. // Is there any flushed work that can be removed?
  1433. rd_lock(&(sc_info->stat_lock));
  1434. if (sc_info->sc_devs[dev].flushed) {
  1435. if (sc_info->sc_devs[dev].result_id > (sc_info->sc_devs[dev].flush_id + sc_info->flush_size))
  1436. cleanup = true;
  1437. }
  1438. rd_unlock(&(sc_info->stat_lock));
  1439. // yes remove the flushed work that can be removed
  1440. if (cleanup) {
  1441. wr_lock(&bflsc->qlock);
  1442. HASH_ITER(hh, bflsc->queued_work, work, tmp) {
  1443. if (work->devflag && work->subid == dev) {
  1444. bflsc->queued_count--;
  1445. HASH_DEL(bflsc->queued_work, work);
  1446. discard_work(work);
  1447. }
  1448. }
  1449. wr_unlock(&bflsc->qlock);
  1450. wr_lock(&(sc_info->stat_lock));
  1451. sc_info->sc_devs[dev].flushed = false;
  1452. wr_unlock(&(sc_info->stat_lock));
  1453. }
  1454. }
  1455. }
  1456. waited = restart_wait(thr, sc_info->scan_sleep_time);
  1457. if (waited == ETIMEDOUT) {
  1458. unsigned int old_sleep_time, new_sleep_time = 0;
  1459. int min_queued = sc_info->que_size;
  1460. /* Only adjust the scan_sleep_time if we did not receive a
  1461. * restart message while waiting. Try to adjust sleep time
  1462. * so we drop to sc_info->que_watermark before getting more work.
  1463. */
  1464. rd_lock(&sc_info->stat_lock);
  1465. old_sleep_time = sc_info->scan_sleep_time;
  1466. for (i = 0; i < sc_info->sc_count; i++) {
  1467. if (sc_info->sc_devs[i].work_queued < min_queued)
  1468. min_queued = sc_info->sc_devs[i].work_queued;
  1469. }
  1470. rd_unlock(&sc_info->stat_lock);
  1471. new_sleep_time = old_sleep_time;
  1472. /* Increase slowly but decrease quickly */
  1473. if (min_queued > sc_info->que_full_enough && old_sleep_time < BFLSC_MAX_SLEEP)
  1474. new_sleep_time = old_sleep_time * 21 / 20;
  1475. else if (min_queued < sc_info->que_low)
  1476. new_sleep_time = old_sleep_time * 2 / 3;
  1477. /* Do not sleep more than BFLSC_MAX_SLEEP so we can always
  1478. * report in at least 2 results per 5s log interval. */
  1479. if (new_sleep_time != old_sleep_time) {
  1480. if (new_sleep_time > BFLSC_MAX_SLEEP)
  1481. new_sleep_time = BFLSC_MAX_SLEEP;
  1482. else if (new_sleep_time == 0)
  1483. new_sleep_time = 1;
  1484. applog(LOG_DEBUG, "%s%i: Changed scan sleep time to %d",
  1485. bflsc->drv->name, bflsc->device_id, new_sleep_time);
  1486. wr_lock(&sc_info->stat_lock);
  1487. sc_info->scan_sleep_time = new_sleep_time;
  1488. wr_unlock(&sc_info->stat_lock);
  1489. }
  1490. }
  1491. // Count up the work done since we last were here
  1492. ret = 0;
  1493. wr_lock(&(sc_info->stat_lock));
  1494. for (dev = 0; dev < sc_info->sc_count; dev++) {
  1495. unsent = sc_info->sc_devs[dev].hashes_unsent;
  1496. sc_info->sc_devs[dev].hashes_unsent = 0;
  1497. sc_info->sc_devs[dev].hashes_sent += unsent;
  1498. sc_info->hashes_sent += unsent;
  1499. ret += unsent;
  1500. }
  1501. wr_unlock(&(sc_info->stat_lock));
  1502. return ret;
  1503. }
  1504. #define BFLSC_OVER_TEMP 60
  1505. /* Set the fanspeed to auto for any valid value <= BFLSC_OVER_TEMP,
  1506. * or max for any value > BFLSC_OVER_TEMP or if we don't know the temperature. */
  1507. static void bflsc_set_fanspeed(struct cgpu_info *bflsc)
  1508. {
  1509. struct bflsc_info *sc_info = (struct bflsc_info *)bflsc->device_data;
  1510. char buf[BFLSC_BUFSIZ+1];
  1511. char data[16+1];
  1512. int amount;
  1513. bool sent;
  1514. if ((bflsc->temp <= BFLSC_OVER_TEMP && bflsc->temp > 0 && sc_info->fanauto) ||
  1515. ((bflsc->temp > BFLSC_OVER_TEMP || !bflsc->temp) && !sc_info->fanauto))
  1516. return;
  1517. if (bflsc->temp > BFLSC_OVER_TEMP || !bflsc->temp) {
  1518. strcpy(data, BFLSC_FAN4);
  1519. sc_info->fanauto = false;
  1520. } else {
  1521. strcpy(data, BFLSC_FANAUTO);
  1522. sc_info->fanauto = true;
  1523. }
  1524. applog(LOG_DEBUG, "%s%i: temp=%.0f over=%d set fan to %s",
  1525. bflsc->drv->name, bflsc->device_id, bflsc->temp,
  1526. BFLSC_OVER_TEMP, data);
  1527. mutex_lock(&bflsc->device_mutex);
  1528. send_recv_ss(bflsc, 0, &sent, &amount,
  1529. data, strlen(data), C_SETFAN,
  1530. buf, sizeof(buf)-1, C_FANREPLY, READ_NL);
  1531. mutex_unlock(&bflsc->device_mutex);
  1532. }
  1533. static bool bflsc_get_stats(struct cgpu_info *bflsc)
  1534. {
  1535. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1536. bool allok = true;
  1537. int i;
  1538. // Device is gone
  1539. if (bflsc->usbinfo.nodev)
  1540. return false;
  1541. for (i = 0; i < sc_info->sc_count; i++) {
  1542. if (!bflsc_get_temp(bflsc, i))
  1543. allok = false;
  1544. // Device is gone
  1545. if (bflsc->usbinfo.nodev)
  1546. return false;
  1547. if (i < (sc_info->sc_count - 1))
  1548. cgsleep_ms(BFLSC_TEMP_SLEEPMS);
  1549. }
  1550. bflsc_set_fanspeed(bflsc);
  1551. return allok;
  1552. }
  1553. static void bflsc_identify(struct cgpu_info *bflsc)
  1554. {
  1555. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1556. // TODO: handle x-link
  1557. sc_info->flash_led = true;
  1558. }
  1559. static bool bflsc_thread_init(struct thr_info *thr)
  1560. {
  1561. struct cgpu_info *bflsc = thr->cgpu;
  1562. if (bflsc->usbinfo.nodev)
  1563. return false;
  1564. bflsc_initialise(bflsc);
  1565. return true;
  1566. }
  1567. // there should be a new API function to return device info that isn't the standard stuff
  1568. // instead of bflsc_api_stats - since the stats should really just be internal code info
  1569. // and the new one should be UNusual device stats/extra details - like the stuff below
  1570. static struct api_data *bflsc_api_stats(struct cgpu_info *bflsc)
  1571. {
  1572. struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
  1573. struct api_data *root = NULL;
  1574. char buf[256];
  1575. int i;
  1576. //if no x-link ... etc
  1577. rd_lock(&(sc_info->stat_lock));
  1578. root = api_add_temp(root, "Temp1", &(sc_info->sc_devs[0].temp1), true);
  1579. root = api_add_temp(root, "Temp2", &(sc_info->sc_devs[0].temp2), true);
  1580. root = api_add_volts(root, "Vcc1", &(sc_info->sc_devs[0].vcc1), true);
  1581. root = api_add_volts(root, "Vcc2", &(sc_info->sc_devs[0].vcc2), true);
  1582. root = api_add_volts(root, "Vmain", &(sc_info->sc_devs[0].vmain), true);
  1583. root = api_add_temp(root, "Temp1 Max", &(sc_info->sc_devs[0].temp1_max), true);
  1584. root = api_add_temp(root, "Temp2 Max", &(sc_info->sc_devs[0].temp2_max), true);
  1585. root = api_add_time(root, "Temp1 Max Time", &(sc_info->sc_devs[0].temp1_max_time), true);
  1586. root = api_add_time(root, "Temp2 Max Time", &(sc_info->sc_devs[0].temp2_max_time), true);
  1587. root = api_add_int(root, "Work Queued", &(sc_info->sc_devs[0].work_queued), true);
  1588. root = api_add_int(root, "Work Complete", &(sc_info->sc_devs[0].work_complete), true);
  1589. root = api_add_bool(root, "Overheat", &(sc_info->sc_devs[0].overheat), true);
  1590. root = api_add_uint64(root, "Flush ID", &(sc_info->sc_devs[0].flush_id), true);
  1591. root = api_add_uint64(root, "Result ID", &(sc_info->sc_devs[0].result_id), true);
  1592. root = api_add_bool(root, "Flushed", &(sc_info->sc_devs[0].flushed), true);
  1593. root = api_add_uint(root, "Scan Sleep", &(sc_info->scan_sleep_time), true);
  1594. root = api_add_uint(root, "Results Sleep", &(sc_info->results_sleep_time), true);
  1595. root = api_add_uint(root, "Work ms", &(sc_info->default_ms_work), true);
  1596. buf[0] = '\0';
  1597. for (i = 0; i <= QUE_MAX_RESULTS + 1; i++)
  1598. tailsprintf(buf, sizeof(buf), "%s%"PRIu64, (i > 0) ? "/" : "", sc_info->result_size[i]);
  1599. root = api_add_string(root, "Result Size", buf, true);
  1600. rd_unlock(&(sc_info->stat_lock));
  1601. i = (int)(sc_info->driver_version);
  1602. root = api_add_int(root, "Driver", &i, true);
  1603. root = api_add_string(root, "Firmware", sc_info->sc_devs[0].firmware, false);
  1604. root = api_add_string(root, "Chips", sc_info->sc_devs[0].chips, false);
  1605. root = api_add_int(root, "Que Size", &(sc_info->que_size), false);
  1606. root = api_add_int(root, "Que Full", &(sc_info->que_full_enough), false);
  1607. root = api_add_int(root, "Que Watermark", &(sc_info->que_watermark), false);
  1608. root = api_add_int(root, "Que Low", &(sc_info->que_low), false);
  1609. root = api_add_escape(root, "GetInfo", sc_info->sc_devs[0].getinfo, false);
  1610. /*
  1611. else a whole lot of something like these ... etc
  1612. root = api_add_temp(root, "X-%d-Temp1", &(sc_info->temp1), false);
  1613. root = api_add_temp(root, "X-%d-Temp2", &(sc_info->temp2), false);
  1614. root = api_add_volts(root, "X-%d-Vcc1", &(sc_info->vcc1), false);
  1615. root = api_add_volts(root, "X-%d-Vcc2", &(sc_info->vcc2), false);
  1616. root = api_add_volts(root, "X-%d-Vmain", &(sc_info->vmain), false);
  1617. */
  1618. return root;
  1619. }
  1620. struct device_drv bflsc_drv = {
  1621. .drv_id = DRIVER_BFLSC,
  1622. .dname = "BitForceSC",
  1623. .name = BFLSC_SINGLE,
  1624. .drv_detect = bflsc_detect,
  1625. .get_api_stats = bflsc_api_stats,
  1626. .get_statline_before = get_bflsc_statline_before,
  1627. .get_stats = bflsc_get_stats,
  1628. .identify_device = bflsc_identify,
  1629. .thread_prepare = bflsc_thread_prepare,
  1630. .thread_init = bflsc_thread_init,
  1631. .hash_work = hash_queued_work,
  1632. .scanwork = bflsc_scanwork,
  1633. .queue_full = bflsc_queue_full,
  1634. .flush_work = bflsc_flush_work,
  1635. .thread_shutdown = bflsc_shutdown,
  1636. .thread_enable = bflsc_thread_enable
  1637. };