driver-bitforce.c 42 KB

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