driver-bitforce.c 19 KB

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