driver-bflsc.c 52 KB

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