driver-kncasic.c 26 KB

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