driver-bitforce.c 45 KB

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