driver-bitforce.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 <sys/types.h>
  16. #include <dirent.h>
  17. #ifndef WIN32
  18. #include <termios.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #ifndef O_CLOEXEC
  22. #define O_CLOEXEC 0
  23. #endif
  24. #else
  25. #include <windows.h>
  26. #include <io.h>
  27. #endif
  28. #include <unistd.h>
  29. #include "config.h"
  30. #ifdef HAVE_LIBUDEV
  31. #include <libudev.h>
  32. #endif
  33. #include "elist.h"
  34. #include "miner.h"
  35. #define BITFORCE_SLEEP_US 4500000
  36. #define BITFORCE_SLEEP_MS (BITFORCE_SLEEP_US/1000)
  37. #define BITFORCE_TIMEOUT_MS 30000
  38. struct device_api bitforce_api;
  39. static int BFopen(const char *devpath)
  40. {
  41. #ifdef WIN32
  42. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  43. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  44. return -1;
  45. COMMTIMEOUTS cto = {30000, 0, 30000, 0, 30000};
  46. SetCommTimeouts(hSerial, &cto);
  47. return _open_osfhandle((LONG)hSerial, 0);
  48. #else
  49. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  50. if (likely(fdDev != -1)) {
  51. struct termios pattr;
  52. tcgetattr(fdDev, &pattr);
  53. pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  54. pattr.c_oflag &= ~OPOST;
  55. pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  56. pattr.c_cflag &= ~(CSIZE | PARENB);
  57. pattr.c_cflag |= CS8;
  58. tcsetattr(fdDev, TCSANOW, &pattr);
  59. }
  60. tcflush(fdDev, TCOFLUSH);
  61. tcflush(fdDev, TCIFLUSH);
  62. return fdDev;
  63. #endif
  64. }
  65. static void BFgets(char *buf, size_t bufLen, int fd)
  66. {
  67. do
  68. --bufLen;
  69. while (likely(bufLen && read(fd, buf, 1) && (buf++)[0] != '\n'))
  70. ;
  71. buf[0] = '\0';
  72. }
  73. static ssize_t BFwrite2(int fd, const void *buf, ssize_t bufLen)
  74. {
  75. return write(fd, buf, bufLen);
  76. }
  77. #define BFwrite(fd, buf, bufLen) do { \
  78. if ((bufLen) != BFwrite2(fd, buf, bufLen)) { \
  79. applog(LOG_ERR, "Error writing to BitForce (" #buf ")"); \
  80. return 0; \
  81. } \
  82. } while(0)
  83. #define BFclose(fd) close(fd)
  84. static bool bitforce_detect_one(const char *devpath)
  85. {
  86. char *s;
  87. char pdevbuf[0x100];
  88. if (total_devices == MAX_DEVICES)
  89. return false;
  90. int fdDev = BFopen(devpath);
  91. if (unlikely(fdDev == -1)) {
  92. applog(LOG_ERR, "BitForce Detect: Failed to open %s", devpath);
  93. return false;
  94. }
  95. BFwrite(fdDev, "ZGX", 3);
  96. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  97. if (unlikely(!pdevbuf[0])) {
  98. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  99. return 0;
  100. }
  101. BFclose(fdDev);
  102. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  103. applog(LOG_ERR, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
  104. return false;
  105. }
  106. // We have a real BitForce!
  107. struct cgpu_info *bitforce;
  108. bitforce = calloc(1, sizeof(*bitforce));
  109. bitforce->api = &bitforce_api;
  110. bitforce->device_path = strdup(devpath);
  111. bitforce->deven = DEV_ENABLED;
  112. bitforce->threads = 1;
  113. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
  114. {
  115. s[0] = '\0';
  116. bitforce->name = strdup(pdevbuf + 7);
  117. }
  118. mutex_init(&bitforce->dev_lock);
  119. return add_cgpu(bitforce);
  120. }
  121. static bool bitforce_detect_auto_udev()
  122. {
  123. #ifdef HAVE_LIBUDEV
  124. struct udev *udev = udev_new();
  125. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  126. struct udev_list_entry *list_entry;
  127. bool foundany = false;
  128. udev_enumerate_add_match_subsystem(enumerate, "tty");
  129. udev_enumerate_add_match_property(enumerate, "ID_MODEL", "BitFORCE*SHA256");
  130. udev_enumerate_scan_devices(enumerate);
  131. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  132. struct udev_device *device = udev_device_new_from_syspath(
  133. udev_enumerate_get_udev(enumerate),
  134. udev_list_entry_get_name(list_entry)
  135. );
  136. if (!device)
  137. continue;
  138. const char *devpath = udev_device_get_devnode(device);
  139. if (devpath) {
  140. foundany = true;
  141. bitforce_detect_one(devpath);
  142. }
  143. udev_device_unref(device);
  144. }
  145. udev_enumerate_unref(enumerate);
  146. udev_unref(udev);
  147. return foundany;
  148. #else
  149. return false;
  150. #endif
  151. }
  152. static bool bitforce_detect_auto_devserial()
  153. {
  154. #ifndef WIN32
  155. DIR *D;
  156. struct dirent *de;
  157. const char udevdir[] = "/dev/serial/by-id";
  158. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  159. char *devfile = devpath + sizeof(udevdir);
  160. bool foundany = false;
  161. D = opendir(udevdir);
  162. if (!D)
  163. return false;
  164. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  165. devpath[sizeof(udevdir) - 1] = '/';
  166. while ( (de = readdir(D)) ) {
  167. if (!strstr(de->d_name, "BitFORCE_SHA256"))
  168. continue;
  169. foundany = true;
  170. strcpy(devfile, de->d_name);
  171. bitforce_detect_one(devpath);
  172. }
  173. closedir(D);
  174. return foundany;
  175. #else
  176. return false;
  177. #endif
  178. }
  179. static void bitforce_detect_auto()
  180. {
  181. bitforce_detect_auto_udev() ?:
  182. bitforce_detect_auto_devserial() ?:
  183. 0;
  184. }
  185. static void bitforce_detect()
  186. {
  187. struct string_elist *iter, *tmp;
  188. const char*s;
  189. bool found = false;
  190. bool autoscan = false;
  191. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  192. s = iter->string;
  193. if (!strncmp("bitforce:", iter->string, 9))
  194. s += 9;
  195. if (!strcmp(s, "auto"))
  196. autoscan = true;
  197. else
  198. if (!strcmp(s, "noauto"))
  199. found = true;
  200. else if (bitforce_detect_one(s)) {
  201. string_elist_del(iter);
  202. found = true;
  203. }
  204. }
  205. if (autoscan || !found)
  206. bitforce_detect_auto();
  207. }
  208. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  209. {
  210. float gt = bitforce->temp;
  211. if (gt > 0)
  212. tailsprintf(buf, "%5.1fC ", gt);
  213. else
  214. tailsprintf(buf, " ", gt);
  215. tailsprintf(buf, " | ");
  216. }
  217. static bool bitforce_thread_prepare(struct thr_info *thr)
  218. {
  219. struct cgpu_info *bitforce = thr->cgpu;
  220. struct timeval now;
  221. int fdDev = BFopen(bitforce->device_path);
  222. if (unlikely(-1 == fdDev)) {
  223. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  224. return false;
  225. }
  226. bitforce->device_fd = fdDev;
  227. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  228. gettimeofday(&now, NULL);
  229. get_datestamp(bitforce->init, &now);
  230. return true;
  231. }
  232. static bool bitforce_init(struct cgpu_info *bitforce)
  233. {
  234. int fdDev = bitforce->device_fd;
  235. char *devpath = bitforce->device_path;
  236. char pdevbuf[0x100];
  237. char *s;
  238. applog(LOG_DEBUG, "BFL%i: Re-initalizing", bitforce->device_id);
  239. BFclose(fdDev);
  240. fdDev = BFopen(devpath);
  241. if (unlikely(fdDev == -1)) {
  242. applog(LOG_ERR, "BitForce init: Failed to open %s", devpath);
  243. return false;
  244. }
  245. bitforce->device_fd = fdDev;
  246. mutex_lock(&bitforce->dev_lock);
  247. BFwrite(fdDev, "ZGX", 3);
  248. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  249. mutex_unlock(&bitforce->dev_lock);
  250. if (unlikely(!pdevbuf[0])) {
  251. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  252. return false;
  253. }
  254. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  255. applog(LOG_ERR, "BitForce init: Didn't recognise BitForce on %s", devpath);
  256. return false;
  257. }
  258. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
  259. {
  260. s[0] = '\0';
  261. bitforce->name = strdup(pdevbuf + 7);
  262. }
  263. return true;
  264. }
  265. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  266. {
  267. int fdDev = bitforce->device_fd;
  268. char pdevbuf[0x100];
  269. char *s;
  270. mutex_lock(&bitforce->dev_lock);
  271. BFwrite(fdDev, "ZLX", 3);
  272. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  273. mutex_unlock(&bitforce->dev_lock);
  274. if (unlikely(!pdevbuf[0])) {
  275. applog(LOG_ERR, "Error reading temp from BitForce (ZLX)");
  276. return false;
  277. }
  278. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  279. float temp = strtof(s + 1, NULL);
  280. if (temp > 0) {
  281. bitforce->temp = temp;
  282. if (temp > bitforce->cutofftemp) {
  283. applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
  284. bitforce->deven = DEV_RECOVER;
  285. bitforce->device_last_not_well = time(NULL);
  286. bitforce->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  287. bitforce->dev_thermal_cutoff_count++;
  288. }
  289. }
  290. }
  291. return true;
  292. }
  293. static bool bitforce_send_work(struct thr_info *thr, struct work *work)
  294. {
  295. struct cgpu_info *bitforce = thr->cgpu;
  296. int fdDev = bitforce->device_fd;
  297. char pdevbuf[0x100];
  298. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  299. char *s;
  300. mutex_lock(&bitforce->dev_lock);
  301. BFwrite(fdDev, "ZDX", 3);
  302. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  303. if (unlikely(!pdevbuf[0])) {
  304. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  305. mutex_unlock(&bitforce->dev_lock);
  306. return false;
  307. }
  308. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  309. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  310. mutex_unlock(&bitforce->dev_lock);
  311. return false;
  312. }
  313. memcpy(ob + 8, work->midstate, 32);
  314. memcpy(ob + 8 + 32, work->data + 64, 12);
  315. BFwrite(fdDev, ob, 60);
  316. if (opt_debug) {
  317. s = bin2hex(ob + 8, 44);
  318. applog(LOG_DEBUG, "BitForce block data: %s", s);
  319. free(s);
  320. }
  321. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  322. mutex_unlock(&bitforce->dev_lock);
  323. if (unlikely(!pdevbuf[0])) {
  324. applog(LOG_ERR, "Error reading from BitForce (block data)");
  325. return false;
  326. }
  327. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  328. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  329. return false;
  330. }
  331. return true;
  332. }
  333. static uint64_t bitforce_get_result(struct thr_info *thr, struct work *work)
  334. {
  335. struct cgpu_info *bitforce = thr->cgpu;
  336. int fdDev = bitforce->device_fd;
  337. char pdevbuf[0x100];
  338. char *pnoncebuf;
  339. uint32_t nonce;
  340. int i;
  341. i = BITFORCE_SLEEP_MS;
  342. while (i < BITFORCE_TIMEOUT_MS) {
  343. mutex_lock(&bitforce->dev_lock);
  344. BFwrite(fdDev, "ZFX", 3);
  345. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  346. mutex_unlock(&bitforce->dev_lock);
  347. if (unlikely(!pdevbuf[0])) {
  348. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  349. mutex_unlock(&bitforce->dev_lock);
  350. return 0;
  351. }
  352. if (pdevbuf[0] != 'B')
  353. break;
  354. usleep(10000);
  355. i += 10;
  356. }
  357. if (i >= BITFORCE_TIMEOUT_MS) {
  358. applog(LOG_ERR, "BitForce took longer than 30s");
  359. bitforce->device_last_not_well = time(NULL);
  360. bitforce->device_not_well_reason = REASON_THREAD_ZERO_HASH;
  361. bitforce->thread_zero_hash_count++;
  362. return 1;
  363. }
  364. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  365. work->blk.nonce = 0xffffffff;
  366. if (pdevbuf[2] == '-')
  367. return 0xffffffff; /* No valid nonce found */
  368. else if (pdevbuf[0] == 'I')
  369. return 1; /* Device idle */
  370. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  371. applog(LOG_WARNING, "BitForce result reports: %s", pdevbuf);
  372. return 1;
  373. }
  374. pnoncebuf = &pdevbuf[12];
  375. while (1) {
  376. hex2bin((void*)&nonce, pnoncebuf, 4);
  377. #ifndef __BIG_ENDIAN__
  378. nonce = swab32(nonce);
  379. #endif
  380. submit_nonce(thr, work, nonce);
  381. if (pnoncebuf[8] != ',')
  382. break;
  383. pnoncebuf += 9;
  384. }
  385. return 0xffffffff;
  386. }
  387. static void bitforce_shutdown(struct thr_info *thr)
  388. {
  389. struct cgpu_info *bitforce = thr->cgpu;
  390. int fdDev = bitforce->device_fd;
  391. BFclose(fdDev);
  392. }
  393. #define CHECK_INTERVAL_MS 200
  394. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  395. {
  396. struct cgpu_info *bitforce = thr->cgpu;
  397. bool dev_enabled = (bitforce->deven == DEV_ENABLED);
  398. static enum dev_enable last_dev_state = DEV_ENABLED;
  399. int wait_ms = 0;
  400. if (bitforce->deven == DEV_DISABLED) {
  401. bitforce_shutdown(thr);
  402. return 1;
  403. }
  404. // if device has just gone from disabled to enabled, re-initialise it
  405. if (last_dev_state == DEV_DISABLED && dev_enabled)
  406. bitforce_init(bitforce);
  407. last_dev_state = bitforce->deven;
  408. if (!bitforce_send_work(thr, work))
  409. return 0;
  410. while (wait_ms < BITFORCE_SLEEP_MS) {
  411. usleep(CHECK_INTERVAL_MS * 1000);
  412. wait_ms += CHECK_INTERVAL_MS;
  413. if (work_restart[thr->id].restart) {
  414. applog(LOG_DEBUG, "BFL%i: New work detected, discarding current job", bitforce->device_id);
  415. return 1; //we have discard all work; equivilent to 0 hashes done.
  416. }
  417. }
  418. return bitforce_get_result(thr, work);
  419. }
  420. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  421. {
  422. return bitforce_get_temp(bitforce);
  423. }
  424. struct device_api bitforce_api = {
  425. .dname = "bitforce",
  426. .name = "BFL",
  427. .api_detect = bitforce_detect,
  428. .get_statline_before = get_bitforce_statline_before,
  429. .get_stats = bitforce_get_stats,
  430. .thread_prepare = bitforce_thread_prepare,
  431. .scanhash = bitforce_scanhash,
  432. .thread_shutdown = bitforce_shutdown
  433. };