driver-bitforce.c 15 KB

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