driver-icarus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. #ifdef HAVE_SYS_EPOLL_H
  48. #include <sys/epoll.h>
  49. #define HAVE_EPOLL
  50. #endif
  51. #include "elist.h"
  52. #include "miner.h"
  53. // 8 second timeout
  54. #define ICARUS_READ_FAULT_DECISECONDS (1)
  55. #define ICARUS_READ_FAULT_COUNT (80)
  56. struct device_api icarus_api;
  57. static void rev(unsigned char *s, size_t l)
  58. {
  59. size_t i, j;
  60. unsigned char t;
  61. for (i = 0, j = l - 1; i < j; i++, j--) {
  62. t = s[i];
  63. s[i] = s[j];
  64. s[j] = t;
  65. }
  66. }
  67. static int icarus_open(const char *devpath)
  68. {
  69. #ifndef WIN32
  70. struct termios my_termios;
  71. int serialfd = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  72. if (serialfd == -1)
  73. return -1;
  74. tcgetattr(serialfd, &my_termios);
  75. my_termios.c_cflag = B115200;
  76. my_termios.c_cflag |= CS8;
  77. my_termios.c_cflag |= CREAD;
  78. my_termios.c_cflag |= CLOCAL;
  79. my_termios.c_cflag &= ~(CSIZE | PARENB);
  80. my_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
  81. ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  82. my_termios.c_oflag &= ~OPOST;
  83. my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  84. my_termios.c_cc[VTIME] = ICARUS_READ_FAULT_DECISECONDS;
  85. my_termios.c_cc[VMIN] = 0;
  86. tcsetattr(serialfd, TCSANOW, &my_termios);
  87. tcflush(serialfd, TCOFLUSH);
  88. tcflush(serialfd, TCIFLUSH);
  89. return serialfd;
  90. #else
  91. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0,
  92. NULL, OPEN_EXISTING, 0, NULL);
  93. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  94. return -1;
  95. const DWORD ctoms = ICARUS_READ_FAULT_DECISECONDS * 100;
  96. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  97. SetCommTimeouts(hSerial, &cto);
  98. return _open_osfhandle((LONG)hSerial, 0);
  99. #endif
  100. }
  101. static int icarus_gets(unsigned char *buf, size_t bufLen, int fd, volatile unsigned long *wr)
  102. {
  103. ssize_t ret = 0;
  104. int rc = 0;
  105. int epollfd = -1;
  106. #ifdef HAVE_EPOLL
  107. struct epoll_event ev, evr;
  108. epollfd = epoll_create(1);
  109. if (epollfd != -1) {
  110. ev.events = EPOLLIN;
  111. ev.data.fd = fd;
  112. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
  113. close(epollfd);
  114. epollfd = -1;
  115. }
  116. }
  117. #endif
  118. while (bufLen) {
  119. #ifdef HAVE_EPOLL
  120. if (epollfd != -1 && epoll_wait(epollfd, &evr, 1, ICARUS_READ_FAULT_DECISECONDS * 100) != 1)
  121. ret = 0;
  122. else
  123. #endif
  124. ret = read(fd, buf, 1);
  125. if (ret == 1) {
  126. bufLen--;
  127. buf++;
  128. continue;
  129. }
  130. rc++;
  131. if (*wr)
  132. return 1;
  133. if (rc == ICARUS_READ_FAULT_COUNT) {
  134. if (epollfd != -1)
  135. close(epollfd);
  136. applog(LOG_DEBUG,
  137. "Icarus Read: No data in %d seconds", rc * ICARUS_READ_FAULT_DECISECONDS / 10);
  138. return 1;
  139. }
  140. }
  141. if (epollfd != -1)
  142. close(epollfd);
  143. return 0;
  144. }
  145. static int icarus_write(int fd, const void *buf, size_t bufLen)
  146. {
  147. size_t ret;
  148. ret = write(fd, buf, bufLen);
  149. if (unlikely(ret != bufLen))
  150. return 1;
  151. return 0;
  152. }
  153. #define icarus_close(fd) close(fd)
  154. static bool icarus_detect_one(const char *devpath)
  155. {
  156. int fd;
  157. const char golden_ob[] =
  158. "2db907f9cb4eb938ded904f4832c4331"
  159. "0380e3aeb54364057e7fec5157bfc533"
  160. "00000000000000000000000080000000"
  161. "00000000a58e091ac342724e7c3dc346";
  162. const char golden_nonce[] = "063c5e01";
  163. unsigned char ob_bin[64], nonce_bin[4];
  164. char *nonce_hex;
  165. if (total_devices == MAX_DEVICES)
  166. return false;
  167. fd = icarus_open(devpath);
  168. if (unlikely(fd == -1)) {
  169. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  170. return false;
  171. }
  172. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  173. icarus_write(fd, ob_bin, sizeof(ob_bin));
  174. memset(nonce_bin, 0, sizeof(nonce_bin));
  175. volatile unsigned long wr = 0;
  176. icarus_gets(nonce_bin, sizeof(nonce_bin), fd, &wr);
  177. icarus_close(fd);
  178. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  179. if (nonce_hex) {
  180. if (strncmp(nonce_hex, golden_nonce, 8)) {
  181. applog(LOG_ERR,
  182. "Icarus Detect: "
  183. "Test failed at %s: get %s, should: %s",
  184. devpath, nonce_hex, golden_nonce);
  185. free(nonce_hex);
  186. return false;
  187. }
  188. free(nonce_hex);
  189. } else
  190. return false;
  191. /* We have a real Icarus! */
  192. struct cgpu_info *icarus;
  193. icarus = calloc(1, sizeof(struct cgpu_info));
  194. icarus->api = &icarus_api;
  195. icarus->device_path = strdup(devpath);
  196. icarus->threads = 1;
  197. add_cgpu(icarus);
  198. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  199. devpath, icarus->device_id);
  200. return true;
  201. }
  202. static void icarus_detect()
  203. {
  204. struct string_elist *iter, *tmp;
  205. const char*s;
  206. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  207. s = iter->string;
  208. if (!strncmp("icarus:", iter->string, 7))
  209. s += 7;
  210. if (!strcmp(s, "auto") || !strcmp(s, "noauto"))
  211. continue;
  212. if (icarus_detect_one(s))
  213. string_elist_del(iter);
  214. }
  215. }
  216. struct icarus_state {
  217. bool firstrun;
  218. struct timeval tv_workstart;
  219. struct work last_work;
  220. bool changework;
  221. };
  222. static bool icarus_prepare(struct thr_info *thr)
  223. {
  224. struct cgpu_info *icarus = thr->cgpu;
  225. struct timeval now;
  226. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  227. gettimeofday(&now, NULL);
  228. get_datestamp(icarus->init, &now);
  229. struct icarus_state *state;
  230. thr->cgpu_data = state = calloc(1, sizeof(*state));
  231. state->firstrun = true;
  232. return true;
  233. }
  234. static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  235. __maybe_unused uint64_t max_nonce)
  236. {
  237. volatile unsigned long *wr = &work_restart[thr->id].restart;
  238. struct cgpu_info *icarus;
  239. int fd;
  240. int ret, lret;
  241. unsigned char ob_bin[64] = {0}, nonce_bin[4] = {0};
  242. char *ob_hex, *nonce_hex;
  243. uint32_t nonce;
  244. uint32_t hash_count;
  245. struct timeval tv_end, diff;
  246. icarus = thr->cgpu;
  247. struct icarus_state *state = thr->cgpu_data;
  248. // Prepare the next work immediately
  249. memcpy(ob_bin, work->midstate, 32);
  250. memcpy(ob_bin + 52, work->data + 64, 12);
  251. rev(ob_bin, 32);
  252. rev(ob_bin + 52, 12);
  253. // Open serial port and wait for the previous run's result
  254. fd = icarus_open(icarus->device_path);
  255. if (unlikely(-1 == fd)) {
  256. applog(LOG_ERR, "Failed to open Icarus on %s",
  257. icarus->device_path);
  258. return 0;
  259. }
  260. if (!state->firstrun) {
  261. if (state->changework)
  262. state->changework = false;
  263. else
  264. {
  265. /* Icarus will return 8 bytes nonces or nothing */
  266. lret = icarus_gets(nonce_bin, sizeof(nonce_bin), fd, wr);
  267. if (lret && *wr) {
  268. // The prepared work is invalid, and the current work is abandoned
  269. // Go back to the main loop to get the next work, and stuff
  270. // Returning to the main loop will clear work_restart, so use a flag...
  271. state->changework = true;
  272. return 1;
  273. }
  274. }
  275. gettimeofday(&tv_end, NULL);
  276. timeval_subtract(&diff, &tv_end, &state->tv_workstart);
  277. }
  278. #ifndef WIN32
  279. tcflush(fd, TCOFLUSH);
  280. #endif
  281. gettimeofday(&state->tv_workstart, NULL);
  282. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  283. if (ret) {
  284. icarus_close(fd);
  285. return 0; /* This should never happen */
  286. }
  287. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  288. if (ob_hex) {
  289. applog(LOG_DEBUG, "Icarus %s send: %s",
  290. icarus->device_id, ob_hex);
  291. free(ob_hex);
  292. }
  293. icarus_close(fd);
  294. work->blk.nonce = 0xffffffff;
  295. if (state->firstrun) {
  296. state->firstrun = false;
  297. memcpy(&state->last_work, work, sizeof(state->last_work));
  298. return 1;
  299. }
  300. // OK, done starting Icarus's next job... now process the last run's result!
  301. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  302. if (nonce_hex) {
  303. applog(LOG_DEBUG, "Icarus %d returned (in %d.%06d seconds): %s",
  304. icarus->device_id, diff.tv_sec, diff.tv_usec, nonce_hex);
  305. free(nonce_hex);
  306. }
  307. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  308. if (nonce == 0 && lret) {
  309. memcpy(&state->last_work, work, sizeof(state->last_work));
  310. if (unlikely(diff.tv_sec > 12 || (diff.tv_sec == 11 && diff.tv_usec > 300067)))
  311. return 0xffffffff;
  312. // Approximately how much of the nonce Icarus scans in 1 second...
  313. // 0x16a7a561 would be if it was exactly 380 MH/s
  314. // 0x168b7b4b was the average over a 201-sample period based on time to find actual shares
  315. return (0x168b7b4b * diff.tv_sec) + (0x17a * diff.tv_usec);
  316. }
  317. #ifndef __BIG_ENDIAN__
  318. nonce = swab32(nonce);
  319. #endif
  320. submit_nonce(thr, &state->last_work, nonce);
  321. memcpy(&state->last_work, work, sizeof(state->last_work));
  322. hash_count = (nonce & 0x7fffffff);
  323. if (hash_count == 0)
  324. hash_count = 2;
  325. else {
  326. if (hash_count++ == 0x7fffffff)
  327. hash_count = 0xffffffff;
  328. else
  329. hash_count <<= 1;
  330. }
  331. applog(LOG_DEBUG, "0x%x hashes in %d.%06d seconds", hash_count, diff.tv_sec, diff.tv_usec);
  332. return hash_count;
  333. }
  334. static void icarus_shutdown(struct thr_info *thr)
  335. {
  336. struct cgpu_info *icarus;
  337. free(thr->cgpu_data);
  338. if (thr->cgpu) {
  339. icarus = thr->cgpu;
  340. if (icarus->device_path)
  341. free(icarus->device_path);
  342. devices[icarus->device_id] = NULL;
  343. free(icarus);
  344. thr->cgpu = NULL;
  345. }
  346. }
  347. struct device_api icarus_api = {
  348. .dname = "icarus",
  349. .name = "PGA",
  350. .api_detect = icarus_detect,
  351. .thread_prepare = icarus_prepare,
  352. .scanhash = icarus_scanhash,
  353. .thread_shutdown = icarus_shutdown,
  354. };