driver-bitforce.c 21 KB

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