driver-titan.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright 2014 Vitalii Demianets
  3. * Copyright 2014 KnCMiner
  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. #include <fcntl.h>
  11. #include <sys/ioctl.h>
  12. #include <linux/spi/spidev.h>
  13. #include "deviceapi.h"
  14. #include "logging.h"
  15. #include "lowl-spi.h"
  16. #include "miner.h"
  17. #include "util.h"
  18. #include "titan-asic.h"
  19. #define KNC_TITAN_DEFAULT_FREQUENCY 600
  20. #define KNC_TITAN_HWERR_DISABLE_SECS 10
  21. #define KNC_TITAN_SPI_SPEED 3000000
  22. #define KNC_TITAN_SPI_DELAY 0
  23. #define KNC_TITAN_SPI_MODE (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH)
  24. #define KNC_TITAN_SPI_BITS 8
  25. BFG_REGISTER_DRIVER(knc_titan_drv)
  26. static const struct bfg_set_device_definition knc_titan_set_device_funcs[];
  27. struct knc_titan_info {
  28. struct spi_port *spi;
  29. struct cgpu_info *cgpu;
  30. int freq;
  31. };
  32. struct knc_titan_core {
  33. int asicno;
  34. int coreno;
  35. int hwerr_in_row;
  36. int hwerr_disable_time;
  37. struct timeval enable_at;
  38. struct timeval first_hwerr;
  39. };
  40. static struct spi_port global_spi;
  41. static bool knc_titan_spi_open(const char *repr, struct spi_port * const spi)
  42. {
  43. const char * const spipath = "/dev/spidev1.0";
  44. const int fd = open(spipath, O_RDWR);
  45. const uint8_t lsbfirst = 0;
  46. if (0 > fd)
  47. return false;
  48. if (ioctl(fd, SPI_IOC_WR_MODE , &spi->mode )) goto fail;
  49. if (ioctl(fd, SPI_IOC_WR_LSB_FIRST , &lsbfirst )) goto fail;
  50. if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spi->bits )) goto fail;
  51. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ , &spi->speed)) goto fail;
  52. spi->fd = fd;
  53. return true;
  54. fail:
  55. close(fd);
  56. spi->fd = -1;
  57. applog(LOG_WARNING, "%s: Failed to open %s", repr, spipath);
  58. return false;
  59. }
  60. #define knc_titan_spi_txrx linux_spi_txrx
  61. static bool knc_titan_detect_one(const char *devpath)
  62. {
  63. static struct cgpu_info *prev_cgpu = NULL;
  64. struct cgpu_info *cgpu;
  65. struct spi_port *spi = &global_spi;
  66. int cores = 0, die;
  67. struct titan_info_response resp;
  68. char repr[6];
  69. cgpu = malloc(sizeof(*cgpu));
  70. if (unlikely(!cgpu))
  71. quit(1, "Failed to alloc cgpu_info");
  72. if (!prev_cgpu) {
  73. /* Be careful, read lowl-spi.h comments for warnings */
  74. memset(spi, 0, sizeof(*spi));
  75. spi->txrx = knc_titan_spi_txrx;
  76. spi->cgpu = cgpu;
  77. spi->repr = knc_titan_drv.dname;
  78. spi->logprio = LOG_ERR;
  79. spi->speed = KNC_TITAN_SPI_SPEED;
  80. spi->delay = KNC_TITAN_SPI_DELAY;
  81. spi->mode = KNC_TITAN_SPI_MODE;
  82. spi->bits = KNC_TITAN_SPI_BITS;
  83. if (!knc_titan_spi_open(knc_titan_drv.name, spi)) {
  84. free(cgpu);
  85. return false;
  86. }
  87. }
  88. snprintf(repr, sizeof(repr), "%s%s", knc_titan_drv.name, devpath);
  89. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  90. if (!knc_titan_spi_get_info(repr, spi, &resp, die, KNC_TITAN_CORES_PER_DIE))
  91. continue;
  92. cores += resp.cores;
  93. }
  94. if (0 == cores) {
  95. free(cgpu);
  96. return false;
  97. }
  98. applog(LOG_NOTICE, "%s: Found ASIC with %d cores", repr, cores);
  99. *cgpu = (struct cgpu_info) {
  100. .drv = &knc_titan_drv,
  101. .device_path = strdup(devpath),
  102. .set_device_funcs = knc_titan_set_device_funcs,
  103. .deven = DEV_ENABLED,
  104. .procs = cores,
  105. .threads = prev_cgpu ? 0 : 1,
  106. };
  107. const bool rv = add_cgpu_slave(cgpu, prev_cgpu);
  108. prev_cgpu = cgpu;
  109. return rv;
  110. }
  111. static int knc_titan_detect_auto(void)
  112. {
  113. const int first = 0, last = KNC_TITAN_MAX_ASICS - 1;
  114. char devpath[256];
  115. int found = 0, i;
  116. for (i = first; i <= last; ++i)
  117. {
  118. sprintf(devpath, "%d", i);
  119. if (knc_titan_detect_one(devpath))
  120. ++found;
  121. }
  122. return found;
  123. }
  124. static void knc_titan_detect(void)
  125. {
  126. generic_detect(&knc_titan_drv, knc_titan_detect_one, knc_titan_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  127. }
  128. static bool knc_titan_init(struct thr_info * const thr)
  129. {
  130. const int max_cores = KNC_TITAN_CORES_PER_ASIC;
  131. struct thr_info *mythr;
  132. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  133. struct knc_titan_info *knc;
  134. struct knc_titan_core *knccore;
  135. int i, asic;
  136. knc = calloc(1, sizeof(*knc));
  137. if (unlikely(!knc))
  138. quit(1, "Failed to alloc knc_titan_info");
  139. for (proc = cgpu; proc; ) {
  140. if (proc->device != proc) {
  141. applog(LOG_WARNING, "%"PRIpreprv": Extra processor?", proc->proc_repr);
  142. proc = proc->next_proc;
  143. continue;
  144. }
  145. asic = atoi(proc->device_path);
  146. for (i = 0; i < max_cores; ++i) {
  147. mythr = proc->thr[0];
  148. mythr->cgpu_data = knccore = malloc(sizeof(*knccore));
  149. if (unlikely(!knccore))
  150. quit(1, "Failed to alloc knc_titan_core");
  151. *knccore = (struct knc_titan_core) {
  152. .asicno = asic,
  153. .coreno = i,
  154. .hwerr_in_row = 0,
  155. .hwerr_disable_time = KNC_TITAN_HWERR_DISABLE_SECS,
  156. };
  157. timer_set_now(&knccore->enable_at);
  158. proc->device_data = knc;
  159. proc = proc->next_proc;
  160. if ((!proc) || proc->device == proc)
  161. break;
  162. }
  163. }
  164. *knc = (struct knc_titan_info) {
  165. .spi = &global_spi,
  166. .cgpu = cgpu,
  167. .freq = KNC_TITAN_DEFAULT_FREQUENCY,
  168. };
  169. cgpu_set_defaults(cgpu);
  170. return true;
  171. }
  172. static int64_t knc_titan_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  173. {
  174. return 0;
  175. }
  176. /*
  177. * specify settings / options via RPC or command line
  178. */
  179. /* support for --set-device
  180. * must be set before probing the device
  181. */
  182. static void knc_titan_set_clock_freq(struct cgpu_info * const device, int const val)
  183. {
  184. struct knc_titan_info * const info = device->device_data;
  185. info->freq = val;
  186. }
  187. static const char *knc_titan_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)
  188. {
  189. knc_titan_set_clock_freq(device, atoi(setting));
  190. return NULL;
  191. }
  192. static const struct bfg_set_device_definition knc_titan_set_device_funcs[] = {
  193. { "clock", knc_titan_set_clock, NULL },
  194. { NULL },
  195. };
  196. /*
  197. * specify settings / options via TUI
  198. */
  199. #ifdef HAVE_CURSES
  200. static void knc_titan_tui_wlogprint_choices(struct cgpu_info * const proc)
  201. {
  202. wlogprint("[C]lock speed ");
  203. }
  204. static const char *knc_titan_tui_handle_choice(struct cgpu_info * const proc, const int input)
  205. {
  206. static char buf[0x100]; /* Static for replies */
  207. switch (input)
  208. {
  209. case 'c': case 'C':
  210. {
  211. sprintf(buf, "Set clock speed");
  212. char * const setting = curses_input(buf);
  213. knc_titan_set_clock_freq(proc->device, atoi(setting));
  214. return "Clock speed changed\n";
  215. }
  216. }
  217. return NULL;
  218. }
  219. static void knc_titan_wlogprint_status(struct cgpu_info * const proc)
  220. {
  221. wlogprint("Clock speed: N/A\n");
  222. }
  223. #endif
  224. struct device_drv knc_titan_drv =
  225. {
  226. /* metadata */
  227. .dname = "titan",
  228. .name = "KNC",
  229. .supported_algos = POW_SCRYPT,
  230. .drv_detect = knc_titan_detect,
  231. .thread_init = knc_titan_init,
  232. /* specify mining type - scanhash */
  233. .minerloop = minerloop_scanhash,
  234. /* scanhash mining hooks */
  235. .scanhash = knc_titan_scanhash,
  236. /* TUI support - e.g. setting clock via UI */
  237. #ifdef HAVE_CURSES
  238. .proc_wlogprint_status = knc_titan_wlogprint_status,
  239. .proc_tui_wlogprint_choices = knc_titan_tui_wlogprint_choices,
  240. .proc_tui_handle_choice = knc_titan_tui_handle_choice,
  241. #endif
  242. };