driver-kncasic.c 26 KB

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