driver-bitforce.c 54 KB

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