forked from aribere1/cgminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver-bitmain.c
3178 lines (2910 loc) · 97 KB
/
driver-bitmain.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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012-2013 Lingchao Xu <[email protected]>
* Copyright 2014-2015 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.
*/
#include "config.h"
#include "compat.h"
#include "miner.h"
#ifndef LINUX
#ifdef USE_ANT_S1
static void ants1_detect(__maybe_unused bool hotplug)
{
}
#endif
#ifdef USE_ANT_S2
#ifdef USE_ANT_S3
static void ants3_detect(__maybe_unused bool hotplug)
{
}
#else
static void ants2_detect(__maybe_unused bool hotplug)
{
}
#endif
#endif
#else // LINUX
#include "elist.h"
#if (defined(USE_ANT_S1) || defined(USE_ANT_S3))
#include "usbutils.h"
#else
#define C_BITMAIN_READ 0
#define C_BITMAIN_DATA_RXSTATUS 0
#endif
#include "driver-bitmain.h"
#include "hexdump.c"
#include "util.h"
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#ifdef USE_ANT_S1
#define ANTDRV ants1_drv
#else
#ifdef USE_ANT_S3
#define ANTDRV ants3_drv
#else
#define ANTDRV ants2_drv
#endif
#endif
#define BITMAIN_CALC_DIFF1 1
bool opt_bitmain_hwerror = false;
#ifdef USE_ANT_S2
bool opt_bitmain_checkall = false;
bool opt_bitmain_checkn2diff = false;
bool opt_bitmain_beeper = false;
bool opt_bitmain_tempoverctrl = false;
#endif
int opt_bitmain_temp = BITMAIN_TEMP_TARGET;
int opt_bitmain_workdelay = BITMAIN_WORK_DELAY;
int opt_bitmain_overheat = BITMAIN_TEMP_OVERHEAT;
int opt_bitmain_fan_min = BITMAIN_DEFAULT_FAN_MIN_PWM;
int opt_bitmain_fan_max = BITMAIN_DEFAULT_FAN_MAX_PWM;
bool opt_bitmain_auto;
static int option_offset = -1;
#if (defined(USE_ANT_S1) || defined(USE_ANT_S3))
static unsigned char bit_swap_table[256] =
{
0x00, 0x80, 0x40, 0xc0, 0x20, 0x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, 0xfd,
0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
};
#define bitswap(x) (bit_swap_table[x])
#else
#define bitswap(x) (x)
#endif
// --------------------------------------------------------------
// CRC16 check table
// --------------------------------------------------------------
const uint8_t chCRCHTalbe[] = // CRC high byte table
{
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40
};
const uint8_t chCRCLTalbe[] = // CRC low byte table
{
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 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A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
0x41, 0x81, 0x80, 0x40
};
static uint16_t CRC16(const uint8_t* p_data, uint16_t w_len)
{
uint8_t chCRCHi = 0xFF; // CRC high byte initialize
uint8_t chCRCLo = 0xFF; // CRC low byte initialize
uint16_t wIndex = 0; // CRC cycling index
while (w_len--) {
wIndex = chCRCLo ^ *p_data++;
chCRCLo = chCRCHi ^ chCRCHTalbe[wIndex];
chCRCHi = chCRCLTalbe[wIndex];
}
return ((chCRCHi << 8) | chCRCLo);
}
static uint32_t num2bit(int num)
{
if (num < 0 || num > 31)
return 0;
else
return (((uint32_t)1) << (31 - num));
}
#ifdef USE_ANT_S1
static bool get_options(int this_option_offset, int *baud, int *chain_num,
int *asic_num, int *timeout, int *frequency,
uint8_t *reg_data)
{
char buf[BUFSIZ+1];
char *ptr, *comma, *colon, *colon2, *colon3, *colon4, *colon5;
size_t max;
int tmp;
if (opt_bitmain_options == NULL)
buf[0] = '\0';
else {
// Always use the first set if more than one
ptr = opt_bitmain_options;
comma = strchr(ptr, ',');
if (comma)
*comma = '\0';
max = strlen(ptr);
if (max > BUFSIZ)
max = BUFSIZ;
strncpy(buf, ptr, max);
buf[max] = '\0';
}
if (!(*buf))
return false;
colon = strchr(buf, ':');
if (colon)
*(colon++) = '\0';
tmp = atoi(buf);
switch (tmp) {
case 115200:
*baud = 115200;
break;
case 57600:
*baud = 57600;
break;
case 38400:
*baud = 38400;
break;
case 19200:
*baud = 19200;
break;
default:
quit(1, "Invalid bitmain-options for baud (%s) "
"must be 115200, 57600, 38400 or 19200", buf);
}
if (colon && *colon) {
colon2 = strchr(colon, ':');
if (colon2)
*(colon2++) = '\0';
if (*colon) {
tmp = atoi(colon);
if (tmp > 0)
*chain_num = tmp;
else {
quit(1, "Invalid bitmain-options for "
"chain_num (%s) must be 1 ~ %d",
colon, BITMAIN_DEFAULT_CHAIN_NUM);
}
}
if (colon2 && *colon2) {
colon3 = strchr(colon2, ':');
if (colon3)
*(colon3++) = '\0';
tmp = atoi(colon2);
if (tmp > 0 && tmp <= BITMAIN_DEFAULT_ASIC_NUM)
*asic_num = tmp;
else {
quit(1, "Invalid bitmain-options for "
"asic_num (%s) must be 1 ~ %d",
colon2, BITMAIN_DEFAULT_ASIC_NUM);
}
if (colon3 && *colon3) {
colon4 = strchr(colon3, ':');
if (colon4)
*(colon4++) = '\0';
tmp = atoi(colon3);
if (tmp > 0 && tmp <= 0xff)
*timeout = tmp;
else {
quit(1, "Invalid bitmain-options for "
"timeout (%s) must be 1 ~ %d",
colon3, 0xff);
}
if (colon4 && *colon4) {
colon5 = strchr(colon4, ':');
if (colon5)
*(colon5++) = '\0';
tmp = atoi(colon4);
if (tmp < BITMAIN_MIN_FREQUENCY ||
tmp > BITMAIN_MAX_FREQUENCY) {
quit(1, "Invalid bitmain-options for frequency,"
" must be %d <= frequency <= %d",
BITMAIN_MIN_FREQUENCY,
BITMAIN_MAX_FREQUENCY);
} else
*frequency = tmp;
if (colon5 && *colon5) {
if (strlen(colon5) > 8 ||
strlen(colon5)%2 != 0 ||
strlen(colon5)/2 == 0) {
quit(1, "Invalid bitmain-options for"
" reg data, must be hex now: %s",
colon5);
}
memset(reg_data, 0, 4);
if (!hex2bin(reg_data, colon5, strlen(colon5)/2)) {
quit(1, "Invalid bitmain-options for reg"
" data, hex2bin error now: %s",
colon5);
}
}
}
}
}
}
return true;
}
#else
static bool get_options(__maybe_unused int this_option_offset, int *baud,
int *chain_num, int *asic_num)
{
char buf[BUFSIZ+1];
char *ptr, *comma, *colon, *colon2, *colon3;
size_t max;
int tmp;
if (opt_bitmain_options == NULL)
buf[0] = '\0';
else {
// Always use the first set if more than one
ptr = opt_bitmain_options;
comma = strchr(ptr, ',');
if (comma)
*comma = '\0';
max = strlen(ptr);
if (max > BUFSIZ)
max = BUFSIZ;
strncpy(buf, ptr, max);
buf[max] = '\0';
}
if (!(*buf))
return false;
colon = strchr(buf, ':');
if (colon)
*(colon++) = '\0';
tmp = atoi(buf);
switch (tmp) {
case 115200:
*baud = 115200;
break;
case 57600:
*baud = 57600;
break;
case 38400:
*baud = 38400;
break;
case 19200:
*baud = 19200;
break;
default:
quit(1, "Invalid bitmain-options for baud (%s) "
"must be 115200, 57600, 38400 or 19200", buf);
}
if (colon && *colon) {
colon2 = strchr(colon, ':');
if (colon2)
*(colon2++) = '\0';
if (*colon) {
tmp = atoi(colon);
if (tmp > 0)
*chain_num = tmp;
else {
quit(1, "Invalid bitmain-options for "
"chain_num (%s) must be 1 ~ %d",
colon, BITMAIN_DEFAULT_CHAIN_NUM);
}
}
if (colon2 && *colon2) {
colon3 = strchr(colon2, ':');
if (colon3)
*(colon3++) = '\0';
tmp = atoi(colon2);
if (tmp > 0 && tmp <= BITMAIN_DEFAULT_ASIC_NUM)
*asic_num = tmp;
else {
quit(1, "Invalid bitmain-options for "
"asic_num (%s) must be 1 ~ %d",
colon2, BITMAIN_DEFAULT_ASIC_NUM);
}
}
}
return true;
}
#endif
#ifdef USE_ANT_S1
static int bitmain_set_txconfig(struct bitmain_txconfig_token *bm,
uint8_t reset, uint8_t fan_eft, uint8_t timeout_eft, uint8_t frequency_eft,
uint8_t voltage_eft, uint8_t chain_check_time_eft, uint8_t chip_config_eft,
uint8_t hw_error_eft, uint8_t chain_num, uint8_t asic_num,
uint8_t fan_pwm_data, uint8_t timeout_data,
uint16_t frequency, uint8_t voltage, uint8_t chain_check_time,
uint8_t chip_address, uint8_t reg_address, uint8_t * reg_data)
#else
static int bitmain_set_txconfig(struct bitmain_txconfig_token *bm,
uint8_t reset, uint8_t fan_eft, uint8_t timeout_eft, uint8_t frequency_eft,
uint8_t voltage_eft, uint8_t chain_check_time_eft, uint8_t chip_config_eft,
uint8_t hw_error_eft, uint8_t beeper_ctrl, uint8_t temp_over_ctrl,
uint8_t chain_num, uint8_t asic_num,
uint8_t fan_pwm_data, uint8_t timeout_data,
uint16_t frequency, uint8_t *voltage, uint8_t chain_check_time,
uint8_t chip_address, uint8_t reg_address, uint8_t * reg_data)
#endif
{
uint16_t crc = 0;
int datalen = 0;
#ifdef USE_ANT_S2
uint8_t version = 0;
#endif
uint8_t *sendbuf = (uint8_t *)bm;
if (unlikely(!bm)) {
applog(LOG_WARNING, "%s: %s() bm is null", ANTDRV.dname, __func__);
return -1;
}
if (unlikely(timeout_data <= 0 || asic_num <= 0 || chain_num <= 0)) {
applog(LOG_WARNING, "%s: %s() parameter invalid"
" timeout_data(%d) asic_num(%d) chain_num(%d)",
ANTDRV.dname, __func__,
(int)timeout_data, (int)asic_num, (int)chain_num);
return -1;
}
datalen = sizeof(struct bitmain_txconfig_token);
memset(bm, 0, datalen);
bm->token_type = BITMAIN_TOKEN_TYPE_TXCONFIG;
#ifdef USE_ANT_S1
bm->length = datalen-2;
#else
bm->version = version;
bm->length = datalen-4;
bm->length = htole16(bm->length);
#endif
bm->reset = reset;
bm->fan_eft = fan_eft;
bm->timeout_eft = timeout_eft;
bm->frequency_eft = frequency_eft;
bm->voltage_eft = voltage_eft;
bm->chain_check_time_eft = chain_check_time_eft;
bm->chip_config_eft = chip_config_eft;
bm->hw_error_eft = hw_error_eft;
#ifdef USE_ANT_S1
sendbuf[2] = bitswap(sendbuf[2]);
#else
bm->beeper_ctrl = beeper_ctrl;
bm->temp_over_ctrl = temp_over_ctrl;
sendbuf[4] = bitswap(sendbuf[4]);
sendbuf[5] = bitswap(sendbuf[5]);
#endif
bm->chain_num = chain_num;
bm->asic_num = asic_num;
bm->fan_pwm_data = fan_pwm_data;
bm->timeout_data = timeout_data;
bm->frequency = htole16(frequency);
#ifdef USE_ANT_S1
bm->voltage = voltage;
#else
bm->voltage[0] = voltage[0];
bm->voltage[1] = voltage[1];
#endif
bm->chain_check_time = chain_check_time;
memcpy(bm->reg_data, reg_data, 4);
bm->chip_address = chip_address;
bm->reg_address = reg_address;
crc = CRC16((uint8_t *)bm, datalen-2);
bm->crc = htole16(crc);
#ifdef USE_ANT_S1
applogsiz(LOG_DEBUG, 512, "%s: %s() reset(%d) faneft(%d) touteft(%d) freqeft(%d)"
" volteft(%d) chainceft(%d) chipceft(%d) hweft(%d) mnum(%d)"
" anum(%d) fanpwmdata(%d) toutdata(%d) freq(%d) volt(%d)"
" chainctime(%d) regdata(%02x%02x%02x%02x) chipaddr(%02x)"
" regaddr(%02x) crc(%04x)",
ANTDRV.dname, __func__,
(int)reset, (int)fan_eft, (int)timeout_eft, (int)frequency_eft,
(int)voltage_eft, (int)chain_check_time_eft, (int)chip_config_eft,
(int)hw_error_eft, (int)chain_num, (int)asic_num, (int)fan_pwm_data,
(int)timeout_data, (int)frequency, (int)voltage, (int)chain_check_time,
(int)reg_data[0], (int)reg_data[1], (int)reg_data[2], (int)reg_data[3],
(int)chip_address, (int)reg_address, (int)crc);
#else
applogsiz(LOG_DEBUG, 512, "%s: %s() v(%d) reset(%d) faneft(%d) touteft(%d) freqeft(%d)"
" volteft(%d) chainceft(%d) chipceft(%d) hweft(%d)"
" beepctrl(%d) toverctl(%d) mnum(%d)"
" anum(%d) fanpwmdata(%d) toutdata(%d) freq(%d) volt(%02x%02x)"
" chainctime(%d) regdata(%02x%02x%02x%02x) chipaddr(%02x)"
" regaddr(%02x) crc(%04x)",
ANTDRV.dname, __func__,
(int)version, (int)reset, (int)fan_eft, (int)timeout_eft,
(int)frequency_eft, (int)voltage_eft, (int)chain_check_time_eft,
(int)chip_config_eft, (int)hw_error_eft, (int)beeper_ctrl,
(int)temp_over_ctrl, (int)chain_num, (int)asic_num, (int)fan_pwm_data,
(int)timeout_data, (int)frequency, (int)voltage[0], (int)voltage[1],
(int)chain_check_time, (int)reg_data[0], (int)reg_data[1],
(int)reg_data[2], (int)reg_data[3], (int)chip_address,
(int)reg_address, (int)crc);
#endif
return datalen;
}
static int bitmain_set_txtask(struct bitmain_info *info, uint8_t *sendbuf,
unsigned int *last_work_block, int *sentcount)
{
uint16_t crc = 0;
uint32_t wid = 0;
int datalen = 0;
uint8_t new_block = 0;
//char *ob_hex = NULL;
struct bitmain_txtask_token *bm = (struct bitmain_txtask_token *)sendbuf;
int cursentcount = 0;
#ifdef USE_ANT_S2
uint8_t version = 0;
int diffbits, lowestdiffbits = -1;
double workdiff;
#endif
K_ITEM *witem;
*sentcount = 0;
if (unlikely(!bm)) {
applog(LOG_WARNING, "%s: %s() bm is null", ANTDRV.dname, __func__);
return -1;
}
memset(bm, 0, sizeof(struct bitmain_txtask_token));
bm->token_type = BITMAIN_TOKEN_TYPE_TXTASK;
#ifdef USE_ANT_S2
bm->version = version;
if (info->wbuild->head)
quithere(1, "%s: %s() wbuild wasn't empty", ANTDRV.dname, __func__);
#endif
datalen = 10;
applog(LOG_DEBUG, "%s: send work count %d", ANTDRV.dname, info->work_ready->count);
while (info->work_ready->count) {
witem = k_unlink_tail(info->work_ready);
if (DATAW(witem)->work->work_block > *last_work_block) {
applog(LOG_ERR, "%s: send task new block %d old(%d)",
ANTDRV.dname,
DATAW(witem)->work->work_block, *last_work_block);
new_block = 1;
*last_work_block = DATAW(witem)->work->work_block;
}
wid = DATAW(witem)->wid;
bm->works[cursentcount].work_id = htole32(wid);
applog(LOG_DEBUG, "%s: send task work id:%"PRIu32" %"PRIu32,
ANTDRV.dname,
wid, bm->works[cursentcount].work_id);
memcpy(bm->works[cursentcount].midstate, DATAW(witem)->work->midstate, 32);
memcpy(bm->works[cursentcount].data2, DATAW(witem)->work->data + 64, 12);
cursentcount++;
#ifdef USE_ANT_S1
k_add_head(info->work_list, witem);
#else
k_add_head(info->wbuild, witem);
diffbits = (int)floor(log2(DATAW(witem)->work->sdiff));
if (diffbits < 0)
diffbits = 0;
// Limit to 4096 so solo mining has reasonable mining stats
if (diffbits > 12)
diffbits = 12;
// Must use diffbits <= all work being sent
if (lowestdiffbits == -1 || lowestdiffbits > diffbits)
lowestdiffbits = diffbits;
#endif
}
if (cursentcount <= 0) {
applog(LOG_ERR, "%s: send work count %d", ANTDRV.dname, cursentcount);
return 0;
}
#ifdef USE_ANT_S2
workdiff = pow(2.0, (double)lowestdiffbits);
witem = info->wbuild->head;
while (witem) {
DATAW(witem)->work->device_diff = workdiff;
witem = witem->next;
}
k_list_transfer_to_head(info->wbuild, info->work_list);
#endif
datalen += 48*cursentcount;
bm->length = datalen-4;
bm->length = htole16(bm->length);
//len = datalen-3;
//len = htole16(len);
//memcpy(sendbuf+1, &len, 2);
bm->new_block = new_block;
#ifdef USE_ANT_S2
bm->diff = lowestdiffbits;
#endif
sendbuf[4] = bitswap(sendbuf[4]);
applog(LOG_DEBUG, "%s: TxTask Token: %d %d %02x%02x%02x%02x%02x%02x",
ANTDRV.dname,
datalen, bm->length,
sendbuf[0], sendbuf[1], sendbuf[2],
sendbuf[3], sendbuf[4], sendbuf[5]);
*sentcount = cursentcount;
crc = CRC16(sendbuf, datalen-2);
crc = htole16(crc);
memcpy(sendbuf+datalen-2, &crc, 2);
#ifdef USE_ANT_S1
applog(LOG_DEBUG, "%s: TxTask Token: new_block(%d) work_num(%d)"
" crc(%04x)",
ANTDRV.dname,
(int)new_block, cursentcount, (int)crc);
#else
applog(LOG_DEBUG, "%s: TxTask Token: v(%d) new_block(%d)"
" diff(%d work:%f) work_num(%d) crc(%04x)",
ANTDRV.dname,
(int)version, (int)new_block, lowestdiffbits, workdiff,
cursentcount, (int)crc);
#endif
applog(LOG_DEBUG, "%s: TxTask Token: %d %d %02x%02x%02x%02x%02x%02x",
ANTDRV.dname,
datalen, bm->length,
sendbuf[0], sendbuf[1], sendbuf[2],
sendbuf[3], sendbuf[4], sendbuf[5]);
return datalen;
}
static int bitmain_set_rxstatus(struct bitmain_rxstatus_token *bm,
uint8_t chip_status_eft, uint8_t detect_get, uint8_t chip_address, uint8_t reg_address)
{
uint16_t crc = 0;
int datalen = 0;
uint8_t *sendbuf = (uint8_t *)bm;
#ifdef USE_ANT_S2
uint8_t version = 0;
#endif
if (unlikely(!bm)) {
applog(LOG_WARNING, "%s: %s() bm is null", ANTDRV.dname, __func__);
return -1;
}
datalen = sizeof(struct bitmain_rxstatus_token);
memset(bm, 0, datalen);
bm->token_type = BITMAIN_TOKEN_TYPE_RXSTATUS;
#ifdef USE_ANT_S1
bm->length = datalen-2;
#else
bm->version = version;
bm->length = datalen-4;
bm->length = htole16(bm->length);
#endif
bm->chip_status_eft = chip_status_eft;
bm->detect_get = detect_get;
#ifdef USE_ANT_S1
sendbuf[2] = bitswap(sendbuf[2]);
#else
sendbuf[4] = bitswap(sendbuf[4]);
#endif
bm->chip_address = chip_address;
bm->reg_address = reg_address;
crc = CRC16((uint8_t *)bm, datalen-2);
bm->crc = htole16(crc);
#ifdef USE_ANT_S1
applog(LOG_DEBUG, "%s: RxStatus Token: chip_status_eft(%d) detect_get(%d)"
" chip_address(%02x) reg_address(%02x) crc(%04x)",
ANTDRV.dname,
(int)chip_status_eft, (int)detect_get, chip_address, reg_address, crc);
#else
applog(LOG_DEBUG, "%s: RxStatus Token: v(%d) chip_status_eft(%d) detect_get(%d)"
" chip_address(%02x) reg_address(%02x) crc(%04x)",
ANTDRV.dname,
(int)version, (int)chip_status_eft, (int)detect_get,
chip_address, reg_address, crc);
#endif
return datalen;
}
static int bitmain_parse_rxstatus(const uint8_t * data, int datalen, struct bitmain_rxstatus_data *bm)
{
uint16_t crc = 0;
int i = 0;
#ifdef USE_ANT_S2
uint8_t version = 0;
int j = 0;
int asic_num = 0;
int dataindex = 0;
#endif
if (unlikely(!bm)) {
applog(LOG_ERR, "%s: %s() bm is null", ANTDRV.dname, __func__);
return -1;
}
if (unlikely(!data || datalen <= 0)) {
applog(LOG_ERR, "%s: %s() parameter invalid data is null"
" or datalen(%d) error",
ANTDRV.dname, __func__, datalen);
return -1;
}
#ifdef USE_ANT_S1
memcpy(bm, data, sizeof(struct bitmain_rxstatus_data));
if (bm->data_type != BITMAIN_DATA_TYPE_RXSTATUS) {
applog(LOG_ERR, "%s: %s() datatype(%02x) error",
ANTDRV.dname, __func__,
bm->data_type);
return -1;
}
if (bm->length+2 != datalen) {
applog(LOG_ERR, "%s: %s() length(%d) datalen(%d) error",
ANTDRV.dname, __func__,
bm->length, datalen);
return -1;
}
crc = CRC16(data, datalen-2);
memcpy(&(bm->crc), data+datalen-2, 2);
bm->crc = htole16(bm->crc);
if (crc != bm->crc) {
applog(LOG_ERR, "%s: %s() check crc(%d)"
" != bm crc(%d) datalen(%d)",
ANTDRV.dname, __func__,
crc, bm->crc, datalen);
return -1;
}
bm->fifo_space = htole32(bm->fifo_space);
bm->nonce_error = htole32(bm->nonce_error);
if (bm->chain_num*5 + bm->temp_num + bm->fan_num + 22 != datalen) {
applog(LOG_ERR, "%s: %s() chain_num(%d) temp_num(%d)"
" fan_num(%d) not match datalen(%d)",
ANTDRV.dname, __func__,
bm->chain_num, bm->temp_num, bm->fan_num, datalen);
return -1;
}
if (bm->chain_num > BITMAIN_MAX_CHAIN_NUM) {
applog(LOG_ERR, "%s: %s() chain_num=%d error",
ANTDRV.dname, __func__,
bm->chain_num);
return -1;
}
if (bm->chain_num > 0) {
memcpy(bm->chain_asic_status, data+20, bm->chain_num*4);
memcpy(bm->chain_asic_num, data+20+bm->chain_num*4, bm->chain_num);
}
for (i = 0; i < bm->chain_num; i++)
bm->chain_asic_status[i] = htole32(bm->chain_asic_status[i]);
if (bm->temp_num > 0)
memcpy(bm->temp, data+20+bm->chain_num*5, bm->temp_num);
if (bm->fan_num > 0)
memcpy(bm->fan, data+20+bm->chain_num*5+bm->temp_num, bm->fan_num);
applog(LOG_DEBUG, "%s: RxStatus Data chipvalueeft(%d) version(%d) fifospace(%d)"
" regvalue(%d) chainnum(%d) tempnum(%d) fannum(%d) crc(%04x)",
ANTDRV.dname,
bm->chip_value_eft, bm->version, bm->fifo_space, bm->reg_value,
bm->chain_num, bm->temp_num, bm->fan_num, bm->crc);
applog(LOG_DEBUG, "%s: RxStatus Data chain info:", ANTDRV.dname);
for (i = 0; i < bm->chain_num; i++) {
applog(LOG_DEBUG, "%s: RxStatus Data chain(%d) asic num=%d asic_status=%08x",
ANTDRV.dname,
i+1, bm->chain_asic_num[i], bm->chain_asic_status[i]);
}
#else // USE_ANT_S2
memset(bm, 0, sizeof(struct bitmain_rxstatus_data));
memcpy(bm, data, 28);
if (bm->data_type != BITMAIN_DATA_TYPE_RXSTATUS) {
applog(LOG_ERR, "%s: %s() datatype(%02x) error",
ANTDRV.dname, __func__,
bm->data_type);
return -1;
}
if (bm->version != version) {
applog(LOG_ERR, "%s: %s() version(%02x) error",
ANTDRV.dname, __func__,
bm->version);
return -1;
}
bm->length = htole16(bm->length);
if (bm->length+4 != datalen) {
applog(LOG_ERR, "%s: %s() length(%d) datalen(%d) error",
ANTDRV.dname, __func__,
bm->length, datalen);
return -1;
}
crc = CRC16(data, datalen-2);
memcpy(&(bm->crc), data+datalen-2, 2);
bm->crc = htole16(bm->crc);
if (crc != bm->crc) {
applog(LOG_ERR, "%s: %s() check crc(%d)"
" != bm crc(%d) datalen(%d)",
ANTDRV.dname, __func__,
crc, bm->crc, datalen);
return -1;
}
bm->fifo_space = htole16(bm->fifo_space);
bm->fan_exist = htole16(bm->fan_exist);
bm->temp_exist = htole32(bm->temp_exist);
bm->nonce_error = htole32(bm->nonce_error);
if (bm->chain_num > BITMAIN_MAX_CHAIN_NUM) {
applog(LOG_ERR, "%s: %s() chain_num=%d error",
ANTDRV.dname, __func__,
bm->chain_num);
return -1;
}
dataindex = 28;
if (bm->chain_num > 0) {
memcpy(bm->chain_asic_num,
data+datalen-2-bm->chain_num-bm->temp_num-bm->fan_num,
bm->chain_num);
}
for (i = 0; i < bm->chain_num; i++) {
asic_num = bm->chain_asic_num[i];
if (asic_num < 0)
asic_num = 1;
else {
if (asic_num % 32 == 0)
asic_num = asic_num / 32;
else
asic_num = asic_num / 32 + 1;
}
memcpy((uint8_t *)bm->chain_asic_exist+i*32, data+dataindex, asic_num*4);
dataindex += asic_num*4;
}
for(i = 0; i < bm->chain_num; i++) {
asic_num = bm->chain_asic_num[i];
if (asic_num < 0)
asic_num = 1;
else {
if (asic_num % 32 == 0)
asic_num = asic_num / 32;
else
asic_num = asic_num / 32 + 1;
}
memcpy((uint8_t *)bm->chain_asic_status+i*32, data+dataindex, asic_num*4);
dataindex += asic_num*4;
}
dataindex += bm->chain_num;
if ((dataindex + bm->temp_num + bm->fan_num + 2) != datalen) {
applog(LOG_ERR, "%s: %s() dataindex(%d) chain_num(%d) temp_num(%d)"
" fan_num(%d) not match datalen(%d)",
ANTDRV.dname, __func__,
dataindex, bm->chain_num, bm->temp_num, bm->fan_num, datalen);
return -1;
}
for (i = 0; i < bm->chain_num; i++) {
for (j = 0; j < 8; j++) {
bm->chain_asic_exist[i*8+j] = htole32(bm->chain_asic_exist[i*8+j]);
bm->chain_asic_status[i*8+j] = htole32(bm->chain_asic_status[i*8+j]);
}
}
if (bm->temp_num > 0) {
memcpy(bm->temp, data+dataindex, bm->temp_num);
dataindex += bm->temp_num;
}
if (bm->fan_num > 0) {
memcpy(bm->fan, data+dataindex, bm->fan_num);
dataindex += bm->fan_num;
}
applog(LOG_DEBUG, "%s: RxStatus Data chipv_e(%d) chainnum(%d) fifos(%d)"
" v1(%d) v2(%d) v3(%d) v4(%d) fann(%d) tempn(%d) fanet(%04x)"
" tempet(%08x) ne(%d) regvalue(%d) crc(%04x)",
ANTDRV.dname,
bm->chip_value_eft, bm->chain_num, bm->fifo_space,
bm->hw_version[0], bm->hw_version[1], bm->hw_version[2],
bm->hw_version[3], bm->fan_num, bm->temp_num, bm->fan_exist,
bm->temp_exist, bm->nonce_error, bm->reg_value, bm->crc);
applog(LOG_DEBUG, "%s: RxStatus Data chain info:", ANTDRV.dname);
for (i = 0; i < bm->chain_num; i++) {
applog(LOG_DEBUG, "%s: RxStatus Data chain(%d) asic num=%d asic_exists=%08x"
" asic_status=%08x",
ANTDRV.dname,
i+1, bm->chain_asic_num[i], bm->chain_asic_exist[i*8],
bm->chain_asic_status[i*8]);
}
#endif
applog(LOG_DEBUG, "%s: RxStatus Data temp info:", ANTDRV.dname);
for (i = 0; i < bm->temp_num; i++) {
applog(LOG_DEBUG, "%s: RxStatus Data temp(%d) temp=%d",
ANTDRV.dname,
i+1, bm->temp[i]);
}
applog(LOG_DEBUG, "%s: RxStatus Data fan info:", ANTDRV.dname);
for (i = 0; i < bm->fan_num; i++) {
applog(LOG_DEBUG, "%s: RxStatus Data fan(%d) fan=%d",
ANTDRV.dname,
i+1, bm->fan[i]);
}
return 0;
}
static int bitmain_parse_rxnonce(const uint8_t * data, int datalen, struct bitmain_rxnonce_data *bm, int * nonce_num)
{
int i = 0;
uint16_t crc = 0;
#ifdef USE_ANT_S2
uint8_t version = 0;
#endif
int curnoncenum = 0;
if (unlikely(!bm)) {
applog(LOG_ERR, "%s: %s() bm is null", ANTDRV.dname, __func__);
return -1;
}
if (unlikely(!data || datalen <= 0)) {
applog(LOG_ERR, "%s: %s() parameter invalid data is null"
" or datalen(%d) error",
ANTDRV.dname, __func__, datalen);
return -1;
}
memcpy(bm, data, sizeof(struct bitmain_rxnonce_data));
if (bm->data_type != BITMAIN_DATA_TYPE_RXNONCE) {
applog(LOG_ERR, "%s: %s() datatype(%02x) error",
ANTDRV.dname, __func__,
bm->data_type);
return -1;
}
#ifdef USE_ANT_S1
if (bm->length+2 != datalen) {
applog(LOG_ERR, "%s: %s() length(%d) error",
ANTDRV.dname, __func__,
bm->length);
return -1;
}
#else
if (bm->version != version) {
applog(LOG_ERR, "%s: %s() version(%02x) error",
ANTDRV.dname, __func__,
bm->version);
return -1;
}
bm->length = htole16(bm->length);
if (bm->length+4 != datalen) {
applog(LOG_ERR, "%s: %s() length(%d) datalen(%d) error",
ANTDRV.dname, __func__,
bm->length, datalen);
return -1;
}
#endif
crc = CRC16(data, datalen-2);
memcpy(&(bm->crc), data+datalen-2, 2);
bm->crc = htole16(bm->crc);
if (crc != bm->crc) {
applog(LOG_ERR, "%s: %s() check crc(%d)"
" != bm crc(%d) datalen(%d)",
ANTDRV.dname, __func__,
crc, bm->crc, datalen);
return -1;
}
#ifdef USE_ANT_S1
curnoncenum = (datalen-4)/8;
#else
bm->fifo_space = htole16(bm->fifo_space);
bm->diff = htole16(bm->diff);
bm->total_nonce_num = htole64(bm->total_nonce_num);
curnoncenum = (datalen-14)/8;