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