driver-bitforce.c 60 KB

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