driver-bitforce.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 2000
  20. #define BITFORCE_TIMEOUT_MS 10000
  21. #define BITFORCE_CHECK_INTERVAL_MS 10
  22. #define WORK_CHECK_INTERVAL_MS 50
  23. #define MAX_START_DELAY_US 100000
  24. struct device_api bitforce_api;
  25. #define BFopen(devpath) serial_open(devpath, 0, -1, true)
  26. static void BFgets(char *buf, size_t bufLen, int fd)
  27. {
  28. do
  29. --bufLen;
  30. while (likely(bufLen && read(fd, buf, 1) && (buf++)[0] != '\n'));
  31. buf[0] = '\0';
  32. }
  33. static ssize_t BFwrite(int fd, const void *buf, ssize_t bufLen)
  34. {
  35. if ((bufLen) != write(fd, buf, bufLen)) {
  36. applog(LOG_ERR, "BFL: Error writing: %s", buf);
  37. return 0;
  38. } else {
  39. usleep(100);
  40. return bufLen;
  41. }
  42. }
  43. #define BFclose(fd) close(fd)
  44. static bool bitforce_detect_one(const char *devpath)
  45. {
  46. char *s;
  47. char pdevbuf[0x100];
  48. applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
  49. int fdDev = BFopen(devpath);
  50. if (unlikely(fdDev == -1)) {
  51. applog(LOG_ERR, "BFL: Failed to open %s", devpath);
  52. return false;
  53. }
  54. BFwrite(fdDev, "ZGX", 3);
  55. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  56. if (unlikely(!pdevbuf[0])) {
  57. applog(LOG_ERR, "BFL: Error reading (ZGX)");
  58. return 0;
  59. }
  60. BFclose(fdDev);
  61. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  62. applog(LOG_ERR, "BFL: Didn't recognise BitForce on %s", devpath);
  63. return false;
  64. }
  65. // We have a real BitForce!
  66. struct cgpu_info *bitforce;
  67. bitforce = calloc(1, sizeof(*bitforce));
  68. bitforce->api = &bitforce_api;
  69. bitforce->device_path = strdup(devpath);
  70. bitforce->deven = DEV_ENABLED;
  71. bitforce->threads = 1;
  72. bitforce->sleep_ms = BITFORCE_SLEEP_MS;
  73. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
  74. {
  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
  84. serial_autodetect_udev (bitforce_detect_one, "BitFORCE*SHA256") ?:
  85. serial_autodetect_devserial(bitforce_detect_one, "BitFORCE_SHA256") ?:
  86. 0;
  87. }
  88. static void bitforce_detect()
  89. {
  90. serial_detect_auto(bitforce_api.dname, bitforce_detect_one, bitforce_detect_auto);
  91. }
  92. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  93. {
  94. float gt = bitforce->temp;
  95. if (gt > 0)
  96. tailsprintf(buf, "%5.1fC ", gt);
  97. else
  98. tailsprintf(buf, " ", gt);
  99. tailsprintf(buf, " | ");
  100. }
  101. static bool bitforce_thread_prepare(struct thr_info *thr)
  102. {
  103. struct cgpu_info *bitforce = thr->cgpu;
  104. struct timeval now;
  105. int fdDev = BFopen(bitforce->device_path);
  106. if (unlikely(-1 == fdDev)) {
  107. applog(LOG_ERR, "BFL%i: Failed to open %s", bitforce->device_id, bitforce->device_path);
  108. return false;
  109. }
  110. bitforce->device_fd = fdDev;
  111. applog(LOG_INFO, "BFL%i: Opened %s", bitforce->device_id, bitforce->device_path);
  112. gettimeofday(&now, NULL);
  113. get_datestamp(bitforce->init, &now);
  114. return true;
  115. }
  116. static void biforce_clear_buffer(struct cgpu_info *bitforce)
  117. {
  118. int fdDev = bitforce->device_fd;
  119. char pdevbuf[0x100];
  120. applog(LOG_DEBUG, "BFL%i: Clearing read buffer", bitforce->device_id);
  121. mutex_lock(&bitforce->device_mutex);
  122. do {
  123. pdevbuf[0] = '\0';
  124. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  125. } while (pdevbuf[0]);
  126. mutex_unlock(&bitforce->device_mutex);
  127. }
  128. void bitforce_init(struct cgpu_info *bitforce)
  129. {
  130. int fdDev = bitforce->device_fd;
  131. char *devpath = bitforce->device_path;
  132. char pdevbuf[0x100];
  133. char *s;
  134. applog(LOG_WARNING, "BFL%i: Re-initalizing", bitforce->device_id);
  135. biforce_clear_buffer(bitforce);
  136. mutex_lock(&bitforce->device_mutex);
  137. if (fdDev)
  138. BFclose(fdDev);
  139. bitforce->device_fd = 0;
  140. fdDev = BFopen(devpath);
  141. if (unlikely(fdDev == -1)) {
  142. applog(LOG_ERR, "BFL%i: Failed to open %s", bitforce->device_id, devpath);
  143. mutex_unlock(&bitforce->device_mutex);
  144. return;
  145. }
  146. BFwrite(fdDev, "ZGX", 3);
  147. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  148. if (unlikely(!pdevbuf[0])) {
  149. applog(LOG_ERR, "BFL%i: Error reading (ZGX)", bitforce->device_id);
  150. mutex_unlock(&bitforce->device_mutex);
  151. return;
  152. }
  153. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  154. applog(LOG_ERR, "BFL%i: Didn't recognise BitForce on %s returned: %s", bitforce->device_id, devpath, pdevbuf);
  155. mutex_unlock(&bitforce->device_mutex);
  156. return;
  157. }
  158. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
  159. {
  160. s[0] = '\0';
  161. bitforce->name = strdup(pdevbuf + 7);
  162. }
  163. bitforce->device_fd = fdDev;
  164. mutex_unlock(&bitforce->device_mutex);
  165. }
  166. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  167. {
  168. int fdDev = bitforce->device_fd;
  169. char pdevbuf[0x100];
  170. char *s;
  171. if (!fdDev)
  172. return false;
  173. mutex_lock(&bitforce->device_mutex);
  174. BFwrite(fdDev, "ZLX", 3);
  175. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  176. mutex_unlock(&bitforce->device_mutex);
  177. if (unlikely(!pdevbuf[0])) {
  178. applog(LOG_ERR, "BFL%i: Error: Get temp returned empty string", bitforce->device_id);
  179. bitforce->temp = 0;
  180. return false;
  181. }
  182. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  183. float temp = strtof(s + 1, NULL);
  184. if (temp > 0) {
  185. bitforce->temp = temp;
  186. if (temp > bitforce->cutofftemp) {
  187. applog(LOG_WARNING, "BFL%i: Hit thermal cutoff limit, disabling!", bitforce->device_id);
  188. bitforce->deven = DEV_RECOVER;
  189. bitforce->device_last_not_well = time(NULL);
  190. bitforce->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  191. bitforce->dev_thermal_cutoff_count++;
  192. }
  193. }
  194. }
  195. return true;
  196. }
  197. static bool bitforce_send_work(struct thr_info *thr, struct work *work)
  198. {
  199. struct cgpu_info *bitforce = thr->cgpu;
  200. int fdDev = bitforce->device_fd;
  201. char pdevbuf[0x100];
  202. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  203. char *s;
  204. if (!fdDev)
  205. return false;
  206. re_send:
  207. mutex_lock(&bitforce->device_mutex);
  208. BFwrite(fdDev, "ZDX", 3);
  209. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  210. if (unlikely(!pdevbuf[0])) {
  211. applog(LOG_ERR, "BFL%i: Error: Send work returned empty string", bitforce->device_id);
  212. mutex_unlock(&bitforce->device_mutex);
  213. return false;
  214. } else if (pdevbuf[0] == 'B') {
  215. applog(LOG_DEBUG, "BFL%i: Busy", bitforce->device_id);
  216. mutex_unlock(&bitforce->device_mutex);
  217. bitforce->wait_ms += BITFORCE_CHECK_INTERVAL_MS;
  218. usleep(BITFORCE_CHECK_INTERVAL_MS*1000);
  219. goto re_send;
  220. } else if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  221. applog(LOG_ERR, "BFL%i: Error: Send work reports: %s", bitforce->device_id, pdevbuf);
  222. mutex_unlock(&bitforce->device_mutex);
  223. return false;
  224. }
  225. memcpy(ob + 8, work->midstate, 32);
  226. memcpy(ob + 8 + 32, work->data + 64, 12);
  227. BFwrite(fdDev, ob, 60);
  228. if (opt_debug) {
  229. s = bin2hex(ob + 8, 44);
  230. applog(LOG_DEBUG, "BFL%i: block data: %s", bitforce->device_id, s);
  231. free(s);
  232. }
  233. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  234. mutex_unlock(&bitforce->device_mutex);
  235. if (unlikely(!pdevbuf[0])) {
  236. applog(LOG_ERR, "BFL%i: Error: Send block data returned empty string", bitforce->device_id);
  237. return false;
  238. }
  239. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  240. applog(LOG_ERR, "BFL%i: Error: Send block data reports: %s", bitforce->device_id, pdevbuf);
  241. return false;
  242. }
  243. return true;
  244. }
  245. static uint64_t bitforce_get_result(struct thr_info *thr, struct work *work)
  246. {
  247. struct cgpu_info *bitforce = thr->cgpu;
  248. int fdDev = bitforce->device_fd;
  249. char pdevbuf[0x100];
  250. char *pnoncebuf;
  251. uint32_t nonce;
  252. if (!fdDev)
  253. return 0;
  254. while (bitforce->wait_ms < BITFORCE_TIMEOUT_MS) {
  255. mutex_lock(&bitforce->device_mutex);
  256. BFwrite(fdDev, "ZFX", 3);
  257. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  258. mutex_unlock(&bitforce->device_mutex);
  259. if (unlikely(!pdevbuf[0])) {
  260. applog(LOG_ERR, "BFL%i: Error: Get result returned empty string", bitforce->device_id);
  261. return 0;
  262. }
  263. if (pdevbuf[0] != 'B')
  264. break;
  265. usleep(BITFORCE_CHECK_INTERVAL_MS*1000);
  266. bitforce->wait_ms += BITFORCE_CHECK_INTERVAL_MS;
  267. }
  268. if (bitforce->wait_ms >= BITFORCE_TIMEOUT_MS) {
  269. applog(LOG_ERR, "BFL%i: took longer than 10s", bitforce->device_id);
  270. bitforce->device_last_not_well = time(NULL);
  271. bitforce->device_not_well_reason = REASON_DEV_OVER_HEAT;
  272. bitforce->dev_over_heat_count++;
  273. return 1;
  274. } else if (pdevbuf[0] == 'N') {/* Hashing complete (NONCE-FOUND or NO-NONCE) */
  275. /* Simple timing adjustment */
  276. if (bitforce->wait_ms > (bitforce->sleep_ms + WORK_CHECK_INTERVAL_MS))
  277. bitforce->sleep_ms += WORK_CHECK_INTERVAL_MS;
  278. else if (bitforce->wait_ms == bitforce->sleep_ms)
  279. bitforce->sleep_ms -= WORK_CHECK_INTERVAL_MS;
  280. }
  281. applog(LOG_DEBUG, "BFL%i: waited %dms until %s", bitforce->device_id, bitforce->wait_ms, pdevbuf);
  282. work->blk.nonce = 0xffffffff;
  283. if (pdevbuf[2] == '-')
  284. return 0xffffffff; /* No valid nonce found */
  285. else if (pdevbuf[0] == 'I')
  286. return 1; /* Device idle */
  287. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  288. applog(LOG_WARNING, "BFL%i: Error: Get result reports: %s", bitforce->device_id, pdevbuf);
  289. return 1;
  290. }
  291. pnoncebuf = &pdevbuf[12];
  292. while (1) {
  293. hex2bin((void*)&nonce, pnoncebuf, 4);
  294. #ifndef __BIG_ENDIAN__
  295. nonce = swab32(nonce);
  296. #endif
  297. submit_nonce(thr, work, nonce);
  298. if (pnoncebuf[8] != ',')
  299. break;
  300. pnoncebuf += 9;
  301. }
  302. return 0xffffffff;
  303. }
  304. static void bitforce_shutdown(struct thr_info *thr)
  305. {
  306. struct cgpu_info *bitforce = thr->cgpu;
  307. BFclose(bitforce->device_fd);
  308. bitforce->device_fd = 0;
  309. }
  310. static void biforce_thread_enable(struct thr_info *thr)
  311. {
  312. struct cgpu_info *bitforce = thr->cgpu;
  313. bitforce_init(bitforce);
  314. }
  315. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  316. {
  317. struct cgpu_info *bitforce = thr->cgpu;
  318. bitforce->wait_ms = 0;
  319. uint64_t ret;
  320. ret = bitforce_send_work(thr, work);
  321. while (bitforce->wait_ms < bitforce->sleep_ms) {
  322. usleep(WORK_CHECK_INTERVAL_MS*1000);
  323. bitforce->wait_ms += WORK_CHECK_INTERVAL_MS;
  324. if (work_restart[thr->id].restart) {
  325. applog(LOG_DEBUG, "BFL%i: Work restart, discarding after %dms", bitforce->device_id, bitforce->wait_ms);
  326. return 1; //we have discarded all work; equivilent to 0 hashes done.
  327. }
  328. }
  329. if (ret)
  330. ret = bitforce_get_result(thr, work);
  331. if (!ret) {
  332. ret = 1;
  333. applog(LOG_ERR, "BFL%i: Comms error", bitforce->device_id);
  334. bitforce->device_last_not_well = time(NULL);
  335. bitforce->device_not_well_reason = REASON_DEV_NOSTART;
  336. bitforce->dev_nostart_count++;
  337. /* empty read buffer */
  338. biforce_clear_buffer(bitforce);
  339. }
  340. return ret;
  341. }
  342. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  343. {
  344. return bitforce_get_temp(bitforce);
  345. }
  346. static bool bitforce_thread_init(struct thr_info *thr)
  347. {
  348. struct cgpu_info *bitforce = thr->cgpu;
  349. unsigned int wait;
  350. /* Pause each new thread a random time between 0-100ms
  351. so the devices aren't making calls all at the same time. */
  352. wait = (rand() * MAX_START_DELAY_US)/RAND_MAX;
  353. applog(LOG_DEBUG, "BFL%i: Delaying start by %dms", bitforce->device_id, wait/1000);
  354. usleep(wait);
  355. return true;
  356. }
  357. struct device_api bitforce_api = {
  358. .dname = "bitforce",
  359. .name = "BFL",
  360. .api_detect = bitforce_detect,
  361. .reinit_device = bitforce_init,
  362. .get_statline_before = get_bitforce_statline_before,
  363. .get_stats = bitforce_get_stats,
  364. .thread_prepare = bitforce_thread_prepare,
  365. .thread_init = bitforce_thread_init,
  366. .scanhash = bitforce_scanhash,
  367. .thread_shutdown = bitforce_shutdown,
  368. .thread_enable = biforce_thread_enable
  369. };