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