driver-bitforce.c 52 KB

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