driver-minergate.c 18 KB

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