driver-bitforce.c 53 KB

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