driver-bflsc.c 62 KB

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