driver-bitforce.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * Copyright 2012 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 <limits.h>
  11. #include <pthread.h>
  12. #include <stdio.h>
  13. #include <strings.h>
  14. #include <sys/time.h>
  15. #include <unistd.h>
  16. #include "config.h"
  17. #include "compat.h"
  18. #include "fpgautils.h"
  19. #include "miner.h"
  20. #define BITFORCE_SLEEP_MS 500
  21. #define BITFORCE_TIMEOUT_S 7
  22. #define BITFORCE_TIMEOUT_MS (BITFORCE_TIMEOUT_S * 1000)
  23. #define BITFORCE_LONG_TIMEOUT_S 15
  24. #define BITFORCE_LONG_TIMEOUT_MS (BITFORCE_LONG_TIMEOUT_S * 1000)
  25. #define BITFORCE_CHECK_INTERVAL_MS 10
  26. #define WORK_CHECK_INTERVAL_MS 50
  27. #define MAX_START_DELAY_US 100000
  28. #define tv_to_ms(tval) (tval.tv_sec * 1000 + tval.tv_usec / 1000)
  29. #define TIME_AVG_CONSTANT 8
  30. #define KNAME_WORK "full work"
  31. #define KNAME_RANGE "nonce range"
  32. struct device_api bitforce_api;
  33. // Code must deal with a timeout
  34. #define BFopen(devpath) serial_open(devpath, 0, 1, true)
  35. static void BFgets(char *buf, size_t bufLen, int fd)
  36. {
  37. do {
  38. buf[0] = '\0';
  39. --bufLen;
  40. } while (likely(bufLen && read(fd, buf, 1) == 1 && (buf++)[0] != '\n'));
  41. buf[0] = '\0';
  42. }
  43. static ssize_t BFwrite(int fd, const void *buf, ssize_t bufLen)
  44. {
  45. if ((bufLen) != write(fd, buf, bufLen))
  46. return 0;
  47. else
  48. return bufLen;
  49. }
  50. #define BFclose(fd) close(fd)
  51. static bool bitforce_detect_one(const char *devpath)
  52. {
  53. int fdDev = BFopen(devpath);
  54. struct cgpu_info *bitforce;
  55. char pdevbuf[0x100];
  56. char *s;
  57. applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
  58. if (unlikely(fdDev == -1)) {
  59. applog(LOG_ERR, "BFL: Failed to open %s", devpath);
  60. return false;
  61. }
  62. BFwrite(fdDev, "ZGX", 3);
  63. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  64. if (unlikely(!pdevbuf[0])) {
  65. applog(LOG_ERR, "BFL: Error reading/timeout (ZGX)");
  66. return 0;
  67. }
  68. BFclose(fdDev);
  69. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  70. applog(LOG_ERR, "BFL: Didn't recognise BitForce on %s", devpath);
  71. return false;
  72. }
  73. // We have a real BitForce!
  74. bitforce = calloc(1, sizeof(*bitforce));
  75. bitforce->api = &bitforce_api;
  76. bitforce->device_path = strdup(devpath);
  77. bitforce->deven = DEV_ENABLED;
  78. bitforce->threads = 1;
  79. /* Initially enable support for nonce range and disable it later if it
  80. * fails */
  81. if (opt_bfl_noncerange) {
  82. bitforce->nonce_range = true;
  83. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  84. bitforce->kname = KNAME_RANGE;
  85. } else {
  86. bitforce->sleep_ms = BITFORCE_SLEEP_MS * 5;
  87. bitforce->kname = KNAME_WORK;
  88. }
  89. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  90. s[0] = '\0';
  91. bitforce->name = strdup(pdevbuf + 7);
  92. }
  93. mutex_init(&bitforce->device_mutex);
  94. return add_cgpu(bitforce);
  95. }
  96. static char bitforce_detect_auto()
  97. {
  98. return (serial_autodetect_udev (bitforce_detect_one, "BitFORCE*SHA256") ?:
  99. serial_autodetect_devserial(bitforce_detect_one, "BitFORCE_SHA256") ?:
  100. 0);
  101. }
  102. static void bitforce_detect()
  103. {
  104. serial_detect_auto(bitforce_api.dname, bitforce_detect_one, bitforce_detect_auto);
  105. }
  106. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  107. {
  108. float gt = bitforce->temp;
  109. if (gt > 0)
  110. tailsprintf(buf, "%5.1fC ", gt);
  111. else
  112. tailsprintf(buf, " ", gt);
  113. tailsprintf(buf, " | ");
  114. }
  115. static bool bitforce_thread_prepare(struct thr_info *thr)
  116. {
  117. struct cgpu_info *bitforce = thr->cgpu;
  118. int fdDev = BFopen(bitforce->device_path);
  119. struct timeval now;
  120. if (unlikely(fdDev == -1)) {
  121. applog(LOG_ERR, "BFL%i: Failed to open %s", bitforce->device_id, bitforce->device_path);
  122. return false;
  123. }
  124. bitforce->device_fd = fdDev;
  125. applog(LOG_INFO, "BFL%i: Opened %s", bitforce->device_id, bitforce->device_path);
  126. gettimeofday(&now, NULL);
  127. get_datestamp(bitforce->init, &now);
  128. return true;
  129. }
  130. static void biforce_clear_buffer(struct cgpu_info *bitforce)
  131. {
  132. int fdDev = bitforce->device_fd;
  133. char pdevbuf[0x100];
  134. int count = 0;
  135. if (!fdDev)
  136. return;
  137. applog(LOG_DEBUG, "BFL%i: Clearing read buffer", bitforce->device_id);
  138. mutex_lock(&bitforce->device_mutex);
  139. do {
  140. pdevbuf[0] = '\0';
  141. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  142. } while (pdevbuf[0] && (++count < 10));
  143. mutex_unlock(&bitforce->device_mutex);
  144. }
  145. void bitforce_init(struct cgpu_info *bitforce)
  146. {
  147. char *devpath = bitforce->device_path;
  148. int fdDev = bitforce->device_fd, retries = 0;
  149. char pdevbuf[0x100];
  150. char *s;
  151. applog(LOG_WARNING, "BFL%i: Re-initialising", bitforce->device_id);
  152. biforce_clear_buffer(bitforce);
  153. mutex_lock(&bitforce->device_mutex);
  154. if (fdDev) {
  155. BFclose(fdDev);
  156. sleep(5);
  157. }
  158. bitforce->device_fd = 0;
  159. fdDev = BFopen(devpath);
  160. if (unlikely(fdDev == -1)) {
  161. mutex_unlock(&bitforce->device_mutex);
  162. applog(LOG_ERR, "BFL%i: Failed to open %s", bitforce->device_id, devpath);
  163. return;
  164. }
  165. do {
  166. BFwrite(fdDev, "ZGX", 3);
  167. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  168. if (unlikely(!pdevbuf[0])) {
  169. mutex_unlock(&bitforce->device_mutex);
  170. applog(LOG_ERR, "BFL%i: Error reading/timeout (ZGX)", bitforce->device_id);
  171. return;
  172. }
  173. if (retries++)
  174. nmsleep(10);
  175. } while (!strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  176. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  177. mutex_unlock(&bitforce->device_mutex);
  178. applog(LOG_ERR, "BFL%i: Didn't recognise BitForce on %s returned: %s", bitforce->device_id, devpath, pdevbuf);
  179. return;
  180. }
  181. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  182. s[0] = '\0';
  183. bitforce->name = strdup(pdevbuf + 7);
  184. }
  185. bitforce->device_fd = fdDev;
  186. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  187. mutex_unlock(&bitforce->device_mutex);
  188. }
  189. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  190. {
  191. int fdDev = bitforce->device_fd;
  192. char pdevbuf[0x100];
  193. char *s;
  194. if (!fdDev)
  195. return false;
  196. mutex_lock(&bitforce->device_mutex);
  197. BFwrite(fdDev, "ZLX", 3);
  198. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  199. mutex_unlock(&bitforce->device_mutex);
  200. if (unlikely(!pdevbuf[0])) {
  201. applog(LOG_ERR, "BFL%i: Error: Get temp returned empty string/timed out", bitforce->device_id);
  202. bitforce->temp = 0;
  203. return false;
  204. }
  205. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  206. float temp = strtof(s + 1, NULL);
  207. if (temp > 0) {
  208. bitforce->temp = temp;
  209. if (temp > bitforce->cutofftemp) {
  210. applog(LOG_WARNING, "BFL%i: Hit thermal cutoff limit, disabling!", bitforce->device_id);
  211. bitforce->deven = DEV_RECOVER;
  212. bitforce->device_last_not_well = time(NULL);
  213. bitforce->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  214. bitforce->dev_thermal_cutoff_count++;
  215. }
  216. }
  217. }
  218. return true;
  219. }
  220. static bool bitforce_send_work(struct thr_info *thr, struct work *work)
  221. {
  222. struct cgpu_info *bitforce = thr->cgpu;
  223. int fdDev = bitforce->device_fd;
  224. unsigned char ob[70];
  225. char pdevbuf[0x100];
  226. char *s;
  227. if (!fdDev)
  228. return false;
  229. re_send:
  230. mutex_lock(&bitforce->device_mutex);
  231. if (bitforce->nonce_range)
  232. BFwrite(fdDev, "ZPX", 3);
  233. else
  234. BFwrite(fdDev, "ZDX", 3);
  235. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  236. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  237. mutex_unlock(&bitforce->device_mutex);
  238. nmsleep(WORK_CHECK_INTERVAL_MS);
  239. goto re_send;
  240. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  241. mutex_unlock(&bitforce->device_mutex);
  242. if (bitforce->nonce_range) {
  243. applog(LOG_WARNING, "BFL%i: Does not support nonce range, disabling", bitforce->device_id);
  244. bitforce->nonce_range = false;
  245. bitforce->sleep_ms *= 5;
  246. bitforce->kname = KNAME_WORK;
  247. goto re_send;
  248. }
  249. applog(LOG_ERR, "BFL%i: Error: Send work reports: %s", bitforce->device_id, pdevbuf);
  250. return false;
  251. }
  252. sprintf((char *)ob, ">>>>>>>>");
  253. memcpy(ob + 8, work->midstate, 32);
  254. memcpy(ob + 8 + 32, work->data + 64, 12);
  255. if (!bitforce->nonce_range) {
  256. sprintf((char *)ob + 8 + 32 + 12, ">>>>>>>>");
  257. work->blk.nonce = bitforce->nonces = 0xffffffff;
  258. BFwrite(fdDev, ob, 60);
  259. } else {
  260. uint32_t *nonce;
  261. nonce = (uint32_t *)(ob + 8 + 32 + 12);
  262. *nonce = htobe32(work->blk.nonce);
  263. nonce = (uint32_t *)(ob + 8 + 32 + 12 + 4);
  264. /* Split work up into 1/5th nonce ranges */
  265. bitforce->nonces = 0x33333332;
  266. *nonce = htobe32(work->blk.nonce + bitforce->nonces);
  267. work->blk.nonce += bitforce->nonces + 1;
  268. sprintf((char *)ob + 8 + 32 + 12 + 8, ">>>>>>>>");
  269. BFwrite(fdDev, ob, 68);
  270. }
  271. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  272. mutex_unlock(&bitforce->device_mutex);
  273. if (opt_debug) {
  274. s = bin2hex(ob + 8, 44);
  275. applog(LOG_DEBUG, "BFL%i: block data: %s", bitforce->device_id, s);
  276. free(s);
  277. }
  278. if (unlikely(!pdevbuf[0])) {
  279. applog(LOG_ERR, "BFL%i: Error: Send block data returned empty string/timed out", bitforce->device_id);
  280. return false;
  281. }
  282. if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  283. applog(LOG_ERR, "BFL%i: Error: Send block data reports: %s", bitforce->device_id, pdevbuf);
  284. return false;
  285. }
  286. gettimeofday(&bitforce->work_start_tv, NULL);
  287. return true;
  288. }
  289. static int64_t bitforce_get_result(struct thr_info *thr, struct work *work)
  290. {
  291. struct cgpu_info *bitforce = thr->cgpu;
  292. int fdDev = bitforce->device_fd;
  293. unsigned int delay_time_ms;
  294. struct timeval elapsed;
  295. struct timeval now;
  296. char pdevbuf[0x100];
  297. char *pnoncebuf;
  298. uint32_t nonce;
  299. if (!fdDev)
  300. return -1;
  301. while (1) {
  302. if (unlikely(thr->work_restart))
  303. return 0;
  304. mutex_lock(&bitforce->device_mutex);
  305. BFwrite(fdDev, "ZFX", 3);
  306. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  307. mutex_unlock(&bitforce->device_mutex);
  308. gettimeofday(&now, NULL);
  309. timersub(&now, &bitforce->work_start_tv, &elapsed);
  310. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  311. applog(LOG_ERR, "BFL%i: took %dms - longer than %dms", bitforce->device_id,
  312. tv_to_ms(elapsed), BITFORCE_LONG_TIMEOUT_MS);
  313. return 0;
  314. }
  315. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  316. break;
  317. /* if BFL is throttling, no point checking so quickly */
  318. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  319. nmsleep(delay_time_ms);
  320. bitforce->wait_ms += delay_time_ms;
  321. }
  322. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  323. applog(LOG_ERR, "BFL%i: took %dms - longer than %dms", bitforce->device_id,
  324. tv_to_ms(elapsed), BITFORCE_TIMEOUT_MS);
  325. bitforce->device_last_not_well = time(NULL);
  326. bitforce->device_not_well_reason = REASON_DEV_OVER_HEAT;
  327. bitforce->dev_over_heat_count++;
  328. if (!pdevbuf[0]) /* Only return if we got nothing after timeout - there still may be results */
  329. return 0;
  330. } else if (!strncasecmp(pdevbuf, "N", 1)) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  331. /* Simple timing adjustment. Allow a few polls to cope with
  332. * OS timer delays being variably reliable. wait_ms will
  333. * always equal sleep_ms when we've waited greater than or
  334. * equal to the result return time.*/
  335. delay_time_ms = bitforce->sleep_ms;
  336. if (bitforce->wait_ms > bitforce->sleep_ms + (WORK_CHECK_INTERVAL_MS * 2))
  337. bitforce->sleep_ms += (bitforce->wait_ms - bitforce->sleep_ms) / 2;
  338. else if (bitforce->wait_ms == bitforce->sleep_ms) {
  339. if (bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  340. bitforce->sleep_ms -= WORK_CHECK_INTERVAL_MS;
  341. else if (bitforce->sleep_ms > BITFORCE_CHECK_INTERVAL_MS)
  342. bitforce->sleep_ms -= BITFORCE_CHECK_INTERVAL_MS;
  343. }
  344. if (delay_time_ms != bitforce->sleep_ms)
  345. applog(LOG_DEBUG, "BFL%i: Wait time changed to: %d, waited %u", bitforce->device_id, bitforce->sleep_ms, bitforce->wait_ms);
  346. /* Work out the average time taken. Float for calculation, uint for display */
  347. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  348. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  349. }
  350. applog(LOG_DEBUG, "BFL%i: waited %dms until %s", bitforce->device_id, bitforce->wait_ms, pdevbuf);
  351. if (!strncasecmp(&pdevbuf[2], "-", 1))
  352. return bitforce->nonces; /* No valid nonce found */
  353. else if (!strncasecmp(pdevbuf, "I", 1))
  354. return 0; /* Device idle */
  355. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  356. applog(LOG_WARNING, "BFL%i: Error: Get result reports: %s", bitforce->device_id, pdevbuf);
  357. return 0;
  358. }
  359. pnoncebuf = &pdevbuf[12];
  360. while (1) {
  361. hex2bin((void*)&nonce, pnoncebuf, 4);
  362. #ifndef __BIG_ENDIAN__
  363. nonce = swab32(nonce);
  364. #endif
  365. if (unlikely(bitforce->nonce_range && (nonce >= work->blk.nonce ||
  366. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  367. applog(LOG_WARNING, "BFL%i: Disabling broken nonce range support", bitforce->device_id);
  368. bitforce->nonce_range = false;
  369. work->blk.nonce = 0xffffffff;
  370. bitforce->sleep_ms *= 5;
  371. bitforce->kname = KNAME_WORK;
  372. }
  373. submit_nonce(thr, work, nonce);
  374. if (strncmp(&pnoncebuf[8], ",", 1))
  375. break;
  376. pnoncebuf += 9;
  377. }
  378. return bitforce->nonces;
  379. }
  380. static void bitforce_shutdown(struct thr_info *thr)
  381. {
  382. struct cgpu_info *bitforce = thr->cgpu;
  383. BFclose(bitforce->device_fd);
  384. bitforce->device_fd = 0;
  385. }
  386. static void biforce_thread_enable(struct thr_info *thr)
  387. {
  388. struct cgpu_info *bitforce = thr->cgpu;
  389. bitforce_init(bitforce);
  390. }
  391. static int64_t bitforce_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  392. {
  393. struct cgpu_info *bitforce = thr->cgpu;
  394. unsigned int sleep_time;
  395. bool send_ret;
  396. int64_t ret;
  397. send_ret = bitforce_send_work(thr, work);
  398. if (!bitforce->nonce_range) {
  399. /* Initially wait 2/3 of the average cycle time so we can request more
  400. work before full scan is up */
  401. sleep_time = (2 * bitforce->sleep_ms) / 3;
  402. if (!restart_wait(sleep_time))
  403. return 0;
  404. bitforce->wait_ms = sleep_time;
  405. queue_request(thr, false);
  406. /* Now wait athe final 1/3rd; no bitforce should be finished by now */
  407. sleep_time = bitforce->sleep_ms - sleep_time;
  408. if (!restart_wait(sleep_time))
  409. return 0;
  410. bitforce->wait_ms += sleep_time;
  411. } else {
  412. sleep_time = bitforce->sleep_ms;
  413. if (!restart_wait(sleep_time))
  414. return 0;
  415. bitforce->wait_ms = sleep_time;
  416. }
  417. if (send_ret)
  418. ret = bitforce_get_result(thr, work);
  419. else
  420. ret = -1;
  421. if (ret == -1) {
  422. ret = 0;
  423. applog(LOG_ERR, "BFL%i: Comms error", bitforce->device_id);
  424. bitforce->device_last_not_well = time(NULL);
  425. bitforce->device_not_well_reason = REASON_DEV_COMMS_ERROR;
  426. bitforce->dev_comms_error_count++;
  427. /* empty read buffer */
  428. biforce_clear_buffer(bitforce);
  429. }
  430. return ret;
  431. }
  432. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  433. {
  434. return bitforce_get_temp(bitforce);
  435. }
  436. static bool bitforce_thread_init(struct thr_info *thr)
  437. {
  438. struct cgpu_info *bitforce = thr->cgpu;
  439. unsigned int wait;
  440. /* Pause each new thread at least 100ms between initialising
  441. * so the devices aren't making calls all at the same time. */
  442. wait = thr->id * MAX_START_DELAY_US;
  443. applog(LOG_DEBUG, "BFL%i: Delaying start by %dms", bitforce->device_id, wait / 1000);
  444. usleep(wait);
  445. return true;
  446. }
  447. static struct api_data *bitforce_api_stats(struct cgpu_info *cgpu)
  448. {
  449. struct api_data *root = NULL;
  450. // Warning, access to these is not locked - but we don't really
  451. // care since hashing performance is way more important than
  452. // locking access to displaying API debug 'stats'
  453. // If locking becomes an issue for any of them, use copy_data=true also
  454. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  455. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  456. return root;
  457. }
  458. struct device_api bitforce_api = {
  459. .dname = "bitforce",
  460. .name = "BFL",
  461. .api_detect = bitforce_detect,
  462. .get_api_stats = bitforce_api_stats,
  463. .reinit_device = bitforce_init,
  464. .get_statline_before = get_bitforce_statline_before,
  465. .get_stats = bitforce_get_stats,
  466. .thread_prepare = bitforce_thread_prepare,
  467. .thread_init = bitforce_thread_init,
  468. .scanhash = bitforce_scanhash,
  469. .thread_shutdown = bitforce_shutdown,
  470. .thread_enable = biforce_thread_enable
  471. };