driver-knc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. #define KNC_I2C_TEMPLATE "/dev/i2c-%d"
  35. enum knc_request_cmd {
  36. KNC_REQ_SUBMIT_WORK = 2,
  37. KNC_REQ_FLUSH_QUEUE = 3,
  38. };
  39. enum knc_reply_type {
  40. KNC_REPLY_NONCE_FOUND = 1,
  41. KNC_REPLY_WORK_DONE = 2,
  42. };
  43. struct device_drv knc_drv;
  44. struct knc_device {
  45. int i2c;
  46. struct spi_port *spi;
  47. bool need_flush;
  48. struct work *workqueue;
  49. int workqueue_size;
  50. int workqueue_max;
  51. int next_id;
  52. struct work *devicework;
  53. };
  54. struct knc_core {
  55. int asicno;
  56. };
  57. static
  58. bool knc_detect_one(const char *devpath)
  59. {
  60. static struct cgpu_info *prev_cgpu = NULL;
  61. struct cgpu_info *cgpu;
  62. int i;
  63. const int fd = open(i2cpath, O_RDWR);
  64. char *leftover = NULL;
  65. const int i2cslave = strtol(devpath, &leftover, 0);
  66. uint8_t buf[0x20];
  67. if (leftover && leftover[0])
  68. return false;
  69. if (unlikely(fd == -1))
  70. {
  71. applog(LOG_DEBUG, "%s: Failed to open %s", __func__, i2cpath);
  72. return false;
  73. }
  74. if (ioctl(fd, I2C_SLAVE, i2cslave))
  75. {
  76. close(fd);
  77. applog(LOG_DEBUG, "%s: Failed to select i2c slave 0x%x",
  78. __func__, i2cslave);
  79. return false;
  80. }
  81. i = i2c_smbus_read_i2c_block_data(fd, 0, 0x20, buf);
  82. close(fd);
  83. if (-1 == i)
  84. {
  85. applog(LOG_DEBUG, "%s: 0x%x: Failed to read i2c block data",
  86. __func__, i2cslave);
  87. return false;
  88. }
  89. for (i = 0; ; ++i)
  90. {
  91. if (buf[i] == 3)
  92. break;
  93. if (i == 0x1f)
  94. return false;
  95. }
  96. cgpu = malloc(sizeof(*cgpu));
  97. *cgpu = (struct cgpu_info){
  98. .drv = &knc_drv,
  99. .device_path = strdup(devpath),
  100. .deven = DEV_ENABLED,
  101. .procs = 192,
  102. .threads = prev_cgpu ? 0 : 1,
  103. };
  104. const bool rv = add_cgpu_slave(cgpu, prev_cgpu);
  105. prev_cgpu = cgpu;
  106. return rv;
  107. }
  108. static int knc_detect_auto(void)
  109. {
  110. const int first = 0x20, last = 0x26;
  111. char devpath[4];
  112. int found = 0, i;
  113. for (i = first; i <= last; ++i)
  114. {
  115. sprintf(devpath, "%d", i);
  116. if (knc_detect_one(devpath))
  117. ++found;
  118. }
  119. return found;
  120. }
  121. static void knc_detect(void)
  122. {
  123. generic_detect(&knc_drv, knc_detect_one, knc_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  124. }
  125. static
  126. bool knc_spi_open(const char *repr, struct spi_port * const spi)
  127. {
  128. const char * const spipath = "/dev/spidev1.0";
  129. const int fd = open(spipath, O_RDWR);
  130. const uint8_t lsbfirst = 0;
  131. if (fd == -1)
  132. return false;
  133. if (ioctl(fd, SPI_IOC_WR_MODE , &spi->mode )) goto fail;
  134. if (ioctl(fd, SPI_IOC_WR_LSB_FIRST , &lsbfirst )) goto fail;
  135. if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spi->bits )) goto fail;
  136. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ , &spi->speed)) goto fail;
  137. spi->fd = fd;
  138. return true;
  139. fail:
  140. close(fd);
  141. spi->fd = -1;
  142. applog(LOG_WARNING, "%s: Failed to open %s", repr, spipath);
  143. return false;
  144. }
  145. static
  146. bool knc_spi_txrx(struct spi_port * const spi)
  147. {
  148. const void * const wrbuf = spi_gettxbuf(spi);
  149. void * const rdbuf = spi_getrxbuf(spi);
  150. const size_t bufsz = spi_getbufsz(spi);
  151. const int fd = spi->fd;
  152. struct spi_ioc_transfer xf = {
  153. .tx_buf = (uintptr_t) wrbuf,
  154. .rx_buf = (uintptr_t) rdbuf,
  155. .len = bufsz,
  156. .delay_usecs = spi->delay,
  157. .speed_hz = spi->speed,
  158. .bits_per_word = spi->bits,
  159. };
  160. return (ioctl(fd, SPI_IOC_MESSAGE(1), &xf) > 0);
  161. }
  162. static
  163. void knc_clean_flush(struct spi_port * const spi)
  164. {
  165. const uint8_t flushcmd = KNC_REQ_FLUSH_QUEUE << 4;
  166. const size_t spi_req_sz = 0x1000;
  167. spi_clear_buf(spi);
  168. spi_emit_buf(spi, &flushcmd, 1);
  169. spi_emit_nop(spi, spi_req_sz - spi_getbufsz(spi));
  170. applog(LOG_DEBUG, "%s: Issuing flush command to clear out device queues", knc_drv.dname);
  171. spi_txrx(spi);
  172. }
  173. static
  174. bool knc_init(struct thr_info * const thr)
  175. {
  176. const int max_cores = 192;
  177. struct thr_info *mythr;
  178. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  179. struct knc_device *knc;
  180. struct knc_core *knccore;
  181. struct spi_port *spi;
  182. const int i2c = open(i2cpath, O_RDWR);
  183. int i2cslave, i, j;
  184. uint8_t buf[0x20];
  185. if (unlikely(i2c == -1))
  186. {
  187. applog(LOG_DEBUG, "%s: Failed to open %s", __func__, i2cpath);
  188. return false;
  189. }
  190. for (proc = cgpu; proc; )
  191. {
  192. if (proc->device != proc)
  193. {
  194. applog(LOG_WARNING, "%"PRIpreprv": Extra processor?", proc->proc_repr);
  195. continue;
  196. }
  197. i2cslave = atoi(proc->device_path);
  198. if (ioctl(i2c, I2C_SLAVE, i2cslave))
  199. {
  200. applog(LOG_DEBUG, "%s: Failed to select i2c slave 0x%x",
  201. __func__, i2cslave);
  202. return false;
  203. }
  204. for (i = 0; i < max_cores; i += 0x20)
  205. {
  206. i2c_smbus_read_i2c_block_data(i2c, i, 0x20, buf);
  207. for (j = 0; j < 0x20; ++j)
  208. {
  209. mythr = proc->thr[0];
  210. mythr->cgpu_data = knccore = malloc(sizeof(*knccore));
  211. *knccore = (struct knc_core){
  212. .asicno = i2cslave - 0x20,
  213. };
  214. if (proc != cgpu)
  215. {
  216. mythr->queue_full = true;
  217. proc->device_data = NULL;
  218. }
  219. if (buf[j] != 3)
  220. proc->deven = DEV_DISABLED;
  221. proc = proc->next_proc;
  222. if ((!proc) || proc->device == proc)
  223. goto nomorecores;
  224. }
  225. }
  226. nomorecores: ;
  227. }
  228. cgpu->device_data = knc = malloc(sizeof(*knc));
  229. spi = malloc(sizeof(*spi));
  230. *knc = (struct knc_device){
  231. .i2c = i2c,
  232. .spi = spi,
  233. .workqueue_max = 1,
  234. };
  235. *spi = (struct spi_port){
  236. .txrx = knc_spi_txrx,
  237. .cgpu = cgpu,
  238. .repr = knc_drv.dname,
  239. .logprio = LOG_ERR,
  240. .speed = KNC_SPI_SPEED,
  241. .delay = KNC_SPI_DELAY,
  242. .mode = KNC_SPI_MODE,
  243. .bits = KNC_SPI_BITS,
  244. };
  245. if (!knc_spi_open(cgpu->dev_repr, spi))
  246. return false;
  247. knc_clean_flush(spi);
  248. timer_set_now(&thr->tv_poll);
  249. return true;
  250. }
  251. static
  252. void knc_remove_local_queue(struct knc_device * const knc, struct work * const work)
  253. {
  254. DL_DELETE(knc->workqueue, work);
  255. free_work(work);
  256. --knc->workqueue_size;
  257. }
  258. static
  259. void knc_prune_local_queue(struct thr_info *thr)
  260. {
  261. struct cgpu_info * const cgpu = thr->cgpu;
  262. struct knc_device * const knc = cgpu->device_data;
  263. struct work *work, *tmp;
  264. DL_FOREACH_SAFE(knc->workqueue, work, tmp)
  265. {
  266. if (stale_work(work, false))
  267. knc_remove_local_queue(knc, work);
  268. }
  269. thr->queue_full = (knc->workqueue_size >= knc->workqueue_max);
  270. }
  271. static
  272. bool knc_queue_append(struct thr_info * const thr, struct work * const work)
  273. {
  274. struct cgpu_info * const cgpu = thr->cgpu;
  275. struct knc_device * const knc = cgpu->device_data;
  276. if (knc->workqueue_size >= knc->workqueue_max)
  277. {
  278. knc_prune_local_queue(thr);
  279. if (thr->queue_full)
  280. return false;
  281. }
  282. DL_APPEND(knc->workqueue, work);
  283. ++knc->workqueue_size;
  284. thr->queue_full = (knc->workqueue_size >= knc->workqueue_max);
  285. if (thr->queue_full)
  286. knc_prune_local_queue(thr);
  287. return true;
  288. }
  289. #define HASH_LAST_ADDED(head, out) \
  290. (out = (head) ? (ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail)) : NULL)
  291. static
  292. void knc_queue_flush(struct thr_info * const thr)
  293. {
  294. struct cgpu_info * const cgpu = thr->cgpu;
  295. struct knc_device * const knc = cgpu->device_data;
  296. struct work *work, *tmp;
  297. if (!knc)
  298. return;
  299. DL_FOREACH_SAFE(knc->workqueue, work, tmp)
  300. {
  301. knc_remove_local_queue(knc, work);
  302. }
  303. thr->queue_full = false;
  304. HASH_LAST_ADDED(knc->devicework, work);
  305. if (work && stale_work(work, true))
  306. {
  307. knc->need_flush = true;
  308. timer_set_now(&thr->tv_poll);
  309. }
  310. }
  311. static inline
  312. uint16_t get_u16be(const void * const p)
  313. {
  314. const uint8_t * const b = p;
  315. return (((uint16_t)b[0]) << 8) | b[1];
  316. }
  317. static inline
  318. uint32_t get_u32be(const void * const p)
  319. {
  320. const uint8_t * const b = p;
  321. return (((uint32_t)b[0]) << 0x18)
  322. | (((uint32_t)b[1]) << 0x10)
  323. | (((uint32_t)b[2]) << 8)
  324. | b[3];
  325. }
  326. static
  327. void knc_poll(struct thr_info * const thr)
  328. {
  329. struct thr_info *mythr;
  330. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  331. struct knc_device * const knc = cgpu->device_data;
  332. struct spi_port * const spi = knc->spi;
  333. struct knc_core *knccore;
  334. struct work *work, *tmp;
  335. uint8_t buf[0x30], *rxbuf;
  336. int works_sent = 0, asicno, i;
  337. uint16_t workaccept;
  338. int workid = knc->next_id;
  339. uint32_t nonce, coreno;
  340. size_t spi_req_sz = 0x1000;
  341. unsigned long delay_usecs = KNC_POLL_INTERVAL_US;
  342. knc_prune_local_queue(thr);
  343. spi_clear_buf(spi);
  344. if (knc->need_flush)
  345. {
  346. applog(LOG_NOTICE, "%s: Abandoning stale searches to restart", knc_drv.dname);
  347. buf[0] = KNC_REQ_FLUSH_QUEUE << 4;
  348. spi_emit_buf(spi, buf, sizeof(buf));
  349. }
  350. DL_FOREACH(knc->workqueue, work)
  351. {
  352. buf[0] = KNC_REQ_SUBMIT_WORK << 4;
  353. buf[1] = 0;
  354. buf[2] = (workid >> 8) & 0x7f;
  355. buf[3] = workid & 0xff;
  356. for (i = 0; i < 0x20; ++i)
  357. buf[4 + i] = work->midstate[0x1f - i];
  358. for (i = 0; i < 0xc; ++i)
  359. buf[0x24 + i] = work->data[0x4b - i];
  360. spi_emit_buf(spi, buf, sizeof(buf));
  361. ++works_sent;
  362. ++workid;
  363. }
  364. spi_emit_nop(spi, spi_req_sz - spi_getbufsz(spi));
  365. spi_txrx(spi);
  366. rxbuf = spi_getrxbuf(spi);
  367. if (rxbuf[3] & 1)
  368. applog(LOG_DEBUG, "%s: Receive buffer overflow reported", knc_drv.dname);
  369. workaccept = get_u16be(&rxbuf[6]);
  370. applog(LOG_DEBUG, "%s: %lu/%d jobs accepted to queue (max=%d)",
  371. knc_drv.dname, (unsigned long)workaccept, works_sent, knc->workqueue_max);
  372. while (true)
  373. {
  374. rxbuf += 0xc;
  375. spi_req_sz -= 0xc;
  376. if (spi_req_sz < 0xc)
  377. break;
  378. const int rtype = rxbuf[0] >> 6;
  379. if (rtype && opt_debug)
  380. {
  381. char x[(0xc * 2) + 1];
  382. bin2hex(x, rxbuf, 0xc);
  383. applog(LOG_DEBUG, "%s: RECV: %s", knc_drv.dname, x);
  384. }
  385. if (rtype != KNC_REPLY_NONCE_FOUND && rtype != KNC_REPLY_WORK_DONE)
  386. continue;
  387. asicno = (rxbuf[0] & 0x38) >> 3;
  388. coreno = get_u32be(&rxbuf[8]);
  389. proc = cgpu;
  390. while (true)
  391. {
  392. knccore = proc->thr[0]->cgpu_data;
  393. if (knccore->asicno == asicno)
  394. break;
  395. do {
  396. proc = proc->next_proc;
  397. } while(proc != proc->device);
  398. }
  399. for (i = 0; i < coreno; ++i)
  400. proc = proc->next_proc;
  401. mythr = proc->thr[0];
  402. i = get_u16be(&rxbuf[2]);
  403. HASH_FIND_INT(knc->devicework, &i, work);
  404. if (!work)
  405. {
  406. const char * const msgtype = (rtype == KNC_REPLY_NONCE_FOUND) ? "nonce found" : "work done";
  407. applog(LOG_WARNING, "%"PRIpreprv": Got %s message about unknown work 0x%04x",
  408. proc->proc_repr, msgtype, i);
  409. if (KNC_REPLY_NONCE_FOUND == rtype)
  410. {
  411. nonce = get_u32be(&rxbuf[4]);
  412. nonce = le32toh(nonce);
  413. inc_hw_errors2(mythr, NULL, &nonce);
  414. }
  415. else
  416. inc_hw_errors2(mythr, NULL, NULL);
  417. continue;
  418. }
  419. switch (rtype)
  420. {
  421. case KNC_REPLY_NONCE_FOUND:
  422. nonce = get_u32be(&rxbuf[4]);
  423. nonce = le32toh(nonce);
  424. submit_nonce(mythr, work, nonce);
  425. break;
  426. case KNC_REPLY_WORK_DONE:
  427. HASH_DEL(knc->devicework, work);
  428. free_work(work);
  429. hashes_done2(mythr, 0x100000000, NULL);
  430. break;
  431. }
  432. }
  433. if (knc->need_flush)
  434. {
  435. knc->need_flush = false;
  436. HASH_ITER(hh, knc->devicework, work, tmp)
  437. {
  438. HASH_DEL(knc->devicework, work);
  439. free_work(work);
  440. }
  441. delay_usecs = 0;
  442. }
  443. if (workaccept)
  444. {
  445. if (workaccept >= knc->workqueue_max)
  446. {
  447. knc->workqueue_max = workaccept;
  448. delay_usecs = 0;
  449. }
  450. DL_FOREACH_SAFE(knc->workqueue, work, tmp)
  451. {
  452. --knc->workqueue_size;
  453. DL_DELETE(knc->workqueue, work);
  454. work->device_id = knc->next_id++ & 0x7fff;
  455. HASH_ADD_INT(knc->devicework, device_id, work);
  456. if (!--workaccept)
  457. break;
  458. }
  459. thr->queue_full = (knc->workqueue_size >= knc->workqueue_max);
  460. }
  461. timer_set_delay_from_now(&thr->tv_poll, delay_usecs);
  462. }
  463. static
  464. bool knc_get_stats(struct cgpu_info * const cgpu)
  465. {
  466. if (cgpu->device != cgpu)
  467. return true;
  468. struct knc_core * const knccore = cgpu->thr[0]->cgpu_data;
  469. struct cgpu_info *proc;
  470. const int i2cdev = knccore->asicno + 3;
  471. const int i2cslave = 0x48;
  472. int i2c;
  473. int32_t rawtemp;
  474. float temp;
  475. bool rv = false;
  476. char i2cpath[sizeof(KNC_I2C_TEMPLATE)];
  477. sprintf(i2cpath, KNC_I2C_TEMPLATE, i2cdev);
  478. i2c = open(i2cpath, O_RDWR);
  479. if (i2c == -1)
  480. {
  481. applog(LOG_DEBUG, "%s: %s: Failed to open %s",
  482. cgpu->dev_repr, __func__, i2cpath);
  483. return false;
  484. }
  485. if (ioctl(i2c, I2C_SLAVE, i2cslave))
  486. {
  487. applog(LOG_DEBUG, "%s: %s: Failed to select i2c slave 0x%x",
  488. cgpu->dev_repr, __func__, i2cslave);
  489. goto out;
  490. }
  491. rawtemp = i2c_smbus_read_word_data(i2c, 0);
  492. if (rawtemp == -1)
  493. goto out;
  494. temp = ((float)(rawtemp & 0xff));
  495. if (rawtemp & 0x100)
  496. temp += 0.5;
  497. for (proc = cgpu; proc && proc->device == cgpu; proc = proc->next_proc)
  498. proc->temp = temp;
  499. rv = true;
  500. out:
  501. close(i2c);
  502. return rv;
  503. }
  504. struct device_drv knc_drv = {
  505. .dname = "knc",
  506. .name = "KNC",
  507. .drv_detect = knc_detect,
  508. .thread_init = knc_init,
  509. .minerloop = minerloop_queue,
  510. .queue_append = knc_queue_append,
  511. .queue_flush = knc_queue_flush,
  512. .poll = knc_poll,
  513. .get_stats = knc_get_stats,
  514. };