driver-bitforce.c 43 KB

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