driver-kncasic.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * cgminer driver for KnCminer devices
  3. *
  4. * Copyright 2014 KnCminer
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include <stdlib.h>
  12. #include <assert.h>
  13. #include <fcntl.h>
  14. #include <limits.h>
  15. #include <unistd.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/time.h>
  18. #include <linux/types.h>
  19. #include <linux/spi/spidev.h>
  20. #include <zlib.h>
  21. #include "logging.h"
  22. #include "miner.h"
  23. #include "knc-transport.h"
  24. #include "knc-asic.h"
  25. #define MAX_ASICS 6
  26. #define DIES_PER_ASIC 4
  27. #define MAX_CORES_PER_DIE 360
  28. #define WORKS_PER_CORE 3
  29. #define CORE_ERROR_LIMIT 30
  30. #define CORE_ERROR_INTERVAL 30
  31. #define CORE_ERROR_DISABLE_TIME 5*60
  32. #define CORE_SUBMIT_MIN_TIME 2
  33. #define CORE_TIMEOUT 20
  34. #define SCAN_ADJUST_RANGE 32
  35. static struct timeval now;
  36. static const struct timeval core_check_interval = {
  37. CORE_ERROR_INTERVAL, 0
  38. };
  39. static const struct timeval core_disable_interval = {
  40. CORE_ERROR_DISABLE_TIME, 0
  41. };
  42. static const struct timeval core_submit_interval = {
  43. CORE_SUBMIT_MIN_TIME, 0
  44. };
  45. static const struct timeval core_timeout_interval = {
  46. CORE_TIMEOUT, 0
  47. };
  48. struct knc_die;
  49. struct knc_core_state {
  50. int generation;
  51. int core;
  52. int coreid;
  53. struct knc_die *die;
  54. struct {
  55. int slot;
  56. struct work *work;
  57. } workslot[WORKS_PER_CORE]; /* active, next */
  58. int transfer_stamp;
  59. struct knc_report report;
  60. struct {
  61. int slot;
  62. uint32_t nonce;
  63. } last_nonce;
  64. uint32_t works;
  65. uint32_t shares;
  66. uint32_t errors;
  67. uint32_t completed;
  68. int last_slot;
  69. uint32_t errors_now;
  70. struct timeval disabled_until;
  71. struct timeval hold_work_until;
  72. struct timeval timeout;
  73. bool inuse;
  74. };
  75. struct knc_state;
  76. struct knc_die {
  77. int channel;
  78. int die;
  79. int version;
  80. int cores;
  81. struct knc_state *knc;
  82. struct knc_core_state *core;
  83. };
  84. #define MAX_SPI_SIZE (4096)
  85. #define MAX_SPI_RESPONSES (MAX_SPI_SIZE / (2 + 4 + 1 + 1 + 1 + 4))
  86. #define MAX_SPI_MESSAGE (128)
  87. #define KNC_SPI_BUFFERS (3)
  88. struct knc_state {
  89. struct cgpu_info *cgpu;
  90. void *ctx;
  91. int generation; /* work/block generation, incremented on each flush invalidating older works */
  92. int dies;
  93. struct knc_die die[MAX_ASICS*DIES_PER_ASIC];
  94. int cores;
  95. int scan_adjust;
  96. int startup;
  97. /* Statistics */
  98. uint64_t shares; /* diff1 shares reported by hardware */
  99. uint64_t works; /* Work units submitted */
  100. uint64_t completed; /* Work units completed */
  101. uint64_t errors; /* Hardware & communication errors */
  102. struct timeval next_error_interval;
  103. /* End of statistics */
  104. /* SPI communications thread */
  105. pthread_mutex_t spi_qlock; /* SPI queue status lock */
  106. struct thr_info spi_thr; /* SPI I/O thread */
  107. pthread_cond_t spi_qcond; /* SPI queue change wakeup */
  108. struct knc_spi_buffer {
  109. enum {
  110. KNC_SPI_IDLE=0,
  111. KNC_SPI_PENDING,
  112. KNC_SPI_DONE
  113. } state;
  114. int size;
  115. uint8_t txbuf[MAX_SPI_SIZE];
  116. uint8_t rxbuf[MAX_SPI_SIZE];
  117. int responses;
  118. struct knc_spi_response {
  119. int request_length;
  120. int response_length;
  121. enum {
  122. KNC_UNKNOWN = 0,
  123. KNC_NO_RESPONSE,
  124. KNC_SETWORK,
  125. KNC_REPORT,
  126. KNC_INFO
  127. } type;
  128. struct knc_core_state *core;
  129. uint32_t data;
  130. int offset;
  131. } response_info[MAX_SPI_RESPONSES];
  132. } spi_buffer[KNC_SPI_BUFFERS];
  133. int send_buffer;
  134. int read_buffer;
  135. int send_buffer_count;
  136. int read_buffer_count;
  137. /* end SPI thread */
  138. /* lock to protect resources between different threads */
  139. pthread_mutex_t state_lock;
  140. /* Do not add anything below here!! core[] must be last */
  141. struct knc_core_state core[];
  142. };
  143. int opt_knc_device_idx = 0;
  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_sync(struct thr_info *thr)
  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. int sent = 0;
  200. pthread_mutex_lock(&knc->spi_qlock);
  201. if (buffer->state == KNC_SPI_IDLE && buffer->size > 0) {
  202. buffer->state = KNC_SPI_PENDING;
  203. pthread_cond_signal(&knc->spi_qcond);
  204. knc->send_buffer += 1;
  205. knc->send_buffer_count += 1;
  206. if (knc->send_buffer >= KNC_SPI_BUFFERS)
  207. knc->send_buffer = 0;
  208. sent = 1;
  209. }
  210. int prev_buffer = knc->send_buffer - 1;
  211. if (prev_buffer < 0)
  212. prev_buffer = KNC_SPI_BUFFERS - 1;
  213. buffer = &knc->spi_buffer[prev_buffer];
  214. while (buffer->state == KNC_SPI_PENDING)
  215. pthread_cond_wait(&knc->spi_qcond, &knc->spi_qlock);
  216. pthread_mutex_unlock(&knc->spi_qlock);
  217. int pending = knc->send_buffer - knc->read_buffer;
  218. if (pending <= 0)
  219. pending += KNC_SPI_BUFFERS;
  220. pending -= 1 - sent;
  221. applog(LOG_INFO, "KnC: sync %d pending buffers", pending);
  222. knc_process_responses(thr);
  223. }
  224. 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)
  225. {
  226. struct cgpu_info *cgpu = thr->cgpu;
  227. struct knc_state *knc = cgpu->device_data;
  228. struct knc_spi_buffer *buffer = &knc->spi_buffer[knc->send_buffer];
  229. /* FPGA control, request header, request body/response, CRC(4), ACK(1), EXTRA(3) */
  230. int msglen = 2 + MAX(request_length, 4 + response_length ) + 4 + 1 + 3;
  231. if (buffer->size + msglen > MAX_SPI_SIZE || buffer->responses >= MAX_SPI_RESPONSES) {
  232. applog(LOG_INFO, "KnC: SPI buffer sent, %d messages %d bytes", buffer->responses, buffer->size);
  233. knc_flush(thr);
  234. buffer = &knc->spi_buffer[knc->send_buffer];
  235. }
  236. struct knc_spi_response *response_info = &buffer->response_info[buffer->responses];
  237. buffer->responses++;
  238. response_info->offset = buffer->size;
  239. response_info->type = response_type;
  240. response_info->request_length = request_length;
  241. response_info->response_length = response_length;
  242. response_info->core = core;
  243. response_info->data = data;
  244. buffer->size = knc_prepare_transfer(buffer->txbuf, buffer->size, MAX_SPI_SIZE, core->die->channel, request_length, request, response_length);
  245. }
  246. static int knc_transfer_stamp(struct knc_state *knc)
  247. {
  248. return knc->send_buffer_count;
  249. }
  250. static int knc_transfer_completed(struct knc_state *knc, int stamp)
  251. {
  252. /* signed delta math, counter wrap OK */
  253. return (int)(knc->read_buffer_count - stamp) >= 1;
  254. }
  255. static bool knc_detect_one(void *ctx)
  256. {
  257. /* Scan device for ASICs */
  258. int channel, die, cores = 0, core;
  259. struct cgpu_info *cgpu;
  260. struct knc_state *knc;
  261. struct knc_die_info die_info[MAX_ASICS][DIES_PER_ASIC];
  262. memset(die_info, 0, sizeof(die_info));
  263. /* Send GETINFO to each die to detect if it is usable */
  264. for (channel = 0; channel < MAX_ASICS; channel++) {
  265. if (!knc_trnsp_asic_detect(ctx, channel))
  266. continue;
  267. for (die = 0; die < DIES_PER_ASIC; die++) {
  268. if (knc_detect_die(ctx, channel, die, &die_info[channel][die]) == 0)
  269. cores += die_info[channel][die].cores;
  270. }
  271. }
  272. if (!cores) {
  273. applog(LOG_NOTICE, "no KnCminer cores found");
  274. return false;
  275. }
  276. applog(LOG_ERR, "Found a KnC miner with %d cores", cores);
  277. cgpu = calloc(1, sizeof(*cgpu));
  278. knc = calloc(1, sizeof(*knc) + cores * sizeof(struct knc_core_state));
  279. if (!cgpu || !knc) {
  280. applog(LOG_ERR, "KnC miner detected, but failed to allocate memory");
  281. return false;
  282. }
  283. knc->cgpu = cgpu;
  284. knc->ctx = ctx;
  285. knc->generation = 1;
  286. /* Index all cores */
  287. int dies = 0;
  288. cores = 0;
  289. struct knc_core_state *pcore = knc->core;
  290. for (channel = 0; channel < MAX_ASICS; channel++) {
  291. for (die = 0; die < DIES_PER_ASIC; die++) {
  292. if (die_info[channel][die].cores) {
  293. knc->die[dies].channel = channel;
  294. knc->die[dies].die = die;
  295. knc->die[dies].version = die_info[channel][die].version;
  296. knc->die[dies].cores = die_info[channel][die].cores;
  297. knc->die[dies].core = pcore;
  298. knc->die[dies].knc = knc;
  299. for (core = 0; core < knc->die[dies].cores; core++) {
  300. knc->die[dies].core[core].die = &knc->die[dies];
  301. knc->die[dies].core[core].core = core;
  302. }
  303. cores += knc->die[dies].cores;
  304. pcore += knc->die[dies].cores;
  305. dies++;
  306. }
  307. }
  308. }
  309. for (core = 0; core < cores; core++)
  310. knc->core[core].coreid = core;
  311. knc->dies = dies;
  312. knc->cores = cores;
  313. knc->startup = 2;
  314. cgpu->drv = &knc_drv;
  315. cgpu->name = "KnCminer";
  316. cgpu->threads = 1;
  317. cgpu->device_data = knc;
  318. pthread_mutex_init(&knc->spi_qlock, NULL);
  319. pthread_cond_init(&knc->spi_qcond, NULL);
  320. pthread_mutex_init(&knc->state_lock, NULL);
  321. if (thr_info_create(&knc->spi_thr, NULL, knc_spi, (void *)cgpu)) {
  322. applog(LOG_ERR, "%s%i: SPI thread create failed",
  323. cgpu->drv->name, cgpu->device_id);
  324. free(cgpu);
  325. free(knc);
  326. return false;
  327. }
  328. add_cgpu(cgpu);
  329. return true;
  330. }
  331. /* Probe devices and register with add_cgpu */
  332. void knc_detect(bool __maybe_unused hotplug)
  333. {
  334. void *ctx = knc_trnsp_new(opt_knc_device_idx);
  335. if (ctx != NULL) {
  336. if (!knc_detect_one(ctx))
  337. knc_trnsp_free(ctx);
  338. }
  339. }
  340. /* Core helper functions */
  341. static int knc_core_hold_work(struct knc_core_state *core)
  342. {
  343. return timercmp(&core->hold_work_until, &now, >);
  344. }
  345. static int knc_core_has_work(struct knc_core_state *core)
  346. {
  347. int i;
  348. for (i = 0; i < WORKS_PER_CORE; i++) {
  349. if (core->workslot[i].slot > 0)
  350. return true;
  351. }
  352. return false;
  353. }
  354. static int knc_core_need_work(struct knc_core_state *core)
  355. {
  356. return !knc_core_hold_work(core) && !core->workslot[1].work && !core->workslot[2].work;
  357. }
  358. static int knc_core_disabled(struct knc_core_state *core)
  359. {
  360. return timercmp(&core->disabled_until, &now, >);
  361. }
  362. static int _knc_core_next_slot(struct knc_core_state *core)
  363. {
  364. /* Avoid slot #0 and #15. #0 is "no work assigned" and #15 is seen on bad cores */
  365. int slot = core->last_slot + 1;
  366. if (slot >= 15)
  367. slot = 1;
  368. core->last_slot = slot;
  369. return slot;
  370. }
  371. static bool knc_core_slot_busy(struct knc_core_state *core, int slot)
  372. {
  373. if (slot == core->report.active_slot)
  374. return true;
  375. if (slot == core->report.next_slot)
  376. return true;
  377. int i;
  378. for (i = 0; i < WORKS_PER_CORE; i++) {
  379. if (slot == core->workslot[i].slot)
  380. return true;
  381. }
  382. return false;
  383. }
  384. static int knc_core_next_slot(struct knc_core_state *core)
  385. {
  386. int slot;
  387. do slot = _knc_core_next_slot(core);
  388. while (knc_core_slot_busy(core, slot));
  389. return slot;
  390. }
  391. static void knc_core_failure(struct knc_core_state *core)
  392. {
  393. core->errors++;
  394. core->errors_now++;
  395. core->die->knc->errors++;
  396. if (knc_core_disabled(core))
  397. return;
  398. if (core->errors_now > CORE_ERROR_LIMIT) {
  399. applog(LOG_ERR, "KnC: %d.%d.%d disabled for %d seconds due to repeated hardware errors",
  400. core->die->channel, core->die->die, core->core, core_disable_interval.tv_sec);
  401. timeradd(&now, &core_disable_interval, &core->disabled_until);
  402. }
  403. }
  404. static int knc_core_handle_nonce(struct thr_info *thr, struct knc_core_state *core, int slot, uint32_t nonce)
  405. {
  406. int i;
  407. if (!slot)
  408. return;
  409. core->last_nonce.slot = slot;
  410. core->last_nonce.nonce = nonce;
  411. if (core->die->knc->startup)
  412. return;
  413. for (i = 0; i < WORKS_PER_CORE; i++) {
  414. if (slot == core->workslot[i].slot && core->workslot[i].work) {
  415. applog(LOG_INFO, "KnC: %d.%d.%d found nonce %08x", core->die->channel, core->die->die, core->core, nonce);
  416. if (submit_nonce(thr, core->workslot[i].work, nonce)) {
  417. /* Good share */
  418. core->shares++;
  419. core->die->knc->shares++;
  420. /* This core is useful. Ignore any errors */
  421. core->errors_now = 0;
  422. } else {
  423. applog(LOG_INFO, "KnC: %d.%d.%d hwerror nonce %08x", core->die->channel, core->die->die, core->core, nonce);
  424. /* Bad share */
  425. knc_core_failure(core);
  426. }
  427. }
  428. }
  429. }
  430. static int knc_core_process_report(struct thr_info *thr, struct knc_core_state *core, uint8_t *response)
  431. {
  432. struct knc_report *report = &core->report;
  433. knc_decode_report(response, report, core->die->version);
  434. bool had_event = false;
  435. applog(LOG_DEBUG, "KnC %d.%d.%d: Process report %d %d(%d) / %d %d %d", core->die->channel, core->die->die, core->core, report->active_slot, report->next_slot, report->next_state, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  436. int n;
  437. for (n = 0; n < KNC_NONCES_PER_REPORT; n++) {
  438. if (report->nonce[n].slot < 0)
  439. break;
  440. if (core->last_nonce.slot == report->nonce[n].slot && core->last_nonce.nonce == report->nonce[n].nonce)
  441. break;
  442. }
  443. while(n-- > 0) {
  444. knc_core_handle_nonce(thr, core, report->nonce[n].slot, report->nonce[n].nonce);
  445. }
  446. if (report->active_slot && core->workslot[0].slot != report->active_slot) {
  447. had_event = true;
  448. applog(LOG_INFO, "KnC: New work on %d.%d.%d, %d %d / %d %d %d", core->die->channel, core->die->die, core->core, report->active_slot, report->next_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  449. /* Core switched to next work */
  450. if (core->workslot[0].work) {
  451. core->die->knc->completed++;
  452. core->completed++;
  453. applog(LOG_INFO, "KnC: Work completed on core %d.%d.%d!", core->die->channel, core->die->die, core->core);
  454. free_work(core->workslot[0].work);
  455. }
  456. core->workslot[0] = core->workslot[1];
  457. core->workslot[1].work = NULL;
  458. core->workslot[1].slot = -1;
  459. /* or did it switch directly to pending work? */
  460. if (report->active_slot == core->workslot[2].slot) {
  461. applog(LOG_INFO, "KnC: New work on %d.%d.%d, %d %d %d %d (pending)", core->die->channel, core->die->die, core->core, report->active_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  462. if (core->workslot[0].work)
  463. free_work(core->workslot[0].work);
  464. core->workslot[0] = core->workslot[2];
  465. core->workslot[2].work = NULL;
  466. core->workslot[2].slot = -1;
  467. }
  468. }
  469. if (report->next_state && core->workslot[2].slot > 0 && (core->workslot[2].slot == report->next_slot || report->next_slot == -1)) {
  470. had_event = true;
  471. applog(LOG_INFO, "KnC: Accepted work on %d.%d.%d, %d %d %d %d (pending)", core->die->channel, core->die->die, core->core, report->active_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  472. /* core accepted next work */
  473. if (core->workslot[1].work)
  474. free_work(core->workslot[1].work);
  475. core->workslot[1] = core->workslot[2];
  476. core->workslot[2].work = NULL;
  477. core->workslot[2].slot = -1;
  478. }
  479. if (core->workslot[2].work && knc_transfer_completed(core->die->knc, core->transfer_stamp)) {
  480. had_event = true;
  481. applog(LOG_INFO, "KnC: Setwork failed on core %d.%d.%d?", core->die->channel, core->die->die, core->core);
  482. free_work(core->workslot[2].work);
  483. core->workslot[2].slot = -1;
  484. }
  485. if (had_event)
  486. applog(LOG_INFO, "KnC: Exit report on %d.%d.%d, %d %d / %d %d %d", core->die->channel, core->die->die, core->core, report->active_slot, report->next_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  487. return 0;
  488. }
  489. static void knc_process_responses(struct thr_info *thr)
  490. {
  491. struct cgpu_info *cgpu = thr->cgpu;
  492. struct knc_state *knc = cgpu->device_data;
  493. struct knc_spi_buffer *buffer = &knc->spi_buffer[knc->read_buffer];
  494. while (buffer->state == KNC_SPI_DONE) {
  495. int i;
  496. for (i = 0; i < buffer->responses; i++) {
  497. struct knc_spi_response *response_info = &buffer->response_info[i];
  498. uint8_t *rxbuf = &buffer->rxbuf[response_info->offset];
  499. struct knc_core_state *core = response_info->core;
  500. int status = knc_decode_response(rxbuf, response_info->request_length, &rxbuf, response_info->response_length);
  501. /* Invert KNC_ACCEPTED to simplify logics below */
  502. if (response_info->type == KNC_SETWORK && !KNC_IS_ERROR(status))
  503. status ^= KNC_ACCEPTED;
  504. if (core->die->version != KNC_VERSION_JUPITER && status != 0) {
  505. applog(LOG_ERR, "KnC %d.%d.%d: Communication error (%x / %d)", core->die->channel, core->die->die, core->core, status, i);
  506. if (status == KNC_ACCEPTED) {
  507. /* Core refused our work vector. Likely out of sync. Reset it */
  508. core->inuse = false;
  509. }
  510. knc_core_failure(core);
  511. }
  512. switch(response_info->type) {
  513. case KNC_REPORT:
  514. case KNC_SETWORK:
  515. /* Should we care about failed SETWORK explicit? Or simply handle it by next state not loaded indication in reports? */
  516. knc_core_process_report(thr, core, rxbuf);
  517. break;
  518. }
  519. }
  520. buffer->state = KNC_SPI_IDLE;
  521. buffer->responses = 0;
  522. buffer->size = 0;
  523. knc->read_buffer += 1;
  524. knc->read_buffer_count += 1;
  525. if (knc->read_buffer >= KNC_SPI_BUFFERS)
  526. knc->read_buffer = 0;
  527. buffer = &knc->spi_buffer[knc->read_buffer];
  528. }
  529. }
  530. static int knc_core_send_work(struct thr_info *thr, struct knc_core_state *core, struct work *work, bool clean)
  531. {
  532. struct knc_state *knc = core->die->knc;
  533. struct cgpu_info *cgpu = knc->cgpu;
  534. int request_length = 4 + 1 + 6*4 + 3*4 + 8*4;
  535. uint8_t request[request_length];
  536. int response_length = 1 + 1 + (1 + 4) * 5;
  537. uint8_t response[response_length];
  538. int slot = knc_core_next_slot(core);
  539. if (slot < 0)
  540. goto error;
  541. applog(LOG_INFO, "KnC setwork%s %d.%d.%d = %d, %d %d / %d %d %d", clean ? " CLEAN" : "", core->die->channel, core->die->die, core->core, slot, core->report.active_slot, core->report.next_slot, core->workslot[0].slot, core->workslot[1].slot, core->workslot[2].slot);
  542. if (!clean && !knc_core_need_work(core))
  543. goto error;
  544. switch(core->die->version) {
  545. case KNC_VERSION_JUPITER:
  546. if (clean) {
  547. /* Double halt to get rid of any previous queued work */
  548. request_length = knc_prepare_jupiter_halt(request, core->die->die, core->core);
  549. knc_transfer(thr, core, request_length, request, 0, KNC_NO_RESPONSE, 0);
  550. knc_transfer(thr, core, request_length, request, 0, KNC_NO_RESPONSE, 0);
  551. }
  552. request_length = knc_prepare_jupiter_setwork(request, core->die->die, core->core, slot, work);
  553. knc_transfer(thr, core, request_length, request, 0, KNC_NO_RESPONSE, 0);
  554. break;
  555. case KNC_VERSION_NEPTUNE:
  556. request_length = knc_prepare_neptune_setwork(request, core->die->die, core->core, slot, work, clean);
  557. knc_transfer(thr, core, request_length, request, response_length, KNC_SETWORK, slot);
  558. break;
  559. default:
  560. goto error;
  561. }
  562. core->workslot[2].work = work;
  563. core->workslot[2].slot = slot;
  564. core->works++;
  565. core->die->knc->works++;
  566. core->transfer_stamp = knc_transfer_stamp(knc);
  567. core->inuse = true;
  568. timeradd(&now, &core_submit_interval, &core->hold_work_until);
  569. timeradd(&now, &core_timeout_interval, &core->timeout);
  570. return 0;
  571. error:
  572. applog(LOG_INFO, "KnC: %d.%d.%d Failed to setwork (%d)",
  573. core->die->channel, core->die->die, core->core, core->errors_now);
  574. knc_core_failure(core);
  575. free_work(work);
  576. return -1;
  577. }
  578. static int knc_core_request_report(struct thr_info *thr, struct knc_core_state *core)
  579. {
  580. struct knc_state *knc = core->die->knc;
  581. struct cgpu_info *cgpu = knc->cgpu;
  582. int request_length = 4;
  583. uint8_t request[request_length];
  584. int response_length = 1 + 1 + (1 + 4) * 5;
  585. uint8_t response[response_length];
  586. applog(LOG_DEBUG, "KnC: %d.%d.%d Request report", core->die->channel, core->die->die, core->core);
  587. request_length = knc_prepare_report(request, core->die->die, core->core);
  588. switch(core->die->version) {
  589. case KNC_VERSION_JUPITER:
  590. response_length = 1 + 1 + (1 + 4);
  591. knc_transfer(thr, core, request_length, request, response_length, KNC_REPORT, 0);
  592. return 0;
  593. case KNC_VERSION_NEPTUNE:
  594. knc_transfer(thr, core, request_length, request, response_length, KNC_REPORT, 0);
  595. return 0;
  596. }
  597. error:
  598. applog(LOG_INFO, "KnC: Failed to scan work report");
  599. knc_core_failure(core);
  600. return -1;
  601. }
  602. /* return value is number of nonces that have been checked since
  603. * previous call
  604. */
  605. static int64_t knc_scanwork(struct thr_info *thr)
  606. {
  607. #define KNC_COUNT_UNIT shares
  608. struct cgpu_info *cgpu = thr->cgpu;
  609. struct knc_state *knc = cgpu->device_data;
  610. int64_t ret = 0;
  611. uint32_t last_count = knc->KNC_COUNT_UNIT;
  612. applog(LOG_DEBUG, "KnC running scanwork");
  613. mutex_lock(&knc->state_lock);
  614. gettimeofday(&now, NULL);
  615. knc_trnsp_periodic_check(knc->ctx);
  616. int i;
  617. knc_process_responses(thr);
  618. if (timercmp(&knc->next_error_interval, &now, >)) {
  619. /* Reset hw error limiter every check interval */
  620. timeradd(&now, &core_check_interval, &knc->next_error_interval);
  621. for (i = 0; i < knc->cores; i++) {
  622. struct knc_core_state *core = &knc->core[i];
  623. core->errors_now = 0;
  624. }
  625. }
  626. for (i = 0; i < knc->cores; i++) {
  627. struct knc_core_state *core = &knc->core[i];
  628. bool clean = !core->inuse;
  629. if (knc_core_disabled(core))
  630. continue;
  631. if (core->generation != knc->generation) {
  632. applog(LOG_INFO, "KnC %d.%d.%d flush gen=%d/%d", core->die->channel, core->die->die, core->core, core->generation, knc->generation);
  633. /* clean set state, forget everything */
  634. int slot;
  635. for (slot = 0; slot < WORKS_PER_CORE; slot ++) {
  636. if (core->workslot[slot].work)
  637. free_work(core->workslot[slot].work);
  638. core->workslot[slot].slot = -1;
  639. }
  640. core->hold_work_until = now;
  641. core->generation = knc->generation;
  642. } else if (timercmp(&core->timeout, &now, <=) && (core->workslot[0].slot > 0 || core->workslot[1].slot > 0 || core->workslot[2].slot > 0)) {
  643. applog(LOG_ERR, "KnC %d.%d.%d timeout", core->die->channel, core->die->die, core->core, core->generation, knc->generation);
  644. clean = true;
  645. }
  646. if (!knc_core_has_work(core))
  647. clean = true;
  648. if (core->workslot[0].slot < 0 && core->workslot[1].slot < 0 && core->workslot[2].slot < 0)
  649. clean = true;
  650. if (i % SCAN_ADJUST_RANGE == knc->scan_adjust)
  651. clean = true;
  652. if ((knc_core_need_work(core) || clean) && !knc->startup) {
  653. struct work *work = get_work(thr, thr->id);
  654. knc_core_send_work(thr, core, work, clean);
  655. } else {
  656. knc_core_request_report(thr, core);
  657. }
  658. }
  659. /* 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 */
  660. if (knc->startup)
  661. knc->startup--;
  662. else if (knc->scan_adjust < SCAN_ADJUST_RANGE)
  663. knc->scan_adjust++;
  664. knc_flush(thr);
  665. int64_t nonces_number = (int64_t)(knc->KNC_COUNT_UNIT - last_count) * 0x100000000UL;
  666. mutex_unlock(&knc->state_lock);
  667. return nonces_number;
  668. }
  669. static void knc_flush_work(struct cgpu_info *cgpu)
  670. {
  671. struct knc_state *knc = cgpu->device_data;
  672. applog(LOG_INFO, "KnC running flushwork");
  673. mutex_lock(&knc->state_lock);
  674. knc->generation++;
  675. knc->scan_adjust=0;
  676. if (!knc->generation)
  677. knc->generation++;
  678. mutex_unlock(&knc->state_lock);
  679. }
  680. static void knc_zero_stats(struct cgpu_info *cgpu)
  681. {
  682. int core;
  683. struct knc_state *knc = cgpu->device_data;
  684. mutex_lock(&knc->state_lock);
  685. for (core = 0; core < knc->cores; core++) {
  686. knc->shares = 0;
  687. knc->completed = 0;
  688. knc->works = 0;
  689. knc->errors = 0;
  690. knc->core[core].works = 0;
  691. knc->core[core].errors = 0;
  692. knc->core[core].shares = 0;
  693. knc->core[core].completed = 0;
  694. }
  695. mutex_unlock(&knc->state_lock);
  696. }
  697. static struct api_data *knc_api_stats(struct cgpu_info *cgpu)
  698. {
  699. struct knc_state *knc = cgpu->device_data;
  700. struct api_data *root = NULL;
  701. unsigned int cursize;
  702. int asic, core, n;
  703. char label[256];
  704. mutex_lock(&knc->state_lock);
  705. root = api_add_int(root, "dies", &knc->dies, 1);
  706. root = api_add_int(root, "cores", &knc->cores, 1);
  707. root = api_add_uint64(root, "shares", &knc->shares, 1);
  708. root = api_add_uint64(root, "works", &knc->works, 1);
  709. root = api_add_uint64(root, "completed", &knc->completed, 1);
  710. root = api_add_uint64(root, "errors", &knc->errors, 1);
  711. /* Active cores */
  712. int active = knc->cores;
  713. for (core = 0; core < knc->cores; core++) {
  714. if (knc_core_disabled(&knc->core[core]))
  715. active -= 1;
  716. }
  717. root = api_add_int(root, "active", &active, 1);
  718. /* Per ASIC/die data */
  719. for (n = 0; n < knc->dies; n++) {
  720. struct knc_die *die = &knc->die[n];
  721. #define knc_api_die_string(name, value) do { \
  722. snprintf(label, sizeof(label), "%d.%d.%s", die->channel, die->die, name); \
  723. root = api_add_string(root, label, value, 1); \
  724. } while(0)
  725. #define knc_api_die_int(name, value) do { \
  726. snprintf(label, sizeof(label), "%d.%d.%s", die->channel, die->die, name); \
  727. uint64_t v = value; \
  728. root = api_add_uint64(root, label, &v, 1); \
  729. } while(0)
  730. /* Model */
  731. {
  732. char *model = "?";
  733. switch(die->version) {
  734. case KNC_VERSION_JUPITER:
  735. model = "Jupiter";
  736. break;
  737. case KNC_VERSION_NEPTUNE:
  738. model = "Neptune";
  739. break;
  740. }
  741. knc_api_die_string("model", model);
  742. knc_api_die_int("cores", die->cores);
  743. }
  744. /* Core based stats */
  745. {
  746. int active = 0;
  747. uint64_t errors = 0;
  748. uint64_t shares = 0;
  749. uint64_t works = 0;
  750. uint64_t completed = 0;
  751. char coremap[die->cores+1];
  752. for (core = 0; core < die->cores; core++) {
  753. coremap[core] = knc_core_disabled(&die->core[core]) ? '0' : '1';
  754. works += die->core[core].works;
  755. shares += die->core[core].shares;
  756. errors += die->core[core].errors;
  757. completed += die->core[core].completed;
  758. }
  759. coremap[die->cores] = '\0';
  760. knc_api_die_int("errors", errors);
  761. knc_api_die_int("shares", shares);
  762. knc_api_die_int("works", works);
  763. knc_api_die_int("completed", completed);
  764. knc_api_die_string("coremap", coremap);
  765. }
  766. }
  767. mutex_unlock(&knc->state_lock);
  768. return root;
  769. }
  770. struct device_drv knc_drv = {
  771. .drv_id = DRIVER_knc,
  772. .dname = "KnCminer Neptune",
  773. .name = "KnC",
  774. .drv_detect = knc_detect,
  775. .hash_work = hash_driver_work,
  776. .flush_work = knc_flush_work,
  777. .scanwork = knc_scanwork,
  778. .zero_stats = knc_zero_stats,
  779. .get_api_stats = knc_api_stats,
  780. };