driver-bitforce.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 int64_t bitforce_get_result(struct thr_info *thr, struct work *work)
  274. {
  275. struct cgpu_info *bitforce = thr->cgpu;
  276. int fdDev = bitforce->device_fd;
  277. unsigned int delay_time_ms;
  278. char pdevbuf[0x100];
  279. char *pnoncebuf;
  280. uint32_t nonce;
  281. if (!fdDev)
  282. return -1;
  283. while (bitforce->wait_ms < BITFORCE_LONG_TIMEOUT_MS) {
  284. mutex_lock(&bitforce->device_mutex);
  285. BFwrite(fdDev, "ZFX", 3);
  286. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  287. mutex_unlock(&bitforce->device_mutex);
  288. if (pdevbuf[0] && strncasecmp(pdevbuf, "B", 1)) /* BFL does not respond during throttling */
  289. break;
  290. /* if BFL is throttling, no point checking so quickly */
  291. delay_time_ms = (pdevbuf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
  292. nmsleep(delay_time_ms);
  293. bitforce->wait_ms += delay_time_ms;
  294. }
  295. if (bitforce->wait_ms >= BITFORCE_TIMEOUT_MS) {
  296. applog(LOG_ERR, "BFL%i: took %dms - longer than %dms", bitforce->device_id,
  297. bitforce->wait_ms, BITFORCE_TIMEOUT_MS);
  298. bitforce->device_last_not_well = time(NULL);
  299. bitforce->device_not_well_reason = REASON_DEV_OVER_HEAT;
  300. bitforce->dev_over_heat_count++;
  301. if (!pdevbuf[0]) /* Only return if we got nothing after timeout - there still may be results */
  302. return 0;
  303. } else if (!strncasecmp(pdevbuf, "N", 1)) {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  304. /* Simple timing adjustment. Allow a few polls to cope with
  305. * OS timer delays being variably reliable. wait_ms will
  306. * always equal sleep_ms when we've waited greater than or
  307. * equal to the result return time.*/
  308. delay_time_ms = bitforce->sleep_ms;
  309. if (bitforce->wait_ms > bitforce->sleep_ms + (WORK_CHECK_INTERVAL_MS * 2))
  310. bitforce->sleep_ms += (bitforce->wait_ms - bitforce->sleep_ms) / 2;
  311. else if (bitforce->wait_ms == bitforce->sleep_ms) {
  312. if (bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
  313. bitforce->sleep_ms -= WORK_CHECK_INTERVAL_MS;
  314. else if (bitforce->sleep_ms > BITFORCE_CHECK_INTERVAL_MS)
  315. bitforce->sleep_ms -= BITFORCE_CHECK_INTERVAL_MS;
  316. }
  317. if (delay_time_ms != bitforce->sleep_ms)
  318. applog(LOG_DEBUG, "BFL%i: Wait time changed to: %d", bitforce->device_id, bitforce->sleep_ms, bitforce->wait_ms);
  319. }
  320. applog(LOG_DEBUG, "BFL%i: waited %dms until %s", bitforce->device_id, bitforce->wait_ms, pdevbuf);
  321. if (!strncasecmp(&pdevbuf[2], "-", 1))
  322. return bitforce->nonces; /* No valid nonce found */
  323. else if (!strncasecmp(pdevbuf, "I", 1))
  324. return 0; /* Device idle */
  325. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  326. applog(LOG_WARNING, "BFL%i: Error: Get result reports: %s", bitforce->device_id, pdevbuf);
  327. return 0;
  328. }
  329. pnoncebuf = &pdevbuf[12];
  330. while (1) {
  331. hex2bin((void*)&nonce, pnoncebuf, 4);
  332. #ifndef __BIG_ENDIAN__
  333. nonce = swab32(nonce);
  334. #endif
  335. if (unlikely(bitforce->nonce_range && (nonce >= work->blk.nonce ||
  336. (work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
  337. applog(LOG_WARNING, "BFL%i: Disabling broken nonce range support", bitforce->device_id);
  338. bitforce->nonce_range = false;
  339. work->blk.nonce = 0xffffffff;
  340. bitforce->sleep_ms *= 5;
  341. }
  342. submit_nonce(thr, work, nonce);
  343. if (strncmp(&pnoncebuf[8], ",", 1))
  344. break;
  345. pnoncebuf += 9;
  346. }
  347. return bitforce->nonces;
  348. }
  349. static void bitforce_shutdown(struct thr_info *thr)
  350. {
  351. struct cgpu_info *bitforce = thr->cgpu;
  352. BFclose(bitforce->device_fd);
  353. bitforce->device_fd = 0;
  354. }
  355. static void biforce_thread_enable(struct thr_info *thr)
  356. {
  357. struct cgpu_info *bitforce = thr->cgpu;
  358. bitforce_init(bitforce);
  359. }
  360. static int64_t bitforce_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  361. {
  362. struct cgpu_info *bitforce = thr->cgpu;
  363. unsigned int sleep_time;
  364. int64_t ret;
  365. ret = bitforce_send_work(thr, work);
  366. if (!bitforce->nonce_range) {
  367. /* Initially wait 2/3 of the average cycle time so we can request more
  368. work before full scan is up */
  369. sleep_time = (2 * bitforce->sleep_ms) / 3;
  370. nmsleep(sleep_time);
  371. bitforce->wait_ms = sleep_time;
  372. queue_request(thr, false);
  373. /* Now wait athe final 1/3rd; no bitforce should be finished by now */
  374. sleep_time = bitforce->sleep_ms - sleep_time;
  375. nmsleep(sleep_time);
  376. bitforce->wait_ms += sleep_time;
  377. } else {
  378. sleep_time = bitforce->sleep_ms;
  379. nmsleep(sleep_time);
  380. bitforce->wait_ms = sleep_time;
  381. }
  382. if (ret)
  383. ret = bitforce_get_result(thr, work);
  384. if (!ret) {
  385. ret = 0;
  386. applog(LOG_ERR, "BFL%i: Comms error", bitforce->device_id);
  387. bitforce->device_last_not_well = time(NULL);
  388. bitforce->device_not_well_reason = REASON_DEV_COMMS_ERROR;
  389. bitforce->dev_comms_error_count++;
  390. /* empty read buffer */
  391. biforce_clear_buffer(bitforce);
  392. }
  393. return ret;
  394. }
  395. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  396. {
  397. return bitforce_get_temp(bitforce);
  398. }
  399. static bool bitforce_thread_init(struct thr_info *thr)
  400. {
  401. struct cgpu_info *bitforce = thr->cgpu;
  402. unsigned int wait;
  403. /* Pause each new thread at least 100ms between initialising
  404. * so the devices aren't making calls all at the same time. */
  405. wait = thr->id * MAX_START_DELAY_US;
  406. applog(LOG_DEBUG, "BFL%i: Delaying start by %dms", bitforce->device_id, wait / 1000);
  407. usleep(wait);
  408. return true;
  409. }
  410. static struct api_data *bitforce_api_stats(struct cgpu_info *cgpu)
  411. {
  412. struct api_data *root = NULL;
  413. // Warning, access to these is not locked - but we don't really
  414. // care since hashing performance is way more important than
  415. // locking access to displaying API debug 'stats'
  416. // If locking becomes an issue for any of them, use copy_data=true also
  417. root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
  418. return root;
  419. }
  420. struct device_api bitforce_api = {
  421. .dname = "bitforce",
  422. .name = "BFL",
  423. .api_detect = bitforce_detect,
  424. .get_api_stats = bitforce_api_stats,
  425. .reinit_device = bitforce_init,
  426. .get_statline_before = get_bitforce_statline_before,
  427. .get_stats = bitforce_get_stats,
  428. .thread_prepare = bitforce_thread_prepare,
  429. .thread_init = bitforce_thread_init,
  430. .scanhash = bitforce_scanhash,
  431. .thread_shutdown = bitforce_shutdown,
  432. .thread_enable = biforce_thread_enable
  433. };