driver-bitforce.c 52 KB

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