forked from luke-jr/bfgminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver-bitmain.c
1873 lines (1668 loc) · 68.1 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-2014 Lingchao Xu
* Copyright 2015 Luke Dashjr
*
* 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 <ctype.h>
#include <limits.h>
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#ifndef WIN32
#include <sys/select.h>
#include <termios.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#else
#include "compat.h"
#include <windows.h>
#include <io.h>
#endif
#include <curl/curl.h>
#include <uthash.h>
#include "deviceapi.h"
#include "miner.h"
#include "driver-bitmain.h"
#include "lowl-vcom.h"
#include "util.h"
const bool opt_bitmain_hwerror = true;
const unsigned bitmain_poll_interval_us = 10000;
BFG_REGISTER_DRIVER(bitmain_drv)
static const struct bfg_set_device_definition bitmain_set_device_funcs_init[];
#define htole8(x) (x)
#define BITMAIN_USING_CURL -2
static
struct cgpu_info *btm_alloc_cgpu(struct device_drv *drv, int threads)
{
struct cgpu_info *cgpu = calloc(1, sizeof(*cgpu));
if (unlikely(!cgpu))
quit(1, "Failed to calloc cgpu for %s in usb_alloc_cgpu", drv->dname);
cgpu->drv = drv;
cgpu->deven = DEV_ENABLED;
cgpu->threads = threads;
cgpu->device_fd = -1;
struct bitmain_info *info = malloc(sizeof(*info));
if (unlikely(!info))
quit(1, "Failed to calloc bitmain_info data");
cgpu->device_data = info;
*info = (struct bitmain_info){
.chain_num = BITMAIN_DEFAULT_CHAIN_NUM,
.asic_num = BITMAIN_DEFAULT_ASIC_NUM,
.timeout = BITMAIN_DEFAULT_TIMEOUT,
.frequency = BITMAIN_DEFAULT_FREQUENCY,
.voltage[0] = BITMAIN_DEFAULT_VOLTAGE0,
.voltage[1] = BITMAIN_DEFAULT_VOLTAGE1,
.packet_max_nonce = BITMAIN_MAX_PACKET_MAX_NONCE,
.diff = 255,
.lowest_goal_diff = 255,
.work_restart = true,
};
sprintf(info->frequency_t, "%d", BITMAIN_DEFAULT_FREQUENCY),
strcpy(info->voltage_t, BITMAIN_DEFAULT_VOLTAGE_T);
return cgpu;
}
static curl_socket_t bitmain_grab_socket_opensocket_cb(void *clientp, __maybe_unused curlsocktype purpose, struct curl_sockaddr *addr)
{
struct bitmain_info * const info = clientp;
curl_socket_t sck = bfg_socket(addr->family, addr->socktype, addr->protocol);
info->curl_sock = sck;
return sck;
}
static
bool btm_init(struct cgpu_info *cgpu, const char * devpath)
{
applog(LOG_DEBUG, "btm_init cgpu->device_fd=%d", cgpu->device_fd);
int fd = -1;
if(cgpu->device_fd >= 0) {
return false;
}
struct bitmain_info *info = cgpu->device_data;
if (!strncmp(devpath, "ip:", 3)) {
CURL *curl = curl_easy_init();
if (!curl)
applogr(false, LOG_ERR, "%s: curl_easy_init failed", cgpu->drv->dname);
// CURLINFO_LASTSOCKET is broken on Win64 (which has a wider SOCKET type than curl_easy_getinfo returns), so we use this hack for now
info->curl_sock = -1;
curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, bitmain_grab_socket_opensocket_cb);
curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, info);
curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1);
curl_easy_setopt(curl, CURLOPT_URL, &devpath[3]);
if (curl_easy_perform(curl)) {
curl_easy_cleanup(curl);
applogr(false, LOG_ERR, "%s: curl_easy_perform failed for %s", cgpu->drv->dname, &devpath[3]);
}
cgpu->device_path = strdup(devpath);
cgpu->device_fd = BITMAIN_USING_CURL;
info->device_curl = curl;
return true;
}
fd = serial_open(devpath, 0, 1, false);
if(fd == -1) {
applog(LOG_DEBUG, "%s open %s error %d",
cgpu->drv->dname, devpath, errno);
return false;
}
cgpu->device_path = strdup(devpath);
cgpu->device_fd = fd;
applog(LOG_DEBUG, "btm_init open device fd = %d", cgpu->device_fd);
return true;
}
static
void btm_uninit(struct cgpu_info *cgpu)
{
struct bitmain_info * const info = cgpu->device_data;
applog(LOG_DEBUG, "BTM uninit %s%i", cgpu->drv->name, cgpu->device_fd);
// May have happened already during a failed initialisation
// if release_cgpu() was called due to a USB NODEV(err)
if (cgpu->device_fd >= 0) {
serial_close(cgpu->device_fd);
cgpu->device_fd = -1;
}
if (info->device_curl) {
curl_easy_cleanup(info->device_curl);
info->device_curl = NULL;
}
if(cgpu->device_path) {
free((char*)cgpu->device_path);
cgpu->device_path = NULL;
}
}
bool bitmain_curl_all(const bool is_recv, const int fd, CURL * const curl, void *p, size_t remsz)
{
CURLcode (* const func)(CURL *, void *, size_t, size_t *) = is_recv ? (void*)curl_easy_recv : (void*)curl_easy_send;
CURLcode r;
size_t sz;
while (remsz) {
fd_set otherfds, thisfds;
FD_ZERO(&otherfds);
FD_ZERO(&thisfds);
FD_SET(fd, &thisfds);
select(fd + 1, is_recv ? &thisfds : &otherfds, is_recv ? &otherfds : &thisfds, &thisfds, NULL);
r = func(curl, p, remsz, &sz);
switch (r) {
case CURLE_OK:
remsz -= sz;
p += sz;
break;
case CURLE_AGAIN:
break;
default:
return false;
}
}
return true;
}
static
int btm_read(struct cgpu_info * const cgpu, void * const buf, const size_t bufsize)
{
int err = 0;
//applog(LOG_DEBUG, "btm_read ----- %d -----", bufsize);
if (unlikely(cgpu->device_fd == BITMAIN_USING_CURL)) {
struct bitmain_info * const info = cgpu->device_data;
uint8_t headbuf[5];
headbuf[0] = 0;
pk_u32be(headbuf, 1, bufsize);
if (!bitmain_curl_all(false, info->curl_sock, info->device_curl, headbuf, sizeof(headbuf)))
return -1;
if (!bitmain_curl_all( true, info->curl_sock, info->device_curl, headbuf, 4))
return -1;
if (headbuf[0] == 0xff && headbuf[1] == 0xff && headbuf[2] == 0xff && headbuf[3] == 0xff)
return -1;
size_t sz = upk_u32be(headbuf, 0);
if (!bitmain_curl_all( true, info->curl_sock, info->device_curl, buf, sz))
return -1;
return sz;
}
err = read(cgpu->device_fd, buf, bufsize);
return err;
}
static
int btm_write(struct cgpu_info * const cgpu, void * const buf, const size_t bufsize)
{
int err = 0;
//applog(LOG_DEBUG, "btm_write ----- %d -----", bufsize);
if (unlikely(cgpu->device_fd == BITMAIN_USING_CURL)) {
struct bitmain_info * const info = cgpu->device_data;
uint8_t headbuf[5];
headbuf[0] = 1;
pk_u32be(headbuf, 1, bufsize);
if (!bitmain_curl_all(false, info->curl_sock, info->device_curl, headbuf, sizeof(headbuf)))
return -1;
if (!bitmain_curl_all(false, info->curl_sock, info->device_curl, buf, bufsize))
return -1;
if (!bitmain_curl_all( true, info->curl_sock, info->device_curl, headbuf, 4))
return -1;
if (headbuf[0] == 0xff && headbuf[1] == 0xff && headbuf[2] == 0xff && headbuf[3] == 0xff)
return -1;
return upk_u32be(headbuf, 0);
}
err = write(cgpu->device_fd, buf, bufsize);
return err;
}
#ifdef WIN32
#define BITMAIN_TEST
#endif
#define BITMAIN_TEST_PRINT_WORK 0
#ifdef BITMAIN_TEST
#define BITMAIN_TEST_NUM 19
#define BITMAIN_TEST_USENUM 1
int g_test_index = 0;
const char btm_work_test_data[BITMAIN_TEST_NUM][256] = {
"00000002ddc1ce5579dbec17f17fbb8f31ae218a814b2a0c1900f0d90000000100000000b58aa6ca86546b07a5a46698f736c7ca9c0eedc756d8f28ac33c20cc24d792675276f879190afc85b6888022000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000eb2d45233c5b02de50ddcb9049ba16040e0ba00e9750a474eec75891571d925b52dfda4a190266667145b02f000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000090c7d3743e0b0562e4f56d3dd35cece3c5e8275d0abb21bf7e503cb72bd7ed3b52dfda4a190266667bbb58d7000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000006e0561da06022bfbb42c5ecd74a46bfd91934f201b777e9155cc6c3674724ec652dfda4a19026666a0cd827b000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000000312f42ce4964cc23f2d8c039f106f25ddd58e10a1faed21b3bba4b0e621807b52dfda4a1902666629c9497d000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000033093a6540dbe8f7f3d19e3d2af05585ac58dafad890fa9a942e977334a23d6e52dfda4a190266665ae95079000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000bd7893057d06e69705bddf9a89c7bac6b40c5b32f15e2295fc8c5edf491ea24952dfda4a190266664b89b4d3000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000075e66f533e53837d14236a793ee4e493985642bc39e016b9e63adf14a584a2aa52dfda4a19026666ab5d638d000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000d936f90c5db5f0fe1d017344443854fbf9e40a07a9b7e74fedc8661c23162bff52dfda4a19026666338e79cb000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000d2c1a7d279a4355b017bc0a4b0a9425707786729f21ee18add3fda4252a31a4152dfda4a190266669bc90806000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000ad36d19f33d04ca779942843890bc3b083cec83a4b60b6c45cf7d21fc187746552dfda4a1902666675d81ab7000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000093b809cf82b76082eacb55bc35b79f31882ed0976fd102ef54783cd24341319b52dfda4a1902666642ab4e42000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000007411ff315430a7bbf41de8a685d457e82d5177c05640d6a4436a40f39e99667852dfda4a190266662affa4b5000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000001ad0db5b9e1e2b57c8d3654c160f5a51067521eab7e340a270639d97f00a3fa252dfda4a1902666601a47bb6000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000022e055c442c46bbe16df68603a26891f6e4cf85b90102b39fd7cadb602b4e34552dfda4a1902666695d33cea000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000009c8baf5a8a1e16de2d6ae949d5fec3ed751f10dcd4c99810f2ce08040fb9e31d52dfda4a19026666fe78849d000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000e5655532b414887f35eb4652bc7b11ebac12891f65bc08cbe0ce5b277b9e795152dfda4a19026666fcc0d1d1000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000f272c5508704e2b62dd1c30ea970372c40bf00f9203f9bf69d456b4a7fbfffe352dfda4a19026666c03d4399000000800000000000000000000000000000000000000000000000000000000000000000",
"0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000fca3b4531ba627ad9b0e23cdd84c888952c23810df196e9c6db0bcecba6a830952dfda4a19026666c14009cb000000800000000000000000000000000000000000000000000000000000000000000000"
};
const char btm_work_test_midstate[BITMAIN_TEST_NUM][256] = {
"2d8738e7f5bcf76dcb8316fec772e20e240cd58c88d47f2d3f5a6a9547ed0a35",
"d31b6ce09c0bfc2af6f3fe3a03475ebefa5aa191fa70a327a354b2c22f9692f1",
"84a8c8224b80d36caeb42eff2a100f634e1ff873e83fd02ef1306a34abef9dbe",
"059882159439b9b32968c79a93c5521e769dbea9d840f56c2a17b9ad87e530b8",
"17fa435d05012574f8f1da26994cc87b6cb9660b5e82072dc6a0881cec150a0d",
"92a28cc5ec4ba6a2688471dfe2032b5fe97c805ca286c503e447d6749796c6af",
"1677a03516d6e9509ac37e273d2482da9af6e077abe8392cdca6a30e916a7ae9",
"50bbe09f1b8ac18c97aeb745d5d2c3b5d669b6ac7803e646f65ac7b763a392d1",
"e46a0022ebdc303a7fb1a0ebfa82b523946c312e745e5b8a116b17ae6b4ce981",
"8f2f61e7f5b4d76d854e6d266acfff4d40347548216838ccc4ef3b9e43d3c9ea",
"0a450588ae99f75d676a08d0326e1ea874a3497f696722c78a80c7b6ee961ea6",
"3c4c0fc2cf040b806c51b46de9ec0dcc678a7cc5cf3eff11c6c03de3bc7818cc",
"f6c7c785ab5daddb8f98e5f854f2cb41879fcaf47289eb2b4196fefc1b28316f",
"005312351ccb0d0794779f5023e4335b5cad221accf0dfa3da7b881266fa9f5a",
"7b26d189c6bba7add54143179aadbba7ccaeff6887bd8d5bec9597d5716126e6",
"a4718f4c801e7ddf913a9474eb71774993525684ffea1915f767ab16e05e6889",
"6b6226a8c18919d0e55684638d33a6892a00d22492cc2f5906ca7a4ac21c74a7",
"383114dccd1cb824b869158aa2984d157fcb02f46234ceca65943e919329e697",
"d4d478df3016852b27cb1ae9e1e98d98617f8d0943bf9dc1217f47f817236222"
};
#endif
bool opt_bitmain_checkall = false;
bool opt_bitmain_nobeeper = false;
bool opt_bitmain_notempoverctrl = false;
bool opt_bitmain_homemode = false;
bool opt_bitmain_auto;
// --------------------------------------------------------------
// CRC16 check table
// --------------------------------------------------------------
static
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
};
static
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, 0xD9,
0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
0x5A, 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) {
return 1L << (31 - num);
}
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 fan_home_mode,
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)
{
uint16_t crc = 0;
int datalen = 0;
uint8_t version = 0;
uint8_t * sendbuf = (uint8_t *)bm;
if (unlikely(!bm)) {
applog(LOG_WARNING, "bitmain_set_txconfig bitmain_txconfig_token is null");
return -1;
}
if (unlikely(timeout_data <= 0 || asic_num <= 0 || chain_num <= 0)) {
applog(LOG_WARNING, "bitmain_set_txconfig parameter invalid timeout_data(%d) asic_num(%d) chain_num(%d)",
timeout_data, asic_num, chain_num);
return -1;
}
datalen = sizeof(struct bitmain_txconfig_token);
memset(bm, 0, datalen);
bm->token_type = BITMAIN_TOKEN_TYPE_TXCONFIG;
bm->version = version;
bm->length = datalen-4;
bm->length = htole16(bm->length);
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;
bm->beeper_ctrl = beeper_ctrl;
bm->temp_over_ctrl = temp_over_ctrl;
bm->fan_home_mode = fan_home_mode;
sendbuf[4] = htole8(sendbuf[4]);
sendbuf[5] = htole8(sendbuf[5]);
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);
memcpy(bm->voltage, voltage, 2);
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);
applog(LOG_ERR, "BTM TxConfigToken:v(%d) reset(%d) fan_e(%d) tout_e(%d) fq_e(%d) vt_e(%d) chainc_e(%d) chipc_e(%d) hw_e(%d) b_c(%d) t_c(%d) f_m(%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)",
version, reset, fan_eft, timeout_eft, frequency_eft, voltage_eft,
chain_check_time_eft, chip_config_eft, hw_error_eft, beeper_ctrl, temp_over_ctrl,fan_home_mode,chain_num, asic_num,
fan_pwm_data, timeout_data, frequency, voltage[0], voltage[1],
chain_check_time, reg_data[0], reg_data[1], reg_data[2], reg_data[3], chip_address, reg_address, crc);
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;
uint8_t version = 0;
int datalen = 0;
uint8_t * sendbuf = (uint8_t *)bm;
if (unlikely(!bm)) {
applog(LOG_WARNING, "bitmain_set_rxstatus bitmain_rxstatus_token is null");
return -1;
}
datalen = sizeof(struct bitmain_rxstatus_token);
memset(bm, 0, datalen);
bm->token_type = BITMAIN_TOKEN_TYPE_RXSTATUS;
bm->version = version;
bm->length = datalen-4;
bm->length = htole16(bm->length);
bm->chip_status_eft = chip_status_eft;
bm->detect_get = detect_get;
sendbuf[4] = htole8(sendbuf[4]);
bm->chip_address = chip_address;
bm->reg_address = reg_address;
crc = CRC16((uint8_t *)bm, datalen-2);
bm->crc = htole16(crc);
applog(LOG_ERR, "BitMain RxStatus Token: v(%d) chip_status_eft(%d) detect_get(%d) chip_address(%02x) reg_address(%02x) crc(%04x)",
version, chip_status_eft, detect_get, chip_address, reg_address, crc);
return datalen;
}
static int bitmain_parse_rxstatus(const uint8_t * data, int datalen, struct bitmain_rxstatus_data *bm)
{
uint16_t crc = 0;
uint8_t version = 0;
int i = 0, j = 0;
int asic_num = 0;
int dataindex = 0;
uint8_t tmp = 0x01;
if (unlikely(!bm)) {
applog(LOG_WARNING, "bitmain_parse_rxstatus bitmain_rxstatus_data is null");
return -1;
}
if (unlikely(!data || datalen <= 0)) {
applog(LOG_WARNING, "bitmain_parse_rxstatus parameter invalid data is null or datalen(%d) error", datalen);
return -1;
}
memset(bm, 0, sizeof(struct bitmain_rxstatus_data));
memcpy(bm, data, 28);
if (bm->data_type != BITMAIN_DATA_TYPE_RXSTATUS) {
applog(LOG_ERR, "bitmain_parse_rxstatus datatype(%02x) error", bm->data_type);
return -1;
}
if (bm->version != version) {
applog(LOG_ERR, "bitmain_parse_rxstatus version(%02x) error", bm->version);
return -1;
}
bm->length = htole16(bm->length);
if (bm->length+4 != datalen) {
applog(LOG_ERR, "bitmain_parse_rxstatus length(%d) datalen(%d) error", 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, "bitmain_parse_rxstatus check crc(%d) != bm crc(%d) datalen(%d)", 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, "bitmain_parse_rxstatus chain_num=%d error", 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, "bitmain_parse_rxstatus dataindex(%d) chain_num(%d) temp_num(%d) fan_num(%d) not match datalen(%d)",
dataindex, bm->chain_num, bm->temp_num, bm->fan_num, datalen);
return -1;
}
for(i = 0; i < bm->chain_num; i++) {
//bm->chain_asic_status[i] = swab32(bm->chain_asic_status[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;
}
if(!opt_bitmain_checkall){
if(tmp != htole8(tmp)){
applog(LOG_ERR, "BitMain RxStatus byte4 0x%02x chip_value_eft %d reserved %d get_blk_num %d ",*((uint8_t* )bm +4),bm->chip_value_eft,bm->reserved1,bm->get_blk_num);
memcpy(&tmp,data+4,1);
bm->chip_value_eft = tmp >>7;
bm->get_blk_num = tmp >> 4;
bm->reserved1 = ((tmp << 4) & 0xff) >> 5;
}
found_blocks = bm->get_blk_num;
applog(LOG_ERR, "BitMain RxStatus tmp :0x%02x byte4 0x%02x chip_value_eft %d reserved %d get_blk_num %d ",tmp,*((uint8_t* )bm +4),bm->chip_value_eft,bm->reserved1,bm->get_blk_num);
}
applog(LOG_DEBUG, "BitMain RxStatusData: 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)",
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, "BitMain RxStatus Data chain info:");
for(i = 0; i < bm->chain_num; i++) {
applog(LOG_DEBUG, "BitMain RxStatus Data chain(%d) asic num=%d asic_exist=%08x asic_status=%08x", i+1, bm->chain_asic_num[i], bm->chain_asic_exist[i*8], bm->chain_asic_status[i*8]);
}
applog(LOG_DEBUG, "BitMain RxStatus Data temp info:");
for(i = 0; i < bm->temp_num; i++) {
applog(LOG_DEBUG, "BitMain RxStatus Data temp(%d) temp=%d", i+1, bm->temp[i]);
}
applog(LOG_DEBUG, "BitMain RxStatus Data fan info:");
for(i = 0; i < bm->fan_num; i++) {
applog(LOG_DEBUG, "BitMain RxStatus Data fan(%d) fan=%d", 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;
uint8_t version = 0;
int curnoncenum = 0;
if (unlikely(!bm)) {
applog(LOG_ERR, "bitmain_parse_rxnonce bitmain_rxstatus_data null");
return -1;
}
if (unlikely(!data || datalen <= 0)) {
applog(LOG_ERR, "bitmain_parse_rxnonce data null or datalen(%d) error", datalen);
return -1;
}
memcpy(bm, data, sizeof(struct bitmain_rxnonce_data));
if (bm->data_type != BITMAIN_DATA_TYPE_RXNONCE) {
applog(LOG_ERR, "bitmain_parse_rxnonce datatype(%02x) error", bm->data_type);
return -1;
}
if (bm->version != version) {
applog(LOG_ERR, "bitmain_parse_rxnonce version(%02x) error", bm->version);
return -1;
}
bm->length = htole16(bm->length);
if (bm->length+4 != datalen) {
applog(LOG_ERR, "bitmain_parse_rxnonce length(%d) error", bm->length);
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, "bitmain_parse_rxnonce check crc(%d) != bm crc(%d) datalen(%d)", crc, bm->crc, datalen);
return -1;
}
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;
applog(LOG_DEBUG, "BitMain RxNonce Data: nonce_num(%d) fifo_space(%d) diff(%d) tnn(%"PRIu64")", curnoncenum, bm->fifo_space, bm->diff, bm->total_nonce_num);
for(i = 0; i < curnoncenum; i++) {
bm->nonces[i].work_id = htole32(bm->nonces[i].work_id);
bm->nonces[i].nonce = htole32(bm->nonces[i].nonce);
applog(LOG_DEBUG, "BitMain RxNonce Data %d: work_id(%d) nonce(%08x)(%d)",
i, bm->nonces[i].work_id, bm->nonces[i].nonce, bm->nonces[i].nonce);
}
*nonce_num = curnoncenum;
return 0;
}
static int bitmain_read(struct cgpu_info *bitmain, unsigned char *buf,
size_t bufsize, int timeout)
{
int err = 0;
size_t total = 0;
if(bitmain == NULL || buf == NULL || bufsize <= 0) {
applog(LOG_WARNING, "bitmain_read parameter error bufsize(%"PRIu64")", (uint64_t)bufsize);
return -1;
}
{
err = btm_read(bitmain, buf, bufsize);
total = err;
}
return total;
}
static int bitmain_write(struct cgpu_info *bitmain, char *buf, ssize_t len)
{
int err;
{
int havelen = 0;
while(havelen < len) {
err = btm_write(bitmain, buf+havelen, len-havelen);
if(err < 0) {
applog(LOG_DEBUG, "%s%i: btm_write got err %d", bitmain->drv->name,
bitmain->device_id, err);
applog(LOG_WARNING, "usb_write error on bitmain_write");
return BTM_SEND_ERROR;
} else {
havelen += err;
}
}
}
return BTM_SEND_OK;
}
static int bitmain_send_data(const uint8_t * data, int datalen, struct cgpu_info *bitmain)
{
int ret;
if(datalen <= 0) {
return 0;
}
//struct bitmain_info *info = bitmain->device_data;
//int delay;
//delay = datalen * 10 * 1000000;
//delay = delay / info->baud;
//delay += 4000;
if(opt_debug) {
char hex[(datalen * 2) + 1];
bin2hex(hex, data, datalen);
applog(LOG_DEBUG, "BitMain: Sent(%d): %s", datalen, hex);
}
//cgtimer_t ts_start;
//cgsleep_prepare_r(&ts_start);
//applog(LOG_DEBUG, "----bitmain_send_data start");
ret = bitmain_write(bitmain, (char *)data, datalen);
applog(LOG_DEBUG, "----bitmain_send_data stop ret=%d datalen=%d", ret, datalen);
//cgsleep_us_r(&ts_start, delay);
//applog(LOG_DEBUG, "BitMain: Sent: Buffer delay: %dus", delay);
return ret;
}
static void bitmain_inc_nvw(struct bitmain_info *info, struct thr_info *thr)
{
applog(LOG_INFO, "%s%d: No matching work - HW error",
thr->cgpu->drv->name, thr->cgpu->device_id);
inc_hw_errors_only(thr);
info->no_matching_work++;
}
static inline void record_temp_fan(struct bitmain_info *info, struct bitmain_rxstatus_data *bm, float *temp)
{
int i = 0;
int maxfan = 0, maxtemp = 0;
int temp_avg = 0;
info->fan_num = bm->fan_num;
for(i = 0; i < bm->fan_num; i++) {
info->fan[i] = bm->fan[i] * BITMAIN_FAN_FACTOR;
if(info->fan[i] > maxfan)
maxfan = info->fan[i];
}
info->temp_num = bm->temp_num;
for(i = 0; i < bm->temp_num; i++) {
info->temp[i] = bm->temp[i];
/*
if(bm->temp[i] & 0x80) {
bm->temp[i] &= 0x7f;
info->temp[i] = 0 - ((~bm->temp[i] & 0x7f) + 1);
}*/
temp_avg += info->temp[i];
if(info->temp[i] > info->temp_max) {
info->temp_max = info->temp[i];
}
if(info->temp[i] > maxtemp)
maxtemp = info->temp[i];
}
if(bm->temp_num > 0) {
temp_avg /= bm->temp_num;
info->temp_avg = temp_avg;
}
*temp = maxtemp;
}
static void bitmain_update_temps(struct cgpu_info *bitmain, struct bitmain_info *info,
struct bitmain_rxstatus_data *bm)
{
char tmp[64] = {0};
char msg[10240] = {0};
int i = 0;
record_temp_fan(info, bm, &(bitmain->temp));
strcpy(msg, "BitMain: ");
for(i = 0; i < bm->fan_num; i++) {
if(i != 0) {
strcat(msg, ", ");
}
sprintf(tmp, "Fan%d: %d/m", i+1, info->fan[i]);
strcat(msg, tmp);
}
strcat(msg, "\t");
for(i = 0; i < bm->temp_num; i++) {
if(i != 0) {
strcat(msg, ", ");
}
sprintf(tmp, "Temp%d: %dC", i+1, info->temp[i]);
strcat(msg, tmp);
}
sprintf(tmp, ", TempMAX: %dC", info->temp_max);
strcat(msg, tmp);
applog(LOG_INFO, "%s", msg);
info->temp_history_index++;
info->temp_sum += bitmain->temp;
applog(LOG_DEBUG, "BitMain: temp_index: %d, temp_count: %d, temp_old: %d",
info->temp_history_index, info->temp_history_count, info->temp_old);
if (info->temp_history_index == info->temp_history_count) {
info->temp_history_index = 0;
info->temp_sum = 0;
}
}
static void bitmain_set_fifo_space(struct cgpu_info * const dev, const int fifo_space)
{
struct thr_info * const master_thr = dev->thr[0];
struct bitmain_info * const info = dev->device_data;
if (unlikely(fifo_space > info->max_fifo_space))
info->max_fifo_space = fifo_space;
info->fifo_space = fifo_space;
master_thr->queue_full = !fifo_space;
}
static void bitmain_parse_results(struct cgpu_info *bitmain, struct bitmain_info *info,
struct thr_info *thr, uint8_t *buf, int *offset)
{
int i, j, n, m, r, errordiff, spare = BITMAIN_READ_SIZE;
uint32_t checkbit = 0x00000000;
bool found = false;
struct work *work = NULL;
struct bitmain_packet_head packethead;
int asicnum = 0;
int mod = 0,tmp = 0;
for (i = 0; i <= spare; i++) {
if(buf[i] == 0xa1) {
struct bitmain_rxstatus_data rxstatusdata;
applog(LOG_DEBUG, "bitmain_parse_results RxStatus Data");
if(*offset < 4) {
return;
}
memcpy(&packethead, buf+i, sizeof(struct bitmain_packet_head));
packethead.length = htole16(packethead.length);
if(packethead.length > 1130) {
applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxstatus datalen=%d error", packethead.length+4);
continue;
}
if(*offset < packethead.length + 4) {
return;
}
if(bitmain_parse_rxstatus(buf+i, packethead.length+4, &rxstatusdata) != 0) {
applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxstatus error len=%d", packethead.length+4);
} else {
mutex_lock(&info->qlock);
info->chain_num = rxstatusdata.chain_num;
bitmain_set_fifo_space(bitmain, rxstatusdata.fifo_space);
info->hw_version[0] = rxstatusdata.hw_version[0];
info->hw_version[1] = rxstatusdata.hw_version[1];
info->hw_version[2] = rxstatusdata.hw_version[2];
info->hw_version[3] = rxstatusdata.hw_version[3];
info->nonce_error = rxstatusdata.nonce_error;
errordiff = info->nonce_error-info->last_nonce_error;
//sprintf(g_miner_version, "%d.%d.%d.%d", info->hw_version[0], info->hw_version[1], info->hw_version[2], info->hw_version[3]);
applog(LOG_ERR, "bitmain_parse_results v=%d chain=%d fifo=%d hwv1=%d hwv2=%d hwv3=%d hwv4=%d nerr=%d-%d freq=%d chain info:",
rxstatusdata.version, info->chain_num, info->fifo_space, info->hw_version[0], info->hw_version[1], info->hw_version[2], info->hw_version[3],
info->last_nonce_error, info->nonce_error, info->frequency);
memcpy(info->chain_asic_exist, rxstatusdata.chain_asic_exist, BITMAIN_MAX_CHAIN_NUM*32);
memcpy(info->chain_asic_status, rxstatusdata.chain_asic_status, BITMAIN_MAX_CHAIN_NUM*32);
for(n = 0; n < rxstatusdata.chain_num; n++) {
info->chain_asic_num[n] = rxstatusdata.chain_asic_num[n];
memset(info->chain_asic_status_t[n], 0, 320);
j = 0;
mod = 0;
if(info->chain_asic_num[n] <= 0) {
asicnum = 0;
} else {
mod = info->chain_asic_num[n] % 32;
if(mod == 0) {
asicnum = info->chain_asic_num[n] / 32;
} else {
asicnum = info->chain_asic_num[n] / 32 + 1;
}
}
if(asicnum > 0) {
for(m = asicnum-1; m >= 0; m--) {
tmp = mod ? (32-mod): 0;
for(r = tmp;r < 32;r++){
if((r-tmp)%8 == 0 && (r-tmp) !=0){
info->chain_asic_status_t[n][j] = ' ';
j++;
}
checkbit = num2bit(r);
if(rxstatusdata.chain_asic_exist[n*8+m] & checkbit) {
if(rxstatusdata.chain_asic_status[n*8+m] & checkbit) {
info->chain_asic_status_t[n][j] = 'o';
} else {
info->chain_asic_status_t[n][j] = 'x';
}
} else {
info->chain_asic_status_t[n][j] = '-';
}
j++;
}
info->chain_asic_status_t[n][j] = ' ';
j++;
mod = 0;
}
}
applog(LOG_DEBUG, "bitmain_parse_results chain(%d) asic_num=%d asic_exist=%08x%08x%08x%08x%08x%08x%08x%08x asic_status=%08x%08x%08x%08x%08x%08x%08x%08x",
n, info->chain_asic_num[n],
info->chain_asic_exist[n*8+0], info->chain_asic_exist[n*8+1], info->chain_asic_exist[n*8+2], info->chain_asic_exist[n*8+3], info->chain_asic_exist[n*8+4], info->chain_asic_exist[n*8+5], info->chain_asic_exist[n*8+6], info->chain_asic_exist[n*8+7],
info->chain_asic_status[n*8+0], info->chain_asic_status[n*8+1], info->chain_asic_status[n*8+2], info->chain_asic_status[n*8+3], info->chain_asic_status[n*8+4], info->chain_asic_status[n*8+5], info->chain_asic_status[n*8+6], info->chain_asic_status[n*8+7]);
applog(LOG_ERR, "bitmain_parse_results chain(%d) asic_num=%d asic_status=%s", n, info->chain_asic_num[n], info->chain_asic_status_t[n]);
}
mutex_unlock(&info->qlock);
if(errordiff > 0) {
for(j = 0; j < errordiff; j++) {
bitmain_inc_nvw(info, thr);
}
mutex_lock(&info->qlock);
info->last_nonce_error += errordiff;
mutex_unlock(&info->qlock);
}
bitmain_update_temps(bitmain, info, &rxstatusdata);
}
found = true;
spare = packethead.length + 4 + i;
if(spare > *offset) {
applog(LOG_ERR, "bitmain_parse_rxresults space(%d) > offset(%d)", spare, *offset);
spare = *offset;
}
break;
} else if(buf[i] == 0xa2) {
struct bitmain_rxnonce_data rxnoncedata;
int nonce_num = 0;
applog(LOG_DEBUG, "bitmain_parse_results RxNonce Data");
if(*offset < 4) {
return;
}
memcpy(&packethead, buf+i, sizeof(struct bitmain_packet_head));
packethead.length = htole16(packethead.length);
if(packethead.length > 1038) {
applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxnonce datalen=%d error", packethead.length+4);
continue;
}
if(*offset < packethead.length + 4) {
return;
}
if(bitmain_parse_rxnonce(buf+i, packethead.length+4, &rxnoncedata, &nonce_num) != 0) {
applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxnonce error len=%d", packethead.length+4);
} else {
const float nonce_diff = 1 << rxnoncedata.diff;
for(j = 0; j < nonce_num; j++) {
const work_device_id_t work_id = rxnoncedata.nonces[j].work_id;
HASH_FIND(hh, thr->work_list, &work_id, sizeof(work_id), work);
if(work) {
if(BITMAIN_TEST_PRINT_WORK) {
applog(LOG_ERR, "bitmain_parse_results nonce find work(%d-%d)(%08x)", work->id, rxnoncedata.nonces[j].work_id, rxnoncedata.nonces[j].nonce);
char ob_hex[(32 * 2) + 1];
bin2hex(ob_hex, work->midstate, 32);
applog(LOG_ERR, "work %d midstate: %s", work->id, ob_hex);
bin2hex(ob_hex, &work->data[64], 12);
applog(LOG_ERR, "work %d data2: %s", work->id, ob_hex);
}
{
const uint32_t nonce = rxnoncedata.nonces[j].nonce;
applog(LOG_DEBUG, "BitMain: submit nonce = %08lx", (unsigned long)nonce);
work->nonce_diff = nonce_diff;
if (submit_nonce(thr, work, nonce)) {
mutex_lock(&info->qlock);
hashes_done2(thr, 0x100000000 * work->nonce_diff, NULL);
mutex_unlock(&info->qlock);
} else {
applog(LOG_ERR, "BitMain: bitmain_decode_nonce error work(%d)", rxnoncedata.nonces[j].work_id);
}
}
} else {
bitmain_inc_nvw(info, thr);
applog(LOG_ERR, "BitMain: Nonce not find work(%d)", rxnoncedata.nonces[j].work_id);
}
}
mutex_lock(&info->qlock);
bitmain_set_fifo_space(bitmain, rxnoncedata.fifo_space);
mutex_unlock(&info->qlock);
applog(LOG_DEBUG, "bitmain_parse_rxnonce fifo space=%d", info->fifo_space);