forked from luke-jr/bfgminer
-
Notifications
You must be signed in to change notification settings - Fork 10
/
driver-bitforce.c
2660 lines (2330 loc) · 72.1 KB
/
driver-bitforce.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 Luke Dashjr
* Copyright 2012 Con Kolivas
*
* 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 <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include <unistd.h>
#include "compat.h"
#include "deviceapi.h"
#include "miner.h"
#include "lowlevel.h"
#include "lowl-pci.h"
#include "lowl-vcom.h"
#include "util.h"
#define BFL_PCI_VENDOR_ID 0x1cf9
#define BITFORCE_SLEEP_MS 500
#define BITFORCE_VCOM_TIMEOUT_DSEC 250
#define BITFORCE_VCOM_TIMEOUT_DSEC_ZCX 10
#define BITFORCE_TIMEOUT_S 7
#define BITFORCE_TIMEOUT_MS (BITFORCE_TIMEOUT_S * 1000)
#define BITFORCE_LONG_TIMEOUT_S 25
#define BITFORCE_LONG_TIMEOUT_MS (BITFORCE_LONG_TIMEOUT_S * 1000)
#define BITFORCE_CHECK_INTERVAL_MS 10
#define WORK_CHECK_INTERVAL_MS 50
#define MAX_START_DELAY_MS 100
#define tv_to_ms(tval) ((unsigned long)(tval.tv_sec * 1000 + tval.tv_usec / 1000))
#define TIME_AVG_CONSTANT 8
#define BITFORCE_QRESULT_LINE_LEN 165
#define BITFORCE_MAX_QUEUED_MAX 40
#define BITFORCE_MIN_QUEUED_MAX 10
#define BITFORCE_MAX_QRESULTS 16
#define BITFORCE_GOAL_QRESULTS 5
#define BITFORCE_MIN_QRESULT_WAIT BITFORCE_CHECK_INTERVAL_MS
#define BITFORCE_MAX_QRESULT_WAIT 1000
#define BITFORCE_MAX_BQUEUE_AT_ONCE_65NM 5
#define BITFORCE_MAX_BQUEUE_AT_ONCE_28NM 20
enum bitforce_proto {
BFP_WORK = 0,
BFP_RANGE = 1,
BFP_BQUEUE = 3,
BFP_PQUEUE = 4,
};
static const char *protonames[] = {
"full work",
"nonce range",
NULL,
"bulk queue",
"parallel queue",
};
BFG_REGISTER_DRIVER(bitforce_drv)
BFG_REGISTER_DRIVER(bitforce_queue_api)
static const struct bfg_set_device_definition bitforce_set_device_funcs[];
enum bitforce_style {
BFS_FPGA,
BFS_65NM,
BFS_28NM,
};
struct bitforce_lowl_interface {
bool (*open)(struct cgpu_info *);
void (*close)(struct cgpu_info *);
ssize_t (*read)(void *, size_t, struct cgpu_info *);
void (*gets)(char *, size_t, struct cgpu_info *);
ssize_t (*write)(struct cgpu_info *, const void *, ssize_t);
bool (*set_timeout)(struct cgpu_info* , uint8_t);
};
struct bitforce_data {
struct bitforce_lowl_interface *lowlif;
bool is_open;
struct lowl_pci_handle *lph;
uint8_t lasttag;
bytes_t getsbuf;
int xlink_id;
unsigned char next_work_ob[70]; // Data aligned for 32-bit access
unsigned char *next_work_obs; // Start of data to send
unsigned char next_work_obsz;
const char *next_work_cmd;
char noncebuf[14 + ((BITFORCE_MAX_QRESULTS+1) * BITFORCE_QRESULT_LINE_LEN)];
int poll_func;
enum bitforce_proto proto;
enum bitforce_style style;
int queued;
int queued_max;
int parallel;
bool parallel_protocol;
bool missing_zwx;
bool already_have_results;
bool just_flushed;
int max_queue_at_once;
int ready_to_queue;
bool want_to_send_queue;
unsigned result_busy_polled;
unsigned sleep_ms_default;
struct timeval tv_hashmeter_start;
float temp[2];
long *volts;
int volts_count;
unsigned max_queueid;
bool probed;
bool supports_fanspeed;
};
// Code must deal with a timeout
static
bool bitforce_vcom_open(struct cgpu_info * const dev)
{
struct bitforce_data * const devdata = dev->device_data;
const char * const devpath = dev->device_path;
dev->device_fd = serial_open(devpath, 0, BITFORCE_VCOM_TIMEOUT_DSEC, true);
devdata->is_open = (dev->device_fd != -1);
return devdata->is_open;
}
static
void bitforce_vcom_close(struct cgpu_info * const dev)
{
struct bitforce_data * const devdata = dev->device_data;
if (devdata->is_open)
{
serial_close(dev->device_fd);
dev->device_fd = -1;
devdata->is_open = false;
}
}
static
ssize_t bitforce_vcom_read(void * const buf_p, size_t bufLen, struct cgpu_info * const dev)
{
uint8_t *buf = buf_p;
const int fd = dev->device_fd;
ssize_t rv, ret = 0;
while (bufLen > 0)
{
rv = read(fd, buf, bufLen);
if (rv <= 0)
{
if (ret > 0)
return ret;
return rv;
}
buf += rv;
bufLen -= rv;
ret += rv;
}
return ret;
}
static
void bitforce_vcom_gets(char *buf, size_t bufLen, struct cgpu_info * const dev)
{
const int fd = dev->device_fd;
while (likely(bufLen > 1 && read(fd, buf, 1) == 1 && (buf++)[0] != '\n'))
{}
buf[0] = '\0';
}
static
ssize_t bitforce_vcom_write(struct cgpu_info * const dev, const void *buf, ssize_t bufLen)
{
const int fd = dev->device_fd;
if ((bufLen) != write(fd, buf, bufLen))
return 0;
else
return bufLen;
}
static
bool bitforce_vcom_set_timeout(struct cgpu_info * const dev, const uint8_t timeout)
{
const int fd = dev->device_fd;
return vcom_set_timeout(fd, timeout);
}
static struct bitforce_lowl_interface bfllif_vcom = {
.open = bitforce_vcom_open,
.close = bitforce_vcom_close,
.read = bitforce_vcom_read,
.gets = bitforce_vcom_gets,
.write = bitforce_vcom_write,
.set_timeout = bitforce_vcom_set_timeout,
};
#ifdef NEED_BFG_LOWL_PCI
static
bool bitforce_pci_open(struct cgpu_info * const dev)
{
const char * const devpath = dev->device_path;
struct bitforce_data * const devdata = dev->device_data;
devdata->lph = lowl_pci_open(devpath, LP_BARINFO(
LP_BAR(0, 0x1000, O_WRONLY),
LP_BAR(1, 0x1000, O_RDONLY),
LP_BAR(2, 0x80, O_RDWR),
));
if (!devdata->lph)
return false;
devdata->lasttag = (lowl_pci_get_word(devdata->lph, 2, 2) >> 16) & 0xff;
devdata->is_open = true;
return devdata->is_open;
}
static
void bitforce_pci_close(struct cgpu_info * const dev)
{
struct bitforce_data * const devdata = dev->device_data;
if (devdata->is_open)
{
lowl_pci_close(devdata->lph);
devdata->is_open = false;
}
}
static
void _bitforce_pci_read(struct cgpu_info * const dev)
{
struct bitforce_data * const devdata = dev->device_data;
const uint32_t looking_for = (uint32_t)devdata->lasttag << 0x10;
uint32_t resp;
bytes_t *b = &devdata->getsbuf;
if (!bytes_len(&devdata->getsbuf))
{
while (((resp = lowl_pci_get_word(devdata->lph, 2, 2)) & 0xff0000) != looking_for)
cgsleep_ms(1);
resp &= 0xffff;
if (unlikely(resp > 0x1000))
resp = 0x1000;
void * const buf = bytes_preappend(b, resp + LOWL_PCI_GET_DATA_PADDING);
if (lowl_pci_read_data(devdata->lph, buf, resp, 1, 0))
bytes_postappend(b, resp);
}
}
static
ssize_t bitforce_pci_read(void * const buf, const size_t bufLen, struct cgpu_info * const dev)
{
struct bitforce_data * const devdata = dev->device_data;
bytes_t *b = &devdata->getsbuf;
_bitforce_pci_read(dev);
ssize_t datalen = bytes_len(b);
if (datalen <= 0)
return datalen;
if (datalen > bufLen)
datalen = bufLen;
memcpy(buf, bytes_buf(b), datalen);
bytes_shift(b, datalen);
return datalen;
}
static
void bitforce_pci_gets(char * const buf, size_t bufLen, struct cgpu_info * const dev)
{
struct bitforce_data * const devdata = dev->device_data;
bytes_t *b = &devdata->getsbuf;
_bitforce_pci_read(dev);
ssize_t linelen = (bytes_find(b, '\n') + 1) ?: bytes_len(b);
if (linelen > --bufLen)
linelen = bufLen;
memcpy(buf, bytes_buf(b), linelen);
bytes_shift(b, linelen);
buf[linelen] = '\0';
}
static
ssize_t bitforce_pci_write(struct cgpu_info * const dev, const void * const bufp, ssize_t bufLen)
{
const uint8_t *buf = bufp;
struct bitforce_data * const devdata = dev->device_data;
if (unlikely(bufLen > 0x1000))
return 0;
if (!lowl_pci_set_data(devdata->lph, buf, bufLen, 0, 0))
return 0;
if (++devdata->lasttag == 0)
++devdata->lasttag;
if (!lowl_pci_set_word(devdata->lph, 2, 0, ((uint32_t)devdata->lasttag << 0x10) | bufLen))
return 0;
return bufLen;
}
static struct bitforce_lowl_interface bfllif_pci = {
.open = bitforce_pci_open,
.close = bitforce_pci_close,
.read = bitforce_pci_read,
.gets = bitforce_pci_gets,
.write = bitforce_pci_write,
};
#endif
static
void bitforce_close(struct cgpu_info * const proc)
{
struct cgpu_info * const dev = proc->device;
struct bitforce_data * const devdata = dev->device_data;
if (devdata->is_open)
devdata->lowlif->close(dev);
}
static
bool bitforce_open(struct cgpu_info * const proc)
{
struct cgpu_info * const dev = proc->device;
struct bitforce_data * const devdata = dev->device_data;
bitforce_close(proc);
return devdata->lowlif->open(dev);
}
static
ssize_t bitforce_read(struct cgpu_info * const proc, void * const buf, const size_t bufLen)
{
struct cgpu_info * const dev = proc->device;
struct bitforce_data * const devdata = dev->device_data;
ssize_t rv;
if (likely(devdata->is_open))
rv = devdata->lowlif->read(buf, bufLen, dev);
else
rv = -1;
if (unlikely(opt_dev_protocol))
{
size_t datalen = (rv > 0) ? rv : 0;
char hex[(rv * 2) + 1];
bin2hex(hex, buf, datalen);
applog(LOG_DEBUG, "DEVPROTO: %s: READ(%lu): %s",
dev->dev_repr, (unsigned long)bufLen, hex);
}
return rv;
}
static
void bitforce_gets(char * const buf, const size_t bufLen, struct cgpu_info * const proc)
{
struct cgpu_info * const dev = proc->device;
struct bitforce_data * const devdata = dev->device_data;
if (likely(devdata->is_open))
devdata->lowlif->gets(buf, bufLen, dev);
else
buf[0] = '\0';
if (unlikely(opt_dev_protocol))
applog(LOG_DEBUG, "DEVPROTO: %s: GETS: %s", dev->dev_repr, buf);
}
static
ssize_t bitforce_write(struct cgpu_info * const proc, const void * const buf, const ssize_t bufLen)
{
struct cgpu_info * const dev = proc->device;
struct bitforce_data * const devdata = dev->device_data;
if (unlikely(!devdata->is_open))
return 0;
return devdata->lowlif->write(dev, buf, bufLen);
}
static ssize_t bitforce_send(struct cgpu_info * const proc, const void *buf, ssize_t bufLen)
{
struct bitforce_data * const data = proc->device_data;
const int procid = data->xlink_id;
if (!procid)
return bitforce_write(proc, buf, bufLen);
if (bufLen > 255)
return -1;
size_t bufLeft = bufLen + 3;
char realbuf[bufLeft], *bufp;
ssize_t rv;
memcpy(&realbuf[3], buf, bufLen);
realbuf[0] = '@';
realbuf[1] = bufLen;
realbuf[2] = procid;
bufp = realbuf;
do
{
rv = bitforce_write(proc, bufp, bufLeft);
if (rv <= 0)
return rv;
bufLeft -= rv;
}
while (bufLeft > 0);
return bufLen;
}
static
void bitforce_cmd1b(struct cgpu_info * const proc, void *buf, size_t bufsz, const char *cmd, size_t cmdsz)
{
if (unlikely(opt_dev_protocol))
applog(LOG_DEBUG, "DEVPROTO: %"PRIpreprv": CMD1: %s",
proc->proc_repr, cmd);
bitforce_send(proc, cmd, cmdsz);
bitforce_gets(buf, bufsz, proc);
}
static
void bitforce_cmd1c(struct cgpu_info * const proc, void *buf, size_t bufsz, void *cmd, size_t cmdsz)
{
if (unlikely(opt_dev_protocol))
{
char hex[(cmdsz * 2) + 1];
bin2hex(hex, cmd, cmdsz);
applog(LOG_DEBUG, "DEVPROTO: %"PRIpreprv": CMD1 HEX: %s",
proc->proc_repr, hex);
}
bitforce_send(proc, cmd, cmdsz);
bitforce_gets(buf, bufsz, proc);
}
static
void bitforce_cmd2(struct cgpu_info * const proc, void *buf, size_t bufsz, const char *cmd, void *data, size_t datasz)
{
bitforce_cmd1b(proc, buf, bufsz, cmd, 3);
if (strncasecmp(buf, "OK", 2))
return;
if (unlikely(opt_dev_protocol))
{
char hex[(datasz * 2) + 1];
bin2hex(hex, data, datasz);
applog(LOG_DEBUG, "DEVPROTO: %"PRIpreprv": CMD2: %s",
proc->proc_repr, hex);
}
bitforce_send(proc, data, datasz);
bitforce_gets(buf, bufsz, proc);
}
static
void bitforce_zgx(struct cgpu_info * const proc, void *buf, size_t bufsz)
{
struct cgpu_info * const dev = proc->device;
struct bitforce_data * const devdata = dev->device_data;
if (devdata->is_open && devdata->lowlif->set_timeout)
{
devdata->lowlif->set_timeout(dev, BITFORCE_VCOM_TIMEOUT_DSEC_ZCX);
bitforce_cmd1b(proc, buf, bufsz, "ZGX", 3);
devdata->lowlif->set_timeout(dev, BITFORCE_VCOM_TIMEOUT_DSEC);
}
else
bitforce_cmd1b(proc, buf, bufsz, "ZGX", 3);
}
struct bitforce_init_data {
struct bitforce_lowl_interface *lowlif;
enum bitforce_style style;
long devmask;
int *parallels;
unsigned queue_depth;
unsigned long scan_interval_ms;
unsigned max_queueid;
};
static
int bitforce_chips_to_plan_for(int parallel, int chipcount) {
if (parallel < 1)
return parallel;
return upper_power_of_two_u32(chipcount);
}
static
bool bitforce_lowl_match(const struct lowlevel_device_info * const info)
{
#ifdef NEED_BFG_LOWL_PCI
if (info->lowl == &lowl_pci)
return info->vid == BFL_PCI_VENDOR_ID;
#endif
return lowlevel_match_product(info, "BitFORCE", "SHA256");
}
char *bitforce_copy_name(char * const pdevbuf)
{
char *s;
if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
{
s[0] = '\0';
s = strdup(&pdevbuf[7]);
}
else
{
for (s = &pdevbuf[strlen(pdevbuf)]; isspace(*--s); )
*s = '\0';
s = strdup(pdevbuf);
}
return s;
}
static
bool bitforce_detect_oneof(const char * const devpath, struct bitforce_lowl_interface * const lowlif)
{
struct cgpu_info *bitforce;
char pdevbuf[0x100];
size_t pdevbuf_len;
char *s;
int procs = 1, parallel = -1;
long maxchipno = 0;
struct bitforce_init_data *initdata;
char *manuf = NULL;
struct bitforce_data dummy_bfdata = {
.lowlif = lowlif,
.xlink_id = 0,
};
struct cgpu_info dummy_cgpu = {
.device = &dummy_cgpu,
.dev_repr = "BFL",
.proc_repr = "BFL",
.device_path = devpath,
.device_data = &dummy_bfdata,
};
dummy_cgpu.device_fd = -1;
applog(LOG_DEBUG, "BFL: Attempting to open %s", devpath);
bitforce_open(&dummy_cgpu);
if (unlikely(!dummy_bfdata.is_open)) {
applog(LOG_DEBUG, "BFL: Failed to open %s", devpath);
return false;
}
bitforce_zgx(&dummy_cgpu, pdevbuf, sizeof(pdevbuf));
if (unlikely(!pdevbuf[0])) {
applog(LOG_DEBUG, "BFL: Error reading/timeout (ZGX)");
bitforce_close(&dummy_cgpu);
return 0;
}
if (unlikely(!strstr(pdevbuf, "SHA256"))) {
applog(LOG_DEBUG, "BFL: Didn't recognise BitForce on %s", devpath);
bitforce_close(&dummy_cgpu);
return false;
}
if (serial_claim_v(devpath, &bitforce_drv))
{
bitforce_close(&dummy_cgpu);
return false;
}
applog(LOG_DEBUG, "Found BitForce device on %s", devpath);
s = bitforce_copy_name(pdevbuf);
initdata = malloc(sizeof(*initdata));
*initdata = (struct bitforce_init_data){
.lowlif = lowlif,
.style = BFS_FPGA,
.queue_depth = BITFORCE_MAX_QUEUED_MAX,
};
bitforce_cmd1b(&dummy_cgpu, pdevbuf, sizeof(pdevbuf), "ZCX", 3);
for (int i = 0; (!pdevbuf[0]) && i < 4; ++i)
bitforce_gets(pdevbuf, sizeof(pdevbuf), &dummy_cgpu);
for ( ;
strncasecmp(pdevbuf, "OK", 2);
bitforce_gets(pdevbuf, sizeof(pdevbuf), &dummy_cgpu) )
{
pdevbuf_len = strlen(pdevbuf);
if (unlikely(!pdevbuf_len))
continue;
pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
applog(LOG_DEBUG, " %s", pdevbuf);
if (!strncasecmp(pdevbuf, "PROCESSOR ", 10))
maxchipno = max(maxchipno, atoi(&pdevbuf[10]));
else
if (!strncasecmp(pdevbuf, "CHANNEL", 7))
maxchipno = max(maxchipno, atoi(&pdevbuf[7]));
else
if (!strncasecmp(pdevbuf, "CORTEX-", 7))
maxchipno = max(maxchipno, strtol(&pdevbuf[7], NULL, 0x10));
else
if (!strncasecmp(pdevbuf, "DEVICES IN CHAIN:", 17))
procs = atoi(&pdevbuf[17]);
else
if (!strncasecmp(pdevbuf, "CHAIN PRESENCE MASK:", 20))
initdata->devmask = strtol(&pdevbuf[20], NULL, 16);
else
if (!strncasecmp(pdevbuf, "DEVICE:", 7) && strstr(pdevbuf, "SC") && initdata->style == BFS_FPGA)
initdata->style = BFS_65NM;
else
if (!strncasecmp(pdevbuf, "CHIP PARALLELIZATION: YES @", 27))
parallel = atoi(&pdevbuf[27]);
else
if (!strncasecmp(pdevbuf, "ASIC CHANNELS:", 14))
{
parallel = atoi(&pdevbuf[14]);
initdata->style = BFS_28NM;
}
else
if (!strncasecmp(pdevbuf, "Queue Depth:", 12))
initdata->queue_depth = atoi(&pdevbuf[12]);
else
if (!strncasecmp(pdevbuf, "Scan Interval:", 14))
initdata->scan_interval_ms = atoi(&pdevbuf[14]);
else
if (!strncasecmp(pdevbuf, "Max Queue ID:", 13))
initdata->max_queueid = strtol(&pdevbuf[13], NULL, 0x10);
else
if (!strncasecmp(pdevbuf, "MANUFACTURER:", 13))
{
manuf = &pdevbuf[13];
while (manuf[0] && isspace(manuf[0]))
++manuf;
if (manuf[0])
manuf = strdup(manuf);
else
manuf = NULL;
}
}
parallel = bitforce_chips_to_plan_for(parallel, maxchipno);
initdata->parallels = malloc(sizeof(initdata->parallels[0]) * procs);
initdata->parallels[0] = parallel;
parallel = abs(parallel);
for (int proc = 1; proc < procs; ++proc)
{
applog(LOG_DEBUG, "Slave board %d:", proc);
initdata->parallels[proc] = -1;
maxchipno = 0;
bitforce_cmd1b(&dummy_cgpu, pdevbuf, sizeof(pdevbuf), "ZCX", 3);
for (int i = 0; (!pdevbuf[0]) && i < 4; ++i)
bitforce_gets(pdevbuf, sizeof(pdevbuf), &dummy_cgpu);
for ( ;
strncasecmp(pdevbuf, "OK", 2);
bitforce_gets(pdevbuf, sizeof(pdevbuf), &dummy_cgpu) )
{
pdevbuf_len = strlen(pdevbuf);
if (unlikely(!pdevbuf_len))
continue;
pdevbuf[pdevbuf_len-1] = '\0'; // trim newline
applog(LOG_DEBUG, " %s", pdevbuf);
if (!strncasecmp(pdevbuf, "PROCESSOR ", 10))
maxchipno = max(maxchipno, atoi(&pdevbuf[10]));
else
if (!strncasecmp(pdevbuf, "CHIP PARALLELIZATION: YES @", 27))
initdata->parallels[proc] = atoi(&pdevbuf[27]);
}
initdata->parallels[proc] = bitforce_chips_to_plan_for(initdata->parallels[proc], maxchipno);
parallel += abs(initdata->parallels[proc]);
}
bitforce_close(&dummy_cgpu);
if (unlikely((procs != 1 || parallel != 1) && initdata->style == BFS_FPGA))
{
// Only bitforce_queue supports parallelization and XLINK, so force SC mode and hope for the best
applog(LOG_WARNING, "SC features detected with non-SC device; this is not supported!");
initdata->style = BFS_65NM;
}
// We have a real BitForce!
bitforce = calloc(1, sizeof(*bitforce));
bitforce->drv = &bitforce_drv;
if (initdata->style != BFS_FPGA)
bitforce->drv = &bitforce_queue_api;
bitforce->device_path = strdup(devpath);
if (manuf)
bitforce->dev_manufacturer = manuf;
bitforce->deven = DEV_ENABLED;
bitforce->procs = parallel;
bitforce->threads = 1;
if (initdata->style != BFS_FPGA)
bitforce->cutofftemp = 85;
if (s)
bitforce->name = s;
bitforce->device_data = initdata;
// Skip fanspeed until we probe support for it
bitforce->set_device_funcs = &bitforce_set_device_funcs[1];
mutex_init(&bitforce->device_mutex);
return add_cgpu(bitforce);
}
static
bool bitforce_detect_one(const char * const devpath)
{
return bitforce_detect_oneof(devpath, &bfllif_vcom);
}
static
bool bitforce_lowl_probe(const struct lowlevel_device_info * const info)
{
#ifdef NEED_BFG_LOWL_PCI
if (info->lowl == &lowl_pci)
return bitforce_detect_oneof(info->path, &bfllif_pci);
#endif
return vcom_lowl_probe_wrapper(info, bitforce_detect_one);
}
struct bitforce_proc_data {
struct cgpu_info *cgpu;
bool handles_board; // The first processor handles the queue for the entire board
};
static void bitforce_clear_buffer(struct cgpu_info *);
static
void bitforce_comm_error(struct thr_info *thr)
{
struct cgpu_info *bitforce = thr->cgpu;
struct bitforce_data *data = bitforce->device_data;
data->noncebuf[0] = '\0';
applog(LOG_ERR, "%"PRIpreprv": Comms error", bitforce->proc_repr);
dev_error(bitforce, REASON_DEV_COMMS_ERROR);
inc_hw_errors_only(thr);
if (!bitforce_open(bitforce))
{
applog(LOG_ERR, "%s: Error reopening %s", bitforce->dev_repr, bitforce->device_path);
return;
}
/* empty read buffer */
bitforce_clear_buffer(bitforce);
}
static
void __bitforce_clear_buffer(struct cgpu_info * const dev)
{
char pdevbuf[0x100];
int count = 0;
do {
pdevbuf[0] = '\0';
bitforce_gets(pdevbuf, sizeof(pdevbuf), dev);
} while (pdevbuf[0] && (++count < 10));
}
static void bitforce_clear_buffer(struct cgpu_info *bitforce)
{
struct cgpu_info * const dev = bitforce->device;
struct bitforce_data * const devdata = dev->device_data;
pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
mutex_lock(mutexp);
if (devdata->is_open)
{
applog(LOG_DEBUG, "%"PRIpreprv": Clearing read buffer", bitforce->proc_repr);
__bitforce_clear_buffer(bitforce);
}
mutex_unlock(mutexp);
}
void work_list_del(struct work **head, struct work *);
void bitforce_reinit(struct cgpu_info *bitforce)
{
struct cgpu_info * const dev = bitforce->device;
struct bitforce_data * const devdata = dev->device_data;
struct bitforce_data *data = bitforce->device_data;
struct thr_info *thr = bitforce->thr[0];
struct bitforce_proc_data *procdata = thr->cgpu_data;
const char *devpath = bitforce->device_path;
pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
int retries = 0;
char pdevbuf[0x100];
if (!procdata->handles_board)
return;
mutex_lock(mutexp);
applog(LOG_WARNING, "%"PRIpreprv": Re-initialising", bitforce->proc_repr);
if (devdata->is_open)
{
bitforce_close(bitforce);
cgsleep_ms(5000);
}
bitforce_open(bitforce);
if (unlikely(!devdata->is_open)) {
mutex_unlock(mutexp);
applog(LOG_ERR, "%s: Failed to open %s", bitforce->dev_repr, devpath);
return;
}
__bitforce_clear_buffer(bitforce);
do {
bitforce_zgx(bitforce, pdevbuf, sizeof(pdevbuf));
if (unlikely(!pdevbuf[0])) {
mutex_unlock(mutexp);
bitforce_close(bitforce);
applog(LOG_ERR, "%s: Error reading/timeout (ZGX)", bitforce->dev_repr);
return;
}
if (retries++)
cgsleep_ms(10);
} while (strstr(pdevbuf, "BUSY") && (retries * 10 < BITFORCE_TIMEOUT_MS));
if (unlikely(!strstr(pdevbuf, "SHA256"))) {
mutex_unlock(mutexp);
bitforce_close(bitforce);
applog(LOG_ERR, "%s: Didn't recognise BitForce on %s returned: %s", bitforce->dev_repr, devpath, pdevbuf);
return;
}
free((void*)bitforce->name);
bitforce->name = bitforce_copy_name(pdevbuf);
bitforce->sleep_ms = data->sleep_ms_default;
if (bitforce->drv == &bitforce_queue_api)
{
struct work *work, *tmp;
timer_set_delay_from_now(&thr->tv_poll, 0);
notifier_wake(thr->notifier);
bitforce_cmd1b(bitforce, pdevbuf, sizeof(pdevbuf), "ZQX", 3);
DL_FOREACH_SAFE(thr->work_list, work, tmp)
work_list_del(&thr->work_list, work);
data->queued = 0;
data->ready_to_queue = 0;
data->already_have_results = false;
data->just_flushed = true;
thr->queue_full = false;
}
mutex_unlock(mutexp);
}
static void bitforce_flash_led(struct cgpu_info *bitforce)
{
struct cgpu_info * const dev = bitforce->device;
struct bitforce_data * const devdata = dev->device_data;
pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
if (unlikely(!devdata->is_open))
return;
/* Do not try to flash the led if we're polling for a result to
* minimise the chance of interleaved results */
if (bitforce->polling)
return;
/* It is not critical flashing the led so don't get stuck if we
* can't grab the mutex here */
if (mutex_trylock(mutexp))
return;
char pdevbuf[0x100];
bitforce_cmd1b(bitforce, pdevbuf, sizeof(pdevbuf), "ZMX", 3);
/* Once we've tried - don't do it until told to again */
bitforce->flash_led = false;
/* However, this stops anything else getting a reply
* So best to delay any other access to the BFL */
cgsleep_ms(4000);
mutex_unlock(mutexp);
return; // nothing is returned by the BFL
}
static
float my_strtof(const char *nptr, char **endptr)
{
float f = strtof(nptr, endptr);
/* Cope with older software that breaks and reads nonsense
* values */
if (f > 100)
f = strtod(nptr, endptr);
return f;
}
static
void set_float_if_gt_zero(float *var, float value)
{
if (value > 0)
*var = value;
}
static bool bitforce_get_temp(struct cgpu_info *bitforce)
{
struct cgpu_info * const dev = bitforce->device;
struct bitforce_data * const devdata = dev->device_data;
struct bitforce_data *data = bitforce->device_data;
pthread_mutex_t *mutexp = &bitforce->device->device_mutex;
char pdevbuf[0x40];
char voltbuf[0x40];
char *s;
struct cgpu_info *chip_cgpu;
if (unlikely(!devdata->is_open))
return false;
/* Do not try to get the temperature if we're polling for a result to
* minimise the chance of interleaved results */
if (bitforce->polling)
return true;
// Flash instead of Temp - doing both can be too slow
if (bitforce->flash_led) {
bitforce_flash_led(bitforce);
return true;
}
/* It is not critical getting temperature so don't get stuck if we
* can't grab the mutex here */
if (mutex_trylock(mutexp))
return false;
if (data->style != BFS_FPGA)
{
if (unlikely(!data->probed))
{
bitforce_cmd1b(bitforce, voltbuf, sizeof(voltbuf), "Z9X", 3);
if (strncasecmp(voltbuf, "ERR", 3))
{
data->supports_fanspeed = true;
bitforce->set_device_funcs = bitforce_set_device_funcs;
}
data->probed = true;
}
bitforce_cmd1b(bitforce, voltbuf, sizeof(voltbuf), "ZTX", 3);
}
bitforce_cmd1b(bitforce, pdevbuf, sizeof(pdevbuf), "ZLX", 3);
mutex_unlock(mutexp);
if (data->style != BFS_FPGA && likely(voltbuf[0]))
{
// Process voltage info
// "NNNxxx,NNNxxx,NNNxxx"
int n = 1;
for (char *p = voltbuf; p[0]; ++p)
if (p[0] == ',')
++n;
long *out = malloc(sizeof(long) * n);
if (!out)
goto skipvolts;
n = 0;
char *saveptr, *v;
for (v = strtok_r(voltbuf, ",", &saveptr); v; v = strtok_r(NULL, ",", &saveptr))
out[n++] = strtol(v, NULL, 10);
data->volts_count = 0;
free(data->volts);
data->volts = out;
data->volts_count = n;
}
skipvolts:
if (unlikely(!pdevbuf[0])) {
struct thr_info *thr = bitforce->thr[0];
applog(LOG_ERR, "%"PRIpreprv": Error: Get temp returned empty string/timed out", bitforce->proc_repr);
inc_hw_errors_only(thr);
return false;
}