driver-bitforce.c 17 KB

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