Commit 48ef55a6c58fd7d8b5f2fc46988be836724dee5f

kanoi 2014-06-27T00:21:53

Merge pull request #618 from kanoi/master Minion changes and S1 fix

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
diff --git a/API-README b/API-README
index d4db726..62d3c47 100644
--- a/API-README
+++ b/API-README
@@ -442,6 +442,7 @@ The list of requests - a (*) means it requires privileged access - and replies:
                               The current options are:
                                AVA+BTB opt=freq val=256 to 1024 - chip frequency
                                BTB opt=millivolts val=1000 to 1400 - corevoltage
+                               MBA opt=reset val=0 to chipcount - reset a chip
 
  lcd           LCD            An all-in-one short status summary of the miner
                               e.g. Elapsed,GHS av,GHS 5m,GHS 5s,Temp,
diff --git a/driver-bitmain.c b/driver-bitmain.c
index cb52bc1..4f59847 100644
--- a/driver-bitmain.c
+++ b/driver-bitmain.c
@@ -921,7 +921,7 @@ static int bitmain_parse_rxnonce(const uint8_t * data, int datalen, struct bitma
 static int bitmain_read(struct cgpu_info *bitmain, unsigned char *buf,
 		       size_t bufsize, int timeout, int ep)
 {
-	struct bitmain_info *info = bitmain->device_data;
+	__maybe_unused struct bitmain_info *info = bitmain->device_data;
 	int readlen = 0, err = 0;
 
 	if (bitmain == NULL || buf == NULL || bufsize <= 0) {
@@ -936,16 +936,18 @@ static int bitmain_read(struct cgpu_info *bitmain, unsigned char *buf,
 		applog(LOG_DEBUG, "%s%i: Get %s() got readlen %d err %d",
 				  bitmain->drv->name, bitmain->device_id,
 				  __func__, readlen, err);
-	} else
+	}
+#ifdef USE_ANT_S2
+	else
 		readlen = read(info->device_fd, buf, bufsize);
-
+#endif
 	return readlen;
 }
 
 static int bitmain_write(struct cgpu_info *bitmain, char *buf, ssize_t len, int ep)
 {
-	struct bitmain_info *info = bitmain->device_data;
-	int err, amount, sent;
+	__maybe_unused struct bitmain_info *info = bitmain->device_data;
+	int err, amount, __maybe_unused sent;
 
 	if (is_usb) {
 		err = usb_write(bitmain, buf, len, &amount, ep);
@@ -964,7 +966,9 @@ static int bitmain_write(struct cgpu_info *bitmain, char *buf, ssize_t len, int 
 					amount, (int)len);
 			return BTM_SEND_ERROR;
 		}
-	} else {
+	}
+#ifdef USE_ANT_S2
+	else {
 		sent = 0;
 
 		while (sent < len) {
@@ -977,7 +981,7 @@ static int bitmain_write(struct cgpu_info *bitmain, char *buf, ssize_t len, int 
 			sent += amount;
 		}
 	}
-
+#endif
 	return BTM_SEND_OK;
 }
 
@@ -1316,8 +1320,8 @@ static void bitmain_parse_results(struct cgpu_info *bitmain, struct bitmain_info
 						applog(LOG_ERR, "%s%d: %s() Work not found for id (%"PRIu32")"
 								" (min=%"PRIu32" max=%"PRIu32" count=%d)",
 								bitmain->drv->name, bitmain->device_id,
-								__func__, rxnoncedata.nonces[j].work_id.
-								min, max);
+								__func__, rxnoncedata.nonces[j].work_id,
+								min, max, count);
 					}
 				}
 				mutex_lock(&info->qlock);
diff --git a/driver-minion.c b/driver-minion.c
index 2bcf473..90c0321 100644
--- a/driver-minion.c
+++ b/driver-minion.c
@@ -498,6 +498,8 @@ typedef struct witem {
 	bool stale; // if stale, don't decrement que/chipwork when discarded
 	bool rolled;
 	int errors; // uncertain since the error could mean task_id is wrong
+	struct timeval created; // when work was generated
+	uint64_t ioseq;
 } WITEM;
 
 #define ALLOC_TITEMS 256
@@ -520,6 +522,7 @@ typedef struct titem {
 	uint8_t work_state;
 	struct work *work;
 	K_ITEM *witem;
+	uint64_t ioseq;
 } TITEM;
 
 #define ALLOC_RITEMS 256
@@ -671,6 +674,9 @@ typedef struct iostat {
 #define IO_STAT_STORE(_sta, _fin, _lsta, _lfin, _tsd, _buf, _siz, _reply, _ioc)
 #endif
 
+static double time_bands[] = { 0.1, 0.5, 1.0, 2.0, 4.0, 8.0, 16.0, 32.0 };
+#define TIME_BANDS ((int)(sizeof(time_bands)/sizeof(double)))
+
 struct minion_info {
 	struct thr_info spiw_thr;
 	struct thr_info spir_thr;
@@ -699,6 +705,7 @@ struct minion_info {
 	uint8_t chipid[MINION_CHIPS]; // Chip Number
 	int chip_pin[MINION_CHIPS];
 
+	uint64_t ioseq;
 	uint32_t next_task_id;
 
 	// Stats
@@ -742,12 +749,17 @@ struct minion_info {
 	uint64_t nonces_recovered[MINION_CHIPS];
 	struct timeval last_reset[MINION_CHIPS];
 	double do_reset[MINION_CHIPS];
+	bool flag_reset[MINION_CHIPS];
 
 	// Work items
 	K_LIST *wfree_list;
 	K_STORE *wwork_list;
+	K_STORE *wstale_list;
 	K_STORE *wque_list[MINION_CHIPS];
 	K_STORE *wchip_list[MINION_CHIPS];
+	uint64_t wwork_flushed;
+	uint64_t wque_flushed;
+	uint64_t wchip_staled;
 
 	// Task list
 	K_LIST *tfree_list;
@@ -782,6 +794,20 @@ struct minion_info {
 	IOSTAT iostats[0x200];
 #endif
 
+	// Stats on how long work is waiting to move from wwork_list to wque_list
+	uint64_t que_work;
+	double que_time;
+	double que_min;
+	double que_max;
+	uint64_t que_bands[TIME_BANDS+1];
+
+	// From wwork_list to txrx
+	uint64_t wt_work;
+	double wt_time;
+	double wt_min;
+	double wt_max;
+	uint64_t wt_bands[TIME_BANDS+1];
+
 	bool initialised;
 };
 
@@ -801,6 +827,7 @@ static void ready_work(struct cgpu_info *minioncgpu, struct work *work, bool rol
 	DATAW(item)->urgent = false;
 	DATAW(item)->rolled = rolled;
 	DATAW(item)->errors = 0;
+	cgtime(&(DATAW(item)->created));
 
 	k_add_head(minioninfo->wwork_list, item);
 
@@ -1052,17 +1079,13 @@ static void init_pins(struct minion_info *minioninfo)
 }
 
 #define EXTRA_LOG_IO 0
-#if EXTRA_LOG_IO
-static int ioc = 0;
-#endif
 
-static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, uint32_t osiz, uint8_t *rbuf, uint32_t rsiz, MINION_FFL_ARGS)
+static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf,
+		      uint32_t osiz, uint8_t *rbuf, uint32_t rsiz,
+		      uint64_t *ioseq, MINION_FFL_ARGS)
 {
 	struct spi_ioc_transfer tran;
 	int ret;
-#if EXTRA_LOG_IO
-	int myioc;
-#endif
 #if MINION_SHOW_IO
 	char dataw[DATA_ALL], datar[DATA_ALL];
 #endif
@@ -1127,9 +1150,7 @@ static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, ui
 	}
 	IO_STAT_NOW(&sta);
 	ret = ioctl(minioninfo->spifd, SPI_IOC_MESSAGE(1), (void *)&tran);
-#if EXTRA_LOG_IO
-	myioc = ioc++;
-#endif
+	*ioseq = minioninfo->ioseq++;
 	IO_STAT_NOW(&fin);
 	if (usepins) {
 		MINION_PIN_AFTER;
@@ -1175,16 +1196,16 @@ static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, ui
 				c = 'F';
 				break;
 		}
-		applog(LOG_WARNING, "*** seq %d cmd %c %s rep %.8s %s",
-				    myioc, c, uf1, uf2, uf2+8);
+		applog(LOG_WARNING, "*** ioseq %"PRIu64" cmd %c %s rep %.8s %s",
+				    *ioseq, c, uf1, uf2, uf2+8);
 		free(uf2);
 		free(uf1);
 	}
 	if (obuf[1] == WRITE_ADDR(MINION_QUE_0)) {
 		char *uf;
 		uf = bin2hex(obuf, osiz);
-		applog(LOG_WARNING, "*** seq %d work %s",
-				    myioc, uf);
+		applog(LOG_WARNING, "*** ioseq %"PRIu64" work %s",
+				    *ioseq, uf);
 		free(uf);
 	}
 #endif
@@ -1192,11 +1213,15 @@ static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, ui
 }
 
 #if 1
-#define do_ioctl(_pin, _obuf, _osiz, _rbuf, _rsiz) __do_ioctl(minioninfo, _pin, _obuf, _osiz, _rbuf, _rsiz, MINION_FFL_HERE)
+#define do_ioctl(_pin, _obuf, _osiz, _rbuf, _rsiz, _ioseq) \
+		__do_ioctl(minioninfo, _pin, _obuf, _osiz, _rbuf, \
+			   _rsiz, _ioseq, MINION_FFL_HERE)
 #else
-#define do_ioctl(_pin, _obuf, _osiz, _rbuf, _rsiz) _do_ioctl(minioninfo, _pin, _obuf, _osiz, _rbuf, _rsiz, MINION_FFL_HERE)
+#define do_ioctl(_pin, _obuf, _osiz, _rbuf, _rsiz, _ioseq) \
+		_do_ioctl(minioninfo, _pin, _obuf, _osiz, _rbuf, \
+			  _rsiz, _ioseq, MINION_FFL_HERE)
 // This sends an expected to work, SPI command before each SPI command
-static int _do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, uint32_t osiz, uint8_t *rbuf, uint32_t rsiz, MINION_FFL_ARGS)
+static int _do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, uint32_t osiz, uint8_t *rbuf, uint32_t rsiz, uint64_t *ioseq, MINION_FFL_ARGS)
 {
 	struct minion_header *head;
 	uint8_t buf1[MINION_BUFSIZ];
@@ -1208,9 +1233,9 @@ static int _do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, uin
 	head->reg = READ_ADDR(MINION_SYS_FIFO_STA);
 	SET_HEAD_SIZ(head, DATA_SIZ);
 	siz = HSIZE() + DATA_SIZ;
-	__do_ioctl(minioninfo, pin, buf1, siz, buf2, MINION_CORE_SIZ, MINION_FFL_PASS);
+	__do_ioctl(minioninfo, pin, buf1, siz, buf2, MINION_CORE_SIZ, ioseq, MINION_FFL_PASS);
 
-	return __do_ioctl(minioninfo, pin, obuf, osiz, rbuf, rsiz, MINION_FFL_PASS);
+	return __do_ioctl(minioninfo, pin, obuf, osiz, rbuf, rsiz, ioseq, MINION_FFL_PASS);
 }
 #endif
 
@@ -1231,7 +1256,8 @@ static bool _minion_txrx(struct cgpu_info *minioncgpu, struct minion_info *minio
 		memcpy(&(head->data[0]), task->wbuf, task->wsiz);
 	task->osiz = HSIZE() + task->wsiz + task->rsiz;
 
-	task->reply = do_ioctl(CHIP_PIN(task->chip), task->obuf, task->osiz, task->rbuf, task->rsiz);
+	task->reply = do_ioctl(CHIP_PIN(task->chip), task->obuf, task->osiz, task->rbuf, task->rsiz,
+			       &(task->ioseq));
 	if (task->reply < 0) {
 		applog(LOG_ERR, "%s%d: chip=%d ioctl failed reply=%d err=%d" MINION_FFL,
 				minioncgpu->drv->name, minioncgpu->device_id,
@@ -1251,6 +1277,7 @@ static int build_cmd(struct cgpu_info *minioncgpu, struct minion_info *minioninf
 	struct minion_header *head;
 	uint8_t wbuf[MINION_BUFSIZ];
 	uint32_t wsiz;
+	uint64_t ioseq;
 	int reply;
 
 	head = (struct minion_header *)wbuf;
@@ -1264,7 +1291,7 @@ static int build_cmd(struct cgpu_info *minioncgpu, struct minion_info *minioninf
 	head->data[3] = data[3];
 
 	wsiz = HSIZE() + DATA_SIZ;
-	reply = do_ioctl(CHIP_PIN(chip), wbuf, wsiz, rbuf, rsiz);
+	reply = do_ioctl(CHIP_PIN(chip), wbuf, wsiz, rbuf, rsiz, &ioseq);
 
 	if (reply != (int)wsiz) {
 		applog(LOG_ERR, "%s: chip %d %s returned %d (should be %d)",
@@ -1526,6 +1553,7 @@ static void minion_detect_one(struct cgpu_info *minioncgpu, struct minion_info *
 	uint8_t rbuf[MINION_BUFSIZ];
 	uint32_t wsiz, rsiz;
 	int reply, tries, newchip;
+	uint64_t ioseq;
 	bool ok;
 
 	head = (struct minion_header *)wbuf;
@@ -1538,7 +1566,7 @@ static void minion_detect_one(struct cgpu_info *minioncgpu, struct minion_info *
 	tries = 0;
 	ok = false;
 	do {
-		reply = do_ioctl(pin, wbuf, wsiz, rbuf, rsiz);
+		reply = do_ioctl(pin, wbuf, wsiz, rbuf, rsiz, &ioseq);
 
 		if (reply == (int)(wsiz)) {
 			uint32_t sig = u8tou32(rbuf, wsiz - rsiz);
@@ -2129,6 +2157,7 @@ static void minion_detect(bool hotplug)
 
 	minioninfo->wfree_list = k_new_list("Work", sizeof(WITEM), ALLOC_WITEMS, LIMIT_WITEMS, true);
 	minioninfo->wwork_list = k_new_store(minioninfo->wfree_list);
+	minioninfo->wstale_list = k_new_store(minioninfo->wfree_list);
 	// Initialise them all in case we later decide to enable chips
 	for (i = 0; i < (int)MINION_CHIPS; i++) {
 		minioninfo->wque_list[i] = k_new_store(minioninfo->wfree_list);
@@ -2167,6 +2196,44 @@ unalloc:
 	free(minioncgpu);
 }
 
+static char *minion_set(struct cgpu_info *minioncgpu, char *option, char *setting, char *replybuf)
+{
+	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
+	int chip;
+
+	if (strcasecmp(option, "help") == 0) {
+		sprintf(replybuf, "reset: chip 0-%d",
+				  minioninfo->chips - 1);
+		return replybuf;
+	}
+
+	if (strcasecmp(option, "reset") == 0) {
+		if (!setting || !*setting) {
+			sprintf(replybuf, "missing chip to reset");
+			return replybuf;
+		}
+
+		chip = atoi(setting);
+		if (chip < 0 || chip >= minioninfo->chips) {
+			sprintf(replybuf, "invalid reset: chip '%s' valid range 0-%d",
+					  setting,
+					  minioninfo->chips);
+			return replybuf;
+		}
+
+		if (!minioninfo->has_chip[chip]) {
+			sprintf(replybuf, "unable to reset chip %d - chip disabled",
+					  chip);
+			return replybuf;
+		}
+		minioninfo->flag_reset[chip] = true;
+		return NULL;
+	}
+
+	sprintf(replybuf, "Unknown option: %s", option);
+	return replybuf;
+}
+
 static void minion_identify(__maybe_unused struct cgpu_info *minioncgpu)
 {
 	// flash a led
@@ -2219,6 +2286,9 @@ static void *minion_spi_write(void *userdata)
 		if (item) {
 			bool do_txrx = true;
 			bool store_reply = true;
+			struct timeval now;
+			double howlong;
+			int i;
 
 			titem = DATAT(item);
 
@@ -2255,6 +2325,25 @@ static void *minion_spi_write(void *userdata)
 			}
 
 			if (do_txrx) {
+				if (titem->witem) {
+					cgtime(&now);
+					howlong = tdiff(&now, &(DATAW(titem->witem)->created));
+					minioninfo->wt_work++;
+					minioninfo->wt_time += howlong;
+					if (minioninfo->wt_min == 0 || minioninfo->wt_min > howlong)
+						minioninfo->wt_min = howlong;
+					else if (minioninfo->wt_max < howlong)
+						minioninfo->wt_max = howlong;
+					for (i = 0; i < TIME_BANDS; i++) {
+						if (howlong < time_bands[i]) {
+							minioninfo->wt_bands[i]++;
+							break;
+						}
+					}
+					if (i >= TIME_BANDS)
+						minioninfo->wt_bands[TIME_BANDS]++;
+				}
+
 				minion_txrx(titem);
 
 				int chip = titem->chip;
@@ -2335,6 +2424,7 @@ static void *minion_spi_write(void *userdata)
 							}
 							minioninfo->chip_status[chip].chipwork = 0;
 							minioninfo->chip_status[chip].realwork = 0;
+							minioninfo->wchip_staled += cnt;
 #if MINION_SHOW_IO
 							applog(IOCTRL_LOG, "RSTN chip %d (cnt=%d) cw0=%u rw0=%u qw=%u",
 									   chip, cnt,
@@ -2349,6 +2439,7 @@ static void *minion_spi_write(void *userdata)
 						K_WLOCK(minioninfo->wchip_list[chip]);
 						k_unlink_item(minioninfo->wque_list[chip], titem->witem);
 						k_add_head(minioninfo->wchip_list[chip], titem->witem);
+						DATAW(titem->witem)->ioseq = titem->ioseq;
 						minioninfo->chip_status[chip].quework--;
 						minioninfo->chip_status[chip].chipwork++;
 #if MINION_SHOW_IO
@@ -2359,6 +2450,9 @@ static void *minion_spi_write(void *userdata)
 								   minioninfo->chip_status[chip].quework);
 #endif
 						K_WUNLOCK(minioninfo->wchip_list[chip]);
+						applog(LOG_DEBUG, "%s%d: task 0x%04x sent to chip %d",
+								  minioncgpu->drv->name, minioncgpu->device_id,
+								  titem->task_id, chip);
 						break;
 					case READ_ADDR(MINION_CORE_ENA0_31):
 					case READ_ADDR(MINION_CORE_ENA32_63):
@@ -2421,6 +2515,7 @@ static void *minion_spi_reply(void *userdata)
 	struct timeval now;
 
 #if ENABLE_INT_NONO
+	uint64_t ioseq;
 	TITEM clr_task;
 	struct pollfd pfd;
 	struct minion_header *head;
@@ -2673,6 +2768,14 @@ repeek:
 									DATAR(item)->nonce = RES_NONCE(use1);
 									DATAR(item)->no_nonce = !RES_GOLD(use1);
 									memcpy(&(DATAR(item)->when), &now, sizeof(now));
+									applog(LOG_DEBUG, "%s%i: reply task_id 0x%04x"
+											  " - chip %d - gold %d",
+											  minioncgpu->drv->name,
+											  minioncgpu->device_id,
+											  RES_TASK(use1),
+											  (int)RES_CHIPID(use1),
+											  (int)RES_GOLD(use1));
+
 									if (!use2)
 										DATAR(item)->another = false;
 									else {
@@ -2680,8 +2783,6 @@ repeek:
 										DATAR(item)->task_id2 = RES_TASK(use2);
 										DATAR(item)->nonce2 = RES_NONCE(use2);
 									}
-//applog(MINTASK_LOG, "%s%i: ZZZ reply task_id 0x%04x - chip %d - gold %d", minioncgpu->drv->name, minioncgpu->device_id, RES_TASK(use1), (int)RES_CHIPID(use1), (int)RES_GOLD(use1));
-
 //if (RES_GOLD(use1))
 //applog(MINTASK_LOG, "%s%i: found a result chip %d core %d task 0x%04x nonce 0x%08x gold=%d", minioncgpu->drv->name, minioncgpu->device_id, DATAR(item)->chip, DATAR(item)->core, DATAR(item)->task_id, DATAR(item)->nonce, (int)RES_GOLD(use1));
 
@@ -2745,7 +2846,7 @@ repeek:
 				if (minioninfo->has_chip[chip]) {
 					SET_HEAD_READ(head, MINION_SYS_INT_STA);
 					head->chipid = minioninfo->chipid[chip];
-					reply = do_ioctl(CHIP_PIN(chip), wbuf, wsiz, rbuf, rsiz);
+					reply = do_ioctl(CHIP_PIN(chip), wbuf, wsiz, rbuf, rsiz, &ioseq);
 					if (reply != (int)wsiz) {
 						applog(LOG_ERR, "%s: chip %d int status returned %d"
 								" (should be %d)",
@@ -2824,72 +2925,75 @@ static void cleanup_older(struct cgpu_info *minioncgpu, int chip, K_ITEM *item, 
 	K_ITEM *tail;
 //	bool errs;
 
-	/*
-	 * remove older work items (no_nonce means this 'item' has finished also)
-	 */
-	if (item->next || no_nonce) {
-		K_WLOCK(minioninfo->wchip_list[chip]);
-		tail = minioninfo->wchip_list[chip]->tail;
-		while (tail && tail != item) {
-			k_unlink_item(minioninfo->wchip_list[chip], tail);
-			if (!(DATAW(tail)->stale)) {
-				minioninfo->chip_status[chip].chipwork--;
+	/* remove older ioseq work items
+	   no_nonce means this 'item' has finished also */
+	tail = minioninfo->wchip_list[chip]->tail;
+	while (tail && (DATAW(tail)->ioseq < DATAW(item)->ioseq)) {
+		k_unlink_item(minioninfo->wchip_list[chip], tail);
+		if (!(DATAW(tail)->stale)) {
+			minioninfo->chip_status[chip].chipwork--;
 #if MINION_SHOW_IO
-				applog(IOCTRL_LOG, "COld chip %d cw-1=%u rw=%u qw=%u",
-						   chip,
-						   minioninfo->chip_status[chip].chipwork,
-						   minioninfo->chip_status[chip].realwork,
-						   minioninfo->chip_status[chip].quework);
+			applog(IOCTRL_LOG, "COld chip %d cw-1=%u rw=%u qw=%u",
+					   chip,
+					   minioninfo->chip_status[chip].chipwork,
+					   minioninfo->chip_status[chip].realwork,
+					   minioninfo->chip_status[chip].quework);
 #endif
 /*
-				// If it had no valid work (only errors) then it won't have been cleaned up
-				errs = (DATAW(tail)->errors > 0);
-				applog(errs ? LOG_DEBUG : LOG_ERR,
-				applog(LOG_ERR,
-					"%s%i: discarded old task 0x%04x chip %d no reply errs=%d",
-					minioncgpu->drv->name, minioncgpu->device_id,
-					DATAW(tail)->task_id, chip, DATAW(tail)->errors);
+			// If it had no valid work (only errors) then it won't have been cleaned up
+			errs = (DATAW(tail)->errors > 0);
+			applog(errs ? LOG_DEBUG : LOG_ERR,
+			applog(LOG_ERR,
+				"%s%i: discarded old task 0x%04x chip %d no reply errs=%d",
+				minioncgpu->drv->name, minioncgpu->device_id,
+				DATAW(tail)->task_id, chip, DATAW(tail)->errors);
 */
-			}
-			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);
-			if (DATAW(tail)->rolled)
-				free_work(DATAW(tail)->work);
-			else
-				work_completed(minioncgpu, DATAW(tail)->work);
-			K_WLOCK(minioninfo->wchip_list[chip]);
-			k_free_head(minioninfo->wfree_list, tail);
-			tail = minioninfo->wchip_list[chip]->tail;
 		}
-		if (no_nonce) {
-			k_unlink_item(minioninfo->wchip_list[chip], item);
-			if (!(DATAW(item)->stale)) {
-				minioninfo->chip_status[chip].chipwork--;
+		applog(MINION_LOG, "%s%i: marking complete - old task 0x%04x chip %d",
+				   minioncgpu->drv->name, minioncgpu->device_id,
+				   DATAW(tail)->task_id, chip);
+		if (DATAW(tail)->rolled)
+			free_work(DATAW(tail)->work);
+		else
+			work_completed(minioncgpu, DATAW(tail)->work);
+		k_free_head(minioninfo->wfree_list, tail);
+		tail = minioninfo->wchip_list[chip]->tail;
+	}
+	if (no_nonce) {
+		if (!(DATAW(item)->stale)) {
+			minioninfo->chip_status[chip].chipwork--;
 #if MINION_SHOW_IO
-			applog(IOCTRL_LOG, "CONoN chip %d cw-1=%u rw=%u qw=%u",
-					   chip,
-					   minioninfo->chip_status[chip].chipwork,
-					   minioninfo->chip_status[chip].realwork,
-					   minioninfo->chip_status[chip].quework);
+		applog(IOCTRL_LOG, "CONoN chip %d cw-1=%u rw=%u qw=%u",
+				   chip,
+				   minioninfo->chip_status[chip].chipwork,
+				   minioninfo->chip_status[chip].realwork,
+				   minioninfo->chip_status[chip].quework);
 #endif
-			}
-			K_WUNLOCK(minioninfo->wchip_list[chip]);
-			applog(MINION_LOG, "%s%i: marking complete - no_nonce task 0x%04x chip %d",
-					   minioncgpu->drv->name, minioncgpu->device_id,
-					   DATAW(item)->task_id, chip);
-			if (DATAW(item)->rolled)
-				free_work(DATAW(item)->work);
-			else
-				work_completed(minioncgpu, DATAW(item)->work);
-			K_WLOCK(minioninfo->wchip_list[chip]);
-			k_free_head(minioninfo->wfree_list, item);
 		}
-		K_WUNLOCK(minioninfo->wchip_list[chip]);
+		applog(MINION_LOG, "%s%i: marking complete - no_nonce task 0x%04x chip %d",
+				   minioncgpu->drv->name, minioncgpu->device_id,
+				   DATAW(item)->task_id, chip);
+		if (DATAW(item)->rolled)
+			free_work(DATAW(item)->work);
+		else
+			work_completed(minioncgpu, DATAW(item)->work);
 	}
 }
 
+// Need to put it back in the list where it was - according to ioseq
+static void restorework(struct minion_info *minioninfo, int chip, K_ITEM *item)
+{
+	K_ITEM *look;
+
+	look = minioninfo->wchip_list[chip]->tail;
+	while (look && DATAW(look)->ioseq < DATAW(item)->ioseq)
+		look = look->prev;
+	if (!look)
+		k_add_head(minioninfo->wchip_list[chip], item);
+	else
+		k_insert_after(minioninfo->wchip_list[chip], item, look);
+}
+
 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 timeval *when,
 				bool another, uint32_t task_id2, uint32_t nonce2)
@@ -2926,11 +3030,11 @@ static enum nonce_state oknonce(struct thr_info *thr, struct cgpu_info *minioncg
 
 	redo = false;
 retry:
-	K_RLOCK(minioninfo->wchip_list[chip]);
+	K_WLOCK(minioninfo->wchip_list[chip]);
 	item = minioninfo->wchip_list[chip]->tail;
 
 	if (!item) {
-		K_RUNLOCK(minioninfo->wchip_list[chip]);
+		K_WUNLOCK(minioninfo->wchip_list[chip]);
 		minioninfo->spi_errors++;
 		minioninfo->res_spi_errors[chip]++;
 		minioninfo->res_err_count[chip]++;
@@ -2952,10 +3056,9 @@ retry:
 		item = item->prev;
 	}
 	max_task_id = DATAW(minioninfo->wchip_list[chip]->head)->task_id;
-	K_RUNLOCK(minioninfo->wchip_list[chip]);
-
 
 	if (!item) {
+		K_WUNLOCK(minioninfo->wchip_list[chip]);
 		if (another && task_id != task_id2) {
 			minioninfo->tasks_failed[chip]++;
 			task_id = task_id2;
@@ -2966,7 +3069,8 @@ retry:
 		minioninfo->spi_errors++;
 		minioninfo->res_spi_errors[chip]++;
 		minioninfo->res_err_count[chip]++;
-		applog(MINTASK_LOG, "%s%i: chip %d core %d unknown task 0x%04x (min=0x%04x max=0x%04x no_nonce=%d)",
+		applog(MINTASK_LOG, "%s%i: chip %d core %d unknown task 0x%04x "
+				    "(min=0x%04x max=0x%04x no_nonce=%d)",
 				    minioncgpu->drv->name, minioncgpu->device_id,
 				    chip, core, (int)task_id, (int)min_task_id,
 				    (int)max_task_id, no_nonce);
@@ -2979,25 +3083,33 @@ retry:
 	if (redo)
 		minioninfo->tasks_recovered[chip]++;
 
+	k_unlink_item(minioninfo->wchip_list[chip], item);
 	if (no_nonce) {
 		cleanup_older(minioncgpu, chip, item, no_nonce);
+		k_free_head(minioninfo->wfree_list, item);
+		K_WUNLOCK(minioninfo->wchip_list[chip]);
 		return NONCE_NO_NONCE;
 	}
+	K_WUNLOCK(minioninfo->wchip_list[chip]);
 
 	minioninfo->tested_nonces++;
 
 	redo = false;
 retest:
 	if (test_nonce(DATAW(item)->work, nonce)) {
+/*
 		if (isdupnonce(minioncgpu, DATAW(item)->work, nonce)) {
 			minioninfo->chip_dup[chip]++;
 			applog(LOG_WARNING, " ... nonce %02x%02x%02x%02x chip %d core %d task 0x%04x",
 					    (nonce & 0xff), ((nonce >> 8) & 0xff),
 					    ((nonce >> 16) & 0xff), ((nonce >> 24) & 0xff),
 					    chip, core, task_id);
+			K_WLOCK(minioninfo->wchip_list[chip]);
+			restorework(minioninfo, chip, item);
+			K_WUNLOCK(minioninfo->wchip_list[chip]);
 			return NONCE_DUP_NONCE;
 		}
-
+*/
 //applog(MINTASK_LOG, "%s%i: Valid Nonce chip %d core %d task 0x%04x nonce 0x%08x", minioncgpu->drv->name, minioncgpu->device_id, chip, core, task_id, nonce);
 //
 		submit_tested_work(thr, DATAW(item)->work);
@@ -3014,8 +3126,12 @@ retest:
 		mutex_unlock(&(minioninfo->nonce_lock));
 		minioninfo->ok_nonces++;
 
+		K_WLOCK(minioninfo->wchip_list[chip]);
 		cleanup_older(minioncgpu, chip, item, no_nonce);
+		restorework(minioninfo, chip, item);
+		K_WUNLOCK(minioninfo->wchip_list[chip]);
 
+		// add to history and remove old history
 		int chip_tmp;
 		cgtime(&now);
 		K_WLOCK(minioninfo->hfree_list);
@@ -3043,6 +3159,10 @@ retest:
 	}
 
 	DATAW(item)->errors++;
+	K_WLOCK(minioninfo->wchip_list[chip]);
+	restorework(minioninfo, chip, item);
+	K_WUNLOCK(minioninfo->wchip_list[chip]);
+
 	minioninfo->chip_bad[chip]++;
 	minioninfo->core_bad[chip][core]++;
 	inc_hw_errors(thr);
@@ -3094,7 +3214,7 @@ 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, *witem;
+	K_ITEM *prev_unused, *task, *prev_task, *witem;
 	int i;
 
 	if (minioninfo->initialised == false)
@@ -3107,12 +3227,8 @@ static void minion_flush_work(struct cgpu_info *minioncgpu)
 	K_WLOCK(minioninfo->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;
-	}
+	k_list_transfer_to_head(minioninfo->wwork_list, minioninfo->wstale_list);
+	minioninfo->wwork_flushed += minioninfo->wstale_list->count;
 
 	// TODO: flush/work tasks should have a block sequence number so this task removal code
 	// might be better implemented in minion_spi_write where each work task would
@@ -3139,6 +3255,11 @@ static void minion_flush_work(struct cgpu_info *minioncgpu)
 #endif
 			witem = DATAT(task)->witem;
 			k_unlink_item(minioninfo->wque_list[DATAT(task)->chip], witem);
+			minioninfo->wque_flushed++;
+			if (DATAW(witem)->rolled)
+				free_work(DATAW(witem)->work);
+			else
+				work_completed(minioncgpu, DATAW(witem)->work);
 			k_free_head(minioninfo->wfree_list, witem);
 			k_unlink_item(minioninfo->task_list, task);
 			k_free_head(minioninfo->tfree_list, task);
@@ -3171,9 +3292,9 @@ static void minion_flush_work(struct cgpu_info *minioncgpu)
 	// 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) {
+	if (minioninfo->wstale_list->count) {
 		// mark complete all stale unused work (oldest first)
-		prev_unused = stale_unused_work;
+		prev_unused = minioninfo->wstale_list->tail;
 		while (prev_unused) {
 			if (DATAW(prev_unused)->rolled)
 				free_work(DATAW(prev_unused)->work);
@@ -3182,13 +3303,9 @@ static void minion_flush_work(struct cgpu_info *minioncgpu)
 			prev_unused = prev_unused->prev;
 		}
 
-		// put the items back in the wfree_list (oldest first)
+		// put them back in the wfree_list
 		K_WLOCK(minioninfo->wfree_list);
-		while (stale_unused_work) {
-			prev_unused = stale_unused_work->prev;
-			k_free_head(minioninfo->wfree_list, stale_unused_work);
-			stale_unused_work = prev_unused;
-		}
+		k_list_transfer_to_head(minioninfo->wstale_list, minioninfo->wfree_list);
 		K_WUNLOCK(minioninfo->wfree_list);
 	}
 }
@@ -3347,10 +3464,31 @@ static void new_work_task(struct cgpu_info *minioncgpu, K_ITEM *witem, int chip,
 static K_ITEM *next_work(struct minion_info *minioninfo)
 {
 	K_ITEM *item;
+	struct timeval now;
+	double howlong;
+	int i;
 
 	K_WLOCK(minioninfo->wwork_list);
 	item = k_unlink_tail(minioninfo->wwork_list);
 	K_WUNLOCK(minioninfo->wwork_list);
+	if (item) {
+		cgtime(&now);
+		howlong = tdiff(&now, &(DATAW(item)->created));
+		minioninfo->que_work++;
+		minioninfo->que_time += howlong;
+		if (minioninfo->que_min == 0 || minioninfo->que_min > howlong)
+			minioninfo->que_min = howlong;
+		else if (minioninfo->que_max < howlong)
+			minioninfo->que_max = howlong;
+		for (i = 0; i < TIME_BANDS; i++) {
+			if (howlong < time_bands[i]) {
+				minioninfo->que_bands[i]++;
+				break;
+			}
+		}
+		if (i >= TIME_BANDS)
+			minioninfo->que_bands[TIME_BANDS]++;
+	}
 
 	return item;
 }
@@ -3432,7 +3570,7 @@ static void minion_do_work(struct cgpu_info *minioncgpu)
 								K_WLOCK(minioninfo->wchip_list[chip]);
 								minioninfo->chip_status[chip].realwork = cmd;
 								K_WUNLOCK(minioninfo->wchip_list[chip]);
-								if (cmd < 10 || cmd >= MINION_QUE_HIGH) {
+								if (cmd <= MINION_QUE_LOW || cmd >= MINION_QUE_HIGH) {
 									applog(LOG_DEBUG, "%s%i: Chip %d fifo cmd %d",
 											  minioncgpu->drv->name,
 											  minioncgpu->device_id,
@@ -3623,7 +3761,7 @@ static bool minion_queue_full(struct cgpu_info *minioncgpu)
 {
 	struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
 	struct work *work, *usework;
-	int count, need, roll, roll_limit;
+	int count, totneed, need, roll, roll_limit, chip;
 	bool ret, rolled;
 
 	if (minioninfo->initialised == false) {
@@ -3633,12 +3771,27 @@ static bool minion_queue_full(struct cgpu_info *minioncgpu)
 
 	K_RLOCK(minioninfo->wwork_list);
 	count = minioninfo->wwork_list->count;
+	totneed = 0;
+	for (chip = 0; chip < (int)MINION_CHIPS; chip++) {
+		if (minioninfo->has_chip[chip] &&
+		    !minioninfo->chip_status[chip].overheat) {
+			totneed += MINION_QUE_HIGH;
+			totneed -= minioninfo->chip_status[chip].quework;
+			totneed -= minioninfo->chip_status[chip].realwork;
+			// One for the pot :)
+			totneed++;
+		}
+	}
 	K_RUNLOCK(minioninfo->wwork_list);
 
-	if (count >= (MINION_QUE_HIGH * minioninfo->chips))
+	if (count >= totneed)
 		ret = true;
 	else {
-		need = (MINION_QUE_HIGH * minioninfo->chips) - count;
+		need = totneed - count;
+		/* Ensure we do enough rolling to reduce CPU
+		   but dont roll too much to have them end up stale */
+		if (need < 16)
+			need = 16;
 		work = get_queued(minioncgpu);
 		if (work) {
 			roll_limit = work->drv_rolllimit;
@@ -3787,11 +3940,25 @@ static void chip_report(struct cgpu_info *minioncgpu)
 					minioninfo->do_reset[chip] = 0.0;
 					memcpy(&(minioninfo->last_reset[chip]), &now, sizeof(now));
 					init_chip(minioncgpu, minioninfo, chip);
+					minioninfo->flag_reset[chip] = false;
 				}
 			}
 		}
 		memcpy(&(minioninfo->chip_chk), &now, sizeof(now));
 	}
+
+	for (chip = 0; chip < (int)MINION_CHIPS; chip++) {
+		if (minioninfo->has_chip[chip]) {
+			if (minioninfo->flag_reset[chip]) {
+				applog(LOG_WARNING, "%s%d: Chip %d flagged - resetting ...",
+						    minioncgpu->drv->name, minioncgpu->device_id,
+						    chip);
+				memcpy(&(minioninfo->last_reset[chip]), &now, sizeof(now));
+				init_chip(minioncgpu, minioninfo, chip);
+				minioninfo->flag_reset[chip] = false;
+			}
+		}
+	}
 }
 
 static int64_t minion_scanwork(__maybe_unused struct thr_info *thr)
@@ -4085,17 +4252,21 @@ static struct api_data *minion_api_stats(struct cgpu_info *minioncgpu)
 	}
 
 	que_work = chip_work = 0;
-	for (chip = 0; chip <= max_chip; chip++)
+	for (chip = 0; chip <= max_chip; chip++) {
 		if (minioninfo->has_chip[chip]) {
-			que_work += minioninfo->wchip_list[chip]->count;
+			que_work += minioninfo->wque_list[chip]->count;
 			chip_work += minioninfo->wchip_list[chip]->count;
 		}
+	}
 
 	root = api_add_int(root, "WFree Total", &(minioninfo->wfree_list->total), true);
 	root = api_add_int(root, "WFree Count", &(minioninfo->wfree_list->count), true);
 	root = api_add_int(root, "WWork Count", &(minioninfo->wwork_list->count), true);
+	root = api_add_uint64(root, "WWork Flushed", &(minioninfo->wwork_flushed), true);
 	root = api_add_int(root, "WQue Count", &que_work, true);
+	root = api_add_uint64(root, "WQue Flushed", &(minioninfo->wque_flushed), true);
 	root = api_add_int(root, "WChip Count", &chip_work, true);
+	root = api_add_uint64(root, "WChip Stale", &(minioninfo->wchip_staled), true);
 
 	root = api_add_int(root, "TFree Total", &(minioninfo->tfree_list->total), true);
 	root = api_add_int(root, "TFree Count", &(minioninfo->tfree_list->count), true);
@@ -4161,6 +4332,43 @@ static struct api_data *minion_api_stats(struct cgpu_info *minioncgpu)
 	root = api_add_string(root, "Last Int", minioninfo->last_interrupt, true);
 	root = api_add_hex32(root, "Next TaskID", &(minioninfo->next_task_id), true);
 
+	double avg;
+	root = api_add_uint64(root, "ToQue", &(minioninfo->que_work), true);
+	if (minioninfo->que_work)
+		avg = minioninfo->que_time / (double)(minioninfo->que_work);
+	else
+		avg = 0;
+	root = api_add_double(root, "Que Avg", &avg, true);
+	root = api_add_double(root, "Que Min", &(minioninfo->que_min), true);
+	root = api_add_double(root, "Que Max", &(minioninfo->que_max), true);
+	data[0] = '\0';
+	for (i = 0; i <= TIME_BANDS; i++) {
+		snprintf(buf, sizeof(buf),
+				"%s%"PRIu64,
+				i == 0 ? "" : "/",
+				minioninfo->que_bands[i]);
+		strcat(data, buf);
+	}
+	root = api_add_string(root, "Que Bands", data, true);
+
+	root = api_add_uint64(root, "ToTxRx", &(minioninfo->wt_work), true);
+	if (minioninfo->wt_work)
+		avg = minioninfo->wt_time / (double)(minioninfo->wt_work);
+	else
+		avg = 0;
+	root = api_add_double(root, "TxRx Avg", &avg, true);
+	root = api_add_double(root, "TxRx Min", &(minioninfo->wt_min), true);
+	root = api_add_double(root, "TxRx Max", &(minioninfo->wt_max), true);
+	data[0] = '\0';
+	for (i = 0; i <= TIME_BANDS; i++) {
+		snprintf(buf, sizeof(buf),
+				"%s%"PRIu64,
+				i == 0 ? "" : "/",
+				minioninfo->wt_bands[i]);
+		strcat(data, buf);
+	}
+	root = api_add_string(root, "TxRx Bands", data, true);
+
 	uint64_t checked, dups;
 	dupcounters(minioncgpu, &checked, &dups);
 	root = api_add_uint64(root, "Dups", &dups, true);
@@ -4177,6 +4385,7 @@ struct device_drv minion_drv = {
 #ifdef LINUX
 	.get_api_stats = minion_api_stats,
 	.get_statline_before = minion_get_statline_before,
+	.set_device = minion_set,
 	.identify_device = minion_identify,
 	.thread_prepare = minion_thread_prepare,
 	.hash_work = hash_queued_work,
diff --git a/klist.c b/klist.c
index 8a3098d..a552017 100644
--- a/klist.c
+++ b/klist.c
@@ -255,6 +255,56 @@ void _k_add_tail(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
 	list->count_up++;
 }
 
+void _k_insert_before(K_LIST *list, K_ITEM *item, K_ITEM *before, 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 (!before) {
+		quithere(1, "%s() (%s) can't before a null item" KLIST_FFL,
+				__func__, list->name, KLIST_FFL_PASS);
+	}
+
+	item->next = before;
+	item->prev = before->prev;
+	if (before->prev)
+		before->prev->next = item;
+	else
+		list->head = item;
+	before->prev = item;
+
+	list->count++;
+	list->count_up++;
+}
+
+void _k_insert_after(K_LIST *list, K_ITEM *item, K_ITEM *after, 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 (!after) {
+		quithere(1, "%s() (%s) can't after a null item" KLIST_FFL,
+				__func__, list->name, KLIST_FFL_PASS);
+	}
+
+	item->prev = after;
+	item->next = after->next;
+	if (after->next)
+		after->next->prev = item;
+	else {
+		if (list->do_tail)
+			list->tail = item;
+	}
+	after->next = item;
+
+	list->count++;
+	list->count_up++;
+}
+
 void _k_unlink_item(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
 {
 	if (item->name != list->name) {
diff --git a/klist.h b/klist.h
index dfcc41d..a79a4ed 100644
--- a/klist.h
+++ b/klist.h
@@ -76,6 +76,10 @@ extern void _k_add_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_insert_before(K_LIST *list, K_ITEM *item, K_ITEM *before, KLIST_FFL_ARGS);
+#define k_insert_before(_list, _item, _before) _k_insert_before(_list, _item, _before, KLIST_FFL_HERE)
+extern void _k_insert_after(K_LIST *list, K_ITEM *item, K_ITEM *after, KLIST_FFL_ARGS);
+#define k_insert_after(_list, _item, _after) _k_insert_after(_list, _item, _after, KLIST_FFL_HERE)
 extern void _k_unlink_item(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
 #define k_unlink_item(_list, _item) _k_unlink_item(_list, _item, KLIST_FFL_HERE)
 void _k_list_transfer_to_head(K_LIST *from, K_LIST *to, KLIST_FFL_ARGS);