driver-bitforce.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. * Copyright 2012 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 <ctype.h>
  12. #include <limits.h>
  13. #include <pthread.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <strings.h>
  18. #include <sys/time.h>
  19. #include <unistd.h>
  20. #include "compat.h"
  21. #include "deviceapi.h"
  22. #include "miner.h"
  23. #include "fpgautils.h"
  24. #include "util.h"
  25. #define BITFORCE_SLEEP_MS 500
  26. #define BITFORCE_TIMEOUT_S 7
  27. #define BITFORCE_TIMEOUT_MS (BITFORCE_TIMEOUT_S * 1000)
  28. #define BITFORCE_LONG_TIMEOUT_S 25
  29. #define BITFORCE_LONG_TIMEOUT_MS (BITFORCE_LONG_TIMEOUT_S * 1000)
  30. #define BITFORCE_CHECK_INTERVAL_MS 10
  31. #define WORK_CHECK_INTERVAL_MS 50
  32. #define MAX_START_DELAY_MS 100
  33. #define tv_to_ms(tval) ((unsigned long)(tval.tv_sec * 1000 + tval.tv_usec / 1000))
  34. #define TIME_AVG_CONSTANT 8
  35. #define BITFORCE_QRESULT_LINE_LEN 165
  36. #define BITFORCE_MAX_QUEUED_MAX 40
  37. #define BITFORCE_MIN_QUEUED_MAX 10
  38. #define BITFORCE_MAX_QRESULTS 16
  39. #define BITFORCE_GOAL_QRESULTS 5
  40. #define BITFORCE_MIN_QRESULT_WAIT BITFORCE_CHECK_INTERVAL_MS
  41. #define BITFORCE_MAX_QRESULT_WAIT 1000
  42. #define BITFORCE_MAX_BQUEUE_AT_ONCE 5
  43. enum bitforce_proto {
  44. BFP_WORK,
  45. BFP_RANGE,
  46. BFP_QUEUE,
  47. BFP_BQUEUE,
  48. BFP_PQUEUE,
  49. };
  50. static const char *protonames[] = {
  51. "full work",
  52. "nonce range",
  53. "work queue",
  54. "bulk queue",
  55. "parallel queue",
  56. };
  57. BFG_REGISTER_DRIVER(bitforce_drv)
  58. BFG_REGISTER_DRIVER(bitforce_queue_api)
  59. // Code must deal with a timeout
  60. #define BFopen(devpath) serial_open(devpath, 0, 250, true)
  61. static void BFgets(char *buf, size_t bufLen, int fd)
  62. {
  63. char *obuf = buf;
  64. do {
  65. buf[0] = '\0';
  66. --bufLen;
  67. } while (likely(bufLen && read(fd, buf, 1) == 1 && (buf++)[0] != '\n'));
  68. buf[0] = '\0';
  69. if (unlikely(opt_dev_protocol))
  70. applog(LOG_DEBUG, "DEVPROTO: GETS (fd=%d): %s", fd, obuf);
  71. }
  72. static ssize_t BFwrite(int fd, const void *buf, ssize_t bufLen)
  73. {
  74. if ((bufLen) != write(fd, buf, bufLen))
  75. return 0;
  76. else
  77. return bufLen;
  78. }
  79. static ssize_t bitforce_send(int fd, int procid, const void *buf, ssize_t bufLen)
  80. {
  81. if (!procid)
  82. return BFwrite(fd, buf, bufLen);
  83. if (bufLen > 255)
  84. return -1;
  85. size_t bufLeft = bufLen + 3;
  86. char realbuf[bufLeft], *bufp;
  87. ssize_t rv;
  88. memcpy(&realbuf[3], buf, bufLen);
  89. realbuf[0] = '@';
  90. realbuf[1] = bufLen;
  91. realbuf[2] = procid;
  92. bufp = realbuf;
  93. do
  94. {
  95. rv = BFwrite(fd, bufp, bufLeft);
  96. if (rv <= 0)
  97. return rv;
  98. bufLeft -= rv;
  99. }
  100. while (bufLeft > 0);
  101. return bufLen;
  102. }
  103. static
  104. void bitforce_cmd1b(int fd, int procid, void *buf, size_t bufsz, const char *cmd, size_t cmdsz)
  105. {
  106. if (unlikely(opt_dev_protocol))
  107. applog(LOG_DEBUG, "DEVPROTO: CMD1 (fd=%d xlink=%d): %s", fd, procid, cmd);
  108. bitforce_send(fd, procid, cmd, cmdsz);
  109. BFgets(buf, bufsz, fd);
  110. }
  111. #define bitforce_cmd1(fd, xlinkid, buf, bufsz, cmd) bitforce_cmd1b(fd, xlinkid, buf, bufsz, cmd, 3)
  112. static
  113. void bitforce_cmd2(int fd, int procid, void *buf, size_t bufsz, const char *cmd, void *data, size_t datasz)
  114. {
  115. bitforce_cmd1(fd, procid, buf, bufsz, cmd);
  116. if (strncasecmp(buf, "OK", 2))
  117. return;
  118. if (unlikely(opt_dev_protocol))
  119. {
  120. char hex[(datasz * 2) + 1];
  121. bin2hex(hex, data, datasz);
  122. applog(LOG_DEBUG, "DEVPROTO: CMD2 (fd=%d xlink=%d): %s", fd, procid, hex);
  123. }
  124. bitforce_send(fd, procid, data, datasz);
  125. BFgets(buf, bufsz, fd);
  126. }
  127. #define BFclose(fd) serial_close(fd)
  128. struct bitforce_init_data {
  129. bool sc;
  130. long devmask;
  131. int *parallels;
  132. };
  133. static
  134. int bitforce_chips_to_plan_for(int parallel, int chipcount) {
  135. if (parallel < 1)
  136. return parallel;
  137. if (chipcount > 15) return 32;
  138. if (chipcount > 7) return 16;
  139. if (chipcount > 3) return 8;
  140. if (chipcount > 1) return 4;
  141. if (chipcount ) return 2;
  142. return 1;
  143. }
  144. static bool bitforce_detect_one(const char *devpath)
  145. {
  146. int fdDev = serial_open(devpath, 0, 10, true);
  147. struct cgpu_info *bitforce;
  148. char pdevbuf[0x100];
  149. size_t pdevbuf_len;
  150. char *s;
  151. int procs = 1, parallel = -1;
  152. long maxchipno = 0;
  153. struct bitforce_init_data *initdata;
  154. char *manuf = NULL;
  155. applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
  156. if (unlikely(fdDev == -1)) {
  157. applog(LOG_DEBUG, "BFL: Failed to open %s", devpath);
  158. return false;
  159. }
  160. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  161. if (unlikely(!pdevbuf[0])) {
  162. applog(LOG_DEBUG, "BFL: Error reading/timeout (ZGX)");
  163. BFclose(fdDev);
  164. return 0;
  165. }
  166. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  167. applog(LOG_DEBUG, "BFL: Didn't recognise BitForce on %s", devpath);
  168. BFclose(fdDev);
  169. return false;
  170. }
  171. if (serial_claim_v(devpath, &bitforce_drv))
  172. {
  173. BFclose(fdDev);
  174. return false;
  175. }
  176. applog(LOG_DEBUG, "Found BitForce device on %s", devpath);
  177. initdata = malloc(sizeof(*initdata));
  178. *initdata = (struct bitforce_init_data){
  179. .sc = false,
  180. };
  181. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZCX");
  182. for (int i = 0; (!pdevbuf[0]) && i < 4; ++i)
  183. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  184. for ( ;
  185. strncasecmp(pdevbuf, "OK", 2);
  186. BFgets(pdevbuf, sizeof(pdevbuf), fdDev) )
  187. {
  188. pdevbuf_len = strlen(pdevbuf);
  189. if (unlikely(!pdevbuf_len))
  190. continue;
  191. pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
  192. applog(LOG_DEBUG, " %s", pdevbuf);
  193. if (!strncasecmp(pdevbuf, "PROCESSOR ", 10))
  194. maxchipno = max(maxchipno, atoi(&pdevbuf[10]));
  195. else
  196. if (!strncasecmp(pdevbuf, "DEVICES IN CHAIN:", 17))
  197. procs = atoi(&pdevbuf[17]);
  198. else
  199. if (!strncasecmp(pdevbuf, "CHAIN PRESENCE MASK:", 20))
  200. initdata->devmask = strtol(&pdevbuf[20], NULL, 16);
  201. else
  202. if (!strncasecmp(pdevbuf, "DEVICE:", 7) && strstr(pdevbuf, "SC"))
  203. initdata->sc = true;
  204. else
  205. if (!strncasecmp(pdevbuf, "CHIP PARALLELIZATION: YES @", 27))
  206. parallel = atoi(&pdevbuf[27]);
  207. else
  208. if (!strncasecmp(pdevbuf, "MANUFACTURER:", 13))
  209. {
  210. manuf = &pdevbuf[13];
  211. while (manuf[0] && isspace(manuf[0]))
  212. ++manuf;
  213. if (manuf[0])
  214. manuf = strdup(manuf);
  215. else
  216. manuf = NULL;
  217. }
  218. }
  219. parallel = bitforce_chips_to_plan_for(parallel, maxchipno);
  220. initdata->parallels = malloc(sizeof(initdata->parallels[0]) * procs);
  221. initdata->parallels[0] = parallel;
  222. parallel = abs(parallel);
  223. for (int proc = 1; proc < procs; ++proc)
  224. {
  225. applog(LOG_DEBUG, "Slave board %d:", proc);
  226. initdata->parallels[proc] = -1;
  227. maxchipno = 0;
  228. bitforce_cmd1(fdDev, proc, pdevbuf, sizeof(pdevbuf), "ZCX");
  229. for (int i = 0; (!pdevbuf[0]) && i < 4; ++i)
  230. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  231. for ( ;
  232. strncasecmp(pdevbuf, "OK", 2);
  233. BFgets(pdevbuf, sizeof(pdevbuf), fdDev) )
  234. {
  235. pdevbuf_len = strlen(pdevbuf);
  236. if (unlikely(!pdevbuf_len))
  237. continue;
  238. pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
  239. applog(LOG_DEBUG, " %s", pdevbuf);
  240. if (!strncasecmp(pdevbuf, "PROCESSOR ", 10))
  241. maxchipno = max(maxchipno, atoi(&pdevbuf[10]));
  242. else
  243. if (!strncasecmp(pdevbuf, "CHIP PARALLELIZATION: YES @", 27))
  244. initdata->parallels[proc] = atoi(&pdevbuf[27]);
  245. }
  246. initdata->parallels[proc] = bitforce_chips_to_plan_for(initdata->parallels[proc], maxchipno);
  247. parallel += abs(initdata->parallels[proc]);
  248. }
  249. BFclose(fdDev);
  250. if (unlikely((procs != 1 || parallel != 1) && !initdata->sc))
  251. {
  252. // Only bitforce_queue supports parallelization and XLINK, so force SC mode and hope for the best
  253. applog(LOG_WARNING, "SC features detected with non-SC device; this is not supported!");
  254. initdata->sc = true;
  255. }
  256. // We have a real BitForce!
  257. bitforce = calloc(1, sizeof(*bitforce));
  258. bitforce->drv = &bitforce_drv;
  259. if (initdata->sc)
  260. bitforce->drv = &bitforce_queue_api;
  261. bitforce->device_path = strdup(devpath);
  262. if (manuf)
  263. bitforce->dev_manufacturer = manuf;
  264. bitforce->deven = DEV_ENABLED;
  265. bitforce->procs = parallel;
  266. bitforce->threads = 1;
  267. if (initdata->sc)
  268. bitforce->cutofftemp = 85;
  269. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  270. s[0] = '\0';
  271. bitforce->name = strdup(pdevbuf + 7);
  272. }
  273. bitforce->device_data = initdata;
  274. mutex_init(&bitforce->device_mutex);
  275. return add_cgpu(bitforce);
  276. }
  277. static int bitforce_detect_auto(void)
  278. {
  279. return serial_autodetect(bitforce_detect_one, "BitFORCE", "SHA256");
  280. }
  281. static void bitforce_detect(void)
  282. {
  283. serial_detect_auto(&bitforce_drv, bitforce_detect_one, bitforce_detect_auto);
  284. }
  285. struct bitforce_data {
  286. int xlink_id;
  287. unsigned char next_work_ob[70]; // Data aligned for 32-bit access
  288. unsigned char *next_work_obs; // Start of data to send
  289. unsigned char next_work_obsz;
  290. const char *next_work_cmd;
  291. char noncebuf[14 + ((BITFORCE_MAX_QRESULTS+1) * BITFORCE_QRESULT_LINE_LEN)];
  292. int poll_func;
  293. enum bitforce_proto proto;
  294. bool sc;
  295. int queued;
  296. int queued_max;
  297. int parallel;
  298. bool parallel_protocol;
  299. bool missing_zwx;
  300. bool already_have_results;
  301. bool just_flushed;
  302. int ready_to_queue;
  303. bool want_to_send_queue;
  304. unsigned result_busy_polled;
  305. unsigned sleep_ms_default;
  306. struct timeval tv_hashmeter_start;
  307. float temp[2];
  308. long *volts;
  309. int volts_count;
  310. bool probed;
  311. bool supports_fanspeed;
  312. };
  313. struct bitforce_proc_data {
  314. struct cgpu_info *cgpu;
  315. bool handles_board; // The first processor handles the queue for the entire board
  316. };
  317. static void bitforce_clear_buffer(struct cgpu_info *);
  318. static
  319. void bitforce_comm_error(struct thr_info *thr)
  320. {
  321. struct cgpu_info *bitforce = thr->cgpu;
  322. struct bitforce_data *data = bitforce->device_data;
  323. int *p_fdDev = &bitforce->device->device_fd;
  324. data->noncebuf[0] = '\0';
  325. applog(LOG_ERR, "%"PRIpreprv": Comms error", bitforce->proc_repr);
  326. dev_error(bitforce, REASON_DEV_COMMS_ERROR);
  327. inc_hw_errors_only(thr);
  328. BFclose(*p_fdDev);
  329. int fd = *p_fdDev = BFopen(bitforce->device_path);
  330. if (fd == -1)
  331. {
  332. applog(LOG_ERR, "%s: Error reopening %s", bitforce->dev_repr, bitforce->device_path);
  333. return;
  334. }
  335. /* empty read buffer */
  336. bitforce_clear_buffer(bitforce);
  337. }
  338. static bool bitforce_thread_prepare(struct thr_info *thr)
  339. {
  340. struct cgpu_info *bitforce = thr->cgpu;
  341. int fdDev = BFopen(bitforce->device_path);
  342. if (unlikely(fdDev == -1)) {
  343. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, bitforce->device_path);
  344. return false;
  345. }
  346. bitforce->device_fd = fdDev;
  347. applog(LOG_INFO, "%s: Opened %s", bitforce->dev_repr, bitforce->device_path);
  348. return true;
  349. }
  350. static void __bitforce_clear_buffer(int fdDev)
  351. {
  352. char pdevbuf[0x100];
  353. int count = 0;
  354. do {
  355. pdevbuf[0] = '\0';
  356. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  357. } while (pdevbuf[0] && (++count < 10));
  358. }
  359. static void bitforce_clear_buffer(struct cgpu_info *bitforce)
  360. {
  361. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  362. int fdDev;
  363. mutex_lock(mutexp);
  364. fdDev = bitforce->device->device_fd;
  365. if (fdDev)
  366. {
  367. applog(LOG_DEBUG, "%"PRIpreprv": Clearing read buffer", bitforce->proc_repr);
  368. __bitforce_clear_buffer(fdDev);
  369. }
  370. mutex_unlock(mutexp);
  371. }
  372. void work_list_del(struct work **head, struct work *);
  373. void bitforce_reinit(struct cgpu_info *bitforce)
  374. {
  375. struct bitforce_data *data = bitforce->device_data;
  376. struct thr_info *thr = bitforce->thr[0];
  377. struct bitforce_proc_data *procdata = thr->cgpu_data;
  378. const char *devpath = bitforce->device_path;
  379. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  380. int *p_fdDev = &bitforce->device->device_fd;
  381. int fdDev, retries = 0;
  382. char pdevbuf[0x100];
  383. char *s;
  384. if (!procdata->handles_board)
  385. return;
  386. mutex_lock(mutexp);
  387. fdDev = *p_fdDev;
  388. applog(LOG_WARNING, "%"PRIpreprv": Re-initialising", bitforce->proc_repr);
  389. if (fdDev) {
  390. BFclose(fdDev);
  391. cgsleep_ms(5000);
  392. *p_fdDev = 0;
  393. }
  394. fdDev = BFopen(devpath);
  395. if (unlikely(fdDev == -1)) {
  396. mutex_unlock(mutexp);
  397. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, devpath);
  398. return;
  399. }
  400. __bitforce_clear_buffer(fdDev);
  401. do {
  402. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  403. if (unlikely(!pdevbuf[0])) {
  404. mutex_unlock(mutexp);
  405. BFclose(fdDev);
  406. applog(LOG_ERR, "%s: Error reading/timeout (ZGX)", bitforce->dev_repr);
  407. return;
  408. }
  409. if (retries++)
  410. cgsleep_ms(10);
  411. } while (strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  412. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  413. mutex_unlock(mutexp);
  414. BFclose(fdDev);
  415. applog(LOG_ERR, "%s: Didn't recognise BitForce on %s returned: %s", bitforce->dev_repr, devpath, pdevbuf);
  416. return;
  417. }
  418. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  419. s[0] = '\0';
  420. free((void*)bitforce->name);
  421. bitforce->name = strdup(pdevbuf + 7);
  422. }
  423. *p_fdDev = fdDev;
  424. bitforce->sleep_ms = data->sleep_ms_default;
  425. if (bitforce->drv == &bitforce_queue_api)
  426. {
  427. struct work *work, *tmp;
  428. timer_set_delay_from_now(&thr->tv_poll, 0);
  429. notifier_wake(thr->notifier);
  430. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZQX");
  431. DL_FOREACH_SAFE(thr->work_list, work, tmp)
  432. work_list_del(&thr->work_list, work);
  433. data->queued = 0;
  434. data->ready_to_queue = 0;
  435. data->already_have_results = false;
  436. data->just_flushed = true;
  437. thr->queue_full = false;
  438. }
  439. mutex_unlock(mutexp);
  440. }
  441. static void bitforce_flash_led(struct cgpu_info *bitforce)
  442. {
  443. struct bitforce_data *data = bitforce->device_data;
  444. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  445. int fdDev = bitforce->device->device_fd;
  446. if (!fdDev)
  447. return;
  448. /* Do not try to flash the led if we're polling for a result to
  449. * minimise the chance of interleaved results */
  450. if (bitforce->polling)
  451. return;
  452. /* It is not critical flashing the led so don't get stuck if we
  453. * can't grab the mutex here */
  454. if (mutex_trylock(mutexp))
  455. return;
  456. char pdevbuf[0x100];
  457. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZMX");
  458. /* Once we've tried - don't do it until told to again */
  459. bitforce->flash_led = false;
  460. /* However, this stops anything else getting a reply
  461. * So best to delay any other access to the BFL */
  462. cgsleep_ms(4000);
  463. mutex_unlock(mutexp);
  464. return; // nothing is returned by the BFL
  465. }
  466. static
  467. float my_strtof(const char *nptr, char **endptr)
  468. {
  469. float f = strtof(nptr, endptr);
  470. /* Cope with older software that breaks and reads nonsense
  471. * values */
  472. if (f > 100)
  473. f = strtod(nptr, endptr);
  474. return f;
  475. }
  476. static
  477. void set_float_if_gt_zero(float *var, float value)
  478. {
  479. if (value > 0)
  480. *var = value;
  481. }
  482. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  483. {
  484. struct bitforce_data *data = bitforce->device_data;
  485. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  486. int fdDev = bitforce->device->device_fd;
  487. char pdevbuf[0x40];
  488. char voltbuf[0x40];
  489. char *s;
  490. struct cgpu_info *chip_cgpu;
  491. if (!fdDev)
  492. return false;
  493. /* Do not try to get the temperature if we're polling for a result to
  494. * minimise the chance of interleaved results */
  495. if (bitforce->polling)
  496. return true;
  497. // Flash instead of Temp - doing both can be too slow
  498. if (bitforce->flash_led) {
  499. bitforce_flash_led(bitforce);
  500. return true;
  501. }
  502. /* It is not critical getting temperature so don't get stuck if we
  503. * can't grab the mutex here */
  504. if (mutex_trylock(mutexp))
  505. return false;
  506. if (data->sc)
  507. {
  508. if (unlikely(!data->probed))
  509. {
  510. bitforce_cmd1(fdDev, data->xlink_id, voltbuf, sizeof(voltbuf), "Z9X");
  511. if (strncasecmp(voltbuf, "ERR", 3))
  512. data->supports_fanspeed = true;
  513. data->probed = true;
  514. }
  515. bitforce_cmd1(fdDev, data->xlink_id, voltbuf, sizeof(voltbuf), "ZTX");
  516. }
  517. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZLX");
  518. mutex_unlock(mutexp);
  519. if (data->sc && likely(voltbuf[0]))
  520. {
  521. // Process voltage info
  522. // "NNNxxx,NNNxxx,NNNxxx"
  523. int n = 1;
  524. for (char *p = voltbuf; p[0]; ++p)
  525. if (p[0] == ',')
  526. ++n;
  527. long *out = malloc(sizeof(long) * n);
  528. if (!out)
  529. goto skipvolts;
  530. n = 0;
  531. char *saveptr, *v;
  532. for (v = strtok_r(voltbuf, ",", &saveptr); v; v = strtok_r(NULL, ",", &saveptr))
  533. out[n++] = strtol(v, NULL, 10);
  534. data->volts_count = 0;
  535. free(data->volts);
  536. data->volts = out;
  537. data->volts_count = n;
  538. }
  539. skipvolts:
  540. if (unlikely(!pdevbuf[0])) {
  541. struct thr_info *thr = bitforce->thr[0];
  542. applog(LOG_ERR, "%"PRIpreprv": Error: Get temp returned empty string/timed out", bitforce->proc_repr);
  543. inc_hw_errors_only(thr);
  544. return false;
  545. }
  546. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  547. float temp = my_strtof(s + 1, &s);
  548. set_float_if_gt_zero(&data->temp[0], temp);
  549. for ( ; s[0]; ++s)
  550. {
  551. if (!strncasecmp(s, "TEMP", 4) && (s = strchr(&s[4], ':')))
  552. {
  553. float temp2 = my_strtof(s + 1, &s);
  554. set_float_if_gt_zero(&data->temp[1], temp2);
  555. if (temp2 > temp)
  556. temp = temp2;
  557. }
  558. }
  559. if (temp > 0)
  560. {
  561. chip_cgpu = bitforce;
  562. for (int i = 0; i < data->parallel; ++i, (chip_cgpu = chip_cgpu->next_proc))
  563. chip_cgpu->temp = temp;
  564. }
  565. } else {
  566. struct thr_info *thr = bitforce->thr[0];
  567. /* Use the temperature monitor as a kind of watchdog for when
  568. * our responses are out of sync and flush the buffer to
  569. * hopefully recover */
  570. applog(LOG_WARNING, "%"PRIpreprv": Garbled response probably throttling, clearing buffer", bitforce->proc_repr);
  571. dev_error(bitforce, REASON_DEV_THROTTLE);
  572. /* Count throttling episodes as hardware errors */
  573. inc_hw_errors_only(thr);
  574. bitforce_clear_buffer(bitforce);
  575. return false;
  576. }
  577. return true;
  578. }
  579. static inline
  580. void dbg_block_data(struct cgpu_info *bitforce)
  581. {
  582. if (!opt_debug)
  583. return;
  584. struct bitforce_data *data = bitforce->device_data;
  585. char s[89];
  586. bin2hex(s, &data->next_work_ob[8], 44);
  587. applog(LOG_DEBUG, "%"PRIpreprv": block data: %s", bitforce->proc_repr, s);
  588. }
  589. static void bitforce_change_mode(struct cgpu_info *, enum bitforce_proto);
  590. static
  591. bool bitforce_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  592. {
  593. struct cgpu_info *bitforce = thr->cgpu;
  594. struct bitforce_data *data = bitforce->device_data;
  595. int fdDev = bitforce->device->device_fd;
  596. unsigned char *ob_ms = &data->next_work_ob[8];
  597. unsigned char *ob_dt = &ob_ms[32];
  598. // If polling job_start, cancel it
  599. if (data->poll_func == 1)
  600. {
  601. thr->tv_poll.tv_sec = -1;
  602. data->poll_func = 0;
  603. }
  604. memcpy(ob_ms, work->midstate, 32);
  605. memcpy(ob_dt, work->data + 64, 12);
  606. switch (data->proto)
  607. {
  608. case BFP_BQUEUE:
  609. quithere(1, "%"PRIpreprv": Impossible BFP_BQUEUE", bitforce->proc_repr);
  610. case BFP_PQUEUE:
  611. quithere(1, "%"PRIpreprv": Impossible BFP_PQUEUE", bitforce->proc_repr);
  612. case BFP_RANGE:
  613. {
  614. uint32_t *ob_nonce = (uint32_t*)&(ob_dt[32]);
  615. ob_nonce[0] = htobe32(work->blk.nonce);
  616. ob_nonce[1] = htobe32(work->blk.nonce + bitforce->nonces);
  617. // FIXME: if nonce range fails... we didn't increment enough
  618. work->blk.nonce += bitforce->nonces + 1;
  619. break;
  620. }
  621. case BFP_QUEUE:
  622. if (thr->work)
  623. {
  624. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  625. char pdevbuf[0x100];
  626. if (unlikely(!fdDev))
  627. return false;
  628. mutex_lock(mutexp);
  629. if (data->queued)
  630. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZQX");
  631. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, data->next_work_obs, data->next_work_obsz);
  632. mutex_unlock(mutexp);
  633. if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  634. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  635. bitforce_change_mode(bitforce, BFP_WORK);
  636. }
  637. else
  638. {
  639. dbg_block_data(bitforce);
  640. data->queued = 1;
  641. }
  642. }
  643. // fallthru...
  644. case BFP_WORK:
  645. work->blk.nonce = 0xffffffff;
  646. }
  647. return true;
  648. }
  649. static
  650. void bitforce_change_mode(struct cgpu_info *bitforce, enum bitforce_proto proto)
  651. {
  652. struct bitforce_data *data = bitforce->device_data;
  653. if (data->proto == proto)
  654. return;
  655. if (data->proto == BFP_RANGE)
  656. {
  657. bitforce->nonces = 0xffffffff;
  658. bitforce->sleep_ms *= 5;
  659. data->sleep_ms_default *= 5;
  660. switch (proto)
  661. {
  662. case BFP_WORK:
  663. data->next_work_cmd = "ZDX";
  664. break;
  665. case BFP_QUEUE:
  666. data->next_work_cmd = "ZNX";
  667. default:
  668. ;
  669. }
  670. if (data->sc)
  671. {
  672. // "S|---------- MidState ----------||-DataTail-|E"
  673. data->next_work_ob[7] = 45;
  674. data->next_work_ob[8+32+12] = '\xAA';
  675. data->next_work_obsz = 46;
  676. }
  677. else
  678. {
  679. // ">>>>>>>>|---------- MidState ----------||-DataTail-|>>>>>>>>"
  680. memset(&data->next_work_ob[8+32+12], '>', 8);
  681. data->next_work_obsz = 60;
  682. }
  683. }
  684. else
  685. if (proto == BFP_RANGE)
  686. {
  687. /* Split work up into 1/5th nonce ranges */
  688. bitforce->nonces = 0x33333332;
  689. bitforce->sleep_ms /= 5;
  690. data->sleep_ms_default /= 5;
  691. data->next_work_cmd = "ZPX";
  692. if (data->sc)
  693. {
  694. data->next_work_ob[7] = 53;
  695. data->next_work_obsz = 54;
  696. }
  697. else
  698. data->next_work_obsz = 68;
  699. }
  700. data->proto = proto;
  701. bitforce->kname = protonames[proto];
  702. }
  703. static
  704. void bitforce_job_start(struct thr_info *thr)
  705. {
  706. struct cgpu_info *bitforce = thr->cgpu;
  707. struct bitforce_data *data = bitforce->device_data;
  708. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  709. int fdDev = bitforce->device->device_fd;
  710. unsigned char *ob = data->next_work_obs;
  711. char pdevbuf[0x100];
  712. struct timeval tv_now;
  713. data->result_busy_polled = 0;
  714. if (data->queued)
  715. {
  716. uint32_t delay;
  717. // get_results collected more accurate job start time
  718. mt_job_transition(thr);
  719. job_start_complete(thr);
  720. data->queued = 0;
  721. delay = (uint32_t)bitforce->sleep_ms * 1000;
  722. if (unlikely(data->already_have_results))
  723. delay = 0;
  724. timer_set_delay(&thr->tv_morework, &bitforce->work_start_tv, delay);
  725. return;
  726. }
  727. if (!fdDev)
  728. goto commerr;
  729. re_send:
  730. mutex_lock(mutexp);
  731. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, ob, data->next_work_obsz);
  732. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  733. mutex_unlock(mutexp);
  734. cgtime(&tv_now);
  735. timer_set_delay(&thr->tv_poll, &tv_now, WORK_CHECK_INTERVAL_MS * 1000);
  736. data->poll_func = 1;
  737. return;
  738. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  739. mutex_unlock(mutexp);
  740. switch (data->proto)
  741. {
  742. case BFP_RANGE:
  743. applog(LOG_WARNING, "%"PRIpreprv": Does not support nonce range, disabling", bitforce->proc_repr);
  744. bitforce_change_mode(bitforce, BFP_WORK);
  745. goto re_send;
  746. case BFP_QUEUE:
  747. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  748. bitforce_change_mode(bitforce, BFP_WORK);
  749. goto re_send;
  750. default:
  751. ;
  752. }
  753. applog(LOG_ERR, "%"PRIpreprv": Error: Send work reports: %s", bitforce->proc_repr, pdevbuf);
  754. goto commerr;
  755. }
  756. mt_job_transition(thr);
  757. mutex_unlock(mutexp);
  758. dbg_block_data(bitforce);
  759. cgtime(&tv_now);
  760. bitforce->work_start_tv = tv_now;
  761. timer_set_delay(&thr->tv_morework, &tv_now, bitforce->sleep_ms * 1000);
  762. job_start_complete(thr);
  763. return;
  764. commerr:
  765. bitforce_comm_error(thr);
  766. job_start_abort(thr, true);
  767. }
  768. static char _discardedbuf[0x10];
  769. static
  770. int bitforce_zox(struct thr_info *thr, const char *cmd)
  771. {
  772. struct cgpu_info *bitforce = thr->cgpu;
  773. struct bitforce_data *data = bitforce->device_data;
  774. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  775. int fd = bitforce->device->device_fd;
  776. char *pdevbuf = &data->noncebuf[0];
  777. int count;
  778. mutex_lock(mutexp);
  779. bitforce_cmd1(fd, data->xlink_id, pdevbuf, sizeof(data->noncebuf), cmd);
  780. if (!strncasecmp(pdevbuf, "INPROCESS:", 10))
  781. BFgets(pdevbuf, sizeof(data->noncebuf), fd);
  782. if (!strncasecmp(pdevbuf, "COUNT:", 6))
  783. {
  784. count = atoi(&pdevbuf[6]);
  785. size_t cls = strlen(pdevbuf);
  786. char *pmorebuf = &pdevbuf[cls];
  787. size_t szleft = sizeof(data->noncebuf) - cls, sz;
  788. if (count && data->queued)
  789. cgtime(&bitforce->work_start_tv);
  790. while (true)
  791. {
  792. BFgets(pmorebuf, szleft, fd);
  793. if (!strncasecmp(pmorebuf, "OK", 2))
  794. {
  795. pmorebuf[0] = '\0'; // process expects only results
  796. break;
  797. }
  798. sz = strlen(pmorebuf);
  799. if (!sz)
  800. {
  801. applog(LOG_ERR, "%"PRIpreprv": Timeout during %s", bitforce->proc_repr, cmd);
  802. break;
  803. }
  804. szleft -= sz;
  805. pmorebuf += sz;
  806. if (unlikely(szleft < BITFORCE_QRESULT_LINE_LEN))
  807. {
  808. // Out of buffer space somehow :(
  809. applog(LOG_ERR, "%"PRIpreprv": Ran out of buffer space for results, discarding extra data", bitforce->proc_repr);
  810. pmorebuf = _discardedbuf;
  811. szleft = sizeof(_discardedbuf);
  812. }
  813. }
  814. }
  815. else
  816. count = -1;
  817. mutex_unlock(mutexp);
  818. return count;
  819. }
  820. static inline char *next_line(char *);
  821. static
  822. void bitforce_job_get_results(struct thr_info *thr, struct work *work)
  823. {
  824. struct cgpu_info *bitforce = thr->cgpu;
  825. struct bitforce_data *data = bitforce->device_data;
  826. int fdDev = bitforce->device->device_fd;
  827. unsigned int delay_time_ms;
  828. struct timeval elapsed;
  829. struct timeval now;
  830. char *pdevbuf = &data->noncebuf[0];
  831. bool stale;
  832. int count;
  833. cgtime(&now);
  834. timersub(&now, &bitforce->work_start_tv, &elapsed);
  835. bitforce->wait_ms = tv_to_ms(elapsed);
  836. bitforce->polling = true;
  837. if (!fdDev)
  838. goto commerr;
  839. stale = stale_work(work, true);
  840. if (unlikely(bitforce->wait_ms < bitforce->sleep_ms))
  841. {
  842. // We're likely here because of a work restart
  843. // Since Bitforce cannot stop a work without losing results, only do it if the current job is finding stale shares
  844. // BFP_QUEUE does not support stopping work at all
  845. if (data->proto == BFP_QUEUE || !stale)
  846. {
  847. delay_time_ms = bitforce->sleep_ms - bitforce->wait_ms;
  848. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  849. data->poll_func = 2;
  850. return;
  851. }
  852. }
  853. while (1) {
  854. if (data->already_have_results)
  855. {
  856. data->already_have_results = false;
  857. strcpy(pdevbuf, "COUNT:0");
  858. count = 1;
  859. break;
  860. }
  861. const char *cmd = (data->proto == BFP_QUEUE) ? "ZOX" : "ZFX";
  862. count = bitforce_zox(thr, cmd);
  863. cgtime(&now);
  864. timersub(&now, &bitforce->work_start_tv, &elapsed);
  865. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  866. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  867. tv_to_ms(elapsed), (unsigned long)BITFORCE_LONG_TIMEOUT_MS);
  868. goto out;
  869. }
  870. if (count > 0)
  871. {
  872. // Check that queue results match the current work
  873. // Also, if there are results from the next work, short-circuit this wait
  874. unsigned char midstate[32], datatail[12];
  875. char *p;
  876. int i;
  877. p = pdevbuf;
  878. for (i = 0; i < count; ++i)
  879. {
  880. p = next_line(p);
  881. hex2bin(midstate, p, 32);
  882. hex2bin(datatail, &p[65], 12);
  883. if (!(memcmp(work->midstate, midstate, 32) || memcmp(&work->data[64], datatail, 12)))
  884. break;
  885. }
  886. if (i == count)
  887. {
  888. // Didn't find the one we're waiting on
  889. // Must be extra stuff in the queue results
  890. char xmid[65];
  891. char xdt[25];
  892. bin2hex(xmid, work->midstate, 32);
  893. bin2hex(xdt, &work->data[64], 12);
  894. applog(LOG_WARNING, "%"PRIpreprv": Found extra garbage in queue results: %s",
  895. bitforce->proc_repr, pdevbuf);
  896. applog(LOG_WARNING, "%"PRIpreprv": ...while waiting on: %s,%s",
  897. bitforce->proc_repr, xmid, xdt);
  898. count = 0;
  899. }
  900. else
  901. if (i == count - 1)
  902. // Last one found is what we're looking for
  903. {}
  904. else
  905. // We finished the next job too!
  906. data->already_have_results = true;
  907. }
  908. if (!count)
  909. goto noqr;
  910. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  911. break;
  912. data->result_busy_polled = bitforce->wait_ms;
  913. if (stale && data->proto != BFP_QUEUE)
  914. {
  915. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  916. bitforce->proc_repr);
  917. goto out;
  918. }
  919. noqr:
  920. data->result_busy_polled = bitforce->wait_ms;
  921. /* if BFL is throttling, no point checking so quickly */
  922. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  923. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  924. data->poll_func = 2;
  925. return;
  926. }
  927. if (count < 0 && pdevbuf[0] == 'N')
  928. count = strncasecmp(pdevbuf, "NONCE-FOUND", 11) ? 1 : 0;
  929. // At this point, 'count' is:
  930. // negative, in case of some kind of error
  931. // zero, if NO-NONCE (FPGA either completed with no results, or rebooted)
  932. // positive, if at least one job completed successfully
  933. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  934. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  935. tv_to_ms(elapsed), (unsigned long)BITFORCE_TIMEOUT_MS);
  936. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  937. inc_hw_errors_only(thr);
  938. /* If the device truly throttled, it didn't process the job and there
  939. * are no results. But check first, just in case we're wrong about it
  940. * throttling.
  941. */
  942. if (count > 0)
  943. goto out;
  944. } else if (count >= 0) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  945. /* Simple timing adjustment. Allow a few polls to cope with
  946. * OS timer delays being variably reliable. wait_ms will
  947. * always equal sleep_ms when we've waited greater than or
  948. * equal to the result return time.*/
  949. delay_time_ms = bitforce->sleep_ms;
  950. if (!data->result_busy_polled)
  951. {
  952. // No busy polls before results received
  953. if (bitforce->wait_ms > delay_time_ms + (WORK_CHECK_INTERVAL_MS * 8))
  954. // ... due to poll being rather late; ignore it as an anomaly
  955. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, later than scheduled %ums (ignoring)",
  956. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms);
  957. else
  958. if (bitforce->sleep_ms > data->sleep_ms_default + (BITFORCE_CHECK_INTERVAL_MS * 0x20))
  959. {
  960. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on delayed schedule %ums; Wait time changed to: %ums (default sch)",
  961. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, data->sleep_ms_default);
  962. bitforce->sleep_ms = data->sleep_ms_default;
  963. }
  964. else
  965. {
  966. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on default schedule %ums; Wait time changed to: %ums (check interval)",
  967. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, BITFORCE_CHECK_INTERVAL_MS);
  968. bitforce->sleep_ms = BITFORCE_CHECK_INTERVAL_MS;
  969. }
  970. }
  971. else
  972. {
  973. if (data->result_busy_polled - bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  974. {
  975. bitforce->sleep_ms = data->result_busy_polled - (WORK_CHECK_INTERVAL_MS / 2);
  976. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time changed to: %ums",
  977. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms, bitforce->sleep_ms);
  978. }
  979. else
  980. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time unchanged",
  981. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms);
  982. }
  983. /* Work out the average time taken. Float for calculation, uint for display */
  984. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  985. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  986. }
  987. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  988. if (count < 0 && strncasecmp(pdevbuf, "I", 1)) {
  989. inc_hw_errors_only(thr);
  990. applog(LOG_WARNING, "%"PRIpreprv": Error: Get result reports: %s", bitforce->proc_repr, pdevbuf);
  991. bitforce_clear_buffer(bitforce);
  992. }
  993. out:
  994. bitforce->polling = false;
  995. job_results_fetched(thr);
  996. return;
  997. commerr:
  998. bitforce_comm_error(thr);
  999. goto out;
  1000. }
  1001. static
  1002. void bitforce_process_result_nonces(struct thr_info *thr, struct work *work, char *pnoncebuf)
  1003. {
  1004. struct cgpu_info *bitforce = thr->cgpu;
  1005. struct bitforce_data *data = bitforce->device_data;
  1006. uint32_t nonce;
  1007. while (1) {
  1008. hex2bin((void*)&nonce, pnoncebuf, 4);
  1009. nonce = be32toh(nonce);
  1010. if (unlikely(data->proto == BFP_RANGE && (nonce >= work->blk.nonce ||
  1011. /* FIXME: blk.nonce is probably moved on quite a bit now! */
  1012. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  1013. applog(LOG_WARNING, "%"PRIpreprv": Disabling broken nonce range support", bitforce->proc_repr);
  1014. bitforce_change_mode(bitforce, BFP_WORK);
  1015. }
  1016. submit_nonce(thr, work, nonce);
  1017. if (strncmp(&pnoncebuf[8], ",", 1))
  1018. break;
  1019. pnoncebuf += 9;
  1020. }
  1021. }
  1022. static
  1023. bool bitforce_process_qresult_line_i(struct thr_info *thr, char *midstate, char *datatail, char *buf, struct work *work)
  1024. {
  1025. if (!work)
  1026. return false;
  1027. if (memcmp(work->midstate, midstate, 32))
  1028. return false;
  1029. if (memcmp(&work->data[64], datatail, 12))
  1030. return false;
  1031. char *end;
  1032. if (strtol(&buf[90], &end, 10))
  1033. bitforce_process_result_nonces(thr, work, &end[1]);
  1034. return true;
  1035. }
  1036. static
  1037. void bitforce_process_qresult_line(struct thr_info *thr, char *buf, struct work *work)
  1038. {
  1039. struct cgpu_info *bitforce = thr->cgpu;
  1040. char midstate[32], datatail[12];
  1041. hex2bin((void*)midstate, buf, 32);
  1042. hex2bin((void*)datatail, &buf[65], 12);
  1043. if (!( bitforce_process_qresult_line_i(thr, midstate, datatail, buf, work)
  1044. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->work)
  1045. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->prev_work)
  1046. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->next_work) ))
  1047. {
  1048. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queued results", bitforce->proc_repr);
  1049. inc_hw_errors_only(thr);
  1050. }
  1051. }
  1052. static inline
  1053. char *next_line(char *in)
  1054. {
  1055. while (in[0] && (in++)[0] != '\n')
  1056. {}
  1057. return in;
  1058. }
  1059. static
  1060. int64_t bitforce_job_process_results(struct thr_info *thr, struct work *work, __maybe_unused bool stopping)
  1061. {
  1062. struct cgpu_info *bitforce = thr->cgpu;
  1063. struct bitforce_data *data = bitforce->device_data;
  1064. char *pnoncebuf = &data->noncebuf[0];
  1065. int count;
  1066. if (!strncasecmp(pnoncebuf, "NO-", 3))
  1067. return bitforce->nonces; /* No valid nonce found */
  1068. if (!strncasecmp(pnoncebuf, "NONCE-FOUND", 11))
  1069. {
  1070. bitforce_process_result_nonces(thr, work, &pnoncebuf[12]);
  1071. count = 1;
  1072. }
  1073. else
  1074. if (!strncasecmp(pnoncebuf, "COUNT:", 6))
  1075. {
  1076. count = 0;
  1077. pnoncebuf = next_line(pnoncebuf);
  1078. while (pnoncebuf[0])
  1079. {
  1080. bitforce_process_qresult_line(thr, pnoncebuf, work);
  1081. ++count;
  1082. pnoncebuf = next_line(pnoncebuf);
  1083. }
  1084. }
  1085. else
  1086. return 0;
  1087. // FIXME: This might have changed in the meantime (new job start, or broken)
  1088. return bitforce->nonces * count;
  1089. }
  1090. static void bitforce_shutdown(struct thr_info *thr)
  1091. {
  1092. struct cgpu_info *bitforce = thr->cgpu;
  1093. int *p_fdDev = &bitforce->device->device_fd;
  1094. BFclose(*p_fdDev);
  1095. *p_fdDev = 0;
  1096. }
  1097. static void biforce_thread_enable(struct thr_info *thr)
  1098. {
  1099. struct cgpu_info *bitforce = thr->cgpu;
  1100. bitforce_reinit(bitforce);
  1101. }
  1102. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  1103. {
  1104. struct bitforce_proc_data *procdata = bitforce->thr[0]->cgpu_data;
  1105. if (!procdata->handles_board)
  1106. return true;
  1107. return bitforce_get_temp(bitforce);
  1108. }
  1109. static bool bitforce_identify(struct cgpu_info *bitforce)
  1110. {
  1111. bitforce->flash_led = true;
  1112. return true;
  1113. }
  1114. static bool bitforce_thread_init(struct thr_info *thr)
  1115. {
  1116. struct cgpu_info *bitforce = thr->cgpu;
  1117. unsigned int wait;
  1118. struct bitforce_data *data;
  1119. struct bitforce_proc_data *procdata;
  1120. struct bitforce_init_data *initdata = bitforce->device_data;
  1121. bool sc = initdata->sc;
  1122. int xlink_id = 0, boardno = 0;
  1123. struct bitforce_proc_data *first_on_this_board;
  1124. char buf[100];
  1125. int fd = bitforce->device_fd;
  1126. for ( ; bitforce; bitforce = bitforce->next_proc)
  1127. {
  1128. thr = bitforce->thr[0];
  1129. if (unlikely(xlink_id > 30))
  1130. {
  1131. applog(LOG_ERR, "%"PRIpreprv": Failed to find XLINK address", bitforce->proc_repr);
  1132. dev_error(bitforce, REASON_THREAD_FAIL_INIT);
  1133. bitforce->reinit_backoff = 1e10;
  1134. continue;
  1135. }
  1136. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  1137. bitforce->device_data = data = malloc(sizeof(*data));
  1138. *data = (struct bitforce_data){
  1139. .xlink_id = xlink_id,
  1140. .next_work_ob = ">>>>>>>>|---------- MidState ----------||-DataTail-||Nonces|>>>>>>>>",
  1141. .proto = BFP_RANGE,
  1142. .sc = sc,
  1143. .sleep_ms_default = BITFORCE_SLEEP_MS,
  1144. .parallel = abs(initdata->parallels[boardno]),
  1145. .parallel_protocol = (initdata->parallels[boardno] != -1),
  1146. };
  1147. thr->cgpu_data = procdata = malloc(sizeof(*procdata));
  1148. *procdata = (struct bitforce_proc_data){
  1149. .handles_board = true,
  1150. .cgpu = bitforce,
  1151. };
  1152. if (sc)
  1153. {
  1154. // ".......S|---------- MidState ----------||-DataTail-||Nonces|E"
  1155. data->next_work_ob[8+32+12+8] = '\xAA';
  1156. data->next_work_obs = &data->next_work_ob[7];
  1157. if (bitforce->drv == &bitforce_queue_api)
  1158. {
  1159. bitforce_change_mode(bitforce, data->parallel_protocol ? BFP_PQUEUE : BFP_BQUEUE);
  1160. bitforce->sleep_ms = data->sleep_ms_default = 100;
  1161. timer_set_delay_from_now(&thr->tv_poll, 0);
  1162. data->queued_max = data->parallel * 2;
  1163. if (data->queued_max < BITFORCE_MIN_QUEUED_MAX)
  1164. data->queued_max = BITFORCE_MIN_QUEUED_MAX;
  1165. if (data->queued_max > BITFORCE_MAX_QUEUED_MAX)
  1166. data->queued_max = BITFORCE_MAX_QUEUED_MAX;
  1167. }
  1168. else
  1169. bitforce_change_mode(bitforce, BFP_QUEUE);
  1170. // Clear job queue to start fresh; ignore response
  1171. bitforce_cmd1(fd, data->xlink_id, buf, sizeof(buf), "ZQX");
  1172. }
  1173. else
  1174. {
  1175. data->next_work_obs = &data->next_work_ob[0];
  1176. // Unconditionally change away from cold-initialized BFP_RANGE, to allow for setting up other variables
  1177. bitforce_change_mode(bitforce, BFP_WORK);
  1178. /* Initially enable support for nonce range and disable it later if it
  1179. * fails */
  1180. if (opt_bfl_noncerange)
  1181. bitforce_change_mode(bitforce, BFP_RANGE);
  1182. }
  1183. bitforce->status = LIFE_INIT2;
  1184. first_on_this_board = procdata;
  1185. for (int proc = 1; proc < data->parallel; ++proc)
  1186. {
  1187. bitforce = bitforce->next_proc;
  1188. assert(bitforce);
  1189. thr = bitforce->thr[0];
  1190. thr->queue_full = true;
  1191. thr->cgpu_data = procdata = malloc(sizeof(*procdata));
  1192. *procdata = *first_on_this_board;
  1193. procdata->handles_board = false;
  1194. procdata->cgpu = bitforce;
  1195. bitforce->device_data = data;
  1196. bitforce->status = LIFE_INIT2;
  1197. bitforce->kname = first_on_this_board->cgpu->kname;
  1198. }
  1199. applog(LOG_DEBUG, "%s: Board %d: %"PRIpreprv"-%"PRIpreprv, bitforce->dev_repr, boardno, first_on_this_board->cgpu->proc_repr, bitforce->proc_repr);
  1200. ++boardno;
  1201. while (xlink_id < 31 && !(initdata->devmask & (1 << ++xlink_id)))
  1202. {}
  1203. }
  1204. // NOTE: This doesn't restore the first processor, but it does get us the last one; this is sufficient for the delay debug and start of the next loop below
  1205. bitforce = thr->cgpu;
  1206. free(initdata->parallels);
  1207. free(initdata);
  1208. /* Pause each new thread at least 100ms between initialising
  1209. * so the devices aren't making calls all at the same time. */
  1210. wait = thr->id * MAX_START_DELAY_MS;
  1211. applog(LOG_DEBUG, "%s: Delaying start by %dms", bitforce->dev_repr, wait / 1000);
  1212. cgsleep_ms(wait);
  1213. if (sc)
  1214. {
  1215. // Clear results queue last, to start fresh; ignore response
  1216. int last_xlink_id = -1;
  1217. for (bitforce = bitforce->device; bitforce; bitforce = bitforce->next_proc)
  1218. {
  1219. struct bitforce_data * const data = bitforce->device_data;
  1220. if (data->xlink_id == last_xlink_id)
  1221. continue;
  1222. last_xlink_id = data->xlink_id;
  1223. thr = bitforce->thr[0];
  1224. bitforce_zox(thr, "ZOX");
  1225. }
  1226. }
  1227. return true;
  1228. }
  1229. #ifdef HAVE_CURSES
  1230. static
  1231. void bitforce_tui_wlogprint_choices(struct cgpu_info *cgpu)
  1232. {
  1233. struct bitforce_data *data = cgpu->device_data;
  1234. if (data->supports_fanspeed)
  1235. wlogprint("[F]an control ");
  1236. }
  1237. static
  1238. const char *bitforce_tui_handle_choice(struct cgpu_info *cgpu, int input)
  1239. {
  1240. struct bitforce_data *data = cgpu->device_data;
  1241. pthread_mutex_t *mutexp;
  1242. int fd;
  1243. static char replybuf[0x100];
  1244. if (!data->supports_fanspeed)
  1245. return NULL;
  1246. switch (input)
  1247. {
  1248. case 'f': case 'F':
  1249. {
  1250. int fanspeed;
  1251. char *intvar;
  1252. intvar = curses_input("Set fan speed (range 0-5 for low to fast or 9 for auto)");
  1253. if (!intvar)
  1254. return "Invalid fan speed\n";
  1255. fanspeed = atoi(intvar);
  1256. free(intvar);
  1257. if ((fanspeed < 0 || fanspeed > 5) && fanspeed != 9)
  1258. return "Invalid fan speed\n";
  1259. char cmd[4] = "Z0X";
  1260. cmd[1] += fanspeed;
  1261. mutexp = &cgpu->device->device_mutex;
  1262. mutex_lock(mutexp);
  1263. fd = cgpu->device->device_fd;
  1264. bitforce_cmd1(fd, data->xlink_id, replybuf, sizeof(replybuf), cmd);
  1265. mutex_unlock(mutexp);
  1266. return replybuf;
  1267. }
  1268. }
  1269. return NULL;
  1270. }
  1271. static
  1272. void bitforce_wlogprint_status(struct cgpu_info *cgpu)
  1273. {
  1274. struct bitforce_data *data = cgpu->device_data;
  1275. if (data->temp[0] > 0 && data->temp[1] > 0)
  1276. wlogprint("Temperatures: %4.1fC %4.1fC\n", data->temp[0], data->temp[1]);
  1277. if (data->volts_count)
  1278. {
  1279. // -> "NNN.xxx / NNN.xxx / NNN.xxx"
  1280. size_t sz = (data->volts_count * 10) + 1;
  1281. char buf[sz];
  1282. char *s = buf;
  1283. int rv = 0;
  1284. for (int i = 0; i < data->volts_count; ++i)
  1285. {
  1286. long v = data->volts[i];
  1287. _SNP("%ld.%03d / ", v / 1000, (int)(v % 1000));
  1288. }
  1289. if (rv >= 3 && s[-2] == '/')
  1290. s[-3] = '\0';
  1291. wlogprint("Voltages: %s\n", buf);
  1292. }
  1293. }
  1294. #endif
  1295. static struct api_data *bitforce_drv_stats(struct cgpu_info *cgpu)
  1296. {
  1297. struct bitforce_data *data = cgpu->device_data;
  1298. struct api_data *root = NULL;
  1299. // Warning, access to these is not locked - but we don't really
  1300. // care since hashing performance is way more important than
  1301. // locking access to displaying API debug 'stats'
  1302. // If locking becomes an issue for any of them, use copy_data=true also
  1303. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  1304. if (data->proto != BFP_BQUEUE && data->proto != BFP_PQUEUE)
  1305. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  1306. if (data->temp[0] > 0 && data->temp[1] > 0)
  1307. {
  1308. root = api_add_temp(root, "Temperature0", &(data->temp[0]), false);
  1309. root = api_add_temp(root, "Temperature1", &(data->temp[1]), false);
  1310. }
  1311. for (int i = 0; i < data->volts_count; ++i)
  1312. {
  1313. float voltage = data->volts[i];
  1314. char key[] = "VoltageNN";
  1315. snprintf(&key[7], 3, "%d", i);
  1316. voltage /= 1e3;
  1317. root = api_add_volts(root, key, &voltage, true);
  1318. }
  1319. return root;
  1320. }
  1321. void bitforce_poll(struct thr_info *thr)
  1322. {
  1323. struct cgpu_info *bitforce = thr->cgpu;
  1324. struct bitforce_data *data = bitforce->device_data;
  1325. int poll = data->poll_func;
  1326. thr->tv_poll.tv_sec = -1;
  1327. data->poll_func = 0;
  1328. switch (poll)
  1329. {
  1330. case 1:
  1331. bitforce_job_start(thr);
  1332. break;
  1333. case 2:
  1334. bitforce_job_get_results(thr, thr->work);
  1335. break;
  1336. default:
  1337. applog(LOG_ERR, "%"PRIpreprv": Unexpected poll from device API!", thr->cgpu->proc_repr);
  1338. }
  1339. }
  1340. static
  1341. char *bitforce_set_device(struct cgpu_info *proc, char *option, char *setting, char *replybuf)
  1342. {
  1343. struct bitforce_data *data = proc->device_data;
  1344. pthread_mutex_t *mutexp = &proc->device->device_mutex;
  1345. int fd;
  1346. if (!strcasecmp(option, "help"))
  1347. {
  1348. sprintf(replybuf, "fanmode: range 0-5 (low to fast) or 9 (auto)");
  1349. return replybuf;
  1350. }
  1351. if (!strcasecmp(option, "fanmode"))
  1352. {
  1353. if (!data->supports_fanspeed)
  1354. {
  1355. sprintf(replybuf, "fanmode not supported");
  1356. return replybuf;
  1357. }
  1358. if (!setting || !*setting)
  1359. {
  1360. sprintf(replybuf, "missing fanmode setting");
  1361. return replybuf;
  1362. }
  1363. if (setting[1] || ((setting[0] < '0' || setting[0] > '5') && setting[0] != '9'))
  1364. {
  1365. sprintf(replybuf, "invalid fanmode setting");
  1366. return replybuf;
  1367. }
  1368. char cmd[4] = "Z5X";
  1369. cmd[1] = setting[0];
  1370. mutex_lock(mutexp);
  1371. fd = proc->device->device_fd;
  1372. bitforce_cmd1(fd, data->xlink_id, replybuf, 256, cmd);
  1373. mutex_unlock(mutexp);
  1374. return replybuf;
  1375. }
  1376. if (!strcasecmp(option, "_cmd1"))
  1377. {
  1378. mutex_lock(mutexp);
  1379. fd = proc->device->device_fd;
  1380. bitforce_cmd1b(fd, data->xlink_id, replybuf, 8000, setting, strlen(setting));
  1381. mutex_unlock(mutexp);
  1382. return replybuf;
  1383. }
  1384. sprintf(replybuf, "Unknown option: %s", option);
  1385. return replybuf;
  1386. }
  1387. struct device_drv bitforce_drv = {
  1388. .dname = "bitforce",
  1389. .name = "BFL",
  1390. .drv_detect = bitforce_detect,
  1391. #ifdef HAVE_CURSES
  1392. .proc_wlogprint_status = bitforce_wlogprint_status,
  1393. .proc_tui_wlogprint_choices = bitforce_tui_wlogprint_choices,
  1394. .proc_tui_handle_choice = bitforce_tui_handle_choice,
  1395. #endif
  1396. .get_api_stats = bitforce_drv_stats,
  1397. .minerloop = minerloop_async,
  1398. .reinit_device = bitforce_reinit,
  1399. .get_stats = bitforce_get_stats,
  1400. .set_device = bitforce_set_device,
  1401. .identify_device = bitforce_identify,
  1402. .thread_prepare = bitforce_thread_prepare,
  1403. .thread_init = bitforce_thread_init,
  1404. .job_prepare = bitforce_job_prepare,
  1405. .job_start = bitforce_job_start,
  1406. .job_get_results = bitforce_job_get_results,
  1407. .poll = bitforce_poll,
  1408. .job_process_results = bitforce_job_process_results,
  1409. .thread_shutdown = bitforce_shutdown,
  1410. .thread_enable = biforce_thread_enable
  1411. };
  1412. static inline
  1413. void bitforce_set_queue_full(struct thr_info *thr)
  1414. {
  1415. struct cgpu_info *bitforce = thr->cgpu;
  1416. struct bitforce_data *data = bitforce->device_data;
  1417. thr->queue_full = (data->queued + data->ready_to_queue >= data->queued_max) || (data->ready_to_queue >= BITFORCE_MAX_BQUEUE_AT_ONCE);
  1418. }
  1419. static
  1420. bool bitforce_send_queue(struct thr_info *thr)
  1421. {
  1422. struct cgpu_info *bitforce = thr->cgpu;
  1423. struct bitforce_data *data = bitforce->device_data;
  1424. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  1425. int fd = bitforce->device->device_fd;
  1426. struct work *work;
  1427. if (unlikely(!(fd && data->ready_to_queue)))
  1428. return false;
  1429. char buf[0x100];
  1430. int queued_ok;
  1431. size_t qjs_sz = (32 + 12 + 2);
  1432. size_t qjp_sz = 4 + (qjs_sz * data->ready_to_queue);
  1433. uint8_t qjp[qjp_sz], *qjs;
  1434. qjp[0] = qjp_sz - 1;
  1435. qjp[1] = 0xc1;
  1436. qjp[2] = data->ready_to_queue;
  1437. qjp[qjp_sz - 1] = 0xfe;
  1438. qjs = &qjp[qjp_sz - 1];
  1439. work = thr->work_list->prev;
  1440. for (int i = data->ready_to_queue; i > 0; --i, work = work->prev)
  1441. {
  1442. *(--qjs) = 0xaa;
  1443. memcpy(qjs -= 12, work->data + 64, 12);
  1444. memcpy(qjs -= 32, work->midstate, 32);
  1445. *(--qjs) = 45;
  1446. }
  1447. retry:
  1448. mutex_lock(mutexp);
  1449. if (data->missing_zwx)
  1450. bitforce_cmd2(fd, data->xlink_id, buf, sizeof(buf), "ZNX", &qjp[3], qjp_sz - 4);
  1451. else
  1452. bitforce_cmd2(fd, data->xlink_id, buf, sizeof(buf), "ZWX", qjp, qjp_sz);
  1453. mutex_unlock(mutexp);
  1454. if (!strncasecmp(buf, "ERR:QUEUE", 9))
  1455. {
  1456. // Queue full :(
  1457. applog(LOG_DEBUG, "%"PRIpreprv": Device queue full while attempting to append %d jobs (queued<=%d)",
  1458. bitforce->proc_repr,
  1459. data->ready_to_queue, data->queued);
  1460. thr->queue_full = true;
  1461. return false;
  1462. }
  1463. if (strncasecmp(buf, "OK:QUEUED", 9))
  1464. {
  1465. if ((!strncasecmp(buf, "ERROR: UNKNOWN", 11)) && !data->missing_zwx)
  1466. {
  1467. applog(LOG_DEBUG, "%"PRIpreprv": Missing ZWX command, trying ZNX",
  1468. bitforce->proc_repr);
  1469. data->missing_zwx = true;
  1470. goto retry;
  1471. }
  1472. applog(LOG_DEBUG, "%"PRIpreprv": Unexpected error attempting to append %d jobs (queued<=%d): %s",
  1473. bitforce->proc_repr,
  1474. data->ready_to_queue, data->queued, buf);
  1475. return false;
  1476. }
  1477. if (!data->queued)
  1478. cgtime(&data->tv_hashmeter_start);
  1479. if (data->missing_zwx)
  1480. queued_ok = 1;
  1481. else
  1482. queued_ok = atoi(&buf[9]);
  1483. data->queued += queued_ok;
  1484. applog(LOG_DEBUG, "%"PRIpreprv": Successfully queued %d/%d jobs on device (queued<=%d)",
  1485. bitforce->proc_repr,
  1486. queued_ok, data->ready_to_queue, data->queued);
  1487. data->ready_to_queue -= queued_ok;
  1488. if (!data->missing_zwx)
  1489. thr->queue_full = data->ready_to_queue;
  1490. data->just_flushed = false;
  1491. data->want_to_send_queue = false;
  1492. return true;
  1493. }
  1494. void work_list_del(struct work **head, struct work *work)
  1495. {
  1496. DL_DELETE(*head, work);
  1497. free_work(work);
  1498. }
  1499. static
  1500. bool bitforce_queue_do_results(struct thr_info *thr)
  1501. {
  1502. struct cgpu_info *bitforce = thr->cgpu;
  1503. struct bitforce_data *data = bitforce->device_data;
  1504. int fd = bitforce->device->device_fd;
  1505. int count;
  1506. int fcount;
  1507. char *noncebuf, *buf, *end;
  1508. unsigned char midstate[32], datatail[12];
  1509. struct work *work, *tmpwork, *thiswork;
  1510. struct timeval tv_now, tv_elapsed;
  1511. long chipno = 0; // Initialized value is used for non-parallelized boards
  1512. struct cgpu_info *chip_cgpu;
  1513. struct thr_info *chip_thr;
  1514. int counts[data->parallel];
  1515. if (unlikely(!fd))
  1516. return false;
  1517. again:
  1518. noncebuf = &data->noncebuf[0];
  1519. count = bitforce_zox(thr, "ZOX");
  1520. if (unlikely(count < 0))
  1521. {
  1522. applog(LOG_ERR, "%"PRIpreprv": Received unexpected queue result response: %s", bitforce->proc_repr, noncebuf);
  1523. inc_hw_errors_only(thr);
  1524. return false;
  1525. }
  1526. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results on poll (max=%d)", bitforce->proc_repr, count, (int)BITFORCE_MAX_QRESULTS);
  1527. if (!count)
  1528. return true;
  1529. fcount = 0;
  1530. for (int i = 0; i < data->parallel; ++i)
  1531. counts[i] = 0;
  1532. noncebuf = next_line(noncebuf);
  1533. while ((buf = noncebuf)[0])
  1534. {
  1535. if ( (noncebuf = next_line(buf)) )
  1536. noncebuf[-1] = '\0';
  1537. if (strlen(buf) <= 90)
  1538. {
  1539. applog(LOG_ERR, "%"PRIpreprv": Gibberish within queue results: %s", bitforce->proc_repr, buf);
  1540. continue;
  1541. }
  1542. hex2bin(midstate, buf, 32);
  1543. hex2bin(datatail, &buf[65], 12);
  1544. thiswork = NULL;
  1545. DL_FOREACH(thr->work_list, work)
  1546. {
  1547. if (unlikely(memcmp(work->midstate, midstate, 32)))
  1548. continue;
  1549. if (unlikely(memcmp(&work->data[64], datatail, 12)))
  1550. continue;
  1551. thiswork = work;
  1552. break;
  1553. }
  1554. end = &buf[89];
  1555. chip_cgpu = bitforce;
  1556. if (data->parallel_protocol)
  1557. {
  1558. chipno = strtol(&end[1], &end, 16);
  1559. if (chipno >= data->parallel)
  1560. {
  1561. applog(LOG_ERR, "%"PRIpreprv": Chip number out of range for queue result: %s", chip_cgpu->proc_repr, buf);
  1562. chipno = 0;
  1563. }
  1564. for (int i = 0; i < chipno; ++i)
  1565. chip_cgpu = chip_cgpu->next_proc;
  1566. }
  1567. chip_thr = chip_cgpu->thr[0];
  1568. applog(LOG_DEBUG, "%"PRIpreprv": Queue result: %s", chip_cgpu->proc_repr, buf);
  1569. if (unlikely(!thiswork))
  1570. {
  1571. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queue results: %s", chip_cgpu->proc_repr, buf);
  1572. inc_hw_errors_only(chip_thr);
  1573. goto next_qline;
  1574. }
  1575. if (unlikely(!end[0]))
  1576. {
  1577. applog(LOG_ERR, "%"PRIpreprv": Missing nonce count in queue results: %s", chip_cgpu->proc_repr, buf);
  1578. goto finishresult;
  1579. }
  1580. if (strtol(&end[1], &end, 10))
  1581. {
  1582. if (unlikely(!end[0]))
  1583. {
  1584. applog(LOG_ERR, "%"PRIpreprv": Missing nonces in queue results: %s", chip_cgpu->proc_repr, buf);
  1585. goto finishresult;
  1586. }
  1587. bitforce_process_result_nonces(chip_thr, work, &end[1]);
  1588. }
  1589. ++fcount;
  1590. ++counts[chipno];
  1591. finishresult:
  1592. if (data->parallel == 1)
  1593. {
  1594. // Queue results are in order, so anything queued prior this is lost
  1595. // Delete all queued work up to, and including, this one
  1596. DL_FOREACH_SAFE(thr->work_list, work, tmpwork)
  1597. {
  1598. work_list_del(&thr->work_list, work);
  1599. --data->queued;
  1600. if (work == thiswork)
  1601. break;
  1602. }
  1603. }
  1604. else
  1605. {
  1606. // Parallel processors means the results might not be in order
  1607. // This could leak if jobs get lost, hence the sanity checks using "ZqX"
  1608. work_list_del(&thr->work_list, thiswork);
  1609. --data->queued;
  1610. }
  1611. next_qline: (void)0;
  1612. }
  1613. bitforce_set_queue_full(thr);
  1614. if (count >= BITFORCE_MAX_QRESULTS)
  1615. goto again;
  1616. if (data->parallel == 1 && (
  1617. (fcount < BITFORCE_GOAL_QRESULTS && bitforce->sleep_ms < BITFORCE_MAX_QRESULT_WAIT && data->queued > 1)
  1618. || (fcount > BITFORCE_GOAL_QRESULTS && bitforce->sleep_ms > BITFORCE_MIN_QRESULT_WAIT) ))
  1619. {
  1620. unsigned int old_sleep_ms = bitforce->sleep_ms;
  1621. bitforce->sleep_ms = (uint32_t)bitforce->sleep_ms * BITFORCE_GOAL_QRESULTS / (fcount ?: 1);
  1622. if (bitforce->sleep_ms > BITFORCE_MAX_QRESULT_WAIT)
  1623. bitforce->sleep_ms = BITFORCE_MAX_QRESULT_WAIT;
  1624. if (bitforce->sleep_ms < BITFORCE_MIN_QRESULT_WAIT)
  1625. bitforce->sleep_ms = BITFORCE_MIN_QRESULT_WAIT;
  1626. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time changed to: %ums (queued<=%d)",
  1627. bitforce->proc_repr, fcount, old_sleep_ms, bitforce->sleep_ms, data->queued);
  1628. }
  1629. else
  1630. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time unchanged (queued<=%d)",
  1631. bitforce->proc_repr, fcount, bitforce->sleep_ms, data->queued);
  1632. cgtime(&tv_now);
  1633. timersub(&tv_now, &data->tv_hashmeter_start, &tv_elapsed);
  1634. chip_cgpu = bitforce;
  1635. for (int i = 0; i < data->parallel; ++i, (chip_cgpu = chip_cgpu->next_proc))
  1636. {
  1637. chip_thr = chip_cgpu->thr[0];
  1638. hashes_done(chip_thr, (uint64_t)bitforce->nonces * counts[i], &tv_elapsed, NULL);
  1639. }
  1640. data->tv_hashmeter_start = tv_now;
  1641. return true;
  1642. }
  1643. static
  1644. bool bitforce_queue_append(struct thr_info *thr, struct work *work)
  1645. {
  1646. struct cgpu_info *bitforce = thr->cgpu;
  1647. struct bitforce_data *data = bitforce->device_data;
  1648. bool rv, ndq;
  1649. bitforce_set_queue_full(thr);
  1650. rv = !thr->queue_full;
  1651. if (rv)
  1652. {
  1653. DL_APPEND(thr->work_list, work);
  1654. ++data->ready_to_queue;
  1655. applog(LOG_DEBUG, "%"PRIpreprv": Appending to driver queue (max=%u, ready=%d, queued<=%d)",
  1656. bitforce->proc_repr,
  1657. (unsigned)data->queued_max, data->ready_to_queue, data->queued);
  1658. bitforce_set_queue_full(thr);
  1659. }
  1660. else
  1661. if (!data->ready_to_queue)
  1662. return rv;
  1663. ndq = !data->queued;
  1664. if ((ndq) // Device is idle
  1665. || (data->ready_to_queue >= BITFORCE_MAX_BQUEUE_AT_ONCE) // ...or 5 items ready to go
  1666. || (thr->queue_full) // ...or done filling queue
  1667. || (data->just_flushed) // ...or queue was just flushed (only remaining job is partly done already)
  1668. || (data->missing_zwx) // ...or device can only queue one at a time
  1669. )
  1670. {
  1671. if (!bitforce_send_queue(thr))
  1672. {
  1673. // Problem sending queue, retry again in a few seconds
  1674. applog(LOG_ERR, "%"PRIpreprv": Failed to send queue", bitforce->proc_repr);
  1675. inc_hw_errors_only(thr);
  1676. data->want_to_send_queue = true;
  1677. }
  1678. }
  1679. return rv;
  1680. }
  1681. struct _jobinfo {
  1682. uint8_t key[32+12];
  1683. int instances;
  1684. UT_hash_handle hh;
  1685. };
  1686. static
  1687. void bitforce_queue_flush(struct thr_info *thr)
  1688. {
  1689. struct bitforce_proc_data *procdata = thr->cgpu_data;
  1690. if (!procdata->handles_board)
  1691. return;
  1692. struct cgpu_info *bitforce = thr->cgpu;
  1693. struct bitforce_data *data = bitforce->device_data;
  1694. char *buf = &data->noncebuf[0], *buf2 = NULL;
  1695. const char *cmd = "ZqX";
  1696. unsigned flushed;
  1697. struct _jobinfo *processing = NULL, *item, *this;
  1698. if (data->parallel == 1)
  1699. // Pre-parallelization neither needs nor supports "ZqX"
  1700. cmd = "ZQX";
  1701. // TODO: Call "ZQX" most of the time: don't need to do sanity checks so often
  1702. bitforce_zox(thr, cmd);
  1703. if (!strncasecmp(buf, "OK:FLUSHED", 10))
  1704. flushed = atoi(&buf[10]);
  1705. else
  1706. if ((!strncasecmp(buf, "COUNT:", 6)) && (buf2 = strstr(buf, "FLUSHED:")) )
  1707. {
  1708. flushed = atoi(&buf2[8]);
  1709. buf2 = next_line(buf2);
  1710. }
  1711. else
  1712. if (!strncasecmp(buf, "OK", 2))
  1713. {
  1714. applog(LOG_DEBUG, "%"PRIpreprv": Didn't report flush count", bitforce->proc_repr);
  1715. thr->queue_full = false;
  1716. flushed = 0;
  1717. }
  1718. else
  1719. {
  1720. applog(LOG_DEBUG, "%"PRIpreprv": Failed to flush device queue: %s", bitforce->proc_repr, buf);
  1721. flushed = 0;
  1722. }
  1723. data->queued -= flushed;
  1724. applog(LOG_DEBUG, "%"PRIpreprv": Flushed %u jobs from device and %d from driver (queued<=%d)",
  1725. bitforce->proc_repr, flushed, data->ready_to_queue, data->queued);
  1726. flushed += data->ready_to_queue;
  1727. data->ready_to_queue = 0;
  1728. while (flushed--)
  1729. work_list_del(&thr->work_list, thr->work_list->prev);
  1730. bitforce_set_queue_full(thr);
  1731. data->just_flushed = true;
  1732. data->want_to_send_queue = false;
  1733. // "ZqX" returns jobs in progress, allowing us to sanity check
  1734. // NOTE: Must process buffer into hash table BEFORE calling bitforce_queue_do_results, which clobbers it
  1735. // NOTE: Must do actual sanity check AFTER calling bitforce_queue_do_results, to ensure we don't delete completed jobs
  1736. if (buf2)
  1737. {
  1738. // First, turn buf2 into a hash
  1739. for ( ; buf2[0]; buf2 = next_line(buf2))
  1740. {
  1741. this = malloc(sizeof(*this));
  1742. hex2bin(&this->key[ 0], &buf2[ 0], 32);
  1743. hex2bin(&this->key[32], &buf2[65], 12);
  1744. HASH_FIND(hh, processing, &this->key[0], sizeof(this->key), item);
  1745. if (likely(!item))
  1746. {
  1747. this->instances = 1;
  1748. HASH_ADD(hh, processing, key, sizeof(this->key), this);
  1749. }
  1750. else
  1751. {
  1752. // This should really only happen in testing/benchmarking...
  1753. ++item->instances;
  1754. free(this);
  1755. }
  1756. }
  1757. }
  1758. bitforce_queue_do_results(thr);
  1759. if (buf2)
  1760. {
  1761. struct work *work, *tmp;
  1762. uint8_t key[32+12];
  1763. // Now iterate over the work_list and delete anything not in the hash
  1764. DL_FOREACH_SAFE(thr->work_list, work, tmp)
  1765. {
  1766. memcpy(&key[ 0], work->midstate, 32);
  1767. memcpy(&key[32], &work->data[64], 12);
  1768. HASH_FIND(hh, processing, &key[0], sizeof(key), item);
  1769. if (unlikely(!item))
  1770. {
  1771. char hex[89];
  1772. bin2hex(hex, key, 32+12);
  1773. applog(LOG_WARNING, "%"PRIpreprv": Sanity check: Device is missing queued job! %s", bitforce->proc_repr, hex);
  1774. work_list_del(&thr->work_list, work);
  1775. continue;
  1776. }
  1777. if (likely(!--item->instances))
  1778. {
  1779. HASH_DEL(processing, item);
  1780. free(item);
  1781. }
  1782. }
  1783. if (unlikely( (flushed = HASH_COUNT(processing)) ))
  1784. {
  1785. //applog(LOG_WARNING, "%"PRIpreprv": Sanity check: Device is working on %d unknown jobs!", bitforce->proc_repr, flushed);
  1786. // FIXME: Probably these were jobs finished after ZqX, included in the result check we just did
  1787. // NOTE: We need to do that result check first to avoid deleting work_list items for things just solved
  1788. HASH_ITER(hh, processing, item, this)
  1789. {
  1790. HASH_DEL(processing, item);
  1791. free(item);
  1792. }
  1793. }
  1794. }
  1795. }
  1796. static
  1797. void bitforce_queue_poll(struct thr_info *thr)
  1798. {
  1799. struct cgpu_info *bitforce = thr->cgpu;
  1800. struct bitforce_data *data = bitforce->device_data;
  1801. unsigned long sleep_us;
  1802. if (data->queued)
  1803. bitforce_queue_do_results(thr);
  1804. sleep_us = (unsigned long)bitforce->sleep_ms * 1000;
  1805. if (data->want_to_send_queue)
  1806. if (!bitforce_send_queue(thr))
  1807. if (!data->queued)
  1808. {
  1809. applog(LOG_ERR, "%"PRIpreprv": Failed to send queue, and queue empty; retrying after 1 second", bitforce->proc_repr);
  1810. inc_hw_errors_only(thr);
  1811. sleep_us = 1000000;
  1812. }
  1813. timer_set_delay_from_now(&thr->tv_poll, sleep_us);
  1814. }
  1815. static void bitforce_queue_thread_deven(struct thr_info *thr)
  1816. {
  1817. struct cgpu_info *bitforce = thr->cgpu, *thisbf;
  1818. struct bitforce_data *data = bitforce->device_data;
  1819. struct thr_info *thisthr;
  1820. for (thisbf = bitforce->device; thisbf && thisbf->device_data != data; thisbf = thisbf->next_proc)
  1821. {}
  1822. for ( ; thisbf && thisbf->device_data == data; thisbf = thisbf->next_proc)
  1823. {
  1824. thisthr = bitforce->thr[0];
  1825. thisthr->pause = thr->pause;
  1826. thisbf->deven = bitforce->deven;
  1827. }
  1828. }
  1829. static void bitforce_queue_thread_disable(struct thr_info *thr)
  1830. {
  1831. // Disable other threads sharing the same queue
  1832. bitforce_queue_thread_deven(thr);
  1833. }
  1834. static void bitforce_queue_thread_enable(struct thr_info *thr)
  1835. {
  1836. // TODO: Maybe reinit?
  1837. // Enable other threads sharing the same queue
  1838. bitforce_queue_thread_deven(thr);
  1839. }
  1840. struct device_drv bitforce_queue_api = {
  1841. .dname = "bitforce_queue",
  1842. .name = "BFL",
  1843. .minerloop = minerloop_queue,
  1844. .reinit_device = bitforce_reinit,
  1845. #ifdef HAVE_CURSES
  1846. .proc_wlogprint_status = bitforce_wlogprint_status,
  1847. .proc_tui_wlogprint_choices = bitforce_tui_wlogprint_choices,
  1848. .proc_tui_handle_choice = bitforce_tui_handle_choice,
  1849. #endif
  1850. .get_api_stats = bitforce_drv_stats,
  1851. .get_stats = bitforce_get_stats,
  1852. .set_device = bitforce_set_device,
  1853. .identify_device = bitforce_identify,
  1854. .thread_prepare = bitforce_thread_prepare,
  1855. .thread_init = bitforce_thread_init,
  1856. .queue_append = bitforce_queue_append,
  1857. .queue_flush = bitforce_queue_flush,
  1858. .poll = bitforce_queue_poll,
  1859. .thread_shutdown = bitforce_shutdown,
  1860. .thread_disable = bitforce_queue_thread_disable,
  1861. .thread_enable = bitforce_queue_thread_enable,
  1862. };