driver-kncasic.c 25 KB

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