-
Notifications
You must be signed in to change notification settings - Fork 5
/
driver-klondike.c
1557 lines (1327 loc) · 42 KB
/
driver-klondike.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 2013 Andrew Smith
* Copyright 2013 Con Kolivas
* Copyright 2013 Chris Savery
*
* 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 <float.h>
#include <limits.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <strings.h>
#include <sys/time.h>
#include <unistd.h>
#include <math.h>
#include "config.h"
#ifdef WIN32
#include <windows.h>
#endif
#include "compat.h"
#include "miner.h"
#include "usbutils.h"
#define K1 "K1"
#define K16 "K16"
#define K64 "K64"
static const char *msg_detect_send = "DSend";
static const char *msg_detect_reply = "DReply";
static const char *msg_send = "Send";
static const char *msg_reply = "Reply";
#define KLN_CMD_ABORT 'A'
#define KLN_CMD_CONFIG 'C'
#define KLN_CMD_ENABLE 'E'
#define KLN_CMD_IDENT 'I'
#define KLN_CMD_NONCE '='
#define KLN_CMD_STATUS 'S'
#define KLN_CMD_WORK 'W'
#define KLN_CMD_ENABLE_OFF '0'
#define KLN_CMD_ENABLE_ON '1'
#define MIDSTATE_BYTES 32
#define MERKLE_OFFSET 64
#define MERKLE_BYTES 12
#define REPLY_SIZE 15 // adequate for all types of replies
#define MAX_KLINES 1024 // unhandled reply limit
#define REPLY_WAIT_TIME 100 // poll interval for a cmd waiting it's reply
#define CMD_REPLY_RETRIES 8 // how many retries for cmds
#define MAX_WORK_COUNT 4 // for now, must be binary multiple and match firmware
#define TACH_FACTOR 87890 // fan rpm divisor
#define KLN_KILLWORK_TEMP 53.5
#define KLN_COOLED_DOWN 45.5
/*
* Work older than 5s will already be completed
* FYI it must not be possible to complete 256 work
* items this quickly on a single device -
* thus limited to 219.9GH/s per device
*/
#define OLD_WORK_MS ((int)(5 * 1000))
/*
* How many incorrect slave counts to ignore in a row
* 2 means it allows random grabage returned twice
* Until slaves are implemented, this should never occur
* so allowing 2 in a row should ignore random errros
*/
#define KLN_ISS_IGNORE 2
/*
* If the queue status hasn't been updated for this long then do it now
* 5GH/s = 859ms per full nonce range
*/
#define LATE_UPDATE_MS ((int)(2.5 * 1000))
// If 5 late updates in a row, try to reset the device
#define LATE_UPDATE_LIMIT 5
// If the reset fails sleep for 1s
#define LATE_UPDATE_SLEEP_MS 1000
// However give up after 8s
#define LATE_UPDATE_NODEV_MS ((int)(8.0 * 1000))
struct device_drv klondike_drv;
typedef struct klondike_header {
uint8_t cmd;
uint8_t dev;
uint8_t buf[REPLY_SIZE-2];
} HEADER;
#define K_2(_bytes) ((int)(_bytes[0]) + \
((int)(_bytes[1]) << 8))
#define K_4(_bytes) ((uint64_t)(_bytes[0]) + \
((uint64_t)(_bytes[1]) << 8) + \
((uint64_t)(_bytes[2]) << 16) + \
((uint64_t)(_bytes[3]) << 24))
#define K_SERIAL(_serial) K_4(_serial)
#define K_HASHCOUNT(_hashcount) K_2(_hashcount)
#define K_MAXCOUNT(_maxcount) K_2(_maxcount)
#define K_NONCE(_nonce) K_4(_nonce)
#define K_HASHCLOCK(_hashclock) K_2(_hashclock)
#define SET_HASHCLOCK(_hashclock, _value) do { \
(_hashclock)[0] = (uint8_t)((_value) & 0xff); \
(_hashclock)[1] = (uint8_t)(((_value) >> 8) & 0xff); \
} while(0)
#define KSENDHD(_add) (sizeof(uint8_t) + sizeof(uint8_t) + _add)
typedef struct klondike_id {
uint8_t cmd;
uint8_t dev;
uint8_t version;
uint8_t product[7];
uint8_t serial[4];
} IDENTITY;
typedef struct klondike_status {
uint8_t cmd;
uint8_t dev;
uint8_t state;
uint8_t chipcount;
uint8_t slavecount;
uint8_t workqc;
uint8_t workid;
uint8_t temp;
uint8_t fanspeed;
uint8_t errorcount;
uint8_t hashcount[2];
uint8_t maxcount[2];
uint8_t noise;
} WORKSTATUS;
typedef struct _worktask {
uint8_t cmd;
uint8_t dev;
uint8_t workid;
uint8_t midstate[32];
uint8_t merkle[12];
} WORKTASK;
typedef struct _workresult {
uint8_t cmd;
uint8_t dev;
uint8_t workid;
uint8_t nonce[4];
} WORKRESULT;
typedef struct klondike_cfg {
uint8_t cmd;
uint8_t dev;
uint8_t hashclock[2];
uint8_t temptarget;
uint8_t tempcritical;
uint8_t fantarget;
uint8_t pad2;
} WORKCFG;
typedef struct kline {
union {
HEADER hd;
IDENTITY id;
WORKSTATUS ws;
WORKTASK wt;
WORKRESULT wr;
WORKCFG cfg;
};
} KLINE;
#define zero_kline(_kline) memset((void *)(_kline), 0, sizeof(KLINE));
typedef struct device_info {
uint32_t noncecount;
uint32_t nextworkid;
uint16_t lasthashcount;
uint64_t totalhashcount;
uint32_t rangesize;
uint32_t *chipstats;
} DEVINFO;
typedef struct klist {
struct klist *prev;
struct klist *next;
KLINE kline;
struct timeval tv_when;
int block_seq;
bool ready;
bool working;
} KLIST;
typedef struct jobque {
int workqc;
struct timeval last_update;
bool overheat;
bool flushed;
int late_update_count;
int late_update_sequential;
} JOBQUE;
struct klondike_info {
pthread_rwlock_t stat_lock;
struct thr_info replies_thr;
cglock_t klist_lock;
KLIST *used;
KLIST *free;
int kline_count;
int used_count;
int block_seq;
KLIST *status;
DEVINFO *devinfo;
KLIST *cfg;
JOBQUE *jobque;
int noncecount;
uint64_t hashcount;
uint64_t errorcount;
uint64_t noisecount;
int incorrect_slave_sequential;
// us Delay from USB reply to being processed
double delay_count;
double delay_total;
double delay_min;
double delay_max;
struct timeval tv_last_nonce_received;
// Time from recieving one nonce to the next
double nonce_count;
double nonce_total;
double nonce_min;
double nonce_max;
int wque_size;
int wque_cleared;
bool initialised;
};
static KLIST *new_klist_set(struct cgpu_info *klncgpu)
{
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
KLIST *klist = NULL;
int i;
klist = calloc(MAX_KLINES, sizeof(*klist));
if (!klist)
quit(1, "Failed to calloc klist - when old count=%d", klninfo->kline_count);
klninfo->kline_count += MAX_KLINES;
klist[0].prev = NULL;
klist[0].next = &(klist[1]);
for (i = 1; i < MAX_KLINES-1; i++) {
klist[i].prev = &klist[i-1];
klist[i].next = &klist[i+1];
}
klist[MAX_KLINES-1].prev = &(klist[MAX_KLINES-2]);
klist[MAX_KLINES-1].next = NULL;
return klist;
}
static KLIST *allocate_kitem(struct cgpu_info *klncgpu)
{
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
KLIST *kitem = NULL;
int ran_out = 0;
char errbuf[1024];
cg_wlock(&klninfo->klist_lock);
if (klninfo->free == NULL) {
ran_out = klninfo->kline_count;
klninfo->free = new_klist_set(klncgpu);
snprintf(errbuf, sizeof(errbuf),
"%s%i: KLINE count exceeded %d, now %d",
klncgpu->drv->name, klncgpu->device_id,
ran_out, klninfo->kline_count);
}
kitem = klninfo->free;
klninfo->free = klninfo->free->next;
if (klninfo->free)
klninfo->free->prev = NULL;
kitem->next = klninfo->used;
kitem->prev = NULL;
if (kitem->next)
kitem->next->prev = kitem;
klninfo->used = kitem;
kitem->ready = false;
kitem->working = false;
memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
klninfo->used_count++;
cg_wunlock(&klninfo->klist_lock);
if (ran_out > 0)
applog(LOG_WARNING, "%s", errbuf);
return kitem;
}
static KLIST *release_kitem(struct cgpu_info *klncgpu, KLIST *kitem)
{
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
cg_wlock(&klninfo->klist_lock);
if (kitem == klninfo->used)
klninfo->used = kitem->next;
if (kitem->next)
kitem->next->prev = kitem->prev;
if (kitem->prev)
kitem->prev->next = kitem->next;
kitem->next = klninfo->free;
if (klninfo->free)
klninfo->free->prev = kitem;
kitem->prev = NULL;
klninfo->free = kitem;
klninfo->used_count--;
cg_wunlock(&klninfo->klist_lock);
return NULL;
}
static double cvtKlnToC(uint8_t temp)
{
double Rt, stein, celsius;
if (temp == 0)
return 0.0;
Rt = 1000.0 * 255.0 / (double)temp - 1000.0;
stein = log(Rt / 2200.0) / 3987.0;
stein += 1.0 / (double)(25.0 + 273.15);
celsius = (1.0 / stein) - 273.15;
// For display of bad data
if (celsius < 0.0)
celsius = 0.0;
if (celsius > 200.0)
celsius = 200.0;
return celsius;
}
static int cvtCToKln(double deg)
{
double Rt, stein, temp;
if (deg < 0.0)
deg = 0.0;
stein = 1.0 / (deg + 273.15);
stein -= 1.0 / (double)(25.0 + 273.15);
Rt = exp(stein * 3987.0) * 2200.0;
if (Rt == -1000.0)
Rt++;
temp = 1000.0 * 256.0 / (Rt + 1000.0);
if (temp > 255)
temp = 255;
if (temp < 0)
temp = 0;
return (int)temp;
}
// Change this to LOG_WARNING if you wish to always see the replies
#define READ_DEBUG LOG_DEBUG
static void display_kline(struct cgpu_info *klncgpu, KLINE *kline, const char *msg)
{
char *hexdata;
switch (kline->hd.cmd) {
case KLN_CMD_NONCE:
applog(READ_DEBUG,
"%s%i:%d %s work [%c] dev=%d workid=%d"
" nonce=0x%08x",
klncgpu->drv->name, klncgpu->device_id,
(int)(kline->wr.dev), msg, kline->wr.cmd,
(int)(kline->wr.dev),
(int)(kline->wr.workid),
(unsigned int)K_NONCE(kline->wr.nonce) - 0xC0);
break;
case KLN_CMD_STATUS:
case KLN_CMD_WORK:
case KLN_CMD_ENABLE:
case KLN_CMD_ABORT:
applog(READ_DEBUG,
"%s%i:%d %s status [%c] dev=%d chips=%d"
" slaves=%d workcq=%d workid=%d temp=%d fan=%d"
" errors=%d hashes=%d max=%d noise=%d",
klncgpu->drv->name, klncgpu->device_id,
(int)(kline->ws.dev), msg, kline->ws.cmd,
(int)(kline->ws.dev),
(int)(kline->ws.chipcount),
(int)(kline->ws.slavecount),
(int)(kline->ws.workqc),
(int)(kline->ws.workid),
(int)(kline->ws.temp),
(int)(kline->ws.fanspeed),
(int)(kline->ws.errorcount),
K_HASHCOUNT(kline->ws.hashcount),
K_MAXCOUNT(kline->ws.maxcount),
(int)(kline->ws.noise));
break;
case KLN_CMD_CONFIG:
applog(READ_DEBUG,
"%s%i:%d %s config [%c] dev=%d clock=%d"
" temptarget=%d tempcrit=%d fan=%d",
klncgpu->drv->name, klncgpu->device_id,
(int)(kline->cfg.dev), msg, kline->cfg.cmd,
(int)(kline->cfg.dev),
K_HASHCLOCK(kline->cfg.hashclock),
(int)(kline->cfg.temptarget),
(int)(kline->cfg.tempcritical),
(int)(kline->cfg.fantarget));
break;
case KLN_CMD_IDENT:
applog(READ_DEBUG,
"%s%i:%d %s info [%c] version=0x%02x prod=%.7s"
" serial=0x%08x",
klncgpu->drv->name, klncgpu->device_id,
(int)(kline->hd.dev), msg, kline->hd.cmd,
(int)(kline->id.version),
kline->id.product,
(unsigned int)K_SERIAL(kline->id.serial));
break;
default:
hexdata = bin2hex((unsigned char *)&(kline->hd.dev), REPLY_SIZE - 1);
applog(LOG_ERR,
"%s%i:%d %s [%c:%s] unknown and ignored",
klncgpu->drv->name, klncgpu->device_id,
(int)(kline->hd.dev), msg, kline->hd.cmd,
hexdata);
free(hexdata);
break;
}
}
static void display_send_kline(struct cgpu_info *klncgpu, KLINE *kline, const char *msg)
{
char *hexdata;
switch (kline->hd.cmd) {
case KLN_CMD_WORK:
applog(READ_DEBUG,
"%s%i:%d %s work [%c] dev=%d workid=0x%02x ...",
klncgpu->drv->name, klncgpu->device_id,
(int)(kline->wt.dev), msg, kline->ws.cmd,
(int)(kline->wt.dev),
(int)(kline->wt.workid));
break;
case KLN_CMD_CONFIG:
applog(READ_DEBUG,
"%s%i:%d %s config [%c] dev=%d clock=%d"
" temptarget=%d tempcrit=%d fan=%d",
klncgpu->drv->name, klncgpu->device_id,
(int)(kline->cfg.dev), msg, kline->cfg.cmd,
(int)(kline->cfg.dev),
K_HASHCLOCK(kline->cfg.hashclock),
(int)(kline->cfg.temptarget),
(int)(kline->cfg.tempcritical),
(int)(kline->cfg.fantarget));
break;
case KLN_CMD_IDENT:
case KLN_CMD_STATUS:
case KLN_CMD_ABORT:
applog(READ_DEBUG,
"%s%i:%d %s cmd [%c]",
klncgpu->drv->name, klncgpu->device_id,
(int)(kline->hd.dev), msg, kline->hd.cmd);
break;
case KLN_CMD_ENABLE:
applog(READ_DEBUG,
"%s%i:%d %s enable [%c] enable=%c",
klncgpu->drv->name, klncgpu->device_id,
(int)(kline->hd.dev), msg, kline->hd.cmd,
(char)(kline->hd.buf[0]));
break;
case KLN_CMD_NONCE:
default:
hexdata = bin2hex((unsigned char *)&(kline->hd.dev), REPLY_SIZE - 1);
applog(LOG_ERR,
"%s%i:%d %s [%c:%s] unknown/unexpected and ignored",
klncgpu->drv->name, klncgpu->device_id,
(int)(kline->hd.dev), msg, kline->hd.cmd,
hexdata);
free(hexdata);
break;
}
}
static bool SendCmd(struct cgpu_info *klncgpu, KLINE *kline, int datalen)
{
int err, amt, writ;
if (klncgpu->usbinfo.nodev)
return false;
display_send_kline(klncgpu, kline, msg_send);
writ = KSENDHD(datalen);
err = usb_write(klncgpu, (char *)kline, writ, &amt, C_REQUESTRESULTS);
if (err < 0 || amt != writ) {
applog(LOG_ERR, "%s%i:%d Cmd:%c Dev:%d, write failed (%d:%d:%d)",
klncgpu->drv->name, klncgpu->device_id,
(int)(kline->hd.dev),
kline->hd.cmd, (int)(kline->hd.dev),
writ, amt, err);
return false;
}
return true;
}
static KLIST *GetReply(struct cgpu_info *klncgpu, uint8_t cmd, uint8_t dev)
{
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
KLIST *kitem;
int retries = CMD_REPLY_RETRIES;
while (retries-- > 0 && klncgpu->shutdown == false) {
cgsleep_ms(REPLY_WAIT_TIME);
cg_rlock(&klninfo->klist_lock);
kitem = klninfo->used;
while (kitem) {
if (kitem->kline.hd.cmd == cmd &&
kitem->kline.hd.dev == dev &&
kitem->ready == true && kitem->working == false) {
kitem->working = true;
cg_runlock(&klninfo->klist_lock);
return kitem;
}
kitem = kitem->next;
}
cg_runlock(&klninfo->klist_lock);
}
return NULL;
}
static KLIST *SendCmdGetReply(struct cgpu_info *klncgpu, KLINE *kline, int datalen)
{
if (!SendCmd(klncgpu, kline, datalen))
return NULL;
return GetReply(klncgpu, kline->hd.cmd, kline->hd.dev);
}
static bool klondike_get_stats(struct cgpu_info *klncgpu)
{
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
KLIST *kitem;
KLINE kline;
int slaves, dev;
if (klncgpu->usbinfo.nodev || klninfo->status == NULL)
return false;
applog(LOG_DEBUG, "%s%i: getting status",
klncgpu->drv->name, klncgpu->device_id);
rd_lock(&(klninfo->stat_lock));
slaves = klninfo->status[0].kline.ws.slavecount;
rd_unlock(&(klninfo->stat_lock));
// loop thru devices and get status for each
for (dev = 0; dev <= slaves; dev++) {
zero_kline(&kline);
kline.hd.cmd = KLN_CMD_STATUS;
kline.hd.dev = dev;
kitem = SendCmdGetReply(klncgpu, &kline, 0);
if (kitem != NULL) {
wr_lock(&(klninfo->stat_lock));
memcpy((void *)(&(klninfo->status[dev])),
(void *)kitem,
sizeof(klninfo->status[dev]));
wr_unlock(&(klninfo->stat_lock));
kitem = release_kitem(klncgpu, kitem);
} else {
applog(LOG_ERR, "%s%i:%d failed to update stats",
klncgpu->drv->name, klncgpu->device_id, dev);
}
}
return true;
}
// TODO: this only enables the master (no slaves)
static bool kln_enable(struct cgpu_info *klncgpu)
{
KLIST *kitem;
KLINE kline;
int tries = 2;
bool ok = false;
zero_kline(&kline);
kline.hd.cmd = KLN_CMD_ENABLE;
kline.hd.dev = 0;
kline.hd.buf[0] = KLN_CMD_ENABLE_ON;
while (tries-- > 0) {
kitem = SendCmdGetReply(klncgpu, &kline, 1);
if (kitem) {
kitem = release_kitem(klncgpu, kitem);
ok = true;
break;
}
cgsleep_ms(50);
}
if (ok)
cgsleep_ms(50);
return ok;
}
static void kln_disable(struct cgpu_info *klncgpu, int dev, bool all)
{
KLINE kline;
int i;
zero_kline(&kline);
kline.hd.cmd = KLN_CMD_ENABLE;
kline.hd.buf[0] = KLN_CMD_ENABLE_OFF;
for (i = (all ? 0 : dev); i <= dev; i++) {
kline.hd.dev = i;
SendCmd(klncgpu, &kline, KSENDHD(1));
}
}
static bool klondike_init(struct cgpu_info *klncgpu)
{
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
KLIST *kitem;
KLINE kline;
int slaves, dev;
klninfo->initialised = false;
zero_kline(&kline);
kline.hd.cmd = KLN_CMD_STATUS;
kline.hd.dev = 0;
kitem = SendCmdGetReply(klncgpu, &kline, 0);
if (kitem == NULL)
return false;
slaves = kitem->kline.ws.slavecount;
if (klninfo->status == NULL) {
applog(LOG_DEBUG, "%s%i: initializing data",
klncgpu->drv->name, klncgpu->device_id);
// alloc space for status, devinfo, cfg and jobque for master and slaves
klninfo->status = calloc(slaves+1, sizeof(*(klninfo->status)));
if (unlikely(!klninfo->status))
quit(1, "Failed to calloc status array in klondke_get_stats");
klninfo->devinfo = calloc(slaves+1, sizeof(*(klninfo->devinfo)));
if (unlikely(!klninfo->devinfo))
quit(1, "Failed to calloc devinfo array in klondke_get_stats");
klninfo->cfg = calloc(slaves+1, sizeof(*(klninfo->cfg)));
if (unlikely(!klninfo->cfg))
quit(1, "Failed to calloc cfg array in klondke_get_stats");
klninfo->jobque = calloc(slaves+1, sizeof(*(klninfo->jobque)));
if (unlikely(!klninfo->jobque))
quit(1, "Failed to calloc jobque array in klondke_get_stats");
}
memcpy((void *)(&(klninfo->status[0])), (void *)kitem, sizeof(klninfo->status[0]));
kitem = release_kitem(klncgpu, kitem);
// zero init triggers read back only
zero_kline(&kline);
kline.cfg.cmd = KLN_CMD_CONFIG;
int size = 2;
// boundaries are checked by device, with valid values returned
if (opt_klondike_options != NULL) {
int hashclock;
double temptarget;
sscanf(opt_klondike_options, "%d:%lf", &hashclock, &temptarget);
SET_HASHCLOCK(kline.cfg.hashclock, hashclock);
kline.cfg.temptarget = cvtCToKln(temptarget);
kline.cfg.tempcritical = 0; // hard code for old firmware
kline.cfg.fantarget = 0xff; // hard code for old firmware
size = sizeof(kline.cfg) - 2;
}
for (dev = 0; dev <= slaves; dev++) {
kline.cfg.dev = dev;
kitem = SendCmdGetReply(klncgpu, &kline, size);
if (kitem != NULL) {
memcpy((void *)&(klninfo->cfg[dev]), kitem, sizeof(klninfo->cfg[dev]));
applog(LOG_WARNING, "%s%i:%d config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
klncgpu->drv->name, klncgpu->device_id, dev,
dev, K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock),
cvtKlnToC(klninfo->cfg[dev].kline.cfg.temptarget),
cvtKlnToC(klninfo->cfg[dev].kline.cfg.tempcritical),
(int)100*klninfo->cfg[dev].kline.cfg.fantarget/256);
kitem = release_kitem(klncgpu, kitem);
}
}
klondike_get_stats(klncgpu);
klninfo->initialised = true;
for (dev = 0; dev <= slaves; dev++) {
klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].kline.ws.chipcount;
klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].kline.ws.chipcount*2 , sizeof(uint32_t));
}
bool ok = kln_enable(klncgpu);
if (!ok)
applog(LOG_ERR, "%s%i: failed to enable", klncgpu->drv->name, klncgpu->device_id);
return ok;
}
static void control_init(struct cgpu_info *klncgpu)
{
int err, interface;
if (klncgpu->usbinfo.nodev)
return;
interface = usb_interface(klncgpu);
err = usb_transfer(klncgpu, 0, 9, 1, interface, C_RESET);
applog(LOG_DEBUG, "%s%i: reset got err %d",
klncgpu->drv->name, klncgpu->device_id, err);
}
static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
{
struct cgpu_info *klncgpu = usb_alloc_cgpu(&klondike_drv, 1);
struct klondike_info *klninfo = NULL;
KLINE kline;
if (unlikely(!klncgpu))
quit(1, "Failed to calloc klncgpu in klondike_detect_one");
klninfo = calloc(1, sizeof(*klninfo));
if (unlikely(!klninfo))
quit(1, "Failed to calloc klninfo in klondke_detect_one");
klncgpu->device_data = (void *)klninfo;
klninfo->free = new_klist_set(klncgpu);
if (usb_init(klncgpu, dev, found)) {
int sent, recd, err;
KLIST kitem;
int attempts = 0;
control_init(klncgpu);
while (attempts++ < 3) {
kline.hd.cmd = KLN_CMD_IDENT;
kline.hd.dev = 0;
display_send_kline(klncgpu, &kline, msg_detect_send);
err = usb_write(klncgpu, (char *)&(kline.hd), 2, &sent, C_REQUESTRESULTS);
if (err < 0 || sent != 2) {
applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)",
klncgpu->drv->dname,
klncgpu->device_path,
sent, err);
}
cgsleep_ms(REPLY_WAIT_TIME*10);
err = usb_read(klncgpu, (char *)&(kitem.kline), REPLY_SIZE, &recd, C_GETRESULTS);
if (err < 0) {
applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)",
klncgpu->drv->dname,
klncgpu->device_path,
recd, err);
} else if (recd < 1) {
applog(LOG_ERR, "%s (%s) detect empty reply (%d)",
klncgpu->drv->dname,
klncgpu->device_path,
recd);
} else if (kitem.kline.hd.cmd == KLN_CMD_IDENT && kitem.kline.hd.dev == 0) {
display_kline(klncgpu, &kitem.kline, msg_detect_reply);
applog(LOG_DEBUG, "%s (%s) detect successful (%d attempt%s)",
klncgpu->drv->dname,
klncgpu->device_path,
attempts, attempts == 1 ? "" : "s");
if (!add_cgpu(klncgpu))
break;
update_usb_stats(klncgpu);
applog(LOG_DEBUG, "Klondike cgpu added");
rwlock_init(&klninfo->stat_lock);
cglock_init(&klninfo->klist_lock);
return true;
}
}
usb_uninit(klncgpu);
}
free(klninfo->free);
free(klninfo);
free(klncgpu);
return false;
}
static void klondike_detect(bool __maybe_unused hotplug)
{
usb_detect(&klondike_drv, klondike_detect_one);
}
static void klondike_identify(__maybe_unused struct cgpu_info *klncgpu)
{
/*
KLINE kline;
zero_kline(&kline);
kline.hd.cmd = KLN_CMD_IDENT;
kline.hd.dev = 0;
SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
*/
}
static void klondike_check_nonce(struct cgpu_info *klncgpu, KLIST *kitem)
{
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
struct work *work, *look, *tmp;
KLINE *kline = &(kitem->kline);
struct timeval tv_now;
double us_diff;
uint32_t nonce = K_NONCE(kline->wr.nonce) - 0xC0;
applog(LOG_DEBUG, "%s%i:%d FOUND NONCE (%02x:%08x)",
klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
kline->wr.workid, (unsigned int)nonce);
work = NULL;
cgtime(&tv_now);
rd_lock(&(klncgpu->qlock));
HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
if (ms_tdiff(&tv_now, &(look->tv_stamp)) < OLD_WORK_MS &&
(look->subid == (kline->wr.dev*256 + kline->wr.workid))) {
work = look;
break;
}
}
rd_unlock(&(klncgpu->qlock));
if (work) {
wr_lock(&(klninfo->stat_lock));
klninfo->devinfo[kline->wr.dev].noncecount++;
klninfo->noncecount++;
wr_unlock(&(klninfo->stat_lock));
applog(LOG_DEBUG, "%s%i:%d SUBMIT NONCE (%02x:%08x)",
klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
kline->wr.workid, (unsigned int)nonce);
cgtime(&tv_now);
bool ok = submit_nonce(klncgpu->thr[0], work, nonce);
applog(LOG_DEBUG, "%s%i:%d chip stats %d, %08x, %d, %d",
klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
kline->wr.dev, (unsigned int)nonce,
klninfo->devinfo[kline->wr.dev].rangesize,
klninfo->status[kline->wr.dev].kline.ws.chipcount);
klninfo->devinfo[kline->wr.dev].chipstats[(nonce / klninfo->devinfo[kline->wr.dev].rangesize) + (ok ? 0 : klninfo->status[kline->wr.dev].kline.ws.chipcount)]++;
us_diff = us_tdiff(&tv_now, &(kitem->tv_when));
if (klninfo->delay_count == 0) {
klninfo->delay_min = us_diff;
klninfo->delay_max = us_diff;
} else {
if (klninfo->delay_min > us_diff)
klninfo->delay_min = us_diff;
if (klninfo->delay_max < us_diff)
klninfo->delay_max = us_diff;
}
klninfo->delay_count++;
klninfo->delay_total += us_diff;
if (klninfo->nonce_count > 0) {
us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
if (klninfo->nonce_count == 1) {
klninfo->nonce_min = us_diff;
klninfo->nonce_max = us_diff;
} else {
if (klninfo->nonce_min > us_diff)
klninfo->nonce_min = us_diff;
if (klninfo->nonce_max < us_diff)
klninfo->nonce_max = us_diff;
}
klninfo->nonce_total += us_diff;
}
klninfo->nonce_count++;
memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
sizeof(klninfo->tv_last_nonce_received));
return;
}
applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
kline->wr.workid, (unsigned int)nonce);
//inc_hw_errors(klncgpu->thr[0]);
}
// thread to keep looking for replies
static void *klondike_get_replies(void *userdata)
{
struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
KLIST *kitem = NULL;
char *hexdata;
int err, recd, slaves, dev, isc;
bool overheat, sent;
applog(LOG_DEBUG, "%s%i: listening for replies",
klncgpu->drv->name, klncgpu->device_id);
while (klncgpu->shutdown == false) {
if (klncgpu->usbinfo.nodev)
return NULL;
if (kitem == NULL)
kitem = allocate_kitem(klncgpu);
else
memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
err = usb_read(klncgpu, (char *)&(kitem->kline), REPLY_SIZE, &recd, C_GETRESULTS);
if (err || recd != REPLY_SIZE) {
if (err != -7)
applog(LOG_ERR, "%s%i: reply err=%d amt=%d",
klncgpu->drv->name, klncgpu->device_id,
err, recd);
}
if (!err && recd == REPLY_SIZE) {
cgtime(&(kitem->tv_when));
rd_lock(&(klninfo->stat_lock));
kitem->block_seq = klninfo->block_seq;
rd_unlock(&(klninfo->stat_lock));
if (opt_log_level <= READ_DEBUG) {
hexdata = bin2hex((unsigned char *)&(kitem->kline.hd.dev), recd-1);
applog(READ_DEBUG, "%s%i:%d reply [%c:%s]",
klncgpu->drv->name, klncgpu->device_id,
(int)(kitem->kline.hd.dev),
kitem->kline.hd.cmd, hexdata);
free(hexdata);
}
// We can't check this until it's initialised
if (klninfo->initialised) {
rd_lock(&(klninfo->stat_lock));
slaves = klninfo->status[0].kline.ws.slavecount;
rd_unlock(&(klninfo->stat_lock));
if (kitem->kline.hd.dev > slaves) {
applog(LOG_ERR, "%s%i: reply [%c] has invalid dev=%d (max=%d) using 0",
klncgpu->drv->name, klncgpu->device_id,
(char)(kitem->kline.hd.cmd),
(int)(kitem->kline.hd.dev),
slaves);
/* TODO: this is rather problematic if there are slaves
* however without slaves - it should always be zero */
kitem->kline.hd.dev = 0;
} else {
wr_lock(&(klninfo->stat_lock));