driver-kncasic.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*
  2. * Copyright 2014 KnCminer
  3. * Copyright 2014 Luke Dashjr
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include <stdlib.h>
  11. #include <assert.h>
  12. #include <fcntl.h>
  13. #include <limits.h>
  14. #include <unistd.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/time.h>
  17. #include <linux/types.h>
  18. #include <linux/spi/spidev.h>
  19. #include <zlib.h>
  20. #include "deviceapi.h"
  21. #include "logging.h"
  22. #include "miner.h"
  23. #include "knc-asic/knc-transport.h"
  24. #include "knc-asic/knc-asic.h"
  25. #define WORKS_PER_CORE 3
  26. #define CORE_ERROR_LIMIT 30
  27. #define CORE_ERROR_INTERVAL 30
  28. #define CORE_ERROR_DISABLE_TIME 5*60
  29. #define CORE_SUBMIT_MIN_TIME 2
  30. #define CORE_TIMEOUT 20
  31. #define SCAN_ADJUST_RANGE 32
  32. BFG_REGISTER_DRIVER(kncasic_drv)
  33. #define KNC_FREE_WORK(WORK) do { \
  34. free_work(WORK); \
  35. WORK = NULL; \
  36. } while (0)
  37. static struct timeval now;
  38. static const struct timeval core_check_interval = {
  39. CORE_ERROR_INTERVAL, 0
  40. };
  41. static const struct timeval core_disable_interval = {
  42. CORE_ERROR_DISABLE_TIME, 0
  43. };
  44. static const struct timeval core_submit_interval = {
  45. CORE_SUBMIT_MIN_TIME, 0
  46. };
  47. static const struct timeval core_timeout_interval = {
  48. CORE_TIMEOUT, 0
  49. };
  50. struct knc_die;
  51. struct knc_core_state {
  52. int generation;
  53. int core;
  54. struct knc_die *die;
  55. struct {
  56. int slot;
  57. struct work *work;
  58. } workslot[WORKS_PER_CORE]; /* active, next */
  59. int transfer_stamp;
  60. struct knc_report report;
  61. struct {
  62. int slot;
  63. uint32_t nonce;
  64. } last_nonce;
  65. uint32_t last_nonce_verified;
  66. uint32_t works;
  67. uint32_t shares;
  68. uint32_t errors;
  69. uint32_t completed;
  70. int last_slot;
  71. uint32_t errors_now;
  72. struct timeval disabled_until;
  73. struct timeval hold_work_until;
  74. struct timeval timeout;
  75. bool inuse;
  76. };
  77. struct knc_state;
  78. struct knc_die {
  79. int channel;
  80. int die;
  81. int version;
  82. int cores;
  83. struct cgpu_info *proc;
  84. struct knc_state *knc;
  85. struct knc_core_state *core;
  86. };
  87. #define MAX_SPI_SIZE (4096)
  88. #define MAX_SPI_RESPONSES (MAX_SPI_SIZE / (2 + 4 + 1 + 1 + 1 + 4))
  89. #define MAX_SPI_MESSAGE (128)
  90. #define KNC_SPI_BUFFERS (3)
  91. struct knc_state {
  92. void *ctx;
  93. int generation; /* work/block generation, incremented on each flush invalidating older works */
  94. int dies;
  95. struct knc_die die[KNC_MAX_ASICS * KNC_MAX_DIES_PER_ASIC];
  96. int cores;
  97. int scan_adjust;
  98. int startup;
  99. /* Statistics */
  100. uint64_t shares; /* diff1 shares reported by hardware */
  101. uint64_t works; /* Work units submitted */
  102. uint64_t completed; /* Work units completed */
  103. uint64_t errors; /* Hardware & communication errors */
  104. struct timeval next_error_interval;
  105. /* End of statistics */
  106. /* SPI communications thread */
  107. pthread_mutex_t spi_qlock; /* SPI queue status lock */
  108. struct thr_info spi_thr; /* SPI I/O thread */
  109. pthread_cond_t spi_qcond; /* SPI queue change wakeup */
  110. struct knc_spi_buffer {
  111. enum {
  112. KNC_SPI_IDLE=0,
  113. KNC_SPI_PENDING,
  114. KNC_SPI_DONE
  115. } state;
  116. int size;
  117. uint8_t txbuf[MAX_SPI_SIZE];
  118. uint8_t rxbuf[MAX_SPI_SIZE];
  119. int responses;
  120. struct knc_spi_response {
  121. int request_length;
  122. int response_length;
  123. enum {
  124. KNC_UNKNOWN = 0,
  125. KNC_NO_RESPONSE,
  126. KNC_SETWORK,
  127. KNC_REPORT,
  128. KNC_INFO
  129. } type;
  130. struct knc_core_state *core;
  131. uint32_t data;
  132. int offset;
  133. } response_info[MAX_SPI_RESPONSES];
  134. } spi_buffer[KNC_SPI_BUFFERS];
  135. int send_buffer;
  136. int read_buffer;
  137. int send_buffer_count;
  138. int read_buffer_count;
  139. /* end SPI thread */
  140. /* lock to protect resources between different threads */
  141. pthread_mutex_t state_lock;
  142. struct knc_core_state core[KNC_MAX_ASICS * KNC_MAX_DIES_PER_ASIC * KNC_MAX_CORES_PER_DIE];
  143. };
  144. int opt_knc_device_bus = -1;
  145. char *knc_log_file = NULL;
  146. static void *knc_spi(void *thr_data)
  147. {
  148. struct cgpu_info *cgpu = thr_data;
  149. struct knc_state *knc = cgpu->device_data;
  150. int buffer = 0;
  151. pthread_mutex_lock(&knc->spi_qlock);
  152. while (!cgpu->shutdown) {
  153. int this_buffer = buffer;
  154. while (knc->spi_buffer[buffer].state != KNC_SPI_PENDING && !cgpu->shutdown)
  155. pthread_cond_wait(&knc->spi_qcond, &knc->spi_qlock);
  156. pthread_mutex_unlock(&knc->spi_qlock);
  157. if (cgpu->shutdown)
  158. return NULL;
  159. knc_trnsp_transfer(knc->ctx, knc->spi_buffer[buffer].txbuf, knc->spi_buffer[buffer].rxbuf, knc->spi_buffer[buffer].size);
  160. buffer += 1;
  161. if (buffer >= KNC_SPI_BUFFERS)
  162. buffer = 0;
  163. pthread_mutex_lock(&knc->spi_qlock);
  164. knc->spi_buffer[this_buffer].state = KNC_SPI_DONE;
  165. pthread_cond_signal(&knc->spi_qcond);
  166. }
  167. pthread_mutex_unlock(&knc->spi_qlock);
  168. return NULL;
  169. }
  170. static void knc_process_responses(struct thr_info *thr);
  171. static void knc_flush(struct thr_info *thr)
  172. {
  173. struct cgpu_info *cgpu = thr->cgpu;
  174. struct knc_state *knc = cgpu->device_data;
  175. struct knc_spi_buffer *buffer = &knc->spi_buffer[knc->send_buffer];
  176. if (buffer->state == KNC_SPI_IDLE && buffer->size > 0) {
  177. pthread_mutex_lock(&knc->spi_qlock);
  178. buffer->state = KNC_SPI_PENDING;
  179. pthread_cond_signal(&knc->spi_qcond);
  180. knc->send_buffer += 1;
  181. knc->send_buffer_count += 1;
  182. if (knc->send_buffer >= KNC_SPI_BUFFERS)
  183. knc->send_buffer = 0;
  184. buffer = &knc->spi_buffer[knc->send_buffer];
  185. /* Block for SPI to finish a transfer if all buffers are busy */
  186. while (buffer->state == KNC_SPI_PENDING) {
  187. applog(LOG_DEBUG, "KnC: SPI buffer full (%d), waiting for SPI thread", buffer->responses);
  188. pthread_cond_wait(&knc->spi_qcond, &knc->spi_qlock);
  189. }
  190. pthread_mutex_unlock(&knc->spi_qlock);
  191. }
  192. knc_process_responses(thr);
  193. }
  194. static void knc_transfer(struct thr_info *thr, struct knc_core_state *core, int request_length, uint8_t *request, int response_length, int response_type, uint32_t data)
  195. {
  196. struct cgpu_info *cgpu = thr->cgpu;
  197. struct knc_state *knc = cgpu->device_data;
  198. struct knc_spi_buffer *buffer = &knc->spi_buffer[knc->send_buffer];
  199. /* FPGA control, request header, request body/response, CRC(4), ACK(1), EXTRA(3) */
  200. int msglen = 2 + max(request_length, 4 + response_length) + 4 + 1 + 3;
  201. if (buffer->size + msglen > MAX_SPI_SIZE || buffer->responses >= MAX_SPI_RESPONSES) {
  202. applog(LOG_INFO, "KnC: SPI buffer sent, %d messages %d bytes", buffer->responses, buffer->size);
  203. knc_flush(thr);
  204. buffer = &knc->spi_buffer[knc->send_buffer];
  205. }
  206. struct knc_spi_response *response_info = &buffer->response_info[buffer->responses];
  207. buffer->responses++;
  208. response_info->offset = buffer->size;
  209. response_info->type = response_type;
  210. response_info->request_length = request_length;
  211. response_info->response_length = response_length;
  212. response_info->core = core;
  213. response_info->data = data;
  214. buffer->size = knc_prepare_transfer(buffer->txbuf, buffer->size, MAX_SPI_SIZE, core->die->channel, request_length, request, response_length);
  215. }
  216. static int knc_transfer_stamp(struct knc_state *knc)
  217. {
  218. return knc->send_buffer_count;
  219. }
  220. static int knc_transfer_completed(struct knc_state *knc, int stamp)
  221. {
  222. /* signed delta math, counter wrap OK */
  223. return (int)(knc->read_buffer_count - stamp) >= 1;
  224. }
  225. static struct cgpu_info * all_cgpus[KNC_MAX_ASICS][KNC_MAX_DIES_PER_ASIC] = {{NULL}};
  226. /* Note: content of knc might be not initialized yet */
  227. static bool prealloc_all_cgpus(struct knc_state *knc)
  228. {
  229. int channel, die;
  230. struct cgpu_info *cgpu, *prev_cgpu;
  231. prev_cgpu = NULL;
  232. for (channel = 0; channel < KNC_MAX_ASICS; ++channel) {
  233. cgpu = all_cgpus[channel][0];
  234. if (NULL != cgpu)
  235. continue;
  236. cgpu = malloc(sizeof(*cgpu));
  237. if (NULL == cgpu)
  238. return false;
  239. *cgpu = (struct cgpu_info){
  240. .drv = &kncasic_drv,
  241. .name = "KnCminer",
  242. .procs = KNC_MAX_DIES_PER_ASIC,
  243. .threads = prev_cgpu ? 0 : 1,
  244. .device_data = knc,
  245. };
  246. if (!add_cgpu_slave(cgpu, prev_cgpu)) {
  247. free(cgpu);
  248. return false;
  249. }
  250. prev_cgpu = cgpu;
  251. die = 0;
  252. for_each_managed_proc(proc, cgpu) {
  253. proc->deven = DEV_DISABLED;
  254. all_cgpus[channel][die++] = proc;
  255. }
  256. }
  257. return true;
  258. }
  259. static struct cgpu_info * get_cgpu(int channel, int die)
  260. {
  261. if ((channel < 0) || (channel >= KNC_MAX_ASICS) || (die < 0) || (die >= KNC_MAX_DIES_PER_ASIC))
  262. return NULL;
  263. return all_cgpus[channel][die];
  264. }
  265. int knc_change_die_state(void* device_data, int asic_id, int die_id, bool enable)
  266. {
  267. int ret = 0;
  268. struct knc_state *knc = device_data;
  269. struct knc_die_info die_info = {};
  270. int die, next_die, core;
  271. applog(LOG_NOTICE, "KnC: %s die, ASIC id=%d, DIE id=%d", enable ? "enable" : "disable", asic_id, die_id);
  272. mutex_lock(&knc->state_lock);
  273. if (asic_id < 0 || asic_id >= KNC_MAX_ASICS || die_id < 0 || die_id >= KNC_MAX_DIES_PER_ASIC) {
  274. ret = EINVAL;
  275. goto out_unlock;
  276. }
  277. struct cgpu_info *proc = get_cgpu(asic_id, die_id);
  278. for (die = 0; die < knc->dies; ++die) {
  279. if (knc->die[die].channel != asic_id || knc->die[die].die != die_id)
  280. continue;
  281. if (!enable) {
  282. int slot, buffer, resp;
  283. int deleted_cores = knc->die[die].cores;
  284. knc->cores -= deleted_cores;
  285. --knc->dies;
  286. /* cgpu[0][0] must be always enabled */
  287. if ((asic_id != 0) || (die_id != 0))
  288. proc->deven = DEV_DISABLED;
  289. struct knc_core_state *pcore_to = knc->die[die].core;
  290. struct knc_core_state *pcore_from = pcore_to + knc->die[die].cores;
  291. struct knc_core_state *pcore;
  292. for (pcore = pcore_to; pcore < pcore_from; ++pcore) {
  293. for (slot = 0; slot < WORKS_PER_CORE; ++slot) {
  294. if (pcore->workslot[slot].work)
  295. KNC_FREE_WORK(pcore->workslot[slot].work);
  296. }
  297. }
  298. int core_move_count = &(knc->core[knc->cores]) - pcore_to;
  299. assert(core_move_count >= 0);
  300. memmove(pcore_to, pcore_from, core_move_count * sizeof(struct knc_core_state));
  301. struct knc_die *pdie_to = &(knc->die[die]);
  302. struct knc_die *pdie_from = pdie_to + 1;
  303. int die_move_count = knc->dies - die;
  304. assert(die_move_count >= 0);
  305. memmove(pdie_to, pdie_from, die_move_count * sizeof(struct knc_die));
  306. /* Now fix pointers */
  307. for (next_die = 0; next_die < knc->dies; ++next_die) {
  308. assert(knc->die[next_die].core != pcore_to);
  309. if (knc->die[next_die].core > pcore_to)
  310. knc->die[next_die].core -= deleted_cores;
  311. }
  312. for (core = 0; core < knc->cores; ++core) {
  313. assert(knc->core[core].die != pdie_to);
  314. if (knc->core[core].die > pdie_to)
  315. --(knc->core[core].die);
  316. }
  317. for (buffer = 0; buffer < KNC_SPI_BUFFERS; ++buffer) {
  318. for (resp = 0; resp < MAX_SPI_RESPONSES; ++resp) {
  319. if (knc->spi_buffer[buffer].response_info[resp].core < pcore_to)
  320. continue;
  321. if (knc->spi_buffer[buffer].response_info[resp].core < pcore_from) {
  322. knc->spi_buffer[buffer].response_info[resp].core = NULL;
  323. continue;
  324. }
  325. knc->spi_buffer[buffer].response_info[resp].core -= deleted_cores;
  326. }
  327. }
  328. }
  329. /* die was found */
  330. ret = 0;
  331. goto out_unlock;
  332. }
  333. /* die was not found */
  334. if (enable) {
  335. /* Send GETINFO to a die to detect if it is usable */
  336. if (knc_trnsp_asic_detect(knc->ctx, asic_id)) {
  337. if (knc_detect_die(knc->ctx, asic_id, die_id, &die_info) != 0) {
  338. ret = ENODEV;
  339. goto out_unlock;
  340. }
  341. } else {
  342. ret = ENODEV;
  343. goto out_unlock;
  344. }
  345. memset(&(knc->core[knc->cores]), 0, die_info.cores * sizeof(struct knc_core_state));
  346. int next_die = knc->dies;
  347. knc->die[next_die].channel = asic_id;
  348. knc->die[next_die].die = die_id;
  349. knc->die[next_die].version = die_info.version;
  350. knc->die[next_die].cores = die_info.cores;
  351. knc->die[next_die].core = &(knc->core[knc->cores]);
  352. knc->die[next_die].knc = knc;
  353. knc->die[next_die].proc = proc;
  354. for (core = 0; core < knc->die[next_die].cores; ++core) {
  355. knc->die[next_die].core[core].die = &knc->die[next_die];
  356. knc->die[next_die].core[core].core = core;
  357. }
  358. ++knc->dies;
  359. knc->cores += die_info.cores;
  360. proc_enable(proc);
  361. }
  362. out_unlock:
  363. mutex_unlock(&knc->state_lock);
  364. return ret;
  365. }
  366. static bool knc_detect_one(void *ctx)
  367. {
  368. /* Scan device for ASICs */
  369. int channel, die, cores = 0, core;
  370. struct knc_state *knc;
  371. struct knc_die_info die_info[KNC_MAX_ASICS][KNC_MAX_DIES_PER_ASIC];
  372. memset(die_info, 0, sizeof(die_info));
  373. /* Send GETINFO to each die to detect if it is usable */
  374. for (channel = 0; channel < KNC_MAX_ASICS; channel++) {
  375. if (!knc_trnsp_asic_detect(ctx, channel))
  376. continue;
  377. for (die = 0; die < KNC_MAX_DIES_PER_ASIC; die++) {
  378. if (knc_detect_die(ctx, channel, die, &die_info[channel][die]) == 0)
  379. cores += die_info[channel][die].cores;
  380. }
  381. }
  382. if (!cores) {
  383. applog(LOG_ERR, "no KnCminer cores found");
  384. return false;
  385. }
  386. applog(LOG_NOTICE, "Found a KnC miner with %d cores", cores);
  387. knc = calloc(1, sizeof(*knc));
  388. if (!knc) {
  389. err_nomem:
  390. applog(LOG_ERR, "KnC miner detected, but failed to allocate memory");
  391. return false;
  392. }
  393. if (!prealloc_all_cgpus(knc)) {
  394. free(knc);
  395. goto err_nomem;
  396. }
  397. knc->ctx = ctx;
  398. knc->generation = 1;
  399. /* Index all cores */
  400. struct cgpu_info *first_cgpu = NULL;
  401. int dies = 0;
  402. cores = 0;
  403. struct knc_core_state *pcore = knc->core;
  404. for (channel = 0; channel < KNC_MAX_ASICS; channel++) {
  405. for (die = 0; die < KNC_MAX_DIES_PER_ASIC; die++) {
  406. if (die_info[channel][die].cores) {
  407. knc->die[dies].channel = channel;
  408. knc->die[dies].die = die;
  409. knc->die[dies].version = die_info[channel][die].version;
  410. knc->die[dies].cores = die_info[channel][die].cores;
  411. knc->die[dies].core = pcore;
  412. knc->die[dies].knc = knc;
  413. knc->die[dies].proc = get_cgpu(channel, die);
  414. knc->die[dies].proc->deven = DEV_ENABLED;
  415. if (NULL == first_cgpu)
  416. first_cgpu = knc->die[dies].proc;
  417. for (core = 0; core < knc->die[dies].cores; core++) {
  418. knc->die[dies].core[core].die = &knc->die[dies];
  419. knc->die[dies].core[core].core = core;
  420. }
  421. cores += knc->die[dies].cores;
  422. pcore += knc->die[dies].cores;
  423. dies++;
  424. }
  425. }
  426. }
  427. knc->dies = dies;
  428. knc->cores = cores;
  429. knc->startup = 2;
  430. pthread_mutex_init(&knc->spi_qlock, NULL);
  431. pthread_cond_init(&knc->spi_qcond, NULL);
  432. pthread_mutex_init(&knc->state_lock, NULL);
  433. if (thr_info_create(&knc->spi_thr, NULL, knc_spi, first_cgpu)) {
  434. applog(LOG_ERR, "%s: SPI thread create failed", first_cgpu->dev_repr);
  435. free(knc);
  436. /* TODO: free all cgpus. We can not do it at the moment as there is no good
  437. * way to free all cgpu-related resources. */
  438. return false;
  439. }
  440. return true;
  441. }
  442. /* Probe devices and register with add_cgpu */
  443. static
  444. bool kncasic_detect_one(const char * const devpath)
  445. {
  446. void *ctx = knc_trnsp_new(devpath);
  447. if (ctx != NULL) {
  448. if (!knc_detect_one(ctx))
  449. knc_trnsp_free(ctx);
  450. else
  451. return true;
  452. }
  453. return false;
  454. }
  455. static
  456. int kncasic_detect_auto(void)
  457. {
  458. return kncasic_detect_one(NULL) ? 1 : 0;
  459. }
  460. static
  461. void kncasic_detect(void)
  462. {
  463. generic_detect(&kncasic_drv, kncasic_detect_one, kncasic_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  464. }
  465. static
  466. bool knc_init(struct thr_info * const thr)
  467. {
  468. int channel, die;
  469. struct cgpu_info *cgpu = thr->cgpu, *proc;
  470. struct knc_state *knc = cgpu->device_data;
  471. /* Set initial enable/disable state */
  472. bool die_detected[KNC_MAX_ASICS][KNC_MAX_DIES_PER_ASIC];
  473. memset(die_detected, 0, sizeof(die_detected));
  474. for (die = 0; die < knc->dies; ++die) {
  475. if (0 < knc->die[die].cores) {
  476. die_detected[knc->die[die].channel][knc->die[die].die] = true;
  477. }
  478. }
  479. /* cgpu[0][0] must be always enabled */
  480. die_detected[0][0] = true;
  481. for (channel = 0; channel < KNC_MAX_ASICS; ++channel) {
  482. for (die = 0; die < KNC_MAX_DIES_PER_ASIC; ++die) {
  483. proc = get_cgpu(channel, die);
  484. if (NULL != proc) {
  485. proc->deven = die_detected[channel][die] ? DEV_ENABLED : DEV_DISABLED;
  486. }
  487. }
  488. }
  489. return true;
  490. }
  491. /* Core helper functions */
  492. static int knc_core_hold_work(struct knc_core_state *core)
  493. {
  494. return timercmp(&core->hold_work_until, &now, >);
  495. }
  496. static int knc_core_has_work(struct knc_core_state *core)
  497. {
  498. int i;
  499. for (i = 0; i < WORKS_PER_CORE; i++) {
  500. if (core->workslot[i].slot > 0)
  501. return true;
  502. }
  503. return false;
  504. }
  505. static int knc_core_need_work(struct knc_core_state *core)
  506. {
  507. return !knc_core_hold_work(core) && !core->workslot[1].work && !core->workslot[2].work;
  508. }
  509. static int knc_core_disabled(struct knc_core_state *core)
  510. {
  511. return timercmp(&core->disabled_until, &now, >);
  512. }
  513. static int _knc_core_next_slot(struct knc_core_state *core)
  514. {
  515. /* Avoid slot #0 and #15. #0 is "no work assigned" and #15 is seen on bad cores */
  516. int slot = core->last_slot + 1;
  517. if (slot >= 15)
  518. slot = 1;
  519. core->last_slot = slot;
  520. return slot;
  521. }
  522. static bool knc_core_slot_busy(struct knc_core_state *core, int slot)
  523. {
  524. if (slot == core->report.active_slot)
  525. return true;
  526. if (slot == core->report.next_slot)
  527. return true;
  528. int i;
  529. for (i = 0; i < WORKS_PER_CORE; i++) {
  530. if (slot == core->workslot[i].slot)
  531. return true;
  532. }
  533. return false;
  534. }
  535. static int knc_core_next_slot(struct knc_core_state *core)
  536. {
  537. int slot;
  538. do slot = _knc_core_next_slot(core);
  539. while (knc_core_slot_busy(core, slot));
  540. return slot;
  541. }
  542. static void knc_core_failure(struct knc_core_state *core)
  543. {
  544. core->errors++;
  545. core->errors_now++;
  546. core->die->knc->errors++;
  547. if (knc_core_disabled(core))
  548. return;
  549. if (core->errors_now > CORE_ERROR_LIMIT) {
  550. struct cgpu_info * const proc = core->die->proc;
  551. applog(LOG_ERR, "%"PRIpreprv"[%d] disabled for %ld seconds due to repeated hardware errors",
  552. proc->proc_repr, core->core, (long)core_disable_interval.tv_sec);
  553. timeradd(&now, &core_disable_interval, &core->disabled_until);
  554. }
  555. }
  556. static
  557. void knc_core_handle_nonce(struct thr_info *thr, struct knc_core_state *core, int slot, uint32_t nonce, bool comm_errors)
  558. {
  559. int i;
  560. if (!slot)
  561. return;
  562. core->last_nonce.slot = slot;
  563. core->last_nonce.nonce = nonce;
  564. if (core->die->knc->startup)
  565. return;
  566. for (i = 0; i < WORKS_PER_CORE; i++) {
  567. if (slot == core->workslot[i].slot && core->workslot[i].work) {
  568. struct cgpu_info * const proc = core->die->proc;
  569. struct thr_info * const corethr = proc->thr[0];
  570. char *comm_err_str = comm_errors ? " (comm error)" : "";
  571. applog(LOG_INFO, "%"PRIpreprv"[%d] found nonce %08x%s", proc->proc_repr, core->core, nonce, comm_err_str);
  572. if (submit_nonce(corethr, core->workslot[i].work, nonce)) {
  573. if (nonce != core->last_nonce_verified) {
  574. /* Good share */
  575. core->shares++;
  576. core->die->knc->shares++;
  577. hashes_done2(corethr, 0x100000000, NULL);
  578. core->last_nonce_verified = nonce;
  579. } else {
  580. applog(LOG_INFO, "%"PRIpreprv"[%d] duplicate nonce %08x%s", proc->proc_repr, core->core, nonce, comm_err_str);
  581. }
  582. /* This core is useful. Ignore any errors */
  583. core->errors_now = 0;
  584. } else {
  585. applog(LOG_INFO, "%"PRIpreprv"[%d] hwerror nonce %08x%s", proc->proc_repr, core->core, nonce, comm_err_str);
  586. /* Bad share */
  587. knc_core_failure(core);
  588. }
  589. }
  590. }
  591. }
  592. static int knc_core_process_report(struct thr_info *thr, struct knc_core_state *core, uint8_t *response, bool comm_errors)
  593. {
  594. struct cgpu_info * const proc = core->die->proc;
  595. struct knc_report *report = &core->report;
  596. knc_decode_report(response, report, core->die->version);
  597. bool had_event = false;
  598. applog(LOG_DEBUG, "%"PRIpreprv"[%d]: Process report %d %d(%d) / %d %d %d", proc->proc_repr, core->core, report->active_slot, report->next_slot, report->next_state, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  599. int n;
  600. for (n = 0; n < KNC_NONCES_PER_REPORT; n++) {
  601. if (report->nonce[n].slot < 0)
  602. break;
  603. if (core->last_nonce.slot == report->nonce[n].slot && core->last_nonce.nonce == report->nonce[n].nonce)
  604. break;
  605. }
  606. while(n-- > 0) {
  607. knc_core_handle_nonce(thr, core, report->nonce[n].slot, report->nonce[n].nonce, comm_errors);
  608. }
  609. if (!comm_errors) {
  610. if (report->active_slot && core->workslot[0].slot != report->active_slot) {
  611. had_event = true;
  612. applog(LOG_INFO, "%"PRIpreprv"[%d]: New work %d %d / %d %d %d", proc->proc_repr, core->core, report->active_slot, report->next_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  613. /* Core switched to next work */
  614. if (core->workslot[0].work) {
  615. core->die->knc->completed++;
  616. core->completed++;
  617. applog(LOG_INFO, "%"PRIpreprv"[%d]: Work completed!", proc->proc_repr, core->core);
  618. KNC_FREE_WORK(core->workslot[0].work);
  619. }
  620. core->workslot[0] = core->workslot[1];
  621. core->workslot[1].work = NULL;
  622. core->workslot[1].slot = -1;
  623. /* or did it switch directly to pending work? */
  624. if (report->active_slot == core->workslot[2].slot) {
  625. applog(LOG_INFO, "%"PRIpreprv"[%d]: New work %d %d %d %d (pending)", proc->proc_repr, core->core, report->active_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  626. if (core->workslot[0].work)
  627. KNC_FREE_WORK(core->workslot[0].work);
  628. core->workslot[0] = core->workslot[2];
  629. core->workslot[2].work = NULL;
  630. core->workslot[2].slot = -1;
  631. }
  632. }
  633. if (report->next_state && core->workslot[2].slot > 0 && (core->workslot[2].slot == report->next_slot || report->next_slot == -1)) {
  634. had_event = true;
  635. applog(LOG_INFO, "%"PRIpreprv"[%d]: Accepted work %d %d %d %d (pending)", proc->proc_repr, core->core, report->active_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  636. /* core accepted next work */
  637. if (core->workslot[1].work)
  638. KNC_FREE_WORK(core->workslot[1].work);
  639. core->workslot[1] = core->workslot[2];
  640. core->workslot[2].work = NULL;
  641. core->workslot[2].slot = -1;
  642. }
  643. }
  644. if (core->workslot[2].work && knc_transfer_completed(core->die->knc, core->transfer_stamp)) {
  645. had_event = true;
  646. applog(LOG_INFO, "%"PRIpreprv"[%d]: Setwork failed?", proc->proc_repr, core->core);
  647. KNC_FREE_WORK(core->workslot[2].work);
  648. core->workslot[2].slot = -1;
  649. }
  650. if (had_event)
  651. applog(LOG_INFO, "%"PRIpreprv"[%d]: Exit report %d %d / %d %d %d", proc->proc_repr, core->core, report->active_slot, report->next_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  652. return 0;
  653. }
  654. static void knc_process_responses(struct thr_info *thr)
  655. {
  656. struct cgpu_info *cgpu = thr->cgpu;
  657. struct knc_state *knc = cgpu->device_data;
  658. struct knc_spi_buffer *buffer = &knc->spi_buffer[knc->read_buffer];
  659. while (buffer->state == KNC_SPI_DONE) {
  660. int i;
  661. for (i = 0; i < buffer->responses; i++) {
  662. struct knc_spi_response *response_info = &buffer->response_info[i];
  663. uint8_t *rxbuf = &buffer->rxbuf[response_info->offset];
  664. struct knc_core_state *core = response_info->core;
  665. if (NULL == core) /* core was deleted, e.g. by API call */
  666. continue;
  667. struct cgpu_info * const proc = core->die->proc;
  668. int status = knc_decode_response(rxbuf, response_info->request_length, &rxbuf, response_info->response_length);
  669. /* Invert KNC_ACCEPTED to simplify logics below */
  670. if (response_info->type == KNC_SETWORK && !KNC_IS_ERROR(status))
  671. status ^= KNC_ACCEPTED;
  672. bool comm_errors = false;
  673. if (core->die->version != KNC_VERSION_JUPITER && status != 0) {
  674. if (response_info->type == KNC_SETWORK && status == KNC_ACCEPTED) {
  675. /* Core refused our work vector. Likely out of sync. Reset it */
  676. core->inuse = false;
  677. } else {
  678. applog(LOG_INFO, "%"PRIpreprv"[%d]: Communication error (%x / %d)", proc->proc_repr, core->core, status, i);
  679. comm_errors = true;
  680. }
  681. knc_core_failure(core);
  682. }
  683. switch(response_info->type) {
  684. case KNC_REPORT:
  685. case KNC_SETWORK:
  686. /* Should we care about failed SETWORK explicit? Or simply handle it by next state not loaded indication in reports? */
  687. knc_core_process_report(thr, core, rxbuf, comm_errors);
  688. break;
  689. default:
  690. break;
  691. }
  692. }
  693. buffer->state = KNC_SPI_IDLE;
  694. buffer->responses = 0;
  695. buffer->size = 0;
  696. knc->read_buffer += 1;
  697. knc->read_buffer_count += 1;
  698. if (knc->read_buffer >= KNC_SPI_BUFFERS)
  699. knc->read_buffer = 0;
  700. buffer = &knc->spi_buffer[knc->read_buffer];
  701. }
  702. }
  703. static int knc_core_send_work(struct thr_info *thr, struct knc_core_state *core, struct work *work, bool clean)
  704. {
  705. struct knc_state *knc = core->die->knc;
  706. struct cgpu_info * const proc = core->die->proc;
  707. int request_length = 4 + 1 + 6*4 + 3*4 + 8*4;
  708. uint8_t request[request_length];
  709. int response_length = 1 + 1 + (1 + 4) * 5;
  710. int slot = knc_core_next_slot(core);
  711. if (slot < 0)
  712. goto error;
  713. applog(LOG_INFO, "%"PRIpreprv"[%d] setwork%s = %d, %d %d / %d %d %d", proc->proc_repr, core->core, clean ? " CLEAN" : "", slot, core->report.active_slot, core->report.next_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  714. if (!clean && !knc_core_need_work(core))
  715. goto error;
  716. switch(core->die->version) {
  717. case KNC_VERSION_JUPITER:
  718. if (clean) {
  719. /* Double halt to get rid of any previous queued work */
  720. request_length = knc_prepare_jupiter_halt(request, core->die->die, core->core);
  721. knc_transfer(thr, core, request_length, request, 0, KNC_NO_RESPONSE, 0);
  722. knc_transfer(thr, core, request_length, request, 0, KNC_NO_RESPONSE, 0);
  723. }
  724. request_length = knc_prepare_jupiter_setwork(request, core->die->die, core->core, slot, work);
  725. knc_transfer(thr, core, request_length, request, 0, KNC_NO_RESPONSE, 0);
  726. break;
  727. case KNC_VERSION_NEPTUNE:
  728. request_length = knc_prepare_neptune_setwork(request, core->die->die, core->core, slot, work, clean);
  729. knc_transfer(thr, core, request_length, request, response_length, KNC_SETWORK, slot);
  730. break;
  731. default:
  732. goto error;
  733. }
  734. if (core->workslot[2].work)
  735. KNC_FREE_WORK(core->workslot[2].work);
  736. core->workslot[2].work = work;
  737. core->workslot[2].slot = slot;
  738. core->works++;
  739. core->die->knc->works++;
  740. core->transfer_stamp = knc_transfer_stamp(knc);
  741. core->inuse = true;
  742. timeradd(&now, &core_submit_interval, &core->hold_work_until);
  743. timeradd(&now, &core_timeout_interval, &core->timeout);
  744. return 0;
  745. error:
  746. applog(LOG_INFO, "%"PRIpreprv"[%d]: Failed to setwork (%d)", proc->proc_repr, core->core, core->errors_now);
  747. knc_core_failure(core);
  748. KNC_FREE_WORK(work);
  749. return -1;
  750. }
  751. static int knc_core_request_report(struct thr_info *thr, struct knc_core_state *core)
  752. {
  753. struct cgpu_info * const proc = core->die->proc;
  754. int request_length = 4;
  755. uint8_t request[request_length];
  756. int response_length = 1 + 1 + (1 + 4) * 5;
  757. applog(LOG_DEBUG, "%"PRIpreprv"[%d]: Request report", proc->proc_repr, core->core);
  758. request_length = knc_prepare_report(request, core->die->die, core->core);
  759. switch(core->die->version) {
  760. case KNC_VERSION_JUPITER:
  761. response_length = 1 + 1 + (1 + 4);
  762. knc_transfer(thr, core, request_length, request, response_length, KNC_REPORT, 0);
  763. return 0;
  764. case KNC_VERSION_NEPTUNE:
  765. knc_transfer(thr, core, request_length, request, response_length, KNC_REPORT, 0);
  766. return 0;
  767. }
  768. applog(LOG_INFO, "%"PRIpreprv"[%d]: Failed to scan work report", proc->proc_repr, core->core);
  769. knc_core_failure(core);
  770. return -1;
  771. }
  772. /* return value is number of nonces that have been checked since
  773. * previous call
  774. */
  775. static int64_t knc_scanwork(struct thr_info *thr)
  776. {
  777. struct cgpu_info *cgpu = thr->cgpu;
  778. struct knc_state *knc = cgpu->device_data;
  779. applog(LOG_DEBUG, "KnC running scanwork");
  780. mutex_lock(&knc->state_lock);
  781. gettimeofday(&now, NULL);
  782. knc_trnsp_periodic_check(knc->ctx);
  783. int i;
  784. knc_process_responses(thr);
  785. if (timercmp(&knc->next_error_interval, &now, >)) {
  786. /* Reset hw error limiter every check interval */
  787. timeradd(&now, &core_check_interval, &knc->next_error_interval);
  788. for (i = 0; i < knc->cores; i++) {
  789. struct knc_core_state *core = &knc->core[i];
  790. core->errors_now = 0;
  791. }
  792. }
  793. for (i = 0; i < knc->cores; i++) {
  794. struct knc_core_state *core = &knc->core[i];
  795. struct cgpu_info * const proc = core->die->proc;
  796. bool clean = !core->inuse;
  797. if (knc_core_disabled(core))
  798. continue;
  799. if (core->generation != knc->generation) {
  800. applog(LOG_INFO, "%"PRIpreprv"[%d] flush gen=%d/%d", proc->proc_repr, core->core, core->generation, knc->generation);
  801. /* clean set state, forget everything */
  802. int slot;
  803. for (slot = 0; slot < WORKS_PER_CORE; slot ++) {
  804. if (core->workslot[slot].work)
  805. KNC_FREE_WORK(core->workslot[slot].work);
  806. core->workslot[slot].slot = -1;
  807. }
  808. core->hold_work_until = now;
  809. core->generation = knc->generation;
  810. } else if (timercmp(&core->timeout, &now, <=) && (core->workslot[0].slot > 0 || core->workslot[1].slot > 0 || core->workslot[2].slot > 0)) {
  811. applog(LOG_INFO, "%"PRIpreprv"[%d] timeout gen=%d/%d", proc->proc_repr, core->core, core->generation, knc->generation);
  812. clean = true;
  813. }
  814. if (!knc_core_has_work(core))
  815. clean = true;
  816. if (core->workslot[0].slot < 0 && core->workslot[1].slot < 0 && core->workslot[2].slot < 0)
  817. clean = true;
  818. if (i % SCAN_ADJUST_RANGE == knc->scan_adjust)
  819. clean = true;
  820. if ((knc_core_need_work(core) || clean) && !knc->startup) {
  821. struct work *work = get_work(thr);
  822. knc_core_send_work(thr, core, work, clean);
  823. } else {
  824. knc_core_request_report(thr, core);
  825. }
  826. }
  827. /* knc->startup delays initial work submission until we have had chance to query all cores on their current status, to avoid slot number collisions with earlier run */
  828. if (knc->startup)
  829. knc->startup--;
  830. else if (knc->scan_adjust < SCAN_ADJUST_RANGE)
  831. knc->scan_adjust++;
  832. knc_flush(thr);
  833. mutex_unlock(&knc->state_lock);
  834. return 0;
  835. }
  836. static void knc_flush_work(struct cgpu_info *cgpu)
  837. {
  838. struct knc_state *knc = cgpu->device_data;
  839. applog(LOG_INFO, "KnC running flushwork");
  840. mutex_lock(&knc->state_lock);
  841. knc->generation++;
  842. knc->scan_adjust=0;
  843. if (!knc->generation)
  844. knc->generation++;
  845. mutex_unlock(&knc->state_lock);
  846. }
  847. static void knc_zero_stats(struct cgpu_info *cgpu)
  848. {
  849. int core;
  850. struct knc_state *knc = cgpu->device_data;
  851. mutex_lock(&knc->state_lock);
  852. for (core = 0; core < knc->cores; core++) {
  853. knc->shares = 0;
  854. knc->completed = 0;
  855. knc->works = 0;
  856. knc->errors = 0;
  857. knc->core[core].works = 0;
  858. knc->core[core].errors = 0;
  859. knc->core[core].shares = 0;
  860. knc->core[core].completed = 0;
  861. }
  862. mutex_unlock(&knc->state_lock);
  863. }
  864. static struct api_data *knc_api_stats(struct cgpu_info *cgpu)
  865. {
  866. struct knc_state *knc = cgpu->device_data;
  867. struct knc_core_state * const proccore = &knc->core[cgpu->proc_id];
  868. struct knc_die * const die = proccore->die;
  869. struct api_data *root = NULL;
  870. int core;
  871. char label[256];
  872. mutex_lock(&knc->state_lock);
  873. root = api_add_int(root, "dies", &knc->dies, 1);
  874. root = api_add_int(root, "cores", &knc->cores, 1);
  875. root = api_add_uint64(root, "shares", &knc->shares, 1);
  876. root = api_add_uint64(root, "works", &knc->works, 1);
  877. root = api_add_uint64(root, "completed", &knc->completed, 1);
  878. root = api_add_uint64(root, "errors", &knc->errors, 1);
  879. /* Active cores */
  880. int active = knc->cores;
  881. for (core = 0; core < knc->cores; core++) {
  882. if (knc_core_disabled(&knc->core[core]))
  883. active -= 1;
  884. }
  885. root = api_add_int(root, "active", &active, 1);
  886. /* Per ASIC/die data */
  887. {
  888. #define knc_api_die_string(name, value) do { \
  889. snprintf(label, sizeof(label), "%d.%d.%s", die->channel, die->die, name); \
  890. root = api_add_string(root, label, value, 1); \
  891. } while(0)
  892. #define knc_api_die_int(name, value) do { \
  893. snprintf(label, sizeof(label), "%d.%d.%s", die->channel, die->die, name); \
  894. uint64_t v = value; \
  895. root = api_add_uint64(root, label, &v, 1); \
  896. } while(0)
  897. /* Model */
  898. {
  899. char *model = "?";
  900. switch(die->version) {
  901. case KNC_VERSION_JUPITER:
  902. model = "Jupiter";
  903. break;
  904. case KNC_VERSION_NEPTUNE:
  905. model = "Neptune";
  906. break;
  907. }
  908. knc_api_die_string("model", model);
  909. knc_api_die_int("cores", die->cores);
  910. }
  911. /* Core based stats */
  912. {
  913. uint64_t errors = 0;
  914. uint64_t shares = 0;
  915. uint64_t works = 0;
  916. uint64_t completed = 0;
  917. char coremap[die->cores+1];
  918. for (core = 0; core < die->cores; core++) {
  919. coremap[core] = knc_core_disabled(&die->core[core]) ? '0' : '1';
  920. works += die->core[core].works;
  921. shares += die->core[core].shares;
  922. errors += die->core[core].errors;
  923. completed += die->core[core].completed;
  924. }
  925. coremap[die->cores] = '\0';
  926. knc_api_die_int("errors", errors);
  927. knc_api_die_int("shares", shares);
  928. knc_api_die_int("works", works);
  929. knc_api_die_int("completed", completed);
  930. knc_api_die_string("coremap", coremap);
  931. }
  932. }
  933. mutex_unlock(&knc->state_lock);
  934. return root;
  935. }
  936. static
  937. void hash_driver_work(struct thr_info * const thr)
  938. {
  939. struct cgpu_info * const cgpu = thr->cgpu;
  940. struct device_drv * const drv = cgpu->drv;
  941. while (likely(!cgpu->shutdown))
  942. {
  943. drv->scanwork(thr);
  944. if (unlikely(thr->pause || cgpu->deven != DEV_ENABLED))
  945. mt_disable(thr);
  946. if (unlikely(thr->work_restart)) {
  947. thr->work_restart = false;
  948. flush_queue(cgpu);
  949. drv->flush_work(cgpu);
  950. }
  951. }
  952. }
  953. struct device_drv kncasic_drv = {
  954. .dname = "kncasic",
  955. .name = "KNC",
  956. .drv_detect = kncasic_detect,
  957. .thread_init = knc_init,
  958. .minerloop = hash_driver_work,
  959. .flush_work = knc_flush_work,
  960. .scanwork = knc_scanwork,
  961. .zero_stats = knc_zero_stats,
  962. .get_api_stats = knc_api_stats,
  963. };