-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver-cointerra.c
1370 lines (1172 loc) · 41 KB
/
driver-cointerra.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-2014 Con Kolivas
* Copyright 2014 Luke Dashjr
*
* 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 <string.h>
#include "miner.h"
#include "deviceapi.h"
#include "driver-cointerra.h"
#include "lowlevel.h"
#include "lowl-usb.h"
#include <math.h>
static const unsigned cointerra_desired_roll = 60;
static const unsigned long cointerra_latest_result_usecs = (10 * 1000000);
static const unsigned cointerra_max_nonce_diff = 0x20;
#define COINTERRA_USB_TIMEOUT 500
#define COINTERRA_PACKET_SIZE 0x40
#define COINTERRA_START_SEQ 0x5a,0x5a
#define COINTERRA_MSG_SIZE (COINTERRA_PACKET_SIZE - sizeof(cointerra_startseq))
#define COINTERRA_MSGBODY_SIZE (COINTERRA_MSG_SIZE - 1)
BFG_REGISTER_DRIVER(cointerra_drv)
static const struct bfg_set_device_definition cointerra_set_device_funcs[];
enum cointerra_msg_type_out {
CMTO_RESET = 1,
CMTO_WORK = 2,
CMTO_REQUEST = 4,
CMTO_HWERR = 5,
CMTO_LEDCTL = 6,
CMTO_HASHRATE = 7,
CMTO_GET_INFO = 0x21,
};
static const uint8_t cointerra_startseq[] = {COINTERRA_START_SEQ};
static const char *cointerra_hdr = "ZZ";
static void cta_gen_message(char *msg, char type)
{
memset(msg, 0, CTA_MSG_SIZE);
memcpy(msg, cointerra_hdr, 2);
msg[CTA_MSG_TYPE] = type;
}
/* Find the number of leading zero bits in diff */
static uint8_t diff_to_bits(double diff)
{
uint64_t diff64;
uint8_t i;
diff *= (double)2147483648.0;
if (diff > 0x8000000000000000ULL)
diff = 0x8000000000000000ULL;
/* Convert it to an integer */
diff64 = diff;
for (i = 0; diff64; i++, diff64 >>= 1);
return i;
}
static double bits_to_diff(uint8_t bits)
{
double ret = 1.0;
if (likely(bits > 32))
ret *= 1ull << (bits - 32);
else if (unlikely(bits < 32))
ret /= 1ull << (32 - bits);
return ret;
}
static bool cta_reset_init(char *buf)
{
return ((buf[CTA_MSG_TYPE] == CTA_RECV_RDONE) && ((buf[CTA_RESET_TYPE]&0x3) == CTA_RESET_INIT));
}
static char *mystrstr(char *haystack, int size, const char *needle)
{
int loop = 0;
while (loop < (size-1)) {
if ((haystack[loop] == needle[0])&&
(haystack[loop+1] == needle[1]))
return &haystack[loop];
loop++;
}
return NULL;
}
static
bool cta_open(struct lowl_usb_endpoint * const ep, const char * const repr, struct cointerra_info * const devstate)
{
int amount, offset = 0;
char buf[CTA_MSG_SIZE];
cgtimer_t ts_start;
bool ret = false;
applog(LOG_INFO, "CTA_OPEN");
cta_gen_message(buf, CTA_SEND_RESET);
// set the initial difficulty
buf[CTA_RESET_TYPE] = CTA_RESET_INIT | CTA_RESET_DIFF;
buf[CTA_RESET_DIFF] = diff_to_bits(CTA_INIT_DIFF);
buf[CTA_RESET_LOAD] = devstate->set_load ?: 255;
buf[CTA_RESET_PSLOAD] = 0;
amount = usb_write(ep, buf, CTA_MSG_SIZE);
if (amount != CTA_MSG_SIZE) {
applog(LOG_INFO, "Write error %s, wrote %d of %d",
bfg_strerror(errno, BST_ERRNO),
amount, CTA_MSG_SIZE);
return ret;
}
cgtimer_time(&ts_start);
/* Read from the device for up to 2 seconds discarding any data that
* doesn't match a reset complete acknowledgement. */
while (42) {
cgtimer_t ts_now, ts_diff;
char *msg;
cgtimer_time(&ts_now);
cgtimer_sub(&ts_now, &ts_start, &ts_diff);
if (cgtimer_to_ms(&ts_diff) > 2000) {
applog(LOG_DEBUG, "%s: Timed out waiting for response to reset init", repr);
break;
}
amount = usb_read(ep, buf + offset, CTA_MSG_SIZE - offset);
if (amount != (CTA_MSG_SIZE - offset) && amount != 0) {
applog(LOG_INFO, "%s: Read error %s, read %d",
repr, bfg_strerror(errno, BST_ERRNO), amount);
break;
}
if (!amount)
continue;
msg = mystrstr(buf, amount, cointerra_hdr);
if (!msg) {
/* Keep the last byte in case it's the first byte of
* the 2 byte header. */
offset = 1;
memmove(buf, buf + amount - 1, offset);
continue;
}
if (msg > buf) {
/* length of message = offset for next usb_read after moving */
offset = CTA_MSG_SIZE - (msg - buf);
memmove(buf, msg, offset);
continue;
}
/* We have a full sized message starting with the header now */
if (cta_reset_init(buf)) {
/* We can't store any other data returned with this
* reset since we have not allocated any memory for
* a cointerra_info structure yet. */
applog(LOG_INFO, "%s: Successful reset init received", repr);
ret = true;
break;
}
}
return ret;
}
static
bool cointerra_open(const struct lowlevel_device_info * const info, const char * const repr, struct libusb_device_handle ** const usbh_p, struct lowl_usb_endpoint ** const ep_p, struct cointerra_info * const devstate)
{
int e;
if (libusb_open(info->lowl_data, usbh_p))
applogr(false, LOG_DEBUG, "%s: USB open failed on %s",
cointerra_drv.dname, info->devid);
if ( (e = libusb_set_configuration(*usbh_p, 1)) )
return_via_applog(fail, , LOG_ERR, "%s: Failed to %s on %s: %s", repr, "set configuration 1", info->devid, bfg_strerror(e, BST_LIBUSB));
if ( (e = libusb_claim_interface(*usbh_p, 0)) )
return_via_applog(fail, , LOG_ERR, "%s: Failed to %s on %s: %s", repr, "claim interface 0", info->devid, bfg_strerror(e, BST_LIBUSB));
*ep_p = usb_open_ep_pair(*usbh_p, LIBUSB_ENDPOINT_IN | 1, 64, LIBUSB_ENDPOINT_OUT | 1, 64);
if (!*ep_p)
{
applog(LOG_DEBUG, "%s: Endpoint open failed on %s",
cointerra_drv.dname, info->devid);
fail:
libusb_close(*usbh_p);
*usbh_p = NULL;
return false;
}
if (!cta_open(*ep_p, repr, devstate))
{
usb_close_ep(*ep_p);
*ep_p = NULL;
goto fail;
}
return true;
}
static void cta_clear_work(struct cgpu_info *cgpu)
{
struct work *work, *tmp;
wr_lock(&cgpu->qlock);
HASH_ITER(hh, cgpu->queued_work, work, tmp) {
__work_completed(cgpu, work);
free_work(work);
}
wr_unlock(&cgpu->qlock);
}
static void cta_close(struct cgpu_info *cointerra)
{
struct cointerra_info *info = cointerra->device_data;
if (info->ep)
{
/* Open does the same reset init followed by response as is required to
* close the device. */
if (!cta_open(info->ep, cointerra->dev_repr, info)) {
applog(LOG_INFO, "%s %d: Reset on close failed", cointerra->drv->name,
cointerra->device_id);
}
}
mutex_destroy(&info->lock);
mutex_destroy(&info->sendlock);
/* Don't free info here to avoid trying to access dereferenced members
* once a device is unplugged. */
cta_clear_work(cointerra);
}
static void cta_parse_info(struct cgpu_info *, struct cointerra_info *, char *);
static void msg_from_hu16(char *, int, uint16_t);
static
bool cointerra_wait_for_info(struct cointerra_info * const ctainfo, struct lowl_usb_endpoint * const ep)
{
char buf[CTA_MSG_SIZE];
int amount;
cta_gen_message(buf, CTA_SEND_REQUEST);
msg_from_hu16(buf, CTA_REQ_MSGTYPE, CTA_RECV_INFO);
msg_from_hu16(buf, CTA_REQ_INTERVAL, 0);
amount = usb_write(ep, buf, CTA_MSG_SIZE);
if (amount != CTA_MSG_SIZE)
return false;
do {
amount = usb_read(ep, buf, CTA_MSG_SIZE);
if (amount != CTA_MSG_SIZE)
applogr(false, LOG_ERR, "%s: Read error %s, read %d",
__func__, bfg_strerror(errno, BST_ERRNO), amount);
if (memcmp(buf, cointerra_hdr, 2))
applogr(false, LOG_ERR, "%s: Packet header mismatch", __func__);
} while (buf[CTA_MSG_TYPE] != CTA_RECV_INFO);
cta_parse_info(NULL, ctainfo, buf);
return true;
}
static
bool cointerra_lowl_probe(const struct lowlevel_device_info * const info)
{
struct cointerra_info ctainfo = { .set_load = 0, };
struct libusb_device_handle *usbh;
struct lowl_usb_endpoint *ep;
bool b;
if (info->lowl != &lowl_usb)
{
bfg_probe_result_flags = BPR_WRONG_DEVTYPE;
return false;
}
if (!cointerra_open(info, cointerra_drv.dname, &usbh, &ep, &ctainfo))
return false;
mutex_init(&ctainfo.lock);
b = cointerra_wait_for_info(&ctainfo, ep);
mutex_destroy(&ctainfo.lock);
usb_close_ep(ep);
libusb_close(usbh);
if (!b)
return false;
applog(LOG_DEBUG, "%s: Found %lu cores on %s",
__func__, (unsigned long)ctainfo.cores, info->devid);
libusb_device * const usbdev = info->lowl_data;
if (bfg_claim_libusb(&cointerra_drv, true, usbdev))
return false;
struct cgpu_info * const dev = malloc(sizeof(*dev));
*dev = (struct cgpu_info){
.drv = &cointerra_drv,
.set_device_funcs = cointerra_set_device_funcs,
.procs = ctainfo.cores,
.device_data = lowlevel_ref(info),
.threads = 1,
.device_path = strdup(info->devid),
.dev_manufacturer = maybe_strdup(info->manufacturer),
.dev_product = maybe_strdup(info->product),
.dev_serial = maybe_strdup(info->serial),
.deven = DEV_ENABLED,
};
const bool rv = add_cgpu(dev);
applog(LOG_INFO, "%s: Successfully set up %s",
cointerra_drv.dname, dev->dev_repr);
return rv;
}
static
void cointerra_set_queue_full(struct cgpu_info * const dev, const bool nv)
{
if (dev->thr[0]->queue_full == nv)
return;
for_each_managed_proc(proc, dev)
proc->thr[0]->queue_full = nv;
}
static
bool cointerra_lowl_match(const struct lowlevel_device_info * const info)
{
return lowlevel_match_lowlproduct(info, &lowl_usb, "GoldStrike");
}
/* This function will remove a work item from the hashtable if it matches the
* id in work->subid and return a pointer to the work but it will not free the
* work. It may return NULL if it cannot find matching work. */
static struct work *take_work_by_id(struct cgpu_info *cgpu, uint16_t id)
{
struct work *work, *tmp, *ret = NULL;
wr_lock(&cgpu->qlock);
HASH_ITER(hh, cgpu->queued_work, work, tmp) {
if (work->subid == id) {
ret = work;
break;
}
}
if (ret)
__work_completed(cgpu, ret);
wr_unlock(&cgpu->qlock);
return ret;
}
/* This function will look up a work item in the hashtable if it matches the
* id in work->subid and return a cloned work item if it matches. It may return
* NULL if it cannot find matching work. */
static struct work *clone_work_by_id(struct cgpu_info *cgpu, uint16_t id)
{
struct work *work, *tmp, *ret = NULL;
rd_lock(&cgpu->qlock);
HASH_ITER(hh, cgpu->queued_work, work, tmp) {
if (work->subid == id) {
ret = work;
break;
}
}
if (ret)
ret = copy_work(ret);
rd_unlock(&cgpu->qlock);
return ret;
}
static bool cta_send_msg(struct cgpu_info *cointerra, char *buf);
static uint16_t hu16_from_msg(char *buf, int msg)
{
return le16toh(*(uint16_t *)&buf[msg]);
}
static uint32_t hu32_from_msg(char *buf, int msg)
{
return le32toh(*(uint32_t *)&buf[msg]);
}
static uint64_t hu64_from_msg(char *buf, int msg)
{
return le64toh(*(uint64_t *)&buf[msg]);
}
static uint8_t u8_from_msg(char *buf, int msg)
{
return *(uint8_t *)&buf[msg];
}
static void msg_from_hu16(char *buf, int msg, uint16_t val)
{
*(uint16_t *)&buf[msg] = htole16(val);
}
static void cta_parse_reqwork(struct cgpu_info *cointerra, struct cointerra_info *info,
char *buf)
{
uint16_t retwork;
retwork = hu16_from_msg(buf, CTA_REQWORK_REQUESTS);
applog(LOG_DEBUG, "%s %d: Request work message for %u items received",
cointerra->drv->name, cointerra->device_id, retwork);
mutex_lock(&info->lock);
info->requested = retwork;
cointerra_set_queue_full(cointerra, !retwork);
mutex_unlock(&info->lock);
}
static void cta_parse_recvmatch(struct thr_info *thr, struct cgpu_info *cointerra,
struct cointerra_info *info, char *buf)
{
struct cgpu_info *corecgpu;
struct thr_info *corethr;
uint32_t timestamp_offset, mcu_tag;
uint16_t retwork;
struct work *work;
uint8_t asic, core, pipe, coreno;
int pipeno, bitchar, bitbit;
/* No endian switch needs doing here since it's sent and returned as
* the same 4 bytes */
retwork = *(uint16_t *)(&buf[CTA_DRIVER_TAG]);
mcu_tag = hu32_from_msg(buf, CTA_MCU_TAG);
const uint8_t wdiffbits = u8_from_msg(buf, CTA_WORK_DIFFBITS);
const uint32_t nonce = hu32_from_msg(buf, CTA_MATCH_NONCE);
asic = u8_from_msg(buf, CTA_MCU_ASIC);
core = u8_from_msg(buf, CTA_MCU_CORE);
pipe = u8_from_msg(buf, CTA_MCU_PIPE);
pipeno = asic * 512 + core * 128 + pipe;
// For some reason, pipe numbers skip 0x?f
const int bfg_pipeno = ((pipe >> 4) * 0xf) + (pipe & 0xf);
const unsigned procno = (asic * 480) + (core * 120) + bfg_pipeno;
corecgpu = device_proc_by_id(cointerra, procno) ?: cointerra;
corethr = corecgpu->thr[0];
applog(LOG_DEBUG, "%s %d: Match message for id 0x%04x MCU id 0x%08x received",
cointerra->drv->name, cointerra->device_id, retwork, mcu_tag);
work = clone_work_by_id(cointerra, retwork);
if (likely(work)) {
unsigned char rhash[32];
char outhash[16];
double wdiff;
uint64_t hashes;
bool ret;
timestamp_offset = hu32_from_msg(buf, CTA_MATCH_NOFFSET);
if (timestamp_offset) {
struct work *base_work = work;
work = copy_work_noffset(base_work, timestamp_offset);
free_work(base_work);
}
/* Test against the difficulty we asked for along with the work */
wdiff = bits_to_diff(wdiffbits);
hashes = (uint64_t)wdiff * 0x100000000ull;
ret = true; // TODO: test_nonce_diff(work, nonce, wdiff);
if (opt_debug) {
/* Debugging, remove me */
swab256(rhash, work->hash);
bin2hex(outhash, rhash, 8);
applog(LOG_DEBUG, "submit work %s 0x%04x 0x%08x %d 0x%08x",
outhash, retwork, mcu_tag, timestamp_offset, nonce);
}
hashes_done2(corethr, hashes, NULL);
if (likely(ret)) {
coreno = asic * 4 + core;
if (unlikely(asic > 1 || core > 3 || pipe > 127 || pipeno > 1023)) {
applog(LOG_WARNING, "%s %d: MCU invalid pipe asic %d core %d pipe %d",
cointerra->drv->name, cointerra->device_id, asic, core, pipe);
coreno = 0;
} else {
info->last_pipe_nonce[pipeno] = time(NULL);
bitchar = pipeno / 8;
bitbit = pipeno % 8;
info->pipe_bitmap[bitchar] |= 0x80 >> bitbit;
}
applog(LOG_DEBUG, "%"PRIpreprv": Submitting tested work job_id %s work_id %u",
corecgpu->proc_repr, work->job_id, work->subid);
ret = submit_nonce(corethr, work, nonce);
mutex_lock(&info->lock);
info->share_hashes += hashes;
info->tot_core_hashes[coreno] += hashes;
info->hashes += nonce;
mutex_unlock(&info->lock);
} else {
char sendbuf[CTA_MSG_SIZE];
applog(LOG_DEBUG, "%s %d: Notify bad match work",
cointerra->drv->name, cointerra->device_id);
if (opt_debug) {
unsigned char midstate[32], wdata[12];
char hexmidstate[68], hexwdata[28];
uint16_t wid;
memcpy(&wid, &info->work_id, 2);
flip32(midstate, work->midstate);
bin2hex(hexmidstate, midstate, 32);
flip12(wdata, &work->data[64]);
bin2hex(hexwdata, wdata, 12);
applog(LOG_DEBUG, "False match sent: work id %u midstate %s blkhdr %s",
wid, hexmidstate, hexwdata);
applog(LOG_DEBUG, "False match reports: work id 0x%04x MCU id 0x%08x work diff %.1f",
retwork, mcu_tag, wdiff);
applog(LOG_DEBUG, "False match tested: nonce 0x%08x noffset %d %s",
nonce, timestamp_offset, outhash);
}
/* Tell the device we got a false match */
cta_gen_message(sendbuf, CTA_SEND_FMATCH);
memcpy(sendbuf + 3, buf, CTA_MSG_SIZE - 3);
cta_send_msg(cointerra, sendbuf);
}
free_work(work);
} else {
applog(LOG_INFO, "%s %d: Matching work id 0x%X %d not found", cointerra->drv->name,
cointerra->device_id, retwork, __LINE__);
inc_hw_errors3(corethr, NULL, &nonce, bits_to_diff(wdiffbits));
mutex_lock(&info->lock);
info->no_matching_work++;
mutex_unlock(&info->lock);
}
}
static void cta_parse_wdone(struct thr_info *thr, struct cgpu_info *cointerra,
struct cointerra_info *info, char *buf)
{
uint16_t retwork = *(uint16_t *)(&buf[CTA_DRIVER_TAG]);
struct work *work = take_work_by_id(cointerra, retwork);
uint64_t hashes;
if (likely(work))
free_work(work);
else {
applog(LOG_INFO, "%s %d: Done work not found id 0x%X %d",
cointerra->drv->name, cointerra->device_id, retwork, __LINE__);
inc_hw_errors_only(thr);
}
/* Removing hashes from work done message */
hashes = hu64_from_msg(buf, CTA_WDONE_NONCES);
if (unlikely(hashes > (61 * 0x100000000ull))) {
applog(LOG_INFO, "%s Invalid hash returned %"PRIu64"x %"PRIu64"x %"PRIu64"X",
__func__, info->hashes, hashes, hashes);
hashes = 0;
}
mutex_lock(&info->lock);
info->hashes += hashes;
mutex_unlock(&info->lock);
}
static void u16array_from_msg(uint16_t *u16, int entries, int var, char *buf)
{
int i, j;
for (i = 0, j = 0; i < entries; i++, j += sizeof(uint16_t))
u16[i] = hu16_from_msg(buf, var + j);
}
static void cta_parse_statread(struct cgpu_info *cointerra, struct cointerra_info *info,
char *buf)
{
int i;
mutex_lock(&info->lock);
u16array_from_msg(info->coretemp, CTA_CORES, CTA_STAT_CORETEMPS, buf);
info->ambtemp_low = hu16_from_msg(buf, CTA_STAT_AMBTEMP_LOW);
info->ambtemp_avg = hu16_from_msg(buf, CTA_STAT_AMBTEMP_AVG);
info->ambtemp_high = hu16_from_msg(buf, CTA_STAT_AMBTEMP_HIGH);
u16array_from_msg(info->pump_tachs, CTA_PUMPS, CTA_STAT_PUMP_TACHS, buf);
u16array_from_msg(info->fan_tachs, CTA_FANS, CTA_STAT_FAN_TACHS, buf);
u16array_from_msg(info->corevolts, CTA_CORES, CTA_STAT_CORE_VOLTS, buf);
info->volts33 = hu16_from_msg(buf, CTA_STAT_VOLTS33);
info->volts12 = hu16_from_msg(buf, CTA_STAT_VOLTS12);
info->inactive = hu16_from_msg(buf, CTA_STAT_INACTIVE);
info->active = hu16_from_msg(buf, CTA_STAT_ACTIVE);
mutex_unlock(&info->lock);
struct cgpu_info *proc = cointerra;
for (i = 0; i < CTA_CORES; i++) {
float temp = info->coretemp[i] / 100.;
for (int j = 0; j < 120; (++j), (proc = proc->next_proc))
proc->temp = temp;
}
}
static void u8array_from_msg(uint8_t *u8, int entries, int var, char *buf)
{
int i;
for (i = 0; i < entries; i++)
u8[i] = u8_from_msg(buf, var + i);
}
static void cta_parse_statset(struct cointerra_info *info, char *buf)
{
mutex_lock(&info->lock);
u8array_from_msg(info->coreperf, CTA_CORES, CTA_STAT_PERFMODE, buf);
u8array_from_msg(info->fanspeed, CTA_FANS, CTA_STAT_FANSPEEDS, buf);
info->dies_active = u8_from_msg(buf, CTA_STAT_DIES_ACTIVE);
u8array_from_msg(info->pipes_enabled, CTA_CORES, CTA_STAT_PIPES_ENABLED, buf);
u16array_from_msg(info->corefreqs, CTA_CORES, CTA_STAT_CORE_FREQS, buf);
info->uptime = hu32_from_msg(buf,CTA_STAT_UPTIME);
mutex_unlock(&info->lock);
}
static void cta_parse_info(struct cgpu_info *cointerra, struct cointerra_info *info,
char *buf)
{
mutex_lock(&info->lock);
info->hwrev = hu64_from_msg(buf, CTA_INFO_HWREV);
info->serial = hu32_from_msg(buf, CTA_INFO_SERNO);
info->asics = u8_from_msg(buf, CTA_INFO_NUMASICS);
info->dies = u8_from_msg(buf, CTA_INFO_NUMDIES);
info->cores = hu16_from_msg(buf, CTA_INFO_NUMCORES);
info->board_number = u8_from_msg(buf, CTA_INFO_BOARDNUMBER);
info->fwrev[0] = u8_from_msg(buf, CTA_INFO_FWREV_MAJ);
info->fwrev[1] = u8_from_msg(buf, CTA_INFO_FWREV_MIN);
info->fwrev[2] = u8_from_msg(buf, CTA_INFO_FWREV_MIC);
info->fw_year = hu16_from_msg(buf, CTA_INFO_FWDATE_YEAR);
info->fw_month = u8_from_msg(buf, CTA_INFO_FWDATE_MONTH);
info->fw_day = u8_from_msg(buf, CTA_INFO_FWDATE_DAY);
info->init_diffbits = u8_from_msg(buf, CTA_INFO_INITDIFFBITS);
info->min_diffbits = u8_from_msg(buf, CTA_INFO_MINDIFFBITS);
info->max_diffbits = u8_from_msg(buf, CTA_INFO_MAXDIFFBITS);
mutex_unlock(&info->lock);
#if 0
if (!cointerra->unique_id) {
uint32_t b32 = htobe32(info->serial);
cointerra->unique_id = malloc((4 * 2) + 1);
bin2hex(cointerra->unique_id, &b32, 4);
}
#endif
}
static void cta_parse_rdone(struct cgpu_info *cointerra, struct cointerra_info *info,
char *buf)
{
uint8_t reset_type, diffbits;
uint64_t wdone;
reset_type = buf[CTA_RESET_TYPE];
diffbits = buf[CTA_RESET_DIFF];
wdone = hu64_from_msg(buf, CTA_WDONE_NONCES);
applog(LOG_INFO, "%s %d: Reset done type %u message %u diffbits %"PRIu64" done received",
cointerra->drv->name, cointerra->device_id, reset_type, diffbits, wdone);
if (wdone) {
applog(LOG_INFO, "%s %d: Reset done type %u message %u diffbits %"PRIu64" done received",
cointerra->drv->name, cointerra->device_id, reset_type, diffbits, wdone);
mutex_lock(&info->lock);
info->hashes += wdone;
mutex_unlock(&info->lock);
}
/* Note that the cgsem that is posted here must not be waited on while
* holding the info->lock to not get into a livelock since this
* function also grabs the lock first and it's always best to not sleep
* while holding a lock. */
if (reset_type == CTA_RESET_NEW) {
cta_clear_work(cointerra);
/* Tell reset sender that the reset is complete
* and it may resume. */
notifier_wake(info->reset_notifier);
}
}
static void cta_zero_stats(struct cgpu_info *cointerra);
static void cta_parse_debug(struct cointerra_info *info, char *buf)
{
mutex_lock(&info->lock);
info->tot_underruns = hu16_from_msg(buf, CTA_STAT_UNDERRUNS);
u16array_from_msg(info->tot_hw_errors, CTA_CORES, CTA_STAT_HW_ERRORS, buf);
info->tot_hashes = hu64_from_msg(buf, CTA_STAT_HASHES);
info->tot_flushed_hashes = hu64_from_msg(buf, CTA_STAT_FLUSHED_HASHES);
info->autovoltage = u8_from_msg(buf, CTA_STAT_AUTOVOLTAGE);
info->current_ps_percent = u8_from_msg(buf, CTA_STAT_POWER_PERCENT);
info->power_used = hu16_from_msg(buf,CTA_STAT_POWER_USED);
info->power_voltage = hu16_from_msg(buf,CTA_STAT_VOLTAGE);
info->ipower_used = hu16_from_msg(buf,CTA_STAT_IPOWER_USED);
info->ipower_voltage = hu16_from_msg(buf,CTA_STAT_IVOLTAGE);
info->power_temps[0] = hu16_from_msg(buf,CTA_STAT_PS_TEMP1);
info->power_temps[1] = hu16_from_msg(buf,CTA_STAT_PS_TEMP2);
mutex_unlock(&info->lock);
#if 0
/* Autovoltage is positive only once at startup and eventually drops
* to zero. After that time we reset the stats since they're unreliable
* till then. */
if (unlikely(!info->autovoltage_complete && !info->autovoltage)) {
struct cgpu_info *cointerra = info->thr->cgpu;
info->autovoltage_complete = true;
cgtime(&cointerra->dev_start_tv);
cta_zero_stats(cointerra);
cointerra->total_mhashes = 0;
cointerra->accepted = 0;
cointerra->rejected = 0;
cointerra->hw_errors = 0;
cointerra->utility = 0.0;
cointerra->last_share_pool_time = 0;
cointerra->diff1 = 0;
cointerra->diff_accepted = 0;
cointerra->diff_rejected = 0;
cointerra->last_share_diff = 0;
}
#endif
}
static void cta_parse_msg(struct thr_info *thr, struct cgpu_info *cointerra,
struct cointerra_info *info, char *buf)
{
switch (buf[CTA_MSG_TYPE]) {
default:
case CTA_RECV_UNUSED:
applog(LOG_INFO, "%s %d: Unidentified message type %u",
cointerra->drv->name, cointerra->device_id, buf[CTA_MSG_TYPE]);
break;
case CTA_RECV_REQWORK:
cta_parse_reqwork(cointerra, info, buf);
break;
case CTA_RECV_MATCH:
cta_parse_recvmatch(thr, cointerra, info, buf);
break;
case CTA_RECV_WDONE:
applog(LOG_DEBUG, "%s %d: Work done message received",
cointerra->drv->name, cointerra->device_id);
cta_parse_wdone(thr, cointerra, info, buf);
break;
case CTA_RECV_STATREAD:
applog(LOG_DEBUG, "%s %d: Status readings message received",
cointerra->drv->name, cointerra->device_id);
cta_parse_statread(cointerra, info, buf);
break;
case CTA_RECV_STATSET:
applog(LOG_DEBUG, "%s %d: Status settings message received",
cointerra->drv->name, cointerra->device_id);
cta_parse_statset(info, buf);
break;
case CTA_RECV_INFO:
applog(LOG_DEBUG, "%s %d: Info message received",
cointerra->drv->name, cointerra->device_id);
cta_parse_info(cointerra, info, buf);
break;
case CTA_RECV_MSG:
applog(LOG_NOTICE, "%s: Devlog: %.*s", cointerra->dev_repr, (int)COINTERRA_MSGBODY_SIZE, &buf[CTA_MSG_RECVD]);
break;
case CTA_RECV_RDONE:
cta_parse_rdone(cointerra, info, buf);
break;
case CTA_RECV_STATDEBUG:
cta_parse_debug(info, buf);
break;
}
}
static
void cta_recv_thread(void *arg)
{
struct thr_info *thr = (struct thr_info *)arg;
struct cgpu_info *cointerra = thr->cgpu;
struct cointerra_info *info = cointerra->device_data;
int offset = 0;
while (likely(!cointerra->shutdown)) {
char buf[CTA_READBUF_SIZE];
int amount;
if (unlikely(0))
{
applog(LOG_DEBUG, "%s %d: Device disappeared, disabling recv thread",
cointerra->drv->name, cointerra->device_id);
break;
}
amount = usb_read(info->ep, buf + offset, CTA_MSG_SIZE);
if (amount != CTA_MSG_SIZE && amount != 0) {
applog(LOG_ERR, "%s: Read error %s, read %d",
cointerra->dev_repr, bfg_strerror(errno, BST_ERRNO), amount);
break;
}
offset += amount;
while (offset >= CTA_MSG_SIZE) {
char *msg = mystrstr(buf, offset, cointerra_hdr);
int begin;
if (unlikely(!msg)) {
applog(LOG_WARNING, "%s %d: No message header found, discarding buffer",
cointerra->drv->name, cointerra->device_id);
inc_hw_errors_only(thr);
/* Save the last byte in case it's the fist
* byte of a header. */
begin = CTA_MSG_SIZE - 1;
offset -= begin;
memmove(buf, buf + begin, offset);
continue;
}
if (unlikely(msg != buf)) {
begin = msg - buf;
applog(LOG_WARNING, "%s %d: Reads out of sync, discarding %d bytes",
cointerra->drv->name, cointerra->device_id, begin);
inc_hw_errors_only(thr);
offset -= begin;
memmove(buf, msg, offset);
if (offset < CTA_MSG_SIZE)
break;
}
/* We have enough buffer for a full message, parse now */
cta_parse_msg(thr, cointerra, info, msg);
offset -= CTA_MSG_SIZE;
if (offset > 0)
memmove(buf, buf + CTA_MSG_SIZE, offset);
}
break;
}
}
static
bool cointerra_write_msg(struct lowl_usb_endpoint * const ep, const char * const repr, const uint8_t msgtype, const void * const msgbody)
{
uint8_t buf[COINTERRA_PACKET_SIZE], *p;
memcpy(buf, cointerra_startseq, sizeof(cointerra_startseq));
p = &buf[sizeof(cointerra_startseq)];
pk_u8(p, 0, msgtype);
memcpy(&p[1], msgbody, COINTERRA_MSGBODY_SIZE);
if (usb_write(ep, buf, sizeof(buf)) != sizeof(buf))
return false;
return true;
}
static bool cta_send_msg(struct cgpu_info *cointerra, char *buf)
{
struct cointerra_info *info = cointerra->device_data;
int amount;
/* Serialise usb writes to prevent overlap in case multiple threads
* send messages */
mutex_lock(&info->sendlock);
amount = usb_write(info->ep, buf, CTA_MSG_SIZE);
mutex_unlock(&info->sendlock);
if (unlikely(amount != CTA_MSG_SIZE)) {
applog(LOG_ERR, "%s: Write error %s, wrote %d of %d",
cointerra->dev_repr, bfg_strerror(errno, BST_ERRNO), amount, CTA_MSG_SIZE);
return false;
}
return true;
}
static bool cta_prepare(struct thr_info *thr)
{
struct cgpu_info *cointerra = thr->cgpu;
struct lowlevel_device_info * const llinfo = cointerra->device_data;
struct cointerra_info *info = calloc(sizeof(struct cointerra_info), 1);
char buf[CTA_MSG_SIZE];
sleep(1);
if (unlikely(!info))
quit(1, "Failed to calloc info in cta_detect_one");
for_each_managed_proc(proc, cointerra)
proc->device_data = info;
/* Nominally set a requested value when starting, preempting the need
* for a req-work message. */
info->requested = CTA_MAX_QUEUE;
cointerra_set_queue_full(cointerra, false);
cgpu_set_defaults(cointerra);
bool open_rv = cointerra_open(llinfo, cointerra->dev_repr, &info->usbh, &info->ep, info);
lowlevel_devinfo_free(llinfo);
if (!open_rv)
return false;
info->thr = thr;
mutex_init(&info->lock);
mutex_init(&info->sendlock);
notifier_init(info->reset_notifier);
/* Request a single status setting message */
cta_gen_message(buf, CTA_SEND_REQUEST);
msg_from_hu16(buf, CTA_REQ_MSGTYPE, CTA_RECV_STATSET);
msg_from_hu16(buf, CTA_REQ_INTERVAL, 0);
if (!cta_send_msg(cointerra, buf))
return false;
/* Request status debug messages every 60 seconds */
cta_gen_message(buf, CTA_SEND_REQUEST);
msg_from_hu16(buf, CTA_REQ_MSGTYPE, CTA_RECV_STATDEBUG);
msg_from_hu16(buf, CTA_REQ_INTERVAL, 6000);
if (!cta_send_msg(cointerra, buf))
return false;
cgtime(&info->core_hash_start);
usb_ep_set_timeouts_ms(info->ep, COINTERRA_USB_TIMEOUT, COINTERRA_USB_TIMEOUT);
timer_set_now(&thr->tv_poll);
return true;
}
static void cta_send_reset(struct cgpu_info *cointerra, struct cointerra_info *info,
uint8_t reset_type, uint8_t diffbits);
static void cta_flush_work(struct cgpu_info *cointerra);
static
bool cointerra_queue_append(struct thr_info * const thr, struct work * const work)
{
struct cgpu_info * const dev = thr->cgpu->device;
// struct thr_info * const master_thr = dev->thr[0];
struct cointerra_info * const devstate = dev->device_data;
struct timeval tv_now, tv_latest;
uint8_t buf[COINTERRA_MSGBODY_SIZE] = {0};
uint16_t ntimeroll, zerobits;
if (unlikely(!devstate->requested))
{
applog(LOG_DEBUG, "%s: Attempt to queue work while none requested; rejecting", dev->dev_repr);
cointerra_set_queue_full(dev, true);
return false;
}
timer_set_now(&tv_now);
timer_set_delay(&tv_latest, &tv_now, cointerra_latest_result_usecs);
ntimeroll = max(0, work_ntime_range(work, &tv_now, &tv_latest, cointerra_desired_roll));
if (unlikely(!devstate->work_id))
++devstate->work_id;
work->device_id = devstate->work_id;
pk_u16be(buf, 0, work->device_id);
work->subid = htobe16(work->device_id);
swap32yes(&buf[ 6], work->midstate , 0x20 / 4);
swap32yes(&buf[0x26], &work->data[0x40], 0xc / 4);
pk_u16le(buf, 50, ntimeroll);
// Use the real share difficulty up to cointerra_max_nonce_diff
if (work->work_difficulty >= cointerra_max_nonce_diff)
work->nonce_diff = cointerra_max_nonce_diff;
else
work->nonce_diff = work->work_difficulty;
zerobits = log2(floor(work->nonce_diff));
work->nonce_diff = pow(2, zerobits);
zerobits += 0x20;
pk_u16le(buf, 52, zerobits);
if (!cointerra_write_msg(devstate->ep, cointerra_drv.dname, CMTO_WORK, buf))
return false;
// HASH_ADD_INT(master_thr->work, device_id, work);
{
++dev->queued_count;
timer_set_now(&work->tv_work_start);
HASH_ADD_INT(dev->queued_work, id, work);
}
++devstate->work_id;
if (!--devstate->requested)
{
applog(LOG_DEBUG, "%s: Sent all requested works, queue full", dev->dev_repr);
cointerra_set_queue_full(dev, true);