driver-bitforce.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. return add_cgpu(bitforce);
  119. }
  120. static bool bitforce_detect_auto_udev()
  121. {
  122. #ifdef HAVE_LIBUDEV
  123. struct udev *udev = udev_new();
  124. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  125. struct udev_list_entry *list_entry;
  126. bool foundany = false;
  127. udev_enumerate_add_match_subsystem(enumerate, "tty");
  128. udev_enumerate_add_match_property(enumerate, "ID_MODEL", "BitFORCE*SHA256");
  129. udev_enumerate_scan_devices(enumerate);
  130. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  131. struct udev_device *device = udev_device_new_from_syspath(
  132. udev_enumerate_get_udev(enumerate),
  133. udev_list_entry_get_name(list_entry)
  134. );
  135. if (!device)
  136. continue;
  137. const char *devpath = udev_device_get_devnode(device);
  138. if (devpath) {
  139. foundany = true;
  140. bitforce_detect_one(devpath);
  141. }
  142. udev_device_unref(device);
  143. }
  144. udev_enumerate_unref(enumerate);
  145. udev_unref(udev);
  146. return foundany;
  147. #else
  148. return false;
  149. #endif
  150. }
  151. static bool bitforce_detect_auto_devserial()
  152. {
  153. #ifndef WIN32
  154. DIR *D;
  155. struct dirent *de;
  156. const char udevdir[] = "/dev/serial/by-id";
  157. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  158. char *devfile = devpath + sizeof(udevdir);
  159. bool foundany = false;
  160. D = opendir(udevdir);
  161. if (!D)
  162. return false;
  163. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  164. devpath[sizeof(udevdir) - 1] = '/';
  165. while ( (de = readdir(D)) ) {
  166. if (!strstr(de->d_name, "BitFORCE_SHA256"))
  167. continue;
  168. foundany = true;
  169. strcpy(devfile, de->d_name);
  170. bitforce_detect_one(devpath);
  171. }
  172. closedir(D);
  173. return foundany;
  174. #else
  175. return false;
  176. #endif
  177. }
  178. static void bitforce_detect_auto()
  179. {
  180. bitforce_detect_auto_udev() ?:
  181. bitforce_detect_auto_devserial() ?:
  182. 0;
  183. }
  184. static void bitforce_detect()
  185. {
  186. struct string_elist *iter, *tmp;
  187. const char*s;
  188. bool found = false;
  189. bool autoscan = false;
  190. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  191. s = iter->string;
  192. if (!strncmp("bitforce:", iter->string, 9))
  193. s += 9;
  194. if (!strcmp(s, "auto"))
  195. autoscan = true;
  196. else
  197. if (!strcmp(s, "noauto"))
  198. found = true;
  199. else if (bitforce_detect_one(s)) {
  200. string_elist_del(iter);
  201. found = true;
  202. }
  203. }
  204. if (autoscan || !found)
  205. bitforce_detect_auto();
  206. }
  207. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  208. {
  209. float gt = bitforce->temp;
  210. if (gt > 0)
  211. tailsprintf(buf, "%5.1fC ", gt);
  212. else
  213. tailsprintf(buf, " ", gt);
  214. tailsprintf(buf, " | ");
  215. }
  216. static bool bitforce_thread_prepare(struct thr_info *thr)
  217. {
  218. struct cgpu_info *bitforce = thr->cgpu;
  219. struct timeval now;
  220. int fdDev = BFopen(bitforce->device_path);
  221. if (unlikely(-1 == fdDev)) {
  222. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  223. return false;
  224. }
  225. bitforce->device_fd = fdDev;
  226. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  227. gettimeofday(&now, NULL);
  228. get_datestamp(bitforce->init, &now);
  229. return true;
  230. }
  231. static bool bitforce_init(struct cgpu_info *bitforce)
  232. {
  233. int fdDev = bitforce->device_fd;
  234. char *devpath = bitforce->device_path;
  235. char pdevbuf[0x100];
  236. char *s;
  237. BFclose(fdDev);
  238. fdDev = BFopen(devpath);
  239. if (unlikely(fdDev == -1)) {
  240. applog(LOG_ERR, "BitForce init: Failed to open %s", devpath);
  241. return false;
  242. }
  243. bitforce->device_fd = fdDev;
  244. BFwrite(fdDev, "ZGX", 3);
  245. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  246. if (unlikely(!pdevbuf[0])) {
  247. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  248. return false;
  249. }
  250. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  251. applog(LOG_ERR, "BitForce init: Didn't recognise BitForce on %s", devpath);
  252. return false;
  253. }
  254. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
  255. {
  256. s[0] = '\0';
  257. bitforce->name = strdup(pdevbuf + 7);
  258. }
  259. return true;
  260. }
  261. static bool bitforce_get_temp(struct cgpu_info *bitforce)
  262. {
  263. int fdDev = bitforce->device_fd;
  264. char pdevbuf[0x100];
  265. char *s;
  266. BFwrite(fdDev, "ZLX", 3);
  267. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  268. if (unlikely(!pdevbuf[0])) {
  269. applog(LOG_ERR, "Error reading temp from BitForce (ZLX)");
  270. return false;
  271. }
  272. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  273. float temp = strtof(s + 1, NULL);
  274. if (temp > 0) {
  275. bitforce->temp = temp;
  276. if (temp > bitforce->cutofftemp) {
  277. applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, setting idle", bitforce->api->name, bitforce->device_id);
  278. bitforce->deven = DEV_IDLE;
  279. bitforce->device_last_not_well = time(NULL);
  280. bitforce->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  281. bitforce->dev_thermal_cutoff_count++;
  282. }
  283. }
  284. }
  285. return true;
  286. }
  287. static bool bitforce_send_work(struct thr_info *thr, struct work *work)
  288. {
  289. struct cgpu_info *bitforce = thr->cgpu;
  290. int fdDev = bitforce->device_fd;
  291. char pdevbuf[0x100];
  292. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  293. char *s;
  294. BFwrite(fdDev, "ZDX", 3);
  295. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  296. if (unlikely(!pdevbuf[0])) {
  297. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  298. return false;
  299. }
  300. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  301. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  302. return false;
  303. }
  304. memcpy(ob + 8, work->midstate, 32);
  305. memcpy(ob + 8 + 32, work->data + 64, 12);
  306. BFwrite(fdDev, ob, 60);
  307. if (opt_debug) {
  308. s = bin2hex(ob + 8, 44);
  309. applog(LOG_DEBUG, "BitForce block data: %s", s);
  310. free(s);
  311. }
  312. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  313. if (unlikely(!pdevbuf[0])) {
  314. applog(LOG_ERR, "Error reading from BitForce (block data)");
  315. return false;
  316. }
  317. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  318. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  319. return false;
  320. }
  321. return true;
  322. }
  323. static uint64_t bitforce_get_result(struct thr_info *thr, struct work *work)
  324. {
  325. struct cgpu_info *bitforce = thr->cgpu;
  326. int fdDev = bitforce->device_fd;
  327. char pdevbuf[0x100];
  328. char *pnoncebuf;
  329. uint32_t nonce;
  330. int i;
  331. i = BITFORCE_SLEEP_MS;
  332. while (i < BITFORCE_TIMEOUT_MS) {
  333. BFwrite(fdDev, "ZFX", 3);
  334. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  335. if (unlikely(!pdevbuf[0])) {
  336. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  337. return 0;
  338. }
  339. if (pdevbuf[0] != 'B')
  340. break;
  341. usleep(10000);
  342. i += 10;
  343. }
  344. if (i >= BITFORCE_TIMEOUT_MS) {
  345. applog(LOG_ERR, "BitForce took longer than 30s");
  346. bitforce->device_last_not_well = time(NULL);
  347. bitforce->device_not_well_reason = REASON_THREAD_ZERO_HASH;
  348. bitforce->thread_zero_hash_count++;
  349. return 1;
  350. }
  351. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  352. work->blk.nonce = 0xffffffff;
  353. if (pdevbuf[2] == '-')
  354. return 0xffffffff; /* No valid nonce found */
  355. else if (pdevbuf[0] == 'I')
  356. return 1; /* Device idle */
  357. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  358. applog(LOG_WARNING, "BitForce result reports: %s", pdevbuf);
  359. return 1;
  360. }
  361. pnoncebuf = &pdevbuf[12];
  362. while (1) {
  363. hex2bin((void*)&nonce, pnoncebuf, 4);
  364. #ifndef __BIG_ENDIAN__
  365. nonce = swab32(nonce);
  366. #endif
  367. submit_nonce(thr, work, nonce);
  368. if (pnoncebuf[8] != ',')
  369. break;
  370. pnoncebuf += 9;
  371. }
  372. return 0xffffffff;
  373. }
  374. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  375. {
  376. struct cgpu_info *bitforce = thr->cgpu;
  377. bool dev_enabled = (bitforce->deven == DEV_ENABLED);
  378. static enum dev_enable last_dev_state = DEV_ENABLED;
  379. // if device has just gone from disabled to enabled, re-initialise it
  380. if (last_dev_state == DEV_DISABLED && dev_enabled)
  381. bitforce_init(bitforce);
  382. if (dev_enabled)
  383. if (!bitforce_send_work(thr, work))
  384. return 0;
  385. if (!bitforce_get_temp(bitforce))
  386. return 0;
  387. usleep(BITFORCE_SLEEP_US);
  388. if (dev_enabled)
  389. return bitforce_get_result(thr, work);
  390. else
  391. return 1;
  392. }
  393. static void bitforce_shutdown(struct thr_info *thr)
  394. {
  395. struct cgpu_info *bitforce = thr->cgpu;
  396. int fdDev = bitforce->device_fd;
  397. BFclose(fdDev);
  398. }
  399. struct device_api bitforce_api = {
  400. .dname = "bitforce",
  401. .name = "BFL",
  402. .api_detect = bitforce_detect,
  403. .get_statline_before = get_bitforce_statline_before,
  404. .thread_prepare = bitforce_thread_prepare,
  405. .scanhash = bitforce_scanhash,
  406. .thread_shutdown = bitforce_shutdown
  407. };