driver-bitforce.c 15 KB

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