driver-bitforce.c 53 KB

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