driver-bitforce.c 43 KB

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