driver-bitforce.c 53 KB

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