driver-bitforce.c 20 KB

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