Commit c2d4d12f5e1520be088112a04df1e642ff0b60f4

ckolivas 2014-01-24T10:21:19

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
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
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
diff --git a/API-README b/API-README
index b5497d3..5d27855 100644
--- a/API-README
+++ b/API-README
@@ -453,6 +453,12 @@ miner.php - an example web page to access the API
 Feature Changelog for external applications using the API:
 
 
+API V3.0 (cgminer v3.9.1)
+
+Allow unlimited size replies
+
+---------
+
 API V2.0 (cgminer v3.8.0)
 
 Removed all GPU related commands and information from the replies
diff --git a/ASIC-README b/ASIC-README
index b206d43..2f881b5 100644
--- a/ASIC-README
+++ b/ASIC-README
@@ -330,6 +330,12 @@ temperature just below an optimal target. This option allows you to change the
 target temperature. When actively cooled below this, the devices will run at
 maximum speed.
 
+--nfu-bits <arg>    Set nanofury bits for overclocking range 32-63 (default: 50)
+
+Cgminer by default sets the clockspeed on nanofury devices to the highest that
+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
 
diff --git a/Makefile.am b/Makefile.am
index af24c49..3a20646 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -43,6 +43,8 @@ cgminer_SOURCES	+= elist.h miner.h compat.h bench_block.h	\
 
 cgminer_SOURCES	+= logging.c
 
+cgminer_SOURCES	+= klist.h klist.c
+
 if NEED_FPGAUTILS
 cgminer_SOURCES += fpgautils.c fpgautils.h
 endif
diff --git a/api.c b/api.c
index c0055bd..546d3a6 100644
--- a/api.c
+++ b/api.c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011-2013 Andrew Smith
+ * Copyright 2011-2014 Andrew Smith
  * Copyright 2011-2013 Con Kolivas
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -24,6 +24,7 @@
 #include "compat.h"
 #include "miner.h"
 #include "util.h"
+#include "klist.h"
 
 #if defined(USE_BFLSC) || defined(USE_AVALON) || defined(USE_HASHFAST) || defined(USE_BITFURY) || defined(USE_KLONDIKE) || defined(USE_KNC) || defined(USE_BAB) || defined(USE_DRILLBIT) || defined(USE_MINION)
 #define HAVE_AN_ASIC 1
@@ -131,7 +132,7 @@ static const char *COMMA = ",";
 static const char SEPARATOR = '|';
 #define SEPSTR "|"
 
-static const char *APIVERSION = "2.0";
+static const char *APIVERSION = "3.0";
 static const char *DEAD = "Dead";
 static const char *SICK = "Sick";
 static const char *NOSTART = "NoStart";
@@ -156,6 +157,9 @@ static const char *DEVICECODE = ""
 #ifdef USE_AVALON
 			"AVA "
 #endif
+#ifdef USE_BAB
+			"BaB "
+#endif
 #ifdef USE_BFLSC
 			"BAS "
 #endif
@@ -165,24 +169,18 @@ static const char *DEVICECODE = ""
 #ifdef USE_BITFURY
 			"BFU "
 #endif
-#ifdef USE_HASHFAST
-			"HFA "
-#endif
 #ifdef USE_DRILLBIT
 			"DRB "
 #endif
+#ifdef USE_HASHFAST
+			"HFA "
+#endif
 #ifdef USE_ICARUS
 			"ICA "
 #endif
-#ifdef USE_MODMINER
-			"MMQ "
-#endif
 #ifdef USE_KNC
 			"KnC "
 #endif
-#ifdef USE_BAB
-			"BaB "
-#endif
 #ifdef USE_MINION
 			"MBA "
 #endif
@@ -614,7 +612,6 @@ struct io_data {
 	char *ptr;
 	char *cur;
 	bool sock;
-	bool full;
 	bool close;
 };
 
@@ -626,14 +623,31 @@ struct io_list {
 
 static struct io_list *io_head = NULL;
 
+#define SOCKBUFALLOCSIZ 65536
+
 #define io_new(init) _io_new(init, false)
-#define sock_io_new() _io_new(SOCKBUFSIZ, true)
+#define sock_io_new() _io_new(SOCKBUFALLOCSIZ, true)
+
+#define ALLOC_SBITEMS 2
+#define LIMIT_SBITEMS 0
+
+typedef struct sbitem {
+	char *buf;
+	size_t siz;
+	size_t tot;
+} SBITEM;
+
+// Size to grow tot if exceeded
+#define SBEXTEND 4096
+
+#define DATASB(_item) ((SBITEM *)(_item->data))
+
+static K_LIST *strbufs;
 
 static void io_reinit(struct io_data *io_data)
 {
 	io_data->cur = io_data->ptr;
 	*(io_data->ptr) = '\0';
-	io_data->full = false;
 	io_data->close = false;
 }
 
@@ -670,29 +684,16 @@ static bool io_add(struct io_data *io_data, char *buf)
 {
 	size_t len, dif, tot;
 
-	if (io_data->full)
-		return false;
-
 	len = strlen(buf);
 	dif = io_data->cur - io_data->ptr;
-	tot = len + 1 + dif;
+	// send will always have enough space to add the JSON
+	tot = len + 1 + dif + sizeof(JSON_CLOSE) + sizeof(JSON_END);
 
 	if (tot > io_data->siz) {
-		size_t new = io_data->siz * 2;
+		size_t new = io_data->siz + (2 * SOCKBUFALLOCSIZ);
 
 		if (new < tot)
-			new = tot * 2;
-
-		if (io_data->sock) {
-			if (new > SOCKBUFSIZ) {
-				if (tot > SOCKBUFSIZ) {
-					io_data->full = true;
-					return false;
-				}
-
-				new = SOCKBUFSIZ;
-			}
-		}
+			new = (2 + (size_t)((float)tot / (float)SOCKBUFALLOCSIZ)) * SOCKBUFALLOCSIZ;
 
 		io_data->ptr = realloc(io_data->ptr, new);
 		io_data->cur = io_data->ptr + dif;
@@ -766,8 +767,10 @@ static char *escape_string(char *str, bool isjson)
 		return str;
 
 	buf = malloc(strlen(str) + count + 1);
-	if (unlikely(!buf))
-		quit(1, "Failed to malloc escape buf");
+	if (unlikely(!buf)) {
+		quithere(1, "Failed to malloc escape buf %d",
+			    (int)(strlen(str) + count + 1));
+	}
 
 	ptr = buf;
 	while (*str)
@@ -838,8 +841,7 @@ static struct api_data *api_add_data_full(struct api_data *root, char *name, enu
 		root = api_data;
 		root->prev = root;
 		root->next = root;
-	}
-	else {
+	} else {
 		api_data->prev = root->prev;
 		root->prev = api_data;
 		api_data->next = root;
@@ -917,6 +919,7 @@ static struct api_data *api_add_data_full(struct api_data *root, char *name, enu
 				break;
 			case API_VOLTS:
 			case API_TEMP:
+			case API_AVG:
 				api_data->data = (void *)malloc(sizeof(float));
 				*((float *)(api_data->data)) = *((float *)data);
 				break;
@@ -1046,114 +1049,171 @@ struct api_data *api_add_percent(struct api_data *root, char *name, double *data
 	return api_add_data_full(root, name, API_PERCENT, (void *)data, copy_data);
 }
 
-static struct api_data *print_data(struct api_data *root, char *buf, bool isjson, bool precom)
+struct api_data *api_add_avg(struct api_data *root, char *name, float *data, bool copy_data)
+{
+	return api_add_data_full(root, name, API_AVG, (void *)data, copy_data);
+}
+
+static void add_item_buf(K_ITEM *item, const char *str)
+{
+	size_t old_siz, new_siz, siz, ext;
+	char *buf;
+
+	buf = DATASB(item)->buf;
+	siz = (size_t)strlen(str);
+
+	old_siz = DATASB(item)->siz;
+	new_siz = old_siz + siz + 1; // include '\0'
+	if (DATASB(item)->tot < new_siz) {
+		ext = (siz + 1) + SBEXTEND - ((siz + 1) % SBEXTEND);
+		DATASB(item)->buf = buf = realloc(DATASB(item)->buf, DATASB(item)->tot + ext);
+		if (!buf) {
+			quithere(1, "OOM buf siz=%d tot=%d ext=%d",
+				    (int)siz, (int)(DATASB(item)->tot), (int)ext);
+		}
+		DATASB(item)->tot += ext;
+	}
+	memcpy(buf + old_siz, str, siz + 1);
+	DATASB(item)->siz += siz;
+}
+
+static struct api_data *print_data(struct io_data *io_data, struct api_data *root, bool isjson, bool precom)
 {
+	// N.B. strings don't use this buffer so 64 is enough (for now)
+	char buf[64];
 	struct api_data *tmp;
-	bool first = true;
+	bool done, first = true;
 	char *original, *escape;
-	char *quote;
+	K_ITEM *item;
 
-	*buf = '\0';
+	K_WLOCK(strbufs);
+	item = k_unlink_head(strbufs);
+	K_WUNLOCK(strbufs);
 
-	if (precom) {
-		*(buf++) = *COMMA;
-		*buf = '\0';
-	}
+	DATASB(item)->siz = 0;
 
-	if (isjson) {
-		strcpy(buf, JSON0);
-		buf = strchr(buf, '\0');
-		quote = JSON1;
-	} else
-		quote = (char *)BLANK;
+	if (precom)
+		add_item_buf(item, COMMA);
+
+	if (isjson)
+		add_item_buf(item, JSON0);
 
 	while (root) {
 		if (!first)
-			*(buf++) = *COMMA;
+			add_item_buf(item, COMMA);
 		else
 			first = false;
 
-		sprintf(buf, "%s%s%s%s", quote, root->name, quote, isjson ? ":" : "=");
+		if (isjson)
+			add_item_buf(item, JSON1);
+
+		add_item_buf(item, root->name);
 
-		buf = strchr(buf, '\0');
+		if (isjson)
+			add_item_buf(item, JSON1);
 
+		if (isjson)
+			add_item_buf(item, ":");
+		else
+			add_item_buf(item, "=");
+
+		first = false;
+
+		done = false;
 		switch(root->type) {
 			case API_STRING:
 			case API_CONST:
-				sprintf(buf, "%s%s%s", quote, (char *)(root->data), quote);
+				if (isjson)
+					add_item_buf(item, JSON1);
+				add_item_buf(item, (char *)(root->data));
+				if (isjson)
+					add_item_buf(item, JSON1);
+				done = true;
 				break;
 			case API_ESCAPE:
 				original = (char *)(root->data);
 				escape = escape_string((char *)(root->data), isjson);
-				sprintf(buf, "%s%s%s", quote, escape, quote);
+				if (isjson)
+					add_item_buf(item, JSON1);
+				add_item_buf(item, escape);
+				if (isjson)
+					add_item_buf(item, JSON1);
 				if (escape != original)
 					free(escape);
+				done = true;
 				break;
 			case API_UINT8:
-				sprintf(buf, "%u", *(uint8_t *)root->data);
+				snprintf(buf, sizeof(buf), "%u", *(uint8_t *)root->data);
 				break;
 			case API_UINT16:
-				sprintf(buf, "%u", *(uint16_t *)root->data);
+				snprintf(buf, sizeof(buf), "%u", *(uint16_t *)root->data);
 				break;
 			case API_INT:
-				sprintf(buf, "%d", *((int *)(root->data)));
+				snprintf(buf, sizeof(buf), "%d", *((int *)(root->data)));
 				break;
 			case API_UINT:
-				sprintf(buf, "%u", *((unsigned int *)(root->data)));
+				snprintf(buf, sizeof(buf), "%u", *((unsigned int *)(root->data)));
 				break;
 			case API_UINT32:
-				sprintf(buf, "%"PRIu32, *((uint32_t *)(root->data)));
+				snprintf(buf, sizeof(buf), "%"PRIu32, *((uint32_t *)(root->data)));
 				break;
 			case API_UINT64:
-				sprintf(buf, "%"PRIu64, *((uint64_t *)(root->data)));
+				snprintf(buf, sizeof(buf), "%"PRIu64, *((uint64_t *)(root->data)));
 				break;
 			case API_TIME:
-				sprintf(buf, "%lu", *((unsigned long *)(root->data)));
+				snprintf(buf, sizeof(buf), "%lu", *((unsigned long *)(root->data)));
 				break;
 			case API_DOUBLE:
-				sprintf(buf, "%f", *((double *)(root->data)));
+				snprintf(buf, sizeof(buf), "%f", *((double *)(root->data)));
 				break;
 			case API_ELAPSED:
-				sprintf(buf, "%.0f", *((double *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.0f", *((double *)(root->data)));
 				break;
 			case API_UTILITY:
 			case API_FREQ:
 			case API_MHS:
-				sprintf(buf, "%.2f", *((double *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.2f", *((double *)(root->data)));
 				break;
 			case API_VOLTS:
-				sprintf(buf, "%.3f", *((float *)(root->data)));
+			case API_AVG:
+				snprintf(buf, sizeof(buf), "%.3f", *((float *)(root->data)));
 				break;
 			case API_MHTOTAL:
-				sprintf(buf, "%.4f", *((double *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.4f", *((double *)(root->data)));
 				break;
 			case API_HS:
-				sprintf(buf, "%.15f", *((double *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.15f", *((double *)(root->data)));
 				break;
 			case API_DIFF:
-				sprintf(buf, "%.8f", *((double *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.8f", *((double *)(root->data)));
 				break;
 			case API_BOOL:
-				sprintf(buf, "%s", *((bool *)(root->data)) ? TRUESTR : FALSESTR);
+				snprintf(buf, sizeof(buf), "%s", *((bool *)(root->data)) ? TRUESTR : FALSESTR);
 				break;
 			case API_TIMEVAL:
-				sprintf(buf, "%ld.%06ld",
+				snprintf(buf, sizeof(buf), "%ld.%06ld",
 					((struct timeval *)(root->data))->tv_sec,
 					((struct timeval *)(root->data))->tv_usec);
 				break;
 			case API_TEMP:
-				sprintf(buf, "%.2f", *((float *)(root->data)));
+				snprintf(buf, sizeof(buf), "%.2f", *((float *)(root->data)));
 				break;
 			case API_PERCENT:
-				sprintf(buf, "%.4f", *((double *)(root->data)) * 100.0);
+				snprintf(buf, sizeof(buf), "%.4f", *((double *)(root->data)) * 100.0);
 				break;
 			default:
 				applog(LOG_ERR, "API: unknown2 data type %d ignored", root->type);
-				sprintf(buf, "%s%s%s", quote, UNKNOWN, quote);
+				if (isjson)
+					add_item_buf(item, JSON1);
+				add_item_buf(item, UNKNOWN);
+				if (isjson)
+					add_item_buf(item, JSON1);
+				done = true;
 				break;
 		}
 
-		buf = strchr(buf, '\0');
+		if (!done)
+			add_item_buf(item, buf);
 
 		free(root->name);
 		if (root->data_was_malloc)
@@ -1171,7 +1231,16 @@ static struct api_data *print_data(struct api_data *root, char *buf, bool isjson
 		}
 	}
 
-	strcpy(buf, isjson ? JSON5 : SEPSTR);
+	if (isjson)
+		add_item_buf(item, JSON5);
+	else
+		add_item_buf(item, SEPSTR);
+
+	io_add(io_data, DATASB(item)->buf);
+
+	K_WLOCK(strbufs);
+	k_add_head(strbufs, item);
+	K_WUNLOCK(strbufs);
 
 	return root;
 }
@@ -1258,7 +1327,6 @@ static void message(struct io_data *io_data, int messageid, int paramid, char *p
 {
 	struct api_data *root = NULL;
 	char buf[TMPBUFSIZ];
-	char buf2[TMPBUFSIZ];
 	char severity[2];
 #ifdef HAVE_AN_ASIC
 	int asc;
@@ -1363,8 +1431,7 @@ static void message(struct io_data *io_data, int messageid, int paramid, char *p
 			root = api_add_escape(root, "Msg", buf, false);
 			root = api_add_escape(root, "Description", opt_api_description, false);
 
-			root = print_data(root, buf2, isjson, false);
-			io_add(io_data, buf2);
+			root = print_data(io_data, root, isjson, false);
 			if (isjson)
 				io_add(io_data, JSON_CLOSE);
 			return;
@@ -1379,8 +1446,7 @@ static void message(struct io_data *io_data, int messageid, int paramid, char *p
 	root = api_add_escape(root, "Msg", buf, false);
 	root = api_add_escape(root, "Description", opt_api_description, false);
 
-	root = print_data(root, buf2, isjson, false);
-	io_add(io_data, buf2);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson)
 		io_add(io_data, JSON_CLOSE);
 }
@@ -1785,7 +1851,6 @@ static void lockstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __ma
 static void apiversion(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 
 	message(io_data, MSG_VERSION, 0, NULL, isjson);
@@ -1794,8 +1859,7 @@ static void apiversion(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
 	root = api_add_string(root, "CGMiner", VERSION, false);
 	root = api_add_const(root, "API", APIVERSION, false);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -1803,7 +1867,6 @@ static void apiversion(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
 static void minerconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 	int asccount = 0;
 	int pgacount = 0;
@@ -1839,8 +1902,7 @@ static void minerconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __
 	root = api_add_const(root, "Hotplug", NONE, false);
 #endif
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -1867,7 +1929,6 @@ static const char *status2str(enum alive status)
 static void ascstatus(struct io_data *io_data, int asc, bool isjson, bool precom)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	char *enabled;
 	char *status;
 	int numasc = numascs();
@@ -1937,8 +1998,7 @@ static void ascstatus(struct io_data *io_data, int asc, bool isjson, bool precom
 		root = api_add_percent(root, "Device Rejected%", &rejp, false);
 		root = api_add_elapsed(root, "Device Elapsed", &(dev_runtime), false);
 
-		root = print_data(root, buf, isjson, precom);
-		io_add(io_data, buf);
+		root = print_data(io_data, root, isjson, precom);
 	}
 }
 #endif
@@ -1947,7 +2007,6 @@ static void ascstatus(struct io_data *io_data, int asc, bool isjson, bool precom
 static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	char *enabled;
 	char *status;
 	int numpga = numpgas();
@@ -2024,8 +2083,7 @@ static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom
 		root = api_add_percent(root, "Device Rejected%", &rejp, false);
 		root = api_add_elapsed(root, "Device Elapsed", &(dev_runtime), false);
 
-		root = print_data(root, buf, isjson, precom);
-		io_add(io_data, buf);
+		root = print_data(io_data, root, isjson, precom);
 	}
 }
 #endif
@@ -2269,7 +2327,6 @@ static void pgaidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, ch
 static void poolstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open = false;
 	char *status, *lp;
 	int i;
@@ -2356,8 +2413,7 @@ static void poolstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
 				(double)(pool->diff_stale) / (double)(pool->diff_accepted + pool->diff_rejected + pool->diff_stale) : 0;
 		root = api_add_percent(root, "Pool Stale%", &stalep, false);
 
-		root = print_data(root, buf, isjson, isjson && (i > 0));
-		io_add(io_data, buf);
+		root = print_data(io_data, root, isjson, isjson && (i > 0));
 	}
 
 	if (isjson && io_open)
@@ -2367,7 +2423,6 @@ static void poolstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
 static void summary(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 	double utility, mhs, work_utility;
 
@@ -2419,8 +2474,7 @@ static void summary(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __mayb
 
 	mutex_unlock(&hash_lock);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -2428,7 +2482,6 @@ static void summary(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __mayb
 static void pgacount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 	int count = 0;
 
@@ -2441,8 +2494,7 @@ static void pgacount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 
 	root = api_add_int(root, "Count", &count, false);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -2826,7 +2878,6 @@ void privileged(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_un
 void notifystatus(struct io_data *io_data, int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	char *reason;
 
 	if (cgpu->device_last_not_well == 0)
@@ -2884,8 +2935,7 @@ void notifystatus(struct io_data *io_data, int device, struct cgpu_info *cgpu, b
 	root = api_add_int(root, "*Dev Comms Error", &(cgpu->dev_comms_error_count), false);
 	root = api_add_int(root, "*Dev Throttle", &(cgpu->dev_throttle_count), false);
 
-	root = print_data(root, buf, isjson, isjson && (device > 0));
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, isjson && (device > 0));
 }
 
 static void notify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
@@ -2916,7 +2966,6 @@ static void notify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe
 static void devdetails(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open = false;
 	struct cgpu_info *cgpu;
 	int i;
@@ -2942,8 +2991,7 @@ static void devdetails(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
 		root = api_add_const(root, "Model", cgpu->name ? : BLANK, false);
 		root = api_add_const(root, "Device Path", cgpu->device_path ? : BLANK, false);
 
-		root = print_data(root, buf, isjson, isjson && (i > 0));
-		io_add(io_data, buf);
+		root = print_data(io_data, root, isjson, isjson && (i > 0));
 	}
 
 	if (isjson && io_open)
@@ -2984,7 +3032,6 @@ void dosave(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, b
 static int itemstats(struct io_data *io_data, int i, char *id, struct cgminer_stats *stats, struct cgminer_pool_stats *pool_stats, struct api_data *extra, struct cgpu_info *cgpu, bool isjson)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 
 	root = api_add_int(root, "STATS", &i, false);
 	root = api_add_string(root, "ID", id, false);
@@ -3079,8 +3126,7 @@ static int itemstats(struct io_data *io_data, int i, char *id, struct cgminer_st
 #endif
 	}
 
-	root = print_data(root, buf, isjson, isjson && (i > 0));
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, isjson && (i > 0));
 
 	return ++i;
 }
@@ -3148,7 +3194,6 @@ static void failoveronly(struct io_data *io_data, __maybe_unused SOCKETTYPE c, c
 static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 
 	message(io_data, MSG_MINECOIN, 0, NULL, isjson);
@@ -3164,8 +3209,7 @@ static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 	root = api_add_bool(root, "LP", &have_longpoll, false);
 	root = api_add_diff(root, "Network Difficulty", &current_diff, true);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -3173,7 +3217,6 @@ static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 
 	if (param == NULL)
@@ -3243,8 +3286,7 @@ static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, cha
 	root = api_add_bool(root, "PerDevice", &want_per_device_stats, false);
 	root = api_add_bool(root, "WorkTime", &opt_worktime, false);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -3291,7 +3333,6 @@ static void usbstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 	struct api_data *root = NULL;
 
 #ifdef USE_USBUTILS
-	char buf[TMPBUFSIZ];
 	bool io_open = false;
 	int count = 0;
 
@@ -3309,16 +3350,14 @@ static void usbstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 	if (isjson)
 		io_open = io_add(io_data, COMSTR JSON_USBSTATS);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 
 	while (42) {
 		root = api_usb_stats(&count);
 		if (!root)
 			break;
 
-		root = print_data(root, buf, isjson, isjson);
-		io_add(io_data, buf);
+		root = print_data(io_data, root, isjson, isjson);
 	}
 
 	if (isjson && io_open)
@@ -3651,7 +3690,6 @@ static void ascidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, ch
 static void asccount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 	int count = 0;
 
@@ -3664,8 +3702,7 @@ static void asccount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __may
 
 	root = api_add_int(root, "Count", &count, false);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
@@ -3789,7 +3826,6 @@ struct CMDS {
 static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
 {
 	struct api_data *root = NULL;
-	char buf[TMPBUFSIZ];
 	bool io_open;
 	char cmdbuf[100];
 	bool found, access;
@@ -3820,36 +3856,31 @@ static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, c
 	root = api_add_const(root, "Exists", found ? YES : NO, false);
 	root = api_add_const(root, "Access", access ? YES : NO, false);
 
-	root = print_data(root, buf, isjson, false);
-	io_add(io_data, buf);
+	root = print_data(io_data, root, isjson, false);
 	if (isjson && io_open)
 		io_close(io_data);
 }
 
 static void send_result(struct io_data *io_data, SOCKETTYPE c, bool isjson)
 {
-	char buf[SOCKBUFSIZ + sizeof(JSON_CLOSE) + sizeof(JSON_END)];
-	int count, res, tosend, len, n;
+	int count, sendc, res, tosend, len, n;
+	char *buf = io_data->ptr;
 
 	strcpy(buf, io_data->ptr);
 
 	if (io_data->close)
 		strcat(buf, JSON_CLOSE);
 
-	if (isjson) {
-		if (io_data->full)
-			strcat(buf, JSON_END_TRUNCATED);
-		else
-			strcat(buf, JSON_END);
-	}
+	if (isjson)
+		strcat(buf, JSON_END);
 
 	len = strlen(buf);
 	tosend = len+1;
 
 	applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", tosend, buf, len > 10 ? "..." : BLANK);
 
-	count = 0;
-	while (count++ < 5 && tosend > 0) {
+	count = sendc = 0;
+	while (count < 5 && tosend > 0) {
 		// allow 50ms per attempt
 		struct timeval timeout = {0, 50000};
 		fd_set wd;
@@ -3862,28 +3893,34 @@ static void send_result(struct io_data *io_data, SOCKETTYPE c, bool isjson)
 		}
 
 		n = send(c, buf, tosend, 0);
+		sendc++;
 
 		if (SOCKETFAIL(n)) {
+			count++;
 			if (sock_blocks())
 				continue;
 
-			applog(LOG_WARNING, "API: send (%d) failed: %s", tosend, SOCKERRMSG);
+			applog(LOG_WARNING, "API: send (%d:%d) failed: %s", len+1, (len+1 - tosend), SOCKERRMSG);
 
 			return;
 		} else {
-			if (count <= 1) {
+			if (sendc <= 1) {
 				if (n == tosend)
 					applog(LOG_DEBUG, "API: sent all of %d first go", tosend);
 				else
 					applog(LOG_DEBUG, "API: sent %d of %d first go", n, tosend);
 			} else {
 				if (n == tosend)
-					applog(LOG_DEBUG, "API: sent all of remaining %d (count=%d)", tosend, count);
+					applog(LOG_DEBUG, "API: sent all of remaining %d (sendc=%d)", tosend, sendc);
 				else
-					applog(LOG_DEBUG, "API: sent %d of remaining %d (count=%d)", n, tosend, count);
+					applog(LOG_DEBUG, "API: sent %d of remaining %d (sendc=%d)", n, tosend, sendc);
 			}
 
 			tosend -= n;
+			buf += n;
+
+			if (n == 0)
+				count++;
 		}
 	}
 }
@@ -4351,7 +4388,7 @@ static void *mcast_thread(void *userdata)
 	pthread_detach(pthread_self());
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
-	RenameThread("api_mcast");
+	RenameThread("APIMcast");
 
 	mcast();
 
@@ -4510,6 +4547,8 @@ void api(int api_thr_id)
 	if (opt_api_mcast)
 		mcast_init();
 
+	strbufs = k_new_list("StrBufs", sizeof(SBITEM), ALLOC_SBITEMS, LIMIT_SBITEMS, false);
+
 	while (!bye) {
 		clisiz = sizeof(cli);
 		if (SOCKETFAIL(c = accept(*apisock, (struct sockaddr *)(&cli), &clisiz))) {
diff --git a/cgminer.c b/cgminer.c
index 2aed581..e07aa90 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -655,6 +655,13 @@ static char *set_int_0_to_200(const char *arg, int *i)
 }
 #endif
 
+#ifdef USE_BITFURY
+static char *set_int_32_to_63(const char *arg, int *i)
+{
+	return set_int_range(arg, i, 32, 63);
+}
+#endif
+
 static char *set_int_1_to_10(const char *arg, int *i)
 {
 	return set_int_range(arg, i, 1, 10);
@@ -1244,6 +1251,11 @@ static struct opt_table opt_config_table[] = {
 		     opt_set_charp, NULL, &opt_stderr_cmd,
 		     "Use custom pipe cmd for output messages"),
 #endif // defined(unix)
+#ifdef USE_BITFURY
+	OPT_WITH_ARG("--nfu-bits",
+		     set_int_32_to_63, opt_show_intval, &opt_nf1_bits,
+		     "Set nanofury bits for overclocking, range 32-63"),
+#endif
 	OPT_WITHOUT_ARG("--net-delay",
 			opt_set_bool, &opt_delaynet,
 			"Impose small delays in networking to not overload slow routers"),
@@ -2339,7 +2351,7 @@ static void check_winsizes(void)
 			statusy = LINES - 2;
 		else
 			statusy = logstart;
-		logcursor = statusy + 1;
+		logcursor = statusy;
 		wresize(statuswin, statusy, x);
 		getmaxyx(mainwin, y, x);
 		y -= logcursor;
@@ -3369,7 +3381,7 @@ static void *submit_work_thread(void *userdata)
 
 	pthread_detach(pthread_self());
 
-	RenameThread("submit_work");
+	RenameThread("SubmitWork");
 
 	applog(LOG_DEBUG, "Creating extra submit work thread");
 
@@ -3713,8 +3725,6 @@ static struct pool *priority_pool(int choice)
 	return ret;
 }
 
-static void clear_pool_work(struct pool *pool);
-
 /* Specifies whether we can switch to this pool or not. */
 static bool pool_unusable(struct pool *pool)
 {
@@ -4072,7 +4082,7 @@ static bool test_work_current(struct work *work)
 				       work->gbt ? "GBT " : "", work->pool->pool_no);
 			}
 		} else if (have_longpoll)
-			applog(LOG_NOTICE, "New block detected on network before longpoll");
+			applog(LOG_NOTICE, "New block detected on network before pool notification");
 		else
 			applog(LOG_NOTICE, "New block detected on network");
 		restart_threads();
@@ -4875,7 +4885,7 @@ static void *input_thread(void __maybe_unused *userdata)
 {
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
-	RenameThread("input");
+	RenameThread("Input");
 
 	if (!curses_active)
 		return NULL;
@@ -4910,7 +4920,7 @@ static void *api_thread(void *userdata)
 	pthread_detach(pthread_self());
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
-	RenameThread("api");
+	RenameThread("API");
 
 	set_lowprio();
 	api(api_thr_id);
@@ -5180,7 +5190,7 @@ void clear_stratum_shares(struct pool *pool)
 	}
 }
 
-static void clear_pool_work(struct pool *pool)
+void clear_pool_work(struct pool *pool)
 {
 	struct work *work, *tmp;
 	int cleared = 0;
@@ -5194,6 +5204,9 @@ static void clear_pool_work(struct pool *pool)
 		}
 	}
 	mutex_unlock(stgd_lock);
+
+	if (cleared)
+		applog(LOG_INFO, "Cleared %d work items due to stratum disconnect on pool %d", cleared, pool->pool_no);
 }
 
 static int cp_prio(void)
@@ -5286,7 +5299,7 @@ static void *stratum_rthread(void *userdata)
 
 	pthread_detach(pthread_self());
 
-	snprintf(threadname, 16, "StratumR/%d", pool->pool_no);
+	snprintf(threadname, sizeof(threadname), "%d/RStratum", pool->pool_no);
 	RenameThread(threadname);
 
 	while (42) {
@@ -5394,7 +5407,7 @@ static void *stratum_sthread(void *userdata)
 
 	pthread_detach(pthread_self());
 
-	snprintf(threadname, 16, "StratumS/%d", pool->pool_no);
+	snprintf(threadname, sizeof(threadname), "%d/SStratum", pool->pool_no);
 	RenameThread(threadname);
 
 	pool->stratum_q = tq_new();
@@ -6587,9 +6600,9 @@ void *miner_thread(void *userdata)
 	const int thr_id = mythr->id;
 	struct cgpu_info *cgpu = mythr->cgpu;
 	struct device_drv *drv = cgpu->drv;
-	char threadname[24];
+	char threadname[16];
 
-        snprintf(threadname, 24, "miner/%d", thr_id);
+        snprintf(threadname, sizeof(threadname), "%d/Miner", thr_id);
 	RenameThread(threadname);
 
 	thread_reportout(mythr);
@@ -6717,7 +6730,7 @@ static void *longpoll_thread(void *userdata)
 	char *lp_url;
 	int rolltime;
 
-	snprintf(threadname, 16, "longpoll/%d", cp->pool_no);
+	snprintf(threadname, sizeof(threadname), "%d/Longpoll", cp->pool_no);
 	RenameThread(threadname);
 
 	curl = curl_easy_init();
@@ -6883,7 +6896,7 @@ static void *watchpool_thread(void __maybe_unused *userdata)
 
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
-	RenameThread("watchpool");
+	RenameThread("Watchpool");
 
 	set_lowprio();
 
@@ -6968,7 +6981,7 @@ static void *watchdog_thread(void __maybe_unused *userdata)
 
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
-	RenameThread("watchdog");
+	RenameThread("Watchdog");
 
 	set_lowprio();
 	memset(&zero_tv, 0, sizeof(struct timeval));
@@ -7732,7 +7745,7 @@ static void *hotplug_thread(void __maybe_unused *userdata)
 {
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
-	RenameThread("hotplug");
+	RenameThread("Hotplug");
 
 	set_lowprio();
 
@@ -7785,7 +7798,7 @@ static void *libusb_poll_thread(void __maybe_unused *arg)
 {
 	struct timeval tv_end = {1, 0};
 
-	RenameThread("usbpoll");
+	RenameThread("USBPoll");
 
 	while (usb_polling)
 		libusb_handle_events_timeout_completed(NULL, &tv_end, NULL);
@@ -8269,6 +8282,8 @@ begin_bench:
 			applog(LOG_WARNING, "Pool %d not providing work fast enough", cp->pool_no);
 			cp->getfail_occasions++;
 			total_go++;
+			if (!pool_localgen(cp))
+				applog(LOG_INFO, "Increasing queue to %d", ++opt_queue);
 		}
 		pool = select_pool(lagging);
 retry:
diff --git a/configure.ac b/configure.ac
index cad6439..4fda5e9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -239,7 +239,7 @@ AM_CONDITIONAL([HAS_KNC], [test x$knc = xyes])
 minion="no"
 
 AC_ARG_ENABLE([minion],
-	[AC_HELP_STRING([--enable-minion],[Compile support for BlackArrow Bitfury (default disabled)])],
+	[AC_HELP_STRING([--enable-minion],[Compile support for Minion BlackArrow ASIC (default disabled)])],
 	[minion=$enableval]
 	)
 if test "x$minion" = xyes; then
diff --git a/driver-avalon.c b/driver-avalon.c
index 2727c8b..bb6121e 100644
--- a/driver-avalon.c
+++ b/driver-avalon.c
@@ -1012,9 +1012,9 @@ static void *avalon_get_results(void *userdata)
 	char readbuf[AVALON_READBUF_SIZE];
 	struct thr_info *thr = info->thr;
 	int offset = 0, ret = 0;
-	char threadname[24];
+	char threadname[16];
 
-	snprintf(threadname, 24, "ava_recv/%d", avalon->device_id);
+	snprintf(threadname, sizeof(threadname), "%d/AvaRecv", avalon->device_id);
 	RenameThread(threadname);
 
 	while (likely(!avalon->shutdown)) {
@@ -1147,9 +1147,9 @@ static void *avalon_send_tasks(void *userdata)
 	struct cgpu_info *avalon = (struct cgpu_info *)userdata;
 	struct avalon_info *info = avalon->device_data;
 	const int avalon_get_work_count = info->miner_count;
-	char threadname[24];
+	char threadname[16];
 
-	snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
+	snprintf(threadname, sizeof(threadname), "%d/AvaSend", avalon->device_id);
 	RenameThread(threadname);
 
 	while (likely(!avalon->shutdown)) {
@@ -1236,9 +1236,9 @@ static void *bitburner_send_tasks(void *userdata)
 	struct cgpu_info *avalon = (struct cgpu_info *)userdata;
 	struct avalon_info *info = avalon->device_data;
 	const int avalon_get_work_count = info->miner_count;
-	char threadname[24];
+	char threadname[16];
 
-	snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
+	snprintf(threadname, sizeof(threadname), "%d/AvaSend", avalon->device_id);
 	RenameThread(threadname);
 
 	while (likely(!avalon->shutdown)) {
diff --git a/driver-bab.c b/driver-bab.c
index d40bf47..fb7f795 100644
--- a/driver-bab.c
+++ b/driver-bab.c
@@ -1,8 +1,8 @@
 /*
- * Copyright 2013 Andrew Smith
+ * Copyright 2013-2014 Andrew Smith
  * Copyright 2013 bitfury
  *
- * BitFury GPIO code based on chainminer code:
+ * BitFury GPIO code originally based on chainminer code:
  *	https://github.com/bfsb/chainminer
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -16,10 +16,12 @@
 #include "miner.h"
 #include "sha2.h"
 #include "libbitfury.h"
+#include "klist.h"
 
 /*
  * Tested on RPi running both Raspbian and Arch
- *  with BlackArrow BitFury V1 16 chip GPIO board
+ *  with BlackArrow BitFury V1 & V2 GPIO Controller
+ *  with 16 chip BlackArrow BitFury boards
  */
 
 #ifndef LINUX
@@ -51,13 +53,26 @@ static void bab_detect(__maybe_unused bool hotplug)
 #define BAB_GPIO_CLR BAB_ADDR(10)
 #define BAB_GPIO_LEVEL BAB_ADDR(13)
 
-#define BAB_MAXCHIPS 256
+// If the V1 test of this many chips finds no chips it will try V2
+#define BAB_V1_CHIP_TEST 32
+
+//maximum number of chips per board
+#define BAB_BOARDCHIPS 16
 #define BAB_MAXBUF (BAB_MAXCHIPS * 512)
+#define BAB_V1_BANK 0
+//maximum number of alternative banks
 #define BAB_MAXBANKS 4
+//maximum number of boards in a bank
+#define BAB_BANKBOARDS 6
+//maximum number of chips on alternative bank
+#define BAB_BANKCHIPS (BAB_BOARDCHIPS * BAB_BANKBOARDS)
+//maximum number of chips
+#define BAB_MAXCHIPS (BAB_MAXBANKS * BAB_BANKCHIPS)
 #define BAB_CORES 16
 #define BAB_X_COORD 21
 #define BAB_Y_COORD 36
 
+#define BAB_NOOP 0
 #define BAB_BREAK ((uint8_t *)"\04")
 #define BAB_ASYNC ((uint8_t *)"\05")
 #define BAB_SYNC ((uint8_t *)"\06")
@@ -67,24 +82,18 @@ static void bab_detect(__maybe_unused bool hotplug)
 #define BAB_FFL_PASS file, func, line
 
 #define bab_reset(_bank, _times) _bab_reset(babcgpu, babinfo, _bank, _times)
-#define bab_txrx(_buf, _siz, _det) _bab_txrx(babcgpu, babinfo, _buf, _siz, _det, BAB_FFL_HERE)
-#define bab_add_buf(_data) _bab_add_buf(babcgpu, babinfo, _data, sizeof(_data)-1, BAB_FFL_HERE)
-#define BAB_ADD_BREAK() _bab_add_buf(babcgpu, babinfo, BAB_BREAK, 1, BAB_FFL_HERE)
-#define BAB_ADD_ASYNC() _bab_add_buf(babcgpu, babinfo, BAB_ASYNC, 1, BAB_FFL_HERE)
-#define bab_config_reg(_reg, _ena) _bab_config_reg(babcgpu, babinfo, _reg, _ena, BAB_FFL_HERE)
-#define bab_add_data(_addr, _data, _siz) _bab_add_data(babcgpu, babinfo, _addr, (const uint8_t *)(_data), _siz, BAB_FFL_HERE)
+#define bab_txrx(_item, _det) _bab_txrx(babcgpu, babinfo, _item, _det, BAB_FFL_HERE)
+#define bab_add_buf(_item, _data) _bab_add_buf(_item, _data, sizeof(_data)-1, BAB_FFL_HERE)
+#define BAB_ADD_BREAK(_item) _bab_add_buf(_item, BAB_BREAK, 1, BAB_FFL_HERE)
+#define BAB_ADD_ASYNC(_item) _bab_add_buf(_item, BAB_ASYNC, 1, BAB_FFL_HERE)
+#define bab_config_reg(_item, _reg, _ena) _bab_config_reg(_item, _reg, _ena, BAB_FFL_HERE)
+#define bab_add_data(_item, _addr, _data, _siz) _bab_add_data(_item, _addr, (const uint8_t *)(_data), _siz, BAB_FFL_HERE)
+
+#define BAB_ADD_NOOPs(_item, _count) _bab_add_noops(_item, _count, BAB_FFL_HERE)
 
 #define BAB_ADD_MIN 4
 #define BAB_ADD_MAX 128
 
-#define BAB_STATE_DONE 0
-#define BAB_STATE_READY 1
-#define BAB_STATE_SENDING 2
-#define BAB_STATE_SENT 3
-#define BAB_STATE_READING 4
-
-#define BAB_SPI_BUFFERS 2
-
 #define BAB_BASEA 4
 #define BAB_BASEB 61
 #define BAB_COUNTERS 16
@@ -122,9 +131,6 @@ static const uint32_t bab_test_data[BAB_TEST_DATA] = {
 	0x8a0bb7b7,	0x33af304f,	0x0b290c1a //,	0xf0c4e61f
 };
 
-//maximum number of chips on alternative bank
-// #define BANKCHIPS 64
-
 /*
  * maximum chip speed available for auto tuner
  * speed/nrate/hrate/watt
@@ -215,6 +221,14 @@ static const uint8_t bab_reg_ena[4] = { 0xc1, 0x6a, 0x59, 0xe3 };
 static const uint8_t bab_reg_dis[4] = { 0x00, 0x00, 0x00, 0x00 };
 
 #define BAB_NONCE_OFFSETS 3
+#define BAB_OFF_0x1C_STA 2
+#define BAB_OFF_0x1C_FIN 2
+#define BAB_OFF_OTHER_STA 0
+#define BAB_OFF_OTHER_FIN 1
+
+#define BAB_EVIL_NONCE 0xe0
+#define BAB_EVIL_MASK 0xff
+
 static const uint32_t bab_nonce_offsets[] = {-0x800000, 0, -0x400000};
 
 struct bab_work_send {
@@ -231,50 +245,99 @@ struct bab_work_reply {
 	uint32_t jobsel;
 };
 
-#define MAX_BLISTS 4096
+#define BAB_CHIP_MIN (sizeof(struct bab_work_reply)+16)
+
+#define ALLOC_WITEMS 1024
+#define LIMIT_WITEMS 0
 
-typedef struct blist {
-	struct blist *prev;
-	struct blist *next;
+// Work
+typedef struct witem {
 	struct work *work;
+	struct bab_work_send chip_input;
+	bool ci_setup;
 	int nonces;
-} BLIST;
-
-#define MAX_RLISTS 256
-
-typedef struct rlist {
-	struct rlist *prev;
-	struct rlist *next;
+	struct timeval work_start;
+} WITEM;
+
+#define ALLOC_SITEMS 8
+#define LIMIT_SITEMS 0
+
+// SPI I/O
+typedef struct sitem {
+	uint32_t siz;
+	uint8_t wbuf[BAB_MAXBUF];
+	uint8_t rbuf[BAB_MAXBUF];
+	uint32_t chip_off[BAB_MAXCHIPS+1];
+	uint32_t bank_off[BAB_MAXBANKS+2];
+	// WITEMs used to build the work
+	K_ITEM *witems[BAB_MAXCHIPS];
+	struct timeval work_start;
+} SITEM;
+
+#define ALLOC_RITEMS 256
+#define LIMIT_RITEMS 0
+
+// Results
+typedef struct ritem {
 	int chip;
-	uint32_t nonce;
-	bool first_second;
-} RLIST;
+	int nonces;
+	uint32_t nonce[BAB_REPLY_NONCES];
+	bool not_first_reply;
+	struct timeval when;
+} RITEM;
+
+#define ALLOC_NITEMS 102400
+#define LIMIT_NITEMS 0
+
+// Nonce History
+typedef struct nitem {
+	struct timeval found;
+} NITEM;
+
+#define DATAW(_item) ((WITEM *)(_item->data))
+#define DATAS(_item) ((SITEM *)(_item->data))
+#define DATAR(_item) ((RITEM *)(_item->data))
+#define DATAN(_item) ((NITEM *)(_item->data))
+
+// Record the number of each band between work sends
+#define BAB_DELAY_BANDS 10
+#define BAB_DELAY_BASE 0.5
+#define BAB_DELAY_STEP 0.2
+
+#define BAB_CHIP_SPEEDS 6
+// less than or equal GH/s
+static double chip_speed_ranges[BAB_CHIP_SPEEDS - 1] =
+	{ 0.0, 0.8, 1.6, 2.2, 2.8 };
+// Greater than the last one above means it's the last speed
+static char *chip_speed_names[BAB_CHIP_SPEEDS] =
+	{ "Dead", "V.Slow", "Slow", "OK", "Good", "Fast" };
 
 struct bab_info {
 	struct thr_info spi_thr;
 	struct thr_info res_thr;
 
-	pthread_mutex_t spi_lock;
-	pthread_mutex_t res_lock;
 	pthread_mutex_t did_lock;
-	cglock_t blist_lock;
+	pthread_mutex_t nonce_lock;
 
 	// All GPIO goes through this
 	volatile unsigned *gpio;
 
+	int version;
 	int spifd;
 	int chips;
+	int chips_per_bank[BAB_MAXBANKS+1];
+	int missing_chips_per_bank[BAB_MAXBANKS+1];
+	int boards;
+	int banks;
 	uint32_t chip_spis[BAB_MAXCHIPS+1];
 
-	int buffer;
-	int buf_status[BAB_SPI_BUFFERS];
-	uint8_t buf_write[BAB_SPI_BUFFERS][BAB_MAXBUF];
-	uint8_t buf_read[BAB_SPI_BUFFERS][BAB_MAXBUF];
-	uint32_t buf_used[BAB_SPI_BUFFERS];
-	uint32_t chip_off[BAB_SPI_BUFFERS][BAB_MAXCHIPS+1];
-	uint32_t bank_off[BAB_SPI_BUFFERS][BAB_MAXBANKS+2];
+	int reply_wait;
+
+	cgsem_t scan_work;
+	cgsem_t spi_work;
+	cgsem_t spi_reply;
+	cgsem_t process_reply;
 
-	struct bab_work_send chip_input[BAB_MAXCHIPS];
 	struct bab_work_reply chip_results[BAB_MAXCHIPS];
 	struct bab_work_reply chip_prev[BAB_MAXCHIPS];
 
@@ -286,20 +349,18 @@ struct bab_info {
 
 	uint8_t osc[BAB_OSC];
 
+	// TODO: performance tuning
 	int fixchip;
 
 	/*
 	 * Ignore errors in the first work reply since
 	 * they may be from a previous run or random junk
-	 * There can be >100 with just a 16 chip board
+	 * There can be >100 with just one 16 chip board
 	 */
 	uint32_t initial_ignored;
-	bool nonce_before[BAB_MAXCHIPS];
 	bool not_first_reply[BAB_MAXCHIPS];
 
 	// Stats
-	struct timeval chip_start[BAB_MAXCHIPS];
-	int chip_busy[BAB_MAXCHIPS];
 	uint64_t core_good[BAB_MAXCHIPS][BAB_CORES];
 	uint64_t core_bad[BAB_MAXCHIPS][BAB_CORES];
 	uint64_t chip_spie[BAB_MAXCHIPS]; // spi errors
@@ -309,6 +370,11 @@ struct bab_info {
 	uint64_t chip_bad[BAB_MAXCHIPS];
 	uint64_t chip_ncore[BAB_MAXCHIPS][BAB_X_COORD][BAB_Y_COORD];
 
+	uint64_t chip_cont_bad[BAB_MAXCHIPS];
+	uint64_t chip_max_bad[BAB_MAXCHIPS];
+
+	uint64_t discarded_e0s;
+
 	uint64_t untested_nonces;
 	uint64_t tested_nonces;
 
@@ -319,308 +385,153 @@ struct bab_info {
 	uint64_t total_tests;
 	uint64_t max_tests_per_nonce;
 	uint64_t total_links;
+	uint64_t total_proc_links;
 	uint64_t max_links;
-
-	int blist_count;
-	int bfree_count;
-	int work_count;
-	int chip_count;
-	BLIST *bfree_list;
-	BLIST *work_list;
-	BLIST *chip_list[BAB_MAXCHIPS];
-
-	int rlist_count;
-	int rfree_count;
-	int res_count;
-	RLIST *rfree_list;
-	RLIST *res_list_head;
-	RLIST *res_list_tail;
+	uint64_t max_proc_links;
+	uint64_t total_work_links;
+
+	uint64_t fail;
+	uint64_t fail_total_tests;
+	uint64_t fail_total_links;
+	uint64_t fail_total_work_links;
+
+	uint64_t ign_total_tests;
+	uint64_t ign_total_links;
+	uint64_t ign_total_work_links;
+
+	struct timeval last_sent_work;
+	uint64_t delay_count;
+	double delay_min;
+	double delay_max;
+	/*
+	 * 0 is below band ranges
+	 * BAB_DELAY_BANDS+1 is above band ranges
+	 */
+	uint64_t delay_bands[BAB_DELAY_BANDS+2];
+
+	uint64_t send_count;
+	double send_total;
+	double send_min;
+	double send_max;
+
+	// Work
+	K_LIST *wfree_list;
+	K_STORE *available_work;
+	K_STORE *chip_work[BAB_MAXCHIPS];
+
+	// SPI I/O
+	K_LIST *sfree_list;
+	// Waiting to send
+	K_STORE *spi_list;
+	// Sent
+	K_STORE *spi_sent;
+
+	// Results
+	K_LIST *rfree_list;
+	K_STORE *res_list;
+
+	// Nonce History
+	K_LIST *nfree_list;
+	K_STORE *good_nonces[BAB_MAXCHIPS];
+	K_STORE *bad_nonces[BAB_MAXCHIPS];
 
 	struct timeval last_did;
 
 	bool initialised;
 };
 
-static BLIST *new_blist_set(struct cgpu_info *babcgpu)
-{
-	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
-	BLIST *blist = NULL;
-	int i;
-
-	blist = calloc(MAX_BLISTS, sizeof(*blist));
-	if (!blist)
-		quithere(1, "Failed to calloc blist - when old count=%d", babinfo->blist_count);
-
-	babinfo->blist_count += MAX_BLISTS;
-	babinfo->bfree_count = MAX_BLISTS;
-
-	blist[0].prev = NULL;
-	blist[0].next = &(blist[1]);
-	for (i = 1; i < MAX_BLISTS-1; i++) {
-		blist[i].prev = &blist[i-1];
-		blist[i].next = &blist[i+1];
-	}
-	blist[MAX_BLISTS-1].prev = &(blist[MAX_BLISTS-2]);
-	blist[MAX_BLISTS-1].next = NULL;
-
-	return blist;
-}
-
-static BLIST *next_work(struct cgpu_info *babcgpu, int chip)
-{
-	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
-	BLIST *bitem;
-
-	cg_wlock(&babinfo->blist_lock);
-	bitem = babinfo->work_list;
-	if (bitem) {
-		// Unlink it from work
-		if (bitem->next)
-			bitem->next->prev = NULL;
-		babinfo->work_list = bitem->next;
-		babinfo->work_count--;
-
-		// Add it to the chip
-		bitem->next = babinfo->chip_list[chip];
-		bitem->prev = NULL;
-		if (bitem->next)
-			bitem->next->prev = bitem;
-		babinfo->chip_list[chip] = bitem;
-		babinfo->chip_count++;
-	}
-	cg_wunlock(&babinfo->blist_lock);
-
-	return bitem;
-}
-
-static void discard_last(struct cgpu_info *babcgpu, int chip)
-{
-	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
-	BLIST *bitem;
-
-	cg_wlock(&babinfo->blist_lock);
-	bitem = babinfo->chip_list[chip];
-	if (bitem) {
-		// Unlink it from the chip
-		if (bitem->next)
-			bitem->next->prev = NULL;
-		babinfo->chip_list[chip] = bitem->next;
-		babinfo->chip_count--;
-
-		// Put it in the free list
-		bitem->next = babinfo->bfree_list;
-		bitem->prev = NULL;
-		if (bitem->next)
-			bitem->next->prev = bitem;
-		babinfo->bfree_list = bitem;
-		babinfo->bfree_count++;
-	}
-	cg_wunlock(&babinfo->blist_lock);
-}
-
-static BLIST *store_work(struct cgpu_info *babcgpu, struct work *work)
-{
-	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
-	BLIST *bitem = NULL;
-	int ran_out = 0;
-
-	cg_wlock(&babinfo->blist_lock);
-
-	if (babinfo->bfree_list == NULL) {
-		ran_out = babinfo->blist_count;
-		babinfo->bfree_list = new_blist_set(babcgpu);
-	}
-
-	// unlink from free
-	bitem = babinfo->bfree_list;
-	babinfo->bfree_list = babinfo->bfree_list->next;
-	if (babinfo->bfree_list)
-		babinfo->bfree_list->prev = NULL;
-	babinfo->bfree_count--;
-
-	// add to work
-	bitem->next = babinfo->work_list;
-	bitem->prev = NULL;
-	if (bitem->next)
-		bitem->next->prev = bitem;
-	babinfo->work_list = bitem;
-	babinfo->work_count++;
-
-	bitem->work = work;
-	bitem->nonces = 0;
-
-	cg_wunlock(&babinfo->blist_lock);
-
-	if (ran_out > 0) {
-		applog(LOG_ERR, "%s%i: BLIST used count exceeded %d, now %d (work=%d chip=%d)",
-				babcgpu->drv->name, babcgpu->device_id,
-				ran_out, babinfo->blist_count,
-				babinfo->work_count,
-				babinfo->chip_count);
-	}
-
-	return bitem;
-}
-
-static void free_blist(struct cgpu_info *babcgpu, BLIST *bhead, int chip)
-{
-	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
-	struct work *work;
-	BLIST *bitem;
-
-	if (!bhead)
-		return;
-
-	// Unlink it from the chip
-	cg_wlock(&babinfo->blist_lock);
-	if (unlikely(bhead == babinfo->chip_list[chip])) {
-		// Removing the chip head is an error
-		bhead = bhead->next;
-		babinfo->chip_list[chip]->next = NULL;
-	} else
-		bhead->prev->next = NULL;
-	bitem = bhead;
-	while (bitem) {
-		babinfo->chip_count--;
-		bitem = bitem->next;
-	}
-	cg_wunlock(&babinfo->blist_lock);
-
-	while (bhead) {
-		bitem = bhead;
-		bhead = bitem->next;
-
-		// add to free
-		cg_wlock(&babinfo->blist_lock);
-		bitem->next = babinfo->bfree_list;
-		if (babinfo->bfree_list)
-			babinfo->bfree_list->prev = bitem;
-		bitem->prev = NULL;
-		babinfo->bfree_list = bitem;
-		babinfo->bfree_count++;
-		work = bitem->work;
-		cg_wunlock(&babinfo->blist_lock);
-
-		work_completed(babcgpu, work);
-	}
+/*
+ * Amount of time for history
+ * Older items in nonce_history are discarded
+ * 300s / 5 minutes
+ */
+#define HISTORY_TIME_S 300
 
-}
+/*
+ * If the SPI I/O thread waits longer than this long for work
+ * it will report an error saying how long it's waiting
+ * and again every BAB_STD_WAIT_mS after that
+ */
+#define BAB_LONG_uS 1200000
 
-static RLIST *new_rlist_set(struct cgpu_info *babcgpu)
-{
-	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
-	RLIST *rlist = NULL;
-	int i;
+/*
+ * If work wasn't available early enough,
+ * report every BAB_LONG_WAIT_mS until it is
+ */
+#define BAB_LONG_WAIT_mS 888
 
-	rlist = calloc(MAX_RLISTS, sizeof(*rlist));
-	if (!rlist)
-		quithere(1, "Failed to calloc rlist - when old count=%d", babinfo->rlist_count);
+/*
+ * Some amount of time to wait for work
+ * before checking how long we've waited
+ */
+#define BAB_STD_WAIT_mS 888
 
-	babinfo->rlist_count += MAX_RLISTS;
-	babinfo->rfree_count = MAX_RLISTS;
+/*
+ * How long to wait for the ioctl() to complete (per BANK)
+ * This is a failsafe in case the ioctl() fails
+ * since bab_txrx() will already post a wakeup when it completes
+ * V1 is set to this x 2
+ * V2 is set to this x active banks
+ */
+#define BAB_REPLY_WAIT_mS 160
 
-	rlist[0].prev = NULL;
-	rlist[0].next = &(rlist[1]);
-	for (i = 1; i < MAX_RLISTS-1; i++) {
-		rlist[i].prev = &rlist[i-1];
-		rlist[i].next = &rlist[i+1];
-	}
-	rlist[MAX_RLISTS-1].prev = &(rlist[MAX_RLISTS-2]);
-	rlist[MAX_RLISTS-1].next = NULL;
+/*
+ * Work items older than this should not expect results
+ * It has to allow for the result buffer returned with the next result
+ * 0.75GH/s takes 5.727s to do a full nonce range
+ * If HW is too high, consider increasing this to see if work is being
+ * expired too early (due to slow chips)
+ */
+#define BAB_WORK_EXPIRE_mS 7800
 
-	return rlist;
-}
+// Don't send work more often than this
+#define BAB_EXPECTED_WORK_DELAY_mS 899
 
-static RLIST *store_nonce(struct cgpu_info *babcgpu, int chip, uint32_t nonce, bool first_second)
+static void cleanup_older(struct cgpu_info *babcgpu, int chip, K_ITEM *witem)
 {
 	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
-	RLIST *ritem = NULL;
-	int ran_out = 0;
-
-	mutex_lock(&(babinfo->res_lock));
-
-	if (babinfo->rfree_list == NULL) {
-		ran_out = babinfo->rlist_count;
-		babinfo->rfree_list = new_rlist_set(babcgpu);
-	}
-
-	// unlink from rfree
-	ritem = babinfo->rfree_list;
-	babinfo->rfree_list = babinfo->rfree_list->next;
-	if (babinfo->rfree_list)
-		babinfo->rfree_list->prev = NULL;
-	babinfo->rfree_count--;
-
-	// add to head of results
-	ritem->next = babinfo->res_list_head;
-	ritem->prev = NULL;
-	babinfo->res_list_head = ritem;
-	if (ritem->next)
-		ritem->next->prev = ritem;
-	else
-		babinfo->res_list_tail = ritem;
+	struct timeval now;
+	bool expired_item;
+	K_ITEM *tail;
 
-	babinfo->res_count++;
+	cgtime(&now);
 
-	ritem->chip = chip;
-	ritem->nonce = nonce;
-	ritem->first_second = first_second;
+	K_WLOCK(babinfo->chip_work[chip]);
+	tail = babinfo->chip_work[chip]->tail;
+	expired_item = false;
+	// Discard expired work
+	while (tail) {
+		if (ms_tdiff(&now, &(DATAW(tail)->work_start)) < BAB_WORK_EXPIRE_mS)
+			break;
 
-	mutex_unlock(&(babinfo->res_lock));
+		if (tail == witem)
+			expired_item = true;
 
-	if (ran_out > 0) {
-		applog(LOG_ERR, "%s%i: RLIST used count exceeded %d, now %d (work=%d chip=%d)",
-				babcgpu->drv->name, babcgpu->device_id,
-				ran_out, babinfo->rlist_count,
-				babinfo->work_count,
-				babinfo->chip_count);
+		k_unlink_item(babinfo->chip_work[chip], tail);
+		K_WUNLOCK(babinfo->chip_work[chip]);
+		work_completed(babcgpu, DATAW(tail)->work);
+		K_WLOCK(babinfo->chip_work[chip]);
+		k_add_head(babinfo->wfree_list, tail);
+		tail = babinfo->chip_work[chip]->tail;
 	}
-
-	return ritem;
-}
-
-static bool oldest_nonce(struct cgpu_info *babcgpu, int *chip, uint32_t *nonce, bool *first_second)
-{
-	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
-	RLIST *ritem = NULL;
-	bool found = false;
-
-	mutex_lock(&(babinfo->res_lock));
-
-	if (babinfo->res_list_tail) {
-		// unlink from res
-		ritem = babinfo->res_list_tail;
-		if (ritem->prev) {
-			ritem->prev->next = NULL;
-			babinfo->res_list_tail = ritem->prev;
-		} else
-			babinfo->res_list_head = babinfo->res_list_tail = NULL;
-
-		babinfo->res_count--;
-
-		found = true;
-		*chip = ritem->chip;
-		*nonce = ritem->nonce;
-		*first_second = ritem->first_second;
-
-		// add to rfree
-		ritem->next = babinfo->rfree_list;
-		ritem->prev = NULL;
-		if (ritem->next)
-			ritem->next->prev = ritem;
-		babinfo->rfree_list = ritem;
-
-		babinfo->rfree_count++;
+	// If we didn't expire witem, then remove all older than it
+	if (!expired_item && witem && witem->next) {
+		tail = babinfo->chip_work[chip]->tail;
+		while (tail && tail != witem) {
+			k_unlink_item(babinfo->chip_work[chip], tail);
+			K_WUNLOCK(babinfo->chip_work[chip]);
+			work_completed(babcgpu, DATAW(tail)->work);
+			K_WLOCK(babinfo->chip_work[chip]);
+			k_add_head(babinfo->wfree_list, tail);
+			tail = babinfo->chip_work[chip]->tail;
+		}
 	}
-
-	mutex_unlock(&(babinfo->res_lock));
-
-	return found;
+	K_WUNLOCK(babinfo->chip_work[chip]);
 }
 
 static void _bab_reset(__maybe_unused struct cgpu_info *babcgpu, struct bab_info *babinfo, int bank, int times)
 {
-	const int banks[4] = { 18, 23, 24, 25 };
+	const int banks[BAB_MAXBANKS] = { 18, 23, 24, 25 };
 	int i;
 
 	BAB_INP_GPIO(10);
@@ -629,7 +540,7 @@ static void _bab_reset(__maybe_unused struct cgpu_info *babcgpu, struct bab_info
 	BAB_OUT_GPIO(11);
 
 	if (bank) {
-		for (i = 0; i < 4; i++) {
+		for (i = 0; i < BAB_MAXBANKS; i++) {
 			BAB_INP_GPIO(banks[i]);
 			BAB_OUT_GPIO(banks[i]);
 			if (bank == i+1)
@@ -639,7 +550,7 @@ static void _bab_reset(__maybe_unused struct cgpu_info *babcgpu, struct bab_info
 		}
 		cgsleep_us(4096);
 	} else {
-		for (i = 0; i < 4; i++)
+		for (i = 0; i < BAB_MAXBANKS; i++)
 			BAB_INP_GPIO(banks[i]);
 	}
 
@@ -660,15 +571,16 @@ static void _bab_reset(__maybe_unused struct cgpu_info *babcgpu, struct bab_info
 }
 
 // TODO: handle a false return where this is called?
-static bool _bab_txrx(struct cgpu_info *babcgpu, struct bab_info *babinfo, int buf, uint32_t siz, bool detect_ignore, const char *file, const char *func, const int line)
+static bool _bab_txrx(struct cgpu_info *babcgpu, struct bab_info *babinfo, K_ITEM *item, bool detect_ignore, const char *file, const char *func, const int line)
 {
-	int bank, i;
-	uint32_t pos;
+	int bank, i, count;
+	uint32_t siz, pos;
 	struct spi_ioc_transfer tran;
 	uintptr_t rbuf, wbuf;
 
-	wbuf = (uintptr_t)(babinfo->buf_write[buf]);
-	rbuf = (uintptr_t)(babinfo->buf_read[buf]);
+	wbuf = (uintptr_t)(DATAS(item)->wbuf);
+	rbuf = (uintptr_t)(DATAS(item)->rbuf);
+	siz = (uint32_t)(DATAS(item)->siz);
 
 	memset(&tran, 0, sizeof(tran));
 	tran.delay_usecs = 0;
@@ -677,7 +589,7 @@ static bool _bab_txrx(struct cgpu_info *babcgpu, struct bab_info *babinfo, int b
 	i = 0;
 	pos = 0;
 	for (bank = 0; bank <= BAB_MAXBANKS; bank++) {
-		if (babinfo->bank_off[buf][bank]) {
+		if (DATAS(item)->bank_off[bank]) {
 			bab_reset(bank, 64);
 			break;
 		}
@@ -690,13 +602,14 @@ static bool _bab_txrx(struct cgpu_info *babcgpu, struct bab_info *babinfo, int b
 		return false;
 	}
 
+	count = 0;
 	while (siz > 0) {
 		tran.tx_buf = wbuf;
 		tran.rx_buf = rbuf;
 		tran.speed_hz = BAB_SPI_SPEED;
-		if (pos == babinfo->bank_off[buf][bank]) {
+		if (pos == DATAS(item)->bank_off[bank]) {
 			for (; ++bank <= BAB_MAXBANKS; ) {
-				if (babinfo->bank_off[buf][bank] > pos) {
+				if (DATAS(item)->bank_off[bank] > pos) {
 					bab_reset(bank, 64);
 					break;
 				}
@@ -707,14 +620,14 @@ static bool _bab_txrx(struct cgpu_info *babcgpu, struct bab_info *babinfo, int b
 		else
 			tran.len = BAB_SPI_BUFSIZ;
 
-		if (pos < babinfo->bank_off[buf][bank] &&
-		    babinfo->bank_off[buf][bank] < (pos + tran.len))
-			tran.len = babinfo->bank_off[buf][bank] - pos;
+		if (pos < DATAS(item)->bank_off[bank] &&
+		    DATAS(item)->bank_off[bank] < (pos + tran.len))
+			tran.len = DATAS(item)->bank_off[bank] - pos;
 
 		for (; i < babinfo->chips; i++) {
-			if (!babinfo->chip_off[buf][i])
+			if (!DATAS(item)->chip_off[i])
 				continue;
-			if (babinfo->chip_off[buf][i] >= pos + tran.len) {
+			if (DATAS(item)->chip_off[i] >= pos + tran.len) {
 				tran.speed_hz = babinfo->chip_spis[i];
 				break;
 			}
@@ -741,11 +654,12 @@ static bool _bab_txrx(struct cgpu_info *babcgpu, struct bab_info *babinfo, int b
 						BAB_SPI_SPEED, BAB_FFL_PASS);
 		}
 
+		count++;
 		if (ioctl(babinfo->spifd, SPI_IOC_MESSAGE(1), (void *)&tran) < 0) {
 			if (!detect_ignore || errno != 110) {
-				applog(LOG_ERR, "%s%d: ioctl failed err=%d" BAB_FFL,
+				applog(LOG_ERR, "%s%d: ioctl (%d) failed err=%d" BAB_FFL,
 						babcgpu->drv->name, babcgpu->device_id,
-						errno, BAB_FFL_PASS);
+						count, errno, BAB_FFL_PASS);
 			}
 			return false;
 		}
@@ -755,24 +669,23 @@ static bool _bab_txrx(struct cgpu_info *babcgpu, struct bab_info *babinfo, int b
 		rbuf += tran.len;
 		pos += tran.len;
 	}
+	cgtime(&(DATAS(item)->work_start));
 	mutex_lock(&(babinfo->did_lock));
 	cgtime(&(babinfo->last_did));
 	mutex_unlock(&(babinfo->did_lock));
 	return true;
 }
 
-static void _bab_add_buf_rev(__maybe_unused struct cgpu_info *babcgpu, struct bab_info *babinfo, const uint8_t *data, uint32_t siz, const char *file, const char *func, const int line)
+static void _bab_add_buf_rev(K_ITEM *item, const uint8_t *data, uint32_t siz, const char *file, const char *func, const int line)
 {
-	uint8_t tmp;
 	uint32_t now_used, i;
-	int buf;
+	uint8_t tmp;
 
-	buf = babinfo->buffer;
-	now_used = babinfo->buf_used[buf];
+	now_used = DATAS(item)->siz;
 	if (now_used + siz >= BAB_MAXBUF) {
 		quitfrom(1, file, func, line,
-			"%s() buffer %d limit of %d exceeded=%d siz=%d",
-			__func__, buf, BAB_MAXBUF, now_used + siz, siz);
+			"%s() buffer limit of %d exceeded=%d siz=%d",
+			__func__, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
 	}
 
 	for (i = 0; i < siz; i++) {
@@ -780,30 +693,43 @@ static void _bab_add_buf_rev(__maybe_unused struct cgpu_info *babcgpu, struct ba
 		tmp = ((tmp & 0xaa)>>1) | ((tmp & 0x55) << 1);
 		tmp = ((tmp & 0xcc)>>2) | ((tmp & 0x33) << 2);
 		tmp = ((tmp & 0xf0)>>4) | ((tmp & 0x0f) << 4);
-		babinfo->buf_write[buf][now_used + i] = tmp;
+		DATAS(item)->wbuf[now_used + i] = tmp;
 	}
 
-	babinfo->buf_used[buf] += siz;
+	DATAS(item)->siz += siz;
 }
 
-static void _bab_add_buf(__maybe_unused struct cgpu_info *babcgpu, struct bab_info *babinfo, const uint8_t *data, size_t siz, const char *file, const char *func, const int line)
+static void _bab_add_buf(K_ITEM *item, const uint8_t *data, size_t siz, const char *file, const char *func, const int line)
 {
 	uint32_t now_used;
-	int buf;
 
-	buf = babinfo->buffer;
-	now_used = babinfo->buf_used[buf];
+	now_used = DATAS(item)->siz;
 	if (now_used + siz >= BAB_MAXBUF) {
 		quitfrom(1, file, func, line,
-			"%s() buffer %d limit of %d exceeded=%d siz=%d",
-			__func__, buf, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
+			"%s() DATAS buffer limit of %d exceeded=%d siz=%d",
+			__func__, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
 	}
 
-	memcpy(&(babinfo->buf_write[buf][now_used]), data, siz);
-	babinfo->buf_used[buf] += siz;
+	memcpy(&(DATAS(item)->wbuf[now_used]), data, siz);
+	DATAS(item)->siz += siz;
 }
 
-static void _bab_add_data(struct cgpu_info *babcgpu, struct bab_info *babinfo, uint32_t addr, const uint8_t *data, size_t siz, const char *file, const char *func, const int line)
+static void _bab_add_noops(K_ITEM *item, size_t siz, const char *file, const char *func, const int line)
+{
+	uint32_t now_used;
+
+	now_used = DATAS(item)->siz;
+	if (now_used + siz >= BAB_MAXBUF) {
+		quitfrom(1, file, func, line,
+			"%s() DATAS buffer limit of %d exceeded=%d siz=%d",
+			__func__, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
+	}
+
+	memset(&(DATAS(item)->wbuf[now_used]), BAB_NOOP, siz);
+	DATAS(item)->siz += siz;
+}
+
+static void _bab_add_data(K_ITEM *item, uint32_t addr, const uint8_t *data, size_t siz, const char *file, const char *func, const int line)
 {
 	uint8_t tmp[3];
 	int trf_siz;
@@ -817,17 +743,17 @@ static void _bab_add_data(struct cgpu_info *babcgpu, struct bab_info *babinfo, u
 	tmp[0] = (trf_siz - 1) | 0xE0;
 	tmp[1] = (addr >> 8) & 0xff;
 	tmp[2] = addr & 0xff;
-	_bab_add_buf(babcgpu, babinfo, tmp, sizeof(tmp), BAB_FFL_PASS);
-	_bab_add_buf_rev(babcgpu, babinfo, data, siz, BAB_FFL_PASS);
+	_bab_add_buf(item, tmp, sizeof(tmp), BAB_FFL_PASS);
+	_bab_add_buf_rev(item, data, siz, BAB_FFL_PASS);
 }
 
-static void _bab_config_reg(struct cgpu_info *babcgpu, struct bab_info *babinfo, uint32_t reg, bool enable, const char *file, const char *func, const int line)
+static void _bab_config_reg(K_ITEM *item, uint32_t reg, bool enable, const char *file, const char *func, const int line)
 {
 	if (enable) {
-		_bab_add_data(babcgpu, babinfo, BAB_REG_ADDR + reg*32,
+		_bab_add_data(item, BAB_REG_ADDR + reg*32,
 				bab_reg_ena, sizeof(bab_reg_ena), BAB_FFL_PASS);
 	} else {
-		_bab_add_data(babcgpu, babinfo, BAB_REG_ADDR + reg*32,
+		_bab_add_data(item, BAB_REG_ADDR + reg*32,
 				bab_reg_dis, sizeof(bab_reg_dis), BAB_FFL_PASS);
 	}
 
@@ -850,57 +776,36 @@ static void bab_set_osc(struct bab_info *babinfo, int chip)
 	applog(LOG_DEBUG, "@osc(chip=%d) fast=%d 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x", chip, fast, babinfo->osc[0], babinfo->osc[1], babinfo->osc[2], babinfo->osc[3], babinfo->osc[4], babinfo->osc[5], babinfo->osc[6], babinfo->osc[7]);
 }
 
-static bool bab_put(struct cgpu_info *babcgpu, struct bab_info *babinfo)
+static void bab_put(struct bab_info *babinfo, K_ITEM *sitem)
 {
-	int buf, i, reg, bank = 0;
-
-	babinfo->buffer = -1;
-
-	mutex_lock(&(babinfo->spi_lock));
-	if (babinfo->buf_status[0] == BAB_STATE_DONE) {
-		babinfo->buffer = 0;
-	} else if (babinfo->buf_status[1] == BAB_STATE_DONE) {
-		babinfo->buffer = 1;
-	} else if (babinfo->buf_status[0] == BAB_STATE_READY) {
-		babinfo->buf_status[0] = BAB_STATE_DONE;
-		babinfo->buffer = 0;
-	} else if (babinfo->buf_status[1] == BAB_STATE_READY) {
-		babinfo->buf_status[1] = BAB_STATE_DONE;
-		babinfo->buffer = 1;
-	}
-	mutex_unlock(&(babinfo->spi_lock));
-
-	if (babinfo->buffer == -1)
-		return false;
-
-	buf = babinfo->buffer;
-	babinfo->buf_used[buf] = 0;
-	memset(babinfo->bank_off[buf], 0, sizeof(babinfo->bank_off) / BAB_SPI_BUFFERS);
+	struct bab_work_send *chip_input;
+	int i, reg, bank = 0;
+	size_t chip_siz;
 
-	BAB_ADD_BREAK();
+	BAB_ADD_BREAK(sitem);
 	for (i = 0; i < babinfo->chips; i++) {
 		if (babinfo->chip_bank[i] != bank) {
-			babinfo->bank_off[buf][bank] = babinfo->buf_used[buf];
+			DATAS(sitem)->bank_off[bank] = DATAS(sitem)->siz;
 			bank = babinfo->chip_bank[i];
-			BAB_ADD_BREAK();
+			BAB_ADD_BREAK(sitem);
 		}
 		if (i == babinfo->fixchip &&
 		    (BAB_CFGD_SET(babinfo->chip_conf[i]) ||
 		     !babinfo->chip_conf[i])) {
 			bab_set_osc(babinfo, i);
-			bab_add_data(BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
-			bab_config_reg(BAB_ICLK_REG, BAB_ICLK_BIT(babinfo->chip_conf[i]));
-			bab_config_reg(BAB_FAST_REG, BAB_FAST_BIT(babinfo->chip_conf[i]));
-			bab_config_reg(BAB_DIV2_REG, BAB_DIV2_BIT(babinfo->chip_conf[i]));
-			bab_config_reg(BAB_SLOW_REG, BAB_SLOW_BIT(babinfo->chip_conf[i]));
-			bab_config_reg(BAB_OCLK_REG, BAB_OCLK_BIT(babinfo->chip_conf[i]));
+			bab_add_data(sitem, BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
+			bab_config_reg(sitem, BAB_ICLK_REG, BAB_ICLK_BIT(babinfo->chip_conf[i]));
+			bab_config_reg(sitem, BAB_FAST_REG, BAB_FAST_BIT(babinfo->chip_conf[i]));
+			bab_config_reg(sitem, BAB_DIV2_REG, BAB_DIV2_BIT(babinfo->chip_conf[i]));
+			bab_config_reg(sitem, BAB_SLOW_REG, BAB_SLOW_BIT(babinfo->chip_conf[i]));
+			bab_config_reg(sitem, BAB_OCLK_REG, BAB_OCLK_BIT(babinfo->chip_conf[i]));
 			for (reg = BAB_REG_CLR_FROM; reg <= BAB_REG_CLR_TO; reg++)
-				bab_config_reg(reg, false);
+				bab_config_reg(sitem, reg, false);
 			if (babinfo->chip_conf[i]) {
-				bab_add_data(BAB_COUNT_ADDR, bab_counters, sizeof(bab_counters));
-				bab_add_data(BAB_W1A_ADDR, bab_w1, sizeof(bab_w1));
-				bab_add_data(BAB_W1B_ADDR, bab_w1, sizeof(bab_w1)/2);
-				bab_add_data(BAB_W2_ADDR, bab_w2, sizeof(bab_w2));
+				bab_add_data(sitem, BAB_COUNT_ADDR, bab_counters, sizeof(bab_counters));
+				bab_add_data(sitem, BAB_W1A_ADDR, bab_w1, sizeof(bab_w1));
+				bab_add_data(sitem, BAB_W1B_ADDR, bab_w1, sizeof(bab_w1)/2);
+				bab_add_data(sitem, BAB_W2_ADDR, bab_w2, sizeof(bab_w2));
 				babinfo->chip_conf[i] ^= BAB_CFGD_VAL;
 			}
 			babinfo->old_fast[i] = babinfo->chip_fast[i];
@@ -908,89 +813,93 @@ static bool bab_put(struct cgpu_info *babcgpu, struct bab_info *babinfo)
 		} else {
 			if (babinfo->old_fast[i] != babinfo->chip_fast[i]) {
 				bab_set_osc(babinfo, i);
-				bab_add_data(BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
+				bab_add_data(sitem, BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
 				babinfo->old_fast[i] = babinfo->chip_fast[i];
 			}
 			if (babinfo->old_conf[i] != babinfo->chip_conf[i]) {
 				if (BAB_ICLK_SET(babinfo->old_conf[i]) !=
 						 BAB_ICLK_SET(babinfo->chip_conf[i]))
-					bab_config_reg(BAB_ICLK_REG,
+					bab_config_reg(sitem, BAB_ICLK_REG,
 							BAB_ICLK_BIT(babinfo->chip_conf[i]));
 				if (BAB_FAST_SET(babinfo->old_conf[i]) !=
 						 BAB_FAST_SET(babinfo->chip_conf[i]))
-					bab_config_reg(BAB_FAST_REG,
+					bab_config_reg(sitem, BAB_FAST_REG,
 							BAB_FAST_BIT(babinfo->chip_conf[i]));
 				if (BAB_DIV2_SET(babinfo->old_conf[i]) !=
 						 BAB_DIV2_SET(babinfo->chip_conf[i]))
-					bab_config_reg(BAB_DIV2_REG,
+					bab_config_reg(sitem, BAB_DIV2_REG,
 							BAB_DIV2_BIT(babinfo->chip_conf[i]));
 				if (BAB_SLOW_SET(babinfo->old_conf[i]) !=
 						 BAB_SLOW_SET(babinfo->chip_conf[i]))
-					bab_config_reg(BAB_SLOW_REG,
+					bab_config_reg(sitem, BAB_SLOW_REG,
 							BAB_SLOW_BIT(babinfo->chip_conf[i]));
 				if (BAB_OCLK_SET(babinfo->old_conf[i]) !=
 						 BAB_OCLK_SET(babinfo->chip_conf[i]))
-					bab_config_reg(BAB_OCLK_REG,
+					bab_config_reg(sitem, BAB_OCLK_REG,
 							BAB_OCLK_BIT(babinfo->chip_conf[i]));
 				babinfo->old_conf[i] = babinfo->chip_conf[i];
 			}
 		}
-		babinfo->chip_off[buf][i] = babinfo->buf_used[buf] + 3;
+		DATAS(sitem)->chip_off[i] = DATAS(sitem)->siz + 3;
+		chip_input = &(DATAW(DATAS(sitem)->witems[i])->chip_input);
+		// Would no-ops need to be added if a chip gets disabled?
 		if (babinfo->chip_conf[i])
-			bab_add_data(BAB_INP_ADDR, (uint8_t *)(&(babinfo->chip_input[i])),
-					sizeof(babinfo->chip_input[i]));
+			bab_add_data(sitem, BAB_INP_ADDR, (uint8_t *)chip_input, sizeof(*chip_input));
+
+		chip_siz = DATAS(sitem)->siz - babinfo->chip_conf[i];
+		if (chip_siz < BAB_CHIP_MIN)
+			BAB_ADD_NOOPs(sitem, BAB_CHIP_MIN - chip_siz);
 
-		BAB_ADD_ASYNC();
+		BAB_ADD_ASYNC(sitem);
 	}
-	babinfo->chip_off[buf][i] = babinfo->buf_used[buf];
-	babinfo->bank_off[buf][bank] = babinfo->buf_used[buf];
+	DATAS(sitem)->chip_off[i] = DATAS(sitem)->siz;
+	DATAS(sitem)->bank_off[bank] = DATAS(sitem)->siz;
 
-	mutex_lock(&(babinfo->spi_lock));
-	babinfo->buf_status[buf] = BAB_STATE_READY;
-	mutex_unlock(&(babinfo->spi_lock));
+	K_WLOCK(babinfo->spi_list);
+	k_add_head(babinfo->spi_list, sitem);
+	K_WUNLOCK(babinfo->spi_list);
+
+	cgsem_post(&(babinfo->spi_work));
 
 	babinfo->fixchip = (babinfo->fixchip + 1) % babinfo->chips;
-	return true;
 }
 
-static bool bab_get(__maybe_unused struct cgpu_info *babcgpu, struct bab_info *babinfo)
+static bool bab_get(__maybe_unused struct cgpu_info *babcgpu, struct bab_info *babinfo, struct timeval *when)
 {
-	int buf, i;
+	K_ITEM *item;
+	int i;
 
-	babinfo->buffer = -1;
+	cgsem_mswait(&(babinfo->spi_reply), babinfo->reply_wait);
 
-	mutex_lock(&(babinfo->spi_lock));
-	if (babinfo->buf_status[0] == BAB_STATE_SENT) {
-		babinfo->buf_status[0] = BAB_STATE_READING;
-		babinfo->buffer = 0;
-	} else if (babinfo->buf_status[1] == BAB_STATE_SENT) {
-			babinfo->buf_status[1] = BAB_STATE_READING;
-			babinfo->buffer = 1;
-	}
-	mutex_unlock(&(babinfo->spi_lock));
+	K_WLOCK(babinfo->spi_sent);
+	item = k_unlink_tail(babinfo->spi_sent);
+	K_WUNLOCK(babinfo->spi_sent);
 
-	if (babinfo->buffer == -1)
+	if (!item)
 		return false;
 
-	buf = babinfo->buffer;
 	for (i = 0; i < babinfo->chips; i++) {
 		if (babinfo->chip_conf[i] & 0x7f) {
 			memcpy((void *)&(babinfo->chip_results[i]),
-				(void *)(babinfo->buf_read[buf] + babinfo->chip_off[buf][i]),
+				(void *)(DATAS(item)->rbuf + DATAS(item)->chip_off[i]),
 				sizeof(babinfo->chip_results[0]));
 		}
 	}
 
-	mutex_lock(&(babinfo->spi_lock));
-	babinfo->buf_status[buf] = BAB_STATE_DONE;
-	mutex_unlock(&(babinfo->spi_lock));
+	// work_start is also the time the results were read
+	memcpy(when, &(DATAS(item)->work_start), sizeof(*when));
+
+	K_WLOCK(babinfo->sfree_list);
+	k_add_head(babinfo->sfree_list, item);
+	K_WUNLOCK(babinfo->sfree_list);
 
 	return true;
 }
 
 void bab_detect_chips(struct cgpu_info *babcgpu, struct bab_info *babinfo, int bank, int first, int last)
 {
-	int buf, i, reg, j;
+	int i, reg, j;
+	K_ITEM *item;
 
 	if (sizeof(struct bab_work_send) != sizeof(bab_test_data)) {
 		quithere(1, "struct bab_work_send (%d) and bab_test_data (%d)"
@@ -999,58 +908,55 @@ void bab_detect_chips(struct cgpu_info *babcgpu, struct bab_info *babinfo, int b
 			    (int)sizeof(bab_test_data));
 	}
 
-	memset(babinfo->bank_off, 0, sizeof(babinfo->bank_off));
-
-	buf = babinfo->buffer = 0;
-	babinfo->buf_used[buf] = 0;
-	BAB_ADD_BREAK();
+	K_WLOCK(babinfo->sfree_list);
+	item = k_unlink_head_zero(babinfo->sfree_list);
+	K_WUNLOCK(babinfo->sfree_list);
+	BAB_ADD_BREAK(item);
 	for (i = first; i < last && i < BAB_MAXCHIPS; i++) {
 		bab_set_osc(babinfo, i);
-		bab_add_data(BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
-		bab_config_reg(BAB_ICLK_REG, BAB_ICLK_BIT(babinfo->chip_conf[i]));
-		bab_config_reg(BAB_FAST_REG, BAB_FAST_BIT(babinfo->chip_conf[i]));
-		bab_config_reg(BAB_DIV2_REG, BAB_DIV2_BIT(babinfo->chip_conf[i]));
-		bab_config_reg(BAB_SLOW_REG, BAB_SLOW_BIT(babinfo->chip_conf[i]));
-		bab_config_reg(BAB_OCLK_REG, BAB_OCLK_BIT(babinfo->chip_conf[i]));
+		bab_add_data(item, BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
+		bab_config_reg(item, BAB_ICLK_REG, BAB_ICLK_BIT(babinfo->chip_conf[i]));
+		bab_config_reg(item, BAB_FAST_REG, BAB_FAST_BIT(babinfo->chip_conf[i]));
+		bab_config_reg(item, BAB_DIV2_REG, BAB_DIV2_BIT(babinfo->chip_conf[i]));
+		bab_config_reg(item, BAB_SLOW_REG, BAB_SLOW_BIT(babinfo->chip_conf[i]));
+		bab_config_reg(item, BAB_OCLK_REG, BAB_OCLK_BIT(babinfo->chip_conf[i]));
 		for (reg = BAB_REG_CLR_FROM; reg <= BAB_REG_CLR_TO; reg++)
-			bab_config_reg(reg, false);
-		bab_add_data(BAB_COUNT_ADDR, bab_counters, sizeof(bab_counters));
-		bab_add_data(BAB_W1A_ADDR, bab_w1, sizeof(bab_w1));
-		bab_add_data(BAB_W1B_ADDR, bab_w1, sizeof(bab_w1)/2);
-		bab_add_data(BAB_W2_ADDR, bab_w2, sizeof(bab_w2));
-		babinfo->chip_off[buf][i] = babinfo->buf_used[buf] + 3;
-		bab_add_data(BAB_INP_ADDR, bab_test_data, sizeof(bab_test_data));
-		babinfo->chip_off[buf][i+1] = babinfo->buf_used[buf];
-		babinfo->bank_off[buf][bank] = babinfo->buf_used[buf];
+			bab_config_reg(item, reg, false);
+		bab_add_data(item, BAB_COUNT_ADDR, bab_counters, sizeof(bab_counters));
+		bab_add_data(item, BAB_W1A_ADDR, bab_w1, sizeof(bab_w1));
+		bab_add_data(item, BAB_W1B_ADDR, bab_w1, sizeof(bab_w1)/2);
+		bab_add_data(item, BAB_W2_ADDR, bab_w2, sizeof(bab_w2));
+		DATAS(item)->chip_off[i] = DATAS(item)->siz + 3;
+		bab_add_data(item, BAB_INP_ADDR, bab_test_data, sizeof(bab_test_data));
+		DATAS(item)->chip_off[i+1] = DATAS(item)->siz;
+		DATAS(item)->bank_off[bank] = DATAS(item)->siz;
 		babinfo->chips = i + 1;
-		bab_txrx(buf, babinfo->buf_used[buf], false);
-		babinfo->buf_used[buf] = 0;
-		BAB_ADD_BREAK();
+		bab_txrx(item, false);
+		DATAS(item)->siz = 0;
+		BAB_ADD_BREAK(item);
 		for (j = first; j <= i; j++) {
-			babinfo->chip_off[buf][j] = babinfo->buf_used[buf] + 3;
-			BAB_ADD_ASYNC();
+			DATAS(item)->chip_off[j] = DATAS(item)->siz + 3;
+			BAB_ADD_ASYNC(item);
 		}
 	}
 
-	buf = babinfo->buffer = 1;
-	babinfo->buf_used[buf] = 0;
-	BAB_ADD_BREAK();
+	memset(item->data, 0, babinfo->sfree_list->siz);
+	BAB_ADD_BREAK(item);
 	for (i = first; i < last && i < BAB_MAXCHIPS; i++) {
-		babinfo->chip_off[buf][i] = babinfo->buf_used[buf] + 3;
-		bab_add_data(BAB_INP_ADDR, bab_test_data, sizeof(bab_test_data));
-		BAB_ADD_ASYNC();
+		DATAS(item)->chip_off[i] = DATAS(item)->siz + 3;
+		bab_add_data(item, BAB_INP_ADDR, bab_test_data, sizeof(bab_test_data));
+		BAB_ADD_ASYNC(item);
 	}
-	babinfo->chip_off[buf][i] = babinfo->buf_used[buf];
-	babinfo->bank_off[buf][bank] = babinfo->buf_used[buf];
+	DATAS(item)->chip_off[i] = DATAS(item)->siz;
+	DATAS(item)->bank_off[bank] = DATAS(item)->siz;
 	babinfo->chips = i;
-	bab_txrx(buf, babinfo->buf_used[buf], true);
-	babinfo->buf_used[buf] = 0;
+	bab_txrx(item, true);
+	DATAS(item)->siz = 0;
 	babinfo->chips = first;
 	for (i = first; i < last && i < BAB_MAXCHIPS; i++) {
 		uint32_t tmp[DATA_UINTS-1];
-		memcpy(tmp, babinfo->buf_read[buf]+babinfo->chip_off[buf][i], sizeof(tmp));
-		for (j = 0; j < BAB_SPI_BUFFERS; j++)
-			babinfo->chip_off[j][i] = 0;
+		memcpy(tmp, DATAS(item)->rbuf + DATAS(item)->chip_off[i], sizeof(tmp));
+		DATAS(item)->chip_off[i] = 0;
 		for (j = 0; j < BAB_REPLY_NONCES; j++) {
 			if (tmp[j] != 0xffffffff && tmp[j] != 0x00000000) {
 				babinfo->chip_bank[i] = bank;
@@ -1061,6 +967,10 @@ void bab_detect_chips(struct cgpu_info *babcgpu, struct bab_info *babinfo, int b
 	}
 	for (i = first ; i < babinfo->chips; i++)
 		babinfo->chip_bank[i] = bank;
+
+	K_WLOCK(babinfo->sfree_list);
+	k_add_head(babinfo->sfree_list, item);
+	K_WUNLOCK(babinfo->sfree_list);
 }
 
 static const char *bab_modules[] = {
@@ -1075,6 +985,7 @@ static const char *bab_memory = "/dev/mem";
 
 static int bab_memory_addr = 0x20200000;
 
+// TODO: add --bab-options for SPEED_HZ, tran.delay_usecs and an inter transfer delay (default 0)
 static struct {
 	int request;
 	int value;
@@ -1167,7 +1078,73 @@ bad_out:
 
 static void bab_init_chips(struct cgpu_info *babcgpu, struct bab_info *babinfo)
 {
-	bab_detect_chips(babcgpu, babinfo, 0, 0, BAB_MAXCHIPS);
+	int chip, chipoff, bank, chips, new_chips, boards, mis;
+
+	applog(LOG_WARNING, "%s V1 first test for %d chips ...",
+			    babcgpu->drv->dname, BAB_V1_CHIP_TEST);
+
+	bab_detect_chips(babcgpu, babinfo, 0, 0, BAB_V1_CHIP_TEST);
+	if (babinfo->chips > 0) {
+		babinfo->version = 1;
+		babinfo->banks = 0;
+		if (babinfo->chips == BAB_V1_CHIP_TEST) {
+			applog(LOG_WARNING, "%s V1 test for %d more chips ...",
+					    babcgpu->drv->dname, BAB_MAXCHIPS - BAB_V1_CHIP_TEST);
+
+			bab_detect_chips(babcgpu, babinfo, 0, BAB_V1_CHIP_TEST, BAB_MAXCHIPS);
+		}
+		babinfo->chips_per_bank[BAB_V1_BANK] = babinfo->chips;
+		babinfo->boards = (int)((float)(babinfo->chips - 1) / BAB_BOARDCHIPS) + 1;
+		babinfo->reply_wait = BAB_REPLY_WAIT_mS * 2;
+
+		if ((chip = (babinfo->chips_per_bank[BAB_V1_BANK] % BAB_BOARDCHIPS))) {
+			mis = BAB_BOARDCHIPS - chip;
+			babinfo->missing_chips_per_bank[BAB_V1_BANK] = mis;
+			applog(LOG_WARNING, "%s V1: missing %d chip%s",
+					    babcgpu->drv->dname, mis,
+					    (mis == 1) ? "" : "s");
+		}
+	} else {
+		applog(LOG_WARNING, "%s no chips found with V1", babcgpu->drv->dname);
+		applog(LOG_WARNING, "%s V2 test %d banks %d chips ...",
+				    babcgpu->drv->dname, BAB_MAXBANKS, BAB_MAXCHIPS);
+
+		chips = 0;
+		babinfo->version = 2;
+		babinfo->banks = 0;
+		for (bank = 1; bank <= BAB_MAXBANKS; bank++) {
+			for (chipoff = 0; chipoff < BAB_BANKCHIPS; chipoff++) {
+				chip = babinfo->chips + chipoff;
+				babinfo->chip_spis[chip] = 625000;
+			}
+			bab_reset(bank, 64);
+			bab_detect_chips(babcgpu, babinfo, bank, babinfo->chips, babinfo->chips + BAB_BANKCHIPS);
+			new_chips = babinfo->chips - chips;
+			babinfo->chips_per_bank[bank] = new_chips;
+			chips = babinfo->chips;
+			if (new_chips == 0)
+				boards = 0;
+			else {
+				boards = (int)((float)(new_chips - 1) / BAB_BOARDCHIPS) + 1;
+				babinfo->banks++;
+			}
+			applog(LOG_WARNING, "%s V2 bank %d: %d chips %d board%s",
+					    babcgpu->drv->dname, bank, new_chips,
+					    boards, (boards == 1) ? "" : "s");
+			babinfo->boards += boards;
+
+			if ((chip = (babinfo->chips_per_bank[bank] % BAB_BOARDCHIPS))) {
+				mis = BAB_BOARDCHIPS - chip;
+				babinfo->missing_chips_per_bank[bank] = mis;
+				applog(LOG_WARNING, "%s V2: bank %d missing %d chip%s",
+						    babcgpu->drv->dname, bank,
+						    mis, (mis == 1) ? "" : "s");
+			}
+		}
+		babinfo->reply_wait = BAB_REPLY_WAIT_mS * babinfo->banks;
+		bab_reset(0, 8);
+	}
+
 	memcpy(babinfo->old_conf, babinfo->chip_conf, sizeof(babinfo->old_conf));
 	memcpy(babinfo->old_fast, babinfo->chip_fast, sizeof(babinfo->old_fast));
 }
@@ -1199,16 +1176,25 @@ static void bab_detect(bool hotplug)
 		babinfo->chip_fast[i] = BAB_DEFSPEED;
 	}
 
-	mutex_init(&babinfo->spi_lock);
-
 	if (!bab_init_gpio(babcgpu, babinfo, BAB_SPI_BUS, BAB_SPI_CHIP))
 		goto unalloc;
 
-	applog(LOG_WARNING, "%s V1 testing for %d chips ...", babcgpu->drv->dname, BAB_MAXCHIPS);
+	babinfo->sfree_list = k_new_list("SPI I/O", sizeof(SITEM),
+					 ALLOC_SITEMS, LIMIT_SITEMS, true);
+	babinfo->spi_list = k_new_store(babinfo->sfree_list);
+	babinfo->spi_sent = k_new_store(babinfo->sfree_list);
 
 	bab_init_chips(babcgpu, babinfo);
 
-	applog(LOG_WARNING, "%s found %d chips", babcgpu->drv->dname, babinfo->chips);
+	if (babinfo->boards) {
+		applog(LOG_WARNING, "%s found %d chips %d board%s",
+				    babcgpu->drv->dname, babinfo->chips,
+				    babinfo->boards,
+				    (babinfo->boards == 1) ? "" : "s");
+	} else {
+		applog(LOG_WARNING, "%s found %d chips",
+				    babcgpu->drv->dname, babinfo->chips);
+	}
 
 	if (babinfo->chips == 0)
 		goto cleanup;
@@ -1216,9 +1202,35 @@ static void bab_detect(bool hotplug)
 	if (!add_cgpu(babcgpu))
 		goto cleanup;
 
-	mutex_init(&babinfo->res_lock);
+	cgsem_init(&(babinfo->scan_work));
+	cgsem_init(&(babinfo->spi_work));
+	cgsem_init(&(babinfo->spi_reply));
+	cgsem_init(&(babinfo->process_reply));
+
 	mutex_init(&babinfo->did_lock);
-	cglock_init(&babinfo->blist_lock);
+	mutex_init(&babinfo->nonce_lock);
+
+	babinfo->rfree_list = k_new_list("Results", sizeof(RITEM),
+					 ALLOC_RITEMS, LIMIT_RITEMS, true);
+	babinfo->res_list = k_new_store(babinfo->rfree_list);
+
+	babinfo->wfree_list = k_new_list("Work", sizeof(WITEM),
+					 ALLOC_WITEMS, LIMIT_WITEMS, true);
+	babinfo->available_work = k_new_store(babinfo->wfree_list);
+	for (i = 0; i < BAB_MAXCHIPS; i++)
+		babinfo->chip_work[i] = k_new_store(babinfo->wfree_list);
+
+	babinfo->nfree_list = k_new_list("Nonce History", sizeof(WITEM),
+					 ALLOC_NITEMS, LIMIT_NITEMS, true);
+	for (i = 0; i < BAB_MAXCHIPS; i++) {
+		babinfo->good_nonces[i] = k_new_store(babinfo->nfree_list);
+		babinfo->bad_nonces[i] = k_new_store(babinfo->nfree_list);
+	}
+
+	// Exclude detection
+	cgtime(&(babcgpu->dev_start_tv));
+	// Ignore detection tests
+	babinfo->last_did.tv_sec = 0;
 
 	babinfo->initialised = true;
 
@@ -1228,7 +1240,6 @@ cleanup:
 	close(babinfo->spifd);
 	munmap((void *)(babinfo->gpio), BAB_SPI_BUFSIZ);
 unalloc:
-	mutex_destroy(&babinfo->spi_lock);
 	free(babinfo);
 	free(babcgpu);
 }
@@ -1237,19 +1248,15 @@ static void bab_identify(__maybe_unused struct cgpu_info *babcgpu)
 {
 }
 
-#define BAB_LONG_WAIT_uS 1200000
-#define BAB_WAIT_MSG_EVERY 10
-#define BAB_LONG_WAIT_SLEEP_uS 100000
-#define BAB_STD_WAIT_uS 3000
-
 // thread to do spi txrx
 static void *bab_spi(void *userdata)
 {
 	struct cgpu_info *babcgpu = (struct cgpu_info *)userdata;
 	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
-	struct timeval start, stop;
-	double wait;
-	int i, buf, msgs;
+	struct timeval start, stop, send, now;
+	K_ITEM *sitem, *witem;
+	double wait, delay;
+	int chip, band;
 
 	applog(LOG_DEBUG, "%s%i: SPIing...",
 			  babcgpu->drv->name, babcgpu->device_id);
@@ -1262,51 +1269,92 @@ static void *bab_spi(void *userdata)
 		cgsleep_ms(3);
 	}
 
-	msgs = 0;
 	cgtime(&start);
 	while (babcgpu->shutdown == false) {
-		buf = -1;
-		mutex_lock(&(babinfo->spi_lock));
-		for (i = 0; i < BAB_SPI_BUFFERS; i++) {
-			if (babinfo->buf_status[i] == BAB_STATE_READY) {
-				babinfo->buf_status[i] = BAB_STATE_SENDING;
-				buf = i;
-				cgtime(&start);
-				break;
-			}
-		}
-		mutex_unlock(&(babinfo->spi_lock));
+		K_WLOCK(babinfo->spi_list);
+		sitem = k_unlink_tail(babinfo->spi_list);
+		K_WUNLOCK(babinfo->spi_list);
 
-		if (buf == -1) {
+		if (!sitem) {
 			cgtime(&stop);
 			wait = us_tdiff(&stop, &start);
-			if (wait > BAB_LONG_WAIT_uS) {
-				if ((msgs++ % BAB_WAIT_MSG_EVERY) == 0) {
-					applog(LOG_WARNING, "%s%i: SPI waiting %.0fus ...",
-								babcgpu->drv->name,
-								babcgpu->device_id,
-								(float)wait);
-				}
-			}
-			cgsleep_us(BAB_LONG_WAIT_SLEEP_uS);
+			if (wait > BAB_LONG_uS) {
+				applog(LOG_WARNING, "%s%i: SPI waiting %fs ...",
+							babcgpu->drv->name,
+							babcgpu->device_id,
+							(float)wait / 1000000.0);
+				cgsem_mswait(&(babinfo->spi_work), BAB_LONG_WAIT_mS);
+			} else
+				cgsem_mswait(&(babinfo->spi_work), (int)((BAB_LONG_uS - wait) / 1000));
 			continue;
 		}
 
-		bab_txrx(buf, babinfo->buf_used[buf], false);
-		cgtime(&stop);
-		wait = us_tdiff(&stop, &start);
-		if (wait < BAB_STD_WAIT_uS)
-			cgsleep_us((uint64_t)(BAB_STD_WAIT_uS - wait));
-		else if (wait > BAB_LONG_WAIT_uS) {
-			applog(LOG_DEBUG, "%s%i: SPI waited %.0fus",
-					  babcgpu->drv->name, babcgpu->device_id,
-					  (float)wait);
+		// TODO: need an LP/urgent flag to skip this possible cgsem_mswait()
+		// maybe zero last_sent_work.tv_sec ?
+		while (babinfo->last_sent_work.tv_sec) {
+			cgtime(&now);
+			delay = tdiff(&now, &(babinfo->last_sent_work)) * 1000.0;
+			if (delay < BAB_EXPECTED_WORK_DELAY_mS)
+				cgsem_mswait(&(babinfo->spi_work), BAB_EXPECTED_WORK_DELAY_mS - delay);
+			else
+				break;
 		}
 
-		mutex_lock(&(babinfo->spi_lock));
-		babinfo->buf_status[i] = BAB_STATE_SENT;
-		mutex_unlock(&(babinfo->spi_lock));
-		msgs = 0;
+		/*
+		 * TODO: handle if an LP happened after bab_do_work() started
+		 * i.e. we don't want to send the work
+		 * Have an LP counter that at this point would show the work
+		 * is stale - so don't send it
+		 */
+		cgtime(&send);
+		bab_txrx(sitem, false);
+		cgtime(&start);
+
+		// The work isn't added to the chip until it has been sent
+		K_WLOCK(babinfo->wfree_list);
+		for (chip = 0; chip < babinfo->chips; chip++) {
+			witem = DATAS(sitem)->witems[chip];
+			if (witem) {
+				memcpy(&(DATAW(witem)->work_start), &(DATAS(sitem)->work_start),
+					sizeof(DATAW(witem)->work_start));
+				k_add_head(babinfo->chip_work[chip], witem);
+			}
+		}
+		K_WUNLOCK(babinfo->wfree_list);
+
+		K_WLOCK(babinfo->spi_sent);
+		k_add_head(babinfo->spi_sent, sitem);
+		K_WUNLOCK(babinfo->spi_sent);
+
+		cgsem_post(&(babinfo->spi_reply));
+
+		// Store stats
+		if (babinfo->last_sent_work.tv_sec) {
+			delay = tdiff(&send, &(babinfo->last_sent_work));
+			babinfo->delay_count++;
+			if (babinfo->delay_min == 0 || babinfo->delay_min > delay)
+				babinfo->delay_min = delay;
+			if (babinfo->delay_max < delay)
+				babinfo->delay_max = delay;
+			if (delay < BAB_DELAY_BASE)
+				band = 0;
+			else if (delay >= (BAB_DELAY_BASE+BAB_DELAY_STEP*(BAB_DELAY_BANDS+1)))
+				band = BAB_DELAY_BANDS+1;
+			else
+				band = (int)(((double)delay - BAB_DELAY_BASE) / BAB_DELAY_STEP) + 1;
+			babinfo->delay_bands[band]++;
+		}
+		memcpy(&(babinfo->last_sent_work), &send, sizeof(start));
+
+		delay = tdiff(&start, &send);
+		babinfo->send_count++;
+		babinfo->send_total += delay;
+		if (babinfo->send_min == 0 || babinfo->send_min > delay)
+			babinfo->send_min = delay;
+		if (babinfo->send_max < delay)
+			babinfo->send_max = delay;
+
+		cgsem_mswait(&(babinfo->spi_work), BAB_STD_WAIT_mS);
 	}
 
 	return NULL;
@@ -1320,8 +1368,10 @@ static void bab_flush_work(struct cgpu_info *babcgpu)
 			  babcgpu->drv->name, babcgpu->device_id);
 
 	mutex_lock(&(babinfo->did_lock));
-	memset(&(babinfo->last_did), 0, sizeof(babinfo->last_did));
+	babinfo->last_did.tv_sec = 0;
 	mutex_unlock(&(babinfo->did_lock));
+
+	cgsem_post(&(babinfo->scan_work));
 }
 
 #define DATA_MERKLE7 16
@@ -1334,94 +1384,228 @@ static void bab_flush_work(struct cgpu_info *babcgpu)
 #define WORK_BITS (18*4)
 #define WORK_NONCE (19*4)
 
+#define UPDATE_HISTORY 1
+
+#if UPDATE_HISTORY
+static void process_history(struct bab_info *babinfo, int chip, struct timeval *when, bool good, struct timeval *now)
+{
+	K_ITEM *item;
+	double diff;
+	int i;
+
+	K_WLOCK(babinfo->nfree_list);
+	item = k_unlink_head(babinfo->nfree_list);
+	memcpy(&(DATAN(item)->found), when, sizeof(*when));
+	if (good)
+		k_add_head(babinfo->good_nonces[chip], item);
+	else
+		k_add_head(babinfo->bad_nonces[chip], item);
+
+	// Remove all expired history
+	for (i = 0; i < babinfo->chips; i++) {
+		item = babinfo->good_nonces[i]->tail;
+		while (item) {
+			diff = tdiff(now, &(DATAN(item)->found));
+			if (diff < HISTORY_TIME_S)
+				break;
+
+			k_unlink_item(babinfo->good_nonces[i], item);
+			k_add_head(babinfo->nfree_list, item);
+
+			item = babinfo->good_nonces[i]->tail;
+		}
+
+		item = babinfo->bad_nonces[i]->tail;
+		while (item) {
+			diff = tdiff(now, &(DATAN(item)->found));
+			if (diff < HISTORY_TIME_S)
+				break;
+
+			k_unlink_item(babinfo->bad_nonces[i], item);
+			k_add_head(babinfo->nfree_list, item);
+
+			item = babinfo->bad_nonces[i]->tail;
+		}
+	}
+	K_WUNLOCK(babinfo->nfree_list);
+}
+#endif
+
 /*
- * Find the matching work item by checking the nonce against each work
- * item for the chip
- * Discard any work items older than a match
+ * Find the matching work item by checking each nonce against
+ * work items for the nonces chip
  */
-static bool oknonce(struct thr_info *thr, struct cgpu_info *babcgpu, int chip, uint32_t nonce)
+static K_ITEM *process_nonce(struct thr_info *thr, struct cgpu_info *babcgpu, K_ITEM *ritem, uint32_t raw_nonce, K_ITEM *newest_witem)
 {
 	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
-	BLIST *bitem;
-	unsigned int links, tests;
-	int i;
+	unsigned int links, proc_links, work_links, tests;
+	int try_sta, try_fin, offset;
+	K_ITEM *witem, *wtail;
+	struct timeval now;
+	bool not_first_reply;
+	uint32_t nonce;
+	int chip;
 
-	babinfo->chip_nonces[chip]++;
+	chip = DATAR(ritem)->chip;
+	not_first_reply = DATAR(ritem)->not_first_reply;
 
-	nonce = decnonce(nonce);
+	babinfo->chip_nonces[chip]++;
 
 	/*
-	 * We can grab the head of the chip work queue and then
-	 * release the lock and follow it to the end
-	 * since the other thread will only add items above the
-	 * head - it wont touch the list->next pointers from the
-	 * head to the end - only the head->prev pointer may get
-	 * changed
+	 * We can grab the head of the chip work queue and then release
+	 * the lock and follow it to the end and back, since the other
+	 * thread will only add items above the head - it wont touch
+	 * any of the prev/next pointers from the head to the end -
+	 * except the head->prev pointer may get changed
 	 */
-	cg_rlock(&babinfo->blist_lock);
-	bitem = babinfo->chip_list[chip];
-	cg_runlock(&babinfo->blist_lock);
+	K_RLOCK(babinfo->chip_work[chip]);
+	witem = babinfo->chip_work[chip]->head;
+	K_RUNLOCK(babinfo->chip_work[chip]);
 
-	if (!bitem) {
-		applog(LOG_ERR, "%s%i: chip %d has no work!",
+	if (!witem) {
+		applog(LOG_ERR, "%s%i: chip %d has no work, 1 nonce discarded!",
 				babcgpu->drv->name, babcgpu->device_id, chip);
 		babinfo->untested_nonces++;
-		return false;
+		return newest_witem;
 	}
 
 	babinfo->tested_nonces++;
 
-	tests = 0;
-	links = 0;
-	while (bitem) {
-		if (!bitem->work) {
-			applog(LOG_ERR, "%s%i: chip %d bitem links %d has no work!",
+	if ((raw_nonce & 0xff) < 0x1c) {
+		// Will only be this offset
+		try_sta = BAB_OFF_0x1C_STA;
+		try_fin = BAB_OFF_0x1C_FIN;
+	} else {
+		// Will only be one of the other offsets
+		try_sta = BAB_OFF_OTHER_STA;
+		try_fin = BAB_OFF_OTHER_FIN;
+	}
+
+	nonce = decnonce(raw_nonce);
+
+	cgtime(&now);
+
+	tests = links = proc_links = work_links = 0;
+	wtail = witem;
+	while (wtail && wtail->next) {
+		work_links++;
+		wtail = wtail->next;
+	}
+	while (wtail) {
+		if (!(DATAW(wtail)->work)) {
+			applog(LOG_ERR, "%s%i: chip %d witem links %d has no work!",
 					babcgpu->drv->name,
 					babcgpu->device_id,
 					chip, links);
 		} else {
-			for (i = 0; i < BAB_NONCE_OFFSETS; i++) {
-				tests++;
-				if (test_nonce(bitem->work, nonce + bab_nonce_offsets[i])) {
-					submit_tested_work(thr, bitem->work);
-					babinfo->nonce_offset_count[i]++;
-					babinfo->chip_good[chip]++;
-					bitem->nonces++;
-					babinfo->new_nonces++;
-					babinfo->ok_nonces++;
-					free_blist(babcgpu, bitem->next, chip);
-					babinfo->total_tests += tests;
-					if (babinfo->max_tests_per_nonce < tests)
-						babinfo->max_tests_per_nonce = tests;
-					babinfo->total_links += links;
-					if (babinfo->max_links < links)
-						babinfo->max_links = links;
-					return true;
+			if (ms_tdiff(&now, &(DATAW(wtail)->work_start)) >= BAB_WORK_EXPIRE_mS)
+				proc_links--;
+			else {
+				for (offset = try_sta; offset <= try_fin; offset++) {
+					tests++;
+					if (test_nonce(DATAW(wtail)->work, nonce + bab_nonce_offsets[offset])) {
+						submit_tested_work(thr, DATAW(wtail)->work);
+						babinfo->nonce_offset_count[offset]++;
+						babinfo->chip_good[chip]++;
+						DATAW(wtail)->nonces++;
+
+						mutex_lock(&(babinfo->nonce_lock));
+						babinfo->new_nonces++;
+						mutex_unlock(&(babinfo->nonce_lock));
+
+						babinfo->ok_nonces++;
+						babinfo->total_tests += tests;
+						if (babinfo->max_tests_per_nonce < tests)
+							babinfo->max_tests_per_nonce = tests;
+						babinfo->total_links += links;
+						babinfo->total_proc_links += proc_links;
+						if (babinfo->max_links < links)
+							babinfo->max_links = links;
+						if (babinfo->max_proc_links < proc_links)
+							babinfo->max_proc_links = proc_links;
+						babinfo->total_work_links += work_links;
+
+						babinfo->chip_cont_bad[chip] = 0;
+#if UPDATE_HISTORY
+						process_history(babinfo, chip,
+								&(DATAR(ritem)->when),
+								true, &now);
+#endif
+
+						if (newest_witem == NULL ||
+						    ms_tdiff(&(DATAW(wtail)->work_start),
+							&(DATAW(newest_witem)->work_start)) < 0)
+								return wtail;
+
+						return newest_witem;
+					}
 				}
 			}
 		}
-		bitem = bitem->next;
+		if (wtail == witem)
+			break;
+		wtail = wtail->prev;
 		links++;
+		proc_links++;
 	}
 
-	if (babinfo->not_first_reply[chip]) {
+	if (not_first_reply) {
 		babinfo->chip_bad[chip]++;
 		inc_hw_errors(thr);
-	} else
+
+		babinfo->fail++;
+		babinfo->fail_total_tests += tests;
+		babinfo->fail_total_links += links;
+		babinfo->fail_total_work_links += work_links;
+
+		babinfo->chip_cont_bad[chip]++;
+		if (babinfo->chip_max_bad[chip] < babinfo->chip_cont_bad[chip])
+			babinfo->chip_max_bad[chip] = babinfo->chip_cont_bad[chip];
+
+#if UPDATE_HISTORY
+		process_history(babinfo, chip, &(DATAR(ritem)->when), false, &now);
+#endif
+	} else {
 		babinfo->initial_ignored++;
+		babinfo->ign_total_tests += tests;
+		babinfo->ign_total_links += links;
+		babinfo->ign_total_work_links += work_links;
+	}
 
-	return false;
+	return newest_witem;
+}
+
+/*
+ * On completion discard any work items older than BAB_WORK_EXPIRE_mS
+ * and any work items of the chip older than the work of the newest nonce work item
+ */
+static void oknonces(struct thr_info *thr, struct cgpu_info *babcgpu, K_ITEM *ritem)
+{
+	uint32_t raw_nonce;
+	K_ITEM *witem;
+	int nonces;
+
+	witem = NULL;
+
+	for (nonces = 0; nonces < DATAR(ritem)->nonces; nonces++) {
+		raw_nonce = DATAR(ritem)->nonce[nonces];
+
+		witem = process_nonce(thr, babcgpu, ritem, raw_nonce, witem);
+	}
+
+	cleanup_older(babcgpu, DATAR(ritem)->chip, witem);
 }
 
+// Check at least every ...
+#define BAB_RESULT_DELAY_mS 999
+
 // Results checking thread
 static void *bab_res(void *userdata)
 {
 	struct cgpu_info *babcgpu = (struct cgpu_info *)userdata;
 	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
 	struct thr_info *thr = babcgpu->thr[0];
-	bool first_second;
-	uint32_t nonce;
-	int chip;
+	K_ITEM *ritem;
 
 	applog(LOG_DEBUG, "%s%i: Results...",
 			  babcgpu->drv->name, babcgpu->device_id);
@@ -1434,59 +1618,109 @@ static void *bab_res(void *userdata)
 		cgsleep_ms(3);
 	}
 
+	ritem = NULL;
 	while (babcgpu->shutdown == false) {
-		if (!oldest_nonce(babcgpu, &chip, &nonce, &first_second)) {
-			cgsleep_ms(3);
-			continue;
+		K_WLOCK(babinfo->res_list);
+		if (ritem) {
+			// Release the old one
+			k_add_head(babinfo->rfree_list, ritem);
+			ritem = NULL;
 		}
+		// Check for a new one
+		ritem = k_unlink_tail(babinfo->res_list);
+		K_WUNLOCK(babinfo->res_list);
 
-		if (first_second)
-			babinfo->not_first_reply[chip] = true;
+		if (!ritem) {
+			cgsem_mswait(&(babinfo->process_reply), BAB_RESULT_DELAY_mS);
+			continue;
+		}
 
-		oknonce(thr, babcgpu, chip, nonce);
+		oknonces(thr, babcgpu, ritem);
 	}
 
 	return NULL;
 }
 
+/*
+ * 1.0s per nonce = 4.2GH/s
+ * 0.9s per nonce = 4.8GH/s
+ * On a slow machine, reducing this may resolve:
+ *  BaB0: SPI waiting 1.2...s
+ */
+#define BAB_STD_WORK_DELAY_uS 900000
+
 static bool bab_do_work(struct cgpu_info *babcgpu)
 {
 	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
-	int busy, newbusy, match, work_items = 0;
-	int spi, mis, miso;
-	int i, j;
-	BLIST *bitem;
-	bool res, got_a_nonce;
+	int work_items = 0;
+	K_ITEM *witem, *sitem, *ritem;
+	struct timeval when, now;
+	double delay;
+	int chip, rep, j, nonces;
+	uint32_t nonce;
+	bool res;
 
-	for (i = 0; i < babinfo->chips; i++) {
-		bitem = next_work(babcgpu, i);
-		if (!bitem) {
-			applog(LOG_ERR, "%s%i: short work list (%i) expected %d - discarded",
+	cgtime(&now);
+	mutex_lock(&(babinfo->did_lock));
+	delay = us_tdiff(&now, &(babinfo->last_did));
+	mutex_unlock(&(babinfo->did_lock));
+	if (delay < BAB_STD_WORK_DELAY_uS)
+		return false;
+
+	K_WLOCK(babinfo->sfree_list);
+	sitem = k_unlink_head_zero(babinfo->sfree_list);
+	K_WUNLOCK(babinfo->sfree_list);
+
+	for (chip = 0; chip < babinfo->chips; chip++) {
+		// TODO: ignore stale work
+		K_WLOCK(babinfo->available_work);
+		witem = k_unlink_tail(babinfo->available_work);
+		K_WUNLOCK(babinfo->available_work);
+		if (!witem) {
+			applog(LOG_ERR, "%s%i: short work list (%d) expected %d - reset",
 					babcgpu->drv->name, babcgpu->device_id,
-					i, babinfo->chips);
-			for (j = 0; j < i; i++)
-				discard_last(babcgpu, j);
+					chip, babinfo->chips);
+
+			// Put them back in the order they were taken
+			K_WLOCK(babinfo->available_work);
+			for (j = chip-1; j >= 0; j--) {
+				witem = DATAS(sitem)->witems[j];
+				k_add_tail(babinfo->available_work, witem);
+			}
+			K_WUNLOCK(babinfo->available_work);
+
+			K_WLOCK(babinfo->sfree_list);
+			k_add_head(babinfo->sfree_list, sitem);
+			K_WUNLOCK(babinfo->sfree_list);
 
 			return false;
 		}
-		memcpy((void *)&(babinfo->chip_input[i].midstate[0]),
-			bitem->work->midstate, sizeof(bitem->work->midstate));
-		memcpy((void *)&(babinfo->chip_input[i].merkle7),
-			(void *)&(bitem->work->data[WORK_MERKLE7]), 12);
 
-		ms3steps((void *)&(babinfo->chip_input[i]));
+		/*
+		 * TODO: do this when we get work except on LP?
+		 * (not LP so we only do ms3steps for work required)
+		 * Though that may more likely trigger the applog(short work list) above?
+		 */
+		if (DATAW(witem)->ci_setup == false) {
+			memcpy((void *)&(DATAW(witem)->chip_input.midstate[0]),
+				DATAW(witem)->work->midstate, sizeof(DATAW(witem)->work->midstate));
+			memcpy((void *)&(DATAW(witem)->chip_input.merkle7),
+				(void *)&(DATAW(witem)->work->data[WORK_MERKLE7]), MERKLE_BYTES);
+
+			ms3steps((void *)&(DATAW(witem)->chip_input));
+
+			DATAW(witem)->ci_setup = true;
+		}
+
+		DATAS(sitem)->witems[chip] = witem;
 		work_items++;
 	}
 
 	// Send
-	res = bab_put(babcgpu, babinfo);
-	if (!res) {
-		applog(LOG_DEBUG, "%s%i: couldn't put work ...",
-				  babcgpu->drv->name, babcgpu->device_id);
-	}
+	bab_put(babinfo, sitem);
 
 	// Receive
-	res = bab_get(babcgpu, babinfo);
+	res = bab_get(babcgpu, babinfo, &when);
 	if (!res) {
 		applog(LOG_DEBUG, "%s%i: didn't get work reply ...",
 				  babcgpu->drv->name, babcgpu->device_id);
@@ -1496,79 +1730,46 @@ static bool bab_do_work(struct cgpu_info *babcgpu)
 	applog(LOG_DEBUG, "%s%i: Did get work reply ...",
 			  babcgpu->drv->name, babcgpu->device_id);
 
-	spi = mis = miso = 0;
-
-	for (i = 0; i < babinfo->chips; i++) {
-		match = 0;
-		newbusy = busy = babinfo->chip_busy[i];
-
-		if (!babinfo->chip_conf[i])
-			continue;
-
-		for (j = 1; j < BAB_REPLY_NONCES; j++) {
-			if (babinfo->chip_results[i].nonce[(busy+j) % BAB_REPLY_NONCES] !=
-			    babinfo->chip_prev[i].nonce[(busy+j) % BAB_REPLY_NONCES])
-				newbusy = (busy+j) % BAB_REPLY_NONCES;
-			else
-				match++;
-		}
-
-		if (!match) {
-			if (!miso) {
-				mis++;
-// ignore for now ...				babinfo->chip_miso[i]++;
+	for (chip = 0; chip < babinfo->chips; chip++) {
+		K_WLOCK(babinfo->rfree_list);
+		ritem = k_unlink_head(babinfo->rfree_list);
+		K_WUNLOCK(babinfo->rfree_list);
+
+		DATAR(ritem)->chip = chip;
+		DATAR(ritem)->not_first_reply = babinfo->not_first_reply[chip];
+		memcpy(&(DATAR(ritem)->when), &when, sizeof(when));
+
+		nonces = 0;
+		for (rep = 0; rep < BAB_REPLY_NONCES; rep++) {
+			nonce = babinfo->chip_results[chip].nonce[rep];
+			if (nonce != babinfo->chip_prev[chip].nonce[rep]) {
+				if ((nonce & BAB_EVIL_MASK) == BAB_EVIL_NONCE)
+					babinfo->discarded_e0s++;
+				else
+					DATAR(ritem)->nonce[nonces++] = nonce;
 			}
-			miso = 1;
-			continue;
 		}
 
-		miso = 0;
-		if (babinfo->chip_results[i].jobsel != 0xffffffff &&
-		    babinfo->chip_results[i].jobsel != 0x00000000) {
-			spi++;
-			babinfo->chip_spie[i]++;
-			applog(LOG_DEBUG, "%s%i: SPI ERROR on chip %d (0x%08x)",
-					  babcgpu->drv->name, babcgpu->device_id,
-					  i, babinfo->chip_results[i].jobsel);
-		}
-
-// Not used yet
-//		if (babinfo->chip_results[i].jobsel != babinfo->chip_prev[i].jobsel) {
-
-		got_a_nonce = false;
-		for (; newbusy != busy; busy = (busy + 1) % BAB_REPLY_NONCES) {
-			if (babinfo->chip_results[i].nonce[busy] == 0xffffffff ||
-			    babinfo->chip_results[i].nonce[busy] == 0x00000000) {
-				babinfo->chip_results[i].nonce[busy] = babinfo->chip_prev[i].nonce[busy];
-				spi = 1;
-				continue;
-			}
+		/*
+		 * Send even with zero nonces
+		 * so cleanup_older() is called for the chip
+		 */
+		DATAR(ritem)->nonces = nonces;
+		K_WLOCK(babinfo->res_list);
+		k_add_head(babinfo->res_list, ritem);
+		K_WUNLOCK(babinfo->res_list);
 
-			store_nonce(babcgpu, i,
-				    babinfo->chip_results[i].nonce[busy],
-				    babinfo->nonce_before[i]);
+		cgsem_post(&(babinfo->process_reply));
 
-			got_a_nonce = true;
-		}
+		babinfo->not_first_reply[chip] = true;
 
-		/*
-		 * We only care about this after the first reply we find a nonce
-		 * After that, the value has no more effect
-		 */
-		if (got_a_nonce)
-			babinfo->nonce_before[i] = true;
+		memcpy((void *)(&(babinfo->chip_prev[chip])),
+			(void *)(&(babinfo->chip_results[chip])),
+			sizeof(struct bab_work_reply));
 
-		mis += miso;
-		babinfo->chip_miso[i] += miso;
-		babinfo->chip_busy[i] = busy;
 	}
 
-	memcpy((void *)(&(babinfo->chip_prev[0])),
-		(void *)(&(babinfo->chip_results[0])),
-		sizeof(babinfo->chip_prev));
-
-	applog(LOG_DEBUG, "Work: items:%d spi:%d miso:%d",
-			  work_items, spi, mis);
+	applog(LOG_DEBUG, "Work: items:%d", work_items);
 
 	return true;
 }
@@ -1620,17 +1821,27 @@ static bool bab_queue_full(struct cgpu_info *babcgpu)
 {
 	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
 	struct work *work;
+	K_ITEM *item;
+	int count;
 	bool ret;
 
-	if (babinfo->work_count >= babinfo->chips)
+	K_RLOCK(babinfo->available_work);
+	count = babinfo->available_work->count;
+	K_RUNLOCK(babinfo->available_work);
+
+	if (count >= babinfo->chips)
 		ret = true;
 	else {
 		work = get_queued(babcgpu);
-		if (work)
-			store_work(babcgpu, work);
-		else
+		if (work) {
+			K_WLOCK(babinfo->wfree_list);
+			item = k_unlink_head_zero(babinfo->wfree_list);
+			DATAW(item)->work = work;
+			k_add_head(babinfo->available_work, item);
+			K_WUNLOCK(babinfo->wfree_list);
+		} else
 			// Avoid a hard loop when we can't get work fast enough
-			cgsleep_ms(10);
+			cgsleep_us(42);
 
 		ret = false;
 	}
@@ -1638,45 +1849,35 @@ static bool bab_queue_full(struct cgpu_info *babcgpu)
 	return ret;
 }
 
-/*
- * 1.0s per nonce = 4.2GH/s
- * So anything around 4GH/s or less per chip should be fine
- */
-#define BAB_STD_WORK_uS 1000000
-
-#define BAB_STD_DELAY_uS 30000
+#define BAB_STD_DELAY_mS 100
 
 /*
  * TODO: allow this to run through more than once - the second+
  * time not sending any new work unless a flush occurs since:
- * at the moment we have BAB_STD_WORK_uS latency added to earliest replies
+ * at the moment we have BAB_STD_WORK_mS latency added to earliest replies
  */
 static int64_t bab_scanwork(__maybe_unused struct thr_info *thr)
 {
 	struct cgpu_info *babcgpu = thr->cgpu;
 	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
 	int64_t hashcount = 0;
-	struct timeval now;
-	double delay;
+	int count;
 
 	bab_do_work(babcgpu);
 
-	// Sleep now so we get the work "bab_queue_full()" just before we use it
-	while (80085) {
-		cgtime(&now);
-		mutex_lock(&(babinfo->did_lock));
-		delay = us_tdiff(&now, &(babinfo->last_did));
-		mutex_unlock(&(babinfo->did_lock));
-		if (delay < (BAB_STD_WORK_uS - BAB_STD_DELAY_uS))
-			cgsleep_us(BAB_STD_DELAY_uS);
-		else
-			break;
-	}
+	K_RLOCK(babinfo->available_work);
+	count = babinfo->available_work->count;
+	K_RUNLOCK(babinfo->available_work);
+
+	if (count >= babinfo->chips)
+		cgsem_mswait(&(babinfo->scan_work), BAB_STD_DELAY_mS);
 
+	mutex_lock(&(babinfo->nonce_lock));
 	if (babinfo->new_nonces) {
 		hashcount += 0xffffffffull * babinfo->new_nonces;
 		babinfo->new_nonces = 0;
 	}
+	mutex_unlock(&(babinfo->nonce_lock));
 
 	return hashcount;
 }
@@ -1686,16 +1887,87 @@ static int64_t bab_scanwork(__maybe_unused struct thr_info *thr)
 static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 {
 	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
+	uint64_t history_good[BAB_MAXCHIPS], history_bad[BAB_MAXCHIPS];
+	double history_elapsed[BAB_MAXCHIPS], diff;
+	bool elapsed_is_good[BAB_MAXCHIPS];
+	int speeds[BAB_CHIP_SPEEDS];
 	struct api_data *root = NULL;
 	char data[2048];
 	char buf[32];
-	int i, to, j;
+	int spi_work, chip_work, i, to, j, sp, bank, chip_off;
+	struct timeval now;
+	double elapsed, ghs;
+	float ghs_sum, ghs_tot;
+	float tot, hw;
+	K_ITEM *item;
 
 	if (babinfo->initialised == false)
 		return NULL;
 
+	memset(&speeds, 0, sizeof(speeds));
+
+	root = api_add_int(root, "Version", &(babinfo->version), true);
 	root = api_add_int(root, "Chips", &(babinfo->chips), true);
+	root = api_add_int(root, "Boards", &(babinfo->boards), true);
+	root = api_add_int(root, "Banks", &(babinfo->banks), true);
+
+	data[0] = '\0';
+	for (i = 0; i <= BAB_MAXBANKS; i++) {
+		snprintf(buf, sizeof(buf), "%s%d",
+					   (i == 0) ? "" : " ",
+					   babinfo->chips_per_bank[i]);
+		strcat(data, buf);
+	}
+	root = api_add_string(root, "Chips Per Bank", data, true);
+
+	data[0] = '\0';
+	for (i = 0; i <= BAB_MAXBANKS; i++) {
+		snprintf(buf, sizeof(buf), "%s%d",
+					   (i == 0) ? "" : " ",
+					   babinfo->missing_chips_per_bank[i]);
+		strcat(data, buf);
+	}
+	root = api_add_string(root, "Missing Chips Per Bank", data, true);
+
+	cgtime(&now);
+	elapsed = tdiff(&now, &(babcgpu->dev_start_tv));
+
+	root = api_add_elapsed(root, "Device Elapsed", &elapsed, true);
+
+	root = api_add_string(root, "History Enabled",
+#if UPDATE_HISTORY
+				"true",
+#else
+				"false",
+#endif
+				true);
+
+	int chs = HISTORY_TIME_S;
+	root = api_add_int(root, "Chip History Limit", &chs, true);
+
+	K_RLOCK(babinfo->nfree_list);
+	for (i = 0; i < babinfo->chips; i++) {
+		item = babinfo->good_nonces[i]->tail;
+		elapsed_is_good[i] = true;
+		if (!item)
+			history_elapsed[i] = 0;
+		else
+			history_elapsed[i] = tdiff(&now, &(DATAN(item)->found));
+
+		item = babinfo->bad_nonces[i]->tail;
+		if (item) {
+			diff = tdiff(&now, &(DATAN(item)->found));
+			if (history_elapsed[i] < diff) {
+				history_elapsed[i] = diff;
+				elapsed_is_good[i] = false;
+			}
+		}
+		history_good[i] = babinfo->good_nonces[i]->count;
+		history_bad[i] = babinfo->bad_nonces[i]->count;
+	}
+	K_RUNLOCK(babinfo->nfree_list);
 
+	ghs_tot = 0;
 	for (i = 0; i < babinfo->chips; i += CHIPS_PER_STAT) {
 		to = i + CHIPS_PER_STAT - 1;
 		if (to >= babinfo->chips)
@@ -1748,48 +2020,324 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
 		data[0] = '\0';
 		for (j = i; j <= to; j++) {
 			snprintf(buf, sizeof(buf),
-					"%s0x%02x",
+					"%s%d",
 					j == i ? "" : " ",
 					(int)(babinfo->chip_fast[j]));
 			strcat(data, buf);
 		}
 		snprintf(buf, sizeof(buf), "Fast %d - %d", i, to);
 		root = api_add_string(root, buf, data, true);
+
+		data[0] = '\0';
+		for (j = i; j <= to; j++) {
+			tot = (float)(babinfo->chip_good[j] + babinfo->chip_bad[j]);
+			if (tot != 0)
+				hw = 100.0 * (float)(babinfo->chip_bad[j]) / tot;
+			else
+				hw = 0;
+			snprintf(buf, sizeof(buf),
+					"%s%.3f",
+					j == i ? "" : " ", hw);
+			strcat(data, buf);
+		}
+		snprintf(buf, sizeof(buf), "HW%% %d - %d", i, to);
+		root = api_add_string(root, buf, data, true);
+
+		ghs_sum = 0;
+		data[0] = '\0';
+		for (j = i; j <= to; j++) {
+			if (elapsed > 0) {
+				ghs = (double)(babinfo->chip_good[j]) * 0xffffffffull /
+					elapsed / 1000000000.0;
+			} else
+				ghs = 0;
+
+			snprintf(buf, sizeof(buf),
+					"%s%.3f",
+					j == i ? "" : " ", ghs);
+			strcat(data, buf);
+			ghs_sum += (float)ghs;
+		}
+		snprintf(buf, sizeof(buf), "GHs %d - %d", i, to);
+		root = api_add_string(root, buf, data, true);
+
+		snprintf(buf, sizeof(buf), "Sum GHs %d - %d", i, to);
+		root = api_add_avg(root, buf, &ghs_sum, true);
+
+		data[0] = '\0';
+		for (j = i; j <= to; j++) {
+			snprintf(buf, sizeof(buf),
+					"%s%"PRIu64,
+					j == i ? "" : " ",
+					babinfo->chip_cont_bad[j]);
+			strcat(data, buf);
+		}
+		snprintf(buf, sizeof(buf), "Cont-Bad %d - %d", i, to);
+		root = api_add_string(root, buf, data, true);
+
+		data[0] = '\0';
+		for (j = i; j <= to; j++) {
+			snprintf(buf, sizeof(buf),
+					"%s%"PRIu64,
+					j == i ? "" : " ",
+					babinfo->chip_max_bad[j]);
+			strcat(data, buf);
+		}
+		snprintf(buf, sizeof(buf), "Max-Bad %d - %d", i, to);
+		root = api_add_string(root, buf, data, true);
+
+		data[0] = '\0';
+		for (j = i; j <= to; j++) {
+			snprintf(buf, sizeof(buf),
+					"%s%"PRIu64,
+					j == i ? "" : " ",
+					history_good[j]);
+			strcat(data, buf);
+		}
+		snprintf(buf, sizeof(buf), "History Good %d - %d", i, to);
+		root = api_add_string(root, buf, data, true);
+
+		data[0] = '\0';
+		for (j = i; j <= to; j++) {
+			snprintf(buf, sizeof(buf),
+					"%s%"PRIu64,
+					j == i ? "" : " ",
+					history_bad[j]);
+			strcat(data, buf);
+		}
+		snprintf(buf, sizeof(buf), "History Bad %d - %d", i, to);
+		root = api_add_string(root, buf, data, true);
+
+		data[0] = '\0';
+		for (j = i; j <= to; j++) {
+			tot = (float)(history_good[j] + history_bad[j]);
+			if (tot != 0)
+				hw = 100.0 * (float)(history_bad[j]) / tot;
+			else
+				hw = 0;
+			snprintf(buf, sizeof(buf),
+					"%s%.3f",
+					j == i ? "" : " ", hw);
+			strcat(data, buf);
+		}
+		snprintf(buf, sizeof(buf), "History HW%% %d - %d", i, to);
+		root = api_add_string(root, buf, data, true);
+
+		ghs_sum = 0;
+		data[0] = '\0';
+		for (j = i; j <= to; j++) {
+			if (history_elapsed[j] > 0) {
+				double num = history_good[j];
+				// exclude the first nonce?
+				if (elapsed_is_good[j])
+					num--;
+				ghs = num * 0xffffffffull /
+					history_elapsed[j] / 1000000000.0;
+			} else
+				ghs = 0;
+
+			snprintf(buf, sizeof(buf),
+					"%s%.3f",
+					j == i ? "" : " ", ghs);
+			strcat(data, buf);
+
+			ghs_sum += (float)ghs;
+
+			// Setup speed range data
+			for (sp = 0; sp < BAB_CHIP_SPEEDS - 1; sp++) {
+				if (ghs <= chip_speed_ranges[sp]) {
+					speeds[sp]++;
+					break;
+				}
+			}
+			if (sp >= (BAB_CHIP_SPEEDS - 1))
+				speeds[BAB_CHIP_SPEEDS - 1]++;
+		}
+		snprintf(buf, sizeof(buf), "History GHs %d - %d", i, to);
+		root = api_add_string(root, buf, data, true);
+
+		snprintf(buf, sizeof(buf), "Sum History GHs %d - %d", i, to);
+		root = api_add_avg(root, buf, &ghs_sum, true);
+
+		ghs_tot += ghs_sum;
 	}
 
+	root = api_add_avg(root, "Total History GHs", &ghs_tot, true);
+
+	for (sp = 0; sp < BAB_CHIP_SPEEDS; sp++) {
+		if (sp < (BAB_CHIP_SPEEDS - 1))
+			ghs = chip_speed_ranges[sp];
+		else
+			ghs = chip_speed_ranges[BAB_CHIP_SPEEDS - 2];
+
+		snprintf(buf, sizeof(buf), "History Speed %s%.1f %s",
+					   (sp < (BAB_CHIP_SPEEDS - 1)) ? "" : ">",
+					   ghs, chip_speed_names[sp]);
+
+		root = api_add_int(root, buf, &(speeds[sp]), true);
+	}
+
+	int len, str, siz = 1024;
+	char *tmp = malloc(siz);
+	if (!tmp)
+		quithere(1, "OOM tmp1");
+	for (sp = 0; sp < 2; sp++) {
+		tmp[0] = '\0';
+		len = 0;
+		for (i = 0; i < babinfo->chips; i++) {
+			if (history_elapsed[i] > 0) {
+				double num = history_good[i];
+				// exclude the first nonce?
+				if (elapsed_is_good[i])
+					num--;
+				ghs = num * 0xffffffffull /
+					history_elapsed[i] / 1000000000.0;
+			} else
+				ghs = 0;
+
+			if ((sp == 0 || ghs > chip_speed_ranges[sp-1]) &&
+			    (ghs <= chip_speed_ranges[sp])) {
+				bank = babinfo->chip_bank[i];
+				chip_off = i;
+				for (j = 0; j < babinfo->chip_bank[i]; j++)
+					chip_off -= babinfo->chips_per_bank[j];
+				/*
+				 * Bank/Board/Chip are all 1 based
+				 * except V1 Bank = BAB_V1_BANK (0)
+				 * If the bank has any missing chips then a "?"
+				 * is placed after the board number
+				 */
+				snprintf(buf, sizeof(buf), "%s%d/%d%s/%d",
+							   len ? " " : "", bank,
+							   (int)(chip_off / BAB_BOARDCHIPS)+1,
+							   babinfo->missing_chips_per_bank[bank] ?
+							   "?" : "",
+							   (chip_off % BAB_BOARDCHIPS)+1);
+				str = strlen(buf);
+				while ((len + str + 1) > siz) {
+					siz += 1024;
+					tmp = realloc(tmp, siz);
+					if (!tmp)
+						quithere(1, "OOM tmp2");
+				}
+				strcpy(tmp + len, buf);
+				len += str;
+			}
+		}
+		snprintf(buf, sizeof(buf), "History %s", chip_speed_names[sp]);
+
+		root = api_add_string(root, buf, len ? tmp : "None", true);
+	}
+	free(tmp);
+	tmp = NULL;
+
 	for (i = 0; i < BAB_NONCE_OFFSETS; i++) {
 		snprintf(buf, sizeof(buf), "Nonce Offset 0x%08x", bab_nonce_offsets[i]);
 		root = api_add_uint64(root, buf, &(babinfo->nonce_offset_count[i]), true);
 	}
 
+	root = api_add_uint64(root, "Discarded E0s", &(babinfo->discarded_e0s), true);
 	root = api_add_uint64(root, "Tested", &(babinfo->tested_nonces), true);
+	root = api_add_uint64(root, "OK", &(babinfo->ok_nonces), true);
 	root = api_add_uint64(root, "Total Tests", &(babinfo->total_tests), true);
 	root = api_add_uint64(root, "Max Tests", &(babinfo->max_tests_per_nonce), true);
-	float avg = babinfo->tested_nonces ? (float)(babinfo->total_tests) /
-					     (float)(babinfo->tested_nonces) : 0;
-// TODO: add a API_AVG which is 3 places - double/float?
-	root = api_add_volts(root, "Avg Tests", &avg, true);
+	float avg = babinfo->ok_nonces ? (float)(babinfo->total_tests) /
+					     (float)(babinfo->ok_nonces) : 0;
+	root = api_add_avg(root, "Avg Tests", &avg, true);
 	root = api_add_uint64(root, "Untested", &(babinfo->untested_nonces), true);
 
 	root = api_add_uint64(root, "Work Links", &(babinfo->total_links), true);
+	root = api_add_uint64(root, "Work Processed Links", &(babinfo->total_proc_links), true);
 	root = api_add_uint64(root, "Max Links", &(babinfo->max_links), true);
-	avg = babinfo->tested_nonces ? (float)(babinfo->total_links) /
-					(float)(babinfo->tested_nonces) : 0;
-	root = api_add_volts(root, "Avg Links", &avg, true);
+	root = api_add_uint64(root, "Max Processed Links", &(babinfo->max_proc_links), true);
+	root = api_add_uint64(root, "Total Work Links", &(babinfo->total_work_links), true);
+	avg = babinfo->ok_nonces ? (float)(babinfo->total_links) /
+					(float)(babinfo->ok_nonces) : 0;
+	root = api_add_avg(root, "Avg Links", &avg, true);
+	avg = babinfo->ok_nonces ? (float)(babinfo->total_proc_links) /
+					(float)(babinfo->ok_nonces) : 0;
+	root = api_add_avg(root, "Avg Proc Links", &avg, true);
+	avg = babinfo->ok_nonces ? (float)(babinfo->total_work_links) /
+					(float)(babinfo->ok_nonces) : 0;
+	root = api_add_avg(root, "Avg Work Links", &avg, true);
+
+	root = api_add_uint64(root, "Fail", &(babinfo->fail), true);
+	root = api_add_uint64(root, "Fail Total Tests", &(babinfo->fail_total_tests), true);
+	avg = babinfo->fail ? (float)(babinfo->fail_total_tests) /
+					(float)(babinfo->fail) : 0;
+	root = api_add_avg(root, "Fail Avg Tests", &avg, true);
+	root = api_add_uint64(root, "Fail Work Links", &(babinfo->fail_total_links), true);
+	root = api_add_uint64(root, "Fail Total Work Links", &(babinfo->fail_total_work_links), true);
 
 	root = api_add_uint32(root, "Initial Ignored", &(babinfo->initial_ignored), true);
+	root = api_add_uint64(root, "Ign Total Tests", &(babinfo->ign_total_tests), true);
+	root = api_add_uint64(root, "Ign Work Links", &(babinfo->ign_total_links), true);
+	root = api_add_uint64(root, "Ign Total Work Links", &(babinfo->ign_total_work_links), true);
 
-	root = api_add_int(root, "BList Count", &(babinfo->blist_count), true);
-	root = api_add_int(root, "BFree Count", &(babinfo->bfree_count), true);
-	root = api_add_int(root, "Work Count", &(babinfo->work_count), true);
-	root = api_add_int(root, "Chip Count", &(babinfo->chip_count), true);
-
-	root = api_add_int(root, "RList Count", &(babinfo->rlist_count), true);
-	root = api_add_int(root, "RFree Count", &(babinfo->rfree_count), true);
-	root = api_add_int(root, "Result Count", &(babinfo->res_count), true);
+	chip_work = 0;
+	for (i = 0; i < babinfo->chips; i++)
+		chip_work += babinfo->chip_work[i]->count;
+	spi_work = babinfo->spi_list->count * babinfo->chips;
+
+	root = api_add_int(root, "WFree Total", &(babinfo->wfree_list->total), true);
+	root = api_add_int(root, "WFree Count", &(babinfo->wfree_list->count), true);
+	root = api_add_int(root, "Available Work", &(babinfo->available_work->count), true);
+	root = api_add_int(root, "SPI Work", &spi_work, true);
+	root = api_add_int(root, "Chip Work", &chip_work, true);
+
+	root = api_add_int(root, "SFree Total", &(babinfo->sfree_list->total), true);
+	root = api_add_int(root, "SFree Count", &(babinfo->sfree_list->count), true);
+	root = api_add_int(root, "SPI Waiting", &(babinfo->spi_list->count), true);
+	root = api_add_int(root, "SPI Sent", &(babinfo->spi_sent->count), true);
+
+	root = api_add_int(root, "RFree Total", &(babinfo->rfree_list->total), true);
+	root = api_add_int(root, "RFree Count", &(babinfo->rfree_list->count), true);
+	root = api_add_int(root, "Result Count", &(babinfo->res_list->count), true);
+
+	int used = babinfo->nfree_list->total - babinfo->nfree_list->count;
+	root = api_add_int(root, "NFree Total", &(babinfo->nfree_list->total), true);
+	root = api_add_int(root, "NFree Used", &used, true);
+
+	root = api_add_uint64(root, "Delay Count", &(babinfo->delay_count), true);
+	root = api_add_double(root, "Delay Min", &(babinfo->delay_min), true);
+	root = api_add_double(root, "Delay Max", &(babinfo->delay_max), true);
+
+	data[0] = '\0';
+	for (i = 0; i <= BAB_DELAY_BANDS; i++) {
+		snprintf(buf, sizeof(buf),
+				"%s<%.1f=%"PRIu64,
+				i == 0 ? "" : " ",
+				BAB_DELAY_BASE+(BAB_DELAY_STEP*i),
+				babinfo->delay_bands[i]);
+		strcat(data, buf);
+	}
+	snprintf(buf, sizeof(buf),
+			" >=%.1f=%"PRIu64,
+			BAB_DELAY_BASE+BAB_DELAY_STEP*(BAB_DELAY_BANDS+1),
+			babinfo->delay_bands[BAB_DELAY_BANDS+1]);
+	strcat(data, buf);
+	root = api_add_string(root, "Delay Bands", data, true);
+
+	root = api_add_uint64(root, "Send Count", &(babinfo->send_count), true);
+	root = api_add_double(root, "Send Total", &(babinfo->send_total), true);
+	avg = babinfo->send_count ? (float)(babinfo->send_total) /
+					(float)(babinfo->send_count) : 0;
+	root = api_add_avg(root, "Send Avg", &avg, true);
+	root = api_add_double(root, "Send Min", &(babinfo->send_min), true);
+	root = api_add_double(root, "Send Max", &(babinfo->send_max), true);
 
 	return root;
 }
+
+static void bab_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info *babcgpu)
+{
+	struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
+
+	tailsprintf(buf, bufsiz, "B:%d B:%02d C:%03d | ",
+				 babinfo->banks,
+				 babinfo->boards,
+				 babinfo->chips);
+}
 #endif
 
 struct device_drv bab_drv = {
@@ -1799,7 +2347,7 @@ struct device_drv bab_drv = {
 	.drv_detect = bab_detect,
 #ifdef LINUX
 	.get_api_stats = bab_api_stats,
-//TODO:	.get_statline_before = get_bab_statline_before,
+	.get_statline_before = bab_get_statline_before,
 	.identify_device = bab_identify,
 	.thread_prepare = bab_thread_prepare,
 	.hash_work = hash_queued_work,
diff --git a/driver-bitfury.c b/driver-bitfury.c
index 3bad374..202c8af 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -16,6 +16,7 @@
 #include "libbitfury.h"
 
 int opt_bxf_temp_target = BXF_TEMP_TARGET / 10;
+int opt_nf1_bits = 50;
 
 /* Wait longer 1/3 longer than it would take for a full nonce range */
 #define BF1WAIT 1600
@@ -414,7 +415,7 @@ static bool nf1_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
 	if (val != MCP2210_GPIO_PIN_LOW)
 		goto out;
 
-	info->osc6_bits = 54;
+	info->osc6_bits = opt_nf1_bits;
 	if (!nf1_reinit(bitfury, info))
 		goto out;
 
diff --git a/driver-bitfury.h b/driver-bitfury.h
index 30d7e4d..e802915 100644
--- a/driver-bitfury.h
+++ b/driver-bitfury.h
@@ -24,6 +24,7 @@
 #define BXF_TEMP_HYSTERESIS 30
 
 extern int opt_bxf_temp_target;
+extern int opt_nf1_bits;
 
 #define NF1_PIN_LED 0
 #define NF1_PIN_SCK_OVR 5
diff --git a/driver-hashfast.c b/driver-hashfast.c
index a6574fd..e450e9a 100644
--- a/driver-hashfast.c
+++ b/driver-hashfast.c
@@ -767,9 +767,9 @@ static void *hfa_read(void *arg)
 	struct thr_info *thr = (struct thr_info *)arg;
 	struct cgpu_info *hashfast = thr->cgpu;
 	struct hashfast_info *info = hashfast->device_data;
-	char threadname[24];
+	char threadname[16];
 
-	snprintf(threadname, 24, "hfa_read/%d", hashfast->device_id);
+	snprintf(threadname, sizeof(threadname), "%d/HfaRead", hashfast->device_id);
 	RenameThread(threadname);
 
 	while (likely(!hashfast->shutdown)) {
diff --git a/klist.c b/klist.c
new file mode 100644
index 0000000..310c6f6
--- /dev/null
+++ b/klist.c
@@ -0,0 +1,315 @@
+/*
+ * Copyright 2013-2014 Andrew Smith - BlackArrow Ltd
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#include <klist.h>
+
+static void k_alloc_items(K_LIST *list, KLIST_FFL_ARGS)
+{
+	K_ITEM *item;
+	int allocate, i;
+
+	if (list->is_store) {
+		quithere(1, "List %s store can't %s()" KLIST_FFL,
+				list->name, __func__, KLIST_FFL_PASS);
+	}
+
+	if (list->limit > 0 && list->total >= list->limit)
+		return;
+
+	allocate = list->allocate;
+	if (list->limit > 0 && (list->total + allocate) > list->limit)
+		allocate = list->limit - list->total;
+
+	list->item_mem_count++;
+	if (!(list->item_memory = realloc(list->item_memory,
+					  list->item_mem_count * sizeof(*(list->item_memory))))) {
+		quithere(1, "List %s item_memory failed to realloc count=%d",
+				list->name, list->item_mem_count);
+	}
+	item = calloc(allocate, sizeof(*item));
+	if (!item) {
+		quithere(1, "List %s failed to calloc %d new items - total was %d, limit was %d",
+				list->name, allocate, list->total, list->limit);
+	}
+	list->item_memory[list->item_mem_count - 1] = (void *)item;
+
+	list->total += allocate;
+	list->count = allocate;
+	list->count_up = allocate;
+
+	item[0].name = list->name;
+	item[0].prev = NULL;
+	item[0].next = &(item[1]);
+	for (i = 1; i < allocate-1; i++) {
+		item[i].name = list->name;
+		item[i].prev = &item[i-1];
+		item[i].next = &item[i+1];
+	}
+	item[allocate-1].name = list->name;
+	item[allocate-1].prev = &(item[allocate-2]);
+	item[allocate-1].next = NULL;
+
+	list->head = item;
+	if (list->do_tail)
+		list->tail = &(item[allocate-1]);
+
+	item = list->head;
+	while (item) {
+		list->data_mem_count++;
+		if (!(list->data_memory = realloc(list->data_memory,
+						  list->data_mem_count *
+						  sizeof(*(list->data_memory))))) {
+			quithere(1, "List %s data_memory failed to realloc count=%d",
+					list->name, list->data_mem_count);
+		}
+		item->data = calloc(1, list->siz);
+		if (!(item->data))
+			quithere(1, "List %s failed to calloc item data", list->name);
+		list->data_memory[list->data_mem_count - 1] = (void *)(item->data);
+		item = item->next;
+	}
+}
+
+K_STORE *k_new_store(K_LIST *list)
+{
+	K_STORE *store;
+
+	store = calloc(1, sizeof(*store));
+	if (!store)
+		quithere(1, "Failed to calloc store for %s", list->name);
+
+	store->is_store = true;
+	store->lock = list->lock;
+	store->name = list->name;
+	store->do_tail = list->do_tail;
+
+	return store;
+}
+
+K_LIST *_k_new_list(const char *name, size_t siz, int allocate, int limit, bool do_tail, KLIST_FFL_ARGS)
+{
+	K_LIST *list;
+
+	if (allocate < 1)
+		quithere(1, "Invalid new list %s with allocate %d must be > 0", name, allocate);
+
+	if (limit < 0)
+		quithere(1, "Invalid new list %s with limit %d must be >= 0", name, limit);
+
+	list = calloc(1, sizeof(*list));
+	if (!list)
+		quithere(1, "Failed to calloc list %s", name);
+
+	list->is_store = false;
+
+	list->lock = calloc(1, sizeof(*(list->lock)));
+	if (!(list->lock))
+		quithere(1, "Failed to calloc lock for list %s", name);
+
+	cglock_init(list->lock);
+
+	list->name = name;
+	list->siz = siz;
+	list->allocate = allocate;
+	list->limit = limit;
+	list->do_tail = do_tail;
+
+	k_alloc_items(list, KLIST_FFL_PASS);
+
+	return list;
+}
+
+/*
+ * Unlink and return the head of the list
+ * If the list is empty:
+ * 1) If it's a store - return NULL
+ * 2) alloc a new list and return the head -
+ *	which is NULL if the list limit has been reached
+ */
+K_ITEM *_k_unlink_head(K_LIST *list, KLIST_FFL_ARGS)
+{
+	K_ITEM *item;
+
+	if (!(list->head) && !(list->is_store))
+		k_alloc_items(list, KLIST_FFL_PASS);
+
+	if (!(list->head))
+		return NULL;
+
+	item = list->head;
+	list->head = item->next;
+	if (list->head)
+		list->head->prev = NULL;
+	else {
+		if (list->do_tail)
+			list->tail = NULL;
+	}
+
+	item->prev = item->next = NULL;
+
+	list->count--;
+
+	return item;
+}
+
+// Zeros the head returned
+K_ITEM *_k_unlink_head_zero(K_LIST *list, KLIST_FFL_ARGS)
+{
+	K_ITEM *item;
+
+	item = _k_unlink_head(list, KLIST_FFL_PASS);
+
+	if (item)
+		memset(item->data, 0, list->siz);
+
+	return item;
+}
+
+// Returns NULL if empty
+K_ITEM *_k_unlink_tail(K_LIST *list, KLIST_FFL_ARGS)
+{
+	K_ITEM *item;
+
+	if (!(list->do_tail)) {
+		quithere(1, "List %s can't %s() - do_tail is false" KLIST_FFL,
+				list->name, __func__, KLIST_FFL_PASS);
+	}
+
+	if (!(list->tail))
+		return NULL;
+
+	item = list->tail;
+	list->tail = item->prev;
+	if (list->tail)
+		list->tail->next = NULL;
+	else
+		list->head = NULL;
+
+	item->prev = item->next = NULL;
+
+	list->count--;
+
+	return item;
+}
+
+void _k_add_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
+{
+	if (item->name != list->name) {
+		quithere(1, "List %s can't %s() a %s item" KLIST_FFL,
+				list->name, __func__, item->name, KLIST_FFL_PASS);
+	}
+
+	item->prev = NULL;
+	item->next = list->head;
+	if (list->head)
+		list->head->prev = item;
+
+	list->head = item;
+
+	if (list->do_tail) {
+		if (!(list->tail))
+			list->tail = item;
+	}
+
+	list->count++;
+	list->count_up++;
+}
+
+/* slows it down (of course) - only for debugging
+void _k_free_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
+{
+	memset(item->data, 0xff, list->siz);
+	_k_add_head(list, item, KLIST_FFL_PASS);
+}
+*/
+
+void _k_add_tail(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
+{
+	if (item->name != list->name) {
+		quithere(1, "List %s can't %s() a %s item" KLIST_FFL,
+				list->name, __func__, item->name, KLIST_FFL_PASS);
+	}
+
+	if (!(list->do_tail)) {
+		quithere(1, "List %s can't %s() - do_tail is false" KLIST_FFL,
+				list->name, __func__, KLIST_FFL_PASS);
+	}
+
+	item->prev = list->tail;
+	item->next = NULL;
+	if (list->tail)
+		list->tail->next = item;
+
+	list->tail = item;
+
+	if (!(list->head))
+		list->head = item;
+
+	list->count++;
+	list->count_up++;
+}
+
+void k_unlink_item(K_LIST *list, K_ITEM *item)
+{
+	if (item->prev)
+		item->prev->next = item->next;
+
+	if (item->next)
+		item->next->prev = item->prev;
+
+	if (list->head == item)
+		list->head = item->next;
+
+	if (list->do_tail) {
+		if (list->tail == item)
+			list->tail = item->prev;
+	}
+
+	item->prev = item->next = NULL;
+
+	list->count--;
+}
+
+K_LIST *_k_free_list(K_LIST *list, KLIST_FFL_ARGS)
+{
+	int i;
+
+	if (list->is_store) {
+		quithere(1, "List %s can't %s() a store" KLIST_FFL,
+				list->name, __func__, KLIST_FFL_PASS);
+	}
+
+	for (i = 0; i < list->item_mem_count; i++)
+		free(list->item_memory[i]);
+	free(list->item_memory);
+
+	for (i = 0; i < list->data_mem_count; i++)
+		free(list->data_memory[i]);
+	free(list->data_memory);
+
+	cglock_destroy(list->lock);
+
+	free(list->lock);
+
+	free(list);
+
+	return NULL;
+}
+
+K_STORE *_k_free_store(K_STORE *store, KLIST_FFL_ARGS)
+{
+	if (!(store->is_store)) {
+		quithere(1, "Store %s can't %s() the list" KLIST_FFL,
+				store->name, __func__, KLIST_FFL_PASS);
+	}
+
+	free(store);
+
+	return NULL;
+}
diff --git a/klist.h b/klist.h
new file mode 100644
index 0000000..4b88f95
--- /dev/null
+++ b/klist.h
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2013-2014 Andrew Smith - BlackArrow Ltd
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#ifndef KLIST_H
+#define KLIST_H
+
+#include <miner.h>
+
+#define KLIST_FFL " - from %s %s() line %d"
+#define KLIST_FFL_HERE __FILE__, __func__, __LINE__
+#define KLIST_FFL_PASS file, func, line
+#define KLIST_FFL_ARGS  __maybe_unused const char *file, \
+			__maybe_unused const char *func, \
+			__maybe_unused const int line
+
+typedef struct k_item {
+	const char *name;
+	struct k_item *prev;
+	struct k_item *next;
+	void *data;
+} K_ITEM;
+
+typedef struct k_list {
+	const char *name;
+	bool is_store;
+	cglock_t *lock;
+	struct k_item *head;
+	struct k_item *tail;
+	size_t siz;		// item data size
+	int total;		// total allocated
+	int count;		// in this list
+	int count_up;		// incremented every time one is added
+	int allocate;		// number to intially allocate and each time we run out
+	int limit;		// total limit - 0 means unlimited
+	bool do_tail;		// track the tail?
+	int item_mem_count;	// how many item memory buffers have been allocated
+	void **item_memory;	// allocated item memory buffers
+	int data_mem_count;	// how many item data memory buffers have been allocated
+	void **data_memory;	// allocated item data memory buffers
+} K_LIST;
+
+/*
+ * K_STORE is for a list of items taken from a K_LIST
+ * The restriction is, a K_STORE must not allocate new items,
+ * only the K_LIST should do that
+ * i.e. all K_STORE items came from a K_LIST
+ */
+#define K_STORE K_LIST
+
+/*
+ * N.B. all locking is done in the code calling the k_ functions
+ */
+#define K_WLOCK(_list) cg_wlock(_list->lock)
+#define K_WUNLOCK(_list) cg_wunlock(_list->lock)
+#define K_RLOCK(_list) cg_rlock(_list->lock)
+#define K_RUNLOCK(_list) cg_runlock(_list->lock)
+
+extern K_STORE *k_new_store(K_LIST *list);
+extern K_LIST *_k_new_list(const char *name, size_t siz, int allocate, int limit, bool do_tail, KLIST_FFL_ARGS);
+#define k_new_list(_name, _siz, _allocate, _limit, _do_tail) _k_new_list(_name, _siz, _allocate, _limit, _do_tail, KLIST_FFL_HERE)
+extern K_ITEM *_k_unlink_head(K_LIST *list, KLIST_FFL_ARGS);
+#define k_unlink_head(_list) _k_unlink_head(_list, KLIST_FFL_HERE)
+extern K_ITEM *_k_unlink_head_zero(K_LIST *list, KLIST_FFL_ARGS);
+#define k_unlink_head_zero(_list) _k_unlink_head_zero(_list, KLIST_FFL_HERE)
+extern K_ITEM *_k_unlink_tail(K_LIST *list, KLIST_FFL_ARGS);
+#define k_unlink_tail(_list) _k_unlink_tail(_list, KLIST_FFL_HERE)
+extern void _k_add_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
+#define k_add_head(_list, _item) _k_add_head(_list, _item, KLIST_FFL_HERE)
+// extern void k_free_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
+#define k_free_head(__list, __item) _k_add_head(__list, __item, KLIST_FFL_HERE)
+extern void _k_add_tail(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
+#define k_add_tail(_list, _item) _k_add_tail(_list, _item, KLIST_FFL_HERE)
+extern void k_unlink_item(K_LIST *list, K_ITEM *item);
+extern K_LIST *_k_free_list(K_LIST *list, KLIST_FFL_ARGS);
+#define k_free_list(_list) _k_free_list(_list, KLIST_FFL_HERE)
+extern K_STORE *_k_free_store(K_STORE *store, KLIST_FFL_ARGS);
+#define k_free_store(_store) _k_free_store(_store, KLIST_FFL_HERE)
+
+#endif
diff --git a/miner.h b/miner.h
index cab07c6..0aac88b 100644
--- a/miner.h
+++ b/miner.h
@@ -1013,6 +1013,7 @@ extern pthread_mutex_t restart_lock;
 extern pthread_cond_t restart_cond;
 
 extern void clear_stratum_shares(struct pool *pool);
+extern void clear_pool_work(struct pool *pool);
 extern void set_target(unsigned char *dest_target, double diff);
 extern int restart_wait(struct thr_info *thr, unsigned int mstime);
 
@@ -1413,7 +1414,8 @@ enum api_data_type {
 	API_VOLTS,
 	API_HS,
 	API_DIFF,
-	API_PERCENT
+	API_PERCENT,
+	API_AVG
 };
 
 struct api_data {
@@ -1448,5 +1450,6 @@ extern struct api_data *api_add_volts(struct api_data *root, char *name, float *
 extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
 extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
 extern struct api_data *api_add_percent(struct api_data *root, char *name, double *data, bool copy_data);
+extern struct api_data *api_add_avg(struct api_data *root, char *name, float *data, bool copy_data);
 
 #endif /* __MINER_H__ */
diff --git a/usbutils.c b/usbutils.c
index 4e76e13..88ebd07 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -3716,7 +3716,7 @@ void *usb_resource_thread(void __maybe_unused *userdata)
 {
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
-	RenameThread("usbresource");
+	RenameThread("USBResource");
 
 	applog(LOG_DEBUG, "RES: thread starting");
 
diff --git a/util.c b/util.c
index 16aa8da..053dae4 100644
--- a/util.c
+++ b/util.c
@@ -1695,6 +1695,8 @@ static bool parse_reconnect(struct pool *pool, json_t *val)
 
 	applog(LOG_NOTICE, "Reconnect requested from pool %d to %s", pool->pool_no, address);
 
+	clear_pool_work(pool);
+
 	mutex_lock(&pool->stratum_lock);
 	__suspend_stratum(pool);
 	tmp = pool->sockaddr_url;
@@ -2531,16 +2533,19 @@ void *str_text(char *ptr)
 
 void RenameThread(const char* name)
 {
+	char buf[16];
+
+	snprintf(buf, sizeof(buf), "cg@%s", name);
 #if defined(PR_SET_NAME)
 	// Only the first 15 characters are used (16 - NUL terminator)
-	prctl(PR_SET_NAME, name, 0, 0, 0);
+	prctl(PR_SET_NAME, buf, 0, 0, 0);
 #elif (defined(__FreeBSD__) || defined(__OpenBSD__))
-	pthread_set_name_np(pthread_self(), name);
+	pthread_set_name_np(pthread_self(), buf);
 #elif defined(MAC_OSX)
-	pthread_setname_np(name);
+	pthread_setname_np(buf);
 #else
-	// Prevent warnings for unused parameters...
-	(void)name;
+	// Prevent warnings
+	(void)buf;
 #endif
 }