driver-bitforce.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  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. };
  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[(datasz * 2) + 1];
  116. bin2hex(hex, data, datasz);
  117. applog(LOG_DEBUG, "DEVPROTO: CMD2 (fd=%d xlink=%d): %s", fd, procid, 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 bool bitforce_thread_prepare(struct thr_info *thr)
  309. {
  310. struct cgpu_info *bitforce = thr->cgpu;
  311. int fdDev = BFopen(bitforce->device_path);
  312. struct timeval now;
  313. if (unlikely(fdDev == -1)) {
  314. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, bitforce->device_path);
  315. return false;
  316. }
  317. bitforce->device_fd = fdDev;
  318. applog(LOG_INFO, "%s: Opened %s", bitforce->dev_repr, bitforce->device_path);
  319. cgtime(&now);
  320. get_datestamp(bitforce->init, &now);
  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 = temp;
  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_RANGE:
  551. {
  552. uint32_t *ob_nonce = (uint32_t*)&(ob_dt[32]);
  553. ob_nonce[0] = htobe32(work->blk.nonce);
  554. ob_nonce[1] = htobe32(work->blk.nonce + bitforce->nonces);
  555. // FIXME: if nonce range fails... we didn't increment enough
  556. work->blk.nonce += bitforce->nonces + 1;
  557. break;
  558. }
  559. case BFP_QUEUE:
  560. if (thr->work)
  561. {
  562. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  563. char pdevbuf[0x100];
  564. if (unlikely(!fdDev))
  565. return false;
  566. mutex_lock(mutexp);
  567. if (data->queued)
  568. bitforce_cmd1(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), "ZQX");
  569. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, data->next_work_obs, data->next_work_obsz);
  570. mutex_unlock(mutexp);
  571. if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  572. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  573. bitforce_change_mode(bitforce, BFP_WORK);
  574. }
  575. else
  576. {
  577. dbg_block_data(bitforce);
  578. data->queued = 1;
  579. }
  580. }
  581. // fallthru...
  582. case BFP_WORK:
  583. work->blk.nonce = 0xffffffff;
  584. }
  585. return true;
  586. }
  587. static
  588. void bitforce_change_mode(struct cgpu_info *bitforce, enum bitforce_proto proto)
  589. {
  590. struct bitforce_data *data = bitforce->device_data;
  591. if (data->proto == proto)
  592. return;
  593. if (data->proto == BFP_RANGE)
  594. {
  595. bitforce->nonces = 0xffffffff;
  596. bitforce->sleep_ms *= 5;
  597. data->sleep_ms_default *= 5;
  598. switch (proto)
  599. {
  600. case BFP_WORK:
  601. data->next_work_cmd = "ZDX";
  602. break;
  603. case BFP_QUEUE:
  604. data->next_work_cmd = "ZNX";
  605. default:
  606. ;
  607. }
  608. if (data->sc)
  609. {
  610. // "S|---------- MidState ----------||-DataTail-|E"
  611. data->next_work_ob[7] = 45;
  612. data->next_work_ob[8+32+12] = '\xAA';
  613. data->next_work_obsz = 46;
  614. }
  615. else
  616. {
  617. // ">>>>>>>>|---------- MidState ----------||-DataTail-|>>>>>>>>"
  618. memset(&data->next_work_ob[8+32+12], '>', 8);
  619. data->next_work_obsz = 60;
  620. }
  621. }
  622. else
  623. if (proto == BFP_RANGE)
  624. {
  625. /* Split work up into 1/5th nonce ranges */
  626. bitforce->nonces = 0x33333332;
  627. bitforce->sleep_ms /= 5;
  628. data->sleep_ms_default /= 5;
  629. data->next_work_cmd = "ZPX";
  630. if (data->sc)
  631. {
  632. data->next_work_ob[7] = 53;
  633. data->next_work_obsz = 54;
  634. }
  635. else
  636. data->next_work_obsz = 68;
  637. }
  638. data->proto = proto;
  639. bitforce->kname = protonames[proto];
  640. }
  641. static
  642. void bitforce_job_start(struct thr_info *thr)
  643. {
  644. struct cgpu_info *bitforce = thr->cgpu;
  645. struct bitforce_data *data = bitforce->device_data;
  646. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  647. int fdDev = bitforce->device->device_fd;
  648. unsigned char *ob = data->next_work_obs;
  649. char pdevbuf[0x100];
  650. struct timeval tv_now;
  651. data->result_busy_polled = 0;
  652. if (data->queued)
  653. {
  654. uint32_t delay;
  655. // get_results collected more accurate job start time
  656. mt_job_transition(thr);
  657. job_start_complete(thr);
  658. data->queued = 0;
  659. delay = (uint32_t)bitforce->sleep_ms * 1000;
  660. if (unlikely(data->already_have_results))
  661. delay = 0;
  662. timer_set_delay(&thr->tv_morework, &bitforce->work_start_tv, delay);
  663. return;
  664. }
  665. if (!fdDev)
  666. goto commerr;
  667. re_send:
  668. mutex_lock(mutexp);
  669. bitforce_cmd2(fdDev, data->xlink_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, ob, data->next_work_obsz);
  670. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  671. mutex_unlock(mutexp);
  672. cgtime(&tv_now);
  673. timer_set_delay(&thr->tv_poll, &tv_now, WORK_CHECK_INTERVAL_MS * 1000);
  674. data->poll_func = 1;
  675. return;
  676. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  677. mutex_unlock(mutexp);
  678. switch (data->proto)
  679. {
  680. case BFP_RANGE:
  681. applog(LOG_WARNING, "%"PRIpreprv": Does not support nonce range, disabling", bitforce->proc_repr);
  682. bitforce_change_mode(bitforce, BFP_WORK);
  683. goto re_send;
  684. case BFP_QUEUE:
  685. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  686. bitforce_change_mode(bitforce, BFP_WORK);
  687. goto re_send;
  688. default:
  689. ;
  690. }
  691. applog(LOG_ERR, "%"PRIpreprv": Error: Send work reports: %s", bitforce->proc_repr, pdevbuf);
  692. goto commerr;
  693. }
  694. mt_job_transition(thr);
  695. mutex_unlock(mutexp);
  696. dbg_block_data(bitforce);
  697. cgtime(&tv_now);
  698. bitforce->work_start_tv = tv_now;
  699. timer_set_delay(&thr->tv_morework, &tv_now, bitforce->sleep_ms * 1000);
  700. job_start_complete(thr);
  701. return;
  702. commerr:
  703. bitforce_comm_error(thr);
  704. job_start_abort(thr, true);
  705. }
  706. static char _discardedbuf[0x10];
  707. static
  708. int bitforce_zox(struct thr_info *thr, const char *cmd)
  709. {
  710. struct cgpu_info *bitforce = thr->cgpu;
  711. struct bitforce_data *data = bitforce->device_data;
  712. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  713. int fd = bitforce->device->device_fd;
  714. char *pdevbuf = &data->noncebuf[0];
  715. int count;
  716. mutex_lock(mutexp);
  717. bitforce_cmd1(fd, data->xlink_id, pdevbuf, sizeof(data->noncebuf), cmd);
  718. if (!strncasecmp(pdevbuf, "INPROCESS:", 10))
  719. BFgets(pdevbuf, sizeof(data->noncebuf), fd);
  720. if (!strncasecmp(pdevbuf, "COUNT:", 6))
  721. {
  722. count = atoi(&pdevbuf[6]);
  723. size_t cls = strlen(pdevbuf);
  724. char *pmorebuf = &pdevbuf[cls];
  725. size_t szleft = sizeof(data->noncebuf) - cls, sz;
  726. if (count && data->queued)
  727. cgtime(&bitforce->work_start_tv);
  728. while (true)
  729. {
  730. BFgets(pmorebuf, szleft, fd);
  731. if (!strncasecmp(pmorebuf, "OK", 2))
  732. {
  733. pmorebuf[0] = '\0'; // process expects only results
  734. break;
  735. }
  736. sz = strlen(pmorebuf);
  737. if (!sz)
  738. {
  739. applog(LOG_ERR, "%"PRIpreprv": Timeout during %s", bitforce->proc_repr, cmd);
  740. break;
  741. }
  742. szleft -= sz;
  743. pmorebuf += sz;
  744. if (unlikely(szleft < BITFORCE_QRESULT_LINE_LEN))
  745. {
  746. // Out of buffer space somehow :(
  747. applog(LOG_ERR, "%"PRIpreprv": Ran out of buffer space for results, discarding extra data", bitforce->proc_repr);
  748. pmorebuf = _discardedbuf;
  749. szleft = sizeof(_discardedbuf);
  750. }
  751. }
  752. }
  753. else
  754. count = -1;
  755. mutex_unlock(mutexp);
  756. return count;
  757. }
  758. static inline char *next_line(char *);
  759. static
  760. void bitforce_job_get_results(struct thr_info *thr, struct work *work)
  761. {
  762. struct cgpu_info *bitforce = thr->cgpu;
  763. struct bitforce_data *data = bitforce->device_data;
  764. int fdDev = bitforce->device->device_fd;
  765. unsigned int delay_time_ms;
  766. struct timeval elapsed;
  767. struct timeval now;
  768. char *pdevbuf = &data->noncebuf[0];
  769. bool stale;
  770. int count;
  771. cgtime(&now);
  772. timersub(&now, &bitforce->work_start_tv, &elapsed);
  773. bitforce->wait_ms = tv_to_ms(elapsed);
  774. bitforce->polling = true;
  775. if (!fdDev)
  776. goto commerr;
  777. stale = stale_work(work, true);
  778. if (unlikely(bitforce->wait_ms < bitforce->sleep_ms))
  779. {
  780. // We're likely here because of a work restart
  781. // Since Bitforce cannot stop a work without losing results, only do it if the current job is finding stale shares
  782. // BFP_QUEUE does not support stopping work at all
  783. if (data->proto == BFP_QUEUE || !stale)
  784. {
  785. delay_time_ms = bitforce->sleep_ms - bitforce->wait_ms;
  786. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  787. data->poll_func = 2;
  788. return;
  789. }
  790. }
  791. while (1) {
  792. if (data->already_have_results)
  793. {
  794. data->already_have_results = false;
  795. strcpy(pdevbuf, "COUNT:0");
  796. count = 1;
  797. break;
  798. }
  799. const char *cmd = (data->proto == BFP_QUEUE) ? "ZOX" : "ZFX";
  800. count = bitforce_zox(thr, cmd);
  801. cgtime(&now);
  802. timersub(&now, &bitforce->work_start_tv, &elapsed);
  803. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  804. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  805. tv_to_ms(elapsed), (unsigned long)BITFORCE_LONG_TIMEOUT_MS);
  806. goto out;
  807. }
  808. if (count > 0)
  809. {
  810. // Check that queue results match the current work
  811. // Also, if there are results from the next work, short-circuit this wait
  812. unsigned char midstate[32], datatail[12];
  813. char *p;
  814. int i;
  815. p = pdevbuf;
  816. for (i = 0; i < count; ++i)
  817. {
  818. p = next_line(p);
  819. hex2bin(midstate, p, 32);
  820. hex2bin(datatail, &p[65], 12);
  821. if (!(memcmp(work->midstate, midstate, 32) || memcmp(&work->data[64], datatail, 12)))
  822. break;
  823. }
  824. if (i == count)
  825. {
  826. // Didn't find the one we're waiting on
  827. // Must be extra stuff in the queue results
  828. char xmid[65];
  829. char xdt[25];
  830. bin2hex(xmid, work->midstate, 32);
  831. bin2hex(xdt, &work->data[64], 12);
  832. applog(LOG_WARNING, "%"PRIpreprv": Found extra garbage in queue results: %s",
  833. bitforce->proc_repr, pdevbuf);
  834. applog(LOG_WARNING, "%"PRIpreprv": ...while waiting on: %s,%s",
  835. bitforce->proc_repr, xmid, xdt);
  836. count = 0;
  837. }
  838. else
  839. if (i == count - 1)
  840. // Last one found is what we're looking for
  841. {}
  842. else
  843. // We finished the next job too!
  844. data->already_have_results = true;
  845. }
  846. if (!count)
  847. goto noqr;
  848. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  849. break;
  850. data->result_busy_polled = bitforce->wait_ms;
  851. if (stale && data->proto != BFP_QUEUE)
  852. {
  853. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  854. bitforce->proc_repr);
  855. goto out;
  856. }
  857. noqr:
  858. data->result_busy_polled = bitforce->wait_ms;
  859. /* if BFL is throttling, no point checking so quickly */
  860. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  861. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  862. data->poll_func = 2;
  863. return;
  864. }
  865. if (count < 0 && pdevbuf[0] == 'N')
  866. count = strncasecmp(pdevbuf, "NONCE-FOUND", 11) ? 1 : 0;
  867. // At this point, 'count' is:
  868. // negative, in case of some kind of error
  869. // zero, if NO-NONCE (FPGA either completed with no results, or rebooted)
  870. // positive, if at least one job completed successfully
  871. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  872. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  873. tv_to_ms(elapsed), (unsigned long)BITFORCE_TIMEOUT_MS);
  874. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  875. inc_hw_errors_only(thr);
  876. /* If the device truly throttled, it didn't process the job and there
  877. * are no results. But check first, just in case we're wrong about it
  878. * throttling.
  879. */
  880. if (count > 0)
  881. goto out;
  882. } else if (count >= 0) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  883. /* Simple timing adjustment. Allow a few polls to cope with
  884. * OS timer delays being variably reliable. wait_ms will
  885. * always equal sleep_ms when we've waited greater than or
  886. * equal to the result return time.*/
  887. delay_time_ms = bitforce->sleep_ms;
  888. if (!data->result_busy_polled)
  889. {
  890. // No busy polls before results received
  891. if (bitforce->wait_ms > delay_time_ms + (WORK_CHECK_INTERVAL_MS * 8))
  892. // ... due to poll being rather late; ignore it as an anomaly
  893. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, later than scheduled %ums (ignoring)",
  894. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms);
  895. else
  896. if (bitforce->sleep_ms > data->sleep_ms_default + (BITFORCE_CHECK_INTERVAL_MS * 0x20))
  897. {
  898. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on delayed schedule %ums; Wait time changed to: %ums (default sch)",
  899. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, data->sleep_ms_default);
  900. bitforce->sleep_ms = data->sleep_ms_default;
  901. }
  902. else
  903. {
  904. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on default schedule %ums; Wait time changed to: %ums (check interval)",
  905. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, BITFORCE_CHECK_INTERVAL_MS);
  906. bitforce->sleep_ms = BITFORCE_CHECK_INTERVAL_MS;
  907. }
  908. }
  909. else
  910. {
  911. if (data->result_busy_polled - bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  912. {
  913. bitforce->sleep_ms = data->result_busy_polled - (WORK_CHECK_INTERVAL_MS / 2);
  914. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time changed to: %ums",
  915. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms, bitforce->sleep_ms);
  916. }
  917. else
  918. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time unchanged",
  919. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms);
  920. }
  921. /* Work out the average time taken. Float for calculation, uint for display */
  922. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  923. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  924. }
  925. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  926. if (count < 0 && strncasecmp(pdevbuf, "I", 1)) {
  927. inc_hw_errors_only(thr);
  928. applog(LOG_WARNING, "%"PRIpreprv": Error: Get result reports: %s", bitforce->proc_repr, pdevbuf);
  929. bitforce_clear_buffer(bitforce);
  930. }
  931. out:
  932. bitforce->polling = false;
  933. job_results_fetched(thr);
  934. return;
  935. commerr:
  936. bitforce_comm_error(thr);
  937. goto out;
  938. }
  939. static
  940. void bitforce_process_result_nonces(struct thr_info *thr, struct work *work, char *pnoncebuf)
  941. {
  942. struct cgpu_info *bitforce = thr->cgpu;
  943. struct bitforce_data *data = bitforce->device_data;
  944. uint32_t nonce;
  945. while (1) {
  946. hex2bin((void*)&nonce, pnoncebuf, 4);
  947. nonce = be32toh(nonce);
  948. if (unlikely(data->proto == BFP_RANGE && (nonce >= work->blk.nonce ||
  949. /* FIXME: blk.nonce is probably moved on quite a bit now! */
  950. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  951. applog(LOG_WARNING, "%"PRIpreprv": Disabling broken nonce range support", bitforce->proc_repr);
  952. bitforce_change_mode(bitforce, BFP_WORK);
  953. }
  954. submit_nonce(thr, work, nonce);
  955. if (strncmp(&pnoncebuf[8], ",", 1))
  956. break;
  957. pnoncebuf += 9;
  958. }
  959. }
  960. static
  961. bool bitforce_process_qresult_line_i(struct thr_info *thr, char *midstate, char *datatail, char *buf, struct work *work)
  962. {
  963. if (!work)
  964. return false;
  965. if (memcmp(work->midstate, midstate, 32))
  966. return false;
  967. if (memcmp(&work->data[64], datatail, 12))
  968. return false;
  969. char *end;
  970. if (strtol(&buf[90], &end, 10))
  971. bitforce_process_result_nonces(thr, work, &end[1]);
  972. return true;
  973. }
  974. static
  975. void bitforce_process_qresult_line(struct thr_info *thr, char *buf, struct work *work)
  976. {
  977. struct cgpu_info *bitforce = thr->cgpu;
  978. char midstate[32], datatail[12];
  979. hex2bin((void*)midstate, buf, 32);
  980. hex2bin((void*)datatail, &buf[65], 12);
  981. if (!( bitforce_process_qresult_line_i(thr, midstate, datatail, buf, work)
  982. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->work)
  983. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->prev_work)
  984. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->next_work) ))
  985. {
  986. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queued results", bitforce->proc_repr);
  987. inc_hw_errors_only(thr);
  988. }
  989. }
  990. static inline
  991. char *next_line(char *in)
  992. {
  993. while (in[0] && (in++)[0] != '\n')
  994. {}
  995. return in;
  996. }
  997. static
  998. int64_t bitforce_job_process_results(struct thr_info *thr, struct work *work, __maybe_unused bool stopping)
  999. {
  1000. struct cgpu_info *bitforce = thr->cgpu;
  1001. struct bitforce_data *data = bitforce->device_data;
  1002. char *pnoncebuf = &data->noncebuf[0];
  1003. int count;
  1004. if (!strncasecmp(pnoncebuf, "NO-", 3))
  1005. return bitforce->nonces; /* No valid nonce found */
  1006. if (!strncasecmp(pnoncebuf, "NONCE-FOUND", 11))
  1007. {
  1008. bitforce_process_result_nonces(thr, work, &pnoncebuf[12]);
  1009. count = 1;
  1010. }
  1011. else
  1012. if (!strncasecmp(pnoncebuf, "COUNT:", 6))
  1013. {
  1014. count = 0;
  1015. pnoncebuf = next_line(pnoncebuf);
  1016. while (pnoncebuf[0])
  1017. {
  1018. bitforce_process_qresult_line(thr, pnoncebuf, work);
  1019. ++count;
  1020. pnoncebuf = next_line(pnoncebuf);
  1021. }
  1022. }
  1023. else
  1024. return 0;
  1025. // FIXME: This might have changed in the meantime (new job start, or broken)
  1026. return bitforce->nonces * count;
  1027. }
  1028. static void bitforce_shutdown(struct thr_info *thr)
  1029. {
  1030. struct cgpu_info *bitforce = thr->cgpu;
  1031. int *p_fdDev = &bitforce->device->device_fd;
  1032. BFclose(*p_fdDev);
  1033. *p_fdDev = 0;
  1034. }
  1035. static void biforce_thread_enable(struct thr_info *thr)
  1036. {
  1037. struct cgpu_info *bitforce = thr->cgpu;
  1038. bitforce_reinit(bitforce);
  1039. }
  1040. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  1041. {
  1042. struct bitforce_proc_data *procdata = bitforce->thr[0]->cgpu_data;
  1043. if (!procdata->handles_board)
  1044. return true;
  1045. return bitforce_get_temp(bitforce);
  1046. }
  1047. static bool bitforce_identify(struct cgpu_info *bitforce)
  1048. {
  1049. bitforce->flash_led = true;
  1050. return true;
  1051. }
  1052. static bool bitforce_thread_init(struct thr_info *thr)
  1053. {
  1054. struct cgpu_info *bitforce = thr->cgpu;
  1055. unsigned int wait;
  1056. struct bitforce_data *data;
  1057. struct bitforce_proc_data *procdata;
  1058. struct bitforce_init_data *initdata = bitforce->device_data;
  1059. bool sc = initdata->sc;
  1060. int xlink_id = 0, boardno = 0;
  1061. struct bitforce_proc_data *first_on_this_board;
  1062. for ( ; bitforce; bitforce = bitforce->next_proc)
  1063. {
  1064. thr = bitforce->thr[0];
  1065. if (unlikely(xlink_id > 30))
  1066. {
  1067. applog(LOG_ERR, "%"PRIpreprv": Failed to find XLINK address", bitforce->proc_repr);
  1068. dev_error(bitforce, REASON_THREAD_FAIL_INIT);
  1069. bitforce->reinit_backoff = 1e10;
  1070. continue;
  1071. }
  1072. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  1073. bitforce->device_data = data = malloc(sizeof(*data));
  1074. *data = (struct bitforce_data){
  1075. .xlink_id = xlink_id,
  1076. .next_work_ob = ">>>>>>>>|---------- MidState ----------||-DataTail-||Nonces|>>>>>>>>",
  1077. .proto = BFP_RANGE,
  1078. .sc = sc,
  1079. .sleep_ms_default = BITFORCE_SLEEP_MS,
  1080. .parallel = abs(initdata->parallels[boardno]),
  1081. .parallel_protocol = (initdata->parallels[boardno] != -1),
  1082. };
  1083. thr->cgpu_data = procdata = malloc(sizeof(*procdata));
  1084. *procdata = (struct bitforce_proc_data){
  1085. .handles_board = true,
  1086. .cgpu = bitforce,
  1087. };
  1088. if (sc)
  1089. {
  1090. // ".......S|---------- MidState ----------||-DataTail-||Nonces|E"
  1091. data->next_work_ob[8+32+12+8] = '\xAA';
  1092. data->next_work_obs = &data->next_work_ob[7];
  1093. if (bitforce->drv == &bitforce_queue_api)
  1094. {
  1095. bitforce_change_mode(bitforce, BFP_BQUEUE);
  1096. bitforce->sleep_ms = data->sleep_ms_default = 100;
  1097. timer_set_delay_from_now(&thr->tv_poll, 0);
  1098. data->queued_max = data->parallel * 2;
  1099. if (data->queued_max < BITFORCE_MIN_QUEUED_MAX)
  1100. data->queued_max = BITFORCE_MIN_QUEUED_MAX;
  1101. if (data->queued_max > BITFORCE_MAX_QUEUED_MAX)
  1102. data->queued_max = BITFORCE_MAX_QUEUED_MAX;
  1103. }
  1104. else
  1105. bitforce_change_mode(bitforce, BFP_QUEUE);
  1106. }
  1107. else
  1108. {
  1109. data->next_work_obs = &data->next_work_ob[0];
  1110. // Unconditionally change away from cold-initialized BFP_RANGE, to allow for setting up other variables
  1111. bitforce_change_mode(bitforce, BFP_WORK);
  1112. /* Initially enable support for nonce range and disable it later if it
  1113. * fails */
  1114. if (opt_bfl_noncerange)
  1115. bitforce_change_mode(bitforce, BFP_RANGE);
  1116. }
  1117. bitforce->status = LIFE_INIT2;
  1118. first_on_this_board = procdata;
  1119. for (int proc = 1; proc < data->parallel; ++proc)
  1120. {
  1121. bitforce = bitforce->next_proc;
  1122. assert(bitforce);
  1123. thr = bitforce->thr[0];
  1124. thr->queue_full = true;
  1125. thr->cgpu_data = procdata = malloc(sizeof(*procdata));
  1126. *procdata = *first_on_this_board;
  1127. procdata->handles_board = false;
  1128. procdata->cgpu = bitforce;
  1129. bitforce->device_data = data;
  1130. bitforce->status = LIFE_INIT2;
  1131. }
  1132. applog(LOG_DEBUG, "%s: Board %d: %"PRIpreprv"-%"PRIpreprv, bitforce->dev_repr, boardno, first_on_this_board->cgpu->proc_repr, bitforce->proc_repr);
  1133. ++boardno;
  1134. while (xlink_id < 31 && !(initdata->devmask & (1 << ++xlink_id)))
  1135. {}
  1136. }
  1137. bitforce = thr->cgpu;
  1138. free(initdata->parallels);
  1139. free(initdata);
  1140. /* Pause each new thread at least 100ms between initialising
  1141. * so the devices aren't making calls all at the same time. */
  1142. wait = thr->id * MAX_START_DELAY_MS;
  1143. applog(LOG_DEBUG, "%s: Delaying start by %dms", bitforce->dev_repr, wait / 1000);
  1144. nmsleep(wait);
  1145. return true;
  1146. }
  1147. static struct api_data *bitforce_drv_stats(struct cgpu_info *cgpu)
  1148. {
  1149. struct bitforce_data *data = cgpu->device_data;
  1150. struct api_data *root = NULL;
  1151. // Warning, access to these is not locked - but we don't really
  1152. // care since hashing performance is way more important than
  1153. // locking access to displaying API debug 'stats'
  1154. // If locking becomes an issue for any of them, use copy_data=true also
  1155. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  1156. if (data->proto != BFP_BQUEUE)
  1157. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  1158. if (data->temp[0] > 0 && data->temp[1] > 0)
  1159. {
  1160. root = api_add_temp(root, "Temperature0", &(data->temp[0]), false);
  1161. root = api_add_temp(root, "Temperature1", &(data->temp[1]), false);
  1162. }
  1163. return root;
  1164. }
  1165. void bitforce_poll(struct thr_info *thr)
  1166. {
  1167. struct cgpu_info *bitforce = thr->cgpu;
  1168. struct bitforce_data *data = bitforce->device_data;
  1169. int poll = data->poll_func;
  1170. thr->tv_poll.tv_sec = -1;
  1171. data->poll_func = 0;
  1172. switch (poll)
  1173. {
  1174. case 1:
  1175. bitforce_job_start(thr);
  1176. break;
  1177. case 2:
  1178. bitforce_job_get_results(thr, thr->work);
  1179. break;
  1180. default:
  1181. applog(LOG_ERR, "%"PRIpreprv": Unexpected poll from device API!", thr->cgpu->proc_repr);
  1182. }
  1183. }
  1184. static
  1185. char *bitforce_set_device(struct cgpu_info *proc, char *option, char *setting, char *replybuf)
  1186. {
  1187. struct bitforce_data *data = proc->device_data;
  1188. pthread_mutex_t *mutexp = &proc->device->device_mutex;
  1189. int fd;
  1190. if (!strcasecmp(option, "help"))
  1191. {
  1192. sprintf(replybuf, "fanmode: range 0-4 (low to fast) or 5 (auto)");
  1193. return replybuf;
  1194. }
  1195. if (!strcasecmp(option, "fanmode"))
  1196. {
  1197. if (!setting || !*setting)
  1198. {
  1199. sprintf(replybuf, "missing fanmode setting");
  1200. return replybuf;
  1201. }
  1202. if (setting[1] || ((setting[0] < '0' || setting[0] > '5') && setting[0] != '9'))
  1203. {
  1204. sprintf(replybuf, "invalid fanmode setting");
  1205. return replybuf;
  1206. }
  1207. char cmd[4] = "Z5X";
  1208. cmd[1] = setting[0];
  1209. mutex_lock(mutexp);
  1210. fd = proc->device->device_fd;
  1211. bitforce_cmd1(fd, data->xlink_id, replybuf, 256, cmd);
  1212. mutex_unlock(mutexp);
  1213. return replybuf;
  1214. }
  1215. sprintf(replybuf, "Unknown option: %s", option);
  1216. return replybuf;
  1217. }
  1218. struct device_drv bitforce_drv = {
  1219. .dname = "bitforce",
  1220. .name = "BFL",
  1221. .drv_detect = bitforce_detect,
  1222. .get_api_stats = bitforce_drv_stats,
  1223. .minerloop = minerloop_async,
  1224. .reinit_device = bitforce_reinit,
  1225. .get_stats = bitforce_get_stats,
  1226. .set_device = bitforce_set_device,
  1227. .identify_device = bitforce_identify,
  1228. .thread_prepare = bitforce_thread_prepare,
  1229. .thread_init = bitforce_thread_init,
  1230. .job_prepare = bitforce_job_prepare,
  1231. .job_start = bitforce_job_start,
  1232. .job_get_results = bitforce_job_get_results,
  1233. .poll = bitforce_poll,
  1234. .job_process_results = bitforce_job_process_results,
  1235. .thread_shutdown = bitforce_shutdown,
  1236. .thread_enable = biforce_thread_enable
  1237. };
  1238. static inline
  1239. void bitforce_set_queue_full(struct thr_info *thr)
  1240. {
  1241. struct cgpu_info *bitforce = thr->cgpu;
  1242. struct bitforce_data *data = bitforce->device_data;
  1243. thr->queue_full = (data->queued + data->ready_to_queue >= data->queued_max) || (data->ready_to_queue >= BITFORCE_MAX_BQUEUE_AT_ONCE);
  1244. }
  1245. static
  1246. bool bitforce_send_queue(struct thr_info *thr)
  1247. {
  1248. struct cgpu_info *bitforce = thr->cgpu;
  1249. struct bitforce_data *data = bitforce->device_data;
  1250. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  1251. int fd = bitforce->device->device_fd;
  1252. struct work *work;
  1253. if (unlikely(!(fd && data->ready_to_queue)))
  1254. return false;
  1255. char buf[0x100];
  1256. int queued_ok;
  1257. size_t qjs_sz = (32 + 12 + 2);
  1258. size_t qjp_sz = 4 + (qjs_sz * data->ready_to_queue);
  1259. uint8_t qjp[qjp_sz], *qjs;
  1260. qjp[0] = qjp_sz - 1;
  1261. qjp[1] = 0xc1;
  1262. qjp[2] = data->ready_to_queue;
  1263. qjp[qjp_sz - 1] = 0xfe;
  1264. qjs = &qjp[qjp_sz - 1];
  1265. work = thr->work_list->prev;
  1266. for (int i = data->ready_to_queue; i > 0; --i, work = work->prev)
  1267. {
  1268. *(--qjs) = 0xaa;
  1269. memcpy(qjs -= 12, work->data + 64, 12);
  1270. memcpy(qjs -= 32, work->midstate, 32);
  1271. *(--qjs) = 45;
  1272. }
  1273. mutex_lock(mutexp);
  1274. bitforce_cmd2(fd, data->xlink_id, buf, sizeof(buf), "ZWX", qjp, qjp_sz);
  1275. mutex_unlock(mutexp);
  1276. if (!strncasecmp(buf, "ERR:QUEUE", 9))
  1277. {
  1278. // Queue full :(
  1279. applog(LOG_DEBUG, "%"PRIpreprv": Device queue full while attempting to append %d jobs (queued<=%d)",
  1280. bitforce->proc_repr,
  1281. data->ready_to_queue, data->queued);
  1282. thr->queue_full = true;
  1283. return false;
  1284. }
  1285. if (strncasecmp(buf, "OK:QUEUED", 9))
  1286. {
  1287. // TODO: ERR:UNKNOWN COMMAND
  1288. applog(LOG_DEBUG, "%"PRIpreprv": Unexpected error attempting to append %d jobs (queued<=%d): %s",
  1289. bitforce->proc_repr,
  1290. data->ready_to_queue, data->queued, buf);
  1291. return false;
  1292. }
  1293. if (!data->queued)
  1294. cgtime(&data->tv_hashmeter_start);
  1295. queued_ok = atoi(&buf[9]);
  1296. data->queued += queued_ok;
  1297. applog(LOG_DEBUG, "%"PRIpreprv": Successfully queued %d/%d jobs on device (queued<=%d)",
  1298. bitforce->proc_repr,
  1299. queued_ok, data->ready_to_queue, data->queued);
  1300. data->ready_to_queue -= queued_ok;
  1301. thr->queue_full = data->ready_to_queue;
  1302. data->just_flushed = false;
  1303. data->want_to_send_queue = false;
  1304. return true;
  1305. }
  1306. void work_list_del(struct work **head, struct work *work)
  1307. {
  1308. DL_DELETE(*head, work);
  1309. free_work(work);
  1310. }
  1311. static
  1312. bool bitforce_queue_do_results(struct thr_info *thr)
  1313. {
  1314. struct cgpu_info *bitforce = thr->cgpu;
  1315. struct bitforce_data *data = bitforce->device_data;
  1316. int fd = bitforce->device->device_fd;
  1317. int count;
  1318. int fcount;
  1319. char *noncebuf, *buf, *end;
  1320. unsigned char midstate[32], datatail[12];
  1321. struct work *work, *tmpwork, *thiswork;
  1322. struct timeval tv_now, tv_elapsed;
  1323. long chipno = 0; // Initialized value is used for non-parallelized boards
  1324. struct cgpu_info *chip_cgpu;
  1325. struct thr_info *chip_thr;
  1326. int counts[data->parallel];
  1327. if (unlikely(!fd))
  1328. return false;
  1329. again:
  1330. noncebuf = &data->noncebuf[0];
  1331. count = bitforce_zox(thr, "ZOX");
  1332. if (unlikely(count < 0))
  1333. {
  1334. applog(LOG_ERR, "%"PRIpreprv": Received unexpected queue result response: %s", bitforce->proc_repr, noncebuf);
  1335. inc_hw_errors_only(thr);
  1336. return false;
  1337. }
  1338. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results on poll (max=%d)", bitforce->proc_repr, count, (int)BITFORCE_MAX_QRESULTS);
  1339. if (!count)
  1340. return true;
  1341. fcount = 0;
  1342. for (int i = 0; i < data->parallel; ++i)
  1343. counts[i] = 0;
  1344. noncebuf = next_line(noncebuf);
  1345. while ((buf = noncebuf)[0])
  1346. {
  1347. if ( (noncebuf = next_line(buf)) )
  1348. noncebuf[-1] = '\0';
  1349. if (strlen(buf) <= 90)
  1350. {
  1351. applog(LOG_ERR, "%"PRIpreprv": Gibberish within queue results: %s", bitforce->proc_repr, buf);
  1352. continue;
  1353. }
  1354. hex2bin(midstate, buf, 32);
  1355. hex2bin(datatail, &buf[65], 12);
  1356. thiswork = NULL;
  1357. DL_FOREACH(thr->work_list, work)
  1358. {
  1359. if (unlikely(memcmp(work->midstate, midstate, 32)))
  1360. continue;
  1361. if (unlikely(memcmp(&work->data[64], datatail, 12)))
  1362. continue;
  1363. thiswork = work;
  1364. break;
  1365. }
  1366. end = &buf[89];
  1367. chip_cgpu = bitforce;
  1368. if (data->parallel_protocol)
  1369. {
  1370. chipno = strtol(&end[1], &end, 16);
  1371. if (chipno >= data->parallel)
  1372. {
  1373. applog(LOG_ERR, "%"PRIpreprv": Chip number out of range for queue result: %s", chip_cgpu->proc_repr, buf);
  1374. chipno = 0;
  1375. }
  1376. for (int i = 0; i < chipno; ++i)
  1377. chip_cgpu = chip_cgpu->next_proc;
  1378. }
  1379. chip_thr = chip_cgpu->thr[0];
  1380. applog(LOG_DEBUG, "%"PRIpreprv": Queue result: %s", chip_cgpu->proc_repr, buf);
  1381. if (unlikely(!thiswork))
  1382. {
  1383. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queue results: %s", chip_cgpu->proc_repr, buf);
  1384. inc_hw_errors_only(chip_thr);
  1385. goto next_qline;
  1386. }
  1387. if (unlikely(!end[0]))
  1388. {
  1389. applog(LOG_ERR, "%"PRIpreprv": Missing nonce count in queue results: %s", chip_cgpu->proc_repr, buf);
  1390. goto finishresult;
  1391. }
  1392. if (strtol(&end[1], &end, 10))
  1393. {
  1394. if (unlikely(!end[0]))
  1395. {
  1396. applog(LOG_ERR, "%"PRIpreprv": Missing nonces in queue results: %s", chip_cgpu->proc_repr, buf);
  1397. goto finishresult;
  1398. }
  1399. bitforce_process_result_nonces(chip_thr, work, &end[1]);
  1400. }
  1401. ++fcount;
  1402. ++counts[chipno];
  1403. finishresult:
  1404. if (data->parallel == 1)
  1405. {
  1406. // Queue results are in order, so anything queued prior this is lost
  1407. // Delete all queued work up to, and including, this one
  1408. DL_FOREACH_SAFE(thr->work_list, work, tmpwork)
  1409. {
  1410. work_list_del(&thr->work_list, work);
  1411. --data->queued;
  1412. if (work == thiswork)
  1413. break;
  1414. }
  1415. }
  1416. else
  1417. {
  1418. // Parallel processors means the results might not be in order
  1419. // This could leak if jobs get lost, hence the sanity checks using "ZqX"
  1420. work_list_del(&thr->work_list, thiswork);
  1421. --data->queued;
  1422. }
  1423. next_qline: (void)0;
  1424. }
  1425. bitforce_set_queue_full(thr);
  1426. if (count >= BITFORCE_MAX_QRESULTS)
  1427. goto again;
  1428. if (data->parallel == 1 && (
  1429. (fcount < BITFORCE_GOAL_QRESULTS && bitforce->sleep_ms < BITFORCE_MAX_QRESULT_WAIT && data->queued > 1)
  1430. || (fcount > BITFORCE_GOAL_QRESULTS && bitforce->sleep_ms > BITFORCE_MIN_QRESULT_WAIT) ))
  1431. {
  1432. unsigned int old_sleep_ms = bitforce->sleep_ms;
  1433. bitforce->sleep_ms = (uint32_t)bitforce->sleep_ms * BITFORCE_GOAL_QRESULTS / (fcount ?: 1);
  1434. if (bitforce->sleep_ms > BITFORCE_MAX_QRESULT_WAIT)
  1435. bitforce->sleep_ms = BITFORCE_MAX_QRESULT_WAIT;
  1436. if (bitforce->sleep_ms < BITFORCE_MIN_QRESULT_WAIT)
  1437. bitforce->sleep_ms = BITFORCE_MIN_QRESULT_WAIT;
  1438. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time changed to: %ums (queued<=%d)",
  1439. bitforce->proc_repr, fcount, old_sleep_ms, bitforce->sleep_ms, data->queued);
  1440. }
  1441. else
  1442. applog(LOG_DEBUG, "%"PRIpreprv": Received %d queue results after %ums; Wait time unchanged (queued<=%d)",
  1443. bitforce->proc_repr, fcount, bitforce->sleep_ms, data->queued);
  1444. cgtime(&tv_now);
  1445. timersub(&tv_now, &data->tv_hashmeter_start, &tv_elapsed);
  1446. chip_cgpu = bitforce;
  1447. for (int i = 0; i < data->parallel; ++i, (chip_cgpu = chip_cgpu->next_proc))
  1448. {
  1449. chip_thr = chip_cgpu->thr[0];
  1450. hashes_done(chip_thr, (uint64_t)bitforce->nonces * counts[i], &tv_elapsed, NULL);
  1451. }
  1452. data->tv_hashmeter_start = tv_now;
  1453. return true;
  1454. }
  1455. static
  1456. bool bitforce_queue_append(struct thr_info *thr, struct work *work)
  1457. {
  1458. struct cgpu_info *bitforce = thr->cgpu;
  1459. struct bitforce_data *data = bitforce->device_data;
  1460. bool rv, ndq;
  1461. bitforce_set_queue_full(thr);
  1462. rv = !thr->queue_full;
  1463. if (rv)
  1464. {
  1465. DL_APPEND(thr->work_list, work);
  1466. ++data->ready_to_queue;
  1467. applog(LOG_DEBUG, "%"PRIpreprv": Appending to driver queue (max=%u, ready=%d, queued<=%d)",
  1468. bitforce->proc_repr,
  1469. (unsigned)data->queued_max, data->ready_to_queue, data->queued);
  1470. bitforce_set_queue_full(thr);
  1471. }
  1472. else
  1473. if (!data->ready_to_queue)
  1474. return rv;
  1475. ndq = !data->queued;
  1476. if ((ndq) // Device is idle
  1477. || (data->ready_to_queue >= BITFORCE_MAX_BQUEUE_AT_ONCE) // ...or 5 items ready to go
  1478. || (thr->queue_full) // ...or done filling queue
  1479. || (data->just_flushed) // ...or queue was just flushed (only remaining job is partly done already)
  1480. )
  1481. {
  1482. if (!bitforce_send_queue(thr))
  1483. {
  1484. // Problem sending queue, retry again in a few seconds
  1485. applog(LOG_ERR, "%"PRIpreprv": Failed to send queue", bitforce->proc_repr);
  1486. inc_hw_errors_only(thr);
  1487. data->want_to_send_queue = true;
  1488. }
  1489. }
  1490. return rv;
  1491. }
  1492. struct _jobinfo {
  1493. uint8_t key[32+12];
  1494. int instances;
  1495. UT_hash_handle hh;
  1496. };
  1497. static
  1498. void bitforce_queue_flush(struct thr_info *thr)
  1499. {
  1500. struct bitforce_proc_data *procdata = thr->cgpu_data;
  1501. if (!procdata->handles_board)
  1502. return;
  1503. struct cgpu_info *bitforce = thr->cgpu;
  1504. struct bitforce_data *data = bitforce->device_data;
  1505. char *buf = &data->noncebuf[0], *buf2 = NULL;
  1506. const char *cmd = "ZqX";
  1507. unsigned flushed;
  1508. struct _jobinfo *processing = NULL, *item, *this;
  1509. if (data->parallel == 1)
  1510. // Pre-parallelization neither needs nor supports "ZqX"
  1511. cmd = "ZQX";
  1512. // TODO: Call "ZQX" most of the time: don't need to do sanity checks so often
  1513. bitforce_zox(thr, cmd);
  1514. if (!strncasecmp(buf, "OK:FLUSHED", 10))
  1515. flushed = atoi(&buf[10]);
  1516. else
  1517. if ((!strncasecmp(buf, "COUNT:", 6)) && (buf2 = strstr(buf, "FLUSHED:")) )
  1518. {
  1519. flushed = atoi(&buf2[8]);
  1520. buf2 = next_line(buf2);
  1521. }
  1522. else
  1523. {
  1524. applog(LOG_DEBUG, "%"PRIpreprv": Failed to flush device queue: %s", bitforce->proc_repr, buf);
  1525. flushed = 0;
  1526. }
  1527. data->queued -= flushed;
  1528. applog(LOG_DEBUG, "%"PRIpreprv": Flushed %u jobs from device and %d from driver (queued<=%d)",
  1529. bitforce->proc_repr, flushed, data->ready_to_queue, data->queued);
  1530. flushed += data->ready_to_queue;
  1531. data->ready_to_queue = 0;
  1532. while (flushed--)
  1533. work_list_del(&thr->work_list, thr->work_list->prev);
  1534. bitforce_set_queue_full(thr);
  1535. data->just_flushed = true;
  1536. data->want_to_send_queue = false;
  1537. // "ZqX" returns jobs in progress, allowing us to sanity check
  1538. // NOTE: Must process buffer into hash table BEFORE calling bitforce_queue_do_results, which clobbers it
  1539. // NOTE: Must do actual sanity check AFTER calling bitforce_queue_do_results, to ensure we don't delete completed jobs
  1540. if (buf2)
  1541. {
  1542. // First, turn buf2 into a hash
  1543. for ( ; buf2[0]; buf2 = next_line(buf2))
  1544. {
  1545. this = malloc(sizeof(*this));
  1546. hex2bin(&this->key[ 0], &buf2[ 0], 32);
  1547. hex2bin(&this->key[32], &buf2[65], 12);
  1548. HASH_FIND(hh, processing, &this->key[0], sizeof(this->key), item);
  1549. if (likely(!item))
  1550. {
  1551. this->instances = 1;
  1552. HASH_ADD(hh, processing, key, sizeof(this->key), this);
  1553. }
  1554. else
  1555. {
  1556. // This should really only happen in testing/benchmarking...
  1557. ++item->instances;
  1558. free(this);
  1559. }
  1560. }
  1561. }
  1562. bitforce_queue_do_results(thr);
  1563. if (buf2)
  1564. {
  1565. struct work *work, *tmp;
  1566. uint8_t key[32+12];
  1567. // Now iterate over the work_list and delete anything not in the hash
  1568. DL_FOREACH_SAFE(thr->work_list, work, tmp)
  1569. {
  1570. memcpy(&key[ 0], work->midstate, 32);
  1571. memcpy(&key[32], &work->data[64], 12);
  1572. HASH_FIND(hh, processing, &key[0], sizeof(key), item);
  1573. if (unlikely(!item))
  1574. {
  1575. char hex[89];
  1576. bin2hex(hex, key, 32+12);
  1577. applog(LOG_WARNING, "%"PRIpreprv": Sanity check: Device is missing queued job! %s", bitforce->proc_repr, hex);
  1578. work_list_del(&thr->work_list, work);
  1579. continue;
  1580. }
  1581. if (likely(!--item->instances))
  1582. {
  1583. HASH_DEL(processing, item);
  1584. free(item);
  1585. }
  1586. }
  1587. if (unlikely( (flushed = HASH_COUNT(processing)) ))
  1588. {
  1589. //applog(LOG_WARNING, "%"PRIpreprv": Sanity check: Device is working on %d unknown jobs!", bitforce->proc_repr, flushed);
  1590. // FIXME: Probably these were jobs finished after ZqX, included in the result check we just did
  1591. // NOTE: We need to do that result check first to avoid deleting work_list items for things just solved
  1592. HASH_ITER(hh, processing, item, this)
  1593. {
  1594. HASH_DEL(processing, item);
  1595. free(item);
  1596. }
  1597. }
  1598. }
  1599. }
  1600. static
  1601. void bitforce_queue_poll(struct thr_info *thr)
  1602. {
  1603. struct cgpu_info *bitforce = thr->cgpu;
  1604. struct bitforce_data *data = bitforce->device_data;
  1605. unsigned long sleep_us;
  1606. if (data->queued)
  1607. bitforce_queue_do_results(thr);
  1608. sleep_us = (unsigned long)bitforce->sleep_ms * 1000;
  1609. if (data->want_to_send_queue)
  1610. if (!bitforce_send_queue(thr))
  1611. if (!data->queued)
  1612. {
  1613. applog(LOG_ERR, "%"PRIpreprv": Failed to send queue, and queue empty; retrying after 1 second", bitforce->proc_repr);
  1614. inc_hw_errors_only(thr);
  1615. sleep_us = 1000000;
  1616. }
  1617. timer_set_delay_from_now(&thr->tv_poll, sleep_us);
  1618. }
  1619. static void bitforce_queue_thread_deven(struct thr_info *thr)
  1620. {
  1621. struct cgpu_info *bitforce = thr->cgpu, *thisbf;
  1622. struct bitforce_data *data = bitforce->device_data;
  1623. struct thr_info *thisthr;
  1624. for (thisbf = bitforce; thisbf && thisbf->device_data == data; thisbf = thisbf->next_proc)
  1625. {
  1626. thisthr = bitforce->thr[0];
  1627. thisthr->pause = thr->pause;
  1628. thisbf->deven = bitforce->deven;
  1629. }
  1630. }
  1631. static void bitforce_queue_thread_disable(struct thr_info *thr)
  1632. {
  1633. // Disable other threads sharing the same queue
  1634. bitforce_queue_thread_deven(thr);
  1635. }
  1636. static void bitforce_queue_thread_enable(struct thr_info *thr)
  1637. {
  1638. // TODO: Maybe reinit?
  1639. // Enable other threads sharing the same queue
  1640. bitforce_queue_thread_deven(thr);
  1641. }
  1642. struct device_drv bitforce_queue_api = {
  1643. .dname = "bitforce_queue",
  1644. .name = "BFL",
  1645. .minerloop = minerloop_queue,
  1646. .reinit_device = bitforce_reinit,
  1647. .get_api_stats = bitforce_drv_stats,
  1648. .get_stats = bitforce_get_stats,
  1649. .set_device = bitforce_set_device,
  1650. .identify_device = bitforce_identify,
  1651. .thread_prepare = bitforce_thread_prepare,
  1652. .thread_init = bitforce_thread_init,
  1653. .queue_append = bitforce_queue_append,
  1654. .queue_flush = bitforce_queue_flush,
  1655. .poll = bitforce_queue_poll,
  1656. .thread_shutdown = bitforce_shutdown,
  1657. .thread_disable = bitforce_queue_thread_disable,
  1658. .thread_enable = bitforce_queue_thread_enable,
  1659. };