driver-bitforce.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. BFclose(fdDev);
  239. fdDev = BFopen(devpath);
  240. if (unlikely(fdDev == -1)) {
  241. applog(LOG_ERR, "BitForce init: Failed to open %s", devpath);
  242. return false;
  243. }
  244. bitforce->device_fd = fdDev;
  245. mutex_lock(&bitforce->dev_lock);
  246. BFwrite(fdDev, "ZGX", 3);
  247. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  248. mutex_unlock(&bitforce->dev_lock);
  249. if (unlikely(!pdevbuf[0])) {
  250. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  251. return false;
  252. }
  253. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  254. applog(LOG_ERR, "BitForce init: Didn't recognise BitForce on %s", devpath);
  255. return false;
  256. }
  257. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
  258. {
  259. s[0] = '\0';
  260. bitforce->name = strdup(pdevbuf + 7);
  261. }
  262. return true;
  263. }
  264. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  265. {
  266. int fdDev = bitforce->device_fd;
  267. char pdevbuf[0x100];
  268. char *s;
  269. mutex_lock(&bitforce->dev_lock);
  270. BFwrite(fdDev, "ZLX", 3);
  271. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  272. mutex_unlock(&bitforce->dev_lock);
  273. if (unlikely(!pdevbuf[0])) {
  274. applog(LOG_ERR, "Error reading temp from BitForce (ZLX)");
  275. return false;
  276. }
  277. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  278. float temp = strtof(s + 1, NULL);
  279. if (temp > 0) {
  280. bitforce->temp = temp;
  281. if (temp > bitforce->cutofftemp) {
  282. applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
  283. bitforce->deven = DEV_RECOVER;
  284. bitforce->device_last_not_well = time(NULL);
  285. bitforce->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  286. bitforce->dev_thermal_cutoff_count++;
  287. }
  288. }
  289. }
  290. return true;
  291. }
  292. static bool bitforce_send_work(struct thr_info *thr, struct work *work)
  293. {
  294. struct cgpu_info *bitforce = thr->cgpu;
  295. int fdDev = bitforce->device_fd;
  296. char pdevbuf[0x100];
  297. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  298. char *s;
  299. mutex_lock(&bitforce->dev_lock);
  300. BFwrite(fdDev, "ZDX", 3);
  301. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  302. if (unlikely(!pdevbuf[0])) {
  303. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  304. mutex_unlock(&bitforce->dev_lock);
  305. return false;
  306. }
  307. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  308. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  309. mutex_unlock(&bitforce->dev_lock);
  310. return false;
  311. }
  312. memcpy(ob + 8, work->midstate, 32);
  313. memcpy(ob + 8 + 32, work->data + 64, 12);
  314. BFwrite(fdDev, ob, 60);
  315. if (opt_debug) {
  316. s = bin2hex(ob + 8, 44);
  317. applog(LOG_DEBUG, "BitForce block data: %s", s);
  318. free(s);
  319. }
  320. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  321. mutex_unlock(&bitforce->dev_lock);
  322. if (unlikely(!pdevbuf[0])) {
  323. applog(LOG_ERR, "Error reading from BitForce (block data)");
  324. return false;
  325. }
  326. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  327. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  328. return false;
  329. }
  330. return true;
  331. }
  332. static uint64_t bitforce_get_result(struct thr_info *thr, struct work *work)
  333. {
  334. struct cgpu_info *bitforce = thr->cgpu;
  335. int fdDev = bitforce->device_fd;
  336. char pdevbuf[0x100];
  337. char *pnoncebuf;
  338. uint32_t nonce;
  339. int i;
  340. i = BITFORCE_SLEEP_MS;
  341. while (i < BITFORCE_TIMEOUT_MS) {
  342. mutex_lock(&bitforce->dev_lock);
  343. BFwrite(fdDev, "ZFX", 3);
  344. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  345. mutex_unlock(&bitforce->dev_lock);
  346. if (unlikely(!pdevbuf[0])) {
  347. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  348. mutex_unlock(&bitforce->dev_lock);
  349. return 0;
  350. }
  351. if (pdevbuf[0] != 'B')
  352. break;
  353. usleep(10000);
  354. i += 10;
  355. }
  356. if (i >= BITFORCE_TIMEOUT_MS) {
  357. applog(LOG_ERR, "BitForce took longer than 30s");
  358. bitforce->device_last_not_well = time(NULL);
  359. bitforce->device_not_well_reason = REASON_THREAD_ZERO_HASH;
  360. bitforce->thread_zero_hash_count++;
  361. return 1;
  362. }
  363. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  364. work->blk.nonce = 0xffffffff;
  365. if (pdevbuf[2] == '-')
  366. return 0xffffffff; /* No valid nonce found */
  367. else if (pdevbuf[0] == 'I')
  368. return 1; /* Device idle */
  369. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  370. applog(LOG_WARNING, "BitForce result reports: %s", pdevbuf);
  371. return 1;
  372. }
  373. pnoncebuf = &pdevbuf[12];
  374. while (1) {
  375. hex2bin((void*)&nonce, pnoncebuf, 4);
  376. #ifndef __BIG_ENDIAN__
  377. nonce = swab32(nonce);
  378. #endif
  379. submit_nonce(thr, work, nonce);
  380. if (pnoncebuf[8] != ',')
  381. break;
  382. pnoncebuf += 9;
  383. }
  384. return 0xffffffff;
  385. }
  386. static void bitforce_shutdown(struct thr_info *thr)
  387. {
  388. struct cgpu_info *bitforce = thr->cgpu;
  389. int fdDev = bitforce->device_fd;
  390. BFclose(fdDev);
  391. }
  392. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  393. {
  394. struct cgpu_info *bitforce = thr->cgpu;
  395. bool dev_enabled = (bitforce->deven == DEV_ENABLED);
  396. static enum dev_enable last_dev_state = DEV_ENABLED;
  397. if (bitforce->deven == DEV_DISABLED) {
  398. bitforce_shutdown(thr);
  399. return 1;
  400. }
  401. // if device has just gone from disabled to enabled, re-initialise it
  402. if (last_dev_state == DEV_DISABLED && dev_enabled)
  403. bitforce_init(bitforce);
  404. if (dev_enabled)
  405. if (!bitforce_send_work(thr, work))
  406. return 0;
  407. usleep(BITFORCE_SLEEP_US);
  408. if (dev_enabled)
  409. return bitforce_get_result(thr, work);
  410. else
  411. return 1;
  412. }
  413. static bool bitforce_get_stats(struct cgpu_info *bitforce)
  414. {
  415. return bitforce_get_temp(bitforce);
  416. }
  417. struct device_api bitforce_api = {
  418. .dname = "bitforce",
  419. .name = "BFL",
  420. .api_detect = bitforce_detect,
  421. .get_statline_before = get_bitforce_statline_before,
  422. .get_stats = bitforce_get_stats,
  423. .thread_prepare = bitforce_thread_prepare,
  424. .scanhash = bitforce_scanhash,
  425. .thread_shutdown = bitforce_shutdown
  426. };