driver-bitforce.c 51 KB

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