driver-bitforce.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. int poll_func;
  101. };
  102. static void bitforce_clear_buffer(struct cgpu_info *);
  103. static
  104. void bitforce_comm_error(struct thr_info *thr)
  105. {
  106. struct cgpu_info *bitforce = thr->cgpu;
  107. struct bitforce_data *data = bitforce->cgpu_data;
  108. data->noncebuf[0] = '\0';
  109. applog(LOG_ERR, "%"PRIpreprv": Comms error", bitforce->proc_repr);
  110. dev_error(bitforce, REASON_DEV_COMMS_ERROR);
  111. ++bitforce->hw_errors;
  112. ++hw_errors;
  113. BFclose(bitforce->device_fd);
  114. int fd = bitforce->device_fd = BFopen(bitforce->device_path);
  115. if (fd == -1)
  116. {
  117. applog(LOG_ERR, "%"PRIpreprv": Error reopening", bitforce->proc_repr);
  118. return;
  119. }
  120. /* empty read buffer */
  121. bitforce_clear_buffer(bitforce);
  122. }
  123. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  124. {
  125. float gt = bitforce->temp;
  126. if (gt > 0)
  127. tailsprintf(buf, "%5.1fC ", gt);
  128. else
  129. tailsprintf(buf, " ", gt);
  130. tailsprintf(buf, " | ");
  131. }
  132. static bool bitforce_thread_prepare(struct thr_info *thr)
  133. {
  134. struct cgpu_info *bitforce = thr->cgpu;
  135. int fdDev = BFopen(bitforce->device_path);
  136. struct timeval now;
  137. if (unlikely(fdDev == -1)) {
  138. applog(LOG_ERR, "%"PRIpreprv": Failed to open %s", bitforce->proc_repr, bitforce->device_path);
  139. return false;
  140. }
  141. bitforce->device_fd = fdDev;
  142. applog(LOG_INFO, "%"PRIpreprv": Opened %s", bitforce->proc_repr, bitforce->device_path);
  143. gettimeofday(&now, NULL);
  144. get_datestamp(bitforce->init, &now);
  145. return true;
  146. }
  147. static void bitforce_clear_buffer(struct cgpu_info *bitforce)
  148. {
  149. int fdDev = bitforce->device_fd;
  150. char pdevbuf[0x100];
  151. int count = 0;
  152. if (!fdDev)
  153. return;
  154. applog(LOG_DEBUG, "%"PRIpreprv": Clearing read buffer", bitforce->proc_repr);
  155. mutex_lock(&bitforce->device_mutex);
  156. do {
  157. pdevbuf[0] = '\0';
  158. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  159. } while (pdevbuf[0] && (++count < 10));
  160. mutex_unlock(&bitforce->device_mutex);
  161. }
  162. void bitforce_init(struct cgpu_info *bitforce)
  163. {
  164. const char *devpath = bitforce->device_path;
  165. int fdDev = bitforce->device_fd, retries = 0;
  166. char pdevbuf[0x100];
  167. char *s;
  168. applog(LOG_WARNING, "%"PRIpreprv": Re-initialising", bitforce->proc_repr);
  169. bitforce_clear_buffer(bitforce);
  170. mutex_lock(&bitforce->device_mutex);
  171. if (fdDev) {
  172. BFclose(fdDev);
  173. sleep(5);
  174. }
  175. bitforce->device_fd = 0;
  176. fdDev = BFopen(devpath);
  177. if (unlikely(fdDev == -1)) {
  178. mutex_unlock(&bitforce->device_mutex);
  179. applog(LOG_ERR, "%"PRIpreprv": Failed to open %s", bitforce->proc_repr, devpath);
  180. return;
  181. }
  182. do {
  183. BFwrite(fdDev, "ZGX", 3);
  184. pdevbuf[0] = '\0';
  185. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  186. if (unlikely(!pdevbuf[0])) {
  187. mutex_unlock(&bitforce->device_mutex);
  188. applog(LOG_ERR, "%"PRIpreprv": Error reading/timeout (ZGX)", bitforce->proc_repr);
  189. return;
  190. }
  191. if (retries++)
  192. nmsleep(10);
  193. } while (!strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  194. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  195. mutex_unlock(&bitforce->device_mutex);
  196. applog(LOG_ERR, "%"PRIpreprv": Didn't recognise BitForce on %s returned: %s", bitforce->proc_repr, devpath, pdevbuf);
  197. return;
  198. }
  199. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  200. s[0] = '\0';
  201. bitforce->name = strdup(pdevbuf + 7);
  202. }
  203. bitforce->device_fd = fdDev;
  204. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  205. mutex_unlock(&bitforce->device_mutex);
  206. }
  207. static void bitforce_flash_led(struct cgpu_info *bitforce)
  208. {
  209. int fdDev = bitforce->device_fd;
  210. if (!fdDev)
  211. return;
  212. /* Do not try to flash the led if we're polling for a result to
  213. * minimise the chance of interleaved results */
  214. if (bitforce->polling)
  215. return;
  216. /* It is not critical flashing the led so don't get stuck if we
  217. * can't grab the mutex here */
  218. if (mutex_trylock(&bitforce->device_mutex))
  219. return;
  220. BFwrite(fdDev, "ZMX", 3);
  221. /* Once we've tried - don't do it until told to again */
  222. bitforce->flash_led = false;
  223. /* However, this stops anything else getting a reply
  224. * So best to delay any other access to the BFL */
  225. sleep(4);
  226. mutex_unlock(&bitforce->device_mutex);
  227. return; // nothing is returned by the BFL
  228. }
  229. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  230. {
  231. int fdDev = bitforce->device_fd;
  232. char pdevbuf[0x100];
  233. char *s;
  234. if (!fdDev)
  235. return false;
  236. /* Do not try to get the temperature if we're polling for a result to
  237. * minimise the chance of interleaved results */
  238. if (bitforce->polling)
  239. return true;
  240. // Flash instead of Temp - doing both can be too slow
  241. if (bitforce->flash_led) {
  242. bitforce_flash_led(bitforce);
  243. return true;
  244. }
  245. /* It is not critical getting temperature so don't get stuck if we
  246. * can't grab the mutex here */
  247. if (mutex_trylock(&bitforce->device_mutex))
  248. return false;
  249. BFwrite(fdDev, "ZLX", 3);
  250. pdevbuf[0] = '\0';
  251. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  252. mutex_unlock(&bitforce->device_mutex);
  253. if (unlikely(!pdevbuf[0])) {
  254. applog(LOG_ERR, "%"PRIpreprv": Error: Get temp returned empty string/timed out", bitforce->proc_repr);
  255. bitforce->hw_errors++;
  256. ++hw_errors;
  257. return false;
  258. }
  259. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  260. float temp = strtof(s + 1, NULL);
  261. /* Cope with older software that breaks and reads nonsense
  262. * values */
  263. if (temp > 100)
  264. temp = strtod(s + 1, NULL);
  265. if (temp > 0) {
  266. bitforce->temp = temp;
  267. }
  268. } else {
  269. /* Use the temperature monitor as a kind of watchdog for when
  270. * our responses are out of sync and flush the buffer to
  271. * hopefully recover */
  272. applog(LOG_WARNING, "%"PRIpreprv": Garbled response probably throttling, clearing buffer", bitforce->proc_repr);
  273. dev_error(bitforce, REASON_DEV_THROTTLE);
  274. /* Count throttling episodes as hardware errors */
  275. bitforce->hw_errors++;
  276. ++hw_errors;
  277. bitforce_clear_buffer(bitforce);
  278. return false;
  279. }
  280. return true;
  281. }
  282. static
  283. bool bitforce_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  284. {
  285. struct cgpu_info *bitforce = thr->cgpu;
  286. struct bitforce_data *data = bitforce->cgpu_data;
  287. unsigned char *ob_ms = &data->next_work_ob[8];
  288. unsigned char *ob_dt = &ob_ms[32];
  289. // If polling job_start, cancel it
  290. if (data->poll_func == 1)
  291. {
  292. thr->tv_poll.tv_sec = -1;
  293. data->poll_func = 0;
  294. }
  295. memcpy(ob_ms, work->midstate, 32);
  296. memcpy(ob_dt, work->data + 64, 12);
  297. if (bitforce->nonce_range)
  298. {
  299. uint32_t *ob_nonce = (uint32_t*)&(ob_dt[32]);
  300. ob_nonce[0] = htobe32(work->blk.nonce);
  301. ob_nonce[1] = htobe32(work->blk.nonce + bitforce->nonces);
  302. // FIXME: if nonce range fails... we didn't increment enough
  303. work->blk.nonce += bitforce->nonces + 1;
  304. }
  305. else
  306. work->blk.nonce = 0xffffffff;
  307. return true;
  308. }
  309. static
  310. void bitforce_change_mode(struct cgpu_info *bitforce, bool noncerange)
  311. {
  312. struct bitforce_data *data = bitforce->cgpu_data;
  313. if (bitforce->nonce_range == noncerange)
  314. return;
  315. bitforce->nonce_range = noncerange;
  316. if (noncerange)
  317. {
  318. /* Split work up into 1/5th nonce ranges */
  319. bitforce->nonces = 0x33333332;
  320. bitforce->sleep_ms /= 5;
  321. bitforce->kname = KNAME_RANGE;
  322. }
  323. else
  324. {
  325. bitforce->nonces = 0xffffffff;
  326. bitforce->sleep_ms *= 5;
  327. bitforce->kname = KNAME_WORK;
  328. memset(&data->next_work_ob[8+32+12], '>', 8);
  329. }
  330. }
  331. static
  332. void bitforce_job_start(struct thr_info *thr)
  333. {
  334. struct cgpu_info *bitforce = thr->cgpu;
  335. struct bitforce_data *data = bitforce->cgpu_data;
  336. int fdDev = bitforce->device_fd;
  337. unsigned char *ob = data->next_work_ob;
  338. char pdevbuf[0x100];
  339. char *s;
  340. struct timeval tv_now;
  341. if (!fdDev)
  342. goto commerr;
  343. re_send:
  344. mutex_lock(&bitforce->device_mutex);
  345. if (bitforce->nonce_range)
  346. BFwrite(fdDev, "ZPX", 3);
  347. else
  348. BFwrite(fdDev, "ZDX", 3);
  349. pdevbuf[0] = '\0';
  350. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  351. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  352. mutex_unlock(&bitforce->device_mutex);
  353. gettimeofday(&tv_now, NULL);
  354. timer_set_delay(&thr->tv_poll, &tv_now, WORK_CHECK_INTERVAL_MS * 1000);
  355. data->poll_func = 1;
  356. return;
  357. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  358. mutex_unlock(&bitforce->device_mutex);
  359. if (bitforce->nonce_range) {
  360. applog(LOG_WARNING, "%"PRIpreprv": Does not support nonce range, disabling", bitforce->proc_repr);
  361. bitforce_change_mode(bitforce, false);
  362. goto re_send;
  363. }
  364. applog(LOG_ERR, "%"PRIpreprv": Error: Send work reports: %s", bitforce->proc_repr, pdevbuf);
  365. goto commerr;
  366. }
  367. if (!bitforce->nonce_range) {
  368. BFwrite(fdDev, ob, 60);
  369. } else {
  370. BFwrite(fdDev, ob, 68);
  371. }
  372. pdevbuf[0] = '\0';
  373. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  374. mt_job_transition(thr);
  375. mutex_unlock(&bitforce->device_mutex);
  376. if (opt_debug) {
  377. s = bin2hex(ob + 8, 44);
  378. applog(LOG_DEBUG, "%"PRIpreprv": block data: %s", bitforce->proc_repr, s);
  379. free(s);
  380. }
  381. if (unlikely(!pdevbuf[0])) {
  382. applog(LOG_ERR, "%"PRIpreprv": Error: Send block data returned empty string/timed out", bitforce->proc_repr);
  383. goto commerr;
  384. }
  385. if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  386. applog(LOG_ERR, "%"PRIpreprv": Error: Send block data reports: %s", bitforce->proc_repr, pdevbuf);
  387. goto commerr;
  388. }
  389. gettimeofday(&tv_now, NULL);
  390. bitforce->work_start_tv = tv_now;
  391. timer_set_delay(&thr->tv_morework, &tv_now, bitforce->sleep_ms * 1000);
  392. job_start_complete(thr);
  393. return;
  394. commerr:
  395. bitforce_comm_error(thr);
  396. job_start_abort(thr, true);
  397. }
  398. static inline int noisy_stale_wait(unsigned int mstime, struct work*work, bool checkend, struct cgpu_info*bitforce)
  399. {
  400. int rv = stale_wait(mstime, work, checkend);
  401. if (rv)
  402. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  403. bitforce->proc_repr);
  404. return rv;
  405. }
  406. #define noisy_stale_wait(mstime, work, checkend) noisy_stale_wait(mstime, work, checkend, bitforce)
  407. static
  408. void bitforce_job_get_results(struct thr_info *thr, struct work *work)
  409. {
  410. struct cgpu_info *bitforce = thr->cgpu;
  411. struct bitforce_data *data = bitforce->cgpu_data;
  412. int fdDev = bitforce->device_fd;
  413. unsigned int delay_time_ms;
  414. struct timeval elapsed;
  415. struct timeval now;
  416. char *pdevbuf = &data->noncebuf[0];
  417. bool stale;
  418. gettimeofday(&now, NULL);
  419. timersub(&now, &bitforce->work_start_tv, &elapsed);
  420. bitforce->wait_ms = tv_to_ms(elapsed);
  421. bitforce->polling = true;
  422. if (!fdDev)
  423. goto commerr;
  424. stale = stale_work(work, true);
  425. if (unlikely(bitforce->wait_ms < bitforce->sleep_ms))
  426. {
  427. // We're likely here because of a work restart
  428. // Since Bitforce cannot stop a work without losing results, only do it if the current job is finding stale shares
  429. if (!stale)
  430. {
  431. delay_time_ms = bitforce->sleep_ms - bitforce->wait_ms;
  432. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  433. data->poll_func = 2;
  434. return;
  435. }
  436. }
  437. while (1) {
  438. mutex_lock(&bitforce->device_mutex);
  439. BFwrite(fdDev, "ZFX", 3);
  440. pdevbuf[0] = '\0';
  441. BFgets(pdevbuf, sizeof(data->noncebuf), fdDev);
  442. mutex_unlock(&bitforce->device_mutex);
  443. gettimeofday(&now, NULL);
  444. timersub(&now, &bitforce->work_start_tv, &elapsed);
  445. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  446. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  447. tv_to_ms(elapsed), (unsigned long)BITFORCE_LONG_TIMEOUT_MS);
  448. goto out;
  449. }
  450. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  451. break;
  452. if (stale)
  453. {
  454. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  455. bitforce->proc_repr);
  456. goto out;
  457. }
  458. /* if BFL is throttling, no point checking so quickly */
  459. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  460. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  461. data->poll_func = 2;
  462. return;
  463. }
  464. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  465. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  466. tv_to_ms(elapsed), (unsigned long)BITFORCE_TIMEOUT_MS);
  467. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  468. ++bitforce->hw_errors;
  469. ++hw_errors;
  470. /* If the device truly throttled, it didn't process the job and there
  471. * are no results. But check first, just in case we're wrong about it
  472. * throttling.
  473. */
  474. if (strncasecmp(pdevbuf, "NONCE-FOUND", 11))
  475. goto out;
  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, "%"PRIpreprv": Wait time changed to: %d, waited %u", bitforce->proc_repr, 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, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  497. if (strncasecmp(pdevbuf, "NONCE-FOUND", 11) && (pdevbuf[2] != '-') && strncasecmp(pdevbuf, "I", 1)) {
  498. bitforce->hw_errors++;
  499. ++hw_errors;
  500. applog(LOG_WARNING, "%"PRIpreprv": Error: Get result reports: %s", bitforce->proc_repr, pdevbuf);
  501. bitforce_clear_buffer(bitforce);
  502. }
  503. out:
  504. bitforce->polling = false;
  505. job_results_fetched(thr);
  506. return;
  507. commerr:
  508. bitforce_comm_error(thr);
  509. goto out;
  510. }
  511. static
  512. int64_t bitforce_job_process_results(struct thr_info *thr, struct work *work, __maybe_unused bool stopping)
  513. {
  514. struct cgpu_info *bitforce = thr->cgpu;
  515. struct bitforce_data *data = bitforce->cgpu_data;
  516. char *pnoncebuf = &data->noncebuf[0];
  517. uint32_t nonce;
  518. if (!strncasecmp(pnoncebuf, "NO-", 3))
  519. return bitforce->nonces; /* No valid nonce found */
  520. if (strncasecmp(pnoncebuf, "NONCE-FOUND", 11))
  521. return 0;
  522. pnoncebuf += 12;
  523. while (1) {
  524. hex2bin((void*)&nonce, pnoncebuf, 4);
  525. nonce = be32toh(nonce);
  526. if (unlikely(bitforce->nonce_range && (nonce >= work->blk.nonce ||
  527. /* FIXME: blk.nonce is probably moved on quite a bit now! */
  528. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  529. applog(LOG_WARNING, "%"PRIpreprv": Disabling broken nonce range support", bitforce->proc_repr);
  530. bitforce_change_mode(bitforce, false);
  531. }
  532. submit_nonce(thr, work, nonce);
  533. if (strncmp(&pnoncebuf[8], ",", 1))
  534. break;
  535. pnoncebuf += 9;
  536. }
  537. // FIXME: This might have changed in the meantime (new job start, or broken)
  538. return bitforce->nonces;
  539. }
  540. static void bitforce_shutdown(struct thr_info *thr)
  541. {
  542. struct cgpu_info *bitforce = thr->cgpu;
  543. BFclose(bitforce->device_fd);
  544. bitforce->device_fd = 0;
  545. }
  546. static void biforce_thread_enable(struct thr_info *thr)
  547. {
  548. struct cgpu_info *bitforce = thr->cgpu;
  549. bitforce_init(bitforce);
  550. }
  551. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  552. {
  553. return bitforce_get_temp(bitforce);
  554. }
  555. static bool bitforce_identify(struct cgpu_info *bitforce)
  556. {
  557. bitforce->flash_led = true;
  558. return true;
  559. }
  560. static bool bitforce_thread_init(struct thr_info *thr)
  561. {
  562. struct cgpu_info *bitforce = thr->cgpu;
  563. unsigned int wait;
  564. struct bitforce_data *data;
  565. bitforce->cgpu_data = data = malloc(sizeof(*data));
  566. *data = (struct bitforce_data){
  567. .next_work_ob = ">>>>>>>>|---------- MidState ----------||-DataTail-|>>>>>>>>>>>>>>>>",
  568. };
  569. bitforce->nonce_range = true;
  570. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  571. bitforce_change_mode(bitforce, false);
  572. /* Initially enable support for nonce range and disable it later if it
  573. * fails */
  574. if (opt_bfl_noncerange)
  575. bitforce_change_mode(bitforce, true);
  576. /* Pause each new thread at least 100ms between initialising
  577. * so the devices aren't making calls all at the same time. */
  578. wait = thr->id * MAX_START_DELAY_MS;
  579. applog(LOG_DEBUG, "%"PRIpreprv": Delaying start by %dms", bitforce->proc_repr, wait / 1000);
  580. nmsleep(wait);
  581. return true;
  582. }
  583. static struct api_data *bitforce_api_stats(struct cgpu_info *cgpu)
  584. {
  585. struct api_data *root = NULL;
  586. // Warning, access to these is not locked - but we don't really
  587. // care since hashing performance is way more important than
  588. // locking access to displaying API debug 'stats'
  589. // If locking becomes an issue for any of them, use copy_data=true also
  590. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  591. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  592. return root;
  593. }
  594. void bitforce_poll(struct thr_info *thr)
  595. {
  596. struct cgpu_info *bitforce = thr->cgpu;
  597. struct bitforce_data *data = bitforce->cgpu_data;
  598. int poll = data->poll_func;
  599. thr->tv_poll.tv_sec = -1;
  600. data->poll_func = 0;
  601. switch (poll)
  602. {
  603. case 1:
  604. bitforce_job_start(thr);
  605. break;
  606. case 2:
  607. bitforce_job_get_results(thr, thr->work);
  608. break;
  609. default:
  610. applog(LOG_ERR, "%"PRIpreprv": Unexpected poll from device API!", thr->cgpu->proc_repr);
  611. }
  612. }
  613. struct device_api bitforce_api = {
  614. .dname = "bitforce",
  615. .name = "BFL",
  616. .api_detect = bitforce_detect,
  617. .get_api_stats = bitforce_api_stats,
  618. .minerloop = minerloop_async,
  619. .reinit_device = bitforce_init,
  620. .get_statline_before = get_bitforce_statline_before,
  621. .get_stats = bitforce_get_stats,
  622. .identify_device = bitforce_identify,
  623. .thread_prepare = bitforce_thread_prepare,
  624. .thread_init = bitforce_thread_init,
  625. .job_prepare = bitforce_job_prepare,
  626. .job_start = bitforce_job_start,
  627. .job_get_results = bitforce_job_get_results,
  628. .poll = bitforce_poll,
  629. .job_process_results = bitforce_job_process_results,
  630. .thread_shutdown = bitforce_shutdown,
  631. .thread_enable = biforce_thread_enable
  632. };