forked from luke-jr/bfgminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver-icarus.c
1456 lines (1249 loc) · 44.4 KB
/
driver-icarus.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-2015 Luke Dashjr
* Copyright 2012 Xiangfu
* Copyright 2014 Nate Woolls
* Copyright 2012 Andrew Smith
*
* 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.
*/
/*
* Those code should be works fine with V2 and V3 bitstream of Icarus.
* Operation:
* No detection implement.
* Input: 64B = 32B midstate + 20B fill bytes + last 12 bytes of block head.
* Return: send back 32bits immediately when Icarus found a valid nonce.
* no query protocol implemented here, if no data send back in ~11.3
* seconds (full cover time on 32bit nonce range by 380MH/s speed)
* just send another work.
* Notice:
* 1. Icarus will start calculate when you push a work to them, even they
* are busy.
* 2. The 2 FPGAs on Icarus will distribute the job, one will calculate the
* 0 ~ 7FFFFFFF, another one will cover the 80000000 ~ FFFFFFFF.
* 3. It's possible for 2 FPGAs both find valid nonce in the meantime, the 2
* valid nonce will all be send back.
* 4. Icarus will stop work when: a valid nonce has been found or 32 bits
* nonce range is completely calculated.
*/
#include "config.h"
#include "miner.h"
#include <limits.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#ifndef WIN32
#include <termios.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#else
#include <windows.h>
#include <io.h>
#endif
#ifdef HAVE_SYS_EPOLL_H
#include <sys/epoll.h>
#define HAVE_EPOLL
#endif
#include "compat.h"
#include "dynclock.h"
#include "driver-icarus.h"
#include "lowl-vcom.h"
// The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
#define ICARUS_IO_SPEED 115200
// The number of bytes in a nonce (always 4)
// This is NOT the read-size for the Icarus driver
// That is defined in ICARUS_INFO->read_size
#define ICARUS_NONCE_SIZE 4
#define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
ASSERT1(sizeof(uint32_t) == 4);
#define ICARUS_READ_TIME(baud, read_size) ((double)read_size * (double)8.0 / (double)(baud))
// Defined in deciseconds
// There's no need to have this bigger, since the overhead/latency of extra work
// is pretty small once you get beyond a 10s nonce range time and 10s also
// means that nothing slower than 429MH/s can go idle so most icarus devices
// will always mine without idling
#define ICARUS_READ_COUNT_LIMIT_MAX 100
// In timing mode: Default starting value until an estimate can be obtained
#define ICARUS_READ_COUNT_TIMING_MS 75
// For a standard Icarus REV3
#define ICARUS_REV3_HASH_TIME 0.00000000264083
// Icarus Rev3 doesn't send a completion message when it finishes
// the full nonce range, so to avoid being idle we must abort the
// work (by starting a new work) shortly before it finishes
//
// Thus we need to estimate 2 things:
// 1) How many hashes were done if the work was aborted
// 2) How high can the timeout be before the Icarus is idle,
// to minimise the number of work started
// We set 2) to 'the calculated estimate' - 1
// to ensure the estimate ends before idle
//
// The simple calculation used is:
// Tn = Total time in seconds to calculate n hashes
// Hs = seconds per hash
// Xn = number of hashes
// W = code overhead per work
//
// Rough but reasonable estimate:
// Tn = Hs * Xn + W (of the form y = mx + b)
//
// Thus:
// Line of best fit (using least squares)
//
// Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2)
// W = Sum(Ti)/n - (Hs*Sum(Xi))/n
//
// N.B. W is less when aborting work since we aren't waiting for the reply
// to be transferred back (ICARUS_READ_TIME)
// Calculating the hashes aborted at n seconds is thus just n/Hs
// (though this is still a slight overestimate due to code delays)
//
// Both below must be exceeded to complete a set of data
// Minimum how long after the first, the last data point must be
#define HISTORY_SEC 60
// Minimum how many points a single ICARUS_HISTORY should have
#define MIN_DATA_COUNT 5
// The value above used is doubled each history until it exceeds:
#define MAX_MIN_DATA_COUNT 100
static struct timeval history_sec = { HISTORY_SEC, 0 };
static const char *MODE_DEFAULT_STR = "default";
static const char *MODE_SHORT_STR = "short";
static const char *MODE_SHORT_STREQ = "short=";
static const char *MODE_LONG_STR = "long";
static const char *MODE_LONG_STREQ = "long=";
static const char *MODE_VALUE_STR = "value";
static const char *MODE_UNKNOWN_STR = "unknown";
#define END_CONDITION 0x0000ffff
#define DEFAULT_DETECT_THRESHOLD 1
BFG_REGISTER_DRIVER(icarus_drv)
extern const struct bfg_set_device_definition icarus_set_device_funcs[];
extern const struct bfg_set_device_definition icarus_set_device_funcs_live[];
extern void convert_icarus_to_cairnsmore(struct cgpu_info *);
static inline
uint32_t icarus_nonce32toh(const struct ICARUS_INFO * const info, const uint32_t nonce)
{
return info->nonce_littleendian ? le32toh(nonce) : be32toh(nonce);
}
#define icarus_open2(devpath, baud, purge) serial_open(devpath, baud, ICARUS_READ_FAULT_DECISECONDS, purge)
#define icarus_open(devpath, baud) icarus_open2(devpath, baud, false)
static
void icarus_log_protocol(const char * const repr, const void *buf, size_t bufLen, const char *prefix)
{
char hex[(bufLen * 2) + 1];
bin2hex(hex, buf, bufLen);
applog(LOG_DEBUG, "%s: DEVPROTO: %s %s", repr, prefix, hex);
}
int icarus_read(const char * const repr, uint8_t *buf, const int fd, struct timeval * const tvp_finish, struct thr_info * const thr, const struct timeval * const tvp_timeout, struct timeval * const tvp_now, int read_size)
{
int rv;
long remaining_ms;
ssize_t ret;
struct timeval tv_start = *tvp_now;
bool first = true;
// If there is no thr, then there's no work restart to watch..
#ifdef HAVE_EPOLL
bool watching_work_restart = !thr;
int epollfd;
struct epoll_event evr[2];
epollfd = epoll_create(2);
if (epollfd != -1) {
struct epoll_event ev = {
.events = EPOLLIN,
.data.fd = fd,
};
if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
applog(LOG_DEBUG, "%s: Error adding %s fd to epoll", "device", repr);
close(epollfd);
epollfd = -1;
}
else
if (thr && thr->work_restart_notifier[1] != -1)
{
ev.data.fd = thr->work_restart_notifier[0];
if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, thr->work_restart_notifier[0], &ev))
applog(LOG_DEBUG, "%s: Error adding %s fd to epoll", "work restart", repr);
else
watching_work_restart = true;
}
}
else
applog(LOG_DEBUG, "%s: Error creating epoll", repr);
if (epollfd == -1 && (remaining_ms = timer_remaining_us(tvp_timeout, tvp_now)) < 100000)
applog(LOG_WARNING, "%s: Failed to use epoll, and very short read timeout (%ldms)", repr, remaining_ms);
#endif
while (true) {
remaining_ms = timer_remaining_us(tvp_timeout, tvp_now) / 1000;
#ifdef HAVE_EPOLL
if (epollfd != -1)
{
if ((!watching_work_restart) && remaining_ms > 100)
remaining_ms = 100;
ret = epoll_wait(epollfd, evr, 2, remaining_ms);
timer_set_now(tvp_now);
switch (ret)
{
case -1:
if (unlikely(errno != EINTR))
return_via(out, rv = ICA_GETS_ERROR);
ret = 0;
break;
case 0: // timeout
// handled after switch
break;
case 1:
if (evr[0].data.fd != fd) // must be work restart notifier
{
notifier_read(thr->work_restart_notifier);
ret = 0;
break;
}
// fallthru to...
case 2: // device has data
ret = read(fd, buf, read_size);
break;
default:
return_via(out, rv = ICA_GETS_ERROR);
}
}
else
#endif
{
if (remaining_ms > 100)
remaining_ms = 100;
else
if (remaining_ms < 1)
remaining_ms = 1;
vcom_set_timeout_ms(fd, remaining_ms);
// Read first byte alone to get earliest tv_finish
ret = read(fd, buf, first ? 1 : read_size);
timer_set_now(tvp_now);
}
if (first)
*tvp_finish = *tvp_now;
if (ret)
{
if (unlikely(ret < 0))
return_via(out, rv = ICA_GETS_ERROR);
first = false;
if (opt_dev_protocol && opt_debug)
icarus_log_protocol(repr, buf, ret, "RECV");
if (ret >= read_size)
return_via(out, rv = ICA_GETS_OK);
read_size -= ret;
buf += ret;
// Always continue reading while data is coming in, ignoring the timeout
continue;
}
if (thr && thr->work_restart)
return_via_applog(out, rv = ICA_GETS_RESTART, LOG_DEBUG, "%s: Interrupted by work restart", repr);
if (timer_passed(tvp_timeout, tvp_now))
return_via_applog(out, rv = ICA_GETS_TIMEOUT, LOG_DEBUG, "%s: No data in %.3f seconds", repr, timer_elapsed_us(&tv_start, tvp_now) / 1e6);
}
out:
#ifdef HAVE_EPOLL
if (epollfd != -1)
close(epollfd);
#endif
return rv;
}
int icarus_write(const char * const repr, int fd, const void *buf, size_t bufLen)
{
size_t ret;
if (opt_dev_protocol && opt_debug)
icarus_log_protocol(repr, buf, bufLen, "SEND");
if (unlikely(fd == -1))
return 1;
ret = write(fd, buf, bufLen);
if (unlikely(ret != bufLen))
return 1;
return 0;
}
#define icarus_close(fd) serial_close(fd)
void do_icarus_close(struct thr_info *thr)
{
struct cgpu_info *icarus = thr->cgpu;
const int fd = icarus->device_fd;
if (fd == -1)
return;
icarus_close(fd);
icarus->device_fd = -1;
}
static const char *timing_mode_str(enum timing_mode timing_mode)
{
switch(timing_mode) {
case MODE_DEFAULT:
return MODE_DEFAULT_STR;
case MODE_SHORT:
return MODE_SHORT_STR;
case MODE_LONG:
return MODE_LONG_STR;
case MODE_VALUE:
return MODE_VALUE_STR;
default:
return MODE_UNKNOWN_STR;
}
}
static
const char *_icarus_set_timing(struct ICARUS_INFO * const info, const char * const repr, const struct device_drv * const drv, const char * const buf)
{
double Hs;
char *eq;
if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
// short
info->read_timeout_ms = ICARUS_READ_COUNT_TIMING_MS;
info->read_count_limit = 0; // 0 = no limit
info->timing_mode = MODE_SHORT;
info->do_icarus_timing = true;
} else if (strncasecmp(buf, MODE_SHORT_STREQ, strlen(MODE_SHORT_STREQ)) == 0) {
// short=limit
info->read_timeout_ms = ICARUS_READ_COUNT_TIMING_MS;
info->timing_mode = MODE_SHORT;
info->do_icarus_timing = true;
info->read_count_limit = atoi(&buf[strlen(MODE_SHORT_STREQ)]);
if (info->read_count_limit < 0)
info->read_count_limit = 0;
if (info->read_count_limit > ICARUS_READ_COUNT_LIMIT_MAX)
info->read_count_limit = ICARUS_READ_COUNT_LIMIT_MAX;
} else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
// long
info->read_timeout_ms = ICARUS_READ_COUNT_TIMING_MS;
info->read_count_limit = 0; // 0 = no limit
info->timing_mode = MODE_LONG;
info->do_icarus_timing = true;
} else if (strncasecmp(buf, MODE_LONG_STREQ, strlen(MODE_LONG_STREQ)) == 0) {
// long=limit
info->read_timeout_ms = ICARUS_READ_COUNT_TIMING_MS;
info->timing_mode = MODE_LONG;
info->do_icarus_timing = true;
info->read_count_limit = atoi(&buf[strlen(MODE_LONG_STREQ)]);
if (info->read_count_limit < 0)
info->read_count_limit = 0;
if (info->read_count_limit > ICARUS_READ_COUNT_LIMIT_MAX)
info->read_count_limit = ICARUS_READ_COUNT_LIMIT_MAX;
} else if ((Hs = atof(buf)) != 0) {
// ns[=read_count]
info->Hs = Hs / NANOSEC;
info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
info->read_timeout_ms = 0;
if ((eq = strchr(buf, '=')) != NULL)
info->read_timeout_ms = atof(&eq[1]) * 100;
if (info->read_timeout_ms < 1)
{
info->read_timeout_ms = info->fullnonce * 1000;
if (unlikely(info->read_timeout_ms < 2))
info->read_timeout_ms = 1;
else
--info->read_timeout_ms;
}
info->read_count_limit = 0; // 0 = no limit
info->timing_mode = MODE_VALUE;
info->do_icarus_timing = false;
} else {
// Anything else in buf just uses DEFAULT mode
info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
info->read_timeout_ms = 0;
if ((eq = strchr(buf, '=')) != NULL)
info->read_timeout_ms = atof(&eq[1]) * 100;
unsigned def_read_timeout_ms = ICARUS_READ_COUNT_TIMING_MS;
if (info->timing_mode == MODE_DEFAULT) {
if (drv == &icarus_drv) {
info->do_default_detection = 0x10;
} else {
def_read_timeout_ms = info->fullnonce * 1000;
if (def_read_timeout_ms > 0)
--def_read_timeout_ms;
}
info->do_icarus_timing = false;
}
if (info->read_timeout_ms < 1)
info->read_timeout_ms = def_read_timeout_ms;
info->read_count_limit = 0; // 0 = no limit
}
info->min_data_count = MIN_DATA_COUNT;
applog(LOG_DEBUG, "%s: Init: mode=%s read_timeout_ms=%u limit=%dms Hs=%e",
repr,
timing_mode_str(info->timing_mode),
info->read_timeout_ms, info->read_count_limit, info->Hs);
return NULL;
}
const char *icarus_set_timing(struct cgpu_info * const proc, const char * const optname, const char * const buf, char * const replybuf, enum bfg_set_device_replytype * const out_success)
{
struct ICARUS_INFO * const info = proc->device_data;
return _icarus_set_timing(info, proc->dev_repr, proc->drv, buf);
}
static uint32_t mask(int work_division)
{
return 0xffffffff / work_division;
}
// Number of bytes remaining after reading a nonce from Icarus
int icarus_excess_nonce_size(int fd, struct ICARUS_INFO *info)
{
// How big a buffer?
int excess_size = info->read_size - ICARUS_NONCE_SIZE;
// Try to read one more to ensure the device doesn't return
// more than we want for this driver
excess_size++;
unsigned char excess_bin[excess_size];
// Read excess_size from Icarus
struct timeval tv_now;
timer_set_now(&tv_now);
int bytes_read = read(fd, excess_bin, excess_size);
// Number of bytes that were still available
return bytes_read;
}
int icarus_probe_work_division(const int fd, const char * const repr, struct ICARUS_INFO * const info)
{
struct timeval tv_now, tv_timeout;
struct timeval tv_finish;
// For reading the nonce from Icarus
unsigned char res_bin[info->read_size];
// For storing the the 32-bit nonce
uint32_t res;
int work_division = 0;
applog(LOG_DEBUG, "%s: Work division not specified - autodetecting", repr);
// Special packet to probe work_division
unsigned char pkt[64] =
"\x2e\x4c\x8f\x91\xfd\x59\x5d\x2d\x7e\xa2\x0a\xaa\xcb\x64\xa2\xa0"
"\x43\x82\x86\x02\x77\xcf\x26\xb6\xa1\xee\x04\xc5\x6a\x5b\x50\x4a"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\x64\x61\x01\x1a\xc9\x06\xa9\x51\xfb\x9b\x3c\x73";
icarus_write(repr, fd, pkt, sizeof(pkt));
memset(res_bin, 0, sizeof(res_bin));
timer_set_now(&tv_now);
timer_set_delay(&tv_timeout, &tv_now, info->read_timeout_ms * 1000);
if (ICA_GETS_OK == icarus_read(repr, res_bin, fd, &tv_finish, NULL, &tv_timeout, &tv_now, info->read_size))
{
memcpy(&res, res_bin, sizeof(res));
res = icarus_nonce32toh(info, res);
}
else
res = 0;
switch (res) {
case 0x04C0FDB4:
work_division = 1;
break;
case 0x82540E46:
work_division = 2;
break;
case 0x417C0F36:
work_division = 4;
break;
case 0x60C994D5:
work_division = 8;
break;
default:
applog(LOG_ERR, "%s: Work division autodetection failed (assuming 2): got %08x", repr, res);
work_division = 2;
}
applog(LOG_DEBUG, "%s: Work division autodetection got %08x (=%d)", repr, res, work_division);
return work_division;
}
struct cgpu_info *icarus_detect_custom(const char *devpath, struct device_drv *api, struct ICARUS_INFO *info)
{
struct timeval tv_start, tv_finish;
int fd;
unsigned char nonce_bin[ICARUS_NONCE_SIZE];
char nonce_hex[(sizeof(nonce_bin) * 2) + 1];
drv_set_defaults(api, icarus_set_device_funcs, info, devpath, detectone_meta_info.serial, 1);
int baud = info->baud;
int work_division = info->work_division;
int fpga_count = info->fpga_count;
applog(LOG_DEBUG, "%s: Attempting to open %s", api->dname, devpath);
fd = icarus_open2(devpath, baud, true);
if (unlikely(fd == -1)) {
applog(LOG_DEBUG, "%s: Failed to open %s", api->dname, devpath);
return NULL;
}
// Set a default so that individual drivers need not specify
// e.g. Cairnsmore
BFGINIT(info->probe_read_count, 1);
if (info->read_size == 0)
info->read_size = ICARUS_DEFAULT_READ_SIZE;
if (!info->golden_ob)
{
// Block 171874 nonce = (0xa2870100) = 0x000187a2
// NOTE: this MUST take less time to calculate
// than the timeout set in icarus_open()
// This one takes ~0.53ms on Rev3 Icarus
info->golden_ob =
"4679ba4ec99876bf4bfe086082b40025"
"4df6c356451471139a3afa71e48f544a"
"00000000000000000000000000000000"
"0000000087320b1a1426674f2fa722ce";
/* NOTE: This gets sent to basically every port specified in --scan-serial,
* even ones that aren't Icarus; be sure they can all handle it, when
* this is changed...
* BitForce: Ignores entirely
* ModMiner: Starts (useless) work, gets back to clean state
*/
info->golden_nonce = "000187a2";
}
if (info->detect_init_func)
info->detect_init_func(devpath, fd, info);
int ob_size = strlen(info->golden_ob) / 2;
unsigned char ob_bin[ob_size];
BFGINIT(info->ob_size, ob_size);
if (!info->ignore_golden_nonce)
{
hex2bin(ob_bin, info->golden_ob, sizeof(ob_bin));
icarus_write(devpath, fd, ob_bin, sizeof(ob_bin));
cgtime(&tv_start);
memset(nonce_bin, 0, sizeof(nonce_bin));
// Do not use info->read_size here, instead read exactly ICARUS_NONCE_SIZE
// We will then compare the bytes left in fd with info->read_size to determine
// if this is a valid device
struct timeval tv_now, tv_timeout;
timer_set_now(&tv_now);
timer_set_delay(&tv_timeout, &tv_now, info->probe_read_count * 100000);
icarus_read(devpath, nonce_bin, fd, &tv_finish, NULL, &tv_timeout, &tv_now, ICARUS_NONCE_SIZE);
// How many bytes were left after reading the above nonce
int bytes_left = icarus_excess_nonce_size(fd, info);
icarus_close(fd);
bin2hex(nonce_hex, nonce_bin, sizeof(nonce_bin));
if (strncmp(nonce_hex, info->golden_nonce, 8))
{
applog(LOG_DEBUG,
"%s: "
"Test failed at %s: get %s, should: %s",
api->dname,
devpath, nonce_hex, info->golden_nonce);
return NULL;
}
if (info->read_size - ICARUS_NONCE_SIZE != bytes_left)
{
applog(LOG_DEBUG,
"%s: "
"Test failed at %s: expected %d bytes, got %d",
api->dname,
devpath, info->read_size, ICARUS_NONCE_SIZE + bytes_left);
return NULL;
}
}
else
icarus_close(fd);
applog(LOG_DEBUG,
"%s: "
"Test succeeded at %s: got %s",
api->dname,
devpath, nonce_hex);
if (serial_claim_v(devpath, api))
return NULL;
_icarus_set_timing(info, devpath, api, "");
if (!info->fpga_count)
{
if (!info->work_division)
{
fd = icarus_open2(devpath, baud, true);
info->work_division = icarus_probe_work_division(fd, api->dname, info);
icarus_close(fd);
}
info->fpga_count = info->work_division;
}
// Lock fpga_count from set_work_division
info->user_set |= IUS_FPGA_COUNT;
/* We have a real Icarus! */
struct cgpu_info *icarus;
icarus = calloc(1, sizeof(struct cgpu_info));
icarus->drv = api;
icarus->device_path = strdup(devpath);
icarus->device_fd = -1;
icarus->threads = 1;
icarus->procs = info->fpga_count;
icarus->device_data = info;
icarus->set_device_funcs = icarus_set_device_funcs_live;
add_cgpu(icarus);
applog(LOG_INFO, "Found %s at %s",
icarus->dev_repr,
devpath);
applog(LOG_DEBUG, "%s: Init: baud=%d work_division=%d fpga_count=%d",
icarus->dev_repr,
baud, work_division, fpga_count);
timersub(&tv_finish, &tv_start, &(info->golden_tv));
return icarus;
}
static bool icarus_detect_one(const char *devpath)
{
struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
if (unlikely(!info))
quit(1, "Failed to malloc ICARUS_INFO");
// TODO: try some higher speeds with the Icarus and BFL to see
// if they support them and if setting them makes any difference
// N.B. B3000000 doesn't work on Icarus
info->baud = ICARUS_IO_SPEED;
info->reopen_mode = IRM_TIMEOUT;
info->Hs = ICARUS_REV3_HASH_TIME;
info->timing_mode = MODE_DEFAULT;
info->read_size = ICARUS_DEFAULT_READ_SIZE;
if (!icarus_detect_custom(devpath, &icarus_drv, info)) {
free(info);
return false;
}
return true;
}
static
bool icarus_lowl_probe(const struct lowlevel_device_info * const info)
{
return vcom_lowl_probe_wrapper(info, icarus_detect_one);
}
static bool icarus_prepare(struct thr_info *thr)
{
struct cgpu_info *icarus = thr->cgpu;
struct icarus_state *state;
thr->cgpu_data = state = calloc(1, sizeof(*state));
state->firstrun = true;
#ifdef HAVE_EPOLL
int epollfd = epoll_create(2);
if (epollfd != -1)
{
close(epollfd);
notifier_init(thr->work_restart_notifier);
}
#endif
icarus->status = LIFE_INIT2;
return true;
}
bool icarus_init(struct thr_info *thr)
{
struct cgpu_info *icarus = thr->cgpu;
struct ICARUS_INFO *info = icarus->device_data;
struct icarus_state * const state = thr->cgpu_data;
int fd = icarus_open2(icarus->device_path, info->baud, true);
icarus->device_fd = fd;
if (unlikely(-1 == fd)) {
applog(LOG_ERR, "%s: Failed to open %s",
icarus->dev_repr,
icarus->device_path);
return false;
}
applog(LOG_INFO, "%s: Opened %s", icarus->dev_repr, icarus->device_path);
BFGINIT(info->job_start_func, icarus_job_start);
BFGINIT(state->ob_bin, calloc(1, info->ob_size));
if (!info->work_division)
info->work_division = icarus_probe_work_division(fd, icarus->dev_repr, info);
if (!is_power_of_two(info->work_division))
info->work_division = upper_power_of_two_u32(info->work_division);
info->nonce_mask = mask(info->work_division);
return true;
}
static
const struct cgpu_info *icarus_proc_for_nonce(const struct cgpu_info * const icarus, const uint32_t nonce)
{
struct ICARUS_INFO * const info = icarus->device_data;
unsigned proc_id = 0;
for (int i = info->work_division, j = 0; i /= 2; ++j)
if (nonce & (1 << (31 - j)))
proc_id |= (1 << j);
const struct cgpu_info * const proc = device_proc_by_id(icarus, proc_id) ?: icarus;
return proc;
}
static bool icarus_reopen(struct cgpu_info *icarus, struct icarus_state *state, int *fdp)
{
struct ICARUS_INFO *info = icarus->device_data;
// Reopen the serial port to workaround a USB-host-chipset-specific issue with the Icarus's buggy USB-UART
do_icarus_close(icarus->thr[0]);
*fdp = icarus->device_fd = icarus_open(icarus->device_path, info->baud);
if (unlikely(-1 == *fdp)) {
applog(LOG_ERR, "%s: Failed to reopen on %s", icarus->dev_repr, icarus->device_path);
dev_error(icarus, REASON_DEV_COMMS_ERROR);
state->firstrun = true;
return false;
}
return true;
}
static
bool icarus_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
{
struct cgpu_info * const icarus = thr->cgpu;
struct icarus_state * const state = thr->cgpu_data;
uint8_t * const ob_bin = state->ob_bin;
swab256(ob_bin, work->midstate);
bswap_96p(&ob_bin[0x34], &work->data[0x40]);
if (!(memcmp(&ob_bin[56], "\xff\xff\xff\xff", 4)
|| memcmp(&ob_bin, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 32))) {
// This sequence is used on cairnsmore bitstreams for commands, NEVER send it otherwise
applog(LOG_WARNING, "%s: Received job attempting to send a command, corrupting it!",
icarus->dev_repr);
ob_bin[56] = 0;
}
return true;
}
bool icarus_job_start(struct thr_info *thr)
{
struct cgpu_info *icarus = thr->cgpu;
struct ICARUS_INFO *info = icarus->device_data;
struct icarus_state *state = thr->cgpu_data;
const uint8_t * const ob_bin = state->ob_bin;
int fd = icarus->device_fd;
int ret;
// Handle dynamic clocking for "subclass" devices
// This needs to run before sending next job, since it hashes the command too
if (info->dclk.freqM && likely(!state->firstrun)) {
dclk_preUpdate(&info->dclk);
dclk_updateFreq(&info->dclk, info->dclk_change_clock_func, thr);
}
cgtime(&state->tv_workstart);
ret = icarus_write(icarus->dev_repr, fd, ob_bin, info->ob_size);
if (ret) {
do_icarus_close(thr);
applog(LOG_ERR, "%s: Comms error (werr=%d)", icarus->dev_repr, ret);
dev_error(icarus, REASON_DEV_COMMS_ERROR);
return false; /* This should never happen */
}
return true;
}
static
struct work *icarus_process_worknonce(const struct ICARUS_INFO * const info, struct icarus_state *state, uint32_t *nonce)
{
*nonce = icarus_nonce32toh(info, *nonce);
if (test_nonce(state->last_work, *nonce, false))
return state->last_work;
if (likely(state->last2_work && test_nonce(state->last2_work, *nonce, false)))
return state->last2_work;
return NULL;
}
static
void handle_identify(struct thr_info * const thr, int ret, const bool was_first_run)
{
const struct cgpu_info * const icarus = thr->cgpu;
const struct ICARUS_INFO * const info = icarus->device_data;
struct icarus_state * const state = thr->cgpu_data;
int fd = icarus->device_fd;
struct timeval tv_now;
struct timeval tv_timeout, tv_finish;
double delapsed;
// For reading the nonce from Icarus
unsigned char nonce_bin[info->read_size];
// For storing the the 32-bit nonce
uint32_t nonce;
if (fd == -1)
return;
// If identify is requested (block erupters):
// 1. Don't start the next job right away (above)
// 2. Wait for the current job to complete 100%
if (!was_first_run)
{
applog(LOG_DEBUG, "%s: Identify: Waiting for current job to finish", icarus->dev_repr);
while (true)
{
cgtime(&tv_now);
delapsed = tdiff(&tv_now, &state->tv_workstart);
if (delapsed + 0.1 > info->fullnonce)
break;
// Try to get more nonces (ignoring work restart)
memset(nonce_bin, 0, sizeof(nonce_bin));
timer_set_delay(&tv_timeout, &tv_now, (uint64_t)(info->fullnonce - delapsed) * 1000000);
ret = icarus_read(icarus->dev_repr, nonce_bin, fd, &tv_finish, NULL, &tv_timeout, &tv_now, info->read_size);
if (ret == ICA_GETS_OK)
{
memcpy(&nonce, nonce_bin, sizeof(nonce));
nonce = icarus_nonce32toh(info, nonce);
submit_nonce(icarus_proc_for_nonce(icarus, nonce)->thr[0], state->last_work, nonce);
}
}
}
else
applog(LOG_DEBUG, "%s: Identify: Current job should already be finished", icarus->dev_repr);
// 3. Delay 3 more seconds
applog(LOG_DEBUG, "%s: Identify: Leaving idle for 3 seconds", icarus->dev_repr);
cgsleep_ms(3000);
// Check for work restart in the meantime
if (thr->work_restart)
{
applog(LOG_DEBUG, "%s: Identify: Work restart requested during delay", icarus->dev_repr);
goto no_job_start;
}
// 4. Start next job
if (!state->firstrun)
{
applog(LOG_DEBUG, "%s: Identify: Starting next job", icarus->dev_repr);
if (!info->job_start_func(thr))
no_job_start:
state->firstrun = true;
}
state->identify = false;
}
static
void icarus_transition_work(struct icarus_state *state, struct work *work)
{
if (state->last2_work)
free_work(state->last2_work);
state->last2_work = state->last_work;
state->last_work = copy_work(work);
}
static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
__maybe_unused int64_t max_nonce)
{
struct cgpu_info *icarus;
int fd;
int ret;
struct ICARUS_INFO *info;
struct work *nonce_work;
int64_t hash_count;
struct timeval tv_start = {.tv_sec=0}, elapsed;
struct timeval tv_history_start, tv_history_finish;
struct timeval tv_now, tv_timeout;
double Ti, Xi;
int i;
bool was_hw_error = false;
bool was_first_run;
struct ICARUS_HISTORY *history0, *history;
int count;
double Hs, W, fullnonce;
int read_timeout_ms;
bool limited;
uint32_t values;
int64_t hash_count_range;
elapsed.tv_sec = elapsed.tv_usec = 0;
icarus = thr->cgpu;
struct icarus_state *state = thr->cgpu_data;
was_first_run = state->firstrun;
icarus->drv->job_prepare(thr, work, max_nonce);
// Wait for the previous run's result
fd = icarus->device_fd;
info = icarus->device_data;
// For reading the nonce from Icarus
unsigned char nonce_bin[info->read_size];
// For storing the the 32-bit nonce
uint32_t nonce;
if (unlikely(fd == -1) && !icarus_reopen(icarus, state, &fd))
return -1;
if (!state->firstrun) {
if (state->changework)
{
state->changework = false;
ret = ICA_GETS_RESTART;
}
else
{
read_timeout_ms = info->read_timeout_ms;
keepwaiting:
/* Icarus will return info->read_size bytes nonces or nothing */
memset(nonce_bin, 0, sizeof(nonce_bin));
timer_set_now(&tv_now);
timer_set_delay(&tv_timeout, &tv_now, read_timeout_ms * 1000);
ret = icarus_read(icarus->dev_repr, nonce_bin, fd, &state->tv_workfinish, thr, &tv_timeout, &tv_now, info->read_size);
switch (ret) {
case ICA_GETS_RESTART:
// The prepared work is invalid, and the current work is abandoned
// Go back to the main loop to get the next work, and stuff
// Returning to the main loop will clear work_restart, so use a flag...
state->changework = true;
return 0;
case ICA_GETS_ERROR:
do_icarus_close(thr);
applog(LOG_ERR, "%s: Comms error (rerr)", icarus->dev_repr);
dev_error(icarus, REASON_DEV_COMMS_ERROR);
if (!icarus_reopen(icarus, state, &fd))
return -1;
break;
case ICA_GETS_TIMEOUT:
if (info->reopen_mode == IRM_TIMEOUT && !icarus_reopen(icarus, state, &fd))
return -1;
case ICA_GETS_OK: