driver-bitforce.c 15 KB

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