forked from ckolivas/cgminer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver-bflsc.c
2004 lines (1685 loc) · 53.6 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 <string.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"
#include "driver-bflsc.h"
int opt_bflsc_overheat = BFLSC_TEMP_OVERHEAT;
static const char *blank = "";
static enum driver_version drv_ver(struct cgpu_info *bflsc, const char *ver)
{
char *tmp;
if (strstr(ver, "1.0.0"))
return BFLSC_DRV1;
if (strstr(ver, "1.0.") || strstr(ver, "1.1.")) {
applog(LOG_WARNING, "%s detect (%s) Warning assuming firmware '%s' is Ver1",
bflsc->drv->dname, bflsc->device_path, ver);
return BFLSC_DRV1;
}
if (strstr(ver, "1.2."))
return BFLSC_DRV2;
tmp = str_text((char *)ver);
applog(LOG_WARNING, "%s detect (%s) Warning unknown firmware '%s' using Ver2",
bflsc->drv->dname, bflsc->device_path, tmp);
free(tmp);
return BFLSC_DRV2;
}
static void xlinkstr(char *xlink, size_t siz, int dev, struct bflsc_info *sc_info)
{
if (dev > 0)
snprintf(xlink, siz, " 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_data);
char xlink[17];
xlinkstr(xlink, sizeof(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 = false;
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 ok;
}
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 {
applog(LOG_DEBUG, "USB: %s%i: (%d) missing lf(s) in %s",
bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
return ok;
}
}
ok = true;
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 = false;
#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 ok;
ptr = p_firstname = strdup(buf);
p_lf = strchr(p_firstname, '\n');
if (mode == ONECOLON) {
colon = strchr(ptr, ':');
if (colon) {
ptr = colon;
*(ptr++) = '\0';
} else
return ok;
}
while (ptr && *ptr) {
if (mode == ALLCOLON) {
colon = strchr(ptr, ':');
if (colon)
ptr = colon + 1;
else
return ok;
}
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;
}
ok = true;
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 bool isokerr(int err, char *buf, int amount)
{
if (err < 0 || amount < (int)BFLSC_OK_LEN)
return false;
else {
if (strstr(buf, BFLSC_ANERR))
return false;
else
return true;
}
}
// send+receive dual stage - always single line replies
static int send_recv_ds(struct cgpu_info *bflsc, int dev, int *stage, bool *sent, int *amount, char *send1, int send1_len, enum usb_cmds send1_cmd, enum usb_cmds recv1_cmd, char *send2, int send2_len, enum usb_cmds send2_cmd, enum usb_cmds recv2_cmd, char *recv, int recv_siz)
{
struct DataForwardToChain data;
int len, err, tried;
if (dev == 0) {
usb_buffer_clear(bflsc);
*stage = 1;
*sent = false;
err = usb_write(bflsc, send1, send1_len, amount, send1_cmd);
if (err < 0 || *amount < send1_len)
return err;
*sent = true;
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
if (!isokerr(err, recv, *amount))
return err;
usb_buffer_clear(bflsc);
*stage = 2;
*sent = false;
err = usb_write(bflsc, send2, send2_len, amount, send2_cmd);
if (err < 0 || *amount < send2_len)
return err;
*sent = true;
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
return err;
}
data.header = BFLSC_XLINKHDR;
data.deviceAddress = (uint8_t)dev;
tried = 0;
while (tried++ < 3) {
data.payloadSize = send1_len;
memcpy(data.payloadData, send1, send1_len);
len = DATAFORWARDSIZE(data);
usb_buffer_clear(bflsc);
*stage = 1;
*sent = false;
err = usb_write(bflsc, (char *)&data, len, amount, send1_cmd);
if (err < 0 || *amount < send1_len)
return err;
*sent = true;
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
if (err != LIBUSB_SUCCESS)
return err;
// x-link timeout? - try again?
if (strstr(recv, BFLSC_XTIMEOUT))
continue;
if (!isokerr(err, recv, *amount))
return err;
data.payloadSize = send2_len;
memcpy(data.payloadData, send2, send2_len);
len = DATAFORWARDSIZE(data);
usb_buffer_clear(bflsc);
*stage = 2;
*sent = false;
err = usb_write(bflsc, (char *)&data, len, amount, send2_cmd);
if (err < 0 || *amount < send2_len)
return err;
*sent = true;
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
if (err != LIBUSB_SUCCESS)
return err;
// x-link timeout? - try again?
if (strstr(recv, BFLSC_XTIMEOUT))
continue;
// SUCCESS - return it
break;
}
return err;
}
#define READ_OK true
#define READ_NL false
// send+receive single stage
static int send_recv_ss(struct cgpu_info *bflsc, int dev, bool *sent, int *amount, char *send, int send_len, enum usb_cmds send_cmd, char *recv, int recv_siz, enum usb_cmds recv_cmd, bool read_ok)
{
struct DataForwardToChain data;
int len, err, tried;
if (dev == 0) {
usb_buffer_clear(bflsc);
*sent = false;
err = usb_write(bflsc, send, send_len, amount, send_cmd);
if (err < 0 || *amount < send_len) {
// N.B. thus !(*sent) directly implies err < 0 or *amount < send_len
return err;
}
*sent = true;
if (read_ok == READ_OK)
err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
else
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
return err;
}
data.header = BFLSC_XLINKHDR;
data.deviceAddress = (uint8_t)dev;
data.payloadSize = send_len;
memcpy(data.payloadData, send, send_len);
len = DATAFORWARDSIZE(data);
tried = 0;
while (tried++ < 3) {
usb_buffer_clear(bflsc);
*sent = false;
err = usb_write(bflsc, (char *)&data, len, amount, recv_cmd);
if (err < 0 || *amount < send_len)
return err;
*sent = true;
if (read_ok == READ_OK)
err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
else
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_TIMEOUT)
return err;
// read_ok can err timeout if it's looking for OK<LF>
// TODO: add a usb_read() option to spot the ERR: and convert end=OK<LF> to just <LF>
// x-link timeout? - try again?
if ((err == LIBUSB_SUCCESS || (read_ok == READ_OK && err == LIBUSB_ERROR_TIMEOUT)) &&
strstr(recv, BFLSC_XTIMEOUT))
continue;
// SUCCESS or TIMEOUT - return it
break;
}
return err;
}
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;
/*
* The protocol is syncronous so any previous excess can be
* discarded and assumed corrupt data or failed USB transfers
*/
usb_buffer_clear(bflsc);
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);
return usb_write(bflsc, (char *)&data, len, amount, cmd);
}
static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
{
char buf[BFLSC_BUFSIZ+1];
int err, amount;
bool sent;
// Device is gone
if (bflsc->usbinfo.nodev)
return;
mutex_lock(&bflsc->device_mutex);
err = send_recv_ss(bflsc, dev, &sent, &amount,
BFLSC_QFLUSH, BFLSC_QFLUSH_LEN, C_QUEFLUSH,
buf, sizeof(buf)-1, C_QUEFLUSHREPLY, READ_NL);
mutex_unlock(&bflsc->device_mutex);
if (!sent)
bflsc_applog(bflsc, dev, C_QUEFLUSH, amount, err);
else {
// TODO: do we care if we don't get 'OK'? (always will in normal processing)
}
}
/* return True = attempted usb_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 = send_recv_ss(bflsc, dev, &readok, amount,
BFLSC_QRES, BFLSC_QRES_LEN, C_REQUESTRESULTS,
buf, bufsiz-1, C_GETRESULTS, READ_OK);
mutex_unlock(&(bflsc->device_mutex));
if (!readok) {
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 {
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, interface;
// 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;
interface = usb_interface(bflsc);
// Reset
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
FTDI_VALUE_RESET, interface, C_RESET);
applog(LOG_DEBUG, "%s%i: reset got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
usb_ftdi_set_latency(bflsc);
if (bflsc->usbinfo.nodev)
return;
// Set data control
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
FTDI_VALUE_DATA_BAS, 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_BAS,
(FTDI_INDEX_BAUD_BAS & 0xff00) | 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, 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, 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, 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, interface, C_PURGERX);
applog(LOG_DEBUG, "%s%i: purgerx got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (!bflsc->cutofftemp)
bflsc->cutofftemp = opt_bflsc_overheat;
}
static void bflsc_initialise(struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
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_data);
struct bflsc_dev sc_dev;
char buf[BFLSC_BUFSIZ+1];
int err, amount;
char **items, *firstname, **fields, *lf;
bool res, ok = false;
int i, lines, count;
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>
*/
/*
* Don't use send_recv_ss() since we have a different receive timeout
* Also getinfo() is called multiple times if it fails anyway
*/
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 ok;
}
err = usb_read_ok_timeout(bflsc, buf, sizeof(buf)-1, &amount,
BFLSC_INFO_TIMEOUT, 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 ok;
}
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 ok;
tmp = str_text(buf);
strncpy(sc_dev.getinfo, tmp, sizeof(sc_dev.getinfo));
sc_dev.getinfo[sizeof(sc_dev.getinfo)-1] = '\0';
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]);
applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
"%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 (strstr(firstname, BFLSC_DI_FIRMWARE)) {
sc_dev.firmware = strdup(fields[0]);
sc_info->driver_version = drv_ver(bflsc, sc_dev.firmware);
}
else if (strstr(firstname, BFLSC_DI_ENGINES)) {
sc_dev.engines = atoi(fields[0]);
if (sc_dev.engines < 1) {
tmp = str_text(items[i]);
applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
"%s detect (%s) invalid engine count: '%s'",
bflsc->drv->dname, bflsc->device_path, tmp);
free(tmp);
goto mata;
}
}
else if (strstr(firstname, BFLSC_DI_XLINKMODE))
sc_dev.xlink_mode = strdup(fields[0]);
else if (strstr(firstname, BFLSC_DI_XLINKPRESENT))
sc_dev.xlink_present = strdup(fields[0]);
else if (strstr(firstname, BFLSC_DI_DEVICESINCHAIN)) {
if (fields[0][0] == '0' ||
(fields[0][0] == ' ' && fields[0][1] == '0'))
sc_info->sc_count = 1;
else
sc_info->sc_count = atoi(fields[0]);
if (sc_info->sc_count < 1 || sc_info->sc_count > 30) {
tmp = str_text(items[i]);
applogsiz(LOG_WARNING, BFLSC_APPLOGSIZ,
"%s detect (%s) invalid x-link count: '%s'",
bflsc->drv->dname, bflsc->device_path, tmp);
free(tmp);
goto mata;
}
}
else if (strstr(firstname, BFLSC_DI_CHIPS))
sc_dev.chips = strdup(fields[0]);
freebreakdown(&count, &firstname, &fields);
}
if (sc_info->driver_version == BFLSC_DRVUNDEF) {
applog(LOG_WARNING, "%s detect (%s) missing %s",
bflsc->drv->dname, bflsc->device_path, BFLSC_DI_FIRMWARE);
goto ne;
}
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 struct cgpu_info *bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
{
struct bflsc_info *sc_info = NULL;
char buf[BFLSC_BUFSIZ+1];
int i, err, amount;
struct timeval init_start, init_now;
int init_sleep, init_count;
bool ident_first, sent;
char *newname;
uint16_t latency;
struct cgpu_info *bflsc = usb_alloc_cgpu(&bflsc_drv, 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_data = (FILE *)sc_info;
if (!usb_init(bflsc, dev, found))
goto shin;
// 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 = send_recv_ss(bflsc, 0, &sent, &amount,
BFLSC_IDENTIFY, BFLSC_IDENTIFY_LEN, C_REQUESTIDENTIFY,
buf, sizeof(buf)-1, C_GETIDENTIFY, READ_NL);
if (!sent) {
applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
goto unshin;
}
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, bflsc->device_path, amount, err);
}
cgsleep_ms(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, bflsc->device_path, init_count, tdiff(&init_now, &init_start));
if (err < 0) {
applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
} else {
applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
bflsc->drv->dname, bflsc->device_path, 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, bflsc->device_path, 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, bflsc->device_path, buf);
ident_first = false;
goto retry;
}
applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' on 2nd attempt",
bflsc->drv->dname, bflsc->device_path, buf);
goto unshin;
}
int tries = 0;
while (7734) {
if (getinfo(bflsc, 0))
break;
// N.B. we will get displayed errors each time it fails
if (++tries > 2)
goto unshin;
cgsleep_ms(40);
}
switch (sc_info->driver_version) {
case BFLSC_DRV1:
sc_info->que_size = BFLSC_QUE_SIZE_V1;
sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V1;
sc_info->que_watermark = BFLSC_QUE_WATERMARK_V1;
sc_info->que_low = BFLSC_QUE_LOW_V1;
sc_info->que_noncecount = QUE_NONCECOUNT_V1;
sc_info->que_fld_min = QUE_FLD_MIN_V1;
sc_info->que_fld_max = QUE_FLD_MAX_V1;
// Only Jalapeno uses 1.0.0
sc_info->flush_size = 1;
break;
case BFLSC_DRV2:
case BFLSC_DRVUNDEF:
default:
sc_info->driver_version = BFLSC_DRV2;
sc_info->que_size = BFLSC_QUE_SIZE_V2;
sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V2;
sc_info->que_watermark = BFLSC_QUE_WATERMARK_V2;
sc_info->que_low = BFLSC_QUE_LOW_V2;
sc_info->que_noncecount = QUE_NONCECOUNT_V2;
sc_info->que_fld_min = QUE_FLD_MIN_V2;
sc_info->que_fld_max = QUE_FLD_MAX_V2;
// TODO: this can be reduced to total chip count
sc_info->flush_size = 16 * sc_info->sc_count;
break;
}
// Set parallelization based on the getinfo() response if it is present
if (sc_info->sc_devs[0].chips && strlen(sc_info->sc_devs[0].chips)) {
if (strstr(sc_info->sc_devs[0].chips, BFLSC_DI_CHIPS_PARALLEL)) {
sc_info->que_noncecount = QUE_NONCECOUNT_V2;
sc_info->que_fld_min = QUE_FLD_MIN_V2;
sc_info->que_fld_max = QUE_FLD_MAX_V2;
} else {
sc_info->que_noncecount = QUE_NONCECOUNT_V1;
sc_info->que_fld_min = QUE_FLD_MIN_V1;
sc_info->que_fld_max = QUE_FLD_MAX_V1;
}
}
sc_info->scan_sleep_time = BAS_SCAN_TIME;
sc_info->results_sleep_time = BFLSC_RES_TIME;
sc_info->default_ms_work = BAS_WORK_TIME;
latency = BAS_LATENCY;
/* 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->default_ms_work = BAM_WORK_TIME;
bflsc->usbdev->ident = IDENT_BAM;
latency = BAM_LATENCY;
} 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->default_ms_work = BAJ_WORK_TIME;
bflsc->usbdev->ident = IDENT_BAJ;
latency = BAJ_LATENCY;
} 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->default_ms_work = BAL_WORK_TIME;
bflsc->usbdev->ident = IDENT_BAL;
latency = BAL_LATENCY;
}
}
if (latency != bflsc->usbdev->found->latency) {
bflsc->usbdev->found->latency = latency;
usb_ftdi_set_latency(bflsc);
}
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, bflsc->device_path, 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 bflsc;
unshin:
usb_uninit(bflsc);
shin:
free(bflsc->device_data);
bflsc->device_data = NULL;
if (bflsc->name != blank) {
free(bflsc->name);
bflsc->name = NULL;
}
bflsc = usb_free_cgpu(bflsc);
return NULL;
}
static void bflsc_detect(bool __maybe_unused hotplug)
{
usb_detect(&bflsc_drv, bflsc_detect_one);
}
static void get_bflsc_statline_before(char *buf, size_t bufsiz, struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
float temp = 0;
float vcc2 = 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].vcc2 > vcc2)
vcc2 = sc_info->sc_devs[i].vcc2;
}
rd_unlock(&(sc_info->stat_lock));
tailsprintf(buf, bufsiz, "max%3.0fC %4.2fV", temp, vcc2);
}
static void flush_one_dev(struct cgpu_info *bflsc, int dev)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
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->subid == dev) {
// devflag is used to flag stale work
work->devflag = true;
did = true;
}
}
rd_unlock(&bflsc->qlock);
if (did) {
wr_lock(&(sc_info->stat_lock));
sc_info->sc_devs[dev].flushed = true;
sc_info->sc_devs[dev].flush_id = sc_info->sc_devs[dev].result_id;
sc_info->sc_devs[dev].work_queued = 0;
wr_unlock(&(sc_info->stat_lock));
}
}
static void bflsc_flush_work(struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
int dev;
for (dev = 0; dev < sc_info->sc_count; dev++)
flush_one_dev(bflsc, dev);
}
static void bflsc_flash_led(struct cgpu_info *bflsc, int dev)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char buf[BFLSC_BUFSIZ+1];
int err, amount;
bool sent;
// Device is gone
if (bflsc->usbinfo.nodev)
return;
// It is not critical flashing the led so don't get stuck if we
// can't grab the mutex now
if (mutex_trylock(&bflsc->device_mutex))
return;
err = send_recv_ss(bflsc, dev, &sent, &amount,
BFLSC_FLASH, BFLSC_FLASH_LEN, C_REQUESTFLASH,
buf, sizeof(buf)-1, C_FLASHREPLY, READ_NL);
mutex_unlock(&(bflsc->device_mutex));
if (!sent)
bflsc_applog(bflsc, dev, C_REQUESTFLASH, amount, err);
else {
// Don't care