driver-bitforce.c 43 KB

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