driver-bitforce.c 8.6 KB

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