driver-bitforce.c 59 KB

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