driver-minion.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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_CORE_EN_ = 0x10,
  33. MRA_RESULT = 0x20,
  34. MRA_TASK = 0x30,
  35. MRA_NONCE_START = 0x70,
  36. MRA_NONCE_INC = 0x71,
  37. };
  38. struct minion_chip {
  39. uint8_t chipid;
  40. uint8_t core_count;
  41. uint8_t core_enabled_count;
  42. uint16_t next_taskid;
  43. struct cgpu_info *first_proc;
  44. unsigned queue_count;
  45. uint32_t core_nonce_inc;
  46. };
  47. struct minion_bus {
  48. struct spi_port *spi;
  49. };
  50. static
  51. void minion_get(struct spi_port * const spi, const uint8_t chipid, const uint8_t addr, void * const buf, const size_t bufsz)
  52. {
  53. const uint8_t header[] = {chipid, addr | 0x80, bufsz & 0xff, bufsz >> 8};
  54. spi_clear_buf(spi);
  55. spi_emit_buf(spi, header, sizeof(header));
  56. uint8_t dummy[bufsz];
  57. memset(dummy, 0xff, bufsz);
  58. spi_emit_buf(spi, dummy, bufsz);
  59. spi_txrx(spi);
  60. uint8_t * const rdbuf = spi_getrxbuf(spi);
  61. memcpy(buf, &rdbuf[sizeof(header)], bufsz);
  62. }
  63. static
  64. void minion_set(struct spi_port * const spi, const uint8_t chipid, const uint8_t addr, const void * const buf, const size_t bufsz)
  65. {
  66. const uint8_t header[] = {chipid, addr, bufsz & 0xff, bufsz >> 8};
  67. spi_clear_buf(spi);
  68. spi_emit_buf(spi, header, sizeof(header));
  69. spi_emit_buf(spi, buf, bufsz);
  70. spi_txrx(spi);
  71. }
  72. static
  73. unsigned minion_count_cores(struct spi_port * const spi)
  74. {
  75. uint8_t buf[max(4, sizeof(minion_chip_signature))];
  76. unsigned total_core_count = 0;
  77. for (unsigned chipid = 0; chipid <= minion_max_chipid; ++chipid)
  78. {
  79. minion_get(spi, chipid, MRA_SIGNATURE, buf, sizeof(minion_chip_signature));
  80. if (memcmp(buf, minion_chip_signature, sizeof(minion_chip_signature)))
  81. {
  82. for (unsigned i = 0; i < sizeof(minion_chip_signature); ++i)
  83. {
  84. if (buf[i] != 0xff)
  85. {
  86. char hex[(sizeof(minion_chip_signature) * 2) + 1];
  87. bin2hex(hex, buf, sizeof(minion_chip_signature));
  88. applog(LOG_DEBUG, "%s: chipid %u: Bad signature (%s)", spi->repr, chipid, hex);
  89. break;
  90. }
  91. }
  92. continue;
  93. }
  94. minion_get(spi, chipid, MRA_STATUS, buf, 4);
  95. const uint8_t core_count = buf[2];
  96. applog(LOG_DEBUG, "%s: chipid %u: Found %u cores", spi->repr, chipid, core_count);
  97. total_core_count += core_count;
  98. }
  99. return total_core_count;
  100. }
  101. static inline
  102. void minion_core_enable_register_position(const uint8_t coreid, uint8_t * const corereg, uint8_t * const corebyte, uint8_t * const corebit)
  103. {
  104. *corereg = MRA_CORE_EN_ + (coreid >> 5);
  105. *corebyte = (coreid >> 3) % 4;
  106. *corebit = 1 << (coreid % 8);
  107. }
  108. static
  109. bool minion_init(struct thr_info * const thr)
  110. {
  111. struct cgpu_info * const dev = thr->cgpu, *proc = dev;
  112. struct minion_bus * const mbus = dev->device_data;
  113. struct spi_port * const spi = mbus->spi;
  114. uint8_t buf[max(4, sizeof(minion_chip_signature))];
  115. struct minion_chip * const chips = malloc(sizeof(*chips) * ((size_t)minion_max_chipid + 1));
  116. for (unsigned chipid = 0; proc; ++chipid)
  117. {
  118. struct minion_chip * const chip = &chips[chipid];
  119. spi->repr = proc->proc_repr;
  120. minion_get(spi, chipid, MRA_SIGNATURE, buf, sizeof(minion_chip_signature));
  121. if (memcmp(buf, minion_chip_signature, sizeof(minion_chip_signature)))
  122. continue;
  123. minion_get(spi, chipid, MRA_STATUS, buf, 4);
  124. if (!buf[2])
  125. continue;
  126. *chip = (struct minion_chip){
  127. .chipid = chipid,
  128. .core_count = buf[2],
  129. .first_proc = proc,
  130. };
  131. minion_set(spi, chipid, MRA_NONCE_START, "\0\0\0\0", 4);
  132. chip->core_nonce_inc = 0xffffffff / chip->core_count;
  133. pk_u32le(buf, 0, chip->core_nonce_inc);
  134. minion_set(spi, chipid, MRA_NONCE_INC, buf, 4);
  135. minion_get(spi, chipid, MRA_MISC_CTL, buf, 4);
  136. buf[0] |= 1 << 2; // Enable "no nonce" result reports
  137. minion_set(spi, chipid, MRA_MISC_CTL, buf, 4);
  138. timer_set_delay_from_now(&proc->thr[0]->tv_poll, minion_poll_us);
  139. for (unsigned coreid = 0; coreid < chip->core_count; ++coreid)
  140. {
  141. struct thr_info * const thr = proc->thr[0];
  142. uint8_t corereg, corebyte, corebit;
  143. minion_core_enable_register_position(coreid, &corereg, &corebyte, &corebit);
  144. if (coreid % 0x20 == 0)
  145. {
  146. spi->repr = proc->proc_repr;
  147. minion_get(spi, chipid, corereg, buf, 4);
  148. }
  149. if (buf[corebyte] & corebit)
  150. ++chip->core_enabled_count;
  151. else
  152. proc->deven = DEV_DISABLED;
  153. thr->cgpu_data = chip;
  154. proc = proc->next_proc;
  155. }
  156. }
  157. return true;
  158. }
  159. static
  160. bool minion_queue_full(struct minion_chip * const chip)
  161. {
  162. struct cgpu_info *proc = chip->first_proc;
  163. struct thr_info *thr = proc->thr[0];
  164. const bool full = (chip->queue_count >= minion_max_queued);
  165. if (full != thr->queue_full)
  166. {
  167. for (unsigned i = 0; i < chip->core_count; (proc = proc->next_proc), ++i)
  168. {
  169. thr = proc->thr[0];
  170. thr->queue_full = full;
  171. }
  172. }
  173. return full;
  174. }
  175. static
  176. void minion_core_enabledisable(struct thr_info * const thr, const bool enable)
  177. {
  178. struct cgpu_info * const proc = thr->cgpu;
  179. struct minion_bus * const mbus = proc->device_data;
  180. struct minion_chip * const chip = thr->cgpu_data;
  181. struct spi_port * const spi = mbus->spi;
  182. const uint8_t chipid = chip->chipid;
  183. uint8_t coreid = 0;
  184. for (struct cgpu_info *p = chip->first_proc; p != proc; p = p->next_proc)
  185. ++coreid;
  186. uint8_t corereg, corebyte, corebit;
  187. minion_core_enable_register_position(coreid, &corereg, &corebyte, &corebit);
  188. uint8_t buf[4];
  189. minion_get(spi, chipid, corereg, buf, 4);
  190. const uint8_t oldbyte = buf[corebyte];
  191. if (enable)
  192. buf[corebyte] |= corebit;
  193. else
  194. buf[corebyte] &= ~corebit;
  195. if (buf[corebyte] != oldbyte)
  196. {
  197. minion_set(spi, chipid, corereg, buf, 4);
  198. chip->core_enabled_count += enable ? 1 : -1;
  199. }
  200. }
  201. static
  202. void minion_core_disable(struct thr_info * const thr)
  203. {
  204. minion_core_enabledisable(thr, false);
  205. }
  206. static
  207. void minion_core_enable(struct thr_info * const thr)
  208. {
  209. minion_core_enabledisable(thr, true);
  210. }
  211. static
  212. bool minion_queue_append(struct thr_info *thr, struct work * const work)
  213. {
  214. struct cgpu_info *proc = thr->cgpu;
  215. struct minion_bus * const mbus = proc->device_data;
  216. struct minion_chip * const chip = thr->cgpu_data;
  217. proc = chip->first_proc;
  218. thr = proc->thr[0];
  219. if (minion_queue_full(chip))
  220. return false;
  221. struct spi_port * const spi = mbus->spi;
  222. const uint8_t chipid = chip->chipid;
  223. uint8_t taskdata[0x30];
  224. spi->repr = proc->proc_repr;
  225. work->device_id = ++chip->next_taskid;
  226. work->tv_stamp.tv_sec = 1;
  227. work->blk.nonce = 0;
  228. pk_u16be(taskdata, 0, work->device_id);
  229. memset(&taskdata[2], 0, 2);
  230. memcpy(&taskdata[4], work->midstate, 0x20);
  231. memcpy(&taskdata[0x24], &work->data[0x40], 0xc);
  232. minion_set(spi, chipid, MRA_TASK, taskdata, sizeof(taskdata));
  233. DL_APPEND(thr->work_list, work);
  234. ++chip->queue_count;
  235. minion_queue_full(chip);
  236. return true;
  237. }
  238. static
  239. void minion_queue_flush(struct thr_info * const thr)
  240. {
  241. struct cgpu_info * const proc = thr->cgpu;
  242. struct minion_bus * const mbus = proc->device_data;
  243. struct minion_chip * const chip = thr->cgpu_data;
  244. if (proc != chip->first_proc)
  245. // Redundant, all queues flush at the same time
  246. return;
  247. const uint8_t chipid = chip->chipid;
  248. struct spi_port * const spi = mbus->spi;
  249. static const uint8_t flushcmd[4] = {0xfb, 0xff, 0xff, 0xff};
  250. minion_set(spi, chipid, MRA_RESET, flushcmd, sizeof(flushcmd));
  251. struct work *work;
  252. DL_FOREACH(thr->work_list, work)
  253. {
  254. work->tv_stamp.tv_sec = 0;
  255. }
  256. chip->queue_count = 0;
  257. minion_queue_full(chip);
  258. }
  259. static
  260. void minion_hashes_done(struct cgpu_info *proc, const uint8_t core_count, const uint64_t hashes)
  261. {
  262. for (int j = 0; j < core_count; (proc = proc->next_proc), ++j)
  263. {
  264. if (proc->deven != DEV_ENABLED)
  265. continue;
  266. struct thr_info * const thr = proc->thr[0];
  267. hashes_done2(thr, hashes, NULL);
  268. }
  269. }
  270. static
  271. void minion_poll(struct thr_info * const chip_thr)
  272. {
  273. struct cgpu_info * const first_proc = chip_thr->cgpu;
  274. struct minion_bus * const mbus = first_proc->device_data;
  275. struct minion_chip * const chip = chip_thr->cgpu_data;
  276. struct spi_port * const spi = mbus->spi;
  277. const uint8_t chipid = chip->chipid;
  278. spi->repr = first_proc->proc_repr;
  279. uint8_t buf[4];
  280. minion_get(spi, chipid, MRA_FIFO_STATUS, buf, 4);
  281. const uint8_t res_fifo_len = buf[0];
  282. if (res_fifo_len)
  283. {
  284. static const size_t resbuf_i_len = 8;
  285. const size_t resbuf_len = (size_t)res_fifo_len * resbuf_i_len;
  286. uint8_t resbuf[resbuf_len], *resbuf_i = resbuf;
  287. minion_get(spi, chipid, MRA_RESULT, resbuf, resbuf_len);
  288. for (unsigned i = 0; i < res_fifo_len; (resbuf_i += resbuf_i_len), ++i)
  289. {
  290. const uint8_t coreid = resbuf_i[2];
  291. work_device_id_t taskid = upk_u16be(resbuf_i, 0);
  292. const bool have_nonce = !(resbuf_i[3] & 0x80);
  293. struct cgpu_info *proc;
  294. struct thr_info *core_thr;
  295. bool clean = false;
  296. if (likely(coreid < chip->core_count))
  297. {
  298. proc = first_proc;
  299. for (int j = 0; j < coreid; ++j)
  300. proc = proc->next_proc;
  301. core_thr = proc->thr[0];
  302. }
  303. else
  304. {
  305. proc = first_proc;
  306. core_thr = proc->thr[0];
  307. inc_hw_errors_only(core_thr);
  308. applog(LOG_ERR, "%"PRIpreprv": Core id out of range (%u >= %u)", proc->proc_repr, coreid, chip->core_count);
  309. }
  310. struct work *work;
  311. DL_SEARCH_SCALAR(chip_thr->work_list, work, device_id, taskid);
  312. if (unlikely(!work))
  313. {
  314. inc_hw_errors_only(core_thr);
  315. applog(LOG_ERR, "%"PRIpreprv": Unknown task %"PRIwdi, proc->proc_repr, taskid);
  316. continue;
  317. }
  318. if (have_nonce)
  319. {
  320. const uint32_t nonce = upk_u32le(resbuf_i, 4);
  321. if (submit_nonce(core_thr, work, nonce))
  322. {
  323. clean = (coreid < chip->core_count);
  324. // It's only 0xffffffff if we prematurely considered it complete
  325. if (likely(work->blk.nonce != 0xffffffff))
  326. {
  327. uint32_t hashes = (nonce % chip->core_nonce_inc);
  328. if (hashes > work->blk.nonce)
  329. {
  330. hashes -= work->blk.nonce - 1;
  331. minion_hashes_done(first_proc, chip->core_count, hashes);
  332. work->blk.nonce = hashes + 1;
  333. }
  334. }
  335. }
  336. }
  337. else
  338. {
  339. const uint32_t hashes = chip->core_nonce_inc - work->blk.nonce;
  340. minion_hashes_done(first_proc, chip->core_count, hashes);
  341. work->blk.nonce = 0xffffffff;
  342. }
  343. // Flag previous work(s) as done, and delete them when we are sure
  344. struct work *work_tmp;
  345. DL_FOREACH_SAFE(chip_thr->work_list, work, work_tmp)
  346. {
  347. if (work->device_id == taskid)
  348. break;
  349. if (work->blk.nonce && work->blk.nonce != 0xffffffff)
  350. {
  351. // At least one nonce was found, assume the job completed
  352. const uint32_t hashes = chip->core_nonce_inc - work->blk.nonce;
  353. minion_hashes_done(first_proc, chip->core_count, hashes);
  354. work->blk.nonce = 0xffffffff;
  355. }
  356. if (work->tv_stamp.tv_sec)
  357. {
  358. --chip->queue_count;
  359. work->tv_stamp.tv_sec = 0;
  360. }
  361. if (clean)
  362. {
  363. DL_DELETE(chip_thr->work_list, work);
  364. free_work(work);
  365. }
  366. }
  367. }
  368. minion_queue_full(chip);
  369. }
  370. timer_set_delay_from_now(&chip_thr->tv_poll, minion_poll_us);
  371. }
  372. BFG_REGISTER_DRIVER(minion_drv)
  373. static
  374. bool minion_detect_one(const char * const devpath)
  375. {
  376. spi_init();
  377. struct spi_port *spi = malloc(sizeof(*spi));
  378. // Be careful, read lowl-spi.h comments for warnings
  379. memset(spi, 0, sizeof(*spi));
  380. spi->speed = 50000000;
  381. spi->mode = SPI_MODE_0;
  382. spi->bits = 8;
  383. spi->txrx = linux_spi_txrx2;
  384. if (spi_open(spi, devpath) < 0)
  385. {
  386. free(spi);
  387. applogr(false, LOG_ERR, "%s: Failed to open %s", minion_drv.dname, devpath);
  388. }
  389. spi->repr = minion_drv.dname;
  390. spi->logprio = LOG_WARNING;
  391. const unsigned total_core_count = minion_count_cores(spi);
  392. struct minion_bus * const mbus = malloc(sizeof(*mbus));
  393. *mbus = (struct minion_bus){
  394. .spi = spi,
  395. };
  396. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  397. *cgpu = (struct cgpu_info){
  398. .drv = &minion_drv,
  399. .device_path = strdup(devpath),
  400. .device_data = mbus,
  401. .deven = DEV_ENABLED,
  402. .procs = total_core_count,
  403. .threads = 1,
  404. };
  405. return add_cgpu(cgpu);
  406. }
  407. static
  408. int minion_detect_auto(void)
  409. {
  410. return minion_detect_one("/dev/spidev0.0") ? 1 : 0;
  411. }
  412. static
  413. void minion_detect(void)
  414. {
  415. generic_detect(&minion_drv, minion_detect_one, minion_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  416. }
  417. struct device_drv minion_drv = {
  418. .dname = "minion",
  419. .name = "MNN",
  420. .drv_detect = minion_detect,
  421. .thread_init = minion_init,
  422. .minerloop = minerloop_queue,
  423. .thread_disable = minion_core_disable,
  424. .thread_enable = minion_core_enable,
  425. .queue_append = minion_queue_append,
  426. .queue_flush = minion_queue_flush,
  427. .poll = minion_poll,
  428. };