driver-bitforce.c 38 KB

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