Hash :
c104366f
Author :
Date :
2014-06-18T16:41:03
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
/*
* cgminer driver for KnCminer devices
*
* Copyright 2014 KnCminer
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include <stdlib.h>
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <zlib.h>
#include "logging.h"
#include "miner.h"
#include "knc-transport.h"
#include "knc-asic.h"
#define MAX_ASICS 6
#define DIES_PER_ASIC 4
#define MAX_CORES_PER_DIE 360
#define WORKS_PER_CORE 2
#define CORE_ERROR_LIMIT 30
#define CORE_ERROR_INTERVAL 30
#define CORE_ERROR_DISABLE_TIME 5*60
#define CORE_SUBMIT_MIN_TIME 2
#define CORE_TIMEOUT 10
static struct timeval now;
static const struct timeval core_check_interval = {
CORE_ERROR_INTERVAL, 0
};
static const struct timeval core_disable_interval = {
CORE_ERROR_DISABLE_TIME, 0
};
static const struct timeval core_submit_interval = {
CORE_SUBMIT_MIN_TIME, 0
};
static const struct timeval core_timeout_interval = {
CORE_TIMEOUT, 0
};
struct knc_die;
struct knc_core_state {
int generation;
int core;
struct knc_die *die;
struct {
int slot;
struct work *work;
} workslot[WORKS_PER_CORE];
struct {
int slot;
uint32_t nonce;
} seen_nonces[5];
struct {
int slot;
uint32_t nonce;
} last_nonce;
uint32_t works;
uint32_t shares;
uint32_t errors;
uint32_t completed;
int last_slot;
uint32_t errors_now;
struct timeval disabled_until;
struct timeval hold_work_until;
struct timeval timeout;
};
struct knc_state;
struct knc_die {
int channel;
int die;
int version;
int cores;
struct knc_state *knc;
struct knc_core_state *core;
};
struct knc_state {
struct cgpu_info *cgpu;
void *ctx;
int generation; /* work/block generation, incremented on each flush invalidating older works */
int channel[MAX_ASICS];
int dies;
struct knc_die die[MAX_ASICS*DIES_PER_ASIC];
int cores;
int scan_adjust_core;
int startup;
/* Statistics */
uint64_t shares; /* diff1 shares reported by hardware */
uint64_t works; /* Work units submitted */
uint64_t completed; /* Work units completed */
uint64_t errors; /* Hardware & communication errors */
struct timeval next_error_interval;
/* End of statistics */
struct knc_core_state core[];
};
int opt_knc_device_idx = 0;
int opt_knc_device_bus = -1;
char *knc_log_file = NULL;
static bool knc_detect_one(void *ctx)
{
/* Scan device for ASICs */
int channel, die, cores = 0, core;
struct cgpu_info *cgpu;
struct knc_state *knc;
struct knc_die_info die_info[MAX_ASICS][DIES_PER_ASIC];
memset(die_info, 0, sizeof(die_info));
/* Send GETINFO to each die to detect if it is usable */
for (channel = 0; channel < MAX_ASICS; channel++) {
if (!knc_trnsp_asic_detect(ctx, channel))
continue;
for (die = 0; die < DIES_PER_ASIC; die++) {
if (knc_detect_die(ctx, channel, die, &die_info[channel][die]) == 0)
cores += die_info[channel][die].cores;
}
}
if (!cores) {
applog(LOG_NOTICE, "no KnCminer cores found");
return false;
}
applog(LOG_ERR, "Found a KnC miner with %d cores", cores);
cgpu = calloc(1, sizeof(*cgpu));
knc = calloc(1, sizeof(*knc) + cores * sizeof(struct knc_core_state));
if (!cgpu || !knc) {
applog(LOG_ERR, "KnC miner detected, but failed to allocate memory");
return false;
}
knc->cgpu = cgpu;
knc->ctx = ctx;
knc->generation = 1;
/* Index all cores */
int dies = 0;
cores = 0;
struct knc_core_state *pcore = knc->core;
for (channel = 0; channel < MAX_ASICS; channel++) {
for (die = 0; die < DIES_PER_ASIC; die++) {
if (die_info[channel][die].cores) {
knc->channel[channel] = 1;
knc->die[dies].channel = channel;
knc->die[dies].die = die;
knc->die[dies].version = die_info[channel][die].version;
knc->die[dies].cores = die_info[channel][die].cores;
knc->die[dies].core = pcore;
knc->die[dies].knc = knc;
for (core = 0; core < knc->die[dies].cores; core++) {
knc->die[dies].core[core].die = &knc->die[dies];
knc->die[dies].core[core].core = core;
}
cores += knc->die[dies].cores;
pcore += knc->die[dies].cores;
dies++;
}
}
}
knc->dies = dies;
knc->cores = cores;
knc->startup = 2;
cgpu->drv = &knc_drv;
cgpu->name = "KnCminer";
cgpu->threads = 1;
cgpu->device_data = knc;
add_cgpu(cgpu);
return true;
}
/* Probe devices and register with add_cgpu */
void knc_detect(bool __maybe_unused hotplug)
{
void *ctx = knc_trnsp_new(opt_knc_device_idx);
if (ctx != NULL) {
if (!knc_detect_one(ctx))
knc_trnsp_free(ctx);
}
}
/* Core helper functions */
static int knc_core_hold_work(struct knc_core_state *core)
{
return timercmp(&core->hold_work_until, &now, >);
}
static int knc_core_need_work(struct knc_core_state *core)
{
return !knc_core_hold_work(core) && !core->workslot[1].work;
}
static int knc_core_disabled(struct knc_core_state *core)
{
return timercmp(&core->disabled_until, &now, >);
}
static int knc_core_next_slot(struct knc_core_state *core)
{
int slot = core->last_slot + 1;
if (slot >= 15)
slot = 1;
core->last_slot = slot;
return slot;
}
static void knc_core_failure(struct knc_core_state *core)
{
core->errors++;
core->errors_now++;
if (knc_core_disabled(core))
return;
if (core->errors_now > CORE_ERROR_LIMIT) {
applog(LOG_ERR, "KnC: %d.%d.%d disabled for %d seconds due to repeated hardware errors",
core->die->channel, core->die->die, core->core, core_disable_interval.tv_sec);
timeradd(&now, &core_disable_interval, &core->disabled_until);
}
}
static int knc_core_handle_nonce(struct thr_info *thr, struct knc_core_state *core, int slot, uint32_t nonce)
{
int i;
if (!slot)
return;
core->last_nonce.slot = slot;
core->last_nonce.nonce = nonce;
if (core->die->knc->startup)
return;
for (i = 0; i < WORKS_PER_CORE; i++) {
if (slot == core->workslot[i].slot && core->workslot[i].work) {
applog(LOG_INFO, "KnC: %d.%d.%d found nonce %08x", core->die->channel, core->die->die, core->core, nonce);
if (submit_nonce(thr, core->workslot[i].work, nonce)) {
/* Good share */
core->shares++;
core->die->knc->shares++;
} else {
applog(LOG_INFO, "KnC: %d.%d.%d hwerror nonce %08x", core->die->channel, core->die->die, core->core, nonce);
/* Bad share */
knc_core_failure(core);
}
}
}
}
static int knc_core_process_report(struct thr_info *thr, struct knc_core_state *core, uint8_t *report)
{
int n_nonces = core->die->version == KNC_VERSION_NEPTUNE ? 5 : 1;
struct {
int slot;
uint32_t nonce;
} nonces[5];
int n;
for (n = 0; n < n_nonces; n++) {
int slot = report[1+1+0+(1+4)*n]&0x0f;
uint32_t nonce = report[1+1+1+(1+4)*n] << 24 |
report[1+1+2+(1+4)*n] << 16 |
report[1+1+3+(1+4)*n] << 8 |
report[1+1+4+(1+4)*n] << 0;
if (core->last_nonce.slot == slot && core->last_nonce.nonce == nonce)
break;
nonces[n].slot = slot;
nonces[n].nonce = nonce;
}
while(n-- > 0) {
knc_core_handle_nonce(thr, core, nonces[n].slot, nonces[n].nonce);
}
int active_slot = report[2] >> 4;
if (active_slot && core->workslot[1].slot == active_slot) {
/* Core switched to next work */
if (core->workslot[0].work) {
core->die->knc->completed++;
core->completed++;
applog(LOG_INFO, "KnC: Work completed on core %d.%d.%d!", core->die->channel, core->die->die, core->core);
free_work(core->workslot[0].work);
}
core->workslot[0] = core->workslot[1];
core->workslot[1].work = NULL;
core->workslot[1].slot = 0;
}
return 0;
}
static int knc_core_send_work(struct thr_info *thr, struct knc_core_state *core, struct work *work, bool clean)
{
struct knc_state *knc = core->die->knc;
struct cgpu_info *cgpu = knc->cgpu;
int request_length = 4 + 1 + 6*4 + 3*4 + 8*4;
uint8_t request[request_length];
int response_length = 1 + 1 + (1 + 4) * 5;
uint8_t response[response_length];
int status;
int slot = knc_core_next_slot(core);
if (slot < 0)
goto error;
applog(LOG_INFO, "KnC setwork%s %d.%d.%d slot %x", clean ? " CLEAN" : "", core->die->channel, core->die->die, core->core, slot);
if (!clean && !knc_core_need_work(core))
goto error;
switch(core->die->version) {
case KNC_VERSION_JUPITER:
if (clean) {
/* Double halt to get rid of any previous queued work */
request_length = knc_prepare_jupiter_halt(request, core->die->die, core->core);
knc_syncronous_transfer(knc->ctx, core->die->channel, request_length, request, 0, NULL);
knc_syncronous_transfer(knc->ctx, core->die->channel, request_length, request, 0, NULL);
}
request_length = knc_prepare_jupiter_setwork(request, core->die->die, core->core, slot, work);
knc_syncronous_transfer(knc->ctx, core->die->channel, request_length, request, 0, NULL);
break;
case KNC_VERSION_NEPTUNE:
request_length = knc_prepare_neptune_setwork(request, core->die->die, core->core, slot, work, clean);
status = knc_syncronous_transfer(knc->ctx, core->die->channel, request_length, request, response_length, response);
if (status != KNC_ACCEPTED) {
applog(LOG_INFO, "KnC: Communication error %x", status);
goto error;
}
knc_core_process_report(thr, core, response);
break;
default:
goto error;
}
core->workslot[1].work = work;
core->workslot[1].slot = slot;
core->generation = knc->generation;
core->works++;
timeradd(&now, &core_submit_interval, &core->hold_work_until);
timeradd(&now, &core_timeout_interval, &core->timeout);
return 0;
error:
applog(LOG_NOTICE, "KnC: %d.%d.%d Failed to setwork (%d)",
core->die->channel, core->die->die, core->core, core->errors_now);
if (core->generation != ~0) {
core->generation = ~0; /* Flush it, We are likely out of sync */
} else {
knc_core_failure(core);
}
free_work(work);
return -1;
}
static int knc_core_get_report(struct thr_info *thr, struct knc_core_state *core)
{
struct knc_state *knc = core->die->knc;
struct cgpu_info *cgpu = knc->cgpu;
int request_length = 4;
uint8_t request[request_length];
int response_length = 1 + 1 + (1 + 4) * 5;
uint8_t response[response_length];
int status;
request_length = knc_prepare_report(request, core->die->die, core->core);
switch(core->die->version) {
case KNC_VERSION_JUPITER:
response_length = 1 + 1 + (1 + 4);
knc_syncronous_transfer(knc->ctx, core->die->channel, request_length, request, response_length, response);
knc_core_process_report(thr, core, response);
return 0;
case KNC_VERSION_NEPTUNE:
status = knc_syncronous_transfer(knc->ctx, core->die->channel, request_length, request, response_length, response);
if (status != 0)
break;
knc_core_process_report(thr, core, response);
return 0;
}
error:
applog(LOG_NOTICE, "KnC: Failed to scan work report");
knc_core_failure(core);
return -1;
}
/* return value is number of nonces that have been checked since
* previous call
*/
static int64_t knc_scanwork(struct thr_info *thr)
{
#define KNC_COUNT_UNIT shares
struct cgpu_info *cgpu = thr->cgpu;
struct knc_state *knc = cgpu->device_data;
int64_t ret = 0;
uint32_t last_count = knc->KNC_COUNT_UNIT;
applog(LOG_DEBUG, "KnC running scanwork");
gettimeofday(&now, NULL);
knc_trnsp_periodic_check(knc->ctx);
int i;
if (timercmp(&knc->next_error_interval, &now, >)) {
/* Reset hw error limiter every check interval */
timeradd(&now, &core_check_interval, &knc->next_error_interval);
for (i = 0; i < knc->cores; i++) {
struct knc_core_state *core = &knc->core[i];
core->errors_now = 0;
}
}
for (i = 0; i < knc->cores; i++) {
bool clean = false;
struct knc_core_state *core = &knc->core[i];
if (core->generation != knc->generation || timercmp(&core->timeout, &now, <)) {
/* clean set state, forget everything */
clean = true;
int slot;
for (slot = 0; slot < WORKS_PER_CORE; slot ++) {
if (core->workslot[slot].work)
free_work(core->workslot[slot].work);
}
core->hold_work_until = now;
}
if (knc_core_disabled(core))
continue;
if (i == knc->scan_adjust_core) {
/* TODO: Do a forced submit to even out work generation over time.
* but don't forget scheduled works until the new one gets active
*/
}
if (knc_core_need_work(core)) {
struct work *work = get_work(thr, thr->id);
knc_core_send_work(thr, core, work, clean);
} else {
knc_core_get_report(thr, core);
}
}
if (knc->startup)
knc->startup--;
if (knc->scan_adjust_core < knc->cores)
knc->scan_adjust_core++;
return (int64_t)(knc->KNC_COUNT_UNIT - last_count) * 0x100000000UL;
}
static void knc_flush_work(struct cgpu_info *cgpu)
{
struct knc_state *knc = cgpu->device_data;
applog(LOG_INFO, "KnC running flushwork");
knc->generation++;
knc->scan_adjust_core=0;
if (!knc->generation)
knc->generation++;
}
static void knc_zero_stats(struct cgpu_info *cgpu)
{
int core;
struct knc_state *knc = cgpu->device_data;
for (core = 0; core < knc->cores; core++) {
knc->completed = 0;
knc->core[core].errors = 0;
knc->core[core].shares = 0;
knc->core[core].completed = 0;
}
}
static struct api_data *knc_api_stats(struct cgpu_info *cgpu)
{
struct knc_state *knc = cgpu->device_data;
struct api_data *root = NULL;
unsigned int cursize;
int asic, core, n;
char label[256];
root = api_add_int(root, "dies", &knc->dies, 1);
root = api_add_int(root, "cores", &knc->cores, 1);
root = api_add_uint64(root, "shares", &knc->shares, 1);
root = api_add_uint64(root, "works", &knc->works, 1);
root = api_add_uint64(root, "completed", &knc->completed, 1);
root = api_add_uint64(root, "errors", &knc->errors, 1);
/* Active cores */
int active = knc->cores;
for (core = 0; core < knc->cores; core++) {
if (knc_core_disabled(&knc->core[core]))
active -= 1;
}
root = api_add_int(root, "active", &active, 1);
/* Per ASIC/die data */
for (n = 0; n < knc->dies; n++) {
struct knc_die *die = &knc->die[n];
#define knc_api_die_string(name, value) do { \
snprintf(label, sizeof(label), "%d.%d.%s", die->channel, die->die, name); \
root = api_add_string(root, label, value, 1); \
} while(0)
#define knc_api_die_int(name, value) do { \
snprintf(label, sizeof(label), "%d.%d.%s", die->channel, die->die, name); \
uint64_t v = value; \
root = api_add_uint64(root, label, &v, 1); \
} while(0)
/* Model */
{
char *model = "?";
switch(die->version) {
case KNC_VERSION_JUPITER:
model = "Jupiter";
break;
case KNC_VERSION_NEPTUNE:
model = "Neptune";
break;
}
knc_api_die_string("model", model);
knc_api_die_int("cores", die->cores);
}
/* Core based stats */
{
int active = 0;
uint64_t errors = 0;
uint64_t shares = 0;
uint64_t works = 0;
uint64_t completed = 0;
char coremap[die->cores+1];
/* core map */
for (core = 0; core < die->cores; core++) {
coremap[core] = knc_core_disabled(&die->core[core]) ? '0' : '1';
works += die->core[core].works;
shares += die->core[core].shares;
errors += die->core[core].errors;
completed += die->core[core].completed;
}
knc_api_die_string("coremap", coremap);
knc_api_die_int("errors", errors);
knc_api_die_int("shares", shares);
knc_api_die_int("works", works);
knc_api_die_int("completed", completed);
}
}
return root;
}
struct device_drv knc_drv = {
.drv_id = DRIVER_knc,
.dname = "KnCminer Neptune",
.name = "KnC",
.drv_detect = knc_detect,
.hash_work = hash_driver_work,
.flush_work = knc_flush_work,
.scanwork = knc_scanwork,
.zero_stats = knc_zero_stats,
.get_api_stats = knc_api_stats,
};