icarus.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. /* TODO: Needs setup read block time. just like VTIME = 10 */
  90. return _open_osfhandle((LONG)hSerial, 0);
  91. #endif
  92. }
  93. static int icarus_gets(unsigned char *buf, size_t bufLen, int fd)
  94. {
  95. ssize_t ret = 0;
  96. int rc = 0;
  97. while (bufLen) {
  98. ret = read(fd, buf, 1);
  99. if (ret == 1) {
  100. bufLen--;
  101. buf++;
  102. continue;
  103. }
  104. rc++;
  105. if (rc == ICARUS_READ_FAULT_COUNT) {
  106. applog(LOG_WARNING,
  107. "Icarus Read: No data in %d seconds", rc);
  108. return 1;
  109. }
  110. }
  111. return 0;
  112. }
  113. static int icarus_write(int fd, const void *buf, size_t bufLen)
  114. {
  115. size_t ret;
  116. ret = write(fd, buf, bufLen);
  117. if (unlikely(ret != bufLen))
  118. return 1;
  119. return 0;
  120. }
  121. #define icarus_close(fd) close(fd)
  122. static bool icarus_detect_one(const char *devpath)
  123. {
  124. int fd;
  125. const char golden_ob[] =
  126. "2db907f9cb4eb938ded904f4832c4331"
  127. "0380e3aeb54364057e7fec5157bfc533"
  128. "00000000000000000000000080000000"
  129. "00000000a58e091ac342724e7c3dc346";
  130. const char golden_nonce[] = "063c5e01";
  131. unsigned char ob_bin[64], nonce_bin[4];
  132. char *nonce_hex;
  133. if (total_devices == MAX_DEVICES)
  134. return false;
  135. fd = icarus_open(devpath);
  136. if (unlikely(fd == -1)) {
  137. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  138. return false;
  139. }
  140. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  141. icarus_write(fd, ob_bin, sizeof(ob_bin));
  142. memset(nonce_bin, 0, sizeof(nonce_bin));
  143. icarus_gets(nonce_bin, sizeof(nonce_bin), fd);
  144. icarus_close(fd);
  145. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  146. if (nonce_hex) {
  147. if (strncmp(nonce_hex, golden_nonce, 8)) {
  148. applog(LOG_ERR,
  149. "Icarus Detect: "
  150. "Test failed at %s: get %s, should: %s",
  151. devpath, nonce_hex, golden_nonce);
  152. free(nonce_hex);
  153. return false;
  154. }
  155. free(nonce_hex);
  156. } else
  157. return false;
  158. /* We have a real Icarus! */
  159. struct cgpu_info *icarus;
  160. icarus = calloc(1, sizeof(struct cgpu_info));
  161. icarus->api = &icarus_api;
  162. icarus->device_id = total_devices;
  163. icarus->device_path = strdup(devpath);
  164. icarus->threads = 1;
  165. devices[total_devices++] = icarus;
  166. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  167. devpath, icarus->device_id);
  168. return true;
  169. }
  170. static void icarus_detect()
  171. {
  172. struct string_elist *iter, *tmp;
  173. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  174. if (icarus_detect_one(iter->string))
  175. string_elist_del(iter);
  176. }
  177. }
  178. static bool icarus_prepare(struct thr_info *thr)
  179. {
  180. struct cgpu_info *icarus = thr->cgpu;
  181. struct timeval now;
  182. int fd = icarus_open(icarus->device_path);
  183. if (unlikely(-1 == fd)) {
  184. applog(LOG_ERR, "Failed to open Icarus on %s",
  185. icarus->device_path);
  186. return false;
  187. }
  188. icarus->device_fd = fd;
  189. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  190. gettimeofday(&now, NULL);
  191. get_datestamp(icarus->init, &now);
  192. return true;
  193. }
  194. static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  195. __maybe_unused uint64_t max_nonce)
  196. {
  197. struct cgpu_info *icarus;
  198. int fd;
  199. int ret;
  200. unsigned char ob_bin[64], nonce_bin[4];
  201. char *ob_hex, *nonce_hex;
  202. uint32_t nonce;
  203. uint32_t hash_count;
  204. time_t t = 0;
  205. icarus = thr->cgpu;
  206. fd = icarus->device_fd;
  207. memset(ob_bin, 0, sizeof(ob_bin));
  208. memcpy(ob_bin, work->midstate, 32);
  209. memcpy(ob_bin + 52, work->data + 64, 12);
  210. rev(ob_bin, 32);
  211. rev(ob_bin + 52, 12);
  212. #ifndef WIN32
  213. tcflush(fd, TCOFLUSH);
  214. #endif
  215. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  216. if (ret)
  217. return 0; /* This should never happen */
  218. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  219. if (ob_hex) {
  220. t = time(NULL);
  221. applog(LOG_DEBUG, "Icarus %s send: %s",
  222. icarus->device_id, ob_hex);
  223. free(ob_hex);
  224. }
  225. /* Icarus will return 8 bytes nonces or nothing */
  226. memset(nonce_bin, 0, sizeof(nonce_bin));
  227. ret = icarus_gets(nonce_bin, sizeof(nonce_bin), fd);
  228. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  229. if (nonce_hex) {
  230. t = time(NULL) - t;
  231. applog(LOG_DEBUG, "Icarus %d return (elapse %d seconds): %s",
  232. icarus->device_id, t, nonce_hex);
  233. free(nonce_hex);
  234. }
  235. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  236. if (nonce == 0 && ret)
  237. return 0xffffffff;
  238. #ifndef __BIG_ENDIAN__
  239. nonce = swab32(nonce);
  240. #endif
  241. work->blk.nonce = 0xffffffff;
  242. submit_nonce(thr, work, nonce);
  243. hash_count = (nonce & 0x7fffffff);
  244. if (hash_count == 0)
  245. hash_count = 2;
  246. else {
  247. if (hash_count++ == 0x7fffffff)
  248. hash_count = 0xffffffff;
  249. else
  250. hash_count <<= 1;
  251. }
  252. return hash_count;
  253. }
  254. static void icarus_shutdown(struct thr_info *thr)
  255. {
  256. struct cgpu_info *icarus;
  257. if (thr->cgpu) {
  258. icarus = thr->cgpu;
  259. if (icarus->device_path)
  260. free(icarus->device_path);
  261. close(icarus->device_fd);
  262. devices[icarus->device_id] = NULL;
  263. free(icarus);
  264. thr->cgpu = NULL;
  265. }
  266. }
  267. struct device_api icarus_api = {
  268. .name = "ICA",
  269. .api_detect = icarus_detect,
  270. .thread_prepare = icarus_prepare,
  271. .scanhash = icarus_scanhash,
  272. .thread_shutdown = icarus_shutdown,
  273. };