driver-bitforce.c 51 KB

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