driver-bitforce.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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. #define BITFORCE_SLEEP_MS 500
  23. #define BITFORCE_TIMEOUT_S 7
  24. #define BITFORCE_TIMEOUT_MS (BITFORCE_TIMEOUT_S * 1000)
  25. #define BITFORCE_LONG_TIMEOUT_S 25
  26. #define BITFORCE_LONG_TIMEOUT_MS (BITFORCE_LONG_TIMEOUT_S * 1000)
  27. #define BITFORCE_CHECK_INTERVAL_MS 10
  28. #define WORK_CHECK_INTERVAL_MS 50
  29. #define MAX_START_DELAY_MS 100
  30. #define tv_to_ms(tval) ((unsigned long)(tval.tv_sec * 1000 + tval.tv_usec / 1000))
  31. #define TIME_AVG_CONSTANT 8
  32. #define BITFORCE_MAX_QUEUED 10
  33. #define BITFORCE_MAX_QRESULTS 10
  34. #define BITFORCE_GOAL_QRESULTS (BITFORCE_MAX_QRESULTS / 2)
  35. enum bitforce_proto {
  36. BFP_WORK,
  37. BFP_RANGE,
  38. BFP_QUEUE,
  39. BFP_BQUEUE,
  40. };
  41. static const char *protonames[] = {
  42. "full work",
  43. "nonce range",
  44. "work queue",
  45. "bulk queue",
  46. };
  47. struct device_api bitforce_api;
  48. struct device_api bitforce_queue_api;
  49. // Code must deal with a timeout
  50. #define BFopen(devpath) serial_open(devpath, 0, 250, true)
  51. static void BFgets(char *buf, size_t bufLen, int fd)
  52. {
  53. do {
  54. buf[0] = '\0';
  55. --bufLen;
  56. } while (likely(bufLen && read(fd, buf, 1) == 1 && (buf++)[0] != '\n'));
  57. buf[0] = '\0';
  58. }
  59. static ssize_t BFwrite(int fd, const void *buf, ssize_t bufLen)
  60. {
  61. if ((bufLen) != write(fd, buf, bufLen))
  62. return 0;
  63. else
  64. return bufLen;
  65. }
  66. static ssize_t bitforce_send(int fd, int procid, const void *buf, ssize_t bufLen)
  67. {
  68. if (!procid)
  69. return BFwrite(fd, buf, bufLen);
  70. if (bufLen > 255)
  71. return -1;
  72. size_t bufLeft = bufLen + 3;
  73. char realbuf[bufLeft], *bufp;
  74. ssize_t rv;
  75. memcpy(&realbuf[3], buf, bufLen);
  76. realbuf[0] = '@';
  77. realbuf[1] = procid;
  78. realbuf[2] = bufLen;
  79. bufp = realbuf;
  80. while (true)
  81. {
  82. rv = BFwrite(fd, bufp, bufLeft);
  83. if (rv <= 0)
  84. return rv;
  85. bufLeft -= rv;
  86. }
  87. return bufLen;
  88. }
  89. static
  90. void bitforce_cmd1(int fd, int procid, void *buf, size_t bufsz, const char *cmd)
  91. {
  92. bitforce_send(fd, procid, cmd, 3);
  93. BFgets(buf, bufsz, fd);
  94. }
  95. static
  96. void bitforce_cmd2(int fd, int procid, void *buf, size_t bufsz, const char *cmd, void *data, size_t datasz)
  97. {
  98. bitforce_cmd1(fd, procid, buf, bufsz, cmd);
  99. if (strncasecmp(buf, "OK", 2))
  100. return;
  101. bitforce_send(fd, procid, data, datasz);
  102. BFgets(buf, bufsz, fd);
  103. }
  104. #define BFclose(fd) close(fd)
  105. struct bitforce_init_data {
  106. bool sc;
  107. long devmask;
  108. };
  109. static bool bitforce_detect_one(const char *devpath)
  110. {
  111. int fdDev = serial_open(devpath, 0, 10, true);
  112. struct cgpu_info *bitforce;
  113. char pdevbuf[0x100];
  114. size_t pdevbuf_len;
  115. char *s;
  116. int procs = 1;
  117. struct bitforce_init_data *initdata;
  118. applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
  119. if (unlikely(fdDev == -1)) {
  120. applog(LOG_DEBUG, "BFL: Failed to open %s", devpath);
  121. return false;
  122. }
  123. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  124. if (unlikely(!pdevbuf[0])) {
  125. applog(LOG_DEBUG, "BFL: Error reading/timeout (ZGX)");
  126. return 0;
  127. }
  128. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  129. applog(LOG_DEBUG, "BFL: Didn't recognise BitForce on %s", devpath);
  130. BFclose(fdDev);
  131. return false;
  132. }
  133. applog(LOG_DEBUG, "Found BitForce device on %s", devpath);
  134. initdata = malloc(sizeof(*initdata));
  135. *initdata = (struct bitforce_init_data){
  136. .sc = false,
  137. };
  138. for ( bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZCX");
  139. strncasecmp(pdevbuf, "OK", 2);
  140. BFgets(pdevbuf, sizeof(pdevbuf), fdDev) )
  141. {
  142. pdevbuf_len = strlen(pdevbuf);
  143. if (unlikely(!pdevbuf_len))
  144. continue;
  145. pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
  146. applog(LOG_DEBUG, " %s", pdevbuf);
  147. if (!strncasecmp(pdevbuf, "DEVICES IN CHAIN:", 17))
  148. procs = atoi(&pdevbuf[17]);
  149. else
  150. if (!strncasecmp(pdevbuf, "CHAIN PRESENCE MASK:", 20))
  151. initdata->devmask = strtol(&pdevbuf[20], NULL, 16);
  152. else
  153. if (!strncasecmp(pdevbuf, "DEVICE:", 7) && strstr(pdevbuf, "SC"))
  154. initdata->sc = true;
  155. }
  156. BFclose(fdDev);
  157. // We have a real BitForce!
  158. bitforce = calloc(1, sizeof(*bitforce));
  159. bitforce->api = &bitforce_api;
  160. if (initdata->sc && procs > 1)
  161. bitforce->api = &bitforce_queue_api;
  162. bitforce->device_path = strdup(devpath);
  163. bitforce->deven = DEV_ENABLED;
  164. bitforce->procs = procs;
  165. bitforce->threads = 1;
  166. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  167. s[0] = '\0';
  168. bitforce->name = strdup(pdevbuf + 7);
  169. }
  170. bitforce->cgpu_data = initdata;
  171. mutex_init(&bitforce->device_mutex);
  172. return add_cgpu(bitforce);
  173. }
  174. static int bitforce_detect_auto(void)
  175. {
  176. return serial_autodetect(bitforce_detect_one, "BitFORCE", "SHA256");
  177. }
  178. static void bitforce_detect(void)
  179. {
  180. serial_detect_auto(&bitforce_api, bitforce_detect_one, bitforce_detect_auto);
  181. }
  182. struct bitforce_data {
  183. int xlink_id;
  184. unsigned char next_work_ob[70]; // Data aligned for 32-bit access
  185. unsigned char *next_work_obs; // Start of data to send
  186. unsigned char next_work_obsz;
  187. const char *next_work_cmd;
  188. char noncebuf[14 + (BITFORCE_MAX_QUEUED * 165)];
  189. int poll_func;
  190. enum bitforce_proto proto;
  191. bool sc;
  192. int queued;
  193. bool already_have_results;
  194. bool just_flushed;
  195. int ready_to_queue;
  196. unsigned result_busy_polled;
  197. unsigned sleep_ms_default;
  198. struct timeval tv_hashmeter_start;
  199. };
  200. static void bitforce_clear_buffer(struct cgpu_info *);
  201. static
  202. void bitforce_comm_error(struct thr_info *thr)
  203. {
  204. struct cgpu_info *bitforce = thr->cgpu;
  205. struct bitforce_data *data = bitforce->cgpu_data;
  206. int *p_fdDev = &bitforce->device->device_fd;
  207. data->noncebuf[0] = '\0';
  208. applog(LOG_ERR, "%"PRIpreprv": Comms error", bitforce->proc_repr);
  209. dev_error(bitforce, REASON_DEV_COMMS_ERROR);
  210. ++bitforce->hw_errors;
  211. ++hw_errors;
  212. BFclose(*p_fdDev);
  213. int fd = *p_fdDev = BFopen(bitforce->device_path);
  214. if (fd == -1)
  215. {
  216. applog(LOG_ERR, "%s: Error reopening %s", bitforce->dev_repr, bitforce->device_path);
  217. return;
  218. }
  219. /* empty read buffer */
  220. bitforce_clear_buffer(bitforce);
  221. }
  222. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  223. {
  224. float gt = bitforce->temp;
  225. if (gt > 0)
  226. tailsprintf(buf, "%5.1fC ", gt);
  227. else
  228. tailsprintf(buf, " ", gt);
  229. tailsprintf(buf, " | ");
  230. }
  231. static bool bitforce_thread_prepare(struct thr_info *thr)
  232. {
  233. struct cgpu_info *bitforce = thr->cgpu;
  234. int fdDev = BFopen(bitforce->device_path);
  235. struct timeval now;
  236. if (unlikely(fdDev == -1)) {
  237. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, bitforce->device_path);
  238. return false;
  239. }
  240. bitforce->device_fd = fdDev;
  241. applog(LOG_INFO, "%s: Opened %s", bitforce->dev_repr, bitforce->device_path);
  242. gettimeofday(&now, NULL);
  243. get_datestamp(bitforce->init, &now);
  244. return true;
  245. }
  246. static void bitforce_clear_buffer(struct cgpu_info *bitforce)
  247. {
  248. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  249. int fdDev = bitforce->device->device_fd;
  250. char pdevbuf[0x100];
  251. int count = 0;
  252. if (!fdDev)
  253. return;
  254. applog(LOG_DEBUG, "%"PRIpreprv": Clearing read buffer", bitforce->proc_repr);
  255. mutex_lock(mutexp);
  256. do {
  257. pdevbuf[0] = '\0';
  258. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  259. } while (pdevbuf[0] && (++count < 10));
  260. mutex_unlock(mutexp);
  261. }
  262. void bitforce_init(struct cgpu_info *bitforce)
  263. {
  264. const char *devpath = bitforce->device_path;
  265. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  266. int *p_fdDev = &bitforce->device->device_fd;
  267. int fdDev = *p_fdDev, retries = 0;
  268. char pdevbuf[0x100];
  269. char *s;
  270. applog(LOG_WARNING, "%"PRIpreprv": Re-initialising", bitforce->proc_repr);
  271. bitforce_clear_buffer(bitforce);
  272. mutex_lock(mutexp);
  273. if (fdDev) {
  274. BFclose(fdDev);
  275. sleep(5);
  276. }
  277. *p_fdDev = 0;
  278. fdDev = BFopen(devpath);
  279. if (unlikely(fdDev == -1)) {
  280. mutex_unlock(mutexp);
  281. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, devpath);
  282. return;
  283. }
  284. do {
  285. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  286. if (unlikely(!pdevbuf[0])) {
  287. mutex_unlock(mutexp);
  288. applog(LOG_ERR, "%s: Error reading/timeout (ZGX)", bitforce->dev_repr);
  289. return;
  290. }
  291. if (retries++)
  292. nmsleep(10);
  293. } while (!strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  294. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  295. mutex_unlock(mutexp);
  296. applog(LOG_ERR, "%s: Didn't recognise BitForce on %s returned: %s", bitforce->dev_repr, devpath, pdevbuf);
  297. return;
  298. }
  299. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  300. s[0] = '\0';
  301. free((void*)bitforce->name);
  302. bitforce->name = strdup(pdevbuf + 7);
  303. }
  304. *p_fdDev = fdDev;
  305. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  306. mutex_unlock(mutexp);
  307. }
  308. static void bitforce_flash_led(struct cgpu_info *bitforce)
  309. {
  310. struct bitforce_data *data = bitforce->cgpu_data;
  311. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  312. int fdDev = bitforce->device->device_fd;
  313. if (!fdDev)
  314. return;
  315. /* Do not try to flash the led if we're polling for a result to
  316. * minimise the chance of interleaved results */
  317. if (bitforce->polling)
  318. return;
  319. /* It is not critical flashing the led so don't get stuck if we
  320. * can't grab the mutex here */
  321. if (mutex_trylock(mutexp))
  322. return;
  323. char pdevbuf[0x100];
  324. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZMX");
  325. /* Once we've tried - don't do it until told to again */
  326. bitforce->flash_led = false;
  327. /* However, this stops anything else getting a reply
  328. * So best to delay any other access to the BFL */
  329. sleep(4);
  330. mutex_unlock(mutexp);
  331. return; // nothing is returned by the BFL
  332. }
  333. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  334. {
  335. struct bitforce_data *data = bitforce->cgpu_data;
  336. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  337. int fdDev = bitforce->device->device_fd;
  338. char pdevbuf[0x100];
  339. char *s;
  340. if (!fdDev)
  341. return false;
  342. /* Do not try to get the temperature if we're polling for a result to
  343. * minimise the chance of interleaved results */
  344. if (bitforce->polling)
  345. return true;
  346. // Flash instead of Temp - doing both can be too slow
  347. if (bitforce->flash_led) {
  348. bitforce_flash_led(bitforce);
  349. return true;
  350. }
  351. /* It is not critical getting temperature so don't get stuck if we
  352. * can't grab the mutex here */
  353. if (mutex_trylock(mutexp))
  354. return false;
  355. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZLX");
  356. mutex_unlock(mutexp);
  357. if (unlikely(!pdevbuf[0])) {
  358. applog(LOG_ERR, "%"PRIpreprv": Error: Get temp returned empty string/timed out", bitforce->proc_repr);
  359. bitforce->hw_errors++;
  360. ++hw_errors;
  361. return false;
  362. }
  363. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  364. float temp = strtof(s + 1, NULL);
  365. /* Cope with older software that breaks and reads nonsense
  366. * values */
  367. if (temp > 100)
  368. temp = strtod(s + 1, NULL);
  369. if (temp > 0) {
  370. bitforce->temp = temp;
  371. }
  372. } else {
  373. /* Use the temperature monitor as a kind of watchdog for when
  374. * our responses are out of sync and flush the buffer to
  375. * hopefully recover */
  376. applog(LOG_WARNING, "%"PRIpreprv": Garbled response probably throttling, clearing buffer", bitforce->proc_repr);
  377. dev_error(bitforce, REASON_DEV_THROTTLE);
  378. /* Count throttling episodes as hardware errors */
  379. bitforce->hw_errors++;
  380. ++hw_errors;
  381. bitforce_clear_buffer(bitforce);
  382. return false;
  383. }
  384. return true;
  385. }
  386. static inline
  387. void dbg_block_data(struct cgpu_info *bitforce)
  388. {
  389. if (!opt_debug)
  390. return;
  391. struct bitforce_data *data = bitforce->cgpu_data;
  392. char *s;
  393. s = bin2hex(&data->next_work_ob[8], 44);
  394. applog(LOG_DEBUG, "%"PRIpreprv": block data: %s", bitforce->proc_repr, s);
  395. free(s);
  396. }
  397. static void bitforce_change_mode(struct cgpu_info *, enum bitforce_proto);
  398. static
  399. bool bitforce_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  400. {
  401. struct cgpu_info *bitforce = thr->cgpu;
  402. struct bitforce_data *data = bitforce->cgpu_data;
  403. int fdDev = bitforce->device->device_fd;
  404. unsigned char *ob_ms = &data->next_work_ob[8];
  405. unsigned char *ob_dt = &ob_ms[32];
  406. // If polling job_start, cancel it
  407. if (data->poll_func == 1)
  408. {
  409. thr->tv_poll.tv_sec = -1;
  410. data->poll_func = 0;
  411. }
  412. memcpy(ob_ms, work->midstate, 32);
  413. memcpy(ob_dt, work->data + 64, 12);
  414. switch (data->proto)
  415. {
  416. case BFP_BQUEUE:
  417. quit(1, "%"PRIpreprv": Impossible BFP_BQUEUE in bitforce_job_prepare", bitforce->proc_repr);
  418. case BFP_RANGE:
  419. {
  420. uint32_t *ob_nonce = (uint32_t*)&(ob_dt[32]);
  421. ob_nonce[0] = htobe32(work->blk.nonce);
  422. ob_nonce[1] = htobe32(work->blk.nonce + bitforce->nonces);
  423. // FIXME: if nonce range fails... we didn't increment enough
  424. work->blk.nonce += bitforce->nonces + 1;
  425. break;
  426. }
  427. case BFP_QUEUE:
  428. if (thr->work)
  429. {
  430. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  431. char pdevbuf[0x100];
  432. if (unlikely(!fdDev))
  433. return false;
  434. mutex_lock(mutexp);
  435. if (data->queued)
  436. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZQX");
  437. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, data->next_work_obs, data->next_work_obsz);
  438. mutex_unlock(mutexp);
  439. if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  440. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  441. bitforce_change_mode(bitforce, BFP_WORK);
  442. }
  443. else
  444. {
  445. dbg_block_data(bitforce);
  446. data->queued = 1;
  447. }
  448. }
  449. // fallthru...
  450. case BFP_WORK:
  451. work->blk.nonce = 0xffffffff;
  452. }
  453. return true;
  454. }
  455. static
  456. void bitforce_change_mode(struct cgpu_info *bitforce, enum bitforce_proto proto)
  457. {
  458. struct bitforce_data *data = bitforce->cgpu_data;
  459. if (data->proto == proto)
  460. return;
  461. if (data->proto == BFP_RANGE)
  462. {
  463. bitforce->nonces = 0xffffffff;
  464. bitforce->sleep_ms *= 5;
  465. data->sleep_ms_default *= 5;
  466. switch (proto)
  467. {
  468. case BFP_WORK:
  469. data->next_work_cmd = "ZDX";
  470. break;
  471. case BFP_QUEUE:
  472. data->next_work_cmd = "ZNX";
  473. default:
  474. ;
  475. }
  476. if (data->sc)
  477. {
  478. // "S|---------- MidState ----------||-DataTail-|E"
  479. data->next_work_ob[7] = 45;
  480. data->next_work_ob[8+32+12] = '\xAA';
  481. data->next_work_obsz = 46;
  482. }
  483. else
  484. {
  485. // ">>>>>>>>|---------- MidState ----------||-DataTail-|>>>>>>>>"
  486. memset(&data->next_work_ob[8+32+12], '>', 8);
  487. data->next_work_obsz = 60;
  488. }
  489. }
  490. else
  491. if (proto == BFP_RANGE)
  492. {
  493. /* Split work up into 1/5th nonce ranges */
  494. bitforce->nonces = 0x33333332;
  495. bitforce->sleep_ms /= 5;
  496. data->sleep_ms_default /= 5;
  497. data->next_work_cmd = "ZPX";
  498. if (data->sc)
  499. {
  500. data->next_work_ob[7] = 53;
  501. data->next_work_obsz = 54;
  502. }
  503. else
  504. data->next_work_obsz = 68;
  505. }
  506. data->proto = proto;
  507. bitforce->kname = protonames[proto];
  508. }
  509. static
  510. void bitforce_job_start(struct thr_info *thr)
  511. {
  512. struct cgpu_info *bitforce = thr->cgpu;
  513. struct bitforce_data *data = bitforce->cgpu_data;
  514. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  515. int fdDev = bitforce->device->device_fd;
  516. unsigned char *ob = data->next_work_obs;
  517. char pdevbuf[0x100];
  518. struct timeval tv_now;
  519. data->result_busy_polled = 0;
  520. if (data->queued)
  521. {
  522. uint32_t delay;
  523. // get_results collected more accurate job start time
  524. mt_job_transition(thr);
  525. job_start_complete(thr);
  526. data->queued = 0;
  527. delay = (uint32_t)bitforce->sleep_ms * 1000;
  528. if (unlikely(data->already_have_results))
  529. delay = 0;
  530. timer_set_delay(&thr->tv_morework, &bitforce->work_start_tv, delay);
  531. return;
  532. }
  533. if (!fdDev)
  534. goto commerr;
  535. re_send:
  536. mutex_lock(mutexp);
  537. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, ob, data->next_work_obsz);
  538. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  539. mutex_unlock(mutexp);
  540. gettimeofday(&tv_now, NULL);
  541. timer_set_delay(&thr->tv_poll, &tv_now, WORK_CHECK_INTERVAL_MS * 1000);
  542. data->poll_func = 1;
  543. return;
  544. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  545. mutex_unlock(mutexp);
  546. switch (data->proto)
  547. {
  548. case BFP_RANGE:
  549. applog(LOG_WARNING, "%"PRIpreprv": Does not support nonce range, disabling", bitforce->proc_repr);
  550. bitforce_change_mode(bitforce, BFP_WORK);
  551. goto re_send;
  552. case BFP_QUEUE:
  553. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  554. bitforce_change_mode(bitforce, BFP_WORK);
  555. goto re_send;
  556. default:
  557. ;
  558. }
  559. applog(LOG_ERR, "%"PRIpreprv": Error: Send work reports: %s", bitforce->proc_repr, pdevbuf);
  560. goto commerr;
  561. }
  562. mt_job_transition(thr);
  563. mutex_unlock(mutexp);
  564. dbg_block_data(bitforce);
  565. gettimeofday(&tv_now, NULL);
  566. bitforce->work_start_tv = tv_now;
  567. timer_set_delay(&thr->tv_morework, &tv_now, bitforce->sleep_ms * 1000);
  568. job_start_complete(thr);
  569. return;
  570. commerr:
  571. bitforce_comm_error(thr);
  572. job_start_abort(thr, true);
  573. }
  574. static char _discardedbuf[0x10];
  575. static
  576. int bitforce_zox(struct thr_info *thr, const char *cmd)
  577. {
  578. struct cgpu_info *bitforce = thr->cgpu;
  579. struct bitforce_data *data = bitforce->cgpu_data;
  580. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  581. int fd = bitforce->device->device_fd;
  582. char *pdevbuf = &data->noncebuf[0];
  583. int count;
  584. mutex_lock(mutexp);
  585. bitforce_cmd1(fd, data->xlink_id, pdevbuf, sizeof(data->noncebuf), cmd);
  586. if (!strncasecmp(pdevbuf, "INPROCESS:", 10))
  587. BFgets(pdevbuf, sizeof(data->noncebuf), fd);
  588. if (!strncasecmp(pdevbuf, "COUNT:", 6))
  589. {
  590. count = atoi(&pdevbuf[6]);
  591. size_t cls = strlen(pdevbuf);
  592. char *pmorebuf = &pdevbuf[cls];
  593. size_t szleft = sizeof(data->noncebuf) - cls, sz;
  594. if (count && data->queued)
  595. gettimeofday(&bitforce->work_start_tv, NULL);
  596. while (true)
  597. {
  598. BFgets(pmorebuf, szleft, fd);
  599. if (!strncasecmp(pmorebuf, "OK", 2))
  600. {
  601. pmorebuf[0] = '\0'; // process expects only results
  602. break;
  603. }
  604. sz = strlen(pmorebuf);
  605. szleft -= sz;
  606. pmorebuf += sz;
  607. if (unlikely(!szleft))
  608. {
  609. // Out of buffer space somehow :(
  610. applog(LOG_DEBUG, "%"PRIpreprv": Ran out of buffer space for results, discarding extra data", bitforce->proc_repr);
  611. pmorebuf = _discardedbuf;
  612. szleft = sizeof(_discardedbuf);
  613. }
  614. }
  615. }
  616. else
  617. count = -1;
  618. mutex_unlock(mutexp);
  619. return count;
  620. }
  621. static inline char *next_line(char *);
  622. static
  623. void bitforce_job_get_results(struct thr_info *thr, struct work *work)
  624. {
  625. struct cgpu_info *bitforce = thr->cgpu;
  626. struct bitforce_data *data = bitforce->cgpu_data;
  627. int fdDev = bitforce->device->device_fd;
  628. unsigned int delay_time_ms;
  629. struct timeval elapsed;
  630. struct timeval now;
  631. char *pdevbuf = &data->noncebuf[0];
  632. bool stale;
  633. int count;
  634. gettimeofday(&now, NULL);
  635. timersub(&now, &bitforce->work_start_tv, &elapsed);
  636. bitforce->wait_ms = tv_to_ms(elapsed);
  637. bitforce->polling = true;
  638. if (!fdDev)
  639. goto commerr;
  640. stale = stale_work(work, true);
  641. if (unlikely(bitforce->wait_ms < bitforce->sleep_ms))
  642. {
  643. // We're likely here because of a work restart
  644. // Since Bitforce cannot stop a work without losing results, only do it if the current job is finding stale shares
  645. // BFP_QUEUE does not support stopping work at all
  646. if (data->proto == BFP_QUEUE || !stale)
  647. {
  648. delay_time_ms = bitforce->sleep_ms - bitforce->wait_ms;
  649. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  650. data->poll_func = 2;
  651. return;
  652. }
  653. }
  654. while (1) {
  655. if (data->already_have_results)
  656. {
  657. data->already_have_results = false;
  658. strcpy(pdevbuf, "COUNT:0");
  659. count = 1;
  660. break;
  661. }
  662. const char *cmd = (data->proto == BFP_QUEUE) ? "ZOX" : "ZFX";
  663. count = bitforce_zox(thr, cmd);
  664. gettimeofday(&now, NULL);
  665. timersub(&now, &bitforce->work_start_tv, &elapsed);
  666. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  667. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  668. tv_to_ms(elapsed), (unsigned long)BITFORCE_LONG_TIMEOUT_MS);
  669. goto out;
  670. }
  671. if (count > 0)
  672. {
  673. // Check that queue results match the current work
  674. // Also, if there are results from the next work, short-circuit this wait
  675. unsigned char midstate[32], datatail[12];
  676. char *p;
  677. int i;
  678. p = pdevbuf;
  679. for (i = 0; i < count; ++i)
  680. {
  681. p = next_line(p);
  682. hex2bin(midstate, p, 32);
  683. hex2bin(datatail, &p[65], 12);
  684. if (!(memcmp(work->midstate, midstate, 32) || memcmp(&work->data[64], datatail, 12)))
  685. break;
  686. }
  687. if (i == count)
  688. {
  689. // Didn't find the one we're waiting on
  690. // Must be extra stuff in the queue results
  691. applog(LOG_WARNING, "%"PRIpreprv": Found extra garbage in queue results: %s",
  692. bitforce->proc_repr, pdevbuf);
  693. count = 0;
  694. }
  695. else
  696. if (i == count - 1)
  697. // Last one found is what we're looking for
  698. {}
  699. else
  700. // We finished the next job too!
  701. data->already_have_results = true;
  702. }
  703. if (!count)
  704. goto noqr;
  705. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  706. break;
  707. data->result_busy_polled = bitforce->wait_ms;
  708. if (stale && data->proto != BFP_QUEUE)
  709. {
  710. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  711. bitforce->proc_repr);
  712. goto out;
  713. }
  714. noqr:
  715. data->result_busy_polled = bitforce->wait_ms;
  716. /* if BFL is throttling, no point checking so quickly */
  717. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  718. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  719. data->poll_func = 2;
  720. return;
  721. }
  722. if (count < 0 && pdevbuf[0] == 'N')
  723. count = strncasecmp(pdevbuf, "NONCE-FOUND", 11) ? 1 : 0;
  724. // At this point, 'count' is:
  725. // negative, in case of some kind of error
  726. // zero, if NO-NONCE (FPGA either completed with no results, or rebooted)
  727. // positive, if at least one job completed successfully
  728. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  729. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  730. tv_to_ms(elapsed), (unsigned long)BITFORCE_TIMEOUT_MS);
  731. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  732. ++bitforce->hw_errors;
  733. ++hw_errors;
  734. /* If the device truly throttled, it didn't process the job and there
  735. * are no results. But check first, just in case we're wrong about it
  736. * throttling.
  737. */
  738. if (count > 0)
  739. goto out;
  740. } else if (count >= 0) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  741. /* Simple timing adjustment. Allow a few polls to cope with
  742. * OS timer delays being variably reliable. wait_ms will
  743. * always equal sleep_ms when we've waited greater than or
  744. * equal to the result return time.*/
  745. delay_time_ms = bitforce->sleep_ms;
  746. if (!data->result_busy_polled)
  747. {
  748. // No busy polls before results received
  749. if (bitforce->wait_ms > delay_time_ms + (WORK_CHECK_INTERVAL_MS * 8))
  750. // ... due to poll being rather late; ignore it as an anomaly
  751. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, later than scheduled %ums (ignoring)",
  752. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms);
  753. else
  754. if (bitforce->sleep_ms > data->sleep_ms_default + (BITFORCE_CHECK_INTERVAL_MS * 0x20))
  755. {
  756. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on delayed schedule %ums; Wait time changed to: %ums (default sch)",
  757. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, data->sleep_ms_default);
  758. bitforce->sleep_ms = data->sleep_ms_default;
  759. }
  760. else
  761. {
  762. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on default schedule %ums; Wait time changed to: %ums (check interval)",
  763. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, BITFORCE_CHECK_INTERVAL_MS);
  764. bitforce->sleep_ms = BITFORCE_CHECK_INTERVAL_MS;
  765. }
  766. }
  767. else
  768. {
  769. if (data->result_busy_polled - bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  770. {
  771. bitforce->sleep_ms = data->result_busy_polled - (WORK_CHECK_INTERVAL_MS / 2);
  772. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time changed to: %ums",
  773. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms, bitforce->sleep_ms);
  774. }
  775. else
  776. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time unchanged",
  777. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms);
  778. }
  779. /* Work out the average time taken. Float for calculation, uint for display */
  780. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  781. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  782. }
  783. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  784. if (count < 0 && strncasecmp(pdevbuf, "I", 1)) {
  785. bitforce->hw_errors++;
  786. ++hw_errors;
  787. applog(LOG_WARNING, "%"PRIpreprv": Error: Get result reports: %s", bitforce->proc_repr, pdevbuf);
  788. bitforce_clear_buffer(bitforce);
  789. }
  790. out:
  791. bitforce->polling = false;
  792. job_results_fetched(thr);
  793. return;
  794. commerr:
  795. bitforce_comm_error(thr);
  796. goto out;
  797. }
  798. static
  799. void bitforce_process_result_nonces(struct thr_info *thr, struct work *work, char *pnoncebuf)
  800. {
  801. struct cgpu_info *bitforce = thr->cgpu;
  802. struct bitforce_data *data = bitforce->cgpu_data;
  803. uint32_t nonce;
  804. while (1) {
  805. hex2bin((void*)&nonce, pnoncebuf, 4);
  806. nonce = be32toh(nonce);
  807. if (unlikely(data->proto == BFP_RANGE && (nonce >= work->blk.nonce ||
  808. /* FIXME: blk.nonce is probably moved on quite a bit now! */
  809. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  810. applog(LOG_WARNING, "%"PRIpreprv": Disabling broken nonce range support", bitforce->proc_repr);
  811. bitforce_change_mode(bitforce, BFP_WORK);
  812. }
  813. submit_nonce(thr, work, nonce);
  814. if (strncmp(&pnoncebuf[8], ",", 1))
  815. break;
  816. pnoncebuf += 9;
  817. }
  818. }
  819. static
  820. bool bitforce_process_qresult_line_i(struct thr_info *thr, char *midstate, char *datatail, char *buf, struct work *work)
  821. {
  822. if (!work)
  823. return false;
  824. if (memcmp(work->midstate, midstate, 32))
  825. return false;
  826. if (memcmp(&work->data[64], datatail, 12))
  827. return false;
  828. if (!atoi(&buf[90]))
  829. return true;
  830. bitforce_process_result_nonces(thr, work, &buf[92]);
  831. return true;
  832. }
  833. static
  834. void bitforce_process_qresult_line(struct thr_info *thr, char *buf, struct work *work)
  835. {
  836. struct cgpu_info *bitforce = thr->cgpu;
  837. char midstate[32], datatail[12];
  838. hex2bin((void*)midstate, buf, 32);
  839. hex2bin((void*)datatail, &buf[65], 12);
  840. if (!( bitforce_process_qresult_line_i(thr, midstate, datatail, buf, work)
  841. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->work)
  842. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->prev_work)
  843. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->next_work) ))
  844. {
  845. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queued results", bitforce->proc_repr);
  846. ++bitforce->hw_errors;
  847. ++hw_errors;
  848. }
  849. }
  850. static inline
  851. char *next_line(char *in)
  852. {
  853. while (in[0] && (in++)[0] != '\n')
  854. {}
  855. return in;
  856. }
  857. static
  858. int64_t bitforce_job_process_results(struct thr_info *thr, struct work *work, __maybe_unused bool stopping)
  859. {
  860. struct cgpu_info *bitforce = thr->cgpu;
  861. struct bitforce_data *data = bitforce->cgpu_data;
  862. char *pnoncebuf = &data->noncebuf[0];
  863. int count;
  864. if (!strncasecmp(pnoncebuf, "NO-", 3))
  865. return bitforce->nonces; /* No valid nonce found */
  866. if (!strncasecmp(pnoncebuf, "NONCE-FOUND", 11))
  867. {
  868. bitforce_process_result_nonces(thr, work, &pnoncebuf[12]);
  869. count = 1;
  870. }
  871. else
  872. if (!strncasecmp(pnoncebuf, "COUNT:", 6))
  873. {
  874. count = 0;
  875. pnoncebuf = next_line(pnoncebuf);
  876. while (pnoncebuf[0])
  877. {
  878. bitforce_process_qresult_line(thr, pnoncebuf, work);
  879. ++count;
  880. pnoncebuf = next_line(pnoncebuf);
  881. }
  882. }
  883. else
  884. return 0;
  885. // FIXME: This might have changed in the meantime (new job start, or broken)
  886. return bitforce->nonces * count;
  887. }
  888. static void bitforce_shutdown(struct thr_info *thr)
  889. {
  890. struct cgpu_info *bitforce = thr->cgpu;
  891. int *p_fdDev = &bitforce->device->device_fd;
  892. BFclose(*p_fdDev);
  893. *p_fdDev = 0;
  894. }
  895. static void biforce_thread_enable(struct thr_info *thr)
  896. {
  897. struct cgpu_info *bitforce = thr->cgpu;
  898. bitforce_init(bitforce);
  899. }
  900. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  901. {
  902. return bitforce_get_temp(bitforce);
  903. }
  904. static bool bitforce_identify(struct cgpu_info *bitforce)
  905. {
  906. bitforce->flash_led = true;
  907. return true;
  908. }
  909. static bool bitforce_thread_init(struct thr_info *thr)
  910. {
  911. struct cgpu_info *bitforce = thr->cgpu;
  912. unsigned int wait;
  913. struct bitforce_data *data;
  914. struct bitforce_init_data *initdata = bitforce->cgpu_data;
  915. bool sc = initdata->sc;
  916. int xlink_id = 0;
  917. for ( ; bitforce; bitforce = bitforce->next_proc)
  918. {
  919. thr = bitforce->thr[0];
  920. if (unlikely(xlink_id > 30))
  921. {
  922. applog(LOG_ERR, "%"PRIpreprv": Failed to find XLINK address", bitforce->proc_repr);
  923. dev_error(bitforce, REASON_THREAD_FAIL_INIT);
  924. bitforce->reinit_backoff = 1e10;
  925. continue;
  926. }
  927. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  928. bitforce->cgpu_data = data = malloc(sizeof(*data));
  929. *data = (struct bitforce_data){
  930. .xlink_id = xlink_id,
  931. .next_work_ob = ">>>>>>>>|---------- MidState ----------||-DataTail-||Nonces|>>>>>>>>",
  932. .proto = BFP_RANGE,
  933. .sc = sc,
  934. .sleep_ms_default = BITFORCE_SLEEP_MS,
  935. };
  936. if (sc)
  937. {
  938. // ".......S|---------- MidState ----------||-DataTail-||Nonces|E"
  939. data->next_work_ob[8+32+12+8] = '\xAA';
  940. data->next_work_obs = &data->next_work_ob[7];
  941. if (bitforce->api == &bitforce_queue_api)
  942. {
  943. INIT_LIST_HEAD(&thr->work_list);
  944. bitforce_change_mode(bitforce, BFP_BQUEUE);
  945. timer_set_delay_from_now(&thr->tv_poll, 0);
  946. }
  947. else
  948. bitforce_change_mode(bitforce, BFP_QUEUE);
  949. }
  950. else
  951. {
  952. data->next_work_obs = &data->next_work_ob[0];
  953. // Unconditionally change away from cold-initialized BFP_RANGE, to allow for setting up other variables
  954. bitforce_change_mode(bitforce, BFP_WORK);
  955. /* Initially enable support for nonce range and disable it later if it
  956. * fails */
  957. if (opt_bfl_noncerange)
  958. bitforce_change_mode(bitforce, BFP_RANGE);
  959. }
  960. while (xlink_id < 31 && !(initdata->devmask & (1 << ++xlink_id)))
  961. {}
  962. }
  963. bitforce = thr->cgpu;
  964. /* Pause each new thread at least 100ms between initialising
  965. * so the devices aren't making calls all at the same time. */
  966. wait = thr->id * MAX_START_DELAY_MS;
  967. applog(LOG_DEBUG, "%s: Delaying start by %dms", bitforce->dev_repr, wait / 1000);
  968. nmsleep(wait);
  969. return true;
  970. }
  971. static struct api_data *bitforce_api_stats(struct cgpu_info *cgpu)
  972. {
  973. struct api_data *root = NULL;
  974. // Warning, access to these is not locked - but we don't really
  975. // care since hashing performance is way more important than
  976. // locking access to displaying API debug 'stats'
  977. // If locking becomes an issue for any of them, use copy_data=true also
  978. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  979. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  980. return root;
  981. }
  982. void bitforce_poll(struct thr_info *thr)
  983. {
  984. struct cgpu_info *bitforce = thr->cgpu;
  985. struct bitforce_data *data = bitforce->cgpu_data;
  986. int poll = data->poll_func;
  987. thr->tv_poll.tv_sec = -1;
  988. data->poll_func = 0;
  989. switch (poll)
  990. {
  991. case 1:
  992. bitforce_job_start(thr);
  993. break;
  994. case 2:
  995. bitforce_job_get_results(thr, thr->work);
  996. break;
  997. default:
  998. applog(LOG_ERR, "%"PRIpreprv": Unexpected poll from device API!", thr->cgpu->proc_repr);
  999. }
  1000. }
  1001. struct device_api bitforce_api = {
  1002. .dname = "bitforce",
  1003. .name = "BFL",
  1004. .api_detect = bitforce_detect,
  1005. .get_api_stats = bitforce_api_stats,
  1006. .minerloop = minerloop_async,
  1007. .reinit_device = bitforce_init,
  1008. .get_statline_before = get_bitforce_statline_before,
  1009. .get_stats = bitforce_get_stats,
  1010. .identify_device = bitforce_identify,
  1011. .thread_prepare = bitforce_thread_prepare,
  1012. .thread_init = bitforce_thread_init,
  1013. .job_prepare = bitforce_job_prepare,
  1014. .job_start = bitforce_job_start,
  1015. .job_get_results = bitforce_job_get_results,
  1016. .poll = bitforce_poll,
  1017. .job_process_results = bitforce_job_process_results,
  1018. .thread_shutdown = bitforce_shutdown,
  1019. .thread_enable = biforce_thread_enable
  1020. };
  1021. static inline
  1022. void bitforce_set_queue_full(struct thr_info *thr)
  1023. {
  1024. struct cgpu_info *bitforce = thr->cgpu;
  1025. struct bitforce_data *data = bitforce->cgpu_data;
  1026. thr->queue_full = (data->queued + data->ready_to_queue >= BITFORCE_MAX_QUEUED);
  1027. }
  1028. static
  1029. bool bitforce_send_queue(struct thr_info *thr)
  1030. {
  1031. struct cgpu_info *bitforce = thr->cgpu;
  1032. struct bitforce_data *data = bitforce->cgpu_data;
  1033. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  1034. int fd = bitforce->device->device_fd;
  1035. struct work *work;
  1036. struct list_head *pos;
  1037. if (unlikely(!(fd && data->ready_to_queue)))
  1038. return false;
  1039. char buf[0x100];
  1040. int queued_ok;
  1041. size_t qjs_sz = (32 + 12 + 2);
  1042. size_t qjp_sz = 4 + (qjs_sz * data->ready_to_queue);
  1043. uint8_t qjp[qjp_sz], *qjs;
  1044. qjp[0] = qjp_sz - 1;
  1045. qjp[1] = 0xc1;
  1046. qjp[2] = data->ready_to_queue;
  1047. qjp[qjp_sz - 1] = 0xfe;
  1048. qjs = &qjp[qjp_sz - 1];
  1049. pos = thr->work_list.prev;
  1050. for (int i = data->ready_to_queue; i > 0; --i, pos = pos->prev)
  1051. {
  1052. work = list_entry(pos, typeof(*work), list);
  1053. *(--qjs) = 0xaa;
  1054. memcpy(qjs -= 12, work->data + 64, 12);
  1055. memcpy(qjs -= 32, work->midstate, 32);
  1056. *(--qjs) = 45;
  1057. }
  1058. mutex_lock(mutexp);
  1059. bitforce_cmd2(fd, data->xlink_id, buf, sizeof(buf), "ZWX", qjp, qjp_sz);
  1060. mutex_unlock(mutexp);
  1061. if (!strncasecmp(buf, "ERR:QUEUE", 9))
  1062. {
  1063. // Queue full :(
  1064. applog(LOG_DEBUG, "%"PRIpreprv": Device queue full while attempting to append %d jobs (queued<=%d)",
  1065. bitforce->proc_repr,
  1066. data->ready_to_queue, data->queued);
  1067. thr->queue_full = true;
  1068. return false;
  1069. }
  1070. if (strncasecmp(buf, "OK:QUEUED", 9))
  1071. {
  1072. // TODO: ERR:UNKNOWN COMMAND
  1073. applog(LOG_DEBUG, "%"PRIpreprv": Unexpected error attempting to append %d jobs (queued<=%d): %s",
  1074. bitforce->proc_repr,
  1075. data->ready_to_queue, data->queued, buf);
  1076. return false;
  1077. }
  1078. queued_ok = atoi(&buf[9]);
  1079. data->queued += queued_ok;
  1080. applog(LOG_DEBUG, "%"PRIpreprv": Successfully queued %d/%d jobs on device (queued<=%d)",
  1081. bitforce->proc_repr,
  1082. queued_ok, data->ready_to_queue, data->queued);
  1083. data->ready_to_queue -= queued_ok;
  1084. thr->queue_full = data->ready_to_queue;
  1085. data->just_flushed = false;
  1086. return true;
  1087. }
  1088. void work_list_del(struct list_head *pos)
  1089. {
  1090. struct work *work;
  1091. work = list_entry(pos, typeof(*work), list);
  1092. list_del(pos);
  1093. free_work(work);
  1094. }
  1095. static
  1096. bool bitforce_queue_do_results(struct thr_info *thr)
  1097. {
  1098. struct cgpu_info *bitforce = thr->cgpu;
  1099. struct bitforce_data *data = bitforce->cgpu_data;
  1100. int fd = bitforce->device->device_fd;
  1101. int count;
  1102. char *buf = &data->noncebuf[0];
  1103. unsigned char midstate[32], datatail[12];
  1104. struct work *work;
  1105. struct list_head *pos, *next_pos;
  1106. struct timeval tv_now, tv_elapsed;
  1107. if (unlikely(!fd))
  1108. return false;
  1109. count = bitforce_zox(thr, "ZOX");
  1110. if (!count)
  1111. return true;
  1112. if (unlikely(count < 0))
  1113. {
  1114. applog(LOG_ERR, "%"PRIpreprv": Received unexpected queue result response: %s", bitforce->proc_repr, buf);
  1115. ++bitforce->hw_errors;
  1116. ++hw_errors;
  1117. return false;
  1118. }
  1119. if (unlikely(list_empty(&thr->work_list)))
  1120. {
  1121. applog(LOG_ERR, "%"PRIpreprv": Received %d queued results when there was no queue", bitforce->proc_repr, count);
  1122. ++bitforce->hw_errors;
  1123. ++hw_errors;
  1124. return true;
  1125. }
  1126. if (count != BITFORCE_GOAL_QRESULTS)
  1127. {
  1128. unsigned int old_sleep_ms = bitforce->sleep_ms;
  1129. bitforce->sleep_ms = (uint32_t)bitforce->sleep_ms * BITFORCE_GOAL_QRESULTS / (count ?: 1);
  1130. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time changed to: %ums",
  1131. bitforce->proc_repr, count, old_sleep_ms, bitforce->sleep_ms);
  1132. }
  1133. else
  1134. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time unchanged",
  1135. bitforce->proc_repr, count, bitforce->sleep_ms);
  1136. count = 0;
  1137. while ((buf = next_line(buf)), buf[0])
  1138. {
  1139. if (strlen(buf) <= 90)
  1140. {
  1141. applog(LOG_ERR, "%"PRIpreprv": Gibberish within queue results: %s", bitforce->proc_repr, buf);
  1142. continue;
  1143. }
  1144. hex2bin(midstate, buf, 32);
  1145. hex2bin(datatail, &buf[65], 12);
  1146. for (pos = thr->work_list.next; ; pos = pos->next)
  1147. {
  1148. if (unlikely(pos == &thr->work_list))
  1149. {
  1150. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queue results", bitforce->proc_repr);
  1151. ++bitforce->hw_errors;
  1152. ++hw_errors;
  1153. goto next_qline;
  1154. }
  1155. work = list_entry(pos, typeof(*work), list);
  1156. if (unlikely(memcmp(work->midstate, midstate, 32)))
  1157. continue;
  1158. if (unlikely(memcmp(&work->data[64], datatail, 12)))
  1159. continue;
  1160. break;
  1161. }
  1162. ++count;
  1163. if (atoi(&buf[90]))
  1164. bitforce_process_result_nonces(thr, work, &buf[92]);
  1165. // Queue results are in order, so anything queued prior this is lost
  1166. // Delete all queued work up to, and including, this one
  1167. for ( ; pos != &thr->work_list; pos = next_pos)
  1168. {
  1169. next_pos = pos->prev;
  1170. work_list_del(pos);
  1171. --data->queued;
  1172. }
  1173. next_qline: (void)0;
  1174. }
  1175. bitforce_set_queue_full(thr);
  1176. gettimeofday(&tv_now, NULL);
  1177. timersub(&tv_now, &data->tv_hashmeter_start, &tv_elapsed);
  1178. hashes_done(thr, (uint64_t)bitforce->nonces * count, &tv_elapsed, NULL);
  1179. data->tv_hashmeter_start = tv_now;
  1180. return true;
  1181. }
  1182. static
  1183. bool bitforce_queue_append(struct thr_info *thr, struct work *work)
  1184. {
  1185. struct cgpu_info *bitforce = thr->cgpu;
  1186. struct bitforce_data *data = bitforce->cgpu_data;
  1187. bool rv, ndq;
  1188. bitforce_set_queue_full(thr);
  1189. rv = !thr->queue_full;
  1190. if (rv)
  1191. {
  1192. list_add_tail(&work->list, &thr->work_list);
  1193. ++data->ready_to_queue;
  1194. applog(LOG_DEBUG, "%"PRIpreprv": Appending to driver queue (max=%u, ready=%d, queued<=%d)",
  1195. bitforce->proc_repr,
  1196. (unsigned)BITFORCE_MAX_QUEUED, data->ready_to_queue, data->queued);
  1197. bitforce_set_queue_full(thr);
  1198. }
  1199. ndq = !data->queued;
  1200. if ((ndq) // Device is idle
  1201. || (data->ready_to_queue >= 5) // ...or 5 items ready to go
  1202. || (thr->queue_full) // ...or done filling queue
  1203. || (data->just_flushed) // ...or queue was just flushed (only remaining job is partly done already)
  1204. )
  1205. {
  1206. bitforce_send_queue(thr);
  1207. if (ndq)
  1208. gettimeofday(&data->tv_hashmeter_start, NULL);
  1209. }
  1210. return rv;
  1211. }
  1212. static
  1213. void bitforce_queue_flush(struct thr_info *thr)
  1214. {
  1215. struct cgpu_info *bitforce = thr->cgpu;
  1216. struct bitforce_data *data = bitforce->cgpu_data;
  1217. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  1218. int fd = bitforce->device->device_fd;
  1219. char buf[100];
  1220. unsigned flushed;
  1221. mutex_lock(mutexp);
  1222. bitforce_cmd1(fd, data->xlink_id, buf, sizeof(buf), "ZQX");
  1223. mutex_unlock(mutexp);
  1224. if (unlikely(strncasecmp(buf, "OK:FLUSHED", 10)))
  1225. {
  1226. applog(LOG_DEBUG, "%"PRIpreprv": Failed to flush device queue: %s", bitforce->proc_repr, buf);
  1227. flushed = 0;
  1228. }
  1229. else
  1230. flushed = atoi(&buf[10]);
  1231. applog(LOG_DEBUG, "%"PRIpreprv": Flushed %u jobs from device and %d from driver",
  1232. bitforce->proc_repr, flushed, data->ready_to_queue);
  1233. data->queued -= flushed;
  1234. flushed += data->ready_to_queue;
  1235. data->ready_to_queue = 0;
  1236. while (flushed--)
  1237. work_list_del(thr->work_list.prev);
  1238. thr->queue_full = false;
  1239. data->just_flushed = true;
  1240. bitforce_queue_do_results(thr);
  1241. }
  1242. static
  1243. void bitforce_queue_poll(struct thr_info *thr)
  1244. {
  1245. struct cgpu_info *bitforce = thr->cgpu;
  1246. bitforce_queue_do_results(thr);
  1247. timer_set_delay_from_now(&thr->tv_poll, bitforce->sleep_ms * 1000);
  1248. }
  1249. struct device_api bitforce_queue_api = {
  1250. .dname = "bitforce_queue",
  1251. .name = "BFL",
  1252. .minerloop = minerloop_queue,
  1253. .reinit_device = bitforce_init,
  1254. .get_statline_before = get_bitforce_statline_before,
  1255. .get_stats = bitforce_get_stats,
  1256. .identify_device = bitforce_identify,
  1257. .thread_prepare = bitforce_thread_prepare,
  1258. .thread_init = bitforce_thread_init,
  1259. .queue_append = bitforce_queue_append,
  1260. .queue_flush = bitforce_queue_flush,
  1261. .poll = bitforce_queue_poll,
  1262. .thread_shutdown = bitforce_shutdown,
  1263. .thread_enable = biforce_thread_enable
  1264. };