driver-knc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdbool.h>
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #ifdef HAVE_LINUX_I2C_DEV_USER_H
  18. #include <linux/i2c-dev-user.h>
  19. #else
  20. #include <linux/i2c-dev.h>
  21. #endif
  22. #include <linux/spi/spidev.h>
  23. #include <uthash.h>
  24. #include "deviceapi.h"
  25. #include "logging.h"
  26. #include "miner.h"
  27. #include "spidevc.h"
  28. #define KNC_POLL_INTERVAL_US 10000
  29. #define KNC_SPI_SPEED 3000000
  30. #define KNC_SPI_DELAY 0
  31. #define KNC_SPI_MODE (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH)
  32. #define KNC_SPI_BITS 8
  33. static const char * const i2cpath = "/dev/i2c-2";
  34. enum knc_request_cmd {
  35. KNC_REQ_SUBMIT_WORK = 2,
  36. KNC_REQ_FLUSH_QUEUE = 3,
  37. };
  38. enum knc_reply_type {
  39. KNC_REPLY_NONCE_FOUND = 1,
  40. KNC_REPLY_WORK_DONE = 2,
  41. };
  42. struct device_drv knc_drv;
  43. struct knc_device {
  44. int i2c;
  45. struct spi_port *spi;
  46. struct work *workqueue;
  47. int workqueue_size;
  48. int workqueue_max;
  49. int next_id;
  50. struct work *devicework;
  51. };
  52. struct knc_core {
  53. int asicno;
  54. };
  55. static
  56. bool knc_detect_one(const char *devpath)
  57. {
  58. static struct cgpu_info *prev_cgpu = NULL;
  59. struct cgpu_info *cgpu;
  60. int i;
  61. const int fd = open(i2cpath, O_RDWR);
  62. char *leftover = NULL;
  63. const int i2cslave = strtol(devpath, &leftover, 0);
  64. uint8_t buf[0x20];
  65. if (leftover && leftover[0])
  66. return false;
  67. if (unlikely(fd == -1))
  68. {
  69. applog(LOG_DEBUG, "%s: Failed to open %s", __func__, i2cpath);
  70. return false;
  71. }
  72. if (ioctl(fd, I2C_SLAVE, i2cslave))
  73. {
  74. close(fd);
  75. applog(LOG_DEBUG, "%s: Failed to select i2c slave 0x%x",
  76. __func__, i2cslave);
  77. return false;
  78. }
  79. i = i2c_smbus_read_i2c_block_data(fd, 0, 0x20, buf);
  80. close(fd);
  81. if (-1 == i)
  82. {
  83. applog(LOG_DEBUG, "%s: 0x%x: Failed to read i2c block data",
  84. __func__, i2cslave);
  85. return false;
  86. }
  87. for (i = 0; ; ++i)
  88. {
  89. if (buf[i] == 3)
  90. break;
  91. if (i == 0x1f)
  92. return false;
  93. }
  94. cgpu = malloc(sizeof(*cgpu));
  95. *cgpu = (struct cgpu_info){
  96. .drv = &knc_drv,
  97. .device_path = strdup(devpath),
  98. .deven = DEV_ENABLED,
  99. .procs = 192,
  100. .threads = prev_cgpu ? 0 : 1,
  101. };
  102. const bool rv = add_cgpu_slave(cgpu, prev_cgpu);
  103. prev_cgpu = cgpu;
  104. return rv;
  105. }
  106. static int knc_detect_auto(void)
  107. {
  108. const int first = 0x20, last = 0x26;
  109. char devpath[4];
  110. int found = 0, i;
  111. for (i = first; i <= last; ++i)
  112. {
  113. sprintf(devpath, "%d", i);
  114. if (knc_detect_one(devpath))
  115. ++found;
  116. }
  117. return found;
  118. }
  119. static void knc_detect(void)
  120. {
  121. generic_detect(&knc_drv, knc_detect_one, knc_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  122. }
  123. static
  124. bool knc_spi_open(const char *repr, struct spi_port * const spi)
  125. {
  126. const char * const spipath = "/dev/spidev1.0";
  127. const int fd = open(spipath, O_RDWR);
  128. const uint8_t lsbfirst = 0;
  129. if (fd == -1)
  130. return false;
  131. if (ioctl(fd, SPI_IOC_WR_MODE , &spi->mode )) goto fail;
  132. if (ioctl(fd, SPI_IOC_WR_LSB_FIRST , &lsbfirst )) goto fail;
  133. if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spi->bits )) goto fail;
  134. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ , &spi->speed)) goto fail;
  135. spi->fd = fd;
  136. return true;
  137. fail:
  138. close(fd);
  139. spi->fd = -1;
  140. applog(LOG_WARNING, "%s: Failed to open %s", repr, spipath);
  141. return false;
  142. }
  143. static
  144. bool knc_spi_txrx(struct spi_port * const spi)
  145. {
  146. const void * const wrbuf = spi_gettxbuf(spi);
  147. void * const rdbuf = spi_getrxbuf(spi);
  148. const size_t bufsz = spi_getbufsz(spi);
  149. const int fd = spi->fd;
  150. struct spi_ioc_transfer xf = {
  151. .tx_buf = (uintptr_t) wrbuf,
  152. .rx_buf = (uintptr_t) rdbuf,
  153. .len = bufsz,
  154. .delay_usecs = spi->delay,
  155. .speed_hz = spi->speed,
  156. .bits_per_word = spi->bits,
  157. };
  158. return (ioctl(fd, SPI_IOC_MESSAGE(1), &xf) > 0);
  159. }
  160. static
  161. bool knc_init(struct thr_info * const thr)
  162. {
  163. const int max_cores = 192;
  164. struct thr_info *mythr;
  165. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  166. struct knc_device *knc;
  167. struct knc_core *knccore;
  168. struct spi_port *spi;
  169. const int i2c = open(i2cpath, O_RDWR);
  170. int i2cslave, i, j;
  171. uint8_t buf[0x20];
  172. if (unlikely(i2c == -1))
  173. {
  174. applog(LOG_DEBUG, "%s: Failed to open %s", __func__, i2cpath);
  175. return false;
  176. }
  177. for (proc = cgpu; proc; )
  178. {
  179. if (proc->device != proc)
  180. {
  181. applog(LOG_WARNING, "%"PRIpreprv": Extra processor?", proc->proc_repr);
  182. continue;
  183. }
  184. i2cslave = atoi(proc->device_path);
  185. if (ioctl(i2c, I2C_SLAVE, i2cslave))
  186. {
  187. applog(LOG_DEBUG, "%s: Failed to select i2c slave 0x%x",
  188. __func__, i2cslave);
  189. return false;
  190. }
  191. for (i = 0; i < max_cores; i += 0x20)
  192. {
  193. i2c_smbus_read_i2c_block_data(i2c, i, 0x20, buf);
  194. for (j = 0; j < 0x20; ++j)
  195. {
  196. mythr = proc->thr[0];
  197. mythr->cgpu_data = knccore = malloc(sizeof(*knccore));
  198. *knccore = (struct knc_core){
  199. .asicno = i2cslave - 0x20,
  200. };
  201. if (proc != cgpu)
  202. {
  203. mythr->queue_full = true;
  204. proc->device_data = NULL;
  205. }
  206. if (buf[j] != 3)
  207. proc->deven = DEV_DISABLED;
  208. proc = proc->next_proc;
  209. if ((!proc) || proc->device == proc)
  210. goto nomorecores;
  211. }
  212. }
  213. nomorecores: ;
  214. }
  215. cgpu->device_data = knc = malloc(sizeof(*knc));
  216. spi = malloc(sizeof(*spi));
  217. *knc = (struct knc_device){
  218. .i2c = i2c,
  219. .spi = spi,
  220. .workqueue_max = 1,
  221. };
  222. *spi = (struct spi_port){
  223. .txrx = knc_spi_txrx,
  224. .cgpu = cgpu,
  225. .repr = knc_drv.dname,
  226. .logprio = LOG_ERR,
  227. .speed = KNC_SPI_SPEED,
  228. .delay = KNC_SPI_DELAY,
  229. .mode = KNC_SPI_MODE,
  230. .bits = KNC_SPI_BITS,
  231. };
  232. if (!knc_spi_open(cgpu->dev_repr, spi))
  233. return false;
  234. timer_set_now(&thr->tv_poll);
  235. return true;
  236. }
  237. static
  238. void knc_remove_local_queue(struct knc_device * const knc, struct work * const work)
  239. {
  240. DL_DELETE(knc->workqueue, work);
  241. free_work(work);
  242. --knc->workqueue_size;
  243. }
  244. static
  245. void knc_prune_local_queue(struct thr_info *thr)
  246. {
  247. struct cgpu_info * const cgpu = thr->cgpu;
  248. struct knc_device * const knc = cgpu->device_data;
  249. struct work *work, *tmp;
  250. DL_FOREACH_SAFE(knc->workqueue, work, tmp)
  251. {
  252. if (stale_work(work, false))
  253. knc_remove_local_queue(knc, work);
  254. }
  255. thr->queue_full = (knc->workqueue_size >= knc->workqueue_max);
  256. }
  257. static
  258. bool knc_queue_append(struct thr_info * const thr, struct work * const work)
  259. {
  260. struct cgpu_info * const cgpu = thr->cgpu;
  261. struct knc_device * const knc = cgpu->device_data;
  262. if (knc->workqueue_size >= knc->workqueue_max)
  263. {
  264. knc_prune_local_queue(thr);
  265. if (thr->queue_full)
  266. return false;
  267. }
  268. DL_APPEND(knc->workqueue, work);
  269. ++knc->workqueue_size;
  270. thr->queue_full = (knc->workqueue_size >= knc->workqueue_max);
  271. if (thr->queue_full)
  272. knc_prune_local_queue(thr);
  273. return true;
  274. }
  275. static
  276. void knc_queue_flush(struct thr_info * const thr)
  277. {
  278. struct cgpu_info * const cgpu = thr->cgpu;
  279. struct knc_device * const knc = cgpu->device_data;
  280. struct work *work, *tmp;
  281. if (!knc)
  282. return;
  283. DL_FOREACH_SAFE(knc->workqueue, work, tmp)
  284. {
  285. knc_remove_local_queue(knc, work);
  286. }
  287. thr->queue_full = false;
  288. }
  289. static inline
  290. uint16_t get_u16be(const void * const p)
  291. {
  292. const uint8_t * const b = p;
  293. return (((uint16_t)b[0]) << 8) | b[1];
  294. }
  295. static inline
  296. uint32_t get_u32be(const void * const p)
  297. {
  298. const uint8_t * const b = p;
  299. return (((uint32_t)b[0]) << 0x18)
  300. | (((uint32_t)b[1]) << 0x10)
  301. | (((uint32_t)b[2]) << 8)
  302. | b[3];
  303. }
  304. static
  305. void knc_poll(struct thr_info * const thr)
  306. {
  307. struct thr_info *mythr;
  308. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  309. struct knc_device * const knc = cgpu->device_data;
  310. struct spi_port * const spi = knc->spi;
  311. struct knc_core *knccore;
  312. struct work *work, *tmp;
  313. uint8_t buf[0x30], *rxbuf;
  314. int works_sent = 0, asicno, i;
  315. uint16_t workaccept;
  316. int workid = knc->next_id;
  317. uint32_t nonce, coreno;
  318. size_t spi_req_sz = 0x1000;
  319. unsigned long delay_usecs = KNC_POLL_INTERVAL_US;
  320. knc_prune_local_queue(thr);
  321. spi_clear_buf(spi);
  322. DL_FOREACH(knc->workqueue, work)
  323. {
  324. buf[0] = KNC_REQ_SUBMIT_WORK << 4;
  325. buf[1] = 0;
  326. buf[2] = (workid >> 8) & 0x7f;
  327. buf[3] = workid & 0xff;
  328. for (i = 0; i < 0x20; ++i)
  329. buf[4 + i] = work->midstate[0x1f - i];
  330. for (i = 0; i < 0xc; ++i)
  331. buf[0x24 + i] = work->data[0x4b - i];
  332. spi_emit_buf(spi, buf, sizeof(buf));
  333. ++works_sent;
  334. ++workid;
  335. }
  336. spi_emit_nop(spi, spi_req_sz - spi_getbufsz(spi));
  337. spi_txrx(spi);
  338. rxbuf = spi_getrxbuf(spi);
  339. if (rxbuf[3] & 1)
  340. applog(LOG_DEBUG, "%s: Receive buffer overflow reported", knc_drv.dname);
  341. workaccept = get_u16be(&rxbuf[6]);
  342. applog(LOG_DEBUG, "%s: %lu/%d jobs accepted to queue (max=%d)",
  343. knc_drv.dname, (unsigned long)workaccept, works_sent, knc->workqueue_max);
  344. if (workaccept)
  345. {
  346. if (workaccept >= knc->workqueue_max)
  347. {
  348. knc->workqueue_max = workaccept;
  349. delay_usecs = 0;
  350. }
  351. DL_FOREACH_SAFE(knc->workqueue, work, tmp)
  352. {
  353. --knc->workqueue_size;
  354. DL_DELETE(knc->workqueue, work);
  355. work->device_id = knc->next_id++ & 0x7fff;
  356. HASH_ADD_INT(knc->devicework, device_id, work);
  357. if (!--workaccept)
  358. break;
  359. }
  360. thr->queue_full = (knc->workqueue_size >= knc->workqueue_max);
  361. }
  362. while (true)
  363. {
  364. rxbuf += 0xc;
  365. spi_req_sz -= 0xc;
  366. if (spi_req_sz < 0xc)
  367. break;
  368. const int rtype = rxbuf[0] >> 6;
  369. if (rtype && opt_debug)
  370. {
  371. char x[(0xc * 2) + 1];
  372. bin2hex(x, rxbuf, 0xc);
  373. applog(LOG_DEBUG, "%s: RECV: %s", knc_drv.dname, x);
  374. }
  375. if (rtype != KNC_REPLY_NONCE_FOUND && rtype != KNC_REPLY_WORK_DONE)
  376. continue;
  377. asicno = (rxbuf[0] & 0x38) >> 3;
  378. coreno = get_u32be(&rxbuf[8]);
  379. proc = cgpu;
  380. while (true)
  381. {
  382. knccore = proc->thr[0]->cgpu_data;
  383. if (knccore->asicno == asicno)
  384. break;
  385. do {
  386. proc = proc->next_proc;
  387. } while(proc != proc->device);
  388. }
  389. for (i = 0; i < coreno; ++i)
  390. proc = proc->next_proc;
  391. mythr = proc->thr[0];
  392. i = get_u16be(&rxbuf[2]);
  393. HASH_FIND_INT(knc->devicework, &i, work);
  394. if (!work)
  395. {
  396. const char * const msgtype = (rtype == KNC_REPLY_NONCE_FOUND) ? "nonce found" : "work done";
  397. applog(LOG_WARNING, "%"PRIpreprv": Got %s message about unknown work 0x%04x",
  398. proc->proc_repr, msgtype, i);
  399. if (KNC_REPLY_NONCE_FOUND == rtype)
  400. {
  401. nonce = get_u32be(&rxbuf[4]);
  402. nonce = le32toh(nonce);
  403. inc_hw_errors2(mythr, NULL, &nonce);
  404. }
  405. else
  406. inc_hw_errors2(mythr, NULL, NULL);
  407. continue;
  408. }
  409. switch (rtype)
  410. {
  411. case KNC_REPLY_NONCE_FOUND:
  412. nonce = get_u32be(&rxbuf[4]);
  413. nonce = le32toh(nonce);
  414. submit_nonce(mythr, work, nonce);
  415. break;
  416. case KNC_REPLY_WORK_DONE:
  417. HASH_DEL(knc->devicework, work);
  418. free_work(work);
  419. hashes_done2(mythr, 0x100000000, NULL);
  420. break;
  421. }
  422. }
  423. timer_set_delay_from_now(&thr->tv_poll, delay_usecs);
  424. }
  425. struct device_drv knc_drv = {
  426. .dname = "knc",
  427. .name = "KNC",
  428. .drv_detect = knc_detect,
  429. .thread_init = knc_init,
  430. .minerloop = minerloop_queue,
  431. .queue_append = knc_queue_append,
  432. .queue_flush = knc_queue_flush,
  433. .poll = knc_poll,
  434. };