driver-bitforce.c 22 KB

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