driver-bitforce.c 18 KB

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