driver-bitforce.c 12 KB

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