driver-bitforce.c 9.5 KB

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