driver-bitforce.c 23 KB

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