Commit a60f7355dd063351dc651501422c079d1291ea34

kanoi 2014-03-15T00:37:24

Merge pull request #563 from kanoi/master New Drillbit driver + Icarus ANU allow timing to work

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
diff --git a/ASIC-README b/ASIC-README
index 1619c44..dafbfb1 100644
--- a/ASIC-README
+++ b/ASIC-README
@@ -453,17 +453,37 @@ is still within USB2 spec. This value allows you to alter the clockspeed, with
 ~54 being the optimal but requiring a higher power or USB3 port.
 
 
-BITFURY Devices
+Drillbit Systems Devices
 
 --drillbit-options <arg> Set drillbit options <int|ext>:clock[:clock_divider][:voltage]
 
-int/ext defines the clock source - default int
-clock_divider must be 1 or 2 with a default of 1
-int allows a clock of 0 to 63 with recommended 48-58 for div 2 and 30-48 for div 1
- with a default of 40 with div 1
-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
+* int/ext defines the clock source - default int. Not all boards support ext.
+* clock_divider must be 1 or 2 with a default of 1. Bitfury only,
+  ignored on Avalon.
+* clock is in MHz, on Drillbit range 80-250 with a default of 200,
+  recommended maximum 230. On Avalon range 500-1000 with a
+  recommended maximum of 800.
+* voltage is ASIC core voltage in millivolts, available values vary per board but
+  default is 850 and the recommended maximum is 950 (Bitfury) and 1000 (Avalon.)
+
+--drillbit-auto <every>:[<gooderr>:<baderr>:<maxerr>]
+
+If supported by firmware and device, this feature allows cgminer to
+automatically tweak each ASIC's clock rate up and down in to achieve
+optimal performance.
+
+* every - only required param, check each ASIC after each block of
+  this many work units. Recommended value 100.
+* gooderr - the "Good" threshold is when less hardware errors than
+  this per "every" work units, the clock rate will be increased.
+  Default value 1.
+* baderr - the "Bad" threshold is when more hardware errors than
+  this per "every" work units, the clock rate will be decreased.
+  Default value 3.
+* maxerr - the "Max" threshold is when more hardware errors than
+  this per "every" work units (including pre-empting before
+  "every" work units is up), the clock rate will be decreased and
+  will not be increased again past this point. Default value 10.
 
 
 BlackArrow Bitfury devices
diff --git a/cgminer.c b/cgminer.c
index 9be9e86..145d511 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -209,6 +209,7 @@ char *opt_klondike_options = NULL;
 #endif
 #ifdef USE_DRILLBIT
 char *opt_drillbit_options = NULL;
+char *opt_drillbit_auto = NULL;
 #endif
 char *opt_bab_options = NULL;
 #ifdef USE_BITMINE_A1
@@ -1140,6 +1141,14 @@ static char *set_drillbit_options(const char *arg)
 
 	return NULL;
 }
+
+static char *set_drillbit_auto(const char *arg)
+{
+	opt_set_charp(arg, &opt_drillbit_auto);
+
+	return NULL;
+}
+
 #endif
 
 #ifdef USE_BAB
@@ -1341,8 +1350,11 @@ static struct opt_table opt_config_table[] = {
 			"Automatically disable pools that continually reject shares"),
 #ifdef USE_DRILLBIT
         OPT_WITH_ARG("--drillbit-options",
-                     set_drillbit_options, NULL, NULL,
-                     "Set drillbit options <int|ext>:clock[:clock_divider][:voltage]"),
+		set_drillbit_options, NULL, NULL,
+		"Set drillbit options <int|ext>:clock[:clock_divider][:voltage]"),
+        OPT_WITH_ARG("--drillbit-auto",
+		set_drillbit_auto, NULL, NULL,
+		"Enable drillbit automatic tuning <every>:[<gooderr>:<baderr>:<maxerr>]"),
 #endif
 	OPT_WITH_ARG("--expiry|-E",
 		     set_int_0_to_9999, opt_show_intval, &opt_expiry,
@@ -4798,6 +4810,8 @@ void write_config(FILE *fcfg)
 #ifdef USE_DRILLBIT
         if (opt_drillbit_options)
                 fprintf(fcfg, ",\n\"drillbit-options\" : \"%s\"", json_escape(opt_drillbit_options));
+	if (opt_drillbit_auto)
+                fprintf(fcfg, ",\n\"drillbit-auto\" : \"%s\"", json_escape(opt_drillbit_auto));
 #endif
 	if (opt_bab_options)
 		fprintf(fcfg, ",\n\"bab-options\" : \"%s\"", json_escape(opt_bab_options));
diff --git a/driver-drillbit.c b/driver-drillbit.c
index 08bc997..a838e07 100644
--- a/driver-drillbit.c
+++ b/driver-drillbit.c
@@ -16,6 +16,7 @@
 #include "sha2.h"
 
 #define TIMEOUT 3000
+#define RESULT_TIMEOUT 5000
 #define MAX_RESULTS 16 // max results from a single chip
 
 #define drvlog(prio, fmt, ...) do { \
@@ -52,26 +53,44 @@ typedef struct {
 #define SZ_SERIALISED_WORKRESULT (4+4*MAX_RESULTS)
 static void deserialise_work_result(WorkResult *work_result, const char *buf);
 
+/* V4 config is the preferred one, used internally, non-ASIC-specific */
+typedef struct {
+	uint16_t core_voltage; // Millivolts
+	uint16_t clock_freq; // Clock frequency in MHz (or clock level 30-48 for Bitfury internal clock level)
+	uint8_t clock_div2;	 // Apply the /2 clock divider (both internal and external), where available
+	uint8_t use_ext_clock; // Flag. Ignored on boards without external clocks
+} BoardConfig;
+
+typedef struct
+{
+	uint16_t chip_id;
+	uint8_t increase_clock;
+} AutoTuneRequest;
+
+#define SZ_SERIALISED_AUTOTUNEREQUEST 3
+static void serialise_autotune_request(char *buf, AutoTuneRequest *aq);
+
 #define CONFIG_PW1 (1<<0)
 #define CONFIG_PW2 (1<<1)
 
-// Possible core voltage settings on PW1 & PW2
+// Possible core voltage settings on PW1 & PW2, used by legacy V3 config only
 #define CONFIG_CORE_065V 0
 #define CONFIG_CORE_075V CONFIG_PW2
 #define CONFIG_CORE_085V CONFIG_PW1
 #define CONFIG_CORE_095V (CONFIG_PW1|CONFIG_PW2)
 
+/* V3 config is for backwards compatibility with older firmwares */
 typedef struct {
 	uint8_t core_voltage; // Set to flags defined above
 	uint8_t int_clock_level; // Clock level (30-48 without divider), see asic.c for details
-	uint8_t clock_div2;	 // Apply the /2 clock divider (both internal and external)
+	uint8_t clock_div2;	// Apply the /2 clock divider (both internal and external)
 	uint8_t use_ext_clock; // Ignored on boards without external clocks
 	uint16_t ext_clock_freq;
-	uint16_t core_voltage_mv; // set to a plain human-readable integer value (not serialised atm)
-} BoardConfig;
+ } BoardConfigV3;
 
 #define SZ_SERIALISED_BOARDCONFIG 6
-static void serialise_board_config(char *buf, const BoardConfig *boardconfig);
+static void serialise_board_configV4(char *buf, BoardConfig *boardconfig);
+static void serialise_board_configV3(char *buf, BoardConfigV3 *boardconfig);
 
 typedef struct {
 	uint8_t protocol_version;
@@ -82,8 +101,10 @@ typedef struct {
 } Identity;
 
 /* Capabilities flags known to cgminer */
-#define CAP_TEMP 1
-#define CAP_EXT_CLOCK 2
+#define CAP_TEMP (1<<0)
+#define CAP_EXT_CLOCK (1<<1)
+#define CAP_IS_AVALON (1<<2)
+#define CAP_LIMITER_REMOVED (1<<3)
 
 #define SZ_SERIALISED_IDENTITY 16
 static void deserialise_identity(Identity *identity, const char *buf);
@@ -95,24 +116,20 @@ typedef struct {
 	UT_hash_handle hh;
 } config_setting;
 
-/* Comparatively modest default settings */
-static config_setting default_settings = {
-	key: { 0 },
-	config: {
-		core_voltage: CONFIG_CORE_085V,
-		core_voltage_mv: 850,
-		use_ext_clock: 0,
-		int_clock_level: 40,
-		clock_div2: 0,
-		ext_clock_freq: 200
-	},
-};
-
 static config_setting *settings;
 
+static void drillbit_empty_buffer(struct cgpu_info *drillbit);
+
+/* Automatic tuning parameters */
+static uint32_t auto_every = 100;
+static uint32_t auto_good = 1;
+static uint32_t auto_bad = 3;
+static uint32_t auto_max = 10;
+
 /* Return a pointer to the chip_info structure for a given chip id, or NULL otherwise */
 static struct drillbit_chip_info *find_chip(struct drillbit_info *info, uint16_t chip_id) {
 	int i;
+
 	for (i = 0; i < info->num_chips; i++) {
 		if (info->chips[i].chip_id == chip_id)
 			return &info->chips[i];
@@ -144,6 +161,7 @@ static bool usb_read_fixed_size(struct cgpu_info *drillbit, void *result, size_t
 	}
 	drvlog(LOG_ERR, "Read incomplete fixed size packet - got %d bytes / %d (timeout %d)",
 			(int)count, (int)result_size, timeout);
+	drillbit_empty_buffer(drillbit);
 	return false;
 }
 
@@ -154,6 +172,7 @@ static bool usb_read_simple_response(struct cgpu_info *drillbit, char command, e
 */
 static bool usb_send_simple_command(struct cgpu_info *drillbit, char command, enum usb_cmds command_name) {
 	int amount;
+
 	usb_write_timeout(drillbit, &command, 1, &amount, TIMEOUT, C_BF_REQWORK);
 	if (amount != 1) {
 		drvlog(LOG_ERR, "Failed to write command %c", command);
@@ -246,7 +265,7 @@ static bool drillbit_getinfo(struct cgpu_info *drillbit, struct drillbit_info *i
 	}
 
 	const int MIN_VERSION = 2;
-	const int MAX_VERSION = 3;
+	const int MAX_VERSION = 4;
 	if (identity.protocol_version < MIN_VERSION) {
 		drvlog(LOG_ERR, "Unknown device protocol version %d.", identity.protocol_version);
 		return false;
@@ -262,7 +281,7 @@ static bool drillbit_getinfo(struct cgpu_info *drillbit, struct drillbit_info *i
 	}
 
 	// load identity data into device info structure
-	info->version = identity.protocol_version;
+	info->protocol_version = identity.protocol_version;
 	if (strncmp(identity.product, "DRILLBIT", sizeof(identity.product)) == 0) {
 		// Hack: first production firmwares all described themselves as DRILLBIT, so fill in the gaps
 		if (identity.num_chips == 1)
@@ -277,7 +296,7 @@ static bool drillbit_getinfo(struct cgpu_info *drillbit, struct drillbit_info *i
 	info->capabilities = identity.capabilities;
 
 	drvlog(LOG_INFO, "Getinfo returned version %d, product %s serial %08x num_chips %d",
-	       info->version, info->product, info->serial, info->num_chips);
+	       info->protocol_version, info->product, info->serial, info->num_chips);
 
 	drillbit_empty_buffer(drillbit);
 	return true;
@@ -313,11 +332,17 @@ static config_setting *find_settings(struct cgpu_info *drillbit)
 	config_setting *setting;
 	char search_key[9];
 
-	// Search by serial (8 character hex string)
+	if (!settings) {
+		drvlog(LOG_INFO, "Keeping onboard defaults for device %s (serial %08x)",
+			info->product, info->serial);
+		return NULL;
+	}
+
+	// Search by serial
 	sprintf(search_key, "%08x", info->serial);
 	HASH_FIND_STR(settings, search_key, setting);
 	if (setting) {
-		drvlog(LOG_INFO, "Using unit-specific settings for serial %s", search_key);
+		drvlog(LOG_INFO, "Using serial specific settings for serial %s", search_key);
 		return setting;
 	}
 
@@ -344,45 +369,66 @@ static config_setting *find_settings(struct cgpu_info *drillbit)
 		return setting;
 	}
 
-	// Failing that, return default/generic config (null key)
+	// Check for a generic/catchall drillbit-options argument (key set to NULL)
 	search_key[0] = 0;
 	HASH_FIND_STR(settings, search_key, setting);
-	drvlog(LOG_INFO, "Using non-specific settings for device %s (serial %08x)", info->product,
-	       info->serial);
-	return setting;
+	if (setting) {
+		drvlog(LOG_INFO, "Using non-specific settings for device %s (serial %08x)", info->product,
+			info->serial);
+		return setting;
+	}
+
+	drvlog(LOG_WARNING, "Keeping onboard defaults for device %s (serial %08x)",
+		info->product, info->serial);
+	return NULL;
 }
 
 static void drillbit_send_config(struct cgpu_info *drillbit)
 {
 	struct drillbit_info *info = drillbit->device_data;
-	char cmd;
 	int amount;
-	char buf[SZ_SERIALISED_BOARDCONFIG];
+	char buf[SZ_SERIALISED_BOARDCONFIG+1];
 	config_setting *setting;
+	BoardConfigV3 v3_config;
 
 	// Find the relevant board config
 	setting = find_settings(drillbit);
+	if (!setting)
+		return; // Don't update board config from defaults
 	drvlog(LOG_NOTICE, "Config: %s:%d:%d:%d Serial: %08x",
 	       setting->config.use_ext_clock ? "ext" : "int",
-	       setting->config.use_ext_clock ? setting->config.ext_clock_freq : setting->config.int_clock_level,
+	       setting->config.clock_freq,
 	       setting->config.clock_div2 ? 2 : 1,
-	       setting->config.core_voltage_mv,
+	       setting->config.core_voltage,
 	       info->serial);
 
-	drvlog(LOG_INFO, "Sending board configuration voltage=%d use_ext_clock=%d int_clock_level=%d clock_div2=%d ext_clock_freq=%d",
-	       setting->config.core_voltage, setting->config.use_ext_clock,
-	       setting->config.int_clock_level,
-	       setting->config.clock_div2, setting->config.ext_clock_freq);
-
 	if (setting->config.use_ext_clock && !(info->capabilities & CAP_EXT_CLOCK)) {
 		drvlog(LOG_WARNING, "Chosen configuration specifies external clock but this device (serial %08x) has no external clock!", info->serial);
 	}
 
-	cmd = 'C';
-	usb_write_timeout(drillbit, &cmd, 1, &amount, TIMEOUT, C_BF_REQWORK);
-
-	serialise_board_config(buf, &setting->config);
-	usb_write_timeout(drillbit, buf, SZ_SERIALISED_BOARDCONFIG, &amount, TIMEOUT, C_BF_CONFIG);
+	if (info->protocol_version <= 3) {
+		/* Make up a backwards compatible V3 config structure to send to the miner */
+		if (setting->config.core_voltage >= 950)
+			v3_config.core_voltage = CONFIG_CORE_095V;
+		else if (setting->config.core_voltage >= 850)
+			v3_config.core_voltage = CONFIG_CORE_085V;
+		else if (setting->config.core_voltage >= 750)
+			v3_config.core_voltage = CONFIG_CORE_075V;
+		else
+			v3_config.core_voltage = CONFIG_CORE_065V;
+		if (setting->config.clock_freq > 64)
+			v3_config.int_clock_level = setting->config.clock_freq / 5;
+		else
+			v3_config.int_clock_level = setting->config.clock_freq;
+		v3_config.clock_div2 = setting->config.clock_div2;
+		v3_config.use_ext_clock = setting->config.use_ext_clock;
+		v3_config.ext_clock_freq = setting->config.clock_freq;
+		serialise_board_configV3(&buf[1], &v3_config);
+	} else {
+		serialise_board_configV4(&buf[1], &setting->config);
+	}
+	buf[0] = 'C';
+	usb_write_timeout(drillbit, buf, sizeof(buf), &amount, TIMEOUT, C_BF_CONFIG);
 
 	/* Expect a single 'C' byte as acknowledgement */
 	usb_read_simple_response(drillbit, 'C', C_BF_CONFIG); // TODO: verify response
@@ -437,15 +483,12 @@ static void drillbit_get_statline_before(char *buf, size_t bufsiz, struct cgpu_i
 }
 
 
-static bool drillbit_parse_options(struct cgpu_info *drillbit)
+static bool drillbit_parse_options(__maybe_unused struct cgpu_info *drillbit)
 {
 	/* Read configuration options (currently global not per-ASIC or per-board) */
 	if (settings != NULL)
 		return true; // Already initialised
 
-	// Start with the system-wide defaults
-	HASH_ADD_STR(settings, key, (&default_settings));
-
 	char *next_opt = opt_drillbit_options;
 	while (next_opt && strlen(next_opt)) {
 		BoardConfig parsed_config;
@@ -473,42 +516,13 @@ static bool drillbit_parse_options(struct cgpu_info *drillbit)
 
 		if (!strcmp("int",clksrc)) {
 			parsed_config.use_ext_clock = 0;
-			if (freq < 0 || freq > 63) {
-				quithere(1, "Invalid internal oscillator level %d. Recommended range is %s for this clock divider (possible is 0-63)", freq, parsed_config.clock_div2 ? "48-57":"30-48");
-			}
-			if (parsed_config.clock_div2 && (freq < 48 || freq > 57)) {
-				drvlog(LOG_WARNING, "Internal oscillator level %d outside recommended range 48-57.", freq);
-			}
-			if (!parsed_config.clock_div2 && (freq < 30 || freq > 48)) {
-				drvlog(LOG_WARNING, "Internal oscillator level %d outside recommended range 30-48.", freq);
-			}
-			parsed_config.int_clock_level = freq;
-		} else if (!strcmp("ext", clksrc)) {
+		}
+		else if (!strcmp("ext", clksrc)) {
 			parsed_config.use_ext_clock = 1;
-			parsed_config.ext_clock_freq = freq;
-			if (freq < 80 || freq > 230) {
-				drvlog(LOG_WARNING, "Warning: recommended external clock frequencies are 80-230MHz. Value %d may produce unexpected results.", freq);
-			}
 		} else
 			quithere(1, "Invalid clock source. Valid choices are int, ext.");
 
-		parsed_config.core_voltage_mv = voltage;
-		switch(voltage) {
-		case 650:
-			voltage = CONFIG_CORE_065V;
-			break;
-		case 750:
-			voltage = CONFIG_CORE_075V;
-			break;
-		case 850:
-			voltage = CONFIG_CORE_085V;
-			break;
-		case 950:
-			voltage = CONFIG_CORE_095V;
-			break;
-		default:
-			quithere(1, "Invalid core voltage %d. Valid values 650,750,850,950mV)", voltage);
-		}
+		parsed_config.clock_freq = freq;
 		parsed_config.core_voltage = voltage;
 
 		// Add the new set of settings to the configuration choices hash table
@@ -523,6 +537,18 @@ static bool drillbit_parse_options(struct cgpu_info *drillbit)
 		if (next_opt)
 			next_opt++;
 	}
+
+	if (opt_drillbit_auto) {
+		sscanf(opt_drillbit_auto, "%d:%d:%d:%d",
+			&auto_every, &auto_good, &auto_bad, &auto_max);
+		if (auto_max < auto_bad) {
+			quithere(1, "Bad drillbit-auto: MAX limit must be greater than BAD limit");
+		}
+		if (auto_bad < auto_good) {
+			quithere(1, "Bad drillbit-auto: GOOD limit must be greater than BAD limit");
+		}
+	}
+
 	return true;
 }
 
@@ -559,6 +585,7 @@ static struct cgpu_info *drillbit_detect_one(struct libusb_device *dev, struct u
 	info->chips = calloc(sizeof(struct drillbit_chip_info), info->num_chips);
 	for (i = 0; i < info->num_chips; i++) {
 		info->chips[i].chip_id = i;
+		info->chips[i].auto_max = 999;
 	}
 
 	/* Send reset request */
@@ -575,6 +602,10 @@ static struct cgpu_info *drillbit_detect_one(struct libusb_device *dev, struct u
 
 	update_usb_stats(drillbit);
 
+	if (info->capabilities & CAP_LIMITER_REMOVED) {
+		drvlog(LOG_WARNING, "Recommended limits have been disabled on this board, take care when changing settings.");
+	}
+
 	drillbit_send_config(drillbit);
 
 	drvlog(LOG_INFO, "Successfully initialised %s",
@@ -621,18 +652,79 @@ static const uint32_t bf_offsets[] = {-0x800000, 0, -0x400000};
 
 static bool drillbit_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce)
 {
+	struct cgpu_info *drillbit = thr->cgpu;
+	struct drillbit_info *info = drillbit->device_data;
 	int i;
 
-	nonce = decnonce(nonce);
-	for (i = 0; i < BF_OFFSETS; i++) {
-		if (test_nonce(work, nonce + bf_offsets[i])) {
+	if (info->capabilities & CAP_IS_AVALON) {
+		if (test_nonce(work, nonce)) {
 			submit_tested_work(thr, work);
 			return true;
 		}
 	}
+	else { /* Bitfury */
+		nonce = decnonce(nonce);
+		for (i = 0; i < BF_OFFSETS; i++) {
+			if (test_nonce(work, nonce + bf_offsets[i])) {
+				submit_tested_work(thr, work);
+				return true;
+			}
+		}
+	}
 	return false;
 }
 
+/* Check if this ASIC should be tweaked up or down in clock speed */
+static void drillbit_check_auto(struct thr_info *thr, struct drillbit_chip_info *chip)
+{
+	struct cgpu_info *drillbit = thr->cgpu;
+	AutoTuneRequest request;
+	char buf[SZ_SERIALISED_AUTOTUNEREQUEST+1];
+	int amount;
+	bool tune_up, tune_down;
+
+	/*
+	  Only check automatic tuning every "auto_every" work units,
+	  or if the error count exceeds the 'max' count
+	*/
+	if (chip->success_auto + chip->error_auto < auto_every &&
+	    (chip->error_auto < auto_max))
+		return;
+
+	tune_up = chip->error_auto < auto_good && chip->auto_delta < chip->auto_max;
+	tune_down = chip->error_auto > auto_bad;
+
+
+	drvlog(tune_up||tune_down ? LOG_NOTICE : LOG_DEBUG,
+		"Chip id %d has %d/%d error rate %s", chip->chip_id, chip->error_auto,
+		chip->error_auto + chip->success_auto,
+		tune_up ? " - tuning up" : tune_down ? " - tuning down" : " - no change");
+
+	if (tune_up || tune_down) {
+		/* Value should be tweaked */
+		buf[0] = 'A';
+		request.chip_id = chip->chip_id;
+		request.increase_clock = tune_up;
+		serialise_autotune_request(&buf[1], &request);
+		usb_write_timeout(drillbit, buf, sizeof(buf), &amount, TIMEOUT, C_BF_AUTOTUNE);
+		usb_read_simple_response(drillbit, 'A', C_BF_AUTOTUNE);
+		if (tune_up) {
+			chip->auto_delta++;
+		} else {
+			chip->auto_delta--;
+			if (chip->error_auto >= auto_max
+				&& chip->success_count + chip->error_count > auto_every) {
+				drvlog(LOG_ERR, "Chip id %d capping auto delta at max %d",chip->chip_id,
+					chip->auto_delta);
+				chip->auto_max = chip->auto_delta;
+			}
+		}
+	}
+
+	chip->success_auto = 0;
+	chip->error_auto = 0;
+}
+
 // Check and submit back any pending work results from firmware,
 // returns number of successful results found
 static int check_for_results(struct thr_info *thr)
@@ -684,6 +776,7 @@ static int check_for_results(struct thr_info *thr)
 			goto cleanup;
 		if (!usb_read_fixed_size(drillbit, buf, SZ_SERIALISED_WORKRESULT, TIMEOUT, C_BF_GETRES)) {
 			drvlog(LOG_ERR, "Failed to read response data packet idx %d count 0x%x", j, result_count);
+			drillbit_empty_buffer(drillbit);
 			goto cleanup;
 		}
 		deserialise_work_result(&responses[j], buf);
@@ -698,6 +791,7 @@ static int check_for_results(struct thr_info *thr)
 		chip = find_chip(info, response->chip_id);
 		if (!chip) {
 			drvlog(LOG_ERR, "Got work result for unknown chip id %d", response->chip_id);
+			drillbit_empty_buffer(drillbit);
 			continue;
 		}
 		if (chip->state == IDLE) {
@@ -705,41 +799,43 @@ static int check_for_results(struct thr_info *thr)
 		}
 		if (response->num_nonces > MAX_RESULTS) {
 			drvlog(LOG_ERR, "Got invalid number of result nonces (%d) for chip id %d", response->num_nonces, response->chip_id);
+			drillbit_empty_buffer(drillbit);
 			goto cleanup;
 		}
+
+		found = false;
 		for (i = 0; i < response->num_nonces; i++) {
 			if (unlikely(thr->work_restart))
 				goto cleanup;
-			found = false;
 			for (k = 0; k < WORK_HISTORY_LEN; k++) {
 				/* NB we deliberately check all results against all work because sometimes ASICs seem to give multiple "valid" nonces,
 				   and this seems to avoid some result that would otherwise be rejected by the pool.
-
-				   However we only count one success per result set to avoid artificially inflating the hashrate.
-				   A smarter thing to do here might be to look at the full set of nonces in the response and start from the "best" one first.
 				*/
 				if (chip->current_work[k] && drillbit_checkresults(thr, chip->current_work[k], response->nonce[i])) {
-					if (!found) {
-						chip->success_count++;
-						successful_results++;
-						found = true;
-					}
+					chip->success_count++;
+					chip->success_auto++;
+					successful_results++;
+					found = true;
 				}
 			}
-			if (!found && chip->state != IDLE) {
-				/* all nonces we got back from this chip were invalid */
-				inc_hw_errors(thr);
-				chip->error_count++;
-			}
+		}
+		drvlog(LOG_DEBUG, "%s nonce %08x", (found ? "Good":"Bad"), response->num_nonces ? response->nonce[0] : 0);
+		if (!found && chip->state != IDLE && response->num_nonces > 0) {
+			/* all nonces we got back from this chip were invalid */
+			inc_hw_errors(thr);
+			chip->error_count++;
+			chip->error_auto++;
 		}
 		if (chip->state == WORKING_QUEUED && !response->is_idle)
 			chip->state = WORKING_NOQUEUED; // Time to queue up another piece of "next work"
 		else
 			chip->state = IDLE; // Uh-oh, we're totally out of work for this ASIC!
+
+		if (opt_drillbit_auto && info->protocol_version >= 4)
+			drillbit_check_auto(thr, chip);
 	}
 
 cleanup:
-	drillbit_empty_buffer(drillbit);
 	if (responses)
 		free(responses);
 	return successful_results;
@@ -749,8 +845,7 @@ static void drillbit_send_work_to_chip(struct thr_info *thr, struct drillbit_chi
 {
 	struct cgpu_info *drillbit = thr->cgpu;
 	struct work *work;
-	char cmd;
-	char buf[SZ_SERIALISED_WORKREQUEST];
+	char buf[SZ_SERIALISED_WORKREQUEST+1];
 	int amount, i;
 
 	/* Get some new work for the chip */
@@ -761,12 +856,11 @@ static void drillbit_send_work_to_chip(struct thr_info *thr, struct drillbit_chi
 	}
 
 	drvlog(LOG_DEBUG, "Sending work to chip_id %d", chip->chip_id);
-	serialise_work_request(buf, chip->chip_id, work);
+	serialise_work_request(&buf[1], chip->chip_id, work);
 
 	/* Send work to cgminer */
-	cmd = 'W';
-	usb_write_timeout(drillbit, &cmd, 1, &amount, TIMEOUT, C_BF_REQWORK);
-	usb_write_timeout(drillbit, buf, SZ_SERIALISED_WORKREQUEST, &amount, TIMEOUT, C_BF_REQWORK);
+	buf[0] = 'W';
+	usb_write_timeout(drillbit, buf, sizeof(buf), &amount, TIMEOUT, C_BF_REQWORK);
 
 	/* Expect a single 'W' byte as acknowledgement */
 	usb_read_simple_response(drillbit, 'W', C_BF_REQWORK);
@@ -797,13 +891,14 @@ static int64_t drillbit_scanwork(struct thr_info *thr)
 	struct drillbit_info *info = drillbit->device_data;
 	struct drillbit_chip_info *chip;
 	struct timeval tv_now;
-	int amount, i, j, ms_diff, result_count = 0;;
+	int amount, i, j, ms_diff, result_count = 0, sent_count = 0;;
 	char buf[200];
 
 	/* send work to an any chip without queued work */
-	for (i = 0; i < info->num_chips; i++) {
+	for (i = 0; i < info->num_chips && sent_count < 8; i++) {
 		if (info->chips[i].state != WORKING_QUEUED) {
 			drillbit_send_work_to_chip(thr, &info->chips[i]);
+			sent_count++;
 		}
 		if (unlikely(thr->work_restart) || unlikely(drillbit->usbinfo.nodev))
 			goto cascade;
@@ -815,12 +910,13 @@ static int64_t drillbit_scanwork(struct thr_info *thr)
 		if (info->chips[i].state == IDLE)
 			continue;
 		ms_diff = ms_tdiff(&tv_now, &info->chips[i].tv_start);
-		if (ms_diff > TIMEOUT) {
+		if (ms_diff > RESULT_TIMEOUT) {
 			if (info->chips[i].work_sent_count > 4) {
 				/* Only count ASIC timeouts after the pool has started to send work in earnest,
 				   some pools can create unusual delays early on */
 				drvlog(LOG_ERR, "Timing out unresponsive ASIC %d", info->chips[i].chip_id);
 				info->chips[i].timeout_count++;
+				info->chips[i].error_auto++;
 			}
 			info->chips[i].state = IDLE;
 			drillbit_send_work_to_chip(thr, &info->chips[i]);
@@ -859,8 +955,6 @@ static int64_t drillbit_scanwork(struct thr_info *thr)
 	drillbit_updatetemps(thr);
 
 cascade:
-	drillbit_empty_buffer(drillbit);
-
 	if (unlikely(drillbit->usbinfo.nodev)) {
 		drvlog(LOG_WARNING, "Device disappeared, disabling thread");
 		return -1;
@@ -882,7 +976,7 @@ static struct api_data *drillbit_api_stats(struct cgpu_info *cgpu)
 	char serial[16];
 	int version;
 
-	version = info->version;
+	version = info->protocol_version;
 	root = api_add_int(root, "Protocol Version", &version, true);
 	root = api_add_string(root, "Product", info->product, false);
 	sprintf(serial, "%08x", info->serial);
@@ -961,7 +1055,7 @@ static void deserialise_work_result(WorkResult *wr, const char *buf)
 		DESERIALISE(wr->nonce[i]);
 }
 
-static void serialise_board_config(char *buf, const BoardConfig *bc)
+static void serialise_board_configV3(char *buf, BoardConfigV3 *bc)
 {
 	size_t offset = 0;
 	SERIALISE(bc->core_voltage);
@@ -971,6 +1065,22 @@ static void serialise_board_config(char *buf, const BoardConfig *bc)
 	SERIALISE(bc->ext_clock_freq);
 }
 
+static void serialise_board_configV4(char *buf, BoardConfig *bc)
+{
+	size_t offset = 0;
+	SERIALISE(bc->core_voltage);
+	SERIALISE(bc->clock_freq);
+	SERIALISE(bc->clock_div2);
+	SERIALISE(bc->use_ext_clock);
+}
+
+static void serialise_autotune_request(char *buf, AutoTuneRequest *aq)
+{
+	size_t offset = 0;
+	SERIALISE(aq->chip_id);
+	SERIALISE(aq->increase_clock);
+}
+
 static void deserialise_identity(Identity *id, const char *buf)
 {
 	size_t offset = 0;
diff --git a/driver-drillbit.h b/driver-drillbit.h
index 7377352..b386107 100644
--- a/driver-drillbit.h
+++ b/driver-drillbit.h
@@ -20,7 +20,7 @@ struct drillbit_chip_info;
 /* drillbit_info structure applies to entire device */
 struct drillbit_info {
   struct cgpu_info *base_cgpu;
-  uint8_t version;
+  uint8_t protocol_version;
   uint8_t num_chips;
   uint16_t capabilities;
   char product[8];
@@ -47,6 +47,10 @@ struct drillbit_chip_info {
   uint32_t error_count;
   uint32_t timeout_count;
   uint32_t work_sent_count;
+  uint32_t success_auto;
+  uint32_t error_auto;
+  int auto_delta;
+  int auto_max;
 };
 
 #endif /* BITFURY_H */
diff --git a/driver-icarus.c b/driver-icarus.c
index ba0954d..29d6d8d 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -87,6 +87,11 @@ ASSERT1(sizeof(uint32_t) == 4);
 // In timing mode: Default starting value until an estimate can be obtained
 // 5000 ms allows for up to a ~840MH/s device
 #define ICARUS_READ_COUNT_TIMING	5000
+
+// Antminer USB is > 1GH/s so use a shorter limit
+// 1000 ms allows for up to ~4GH/s device
+#define ANTUSB_READ_COUNT_TIMING	1000
+
 #define ICARUS_READ_COUNT_MIN		ICARUS_WAIT_TIMEOUT
 #define SECTOMS(s)	((int)((s) * 1000))
 // How many ms below the expected completion time to abort work
@@ -533,6 +538,7 @@ static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
 	char buf[BUFSIZ+1];
 	char *ptr, *comma, *eq;
 	size_t max;
+	int read_count_timing;
 	int i;
 
 	if (opt_icarus_timing == NULL)
@@ -562,22 +568,28 @@ static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
 	switch (ident) {
 		case IDENT_ICA:
 			info->Hs = ICARUS_REV3_HASH_TIME;
+			read_count_timing = ICARUS_READ_COUNT_TIMING;
 			break;
 		case IDENT_BLT:
 		case IDENT_LLT:
 			info->Hs = LANCELOT_HASH_TIME;
+			read_count_timing = ICARUS_READ_COUNT_TIMING;
 			break;
 		case IDENT_AMU:
 			info->Hs = ASICMINERUSB_HASH_TIME;
+			read_count_timing = ICARUS_READ_COUNT_TIMING;
 			break;
 		case IDENT_CMR1:
 			info->Hs = CAIRNSMORE1_HASH_TIME;
+			read_count_timing = ICARUS_READ_COUNT_TIMING;
 			break;
 		case IDENT_CMR2:
 			info->Hs = CAIRNSMORE2_HASH_TIME;
+			read_count_timing = ICARUS_READ_COUNT_TIMING;
 			break;
 		case IDENT_ANU:
 			info->Hs = ANTMINERUSB_HASH_TIME;
+			read_count_timing = ANTUSB_READ_COUNT_TIMING;
 			break;
 		default:
 			quit(1, "Icarus get_options() called with invalid %s ident=%d",
@@ -589,13 +601,13 @@ static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
 
 	if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
 		// short
-		info->read_time = ICARUS_READ_COUNT_TIMING;
+		info->read_time = read_count_timing;
 
 		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_time = ICARUS_READ_COUNT_TIMING;
+		info->read_time = read_count_timing;
 
 		info->timing_mode = MODE_SHORT;
 		info->do_icarus_timing = true;
@@ -607,13 +619,13 @@ static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
 			info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX;
 	} else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
 		// long
-		info->read_time = ICARUS_READ_COUNT_TIMING;
+		info->read_time = read_count_timing;
 
 		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_time = ICARUS_READ_COUNT_TIMING;
+		info->read_time = read_count_timing;
 
 		info->timing_mode = MODE_LONG;
 		info->do_icarus_timing = true;
diff --git a/miner.h b/miner.h
index 353b51a..cd04afa 100644
--- a/miner.h
+++ b/miner.h
@@ -984,6 +984,7 @@ extern char *opt_klondike_options;
 #endif
 #ifdef USE_DRILLBIT
 extern char *opt_drillbit_options;
+extern char *opt_drillbit_auto;
 #endif
 #ifdef USE_BAB
 extern char *opt_bab_options;
diff --git a/usbutils.c b/usbutils.c
index c305f6c..df724cf 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -2891,7 +2891,7 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 {
 	unsigned char *ptr, usbbuf[USB_READ_BUFSIZE];
 	struct timeval read_start, tv_finish;
-	int bufleft, err, got, tot, pstate;
+	int bufleft, err, got, tot, pstate, tried_reset;
 	struct cg_usb_device *usbdev;
 	unsigned int initial_timeout;
 	bool first = true;
@@ -2946,6 +2946,7 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 
 	initial_timeout = timeout;
 	cgtime(&read_start);
+	tried_reset = 0;
 	while (bufleft > 0 && !eom) {
 		err = usb_perform_transfer(cgpu, usbdev, intinfo, epinfo, ptr, usbbufread,
 					&got, timeout, MODE_BULK_READ, cmd,
@@ -2976,11 +2977,14 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 		if (err && err != LIBUSB_ERROR_TIMEOUT) {
 			applog(LOG_WARNING, "%s %i %s usb read err:(%d) %s", cgpu->drv->name,
 			       cgpu->device_id, usb_cmdname(cmd), err, libusb_error_name(err));
-			if (err != LIBUSB_ERROR_NO_DEVICE) {
+			if (err != LIBUSB_ERROR_NO_DEVICE && !tried_reset) {
 				err = libusb_reset_device(usbdev->handle);
+				tried_reset = 1; // don't call reset twice in a row
 				applog(LOG_WARNING, "%s %i attempted reset got err:(%d) %s",
 				       cgpu->drv->name, cgpu->device_id, err, libusb_error_name(err));
 			}
+		} else {
+			tried_reset = 0;
 		}
 		ptr += got;
 		bufleft -= got;
@@ -3040,7 +3044,7 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_
 	struct timeval write_start, tv_finish;
 	struct cg_usb_device *usbdev;
 	unsigned int initial_timeout;
-	int err, sent, tot, pstate;
+	int err, sent, tot, pstate, tried_reset;
 	bool first = true;
 	double done;
 
@@ -3065,6 +3069,7 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_
 	err = LIBUSB_SUCCESS;
 	initial_timeout = timeout;
 	cgtime(&write_start);
+	tried_reset = 0;
 	while (bufsiz > 0) {
 		int tosend = bufsiz;
 
@@ -3094,11 +3099,14 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_
 		if (err) {
 			applog(LOG_WARNING, "%s %i %s usb write err:(%d) %s", cgpu->drv->name,
 			       cgpu->device_id, usb_cmdname(cmd), err, libusb_error_name(err));
-			if (err != LIBUSB_ERROR_NO_DEVICE) {
+			if (err != LIBUSB_ERROR_NO_DEVICE && !tried_reset) {
 				err = libusb_reset_device(usbdev->handle);
+				tried_reset = 1; // don't try reset twice in a row
 				applog(LOG_WARNING, "%s %i attempted reset got err:(%d) %s",
 				       cgpu->drv->name, cgpu->device_id, err, libusb_error_name(err));
 			}
+		} else {
+			tried_reset = 0;
 		}
 		if (err)
 			break;
diff --git a/usbutils.h b/usbutils.h
index 5d918fd..b05882e 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -353,7 +353,8 @@ struct cg_usb_info {
 	USB_ADD_COMMAND(C_BF_IDENTIFY, "BFIdentify") \
 	USB_ADD_COMMAND(C_BF_DETECTCHIPS, "BFDetectChips") \
 	USB_ADD_COMMAND(C_BF_CONFIG, "BFConfig") \
-        USB_ADD_COMMAND(C_BF_GETTEMP, "BFGetTemp") \
+	USB_ADD_COMMAND(C_BF_GETTEMP, "BFGetTemp") \
+	USB_ADD_COMMAND(C_BF_AUTOTUNE, "BFAutoTune") \
 	USB_ADD_COMMAND(C_ATMEL_RESET, "AtmelReset") \
 	USB_ADD_COMMAND(C_ATMEL_OPEN, "AtmelOpen") \
 	USB_ADD_COMMAND(C_ATMEL_INIT, "AtmelInit") \