bitforce.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 pdevbuf[0x100];
  80. static int i = 0;
  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. devices[total_devices++] = bitforce;
  103. bitforce->api = &bitforce_api;
  104. bitforce->device_id = i++;
  105. bitforce->device_path = strdup(devpath);
  106. bitforce->deven = DEV_ENABLED;
  107. bitforce->threads = 1;
  108. return true;
  109. }
  110. static bool bitforce_detect_auto_udev()
  111. {
  112. #ifdef HAVE_LIBUDEV
  113. struct udev *udev = udev_new();
  114. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  115. struct udev_list_entry *list_entry;
  116. bool foundany = false;
  117. udev_enumerate_add_match_subsystem(enumerate, "tty");
  118. udev_enumerate_add_match_property(enumerate, "ID_MODEL", "BitFORCE*SHA256");
  119. udev_enumerate_scan_devices(enumerate);
  120. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  121. struct udev_device *device = udev_device_new_from_syspath(
  122. udev_enumerate_get_udev(enumerate),
  123. udev_list_entry_get_name(list_entry)
  124. );
  125. if (!device)
  126. continue;
  127. const char *devpath = udev_device_get_devnode(device);
  128. if (devpath) {
  129. foundany = true;
  130. bitforce_detect_one(devpath);
  131. }
  132. udev_device_unref(device);
  133. }
  134. udev_enumerate_unref(enumerate);
  135. udev_unref(udev);
  136. return foundany;
  137. #else
  138. return false;
  139. #endif
  140. }
  141. static bool bitforce_detect_auto_devserial()
  142. {
  143. #ifndef WIN32
  144. DIR *D;
  145. struct dirent *de;
  146. const char udevdir[] = "/dev/serial/by-id";
  147. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  148. char *devfile = devpath + sizeof(udevdir);
  149. bool foundany = false;
  150. D = opendir(udevdir);
  151. if (!D)
  152. return false;
  153. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  154. devpath[sizeof(udevdir) - 1] = '/';
  155. while ( (de = readdir(D)) ) {
  156. if (!strstr(de->d_name, "BitFORCE_SHA256"))
  157. continue;
  158. foundany = true;
  159. strcpy(devfile, de->d_name);
  160. bitforce_detect_one(devpath);
  161. }
  162. closedir(D);
  163. return foundany;
  164. #else
  165. return false;
  166. #endif
  167. }
  168. static void bitforce_detect_auto()
  169. {
  170. bitforce_detect_auto_udev() ?:
  171. bitforce_detect_auto_devserial() ?:
  172. 0;
  173. }
  174. static void bitforce_detect()
  175. {
  176. struct string_elist *iter, *tmp;
  177. const char*s;
  178. bool found = false;
  179. bool autoscan = false;
  180. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  181. s = iter->string;
  182. if (!strncmp("bitforce:", iter->string, 9))
  183. s += 9;
  184. if (!strcmp(s, "auto"))
  185. autoscan = true;
  186. else if (bitforce_detect_one(s)) {
  187. string_elist_del(iter);
  188. found = true;
  189. }
  190. }
  191. if (autoscan || !found)
  192. bitforce_detect_auto();
  193. }
  194. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  195. {
  196. float gt = bitforce->temp;
  197. if (gt > 0)
  198. tailsprintf(buf, "%5.1fC ", gt);
  199. else
  200. tailsprintf(buf, " ", gt);
  201. tailsprintf(buf, " | ");
  202. }
  203. static bool bitforce_thread_prepare(struct thr_info *thr)
  204. {
  205. struct cgpu_info *bitforce = thr->cgpu;
  206. struct timeval now;
  207. int fdDev = BFopen(bitforce->device_path);
  208. if (unlikely(-1 == fdDev)) {
  209. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  210. return false;
  211. }
  212. bitforce->device_fd = fdDev;
  213. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  214. gettimeofday(&now, NULL);
  215. get_datestamp(bitforce->init, &now);
  216. return true;
  217. }
  218. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  219. {
  220. struct cgpu_info *bitforce = thr->cgpu;
  221. int fdDev = bitforce->device_fd;
  222. char pdevbuf[0x100];
  223. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  224. int i;
  225. char *pnoncebuf;
  226. char *s;
  227. uint32_t nonce;
  228. BFwrite(fdDev, "ZDX", 3);
  229. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  230. if (unlikely(!pdevbuf[0])) {
  231. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  232. return 0;
  233. }
  234. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  235. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  236. return 0;
  237. }
  238. memcpy(ob + 8, work->midstate, 32);
  239. memcpy(ob + 8 + 32, work->data + 64, 12);
  240. BFwrite(fdDev, ob, 60);
  241. if (opt_debug) {
  242. s = bin2hex(ob + 8, 44);
  243. applog(LOG_DEBUG, "BitForce block data: %s", s);
  244. free(s);
  245. }
  246. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  247. if (unlikely(!pdevbuf[0])) {
  248. applog(LOG_ERR, "Error reading from BitForce (block data)");
  249. return 0;
  250. }
  251. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  252. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  253. return 0;
  254. }
  255. BFwrite(fdDev, "ZLX", 3);
  256. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  257. if (unlikely(!pdevbuf[0])) {
  258. applog(LOG_ERR, "Error reading from BitForce (ZKX)");
  259. return 0;
  260. }
  261. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  262. float temp = strtof(s + 1, NULL);
  263. if (temp > 0) {
  264. bitforce->temp = temp;
  265. if (temp > bitforce->cutofftemp) {
  266. applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
  267. bitforce->deven = DEV_RECOVER;
  268. bitforce->device_last_not_well = time(NULL);
  269. bitforce->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  270. bitforce->dev_thermal_cutoff_count++;
  271. }
  272. }
  273. }
  274. usleep(4500000);
  275. i = 4500;
  276. while (1) {
  277. BFwrite(fdDev, "ZFX", 3);
  278. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  279. if (unlikely(!pdevbuf[0])) {
  280. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  281. return 0;
  282. }
  283. if (pdevbuf[0] != 'B')
  284. break;
  285. usleep(10000);
  286. i += 10;
  287. }
  288. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  289. work->blk.nonce = 0xffffffff;
  290. if (pdevbuf[2] == '-')
  291. return 0xffffffff;
  292. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  293. applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
  294. return 0;
  295. }
  296. pnoncebuf = &pdevbuf[12];
  297. while (1) {
  298. hex2bin((void*)&nonce, pnoncebuf, 4);
  299. #ifndef __BIG_ENDIAN__
  300. nonce = swab32(nonce);
  301. #endif
  302. submit_nonce(thr, work, nonce);
  303. if (pnoncebuf[8] != ',')
  304. break;
  305. pnoncebuf += 9;
  306. }
  307. return 0xffffffff;
  308. }
  309. struct device_api bitforce_api = {
  310. .name = "BFL",
  311. .api_detect = bitforce_detect,
  312. .get_statline_before = get_bitforce_statline_before,
  313. .thread_prepare = bitforce_thread_prepare,
  314. .scanhash = bitforce_scanhash,
  315. };