driver-bitforce.c 14 KB

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