driver-bflsc.c 52 KB

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