driver-bitforce.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079
  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 <ctype.h>
  12. #include <limits.h>
  13. #include <pthread.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <strings.h>
  18. #include <sys/time.h>
  19. #include <unistd.h>
  20. #include "compat.h"
  21. #include "deviceapi.h"
  22. #include "miner.h"
  23. #include "fpgautils.h"
  24. #include "util.h"
  25. #define BITFORCE_SLEEP_MS 500
  26. #define BITFORCE_TIMEOUT_S 7
  27. #define BITFORCE_TIMEOUT_MS (BITFORCE_TIMEOUT_S * 1000)
  28. #define BITFORCE_LONG_TIMEOUT_S 25
  29. #define BITFORCE_LONG_TIMEOUT_MS (BITFORCE_LONG_TIMEOUT_S * 1000)
  30. #define BITFORCE_CHECK_INTERVAL_MS 10
  31. #define WORK_CHECK_INTERVAL_MS 50
  32. #define MAX_START_DELAY_MS 100
  33. #define tv_to_ms(tval) ((unsigned long)(tval.tv_sec * 1000 + tval.tv_usec / 1000))
  34. #define TIME_AVG_CONSTANT 8
  35. #define BITFORCE_QRESULT_LINE_LEN 165
  36. #define BITFORCE_MAX_QUEUED_MAX 40
  37. #define BITFORCE_MIN_QUEUED_MAX 10
  38. #define BITFORCE_MAX_QRESULTS 16
  39. #define BITFORCE_GOAL_QRESULTS 5
  40. #define BITFORCE_MIN_QRESULT_WAIT BITFORCE_CHECK_INTERVAL_MS
  41. #define BITFORCE_MAX_QRESULT_WAIT 1000
  42. #define BITFORCE_MAX_BQUEUE_AT_ONCE 5
  43. enum bitforce_proto {
  44. BFP_WORK,
  45. BFP_RANGE,
  46. BFP_QUEUE,
  47. BFP_BQUEUE,
  48. BFP_PQUEUE,
  49. };
  50. static const char *protonames[] = {
  51. "full work",
  52. "nonce range",
  53. "work queue",
  54. "bulk queue",
  55. "parallel queue",
  56. };
  57. struct device_drv bitforce_drv;
  58. struct device_drv bitforce_queue_api;
  59. // Code must deal with a timeout
  60. #define BFopen(devpath) serial_open(devpath, 0, 250, true)
  61. static void BFgets(char *buf, size_t bufLen, int fd)
  62. {
  63. char *obuf = buf;
  64. do {
  65. buf[0] = '\0';
  66. --bufLen;
  67. } while (likely(bufLen && read(fd, buf, 1) == 1 && (buf++)[0] != '\n'));
  68. buf[0] = '\0';
  69. if (unlikely(opt_dev_protocol))
  70. applog(LOG_DEBUG, "DEVPROTO: GETS (fd=%d): %s", fd, obuf);
  71. }
  72. static ssize_t BFwrite(int fd, const void *buf, ssize_t bufLen)
  73. {
  74. if ((bufLen) != write(fd, buf, bufLen))
  75. return 0;
  76. else
  77. return bufLen;
  78. }
  79. static ssize_t bitforce_send(int fd, int procid, const void *buf, ssize_t bufLen)
  80. {
  81. if (!procid)
  82. return BFwrite(fd, buf, bufLen);
  83. if (bufLen > 255)
  84. return -1;
  85. size_t bufLeft = bufLen + 3;
  86. char realbuf[bufLeft], *bufp;
  87. ssize_t rv;
  88. memcpy(&realbuf[3], buf, bufLen);
  89. realbuf[0] = '@';
  90. realbuf[1] = bufLen;
  91. realbuf[2] = procid;
  92. bufp = realbuf;
  93. do
  94. {
  95. rv = BFwrite(fd, bufp, bufLeft);
  96. if (rv <= 0)
  97. return rv;
  98. bufLeft -= rv;
  99. }
  100. while (bufLeft > 0);
  101. return bufLen;
  102. }
  103. static
  104. void bitforce_cmd1b(int fd, int procid, void *buf, size_t bufsz, const char *cmd, size_t cmdsz)
  105. {
  106. if (unlikely(opt_dev_protocol))
  107. applog(LOG_DEBUG, "DEVPROTO: CMD1 (fd=%d xlink=%d): %s", fd, procid, cmd);
  108. bitforce_send(fd, procid, cmd, cmdsz);
  109. BFgets(buf, bufsz, fd);
  110. }
  111. #define bitforce_cmd1(fd, xlinkid, buf, bufsz, cmd) bitforce_cmd1b(fd, xlinkid, buf, bufsz, cmd, 3)
  112. static
  113. void bitforce_cmd2(int fd, int procid, void *buf, size_t bufsz, const char *cmd, void *data, size_t datasz)
  114. {
  115. bitforce_cmd1(fd, procid, buf, bufsz, cmd);
  116. if (strncasecmp(buf, "OK", 2))
  117. return;
  118. if (unlikely(opt_dev_protocol))
  119. {
  120. char hex[(datasz * 2) + 1];
  121. bin2hex(hex, data, datasz);
  122. applog(LOG_DEBUG, "DEVPROTO: CMD2 (fd=%d xlink=%d): %s", fd, procid, hex);
  123. }
  124. bitforce_send(fd, procid, data, datasz);
  125. BFgets(buf, bufsz, fd);
  126. }
  127. #define BFclose(fd) serial_close(fd)
  128. struct bitforce_init_data {
  129. bool sc;
  130. long devmask;
  131. int *parallels;
  132. };
  133. static
  134. int bitforce_chips_to_plan_for(int parallel, int chipcount) {
  135. if (parallel < 1)
  136. return parallel;
  137. if (chipcount > 15) return 32;
  138. if (chipcount > 7) return 16;
  139. if (chipcount > 3) return 8;
  140. if (chipcount > 1) return 4;
  141. if (chipcount ) return 2;
  142. return 1;
  143. }
  144. static bool bitforce_detect_one(const char *devpath)
  145. {
  146. int fdDev = serial_open(devpath, 0, 10, true);
  147. struct cgpu_info *bitforce;
  148. char pdevbuf[0x100];
  149. size_t pdevbuf_len;
  150. char *s;
  151. int procs = 1, parallel = -1;
  152. long maxchipno = 0;
  153. struct bitforce_init_data *initdata;
  154. applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
  155. if (unlikely(fdDev == -1)) {
  156. applog(LOG_DEBUG, "BFL: Failed to open %s", devpath);
  157. return false;
  158. }
  159. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  160. if (unlikely(!pdevbuf[0])) {
  161. applog(LOG_DEBUG, "BFL: Error reading/timeout (ZGX)");
  162. return 0;
  163. }
  164. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  165. applog(LOG_DEBUG, "BFL: Didn't recognise BitForce on %s", devpath);
  166. BFclose(fdDev);
  167. return false;
  168. }
  169. if (serial_claim_v(devpath, &bitforce_drv))
  170. return false;
  171. applog(LOG_DEBUG, "Found BitForce device on %s", devpath);
  172. initdata = malloc(sizeof(*initdata));
  173. *initdata = (struct bitforce_init_data){
  174. .sc = false,
  175. };
  176. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZCX");
  177. for (int i = 0; (!pdevbuf[0]) && i < 4; ++i)
  178. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  179. for ( ;
  180. strncasecmp(pdevbuf, "OK", 2);
  181. BFgets(pdevbuf, sizeof(pdevbuf), fdDev) )
  182. {
  183. pdevbuf_len = strlen(pdevbuf);
  184. if (unlikely(!pdevbuf_len))
  185. continue;
  186. pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
  187. applog(LOG_DEBUG, " %s", pdevbuf);
  188. if (!strncasecmp(pdevbuf, "PROCESSOR ", 10))
  189. maxchipno = max(maxchipno, atoi(&pdevbuf[10]));
  190. else
  191. if (!strncasecmp(pdevbuf, "DEVICES IN CHAIN:", 17))
  192. procs = atoi(&pdevbuf[17]);
  193. else
  194. if (!strncasecmp(pdevbuf, "CHAIN PRESENCE MASK:", 20))
  195. initdata->devmask = strtol(&pdevbuf[20], NULL, 16);
  196. else
  197. if (!strncasecmp(pdevbuf, "DEVICE:", 7) && strstr(pdevbuf, "SC"))
  198. initdata->sc = true;
  199. else
  200. if (!strncasecmp(pdevbuf, "CHIP PARALLELIZATION: YES @", 27))
  201. parallel = atoi(&pdevbuf[27]);
  202. }
  203. parallel = bitforce_chips_to_plan_for(parallel, maxchipno);
  204. initdata->parallels = malloc(sizeof(initdata->parallels[0]) * procs);
  205. initdata->parallels[0] = parallel;
  206. parallel = abs(parallel);
  207. for (int proc = 1; proc < procs; ++proc)
  208. {
  209. applog(LOG_DEBUG, "Slave board %d:", proc);
  210. initdata->parallels[proc] = -1;
  211. maxchipno = 0;
  212. bitforce_cmd1(fdDev, proc, pdevbuf, sizeof(pdevbuf), "ZCX");
  213. for (int i = 0; (!pdevbuf[0]) && i < 4; ++i)
  214. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  215. for ( ;
  216. strncasecmp(pdevbuf, "OK", 2);
  217. BFgets(pdevbuf, sizeof(pdevbuf), fdDev) )
  218. {
  219. pdevbuf_len = strlen(pdevbuf);
  220. if (unlikely(!pdevbuf_len))
  221. continue;
  222. pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
  223. applog(LOG_DEBUG, " %s", pdevbuf);
  224. if (!strncasecmp(pdevbuf, "PROCESSOR ", 10))
  225. maxchipno = max(maxchipno, atoi(&pdevbuf[10]));
  226. else
  227. if (!strncasecmp(pdevbuf, "CHIP PARALLELIZATION: YES @", 27))
  228. initdata->parallels[proc] = atoi(&pdevbuf[27]);
  229. }
  230. initdata->parallels[proc] = bitforce_chips_to_plan_for(initdata->parallels[proc], maxchipno);
  231. parallel += abs(initdata->parallels[proc]);
  232. }
  233. BFclose(fdDev);
  234. if (unlikely((procs != 1 || parallel != 1) && !initdata->sc))
  235. {
  236. // Only bitforce_queue supports parallelization and XLINK, so force SC mode and hope for the best
  237. applog(LOG_WARNING, "SC features detected with non-SC device; this is not supported!");
  238. initdata->sc = true;
  239. }
  240. // We have a real BitForce!
  241. bitforce = calloc(1, sizeof(*bitforce));
  242. bitforce->drv = &bitforce_drv;
  243. if (initdata->sc)
  244. bitforce->drv = &bitforce_queue_api;
  245. bitforce->device_path = strdup(devpath);
  246. bitforce->deven = DEV_ENABLED;
  247. bitforce->procs = parallel;
  248. bitforce->threads = 1;
  249. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  250. s[0] = '\0';
  251. bitforce->name = strdup(pdevbuf + 7);
  252. }
  253. bitforce->device_data = initdata;
  254. mutex_init(&bitforce->device_mutex);
  255. return add_cgpu(bitforce);
  256. }
  257. static int bitforce_detect_auto(void)
  258. {
  259. return serial_autodetect(bitforce_detect_one, "BitFORCE", "SHA256");
  260. }
  261. static void bitforce_detect(void)
  262. {
  263. serial_detect_auto(&bitforce_drv, bitforce_detect_one, bitforce_detect_auto);
  264. }
  265. struct bitforce_data {
  266. int xlink_id;
  267. unsigned char next_work_ob[70]; // Data aligned for 32-bit access
  268. unsigned char *next_work_obs; // Start of data to send
  269. unsigned char next_work_obsz;
  270. const char *next_work_cmd;
  271. char noncebuf[14 + ((BITFORCE_MAX_QRESULTS+1) * BITFORCE_QRESULT_LINE_LEN)];
  272. int poll_func;
  273. enum bitforce_proto proto;
  274. bool sc;
  275. int queued;
  276. int queued_max;
  277. int parallel;
  278. bool parallel_protocol;
  279. bool already_have_results;
  280. bool just_flushed;
  281. int ready_to_queue;
  282. bool want_to_send_queue;
  283. unsigned result_busy_polled;
  284. unsigned sleep_ms_default;
  285. struct timeval tv_hashmeter_start;
  286. float temp[2];
  287. long *volts;
  288. int volts_count;
  289. bool probed;
  290. bool supports_fanspeed;
  291. };
  292. struct bitforce_proc_data {
  293. struct cgpu_info *cgpu;
  294. bool handles_board; // The first processor handles the queue for the entire board
  295. };
  296. static void bitforce_clear_buffer(struct cgpu_info *);
  297. static
  298. void bitforce_comm_error(struct thr_info *thr)
  299. {
  300. struct cgpu_info *bitforce = thr->cgpu;
  301. struct bitforce_data *data = bitforce->device_data;
  302. int *p_fdDev = &bitforce->device->device_fd;
  303. data->noncebuf[0] = '\0';
  304. applog(LOG_ERR, "%"PRIpreprv": Comms error", bitforce->proc_repr);
  305. dev_error(bitforce, REASON_DEV_COMMS_ERROR);
  306. inc_hw_errors_only(thr);
  307. BFclose(*p_fdDev);
  308. int fd = *p_fdDev = BFopen(bitforce->device_path);
  309. if (fd == -1)
  310. {
  311. applog(LOG_ERR, "%s: Error reopening %s", bitforce->dev_repr, bitforce->device_path);
  312. return;
  313. }
  314. /* empty read buffer */
  315. bitforce_clear_buffer(bitforce);
  316. }
  317. static bool bitforce_thread_prepare(struct thr_info *thr)
  318. {
  319. struct cgpu_info *bitforce = thr->cgpu;
  320. int fdDev = BFopen(bitforce->device_path);
  321. if (unlikely(fdDev == -1)) {
  322. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, bitforce->device_path);
  323. return false;
  324. }
  325. bitforce->device_fd = fdDev;
  326. applog(LOG_INFO, "%s: Opened %s", bitforce->dev_repr, bitforce->device_path);
  327. return true;
  328. }
  329. static void __bitforce_clear_buffer(int fdDev)
  330. {
  331. char pdevbuf[0x100];
  332. int count = 0;
  333. do {
  334. pdevbuf[0] = '\0';
  335. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  336. } while (pdevbuf[0] && (++count < 10));
  337. }
  338. static void bitforce_clear_buffer(struct cgpu_info *bitforce)
  339. {
  340. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  341. int fdDev;
  342. mutex_lock(mutexp);
  343. fdDev = bitforce->device->device_fd;
  344. if (fdDev)
  345. {
  346. applog(LOG_DEBUG, "%"PRIpreprv": Clearing read buffer", bitforce->proc_repr);
  347. __bitforce_clear_buffer(fdDev);
  348. }
  349. mutex_unlock(mutexp);
  350. }
  351. void work_list_del(struct work **head, struct work *);
  352. void bitforce_reinit(struct cgpu_info *bitforce)
  353. {
  354. struct bitforce_data *data = bitforce->device_data;
  355. struct thr_info *thr = bitforce->thr[0];
  356. struct bitforce_proc_data *procdata = thr->cgpu_data;
  357. const char *devpath = bitforce->device_path;
  358. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  359. int *p_fdDev = &bitforce->device->device_fd;
  360. int fdDev, retries = 0;
  361. char pdevbuf[0x100];
  362. char *s;
  363. if (!procdata->handles_board)
  364. return;
  365. mutex_lock(mutexp);
  366. fdDev = *p_fdDev;
  367. applog(LOG_WARNING, "%"PRIpreprv": Re-initialising", bitforce->proc_repr);
  368. if (fdDev) {
  369. BFclose(fdDev);
  370. cgsleep_ms(5000);
  371. *p_fdDev = 0;
  372. }
  373. fdDev = BFopen(devpath);
  374. if (unlikely(fdDev == -1)) {
  375. mutex_unlock(mutexp);
  376. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, devpath);
  377. return;
  378. }
  379. __bitforce_clear_buffer(fdDev);
  380. do {
  381. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  382. if (unlikely(!pdevbuf[0])) {
  383. mutex_unlock(mutexp);
  384. BFclose(fdDev);
  385. applog(LOG_ERR, "%s: Error reading/timeout (ZGX)", bitforce->dev_repr);
  386. return;
  387. }
  388. if (retries++)
  389. cgsleep_ms(10);
  390. } while (strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  391. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  392. mutex_unlock(mutexp);
  393. BFclose(fdDev);
  394. applog(LOG_ERR, "%s: Didn't recognise BitForce on %s returned: %s", bitforce->dev_repr, devpath, pdevbuf);
  395. return;
  396. }
  397. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  398. s[0] = '\0';
  399. free((void*)bitforce->name);
  400. bitforce->name = strdup(pdevbuf + 7);
  401. }
  402. *p_fdDev = fdDev;
  403. bitforce->sleep_ms = data->sleep_ms_default;
  404. if (bitforce->drv == &bitforce_queue_api)
  405. {
  406. struct work *work, *tmp;
  407. timer_set_delay_from_now(&thr->tv_poll, 0);
  408. notifier_wake(thr->notifier);
  409. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZQX");
  410. DL_FOREACH_SAFE(thr->work_list, work, tmp)
  411. work_list_del(&thr->work_list, work);
  412. data->queued = 0;
  413. data->ready_to_queue = 0;
  414. data->already_have_results = false;
  415. data->just_flushed = true;
  416. thr->queue_full = false;
  417. }
  418. mutex_unlock(mutexp);
  419. }
  420. static void bitforce_flash_led(struct cgpu_info *bitforce)
  421. {
  422. struct bitforce_data *data = bitforce->device_data;
  423. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  424. int fdDev = bitforce->device->device_fd;
  425. if (!fdDev)
  426. return;
  427. /* Do not try to flash the led if we're polling for a result to
  428. * minimise the chance of interleaved results */
  429. if (bitforce->polling)
  430. return;
  431. /* It is not critical flashing the led so don't get stuck if we
  432. * can't grab the mutex here */
  433. if (mutex_trylock(mutexp))
  434. return;
  435. char pdevbuf[0x100];
  436. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZMX");
  437. /* Once we've tried - don't do it until told to again */
  438. bitforce->flash_led = false;
  439. /* However, this stops anything else getting a reply
  440. * So best to delay any other access to the BFL */
  441. cgsleep_ms(4000);
  442. mutex_unlock(mutexp);
  443. return; // nothing is returned by the BFL
  444. }
  445. static
  446. float my_strtof(const char *nptr, char **endptr)
  447. {
  448. float f = strtof(nptr, endptr);
  449. /* Cope with older software that breaks and reads nonsense
  450. * values */
  451. if (f > 100)
  452. f = strtod(nptr, endptr);
  453. return f;
  454. }
  455. static
  456. void set_float_if_gt_zero(float *var, float value)
  457. {
  458. if (value > 0)
  459. *var = value;
  460. }
  461. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  462. {
  463. struct bitforce_data *data = bitforce->device_data;
  464. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  465. int fdDev = bitforce->device->device_fd;
  466. char pdevbuf[0x40];
  467. char voltbuf[0x40];
  468. char *s;
  469. struct cgpu_info *chip_cgpu;
  470. if (!fdDev)
  471. return false;
  472. /* Do not try to get the temperature if we're polling for a result to
  473. * minimise the chance of interleaved results */
  474. if (bitforce->polling)
  475. return true;
  476. // Flash instead of Temp - doing both can be too slow
  477. if (bitforce->flash_led) {
  478. bitforce_flash_led(bitforce);
  479. return true;
  480. }
  481. /* It is not critical getting temperature so don't get stuck if we
  482. * can't grab the mutex here */
  483. if (mutex_trylock(mutexp))
  484. return false;
  485. if (data->sc)
  486. {
  487. if (unlikely(!data->probed))
  488. {
  489. bitforce_cmd1(fdDev, data->xlink_id, voltbuf, sizeof(voltbuf), "Z9X");
  490. if (strncasecmp(voltbuf, "ERR", 3))
  491. data->supports_fanspeed = true;
  492. data->probed = true;
  493. }
  494. bitforce_cmd1(fdDev, data->xlink_id, voltbuf, sizeof(voltbuf), "ZTX");
  495. }
  496. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZLX");
  497. mutex_unlock(mutexp);
  498. if (data->sc && likely(voltbuf[0]))
  499. {
  500. // Process voltage info
  501. // "NNNxxx,NNNxxx,NNNxxx"
  502. int n = 1;
  503. for (char *p = voltbuf; p[0]; ++p)
  504. if (p[0] == ',')
  505. ++n;
  506. long *out = malloc(sizeof(long) * n);
  507. if (!out)
  508. goto skipvolts;
  509. n = 0;
  510. char *saveptr, *v;
  511. for (v = strtok_r(voltbuf, ",", &saveptr); v; v = strtok_r(NULL, ",", &saveptr))
  512. out[n++] = strtol(v, NULL, 10);
  513. data->volts_count = 0;
  514. free(data->volts);
  515. data->volts = out;
  516. data->volts_count = n;
  517. }
  518. skipvolts:
  519. if (unlikely(!pdevbuf[0])) {
  520. struct thr_info *thr = bitforce->thr[0];
  521. applog(LOG_ERR, "%"PRIpreprv": Error: Get temp returned empty string/timed out", bitforce->proc_repr);
  522. inc_hw_errors_only(thr);
  523. return false;
  524. }
  525. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  526. float temp = my_strtof(s + 1, &s);
  527. set_float_if_gt_zero(&data->temp[0], temp);
  528. for ( ; s[0]; ++s)
  529. {
  530. if (!strncasecmp(s, "TEMP", 4) && (s = strchr(&s[4], ':')))
  531. {
  532. float temp2 = my_strtof(s + 1, &s);
  533. set_float_if_gt_zero(&data->temp[1], temp2);
  534. if (temp2 > temp)
  535. temp = temp2;
  536. }
  537. }
  538. if (temp > 0)
  539. {
  540. chip_cgpu = bitforce;
  541. for (int i = 0; i < data->parallel; ++i, (chip_cgpu = chip_cgpu->next_proc))
  542. chip_cgpu->temp = temp;
  543. }
  544. } else {
  545. struct thr_info *thr = bitforce->thr[0];
  546. /* Use the temperature monitor as a kind of watchdog for when
  547. * our responses are out of sync and flush the buffer to
  548. * hopefully recover */
  549. applog(LOG_WARNING, "%"PRIpreprv": Garbled response probably throttling, clearing buffer", bitforce->proc_repr);
  550. dev_error(bitforce, REASON_DEV_THROTTLE);
  551. /* Count throttling episodes as hardware errors */
  552. inc_hw_errors_only(thr);
  553. bitforce_clear_buffer(bitforce);
  554. return false;
  555. }
  556. return true;
  557. }
  558. static inline
  559. void dbg_block_data(struct cgpu_info *bitforce)
  560. {
  561. if (!opt_debug)
  562. return;
  563. struct bitforce_data *data = bitforce->device_data;
  564. char s[89];
  565. bin2hex(s, &data->next_work_ob[8], 44);
  566. applog(LOG_DEBUG, "%"PRIpreprv": block data: %s", bitforce->proc_repr, s);
  567. }
  568. static void bitforce_change_mode(struct cgpu_info *, enum bitforce_proto);
  569. static
  570. bool bitforce_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  571. {
  572. struct cgpu_info *bitforce = thr->cgpu;
  573. struct bitforce_data *data = bitforce->device_data;
  574. int fdDev = bitforce->device->device_fd;
  575. unsigned char *ob_ms = &data->next_work_ob[8];
  576. unsigned char *ob_dt = &ob_ms[32];
  577. // If polling job_start, cancel it
  578. if (data->poll_func == 1)
  579. {
  580. thr->tv_poll.tv_sec = -1;
  581. data->poll_func = 0;
  582. }
  583. memcpy(ob_ms, work->midstate, 32);
  584. memcpy(ob_dt, work->data + 64, 12);
  585. switch (data->proto)
  586. {
  587. case BFP_BQUEUE:
  588. quithere(1, "%"PRIpreprv": Impossible BFP_BQUEUE", bitforce->proc_repr);
  589. case BFP_PQUEUE:
  590. quithere(1, "%"PRIpreprv": Impossible BFP_PQUEUE", bitforce->proc_repr);
  591. case BFP_RANGE:
  592. {
  593. uint32_t *ob_nonce = (uint32_t*)&(ob_dt[32]);
  594. ob_nonce[0] = htobe32(work->blk.nonce);
  595. ob_nonce[1] = htobe32(work->blk.nonce + bitforce->nonces);
  596. // FIXME: if nonce range fails... we didn't increment enough
  597. work->blk.nonce += bitforce->nonces + 1;
  598. break;
  599. }
  600. case BFP_QUEUE:
  601. if (thr->work)
  602. {
  603. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  604. char pdevbuf[0x100];
  605. if (unlikely(!fdDev))
  606. return false;
  607. mutex_lock(mutexp);
  608. if (data->queued)
  609. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZQX");
  610. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, data->next_work_obs, data->next_work_obsz);
  611. mutex_unlock(mutexp);
  612. if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  613. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  614. bitforce_change_mode(bitforce, BFP_WORK);
  615. }
  616. else
  617. {
  618. dbg_block_data(bitforce);
  619. data->queued = 1;
  620. }
  621. }
  622. // fallthru...
  623. case BFP_WORK:
  624. work->blk.nonce = 0xffffffff;
  625. }
  626. return true;
  627. }
  628. static
  629. void bitforce_change_mode(struct cgpu_info *bitforce, enum bitforce_proto proto)
  630. {
  631. struct bitforce_data *data = bitforce->device_data;
  632. if (data->proto == proto)
  633. return;
  634. if (data->proto == BFP_RANGE)
  635. {
  636. bitforce->nonces = 0xffffffff;
  637. bitforce->sleep_ms *= 5;
  638. data->sleep_ms_default *= 5;
  639. switch (proto)
  640. {
  641. case BFP_WORK:
  642. data->next_work_cmd = "ZDX";
  643. break;
  644. case BFP_QUEUE:
  645. data->next_work_cmd = "ZNX";
  646. default:
  647. ;
  648. }
  649. if (data->sc)
  650. {
  651. // "S|---------- MidState ----------||-DataTail-|E"
  652. data->next_work_ob[7] = 45;
  653. data->next_work_ob[8+32+12] = '\xAA';
  654. data->next_work_obsz = 46;
  655. }
  656. else
  657. {
  658. // ">>>>>>>>|---------- MidState ----------||-DataTail-|>>>>>>>>"
  659. memset(&data->next_work_ob[8+32+12], '>', 8);
  660. data->next_work_obsz = 60;
  661. }
  662. }
  663. else
  664. if (proto == BFP_RANGE)
  665. {
  666. /* Split work up into 1/5th nonce ranges */
  667. bitforce->nonces = 0x33333332;
  668. bitforce->sleep_ms /= 5;
  669. data->sleep_ms_default /= 5;
  670. data->next_work_cmd = "ZPX";
  671. if (data->sc)
  672. {
  673. data->next_work_ob[7] = 53;
  674. data->next_work_obsz = 54;
  675. }
  676. else
  677. data->next_work_obsz = 68;
  678. }
  679. data->proto = proto;
  680. bitforce->kname = protonames[proto];
  681. }
  682. static
  683. void bitforce_job_start(struct thr_info *thr)
  684. {
  685. struct cgpu_info *bitforce = thr->cgpu;
  686. struct bitforce_data *data = bitforce->device_data;
  687. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  688. int fdDev = bitforce->device->device_fd;
  689. unsigned char *ob = data->next_work_obs;
  690. char pdevbuf[0x100];
  691. struct timeval tv_now;
  692. data->result_busy_polled = 0;
  693. if (data->queued)
  694. {
  695. uint32_t delay;
  696. // get_results collected more accurate job start time
  697. mt_job_transition(thr);
  698. job_start_complete(thr);
  699. data->queued = 0;
  700. delay = (uint32_t)bitforce->sleep_ms * 1000;
  701. if (unlikely(data->already_have_results))
  702. delay = 0;
  703. timer_set_delay(&thr->tv_morework, &bitforce->work_start_tv, delay);
  704. return;
  705. }
  706. if (!fdDev)
  707. goto commerr;
  708. re_send:
  709. mutex_lock(mutexp);
  710. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, ob, data->next_work_obsz);
  711. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  712. mutex_unlock(mutexp);
  713. cgtime(&tv_now);
  714. timer_set_delay(&thr->tv_poll, &tv_now, WORK_CHECK_INTERVAL_MS * 1000);
  715. data->poll_func = 1;
  716. return;
  717. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  718. mutex_unlock(mutexp);
  719. switch (data->proto)
  720. {
  721. case BFP_RANGE:
  722. applog(LOG_WARNING, "%"PRIpreprv": Does not support nonce range, disabling", bitforce->proc_repr);
  723. bitforce_change_mode(bitforce, BFP_WORK);
  724. goto re_send;
  725. case BFP_QUEUE:
  726. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  727. bitforce_change_mode(bitforce, BFP_WORK);
  728. goto re_send;
  729. default:
  730. ;
  731. }
  732. applog(LOG_ERR, "%"PRIpreprv": Error: Send work reports: %s", bitforce->proc_repr, pdevbuf);
  733. goto commerr;
  734. }
  735. mt_job_transition(thr);
  736. mutex_unlock(mutexp);
  737. dbg_block_data(bitforce);
  738. cgtime(&tv_now);
  739. bitforce->work_start_tv = tv_now;
  740. timer_set_delay(&thr->tv_morework, &tv_now, bitforce->sleep_ms * 1000);
  741. job_start_complete(thr);
  742. return;
  743. commerr:
  744. bitforce_comm_error(thr);
  745. job_start_abort(thr, true);
  746. }
  747. static char _discardedbuf[0x10];
  748. static
  749. int bitforce_zox(struct thr_info *thr, const char *cmd)
  750. {
  751. struct cgpu_info *bitforce = thr->cgpu;
  752. struct bitforce_data *data = bitforce->device_data;
  753. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  754. int fd = bitforce->device->device_fd;
  755. char *pdevbuf = &data->noncebuf[0];
  756. int count;
  757. mutex_lock(mutexp);
  758. bitforce_cmd1(fd, data->xlink_id, pdevbuf, sizeof(data->noncebuf), cmd);
  759. if (!strncasecmp(pdevbuf, "INPROCESS:", 10))
  760. BFgets(pdevbuf, sizeof(data->noncebuf), fd);
  761. if (!strncasecmp(pdevbuf, "COUNT:", 6))
  762. {
  763. count = atoi(&pdevbuf[6]);
  764. size_t cls = strlen(pdevbuf);
  765. char *pmorebuf = &pdevbuf[cls];
  766. size_t szleft = sizeof(data->noncebuf) - cls, sz;
  767. if (count && data->queued)
  768. cgtime(&bitforce->work_start_tv);
  769. while (true)
  770. {
  771. BFgets(pmorebuf, szleft, fd);
  772. if (!strncasecmp(pmorebuf, "OK", 2))
  773. {
  774. pmorebuf[0] = '\0'; // process expects only results
  775. break;
  776. }
  777. sz = strlen(pmorebuf);
  778. if (!sz)
  779. {
  780. applog(LOG_ERR, "%"PRIpreprv": Timeout during %s", bitforce->proc_repr, cmd);
  781. break;
  782. }
  783. szleft -= sz;
  784. pmorebuf += sz;
  785. if (unlikely(szleft < BITFORCE_QRESULT_LINE_LEN))
  786. {
  787. // Out of buffer space somehow :(
  788. applog(LOG_ERR, "%"PRIpreprv": Ran out of buffer space for results, discarding extra data", bitforce->proc_repr);
  789. pmorebuf = _discardedbuf;
  790. szleft = sizeof(_discardedbuf);
  791. }
  792. }
  793. }
  794. else
  795. count = -1;
  796. mutex_unlock(mutexp);
  797. return count;
  798. }
  799. static inline char *next_line(char *);
  800. static
  801. void bitforce_job_get_results(struct thr_info *thr, struct work *work)
  802. {
  803. struct cgpu_info *bitforce = thr->cgpu;
  804. struct bitforce_data *data = bitforce->device_data;
  805. int fdDev = bitforce->device->device_fd;
  806. unsigned int delay_time_ms;
  807. struct timeval elapsed;
  808. struct timeval now;
  809. char *pdevbuf = &data->noncebuf[0];
  810. bool stale;
  811. int count;
  812. cgtime(&now);
  813. timersub(&now, &bitforce->work_start_tv, &elapsed);
  814. bitforce->wait_ms = tv_to_ms(elapsed);
  815. bitforce->polling = true;
  816. if (!fdDev)
  817. goto commerr;
  818. stale = stale_work(work, true);
  819. if (unlikely(bitforce->wait_ms < bitforce->sleep_ms))
  820. {
  821. // We're likely here because of a work restart
  822. // Since Bitforce cannot stop a work without losing results, only do it if the current job is finding stale shares
  823. // BFP_QUEUE does not support stopping work at all
  824. if (data->proto == BFP_QUEUE || !stale)
  825. {
  826. delay_time_ms = bitforce->sleep_ms - bitforce->wait_ms;
  827. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  828. data->poll_func = 2;
  829. return;
  830. }
  831. }
  832. while (1) {
  833. if (data->already_have_results)
  834. {
  835. data->already_have_results = false;
  836. strcpy(pdevbuf, "COUNT:0");
  837. count = 1;
  838. break;
  839. }
  840. const char *cmd = (data->proto == BFP_QUEUE) ? "ZOX" : "ZFX";
  841. count = bitforce_zox(thr, cmd);
  842. cgtime(&now);
  843. timersub(&now, &bitforce->work_start_tv, &elapsed);
  844. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  845. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  846. tv_to_ms(elapsed), (unsigned long)BITFORCE_LONG_TIMEOUT_MS);
  847. goto out;
  848. }
  849. if (count > 0)
  850. {
  851. // Check that queue results match the current work
  852. // Also, if there are results from the next work, short-circuit this wait
  853. unsigned char midstate[32], datatail[12];
  854. char *p;
  855. int i;
  856. p = pdevbuf;
  857. for (i = 0; i < count; ++i)
  858. {
  859. p = next_line(p);
  860. hex2bin(midstate, p, 32);
  861. hex2bin(datatail, &p[65], 12);
  862. if (!(memcmp(work->midstate, midstate, 32) || memcmp(&work->data[64], datatail, 12)))
  863. break;
  864. }
  865. if (i == count)
  866. {
  867. // Didn't find the one we're waiting on
  868. // Must be extra stuff in the queue results
  869. char xmid[65];
  870. char xdt[25];
  871. bin2hex(xmid, work->midstate, 32);
  872. bin2hex(xdt, &work->data[64], 12);
  873. applog(LOG_WARNING, "%"PRIpreprv": Found extra garbage in queue results: %s",
  874. bitforce->proc_repr, pdevbuf);
  875. applog(LOG_WARNING, "%"PRIpreprv": ...while waiting on: %s,%s",
  876. bitforce->proc_repr, xmid, xdt);
  877. count = 0;
  878. }
  879. else
  880. if (i == count - 1)
  881. // Last one found is what we're looking for
  882. {}
  883. else
  884. // We finished the next job too!
  885. data->already_have_results = true;
  886. }
  887. if (!count)
  888. goto noqr;
  889. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  890. break;
  891. data->result_busy_polled = bitforce->wait_ms;
  892. if (stale && data->proto != BFP_QUEUE)
  893. {
  894. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  895. bitforce->proc_repr);
  896. goto out;
  897. }
  898. noqr:
  899. data->result_busy_polled = bitforce->wait_ms;
  900. /* if BFL is throttling, no point checking so quickly */
  901. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  902. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  903. data->poll_func = 2;
  904. return;
  905. }
  906. if (count < 0 && pdevbuf[0] == 'N')
  907. count = strncasecmp(pdevbuf, "NONCE-FOUND", 11) ? 1 : 0;
  908. // At this point, 'count' is:
  909. // negative, in case of some kind of error
  910. // zero, if NO-NONCE (FPGA either completed with no results, or rebooted)
  911. // positive, if at least one job completed successfully
  912. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  913. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  914. tv_to_ms(elapsed), (unsigned long)BITFORCE_TIMEOUT_MS);
  915. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  916. inc_hw_errors_only(thr);
  917. /* If the device truly throttled, it didn't process the job and there
  918. * are no results. But check first, just in case we're wrong about it
  919. * throttling.
  920. */
  921. if (count > 0)
  922. goto out;
  923. } else if (count >= 0) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  924. /* Simple timing adjustment. Allow a few polls to cope with
  925. * OS timer delays being variably reliable. wait_ms will
  926. * always equal sleep_ms when we've waited greater than or
  927. * equal to the result return time.*/
  928. delay_time_ms = bitforce->sleep_ms;
  929. if (!data->result_busy_polled)
  930. {
  931. // No busy polls before results received
  932. if (bitforce->wait_ms > delay_time_ms + (WORK_CHECK_INTERVAL_MS * 8))
  933. // ... due to poll being rather late; ignore it as an anomaly
  934. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, later than scheduled %ums (ignoring)",
  935. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms);
  936. else
  937. if (bitforce->sleep_ms > data->sleep_ms_default + (BITFORCE_CHECK_INTERVAL_MS * 0x20))
  938. {
  939. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on delayed schedule %ums; Wait time changed to: %ums (default sch)",
  940. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, data->sleep_ms_default);
  941. bitforce->sleep_ms = data->sleep_ms_default;
  942. }
  943. else
  944. {
  945. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on default schedule %ums; Wait time changed to: %ums (check interval)",
  946. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, BITFORCE_CHECK_INTERVAL_MS);
  947. bitforce->sleep_ms = BITFORCE_CHECK_INTERVAL_MS;
  948. }
  949. }
  950. else
  951. {
  952. if (data->result_busy_polled - bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  953. {
  954. bitforce->sleep_ms = data->result_busy_polled - (WORK_CHECK_INTERVAL_MS / 2);
  955. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time changed to: %ums",
  956. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms, bitforce->sleep_ms);
  957. }
  958. else
  959. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time unchanged",
  960. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms);
  961. }
  962. /* Work out the average time taken. Float for calculation, uint for display */
  963. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  964. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  965. }
  966. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  967. if (count < 0 && strncasecmp(pdevbuf, "I", 1)) {
  968. inc_hw_errors_only(thr);
  969. applog(LOG_WARNING, "%"PRIpreprv": Error: Get result reports: %s", bitforce->proc_repr, pdevbuf);
  970. bitforce_clear_buffer(bitforce);
  971. }
  972. out:
  973. bitforce->polling = false;
  974. job_results_fetched(thr);
  975. return;
  976. commerr:
  977. bitforce_comm_error(thr);
  978. goto out;
  979. }
  980. static
  981. void bitforce_process_result_nonces(struct thr_info *thr, struct work *work, char *pnoncebuf)
  982. {
  983. struct cgpu_info *bitforce = thr->cgpu;
  984. struct bitforce_data *data = bitforce->device_data;
  985. uint32_t nonce;
  986. while (1) {
  987. hex2bin((void*)&nonce, pnoncebuf, 4);
  988. nonce = be32toh(nonce);
  989. if (unlikely(data->proto == BFP_RANGE && (nonce >= work->blk.nonce ||
  990. /* FIXME: blk.nonce is probably moved on quite a bit now! */
  991. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  992. applog(LOG_WARNING, "%"PRIpreprv": Disabling broken nonce range support", bitforce->proc_repr);
  993. bitforce_change_mode(bitforce, BFP_WORK);
  994. }
  995. submit_nonce(thr, work, nonce);
  996. if (strncmp(&pnoncebuf[8], ",", 1))
  997. break;
  998. pnoncebuf += 9;
  999. }
  1000. }
  1001. static
  1002. bool bitforce_process_qresult_line_i(struct thr_info *thr, char *midstate, char *datatail, char *buf, struct work *work)
  1003. {
  1004. if (!work)
  1005. return false;
  1006. if (memcmp(work->midstate, midstate, 32))
  1007. return false;
  1008. if (memcmp(&work->data[64], datatail, 12))
  1009. return false;
  1010. char *end;
  1011. if (strtol(&buf[90], &end, 10))
  1012. bitforce_process_result_nonces(thr, work, &end[1]);
  1013. return true;
  1014. }
  1015. static
  1016. void bitforce_process_qresult_line(struct thr_info *thr, char *buf, struct work *work)
  1017. {
  1018. struct cgpu_info *bitforce = thr->cgpu;
  1019. char midstate[32], datatail[12];
  1020. hex2bin((void*)midstate, buf, 32);
  1021. hex2bin((void*)datatail, &buf[65], 12);
  1022. if (!( bitforce_process_qresult_line_i(thr, midstate, datatail, buf, work)
  1023. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->work)
  1024. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->prev_work)
  1025. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->next_work) ))
  1026. {
  1027. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queued results", bitforce->proc_repr);
  1028. inc_hw_errors_only(thr);
  1029. }
  1030. }
  1031. static inline
  1032. char *next_line(char *in)
  1033. {
  1034. while (in[0] && (in++)[0] != '\n')
  1035. {}
  1036. return in;
  1037. }
  1038. static
  1039. int64_t bitforce_job_process_results(struct thr_info *thr, struct work *work, __maybe_unused bool stopping)
  1040. {
  1041. struct cgpu_info *bitforce = thr->cgpu;
  1042. struct bitforce_data *data = bitforce->device_data;
  1043. char *pnoncebuf = &data->noncebuf[0];
  1044. int count;
  1045. if (!strncasecmp(pnoncebuf, "NO-", 3))
  1046. return bitforce->nonces; /* No valid nonce found */
  1047. if (!strncasecmp(pnoncebuf, "NONCE-FOUND", 11))
  1048. {
  1049. bitforce_process_result_nonces(thr, work, &pnoncebuf[12]);
  1050. count = 1;
  1051. }
  1052. else
  1053. if (!strncasecmp(pnoncebuf, "COUNT:", 6))
  1054. {
  1055. count = 0;
  1056. pnoncebuf = next_line(pnoncebuf);
  1057. while (pnoncebuf[0])
  1058. {
  1059. bitforce_process_qresult_line(thr, pnoncebuf, work);
  1060. ++count;
  1061. pnoncebuf = next_line(pnoncebuf);
  1062. }
  1063. }
  1064. else
  1065. return 0;
  1066. // FIXME: This might have changed in the meantime (new job start, or broken)
  1067. return bitforce->nonces * count;
  1068. }
  1069. static void bitforce_shutdown(struct thr_info *thr)
  1070. {
  1071. struct cgpu_info *bitforce = thr->cgpu;
  1072. int *p_fdDev = &bitforce->device->device_fd;
  1073. BFclose(*p_fdDev);
  1074. *p_fdDev = 0;
  1075. }
  1076. static void biforce_thread_enable(struct thr_info *thr)
  1077. {
  1078. struct cgpu_info *bitforce = thr->cgpu;
  1079. bitforce_reinit(bitforce);
  1080. }
  1081. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  1082. {
  1083. struct bitforce_proc_data *procdata = bitforce->thr[0]->cgpu_data;
  1084. if (!procdata->handles_board)
  1085. return true;
  1086. return bitforce_get_temp(bitforce);
  1087. }
  1088. static bool bitforce_identify(struct cgpu_info *bitforce)
  1089. {
  1090. bitforce->flash_led = true;
  1091. return true;
  1092. }
  1093. static bool bitforce_thread_init(struct thr_info *thr)
  1094. {
  1095. struct cgpu_info *bitforce = thr->cgpu;
  1096. unsigned int wait;
  1097. struct bitforce_data *data;
  1098. struct bitforce_proc_data *procdata;
  1099. struct bitforce_init_data *initdata = bitforce->device_data;
  1100. bool sc = initdata->sc;
  1101. int xlink_id = 0, boardno = 0;
  1102. struct bitforce_proc_data *first_on_this_board;
  1103. char buf[100];
  1104. int fd = bitforce->device_fd;
  1105. for ( ; bitforce; bitforce = bitforce->next_proc)
  1106. {
  1107. thr = bitforce->thr[0];
  1108. if (unlikely(xlink_id > 30))
  1109. {
  1110. applog(LOG_ERR, "%"PRIpreprv": Failed to find XLINK address", bitforce->proc_repr);
  1111. dev_error(bitforce, REASON_THREAD_FAIL_INIT);
  1112. bitforce->reinit_backoff = 1e10;
  1113. continue;
  1114. }
  1115. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  1116. bitforce->device_data = data = malloc(sizeof(*data));
  1117. *data = (struct bitforce_data){
  1118. .xlink_id = xlink_id,
  1119. .next_work_ob = ">>>>>>>>|---------- MidState ----------||-DataTail-||Nonces|>>>>>>>>",
  1120. .proto = BFP_RANGE,
  1121. .sc = sc,
  1122. .sleep_ms_default = BITFORCE_SLEEP_MS,
  1123. .parallel = abs(initdata->parallels[boardno]),
  1124. .parallel_protocol = (initdata->parallels[boardno] != -1),
  1125. };
  1126. thr->cgpu_data = procdata = malloc(sizeof(*procdata));
  1127. *procdata = (struct bitforce_proc_data){
  1128. .handles_board = true,
  1129. .cgpu = bitforce,
  1130. };
  1131. if (sc)
  1132. {
  1133. // ".......S|---------- MidState ----------||-DataTail-||Nonces|E"
  1134. data->next_work_ob[8+32+12+8] = '\xAA';
  1135. data->next_work_obs = &data->next_work_ob[7];
  1136. if (bitforce->drv == &bitforce_queue_api)
  1137. {
  1138. bitforce_change_mode(bitforce, data->parallel_protocol ? BFP_PQUEUE : BFP_BQUEUE);
  1139. bitforce->sleep_ms = data->sleep_ms_default = 100;
  1140. timer_set_delay_from_now(&thr->tv_poll, 0);
  1141. data->queued_max = data->parallel * 2;
  1142. if (data->queued_max < BITFORCE_MIN_QUEUED_MAX)
  1143. data->queued_max = BITFORCE_MIN_QUEUED_MAX;
  1144. if (data->queued_max > BITFORCE_MAX_QUEUED_MAX)
  1145. data->queued_max = BITFORCE_MAX_QUEUED_MAX;
  1146. }
  1147. else
  1148. bitforce_change_mode(bitforce, BFP_QUEUE);
  1149. // Clear job queue to start fresh; ignore response
  1150. bitforce_cmd1(fd, data->xlink_id, buf, sizeof(buf), "ZQX");
  1151. }
  1152. else
  1153. {
  1154. data->next_work_obs = &data->next_work_ob[0];
  1155. // Unconditionally change away from cold-initialized BFP_RANGE, to allow for setting up other variables
  1156. bitforce_change_mode(bitforce, BFP_WORK);
  1157. /* Initially enable support for nonce range and disable it later if it
  1158. * fails */
  1159. if (opt_bfl_noncerange)
  1160. bitforce_change_mode(bitforce, BFP_RANGE);
  1161. }
  1162. bitforce->status = LIFE_INIT2;
  1163. first_on_this_board = procdata;
  1164. for (int proc = 1; proc < data->parallel; ++proc)
  1165. {
  1166. bitforce = bitforce->next_proc;
  1167. assert(bitforce);
  1168. thr = bitforce->thr[0];
  1169. thr->queue_full = true;
  1170. thr->cgpu_data = procdata = malloc(sizeof(*procdata));
  1171. *procdata = *first_on_this_board;
  1172. procdata->handles_board = false;
  1173. procdata->cgpu = bitforce;
  1174. bitforce->device_data = data;
  1175. bitforce->status = LIFE_INIT2;
  1176. bitforce->kname = first_on_this_board->cgpu->kname;
  1177. }
  1178. applog(LOG_DEBUG, "%s: Board %d: %"PRIpreprv"-%"PRIpreprv, bitforce->dev_repr, boardno, first_on_this_board->cgpu->proc_repr, bitforce->proc_repr);
  1179. ++boardno;
  1180. while (xlink_id < 31 && !(initdata->devmask & (1 << ++xlink_id)))
  1181. {}
  1182. }
  1183. // 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
  1184. bitforce = thr->cgpu;
  1185. free(initdata->parallels);
  1186. free(initdata);
  1187. /* Pause each new thread at least 100ms between initialising
  1188. * so the devices aren't making calls all at the same time. */
  1189. wait = thr->id * MAX_START_DELAY_MS;
  1190. applog(LOG_DEBUG, "%s: Delaying start by %dms", bitforce->dev_repr, wait / 1000);
  1191. cgsleep_ms(wait);
  1192. if (sc)
  1193. {
  1194. // Clear results queue last, to start fresh; ignore response
  1195. for (bitforce = bitforce->device; bitforce; bitforce = bitforce->next_proc)
  1196. bitforce_zox(thr, "ZOX");
  1197. }
  1198. return true;
  1199. }
  1200. #ifdef HAVE_CURSES
  1201. static
  1202. void bitforce_tui_wlogprint_choices(struct cgpu_info *cgpu)
  1203. {
  1204. struct bitforce_data *data = cgpu->device_data;
  1205. if (data->supports_fanspeed)
  1206. wlogprint("[F]an control ");
  1207. }
  1208. static
  1209. const char *bitforce_tui_handle_choice(struct cgpu_info *cgpu, int input)
  1210. {
  1211. struct bitforce_data *data = cgpu->device_data;
  1212. pthread_mutex_t *mutexp;
  1213. int fd;
  1214. static char replybuf[0x100];
  1215. if (!data->supports_fanspeed)
  1216. return NULL;
  1217. switch (input)
  1218. {
  1219. case 'f': case 'F':
  1220. {
  1221. int fanspeed;
  1222. char *intvar;
  1223. intvar = curses_input("Set fan speed (range 0-5 for low to fast or 9 for auto)");
  1224. if (!intvar)
  1225. return "Invalid fan speed\n";
  1226. fanspeed = atoi(intvar);
  1227. free(intvar);
  1228. if ((fanspeed < 0 || fanspeed > 5) && fanspeed != 9)
  1229. return "Invalid fan speed\n";
  1230. char cmd[4] = "Z0X";
  1231. cmd[1] += fanspeed;
  1232. mutexp = &cgpu->device->device_mutex;
  1233. mutex_lock(mutexp);
  1234. fd = cgpu->device->device_fd;
  1235. bitforce_cmd1(fd, data->xlink_id, replybuf, sizeof(replybuf), cmd);
  1236. mutex_unlock(mutexp);
  1237. return replybuf;
  1238. }
  1239. }
  1240. return NULL;
  1241. }
  1242. static
  1243. void bitforce_wlogprint_status(struct cgpu_info *cgpu)
  1244. {
  1245. struct bitforce_data *data = cgpu->device_data;
  1246. if (data->temp[0] > 0 && data->temp[1] > 0)
  1247. wlogprint("Temperatures: %4.1fC %4.1fC\n", data->temp[0], data->temp[1]);
  1248. if (data->volts_count)
  1249. {
  1250. // -> "NNN.xxx / NNN.xxx / NNN.xxx"
  1251. size_t sz = (data->volts_count * 10) + 1;
  1252. char buf[sz];
  1253. char *s = buf;
  1254. int rv = 0;
  1255. for (int i = 0; i < data->volts_count; ++i)
  1256. {
  1257. long v = data->volts[i];
  1258. _SNP("%ld.%03d / ", v / 1000, (int)(v % 1000));
  1259. }
  1260. if (rv >= 3 && s[-2] == '/')
  1261. s[-3] = '\0';
  1262. wlogprint("Voltages: %s\n", buf);
  1263. }
  1264. }
  1265. #endif
  1266. static struct api_data *bitforce_drv_stats(struct cgpu_info *cgpu)
  1267. {
  1268. struct bitforce_data *data = cgpu->device_data;
  1269. struct api_data *root = NULL;
  1270. // Warning, access to these is not locked - but we don't really
  1271. // care since hashing performance is way more important than
  1272. // locking access to displaying API debug 'stats'
  1273. // If locking becomes an issue for any of them, use copy_data=true also
  1274. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  1275. if (data->proto != BFP_BQUEUE && data->proto != BFP_PQUEUE)
  1276. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  1277. if (data->temp[0] > 0 && data->temp[1] > 0)
  1278. {
  1279. root = api_add_temp(root, "Temperature0", &(data->temp[0]), false);
  1280. root = api_add_temp(root, "Temperature1", &(data->temp[1]), false);
  1281. }
  1282. for (int i = 0; i < data->volts_count; ++i)
  1283. {
  1284. float voltage = data->volts[i];
  1285. char key[] = "VoltageNN";
  1286. snprintf(&key[7], 3, "%d", i);
  1287. voltage /= 1e3;
  1288. root = api_add_volts(root, key, &voltage, true);
  1289. }
  1290. return root;
  1291. }
  1292. void bitforce_poll(struct thr_info *thr)
  1293. {
  1294. struct cgpu_info *bitforce = thr->cgpu;
  1295. struct bitforce_data *data = bitforce->device_data;
  1296. int poll = data->poll_func;
  1297. thr->tv_poll.tv_sec = -1;
  1298. data->poll_func = 0;
  1299. switch (poll)
  1300. {
  1301. case 1:
  1302. bitforce_job_start(thr);
  1303. break;
  1304. case 2:
  1305. bitforce_job_get_results(thr, thr->work);
  1306. break;
  1307. default:
  1308. applog(LOG_ERR, "%"PRIpreprv": Unexpected poll from device API!", thr->cgpu->proc_repr);
  1309. }
  1310. }
  1311. static
  1312. char *bitforce_set_device(struct cgpu_info *proc, char *option, char *setting, char *replybuf)
  1313. {
  1314. struct bitforce_data *data = proc->device_data;
  1315. pthread_mutex_t *mutexp = &proc->device->device_mutex;
  1316. int fd;
  1317. if (!strcasecmp(option, "help"))
  1318. {
  1319. sprintf(replybuf, "fanmode: range 0-5 (low to fast) or 9 (auto)");
  1320. return replybuf;
  1321. }
  1322. if (!strcasecmp(option, "fanmode"))
  1323. {
  1324. if (!data->supports_fanspeed)
  1325. {
  1326. sprintf(replybuf, "fanmode not supported");
  1327. return replybuf;
  1328. }
  1329. if (!setting || !*setting)
  1330. {
  1331. sprintf(replybuf, "missing fanmode setting");
  1332. return replybuf;
  1333. }
  1334. if (setting[1] || ((setting[0] < '0' || setting[0] > '5') && setting[0] != '9'))
  1335. {
  1336. sprintf(replybuf, "invalid fanmode setting");
  1337. return replybuf;
  1338. }
  1339. char cmd[4] = "Z5X";
  1340. cmd[1] = setting[0];
  1341. mutex_lock(mutexp);
  1342. fd = proc->device->device_fd;
  1343. bitforce_cmd1(fd, data->xlink_id, replybuf, 256, cmd);
  1344. mutex_unlock(mutexp);
  1345. return replybuf;
  1346. }
  1347. if (!strcasecmp(option, "_cmd1"))
  1348. {
  1349. mutex_lock(mutexp);
  1350. fd = proc->device->device_fd;
  1351. bitforce_cmd1b(fd, data->xlink_id, replybuf, 8000, setting, strlen(setting));
  1352. mutex_unlock(mutexp);
  1353. return replybuf;
  1354. }
  1355. sprintf(replybuf, "Unknown option: %s", option);
  1356. return replybuf;
  1357. }
  1358. struct device_drv bitforce_drv = {
  1359. .dname = "bitforce",
  1360. .name = "BFL",
  1361. .drv_detect = bitforce_detect,
  1362. #ifdef HAVE_CURSES
  1363. .proc_wlogprint_status = bitforce_wlogprint_status,
  1364. .proc_tui_wlogprint_choices = bitforce_tui_wlogprint_choices,
  1365. .proc_tui_handle_choice = bitforce_tui_handle_choice,
  1366. #endif
  1367. .get_api_stats = bitforce_drv_stats,
  1368. .minerloop = minerloop_async,
  1369. .reinit_device = bitforce_reinit,
  1370. .get_stats = bitforce_get_stats,
  1371. .set_device = bitforce_set_device,
  1372. .identify_device = bitforce_identify,
  1373. .thread_prepare = bitforce_thread_prepare,
  1374. .thread_init = bitforce_thread_init,
  1375. .job_prepare = bitforce_job_prepare,
  1376. .job_start = bitforce_job_start,
  1377. .job_get_results = bitforce_job_get_results,
  1378. .poll = bitforce_poll,
  1379. .job_process_results = bitforce_job_process_results,
  1380. .thread_shutdown = bitforce_shutdown,
  1381. .thread_enable = biforce_thread_enable
  1382. };
  1383. static inline
  1384. void bitforce_set_queue_full(struct thr_info *thr)
  1385. {
  1386. struct cgpu_info *bitforce = thr->cgpu;
  1387. struct bitforce_data *data = bitforce->device_data;
  1388. thr->queue_full = (data->queued + data->ready_to_queue >= data->queued_max) || (data->ready_to_queue >= BITFORCE_MAX_BQUEUE_AT_ONCE);
  1389. }
  1390. static
  1391. bool bitforce_send_queue(struct thr_info *thr)
  1392. {
  1393. struct cgpu_info *bitforce = thr->cgpu;
  1394. struct bitforce_data *data = bitforce->device_data;
  1395. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  1396. int fd = bitforce->device->device_fd;
  1397. struct work *work;
  1398. if (unlikely(!(fd && data->ready_to_queue)))
  1399. return false;
  1400. char buf[0x100];
  1401. int queued_ok;
  1402. size_t qjs_sz = (32 + 12 + 2);
  1403. size_t qjp_sz = 4 + (qjs_sz * data->ready_to_queue);
  1404. uint8_t qjp[qjp_sz], *qjs;
  1405. qjp[0] = qjp_sz - 1;
  1406. qjp[1] = 0xc1;
  1407. qjp[2] = data->ready_to_queue;
  1408. qjp[qjp_sz - 1] = 0xfe;
  1409. qjs = &qjp[qjp_sz - 1];
  1410. work = thr->work_list->prev;
  1411. for (int i = data->ready_to_queue; i > 0; --i, work = work->prev)
  1412. {
  1413. *(--qjs) = 0xaa;
  1414. memcpy(qjs -= 12, work->data + 64, 12);
  1415. memcpy(qjs -= 32, work->midstate, 32);
  1416. *(--qjs) = 45;
  1417. }
  1418. mutex_lock(mutexp);
  1419. bitforce_cmd2(fd, data->xlink_id, buf, sizeof(buf), "ZWX", qjp, qjp_sz);
  1420. mutex_unlock(mutexp);
  1421. if (!strncasecmp(buf, "ERR:QUEUE", 9))
  1422. {
  1423. // Queue full :(
  1424. applog(LOG_DEBUG, "%"PRIpreprv": Device queue full while attempting to append %d jobs (queued<=%d)",
  1425. bitforce->proc_repr,
  1426. data->ready_to_queue, data->queued);
  1427. thr->queue_full = true;
  1428. return false;
  1429. }
  1430. if (strncasecmp(buf, "OK:QUEUED", 9))
  1431. {
  1432. // TODO: ERR:UNKNOWN COMMAND
  1433. applog(LOG_DEBUG, "%"PRIpreprv": Unexpected error attempting to append %d jobs (queued<=%d): %s",
  1434. bitforce->proc_repr,
  1435. data->ready_to_queue, data->queued, buf);
  1436. return false;
  1437. }
  1438. if (!data->queued)
  1439. cgtime(&data->tv_hashmeter_start);
  1440. queued_ok = atoi(&buf[9]);
  1441. data->queued += queued_ok;
  1442. applog(LOG_DEBUG, "%"PRIpreprv": Successfully queued %d/%d jobs on device (queued<=%d)",
  1443. bitforce->proc_repr,
  1444. queued_ok, data->ready_to_queue, data->queued);
  1445. data->ready_to_queue -= queued_ok;
  1446. thr->queue_full = data->ready_to_queue;
  1447. data->just_flushed = false;
  1448. data->want_to_send_queue = false;
  1449. return true;
  1450. }
  1451. void work_list_del(struct work **head, struct work *work)
  1452. {
  1453. DL_DELETE(*head, work);
  1454. free_work(work);
  1455. }
  1456. static
  1457. bool bitforce_queue_do_results(struct thr_info *thr)
  1458. {
  1459. struct cgpu_info *bitforce = thr->cgpu;
  1460. struct bitforce_data *data = bitforce->device_data;
  1461. int fd = bitforce->device->device_fd;
  1462. int count;
  1463. int fcount;
  1464. char *noncebuf, *buf, *end;
  1465. unsigned char midstate[32], datatail[12];
  1466. struct work *work, *tmpwork, *thiswork;
  1467. struct timeval tv_now, tv_elapsed;
  1468. long chipno = 0; // Initialized value is used for non-parallelized boards
  1469. struct cgpu_info *chip_cgpu;
  1470. struct thr_info *chip_thr;
  1471. int counts[data->parallel];
  1472. if (unlikely(!fd))
  1473. return false;
  1474. again:
  1475. noncebuf = &data->noncebuf[0];
  1476. count = bitforce_zox(thr, "ZOX");
  1477. if (unlikely(count < 0))
  1478. {
  1479. applog(LOG_ERR, "%"PRIpreprv": Received unexpected queue result response: %s", bitforce->proc_repr, noncebuf);
  1480. inc_hw_errors_only(thr);
  1481. return false;
  1482. }
  1483. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results on poll (max=%d)", bitforce->proc_repr, count, (int)BITFORCE_MAX_QRESULTS);
  1484. if (!count)
  1485. return true;
  1486. fcount = 0;
  1487. for (int i = 0; i < data->parallel; ++i)
  1488. counts[i] = 0;
  1489. noncebuf = next_line(noncebuf);
  1490. while ((buf = noncebuf)[0])
  1491. {
  1492. if ( (noncebuf = next_line(buf)) )
  1493. noncebuf[-1] = '\0';
  1494. if (strlen(buf) <= 90)
  1495. {
  1496. applog(LOG_ERR, "%"PRIpreprv": Gibberish within queue results: %s", bitforce->proc_repr, buf);
  1497. continue;
  1498. }
  1499. hex2bin(midstate, buf, 32);
  1500. hex2bin(datatail, &buf[65], 12);
  1501. thiswork = NULL;
  1502. DL_FOREACH(thr->work_list, work)
  1503. {
  1504. if (unlikely(memcmp(work->midstate, midstate, 32)))
  1505. continue;
  1506. if (unlikely(memcmp(&work->data[64], datatail, 12)))
  1507. continue;
  1508. thiswork = work;
  1509. break;
  1510. }
  1511. end = &buf[89];
  1512. chip_cgpu = bitforce;
  1513. if (data->parallel_protocol)
  1514. {
  1515. chipno = strtol(&end[1], &end, 16);
  1516. if (chipno >= data->parallel)
  1517. {
  1518. applog(LOG_ERR, "%"PRIpreprv": Chip number out of range for queue result: %s", chip_cgpu->proc_repr, buf);
  1519. chipno = 0;
  1520. }
  1521. for (int i = 0; i < chipno; ++i)
  1522. chip_cgpu = chip_cgpu->next_proc;
  1523. }
  1524. chip_thr = chip_cgpu->thr[0];
  1525. applog(LOG_DEBUG, "%"PRIpreprv": Queue result: %s", chip_cgpu->proc_repr, buf);
  1526. if (unlikely(!thiswork))
  1527. {
  1528. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queue results: %s", chip_cgpu->proc_repr, buf);
  1529. inc_hw_errors_only(chip_thr);
  1530. goto next_qline;
  1531. }
  1532. if (unlikely(!end[0]))
  1533. {
  1534. applog(LOG_ERR, "%"PRIpreprv": Missing nonce count in queue results: %s", chip_cgpu->proc_repr, buf);
  1535. goto finishresult;
  1536. }
  1537. if (strtol(&end[1], &end, 10))
  1538. {
  1539. if (unlikely(!end[0]))
  1540. {
  1541. applog(LOG_ERR, "%"PRIpreprv": Missing nonces in queue results: %s", chip_cgpu->proc_repr, buf);
  1542. goto finishresult;
  1543. }
  1544. bitforce_process_result_nonces(chip_thr, work, &end[1]);
  1545. }
  1546. ++fcount;
  1547. ++counts[chipno];
  1548. finishresult:
  1549. if (data->parallel == 1)
  1550. {
  1551. // Queue results are in order, so anything queued prior this is lost
  1552. // Delete all queued work up to, and including, this one
  1553. DL_FOREACH_SAFE(thr->work_list, work, tmpwork)
  1554. {
  1555. work_list_del(&thr->work_list, work);
  1556. --data->queued;
  1557. if (work == thiswork)
  1558. break;
  1559. }
  1560. }
  1561. else
  1562. {
  1563. // Parallel processors means the results might not be in order
  1564. // This could leak if jobs get lost, hence the sanity checks using "ZqX"
  1565. work_list_del(&thr->work_list, thiswork);
  1566. --data->queued;
  1567. }
  1568. next_qline: (void)0;
  1569. }
  1570. bitforce_set_queue_full(thr);
  1571. if (count >= BITFORCE_MAX_QRESULTS)
  1572. goto again;
  1573. if (data->parallel == 1 && (
  1574. (fcount < BITFORCE_GOAL_QRESULTS && bitforce->sleep_ms < BITFORCE_MAX_QRESULT_WAIT && data->queued > 1)
  1575. || (fcount > BITFORCE_GOAL_QRESULTS && bitforce->sleep_ms > BITFORCE_MIN_QRESULT_WAIT) ))
  1576. {
  1577. unsigned int old_sleep_ms = bitforce->sleep_ms;
  1578. bitforce->sleep_ms = (uint32_t)bitforce->sleep_ms * BITFORCE_GOAL_QRESULTS / (fcount ?: 1);
  1579. if (bitforce->sleep_ms > BITFORCE_MAX_QRESULT_WAIT)
  1580. bitforce->sleep_ms = BITFORCE_MAX_QRESULT_WAIT;
  1581. if (bitforce->sleep_ms < BITFORCE_MIN_QRESULT_WAIT)
  1582. bitforce->sleep_ms = BITFORCE_MIN_QRESULT_WAIT;
  1583. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time changed to: %ums (queued<=%d)",
  1584. bitforce->proc_repr, fcount, old_sleep_ms, bitforce->sleep_ms, data->queued);
  1585. }
  1586. else
  1587. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time unchanged (queued<=%d)",
  1588. bitforce->proc_repr, fcount, bitforce->sleep_ms, data->queued);
  1589. cgtime(&tv_now);
  1590. timersub(&tv_now, &data->tv_hashmeter_start, &tv_elapsed);
  1591. chip_cgpu = bitforce;
  1592. for (int i = 0; i < data->parallel; ++i, (chip_cgpu = chip_cgpu->next_proc))
  1593. {
  1594. chip_thr = chip_cgpu->thr[0];
  1595. hashes_done(chip_thr, (uint64_t)bitforce->nonces * counts[i], &tv_elapsed, NULL);
  1596. }
  1597. data->tv_hashmeter_start = tv_now;
  1598. return true;
  1599. }
  1600. static
  1601. bool bitforce_queue_append(struct thr_info *thr, struct work *work)
  1602. {
  1603. struct cgpu_info *bitforce = thr->cgpu;
  1604. struct bitforce_data *data = bitforce->device_data;
  1605. bool rv, ndq;
  1606. bitforce_set_queue_full(thr);
  1607. rv = !thr->queue_full;
  1608. if (rv)
  1609. {
  1610. DL_APPEND(thr->work_list, work);
  1611. ++data->ready_to_queue;
  1612. applog(LOG_DEBUG, "%"PRIpreprv": Appending to driver queue (max=%u, ready=%d, queued<=%d)",
  1613. bitforce->proc_repr,
  1614. (unsigned)data->queued_max, data->ready_to_queue, data->queued);
  1615. bitforce_set_queue_full(thr);
  1616. }
  1617. else
  1618. if (!data->ready_to_queue)
  1619. return rv;
  1620. ndq = !data->queued;
  1621. if ((ndq) // Device is idle
  1622. || (data->ready_to_queue >= BITFORCE_MAX_BQUEUE_AT_ONCE) // ...or 5 items ready to go
  1623. || (thr->queue_full) // ...or done filling queue
  1624. || (data->just_flushed) // ...or queue was just flushed (only remaining job is partly done already)
  1625. )
  1626. {
  1627. if (!bitforce_send_queue(thr))
  1628. {
  1629. // Problem sending queue, retry again in a few seconds
  1630. applog(LOG_ERR, "%"PRIpreprv": Failed to send queue", bitforce->proc_repr);
  1631. inc_hw_errors_only(thr);
  1632. data->want_to_send_queue = true;
  1633. }
  1634. }
  1635. return rv;
  1636. }
  1637. struct _jobinfo {
  1638. uint8_t key[32+12];
  1639. int instances;
  1640. UT_hash_handle hh;
  1641. };
  1642. static
  1643. void bitforce_queue_flush(struct thr_info *thr)
  1644. {
  1645. struct bitforce_proc_data *procdata = thr->cgpu_data;
  1646. if (!procdata->handles_board)
  1647. return;
  1648. struct cgpu_info *bitforce = thr->cgpu;
  1649. struct bitforce_data *data = bitforce->device_data;
  1650. char *buf = &data->noncebuf[0], *buf2 = NULL;
  1651. const char *cmd = "ZqX";
  1652. unsigned flushed;
  1653. struct _jobinfo *processing = NULL, *item, *this;
  1654. if (data->parallel == 1)
  1655. // Pre-parallelization neither needs nor supports "ZqX"
  1656. cmd = "ZQX";
  1657. // TODO: Call "ZQX" most of the time: don't need to do sanity checks so often
  1658. bitforce_zox(thr, cmd);
  1659. if (!strncasecmp(buf, "OK:FLUSHED", 10))
  1660. flushed = atoi(&buf[10]);
  1661. else
  1662. if ((!strncasecmp(buf, "COUNT:", 6)) && (buf2 = strstr(buf, "FLUSHED:")) )
  1663. {
  1664. flushed = atoi(&buf2[8]);
  1665. buf2 = next_line(buf2);
  1666. }
  1667. else
  1668. {
  1669. applog(LOG_DEBUG, "%"PRIpreprv": Failed to flush device queue: %s", bitforce->proc_repr, buf);
  1670. flushed = 0;
  1671. }
  1672. data->queued -= flushed;
  1673. applog(LOG_DEBUG, "%"PRIpreprv": Flushed %u jobs from device and %d from driver (queued<=%d)",
  1674. bitforce->proc_repr, flushed, data->ready_to_queue, data->queued);
  1675. flushed += data->ready_to_queue;
  1676. data->ready_to_queue = 0;
  1677. while (flushed--)
  1678. work_list_del(&thr->work_list, thr->work_list->prev);
  1679. bitforce_set_queue_full(thr);
  1680. data->just_flushed = true;
  1681. data->want_to_send_queue = false;
  1682. // "ZqX" returns jobs in progress, allowing us to sanity check
  1683. // NOTE: Must process buffer into hash table BEFORE calling bitforce_queue_do_results, which clobbers it
  1684. // NOTE: Must do actual sanity check AFTER calling bitforce_queue_do_results, to ensure we don't delete completed jobs
  1685. if (buf2)
  1686. {
  1687. // First, turn buf2 into a hash
  1688. for ( ; buf2[0]; buf2 = next_line(buf2))
  1689. {
  1690. this = malloc(sizeof(*this));
  1691. hex2bin(&this->key[ 0], &buf2[ 0], 32);
  1692. hex2bin(&this->key[32], &buf2[65], 12);
  1693. HASH_FIND(hh, processing, &this->key[0], sizeof(this->key), item);
  1694. if (likely(!item))
  1695. {
  1696. this->instances = 1;
  1697. HASH_ADD(hh, processing, key, sizeof(this->key), this);
  1698. }
  1699. else
  1700. {
  1701. // This should really only happen in testing/benchmarking...
  1702. ++item->instances;
  1703. free(this);
  1704. }
  1705. }
  1706. }
  1707. bitforce_queue_do_results(thr);
  1708. if (buf2)
  1709. {
  1710. struct work *work, *tmp;
  1711. uint8_t key[32+12];
  1712. // Now iterate over the work_list and delete anything not in the hash
  1713. DL_FOREACH_SAFE(thr->work_list, work, tmp)
  1714. {
  1715. memcpy(&key[ 0], work->midstate, 32);
  1716. memcpy(&key[32], &work->data[64], 12);
  1717. HASH_FIND(hh, processing, &key[0], sizeof(key), item);
  1718. if (unlikely(!item))
  1719. {
  1720. char hex[89];
  1721. bin2hex(hex, key, 32+12);
  1722. applog(LOG_WARNING, "%"PRIpreprv": Sanity check: Device is missing queued job! %s", bitforce->proc_repr, hex);
  1723. work_list_del(&thr->work_list, work);
  1724. continue;
  1725. }
  1726. if (likely(!--item->instances))
  1727. {
  1728. HASH_DEL(processing, item);
  1729. free(item);
  1730. }
  1731. }
  1732. if (unlikely( (flushed = HASH_COUNT(processing)) ))
  1733. {
  1734. //applog(LOG_WARNING, "%"PRIpreprv": Sanity check: Device is working on %d unknown jobs!", bitforce->proc_repr, flushed);
  1735. // FIXME: Probably these were jobs finished after ZqX, included in the result check we just did
  1736. // NOTE: We need to do that result check first to avoid deleting work_list items for things just solved
  1737. HASH_ITER(hh, processing, item, this)
  1738. {
  1739. HASH_DEL(processing, item);
  1740. free(item);
  1741. }
  1742. }
  1743. }
  1744. }
  1745. static
  1746. void bitforce_queue_poll(struct thr_info *thr)
  1747. {
  1748. struct cgpu_info *bitforce = thr->cgpu;
  1749. struct bitforce_data *data = bitforce->device_data;
  1750. unsigned long sleep_us;
  1751. if (data->queued)
  1752. bitforce_queue_do_results(thr);
  1753. sleep_us = (unsigned long)bitforce->sleep_ms * 1000;
  1754. if (data->want_to_send_queue)
  1755. if (!bitforce_send_queue(thr))
  1756. if (!data->queued)
  1757. {
  1758. applog(LOG_ERR, "%"PRIpreprv": Failed to send queue, and queue empty; retrying after 1 second", bitforce->proc_repr);
  1759. inc_hw_errors_only(thr);
  1760. sleep_us = 1000000;
  1761. }
  1762. timer_set_delay_from_now(&thr->tv_poll, sleep_us);
  1763. }
  1764. static void bitforce_queue_thread_deven(struct thr_info *thr)
  1765. {
  1766. struct cgpu_info *bitforce = thr->cgpu, *thisbf;
  1767. struct bitforce_data *data = bitforce->device_data;
  1768. struct thr_info *thisthr;
  1769. for (thisbf = bitforce; thisbf && thisbf->device_data == data; thisbf = thisbf->next_proc)
  1770. {
  1771. thisthr = bitforce->thr[0];
  1772. thisthr->pause = thr->pause;
  1773. thisbf->deven = bitforce->deven;
  1774. }
  1775. }
  1776. static void bitforce_queue_thread_disable(struct thr_info *thr)
  1777. {
  1778. // Disable other threads sharing the same queue
  1779. bitforce_queue_thread_deven(thr);
  1780. }
  1781. static void bitforce_queue_thread_enable(struct thr_info *thr)
  1782. {
  1783. // TODO: Maybe reinit?
  1784. // Enable other threads sharing the same queue
  1785. bitforce_queue_thread_deven(thr);
  1786. }
  1787. struct device_drv bitforce_queue_api = {
  1788. .dname = "bitforce_queue",
  1789. .name = "BFL",
  1790. .minerloop = minerloop_queue,
  1791. .reinit_device = bitforce_reinit,
  1792. #ifdef HAVE_CURSES
  1793. .proc_wlogprint_status = bitforce_wlogprint_status,
  1794. .proc_tui_wlogprint_choices = bitforce_tui_wlogprint_choices,
  1795. .proc_tui_handle_choice = bitforce_tui_handle_choice,
  1796. #endif
  1797. .get_api_stats = bitforce_drv_stats,
  1798. .get_stats = bitforce_get_stats,
  1799. .set_device = bitforce_set_device,
  1800. .identify_device = bitforce_identify,
  1801. .thread_prepare = bitforce_thread_prepare,
  1802. .thread_init = bitforce_thread_init,
  1803. .queue_append = bitforce_queue_append,
  1804. .queue_flush = bitforce_queue_flush,
  1805. .poll = bitforce_queue_poll,
  1806. .thread_shutdown = bitforce_shutdown,
  1807. .thread_disable = bitforce_queue_thread_disable,
  1808. .thread_enable = bitforce_queue_thread_enable,
  1809. };