-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver-bflsc.c
1871 lines (1579 loc) · 50.8 KB
/
driver-bflsc.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
*
* 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 "config.h"
#ifdef WIN32
#include <windows.h>
#endif
#include "compat.h"
#include "miner.h"
#include "usbutils.h"
#define BLANK ""
#define LFSTR "<LF>"
/*
* With Firmware 1.0.0 and a result queue of 20 the Max is:
* header = 9
* 64+1+32+1+1+(1+8)*8+1 per line = 172 * 20
* OK = 3
* Total: 3452
*/
#define BFLSC_BUFSIZ (0x1000)
#define BFLSC_DI_FIRMWARE "FIRMWARE"
#define BFLSC_DI_ENGINES "ENGINES"
#define BFLSC_DI_JOBSINQUE "JOBS IN QUEUE"
#define BFLSC_DI_XLINKMODE "XLINK MODE"
#define BFLSC_DI_XLINKPRESENT "XLINK PRESENT"
#define BFLSC_DI_DEVICESINCHAIN "DEVICES IN CHAIN"
#define BFLSC_DI_CHAINPRESENCE "CHAIN PRESENCE MASK"
#define FULLNONCE 0x100000000ULL
struct bflsc_dev {
// Work
unsigned int ms_work;
int work_queued;
int work_complete;
int nonces_hw; // TODO: this - need to add a paramter to submit_nonce()
// so can pass 'dev' to hw_error
uint64_t hashes_unsent;
uint64_t hashes_sent;
uint64_t nonces_found;
struct timeval last_check_result;
struct timeval last_dev_result; // array > 0
struct timeval last_nonce_result; // > 0 nonce
// Info
char getinfo[(BFLSC_BUFSIZ+4)*4];
char *firmware;
int engines; // each engine represents a 'thread' in a chip
char *xlink_mode;
char *xlink_present;
// Status
bool dead; // TODO: handle seperate x-link devices failing?
bool overheat;
// Stats
float temp1;
float temp2;
float vcc1;
float vcc2;
float vmain;
float temp1_max;
float temp2_max;
time_t temp1_max_time;
time_t temp2_max_time;
float temp1_5min_av; // TODO:
float temp2_5min_av; // TODO:
// To handle the fact that flushing the queue may not remove all work
// (normally one item is still being processed)
// and also that once the queue is flushed, results may still be in
// the output queue - but we don't want to process them at the time of doing an LP
// when result_id > flush_id+1, flushed work can be discarded since it
// is no longer in the device
uint64_t flush_id; // counter when results were last flushed
uint64_t result_id; // counter when results were last checked
bool flushed; // are any flushed?
};
// TODO: I stole cgpu_info.device_file
// ... need to update miner.h to instead have a generic void *device_info = NULL;
// ... and these structure definitions need to be in miner.h if API needs to see them
// ... but then again maybe not - maybe another devinfo that the driver provides
// However, clean up all that for all devices in miner.h ... miner.h is a mess at the moment
struct bflsc_info {
pthread_rwlock_t stat_lock;
struct thr_info results_thr;
uint64_t hashes_sent;
uint32_t update_count;
struct timeval last_update;
int sc_count;
struct bflsc_dev *sc_devs;
unsigned int scan_sleep_time;
unsigned int results_sleep_time;
unsigned int default_ms_work;
bool shutdown;
bool flash_led;
bool not_first_work; // allow ignoring the first nonce error
};
#define BFLSC_XLINKHDR '@'
#define BFLSC_MAXPAYLOAD 255
struct DataForwardToChain {
uint8_t header;
uint8_t deviceAddress;
uint8_t payloadSize;
uint8_t payloadData[BFLSC_MAXPAYLOAD];
};
#define DATAFORWARDSIZE(data) (1 + 1 + 1 + data.payloadSize)
#define MIDSTATE_BYTES 32
#define MERKLE_OFFSET 64
#define MERKLE_BYTES 12
#define BFLSC_QJOBSIZ (MIDSTATE_BYTES+MERKLE_BYTES+1)
#define BFLSC_EOB 0xaa
struct QueueJobStructure {
uint8_t payloadSize;
uint8_t midState[MIDSTATE_BYTES];
uint8_t blockData[MERKLE_BYTES];
uint8_t endOfBlock;
};
#define QUE_RES_LINES_MIN 3
#define QUE_MIDSTATE 0
#define QUE_BLOCKDATA 1
#define QUE_NONCECOUNT 2
#define QUE_FLD_MIN 3
#define QUE_FLD_MAX 11
#define BFLSC_SIGNATURE 0xc1
#define BFLSC_EOW 0xfe
// N.B. this will only work with 5 jobs
// requires a different jobs[N] for each job count
// but really only need to handle 5 anyway
struct QueueJobPackStructure {
uint8_t payloadSize;
uint8_t signature;
uint8_t jobsInArray;
struct QueueJobStructure jobs[5];
uint8_t endOfWrapper;
};
// TODO: Implement in API and also in usb device selection
struct SaveString {
uint8_t payloadSize;
uint8_t payloadData[BFLSC_MAXPAYLOAD];
};
// Commands
#define BFLSC_IDENTIFY "ZGX"
#define BFLSC_IDENTIFY_LEN (sizeof(BFLSC_IDENTIFY)-1)
#define BFLSC_DETAILS "ZCX"
#define BFLSC_DETAILS_LEN (sizeof(BFLSC_DETAILS)-1)
#define BFLSC_FIRMWARE "ZJX"
#define BFLSC_FIRMWARE_LEN (sizeof(BFLSC_FIRMWARE)-1)
#define BFLSC_FLASH "ZMX"
#define BFLSC_FLASH_LEN (sizeof(BFLSC_FLASH)-1)
#define BFLSC_VOLTAGE "ZTX"
#define BFLSC_VOLTAGE_LEN (sizeof(BFLSC_VOLTAGE)-1)
#define BFLSC_TEMPERATURE "ZLX"
#define BFLSC_TEMPERATURE_LEN (sizeof(BFLSC_TEMPERATURE)-1)
#define BFLSC_QJOB "ZNX"
#define BFLSC_QJOB_LEN (sizeof(BFLSC_QJOB)-1)
#define BFLSC_QJOBS "ZWX"
#define BFLSC_QJOBS_LEN (sizeof(BFLSC_QJOBS)-1)
#define BFLSC_QRES "ZOX"
#define BFLSC_QRES_LEN (sizeof(BFLSC_QRES)-1)
#define BFLSC_QFLUSH "ZQX"
#define BFLSC_QFLUSH_LEN (sizeof(BFLSC_QFLUSH)-1)
#define BFLSC_FANAUTO "Z5X"
#define BFLSC_FANOUT_LEN (sizeof(BFLSC_FANAUTO)-1)
#define BFLSC_FAN0 "Z0X"
#define BFLSC_FAN0_LEN (sizeof(BFLSC_FAN0)-1)
#define BFLSC_FAN1 "Z1X"
#define BFLSC_FAN1_LEN (sizeof(BFLSC_FAN1)-1)
#define BFLSC_FAN2 "Z2X"
#define BFLSC_FAN2_LEN (sizeof(BFLSC_FAN2)-1)
#define BFLSC_FAN3 "Z3X"
#define BFLSC_FAN3_LEN (sizeof(BFLSC_FAN3)-1)
#define BFLSC_FAN4 "Z4X"
#define BFLSC_FAN4_LEN (sizeof(BFLSC_FAN4)-1)
#define BFLSC_SAVESTR "ZSX"
#define BFLSC_SAVESTR_LEN (sizeof(BFLSC_SAVESTR)-1)
#define BFLSC_LOADSTR "ZUX"
#define BFLSC_LOADSTR_LEN (sizeof(BFLSC_LOADSTR)-1)
// Replies
#define BFLSC_IDENTITY "BitFORCE SC"
#define BFLSC_BFLSC "SHA256 SC"
#define BFLSC_OK "OK\n"
#define BFLSC_OK_LEN (sizeof(BFLSC_OK)-1)
#define BFLSC_SUCCESS "SUCCESS\n"
#define BFLSC_SUCCESS_LEN (sizeof(BFLSC_SUCCESS)-1)
#define BFLSC_RESULT "COUNT:"
#define BFLSC_RESULT_LEN (sizeof(BFLSC_RESULT)-1)
#define BFLSC_ANERR "ERR:"
#define BFLSC_ANERR_LEN (sizeof(BFLSC_ANERR)-1)
#define BFLSC_TIMEOUT BFLSC_ANERR "TIMEOUT"
#define BFLSC_TIMEOUT_LEN (sizeof(BFLSC_TIMEOUT)-1)
#define BFLSC_INVALID BFLSC_ANERR "INVALID DATA"
#define BFLSC_INVALID_LEN (sizeof(BFLSC_INVALID)-1)
#define BFLSC_ERRSIG BFLSC_ANERR "SIGNATURE"
#define BFLSC_ERRSIG_LEN (sizeof(BFLSC_ERRSIG)-1)
#define BFLSC_OKQ "OK:QUEUED"
#define BFLSC_OKQ_LEN (sizeof(BFLSC_OKQ)-1)
// Followed by N=1..5
#define BFLSC_OKQN "OK:QUEUED "
#define BFLSC_OKQN_LEN (sizeof(BFLSC_OKQN)-1)
#define BFLSC_QFULL "QUEUE FULL"
#define BFLSC_QFULL_LEN (sizeof(BFLSC_QFULL)-1)
#define BFLSC_HITEMP "HIGH TEMPERATURE RECOVERY"
#define BFLSC_HITEMP_LEN (sizeof(BFLSC_HITEMP)-1)
#define BFLSC_EMPTYSTR "MEMORY EMPTY"
#define BFLSC_EMPTYSTR_LEN (sizeof(BFLSC_EMPTYSTR)-1)
// Queued and non-queued are the same
#define FullNonceRangeJob QueueJobStructure
#define BFLSC_JOBSIZ BFLSC_QJOBSIZ
// Non queued commands
#define BFLSC_SENDWORK "ZDX"
#define BFLSC_SENDWORK_LEN (sizeof(BFLSC_SENDWORK)-1)
// Non queued commands (not used)
#define BFLSC_WORKSTATUS "ZFX"
#define BFLSC_WORKSTATUS_LEN (sizeof(BFLSC_WORKSTATUS)-1)
#define BFLSC_SENDRANGE "ZPX"
#define BFLSC_SENDRANGE_LEN (sizeof(BFLSC_SENDRANGE)-1)
// Non queued work replies (not used)
#define BFLSC_NONCE "NONCE-FOUND:"
#define BFLSC_NONCE_LEN (sizeof(BFLSC_NONCE)-1)
#define BFLSC_NO_NONCE "NO-NONCE"
#define BFLSC_NO_NONCE_LEN (sizeof(BFLSC_NO_NONCE)-1)
#define BFLSC_IDLE "IDLE"
#define BFLSC_IDLE_LEN (sizeof(BFLSC_IDLE)-1)
#define BFLSC_BUSY "BUSY"
#define BFLSC_BUSY_LEN (sizeof(BFLSC_BUSY)-1)
#define BFLSC_MINIRIG "BAM"
#define BFLSC_SINGLE "BAS"
#define BFLSC_LITTLESINGLE "BAL"
#define BFLSC_JALAPENO "BAJ"
// Default expected time for a nonce range
// - thus no need to check until this + last time work was found
// 60GH/s MiniRig (1 board) or Single
#define BAM_WORK_TIME 71.58
#define BAS_WORK_TIME 71.58
// 30GH/s Little Single
#define BAL_WORK_TIME 143.17
// 4.5GH/s Jalapeno
#define BAJ_WORK_TIME 954.44
// Defaults (slightly over half the work time) but ensure none are above 100
// SCAN_TIME - delay after sending work
// RES_TIME - delay between checking for results
#define BAM_SCAN_TIME 20
#define BAM_RES_TIME 2
#define BAS_SCAN_TIME 360
#define BAS_RES_TIME 36
#define BAL_SCAN_TIME 720
#define BAL_RES_TIME 72
#define BAJ_SCAN_TIME 1000
#define BAJ_RES_TIME 100
#define BFLSC_MAX_SLEEP 2000
#define BFLSC_TEMP_SLEEPMS 5
#define BFLSC_QUE_SIZE 20
#define BFLSC_QUE_FULL_ENOUGH 13
#define BFLSC_QUE_WATERMARK 6
// Must drop this far below cutoff before resuming work
#define BFLSC_TEMP_RECOVER 5
// If initialisation fails the first time,
// sleep this amount (ms) and try again
#define REINIT_TIME_FIRST_MS 100
// Max ms per sleep
#define REINIT_TIME_MAX_MS 800
// Keep trying up to this many us
#define REINIT_TIME_MAX 3000000
static const char *blank = "";
struct device_drv bflsc_drv;
static void xlinkstr(char *xlink, int dev, struct bflsc_info *sc_info)
{
if (dev > 0)
sprintf(xlink, " x-%d", dev);
else {
if (sc_info->sc_count > 1)
strcpy(xlink, " master");
else
*xlink = '\0';
}
}
static void bflsc_applog(struct cgpu_info *bflsc, int dev, enum usb_cmds cmd, int amount, int err)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
char xlink[17];
xlinkstr(xlink, dev, sc_info);
usb_applog(bflsc, cmd, xlink, amount, err);
}
// Break an input up into lines with LFs removed
// false means an error, but if *lines > 0 then data was also found
// error would be no data or missing LF at the end
static bool tolines(struct cgpu_info *bflsc, int dev, char *buf, int *lines, char ***items, enum usb_cmds cmd)
{
bool ok = true;
char *ptr;
#define p_lines (*lines)
#define p_items (*items)
p_lines = 0;
p_items = NULL;
if (!buf || !(*buf)) {
applog(LOG_DEBUG, "USB: %s%i: (%d) empty %s",
bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
return false;
}
ptr = strdup(buf);
while (ptr && *ptr) {
p_items = realloc(p_items, ++p_lines * sizeof(*p_items));
if (unlikely(!p_items))
quit(1, "Failed to realloc p_items in tolines");
p_items[p_lines-1] = ptr;
ptr = strchr(ptr, '\n');
if (ptr)
*(ptr++) = '\0';
else {
if (ok) {
applog(LOG_DEBUG, "USB: %s%i: (%d) missing lf(s) in %s",
bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
}
ok = false;
}
}
return ok;
}
static void freetolines(int *lines, char ***items)
{
if (*lines > 0) {
free(**items);
free(*items);
}
*lines = 0;
*items = NULL;
}
enum breakmode {
NOCOLON,
ONECOLON,
ALLCOLON // Temperature uses this
};
// Break down a single line into 'fields'
// 'lf' will be a pointer to the final LF if it is there (or NULL)
// firstname will be the allocated buf copy pointer which is also
// the string before ':' for ONECOLON and ALLCOLON
// If any string is missing the ':' when it was expected, false is returned
static bool breakdown(enum breakmode mode, char *buf, int *count, char **firstname, char ***fields, char **lf)
{
char *ptr, *colon, *comma;
bool ok;
#define p_count (*count)
#define p_firstname (*firstname)
#define p_fields (*fields)
#define p_lf (*lf)
p_count = 0;
p_firstname = NULL;
p_fields = NULL;
p_lf = NULL;
if (!buf || !(*buf))
return false;
ptr = p_firstname = strdup(buf);
p_lf = strchr(p_firstname, '\n');
if (mode == ONECOLON) {
colon = strchr(ptr, ':');
if (colon) {
ptr = colon;
*(ptr++) = '\0';
} else
ok = false;
}
while (*ptr == ' ')
ptr++;
ok = true;
while (ptr && *ptr) {
if (mode == ALLCOLON) {
colon = strchr(ptr, ':');
if (colon)
ptr = colon + 1;
else
ok = false;
}
while (*ptr == ' ')
ptr++;
comma = strchr(ptr, ',');
if (comma)
*(comma++) = '\0';
p_fields = realloc(p_fields, ++p_count * sizeof(*p_fields));
if (unlikely(!p_fields))
quit(1, "Failed to realloc p_fields in breakdown");
p_fields[p_count-1] = ptr;
ptr = comma;
}
return ok;
}
static void freebreakdown(int *count, char **firstname, char ***fields)
{
if (*firstname)
free(*firstname);
if (*count > 0)
free(*fields);
*count = 0;
*firstname = NULL;
*fields = NULL;
}
static int write_to_dev(struct cgpu_info *bflsc, int dev, char *buf, int buflen, int *amount, enum usb_cmds cmd)
{
struct DataForwardToChain data;
int len;
if (dev == 0)
return usb_write(bflsc, buf, buflen, amount, cmd);
data.header = BFLSC_XLINKHDR;
data.deviceAddress = (uint8_t)dev;
data.payloadSize = buflen;
memcpy(data.payloadData, buf, buflen);
len = DATAFORWARDSIZE(data);
// TODO: handle xlink timeout message - here or at call?
return usb_write(bflsc, (char *)&data, len, amount, cmd);
}
static bool getok(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amount)
{
char buf[BFLSC_BUFSIZ+1];
*err = usb_ftdi_read_nl(bflsc, buf, sizeof(buf)-1, amount, cmd);
if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
return false;
else
return true;
}
static bool getokerr(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amount, char *buf, size_t bufsiz)
{
*err = usb_ftdi_read_nl(bflsc, buf, bufsiz-1, amount, cmd);
if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
return false;
else {
if (*amount > (int)BFLSC_ANERR_LEN && strncmp(buf, BFLSC_ANERR, BFLSC_ANERR_LEN) == 0)
return false;
else
return true;
}
}
static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
{
int err, amount;
// Device is gone
if (bflsc->usbinfo.nodev)
return;
mutex_lock(&bflsc->device_mutex);
err = write_to_dev(bflsc, dev, BFLSC_QFLUSH, BFLSC_QFLUSH_LEN, &amount, C_QUEFLUSH);
if (err < 0 || amount != BFLSC_QFLUSH_LEN) {
mutex_unlock(&bflsc->device_mutex);
bflsc_applog(bflsc, dev, C_QUEFLUSH, amount, err);
} else {
// TODO: do we care if we don't get 'OK'? (always will in normal processing)
err = getok(bflsc, C_QUEFLUSHREPLY, &err, &amount);
mutex_unlock(&bflsc->device_mutex);
// TODO: report an error if not 'OK' ?
}
}
/* return True = attempted usb_ftdi_read_ok()
* set ignore to true means no applog/ignore errors */
static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore)
{
bool readok = false;
mutex_lock(&(bflsc->device_mutex));
*err = write_to_dev(bflsc, dev, BFLSC_QRES, BFLSC_QRES_LEN, amount, C_REQUESTRESULTS);
if (*err < 0 || *amount != BFLSC_QRES_LEN) {
mutex_unlock(&(bflsc->device_mutex));
if (!ignore)
bflsc_applog(bflsc, dev, C_REQUESTRESULTS, *amount, *err);
// TODO: do what? flag as dead device?
// count how many times it has happened and reset/fail it
// or even make sure it is all x-link and that means device
// has failed after some limit of this?
// of course all other I/O must also be failing ...
} else {
readok = true;
*err = usb_ftdi_read_ok(bflsc, buf, bufsiz-1, amount, C_GETRESULTS);
mutex_unlock(&(bflsc->device_mutex));
if (*err < 0 || *amount < 1) {
if (!ignore)
bflsc_applog(bflsc, dev, C_GETRESULTS, *amount, *err);
// TODO: do what? ... see above
}
}
return readok;
}
static void __bflsc_initialise(struct cgpu_info *bflsc)
{
int err;
// TODO: this is a standard BFL FPGA Initialisation
// it probably will need changing ...
// TODO: does x-link bypass the other device FTDI? (I think it does)
// So no initialisation required except for the master device?
if (bflsc->usbinfo.nodev)
return;
// Reset
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
FTDI_VALUE_RESET, bflsc->usbdev->found->interface, C_RESET);
applog(LOG_DEBUG, "%s%i: reset got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
// Set data control
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
FTDI_VALUE_DATA, bflsc->usbdev->found->interface, C_SETDATA);
applog(LOG_DEBUG, "%s%i: setdata got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
// Set the baud
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD,
(FTDI_INDEX_BAUD & 0xff00) | bflsc->usbdev->found->interface,
C_SETBAUD);
applog(LOG_DEBUG, "%s%i: setbaud got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
// Set Flow Control
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
FTDI_VALUE_FLOW, bflsc->usbdev->found->interface, C_SETFLOW);
applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
// Set Modem Control
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
FTDI_VALUE_MODEM, bflsc->usbdev->found->interface, C_SETMODEM);
applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
// Clear any sent data
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
FTDI_VALUE_PURGE_TX, bflsc->usbdev->found->interface, C_PURGETX);
applog(LOG_DEBUG, "%s%i: purgetx got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
// Clear any received data
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
FTDI_VALUE_PURGE_RX, bflsc->usbdev->found->interface, C_PURGERX);
applog(LOG_DEBUG, "%s%i: purgerx got err %d",
bflsc->drv->name, bflsc->device_id, err);
}
static void bflsc_initialise(struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
char buf[BFLSC_BUFSIZ+1];
int err, amount;
int dev;
mutex_lock(&(bflsc->device_mutex));
__bflsc_initialise(bflsc);
mutex_unlock(&(bflsc->device_mutex));
for (dev = 0; dev < sc_info->sc_count; dev++) {
bflsc_send_flush_work(bflsc, dev);
bflsc_qres(bflsc, buf, sizeof(buf), dev, &err, &amount, true);
}
}
static bool getinfo(struct cgpu_info *bflsc, int dev)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
struct bflsc_dev sc_dev;
char buf[BFLSC_BUFSIZ+1];
int err, amount;
char **items, *firstname, **fields, *lf;
int i, lines, count;
bool res, ok;
char *tmp;
/*
* Kano's first dev Jalapeno output:
* DEVICE: BitFORCE SC<LF>
* FIRMWARE: 1.0.0<LF>
* ENGINES: 30<LF>
* FREQUENCY: [UNKNOWN]<LF>
* XLINK MODE: MASTER<LF>
* XLINK PRESENT: YES<LF>
* --DEVICES IN CHAIN: 0<LF>
* --CHAIN PRESENCE MASK: 00000000<LF>
* OK<LF>
*/
// TODO: if dev is ever > 0 must handle xlink timeout message
err = write_to_dev(bflsc, dev, BFLSC_DETAILS, BFLSC_DETAILS_LEN, &amount, C_REQUESTDETAILS);
if (err < 0 || amount != BFLSC_DETAILS_LEN) {
applog(LOG_ERR, "%s detect (%s) send details request failed (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
return false;
}
err = usb_ftdi_read_ok(bflsc, buf, sizeof(buf)-1, &amount, C_GETDETAILS);
if (err < 0 || amount < 1) {
if (err < 0) {
applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
} else {
applog(LOG_ERR, "%s detect (%s) get details returned nothing (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
}
return false;
}
memset(&sc_dev, 0, sizeof(struct bflsc_dev));
sc_info->sc_count = 1;
res = tolines(bflsc, dev, &(buf[0]), &lines, &items, C_GETDETAILS);
if (!res)
return false;
tmp = str_text(buf);
strcpy(sc_dev.getinfo, tmp);
free(tmp);
for (i = 0; i < lines-2; i++) {
res = breakdown(ONECOLON, items[i], &count, &firstname, &fields, &lf);
if (lf)
*lf = '\0';
if (!res || count != 1) {
tmp = str_text(items[i]);
applog(LOG_WARNING, "%s detect (%s) invalid details line: '%s' %d",
bflsc->drv->dname, bflsc->device_path, tmp, count);
free(tmp);
dev_error(bflsc, REASON_DEV_COMMS_ERROR);
goto mata;
}
if (strcmp(firstname, BFLSC_DI_FIRMWARE) == 0) {
sc_dev.firmware = strdup(fields[0]);
if (strcmp(sc_dev.firmware, "1.0.0")) {
tmp = str_text(items[i]);
applog(LOG_WARNING, "%s detect (%s) Warning unknown firmware '%s'",
bflsc->drv->dname, bflsc->device_path, tmp);
free(tmp);
}
}
else if (strcmp(firstname, BFLSC_DI_ENGINES) == 0) {
sc_dev.engines = atoi(fields[0]);
if (sc_dev.engines < 1) {
tmp = str_text(items[i]);
applog(LOG_WARNING, "%s detect (%s) invalid engine count: '%s'",
bflsc->drv->dname, bflsc->device_path, tmp);
free(tmp);
goto mata;
}
}
else if (strcmp(firstname, BFLSC_DI_XLINKMODE) == 0)
sc_dev.xlink_mode = strdup(fields[0]);
else if (strcmp(firstname, BFLSC_DI_XLINKPRESENT) == 0)
sc_dev.xlink_present = strdup(fields[0]);
else if (strcmp(firstname, BFLSC_DI_DEVICESINCHAIN) == 0) {
sc_info->sc_count = atoi(fields[0]) + 1;
if (sc_info->sc_count < 1 || sc_info->sc_count > 30) {
tmp = str_text(items[i]);
applog(LOG_WARNING, "%s detect (%s) invalid s-link count: '%s'",
bflsc->drv->dname, bflsc->device_path, tmp);
free(tmp);
goto mata;
}
}
freebreakdown(&count, &firstname, &fields);
}
sc_info->sc_devs = calloc(sc_info->sc_count, sizeof(struct bflsc_dev));
if (unlikely(!sc_info->sc_devs))
quit(1, "Failed to calloc in getinfo");
memcpy(&(sc_info->sc_devs[0]), &sc_dev, sizeof(sc_dev));
// TODO: do we care about getting this info for the rest if > 0 x-link
ok = true;
goto ne;
mata:
freebreakdown(&count, &firstname, &fields);
ok = false;
ne:
freetolines(&lines, &items);
return ok;
}
static bool bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
{
struct bflsc_info *sc_info = NULL;
char buf[BFLSC_BUFSIZ+1];
char devpath[20];
int i, err, amount;
struct timeval init_start, init_now;
int init_sleep, init_count;
bool ident_first;
char *newname;
struct cgpu_info *bflsc = calloc(1, sizeof(*bflsc));
if (unlikely(!bflsc))
quit(1, "Failed to calloc bflsc in bflsc_detect_one");
bflsc->drv = &bflsc_drv;
bflsc->deven = DEV_ENABLED;
bflsc->threads = 1;
sc_info = calloc(1, sizeof(*sc_info));
if (unlikely(!sc_info))
quit(1, "Failed to calloc sc_info in bflsc_detect_one");
// TODO: fix ... everywhere ...
bflsc->device_file = (FILE *)sc_info;
if (!usb_init(bflsc, dev, found))
goto shin;
sprintf(devpath, "%d:%d",
(int)(bflsc->usbinfo.bus_number),
(int)(bflsc->usbinfo.device_address));
// Allow 2 complete attempts if the 1st time returns an unrecognised reply
ident_first = true;
retry:
init_count = 0;
init_sleep = REINIT_TIME_FIRST_MS;
cgtime(&init_start);
reinit:
__bflsc_initialise(bflsc);
err = write_to_dev(bflsc, 0, BFLSC_IDENTIFY, BFLSC_IDENTIFY_LEN, &amount, C_REQUESTIDENTIFY);
if (err < 0 || amount != BFLSC_IDENTIFY_LEN) {
applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
bflsc->drv->dname, devpath, amount, err);
goto unshin;
}
err = usb_ftdi_read_nl(bflsc, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY);
if (err < 0 || amount < 1) {
init_count++;
cgtime(&init_now);
if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
if (init_count == 2) {
applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying",
bflsc->drv->dname, devpath, amount, err);
}
nmsleep(init_sleep);
if ((init_sleep * 2) <= REINIT_TIME_MAX_MS)
init_sleep *= 2;
goto reinit;
}
if (init_count > 0)
applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs",
bflsc->drv->dname, devpath, init_count, tdiff(&init_now, &init_start));
if (err < 0) {
applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
bflsc->drv->dname, devpath, amount, err);
} else {
applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
bflsc->drv->dname, devpath, amount);
}
goto unshin;
}
buf[amount] = '\0';
if (unlikely(!strstr(buf, BFLSC_BFLSC))) {
applog(LOG_DEBUG, "%s detect (%s) found an FPGA '%s' ignoring",
bflsc->drv->dname, devpath, buf);
goto unshin;
}
if (unlikely(strstr(buf, BFLSC_IDENTITY))) {
if (ident_first) {
applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' trying again ...",
bflsc->drv->dname, devpath, buf);
ident_first = false;
goto retry;
}
applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' on 2nd attempt",
bflsc->drv->dname, devpath, buf);
goto unshin;
}
bflsc->device_path = strdup(devpath);
if (!getinfo(bflsc, 0))
goto unshin;
sc_info->scan_sleep_time = BAS_SCAN_TIME;
sc_info->results_sleep_time = BAS_RES_TIME;
sc_info->default_ms_work = BAS_WORK_TIME;
/* When getinfo() "FREQUENCY: [UNKNOWN]" is fixed -
* use 'freq * engines' to estimate.
* Otherwise for now: */
newname = NULL;
if (sc_info->sc_count > 1) {
newname = BFLSC_MINIRIG;
sc_info->scan_sleep_time = BAM_SCAN_TIME;
sc_info->results_sleep_time = BAM_RES_TIME;
sc_info->default_ms_work = BAM_WORK_TIME;
} else {
if (sc_info->sc_devs[0].engines < 34) { // 16 * 2 + 2
newname = BFLSC_JALAPENO;
sc_info->scan_sleep_time = BAJ_SCAN_TIME;
sc_info->results_sleep_time = BAJ_RES_TIME;
sc_info->default_ms_work = BAJ_WORK_TIME;
} else if (sc_info->sc_devs[0].engines < 130) { // 16 * 8 + 2
newname = BFLSC_LITTLESINGLE;
sc_info->scan_sleep_time = BAL_SCAN_TIME;
sc_info->results_sleep_time = BAL_RES_TIME;
sc_info->default_ms_work = BAL_WORK_TIME;
}
}
for (i = 0; i < sc_info->sc_count; i++)
sc_info->sc_devs[i].ms_work = sc_info->default_ms_work;
if (newname) {
if (!bflsc->drv->copy)
bflsc->drv = copy_drv(bflsc->drv);
bflsc->drv->name = newname;
}
// We have a real BFLSC!
applog(LOG_DEBUG, "%s (%s) identified as: '%s'",
bflsc->drv->dname, devpath, bflsc->drv->name);
if (!add_cgpu(bflsc))
goto unshin;
update_usb_stats(bflsc);
mutex_init(&bflsc->device_mutex);
rwlock_init(&sc_info->stat_lock);
return true;
unshin:
usb_uninit(bflsc);
shin:
free(bflsc->device_path);
free(bflsc->device_file);
if (bflsc->name != blank)
free(bflsc->name);
if (bflsc->drv->copy)
free(bflsc->drv);
free(bflsc);
return false;
}
static void bflsc_detect(void)
{
usb_detect(&bflsc_drv, bflsc_detect_one);
}
static void get_bflsc_statline_before(char *buf, struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
float temp = 0;
float vcc1 = 0;
int i;
rd_lock(&(sc_info->stat_lock));
for (i = 0; i < sc_info->sc_count; i++) {
if (sc_info->sc_devs[i].temp1 > temp)
temp = sc_info->sc_devs[i].temp1;
if (sc_info->sc_devs[i].temp2 > temp)
temp = sc_info->sc_devs[i].temp2;
if (sc_info->sc_devs[i].vcc1 > vcc1)
vcc1 = sc_info->sc_devs[i].vcc1;
}
rd_unlock(&(sc_info->stat_lock));
tailsprintf(buf, " max%3.0fC %4.2fV | ", temp, vcc1);
}
static void flush_one_dev(struct cgpu_info *bflsc, int dev)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file);
struct work *work, *tmp;
bool did = false;
bflsc_send_flush_work(bflsc, dev);
rd_lock(&bflsc->qlock);
HASH_ITER(hh, bflsc->queued_work, work, tmp) {
if (work->queued && work->subid == dev) {
// devflag is used to flag stale work
work->devflag = true;
did = true;
}
}