driver-bitforce.c 58 KB

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