driver-icarus.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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, int read_count)
  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 >= read_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. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  157. // N.B. golden_ob MUST take less time to calculate
  158. // than the timeout set in icarus_open()
  159. // This one takes ~0.53ms on Rev3 Icarus
  160. const char golden_ob[] =
  161. "4679ba4ec99876bf4bfe086082b40025"
  162. "4df6c356451471139a3afa71e48f544a"
  163. "00000000000000000000000000000000"
  164. "0000000087320b1a1426674f2fa722ce";
  165. const char golden_nonce[] = "000187a2";
  166. unsigned char ob_bin[64], nonce_bin[4];
  167. char *nonce_hex;
  168. if (total_devices == MAX_DEVICES)
  169. return false;
  170. fd = icarus_open(devpath);
  171. if (unlikely(fd == -1)) {
  172. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  173. return false;
  174. }
  175. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  176. icarus_write(fd, ob_bin, sizeof(ob_bin));
  177. memset(nonce_bin, 0, sizeof(nonce_bin));
  178. volatile unsigned long wr = 0;
  179. icarus_gets(nonce_bin, sizeof(nonce_bin), fd, &wr, 1);
  180. icarus_close(fd);
  181. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  182. if (nonce_hex) {
  183. if (strncmp(nonce_hex, golden_nonce, 8)) {
  184. applog(LOG_ERR,
  185. "Icarus Detect: "
  186. "Test failed at %s: get %s, should: %s",
  187. devpath, nonce_hex, golden_nonce);
  188. free(nonce_hex);
  189. return false;
  190. }
  191. applog(LOG_DEBUG,
  192. "Icarus Detect: "
  193. "Test succeeded at %s: got %s",
  194. devpath, nonce_hex);
  195. free(nonce_hex);
  196. } else
  197. return false;
  198. /* We have a real Icarus! */
  199. struct cgpu_info *icarus;
  200. icarus = calloc(1, sizeof(struct cgpu_info));
  201. icarus->api = &icarus_api;
  202. icarus->device_path = strdup(devpath);
  203. icarus->threads = 1;
  204. add_cgpu(icarus);
  205. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  206. devpath, icarus->device_id);
  207. return true;
  208. }
  209. static void icarus_detect()
  210. {
  211. struct string_elist *iter, *tmp;
  212. const char*s;
  213. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  214. s = iter->string;
  215. if (!strncmp("icarus:", iter->string, 7))
  216. s += 7;
  217. if (!strcmp(s, "auto") || !strcmp(s, "noauto"))
  218. continue;
  219. if (icarus_detect_one(s))
  220. string_elist_del(iter);
  221. }
  222. }
  223. static bool icarus_prepare(struct thr_info *thr)
  224. {
  225. struct cgpu_info *icarus = thr->cgpu;
  226. struct timeval now;
  227. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  228. gettimeofday(&now, NULL);
  229. get_datestamp(icarus->init, &now);
  230. return true;
  231. }
  232. static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  233. __maybe_unused uint64_t max_nonce)
  234. {
  235. volatile unsigned long *wr = &work_restart[thr->id].restart;
  236. struct cgpu_info *icarus;
  237. int fd;
  238. int ret;
  239. unsigned char ob_bin[64], nonce_bin[4];
  240. char *ob_hex, *nonce_hex;
  241. uint32_t nonce;
  242. uint32_t hash_count;
  243. struct timeval tv_start, tv_end, diff;
  244. icarus = thr->cgpu;
  245. fd = icarus_open(icarus->device_path);
  246. if (unlikely(-1 == fd)) {
  247. applog(LOG_ERR, "Failed to open Icarus on %s",
  248. icarus->device_path);
  249. return 0;
  250. }
  251. memset(ob_bin, 0, sizeof(ob_bin));
  252. memcpy(ob_bin, work->midstate, 32);
  253. memcpy(ob_bin + 52, work->data + 64, 12);
  254. rev(ob_bin, 32);
  255. rev(ob_bin + 52, 12);
  256. #ifndef WIN32
  257. tcflush(fd, TCOFLUSH);
  258. #endif
  259. gettimeofday(&tv_start, NULL);
  260. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  261. if (ret) {
  262. icarus_close(fd);
  263. return 0; /* This should never happen */
  264. }
  265. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  266. if (ob_hex) {
  267. applog(LOG_DEBUG, "Icarus %s send: %s",
  268. icarus->device_id, ob_hex);
  269. free(ob_hex);
  270. }
  271. /* Icarus will return 8 bytes nonces or nothing */
  272. memset(nonce_bin, 0, sizeof(nonce_bin));
  273. ret = icarus_gets(nonce_bin, sizeof(nonce_bin), fd, wr,
  274. ICARUS_READ_FAULT_COUNT);
  275. gettimeofday(&tv_end, NULL);
  276. timeval_subtract(&diff, &tv_end, &tv_start);
  277. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  278. if (nonce_hex) {
  279. applog(LOG_DEBUG, "Icarus %d returned (in %d.%06d seconds): %s",
  280. icarus->device_id, diff.tv_sec, diff.tv_usec, nonce_hex);
  281. free(nonce_hex);
  282. }
  283. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  284. work->blk.nonce = 0xffffffff;
  285. icarus_close(fd);
  286. if (nonce == 0 && ret) {
  287. if (unlikely(diff.tv_sec > 12 || (diff.tv_sec == 11 && diff.tv_usec > 300067)))
  288. return 0xffffffff;
  289. // Approximately how much of the nonce Icarus scans in 1 second...
  290. // 0x16a7a561 would be if it was exactly 380 MH/s
  291. // 0x168b7b4b was the average over a 201-sample period based on time to find actual shares
  292. return (0x168b7b4b * diff.tv_sec) + (0x17a * diff.tv_usec);
  293. }
  294. #ifndef __BIG_ENDIAN__
  295. nonce = swab32(nonce);
  296. #endif
  297. submit_nonce(thr, work, nonce);
  298. hash_count = (nonce & 0x7fffffff);
  299. if (hash_count == 0)
  300. hash_count = 2;
  301. else {
  302. if (hash_count++ == 0x7fffffff)
  303. hash_count = 0xffffffff;
  304. else
  305. hash_count <<= 1;
  306. }
  307. applog(LOG_DEBUG, "0x%x hashes in %d.%06d seconds", hash_count, diff.tv_sec, diff.tv_usec);
  308. return hash_count;
  309. }
  310. static void icarus_shutdown(struct thr_info *thr)
  311. {
  312. struct cgpu_info *icarus;
  313. if (thr->cgpu) {
  314. icarus = thr->cgpu;
  315. if (icarus->device_path)
  316. free(icarus->device_path);
  317. devices[icarus->device_id] = NULL;
  318. free(icarus);
  319. thr->cgpu = NULL;
  320. }
  321. }
  322. struct device_api icarus_api = {
  323. .dname = "icarus",
  324. .name = "ICA",
  325. .api_detect = icarus_detect,
  326. .thread_prepare = icarus_prepare,
  327. .scanhash = icarus_scanhash,
  328. .thread_shutdown = icarus_shutdown,
  329. };