driver-bitforce.c 21 KB

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