driver-bitforce.c 15 KB

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