driver-bitforce.c 22 KB

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