driver-bitforce.c 54 KB

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