driver-knc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. /* Do not add anything below here!! core[] must be last */
  139. struct knc_core_state core[];
  140. };
  141. int opt_knc_device_idx = 0;
  142. int opt_knc_device_bus = -1;
  143. char *knc_log_file = NULL;
  144. static void *knc_spi(void *thr_data)
  145. {
  146. struct cgpu_info *cgpu = thr_data;
  147. struct knc_state *knc = cgpu->device_data;
  148. int buffer = 0;
  149. pthread_mutex_lock(&knc->spi_qlock);
  150. while (!cgpu->shutdown) {
  151. int this_buffer = buffer;
  152. while (knc->spi_buffer[buffer].state != KNC_SPI_PENDING && !cgpu->shutdown)
  153. pthread_cond_wait(&knc->spi_qcond, &knc->spi_qlock);
  154. pthread_mutex_unlock(&knc->spi_qlock);
  155. if (cgpu->shutdown)
  156. return NULL;
  157. knc_trnsp_transfer(knc->ctx, knc->spi_buffer[buffer].txbuf, knc->spi_buffer[buffer].rxbuf, knc->spi_buffer[buffer].size);
  158. buffer += 1;
  159. if (buffer >= KNC_SPI_BUFFERS)
  160. buffer = 0;
  161. pthread_mutex_lock(&knc->spi_qlock);
  162. knc->spi_buffer[this_buffer].state = KNC_SPI_DONE;
  163. pthread_cond_signal(&knc->spi_qcond);
  164. }
  165. pthread_mutex_unlock(&knc->spi_qlock);
  166. return NULL;
  167. }
  168. static void knc_process_responses(struct thr_info *thr);
  169. static void knc_flush(struct thr_info *thr)
  170. {
  171. struct cgpu_info *cgpu = thr->cgpu;
  172. struct knc_state *knc = cgpu->device_data;
  173. struct knc_spi_buffer *buffer = &knc->spi_buffer[knc->send_buffer];
  174. if (buffer->state == KNC_SPI_IDLE && buffer->size > 0) {
  175. pthread_mutex_lock(&knc->spi_qlock);
  176. buffer->state = KNC_SPI_PENDING;
  177. pthread_cond_signal(&knc->spi_qcond);
  178. knc->send_buffer += 1;
  179. knc->send_buffer_count += 1;
  180. if (knc->send_buffer >= KNC_SPI_BUFFERS)
  181. knc->send_buffer = 0;
  182. buffer = &knc->spi_buffer[knc->send_buffer];
  183. /* Block for SPI to finish a transfer if all buffers are busy */
  184. while (buffer->state == KNC_SPI_PENDING) {
  185. applog(LOG_DEBUG, "KnC: SPI buffer full (%d), waiting for SPI thread", buffer->responses);
  186. pthread_cond_wait(&knc->spi_qcond, &knc->spi_qlock);
  187. }
  188. pthread_mutex_unlock(&knc->spi_qlock);
  189. }
  190. knc_process_responses(thr);
  191. }
  192. static void knc_sync(struct thr_info *thr)
  193. {
  194. struct cgpu_info *cgpu = thr->cgpu;
  195. struct knc_state *knc = cgpu->device_data;
  196. struct knc_spi_buffer *buffer = &knc->spi_buffer[knc->send_buffer];
  197. int sent = 0;
  198. pthread_mutex_lock(&knc->spi_qlock);
  199. if (buffer->state == KNC_SPI_IDLE && buffer->size > 0) {
  200. buffer->state = KNC_SPI_PENDING;
  201. pthread_cond_signal(&knc->spi_qcond);
  202. knc->send_buffer += 1;
  203. knc->send_buffer_count += 1;
  204. if (knc->send_buffer >= KNC_SPI_BUFFERS)
  205. knc->send_buffer = 0;
  206. sent = 1;
  207. }
  208. int prev_buffer = knc->send_buffer - 1;
  209. if (prev_buffer < 0)
  210. prev_buffer = KNC_SPI_BUFFERS - 1;
  211. buffer = &knc->spi_buffer[prev_buffer];
  212. while (buffer->state == KNC_SPI_PENDING)
  213. pthread_cond_wait(&knc->spi_qcond, &knc->spi_qlock);
  214. pthread_mutex_unlock(&knc->spi_qlock);
  215. int pending = knc->send_buffer - knc->read_buffer;
  216. if (pending <= 0)
  217. pending += KNC_SPI_BUFFERS;
  218. pending -= 1 - sent;
  219. applog(LOG_INFO, "KnC: sync %d pending buffers", pending);
  220. knc_process_responses(thr);
  221. }
  222. 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)
  223. {
  224. struct cgpu_info *cgpu = thr->cgpu;
  225. struct knc_state *knc = cgpu->device_data;
  226. struct knc_spi_buffer *buffer = &knc->spi_buffer[knc->send_buffer];
  227. /* FPGA control, request header, request body/response, CRC(4), ACK(1), EXTRA(3) */
  228. int msglen = 2 + MAX(request_length, 4 + response_length ) + 4 + 1 + 3;
  229. if (buffer->size + msglen > MAX_SPI_SIZE || buffer->responses >= MAX_SPI_RESPONSES) {
  230. applog(LOG_INFO, "KnC: SPI buffer sent, %d messages %d bytes", buffer->responses, buffer->size);
  231. knc_flush(thr);
  232. buffer = &knc->spi_buffer[knc->send_buffer];
  233. }
  234. struct knc_spi_response *response_info = &buffer->response_info[buffer->responses];
  235. buffer->responses++;
  236. response_info->offset = buffer->size;
  237. response_info->type = response_type;
  238. response_info->request_length = request_length;
  239. response_info->response_length = response_length;
  240. response_info->core = core;
  241. response_info->data = data;
  242. buffer->size = knc_prepare_transfer(buffer->txbuf, buffer->size, MAX_SPI_SIZE, core->die->channel, request_length, request, response_length);
  243. }
  244. static int knc_transfer_stamp(struct knc_state *knc)
  245. {
  246. return knc->send_buffer_count;
  247. }
  248. static int knc_transfer_completed(struct knc_state *knc, int stamp)
  249. {
  250. /* signed delta math, counter wrap OK */
  251. return (int)(knc->read_buffer_count - stamp) >= 1;
  252. }
  253. static bool knc_detect_one(void *ctx)
  254. {
  255. /* Scan device for ASICs */
  256. int channel, die, cores = 0, core;
  257. struct cgpu_info *cgpu;
  258. struct knc_state *knc;
  259. struct knc_die_info die_info[MAX_ASICS][DIES_PER_ASIC];
  260. memset(die_info, 0, sizeof(die_info));
  261. /* Send GETINFO to each die to detect if it is usable */
  262. for (channel = 0; channel < MAX_ASICS; channel++) {
  263. if (!knc_trnsp_asic_detect(ctx, channel))
  264. continue;
  265. for (die = 0; die < DIES_PER_ASIC; die++) {
  266. if (knc_detect_die(ctx, channel, die, &die_info[channel][die]) == 0)
  267. cores += die_info[channel][die].cores;
  268. }
  269. }
  270. if (!cores) {
  271. applog(LOG_NOTICE, "no KnCminer cores found");
  272. return false;
  273. }
  274. applog(LOG_ERR, "Found a KnC miner with %d cores", cores);
  275. cgpu = calloc(1, sizeof(*cgpu));
  276. knc = calloc(1, sizeof(*knc) + cores * sizeof(struct knc_core_state));
  277. if (!cgpu || !knc) {
  278. applog(LOG_ERR, "KnC miner detected, but failed to allocate memory");
  279. return false;
  280. }
  281. knc->cgpu = cgpu;
  282. knc->ctx = ctx;
  283. knc->generation = 1;
  284. /* Index all cores */
  285. int dies = 0;
  286. cores = 0;
  287. struct knc_core_state *pcore = knc->core;
  288. for (channel = 0; channel < MAX_ASICS; channel++) {
  289. for (die = 0; die < DIES_PER_ASIC; die++) {
  290. if (die_info[channel][die].cores) {
  291. knc->die[dies].channel = channel;
  292. knc->die[dies].die = die;
  293. knc->die[dies].version = die_info[channel][die].version;
  294. knc->die[dies].cores = die_info[channel][die].cores;
  295. knc->die[dies].core = pcore;
  296. knc->die[dies].knc = knc;
  297. for (core = 0; core < knc->die[dies].cores; core++) {
  298. knc->die[dies].core[core].die = &knc->die[dies];
  299. knc->die[dies].core[core].core = core;
  300. }
  301. cores += knc->die[dies].cores;
  302. pcore += knc->die[dies].cores;
  303. dies++;
  304. }
  305. }
  306. }
  307. for (core = 0; core < cores; core++)
  308. knc->core[core].coreid = core;
  309. knc->dies = dies;
  310. knc->cores = cores;
  311. knc->startup = 2;
  312. cgpu->drv = &knc_drv;
  313. cgpu->name = "KnCminer";
  314. cgpu->threads = 1;
  315. cgpu->device_data = knc;
  316. pthread_mutex_init(&knc->spi_qlock, NULL);
  317. pthread_cond_init(&knc->spi_qcond, NULL);
  318. if (thr_info_create(&knc->spi_thr, NULL, knc_spi, (void *)cgpu)) {
  319. applog(LOG_ERR, "%s%i: SPI thread create failed",
  320. cgpu->drv->name, cgpu->device_id);
  321. free(cgpu);
  322. free(knc);
  323. return false;
  324. }
  325. add_cgpu(cgpu);
  326. return true;
  327. }
  328. /* Probe devices and register with add_cgpu */
  329. void knc_detect(bool __maybe_unused hotplug)
  330. {
  331. void *ctx = knc_trnsp_new(opt_knc_device_idx);
  332. if (ctx != NULL) {
  333. if (!knc_detect_one(ctx))
  334. knc_trnsp_free(ctx);
  335. }
  336. }
  337. /* Core helper functions */
  338. static int knc_core_hold_work(struct knc_core_state *core)
  339. {
  340. return timercmp(&core->hold_work_until, &now, >);
  341. }
  342. static int knc_core_has_work(struct knc_core_state *core)
  343. {
  344. int i;
  345. for (i = 0; i < WORKS_PER_CORE; i++) {
  346. if (core->workslot[i].slot > 0)
  347. return true;
  348. }
  349. return false;
  350. }
  351. static int knc_core_need_work(struct knc_core_state *core)
  352. {
  353. return !knc_core_hold_work(core) && !core->workslot[1].work && !core->workslot[2].work;
  354. }
  355. static int knc_core_disabled(struct knc_core_state *core)
  356. {
  357. return timercmp(&core->disabled_until, &now, >);
  358. }
  359. static int _knc_core_next_slot(struct knc_core_state *core)
  360. {
  361. /* Avoid slot #0 and #15. #0 is "no work assigned" and #15 is seen on bad cores */
  362. int slot = core->last_slot + 1;
  363. if (slot >= 15)
  364. slot = 1;
  365. core->last_slot = slot;
  366. return slot;
  367. }
  368. static bool knc_core_slot_busy(struct knc_core_state *core, int slot)
  369. {
  370. if (slot == core->report.active_slot)
  371. return true;
  372. if (slot == core->report.next_slot)
  373. return true;
  374. int i;
  375. for (i = 0; i < WORKS_PER_CORE; i++) {
  376. if (slot == core->workslot[i].slot)
  377. return true;
  378. }
  379. return false;
  380. }
  381. static int knc_core_next_slot(struct knc_core_state *core)
  382. {
  383. int slot;
  384. do slot = _knc_core_next_slot(core);
  385. while (knc_core_slot_busy(core, slot));
  386. return slot;
  387. }
  388. static void knc_core_failure(struct knc_core_state *core)
  389. {
  390. core->errors++;
  391. core->errors_now++;
  392. core->die->knc->errors++;
  393. if (knc_core_disabled(core))
  394. return;
  395. if (core->errors_now > CORE_ERROR_LIMIT) {
  396. applog(LOG_ERR, "KnC: %d.%d.%d disabled for %d seconds due to repeated hardware errors",
  397. core->die->channel, core->die->die, core->core, core_disable_interval.tv_sec);
  398. timeradd(&now, &core_disable_interval, &core->disabled_until);
  399. }
  400. }
  401. static int knc_core_handle_nonce(struct thr_info *thr, struct knc_core_state *core, int slot, uint32_t nonce)
  402. {
  403. int i;
  404. if (!slot)
  405. return;
  406. core->last_nonce.slot = slot;
  407. core->last_nonce.nonce = nonce;
  408. if (core->die->knc->startup)
  409. return;
  410. for (i = 0; i < WORKS_PER_CORE; i++) {
  411. if (slot == core->workslot[i].slot && core->workslot[i].work) {
  412. applog(LOG_INFO, "KnC: %d.%d.%d found nonce %08x", core->die->channel, core->die->die, core->core, nonce);
  413. if (submit_nonce(thr, core->workslot[i].work, nonce)) {
  414. /* Good share */
  415. core->shares++;
  416. core->die->knc->shares++;
  417. /* This core is useful. Ignore any errors */
  418. core->errors_now = 0;
  419. } else {
  420. applog(LOG_INFO, "KnC: %d.%d.%d hwerror nonce %08x", core->die->channel, core->die->die, core->core, nonce);
  421. /* Bad share */
  422. knc_core_failure(core);
  423. }
  424. }
  425. }
  426. }
  427. static int knc_core_process_report(struct thr_info *thr, struct knc_core_state *core, uint8_t *response)
  428. {
  429. struct knc_report *report = &core->report;
  430. knc_decode_report(response, report, core->die->version);
  431. bool had_event = false;
  432. 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);
  433. int n;
  434. for (n = 0; n < KNC_NONCES_PER_REPORT; n++) {
  435. if (report->nonce[n].slot < 0)
  436. break;
  437. if (core->last_nonce.slot == report->nonce[n].slot && core->last_nonce.nonce == report->nonce[n].nonce)
  438. break;
  439. }
  440. while(n-- > 0) {
  441. knc_core_handle_nonce(thr, core, report->nonce[n].slot, report->nonce[n].nonce);
  442. }
  443. if (report->active_slot && core->workslot[0].slot != report->active_slot) {
  444. had_event = true;
  445. 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);
  446. /* Core switched to next work */
  447. if (core->workslot[0].work) {
  448. core->die->knc->completed++;
  449. core->completed++;
  450. applog(LOG_INFO, "KnC: Work completed on core %d.%d.%d!", core->die->channel, core->die->die, core->core);
  451. free_work(core->workslot[0].work);
  452. }
  453. core->workslot[0] = core->workslot[1];
  454. core->workslot[1].work = NULL;
  455. core->workslot[1].slot = -1;
  456. /* or did it switch directly to pending work? */
  457. if (report->active_slot == core->workslot[2].slot) {
  458. 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);
  459. if (core->workslot[0].work)
  460. free_work(core->workslot[0].work);
  461. core->workslot[0] = core->workslot[2];
  462. core->workslot[2].work = NULL;
  463. core->workslot[2].slot = -1;
  464. }
  465. }
  466. if (report->next_state && core->workslot[2].slot > 0 && (core->workslot[2].slot == report->next_slot || report->next_slot == -1)) {
  467. had_event = true;
  468. 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);
  469. /* core accepted next work */
  470. if (core->workslot[1].work)
  471. free_work(core->workslot[1].work);
  472. core->workslot[1] = core->workslot[2];
  473. core->workslot[2].work = NULL;
  474. core->workslot[2].slot = -1;
  475. }
  476. if (core->workslot[2].work && knc_transfer_completed(core->die->knc, core->transfer_stamp)) {
  477. had_event = true;
  478. applog(LOG_INFO, "KnC: Setwork failed on core %d.%d.%d?", core->die->channel, core->die->die, core->core);
  479. free_work(core->workslot[2].work);
  480. core->workslot[2].slot = -1;
  481. }
  482. if (had_event)
  483. 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);
  484. return 0;
  485. }
  486. static void knc_process_responses(struct thr_info *thr)
  487. {
  488. struct cgpu_info *cgpu = thr->cgpu;
  489. struct knc_state *knc = cgpu->device_data;
  490. struct knc_spi_buffer *buffer = &knc->spi_buffer[knc->read_buffer];
  491. while (buffer->state == KNC_SPI_DONE) {
  492. int i;
  493. for (i = 0; i < buffer->responses; i++) {
  494. struct knc_spi_response *response_info = &buffer->response_info[i];
  495. uint8_t *rxbuf = &buffer->rxbuf[response_info->offset];
  496. struct knc_core_state *core = response_info->core;
  497. int status = knc_decode_response(rxbuf, response_info->request_length, &rxbuf, response_info->response_length);
  498. /* Invert KNC_ACCEPTED to simplify logics below */
  499. if (response_info->type == KNC_SETWORK && !KNC_IS_ERROR(status))
  500. status ^= KNC_ACCEPTED;
  501. if (core->die->version != KNC_VERSION_JUPITER && status != 0) {
  502. applog(LOG_ERR, "KnC %d.%d.%d: Communication error (%x / %d)", core->die->channel, core->die->die, core->core, status, i);
  503. if (status == KNC_ACCEPTED) {
  504. /* Core refused our work vector. Likely out of sync. Reset it */
  505. core->inuse = false;
  506. }
  507. knc_core_failure(core);
  508. }
  509. switch(response_info->type) {
  510. case KNC_REPORT:
  511. case KNC_SETWORK:
  512. /* Should we care about failed SETWORK explicit? Or simply handle it by next state not loaded indication in reports? */
  513. knc_core_process_report(thr, core, rxbuf);
  514. break;
  515. }
  516. }
  517. buffer->state = KNC_SPI_IDLE;
  518. buffer->responses = 0;
  519. buffer->size = 0;
  520. knc->read_buffer += 1;
  521. knc->read_buffer_count += 1;
  522. if (knc->read_buffer >= KNC_SPI_BUFFERS)
  523. knc->read_buffer = 0;
  524. buffer = &knc->spi_buffer[knc->read_buffer];
  525. }
  526. }
  527. static int knc_core_send_work(struct thr_info *thr, struct knc_core_state *core, struct work *work, bool clean)
  528. {
  529. struct knc_state *knc = core->die->knc;
  530. struct cgpu_info *cgpu = knc->cgpu;
  531. int request_length = 4 + 1 + 6*4 + 3*4 + 8*4;
  532. uint8_t request[request_length];
  533. int response_length = 1 + 1 + (1 + 4) * 5;
  534. uint8_t response[response_length];
  535. int slot = knc_core_next_slot(core);
  536. if (slot < 0)
  537. goto error;
  538. 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);
  539. if (!clean && !knc_core_need_work(core))
  540. goto error;
  541. switch(core->die->version) {
  542. case KNC_VERSION_JUPITER:
  543. if (clean) {
  544. /* Double halt to get rid of any previous queued work */
  545. request_length = knc_prepare_jupiter_halt(request, core->die->die, core->core);
  546. knc_transfer(thr, core, request_length, request, 0, KNC_NO_RESPONSE, 0);
  547. knc_transfer(thr, core, request_length, request, 0, KNC_NO_RESPONSE, 0);
  548. }
  549. request_length = knc_prepare_jupiter_setwork(request, core->die->die, core->core, slot, work);
  550. knc_transfer(thr, core, request_length, request, 0, KNC_NO_RESPONSE, 0);
  551. break;
  552. case KNC_VERSION_NEPTUNE:
  553. request_length = knc_prepare_neptune_setwork(request, core->die->die, core->core, slot, work, clean);
  554. knc_transfer(thr, core, request_length, request, response_length, KNC_SETWORK, slot);
  555. break;
  556. default:
  557. goto error;
  558. }
  559. core->workslot[2].work = work;
  560. core->workslot[2].slot = slot;
  561. core->works++;
  562. core->die->knc->works++;
  563. core->transfer_stamp = knc_transfer_stamp(knc);
  564. core->inuse = true;
  565. timeradd(&now, &core_submit_interval, &core->hold_work_until);
  566. timeradd(&now, &core_timeout_interval, &core->timeout);
  567. return 0;
  568. error:
  569. applog(LOG_INFO, "KnC: %d.%d.%d Failed to setwork (%d)",
  570. core->die->channel, core->die->die, core->core, core->errors_now);
  571. knc_core_failure(core);
  572. free_work(work);
  573. return -1;
  574. }
  575. static int knc_core_request_report(struct thr_info *thr, struct knc_core_state *core)
  576. {
  577. struct knc_state *knc = core->die->knc;
  578. struct cgpu_info *cgpu = knc->cgpu;
  579. int request_length = 4;
  580. uint8_t request[request_length];
  581. int response_length = 1 + 1 + (1 + 4) * 5;
  582. uint8_t response[response_length];
  583. applog(LOG_DEBUG, "KnC: %d.%d.%d Request report", core->die->channel, core->die->die, core->core);
  584. request_length = knc_prepare_report(request, core->die->die, core->core);
  585. switch(core->die->version) {
  586. case KNC_VERSION_JUPITER:
  587. response_length = 1 + 1 + (1 + 4);
  588. knc_transfer(thr, core, request_length, request, response_length, KNC_REPORT, 0);
  589. return 0;
  590. case KNC_VERSION_NEPTUNE:
  591. knc_transfer(thr, core, request_length, request, response_length, KNC_REPORT, 0);
  592. return 0;
  593. }
  594. error:
  595. applog(LOG_INFO, "KnC: Failed to scan work report");
  596. knc_core_failure(core);
  597. return -1;
  598. }
  599. /* return value is number of nonces that have been checked since
  600. * previous call
  601. */
  602. static int64_t knc_scanwork(struct thr_info *thr)
  603. {
  604. #define KNC_COUNT_UNIT shares
  605. struct cgpu_info *cgpu = thr->cgpu;
  606. struct knc_state *knc = cgpu->device_data;
  607. int64_t ret = 0;
  608. uint32_t last_count = knc->KNC_COUNT_UNIT;
  609. applog(LOG_DEBUG, "KnC running scanwork");
  610. gettimeofday(&now, NULL);
  611. knc_trnsp_periodic_check(knc->ctx);
  612. int i;
  613. knc_process_responses(thr);
  614. if (timercmp(&knc->next_error_interval, &now, >)) {
  615. /* Reset hw error limiter every check interval */
  616. timeradd(&now, &core_check_interval, &knc->next_error_interval);
  617. for (i = 0; i < knc->cores; i++) {
  618. struct knc_core_state *core = &knc->core[i];
  619. core->errors_now = 0;
  620. }
  621. }
  622. for (i = 0; i < knc->cores; i++) {
  623. struct knc_core_state *core = &knc->core[i];
  624. bool clean = !core->inuse;
  625. if (knc_core_disabled(core))
  626. continue;
  627. if (core->generation != knc->generation) {
  628. applog(LOG_INFO, "KnC %d.%d.%d flush gen=%d/%d", core->die->channel, core->die->die, core->core, core->generation, knc->generation);
  629. /* clean set state, forget everything */
  630. int slot;
  631. for (slot = 0; slot < WORKS_PER_CORE; slot ++) {
  632. if (core->workslot[slot].work)
  633. free_work(core->workslot[slot].work);
  634. core->workslot[slot].slot = -1;
  635. }
  636. core->hold_work_until = now;
  637. core->generation = knc->generation;
  638. } else if (timercmp(&core->timeout, &now, <=) && (core->workslot[0].slot > 0 || core->workslot[1].slot > 0 || core->workslot[2].slot > 0)) {
  639. applog(LOG_ERR, "KnC %d.%d.%d timeout", core->die->channel, core->die->die, core->core, core->generation, knc->generation);
  640. clean = true;
  641. }
  642. if (!knc_core_has_work(core))
  643. clean = true;
  644. if (core->workslot[0].slot < 0 && core->workslot[1].slot < 0 && core->workslot[2].slot < 0)
  645. clean = true;
  646. if (i % SCAN_ADJUST_RANGE == knc->scan_adjust)
  647. clean = true;
  648. if ((knc_core_need_work(core) || clean) && !knc->startup) {
  649. struct work *work = get_work(thr, thr->id);
  650. knc_core_send_work(thr, core, work, clean);
  651. } else {
  652. knc_core_request_report(thr, core);
  653. }
  654. }
  655. /* 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 */
  656. if (knc->startup)
  657. knc->startup--;
  658. else if (knc->scan_adjust < SCAN_ADJUST_RANGE)
  659. knc->scan_adjust++;
  660. knc_flush(thr);
  661. return (int64_t)(knc->KNC_COUNT_UNIT - last_count) * 0x100000000UL;
  662. }
  663. static void knc_flush_work(struct cgpu_info *cgpu)
  664. {
  665. struct knc_state *knc = cgpu->device_data;
  666. applog(LOG_INFO, "KnC running flushwork");
  667. knc->generation++;
  668. knc->scan_adjust=0;
  669. if (!knc->generation)
  670. knc->generation++;
  671. }
  672. static void knc_zero_stats(struct cgpu_info *cgpu)
  673. {
  674. int core;
  675. struct knc_state *knc = cgpu->device_data;
  676. for (core = 0; core < knc->cores; core++) {
  677. knc->shares = 0;
  678. knc->completed = 0;
  679. knc->works = 0;
  680. knc->errors = 0;
  681. knc->core[core].works = 0;
  682. knc->core[core].errors = 0;
  683. knc->core[core].shares = 0;
  684. knc->core[core].completed = 0;
  685. }
  686. }
  687. static struct api_data *knc_api_stats(struct cgpu_info *cgpu)
  688. {
  689. struct knc_state *knc = cgpu->device_data;
  690. struct api_data *root = NULL;
  691. unsigned int cursize;
  692. int asic, core, n;
  693. char label[256];
  694. root = api_add_int(root, "dies", &knc->dies, 1);
  695. root = api_add_int(root, "cores", &knc->cores, 1);
  696. root = api_add_uint64(root, "shares", &knc->shares, 1);
  697. root = api_add_uint64(root, "works", &knc->works, 1);
  698. root = api_add_uint64(root, "completed", &knc->completed, 1);
  699. root = api_add_uint64(root, "errors", &knc->errors, 1);
  700. /* Active cores */
  701. int active = knc->cores;
  702. for (core = 0; core < knc->cores; core++) {
  703. if (knc_core_disabled(&knc->core[core]))
  704. active -= 1;
  705. }
  706. root = api_add_int(root, "active", &active, 1);
  707. /* Per ASIC/die data */
  708. for (n = 0; n < knc->dies; n++) {
  709. struct knc_die *die = &knc->die[n];
  710. #define knc_api_die_string(name, value) do { \
  711. snprintf(label, sizeof(label), "%d.%d.%s", die->channel, die->die, name); \
  712. root = api_add_string(root, label, value, 1); \
  713. } while(0)
  714. #define knc_api_die_int(name, value) do { \
  715. snprintf(label, sizeof(label), "%d.%d.%s", die->channel, die->die, name); \
  716. uint64_t v = value; \
  717. root = api_add_uint64(root, label, &v, 1); \
  718. } while(0)
  719. /* Model */
  720. {
  721. char *model = "?";
  722. switch(die->version) {
  723. case KNC_VERSION_JUPITER:
  724. model = "Jupiter";
  725. break;
  726. case KNC_VERSION_NEPTUNE:
  727. model = "Neptune";
  728. break;
  729. }
  730. knc_api_die_string("model", model);
  731. knc_api_die_int("cores", die->cores);
  732. }
  733. /* Core based stats */
  734. {
  735. int active = 0;
  736. uint64_t errors = 0;
  737. uint64_t shares = 0;
  738. uint64_t works = 0;
  739. uint64_t completed = 0;
  740. char coremap[die->cores+1];
  741. for (core = 0; core < die->cores; core++) {
  742. coremap[core] = knc_core_disabled(&die->core[core]) ? '0' : '1';
  743. works += die->core[core].works;
  744. shares += die->core[core].shares;
  745. errors += die->core[core].errors;
  746. completed += die->core[core].completed;
  747. }
  748. coremap[die->cores] = '\0';
  749. knc_api_die_int("errors", errors);
  750. knc_api_die_int("shares", shares);
  751. knc_api_die_int("works", works);
  752. knc_api_die_int("completed", completed);
  753. knc_api_die_string("coremap", coremap);
  754. }
  755. }
  756. return root;
  757. }
  758. struct device_drv knc_drv = {
  759. .drv_id = DRIVER_knc,
  760. .dname = "KnCminer Neptune",
  761. .name = "KnC",
  762. .drv_detect = knc_detect,
  763. .hash_work = hash_driver_work,
  764. .flush_work = knc_flush_work,
  765. .scanwork = knc_scanwork,
  766. .zero_stats = knc_zero_stats,
  767. .get_api_stats = knc_api_stats,
  768. };