driver-bitforce.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. {
  581. pmorebuf[0] = '\0'; // process expects only results
  582. break;
  583. }
  584. sz = strlen(pmorebuf);
  585. szleft -= sz;
  586. pmorebuf += sz;
  587. if (unlikely(!szleft))
  588. {
  589. // Out of buffer space somehow :(
  590. applog(LOG_DEBUG, "%"PRIpreprv": Ran out of buffer space for results, discarding extra data", bitforce->proc_repr);
  591. pmorebuf = _discardedbuf;
  592. szleft = sizeof(_discardedbuf);
  593. }
  594. }
  595. }
  596. else
  597. count = -1;
  598. mutex_unlock(mutexp);
  599. return count;
  600. }
  601. static
  602. void bitforce_job_get_results(struct thr_info *thr, struct work *work)
  603. {
  604. struct cgpu_info *bitforce = thr->cgpu;
  605. struct bitforce_data *data = bitforce->cgpu_data;
  606. int fdDev = bitforce->device->device_fd;
  607. unsigned int delay_time_ms;
  608. struct timeval elapsed;
  609. struct timeval now;
  610. char *pdevbuf = &data->noncebuf[0];
  611. bool stale;
  612. int count;
  613. gettimeofday(&now, NULL);
  614. timersub(&now, &bitforce->work_start_tv, &elapsed);
  615. bitforce->wait_ms = tv_to_ms(elapsed);
  616. bitforce->polling = true;
  617. if (!fdDev)
  618. goto commerr;
  619. stale = stale_work(work, true);
  620. if (unlikely(bitforce->wait_ms < bitforce->sleep_ms))
  621. {
  622. // We're likely here because of a work restart
  623. // Since Bitforce cannot stop a work without losing results, only do it if the current job is finding stale shares
  624. // BFP_QUEUE does not support stopping work at all
  625. if (data->proto == BFP_QUEUE || !stale)
  626. {
  627. delay_time_ms = bitforce->sleep_ms - bitforce->wait_ms;
  628. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  629. data->poll_func = 2;
  630. return;
  631. }
  632. }
  633. while (1) {
  634. const char *cmd = (data->proto == BFP_QUEUE) ? "ZOX" : "ZFX";
  635. count = bitforce_zox(thr, cmd);
  636. gettimeofday(&now, NULL);
  637. timersub(&now, &bitforce->work_start_tv, &elapsed);
  638. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  639. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  640. tv_to_ms(elapsed), (unsigned long)BITFORCE_LONG_TIMEOUT_MS);
  641. goto out;
  642. }
  643. if (!count)
  644. goto noqr;
  645. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  646. break;
  647. data->result_busy_polled = bitforce->wait_ms;
  648. if (stale && data->proto != BFP_QUEUE)
  649. {
  650. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  651. bitforce->proc_repr);
  652. goto out;
  653. }
  654. noqr:
  655. data->result_busy_polled = bitforce->wait_ms;
  656. /* if BFL is throttling, no point checking so quickly */
  657. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  658. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  659. data->poll_func = 2;
  660. return;
  661. }
  662. if (count < 0 && pdevbuf[0] == 'N')
  663. count = strncasecmp(pdevbuf, "NONCE-FOUND", 11) ? 1 : 0;
  664. // At this point, 'count' is:
  665. // negative, in case of some kind of error
  666. // zero, if NO-NONCE (FPGA either completed with no results, or rebooted)
  667. // positive, if at least one job completed successfully
  668. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  669. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  670. tv_to_ms(elapsed), (unsigned long)BITFORCE_TIMEOUT_MS);
  671. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  672. ++bitforce->hw_errors;
  673. ++hw_errors;
  674. /* If the device truly throttled, it didn't process the job and there
  675. * are no results. But check first, just in case we're wrong about it
  676. * throttling.
  677. */
  678. if (count > 0)
  679. goto out;
  680. } else if (count >= 0) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  681. /* Simple timing adjustment. Allow a few polls to cope with
  682. * OS timer delays being variably reliable. wait_ms will
  683. * always equal sleep_ms when we've waited greater than or
  684. * equal to the result return time.*/
  685. delay_time_ms = bitforce->sleep_ms;
  686. if (!data->result_busy_polled)
  687. {
  688. // No busy polls before results received
  689. if (bitforce->wait_ms > delay_time_ms + (WORK_CHECK_INTERVAL_MS * 8))
  690. // ... due to poll being rather late; ignore it as an anomaly
  691. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, later than scheduled %ums (ignoring)",
  692. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms);
  693. else
  694. if (bitforce->sleep_ms > data->sleep_ms_default + (BITFORCE_CHECK_INTERVAL_MS * 0x20))
  695. {
  696. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on delayed schedule %ums; Wait time changed to: %ums (default sch)",
  697. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, data->sleep_ms_default);
  698. bitforce->sleep_ms = data->sleep_ms_default;
  699. }
  700. else
  701. {
  702. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on default schedule %ums; Wait time changed to: %ums (check interval)",
  703. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, BITFORCE_CHECK_INTERVAL_MS);
  704. bitforce->sleep_ms = BITFORCE_CHECK_INTERVAL_MS;
  705. }
  706. }
  707. else
  708. {
  709. if (data->result_busy_polled - bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  710. {
  711. bitforce->sleep_ms = data->result_busy_polled - (WORK_CHECK_INTERVAL_MS / 2);
  712. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time changed to: %ums",
  713. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms, bitforce->sleep_ms);
  714. }
  715. else
  716. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time unchanged",
  717. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms);
  718. }
  719. /* Work out the average time taken. Float for calculation, uint for display */
  720. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  721. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  722. }
  723. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  724. if (count < 0 && strncasecmp(pdevbuf, "I", 1)) {
  725. bitforce->hw_errors++;
  726. ++hw_errors;
  727. applog(LOG_WARNING, "%"PRIpreprv": Error: Get result reports: %s", bitforce->proc_repr, pdevbuf);
  728. bitforce_clear_buffer(bitforce);
  729. }
  730. out:
  731. bitforce->polling = false;
  732. job_results_fetched(thr);
  733. return;
  734. commerr:
  735. bitforce_comm_error(thr);
  736. goto out;
  737. }
  738. static
  739. void bitforce_process_result_nonces(struct thr_info *thr, struct work *work, char *pnoncebuf)
  740. {
  741. struct cgpu_info *bitforce = thr->cgpu;
  742. struct bitforce_data *data = bitforce->cgpu_data;
  743. uint32_t nonce;
  744. while (1) {
  745. hex2bin((void*)&nonce, pnoncebuf, 4);
  746. nonce = be32toh(nonce);
  747. if (unlikely(data->proto == BFP_RANGE && (nonce >= work->blk.nonce ||
  748. /* FIXME: blk.nonce is probably moved on quite a bit now! */
  749. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  750. applog(LOG_WARNING, "%"PRIpreprv": Disabling broken nonce range support", bitforce->proc_repr);
  751. bitforce_change_mode(bitforce, BFP_WORK);
  752. }
  753. submit_nonce(thr, work, nonce);
  754. if (strncmp(&pnoncebuf[8], ",", 1))
  755. break;
  756. pnoncebuf += 9;
  757. }
  758. }
  759. static
  760. bool bitforce_process_qresult_line_i(struct thr_info *thr, char *midstate, char *datatail, char *buf, struct work *work)
  761. {
  762. if (!work)
  763. return false;
  764. if (memcmp(work->midstate, midstate, 32))
  765. return false;
  766. if (memcmp(&work->data[64], datatail, 12))
  767. return false;
  768. if (!atoi(&buf[90]))
  769. return true;
  770. bitforce_process_result_nonces(thr, work, &buf[92]);
  771. return true;
  772. }
  773. static
  774. void bitforce_process_qresult_line(struct thr_info *thr, char *buf, struct work *work)
  775. {
  776. struct cgpu_info *bitforce = thr->cgpu;
  777. char midstate[32], datatail[12];
  778. hex2bin((void*)midstate, buf, 32);
  779. hex2bin((void*)datatail, &buf[65], 12);
  780. if (!( bitforce_process_qresult_line_i(thr, midstate, datatail, buf, work)
  781. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->work)
  782. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->prev_work)
  783. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->next_work) ))
  784. {
  785. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queued results", bitforce->proc_repr);
  786. ++bitforce->hw_errors;
  787. ++hw_errors;
  788. }
  789. }
  790. static inline
  791. char *next_line(char *in)
  792. {
  793. while (in[0] && (in++)[0] != '\n')
  794. {}
  795. return in;
  796. }
  797. static
  798. int64_t bitforce_job_process_results(struct thr_info *thr, struct work *work, __maybe_unused bool stopping)
  799. {
  800. struct cgpu_info *bitforce = thr->cgpu;
  801. struct bitforce_data *data = bitforce->cgpu_data;
  802. char *pnoncebuf = &data->noncebuf[0];
  803. int count;
  804. if (!strncasecmp(pnoncebuf, "NO-", 3))
  805. return bitforce->nonces; /* No valid nonce found */
  806. if (!strncasecmp(pnoncebuf, "NONCE-FOUND", 11))
  807. {
  808. bitforce_process_result_nonces(thr, work, &pnoncebuf[12]);
  809. count = 1;
  810. }
  811. else
  812. if (!strncasecmp(pnoncebuf, "COUNT:", 6))
  813. {
  814. count = 0;
  815. pnoncebuf = next_line(pnoncebuf);
  816. while (pnoncebuf[0])
  817. {
  818. bitforce_process_qresult_line(thr, pnoncebuf, work);
  819. ++count;
  820. pnoncebuf = next_line(pnoncebuf);
  821. }
  822. }
  823. else
  824. return 0;
  825. // FIXME: This might have changed in the meantime (new job start, or broken)
  826. return bitforce->nonces * count;
  827. }
  828. static void bitforce_shutdown(struct thr_info *thr)
  829. {
  830. struct cgpu_info *bitforce = thr->cgpu;
  831. int *p_fdDev = &bitforce->device->device_fd;
  832. BFclose(*p_fdDev);
  833. *p_fdDev = 0;
  834. }
  835. static void biforce_thread_enable(struct thr_info *thr)
  836. {
  837. struct cgpu_info *bitforce = thr->cgpu;
  838. bitforce_init(bitforce);
  839. }
  840. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  841. {
  842. return bitforce_get_temp(bitforce);
  843. }
  844. static bool bitforce_identify(struct cgpu_info *bitforce)
  845. {
  846. bitforce->flash_led = true;
  847. return true;
  848. }
  849. static bool bitforce_thread_init(struct thr_info *thr)
  850. {
  851. struct cgpu_info *bitforce = thr->cgpu;
  852. unsigned int wait;
  853. struct bitforce_data *data;
  854. struct bitforce_init_data *initdata = bitforce->cgpu_data;
  855. bool sc = initdata->sc;
  856. int xlink_id = 0;
  857. for ( ; bitforce; bitforce = bitforce->next_proc)
  858. {
  859. if (unlikely(xlink_id > 30))
  860. {
  861. applog(LOG_ERR, "%"PRIpreprv": Failed to find XLINK address", bitforce->proc_repr);
  862. dev_error(bitforce, REASON_THREAD_FAIL_INIT);
  863. bitforce->reinit_backoff = 1e10;
  864. continue;
  865. }
  866. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  867. bitforce->cgpu_data = data = malloc(sizeof(*data));
  868. *data = (struct bitforce_data){
  869. .xlink_id = xlink_id,
  870. .next_work_ob = ">>>>>>>>|---------- MidState ----------||-DataTail-||Nonces|>>>>>>>>",
  871. .proto = BFP_RANGE,
  872. .sc = sc,
  873. .sleep_ms_default = BITFORCE_SLEEP_MS,
  874. };
  875. if (sc)
  876. {
  877. // ".......S|---------- MidState ----------||-DataTail-||Nonces|E"
  878. data->next_work_ob[8+32+12+8] = '\xAA';
  879. data->next_work_obs = &data->next_work_ob[7];
  880. bitforce_change_mode(bitforce, BFP_QUEUE);
  881. }
  882. else
  883. {
  884. data->next_work_obs = &data->next_work_ob[0];
  885. // Unconditionally change away from cold-initialized BFP_RANGE, to allow for setting up other variables
  886. bitforce_change_mode(bitforce, BFP_WORK);
  887. /* Initially enable support for nonce range and disable it later if it
  888. * fails */
  889. if (opt_bfl_noncerange)
  890. bitforce_change_mode(bitforce, BFP_RANGE);
  891. }
  892. while (xlink_id < 31 && !(initdata->devmask & (1 << ++xlink_id)))
  893. {}
  894. }
  895. bitforce = thr->cgpu;
  896. /* Pause each new thread at least 100ms between initialising
  897. * so the devices aren't making calls all at the same time. */
  898. wait = thr->id * MAX_START_DELAY_MS;
  899. applog(LOG_DEBUG, "%s: Delaying start by %dms", bitforce->dev_repr, wait / 1000);
  900. nmsleep(wait);
  901. return true;
  902. }
  903. static struct api_data *bitforce_api_stats(struct cgpu_info *cgpu)
  904. {
  905. struct api_data *root = NULL;
  906. // Warning, access to these is not locked - but we don't really
  907. // care since hashing performance is way more important than
  908. // locking access to displaying API debug 'stats'
  909. // If locking becomes an issue for any of them, use copy_data=true also
  910. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  911. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  912. return root;
  913. }
  914. void bitforce_poll(struct thr_info *thr)
  915. {
  916. struct cgpu_info *bitforce = thr->cgpu;
  917. struct bitforce_data *data = bitforce->cgpu_data;
  918. int poll = data->poll_func;
  919. thr->tv_poll.tv_sec = -1;
  920. data->poll_func = 0;
  921. switch (poll)
  922. {
  923. case 1:
  924. bitforce_job_start(thr);
  925. break;
  926. case 2:
  927. bitforce_job_get_results(thr, thr->work);
  928. break;
  929. default:
  930. applog(LOG_ERR, "%"PRIpreprv": Unexpected poll from device API!", thr->cgpu->proc_repr);
  931. }
  932. }
  933. struct device_api bitforce_api = {
  934. .dname = "bitforce",
  935. .name = "BFL",
  936. .api_detect = bitforce_detect,
  937. .get_api_stats = bitforce_api_stats,
  938. .minerloop = minerloop_async,
  939. .reinit_device = bitforce_init,
  940. .get_statline_before = get_bitforce_statline_before,
  941. .get_stats = bitforce_get_stats,
  942. .identify_device = bitforce_identify,
  943. .thread_prepare = bitforce_thread_prepare,
  944. .thread_init = bitforce_thread_init,
  945. .job_prepare = bitforce_job_prepare,
  946. .job_start = bitforce_job_start,
  947. .job_get_results = bitforce_job_get_results,
  948. .poll = bitforce_poll,
  949. .job_process_results = bitforce_job_process_results,
  950. .thread_shutdown = bitforce_shutdown,
  951. .thread_enable = biforce_thread_enable
  952. };