bitforce.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. bool found = false;
  178. bool autoscan = false;
  179. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  180. if (!strcmp(iter->string, "auto"))
  181. autoscan = true;
  182. else if (bitforce_detect_one(iter->string)) {
  183. string_elist_del(iter);
  184. found = true;
  185. }
  186. }
  187. if (autoscan || !found)
  188. bitforce_detect_auto();
  189. }
  190. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  191. {
  192. float gt = bitforce->temp;
  193. if (gt > 0)
  194. tailsprintf(buf, "%5.1fC ", gt);
  195. else
  196. tailsprintf(buf, " ", gt);
  197. tailsprintf(buf, " | ");
  198. }
  199. static bool bitforce_thread_prepare(struct thr_info *thr)
  200. {
  201. struct cgpu_info *bitforce = thr->cgpu;
  202. struct timeval now;
  203. int fdDev = BFopen(bitforce->device_path);
  204. if (unlikely(-1 == fdDev)) {
  205. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  206. return false;
  207. }
  208. bitforce->device_fd = fdDev;
  209. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  210. gettimeofday(&now, NULL);
  211. get_datestamp(bitforce->init, &now);
  212. return true;
  213. }
  214. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  215. {
  216. struct cgpu_info *bitforce = thr->cgpu;
  217. int fdDev = bitforce->device_fd;
  218. char pdevbuf[0x100];
  219. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  220. int i;
  221. char *pnoncebuf;
  222. char *s;
  223. uint32_t nonce;
  224. BFwrite(fdDev, "ZDX", 3);
  225. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  226. if (unlikely(!pdevbuf[0])) {
  227. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  228. return 0;
  229. }
  230. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  231. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  232. return 0;
  233. }
  234. memcpy(ob + 8, work->midstate, 32);
  235. memcpy(ob + 8 + 32, work->data + 64, 12);
  236. BFwrite(fdDev, ob, 60);
  237. if (opt_debug) {
  238. s = bin2hex(ob + 8, 44);
  239. applog(LOG_DEBUG, "BitForce block data: %s", s);
  240. free(s);
  241. }
  242. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  243. if (unlikely(!pdevbuf[0])) {
  244. applog(LOG_ERR, "Error reading from BitForce (block data)");
  245. return 0;
  246. }
  247. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  248. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  249. return 0;
  250. }
  251. BFwrite(fdDev, "ZLX", 3);
  252. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  253. if (unlikely(!pdevbuf[0])) {
  254. applog(LOG_ERR, "Error reading from BitForce (ZKX)");
  255. return 0;
  256. }
  257. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  258. float temp = strtof(s + 1, NULL);
  259. if (temp > 0) {
  260. bitforce->temp = temp;
  261. if (temp > bitforce->cutofftemp) {
  262. applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
  263. bitforce->deven = DEV_RECOVER;
  264. bitforce->device_last_not_well = time(NULL);
  265. bitforce->device_not_well_reason = REASON_THERMAL_CUTOFF;
  266. bitforce->dev_thermal_cutoff_count++;
  267. }
  268. }
  269. }
  270. usleep(4500000);
  271. i = 4500;
  272. while (1) {
  273. BFwrite(fdDev, "ZFX", 3);
  274. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  275. if (unlikely(!pdevbuf[0])) {
  276. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  277. return 0;
  278. }
  279. if (pdevbuf[0] != 'B')
  280. break;
  281. usleep(10000);
  282. i += 10;
  283. }
  284. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  285. work->blk.nonce = 0xffffffff;
  286. if (pdevbuf[2] == '-')
  287. return 0xffffffff;
  288. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  289. applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
  290. return 0;
  291. }
  292. pnoncebuf = &pdevbuf[12];
  293. while (1) {
  294. hex2bin((void*)&nonce, pnoncebuf, 4);
  295. #ifndef __BIG_ENDIAN__
  296. nonce = swab32(nonce);
  297. #endif
  298. submit_nonce(thr, work, nonce);
  299. if (pnoncebuf[8] != ',')
  300. break;
  301. pnoncebuf += 9;
  302. }
  303. return 0xffffffff;
  304. }
  305. struct device_api bitforce_api = {
  306. .name = "BFL",
  307. .api_detect = bitforce_detect,
  308. .get_statline_before = get_bitforce_statline_before,
  309. .thread_prepare = bitforce_thread_prepare,
  310. .scanhash = bitforce_scanhash,
  311. };