bitforce.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include <limits.h>
  10. #include <pthread.h>
  11. #include <stdio.h>
  12. #include <strings.h>
  13. #include <sys/time.h>
  14. #include <sys/types.h>
  15. #include <dirent.h>
  16. #ifndef WIN32
  17. #include <termios.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #ifndef O_CLOEXEC
  21. #define O_CLOEXEC 0
  22. #endif
  23. #else
  24. #include <windows.h>
  25. #include <io.h>
  26. #endif
  27. #include <unistd.h>
  28. #include "elist.h"
  29. #include "miner.h"
  30. struct device_api bitforce_api;
  31. #ifdef WIN32
  32. static int BFopen(const char *devpath)
  33. {
  34. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  35. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  36. return -1;
  37. return _open_osfhandle((LONG)hSerial, 0);
  38. }
  39. #else
  40. static int BFopen(const char *devpath)
  41. {
  42. return open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  43. }
  44. #endif
  45. static void BFgets(char *buf, size_t bufLen, int fd)
  46. {
  47. do
  48. --bufLen;
  49. while (likely(bufLen && read(fd, buf, 1) && (buf++)[0] != '\n'))
  50. ;
  51. buf[0] = '\0';
  52. }
  53. static void BFwrite(int fd, const void *buf, ssize_t bufLen)
  54. {
  55. ssize_t ret = write(fd, buf, bufLen);
  56. if (unlikely(ret != bufLen))
  57. quit(1, "BFwrite failed");
  58. }
  59. #define BFclose(fd) close(fd)
  60. static bool bitforce_detect_one(const char *devpath)
  61. {
  62. char pdevbuf[0x100];
  63. static int i = 0;
  64. if (total_devices == MAX_DEVICES)
  65. return false;
  66. int fdDev = BFopen(devpath);
  67. if (unlikely(fdDev == -1))
  68. {
  69. applog(LOG_DEBUG, "BitForce Detect: Failed to open %s", devpath);
  70. return false;
  71. }
  72. BFwrite(fdDev, "ZGX", 3);
  73. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  74. if (unlikely(!pdevbuf[0]))
  75. {
  76. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  77. return 0;
  78. }
  79. BFclose(fdDev);
  80. if (unlikely(!strstr(pdevbuf, "SHA256")))
  81. {
  82. applog(LOG_DEBUG, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
  83. return false;
  84. }
  85. // We have a real BitForce!
  86. struct cgpu_info *bitforce;
  87. bitforce = calloc(1, sizeof(*bitforce));
  88. devices[total_devices++] = bitforce;
  89. bitforce->api = &bitforce_api;
  90. bitforce->device_id = i++;
  91. bitforce->device_path = strdup(devpath);
  92. bitforce->enabled = true;
  93. bitforce->threads = 1;
  94. return true;
  95. }
  96. static void bitforce_detect_auto()
  97. {
  98. #ifndef WIN32
  99. DIR *D;
  100. struct dirent *de;
  101. const char udevdir[] = "/dev/serial/by-id";
  102. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  103. char *devfile = devpath + sizeof(udevdir);
  104. D = opendir(udevdir);
  105. if (!D)
  106. return;
  107. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  108. devpath[sizeof(udevdir) - 1] = '/';
  109. while ( (de = readdir(D)) ) {
  110. if (!strstr(de->d_name, "BitFORCE_SHA256"))
  111. continue;
  112. strcpy(devfile, de->d_name);
  113. bitforce_detect_one(devpath);
  114. }
  115. closedir(D);
  116. #endif
  117. }
  118. static void bitforce_detect()
  119. {
  120. struct string_elist *iter, *tmp;
  121. bool found = false;
  122. bool autoscan = false;
  123. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  124. if (!strcmp(iter->string, "auto"))
  125. autoscan = true;
  126. else
  127. if (bitforce_detect_one(iter->string))
  128. {
  129. string_elist_del(iter);
  130. found = true;
  131. }
  132. }
  133. if (autoscan || !found)
  134. bitforce_detect_auto();
  135. }
  136. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  137. {
  138. float gt = bitforce->temp;
  139. if (gt > 0)
  140. tailsprintf(buf, "%5.1fC ", gt);
  141. else
  142. tailsprintf(buf, " ", gt);
  143. tailsprintf(buf, " | ");
  144. }
  145. static bool bitforce_thread_prepare(struct thr_info *thr)
  146. {
  147. struct cgpu_info *bitforce = thr->cgpu;
  148. struct timeval now;
  149. int fdDev = BFopen(bitforce->device_path);
  150. if (unlikely(-1 == fdDev))
  151. {
  152. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  153. return false;
  154. }
  155. #ifndef WIN32
  156. {
  157. struct termios pattr;
  158. tcgetattr(fdDev, &pattr);
  159. pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  160. pattr.c_oflag &= ~OPOST;
  161. pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  162. pattr.c_cflag &= ~(CSIZE | PARENB);
  163. pattr.c_cflag |= CS8;
  164. tcsetattr(fdDev, TCSANOW, &pattr);
  165. }
  166. #endif
  167. bitforce->device_fd = fdDev;
  168. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  169. gettimeofday(&now, NULL);
  170. get_datestamp(bitforce->init, &now);
  171. return true;
  172. }
  173. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  174. {
  175. struct cgpu_info *bitforce = thr->cgpu;
  176. int fdDev = bitforce->device_fd;
  177. char pdevbuf[0x100];
  178. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  179. int i;
  180. char *pnoncebuf;
  181. uint32_t nonce;
  182. BFwrite(fdDev, "ZDX", 3);
  183. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  184. if (unlikely(!pdevbuf[0])) {
  185. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  186. return 0;
  187. }
  188. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K'))
  189. {
  190. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  191. return 0;
  192. }
  193. memcpy(ob + 8, work->midstate, 32);
  194. memcpy(ob + 8 + 32, work->data + 64, 12);
  195. BFwrite(fdDev, ob, 60);
  196. applog(LOG_DEBUG, "BitForce block data: %s", bin2hex(ob + 8, 44));
  197. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  198. if (unlikely(!pdevbuf[0]))
  199. {
  200. applog(LOG_ERR, "Error reading from BitForce (block data)");
  201. return 0;
  202. }
  203. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K'))
  204. {
  205. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  206. return 0;
  207. }
  208. BFwrite(fdDev, "ZKX", 3);
  209. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  210. if (unlikely(!pdevbuf[0])) {
  211. applog(LOG_ERR, "Error reading from BitForce (ZKX)");
  212. return 0;
  213. }
  214. if (!strncasecmp(pdevbuf, "TEMP:", 5)) {
  215. float temp = strtof(pdevbuf + 5, NULL);
  216. if (temp > 0) {
  217. bitforce->temp = temp;
  218. if (temp > bitforce->cutofftemp) {
  219. applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
  220. bitforce->enabled = false;
  221. }
  222. }
  223. }
  224. usleep(4500000);
  225. i = 4500;
  226. while (1) {
  227. BFwrite(fdDev, "ZFX", 3);
  228. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  229. if (unlikely(!pdevbuf[0]))
  230. {
  231. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  232. return 0;
  233. }
  234. if (pdevbuf[0] != 'B')
  235. break;
  236. usleep(10000);
  237. i += 10;
  238. }
  239. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  240. work->blk.nonce = 0xffffffff;
  241. if (pdevbuf[2] == '-')
  242. return 0xffffffff;
  243. else
  244. if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  245. applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
  246. return 0;
  247. }
  248. pnoncebuf = &pdevbuf[12];
  249. while (1) {
  250. hex2bin((void*)&nonce, pnoncebuf, 4);
  251. #ifndef __BIG_ENDIAN__
  252. nonce = swab32(nonce);
  253. #endif
  254. submit_nonce(thr, work, nonce);
  255. if (pnoncebuf[8] != ',')
  256. break;
  257. pnoncebuf += 9;
  258. }
  259. return 0xffffffff;
  260. }
  261. struct device_api bitforce_api = {
  262. .name = "BFL",
  263. .api_detect = bitforce_detect,
  264. // .reinit_device = TODO
  265. .get_statline_before = get_bitforce_statline_before,
  266. .thread_prepare = bitforce_thread_prepare,
  267. .scanhash = bitforce_scanhash,
  268. };