driver-bflsc.c 52 KB

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