driver-minergate.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. *
  9. * To avoid doubt: Programs using the minergate protocol to interface with
  10. * BFGMiner are considered part of the Corresponding Source, and not an
  11. * independent work. This means that such programs distrbuted with BFGMiner
  12. * must be released under the terms of the GPLv3 license, or sufficiently
  13. * compatible terms.
  14. */
  15. #include "config.h"
  16. #include <ctype.h>
  17. #include <math.h>
  18. #include <stdbool.h>
  19. #include <stdint.h>
  20. #include <sys/epoll.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <sys/un.h>
  24. #include "deviceapi.h"
  25. #include "logging.h"
  26. #include "miner.h"
  27. #define MINERGATE_MAX_NONCE_DIFF 0x20
  28. static const char * const minergate_stats_file = "/var/run/mg_rate_temp";
  29. #define MINERGATE_PROTOCOL_VER 6
  30. #define MINERGATE_MAGIC 0xcaf4
  31. static const int minergate_max_responses = 300;
  32. #define MINERGATE_PKT_HEADER_SZ 8
  33. #define MINERGATE_PKT_REQ_ITEM_SZ 0x34
  34. #define MINERGATE_PKT_REQ_MAX 100
  35. #define MINERGATE_PKT_RSP_ITEM_SZ 0x14
  36. #define MINERGATE_PKT_RSP_MAX 300
  37. #define MINERGATE_POLL_US 100000
  38. #define MINERGATE_RETRY_US 5000000
  39. #define MINERGATE_PKT_REQ_SZ (MINERGATE_PKT_HEADER_SZ + (MINERGATE_PKT_REQ_ITEM_SZ * MINERGATE_PKT_REQ_MAX))
  40. #define MINERGATE_PKT_RSP_SZ (MINERGATE_PKT_HEADER_SZ + (MINERGATE_PKT_RSP_ITEM_SZ * MINERGATE_PKT_RSP_MAX))
  41. BFG_REGISTER_DRIVER(minergate_drv)
  42. enum minergate_reqpkt_flags {
  43. MRPF_FIRST = 1,
  44. MRPF_FLUSH = 2,
  45. };
  46. struct minergate_state {
  47. work_device_id_t next_jobid;
  48. unsigned ready_to_queue;
  49. uint8_t *req_buffer;
  50. long *stats;
  51. unsigned stats_count;
  52. };
  53. static
  54. int minergate_open(const char * const devpath)
  55. {
  56. size_t devpath_len = strlen(devpath);
  57. struct sockaddr_un sa = {
  58. .sun_family = AF_UNIX,
  59. };
  60. #ifdef UNIX_PATH_MAX
  61. if (devpath_len >= UNIX_PATH_MAX)
  62. #else
  63. if (devpath_len >= sizeof(sa.sun_path))
  64. #endif
  65. return -1;
  66. const int fd = socket(PF_UNIX, SOCK_STREAM, 0);
  67. strcpy(sa.sun_path, devpath);
  68. if (connect(fd, &sa, sizeof(sa)))
  69. {
  70. close(fd);
  71. return -1;
  72. }
  73. return fd;
  74. }
  75. static
  76. ssize_t minergate_read(const int fd, void * const buf_p, size_t bufLen)
  77. {
  78. uint8_t *buf = buf_p;
  79. ssize_t rv, ret = 0;
  80. while (bufLen > 0)
  81. {
  82. rv = read(fd, buf, bufLen);
  83. if (rv <= 0)
  84. {
  85. if (ret > 0)
  86. return ret;
  87. return rv;
  88. }
  89. buf += rv;
  90. bufLen -= rv;
  91. ret += rv;
  92. }
  93. return ret;
  94. }
  95. static
  96. bool minergate_detect_one(const char * const devpath)
  97. {
  98. bool rv = false;
  99. const int fd = minergate_open(devpath);
  100. if (unlikely(fd < 0))
  101. applogr(false, LOG_DEBUG, "%s: %s: Cannot connect", minergate_drv.dname, devpath);
  102. int epfd = -1;
  103. uint8_t buf[MINERGATE_PKT_REQ_SZ] = {0xbf, 0x90, MINERGATE_PROTOCOL_VER, MRPF_FIRST, 0,0, 0 /* req count */,};
  104. pk_u16le(buf, 4, MINERGATE_MAGIC);
  105. if (MINERGATE_PKT_REQ_SZ != write(fd, buf, MINERGATE_PKT_REQ_SZ))
  106. return_via_applog(out, , LOG_DEBUG, "%s: %s: write incomplete or failed", minergate_drv.dname, devpath);
  107. epfd = epoll_create(1);
  108. if (epfd < 0)
  109. return_via_applog(out, , LOG_DEBUG, "%s: %s: %s failed", minergate_drv.dname, devpath, "epoll_create");
  110. struct epoll_event eev;
  111. eev.events = EPOLLIN;
  112. if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &eev))
  113. return_via_applog(out, , LOG_DEBUG, "%s: %s: %s failed", minergate_drv.dname, devpath, "epoll_ctl");
  114. size_t read_bytes = 0;
  115. static const size_t read_expect = MINERGATE_PKT_HEADER_SZ;
  116. ssize_t r;
  117. while (read_bytes < read_expect)
  118. {
  119. if (epoll_wait(epfd, &eev, 1, 1000) != 1)
  120. return_via_applog(out, , LOG_DEBUG, "%s: %s: Timeout waiting for response", minergate_drv.dname, devpath);
  121. r = read(fd, &buf[read_bytes], read_expect - read_bytes);
  122. if (r <= 0)
  123. return_via_applog(out, , LOG_DEBUG, "%s: %s: %s failed", minergate_drv.dname, devpath, "read");
  124. read_bytes += r;
  125. }
  126. if (buf[1] != 0x90)
  127. return_via_applog(out, , LOG_DEBUG, "%s: %s: %s mismatch", minergate_drv.dname, devpath, "request_id");
  128. if (buf[2] != MINERGATE_PROTOCOL_VER)
  129. return_via_applog(out, , LOG_DEBUG, "%s: %s: %s mismatch", minergate_drv.dname, devpath, "Protocol version");
  130. if (upk_u16le(buf, 4) != MINERGATE_MAGIC)
  131. return_via_applog(out, , LOG_DEBUG, "%s: %s: %s mismatch", minergate_drv.dname, devpath, "magic");
  132. uint16_t responses = upk_u16le(buf, 6);
  133. if (responses > minergate_max_responses)
  134. return_via_applog(out, , LOG_DEBUG, "%s: %s: More than maximum responses", minergate_drv.dname, devpath);
  135. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  136. *cgpu = (struct cgpu_info){
  137. .drv = &minergate_drv,
  138. .device_path = strdup(devpath),
  139. .deven = DEV_ENABLED,
  140. .threads = 1,
  141. };
  142. rv = add_cgpu(cgpu);
  143. out:
  144. close(fd);
  145. if (epfd >= 0)
  146. close(epfd);
  147. return rv;
  148. }
  149. static
  150. int minergate_detect_auto(void)
  151. {
  152. return minergate_detect_one("/tmp/connection_pipe") ? 1 : 0;
  153. }
  154. static
  155. void minergate_detect(void)
  156. {
  157. generic_detect(&minergate_drv, minergate_detect_one, minergate_detect_auto, 0);
  158. }
  159. static
  160. bool minergate_init(struct thr_info * const thr)
  161. {
  162. struct cgpu_info * const dev = thr->cgpu;
  163. const int fd = minergate_open(dev->device_path);
  164. dev->device_fd = fd;
  165. if (fd < 0)
  166. applogr(false, LOG_ERR, "%s: Cannot connect", dev->dev_repr);
  167. struct minergate_state * const state = malloc(sizeof(*state) + MINERGATE_PKT_REQ_SZ);
  168. if (!state)
  169. applogr(false, LOG_ERR, "%s: %s failed", dev->dev_repr, "malloc");
  170. *state = (struct minergate_state){
  171. .req_buffer = (void*)&state[1]
  172. };
  173. thr->cgpu_data = state;
  174. thr->work = thr->work_list = NULL;
  175. mutex_init(&dev->device_mutex);
  176. memset(state->req_buffer, 0, MINERGATE_PKT_REQ_SZ);
  177. pk_u8(state->req_buffer, 2, MINERGATE_PROTOCOL_VER);
  178. state->req_buffer[3] = MRPF_FIRST;
  179. pk_u16le(state->req_buffer, 4, MINERGATE_MAGIC);
  180. return true;
  181. }
  182. static
  183. bool minergate_queue_full(struct thr_info * const thr)
  184. {
  185. static const unsigned max_minergate_jobs = 300, max_requests = 100;
  186. struct minergate_state * const state = thr->cgpu_data;
  187. bool qf;
  188. if (HASH_COUNT(thr->work) + state->ready_to_queue >= max_minergate_jobs)
  189. qf = true;
  190. else
  191. if (state->ready_to_queue >= max_requests)
  192. qf = true;
  193. else
  194. qf = false;
  195. thr->queue_full = qf;
  196. return qf;
  197. }
  198. static
  199. bool minergate_queue_append(struct thr_info * const thr, struct work * const work)
  200. {
  201. struct minergate_state * const state = thr->cgpu_data;
  202. if (minergate_queue_full(thr))
  203. return false;
  204. work->device_id = (uint32_t)(state->next_jobid++);
  205. uint8_t * const my_buf = &state->req_buffer[MINERGATE_PKT_HEADER_SZ + (MINERGATE_PKT_REQ_ITEM_SZ * state->ready_to_queue++)];
  206. pk_u32be(my_buf, 0, work->device_id);
  207. memcpy(&my_buf[ 4], &work->data[0x48], 4); // nbits
  208. memcpy(&my_buf[ 8], &work->data[0x44], 4); // ntime
  209. memcpy(&my_buf[0x0c], &work->data[0x40], 4); // merkle-tail
  210. memcpy(&my_buf[0x10], work->midstate, 0x20);
  211. if (work->work_difficulty >= MINERGATE_MAX_NONCE_DIFF)
  212. work->nonce_diff = MINERGATE_MAX_NONCE_DIFF;
  213. else
  214. work->nonce_diff = work->work_difficulty;
  215. const uint16_t zerobits = log2(floor(work->nonce_diff * 4294967296));
  216. work->nonce_diff = pow(2, zerobits) / 4294967296;
  217. pk_u8(my_buf, 0x30, zerobits);
  218. pk_u8(my_buf, 0x31, 0); // ntime limit
  219. pk_u8(my_buf, 0x32, 0); // ntime offset
  220. pk_u8(my_buf, 0x33, 0); // reserved
  221. HASH_ADD(hh, thr->work, device_id, sizeof(work->device_id), work);
  222. LL_PREPEND(thr->work_list, work);
  223. timer_set_delay_from_now(&thr->tv_poll, 0);
  224. minergate_queue_full(thr);
  225. return true;
  226. }
  227. static
  228. void minergate_queue_flush(struct thr_info * const thr)
  229. {
  230. struct minergate_state * const state = thr->cgpu_data;
  231. // TODO: prune state->ready_to_queue
  232. state->req_buffer[3] |= MRPF_FLUSH;
  233. timer_set_delay_from_now(&thr->tv_poll, 0);
  234. }
  235. static
  236. void minergate_poll(struct thr_info * const thr)
  237. {
  238. struct cgpu_info * const dev = thr->cgpu;
  239. struct minergate_state * const state = thr->cgpu_data;
  240. const int fd = dev->device_fd;
  241. if (opt_dev_protocol || state->ready_to_queue)
  242. applog(LOG_DEBUG, "%s: Polling with %u new jobs", dev->dev_repr, state->ready_to_queue);
  243. pk_u16le(state->req_buffer, 6, state->ready_to_queue);
  244. if (MINERGATE_PKT_REQ_SZ != write(fd, state->req_buffer, MINERGATE_PKT_REQ_SZ))
  245. return_via_applog(err, , LOG_ERR, "%s: write incomplete or failed", dev->dev_repr);
  246. state->req_buffer[3] = 0;
  247. state->ready_to_queue = 0;
  248. thr->work_list = NULL;
  249. uint8_t buf[MINERGATE_PKT_RSP_SZ];
  250. if (minergate_read(fd, buf, MINERGATE_PKT_RSP_SZ) != MINERGATE_PKT_RSP_SZ)
  251. return_via_applog(err, , LOG_ERR, "%s: %s failed", dev->dev_repr, "read");
  252. if (upk_u8(buf, 2) != MINERGATE_PROTOCOL_VER || upk_u16le(buf, 4) != MINERGATE_MAGIC)
  253. return_via_applog(err, , LOG_ERR, "%s: Protocol mismatch", dev->dev_repr);
  254. uint8_t *jobrsp = &buf[MINERGATE_PKT_HEADER_SZ];
  255. struct work *work;
  256. uint16_t rsp_count = upk_u16le(buf, 6);
  257. if (rsp_count || opt_dev_protocol)
  258. applog(LOG_DEBUG, "%s: Received %u job completions", dev->dev_repr, rsp_count);
  259. uint32_t nonce;
  260. int64_t hashes = 0;
  261. for (unsigned i = 0; i < rsp_count; ++i, (jobrsp += MINERGATE_PKT_RSP_ITEM_SZ))
  262. {
  263. work_device_id_t jobid = upk_u32be(jobrsp, 0);
  264. nonce = upk_u32le(jobrsp, 8);
  265. HASH_FIND(hh, thr->work, &jobid, sizeof(jobid), work);
  266. if (!work)
  267. {
  268. applog(LOG_ERR, "%s: Unknown job %"PRIwdi, dev->dev_repr, jobid);
  269. if (nonce)
  270. {
  271. inc_hw_errors3(thr, NULL, &nonce, 1.);
  272. nonce = upk_u32le(jobrsp, 0xc);
  273. if (nonce)
  274. inc_hw_errors3(thr, NULL, &nonce, 1.);
  275. }
  276. else
  277. inc_hw_errors_only(thr);
  278. continue;
  279. }
  280. if (nonce)
  281. {
  282. submit_nonce(thr, work, nonce);
  283. nonce = upk_u32be(jobrsp, 0xc);
  284. if (nonce)
  285. submit_nonce(thr, work, nonce);
  286. }
  287. HASH_DEL(thr->work, work);
  288. hashes += 100000000 * work->nonce_diff;
  289. free_work(work);
  290. }
  291. hashes_done2(thr, hashes, NULL);
  292. minergate_queue_full(thr);
  293. timer_set_delay_from_now(&thr->tv_poll, MINERGATE_POLL_US);
  294. return;
  295. err:
  296. // TODO: reconnect
  297. timer_set_delay_from_now(&thr->tv_poll, MINERGATE_RETRY_US);
  298. }
  299. static
  300. bool minergate_get_stats(struct cgpu_info * const dev)
  301. {
  302. static const int skip_stats = 1;
  303. struct thr_info * const thr = dev->thr[0];
  304. struct minergate_state * const state = thr->cgpu_data;
  305. FILE *F = fopen(minergate_stats_file, "r");
  306. char buf[0x100];
  307. if (F)
  308. {
  309. char *p = fgets(buf, sizeof(buf), F);
  310. fclose(F);
  311. if (p)
  312. {
  313. long nums[0x80];
  314. char *endptr;
  315. unsigned i;
  316. float max_temp = 0;
  317. for (i = 0; 1; ++i)
  318. {
  319. if (!p[0])
  320. break;
  321. nums[i] = strtol(p, &endptr, 0);
  322. if (p == endptr)
  323. break;
  324. if (i >= skip_stats && nums[i] > max_temp)
  325. max_temp = nums[i];
  326. while (endptr[0] && !isspace(endptr[0]))
  327. ++endptr;
  328. while (endptr[0] && isspace(endptr[0]))
  329. ++endptr;
  330. p = endptr;
  331. }
  332. i -= skip_stats;
  333. long *new_stats = malloc(sizeof(*state->stats) * i);
  334. memcpy(new_stats, &nums[skip_stats], sizeof(*nums) * i);
  335. mutex_lock(&dev->device_mutex);
  336. free(state->stats);
  337. state->stats = new_stats;
  338. state->stats_count = i;
  339. mutex_unlock(&dev->device_mutex);
  340. dev->temp = max_temp;
  341. }
  342. }
  343. return true;
  344. }
  345. static
  346. struct api_data *minergate_api_extra_device_status(struct cgpu_info * const dev)
  347. {
  348. struct thr_info * const thr = dev->thr[0];
  349. struct minergate_state * const state = thr->cgpu_data;
  350. struct api_data *root = NULL;
  351. mutex_lock(&dev->device_mutex);
  352. if (state->stats_count > 1)
  353. {
  354. char buf[0x10];
  355. for (unsigned i = 0; i < state->stats_count; ++i)
  356. {
  357. float temp = state->stats[i];
  358. if (!temp)
  359. continue;
  360. sprintf(buf, "Temperature%u", i);
  361. root = api_add_temp(root, buf, &temp, true);
  362. }
  363. }
  364. mutex_unlock(&dev->device_mutex);
  365. return root;
  366. }
  367. struct device_drv minergate_drv = {
  368. .dname = "minergate",
  369. .name = "MGT",
  370. .drv_detect = minergate_detect,
  371. .thread_init = minergate_init,
  372. .minerloop = minerloop_queue,
  373. .queue_append = minergate_queue_append,
  374. .queue_flush = minergate_queue_flush,
  375. .poll = minergate_poll,
  376. .get_stats = minergate_get_stats,
  377. .get_api_extra_device_status = minergate_api_extra_device_status,
  378. };