driver-bitforce.c 13 KB

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