driver-bflsc.c 52 KB

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