driver-bitforce.c 17 KB

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