driver-icarus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. COMMCONFIG comCfg;
  92. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0,
  93. NULL, OPEN_EXISTING, 0, NULL);
  94. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  95. return -1;
  96. // thanks to af_newbie for pointers about this
  97. memset(&comCfg, 0 , sizeof(comCfg));
  98. comCfg.dwSize = sizeof(COMMCONFIG);
  99. comCfg.wVersion = 1;
  100. comCfg.dcb.DCBlength = sizeof(DCB);
  101. comCfg.dcb.BaudRate = 115200;
  102. comCfg.dcb.fBinary = 1;
  103. comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
  104. comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
  105. comCfg.dcb.ByteSize = 8;
  106. SetCommConfig(hSerial, &comCfg, sizeof(comCfg));
  107. const DWORD ctoms = ICARUS_READ_FAULT_DECISECONDS * 100;
  108. COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
  109. SetCommTimeouts(hSerial, &cto);
  110. return _open_osfhandle((LONG)hSerial, 0);
  111. #endif
  112. }
  113. static int icarus_gets(unsigned char *buf, size_t bufLen, int fd, volatile unsigned long *wr)
  114. {
  115. ssize_t ret = 0;
  116. int rc = 0;
  117. int epollfd = -1;
  118. #ifdef HAVE_EPOLL
  119. struct epoll_event ev, evr;
  120. epollfd = epoll_create(1);
  121. if (epollfd != -1) {
  122. ev.events = EPOLLIN;
  123. ev.data.fd = fd;
  124. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
  125. close(epollfd);
  126. epollfd = -1;
  127. }
  128. }
  129. #endif
  130. while (bufLen) {
  131. #ifdef HAVE_EPOLL
  132. if (epollfd != -1 && epoll_wait(epollfd, &evr, 1, ICARUS_READ_FAULT_DECISECONDS * 100) != 1)
  133. ret = 0;
  134. else
  135. #endif
  136. ret = read(fd, buf, 1);
  137. if (ret == 1) {
  138. bufLen--;
  139. buf++;
  140. continue;
  141. }
  142. rc++;
  143. if (*wr)
  144. return 1;
  145. if (rc == ICARUS_READ_FAULT_COUNT) {
  146. if (epollfd != -1)
  147. close(epollfd);
  148. applog(LOG_DEBUG,
  149. "Icarus Read: No data in %d seconds", rc * ICARUS_READ_FAULT_DECISECONDS / 10);
  150. return 1;
  151. }
  152. }
  153. if (epollfd != -1)
  154. close(epollfd);
  155. return 0;
  156. }
  157. static int icarus_write(int fd, const void *buf, size_t bufLen)
  158. {
  159. size_t ret;
  160. ret = write(fd, buf, bufLen);
  161. if (unlikely(ret != bufLen))
  162. return 1;
  163. return 0;
  164. }
  165. #define icarus_close(fd) close(fd)
  166. static bool icarus_detect_one(const char *devpath)
  167. {
  168. int fd;
  169. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  170. // This one takes ~0.53ms on Rev3 Icarus
  171. const char golden_ob[] =
  172. "4679ba4ec99876bf4bfe086082b40025"
  173. "4df6c356451471139a3afa71e48f544a"
  174. "00000000000000000000000000000000"
  175. "0000000087320b1a1426674f2fa722ce";
  176. const char golden_nonce[] = "000187a2";
  177. unsigned char ob_bin[64], nonce_bin[4];
  178. char *nonce_hex;
  179. if (total_devices == MAX_DEVICES)
  180. return false;
  181. fd = icarus_open(devpath);
  182. if (unlikely(fd == -1)) {
  183. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  184. return false;
  185. }
  186. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  187. icarus_write(fd, ob_bin, sizeof(ob_bin));
  188. memset(nonce_bin, 0, sizeof(nonce_bin));
  189. volatile unsigned long wr = 0;
  190. icarus_gets(nonce_bin, sizeof(nonce_bin), fd, &wr);
  191. icarus_close(fd);
  192. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  193. if (nonce_hex) {
  194. if (strncmp(nonce_hex, golden_nonce, 8)) {
  195. applog(LOG_ERR,
  196. "Icarus Detect: "
  197. "Test failed at %s: get %s, should: %s",
  198. devpath, nonce_hex, golden_nonce);
  199. free(nonce_hex);
  200. return false;
  201. }
  202. applog(LOG_DEBUG,
  203. "Icarus Detect: "
  204. "Test succeeded at %s: got %s",
  205. devpath, nonce_hex);
  206. free(nonce_hex);
  207. } else
  208. return false;
  209. /* We have a real Icarus! */
  210. struct cgpu_info *icarus;
  211. icarus = calloc(1, sizeof(struct cgpu_info));
  212. icarus->api = &icarus_api;
  213. icarus->device_path = strdup(devpath);
  214. icarus->threads = 1;
  215. add_cgpu(icarus);
  216. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  217. devpath, icarus->device_id);
  218. return true;
  219. }
  220. static void icarus_detect()
  221. {
  222. struct string_elist *iter, *tmp;
  223. const char*s;
  224. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  225. s = iter->string;
  226. if (!strncmp("icarus:", iter->string, 7))
  227. s += 7;
  228. if (!strcmp(s, "auto") || !strcmp(s, "noauto"))
  229. continue;
  230. if (icarus_detect_one(s))
  231. string_elist_del(iter);
  232. }
  233. }
  234. struct icarus_state {
  235. bool firstrun;
  236. struct timeval tv_workstart;
  237. struct work last_work;
  238. bool changework;
  239. };
  240. static bool icarus_prepare(struct thr_info *thr)
  241. {
  242. struct cgpu_info *icarus = thr->cgpu;
  243. struct timeval now;
  244. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  245. gettimeofday(&now, NULL);
  246. get_datestamp(icarus->init, &now);
  247. struct icarus_state *state;
  248. thr->cgpu_data = state = calloc(1, sizeof(*state));
  249. state->firstrun = true;
  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. volatile unsigned long *wr = &work_restart[thr->id].restart;
  256. struct cgpu_info *icarus;
  257. int fd;
  258. int ret, lret;
  259. unsigned char ob_bin[64] = {0}, nonce_bin[4] = {0};
  260. char *ob_hex, *nonce_hex;
  261. uint32_t nonce;
  262. uint32_t hash_count;
  263. struct timeval tv_end, elapsed;
  264. icarus = thr->cgpu;
  265. struct icarus_state *state = thr->cgpu_data;
  266. // Prepare the next work immediately
  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. // Open serial port and wait for the previous run's result
  272. fd = icarus_open(icarus->device_path);
  273. if (unlikely(-1 == fd)) {
  274. applog(LOG_ERR, "Failed to open Icarus on %s",
  275. icarus->device_path);
  276. return 0;
  277. }
  278. if (!state->firstrun) {
  279. if (state->changework)
  280. state->changework = false;
  281. else
  282. {
  283. /* Icarus will return 8 bytes nonces or nothing */
  284. lret = icarus_gets(nonce_bin, sizeof(nonce_bin), fd, wr);
  285. if (lret && *wr) {
  286. // The prepared work is invalid, and the current work is abandoned
  287. // Go back to the main loop to get the next work, and stuff
  288. // Returning to the main loop will clear work_restart, so use a flag...
  289. state->changework = true;
  290. return 1;
  291. }
  292. }
  293. gettimeofday(&tv_end, NULL);
  294. timeval_subtract(&elapsed, &tv_end, &state->tv_workstart);
  295. }
  296. #ifndef WIN32
  297. tcflush(fd, TCOFLUSH);
  298. #endif
  299. gettimeofday(&state->tv_workstart, NULL);
  300. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  301. if (ret) {
  302. icarus_close(fd);
  303. return 0; /* This should never happen */
  304. }
  305. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  306. if (ob_hex) {
  307. applog(LOG_DEBUG, "Icarus %d sent: %s",
  308. icarus->device_id, ob_hex);
  309. free(ob_hex);
  310. }
  311. icarus_close(fd);
  312. work->blk.nonce = 0xffffffff;
  313. if (state->firstrun) {
  314. state->firstrun = false;
  315. memcpy(&state->last_work, work, sizeof(state->last_work));
  316. return 1;
  317. }
  318. // OK, done starting Icarus's next job... now process the last run's result!
  319. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  320. if (nonce_hex) {
  321. applog(LOG_DEBUG, "Icarus %d returned (in %d.%06d seconds): %s",
  322. icarus->device_id, elapsed.tv_sec, elapsed.tv_usec, nonce_hex);
  323. free(nonce_hex);
  324. }
  325. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  326. if (nonce == 0 && lret) {
  327. memcpy(&state->last_work, work, sizeof(state->last_work));
  328. if (unlikely(elapsed.tv_sec > 12 || (elapsed.tv_sec == 11 && elapsed.tv_usec > 300067)))
  329. return 0xffffffff;
  330. // Approximately how much of the nonce Icarus scans in 1 second...
  331. // 0x16a7a561 would be if it was exactly 380 MH/s
  332. // 0x168b7b4b was the average over a 201-sample period based on time to find actual shares
  333. return (0x168b7b4b * elapsed.tv_sec) + (0x17a * elapsed.tv_usec);
  334. }
  335. #ifndef __BIG_ENDIAN__
  336. nonce = swab32(nonce);
  337. #endif
  338. submit_nonce(thr, &state->last_work, nonce);
  339. memcpy(&state->last_work, work, sizeof(state->last_work));
  340. hash_count = (nonce & 0x7fffffff);
  341. if (hash_count == 0)
  342. hash_count = 2;
  343. else {
  344. if (hash_count++ == 0x7fffffff)
  345. hash_count = 0xffffffff;
  346. else
  347. hash_count <<= 1;
  348. }
  349. applog(LOG_DEBUG, "0x%x hashes in %d.%06d seconds", hash_count, elapsed.tv_sec, elapsed.tv_usec);
  350. return hash_count;
  351. }
  352. static void icarus_shutdown(struct thr_info *thr)
  353. {
  354. struct cgpu_info *icarus;
  355. free(thr->cgpu_data);
  356. if (thr->cgpu) {
  357. icarus = thr->cgpu;
  358. if (icarus->device_path)
  359. free(icarus->device_path);
  360. devices[icarus->device_id] = NULL;
  361. free(icarus);
  362. thr->cgpu = NULL;
  363. }
  364. }
  365. struct device_api icarus_api = {
  366. .dname = "icarus",
  367. .name = "PGA",
  368. .api_detect = icarus_detect,
  369. .thread_prepare = icarus_prepare,
  370. .scanhash = icarus_scanhash,
  371. .thread_shutdown = icarus_shutdown,
  372. };