driver-icarus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
  4. * Copyright 2012 Andrew Smith
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. /*
  12. * Those code should be works fine with V2 and V3 bitstream of Icarus.
  13. * Operation:
  14. * No detection implement.
  15. * Input: 64B = 32B midstate + 20B fill bytes + last 12 bytes of block head.
  16. * Return: send back 32bits immediately when Icarus found a valid nonce.
  17. * no query protocol implemented here, if no data send back in ~11.3
  18. * seconds (full cover time on 32bit nonce range by 380MH/s speed)
  19. * just send another work.
  20. * Notice:
  21. * 1. Icarus will start calculate when you push a work to them, even they
  22. * are busy.
  23. * 2. The 2 FPGAs on Icarus will distribute the job, one will calculate the
  24. * 0 ~ 7FFFFFFF, another one will cover the 80000000 ~ FFFFFFFF.
  25. * 3. It's possible for 2 FPGAs both find valid nonce in the meantime, the 2
  26. * valid nonce will all be send back.
  27. * 4. Icarus will stop work when: a valid nonce has been found or 32 bits
  28. * nonce range is completely calculated.
  29. */
  30. #include <limits.h>
  31. #include <pthread.h>
  32. #include <stdio.h>
  33. #include <sys/time.h>
  34. #include <sys/types.h>
  35. #include <dirent.h>
  36. #include <unistd.h>
  37. #ifndef WIN32
  38. #include <termios.h>
  39. #include <sys/stat.h>
  40. #include <fcntl.h>
  41. #ifndef O_CLOEXEC
  42. #define O_CLOEXEC 0
  43. #endif
  44. #else
  45. #include <windows.h>
  46. #include <io.h>
  47. #ifndef timersub
  48. #define timersub(a, b, result) \
  49. do { \
  50. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  51. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  52. if ((result)->tv_usec < 0) { \
  53. --(result)->tv_sec; \
  54. (result)->tv_usec += 1000000; \
  55. } \
  56. } while (0)
  57. #endif
  58. #endif
  59. #include "elist.h"
  60. #include "miner.h"
  61. // This is valid for a standard Icarus Rev 3
  62. // Assuming each hash pair takes 5.26ns then a whole nonce range would take 11.3s
  63. // Giving a little leaway 11.1s would be best
  64. //#define ICARUS_READ_COUNT_DEFAULT 111
  65. #define ICARUS_READ_COUNT_DEFAULT 80
  66. // 2 x 11.1 / (5.26 x 10^-9)
  67. //#define ESTIMATE_HASHES 0xFB90365E
  68. // This is the 8s value but causes hash rate loss
  69. //#define ESTIMATE_HASHES 0xB54E9147
  70. // TODO: determine why returning any other value when no nonce is found
  71. // causes hash rate loss
  72. #define ESTIMATE_HASHES 0xffffffff
  73. struct device_api icarus_api;
  74. static void rev(unsigned char *s, size_t l)
  75. {
  76. size_t i, j;
  77. unsigned char t;
  78. for (i = 0, j = l - 1; i < j; i++, j--) {
  79. t = s[i];
  80. s[i] = s[j];
  81. s[j] = t;
  82. }
  83. }
  84. static int icarus_open(const char *devpath)
  85. {
  86. #ifndef WIN32
  87. struct termios my_termios;
  88. int serialfd = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  89. if (serialfd == -1)
  90. return -1;
  91. tcgetattr(serialfd, &my_termios);
  92. my_termios.c_cflag = B115200;
  93. my_termios.c_cflag |= CS8;
  94. my_termios.c_cflag |= CREAD;
  95. my_termios.c_cflag |= CLOCAL;
  96. my_termios.c_cflag &= ~(CSIZE | PARENB);
  97. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  98. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  99. my_termios.c_oflag &= ~OPOST;
  100. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  101. my_termios.c_cc[VTIME] = 1; /* block 0.1 second */
  102. my_termios.c_cc[VMIN] = 0;
  103. tcsetattr(serialfd, TCSANOW, &my_termios);
  104. tcflush(serialfd, TCOFLUSH);
  105. tcflush(serialfd, TCIFLUSH);
  106. return serialfd;
  107. #else
  108. COMMCONFIG comCfg;
  109. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0,
  110. NULL, OPEN_EXISTING, 0, NULL);
  111. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  112. return -1;
  113. // thanks to af_newbie for pointers about this
  114. memset(&comCfg, 0 , sizeof(comCfg));
  115. comCfg.dwSize = sizeof(COMMCONFIG);
  116. comCfg.wVersion = 1;
  117. comCfg.dcb.DCBlength = sizeof(DCB);
  118. comCfg.dcb.BaudRate = 115200;
  119. comCfg.dcb.fBinary = 1;
  120. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  121. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  122. comCfg.dcb.ByteSize = 8;
  123. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  124. // block 0.1 second
  125. COMMTIMEOUTS cto = {100, 0, 100, 0, 100};
  126. SetCommTimeouts(hSerial, &cto);
  127. return _open_osfhandle((LONG)hSerial, 0);
  128. #endif
  129. }
  130. static int icarus_gets(unsigned char *buf, size_t bufLen, int fd, int thr_id, int read_count)
  131. {
  132. ssize_t ret = 0;
  133. int rc = 0;
  134. while (bufLen) {
  135. ret = read(fd, buf, 1);
  136. if (ret == 1) {
  137. bufLen--;
  138. buf++;
  139. continue;
  140. }
  141. rc++;
  142. if (rc >= read_count) {
  143. applog(LOG_DEBUG,
  144. "Icarus Read: No data in %.2f seconds", (float)(rc/10.0f));
  145. return 1;
  146. }
  147. if (thr_id >= 0 && work_restart[thr_id].restart) {
  148. applog(LOG_DEBUG,
  149. "Icarus Read: Work restart at %.2f seconds", (float)(rc/10.0f));
  150. return 1;
  151. }
  152. }
  153. return 0;
  154. }
  155. static int icarus_write(int fd, const void *buf, size_t bufLen)
  156. {
  157. size_t ret;
  158. ret = write(fd, buf, bufLen);
  159. if (unlikely(ret != bufLen))
  160. return 1;
  161. return 0;
  162. }
  163. #define icarus_close(fd) close(fd)
  164. static bool icarus_detect_one(const char *devpath)
  165. {
  166. struct timeval tv1, tv2;
  167. int fd;
  168. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  169. // N.B. golden_ob MUST take less time to calculate
  170. // than the timeout set in icarus_open()
  171. // This one takes ~0.53ms on Rev3 Icarus
  172. const char golden_ob[] =
  173. "4679ba4ec99876bf4bfe086082b40025"
  174. "4df6c356451471139a3afa71e48f544a"
  175. "00000000000000000000000000000000"
  176. "0000000087320b1a1426674f2fa722ce";
  177. const char golden_nonce[] = "000187a2";
  178. unsigned char ob_bin[64], nonce_bin[4];
  179. char *nonce_hex;
  180. if (total_devices == MAX_DEVICES)
  181. return false;
  182. fd = icarus_open(devpath);
  183. if (unlikely(fd == -1)) {
  184. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  185. return false;
  186. }
  187. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  188. icarus_write(fd, ob_bin, sizeof(ob_bin));
  189. gettimeofday(&tv1, NULL);
  190. memset(nonce_bin, 0, sizeof(nonce_bin));
  191. icarus_gets(nonce_bin, sizeof(nonce_bin), fd, -1, 1);
  192. gettimeofday(&tv2, NULL);
  193. icarus_close(fd);
  194. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  195. if (nonce_hex) {
  196. if (strncmp(nonce_hex, golden_nonce, 8)) {
  197. applog(LOG_ERR,
  198. "Icarus Detect: "
  199. "Test failed at %s: get %s, should: %s",
  200. devpath, nonce_hex, golden_nonce);
  201. free(nonce_hex);
  202. return false;
  203. }
  204. applog(LOG_DEBUG,
  205. "Icarus Detect: "
  206. "Test succeeded at %s: got %s",
  207. devpath, nonce_hex);
  208. free(nonce_hex);
  209. } else
  210. return false;
  211. /* We have a real Icarus! */
  212. struct cgpu_info *icarus;
  213. icarus = calloc(1, sizeof(struct cgpu_info));
  214. icarus->api = &icarus_api;
  215. icarus->device_path = strdup(devpath);
  216. icarus->threads = 1;
  217. add_cgpu(icarus);
  218. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  219. devpath, icarus->device_id);
  220. return true;
  221. }
  222. static void icarus_detect()
  223. {
  224. struct string_elist *iter, *tmp;
  225. const char*s;
  226. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  227. s = iter->string;
  228. if (!strncmp("icarus:", iter->string, 7))
  229. s += 7;
  230. if (!strcmp(s, "auto") || !strcmp(s, "noauto"))
  231. continue;
  232. if (icarus_detect_one(s))
  233. string_elist_del(iter);
  234. }
  235. }
  236. static bool icarus_prepare(struct thr_info *thr)
  237. {
  238. struct cgpu_info *icarus = thr->cgpu;
  239. struct timeval now;
  240. int fd = icarus_open(icarus->device_path);
  241. if (unlikely(-1 == fd)) {
  242. applog(LOG_ERR, "Failed to open Icarus on %s",
  243. icarus->device_path);
  244. return false;
  245. }
  246. icarus->device_fd = fd;
  247. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  248. gettimeofday(&now, NULL);
  249. get_datestamp(icarus->init, &now);
  250. return true;
  251. }
  252. static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  253. __maybe_unused uint64_t max_nonce)
  254. {
  255. const int thr_id = thr->id;
  256. struct cgpu_info *icarus;
  257. int fd;
  258. int ret;
  259. unsigned char ob_bin[64], nonce_bin[4];
  260. char *ob_hex, *nonce_hex;
  261. uint32_t nonce;
  262. uint32_t hash_count;
  263. struct timeval tv1, tv2, elapsed;
  264. icarus = thr->cgpu;
  265. fd = icarus->device_fd;
  266. memset(ob_bin, 0, sizeof(ob_bin));
  267. memcpy(ob_bin, work->midstate, 32);
  268. memcpy(ob_bin + 52, work->data + 64, 12);
  269. rev(ob_bin, 32);
  270. rev(ob_bin + 52, 12);
  271. #ifndef WIN32
  272. tcflush(fd, TCOFLUSH);
  273. #endif
  274. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  275. gettimeofday(&tv1, NULL);
  276. if (ret)
  277. return 0; /* This should never happen */
  278. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  279. if (ob_hex) {
  280. applog(LOG_DEBUG, "Icarus %d sent: %s",
  281. icarus->device_id, ob_hex);
  282. free(ob_hex);
  283. }
  284. /* Icarus will return 8 bytes nonces or nothing */
  285. memset(nonce_bin, 0, sizeof(nonce_bin));
  286. ret = icarus_gets(nonce_bin, sizeof(nonce_bin), fd, thr_id,
  287. ICARUS_READ_COUNT_DEFAULT);
  288. gettimeofday(&tv2, NULL);
  289. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  290. // aborted before becoming idle, get new work
  291. if (nonce == 0 && ret) {
  292. timersub(&tv2, &tv1, &elapsed);
  293. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08x hashes (%ld.%06lds)",
  294. icarus->device_id, ESTIMATE_HASHES, elapsed.tv_sec, elapsed.tv_usec);
  295. return ESTIMATE_HASHES;
  296. }
  297. #ifndef __BIG_ENDIAN__
  298. nonce = swab32(nonce);
  299. #endif
  300. work->blk.nonce = 0xffffffff;
  301. submit_nonce(thr, work, nonce);
  302. timersub(&tv2, &tv1, &elapsed);
  303. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  304. if (nonce_hex) {
  305. applog(LOG_DEBUG, "Icarus %d returned (elapsed %ld.%06ld seconds): %s",
  306. icarus->device_id, elapsed.tv_sec, elapsed.tv_usec, nonce_hex);
  307. free(nonce_hex);
  308. }
  309. hash_count = (nonce & 0x7fffffff);
  310. if (hash_count == 0)
  311. hash_count = 2;
  312. else {
  313. if (hash_count++ == 0x7fffffff)
  314. hash_count = 0xffffffff;
  315. else
  316. hash_count <<= 1;
  317. }
  318. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08x hashes (%ld.%06lds)",
  319. icarus->device_id, nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  320. return hash_count;
  321. }
  322. static void icarus_shutdown(struct thr_info *thr)
  323. {
  324. struct cgpu_info *icarus;
  325. if (thr->cgpu) {
  326. icarus = thr->cgpu;
  327. if (icarus->device_path)
  328. free(icarus->device_path);
  329. close(icarus->device_fd);
  330. devices[icarus->device_id] = NULL;
  331. free(icarus);
  332. thr->cgpu = NULL;
  333. }
  334. }
  335. struct device_api icarus_api = {
  336. .dname = "icarus",
  337. .name = "ICA",
  338. .api_detect = icarus_detect,
  339. .thread_prepare = icarus_prepare,
  340. .scanhash = icarus_scanhash,
  341. .thread_shutdown = icarus_shutdown,
  342. };