icarus.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. static int icarus_read_count;
  51. static int icarus_write_fault;
  52. struct device_api icarus_api;
  53. static void rev(unsigned char *s, size_t l)
  54. {
  55. size_t i, j;
  56. unsigned char t;
  57. for (i = 0, j = l - 1; i < j; i++, j--) {
  58. t = s[i];
  59. s[i] = s[j];
  60. s[j] = t;
  61. }
  62. }
  63. static int icarus_open(const char *devpath)
  64. {
  65. #ifndef WIN32
  66. struct termios my_termios;
  67. int serialfd = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  68. if (serialfd == -1)
  69. return -1;
  70. tcgetattr(serialfd, &my_termios);
  71. my_termios.c_cflag = B115200;
  72. my_termios.c_cflag |= CS8;
  73. my_termios.c_cflag |= CREAD;
  74. my_termios.c_cflag |= CLOCAL;
  75. my_termios.c_cflag &= ~(CSIZE | PARENB);
  76. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  77. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  78. my_termios.c_oflag &= ~OPOST;
  79. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  80. my_termios.c_cc[VTIME] = 10; /* block 1 second */
  81. my_termios.c_cc[VMIN] = 0;
  82. tcsetattr(serialfd, TCSANOW, &my_termios);
  83. tcflush(serialfd, TCOFLUSH);
  84. tcflush(serialfd, TCIFLUSH);
  85. return serialfd;
  86. #else
  87. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0,
  88. NULL, OPEN_EXISTING, 0, NULL);
  89. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  90. return -1;
  91. /* TODO: Needs setup read block time. just like VTIME = 10 */
  92. return _open_osfhandle((LONG)hSerial, 0);
  93. #endif
  94. }
  95. static void icarus_gets(char *buf, size_t bufLen, int fd)
  96. {
  97. ssize_t ret = 0;
  98. icarus_read_count = 0;
  99. while (bufLen) {
  100. ret = read(fd, buf, 1);
  101. if (ret == 1) {
  102. bufLen--;
  103. buf++;
  104. continue;
  105. }
  106. icarus_read_count++;
  107. if (icarus_read_count == ICARUS_READ_FAULT_COUNT) {
  108. applog(LOG_WARNING,
  109. "Icarus Read: No data in %d seconds",
  110. ICARUS_READ_FAULT_COUNT);
  111. break;
  112. }
  113. }
  114. }
  115. static void icarus_write(int fd, const void *buf, size_t bufLen)
  116. {
  117. ssize_t ret;
  118. ret = write(fd, buf, bufLen);
  119. if (unlikely(ret != bufLen))
  120. icarus_write_fault = 1;
  121. }
  122. #define icarus_close(fd) close(fd)
  123. static bool icarus_detect_one(const char *devpath)
  124. {
  125. int fd;
  126. const unsigned char golden_ob[] =
  127. "2db907f9cb4eb938ded904f4832c4331"
  128. "0380e3aeb54364057e7fec5157bfc533"
  129. "00000000000000000000000080000000"
  130. "00000000a58e091ac342724e7c3dc346";
  131. const unsigned char golden_nonce[] = "063c5e01";
  132. 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. icarus_write_fault = 0;
  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. uint64_t max_nonce)
  196. {
  197. struct cgpu_info *icarus;
  198. int fd;
  199. unsigned char ob_bin[64], nonce_bin[4];
  200. unsigned char *ob_hex, *nonce_hex;
  201. uint32_t nonce;
  202. uint32_t hash_count;
  203. time_t t;
  204. icarus = thr->cgpu;
  205. fd = icarus->device_fd;
  206. memset(ob_bin, 0, sizeof(ob_bin));
  207. memcpy(ob_bin, work->midstate, 32);
  208. memcpy(ob_bin + 52, work->data + 64, 12);
  209. rev(ob_bin, 32);
  210. rev(ob_bin + 52, 12);
  211. #ifndef WIN32
  212. tcflush(fd, TCOFLUSH);
  213. #endif
  214. icarus_write(fd, ob_bin, sizeof(ob_bin));
  215. if (icarus_write_fault)
  216. return 0; /* This should never happen */
  217. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  218. if (ob_hex) {
  219. t = time(NULL);
  220. applog(LOG_DEBUG, "Icarus send : %s", ob_hex);
  221. free(ob_hex);
  222. }
  223. /* Icarus will return 8 bytes nonces or nothing */
  224. memset(nonce_bin, 0, sizeof(nonce_bin));
  225. icarus_gets(nonce_bin, sizeof(nonce_bin), fd);
  226. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  227. if (nonce_hex) {
  228. t = time(NULL) - t;
  229. applog(LOG_DEBUG, "Icarus return (elapse %d seconds): %s",
  230. t, nonce_hex);
  231. free(nonce_hex);
  232. }
  233. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  234. if (nonce == 0 && icarus_read_count == ICARUS_READ_FAULT_COUNT)
  235. return 0xffffffff;
  236. #ifndef __BIG_ENDIAN__
  237. nonce = swab32(nonce);
  238. #endif
  239. work->blk.nonce = 0xffffffff;
  240. submit_nonce(thr, work, nonce);
  241. hash_count = (nonce & 0x7fffffff);
  242. if (hash_count == 0)
  243. hash_count = 2;
  244. else {
  245. if (hash_count++ == 0x7fffffff)
  246. hash_count = 0xffffffff;
  247. else
  248. hash_count <<= 1;
  249. }
  250. return hash_count;
  251. }
  252. static void icarus_shutdown(struct thr_info *thr)
  253. {
  254. struct cgpu_info *icarus;
  255. int fd;
  256. if (thr->cgpu) {
  257. icarus = thr->cgpu;
  258. if (icarus->device_path)
  259. free(icarus->device_path);
  260. close(icarus->device_fd);
  261. devices[icarus->device_id] = NULL;
  262. free(icarus);
  263. thr->cgpu = NULL;
  264. }
  265. }
  266. struct device_api icarus_api = {
  267. .name = "Icarus",
  268. .api_detect = icarus_detect,
  269. .thread_prepare = icarus_prepare,
  270. .scanhash = icarus_scanhash,
  271. .thread_shutdown = icarus_shutdown,
  272. };