driver-bitforce.c 21 KB

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