driver-knc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. void knc_clean_flush(struct spi_port * const spi)
  162. {
  163. const uint8_t flushcmd = KNC_REQ_FLUSH_QUEUE << 4;
  164. const size_t spi_req_sz = 0x1000;
  165. spi_clear_buf(spi);
  166. spi_emit_buf(spi, &flushcmd, 1);
  167. spi_emit_nop(spi, spi_req_sz - spi_getbufsz(spi));
  168. applog(LOG_DEBUG, "%s: Issuing flush command to clear out device queues", knc_drv.dname);
  169. spi_txrx(spi);
  170. }
  171. static
  172. bool knc_init(struct thr_info * const thr)
  173. {
  174. const int max_cores = 192;
  175. struct thr_info *mythr;
  176. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  177. struct knc_device *knc;
  178. struct knc_core *knccore;
  179. struct spi_port *spi;
  180. const int i2c = open(i2cpath, O_RDWR);
  181. int i2cslave, i, j;
  182. uint8_t buf[0x20];
  183. if (unlikely(i2c == -1))
  184. {
  185. applog(LOG_DEBUG, "%s: Failed to open %s", __func__, i2cpath);
  186. return false;
  187. }
  188. for (proc = cgpu; proc; )
  189. {
  190. if (proc->device != proc)
  191. {
  192. applog(LOG_WARNING, "%"PRIpreprv": Extra processor?", proc->proc_repr);
  193. continue;
  194. }
  195. i2cslave = atoi(proc->device_path);
  196. if (ioctl(i2c, I2C_SLAVE, i2cslave))
  197. {
  198. applog(LOG_DEBUG, "%s: Failed to select i2c slave 0x%x",
  199. __func__, i2cslave);
  200. return false;
  201. }
  202. for (i = 0; i < max_cores; i += 0x20)
  203. {
  204. i2c_smbus_read_i2c_block_data(i2c, i, 0x20, buf);
  205. for (j = 0; j < 0x20; ++j)
  206. {
  207. mythr = proc->thr[0];
  208. mythr->cgpu_data = knccore = malloc(sizeof(*knccore));
  209. *knccore = (struct knc_core){
  210. .asicno = i2cslave - 0x20,
  211. };
  212. if (proc != cgpu)
  213. {
  214. mythr->queue_full = true;
  215. proc->device_data = NULL;
  216. }
  217. if (buf[j] != 3)
  218. proc->deven = DEV_DISABLED;
  219. proc = proc->next_proc;
  220. if ((!proc) || proc->device == proc)
  221. goto nomorecores;
  222. }
  223. }
  224. nomorecores: ;
  225. }
  226. cgpu->device_data = knc = malloc(sizeof(*knc));
  227. spi = malloc(sizeof(*spi));
  228. *knc = (struct knc_device){
  229. .i2c = i2c,
  230. .spi = spi,
  231. .workqueue_max = 1,
  232. };
  233. *spi = (struct spi_port){
  234. .txrx = knc_spi_txrx,
  235. .cgpu = cgpu,
  236. .repr = knc_drv.dname,
  237. .logprio = LOG_ERR,
  238. .speed = KNC_SPI_SPEED,
  239. .delay = KNC_SPI_DELAY,
  240. .mode = KNC_SPI_MODE,
  241. .bits = KNC_SPI_BITS,
  242. };
  243. if (!knc_spi_open(cgpu->dev_repr, spi))
  244. return false;
  245. knc_clean_flush(spi);
  246. timer_set_now(&thr->tv_poll);
  247. return true;
  248. }
  249. static
  250. void knc_remove_local_queue(struct knc_device * const knc, struct work * const work)
  251. {
  252. DL_DELETE(knc->workqueue, work);
  253. free_work(work);
  254. --knc->workqueue_size;
  255. }
  256. static
  257. void knc_prune_local_queue(struct thr_info *thr)
  258. {
  259. struct cgpu_info * const cgpu = thr->cgpu;
  260. struct knc_device * const knc = cgpu->device_data;
  261. struct work *work, *tmp;
  262. DL_FOREACH_SAFE(knc->workqueue, work, tmp)
  263. {
  264. if (stale_work(work, false))
  265. knc_remove_local_queue(knc, work);
  266. }
  267. thr->queue_full = (knc->workqueue_size >= knc->workqueue_max);
  268. }
  269. static
  270. bool knc_queue_append(struct thr_info * const thr, struct work * const work)
  271. {
  272. struct cgpu_info * const cgpu = thr->cgpu;
  273. struct knc_device * const knc = cgpu->device_data;
  274. if (knc->workqueue_size >= knc->workqueue_max)
  275. {
  276. knc_prune_local_queue(thr);
  277. if (thr->queue_full)
  278. return false;
  279. }
  280. DL_APPEND(knc->workqueue, work);
  281. ++knc->workqueue_size;
  282. thr->queue_full = (knc->workqueue_size >= knc->workqueue_max);
  283. if (thr->queue_full)
  284. knc_prune_local_queue(thr);
  285. return true;
  286. }
  287. static
  288. void knc_queue_flush(struct thr_info * const thr)
  289. {
  290. struct cgpu_info * const cgpu = thr->cgpu;
  291. struct knc_device * const knc = cgpu->device_data;
  292. struct work *work, *tmp;
  293. if (!knc)
  294. return;
  295. DL_FOREACH_SAFE(knc->workqueue, work, tmp)
  296. {
  297. knc_remove_local_queue(knc, work);
  298. }
  299. thr->queue_full = false;
  300. }
  301. static inline
  302. uint16_t get_u16be(const void * const p)
  303. {
  304. const uint8_t * const b = p;
  305. return (((uint16_t)b[0]) << 8) | b[1];
  306. }
  307. static inline
  308. uint32_t get_u32be(const void * const p)
  309. {
  310. const uint8_t * const b = p;
  311. return (((uint32_t)b[0]) << 0x18)
  312. | (((uint32_t)b[1]) << 0x10)
  313. | (((uint32_t)b[2]) << 8)
  314. | b[3];
  315. }
  316. static
  317. void knc_poll(struct thr_info * const thr)
  318. {
  319. struct thr_info *mythr;
  320. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  321. struct knc_device * const knc = cgpu->device_data;
  322. struct spi_port * const spi = knc->spi;
  323. struct knc_core *knccore;
  324. struct work *work, *tmp;
  325. uint8_t buf[0x30], *rxbuf;
  326. int works_sent = 0, asicno, i;
  327. uint16_t workaccept;
  328. int workid = knc->next_id;
  329. uint32_t nonce, coreno;
  330. size_t spi_req_sz = 0x1000;
  331. unsigned long delay_usecs = KNC_POLL_INTERVAL_US;
  332. knc_prune_local_queue(thr);
  333. spi_clear_buf(spi);
  334. DL_FOREACH(knc->workqueue, work)
  335. {
  336. buf[0] = KNC_REQ_SUBMIT_WORK << 4;
  337. buf[1] = 0;
  338. buf[2] = (workid >> 8) & 0x7f;
  339. buf[3] = workid & 0xff;
  340. for (i = 0; i < 0x20; ++i)
  341. buf[4 + i] = work->midstate[0x1f - i];
  342. for (i = 0; i < 0xc; ++i)
  343. buf[0x24 + i] = work->data[0x4b - i];
  344. spi_emit_buf(spi, buf, sizeof(buf));
  345. ++works_sent;
  346. ++workid;
  347. }
  348. spi_emit_nop(spi, spi_req_sz - spi_getbufsz(spi));
  349. spi_txrx(spi);
  350. rxbuf = spi_getrxbuf(spi);
  351. if (rxbuf[3] & 1)
  352. applog(LOG_DEBUG, "%s: Receive buffer overflow reported", knc_drv.dname);
  353. workaccept = get_u16be(&rxbuf[6]);
  354. applog(LOG_DEBUG, "%s: %lu/%d jobs accepted to queue (max=%d)",
  355. knc_drv.dname, (unsigned long)workaccept, works_sent, knc->workqueue_max);
  356. if (workaccept)
  357. {
  358. if (workaccept >= knc->workqueue_max)
  359. {
  360. knc->workqueue_max = workaccept;
  361. delay_usecs = 0;
  362. }
  363. DL_FOREACH_SAFE(knc->workqueue, work, tmp)
  364. {
  365. --knc->workqueue_size;
  366. DL_DELETE(knc->workqueue, work);
  367. work->device_id = knc->next_id++ & 0x7fff;
  368. HASH_ADD_INT(knc->devicework, device_id, work);
  369. if (!--workaccept)
  370. break;
  371. }
  372. thr->queue_full = (knc->workqueue_size >= knc->workqueue_max);
  373. }
  374. while (true)
  375. {
  376. rxbuf += 0xc;
  377. spi_req_sz -= 0xc;
  378. if (spi_req_sz < 0xc)
  379. break;
  380. const int rtype = rxbuf[0] >> 6;
  381. if (rtype && opt_debug)
  382. {
  383. char x[(0xc * 2) + 1];
  384. bin2hex(x, rxbuf, 0xc);
  385. applog(LOG_DEBUG, "%s: RECV: %s", knc_drv.dname, x);
  386. }
  387. if (rtype != KNC_REPLY_NONCE_FOUND && rtype != KNC_REPLY_WORK_DONE)
  388. continue;
  389. asicno = (rxbuf[0] & 0x38) >> 3;
  390. coreno = get_u32be(&rxbuf[8]);
  391. proc = cgpu;
  392. while (true)
  393. {
  394. knccore = proc->thr[0]->cgpu_data;
  395. if (knccore->asicno == asicno)
  396. break;
  397. do {
  398. proc = proc->next_proc;
  399. } while(proc != proc->device);
  400. }
  401. for (i = 0; i < coreno; ++i)
  402. proc = proc->next_proc;
  403. mythr = proc->thr[0];
  404. i = get_u16be(&rxbuf[2]);
  405. HASH_FIND_INT(knc->devicework, &i, work);
  406. if (!work)
  407. {
  408. const char * const msgtype = (rtype == KNC_REPLY_NONCE_FOUND) ? "nonce found" : "work done";
  409. applog(LOG_WARNING, "%"PRIpreprv": Got %s message about unknown work 0x%04x",
  410. proc->proc_repr, msgtype, i);
  411. if (KNC_REPLY_NONCE_FOUND == rtype)
  412. {
  413. nonce = get_u32be(&rxbuf[4]);
  414. nonce = le32toh(nonce);
  415. inc_hw_errors2(mythr, NULL, &nonce);
  416. }
  417. else
  418. inc_hw_errors2(mythr, NULL, NULL);
  419. continue;
  420. }
  421. switch (rtype)
  422. {
  423. case KNC_REPLY_NONCE_FOUND:
  424. nonce = get_u32be(&rxbuf[4]);
  425. nonce = le32toh(nonce);
  426. submit_nonce(mythr, work, nonce);
  427. break;
  428. case KNC_REPLY_WORK_DONE:
  429. HASH_DEL(knc->devicework, work);
  430. free_work(work);
  431. hashes_done2(mythr, 0x100000000, NULL);
  432. break;
  433. }
  434. }
  435. timer_set_delay_from_now(&thr->tv_poll, delay_usecs);
  436. }
  437. struct device_drv knc_drv = {
  438. .dname = "knc",
  439. .name = "KNC",
  440. .drv_detect = knc_detect,
  441. .thread_init = knc_init,
  442. .minerloop = minerloop_queue,
  443. .queue_append = knc_queue_append,
  444. .queue_flush = knc_queue_flush,
  445. .poll = knc_poll,
  446. };