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