driver-dualminer.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. * Copyright 2014 Nate Woolls
  4. * Copyright 2014 Dualminer Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #include <stdbool.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <strings.h>
  18. #include "miner.h"
  19. #include "icarus-common.h"
  20. #include "lowlevel.h"
  21. #include "lowl-vcom.h"
  22. #include "deviceapi.h"
  23. #include "logging.h"
  24. #include "util.h"
  25. #include "gc3355.h"
  26. #ifndef WIN32
  27. #include <sys/ioctl.h>
  28. #else
  29. #include <io.h>
  30. #endif
  31. #define DUALMINER_IO_SPEED 115200
  32. #define DUALMINER_SCRYPT_HASH_TIME 0.00001428571429
  33. #define DUALMINER_SCRYPT_DM_HASH_TIME 0.00003333333333
  34. #define DUALMINER_SHA2_HASH_TIME 0.00000000300000
  35. #define DUALMINER_SCRYPT_READ_COUNT 48 // 4.8s to read
  36. #define DUALMINER_SHA2_READ_COUNT 16 // 1.6s to read
  37. static
  38. const char sha2_golden_ob[] =
  39. "55aa0f00a08701004a548fe471fa3a9a"
  40. "1371144556c3f64d2500b4826008fe4b"
  41. "bf7698c94eba7946ce22a72f4f672614"
  42. "1a0b3287";
  43. static
  44. const char sha2_golden_nonce[] = "a2870100";
  45. static
  46. const char scrypt_golden_ob[] =
  47. "55aa1f00000000000000000000000000"
  48. "000000000000000000000000aaaaaaaa"
  49. "711c0000603ebdb6e35b05223c54f815"
  50. "5ac33123006b4192e7aafafbeb9ef654"
  51. "4d2973d700000002069b9f9e3ce8a677"
  52. "8dea3d7a00926cd6eaa9585502c9b83a"
  53. "5601f198d7fbf09be9559d6335ebad36"
  54. "3e4f147a8d9934006963030b4e54c408"
  55. "c837ebc2eeac129852a55fee1b1d88f6"
  56. "000c050000000600";
  57. static
  58. const char scrypt_golden_nonce[] = "dd0c0500";
  59. enum
  60. {
  61. RTS_LOW = 0,
  62. RTS_HIGH = 1
  63. };
  64. BFG_REGISTER_DRIVER(dualminer_drv)
  65. static
  66. const struct bfg_set_device_definition dualminer_set_device_funcs[];
  67. // device helper functions
  68. static
  69. void dualminer_bootstrap_device(int fd)
  70. {
  71. gc3355_dual_reset(fd);
  72. if (opt_scrypt && !opt_dual_mode)
  73. gc3355_opt_scrypt_only_init(fd);
  74. else
  75. gc3355_dualminer_init(fd);
  76. usleep(1000);
  77. }
  78. static
  79. void dualminer_teardown_device(int fd)
  80. {
  81. if (opt_scrypt)
  82. gc3355_open_scrypt_unit(fd, SCRYPT_UNIT_CLOSE);
  83. else
  84. gc3355_open_sha2_unit(fd, "0");
  85. gc3355_set_rts_status(fd, RTS_LOW);
  86. }
  87. static
  88. bool dualminer_init(struct thr_info * const thr)
  89. {
  90. struct cgpu_info * const cgpu = thr->cgpu;
  91. if (opt_scrypt)
  92. cgpu->min_nonce_diff = 1./0x10000;
  93. return icarus_init(thr);
  94. }
  95. static
  96. void dualminer_init_firstrun(struct cgpu_info *icarus)
  97. {
  98. struct ICARUS_INFO *info = icarus->device_data;
  99. int fd = icarus->device_fd;
  100. dualminer_bootstrap_device(fd);
  101. if (opt_scrypt)
  102. gc3355_set_rts_status(fd, RTS_HIGH);
  103. gc3355_init(fd, opt_dualminer_sha2_gating, !opt_dual_mode);
  104. applog(LOG_DEBUG, "%"PRIpreprv": scrypt: %d, scrypt only: %d; have fan: %d\n", icarus->proc_repr, opt_scrypt, opt_scrypt, opt_hubfans);
  105. if (gc3355_get_cts_status(fd) != 1)
  106. {
  107. // Scrypt + SHA2 mode
  108. if (opt_scrypt)
  109. info->Hs = DUALMINER_SCRYPT_DM_HASH_TIME;
  110. }
  111. applog(LOG_DEBUG, "%"PRIpreprv": dualminer: Init: pll=%d, sha2num=%d", icarus->proc_repr, opt_pll_freq, opt_sha2_number);
  112. }
  113. // ICARUS_INFO functions - icarus-common.h
  114. static
  115. bool dualminer_detect_init(const char *devpath, int fd, struct ICARUS_INFO * __maybe_unused info)
  116. {
  117. dualminer_bootstrap_device(fd);
  118. return true;
  119. }
  120. static
  121. bool dualminer_job_start(struct thr_info * const thr)
  122. {
  123. struct cgpu_info *icarus = thr->cgpu;
  124. struct icarus_state * const state = thr->cgpu_data;
  125. int fd = icarus->device_fd;
  126. if (state->firstrun)
  127. dualminer_init_firstrun(icarus);
  128. if (opt_scrypt)
  129. {
  130. if (opt_dual_mode)
  131. gc3355_dualminer_init(fd);
  132. else
  133. gc3355_opt_scrypt_init(fd);
  134. }
  135. return icarus_job_start(thr);
  136. }
  137. // device detection
  138. static
  139. bool dualminer_detect_one(const char *devpath)
  140. {
  141. struct device_drv *drv = &dualminer_drv;
  142. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  143. if (unlikely(!info))
  144. quit(1, "Failed to malloc ICARUS_INFO");
  145. *info = (struct ICARUS_INFO){
  146. .baud = DUALMINER_IO_SPEED,
  147. .timing_mode = MODE_DEFAULT,
  148. .do_icarus_timing = false,
  149. .nonce_littleendian = true,
  150. .work_division = 2,
  151. .fpga_count = 2,
  152. .detect_init_func = dualminer_detect_init,
  153. .job_start_func = dualminer_job_start
  154. };
  155. if (opt_scrypt)
  156. {
  157. info->golden_ob = (char*)scrypt_golden_ob;
  158. info->golden_nonce = (char*)scrypt_golden_nonce;
  159. info->Hs = DUALMINER_SCRYPT_HASH_TIME;
  160. }
  161. else
  162. {
  163. info->golden_ob = (char*)sha2_golden_ob;
  164. info->golden_nonce = (char*)sha2_golden_nonce;
  165. info->Hs = DUALMINER_SHA2_HASH_TIME;
  166. }
  167. drv_set_defaults(drv, dualminer_set_device_funcs, info, devpath, detectone_meta_info.serial, 1);
  168. if (!icarus_detect_custom(devpath, drv, info))
  169. {
  170. free(info);
  171. return false;
  172. }
  173. if (opt_scrypt)
  174. info->read_count = DUALMINER_SCRYPT_READ_COUNT; // 4.8s to read
  175. else
  176. info->read_count = DUALMINER_SHA2_READ_COUNT; // 1.6s to read
  177. return true;
  178. }
  179. // support for --set-device dualminer:dual_mode=1
  180. // most be set before probing the device
  181. static
  182. const char *dualminer_set_dual_mode(struct cgpu_info * const proc, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  183. {
  184. int val = atoi(setting);
  185. opt_dual_mode = val == 1;
  186. return NULL;
  187. }
  188. static
  189. const struct bfg_set_device_definition dualminer_set_device_funcs[] = {
  190. {"dual_mode", dualminer_set_dual_mode, "set to 1 to enable dual algorithm mining with two BFGMiner processes"},
  191. {NULL},
  192. };
  193. // device_drv functions - miner.h
  194. static
  195. bool dualminer_lowl_probe(const struct lowlevel_device_info * const info)
  196. {
  197. return vcom_lowl_probe_wrapper(info, dualminer_detect_one);
  198. }
  199. static
  200. void dualminer_thread_shutdown(struct thr_info *thr)
  201. {
  202. // dualminer teardown
  203. dualminer_teardown_device(thr->cgpu->device_fd);
  204. // icarus teardown
  205. do_icarus_close(thr);
  206. free(thr->cgpu_data);
  207. }
  208. static
  209. bool dualminer_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  210. {
  211. struct cgpu_info * const icarus = thr->cgpu;
  212. struct icarus_state * const state = thr->cgpu_data;
  213. struct ICARUS_INFO * const info = icarus->device_data;
  214. memset(state->ob_bin, 0, info->ob_size);
  215. if (opt_scrypt)
  216. {
  217. state->ob_bin[0] = 0x55;
  218. state->ob_bin[1] = 0xaa;
  219. state->ob_bin[2] = 0x1f;
  220. state->ob_bin[3] = 0x00;
  221. memcpy(state->ob_bin + 4, work->target, 32);
  222. memcpy(state->ob_bin + 36, work->midstate, 32);
  223. memcpy(state->ob_bin + 68, work->data, 80);
  224. state->ob_bin[148] = 0xff;
  225. state->ob_bin[149] = 0xff;
  226. state->ob_bin[150] = 0xff;
  227. state->ob_bin[151] = 0xff;
  228. }
  229. else
  230. {
  231. uint8_t temp_bin[64];
  232. memset(temp_bin, 0, 64);
  233. memcpy(temp_bin, work->midstate, 32);
  234. memcpy(temp_bin+52, work->data + 64, 12);
  235. state->ob_bin[0] = 0x55;
  236. state->ob_bin[1] = 0xaa;
  237. state->ob_bin[2] = 0x0f;
  238. state->ob_bin[3] = 0x00;
  239. memcpy(state->ob_bin + 8, temp_bin, 32);
  240. memcpy(state->ob_bin + 40, temp_bin + 52, 12);
  241. }
  242. return true;
  243. }
  244. // support for --set-device dualminer:clock=freq
  245. static
  246. char *dualminer_set_device(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf)
  247. {
  248. if (strcasecmp(option, "clock") == 0)
  249. {
  250. int val = atoi(setting);
  251. opt_pll_freq = val;
  252. return NULL;
  253. }
  254. sprintf(replybuf, "Unknown option: %s", option);
  255. return replybuf;
  256. }
  257. // device_drv definition - miner.h
  258. static
  259. void dualminer_drv_init()
  260. {
  261. dualminer_drv = icarus_drv;
  262. dualminer_drv.dname = "dualminer";
  263. dualminer_drv.name = "DMU";
  264. dualminer_drv.lowl_probe = dualminer_lowl_probe;
  265. dualminer_drv.thread_init = dualminer_init;
  266. dualminer_drv.thread_shutdown = dualminer_thread_shutdown;
  267. dualminer_drv.job_prepare = dualminer_job_prepare;
  268. dualminer_drv.set_device = dualminer_set_device;
  269. ++dualminer_drv.probe_priority;
  270. }
  271. struct device_drv dualminer_drv =
  272. {
  273. .drv_init = dualminer_drv_init,
  274. };