driver-icarus.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
  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. /*
  11. * Those code should be works fine with V2 and V3 bitstream of Icarus.
  12. * Operation:
  13. * No detection implement.
  14. * Input: 64B = 32B midstate + 20B fill bytes + last 12 bytes of block head.
  15. * Return: send back 32bits immediately when Icarus found a valid nonce.
  16. * no query protocol implemented here, if no data send back in ~11.3
  17. * seconds (full cover time on 32bit nonce range by 380MH/s speed)
  18. * just send another work.
  19. * Notice:
  20. * 1. Icarus will start calculate when you push a work to them, even they
  21. * are busy.
  22. * 2. The 2 FPGAs on Icarus will distribute the job, one will calculate the
  23. * 0 ~ 7FFFFFFF, another one will cover the 80000000 ~ FFFFFFFF.
  24. * 3. It's possible for 2 FPGAs both find valid nonce in the meantime, the 2
  25. * valid nonce will all be send back.
  26. * 4. Icarus will stop work when: a valid nonce has been found or 32 bits
  27. * nonce range is completely calculated.
  28. */
  29. #include <limits.h>
  30. #include <pthread.h>
  31. #include <stdio.h>
  32. #include <sys/time.h>
  33. #include <sys/types.h>
  34. #include <dirent.h>
  35. #include <unistd.h>
  36. #ifndef WIN32
  37. #include <termios.h>
  38. #include <sys/stat.h>
  39. #include <fcntl.h>
  40. #ifndef O_CLOEXEC
  41. #define O_CLOEXEC 0
  42. #endif
  43. #else
  44. #include <windows.h>
  45. #include <io.h>
  46. #endif
  47. #ifdef HAVE_SYS_EPOLL_H
  48. #include <sys/epoll.h>
  49. #define HAVE_EPOLL
  50. #endif
  51. #include "elist.h"
  52. #include "miner.h"
  53. // 8 second timeout
  54. #define ICARUS_READ_FAULT_DECISECONDS (1)
  55. #define ICARUS_READ_FAULT_COUNT (80)
  56. struct device_api icarus_api;
  57. static void rev(unsigned char *s, size_t l)
  58. {
  59. size_t i, j;
  60. unsigned char t;
  61. for (i = 0, j = l - 1; i < j; i++, j--) {
  62. t = s[i];
  63. s[i] = s[j];
  64. s[j] = t;
  65. }
  66. }
  67. static int icarus_open(const char *devpath)
  68. {
  69. #ifndef WIN32
  70. struct termios my_termios;
  71. int serialfd = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  72. if (serialfd == -1)
  73. return -1;
  74. tcgetattr(serialfd, &my_termios);
  75. my_termios.c_cflag = B115200;
  76. my_termios.c_cflag |= CS8;
  77. my_termios.c_cflag |= CREAD;
  78. my_termios.c_cflag |= CLOCAL;
  79. my_termios.c_cflag &= ~(CSIZE | PARENB);
  80. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  81. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  82. my_termios.c_oflag &= ~OPOST;
  83. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  84. my_termios.c_cc[VTIME] = ICARUS_READ_FAULT_DECISECONDS;
  85. my_termios.c_cc[VMIN] = 0;
  86. tcsetattr(serialfd, TCSANOW, &my_termios);
  87. tcflush(serialfd, TCOFLUSH);
  88. tcflush(serialfd, TCIFLUSH);
  89. return serialfd;
  90. #else
  91. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0,
  92. NULL, OPEN_EXISTING, 0, NULL);
  93. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  94. return -1;
  95. COMMTIMEOUTS cto = {1000, 0, 1000, 0, 1000};
  96. SetCommTimeouts(hSerial, &cto);
  97. return _open_osfhandle((LONG)hSerial, 0);
  98. #endif
  99. }
  100. static int icarus_gets(unsigned char *buf, size_t bufLen, int fd, volatile unsigned long *wr)
  101. {
  102. ssize_t ret = 0;
  103. int rc = 0;
  104. int epollfd = -1;
  105. #ifdef HAVE_EPOLL
  106. struct epoll_event ev, evr;
  107. epollfd = epoll_create(1);
  108. if (epollfd != -1) {
  109. ev.events = EPOLLIN;
  110. ev.data.fd = fd;
  111. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
  112. close(epollfd);
  113. epollfd = -1;
  114. }
  115. }
  116. #endif
  117. while (bufLen) {
  118. #ifdef HAVE_EPOLL
  119. if (epollfd != -1 && epoll_wait(epollfd, &evr, 1, ICARUS_READ_FAULT_DECISECONDS * 100) != 1)
  120. ret = 0;
  121. else
  122. #endif
  123. ret = read(fd, buf, 1);
  124. if (ret == 1) {
  125. bufLen--;
  126. buf++;
  127. continue;
  128. }
  129. rc++;
  130. if (*wr)
  131. return 1;
  132. if (rc == ICARUS_READ_FAULT_COUNT) {
  133. if (epollfd != -1)
  134. close(epollfd);
  135. applog(LOG_DEBUG,
  136. "Icarus Read: No data in %d seconds", rc * ICARUS_READ_FAULT_DECISECONDS / 10);
  137. return 1;
  138. }
  139. }
  140. if (epollfd != -1)
  141. close(epollfd);
  142. return 0;
  143. }
  144. static int icarus_write(int fd, const void *buf, size_t bufLen)
  145. {
  146. size_t ret;
  147. ret = write(fd, buf, bufLen);
  148. if (unlikely(ret != bufLen))
  149. return 1;
  150. return 0;
  151. }
  152. #define icarus_close(fd) close(fd)
  153. static bool icarus_detect_one(const char *devpath)
  154. {
  155. int fd;
  156. const char golden_ob[] =
  157. "2db907f9cb4eb938ded904f4832c4331"
  158. "0380e3aeb54364057e7fec5157bfc533"
  159. "00000000000000000000000080000000"
  160. "00000000a58e091ac342724e7c3dc346";
  161. const char golden_nonce[] = "063c5e01";
  162. unsigned char ob_bin[64], nonce_bin[4];
  163. char *nonce_hex;
  164. if (total_devices == MAX_DEVICES)
  165. return false;
  166. fd = icarus_open(devpath);
  167. if (unlikely(fd == -1)) {
  168. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  169. return false;
  170. }
  171. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  172. icarus_write(fd, ob_bin, sizeof(ob_bin));
  173. memset(nonce_bin, 0, sizeof(nonce_bin));
  174. volatile unsigned long wr = 0;
  175. icarus_gets(nonce_bin, sizeof(nonce_bin), fd, &wr);
  176. icarus_close(fd);
  177. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  178. if (nonce_hex) {
  179. if (strncmp(nonce_hex, golden_nonce, 8)) {
  180. applog(LOG_ERR,
  181. "Icarus Detect: "
  182. "Test failed at %s: get %s, should: %s",
  183. devpath, nonce_hex, golden_nonce);
  184. free(nonce_hex);
  185. return false;
  186. }
  187. free(nonce_hex);
  188. } else
  189. return false;
  190. /* We have a real Icarus! */
  191. struct cgpu_info *icarus;
  192. icarus = calloc(1, sizeof(struct cgpu_info));
  193. icarus->api = &icarus_api;
  194. icarus->device_path = strdup(devpath);
  195. icarus->threads = 1;
  196. add_cgpu(icarus);
  197. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  198. devpath, icarus->device_id);
  199. return true;
  200. }
  201. static void icarus_detect()
  202. {
  203. struct string_elist *iter, *tmp;
  204. const char*s;
  205. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  206. s = iter->string;
  207. if (!strncmp("icarus:", iter->string, 7))
  208. s += 7;
  209. if (icarus_detect_one(s))
  210. string_elist_del(iter);
  211. }
  212. }
  213. static bool icarus_prepare(struct thr_info *thr)
  214. {
  215. struct cgpu_info *icarus = thr->cgpu;
  216. struct timeval now;
  217. int fd = icarus_open(icarus->device_path);
  218. if (unlikely(-1 == fd)) {
  219. applog(LOG_ERR, "Failed to open Icarus on %s",
  220. icarus->device_path);
  221. return false;
  222. }
  223. icarus->device_fd = fd;
  224. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  225. gettimeofday(&now, NULL);
  226. get_datestamp(icarus->init, &now);
  227. return true;
  228. }
  229. static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  230. __maybe_unused uint64_t max_nonce)
  231. {
  232. volatile unsigned long *wr = &work_restart[thr->id].restart;
  233. *wr = 0;
  234. struct cgpu_info *icarus;
  235. int fd;
  236. int ret;
  237. unsigned char ob_bin[64], nonce_bin[4];
  238. char *ob_hex, *nonce_hex;
  239. uint32_t nonce;
  240. uint32_t hash_count;
  241. time_t t = 0;
  242. icarus = thr->cgpu;
  243. fd = icarus->device_fd;
  244. memset(ob_bin, 0, sizeof(ob_bin));
  245. memcpy(ob_bin, work->midstate, 32);
  246. memcpy(ob_bin + 52, work->data + 64, 12);
  247. rev(ob_bin, 32);
  248. rev(ob_bin + 52, 12);
  249. #ifndef WIN32
  250. tcflush(fd, TCOFLUSH);
  251. #endif
  252. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  253. if (ret)
  254. return 0; /* This should never happen */
  255. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  256. if (ob_hex) {
  257. t = time(NULL);
  258. applog(LOG_DEBUG, "Icarus %s send: %s",
  259. icarus->device_id, ob_hex);
  260. free(ob_hex);
  261. }
  262. /* Icarus will return 8 bytes nonces or nothing */
  263. memset(nonce_bin, 0, sizeof(nonce_bin));
  264. ret = icarus_gets(nonce_bin, sizeof(nonce_bin), fd, wr);
  265. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  266. if (nonce_hex) {
  267. t = time(NULL) - t;
  268. applog(LOG_DEBUG, "Icarus %d return (elapse %d seconds): %s",
  269. icarus->device_id, t, nonce_hex);
  270. free(nonce_hex);
  271. }
  272. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  273. if (nonce == 0 && ret)
  274. return 0xffffffff;
  275. #ifndef __BIG_ENDIAN__
  276. nonce = swab32(nonce);
  277. #endif
  278. work->blk.nonce = 0xffffffff;
  279. submit_nonce(thr, work, nonce);
  280. hash_count = (nonce & 0x7fffffff);
  281. if (hash_count == 0)
  282. hash_count = 2;
  283. else {
  284. if (hash_count++ == 0x7fffffff)
  285. hash_count = 0xffffffff;
  286. else
  287. hash_count <<= 1;
  288. }
  289. return hash_count;
  290. }
  291. static void icarus_shutdown(struct thr_info *thr)
  292. {
  293. struct cgpu_info *icarus;
  294. if (thr->cgpu) {
  295. icarus = thr->cgpu;
  296. if (icarus->device_path)
  297. free(icarus->device_path);
  298. close(icarus->device_fd);
  299. devices[icarus->device_id] = NULL;
  300. free(icarus);
  301. thr->cgpu = NULL;
  302. }
  303. }
  304. struct device_api icarus_api = {
  305. .dname = "icarus",
  306. .name = "PGA",
  307. .api_detect = icarus_detect,
  308. .thread_prepare = icarus_prepare,
  309. .scanhash = icarus_scanhash,
  310. .thread_shutdown = icarus_shutdown,
  311. };