-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdriver-zeus.c
executable file
·756 lines (544 loc) · 15.9 KB
/
driver-zeus.c
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/*
* Copyright 2012 Luke Dashjr
* Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
* Copyright 2012 Andrew Smith
*
* 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.
*/
/*
* The driver supports Zeus scrypt chip (www.zeusminer.com)
* It comes from driver-icarus.c
* Reference Docs:
* https://onedrive.live.com/view.aspx?resid=7B9890399DE89575!603&ithint=file%2c.docx&app=Word&authkey=!ABXOuckyMb5Cu2w
*
*/
#include "config.h"
#include "miner.h"
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#ifndef WIN32
#include <termios.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#else
#include <windows.h>
#include <io.h>
#endif
#include <math.h>
#include "elist.h"
#include "fpgautils.h"
#include "compat.h"
#define CHIP_GEN1_CORES 8
#define CHIP_GEN 1
#define CHIP_CORES CHIP_GEN1_CORES
int opt_chips_count_max=1;
// The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
#define ZEUS_IO_SPEED 115200
// The size of a successful nonce read
#define ZEUS_READ_SIZE 4
// Ensure the sizes are correct for the Serial read
#if (ZEUS_READ_SIZE != 4)
#error ZEUS_READ_SIZE must be 4
#endif
#define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
ASSERT1(sizeof(uint32_t) == 4);
// Fraction of a second, USB timeout is measured in
// i.e. 10 means 1/10 of a second
#define TIME_FACTOR 10
// It's 10 per second, thus value = 10/TIME_FACTOR =
#define ZEUS_READ_FAULT_DECISECONDS 1
struct ZEUS_INFO {
uint32_t read_count;
uint64_t golden_speed_percore;// speed pre core per sec
int check_num;
int baud;
int cores_perchip;
int chips_count_max;
int chips_count;
int chip_clk;
uint32_t clk_header;
int chips_bit_num;//log2(chips_count_max)
char core_hash[10];
char chip_hash[10];
char board_hash[10];
};
// One for each possible device
static struct ZEUS_INFO **zeus_info;
static int option_offset = -1;
struct device_drv zeus_drv;
uint8_t flush_buf[400];
void flush_uart(int fd)
{
#ifdef WIN32
//read(fd, flush_buf, 100);
const HANDLE fh = (HANDLE)_get_osfhandle(fd);
PurgeComm(fh, PURGE_RXCLEAR);
#else
tcflush(fd, TCIFLUSH);
//read(fd, flush_buf, 100);
#endif
}
int log_2(int value) //非递归判断一个数是2的多少次方
{
int x=0;
while(value>1)
{
value>>=1;
x++;
}
return x;
}
uint32_t get_revindex(uint32_t value,int bit_num)
{
uint32_t newvalue;
int i;
#if CHIP_GEN==1
value = (value&0x1ff80000)>>(29-bit_num);
#else
#error
#endif
newvalue=0;
for(i=0;i<bit_num;i++){
newvalue = newvalue<<1;
newvalue += value&0x01;
value = value>>1;
}
return newvalue;
}
static void rev(unsigned char *s, size_t l)
{
size_t i, j;
unsigned char t;
for (i = 0, j = l - 1; i < j; i++, j--) {
t = s[i];
s[i] = s[j];
s[j] = t;
}
}
#define zeus_open2(devpath, baud, purge) serial_open(devpath, baud, ZEUS_READ_FAULT_DECISECONDS, purge)
#define zeus_open(devpath, baud) zeus_open2(devpath, baud, false)
#define ZEUS_GETS_ERROR -1
#define ZEUS_GETS_OK 0
#define ZEUS_GETS_RESTART 1
#define ZEUS_GETS_TIMEOUT 2
static int zeus_gets(unsigned char *buf, int fd, struct timeval *tv_finish, struct thr_info *thr, int read_count,uint32_t *elapsed_count)
{
ssize_t ret = 0;
int rc = 0;
int read_amount = ZEUS_READ_SIZE;
bool first = true;
*elapsed_count = 0;
// Read reply 1 byte at a time to get earliest tv_finish
while (true) {
ret = read(fd, buf, 1);
if (ret < 0)
return ZEUS_GETS_ERROR;
if (first)
cgtime(tv_finish);
if (ret >= read_amount)
return ZEUS_GETS_OK;
if (ret > 0) {
buf += ret;
read_amount -= ret;
first = false;
continue;
}
rc++;
*elapsed_count=rc;
if (rc >= read_count) {
if (opt_debug) {
applog(LOG_DEBUG,
"ZEUS Read: No data in %.2f seconds",
(float)rc/(float)TIME_FACTOR);
}
return ZEUS_GETS_TIMEOUT;
}
if (thr && thr->work_restart) {
if (opt_debug) {
applog(LOG_DEBUG,
"ZEUS Read: Work restart at %.2f seconds",
(float)(rc)/(float)TIME_FACTOR);
}
return ZEUS_GETS_RESTART;
}
}
}
static int zeus_write(int fd, const void *buf, size_t bufLen)
{
size_t ret;
#if 0
char *hexstr;
hexstr = bin2hex(buf, bufLen);
applog(LOG_WARNING, "zeus_write %s", hexstr);
free(hexstr);
#endif
ret = write(fd, buf, bufLen);
if (unlikely(ret != bufLen))
return 1;
return 0;
}
#define zeus_close(fd) close(fd)
static void do_zeus_close(struct thr_info *thr)
{
struct cgpu_info *zeus = thr->cgpu;
zeus_close(zeus->device_fd);
zeus->device_fd = -1;
}
extern void suffix_string(uint64_t val, char *buf, int sigdigits);
int update_num(int chips_count)
{
int i;
for (i=1;i<1024;i=i*2){
if (chips_count<=i){
return i;
}
}
return 1024;
}
static bool zeus_detect_one(const char *devpath)
{
int this_option_offset = ++option_offset;
struct ZEUS_INFO *info;
int fd;
struct timeval tv_start, tv_finish;
struct timeval golden_tv;
int numbytes = 84;
int baud, cores_perchip, chips_count_max,chips_count;
uint32_t clk_reg;
uint32_t clk_reg_init;
uint64_t golden_speed_percore;
#if 1
if(opt_chip_clk>(0xff*3/2)){
opt_chip_clk = 0xff*3/2;
}
else if(opt_chip_clk<2){
opt_chip_clk = 2;
}
clk_reg= (uint32_t)(opt_chip_clk*2/3);
#endif
char clk_header_str[10];
#if 1
char golden_ob[] =
"55aa0001"
"00038000063b0b1b028f32535e900609c15dc49a42b1d8492a6dd4f8f15295c989a1decf584a6aa93be26066d3185f55ef635b5865a7a79b7fa74121a6bb819da416328a9bd2f8cef72794bf02000000";
char golden_ob2[] =
"55aa00ff"
"c00278894532091be6f16a5381ad33619dacb9e6a4a6e79956aac97b51112bfb93dc450b8fc765181a344b6244d42d78625f5c39463bbfdc10405ff711dc1222dd065b015ac9c2c66e28da7202000000";
const char golden_nonce[] = "00038d26";
const uint32_t golden_nonce_val = 0x00038d26;// 0xd26= 3366
#endif
unsigned char ob_bin[84], nonce_bin[ZEUS_READ_SIZE];
char *nonce_hex;
baud = ZEUS_IO_SPEED;
cores_perchip = CHIP_CORES;
chips_count = opt_chips_count;
if(chips_count>opt_chips_count_max){
opt_chips_count_max = update_num(chips_count);
}
chips_count_max = opt_chips_count_max;
applog(LOG_DEBUG, "Zeus Detect: Attempting to open %s", devpath);
fd = zeus_open2(devpath, baud, true);
if (unlikely(fd == -1)) {
applog(LOG_ERR, "Zeus Detect: Failed to open %s", devpath);
return false;
}
uint32_t clk_header;
//from 150M step to the high or low speed. we need to add delay and resend to init chip
if(clk_reg>(150*2/3)){
clk_reg_init = 165*2/3;
}
else {
clk_reg_init = 139*2/3;
}
flush_uart(fd);
clk_header = (clk_reg_init<<24)+((0xff-clk_reg_init)<<16);
sprintf(clk_header_str,"%08x",clk_header+0x01);
memcpy(golden_ob2,clk_header_str,8);
hex2bin(ob_bin, golden_ob2, numbytes);
zeus_write(fd, ob_bin, numbytes);
sleep(1);
flush_uart(fd);
zeus_write(fd, ob_bin, numbytes);
sleep(1);
flush_uart(fd);
zeus_write(fd, ob_bin, numbytes);
read(fd, flush_buf, 400);
clk_header = (clk_reg<<24)+((0xff-clk_reg)<<16);
sprintf(clk_header_str,"%08x",clk_header+0x01);
memcpy(golden_ob2,clk_header_str,8);
hex2bin(ob_bin, golden_ob2, numbytes);
zeus_write(fd, ob_bin, numbytes);
sleep(1);
flush_uart(fd);
zeus_write(fd, ob_bin, numbytes);
sleep(1);
flush_uart(fd);
clk_header = (clk_reg<<24)+((0xff-clk_reg)<<16);
sprintf(clk_header_str,"%08x",clk_header+1);
memcpy(golden_ob,clk_header_str,8);
if (opt_ltc_nocheck_golden==false){
read(fd, flush_buf, 400);
hex2bin(ob_bin, golden_ob, numbytes);
zeus_write(fd, ob_bin, numbytes);
cgtime(&tv_start);
memset(nonce_bin, 0, sizeof(nonce_bin));
uint32_t elapsed_count;
zeus_gets(nonce_bin, fd, &tv_finish, NULL, 50,&elapsed_count);
zeus_close(fd);
nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
if (strncmp(nonce_hex, golden_nonce, 8)) {
applog(LOG_ERR,
"Zeus Detect: "
"Test failed at %s: get %s, should: %s",
devpath, nonce_hex, golden_nonce);
free(nonce_hex);
return false;
}
timersub(&tv_finish, &tv_start, &golden_tv);
golden_speed_percore = (uint64_t)(((double)0xd26)/((double)(golden_tv.tv_sec) + ((double)(golden_tv.tv_usec))/((double)1000000)));
if(opt_ltc_debug){
applog(LOG_ERR,
"[Test succeeded] at %s: got %s.",
devpath, nonce_hex);
}
free(nonce_hex);
}
else{
zeus_close(fd);
golden_speed_percore = (((opt_chip_clk*2)/3)*1024)/8;
}
/* We have a real Zeus! */
struct cgpu_info *zeus;
zeus = calloc(1, sizeof(struct cgpu_info));
zeus->drv = &zeus_drv;
zeus->device_path = strdup(devpath);
zeus->device_fd = -1;
zeus->threads = 1;
add_cgpu(zeus);
zeus_info = realloc(zeus_info, sizeof(struct ZEUS_INFO *) * (total_devices + 1));
applog(LOG_INFO, "Found Zeus at %s, mark as %d",
devpath, zeus->device_id);
applog(LOG_DEBUG, "Zeus: Init: %d baud=%d cores_perchip=%d chips_count=%d",
zeus->device_id, baud, cores_perchip, chips_count);
// Since we are adding a new device on the end it needs to always be allocated
zeus_info[zeus->device_id] = (struct ZEUS_INFO *)malloc(sizeof(struct ZEUS_INFO));
if (unlikely(!(zeus_info[zeus->device_id])))
quit(1, "Failed to malloc ZEUS_INFO");
info = zeus_info[zeus->device_id];
// Initialise everything to zero for a new device
memset(info, 0, sizeof(struct ZEUS_INFO));
info->check_num = 0x1234;
info->baud = baud;
info->cores_perchip = cores_perchip;
info->chips_count = chips_count;
info->chips_count_max= chips_count_max;
if ((chips_count_max &(chips_count_max-1))!=0){
quit(1, "chips_count_max must be 2^n");
}
info->chips_bit_num = log_2(chips_count_max);
info->golden_speed_percore = golden_speed_percore;
info->read_count = (uint32_t)((4294967296*10)/(cores_perchip*chips_count_max*golden_speed_percore*2));
if(info->read_count>opt_zeus_readcount){
info->read_count = opt_zeus_readcount;//send a new work every 10 seconds
}
info->chip_clk=opt_chip_clk;
info->clk_header=clk_header;
suffix_string(golden_speed_percore, info->core_hash, 0);
suffix_string(golden_speed_percore*cores_perchip, info->chip_hash, 0);
suffix_string(golden_speed_percore*cores_perchip*chips_count, info->board_hash, 0);
if(opt_ltc_debug){
applog(LOG_ERR,
"[Speed] %dMhz core|chip|board: [%s/s], [%s/s], [%s/s], readcount:%d,bitnum:%d ",
info->chip_clk,info->core_hash,info->chip_hash,info->board_hash,info->read_count,info->chips_bit_num);
}
return true;
}
static void zeus_detect()
{
serial_detect(&zeus_drv, zeus_detect_one);
}
static bool zeus_prepare(struct thr_info *thr)
{
struct cgpu_info *zeus = thr->cgpu;
struct timeval now;
zeus->device_fd = -1;
int fd = zeus_open(zeus->device_path, zeus_info[zeus->device_id]->baud);
if (unlikely(-1 == fd)) {
applog(LOG_ERR, "Failed to open Zeus on %s",
zeus->device_path);
return false;
}
zeus->device_fd = fd;
applog(LOG_INFO, "Opened Zeus on %s", zeus->device_path);
cgtime(&now);
get_datestamp(zeus->init, &now);
return true;
}
void update_chip_stat(struct ZEUS_INFO *info,uint32_t nonce);
static int64_t zeus_scanhash(struct thr_info *thr, struct work *work,
__maybe_unused int64_t max_nonce)
{
struct cgpu_info *zeus;
int fd;
int ret;
struct ZEUS_INFO *info;
int numbytes = 84; // KRAMBLE 84 byte protocol
unsigned char ob_bin[84], nonce_bin[ZEUS_READ_SIZE];
char *ob_hex;
uint32_t nonce;
int64_t hash_count;
uint32_t mask;
struct timeval tv_start, tv_finish, elapsed;
int curr_hw_errors, i;
bool was_hw_error;
int64_t estimate_hashes;
elapsed.tv_sec = elapsed.tv_usec = 0;
zeus = thr->cgpu;
if (zeus->device_fd == -1)
if (!zeus_prepare(thr)) {
applog(LOG_ERR, "%s%i: Comms error", zeus->drv->name, zeus->device_id);
dev_error(zeus, REASON_DEV_COMMS_ERROR);
// fail the device if the reopen attempt fails
return -1;
}
fd = zeus->device_fd;
info = zeus_info[zeus->device_id];
uint32_t clock = info->clk_header;
int diff = floor(work->device_diff);
if(diff<info->chips_count){
diff=info->chips_count;
}
uint32_t target_me = 0xffff/diff;
uint32_t header = clock+target_me;
#if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
header = header;
#else
header = swab32(header);
#endif
memcpy(ob_bin,(uint8_t *)&header,4);
memcpy(&ob_bin[4], work->data, 80);
rev(ob_bin, 4);
rev(ob_bin+4, 80);
if (opt_ltc_debug&1) {
//ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
ob_hex = bin2hex(ob_bin, 8);
applog(LOG_ERR, "Zeus %d nounce2 = %s readcount = %d try sent: %s",
zeus->device_id,work->nonce2,info->read_count, ob_hex);
free(ob_hex);
}
//read(fd, flush_buf, 400);
flush_uart(fd);
ret = zeus_write(fd, ob_bin, 84);
if (ret) {
do_zeus_close(thr);
applog(LOG_ERR, "%s%i: Comms error", zeus->drv->name, zeus->device_id);
dev_error(zeus, REASON_DEV_COMMS_ERROR);
return 0; /* This should never happen */
}
cgtime(&tv_start);
/* Zeus will return 4 bytes (ZEUS_READ_SIZE) nonces or nothing */
memset(nonce_bin, 0, sizeof(nonce_bin));
if (opt_ltc_debug&0) {
applog(LOG_ERR, "diff is %d",diff);
}
uint32_t elapsed_count;
uint32_t read_count = info->read_count;
while(1){
ret = zeus_gets(nonce_bin, fd, &tv_finish, thr, read_count,&elapsed_count);
if (ret == ZEUS_GETS_ERROR) {
do_zeus_close(thr);
applog(LOG_ERR, "%s%i: Comms error", zeus->drv->name, zeus->device_id);
dev_error(zeus, REASON_DEV_COMMS_ERROR);
return 0;
}
#ifndef WIN32
//openwrt
// flush_uart(fd);
#endif
work->blk.nonce = 0xffffffff;
// aborted before becoming idle, get new work
if (ret == ZEUS_GETS_TIMEOUT || ret == ZEUS_GETS_RESTART) {
if (opt_ltc_debug&1) {
applog(LOG_ERR, "1restart or 2timeout:%d ",ret);
}
timersub(&tv_finish, &tv_start, &elapsed);
estimate_hashes = ((double)(elapsed.tv_sec) + ((double)(elapsed.tv_usec))/((double)1000000))
* info->golden_speed_percore*info->chips_count*info->cores_perchip;
if (unlikely(estimate_hashes > 0xffffffff))
estimate_hashes = 0xffffffff;
return estimate_hashes;
}
if(read_count>elapsed_count){
read_count -= elapsed_count;
}
else {
read_count=0;
}
memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
//rev(nonce_bin,4);
#if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
nonce = swab32(nonce);
#endif
curr_hw_errors = zeus->hw_errors;
submit_nonce(thr, work, nonce);
was_hw_error = (curr_hw_errors < zeus->hw_errors);
if (was_hw_error){
flush_uart(fd);
if (opt_ltc_debug&&1) {
applog(LOG_ERR, "ERR nonce:%08x ",nonce);
}
}
else {
if (opt_ltc_debug&&0) {
#if CHIP_GEN==1
uint32_t chip_index=get_revindex(nonce,info->chips_bit_num);
uint32_t core_index=(nonce&0xe0000000)>>29;
#else
#error
#endif
applog(LOG_ERR, "nonce:%08x,chip_index:%d ",nonce,chip_index);
}
}
}
}
static struct api_data *zeus_api_stats(struct cgpu_info *cgpu)
{
struct api_data *root = NULL;
struct ZEUS_INFO *info = zeus_info[cgpu->device_id];
// Warning, access to these is not locked - but we don't really
// care since hashing performance is way more important than
// locking access to displaying API debug 'stats'
// If locking becomes an issue for any of them, use copy_data=true also
root = api_add_string(root, "golden_speed_chip", info->chip_hash, false);
root = api_add_int(root, "chipclk", &(info->chip_clk), false);
root = api_add_int(root, "chips_count", &(info->chips_count), false);
root = api_add_int(root, "chips_count_max", &(info->chips_count_max), false);
root = api_add_uint32(root, "readcount", &(info->read_count), false);
return root;
}
static void zeus_shutdown(struct thr_info *thr)
{
do_zeus_close(thr);
}
struct device_drv zeus_drv = {
.drv_id = DRIVER_ZEUS,
.dname = "Zeus",
.name = "Zeus",
.max_diff = 32768,
.drv_detect = zeus_detect,
.get_api_stats = zeus_api_stats,
.thread_prepare = zeus_prepare,
.scanhash = zeus_scanhash,
.thread_shutdown = zeus_shutdown,
};