driver-minergate.c 17 KB

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