Commit e06e495ab81304a5eb31d08f8b22bd671a5db774

Con Kolivas 2013-10-14T15:07:32

Merge branch 'master' into hashfast

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
diff --git a/api.c b/api.c
index b573767..44e6c3f 100644
--- a/api.c
+++ b/api.c
@@ -630,7 +630,7 @@ struct CODES {
  { SEVERITY_SUCC,  MSG_ASCIDENT,PARAM_ASC,	"Identify command sent to ASC%d" },
  { SEVERITY_WARN,  MSG_ASCNOID,	PARAM_ASC,	"ASC%d does not support identify" },
  { SEVERITY_ERR,   MSG_MISASCOPT, PARAM_NONE,	"Missing option after ASC number" },
- { SEVERITY_WARN,  MSG_ASCNOSET, PARAM_ASC,	"ASC %d does not support pgaset" },
+ { SEVERITY_WARN,  MSG_ASCNOSET, PARAM_ASC,	"ASC %d does not support ascset" },
  { SEVERITY_INFO,  MSG_ASCHELP, PARAM_BOTH,	"ASC %d set help: %s" },
  { SEVERITY_SUCC,  MSG_ASCSETOK, PARAM_BOTH,	"ASC %d set OK" },
  { SEVERITY_ERR,   MSG_ASCSETERR, PARAM_BOTH,	"ASC %d set failed: %s" },
diff --git a/cgminer.c b/cgminer.c
index 89baa2f..cadd9d5 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -4011,7 +4011,7 @@ static void set_blockdiff(const struct work *work)
 static bool test_work_current(struct work *work)
 {
 	bool ret = true;
-	char hexstr[20];
+	char hexstr[40];
 
 	if (work->mandatory)
 		return ret;
diff --git a/driver-klondike.c b/driver-klondike.c
index 7dafc1d..6983465 100644
--- a/driver-klondike.c
+++ b/driver-klondike.c
@@ -38,8 +38,7 @@
 #define MERKLE_BYTES 12
 
 #define REPLY_SIZE		15	// adequate for all types of replies
-#define REPLY_BUFSIZE 		16	// reply + 1 byte to mark used
-#define MAX_REPLY_COUNT		4096	// more unhandled replies than this will result in data loss
+#define MAX_KLINES		1024	// unhandled reply limit
 #define REPLY_WAIT_TIME		100 	// poll interval for a cmd waiting it's reply
 #define CMD_REPLY_RETRIES	8	// how many retries for cmds
 #define MAX_WORK_COUNT		4	// for now, must be binary multiple and match firmware
@@ -47,13 +46,44 @@
 
 struct device_drv klondike_drv;
 
+typedef struct klondike_header {
+	uint8_t cmd;
+	uint8_t dev;
+	uint8_t buf[REPLY_SIZE-2];
+} HEADER;
+
+#define K_2(_bytes) ((int)(_bytes[0]) + \
+			((int)(_bytes[1]) << 8))
+
+#define K_4(_bytes) ((uint64_t)(_bytes[0]) + \
+			((uint64_t)(_bytes[1]) << 8) + \
+			((uint64_t)(_bytes[2]) << 16) + \
+			((uint64_t)(_bytes[3]) << 24))
+
+#define K_SERIAL(_serial) K_4(_serial)
+#define K_HASHCOUNT(_hashcount) K_2(_hashcount)
+#define K_MAXCOUNT(_maxcount) K_2(_maxcount)
+#define K_NONCE(_nonce) K_4(_nonce)
+#define K_HASHCLOCK(_hashclock) K_2(_hashclock)
+
+#define SET_HASHCLOCK(_hashclock, _value) do { \
+						(_hashclock)[0] = (uint8_t)((_value) & 0xff); \
+						(_hashclock)[1] = (uint8_t)(((_value) >> 8) & 0xff); \
+					  } while(0)
+
+#define KSENDHD(_add) (sizeof(char) + sizeof(uint8_t) + _add)
+
 typedef struct klondike_id {
+	uint8_t cmd;
+	uint8_t dev;
 	uint8_t version;
 	uint8_t product[7];
-	uint32_t serial;
+	uint8_t serial[4];
 } IDENTITY;
 
 typedef struct klondike_status {
+	uint8_t cmd;
+	uint8_t dev;
 	uint8_t state;
 	uint8_t chipcount;
 	uint8_t slavecount;
@@ -62,34 +92,47 @@ typedef struct klondike_status {
 	uint8_t temp;
 	uint8_t fanspeed;
 	uint8_t errorcount;
-	uint16_t hashcount;
-	uint16_t maxcount;
+	uint8_t hashcount[2];
+	uint8_t maxcount[2];
 	uint8_t noise;
 } WORKSTATUS;
 
 typedef struct _worktask {
-	uint16_t pad1;
-	uint8_t pad2;
+	uint8_t cmd;
+	uint8_t dev;
 	uint8_t workid;
-	uint32_t midstate[8];
-	uint32_t merkle[3];
+	uint8_t midstate[32];
+	uint8_t merkle[12];
 } WORKTASK;
 
 typedef struct _workresult {
-	uint16_t pad;
-	uint8_t device;
+	uint8_t cmd;
+	uint8_t dev;
 	uint8_t workid;
-	uint32_t nonce;
+	uint8_t nonce[4];
 } WORKRESULT;
 
 typedef struct klondike_cfg {
-	uint16_t hashclock;
+	uint8_t cmd;
+	uint8_t dev;
+	uint8_t hashclock[2];
 	uint8_t temptarget;
 	uint8_t tempcritical;
 	uint8_t fantarget;
-	uint8_t pad;
+	uint8_t pad2;
 } WORKCFG;
 
+typedef struct kline {
+	union {
+		HEADER hd;
+		IDENTITY id;
+		WORKSTATUS ws;
+		WORKTASK wt;
+		WORKRESULT wr;
+		WORKCFG cfg;
+	};
+} KLINE;
+
 typedef struct device_info {
 	uint32_t noncecount;
 	uint32_t nextworkid;
@@ -99,22 +142,144 @@ typedef struct device_info {
 	uint32_t *chipstats;
 } DEVINFO;
 
+typedef struct klist {
+	struct klist *prev;
+	struct klist *next;
+	KLINE kline;
+	struct timeval tv_when;
+	int block_seq;
+	bool ready;
+	bool working;
+} KLIST;
+
 struct klondike_info {
 	bool shutdown;
 	pthread_rwlock_t stat_lock;
 	struct thr_info replies_thr;
-	WORKSTATUS *status;
+	cglock_t klist_lock;
+	KLIST *used;
+	KLIST *free;
+	int kline_count;
+	int used_count;
+	int block_seq;
+	KLIST *status;
 	DEVINFO *devinfo;
-	WORKCFG *cfg;
-	char *replies;
-	int nextreply;
+	KLIST *cfg;
 	int noncecount;
 	uint64_t hashcount;
 	uint64_t errorcount;
 	uint64_t noisecount;
+
+	// us Delay from USB reply to being processed
+	double delay_count;
+	double delay_total;
+	double delay_min;
+	double delay_max;
+
+	struct timeval tv_last_nonce_received;
+
+	// Time from recieving one nonce to the next
+	double nonce_count;
+	double nonce_total;
+	double nonce_min;
+	double nonce_max;
 };
 
-IDENTITY KlondikeID;
+static KLIST *new_klist_set(struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	KLIST *klist = NULL;
+	int i;
+
+	klist = calloc(MAX_KLINES, sizeof(*klist));
+	if (!klist)
+		quit(1, "Failed to calloc klist - when old count=%d", klninfo->kline_count);
+
+	klninfo->kline_count += MAX_KLINES;
+
+	klist[0].prev = NULL;
+	klist[0].next = &(klist[1]);
+	for (i = 1; i < MAX_KLINES-1; i++) {
+		klist[i].prev = &klist[i-1];
+		klist[i].next = &klist[i+1];
+	}
+	klist[MAX_KLINES-1].prev = &(klist[MAX_KLINES-2]);
+	klist[MAX_KLINES-1].next = NULL;
+
+	return klist;
+}
+
+static KLIST *allocate_kitem(struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	KLIST *kitem = NULL;
+	int ran_out = 0;
+	char errbuf[1024];
+
+	cg_wlock(&klninfo->klist_lock);
+
+	if (klninfo->free == NULL) {
+		ran_out = klninfo->kline_count;
+		klninfo->free = new_klist_set(klncgpu);
+		snprintf(errbuf, sizeof(errbuf),
+				 "%s%i: KLINE count exceeded %d, now %d",
+				 klncgpu->drv->name, klncgpu->device_id,
+				 ran_out, klninfo->kline_count);
+	}
+
+	kitem = klninfo->free;
+
+	klninfo->free = klninfo->free->next;
+	if (klninfo->free)
+		klninfo->free->prev = NULL;
+
+	kitem->next = klninfo->used;
+	kitem->prev = NULL;
+	if (kitem->next)
+		kitem->next->prev = kitem;
+	klninfo->used = kitem;
+
+	kitem->ready = false;
+	kitem->working = false;
+
+	memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
+
+	klninfo->used_count++;
+
+	cg_wunlock(&klninfo->klist_lock);
+
+	if (ran_out > 0)
+		applog(LOG_ERR, "%s", errbuf);
+
+	return kitem;
+}
+
+static void release_kitem(struct cgpu_info *klncgpu, KLIST *kitem)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+
+	cg_wlock(&klninfo->klist_lock);
+
+	if (kitem == klninfo->used)
+		klninfo->used = kitem->next;
+
+	if (kitem->next)
+		kitem->next->prev = kitem->prev;
+	if (kitem->prev)
+		kitem->prev->next = kitem->next;
+
+	kitem->next = klninfo->free;
+	if (klninfo->free)
+		klninfo->free->prev = kitem;
+
+	kitem->prev = NULL;
+
+	klninfo->free = kitem;
+
+	klninfo->used_count--;
+
+	cg_wunlock(&klninfo->klist_lock);
+}
 
 static double cvtKlnToC(uint8_t temp)
 {
@@ -166,36 +331,114 @@ static int cvtCToKln(double deg)
 	return (int)temp;
 }
 
-static char *SendCmdGetReply(struct cgpu_info *klncgpu, char Cmd, int device, int datalen, void *data)
+// Change this to LOG_WARNING if you wish to always see the replies
+#define READ_DEBUG LOG_DEBUG
+//#define READ_DEBUG LOG_ERR
+
+static void display_kline(struct cgpu_info *klncgpu, KLINE *kline)
+{
+	char *hexdata;
+
+	switch (kline->hd.cmd) {
+		case '=':
+			applog(READ_DEBUG,
+				"%s (%s) work [%c] dev=%d workid=%d"
+				" nonce=0x%08x",
+				klncgpu->drv->dname, klncgpu->device_path,
+				kline->wr.cmd,
+				(int)(kline->wr.dev),
+				(int)(kline->wr.workid),
+				(unsigned int)K_NONCE(kline->wr.nonce));
+			break;
+		case 'S':
+		case 'W':
+		case 'A':
+		case 'E':
+			applog(READ_DEBUG,
+				"%s (%s) status [%c] dev=%d chips=%d"
+				" slaves=%d workcq=%d workid=%d temp=%d fan=%d"
+				" errors=%d hashes=%d max=%d noise=%d",
+				klncgpu->drv->dname, klncgpu->device_path,
+				kline->ws.cmd,
+				(int)(kline->ws.dev),
+				(int)(kline->ws.chipcount),
+				(int)(kline->ws.slavecount),
+				(int)(kline->ws.workqc),
+				(int)(kline->ws.workid),
+				(int)(kline->ws.temp),
+				(int)(kline->ws.fanspeed),
+				(int)(kline->ws.errorcount),
+				K_HASHCOUNT(kline->ws.hashcount),
+				K_MAXCOUNT(kline->ws.maxcount),
+				(int)(kline->ws.noise));
+			break;
+		case 'C':
+			applog(READ_DEBUG,
+				"%s (%s) config [%c] dev=%d clock=%d"
+				" temptarget=%d tempcrit=%d fan=%d",
+				klncgpu->drv->dname, klncgpu->device_path,
+				kline->cfg.cmd,
+				(int)(kline->cfg.dev),
+				K_HASHCLOCK(kline->cfg.hashclock),
+				(int)(kline->cfg.temptarget),
+				(int)(kline->cfg.tempcritical),
+				(int)(kline->cfg.fantarget));
+			break;
+		case 'I':
+			applog(READ_DEBUG,
+				"%s (%s) info [%c] version=0x%02x prod=%.7s"
+				" serial=0x%08x",
+				klncgpu->drv->dname, klncgpu->device_path,
+				kline->hd.cmd,
+				(int)(kline->id.version),
+				kline->id.product,
+				(unsigned int)K_SERIAL(kline->id.serial));
+			break;
+		default:
+			hexdata = bin2hex((unsigned char *)&(kline->hd.dev), REPLY_SIZE - 1);
+			applog(LOG_ERR,
+				"%s (%s) [%c:%s] unknown and ignored",
+				klncgpu->drv->dname, klncgpu->device_path,
+				kline->hd.cmd, hexdata);
+			free(hexdata);
+			break;
+	}
+}
+
+static KLIST *SendCmdGetReply(struct cgpu_info *klncgpu, KLINE *kline, int datalen)
 {
 	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
-	char outbuf[64];
+	KLIST *kitem;
 	int retries = CMD_REPLY_RETRIES;
-	int chkreply = klninfo->nextreply;
-	int sent, err;
+	int err, amt, writ;
 
 	if (klncgpu->usbinfo.nodev)
 		return NULL;
 
-	outbuf[0] = Cmd;
-	outbuf[1] = device;
-	memcpy(outbuf+2, data, datalen);
-	err = usb_write(klncgpu, outbuf, 2+datalen, &sent, C_REQUESTRESULTS);
-	if (err < 0 || sent != 2+datalen) {
-		applog(LOG_ERR, "%s (%s) Cmd:%c Dev:%d, write failed (%d:%d)", klncgpu->drv->dname, klncgpu->device_path, Cmd, device, sent, err);
+	writ = KSENDHD(datalen);
+	err = usb_write(klncgpu, (char *)kline, writ, &amt, C_REQUESTRESULTS);
+	if (err < 0 || amt != writ) {
+		applog(LOG_ERR, "%s (%s) Cmd:%c Dev:%d, write failed (%d:%d:%d)",
+				klncgpu->drv->dname, klncgpu->device_path,
+				kline->hd.cmd, (int)kline->hd.dev,
+				writ, amt, err);
 	}
+
 	while (retries-- > 0 && klninfo->shutdown == false) {
 		cgsleep_ms(REPLY_WAIT_TIME);
-		while (*(klninfo->replies + chkreply*REPLY_BUFSIZE) != Cmd || *(klninfo->replies + chkreply*REPLY_BUFSIZE + 2) != device) {
-			if (++chkreply == MAX_REPLY_COUNT)
-				chkreply = 0;
-			if (chkreply == klninfo->nextreply)
-				break;
+		cg_rlock(&klninfo->klist_lock);
+		kitem = klninfo->used;
+		while (kitem) {
+			if (kitem->kline.hd.cmd == kline->hd.cmd &&
+			    kitem->kline.hd.dev == kline->hd.dev &&
+			    kitem->ready == true && kitem->working == false) {
+				kitem->working = true;
+				cg_runlock(&klninfo->klist_lock);
+				return kitem;
+			}
+			kitem = kitem->next;
 		}
-		if (chkreply == klninfo->nextreply)
-			continue;
-		*(klninfo->replies + chkreply*REPLY_BUFSIZE) = '!';  // mark to prevent re-use
-		return klninfo->replies + chkreply*REPLY_BUFSIZE + 1;
+		cg_runlock(&klninfo->klist_lock);
 	}
 	return NULL;
 }
@@ -203,22 +446,30 @@ static char *SendCmdGetReply(struct cgpu_info *klncgpu, char Cmd, int device, in
 static bool klondike_get_stats(struct cgpu_info *klncgpu)
 {
 	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	KLIST *kitem;
+	KLINE kline;
 	int slaves, dev;
 
 	if (klncgpu->usbinfo.nodev || klninfo->status == NULL)
 		return false;
 
 	applog(LOG_DEBUG, "Klondike getting status");
-	slaves = klninfo->status[0].slavecount;
+
+	slaves = klninfo->status[0].kline.ws.slavecount;
 
 	// loop thru devices and get status for each
-	wr_lock(&(klninfo->stat_lock));
 	for (dev = 0; dev <= slaves; dev++) {
-		char *reply = SendCmdGetReply(klncgpu, 'S', dev, 0, NULL);
-		if (reply != NULL)
-			memcpy((void *)(&(klninfo->status[dev])), reply+2, sizeof(klninfo->status[dev]));
+		kline.hd.cmd = 'S';
+		kline.hd.dev = dev;
+		kitem = SendCmdGetReply(klncgpu, &kline, 0);
+		if (kitem != NULL) {
+			wr_lock(&(klninfo->stat_lock));
+			memcpy((void *)(&(klninfo->status[dev])), (void *)kitem, sizeof(*kitem));
+			wr_unlock(&(klninfo->stat_lock));
+			release_kitem(klncgpu, kitem);
+			kitem = NULL;
+		}
 	}
-	wr_unlock(&(klninfo->stat_lock));
 
 	// todo: detect slavecount change and realloc space
 
@@ -228,60 +479,97 @@ static bool klondike_get_stats(struct cgpu_info *klncgpu)
 static bool klondike_init(struct cgpu_info *klncgpu)
 {
 	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	KLIST *kitem;
+	KLINE kline;
 	int slaves, dev;
 
-	char *reply = SendCmdGetReply(klncgpu, 'S', 0, 0, NULL);
-	if (reply == NULL)
+	kline.hd.cmd = 'S';
+	kline.hd.dev = 0;
+	kitem = SendCmdGetReply(klncgpu, &kline, 0);
+	if (kitem == NULL)
 		return false;
 
-	slaves = ((WORKSTATUS *)(reply+2))->slavecount;
+	slaves = kitem->kline.ws.slavecount;
+	release_kitem(klncgpu, kitem);
+	kitem = NULL;
 	if (klninfo->status == NULL) {
 		applog(LOG_DEBUG, "Klondike initializing data");
 
 		// alloc space for status, devinfo and cfg for master and slaves
-		klninfo->status = calloc(slaves+1, sizeof(WORKSTATUS));
+		klninfo->status = calloc(slaves+1, sizeof(KLIST));
 		if (unlikely(!klninfo->status))
 			quit(1, "Failed to calloc status array in klondke_get_stats");
 		klninfo->devinfo = calloc(slaves+1, sizeof(DEVINFO));
 		if (unlikely(!klninfo->devinfo))
 			quit(1, "Failed to calloc devinfo array in klondke_get_stats");
-		klninfo->cfg = calloc(slaves+1, sizeof(WORKCFG));
+		klninfo->cfg = calloc(slaves+1, sizeof(KLIST));
 		if (unlikely(!klninfo->cfg))
 			quit(1, "Failed to calloc cfg array in klondke_get_stats");
 	}
 
-	WORKCFG cfgset = { 0,0,0,0,0 }; // zero init triggers read back only
-	double temp1, temp2;
+	// zero init triggers read back only
+	memset(&(kline.cfg), 0, sizeof(kline.cfg));
+	kline.cfg.cmd = 'C';
+
 	int size = 2;
 
-	if (opt_klondike_options != NULL) {  // boundaries are checked by device, with valid values returned
-		sscanf(opt_klondike_options, "%hu:%lf:%lf:%hhu", &cfgset.hashclock, &temp1, &temp2, &cfgset.fantarget);
-		cfgset.temptarget = cvtCToKln(temp1);
-		cfgset.tempcritical = cvtCToKln(temp2);
-		cfgset.fantarget = (int)255*cfgset.fantarget/100;
-		size = sizeof(cfgset);
+	// boundaries are checked by device, with valid values returned
+	if (opt_klondike_options != NULL) {
+		int hashclock;
+		double temp1, temp2;
+
+		sscanf(opt_klondike_options, "%d:%lf:%lf:%"SCNu8,
+						&hashclock,
+						&temp1, &temp2,
+						&kline.cfg.fantarget);
+		SET_HASHCLOCK(kline.cfg.hashclock, hashclock);
+		kline.cfg.temptarget = cvtCToKln(temp1);
+		kline.cfg.tempcritical = cvtCToKln(temp2);
+		kline.cfg.fantarget = (int)255*kline.cfg.fantarget/100;
+		size = sizeof(kline.cfg) - 2;
 	}
 
 	for (dev = 0; dev <= slaves; dev++) {
-		char *reply = SendCmdGetReply(klncgpu, 'C', dev, size, &cfgset);
-		if (reply != NULL) {
-			klninfo->cfg[dev] = *(WORKCFG *)(reply+2);
-			applog(LOG_NOTICE, "Klondike config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
-				dev, klninfo->cfg[dev].hashclock,
-				cvtKlnToC(klninfo->cfg[dev].temptarget),
-				cvtKlnToC(klninfo->cfg[dev].tempcritical),
-				(int)100*klninfo->cfg[dev].fantarget/256);
+		kline.cfg.dev = dev;
+		kitem = SendCmdGetReply(klncgpu, &kline, size);
+		if (kitem != NULL) {
+			memcpy((void *)&(klninfo->cfg[dev]), kitem, sizeof(*kitem));
+			applog(LOG_WARNING, "Klondike config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
+				dev, K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock),
+				cvtKlnToC(klninfo->cfg[dev].kline.cfg.temptarget),
+				cvtKlnToC(klninfo->cfg[dev].kline.cfg.tempcritical),
+				(int)100*klninfo->cfg[dev].kline.cfg.fantarget/256);
+			release_kitem(klncgpu, kitem);
+			kitem = NULL;
 		}
 	}
 	klondike_get_stats(klncgpu);
 	for (dev = 0; dev <= slaves; dev++) {
-		klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].chipcount;
-		klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].chipcount*2 , sizeof(uint32_t));
+		klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].kline.ws.chipcount;
+		klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].kline.ws.chipcount*2 , sizeof(uint32_t));
+	}
+
+	int tries = 2;
+	bool ok = false;
+
+	kline.hd.cmd = 'E';
+	kline.hd.dev = 0;
+	kline.hd.buf[0] = '1';
+	
+	while (tries-- > 0) {
+		kitem = SendCmdGetReply(klncgpu, &kline, 1);
+		if (kitem) {
+			release_kitem(klncgpu, kitem);
+			kitem = NULL;
+			ok = true;
+			break;
+		}
 	}
 
-	SendCmdGetReply(klncgpu, 'E', 0, 1, "1");
+	if (!ok)
+		applog(LOG_ERR, "%s%i: failed to enable", klncgpu->drv->name, klncgpu->device_id);
 
-	return true;
+	return ok;
 }
 
 static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
@@ -295,45 +583,52 @@ static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devic
 	klninfo = calloc(1, sizeof(*klninfo));
 	if (unlikely(!klninfo))
 		quit(1, "Failed to calloc klninfo in klondke_detect_one");
-	klncgpu->device_data = (FILE *)klninfo;
+	klncgpu->device_data = (void *)klninfo;
 
-	klninfo->replies = calloc(MAX_REPLY_COUNT, REPLY_BUFSIZE);
-	if (unlikely(!klninfo->replies))
-		quit(1, "Failed to calloc replies buffer in klondke_detect_one");
-	klninfo->nextreply = 0;
+	klninfo->free = new_klist_set(klncgpu);
 
 	if (usb_init(klncgpu, dev, found)) {
+		int sent, recd, err;
+		KLIST kitem;
 		int attempts = 0;
-		while (attempts++ < 3) {
-			char devpath[20], reply[REPLY_SIZE];
-			int sent, recd, err;
 
-			sprintf(devpath, "%d:%d", (int)(klncgpu->usbinfo.bus_number), (int)(klncgpu->usbinfo.device_address));
+		while (attempts++ < 3) {
 			err = usb_write(klncgpu, "I", 2, &sent, C_REQUESTRESULTS);
 			if (err < 0 || sent != 2) {
-				applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)", klncgpu->drv->dname, devpath, sent, err);
+				applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)",
+						klncgpu->drv->dname,
+						klncgpu->device_path,
+						sent, err);
 			}
 			cgsleep_ms(REPLY_WAIT_TIME*10);
-			err = usb_read(klncgpu, reply, REPLY_SIZE, &recd, C_GETRESULTS);
+			err = usb_read(klncgpu, (char *)&(kitem.kline), REPLY_SIZE, &recd, C_GETRESULTS);
 			if (err < 0) {
-				applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)", klncgpu->drv->dname, devpath, recd, err);
+				applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)",
+						klncgpu->drv->dname,
+						klncgpu->device_path,
+						recd, err);
 			} else if (recd < 1) {
-				applog(LOG_ERR, "%s (%s) detect empty reply (%d)",	klncgpu->drv->dname, devpath, recd);
-			} else if (reply[0] == 'I' && reply[1] == 0) {
-
-				applog(LOG_DEBUG, "%s (%s) detect successful", klncgpu->drv->dname, devpath);
-				KlondikeID = *(IDENTITY *)(&reply[2]);
-				klncgpu->device_path = strdup(devpath);
-				update_usb_stats(klncgpu);
+				applog(LOG_ERR, "%s (%s) detect empty reply (%d)",
+						klncgpu->drv->dname,
+						klncgpu->device_path,
+						recd);
+			} else if (kitem.kline.hd.cmd == 'I' && kitem.kline.hd.dev == 0) {
+display_kline(klncgpu, &kitem.kline);
+				applog(LOG_DEBUG, "%s (%s) detect successful",
+						  klncgpu->drv->dname,
+						  klncgpu->device_path);
 				if (!add_cgpu(klncgpu))
 					break;
+				update_usb_stats(klncgpu);
 				applog(LOG_DEBUG, "Klondike cgpu added");
+				cglock_init(&klninfo->klist_lock);
 				return true;
 			}
 		}
 		usb_uninit(klncgpu);
 	}
-	free(klninfo->replies);
+	free(klninfo->free);
+	free(klninfo);
 	free(klncgpu);
 	return false;
 }
@@ -345,137 +640,150 @@ static void klondike_detect(bool __maybe_unused hotplug)
 
 static void klondike_identify(__maybe_unused struct cgpu_info *klncgpu)
 {
-	//SendCmdGetReply(klncgpu, 'I', 0, 0, NULL);
+/*
+	KLINE kline;
+
+	kline.hd.cmd = 'I';
+	kline.hd.dev = 0;
+	SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
+*/
 }
 
-static void klondike_check_nonce(struct cgpu_info *klncgpu, WORKRESULT *result)
+static void klondike_check_nonce(struct cgpu_info *klncgpu, KLIST *kitem)
 {
 	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
 	struct work *work, *tmp;
+	KLINE *kline = &(kitem->kline);
+	struct timeval tv_now;
+	double us_diff;
+	uint32_t nonce = K_NONCE(kline->wr.nonce) - 0xC0;
 
-	applog(LOG_DEBUG, "Klondike FOUND NONCE (%02x:%08x)", result->workid, result->nonce);
+	applog(LOG_DEBUG, "Klondike FOUND NONCE (%02x:%08x)",
+			  kline->wr.workid, (unsigned int)nonce);
 
 	HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
-		if (work->queued && (work->subid == (result->device*256 + result->workid))) {
+		if (work->queued && (work->subid == (kline->wr.dev*256 + kline->wr.workid))) {
 
 			wr_lock(&(klninfo->stat_lock));
-			klninfo->devinfo[result->device].noncecount++;
+			klninfo->devinfo[kline->wr.dev].noncecount++;
 			klninfo->noncecount++;
 			wr_unlock(&(klninfo->stat_lock));
 
-			result->nonce = le32toh(result->nonce - 0xC0);
-			applog(LOG_DEBUG, "Klondike SUBMIT NONCE (%02x:%08x)", result->workid, result->nonce);
-			bool ok = submit_nonce(klncgpu->thr[0], work, result->nonce);
+//			kline->wr.nonce = le32toh(kline->wr.nonce - 0xC0);
+			applog(LOG_DEBUG, "Klondike SUBMIT NONCE (%02x:%08x)",
+					  kline->wr.workid, (unsigned int)nonce);
+
+			cgtime(&tv_now);
+			bool ok = submit_nonce(klncgpu->thr[0], work, nonce);
+
+			applog(LOG_DEBUG, "Klondike chip stats %d, %08x, %d, %d",
+					  kline->wr.dev, (unsigned int)nonce,
+					  klninfo->devinfo[kline->wr.dev].rangesize,
+					  klninfo->status[kline->wr.dev].kline.ws.chipcount);
+
+			klninfo->devinfo[kline->wr.dev].chipstats[(nonce / klninfo->devinfo[kline->wr.dev].rangesize) + (ok ? 0 : klninfo->status[kline->wr.dev].kline.ws.chipcount)]++;
+
+			us_diff = us_tdiff(&tv_now, &(kitem->tv_when));
+			if (klninfo->delay_count == 0) {
+				klninfo->delay_min = us_diff;
+				klninfo->delay_max = us_diff;
+			} else {
+				if (klninfo->delay_min > us_diff)
+					klninfo->delay_min = us_diff;
+				if (klninfo->delay_max < us_diff)
+					klninfo->delay_max = us_diff;
+			}
+			klninfo->delay_count++;
+			klninfo->delay_total += us_diff;
+
+			us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
+			if (klninfo->nonce_count == 0) {
+				klninfo->nonce_min = us_diff;
+				klninfo->nonce_max = us_diff;
+			} else {
+				if (klninfo->nonce_min > us_diff)
+					klninfo->nonce_min = us_diff;
+				if (klninfo->nonce_max < us_diff)
+					klninfo->nonce_max = us_diff;
+			}
+			klninfo->nonce_count++;
+			klninfo->nonce_total += us_diff;
+
+			memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
+				sizeof(klninfo->tv_last_nonce_received));
 
-			applog(LOG_DEBUG, "Klondike chip stats %d, %08x, %d, %d", result->device, result->nonce, klninfo->devinfo[result->device].rangesize, klninfo->status[result->device].chipcount);
-			klninfo->devinfo[result->device].chipstats[(result->nonce / klninfo->devinfo[result->device].rangesize) + (ok ? 0 : klninfo->status[result->device].chipcount)]++;
 			return;
 		}
 	}
 
 	applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
-		klncgpu->drv->name, klncgpu->device_id, result->device, result->workid, result->nonce);
+			klncgpu->drv->name, klncgpu->device_id,
+			kline->wr.dev, kline->wr.workid, (unsigned int)nonce);
+
 	//inc_hw_errors(klncgpu->thr[0]);
 }
 
-// Change this to LOG_WARNING if you wish to always see the replies
-#define READ_DEBUG LOG_DEBUG
-
 // thread to keep looking for replies
 static void *klondike_get_replies(void *userdata)
 {
 	struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
 	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
-	struct klondike_status *ks;
-	struct _workresult *wr;
-	struct klondike_cfg *kc;
-	struct klondike_id *ki;
-	char *replybuf;
+	KLIST *kitem = NULL;
+	char *hexdata;
 	int err, recd;
 
-	applog(LOG_DEBUG, "Klondike listening for replies");
+	applog(LOG_ERR, "Klondike listening for replies");
 
 	while (klninfo->shutdown == false) {
 		if (klncgpu->usbinfo.nodev)
 			return NULL;
 
-		replybuf = klninfo->replies + klninfo->nextreply * REPLY_BUFSIZE;
-		replybuf[0] = 0;
+		if (kitem == NULL)
+			kitem = allocate_kitem(klncgpu);
+		else
+			memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
 
-		err = usb_read(klncgpu, replybuf+1, REPLY_SIZE, &recd, C_GETRESULTS);
+		err = usb_read(klncgpu, (char *)&(kitem->kline), REPLY_SIZE, &recd, C_GETRESULTS);
 		if (!err && recd == REPLY_SIZE) {
+			cgtime(&(kitem->tv_when));
+			kitem->block_seq = klninfo->block_seq;
 			if (opt_log_level <= READ_DEBUG) {
-				char *hexdata = bin2hex((unsigned char *)(replybuf+1), recd);
-				applog(READ_DEBUG, "%s (%s) reply [%s:%s]", klncgpu->drv->dname, klncgpu->device_path, replybuf+1, hexdata);
+				hexdata = bin2hex((unsigned char *)&(kitem->kline.hd.dev), recd-1);
+				applog(READ_DEBUG, "%s (%s) reply [%c:%s]",
+					klncgpu->drv->dname, klncgpu->device_path,
+					kitem->kline.hd.cmd, hexdata);
 				free(hexdata);
 			}
-			if (++klninfo->nextreply == MAX_REPLY_COUNT)
-				klninfo->nextreply = 0;
 
-			replybuf[0] = replybuf[1];
-			switch (replybuf[0]) {
+			switch (kitem->kline.hd.cmd) {
 				case '=':
-					wr = (struct _workresult *)(replybuf+1);
-					klondike_check_nonce(klncgpu, (WORKRESULT *)replybuf);
-					applog(READ_DEBUG,
-						"%s (%s) reply: work [%c] device=%d workid=%d"
-						" nonce=0x%08x",
-						klncgpu->drv->dname, klncgpu->device_path,
-						*(replybuf+1),
-						(int)(wr->device),
-						(int)(wr->workid),
-						(unsigned int)(wr->nonce));
+					klondike_check_nonce(klncgpu, kitem);
+					display_kline(klncgpu, &kitem->kline);
 					break;
 				case 'S':
 				case 'W':
 				case 'A':
 				case 'E':
-					ks = (struct klondike_status *)(replybuf+1);
 					wr_lock(&(klninfo->stat_lock));
-					klninfo->errorcount += ks->errorcount;
-					klninfo->noisecount += ks->noise;
+					klninfo->errorcount += kitem->kline.ws.errorcount;
+					klninfo->noisecount += kitem->kline.ws.noise;
 					wr_unlock(&(klninfo->stat_lock));
-					applog(READ_DEBUG,
-						"%s (%s) reply: status [%c] chips=%d slaves=%d"
-						" workcq=%d workid=%d temp=%d fan=%d errors=%d"
-						" hashes=%d max=%d noise=%d",
-						klncgpu->drv->dname, klncgpu->device_path,
-						*(replybuf+1),
-						(int)(ks->chipcount),
-						(int)(ks->slavecount),
-						(int)(ks->workqc),
-						(int)(ks->workid),
-						(int)(ks->temp),
-						(int)(ks->fanspeed),
-						(int)(ks->errorcount),
-						(int)(ks->hashcount),
-						(int)(ks->maxcount),
-						(int)(ks->noise));
+					display_kline(klncgpu, &kitem->kline);
+					kitem->ready = true;
+					kitem = NULL;
 					break;
 				case 'C':
-					kc = (struct klondike_cfg *)(replybuf+2);
-					applog(READ_DEBUG,
-						"%s (%s) reply: config [%c] clock=%d temptarget=%d"
-						" tempcrit=%d fan=%d",
-						klncgpu->drv->dname, klncgpu->device_path,
-						*(replybuf+1),
-						(int)(kc->hashclock),
-						(int)(kc->temptarget),
-						(int)(kc->tempcritical),
-						(int)(kc->fantarget));
+					display_kline(klncgpu, &kitem->kline);
+					kitem->ready = true;
+					kitem = NULL;
 					break;
 				case 'I':
-					ki = (struct klondike_id *)(replybuf+2);
-					applog(READ_DEBUG,
-						"%s (%s) reply: info [%c] version=0x%02x prod=%.7s"
-						" serial=0x%08x",
-						klncgpu->drv->dname, klncgpu->device_path,
-						*(replybuf+1),
-						(int)(ki->version),
-						ki->product,
-						(unsigned int)(ki->serial));
+					display_kline(klncgpu, &kitem->kline);
+					kitem->ready = true;
+					kitem = NULL;
 					break;
 				default:
+					display_kline(klncgpu, &kitem->kline);
 					break;
 			}
 		}
@@ -486,15 +794,24 @@ static void *klondike_get_replies(void *userdata)
 static void klondike_flush_work(struct cgpu_info *klncgpu)
 {
 	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
-	int dev;
+	KLIST *kitem;
+	KLINE kline;
+	int slaves, dev;
+
+	klninfo->block_seq++;
 
 	applog(LOG_DEBUG, "Klondike flushing work");
-	for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
-		char *reply = SendCmdGetReply(klncgpu, 'A', dev, 0, NULL);
-		if (reply != NULL) {
+	slaves = klninfo->status[0].kline.ws.slavecount;
+	kline.hd.cmd = 'A';
+	for (dev = 0; dev <= slaves; dev++) {
+		kline.hd.dev = dev;
+		kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
+		if (kitem != NULL) {
 			wr_lock(&(klninfo->stat_lock));
-			klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
+			memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(*kitem));
 			wr_unlock(&(klninfo->stat_lock));
+			release_kitem(klncgpu, kitem);
+			kitem = NULL;
 		}
 	}
 }
@@ -532,11 +849,18 @@ static void klondike_shutdown(struct thr_info *thr)
 {
 	struct cgpu_info *klncgpu = thr->cgpu;
 	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	KLIST *kitem;
+	KLINE kline;
 	int dev;
 
 	applog(LOG_DEBUG, "Klondike shutting down work");
-	for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
-		SendCmdGetReply(klncgpu, 'E', dev, 1, "0");
+	kline.hd.cmd = 'E';
+	for (dev = 0; dev <= klninfo->status[0].kline.ws.slavecount; dev++) {
+		kline.hd.dev = dev;
+		kline.hd.buf[0] = '0';
+		kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
+		if (kitem)
+			release_kitem(klncgpu, kitem);
 	}
 	klncgpu->shutdown = klninfo->shutdown = true;
 }
@@ -548,7 +872,14 @@ static void klondike_thread_enable(struct thr_info *thr)
 	if (klncgpu->usbinfo.nodev)
 		return;
 
-	//SendCmdGetReply(klncgpu, 'E', 0, 1, "0");
+/*
+	KLINE kline;
+
+	kline.hd.cmd = 'E';
+	kline.hd.dev = dev;
+	kline.hd.buf[0] = '0';
+	kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
+*/
 
 }
 
@@ -556,28 +887,32 @@ static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *
 {
 	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
 	struct work *tmp;
-	WORKTASK data;
+	KLINE kline;
 
 	if (klncgpu->usbinfo.nodev)
 		return false;
 
-	memcpy(data.midstate, work->midstate, MIDSTATE_BYTES);
-	memcpy(data.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
-	data.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
-	work->subid = dev*256 + data.workid;
+	kline.wt.cmd = 'W';
+	kline.wt.dev = dev;
+	memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
+	memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
+	kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
+	work->subid = dev*256 + kline.wt.workid;
 
 	if (opt_log_level <= LOG_DEBUG) {
-		char *hexdata = bin2hex(&data.workid, sizeof(data)-3);
+		char *hexdata = bin2hex((void *)&kline.wt, sizeof(kline.wt));
 		applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
 		free(hexdata);
 	}
 
-	applog(LOG_DEBUG, "Klondike sending work (%d:%02x)", dev, data.workid);
-	char *reply = SendCmdGetReply(klncgpu, 'W', dev, sizeof(data)-3, &data.workid);
-	if (reply != NULL) {
+	applog(LOG_DEBUG, "Klondike sending work (%d:%02x)", dev, kline.wt.workid);
+	KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
+	if (kitem != NULL) {
 		wr_lock(&(klninfo->stat_lock));
-		klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
+		memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(*kitem));
 		wr_unlock(&(klninfo->stat_lock));
+		release_kitem(klncgpu, kitem);
+		kitem = NULL;
 
 		// remove old work
 		HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
@@ -593,11 +928,12 @@ static bool klondike_queue_full(struct cgpu_info *klncgpu)
 {
 	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
 	struct work *work = NULL;
-	int dev, queued;
+	int dev, queued, slaves;
 
+	slaves = klninfo->status[0].kline.ws.slavecount;
 	for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
-		for (dev = 0; dev <= klninfo->status->slavecount; dev++)
-			if (klninfo->status[dev].workqc <= queued) {
+		for (dev = 0; dev <= slaves; dev++)
+			if (klninfo->status[dev].kline.ws.workqc <= queued) {
 				if (!work)
 					work = get_queued(klncgpu);
 				if (unlikely(!work))
@@ -616,7 +952,7 @@ static int64_t klondike_scanwork(struct thr_info *thr)
 	struct cgpu_info *klncgpu = thr->cgpu;
 	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
 	int64_t newhashcount = 0;
-	int dev;
+	int dev, slaves;
 
 	if (klncgpu->usbinfo.nodev)
 		return -1;
@@ -624,14 +960,19 @@ static int64_t klondike_scanwork(struct thr_info *thr)
 	restart_wait(thr, 200);
 	if (klninfo->status != NULL) {
 		rd_lock(&(klninfo->stat_lock));
-		for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
-			uint64_t newhashdev = 0;
-			if (klninfo->devinfo[dev].lasthashcount > klninfo->status[dev].hashcount) // todo: chg this to check workid for wrapped instead
-				newhashdev += klninfo->status[dev].maxcount; // hash counter wrapped
-			newhashdev += klninfo->status[dev].hashcount - klninfo->devinfo[dev].lasthashcount;
-			klninfo->devinfo[dev].lasthashcount = klninfo->status[dev].hashcount;
-			if (klninfo->status[dev].maxcount != 0)
-				klninfo->hashcount += (newhashdev << 32) / klninfo->status[dev].maxcount;
+		slaves = klninfo->status[0].kline.ws.slavecount;
+		for (dev = 0; dev <= slaves; dev++) {
+			uint64_t newhashdev = 0, hashcount;
+			int maxcount;
+
+			hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
+			maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
+			if (klninfo->devinfo[dev].lasthashcount > hashcount) // todo: chg this to check workid for wrapped instead
+				newhashdev += maxcount; // hash counter wrapped
+			newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
+			klninfo->devinfo[dev].lasthashcount = hashcount;
+			if (maxcount != 0)
+				klninfo->hashcount += (newhashdev << 32) / maxcount;
 
 			// todo: check stats for critical conditions
 		}
@@ -649,21 +990,24 @@ static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info
 	uint8_t temp = 0xFF;
 	uint16_t fan = 0;
 	uint16_t clock = 0;
-	int dev;
+	int dev, slaves;
 	char tmp[16];
 
-	if (klninfo->status == NULL)
+	if (klninfo->status == NULL) {
+		blank_get_statline_before(buf, siz, klncgpu);
 		return;
+	}
 
 	rd_lock(&(klninfo->stat_lock));
-	for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
-		if (klninfo->status[dev].temp < temp)
-			temp = klninfo->status[dev].temp;
-		fan += klninfo->cfg[dev].fantarget;
-		clock += klninfo->cfg[dev].hashclock;
+	slaves = klninfo->status[0].kline.ws.slavecount;
+	for (dev = 0; dev <= slaves; dev++) {
+		if (klninfo->status[dev].kline.ws.temp < temp)
+			temp = klninfo->status[dev].kline.ws.temp;
+		fan += klninfo->cfg[dev].kline.cfg.fantarget;
+		clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
 	}
-	fan /= klninfo->status->slavecount+1;
-	clock /= klninfo->status->slavecount+1;
+	fan /= slaves + 1;
+	clock /= slaves + 1;
 	rd_unlock(&(klninfo->stat_lock));
 
 	snprintf(tmp, sizeof(tmp), "%2.0fC", cvtKlnToC(temp));
@@ -678,29 +1022,30 @@ static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
 	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
 	struct api_data *root = NULL;
 	char buf[32];
-	int dev;
+	int dev, slaves;
 
 	if (klninfo->status == NULL)
 		return NULL;
 
 	rd_lock(&(klninfo->stat_lock));
-	for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
+	slaves = klninfo->status[0].kline.ws.slavecount;
+	for (dev = 0; dev <= slaves; dev++) {
 
-		float fTemp = cvtKlnToC(klninfo->status[dev].temp);
+		float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
 		sprintf(buf, "Temp %d", dev);
 		root = api_add_temp(root, buf, &fTemp, true);
 
-		double dClk = (double)klninfo->cfg[dev].hashclock;
+		double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
 		sprintf(buf, "Clock %d", dev);
 		root = api_add_freq(root, buf, &dClk, true);
 
-		unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].fantarget / 255;
+		unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
 		sprintf(buf, "Fan Percent %d", dev);
 		root = api_add_int(root, buf, (int *)(&iFan), true);
 
 		iFan = 0;
-		if (klninfo->status[dev].fanspeed > 0)
-			iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].fanspeed;
+		if (klninfo->status[dev].kline.ws.fanspeed > 0)
+			iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
 		sprintf(buf, "Fan RPM %d", dev);
 		root = api_add_int(root, buf, (int *)(&iFan), true);
 
@@ -711,7 +1056,7 @@ static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
 
 			sprintf(buf, "Nonces / Chip %d", dev);
 			data[0] = '\0';
-			for (n = 0; n < klninfo->status[dev].chipcount; n++) {
+			for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
 				snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
 				strcat(data, one);
 			}
@@ -719,8 +1064,8 @@ static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
 
 			sprintf(buf, "Errors / Chip %d", dev);
 			data[0] = '\0';
-			for (n = 0; n < klninfo->status[dev].chipcount; n++) {
-				snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].chipcount]);
+			for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
+				snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
 				strcat(data, one);
 			}
 			root = api_add_string(root, buf, data, true);
@@ -731,6 +1076,30 @@ static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
 	root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
 	root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
 
+	root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
+	root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
+
+	root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
+	root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
+	root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
+	root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
+	double avg;
+	if (klninfo->delay_count == 0)
+		avg = 0;
+	else
+		avg = klninfo->delay_total / klninfo->delay_count;
+	root = api_add_diff(root, "KQue Delay Avg", &avg, true);
+
+	root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
+	root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
+	root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
+	root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
+	if (klninfo->nonce_count == 0)
+		avg = 0;
+	else
+		avg = klninfo->nonce_total / klninfo->nonce_count;
+	root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
+
 	rd_unlock(&(klninfo->stat_lock));
 
 	return root;
diff --git a/usbutils.c b/usbutils.c
index 2a6a978..963796b 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -2229,15 +2229,13 @@ static char *find_end(unsigned char *buf, unsigned char *ptr, int ptrlen, int to
 #define USB_RETRY_MAX 5
 
 struct usb_transfer {
-	pthread_mutex_t mutex;
-	pthread_cond_t cond;
+	cgsem_t cgsem;
 	struct libusb_transfer *transfer;
 };
 
 static void init_usb_transfer(struct usb_transfer *ut)
 {
-	mutex_init(&ut->mutex);
-	pthread_cond_init(&ut->cond, NULL);
+	cgsem_init(&ut->cgsem);
 	ut->transfer = libusb_alloc_transfer(0);
 	if (unlikely(!ut->transfer))
 		quit(1, "Failed to libusb_alloc_transfer");
@@ -2248,7 +2246,7 @@ static void LIBUSB_CALL transfer_callback(struct libusb_transfer *transfer)
 {
 	struct usb_transfer *ut = transfer->user_data;
 
-	pthread_cond_signal(&ut->cond);
+	cgsem_post(&ut->cgsem);
 }
 
 /* Wait for callback function to tell us it has finished the USB transfer, but
@@ -2256,21 +2254,15 @@ static void LIBUSB_CALL transfer_callback(struct libusb_transfer *transfer)
 static int callback_wait(struct usb_transfer *ut, int *transferred, unsigned int timeout)
 {
 	struct libusb_transfer *transfer= ut->transfer;
-	struct timespec ts_now, ts_end;
-	struct timeval tv_now;
 	int ret;
 
-	cgtime(&tv_now);
-	ms_to_timespec(&ts_end, timeout);
-	timeval_to_spec(&ts_now, &tv_now);
-	timeraddspec(&ts_end, &ts_now);
-	ret = pthread_cond_timedwait(&ut->cond, &ut->mutex, &ts_end);
-	if (ret) {
+	ret = cgsem_mswait(&ut->cgsem, timeout);
+	if (ret == ETIMEDOUT) {
 		/* We are emulating a timeout ourself here */
 		libusb_cancel_transfer(transfer);
 
 		/* Now wait for the callback function to be invoked. */
-		pthread_cond_wait(&ut->cond, &ut->mutex);
+		cgsem_wait(&ut->cgsem);
 	}
 	ret = transfer->status;
 	if (ret == LIBUSB_TRANSFER_CANCELLED)
@@ -2316,7 +2308,6 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 	USBDEBUG("USB debug: @usb_bulk_transfer(%s (nodev=%s),intinfo=%d,epinfo=%d,data=%p,length=%d,timeout=%u,mode=%d,cmd=%s,seq=%d) endpoint=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), intinfo, epinfo, data, length, timeout, mode, usb_cmdname(cmd), seq, (int)endpoint);
 
 	init_usb_transfer(&ut);
-	mutex_lock(&ut.mutex);
 	/* We give the transfer no timeout since we manage timeouts ourself */
 	libusb_fill_bulk_transfer(ut.transfer, dev_handle, endpoint, buf, length,
 				  transfer_callback, &ut, 0);
@@ -2749,7 +2740,6 @@ static int usb_control_transfer(libusb_device_handle *dev_handle, uint8_t bmRequ
 	int err, transferred;
 
 	init_usb_transfer(&ut);
-	mutex_lock(&ut.mutex);
 	libusb_fill_control_setup(buf, bmRequestType, bRequest, wValue,
 				  wIndex, wLength);
 	libusb_fill_control_transfer(ut.transfer, dev_handle, buf, transfer_callback,
diff --git a/util.c b/util.c
index 8e03281..8235b1c 100644
--- a/util.c
+++ b/util.c
@@ -881,6 +881,14 @@ void ms_to_timespec(struct timespec *spec, int64_t ms)
 	spec->tv_nsec = tvdiv.rem * 1000000;
 }
 
+void ms_to_timeval(struct timeval *val, int64_t ms)
+{
+	lldiv_t tvdiv = lldiv(ms, 1000);
+
+	val->tv_sec = tvdiv.quot;
+	val->tv_usec = tvdiv.rem * 1000;
+}
+
 void timeraddspec(struct timespec *a, const struct timespec *b)
 {
 	a->tv_sec += b->tv_sec;
@@ -2407,6 +2415,31 @@ void _cgsem_destroy(cgsem_t *cgsem)
 	close(cgsem->pipefd[1]);
 	close(cgsem->pipefd[0]);
 }
+
+/* This is similar to sem_timedwait but takes a millisecond value */
+int _cgsem_mswait(cgsem_t *cgsem, int ms, const char *file, const char *func, const int line)
+{
+	struct timeval timeout;
+	int ret, fd;
+	fd_set rd;
+	char buf;
+
+	fd = cgsem->pipefd[0];
+	FD_ZERO(&rd);
+	FD_SET(fd, &rd);
+	ms_to_timeval(&timeout, ms);
+	ret = select(fd + 1, &rd, NULL, NULL, &timeout);
+
+	if (ret > 0) {
+		ret = read(fd, &buf, 1);
+		return 0;
+	}
+	if (likely(!ret))
+		return ETIMEDOUT;
+	quitfrom(1, file, func, line, "Failed to sem_timedwait errno=%d cgsem=0x%p", errno, cgsem);
+	/* We don't reach here */
+	return 0;
+}
 #else
 void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int line)
 {
@@ -2427,6 +2460,26 @@ void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int l
 		quitfrom(1, file, func, line, "Failed to sem_wait errno=%d cgsem=0x%p", errno, cgsem);
 }
 
+int _cgsem_mswait(cgsem_t *cgsem, int ms, const char *file, const char *func, const int line)
+{
+	struct timespec abs_timeout, ts_now;
+	struct timeval tv_now;
+	int ret;
+
+	cgtime(&tv_now);
+	timeval_to_spec(&ts_now, &tv_now);
+	ms_to_timespec(&abs_timeout, ms);
+	timeraddspec(&abs_timeout, &ts_now);
+	ret = sem_timedwait(cgsem, &abs_timeout);
+
+	if (ret) {
+		if (likely(sock_timeout()))
+			return ETIMEDOUT;
+		quitfrom(1, file, func, line, "Failed to sem_timedwait errno=%d cgsem=0x%p", errno, cgsem);
+	}
+	return 0;
+}
+
 void _cgsem_destroy(cgsem_t *cgsem)
 {
 	sem_destroy(cgsem);
diff --git a/util.h b/util.h
index f0ee041..3f4b1c3 100644
--- a/util.h
+++ b/util.h
@@ -20,6 +20,10 @@
 	{
 		return (errno == EAGAIN || errno == EWOULDBLOCK);
 	}
+	static inline bool sock_timeout(void)
+	{
+		return (errno == ETIMEDOUT);
+	}
 #elif defined WIN32
 	#include <ws2tcpip.h>
 	#include <winsock2.h>
@@ -37,6 +41,10 @@
 	{
 		return (WSAGetLastError() == WSAEWOULDBLOCK);
 	}
+	static inline bool sock_timeout(void)
+	{
+		return (errno == WSAETIMEDOUT);
+	}
 	#ifndef SHUT_RDWR
 	#define SHUT_RDWR SD_BOTH
 	#endif
@@ -125,11 +133,13 @@ void RenameThread(const char* name);
 void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int line);
 void _cgsem_post(cgsem_t *cgsem, const char *file, const char *func, const int line);
 void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int line);
+int _cgsem_mswait(cgsem_t *cgsem, int ms, const char *file, const char *func, const int line);
 void _cgsem_destroy(cgsem_t *cgsem);
 
 #define cgsem_init(_sem) _cgsem_init(_sem, __FILE__, __func__, __LINE__)
 #define cgsem_post(_sem) _cgsem_post(_sem, __FILE__, __func__, __LINE__)
 #define cgsem_wait(_sem) _cgsem_wait(_sem, __FILE__, __func__, __LINE__)
+#define cgsem_mswait(_sem, _timeout) _cgsem_mswait(_sem, _timeout, __FILE__, __func__, __LINE__)
 #define cgsem_destroy(_sem) _cgsem_destroy(_sem)
 
 /* Align a size_t to 4 byte boundaries for fussy arches */