driver-bflsc.c 51 KB

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