driver-bitforce.c 51 KB

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