driver-bitforce.c 42 KB

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