driver-icarus.c 9.2 KB

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