driver-bitforce.c 28 KB

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