driver-bitforce.c 12 KB

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