driver-bitforce.c 15 KB

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