driver-icarus.c 10 KB

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