Commit 93566d15ba1697c62d29f8b2253ae61259b9cbb4

kanoi 2014-07-04T14:08:32

Merge pull request #622 from kanoi/master MInion and Icarus changes

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
diff --git a/driver-icarus.c b/driver-icarus.c
index 3acf961..b429ef5 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -50,8 +50,6 @@
 #include "miner.h"
 #include "usbutils.h"
 
-#define ROCKMINER
-#define ROCKMINER_PRINT_DEBUG 0
 // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
 #define ICARUS_IO_SPEED 115200
 
@@ -188,6 +186,77 @@ static const char *MODE_LONG_STREQ = "long=";
 static const char *MODE_VALUE_STR = "value";
 static const char *MODE_UNKNOWN_STR = "unknown";
 
+#define MAX_DEVICE_NUM 100
+#define MAX_WORK_BUFFER_SIZE 2
+#define MAX_CHIP_NUM 24
+// Set it to 3 or 9
+#define	NONCE_CORRECTION_TIMES	3
+#define MAX_TRIES	4
+#define	RM_CMD_MASK		0x0F
+#define	RM_STATUS_MASK		0xF0
+#define	RM_CHIP_MASK		0x3F
+#define	RM_PRODUCT_MASK		0xC0
+#define	RM_PRODUCT_RBOX		0x00
+#define	RM_PRODUCT_T1		0x40
+#define	RM_PRODUCT_T2		0x80
+#define	RM_PRODUCT_TEST		0xC0
+
+#if (NONCE_CORRECTION_TIMES == 9)
+static int32_t rbox_corr_values[] = {0, 1, -1, 2, -2, 3, -3, 4, -4};
+#endif
+#if (NONCE_CORRECTION_TIMES == 3)
+static int32_t rbox_corr_values[] = {0, 1, -1};
+#endif
+
+typedef enum {
+	NONCE_DATA1_OFFSET = 0,
+	NONCE_DATA2_OFFSET,
+	NONCE_DATA3_OFFSET,
+	NONCE_DATA4_OFFSET,
+	NONCE_TASK_CMD_OFFSET,
+	NONCE_CHIP_NO_OFFSET,
+	NONCE_TASK_NO_OFFSET,
+	NONCE_COMMAND_OFFSET,
+	NONCE_MAX_OFFSET
+} NONCE_OFFSET;
+
+typedef enum {
+	NONCE_DATA_CMD = 0,
+	NONCE_TASK_COMPLETE_CMD,
+	NONCE_GET_TASK_CMD,
+} NONCE_COMMAND;
+
+typedef struct nonce_data {
+	int chip_no;
+	unsigned int task_no ;
+	unsigned char work_state;
+	int cmd_value;
+} NONCE_DATA;
+
+typedef enum {
+	ROCKMINER_RBOX = 0,
+	ROCKMINER_T1,
+	ROCKMINER_T2,
+	ROCKMINER_MAX
+} ROCKMINER_PRODUCT_T;
+
+typedef struct rockminer_chip_info {
+	unsigned char freq;
+	int error_cnt;
+	time_t last_received_task_complete_time;
+} ROCKMINER_CHIP_INFO;
+
+typedef struct rockminer_device_info {
+	unsigned char detect_chip_no;
+	unsigned char chip_max;
+	unsigned char product_id;
+	float min_frq;
+	float def_frq;
+	float max_frq;
+	ROCKMINER_CHIP_INFO chip[MAX_CHIP_NUM];
+	time_t dev_detect_time;
+} ROCKMINER_DEVICE_INFO;
+
 struct ICARUS_INFO {
 	enum sub_ident ident;
 	int intinfo;
@@ -237,6 +306,15 @@ struct ICARUS_INFO {
 	int nonce_size;
 
 	bool failing;
+
+	ROCKMINER_DEVICE_INFO rmdev;
+	struct work *g_work[MAX_CHIP_NUM][MAX_WORK_BUFFER_SIZE];
+	char rock_init[64];
+	uint64_t nonces_checked;
+	uint64_t nonces_correction_times;
+	uint64_t nonces_correction_tests;
+	uint64_t nonces_fail;
+	uint64_t nonces_correction[NONCE_CORRECTION_TIMES];
 };
 
 #define ICARUS_MIDSTATE_SIZE 32
@@ -275,84 +353,6 @@ struct ICARUS_WORK {
 	uint8_t work[ICARUS_WORK_SIZE];
 };
 
-#define MAX_DEVICE_NUM 100
-#define MAX_WORK_BUFFER_SIZE 2
-#define MAX_CHIP_NUM 24
-#define	NONCE_CORRECTION_TIMES	3
-#define MAX_TRIES	4
-//#define	NONCE_TEST
-#define	RM_CMD_MASK		0x0F
-#define	RM_STATUS_MASK		0xF0
-#define	RM_CHIP_MASK		0x3F
-#define	RM_PRODUCT_MASK		0xC0
-#define	RM_PRODUCT_RBOX		0x00
-#define	RM_PRODUCT_T1		0x10
-
-#ifdef NONCE_TEST
-int device_nonce_conunts[MAX_DEVICE_NUM];
-int chip_nonce_conunts[MAX_CHIP_NUM];
-int chip_work_conunts[MAX_CHIP_NUM];
-int device_nonce_corr_ok_1[MAX_DEVICE_NUM];
-int device_nonce_corr_ok[MAX_DEVICE_NUM];
-int device_nonce_corr_fail[MAX_DEVICE_NUM];
-int device_nonce_corr_fail_2[MAX_DEVICE_NUM];
-#endif
-
-typedef enum {
-	NONCE_DATA1_OFFSET= 0,
-	NONCE_DATA2_OFFSET,
-	NONCE_DATA3_OFFSET,
-	NONCE_DATA4_OFFSET,
-	NONCE_TASK_CMD_OFFSET,
-	NONCE_CHIP_NO_OFFSET,
-	NONCE_TASK_NO_OFFSET,
-	NONCE_COMMAND_OFFSET,
-	NONCE_MAX_OFFSET
-} NONCE_OFFSET;
-
-typedef enum {
-	NONCE_DATA_CMD= 0,
-	NONCE_TASK_COMPLETE_CMD,
-	NONCE_GET_TASK_CMD,
-} NONCE_COMMAND;
-
-typedef struct NONCE_DATA
-{
-int chip_no ;
-unsigned int task_no ;
-unsigned char work_state;
-int cmd_value;
-}NONCE_DATA;
-
-
-
-typedef enum {
-	ROCKMINER_RBOX= 0,
-	ROCKMINER_T1,
-	ROCKMINER_MAX
-} ROCKMINER_PRODUCT_T;
-
-typedef struct ROCKMINER_CHIP_INFO
-{
-unsigned char freq;
-int error_cnt;
-time_t last_received_task_complete_time;
-}ROCKMINER_CHIP_INFO;
-
-typedef struct ROCKMINER_DEVICE_INFO
-{
-unsigned char detect_chip_no;
-unsigned char chip_max;
-unsigned char product_id;
-float min_frq;
-float def_frq;
-float max_frq;
-ROCKMINER_CHIP_INFO chip[MAX_CHIP_NUM];
-time_t dev_detect_time;
-}ROCKMINER_DEVICE_INFO;
-
-ROCKMINER_DEVICE_INFO rmdev[MAX_DEVICE_NUM];
-
 #define END_CONDITION 0x0000ffff
 
 // Looking for options in --icarus-timing and --icarus-options:
@@ -513,6 +513,7 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
 			break;
 		case IDENT_AMU:
 		case IDENT_ANU:
+		case IDENT_LIN:
 			// Enable the UART
 			transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE,
 				 CP210X_VALUE_UART_ENABLE,
@@ -930,15 +931,15 @@ unsigned char crc5(unsigned char *ptr, unsigned char len)
 		memcpy(crcin, crcout, 5);
 	}
 	crc = 0;
-	if(crcin[4])
+	if (crcin[4])
 		crc |= 0x10;
-	if(crcin[3])
+	if (crcin[3])
 		crc |= 0x08;
-	if(crcin[2])
+	if (crcin[2])
 		crc |= 0x04;
-	if(crcin[1])
+	if (crcin[1])
 		crc |= 0x02;
-	if(crcin[0])
+	if (crcin[0])
 		crc |= 0x01;
 	return crc;
 }
@@ -1036,45 +1037,23 @@ static bool set_anu_freq(struct cgpu_info *icarus, struct ICARUS_INFO *info)
 	return true;
 }
 
-static void rock_init_last_received_task_complete_time(uint32_t dev_id)
+static void rock_init_last_received_task_complete_time(struct ICARUS_INFO *info)
 {
 	int i;
-	if(opt_rock_freq<rmdev[dev_id].min_frq||(opt_rock_freq>rmdev[dev_id].max_frq))
-			opt_rock_freq =rmdev[dev_id].def_frq;
-	for (i = 0; i < MAX_CHIP_NUM; ++i)
-	{
-		rmdev[dev_id].chip[i].last_received_task_complete_time = time(NULL);
-		rmdev[dev_id].chip[i].freq = opt_rock_freq/10 - 1;
-		rmdev[dev_id].chip[i].error_cnt= 0;
-	}
-	rmdev[dev_id].dev_detect_time = time(NULL);
-}
-
-struct work *g_work[MAX_DEVICE_NUM][MAX_CHIP_NUM][MAX_WORK_BUFFER_SIZE];
 
-void clear_chip_busy(int device_id)
-{
-	memset(&(g_work[device_id][0]), 0, (sizeof(g_work)/MAX_DEVICE_NUM));
-}
+	if (opt_rock_freq < info->rmdev.min_frq ||
+	    opt_rock_freq > info->rmdev.max_frq)
+		opt_rock_freq = info->rmdev.def_frq;
 
-void rock_print_work_data(struct ICARUS_WORK *p_workdata)
-{
-	char *ob_hex = NULL;
-	opt_debug = ROCKMINER_PRINT_DEBUG;
-	if (opt_debug) {
-		ob_hex = bin2hex((void *)(p_workdata), sizeof(*p_workdata));
-		applog(LOG_WARNING, "sent %s", ob_hex);
-		free(ob_hex);
-
-        rev((void *)(&(p_workdata->midstate)), ICARUS_MIDSTATE_SIZE);
-    	rev((void *)(&(p_workdata->work)), ICARUS_WORK_SIZE);
-    	ob_hex = bin2hex((void *)(p_workdata), sizeof(*p_workdata));
-    	applog(LOG_WARNING, "revert sent %s", ob_hex);
-    	free(ob_hex);
+	for (i = 0; i < MAX_CHIP_NUM; ++i) {
+		info->rmdev.chip[i].last_received_task_complete_time = time(NULL);
+		info->rmdev.chip[i].freq = opt_rock_freq/10 - 1;
+		info->rmdev.chip[i].error_cnt = 0;
 	}
+
+	info->rmdev.dev_detect_time = time(NULL);
 }
 
-bool icarus_get_device_id(struct cgpu_info *cgpu);
 
 static void icarus_clear(struct cgpu_info *icarus, struct ICARUS_INFO *info)
 {
@@ -1359,14 +1338,6 @@ shin:
 
 static int64_t rock_scanwork(struct thr_info *thr);
 
-static bool rock_prepare(struct thr_info *thr)
-{
-	struct cgpu_info *icarus = thr->cgpu;
-
-	icarus->drv->name = "LIN";
-	return true;
-}
-
 static void rock_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu)
 {
 	if (cgpu->temp)
@@ -1379,7 +1350,6 @@ static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_f
 {
 	struct ICARUS_INFO *info;
 	struct timeval tv_start, tv_finish;
-	int opt_debug = ROCKMINER_PRINT_DEBUG;
 	char *ob_hex = NULL;
 
 	// Block 171874 nonce = (0xa2870100) = 0x000187a2
@@ -1401,17 +1371,9 @@ static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_f
 	int ret, err, amount, tries;
 	bool ok;
 	int correction_times = 0;
-#if (NONCE_CORRECTION_TIMES == 9)
-	int32_t correction_value[] = {0, 1, -1, 2, -2, 3, -3, 4, -4};
-#endif
-#if (NONCE_CORRECTION_TIMES == 3)
-	int32_t correction_value[] = {0, 1, -1};
-#endif
 	NONCE_DATA nonce_data;
-
 	uint32_t nonce;
-	//int cmd_value = 0;
-	int dev_id;
+	char *newname = NULL;
 
 	if ((sizeof(workdata) << 1) != (sizeof(golden_ob) - 1))
 		quithere(1, "Data and golden_ob sizes don't match");
@@ -1421,16 +1383,9 @@ static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_f
 	if (!usb_init(icarus, dev, found))
 		goto shin;
 
-#ifdef WIN32
-	dev_id = icarus_get_device_id(icarus);
-#else
-	dev_id = icarus->device_id;
-#endif
-
 	hex2bin((void *)(&workdata), golden_ob, sizeof(workdata));
 	rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE);
 	rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE);
-	opt_debug = ROCKMINER_PRINT_DEBUG;
 	if (opt_debug) {
 		ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
 		applog(LOG_WARNING, "%s%d: send_gold_nonce %s",
@@ -1452,100 +1407,101 @@ static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_f
 	tries = MAX_TRIES;
 	ok = false;
 	while (!ok && tries-- > 0) {
-#if ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "tries: %d", tries);
-#endif
+		icarus_initialise(icarus, info->baud);
+
+		applog(LOG_DEBUG, "tries: %d", tries);
 		workdata.unused[ICARUS_UNUSED_SIZE - 3] = opt_rock_freq/10 - 1;
 		workdata.unused[ICARUS_UNUSED_SIZE - 2] = (MAX_TRIES-1-tries);
-		rmdev[dev_id].detect_chip_no ++;
-			if(rmdev[dev_id].detect_chip_no >=MAX_TRIES )
-				rmdev[dev_id].detect_chip_no = 0;
-		//g_detect_chip_no[dev_id] = (g_detect_chip_no[dev_id] + 1) & MAX_CHIP_NUM;
+		info->rmdev.detect_chip_no++;
+		if (info->rmdev.detect_chip_no >= MAX_TRIES)
+			info->rmdev.detect_chip_no = 0;
+		//g_detect_chip_no = (g_detect_chip_no + 1) & MAX_CHIP_NUM;
 
 		err = usb_write_ii(icarus, info->intinfo,
 				   (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
-		rock_print_work_data(&workdata);
 		if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
-		continue;
+			continue;
 
 		memset(nonce_bin, 0, sizeof(nonce_bin));
 		ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 100);
-#if ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "Rockminer nonce_bin: %02X, %02X, %02X, %02X, %02X, %02X, %02X, %02X", nonce_bin[0], nonce_bin[1], nonce_bin[2], nonce_bin[3], nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]);
-#endif
-		if (ret != ICA_NONCE_OK)
-		{
-#if ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "detect_one get_gold_nonce error, tries = %d.\r\n", tries);
-#endif
+
+		applog(LOG_DEBUG, "Rockminer nonce_bin: %02x %02x %02x %02x %02x %02x %02x %02x",
+				  nonce_bin[0], nonce_bin[1], nonce_bin[2], nonce_bin[3],
+				  nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]);
+		if (ret != ICA_NONCE_OK) {
+			applog(LOG_DEBUG, "detect_one get_gold_nonce error, tries = %d", tries);
 			continue;
 		}
 
-		//device max chip
-		if((nonce_bin[NONCE_CHIP_NO_OFFSET]&RM_PRODUCT_MASK)==RM_PRODUCT_T1)
-		{
-			rmdev[dev_id].product_id = ROCKMINER_T1;
-			rmdev[dev_id].chip_max = 24;
-			rmdev[dev_id].min_frq = 200;
-			rmdev[dev_id].def_frq = 280;
-			rmdev[dev_id].max_frq = 320;
-		}
-		else
-		{
-			rmdev[dev_id].product_id= ROCKMINER_RBOX;
-			rmdev[dev_id].chip_max = 4;
-			rmdev[dev_id].min_frq = 200;
-			rmdev[dev_id].def_frq = 270;
-			rmdev[dev_id].max_frq = 290;
+		newname = NULL;
+		switch (nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_PRODUCT_MASK) {
+			case RM_PRODUCT_T1:
+				newname = "LIR"; // Rocketbox
+				info->rmdev.product_id = ROCKMINER_T1;
+				info->rmdev.chip_max = 12;
+				info->rmdev.min_frq = 200;
+				info->rmdev.def_frq = 300;
+				info->rmdev.max_frq = 400;
+				break;
+			case RM_PRODUCT_T2: // what's this?
+				newname = "LIX";
+				info->rmdev.product_id = ROCKMINER_T2;
+				info->rmdev.chip_max = 16;
+				info->rmdev.min_frq = 200;
+				info->rmdev.def_frq = 300;
+				info->rmdev.max_frq = 400;
+				break;
+			case RM_PRODUCT_RBOX:
+				newname = "LIN"; // R-Box
+				info->rmdev.product_id = ROCKMINER_RBOX;
+				info->rmdev.chip_max = 4;
+				info->rmdev.min_frq = 200;
+				info->rmdev.def_frq = 270;
+				info->rmdev.max_frq = 400;
+				break;
+			default:
+				continue;
 		}
-		nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET]&RM_CHIP_MASK;
-		if (nonce_data.chip_no >= rmdev[dev_id].chip_max)
-		{
+
+		snprintf(info->rock_init, sizeof(info->rock_init), "%02x %02x %02x %02x",
+				  nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]);
+
+		nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_CHIP_MASK;
+		if (nonce_data.chip_no >= info->rmdev.chip_max)
 			nonce_data.chip_no = 0;
-		}
 
-		nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET]&RM_CMD_MASK;
-		if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD)
-		{
-#if ROCKMINER_PRINT_DEBUG
-			applog(LOG_WARNING, "complete g_detect_chip_no: %d", rmdev[dev_id].detect_chip_no);
-#endif
-		workdata.unused[ICARUS_UNUSED_SIZE - 3] = opt_rock_freq/10 - 1;
-			workdata.unused[ICARUS_UNUSED_SIZE - 2] =  rmdev[dev_id].detect_chip_no;
-			rmdev[dev_id].detect_chip_no ++;
-			if(rmdev[dev_id].detect_chip_no>=MAX_TRIES)
-				rmdev[dev_id].detect_chip_no = 0;
+		nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_CMD_MASK;
+		if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD) {
+			applog(LOG_DEBUG, "complete g_detect_chip_no: %d", info->rmdev.detect_chip_no);
+			workdata.unused[ICARUS_UNUSED_SIZE - 3] = opt_rock_freq/10 - 1;
+			workdata.unused[ICARUS_UNUSED_SIZE - 2] =  info->rmdev.detect_chip_no;
+			info->rmdev.detect_chip_no++;
+			if (info->rmdev.detect_chip_no >= MAX_TRIES)
+				info->rmdev.detect_chip_no = 0;
 
 			err = usb_write_ii(icarus, info->intinfo,
 				   (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
-			rock_print_work_data(&workdata);
 			if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
 				continue;
-#if ROCKMINER_PRINT_DEBUG
-			applog(LOG_WARNING, "send_gold_nonce usb_write_ii");
-#endif
+			applog(LOG_DEBUG, "send_gold_nonce usb_write_ii");
 			continue;
 		}
 
 		memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE);
 		nonce = htobe32(nonce);
-#if ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "Rockminer nonce: %08X", nonce);
-#endif
+		applog(LOG_DEBUG, "Rockminer nonce: %08X", nonce);
 		correction_times = 0;
-		while(correction_times<NONCE_CORRECTION_TIMES)
-		{
+		while (correction_times < NONCE_CORRECTION_TIMES) {
 			nonce_hex = bin2hex(nonce_bin, 4);
-			if (golden_nonce_val == nonce + correction_value[correction_times]) {
-				clear_chip_busy(dev_id);
-				rock_init_last_received_task_complete_time(dev_id);
+			if (golden_nonce_val == nonce + rbox_corr_values[correction_times]) {
+				memset(&(info->g_work[0]), 0, sizeof(info->g_work));
+				rock_init_last_received_task_complete_time(info);
 
 				ok = true;
 				break;
 			} else {
-#if ROCKMINER_PRINT_DEBUG
-				applog(LOG_WARNING, "detect_one gold_nonce compare error times = %d\r\n", correction_times);
-#endif
+				applog(LOG_DEBUG, "detect_one gold_nonce compare error times = %d",
+						  correction_times);
 				if (tries < 0 && info->ident != IDENT_CMR2) {
 					applog(LOG_WARNING,
 						"Icarus Detect: "
@@ -1553,31 +1509,25 @@ static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_f
 						icarus->device_path, nonce_hex, golden_nonce);
 				}
 
-				if(nonce == 0)
-				{
+				if (nonce == 0)
 					break;
-				}
 			}
 			free(nonce_hex);
 			correction_times++;
 		}
-
-#if ROCKMINER_PRINT_DEBUG
-			if(correction_times > 0 )
-			{
-				if (correction_times < NONCE_CORRECTION_TIMES)
-				applog(LOG_WARNING, "Rockminer correction the Golden nonce success %s%d[%d]=%d",icarus->drv->name, icarus->device_id,chip_no,correction_times);
-				else
-				applog(LOG_WARNING, "Rockminer correction the Golden nonce fail %s%d[%d]=%d",icarus->drv->name, icarus->device_id,chip_no,correction_times);
-			}
-#endif
-
 	}
 
 	if (!ok)
 		goto unshin;
+
+	if (newname) {
+		if (!icarus->drv->copy)
+			icarus->drv = copy_drv(icarus->drv);
+		icarus->drv->name = newname;
+	}
+
 	applog(LOG_DEBUG, "Icarus Detect: Test succeeded at %s: got %s",
-	       icarus->device_path, golden_nonce);
+		          icarus->device_path, golden_nonce);
 
 	/* We have a real Rockminer! */
 	if (!add_cgpu(icarus))
@@ -1585,11 +1535,11 @@ static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_f
 
 	icarus->drv->scanwork = rock_scanwork;
 	icarus->drv->dname = "Rockminer";
-	icarus->drv->thread_prepare = &rock_prepare;
 	icarus->drv->get_statline_before = &rock_statline_before;
 
 	applog(LOG_INFO, "%s%d: Found at %s",
-		icarus->drv->name, icarus->device_id, icarus->device_path);
+			  icarus->drv->name, icarus->device_id,
+			  icarus->device_path);
 
 	timersub(&tv_finish, &tv_start, &(info->golden_tv));
 
@@ -1668,47 +1618,29 @@ void rock_send_task(unsigned char chip_no, unsigned int current_task_id, struct 
 	struct ICARUS_WORK workdata;
 	char *ob_hex;
 	struct work *work = NULL;
-	int dev_id = icarus->device_id;
 
-#if ROCKMINER_PRINT_DEBUG
-	applog(LOG_WARNING, "thr_info id:%d, device_thread:%d, icarus cgminer_id: %d, device_id: %d.", thr->id, thr->device_thread, icarus->cgminer_id, icarus->device_id);
-	applog(LOG_WARNING, "::::::::::::::g_work[%d][%d][%d] = 0x%x", dev_id, chip_no, current_task_id, (int)g_work[dev_id][chip_no][current_task_id]);
-#endif
-	if (g_work[dev_id][chip_no][current_task_id] == NULL)
-	{
+	if (info->g_work[chip_no][current_task_id] == NULL) {
 		work = get_work(thr, thr->id);
 		if (work == NULL)
-		{
 			return;
-		}
-		g_work[dev_id][chip_no][current_task_id] = work;
-	}
-	else
-	{
-		work = g_work[dev_id][chip_no][current_task_id];
-#if ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "::resend work");
-#endif
+		info->g_work[chip_no][current_task_id] = work;
+	} else {
+		work = info->g_work[chip_no][current_task_id];
+		applog(LOG_DEBUG, "::resend work");
 	}
 
-#if ROCKMINER_PRINT_DEBUG
-	//if(chip_no == 7)
-	applog(LOG_WARNING, "::::::::::::::g_work[%d][%d][%d] = 0x%x", dev_id, chip_no, current_task_id, (int)g_work[dev_id][chip_no][current_task_id]);
-#endif
-
 	memset((void *)(&workdata), 0, sizeof(workdata));
 	memcpy(&(workdata.midstate), work->midstate, ICARUS_MIDSTATE_SIZE);
 	memcpy(&(workdata.work), work->data + ICARUS_WORK_DATA_OFFSET, ICARUS_WORK_SIZE);
 	workdata.unused[ICARUS_UNUSED_SIZE - 4] = 0xaa;
-	if((rmdev[dev_id].chip[chip_no].freq>(rmdev[dev_id].max_frq/10-1))||(rmdev[dev_id].chip[chip_no].freq<(rmdev[dev_id].min_frq/10-1)))
-	{
-		rock_init_last_received_task_complete_time(dev_id);
-	}
-	workdata.unused[ICARUS_UNUSED_SIZE - 3] = rmdev[dev_id].chip[chip_no].freq; //icarus->freq/10 - 1; ;
+	if (info->rmdev.chip[chip_no].freq > (info->rmdev.max_frq/10 - 1) || 
+	    info->rmdev.chip[chip_no].freq < (info->rmdev.min_frq/10 - 1))
+		rock_init_last_received_task_complete_time(info);
+
+	workdata.unused[ICARUS_UNUSED_SIZE - 3] = info->rmdev.chip[chip_no].freq; //icarus->freq/10 - 1; ;
 	workdata.unused[ICARUS_UNUSED_SIZE - 2] = chip_no ;
 	workdata.unused[ICARUS_UNUSED_SIZE - 1] = 0x55;
 
-	opt_debug = ROCKMINER_PRINT_DEBUG;
 	if (opt_debug) {
 		ob_hex = bin2hex((void *)(work->data), 128);
 		applog(LOG_WARNING, "%s%d: work->data %s",
@@ -1716,18 +1648,9 @@ void rock_send_task(unsigned char chip_no, unsigned int current_task_id, struct 
 		free(ob_hex);
 	}
 
-#ifndef ROCKMINER
-	rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE);
-	rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE);
-
-	if (info->speed_next_work || info->flash_next_work)
-		//cmr2_commands(icarus);
-#endif
-
 	// We only want results for the work we are about to send
 	usb_buffer_clear(icarus);
 
-
 	err = usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
 
 	if (err < 0 || amount != sizeof(workdata)) {
@@ -1736,31 +1659,135 @@ void rock_send_task(unsigned char chip_no, unsigned int current_task_id, struct 
 		dev_error(icarus, REASON_DEV_COMMS_ERROR);
 		icarus_initialise(icarus, info->baud);
 
-		if(g_work[dev_id][chip_no][current_task_id])
+		if (info->g_work[chip_no][current_task_id])
 		{
-			free_work(g_work[dev_id][chip_no][current_task_id]);
-			g_work[dev_id][chip_no][current_task_id] = NULL;
+			free_work(info->g_work[chip_no][current_task_id]);
+			info->g_work[chip_no][current_task_id] = NULL;
 		}
 
 		return;
 	}
 
-	opt_debug = ROCKMINER_PRINT_DEBUG;
-	if (opt_debug) {
-		ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
-		applog(LOG_WARNING, "%s%d: sent %s",
-			icarus->drv->name, icarus->device_id, ob_hex);
-		free(ob_hex);
+	return;
+}
 
-        rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE);
-    	rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE);
-    	ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
-    	applog(LOG_WARNING, "%s%d: revert sent %s",
-    		icarus->drv->name, icarus->device_id, ob_hex);
-    	free(ob_hex);
+static void process_history(struct cgpu_info *icarus, struct ICARUS_INFO *info, uint32_t nonce,
+			    uint64_t hash_count, struct timeval *elapsed, struct timeval *tv_start)
+{
+	struct ICARUS_HISTORY *history0, *history;
+	struct timeval tv_history_start, tv_history_finish;
+	int count;
+	double Hs, W, fullnonce;
+	int read_time, i;
+	bool limited;
+	uint32_t values;
+	int64_t hash_count_range;
+	double Ti, Xi;
+
+	// Ignore possible end condition values ...
+	// TODO: set limitations on calculated values depending on the device
+	// to avoid crap values caused by CPU/Task Switching/Swapping/etc
+	if ((nonce & info->nonce_mask) <= END_CONDITION ||
+	    (nonce & info->nonce_mask) >= (info->nonce_mask & ~END_CONDITION))
+		return;
+
+	cgtime(&tv_history_start);
+
+	history0 = &(info->history[0]);
+
+	if (history0->values == 0)
+		timeradd(tv_start, &history_sec, &(history0->finish));
+
+	Ti = (double)(elapsed->tv_sec)
+		+ ((double)(elapsed->tv_usec))/((double)1000000)
+		- ((double)ICARUS_READ_TIME(info->baud));
+	Xi = (double)hash_count;
+	history0->sumXiTi += Xi * Ti;
+	history0->sumXi += Xi;
+	history0->sumTi += Ti;
+	history0->sumXi2 += Xi * Xi;
+
+	history0->values++;
+
+	if (history0->hash_count_max < hash_count)
+		history0->hash_count_max = hash_count;
+	if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
+		history0->hash_count_min = hash_count;
+
+	if (history0->values >= info->min_data_count
+	&&  timercmp(tv_start, &(history0->finish), >)) {
+		for (i = INFO_HISTORY; i > 0; i--)
+			memcpy(&(info->history[i]),
+				&(info->history[i-1]),
+				sizeof(struct ICARUS_HISTORY));
+
+		// Initialise history0 to zero for summary calculation
+		memset(history0, 0, sizeof(struct ICARUS_HISTORY));
+
+		// We just completed a history data set
+		// So now recalc read_time based on the whole history thus we will
+		// initially get more accurate until it completes INFO_HISTORY
+		// total data sets
+		count = 0;
+		for (i = 1 ; i <= INFO_HISTORY; i++) {
+			history = &(info->history[i]);
+			if (history->values >= MIN_DATA_COUNT) {
+				count++;
+
+				history0->sumXiTi += history->sumXiTi;
+				history0->sumXi += history->sumXi;
+				history0->sumTi += history->sumTi;
+				history0->sumXi2 += history->sumXi2;
+				history0->values += history->values;
+
+				if (history0->hash_count_max < history->hash_count_max)
+					history0->hash_count_max = history->hash_count_max;
+				if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
+					history0->hash_count_min = history->hash_count_min;
+			}
+		}
+
+		// All history data
+		Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
+			/ (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
+		W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
+		hash_count_range = history0->hash_count_max - history0->hash_count_min;
+		values = history0->values;
+
+		// Initialise history0 to zero for next data set
+		memset(history0, 0, sizeof(struct ICARUS_HISTORY));
+
+		fullnonce = W + Hs * (((double)0xffffffff) + 1);
+		read_time = SECTOMS(fullnonce) - ICARUS_READ_REDUCE;
+		if (info->read_time_limit > 0 && read_time > info->read_time_limit) {
+			read_time = info->read_time_limit;
+			limited = true;
+		} else
+			limited = false;
+
+		info->Hs = Hs;
+		info->read_time = read_time;
+
+		info->fullnonce = fullnonce;
+		info->count = count;
+		info->W = W;
+		info->values = values;
+		info->hash_count_range = hash_count_range;
+
+		if (info->min_data_count < MAX_MIN_DATA_COUNT)
+			info->min_data_count *= 2;
+		else if (info->timing_mode == MODE_SHORT)
+			info->do_icarus_timing = false;
+
+		applog(LOG_WARNING, "%s%d Re-estimate: Hs=%e W=%e read_time=%dms%s fullnonce=%.3fs",
+				icarus->drv->name, icarus->device_id, Hs, W, read_time,
+				limited ? " (limited)" : "", fullnonce);
 	}
+	info->history_count++;
+	cgtime(&tv_history_finish);
 
-	return;
+	timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
+	timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
 }
 
 static int64_t icarus_scanwork(struct thr_info *thr)
@@ -1774,20 +1801,10 @@ static int64_t icarus_scanwork(struct thr_info *thr)
 	uint32_t nonce;
 	int64_t hash_count = 0;
 	struct timeval tv_start, tv_finish, elapsed;
-	struct timeval tv_history_start, tv_history_finish;
-	double Ti, Xi;
-	int curr_hw_errors, i;
+	int curr_hw_errors;
 	bool was_hw_error;
 	struct work *work;
-
-	struct ICARUS_HISTORY *history0, *history;
-	int count;
-	double Hs, W, fullnonce;
-	int read_time;
-	bool limited;
 	int64_t estimate_hashes;
-	uint32_t values;
-	int64_t hash_count_range;
 
 	if (unlikely(share_work_tdiff(icarus) > info->fail_time)) {
 		if (info->failing) {
@@ -1906,111 +1923,8 @@ static int64_t icarus_scanwork(struct thr_info *thr)
 			nonce, (long unsigned int)hash_count,
 			(long)elapsed.tv_sec, (long)elapsed.tv_usec);
 
-	// Ignore possible end condition values ... and hw errors
-	// TODO: set limitations on calculated values depending on the device
-	// to avoid crap values caused by CPU/Task Switching/Swapping/etc
-	if (info->do_icarus_timing
-	&&  !was_hw_error
-	&&  ((nonce & info->nonce_mask) > END_CONDITION)
-	&&  ((nonce & info->nonce_mask) < (info->nonce_mask & ~END_CONDITION))) {
-		cgtime(&tv_history_start);
-
-		history0 = &(info->history[0]);
-
-		if (history0->values == 0)
-			timeradd(&tv_start, &history_sec, &(history0->finish));
-
-		Ti = (double)(elapsed.tv_sec)
-			+ ((double)(elapsed.tv_usec))/((double)1000000)
-			- ((double)ICARUS_READ_TIME(info->baud));
-		Xi = (double)hash_count;
-		history0->sumXiTi += Xi * Ti;
-		history0->sumXi += Xi;
-		history0->sumTi += Ti;
-		history0->sumXi2 += Xi * Xi;
-
-		history0->values++;
-
-		if (history0->hash_count_max < hash_count)
-			history0->hash_count_max = hash_count;
-		if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
-			history0->hash_count_min = hash_count;
-
-		if (history0->values >= info->min_data_count
-		&&  timercmp(&tv_start, &(history0->finish), >)) {
-			for (i = INFO_HISTORY; i > 0; i--)
-				memcpy(&(info->history[i]),
-					&(info->history[i-1]),
-					sizeof(struct ICARUS_HISTORY));
-
-			// Initialise history0 to zero for summary calculation
-			memset(history0, 0, sizeof(struct ICARUS_HISTORY));
-
-			// We just completed a history data set
-			// So now recalc read_time based on the whole history thus we will
-			// initially get more accurate until it completes INFO_HISTORY
-			// total data sets
-			count = 0;
-			for (i = 1 ; i <= INFO_HISTORY; i++) {
-				history = &(info->history[i]);
-				if (history->values >= MIN_DATA_COUNT) {
-					count++;
-
-					history0->sumXiTi += history->sumXiTi;
-					history0->sumXi += history->sumXi;
-					history0->sumTi += history->sumTi;
-					history0->sumXi2 += history->sumXi2;
-					history0->values += history->values;
-
-					if (history0->hash_count_max < history->hash_count_max)
-						history0->hash_count_max = history->hash_count_max;
-					if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
-						history0->hash_count_min = history->hash_count_min;
-				}
-			}
-
-			// All history data
-			Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
-				/ (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
-			W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
-			hash_count_range = history0->hash_count_max - history0->hash_count_min;
-			values = history0->values;
-			
-			// Initialise history0 to zero for next data set
-			memset(history0, 0, sizeof(struct ICARUS_HISTORY));
-
-			fullnonce = W + Hs * (((double)0xffffffff) + 1);
-			read_time = SECTOMS(fullnonce) - ICARUS_READ_REDUCE;
-			if (info->read_time_limit > 0 && read_time > info->read_time_limit) {
-				read_time = info->read_time_limit;
-				limited = true;
-			} else
-				limited = false;
-
-			info->Hs = Hs;
-			info->read_time = read_time;
-
-			info->fullnonce = fullnonce;
-			info->count = count;
-			info->W = W;
-			info->values = values;
-			info->hash_count_range = hash_count_range;
-
-			if (info->min_data_count < MAX_MIN_DATA_COUNT)
-				info->min_data_count *= 2;
-			else if (info->timing_mode == MODE_SHORT)
-				info->do_icarus_timing = false;
-
-			applog(LOG_WARNING, "%s%d Re-estimate: Hs=%e W=%e read_time=%dms%s fullnonce=%.3fs",
-					icarus->drv->name, icarus->device_id, Hs, W, read_time,
-					limited ? " (limited)" : "", fullnonce);
-		}
-		info->history_count++;
-		cgtime(&tv_history_finish);
-
-		timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
-		timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
-	}
+	if (info->do_icarus_timing && !was_hw_error)
+		process_history(icarus, info, nonce, hash_count, &elapsed, &tv_start);
 out:
 	free_work(work);
 
@@ -2027,23 +1941,12 @@ static int64_t rock_scanwork(struct thr_info *thr)
 	int64_t hash_count = 0;
 	struct timeval tv_start, tv_finish, elapsed;
 	struct work *work = NULL;
-
 	int64_t estimate_hashes;
 	int correction_times = 0;
-#if (NONCE_CORRECTION_TIMES == 9)
-	int32_t correction_value[] = {0, 1, -1, 2, -2, 3, -3, 4, -4};
-#endif
-#if (NONCE_CORRECTION_TIMES == 3)
-	int32_t correction_value[] = {0, 1, -1};
-#endif
 	NONCE_DATA nonce_data;
 
 	int chip_no = 0;
 	time_t recv_time = 0;
-	//unsigned int task_no = 0;
-	//	int cmd_value = 0;
-	int dev_id = icarus->device_id;
-	//unsigned char work_state;
 
 	if (unlikely(share_work_tdiff(icarus) > info->fail_time)) {
 		if (info->failing) {
@@ -2067,97 +1970,49 @@ static int64_t rock_scanwork(struct thr_info *thr)
 
 	elapsed.tv_sec = elapsed.tv_usec = 0;
 
-
-	for (chip_no = 0; chip_no < rmdev[dev_id].chip_max; chip_no++)
-	{
+	for (chip_no = 0; chip_no < info->rmdev.chip_max; chip_no++) {
 		recv_time = time(NULL);
-		if (recv_time > rmdev[dev_id].chip[chip_no].last_received_task_complete_time + 1)
-		{
-#if NONCE_TEST
-			applog(LOG_WARNING, ":::chip_no error = %d nonce timeout", chip_no);
-#endif
-			rmdev[dev_id].chip[chip_no].last_received_task_complete_time = recv_time;
+		if (recv_time > info->rmdev.chip[chip_no].last_received_task_complete_time + 1) {
+			info->rmdev.chip[chip_no].last_received_task_complete_time = recv_time;
 			rock_send_task(chip_no, 0,thr);
 			break;
 		}
 	}
 
-	/* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
 	memset(nonce_bin, 0, sizeof(nonce_bin));
 	ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, 3000);//info->read_time);
-#if ROCKMINER_PRINT_DEBUG
-	applog(LOG_WARNING, "Rockminer nonce_bin: ret = %d. %02X, %02X, %02X, %02X, %02X, %02X, %02X, %02X", ret, nonce_bin[0], nonce_bin[1], nonce_bin[2], nonce_bin[3], nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]);
-#endif
-	if((nonce_bin[NONCE_CHIP_NO_OFFSET]&RM_PRODUCT_MASK)==RM_PRODUCT_T1)
-		{
-			rmdev[dev_id].product_id = ROCKMINER_T1;
-			rmdev[dev_id].chip_max = 24;
-			rmdev[dev_id].min_frq = 200;
-			rmdev[dev_id].def_frq = 280;
-			rmdev[dev_id].max_frq = 320;
-		}
-		else
-		{
-			rmdev[dev_id].product_id= ROCKMINER_RBOX;
-			rmdev[dev_id].chip_max = 4;
-			rmdev[dev_id].min_frq = 200;
-			rmdev[dev_id].def_frq = 270;
-			rmdev[dev_id].max_frq = 290;
-		}
-	nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET]&RM_CHIP_MASK;
-	if(nonce_data.chip_no>=rmdev[dev_id].chip_max)
-		nonce_data.chip_no=0;
-	nonce_data.task_no = nonce_bin[NONCE_TASK_NO_OFFSET]>=2?0:nonce_bin[NONCE_TASK_NO_OFFSET];
-	nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET]&RM_CMD_MASK;
-	nonce_data.work_state = nonce_bin[NONCE_TASK_CMD_OFFSET]&RM_STATUS_MASK;
-#if ROCKMINER_PRINT_DEBUG
-	if(nonce_data.work_state == 2)
-		applog(LOG_WARNING, "Temperature of Device is too hign to work!");
-#endif
+
+	nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_CHIP_MASK;
+	if (nonce_data.chip_no >= info->rmdev.chip_max)
+		nonce_data.chip_no = 0;
+	nonce_data.task_no = (nonce_bin[NONCE_TASK_NO_OFFSET] >= 2) ? 0 : nonce_bin[NONCE_TASK_NO_OFFSET];
+	nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_CMD_MASK;
+	nonce_data.work_state = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_STATUS_MASK;
 
 	icarus->temp = (double)nonce_bin[NONCE_COMMAND_OFFSET];
 	if (icarus->temp == 128)
 		icarus->temp = 0;
 
-	if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD)
-	{
-		rmdev[dev_id].chip[nonce_data.chip_no].last_received_task_complete_time = time(NULL);
-
-		if (g_work[dev_id][nonce_data.chip_no][nonce_data.task_no])
-		{
-			free_work(g_work[dev_id][nonce_data.chip_no][nonce_data.task_no]);
-			g_work[dev_id][nonce_data.chip_no][nonce_data.task_no] = NULL;
+	if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD) {
+		info->rmdev.chip[nonce_data.chip_no].last_received_task_complete_time = time(NULL);
+		if (info->g_work[nonce_data.chip_no][nonce_data.task_no]) {
+			free_work(info->g_work[nonce_data.chip_no][nonce_data.task_no]);
+			info->g_work[nonce_data.chip_no][nonce_data.task_no] = NULL;
 		}
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-			chip_work_conunts[nonce_data.chip_no] ++;
-#endif
-
 		goto out;
 	}
 
-	if (nonce_data.cmd_value == NONCE_GET_TASK_CMD)
-	{
+	if (nonce_data.cmd_value == NONCE_GET_TASK_CMD) {
 		rock_send_task(nonce_data.chip_no, nonce_data.task_no, thr);
 		goto out;
 	}
 
 	if (ret == ICA_NONCE_TIMEOUT)
-	{
-#if ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "chip_no = %d nonce timeout, ret = %d", nonce_data.chip_no, ret);
-#endif
 		rock_send_task(nonce_data.chip_no, nonce_data.task_no, thr);
-	}
-
-#if ROCKMINER_PRINT_DEBUG
-	applog(LOG_WARNING, "Rockminer chip_no: %d", nonce_data.chip_no);
-#endif
 
-	work = g_work[dev_id][nonce_data.chip_no][nonce_data.task_no];
+	work = info->g_work[nonce_data.chip_no][nonce_data.task_no];
 	if (work == NULL)
-	{
 		goto out;
-	}
 
 	if (ret == ICA_NONCE_ERROR)
 		goto out;
@@ -2186,108 +2041,45 @@ static int64_t rock_scanwork(struct thr_info *thr)
 
 	memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE);
 	nonce = htobe32(nonce);
-#if  ROCKMINER_PRINT_DEBUG
-	applog(LOG_WARNING, "Rockminer nonce %s%d[%d/24]-%d: %08X",icarus->drv->name, icarus->device_id,nonce_data.chip_no,nonce_data.task_no, nonce);
-#endif
-
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-	device_nonce_conunts[dev_id] ++;
-	chip_nonce_conunts[nonce_data.chip_no] ++;
-#endif
-
 	recv_time = time(NULL);
-	if((recv_time-rmdev[dev_id].dev_detect_time )>=60)//device_chip_max[device_id]
-		{
+	if ((recv_time-info->rmdev.dev_detect_time) >= 60) {
 		unsigned char i;
-		rmdev[dev_id].dev_detect_time  = recv_time;
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "AMU%d Freq: %d0MHz/ %d0MHz/ %d0MHz/ %d0MHz ",dev_id,rmdev[dev_id].chip[0].freq+1,rmdev[dev_id].chip[1].freq+1,rmdev[dev_id].chip[2].freq+1,rmdev[dev_id].chip[3].freq+1);
-#endif
-			for (i = 0; i  < rmdev[dev_id].chip_max; i ++)
-			{
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-			applog(LOG_WARNING, "AMU%d Chip[%d] nonce=%d/%d/%d,freq:%d0MHz, ",dev_id,i,rmdev[dev_id].chip[i].error_cnt,chip_nonce_conunts[i],chip_work_conunts[i],rmdev[dev_id].chip[i].freq+1);
-			chip_nonce_conunts[i] = 0;
-			chip_work_conunts[i] = 0;
-#endif
-			if(rmdev[dev_id].chip[i].error_cnt>=12)
-				{
-				if(rmdev[dev_id].chip[i].freq>rmdev[dev_id].min_frq)	//200Mhz
-					{
-					rmdev[dev_id].chip[i].freq --;
-#ifdef  NONCE_TEST
-				applog(LOG_WARNING, "AMU%d Chip[%d]=%d SUB FREQ TO %d0MHz,DEF(%fMHz) ",dev_id,i,rmdev[dev_id].chip[i].error_cnt,rmdev[dev_id].chip[i].freq+1,opt_rock_freq);
-#endif
-					}
-				}
-			else if(rmdev[dev_id].chip[i].error_cnt<=1)
-				{
-				if(rmdev[dev_id].chip[i].freq<(rmdev[dev_id].def_frq/10-1))
-					{
-					rmdev[dev_id].chip[i].freq ++;
-#ifdef  NONCE_TEST
-				applog(LOG_WARNING, "AMU%d Chip[%d]=%d ADD FREQ TO %d0MHz,DEF(%fMHz) ",dev_id,i,rmdev[dev_id].chip[i].error_cnt,rmdev[dev_id].chip[i].freq+1,opt_rock_freq);
-#endif
-					}
-				}
-			rmdev[dev_id].chip[i].error_cnt = 0;
+		info->rmdev.dev_detect_time  = recv_time;
+		for (i = 0; i < info->rmdev.chip_max; i ++) {
+			if (info->rmdev.chip[i].error_cnt >= 12) {
+				if (info->rmdev.chip[i].freq > info->rmdev.min_frq)
+					info->rmdev.chip[i].freq--;
+			} else if (info->rmdev.chip[i].error_cnt <= 1) {
+				if (info->rmdev.chip[i].freq < (info->rmdev.def_frq / 10 - 1))
+					info->rmdev.chip[i].freq++;
 			}
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-						device_nonce_corr_ok[dev_id] = 0;
-						device_nonce_corr_fail[dev_id] = 0;
-						device_nonce_corr_fail_2[dev_id] = 0;
-						device_nonce_conunts[dev_id] = 0;
-#endif
-
+			info->rmdev.chip[i].error_cnt = 0;
 		}
+	}
 
 	correction_times = 0;
-	while(correction_times<NONCE_CORRECTION_TIMES)
-	{
-		if (submit_nonce(thr, work, nonce + correction_value[correction_times]))
-		{
+	info->nonces_checked++;
+	while (correction_times < NONCE_CORRECTION_TIMES) {
+		if (correction_times > 0) {
+			info->nonces_correction_tests++;
+			if (correction_times == 1)
+				info->nonces_correction_times++;
+		}
+		if (submit_nonce(thr, work, nonce + rbox_corr_values[correction_times])) {
+			info->nonces_correction[correction_times]++;
 			hash_count++;
 			info->failing = false;
-#if ROCKMINER_PRINT_DEBUG
-			applog(LOG_WARNING, "Rockminer nonce :::OK:::\r\n");
-#endif
+			applog(LOG_DEBUG, "Rockminer nonce :::OK:::");
 			break;
-		}
-		else
-		{
-#if ROCKMINER_PRINT_DEBUG
-			applog(LOG_WARNING, "Rockminer nonce error times = %d\r\n", correction_times);
-#endif
-			if(nonce == 0)
-			{
+		} else {
+			applog(LOG_DEBUG, "Rockminer nonce error times = %d", correction_times);
+			if (nonce == 0)
 				break;
-			}
 		}
 		correction_times++;
 	}
-
-	if(correction_times > 0 )
-	{
-
-	/* FIXME: Where is this i supposed to come from? Comment out instead */
-	//rmdev[dev_id].chip[i].error_cnt ++;
-	    if (correction_times < NONCE_CORRECTION_TIMES)
-	    	{
-
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-	    		device_nonce_corr_ok[dev_id] ++;
-		//applog(LOG_WARNING, "Rockminer correction the nonce success %s%d[%d]=%d",icarus->drv->name, icarus->dev_id,chip_no,correction_times);
-#endif
-	    	}
-		else
-			{
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-		device_nonce_corr_fail[dev_id] ++;
-#endif
-		//applog(LOG_WARNING, "Rockminer correction the nonce fail ---------%s%d[%d]=%d",icarus->drv->name, icarus->dev_id,chip_no,correction_times);
-			}
-
-	}
+	if (correction_times >= NONCE_CORRECTION_TIMES)
+		info->nonces_fail++;
 
 	hash_count = (hash_count * info->nonce_mask);
 
@@ -2308,6 +2100,10 @@ static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
 {
 	struct api_data *root = NULL;
 	struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
+	char data[4096];
+	int i, off;
+	size_t len;
+	float avg;
 
 	// Warning, access to these is not locked - but we don't really
 	// care since hashing performance is way more important than
@@ -2331,6 +2127,39 @@ static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
 	root = api_add_int(root, "work_division", &(info->work_division), false);
 	root = api_add_int(root, "fpga_count", &(info->fpga_count), false);
 
+	if (info->ident == IDENT_LIN) {
+		root = api_add_string(root, "rock_init", info->rock_init, false);
+		root = api_add_uint8(root, "rock_chips", &(info->rmdev.detect_chip_no), false);
+		root = api_add_uint8(root, "rock_chip_max", &(info->rmdev.chip_max), false);
+		root = api_add_uint8(root, "rock_prod_id", &(info->rmdev.product_id), false);
+		root = api_add_avg(root, "rock_min_freq", &(info->rmdev.min_frq), false);
+		root = api_add_avg(root, "rock_max_freq", &(info->rmdev.max_frq), false);
+		root = api_add_uint64(root, "rock_check", &(info->nonces_checked), false);
+		root = api_add_uint64(root, "rock_corr", &(info->nonces_correction_times), false);
+		root = api_add_uint64(root, "rock_corr_tests", &(info->nonces_correction_tests), false);
+		root = api_add_uint64(root, "rock_corr_fail", &(info->nonces_fail), false);
+		if (info->nonces_checked <= 0)
+			avg = 0;
+		else
+			avg = (float)(info->nonces_correction_tests) / (float)(info->nonces_checked);
+		root = api_add_avg(root, "rock_corr_avg", &avg, true);
+		data[0] = '\0';
+		off = 0;
+		for (i = 0; i < NONCE_CORRECTION_TIMES; i++) {
+			len = snprintf(data+off, sizeof(data)-off,
+						"%s%"PRIu64,
+						i > 0 ? "/" : "",
+						info->nonces_correction[i]);
+			if (len >= (sizeof(data)-off))
+				off = sizeof(data)-1;
+			else {
+				if (len > 0)
+					off += len;
+			}
+		}
+		root = api_add_string(root, "rock_corr_finds", data, true);
+	}
+
 	return root;
 }
 
diff --git a/driver-minion.c b/driver-minion.c
index 90c0321..6e33a6d 100644
--- a/driver-minion.c
+++ b/driver-minion.c
@@ -486,10 +486,8 @@ struct minion_que {
  */
 #define MINION_SCAN_mS 88
 
-#define ALLOC_WITEMS 4096
-#define LIMIT_WITEMS 0
-
-typedef struct witem {
+// *** Work lists: generated, queued for a chip, sent to chip
+typedef struct work_item {
 	struct work *work;
 	uint32_t task_id;
 	struct timeval sent;
@@ -500,12 +498,13 @@ typedef struct witem {
 	int errors; // uncertain since the error could mean task_id is wrong
 	struct timeval created; // when work was generated
 	uint64_t ioseq;
-} WITEM;
+} WORK_ITEM;
 
-#define ALLOC_TITEMS 256
-#define LIMIT_TITEMS 0
+#define ALLOC_WORK_ITEMS 4096
+#define LIMIT_WORK_ITEMS 0
 
-typedef struct titem {
+// *** Task queue ready to be sent
+typedef struct task_item {
 	uint64_t tid;
 	uint8_t chip;
 	bool write;
@@ -523,12 +522,13 @@ typedef struct titem {
 	struct work *work;
 	K_ITEM *witem;
 	uint64_t ioseq;
-} TITEM;
+} TASK_ITEM;
 
-#define ALLOC_RITEMS 256
-#define LIMIT_RITEMS 0
+#define ALLOC_TASK_ITEMS 256
+#define LIMIT_TASK_ITEMS 0
 
-typedef struct ritem {
+// *** Results queue ready to be checked
+typedef struct res_item {
 	int chip;
 	int core;
 	uint32_t task_id;
@@ -546,25 +546,47 @@ typedef struct ritem {
 	bool another;
 	uint32_t task_id2;
 	uint32_t nonce2;
-} RITEM;
+} RES_ITEM;
+
+#define ALLOC_RES_ITEMS 256
+#define LIMIT_RES_ITEMS 0
+
+// *** Per chip nonce history
+typedef struct hist_item {
+	struct timeval when;
+} HIST_ITEM;
 
-#define ALLOC_HITEMS 4096
-#define LIMIT_HITEMS 0
+#define ALLOC_HIST_ITEMS 4096
+#define LIMIT_HIST_ITEMS 0
 
 // How much history to keep (5min)
 #define MINION_HISTORY_s 300
+// History required to decide a reset
+#define MINION_RESET_s 120
+
+#if (MINION_RESET_s > MINION_HISTORY_s)
+#error "MINION_RESET_s can't be greater than MINION_HISTORY_s"
+#endif
 
 // History must be always generated for the reset check
 #define MINION_MAX_RESET_CHECK 2
 
-typedef struct hitem {
+// *** Chip freq/MHs performance history
+typedef struct perf_item {
+	double elapsed;
+	uint64_t nonces;
+	uint32_t freq;
 	struct timeval when;
-} HITEM;
+} PREF_ITEM;
+
+#define ALLOC_PERF_ITEMS 128
+#define LIMIT_PERF_ITEMS 0
 
-#define DATAW(_item) ((WITEM *)(_item->data))
-#define DATAT(_item) ((TITEM *)(_item->data))
-#define DATAR(_item) ((RITEM *)(_item->data))
-#define DATAH(_item) ((HITEM *)(_item->data))
+#define DATA_WORK(_item) ((WORK_ITEM *)(_item->data))
+#define DATA_TASK(_item) ((TASK_ITEM *)(_item->data))
+#define DATA_RES(_item) ((RES_ITEM *)(_item->data))
+#define DATA_HIST(_item) ((HIST_ITEM *)(_item->data))
+#define DATA_PERF(_item) ((PREF_ITEM *)(_item->data))
 
 // Set this to 1 to enable iostats processing
 // N.B. it slows down mining
@@ -782,6 +804,11 @@ struct minion_info {
 	struct timeval chip_chk;
 	struct timeval chip_rpt;
 	double history_ghs[MINION_CHIPS];
+
+	// Performance history
+	K_LIST *pfree_list;
+	K_STORE *p_list[MINION_CHIPS];
+
 	// Gets reset to zero each time it is used in reporting
 	int res_err_count[MINION_CHIPS];
 
@@ -820,14 +847,14 @@ static void ready_work(struct cgpu_info *minioncgpu, struct work *work, bool rol
 
 	item = k_unlink_head(minioninfo->wfree_list);
 
-	DATAW(item)->work = work;
-	DATAW(item)->task_id = 0;
-	memset(&(DATAW(item)->sent), 0, sizeof(DATAW(item)->sent));
-	DATAW(item)->nonces = 0;
-	DATAW(item)->urgent = false;
-	DATAW(item)->rolled = rolled;
-	DATAW(item)->errors = 0;
-	cgtime(&(DATAW(item)->created));
+	DATA_WORK(item)->work = work;
+	DATA_WORK(item)->task_id = 0;
+	memset(&(DATA_WORK(item)->sent), 0, sizeof(DATA_WORK(item)->sent));
+	DATA_WORK(item)->nonces = 0;
+	DATA_WORK(item)->urgent = false;
+	DATA_WORK(item)->rolled = rolled;
+	DATA_WORK(item)->errors = 0;
+	cgtime(&(DATA_WORK(item)->created));
 
 	k_add_head(minioninfo->wwork_list, item);
 
@@ -847,15 +874,15 @@ static bool oldest_nonce(struct cgpu_info *minioncgpu, int *chip, int *core, uin
 	item = k_unlink_tail(minioninfo->rnonce_list);
 	if (item) {
 		found = true;
-		*chip = DATAR(item)->chip;
-		*core = DATAR(item)->core;
-		*task_id = DATAR(item)->task_id;
-		*nonce = DATAR(item)->nonce;
-		*no_nonce = DATAR(item)->no_nonce;
-		memcpy(when, &(DATAR(item)->when), sizeof(*when));
-		*another = DATAR(item)->another;
-		*task_id2 = DATAR(item)->task_id2;
-		*nonce2 = DATAR(item)->nonce2;
+		*chip = DATA_RES(item)->chip;
+		*core = DATA_RES(item)->core;
+		*task_id = DATA_RES(item)->task_id;
+		*nonce = DATA_RES(item)->nonce;
+		*no_nonce = DATA_RES(item)->no_nonce;
+		memcpy(when, &(DATA_RES(item)->when), sizeof(*when));
+		*another = DATA_RES(item)->another;
+		*task_id2 = DATA_RES(item)->task_id2;
+		*nonce2 = DATA_RES(item)->nonce2;
 
 		k_free_head(minioninfo->rfree_list, item);
 	}
@@ -1239,7 +1266,7 @@ static int _do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, uin
 }
 #endif
 
-static bool _minion_txrx(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, TITEM *task, MINION_FFL_ARGS)
+static bool _minion_txrx(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, TASK_ITEM *task, MINION_FFL_ARGS)
 {
 	struct minion_header *head;
 
@@ -2155,7 +2182,8 @@ static void minion_detect(bool hotplug)
 
 	mutex_init(&(minioninfo->nonce_lock));
 
-	minioninfo->wfree_list = k_new_list("Work", sizeof(WITEM), ALLOC_WITEMS, LIMIT_WITEMS, true);
+	minioninfo->wfree_list = k_new_list("Work", sizeof(WORK_ITEM),
+					    ALLOC_WORK_ITEMS, LIMIT_WORK_ITEMS, true);
 	minioninfo->wwork_list = k_new_store(minioninfo->wfree_list);
 	minioninfo->wstale_list = k_new_store(minioninfo->wfree_list);
 	// Initialise them all in case we later decide to enable chips
@@ -2164,15 +2192,18 @@ static void minion_detect(bool hotplug)
 		minioninfo->wchip_list[i] = k_new_store(minioninfo->wfree_list);
 	}
 
-	minioninfo->tfree_list = k_new_list("Task", sizeof(TITEM), ALLOC_TITEMS, LIMIT_TITEMS, true);
+	minioninfo->tfree_list = k_new_list("Task", sizeof(TASK_ITEM),
+					    ALLOC_TASK_ITEMS, LIMIT_TASK_ITEMS, true);
 	minioninfo->task_list = k_new_store(minioninfo->tfree_list);
 	minioninfo->treply_list = k_new_store(minioninfo->tfree_list);
 
-	minioninfo->rfree_list = k_new_list("Reply", sizeof(RITEM), ALLOC_RITEMS, LIMIT_RITEMS, true);
+	minioninfo->rfree_list = k_new_list("Reply", sizeof(RES_ITEM),
+					    ALLOC_RES_ITEMS, LIMIT_RES_ITEMS, true);
 	minioninfo->rnonce_list = k_new_store(minioninfo->rfree_list);
 
 	minioninfo->history_gen = MINION_MAX_RESET_CHECK;
-	minioninfo->hfree_list = k_new_list("History", sizeof(HITEM), ALLOC_HITEMS, LIMIT_HITEMS, true);
+	minioninfo->hfree_list = k_new_list("History", sizeof(HIST_ITEM),
+					    ALLOC_HIST_ITEMS, LIMIT_HIST_ITEMS, true);
 	for (i = 0; i < (int)MINION_CHIPS; i++)
 		minioninfo->hchip_list[i] = k_new_store(minioninfo->hfree_list);
 
@@ -2249,7 +2280,7 @@ static void *minion_spi_write(void *userdata)
 	struct cgpu_info *minioncgpu = (struct cgpu_info *)userdata;
 	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
 	K_ITEM *item, *tail, *task, *work;
-	TITEM *titem;
+	TASK_ITEM *titem;
 
 	applog(MINION_LOG, "%s%i: SPI writing...",
 				minioncgpu->drv->name, minioncgpu->device_id);
@@ -2272,7 +2303,7 @@ static void *minion_spi_write(void *userdata)
 		if (tail) {
 			// Find first urgent item
 			item = tail;
-			while (item && !(DATAT(item)->urgent))
+			while (item && !(DATA_TASK(item)->urgent))
 				item = item->prev;
 
 			// No urgent items, just do the tail
@@ -2290,7 +2321,7 @@ static void *minion_spi_write(void *userdata)
 			double howlong;
 			int i;
 
-			titem = DATAT(item);
+			titem = DATA_TASK(item);
 
 			switch (titem->address) {
 				// TODO: case MINION_SYS_TEMP_CTL:
@@ -2327,7 +2358,7 @@ static void *minion_spi_write(void *userdata)
 			if (do_txrx) {
 				if (titem->witem) {
 					cgtime(&now);
-					howlong = tdiff(&now, &(DATAW(titem->witem)->created));
+					howlong = tdiff(&now, &(DATA_WORK(titem->witem)->created));
 					minioninfo->wt_work++;
 					minioninfo->wt_time += howlong;
 					if (minioninfo->wt_min == 0 || minioninfo->wt_min > howlong)
@@ -2386,18 +2417,18 @@ static void *minion_spi_write(void *userdata)
 											    chip);
 									K_WLOCK(minioninfo->tfree_list);
 									task = k_unlink_head(minioninfo->tfree_list);
-									DATAT(task)->tid = ++(minioninfo->next_tid);
-									DATAT(task)->chip = chip;
-									DATAT(task)->write = true;
-									DATAT(task)->address = MINION_SYS_RSTN_CTL;
-									DATAT(task)->task_id = 0; // ignored
-									DATAT(task)->wsiz = MINION_SYS_SIZ;
-									DATAT(task)->rsiz = 0;
-									DATAT(task)->wbuf[0] = SYS_RSTN_CTL_FLUSH;
-									DATAT(task)->wbuf[1] = 0;
-									DATAT(task)->wbuf[2] = 0;
-									DATAT(task)->wbuf[3] = 0;
-									DATAT(task)->urgent = true;
+									DATA_TASK(task)->tid = ++(minioninfo->next_tid);
+									DATA_TASK(task)->chip = chip;
+									DATA_TASK(task)->write = true;
+									DATA_TASK(task)->address = MINION_SYS_RSTN_CTL;
+									DATA_TASK(task)->task_id = 0; // ignored
+									DATA_TASK(task)->wsiz = MINION_SYS_SIZ;
+									DATA_TASK(task)->rsiz = 0;
+									DATA_TASK(task)->wbuf[0] = SYS_RSTN_CTL_FLUSH;
+									DATA_TASK(task)->wbuf[1] = 0;
+									DATA_TASK(task)->wbuf[2] = 0;
+									DATA_TASK(task)->wbuf[3] = 0;
+									DATA_TASK(task)->urgent = true;
 									k_add_head(minioninfo->task_list, task);
 									K_WUNLOCK(minioninfo->tfree_list);
 									minioninfo->chip_status[chip].overheats++;
@@ -2419,7 +2450,7 @@ static void *minion_spi_write(void *userdata)
 							work = minioninfo->wchip_list[chip]->head;
 							while (work) {
 								cnt++;
-								DATAW(work)->stale = true;
+								DATA_WORK(work)->stale = true;
 								work = work->next;
 							}
 							minioninfo->chip_status[chip].chipwork = 0;
@@ -2439,7 +2470,7 @@ static void *minion_spi_write(void *userdata)
 						K_WLOCK(minioninfo->wchip_list[chip]);
 						k_unlink_item(minioninfo->wque_list[chip], titem->witem);
 						k_add_head(minioninfo->wchip_list[chip], titem->witem);
-						DATAW(titem->witem)->ioseq = titem->ioseq;
+						DATA_WORK(titem->witem)->ioseq = titem->ioseq;
 						minioninfo->chip_status[chip].quework--;
 						minioninfo->chip_status[chip].chipwork++;
 #if MINION_SHOW_IO
@@ -2509,14 +2540,14 @@ static void *minion_spi_reply(void *userdata)
 	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
 	struct minion_result *result1, *result2, *use1, *use2;
 	K_ITEM *item;
-	TITEM fifo_task, res1_task, res2_task;
+	TASK_ITEM fifo_task, res1_task, res2_task;
 	int chip, resoff;
 	bool somelow;
 	struct timeval now;
 
 #if ENABLE_INT_NONO
 	uint64_t ioseq;
-	TITEM clr_task;
+	TASK_ITEM clr_task;
 	struct pollfd pfd;
 	struct minion_header *head;
 	uint8_t rbuf[MINION_BUFSIZ];
@@ -2750,9 +2781,9 @@ repeek:
 										minioninfo->use_res2[chip]++;
 									}
 
-									//DATAR(item)->chip = RES_CHIPID(use1);
+									//DATA_RES(item)->chip = RES_CHIPID(use1);
 									// We can avoid any SPI transmission error of the chip number
-									DATAR(item)->chip = (uint8_t)chip;
+									DATA_RES(item)->chip = (uint8_t)chip;
 									if (minioninfo->chipid[chip] != RES_CHIPID(use1)) {
 										minioninfo->spi_errors++;
 										minioninfo->res_spi_errors[chip]++;
@@ -2763,11 +2794,11 @@ repeek:
 										minioninfo->res_spi_errors[chip]++;
 										minioninfo->res_err_count[chip]++;
 									}
-									DATAR(item)->core = RES_CORE(use1);
-									DATAR(item)->task_id = RES_TASK(use1);
-									DATAR(item)->nonce = RES_NONCE(use1);
-									DATAR(item)->no_nonce = !RES_GOLD(use1);
-									memcpy(&(DATAR(item)->when), &now, sizeof(now));
+									DATA_RES(item)->core = RES_CORE(use1);
+									DATA_RES(item)->task_id = RES_TASK(use1);
+									DATA_RES(item)->nonce = RES_NONCE(use1);
+									DATA_RES(item)->no_nonce = !RES_GOLD(use1);
+									memcpy(&(DATA_RES(item)->when), &now, sizeof(now));
 									applog(LOG_DEBUG, "%s%i: reply task_id 0x%04x"
 											  " - chip %d - gold %d",
 											  minioncgpu->drv->name,
@@ -2777,14 +2808,14 @@ repeek:
 											  (int)RES_GOLD(use1));
 
 									if (!use2)
-										DATAR(item)->another = false;
+										DATA_RES(item)->another = false;
 									else {
-										DATAR(item)->another = true;
-										DATAR(item)->task_id2 = RES_TASK(use2);
-										DATAR(item)->nonce2 = RES_NONCE(use2);
+										DATA_RES(item)->another = true;
+										DATA_RES(item)->task_id2 = RES_TASK(use2);
+										DATA_RES(item)->nonce2 = RES_NONCE(use2);
 									}
 //if (RES_GOLD(use1))
-//applog(MINTASK_LOG, "%s%i: found a result chip %d core %d task 0x%04x nonce 0x%08x gold=%d", minioncgpu->drv->name, minioncgpu->device_id, DATAR(item)->chip, DATAR(item)->core, DATAR(item)->task_id, DATAR(item)->nonce, (int)RES_GOLD(use1));
+//applog(MINTASK_LOG, "%s%i: found a result chip %d core %d task 0x%04x nonce 0x%08x gold=%d", minioncgpu->drv->name, minioncgpu->device_id, DATA_RES(item)->chip, DATA_RES(item)->core, DATA_RES(item)->task_id, DATA_RES(item)->nonce, (int)RES_GOLD(use1));
 
 									K_WLOCK(minioninfo->rnonce_list);
 									k_add_head(minioninfo->rnonce_list, item);
@@ -2928,9 +2959,9 @@ static void cleanup_older(struct cgpu_info *minioncgpu, int chip, K_ITEM *item, 
 	/* remove older ioseq work items
 	   no_nonce means this 'item' has finished also */
 	tail = minioninfo->wchip_list[chip]->tail;
-	while (tail && (DATAW(tail)->ioseq < DATAW(item)->ioseq)) {
+	while (tail && (DATA_WORK(tail)->ioseq < DATA_WORK(item)->ioseq)) {
 		k_unlink_item(minioninfo->wchip_list[chip], tail);
-		if (!(DATAW(tail)->stale)) {
+		if (!(DATA_WORK(tail)->stale)) {
 			minioninfo->chip_status[chip].chipwork--;
 #if MINION_SHOW_IO
 			applog(IOCTRL_LOG, "COld chip %d cw-1=%u rw=%u qw=%u",
@@ -2941,26 +2972,26 @@ static void cleanup_older(struct cgpu_info *minioncgpu, int chip, K_ITEM *item, 
 #endif
 /*
 			// If it had no valid work (only errors) then it won't have been cleaned up
-			errs = (DATAW(tail)->errors > 0);
+			errs = (DATA_WORK(tail)->errors > 0);
 			applog(errs ? LOG_DEBUG : LOG_ERR,
 			applog(LOG_ERR,
 				"%s%i: discarded old task 0x%04x chip %d no reply errs=%d",
 				minioncgpu->drv->name, minioncgpu->device_id,
-				DATAW(tail)->task_id, chip, DATAW(tail)->errors);
+				DATA_WORK(tail)->task_id, chip, DATA_WORK(tail)->errors);
 */
 		}
 		applog(MINION_LOG, "%s%i: marking complete - old task 0x%04x chip %d",
 				   minioncgpu->drv->name, minioncgpu->device_id,
-				   DATAW(tail)->task_id, chip);
-		if (DATAW(tail)->rolled)
-			free_work(DATAW(tail)->work);
+				   DATA_WORK(tail)->task_id, chip);
+		if (DATA_WORK(tail)->rolled)
+			free_work(DATA_WORK(tail)->work);
 		else
-			work_completed(minioncgpu, DATAW(tail)->work);
+			work_completed(minioncgpu, DATA_WORK(tail)->work);
 		k_free_head(minioninfo->wfree_list, tail);
 		tail = minioninfo->wchip_list[chip]->tail;
 	}
 	if (no_nonce) {
-		if (!(DATAW(item)->stale)) {
+		if (!(DATA_WORK(item)->stale)) {
 			minioninfo->chip_status[chip].chipwork--;
 #if MINION_SHOW_IO
 		applog(IOCTRL_LOG, "CONoN chip %d cw-1=%u rw=%u qw=%u",
@@ -2972,11 +3003,11 @@ static void cleanup_older(struct cgpu_info *minioncgpu, int chip, K_ITEM *item, 
 		}
 		applog(MINION_LOG, "%s%i: marking complete - no_nonce task 0x%04x chip %d",
 				   minioncgpu->drv->name, minioncgpu->device_id,
-				   DATAW(item)->task_id, chip);
-		if (DATAW(item)->rolled)
-			free_work(DATAW(item)->work);
+				   DATA_WORK(item)->task_id, chip);
+		if (DATA_WORK(item)->rolled)
+			free_work(DATA_WORK(item)->work);
 		else
-			work_completed(minioncgpu, DATAW(item)->work);
+			work_completed(minioncgpu, DATA_WORK(item)->work);
 	}
 }
 
@@ -2986,7 +3017,7 @@ static void restorework(struct minion_info *minioninfo, int chip, K_ITEM *item)
 	K_ITEM *look;
 
 	look = minioninfo->wchip_list[chip]->tail;
-	while (look && DATAW(look)->ioseq < DATAW(item)->ioseq)
+	while (look && DATA_WORK(look)->ioseq < DATA_WORK(item)->ioseq)
 		look = look->prev;
 	if (!look)
 		k_add_head(minioninfo->wchip_list[chip], item);
@@ -3002,6 +3033,7 @@ static enum nonce_state oknonce(struct thr_info *thr, struct cgpu_info *minioncg
 	struct timeval now;
 	K_ITEM *item, *tail;
 	uint32_t min_task_id, max_task_id;
+	uint64_t chip_good;
 	bool redo;
 
 	// if the chip has been disabled - but we don't do that - so not possible (yet)
@@ -3048,14 +3080,14 @@ retry:
 		return NONCE_NO_WORK;
 	}
 
-	min_task_id = DATAW(item)->task_id;
+	min_task_id = DATA_WORK(item)->task_id;
 	while (item) {
-		if (DATAW(item)->task_id == task_id)
+		if (DATA_WORK(item)->task_id == task_id)
 			break;
 
 		item = item->prev;
 	}
-	max_task_id = DATAW(minioninfo->wchip_list[chip]->head)->task_id;
+	max_task_id = DATA_WORK(minioninfo->wchip_list[chip]->head)->task_id;
 
 	if (!item) {
 		K_WUNLOCK(minioninfo->wchip_list[chip]);
@@ -3096,9 +3128,9 @@ retry:
 
 	redo = false;
 retest:
-	if (test_nonce(DATAW(item)->work, nonce)) {
+	if (test_nonce(DATA_WORK(item)->work, nonce)) {
 /*
-		if (isdupnonce(minioncgpu, DATAW(item)->work, nonce)) {
+		if (isdupnonce(minioncgpu, DATA_WORK(item)->work, nonce)) {
 			minioninfo->chip_dup[chip]++;
 			applog(LOG_WARNING, " ... nonce %02x%02x%02x%02x chip %d core %d task 0x%04x",
 					    (nonce & 0xff), ((nonce >> 8) & 0xff),
@@ -3112,14 +3144,14 @@ retest:
 */
 //applog(MINTASK_LOG, "%s%i: Valid Nonce chip %d core %d task 0x%04x nonce 0x%08x", minioncgpu->drv->name, minioncgpu->device_id, chip, core, task_id, nonce);
 //
-		submit_tested_work(thr, DATAW(item)->work);
+		submit_tested_work(thr, DATA_WORK(item)->work);
 
 		if (redo)
 			minioninfo->nonces_recovered[chip]++;
 
-		minioninfo->chip_good[chip]++;
+		chip_good = ++(minioninfo->chip_good[chip]);
 		minioninfo->core_good[chip][core]++;
-		DATAW(item)->nonces++;
+		DATA_WORK(item)->nonces++;
 
 		mutex_lock(&(minioninfo->nonce_lock));
 		minioninfo->new_nonces++;
@@ -3136,11 +3168,11 @@ retest:
 		cgtime(&now);
 		K_WLOCK(minioninfo->hfree_list);
 		item = k_unlink_head(minioninfo->hfree_list);
-		memcpy(&(DATAH(item)->when), when, sizeof(*when));
+		memcpy(&(DATA_HIST(item)->when), when, sizeof(*when));
 		k_add_head(minioninfo->hchip_list[chip], item);
 		for (chip_tmp = 0; chip_tmp < (int)MINION_CHIPS; chip_tmp++) {
 			tail = minioninfo->hchip_list[chip_tmp]->tail;
-			while (tail && tdiff(&(DATAH(tail)->when), &now) > MINION_HISTORY_s) {
+			while (tail && tdiff(&(DATA_HIST(tail)->when), &now) > MINION_HISTORY_s) {
 				tail = k_unlink_tail(minioninfo->hchip_list[chip_tmp]);
 				k_add_head(minioninfo->hfree_list, item);
 				tail = minioninfo->hchip_list[chip_tmp]->tail;
@@ -3148,6 +3180,12 @@ retest:
 		}
 		K_WUNLOCK(minioninfo->hfree_list);
 
+		// Reset the chip after 8 nonces found
+		if (chip_good == 8) {
+			memcpy(&(minioninfo->last_reset[chip]), &now, sizeof(now));
+			init_chip(minioncgpu, minioninfo, chip);
+		}
+
 		return NONCE_GOOD_NONCE;
 	}
 
@@ -3158,7 +3196,7 @@ retest:
 		goto retest;
 	}
 
-	DATAW(item)->errors++;
+	DATA_WORK(item)->errors++;
 	K_WLOCK(minioninfo->wchip_list[chip]);
 	restorework(minioninfo, chip, item);
 	K_WUNLOCK(minioninfo->wchip_list[chip]);
@@ -3244,22 +3282,22 @@ static void minion_flush_work(struct cgpu_info *minioncgpu)
 	task = minioninfo->task_list->tail;
 	while (task) {
 		prev_task = task->prev;
-		if (DATAT(task)->address == WRITE_ADDR(MINION_QUE_0)) {
-			minioninfo->chip_status[DATAT(task)->chip].quework--;
+		if (DATA_TASK(task)->address == WRITE_ADDR(MINION_QUE_0)) {
+			minioninfo->chip_status[DATA_TASK(task)->chip].quework--;
 #if MINION_SHOW_IO
 			applog(IOCTRL_LOG, "QueFlush chip %d cw=%u rw=%u qw-1=%u",
-					   (int)DATAT(task)->chip,
-					   minioninfo->chip_status[DATAT(task)->chip].chipwork,
-					   minioninfo->chip_status[DATAT(task)->chip].realwork,
-					   minioninfo->chip_status[DATAT(task)->chip].quework);
+					   (int)DATA_TASK(task)->chip,
+					   minioninfo->chip_status[DATA_TASK(task)->chip].chipwork,
+					   minioninfo->chip_status[DATA_TASK(task)->chip].realwork,
+					   minioninfo->chip_status[DATA_TASK(task)->chip].quework);
 #endif
-			witem = DATAT(task)->witem;
-			k_unlink_item(minioninfo->wque_list[DATAT(task)->chip], witem);
+			witem = DATA_TASK(task)->witem;
+			k_unlink_item(minioninfo->wque_list[DATA_TASK(task)->chip], witem);
 			minioninfo->wque_flushed++;
-			if (DATAW(witem)->rolled)
-				free_work(DATAW(witem)->work);
+			if (DATA_WORK(witem)->rolled)
+				free_work(DATA_WORK(witem)->work);
 			else
-				work_completed(minioncgpu, DATAW(witem)->work);
+				work_completed(minioncgpu, DATA_WORK(witem)->work);
 			k_free_head(minioninfo->wfree_list, witem);
 			k_unlink_item(minioninfo->task_list, task);
 			k_free_head(minioninfo->tfree_list, task);
@@ -3270,18 +3308,18 @@ static void minion_flush_work(struct cgpu_info *minioncgpu)
 		if (minioninfo->has_chip[i]) {
 			// TODO: consider sending it now rather than adding to the task list?
 			task = k_unlink_head(minioninfo->tfree_list);
-			DATAT(task)->tid = ++(minioninfo->next_tid);
-			DATAT(task)->chip = i;
-			DATAT(task)->write = true;
-			DATAT(task)->address = MINION_SYS_RSTN_CTL;
-			DATAT(task)->task_id = 0; // ignored
-			DATAT(task)->wsiz = MINION_SYS_SIZ;
-			DATAT(task)->rsiz = 0;
-			DATAT(task)->wbuf[0] = SYS_RSTN_CTL_FLUSH;
-			DATAT(task)->wbuf[1] = 0;
-			DATAT(task)->wbuf[2] = 0;
-			DATAT(task)->wbuf[3] = 0;
-			DATAT(task)->urgent = true;
+			DATA_TASK(task)->tid = ++(minioninfo->next_tid);
+			DATA_TASK(task)->chip = i;
+			DATA_TASK(task)->write = true;
+			DATA_TASK(task)->address = MINION_SYS_RSTN_CTL;
+			DATA_TASK(task)->task_id = 0; // ignored
+			DATA_TASK(task)->wsiz = MINION_SYS_SIZ;
+			DATA_TASK(task)->rsiz = 0;
+			DATA_TASK(task)->wbuf[0] = SYS_RSTN_CTL_FLUSH;
+			DATA_TASK(task)->wbuf[1] = 0;
+			DATA_TASK(task)->wbuf[2] = 0;
+			DATA_TASK(task)->wbuf[3] = 0;
+			DATA_TASK(task)->urgent = true;
 			k_add_head(minioninfo->task_list, task);
 		}
 	}
@@ -3296,10 +3334,10 @@ static void minion_flush_work(struct cgpu_info *minioncgpu)
 		// mark complete all stale unused work (oldest first)
 		prev_unused = minioninfo->wstale_list->tail;
 		while (prev_unused) {
-			if (DATAW(prev_unused)->rolled)
-				free_work(DATAW(prev_unused)->work);
+			if (DATA_WORK(prev_unused)->rolled)
+				free_work(DATA_WORK(prev_unused)->work);
 			else
-				work_completed(minioncgpu, DATAW(prev_unused)->work);
+				work_completed(minioncgpu, DATA_WORK(prev_unused)->work);
 			prev_unused = prev_unused->prev;
 		}
 
@@ -3329,30 +3367,30 @@ static void sys_chip_sta(struct cgpu_info *minioncgpu, int chip)
 
 			K_WLOCK(minioninfo->tfree_list);
 			item = k_unlink_head(minioninfo->tfree_list);
-			DATAT(item)->tid = ++(minioninfo->next_tid);
+			DATA_TASK(item)->tid = ++(minioninfo->next_tid);
 			K_WUNLOCK(minioninfo->tfree_list);
 
-			DATAT(item)->chip = chip;
-			DATAT(item)->write = false;
-			DATAT(item)->address = READ_ADDR(MINION_SYS_CHIP_STA);
-			DATAT(item)->task_id = 0;
-			DATAT(item)->wsiz = 0;
-			DATAT(item)->rsiz = MINION_SYS_SIZ;
-			DATAT(item)->urgent = false;
+			DATA_TASK(item)->chip = chip;
+			DATA_TASK(item)->write = false;
+			DATA_TASK(item)->address = READ_ADDR(MINION_SYS_CHIP_STA);
+			DATA_TASK(item)->task_id = 0;
+			DATA_TASK(item)->wsiz = 0;
+			DATA_TASK(item)->rsiz = MINION_SYS_SIZ;
+			DATA_TASK(item)->urgent = false;
 
 			K_WLOCK(minioninfo->task_list);
 			k_add_head(minioninfo->task_list, item);
 			item = k_unlink_head(minioninfo->tfree_list);
-			DATAT(item)->tid = ++(minioninfo->next_tid);
+			DATA_TASK(item)->tid = ++(minioninfo->next_tid);
 			K_WUNLOCK(minioninfo->task_list);
 
-			DATAT(item)->chip = chip;
-			DATAT(item)->write = false;
-			DATAT(item)->address = READ_ADDR(MINION_SYS_IDLE_CNT);
-			DATAT(item)->task_id = 0;
-			DATAT(item)->wsiz = 0;
-			DATAT(item)->rsiz = MINION_SYS_SIZ;
-			DATAT(item)->urgent = false;
+			DATA_TASK(item)->chip = chip;
+			DATA_TASK(item)->write = false;
+			DATA_TASK(item)->address = READ_ADDR(MINION_SYS_IDLE_CNT);
+			DATA_TASK(item)->task_id = 0;
+			DATA_TASK(item)->wsiz = 0;
+			DATA_TASK(item)->rsiz = MINION_SYS_SIZ;
+			DATA_TASK(item)->urgent = false;
 
 			K_WLOCK(minioninfo->task_list);
 			k_add_head(minioninfo->task_list, item);
@@ -3363,31 +3401,31 @@ static void sys_chip_sta(struct cgpu_info *minioncgpu, int chip)
 				// Ena
 				K_WLOCK(minioninfo->tfree_list);
 				item = k_unlink_head(minioninfo->tfree_list);
-				DATAT(item)->tid = ++(minioninfo->next_tid);
+				DATA_TASK(item)->tid = ++(minioninfo->next_tid);
 				K_WUNLOCK(minioninfo->tfree_list);
 
-				DATAT(item)->chip = chip;
-				DATAT(item)->write = false;
-				DATAT(item)->address = READ_ADDR(MINION_CORE_ENA0_31 + rep);
-				DATAT(item)->task_id = 0;
-				DATAT(item)->wsiz = 0;
-				DATAT(item)->rsiz = MINION_SYS_SIZ;
-				DATAT(item)->urgent = false;
+				DATA_TASK(item)->chip = chip;
+				DATA_TASK(item)->write = false;
+				DATA_TASK(item)->address = READ_ADDR(MINION_CORE_ENA0_31 + rep);
+				DATA_TASK(item)->task_id = 0;
+				DATA_TASK(item)->wsiz = 0;
+				DATA_TASK(item)->rsiz = MINION_SYS_SIZ;
+				DATA_TASK(item)->urgent = false;
 
 				K_WLOCK(minioninfo->task_list);
 				k_add_head(minioninfo->task_list, item);
 				// Act
 				item = k_unlink_head(minioninfo->tfree_list);
-				DATAT(item)->tid = ++(minioninfo->next_tid);
+				DATA_TASK(item)->tid = ++(minioninfo->next_tid);
 				K_WUNLOCK(minioninfo->task_list);
 
-				DATAT(item)->chip = chip;
-				DATAT(item)->write = false;
-				DATAT(item)->address = READ_ADDR(MINION_CORE_ACT0_31 + rep);
-				DATAT(item)->task_id = 0;
-				DATAT(item)->wsiz = 0;
-				DATAT(item)->rsiz = MINION_SYS_SIZ;
-				DATAT(item)->urgent = false;
+				DATA_TASK(item)->chip = chip;
+				DATA_TASK(item)->write = false;
+				DATA_TASK(item)->address = READ_ADDR(MINION_CORE_ACT0_31 + rep);
+				DATA_TASK(item)->task_id = 0;
+				DATA_TASK(item)->wsiz = 0;
+				DATA_TASK(item)->rsiz = MINION_SYS_SIZ;
+				DATA_TASK(item)->urgent = false;
 
 				K_WLOCK(minioninfo->task_list);
 				k_add_head(minioninfo->task_list, item);
@@ -3405,36 +3443,36 @@ static void new_work_task(struct cgpu_info *minioncgpu, K_ITEM *witem, int chip,
 
 	K_WLOCK(minioninfo->tfree_list);
 	item = k_unlink_head(minioninfo->tfree_list);
-	DATAT(item)->tid = ++(minioninfo->next_tid);
+	DATA_TASK(item)->tid = ++(minioninfo->next_tid);
 	K_WUNLOCK(minioninfo->tfree_list);
 
-	DATAT(item)->chip = chip;
-	DATAT(item)->write = true;
-	DATAT(item)->address = MINION_QUE_0;
+	DATA_TASK(item)->chip = chip;
+	DATA_TASK(item)->write = true;
+	DATA_TASK(item)->address = MINION_QUE_0;
 
 	// if threaded access to new_work_task() is added, this will need locking
 	// Don't use task_id 0 so that we can ignore all '0' work replies
 	// ... and report them as errors
 	if (minioninfo->next_task_id == 0)
 		minioninfo->next_task_id = 1;
-	DATAT(item)->task_id = minioninfo->next_task_id;
-	DATAW(witem)->task_id = minioninfo->next_task_id;
+	DATA_TASK(item)->task_id = minioninfo->next_task_id;
+	DATA_WORK(witem)->task_id = minioninfo->next_task_id;
 	minioninfo->next_task_id = (minioninfo->next_task_id + 1) & MINION_MAX_TASK_ID;
 
-	DATAT(item)->urgent = urgent;
-	DATAT(item)->work_state = state;
-	DATAT(item)->work = DATAW(witem)->work;
-	DATAT(item)->witem = witem;
+	DATA_TASK(item)->urgent = urgent;
+	DATA_TASK(item)->work_state = state;
+	DATA_TASK(item)->work = DATA_WORK(witem)->work;
+	DATA_TASK(item)->witem = witem;
 
-	que = (struct minion_que *)&(DATAT(item)->wbuf[0]);
-	que->task_id[0] = DATAT(item)->task_id & 0xff;
-	que->task_id[1] = (DATAT(item)->task_id & 0xff00) >> 8;
+	que = (struct minion_que *)&(DATA_TASK(item)->wbuf[0]);
+	que->task_id[0] = DATA_TASK(item)->task_id & 0xff;
+	que->task_id[1] = (DATA_TASK(item)->task_id & 0xff00) >> 8;
 
-	memcpy(&(que->midstate[0]), &(DATAW(witem)->work->midstate[0]), MIDSTATE_BYTES);
-	memcpy(&(que->merkle7[0]), &(DATAW(witem)->work->data[MERKLE7_OFFSET]), MERKLE_BYTES);
+	memcpy(&(que->midstate[0]), &(DATA_WORK(witem)->work->midstate[0]), MIDSTATE_BYTES);
+	memcpy(&(que->merkle7[0]), &(DATA_WORK(witem)->work->data[MERKLE7_OFFSET]), MERKLE_BYTES);
 
-	DATAT(item)->wsiz = (int)sizeof(*que);
-	DATAT(item)->rsiz = 0;
+	DATA_TASK(item)->wsiz = (int)sizeof(*que);
+	DATA_TASK(item)->rsiz = 0;
 
 	K_WLOCK(minioninfo->wque_list[chip]);
 	k_add_head(minioninfo->wque_list[chip], witem);
@@ -3473,7 +3511,7 @@ static K_ITEM *next_work(struct minion_info *minioninfo)
 	K_WUNLOCK(minioninfo->wwork_list);
 	if (item) {
 		cgtime(&now);
-		howlong = tdiff(&now, &(DATAW(item)->created));
+		howlong = tdiff(&now, &(DATA_WORK(item)->created));
 		minioninfo->que_work++;
 		minioninfo->que_time += howlong;
 		if (minioninfo->que_min == 0 || minioninfo->que_min > howlong)
@@ -3497,7 +3535,7 @@ static void minion_do_work(struct cgpu_info *minioncgpu)
 {
 	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
 	int count, chip, j, lowcount;
-	TITEM fifo_task;
+	TASK_ITEM fifo_task;
 	uint8_t state, cmd;
 	K_ITEM *item;
 #if ENABLE_INT_NONO
@@ -3607,7 +3645,7 @@ static void minion_do_work(struct cgpu_info *minioncgpu)
 								applog(MINION_LOG, "%s%i: 0 task 0x%04x in chip %d list",
 										   minioncgpu->drv->name,
 										   minioncgpu->device_id,
-										   DATAW(item)->task_id, chip);
+										   DATA_WORK(item)->task_id, chip);
 							} else {
 								applog(LOG_ERR, "%s%i: chip %d urgent empty work list",
 										minioncgpu->drv->name,
@@ -3637,7 +3675,7 @@ static void minion_do_work(struct cgpu_info *minioncgpu)
 									applog(MINION_LOG, "%s%i: 1 task 0x%04x in chip %d list",
 											   minioncgpu->drv->name,
 											   minioncgpu->device_id,
-											   DATAW(item)->task_id, chip);
+											   DATA_WORK(item)->task_id, chip);
 								} else {
 									applog(LOG_ERR, "%s%i: chip %d non-urgent lo "
 											   "empty work list (count=%d)",
@@ -3658,7 +3696,7 @@ static void minion_do_work(struct cgpu_info *minioncgpu)
 									applog(MINION_LOG, "%s%i: 2 task 0x%04x in chip %d list",
 											   minioncgpu->drv->name,
 											   minioncgpu->device_id,
-											   DATAW(item)->task_id, chip);
+											   DATA_WORK(item)->task_id, chip);
 								} else {
 									applog(LOG_DEBUG, "%s%i: chip %d non-urgent hi "
 											   "empty work list (count=%d)",
@@ -3682,18 +3720,18 @@ static void minion_do_work(struct cgpu_info *minioncgpu)
 		// Clear CMD interrupt since we've now sent more
 		K_WLOCK(minioninfo->tfree_list);
 		task = k_unlink_head(minioninfo->tfree_list);
-		DATAT(task)->tid = ++(minioninfo->next_tid);
-		DATAT(task)->chip = 0; // ignored
-		DATAT(task)->write = true;
-		DATAT(task)->address = MINION_SYS_INT_CLR;
-		DATAT(task)->task_id = 0; // ignored
-		DATAT(task)->wsiz = MINION_SYS_SIZ;
-		DATAT(task)->rsiz = 0;
-		DATAT(task)->wbuf[0] = MINION_CMD_INT;
-		DATAT(task)->wbuf[1] = 0;
-		DATAT(task)->wbuf[2] = 0;
-		DATAT(task)->wbuf[3] = 0;
-		DATAT(task)->urgent = false;
+		DATA_TASK(task)->tid = ++(minioninfo->next_tid);
+		DATA_TASK(task)->chip = 0; // ignored
+		DATA_TASK(task)->write = true;
+		DATA_TASK(task)->address = MINION_SYS_INT_CLR;
+		DATA_TASK(task)->task_id = 0; // ignored
+		DATA_TASK(task)->wsiz = MINION_SYS_SIZ;
+		DATA_TASK(task)->rsiz = 0;
+		DATA_TASK(task)->wbuf[0] = MINION_CMD_INT;
+		DATA_TASK(task)->wbuf[1] = 0;
+		DATA_TASK(task)->wbuf[2] = 0;
+		DATA_TASK(task)->wbuf[3] = 0;
+		DATA_TASK(task)->urgent = false;
 		k_add_head(minioninfo->task_list, task);
 		K_WUNLOCK(minioninfo->tfree_list);
 	}
@@ -3883,7 +3921,7 @@ static void chip_report(struct cgpu_info *minioncgpu)
 					else {
 						ghs = 0xffffffffull * (minioninfo->hchip_list[chip]->count - 1);
 						ghs /= 1000000000.0;
-						ghs /= tdiff(&now, &(DATAH(minioninfo->hchip_list[chip]->tail)->when));
+						ghs /= tdiff(&now, &(DATA_HIST(minioninfo->hchip_list[chip]->tail)->when));
 					}
 					res_err_count = minioninfo->res_err_count[chip];
 					minioninfo->res_err_count[chip] = 0;
@@ -3919,7 +3957,7 @@ static void chip_report(struct cgpu_info *minioncgpu)
 				else {
 					ghs = 0xffffffffull * (minioninfo->hchip_list[chip]->count - 1);
 					ghs /= 1000000000.0;
-					ghs /= tdiff(&now, &(DATAH(minioninfo->hchip_list[chip]->tail)->when));
+					ghs /= tdiff(&now, &(DATA_HIST(minioninfo->hchip_list[chip]->tail)->when));
 				}
 				expect = (double)(minioninfo->init_freq[chip]) *
 					 MINION_RESET_PERCENT / 1000.0;