driver-spondoolies-sp10.c 13 KB

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