driver-spondoolies-sp10.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright 2014 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2014 Zvi (Zvisha) Shteingart - Spondoolies-tech.com
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. /*
  11. This driver communicates the job requests via Unix socket to the minergate
  12. process, that is responsible for controlling the Spondoolies Dawson SP10 miner.
  13. The jobs sent each with unique ID and returned asynchronously in one of the next
  14. transactions. REQUEST_PERIOD and REQUEST_SIZE define the communication rate with minergate.
  15. */
  16. #include <float.h>
  17. #include <limits.h>
  18. #include <pthread.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <strings.h>
  22. #include <sys/time.h>
  23. #include <unistd.h>
  24. #include <assert.h>
  25. #include <time.h>
  26. #include <sys/socket.h>
  27. #include <sys/un.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include <math.h>
  31. #include "config.h"
  32. #include "compat.h"
  33. #include "miner.h"
  34. #include "driver-spondoolies-sp10-p.h"
  35. #include "driver-spondoolies-sp10.h"
  36. #ifdef WORDS_BIGENDIAN
  37. # define swap32tobe(out, in, sz) ((out == in) ? (void)0 : memmove(out, in, sz))
  38. # define LOCAL_swap32be(type, var, sz) ;
  39. # define swap32tole(out, in, sz) swap32yes(out, in, sz)
  40. # define LOCAL_swap32le(type, var, sz) LOCAL_swap32(type, var, sz)
  41. #else
  42. # define swap32tobe(out, in, sz) swap32yes(out, in, sz)
  43. # define LOCAL_swap32be(type, var, sz) LOCAL_swap32(type, var, sz)
  44. # define swap32tole(out, in, sz) ((out == in) ? (void)0 : memmove(out, in, sz))
  45. # define LOCAL_swap32le(type, var, sz) ;
  46. #endif
  47. static inline void swap32yes(void *out, const void *in, size_t sz)
  48. {
  49. size_t swapcounter;
  50. for (swapcounter = 0; swapcounter < sz; ++swapcounter)
  51. (((uint32_t*)out)[swapcounter]) = swab32(((uint32_t*)in)[swapcounter]);
  52. }
  53. static void send_minergate_pkt(const minergate_req_packet* mp_req, minergate_rsp_packet* mp_rsp,
  54. int socket_fd)
  55. {
  56. int nbytes, nwrote, nread;
  57. nbytes = sizeof(minergate_req_packet);
  58. nwrote = write(socket_fd, (const void *)mp_req, nbytes);
  59. if (unlikely(nwrote != nbytes))
  60. _quit(-1);
  61. nbytes = sizeof(minergate_rsp_packet);
  62. nread = read(socket_fd, (void *)mp_rsp, nbytes);
  63. if (unlikely(nread != nbytes))
  64. _quit(-1);
  65. passert(mp_rsp->magic == 0xcaf4);
  66. }
  67. static bool spondoolies_prepare(struct thr_info *thr)
  68. {
  69. struct cgpu_info *spondoolies = thr->cgpu;
  70. struct timeval now;
  71. assert(spondoolies);
  72. cgtime(&now);
  73. /* FIXME: Vladik */
  74. #if NEED_FIX
  75. get_datestamp(spondoolies->init, &now);
  76. #endif
  77. return true;
  78. }
  79. static int init_socket(void)
  80. {
  81. int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
  82. struct sockaddr_un address;
  83. if (socket_fd < 0) {
  84. printf("socket() failed\n");
  85. perror("Err:");
  86. return 0;
  87. }
  88. /* start with a clean address structure */
  89. memset(&address, 0, sizeof(struct sockaddr_un));
  90. address.sun_family = AF_UNIX;
  91. sprintf(address.sun_path, MINERGATE_SOCKET_FILE);
  92. if(connect(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un))) {
  93. printf("connect() failed\n");
  94. perror("Err:");
  95. return 0;
  96. }
  97. return socket_fd;
  98. }
  99. static bool spondoolies_flush_queue(struct spond_adapter* a, bool flush_queue)
  100. {
  101. if (!a->parse_resp) {
  102. static int i = 0;
  103. if (i++ % 10 == 0 && a->works_in_minergate_and_pending_tx + a->works_pending_tx != a->works_in_driver)
  104. printf("%d + %d != %d\n", a->works_in_minergate_and_pending_tx, a->works_pending_tx,a->works_in_driver);
  105. assert(a->works_in_minergate_and_pending_tx + a->works_pending_tx == a->works_in_driver);
  106. send_minergate_pkt(a->mp_next_req, a->mp_last_rsp, a->socket_fd);
  107. if (flush_queue)
  108. a->mp_next_req->mask |= 0x02;
  109. else
  110. a->mp_next_req->mask &= ~0x02;
  111. a->mp_next_req->req_count = 0;
  112. a->parse_resp = 1;
  113. a->works_in_minergate_and_pending_tx += a->works_pending_tx;
  114. a->works_pending_tx = 0;
  115. }
  116. return true;
  117. }
  118. static void spondoolies_detect(__maybe_unused bool hotplug)
  119. {
  120. struct cgpu_info *cgpu = cgcalloc(1, sizeof(*cgpu));
  121. struct device_drv *drv = &sp10_drv;
  122. struct spond_adapter *a;
  123. #if NEED_FIX
  124. nDevs = 1;
  125. #endif
  126. assert(cgpu);
  127. cgpu->drv = drv;
  128. cgpu->deven = DEV_ENABLED;
  129. cgpu->threads = 1;
  130. cgpu->device_data = cgcalloc(sizeof(struct spond_adapter), 1);
  131. a = cgpu->device_data;
  132. a->cgpu = (void *)cgpu;
  133. a->adapter_state = ADAPTER_STATE_OPERATIONAL;
  134. a->mp_next_req = allocate_minergate_packet_req(0xca, 0xfe);
  135. a->mp_last_rsp = allocate_minergate_packet_rsp(0xca, 0xfe);
  136. pthread_mutex_init(&a->lock, NULL);
  137. a->socket_fd = init_socket();
  138. if (a->socket_fd < 1) {
  139. printf("Error connecting to minergate server!");
  140. _quit(-1);
  141. }
  142. assert(add_cgpu(cgpu));
  143. // Clean MG socket
  144. spondoolies_flush_queue(a, true);
  145. spondoolies_flush_queue(a, true);
  146. spondoolies_flush_queue(a, true);
  147. applog(LOG_DEBUG, "SPOND spondoolies_detect done");
  148. }
  149. static struct api_data *spondoolies_api_stats(struct cgpu_info *cgpu)
  150. {
  151. struct spond_adapter *a = cgpu->device_data;
  152. struct api_data *root = NULL;
  153. root = api_add_int(root, "ASICs total rate", &a->temp_rate, false);
  154. root = api_add_int(root, "Temperature front", &a->front_temp, false);
  155. root = api_add_int(root, "Temperature rear top", &a->rear_temp_top, false);
  156. root = api_add_int(root, "Temperature rear bot", &a->rear_temp_bot, false);
  157. return root;
  158. }
  159. #if 0
  160. static unsigned char get_leading_zeroes(const unsigned char *target)
  161. {
  162. unsigned char leading = 0;
  163. int first_non_zero_chr;
  164. uint8_t m;
  165. for (first_non_zero_chr = 31; first_non_zero_chr >= 0; first_non_zero_chr--) {
  166. if (target[first_non_zero_chr] == 0)
  167. leading += 8;
  168. else
  169. break;
  170. }
  171. // j = first non-zero
  172. m = target[first_non_zero_chr];
  173. while ((m & 0x80) == 0) {
  174. leading++;
  175. m = m << 1;
  176. }
  177. return leading;
  178. }
  179. #endif
  180. static void spondoolies_shutdown(__maybe_unused struct thr_info *thr)
  181. {
  182. }
  183. static void fill_minergate_request(minergate_do_job_req* work, struct work *cg_work,
  184. int ntime_offset)
  185. {
  186. uint32_t x[64/4];
  187. uint64_t wd;
  188. memset(work, 0, sizeof(minergate_do_job_req));
  189. //work->
  190. LOCAL_swap32le(unsigned char, cg_work->midstate, 32/4)
  191. LOCAL_swap32le(unsigned char, cg_work->data+64, 64/4)
  192. swap32yes(x, cg_work->data + 64, 64/4);
  193. memcpy(work->midstate, cg_work->midstate, 32);
  194. work->mrkle_root = ntohl(x[0]);
  195. work->timestamp = ntohl(x[1]);
  196. work->difficulty = ntohl(x[2]);
  197. //work->leading_zeroes = get_leading_zeroes(cg_work->target);
  198. // Is there no better way to get leading zeroes?
  199. work->leading_zeroes = 30;
  200. wd = round(cg_work->work_difficulty);
  201. while (wd) {
  202. work->leading_zeroes++;
  203. wd = wd >> 1;
  204. }
  205. //printf("%d %d\n",work->leading_zeroes, (int)round(cg_work->work_difficulty));
  206. work->work_id_in_sw = cg_work->subid;
  207. work->ntime_limit = 0;
  208. work->ntime_offset = ntime_offset;
  209. }
  210. // returns true if queue full.
  211. static struct timeval last_force_queue = {0};
  212. static bool spondoolies_queue_full(struct cgpu_info *cgpu)
  213. {
  214. // Only once every 1/10 second do work.
  215. struct spond_adapter* a = cgpu->device_data;
  216. int next_job_id, ntime_clones, i;
  217. struct timeval tv;
  218. struct work *work;
  219. unsigned int usec;
  220. bool ret = false;
  221. mutex_lock(&a->lock);
  222. passert(a->works_pending_tx <= REQUEST_SIZE);
  223. gettimeofday(&tv, NULL);
  224. usec = (tv.tv_sec-last_force_queue.tv_sec) * 1000000;
  225. usec += (tv.tv_usec-last_force_queue.tv_usec);
  226. if ((usec >= REQUEST_PERIOD) || (a->reset_mg_queue == 2) ||
  227. ((a->reset_mg_queue == 1) && (a->works_pending_tx == REQUEST_SIZE))) {
  228. spondoolies_flush_queue(a, (a->reset_mg_queue == 2));
  229. if (a->reset_mg_queue)
  230. a->reset_mg_queue--;
  231. last_force_queue = tv;
  232. }
  233. // see if we have enough jobs
  234. if (a->works_pending_tx == REQUEST_SIZE) {
  235. ret = true;
  236. goto return_unlock;
  237. }
  238. // see if can take 1 more job.
  239. next_job_id = (a->current_job_id + 1) % MAX_JOBS_IN_MINERGATE;
  240. if (a->my_jobs[next_job_id].cgminer_work) {
  241. ret = true;
  242. goto return_unlock;
  243. }
  244. work = get_queued(cgpu);
  245. if (!work) {
  246. cgsleep_ms(10);
  247. goto return_unlock;
  248. }
  249. work->thr = cgpu->thr[0];
  250. work->thr_id = cgpu->thr[0]->id;
  251. assert(work->thr);
  252. // Create 5 works using ntime increment
  253. a->current_job_id = next_job_id;
  254. work->subid = a->current_job_id;
  255. // Get pointer for the request
  256. a->my_jobs[a->current_job_id].cgminer_work = work;
  257. a->my_jobs[a->current_job_id].state = SPONDWORK_STATE_IN_BUSY;
  258. a->my_jobs[a->current_job_id].ntime_clones = 0;
  259. ntime_clones = (work->drv_rolllimit < MAX_NROLES) ? work->drv_rolllimit : MAX_NROLES;
  260. for (i = 0 ; (i < ntime_clones) && (a->works_pending_tx < REQUEST_SIZE) ; i++) {
  261. minergate_do_job_req* pkt_job = &a->mp_next_req->req[a->works_pending_tx];
  262. fill_minergate_request(pkt_job, work, i);
  263. a->works_in_driver++;
  264. a->works_pending_tx++;
  265. a->mp_next_req->req_count++;
  266. a->my_jobs[a->current_job_id].merkle_root = pkt_job->mrkle_root;
  267. a->my_jobs[a->current_job_id].ntime_clones++;
  268. }
  269. return_unlock:
  270. mutex_unlock(&a->lock);
  271. return ret;
  272. }
  273. static void spond_poll_stats(struct cgpu_info *spond, struct spond_adapter *a)
  274. {
  275. FILE *fp = fopen("/var/run/mg_rate_temp", "r");
  276. if (!fp) {
  277. applog(LOG_DEBUG, "SPOND unable to open mg_rate_temp");
  278. a->temp_rate = a->front_temp = a->rear_temp_top = a->rear_temp_bot = 0;
  279. } else {
  280. int ret = fscanf(fp, "%d %d %d %d", &a->temp_rate, &a->front_temp , &a->rear_temp_top , &a->rear_temp_bot);
  281. if (ret != 4)
  282. a->temp_rate = a->front_temp = a->rear_temp_top = a->rear_temp_bot = 0;
  283. fclose(fp);
  284. }
  285. applog(LOG_DEBUG, "SPOND poll_stats rate: %d front: %d rear(T/B): %d/%d",
  286. a->temp_rate, a->front_temp , a->rear_temp_top, a->rear_temp_bot);
  287. /* Use the rear temperature as the dev temperature for now */
  288. spond->temp = (a->rear_temp_top + a->rear_temp_bot)/2;
  289. }
  290. // Return completed work to submit_nonce() and work_completed()
  291. // struct timeval last_force_queue = {0};
  292. static int64_t spond_scanhash(struct thr_info *thr)
  293. {
  294. struct cgpu_info *cgpu = thr->cgpu;
  295. struct spond_adapter *a = cgpu->device_data;
  296. int64_t ghashes = 0;
  297. cgtimer_t cgt;
  298. time_t now_t;
  299. cgsleep_prepare_r(&cgt);
  300. now_t = time(NULL);
  301. /* Poll stats only once per second */
  302. if (now_t != a->last_stats) {
  303. a->last_stats = now_t;
  304. spond_poll_stats(cgpu, a);
  305. }
  306. if (a->parse_resp) {
  307. int array_size, i, j;
  308. mutex_lock(&a->lock);
  309. ghashes = (a->mp_last_rsp->gh_div_10_rate);
  310. ghashes = ghashes * 10000 * REQUEST_PERIOD;
  311. array_size = a->mp_last_rsp->rsp_count;
  312. for (i = 0; i < array_size; i++) { // walk the jobs
  313. int job_id;
  314. minergate_do_job_rsp* work = a->mp_last_rsp->rsp + i;
  315. job_id = work->work_id_in_sw;
  316. if ((a->my_jobs[job_id].cgminer_work)) {
  317. if (a->my_jobs[job_id].merkle_root == work->mrkle_root) {
  318. assert(a->my_jobs[job_id].state == SPONDWORK_STATE_IN_BUSY);
  319. a->works_in_minergate_and_pending_tx--;
  320. a->works_in_driver--;
  321. for (j = 0; j < 2; j++) {
  322. if (work->winner_nonce[j]) {
  323. bool __maybe_unused ok;
  324. struct work *cg_work = a->my_jobs[job_id].cgminer_work;
  325. #ifndef SP_NTIME
  326. ok = submit_nonce(cg_work->thr, cg_work, work->winner_nonce[j]);
  327. #else
  328. ok = submit_noffset_nonce(cg_work->thr, cg_work, work->winner_nonce[j], work->ntime_offset);
  329. #endif
  330. //printf("OK on %d:%d = %d\n",work->work_id_in_sw,j, ok);
  331. a->wins++;
  332. }
  333. }
  334. //printf("%d ntime_clones = %d\n",job_id,a->my_jobs[job_id].ntime_clones);
  335. if ((--a->my_jobs[job_id].ntime_clones) == 0) {
  336. //printf("Done with %d\n", job_id);
  337. work_completed(a->cgpu, a->my_jobs[job_id].cgminer_work);
  338. a->good++;
  339. a->my_jobs[job_id].cgminer_work = NULL;
  340. a->my_jobs[job_id].state = SPONDWORK_STATE_EMPTY;
  341. }
  342. } else {
  343. a->bad++;
  344. printf("Dropping minergate old job id=%d mrkl=%x my-mrkl=%x\n",
  345. job_id, a->my_jobs[job_id].merkle_root, work->mrkle_root);
  346. }
  347. } else {
  348. a->empty++;
  349. printf("No cgminer job (id:%d res:%d)!\n",job_id, work->res);
  350. }
  351. }
  352. mutex_unlock(&a->lock);
  353. a->parse_resp = 0;
  354. }
  355. cgsleep_ms_r(&cgt, 40);
  356. return ghashes;
  357. }
  358. // Remove all work from queue
  359. static void spond_flush_work(struct cgpu_info *cgpu)
  360. {
  361. struct spond_adapter *a = cgpu->device_data;
  362. mutex_lock(&a->lock);
  363. a->reset_mg_queue = 2;
  364. mutex_unlock(&a->lock);
  365. }
  366. struct device_drv sp10_drv = {
  367. .drv_id = DRIVER_sp10,
  368. .dname = "Spondoolies",
  369. .name = "SPN",
  370. .max_diff = 64.0, // Limit max diff to get some nonces back regardless
  371. .drv_detect = spondoolies_detect,
  372. .get_api_stats = spondoolies_api_stats,
  373. .thread_prepare = spondoolies_prepare,
  374. .thread_shutdown = spondoolies_shutdown,
  375. .hash_work = hash_queued_work,
  376. .queue_full = spondoolies_queue_full,
  377. .scanwork = spond_scanhash,
  378. .flush_work = spond_flush_work,
  379. };