icarus.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include "elist.h"
  48. #include "miner.h"
  49. #define ICARUS_READ_FAULT_COUNT (8)
  50. struct device_api icarus_api;
  51. static void rev(unsigned char *s, size_t l)
  52. {
  53. size_t i, j;
  54. unsigned char t;
  55. for (i = 0, j = l - 1; i < j; i++, j--) {
  56. t = s[i];
  57. s[i] = s[j];
  58. s[j] = t;
  59. }
  60. }
  61. static int icarus_open(const char *devpath)
  62. {
  63. #ifndef WIN32
  64. struct termios my_termios;
  65. int serialfd = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  66. if (serialfd == -1)
  67. return -1;
  68. tcgetattr(serialfd, &my_termios);
  69. my_termios.c_cflag = B115200;
  70. my_termios.c_cflag |= CS8;
  71. my_termios.c_cflag |= CREAD;
  72. my_termios.c_cflag |= CLOCAL;
  73. my_termios.c_cflag &= ~(CSIZE | PARENB);
  74. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  75. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  76. my_termios.c_oflag &= ~OPOST;
  77. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  78. my_termios.c_cc[VTIME] = 10; /* block 1 second */
  79. my_termios.c_cc[VMIN] = 0;
  80. tcsetattr(serialfd, TCSANOW, &my_termios);
  81. tcflush(serialfd, TCOFLUSH);
  82. tcflush(serialfd, TCIFLUSH);
  83. return serialfd;
  84. #else
  85. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0,
  86. NULL, OPEN_EXISTING, 0, NULL);
  87. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  88. return -1;
  89. COMMTIMEOUTS cto = {1000, 0, 1000, 0, 1000};
  90. SetCommTimeouts(hSerial, &cto);
  91. return _open_osfhandle((LONG)hSerial, 0);
  92. #endif
  93. }
  94. static int icarus_gets(unsigned char *buf, size_t bufLen, int fd)
  95. {
  96. ssize_t ret = 0;
  97. int rc = 0;
  98. while (bufLen) {
  99. ret = read(fd, buf, 1);
  100. if (ret == 1) {
  101. bufLen--;
  102. buf++;
  103. continue;
  104. }
  105. rc++;
  106. if (rc == ICARUS_READ_FAULT_COUNT) {
  107. applog(LOG_DEBUG,
  108. "Icarus Read: No data in %d seconds", rc);
  109. return 1;
  110. }
  111. }
  112. return 0;
  113. }
  114. static int icarus_write(int fd, const void *buf, size_t bufLen)
  115. {
  116. size_t ret;
  117. ret = write(fd, buf, bufLen);
  118. if (unlikely(ret != bufLen))
  119. return 1;
  120. return 0;
  121. }
  122. #define icarus_close(fd) close(fd)
  123. static bool icarus_detect_one(const char *devpath)
  124. {
  125. int fd;
  126. const char golden_ob[] =
  127. "2db907f9cb4eb938ded904f4832c4331"
  128. "0380e3aeb54364057e7fec5157bfc533"
  129. "00000000000000000000000080000000"
  130. "00000000a58e091ac342724e7c3dc346";
  131. const char golden_nonce[] = "063c5e01";
  132. unsigned char ob_bin[64], nonce_bin[4];
  133. char *nonce_hex;
  134. if (total_devices == MAX_DEVICES)
  135. return false;
  136. fd = icarus_open(devpath);
  137. if (unlikely(fd == -1)) {
  138. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  139. return false;
  140. }
  141. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  142. icarus_write(fd, ob_bin, sizeof(ob_bin));
  143. memset(nonce_bin, 0, sizeof(nonce_bin));
  144. icarus_gets(nonce_bin, sizeof(nonce_bin), fd);
  145. icarus_close(fd);
  146. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  147. if (nonce_hex) {
  148. if (strncmp(nonce_hex, golden_nonce, 8)) {
  149. applog(LOG_ERR,
  150. "Icarus Detect: "
  151. "Test failed at %s: get %s, should: %s",
  152. devpath, nonce_hex, golden_nonce);
  153. free(nonce_hex);
  154. return false;
  155. }
  156. free(nonce_hex);
  157. } else
  158. return false;
  159. /* We have a real Icarus! */
  160. struct cgpu_info *icarus;
  161. icarus = calloc(1, sizeof(struct cgpu_info));
  162. icarus->api = &icarus_api;
  163. icarus->device_id = total_devices;
  164. icarus->device_path = strdup(devpath);
  165. icarus->threads = 1;
  166. devices[total_devices++] = icarus;
  167. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  168. devpath, icarus->device_id);
  169. return true;
  170. }
  171. static void icarus_detect()
  172. {
  173. struct string_elist *iter, *tmp;
  174. const char*s;
  175. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  176. s = iter->string;
  177. if (!strncmp("icarus:", iter->string, 7))
  178. s += 7;
  179. if (icarus_detect_one(s))
  180. string_elist_del(iter);
  181. }
  182. }
  183. static bool icarus_prepare(struct thr_info *thr)
  184. {
  185. struct cgpu_info *icarus = thr->cgpu;
  186. struct timeval now;
  187. int fd = icarus_open(icarus->device_path);
  188. if (unlikely(-1 == fd)) {
  189. applog(LOG_ERR, "Failed to open Icarus on %s",
  190. icarus->device_path);
  191. return false;
  192. }
  193. icarus->device_fd = fd;
  194. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  195. gettimeofday(&now, NULL);
  196. get_datestamp(icarus->init, &now);
  197. return true;
  198. }
  199. static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  200. __maybe_unused uint64_t max_nonce)
  201. {
  202. struct cgpu_info *icarus;
  203. int fd;
  204. int ret;
  205. unsigned char ob_bin[64], nonce_bin[4];
  206. char *ob_hex, *nonce_hex;
  207. uint32_t nonce;
  208. uint32_t hash_count;
  209. time_t t = 0;
  210. icarus = thr->cgpu;
  211. fd = icarus->device_fd;
  212. memset(ob_bin, 0, sizeof(ob_bin));
  213. memcpy(ob_bin, work->midstate, 32);
  214. memcpy(ob_bin + 52, work->data + 64, 12);
  215. rev(ob_bin, 32);
  216. rev(ob_bin + 52, 12);
  217. #ifndef WIN32
  218. tcflush(fd, TCOFLUSH);
  219. #endif
  220. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  221. if (ret)
  222. return 0; /* This should never happen */
  223. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  224. if (ob_hex) {
  225. t = time(NULL);
  226. applog(LOG_DEBUG, "Icarus %s send: %s",
  227. icarus->device_id, ob_hex);
  228. free(ob_hex);
  229. }
  230. /* Icarus will return 8 bytes nonces or nothing */
  231. memset(nonce_bin, 0, sizeof(nonce_bin));
  232. ret = icarus_gets(nonce_bin, sizeof(nonce_bin), fd);
  233. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  234. if (nonce_hex) {
  235. t = time(NULL) - t;
  236. applog(LOG_DEBUG, "Icarus %d return (elapse %d seconds): %s",
  237. icarus->device_id, t, nonce_hex);
  238. free(nonce_hex);
  239. }
  240. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  241. if (nonce == 0 && ret)
  242. return 0xffffffff;
  243. #ifndef __BIG_ENDIAN__
  244. nonce = swab32(nonce);
  245. #endif
  246. work->blk.nonce = 0xffffffff;
  247. submit_nonce(thr, work, nonce);
  248. hash_count = (nonce & 0x7fffffff);
  249. if (hash_count == 0)
  250. hash_count = 2;
  251. else {
  252. if (hash_count++ == 0x7fffffff)
  253. hash_count = 0xffffffff;
  254. else
  255. hash_count <<= 1;
  256. }
  257. return hash_count;
  258. }
  259. static void icarus_shutdown(struct thr_info *thr)
  260. {
  261. struct cgpu_info *icarus;
  262. if (thr->cgpu) {
  263. icarus = thr->cgpu;
  264. if (icarus->device_path)
  265. free(icarus->device_path);
  266. close(icarus->device_fd);
  267. devices[icarus->device_id] = NULL;
  268. free(icarus);
  269. thr->cgpu = NULL;
  270. }
  271. }
  272. struct device_api icarus_api = {
  273. .name = "ICA",
  274. .api_detect = icarus_detect,
  275. .thread_prepare = icarus_prepare,
  276. .scanhash = icarus_scanhash,
  277. .thread_shutdown = icarus_shutdown,
  278. };