driver-bitforce.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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. #define BITFORCE_MAX_QUEUED 10
  33. #define BITFORCE_MAX_QRESULTS 10
  34. #define BITFORCE_GOAL_QRESULTS (BITFORCE_MAX_QRESULTS / 2)
  35. enum bitforce_proto {
  36. BFP_WORK,
  37. BFP_RANGE,
  38. BFP_QUEUE,
  39. BFP_BQUEUE,
  40. };
  41. static const char *protonames[] = {
  42. "full work",
  43. "nonce range",
  44. "work queue",
  45. "bulk queue",
  46. };
  47. struct device_api bitforce_api;
  48. struct device_api bitforce_queue_api;
  49. // Code must deal with a timeout
  50. #define BFopen(devpath) serial_open(devpath, 0, 250, true)
  51. static void BFgets(char *buf, size_t bufLen, int fd)
  52. {
  53. do {
  54. buf[0] = '\0';
  55. --bufLen;
  56. } while (likely(bufLen && read(fd, buf, 1) == 1 && (buf++)[0] != '\n'));
  57. buf[0] = '\0';
  58. }
  59. static ssize_t BFwrite(int fd, const void *buf, ssize_t bufLen)
  60. {
  61. if ((bufLen) != write(fd, buf, bufLen))
  62. return 0;
  63. else
  64. return bufLen;
  65. }
  66. static ssize_t bitforce_send(int fd, int procid, const void *buf, ssize_t bufLen)
  67. {
  68. if (!procid)
  69. return BFwrite(fd, buf, bufLen);
  70. if (bufLen > 255)
  71. return -1;
  72. size_t bufLeft = bufLen + 3;
  73. char realbuf[bufLeft], *bufp;
  74. ssize_t rv;
  75. memcpy(&realbuf[3], buf, bufLen);
  76. realbuf[0] = '@';
  77. realbuf[1] = procid;
  78. realbuf[2] = bufLen;
  79. bufp = realbuf;
  80. while (true)
  81. {
  82. rv = BFwrite(fd, bufp, bufLeft);
  83. if (rv <= 0)
  84. return rv;
  85. bufLeft -= rv;
  86. }
  87. return bufLen;
  88. }
  89. static
  90. void bitforce_cmd1(int fd, int procid, void *buf, size_t bufsz, const char *cmd)
  91. {
  92. bitforce_send(fd, procid, cmd, 3);
  93. BFgets(buf, bufsz, fd);
  94. }
  95. static
  96. void bitforce_cmd2(int fd, int procid, void *buf, size_t bufsz, const char *cmd, void *data, size_t datasz)
  97. {
  98. bitforce_cmd1(fd, procid, buf, bufsz, cmd);
  99. if (strncasecmp(buf, "OK", 2))
  100. return;
  101. bitforce_send(fd, procid, data, datasz);
  102. BFgets(buf, bufsz, fd);
  103. }
  104. #define BFclose(fd) close(fd)
  105. struct bitforce_init_data {
  106. bool sc;
  107. long devmask;
  108. };
  109. static bool bitforce_detect_one(const char *devpath)
  110. {
  111. int fdDev = serial_open(devpath, 0, 10, true);
  112. struct cgpu_info *bitforce;
  113. char pdevbuf[0x100];
  114. size_t pdevbuf_len;
  115. char *s;
  116. int procs = 1;
  117. struct bitforce_init_data *initdata;
  118. applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
  119. if (unlikely(fdDev == -1)) {
  120. applog(LOG_DEBUG, "BFL: Failed to open %s", devpath);
  121. return false;
  122. }
  123. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  124. if (unlikely(!pdevbuf[0])) {
  125. applog(LOG_DEBUG, "BFL: Error reading/timeout (ZGX)");
  126. return 0;
  127. }
  128. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  129. applog(LOG_DEBUG, "BFL: Didn't recognise BitForce on %s", devpath);
  130. BFclose(fdDev);
  131. return false;
  132. }
  133. applog(LOG_DEBUG, "Found BitForce device on %s", devpath);
  134. initdata = malloc(sizeof(*initdata));
  135. *initdata = (struct bitforce_init_data){
  136. .sc = false,
  137. };
  138. for ( bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZCX");
  139. strncasecmp(pdevbuf, "OK", 2);
  140. BFgets(pdevbuf, sizeof(pdevbuf), fdDev) )
  141. {
  142. pdevbuf_len = strlen(pdevbuf);
  143. if (unlikely(!pdevbuf_len))
  144. continue;
  145. pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
  146. applog(LOG_DEBUG, " %s", pdevbuf);
  147. if (!strncasecmp(pdevbuf, "DEVICES IN CHAIN:", 17))
  148. procs = atoi(&pdevbuf[17]);
  149. else
  150. if (!strncasecmp(pdevbuf, "CHAIN PRESENCE MASK:", 20))
  151. initdata->devmask = strtol(&pdevbuf[20], NULL, 16);
  152. else
  153. if (!strncasecmp(pdevbuf, "DEVICE:", 7) && strstr(pdevbuf, "SC"))
  154. initdata->sc = true;
  155. }
  156. BFclose(fdDev);
  157. // We have a real BitForce!
  158. bitforce = calloc(1, sizeof(*bitforce));
  159. bitforce->api = &bitforce_api;
  160. if (initdata->sc && procs > 1)
  161. bitforce->api = &bitforce_queue_api;
  162. bitforce->device_path = strdup(devpath);
  163. bitforce->deven = DEV_ENABLED;
  164. bitforce->procs = procs;
  165. bitforce->threads = 1;
  166. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  167. s[0] = '\0';
  168. bitforce->name = strdup(pdevbuf + 7);
  169. }
  170. bitforce->cgpu_data = initdata;
  171. mutex_init(&bitforce->device_mutex);
  172. return add_cgpu(bitforce);
  173. }
  174. static int bitforce_detect_auto(void)
  175. {
  176. return serial_autodetect(bitforce_detect_one, "BitFORCE", "SHA256");
  177. }
  178. static void bitforce_detect(void)
  179. {
  180. serial_detect_auto(&bitforce_api, bitforce_detect_one, bitforce_detect_auto);
  181. }
  182. struct bitforce_data {
  183. int xlink_id;
  184. unsigned char next_work_ob[70]; // Data aligned for 32-bit access
  185. unsigned char *next_work_obs; // Start of data to send
  186. unsigned char next_work_obsz;
  187. const char *next_work_cmd;
  188. char noncebuf[14 + (BITFORCE_MAX_QUEUED * 165)];
  189. int poll_func;
  190. enum bitforce_proto proto;
  191. bool sc;
  192. int queued;
  193. bool already_have_results;
  194. bool just_flushed;
  195. int ready_to_queue;
  196. unsigned result_busy_polled;
  197. unsigned sleep_ms_default;
  198. struct timeval tv_hashmeter_start;
  199. float temp[2];
  200. };
  201. static void bitforce_clear_buffer(struct cgpu_info *);
  202. static
  203. void bitforce_comm_error(struct thr_info *thr)
  204. {
  205. struct cgpu_info *bitforce = thr->cgpu;
  206. struct bitforce_data *data = bitforce->cgpu_data;
  207. int *p_fdDev = &bitforce->device->device_fd;
  208. data->noncebuf[0] = '\0';
  209. applog(LOG_ERR, "%"PRIpreprv": Comms error", bitforce->proc_repr);
  210. dev_error(bitforce, REASON_DEV_COMMS_ERROR);
  211. ++bitforce->hw_errors;
  212. ++hw_errors;
  213. BFclose(*p_fdDev);
  214. int fd = *p_fdDev = BFopen(bitforce->device_path);
  215. if (fd == -1)
  216. {
  217. applog(LOG_ERR, "%s: Error reopening %s", bitforce->dev_repr, bitforce->device_path);
  218. return;
  219. }
  220. /* empty read buffer */
  221. bitforce_clear_buffer(bitforce);
  222. }
  223. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  224. {
  225. struct bitforce_data *data = bitforce->cgpu_data;
  226. if (data->temp[0] > 0 && data->temp[1] > 0)
  227. tailsprintf(buf, "%5.1fC/%4.1fC | ", data->temp[0], data->temp[1]);
  228. else
  229. if (bitforce->temp > 0)
  230. tailsprintf(buf, "%5.1fC | ", bitforce->temp);
  231. else
  232. tailsprintf(buf, " | ");
  233. }
  234. static bool bitforce_thread_prepare(struct thr_info *thr)
  235. {
  236. struct cgpu_info *bitforce = thr->cgpu;
  237. int fdDev = BFopen(bitforce->device_path);
  238. struct timeval now;
  239. if (unlikely(fdDev == -1)) {
  240. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, bitforce->device_path);
  241. return false;
  242. }
  243. bitforce->device_fd = fdDev;
  244. applog(LOG_INFO, "%s: Opened %s", bitforce->dev_repr, bitforce->device_path);
  245. gettimeofday(&now, NULL);
  246. get_datestamp(bitforce->init, &now);
  247. return true;
  248. }
  249. static void bitforce_clear_buffer(struct cgpu_info *bitforce)
  250. {
  251. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  252. int fdDev = bitforce->device->device_fd;
  253. char pdevbuf[0x100];
  254. int count = 0;
  255. if (!fdDev)
  256. return;
  257. applog(LOG_DEBUG, "%"PRIpreprv": Clearing read buffer", bitforce->proc_repr);
  258. mutex_lock(mutexp);
  259. do {
  260. pdevbuf[0] = '\0';
  261. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  262. } while (pdevbuf[0] && (++count < 10));
  263. mutex_unlock(mutexp);
  264. }
  265. void bitforce_init(struct cgpu_info *bitforce)
  266. {
  267. const char *devpath = bitforce->device_path;
  268. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  269. int *p_fdDev = &bitforce->device->device_fd;
  270. int fdDev = *p_fdDev, retries = 0;
  271. char pdevbuf[0x100];
  272. char *s;
  273. applog(LOG_WARNING, "%"PRIpreprv": Re-initialising", bitforce->proc_repr);
  274. bitforce_clear_buffer(bitforce);
  275. mutex_lock(mutexp);
  276. if (fdDev) {
  277. BFclose(fdDev);
  278. sleep(5);
  279. }
  280. *p_fdDev = 0;
  281. fdDev = BFopen(devpath);
  282. if (unlikely(fdDev == -1)) {
  283. mutex_unlock(mutexp);
  284. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, devpath);
  285. return;
  286. }
  287. do {
  288. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  289. if (unlikely(!pdevbuf[0])) {
  290. mutex_unlock(mutexp);
  291. applog(LOG_ERR, "%s: Error reading/timeout (ZGX)", bitforce->dev_repr);
  292. return;
  293. }
  294. if (retries++)
  295. nmsleep(10);
  296. } while (!strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  297. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  298. mutex_unlock(mutexp);
  299. applog(LOG_ERR, "%s: Didn't recognise BitForce on %s returned: %s", bitforce->dev_repr, devpath, pdevbuf);
  300. return;
  301. }
  302. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  303. s[0] = '\0';
  304. free((void*)bitforce->name);
  305. bitforce->name = strdup(pdevbuf + 7);
  306. }
  307. *p_fdDev = fdDev;
  308. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  309. mutex_unlock(mutexp);
  310. }
  311. static void bitforce_flash_led(struct cgpu_info *bitforce)
  312. {
  313. struct bitforce_data *data = bitforce->cgpu_data;
  314. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  315. int fdDev = bitforce->device->device_fd;
  316. if (!fdDev)
  317. return;
  318. /* Do not try to flash the led if we're polling for a result to
  319. * minimise the chance of interleaved results */
  320. if (bitforce->polling)
  321. return;
  322. /* It is not critical flashing the led so don't get stuck if we
  323. * can't grab the mutex here */
  324. if (mutex_trylock(mutexp))
  325. return;
  326. char pdevbuf[0x100];
  327. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZMX");
  328. /* Once we've tried - don't do it until told to again */
  329. bitforce->flash_led = false;
  330. /* However, this stops anything else getting a reply
  331. * So best to delay any other access to the BFL */
  332. sleep(4);
  333. mutex_unlock(mutexp);
  334. return; // nothing is returned by the BFL
  335. }
  336. static
  337. float my_strtof(const char *nptr, char **endptr)
  338. {
  339. float f = strtof(nptr, endptr);
  340. /* Cope with older software that breaks and reads nonsense
  341. * values */
  342. if (f > 100)
  343. f = strtod(nptr, endptr);
  344. return f;
  345. }
  346. static
  347. void set_float_if_gt_zero(float *var, float value)
  348. {
  349. if (value > 0)
  350. *var = value;
  351. }
  352. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  353. {
  354. struct bitforce_data *data = bitforce->cgpu_data;
  355. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  356. int fdDev = bitforce->device->device_fd;
  357. char pdevbuf[0x100];
  358. char *s;
  359. if (!fdDev)
  360. return false;
  361. /* Do not try to get the temperature if we're polling for a result to
  362. * minimise the chance of interleaved results */
  363. if (bitforce->polling)
  364. return true;
  365. // Flash instead of Temp - doing both can be too slow
  366. if (bitforce->flash_led) {
  367. bitforce_flash_led(bitforce);
  368. return true;
  369. }
  370. /* It is not critical getting temperature so don't get stuck if we
  371. * can't grab the mutex here */
  372. if (mutex_trylock(mutexp))
  373. return false;
  374. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZLX");
  375. mutex_unlock(mutexp);
  376. if (unlikely(!pdevbuf[0])) {
  377. applog(LOG_ERR, "%"PRIpreprv": Error: Get temp returned empty string/timed out", bitforce->proc_repr);
  378. bitforce->hw_errors++;
  379. ++hw_errors;
  380. return false;
  381. }
  382. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  383. float temp = my_strtof(s + 1, &s);
  384. set_float_if_gt_zero(&data->temp[0], temp);
  385. for ( ; s[0]; ++s)
  386. {
  387. if (!strncasecmp(s, "TEMP", 4) && (s = strchr(&s[4], ':')))
  388. {
  389. float temp2 = my_strtof(s + 1, &s);
  390. set_float_if_gt_zero(&data->temp[1], temp2);
  391. if (temp2 > temp)
  392. temp = temp;
  393. }
  394. }
  395. set_float_if_gt_zero(&bitforce->temp, temp);
  396. } else {
  397. /* Use the temperature monitor as a kind of watchdog for when
  398. * our responses are out of sync and flush the buffer to
  399. * hopefully recover */
  400. applog(LOG_WARNING, "%"PRIpreprv": Garbled response probably throttling, clearing buffer", bitforce->proc_repr);
  401. dev_error(bitforce, REASON_DEV_THROTTLE);
  402. /* Count throttling episodes as hardware errors */
  403. bitforce->hw_errors++;
  404. ++hw_errors;
  405. bitforce_clear_buffer(bitforce);
  406. return false;
  407. }
  408. return true;
  409. }
  410. static inline
  411. void dbg_block_data(struct cgpu_info *bitforce)
  412. {
  413. if (!opt_debug)
  414. return;
  415. struct bitforce_data *data = bitforce->cgpu_data;
  416. char *s;
  417. s = bin2hex(&data->next_work_ob[8], 44);
  418. applog(LOG_DEBUG, "%"PRIpreprv": block data: %s", bitforce->proc_repr, s);
  419. free(s);
  420. }
  421. static void bitforce_change_mode(struct cgpu_info *, enum bitforce_proto);
  422. static
  423. bool bitforce_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  424. {
  425. struct cgpu_info *bitforce = thr->cgpu;
  426. struct bitforce_data *data = bitforce->cgpu_data;
  427. int fdDev = bitforce->device->device_fd;
  428. unsigned char *ob_ms = &data->next_work_ob[8];
  429. unsigned char *ob_dt = &ob_ms[32];
  430. // If polling job_start, cancel it
  431. if (data->poll_func == 1)
  432. {
  433. thr->tv_poll.tv_sec = -1;
  434. data->poll_func = 0;
  435. }
  436. memcpy(ob_ms, work->midstate, 32);
  437. memcpy(ob_dt, work->data + 64, 12);
  438. switch (data->proto)
  439. {
  440. case BFP_BQUEUE:
  441. quit(1, "%"PRIpreprv": Impossible BFP_BQUEUE in bitforce_job_prepare", bitforce->proc_repr);
  442. case BFP_RANGE:
  443. {
  444. uint32_t *ob_nonce = (uint32_t*)&(ob_dt[32]);
  445. ob_nonce[0] = htobe32(work->blk.nonce);
  446. ob_nonce[1] = htobe32(work->blk.nonce + bitforce->nonces);
  447. // FIXME: if nonce range fails... we didn't increment enough
  448. work->blk.nonce += bitforce->nonces + 1;
  449. break;
  450. }
  451. case BFP_QUEUE:
  452. if (thr->work)
  453. {
  454. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  455. char pdevbuf[0x100];
  456. if (unlikely(!fdDev))
  457. return false;
  458. mutex_lock(mutexp);
  459. if (data->queued)
  460. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZQX");
  461. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, data->next_work_obs, data->next_work_obsz);
  462. mutex_unlock(mutexp);
  463. if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  464. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  465. bitforce_change_mode(bitforce, BFP_WORK);
  466. }
  467. else
  468. {
  469. dbg_block_data(bitforce);
  470. data->queued = 1;
  471. }
  472. }
  473. // fallthru...
  474. case BFP_WORK:
  475. work->blk.nonce = 0xffffffff;
  476. }
  477. return true;
  478. }
  479. static
  480. void bitforce_change_mode(struct cgpu_info *bitforce, enum bitforce_proto proto)
  481. {
  482. struct bitforce_data *data = bitforce->cgpu_data;
  483. if (data->proto == proto)
  484. return;
  485. if (data->proto == BFP_RANGE)
  486. {
  487. bitforce->nonces = 0xffffffff;
  488. bitforce->sleep_ms *= 5;
  489. data->sleep_ms_default *= 5;
  490. switch (proto)
  491. {
  492. case BFP_WORK:
  493. data->next_work_cmd = "ZDX";
  494. break;
  495. case BFP_QUEUE:
  496. data->next_work_cmd = "ZNX";
  497. default:
  498. ;
  499. }
  500. if (data->sc)
  501. {
  502. // "S|---------- MidState ----------||-DataTail-|E"
  503. data->next_work_ob[7] = 45;
  504. data->next_work_ob[8+32+12] = '\xAA';
  505. data->next_work_obsz = 46;
  506. }
  507. else
  508. {
  509. // ">>>>>>>>|---------- MidState ----------||-DataTail-|>>>>>>>>"
  510. memset(&data->next_work_ob[8+32+12], '>', 8);
  511. data->next_work_obsz = 60;
  512. }
  513. }
  514. else
  515. if (proto == BFP_RANGE)
  516. {
  517. /* Split work up into 1/5th nonce ranges */
  518. bitforce->nonces = 0x33333332;
  519. bitforce->sleep_ms /= 5;
  520. data->sleep_ms_default /= 5;
  521. data->next_work_cmd = "ZPX";
  522. if (data->sc)
  523. {
  524. data->next_work_ob[7] = 53;
  525. data->next_work_obsz = 54;
  526. }
  527. else
  528. data->next_work_obsz = 68;
  529. }
  530. data->proto = proto;
  531. bitforce->kname = protonames[proto];
  532. }
  533. static
  534. void bitforce_job_start(struct thr_info *thr)
  535. {
  536. struct cgpu_info *bitforce = thr->cgpu;
  537. struct bitforce_data *data = bitforce->cgpu_data;
  538. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  539. int fdDev = bitforce->device->device_fd;
  540. unsigned char *ob = data->next_work_obs;
  541. char pdevbuf[0x100];
  542. struct timeval tv_now;
  543. data->result_busy_polled = 0;
  544. if (data->queued)
  545. {
  546. uint32_t delay;
  547. // get_results collected more accurate job start time
  548. mt_job_transition(thr);
  549. job_start_complete(thr);
  550. data->queued = 0;
  551. delay = (uint32_t)bitforce->sleep_ms * 1000;
  552. if (unlikely(data->already_have_results))
  553. delay = 0;
  554. timer_set_delay(&thr->tv_morework, &bitforce->work_start_tv, delay);
  555. return;
  556. }
  557. if (!fdDev)
  558. goto commerr;
  559. re_send:
  560. mutex_lock(mutexp);
  561. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, ob, data->next_work_obsz);
  562. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  563. mutex_unlock(mutexp);
  564. gettimeofday(&tv_now, NULL);
  565. timer_set_delay(&thr->tv_poll, &tv_now, WORK_CHECK_INTERVAL_MS * 1000);
  566. data->poll_func = 1;
  567. return;
  568. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  569. mutex_unlock(mutexp);
  570. switch (data->proto)
  571. {
  572. case BFP_RANGE:
  573. applog(LOG_WARNING, "%"PRIpreprv": Does not support nonce range, disabling", bitforce->proc_repr);
  574. bitforce_change_mode(bitforce, BFP_WORK);
  575. goto re_send;
  576. case BFP_QUEUE:
  577. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  578. bitforce_change_mode(bitforce, BFP_WORK);
  579. goto re_send;
  580. default:
  581. ;
  582. }
  583. applog(LOG_ERR, "%"PRIpreprv": Error: Send work reports: %s", bitforce->proc_repr, pdevbuf);
  584. goto commerr;
  585. }
  586. mt_job_transition(thr);
  587. mutex_unlock(mutexp);
  588. dbg_block_data(bitforce);
  589. gettimeofday(&tv_now, NULL);
  590. bitforce->work_start_tv = tv_now;
  591. timer_set_delay(&thr->tv_morework, &tv_now, bitforce->sleep_ms * 1000);
  592. job_start_complete(thr);
  593. return;
  594. commerr:
  595. bitforce_comm_error(thr);
  596. job_start_abort(thr, true);
  597. }
  598. static char _discardedbuf[0x10];
  599. static
  600. int bitforce_zox(struct thr_info *thr, const char *cmd)
  601. {
  602. struct cgpu_info *bitforce = thr->cgpu;
  603. struct bitforce_data *data = bitforce->cgpu_data;
  604. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  605. int fd = bitforce->device->device_fd;
  606. char *pdevbuf = &data->noncebuf[0];
  607. int count;
  608. mutex_lock(mutexp);
  609. bitforce_cmd1(fd, data->xlink_id, pdevbuf, sizeof(data->noncebuf), cmd);
  610. if (!strncasecmp(pdevbuf, "INPROCESS:", 10))
  611. BFgets(pdevbuf, sizeof(data->noncebuf), fd);
  612. if (!strncasecmp(pdevbuf, "COUNT:", 6))
  613. {
  614. count = atoi(&pdevbuf[6]);
  615. size_t cls = strlen(pdevbuf);
  616. char *pmorebuf = &pdevbuf[cls];
  617. size_t szleft = sizeof(data->noncebuf) - cls, sz;
  618. if (count && data->queued)
  619. gettimeofday(&bitforce->work_start_tv, NULL);
  620. while (true)
  621. {
  622. BFgets(pmorebuf, szleft, fd);
  623. if (!strncasecmp(pmorebuf, "OK", 2))
  624. {
  625. pmorebuf[0] = '\0'; // process expects only results
  626. break;
  627. }
  628. sz = strlen(pmorebuf);
  629. szleft -= sz;
  630. pmorebuf += sz;
  631. if (unlikely(!szleft))
  632. {
  633. // Out of buffer space somehow :(
  634. applog(LOG_DEBUG, "%"PRIpreprv": Ran out of buffer space for results, discarding extra data", bitforce->proc_repr);
  635. pmorebuf = _discardedbuf;
  636. szleft = sizeof(_discardedbuf);
  637. }
  638. }
  639. }
  640. else
  641. count = -1;
  642. mutex_unlock(mutexp);
  643. return count;
  644. }
  645. static inline char *next_line(char *);
  646. static
  647. void bitforce_job_get_results(struct thr_info *thr, struct work *work)
  648. {
  649. struct cgpu_info *bitforce = thr->cgpu;
  650. struct bitforce_data *data = bitforce->cgpu_data;
  651. int fdDev = bitforce->device->device_fd;
  652. unsigned int delay_time_ms;
  653. struct timeval elapsed;
  654. struct timeval now;
  655. char *pdevbuf = &data->noncebuf[0];
  656. bool stale;
  657. int count;
  658. gettimeofday(&now, NULL);
  659. timersub(&now, &bitforce->work_start_tv, &elapsed);
  660. bitforce->wait_ms = tv_to_ms(elapsed);
  661. bitforce->polling = true;
  662. if (!fdDev)
  663. goto commerr;
  664. stale = stale_work(work, true);
  665. if (unlikely(bitforce->wait_ms < bitforce->sleep_ms))
  666. {
  667. // We're likely here because of a work restart
  668. // Since Bitforce cannot stop a work without losing results, only do it if the current job is finding stale shares
  669. // BFP_QUEUE does not support stopping work at all
  670. if (data->proto == BFP_QUEUE || !stale)
  671. {
  672. delay_time_ms = bitforce->sleep_ms - bitforce->wait_ms;
  673. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  674. data->poll_func = 2;
  675. return;
  676. }
  677. }
  678. while (1) {
  679. if (data->already_have_results)
  680. {
  681. data->already_have_results = false;
  682. strcpy(pdevbuf, "COUNT:0");
  683. count = 1;
  684. break;
  685. }
  686. const char *cmd = (data->proto == BFP_QUEUE) ? "ZOX" : "ZFX";
  687. count = bitforce_zox(thr, cmd);
  688. gettimeofday(&now, NULL);
  689. timersub(&now, &bitforce->work_start_tv, &elapsed);
  690. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  691. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  692. tv_to_ms(elapsed), (unsigned long)BITFORCE_LONG_TIMEOUT_MS);
  693. goto out;
  694. }
  695. if (count > 0)
  696. {
  697. // Check that queue results match the current work
  698. // Also, if there are results from the next work, short-circuit this wait
  699. unsigned char midstate[32], datatail[12];
  700. char *p;
  701. int i;
  702. p = pdevbuf;
  703. for (i = 0; i < count; ++i)
  704. {
  705. p = next_line(p);
  706. hex2bin(midstate, p, 32);
  707. hex2bin(datatail, &p[65], 12);
  708. if (!(memcmp(work->midstate, midstate, 32) || memcmp(&work->data[64], datatail, 12)))
  709. break;
  710. }
  711. if (i == count)
  712. {
  713. // Didn't find the one we're waiting on
  714. // Must be extra stuff in the queue results
  715. applog(LOG_WARNING, "%"PRIpreprv": Found extra garbage in queue results: %s",
  716. bitforce->proc_repr, pdevbuf);
  717. count = 0;
  718. }
  719. else
  720. if (i == count - 1)
  721. // Last one found is what we're looking for
  722. {}
  723. else
  724. // We finished the next job too!
  725. data->already_have_results = true;
  726. }
  727. if (!count)
  728. goto noqr;
  729. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  730. break;
  731. data->result_busy_polled = bitforce->wait_ms;
  732. if (stale && data->proto != BFP_QUEUE)
  733. {
  734. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  735. bitforce->proc_repr);
  736. goto out;
  737. }
  738. noqr:
  739. data->result_busy_polled = bitforce->wait_ms;
  740. /* if BFL is throttling, no point checking so quickly */
  741. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  742. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  743. data->poll_func = 2;
  744. return;
  745. }
  746. if (count < 0 && pdevbuf[0] == 'N')
  747. count = strncasecmp(pdevbuf, "NONCE-FOUND", 11) ? 1 : 0;
  748. // At this point, 'count' is:
  749. // negative, in case of some kind of error
  750. // zero, if NO-NONCE (FPGA either completed with no results, or rebooted)
  751. // positive, if at least one job completed successfully
  752. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  753. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  754. tv_to_ms(elapsed), (unsigned long)BITFORCE_TIMEOUT_MS);
  755. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  756. ++bitforce->hw_errors;
  757. ++hw_errors;
  758. /* If the device truly throttled, it didn't process the job and there
  759. * are no results. But check first, just in case we're wrong about it
  760. * throttling.
  761. */
  762. if (count > 0)
  763. goto out;
  764. } else if (count >= 0) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  765. /* Simple timing adjustment. Allow a few polls to cope with
  766. * OS timer delays being variably reliable. wait_ms will
  767. * always equal sleep_ms when we've waited greater than or
  768. * equal to the result return time.*/
  769. delay_time_ms = bitforce->sleep_ms;
  770. if (!data->result_busy_polled)
  771. {
  772. // No busy polls before results received
  773. if (bitforce->wait_ms > delay_time_ms + (WORK_CHECK_INTERVAL_MS * 8))
  774. // ... due to poll being rather late; ignore it as an anomaly
  775. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, later than scheduled %ums (ignoring)",
  776. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms);
  777. else
  778. if (bitforce->sleep_ms > data->sleep_ms_default + (BITFORCE_CHECK_INTERVAL_MS * 0x20))
  779. {
  780. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on delayed schedule %ums; Wait time changed to: %ums (default sch)",
  781. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, data->sleep_ms_default);
  782. bitforce->sleep_ms = data->sleep_ms_default;
  783. }
  784. else
  785. {
  786. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on default schedule %ums; Wait time changed to: %ums (check interval)",
  787. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, BITFORCE_CHECK_INTERVAL_MS);
  788. bitforce->sleep_ms = BITFORCE_CHECK_INTERVAL_MS;
  789. }
  790. }
  791. else
  792. {
  793. if (data->result_busy_polled - bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  794. {
  795. bitforce->sleep_ms = data->result_busy_polled - (WORK_CHECK_INTERVAL_MS / 2);
  796. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time changed to: %ums",
  797. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms, bitforce->sleep_ms);
  798. }
  799. else
  800. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time unchanged",
  801. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms);
  802. }
  803. /* Work out the average time taken. Float for calculation, uint for display */
  804. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  805. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  806. }
  807. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  808. if (count < 0 && strncasecmp(pdevbuf, "I", 1)) {
  809. bitforce->hw_errors++;
  810. ++hw_errors;
  811. applog(LOG_WARNING, "%"PRIpreprv": Error: Get result reports: %s", bitforce->proc_repr, pdevbuf);
  812. bitforce_clear_buffer(bitforce);
  813. }
  814. out:
  815. bitforce->polling = false;
  816. job_results_fetched(thr);
  817. return;
  818. commerr:
  819. bitforce_comm_error(thr);
  820. goto out;
  821. }
  822. static
  823. void bitforce_process_result_nonces(struct thr_info *thr, struct work *work, char *pnoncebuf)
  824. {
  825. struct cgpu_info *bitforce = thr->cgpu;
  826. struct bitforce_data *data = bitforce->cgpu_data;
  827. uint32_t nonce;
  828. while (1) {
  829. hex2bin((void*)&nonce, pnoncebuf, 4);
  830. nonce = be32toh(nonce);
  831. if (unlikely(data->proto == BFP_RANGE && (nonce >= work->blk.nonce ||
  832. /* FIXME: blk.nonce is probably moved on quite a bit now! */
  833. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  834. applog(LOG_WARNING, "%"PRIpreprv": Disabling broken nonce range support", bitforce->proc_repr);
  835. bitforce_change_mode(bitforce, BFP_WORK);
  836. }
  837. submit_nonce(thr, work, nonce);
  838. if (strncmp(&pnoncebuf[8], ",", 1))
  839. break;
  840. pnoncebuf += 9;
  841. }
  842. }
  843. static
  844. bool bitforce_process_qresult_line_i(struct thr_info *thr, char *midstate, char *datatail, char *buf, struct work *work)
  845. {
  846. if (!work)
  847. return false;
  848. if (memcmp(work->midstate, midstate, 32))
  849. return false;
  850. if (memcmp(&work->data[64], datatail, 12))
  851. return false;
  852. if (!atoi(&buf[90]))
  853. return true;
  854. bitforce_process_result_nonces(thr, work, &buf[92]);
  855. return true;
  856. }
  857. static
  858. void bitforce_process_qresult_line(struct thr_info *thr, char *buf, struct work *work)
  859. {
  860. struct cgpu_info *bitforce = thr->cgpu;
  861. char midstate[32], datatail[12];
  862. hex2bin((void*)midstate, buf, 32);
  863. hex2bin((void*)datatail, &buf[65], 12);
  864. if (!( bitforce_process_qresult_line_i(thr, midstate, datatail, buf, work)
  865. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->work)
  866. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->prev_work)
  867. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->next_work) ))
  868. {
  869. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queued results", bitforce->proc_repr);
  870. ++bitforce->hw_errors;
  871. ++hw_errors;
  872. }
  873. }
  874. static inline
  875. char *next_line(char *in)
  876. {
  877. while (in[0] && (in++)[0] != '\n')
  878. {}
  879. return in;
  880. }
  881. static
  882. int64_t bitforce_job_process_results(struct thr_info *thr, struct work *work, __maybe_unused bool stopping)
  883. {
  884. struct cgpu_info *bitforce = thr->cgpu;
  885. struct bitforce_data *data = bitforce->cgpu_data;
  886. char *pnoncebuf = &data->noncebuf[0];
  887. int count;
  888. if (!strncasecmp(pnoncebuf, "NO-", 3))
  889. return bitforce->nonces; /* No valid nonce found */
  890. if (!strncasecmp(pnoncebuf, "NONCE-FOUND", 11))
  891. {
  892. bitforce_process_result_nonces(thr, work, &pnoncebuf[12]);
  893. count = 1;
  894. }
  895. else
  896. if (!strncasecmp(pnoncebuf, "COUNT:", 6))
  897. {
  898. count = 0;
  899. pnoncebuf = next_line(pnoncebuf);
  900. while (pnoncebuf[0])
  901. {
  902. bitforce_process_qresult_line(thr, pnoncebuf, work);
  903. ++count;
  904. pnoncebuf = next_line(pnoncebuf);
  905. }
  906. }
  907. else
  908. return 0;
  909. // FIXME: This might have changed in the meantime (new job start, or broken)
  910. return bitforce->nonces * count;
  911. }
  912. static void bitforce_shutdown(struct thr_info *thr)
  913. {
  914. struct cgpu_info *bitforce = thr->cgpu;
  915. int *p_fdDev = &bitforce->device->device_fd;
  916. BFclose(*p_fdDev);
  917. *p_fdDev = 0;
  918. }
  919. static void biforce_thread_enable(struct thr_info *thr)
  920. {
  921. struct cgpu_info *bitforce = thr->cgpu;
  922. bitforce_init(bitforce);
  923. }
  924. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  925. {
  926. return bitforce_get_temp(bitforce);
  927. }
  928. static bool bitforce_identify(struct cgpu_info *bitforce)
  929. {
  930. bitforce->flash_led = true;
  931. return true;
  932. }
  933. static bool bitforce_thread_init(struct thr_info *thr)
  934. {
  935. struct cgpu_info *bitforce = thr->cgpu;
  936. unsigned int wait;
  937. struct bitforce_data *data;
  938. struct bitforce_init_data *initdata = bitforce->cgpu_data;
  939. bool sc = initdata->sc;
  940. int xlink_id = 0;
  941. for ( ; bitforce; bitforce = bitforce->next_proc)
  942. {
  943. thr = bitforce->thr[0];
  944. if (unlikely(xlink_id > 30))
  945. {
  946. applog(LOG_ERR, "%"PRIpreprv": Failed to find XLINK address", bitforce->proc_repr);
  947. dev_error(bitforce, REASON_THREAD_FAIL_INIT);
  948. bitforce->reinit_backoff = 1e10;
  949. continue;
  950. }
  951. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  952. bitforce->cgpu_data = data = malloc(sizeof(*data));
  953. *data = (struct bitforce_data){
  954. .xlink_id = xlink_id,
  955. .next_work_ob = ">>>>>>>>|---------- MidState ----------||-DataTail-||Nonces|>>>>>>>>",
  956. .proto = BFP_RANGE,
  957. .sc = sc,
  958. .sleep_ms_default = BITFORCE_SLEEP_MS,
  959. };
  960. if (sc)
  961. {
  962. // ".......S|---------- MidState ----------||-DataTail-||Nonces|E"
  963. data->next_work_ob[8+32+12+8] = '\xAA';
  964. data->next_work_obs = &data->next_work_ob[7];
  965. if (bitforce->api == &bitforce_queue_api)
  966. {
  967. INIT_LIST_HEAD(&thr->work_list);
  968. bitforce_change_mode(bitforce, BFP_BQUEUE);
  969. timer_set_delay_from_now(&thr->tv_poll, 0);
  970. }
  971. else
  972. bitforce_change_mode(bitforce, BFP_QUEUE);
  973. }
  974. else
  975. {
  976. data->next_work_obs = &data->next_work_ob[0];
  977. // Unconditionally change away from cold-initialized BFP_RANGE, to allow for setting up other variables
  978. bitforce_change_mode(bitforce, BFP_WORK);
  979. /* Initially enable support for nonce range and disable it later if it
  980. * fails */
  981. if (opt_bfl_noncerange)
  982. bitforce_change_mode(bitforce, BFP_RANGE);
  983. }
  984. while (xlink_id < 31 && !(initdata->devmask & (1 << ++xlink_id)))
  985. {}
  986. }
  987. bitforce = thr->cgpu;
  988. /* Pause each new thread at least 100ms between initialising
  989. * so the devices aren't making calls all at the same time. */
  990. wait = thr->id * MAX_START_DELAY_MS;
  991. applog(LOG_DEBUG, "%s: Delaying start by %dms", bitforce->dev_repr, wait / 1000);
  992. nmsleep(wait);
  993. return true;
  994. }
  995. static struct api_data *bitforce_api_stats(struct cgpu_info *cgpu)
  996. {
  997. struct api_data *root = NULL;
  998. // Warning, access to these is not locked - but we don't really
  999. // care since hashing performance is way more important than
  1000. // locking access to displaying API debug 'stats'
  1001. // If locking becomes an issue for any of them, use copy_data=true also
  1002. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  1003. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  1004. return root;
  1005. }
  1006. void bitforce_poll(struct thr_info *thr)
  1007. {
  1008. struct cgpu_info *bitforce = thr->cgpu;
  1009. struct bitforce_data *data = bitforce->cgpu_data;
  1010. int poll = data->poll_func;
  1011. thr->tv_poll.tv_sec = -1;
  1012. data->poll_func = 0;
  1013. switch (poll)
  1014. {
  1015. case 1:
  1016. bitforce_job_start(thr);
  1017. break;
  1018. case 2:
  1019. bitforce_job_get_results(thr, thr->work);
  1020. break;
  1021. default:
  1022. applog(LOG_ERR, "%"PRIpreprv": Unexpected poll from device API!", thr->cgpu->proc_repr);
  1023. }
  1024. }
  1025. struct device_api bitforce_api = {
  1026. .dname = "bitforce",
  1027. .name = "BFL",
  1028. .api_detect = bitforce_detect,
  1029. .get_api_stats = bitforce_api_stats,
  1030. .minerloop = minerloop_async,
  1031. .reinit_device = bitforce_init,
  1032. .get_statline_before = get_bitforce_statline_before,
  1033. .get_stats = bitforce_get_stats,
  1034. .identify_device = bitforce_identify,
  1035. .thread_prepare = bitforce_thread_prepare,
  1036. .thread_init = bitforce_thread_init,
  1037. .job_prepare = bitforce_job_prepare,
  1038. .job_start = bitforce_job_start,
  1039. .job_get_results = bitforce_job_get_results,
  1040. .poll = bitforce_poll,
  1041. .job_process_results = bitforce_job_process_results,
  1042. .thread_shutdown = bitforce_shutdown,
  1043. .thread_enable = biforce_thread_enable
  1044. };
  1045. static inline
  1046. void bitforce_set_queue_full(struct thr_info *thr)
  1047. {
  1048. struct cgpu_info *bitforce = thr->cgpu;
  1049. struct bitforce_data *data = bitforce->cgpu_data;
  1050. thr->queue_full = (data->queued + data->ready_to_queue >= BITFORCE_MAX_QUEUED);
  1051. }
  1052. static
  1053. bool bitforce_send_queue(struct thr_info *thr)
  1054. {
  1055. struct cgpu_info *bitforce = thr->cgpu;
  1056. struct bitforce_data *data = bitforce->cgpu_data;
  1057. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  1058. int fd = bitforce->device->device_fd;
  1059. struct work *work;
  1060. struct list_head *pos;
  1061. if (unlikely(!(fd && data->ready_to_queue)))
  1062. return false;
  1063. char buf[0x100];
  1064. int queued_ok;
  1065. size_t qjs_sz = (32 + 12 + 2);
  1066. size_t qjp_sz = 4 + (qjs_sz * data->ready_to_queue);
  1067. uint8_t qjp[qjp_sz], *qjs;
  1068. qjp[0] = qjp_sz - 1;
  1069. qjp[1] = 0xc1;
  1070. qjp[2] = data->ready_to_queue;
  1071. qjp[qjp_sz - 1] = 0xfe;
  1072. qjs = &qjp[qjp_sz - 1];
  1073. pos = thr->work_list.prev;
  1074. for (int i = data->ready_to_queue; i > 0; --i, pos = pos->prev)
  1075. {
  1076. work = list_entry(pos, typeof(*work), list);
  1077. *(--qjs) = 0xaa;
  1078. memcpy(qjs -= 12, work->data + 64, 12);
  1079. memcpy(qjs -= 32, work->midstate, 32);
  1080. *(--qjs) = 45;
  1081. }
  1082. mutex_lock(mutexp);
  1083. bitforce_cmd2(fd, data->xlink_id, buf, sizeof(buf), "ZWX", qjp, qjp_sz);
  1084. mutex_unlock(mutexp);
  1085. if (!strncasecmp(buf, "ERR:QUEUE", 9))
  1086. {
  1087. // Queue full :(
  1088. applog(LOG_DEBUG, "%"PRIpreprv": Device queue full while attempting to append %d jobs (queued<=%d)",
  1089. bitforce->proc_repr,
  1090. data->ready_to_queue, data->queued);
  1091. thr->queue_full = true;
  1092. return false;
  1093. }
  1094. if (strncasecmp(buf, "OK:QUEUED", 9))
  1095. {
  1096. // TODO: ERR:UNKNOWN COMMAND
  1097. applog(LOG_DEBUG, "%"PRIpreprv": Unexpected error attempting to append %d jobs (queued<=%d): %s",
  1098. bitforce->proc_repr,
  1099. data->ready_to_queue, data->queued, buf);
  1100. return false;
  1101. }
  1102. queued_ok = atoi(&buf[9]);
  1103. data->queued += queued_ok;
  1104. applog(LOG_DEBUG, "%"PRIpreprv": Successfully queued %d/%d jobs on device (queued<=%d)",
  1105. bitforce->proc_repr,
  1106. queued_ok, data->ready_to_queue, data->queued);
  1107. data->ready_to_queue -= queued_ok;
  1108. thr->queue_full = data->ready_to_queue;
  1109. data->just_flushed = false;
  1110. return true;
  1111. }
  1112. void work_list_del(struct list_head *pos)
  1113. {
  1114. struct work *work;
  1115. work = list_entry(pos, typeof(*work), list);
  1116. list_del(pos);
  1117. free_work(work);
  1118. }
  1119. static
  1120. bool bitforce_queue_do_results(struct thr_info *thr)
  1121. {
  1122. struct cgpu_info *bitforce = thr->cgpu;
  1123. struct bitforce_data *data = bitforce->cgpu_data;
  1124. int fd = bitforce->device->device_fd;
  1125. int count;
  1126. char *buf = &data->noncebuf[0];
  1127. unsigned char midstate[32], datatail[12];
  1128. struct work *work;
  1129. struct list_head *pos, *next_pos;
  1130. struct timeval tv_now, tv_elapsed;
  1131. if (unlikely(!fd))
  1132. return false;
  1133. count = bitforce_zox(thr, "ZOX");
  1134. if (!count)
  1135. return true;
  1136. if (unlikely(count < 0))
  1137. {
  1138. applog(LOG_ERR, "%"PRIpreprv": Received unexpected queue result response: %s", bitforce->proc_repr, buf);
  1139. ++bitforce->hw_errors;
  1140. ++hw_errors;
  1141. return false;
  1142. }
  1143. if (unlikely(list_empty(&thr->work_list)))
  1144. {
  1145. applog(LOG_ERR, "%"PRIpreprv": Received %d queued results when there was no queue", bitforce->proc_repr, count);
  1146. ++bitforce->hw_errors;
  1147. ++hw_errors;
  1148. return true;
  1149. }
  1150. if (count != BITFORCE_GOAL_QRESULTS)
  1151. {
  1152. unsigned int old_sleep_ms = bitforce->sleep_ms;
  1153. bitforce->sleep_ms = (uint32_t)bitforce->sleep_ms * BITFORCE_GOAL_QRESULTS / (count ?: 1);
  1154. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time changed to: %ums",
  1155. bitforce->proc_repr, count, old_sleep_ms, bitforce->sleep_ms);
  1156. }
  1157. else
  1158. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time unchanged",
  1159. bitforce->proc_repr, count, bitforce->sleep_ms);
  1160. count = 0;
  1161. while ((buf = next_line(buf)), buf[0])
  1162. {
  1163. if (strlen(buf) <= 90)
  1164. {
  1165. applog(LOG_ERR, "%"PRIpreprv": Gibberish within queue results: %s", bitforce->proc_repr, buf);
  1166. continue;
  1167. }
  1168. hex2bin(midstate, buf, 32);
  1169. hex2bin(datatail, &buf[65], 12);
  1170. for (pos = thr->work_list.next; ; pos = pos->next)
  1171. {
  1172. if (unlikely(pos == &thr->work_list))
  1173. {
  1174. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queue results", bitforce->proc_repr);
  1175. ++bitforce->hw_errors;
  1176. ++hw_errors;
  1177. goto next_qline;
  1178. }
  1179. work = list_entry(pos, typeof(*work), list);
  1180. if (unlikely(memcmp(work->midstate, midstate, 32)))
  1181. continue;
  1182. if (unlikely(memcmp(&work->data[64], datatail, 12)))
  1183. continue;
  1184. break;
  1185. }
  1186. ++count;
  1187. if (atoi(&buf[90]))
  1188. bitforce_process_result_nonces(thr, work, &buf[92]);
  1189. // Queue results are in order, so anything queued prior this is lost
  1190. // Delete all queued work up to, and including, this one
  1191. for ( ; pos != &thr->work_list; pos = next_pos)
  1192. {
  1193. next_pos = pos->prev;
  1194. work_list_del(pos);
  1195. --data->queued;
  1196. }
  1197. next_qline: (void)0;
  1198. }
  1199. bitforce_set_queue_full(thr);
  1200. gettimeofday(&tv_now, NULL);
  1201. timersub(&tv_now, &data->tv_hashmeter_start, &tv_elapsed);
  1202. hashes_done(thr, (uint64_t)bitforce->nonces * count, &tv_elapsed, NULL);
  1203. data->tv_hashmeter_start = tv_now;
  1204. return true;
  1205. }
  1206. static
  1207. bool bitforce_queue_append(struct thr_info *thr, struct work *work)
  1208. {
  1209. struct cgpu_info *bitforce = thr->cgpu;
  1210. struct bitforce_data *data = bitforce->cgpu_data;
  1211. bool rv, ndq;
  1212. bitforce_set_queue_full(thr);
  1213. rv = !thr->queue_full;
  1214. if (rv)
  1215. {
  1216. list_add_tail(&work->list, &thr->work_list);
  1217. ++data->ready_to_queue;
  1218. applog(LOG_DEBUG, "%"PRIpreprv": Appending to driver queue (max=%u, ready=%d, queued<=%d)",
  1219. bitforce->proc_repr,
  1220. (unsigned)BITFORCE_MAX_QUEUED, data->ready_to_queue, data->queued);
  1221. bitforce_set_queue_full(thr);
  1222. }
  1223. ndq = !data->queued;
  1224. if ((ndq) // Device is idle
  1225. || (data->ready_to_queue >= 5) // ...or 5 items ready to go
  1226. || (thr->queue_full) // ...or done filling queue
  1227. || (data->just_flushed) // ...or queue was just flushed (only remaining job is partly done already)
  1228. )
  1229. {
  1230. bitforce_send_queue(thr);
  1231. if (ndq)
  1232. gettimeofday(&data->tv_hashmeter_start, NULL);
  1233. }
  1234. return rv;
  1235. }
  1236. static
  1237. void bitforce_queue_flush(struct thr_info *thr)
  1238. {
  1239. struct cgpu_info *bitforce = thr->cgpu;
  1240. struct bitforce_data *data = bitforce->cgpu_data;
  1241. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  1242. int fd = bitforce->device->device_fd;
  1243. char buf[100];
  1244. unsigned flushed;
  1245. mutex_lock(mutexp);
  1246. bitforce_cmd1(fd, data->xlink_id, buf, sizeof(buf), "ZQX");
  1247. mutex_unlock(mutexp);
  1248. if (unlikely(strncasecmp(buf, "OK:FLUSHED", 10)))
  1249. {
  1250. applog(LOG_DEBUG, "%"PRIpreprv": Failed to flush device queue: %s", bitforce->proc_repr, buf);
  1251. flushed = 0;
  1252. }
  1253. else
  1254. flushed = atoi(&buf[10]);
  1255. applog(LOG_DEBUG, "%"PRIpreprv": Flushed %u jobs from device and %d from driver",
  1256. bitforce->proc_repr, flushed, data->ready_to_queue);
  1257. data->queued -= flushed;
  1258. flushed += data->ready_to_queue;
  1259. data->ready_to_queue = 0;
  1260. while (flushed--)
  1261. work_list_del(thr->work_list.prev);
  1262. thr->queue_full = false;
  1263. data->just_flushed = true;
  1264. bitforce_queue_do_results(thr);
  1265. }
  1266. static
  1267. void bitforce_queue_poll(struct thr_info *thr)
  1268. {
  1269. struct cgpu_info *bitforce = thr->cgpu;
  1270. bitforce_queue_do_results(thr);
  1271. timer_set_delay_from_now(&thr->tv_poll, bitforce->sleep_ms * 1000);
  1272. }
  1273. struct device_api bitforce_queue_api = {
  1274. .dname = "bitforce_queue",
  1275. .name = "BFL",
  1276. .minerloop = minerloop_queue,
  1277. .reinit_device = bitforce_init,
  1278. .get_statline_before = get_bitforce_statline_before,
  1279. .get_stats = bitforce_get_stats,
  1280. .identify_device = bitforce_identify,
  1281. .thread_prepare = bitforce_thread_prepare,
  1282. .thread_init = bitforce_thread_init,
  1283. .queue_append = bitforce_queue_append,
  1284. .queue_flush = bitforce_queue_flush,
  1285. .poll = bitforce_queue_poll,
  1286. .thread_shutdown = bitforce_shutdown,
  1287. .thread_enable = biforce_thread_enable
  1288. };