driver-gridseed.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. #define GRIDSEED_MAX_QUEUED 10
  19. BFG_REGISTER_DRIVER(gridseed_drv)
  20. /*
  21. * helper functions
  22. */
  23. static
  24. struct cgpu_info *gridseed_alloc_device(const char *path, struct device_drv *driver, struct gc3355_orb_info *info)
  25. {
  26. struct cgpu_info *device = calloc(1, sizeof(struct cgpu_info));
  27. if (unlikely(!device))
  28. quit(1, "Failed to malloc cgpu_info");
  29. device->drv = driver;
  30. device->device_path = strdup(path);
  31. device->device_fd = -1;
  32. device->threads = 1;
  33. device->procs = GC3355_ORB_DEFAULT_CHIPS;
  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. uint32_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. struct cgpu_info *device = gridseed_alloc_device(path, driver, info);
  70. if (serial_claim_v(path, driver))
  71. return false;
  72. if (!add_cgpu(device))
  73. return false;
  74. device->device_fd = fd;
  75. gc3355_init_usborb(device->device_fd, info->freq, false, false);
  76. applog(LOG_INFO, "Found %"PRIpreprv" at %s", device->proc_repr, path);
  77. applog(LOG_DEBUG, "%"PRIpreprv": Init: firmware=%d", device->proc_repr, fw_version);
  78. return true;
  79. }
  80. static
  81. bool gridseed_detect_one(const char *path)
  82. {
  83. struct gc3355_orb_info *info = gridseed_alloc_info();
  84. if (!gridseed_detect_custom(path, &gridseed_drv, info))
  85. {
  86. free(info);
  87. return false;
  88. }
  89. return true;
  90. }
  91. static
  92. bool gridseed_lowl_probe(const struct lowlevel_device_info * const info)
  93. {
  94. return vcom_lowl_probe_wrapper(info, gridseed_detect_one);
  95. }
  96. /*
  97. * setup & shutdown
  98. */
  99. static
  100. bool gridseed_thread_prepare(struct thr_info *thr)
  101. {
  102. thr->cgpu_data = calloc(1, sizeof(*thr->cgpu_data));
  103. if (opt_scrypt)
  104. {
  105. struct cgpu_info *device = thr->cgpu;
  106. device->min_nonce_diff = 1./0x10000;
  107. }
  108. return true;
  109. }
  110. static
  111. bool gridseed_set_queue_full(const struct cgpu_info * const device, int needwork);
  112. static
  113. bool gridseed_thread_init(struct thr_info *master_thr)
  114. {
  115. struct cgpu_info * const device = master_thr->cgpu;
  116. gridseed_set_queue_full(device, 0);
  117. timer_set_now(&master_thr->tv_poll);
  118. // kick off queue minerloop
  119. gridseed_set_queue_full(device, device->procs * 2);
  120. return true;
  121. }
  122. static
  123. void gridseed_thread_shutdown(struct thr_info *thr)
  124. {
  125. struct cgpu_info *device = thr->cgpu;
  126. gc3355_close(device->device_fd);
  127. free(thr->cgpu_data);
  128. }
  129. static
  130. void gridseed_reinit_device(struct cgpu_info * const proc)
  131. {
  132. timer_set_now(&proc->thr[0]->tv_poll);
  133. }
  134. /*
  135. * queued mining loop
  136. */
  137. static
  138. bool gridseed_set_queue_full(const struct cgpu_info * const device, int needwork)
  139. {
  140. struct gc3355_orb_info * const info = device->device_data;
  141. struct thr_info * const master_thr = device->thr[0];
  142. if (needwork != -1)
  143. info->needwork = needwork;
  144. const bool full = (device->device_fd == -1 || !info->needwork);
  145. if (full == master_thr->queue_full)
  146. return full;
  147. for (const struct cgpu_info *proc = device; proc; proc = proc->next_proc)
  148. {
  149. struct thr_info * const thr = proc->thr[0];
  150. thr->queue_full = full;
  151. }
  152. return full;
  153. }
  154. static
  155. bool gridseed_send_work(const struct cgpu_info * const device, struct work *work)
  156. {
  157. int work_size = opt_scrypt ? 156 : 52;
  158. unsigned char cmd[work_size];
  159. if (opt_scrypt)
  160. {
  161. gc3355_scrypt_reset(device->device_fd);
  162. gc3355_scrypt_prepare_work(cmd, work);
  163. }
  164. else
  165. gc3355_sha2_prepare_work(cmd, work, true);
  166. // send work
  167. if (sizeof(cmd) != gc3355_write(device->device_fd, cmd, sizeof(cmd)))
  168. {
  169. applog(LOG_ERR, "%s: Failed to send work", device->dev_repr);
  170. return false;
  171. }
  172. return true;
  173. }
  174. static
  175. void gridseed_prune_queue(const struct cgpu_info * const device, struct work *work)
  176. {
  177. struct thr_info * const master_thr = device->thr[0];
  178. // prune queue
  179. int prunequeue = HASH_COUNT(master_thr->work_list) - GRIDSEED_MAX_QUEUED;
  180. if (prunequeue > 0)
  181. {
  182. struct work *tmp;
  183. applog(LOG_DEBUG, "%s: Pruning %d old work item%s",
  184. device->dev_repr, prunequeue, prunequeue == 1 ? "" : "s");
  185. HASH_ITER(hh, master_thr->work_list, work, tmp)
  186. {
  187. HASH_DEL(master_thr->work_list, work);
  188. free_work(work);
  189. if (--prunequeue < 1)
  190. break;
  191. }
  192. }
  193. }
  194. // send work to the device & queue work
  195. static
  196. bool gridseed_queue_append(struct thr_info * const thr, struct work *work)
  197. {
  198. const struct cgpu_info * const device = thr->cgpu->device;
  199. struct gc3355_orb_info * const info = device->device_data;
  200. struct thr_info * const master_thr = device->thr[0];
  201. // if queue is full (-1 is a check flag) do not append new work
  202. if (gridseed_set_queue_full(device, -1))
  203. return false;
  204. // send work
  205. if (!gridseed_send_work(device, work))
  206. return false;
  207. // store work in queue
  208. HASH_ADD(hh, master_thr->work_list, id, sizeof(work->id), work);
  209. // prune queue
  210. gridseed_prune_queue(device, work);
  211. // sets info->needwork equal to 2nd arg and updates "full" flags
  212. gridseed_set_queue_full(device, info->needwork - 1);
  213. return true;
  214. }
  215. static
  216. void gridseed_queue_flush(struct thr_info * const thr)
  217. {
  218. const struct cgpu_info *device = thr->cgpu;
  219. if (device != device->device)
  220. return;
  221. gridseed_set_queue_full(device, device->procs);
  222. }
  223. static
  224. const struct cgpu_info *gridseed_proc_by_id(const struct cgpu_info * const dev, int procid)
  225. {
  226. const struct cgpu_info *proc = dev;
  227. for (int i = 0; i < procid; ++i)
  228. {
  229. proc = proc->next_proc;
  230. if (unlikely(!proc))
  231. return NULL;
  232. }
  233. return proc;
  234. }
  235. static
  236. void gridseed_submit_nonce(struct thr_info * const master_thr, const unsigned char buf[GC3355_READ_SIZE])
  237. {
  238. struct work *work;
  239. uint32_t nonce;
  240. int workid;
  241. struct cgpu_info * const device = master_thr->cgpu;
  242. struct gc3355_orb_info * const info = device->device_data;
  243. // extract workid from buffer
  244. memcpy(&workid, buf + 8, 4);
  245. // extract nonce from buffer
  246. memcpy(&nonce, buf + 4, 4);
  247. // extract chip # from nonce
  248. const int chip = nonce / ((uint32_t)0xffffffff / GC3355_ORB_DEFAULT_CHIPS);
  249. // find processor by device & chip
  250. const struct cgpu_info *proc = gridseed_proc_by_id(device, chip);
  251. // default process to device
  252. if (unlikely(!proc))
  253. proc = device;
  254. // the thread specific to the ASIC chip:
  255. struct thr_info * thr = proc->thr[0];
  256. nonce = htole32(nonce);
  257. // find the queued work for this nonce, by workid
  258. HASH_FIND(hh, master_thr->work_list, &workid, sizeof(workid), work);
  259. if (work)
  260. {
  261. submit_nonce(thr, work, nonce);
  262. HASH_DEL(master_thr->work_list, work);
  263. gridseed_set_queue_full(device, info->needwork + 2);
  264. }
  265. }
  266. static
  267. void gridseed_estimate_hashes(const struct cgpu_info * const device)
  268. {
  269. const struct cgpu_info *proc = device;
  270. const struct gc3355_orb_info *info = device->device_data;
  271. while (true)
  272. {
  273. hashes_done2(proc->thr[0], info->freq * 0xA4, NULL);
  274. proc = proc->next_proc;
  275. if (unlikely(!proc))
  276. return;
  277. }
  278. }
  279. #define GRIDSEED_SHORT_WORK_DELAY_MS 20
  280. #define GRIDSEED_LONG_WORK_DELAY_MS 30
  281. // read from device for nonce or command
  282. static
  283. void gridseed_poll(struct thr_info * const master_thr)
  284. {
  285. struct cgpu_info * const device = master_thr->cgpu;
  286. unsigned char buf[GC3355_READ_SIZE];
  287. int read = 0;
  288. struct timeval tv_timeout;
  289. timer_set_delay_from_now(&tv_timeout, GRIDSEED_LONG_WORK_DELAY_MS * 1000); // X MS
  290. while (!master_thr->work_restart && (read = gc3355_read(device->device_fd, (char *)buf, GC3355_READ_SIZE)) > 0)
  291. {
  292. if (buf[0] == 0x55)
  293. {
  294. switch(buf[1]) {
  295. case 0xaa:
  296. // Queue length result
  297. // could watch for watchdog reset here
  298. break;
  299. case 0x10: // BTC result
  300. case 0x20: // LTC result
  301. {
  302. gridseed_submit_nonce(master_thr, buf);
  303. break;
  304. }
  305. }
  306. } else
  307. {
  308. applog(LOG_ERR, "%"PRIpreprv": Unrecognized response", device->proc_repr);
  309. break;
  310. }
  311. if (timer_passed(&tv_timeout, NULL))
  312. {
  313. // allow work to be sent to the device
  314. applog(LOG_DEBUG, "%s poll: timeout met", device->dev_repr);
  315. break;
  316. }
  317. }
  318. gridseed_estimate_hashes(device);
  319. // allow work to be sent to the device
  320. timer_set_delay_from_now(&master_thr->tv_poll, GRIDSEED_SHORT_WORK_DELAY_MS * 1000); // X MS
  321. }
  322. /*
  323. * specify settings / options
  324. */
  325. // support for --set-device dualminer:clock=freq
  326. static
  327. char *gridseed_set_device(struct cgpu_info *device, char *option, char *setting, char *replybuf)
  328. {
  329. if (strcasecmp(option, "clock") == 0)
  330. {
  331. int val = atoi(setting);
  332. struct gc3355_orb_info *info = (struct gc3355_orb_info *)(device->device_data);
  333. info->freq = val;
  334. int fd = device->device_fd;
  335. gc3355_set_pll_freq(fd, val);
  336. return NULL;
  337. }
  338. sprintf(replybuf, "Unknown option: %s", option);
  339. return replybuf;
  340. }
  341. struct device_drv gridseed_drv =
  342. {
  343. // metadata
  344. .dname = "gridseed",
  345. .name = "GSD",
  346. .supported_algos = POW_SCRYPT,
  347. // detect device
  348. .lowl_probe = gridseed_lowl_probe,
  349. // initialize device
  350. .thread_prepare = gridseed_thread_prepare,
  351. .thread_init = gridseed_thread_init,
  352. .reinit_device = gridseed_reinit_device,
  353. // specify mining type - scanhash
  354. .minerloop = minerloop_queue,
  355. // queued mining hooks
  356. .queue_append = gridseed_queue_append,
  357. .queue_flush = gridseed_queue_flush,
  358. .poll = gridseed_poll,
  359. // teardown device
  360. .thread_shutdown = gridseed_thread_shutdown,
  361. // specify settings / options
  362. .set_device = gridseed_set_device,
  363. };