driver-bitforce.c 57 KB

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