Commit 3314ede299d118ecb0fc5e28bcb94c3b0e39841e

Con Kolivas 2014-01-25T13:44:22

Merge branch 'master' of github.com:ckolivas/cgminer

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
diff --git a/ASIC-README b/ASIC-README
index 2f881b5..923a0fb 100644
--- a/ASIC-README
+++ b/ASIC-README
@@ -40,7 +40,7 @@ BlackArrow Bitfury devices
 BlackArrow Bitfury devices need the --enable-bab option when compiling cgminer.
 
 The current BlackArrow Bitfury devices are similar to the Bitfury GPIO mining
-boards and come up as BaB. There are no options available for them.
+boards, with both V1 and V2 controllers, and come up as BaB.
 
 
 BITFURY devices
@@ -129,6 +129,7 @@ ASIC SPECIFIC COMMANDS
 --avalon-freq <arg> Set frequency range for avalon-auto, single value or range
 --avalon-options <arg> Set avalon options baud:miners:asic:timeout:freq:tech
 --avalon-temp <arg> Set avalon target temperature (default: 50)
+--bab-options <arg> Set BaB options max:def:min:up:down:hz:delay:trf
 --bflsc-overheat <arg> Set overheat temperature where BFLSC devices throttle, 0 to disable (default: 90)
 --bitburner-fury-options <arg> Override avalon-options for BitBurner Fury boards baud:miners:asic:timeout:freq
 --bitburner-fury-voltage <arg> Set BitBurner Fury core voltage, in millivolts
@@ -350,6 +351,28 @@ ext recommends a clock of 80 to 230, with a default of 200
 voltage must be one of 650, 750, 850 or 950 with a default of 850
 
 
+BlackArrow Bitfury devices
+
+--bab-options <arg> Set BaB options Max:Def:Min:Up:Down:Hz:Delay:Trf
+
+Any option left blank or starting with 'd' will use the default setting
+If there are not enough options, then the remaining will be left at their
+default value
+
+Max:Def:Min are the chip speed limits to allow, ranging from 52 to 57
+
+Up:Down are the HW error % used to tune the chip speed
+Up means if the HW error % is less than up, over a 5 minute period,
+then increase the chip speed
+Down means if the HW error % is greater than down, over 5 minutes,
+then decrease the chip speed
+
+Hz is the SPI clock speed to use
+
+Delay is the us delay used between bytes for the SPI I/O - default 0
+
+Trf is the us delay used between sends for the SPI I/O - default 0
+
 ---
 
 This code is provided entirely free of charge by the programmer in his spare
diff --git a/README b/README
index a5c955e..8c8fc62 100644
--- a/README
+++ b/README
@@ -218,6 +218,7 @@ ASIC only options:
 --avalon-cutoff <arg> Set avalon overheat cut off temperature (default: 60)
 --avalon-options <arg> Set avalon options baud:miners:asic:timeout:freq:tech
 --avalon-temp <arg> Set avalon target temperature (default: 50)
+--bab-options <arg> Set BaB options max:def:min:up:down:hz:delay:trf
 --bflsc-overheat <arg> Set overheat temperature where BFLSC devices throttle, 0 to disable (default: 90)
 --bitburner-fury-options <arg> Override avalon-options for BitBurner Fury boards baud:miners:asic:timeout:freq
 --bitburner-fury-voltage <arg> Set BitBurner Fury core voltage, in millivolts
diff --git a/cgminer.c b/cgminer.c
index 8707732..2438274 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -174,6 +174,7 @@ char *opt_klondike_options = NULL;
 #ifdef USE_DRILLBIT
 char *opt_drillbit_options = NULL;
 #endif
+char *opt_bab_options = NULL;
 #ifdef USE_USBUTILS
 char *opt_usb_select = NULL;
 int opt_usbdump = -1;
@@ -1070,6 +1071,15 @@ static char *set_drillbit_options(const char *arg)
 }
 #endif
 
+#ifdef USE_BAB
+static char *set_bab_options(const char *arg)
+{
+	opt_set_charp(arg, &opt_bab_options);
+
+	return NULL;
+}
+#endif
+
 #ifdef USE_USBUTILS
 static char *set_usb_select(const char *arg)
 {
@@ -1237,6 +1247,11 @@ static struct opt_table opt_config_table[] = {
                      set_drillbit_options, NULL, NULL,
                      "Set drillbit options <int|ext>:clock[:clock_divider][:voltage]"),
 #endif
+#ifdef USE_BAB
+	OPT_WITH_ARG("--bab-options",
+		     set_bab_options, NULL, NULL,
+		     "Set bab options max:def:min:up:down:hz:delay:trf"),
+#endif
 	OPT_WITHOUT_ARG("--load-balance",
 		     set_loadbalance, &pool_strategy,
 		     "Change multipool strategy from failover to quota based balance"),
@@ -4411,12 +4426,14 @@ void write_config(FILE *fcfg)
 		fprintf(fcfg, ",\n\"icarus-timing\" : \"%s\"", json_escape(opt_icarus_timing));
 #ifdef USE_KLONDIKE
 	if (opt_klondike_options)
-		fprintf(fcfg, ",\n\"klondike-options\" : \"%s\"", json_escape(opt_icarus_options));
+		fprintf(fcfg, ",\n\"klondike-options\" : \"%s\"", json_escape(opt_klondike_options));
 #endif
 #ifdef USE_DRILLBIT
         if (opt_drillbit_options)
                 fprintf(fcfg, ",\n\"drillbit-options\" : \"%s\"", json_escape(opt_drillbit_options));
 #endif
+	if (opt_bab_options)
+		fprintf(fcfg, ",\n\"bab-options\" : \"%s\"", json_escape(opt_bab_options));
 #ifdef USE_USBUTILS
 	if (opt_usb_select)
 		fprintf(fcfg, ",\n\"usb\" : \"%s\"", json_escape(opt_usb_select));
diff --git a/driver-bab.c b/driver-bab.c
index fb7f795..e0b54c6 100644
--- a/driver-bab.c
+++ b/driver-bab.c
@@ -17,6 +17,7 @@
 #include "sha2.h"
 #include "libbitfury.h"
 #include "klist.h"
+#include <ctype.h>
 
 /*
  * Tested on RPi running both Raspbian and Arch
@@ -39,9 +40,13 @@ static void bab_detect(__maybe_unused bool hotplug)
 #define BAB_SPI_BUS 0
 #define BAB_SPI_CHIP 0
 
-#define BAB_SPI_SPEED 96000
+//#define BAB_SPI_SPEED 96000
+#define BAB_SPI_SPEED 1000000
 #define BAB_SPI_BUFSIZ 1024
 
+#define BAB_DELAY_USECS 0
+#define BAB_TRF_DELAY 0
+
 #define BAB_ADDR(_n) (*((babinfo->gpio) + (_n)))
 
 #define BAB_INP_GPIO(_n) BAB_ADDR((_n) / 10) &= (~(7 << (((_n) % 10) * 3)))
@@ -140,9 +145,17 @@ static const uint32_t bab_test_data[BAB_TEST_DATA] = {
  *    56/  101/  125/  99
  */
 #define BAB_MAXSPEED 57
+#define BAB_DEFMAXSPEED 55
 #define BAB_DEFSPEED 54
 #define BAB_MINSPEED 52
 
+/*
+ * % of errors to tune the speed up or down
+ * 1.0 to 10.0 should average around 5.5% errors
+ */
+#define BAB_TUNEUP 1.0
+#define BAB_TUNEDOWN 10.0
+
 #define MIDSTATE_BYTES 32
 #define MERKLE_OFFSET 64
 #define MERKLE_BYTES 12
@@ -243,6 +256,7 @@ struct bab_work_send {
 struct bab_work_reply {
 	uint32_t nonce[BAB_REPLY_NONCES];
 	uint32_t jobsel;
+	uint32_t spichk;
 };
 
 #define BAB_CHIP_MIN (sizeof(struct bab_work_reply)+16)
@@ -312,6 +326,12 @@ static double chip_speed_ranges[BAB_CHIP_SPEEDS - 1] =
 static char *chip_speed_names[BAB_CHIP_SPEEDS] =
 	{ "Dead", "V.Slow", "Slow", "OK", "Good", "Fast" };
 
+/*
+ * This is required to do chip tuning
+ * If disabled, it will simply run the chips at default speed
+ */
+#define UPDATE_HISTORY 1
+
 struct bab_info {
 	struct thr_info spi_thr;
 	struct thr_info res_thr;
@@ -349,9 +369,6 @@ struct bab_info {
 
 	uint8_t osc[BAB_OSC];
 
-	// TODO: performance tuning
-	int fixchip;
-
 	/*
 	 * Ignore errors in the first work reply since
 	 * they may be from a previous run or random junk
@@ -434,6 +451,23 @@ struct bab_info {
 	K_LIST *nfree_list;
 	K_STORE *good_nonces[BAB_MAXCHIPS];
 	K_STORE *bad_nonces[BAB_MAXCHIPS];
+#if UPDATE_HISTORY
+	struct timeval first_work[BAB_MAXCHIPS];
+	uint32_t work_count[BAB_MAXCHIPS];
+	struct timeval last_tune[BAB_MAXCHIPS];
+	uint8_t bad_fast[BAB_MAXCHIPS];
+	bool dead_msg[BAB_MAXCHIPS];
+#endif
+
+	// bab-options (in order)
+	uint8_t max_speed;
+	uint8_t def_speed;
+	uint8_t min_speed;
+	double tune_up;
+	double tune_down;
+	uint32_t speed_hz;
+	uint16_t delay_usecs;
+	uint64_t trf_delay;
 
 	struct timeval last_did;
 
@@ -583,8 +617,8 @@ static bool _bab_txrx(struct cgpu_info *babcgpu, struct bab_info *babinfo, K_ITE
 	siz = (uint32_t)(DATAS(item)->siz);
 
 	memset(&tran, 0, sizeof(tran));
-	tran.delay_usecs = 0;
-	tran.speed_hz = BAB_SPI_SPEED;
+	tran.speed_hz = babinfo->speed_hz;
+	tran.delay_usecs = babinfo->delay_usecs;
 
 	i = 0;
 	pos = 0;
@@ -668,6 +702,9 @@ static bool _bab_txrx(struct cgpu_info *babcgpu, struct bab_info *babinfo, K_ITE
 		wbuf += tran.len;
 		rbuf += tran.len;
 		pos += tran.len;
+
+		if (siz > 0 && babinfo->trf_delay > 0)
+			cgsleep_us(babinfo->trf_delay);
 	}
 	cgtime(&(DATAS(item)->work_start));
 	mutex_lock(&(babinfo->did_lock));
@@ -789,9 +826,7 @@ static void bab_put(struct bab_info *babinfo, K_ITEM *sitem)
 			bank = babinfo->chip_bank[i];
 			BAB_ADD_BREAK(sitem);
 		}
-		if (i == babinfo->fixchip &&
-		    (BAB_CFGD_SET(babinfo->chip_conf[i]) ||
-		     !babinfo->chip_conf[i])) {
+		if (BAB_CFGD_SET(babinfo->chip_conf[i]) || !babinfo->chip_conf[i]) {
 			bab_set_osc(babinfo, i);
 			bab_add_data(sitem, BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
 			bab_config_reg(sitem, BAB_ICLK_REG, BAB_ICLK_BIT(babinfo->chip_conf[i]));
@@ -842,7 +877,7 @@ static void bab_put(struct bab_info *babinfo, K_ITEM *sitem)
 		}
 		DATAS(sitem)->chip_off[i] = DATAS(sitem)->siz + 3;
 		chip_input = &(DATAW(DATAS(sitem)->witems[i])->chip_input);
-		// Would no-ops need to be added if a chip gets disabled?
+
 		if (babinfo->chip_conf[i])
 			bab_add_data(sitem, BAB_INP_ADDR, (uint8_t *)chip_input, sizeof(*chip_input));
 
@@ -860,8 +895,6 @@ static void bab_put(struct bab_info *babinfo, K_ITEM *sitem)
 	K_WUNLOCK(babinfo->spi_list);
 
 	cgsem_post(&(babinfo->spi_work));
-
-	babinfo->fixchip = (babinfo->fixchip + 1) % babinfo->chips;
 }
 
 static bool bab_get(__maybe_unused struct cgpu_info *babcgpu, struct bab_info *babinfo, struct timeval *when)
@@ -1004,6 +1037,9 @@ static bool bab_init_gpio(struct cgpu_info *babcgpu, struct bab_info *babinfo, i
 	int i, err, memfd, data;
 	char buf[64];
 
+	bab_ioc[4].value = (int)(babinfo->speed_hz);
+	bab_ioc[5].value = (int)(babinfo->speed_hz);
+
 	for (i = 0; bab_modules[i]; i++) {
 		snprintf(buf, sizeof(buf), "modprobe %s", bab_modules[i]);
 		err = system(buf);
@@ -1149,6 +1185,148 @@ static void bab_init_chips(struct cgpu_info *babcgpu, struct bab_info *babinfo)
 	memcpy(babinfo->old_fast, babinfo->chip_fast, sizeof(babinfo->old_fast));
 }
 
+static char *bab_options[] = {
+	"MaxSpeed",
+	"DefaultSpeed",
+	"MinSpeed",
+	"TuneUp",
+	"TuneDown",
+	"SPISpeed",
+	"SPIDelayuS",
+	"TransferDelayuS"
+};
+
+#define INVOP " Invalid Option "
+
+static void bab_get_options(struct cgpu_info *babcgpu, struct bab_info *babinfo)
+{
+	char *ptr, *colon;
+	int which, val;
+	double fval;
+	long lval;
+
+	if (opt_bab_options == NULL)
+		return;
+
+	which = 0;
+	ptr = opt_bab_options;
+	while (ptr && *ptr) {
+		colon = strchr(ptr, ':');
+		if (colon)
+			*(colon++) = '\0';
+
+		switch (which) {
+			case 0:
+				if (*ptr && tolower(*ptr) != 'd') {
+					val = atoi(ptr);
+					if (!isdigit(*ptr) || val < BAB_MINSPEED || val > BAB_MAXSPEED) {
+						quit(1, "%s"INVOP"%s '%s' must be %d <= %s <= %d",
+							babcgpu->drv->dname,
+							bab_options[which],
+							ptr, BAB_MINSPEED,
+							bab_options[which],
+							BAB_MAXSPEED);
+					}
+					babinfo->max_speed = (uint8_t)val;
+				}
+				break;
+			case 1:
+				if (*ptr && tolower(*ptr) != 'd') {
+					val = atoi(ptr);
+					if (!isdigit(*ptr) || val < BAB_MINSPEED || val > babinfo->max_speed) {
+						quit(1, "%s"INVOP"%s '%s' must be %d <= %s <= %d",
+							babcgpu->drv->dname,
+							bab_options[which],
+							ptr, BAB_MINSPEED,
+							bab_options[which],
+							babinfo->max_speed);
+					}
+					babinfo->def_speed = (uint8_t)val;
+				}
+				break;
+			case 2:
+				if (*ptr && tolower(*ptr) != 'd') {
+					val = atoi(ptr);
+					if (!isdigit(*ptr) || val < BAB_MINSPEED || val > babinfo->def_speed) {
+						quit(1, "%s"INVOP"%s '%s' must be %d <= %s <= %d",
+							babcgpu->drv->dname,
+							bab_options[which],
+							ptr, BAB_MINSPEED,
+							bab_options[which],
+							babinfo->def_speed);
+					}
+					babinfo->min_speed = (uint8_t)val;
+				}
+				break;
+			case 3:
+				if (*ptr && tolower(*ptr) != 'd') {
+					fval = atof(ptr);
+					if (!isdigit(*ptr) || fval < 0.0 || fval > 100.0) {
+						quit(1, "%s"INVOP"%s '%s' must be 0.0 <= %s <= 100.0",
+							babcgpu->drv->dname,
+							bab_options[which], ptr,
+							bab_options[which]);
+					}
+					babinfo->tune_up = fval;
+				}
+				break;
+			case 4:
+				if (*ptr && tolower(*ptr) != 'd') {
+					fval = atof(ptr);
+					if (!isdigit(*ptr) || fval < 0.0 || fval > 100.0) {
+						quit(1, "%s"INVOP"%s '%s' must be %f <= %s <= 100.0",
+							babcgpu->drv->dname,
+							bab_options[which],
+							ptr, babinfo->tune_up,
+							bab_options[which]);
+					}
+					babinfo->tune_down = fval;
+				}
+				break;
+			case 5:
+				if (*ptr && tolower(*ptr) != 'd') {
+					val = atoi(ptr);
+					if (!isdigit(*ptr) || val < 10000 || val > 10000000) {
+						quit(1, "%s"INVOP"%s '%s' must be 10,000 <= %s <= 10,000,000",
+							babcgpu->drv->dname,
+							bab_options[which], ptr,
+							bab_options[which]);
+					}
+					babinfo->speed_hz = (uint32_t)val;
+				}
+				break;
+			case 6:
+				if (*ptr && tolower(*ptr) != 'd') {
+					val = atoi(ptr);
+					if (!isdigit(*ptr) || val < 0 || val > 65535) {
+						quit(1, "%s"INVOP"%s '%s' must be 0 <= %s <= 65535",
+							babcgpu->drv->dname,
+							bab_options[which], ptr,
+							bab_options[which]);
+					}
+					babinfo->delay_usecs = (uint16_t)val;
+				}
+				break;
+			case 7:
+				if (*ptr && tolower(*ptr) != 'd') {
+					lval = atol(ptr);
+					if (!isdigit(*ptr) || lval < 0) {
+						quit(1, "%s"INVOP"%s '%s' must be %s >= 0",
+							babcgpu->drv->dname,
+							bab_options[which], ptr,
+							bab_options[which]);
+					}
+					babinfo->trf_delay = (uint64_t)lval;
+				}
+				break;
+			default:
+				break;
+		}
+		ptr = colon;
+		which++;
+	}
+}
+
 static void bab_detect(bool hotplug)
 {
 	struct cgpu_info *babcgpu = NULL;
@@ -1171,9 +1349,25 @@ static void bab_detect(bool hotplug)
 		quithere(1, "Failed to calloc babinfo");
 	babcgpu->device_data = (void *)babinfo;
 
+	babinfo->max_speed = BAB_DEFMAXSPEED;
+	babinfo->def_speed = BAB_DEFSPEED;
+	babinfo->min_speed = BAB_MINSPEED;
+
+	babinfo->tune_up = BAB_TUNEUP;
+	babinfo->tune_down = BAB_TUNEDOWN;
+
+	babinfo->speed_hz = BAB_SPI_SPEED;
+	babinfo->delay_usecs = BAB_DELAY_USECS;
+	babinfo->trf_delay = BAB_TRF_DELAY;
+
+	bab_get_options(babcgpu, babinfo);
+
 	for (i = 0; i < BAB_MAXCHIPS; i++) {
 		babinfo->chip_conf[i] = BAB_DEFCONF;
-		babinfo->chip_fast[i] = BAB_DEFSPEED;
+		babinfo->chip_fast[i] = babinfo->def_speed;
+#if UPDATE_HISTORY
+		babinfo->bad_fast[i] = babinfo->max_speed + 1;
+#endif
 	}
 
 	if (!bab_init_gpio(babcgpu, babinfo, BAB_SPI_BUS, BAB_SPI_CHIP))
@@ -1318,6 +1512,11 @@ static void *bab_spi(void *userdata)
 				memcpy(&(DATAW(witem)->work_start), &(DATAS(sitem)->work_start),
 					sizeof(DATAW(witem)->work_start));
 				k_add_head(babinfo->chip_work[chip], witem);
+#if UPDATE_HISTORY
+				babinfo->work_count[chip]++;
+				if (babinfo->first_work[chip].tv_sec == 0)
+					memcpy(&(babinfo->first_work[chip]), &send, sizeof(send));
+#endif
 			}
 		}
 		K_WUNLOCK(babinfo->wfree_list);
@@ -1384,13 +1583,14 @@ static void bab_flush_work(struct cgpu_info *babcgpu)
 #define WORK_BITS (18*4)
 #define WORK_NONCE (19*4)
 
-#define UPDATE_HISTORY 1
-
 #if UPDATE_HISTORY
-static void process_history(struct bab_info *babinfo, int chip, struct timeval *when, bool good, struct timeval *now)
+static void process_history(struct cgpu_info *babcgpu, int chip, struct timeval *when, bool good, struct timeval *now)
 {
+	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
+	uint64_t good_nonces, bad_nonces;
+	uint8_t chip_fast;
+	double tune;
 	K_ITEM *item;
-	double diff;
 	int i;
 
 	K_WLOCK(babinfo->nfree_list);
@@ -1405,8 +1605,7 @@ static void process_history(struct bab_info *babinfo, int chip, struct timeval *
 	for (i = 0; i < babinfo->chips; i++) {
 		item = babinfo->good_nonces[i]->tail;
 		while (item) {
-			diff = tdiff(now, &(DATAN(item)->found));
-			if (diff < HISTORY_TIME_S)
+			if (tdiff(now, &(DATAN(item)->found)) < HISTORY_TIME_S)
 				break;
 
 			k_unlink_item(babinfo->good_nonces[i], item);
@@ -1417,8 +1616,7 @@ static void process_history(struct bab_info *babinfo, int chip, struct timeval *
 
 		item = babinfo->bad_nonces[i]->tail;
 		while (item) {
-			diff = tdiff(now, &(DATAN(item)->found));
-			if (diff < HISTORY_TIME_S)
+			if (tdiff(now, &(DATAN(item)->found)) < HISTORY_TIME_S)
 				break;
 
 			k_unlink_item(babinfo->bad_nonces[i], item);
@@ -1428,6 +1626,99 @@ static void process_history(struct bab_info *babinfo, int chip, struct timeval *
 		}
 	}
 	K_WUNLOCK(babinfo->nfree_list);
+
+	// Tuning ...
+	good_nonces = babinfo->good_nonces[chip]->count;
+	bad_nonces = babinfo->bad_nonces[chip]->count;
+
+	if (tdiff(now, &(babinfo->first_work[chip])) >= HISTORY_TIME_S &&
+	    tdiff(now, &(babinfo->last_tune[chip])) >= HISTORY_TIME_S &&
+	    (good_nonces + bad_nonces) > 0) {
+
+		chip_fast = babinfo->chip_fast[chip];
+
+		/*
+		 * If dead then step it down and remember the speed
+		 * TODO: does a speed change reset the chip? Or is there a reset?
+		 */
+		if (good_nonces == 0) {
+			if (chip_fast > babinfo->min_speed) {
+				if (babinfo->bad_fast[chip] > chip_fast)
+					babinfo->bad_fast[chip] = chip_fast;
+
+				babinfo->chip_fast[chip]--;
+
+				applog(LOG_WARNING, "%s%d: Chip %d DEAD - speed down from %d to %d",
+						    babcgpu->drv->name, babcgpu->device_id,
+						    chip, (int)chip_fast, (int)chip_fast - 1);
+			} else {
+				/*
+				 * Permanently DEAD since we're already at the minumum speed
+				 * but only getting bad nonces
+				 */
+				if (babinfo->dead_msg[chip] == false) {
+					applog(LOG_WARNING, "%s%d: Chip %d DEAD - at min speed %d",
+							    babcgpu->drv->name, babcgpu->device_id,
+							    chip, (int)chip_fast);
+
+					babinfo->dead_msg[chip] = true;
+				}
+			}
+			goto tune_over;
+		}
+
+		/*
+		 * It 'was' permanently DEAD but a good nonce came back!
+		 */
+		if (babinfo->dead_msg[chip]) {
+			applog(LOG_WARNING, "%s%d: Chip %d REVIVED - at speed %d",
+					    babcgpu->drv->name, babcgpu->device_id,
+					    chip, (int)chip_fast);
+
+			babinfo->dead_msg[chip] = false;
+		}
+
+		/*
+		 * Since we have found 'some' nonces -
+		 * make sure bad_fast is higher than current chip_fast
+		 */
+		if (babinfo->bad_fast[chip] <= chip_fast)
+			babinfo->bad_fast[chip] = chip_fast + 1;
+
+		tune = (double)bad_nonces / (double)(good_nonces + bad_nonces) * 100.0;
+
+		// Tune it down if error rate is too high
+		if (tune >= babinfo->tune_down) {
+			babinfo->chip_fast[chip]--;
+
+			applog(LOG_WARNING, "%s%d: Chip %d High errors %.2f%% - speed down %d to %d",
+					    babcgpu->drv->name, babcgpu->device_id,
+					    chip, tune, (int)chip_fast, (int)chip_fast - 1);
+
+			goto tune_over;
+		}
+
+		/*
+		 * TODO: if we are at bad_fast-1 and tune_up
+		 * and bad_fast was set more than some time limit ago
+		 * then consider increasing bad_fast by 1?
+		 */
+
+		// Tune it up if error rate is low enough
+		if (tune <= babinfo->tune_up &&
+		    chip_fast < babinfo->max_speed &&
+		    chip_fast < (babinfo->bad_fast[chip] - 1)) {
+			babinfo->chip_fast[chip]++;
+
+			applog(LOG_WARNING, "%s%d: Chip %d Low errors %.2f%% - speed up %d to %d",
+					    babcgpu->drv->name, babcgpu->device_id,
+					    chip, tune, (int)chip_fast, (int)chip_fast + 1);
+
+			goto tune_over;
+		}
+tune_over:
+		cgtime(&(babinfo->last_tune[chip]));
+	}
 }
 #endif
 
@@ -1527,7 +1818,7 @@ static K_ITEM *process_nonce(struct thr_info *thr, struct cgpu_info *babcgpu, K_
 
 						babinfo->chip_cont_bad[chip] = 0;
 #if UPDATE_HISTORY
-						process_history(babinfo, chip,
+						process_history(babcgpu, chip,
 								&(DATAR(ritem)->when),
 								true, &now);
 #endif
@@ -1563,7 +1854,7 @@ static K_ITEM *process_nonce(struct thr_info *thr, struct cgpu_info *babcgpu, K_
 			babinfo->chip_max_bad[chip] = babinfo->chip_cont_bad[chip];
 
 #if UPDATE_HISTORY
-		process_history(babinfo, chip, &(DATAR(ritem)->when), false, &now);
+		process_history(babcgpu, chip, &(DATAR(ritem)->when), false, &now);
 #endif
 	} else {
 		babinfo->initial_ignored++;
@@ -1656,8 +1947,8 @@ static bool bab_do_work(struct cgpu_info *babcgpu)
 	K_ITEM *witem, *sitem, *ritem;
 	struct timeval when, now;
 	double delay;
-	int chip, rep, j, nonces;
-	uint32_t nonce;
+	int chip, rep, j, nonces, spie = 0, miso = 0;
+	uint32_t nonce, spichk;
 	bool res;
 
 	cgtime(&now);
@@ -1739,6 +2030,12 @@ static bool bab_do_work(struct cgpu_info *babcgpu)
 		DATAR(ritem)->not_first_reply = babinfo->not_first_reply[chip];
 		memcpy(&(DATAR(ritem)->when), &when, sizeof(when));
 
+		spichk = babinfo->chip_results[chip].spichk;
+		if (spichk != 0 && spichk != 0xffffffff) {
+			babinfo->chip_spie[chip]++;
+			spie++;
+		}
+
 		nonces = 0;
 		for (rep = 0; rep < BAB_REPLY_NONCES; rep++) {
 			nonce = babinfo->chip_results[chip].nonce[rep];
@@ -1750,6 +2047,12 @@ static bool bab_do_work(struct cgpu_info *babcgpu)
 			}
 		}
 
+		if (nonces == BAB_REPLY_NONCES) {
+			babinfo->chip_miso[chip]++;
+			miso++;
+			// Test the results anyway
+		}
+
 		/*
 		 * Send even with zero nonces
 		 * so cleanup_older() is called for the chip
@@ -1769,7 +2072,7 @@ static bool bab_do_work(struct cgpu_info *babcgpu)
 
 	}
 
-	applog(LOG_DEBUG, "Work: items:%d", work_items);
+	applog(LOG_DEBUG, "Work: items:%d spie:%d miso:%d", work_items, spie, miso);
 
 	return true;
 }
@@ -1883,6 +2186,7 @@ static int64_t bab_scanwork(__maybe_unused struct thr_info *thr)
 }
 
 #define CHIPS_PER_STAT 16
+#define FMT_RANGE "%d-%d"
 
 static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 {
@@ -1981,7 +2285,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 					babinfo->chip_nonces[j]);
 			strcat(data, buf);
 		}
-		snprintf(buf, sizeof(buf), "Nonces %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "Nonces "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
 		data[0] = '\0';
@@ -1992,7 +2296,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 					babinfo->chip_good[j]);
 			strcat(data, buf);
 		}
-		snprintf(buf, sizeof(buf), "Good %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "Good "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
 		data[0] = '\0';
@@ -2003,7 +2307,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 					babinfo->chip_bad[j]);
 			strcat(data, buf);
 		}
-		snprintf(buf, sizeof(buf), "Bad %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "Bad "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
 		data[0] = '\0';
@@ -2014,7 +2318,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 					(int)(babinfo->chip_conf[j]));
 			strcat(data, buf);
 		}
-		snprintf(buf, sizeof(buf), "Conf %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "Conf "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
 		data[0] = '\0';
@@ -2025,7 +2329,29 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 					(int)(babinfo->chip_fast[j]));
 			strcat(data, buf);
 		}
-		snprintf(buf, sizeof(buf), "Fast %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "Fast "FMT_RANGE, i, to);
+		root = api_add_string(root, buf, data, true);
+
+		data[0] = '\0';
+		for (j = i; j <= to; j++) {
+			snprintf(buf, sizeof(buf),
+					"%s%d",
+					j == i ? "" : " ",
+					(int)(babinfo->chip_spie[j]));
+			strcat(data, buf);
+		}
+		snprintf(buf, sizeof(buf), "Spie "FMT_RANGE, i, to);
+		root = api_add_string(root, buf, data, true);
+
+		data[0] = '\0';
+		for (j = i; j <= to; j++) {
+			snprintf(buf, sizeof(buf),
+					"%s%d",
+					j == i ? "" : " ",
+					(int)(babinfo->chip_miso[j]));
+			strcat(data, buf);
+		}
+		snprintf(buf, sizeof(buf), "Miso "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
 		data[0] = '\0';
@@ -2040,7 +2366,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 					j == i ? "" : " ", hw);
 			strcat(data, buf);
 		}
-		snprintf(buf, sizeof(buf), "HW%% %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "HW%% "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
 		ghs_sum = 0;
@@ -2058,10 +2384,10 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 			strcat(data, buf);
 			ghs_sum += (float)ghs;
 		}
-		snprintf(buf, sizeof(buf), "GHs %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "GHs "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
-		snprintf(buf, sizeof(buf), "Sum GHs %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "Sum GHs "FMT_RANGE, i, to);
 		root = api_add_avg(root, buf, &ghs_sum, true);
 
 		data[0] = '\0';
@@ -2072,7 +2398,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 					babinfo->chip_cont_bad[j]);
 			strcat(data, buf);
 		}
-		snprintf(buf, sizeof(buf), "Cont-Bad %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "Cont-Bad "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
 		data[0] = '\0';
@@ -2083,7 +2409,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 					babinfo->chip_max_bad[j]);
 			strcat(data, buf);
 		}
-		snprintf(buf, sizeof(buf), "Max-Bad %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "Max-Bad "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
 		data[0] = '\0';
@@ -2094,7 +2420,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 					history_good[j]);
 			strcat(data, buf);
 		}
-		snprintf(buf, sizeof(buf), "History Good %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "History Good "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
 		data[0] = '\0';
@@ -2105,7 +2431,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 					history_bad[j]);
 			strcat(data, buf);
 		}
-		snprintf(buf, sizeof(buf), "History Bad %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "History Bad "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
 		data[0] = '\0';
@@ -2120,7 +2446,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 					j == i ? "" : " ", hw);
 			strcat(data, buf);
 		}
-		snprintf(buf, sizeof(buf), "History HW%% %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "History HW%% "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
 		ghs_sum = 0;
@@ -2153,10 +2479,10 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 			if (sp >= (BAB_CHIP_SPEEDS - 1))
 				speeds[BAB_CHIP_SPEEDS - 1]++;
 		}
-		snprintf(buf, sizeof(buf), "History GHs %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "History GHs "FMT_RANGE, i, to);
 		root = api_add_string(root, buf, data, true);
 
-		snprintf(buf, sizeof(buf), "Sum History GHs %d - %d", i, to);
+		snprintf(buf, sizeof(buf), "Sum History GHs "FMT_RANGE, i, to);
 		root = api_add_avg(root, buf, &ghs_sum, true);
 
 		ghs_tot += ghs_sum;
@@ -2326,6 +2652,20 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 	root = api_add_double(root, "Send Min", &(babinfo->send_min), true);
 	root = api_add_double(root, "Send Max", &(babinfo->send_max), true);
 
+	i = (int)(babinfo->max_speed);
+	root = api_add_int(root, bab_options[0], &i, true);
+	i = (int)(babinfo->def_speed);
+	root = api_add_int(root, bab_options[1], &i, true);
+	i = (int)(babinfo->min_speed);
+	root = api_add_int(root, bab_options[2], &i, true);
+	root = api_add_double(root, bab_options[3], &(babinfo->tune_up), true);
+	root = api_add_double(root, bab_options[4], &(babinfo->tune_down), true);
+	i = (int)(babinfo->speed_hz);
+	root = api_add_int(root, bab_options[5], &i, true);
+	i = (int)(babinfo->delay_usecs);
+	root = api_add_int(root, bab_options[6], &i, true);
+	root = api_add_uint64(root, bab_options[7], &(babinfo->trf_delay), true);
+
 	return root;
 }
 
diff --git a/miner.h b/miner.h
index 8d5d647..a4717a6 100644
--- a/miner.h
+++ b/miner.h
@@ -960,6 +960,9 @@ extern char *opt_klondike_options;
 #ifdef USE_DRILLBIT
 extern char *opt_drillbit_options;
 #endif
+#ifdef USE_BAB
+extern char *opt_bab_options;
+#endif
 #ifdef USE_USBUTILS
 extern char *opt_usb_select;
 extern int opt_usbdump;