driver-bitforce.c 27 KB

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