driver-bitforce.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. mutex_lock(&bitforce->device_mutex);
  153. if (fdDev) {
  154. BFclose(fdDev);
  155. sleep(5);
  156. }
  157. bitforce->device_fd = 0;
  158. fdDev = BFopen(devpath);
  159. if (unlikely(fdDev == -1)) {
  160. mutex_unlock(&bitforce->device_mutex);
  161. applog(LOG_ERR, "BFL%i: Failed to open %s", bitforce->device_id, devpath);
  162. return;
  163. }
  164. do {
  165. BFwrite(fdDev, "ZGX", 3);
  166. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  167. if (unlikely(!pdevbuf[0])) {
  168. mutex_unlock(&bitforce->device_mutex);
  169. applog(LOG_ERR, "BFL%i: Error reading/timeout (ZGX)", bitforce->device_id);
  170. return;
  171. }
  172. if (retries++)
  173. nmsleep(10);
  174. } while (!strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
  175. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  176. mutex_unlock(&bitforce->device_mutex);
  177. applog(LOG_ERR, "BFL%i: Didn't recognise BitForce on %s returned: %s", bitforce->device_id, devpath, pdevbuf);
  178. return;
  179. }
  180. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>")))) {
  181. s[0] = '\0';
  182. bitforce->name = strdup(pdevbuf + 7);
  183. }
  184. bitforce->device_fd = fdDev;
  185. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  186. mutex_unlock(&bitforce->device_mutex);
  187. }
  188. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  189. {
  190. int fdDev = bitforce->device_fd;
  191. char pdevbuf[0x100];
  192. char *s;
  193. if (!fdDev)
  194. return false;
  195. mutex_lock(&bitforce->device_mutex);
  196. BFwrite(fdDev, "ZLX", 3);
  197. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  198. mutex_unlock(&bitforce->device_mutex);
  199. if (unlikely(!pdevbuf[0])) {
  200. applog(LOG_ERR, "BFL%i: Error: Get temp returned empty string/timed out", bitforce->device_id);
  201. bitforce->temp = 0;
  202. return false;
  203. }
  204. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  205. float temp = strtof(s + 1, NULL);
  206. if (temp > 0) {
  207. bitforce->temp = temp;
  208. if (temp > bitforce->cutofftemp) {
  209. applog(LOG_WARNING, "BFL%i: Hit thermal cutoff limit, disabling!", bitforce->device_id);
  210. bitforce->deven = DEV_RECOVER;
  211. bitforce->device_last_not_well = time(NULL);
  212. bitforce->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  213. bitforce->dev_thermal_cutoff_count++;
  214. }
  215. }
  216. }
  217. return true;
  218. }
  219. static bool bitforce_send_work(struct thr_info *thr, struct work *work)
  220. {
  221. struct cgpu_info *bitforce = thr->cgpu;
  222. int fdDev = bitforce->device_fd;
  223. unsigned char ob[70];
  224. char pdevbuf[0x100];
  225. char *s;
  226. if (!fdDev)
  227. return false;
  228. re_send:
  229. mutex_lock(&bitforce->device_mutex);
  230. if (bitforce->nonce_range)
  231. BFwrite(fdDev, "ZPX", 3);
  232. else
  233. BFwrite(fdDev, "ZDX", 3);
  234. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  235. if (!pdevbuf[0] || !strncasecmp(pdevbuf, "B", 1)) {
  236. mutex_unlock(&bitforce->device_mutex);
  237. nmsleep(WORK_CHECK_INTERVAL_MS);
  238. goto re_send;
  239. } else if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  240. mutex_unlock(&bitforce->device_mutex);
  241. if (bitforce->nonce_range) {
  242. applog(LOG_WARNING, "BFL%i: Does not support nonce range, disabling", bitforce->device_id);
  243. bitforce->nonce_range = false;
  244. bitforce->sleep_ms *= 5;
  245. bitforce->kname = KNAME_WORK;
  246. goto re_send;
  247. }
  248. applog(LOG_ERR, "BFL%i: Error: Send work reports: %s", bitforce->device_id, pdevbuf);
  249. return false;
  250. }
  251. sprintf((char *)ob, ">>>>>>>>");
  252. memcpy(ob + 8, work->midstate, 32);
  253. memcpy(ob + 8 + 32, work->data + 64, 12);
  254. if (!bitforce->nonce_range) {
  255. sprintf((char *)ob + 8 + 32 + 12, ">>>>>>>>");
  256. work->blk.nonce = bitforce->nonces = 0xffffffff;
  257. BFwrite(fdDev, ob, 60);
  258. } else {
  259. uint32_t *nonce;
  260. nonce = (uint32_t *)(ob + 8 + 32 + 12);
  261. *nonce = htobe32(work->blk.nonce);
  262. nonce = (uint32_t *)(ob + 8 + 32 + 12 + 4);
  263. /* Split work up into 1/5th nonce ranges */
  264. bitforce->nonces = 0x33333332;
  265. *nonce = htobe32(work->blk.nonce + bitforce->nonces);
  266. work->blk.nonce += bitforce->nonces + 1;
  267. sprintf((char *)ob + 8 + 32 + 12 + 8, ">>>>>>>>");
  268. BFwrite(fdDev, ob, 68);
  269. }
  270. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  271. mutex_unlock(&bitforce->device_mutex);
  272. if (opt_debug) {
  273. s = bin2hex(ob + 8, 44);
  274. applog(LOG_DEBUG, "BFL%i: block data: %s", bitforce->device_id, s);
  275. free(s);
  276. }
  277. if (unlikely(!pdevbuf[0])) {
  278. applog(LOG_ERR, "BFL%i: Error: Send block data returned empty string/timed out", bitforce->device_id);
  279. return false;
  280. }
  281. if (unlikely(strncasecmp(pdevbuf, "OK", 2))) {
  282. applog(LOG_ERR, "BFL%i: Error: Send block data reports: %s", bitforce->device_id, pdevbuf);
  283. return false;
  284. }
  285. gettimeofday(&bitforce->work_start_tv, NULL);
  286. return true;
  287. }
  288. static int64_t bitforce_get_result(struct thr_info *thr, struct work *work)
  289. {
  290. struct cgpu_info *bitforce = thr->cgpu;
  291. int fdDev = bitforce->device_fd;
  292. unsigned int delay_time_ms;
  293. struct timeval elapsed;
  294. struct timeval now;
  295. char pdevbuf[0x100];
  296. char *pnoncebuf;
  297. uint32_t nonce;
  298. if (!fdDev)
  299. return -1;
  300. while (1) {
  301. if (unlikely(thr->work_restart))
  302. return 0;
  303. mutex_lock(&bitforce->device_mutex);
  304. BFwrite(fdDev, "ZFX", 3);
  305. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  306. mutex_unlock(&bitforce->device_mutex);
  307. gettimeofday(&now, NULL);
  308. timersub(&now, &bitforce->work_start_tv, &elapsed);
  309. if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
  310. applog(LOG_ERR, "BFL%i: took %dms - longer than %dms", bitforce->device_id,
  311. tv_to_ms(elapsed), BITFORCE_LONG_TIMEOUT_MS);
  312. return 0;
  313. }
  314. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  315. break;
  316. /* if BFL is throttling, no point checking so quickly */
  317. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  318. nmsleep(delay_time_ms);
  319. bitforce->wait_ms += delay_time_ms;
  320. }
  321. if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
  322. applog(LOG_ERR, "BFL%i: took %dms - longer than %dms", bitforce->device_id,
  323. tv_to_ms(elapsed), BITFORCE_TIMEOUT_MS);
  324. bitforce->device_last_not_well = time(NULL);
  325. bitforce->device_not_well_reason = REASON_DEV_OVER_HEAT;
  326. bitforce->dev_over_heat_count++;
  327. if (!pdevbuf[0]) /* Only return if we got nothing after timeout - there still may be results */
  328. return 0;
  329. } else if (!strncasecmp(pdevbuf, "N", 1)) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  330. /* Simple timing adjustment. Allow a few polls to cope with
  331. * OS timer delays being variably reliable. wait_ms will
  332. * always equal sleep_ms when we've waited greater than or
  333. * equal to the result return time.*/
  334. delay_time_ms = bitforce->sleep_ms;
  335. if (bitforce->wait_ms > bitforce->sleep_ms + (WORK_CHECK_INTERVAL_MS * 2))
  336. bitforce->sleep_ms += (bitforce->wait_ms - bitforce->sleep_ms) / 2;
  337. else if (bitforce->wait_ms == bitforce->sleep_ms) {
  338. if (bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  339. bitforce->sleep_ms -= WORK_CHECK_INTERVAL_MS;
  340. else if (bitforce->sleep_ms > BITFORCE_CHECK_INTERVAL_MS)
  341. bitforce->sleep_ms -= BITFORCE_CHECK_INTERVAL_MS;
  342. }
  343. if (delay_time_ms != bitforce->sleep_ms)
  344. applog(LOG_DEBUG, "BFL%i: Wait time changed to: %d", bitforce->device_id, bitforce->sleep_ms, bitforce->wait_ms);
  345. /* Work out the average time taken. Float for calculation, uint for display */
  346. bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
  347. bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
  348. }
  349. applog(LOG_DEBUG, "BFL%i: waited %dms until %s", bitforce->device_id, bitforce->wait_ms, pdevbuf);
  350. if (!strncasecmp(&pdevbuf[2], "-", 1))
  351. return bitforce->nonces; /* No valid nonce found */
  352. else if (!strncasecmp(pdevbuf, "I", 1))
  353. return 0; /* Device idle */
  354. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  355. applog(LOG_WARNING, "BFL%i: Error: Get result reports: %s", bitforce->device_id, pdevbuf);
  356. return 0;
  357. }
  358. pnoncebuf = &pdevbuf[12];
  359. while (1) {
  360. hex2bin((void*)&nonce, pnoncebuf, 4);
  361. #ifndef __BIG_ENDIAN__
  362. nonce = swab32(nonce);
  363. #endif
  364. if (unlikely(bitforce->nonce_range && (nonce >= work->blk.nonce ||
  365. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  366. applog(LOG_WARNING, "BFL%i: Disabling broken nonce range support", bitforce->device_id);
  367. bitforce->nonce_range = false;
  368. work->blk.nonce = 0xffffffff;
  369. bitforce->sleep_ms *= 5;
  370. bitforce->kname = KNAME_WORK;
  371. }
  372. submit_nonce(thr, work, nonce);
  373. if (strncmp(&pnoncebuf[8], ",", 1))
  374. break;
  375. pnoncebuf += 9;
  376. }
  377. return bitforce->nonces;
  378. }
  379. static void bitforce_shutdown(struct thr_info *thr)
  380. {
  381. struct cgpu_info *bitforce = thr->cgpu;
  382. BFclose(bitforce->device_fd);
  383. bitforce->device_fd = 0;
  384. }
  385. static void biforce_thread_enable(struct thr_info *thr)
  386. {
  387. struct cgpu_info *bitforce = thr->cgpu;
  388. bitforce_init(bitforce);
  389. }
  390. static int64_t bitforce_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  391. {
  392. struct cgpu_info *bitforce = thr->cgpu;
  393. unsigned int sleep_time;
  394. int64_t ret;
  395. ret = bitforce_send_work(thr, work);
  396. if (!bitforce->nonce_range) {
  397. /* Initially wait 2/3 of the average cycle time so we can request more
  398. work before full scan is up */
  399. sleep_time = (2 * bitforce->sleep_ms) / 3;
  400. if (!restart_wait(sleep_time))
  401. return 0;
  402. bitforce->wait_ms = sleep_time;
  403. queue_request(thr, false);
  404. /* Now wait athe final 1/3rd; no bitforce should be finished by now */
  405. sleep_time = bitforce->sleep_ms - sleep_time;
  406. if (!restart_wait(sleep_time))
  407. return 0;
  408. bitforce->wait_ms += sleep_time;
  409. } else {
  410. sleep_time = bitforce->sleep_ms;
  411. if (!restart_wait(sleep_time))
  412. return 0;
  413. bitforce->wait_ms = sleep_time;
  414. }
  415. if (ret)
  416. ret = bitforce_get_result(thr, work);
  417. if (ret == -1) {
  418. ret = 0;
  419. applog(LOG_ERR, "BFL%i: Comms error", bitforce->device_id);
  420. bitforce->device_last_not_well = time(NULL);
  421. bitforce->device_not_well_reason = REASON_DEV_COMMS_ERROR;
  422. bitforce->dev_comms_error_count++;
  423. /* empty read buffer */
  424. biforce_clear_buffer(bitforce);
  425. }
  426. return ret;
  427. }
  428. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  429. {
  430. return bitforce_get_temp(bitforce);
  431. }
  432. static struct api_data *bitforce_api_stats(struct cgpu_info *cgpu)
  433. {
  434. struct api_data *root = NULL;
  435. // Warning, access to these is not locked - but we don't really
  436. // care since hashing performance is way more important than
  437. // locking access to displaying API debug 'stats'
  438. // If locking becomes an issue for any of them, use copy_data=true also
  439. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  440. root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
  441. return root;
  442. }
  443. struct device_api bitforce_api = {
  444. .dname = "bitforce",
  445. .name = "BFL",
  446. .api_detect = bitforce_detect,
  447. .get_api_stats = bitforce_api_stats,
  448. .reinit_device = bitforce_init,
  449. .get_statline_before = get_bitforce_statline_before,
  450. .get_stats = bitforce_get_stats,
  451. .thread_prepare = bitforce_thread_prepare,
  452. .scanhash = bitforce_scanhash,
  453. .thread_shutdown = bitforce_shutdown,
  454. .thread_enable = biforce_thread_enable
  455. };