driver-bitforce.c 42 KB

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