driver-bitforce.c 38 KB

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