Commit 146d0d3753938e983577cf6bfd726bf34c375751

Kano 2013-12-16T00:32:10

Working driver-minion.c v0.1

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
diff --git a/code/driver-minion.c b/code/driver-minion.c
index 2c0533e..e4385c1 100644
--- a/code/driver-minion.c
+++ b/code/driver-minion.c
@@ -35,6 +35,7 @@ static void minion_detect(__maybe_unused bool hotplug)
 #define MINION_FFL " - from %s %s() line %d"
 #define MINION_FFL_HERE __FILE__, __func__, __LINE__
 #define MINION_FFL_PASS file, func, line
+#define MINION_FFL_ARGS const char *file, const char *func, const int line
 
 #define minion_txrx(_task, _ignore) _minion_txrx(minioncgpu, minioninfo, _task, _ignore, MINION_FFL_HERE)
 
@@ -42,7 +43,7 @@ static void minion_detect(__maybe_unused bool hotplug)
 #define MINION_CORE_REGS 0x10
 #define MINION_RES_BUF 0x20
 #define MINION_CMD_QUE 0x30
-#define MINION_NONCE_RANGE 0x70
+#define MINION_NONCE_RANGES 0x70
 
 #define DATA_SIZ (sizeof(uint32_t))
 
@@ -61,12 +62,39 @@ static void minion_detect(__maybe_unused bool hotplug)
 #define MINION_SYS_QUE_TRIG 0x0c
 #define MINION_SYS_BUF_TRIG 0x0d
 
-// CORE data size is minion_core
+// All SYS data sizes are DATA_SIZ
+#define MINION_SYS_SIZ DATA_SIZ
+
+#define RSTN_CTL_RESET_CORES 0x01
+#define RSTN_CTL_FLUSH_RESULTS 0x02
+#define RSTN_CTL_FLUSH_CMD_QUEUE 0x04
+#define RSTN_CTL_SPI_SW_RSTN 0x08
+#define RSTN_CTL_SHA_MGR_RESET 0x10
+
+// Init
+#define SYS_RSTN_CTL_INIT (RSTN_CTL_RESET_CORES | \
+				RSTN_CTL_FLUSH_RESULTS | \
+				RSTN_CTL_FLUSH_CMD_QUEUE | \
+				RSTN_CTL_SPI_SW_RSTN | \
+				RSTN_CTL_SHA_MGR_RESET)
+
+// LP
+#define SYS_RSTN_CTL_FLUSH (RSTN_CTL_RESET_CORES | \
+				RSTN_CTL_SPI_SW_RSTN | \
+				RSTN_CTL_FLUSH_CMD_QUEUE)
+
+// enable 'no nonce' report
+#define SYS_MISC_CTL_DEFAULT 0x04
+
+// CORE data size is DATA_SIZ
 #define MINION_CORE_ENA0_31 0x10
 #define MINION_CORE_ENA32_63 0x11
 #define MINION_CORE_ACT0_31 0x14
 #define MINION_CORE_ACT32_63 0x15
 
+// All CORE data sizes are DATA_SIZ
+#define MINION_CORE_SIZ DATA_SIZ
+
 // RES data size is minion_result
 #define MINION_RES_DATA 0x20
 #define MINION_RES_PEEK 0x21
@@ -77,10 +105,27 @@ static void minion_detect(__maybe_unused bool hotplug)
 
 // RANGE data sizes are DATA_SIZ
 #define MINION_NONCE_STA 0x70
-#define MINION_NONCE_FIN 0x71
+#define MINION_NONCE_RANGE 0x71
+
+// This must be >= max txsiz + max rxsiz
+#define MINION_BUFSIZ 1024
+
+#define u8tou32(_c, _off) (((uint8_t *)(_c))[(_off)+0] + \
+			  ((uint8_t *)(_c))[(_off)+1] * 0x100 + \
+			  ((uint8_t *)(_c))[(_off)+2] * 0x10000 + \
+			  ((uint8_t *)(_c))[(_off)+3] * 0x1000000 )
+
+#define MINION_ADDR_WRITE 0x7f
+#define MINION_ADDR_READ 0x80
+
+#define READ_ADDR(_reg) ((_reg) | MINION_ADDR_READ)
+#define WRITE_ADDR(_reg) ((_reg) & MINION_ADDR_WRITE)
+
+#define IS_ADDR_READ(_reg) (((_reg) & MINION_ADDR_READ) == MINION_ADDR_READ)
+#define IS_ADDR_WRITE(_reg) (((_reg) & MINION_ADDR_READ) == 0)
 
-#define SET_HEAD_READ(_h, _reg) ((_h)->reg) = ((_reg) & 0x7f)
-#define SET_HEAD_WRITE(_h, _reg) ((_h)->reg) = ((_reg) | 0x80)
+#define SET_HEAD_WRITE(_h, _reg) ((_h)->reg) = WRITE_ADDR(_reg)
+#define SET_HEAD_READ(_h, _reg) ((_h)->reg) = READ_ADDR(_reg)
 #define SET_HEAD_SIZ(_h, _siz) \
 		do { \
 			((_h)->siz)[0] = (uint8_t)((_siz) & 0xff); \
@@ -96,26 +141,42 @@ struct minion_header {
 
 #define HSIZE() (sizeof(struct minion_header) - 4)
 
+#define MINION_NOCHIP_SIG 0x00000000
 #define MINION_CHIP_SIG 0x32020ffa
 
+#define STA_TEMP(_sta) ((uint16_t)((_sta)[3] & 0x1f))
+#define STA_CORES(_sta) ((uint16_t)((_sta)[2]))
+#define STA_FREQ(_sta) ((uint32_t)((_sta)) * 0x100 + (uint32_t)((_sta)[0]))
+
+// Randomly between 1s and 2s per chip
+#define MINION_STATS_UPDATE_TIME_MS 1000
+#define MINION_STATS_UPDATE_RAND_MS 1000
+
+struct minion_status {
+	uint16_t temp;
+	uint16_t cores;
+	uint32_t freq;
+	struct timeval last;
+};
+
 #define SET_CORE(_core, _n) ((_core)->core[_n << 4] &= (2 >> (_n % 8)))
 #define CORE_IDLE(_core, _n) ((_core)->core[_n << 4] & (2 >> (_n % 8)))
 
-struct minion_core {
-	uint8_t core[DATA_SIZ];
-};
+#define RES_GOLD(_res) ((((_res)->status[3]) & 0x80) == 0)
+#define RES_CHIP(_res) (((_res)->status[3]) & 0x1f)
+#define RES_CORE(_res) ((_res)->status[2])
+#define RES_TASK(_res) ((int)((_res)->status[1]) * 0x100 + (int)((_res)->status[0]))
+#define RES_NONCE(_res) u8tou32((_res)->nonce, 0)
 
-#define RES_GOLD(_res) ((((_res)->status[0]) & 0x80) == 0)
-#define RES_CHIP(_res) (((_res)->status[0]) & 0x1f)
-#define RES_CORE(_res) ((_res)->status[1])
-#define RES_TASK(_res) ((int)((_res)->status[2]) * 0x100 + (int)((_res)->status[2]))
-#define RES_NONCE(_res) (*(uint32_t *)(&((_res)->nonce[0])))
+#define IS_RESULT(_res) ((_res)->status[1] || (_res)->status[0])
 
 struct minion_result {
 	uint8_t status[DATA_SIZ];
 	uint8_t nonce[DATA_SIZ];
 };
 
+#define MINION_RES_DATA_SIZ sizeof(struct minion_result)
+
 #define MIDSTATE_BYTES 32
 #define MERKLE7_OFFSET 64
 #define MERKLE_BYTES 12
@@ -123,8 +184,8 @@ struct minion_result {
 #define MINION_MAX_TASK_ID 0xffff
 
 struct minion_que {
-	uint8_t reserved[2];
 	uint8_t task_id[2];
+	uint8_t reserved[2];
 	uint8_t midstate[MIDSTATE_BYTES];
 	uint8_t merkle7[DATA_SIZ];
 	uint8_t ntime[DATA_SIZ];
@@ -148,10 +209,12 @@ typedef struct titem {
 	bool write;
 	uint8_t address;
 	uint32_t task_id;
-	uint32_t siz;
-	uint8_t wbuf[1024]; // TODO: tune the size of these 3
-	uint8_t obuf[1024];
-	uint8_t rbuf[1024];
+	uint32_t wsiz;
+	uint32_t osiz;
+	uint32_t rsiz;
+	uint8_t wbuf[MINION_BUFSIZ];
+	uint8_t obuf[MINION_BUFSIZ];
+	uint8_t rbuf[MINION_BUFSIZ];
 	int reply;
 	bool urgent;
 	struct work *work;
@@ -168,6 +231,7 @@ typedef struct ritem {
 } RITEM;
 
 typedef struct k_item {
+	const char *name;
 	struct k_item *prev;
 	struct k_item *next;
 	void *data;
@@ -186,7 +250,7 @@ typedef struct k_list {
 	size_t siz;		// item data size
 	int total;		// total allocated
 	int count;		// in this list
-	int stale_count;	// for the work list
+	int stale_count;	// for the chip active work lists
 	int allocate;		// number to intially allocate and each time we run out
 	bool do_tail;		// store tail
 } K_LIST;
@@ -210,12 +274,10 @@ struct minion_info {
 	struct thr_info res_thr;
 
 	pthread_mutex_t spi_lock;
+	pthread_mutex_t sta_lock;
 
-	// TODO: can there be 2x fd's one for each spi thread?
-	// or do I need to have one (as current code) and lock access each ioctl?
 	int spifd;
-	// TODO: need to track disabled chips
-	// detect chip scan will need to check all chips
+	// TODO: need to track disabled chips - done?
 	int chips;
 	bool chip[MINION_CHIPS];
 
@@ -228,6 +290,8 @@ struct minion_info {
 	uint64_t core_good[MINION_CHIPS][MINION_CORES];
 	uint64_t core_bad[MINION_CHIPS][MINION_CORES];
 
+	struct minion_status chip_status[MINION_CHIPS];
+
 	pthread_mutex_t nonce_lock;
 	uint64_t new_nonces;
 
@@ -243,7 +307,7 @@ struct minion_info {
 	// Task list
 	K_LIST *tfree_list;
 	K_STORE *task_list;
-	K_STORE *reply_list;
+	K_STORE *treply_list;
 
 	// Nonce replies
 	K_LIST *rfree_list;
@@ -254,14 +318,14 @@ struct minion_info {
 	bool initialised;
 };
 
-static void alloc_items(K_LIST *list)
+static void alloc_items(K_LIST *list, MINION_FFL_ARGS)
 {
 	K_ITEM *item;
 	int i;
 
 	if (list->is_store) {
-		quithere(1, "List %s store can't %s",
-				list->name, __func__);
+		quithere(1, "List %s store can't %s" MINION_FFL,
+				list->name, __func__, MINION_FFL_PASS);
 	}
 
 	item = calloc(list->allocate, sizeof(*item));
@@ -273,21 +337,32 @@ static void alloc_items(K_LIST *list)
 	list->total += list->allocate;
 	list->count = list->allocate;
 
+	item[0].name = list->name;
 	item[0].prev = NULL;
 	item[0].next = &(item[1]);
 	for (i = 1; i < list->allocate-1; i++) {
+		item[i].name = list->name;
 		item[i].prev = &item[i-1];
 		item[i].next = &item[i+1];
 	}
+	item[list->allocate-1].name = list->name;
 	item[list->allocate-1].prev = &(item[list->allocate-2]);
 	item[list->allocate-1].next = NULL;
 
 	list->head = item;
 	if (list->do_tail)
 		list->tail = &(item[list->allocate-1]);
+
+	item = list->head;
+	while (item) {
+		item->data = calloc(1, list->siz);
+		if (!(item->data))
+			quithere(1, "List %s failed to calloc item data", list->name);
+		item = item->next;
+	}
 }
 
-static K_STORE *new_store(K_LIST *list, bool do_tail)
+static K_STORE *new_store(K_LIST *list)
 {
 	K_STORE *store;
 
@@ -298,12 +373,12 @@ static K_STORE *new_store(K_LIST *list, bool do_tail)
 	store->is_store = true;
 	store->lock = list->lock;
 	store->name = list->name;
-	list->do_tail = do_tail;
+	store->do_tail = list->do_tail;
 
 	return store;
 }
 
-static K_LIST *new_list(const char *name, size_t siz, int allocate, bool do_tail)
+static K_LIST *new_list(const char *name, size_t siz, int allocate, bool do_tail, MINION_FFL_ARGS)
 {
 	K_LIST *list;
 
@@ -327,26 +402,27 @@ static K_LIST *new_list(const char *name, size_t siz, int allocate, bool do_tail
 	list->allocate = allocate;
 	list->do_tail = do_tail;
 
-	alloc_items(list);
+	alloc_items(list, MINION_FFL_PASS);
 
 	return list;
 }
 
-static K_ITEM *k_get_head(K_LIST *list)
+static K_ITEM *k_get_head(K_LIST *list, MINION_FFL_ARGS)
 {
 	K_ITEM *item;
 
+	if (!(list->head))
+		alloc_items(list, MINION_FFL_PASS);
+
 	item = list->head;
-	if (item) {
-		list->count--;
-		if (item->next) {
-			list->head = item->next;
-			list->head->prev = NULL;
-		} else {
-			list->head = NULL;
-			if (list->do_tail)
-				list->tail = NULL;
-		}
+	list->count--;
+	if (item->next) {
+		list->head = item->next;
+		list->head->prev = NULL;
+	} else {
+		list->head = NULL;
+		if (list->do_tail)
+			list->tail = NULL;
 	}
 	item->prev = NULL;
 	item->next = NULL;
@@ -354,8 +430,13 @@ static K_ITEM *k_get_head(K_LIST *list)
 	return item;
 }
 
-static void k_add_head(K_LIST *list, K_ITEM *item)
+static void k_add_head(K_LIST *list, K_ITEM *item, MINION_FFL_ARGS)
 {
+	if (item->name != list->name) {
+		quithere(1, "List %s can't %s a %s item" MINION_FFL,
+				list->name, __func__, item->name, MINION_FFL_PASS);
+	}
+
 	item->prev = NULL;
 	item->next = list->head;
 	if (list->head)
@@ -366,6 +447,8 @@ static void k_add_head(K_LIST *list, K_ITEM *item)
 			list->tail = item;
 	}
 	list->head = item;
+
+	list->count++;
 }
 
 static void k_remove(K_LIST *list, K_ITEM *item)
@@ -400,7 +483,7 @@ static void ready_work(struct cgpu_info *minioncgpu, struct work *work)
 
 	K_WLOCK(minioninfo->wfree_list);
 
-	item = k_get_head(minioninfo->wfree_list);
+	item = k_get_head(minioninfo->wfree_list, MINION_FFL_HERE);
 
 	DATAW(item)->work = work;
 	DATAW(item)->task_id = 0;
@@ -408,7 +491,7 @@ static void ready_work(struct cgpu_info *minioncgpu, struct work *work)
 	DATAW(item)->nonces = 0;
 	DATAW(item)->urgent = false;
 
-	k_add_head(minioninfo->wwork_list, item);
+	k_add_head(minioninfo->wwork_list, item, MINION_FFL_HERE);
 
 	K_WUNLOCK(minioninfo->wfree_list);
 }
@@ -433,7 +516,7 @@ static bool oldest_nonce(struct cgpu_info *minioncgpu, int *chip, int *core, uin
 		*nonce = DATAR(item)->nonce;
 		*no_nonce = DATAR(item)->no_nonce;
 
-		k_add_head(minioninfo->rfree_list, item);
+		k_add_head(minioninfo->rfree_list, item, MINION_FFL_HERE);
 	}
 
 	K_WUNLOCK(minioninfo->rnonce_list);
@@ -441,38 +524,186 @@ static bool oldest_nonce(struct cgpu_info *minioncgpu, int *chip, int *core, uin
 	return found;
 }
 
-#define MINION_UNKNOWN_TASK -999
+static const char *addr2txt(uint8_t addr)
+{
+	switch (addr) {
+		case READ_ADDR(MINION_SYS_CHIP_SIG):
+			return "ReadChipSig";
+		case READ_ADDR(MINION_SYS_CHIP_STA):
+			return "ReadChipStatus";
+		case WRITE_ADDR(MINION_SYS_MISC_CTL):
+			return "WriteMiscControl";
+		case WRITE_ADDR(MINION_SYS_RSTN_CTL):
+			return "WriteResetControl";
+		case READ_ADDR(MINION_CORE_ENA0_31):
+			return "ReadCoreEnable0-31";
+		case WRITE_ADDR(MINION_CORE_ENA0_31):
+			return "WriteCoreEnable0-31";
+		case READ_ADDR(MINION_CORE_ENA32_63):
+			return "ReadCoreEnable32-63";
+		case WRITE_ADDR(MINION_CORE_ENA32_63):
+			return "WriteCoreEnable32-63";
+		case READ_ADDR(MINION_RES_DATA):
+			return "ReadResultData";
+		case WRITE_ADDR(MINION_QUE_0):
+			return "WriteQueWork";
+		case READ_ADDR(MINION_NONCE_RANGE):
+			return "ReadNonceRange";
+		case WRITE_ADDR(MINION_NONCE_RANGE):
+			return "WriteNonceRange";
+	}
+
+	// gcc warning if this is in default:
+	if (IS_ADDR_READ(addr))
+		return "ReadUnhandled";
+	else
+		return "WriteUnhandled";
+}
+
+// For display_ioctl()
+#define IOCTRL_LOG LOG_ERR
+
+// For all other debug so it can easily be switched always on
+#define MINION_LOG LOG_ERR
+
+static void display_ioctl(int reply, uint32_t osiz, uint8_t *obuf, uint32_t rsiz, uint8_t *rbuf)
+{
+	struct minion_result *res;
+	const char *name, *dir, *ex;
+	char buf[1024];
+
+	name = addr2txt(obuf[1]);
+
+	if (IS_ADDR_READ(obuf[1]))
+		dir = "from";
+	else
+		dir = "to";
+
+	buf[0] = '\0';
+	ex = "";
+
+	switch (obuf[1]) {
+		case READ_ADDR(MINION_SYS_CHIP_SIG):
+		case READ_ADDR(MINION_SYS_CHIP_STA):
+			break;
+		case WRITE_ADDR(MINION_SYS_MISC_CTL):
+		case WRITE_ADDR(MINION_SYS_RSTN_CTL):
+			if (osiz > HSIZE()) {
+				ex = " wrote ";
+				__bin2hex(buf, obuf + HSIZE(), osiz - HSIZE());
+			} else
+				ex = " wrote nothing";
+			break;
+		default:
+			if (IS_ADDR_WRITE(obuf[1])) {
+				if (osiz > HSIZE()) {
+					ex = " wrote ";
+					__bin2hex(buf, obuf + HSIZE(), osiz - HSIZE());
+				} else
+					ex = " wrote nothing";
+			}
+			break;
+	}
+
+	if (reply < 0) {
+		applog(IOCTRL_LOG, "%s %s chip %d osiz %d%s%s",
+				   name, dir, (int)obuf[0], (int)osiz, ex, buf);
+		applog(IOCTRL_LOG, "  reply was error %d", reply);
+	} else {
+		if (IS_ADDR_WRITE(obuf[1])) {
+			applog(IOCTRL_LOG, "%s %s chip %d osiz %d%s%s",
+					   name, dir, (int)obuf[0], (int)osiz, ex, buf);
+			applog(IOCTRL_LOG, "  write ret was %d", reply);
+		} else {
+			switch (obuf[1]) {
+				case READ_ADDR(MINION_RES_DATA):
+					res = (struct minion_result *)(rbuf + osiz - rsiz);
+					if (!IS_RESULT(res)) {
+//						applog(IOCTRL_LOG, "%s %s chip %d osiz %d%s%s",
+//								   name, dir, (int)obuf[0], (int)osiz, ex, buf);
+//						applog(IOCTRL_LOG, "  %s reply - none", name);
+					} else {
+						applog(IOCTRL_LOG, "%s %s chip %d osiz %d%s%s",
+								   name, dir, (int)obuf[0], (int)osiz, ex, buf);
+						__bin2hex(buf, res->nonce, DATA_SIZ);
+						applog(IOCTRL_LOG, "  %s reply %d(%d) was task 0x%04x"
+								   " chip %d core %d gold %s nonce 0x%s",
+								   name, reply, rsiz,
+								   RES_TASK(res),
+								   (int)RES_CHIP(res),
+								   (int)RES_CORE(res),
+								   (int)RES_GOLD(res) ? "Y" : "N",
+								   buf);
+					}
+					break;
+				case READ_ADDR(MINION_SYS_CHIP_SIG):
+				case READ_ADDR(MINION_SYS_CHIP_STA):
+				default:
+					applog(IOCTRL_LOG, "%s %s chip %d osiz %d%s%s",
+							   name, dir, (int)obuf[0], (int)osiz, ex, buf);
+					__bin2hex(buf, rbuf + osiz - rsiz, rsiz);
+					applog(IOCTRL_LOG, "  %s reply %d(%d) was %s", name, reply, rsiz, buf);
+					break;
+			}
+		}
+	}
+}
+
+#define MINION_UNEXPECTED_TASK -999
 #define MINION_OVERSIZE_TASK -998
 
-static int do_ioctl(struct minion_info *minioninfo, uintptr_t wbuf, uint32_t wsiz, uintptr_t rbuf)
+static int do_ioctl(struct minion_info *minioninfo, uint8_t *obuf, uint32_t osiz, uint8_t *rbuf, uint32_t rsiz)
 {
 	struct spi_ioc_transfer tran;
 	int ret;
 
+	memset(((char *)obuf) + osiz - rsiz, 0xff, rsiz);
+
+/*
+char *buf = bin2hex((char *)obuf, osiz);
+applog(LOG_ERR, "*** %s sending %s", __func__, buf);
+free(buf);
+*/
+
+	memset((char *)rbuf, 0x00, osiz);
+
+	cgsleep_ms(50); // TODO: a delay ... based on the last command? But subtract elapsed
+			// i.e. do any commands need a delay after the I/O has completed before the next I/O?
+
 	memset(&tran, 0, sizeof(tran));
-	if (wsiz < MINION_SPI_BUFSIZ)
-		tran.len = wsiz;
+	if (osiz < MINION_SPI_BUFSIZ)
+		tran.len = osiz;
 	else
 		return MINION_OVERSIZE_TASK;
+
 	tran.delay_usecs = 0;
 	tran.speed_hz = MINION_SPI_SPEED;
 
-	tran.tx_buf = wbuf;
-	tran.rx_buf = rbuf;
+	tran.tx_buf = (uintptr_t)obuf;
+	tran.rx_buf = (uintptr_t)rbuf;
 	tran.speed_hz = MINION_SPI_SPEED;
 
 	mutex_lock(&(minioninfo->spi_lock));
 	ret = ioctl(minioninfo->spifd, SPI_IOC_MESSAGE(1), (void *)&tran);
 	mutex_unlock(&(minioninfo->spi_lock));
 
+/*
+if (ret > 0) {
+buf = bin2hex((char *)rbuf, ret);
+applog(LOG_ERR, "*** %s reply %d = %s", __func__, ret, buf);
+free(buf);
+} else
+applog(LOG_ERR, "*** %s reply = %d", __func__, ret);
+*/
+
+	display_ioctl(ret, osiz, obuf, rsiz, rbuf);
+
 	return ret;
 }
 
-static bool _minion_txrx(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, TITEM *task, bool detect_ignore, const char *file, const char *func, const int line)
+static bool _minion_txrx(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, TITEM *task, bool detect_ignore, MINION_FFL_ARGS)
 {
 	struct minion_header *head;
-	uintptr_t obuf, rbuf;
-	uint32_t wsiz;
 
 	head = (struct minion_header *)(task->obuf);
 	head->chip = task->chip;
@@ -481,53 +712,238 @@ static bool _minion_txrx(struct cgpu_info *minioncgpu, struct minion_info *minio
 	else
 		SET_HEAD_READ(head, task->address);
 
-	SET_HEAD_SIZ(head, task->siz); // TODO: divide by 4?
-
-	memcpy(&(head->data[0]), task->wbuf, task->siz);
+	SET_HEAD_SIZ(head, task->wsiz + task->rsiz);
 
-	obuf = (uintptr_t)(&(task->obuf));
-	wsiz = HSIZE() + task->siz;
-	rbuf = (uintptr_t)(&(task->rbuf));
+	if (task->wsiz)
+		memcpy(&(head->data[0]), task->wbuf, task->wsiz);
+	task->osiz = HSIZE() + task->wsiz + task->rsiz;
 
-	task->reply = do_ioctl(minioninfo, obuf, wsiz, rbuf);
+	task->reply = do_ioctl(minioninfo, task->obuf, task->osiz, task->rbuf, task->rsiz);
 //TODO:	if (task->reply < 0 && (!detect_ignore || errno != 110)) {
 	if (task->reply < 0) {
-		applog(LOG_ERR, "%s%d: ioctl failed err=%d" MINION_FFL,
+		applog(LOG_ERR, "%s%d: ioctl failed reply=%d err=%d" MINION_FFL,
+				minioncgpu->drv->name, minioncgpu->device_id,
+				task->reply, errno, MINION_FFL_PASS);
+	} else if (task->reply < (int)(task->osiz)) {
+		applog(LOG_ERR, "%s%d: ioctl failed to write %d only wrote %d (err=%d)" MINION_FFL,
 				minioncgpu->drv->name, minioncgpu->device_id,
-				errno, MINION_FFL_PASS);
+				(int)(task->osiz), task->reply, errno, MINION_FFL_PASS);
 	}
-	return (task->reply >= 0);
+
+	return (task->reply >= (int)(task->osiz));
 }
 
-// Simple detect - just check each chip for the signature
-// TODO: retry on failure?
-void minion_detect_chips(struct cgpu_info *minioncgpu, struct minion_info *minioninfo)
+// TODO: hard coded for now
+void init_chip(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, int chip)
 {
-	struct minion_header head;
-	uint8_t rbuf[32];
+	struct minion_header *head;
+	uint8_t rbuf[MINION_BUFSIZ];
+	uint8_t wbuf[MINION_BUFSIZ];
 	uint32_t wsiz;
-	int chip, reply;
+	int reply;
 
-	SET_HEAD_READ(&head, MINION_SYS_CHIP_SIG);
-	SET_HEAD_SIZ(&head, 0);
-	wsiz = HSIZE();
-	for (chip = 0; chip < MINION_CHIPS; chip++) {
-		head.chip = (uint8_t)chip;
+	head = (struct minion_header *)wbuf;
+	head->chip = chip;
+	SET_HEAD_WRITE(head, MINION_SYS_RSTN_CTL);
+	SET_HEAD_SIZ(head, MINION_SYS_SIZ);
+	head->data[0] = 0x00;
+	head->data[1] = 0x00;
+	head->data[2] = 0xa5;
+	head->data[3] = 0xf5;
+
+	wsiz = HSIZE() + MINION_SYS_SIZ;
+	reply = do_ioctl(minioninfo, wbuf, wsiz, rbuf, 0);
+
+	if (reply != (int)wsiz) {
+		applog(LOG_ERR, "%s: chip %d reset full returned %d (should be %d)",
+				minioncgpu->drv->dname, chip, reply, (int)wsiz);
+	}
+
+	head = (struct minion_header *)wbuf;
+	head->chip = chip;
+	SET_HEAD_WRITE(head, MINION_SYS_RSTN_CTL);
+	SET_HEAD_SIZ(head, MINION_SYS_SIZ);
+	head->data[0] = SYS_RSTN_CTL_INIT;
+	head->data[1] = 0x00;
+	head->data[2] = 0x00;
+	head->data[3] = 0x00;
+
+	wsiz = HSIZE() + MINION_SYS_SIZ;
+	reply = do_ioctl(minioninfo, wbuf, wsiz, rbuf, 0);
+
+	if (reply != (int)wsiz) {
+		applog(LOG_ERR, "%s: chip %d reset init returned %d (should be %d)",
+				minioncgpu->drv->dname, chip, reply, (int)wsiz);
+	}
+
+	head = (struct minion_header *)wbuf;
+	head->chip = chip;
+	SET_HEAD_WRITE(head, MINION_SYS_MISC_CTL);
+	SET_HEAD_SIZ(head, MINION_SYS_SIZ);
+	head->data[0] = SYS_MISC_CTL_DEFAULT;
+	head->data[1] = 0x00;
+	head->data[2] = 0x00;
+	head->data[3] = 0x00;
+
+	wsiz = HSIZE() + MINION_SYS_SIZ;
+	reply = do_ioctl(minioninfo, wbuf, wsiz, rbuf, 0);
+
+	if (reply != (int)wsiz) {
+		applog(LOG_ERR, "%s: chip %d control returned %d (should be %d)",
+				minioncgpu->drv->dname, chip, reply, (int)wsiz);
+	}
+}
+
+// TODO: hard coded for now
+void enable_chip_cores(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, int chip)
+{
+	struct minion_header *head;
+	uint8_t rbuf[MINION_BUFSIZ];
+	uint8_t wbuf[MINION_BUFSIZ];
+	uint32_t wsiz;
+	int reply;
+
+	// First see what it reports as
+	head = (struct minion_header *)wbuf;
+	head->chip = chip;
+	SET_HEAD_READ(head, MINION_CORE_ENA0_31);
+	SET_HEAD_SIZ(head, MINION_CORE_SIZ);
+
+	wsiz = HSIZE() + MINION_CORE_SIZ;
+	reply = do_ioctl(minioninfo, wbuf, wsiz, rbuf, MINION_CORE_SIZ);
+
+	if (reply != (int)wsiz) {
+		applog(LOG_ERR, "%s: chip %d core0-31 read returned %d (should be %d)",
+				minioncgpu->drv->dname, chip, reply, (int)wsiz);
+	}
+
+	SET_HEAD_READ(head, MINION_CORE_ENA32_63);
+	reply = do_ioctl(minioninfo, wbuf, wsiz, rbuf, MINION_CORE_SIZ);
+
+	if (reply != (int)wsiz) {
+		applog(LOG_ERR, "%s: chip %d core0-31 read returned %d (should be %d)",
+				minioncgpu->drv->dname, chip, reply, (int)wsiz);
+	}
+
+	head = (struct minion_header *)wbuf;
+	head->chip = chip;
+	SET_HEAD_WRITE(head, MINION_CORE_ENA0_31);
+	SET_HEAD_SIZ(head, MINION_CORE_SIZ);
+/*
+	head->data[0] = 0xff;
+	head->data[1] = 0xff;
+	head->data[2] = 0xff;
+	head->data[3] = 0xff;
+*/
+	/*
+	 * there really is no reason to do this except in testing
+	 * since when mining with real data it will still mine at
+	 * full speed, however if we are testing for specific
+	 * results, not mining speed, then it's necessary to force
+	 * the nonce ranges to be done fully on incomplete hardware
+	 */
+	head->data[0] = 0x0e; // only core 1,2,3
+	head->data[1] = 0x00;
+	head->data[2] = 0x00;
+	head->data[3] = 0x00;
+
+	wsiz = HSIZE() + MINION_CORE_SIZ;
+	reply = do_ioctl(minioninfo, wbuf, wsiz, rbuf, 0);
+
+	if (reply != (int)wsiz) {
+		applog(LOG_ERR, "%s: chip %d core0-31 enable returned %d (should be %d)",
+				minioncgpu->drv->dname, chip, reply, (int)wsiz);
+	}
+
+	head->data[0] = 0x00;
+	head->data[1] = 0x00;
+	head->data[2] = 0x00;
+	head->data[3] = 0x00;
+
+	SET_HEAD_WRITE(head, MINION_CORE_ENA32_63);
+	reply = do_ioctl(minioninfo, wbuf, wsiz, rbuf, 0);
 
-		reply = do_ioctl(minioninfo, (uintptr_t)(&head), wsiz, (uintptr_t)&(rbuf[0]));
-		if (reply == 4) {
-			uint32_t sig = rbuf[0] + rbuf[1] * 0x100 + rbuf[2] * 0x10000 + rbuf[3] * 0x1000000;
+	if (reply != (int)wsiz) {
+		applog(LOG_ERR, "%s: chip %d core32-63 enable returned %d (should be %d)",
+				minioncgpu->drv->dname, chip, reply, (int)wsiz);
+	}
+
+	SET_HEAD_WRITE(head, MINION_NONCE_RANGE);
+	head->data[0] = 0x55;
+	head->data[1] = 0x55;
+	head->data[2] = 0x55;
+	head->data[3] = 0x55;
+
+	// quicker results
+//	head->data[0] = 0x5;
+//	head->data[1] = 0x5;
+//	head->data[2] = 0x5;
+//	head->data[3] = 0x5;
+
+	wsiz = HSIZE() + MINION_CORE_SIZ;
+	reply = do_ioctl(minioninfo, wbuf, wsiz, rbuf, 0);
+
+	if (reply != (int)wsiz) {
+		applog(LOG_ERR, "%s: chip %d nonce range returned %d (should be %d)",
+				minioncgpu->drv->dname, chip, reply, (int)wsiz);
+	}
+}
 
-			if (sig == MINION_CHIP_SIG) {
-				minioninfo->chip[chip] = true;
-				minioninfo->chips++;
+// Simple detect - just check each chip for the signature
+void minion_detect_chips(struct cgpu_info *minioncgpu, struct minion_info *minioninfo)
+{
+	struct minion_header *head;
+	uint8_t wbuf[MINION_BUFSIZ];
+	uint8_t rbuf[MINION_BUFSIZ];
+	uint32_t wsiz, rsiz;
+	int chip, reply, tries;
+	bool ok;
+
+	head = (struct minion_header *)wbuf;
+	rsiz = MINION_SYS_SIZ;
+	SET_HEAD_READ(head, MINION_SYS_CHIP_SIG);
+	SET_HEAD_SIZ(head, rsiz);
+	wsiz = HSIZE() + rsiz;
+	for (chip = 0; chip < MINION_CHIPS; chip++) {
+		head->chip = (uint8_t)chip;
+		tries = 0;
+		ok = false;
+		do {
+			reply = do_ioctl(minioninfo, wbuf, wsiz, rbuf, rsiz);
+
+			if (reply == (int)(wsiz)) {
+				uint32_t sig = u8tou32(rbuf, wsiz - rsiz);
+
+				if (sig == MINION_CHIP_SIG) {
+					minioninfo->chip[chip] = true;
+					minioninfo->chips++;
+					ok = true;
+				} else {
+					if (sig == MINION_NOCHIP_SIG)
+						ok = true;
+					else {
+						applog(LOG_ERR, "%s: chip %d detect failed got"
+								" 0x%08x wanted 0x%08x",
+								minioncgpu->drv->dname, chip, sig,
+								MINION_CHIP_SIG);
+					}
+				}
 			} else {
-				applog(LOG_ERR, "%s: chip %d detect failed got 0x%08x wanted 0x%08x",
-						minioncgpu->drv->dname, chip, sig, MINION_CHIP_SIG);
+				applog(LOG_ERR, "%s: chip %d reply %d ignored should be %d",
+						minioncgpu->drv->dname, chip, reply, (int)(wsiz));
 			}
-		} else {
-			applog(LOG_ERR, "%s: chip %d reply %d ignored",
-					minioncgpu->drv->dname, chip, reply);
+		} while (!ok && ++tries < 3);
+
+		if (!ok) {
+			applog(LOG_ERR, "%s: chip %d - detect failure status",
+					minioncgpu->drv->dname, chip);
+		}
+	}
+
+	for (chip = 0; chip < MINION_CHIPS; chip++) {
+		if (minioninfo->chip[chip]) {
+			init_chip(minioncgpu, minioninfo, chip);
+			enable_chip_cores(minioncgpu, minioninfo, chip);
 		}
 	}
 }
@@ -544,12 +960,12 @@ static struct {
 	int request;
 	int value;
 } minion_ioc[] = {
-	{ SPI_IOC_RD_MODE, 0 }, // ?
-	{ SPI_IOC_WR_MODE, 0 }, // ?
-	{ SPI_IOC_RD_BITS_PER_WORD, 8 }, // 32? 8?
-	{ SPI_IOC_WR_BITS_PER_WORD, 8 }, // 32? 8?
-	{ SPI_IOC_RD_MAX_SPEED_HZ, 1000000 }, // 3000000 ?
-	{ SPI_IOC_WR_MAX_SPEED_HZ, 1000000 }, // 3000000 ?
+	{ SPI_IOC_RD_MODE, 0 },
+	{ SPI_IOC_WR_MODE, 0 },
+	{ SPI_IOC_RD_BITS_PER_WORD, 8 },
+	{ SPI_IOC_WR_BITS_PER_WORD, 8 },
+	{ SPI_IOC_RD_MAX_SPEED_HZ, 1000000 },
+	{ SPI_IOC_WR_MAX_SPEED_HZ, 1000000 },
 	{ -1, -1 }
 };
 
@@ -584,7 +1000,7 @@ static bool minion_init_spi(struct cgpu_info *minioncgpu, struct minion_info *mi
 		data = minion_ioc[i].value;
 		err = ioctl(minioninfo->spifd, minion_ioc[i].request, (void *)&data);
 		if (err < 0) {
-			applog(LOG_ERR, "%s: failed ioctl (%d) (%d)",
+			applog(LOG_ERR, "%s: failed ioctl configuration (%d) (%d)",
 					minioncgpu->drv->dname,
 					i, errno);
 			goto close_out;
@@ -629,12 +1045,15 @@ static void minion_detect(bool hotplug)
 		goto unalloc;
 
 	mutex_init(&(minioninfo->spi_lock));
+	mutex_init(&(minioninfo->sta_lock));
 
 	applog(LOG_WARNING, "%s: checking for chips ...", minioncgpu->drv->dname);
 
 	minion_detect_chips(minioncgpu, minioninfo);
 
-	applog(LOG_WARNING, "%s: found %d chips", minioncgpu->drv->dname, minioninfo->chips);
+	applog(LOG_WARNING, "%s: found %d chip%s",
+				minioncgpu->drv->dname, minioninfo->chips,
+				(minioninfo->chips == 1) ? "" : "s");
 
 	if (minioninfo->chips == 0)
 		goto cleanup;
@@ -644,17 +1063,18 @@ static void minion_detect(bool hotplug)
 
 	mutex_init(&(minioninfo->nonce_lock));
 
-	minioninfo->wfree_list = new_list("Work", sizeof(WITEM), ALLOC_WITEMS, true);
-	minioninfo->wwork_list = new_store(minioninfo->wfree_list, true);
+	minioninfo->wfree_list = new_list("Work", sizeof(WITEM), ALLOC_WITEMS, true, MINION_FFL_HERE);
+	minioninfo->wwork_list = new_store(minioninfo->wfree_list);
 	// Initialise them all in case we later decide to enable chips
 	for (i = 0; i < minioninfo->chips; i++)
-		minioninfo->wchip_list[i] = new_store(minioninfo->wfree_list, true);
+		minioninfo->wchip_list[i] = new_store(minioninfo->wfree_list);
 
-	minioninfo->tfree_list = new_list("Task", sizeof(TITEM), ALLOC_TITEMS, true);
-	minioninfo->task_list = new_store(minioninfo->tfree_list, true);
+	minioninfo->tfree_list = new_list("Task", sizeof(TITEM), ALLOC_TITEMS, true, MINION_FFL_HERE);
+	minioninfo->task_list = new_store(minioninfo->tfree_list);
+	minioninfo->treply_list = new_store(minioninfo->tfree_list);
 
-	minioninfo->rfree_list = new_list("Reply", sizeof(RITEM), ALLOC_RITEMS, true);
-	minioninfo->rnonce_list = new_store(minioninfo->rfree_list, true);
+	minioninfo->rfree_list = new_list("Reply", sizeof(RITEM), ALLOC_RITEMS, true, MINION_FFL_HERE);
+	minioninfo->rnonce_list = new_store(minioninfo->rfree_list);
 
 	minioninfo->initialised = true;
 
@@ -662,6 +1082,7 @@ static void minion_detect(bool hotplug)
 
 cleanup:
 	close(minioninfo->spifd);
+	mutex_destroy(&(minioninfo->sta_lock));
 	mutex_destroy(&(minioninfo->spi_lock));
 unalloc:
 	free(minioninfo);
@@ -691,18 +1112,19 @@ static void *minion_spi_write(void *userdata)
 	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
 	struct timeval start, stop;
 	K_ITEM *item;
+	TITEM *titem;
 	bool do_task;
 	double wait;
 
-	applog(LOG_DEBUG, "%s%i: SPI writing...",
-			  minioncgpu->drv->name, minioncgpu->device_id);
+	applog(MINION_LOG, "%s%i: SPI writing...",
+				minioncgpu->drv->name, minioncgpu->device_id);
 
 	// Wait until we're ready
 	while (minioncgpu->shutdown == false) {
 		if (minioninfo->initialised) {
 			break;
 		}
-		cgsleep_ms(3);
+		cgsleep_ms(3); // asap to start mining
 	}
 
 	cgtime(&start);
@@ -715,50 +1137,84 @@ static void *minion_spi_write(void *userdata)
 
 		K_WLOCK(minioninfo->task_list);
 
+// TODO: change to firstly check the whole list for an urgent task and do that
+// If none, then do the tail (if it exists)
+
 		item = minioninfo->task_list->tail;
-		if (item)
+		if (item) {
 			if (DATAT(item)->urgent)
 				do_task = true;
 
-		if (do_task)
-			k_remove(minioninfo->task_list, item);
-		else
-			item = NULL;
+			if (do_task)
+				k_remove(minioninfo->task_list, item);
+			else
+				item = NULL;
+		}
 
 		K_WUNLOCK(minioninfo->task_list);
 
 		if (item) {
-			bool dotxrx = true;
+			bool do_txrx = true;
+			bool store_reply = true;
+
+			titem = DATAT(item);
 
-			switch (DATAT(item)->address) {
-				// TODO: STA
+			switch (titem->address) {
 				// TODO: case MINION_CORE_ENA0_31:
 				// TODO: case MINION_CORE_ENA32_63:
-				// TODO: case MINION_SYS_RSTN_CTL:
 				// TODO: case MINION_SYS_TEMP_CTL:
 				// TODO: case MINION_SYS_FREQ_CTL:
-				case MINION_QUE_0:
+				case READ_ADDR(MINION_SYS_CHIP_STA):
+					store_reply = false;
+					break;
+				case WRITE_ADDR(MINION_QUE_0):
+					store_reply = false;
+					break;
+				case WRITE_ADDR(MINION_SYS_RSTN_CTL):
+					store_reply = false;
 					break;
 				default:
-					dotxrx = false;
-					DATAT(item)->reply = MINION_UNKNOWN_TASK;
-					applog(LOG_ERR, "%s%i: Unknown task address 0x%02x",
+					do_txrx = false;
+					titem->reply = MINION_UNEXPECTED_TASK;
+					applog(LOG_ERR, "%s%i: Unexpected task address 0x%02x",
 							minioncgpu->drv->name, minioncgpu->device_id,
-							(unsigned int)(DATAT(item)->address));
+							(unsigned int)(titem->address));
 
 					break;
 			}
 
-			if (dotxrx) {
+			if (do_txrx) {
 				cgtime(&start);
-				minion_txrx(DATAT(item), false);
+				minion_txrx(titem, false);
+
+				switch (titem->address) {
+					case READ_ADDR(MINION_SYS_CHIP_STA):
+						if (titem->reply >= (int)(titem->osiz)) {
+							uint8_t *rep = &(titem->rbuf[titem->osiz - titem->rsiz]);
+							int chip = titem->chip;
+
+							mutex_lock(&(minioninfo->sta_lock));
+							minioninfo->chip_status[chip].temp = STA_TEMP(rep);
+							minioninfo->chip_status[chip].cores = STA_CORES(rep);
+							minioninfo->chip_status[chip].freq = STA_FREQ(rep);
+							mutex_unlock(&(minioninfo->sta_lock));
+						}
+						break;
+					case WRITE_ADDR(MINION_QUE_0):
+					case WRITE_ADDR(MINION_SYS_RSTN_CTL):
+					default:
+						break;
+				}
 			}
 
-			K_WLOCK(minioninfo->reply_list);
-			k_add_head(minioninfo->reply_list, item);
-			K_WUNLOCK(minioninfo->reply_list);
+			K_WLOCK(minioninfo->treply_list);
+			if (store_reply)
+				k_add_head(minioninfo->treply_list, item, MINION_FFL_HERE);
+			else
+				k_add_head(minioninfo->tfree_list, item, MINION_FFL_HERE);
+			K_WUNLOCK(minioninfo->treply_list);
 
-			// always do the next task if there is one
+			// always do the next task immediately if we just did one
 			continue;
 		}
 
@@ -774,6 +1230,7 @@ static void *minion_spi_write(void *userdata)
 /*
  * SPI/ioctl reply thread
  * ioctl done every REPLY_uS checking for results
+ * TODO: interrupt handled instead
  */
 static void *minion_spi_reply(void *userdata)
 {
@@ -783,8 +1240,8 @@ static void *minion_spi_reply(void *userdata)
 	K_ITEM *item;
 	TITEM task;
 
-	applog(LOG_DEBUG, "%s%i: SPI replying...",
-			  minioncgpu->drv->name, minioncgpu->device_id);
+	applog(MINION_LOG, "%s%i: SPI replying...",
+				minioncgpu->drv->name, minioncgpu->device_id);
 
 	// Wait until we're ready
 	while (minioncgpu->shutdown == false) {
@@ -797,7 +1254,8 @@ static void *minion_spi_reply(void *userdata)
 	task.chip = 0;
 	task.write = false;
 	task.address = MINION_RES_DATA;
-	task.siz = 0;
+	task.wsiz = 0;
+	task.rsiz = MINION_RES_DATA_SIZ;
 	task.urgent = false;
 	task.work = NULL;
 
@@ -805,34 +1263,38 @@ static void *minion_spi_reply(void *userdata)
 		task.reply = 0;
 		minion_txrx(&task, false);
 		if (task.reply > 0) {
-			if (task.reply < (int)sizeof(struct minion_result)) {
-				applog(LOG_ERR, "%s%i: Bad work reply size %d should be %d",
-						minioncgpu->drv->name, minioncgpu->device_id,
-						task.reply, (int)sizeof(struct minion_result));
+			if (task.reply < (int)(task.osiz)) {
+				char *buf = bin2hex((unsigned char *)(&(task.rbuf[task.osiz - task.rsiz])), (int)(task.rsiz));
+				applog(LOG_ERR, "%s%i: Bad work reply (%s) size %d, should be %d",
+						minioncgpu->drv->name, minioncgpu->device_id, buf,
+						task.reply, (int)(task.osiz));
+				free(buf);
 			} else {
-				if (task.reply > (int)sizeof(struct minion_result)) {
-					applog(LOG_ERR, "%s%i: Unexpected work reply size %d expected %d",
+				if (task.reply > (int)(task.osiz)) {
+					applog(LOG_ERR, "%s%i: Unexpected work reply size %d, expected only %d",
 							minioncgpu->drv->name, minioncgpu->device_id,
-							task.reply, (int)sizeof(struct minion_result));
+							task.reply, (int)(task.osiz));
 				}
-				result = (struct minion_result *)&(task.rbuf[0]);
+				result = (struct minion_result *)&(task.rbuf[task.osiz - task.rsiz]);
 
-				K_WLOCK(minioninfo->rfree_list);
-				item = k_get_head(minioninfo->rfree_list);
-				K_WUNLOCK(minioninfo->rfree_list);
+				if (IS_RESULT(result)) {
+					K_WLOCK(minioninfo->rfree_list);
+					item = k_get_head(minioninfo->rfree_list, MINION_FFL_HERE);
+					K_WUNLOCK(minioninfo->rfree_list);
 
-				DATAR(item)->chip = RES_CHIP(result);
-				DATAR(item)->core = RES_CORE(result);
-				DATAR(item)->task_id = RES_TASK(result);
-				DATAR(item)->nonce = RES_NONCE(result);
-				DATAR(item)->no_nonce = !RES_GOLD(result);
+					DATAR(item)->chip = RES_CHIP(result);
+					DATAR(item)->core = RES_CORE(result);
+					DATAR(item)->task_id = RES_TASK(result);
+					DATAR(item)->nonce = RES_NONCE(result);
+					DATAR(item)->no_nonce = !RES_GOLD(result);
 
-				K_WLOCK(minioninfo->rnonce_list);
-				k_add_head(minioninfo->rnonce_list, item);
-				K_WUNLOCK(minioninfo->rnonce_list);
+					K_WLOCK(minioninfo->rnonce_list);
+					k_add_head(minioninfo->rnonce_list, item, MINION_FFL_HERE);
+					K_WUNLOCK(minioninfo->rnonce_list);
 
-				cgsleep_us(MINION_REPLY_MORE_uS);
-				continue;
+					cgsleep_us(MINION_REPLY_MORE_uS);
+					continue;
+				}
 			}
 		}
 		cgsleep_us(MINION_REPLY_uS);
@@ -842,13 +1304,46 @@ static void *minion_spi_reply(void *userdata)
 }
 
 /*
- * Find the matching work item
- * Discard any older work items
+ * Find the matching work item for this chip
+ * Discard any older work items for this chip
  */
-static bool oknonce(struct thr_info *thr, struct cgpu_info *minioncgpu, int chip, int core, uint32_t task_id, uint32_t nonce)
+
+enum nonce_state {
+	NONCE_GOOD_NONCE,
+	NONCE_NO_NONCE,
+	NONCE_BAD_NONCE,
+	NONCE_BAD_WORK,
+	NONCE_NO_WORK
+};
+
+static void cleanup_older(struct cgpu_info *minioncgpu, int chip, K_ITEM *item)
+{
+	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
+	K_ITEM *tail;
+
+	// remove older work items
+	if (item->next) {
+		K_WLOCK(minioninfo->wchip_list[chip]);
+		tail = minioninfo->wchip_list[chip]->tail;
+		while (tail && tail != item) {
+			k_remove(minioninfo->wchip_list[chip], tail);
+			K_WUNLOCK(minioninfo->wchip_list[chip]);
+			applog(MINION_LOG, "%s%i: marking complete - old task 0x%04x chip %d",
+					   minioncgpu->drv->name, minioncgpu->device_id,
+					   DATAW(tail)->task_id, chip);
+			work_completed(minioncgpu, DATAW(tail)->work);
+			K_WLOCK(minioninfo->wchip_list[chip]);
+			k_add_head(minioninfo->wfree_list, tail, MINION_FFL_HERE);
+			tail = minioninfo->wchip_list[chip]->tail;
+		}
+		K_WUNLOCK(minioninfo->wchip_list[chip]);
+	}
+}
+
+static enum nonce_state oknonce(struct thr_info *thr, struct cgpu_info *minioncgpu, int chip, int core, uint32_t task_id, uint32_t nonce, bool no_nonce)
 {
 	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
-	K_ITEM *item, *tail;
+	K_ITEM *item;
 
 	minioninfo->chip_nonces[chip]++;
 
@@ -867,7 +1362,7 @@ static bool oknonce(struct thr_info *thr, struct cgpu_info *minioncgpu, int chip
 				minioncgpu->drv->name, minioncgpu->device_id,
 				chip, core, (int)task_id);
 		minioninfo->untested_nonces++;
-		return false;
+		return NONCE_NO_WORK;
 	}
 
 	while (item) {
@@ -882,7 +1377,12 @@ static bool oknonce(struct thr_info *thr, struct cgpu_info *minioncgpu, int chip
 				minioncgpu->drv->name, minioncgpu->device_id,
 				chip, core, (int)task_id);
 		minioninfo->untested_nonces++;
-		return false;
+		return NONCE_BAD_WORK;
+	}
+
+	if (no_nonce) {
+		cleanup_older(minioncgpu, chip, item);
+		return NONCE_NO_NONCE;
 	}
 
 	minioninfo->tested_nonces++;
@@ -899,29 +1399,15 @@ static bool oknonce(struct thr_info *thr, struct cgpu_info *minioncgpu, int chip
 		mutex_unlock(&(minioninfo->nonce_lock));
 		minioninfo->ok_nonces++;
 
-		// remove older work items
-		if (item->next) {
-			K_WLOCK(minioninfo->wchip_list[chip]);
-			tail = minioninfo->wchip_list[chip]->tail;
-			while (tail && tail != item) {
-				k_remove(minioninfo->wchip_list[chip], tail);
-				K_WUNLOCK(minioninfo->wchip_list[chip]);
-				work_completed(minioncgpu, DATAW(tail)->work);
-				K_WLOCK(minioninfo->wchip_list[chip]);
-				k_add_head(minioninfo->wfree_list, tail);
-				tail = minioninfo->wchip_list[chip]->tail;
-			}
-			K_WUNLOCK(minioninfo->wchip_list[chip]);
-		}
-
-		return true;
+		cleanup_older(minioncgpu, chip, item);
+		return NONCE_GOOD_NONCE;
 	}
 
 	minioninfo->chip_bad[chip]++;
 	minioninfo->core_bad[chip][core]++;
 	inc_hw_errors(thr);
 
-	return false;
+	return NONCE_BAD_NONCE;
 }
 
 // Results checking thread
@@ -935,8 +1421,8 @@ static void *minion_results(void *userdata)
 	uint32_t nonce;
 	bool no_nonce;
 
-	applog(LOG_DEBUG, "%s%i: Results...",
-			  minioncgpu->drv->name, minioncgpu->device_id);
+	applog(MINION_LOG, "%s%i: Results...",
+				minioncgpu->drv->name, minioncgpu->device_id);
 
 	// Wait until we're ready
 	while (minioncgpu->shutdown == false) {
@@ -953,7 +1439,7 @@ static void *minion_results(void *userdata)
 			continue;
 		}
 
-		oknonce(thr, minioncgpu, chip, core, task_id, nonce);
+		oknonce(thr, minioncgpu, chip, core, task_id, nonce, no_nonce);
 	}
 
 	return NULL;
@@ -962,30 +1448,93 @@ static void *minion_results(void *userdata)
 static void minion_flush_work(struct cgpu_info *minioncgpu)
 {
 	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
+	K_ITEM *stale_unused_work, *prev_unused, *task, *prev_task;
 	int i;
 
-	applog(LOG_DEBUG, "%s%i: flushing work",
-			  minioncgpu->drv->name, minioncgpu->device_id);
+	applog(MINION_LOG, "%s%i: flushing work",
+				minioncgpu->drv->name, minioncgpu->device_id);
 
+	// set stale all wchip_list contents
+	// TODO: N.B. scanwork also gets work locks - which master thread calls flush?
 	K_WLOCK(minioninfo->wwork_list);
-	// TODO: add task flush chip (or a flag to say to flush it) - a flag will avoid an extra lock
+
 	for (i = 0; i < MINION_CHIPS; i++)
 		if (minioninfo->chip[i])
 			minioninfo->wchip_list[i]->stale_count = minioninfo->wchip_list[i]->count;
-	// TODO: discard wwork_list
+
+	// Simply remove the whole unused wwork_list
+	stale_unused_work = minioninfo->wwork_list->tail;
+	if (stale_unused_work) {
+		minioninfo->wwork_list->head = NULL;
+		minioninfo->wwork_list->tail = NULL;
+		minioninfo->wwork_list->count = 0;
+	}
+
+	// No deadlock since this is the only code to get 2 locks
+	K_WLOCK(minioninfo->tfree_list);
+	task = minioninfo->task_list->tail;
+	while (task) {
+		prev_task = task->prev;
+		if (DATAT(task)->address == WRITE_ADDR(MINION_QUE_0)) {
+			k_remove(minioninfo->task_list, task);
+			/*
+			 * Discard it - it's already in the wchip_list and
+			 * will be cleaned up by the next task on the chip
+			 */
+			k_add_head(minioninfo->tfree_list, task, MINION_FFL_HERE);
+		}
+		task = prev_task;
+	}
+	for (i = 0; i < MINION_CHIPS; i++) {
+		if (minioninfo->chip[i]) {
+			task = k_get_head(minioninfo->tfree_list, MINION_FFL_HERE);
+			DATAT(task)->chip = i;
+			DATAT(task)->write = true;
+			DATAT(task)->address = MINION_SYS_RSTN_CTL;
+			DATAT(task)->task_id = 0; // ignored
+			DATAT(task)->wsiz = MINION_SYS_SIZ;
+			DATAT(task)->wbuf[0] = SYS_RSTN_CTL_FLUSH;
+			DATAT(task)->wbuf[1] = 0;
+			DATAT(task)->wbuf[2] = 0;
+			DATAT(task)->wbuf[3] = 0;
+			DATAT(task)->urgent = true;
+			k_add_head(minioninfo->task_list, task, MINION_FFL_HERE);
+		}
+	}
+	K_WUNLOCK(minioninfo->tfree_list);
+
 	K_WUNLOCK(minioninfo->wwork_list);
 
-	// TODO: maybe send a signal to force sending new work - needs cgsem_wait in the sending thread
+	// TODO: send a signal to force getting and sending new work - needs cgsem_wait in the sending thread
+
+	// TODO: should we use this thread to do the following work?
+	if (stale_unused_work) {
+		// mark complete all stale unused work (oldest first)
+		prev_unused = stale_unused_work;
+		while (prev_unused) {
+			work_completed(minioncgpu, DATAW(prev_unused)->work);
+			prev_unused = prev_unused->prev;
+		}
+
+		// put the items back in the wfree_list (oldest first)
+		K_WLOCK(minioninfo->wfree_list);
+		while (stale_unused_work) {
+			prev_unused = stale_unused_work->prev;
+			k_add_head(minioninfo->wfree_list, stale_unused_work, MINION_FFL_HERE);
+			stale_unused_work = prev_unused;
+		}
+		K_WUNLOCK(minioninfo->wfree_list);
+	}
 }
 
-static void new_work_task(struct cgpu_info *minioncgpu, struct work *work, int chip, bool urgent)
+static void new_work_task(struct cgpu_info *minioncgpu, K_ITEM *witem, int chip, bool urgent)
 {
 	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
 	struct minion_que *que;
 	K_ITEM *item;
 
 	K_WLOCK(minioninfo->tfree_list);
-	item = k_get_head(minioninfo->tfree_list);
+	item = k_get_head(minioninfo->tfree_list, MINION_FFL_HERE);
 	K_WUNLOCK(minioninfo->tfree_list);
 
 	DATAT(item)->chip = chip;
@@ -993,22 +1542,65 @@ static void new_work_task(struct cgpu_info *minioncgpu, struct work *work, int c
 	DATAT(item)->address = MINION_QUE_0;
 
 	// if threaded access to new_work_task() is added, this will need locking
+	// Don't use task_id 0 so that we can ignore all '0' work replies
+	if (minioninfo->next_task_id == 0)
+		minioninfo->next_task_id++;
 	DATAT(item)->task_id = minioninfo->next_task_id;
+	DATAW(witem)->task_id = minioninfo->next_task_id;
 	minioninfo->next_task_id = (minioninfo->next_task_id + 1) & MINION_MAX_TASK_ID;
 
 	DATAT(item)->urgent = urgent;
-	DATAT(item)->work = work;
+	DATAT(item)->work = DATAW(witem)->work;
 
 	que = (struct minion_que *)&(DATAT(item)->wbuf[0]);
 	que->task_id[0] = DATAT(item)->task_id & 0xff;
 	que->task_id[1] = (DATAT(item)->task_id & 0xff00) << 8;
-	memcpy(&(que->midstate[0]), &(work->midstate[0]), MIDSTATE_BYTES);
-	memcpy(&(que->merkle7[0]), &(work->data[MERKLE7_OFFSET]), MERKLE_BYTES);
-	DATAT(item)->siz = (int)sizeof(*que);
+
+	memcpy(&(que->midstate[0]), &(DATAW(witem)->work->midstate[0]), MIDSTATE_BYTES);
+	memcpy(&(que->merkle7[0]), &(DATAW(witem)->work->data[MERKLE7_OFFSET]), MERKLE_BYTES);
+
+	DATAT(item)->wsiz = (int)sizeof(*que);
+	DATAT(item)->rsiz = 0;
 
 	K_WLOCK(minioninfo->task_list);
-	k_add_head(minioninfo->task_list, item);
+	k_add_head(minioninfo->task_list, item, MINION_FFL_HERE);
 	K_WUNLOCK(minioninfo->task_list);
+
+	// N.B. this will only update often enough if a chip is > ~2GH/s
+	if (!urgent) {
+		struct timeval now;
+		int limit;
+
+		cgtime(&now);
+		// No lock required since 'last' is only accessed here
+		if (minioninfo->chip_status[chip].last.tv_sec == 0) {
+			memcpy(&(minioninfo->chip_status[chip].last), &now, sizeof(now));
+		} else {
+			limit = MINION_STATS_UPDATE_TIME_MS +
+				(int)(random() % MINION_STATS_UPDATE_RAND_MS);
+			if (ms_tdiff(&now, &(minioninfo->chip_status[chip].last)) > limit) {
+				memcpy(&(minioninfo->chip_status[chip].last), &now, sizeof(now));
+
+				K_WLOCK(minioninfo->tfree_list);
+				item = k_get_head(minioninfo->tfree_list, MINION_FFL_HERE);
+				K_WUNLOCK(minioninfo->tfree_list);
+
+				DATAT(item)->chip = chip;
+				DATAT(item)->write = false;
+				DATAT(item)->address = READ_ADDR(MINION_SYS_CHIP_STA);
+				DATAT(item)->task_id = 0;
+				DATAT(item)->wsiz = 0;
+				DATAT(item)->rsiz = MINION_SYS_SIZ;
+				DATAT(item)->urgent = false;
+
+				K_WLOCK(minioninfo->task_list);
+				k_add_head(minioninfo->task_list, item, MINION_FFL_HERE);
+				K_WUNLOCK(minioninfo->task_list);
+
+				cgtime(&(minioninfo->chip_status[chip].last));
+			}
+		}
+	}
 }
 
 // TODO: stale work ...
@@ -1025,14 +1617,14 @@ static K_ITEM *next_work(struct minion_info *minioninfo)
 	return item;
 }
 
-#define MINION_QUE_HIGH 15
-#define MINION_QUE_LOW 5
+#define MINION_QUE_HIGH 4
+#define MINION_QUE_LOW 2
 
 static void minion_do_work(struct cgpu_info *minioncgpu)
 {
 	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
 	int count;
-	int state, i, j;
+	int state, chip, j;
 	K_ITEM *item;
 
 	/*
@@ -1041,24 +1633,31 @@ static void minion_do_work(struct cgpu_info *minioncgpu)
 	 *	2) push each queue up to LOW
 	 *	3) push each LOW queue up to HIGH
 	 */
-	for (state = 0; state < 2; state++) {
-		for (i = 0; i < MINION_CHIPS; i++) {
-			if (minioninfo->chip[i]) {
-				K_RLOCK(minioninfo->wchip_list[i]);
-				count = minioninfo->wchip_list[i]->count - minioninfo->wchip_list[i]->stale_count;
-				K_RUNLOCK(minioninfo->wchip_list[i]);
+	for (state = 0; state < 3; state++) {
+		for (chip = 0; chip < MINION_CHIPS; chip++) {
+			if (minioninfo->chip[chip]) {
+				K_RLOCK(minioninfo->wchip_list[chip]);
+				count = minioninfo->wchip_list[chip]->count - minioninfo->wchip_list[chip]->stale_count;
+				K_RUNLOCK(minioninfo->wchip_list[chip]);
 
 				switch (state) {
 					case 0:
 						if (count == 0) {
 							item = next_work(minioninfo);
-							if (item)
-								new_work_task(minioncgpu, DATAW(item)->work, i, true);
-							else {
+							if (item) {
+								new_work_task(minioncgpu, item, chip, true);
+								K_WLOCK(minioninfo->wchip_list[chip]);
+								k_add_head(minioninfo->wchip_list[chip], item, MINION_FFL_HERE);
+								K_WUNLOCK(minioninfo->wchip_list[chip]);
+								applog(MINION_LOG, "%s%i: 0 task 0x%04x in chip %d list",
+										   minioncgpu->drv->name,
+										   minioncgpu->device_id,
+										   DATAW(item)->task_id, chip);
+							} else {
 								applog(LOG_ERR, "%s%i: chip %d urgent empty work list",
 										minioncgpu->drv->name,
 										minioncgpu->device_id,
-										i);
+										chip);
 							}
 						}
 						break;
@@ -1066,13 +1665,20 @@ static void minion_do_work(struct cgpu_info *minioncgpu)
 						if (count < MINION_QUE_LOW) {
 							for (j = count; j < MINION_QUE_LOW; j++) {
 								item = next_work(minioninfo);
-								if (item)
-									new_work_task(minioncgpu, DATAW(item)->work, i, false);
-								else {
+								if (item) {
+									new_work_task(minioncgpu, item, chip, false);
+									K_WLOCK(minioninfo->wchip_list[chip]);
+									k_add_head(minioninfo->wchip_list[chip], item, MINION_FFL_HERE);
+									K_WUNLOCK(minioninfo->wchip_list[chip]);
+									applog(MINION_LOG, "%s%i: 1 task 0x%04x in chip %d list",
+											   minioncgpu->drv->name,
+											   minioncgpu->device_id,
+											   DATAW(item)->task_id, chip);
+								} else {
 									applog(LOG_ERR, "%s%i: chip %d non-urgent lo empty work list (count=%d)",
 											minioncgpu->drv->name,
 											minioncgpu->device_id,
-											i, j);
+											chip, j);
 								}
 							}
 						}
@@ -1081,13 +1687,20 @@ static void minion_do_work(struct cgpu_info *minioncgpu)
 						if (count <= MINION_QUE_LOW) {
 							for (j = count; j < MINION_QUE_HIGH; j++) {
 								item = next_work(minioninfo);
-								if (item)
-									new_work_task(minioncgpu, DATAW(item)->work, i, false);
-								else {
+								if (item) {
+									new_work_task(minioncgpu, item, chip, false);
+									K_WLOCK(minioninfo->wchip_list[chip]);
+									k_add_head(minioninfo->wchip_list[chip], item, MINION_FFL_HERE);
+									K_WUNLOCK(minioninfo->wchip_list[chip]);
+									applog(MINION_LOG, "%s%i: 2 task 0x%04x in chip %d list",
+											   minioncgpu->drv->name,
+											   minioncgpu->device_id,
+											   DATAW(item)->task_id, chip);
+								} else {
 									applog(LOG_ERR, "%s%i: chip %d non-urgent hi empty work list (count=%d)",
 											minioncgpu->drv->name,
 											minioncgpu->device_id,
-											i, j);
+											chip, j);
 								}
 							}
 						}
@@ -1142,8 +1755,8 @@ static void minion_shutdown(struct thr_info *thr)
 	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
 	int i;
 
-	applog(LOG_DEBUG, "%s%i: shutting down",
-			  minioncgpu->drv->name, minioncgpu->device_id);
+	applog(MINION_LOG, "%s%i: shutting down",
+				minioncgpu->drv->name, minioncgpu->device_id);
 
 	for (i = 0; i < minioninfo->chips; i++)
 		if (minioninfo->chip[i])
@@ -1200,6 +1813,38 @@ static int64_t minion_scanwork(__maybe_unused struct thr_info *thr)
 	return hashcount;
 }
 
+static void minion_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info *minioncgpu)
+{
+	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
+	uint16_t max_temp, cores;
+	int chip, sp;
+
+	max_temp = 0;
+	cores = 0;
+	mutex_lock(&(minioninfo->sta_lock));
+		for (chip = 0; chip < MINION_CHIPS; chip++) {
+			if (minioninfo->chip[chip]) {
+				cores += minioninfo->chip_status[chip].cores;
+				if (max_temp < minioninfo->chip_status[chip].temp)
+					max_temp = minioninfo->chip_status[chip].temp;
+			}
+		}
+	mutex_unlock(&(minioninfo->sta_lock));
+
+	sp = 0;
+	if (cores < 100) {
+		sp++;
+		if (cores < 10)
+			sp++;
+	}
+
+	if (max_temp > 99)
+		max_temp = 99;
+
+	tailsprintf(buf, bufsiz, "max%2dC Ch:%2d/%d%*s| ", (int)max_temp,
+				 minioninfo->chips, (int)cores, sp, "");
+}
+
 #define CHIPS_PER_STAT 8
 
 static struct api_data *minion_api_stats(struct cgpu_info *minioncgpu)
@@ -1277,7 +1922,7 @@ static struct api_data *minion_api_stats(struct cgpu_info *minioncgpu)
 	root = api_add_int(root, "TFree Total", &(minioninfo->tfree_list->total), true);
 	root = api_add_int(root, "TFree Count", &(minioninfo->tfree_list->count), true);
 	root = api_add_int(root, "Task Count", &(minioninfo->task_list->count), true);
-	root = api_add_int(root, "Reply Count", &(minioninfo->reply_list->count), true);
+	root = api_add_int(root, "Reply Count", &(minioninfo->treply_list->count), true);
 
 	root = api_add_int(root, "RFree Total", &(minioninfo->rfree_list->total), true);
 	root = api_add_int(root, "RFree Count", &(minioninfo->rfree_list->count), true);
@@ -1294,7 +1939,7 @@ struct device_drv minion_drv = {
 	.drv_detect = minion_detect,
 #ifdef LINUX
 	.get_api_stats = minion_api_stats,
-//TODO:	.get_statline_before = get_minion_statline_before,
+	.get_statline_before = minion_get_statline_before,
 	.identify_device = minion_identify,
 	.thread_prepare = minion_thread_prepare,
 	.hash_work = hash_queued_work,