driver-gridseed.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. * Copyright 2014 Nate Woolls
  4. * Copyright 2014 GridSeed 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 "deviceapi.h"
  14. #include "lowlevel.h"
  15. #include "lowl-vcom.h"
  16. #include "gc3355.h"
  17. #define GRIDSEED_DEFAULT_FREQUENCY 600
  18. // 60Kh/s at 700MHz in ms
  19. #define GRIDSEED_HASH_SPEED 0.08571428571429
  20. BFG_REGISTER_DRIVER(gridseed_drv)
  21. static const struct bfg_set_device_definition gridseed_set_device_funcs_probe[];
  22. static const struct bfg_set_device_definition gridseed_set_device_funcs_live[];
  23. /*
  24. * helper functions
  25. */
  26. static
  27. struct cgpu_info *gridseed_alloc_device(const char *path, struct device_drv *driver, struct gc3355_orb_info *info)
  28. {
  29. struct cgpu_info *device = calloc(1, sizeof(struct cgpu_info));
  30. if (unlikely(!device))
  31. quit(1, "Failed to malloc cgpu_info");
  32. device->drv = driver;
  33. device->device_path = strdup(path);
  34. device->device_fd = -1;
  35. device->threads = 1;
  36. device->device_data = info;
  37. device->set_device_funcs = gridseed_set_device_funcs_live;
  38. return device;
  39. }
  40. static
  41. struct gc3355_orb_info *gridseed_alloc_info()
  42. {
  43. struct gc3355_orb_info *info = calloc(1, sizeof(struct gc3355_orb_info));
  44. if (unlikely(!info))
  45. quit(1, "Failed to malloc gc3355_orb_info");
  46. info->freq = GRIDSEED_DEFAULT_FREQUENCY;
  47. return info;
  48. }
  49. static
  50. void gridseed_empty_work(int fd)
  51. {
  52. unsigned char buf[GC3355_READ_SIZE];
  53. gc3355_read(fd, (char *)buf, GC3355_READ_SIZE);
  54. }
  55. /*
  56. * device detection
  57. */
  58. static
  59. bool gridseed_detect_custom(const char *path, struct device_drv *driver, struct gc3355_orb_info *info)
  60. {
  61. int fd = gc3355_open(path);
  62. if(fd < 0)
  63. return false;
  64. gridseed_empty_work(fd);
  65. int64_t fw_version = gc3355_get_firmware_version(fd);
  66. if (fw_version == -1)
  67. {
  68. applog(LOG_ERR, "%s: Invalid detect response from %s", gridseed_drv.dname, path);
  69. gc3355_close(fd);
  70. return false;
  71. }
  72. if (serial_claim_v(path, driver))
  73. return false;
  74. info->chips = GC3355_ORB_DEFAULT_CHIPS;
  75. if((fw_version & 0xffff) == 0x1402)
  76. info->chips = GC3355_BLADE_DEFAULT_CHIPS;
  77. //pick up any user-defined settings passed in via --set
  78. drv_set_defaults(driver, gridseed_set_device_funcs_probe, info, path, detectone_meta_info.serial, 1);
  79. struct cgpu_info *device = gridseed_alloc_device(path, driver, info);
  80. device->device_fd = fd;
  81. device->procs = info->chips;
  82. if (!add_cgpu(device))
  83. return false;
  84. gc3355_init_usborb(device->device_fd, info->freq, false, false);
  85. applog(LOG_INFO, "Found %"PRIpreprv" at %s", device->proc_repr, path);
  86. applog(LOG_DEBUG, "%"PRIpreprv": Init: firmware=%"PRId64", chips=%d", device->proc_repr, fw_version, info->chips);
  87. return true;
  88. }
  89. static
  90. bool gridseed_detect_one(const char *path)
  91. {
  92. struct gc3355_orb_info *info = gridseed_alloc_info();
  93. if (!gridseed_detect_custom(path, &gridseed_drv, info))
  94. {
  95. free(info);
  96. return false;
  97. }
  98. return true;
  99. }
  100. static
  101. bool gridseed_lowl_probe(const struct lowlevel_device_info * const info)
  102. {
  103. return vcom_lowl_probe_wrapper(info, gridseed_detect_one);
  104. }
  105. /*
  106. * setup & shutdown
  107. */
  108. static
  109. bool gridseed_thread_prepare(struct thr_info *thr)
  110. {
  111. thr->cgpu_data = calloc(1, sizeof(*thr->cgpu_data));
  112. struct cgpu_info *device = thr->cgpu;
  113. device->min_nonce_diff = 1./0x10000;
  114. return true;
  115. }
  116. static
  117. void gridseed_thread_shutdown(struct thr_info *thr)
  118. {
  119. struct cgpu_info *device = thr->cgpu;
  120. gc3355_close(device->device_fd);
  121. free(thr->cgpu_data);
  122. }
  123. /*
  124. * scanhash mining loop
  125. */
  126. // send work to the device
  127. static
  128. bool gridseed_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  129. {
  130. struct cgpu_info *device = thr->cgpu;
  131. struct gc3355_orb_info *info = device->device_data;
  132. unsigned char cmd[156];
  133. timer_set_now(&info->scanhash_time);
  134. gc3355_scrypt_reset(device->device_fd);
  135. gc3355_scrypt_prepare_work(cmd, work);
  136. // send work
  137. if (sizeof(cmd) != gc3355_write(device->device_fd, cmd, sizeof(cmd)))
  138. {
  139. applog(LOG_ERR, "%s: Failed to send work", device->dev_repr);
  140. return false;
  141. }
  142. return true;
  143. }
  144. static
  145. void gridseed_submit_nonce(struct thr_info * const thr, const unsigned char buf[GC3355_READ_SIZE], struct work * const work)
  146. {
  147. struct cgpu_info *device = thr->cgpu;
  148. uint32_t nonce = *(uint32_t *)(buf + 4);
  149. nonce = le32toh(nonce);
  150. uint32_t chip = nonce / ((uint32_t)0xffffffff / device->procs);
  151. const struct cgpu_info *proc = device_proc_by_id(device, chip);
  152. if (unlikely(!proc))
  153. proc = device;
  154. struct thr_info *proc_thr = proc->thr[0];
  155. submit_nonce(proc_thr, work, nonce);
  156. }
  157. static
  158. int64_t gridseed_calculate_chip_hashes(struct thr_info *thr)
  159. {
  160. struct cgpu_info *device = thr->cgpu;
  161. struct gc3355_orb_info *info = device->device_data;
  162. struct timeval old_scanhash_time = info->scanhash_time;
  163. timer_set_now(&info->scanhash_time);
  164. int elapsed_ms = ms_tdiff(&info->scanhash_time, &old_scanhash_time);
  165. return GRIDSEED_HASH_SPEED * (double)elapsed_ms * (double)(info->freq);
  166. }
  167. static
  168. void gridseed_hashes_done(struct thr_info *thr)
  169. {
  170. struct cgpu_info *device = thr->cgpu;
  171. int64_t chip_hashes = gridseed_calculate_chip_hashes(thr);
  172. for (struct cgpu_info *proc = device; proc; proc = proc->next_proc)
  173. hashes_done2(proc->thr[0], chip_hashes, NULL);
  174. }
  175. // read from device for nonce or command
  176. static
  177. int64_t gridseed_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  178. {
  179. struct cgpu_info *device = thr->cgpu;
  180. struct gc3355_orb_info *info = device->device_data;
  181. struct timeval tv_nonce_range, tv_hashes_done;
  182. // total hashrate of this device:
  183. uint32_t hashes_per_sec = GRIDSEED_HASH_SPEED * (double)(1000 * info->freq * info->chips);
  184. // amount of time it takes this device to scan a nonce range:
  185. uint32_t nonce_range_sec = 0xffffffff / hashes_per_sec;
  186. // timer to break out of scanning should we scan an entire nonce range
  187. timer_set_delay_from_now(&tv_nonce_range, nonce_range_sec * 1000000);
  188. // timer to estimate hashes every 10s for runs of no shares
  189. const uint32_t hashes_delay = 10 * 1000000;
  190. timer_set_delay_from_now(&tv_hashes_done, hashes_delay);
  191. unsigned char buf[GC3355_READ_SIZE];
  192. int read = 0;
  193. int fd = device->device_fd;
  194. while (!thr->work_restart && // true when new work is available (miner.c)
  195. (read = gc3355_read(fd, (char *)buf, GC3355_READ_SIZE)) >= 0 && // only check for failure - allow 0 bytes
  196. !timer_passed(&tv_nonce_range, NULL)) // true when we've had time to scan a range
  197. {
  198. if (timer_passed(&tv_hashes_done, NULL))
  199. {
  200. gridseed_hashes_done(thr);
  201. timer_set_delay_from_now(&tv_hashes_done, hashes_delay);
  202. }
  203. if (read == 0)
  204. continue;
  205. else if ((buf[0] == 0x55) && (buf[1] == 0x20))
  206. {
  207. gridseed_submit_nonce(thr, buf, work);
  208. gridseed_hashes_done(thr);
  209. timer_set_delay_from_now(&tv_hashes_done, hashes_delay);
  210. }
  211. else
  212. applog(LOG_ERR, "%"PRIpreprv": Unrecognized response", device->proc_repr);
  213. }
  214. return 0;
  215. }
  216. /*
  217. * specify settings / options
  218. */
  219. // support for --set-device
  220. // must be set before probing the device
  221. static
  222. const char *gridseed_set_clock(struct cgpu_info * const device, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  223. {
  224. struct gc3355_orb_info * const info = device->device_data;
  225. int val = atoi(setting);
  226. info->freq = val;
  227. // below required as we may already be mining
  228. gc3355_set_pll_freq(device->device_fd, val);
  229. return NULL;
  230. }
  231. static
  232. const char *gridseed_set_chips(struct cgpu_info * const device, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  233. {
  234. struct gc3355_orb_info * const info = device->device_data;
  235. int val = atoi(setting);
  236. info->chips = val;
  237. return NULL;
  238. }
  239. // for setting clock and chips during probe / detect
  240. static
  241. const struct bfg_set_device_definition gridseed_set_device_funcs_probe[] = {
  242. { "clock", gridseed_set_clock, NULL },
  243. { "chips", gridseed_set_chips, NULL },
  244. { NULL },
  245. };
  246. // for setting clock while mining
  247. static
  248. const struct bfg_set_device_definition gridseed_set_device_funcs_live[] = {
  249. { "clock", gridseed_set_clock, NULL },
  250. { NULL },
  251. };
  252. struct device_drv gridseed_drv =
  253. {
  254. // metadata
  255. .dname = "gridseed",
  256. .name = "GSD",
  257. .supported_algos = POW_SCRYPT,
  258. // detect device
  259. .lowl_probe = gridseed_lowl_probe,
  260. // initialize device
  261. .thread_prepare = gridseed_thread_prepare,
  262. // specify mining type - scanhash
  263. .minerloop = minerloop_scanhash,
  264. // scanhash mining hooks
  265. .prepare_work = gridseed_prepare_work,
  266. .scanhash = gridseed_scanhash,
  267. // teardown device
  268. .thread_shutdown = gridseed_thread_shutdown,
  269. };