driver-bitforce.c 42 KB

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