driver-bitforce.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. enum bitforce_proto {
  33. BFP_WORK,
  34. BFP_RANGE,
  35. BFP_QUEUE,
  36. };
  37. static const char *protonames[] = {
  38. "full work",
  39. "nonce range",
  40. "work queue",
  41. };
  42. struct device_api bitforce_api;
  43. // Code must deal with a timeout
  44. #define BFopen(devpath) serial_open(devpath, 0, 250, true)
  45. static void BFgets(char *buf, size_t bufLen, int fd)
  46. {
  47. do {
  48. buf[0] = '\0';
  49. --bufLen;
  50. } while (likely(bufLen && read(fd, buf, 1) == 1 && (buf++)[0] != '\n'));
  51. buf[0] = '\0';
  52. }
  53. static ssize_t BFwrite(int fd, const void *buf, ssize_t bufLen)
  54. {
  55. if ((bufLen) != write(fd, buf, bufLen))
  56. return 0;
  57. else
  58. return bufLen;
  59. }
  60. static ssize_t bitforce_send(int fd, int procid, const void *buf, ssize_t bufLen)
  61. {
  62. if (!procid)
  63. return BFwrite(fd, buf, bufLen);
  64. if (bufLen > 255)
  65. return -1;
  66. size_t bufLeft = bufLen + 3;
  67. char realbuf[bufLeft], *bufp;
  68. ssize_t rv;
  69. memcpy(&realbuf[3], buf, bufLen);
  70. realbuf[0] = '@';
  71. realbuf[1] = procid;
  72. realbuf[2] = bufLen;
  73. bufp = realbuf;
  74. while (true)
  75. {
  76. rv = BFwrite(fd, bufp, bufLeft);
  77. if (rv <= 0)
  78. return rv;
  79. bufLeft -= rv;
  80. }
  81. return bufLen;
  82. }
  83. static
  84. void bitforce_cmd1(int fd, int procid, void *buf, size_t bufsz, const char *cmd)
  85. {
  86. bitforce_send(fd, procid, cmd, 3);
  87. BFgets(buf, bufsz, fd);
  88. }
  89. static
  90. void bitforce_cmd2(int fd, int procid, void *buf, size_t bufsz, const char *cmd, void *data, size_t datasz)
  91. {
  92. bitforce_cmd1(fd, procid, buf, bufsz, cmd);
  93. if (strncasecmp(buf, "OK", 2))
  94. return;
  95. bitforce_send(fd, procid, data, datasz);
  96. BFgets(buf, bufsz, fd);
  97. }
  98. #define BFclose(fd) close(fd)
  99. struct bitforce_init_data {
  100. bool sc;
  101. long devmask;
  102. };
  103. static bool bitforce_detect_one(const char *devpath)
  104. {
  105. int fdDev = serial_open(devpath, 0, 10, true);
  106. struct cgpu_info *bitforce;
  107. char pdevbuf[0x100];
  108. size_t pdevbuf_len;
  109. char *s;
  110. int procs = 1;
  111. struct bitforce_init_data *initdata;
  112. applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
  113. if (unlikely(fdDev == -1)) {
  114. applog(LOG_DEBUG, "BFL: Failed to open %s", devpath);
  115. return false;
  116. }
  117. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  118. if (unlikely(!pdevbuf[0])) {
  119. applog(LOG_DEBUG, "BFL: Error reading/timeout (ZGX)");
  120. return 0;
  121. }
  122. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  123. applog(LOG_DEBUG, "BFL: Didn't recognise BitForce on %s", devpath);
  124. BFclose(fdDev);
  125. return false;
  126. }
  127. applog(LOG_DEBUG, "Found BitForce device on %s", devpath);
  128. initdata = malloc(sizeof(*initdata));
  129. *initdata = (struct bitforce_init_data){
  130. .sc = false,
  131. };
  132. for ( bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZCX");
  133. strncasecmp(pdevbuf, "OK", 2);
  134. BFgets(pdevbuf, sizeof(pdevbuf), fdDev) )
  135. {
  136. pdevbuf_len = strlen(pdevbuf);
  137. if (unlikely(!pdevbuf_len))
  138. continue;
  139. pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
  140. applog(LOG_DEBUG, " %s", pdevbuf);
  141. if (!strncasecmp(pdevbuf, "DEVICES IN CHAIN:", 17))
  142. procs = atoi(&pdevbuf[17]);
  143. else
  144. if (!strncasecmp(pdevbuf, "CHAIN PRESENCE MASK:", 20))
  145. initdata->devmask = strtol(&pdevbuf[20], NULL, 16);
  146. else
  147. if (!strncasecmp(pdevbuf, "DEVICE:", 7) && strstr(pdevbuf, "SC"))
  148. initdata->sc = true;
  149. }
  150. BFclose(fdDev);
  151. // We have a real BitForce!
  152. bitforce = calloc(1, sizeof(*bitforce));
  153. bitforce->api = &bitforce_api;
  154. bitforce->device_path = strdup(devpath);
  155. bitforce->deven = DEV_ENABLED;
  156. bitforce->procs = procs;
  157. bitforce->threads = 1;
  158. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  159. s[0] = '\0';
  160. bitforce->name = strdup(pdevbuf + 7);
  161. }
  162. bitforce->cgpu_data = initdata;
  163. mutex_init(&bitforce->device_mutex);
  164. return add_cgpu(bitforce);
  165. }
  166. static int bitforce_detect_auto(void)
  167. {
  168. return serial_autodetect(bitforce_detect_one, "BitFORCE", "SHA256");
  169. }
  170. static void bitforce_detect(void)
  171. {
  172. serial_detect_auto(&bitforce_api, bitforce_detect_one, bitforce_detect_auto);
  173. }
  174. struct bitforce_data {
  175. int xlink_id;
  176. unsigned char next_work_ob[70]; // Data aligned for 32-bit access
  177. unsigned char *next_work_obs; // Start of data to send
  178. unsigned char next_work_obsz;
  179. const char *next_work_cmd;
  180. char noncebuf[0x200]; // Large enough for 3 works of queue results
  181. int poll_func;
  182. enum bitforce_proto proto;
  183. bool sc;
  184. bool queued;
  185. unsigned result_busy_polled;
  186. unsigned sleep_ms_default;
  187. };
  188. static void bitforce_clear_buffer(struct cgpu_info *);
  189. static
  190. void bitforce_comm_error(struct thr_info *thr)
  191. {
  192. struct cgpu_info *bitforce = thr->cgpu;
  193. struct bitforce_data *data = bitforce->cgpu_data;
  194. int *p_fdDev = &bitforce->device->device_fd;
  195. data->noncebuf[0] = '\0';
  196. applog(LOG_ERR, "%"PRIpreprv": Comms error", bitforce->proc_repr);
  197. dev_error(bitforce, REASON_DEV_COMMS_ERROR);
  198. ++bitforce->hw_errors;
  199. ++hw_errors;
  200. BFclose(*p_fdDev);
  201. int fd = *p_fdDev = BFopen(bitforce->device_path);
  202. if (fd == -1)
  203. {
  204. applog(LOG_ERR, "%s: Error reopening %s", bitforce->dev_repr, bitforce->device_path);
  205. return;
  206. }
  207. /* empty read buffer */
  208. bitforce_clear_buffer(bitforce);
  209. }
  210. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  211. {
  212. float gt = bitforce->temp;
  213. if (gt > 0)
  214. tailsprintf(buf, "%5.1fC ", gt);
  215. else
  216. tailsprintf(buf, " ", gt);
  217. tailsprintf(buf, " | ");
  218. }
  219. static bool bitforce_thread_prepare(struct thr_info *thr)
  220. {
  221. struct cgpu_info *bitforce = thr->cgpu;
  222. int fdDev = BFopen(bitforce->device_path);
  223. struct timeval now;
  224. if (unlikely(fdDev == -1)) {
  225. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, bitforce->device_path);
  226. return false;
  227. }
  228. bitforce->device_fd = fdDev;
  229. applog(LOG_INFO, "%s: Opened %s", bitforce->dev_repr, bitforce->device_path);
  230. gettimeofday(&now, NULL);
  231. get_datestamp(bitforce->init, &now);
  232. return true;
  233. }
  234. static void bitforce_clear_buffer(struct cgpu_info *bitforce)
  235. {
  236. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  237. int fdDev = bitforce->device->device_fd;
  238. char pdevbuf[0x100];
  239. int count = 0;
  240. if (!fdDev)
  241. return;
  242. applog(LOG_DEBUG, "%"PRIpreprv": Clearing read buffer", bitforce->proc_repr);
  243. mutex_lock(mutexp);
  244. do {
  245. pdevbuf[0] = '\0';
  246. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  247. } while (pdevbuf[0] && (++count < 10));
  248. mutex_unlock(mutexp);
  249. }
  250. void bitforce_init(struct cgpu_info *bitforce)
  251. {
  252. const char *devpath = bitforce->device_path;
  253. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  254. int *p_fdDev = &bitforce->device->device_fd;
  255. int fdDev = *p_fdDev, retries = 0;
  256. char pdevbuf[0x100];
  257. char *s;
  258. applog(LOG_WARNING, "%"PRIpreprv": Re-initialising", bitforce->proc_repr);
  259. bitforce_clear_buffer(bitforce);
  260. mutex_lock(mutexp);
  261. if (fdDev) {
  262. BFclose(fdDev);
  263. sleep(5);
  264. }
  265. *p_fdDev = 0;
  266. fdDev = BFopen(devpath);
  267. if (unlikely(fdDev == -1)) {
  268. mutex_unlock(mutexp);
  269. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, devpath);
  270. return;
  271. }
  272. do {
  273. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  274. if (unlikely(!pdevbuf[0])) {
  275. mutex_unlock(mutexp);
  276. applog(LOG_ERR, "%s: Error reading/timeout (ZGX)", bitforce->dev_repr);
  277. return;
  278. }
  279. if (retries++)
  280. nmsleep(10);
  281. } while (!strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  282. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  283. mutex_unlock(mutexp);
  284. applog(LOG_ERR, "%s: Didn't recognise BitForce on %s returned: %s", bitforce->dev_repr, devpath, pdevbuf);
  285. return;
  286. }
  287. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  288. s[0] = '\0';
  289. free((void*)bitforce->name);
  290. bitforce->name = strdup(pdevbuf + 7);
  291. }
  292. *p_fdDev = fdDev;
  293. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  294. mutex_unlock(mutexp);
  295. }
  296. static void bitforce_flash_led(struct cgpu_info *bitforce)
  297. {
  298. struct bitforce_data *data = bitforce->cgpu_data;
  299. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  300. int fdDev = bitforce->device->device_fd;
  301. if (!fdDev)
  302. return;
  303. /* Do not try to flash the led if we're polling for a result to
  304. * minimise the chance of interleaved results */
  305. if (bitforce->polling)
  306. return;
  307. /* It is not critical flashing the led so don't get stuck if we
  308. * can't grab the mutex here */
  309. if (mutex_trylock(mutexp))
  310. return;
  311. char pdevbuf[0x100];
  312. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZMX");
  313. /* Once we've tried - don't do it until told to again */
  314. bitforce->flash_led = false;
  315. /* However, this stops anything else getting a reply
  316. * So best to delay any other access to the BFL */
  317. sleep(4);
  318. mutex_unlock(mutexp);
  319. return; // nothing is returned by the BFL
  320. }
  321. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  322. {
  323. struct bitforce_data *data = bitforce->cgpu_data;
  324. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  325. int fdDev = bitforce->device->device_fd;
  326. char pdevbuf[0x100];
  327. char *s;
  328. if (!fdDev)
  329. return false;
  330. /* Do not try to get the temperature if we're polling for a result to
  331. * minimise the chance of interleaved results */
  332. if (bitforce->polling)
  333. return true;
  334. // Flash instead of Temp - doing both can be too slow
  335. if (bitforce->flash_led) {
  336. bitforce_flash_led(bitforce);
  337. return true;
  338. }
  339. /* It is not critical getting temperature so don't get stuck if we
  340. * can't grab the mutex here */
  341. if (mutex_trylock(mutexp))
  342. return false;
  343. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZLX");
  344. mutex_unlock(mutexp);
  345. if (unlikely(!pdevbuf[0])) {
  346. applog(LOG_ERR, "%"PRIpreprv": Error: Get temp returned empty string/timed out", bitforce->proc_repr);
  347. bitforce->hw_errors++;
  348. ++hw_errors;
  349. return false;
  350. }
  351. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  352. float temp = strtof(s + 1, NULL);
  353. /* Cope with older software that breaks and reads nonsense
  354. * values */
  355. if (temp > 100)
  356. temp = strtod(s + 1, NULL);
  357. if (temp > 0) {
  358. bitforce->temp = temp;
  359. }
  360. } else {
  361. /* Use the temperature monitor as a kind of watchdog for when
  362. * our responses are out of sync and flush the buffer to
  363. * hopefully recover */
  364. applog(LOG_WARNING, "%"PRIpreprv": Garbled response probably throttling, clearing buffer", bitforce->proc_repr);
  365. dev_error(bitforce, REASON_DEV_THROTTLE);
  366. /* Count throttling episodes as hardware errors */
  367. bitforce->hw_errors++;
  368. ++hw_errors;
  369. bitforce_clear_buffer(bitforce);
  370. return false;
  371. }
  372. return true;
  373. }
  374. static inline
  375. void dbg_block_data(struct cgpu_info *bitforce)
  376. {
  377. if (!opt_debug)
  378. return;
  379. struct bitforce_data *data = bitforce->cgpu_data;
  380. char *s;
  381. s = bin2hex(&data->next_work_ob[8], 44);
  382. applog(LOG_DEBUG, "%"PRIpreprv": block data: %s", bitforce->proc_repr, s);
  383. free(s);
  384. }
  385. static void bitforce_change_mode(struct cgpu_info *, enum bitforce_proto);
  386. static
  387. bool bitforce_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  388. {
  389. struct cgpu_info *bitforce = thr->cgpu;
  390. struct bitforce_data *data = bitforce->cgpu_data;
  391. int fdDev = bitforce->device->device_fd;
  392. unsigned char *ob_ms = &data->next_work_ob[8];
  393. unsigned char *ob_dt = &ob_ms[32];
  394. // If polling job_start, cancel it
  395. if (data->poll_func == 1)
  396. {
  397. thr->tv_poll.tv_sec = -1;
  398. data->poll_func = 0;
  399. }
  400. memcpy(ob_ms, work->midstate, 32);
  401. memcpy(ob_dt, work->data + 64, 12);
  402. switch (data->proto)
  403. {
  404. case BFP_RANGE:
  405. {
  406. uint32_t *ob_nonce = (uint32_t*)&(ob_dt[32]);
  407. ob_nonce[0] = htobe32(work->blk.nonce);
  408. ob_nonce[1] = htobe32(work->blk.nonce + bitforce->nonces);
  409. // FIXME: if nonce range fails... we didn't increment enough
  410. work->blk.nonce += bitforce->nonces + 1;
  411. break;
  412. }
  413. case BFP_QUEUE:
  414. if (thr->work)
  415. {
  416. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  417. char pdevbuf[0x100];
  418. if (unlikely(!fdDev))
  419. return false;
  420. mutex_lock(mutexp);
  421. if (data->queued)
  422. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZQX");
  423. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, data->next_work_obs, data->next_work_obsz);
  424. mutex_unlock(mutexp);
  425. if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  426. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  427. bitforce_change_mode(bitforce, BFP_WORK);
  428. }
  429. else
  430. {
  431. dbg_block_data(bitforce);
  432. data->queued = true;
  433. }
  434. }
  435. // fallthru...
  436. case BFP_WORK:
  437. work->blk.nonce = 0xffffffff;
  438. }
  439. return true;
  440. }
  441. static
  442. void bitforce_change_mode(struct cgpu_info *bitforce, enum bitforce_proto proto)
  443. {
  444. struct bitforce_data *data = bitforce->cgpu_data;
  445. if (data->proto == proto)
  446. return;
  447. if (data->proto == BFP_RANGE)
  448. {
  449. bitforce->nonces = 0xffffffff;
  450. bitforce->sleep_ms *= 5;
  451. data->sleep_ms_default *= 5;
  452. switch (proto)
  453. {
  454. case BFP_WORK:
  455. data->next_work_cmd = "ZDX";
  456. break;
  457. case BFP_QUEUE:
  458. data->next_work_cmd = "ZNX";
  459. default:
  460. ;
  461. }
  462. if (data->sc)
  463. {
  464. // "S|---------- MidState ----------||-DataTail-|E"
  465. data->next_work_ob[7] = 45;
  466. data->next_work_ob[8+32+12] = '\xAA';
  467. data->next_work_obsz = 46;
  468. }
  469. else
  470. {
  471. // ">>>>>>>>|---------- MidState ----------||-DataTail-|>>>>>>>>"
  472. memset(&data->next_work_ob[8+32+12], '>', 8);
  473. data->next_work_obsz = 60;
  474. }
  475. }
  476. else
  477. if (proto == BFP_RANGE)
  478. {
  479. /* Split work up into 1/5th nonce ranges */
  480. bitforce->nonces = 0x33333332;
  481. bitforce->sleep_ms /= 5;
  482. data->sleep_ms_default /= 5;
  483. data->next_work_cmd = "ZPX";
  484. if (data->sc)
  485. {
  486. data->next_work_ob[7] = 53;
  487. data->next_work_obsz = 54;
  488. }
  489. else
  490. data->next_work_obsz = 68;
  491. }
  492. data->proto = proto;
  493. bitforce->kname = protonames[proto];
  494. }
  495. static
  496. void bitforce_job_start(struct thr_info *thr)
  497. {
  498. struct cgpu_info *bitforce = thr->cgpu;
  499. struct bitforce_data *data = bitforce->cgpu_data;
  500. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  501. int fdDev = bitforce->device->device_fd;
  502. unsigned char *ob = data->next_work_obs;
  503. char pdevbuf[0x100];
  504. struct timeval tv_now;
  505. data->result_busy_polled = 0;
  506. if (data->queued)
  507. {
  508. // get_results collected more accurate job start time
  509. mt_job_transition(thr);
  510. job_start_complete(thr);
  511. data->queued = false;
  512. timer_set_delay(&thr->tv_morework, &bitforce->work_start_tv, bitforce->sleep_ms * 1000);
  513. return;
  514. }
  515. if (!fdDev)
  516. goto commerr;
  517. re_send:
  518. mutex_lock(mutexp);
  519. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, ob, data->next_work_obsz);
  520. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  521. mutex_unlock(mutexp);
  522. gettimeofday(&tv_now, NULL);
  523. timer_set_delay(&thr->tv_poll, &tv_now, WORK_CHECK_INTERVAL_MS * 1000);
  524. data->poll_func = 1;
  525. return;
  526. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  527. mutex_unlock(mutexp);
  528. switch (data->proto)
  529. {
  530. case BFP_RANGE:
  531. applog(LOG_WARNING, "%"PRIpreprv": Does not support nonce range, disabling", bitforce->proc_repr);
  532. bitforce_change_mode(bitforce, BFP_WORK);
  533. goto re_send;
  534. case BFP_QUEUE:
  535. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  536. bitforce_change_mode(bitforce, BFP_WORK);
  537. goto re_send;
  538. default:
  539. ;
  540. }
  541. applog(LOG_ERR, "%"PRIpreprv": Error: Send work reports: %s", bitforce->proc_repr, pdevbuf);
  542. goto commerr;
  543. }
  544. mt_job_transition(thr);
  545. mutex_unlock(mutexp);
  546. dbg_block_data(bitforce);
  547. gettimeofday(&tv_now, NULL);
  548. bitforce->work_start_tv = tv_now;
  549. timer_set_delay(&thr->tv_morework, &tv_now, bitforce->sleep_ms * 1000);
  550. job_start_complete(thr);
  551. return;
  552. commerr:
  553. bitforce_comm_error(thr);
  554. job_start_abort(thr, true);
  555. }
  556. static char _discardedbuf[0x10];
  557. static
  558. void bitforce_job_get_results(struct thr_info *thr, struct work *work)
  559. {
  560. struct cgpu_info *bitforce = thr->cgpu;
  561. struct bitforce_data *data = bitforce->cgpu_data;
  562. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  563. int fdDev = bitforce->device->device_fd;
  564. unsigned int delay_time_ms;
  565. struct timeval elapsed;
  566. struct timeval now;
  567. char *pdevbuf = &data->noncebuf[0];
  568. bool stale;
  569. gettimeofday(&now, NULL);
  570. timersub(&now, &bitforce->work_start_tv, &elapsed);
  571. bitforce->wait_ms = tv_to_ms(elapsed);
  572. bitforce->polling = true;
  573. if (!fdDev)
  574. goto commerr;
  575. stale = stale_work(work, true);
  576. if (unlikely(bitforce->wait_ms < bitforce->sleep_ms))
  577. {
  578. // We're likely here because of a work restart
  579. // Since Bitforce cannot stop a work without losing results, only do it if the current job is finding stale shares
  580. // BFP_QUEUE does not support stopping work at all
  581. if (data->proto == BFP_QUEUE || !stale)
  582. {
  583. delay_time_ms = bitforce->sleep_ms - bitforce->wait_ms;
  584. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  585. data->poll_func = 2;
  586. return;
  587. }
  588. }
  589. while (1) {
  590. const char *cmd = (data->proto == BFP_QUEUE) ? "ZOX" : "ZFX";
  591. int count;
  592. mutex_lock(mutexp);
  593. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(data->noncebuf), cmd);
  594. if (!strncasecmp(pdevbuf, "COUNT:", 6))
  595. {
  596. count = atoi(&pdevbuf[6]);
  597. size_t cls = strlen(pdevbuf);
  598. char *pmorebuf = &pdevbuf[cls];
  599. size_t szleft = sizeof(data->noncebuf) - cls, sz;
  600. if (count && data->queued)
  601. {
  602. gettimeofday(&now, NULL);
  603. bitforce->work_start_tv = now;
  604. }
  605. while (true)
  606. {
  607. BFgets(pmorebuf, szleft, fdDev);
  608. if (!strncasecmp(pmorebuf, "OK", 2))
  609. break;
  610. sz = strlen(pmorebuf);
  611. szleft -= sz;
  612. pmorebuf += sz;
  613. if (unlikely(!szleft))
  614. {
  615. // Out of buffer space somehow :(
  616. applog(LOG_DEBUG, "%"PRIpreprv": Ran out of buffer space for results, discarding extra data", bitforce->proc_repr);
  617. pmorebuf = _discardedbuf;
  618. szleft = sizeof(_discardedbuf);
  619. }
  620. }
  621. }
  622. else
  623. count = -1;
  624. mutex_unlock(mutexp);
  625. gettimeofday(&now, NULL);
  626. if (!count)
  627. goto noqr;
  628. timersub(&now, &bitforce->work_start_tv, &elapsed);
  629. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  630. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  631. tv_to_ms(elapsed), (unsigned long)BITFORCE_LONG_TIMEOUT_MS);
  632. goto out;
  633. }
  634. if (count > 0)
  635. {
  636. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  637. goto out;
  638. }
  639. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  640. break;
  641. data->result_busy_polled = bitforce->wait_ms;
  642. if (stale && data->proto != BFP_QUEUE)
  643. {
  644. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  645. bitforce->proc_repr);
  646. goto out;
  647. }
  648. noqr:
  649. /* if BFL is throttling, no point checking so quickly */
  650. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  651. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  652. data->poll_func = 2;
  653. return;
  654. }
  655. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  656. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  657. tv_to_ms(elapsed), (unsigned long)BITFORCE_TIMEOUT_MS);
  658. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  659. ++bitforce->hw_errors;
  660. ++hw_errors;
  661. /* If the device truly throttled, it didn't process the job and there
  662. * are no results. But check first, just in case we're wrong about it
  663. * throttling.
  664. */
  665. if (strncasecmp(pdevbuf, "NONCE-FOUND", 11))
  666. goto out;
  667. } else if (!strncasecmp(pdevbuf, "N", 1)) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  668. /* Simple timing adjustment. Allow a few polls to cope with
  669. * OS timer delays being variably reliable. wait_ms will
  670. * always equal sleep_ms when we've waited greater than or
  671. * equal to the result return time.*/
  672. delay_time_ms = bitforce->sleep_ms;
  673. if (!data->result_busy_polled)
  674. {
  675. // No busy polls before results received
  676. if (bitforce->wait_ms > delay_time_ms + (WORK_CHECK_INTERVAL_MS * 8))
  677. // ... due to poll being rather late; ignore it as an anomaly
  678. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, later than scheduled %ums (ignoring)",
  679. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms);
  680. else
  681. if (bitforce->sleep_ms > data->sleep_ms_default + (BITFORCE_CHECK_INTERVAL_MS * 0x20))
  682. {
  683. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on delayed schedule %ums; Wait time changed to: %ums (default sch)",
  684. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, data->sleep_ms_default);
  685. bitforce->sleep_ms = data->sleep_ms_default;
  686. }
  687. else
  688. {
  689. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on default schedule %ums; Wait time changed to: %ums (check interval)",
  690. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, BITFORCE_CHECK_INTERVAL_MS);
  691. bitforce->sleep_ms = BITFORCE_CHECK_INTERVAL_MS;
  692. }
  693. }
  694. else
  695. {
  696. if (data->result_busy_polled - bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  697. {
  698. bitforce->sleep_ms = data->result_busy_polled - (WORK_CHECK_INTERVAL_MS / 2);
  699. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time changed to: %ums",
  700. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms, bitforce->sleep_ms);
  701. }
  702. else
  703. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time unchanged",
  704. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms);
  705. }
  706. /* Work out the average time taken. Float for calculation, uint for display */
  707. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  708. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  709. }
  710. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  711. if (strncasecmp(pdevbuf, "NONCE-FOUND", 11) && (pdevbuf[2] != '-') && strncasecmp(pdevbuf, "I", 1)) {
  712. bitforce->hw_errors++;
  713. ++hw_errors;
  714. applog(LOG_WARNING, "%"PRIpreprv": Error: Get result reports: %s", bitforce->proc_repr, pdevbuf);
  715. bitforce_clear_buffer(bitforce);
  716. }
  717. out:
  718. bitforce->polling = false;
  719. job_results_fetched(thr);
  720. return;
  721. commerr:
  722. bitforce_comm_error(thr);
  723. goto out;
  724. }
  725. static
  726. void bitforce_process_result_nonces(struct thr_info *thr, struct work *work, char *pnoncebuf)
  727. {
  728. struct cgpu_info *bitforce = thr->cgpu;
  729. struct bitforce_data *data = bitforce->cgpu_data;
  730. uint32_t nonce;
  731. while (1) {
  732. hex2bin((void*)&nonce, pnoncebuf, 4);
  733. nonce = be32toh(nonce);
  734. if (unlikely(data->proto == BFP_RANGE && (nonce >= work->blk.nonce ||
  735. /* FIXME: blk.nonce is probably moved on quite a bit now! */
  736. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  737. applog(LOG_WARNING, "%"PRIpreprv": Disabling broken nonce range support", bitforce->proc_repr);
  738. bitforce_change_mode(bitforce, BFP_WORK);
  739. }
  740. submit_nonce(thr, work, nonce);
  741. if (strncmp(&pnoncebuf[8], ",", 1))
  742. break;
  743. pnoncebuf += 9;
  744. }
  745. }
  746. static
  747. bool bitforce_process_qresult_line_i(struct thr_info *thr, char *midstate, char *datatail, char *buf, struct work *work)
  748. {
  749. if (!work)
  750. return false;
  751. if (memcmp(work->midstate, midstate, 32))
  752. return false;
  753. if (memcmp(&work->data[64], datatail, 12))
  754. return false;
  755. if (!atoi(&buf[90]))
  756. return true;
  757. bitforce_process_result_nonces(thr, work, &buf[92]);
  758. return true;
  759. }
  760. static
  761. void bitforce_process_qresult_line(struct thr_info *thr, char *buf, struct work *work)
  762. {
  763. struct cgpu_info *bitforce = thr->cgpu;
  764. char midstate[32], datatail[12];
  765. hex2bin((void*)midstate, buf, 32);
  766. hex2bin((void*)datatail, &buf[65], 12);
  767. if (!( bitforce_process_qresult_line_i(thr, midstate, datatail, buf, work)
  768. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->work)
  769. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->prev_work)
  770. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->next_work) ))
  771. {
  772. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queued results", bitforce->proc_repr);
  773. ++bitforce->hw_errors;
  774. ++hw_errors;
  775. }
  776. }
  777. static inline
  778. char *next_line(char *in)
  779. {
  780. while (in[0] && in[0] != '\n')
  781. ++in;
  782. return in;
  783. }
  784. static
  785. int64_t bitforce_job_process_results(struct thr_info *thr, struct work *work, __maybe_unused bool stopping)
  786. {
  787. struct cgpu_info *bitforce = thr->cgpu;
  788. struct bitforce_data *data = bitforce->cgpu_data;
  789. char *pnoncebuf = &data->noncebuf[0];
  790. int count;
  791. if (!strncasecmp(pnoncebuf, "NO-", 3))
  792. return bitforce->nonces; /* No valid nonce found */
  793. if (!strncasecmp(pnoncebuf, "NONCE-FOUND", 11))
  794. {
  795. bitforce_process_result_nonces(thr, work, &pnoncebuf[12]);
  796. count = 1;
  797. }
  798. else
  799. if (!strncasecmp(pnoncebuf, "COUNT:", 6))
  800. {
  801. count = 0;
  802. pnoncebuf = next_line(pnoncebuf);
  803. while (pnoncebuf[0])
  804. {
  805. bitforce_process_qresult_line(thr, pnoncebuf, work);
  806. ++count;
  807. pnoncebuf = next_line(pnoncebuf);
  808. }
  809. }
  810. else
  811. return 0;
  812. // FIXME: This might have changed in the meantime (new job start, or broken)
  813. return bitforce->nonces * count;
  814. }
  815. static void bitforce_shutdown(struct thr_info *thr)
  816. {
  817. struct cgpu_info *bitforce = thr->cgpu;
  818. int *p_fdDev = &bitforce->device->device_fd;
  819. BFclose(*p_fdDev);
  820. *p_fdDev = 0;
  821. }
  822. static void biforce_thread_enable(struct thr_info *thr)
  823. {
  824. struct cgpu_info *bitforce = thr->cgpu;
  825. bitforce_init(bitforce);
  826. }
  827. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  828. {
  829. return bitforce_get_temp(bitforce);
  830. }
  831. static bool bitforce_identify(struct cgpu_info *bitforce)
  832. {
  833. bitforce->flash_led = true;
  834. return true;
  835. }
  836. static bool bitforce_thread_init(struct thr_info *thr)
  837. {
  838. struct cgpu_info *bitforce = thr->cgpu;
  839. unsigned int wait;
  840. struct bitforce_data *data;
  841. struct bitforce_init_data *initdata = bitforce->cgpu_data;
  842. bool sc = initdata->sc;
  843. int xlink_id = 0;
  844. for ( ; bitforce; bitforce = bitforce->next_proc)
  845. {
  846. if (unlikely(xlink_id > 30))
  847. {
  848. applog(LOG_ERR, "%"PRIpreprv": Failed to find XLINK address", bitforce->proc_repr);
  849. dev_error(bitforce, REASON_THREAD_FAIL_INIT);
  850. bitforce->reinit_backoff = 1e10;
  851. continue;
  852. }
  853. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  854. bitforce->cgpu_data = data = malloc(sizeof(*data));
  855. *data = (struct bitforce_data){
  856. .xlink_id = xlink_id,
  857. .next_work_ob = ">>>>>>>>|---------- MidState ----------||-DataTail-||Nonces|>>>>>>>>",
  858. .proto = BFP_RANGE,
  859. .sc = sc,
  860. .sleep_ms_default = BITFORCE_SLEEP_MS,
  861. };
  862. if (sc)
  863. {
  864. // ".......S|---------- MidState ----------||-DataTail-||Nonces|E"
  865. data->next_work_ob[8+32+12+8] = '\xAA';
  866. data->next_work_obs = &data->next_work_ob[7];
  867. bitforce_change_mode(bitforce, BFP_QUEUE);
  868. }
  869. else
  870. {
  871. data->next_work_obs = &data->next_work_ob[0];
  872. // Unconditionally change away from cold-initialized BFP_RANGE, to allow for setting up other variables
  873. bitforce_change_mode(bitforce, BFP_WORK);
  874. /* Initially enable support for nonce range and disable it later if it
  875. * fails */
  876. if (opt_bfl_noncerange)
  877. bitforce_change_mode(bitforce, BFP_RANGE);
  878. }
  879. while (xlink_id < 31 && !(initdata->devmask & (1 << ++xlink_id)))
  880. {}
  881. }
  882. bitforce = thr->cgpu;
  883. /* Pause each new thread at least 100ms between initialising
  884. * so the devices aren't making calls all at the same time. */
  885. wait = thr->id * MAX_START_DELAY_MS;
  886. applog(LOG_DEBUG, "%s: Delaying start by %dms", bitforce->dev_repr, wait / 1000);
  887. nmsleep(wait);
  888. return true;
  889. }
  890. static struct api_data *bitforce_api_stats(struct cgpu_info *cgpu)
  891. {
  892. struct api_data *root = NULL;
  893. // Warning, access to these is not locked - but we don't really
  894. // care since hashing performance is way more important than
  895. // locking access to displaying API debug 'stats'
  896. // If locking becomes an issue for any of them, use copy_data=true also
  897. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  898. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  899. return root;
  900. }
  901. void bitforce_poll(struct thr_info *thr)
  902. {
  903. struct cgpu_info *bitforce = thr->cgpu;
  904. struct bitforce_data *data = bitforce->cgpu_data;
  905. int poll = data->poll_func;
  906. thr->tv_poll.tv_sec = -1;
  907. data->poll_func = 0;
  908. switch (poll)
  909. {
  910. case 1:
  911. bitforce_job_start(thr);
  912. break;
  913. case 2:
  914. bitforce_job_get_results(thr, thr->work);
  915. break;
  916. default:
  917. applog(LOG_ERR, "%"PRIpreprv": Unexpected poll from device API!", thr->cgpu->proc_repr);
  918. }
  919. }
  920. struct device_api bitforce_api = {
  921. .dname = "bitforce",
  922. .name = "BFL",
  923. .api_detect = bitforce_detect,
  924. .get_api_stats = bitforce_api_stats,
  925. .minerloop = minerloop_async,
  926. .reinit_device = bitforce_init,
  927. .get_statline_before = get_bitforce_statline_before,
  928. .get_stats = bitforce_get_stats,
  929. .identify_device = bitforce_identify,
  930. .thread_prepare = bitforce_thread_prepare,
  931. .thread_init = bitforce_thread_init,
  932. .job_prepare = bitforce_job_prepare,
  933. .job_start = bitforce_job_start,
  934. .job_get_results = bitforce_job_get_results,
  935. .poll = bitforce_poll,
  936. .job_process_results = bitforce_job_process_results,
  937. .thread_shutdown = bitforce_shutdown,
  938. .thread_enable = biforce_thread_enable
  939. };