driver-bflsc.c 53 KB

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