driver-icarus.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. #endif
  48. #ifdef HAVE_SYS_EPOLL_H
  49. #include <sys/epoll.h>
  50. #define HAVE_EPOLL
  51. #endif
  52. #include "elist.h"
  53. #include "miner.h"
  54. // 8 second timeout
  55. #define ICARUS_READ_FAULT_DECISECONDS (1)
  56. #define ICARUS_READ_FAULT_COUNT (80)
  57. struct device_api icarus_api;
  58. static void rev(unsigned char *s, size_t l)
  59. {
  60. size_t i, j;
  61. unsigned char t;
  62. for (i = 0, j = l - 1; i < j; i++, j--) {
  63. t = s[i];
  64. s[i] = s[j];
  65. s[j] = t;
  66. }
  67. }
  68. static int icarus_open2(const char *devpath, __maybe_unused bool purge)
  69. {
  70. #ifndef WIN32
  71. struct termios my_termios;
  72. int serialfd = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  73. if (serialfd == -1)
  74. return -1;
  75. tcgetattr(serialfd, &my_termios);
  76. my_termios.c_cflag = B115200;
  77. my_termios.c_cflag |= CS8;
  78. my_termios.c_cflag |= CREAD;
  79. my_termios.c_cflag |= CLOCAL;
  80. my_termios.c_cflag &= ~(CSIZE | PARENB);
  81. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  82. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  83. my_termios.c_oflag &= ~OPOST;
  84. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  85. my_termios.c_cc[VTIME] = ICARUS_READ_FAULT_DECISECONDS;
  86. my_termios.c_cc[VMIN] = 0;
  87. tcsetattr(serialfd, TCSANOW, &my_termios);
  88. tcflush(serialfd, TCOFLUSH);
  89. tcflush(serialfd, TCIFLUSH);
  90. return serialfd;
  91. #else
  92. COMMCONFIG comCfg;
  93. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0,
  94. NULL, OPEN_EXISTING, 0, NULL);
  95. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  96. return -1;
  97. // thanks to af_newbie for pointers about this
  98. memset(&comCfg, 0 , sizeof(comCfg));
  99. comCfg.dwSize = sizeof(COMMCONFIG);
  100. comCfg.wVersion = 1;
  101. comCfg.dcb.DCBlength = sizeof(DCB);
  102. comCfg.dcb.BaudRate = 115200;
  103. comCfg.dcb.fBinary = 1;
  104. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  105. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  106. comCfg.dcb.ByteSize = 8;
  107. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  108. const DWORD ctoms = ICARUS_READ_FAULT_DECISECONDS * 100;
  109. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  110. SetCommTimeouts(hSerial, &cto);
  111. if (purge) {
  112. PurgeComm(hSerial, PURGE_RXABORT);
  113. PurgeComm(hSerial, PURGE_TXABORT);
  114. PurgeComm(hSerial, PURGE_RXCLEAR);
  115. PurgeComm(hSerial, PURGE_TXCLEAR);
  116. }
  117. return _open_osfhandle((LONG)hSerial, 0);
  118. #endif
  119. }
  120. #define icarus_open(devpath) icarus_open2(devpath, false)
  121. static int icarus_gets(unsigned char *buf, size_t bufLen, int fd, volatile unsigned long *wr, int read_count)
  122. {
  123. ssize_t ret = 0;
  124. int rc = 0;
  125. int epollfd = -1;
  126. #ifdef HAVE_EPOLL
  127. struct epoll_event ev, evr;
  128. epollfd = epoll_create(1);
  129. if (epollfd != -1) {
  130. ev.events = EPOLLIN;
  131. ev.data.fd = fd;
  132. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
  133. close(epollfd);
  134. epollfd = -1;
  135. }
  136. }
  137. #endif
  138. while (bufLen) {
  139. #ifdef HAVE_EPOLL
  140. if (epollfd != -1 && epoll_wait(epollfd, &evr, 1, ICARUS_READ_FAULT_DECISECONDS * 100) != 1)
  141. ret = 0;
  142. else
  143. #endif
  144. ret = read(fd, buf, 1);
  145. if (ret == 1) {
  146. bufLen--;
  147. buf++;
  148. continue;
  149. }
  150. rc++;
  151. if (*wr) {
  152. rc *= ICARUS_READ_FAULT_DECISECONDS;
  153. applog(LOG_DEBUG,
  154. "Icarus Read: Work restart at %d.%d seconds", rc / 10, rc % 10);
  155. return 1;
  156. }
  157. if (rc >= read_count) {
  158. if (epollfd != -1)
  159. close(epollfd);
  160. rc *= ICARUS_READ_FAULT_DECISECONDS;
  161. applog(LOG_DEBUG,
  162. "Icarus Read: No data in %d.%d seconds", rc / 10, rc % 10);
  163. return 1;
  164. }
  165. }
  166. if (epollfd != -1)
  167. close(epollfd);
  168. return 0;
  169. }
  170. static int icarus_write(int fd, const void *buf, size_t bufLen)
  171. {
  172. size_t ret;
  173. ret = write(fd, buf, bufLen);
  174. if (unlikely(ret != bufLen))
  175. return 1;
  176. return 0;
  177. }
  178. #define icarus_close(fd) close(fd)
  179. static bool icarus_detect_one(const char *devpath)
  180. {
  181. int fd;
  182. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  183. // N.B. golden_ob MUST take less time to calculate
  184. // than the timeout set in icarus_open()
  185. // This one takes ~0.53ms on Rev3 Icarus
  186. const char golden_ob[] =
  187. "4679ba4ec99876bf4bfe086082b40025"
  188. "4df6c356451471139a3afa71e48f544a"
  189. "00000000000000000000000000000000"
  190. "0000000087320b1a1426674f2fa722ce";
  191. const char golden_nonce[] = "000187a2";
  192. unsigned char ob_bin[64], nonce_bin[4];
  193. char *nonce_hex;
  194. if (total_devices == MAX_DEVICES)
  195. return false;
  196. fd = icarus_open2(devpath, true);
  197. if (unlikely(fd == -1)) {
  198. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  199. return false;
  200. }
  201. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  202. icarus_write(fd, ob_bin, sizeof(ob_bin));
  203. memset(nonce_bin, 0, sizeof(nonce_bin));
  204. volatile unsigned long wr = 0;
  205. icarus_gets(nonce_bin, sizeof(nonce_bin), fd, &wr, 1);
  206. icarus_close(fd);
  207. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  208. if (nonce_hex) {
  209. if (strncmp(nonce_hex, golden_nonce, 8)) {
  210. applog(LOG_ERR,
  211. "Icarus Detect: "
  212. "Test failed at %s: get %s, should: %s",
  213. devpath, nonce_hex, golden_nonce);
  214. free(nonce_hex);
  215. return false;
  216. }
  217. applog(LOG_DEBUG,
  218. "Icarus Detect: "
  219. "Test succeeded at %s: got %s",
  220. devpath, nonce_hex);
  221. free(nonce_hex);
  222. } else
  223. return false;
  224. /* We have a real Icarus! */
  225. struct cgpu_info *icarus;
  226. icarus = calloc(1, sizeof(struct cgpu_info));
  227. icarus->api = &icarus_api;
  228. icarus->device_path = strdup(devpath);
  229. icarus->threads = 1;
  230. add_cgpu(icarus);
  231. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  232. devpath, icarus->device_id);
  233. return true;
  234. }
  235. static void icarus_detect()
  236. {
  237. struct string_elist *iter, *tmp;
  238. const char*s;
  239. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  240. s = iter->string;
  241. if (!strncmp("icarus:", iter->string, 7))
  242. s += 7;
  243. if (!strcmp(s, "auto") || !strcmp(s, "noauto"))
  244. continue;
  245. if (icarus_detect_one(s))
  246. string_elist_del(iter);
  247. }
  248. }
  249. struct icarus_state {
  250. bool firstrun;
  251. struct timeval tv_workstart;
  252. struct work last_work;
  253. bool changework;
  254. };
  255. static bool icarus_prepare(struct thr_info *thr)
  256. {
  257. struct cgpu_info *icarus = thr->cgpu;
  258. struct timeval now;
  259. int fd = icarus_open2(icarus->device_path, true);
  260. if (unlikely(-1 == fd)) {
  261. applog(LOG_ERR, "Failed to open Icarus on %s",
  262. icarus->device_path);
  263. return false;
  264. }
  265. icarus->device_fd = fd;
  266. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  267. gettimeofday(&now, NULL);
  268. get_datestamp(icarus->init, &now);
  269. struct icarus_state *state;
  270. thr->cgpu_data = state = calloc(1, sizeof(*state));
  271. state->firstrun = true;
  272. return true;
  273. }
  274. static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  275. __maybe_unused uint64_t max_nonce)
  276. {
  277. volatile unsigned long *wr = &work_restart[thr->id].restart;
  278. struct cgpu_info *icarus;
  279. int fd;
  280. int ret, lret;
  281. unsigned char ob_bin[64] = {0}, nonce_bin[4] = {0};
  282. char *ob_hex, *nonce_hex;
  283. uint32_t nonce;
  284. uint32_t hash_count;
  285. struct timeval tv_finish, elapsed;
  286. elapsed.tv_sec = elapsed.tv_usec = 0;
  287. icarus = thr->cgpu;
  288. struct icarus_state *state = thr->cgpu_data;
  289. // Prepare the next work immediately
  290. memcpy(ob_bin, work->midstate, 32);
  291. memcpy(ob_bin + 52, work->data + 64, 12);
  292. rev(ob_bin, 32);
  293. rev(ob_bin + 52, 12);
  294. // Wait for the previous run's result
  295. fd = icarus->device_fd;
  296. if (!state->firstrun) {
  297. if (state->changework)
  298. state->changework = false;
  299. else
  300. {
  301. /* Icarus will return 4 bytes nonces or nothing */
  302. lret = icarus_gets(nonce_bin, sizeof(nonce_bin), fd, wr,
  303. ICARUS_READ_FAULT_COUNT);
  304. if (lret && *wr) {
  305. // The prepared work is invalid, and the current work is abandoned
  306. // Go back to the main loop to get the next work, and stuff
  307. // Returning to the main loop will clear work_restart, so use a flag...
  308. state->changework = true;
  309. return 1;
  310. }
  311. }
  312. gettimeofday(&tv_finish, NULL);
  313. timeval_subtract(&elapsed, &tv_finish, &state->tv_workstart);
  314. }
  315. #ifndef WIN32
  316. tcflush(fd, TCOFLUSH);
  317. #endif
  318. gettimeofday(&state->tv_workstart, NULL);
  319. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  320. if (ret) {
  321. icarus_close(fd);
  322. return 0; /* This should never happen */
  323. }
  324. if (opt_debug) {
  325. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  326. if (ob_hex) {
  327. applog(LOG_DEBUG, "Icarus %d sent: %s",
  328. icarus->device_id, ob_hex);
  329. free(ob_hex);
  330. }
  331. }
  332. // Reopen the serial port to workaround a USB-host-chipset-specific issue with the Icarus's buggy USB-UART
  333. icarus_close(fd);
  334. fd = icarus_open(icarus->device_path);
  335. if (unlikely(-1 == fd)) {
  336. applog(LOG_ERR, "Failed to reopen Icarus on %s",
  337. icarus->device_path);
  338. return 0;
  339. }
  340. icarus->device_fd = fd;
  341. work->blk.nonce = 0xffffffff;
  342. if (state->firstrun) {
  343. state->firstrun = false;
  344. memcpy(&state->last_work, work, sizeof(state->last_work));
  345. return 1;
  346. }
  347. // OK, done starting Icarus's next job... now process the last run's result!
  348. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  349. // aborted before becoming idle, get new work
  350. if (nonce == 0 && lret) {
  351. memcpy(&state->last_work, work, sizeof(state->last_work));
  352. uint32_t ESTIMATE_HASHES;
  353. if (unlikely(elapsed.tv_sec > 12 || (elapsed.tv_sec == 11 && elapsed.tv_usec > 353063)))
  354. ESTIMATE_HASHES = 0xffffffff;
  355. else
  356. // Approximately how much of the nonce Icarus scans in 1 second...
  357. // 0x16a7a561 would be if it was exactly 380 MH/s
  358. // 0x16a65700 would be the perfect 5.2631579ns/H
  359. // 0x168c89e5 was the average over a 20,050-sample period based on time to find actual shares
  360. ESTIMATE_HASHES = (0x168c89e5 * elapsed.tv_sec) + (0x17a * elapsed.tv_usec);
  361. if (opt_debug) {
  362. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08x hashes (%ld.%06lds)",
  363. icarus->device_id, ESTIMATE_HASHES, elapsed.tv_sec, elapsed.tv_usec);
  364. }
  365. return ESTIMATE_HASHES;
  366. }
  367. #if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
  368. nonce = swab32(nonce);
  369. #endif
  370. submit_nonce(thr, &state->last_work, nonce);
  371. memcpy(&state->last_work, work, sizeof(state->last_work));
  372. if (opt_debug) {
  373. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  374. if (nonce_hex) {
  375. applog(LOG_DEBUG, "Icarus %d returned (elapsed %ld.%06ld seconds): %s",
  376. icarus->device_id, elapsed.tv_sec, elapsed.tv_usec, nonce_hex);
  377. free(nonce_hex);
  378. }
  379. }
  380. hash_count = (nonce & 0x7fffffff);
  381. if (hash_count++ == 0x7fffffff)
  382. hash_count = 0xffffffff;
  383. else
  384. hash_count <<= 1;
  385. if (opt_debug)
  386. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08x hashes (%ld.%06lds)",
  387. icarus->device_id, nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  388. return hash_count;
  389. }
  390. static void icarus_shutdown(struct thr_info *thr)
  391. {
  392. struct cgpu_info *icarus = thr->cgpu;
  393. icarus_close(icarus->device_fd);
  394. free(thr->cgpu_data);
  395. }
  396. struct device_api icarus_api = {
  397. .dname = "icarus",
  398. .name = "PGA",
  399. .api_detect = icarus_detect,
  400. .thread_prepare = icarus_prepare,
  401. .scanhash = icarus_scanhash,
  402. .thread_shutdown = icarus_shutdown,
  403. };