driver-bitforce.c 14 KB

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