driver-bitforce.c 29 KB

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