driver-bitforce.c 22 KB

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