driver-bitforce.c 44 KB

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