driver-minion.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright 2014 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 <stdlib.h>
  14. #include <string.h>
  15. #include <linux/spi/spidev.h>
  16. #include <utlist.h>
  17. #include "deviceapi.h"
  18. #include "logging.h"
  19. #include "lowl-spi.h"
  20. #include "miner.h"
  21. #include "util.h"
  22. static const uint8_t minion_max_chipid = 0x1f;
  23. static const uint8_t minion_chip_signature[] = {0x44, 0x8a, 0xac, 0xb1};
  24. static const unsigned minion_max_queued = 0x10;
  25. static const unsigned minion_poll_us = 10000;
  26. enum minion_register {
  27. MRA_SIGNATURE = 0x00,
  28. MRA_STATUS = 0x01,
  29. MRA_MISC_CTL = 0x06,
  30. MRA_RESET = 0x07,
  31. MRA_FIFO_STATUS = 0x0b,
  32. MRA_RESULT = 0x20,
  33. MRA_TASK = 0x30,
  34. MRA_NONCE_START = 0x70,
  35. MRA_NONCE_INC = 0x71,
  36. };
  37. struct minion_chip {
  38. uint8_t chipid;
  39. uint8_t core_count;
  40. uint16_t next_taskid;
  41. struct cgpu_info *first_proc;
  42. unsigned queue_count;
  43. };
  44. struct minion_bus {
  45. struct spi_port *spi;
  46. };
  47. static
  48. void minion_get(struct spi_port * const spi, const uint8_t chipid, const uint8_t addr, void * const buf, const size_t bufsz)
  49. {
  50. const uint8_t header[] = {chipid, addr | 0x80, bufsz & 0xff, bufsz >> 8};
  51. spi_clear_buf(spi);
  52. spi_emit_buf(spi, header, sizeof(header));
  53. uint8_t dummy[bufsz];
  54. memset(dummy, 0xff, bufsz);
  55. spi_emit_buf(spi, dummy, bufsz);
  56. spi_txrx(spi);
  57. uint8_t * const rdbuf = spi_getrxbuf(spi);
  58. memcpy(buf, &rdbuf[sizeof(header)], bufsz);
  59. }
  60. static
  61. void minion_set(struct spi_port * const spi, const uint8_t chipid, const uint8_t addr, const void * const buf, const size_t bufsz)
  62. {
  63. const uint8_t header[] = {chipid, addr, bufsz & 0xff, bufsz >> 8};
  64. spi_clear_buf(spi);
  65. spi_emit_buf(spi, header, sizeof(header));
  66. spi_emit_buf(spi, buf, bufsz);
  67. spi_txrx(spi);
  68. }
  69. static
  70. unsigned minion_count_cores(struct spi_port * const spi)
  71. {
  72. uint8_t buf[max(4, sizeof(minion_chip_signature))];
  73. unsigned total_core_count = 0;
  74. for (unsigned chipid = 0; chipid <= minion_max_chipid; ++chipid)
  75. {
  76. minion_get(spi, chipid, MRA_SIGNATURE, buf, sizeof(minion_chip_signature));
  77. if (memcmp(buf, minion_chip_signature, sizeof(minion_chip_signature)))
  78. {
  79. for (unsigned i = 0; i < sizeof(minion_chip_signature); ++i)
  80. {
  81. if (buf[i] != 0xff)
  82. {
  83. char hex[(sizeof(minion_chip_signature) * 2) + 1];
  84. bin2hex(hex, buf, sizeof(minion_chip_signature));
  85. applog(LOG_DEBUG, "%s: chipid %u: Bad signature (%s)", spi->repr, chipid, hex);
  86. break;
  87. }
  88. }
  89. continue;
  90. }
  91. minion_get(spi, chipid, MRA_STATUS, buf, 4);
  92. const uint8_t core_count = buf[2];
  93. applog(LOG_DEBUG, "%s: chipid %u: Found %u cores", spi->repr, chipid, core_count);
  94. total_core_count += core_count;
  95. }
  96. return total_core_count;
  97. }
  98. static
  99. bool minion_init(struct thr_info * const thr)
  100. {
  101. struct cgpu_info * const dev = thr->cgpu, *proc = dev;
  102. struct minion_bus * const mbus = dev->device_data;
  103. struct spi_port * const spi = mbus->spi;
  104. uint8_t buf[max(4, sizeof(minion_chip_signature))];
  105. struct minion_chip * const chips = malloc(sizeof(*chips) * ((size_t)minion_max_chipid + 1));
  106. for (unsigned chipid = 0; proc; ++chipid)
  107. {
  108. struct minion_chip * const chip = &chips[chipid];
  109. spi->repr = proc->proc_repr;
  110. minion_get(spi, chipid, MRA_SIGNATURE, buf, sizeof(minion_chip_signature));
  111. if (memcmp(buf, minion_chip_signature, sizeof(minion_chip_signature)))
  112. continue;
  113. minion_get(spi, chipid, MRA_STATUS, buf, 4);
  114. if (!buf[2])
  115. continue;
  116. *chip = (struct minion_chip){
  117. .chipid = chipid,
  118. .core_count = buf[2],
  119. .first_proc = proc,
  120. };
  121. minion_set(spi, chipid, MRA_NONCE_START, "\0\0\0\0", 4);
  122. pk_u32le(buf, 0, 0xffffffff / chip->core_count);
  123. minion_set(spi, chipid, MRA_NONCE_INC, buf, 4);
  124. minion_get(spi, chipid, MRA_MISC_CTL, buf, 4);
  125. buf[0] |= 1 << 2; // Enable "no nonce" result reports
  126. minion_set(spi, chipid, MRA_MISC_CTL, buf, 4);
  127. timer_set_delay_from_now(&proc->thr[0]->tv_poll, minion_poll_us);
  128. for (unsigned i = 0; i < chip->core_count; ++i)
  129. {
  130. struct thr_info * const thr = proc->thr[0];
  131. thr->cgpu_data = chip;
  132. proc = proc->next_proc;
  133. }
  134. }
  135. return true;
  136. }
  137. static
  138. bool minion_queue_full(struct minion_chip * const chip)
  139. {
  140. struct cgpu_info *proc = chip->first_proc;
  141. struct thr_info *thr = proc->thr[0];
  142. const bool full = (chip->queue_count >= minion_max_queued);
  143. if (full != thr->queue_full)
  144. {
  145. for (unsigned i = 0; i < chip->core_count; (proc = proc->next_proc), ++i)
  146. {
  147. thr = proc->thr[0];
  148. thr->queue_full = full;
  149. }
  150. }
  151. return full;
  152. }
  153. static
  154. bool minion_queue_append(struct thr_info *thr, struct work * const work)
  155. {
  156. struct cgpu_info *proc = thr->cgpu;
  157. struct minion_bus * const mbus = proc->device_data;
  158. struct minion_chip * const chip = thr->cgpu_data;
  159. proc = chip->first_proc;
  160. thr = proc->thr[0];
  161. if (minion_queue_full(chip))
  162. return false;
  163. struct spi_port * const spi = mbus->spi;
  164. const uint8_t chipid = chip->chipid;
  165. uint8_t taskdata[0x30];
  166. spi->repr = proc->proc_repr;
  167. work->device_id = ++chip->next_taskid;
  168. work->tv_stamp.tv_sec = 1;
  169. pk_u16be(taskdata, 0, work->device_id);
  170. memset(&taskdata[2], 0, 2);
  171. memcpy(&taskdata[4], work->midstate, 0x20);
  172. memcpy(&taskdata[0x24], &work->data[0x40], 0xc);
  173. minion_set(spi, chipid, MRA_TASK, taskdata, sizeof(taskdata));
  174. DL_APPEND(thr->work_list, work);
  175. ++chip->queue_count;
  176. minion_queue_full(chip);
  177. return true;
  178. }
  179. static
  180. void minion_queue_flush(struct thr_info * const thr)
  181. {
  182. struct cgpu_info * const proc = thr->cgpu;
  183. struct minion_bus * const mbus = proc->device_data;
  184. struct minion_chip * const chip = thr->cgpu_data;
  185. if (proc != chip->first_proc)
  186. // Redundant, all queues flush at the same time
  187. return;
  188. const uint8_t chipid = chip->chipid;
  189. struct spi_port * const spi = mbus->spi;
  190. static const uint8_t flushcmd[4] = {0xfb, 0xff, 0xff, 0xff};
  191. minion_set(spi, chipid, MRA_RESET, flushcmd, sizeof(flushcmd));
  192. struct work *work;
  193. DL_FOREACH(thr->work_list, work)
  194. {
  195. work->tv_stamp.tv_sec = 0;
  196. }
  197. chip->queue_count = 0;
  198. minion_queue_full(chip);
  199. }
  200. static
  201. void minion_poll(struct thr_info * const chip_thr)
  202. {
  203. struct cgpu_info * const first_proc = chip_thr->cgpu;
  204. struct minion_bus * const mbus = first_proc->device_data;
  205. struct minion_chip * const chip = chip_thr->cgpu_data;
  206. struct spi_port * const spi = mbus->spi;
  207. const uint8_t chipid = chip->chipid;
  208. spi->repr = first_proc->proc_repr;
  209. uint8_t buf[4];
  210. minion_get(spi, chipid, MRA_FIFO_STATUS, buf, 4);
  211. const uint8_t res_fifo_len = buf[0];
  212. if (res_fifo_len)
  213. {
  214. static const size_t resbuf_i_len = 8;
  215. const size_t resbuf_len = (size_t)res_fifo_len * resbuf_i_len;
  216. uint8_t resbuf[resbuf_len], *resbuf_i = resbuf;
  217. minion_get(spi, chipid, MRA_RESULT, resbuf, resbuf_len);
  218. for (unsigned i = 0; i < res_fifo_len; (resbuf_i += resbuf_i_len), ++i)
  219. {
  220. const uint8_t coreid = resbuf_i[2];
  221. work_device_id_t taskid = upk_u16be(resbuf_i, 0);
  222. const bool have_nonce = !(resbuf_i[3] & 0x80);
  223. struct cgpu_info *proc;
  224. struct thr_info *core_thr;
  225. bool clean = false;
  226. if (likely(coreid < chip->core_count))
  227. {
  228. proc = first_proc;
  229. for (int j = 0; j < coreid; ++j)
  230. proc = proc->next_proc;
  231. core_thr = proc->thr[0];
  232. }
  233. else
  234. {
  235. proc = first_proc;
  236. core_thr = proc->thr[0];
  237. inc_hw_errors_only(core_thr);
  238. applog(LOG_ERR, "%"PRIpreprv": Core id out of range (%u >= %u)", proc->proc_repr, coreid, chip->core_count);
  239. }
  240. struct work *work;
  241. DL_SEARCH_SCALAR(chip_thr->work_list, work, device_id, taskid);
  242. if (unlikely(!work))
  243. {
  244. inc_hw_errors_only(core_thr);
  245. applog(LOG_ERR, "%"PRIpreprv": Unknown task %"PRIwdi, proc->proc_repr, taskid);
  246. continue;
  247. }
  248. if (have_nonce)
  249. {
  250. const uint32_t nonce = upk_u32le(resbuf_i, 4);
  251. if (submit_nonce(core_thr, work, nonce))
  252. clean = (coreid < chip->core_count);
  253. }
  254. // Flag previous work(s) as done, and delete them when we are sure
  255. struct work *work_tmp;
  256. DL_FOREACH_SAFE(chip_thr->work_list, work, work_tmp)
  257. {
  258. if (work->device_id == taskid)
  259. break;
  260. if (work->tv_stamp.tv_sec)
  261. {
  262. --chip->queue_count;
  263. work->tv_stamp.tv_sec = 0;
  264. }
  265. if (clean)
  266. {
  267. DL_DELETE(chip_thr->work_list, work);
  268. free_work(work);
  269. }
  270. }
  271. }
  272. minion_queue_full(chip);
  273. }
  274. timer_set_delay_from_now(&chip_thr->tv_poll, minion_poll_us);
  275. }
  276. BFG_REGISTER_DRIVER(minion_drv)
  277. static
  278. bool minion_detect_one(const char * const devpath)
  279. {
  280. spi_init();
  281. struct spi_port *spi = malloc(sizeof(*spi));
  282. // Be careful, read lowl-spi.h comments for warnings
  283. memset(spi, 0, sizeof(*spi));
  284. spi->speed = 50000000;
  285. spi->mode = SPI_MODE_0;
  286. spi->bits = 8;
  287. spi->txrx = linux_spi_txrx2;
  288. if (spi_open(spi, devpath) < 0)
  289. {
  290. free(spi);
  291. applogr(false, LOG_ERR, "%s: Failed to open %s", minion_drv.dname, devpath);
  292. }
  293. spi->repr = minion_drv.dname;
  294. spi->logprio = LOG_WARNING;
  295. const unsigned total_core_count = minion_count_cores(spi);
  296. struct minion_bus * const mbus = malloc(sizeof(*mbus));
  297. *mbus = (struct minion_bus){
  298. .spi = spi,
  299. };
  300. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  301. *cgpu = (struct cgpu_info){
  302. .drv = &minion_drv,
  303. .device_path = strdup(devpath),
  304. .device_data = mbus,
  305. .deven = DEV_ENABLED,
  306. .procs = total_core_count,
  307. .threads = 1,
  308. };
  309. return add_cgpu(cgpu);
  310. }
  311. static
  312. int minion_detect_auto(void)
  313. {
  314. return minion_detect_one("/dev/spidev0.0") ? 1 : 0;
  315. }
  316. static
  317. void minion_detect(void)
  318. {
  319. generic_detect(&minion_drv, minion_detect_one, minion_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  320. }
  321. struct device_drv minion_drv = {
  322. .dname = "minion",
  323. .name = "MNN",
  324. .drv_detect = minion_detect,
  325. .thread_init = minion_init,
  326. .minerloop = minerloop_queue,
  327. .queue_append = minion_queue_append,
  328. .queue_flush = minion_queue_flush,
  329. .poll = minion_poll,
  330. };