driver-bitforce.c 18 KB

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