driver-bitforce.c 44 KB

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