driver-minergate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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(const char * const devpath)
  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 = MPV_SP10,
  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. int minergate_detect_auto(void)
  251. {
  252. return minergate_detect_one("/tmp/connection_pipe") ? 1 : 0;
  253. }
  254. static
  255. void minergate_detect(void)
  256. {
  257. generic_detect(&minergate_drv, minergate_detect_one, minergate_detect_auto, 0);
  258. }
  259. static
  260. bool minergate_init(struct thr_info * const thr)
  261. {
  262. struct cgpu_info * const dev = thr->cgpu;
  263. struct minergate_config * const mgcfg = dev->device_data;
  264. const int fd = minergate_open(dev->device_path);
  265. dev->device_fd = fd;
  266. if (fd < 0)
  267. applogr(false, LOG_ERR, "%s: Cannot connect", dev->dev_repr);
  268. struct minergate_state * const state = malloc(sizeof(*state) + mgcfg->pkt_req_sz);
  269. if (!state)
  270. applogr(false, LOG_ERR, "%s: %s failed", dev->dev_repr, "malloc");
  271. *state = (struct minergate_state){
  272. .req_buffer = (void*)&state[1]
  273. };
  274. thr->cgpu_data = state;
  275. thr->work = thr->work_list = NULL;
  276. mutex_init(&dev->device_mutex);
  277. memset(state->req_buffer, 0, mgcfg->pkt_req_sz);
  278. pk_u8(state->req_buffer, 2, mgcfg->protover);
  279. state->req_buffer[3] = MRPF_FIRST | MRPF_FLUSH;
  280. pk_u16le(state->req_buffer, 4, MINERGATE_MAGIC);
  281. timer_set_delay_from_now(&thr->tv_poll, 0);
  282. return true;
  283. }
  284. static
  285. bool minergate_queue_full(struct thr_info * const thr)
  286. {
  287. struct cgpu_info * const dev = thr->cgpu;
  288. struct minergate_config * const mgcfg = dev->device_data;
  289. struct minergate_state * const state = thr->cgpu_data;
  290. bool qf;
  291. if (HASH_COUNT(thr->work) + state->ready_to_queue >= mgcfg->queue)
  292. qf = true;
  293. else
  294. if (state->ready_to_queue >= mgcfg->n_req_queue)
  295. qf = true;
  296. else
  297. if (state->req_buffer[3] & MRPF_FLUSH)
  298. // Job flush occurs after new jobs get queued, so we have to wait until it completes
  299. qf = true;
  300. else
  301. qf = false;
  302. thr->queue_full = qf;
  303. return qf;
  304. }
  305. static
  306. bool minergate_queue_append(struct thr_info * const thr, struct work * const work)
  307. {
  308. struct cgpu_info * const dev = thr->cgpu;
  309. struct minergate_state * const state = thr->cgpu_data;
  310. if (minergate_queue_full(thr))
  311. return false;
  312. work->device_id = (uint32_t)(state->next_jobid++);
  313. work->tv_stamp.tv_sec = 0;
  314. uint8_t * const my_buf = &state->req_buffer[MINERGATE_PKT_HEADER_SZ + (MINERGATE_PKT_REQ_ITEM_SZ * state->ready_to_queue++)];
  315. pk_u32be(my_buf, 0, work->device_id);
  316. memcpy(&my_buf[ 4], &work->data[0x48], 4); // nbits
  317. memcpy(&my_buf[ 8], &work->data[0x44], 4); // ntime
  318. memcpy(&my_buf[0x0c], &work->data[0x40], 4); // merkle-tail
  319. memcpy(&my_buf[0x10], work->midstate, 0x20);
  320. if (work->work_difficulty >= MINERGATE_MAX_NONCE_DIFF)
  321. work->nonce_diff = MINERGATE_MAX_NONCE_DIFF;
  322. else
  323. work->nonce_diff = work->work_difficulty;
  324. const uint16_t zerobits = log2(floor(work->nonce_diff * 4294967296));
  325. work->nonce_diff = pow(2, zerobits) / 4294967296;
  326. pk_u8(my_buf, 0x30, zerobits);
  327. pk_u8(my_buf, 0x31, 0); // ntime limit
  328. pk_u8(my_buf, 0x32, 0); // pv6: ntime offset ; pv30: reserved
  329. pk_u8(my_buf, 0x33, 0); // reserved
  330. struct work *oldwork;
  331. HASH_FIND(hh, thr->work, &work->device_id, sizeof(work->device_id), oldwork);
  332. if (unlikely(oldwork))
  333. {
  334. applog(LOG_WARNING, "%s: Reusing allocated device id %"PRIwdi, dev->dev_repr, work->device_id);
  335. HASH_DEL(thr->work, oldwork);
  336. free_work(oldwork);
  337. }
  338. HASH_ADD(hh, thr->work, device_id, sizeof(work->device_id), work);
  339. LL_PREPEND(thr->work_list, work);
  340. timer_set_delay_from_now(&thr->tv_poll, 0);
  341. minergate_queue_full(thr);
  342. return true;
  343. }
  344. static
  345. void minergate_queue_flush(struct thr_info * const thr)
  346. {
  347. struct minergate_state * const state = thr->cgpu_data;
  348. struct work *work, *worktmp;
  349. // Flush internal ready-to-queue list
  350. LL_FOREACH_SAFE(thr->work_list, work, worktmp)
  351. {
  352. HASH_DEL(thr->work, work);
  353. LL_DELETE(thr->work_list, work);
  354. free_work(work);
  355. }
  356. state->ready_to_queue = 0;
  357. // Trigger minergate flush
  358. state->req_buffer[3] |= MRPF_FLUSH;
  359. timer_set_delay_from_now(&thr->tv_poll, 0);
  360. }
  361. static
  362. bool minergate_submit(struct thr_info * const thr, struct work * const work, const uint32_t nonce)
  363. {
  364. if (!nonce)
  365. return false;
  366. if (likely(work))
  367. submit_nonce(thr, work, nonce);
  368. else
  369. inc_hw_errors3(thr, NULL, &nonce, 1.);
  370. return true;
  371. }
  372. static
  373. void minergate_poll(struct thr_info * const thr)
  374. {
  375. struct cgpu_info * const dev = thr->cgpu;
  376. struct minergate_config * const mgcfg = dev->device_data;
  377. struct minergate_state * const state = thr->cgpu_data;
  378. const int fd = dev->device_fd;
  379. uint8_t buf[mgcfg->pkt_rsp_sz];
  380. if (opt_dev_protocol || state->ready_to_queue)
  381. applog(LOG_DEBUG, "%s: Polling with %u new jobs", dev->dev_repr, state->ready_to_queue);
  382. pk_u16le(state->req_buffer, 6, state->ready_to_queue);
  383. if (mgcfg->pkt_req_sz != write(fd, state->req_buffer, mgcfg->pkt_req_sz))
  384. return_via_applog(err, , LOG_ERR, "%s: write incomplete or failed", dev->dev_repr);
  385. uint8_t flags = state->req_buffer[3];
  386. state->req_buffer[3] = 0;
  387. state->ready_to_queue = 0;
  388. thr->work_list = NULL;
  389. if (minergate_read(fd, buf, mgcfg->pkt_rsp_sz) != mgcfg->pkt_rsp_sz)
  390. return_via_applog(err, , LOG_ERR, "%s: %s failed", dev->dev_repr, "read");
  391. if (upk_u8(buf, 2) != mgcfg->protover || upk_u16le(buf, 4) != MINERGATE_MAGIC)
  392. return_via_applog(err, , LOG_ERR, "%s: Protocol mismatch", dev->dev_repr);
  393. uint8_t *jobrsp = &buf[MINERGATE_PKT_HEADER_SZ];
  394. struct work *work;
  395. uint16_t rsp_count = upk_u16le(buf, 6);
  396. if (rsp_count || opt_dev_protocol)
  397. applog(LOG_DEBUG, "%s: Received %u job completions", dev->dev_repr, rsp_count);
  398. uint32_t nonce;
  399. int64_t hashes = 0;
  400. for (unsigned i = 0; i < rsp_count; ++i, (jobrsp += mgcfg->pkt_rsp_item_sz))
  401. {
  402. work_device_id_t jobid = upk_u32be(jobrsp, 0);
  403. nonce = upk_u32le(jobrsp, 8);
  404. HASH_FIND(hh, thr->work, &jobid, sizeof(jobid), work);
  405. if (unlikely(!work))
  406. applog(LOG_ERR, "%s: Unknown job %"PRIwdi, dev->dev_repr, jobid);
  407. if (minergate_submit(thr, work, nonce))
  408. {
  409. if (mgcfg->protover == MPV_SP10)
  410. {
  411. nonce = upk_u32le(jobrsp, 0xc);
  412. minergate_submit(thr, work, nonce);
  413. }
  414. }
  415. else
  416. if (unlikely(!work))
  417. // Increment HW errors even if no nonce to submit
  418. inc_hw_errors_only(thr);
  419. const bool work_completed = (mgcfg->protover == MPV_SP10) ? (bool)work : (bool)jobrsp[0xe];
  420. if (work_completed)
  421. {
  422. HASH_DEL(thr->work, work);
  423. applog(LOG_DEBUG, "%s: %s job %"PRIwdi" completed", dev->dev_repr, work->tv_stamp.tv_sec ? "Flushed" : "Active", work->device_id);
  424. if (!work->tv_stamp.tv_sec)
  425. hashes += 100000000 * work->nonce_diff;
  426. free_work(work);
  427. }
  428. }
  429. hashes_done2(thr, hashes, NULL);
  430. if (flags & MRPF_FLUSH)
  431. {
  432. // Mark all remaining jobs as flushed so we don't count them in hashes_done
  433. struct work *worktmp;
  434. HASH_ITER(hh, thr->work, work, worktmp)
  435. {
  436. work->tv_stamp.tv_sec = 1;
  437. }
  438. }
  439. minergate_queue_full(thr);
  440. timer_set_delay_from_now(&thr->tv_poll, MINERGATE_POLL_US);
  441. return;
  442. err:
  443. // TODO: reconnect
  444. timer_set_delay_from_now(&thr->tv_poll, MINERGATE_RETRY_US);
  445. }
  446. static
  447. bool minergate_get_stats(struct cgpu_info * const dev)
  448. {
  449. static const int skip_stats = 1;
  450. struct minergate_config * const mgcfg = dev->device_data;
  451. struct thr_info * const thr = dev->thr[0];
  452. struct minergate_state * const state = thr->cgpu_data;
  453. if (!(mgcfg->stats_file && mgcfg->stats_file[0]))
  454. return true;
  455. FILE *F = fopen(mgcfg->stats_file, "r");
  456. char buf[0x100];
  457. if (F)
  458. {
  459. char *p = fgets(buf, sizeof(buf), F);
  460. fclose(F);
  461. if (p)
  462. {
  463. long nums[0x80];
  464. char *endptr;
  465. unsigned i;
  466. float max_temp = 0;
  467. for (i = 0; 1; ++i)
  468. {
  469. if (!p[0])
  470. break;
  471. nums[i] = strtol(p, &endptr, 0);
  472. if (p == endptr)
  473. break;
  474. if (i >= skip_stats && nums[i] > max_temp)
  475. max_temp = nums[i];
  476. while (endptr[0] && !isspace(endptr[0]))
  477. ++endptr;
  478. while (endptr[0] && isspace(endptr[0]))
  479. ++endptr;
  480. p = endptr;
  481. }
  482. i -= skip_stats;
  483. long *new_stats = malloc(sizeof(*state->stats) * i);
  484. memcpy(new_stats, &nums[skip_stats], sizeof(*nums) * i);
  485. mutex_lock(&dev->device_mutex);
  486. free(state->stats);
  487. state->stats = new_stats;
  488. state->stats_count = i;
  489. mutex_unlock(&dev->device_mutex);
  490. dev->temp = max_temp;
  491. }
  492. }
  493. return true;
  494. }
  495. static
  496. struct api_data *minergate_api_extra_device_status(struct cgpu_info * const dev)
  497. {
  498. struct thr_info * const thr = dev->thr[0];
  499. struct minergate_state * const state = thr->cgpu_data;
  500. struct api_data *root = NULL;
  501. mutex_lock(&dev->device_mutex);
  502. if (state->stats_count > 1)
  503. {
  504. char buf[0x10];
  505. for (unsigned i = 0; i < state->stats_count; ++i)
  506. {
  507. float temp = state->stats[i];
  508. if (!temp)
  509. continue;
  510. sprintf(buf, "Temperature%u", i);
  511. root = api_add_temp(root, buf, &temp, true);
  512. }
  513. }
  514. mutex_unlock(&dev->device_mutex);
  515. return root;
  516. }
  517. struct device_drv minergate_drv = {
  518. .dname = "minergate",
  519. .name = "MGT",
  520. .drv_detect = minergate_detect,
  521. .thread_init = minergate_init,
  522. .minerloop = minerloop_queue,
  523. .queue_append = minergate_queue_append,
  524. .queue_flush = minergate_queue_flush,
  525. .poll = minergate_poll,
  526. .get_stats = minergate_get_stats,
  527. .get_api_extra_device_status = minergate_api_extra_device_status,
  528. };