driver-bitforce.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. enum bitforce_proto {
  33. BFP_WORK,
  34. BFP_RANGE,
  35. BFP_QUEUE,
  36. };
  37. static const char *protonames[] = {
  38. "full work",
  39. "nonce range",
  40. "work queue",
  41. };
  42. struct device_api bitforce_api;
  43. // Code must deal with a timeout
  44. #define BFopen(devpath) serial_open(devpath, 0, 250, true)
  45. static void BFgets(char *buf, size_t bufLen, int fd)
  46. {
  47. do {
  48. buf[0] = '\0';
  49. --bufLen;
  50. } while (likely(bufLen && read(fd, buf, 1) == 1 && (buf++)[0] != '\n'));
  51. buf[0] = '\0';
  52. }
  53. static ssize_t BFwrite(int fd, const void *buf, ssize_t bufLen)
  54. {
  55. if ((bufLen) != write(fd, buf, bufLen))
  56. return 0;
  57. else
  58. return bufLen;
  59. }
  60. static ssize_t bitforce_send(int fd, int procid, const void *buf, ssize_t bufLen)
  61. {
  62. if (!procid)
  63. return BFwrite(fd, buf, bufLen);
  64. if (bufLen > 255)
  65. return -1;
  66. size_t bufLeft = bufLen + 3;
  67. char realbuf[bufLeft], *bufp;
  68. ssize_t rv;
  69. memcpy(&realbuf[3], buf, bufLen);
  70. realbuf[0] = '@';
  71. realbuf[1] = procid;
  72. realbuf[2] = bufLen;
  73. bufp = realbuf;
  74. while (true)
  75. {
  76. rv = BFwrite(fd, bufp, bufLeft);
  77. if (rv <= 0)
  78. return rv;
  79. bufLeft -= rv;
  80. }
  81. return bufLen;
  82. }
  83. static
  84. void bitforce_cmd1(int fd, int procid, void *buf, size_t bufsz, const char *cmd)
  85. {
  86. bitforce_send(fd, procid, cmd, 3);
  87. BFgets(buf, bufsz, fd);
  88. }
  89. static
  90. void bitforce_cmd2(int fd, int procid, void *buf, size_t bufsz, const char *cmd, void *data, size_t datasz)
  91. {
  92. bitforce_cmd1(fd, procid, buf, bufsz, cmd);
  93. if (strncasecmp(buf, "OK", 2))
  94. return;
  95. bitforce_send(fd, procid, data, datasz);
  96. BFgets(buf, bufsz, fd);
  97. }
  98. #define BFclose(fd) close(fd)
  99. static bool bitforce_detect_one(const char *devpath)
  100. {
  101. int fdDev = serial_open(devpath, 0, 10, true);
  102. struct cgpu_info *bitforce;
  103. char pdevbuf[0x100];
  104. size_t pdevbuf_len;
  105. char *s;
  106. int procs = 1;
  107. bool sc = false;
  108. applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
  109. if (unlikely(fdDev == -1)) {
  110. applog(LOG_DEBUG, "BFL: Failed to open %s", devpath);
  111. return false;
  112. }
  113. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  114. if (unlikely(!pdevbuf[0])) {
  115. applog(LOG_DEBUG, "BFL: Error reading/timeout (ZGX)");
  116. return 0;
  117. }
  118. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  119. applog(LOG_DEBUG, "BFL: Didn't recognise BitForce on %s", devpath);
  120. BFclose(fdDev);
  121. return false;
  122. }
  123. applog(LOG_DEBUG, "Found BitForce device on %s", devpath);
  124. for ( bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZCX");
  125. strncasecmp(pdevbuf, "OK", 2);
  126. BFgets(pdevbuf, sizeof(pdevbuf), fdDev) )
  127. {
  128. pdevbuf_len = strlen(pdevbuf);
  129. if (unlikely(!pdevbuf_len))
  130. continue;
  131. pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
  132. applog(LOG_DEBUG, " %s", pdevbuf);
  133. if (!strncasecmp(pdevbuf, "DEVICES IN CHAIN:", 17))
  134. procs = atoi(&pdevbuf[17]);
  135. else
  136. if (!strncasecmp(pdevbuf, "DEVICE:", 7) && strstr(pdevbuf, "SC"))
  137. sc = true;
  138. }
  139. BFclose(fdDev);
  140. // We have a real BitForce!
  141. bitforce = calloc(1, sizeof(*bitforce));
  142. bitforce->api = &bitforce_api;
  143. bitforce->device_path = strdup(devpath);
  144. bitforce->deven = DEV_ENABLED;
  145. bitforce->procs = procs;
  146. bitforce->threads = 1;
  147. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  148. s[0] = '\0';
  149. bitforce->name = strdup(pdevbuf + 7);
  150. }
  151. bitforce->cgpu_data = (void*)sc;
  152. mutex_init(&bitforce->device_mutex);
  153. return add_cgpu(bitforce);
  154. }
  155. static int bitforce_detect_auto(void)
  156. {
  157. return serial_autodetect(bitforce_detect_one, "BitFORCE", "SHA256");
  158. }
  159. static void bitforce_detect(void)
  160. {
  161. serial_detect_auto(&bitforce_api, bitforce_detect_one, bitforce_detect_auto);
  162. }
  163. struct bitforce_data {
  164. unsigned char next_work_ob[70]; // Data aligned for 32-bit access
  165. unsigned char *next_work_obs; // Start of data to send
  166. unsigned char next_work_obsz;
  167. const char *next_work_cmd;
  168. char noncebuf[0x200]; // Large enough for 3 works of queue results
  169. int poll_func;
  170. enum bitforce_proto proto;
  171. bool sc;
  172. bool queued;
  173. unsigned result_busy_polled;
  174. unsigned sleep_ms_default;
  175. };
  176. static void bitforce_clear_buffer(struct cgpu_info *);
  177. static
  178. void bitforce_comm_error(struct thr_info *thr)
  179. {
  180. struct cgpu_info *bitforce = thr->cgpu;
  181. struct bitforce_data *data = bitforce->cgpu_data;
  182. int *p_fdDev = &bitforce->device->device_fd;
  183. data->noncebuf[0] = '\0';
  184. applog(LOG_ERR, "%"PRIpreprv": Comms error", bitforce->proc_repr);
  185. dev_error(bitforce, REASON_DEV_COMMS_ERROR);
  186. ++bitforce->hw_errors;
  187. ++hw_errors;
  188. BFclose(*p_fdDev);
  189. int fd = *p_fdDev = BFopen(bitforce->device_path);
  190. if (fd == -1)
  191. {
  192. applog(LOG_ERR, "%s: Error reopening %s", bitforce->dev_repr, bitforce->device_path);
  193. return;
  194. }
  195. /* empty read buffer */
  196. bitforce_clear_buffer(bitforce);
  197. }
  198. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  199. {
  200. float gt = bitforce->temp;
  201. if (gt > 0)
  202. tailsprintf(buf, "%5.1fC ", gt);
  203. else
  204. tailsprintf(buf, " ", gt);
  205. tailsprintf(buf, " | ");
  206. }
  207. static bool bitforce_thread_prepare(struct thr_info *thr)
  208. {
  209. struct cgpu_info *bitforce = thr->cgpu;
  210. int fdDev = BFopen(bitforce->device_path);
  211. struct timeval now;
  212. if (unlikely(fdDev == -1)) {
  213. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, bitforce->device_path);
  214. return false;
  215. }
  216. bitforce->device_fd = fdDev;
  217. applog(LOG_INFO, "%s: Opened %s", bitforce->dev_repr, bitforce->device_path);
  218. gettimeofday(&now, NULL);
  219. get_datestamp(bitforce->init, &now);
  220. return true;
  221. }
  222. static void bitforce_clear_buffer(struct cgpu_info *bitforce)
  223. {
  224. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  225. int fdDev = bitforce->device->device_fd;
  226. char pdevbuf[0x100];
  227. int count = 0;
  228. if (!fdDev)
  229. return;
  230. applog(LOG_DEBUG, "%"PRIpreprv": Clearing read buffer", bitforce->proc_repr);
  231. mutex_lock(mutexp);
  232. do {
  233. pdevbuf[0] = '\0';
  234. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  235. } while (pdevbuf[0] && (++count < 10));
  236. mutex_unlock(mutexp);
  237. }
  238. void bitforce_init(struct cgpu_info *bitforce)
  239. {
  240. const char *devpath = bitforce->device_path;
  241. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  242. int *p_fdDev = &bitforce->device->device_fd;
  243. int fdDev = *p_fdDev, retries = 0;
  244. char pdevbuf[0x100];
  245. char *s;
  246. applog(LOG_WARNING, "%"PRIpreprv": Re-initialising", bitforce->proc_repr);
  247. bitforce_clear_buffer(bitforce);
  248. mutex_lock(mutexp);
  249. if (fdDev) {
  250. BFclose(fdDev);
  251. sleep(5);
  252. }
  253. *p_fdDev = 0;
  254. fdDev = BFopen(devpath);
  255. if (unlikely(fdDev == -1)) {
  256. mutex_unlock(mutexp);
  257. applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, devpath);
  258. return;
  259. }
  260. do {
  261. bitforce_cmd1(fdDev, 0, pdevbuf, sizeof(pdevbuf), "ZGX");
  262. if (unlikely(!pdevbuf[0])) {
  263. mutex_unlock(mutexp);
  264. applog(LOG_ERR, "%s: Error reading/timeout (ZGX)", bitforce->dev_repr);
  265. return;
  266. }
  267. if (retries++)
  268. nmsleep(10);
  269. } while (!strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  270. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  271. mutex_unlock(mutexp);
  272. applog(LOG_ERR, "%s: Didn't recognise BitForce on %s returned: %s", bitforce->dev_repr, devpath, pdevbuf);
  273. return;
  274. }
  275. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  276. s[0] = '\0';
  277. free(bitforce->name);
  278. bitforce->name = strdup(pdevbuf + 7);
  279. }
  280. *p_fdDev = fdDev;
  281. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  282. mutex_unlock(mutexp);
  283. }
  284. static void bitforce_flash_led(struct cgpu_info *bitforce)
  285. {
  286. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  287. int fdDev = bitforce->device->device_fd;
  288. if (!fdDev)
  289. return;
  290. /* Do not try to flash the led if we're polling for a result to
  291. * minimise the chance of interleaved results */
  292. if (bitforce->polling)
  293. return;
  294. /* It is not critical flashing the led so don't get stuck if we
  295. * can't grab the mutex here */
  296. if (mutex_trylock(mutexp))
  297. return;
  298. char pdevbuf[0x100];
  299. bitforce_cmd1(fdDev, bitforce->proc_id, pdevbuf, sizeof(pdevbuf), "ZMX");
  300. /* Once we've tried - don't do it until told to again */
  301. bitforce->flash_led = false;
  302. /* However, this stops anything else getting a reply
  303. * So best to delay any other access to the BFL */
  304. sleep(4);
  305. mutex_unlock(mutexp);
  306. return; // nothing is returned by the BFL
  307. }
  308. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  309. {
  310. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  311. int fdDev = bitforce->device->device_fd;
  312. char pdevbuf[0x100];
  313. char *s;
  314. if (!fdDev)
  315. return false;
  316. /* Do not try to get the temperature if we're polling for a result to
  317. * minimise the chance of interleaved results */
  318. if (bitforce->polling)
  319. return true;
  320. // Flash instead of Temp - doing both can be too slow
  321. if (bitforce->flash_led) {
  322. bitforce_flash_led(bitforce);
  323. return true;
  324. }
  325. /* It is not critical getting temperature so don't get stuck if we
  326. * can't grab the mutex here */
  327. if (mutex_trylock(mutexp))
  328. return false;
  329. bitforce_cmd1(fdDev, bitforce->proc_id, pdevbuf, sizeof(pdevbuf), "ZLX");
  330. mutex_unlock(mutexp);
  331. if (unlikely(!pdevbuf[0])) {
  332. applog(LOG_ERR, "%"PRIpreprv": Error: Get temp returned empty string/timed out", bitforce->proc_repr);
  333. bitforce->hw_errors++;
  334. ++hw_errors;
  335. return false;
  336. }
  337. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  338. float temp = strtof(s + 1, NULL);
  339. /* Cope with older software that breaks and reads nonsense
  340. * values */
  341. if (temp > 100)
  342. temp = strtod(s + 1, NULL);
  343. if (temp > 0) {
  344. bitforce->temp = temp;
  345. }
  346. } else {
  347. /* Use the temperature monitor as a kind of watchdog for when
  348. * our responses are out of sync and flush the buffer to
  349. * hopefully recover */
  350. applog(LOG_WARNING, "%"PRIpreprv": Garbled response probably throttling, clearing buffer", bitforce->proc_repr);
  351. dev_error(bitforce, REASON_DEV_THROTTLE);
  352. /* Count throttling episodes as hardware errors */
  353. bitforce->hw_errors++;
  354. ++hw_errors;
  355. bitforce_clear_buffer(bitforce);
  356. return false;
  357. }
  358. return true;
  359. }
  360. static inline
  361. void dbg_block_data(struct cgpu_info *bitforce)
  362. {
  363. if (!opt_debug)
  364. return;
  365. struct bitforce_data *data = bitforce->cgpu_data;
  366. char *s;
  367. s = bin2hex(&data->next_work_ob[8], 44);
  368. applog(LOG_DEBUG, "%"PRIpreprv": block data: %s", bitforce->proc_repr, s);
  369. free(s);
  370. }
  371. static void bitforce_change_mode(struct cgpu_info *, enum bitforce_proto);
  372. static
  373. bool bitforce_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  374. {
  375. struct cgpu_info *bitforce = thr->cgpu;
  376. struct bitforce_data *data = bitforce->cgpu_data;
  377. int fdDev = bitforce->device->device_fd;
  378. unsigned char *ob_ms = &data->next_work_ob[8];
  379. unsigned char *ob_dt = &ob_ms[32];
  380. // If polling job_start, cancel it
  381. if (data->poll_func == 1)
  382. {
  383. thr->tv_poll.tv_sec = -1;
  384. data->poll_func = 0;
  385. }
  386. memcpy(ob_ms, work->midstate, 32);
  387. memcpy(ob_dt, work->data + 64, 12);
  388. switch (data->proto)
  389. {
  390. case BFP_RANGE:
  391. {
  392. uint32_t *ob_nonce = (uint32_t*)&(ob_dt[32]);
  393. ob_nonce[0] = htobe32(work->blk.nonce);
  394. ob_nonce[1] = htobe32(work->blk.nonce + bitforce->nonces);
  395. // FIXME: if nonce range fails... we didn't increment enough
  396. work->blk.nonce += bitforce->nonces + 1;
  397. break;
  398. }
  399. case BFP_QUEUE:
  400. if (thr->work)
  401. {
  402. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  403. char pdevbuf[0x100];
  404. if (unlikely(!fdDev))
  405. return false;
  406. mutex_lock(mutexp);
  407. if (data->queued)
  408. bitforce_cmd1(fdDev, bitforce->proc_id, pdevbuf, sizeof(pdevbuf), "ZQX");
  409. bitforce_cmd2(fdDev, bitforce->proc_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, data->next_work_obs, data->next_work_obsz);
  410. mutex_unlock(mutexp);
  411. if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  412. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  413. bitforce_change_mode(bitforce, BFP_WORK);
  414. }
  415. else
  416. {
  417. dbg_block_data(bitforce);
  418. data->queued = true;
  419. }
  420. }
  421. // fallthru...
  422. case BFP_WORK:
  423. work->blk.nonce = 0xffffffff;
  424. }
  425. return true;
  426. }
  427. static
  428. void bitforce_change_mode(struct cgpu_info *bitforce, enum bitforce_proto proto)
  429. {
  430. struct bitforce_data *data = bitforce->cgpu_data;
  431. if (data->proto == proto)
  432. return;
  433. if (data->proto == BFP_RANGE)
  434. {
  435. bitforce->nonces = 0xffffffff;
  436. bitforce->sleep_ms *= 5;
  437. data->sleep_ms_default *= 5;
  438. switch (proto)
  439. {
  440. case BFP_WORK:
  441. data->next_work_cmd = "ZDX";
  442. break;
  443. case BFP_QUEUE:
  444. data->next_work_cmd = "ZNX";
  445. default:
  446. ;
  447. }
  448. if (data->sc)
  449. {
  450. // "S|---------- MidState ----------||-DataTail-|E"
  451. data->next_work_ob[7] = 45;
  452. data->next_work_ob[8+32+12] = '\xAA';
  453. data->next_work_obsz = 46;
  454. }
  455. else
  456. {
  457. // ">>>>>>>>|---------- MidState ----------||-DataTail-|>>>>>>>>"
  458. memset(&data->next_work_ob[8+32+12], '>', 8);
  459. data->next_work_obsz = 60;
  460. }
  461. }
  462. else
  463. if (proto == BFP_RANGE)
  464. {
  465. /* Split work up into 1/5th nonce ranges */
  466. bitforce->nonces = 0x33333332;
  467. bitforce->sleep_ms /= 5;
  468. data->sleep_ms_default /= 5;
  469. data->next_work_cmd = "ZPX";
  470. if (data->sc)
  471. {
  472. data->next_work_ob[7] = 53;
  473. data->next_work_obsz = 54;
  474. }
  475. else
  476. data->next_work_obsz = 68;
  477. }
  478. data->proto = proto;
  479. bitforce->kname = protonames[proto];
  480. }
  481. static
  482. void bitforce_job_start(struct thr_info *thr)
  483. {
  484. struct cgpu_info *bitforce = thr->cgpu;
  485. struct bitforce_data *data = bitforce->cgpu_data;
  486. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  487. int fdDev = bitforce->device->device_fd;
  488. unsigned char *ob = data->next_work_obs;
  489. char pdevbuf[0x100];
  490. struct timeval tv_now;
  491. data->result_busy_polled = 0;
  492. if (data->queued)
  493. {
  494. // get_results collected more accurate job start time
  495. mt_job_transition(thr);
  496. job_start_complete(thr);
  497. data->queued = false;
  498. timer_set_delay(&thr->tv_morework, &bitforce->work_start_tv, bitforce->sleep_ms * 1000);
  499. return;
  500. }
  501. if (!fdDev)
  502. goto commerr;
  503. re_send:
  504. mutex_lock(mutexp);
  505. bitforce_cmd2(fdDev, bitforce->proc_id, pdevbuf, sizeof(pdevbuf), data->next_work_cmd, ob, data->next_work_obsz);
  506. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  507. mutex_unlock(mutexp);
  508. gettimeofday(&tv_now, NULL);
  509. timer_set_delay(&thr->tv_poll, &tv_now, WORK_CHECK_INTERVAL_MS * 1000);
  510. data->poll_func = 1;
  511. return;
  512. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  513. mutex_unlock(mutexp);
  514. switch (data->proto)
  515. {
  516. case BFP_RANGE:
  517. applog(LOG_WARNING, "%"PRIpreprv": Does not support nonce range, disabling", bitforce->proc_repr);
  518. bitforce_change_mode(bitforce, BFP_WORK);
  519. goto re_send;
  520. case BFP_QUEUE:
  521. applog(LOG_WARNING, "%"PRIpreprv": Does not support work queue, disabling", bitforce->proc_repr);
  522. bitforce_change_mode(bitforce, BFP_WORK);
  523. goto re_send;
  524. default:
  525. ;
  526. }
  527. applog(LOG_ERR, "%"PRIpreprv": Error: Send work reports: %s", bitforce->proc_repr, pdevbuf);
  528. goto commerr;
  529. }
  530. mt_job_transition(thr);
  531. mutex_unlock(mutexp);
  532. dbg_block_data(bitforce);
  533. gettimeofday(&tv_now, NULL);
  534. bitforce->work_start_tv = tv_now;
  535. timer_set_delay(&thr->tv_morework, &tv_now, bitforce->sleep_ms * 1000);
  536. job_start_complete(thr);
  537. return;
  538. commerr:
  539. bitforce_comm_error(thr);
  540. job_start_abort(thr, true);
  541. }
  542. static char _discardedbuf[0x10];
  543. static
  544. void bitforce_job_get_results(struct thr_info *thr, struct work *work)
  545. {
  546. struct cgpu_info *bitforce = thr->cgpu;
  547. struct bitforce_data *data = bitforce->cgpu_data;
  548. pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
  549. int fdDev = bitforce->device->device_fd;
  550. unsigned int delay_time_ms;
  551. struct timeval elapsed;
  552. struct timeval now;
  553. char *pdevbuf = &data->noncebuf[0];
  554. bool stale;
  555. gettimeofday(&now, NULL);
  556. timersub(&now, &bitforce->work_start_tv, &elapsed);
  557. bitforce->wait_ms = tv_to_ms(elapsed);
  558. bitforce->polling = true;
  559. if (!fdDev)
  560. goto commerr;
  561. stale = stale_work(work, true);
  562. if (unlikely(bitforce->wait_ms < bitforce->sleep_ms))
  563. {
  564. // We're likely here because of a work restart
  565. // Since Bitforce cannot stop a work without losing results, only do it if the current job is finding stale shares
  566. // BFP_QUEUE does not support stopping work at all
  567. if (data->proto == BFP_QUEUE || !stale)
  568. {
  569. delay_time_ms = bitforce->sleep_ms - bitforce->wait_ms;
  570. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  571. data->poll_func = 2;
  572. return;
  573. }
  574. }
  575. while (1) {
  576. const char *cmd = (data->proto == BFP_QUEUE) ? "ZOX" : "ZFX";
  577. int count;
  578. mutex_lock(mutexp);
  579. bitforce_cmd1(fdDev, bitforce->proc_id, pdevbuf, sizeof(data->noncebuf), cmd);
  580. if (!strncasecmp(pdevbuf, "COUNT:", 6))
  581. {
  582. count = atoi(&pdevbuf[6]);
  583. size_t cls = strlen(pdevbuf);
  584. char *pmorebuf = &pdevbuf[cls];
  585. size_t szleft = sizeof(data->noncebuf) - cls, sz;
  586. if (count && data->queued)
  587. {
  588. gettimeofday(&now, NULL);
  589. bitforce->work_start_tv = now;
  590. }
  591. while (true)
  592. {
  593. BFgets(pmorebuf, szleft, fdDev);
  594. if (!strncasecmp(pmorebuf, "OK", 2))
  595. break;
  596. sz = strlen(pmorebuf);
  597. szleft -= sz;
  598. pmorebuf += sz;
  599. if (unlikely(!szleft))
  600. {
  601. // Out of buffer space somehow :(
  602. applog(LOG_DEBUG, "%"PRIpreprv": Ran out of buffer space for results, discarding extra data", bitforce->proc_repr);
  603. pmorebuf = _discardedbuf;
  604. szleft = sizeof(_discardedbuf);
  605. }
  606. }
  607. }
  608. else
  609. count = -1;
  610. mutex_unlock(mutexp);
  611. gettimeofday(&now, NULL);
  612. if (!count)
  613. goto noqr;
  614. timersub(&now, &bitforce->work_start_tv, &elapsed);
  615. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  616. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  617. tv_to_ms(elapsed), (unsigned long)BITFORCE_LONG_TIMEOUT_MS);
  618. goto out;
  619. }
  620. if (count > 0)
  621. {
  622. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  623. goto out;
  624. }
  625. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  626. break;
  627. data->result_busy_polled = bitforce->wait_ms;
  628. if (stale && data->proto != BFP_QUEUE)
  629. {
  630. applog(LOG_NOTICE, "%"PRIpreprv": Abandoning stale search to restart",
  631. bitforce->proc_repr);
  632. goto out;
  633. }
  634. noqr:
  635. /* if BFL is throttling, no point checking so quickly */
  636. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  637. timer_set_delay(&thr->tv_poll, &now, delay_time_ms * 1000);
  638. data->poll_func = 2;
  639. return;
  640. }
  641. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  642. applog(LOG_ERR, "%"PRIpreprv": took %lums - longer than %lums", bitforce->proc_repr,
  643. tv_to_ms(elapsed), (unsigned long)BITFORCE_TIMEOUT_MS);
  644. dev_error(bitforce, REASON_DEV_OVER_HEAT);
  645. ++bitforce->hw_errors;
  646. ++hw_errors;
  647. /* If the device truly throttled, it didn't process the job and there
  648. * are no results. But check first, just in case we're wrong about it
  649. * throttling.
  650. */
  651. if (strncasecmp(pdevbuf, "NONCE-FOUND", 11))
  652. goto out;
  653. } else if (!strncasecmp(pdevbuf, "N", 1)) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  654. /* Simple timing adjustment. Allow a few polls to cope with
  655. * OS timer delays being variably reliable. wait_ms will
  656. * always equal sleep_ms when we've waited greater than or
  657. * equal to the result return time.*/
  658. delay_time_ms = bitforce->sleep_ms;
  659. if (!data->result_busy_polled)
  660. {
  661. // No busy polls before results received
  662. if (bitforce->wait_ms > delay_time_ms + (WORK_CHECK_INTERVAL_MS * 8))
  663. // ... due to poll being rather late; ignore it as an anomaly
  664. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, later than scheduled %ums (ignoring)",
  665. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms);
  666. else
  667. if (bitforce->sleep_ms > data->sleep_ms_default + (BITFORCE_CHECK_INTERVAL_MS * 0x20))
  668. {
  669. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on delayed schedule %ums; Wait time changed to: %ums (default sch)",
  670. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, data->sleep_ms_default);
  671. bitforce->sleep_ms = data->sleep_ms_default;
  672. }
  673. else
  674. {
  675. applog(LOG_DEBUG, "%"PRIpreprv": Got results on first poll after %ums, on default schedule %ums; Wait time changed to: %ums (check interval)",
  676. bitforce->proc_repr, bitforce->wait_ms, delay_time_ms, BITFORCE_CHECK_INTERVAL_MS);
  677. bitforce->sleep_ms = BITFORCE_CHECK_INTERVAL_MS;
  678. }
  679. }
  680. else
  681. {
  682. if (data->result_busy_polled - bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  683. {
  684. bitforce->sleep_ms = data->result_busy_polled - (WORK_CHECK_INTERVAL_MS / 2);
  685. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time changed to: %ums",
  686. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms, bitforce->sleep_ms);
  687. }
  688. else
  689. applog(LOG_DEBUG, "%"PRIpreprv": Got results on Nth poll after %ums (busy poll at %ums, sch'd %ums); Wait time unchanged",
  690. bitforce->proc_repr, bitforce->wait_ms, data->result_busy_polled, delay_time_ms);
  691. }
  692. /* Work out the average time taken. Float for calculation, uint for display */
  693. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  694. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  695. }
  696. applog(LOG_DEBUG, "%"PRIpreprv": waited %dms until %s", bitforce->proc_repr, bitforce->wait_ms, pdevbuf);
  697. if (strncasecmp(pdevbuf, "NONCE-FOUND", 11) && (pdevbuf[2] != '-') && strncasecmp(pdevbuf, "I", 1)) {
  698. bitforce->hw_errors++;
  699. ++hw_errors;
  700. applog(LOG_WARNING, "%"PRIpreprv": Error: Get result reports: %s", bitforce->proc_repr, pdevbuf);
  701. bitforce_clear_buffer(bitforce);
  702. }
  703. out:
  704. bitforce->polling = false;
  705. job_results_fetched(thr);
  706. return;
  707. commerr:
  708. bitforce_comm_error(thr);
  709. goto out;
  710. }
  711. static
  712. void bitforce_process_result_nonces(struct thr_info *thr, struct work *work, char *pnoncebuf)
  713. {
  714. struct cgpu_info *bitforce = thr->cgpu;
  715. struct bitforce_data *data = bitforce->cgpu_data;
  716. uint32_t nonce;
  717. while (1) {
  718. hex2bin((void*)&nonce, pnoncebuf, 4);
  719. nonce = be32toh(nonce);
  720. if (unlikely(data->proto == BFP_RANGE && (nonce >= work->blk.nonce ||
  721. /* FIXME: blk.nonce is probably moved on quite a bit now! */
  722. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  723. applog(LOG_WARNING, "%"PRIpreprv": Disabling broken nonce range support", bitforce->proc_repr);
  724. bitforce_change_mode(bitforce, BFP_WORK);
  725. }
  726. submit_nonce(thr, work, nonce);
  727. if (strncmp(&pnoncebuf[8], ",", 1))
  728. break;
  729. pnoncebuf += 9;
  730. }
  731. }
  732. static
  733. bool bitforce_process_qresult_line_i(struct thr_info *thr, char *midstate, char *datatail, char *buf, struct work *work)
  734. {
  735. if (!work)
  736. return false;
  737. if (memcmp(work->midstate, midstate, 32))
  738. return false;
  739. if (memcmp(&work->data[64], datatail, 12))
  740. return false;
  741. if (!atoi(&buf[90]))
  742. return true;
  743. bitforce_process_result_nonces(thr, work, &buf[92]);
  744. return true;
  745. }
  746. static
  747. void bitforce_process_qresult_line(struct thr_info *thr, char *buf, struct work *work)
  748. {
  749. struct cgpu_info *bitforce = thr->cgpu;
  750. char midstate[32], datatail[12];
  751. hex2bin((void*)midstate, buf, 32);
  752. hex2bin((void*)datatail, &buf[65], 12);
  753. if (!( bitforce_process_qresult_line_i(thr, midstate, datatail, buf, work)
  754. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->work)
  755. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->prev_work)
  756. || bitforce_process_qresult_line_i(thr, midstate, datatail, buf, thr->next_work) ))
  757. {
  758. applog(LOG_ERR, "%"PRIpreprv": Failed to find work for queued results", bitforce->proc_repr);
  759. ++bitforce->hw_errors;
  760. ++hw_errors;
  761. }
  762. }
  763. static inline
  764. char *next_line(char *in)
  765. {
  766. while (in[0] && in[0] != '\n')
  767. ++in;
  768. return in;
  769. }
  770. static
  771. int64_t bitforce_job_process_results(struct thr_info *thr, struct work *work, __maybe_unused bool stopping)
  772. {
  773. struct cgpu_info *bitforce = thr->cgpu;
  774. struct bitforce_data *data = bitforce->cgpu_data;
  775. char *pnoncebuf = &data->noncebuf[0];
  776. int count;
  777. if (!strncasecmp(pnoncebuf, "NO-", 3))
  778. return bitforce->nonces; /* No valid nonce found */
  779. if (!strncasecmp(pnoncebuf, "NONCE-FOUND", 11))
  780. {
  781. bitforce_process_result_nonces(thr, work, &pnoncebuf[12]);
  782. count = 1;
  783. }
  784. else
  785. if (!strncasecmp(pnoncebuf, "COUNT:", 6))
  786. {
  787. count = 0;
  788. pnoncebuf = next_line(pnoncebuf);
  789. while (pnoncebuf[0])
  790. {
  791. bitforce_process_qresult_line(thr, pnoncebuf, work);
  792. ++count;
  793. pnoncebuf = next_line(pnoncebuf);
  794. }
  795. }
  796. else
  797. return 0;
  798. // FIXME: This might have changed in the meantime (new job start, or broken)
  799. return bitforce->nonces * count;
  800. }
  801. static void bitforce_shutdown(struct thr_info *thr)
  802. {
  803. struct cgpu_info *bitforce = thr->cgpu;
  804. int *p_fdDev = &bitforce->device->device_fd;
  805. BFclose(*p_fdDev);
  806. *p_fdDev = 0;
  807. }
  808. static void biforce_thread_enable(struct thr_info *thr)
  809. {
  810. struct cgpu_info *bitforce = thr->cgpu;
  811. bitforce_init(bitforce);
  812. }
  813. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  814. {
  815. return bitforce_get_temp(bitforce);
  816. }
  817. static bool bitforce_identify(struct cgpu_info *bitforce)
  818. {
  819. bitforce->flash_led = true;
  820. return true;
  821. }
  822. static bool bitforce_thread_init(struct thr_info *thr)
  823. {
  824. struct cgpu_info *bitforce = thr->cgpu;
  825. unsigned int wait;
  826. struct bitforce_data *data;
  827. bool sc = (bool)bitforce->cgpu_data;
  828. for ( ; bitforce; bitforce = bitforce->next_proc)
  829. {
  830. bitforce->cgpu_data = data = malloc(sizeof(*data));
  831. *data = (struct bitforce_data){
  832. .next_work_ob = ">>>>>>>>|---------- MidState ----------||-DataTail-||Nonces|>>>>>>>>",
  833. .proto = BFP_RANGE,
  834. .sc = sc,
  835. .sleep_ms_default = BITFORCE_SLEEP_MS,
  836. };
  837. if (sc)
  838. {
  839. // ".......S|---------- MidState ----------||-DataTail-||Nonces|E"
  840. data->next_work_ob[8+32+12+8] = '\xAA';
  841. data->next_work_obs = &data->next_work_ob[7];
  842. }
  843. else
  844. data->next_work_obs = &data->next_work_ob[0];
  845. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  846. bitforce_change_mode(bitforce, BFP_WORK);
  847. /* Initially enable support for nonce range and disable it later if it
  848. * fails */
  849. if (opt_bfl_noncerange)
  850. bitforce_change_mode(bitforce, BFP_RANGE);
  851. }
  852. bitforce = thr->cgpu;
  853. /* Pause each new thread at least 100ms between initialising
  854. * so the devices aren't making calls all at the same time. */
  855. wait = thr->id * MAX_START_DELAY_MS;
  856. applog(LOG_DEBUG, "%s: Delaying start by %dms", bitforce->dev_repr, wait / 1000);
  857. nmsleep(wait);
  858. return true;
  859. }
  860. static struct api_data *bitforce_api_stats(struct cgpu_info *cgpu)
  861. {
  862. struct api_data *root = NULL;
  863. // Warning, access to these is not locked - but we don't really
  864. // care since hashing performance is way more important than
  865. // locking access to displaying API debug 'stats'
  866. // If locking becomes an issue for any of them, use copy_data=true also
  867. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  868. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  869. return root;
  870. }
  871. void bitforce_poll(struct thr_info *thr)
  872. {
  873. struct cgpu_info *bitforce = thr->cgpu;
  874. struct bitforce_data *data = bitforce->cgpu_data;
  875. int poll = data->poll_func;
  876. thr->tv_poll.tv_sec = -1;
  877. data->poll_func = 0;
  878. switch (poll)
  879. {
  880. case 1:
  881. bitforce_job_start(thr);
  882. break;
  883. case 2:
  884. bitforce_job_get_results(thr, thr->work);
  885. break;
  886. default:
  887. applog(LOG_ERR, "%"PRIpreprv": Unexpected poll from device API!", thr->cgpu->proc_repr);
  888. }
  889. }
  890. struct device_api bitforce_api = {
  891. .dname = "bitforce",
  892. .name = "BFL",
  893. .api_detect = bitforce_detect,
  894. .get_api_stats = bitforce_api_stats,
  895. .minerloop = minerloop_async,
  896. .reinit_device = bitforce_init,
  897. .get_statline_before = get_bitforce_statline_before,
  898. .get_stats = bitforce_get_stats,
  899. .identify_device = bitforce_identify,
  900. .thread_prepare = bitforce_thread_prepare,
  901. .thread_init = bitforce_thread_init,
  902. .job_prepare = bitforce_job_prepare,
  903. .job_start = bitforce_job_start,
  904. .job_get_results = bitforce_job_get_results,
  905. .poll = bitforce_poll,
  906. .job_process_results = bitforce_job_process_results,
  907. .thread_shutdown = bitforce_shutdown,
  908. .thread_enable = biforce_thread_enable
  909. };