driver-bitforce.c 51 KB

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