driver-minergate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 "lowlevel.h"
  27. #include "miner.h"
  28. #define MINERGATE_MAX_NONCE_DIFF 0x20
  29. static const char * const minergate_stats_file = "/var/run/mg_rate_temp";
  30. #define MINERGATE_PROTOCOL_VER 6
  31. #define MINERGATE_MAGIC 0xcaf4
  32. static const int minergate_max_responses = 300;
  33. #define MINERGATE_PKT_HEADER_SZ 8
  34. #define MINERGATE_PKT_REQ_ITEM_SZ 0x34
  35. #define MINERGATE_PKT_REQ_MAX 100
  36. #define MINERGATE_PKT_RSP_ITEM_SZ 0x14
  37. #define MINERGATE_PKT_RSP_MAX 300
  38. #define MINERGATE_POLL_US 100000
  39. #define MINERGATE_RETRY_US 5000000
  40. #define MINERGATE_PKT_REQ_SZ (MINERGATE_PKT_HEADER_SZ + (MINERGATE_PKT_REQ_ITEM_SZ * MINERGATE_PKT_REQ_MAX))
  41. #define MINERGATE_PKT_RSP_SZ (MINERGATE_PKT_HEADER_SZ + (MINERGATE_PKT_RSP_ITEM_SZ * MINERGATE_PKT_RSP_MAX))
  42. BFG_REGISTER_DRIVER(minergate_drv)
  43. enum minergate_reqpkt_flags {
  44. MRPF_FIRST = 1,
  45. MRPF_FLUSH = 2,
  46. };
  47. struct minergate_state {
  48. work_device_id_t next_jobid;
  49. unsigned ready_to_queue;
  50. uint8_t *req_buffer;
  51. long *stats;
  52. unsigned stats_count;
  53. struct work *flushed_work;
  54. };
  55. static
  56. int minergate_open(const char * const devpath)
  57. {
  58. size_t devpath_len = strlen(devpath);
  59. struct sockaddr_un sa = {
  60. .sun_family = AF_UNIX,
  61. };
  62. #ifdef UNIX_PATH_MAX
  63. if (devpath_len >= UNIX_PATH_MAX)
  64. #else
  65. if (devpath_len >= sizeof(sa.sun_path))
  66. #endif
  67. return -1;
  68. const int fd = socket(PF_UNIX, SOCK_STREAM, 0);
  69. strcpy(sa.sun_path, devpath);
  70. if (connect(fd, &sa, sizeof(sa)))
  71. {
  72. close(fd);
  73. return -1;
  74. }
  75. return fd;
  76. }
  77. static
  78. ssize_t minergate_read(const int fd, void * const buf_p, size_t bufLen)
  79. {
  80. uint8_t *buf = buf_p;
  81. ssize_t rv, ret = 0;
  82. while (bufLen > 0)
  83. {
  84. rv = read(fd, buf, bufLen);
  85. if (rv <= 0)
  86. {
  87. if (ret > 0)
  88. return ret;
  89. return rv;
  90. }
  91. buf += rv;
  92. bufLen -= rv;
  93. ret += rv;
  94. }
  95. return ret;
  96. }
  97. static
  98. bool minergate_detect_one(const char * const devpath)
  99. {
  100. bool rv = false;
  101. const int fd = minergate_open(devpath);
  102. if (unlikely(fd < 0))
  103. applogr(false, LOG_DEBUG, "%s: %s: Cannot connect", minergate_drv.dname, devpath);
  104. int epfd = -1;
  105. uint8_t buf[MINERGATE_PKT_REQ_SZ] = {0xbf, 0x90, MINERGATE_PROTOCOL_VER, MRPF_FIRST, 0,0, 0 /* req count */,};
  106. pk_u16le(buf, 4, MINERGATE_MAGIC);
  107. if (MINERGATE_PKT_REQ_SZ != write(fd, buf, MINERGATE_PKT_REQ_SZ))
  108. return_via_applog(out, , LOG_DEBUG, "%s: %s: write incomplete or failed", minergate_drv.dname, devpath);
  109. epfd = epoll_create(1);
  110. if (epfd < 0)
  111. return_via_applog(out, , LOG_DEBUG, "%s: %s: %s failed", minergate_drv.dname, devpath, "epoll_create");
  112. struct epoll_event eev;
  113. eev.events = EPOLLIN;
  114. if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &eev))
  115. return_via_applog(out, , LOG_DEBUG, "%s: %s: %s failed", minergate_drv.dname, devpath, "epoll_ctl");
  116. size_t read_bytes = 0;
  117. static const size_t read_expect = MINERGATE_PKT_HEADER_SZ;
  118. ssize_t r;
  119. while (read_bytes < read_expect)
  120. {
  121. if (epoll_wait(epfd, &eev, 1, 1000) != 1)
  122. return_via_applog(out, , LOG_DEBUG, "%s: %s: Timeout waiting for response", minergate_drv.dname, devpath);
  123. r = read(fd, &buf[read_bytes], read_expect - read_bytes);
  124. if (r <= 0)
  125. return_via_applog(out, , LOG_DEBUG, "%s: %s: %s failed", minergate_drv.dname, devpath, "read");
  126. read_bytes += r;
  127. }
  128. if (buf[1] != 0x90)
  129. return_via_applog(out, , LOG_DEBUG, "%s: %s: %s mismatch", minergate_drv.dname, devpath, "request_id");
  130. if (buf[2] != MINERGATE_PROTOCOL_VER)
  131. return_via_applog(out, , LOG_DEBUG, "%s: %s: %s mismatch", minergate_drv.dname, devpath, "Protocol version");
  132. if (upk_u16le(buf, 4) != MINERGATE_MAGIC)
  133. return_via_applog(out, , LOG_DEBUG, "%s: %s: %s mismatch", minergate_drv.dname, devpath, "magic");
  134. uint16_t responses = upk_u16le(buf, 6);
  135. if (responses > minergate_max_responses)
  136. return_via_applog(out, , LOG_DEBUG, "%s: %s: More than maximum responses", minergate_drv.dname, devpath);
  137. if (bfg_claim_any2(&minergate_drv, devpath, "unix", devpath))
  138. goto out;
  139. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  140. *cgpu = (struct cgpu_info){
  141. .drv = &minergate_drv,
  142. .device_path = strdup(devpath),
  143. .deven = DEV_ENABLED,
  144. .threads = 1,
  145. };
  146. rv = add_cgpu(cgpu);
  147. out:
  148. close(fd);
  149. if (epfd >= 0)
  150. close(epfd);
  151. return rv;
  152. }
  153. static
  154. int minergate_detect_auto(void)
  155. {
  156. return minergate_detect_one("/tmp/connection_pipe") ? 1 : 0;
  157. }
  158. static
  159. void minergate_detect(void)
  160. {
  161. generic_detect(&minergate_drv, minergate_detect_one, minergate_detect_auto, 0);
  162. }
  163. static
  164. bool minergate_init(struct thr_info * const thr)
  165. {
  166. struct cgpu_info * const dev = thr->cgpu;
  167. const int fd = minergate_open(dev->device_path);
  168. dev->device_fd = fd;
  169. if (fd < 0)
  170. applogr(false, LOG_ERR, "%s: Cannot connect", dev->dev_repr);
  171. struct minergate_state * const state = malloc(sizeof(*state) + MINERGATE_PKT_REQ_SZ);
  172. if (!state)
  173. applogr(false, LOG_ERR, "%s: %s failed", dev->dev_repr, "malloc");
  174. *state = (struct minergate_state){
  175. .req_buffer = (void*)&state[1]
  176. };
  177. thr->cgpu_data = state;
  178. thr->work = thr->work_list = NULL;
  179. mutex_init(&dev->device_mutex);
  180. memset(state->req_buffer, 0, MINERGATE_PKT_REQ_SZ);
  181. pk_u8(state->req_buffer, 2, MINERGATE_PROTOCOL_VER);
  182. state->req_buffer[3] = MRPF_FIRST | MRPF_FLUSH;
  183. pk_u16le(state->req_buffer, 4, MINERGATE_MAGIC);
  184. timer_set_delay_from_now(&thr->tv_poll, 0);
  185. return true;
  186. }
  187. static
  188. bool minergate_queue_full(struct thr_info * const thr)
  189. {
  190. static const unsigned max_minergate_jobs = 300, max_requests = 100;
  191. struct minergate_state * const state = thr->cgpu_data;
  192. bool qf;
  193. if (HASH_COUNT(thr->work) + state->ready_to_queue >= max_minergate_jobs)
  194. qf = true;
  195. else
  196. if (state->ready_to_queue >= max_requests)
  197. qf = true;
  198. else
  199. if (state->req_buffer[3] & MRPF_FLUSH)
  200. // Job flush occurs after new jobs get queued, so we have to wait until it completes
  201. qf = true;
  202. else
  203. qf = false;
  204. thr->queue_full = qf;
  205. return qf;
  206. }
  207. static
  208. bool minergate_queue_append(struct thr_info * const thr, struct work * const work)
  209. {
  210. struct cgpu_info * const dev = thr->cgpu;
  211. struct minergate_state * const state = thr->cgpu_data;
  212. if (minergate_queue_full(thr))
  213. return false;
  214. work->device_id = (uint32_t)(state->next_jobid++);
  215. work->tv_stamp.tv_sec = 0;
  216. uint8_t * const my_buf = &state->req_buffer[MINERGATE_PKT_HEADER_SZ + (MINERGATE_PKT_REQ_ITEM_SZ * state->ready_to_queue++)];
  217. pk_u32be(my_buf, 0, work->device_id);
  218. memcpy(&my_buf[ 4], &work->data[0x48], 4); // nbits
  219. memcpy(&my_buf[ 8], &work->data[0x44], 4); // ntime
  220. memcpy(&my_buf[0x0c], &work->data[0x40], 4); // merkle-tail
  221. memcpy(&my_buf[0x10], work->midstate, 0x20);
  222. if (work->work_difficulty >= MINERGATE_MAX_NONCE_DIFF)
  223. work->nonce_diff = MINERGATE_MAX_NONCE_DIFF;
  224. else
  225. work->nonce_diff = work->work_difficulty;
  226. const uint16_t zerobits = log2(floor(work->nonce_diff * 4294967296));
  227. work->nonce_diff = pow(2, zerobits) / 4294967296;
  228. pk_u8(my_buf, 0x30, zerobits);
  229. pk_u8(my_buf, 0x31, 0); // ntime limit
  230. pk_u8(my_buf, 0x32, 0); // ntime offset
  231. pk_u8(my_buf, 0x33, 0); // reserved
  232. struct work *oldwork;
  233. HASH_FIND(hh, thr->work, &work->device_id, sizeof(work->device_id), oldwork);
  234. if (unlikely(oldwork))
  235. {
  236. applog(LOG_WARNING, "%s: Reusing allocated device id %"PRIwdi, dev->dev_repr, work->device_id);
  237. HASH_DEL(thr->work, oldwork);
  238. free_work(oldwork);
  239. }
  240. HASH_ADD(hh, thr->work, device_id, sizeof(work->device_id), work);
  241. LL_PREPEND(thr->work_list, work);
  242. timer_set_delay_from_now(&thr->tv_poll, 0);
  243. minergate_queue_full(thr);
  244. return true;
  245. }
  246. static
  247. void minergate_queue_flush(struct thr_info * const thr)
  248. {
  249. struct minergate_state * const state = thr->cgpu_data;
  250. struct work *work, *worktmp;
  251. // Flush internal ready-to-queue list
  252. LL_FOREACH_SAFE(thr->work_list, work, worktmp)
  253. {
  254. HASH_DEL(thr->work, work);
  255. LL_DELETE(thr->work_list, work);
  256. free_work(work);
  257. }
  258. state->ready_to_queue = 0;
  259. // Trigger minergate flush
  260. state->req_buffer[3] |= MRPF_FLUSH;
  261. timer_set_delay_from_now(&thr->tv_poll, 0);
  262. }
  263. static
  264. void minergate_poll(struct thr_info * const thr)
  265. {
  266. struct cgpu_info * const dev = thr->cgpu;
  267. struct minergate_state * const state = thr->cgpu_data;
  268. const int fd = dev->device_fd;
  269. if (opt_dev_protocol || state->ready_to_queue)
  270. applog(LOG_DEBUG, "%s: Polling with %u new jobs", dev->dev_repr, state->ready_to_queue);
  271. pk_u16le(state->req_buffer, 6, state->ready_to_queue);
  272. if (MINERGATE_PKT_REQ_SZ != write(fd, state->req_buffer, MINERGATE_PKT_REQ_SZ))
  273. return_via_applog(err, , LOG_ERR, "%s: write incomplete or failed", dev->dev_repr);
  274. uint8_t flags = state->req_buffer[3];
  275. state->req_buffer[3] = 0;
  276. state->ready_to_queue = 0;
  277. thr->work_list = NULL;
  278. uint8_t buf[MINERGATE_PKT_RSP_SZ];
  279. if (minergate_read(fd, buf, MINERGATE_PKT_RSP_SZ) != MINERGATE_PKT_RSP_SZ)
  280. return_via_applog(err, , LOG_ERR, "%s: %s failed", dev->dev_repr, "read");
  281. if (upk_u8(buf, 2) != MINERGATE_PROTOCOL_VER || upk_u16le(buf, 4) != MINERGATE_MAGIC)
  282. return_via_applog(err, , LOG_ERR, "%s: Protocol mismatch", dev->dev_repr);
  283. uint8_t *jobrsp = &buf[MINERGATE_PKT_HEADER_SZ];
  284. struct work *work;
  285. uint16_t rsp_count = upk_u16le(buf, 6);
  286. if (rsp_count || opt_dev_protocol)
  287. applog(LOG_DEBUG, "%s: Received %u job completions", dev->dev_repr, rsp_count);
  288. uint32_t nonce;
  289. int64_t hashes = 0;
  290. for (unsigned i = 0; i < rsp_count; ++i, (jobrsp += MINERGATE_PKT_RSP_ITEM_SZ))
  291. {
  292. work_device_id_t jobid = upk_u32be(jobrsp, 0);
  293. nonce = upk_u32le(jobrsp, 8);
  294. HASH_FIND(hh, thr->work, &jobid, sizeof(jobid), work);
  295. if (!work)
  296. {
  297. applog(LOG_ERR, "%s: Unknown job %"PRIwdi, dev->dev_repr, jobid);
  298. if (nonce)
  299. {
  300. inc_hw_errors3(thr, NULL, &nonce, 1.);
  301. nonce = upk_u32le(jobrsp, 0xc);
  302. if (nonce)
  303. inc_hw_errors3(thr, NULL, &nonce, 1.);
  304. }
  305. else
  306. inc_hw_errors_only(thr);
  307. continue;
  308. }
  309. if (nonce)
  310. {
  311. submit_nonce(thr, work, nonce);
  312. nonce = upk_u32be(jobrsp, 0xc);
  313. if (nonce)
  314. submit_nonce(thr, work, nonce);
  315. }
  316. HASH_DEL(thr->work, work);
  317. applog(LOG_DEBUG, "%s: %s job %"PRIwdi" completed", dev->dev_repr, work->tv_stamp.tv_sec ? "Flushed" : "Active", work->device_id);
  318. if (!work->tv_stamp.tv_sec)
  319. hashes += 100000000 * work->nonce_diff;
  320. free_work(work);
  321. }
  322. hashes_done2(thr, hashes, NULL);
  323. if (flags & MRPF_FLUSH)
  324. {
  325. // Mark all remaining jobs as flushed so we don't count them in hashes_done
  326. struct work *worktmp;
  327. HASH_ITER(hh, thr->work, work, worktmp)
  328. {
  329. work->tv_stamp.tv_sec = 1;
  330. }
  331. }
  332. minergate_queue_full(thr);
  333. timer_set_delay_from_now(&thr->tv_poll, MINERGATE_POLL_US);
  334. return;
  335. err:
  336. // TODO: reconnect
  337. timer_set_delay_from_now(&thr->tv_poll, MINERGATE_RETRY_US);
  338. }
  339. static
  340. bool minergate_get_stats(struct cgpu_info * const dev)
  341. {
  342. static const int skip_stats = 1;
  343. struct thr_info * const thr = dev->thr[0];
  344. struct minergate_state * const state = thr->cgpu_data;
  345. FILE *F = fopen(minergate_stats_file, "r");
  346. char buf[0x100];
  347. if (F)
  348. {
  349. char *p = fgets(buf, sizeof(buf), F);
  350. fclose(F);
  351. if (p)
  352. {
  353. long nums[0x80];
  354. char *endptr;
  355. int i;
  356. float max_temp = 0;
  357. for (i = 0; 1; ++i)
  358. {
  359. if (!p[0])
  360. break;
  361. nums[i] = strtol(p, &endptr, 0);
  362. if (p == endptr)
  363. break;
  364. if (i >= skip_stats && nums[i] > max_temp)
  365. max_temp = nums[i];
  366. while (endptr[0] && !isspace(endptr[0]))
  367. ++endptr;
  368. while (endptr[0] && isspace(endptr[0]))
  369. ++endptr;
  370. p = endptr;
  371. }
  372. i -= skip_stats;
  373. long *new_stats;
  374. if (likely(i > 0))
  375. {
  376. new_stats = malloc(sizeof(*state->stats) * i);
  377. memcpy(new_stats, &nums[skip_stats], sizeof(*nums) * i);
  378. }
  379. else
  380. new_stats = NULL;
  381. mutex_lock(&dev->device_mutex);
  382. free(state->stats);
  383. state->stats = new_stats;
  384. state->stats_count = i;
  385. mutex_unlock(&dev->device_mutex);
  386. dev->temp = max_temp;
  387. }
  388. }
  389. return true;
  390. }
  391. static
  392. struct api_data *minergate_api_extra_device_status(struct cgpu_info * const dev)
  393. {
  394. struct thr_info * const thr = dev->thr[0];
  395. struct minergate_state * const state = thr->cgpu_data;
  396. struct api_data *root = NULL;
  397. mutex_lock(&dev->device_mutex);
  398. if (state->stats_count > 1)
  399. {
  400. char buf[0x10];
  401. for (unsigned i = 0; i < state->stats_count; ++i)
  402. {
  403. float temp = state->stats[i];
  404. if (!temp)
  405. continue;
  406. sprintf(buf, "Temperature%u", i);
  407. root = api_add_temp(root, buf, &temp, true);
  408. }
  409. }
  410. mutex_unlock(&dev->device_mutex);
  411. return root;
  412. }
  413. struct device_drv minergate_drv = {
  414. .dname = "minergate",
  415. .name = "MGT",
  416. .drv_detect = minergate_detect,
  417. .thread_init = minergate_init,
  418. .minerloop = minerloop_queue,
  419. .queue_append = minergate_queue_append,
  420. .queue_flush = minergate_queue_flush,
  421. .poll = minergate_poll,
  422. .get_stats = minergate_get_stats,
  423. .get_api_extra_device_status = minergate_api_extra_device_status,
  424. };