driver-bitforce.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * Copyright 2012 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 <limits.h>
  11. #include <pthread.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <strings.h>
  15. #include <sys/time.h>
  16. #include <unistd.h>
  17. #include "config.h"
  18. #include "compat.h"
  19. #include "miner.h"
  20. #include "fpgautils.h"
  21. #define BITFORCE_SLEEP_MS 500
  22. #define BITFORCE_TIMEOUT_S 7
  23. #define BITFORCE_TIMEOUT_MS (BITFORCE_TIMEOUT_S * 1000)
  24. #define BITFORCE_LONG_TIMEOUT_S 25
  25. #define BITFORCE_LONG_TIMEOUT_MS (BITFORCE_LONG_TIMEOUT_S * 1000)
  26. #define BITFORCE_CHECK_INTERVAL_MS 10
  27. #define WORK_CHECK_INTERVAL_MS 50
  28. #define MAX_START_DELAY_US 100000
  29. #define tv_to_ms(tval) (tval.tv_sec * 1000 + tval.tv_usec / 1000)
  30. #define TIME_AVG_CONSTANT 8
  31. #define KNAME_WORK "full work"
  32. #define KNAME_RANGE "nonce range"
  33. struct device_api bitforce_api;
  34. // Code must deal with a timeout
  35. #define BFopen(devpath) serial_open(devpath, 0, 250, true)
  36. static void BFgets(char *buf, size_t bufLen, int fd)
  37. {
  38. do {
  39. buf[0] = '\0';
  40. --bufLen;
  41. } while (likely(bufLen && read(fd, buf, 1) == 1 && (buf++)[0] != '\n'));
  42. buf[0] = '\0';
  43. }
  44. static ssize_t BFwrite(int fd, const void *buf, ssize_t bufLen)
  45. {
  46. if ((bufLen) != write(fd, buf, bufLen))
  47. return 0;
  48. else
  49. return bufLen;
  50. }
  51. #define BFclose(fd) close(fd)
  52. static bool bitforce_detect_one(const char *devpath)
  53. {
  54. int fdDev = serial_open(devpath, 0, 10, true);
  55. struct cgpu_info *bitforce;
  56. char pdevbuf[0x100];
  57. char *s;
  58. applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
  59. if (unlikely(fdDev == -1)) {
  60. applog(LOG_DEBUG, "BFL: Failed to open %s", devpath);
  61. return false;
  62. }
  63. BFwrite(fdDev, "ZGX", 3);
  64. pdevbuf[0] = '\0';
  65. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  66. if (unlikely(!pdevbuf[0])) {
  67. applog(LOG_DEBUG, "BFL: Error reading/timeout (ZGX)");
  68. return 0;
  69. }
  70. BFclose(fdDev);
  71. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  72. applog(LOG_DEBUG, "BFL: Didn't recognise BitForce on %s", devpath);
  73. return false;
  74. }
  75. // We have a real BitForce!
  76. bitforce = calloc(1, sizeof(*bitforce));
  77. bitforce->api = &bitforce_api;
  78. bitforce->device_path = strdup(devpath);
  79. bitforce->deven = DEV_ENABLED;
  80. bitforce->threads = 1;
  81. /* Initially enable support for nonce range and disable it later if it
  82. * fails */
  83. if (opt_bfl_noncerange) {
  84. bitforce->nonce_range = true;
  85. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  86. bitforce->kname = KNAME_RANGE;
  87. } else {
  88. bitforce->sleep_ms = BITFORCE_SLEEP_MS * 5;
  89. bitforce->kname = KNAME_WORK;
  90. }
  91. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  92. s[0] = '\0';
  93. bitforce->name = strdup(pdevbuf + 7);
  94. }
  95. mutex_init(&bitforce->device_mutex);
  96. return add_cgpu(bitforce);
  97. }
  98. static int bitforce_detect_auto(void)
  99. {
  100. return (serial_autodetect_udev (bitforce_detect_one, "BitFORCE*SHA256") ?:
  101. serial_autodetect_devserial(bitforce_detect_one, "BitFORCE_SHA256") ?:
  102. serial_autodetect_ftdi(bitforce_detect_one, "BitFORCE", "SHA256") ?:
  103. 0);
  104. }
  105. static void bitforce_detect(void)
  106. {
  107. serial_detect_auto(&bitforce_api, bitforce_detect_one, bitforce_detect_auto);
  108. }
  109. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  110. {
  111. float gt = bitforce->temp;
  112. if (gt > 0)
  113. tailsprintf(buf, "%5.1fC ", gt);
  114. else
  115. tailsprintf(buf, " ", gt);
  116. tailsprintf(buf, " | ");
  117. }
  118. static bool bitforce_thread_prepare(struct thr_info *thr)
  119. {
  120. struct cgpu_info *bitforce = thr->cgpu;
  121. int fdDev = BFopen(bitforce->device_path);
  122. struct timeval now;
  123. if (unlikely(fdDev == -1)) {
  124. applog(LOG_ERR, "BFL%i: Failed to open %s", bitforce->device_id, bitforce->device_path);
  125. return false;
  126. }
  127. bitforce->device_fd = fdDev;
  128. applog(LOG_INFO, "BFL%i: Opened %s", bitforce->device_id, bitforce->device_path);
  129. gettimeofday(&now, NULL);
  130. get_datestamp(bitforce->init, &now);
  131. return true;
  132. }
  133. static void bitforce_clear_buffer(struct cgpu_info *bitforce)
  134. {
  135. int fdDev = bitforce->device_fd;
  136. char pdevbuf[0x100];
  137. int count = 0;
  138. if (!fdDev)
  139. return;
  140. applog(LOG_DEBUG, "BFL%i: Clearing read buffer", bitforce->device_id);
  141. mutex_lock(&bitforce->device_mutex);
  142. do {
  143. pdevbuf[0] = '\0';
  144. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  145. } while (pdevbuf[0] && (++count < 10));
  146. mutex_unlock(&bitforce->device_mutex);
  147. }
  148. void bitforce_init(struct cgpu_info *bitforce)
  149. {
  150. const char *devpath = bitforce->device_path;
  151. int fdDev = bitforce->device_fd, retries = 0;
  152. char pdevbuf[0x100];
  153. char *s;
  154. applog(LOG_WARNING, "BFL%i: Re-initialising", bitforce->device_id);
  155. bitforce_clear_buffer(bitforce);
  156. mutex_lock(&bitforce->device_mutex);
  157. if (fdDev) {
  158. BFclose(fdDev);
  159. sleep(5);
  160. }
  161. bitforce->device_fd = 0;
  162. fdDev = BFopen(devpath);
  163. if (unlikely(fdDev == -1)) {
  164. mutex_unlock(&bitforce->device_mutex);
  165. applog(LOG_ERR, "BFL%i: Failed to open %s", bitforce->device_id, devpath);
  166. return;
  167. }
  168. do {
  169. BFwrite(fdDev, "ZGX", 3);
  170. pdevbuf[0] = '\0';
  171. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  172. if (unlikely(!pdevbuf[0])) {
  173. mutex_unlock(&bitforce->device_mutex);
  174. applog(LOG_ERR, "BFL%i: Error reading/timeout (ZGX)", bitforce->device_id);
  175. return;
  176. }
  177. if (retries++)
  178. nmsleep(10);
  179. } while (!strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  180. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  181. mutex_unlock(&bitforce->device_mutex);
  182. applog(LOG_ERR, "BFL%i: Didn't recognise BitForce on %s returned: %s", bitforce->device_id, devpath, pdevbuf);
  183. return;
  184. }
  185. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  186. s[0] = '\0';
  187. bitforce->name = strdup(pdevbuf + 7);
  188. }
  189. bitforce->device_fd = fdDev;
  190. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  191. mutex_unlock(&bitforce->device_mutex);
  192. }
  193. static void bitforce_flash_led(struct cgpu_info *bitforce)
  194. {
  195. int fdDev = bitforce->device_fd;
  196. if (!fdDev)
  197. return;
  198. /* Do not try to flash the led if we're polling for a result to
  199. * minimise the chance of interleaved results */
  200. if (bitforce->polling)
  201. return;
  202. /* It is not critical flashing the led so don't get stuck if we
  203. * can't grab the mutex here */
  204. if (mutex_trylock(&bitforce->device_mutex))
  205. return;
  206. BFwrite(fdDev, "ZMX", 3);
  207. /* Once we've tried - don't do it until told to again */
  208. bitforce->flash_led = false;
  209. /* However, this stops anything else getting a reply
  210. * So best to delay any other access to the BFL */
  211. sleep(4);
  212. mutex_unlock(&bitforce->device_mutex);
  213. return; // nothing is returned by the BFL
  214. }
  215. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  216. {
  217. int fdDev = bitforce->device_fd;
  218. char pdevbuf[0x100];
  219. char *s;
  220. if (!fdDev)
  221. return false;
  222. /* Do not try to get the temperature if we're polling for a result to
  223. * minimise the chance of interleaved results */
  224. if (bitforce->polling)
  225. return true;
  226. // Flash instead of Temp - doing both can be too slow
  227. if (bitforce->flash_led) {
  228. bitforce_flash_led(bitforce);
  229. return true;
  230. }
  231. /* It is not critical getting temperature so don't get stuck if we
  232. * can't grab the mutex here */
  233. if (mutex_trylock(&bitforce->device_mutex))
  234. return false;
  235. BFwrite(fdDev, "ZLX", 3);
  236. pdevbuf[0] = '\0';
  237. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  238. mutex_unlock(&bitforce->device_mutex);
  239. if (unlikely(!pdevbuf[0])) {
  240. applog(LOG_ERR, "BFL%i: Error: Get temp returned empty string/timed out", bitforce->device_id);
  241. bitforce->hw_errors++;
  242. return false;
  243. }
  244. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  245. float temp = strtof(s + 1, NULL);
  246. if (temp > 0) {
  247. bitforce->temp = temp;
  248. if (unlikely(bitforce->cutofftemp > 0 && temp > bitforce->cutofftemp)) {
  249. applog(LOG_WARNING, "BFL%i: Hit thermal cutoff limit, disabling!", bitforce->device_id);
  250. bitforce->deven = DEV_RECOVER;
  251. bitforce->device_last_not_well = time(NULL);
  252. bitforce->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  253. bitforce->dev_thermal_cutoff_count++;
  254. }
  255. }
  256. } else {
  257. /* Use the temperature monitor as a kind of watchdog for when
  258. * our responses are out of sync and flush the buffer to
  259. * hopefully recover */
  260. applog(LOG_WARNING, "BFL%i: Garbled response probably throttling, clearing buffer", bitforce->device_id);
  261. bitforce->device_last_not_well = time(NULL);
  262. bitforce->device_not_well_reason = REASON_DEV_THROTTLE;
  263. bitforce->dev_throttle_count++;
  264. /* Count throttling episodes as hardware errors */
  265. bitforce->hw_errors++;
  266. bitforce_clear_buffer(bitforce);
  267. return false;
  268. }
  269. return true;
  270. }
  271. static bool bitforce_send_work(struct thr_info *thr, struct work *work)
  272. {
  273. struct cgpu_info *bitforce = thr->cgpu;
  274. int fdDev = bitforce->device_fd;
  275. unsigned char ob[70];
  276. char pdevbuf[0x100];
  277. char *s;
  278. if (!fdDev)
  279. return false;
  280. re_send:
  281. mutex_lock(&bitforce->device_mutex);
  282. if (bitforce->nonce_range)
  283. BFwrite(fdDev, "ZPX", 3);
  284. else
  285. BFwrite(fdDev, "ZDX", 3);
  286. pdevbuf[0] = '\0';
  287. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  288. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  289. mutex_unlock(&bitforce->device_mutex);
  290. if (!restart_wait(WORK_CHECK_INTERVAL_MS))
  291. return false;
  292. goto re_send;
  293. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  294. mutex_unlock(&bitforce->device_mutex);
  295. if (bitforce->nonce_range) {
  296. applog(LOG_WARNING, "BFL%i: Does not support nonce range, disabling", bitforce->device_id);
  297. bitforce->nonce_range = false;
  298. bitforce->sleep_ms *= 5;
  299. bitforce->kname = KNAME_WORK;
  300. goto re_send;
  301. }
  302. applog(LOG_ERR, "BFL%i: Error: Send work reports: %s", bitforce->device_id, pdevbuf);
  303. return false;
  304. }
  305. sprintf((char *)ob, ">>>>>>>>");
  306. memcpy(ob + 8, work->midstate, 32);
  307. memcpy(ob + 8 + 32, work->data + 64, 12);
  308. if (!bitforce->nonce_range) {
  309. sprintf((char *)ob + 8 + 32 + 12, ">>>>>>>>");
  310. work->blk.nonce = bitforce->nonces = 0xffffffff;
  311. BFwrite(fdDev, ob, 60);
  312. } else {
  313. uint32_t *nonce;
  314. nonce = (uint32_t *)(ob + 8 + 32 + 12);
  315. *nonce = htobe32(work->blk.nonce);
  316. nonce = (uint32_t *)(ob + 8 + 32 + 12 + 4);
  317. /* Split work up into 1/5th nonce ranges */
  318. bitforce->nonces = 0x33333332;
  319. *nonce = htobe32(work->blk.nonce + bitforce->nonces);
  320. work->blk.nonce += bitforce->nonces + 1;
  321. sprintf((char *)ob + 8 + 32 + 12 + 8, ">>>>>>>>");
  322. BFwrite(fdDev, ob, 68);
  323. }
  324. pdevbuf[0] = '\0';
  325. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  326. mutex_unlock(&bitforce->device_mutex);
  327. if (opt_debug) {
  328. s = bin2hex(ob + 8, 44);
  329. applog(LOG_DEBUG, "BFL%i: block data: %s", bitforce->device_id, s);
  330. free(s);
  331. }
  332. if (unlikely(!pdevbuf[0])) {
  333. applog(LOG_ERR, "BFL%i: Error: Send block data returned empty string/timed out", bitforce->device_id);
  334. return false;
  335. }
  336. if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  337. applog(LOG_ERR, "BFL%i: Error: Send block data reports: %s", bitforce->device_id, pdevbuf);
  338. return false;
  339. }
  340. gettimeofday(&bitforce->work_start_tv, NULL);
  341. return true;
  342. }
  343. static inline int noisy_stale_wait(unsigned int mstime, struct work*work, bool checkend, struct cgpu_info*bitforce)
  344. {
  345. int rv = stale_wait(mstime, work, checkend);
  346. if (rv)
  347. applog(LOG_NOTICE, "BFL%i: Abandoning stale search to restart",
  348. bitforce->device_id);
  349. return rv;
  350. }
  351. #define noisy_stale_wait(mstime, work, checkend) noisy_stale_wait(mstime, work, checkend, bitforce)
  352. static int64_t bitforce_get_result(struct thr_info *thr, struct work *work)
  353. {
  354. struct cgpu_info *bitforce = thr->cgpu;
  355. int fdDev = bitforce->device_fd;
  356. unsigned int delay_time_ms;
  357. struct timeval elapsed;
  358. struct timeval now;
  359. char pdevbuf[0x100];
  360. char *pnoncebuf;
  361. uint32_t nonce;
  362. if (!fdDev)
  363. return -1;
  364. while (1) {
  365. mutex_lock(&bitforce->device_mutex);
  366. BFwrite(fdDev, "ZFX", 3);
  367. pdevbuf[0] = '\0';
  368. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  369. mutex_unlock(&bitforce->device_mutex);
  370. gettimeofday(&now, NULL);
  371. timersub(&now, &bitforce->work_start_tv, &elapsed);
  372. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  373. applog(LOG_ERR, "BFL%i: took %dms - longer than %dms", bitforce->device_id,
  374. tv_to_ms(elapsed), BITFORCE_LONG_TIMEOUT_MS);
  375. return 0;
  376. }
  377. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  378. break;
  379. /* if BFL is throttling, no point checking so quickly */
  380. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  381. if (noisy_stale_wait(delay_time_ms, work, true))
  382. return 0;
  383. bitforce->wait_ms += delay_time_ms;
  384. }
  385. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  386. applog(LOG_ERR, "BFL%i: took %dms - longer than %dms", bitforce->device_id,
  387. tv_to_ms(elapsed), BITFORCE_TIMEOUT_MS);
  388. bitforce->device_last_not_well = time(NULL);
  389. bitforce->device_not_well_reason = REASON_DEV_OVER_HEAT;
  390. bitforce->dev_over_heat_count++;
  391. /* If the device truly throttled, it didn't process the job and there
  392. * are no results. But check first, just in case we're wrong about it
  393. * throttling.
  394. */
  395. if (strncasecmp(pdevbuf, "NONCE-FOUND", 11))
  396. return 0;
  397. } else if (!strncasecmp(pdevbuf, "N", 1)) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  398. /* Simple timing adjustment. Allow a few polls to cope with
  399. * OS timer delays being variably reliable. wait_ms will
  400. * always equal sleep_ms when we've waited greater than or
  401. * equal to the result return time.*/
  402. delay_time_ms = bitforce->sleep_ms;
  403. if (bitforce->wait_ms > bitforce->sleep_ms + (WORK_CHECK_INTERVAL_MS * 2))
  404. bitforce->sleep_ms += (bitforce->wait_ms - bitforce->sleep_ms) / 2;
  405. else if (bitforce->wait_ms == bitforce->sleep_ms) {
  406. if (bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  407. bitforce->sleep_ms -= WORK_CHECK_INTERVAL_MS;
  408. else if (bitforce->sleep_ms > BITFORCE_CHECK_INTERVAL_MS)
  409. bitforce->sleep_ms -= BITFORCE_CHECK_INTERVAL_MS;
  410. }
  411. if (delay_time_ms != bitforce->sleep_ms)
  412. applog(LOG_DEBUG, "BFL%i: Wait time changed to: %d, waited %u", bitforce->device_id, bitforce->sleep_ms, bitforce->wait_ms);
  413. /* Work out the average time taken. Float for calculation, uint for display */
  414. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  415. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  416. }
  417. applog(LOG_DEBUG, "BFL%i: waited %dms until %s", bitforce->device_id, bitforce->wait_ms, pdevbuf);
  418. if (!strncasecmp(&pdevbuf[2], "-", 1))
  419. return bitforce->nonces; /* No valid nonce found */
  420. else if (!strncasecmp(pdevbuf, "I", 1))
  421. return 0; /* Device idle */
  422. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  423. bitforce->hw_errors++;
  424. applog(LOG_WARNING, "BFL%i: Error: Get result reports: %s", bitforce->device_id, pdevbuf);
  425. bitforce_clear_buffer(bitforce);
  426. return 0;
  427. }
  428. pnoncebuf = &pdevbuf[12];
  429. while (1) {
  430. hex2bin((void*)&nonce, pnoncebuf, 4);
  431. nonce = be32toh(nonce);
  432. if (unlikely(bitforce->nonce_range && (nonce >= work->blk.nonce ||
  433. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  434. applog(LOG_WARNING, "BFL%i: Disabling broken nonce range support", bitforce->device_id);
  435. bitforce->nonce_range = false;
  436. work->blk.nonce = 0xffffffff;
  437. bitforce->sleep_ms *= 5;
  438. bitforce->kname = KNAME_WORK;
  439. }
  440. submit_nonce(thr, work, nonce);
  441. if (strncmp(&pnoncebuf[8], ",", 1))
  442. break;
  443. pnoncebuf += 9;
  444. }
  445. return bitforce->nonces;
  446. }
  447. static void bitforce_shutdown(struct thr_info *thr)
  448. {
  449. struct cgpu_info *bitforce = thr->cgpu;
  450. BFclose(bitforce->device_fd);
  451. bitforce->device_fd = 0;
  452. }
  453. static void biforce_thread_enable(struct thr_info *thr)
  454. {
  455. struct cgpu_info *bitforce = thr->cgpu;
  456. bitforce_init(bitforce);
  457. }
  458. static int64_t bitforce_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  459. {
  460. struct cgpu_info *bitforce = thr->cgpu;
  461. int64_t ret;
  462. if (!bitforce_send_work(thr, work)) {
  463. if (thr->work_restart)
  464. return 0;
  465. sleep(opt_fail_pause);
  466. goto commerr;
  467. }
  468. if (noisy_stale_wait(bitforce->sleep_ms, work, true))
  469. return 0;
  470. bitforce->wait_ms = bitforce->sleep_ms;
  471. {
  472. bitforce->polling = true;
  473. ret = bitforce_get_result(thr, work);
  474. bitforce->polling = false;
  475. }
  476. if (ret == -1) {
  477. commerr:
  478. ret = 0;
  479. applog(LOG_ERR, "BFL%i: Comms error", bitforce->device_id);
  480. bitforce->device_last_not_well = time(NULL);
  481. bitforce->device_not_well_reason = REASON_DEV_COMMS_ERROR;
  482. bitforce->dev_comms_error_count++;
  483. bitforce->hw_errors++;
  484. BFclose(bitforce->device_fd);
  485. int fd = bitforce->device_fd = BFopen(bitforce->device_path);
  486. if (fd == -1) {
  487. applog(LOG_ERR, "BFL%i: Error reopening");
  488. return -1;
  489. }
  490. /* empty read buffer */
  491. bitforce_clear_buffer(bitforce);
  492. }
  493. return ret;
  494. }
  495. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  496. {
  497. return bitforce_get_temp(bitforce);
  498. }
  499. static bool bitforce_identify(struct cgpu_info *bitforce)
  500. {
  501. bitforce->flash_led = true;
  502. return true;
  503. }
  504. static bool bitforce_thread_init(struct thr_info *thr)
  505. {
  506. struct cgpu_info *bitforce = thr->cgpu;
  507. unsigned int wait;
  508. /* Pause each new thread at least 100ms between initialising
  509. * so the devices aren't making calls all at the same time. */
  510. wait = thr->id * MAX_START_DELAY_US;
  511. applog(LOG_DEBUG, "BFL%i: Delaying start by %dms", bitforce->device_id, wait / 1000);
  512. usleep(wait);
  513. return true;
  514. }
  515. static struct api_data *bitforce_api_stats(struct cgpu_info *cgpu)
  516. {
  517. struct api_data *root = NULL;
  518. // Warning, access to these is not locked - but we don't really
  519. // care since hashing performance is way more important than
  520. // locking access to displaying API debug 'stats'
  521. // If locking becomes an issue for any of them, use copy_data=true also
  522. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  523. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  524. return root;
  525. }
  526. struct device_api bitforce_api = {
  527. .dname = "bitforce",
  528. .name = "BFL",
  529. .api_detect = bitforce_detect,
  530. .get_api_stats = bitforce_api_stats,
  531. .reinit_device = bitforce_init,
  532. .get_statline_before = get_bitforce_statline_before,
  533. .get_stats = bitforce_get_stats,
  534. .identify_device = bitforce_identify,
  535. .thread_prepare = bitforce_thread_prepare,
  536. .thread_init = bitforce_thread_init,
  537. .scanhash = bitforce_scanhash,
  538. .thread_shutdown = bitforce_shutdown,
  539. .thread_enable = biforce_thread_enable
  540. };