driver-bitforce.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. struct device_api bitforce_api;
  36. static int BFopen(const char *devpath)
  37. {
  38. #ifdef WIN32
  39. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  40. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  41. return -1;
  42. COMMTIMEOUTS cto = {30000, 0, 30000, 0, 30000};
  43. SetCommTimeouts(hSerial, &cto);
  44. return _open_osfhandle((LONG)hSerial, 0);
  45. #else
  46. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  47. if (likely(fdDev != -1)) {
  48. struct termios pattr;
  49. tcgetattr(fdDev, &pattr);
  50. pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  51. pattr.c_oflag &= ~OPOST;
  52. pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  53. pattr.c_cflag &= ~(CSIZE | PARENB);
  54. pattr.c_cflag |= CS8;
  55. tcsetattr(fdDev, TCSANOW, &pattr);
  56. }
  57. tcflush(fdDev, TCOFLUSH);
  58. tcflush(fdDev, TCIFLUSH);
  59. return fdDev;
  60. #endif
  61. }
  62. static void BFgets(char *buf, size_t bufLen, int fd)
  63. {
  64. do
  65. --bufLen;
  66. while (likely(bufLen && read(fd, buf, 1) && (buf++)[0] != '\n'))
  67. ;
  68. buf[0] = '\0';
  69. }
  70. static void BFwrite(int fd, const void *buf, ssize_t bufLen)
  71. {
  72. ssize_t ret = write(fd, buf, bufLen);
  73. if (unlikely(ret != bufLen))
  74. quit(1, "BFwrite failed");
  75. }
  76. #define BFclose(fd) close(fd)
  77. static bool bitforce_detect_one(const char *devpath)
  78. {
  79. char *s;
  80. char pdevbuf[0x100];
  81. if (total_devices == MAX_DEVICES)
  82. return false;
  83. int fdDev = BFopen(devpath);
  84. if (unlikely(fdDev == -1)) {
  85. applog(LOG_DEBUG, "BitForce Detect: Failed to open %s", devpath);
  86. return false;
  87. }
  88. BFwrite(fdDev, "ZGX", 3);
  89. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  90. if (unlikely(!pdevbuf[0])) {
  91. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  92. return 0;
  93. }
  94. BFclose(fdDev);
  95. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  96. applog(LOG_DEBUG, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
  97. return false;
  98. }
  99. // We have a real BitForce!
  100. struct cgpu_info *bitforce;
  101. bitforce = calloc(1, sizeof(*bitforce));
  102. bitforce->api = &bitforce_api;
  103. bitforce->device_path = strdup(devpath);
  104. bitforce->deven = DEV_ENABLED;
  105. bitforce->threads = 1;
  106. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
  107. {
  108. s[0] = '\0';
  109. bitforce->name = strdup(pdevbuf + 7);
  110. }
  111. return add_cgpu(bitforce);
  112. }
  113. static bool bitforce_detect_auto_udev()
  114. {
  115. #ifdef HAVE_LIBUDEV
  116. struct udev *udev = udev_new();
  117. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  118. struct udev_list_entry *list_entry;
  119. bool foundany = false;
  120. udev_enumerate_add_match_subsystem(enumerate, "tty");
  121. udev_enumerate_add_match_property(enumerate, "ID_MODEL", "BitFORCE*SHA256");
  122. udev_enumerate_scan_devices(enumerate);
  123. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  124. struct udev_device *device = udev_device_new_from_syspath(
  125. udev_enumerate_get_udev(enumerate),
  126. udev_list_entry_get_name(list_entry)
  127. );
  128. if (!device)
  129. continue;
  130. const char *devpath = udev_device_get_devnode(device);
  131. if (devpath) {
  132. foundany = true;
  133. bitforce_detect_one(devpath);
  134. }
  135. udev_device_unref(device);
  136. }
  137. udev_enumerate_unref(enumerate);
  138. udev_unref(udev);
  139. return foundany;
  140. #else
  141. return false;
  142. #endif
  143. }
  144. static bool bitforce_detect_auto_devserial()
  145. {
  146. #ifndef WIN32
  147. DIR *D;
  148. struct dirent *de;
  149. const char udevdir[] = "/dev/serial/by-id";
  150. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  151. char *devfile = devpath + sizeof(udevdir);
  152. bool foundany = false;
  153. D = opendir(udevdir);
  154. if (!D)
  155. return false;
  156. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  157. devpath[sizeof(udevdir) - 1] = '/';
  158. while ( (de = readdir(D)) ) {
  159. if (!strstr(de->d_name, "BitFORCE_SHA256"))
  160. continue;
  161. foundany = true;
  162. strcpy(devfile, de->d_name);
  163. bitforce_detect_one(devpath);
  164. }
  165. closedir(D);
  166. return foundany;
  167. #else
  168. return false;
  169. #endif
  170. }
  171. static void bitforce_detect_auto()
  172. {
  173. bitforce_detect_auto_udev() ?:
  174. bitforce_detect_auto_devserial() ?:
  175. 0;
  176. }
  177. static void bitforce_detect()
  178. {
  179. struct string_elist *iter, *tmp;
  180. const char*s;
  181. bool found = false;
  182. bool autoscan = false;
  183. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  184. s = iter->string;
  185. if (!strncmp("bitforce:", iter->string, 9))
  186. s += 9;
  187. if (!strcmp(s, "auto"))
  188. autoscan = true;
  189. else
  190. if (!strcmp(s, "noauto"))
  191. found = true;
  192. else if (bitforce_detect_one(s)) {
  193. string_elist_del(iter);
  194. found = true;
  195. }
  196. }
  197. if (autoscan || !found)
  198. bitforce_detect_auto();
  199. }
  200. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  201. {
  202. float gt = bitforce->temp;
  203. if (gt > 0)
  204. tailsprintf(buf, "%5.1fC ", gt);
  205. else
  206. tailsprintf(buf, " ", gt);
  207. tailsprintf(buf, " | ");
  208. }
  209. static bool bitforce_thread_prepare(struct thr_info *thr)
  210. {
  211. struct cgpu_info *bitforce = thr->cgpu;
  212. struct timeval now;
  213. int fdDev = BFopen(bitforce->device_path);
  214. if (unlikely(-1 == fdDev)) {
  215. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  216. return false;
  217. }
  218. bitforce->device_fd = fdDev;
  219. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  220. gettimeofday(&now, NULL);
  221. get_datestamp(bitforce->init, &now);
  222. return true;
  223. }
  224. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  225. {
  226. struct cgpu_info *bitforce = thr->cgpu;
  227. int fdDev = bitforce->device_fd;
  228. char pdevbuf[0x100];
  229. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  230. int i;
  231. char *pnoncebuf;
  232. char *s;
  233. uint32_t nonce;
  234. BFwrite(fdDev, "ZDX", 3);
  235. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  236. if (unlikely(!pdevbuf[0])) {
  237. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  238. return 0;
  239. }
  240. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  241. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  242. return 0;
  243. }
  244. memcpy(ob + 8, work->midstate, 32);
  245. memcpy(ob + 8 + 32, work->data + 64, 12);
  246. BFwrite(fdDev, ob, 60);
  247. if (opt_debug) {
  248. s = bin2hex(ob + 8, 44);
  249. applog(LOG_DEBUG, "BitForce block data: %s", s);
  250. free(s);
  251. }
  252. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  253. if (unlikely(!pdevbuf[0])) {
  254. applog(LOG_ERR, "Error reading from BitForce (block data)");
  255. return 0;
  256. }
  257. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  258. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  259. return 0;
  260. }
  261. BFwrite(fdDev, "ZLX", 3);
  262. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  263. if (unlikely(!pdevbuf[0])) {
  264. applog(LOG_ERR, "Error reading from BitForce (ZKX)");
  265. return 0;
  266. }
  267. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  268. float temp = strtof(s + 1, NULL);
  269. if (temp > 0) {
  270. bitforce->temp = temp;
  271. if (temp > bitforce->cutofftemp) {
  272. applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
  273. bitforce->deven = DEV_RECOVER;
  274. bitforce->device_last_not_well = time(NULL);
  275. bitforce->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  276. bitforce->dev_thermal_cutoff_count++;
  277. }
  278. }
  279. }
  280. usleep(4500000);
  281. i = 4500;
  282. while (1) {
  283. BFwrite(fdDev, "ZFX", 3);
  284. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  285. if (unlikely(!pdevbuf[0])) {
  286. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  287. return 0;
  288. }
  289. if (pdevbuf[0] != 'B')
  290. break;
  291. usleep(10000);
  292. i += 10;
  293. }
  294. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  295. work->blk.nonce = 0xffffffff;
  296. if (pdevbuf[2] == '-')
  297. return 0xffffffff;
  298. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  299. applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
  300. return 0;
  301. }
  302. pnoncebuf = &pdevbuf[12];
  303. while (1) {
  304. hex2bin((void*)&nonce, pnoncebuf, 4);
  305. #ifndef __BIG_ENDIAN__
  306. nonce = swab32(nonce);
  307. #endif
  308. submit_nonce(thr, work, nonce);
  309. if (pnoncebuf[8] != ',')
  310. break;
  311. pnoncebuf += 9;
  312. }
  313. return 0xffffffff;
  314. }
  315. struct device_api bitforce_api = {
  316. .dname = "bitforce",
  317. .name = "BFL",
  318. .api_detect = bitforce_detect,
  319. .get_statline_before = get_bitforce_statline_before,
  320. .thread_prepare = bitforce_thread_prepare,
  321. .scanhash = bitforce_scanhash,
  322. };