driver-bitforce.c 41 KB

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