driver-minergate.c 14 KB

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