driver-bitforce.c 20 KB

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