driver-icarus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. COMMCONFIG comCfg;
  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. // thanks to af_newbie for pointers about this
  97. memset(&comCfg, 0 , sizeof(comCfg));
  98. comCfg.dwSize = sizeof(COMMCONFIG);
  99. comCfg.wVersion = 1;
  100. comCfg.dcb.DCBlength = sizeof(DCB);
  101. comCfg.dcb.BaudRate = 115200;
  102. comCfg.dcb.fBinary = 1;
  103. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  104. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  105. comCfg.dcb.ByteSize = 8;
  106. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  107. COMMTIMEOUTS cto = {1000, 0, 1000, 0, 1000};
  108. SetCommTimeouts(hSerial, &cto);
  109. return _open_osfhandle((LONG)hSerial, 0);
  110. #endif
  111. }
  112. static int icarus_gets(unsigned char *buf, size_t bufLen, int fd, volatile unsigned long *wr, int read_count)
  113. {
  114. ssize_t ret = 0;
  115. int rc = 0;
  116. int epollfd = -1;
  117. #ifdef HAVE_EPOLL
  118. struct epoll_event ev, evr;
  119. epollfd = epoll_create(1);
  120. if (epollfd != -1) {
  121. ev.events = EPOLLIN;
  122. ev.data.fd = fd;
  123. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
  124. close(epollfd);
  125. epollfd = -1;
  126. }
  127. }
  128. #endif
  129. while (bufLen) {
  130. #ifdef HAVE_EPOLL
  131. if (epollfd != -1 && epoll_wait(epollfd, &evr, 1, ICARUS_READ_FAULT_DECISECONDS * 100) != 1)
  132. ret = 0;
  133. else
  134. #endif
  135. ret = read(fd, buf, 1);
  136. if (ret == 1) {
  137. bufLen--;
  138. buf++;
  139. continue;
  140. }
  141. rc++;
  142. if (*wr) {
  143. rc *= ICARUS_READ_FAULT_DECISECONDS;
  144. applog(LOG_DEBUG,
  145. "Icarus Read: Work restart at %d.%d seconds", rc / 10, rc % 10);
  146. return 1;
  147. }
  148. if (rc >= read_count) {
  149. if (epollfd != -1)
  150. close(epollfd);
  151. rc *= ICARUS_READ_FAULT_DECISECONDS;
  152. applog(LOG_DEBUG,
  153. "Icarus Read: No data in %d.%d seconds", rc / 10, rc % 10);
  154. return 1;
  155. }
  156. }
  157. if (epollfd != -1)
  158. close(epollfd);
  159. return 0;
  160. }
  161. static int icarus_write(int fd, const void *buf, size_t bufLen)
  162. {
  163. size_t ret;
  164. ret = write(fd, buf, bufLen);
  165. if (unlikely(ret != bufLen))
  166. return 1;
  167. return 0;
  168. }
  169. #define icarus_close(fd) close(fd)
  170. static bool icarus_detect_one(const char *devpath)
  171. {
  172. int fd;
  173. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  174. // N.B. golden_ob MUST take less time to calculate
  175. // than the timeout set in icarus_open()
  176. // This one takes ~0.53ms on Rev3 Icarus
  177. const char golden_ob[] =
  178. "4679ba4ec99876bf4bfe086082b40025"
  179. "4df6c356451471139a3afa71e48f544a"
  180. "00000000000000000000000000000000"
  181. "0000000087320b1a1426674f2fa722ce";
  182. const char golden_nonce[] = "000187a2";
  183. unsigned char ob_bin[64], nonce_bin[4];
  184. char *nonce_hex;
  185. if (total_devices == MAX_DEVICES)
  186. return false;
  187. fd = icarus_open(devpath);
  188. if (unlikely(fd == -1)) {
  189. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  190. return false;
  191. }
  192. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  193. icarus_write(fd, ob_bin, sizeof(ob_bin));
  194. memset(nonce_bin, 0, sizeof(nonce_bin));
  195. volatile unsigned long wr = 0;
  196. icarus_gets(nonce_bin, sizeof(nonce_bin), fd, &wr, 1);
  197. icarus_close(fd);
  198. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  199. if (nonce_hex) {
  200. if (strncmp(nonce_hex, golden_nonce, 8)) {
  201. applog(LOG_ERR,
  202. "Icarus Detect: "
  203. "Test failed at %s: get %s, should: %s",
  204. devpath, nonce_hex, golden_nonce);
  205. free(nonce_hex);
  206. return false;
  207. }
  208. applog(LOG_DEBUG,
  209. "Icarus Detect: "
  210. "Test succeeded at %s: got %s",
  211. devpath, nonce_hex);
  212. free(nonce_hex);
  213. } else
  214. return false;
  215. /* We have a real Icarus! */
  216. struct cgpu_info *icarus;
  217. icarus = calloc(1, sizeof(struct cgpu_info));
  218. icarus->api = &icarus_api;
  219. icarus->device_path = strdup(devpath);
  220. icarus->threads = 1;
  221. add_cgpu(icarus);
  222. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  223. devpath, icarus->device_id);
  224. return true;
  225. }
  226. static void icarus_detect()
  227. {
  228. struct string_elist *iter, *tmp;
  229. const char*s;
  230. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  231. s = iter->string;
  232. if (!strncmp("icarus:", iter->string, 7))
  233. s += 7;
  234. if (!strcmp(s, "auto") || !strcmp(s, "noauto"))
  235. continue;
  236. if (icarus_detect_one(s))
  237. string_elist_del(iter);
  238. }
  239. }
  240. static bool icarus_prepare(struct thr_info *thr)
  241. {
  242. struct cgpu_info *icarus = thr->cgpu;
  243. struct timeval now;
  244. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  245. gettimeofday(&now, NULL);
  246. get_datestamp(icarus->init, &now);
  247. return true;
  248. }
  249. static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  250. __maybe_unused uint64_t max_nonce)
  251. {
  252. volatile unsigned long *wr = &work_restart[thr->id].restart;
  253. struct cgpu_info *icarus;
  254. int fd;
  255. int ret;
  256. unsigned char ob_bin[64], nonce_bin[4];
  257. char *ob_hex, *nonce_hex;
  258. uint32_t nonce;
  259. uint32_t hash_count;
  260. struct timeval tv_start, tv_end, elapsed;
  261. icarus = thr->cgpu;
  262. fd = icarus_open(icarus->device_path);
  263. if (unlikely(-1 == fd)) {
  264. applog(LOG_ERR, "Failed to open Icarus on %s",
  265. icarus->device_path);
  266. return 0;
  267. }
  268. memset(ob_bin, 0, sizeof(ob_bin));
  269. memcpy(ob_bin, work->midstate, 32);
  270. memcpy(ob_bin + 52, work->data + 64, 12);
  271. rev(ob_bin, 32);
  272. rev(ob_bin + 52, 12);
  273. #ifndef WIN32
  274. tcflush(fd, TCOFLUSH);
  275. #endif
  276. gettimeofday(&tv_start, NULL);
  277. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  278. if (ret) {
  279. icarus_close(fd);
  280. return 0; /* This should never happen */
  281. }
  282. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  283. if (ob_hex) {
  284. applog(LOG_DEBUG, "Icarus %d sent: %s",
  285. icarus->device_id, ob_hex);
  286. free(ob_hex);
  287. }
  288. /* Icarus will return 8 bytes nonces or nothing */
  289. memset(nonce_bin, 0, sizeof(nonce_bin));
  290. ret = icarus_gets(nonce_bin, sizeof(nonce_bin), fd, wr,
  291. ICARUS_READ_FAULT_COUNT);
  292. gettimeofday(&tv_end, NULL);
  293. timeval_subtract(&elapsed, &tv_end, &tv_start);
  294. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  295. work->blk.nonce = 0xffffffff;
  296. icarus_close(fd);
  297. // aborted before becoming idle, get new work
  298. if (nonce == 0 && ret) {
  299. uint32_t ESTIMATE_HASHES;
  300. if (unlikely(elapsed.tv_sec > 12 || (elapsed.tv_sec == 11 && elapsed.tv_usec > 300067)))
  301. ESTIMATE_HASHES = 0xffffffff;
  302. else
  303. // Approximately how much of the nonce Icarus scans in 1 second...
  304. // 0x16a7a561 would be if it was exactly 380 MH/s
  305. // 0x168b7b4b was the average over a 201-sample period based on time to find actual shares
  306. ESTIMATE_HASHES = (0x168b7b4b * elapsed.tv_sec) + (0x17a * elapsed.tv_usec);
  307. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08x hashes (%ld.%06lds)",
  308. icarus->device_id, ESTIMATE_HASHES, elapsed.tv_sec, elapsed.tv_usec);
  309. return ESTIMATE_HASHES;
  310. }
  311. #ifndef __BIG_ENDIAN__
  312. nonce = swab32(nonce);
  313. #endif
  314. submit_nonce(thr, work, nonce);
  315. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  316. if (nonce_hex) {
  317. applog(LOG_DEBUG, "Icarus %d returned (elapsed %ld.%06ld seconds): %s",
  318. icarus->device_id, elapsed.tv_sec, elapsed.tv_usec, nonce_hex);
  319. free(nonce_hex);
  320. }
  321. hash_count = (nonce & 0x7fffffff);
  322. if (hash_count == 0)
  323. hash_count = 2;
  324. else {
  325. if (hash_count++ == 0x7fffffff)
  326. hash_count = 0xffffffff;
  327. else
  328. hash_count <<= 1;
  329. }
  330. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08x hashes (%ld.%06lds)",
  331. icarus->device_id, nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  332. return hash_count;
  333. }
  334. static void icarus_shutdown(struct thr_info *thr)
  335. {
  336. struct cgpu_info *icarus;
  337. if (thr->cgpu) {
  338. icarus = thr->cgpu;
  339. if (icarus->device_path)
  340. free(icarus->device_path);
  341. devices[icarus->device_id] = NULL;
  342. free(icarus);
  343. thr->cgpu = NULL;
  344. }
  345. }
  346. struct device_api icarus_api = {
  347. .dname = "icarus",
  348. .name = "ICA",
  349. .api_detect = icarus_detect,
  350. .thread_prepare = icarus_prepare,
  351. .scanhash = icarus_scanhash,
  352. .thread_shutdown = icarus_shutdown,
  353. };