driver-gridseed.c 7.2 KB

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