driver-bitforce.c 45 KB

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